xfs_ialloc.c 44 KB

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