xfs_ialloc.c 44 KB

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