xfs_ialloc.c 42 KB

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