xfs_ialloc.c 45 KB

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