xfs_ialloc.c 41 KB

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