xfs_ialloc.c 45 KB

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