xfs_ialloc.c 42 KB

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