xfs_ialloc.c 42 KB

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