xfs_ialloc.c 45 KB

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