xfs_ialloc.c 45 KB

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