xfs_ialloc.c 44 KB

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