xfs_ialloc.c 45 KB

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