xfs_ialloc.c 57 KB

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