xfs_ialloc.c 42 KB

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