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