xfs_ialloc.c 42 KB

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