xfs_ialloc.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_ialloc.h"
  19. #include "xfs_ialloc_btree.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_rtalloc.h"
  22. #include "xfs_errortag.h"
  23. #include "xfs_error.h"
  24. #include "xfs_bmap.h"
  25. #include "xfs_cksum.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_icreate_item.h"
  29. #include "xfs_icache.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_log.h"
  32. #include "xfs_rmap.h"
  33. /*
  34. * Allocation group level functions.
  35. */
  36. int
  37. xfs_ialloc_cluster_alignment(
  38. struct xfs_mount *mp)
  39. {
  40. if (xfs_sb_version_hasalign(&mp->m_sb) &&
  41. mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp))
  42. return mp->m_sb.sb_inoalignmt;
  43. return 1;
  44. }
  45. /*
  46. * Lookup a record by ino in the btree given by cur.
  47. */
  48. int /* error */
  49. xfs_inobt_lookup(
  50. struct xfs_btree_cur *cur, /* btree cursor */
  51. xfs_agino_t ino, /* starting inode of chunk */
  52. xfs_lookup_t dir, /* <=, >=, == */
  53. int *stat) /* success/failure */
  54. {
  55. cur->bc_rec.i.ir_startino = ino;
  56. cur->bc_rec.i.ir_holemask = 0;
  57. cur->bc_rec.i.ir_count = 0;
  58. cur->bc_rec.i.ir_freecount = 0;
  59. cur->bc_rec.i.ir_free = 0;
  60. return xfs_btree_lookup(cur, dir, stat);
  61. }
  62. /*
  63. * Update the record referred to by cur to the value given.
  64. * This either works (return 0) or gets an EFSCORRUPTED error.
  65. */
  66. STATIC int /* error */
  67. xfs_inobt_update(
  68. struct xfs_btree_cur *cur, /* btree cursor */
  69. xfs_inobt_rec_incore_t *irec) /* btree record */
  70. {
  71. union xfs_btree_rec rec;
  72. rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
  73. if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
  74. rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
  75. rec.inobt.ir_u.sp.ir_count = irec->ir_count;
  76. rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
  77. } else {
  78. /* ir_holemask/ir_count not supported on-disk */
  79. rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
  80. }
  81. rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
  82. return xfs_btree_update(cur, &rec);
  83. }
  84. /* Convert on-disk btree record to incore inobt record. */
  85. void
  86. xfs_inobt_btrec_to_irec(
  87. struct xfs_mount *mp,
  88. union xfs_btree_rec *rec,
  89. struct xfs_inobt_rec_incore *irec)
  90. {
  91. irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
  92. if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
  93. irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
  94. irec->ir_count = rec->inobt.ir_u.sp.ir_count;
  95. irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
  96. } else {
  97. /*
  98. * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
  99. * values for full inode chunks.
  100. */
  101. irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
  102. irec->ir_count = XFS_INODES_PER_CHUNK;
  103. irec->ir_freecount =
  104. be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
  105. }
  106. irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
  107. }
  108. /*
  109. * Get the data from the pointed-to record.
  110. */
  111. int
  112. xfs_inobt_get_rec(
  113. struct xfs_btree_cur *cur,
  114. struct xfs_inobt_rec_incore *irec,
  115. int *stat)
  116. {
  117. struct xfs_mount *mp = cur->bc_mp;
  118. xfs_agnumber_t agno = cur->bc_private.a.agno;
  119. union xfs_btree_rec *rec;
  120. int error;
  121. uint64_t realfree;
  122. error = xfs_btree_get_rec(cur, &rec, stat);
  123. if (error || *stat == 0)
  124. return error;
  125. xfs_inobt_btrec_to_irec(mp, rec, irec);
  126. if (!xfs_verify_agino(mp, agno, irec->ir_startino))
  127. goto out_bad_rec;
  128. if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
  129. irec->ir_count > XFS_INODES_PER_CHUNK)
  130. goto out_bad_rec;
  131. if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
  132. goto out_bad_rec;
  133. /* if there are no holes, return the first available offset */
  134. if (!xfs_inobt_issparse(irec->ir_holemask))
  135. realfree = irec->ir_free;
  136. else
  137. realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
  138. if (hweight64(realfree) != irec->ir_freecount)
  139. goto out_bad_rec;
  140. return 0;
  141. out_bad_rec:
  142. xfs_warn(mp,
  143. "%s Inode BTree record corruption in AG %d detected!",
  144. cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno);
  145. xfs_warn(mp,
  146. "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
  147. irec->ir_startino, irec->ir_count, irec->ir_freecount,
  148. irec->ir_free, irec->ir_holemask);
  149. return -EFSCORRUPTED;
  150. }
  151. /*
  152. * Insert a single inobt record. Cursor must already point to desired location.
  153. */
  154. int
  155. xfs_inobt_insert_rec(
  156. struct xfs_btree_cur *cur,
  157. uint16_t holemask,
  158. uint8_t count,
  159. int32_t freecount,
  160. xfs_inofree_t free,
  161. int *stat)
  162. {
  163. cur->bc_rec.i.ir_holemask = holemask;
  164. cur->bc_rec.i.ir_count = count;
  165. cur->bc_rec.i.ir_freecount = freecount;
  166. cur->bc_rec.i.ir_free = free;
  167. return xfs_btree_insert(cur, stat);
  168. }
  169. /*
  170. * Insert records describing a newly allocated inode chunk into the inobt.
  171. */
  172. STATIC int
  173. xfs_inobt_insert(
  174. struct xfs_mount *mp,
  175. struct xfs_trans *tp,
  176. struct xfs_buf *agbp,
  177. xfs_agino_t newino,
  178. xfs_agino_t newlen,
  179. xfs_btnum_t btnum)
  180. {
  181. struct xfs_btree_cur *cur;
  182. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  183. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  184. xfs_agino_t thisino;
  185. int i;
  186. int error;
  187. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
  188. for (thisino = newino;
  189. thisino < newino + newlen;
  190. thisino += XFS_INODES_PER_CHUNK) {
  191. error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
  192. if (error) {
  193. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  194. return error;
  195. }
  196. ASSERT(i == 0);
  197. error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
  198. XFS_INODES_PER_CHUNK,
  199. XFS_INODES_PER_CHUNK,
  200. XFS_INOBT_ALL_FREE, &i);
  201. if (error) {
  202. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  203. return error;
  204. }
  205. ASSERT(i == 1);
  206. }
  207. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  208. return 0;
  209. }
  210. /*
  211. * Verify that the number of free inodes in the AGI is correct.
  212. */
  213. #ifdef DEBUG
  214. STATIC int
  215. xfs_check_agi_freecount(
  216. struct xfs_btree_cur *cur,
  217. struct xfs_agi *agi)
  218. {
  219. if (cur->bc_nlevels == 1) {
  220. xfs_inobt_rec_incore_t rec;
  221. int freecount = 0;
  222. int error;
  223. int i;
  224. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  225. if (error)
  226. return error;
  227. do {
  228. error = xfs_inobt_get_rec(cur, &rec, &i);
  229. if (error)
  230. return error;
  231. if (i) {
  232. freecount += rec.ir_freecount;
  233. error = xfs_btree_increment(cur, 0, &i);
  234. if (error)
  235. return error;
  236. }
  237. } while (i == 1);
  238. if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
  239. ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
  240. }
  241. return 0;
  242. }
  243. #else
  244. #define xfs_check_agi_freecount(cur, agi) 0
  245. #endif
  246. /*
  247. * Initialise a new set of inodes. When called without a transaction context
  248. * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
  249. * than logging them (which in a transaction context puts them into the AIL
  250. * for writeback rather than the xfsbufd queue).
  251. */
  252. int
  253. xfs_ialloc_inode_init(
  254. struct xfs_mount *mp,
  255. struct xfs_trans *tp,
  256. struct list_head *buffer_list,
  257. int icount,
  258. xfs_agnumber_t agno,
  259. xfs_agblock_t agbno,
  260. xfs_agblock_t length,
  261. unsigned int gen)
  262. {
  263. struct xfs_buf *fbuf;
  264. struct xfs_dinode *free;
  265. int nbufs, blks_per_cluster, inodes_per_cluster;
  266. int version;
  267. int i, j;
  268. xfs_daddr_t d;
  269. xfs_ino_t ino = 0;
  270. /*
  271. * Loop over the new block(s), filling in the inodes. For small block
  272. * sizes, manipulate the inodes in buffers which are multiples of the
  273. * blocks size.
  274. */
  275. blks_per_cluster = xfs_icluster_size_fsb(mp);
  276. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  277. nbufs = length / blks_per_cluster;
  278. /*
  279. * Figure out what version number to use in the inodes we create. If
  280. * the superblock version has caught up to the one that supports the new
  281. * inode format, then use the new inode version. Otherwise use the old
  282. * version so that old kernels will continue to be able to use the file
  283. * system.
  284. *
  285. * For v3 inodes, we also need to write the inode number into the inode,
  286. * so calculate the first inode number of the chunk here as
  287. * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
  288. * across multiple filesystem blocks (such as a cluster) and so cannot
  289. * be used in the cluster buffer loop below.
  290. *
  291. * Further, because we are writing the inode directly into the buffer
  292. * and calculating a CRC on the entire inode, we have ot log the entire
  293. * inode so that the entire range the CRC covers is present in the log.
  294. * That means for v3 inode we log the entire buffer rather than just the
  295. * inode cores.
  296. */
  297. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  298. version = 3;
  299. ino = XFS_AGINO_TO_INO(mp, agno,
  300. XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
  301. /*
  302. * log the initialisation that is about to take place as an
  303. * logical operation. This means the transaction does not
  304. * need to log the physical changes to the inode buffers as log
  305. * recovery will know what initialisation is actually needed.
  306. * Hence we only need to log the buffers as "ordered" buffers so
  307. * they track in the AIL as if they were physically logged.
  308. */
  309. if (tp)
  310. xfs_icreate_log(tp, agno, agbno, icount,
  311. mp->m_sb.sb_inodesize, length, gen);
  312. } else
  313. version = 2;
  314. for (j = 0; j < nbufs; j++) {
  315. /*
  316. * Get the block.
  317. */
  318. d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
  319. fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  320. mp->m_bsize * blks_per_cluster,
  321. XBF_UNMAPPED);
  322. if (!fbuf)
  323. return -ENOMEM;
  324. /* Initialize the inode buffers and log them appropriately. */
  325. fbuf->b_ops = &xfs_inode_buf_ops;
  326. xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
  327. for (i = 0; i < inodes_per_cluster; i++) {
  328. int ioffset = i << mp->m_sb.sb_inodelog;
  329. uint isize = xfs_dinode_size(version);
  330. free = xfs_make_iptr(mp, fbuf, i);
  331. free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
  332. free->di_version = version;
  333. free->di_gen = cpu_to_be32(gen);
  334. free->di_next_unlinked = cpu_to_be32(NULLAGINO);
  335. if (version == 3) {
  336. free->di_ino = cpu_to_be64(ino);
  337. ino++;
  338. uuid_copy(&free->di_uuid,
  339. &mp->m_sb.sb_meta_uuid);
  340. xfs_dinode_calc_crc(mp, free);
  341. } else if (tp) {
  342. /* just log the inode core */
  343. xfs_trans_log_buf(tp, fbuf, ioffset,
  344. ioffset + isize - 1);
  345. }
  346. }
  347. if (tp) {
  348. /*
  349. * Mark the buffer as an inode allocation buffer so it
  350. * sticks in AIL at the point of this allocation
  351. * transaction. This ensures the they are on disk before
  352. * the tail of the log can be moved past this
  353. * transaction (i.e. by preventing relogging from moving
  354. * it forward in the log).
  355. */
  356. xfs_trans_inode_alloc_buf(tp, fbuf);
  357. if (version == 3) {
  358. /*
  359. * Mark the buffer as ordered so that they are
  360. * not physically logged in the transaction but
  361. * still tracked in the AIL as part of the
  362. * transaction and pin the log appropriately.
  363. */
  364. xfs_trans_ordered_buf(tp, fbuf);
  365. }
  366. } else {
  367. fbuf->b_flags |= XBF_DONE;
  368. xfs_buf_delwri_queue(fbuf, buffer_list);
  369. xfs_buf_relse(fbuf);
  370. }
  371. }
  372. return 0;
  373. }
  374. /*
  375. * Align startino and allocmask for a recently allocated sparse chunk such that
  376. * they are fit for insertion (or merge) into the on-disk inode btrees.
  377. *
  378. * Background:
  379. *
  380. * When enabled, sparse inode support increases the inode alignment from cluster
  381. * size to inode chunk size. This means that the minimum range between two
  382. * non-adjacent inode records in the inobt is large enough for a full inode
  383. * record. This allows for cluster sized, cluster aligned block allocation
  384. * without need to worry about whether the resulting inode record overlaps with
  385. * another record in the tree. Without this basic rule, we would have to deal
  386. * with the consequences of overlap by potentially undoing recent allocations in
  387. * the inode allocation codepath.
  388. *
  389. * Because of this alignment rule (which is enforced on mount), there are two
  390. * inobt possibilities for newly allocated sparse chunks. One is that the
  391. * aligned inode record for the chunk covers a range of inodes not already
  392. * covered in the inobt (i.e., it is safe to insert a new sparse record). The
  393. * other is that a record already exists at the aligned startino that considers
  394. * the newly allocated range as sparse. In the latter case, record content is
  395. * merged in hope that sparse inode chunks fill to full chunks over time.
  396. */
  397. STATIC void
  398. xfs_align_sparse_ino(
  399. struct xfs_mount *mp,
  400. xfs_agino_t *startino,
  401. uint16_t *allocmask)
  402. {
  403. xfs_agblock_t agbno;
  404. xfs_agblock_t mod;
  405. int offset;
  406. agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
  407. mod = agbno % mp->m_sb.sb_inoalignmt;
  408. if (!mod)
  409. return;
  410. /* calculate the inode offset and align startino */
  411. offset = mod << mp->m_sb.sb_inopblog;
  412. *startino -= offset;
  413. /*
  414. * Since startino has been aligned down, left shift allocmask such that
  415. * it continues to represent the same physical inodes relative to the
  416. * new startino.
  417. */
  418. *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
  419. }
  420. /*
  421. * Determine whether the source inode record can merge into the target. Both
  422. * records must be sparse, the inode ranges must match and there must be no
  423. * allocation overlap between the records.
  424. */
  425. STATIC bool
  426. __xfs_inobt_can_merge(
  427. struct xfs_inobt_rec_incore *trec, /* tgt record */
  428. struct xfs_inobt_rec_incore *srec) /* src record */
  429. {
  430. uint64_t talloc;
  431. uint64_t salloc;
  432. /* records must cover the same inode range */
  433. if (trec->ir_startino != srec->ir_startino)
  434. return false;
  435. /* both records must be sparse */
  436. if (!xfs_inobt_issparse(trec->ir_holemask) ||
  437. !xfs_inobt_issparse(srec->ir_holemask))
  438. return false;
  439. /* both records must track some inodes */
  440. if (!trec->ir_count || !srec->ir_count)
  441. return false;
  442. /* can't exceed capacity of a full record */
  443. if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
  444. return false;
  445. /* verify there is no allocation overlap */
  446. talloc = xfs_inobt_irec_to_allocmask(trec);
  447. salloc = xfs_inobt_irec_to_allocmask(srec);
  448. if (talloc & salloc)
  449. return false;
  450. return true;
  451. }
  452. /*
  453. * Merge the source inode record into the target. The caller must call
  454. * __xfs_inobt_can_merge() to ensure the merge is valid.
  455. */
  456. STATIC void
  457. __xfs_inobt_rec_merge(
  458. struct xfs_inobt_rec_incore *trec, /* target */
  459. struct xfs_inobt_rec_incore *srec) /* src */
  460. {
  461. ASSERT(trec->ir_startino == srec->ir_startino);
  462. /* combine the counts */
  463. trec->ir_count += srec->ir_count;
  464. trec->ir_freecount += srec->ir_freecount;
  465. /*
  466. * Merge the holemask and free mask. For both fields, 0 bits refer to
  467. * allocated inodes. We combine the allocated ranges with bitwise AND.
  468. */
  469. trec->ir_holemask &= srec->ir_holemask;
  470. trec->ir_free &= srec->ir_free;
  471. }
  472. /*
  473. * Insert a new sparse inode chunk into the associated inode btree. The inode
  474. * record for the sparse chunk is pre-aligned to a startino that should match
  475. * any pre-existing sparse inode record in the tree. This allows sparse chunks
  476. * to fill over time.
  477. *
  478. * This function supports two modes of handling preexisting records depending on
  479. * the merge flag. If merge is true, the provided record is merged with the
  480. * existing record and updated in place. The merged record is returned in nrec.
  481. * If merge is false, an existing record is replaced with the provided record.
  482. * If no preexisting record exists, the provided record is always inserted.
  483. *
  484. * It is considered corruption if a merge is requested and not possible. Given
  485. * the sparse inode alignment constraints, this should never happen.
  486. */
  487. STATIC int
  488. xfs_inobt_insert_sprec(
  489. struct xfs_mount *mp,
  490. struct xfs_trans *tp,
  491. struct xfs_buf *agbp,
  492. int btnum,
  493. struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */
  494. bool merge) /* merge or replace */
  495. {
  496. struct xfs_btree_cur *cur;
  497. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  498. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  499. int error;
  500. int i;
  501. struct xfs_inobt_rec_incore rec;
  502. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
  503. /* the new record is pre-aligned so we know where to look */
  504. error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
  505. if (error)
  506. goto error;
  507. /* if nothing there, insert a new record and return */
  508. if (i == 0) {
  509. error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
  510. nrec->ir_count, nrec->ir_freecount,
  511. nrec->ir_free, &i);
  512. if (error)
  513. goto error;
  514. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
  515. goto out;
  516. }
  517. /*
  518. * A record exists at this startino. Merge or replace the record
  519. * depending on what we've been asked to do.
  520. */
  521. if (merge) {
  522. error = xfs_inobt_get_rec(cur, &rec, &i);
  523. if (error)
  524. goto error;
  525. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
  526. XFS_WANT_CORRUPTED_GOTO(mp,
  527. rec.ir_startino == nrec->ir_startino,
  528. error);
  529. /*
  530. * This should never fail. If we have coexisting records that
  531. * cannot merge, something is seriously wrong.
  532. */
  533. XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec),
  534. error);
  535. trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
  536. rec.ir_holemask, nrec->ir_startino,
  537. nrec->ir_holemask);
  538. /* merge to nrec to output the updated record */
  539. __xfs_inobt_rec_merge(nrec, &rec);
  540. trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
  541. nrec->ir_holemask);
  542. error = xfs_inobt_rec_check_count(mp, nrec);
  543. if (error)
  544. goto error;
  545. }
  546. error = xfs_inobt_update(cur, nrec);
  547. if (error)
  548. goto error;
  549. out:
  550. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  551. return 0;
  552. error:
  553. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  554. return error;
  555. }
  556. /*
  557. * Allocate new inodes in the allocation group specified by agbp.
  558. * Return 0 for success, else error code.
  559. */
  560. STATIC int /* error code or 0 */
  561. xfs_ialloc_ag_alloc(
  562. xfs_trans_t *tp, /* transaction pointer */
  563. xfs_buf_t *agbp, /* alloc group buffer */
  564. int *alloc)
  565. {
  566. xfs_agi_t *agi; /* allocation group header */
  567. xfs_alloc_arg_t args; /* allocation argument structure */
  568. xfs_agnumber_t agno;
  569. int error;
  570. xfs_agino_t newino; /* new first inode's number */
  571. xfs_agino_t newlen; /* new number of inodes */
  572. int isaligned = 0; /* inode allocation at stripe unit */
  573. /* boundary */
  574. uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */
  575. struct xfs_inobt_rec_incore rec;
  576. struct xfs_perag *pag;
  577. int do_sparse = 0;
  578. memset(&args, 0, sizeof(args));
  579. args.tp = tp;
  580. args.mp = tp->t_mountp;
  581. args.fsbno = NULLFSBLOCK;
  582. xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INODES);
  583. #ifdef DEBUG
  584. /* randomly do sparse inode allocations */
  585. if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
  586. args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks)
  587. do_sparse = prandom_u32() & 1;
  588. #endif
  589. /*
  590. * Locking will ensure that we don't have two callers in here
  591. * at one time.
  592. */
  593. newlen = args.mp->m_ialloc_inos;
  594. if (args.mp->m_maxicount &&
  595. percpu_counter_read_positive(&args.mp->m_icount) + newlen >
  596. args.mp->m_maxicount)
  597. return -ENOSPC;
  598. args.minlen = args.maxlen = args.mp->m_ialloc_blks;
  599. /*
  600. * First try to allocate inodes contiguous with the last-allocated
  601. * chunk of inodes. If the filesystem is striped, this will fill
  602. * an entire stripe unit with inodes.
  603. */
  604. agi = XFS_BUF_TO_AGI(agbp);
  605. newino = be32_to_cpu(agi->agi_newino);
  606. agno = be32_to_cpu(agi->agi_seqno);
  607. args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
  608. args.mp->m_ialloc_blks;
  609. if (do_sparse)
  610. goto sparse_alloc;
  611. if (likely(newino != NULLAGINO &&
  612. (args.agbno < be32_to_cpu(agi->agi_length)))) {
  613. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  614. args.type = XFS_ALLOCTYPE_THIS_BNO;
  615. args.prod = 1;
  616. /*
  617. * We need to take into account alignment here to ensure that
  618. * we don't modify the free list if we fail to have an exact
  619. * block. If we don't have an exact match, and every oher
  620. * attempt allocation attempt fails, we'll end up cancelling
  621. * a dirty transaction and shutting down.
  622. *
  623. * For an exact allocation, alignment must be 1,
  624. * however we need to take cluster alignment into account when
  625. * fixing up the freelist. Use the minalignslop field to
  626. * indicate that extra blocks might be required for alignment,
  627. * but not to use them in the actual exact allocation.
  628. */
  629. args.alignment = 1;
  630. args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
  631. /* Allow space for the inode btree to split. */
  632. args.minleft = args.mp->m_in_maxlevels - 1;
  633. if ((error = xfs_alloc_vextent(&args)))
  634. return error;
  635. /*
  636. * This request might have dirtied the transaction if the AG can
  637. * satisfy the request, but the exact block was not available.
  638. * If the allocation did fail, subsequent requests will relax
  639. * the exact agbno requirement and increase the alignment
  640. * instead. It is critical that the total size of the request
  641. * (len + alignment + slop) does not increase from this point
  642. * on, so reset minalignslop to ensure it is not included in
  643. * subsequent requests.
  644. */
  645. args.minalignslop = 0;
  646. }
  647. if (unlikely(args.fsbno == NULLFSBLOCK)) {
  648. /*
  649. * Set the alignment for the allocation.
  650. * If stripe alignment is turned on then align at stripe unit
  651. * boundary.
  652. * If the cluster size is smaller than a filesystem block
  653. * then we're doing I/O for inodes in filesystem block size
  654. * pieces, so don't need alignment anyway.
  655. */
  656. isaligned = 0;
  657. if (args.mp->m_sinoalign) {
  658. ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
  659. args.alignment = args.mp->m_dalign;
  660. isaligned = 1;
  661. } else
  662. args.alignment = xfs_ialloc_cluster_alignment(args.mp);
  663. /*
  664. * Need to figure out where to allocate the inode blocks.
  665. * Ideally they should be spaced out through the a.g.
  666. * For now, just allocate blocks up front.
  667. */
  668. args.agbno = be32_to_cpu(agi->agi_root);
  669. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  670. /*
  671. * Allocate a fixed-size extent of inodes.
  672. */
  673. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  674. args.prod = 1;
  675. /*
  676. * Allow space for the inode btree to split.
  677. */
  678. args.minleft = args.mp->m_in_maxlevels - 1;
  679. if ((error = xfs_alloc_vextent(&args)))
  680. return error;
  681. }
  682. /*
  683. * If stripe alignment is turned on, then try again with cluster
  684. * alignment.
  685. */
  686. if (isaligned && args.fsbno == NULLFSBLOCK) {
  687. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  688. args.agbno = be32_to_cpu(agi->agi_root);
  689. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  690. args.alignment = xfs_ialloc_cluster_alignment(args.mp);
  691. if ((error = xfs_alloc_vextent(&args)))
  692. return error;
  693. }
  694. /*
  695. * Finally, try a sparse allocation if the filesystem supports it and
  696. * the sparse allocation length is smaller than a full chunk.
  697. */
  698. if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
  699. args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks &&
  700. args.fsbno == NULLFSBLOCK) {
  701. sparse_alloc:
  702. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  703. args.agbno = be32_to_cpu(agi->agi_root);
  704. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  705. args.alignment = args.mp->m_sb.sb_spino_align;
  706. args.prod = 1;
  707. args.minlen = args.mp->m_ialloc_min_blks;
  708. args.maxlen = args.minlen;
  709. /*
  710. * The inode record will be aligned to full chunk size. We must
  711. * prevent sparse allocation from AG boundaries that result in
  712. * invalid inode records, such as records that start at agbno 0
  713. * or extend beyond the AG.
  714. *
  715. * Set min agbno to the first aligned, non-zero agbno and max to
  716. * the last aligned agbno that is at least one full chunk from
  717. * the end of the AG.
  718. */
  719. args.min_agbno = args.mp->m_sb.sb_inoalignmt;
  720. args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
  721. args.mp->m_sb.sb_inoalignmt) -
  722. args.mp->m_ialloc_blks;
  723. error = xfs_alloc_vextent(&args);
  724. if (error)
  725. return error;
  726. newlen = args.len << args.mp->m_sb.sb_inopblog;
  727. ASSERT(newlen <= XFS_INODES_PER_CHUNK);
  728. allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
  729. }
  730. if (args.fsbno == NULLFSBLOCK) {
  731. *alloc = 0;
  732. return 0;
  733. }
  734. ASSERT(args.len == args.minlen);
  735. /*
  736. * Stamp and write the inode buffers.
  737. *
  738. * Seed the new inode cluster with a random generation number. This
  739. * prevents short-term reuse of generation numbers if a chunk is
  740. * freed and then immediately reallocated. We use random numbers
  741. * rather than a linear progression to prevent the next generation
  742. * number from being easily guessable.
  743. */
  744. error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
  745. args.agbno, args.len, prandom_u32());
  746. if (error)
  747. return error;
  748. /*
  749. * Convert the results.
  750. */
  751. newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
  752. if (xfs_inobt_issparse(~allocmask)) {
  753. /*
  754. * We've allocated a sparse chunk. Align the startino and mask.
  755. */
  756. xfs_align_sparse_ino(args.mp, &newino, &allocmask);
  757. rec.ir_startino = newino;
  758. rec.ir_holemask = ~allocmask;
  759. rec.ir_count = newlen;
  760. rec.ir_freecount = newlen;
  761. rec.ir_free = XFS_INOBT_ALL_FREE;
  762. /*
  763. * Insert the sparse record into the inobt and allow for a merge
  764. * if necessary. If a merge does occur, rec is updated to the
  765. * merged record.
  766. */
  767. error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
  768. &rec, true);
  769. if (error == -EFSCORRUPTED) {
  770. xfs_alert(args.mp,
  771. "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
  772. XFS_AGINO_TO_INO(args.mp, agno,
  773. rec.ir_startino),
  774. rec.ir_holemask, rec.ir_count);
  775. xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
  776. }
  777. if (error)
  778. return error;
  779. /*
  780. * We can't merge the part we've just allocated as for the inobt
  781. * due to finobt semantics. The original record may or may not
  782. * exist independent of whether physical inodes exist in this
  783. * sparse chunk.
  784. *
  785. * We must update the finobt record based on the inobt record.
  786. * rec contains the fully merged and up to date inobt record
  787. * from the previous call. Set merge false to replace any
  788. * existing record with this one.
  789. */
  790. if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
  791. error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
  792. XFS_BTNUM_FINO, &rec,
  793. false);
  794. if (error)
  795. return error;
  796. }
  797. } else {
  798. /* full chunk - insert new records to both btrees */
  799. error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
  800. XFS_BTNUM_INO);
  801. if (error)
  802. return error;
  803. if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
  804. error = xfs_inobt_insert(args.mp, tp, agbp, newino,
  805. newlen, XFS_BTNUM_FINO);
  806. if (error)
  807. return error;
  808. }
  809. }
  810. /*
  811. * Update AGI counts and newino.
  812. */
  813. be32_add_cpu(&agi->agi_count, newlen);
  814. be32_add_cpu(&agi->agi_freecount, newlen);
  815. pag = xfs_perag_get(args.mp, agno);
  816. pag->pagi_freecount += newlen;
  817. xfs_perag_put(pag);
  818. agi->agi_newino = cpu_to_be32(newino);
  819. /*
  820. * Log allocation group header fields
  821. */
  822. xfs_ialloc_log_agi(tp, agbp,
  823. XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
  824. /*
  825. * Modify/log superblock values for inode count and inode free count.
  826. */
  827. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
  828. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
  829. *alloc = 1;
  830. return 0;
  831. }
  832. STATIC xfs_agnumber_t
  833. xfs_ialloc_next_ag(
  834. xfs_mount_t *mp)
  835. {
  836. xfs_agnumber_t agno;
  837. spin_lock(&mp->m_agirotor_lock);
  838. agno = mp->m_agirotor;
  839. if (++mp->m_agirotor >= mp->m_maxagi)
  840. mp->m_agirotor = 0;
  841. spin_unlock(&mp->m_agirotor_lock);
  842. return agno;
  843. }
  844. /*
  845. * Select an allocation group to look for a free inode in, based on the parent
  846. * inode and the mode. Return the allocation group buffer.
  847. */
  848. STATIC xfs_agnumber_t
  849. xfs_ialloc_ag_select(
  850. xfs_trans_t *tp, /* transaction pointer */
  851. xfs_ino_t parent, /* parent directory inode number */
  852. umode_t mode) /* bits set to indicate file type */
  853. {
  854. xfs_agnumber_t agcount; /* number of ag's in the filesystem */
  855. xfs_agnumber_t agno; /* current ag number */
  856. int flags; /* alloc buffer locking flags */
  857. xfs_extlen_t ineed; /* blocks needed for inode allocation */
  858. xfs_extlen_t longest = 0; /* longest extent available */
  859. xfs_mount_t *mp; /* mount point structure */
  860. int needspace; /* file mode implies space allocated */
  861. xfs_perag_t *pag; /* per allocation group data */
  862. xfs_agnumber_t pagno; /* parent (starting) ag number */
  863. int error;
  864. /*
  865. * Files of these types need at least one block if length > 0
  866. * (and they won't fit in the inode, but that's hard to figure out).
  867. */
  868. needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
  869. mp = tp->t_mountp;
  870. agcount = mp->m_maxagi;
  871. if (S_ISDIR(mode))
  872. pagno = xfs_ialloc_next_ag(mp);
  873. else {
  874. pagno = XFS_INO_TO_AGNO(mp, parent);
  875. if (pagno >= agcount)
  876. pagno = 0;
  877. }
  878. ASSERT(pagno < agcount);
  879. /*
  880. * Loop through allocation groups, looking for one with a little
  881. * free space in it. Note we don't look for free inodes, exactly.
  882. * Instead, we include whether there is a need to allocate inodes
  883. * to mean that blocks must be allocated for them,
  884. * if none are currently free.
  885. */
  886. agno = pagno;
  887. flags = XFS_ALLOC_FLAG_TRYLOCK;
  888. for (;;) {
  889. pag = xfs_perag_get(mp, agno);
  890. if (!pag->pagi_inodeok) {
  891. xfs_ialloc_next_ag(mp);
  892. goto nextag;
  893. }
  894. if (!pag->pagi_init) {
  895. error = xfs_ialloc_pagi_init(mp, tp, agno);
  896. if (error)
  897. goto nextag;
  898. }
  899. if (pag->pagi_freecount) {
  900. xfs_perag_put(pag);
  901. return agno;
  902. }
  903. if (!pag->pagf_init) {
  904. error = xfs_alloc_pagf_init(mp, tp, agno, flags);
  905. if (error)
  906. goto nextag;
  907. }
  908. /*
  909. * Check that there is enough free space for the file plus a
  910. * chunk of inodes if we need to allocate some. If this is the
  911. * first pass across the AGs, take into account the potential
  912. * space needed for alignment of inode chunks when checking the
  913. * longest contiguous free space in the AG - this prevents us
  914. * from getting ENOSPC because we have free space larger than
  915. * m_ialloc_blks but alignment constraints prevent us from using
  916. * it.
  917. *
  918. * If we can't find an AG with space for full alignment slack to
  919. * be taken into account, we must be near ENOSPC in all AGs.
  920. * Hence we don't include alignment for the second pass and so
  921. * if we fail allocation due to alignment issues then it is most
  922. * likely a real ENOSPC condition.
  923. */
  924. ineed = mp->m_ialloc_min_blks;
  925. if (flags && ineed > 1)
  926. ineed += xfs_ialloc_cluster_alignment(mp);
  927. longest = pag->pagf_longest;
  928. if (!longest)
  929. longest = pag->pagf_flcount > 0;
  930. if (pag->pagf_freeblks >= needspace + ineed &&
  931. longest >= ineed) {
  932. xfs_perag_put(pag);
  933. return agno;
  934. }
  935. nextag:
  936. xfs_perag_put(pag);
  937. /*
  938. * No point in iterating over the rest, if we're shutting
  939. * down.
  940. */
  941. if (XFS_FORCED_SHUTDOWN(mp))
  942. return NULLAGNUMBER;
  943. agno++;
  944. if (agno >= agcount)
  945. agno = 0;
  946. if (agno == pagno) {
  947. if (flags == 0)
  948. return NULLAGNUMBER;
  949. flags = 0;
  950. }
  951. }
  952. }
  953. /*
  954. * Try to retrieve the next record to the left/right from the current one.
  955. */
  956. STATIC int
  957. xfs_ialloc_next_rec(
  958. struct xfs_btree_cur *cur,
  959. xfs_inobt_rec_incore_t *rec,
  960. int *done,
  961. int left)
  962. {
  963. int error;
  964. int i;
  965. if (left)
  966. error = xfs_btree_decrement(cur, 0, &i);
  967. else
  968. error = xfs_btree_increment(cur, 0, &i);
  969. if (error)
  970. return error;
  971. *done = !i;
  972. if (i) {
  973. error = xfs_inobt_get_rec(cur, rec, &i);
  974. if (error)
  975. return error;
  976. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  977. }
  978. return 0;
  979. }
  980. STATIC int
  981. xfs_ialloc_get_rec(
  982. struct xfs_btree_cur *cur,
  983. xfs_agino_t agino,
  984. xfs_inobt_rec_incore_t *rec,
  985. int *done)
  986. {
  987. int error;
  988. int i;
  989. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
  990. if (error)
  991. return error;
  992. *done = !i;
  993. if (i) {
  994. error = xfs_inobt_get_rec(cur, rec, &i);
  995. if (error)
  996. return error;
  997. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  998. }
  999. return 0;
  1000. }
  1001. /*
  1002. * Return the offset of the first free inode in the record. If the inode chunk
  1003. * is sparsely allocated, we convert the record holemask to inode granularity
  1004. * and mask off the unallocated regions from the inode free mask.
  1005. */
  1006. STATIC int
  1007. xfs_inobt_first_free_inode(
  1008. struct xfs_inobt_rec_incore *rec)
  1009. {
  1010. xfs_inofree_t realfree;
  1011. /* if there are no holes, return the first available offset */
  1012. if (!xfs_inobt_issparse(rec->ir_holemask))
  1013. return xfs_lowbit64(rec->ir_free);
  1014. realfree = xfs_inobt_irec_to_allocmask(rec);
  1015. realfree &= rec->ir_free;
  1016. return xfs_lowbit64(realfree);
  1017. }
  1018. /*
  1019. * Allocate an inode using the inobt-only algorithm.
  1020. */
  1021. STATIC int
  1022. xfs_dialloc_ag_inobt(
  1023. struct xfs_trans *tp,
  1024. struct xfs_buf *agbp,
  1025. xfs_ino_t parent,
  1026. xfs_ino_t *inop)
  1027. {
  1028. struct xfs_mount *mp = tp->t_mountp;
  1029. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1030. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1031. xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
  1032. xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
  1033. struct xfs_perag *pag;
  1034. struct xfs_btree_cur *cur, *tcur;
  1035. struct xfs_inobt_rec_incore rec, trec;
  1036. xfs_ino_t ino;
  1037. int error;
  1038. int offset;
  1039. int i, j;
  1040. int searchdistance = 10;
  1041. pag = xfs_perag_get(mp, agno);
  1042. ASSERT(pag->pagi_init);
  1043. ASSERT(pag->pagi_inodeok);
  1044. ASSERT(pag->pagi_freecount > 0);
  1045. restart_pagno:
  1046. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1047. /*
  1048. * If pagino is 0 (this is the root inode allocation) use newino.
  1049. * This must work because we've just allocated some.
  1050. */
  1051. if (!pagino)
  1052. pagino = be32_to_cpu(agi->agi_newino);
  1053. error = xfs_check_agi_freecount(cur, agi);
  1054. if (error)
  1055. goto error0;
  1056. /*
  1057. * If in the same AG as the parent, try to get near the parent.
  1058. */
  1059. if (pagno == agno) {
  1060. int doneleft; /* done, to the left */
  1061. int doneright; /* done, to the right */
  1062. error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
  1063. if (error)
  1064. goto error0;
  1065. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1066. error = xfs_inobt_get_rec(cur, &rec, &j);
  1067. if (error)
  1068. goto error0;
  1069. XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
  1070. if (rec.ir_freecount > 0) {
  1071. /*
  1072. * Found a free inode in the same chunk
  1073. * as the parent, done.
  1074. */
  1075. goto alloc_inode;
  1076. }
  1077. /*
  1078. * In the same AG as parent, but parent's chunk is full.
  1079. */
  1080. /* duplicate the cursor, search left & right simultaneously */
  1081. error = xfs_btree_dup_cursor(cur, &tcur);
  1082. if (error)
  1083. goto error0;
  1084. /*
  1085. * Skip to last blocks looked up if same parent inode.
  1086. */
  1087. if (pagino != NULLAGINO &&
  1088. pag->pagl_pagino == pagino &&
  1089. pag->pagl_leftrec != NULLAGINO &&
  1090. pag->pagl_rightrec != NULLAGINO) {
  1091. error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
  1092. &trec, &doneleft);
  1093. if (error)
  1094. goto error1;
  1095. error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
  1096. &rec, &doneright);
  1097. if (error)
  1098. goto error1;
  1099. } else {
  1100. /* search left with tcur, back up 1 record */
  1101. error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
  1102. if (error)
  1103. goto error1;
  1104. /* search right with cur, go forward 1 record. */
  1105. error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
  1106. if (error)
  1107. goto error1;
  1108. }
  1109. /*
  1110. * Loop until we find an inode chunk with a free inode.
  1111. */
  1112. while (--searchdistance > 0 && (!doneleft || !doneright)) {
  1113. int useleft; /* using left inode chunk this time */
  1114. /* figure out the closer block if both are valid. */
  1115. if (!doneleft && !doneright) {
  1116. useleft = pagino -
  1117. (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
  1118. rec.ir_startino - pagino;
  1119. } else {
  1120. useleft = !doneleft;
  1121. }
  1122. /* free inodes to the left? */
  1123. if (useleft && trec.ir_freecount) {
  1124. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1125. cur = tcur;
  1126. pag->pagl_leftrec = trec.ir_startino;
  1127. pag->pagl_rightrec = rec.ir_startino;
  1128. pag->pagl_pagino = pagino;
  1129. rec = trec;
  1130. goto alloc_inode;
  1131. }
  1132. /* free inodes to the right? */
  1133. if (!useleft && rec.ir_freecount) {
  1134. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1135. pag->pagl_leftrec = trec.ir_startino;
  1136. pag->pagl_rightrec = rec.ir_startino;
  1137. pag->pagl_pagino = pagino;
  1138. goto alloc_inode;
  1139. }
  1140. /* get next record to check */
  1141. if (useleft) {
  1142. error = xfs_ialloc_next_rec(tcur, &trec,
  1143. &doneleft, 1);
  1144. } else {
  1145. error = xfs_ialloc_next_rec(cur, &rec,
  1146. &doneright, 0);
  1147. }
  1148. if (error)
  1149. goto error1;
  1150. }
  1151. if (searchdistance <= 0) {
  1152. /*
  1153. * Not in range - save last search
  1154. * location and allocate a new inode
  1155. */
  1156. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1157. pag->pagl_leftrec = trec.ir_startino;
  1158. pag->pagl_rightrec = rec.ir_startino;
  1159. pag->pagl_pagino = pagino;
  1160. } else {
  1161. /*
  1162. * We've reached the end of the btree. because
  1163. * we are only searching a small chunk of the
  1164. * btree each search, there is obviously free
  1165. * inodes closer to the parent inode than we
  1166. * are now. restart the search again.
  1167. */
  1168. pag->pagl_pagino = NULLAGINO;
  1169. pag->pagl_leftrec = NULLAGINO;
  1170. pag->pagl_rightrec = NULLAGINO;
  1171. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1172. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1173. goto restart_pagno;
  1174. }
  1175. }
  1176. /*
  1177. * In a different AG from the parent.
  1178. * See if the most recently allocated block has any free.
  1179. */
  1180. if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
  1181. error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
  1182. XFS_LOOKUP_EQ, &i);
  1183. if (error)
  1184. goto error0;
  1185. if (i == 1) {
  1186. error = xfs_inobt_get_rec(cur, &rec, &j);
  1187. if (error)
  1188. goto error0;
  1189. if (j == 1 && rec.ir_freecount > 0) {
  1190. /*
  1191. * The last chunk allocated in the group
  1192. * still has a free inode.
  1193. */
  1194. goto alloc_inode;
  1195. }
  1196. }
  1197. }
  1198. /*
  1199. * None left in the last group, search the whole AG
  1200. */
  1201. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  1202. if (error)
  1203. goto error0;
  1204. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1205. for (;;) {
  1206. error = xfs_inobt_get_rec(cur, &rec, &i);
  1207. if (error)
  1208. goto error0;
  1209. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1210. if (rec.ir_freecount > 0)
  1211. break;
  1212. error = xfs_btree_increment(cur, 0, &i);
  1213. if (error)
  1214. goto error0;
  1215. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1216. }
  1217. alloc_inode:
  1218. offset = xfs_inobt_first_free_inode(&rec);
  1219. ASSERT(offset >= 0);
  1220. ASSERT(offset < XFS_INODES_PER_CHUNK);
  1221. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  1222. XFS_INODES_PER_CHUNK) == 0);
  1223. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  1224. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1225. rec.ir_freecount--;
  1226. error = xfs_inobt_update(cur, &rec);
  1227. if (error)
  1228. goto error0;
  1229. be32_add_cpu(&agi->agi_freecount, -1);
  1230. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1231. pag->pagi_freecount--;
  1232. error = xfs_check_agi_freecount(cur, agi);
  1233. if (error)
  1234. goto error0;
  1235. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1236. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  1237. xfs_perag_put(pag);
  1238. *inop = ino;
  1239. return 0;
  1240. error1:
  1241. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  1242. error0:
  1243. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1244. xfs_perag_put(pag);
  1245. return error;
  1246. }
  1247. /*
  1248. * Use the free inode btree to allocate an inode based on distance from the
  1249. * parent. Note that the provided cursor may be deleted and replaced.
  1250. */
  1251. STATIC int
  1252. xfs_dialloc_ag_finobt_near(
  1253. xfs_agino_t pagino,
  1254. struct xfs_btree_cur **ocur,
  1255. struct xfs_inobt_rec_incore *rec)
  1256. {
  1257. struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
  1258. struct xfs_btree_cur *rcur; /* right search cursor */
  1259. struct xfs_inobt_rec_incore rrec;
  1260. int error;
  1261. int i, j;
  1262. error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
  1263. if (error)
  1264. return error;
  1265. if (i == 1) {
  1266. error = xfs_inobt_get_rec(lcur, rec, &i);
  1267. if (error)
  1268. return error;
  1269. XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
  1270. /*
  1271. * See if we've landed in the parent inode record. The finobt
  1272. * only tracks chunks with at least one free inode, so record
  1273. * existence is enough.
  1274. */
  1275. if (pagino >= rec->ir_startino &&
  1276. pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
  1277. return 0;
  1278. }
  1279. error = xfs_btree_dup_cursor(lcur, &rcur);
  1280. if (error)
  1281. return error;
  1282. error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
  1283. if (error)
  1284. goto error_rcur;
  1285. if (j == 1) {
  1286. error = xfs_inobt_get_rec(rcur, &rrec, &j);
  1287. if (error)
  1288. goto error_rcur;
  1289. XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
  1290. }
  1291. XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
  1292. if (i == 1 && j == 1) {
  1293. /*
  1294. * Both the left and right records are valid. Choose the closer
  1295. * inode chunk to the target.
  1296. */
  1297. if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
  1298. (rrec.ir_startino - pagino)) {
  1299. *rec = rrec;
  1300. xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
  1301. *ocur = rcur;
  1302. } else {
  1303. xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
  1304. }
  1305. } else if (j == 1) {
  1306. /* only the right record is valid */
  1307. *rec = rrec;
  1308. xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
  1309. *ocur = rcur;
  1310. } else if (i == 1) {
  1311. /* only the left record is valid */
  1312. xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
  1313. }
  1314. return 0;
  1315. error_rcur:
  1316. xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
  1317. return error;
  1318. }
  1319. /*
  1320. * Use the free inode btree to find a free inode based on a newino hint. If
  1321. * the hint is NULL, find the first free inode in the AG.
  1322. */
  1323. STATIC int
  1324. xfs_dialloc_ag_finobt_newino(
  1325. struct xfs_agi *agi,
  1326. struct xfs_btree_cur *cur,
  1327. struct xfs_inobt_rec_incore *rec)
  1328. {
  1329. int error;
  1330. int i;
  1331. if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
  1332. error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
  1333. XFS_LOOKUP_EQ, &i);
  1334. if (error)
  1335. return error;
  1336. if (i == 1) {
  1337. error = xfs_inobt_get_rec(cur, rec, &i);
  1338. if (error)
  1339. return error;
  1340. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1341. return 0;
  1342. }
  1343. }
  1344. /*
  1345. * Find the first inode available in the AG.
  1346. */
  1347. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  1348. if (error)
  1349. return error;
  1350. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1351. error = xfs_inobt_get_rec(cur, rec, &i);
  1352. if (error)
  1353. return error;
  1354. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1355. return 0;
  1356. }
  1357. /*
  1358. * Update the inobt based on a modification made to the finobt. Also ensure that
  1359. * the records from both trees are equivalent post-modification.
  1360. */
  1361. STATIC int
  1362. xfs_dialloc_ag_update_inobt(
  1363. struct xfs_btree_cur *cur, /* inobt cursor */
  1364. struct xfs_inobt_rec_incore *frec, /* finobt record */
  1365. int offset) /* inode offset */
  1366. {
  1367. struct xfs_inobt_rec_incore rec;
  1368. int error;
  1369. int i;
  1370. error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
  1371. if (error)
  1372. return error;
  1373. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1374. error = xfs_inobt_get_rec(cur, &rec, &i);
  1375. if (error)
  1376. return error;
  1377. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1378. ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
  1379. XFS_INODES_PER_CHUNK) == 0);
  1380. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1381. rec.ir_freecount--;
  1382. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
  1383. (rec.ir_freecount == frec->ir_freecount));
  1384. return xfs_inobt_update(cur, &rec);
  1385. }
  1386. /*
  1387. * Allocate an inode using the free inode btree, if available. Otherwise, fall
  1388. * back to the inobt search algorithm.
  1389. *
  1390. * The caller selected an AG for us, and made sure that free inodes are
  1391. * available.
  1392. */
  1393. STATIC int
  1394. xfs_dialloc_ag(
  1395. struct xfs_trans *tp,
  1396. struct xfs_buf *agbp,
  1397. xfs_ino_t parent,
  1398. xfs_ino_t *inop)
  1399. {
  1400. struct xfs_mount *mp = tp->t_mountp;
  1401. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1402. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1403. xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
  1404. xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
  1405. struct xfs_perag *pag;
  1406. struct xfs_btree_cur *cur; /* finobt cursor */
  1407. struct xfs_btree_cur *icur; /* inobt cursor */
  1408. struct xfs_inobt_rec_incore rec;
  1409. xfs_ino_t ino;
  1410. int error;
  1411. int offset;
  1412. int i;
  1413. if (!xfs_sb_version_hasfinobt(&mp->m_sb))
  1414. return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
  1415. pag = xfs_perag_get(mp, agno);
  1416. /*
  1417. * If pagino is 0 (this is the root inode allocation) use newino.
  1418. * This must work because we've just allocated some.
  1419. */
  1420. if (!pagino)
  1421. pagino = be32_to_cpu(agi->agi_newino);
  1422. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
  1423. error = xfs_check_agi_freecount(cur, agi);
  1424. if (error)
  1425. goto error_cur;
  1426. /*
  1427. * The search algorithm depends on whether we're in the same AG as the
  1428. * parent. If so, find the closest available inode to the parent. If
  1429. * not, consider the agi hint or find the first free inode in the AG.
  1430. */
  1431. if (agno == pagno)
  1432. error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
  1433. else
  1434. error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
  1435. if (error)
  1436. goto error_cur;
  1437. offset = xfs_inobt_first_free_inode(&rec);
  1438. ASSERT(offset >= 0);
  1439. ASSERT(offset < XFS_INODES_PER_CHUNK);
  1440. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  1441. XFS_INODES_PER_CHUNK) == 0);
  1442. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  1443. /*
  1444. * Modify or remove the finobt record.
  1445. */
  1446. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1447. rec.ir_freecount--;
  1448. if (rec.ir_freecount)
  1449. error = xfs_inobt_update(cur, &rec);
  1450. else
  1451. error = xfs_btree_delete(cur, &i);
  1452. if (error)
  1453. goto error_cur;
  1454. /*
  1455. * The finobt has now been updated appropriately. We haven't updated the
  1456. * agi and superblock yet, so we can create an inobt cursor and validate
  1457. * the original freecount. If all is well, make the equivalent update to
  1458. * the inobt using the finobt record and offset information.
  1459. */
  1460. icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1461. error = xfs_check_agi_freecount(icur, agi);
  1462. if (error)
  1463. goto error_icur;
  1464. error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
  1465. if (error)
  1466. goto error_icur;
  1467. /*
  1468. * Both trees have now been updated. We must update the perag and
  1469. * superblock before we can check the freecount for each btree.
  1470. */
  1471. be32_add_cpu(&agi->agi_freecount, -1);
  1472. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1473. pag->pagi_freecount--;
  1474. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  1475. error = xfs_check_agi_freecount(icur, agi);
  1476. if (error)
  1477. goto error_icur;
  1478. error = xfs_check_agi_freecount(cur, agi);
  1479. if (error)
  1480. goto error_icur;
  1481. xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
  1482. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1483. xfs_perag_put(pag);
  1484. *inop = ino;
  1485. return 0;
  1486. error_icur:
  1487. xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
  1488. error_cur:
  1489. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1490. xfs_perag_put(pag);
  1491. return error;
  1492. }
  1493. /*
  1494. * Allocate an inode on disk.
  1495. *
  1496. * Mode is used to tell whether the new inode will need space, and whether it
  1497. * is a directory.
  1498. *
  1499. * This function is designed to be called twice if it has to do an allocation
  1500. * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
  1501. * If an inode is available without having to performn an allocation, an inode
  1502. * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
  1503. * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
  1504. * The caller should then commit the current transaction, allocate a
  1505. * new transaction, and call xfs_dialloc() again, passing in the previous value
  1506. * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
  1507. * buffer is locked across the two calls, the second call is guaranteed to have
  1508. * a free inode available.
  1509. *
  1510. * Once we successfully pick an inode its number is returned and the on-disk
  1511. * data structures are updated. The inode itself is not read in, since doing so
  1512. * would break ordering constraints with xfs_reclaim.
  1513. */
  1514. int
  1515. xfs_dialloc(
  1516. struct xfs_trans *tp,
  1517. xfs_ino_t parent,
  1518. umode_t mode,
  1519. struct xfs_buf **IO_agbp,
  1520. xfs_ino_t *inop)
  1521. {
  1522. struct xfs_mount *mp = tp->t_mountp;
  1523. struct xfs_buf *agbp;
  1524. xfs_agnumber_t agno;
  1525. int error;
  1526. int ialloced;
  1527. int noroom = 0;
  1528. xfs_agnumber_t start_agno;
  1529. struct xfs_perag *pag;
  1530. int okalloc = 1;
  1531. if (*IO_agbp) {
  1532. /*
  1533. * If the caller passes in a pointer to the AGI buffer,
  1534. * continue where we left off before. In this case, we
  1535. * know that the allocation group has free inodes.
  1536. */
  1537. agbp = *IO_agbp;
  1538. goto out_alloc;
  1539. }
  1540. /*
  1541. * We do not have an agbp, so select an initial allocation
  1542. * group for inode allocation.
  1543. */
  1544. start_agno = xfs_ialloc_ag_select(tp, parent, mode);
  1545. if (start_agno == NULLAGNUMBER) {
  1546. *inop = NULLFSINO;
  1547. return 0;
  1548. }
  1549. /*
  1550. * If we have already hit the ceiling of inode blocks then clear
  1551. * okalloc so we scan all available agi structures for a free
  1552. * inode.
  1553. *
  1554. * Read rough value of mp->m_icount by percpu_counter_read_positive,
  1555. * which will sacrifice the preciseness but improve the performance.
  1556. */
  1557. if (mp->m_maxicount &&
  1558. percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos
  1559. > mp->m_maxicount) {
  1560. noroom = 1;
  1561. okalloc = 0;
  1562. }
  1563. /*
  1564. * Loop until we find an allocation group that either has free inodes
  1565. * or in which we can allocate some inodes. Iterate through the
  1566. * allocation groups upward, wrapping at the end.
  1567. */
  1568. agno = start_agno;
  1569. for (;;) {
  1570. pag = xfs_perag_get(mp, agno);
  1571. if (!pag->pagi_inodeok) {
  1572. xfs_ialloc_next_ag(mp);
  1573. goto nextag;
  1574. }
  1575. if (!pag->pagi_init) {
  1576. error = xfs_ialloc_pagi_init(mp, tp, agno);
  1577. if (error)
  1578. goto out_error;
  1579. }
  1580. /*
  1581. * Do a first racy fast path check if this AG is usable.
  1582. */
  1583. if (!pag->pagi_freecount && !okalloc)
  1584. goto nextag;
  1585. /*
  1586. * Then read in the AGI buffer and recheck with the AGI buffer
  1587. * lock held.
  1588. */
  1589. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1590. if (error)
  1591. goto out_error;
  1592. if (pag->pagi_freecount) {
  1593. xfs_perag_put(pag);
  1594. goto out_alloc;
  1595. }
  1596. if (!okalloc)
  1597. goto nextag_relse_buffer;
  1598. error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
  1599. if (error) {
  1600. xfs_trans_brelse(tp, agbp);
  1601. if (error != -ENOSPC)
  1602. goto out_error;
  1603. xfs_perag_put(pag);
  1604. *inop = NULLFSINO;
  1605. return 0;
  1606. }
  1607. if (ialloced) {
  1608. /*
  1609. * We successfully allocated some inodes, return
  1610. * the current context to the caller so that it
  1611. * can commit the current transaction and call
  1612. * us again where we left off.
  1613. */
  1614. ASSERT(pag->pagi_freecount > 0);
  1615. xfs_perag_put(pag);
  1616. *IO_agbp = agbp;
  1617. *inop = NULLFSINO;
  1618. return 0;
  1619. }
  1620. nextag_relse_buffer:
  1621. xfs_trans_brelse(tp, agbp);
  1622. nextag:
  1623. xfs_perag_put(pag);
  1624. if (++agno == mp->m_sb.sb_agcount)
  1625. agno = 0;
  1626. if (agno == start_agno) {
  1627. *inop = NULLFSINO;
  1628. return noroom ? -ENOSPC : 0;
  1629. }
  1630. }
  1631. out_alloc:
  1632. *IO_agbp = NULL;
  1633. return xfs_dialloc_ag(tp, agbp, parent, inop);
  1634. out_error:
  1635. xfs_perag_put(pag);
  1636. return error;
  1637. }
  1638. /*
  1639. * Free the blocks of an inode chunk. We must consider that the inode chunk
  1640. * might be sparse and only free the regions that are allocated as part of the
  1641. * chunk.
  1642. */
  1643. STATIC void
  1644. xfs_difree_inode_chunk(
  1645. struct xfs_mount *mp,
  1646. xfs_agnumber_t agno,
  1647. struct xfs_inobt_rec_incore *rec,
  1648. struct xfs_defer_ops *dfops)
  1649. {
  1650. xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino);
  1651. int startidx, endidx;
  1652. int nextbit;
  1653. xfs_agblock_t agbno;
  1654. int contigblk;
  1655. struct xfs_owner_info oinfo;
  1656. DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
  1657. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
  1658. if (!xfs_inobt_issparse(rec->ir_holemask)) {
  1659. /* not sparse, calculate extent info directly */
  1660. xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, sagbno),
  1661. mp->m_ialloc_blks, &oinfo);
  1662. return;
  1663. }
  1664. /* holemask is only 16-bits (fits in an unsigned long) */
  1665. ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
  1666. holemask[0] = rec->ir_holemask;
  1667. /*
  1668. * Find contiguous ranges of zeroes (i.e., allocated regions) in the
  1669. * holemask and convert the start/end index of each range to an extent.
  1670. * We start with the start and end index both pointing at the first 0 in
  1671. * the mask.
  1672. */
  1673. startidx = endidx = find_first_zero_bit(holemask,
  1674. XFS_INOBT_HOLEMASK_BITS);
  1675. nextbit = startidx + 1;
  1676. while (startidx < XFS_INOBT_HOLEMASK_BITS) {
  1677. nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
  1678. nextbit);
  1679. /*
  1680. * If the next zero bit is contiguous, update the end index of
  1681. * the current range and continue.
  1682. */
  1683. if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
  1684. nextbit == endidx + 1) {
  1685. endidx = nextbit;
  1686. goto next;
  1687. }
  1688. /*
  1689. * nextbit is not contiguous with the current end index. Convert
  1690. * the current start/end to an extent and add it to the free
  1691. * list.
  1692. */
  1693. agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
  1694. mp->m_sb.sb_inopblock;
  1695. contigblk = ((endidx - startidx + 1) *
  1696. XFS_INODES_PER_HOLEMASK_BIT) /
  1697. mp->m_sb.sb_inopblock;
  1698. ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
  1699. ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
  1700. xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, agbno),
  1701. contigblk, &oinfo);
  1702. /* reset range to current bit and carry on... */
  1703. startidx = endidx = nextbit;
  1704. next:
  1705. nextbit++;
  1706. }
  1707. }
  1708. STATIC int
  1709. xfs_difree_inobt(
  1710. struct xfs_mount *mp,
  1711. struct xfs_trans *tp,
  1712. struct xfs_buf *agbp,
  1713. xfs_agino_t agino,
  1714. struct xfs_defer_ops *dfops,
  1715. struct xfs_icluster *xic,
  1716. struct xfs_inobt_rec_incore *orec)
  1717. {
  1718. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1719. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1720. struct xfs_perag *pag;
  1721. struct xfs_btree_cur *cur;
  1722. struct xfs_inobt_rec_incore rec;
  1723. int ilen;
  1724. int error;
  1725. int i;
  1726. int off;
  1727. ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
  1728. ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
  1729. /*
  1730. * Initialize the cursor.
  1731. */
  1732. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1733. error = xfs_check_agi_freecount(cur, agi);
  1734. if (error)
  1735. goto error0;
  1736. /*
  1737. * Look for the entry describing this inode.
  1738. */
  1739. if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
  1740. xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
  1741. __func__, error);
  1742. goto error0;
  1743. }
  1744. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1745. error = xfs_inobt_get_rec(cur, &rec, &i);
  1746. if (error) {
  1747. xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
  1748. __func__, error);
  1749. goto error0;
  1750. }
  1751. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1752. /*
  1753. * Get the offset in the inode chunk.
  1754. */
  1755. off = agino - rec.ir_startino;
  1756. ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
  1757. ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
  1758. /*
  1759. * Mark the inode free & increment the count.
  1760. */
  1761. rec.ir_free |= XFS_INOBT_MASK(off);
  1762. rec.ir_freecount++;
  1763. /*
  1764. * When an inode chunk is free, it becomes eligible for removal. Don't
  1765. * remove the chunk if the block size is large enough for multiple inode
  1766. * chunks (that might not be free).
  1767. */
  1768. if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
  1769. rec.ir_free == XFS_INOBT_ALL_FREE &&
  1770. mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
  1771. xic->deleted = true;
  1772. xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
  1773. xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
  1774. /*
  1775. * Remove the inode cluster from the AGI B+Tree, adjust the
  1776. * AGI and Superblock inode counts, and mark the disk space
  1777. * to be freed when the transaction is committed.
  1778. */
  1779. ilen = rec.ir_freecount;
  1780. be32_add_cpu(&agi->agi_count, -ilen);
  1781. be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
  1782. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
  1783. pag = xfs_perag_get(mp, agno);
  1784. pag->pagi_freecount -= ilen - 1;
  1785. xfs_perag_put(pag);
  1786. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
  1787. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
  1788. if ((error = xfs_btree_delete(cur, &i))) {
  1789. xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
  1790. __func__, error);
  1791. goto error0;
  1792. }
  1793. xfs_difree_inode_chunk(mp, agno, &rec, dfops);
  1794. } else {
  1795. xic->deleted = false;
  1796. error = xfs_inobt_update(cur, &rec);
  1797. if (error) {
  1798. xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
  1799. __func__, error);
  1800. goto error0;
  1801. }
  1802. /*
  1803. * Change the inode free counts and log the ag/sb changes.
  1804. */
  1805. be32_add_cpu(&agi->agi_freecount, 1);
  1806. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1807. pag = xfs_perag_get(mp, agno);
  1808. pag->pagi_freecount++;
  1809. xfs_perag_put(pag);
  1810. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
  1811. }
  1812. error = xfs_check_agi_freecount(cur, agi);
  1813. if (error)
  1814. goto error0;
  1815. *orec = rec;
  1816. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1817. return 0;
  1818. error0:
  1819. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1820. return error;
  1821. }
  1822. /*
  1823. * Free an inode in the free inode btree.
  1824. */
  1825. STATIC int
  1826. xfs_difree_finobt(
  1827. struct xfs_mount *mp,
  1828. struct xfs_trans *tp,
  1829. struct xfs_buf *agbp,
  1830. xfs_agino_t agino,
  1831. struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
  1832. {
  1833. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1834. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1835. struct xfs_btree_cur *cur;
  1836. struct xfs_inobt_rec_incore rec;
  1837. int offset = agino - ibtrec->ir_startino;
  1838. int error;
  1839. int i;
  1840. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
  1841. error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
  1842. if (error)
  1843. goto error;
  1844. if (i == 0) {
  1845. /*
  1846. * If the record does not exist in the finobt, we must have just
  1847. * freed an inode in a previously fully allocated chunk. If not,
  1848. * something is out of sync.
  1849. */
  1850. XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
  1851. error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
  1852. ibtrec->ir_count,
  1853. ibtrec->ir_freecount,
  1854. ibtrec->ir_free, &i);
  1855. if (error)
  1856. goto error;
  1857. ASSERT(i == 1);
  1858. goto out;
  1859. }
  1860. /*
  1861. * Read and update the existing record. We could just copy the ibtrec
  1862. * across here, but that would defeat the purpose of having redundant
  1863. * metadata. By making the modifications independently, we can catch
  1864. * corruptions that we wouldn't see if we just copied from one record
  1865. * to another.
  1866. */
  1867. error = xfs_inobt_get_rec(cur, &rec, &i);
  1868. if (error)
  1869. goto error;
  1870. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
  1871. rec.ir_free |= XFS_INOBT_MASK(offset);
  1872. rec.ir_freecount++;
  1873. XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
  1874. (rec.ir_freecount == ibtrec->ir_freecount),
  1875. error);
  1876. /*
  1877. * The content of inobt records should always match between the inobt
  1878. * and finobt. The lifecycle of records in the finobt is different from
  1879. * the inobt in that the finobt only tracks records with at least one
  1880. * free inode. Hence, if all of the inodes are free and we aren't
  1881. * keeping inode chunks permanently on disk, remove the record.
  1882. * Otherwise, update the record with the new information.
  1883. *
  1884. * Note that we currently can't free chunks when the block size is large
  1885. * enough for multiple chunks. Leave the finobt record to remain in sync
  1886. * with the inobt.
  1887. */
  1888. if (rec.ir_free == XFS_INOBT_ALL_FREE &&
  1889. mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
  1890. !(mp->m_flags & XFS_MOUNT_IKEEP)) {
  1891. error = xfs_btree_delete(cur, &i);
  1892. if (error)
  1893. goto error;
  1894. ASSERT(i == 1);
  1895. } else {
  1896. error = xfs_inobt_update(cur, &rec);
  1897. if (error)
  1898. goto error;
  1899. }
  1900. out:
  1901. error = xfs_check_agi_freecount(cur, agi);
  1902. if (error)
  1903. goto error;
  1904. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1905. return 0;
  1906. error:
  1907. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1908. return error;
  1909. }
  1910. /*
  1911. * Free disk inode. Carefully avoids touching the incore inode, all
  1912. * manipulations incore are the caller's responsibility.
  1913. * The on-disk inode is not changed by this operation, only the
  1914. * btree (free inode mask) is changed.
  1915. */
  1916. int
  1917. xfs_difree(
  1918. struct xfs_trans *tp, /* transaction pointer */
  1919. xfs_ino_t inode, /* inode to be freed */
  1920. struct xfs_defer_ops *dfops, /* extents to free */
  1921. struct xfs_icluster *xic) /* cluster info if deleted */
  1922. {
  1923. /* REFERENCED */
  1924. xfs_agblock_t agbno; /* block number containing inode */
  1925. struct xfs_buf *agbp; /* buffer for allocation group header */
  1926. xfs_agino_t agino; /* allocation group inode number */
  1927. xfs_agnumber_t agno; /* allocation group number */
  1928. int error; /* error return value */
  1929. struct xfs_mount *mp; /* mount structure for filesystem */
  1930. struct xfs_inobt_rec_incore rec;/* btree record */
  1931. mp = tp->t_mountp;
  1932. /*
  1933. * Break up inode number into its components.
  1934. */
  1935. agno = XFS_INO_TO_AGNO(mp, inode);
  1936. if (agno >= mp->m_sb.sb_agcount) {
  1937. xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
  1938. __func__, agno, mp->m_sb.sb_agcount);
  1939. ASSERT(0);
  1940. return -EINVAL;
  1941. }
  1942. agino = XFS_INO_TO_AGINO(mp, inode);
  1943. if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1944. xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
  1945. __func__, (unsigned long long)inode,
  1946. (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
  1947. ASSERT(0);
  1948. return -EINVAL;
  1949. }
  1950. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1951. if (agbno >= mp->m_sb.sb_agblocks) {
  1952. xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
  1953. __func__, agbno, mp->m_sb.sb_agblocks);
  1954. ASSERT(0);
  1955. return -EINVAL;
  1956. }
  1957. /*
  1958. * Get the allocation group header.
  1959. */
  1960. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1961. if (error) {
  1962. xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
  1963. __func__, error);
  1964. return error;
  1965. }
  1966. /*
  1967. * Fix up the inode allocation btree.
  1968. */
  1969. error = xfs_difree_inobt(mp, tp, agbp, agino, dfops, xic, &rec);
  1970. if (error)
  1971. goto error0;
  1972. /*
  1973. * Fix up the free inode btree.
  1974. */
  1975. if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
  1976. error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
  1977. if (error)
  1978. goto error0;
  1979. }
  1980. return 0;
  1981. error0:
  1982. return error;
  1983. }
  1984. STATIC int
  1985. xfs_imap_lookup(
  1986. struct xfs_mount *mp,
  1987. struct xfs_trans *tp,
  1988. xfs_agnumber_t agno,
  1989. xfs_agino_t agino,
  1990. xfs_agblock_t agbno,
  1991. xfs_agblock_t *chunk_agbno,
  1992. xfs_agblock_t *offset_agbno,
  1993. int flags)
  1994. {
  1995. struct xfs_inobt_rec_incore rec;
  1996. struct xfs_btree_cur *cur;
  1997. struct xfs_buf *agbp;
  1998. int error;
  1999. int i;
  2000. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  2001. if (error) {
  2002. xfs_alert(mp,
  2003. "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
  2004. __func__, error, agno);
  2005. return error;
  2006. }
  2007. /*
  2008. * Lookup the inode record for the given agino. If the record cannot be
  2009. * found, then it's an invalid inode number and we should abort. Once
  2010. * we have a record, we need to ensure it contains the inode number
  2011. * we are looking up.
  2012. */
  2013. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  2014. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
  2015. if (!error) {
  2016. if (i)
  2017. error = xfs_inobt_get_rec(cur, &rec, &i);
  2018. if (!error && i == 0)
  2019. error = -EINVAL;
  2020. }
  2021. xfs_trans_brelse(tp, agbp);
  2022. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  2023. if (error)
  2024. return error;
  2025. /* check that the returned record contains the required inode */
  2026. if (rec.ir_startino > agino ||
  2027. rec.ir_startino + mp->m_ialloc_inos <= agino)
  2028. return -EINVAL;
  2029. /* for untrusted inodes check it is allocated first */
  2030. if ((flags & XFS_IGET_UNTRUSTED) &&
  2031. (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
  2032. return -EINVAL;
  2033. *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
  2034. *offset_agbno = agbno - *chunk_agbno;
  2035. return 0;
  2036. }
  2037. /*
  2038. * Return the location of the inode in imap, for mapping it into a buffer.
  2039. */
  2040. int
  2041. xfs_imap(
  2042. xfs_mount_t *mp, /* file system mount structure */
  2043. xfs_trans_t *tp, /* transaction pointer */
  2044. xfs_ino_t ino, /* inode to locate */
  2045. struct xfs_imap *imap, /* location map structure */
  2046. uint flags) /* flags for inode btree lookup */
  2047. {
  2048. xfs_agblock_t agbno; /* block number of inode in the alloc group */
  2049. xfs_agino_t agino; /* inode number within alloc group */
  2050. xfs_agnumber_t agno; /* allocation group number */
  2051. int blks_per_cluster; /* num blocks per inode cluster */
  2052. xfs_agblock_t chunk_agbno; /* first block in inode chunk */
  2053. xfs_agblock_t cluster_agbno; /* first block in inode cluster */
  2054. int error; /* error code */
  2055. int offset; /* index of inode in its buffer */
  2056. xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
  2057. ASSERT(ino != NULLFSINO);
  2058. /*
  2059. * Split up the inode number into its parts.
  2060. */
  2061. agno = XFS_INO_TO_AGNO(mp, ino);
  2062. agino = XFS_INO_TO_AGINO(mp, ino);
  2063. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  2064. if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
  2065. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  2066. #ifdef DEBUG
  2067. /*
  2068. * Don't output diagnostic information for untrusted inodes
  2069. * as they can be invalid without implying corruption.
  2070. */
  2071. if (flags & XFS_IGET_UNTRUSTED)
  2072. return -EINVAL;
  2073. if (agno >= mp->m_sb.sb_agcount) {
  2074. xfs_alert(mp,
  2075. "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
  2076. __func__, agno, mp->m_sb.sb_agcount);
  2077. }
  2078. if (agbno >= mp->m_sb.sb_agblocks) {
  2079. xfs_alert(mp,
  2080. "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
  2081. __func__, (unsigned long long)agbno,
  2082. (unsigned long)mp->m_sb.sb_agblocks);
  2083. }
  2084. if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  2085. xfs_alert(mp,
  2086. "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
  2087. __func__, ino,
  2088. XFS_AGINO_TO_INO(mp, agno, agino));
  2089. }
  2090. xfs_stack_trace();
  2091. #endif /* DEBUG */
  2092. return -EINVAL;
  2093. }
  2094. blks_per_cluster = xfs_icluster_size_fsb(mp);
  2095. /*
  2096. * For bulkstat and handle lookups, we have an untrusted inode number
  2097. * that we have to verify is valid. We cannot do this just by reading
  2098. * the inode buffer as it may have been unlinked and removed leaving
  2099. * inodes in stale state on disk. Hence we have to do a btree lookup
  2100. * in all cases where an untrusted inode number is passed.
  2101. */
  2102. if (flags & XFS_IGET_UNTRUSTED) {
  2103. error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
  2104. &chunk_agbno, &offset_agbno, flags);
  2105. if (error)
  2106. return error;
  2107. goto out_map;
  2108. }
  2109. /*
  2110. * If the inode cluster size is the same as the blocksize or
  2111. * smaller we get to the buffer by simple arithmetics.
  2112. */
  2113. if (blks_per_cluster == 1) {
  2114. offset = XFS_INO_TO_OFFSET(mp, ino);
  2115. ASSERT(offset < mp->m_sb.sb_inopblock);
  2116. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
  2117. imap->im_len = XFS_FSB_TO_BB(mp, 1);
  2118. imap->im_boffset = (unsigned short)(offset <<
  2119. mp->m_sb.sb_inodelog);
  2120. return 0;
  2121. }
  2122. /*
  2123. * If the inode chunks are aligned then use simple maths to
  2124. * find the location. Otherwise we have to do a btree
  2125. * lookup to find the location.
  2126. */
  2127. if (mp->m_inoalign_mask) {
  2128. offset_agbno = agbno & mp->m_inoalign_mask;
  2129. chunk_agbno = agbno - offset_agbno;
  2130. } else {
  2131. error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
  2132. &chunk_agbno, &offset_agbno, flags);
  2133. if (error)
  2134. return error;
  2135. }
  2136. out_map:
  2137. ASSERT(agbno >= chunk_agbno);
  2138. cluster_agbno = chunk_agbno +
  2139. ((offset_agbno / blks_per_cluster) * blks_per_cluster);
  2140. offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
  2141. XFS_INO_TO_OFFSET(mp, ino);
  2142. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
  2143. imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
  2144. imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
  2145. /*
  2146. * If the inode number maps to a block outside the bounds
  2147. * of the file system then return NULL rather than calling
  2148. * read_buf and panicing when we get an error from the
  2149. * driver.
  2150. */
  2151. if ((imap->im_blkno + imap->im_len) >
  2152. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  2153. xfs_alert(mp,
  2154. "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
  2155. __func__, (unsigned long long) imap->im_blkno,
  2156. (unsigned long long) imap->im_len,
  2157. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
  2158. return -EINVAL;
  2159. }
  2160. return 0;
  2161. }
  2162. /*
  2163. * Compute and fill in value of m_in_maxlevels.
  2164. */
  2165. void
  2166. xfs_ialloc_compute_maxlevels(
  2167. xfs_mount_t *mp) /* file system mount structure */
  2168. {
  2169. uint inodes;
  2170. inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
  2171. mp->m_in_maxlevels = xfs_btree_compute_maxlevels(mp->m_inobt_mnr,
  2172. inodes);
  2173. }
  2174. /*
  2175. * Log specified fields for the ag hdr (inode section). The growth of the agi
  2176. * structure over time requires that we interpret the buffer as two logical
  2177. * regions delineated by the end of the unlinked list. This is due to the size
  2178. * of the hash table and its location in the middle of the agi.
  2179. *
  2180. * For example, a request to log a field before agi_unlinked and a field after
  2181. * agi_unlinked could cause us to log the entire hash table and use an excessive
  2182. * amount of log space. To avoid this behavior, log the region up through
  2183. * agi_unlinked in one call and the region after agi_unlinked through the end of
  2184. * the structure in another.
  2185. */
  2186. void
  2187. xfs_ialloc_log_agi(
  2188. xfs_trans_t *tp, /* transaction pointer */
  2189. xfs_buf_t *bp, /* allocation group header buffer */
  2190. int fields) /* bitmask of fields to log */
  2191. {
  2192. int first; /* first byte number */
  2193. int last; /* last byte number */
  2194. static const short offsets[] = { /* field starting offsets */
  2195. /* keep in sync with bit definitions */
  2196. offsetof(xfs_agi_t, agi_magicnum),
  2197. offsetof(xfs_agi_t, agi_versionnum),
  2198. offsetof(xfs_agi_t, agi_seqno),
  2199. offsetof(xfs_agi_t, agi_length),
  2200. offsetof(xfs_agi_t, agi_count),
  2201. offsetof(xfs_agi_t, agi_root),
  2202. offsetof(xfs_agi_t, agi_level),
  2203. offsetof(xfs_agi_t, agi_freecount),
  2204. offsetof(xfs_agi_t, agi_newino),
  2205. offsetof(xfs_agi_t, agi_dirino),
  2206. offsetof(xfs_agi_t, agi_unlinked),
  2207. offsetof(xfs_agi_t, agi_free_root),
  2208. offsetof(xfs_agi_t, agi_free_level),
  2209. sizeof(xfs_agi_t)
  2210. };
  2211. #ifdef DEBUG
  2212. xfs_agi_t *agi; /* allocation group header */
  2213. agi = XFS_BUF_TO_AGI(bp);
  2214. ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
  2215. #endif
  2216. /*
  2217. * Compute byte offsets for the first and last fields in the first
  2218. * region and log the agi buffer. This only logs up through
  2219. * agi_unlinked.
  2220. */
  2221. if (fields & XFS_AGI_ALL_BITS_R1) {
  2222. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
  2223. &first, &last);
  2224. xfs_trans_log_buf(tp, bp, first, last);
  2225. }
  2226. /*
  2227. * Mask off the bits in the first region and calculate the first and
  2228. * last field offsets for any bits in the second region.
  2229. */
  2230. fields &= ~XFS_AGI_ALL_BITS_R1;
  2231. if (fields) {
  2232. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
  2233. &first, &last);
  2234. xfs_trans_log_buf(tp, bp, first, last);
  2235. }
  2236. }
  2237. static xfs_failaddr_t
  2238. xfs_agi_verify(
  2239. struct xfs_buf *bp)
  2240. {
  2241. struct xfs_mount *mp = bp->b_target->bt_mount;
  2242. struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
  2243. int i;
  2244. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2245. if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
  2246. return __this_address;
  2247. if (!xfs_log_check_lsn(mp,
  2248. be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn)))
  2249. return __this_address;
  2250. }
  2251. /*
  2252. * Validate the magic number of the agi block.
  2253. */
  2254. if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
  2255. return __this_address;
  2256. if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
  2257. return __this_address;
  2258. if (be32_to_cpu(agi->agi_level) < 1 ||
  2259. be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
  2260. return __this_address;
  2261. if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
  2262. (be32_to_cpu(agi->agi_free_level) < 1 ||
  2263. be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS))
  2264. return __this_address;
  2265. /*
  2266. * during growfs operations, the perag is not fully initialised,
  2267. * so we can't use it for any useful checking. growfs ensures we can't
  2268. * use it by using uncached buffers that don't have the perag attached
  2269. * so we can detect and avoid this problem.
  2270. */
  2271. if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
  2272. return __this_address;
  2273. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
  2274. if (agi->agi_unlinked[i] == NULLAGINO)
  2275. continue;
  2276. if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
  2277. return __this_address;
  2278. }
  2279. return NULL;
  2280. }
  2281. static void
  2282. xfs_agi_read_verify(
  2283. struct xfs_buf *bp)
  2284. {
  2285. struct xfs_mount *mp = bp->b_target->bt_mount;
  2286. xfs_failaddr_t fa;
  2287. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  2288. !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
  2289. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  2290. else {
  2291. fa = xfs_agi_verify(bp);
  2292. if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
  2293. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2294. }
  2295. }
  2296. static void
  2297. xfs_agi_write_verify(
  2298. struct xfs_buf *bp)
  2299. {
  2300. struct xfs_mount *mp = bp->b_target->bt_mount;
  2301. struct xfs_buf_log_item *bip = bp->b_log_item;
  2302. xfs_failaddr_t fa;
  2303. fa = xfs_agi_verify(bp);
  2304. if (fa) {
  2305. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2306. return;
  2307. }
  2308. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2309. return;
  2310. if (bip)
  2311. XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  2312. xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
  2313. }
  2314. const struct xfs_buf_ops xfs_agi_buf_ops = {
  2315. .name = "xfs_agi",
  2316. .verify_read = xfs_agi_read_verify,
  2317. .verify_write = xfs_agi_write_verify,
  2318. .verify_struct = xfs_agi_verify,
  2319. };
  2320. /*
  2321. * Read in the allocation group header (inode allocation section)
  2322. */
  2323. int
  2324. xfs_read_agi(
  2325. struct xfs_mount *mp, /* file system mount structure */
  2326. struct xfs_trans *tp, /* transaction pointer */
  2327. xfs_agnumber_t agno, /* allocation group number */
  2328. struct xfs_buf **bpp) /* allocation group hdr buf */
  2329. {
  2330. int error;
  2331. trace_xfs_read_agi(mp, agno);
  2332. ASSERT(agno != NULLAGNUMBER);
  2333. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  2334. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  2335. XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
  2336. if (error)
  2337. return error;
  2338. if (tp)
  2339. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF);
  2340. xfs_buf_set_ref(*bpp, XFS_AGI_REF);
  2341. return 0;
  2342. }
  2343. int
  2344. xfs_ialloc_read_agi(
  2345. struct xfs_mount *mp, /* file system mount structure */
  2346. struct xfs_trans *tp, /* transaction pointer */
  2347. xfs_agnumber_t agno, /* allocation group number */
  2348. struct xfs_buf **bpp) /* allocation group hdr buf */
  2349. {
  2350. struct xfs_agi *agi; /* allocation group header */
  2351. struct xfs_perag *pag; /* per allocation group data */
  2352. int error;
  2353. trace_xfs_ialloc_read_agi(mp, agno);
  2354. error = xfs_read_agi(mp, tp, agno, bpp);
  2355. if (error)
  2356. return error;
  2357. agi = XFS_BUF_TO_AGI(*bpp);
  2358. pag = xfs_perag_get(mp, agno);
  2359. if (!pag->pagi_init) {
  2360. pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
  2361. pag->pagi_count = be32_to_cpu(agi->agi_count);
  2362. pag->pagi_init = 1;
  2363. }
  2364. /*
  2365. * It's possible for these to be out of sync if
  2366. * we are in the middle of a forced shutdown.
  2367. */
  2368. ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
  2369. XFS_FORCED_SHUTDOWN(mp));
  2370. xfs_perag_put(pag);
  2371. return 0;
  2372. }
  2373. /*
  2374. * Read in the agi to initialise the per-ag data in the mount structure
  2375. */
  2376. int
  2377. xfs_ialloc_pagi_init(
  2378. xfs_mount_t *mp, /* file system mount structure */
  2379. xfs_trans_t *tp, /* transaction pointer */
  2380. xfs_agnumber_t agno) /* allocation group number */
  2381. {
  2382. xfs_buf_t *bp = NULL;
  2383. int error;
  2384. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  2385. if (error)
  2386. return error;
  2387. if (bp)
  2388. xfs_trans_brelse(tp, bp);
  2389. return 0;
  2390. }
  2391. /* Is there an inode record covering a given range of inode numbers? */
  2392. int
  2393. xfs_ialloc_has_inode_record(
  2394. struct xfs_btree_cur *cur,
  2395. xfs_agino_t low,
  2396. xfs_agino_t high,
  2397. bool *exists)
  2398. {
  2399. struct xfs_inobt_rec_incore irec;
  2400. xfs_agino_t agino;
  2401. uint16_t holemask;
  2402. int has_record;
  2403. int i;
  2404. int error;
  2405. *exists = false;
  2406. error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
  2407. while (error == 0 && has_record) {
  2408. error = xfs_inobt_get_rec(cur, &irec, &has_record);
  2409. if (error || irec.ir_startino > high)
  2410. break;
  2411. agino = irec.ir_startino;
  2412. holemask = irec.ir_holemask;
  2413. for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1,
  2414. i++, agino += XFS_INODES_PER_HOLEMASK_BIT) {
  2415. if (holemask & 1)
  2416. continue;
  2417. if (agino + XFS_INODES_PER_HOLEMASK_BIT > low &&
  2418. agino <= high) {
  2419. *exists = true;
  2420. return 0;
  2421. }
  2422. }
  2423. error = xfs_btree_increment(cur, 0, &has_record);
  2424. }
  2425. return error;
  2426. }
  2427. /* Is there an inode record covering a given extent? */
  2428. int
  2429. xfs_ialloc_has_inodes_at_extent(
  2430. struct xfs_btree_cur *cur,
  2431. xfs_agblock_t bno,
  2432. xfs_extlen_t len,
  2433. bool *exists)
  2434. {
  2435. xfs_agino_t low;
  2436. xfs_agino_t high;
  2437. low = XFS_OFFBNO_TO_AGINO(cur->bc_mp, bno, 0);
  2438. high = XFS_OFFBNO_TO_AGINO(cur->bc_mp, bno + len, 0) - 1;
  2439. return xfs_ialloc_has_inode_record(cur, low, high, exists);
  2440. }
  2441. struct xfs_ialloc_count_inodes {
  2442. xfs_agino_t count;
  2443. xfs_agino_t freecount;
  2444. };
  2445. /* Record inode counts across all inobt records. */
  2446. STATIC int
  2447. xfs_ialloc_count_inodes_rec(
  2448. struct xfs_btree_cur *cur,
  2449. union xfs_btree_rec *rec,
  2450. void *priv)
  2451. {
  2452. struct xfs_inobt_rec_incore irec;
  2453. struct xfs_ialloc_count_inodes *ci = priv;
  2454. xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
  2455. ci->count += irec.ir_count;
  2456. ci->freecount += irec.ir_freecount;
  2457. return 0;
  2458. }
  2459. /* Count allocated and free inodes under an inobt. */
  2460. int
  2461. xfs_ialloc_count_inodes(
  2462. struct xfs_btree_cur *cur,
  2463. xfs_agino_t *count,
  2464. xfs_agino_t *freecount)
  2465. {
  2466. struct xfs_ialloc_count_inodes ci = {0};
  2467. int error;
  2468. ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
  2469. error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
  2470. if (error)
  2471. return error;
  2472. *count = ci.count;
  2473. *freecount = ci.freecount;
  2474. return 0;
  2475. }