xfs_ialloc.c 41 KB

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