xfs_ialloc.c 41 KB

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