xfs_ialloc.c 42 KB

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