xfs_ialloc.c 42 KB

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