xfs_ialloc.c 43 KB

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