xfs_ialloc.c 45 KB

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