xfs_ialloc.c 41 KB

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