xfs_ialloc.c 44 KB

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