xfs_ialloc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_rtalloc.h"
  41. #include "xfs_error.h"
  42. #include "xfs_bmap.h"
  43. /*
  44. * Log specified fields for the inode given by bp and off.
  45. */
  46. STATIC void
  47. xfs_ialloc_log_di(
  48. xfs_trans_t *tp, /* transaction pointer */
  49. xfs_buf_t *bp, /* inode buffer */
  50. int off, /* index of inode in buffer */
  51. int fields) /* bitmask of fields to log */
  52. {
  53. int first; /* first byte number */
  54. int ioffset; /* off in bytes */
  55. int last; /* last byte number */
  56. xfs_mount_t *mp; /* mount point structure */
  57. static const short offsets[] = { /* field offsets */
  58. /* keep in sync with bits */
  59. offsetof(xfs_dinode_core_t, di_magic),
  60. offsetof(xfs_dinode_core_t, di_mode),
  61. offsetof(xfs_dinode_core_t, di_version),
  62. offsetof(xfs_dinode_core_t, di_format),
  63. offsetof(xfs_dinode_core_t, di_onlink),
  64. offsetof(xfs_dinode_core_t, di_uid),
  65. offsetof(xfs_dinode_core_t, di_gid),
  66. offsetof(xfs_dinode_core_t, di_nlink),
  67. offsetof(xfs_dinode_core_t, di_projid),
  68. offsetof(xfs_dinode_core_t, di_pad),
  69. offsetof(xfs_dinode_core_t, di_atime),
  70. offsetof(xfs_dinode_core_t, di_mtime),
  71. offsetof(xfs_dinode_core_t, di_ctime),
  72. offsetof(xfs_dinode_core_t, di_size),
  73. offsetof(xfs_dinode_core_t, di_nblocks),
  74. offsetof(xfs_dinode_core_t, di_extsize),
  75. offsetof(xfs_dinode_core_t, di_nextents),
  76. offsetof(xfs_dinode_core_t, di_anextents),
  77. offsetof(xfs_dinode_core_t, di_forkoff),
  78. offsetof(xfs_dinode_core_t, di_aformat),
  79. offsetof(xfs_dinode_core_t, di_dmevmask),
  80. offsetof(xfs_dinode_core_t, di_dmstate),
  81. offsetof(xfs_dinode_core_t, di_flags),
  82. offsetof(xfs_dinode_core_t, di_gen),
  83. offsetof(xfs_dinode_t, di_next_unlinked),
  84. offsetof(xfs_dinode_t, di_u),
  85. offsetof(xfs_dinode_t, di_a),
  86. sizeof(xfs_dinode_t)
  87. };
  88. ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
  89. ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
  90. mp = tp->t_mountp;
  91. /*
  92. * Get the inode-relative first and last bytes for these fields
  93. */
  94. xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
  95. /*
  96. * Convert to buffer offsets and log it.
  97. */
  98. ioffset = off << mp->m_sb.sb_inodelog;
  99. first += ioffset;
  100. last += ioffset;
  101. xfs_trans_log_buf(tp, bp, first, last);
  102. }
  103. /*
  104. * Allocation group level functions.
  105. */
  106. /*
  107. * Allocate new inodes in the allocation group specified by agbp.
  108. * Return 0 for success, else error code.
  109. */
  110. STATIC int /* error code or 0 */
  111. xfs_ialloc_ag_alloc(
  112. xfs_trans_t *tp, /* transaction pointer */
  113. xfs_buf_t *agbp, /* alloc group buffer */
  114. int *alloc)
  115. {
  116. xfs_agi_t *agi; /* allocation group header */
  117. xfs_alloc_arg_t args; /* allocation argument structure */
  118. int blks_per_cluster; /* fs blocks per inode cluster */
  119. xfs_btree_cur_t *cur; /* inode btree cursor */
  120. xfs_daddr_t d; /* disk addr of buffer */
  121. int error;
  122. xfs_buf_t *fbuf; /* new free inodes' buffer */
  123. xfs_dinode_t *free; /* new free inode structure */
  124. int i; /* inode counter */
  125. int j; /* block counter */
  126. int nbufs; /* num bufs of new inodes */
  127. xfs_agino_t newino; /* new first inode's number */
  128. xfs_agino_t newlen; /* new number of inodes */
  129. int ninodes; /* num inodes per buf */
  130. xfs_agino_t thisino; /* current inode number, for loop */
  131. int version; /* inode version number to use */
  132. int isaligned = 0; /* inode allocation at stripe unit */
  133. /* boundary */
  134. args.tp = tp;
  135. args.mp = tp->t_mountp;
  136. /*
  137. * Locking will ensure that we don't have two callers in here
  138. * at one time.
  139. */
  140. newlen = XFS_IALLOC_INODES(args.mp);
  141. if (args.mp->m_maxicount &&
  142. args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
  143. return XFS_ERROR(ENOSPC);
  144. args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
  145. /*
  146. * First try to allocate inodes contiguous with the last-allocated
  147. * chunk of inodes. If the filesystem is striped, this will fill
  148. * an entire stripe unit with inodes.
  149. */
  150. agi = XFS_BUF_TO_AGI(agbp);
  151. newino = be32_to_cpu(agi->agi_newino);
  152. args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
  153. XFS_IALLOC_BLOCKS(args.mp);
  154. if (likely(newino != NULLAGINO &&
  155. (args.agbno < be32_to_cpu(agi->agi_length)))) {
  156. args.fsbno = XFS_AGB_TO_FSB(args.mp,
  157. be32_to_cpu(agi->agi_seqno), args.agbno);
  158. args.type = XFS_ALLOCTYPE_THIS_BNO;
  159. args.mod = args.total = args.wasdel = args.isfl =
  160. args.userdata = args.minalignslop = 0;
  161. args.prod = 1;
  162. args.alignment = 1;
  163. /*
  164. * Allow space for the inode btree to split.
  165. */
  166. args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
  167. if ((error = xfs_alloc_vextent(&args)))
  168. return error;
  169. } else
  170. args.fsbno = NULLFSBLOCK;
  171. if (unlikely(args.fsbno == NULLFSBLOCK)) {
  172. /*
  173. * Set the alignment for the allocation.
  174. * If stripe alignment is turned on then align at stripe unit
  175. * boundary.
  176. * If the cluster size is smaller than a filesystem block
  177. * then we're doing I/O for inodes in filesystem block size
  178. * pieces, so don't need alignment anyway.
  179. */
  180. isaligned = 0;
  181. if (args.mp->m_sinoalign) {
  182. ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
  183. args.alignment = args.mp->m_dalign;
  184. isaligned = 1;
  185. } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
  186. args.mp->m_sb.sb_inoalignmt >=
  187. XFS_B_TO_FSBT(args.mp,
  188. XFS_INODE_CLUSTER_SIZE(args.mp)))
  189. args.alignment = args.mp->m_sb.sb_inoalignmt;
  190. else
  191. args.alignment = 1;
  192. /*
  193. * Need to figure out where to allocate the inode blocks.
  194. * Ideally they should be spaced out through the a.g.
  195. * For now, just allocate blocks up front.
  196. */
  197. args.agbno = be32_to_cpu(agi->agi_root);
  198. args.fsbno = XFS_AGB_TO_FSB(args.mp,
  199. be32_to_cpu(agi->agi_seqno), args.agbno);
  200. /*
  201. * Allocate a fixed-size extent of inodes.
  202. */
  203. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  204. args.mod = args.total = args.wasdel = args.isfl =
  205. args.userdata = args.minalignslop = 0;
  206. args.prod = 1;
  207. /*
  208. * Allow space for the inode btree to split.
  209. */
  210. args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
  211. if ((error = xfs_alloc_vextent(&args)))
  212. return error;
  213. }
  214. /*
  215. * If stripe alignment is turned on, then try again with cluster
  216. * alignment.
  217. */
  218. if (isaligned && args.fsbno == NULLFSBLOCK) {
  219. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  220. args.agbno = be32_to_cpu(agi->agi_root);
  221. args.fsbno = XFS_AGB_TO_FSB(args.mp,
  222. be32_to_cpu(agi->agi_seqno), args.agbno);
  223. if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
  224. args.mp->m_sb.sb_inoalignmt >=
  225. XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
  226. args.alignment = args.mp->m_sb.sb_inoalignmt;
  227. else
  228. args.alignment = 1;
  229. if ((error = xfs_alloc_vextent(&args)))
  230. return error;
  231. }
  232. if (args.fsbno == NULLFSBLOCK) {
  233. *alloc = 0;
  234. return 0;
  235. }
  236. ASSERT(args.len == args.minlen);
  237. /*
  238. * Convert the results.
  239. */
  240. newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
  241. /*
  242. * Loop over the new block(s), filling in the inodes.
  243. * For small block sizes, manipulate the inodes in buffers
  244. * which are multiples of the blocks size.
  245. */
  246. if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
  247. blks_per_cluster = 1;
  248. nbufs = (int)args.len;
  249. ninodes = args.mp->m_sb.sb_inopblock;
  250. } else {
  251. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
  252. args.mp->m_sb.sb_blocksize;
  253. nbufs = (int)args.len / blks_per_cluster;
  254. ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
  255. }
  256. /*
  257. * Figure out what version number to use in the inodes we create.
  258. * If the superblock version has caught up to the one that supports
  259. * the new inode format, then use the new inode version. Otherwise
  260. * use the old version so that old kernels will continue to be
  261. * able to use the file system.
  262. */
  263. if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
  264. version = XFS_DINODE_VERSION_2;
  265. else
  266. version = XFS_DINODE_VERSION_1;
  267. for (j = 0; j < nbufs; j++) {
  268. /*
  269. * Get the block.
  270. */
  271. d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
  272. args.agbno + (j * blks_per_cluster));
  273. fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
  274. args.mp->m_bsize * blks_per_cluster,
  275. XFS_BUF_LOCK);
  276. ASSERT(fbuf);
  277. ASSERT(!XFS_BUF_GETERROR(fbuf));
  278. /*
  279. * Set initial values for the inodes in this buffer.
  280. */
  281. xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
  282. for (i = 0; i < ninodes; i++) {
  283. free = XFS_MAKE_IPTR(args.mp, fbuf, i);
  284. INT_SET(free->di_core.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
  285. INT_SET(free->di_core.di_version, ARCH_CONVERT, version);
  286. INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
  287. xfs_ialloc_log_di(tp, fbuf, i,
  288. XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
  289. }
  290. xfs_trans_inode_alloc_buf(tp, fbuf);
  291. }
  292. be32_add(&agi->agi_count, newlen);
  293. be32_add(&agi->agi_freecount, newlen);
  294. down_read(&args.mp->m_peraglock);
  295. args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen;
  296. up_read(&args.mp->m_peraglock);
  297. agi->agi_newino = cpu_to_be32(newino);
  298. /*
  299. * Insert records describing the new inode chunk into the btree.
  300. */
  301. cur = xfs_btree_init_cursor(args.mp, tp, agbp,
  302. be32_to_cpu(agi->agi_seqno),
  303. XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
  304. for (thisino = newino;
  305. thisino < newino + newlen;
  306. thisino += XFS_INODES_PER_CHUNK) {
  307. if ((error = xfs_inobt_lookup_eq(cur, thisino,
  308. XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
  309. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  310. return error;
  311. }
  312. ASSERT(i == 0);
  313. if ((error = xfs_inobt_insert(cur, &i))) {
  314. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  315. return error;
  316. }
  317. ASSERT(i == 1);
  318. }
  319. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  320. /*
  321. * Log allocation group header fields
  322. */
  323. xfs_ialloc_log_agi(tp, agbp,
  324. XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
  325. /*
  326. * Modify/log superblock values for inode count and inode free count.
  327. */
  328. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
  329. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
  330. *alloc = 1;
  331. return 0;
  332. }
  333. STATIC_INLINE xfs_agnumber_t
  334. xfs_ialloc_next_ag(
  335. xfs_mount_t *mp)
  336. {
  337. xfs_agnumber_t agno;
  338. spin_lock(&mp->m_agirotor_lock);
  339. agno = mp->m_agirotor;
  340. if (++mp->m_agirotor == mp->m_maxagi)
  341. mp->m_agirotor = 0;
  342. spin_unlock(&mp->m_agirotor_lock);
  343. return agno;
  344. }
  345. /*
  346. * Select an allocation group to look for a free inode in, based on the parent
  347. * inode and then mode. Return the allocation group buffer.
  348. */
  349. STATIC xfs_buf_t * /* allocation group buffer */
  350. xfs_ialloc_ag_select(
  351. xfs_trans_t *tp, /* transaction pointer */
  352. xfs_ino_t parent, /* parent directory inode number */
  353. mode_t mode, /* bits set to indicate file type */
  354. int okalloc) /* ok to allocate more space */
  355. {
  356. xfs_buf_t *agbp; /* allocation group header buffer */
  357. xfs_agnumber_t agcount; /* number of ag's in the filesystem */
  358. xfs_agnumber_t agno; /* current ag number */
  359. int flags; /* alloc buffer locking flags */
  360. xfs_extlen_t ineed; /* blocks needed for inode allocation */
  361. xfs_extlen_t longest = 0; /* longest extent available */
  362. xfs_mount_t *mp; /* mount point structure */
  363. int needspace; /* file mode implies space allocated */
  364. xfs_perag_t *pag; /* per allocation group data */
  365. xfs_agnumber_t pagno; /* parent (starting) ag number */
  366. /*
  367. * Files of these types need at least one block if length > 0
  368. * (and they won't fit in the inode, but that's hard to figure out).
  369. */
  370. needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
  371. mp = tp->t_mountp;
  372. agcount = mp->m_maxagi;
  373. if (S_ISDIR(mode))
  374. pagno = xfs_ialloc_next_ag(mp);
  375. else {
  376. pagno = XFS_INO_TO_AGNO(mp, parent);
  377. if (pagno >= agcount)
  378. pagno = 0;
  379. }
  380. ASSERT(pagno < agcount);
  381. /*
  382. * Loop through allocation groups, looking for one with a little
  383. * free space in it. Note we don't look for free inodes, exactly.
  384. * Instead, we include whether there is a need to allocate inodes
  385. * to mean that blocks must be allocated for them,
  386. * if none are currently free.
  387. */
  388. agno = pagno;
  389. flags = XFS_ALLOC_FLAG_TRYLOCK;
  390. down_read(&mp->m_peraglock);
  391. for (;;) {
  392. pag = &mp->m_perag[agno];
  393. if (!pag->pagi_init) {
  394. if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
  395. agbp = NULL;
  396. goto nextag;
  397. }
  398. } else
  399. agbp = NULL;
  400. if (!pag->pagi_inodeok) {
  401. xfs_ialloc_next_ag(mp);
  402. goto unlock_nextag;
  403. }
  404. /*
  405. * Is there enough free space for the file plus a block
  406. * of inodes (if we need to allocate some)?
  407. */
  408. ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
  409. if (ineed && !pag->pagf_init) {
  410. if (agbp == NULL &&
  411. xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
  412. agbp = NULL;
  413. goto nextag;
  414. }
  415. (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
  416. }
  417. if (!ineed || pag->pagf_init) {
  418. if (ineed && !(longest = pag->pagf_longest))
  419. longest = pag->pagf_flcount > 0;
  420. if (!ineed ||
  421. (pag->pagf_freeblks >= needspace + ineed &&
  422. longest >= ineed &&
  423. okalloc)) {
  424. if (agbp == NULL &&
  425. xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
  426. agbp = NULL;
  427. goto nextag;
  428. }
  429. up_read(&mp->m_peraglock);
  430. return agbp;
  431. }
  432. }
  433. unlock_nextag:
  434. if (agbp)
  435. xfs_trans_brelse(tp, agbp);
  436. nextag:
  437. /*
  438. * No point in iterating over the rest, if we're shutting
  439. * down.
  440. */
  441. if (XFS_FORCED_SHUTDOWN(mp)) {
  442. up_read(&mp->m_peraglock);
  443. return NULL;
  444. }
  445. agno++;
  446. if (agno >= agcount)
  447. agno = 0;
  448. if (agno == pagno) {
  449. if (flags == 0) {
  450. up_read(&mp->m_peraglock);
  451. return NULL;
  452. }
  453. flags = 0;
  454. }
  455. }
  456. }
  457. /*
  458. * Visible inode allocation functions.
  459. */
  460. /*
  461. * Allocate an inode on disk.
  462. * Mode is used to tell whether the new inode will need space, and whether
  463. * it is a directory.
  464. *
  465. * The arguments IO_agbp and alloc_done are defined to work within
  466. * the constraint of one allocation per transaction.
  467. * xfs_dialloc() is designed to be called twice if it has to do an
  468. * allocation to make more free inodes. On the first call,
  469. * IO_agbp should be set to NULL. If an inode is available,
  470. * i.e., xfs_dialloc() did not need to do an allocation, an inode
  471. * number is returned. In this case, IO_agbp would be set to the
  472. * current ag_buf and alloc_done set to false.
  473. * If an allocation needed to be done, xfs_dialloc would return
  474. * the current ag_buf in IO_agbp and set alloc_done to true.
  475. * The caller should then commit the current transaction, allocate a new
  476. * transaction, and call xfs_dialloc() again, passing in the previous
  477. * value of IO_agbp. IO_agbp should be held across the transactions.
  478. * Since the agbp is locked across the two calls, the second call is
  479. * guaranteed to have a free inode available.
  480. *
  481. * Once we successfully pick an inode its number is returned and the
  482. * on-disk data structures are updated. The inode itself is not read
  483. * in, since doing so would break ordering constraints with xfs_reclaim.
  484. */
  485. int
  486. xfs_dialloc(
  487. xfs_trans_t *tp, /* transaction pointer */
  488. xfs_ino_t parent, /* parent inode (directory) */
  489. mode_t mode, /* mode bits for new inode */
  490. int okalloc, /* ok to allocate more space */
  491. xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
  492. boolean_t *alloc_done, /* true if we needed to replenish
  493. inode freelist */
  494. xfs_ino_t *inop) /* inode number allocated */
  495. {
  496. xfs_agnumber_t agcount; /* number of allocation groups */
  497. xfs_buf_t *agbp; /* allocation group header's buffer */
  498. xfs_agnumber_t agno; /* allocation group number */
  499. xfs_agi_t *agi; /* allocation group header structure */
  500. xfs_btree_cur_t *cur; /* inode allocation btree cursor */
  501. int error; /* error return value */
  502. int i; /* result code */
  503. int ialloced; /* inode allocation status */
  504. int noroom = 0; /* no space for inode blk allocation */
  505. xfs_ino_t ino; /* fs-relative inode to be returned */
  506. /* REFERENCED */
  507. int j; /* result code */
  508. xfs_mount_t *mp; /* file system mount structure */
  509. int offset; /* index of inode in chunk */
  510. xfs_agino_t pagino; /* parent's a.g. relative inode # */
  511. xfs_agnumber_t pagno; /* parent's allocation group number */
  512. xfs_inobt_rec_incore_t rec; /* inode allocation record */
  513. xfs_agnumber_t tagno; /* testing allocation group number */
  514. xfs_btree_cur_t *tcur; /* temp cursor */
  515. xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
  516. if (*IO_agbp == NULL) {
  517. /*
  518. * We do not have an agbp, so select an initial allocation
  519. * group for inode allocation.
  520. */
  521. agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
  522. /*
  523. * Couldn't find an allocation group satisfying the
  524. * criteria, give up.
  525. */
  526. if (!agbp) {
  527. *inop = NULLFSINO;
  528. return 0;
  529. }
  530. agi = XFS_BUF_TO_AGI(agbp);
  531. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  532. } else {
  533. /*
  534. * Continue where we left off before. In this case, we
  535. * know that the allocation group has free inodes.
  536. */
  537. agbp = *IO_agbp;
  538. agi = XFS_BUF_TO_AGI(agbp);
  539. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  540. ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
  541. }
  542. mp = tp->t_mountp;
  543. agcount = mp->m_sb.sb_agcount;
  544. agno = be32_to_cpu(agi->agi_seqno);
  545. tagno = agno;
  546. pagno = XFS_INO_TO_AGNO(mp, parent);
  547. pagino = XFS_INO_TO_AGINO(mp, parent);
  548. /*
  549. * If we have already hit the ceiling of inode blocks then clear
  550. * okalloc so we scan all available agi structures for a free
  551. * inode.
  552. */
  553. if (mp->m_maxicount &&
  554. mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
  555. noroom = 1;
  556. okalloc = 0;
  557. }
  558. /*
  559. * Loop until we find an allocation group that either has free inodes
  560. * or in which we can allocate some inodes. Iterate through the
  561. * allocation groups upward, wrapping at the end.
  562. */
  563. *alloc_done = B_FALSE;
  564. while (!agi->agi_freecount) {
  565. /*
  566. * Don't do anything if we're not supposed to allocate
  567. * any blocks, just go on to the next ag.
  568. */
  569. if (okalloc) {
  570. /*
  571. * Try to allocate some new inodes in the allocation
  572. * group.
  573. */
  574. if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
  575. xfs_trans_brelse(tp, agbp);
  576. if (error == ENOSPC) {
  577. *inop = NULLFSINO;
  578. return 0;
  579. } else
  580. return error;
  581. }
  582. if (ialloced) {
  583. /*
  584. * We successfully allocated some inodes, return
  585. * the current context to the caller so that it
  586. * can commit the current transaction and call
  587. * us again where we left off.
  588. */
  589. ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
  590. *alloc_done = B_TRUE;
  591. *IO_agbp = agbp;
  592. *inop = NULLFSINO;
  593. return 0;
  594. }
  595. }
  596. /*
  597. * If it failed, give up on this ag.
  598. */
  599. xfs_trans_brelse(tp, agbp);
  600. /*
  601. * Go on to the next ag: get its ag header.
  602. */
  603. nextag:
  604. if (++tagno == agcount)
  605. tagno = 0;
  606. if (tagno == agno) {
  607. *inop = NULLFSINO;
  608. return noroom ? ENOSPC : 0;
  609. }
  610. down_read(&mp->m_peraglock);
  611. if (mp->m_perag[tagno].pagi_inodeok == 0) {
  612. up_read(&mp->m_peraglock);
  613. goto nextag;
  614. }
  615. error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
  616. up_read(&mp->m_peraglock);
  617. if (error)
  618. goto nextag;
  619. agi = XFS_BUF_TO_AGI(agbp);
  620. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  621. }
  622. /*
  623. * Here with an allocation group that has a free inode.
  624. * Reset agno since we may have chosen a new ag in the
  625. * loop above.
  626. */
  627. agno = tagno;
  628. *IO_agbp = NULL;
  629. cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
  630. XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
  631. /*
  632. * If pagino is 0 (this is the root inode allocation) use newino.
  633. * This must work because we've just allocated some.
  634. */
  635. if (!pagino)
  636. pagino = be32_to_cpu(agi->agi_newino);
  637. #ifdef DEBUG
  638. if (cur->bc_nlevels == 1) {
  639. int freecount = 0;
  640. if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
  641. goto error0;
  642. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  643. do {
  644. if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
  645. &rec.ir_freecount, &rec.ir_free, &i)))
  646. goto error0;
  647. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  648. freecount += rec.ir_freecount;
  649. if ((error = xfs_inobt_increment(cur, 0, &i)))
  650. goto error0;
  651. } while (i == 1);
  652. ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
  653. XFS_FORCED_SHUTDOWN(mp));
  654. }
  655. #endif
  656. /*
  657. * If in the same a.g. as the parent, try to get near the parent.
  658. */
  659. if (pagno == agno) {
  660. if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
  661. goto error0;
  662. if (i != 0 &&
  663. (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
  664. &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
  665. j == 1 &&
  666. rec.ir_freecount > 0) {
  667. /*
  668. * Found a free inode in the same chunk
  669. * as parent, done.
  670. */
  671. }
  672. /*
  673. * In the same a.g. as parent, but parent's chunk is full.
  674. */
  675. else {
  676. int doneleft; /* done, to the left */
  677. int doneright; /* done, to the right */
  678. if (error)
  679. goto error0;
  680. ASSERT(i == 1);
  681. ASSERT(j == 1);
  682. /*
  683. * Duplicate the cursor, search left & right
  684. * simultaneously.
  685. */
  686. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  687. goto error0;
  688. /*
  689. * Search left with tcur, back up 1 record.
  690. */
  691. if ((error = xfs_inobt_decrement(tcur, 0, &i)))
  692. goto error1;
  693. doneleft = !i;
  694. if (!doneleft) {
  695. if ((error = xfs_inobt_get_rec(tcur,
  696. &trec.ir_startino,
  697. &trec.ir_freecount,
  698. &trec.ir_free, &i)))
  699. goto error1;
  700. XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
  701. }
  702. /*
  703. * Search right with cur, go forward 1 record.
  704. */
  705. if ((error = xfs_inobt_increment(cur, 0, &i)))
  706. goto error1;
  707. doneright = !i;
  708. if (!doneright) {
  709. if ((error = xfs_inobt_get_rec(cur,
  710. &rec.ir_startino,
  711. &rec.ir_freecount,
  712. &rec.ir_free, &i)))
  713. goto error1;
  714. XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
  715. }
  716. /*
  717. * Loop until we find the closest inode chunk
  718. * with a free one.
  719. */
  720. while (!doneleft || !doneright) {
  721. int useleft; /* using left inode
  722. chunk this time */
  723. /*
  724. * Figure out which block is closer,
  725. * if both are valid.
  726. */
  727. if (!doneleft && !doneright)
  728. useleft =
  729. pagino -
  730. (trec.ir_startino +
  731. XFS_INODES_PER_CHUNK - 1) <
  732. rec.ir_startino - pagino;
  733. else
  734. useleft = !doneleft;
  735. /*
  736. * If checking the left, does it have
  737. * free inodes?
  738. */
  739. if (useleft && trec.ir_freecount) {
  740. /*
  741. * Yes, set it up as the chunk to use.
  742. */
  743. rec = trec;
  744. xfs_btree_del_cursor(cur,
  745. XFS_BTREE_NOERROR);
  746. cur = tcur;
  747. break;
  748. }
  749. /*
  750. * If checking the right, does it have
  751. * free inodes?
  752. */
  753. if (!useleft && rec.ir_freecount) {
  754. /*
  755. * Yes, it's already set up.
  756. */
  757. xfs_btree_del_cursor(tcur,
  758. XFS_BTREE_NOERROR);
  759. break;
  760. }
  761. /*
  762. * If used the left, get another one
  763. * further left.
  764. */
  765. if (useleft) {
  766. if ((error = xfs_inobt_decrement(tcur, 0,
  767. &i)))
  768. goto error1;
  769. doneleft = !i;
  770. if (!doneleft) {
  771. if ((error = xfs_inobt_get_rec(
  772. tcur,
  773. &trec.ir_startino,
  774. &trec.ir_freecount,
  775. &trec.ir_free, &i)))
  776. goto error1;
  777. XFS_WANT_CORRUPTED_GOTO(i == 1,
  778. error1);
  779. }
  780. }
  781. /*
  782. * If used the right, get another one
  783. * further right.
  784. */
  785. else {
  786. if ((error = xfs_inobt_increment(cur, 0,
  787. &i)))
  788. goto error1;
  789. doneright = !i;
  790. if (!doneright) {
  791. if ((error = xfs_inobt_get_rec(
  792. cur,
  793. &rec.ir_startino,
  794. &rec.ir_freecount,
  795. &rec.ir_free, &i)))
  796. goto error1;
  797. XFS_WANT_CORRUPTED_GOTO(i == 1,
  798. error1);
  799. }
  800. }
  801. }
  802. ASSERT(!doneleft || !doneright);
  803. }
  804. }
  805. /*
  806. * In a different a.g. from the parent.
  807. * See if the most recently allocated block has any free.
  808. */
  809. else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
  810. if ((error = xfs_inobt_lookup_eq(cur,
  811. be32_to_cpu(agi->agi_newino), 0, 0, &i)))
  812. goto error0;
  813. if (i == 1 &&
  814. (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
  815. &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
  816. j == 1 &&
  817. rec.ir_freecount > 0) {
  818. /*
  819. * The last chunk allocated in the group still has
  820. * a free inode.
  821. */
  822. }
  823. /*
  824. * None left in the last group, search the whole a.g.
  825. */
  826. else {
  827. if (error)
  828. goto error0;
  829. if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
  830. goto error0;
  831. ASSERT(i == 1);
  832. for (;;) {
  833. if ((error = xfs_inobt_get_rec(cur,
  834. &rec.ir_startino,
  835. &rec.ir_freecount, &rec.ir_free,
  836. &i)))
  837. goto error0;
  838. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  839. if (rec.ir_freecount > 0)
  840. break;
  841. if ((error = xfs_inobt_increment(cur, 0, &i)))
  842. goto error0;
  843. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  844. }
  845. }
  846. }
  847. offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
  848. ASSERT(offset >= 0);
  849. ASSERT(offset < XFS_INODES_PER_CHUNK);
  850. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  851. XFS_INODES_PER_CHUNK) == 0);
  852. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  853. XFS_INOBT_CLR_FREE(&rec, offset);
  854. rec.ir_freecount--;
  855. if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
  856. rec.ir_free)))
  857. goto error0;
  858. be32_add(&agi->agi_freecount, -1);
  859. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  860. down_read(&mp->m_peraglock);
  861. mp->m_perag[tagno].pagi_freecount--;
  862. up_read(&mp->m_peraglock);
  863. #ifdef DEBUG
  864. if (cur->bc_nlevels == 1) {
  865. int freecount = 0;
  866. if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
  867. goto error0;
  868. do {
  869. if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
  870. &rec.ir_freecount, &rec.ir_free, &i)))
  871. goto error0;
  872. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  873. freecount += rec.ir_freecount;
  874. if ((error = xfs_inobt_increment(cur, 0, &i)))
  875. goto error0;
  876. } while (i == 1);
  877. ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
  878. XFS_FORCED_SHUTDOWN(mp));
  879. }
  880. #endif
  881. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  882. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  883. *inop = ino;
  884. return 0;
  885. error1:
  886. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  887. error0:
  888. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  889. return error;
  890. }
  891. /*
  892. * Free disk inode. Carefully avoids touching the incore inode, all
  893. * manipulations incore are the caller's responsibility.
  894. * The on-disk inode is not changed by this operation, only the
  895. * btree (free inode mask) is changed.
  896. */
  897. int
  898. xfs_difree(
  899. xfs_trans_t *tp, /* transaction pointer */
  900. xfs_ino_t inode, /* inode to be freed */
  901. xfs_bmap_free_t *flist, /* extents to free */
  902. int *delete, /* set if inode cluster was deleted */
  903. xfs_ino_t *first_ino) /* first inode in deleted cluster */
  904. {
  905. /* REFERENCED */
  906. xfs_agblock_t agbno; /* block number containing inode */
  907. xfs_buf_t *agbp; /* buffer containing allocation group header */
  908. xfs_agino_t agino; /* inode number relative to allocation group */
  909. xfs_agnumber_t agno; /* allocation group number */
  910. xfs_agi_t *agi; /* allocation group header */
  911. xfs_btree_cur_t *cur; /* inode btree cursor */
  912. int error; /* error return value */
  913. int i; /* result code */
  914. int ilen; /* inodes in an inode cluster */
  915. xfs_mount_t *mp; /* mount structure for filesystem */
  916. int off; /* offset of inode in inode chunk */
  917. xfs_inobt_rec_incore_t rec; /* btree record */
  918. mp = tp->t_mountp;
  919. /*
  920. * Break up inode number into its components.
  921. */
  922. agno = XFS_INO_TO_AGNO(mp, inode);
  923. if (agno >= mp->m_sb.sb_agcount) {
  924. cmn_err(CE_WARN,
  925. "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
  926. agno, mp->m_sb.sb_agcount, mp->m_fsname);
  927. ASSERT(0);
  928. return XFS_ERROR(EINVAL);
  929. }
  930. agino = XFS_INO_TO_AGINO(mp, inode);
  931. if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
  932. cmn_err(CE_WARN,
  933. "xfs_difree: inode != XFS_AGINO_TO_INO() "
  934. "(%llu != %llu) on %s. Returning EINVAL.",
  935. (unsigned long long)inode,
  936. (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
  937. mp->m_fsname);
  938. ASSERT(0);
  939. return XFS_ERROR(EINVAL);
  940. }
  941. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  942. if (agbno >= mp->m_sb.sb_agblocks) {
  943. cmn_err(CE_WARN,
  944. "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
  945. agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
  946. ASSERT(0);
  947. return XFS_ERROR(EINVAL);
  948. }
  949. /*
  950. * Get the allocation group header.
  951. */
  952. down_read(&mp->m_peraglock);
  953. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  954. up_read(&mp->m_peraglock);
  955. if (error) {
  956. cmn_err(CE_WARN,
  957. "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
  958. error, mp->m_fsname);
  959. return error;
  960. }
  961. agi = XFS_BUF_TO_AGI(agbp);
  962. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  963. ASSERT(agbno < be32_to_cpu(agi->agi_length));
  964. /*
  965. * Initialize the cursor.
  966. */
  967. cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
  968. (xfs_inode_t *)0, 0);
  969. #ifdef DEBUG
  970. if (cur->bc_nlevels == 1) {
  971. int freecount = 0;
  972. if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
  973. goto error0;
  974. do {
  975. if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
  976. &rec.ir_freecount, &rec.ir_free, &i)))
  977. goto error0;
  978. if (i) {
  979. freecount += rec.ir_freecount;
  980. if ((error = xfs_inobt_increment(cur, 0, &i)))
  981. goto error0;
  982. }
  983. } while (i == 1);
  984. ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
  985. XFS_FORCED_SHUTDOWN(mp));
  986. }
  987. #endif
  988. /*
  989. * Look for the entry describing this inode.
  990. */
  991. if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
  992. cmn_err(CE_WARN,
  993. "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
  994. error, mp->m_fsname);
  995. goto error0;
  996. }
  997. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  998. if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
  999. &rec.ir_free, &i))) {
  1000. cmn_err(CE_WARN,
  1001. "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
  1002. error, mp->m_fsname);
  1003. goto error0;
  1004. }
  1005. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1006. /*
  1007. * Get the offset in the inode chunk.
  1008. */
  1009. off = agino - rec.ir_startino;
  1010. ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
  1011. ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
  1012. /*
  1013. * Mark the inode free & increment the count.
  1014. */
  1015. XFS_INOBT_SET_FREE(&rec, off);
  1016. rec.ir_freecount++;
  1017. /*
  1018. * When an inode cluster is free, it becomes eligible for removal
  1019. */
  1020. if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
  1021. (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
  1022. *delete = 1;
  1023. *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
  1024. /*
  1025. * Remove the inode cluster from the AGI B+Tree, adjust the
  1026. * AGI and Superblock inode counts, and mark the disk space
  1027. * to be freed when the transaction is committed.
  1028. */
  1029. ilen = XFS_IALLOC_INODES(mp);
  1030. be32_add(&agi->agi_count, -ilen);
  1031. be32_add(&agi->agi_freecount, -(ilen - 1));
  1032. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
  1033. down_read(&mp->m_peraglock);
  1034. mp->m_perag[agno].pagi_freecount -= ilen - 1;
  1035. up_read(&mp->m_peraglock);
  1036. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
  1037. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
  1038. if ((error = xfs_inobt_delete(cur, &i))) {
  1039. cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
  1040. error, mp->m_fsname);
  1041. goto error0;
  1042. }
  1043. xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
  1044. agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
  1045. XFS_IALLOC_BLOCKS(mp), flist, mp);
  1046. } else {
  1047. *delete = 0;
  1048. if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
  1049. cmn_err(CE_WARN,
  1050. "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
  1051. error, mp->m_fsname);
  1052. goto error0;
  1053. }
  1054. /*
  1055. * Change the inode free counts and log the ag/sb changes.
  1056. */
  1057. be32_add(&agi->agi_freecount, 1);
  1058. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1059. down_read(&mp->m_peraglock);
  1060. mp->m_perag[agno].pagi_freecount++;
  1061. up_read(&mp->m_peraglock);
  1062. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
  1063. }
  1064. #ifdef DEBUG
  1065. if (cur->bc_nlevels == 1) {
  1066. int freecount = 0;
  1067. if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
  1068. goto error0;
  1069. do {
  1070. if ((error = xfs_inobt_get_rec(cur,
  1071. &rec.ir_startino,
  1072. &rec.ir_freecount,
  1073. &rec.ir_free, &i)))
  1074. goto error0;
  1075. if (i) {
  1076. freecount += rec.ir_freecount;
  1077. if ((error = xfs_inobt_increment(cur, 0, &i)))
  1078. goto error0;
  1079. }
  1080. } while (i == 1);
  1081. ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
  1082. XFS_FORCED_SHUTDOWN(mp));
  1083. }
  1084. #endif
  1085. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1086. return 0;
  1087. error0:
  1088. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1089. return error;
  1090. }
  1091. /*
  1092. * Return the location of the inode in bno/off, for mapping it into a buffer.
  1093. */
  1094. /*ARGSUSED*/
  1095. int
  1096. xfs_dilocate(
  1097. xfs_mount_t *mp, /* file system mount structure */
  1098. xfs_trans_t *tp, /* transaction pointer */
  1099. xfs_ino_t ino, /* inode to locate */
  1100. xfs_fsblock_t *bno, /* output: block containing inode */
  1101. int *len, /* output: num blocks in inode cluster */
  1102. int *off, /* output: index in block of inode */
  1103. uint flags) /* flags concerning inode lookup */
  1104. {
  1105. xfs_agblock_t agbno; /* block number of inode in the alloc group */
  1106. xfs_buf_t *agbp; /* agi buffer */
  1107. xfs_agino_t agino; /* inode number within alloc group */
  1108. xfs_agnumber_t agno; /* allocation group number */
  1109. int blks_per_cluster; /* num blocks per inode cluster */
  1110. xfs_agblock_t chunk_agbno; /* first block in inode chunk */
  1111. xfs_agino_t chunk_agino; /* first agino in inode chunk */
  1112. __int32_t chunk_cnt; /* count of free inodes in chunk */
  1113. xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
  1114. xfs_agblock_t cluster_agbno; /* first block in inode cluster */
  1115. xfs_btree_cur_t *cur; /* inode btree cursor */
  1116. int error; /* error code */
  1117. int i; /* temp state */
  1118. int offset; /* index of inode in its buffer */
  1119. int offset_agbno; /* blks from chunk start to inode */
  1120. ASSERT(ino != NULLFSINO);
  1121. /*
  1122. * Split up the inode number into its parts.
  1123. */
  1124. agno = XFS_INO_TO_AGNO(mp, ino);
  1125. agino = XFS_INO_TO_AGINO(mp, ino);
  1126. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1127. if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
  1128. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1129. #ifdef DEBUG
  1130. /* no diagnostics for bulkstat, ino comes from userspace */
  1131. if (flags & XFS_IMAP_BULKSTAT)
  1132. return XFS_ERROR(EINVAL);
  1133. if (agno >= mp->m_sb.sb_agcount) {
  1134. xfs_fs_cmn_err(CE_ALERT, mp,
  1135. "xfs_dilocate: agno (%d) >= "
  1136. "mp->m_sb.sb_agcount (%d)",
  1137. agno, mp->m_sb.sb_agcount);
  1138. }
  1139. if (agbno >= mp->m_sb.sb_agblocks) {
  1140. xfs_fs_cmn_err(CE_ALERT, mp,
  1141. "xfs_dilocate: agbno (0x%llx) >= "
  1142. "mp->m_sb.sb_agblocks (0x%lx)",
  1143. (unsigned long long) agbno,
  1144. (unsigned long) mp->m_sb.sb_agblocks);
  1145. }
  1146. if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1147. xfs_fs_cmn_err(CE_ALERT, mp,
  1148. "xfs_dilocate: ino (0x%llx) != "
  1149. "XFS_AGINO_TO_INO(mp, agno, agino) "
  1150. "(0x%llx)",
  1151. ino, XFS_AGINO_TO_INO(mp, agno, agino));
  1152. }
  1153. xfs_stack_trace();
  1154. #endif /* DEBUG */
  1155. return XFS_ERROR(EINVAL);
  1156. }
  1157. if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
  1158. !(flags & XFS_IMAP_LOOKUP)) {
  1159. offset = XFS_INO_TO_OFFSET(mp, ino);
  1160. ASSERT(offset < mp->m_sb.sb_inopblock);
  1161. *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
  1162. *off = offset;
  1163. *len = 1;
  1164. return 0;
  1165. }
  1166. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
  1167. if (*bno != NULLFSBLOCK) {
  1168. offset = XFS_INO_TO_OFFSET(mp, ino);
  1169. ASSERT(offset < mp->m_sb.sb_inopblock);
  1170. cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
  1171. *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
  1172. offset;
  1173. *len = blks_per_cluster;
  1174. return 0;
  1175. }
  1176. if (mp->m_inoalign_mask) {
  1177. offset_agbno = agbno & mp->m_inoalign_mask;
  1178. chunk_agbno = agbno - offset_agbno;
  1179. } else {
  1180. down_read(&mp->m_peraglock);
  1181. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1182. up_read(&mp->m_peraglock);
  1183. if (error) {
  1184. #ifdef DEBUG
  1185. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
  1186. "xfs_ialloc_read_agi() returned "
  1187. "error %d, agno %d",
  1188. error, agno);
  1189. #endif /* DEBUG */
  1190. return error;
  1191. }
  1192. cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
  1193. (xfs_inode_t *)0, 0);
  1194. if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
  1195. #ifdef DEBUG
  1196. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
  1197. "xfs_inobt_lookup_le() failed");
  1198. #endif /* DEBUG */
  1199. goto error0;
  1200. }
  1201. if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
  1202. &chunk_free, &i))) {
  1203. #ifdef DEBUG
  1204. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
  1205. "xfs_inobt_get_rec() failed");
  1206. #endif /* DEBUG */
  1207. goto error0;
  1208. }
  1209. if (i == 0) {
  1210. #ifdef DEBUG
  1211. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
  1212. "xfs_inobt_get_rec() failed");
  1213. #endif /* DEBUG */
  1214. error = XFS_ERROR(EINVAL);
  1215. }
  1216. xfs_trans_brelse(tp, agbp);
  1217. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1218. if (error)
  1219. return error;
  1220. chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
  1221. offset_agbno = agbno - chunk_agbno;
  1222. }
  1223. ASSERT(agbno >= chunk_agbno);
  1224. cluster_agbno = chunk_agbno +
  1225. ((offset_agbno / blks_per_cluster) * blks_per_cluster);
  1226. offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
  1227. XFS_INO_TO_OFFSET(mp, ino);
  1228. *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
  1229. *off = offset;
  1230. *len = blks_per_cluster;
  1231. return 0;
  1232. error0:
  1233. xfs_trans_brelse(tp, agbp);
  1234. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1235. return error;
  1236. }
  1237. /*
  1238. * Compute and fill in value of m_in_maxlevels.
  1239. */
  1240. void
  1241. xfs_ialloc_compute_maxlevels(
  1242. xfs_mount_t *mp) /* file system mount structure */
  1243. {
  1244. int level;
  1245. uint maxblocks;
  1246. uint maxleafents;
  1247. int minleafrecs;
  1248. int minnoderecs;
  1249. maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
  1250. XFS_INODES_PER_CHUNK_LOG;
  1251. minleafrecs = mp->m_alloc_mnr[0];
  1252. minnoderecs = mp->m_alloc_mnr[1];
  1253. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1254. for (level = 1; maxblocks > 1; level++)
  1255. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1256. mp->m_in_maxlevels = level;
  1257. }
  1258. /*
  1259. * Log specified fields for the ag hdr (inode section)
  1260. */
  1261. void
  1262. xfs_ialloc_log_agi(
  1263. xfs_trans_t *tp, /* transaction pointer */
  1264. xfs_buf_t *bp, /* allocation group header buffer */
  1265. int fields) /* bitmask of fields to log */
  1266. {
  1267. int first; /* first byte number */
  1268. int last; /* last byte number */
  1269. static const short offsets[] = { /* field starting offsets */
  1270. /* keep in sync with bit definitions */
  1271. offsetof(xfs_agi_t, agi_magicnum),
  1272. offsetof(xfs_agi_t, agi_versionnum),
  1273. offsetof(xfs_agi_t, agi_seqno),
  1274. offsetof(xfs_agi_t, agi_length),
  1275. offsetof(xfs_agi_t, agi_count),
  1276. offsetof(xfs_agi_t, agi_root),
  1277. offsetof(xfs_agi_t, agi_level),
  1278. offsetof(xfs_agi_t, agi_freecount),
  1279. offsetof(xfs_agi_t, agi_newino),
  1280. offsetof(xfs_agi_t, agi_dirino),
  1281. offsetof(xfs_agi_t, agi_unlinked),
  1282. sizeof(xfs_agi_t)
  1283. };
  1284. #ifdef DEBUG
  1285. xfs_agi_t *agi; /* allocation group header */
  1286. agi = XFS_BUF_TO_AGI(bp);
  1287. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  1288. #endif
  1289. /*
  1290. * Compute byte offsets for the first and last fields.
  1291. */
  1292. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
  1293. /*
  1294. * Log the allocation group inode header buffer.
  1295. */
  1296. xfs_trans_log_buf(tp, bp, first, last);
  1297. }
  1298. /*
  1299. * Read in the allocation group header (inode allocation section)
  1300. */
  1301. int
  1302. xfs_ialloc_read_agi(
  1303. xfs_mount_t *mp, /* file system mount structure */
  1304. xfs_trans_t *tp, /* transaction pointer */
  1305. xfs_agnumber_t agno, /* allocation group number */
  1306. xfs_buf_t **bpp) /* allocation group hdr buf */
  1307. {
  1308. xfs_agi_t *agi; /* allocation group header */
  1309. int agi_ok; /* agi is consistent */
  1310. xfs_buf_t *bp; /* allocation group hdr buf */
  1311. xfs_perag_t *pag; /* per allocation group data */
  1312. int error;
  1313. ASSERT(agno != NULLAGNUMBER);
  1314. error = xfs_trans_read_buf(
  1315. mp, tp, mp->m_ddev_targp,
  1316. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  1317. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  1318. if (error)
  1319. return error;
  1320. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  1321. /*
  1322. * Validate the magic number of the agi block.
  1323. */
  1324. agi = XFS_BUF_TO_AGI(bp);
  1325. agi_ok =
  1326. be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
  1327. XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
  1328. if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
  1329. XFS_RANDOM_IALLOC_READ_AGI))) {
  1330. XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
  1331. mp, agi);
  1332. xfs_trans_brelse(tp, bp);
  1333. return XFS_ERROR(EFSCORRUPTED);
  1334. }
  1335. pag = &mp->m_perag[agno];
  1336. if (!pag->pagi_init) {
  1337. pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
  1338. pag->pagi_init = 1;
  1339. } else {
  1340. /*
  1341. * It's possible for these to be out of sync if
  1342. * we are in the middle of a forced shutdown.
  1343. */
  1344. ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
  1345. XFS_FORCED_SHUTDOWN(mp));
  1346. }
  1347. #ifdef DEBUG
  1348. {
  1349. int i;
  1350. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
  1351. ASSERT(agi->agi_unlinked[i]);
  1352. }
  1353. #endif
  1354. XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
  1355. *bpp = bp;
  1356. return 0;
  1357. }