xfs_ialloc.c 42 KB

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