xfs_ialloc.c 45 KB

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