xfs_ialloc.c 42 KB

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