xfs_ialloc.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. * Visible inode allocation functions.
  555. */
  556. /*
  557. * Allocate an inode on disk.
  558. * Mode is used to tell whether the new inode will need space, and whether
  559. * it is a directory.
  560. *
  561. * The arguments IO_agbp and alloc_done are defined to work within
  562. * the constraint of one allocation per transaction.
  563. * xfs_dialloc() is designed to be called twice if it has to do an
  564. * allocation to make more free inodes. On the first call,
  565. * IO_agbp should be set to NULL. If an inode is available,
  566. * i.e., xfs_dialloc() did not need to do an allocation, an inode
  567. * number is returned. In this case, IO_agbp would be set to the
  568. * current ag_buf and alloc_done set to false.
  569. * If an allocation needed to be done, xfs_dialloc would return
  570. * the current ag_buf in IO_agbp and set alloc_done to true.
  571. * The caller should then commit the current transaction, allocate a new
  572. * transaction, and call xfs_dialloc() again, passing in the previous
  573. * value of IO_agbp. IO_agbp should be held across the transactions.
  574. * Since the agbp is locked across the two calls, the second call is
  575. * guaranteed to have a free inode available.
  576. *
  577. * Once we successfully pick an inode its number is returned and the
  578. * on-disk data structures are updated. The inode itself is not read
  579. * in, since doing so would break ordering constraints with xfs_reclaim.
  580. */
  581. int
  582. xfs_dialloc(
  583. xfs_trans_t *tp, /* transaction pointer */
  584. xfs_ino_t parent, /* parent inode (directory) */
  585. mode_t mode, /* mode bits for new inode */
  586. int okalloc, /* ok to allocate more space */
  587. xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
  588. boolean_t *alloc_done, /* true if we needed to replenish
  589. inode freelist */
  590. xfs_ino_t *inop) /* inode number allocated */
  591. {
  592. xfs_agnumber_t agcount; /* number of allocation groups */
  593. xfs_buf_t *agbp; /* allocation group header's buffer */
  594. xfs_agnumber_t agno; /* allocation group number */
  595. xfs_agi_t *agi; /* allocation group header structure */
  596. xfs_btree_cur_t *cur; /* inode allocation btree cursor */
  597. int error; /* error return value */
  598. int i; /* result code */
  599. int ialloced; /* inode allocation status */
  600. int noroom = 0; /* no space for inode blk allocation */
  601. xfs_ino_t ino; /* fs-relative inode to be returned */
  602. /* REFERENCED */
  603. int j; /* result code */
  604. xfs_mount_t *mp; /* file system mount structure */
  605. int offset; /* index of inode in chunk */
  606. xfs_agino_t pagino; /* parent's a.g. relative inode # */
  607. xfs_agnumber_t pagno; /* parent's allocation group number */
  608. xfs_inobt_rec_incore_t rec; /* inode allocation record */
  609. xfs_agnumber_t tagno; /* testing allocation group number */
  610. xfs_btree_cur_t *tcur; /* temp cursor */
  611. xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
  612. if (*IO_agbp == NULL) {
  613. /*
  614. * We do not have an agbp, so select an initial allocation
  615. * group for inode allocation.
  616. */
  617. agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
  618. /*
  619. * Couldn't find an allocation group satisfying the
  620. * criteria, give up.
  621. */
  622. if (!agbp) {
  623. *inop = NULLFSINO;
  624. return 0;
  625. }
  626. agi = XFS_BUF_TO_AGI(agbp);
  627. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  628. } else {
  629. /*
  630. * Continue where we left off before. In this case, we
  631. * know that the allocation group has free inodes.
  632. */
  633. agbp = *IO_agbp;
  634. agi = XFS_BUF_TO_AGI(agbp);
  635. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  636. ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
  637. }
  638. mp = tp->t_mountp;
  639. agcount = mp->m_sb.sb_agcount;
  640. agno = be32_to_cpu(agi->agi_seqno);
  641. tagno = agno;
  642. pagno = XFS_INO_TO_AGNO(mp, parent);
  643. pagino = XFS_INO_TO_AGINO(mp, parent);
  644. /*
  645. * If we have already hit the ceiling of inode blocks then clear
  646. * okalloc so we scan all available agi structures for a free
  647. * inode.
  648. */
  649. if (mp->m_maxicount &&
  650. mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
  651. noroom = 1;
  652. okalloc = 0;
  653. }
  654. /*
  655. * Loop until we find an allocation group that either has free inodes
  656. * or in which we can allocate some inodes. Iterate through the
  657. * allocation groups upward, wrapping at the end.
  658. */
  659. *alloc_done = B_FALSE;
  660. while (!agi->agi_freecount) {
  661. /*
  662. * Don't do anything if we're not supposed to allocate
  663. * any blocks, just go on to the next ag.
  664. */
  665. if (okalloc) {
  666. /*
  667. * Try to allocate some new inodes in the allocation
  668. * group.
  669. */
  670. if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
  671. xfs_trans_brelse(tp, agbp);
  672. if (error == ENOSPC) {
  673. *inop = NULLFSINO;
  674. return 0;
  675. } else
  676. return error;
  677. }
  678. if (ialloced) {
  679. /*
  680. * We successfully allocated some inodes, return
  681. * the current context to the caller so that it
  682. * can commit the current transaction and call
  683. * us again where we left off.
  684. */
  685. ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
  686. *alloc_done = B_TRUE;
  687. *IO_agbp = agbp;
  688. *inop = NULLFSINO;
  689. return 0;
  690. }
  691. }
  692. /*
  693. * If it failed, give up on this ag.
  694. */
  695. xfs_trans_brelse(tp, agbp);
  696. /*
  697. * Go on to the next ag: get its ag header.
  698. */
  699. nextag:
  700. if (++tagno == agcount)
  701. tagno = 0;
  702. if (tagno == agno) {
  703. *inop = NULLFSINO;
  704. return noroom ? ENOSPC : 0;
  705. }
  706. down_read(&mp->m_peraglock);
  707. if (mp->m_perag[tagno].pagi_inodeok == 0) {
  708. up_read(&mp->m_peraglock);
  709. goto nextag;
  710. }
  711. error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
  712. up_read(&mp->m_peraglock);
  713. if (error)
  714. goto nextag;
  715. agi = XFS_BUF_TO_AGI(agbp);
  716. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  717. }
  718. /*
  719. * Here with an allocation group that has a free inode.
  720. * Reset agno since we may have chosen a new ag in the
  721. * loop above.
  722. */
  723. agno = tagno;
  724. *IO_agbp = NULL;
  725. cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
  726. /*
  727. * If pagino is 0 (this is the root inode allocation) use newino.
  728. * This must work because we've just allocated some.
  729. */
  730. if (!pagino)
  731. pagino = be32_to_cpu(agi->agi_newino);
  732. error = xfs_check_agi_freecount(cur, agi);
  733. if (error)
  734. goto error0;
  735. /*
  736. * If in the same a.g. as the parent, try to get near the parent.
  737. */
  738. if (pagno == agno) {
  739. if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
  740. goto error0;
  741. if (i != 0 &&
  742. (error = xfs_inobt_get_rec(cur, &rec, &j)) == 0 &&
  743. j == 1 &&
  744. rec.ir_freecount > 0) {
  745. /*
  746. * Found a free inode in the same chunk
  747. * as parent, done.
  748. */
  749. }
  750. /*
  751. * In the same a.g. as parent, but parent's chunk is full.
  752. */
  753. else {
  754. int doneleft; /* done, to the left */
  755. int doneright; /* done, to the right */
  756. if (error)
  757. goto error0;
  758. ASSERT(i == 1);
  759. ASSERT(j == 1);
  760. /*
  761. * Duplicate the cursor, search left & right
  762. * simultaneously.
  763. */
  764. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  765. goto error0;
  766. /*
  767. * Search left with tcur, back up 1 record.
  768. */
  769. if ((error = xfs_btree_decrement(tcur, 0, &i)))
  770. goto error1;
  771. doneleft = !i;
  772. if (!doneleft) {
  773. error = xfs_inobt_get_rec(tcur, &trec, &i);
  774. if (error)
  775. goto error1;
  776. XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
  777. }
  778. /*
  779. * Search right with cur, go forward 1 record.
  780. */
  781. if ((error = xfs_btree_increment(cur, 0, &i)))
  782. goto error1;
  783. doneright = !i;
  784. if (!doneright) {
  785. error = xfs_inobt_get_rec(cur, &rec, &i);
  786. if (error)
  787. goto error1;
  788. XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
  789. }
  790. /*
  791. * Loop until we find the closest inode chunk
  792. * with a free one.
  793. */
  794. while (!doneleft || !doneright) {
  795. int useleft; /* using left inode
  796. chunk this time */
  797. /*
  798. * Figure out which block is closer,
  799. * if both are valid.
  800. */
  801. if (!doneleft && !doneright)
  802. useleft =
  803. pagino -
  804. (trec.ir_startino +
  805. XFS_INODES_PER_CHUNK - 1) <
  806. rec.ir_startino - pagino;
  807. else
  808. useleft = !doneleft;
  809. /*
  810. * If checking the left, does it have
  811. * free inodes?
  812. */
  813. if (useleft && trec.ir_freecount) {
  814. /*
  815. * Yes, set it up as the chunk to use.
  816. */
  817. rec = trec;
  818. xfs_btree_del_cursor(cur,
  819. XFS_BTREE_NOERROR);
  820. cur = tcur;
  821. break;
  822. }
  823. /*
  824. * If checking the right, does it have
  825. * free inodes?
  826. */
  827. if (!useleft && rec.ir_freecount) {
  828. /*
  829. * Yes, it's already set up.
  830. */
  831. xfs_btree_del_cursor(tcur,
  832. XFS_BTREE_NOERROR);
  833. break;
  834. }
  835. /*
  836. * If used the left, get another one
  837. * further left.
  838. */
  839. if (useleft) {
  840. if ((error = xfs_btree_decrement(tcur, 0,
  841. &i)))
  842. goto error1;
  843. doneleft = !i;
  844. if (!doneleft) {
  845. error = xfs_inobt_get_rec(
  846. tcur, &trec, &i);
  847. if (error)
  848. goto error1;
  849. XFS_WANT_CORRUPTED_GOTO(i == 1,
  850. error1);
  851. }
  852. }
  853. /*
  854. * If used the right, get another one
  855. * further right.
  856. */
  857. else {
  858. if ((error = xfs_btree_increment(cur, 0,
  859. &i)))
  860. goto error1;
  861. doneright = !i;
  862. if (!doneright) {
  863. error = xfs_inobt_get_rec(
  864. cur, &rec, &i);
  865. if (error)
  866. goto error1;
  867. XFS_WANT_CORRUPTED_GOTO(i == 1,
  868. error1);
  869. }
  870. }
  871. }
  872. ASSERT(!doneleft || !doneright);
  873. }
  874. }
  875. /*
  876. * In a different a.g. from the parent.
  877. * See if the most recently allocated block has any free.
  878. */
  879. else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
  880. if ((error = xfs_inobt_lookup_eq(cur,
  881. be32_to_cpu(agi->agi_newino), 0, 0, &i)))
  882. goto error0;
  883. if (i == 1 &&
  884. (error = xfs_inobt_get_rec(cur, &rec, &j)) == 0 &&
  885. j == 1 &&
  886. rec.ir_freecount > 0) {
  887. /*
  888. * The last chunk allocated in the group still has
  889. * a free inode.
  890. */
  891. }
  892. /*
  893. * None left in the last group, search the whole a.g.
  894. */
  895. else {
  896. if (error)
  897. goto error0;
  898. if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
  899. goto error0;
  900. ASSERT(i == 1);
  901. for (;;) {
  902. error = xfs_inobt_get_rec(cur, &rec, &i);
  903. if (error)
  904. goto error0;
  905. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  906. if (rec.ir_freecount > 0)
  907. break;
  908. if ((error = xfs_btree_increment(cur, 0, &i)))
  909. goto error0;
  910. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  911. }
  912. }
  913. }
  914. offset = xfs_ialloc_find_free(&rec.ir_free);
  915. ASSERT(offset >= 0);
  916. ASSERT(offset < XFS_INODES_PER_CHUNK);
  917. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  918. XFS_INODES_PER_CHUNK) == 0);
  919. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  920. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  921. rec.ir_freecount--;
  922. error = xfs_inobt_update(cur, &rec);
  923. if (error)
  924. goto error0;
  925. be32_add_cpu(&agi->agi_freecount, -1);
  926. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  927. down_read(&mp->m_peraglock);
  928. mp->m_perag[tagno].pagi_freecount--;
  929. up_read(&mp->m_peraglock);
  930. error = xfs_check_agi_freecount(cur, agi);
  931. if (error)
  932. goto error0;
  933. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  934. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  935. *inop = ino;
  936. return 0;
  937. error1:
  938. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  939. error0:
  940. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  941. return error;
  942. }
  943. /*
  944. * Free disk inode. Carefully avoids touching the incore inode, all
  945. * manipulations incore are the caller's responsibility.
  946. * The on-disk inode is not changed by this operation, only the
  947. * btree (free inode mask) is changed.
  948. */
  949. int
  950. xfs_difree(
  951. xfs_trans_t *tp, /* transaction pointer */
  952. xfs_ino_t inode, /* inode to be freed */
  953. xfs_bmap_free_t *flist, /* extents to free */
  954. int *delete, /* set if inode cluster was deleted */
  955. xfs_ino_t *first_ino) /* first inode in deleted cluster */
  956. {
  957. /* REFERENCED */
  958. xfs_agblock_t agbno; /* block number containing inode */
  959. xfs_buf_t *agbp; /* buffer containing allocation group header */
  960. xfs_agino_t agino; /* inode number relative to allocation group */
  961. xfs_agnumber_t agno; /* allocation group number */
  962. xfs_agi_t *agi; /* allocation group header */
  963. xfs_btree_cur_t *cur; /* inode btree cursor */
  964. int error; /* error return value */
  965. int i; /* result code */
  966. int ilen; /* inodes in an inode cluster */
  967. xfs_mount_t *mp; /* mount structure for filesystem */
  968. int off; /* offset of inode in inode chunk */
  969. xfs_inobt_rec_incore_t rec; /* btree record */
  970. mp = tp->t_mountp;
  971. /*
  972. * Break up inode number into its components.
  973. */
  974. agno = XFS_INO_TO_AGNO(mp, inode);
  975. if (agno >= mp->m_sb.sb_agcount) {
  976. cmn_err(CE_WARN,
  977. "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
  978. agno, mp->m_sb.sb_agcount, mp->m_fsname);
  979. ASSERT(0);
  980. return XFS_ERROR(EINVAL);
  981. }
  982. agino = XFS_INO_TO_AGINO(mp, inode);
  983. if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
  984. cmn_err(CE_WARN,
  985. "xfs_difree: inode != XFS_AGINO_TO_INO() "
  986. "(%llu != %llu) on %s. Returning EINVAL.",
  987. (unsigned long long)inode,
  988. (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
  989. mp->m_fsname);
  990. ASSERT(0);
  991. return XFS_ERROR(EINVAL);
  992. }
  993. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  994. if (agbno >= mp->m_sb.sb_agblocks) {
  995. cmn_err(CE_WARN,
  996. "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
  997. agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
  998. ASSERT(0);
  999. return XFS_ERROR(EINVAL);
  1000. }
  1001. /*
  1002. * Get the allocation group header.
  1003. */
  1004. down_read(&mp->m_peraglock);
  1005. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1006. up_read(&mp->m_peraglock);
  1007. if (error) {
  1008. cmn_err(CE_WARN,
  1009. "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
  1010. error, mp->m_fsname);
  1011. return error;
  1012. }
  1013. agi = XFS_BUF_TO_AGI(agbp);
  1014. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  1015. ASSERT(agbno < be32_to_cpu(agi->agi_length));
  1016. /*
  1017. * Initialize the cursor.
  1018. */
  1019. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
  1020. error = xfs_check_agi_freecount(cur, agi);
  1021. if (error)
  1022. goto error0;
  1023. /*
  1024. * Look for the entry describing this inode.
  1025. */
  1026. if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
  1027. cmn_err(CE_WARN,
  1028. "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
  1029. error, mp->m_fsname);
  1030. goto error0;
  1031. }
  1032. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1033. error = xfs_inobt_get_rec(cur, &rec, &i);
  1034. if (error) {
  1035. cmn_err(CE_WARN,
  1036. "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
  1037. error, mp->m_fsname);
  1038. goto error0;
  1039. }
  1040. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1041. /*
  1042. * Get the offset in the inode chunk.
  1043. */
  1044. off = agino - rec.ir_startino;
  1045. ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
  1046. ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
  1047. /*
  1048. * Mark the inode free & increment the count.
  1049. */
  1050. rec.ir_free |= XFS_INOBT_MASK(off);
  1051. rec.ir_freecount++;
  1052. /*
  1053. * When an inode cluster is free, it becomes eligible for removal
  1054. */
  1055. if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
  1056. (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
  1057. *delete = 1;
  1058. *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
  1059. /*
  1060. * Remove the inode cluster from the AGI B+Tree, adjust the
  1061. * AGI and Superblock inode counts, and mark the disk space
  1062. * to be freed when the transaction is committed.
  1063. */
  1064. ilen = XFS_IALLOC_INODES(mp);
  1065. be32_add_cpu(&agi->agi_count, -ilen);
  1066. be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
  1067. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
  1068. down_read(&mp->m_peraglock);
  1069. mp->m_perag[agno].pagi_freecount -= ilen - 1;
  1070. up_read(&mp->m_peraglock);
  1071. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
  1072. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
  1073. if ((error = xfs_btree_delete(cur, &i))) {
  1074. cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
  1075. error, mp->m_fsname);
  1076. goto error0;
  1077. }
  1078. xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
  1079. agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
  1080. XFS_IALLOC_BLOCKS(mp), flist, mp);
  1081. } else {
  1082. *delete = 0;
  1083. error = xfs_inobt_update(cur, &rec);
  1084. if (error) {
  1085. cmn_err(CE_WARN,
  1086. "xfs_difree: xfs_inobt_update returned an error %d on %s.",
  1087. error, mp->m_fsname);
  1088. goto error0;
  1089. }
  1090. /*
  1091. * Change the inode free counts and log the ag/sb changes.
  1092. */
  1093. be32_add_cpu(&agi->agi_freecount, 1);
  1094. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1095. down_read(&mp->m_peraglock);
  1096. mp->m_perag[agno].pagi_freecount++;
  1097. up_read(&mp->m_peraglock);
  1098. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
  1099. }
  1100. error = xfs_check_agi_freecount(cur, agi);
  1101. if (error)
  1102. goto error0;
  1103. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1104. return 0;
  1105. error0:
  1106. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1107. return error;
  1108. }
  1109. /*
  1110. * Return the location of the inode in imap, for mapping it into a buffer.
  1111. */
  1112. int
  1113. xfs_imap(
  1114. xfs_mount_t *mp, /* file system mount structure */
  1115. xfs_trans_t *tp, /* transaction pointer */
  1116. xfs_ino_t ino, /* inode to locate */
  1117. struct xfs_imap *imap, /* location map structure */
  1118. uint flags) /* flags for inode btree lookup */
  1119. {
  1120. xfs_agblock_t agbno; /* block number of inode in the alloc group */
  1121. xfs_agino_t agino; /* inode number within alloc group */
  1122. xfs_agnumber_t agno; /* allocation group number */
  1123. int blks_per_cluster; /* num blocks per inode cluster */
  1124. xfs_agblock_t chunk_agbno; /* first block in inode chunk */
  1125. xfs_agblock_t cluster_agbno; /* first block in inode cluster */
  1126. int error; /* error code */
  1127. int offset; /* index of inode in its buffer */
  1128. int offset_agbno; /* blks from chunk start to inode */
  1129. ASSERT(ino != NULLFSINO);
  1130. /*
  1131. * Split up the inode number into its parts.
  1132. */
  1133. agno = XFS_INO_TO_AGNO(mp, ino);
  1134. agino = XFS_INO_TO_AGINO(mp, ino);
  1135. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1136. if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
  1137. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1138. #ifdef DEBUG
  1139. /* no diagnostics for bulkstat, ino comes from userspace */
  1140. if (flags & XFS_IGET_BULKSTAT)
  1141. return XFS_ERROR(EINVAL);
  1142. if (agno >= mp->m_sb.sb_agcount) {
  1143. xfs_fs_cmn_err(CE_ALERT, mp,
  1144. "xfs_imap: agno (%d) >= "
  1145. "mp->m_sb.sb_agcount (%d)",
  1146. agno, mp->m_sb.sb_agcount);
  1147. }
  1148. if (agbno >= mp->m_sb.sb_agblocks) {
  1149. xfs_fs_cmn_err(CE_ALERT, mp,
  1150. "xfs_imap: agbno (0x%llx) >= "
  1151. "mp->m_sb.sb_agblocks (0x%lx)",
  1152. (unsigned long long) agbno,
  1153. (unsigned long) mp->m_sb.sb_agblocks);
  1154. }
  1155. if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1156. xfs_fs_cmn_err(CE_ALERT, mp,
  1157. "xfs_imap: ino (0x%llx) != "
  1158. "XFS_AGINO_TO_INO(mp, agno, agino) "
  1159. "(0x%llx)",
  1160. ino, XFS_AGINO_TO_INO(mp, agno, agino));
  1161. }
  1162. xfs_stack_trace();
  1163. #endif /* DEBUG */
  1164. return XFS_ERROR(EINVAL);
  1165. }
  1166. /*
  1167. * If the inode cluster size is the same as the blocksize or
  1168. * smaller we get to the buffer by simple arithmetics.
  1169. */
  1170. if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
  1171. offset = XFS_INO_TO_OFFSET(mp, ino);
  1172. ASSERT(offset < mp->m_sb.sb_inopblock);
  1173. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
  1174. imap->im_len = XFS_FSB_TO_BB(mp, 1);
  1175. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1176. return 0;
  1177. }
  1178. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
  1179. /*
  1180. * If we get a block number passed from bulkstat we can use it to
  1181. * find the buffer easily.
  1182. */
  1183. if (imap->im_blkno) {
  1184. offset = XFS_INO_TO_OFFSET(mp, ino);
  1185. ASSERT(offset < mp->m_sb.sb_inopblock);
  1186. cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
  1187. offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
  1188. imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
  1189. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1190. return 0;
  1191. }
  1192. /*
  1193. * If the inode chunks are aligned then use simple maths to
  1194. * find the location. Otherwise we have to do a btree
  1195. * lookup to find the location.
  1196. */
  1197. if (mp->m_inoalign_mask) {
  1198. offset_agbno = agbno & mp->m_inoalign_mask;
  1199. chunk_agbno = agbno - offset_agbno;
  1200. } else {
  1201. xfs_btree_cur_t *cur; /* inode btree cursor */
  1202. xfs_inobt_rec_incore_t chunk_rec;
  1203. xfs_buf_t *agbp; /* agi buffer */
  1204. int i; /* temp state */
  1205. down_read(&mp->m_peraglock);
  1206. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1207. up_read(&mp->m_peraglock);
  1208. if (error) {
  1209. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
  1210. "xfs_ialloc_read_agi() returned "
  1211. "error %d, agno %d",
  1212. error, agno);
  1213. return error;
  1214. }
  1215. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
  1216. error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
  1217. if (error) {
  1218. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
  1219. "xfs_inobt_lookup_le() failed");
  1220. goto error0;
  1221. }
  1222. error = xfs_inobt_get_rec(cur, &chunk_rec, &i);
  1223. if (error) {
  1224. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
  1225. "xfs_inobt_get_rec() failed");
  1226. goto error0;
  1227. }
  1228. if (i == 0) {
  1229. #ifdef DEBUG
  1230. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
  1231. "xfs_inobt_get_rec() failed");
  1232. #endif /* DEBUG */
  1233. error = XFS_ERROR(EINVAL);
  1234. }
  1235. error0:
  1236. xfs_trans_brelse(tp, agbp);
  1237. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1238. if (error)
  1239. return error;
  1240. chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_rec.ir_startino);
  1241. offset_agbno = agbno - chunk_agbno;
  1242. }
  1243. ASSERT(agbno >= chunk_agbno);
  1244. cluster_agbno = chunk_agbno +
  1245. ((offset_agbno / blks_per_cluster) * blks_per_cluster);
  1246. offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
  1247. XFS_INO_TO_OFFSET(mp, ino);
  1248. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
  1249. imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
  1250. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1251. /*
  1252. * If the inode number maps to a block outside the bounds
  1253. * of the file system then return NULL rather than calling
  1254. * read_buf and panicing when we get an error from the
  1255. * driver.
  1256. */
  1257. if ((imap->im_blkno + imap->im_len) >
  1258. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  1259. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
  1260. "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
  1261. " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
  1262. (unsigned long long) imap->im_blkno,
  1263. (unsigned long long) imap->im_len,
  1264. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
  1265. return XFS_ERROR(EINVAL);
  1266. }
  1267. return 0;
  1268. }
  1269. /*
  1270. * Compute and fill in value of m_in_maxlevels.
  1271. */
  1272. void
  1273. xfs_ialloc_compute_maxlevels(
  1274. xfs_mount_t *mp) /* file system mount structure */
  1275. {
  1276. int level;
  1277. uint maxblocks;
  1278. uint maxleafents;
  1279. int minleafrecs;
  1280. int minnoderecs;
  1281. maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
  1282. XFS_INODES_PER_CHUNK_LOG;
  1283. minleafrecs = mp->m_alloc_mnr[0];
  1284. minnoderecs = mp->m_alloc_mnr[1];
  1285. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1286. for (level = 1; maxblocks > 1; level++)
  1287. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1288. mp->m_in_maxlevels = level;
  1289. }
  1290. /*
  1291. * Log specified fields for the ag hdr (inode section)
  1292. */
  1293. void
  1294. xfs_ialloc_log_agi(
  1295. xfs_trans_t *tp, /* transaction pointer */
  1296. xfs_buf_t *bp, /* allocation group header buffer */
  1297. int fields) /* bitmask of fields to log */
  1298. {
  1299. int first; /* first byte number */
  1300. int last; /* last byte number */
  1301. static const short offsets[] = { /* field starting offsets */
  1302. /* keep in sync with bit definitions */
  1303. offsetof(xfs_agi_t, agi_magicnum),
  1304. offsetof(xfs_agi_t, agi_versionnum),
  1305. offsetof(xfs_agi_t, agi_seqno),
  1306. offsetof(xfs_agi_t, agi_length),
  1307. offsetof(xfs_agi_t, agi_count),
  1308. offsetof(xfs_agi_t, agi_root),
  1309. offsetof(xfs_agi_t, agi_level),
  1310. offsetof(xfs_agi_t, agi_freecount),
  1311. offsetof(xfs_agi_t, agi_newino),
  1312. offsetof(xfs_agi_t, agi_dirino),
  1313. offsetof(xfs_agi_t, agi_unlinked),
  1314. sizeof(xfs_agi_t)
  1315. };
  1316. #ifdef DEBUG
  1317. xfs_agi_t *agi; /* allocation group header */
  1318. agi = XFS_BUF_TO_AGI(bp);
  1319. ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
  1320. #endif
  1321. /*
  1322. * Compute byte offsets for the first and last fields.
  1323. */
  1324. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
  1325. /*
  1326. * Log the allocation group inode header buffer.
  1327. */
  1328. xfs_trans_log_buf(tp, bp, first, last);
  1329. }
  1330. #ifdef DEBUG
  1331. STATIC void
  1332. xfs_check_agi_unlinked(
  1333. struct xfs_agi *agi)
  1334. {
  1335. int i;
  1336. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
  1337. ASSERT(agi->agi_unlinked[i]);
  1338. }
  1339. #else
  1340. #define xfs_check_agi_unlinked(agi)
  1341. #endif
  1342. /*
  1343. * Read in the allocation group header (inode allocation section)
  1344. */
  1345. int
  1346. xfs_read_agi(
  1347. struct xfs_mount *mp, /* file system mount structure */
  1348. struct xfs_trans *tp, /* transaction pointer */
  1349. xfs_agnumber_t agno, /* allocation group number */
  1350. struct xfs_buf **bpp) /* allocation group hdr buf */
  1351. {
  1352. struct xfs_agi *agi; /* allocation group header */
  1353. int agi_ok; /* agi is consistent */
  1354. int error;
  1355. ASSERT(agno != NULLAGNUMBER);
  1356. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  1357. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  1358. XFS_FSS_TO_BB(mp, 1), 0, bpp);
  1359. if (error)
  1360. return error;
  1361. ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
  1362. agi = XFS_BUF_TO_AGI(*bpp);
  1363. /*
  1364. * Validate the magic number of the agi block.
  1365. */
  1366. agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
  1367. XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
  1368. be32_to_cpu(agi->agi_seqno) == agno;
  1369. if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
  1370. XFS_RANDOM_IALLOC_READ_AGI))) {
  1371. XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
  1372. mp, agi);
  1373. xfs_trans_brelse(tp, *bpp);
  1374. return XFS_ERROR(EFSCORRUPTED);
  1375. }
  1376. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
  1377. xfs_check_agi_unlinked(agi);
  1378. return 0;
  1379. }
  1380. int
  1381. xfs_ialloc_read_agi(
  1382. struct xfs_mount *mp, /* file system mount structure */
  1383. struct xfs_trans *tp, /* transaction pointer */
  1384. xfs_agnumber_t agno, /* allocation group number */
  1385. struct xfs_buf **bpp) /* allocation group hdr buf */
  1386. {
  1387. struct xfs_agi *agi; /* allocation group header */
  1388. struct xfs_perag *pag; /* per allocation group data */
  1389. int error;
  1390. error = xfs_read_agi(mp, tp, agno, bpp);
  1391. if (error)
  1392. return error;
  1393. agi = XFS_BUF_TO_AGI(*bpp);
  1394. pag = &mp->m_perag[agno];
  1395. if (!pag->pagi_init) {
  1396. pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
  1397. pag->pagi_count = be32_to_cpu(agi->agi_count);
  1398. pag->pagi_init = 1;
  1399. }
  1400. /*
  1401. * It's possible for these to be out of sync if
  1402. * we are in the middle of a forced shutdown.
  1403. */
  1404. ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
  1405. XFS_FORCED_SHUTDOWN(mp));
  1406. return 0;
  1407. }
  1408. /*
  1409. * Read in the agi to initialise the per-ag data in the mount structure
  1410. */
  1411. int
  1412. xfs_ialloc_pagi_init(
  1413. xfs_mount_t *mp, /* file system mount structure */
  1414. xfs_trans_t *tp, /* transaction pointer */
  1415. xfs_agnumber_t agno) /* allocation group number */
  1416. {
  1417. xfs_buf_t *bp = NULL;
  1418. int error;
  1419. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  1420. if (error)
  1421. return error;
  1422. if (bp)
  1423. xfs_trans_brelse(tp, bp);
  1424. return 0;
  1425. }