xfs_ialloc.c 43 KB

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