xfs_ialloc.c 41 KB

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