xfs_ialloc.c 42 KB

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