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