xfs_ialloc.c 41 KB

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