xfs_ialloc.c 43 KB

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