xfs_ialloc.c 43 KB

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