xfs_ialloc.c 79 KB

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