xfs_ialloc.c 40 KB

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