xfs_ialloc.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  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. * Insert a single inobt record. Cursor must already point to desired location.
  108. */
  109. STATIC int
  110. xfs_inobt_insert_rec(
  111. struct xfs_btree_cur *cur,
  112. __int32_t freecount,
  113. xfs_inofree_t free,
  114. int *stat)
  115. {
  116. cur->bc_rec.i.ir_freecount = freecount;
  117. cur->bc_rec.i.ir_free = free;
  118. return xfs_btree_insert(cur, stat);
  119. }
  120. /*
  121. * Insert records describing a newly allocated inode chunk into the inobt.
  122. */
  123. STATIC int
  124. xfs_inobt_insert(
  125. struct xfs_mount *mp,
  126. struct xfs_trans *tp,
  127. struct xfs_buf *agbp,
  128. xfs_agino_t newino,
  129. xfs_agino_t newlen,
  130. xfs_btnum_t btnum)
  131. {
  132. struct xfs_btree_cur *cur;
  133. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  134. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  135. xfs_agino_t thisino;
  136. int i;
  137. int error;
  138. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
  139. for (thisino = newino;
  140. thisino < newino + newlen;
  141. thisino += XFS_INODES_PER_CHUNK) {
  142. error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
  143. if (error) {
  144. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  145. return error;
  146. }
  147. ASSERT(i == 0);
  148. error = xfs_inobt_insert_rec(cur, XFS_INODES_PER_CHUNK,
  149. XFS_INOBT_ALL_FREE, &i);
  150. if (error) {
  151. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  152. return error;
  153. }
  154. ASSERT(i == 1);
  155. }
  156. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  157. return 0;
  158. }
  159. /*
  160. * Verify that the number of free inodes in the AGI is correct.
  161. */
  162. #ifdef DEBUG
  163. STATIC int
  164. xfs_check_agi_freecount(
  165. struct xfs_btree_cur *cur,
  166. struct xfs_agi *agi)
  167. {
  168. if (cur->bc_nlevels == 1) {
  169. xfs_inobt_rec_incore_t rec;
  170. int freecount = 0;
  171. int error;
  172. int i;
  173. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  174. if (error)
  175. return error;
  176. do {
  177. error = xfs_inobt_get_rec(cur, &rec, &i);
  178. if (error)
  179. return error;
  180. if (i) {
  181. freecount += rec.ir_freecount;
  182. error = xfs_btree_increment(cur, 0, &i);
  183. if (error)
  184. return error;
  185. }
  186. } while (i == 1);
  187. if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
  188. ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
  189. }
  190. return 0;
  191. }
  192. #else
  193. #define xfs_check_agi_freecount(cur, agi) 0
  194. #endif
  195. /*
  196. * Initialise a new set of inodes. When called without a transaction context
  197. * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
  198. * than logging them (which in a transaction context puts them into the AIL
  199. * for writeback rather than the xfsbufd queue).
  200. */
  201. int
  202. xfs_ialloc_inode_init(
  203. struct xfs_mount *mp,
  204. struct xfs_trans *tp,
  205. struct list_head *buffer_list,
  206. xfs_agnumber_t agno,
  207. xfs_agblock_t agbno,
  208. xfs_agblock_t length,
  209. unsigned int gen)
  210. {
  211. struct xfs_buf *fbuf;
  212. struct xfs_dinode *free;
  213. int nbufs, blks_per_cluster, inodes_per_cluster;
  214. int version;
  215. int i, j;
  216. xfs_daddr_t d;
  217. xfs_ino_t ino = 0;
  218. /*
  219. * Loop over the new block(s), filling in the inodes. For small block
  220. * sizes, manipulate the inodes in buffers which are multiples of the
  221. * blocks size.
  222. */
  223. blks_per_cluster = xfs_icluster_size_fsb(mp);
  224. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  225. nbufs = length / blks_per_cluster;
  226. /*
  227. * Figure out what version number to use in the inodes we create. If
  228. * the superblock version has caught up to the one that supports the new
  229. * inode format, then use the new inode version. Otherwise use the old
  230. * version so that old kernels will continue to be able to use the file
  231. * system.
  232. *
  233. * For v3 inodes, we also need to write the inode number into the inode,
  234. * so calculate the first inode number of the chunk here as
  235. * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
  236. * across multiple filesystem blocks (such as a cluster) and so cannot
  237. * be used in the cluster buffer loop below.
  238. *
  239. * Further, because we are writing the inode directly into the buffer
  240. * and calculating a CRC on the entire inode, we have ot log the entire
  241. * inode so that the entire range the CRC covers is present in the log.
  242. * That means for v3 inode we log the entire buffer rather than just the
  243. * inode cores.
  244. */
  245. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  246. version = 3;
  247. ino = XFS_AGINO_TO_INO(mp, agno,
  248. XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
  249. /*
  250. * log the initialisation that is about to take place as an
  251. * logical operation. This means the transaction does not
  252. * need to log the physical changes to the inode buffers as log
  253. * recovery will know what initialisation is actually needed.
  254. * Hence we only need to log the buffers as "ordered" buffers so
  255. * they track in the AIL as if they were physically logged.
  256. */
  257. if (tp)
  258. xfs_icreate_log(tp, agno, agbno, mp->m_ialloc_inos,
  259. mp->m_sb.sb_inodesize, length, gen);
  260. } else if (xfs_sb_version_hasnlink(&mp->m_sb))
  261. version = 2;
  262. else
  263. version = 1;
  264. for (j = 0; j < nbufs; j++) {
  265. /*
  266. * Get the block.
  267. */
  268. d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
  269. fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  270. mp->m_bsize * blks_per_cluster,
  271. XBF_UNMAPPED);
  272. if (!fbuf)
  273. return ENOMEM;
  274. /* Initialize the inode buffers and log them appropriately. */
  275. fbuf->b_ops = &xfs_inode_buf_ops;
  276. xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
  277. for (i = 0; i < inodes_per_cluster; i++) {
  278. int ioffset = i << mp->m_sb.sb_inodelog;
  279. uint isize = xfs_dinode_size(version);
  280. free = xfs_make_iptr(mp, fbuf, i);
  281. free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
  282. free->di_version = version;
  283. free->di_gen = cpu_to_be32(gen);
  284. free->di_next_unlinked = cpu_to_be32(NULLAGINO);
  285. if (version == 3) {
  286. free->di_ino = cpu_to_be64(ino);
  287. ino++;
  288. uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
  289. xfs_dinode_calc_crc(mp, free);
  290. } else if (tp) {
  291. /* just log the inode core */
  292. xfs_trans_log_buf(tp, fbuf, ioffset,
  293. ioffset + isize - 1);
  294. }
  295. }
  296. if (tp) {
  297. /*
  298. * Mark the buffer as an inode allocation buffer so it
  299. * sticks in AIL at the point of this allocation
  300. * transaction. This ensures the they are on disk before
  301. * the tail of the log can be moved past this
  302. * transaction (i.e. by preventing relogging from moving
  303. * it forward in the log).
  304. */
  305. xfs_trans_inode_alloc_buf(tp, fbuf);
  306. if (version == 3) {
  307. /*
  308. * Mark the buffer as ordered so that they are
  309. * not physically logged in the transaction but
  310. * still tracked in the AIL as part of the
  311. * transaction and pin the log appropriately.
  312. */
  313. xfs_trans_ordered_buf(tp, fbuf);
  314. xfs_trans_log_buf(tp, fbuf, 0,
  315. BBTOB(fbuf->b_length) - 1);
  316. }
  317. } else {
  318. fbuf->b_flags |= XBF_DONE;
  319. xfs_buf_delwri_queue(fbuf, buffer_list);
  320. xfs_buf_relse(fbuf);
  321. }
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Allocate new inodes in the allocation group specified by agbp.
  327. * Return 0 for success, else error code.
  328. */
  329. STATIC int /* error code or 0 */
  330. xfs_ialloc_ag_alloc(
  331. xfs_trans_t *tp, /* transaction pointer */
  332. xfs_buf_t *agbp, /* alloc group buffer */
  333. int *alloc)
  334. {
  335. xfs_agi_t *agi; /* allocation group header */
  336. xfs_alloc_arg_t args; /* allocation argument structure */
  337. xfs_agnumber_t agno;
  338. int error;
  339. xfs_agino_t newino; /* new first inode's number */
  340. xfs_agino_t newlen; /* new number of inodes */
  341. int isaligned = 0; /* inode allocation at stripe unit */
  342. /* boundary */
  343. struct xfs_perag *pag;
  344. memset(&args, 0, sizeof(args));
  345. args.tp = tp;
  346. args.mp = tp->t_mountp;
  347. /*
  348. * Locking will ensure that we don't have two callers in here
  349. * at one time.
  350. */
  351. newlen = args.mp->m_ialloc_inos;
  352. if (args.mp->m_maxicount &&
  353. args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
  354. return XFS_ERROR(ENOSPC);
  355. args.minlen = args.maxlen = args.mp->m_ialloc_blks;
  356. /*
  357. * First try to allocate inodes contiguous with the last-allocated
  358. * chunk of inodes. If the filesystem is striped, this will fill
  359. * an entire stripe unit with inodes.
  360. */
  361. agi = XFS_BUF_TO_AGI(agbp);
  362. newino = be32_to_cpu(agi->agi_newino);
  363. agno = be32_to_cpu(agi->agi_seqno);
  364. args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
  365. args.mp->m_ialloc_blks;
  366. if (likely(newino != NULLAGINO &&
  367. (args.agbno < be32_to_cpu(agi->agi_length)))) {
  368. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  369. args.type = XFS_ALLOCTYPE_THIS_BNO;
  370. args.prod = 1;
  371. /*
  372. * We need to take into account alignment here to ensure that
  373. * we don't modify the free list if we fail to have an exact
  374. * block. If we don't have an exact match, and every oher
  375. * attempt allocation attempt fails, we'll end up cancelling
  376. * a dirty transaction and shutting down.
  377. *
  378. * For an exact allocation, alignment must be 1,
  379. * however we need to take cluster alignment into account when
  380. * fixing up the freelist. Use the minalignslop field to
  381. * indicate that extra blocks might be required for alignment,
  382. * but not to use them in the actual exact allocation.
  383. */
  384. args.alignment = 1;
  385. args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
  386. /* Allow space for the inode btree to split. */
  387. args.minleft = args.mp->m_in_maxlevels - 1;
  388. if ((error = xfs_alloc_vextent(&args)))
  389. return error;
  390. /*
  391. * This request might have dirtied the transaction if the AG can
  392. * satisfy the request, but the exact block was not available.
  393. * If the allocation did fail, subsequent requests will relax
  394. * the exact agbno requirement and increase the alignment
  395. * instead. It is critical that the total size of the request
  396. * (len + alignment + slop) does not increase from this point
  397. * on, so reset minalignslop to ensure it is not included in
  398. * subsequent requests.
  399. */
  400. args.minalignslop = 0;
  401. } else
  402. args.fsbno = NULLFSBLOCK;
  403. if (unlikely(args.fsbno == NULLFSBLOCK)) {
  404. /*
  405. * Set the alignment for the allocation.
  406. * If stripe alignment is turned on then align at stripe unit
  407. * boundary.
  408. * If the cluster size is smaller than a filesystem block
  409. * then we're doing I/O for inodes in filesystem block size
  410. * pieces, so don't need alignment anyway.
  411. */
  412. isaligned = 0;
  413. if (args.mp->m_sinoalign) {
  414. ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
  415. args.alignment = args.mp->m_dalign;
  416. isaligned = 1;
  417. } else
  418. args.alignment = xfs_ialloc_cluster_alignment(&args);
  419. /*
  420. * Need to figure out where to allocate the inode blocks.
  421. * Ideally they should be spaced out through the a.g.
  422. * For now, just allocate blocks up front.
  423. */
  424. args.agbno = be32_to_cpu(agi->agi_root);
  425. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  426. /*
  427. * Allocate a fixed-size extent of inodes.
  428. */
  429. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  430. args.prod = 1;
  431. /*
  432. * Allow space for the inode btree to split.
  433. */
  434. args.minleft = args.mp->m_in_maxlevels - 1;
  435. if ((error = xfs_alloc_vextent(&args)))
  436. return error;
  437. }
  438. /*
  439. * If stripe alignment is turned on, then try again with cluster
  440. * alignment.
  441. */
  442. if (isaligned && args.fsbno == NULLFSBLOCK) {
  443. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  444. args.agbno = be32_to_cpu(agi->agi_root);
  445. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  446. args.alignment = xfs_ialloc_cluster_alignment(&args);
  447. if ((error = xfs_alloc_vextent(&args)))
  448. return error;
  449. }
  450. if (args.fsbno == NULLFSBLOCK) {
  451. *alloc = 0;
  452. return 0;
  453. }
  454. ASSERT(args.len == args.minlen);
  455. /*
  456. * Stamp and write the inode buffers.
  457. *
  458. * Seed the new inode cluster with a random generation number. This
  459. * prevents short-term reuse of generation numbers if a chunk is
  460. * freed and then immediately reallocated. We use random numbers
  461. * rather than a linear progression to prevent the next generation
  462. * number from being easily guessable.
  463. */
  464. error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
  465. args.len, prandom_u32());
  466. if (error)
  467. return error;
  468. /*
  469. * Convert the results.
  470. */
  471. newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
  472. be32_add_cpu(&agi->agi_count, newlen);
  473. be32_add_cpu(&agi->agi_freecount, newlen);
  474. pag = xfs_perag_get(args.mp, agno);
  475. pag->pagi_freecount += newlen;
  476. xfs_perag_put(pag);
  477. agi->agi_newino = cpu_to_be32(newino);
  478. /*
  479. * Insert records describing the new inode chunk into the btrees.
  480. */
  481. error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
  482. XFS_BTNUM_INO);
  483. if (error)
  484. return error;
  485. if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
  486. error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
  487. XFS_BTNUM_FINO);
  488. if (error)
  489. return error;
  490. }
  491. /*
  492. * Log allocation group header fields
  493. */
  494. xfs_ialloc_log_agi(tp, agbp,
  495. XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
  496. /*
  497. * Modify/log superblock values for inode count and inode free count.
  498. */
  499. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
  500. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
  501. *alloc = 1;
  502. return 0;
  503. }
  504. STATIC xfs_agnumber_t
  505. xfs_ialloc_next_ag(
  506. xfs_mount_t *mp)
  507. {
  508. xfs_agnumber_t agno;
  509. spin_lock(&mp->m_agirotor_lock);
  510. agno = mp->m_agirotor;
  511. if (++mp->m_agirotor >= mp->m_maxagi)
  512. mp->m_agirotor = 0;
  513. spin_unlock(&mp->m_agirotor_lock);
  514. return agno;
  515. }
  516. /*
  517. * Select an allocation group to look for a free inode in, based on the parent
  518. * inode and the mode. Return the allocation group buffer.
  519. */
  520. STATIC xfs_agnumber_t
  521. xfs_ialloc_ag_select(
  522. xfs_trans_t *tp, /* transaction pointer */
  523. xfs_ino_t parent, /* parent directory inode number */
  524. umode_t mode, /* bits set to indicate file type */
  525. int okalloc) /* ok to allocate more space */
  526. {
  527. xfs_agnumber_t agcount; /* number of ag's in the filesystem */
  528. xfs_agnumber_t agno; /* current ag number */
  529. int flags; /* alloc buffer locking flags */
  530. xfs_extlen_t ineed; /* blocks needed for inode allocation */
  531. xfs_extlen_t longest = 0; /* longest extent available */
  532. xfs_mount_t *mp; /* mount point structure */
  533. int needspace; /* file mode implies space allocated */
  534. xfs_perag_t *pag; /* per allocation group data */
  535. xfs_agnumber_t pagno; /* parent (starting) ag number */
  536. int error;
  537. /*
  538. * Files of these types need at least one block if length > 0
  539. * (and they won't fit in the inode, but that's hard to figure out).
  540. */
  541. needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
  542. mp = tp->t_mountp;
  543. agcount = mp->m_maxagi;
  544. if (S_ISDIR(mode))
  545. pagno = xfs_ialloc_next_ag(mp);
  546. else {
  547. pagno = XFS_INO_TO_AGNO(mp, parent);
  548. if (pagno >= agcount)
  549. pagno = 0;
  550. }
  551. ASSERT(pagno < agcount);
  552. /*
  553. * Loop through allocation groups, looking for one with a little
  554. * free space in it. Note we don't look for free inodes, exactly.
  555. * Instead, we include whether there is a need to allocate inodes
  556. * to mean that blocks must be allocated for them,
  557. * if none are currently free.
  558. */
  559. agno = pagno;
  560. flags = XFS_ALLOC_FLAG_TRYLOCK;
  561. for (;;) {
  562. pag = xfs_perag_get(mp, agno);
  563. if (!pag->pagi_inodeok) {
  564. xfs_ialloc_next_ag(mp);
  565. goto nextag;
  566. }
  567. if (!pag->pagi_init) {
  568. error = xfs_ialloc_pagi_init(mp, tp, agno);
  569. if (error)
  570. goto nextag;
  571. }
  572. if (pag->pagi_freecount) {
  573. xfs_perag_put(pag);
  574. return agno;
  575. }
  576. if (!okalloc)
  577. goto nextag;
  578. if (!pag->pagf_init) {
  579. error = xfs_alloc_pagf_init(mp, tp, agno, flags);
  580. if (error)
  581. goto nextag;
  582. }
  583. /*
  584. * Is there enough free space for the file plus a block of
  585. * inodes? (if we need to allocate some)?
  586. */
  587. ineed = mp->m_ialloc_blks;
  588. longest = pag->pagf_longest;
  589. if (!longest)
  590. longest = pag->pagf_flcount > 0;
  591. if (pag->pagf_freeblks >= needspace + ineed &&
  592. longest >= ineed) {
  593. xfs_perag_put(pag);
  594. return agno;
  595. }
  596. nextag:
  597. xfs_perag_put(pag);
  598. /*
  599. * No point in iterating over the rest, if we're shutting
  600. * down.
  601. */
  602. if (XFS_FORCED_SHUTDOWN(mp))
  603. return NULLAGNUMBER;
  604. agno++;
  605. if (agno >= agcount)
  606. agno = 0;
  607. if (agno == pagno) {
  608. if (flags == 0)
  609. return NULLAGNUMBER;
  610. flags = 0;
  611. }
  612. }
  613. }
  614. /*
  615. * Try to retrieve the next record to the left/right from the current one.
  616. */
  617. STATIC int
  618. xfs_ialloc_next_rec(
  619. struct xfs_btree_cur *cur,
  620. xfs_inobt_rec_incore_t *rec,
  621. int *done,
  622. int left)
  623. {
  624. int error;
  625. int i;
  626. if (left)
  627. error = xfs_btree_decrement(cur, 0, &i);
  628. else
  629. error = xfs_btree_increment(cur, 0, &i);
  630. if (error)
  631. return error;
  632. *done = !i;
  633. if (i) {
  634. error = xfs_inobt_get_rec(cur, rec, &i);
  635. if (error)
  636. return error;
  637. XFS_WANT_CORRUPTED_RETURN(i == 1);
  638. }
  639. return 0;
  640. }
  641. STATIC int
  642. xfs_ialloc_get_rec(
  643. struct xfs_btree_cur *cur,
  644. xfs_agino_t agino,
  645. xfs_inobt_rec_incore_t *rec,
  646. int *done)
  647. {
  648. int error;
  649. int i;
  650. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
  651. if (error)
  652. return error;
  653. *done = !i;
  654. if (i) {
  655. error = xfs_inobt_get_rec(cur, rec, &i);
  656. if (error)
  657. return error;
  658. XFS_WANT_CORRUPTED_RETURN(i == 1);
  659. }
  660. return 0;
  661. }
  662. /*
  663. * Allocate an inode using the inobt-only algorithm.
  664. */
  665. STATIC int
  666. xfs_dialloc_ag_inobt(
  667. struct xfs_trans *tp,
  668. struct xfs_buf *agbp,
  669. xfs_ino_t parent,
  670. xfs_ino_t *inop)
  671. {
  672. struct xfs_mount *mp = tp->t_mountp;
  673. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  674. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  675. xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
  676. xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
  677. struct xfs_perag *pag;
  678. struct xfs_btree_cur *cur, *tcur;
  679. struct xfs_inobt_rec_incore rec, trec;
  680. xfs_ino_t ino;
  681. int error;
  682. int offset;
  683. int i, j;
  684. pag = xfs_perag_get(mp, agno);
  685. ASSERT(pag->pagi_init);
  686. ASSERT(pag->pagi_inodeok);
  687. ASSERT(pag->pagi_freecount > 0);
  688. restart_pagno:
  689. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  690. /*
  691. * If pagino is 0 (this is the root inode allocation) use newino.
  692. * This must work because we've just allocated some.
  693. */
  694. if (!pagino)
  695. pagino = be32_to_cpu(agi->agi_newino);
  696. error = xfs_check_agi_freecount(cur, agi);
  697. if (error)
  698. goto error0;
  699. /*
  700. * If in the same AG as the parent, try to get near the parent.
  701. */
  702. if (pagno == agno) {
  703. int doneleft; /* done, to the left */
  704. int doneright; /* done, to the right */
  705. int searchdistance = 10;
  706. error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
  707. if (error)
  708. goto error0;
  709. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  710. error = xfs_inobt_get_rec(cur, &rec, &j);
  711. if (error)
  712. goto error0;
  713. XFS_WANT_CORRUPTED_GOTO(j == 1, error0);
  714. if (rec.ir_freecount > 0) {
  715. /*
  716. * Found a free inode in the same chunk
  717. * as the parent, done.
  718. */
  719. goto alloc_inode;
  720. }
  721. /*
  722. * In the same AG as parent, but parent's chunk is full.
  723. */
  724. /* duplicate the cursor, search left & right simultaneously */
  725. error = xfs_btree_dup_cursor(cur, &tcur);
  726. if (error)
  727. goto error0;
  728. /*
  729. * Skip to last blocks looked up if same parent inode.
  730. */
  731. if (pagino != NULLAGINO &&
  732. pag->pagl_pagino == pagino &&
  733. pag->pagl_leftrec != NULLAGINO &&
  734. pag->pagl_rightrec != NULLAGINO) {
  735. error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
  736. &trec, &doneleft);
  737. if (error)
  738. goto error1;
  739. error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
  740. &rec, &doneright);
  741. if (error)
  742. goto error1;
  743. } else {
  744. /* search left with tcur, back up 1 record */
  745. error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
  746. if (error)
  747. goto error1;
  748. /* search right with cur, go forward 1 record. */
  749. error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
  750. if (error)
  751. goto error1;
  752. }
  753. /*
  754. * Loop until we find an inode chunk with a free inode.
  755. */
  756. while (!doneleft || !doneright) {
  757. int useleft; /* using left inode chunk this time */
  758. if (!--searchdistance) {
  759. /*
  760. * Not in range - save last search
  761. * location and allocate a new inode
  762. */
  763. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  764. pag->pagl_leftrec = trec.ir_startino;
  765. pag->pagl_rightrec = rec.ir_startino;
  766. pag->pagl_pagino = pagino;
  767. goto newino;
  768. }
  769. /* figure out the closer block if both are valid. */
  770. if (!doneleft && !doneright) {
  771. useleft = pagino -
  772. (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
  773. rec.ir_startino - pagino;
  774. } else {
  775. useleft = !doneleft;
  776. }
  777. /* free inodes to the left? */
  778. if (useleft && trec.ir_freecount) {
  779. rec = trec;
  780. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  781. cur = tcur;
  782. pag->pagl_leftrec = trec.ir_startino;
  783. pag->pagl_rightrec = rec.ir_startino;
  784. pag->pagl_pagino = pagino;
  785. goto alloc_inode;
  786. }
  787. /* free inodes to the right? */
  788. if (!useleft && rec.ir_freecount) {
  789. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  790. pag->pagl_leftrec = trec.ir_startino;
  791. pag->pagl_rightrec = rec.ir_startino;
  792. pag->pagl_pagino = pagino;
  793. goto alloc_inode;
  794. }
  795. /* get next record to check */
  796. if (useleft) {
  797. error = xfs_ialloc_next_rec(tcur, &trec,
  798. &doneleft, 1);
  799. } else {
  800. error = xfs_ialloc_next_rec(cur, &rec,
  801. &doneright, 0);
  802. }
  803. if (error)
  804. goto error1;
  805. }
  806. /*
  807. * We've reached the end of the btree. because
  808. * we are only searching a small chunk of the
  809. * btree each search, there is obviously free
  810. * inodes closer to the parent inode than we
  811. * are now. restart the search again.
  812. */
  813. pag->pagl_pagino = NULLAGINO;
  814. pag->pagl_leftrec = NULLAGINO;
  815. pag->pagl_rightrec = NULLAGINO;
  816. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  817. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  818. goto restart_pagno;
  819. }
  820. /*
  821. * In a different AG from the parent.
  822. * See if the most recently allocated block has any free.
  823. */
  824. newino:
  825. if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
  826. error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
  827. XFS_LOOKUP_EQ, &i);
  828. if (error)
  829. goto error0;
  830. if (i == 1) {
  831. error = xfs_inobt_get_rec(cur, &rec, &j);
  832. if (error)
  833. goto error0;
  834. if (j == 1 && rec.ir_freecount > 0) {
  835. /*
  836. * The last chunk allocated in the group
  837. * still has a free inode.
  838. */
  839. goto alloc_inode;
  840. }
  841. }
  842. }
  843. /*
  844. * None left in the last group, search the whole AG
  845. */
  846. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  847. if (error)
  848. goto error0;
  849. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  850. for (;;) {
  851. error = xfs_inobt_get_rec(cur, &rec, &i);
  852. if (error)
  853. goto error0;
  854. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  855. if (rec.ir_freecount > 0)
  856. break;
  857. error = xfs_btree_increment(cur, 0, &i);
  858. if (error)
  859. goto error0;
  860. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  861. }
  862. alloc_inode:
  863. offset = xfs_lowbit64(rec.ir_free);
  864. ASSERT(offset >= 0);
  865. ASSERT(offset < XFS_INODES_PER_CHUNK);
  866. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  867. XFS_INODES_PER_CHUNK) == 0);
  868. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  869. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  870. rec.ir_freecount--;
  871. error = xfs_inobt_update(cur, &rec);
  872. if (error)
  873. goto error0;
  874. be32_add_cpu(&agi->agi_freecount, -1);
  875. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  876. pag->pagi_freecount--;
  877. error = xfs_check_agi_freecount(cur, agi);
  878. if (error)
  879. goto error0;
  880. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  881. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  882. xfs_perag_put(pag);
  883. *inop = ino;
  884. return 0;
  885. error1:
  886. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  887. error0:
  888. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  889. xfs_perag_put(pag);
  890. return error;
  891. }
  892. /*
  893. * Use the free inode btree to allocate an inode based on distance from the
  894. * parent. Note that the provided cursor may be deleted and replaced.
  895. */
  896. STATIC int
  897. xfs_dialloc_ag_finobt_near(
  898. xfs_agino_t pagino,
  899. struct xfs_btree_cur **ocur,
  900. struct xfs_inobt_rec_incore *rec)
  901. {
  902. struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
  903. struct xfs_btree_cur *rcur; /* right search cursor */
  904. struct xfs_inobt_rec_incore rrec;
  905. int error;
  906. int i, j;
  907. error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
  908. if (error)
  909. return error;
  910. if (i == 1) {
  911. error = xfs_inobt_get_rec(lcur, rec, &i);
  912. if (error)
  913. return error;
  914. XFS_WANT_CORRUPTED_RETURN(i == 1);
  915. /*
  916. * See if we've landed in the parent inode record. The finobt
  917. * only tracks chunks with at least one free inode, so record
  918. * existence is enough.
  919. */
  920. if (pagino >= rec->ir_startino &&
  921. pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
  922. return 0;
  923. }
  924. error = xfs_btree_dup_cursor(lcur, &rcur);
  925. if (error)
  926. return error;
  927. error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
  928. if (error)
  929. goto error_rcur;
  930. if (j == 1) {
  931. error = xfs_inobt_get_rec(rcur, &rrec, &j);
  932. if (error)
  933. goto error_rcur;
  934. XFS_WANT_CORRUPTED_GOTO(j == 1, error_rcur);
  935. }
  936. XFS_WANT_CORRUPTED_GOTO(i == 1 || j == 1, error_rcur);
  937. if (i == 1 && j == 1) {
  938. /*
  939. * Both the left and right records are valid. Choose the closer
  940. * inode chunk to the target.
  941. */
  942. if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
  943. (rrec.ir_startino - pagino)) {
  944. *rec = rrec;
  945. xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
  946. *ocur = rcur;
  947. } else {
  948. xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
  949. }
  950. } else if (j == 1) {
  951. /* only the right record is valid */
  952. *rec = rrec;
  953. xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
  954. *ocur = rcur;
  955. } else if (i == 1) {
  956. /* only the left record is valid */
  957. xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
  958. }
  959. return 0;
  960. error_rcur:
  961. xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
  962. return error;
  963. }
  964. /*
  965. * Use the free inode btree to find a free inode based on a newino hint. If
  966. * the hint is NULL, find the first free inode in the AG.
  967. */
  968. STATIC int
  969. xfs_dialloc_ag_finobt_newino(
  970. struct xfs_agi *agi,
  971. struct xfs_btree_cur *cur,
  972. struct xfs_inobt_rec_incore *rec)
  973. {
  974. int error;
  975. int i;
  976. if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
  977. error = xfs_inobt_lookup(cur, agi->agi_newino, XFS_LOOKUP_EQ,
  978. &i);
  979. if (error)
  980. return error;
  981. if (i == 1) {
  982. error = xfs_inobt_get_rec(cur, rec, &i);
  983. if (error)
  984. return error;
  985. XFS_WANT_CORRUPTED_RETURN(i == 1);
  986. return 0;
  987. }
  988. }
  989. /*
  990. * Find the first inode available in the AG.
  991. */
  992. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  993. if (error)
  994. return error;
  995. XFS_WANT_CORRUPTED_RETURN(i == 1);
  996. error = xfs_inobt_get_rec(cur, rec, &i);
  997. if (error)
  998. return error;
  999. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1000. return 0;
  1001. }
  1002. /*
  1003. * Update the inobt based on a modification made to the finobt. Also ensure that
  1004. * the records from both trees are equivalent post-modification.
  1005. */
  1006. STATIC int
  1007. xfs_dialloc_ag_update_inobt(
  1008. struct xfs_btree_cur *cur, /* inobt cursor */
  1009. struct xfs_inobt_rec_incore *frec, /* finobt record */
  1010. int offset) /* inode offset */
  1011. {
  1012. struct xfs_inobt_rec_incore rec;
  1013. int error;
  1014. int i;
  1015. error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
  1016. if (error)
  1017. return error;
  1018. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1019. error = xfs_inobt_get_rec(cur, &rec, &i);
  1020. if (error)
  1021. return error;
  1022. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1023. ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
  1024. XFS_INODES_PER_CHUNK) == 0);
  1025. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1026. rec.ir_freecount--;
  1027. XFS_WANT_CORRUPTED_RETURN((rec.ir_free == frec->ir_free) &&
  1028. (rec.ir_freecount == frec->ir_freecount));
  1029. error = xfs_inobt_update(cur, &rec);
  1030. if (error)
  1031. return error;
  1032. return 0;
  1033. }
  1034. /*
  1035. * Allocate an inode using the free inode btree, if available. Otherwise, fall
  1036. * back to the inobt search algorithm.
  1037. *
  1038. * The caller selected an AG for us, and made sure that free inodes are
  1039. * available.
  1040. */
  1041. STATIC int
  1042. xfs_dialloc_ag(
  1043. struct xfs_trans *tp,
  1044. struct xfs_buf *agbp,
  1045. xfs_ino_t parent,
  1046. xfs_ino_t *inop)
  1047. {
  1048. struct xfs_mount *mp = tp->t_mountp;
  1049. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1050. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1051. xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
  1052. xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
  1053. struct xfs_perag *pag;
  1054. struct xfs_btree_cur *cur; /* finobt cursor */
  1055. struct xfs_btree_cur *icur; /* inobt cursor */
  1056. struct xfs_inobt_rec_incore rec;
  1057. xfs_ino_t ino;
  1058. int error;
  1059. int offset;
  1060. int i;
  1061. if (!xfs_sb_version_hasfinobt(&mp->m_sb))
  1062. return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
  1063. pag = xfs_perag_get(mp, agno);
  1064. /*
  1065. * If pagino is 0 (this is the root inode allocation) use newino.
  1066. * This must work because we've just allocated some.
  1067. */
  1068. if (!pagino)
  1069. pagino = be32_to_cpu(agi->agi_newino);
  1070. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
  1071. error = xfs_check_agi_freecount(cur, agi);
  1072. if (error)
  1073. goto error_cur;
  1074. /*
  1075. * The search algorithm depends on whether we're in the same AG as the
  1076. * parent. If so, find the closest available inode to the parent. If
  1077. * not, consider the agi hint or find the first free inode in the AG.
  1078. */
  1079. if (agno == pagno)
  1080. error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
  1081. else
  1082. error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
  1083. if (error)
  1084. goto error_cur;
  1085. offset = xfs_lowbit64(rec.ir_free);
  1086. ASSERT(offset >= 0);
  1087. ASSERT(offset < XFS_INODES_PER_CHUNK);
  1088. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  1089. XFS_INODES_PER_CHUNK) == 0);
  1090. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  1091. /*
  1092. * Modify or remove the finobt record.
  1093. */
  1094. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1095. rec.ir_freecount--;
  1096. if (rec.ir_freecount)
  1097. error = xfs_inobt_update(cur, &rec);
  1098. else
  1099. error = xfs_btree_delete(cur, &i);
  1100. if (error)
  1101. goto error_cur;
  1102. /*
  1103. * The finobt has now been updated appropriately. We haven't updated the
  1104. * agi and superblock yet, so we can create an inobt cursor and validate
  1105. * the original freecount. If all is well, make the equivalent update to
  1106. * the inobt using the finobt record and offset information.
  1107. */
  1108. icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1109. error = xfs_check_agi_freecount(icur, agi);
  1110. if (error)
  1111. goto error_icur;
  1112. error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
  1113. if (error)
  1114. goto error_icur;
  1115. /*
  1116. * Both trees have now been updated. We must update the perag and
  1117. * superblock before we can check the freecount for each btree.
  1118. */
  1119. be32_add_cpu(&agi->agi_freecount, -1);
  1120. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1121. pag->pagi_freecount--;
  1122. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  1123. error = xfs_check_agi_freecount(icur, agi);
  1124. if (error)
  1125. goto error_icur;
  1126. error = xfs_check_agi_freecount(cur, agi);
  1127. if (error)
  1128. goto error_icur;
  1129. xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
  1130. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1131. xfs_perag_put(pag);
  1132. *inop = ino;
  1133. return 0;
  1134. error_icur:
  1135. xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
  1136. error_cur:
  1137. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1138. xfs_perag_put(pag);
  1139. return error;
  1140. }
  1141. /*
  1142. * Allocate an inode on disk.
  1143. *
  1144. * Mode is used to tell whether the new inode will need space, and whether it
  1145. * is a directory.
  1146. *
  1147. * This function is designed to be called twice if it has to do an allocation
  1148. * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
  1149. * If an inode is available without having to performn an allocation, an inode
  1150. * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
  1151. * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
  1152. * The caller should then commit the current transaction, allocate a
  1153. * new transaction, and call xfs_dialloc() again, passing in the previous value
  1154. * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
  1155. * buffer is locked across the two calls, the second call is guaranteed to have
  1156. * a free inode available.
  1157. *
  1158. * Once we successfully pick an inode its number is returned and the on-disk
  1159. * data structures are updated. The inode itself is not read in, since doing so
  1160. * would break ordering constraints with xfs_reclaim.
  1161. */
  1162. int
  1163. xfs_dialloc(
  1164. struct xfs_trans *tp,
  1165. xfs_ino_t parent,
  1166. umode_t mode,
  1167. int okalloc,
  1168. struct xfs_buf **IO_agbp,
  1169. xfs_ino_t *inop)
  1170. {
  1171. struct xfs_mount *mp = tp->t_mountp;
  1172. struct xfs_buf *agbp;
  1173. xfs_agnumber_t agno;
  1174. int error;
  1175. int ialloced;
  1176. int noroom = 0;
  1177. xfs_agnumber_t start_agno;
  1178. struct xfs_perag *pag;
  1179. if (*IO_agbp) {
  1180. /*
  1181. * If the caller passes in a pointer to the AGI buffer,
  1182. * continue where we left off before. In this case, we
  1183. * know that the allocation group has free inodes.
  1184. */
  1185. agbp = *IO_agbp;
  1186. goto out_alloc;
  1187. }
  1188. /*
  1189. * We do not have an agbp, so select an initial allocation
  1190. * group for inode allocation.
  1191. */
  1192. start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
  1193. if (start_agno == NULLAGNUMBER) {
  1194. *inop = NULLFSINO;
  1195. return 0;
  1196. }
  1197. /*
  1198. * If we have already hit the ceiling of inode blocks then clear
  1199. * okalloc so we scan all available agi structures for a free
  1200. * inode.
  1201. */
  1202. if (mp->m_maxicount &&
  1203. mp->m_sb.sb_icount + mp->m_ialloc_inos > mp->m_maxicount) {
  1204. noroom = 1;
  1205. okalloc = 0;
  1206. }
  1207. /*
  1208. * Loop until we find an allocation group that either has free inodes
  1209. * or in which we can allocate some inodes. Iterate through the
  1210. * allocation groups upward, wrapping at the end.
  1211. */
  1212. agno = start_agno;
  1213. for (;;) {
  1214. pag = xfs_perag_get(mp, agno);
  1215. if (!pag->pagi_inodeok) {
  1216. xfs_ialloc_next_ag(mp);
  1217. goto nextag;
  1218. }
  1219. if (!pag->pagi_init) {
  1220. error = xfs_ialloc_pagi_init(mp, tp, agno);
  1221. if (error)
  1222. goto out_error;
  1223. }
  1224. /*
  1225. * Do a first racy fast path check if this AG is usable.
  1226. */
  1227. if (!pag->pagi_freecount && !okalloc)
  1228. goto nextag;
  1229. /*
  1230. * Then read in the AGI buffer and recheck with the AGI buffer
  1231. * lock held.
  1232. */
  1233. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1234. if (error)
  1235. goto out_error;
  1236. if (pag->pagi_freecount) {
  1237. xfs_perag_put(pag);
  1238. goto out_alloc;
  1239. }
  1240. if (!okalloc)
  1241. goto nextag_relse_buffer;
  1242. error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
  1243. if (error) {
  1244. xfs_trans_brelse(tp, agbp);
  1245. if (error != ENOSPC)
  1246. goto out_error;
  1247. xfs_perag_put(pag);
  1248. *inop = NULLFSINO;
  1249. return 0;
  1250. }
  1251. if (ialloced) {
  1252. /*
  1253. * We successfully allocated some inodes, return
  1254. * the current context to the caller so that it
  1255. * can commit the current transaction and call
  1256. * us again where we left off.
  1257. */
  1258. ASSERT(pag->pagi_freecount > 0);
  1259. xfs_perag_put(pag);
  1260. *IO_agbp = agbp;
  1261. *inop = NULLFSINO;
  1262. return 0;
  1263. }
  1264. nextag_relse_buffer:
  1265. xfs_trans_brelse(tp, agbp);
  1266. nextag:
  1267. xfs_perag_put(pag);
  1268. if (++agno == mp->m_sb.sb_agcount)
  1269. agno = 0;
  1270. if (agno == start_agno) {
  1271. *inop = NULLFSINO;
  1272. return noroom ? ENOSPC : 0;
  1273. }
  1274. }
  1275. out_alloc:
  1276. *IO_agbp = NULL;
  1277. return xfs_dialloc_ag(tp, agbp, parent, inop);
  1278. out_error:
  1279. xfs_perag_put(pag);
  1280. return XFS_ERROR(error);
  1281. }
  1282. STATIC int
  1283. xfs_difree_inobt(
  1284. struct xfs_mount *mp,
  1285. struct xfs_trans *tp,
  1286. struct xfs_buf *agbp,
  1287. xfs_agino_t agino,
  1288. struct xfs_bmap_free *flist,
  1289. int *delete,
  1290. xfs_ino_t *first_ino,
  1291. struct xfs_inobt_rec_incore *orec)
  1292. {
  1293. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1294. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1295. struct xfs_perag *pag;
  1296. struct xfs_btree_cur *cur;
  1297. struct xfs_inobt_rec_incore rec;
  1298. int ilen;
  1299. int error;
  1300. int i;
  1301. int off;
  1302. ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
  1303. ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
  1304. /*
  1305. * Initialize the cursor.
  1306. */
  1307. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1308. error = xfs_check_agi_freecount(cur, agi);
  1309. if (error)
  1310. goto error0;
  1311. /*
  1312. * Look for the entry describing this inode.
  1313. */
  1314. if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
  1315. xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
  1316. __func__, error);
  1317. goto error0;
  1318. }
  1319. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1320. error = xfs_inobt_get_rec(cur, &rec, &i);
  1321. if (error) {
  1322. xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
  1323. __func__, error);
  1324. goto error0;
  1325. }
  1326. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1327. /*
  1328. * Get the offset in the inode chunk.
  1329. */
  1330. off = agino - rec.ir_startino;
  1331. ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
  1332. ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
  1333. /*
  1334. * Mark the inode free & increment the count.
  1335. */
  1336. rec.ir_free |= XFS_INOBT_MASK(off);
  1337. rec.ir_freecount++;
  1338. /*
  1339. * When an inode cluster is free, it becomes eligible for removal
  1340. */
  1341. if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
  1342. (rec.ir_freecount == mp->m_ialloc_inos)) {
  1343. *delete = 1;
  1344. *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
  1345. /*
  1346. * Remove the inode cluster from the AGI B+Tree, adjust the
  1347. * AGI and Superblock inode counts, and mark the disk space
  1348. * to be freed when the transaction is committed.
  1349. */
  1350. ilen = mp->m_ialloc_inos;
  1351. be32_add_cpu(&agi->agi_count, -ilen);
  1352. be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
  1353. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
  1354. pag = xfs_perag_get(mp, agno);
  1355. pag->pagi_freecount -= ilen - 1;
  1356. xfs_perag_put(pag);
  1357. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
  1358. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
  1359. if ((error = xfs_btree_delete(cur, &i))) {
  1360. xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
  1361. __func__, error);
  1362. goto error0;
  1363. }
  1364. xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
  1365. XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
  1366. mp->m_ialloc_blks, flist, mp);
  1367. } else {
  1368. *delete = 0;
  1369. error = xfs_inobt_update(cur, &rec);
  1370. if (error) {
  1371. xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
  1372. __func__, error);
  1373. goto error0;
  1374. }
  1375. /*
  1376. * Change the inode free counts and log the ag/sb changes.
  1377. */
  1378. be32_add_cpu(&agi->agi_freecount, 1);
  1379. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1380. pag = xfs_perag_get(mp, agno);
  1381. pag->pagi_freecount++;
  1382. xfs_perag_put(pag);
  1383. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
  1384. }
  1385. error = xfs_check_agi_freecount(cur, agi);
  1386. if (error)
  1387. goto error0;
  1388. *orec = rec;
  1389. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1390. return 0;
  1391. error0:
  1392. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1393. return error;
  1394. }
  1395. /*
  1396. * Free an inode in the free inode btree.
  1397. */
  1398. STATIC int
  1399. xfs_difree_finobt(
  1400. struct xfs_mount *mp,
  1401. struct xfs_trans *tp,
  1402. struct xfs_buf *agbp,
  1403. xfs_agino_t agino,
  1404. struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
  1405. {
  1406. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1407. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1408. struct xfs_btree_cur *cur;
  1409. struct xfs_inobt_rec_incore rec;
  1410. int offset = agino - ibtrec->ir_startino;
  1411. int error;
  1412. int i;
  1413. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
  1414. error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
  1415. if (error)
  1416. goto error;
  1417. if (i == 0) {
  1418. /*
  1419. * If the record does not exist in the finobt, we must have just
  1420. * freed an inode in a previously fully allocated chunk. If not,
  1421. * something is out of sync.
  1422. */
  1423. XFS_WANT_CORRUPTED_GOTO(ibtrec->ir_freecount == 1, error);
  1424. error = xfs_inobt_insert_rec(cur, ibtrec->ir_freecount,
  1425. ibtrec->ir_free, &i);
  1426. if (error)
  1427. goto error;
  1428. ASSERT(i == 1);
  1429. goto out;
  1430. }
  1431. /*
  1432. * Read and update the existing record. We could just copy the ibtrec
  1433. * across here, but that would defeat the purpose of having redundant
  1434. * metadata. By making the modifications independently, we can catch
  1435. * corruptions that we wouldn't see if we just copied from one record
  1436. * to another.
  1437. */
  1438. error = xfs_inobt_get_rec(cur, &rec, &i);
  1439. if (error)
  1440. goto error;
  1441. XFS_WANT_CORRUPTED_GOTO(i == 1, error);
  1442. rec.ir_free |= XFS_INOBT_MASK(offset);
  1443. rec.ir_freecount++;
  1444. XFS_WANT_CORRUPTED_GOTO((rec.ir_free == ibtrec->ir_free) &&
  1445. (rec.ir_freecount == ibtrec->ir_freecount),
  1446. error);
  1447. /*
  1448. * The content of inobt records should always match between the inobt
  1449. * and finobt. The lifecycle of records in the finobt is different from
  1450. * the inobt in that the finobt only tracks records with at least one
  1451. * free inode. Hence, if all of the inodes are free and we aren't
  1452. * keeping inode chunks permanently on disk, remove the record.
  1453. * Otherwise, update the record with the new information.
  1454. */
  1455. if (rec.ir_freecount == mp->m_ialloc_inos &&
  1456. !(mp->m_flags & XFS_MOUNT_IKEEP)) {
  1457. error = xfs_btree_delete(cur, &i);
  1458. if (error)
  1459. goto error;
  1460. ASSERT(i == 1);
  1461. } else {
  1462. error = xfs_inobt_update(cur, &rec);
  1463. if (error)
  1464. goto error;
  1465. }
  1466. out:
  1467. error = xfs_check_agi_freecount(cur, agi);
  1468. if (error)
  1469. goto error;
  1470. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1471. return 0;
  1472. error:
  1473. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1474. return error;
  1475. }
  1476. /*
  1477. * Free disk inode. Carefully avoids touching the incore inode, all
  1478. * manipulations incore are the caller's responsibility.
  1479. * The on-disk inode is not changed by this operation, only the
  1480. * btree (free inode mask) is changed.
  1481. */
  1482. int
  1483. xfs_difree(
  1484. struct xfs_trans *tp, /* transaction pointer */
  1485. xfs_ino_t inode, /* inode to be freed */
  1486. struct xfs_bmap_free *flist, /* extents to free */
  1487. int *delete,/* set if inode cluster was deleted */
  1488. xfs_ino_t *first_ino)/* first inode in deleted cluster */
  1489. {
  1490. /* REFERENCED */
  1491. xfs_agblock_t agbno; /* block number containing inode */
  1492. struct xfs_buf *agbp; /* buffer for allocation group header */
  1493. xfs_agino_t agino; /* allocation group inode number */
  1494. xfs_agnumber_t agno; /* allocation group number */
  1495. int error; /* error return value */
  1496. struct xfs_mount *mp; /* mount structure for filesystem */
  1497. struct xfs_inobt_rec_incore rec;/* btree record */
  1498. mp = tp->t_mountp;
  1499. /*
  1500. * Break up inode number into its components.
  1501. */
  1502. agno = XFS_INO_TO_AGNO(mp, inode);
  1503. if (agno >= mp->m_sb.sb_agcount) {
  1504. xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
  1505. __func__, agno, mp->m_sb.sb_agcount);
  1506. ASSERT(0);
  1507. return XFS_ERROR(EINVAL);
  1508. }
  1509. agino = XFS_INO_TO_AGINO(mp, inode);
  1510. if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1511. xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
  1512. __func__, (unsigned long long)inode,
  1513. (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
  1514. ASSERT(0);
  1515. return XFS_ERROR(EINVAL);
  1516. }
  1517. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1518. if (agbno >= mp->m_sb.sb_agblocks) {
  1519. xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
  1520. __func__, agbno, mp->m_sb.sb_agblocks);
  1521. ASSERT(0);
  1522. return XFS_ERROR(EINVAL);
  1523. }
  1524. /*
  1525. * Get the allocation group header.
  1526. */
  1527. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1528. if (error) {
  1529. xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
  1530. __func__, error);
  1531. return error;
  1532. }
  1533. /*
  1534. * Fix up the inode allocation btree.
  1535. */
  1536. error = xfs_difree_inobt(mp, tp, agbp, agino, flist, delete, first_ino,
  1537. &rec);
  1538. if (error)
  1539. goto error0;
  1540. /*
  1541. * Fix up the free inode btree.
  1542. */
  1543. if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
  1544. error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
  1545. if (error)
  1546. goto error0;
  1547. }
  1548. return 0;
  1549. error0:
  1550. return error;
  1551. }
  1552. STATIC int
  1553. xfs_imap_lookup(
  1554. struct xfs_mount *mp,
  1555. struct xfs_trans *tp,
  1556. xfs_agnumber_t agno,
  1557. xfs_agino_t agino,
  1558. xfs_agblock_t agbno,
  1559. xfs_agblock_t *chunk_agbno,
  1560. xfs_agblock_t *offset_agbno,
  1561. int flags)
  1562. {
  1563. struct xfs_inobt_rec_incore rec;
  1564. struct xfs_btree_cur *cur;
  1565. struct xfs_buf *agbp;
  1566. int error;
  1567. int i;
  1568. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1569. if (error) {
  1570. xfs_alert(mp,
  1571. "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
  1572. __func__, error, agno);
  1573. return error;
  1574. }
  1575. /*
  1576. * Lookup the inode record for the given agino. If the record cannot be
  1577. * found, then it's an invalid inode number and we should abort. Once
  1578. * we have a record, we need to ensure it contains the inode number
  1579. * we are looking up.
  1580. */
  1581. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1582. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
  1583. if (!error) {
  1584. if (i)
  1585. error = xfs_inobt_get_rec(cur, &rec, &i);
  1586. if (!error && i == 0)
  1587. error = EINVAL;
  1588. }
  1589. xfs_trans_brelse(tp, agbp);
  1590. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1591. if (error)
  1592. return error;
  1593. /* check that the returned record contains the required inode */
  1594. if (rec.ir_startino > agino ||
  1595. rec.ir_startino + mp->m_ialloc_inos <= agino)
  1596. return EINVAL;
  1597. /* for untrusted inodes check it is allocated first */
  1598. if ((flags & XFS_IGET_UNTRUSTED) &&
  1599. (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
  1600. return EINVAL;
  1601. *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
  1602. *offset_agbno = agbno - *chunk_agbno;
  1603. return 0;
  1604. }
  1605. /*
  1606. * Return the location of the inode in imap, for mapping it into a buffer.
  1607. */
  1608. int
  1609. xfs_imap(
  1610. xfs_mount_t *mp, /* file system mount structure */
  1611. xfs_trans_t *tp, /* transaction pointer */
  1612. xfs_ino_t ino, /* inode to locate */
  1613. struct xfs_imap *imap, /* location map structure */
  1614. uint flags) /* flags for inode btree lookup */
  1615. {
  1616. xfs_agblock_t agbno; /* block number of inode in the alloc group */
  1617. xfs_agino_t agino; /* inode number within alloc group */
  1618. xfs_agnumber_t agno; /* allocation group number */
  1619. int blks_per_cluster; /* num blocks per inode cluster */
  1620. xfs_agblock_t chunk_agbno; /* first block in inode chunk */
  1621. xfs_agblock_t cluster_agbno; /* first block in inode cluster */
  1622. int error; /* error code */
  1623. int offset; /* index of inode in its buffer */
  1624. xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
  1625. ASSERT(ino != NULLFSINO);
  1626. /*
  1627. * Split up the inode number into its parts.
  1628. */
  1629. agno = XFS_INO_TO_AGNO(mp, ino);
  1630. agino = XFS_INO_TO_AGINO(mp, ino);
  1631. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1632. if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
  1633. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1634. #ifdef DEBUG
  1635. /*
  1636. * Don't output diagnostic information for untrusted inodes
  1637. * as they can be invalid without implying corruption.
  1638. */
  1639. if (flags & XFS_IGET_UNTRUSTED)
  1640. return XFS_ERROR(EINVAL);
  1641. if (agno >= mp->m_sb.sb_agcount) {
  1642. xfs_alert(mp,
  1643. "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
  1644. __func__, agno, mp->m_sb.sb_agcount);
  1645. }
  1646. if (agbno >= mp->m_sb.sb_agblocks) {
  1647. xfs_alert(mp,
  1648. "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
  1649. __func__, (unsigned long long)agbno,
  1650. (unsigned long)mp->m_sb.sb_agblocks);
  1651. }
  1652. if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1653. xfs_alert(mp,
  1654. "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
  1655. __func__, ino,
  1656. XFS_AGINO_TO_INO(mp, agno, agino));
  1657. }
  1658. xfs_stack_trace();
  1659. #endif /* DEBUG */
  1660. return XFS_ERROR(EINVAL);
  1661. }
  1662. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1663. /*
  1664. * For bulkstat and handle lookups, we have an untrusted inode number
  1665. * that we have to verify is valid. We cannot do this just by reading
  1666. * the inode buffer as it may have been unlinked and removed leaving
  1667. * inodes in stale state on disk. Hence we have to do a btree lookup
  1668. * in all cases where an untrusted inode number is passed.
  1669. */
  1670. if (flags & XFS_IGET_UNTRUSTED) {
  1671. error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
  1672. &chunk_agbno, &offset_agbno, flags);
  1673. if (error)
  1674. return error;
  1675. goto out_map;
  1676. }
  1677. /*
  1678. * If the inode cluster size is the same as the blocksize or
  1679. * smaller we get to the buffer by simple arithmetics.
  1680. */
  1681. if (blks_per_cluster == 1) {
  1682. offset = XFS_INO_TO_OFFSET(mp, ino);
  1683. ASSERT(offset < mp->m_sb.sb_inopblock);
  1684. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
  1685. imap->im_len = XFS_FSB_TO_BB(mp, 1);
  1686. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1687. return 0;
  1688. }
  1689. /*
  1690. * If the inode chunks are aligned then use simple maths to
  1691. * find the location. Otherwise we have to do a btree
  1692. * lookup to find the location.
  1693. */
  1694. if (mp->m_inoalign_mask) {
  1695. offset_agbno = agbno & mp->m_inoalign_mask;
  1696. chunk_agbno = agbno - offset_agbno;
  1697. } else {
  1698. error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
  1699. &chunk_agbno, &offset_agbno, flags);
  1700. if (error)
  1701. return error;
  1702. }
  1703. out_map:
  1704. ASSERT(agbno >= chunk_agbno);
  1705. cluster_agbno = chunk_agbno +
  1706. ((offset_agbno / blks_per_cluster) * blks_per_cluster);
  1707. offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
  1708. XFS_INO_TO_OFFSET(mp, ino);
  1709. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
  1710. imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
  1711. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1712. /*
  1713. * If the inode number maps to a block outside the bounds
  1714. * of the file system then return NULL rather than calling
  1715. * read_buf and panicing when we get an error from the
  1716. * driver.
  1717. */
  1718. if ((imap->im_blkno + imap->im_len) >
  1719. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  1720. xfs_alert(mp,
  1721. "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
  1722. __func__, (unsigned long long) imap->im_blkno,
  1723. (unsigned long long) imap->im_len,
  1724. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
  1725. return XFS_ERROR(EINVAL);
  1726. }
  1727. return 0;
  1728. }
  1729. /*
  1730. * Compute and fill in value of m_in_maxlevels.
  1731. */
  1732. void
  1733. xfs_ialloc_compute_maxlevels(
  1734. xfs_mount_t *mp) /* file system mount structure */
  1735. {
  1736. int level;
  1737. uint maxblocks;
  1738. uint maxleafents;
  1739. int minleafrecs;
  1740. int minnoderecs;
  1741. maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
  1742. XFS_INODES_PER_CHUNK_LOG;
  1743. minleafrecs = mp->m_alloc_mnr[0];
  1744. minnoderecs = mp->m_alloc_mnr[1];
  1745. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1746. for (level = 1; maxblocks > 1; level++)
  1747. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1748. mp->m_in_maxlevels = level;
  1749. }
  1750. /*
  1751. * Log specified fields for the ag hdr (inode section). The growth of the agi
  1752. * structure over time requires that we interpret the buffer as two logical
  1753. * regions delineated by the end of the unlinked list. This is due to the size
  1754. * of the hash table and its location in the middle of the agi.
  1755. *
  1756. * For example, a request to log a field before agi_unlinked and a field after
  1757. * agi_unlinked could cause us to log the entire hash table and use an excessive
  1758. * amount of log space. To avoid this behavior, log the region up through
  1759. * agi_unlinked in one call and the region after agi_unlinked through the end of
  1760. * the structure in another.
  1761. */
  1762. void
  1763. xfs_ialloc_log_agi(
  1764. xfs_trans_t *tp, /* transaction pointer */
  1765. xfs_buf_t *bp, /* allocation group header buffer */
  1766. int fields) /* bitmask of fields to log */
  1767. {
  1768. int first; /* first byte number */
  1769. int last; /* last byte number */
  1770. static const short offsets[] = { /* field starting offsets */
  1771. /* keep in sync with bit definitions */
  1772. offsetof(xfs_agi_t, agi_magicnum),
  1773. offsetof(xfs_agi_t, agi_versionnum),
  1774. offsetof(xfs_agi_t, agi_seqno),
  1775. offsetof(xfs_agi_t, agi_length),
  1776. offsetof(xfs_agi_t, agi_count),
  1777. offsetof(xfs_agi_t, agi_root),
  1778. offsetof(xfs_agi_t, agi_level),
  1779. offsetof(xfs_agi_t, agi_freecount),
  1780. offsetof(xfs_agi_t, agi_newino),
  1781. offsetof(xfs_agi_t, agi_dirino),
  1782. offsetof(xfs_agi_t, agi_unlinked),
  1783. offsetof(xfs_agi_t, agi_free_root),
  1784. offsetof(xfs_agi_t, agi_free_level),
  1785. sizeof(xfs_agi_t)
  1786. };
  1787. #ifdef DEBUG
  1788. xfs_agi_t *agi; /* allocation group header */
  1789. agi = XFS_BUF_TO_AGI(bp);
  1790. ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
  1791. #endif
  1792. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
  1793. /*
  1794. * Compute byte offsets for the first and last fields in the first
  1795. * region and log the agi buffer. This only logs up through
  1796. * agi_unlinked.
  1797. */
  1798. if (fields & XFS_AGI_ALL_BITS_R1) {
  1799. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
  1800. &first, &last);
  1801. xfs_trans_log_buf(tp, bp, first, last);
  1802. }
  1803. /*
  1804. * Mask off the bits in the first region and calculate the first and
  1805. * last field offsets for any bits in the second region.
  1806. */
  1807. fields &= ~XFS_AGI_ALL_BITS_R1;
  1808. if (fields) {
  1809. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
  1810. &first, &last);
  1811. xfs_trans_log_buf(tp, bp, first, last);
  1812. }
  1813. }
  1814. #ifdef DEBUG
  1815. STATIC void
  1816. xfs_check_agi_unlinked(
  1817. struct xfs_agi *agi)
  1818. {
  1819. int i;
  1820. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
  1821. ASSERT(agi->agi_unlinked[i]);
  1822. }
  1823. #else
  1824. #define xfs_check_agi_unlinked(agi)
  1825. #endif
  1826. static bool
  1827. xfs_agi_verify(
  1828. struct xfs_buf *bp)
  1829. {
  1830. struct xfs_mount *mp = bp->b_target->bt_mount;
  1831. struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
  1832. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  1833. !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
  1834. return false;
  1835. /*
  1836. * Validate the magic number of the agi block.
  1837. */
  1838. if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
  1839. return false;
  1840. if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
  1841. return false;
  1842. /*
  1843. * during growfs operations, the perag is not fully initialised,
  1844. * so we can't use it for any useful checking. growfs ensures we can't
  1845. * use it by using uncached buffers that don't have the perag attached
  1846. * so we can detect and avoid this problem.
  1847. */
  1848. if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
  1849. return false;
  1850. xfs_check_agi_unlinked(agi);
  1851. return true;
  1852. }
  1853. static void
  1854. xfs_agi_read_verify(
  1855. struct xfs_buf *bp)
  1856. {
  1857. struct xfs_mount *mp = bp->b_target->bt_mount;
  1858. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  1859. !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
  1860. xfs_buf_ioerror(bp, EFSBADCRC);
  1861. else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
  1862. XFS_ERRTAG_IALLOC_READ_AGI,
  1863. XFS_RANDOM_IALLOC_READ_AGI))
  1864. xfs_buf_ioerror(bp, EFSCORRUPTED);
  1865. if (bp->b_error)
  1866. xfs_verifier_error(bp);
  1867. }
  1868. static void
  1869. xfs_agi_write_verify(
  1870. struct xfs_buf *bp)
  1871. {
  1872. struct xfs_mount *mp = bp->b_target->bt_mount;
  1873. struct xfs_buf_log_item *bip = bp->b_fspriv;
  1874. if (!xfs_agi_verify(bp)) {
  1875. xfs_buf_ioerror(bp, EFSCORRUPTED);
  1876. xfs_verifier_error(bp);
  1877. return;
  1878. }
  1879. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1880. return;
  1881. if (bip)
  1882. XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  1883. xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
  1884. }
  1885. const struct xfs_buf_ops xfs_agi_buf_ops = {
  1886. .verify_read = xfs_agi_read_verify,
  1887. .verify_write = xfs_agi_write_verify,
  1888. };
  1889. /*
  1890. * Read in the allocation group header (inode allocation section)
  1891. */
  1892. int
  1893. xfs_read_agi(
  1894. struct xfs_mount *mp, /* file system mount structure */
  1895. struct xfs_trans *tp, /* transaction pointer */
  1896. xfs_agnumber_t agno, /* allocation group number */
  1897. struct xfs_buf **bpp) /* allocation group hdr buf */
  1898. {
  1899. int error;
  1900. trace_xfs_read_agi(mp, agno);
  1901. ASSERT(agno != NULLAGNUMBER);
  1902. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  1903. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  1904. XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
  1905. if (error)
  1906. return error;
  1907. ASSERT(!xfs_buf_geterror(*bpp));
  1908. xfs_buf_set_ref(*bpp, XFS_AGI_REF);
  1909. return 0;
  1910. }
  1911. int
  1912. xfs_ialloc_read_agi(
  1913. struct xfs_mount *mp, /* file system mount structure */
  1914. struct xfs_trans *tp, /* transaction pointer */
  1915. xfs_agnumber_t agno, /* allocation group number */
  1916. struct xfs_buf **bpp) /* allocation group hdr buf */
  1917. {
  1918. struct xfs_agi *agi; /* allocation group header */
  1919. struct xfs_perag *pag; /* per allocation group data */
  1920. int error;
  1921. trace_xfs_ialloc_read_agi(mp, agno);
  1922. error = xfs_read_agi(mp, tp, agno, bpp);
  1923. if (error)
  1924. return error;
  1925. agi = XFS_BUF_TO_AGI(*bpp);
  1926. pag = xfs_perag_get(mp, agno);
  1927. if (!pag->pagi_init) {
  1928. pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
  1929. pag->pagi_count = be32_to_cpu(agi->agi_count);
  1930. pag->pagi_init = 1;
  1931. }
  1932. /*
  1933. * It's possible for these to be out of sync if
  1934. * we are in the middle of a forced shutdown.
  1935. */
  1936. ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
  1937. XFS_FORCED_SHUTDOWN(mp));
  1938. xfs_perag_put(pag);
  1939. return 0;
  1940. }
  1941. /*
  1942. * Read in the agi to initialise the per-ag data in the mount structure
  1943. */
  1944. int
  1945. xfs_ialloc_pagi_init(
  1946. xfs_mount_t *mp, /* file system mount structure */
  1947. xfs_trans_t *tp, /* transaction pointer */
  1948. xfs_agnumber_t agno) /* allocation group number */
  1949. {
  1950. xfs_buf_t *bp = NULL;
  1951. int error;
  1952. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  1953. if (error)
  1954. return error;
  1955. if (bp)
  1956. xfs_trans_brelse(tp, bp);
  1957. return 0;
  1958. }