xfs_ialloc.c 42 KB

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