xfs_ialloc.c 42 KB

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