xfs_ialloc.c 43 KB

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