xfs_alloc.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170
  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_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_rmap.h"
  19. #include "xfs_alloc_btree.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_extent_busy.h"
  22. #include "xfs_errortag.h"
  23. #include "xfs_error.h"
  24. #include "xfs_cksum.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_log.h"
  29. #include "xfs_ag_resv.h"
  30. #include "xfs_bmap.h"
  31. extern kmem_zone_t *xfs_bmap_free_item_zone;
  32. struct workqueue_struct *xfs_alloc_wq;
  33. #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
  34. #define XFSA_FIXUP_BNO_OK 1
  35. #define XFSA_FIXUP_CNT_OK 2
  36. STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
  37. STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
  38. STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
  39. STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
  40. xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
  41. /*
  42. * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
  43. * the beginning of the block for a proper header with the location information
  44. * and CRC.
  45. */
  46. unsigned int
  47. xfs_agfl_size(
  48. struct xfs_mount *mp)
  49. {
  50. unsigned int size = mp->m_sb.sb_sectsize;
  51. if (xfs_sb_version_hascrc(&mp->m_sb))
  52. size -= sizeof(struct xfs_agfl);
  53. return size / sizeof(xfs_agblock_t);
  54. }
  55. unsigned int
  56. xfs_refc_block(
  57. struct xfs_mount *mp)
  58. {
  59. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  60. return XFS_RMAP_BLOCK(mp) + 1;
  61. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  62. return XFS_FIBT_BLOCK(mp) + 1;
  63. return XFS_IBT_BLOCK(mp) + 1;
  64. }
  65. xfs_extlen_t
  66. xfs_prealloc_blocks(
  67. struct xfs_mount *mp)
  68. {
  69. if (xfs_sb_version_hasreflink(&mp->m_sb))
  70. return xfs_refc_block(mp) + 1;
  71. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  72. return XFS_RMAP_BLOCK(mp) + 1;
  73. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  74. return XFS_FIBT_BLOCK(mp) + 1;
  75. return XFS_IBT_BLOCK(mp) + 1;
  76. }
  77. /*
  78. * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
  79. * AGF buffer (PV 947395), we place constraints on the relationship among
  80. * actual allocations for data blocks, freelist blocks, and potential file data
  81. * bmap btree blocks. However, these restrictions may result in no actual space
  82. * allocated for a delayed extent, for example, a data block in a certain AG is
  83. * allocated but there is no additional block for the additional bmap btree
  84. * block due to a split of the bmap btree of the file. The result of this may
  85. * lead to an infinite loop when the file gets flushed to disk and all delayed
  86. * extents need to be actually allocated. To get around this, we explicitly set
  87. * aside a few blocks which will not be reserved in delayed allocation.
  88. *
  89. * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
  90. * potential split of the file's bmap btree.
  91. */
  92. unsigned int
  93. xfs_alloc_set_aside(
  94. struct xfs_mount *mp)
  95. {
  96. return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
  97. }
  98. /*
  99. * When deciding how much space to allocate out of an AG, we limit the
  100. * allocation maximum size to the size the AG. However, we cannot use all the
  101. * blocks in the AG - some are permanently used by metadata. These
  102. * blocks are generally:
  103. * - the AG superblock, AGF, AGI and AGFL
  104. * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
  105. * the AGI free inode and rmap btree root blocks.
  106. * - blocks on the AGFL according to xfs_alloc_set_aside() limits
  107. * - the rmapbt root block
  108. *
  109. * The AG headers are sector sized, so the amount of space they take up is
  110. * dependent on filesystem geometry. The others are all single blocks.
  111. */
  112. unsigned int
  113. xfs_alloc_ag_max_usable(
  114. struct xfs_mount *mp)
  115. {
  116. unsigned int blocks;
  117. blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
  118. blocks += XFS_ALLOC_AGFL_RESERVE;
  119. blocks += 3; /* AGF, AGI btree root blocks */
  120. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  121. blocks++; /* finobt root block */
  122. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  123. blocks++; /* rmap root block */
  124. if (xfs_sb_version_hasreflink(&mp->m_sb))
  125. blocks++; /* refcount root block */
  126. return mp->m_sb.sb_agblocks - blocks;
  127. }
  128. /*
  129. * Lookup the record equal to [bno, len] in the btree given by cur.
  130. */
  131. STATIC int /* error */
  132. xfs_alloc_lookup_eq(
  133. struct xfs_btree_cur *cur, /* btree cursor */
  134. xfs_agblock_t bno, /* starting block of extent */
  135. xfs_extlen_t len, /* length of extent */
  136. int *stat) /* success/failure */
  137. {
  138. cur->bc_rec.a.ar_startblock = bno;
  139. cur->bc_rec.a.ar_blockcount = len;
  140. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  141. }
  142. /*
  143. * Lookup the first record greater than or equal to [bno, len]
  144. * in the btree given by cur.
  145. */
  146. int /* error */
  147. xfs_alloc_lookup_ge(
  148. struct xfs_btree_cur *cur, /* btree cursor */
  149. xfs_agblock_t bno, /* starting block of extent */
  150. xfs_extlen_t len, /* length of extent */
  151. int *stat) /* success/failure */
  152. {
  153. cur->bc_rec.a.ar_startblock = bno;
  154. cur->bc_rec.a.ar_blockcount = len;
  155. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  156. }
  157. /*
  158. * Lookup the first record less than or equal to [bno, len]
  159. * in the btree given by cur.
  160. */
  161. int /* error */
  162. xfs_alloc_lookup_le(
  163. struct xfs_btree_cur *cur, /* btree cursor */
  164. xfs_agblock_t bno, /* starting block of extent */
  165. xfs_extlen_t len, /* length of extent */
  166. int *stat) /* success/failure */
  167. {
  168. cur->bc_rec.a.ar_startblock = bno;
  169. cur->bc_rec.a.ar_blockcount = len;
  170. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  171. }
  172. /*
  173. * Update the record referred to by cur to the value given
  174. * by [bno, len].
  175. * This either works (return 0) or gets an EFSCORRUPTED error.
  176. */
  177. STATIC int /* error */
  178. xfs_alloc_update(
  179. struct xfs_btree_cur *cur, /* btree cursor */
  180. xfs_agblock_t bno, /* starting block of extent */
  181. xfs_extlen_t len) /* length of extent */
  182. {
  183. union xfs_btree_rec rec;
  184. rec.alloc.ar_startblock = cpu_to_be32(bno);
  185. rec.alloc.ar_blockcount = cpu_to_be32(len);
  186. return xfs_btree_update(cur, &rec);
  187. }
  188. /*
  189. * Get the data from the pointed-to record.
  190. */
  191. int /* error */
  192. xfs_alloc_get_rec(
  193. struct xfs_btree_cur *cur, /* btree cursor */
  194. xfs_agblock_t *bno, /* output: starting block of extent */
  195. xfs_extlen_t *len, /* output: length of extent */
  196. int *stat) /* output: success/failure */
  197. {
  198. struct xfs_mount *mp = cur->bc_mp;
  199. xfs_agnumber_t agno = cur->bc_private.a.agno;
  200. union xfs_btree_rec *rec;
  201. int error;
  202. error = xfs_btree_get_rec(cur, &rec, stat);
  203. if (error || !(*stat))
  204. return error;
  205. *bno = be32_to_cpu(rec->alloc.ar_startblock);
  206. *len = be32_to_cpu(rec->alloc.ar_blockcount);
  207. if (*len == 0)
  208. goto out_bad_rec;
  209. /* check for valid extent range, including overflow */
  210. if (!xfs_verify_agbno(mp, agno, *bno))
  211. goto out_bad_rec;
  212. if (*bno > *bno + *len)
  213. goto out_bad_rec;
  214. if (!xfs_verify_agbno(mp, agno, *bno + *len - 1))
  215. goto out_bad_rec;
  216. return 0;
  217. out_bad_rec:
  218. xfs_warn(mp,
  219. "%s Freespace BTree record corruption in AG %d detected!",
  220. cur->bc_btnum == XFS_BTNUM_BNO ? "Block" : "Size", agno);
  221. xfs_warn(mp,
  222. "start block 0x%x block count 0x%x", *bno, *len);
  223. return -EFSCORRUPTED;
  224. }
  225. /*
  226. * Compute aligned version of the found extent.
  227. * Takes alignment and min length into account.
  228. */
  229. STATIC bool
  230. xfs_alloc_compute_aligned(
  231. xfs_alloc_arg_t *args, /* allocation argument structure */
  232. xfs_agblock_t foundbno, /* starting block in found extent */
  233. xfs_extlen_t foundlen, /* length in found extent */
  234. xfs_agblock_t *resbno, /* result block number */
  235. xfs_extlen_t *reslen, /* result length */
  236. unsigned *busy_gen)
  237. {
  238. xfs_agblock_t bno = foundbno;
  239. xfs_extlen_t len = foundlen;
  240. xfs_extlen_t diff;
  241. bool busy;
  242. /* Trim busy sections out of found extent */
  243. busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
  244. /*
  245. * If we have a largish extent that happens to start before min_agbno,
  246. * see if we can shift it into range...
  247. */
  248. if (bno < args->min_agbno && bno + len > args->min_agbno) {
  249. diff = args->min_agbno - bno;
  250. if (len > diff) {
  251. bno += diff;
  252. len -= diff;
  253. }
  254. }
  255. if (args->alignment > 1 && len >= args->minlen) {
  256. xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
  257. diff = aligned_bno - bno;
  258. *resbno = aligned_bno;
  259. *reslen = diff >= len ? 0 : len - diff;
  260. } else {
  261. *resbno = bno;
  262. *reslen = len;
  263. }
  264. return busy;
  265. }
  266. /*
  267. * Compute best start block and diff for "near" allocations.
  268. * freelen >= wantlen already checked by caller.
  269. */
  270. STATIC xfs_extlen_t /* difference value (absolute) */
  271. xfs_alloc_compute_diff(
  272. xfs_agblock_t wantbno, /* target starting block */
  273. xfs_extlen_t wantlen, /* target length */
  274. xfs_extlen_t alignment, /* target alignment */
  275. int datatype, /* are we allocating data? */
  276. xfs_agblock_t freebno, /* freespace's starting block */
  277. xfs_extlen_t freelen, /* freespace's length */
  278. xfs_agblock_t *newbnop) /* result: best start block from free */
  279. {
  280. xfs_agblock_t freeend; /* end of freespace extent */
  281. xfs_agblock_t newbno1; /* return block number */
  282. xfs_agblock_t newbno2; /* other new block number */
  283. xfs_extlen_t newlen1=0; /* length with newbno1 */
  284. xfs_extlen_t newlen2=0; /* length with newbno2 */
  285. xfs_agblock_t wantend; /* end of target extent */
  286. bool userdata = xfs_alloc_is_userdata(datatype);
  287. ASSERT(freelen >= wantlen);
  288. freeend = freebno + freelen;
  289. wantend = wantbno + wantlen;
  290. /*
  291. * We want to allocate from the start of a free extent if it is past
  292. * the desired block or if we are allocating user data and the free
  293. * extent is before desired block. The second case is there to allow
  294. * for contiguous allocation from the remaining free space if the file
  295. * grows in the short term.
  296. */
  297. if (freebno >= wantbno || (userdata && freeend < wantend)) {
  298. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  299. newbno1 = NULLAGBLOCK;
  300. } else if (freeend >= wantend && alignment > 1) {
  301. newbno1 = roundup(wantbno, alignment);
  302. newbno2 = newbno1 - alignment;
  303. if (newbno1 >= freeend)
  304. newbno1 = NULLAGBLOCK;
  305. else
  306. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  307. if (newbno2 < freebno)
  308. newbno2 = NULLAGBLOCK;
  309. else
  310. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  311. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  312. if (newlen1 < newlen2 ||
  313. (newlen1 == newlen2 &&
  314. XFS_ABSDIFF(newbno1, wantbno) >
  315. XFS_ABSDIFF(newbno2, wantbno)))
  316. newbno1 = newbno2;
  317. } else if (newbno2 != NULLAGBLOCK)
  318. newbno1 = newbno2;
  319. } else if (freeend >= wantend) {
  320. newbno1 = wantbno;
  321. } else if (alignment > 1) {
  322. newbno1 = roundup(freeend - wantlen, alignment);
  323. if (newbno1 > freeend - wantlen &&
  324. newbno1 - alignment >= freebno)
  325. newbno1 -= alignment;
  326. else if (newbno1 >= freeend)
  327. newbno1 = NULLAGBLOCK;
  328. } else
  329. newbno1 = freeend - wantlen;
  330. *newbnop = newbno1;
  331. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  332. }
  333. /*
  334. * Fix up the length, based on mod and prod.
  335. * len should be k * prod + mod for some k.
  336. * If len is too small it is returned unchanged.
  337. * If len hits maxlen it is left alone.
  338. */
  339. STATIC void
  340. xfs_alloc_fix_len(
  341. xfs_alloc_arg_t *args) /* allocation argument structure */
  342. {
  343. xfs_extlen_t k;
  344. xfs_extlen_t rlen;
  345. ASSERT(args->mod < args->prod);
  346. rlen = args->len;
  347. ASSERT(rlen >= args->minlen);
  348. ASSERT(rlen <= args->maxlen);
  349. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  350. (args->mod == 0 && rlen < args->prod))
  351. return;
  352. k = rlen % args->prod;
  353. if (k == args->mod)
  354. return;
  355. if (k > args->mod)
  356. rlen = rlen - (k - args->mod);
  357. else
  358. rlen = rlen - args->prod + (args->mod - k);
  359. /* casts to (int) catch length underflows */
  360. if ((int)rlen < (int)args->minlen)
  361. return;
  362. ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
  363. ASSERT(rlen % args->prod == args->mod);
  364. ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
  365. rlen + args->minleft);
  366. args->len = rlen;
  367. }
  368. /*
  369. * Update the two btrees, logically removing from freespace the extent
  370. * starting at rbno, rlen blocks. The extent is contained within the
  371. * actual (current) free extent fbno for flen blocks.
  372. * Flags are passed in indicating whether the cursors are set to the
  373. * relevant records.
  374. */
  375. STATIC int /* error code */
  376. xfs_alloc_fixup_trees(
  377. xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
  378. xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
  379. xfs_agblock_t fbno, /* starting block of free extent */
  380. xfs_extlen_t flen, /* length of free extent */
  381. xfs_agblock_t rbno, /* starting block of returned extent */
  382. xfs_extlen_t rlen, /* length of returned extent */
  383. int flags) /* flags, XFSA_FIXUP_... */
  384. {
  385. int error; /* error code */
  386. int i; /* operation results */
  387. xfs_agblock_t nfbno1; /* first new free startblock */
  388. xfs_agblock_t nfbno2; /* second new free startblock */
  389. xfs_extlen_t nflen1=0; /* first new free length */
  390. xfs_extlen_t nflen2=0; /* second new free length */
  391. struct xfs_mount *mp;
  392. mp = cnt_cur->bc_mp;
  393. /*
  394. * Look up the record in the by-size tree if necessary.
  395. */
  396. if (flags & XFSA_FIXUP_CNT_OK) {
  397. #ifdef DEBUG
  398. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  399. return error;
  400. XFS_WANT_CORRUPTED_RETURN(mp,
  401. i == 1 && nfbno1 == fbno && nflen1 == flen);
  402. #endif
  403. } else {
  404. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  405. return error;
  406. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  407. }
  408. /*
  409. * Look up the record in the by-block tree if necessary.
  410. */
  411. if (flags & XFSA_FIXUP_BNO_OK) {
  412. #ifdef DEBUG
  413. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  414. return error;
  415. XFS_WANT_CORRUPTED_RETURN(mp,
  416. i == 1 && nfbno1 == fbno && nflen1 == flen);
  417. #endif
  418. } else {
  419. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  420. return error;
  421. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  422. }
  423. #ifdef DEBUG
  424. if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
  425. struct xfs_btree_block *bnoblock;
  426. struct xfs_btree_block *cntblock;
  427. bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
  428. cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
  429. XFS_WANT_CORRUPTED_RETURN(mp,
  430. bnoblock->bb_numrecs == cntblock->bb_numrecs);
  431. }
  432. #endif
  433. /*
  434. * Deal with all four cases: the allocated record is contained
  435. * within the freespace record, so we can have new freespace
  436. * at either (or both) end, or no freespace remaining.
  437. */
  438. if (rbno == fbno && rlen == flen)
  439. nfbno1 = nfbno2 = NULLAGBLOCK;
  440. else if (rbno == fbno) {
  441. nfbno1 = rbno + rlen;
  442. nflen1 = flen - rlen;
  443. nfbno2 = NULLAGBLOCK;
  444. } else if (rbno + rlen == fbno + flen) {
  445. nfbno1 = fbno;
  446. nflen1 = flen - rlen;
  447. nfbno2 = NULLAGBLOCK;
  448. } else {
  449. nfbno1 = fbno;
  450. nflen1 = rbno - fbno;
  451. nfbno2 = rbno + rlen;
  452. nflen2 = (fbno + flen) - nfbno2;
  453. }
  454. /*
  455. * Delete the entry from the by-size btree.
  456. */
  457. if ((error = xfs_btree_delete(cnt_cur, &i)))
  458. return error;
  459. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  460. /*
  461. * Add new by-size btree entry(s).
  462. */
  463. if (nfbno1 != NULLAGBLOCK) {
  464. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  465. return error;
  466. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  467. if ((error = xfs_btree_insert(cnt_cur, &i)))
  468. return error;
  469. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  470. }
  471. if (nfbno2 != NULLAGBLOCK) {
  472. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  473. return error;
  474. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  475. if ((error = xfs_btree_insert(cnt_cur, &i)))
  476. return error;
  477. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  478. }
  479. /*
  480. * Fix up the by-block btree entry(s).
  481. */
  482. if (nfbno1 == NULLAGBLOCK) {
  483. /*
  484. * No remaining freespace, just delete the by-block tree entry.
  485. */
  486. if ((error = xfs_btree_delete(bno_cur, &i)))
  487. return error;
  488. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  489. } else {
  490. /*
  491. * Update the by-block entry to start later|be shorter.
  492. */
  493. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  494. return error;
  495. }
  496. if (nfbno2 != NULLAGBLOCK) {
  497. /*
  498. * 2 resulting free entries, need to add one.
  499. */
  500. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  501. return error;
  502. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  503. if ((error = xfs_btree_insert(bno_cur, &i)))
  504. return error;
  505. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  506. }
  507. return 0;
  508. }
  509. static xfs_failaddr_t
  510. xfs_agfl_verify(
  511. struct xfs_buf *bp)
  512. {
  513. struct xfs_mount *mp = bp->b_target->bt_mount;
  514. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  515. int i;
  516. /*
  517. * There is no verification of non-crc AGFLs because mkfs does not
  518. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  519. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  520. * can't verify just those entries are valid.
  521. */
  522. if (!xfs_sb_version_hascrc(&mp->m_sb))
  523. return NULL;
  524. if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
  525. return __this_address;
  526. if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
  527. return __this_address;
  528. /*
  529. * during growfs operations, the perag is not fully initialised,
  530. * so we can't use it for any useful checking. growfs ensures we can't
  531. * use it by using uncached buffers that don't have the perag attached
  532. * so we can detect and avoid this problem.
  533. */
  534. if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
  535. return __this_address;
  536. for (i = 0; i < xfs_agfl_size(mp); i++) {
  537. if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
  538. be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
  539. return __this_address;
  540. }
  541. if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
  542. return __this_address;
  543. return NULL;
  544. }
  545. static void
  546. xfs_agfl_read_verify(
  547. struct xfs_buf *bp)
  548. {
  549. struct xfs_mount *mp = bp->b_target->bt_mount;
  550. xfs_failaddr_t fa;
  551. /*
  552. * There is no verification of non-crc AGFLs because mkfs does not
  553. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  554. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  555. * can't verify just those entries are valid.
  556. */
  557. if (!xfs_sb_version_hascrc(&mp->m_sb))
  558. return;
  559. if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
  560. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  561. else {
  562. fa = xfs_agfl_verify(bp);
  563. if (fa)
  564. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  565. }
  566. }
  567. static void
  568. xfs_agfl_write_verify(
  569. struct xfs_buf *bp)
  570. {
  571. struct xfs_mount *mp = bp->b_target->bt_mount;
  572. struct xfs_buf_log_item *bip = bp->b_log_item;
  573. xfs_failaddr_t fa;
  574. /* no verification of non-crc AGFLs */
  575. if (!xfs_sb_version_hascrc(&mp->m_sb))
  576. return;
  577. fa = xfs_agfl_verify(bp);
  578. if (fa) {
  579. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  580. return;
  581. }
  582. if (bip)
  583. XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  584. xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
  585. }
  586. const struct xfs_buf_ops xfs_agfl_buf_ops = {
  587. .name = "xfs_agfl",
  588. .verify_read = xfs_agfl_read_verify,
  589. .verify_write = xfs_agfl_write_verify,
  590. .verify_struct = xfs_agfl_verify,
  591. };
  592. /*
  593. * Read in the allocation group free block array.
  594. */
  595. int /* error */
  596. xfs_alloc_read_agfl(
  597. xfs_mount_t *mp, /* mount point structure */
  598. xfs_trans_t *tp, /* transaction pointer */
  599. xfs_agnumber_t agno, /* allocation group number */
  600. xfs_buf_t **bpp) /* buffer for the ag free block array */
  601. {
  602. xfs_buf_t *bp; /* return value */
  603. int error;
  604. ASSERT(agno != NULLAGNUMBER);
  605. error = xfs_trans_read_buf(
  606. mp, tp, mp->m_ddev_targp,
  607. XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
  608. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
  609. if (error)
  610. return error;
  611. xfs_buf_set_ref(bp, XFS_AGFL_REF);
  612. *bpp = bp;
  613. return 0;
  614. }
  615. STATIC int
  616. xfs_alloc_update_counters(
  617. struct xfs_trans *tp,
  618. struct xfs_perag *pag,
  619. struct xfs_buf *agbp,
  620. long len)
  621. {
  622. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  623. pag->pagf_freeblks += len;
  624. be32_add_cpu(&agf->agf_freeblks, len);
  625. xfs_trans_agblocks_delta(tp, len);
  626. if (unlikely(be32_to_cpu(agf->agf_freeblks) >
  627. be32_to_cpu(agf->agf_length)))
  628. return -EFSCORRUPTED;
  629. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  630. return 0;
  631. }
  632. /*
  633. * Allocation group level functions.
  634. */
  635. /*
  636. * Allocate a variable extent in the allocation group agno.
  637. * Type and bno are used to determine where in the allocation group the
  638. * extent will start.
  639. * Extent's length (returned in *len) will be between minlen and maxlen,
  640. * and of the form k * prod + mod unless there's nothing that large.
  641. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  642. */
  643. STATIC int /* error */
  644. xfs_alloc_ag_vextent(
  645. xfs_alloc_arg_t *args) /* argument structure for allocation */
  646. {
  647. int error=0;
  648. ASSERT(args->minlen > 0);
  649. ASSERT(args->maxlen > 0);
  650. ASSERT(args->minlen <= args->maxlen);
  651. ASSERT(args->mod < args->prod);
  652. ASSERT(args->alignment > 0);
  653. /*
  654. * Branch to correct routine based on the type.
  655. */
  656. args->wasfromfl = 0;
  657. switch (args->type) {
  658. case XFS_ALLOCTYPE_THIS_AG:
  659. error = xfs_alloc_ag_vextent_size(args);
  660. break;
  661. case XFS_ALLOCTYPE_NEAR_BNO:
  662. error = xfs_alloc_ag_vextent_near(args);
  663. break;
  664. case XFS_ALLOCTYPE_THIS_BNO:
  665. error = xfs_alloc_ag_vextent_exact(args);
  666. break;
  667. default:
  668. ASSERT(0);
  669. /* NOTREACHED */
  670. }
  671. if (error || args->agbno == NULLAGBLOCK)
  672. return error;
  673. ASSERT(args->len >= args->minlen);
  674. ASSERT(args->len <= args->maxlen);
  675. ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
  676. ASSERT(args->agbno % args->alignment == 0);
  677. /* if not file data, insert new block into the reverse map btree */
  678. if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
  679. error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
  680. args->agbno, args->len, &args->oinfo);
  681. if (error)
  682. return error;
  683. }
  684. if (!args->wasfromfl) {
  685. error = xfs_alloc_update_counters(args->tp, args->pag,
  686. args->agbp,
  687. -((long)(args->len)));
  688. if (error)
  689. return error;
  690. ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
  691. args->agbno, args->len));
  692. }
  693. xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
  694. XFS_STATS_INC(args->mp, xs_allocx);
  695. XFS_STATS_ADD(args->mp, xs_allocb, args->len);
  696. return error;
  697. }
  698. /*
  699. * Allocate a variable extent at exactly agno/bno.
  700. * Extent's length (returned in *len) will be between minlen and maxlen,
  701. * and of the form k * prod + mod unless there's nothing that large.
  702. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  703. */
  704. STATIC int /* error */
  705. xfs_alloc_ag_vextent_exact(
  706. xfs_alloc_arg_t *args) /* allocation argument structure */
  707. {
  708. xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
  709. xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
  710. int error;
  711. xfs_agblock_t fbno; /* start block of found extent */
  712. xfs_extlen_t flen; /* length of found extent */
  713. xfs_agblock_t tbno; /* start block of busy extent */
  714. xfs_extlen_t tlen; /* length of busy extent */
  715. xfs_agblock_t tend; /* end block of busy extent */
  716. int i; /* success/failure of operation */
  717. unsigned busy_gen;
  718. ASSERT(args->alignment == 1);
  719. /*
  720. * Allocate/initialize a cursor for the by-number freespace btree.
  721. */
  722. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  723. args->agno, XFS_BTNUM_BNO);
  724. /*
  725. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  726. * Look for the closest free block <= bno, it must contain bno
  727. * if any free block does.
  728. */
  729. error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
  730. if (error)
  731. goto error0;
  732. if (!i)
  733. goto not_found;
  734. /*
  735. * Grab the freespace record.
  736. */
  737. error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
  738. if (error)
  739. goto error0;
  740. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  741. ASSERT(fbno <= args->agbno);
  742. /*
  743. * Check for overlapping busy extents.
  744. */
  745. tbno = fbno;
  746. tlen = flen;
  747. xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
  748. /*
  749. * Give up if the start of the extent is busy, or the freespace isn't
  750. * long enough for the minimum request.
  751. */
  752. if (tbno > args->agbno)
  753. goto not_found;
  754. if (tlen < args->minlen)
  755. goto not_found;
  756. tend = tbno + tlen;
  757. if (tend < args->agbno + args->minlen)
  758. goto not_found;
  759. /*
  760. * End of extent will be smaller of the freespace end and the
  761. * maximal requested end.
  762. *
  763. * Fix the length according to mod and prod if given.
  764. */
  765. args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
  766. - args->agbno;
  767. xfs_alloc_fix_len(args);
  768. ASSERT(args->agbno + args->len <= tend);
  769. /*
  770. * We are allocating agbno for args->len
  771. * Allocate/initialize a cursor for the by-size btree.
  772. */
  773. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  774. args->agno, XFS_BTNUM_CNT);
  775. ASSERT(args->agbno + args->len <=
  776. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  777. error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
  778. args->len, XFSA_FIXUP_BNO_OK);
  779. if (error) {
  780. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  781. goto error0;
  782. }
  783. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  784. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  785. args->wasfromfl = 0;
  786. trace_xfs_alloc_exact_done(args);
  787. return 0;
  788. not_found:
  789. /* Didn't find it, return null. */
  790. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  791. args->agbno = NULLAGBLOCK;
  792. trace_xfs_alloc_exact_notfound(args);
  793. return 0;
  794. error0:
  795. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  796. trace_xfs_alloc_exact_error(args);
  797. return error;
  798. }
  799. /*
  800. * Search the btree in a given direction via the search cursor and compare
  801. * the records found against the good extent we've already found.
  802. */
  803. STATIC int
  804. xfs_alloc_find_best_extent(
  805. struct xfs_alloc_arg *args, /* allocation argument structure */
  806. struct xfs_btree_cur **gcur, /* good cursor */
  807. struct xfs_btree_cur **scur, /* searching cursor */
  808. xfs_agblock_t gdiff, /* difference for search comparison */
  809. xfs_agblock_t *sbno, /* extent found by search */
  810. xfs_extlen_t *slen, /* extent length */
  811. xfs_agblock_t *sbnoa, /* aligned extent found by search */
  812. xfs_extlen_t *slena, /* aligned extent length */
  813. int dir) /* 0 = search right, 1 = search left */
  814. {
  815. xfs_agblock_t new;
  816. xfs_agblock_t sdiff;
  817. int error;
  818. int i;
  819. unsigned busy_gen;
  820. /* The good extent is perfect, no need to search. */
  821. if (!gdiff)
  822. goto out_use_good;
  823. /*
  824. * Look until we find a better one, run out of space or run off the end.
  825. */
  826. do {
  827. error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
  828. if (error)
  829. goto error0;
  830. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  831. xfs_alloc_compute_aligned(args, *sbno, *slen,
  832. sbnoa, slena, &busy_gen);
  833. /*
  834. * The good extent is closer than this one.
  835. */
  836. if (!dir) {
  837. if (*sbnoa > args->max_agbno)
  838. goto out_use_good;
  839. if (*sbnoa >= args->agbno + gdiff)
  840. goto out_use_good;
  841. } else {
  842. if (*sbnoa < args->min_agbno)
  843. goto out_use_good;
  844. if (*sbnoa <= args->agbno - gdiff)
  845. goto out_use_good;
  846. }
  847. /*
  848. * Same distance, compare length and pick the best.
  849. */
  850. if (*slena >= args->minlen) {
  851. args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
  852. xfs_alloc_fix_len(args);
  853. sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  854. args->alignment,
  855. args->datatype, *sbnoa,
  856. *slena, &new);
  857. /*
  858. * Choose closer size and invalidate other cursor.
  859. */
  860. if (sdiff < gdiff)
  861. goto out_use_search;
  862. goto out_use_good;
  863. }
  864. if (!dir)
  865. error = xfs_btree_increment(*scur, 0, &i);
  866. else
  867. error = xfs_btree_decrement(*scur, 0, &i);
  868. if (error)
  869. goto error0;
  870. } while (i);
  871. out_use_good:
  872. xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
  873. *scur = NULL;
  874. return 0;
  875. out_use_search:
  876. xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
  877. *gcur = NULL;
  878. return 0;
  879. error0:
  880. /* caller invalidates cursors */
  881. return error;
  882. }
  883. /*
  884. * Allocate a variable extent near bno in the allocation group agno.
  885. * Extent's length (returned in len) will be between minlen and maxlen,
  886. * and of the form k * prod + mod unless there's nothing that large.
  887. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  888. */
  889. STATIC int /* error */
  890. xfs_alloc_ag_vextent_near(
  891. xfs_alloc_arg_t *args) /* allocation argument structure */
  892. {
  893. xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
  894. xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
  895. xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
  896. xfs_agblock_t gtbno; /* start bno of right side entry */
  897. xfs_agblock_t gtbnoa; /* aligned ... */
  898. xfs_extlen_t gtdiff; /* difference to right side entry */
  899. xfs_extlen_t gtlen; /* length of right side entry */
  900. xfs_extlen_t gtlena; /* aligned ... */
  901. xfs_agblock_t gtnew; /* useful start bno of right side */
  902. int error; /* error code */
  903. int i; /* result code, temporary */
  904. int j; /* result code, temporary */
  905. xfs_agblock_t ltbno; /* start bno of left side entry */
  906. xfs_agblock_t ltbnoa; /* aligned ... */
  907. xfs_extlen_t ltdiff; /* difference to left side entry */
  908. xfs_extlen_t ltlen; /* length of left side entry */
  909. xfs_extlen_t ltlena; /* aligned ... */
  910. xfs_agblock_t ltnew; /* useful start bno of left side */
  911. xfs_extlen_t rlen; /* length of returned extent */
  912. bool busy;
  913. unsigned busy_gen;
  914. #ifdef DEBUG
  915. /*
  916. * Randomly don't execute the first algorithm.
  917. */
  918. int dofirst; /* set to do first algorithm */
  919. dofirst = prandom_u32() & 1;
  920. #endif
  921. /* handle unitialized agbno range so caller doesn't have to */
  922. if (!args->min_agbno && !args->max_agbno)
  923. args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
  924. ASSERT(args->min_agbno <= args->max_agbno);
  925. /* clamp agbno to the range if it's outside */
  926. if (args->agbno < args->min_agbno)
  927. args->agbno = args->min_agbno;
  928. if (args->agbno > args->max_agbno)
  929. args->agbno = args->max_agbno;
  930. restart:
  931. bno_cur_lt = NULL;
  932. bno_cur_gt = NULL;
  933. ltlen = 0;
  934. gtlena = 0;
  935. ltlena = 0;
  936. busy = false;
  937. /*
  938. * Get a cursor for the by-size btree.
  939. */
  940. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  941. args->agno, XFS_BTNUM_CNT);
  942. /*
  943. * See if there are any free extents as big as maxlen.
  944. */
  945. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
  946. goto error0;
  947. /*
  948. * If none, then pick up the last entry in the tree unless the
  949. * tree is empty.
  950. */
  951. if (!i) {
  952. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
  953. &ltlen, &i)))
  954. goto error0;
  955. if (i == 0 || ltlen == 0) {
  956. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  957. trace_xfs_alloc_near_noentry(args);
  958. return 0;
  959. }
  960. ASSERT(i == 1);
  961. }
  962. args->wasfromfl = 0;
  963. /*
  964. * First algorithm.
  965. * If the requested extent is large wrt the freespaces available
  966. * in this a.g., then the cursor will be pointing to a btree entry
  967. * near the right edge of the tree. If it's in the last btree leaf
  968. * block, then we just examine all the entries in that block
  969. * that are big enough, and pick the best one.
  970. * This is written as a while loop so we can break out of it,
  971. * but we never loop back to the top.
  972. */
  973. while (xfs_btree_islastblock(cnt_cur, 0)) {
  974. xfs_extlen_t bdiff;
  975. int besti=0;
  976. xfs_extlen_t blen=0;
  977. xfs_agblock_t bnew=0;
  978. #ifdef DEBUG
  979. if (dofirst)
  980. break;
  981. #endif
  982. /*
  983. * Start from the entry that lookup found, sequence through
  984. * all larger free blocks. If we're actually pointing at a
  985. * record smaller than maxlen, go to the start of this block,
  986. * and skip all those smaller than minlen.
  987. */
  988. if (ltlen || args->alignment > 1) {
  989. cnt_cur->bc_ptrs[0] = 1;
  990. do {
  991. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
  992. &ltlen, &i)))
  993. goto error0;
  994. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  995. if (ltlen >= args->minlen)
  996. break;
  997. if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
  998. goto error0;
  999. } while (i);
  1000. ASSERT(ltlen >= args->minlen);
  1001. if (!i)
  1002. break;
  1003. }
  1004. i = cnt_cur->bc_ptrs[0];
  1005. for (j = 1, blen = 0, bdiff = 0;
  1006. !error && j && (blen < args->maxlen || bdiff > 0);
  1007. error = xfs_btree_increment(cnt_cur, 0, &j)) {
  1008. /*
  1009. * For each entry, decide if it's better than
  1010. * the previous best entry.
  1011. */
  1012. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  1013. goto error0;
  1014. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1015. busy = xfs_alloc_compute_aligned(args, ltbno, ltlen,
  1016. &ltbnoa, &ltlena, &busy_gen);
  1017. if (ltlena < args->minlen)
  1018. continue;
  1019. if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
  1020. continue;
  1021. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1022. xfs_alloc_fix_len(args);
  1023. ASSERT(args->len >= args->minlen);
  1024. if (args->len < blen)
  1025. continue;
  1026. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1027. args->alignment, args->datatype, ltbnoa,
  1028. ltlena, &ltnew);
  1029. if (ltnew != NULLAGBLOCK &&
  1030. (args->len > blen || ltdiff < bdiff)) {
  1031. bdiff = ltdiff;
  1032. bnew = ltnew;
  1033. blen = args->len;
  1034. besti = cnt_cur->bc_ptrs[0];
  1035. }
  1036. }
  1037. /*
  1038. * It didn't work. We COULD be in a case where
  1039. * there's a good record somewhere, so try again.
  1040. */
  1041. if (blen == 0)
  1042. break;
  1043. /*
  1044. * Point at the best entry, and retrieve it again.
  1045. */
  1046. cnt_cur->bc_ptrs[0] = besti;
  1047. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  1048. goto error0;
  1049. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1050. ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1051. args->len = blen;
  1052. /*
  1053. * We are allocating starting at bnew for blen blocks.
  1054. */
  1055. args->agbno = bnew;
  1056. ASSERT(bnew >= ltbno);
  1057. ASSERT(bnew + blen <= ltbno + ltlen);
  1058. /*
  1059. * Set up a cursor for the by-bno tree.
  1060. */
  1061. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
  1062. args->agbp, args->agno, XFS_BTNUM_BNO);
  1063. /*
  1064. * Fix up the btree entries.
  1065. */
  1066. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
  1067. ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
  1068. goto error0;
  1069. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1070. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1071. trace_xfs_alloc_near_first(args);
  1072. return 0;
  1073. }
  1074. /*
  1075. * Second algorithm.
  1076. * Search in the by-bno tree to the left and to the right
  1077. * simultaneously, until in each case we find a space big enough,
  1078. * or run into the edge of the tree. When we run into the edge,
  1079. * we deallocate that cursor.
  1080. * If both searches succeed, we compare the two spaces and pick
  1081. * the better one.
  1082. * With alignment, it's possible for both to fail; the upper
  1083. * level algorithm that picks allocation groups for allocations
  1084. * is not supposed to do this.
  1085. */
  1086. /*
  1087. * Allocate and initialize the cursor for the leftward search.
  1088. */
  1089. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1090. args->agno, XFS_BTNUM_BNO);
  1091. /*
  1092. * Lookup <= bno to find the leftward search's starting point.
  1093. */
  1094. if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
  1095. goto error0;
  1096. if (!i) {
  1097. /*
  1098. * Didn't find anything; use this cursor for the rightward
  1099. * search.
  1100. */
  1101. bno_cur_gt = bno_cur_lt;
  1102. bno_cur_lt = NULL;
  1103. }
  1104. /*
  1105. * Found something. Duplicate the cursor for the rightward search.
  1106. */
  1107. else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
  1108. goto error0;
  1109. /*
  1110. * Increment the cursor, so we will point at the entry just right
  1111. * of the leftward entry if any, or to the leftmost entry.
  1112. */
  1113. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  1114. goto error0;
  1115. if (!i) {
  1116. /*
  1117. * It failed, there are no rightward entries.
  1118. */
  1119. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
  1120. bno_cur_gt = NULL;
  1121. }
  1122. /*
  1123. * Loop going left with the leftward cursor, right with the
  1124. * rightward cursor, until either both directions give up or
  1125. * we find an entry at least as big as minlen.
  1126. */
  1127. do {
  1128. if (bno_cur_lt) {
  1129. if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
  1130. goto error0;
  1131. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1132. busy |= xfs_alloc_compute_aligned(args, ltbno, ltlen,
  1133. &ltbnoa, &ltlena, &busy_gen);
  1134. if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
  1135. break;
  1136. if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
  1137. goto error0;
  1138. if (!i || ltbnoa < args->min_agbno) {
  1139. xfs_btree_del_cursor(bno_cur_lt,
  1140. XFS_BTREE_NOERROR);
  1141. bno_cur_lt = NULL;
  1142. }
  1143. }
  1144. if (bno_cur_gt) {
  1145. if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
  1146. goto error0;
  1147. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1148. busy |= xfs_alloc_compute_aligned(args, gtbno, gtlen,
  1149. &gtbnoa, &gtlena, &busy_gen);
  1150. if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
  1151. break;
  1152. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  1153. goto error0;
  1154. if (!i || gtbnoa > args->max_agbno) {
  1155. xfs_btree_del_cursor(bno_cur_gt,
  1156. XFS_BTREE_NOERROR);
  1157. bno_cur_gt = NULL;
  1158. }
  1159. }
  1160. } while (bno_cur_lt || bno_cur_gt);
  1161. /*
  1162. * Got both cursors still active, need to find better entry.
  1163. */
  1164. if (bno_cur_lt && bno_cur_gt) {
  1165. if (ltlena >= args->minlen) {
  1166. /*
  1167. * Left side is good, look for a right side entry.
  1168. */
  1169. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1170. xfs_alloc_fix_len(args);
  1171. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1172. args->alignment, args->datatype, ltbnoa,
  1173. ltlena, &ltnew);
  1174. error = xfs_alloc_find_best_extent(args,
  1175. &bno_cur_lt, &bno_cur_gt,
  1176. ltdiff, &gtbno, &gtlen,
  1177. &gtbnoa, &gtlena,
  1178. 0 /* search right */);
  1179. } else {
  1180. ASSERT(gtlena >= args->minlen);
  1181. /*
  1182. * Right side is good, look for a left side entry.
  1183. */
  1184. args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
  1185. xfs_alloc_fix_len(args);
  1186. gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1187. args->alignment, args->datatype, gtbnoa,
  1188. gtlena, &gtnew);
  1189. error = xfs_alloc_find_best_extent(args,
  1190. &bno_cur_gt, &bno_cur_lt,
  1191. gtdiff, &ltbno, &ltlen,
  1192. &ltbnoa, &ltlena,
  1193. 1 /* search left */);
  1194. }
  1195. if (error)
  1196. goto error0;
  1197. }
  1198. /*
  1199. * If we couldn't get anything, give up.
  1200. */
  1201. if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
  1202. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1203. if (busy) {
  1204. trace_xfs_alloc_near_busy(args);
  1205. xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
  1206. goto restart;
  1207. }
  1208. trace_xfs_alloc_size_neither(args);
  1209. args->agbno = NULLAGBLOCK;
  1210. return 0;
  1211. }
  1212. /*
  1213. * At this point we have selected a freespace entry, either to the
  1214. * left or to the right. If it's on the right, copy all the
  1215. * useful variables to the "left" set so we only have one
  1216. * copy of this code.
  1217. */
  1218. if (bno_cur_gt) {
  1219. bno_cur_lt = bno_cur_gt;
  1220. bno_cur_gt = NULL;
  1221. ltbno = gtbno;
  1222. ltbnoa = gtbnoa;
  1223. ltlen = gtlen;
  1224. ltlena = gtlena;
  1225. j = 1;
  1226. } else
  1227. j = 0;
  1228. /*
  1229. * Fix up the length and compute the useful address.
  1230. */
  1231. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1232. xfs_alloc_fix_len(args);
  1233. rlen = args->len;
  1234. (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
  1235. args->datatype, ltbnoa, ltlena, &ltnew);
  1236. ASSERT(ltnew >= ltbno);
  1237. ASSERT(ltnew + rlen <= ltbnoa + ltlena);
  1238. ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1239. ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
  1240. args->agbno = ltnew;
  1241. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
  1242. ltnew, rlen, XFSA_FIXUP_BNO_OK)))
  1243. goto error0;
  1244. if (j)
  1245. trace_xfs_alloc_near_greater(args);
  1246. else
  1247. trace_xfs_alloc_near_lesser(args);
  1248. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1249. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1250. return 0;
  1251. error0:
  1252. trace_xfs_alloc_near_error(args);
  1253. if (cnt_cur != NULL)
  1254. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1255. if (bno_cur_lt != NULL)
  1256. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
  1257. if (bno_cur_gt != NULL)
  1258. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
  1259. return error;
  1260. }
  1261. /*
  1262. * Allocate a variable extent anywhere in the allocation group agno.
  1263. * Extent's length (returned in len) will be between minlen and maxlen,
  1264. * and of the form k * prod + mod unless there's nothing that large.
  1265. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1266. */
  1267. STATIC int /* error */
  1268. xfs_alloc_ag_vextent_size(
  1269. xfs_alloc_arg_t *args) /* allocation argument structure */
  1270. {
  1271. xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
  1272. xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
  1273. int error; /* error result */
  1274. xfs_agblock_t fbno; /* start of found freespace */
  1275. xfs_extlen_t flen; /* length of found freespace */
  1276. int i; /* temp status variable */
  1277. xfs_agblock_t rbno; /* returned block number */
  1278. xfs_extlen_t rlen; /* length of returned extent */
  1279. bool busy;
  1280. unsigned busy_gen;
  1281. restart:
  1282. /*
  1283. * Allocate and initialize a cursor for the by-size btree.
  1284. */
  1285. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1286. args->agno, XFS_BTNUM_CNT);
  1287. bno_cur = NULL;
  1288. busy = false;
  1289. /*
  1290. * Look for an entry >= maxlen+alignment-1 blocks.
  1291. */
  1292. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1293. args->maxlen + args->alignment - 1, &i)))
  1294. goto error0;
  1295. /*
  1296. * If none then we have to settle for a smaller extent. In the case that
  1297. * there are no large extents, this will return the last entry in the
  1298. * tree unless the tree is empty. In the case that there are only busy
  1299. * large extents, this will return the largest small extent unless there
  1300. * are no smaller extents available.
  1301. */
  1302. if (!i) {
  1303. error = xfs_alloc_ag_vextent_small(args, cnt_cur,
  1304. &fbno, &flen, &i);
  1305. if (error)
  1306. goto error0;
  1307. if (i == 0 || flen == 0) {
  1308. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1309. trace_xfs_alloc_size_noentry(args);
  1310. return 0;
  1311. }
  1312. ASSERT(i == 1);
  1313. busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
  1314. &rlen, &busy_gen);
  1315. } else {
  1316. /*
  1317. * Search for a non-busy extent that is large enough.
  1318. */
  1319. for (;;) {
  1320. error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
  1321. if (error)
  1322. goto error0;
  1323. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1324. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1325. &rbno, &rlen, &busy_gen);
  1326. if (rlen >= args->maxlen)
  1327. break;
  1328. error = xfs_btree_increment(cnt_cur, 0, &i);
  1329. if (error)
  1330. goto error0;
  1331. if (i == 0) {
  1332. /*
  1333. * Our only valid extents must have been busy.
  1334. * Make it unbusy by forcing the log out and
  1335. * retrying.
  1336. */
  1337. xfs_btree_del_cursor(cnt_cur,
  1338. XFS_BTREE_NOERROR);
  1339. trace_xfs_alloc_size_busy(args);
  1340. xfs_extent_busy_flush(args->mp,
  1341. args->pag, busy_gen);
  1342. goto restart;
  1343. }
  1344. }
  1345. }
  1346. /*
  1347. * In the first case above, we got the last entry in the
  1348. * by-size btree. Now we check to see if the space hits maxlen
  1349. * once aligned; if not, we search left for something better.
  1350. * This can't happen in the second case above.
  1351. */
  1352. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1353. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
  1354. (rlen <= flen && rbno + rlen <= fbno + flen), error0);
  1355. if (rlen < args->maxlen) {
  1356. xfs_agblock_t bestfbno;
  1357. xfs_extlen_t bestflen;
  1358. xfs_agblock_t bestrbno;
  1359. xfs_extlen_t bestrlen;
  1360. bestrlen = rlen;
  1361. bestrbno = rbno;
  1362. bestflen = flen;
  1363. bestfbno = fbno;
  1364. for (;;) {
  1365. if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
  1366. goto error0;
  1367. if (i == 0)
  1368. break;
  1369. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1370. &i)))
  1371. goto error0;
  1372. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1373. if (flen < bestrlen)
  1374. break;
  1375. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1376. &rbno, &rlen, &busy_gen);
  1377. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1378. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
  1379. (rlen <= flen && rbno + rlen <= fbno + flen),
  1380. error0);
  1381. if (rlen > bestrlen) {
  1382. bestrlen = rlen;
  1383. bestrbno = rbno;
  1384. bestflen = flen;
  1385. bestfbno = fbno;
  1386. if (rlen == args->maxlen)
  1387. break;
  1388. }
  1389. }
  1390. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1391. &i)))
  1392. goto error0;
  1393. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1394. rlen = bestrlen;
  1395. rbno = bestrbno;
  1396. flen = bestflen;
  1397. fbno = bestfbno;
  1398. }
  1399. args->wasfromfl = 0;
  1400. /*
  1401. * Fix up the length.
  1402. */
  1403. args->len = rlen;
  1404. if (rlen < args->minlen) {
  1405. if (busy) {
  1406. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1407. trace_xfs_alloc_size_busy(args);
  1408. xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
  1409. goto restart;
  1410. }
  1411. goto out_nominleft;
  1412. }
  1413. xfs_alloc_fix_len(args);
  1414. rlen = args->len;
  1415. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
  1416. /*
  1417. * Allocate and initialize a cursor for the by-block tree.
  1418. */
  1419. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1420. args->agno, XFS_BTNUM_BNO);
  1421. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1422. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1423. goto error0;
  1424. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1425. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1426. cnt_cur = bno_cur = NULL;
  1427. args->len = rlen;
  1428. args->agbno = rbno;
  1429. XFS_WANT_CORRUPTED_GOTO(args->mp,
  1430. args->agbno + args->len <=
  1431. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1432. error0);
  1433. trace_xfs_alloc_size_done(args);
  1434. return 0;
  1435. error0:
  1436. trace_xfs_alloc_size_error(args);
  1437. if (cnt_cur)
  1438. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1439. if (bno_cur)
  1440. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1441. return error;
  1442. out_nominleft:
  1443. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1444. trace_xfs_alloc_size_nominleft(args);
  1445. args->agbno = NULLAGBLOCK;
  1446. return 0;
  1447. }
  1448. /*
  1449. * Deal with the case where only small freespaces remain.
  1450. * Either return the contents of the last freespace record,
  1451. * or allocate space from the freelist if there is nothing in the tree.
  1452. */
  1453. STATIC int /* error */
  1454. xfs_alloc_ag_vextent_small(
  1455. xfs_alloc_arg_t *args, /* allocation argument structure */
  1456. xfs_btree_cur_t *ccur, /* by-size cursor */
  1457. xfs_agblock_t *fbnop, /* result block number */
  1458. xfs_extlen_t *flenp, /* result length */
  1459. int *stat) /* status: 0-freelist, 1-normal/none */
  1460. {
  1461. struct xfs_owner_info oinfo;
  1462. int error;
  1463. xfs_agblock_t fbno;
  1464. xfs_extlen_t flen;
  1465. int i;
  1466. if ((error = xfs_btree_decrement(ccur, 0, &i)))
  1467. goto error0;
  1468. if (i) {
  1469. if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
  1470. goto error0;
  1471. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1472. }
  1473. /*
  1474. * Nothing in the btree, try the freelist. Make sure
  1475. * to respect minleft even when pulling from the
  1476. * freelist.
  1477. */
  1478. else if (args->minlen == 1 && args->alignment == 1 &&
  1479. args->resv != XFS_AG_RESV_AGFL &&
  1480. (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
  1481. > args->minleft)) {
  1482. error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
  1483. if (error)
  1484. goto error0;
  1485. if (fbno != NULLAGBLOCK) {
  1486. xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
  1487. xfs_alloc_allow_busy_reuse(args->datatype));
  1488. if (xfs_alloc_is_userdata(args->datatype)) {
  1489. xfs_buf_t *bp;
  1490. bp = xfs_btree_get_bufs(args->mp, args->tp,
  1491. args->agno, fbno, 0);
  1492. if (!bp) {
  1493. error = -EFSCORRUPTED;
  1494. goto error0;
  1495. }
  1496. xfs_trans_binval(args->tp, bp);
  1497. }
  1498. args->len = 1;
  1499. args->agbno = fbno;
  1500. XFS_WANT_CORRUPTED_GOTO(args->mp,
  1501. args->agbno + args->len <=
  1502. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1503. error0);
  1504. args->wasfromfl = 1;
  1505. trace_xfs_alloc_small_freelist(args);
  1506. /*
  1507. * If we're feeding an AGFL block to something that
  1508. * doesn't live in the free space, we need to clear
  1509. * out the OWN_AG rmap.
  1510. */
  1511. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
  1512. error = xfs_rmap_free(args->tp, args->agbp, args->agno,
  1513. fbno, 1, &oinfo);
  1514. if (error)
  1515. goto error0;
  1516. *stat = 0;
  1517. return 0;
  1518. }
  1519. /*
  1520. * Nothing in the freelist.
  1521. */
  1522. else
  1523. flen = 0;
  1524. }
  1525. /*
  1526. * Can't allocate from the freelist for some reason.
  1527. */
  1528. else {
  1529. fbno = NULLAGBLOCK;
  1530. flen = 0;
  1531. }
  1532. /*
  1533. * Can't do the allocation, give up.
  1534. */
  1535. if (flen < args->minlen) {
  1536. args->agbno = NULLAGBLOCK;
  1537. trace_xfs_alloc_small_notenough(args);
  1538. flen = 0;
  1539. }
  1540. *fbnop = fbno;
  1541. *flenp = flen;
  1542. *stat = 1;
  1543. trace_xfs_alloc_small_done(args);
  1544. return 0;
  1545. error0:
  1546. trace_xfs_alloc_small_error(args);
  1547. return error;
  1548. }
  1549. /*
  1550. * Free the extent starting at agno/bno for length.
  1551. */
  1552. STATIC int
  1553. xfs_free_ag_extent(
  1554. xfs_trans_t *tp,
  1555. xfs_buf_t *agbp,
  1556. xfs_agnumber_t agno,
  1557. xfs_agblock_t bno,
  1558. xfs_extlen_t len,
  1559. struct xfs_owner_info *oinfo,
  1560. enum xfs_ag_resv_type type)
  1561. {
  1562. xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
  1563. xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
  1564. int error; /* error return value */
  1565. xfs_agblock_t gtbno; /* start of right neighbor block */
  1566. xfs_extlen_t gtlen; /* length of right neighbor block */
  1567. int haveleft; /* have a left neighbor block */
  1568. int haveright; /* have a right neighbor block */
  1569. int i; /* temp, result code */
  1570. xfs_agblock_t ltbno; /* start of left neighbor block */
  1571. xfs_extlen_t ltlen; /* length of left neighbor block */
  1572. xfs_mount_t *mp; /* mount point struct for filesystem */
  1573. xfs_agblock_t nbno; /* new starting block of freespace */
  1574. xfs_extlen_t nlen; /* new length of freespace */
  1575. xfs_perag_t *pag; /* per allocation group data */
  1576. bno_cur = cnt_cur = NULL;
  1577. mp = tp->t_mountp;
  1578. if (!xfs_rmap_should_skip_owner_update(oinfo)) {
  1579. error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
  1580. if (error)
  1581. goto error0;
  1582. }
  1583. /*
  1584. * Allocate and initialize a cursor for the by-block btree.
  1585. */
  1586. bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
  1587. /*
  1588. * Look for a neighboring block on the left (lower block numbers)
  1589. * that is contiguous with this space.
  1590. */
  1591. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1592. goto error0;
  1593. if (haveleft) {
  1594. /*
  1595. * There is a block to our left.
  1596. */
  1597. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1598. goto error0;
  1599. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1600. /*
  1601. * It's not contiguous, though.
  1602. */
  1603. if (ltbno + ltlen < bno)
  1604. haveleft = 0;
  1605. else {
  1606. /*
  1607. * If this failure happens the request to free this
  1608. * space was invalid, it's (partly) already free.
  1609. * Very bad.
  1610. */
  1611. XFS_WANT_CORRUPTED_GOTO(mp,
  1612. ltbno + ltlen <= bno, error0);
  1613. }
  1614. }
  1615. /*
  1616. * Look for a neighboring block on the right (higher block numbers)
  1617. * that is contiguous with this space.
  1618. */
  1619. if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
  1620. goto error0;
  1621. if (haveright) {
  1622. /*
  1623. * There is a block to our right.
  1624. */
  1625. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1626. goto error0;
  1627. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1628. /*
  1629. * It's not contiguous, though.
  1630. */
  1631. if (bno + len < gtbno)
  1632. haveright = 0;
  1633. else {
  1634. /*
  1635. * If this failure happens the request to free this
  1636. * space was invalid, it's (partly) already free.
  1637. * Very bad.
  1638. */
  1639. XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
  1640. }
  1641. }
  1642. /*
  1643. * Now allocate and initialize a cursor for the by-size tree.
  1644. */
  1645. cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
  1646. /*
  1647. * Have both left and right contiguous neighbors.
  1648. * Merge all three into a single free block.
  1649. */
  1650. if (haveleft && haveright) {
  1651. /*
  1652. * Delete the old by-size entry on the left.
  1653. */
  1654. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1655. goto error0;
  1656. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1657. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1658. goto error0;
  1659. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1660. /*
  1661. * Delete the old by-size entry on the right.
  1662. */
  1663. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1664. goto error0;
  1665. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1666. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1667. goto error0;
  1668. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1669. /*
  1670. * Delete the old by-block entry for the right block.
  1671. */
  1672. if ((error = xfs_btree_delete(bno_cur, &i)))
  1673. goto error0;
  1674. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1675. /*
  1676. * Move the by-block cursor back to the left neighbor.
  1677. */
  1678. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1679. goto error0;
  1680. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1681. #ifdef DEBUG
  1682. /*
  1683. * Check that this is the right record: delete didn't
  1684. * mangle the cursor.
  1685. */
  1686. {
  1687. xfs_agblock_t xxbno;
  1688. xfs_extlen_t xxlen;
  1689. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  1690. &i)))
  1691. goto error0;
  1692. XFS_WANT_CORRUPTED_GOTO(mp,
  1693. i == 1 && xxbno == ltbno && xxlen == ltlen,
  1694. error0);
  1695. }
  1696. #endif
  1697. /*
  1698. * Update remaining by-block entry to the new, joined block.
  1699. */
  1700. nbno = ltbno;
  1701. nlen = len + ltlen + gtlen;
  1702. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1703. goto error0;
  1704. }
  1705. /*
  1706. * Have only a left contiguous neighbor.
  1707. * Merge it together with the new freespace.
  1708. */
  1709. else if (haveleft) {
  1710. /*
  1711. * Delete the old by-size entry on the left.
  1712. */
  1713. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1714. goto error0;
  1715. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1716. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1717. goto error0;
  1718. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1719. /*
  1720. * Back up the by-block cursor to the left neighbor, and
  1721. * update its length.
  1722. */
  1723. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1724. goto error0;
  1725. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1726. nbno = ltbno;
  1727. nlen = len + ltlen;
  1728. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1729. goto error0;
  1730. }
  1731. /*
  1732. * Have only a right contiguous neighbor.
  1733. * Merge it together with the new freespace.
  1734. */
  1735. else if (haveright) {
  1736. /*
  1737. * Delete the old by-size entry on the right.
  1738. */
  1739. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1740. goto error0;
  1741. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1742. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1743. goto error0;
  1744. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1745. /*
  1746. * Update the starting block and length of the right
  1747. * neighbor in the by-block tree.
  1748. */
  1749. nbno = bno;
  1750. nlen = len + gtlen;
  1751. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1752. goto error0;
  1753. }
  1754. /*
  1755. * No contiguous neighbors.
  1756. * Insert the new freespace into the by-block tree.
  1757. */
  1758. else {
  1759. nbno = bno;
  1760. nlen = len;
  1761. if ((error = xfs_btree_insert(bno_cur, &i)))
  1762. goto error0;
  1763. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1764. }
  1765. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1766. bno_cur = NULL;
  1767. /*
  1768. * In all cases we need to insert the new freespace in the by-size tree.
  1769. */
  1770. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  1771. goto error0;
  1772. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
  1773. if ((error = xfs_btree_insert(cnt_cur, &i)))
  1774. goto error0;
  1775. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1776. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1777. cnt_cur = NULL;
  1778. /*
  1779. * Update the freespace totals in the ag and superblock.
  1780. */
  1781. pag = xfs_perag_get(mp, agno);
  1782. error = xfs_alloc_update_counters(tp, pag, agbp, len);
  1783. xfs_ag_resv_free_extent(pag, type, tp, len);
  1784. xfs_perag_put(pag);
  1785. if (error)
  1786. goto error0;
  1787. XFS_STATS_INC(mp, xs_freex);
  1788. XFS_STATS_ADD(mp, xs_freeb, len);
  1789. trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
  1790. return 0;
  1791. error0:
  1792. trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
  1793. if (bno_cur)
  1794. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1795. if (cnt_cur)
  1796. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1797. return error;
  1798. }
  1799. /*
  1800. * Visible (exported) allocation/free functions.
  1801. * Some of these are used just by xfs_alloc_btree.c and this file.
  1802. */
  1803. /*
  1804. * Compute and fill in value of m_ag_maxlevels.
  1805. */
  1806. void
  1807. xfs_alloc_compute_maxlevels(
  1808. xfs_mount_t *mp) /* file system mount structure */
  1809. {
  1810. mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
  1811. (mp->m_sb.sb_agblocks + 1) / 2);
  1812. }
  1813. /*
  1814. * Find the length of the longest extent in an AG. The 'need' parameter
  1815. * specifies how much space we're going to need for the AGFL and the
  1816. * 'reserved' parameter tells us how many blocks in this AG are reserved for
  1817. * other callers.
  1818. */
  1819. xfs_extlen_t
  1820. xfs_alloc_longest_free_extent(
  1821. struct xfs_perag *pag,
  1822. xfs_extlen_t need,
  1823. xfs_extlen_t reserved)
  1824. {
  1825. xfs_extlen_t delta = 0;
  1826. /*
  1827. * If the AGFL needs a recharge, we'll have to subtract that from the
  1828. * longest extent.
  1829. */
  1830. if (need > pag->pagf_flcount)
  1831. delta = need - pag->pagf_flcount;
  1832. /*
  1833. * If we cannot maintain others' reservations with space from the
  1834. * not-longest freesp extents, we'll have to subtract /that/ from
  1835. * the longest extent too.
  1836. */
  1837. if (pag->pagf_freeblks - pag->pagf_longest < reserved)
  1838. delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
  1839. /*
  1840. * If the longest extent is long enough to satisfy all the
  1841. * reservations and AGFL rules in place, we can return this extent.
  1842. */
  1843. if (pag->pagf_longest > delta)
  1844. return pag->pagf_longest - delta;
  1845. /* Otherwise, let the caller try for 1 block if there's space. */
  1846. return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
  1847. }
  1848. unsigned int
  1849. xfs_alloc_min_freelist(
  1850. struct xfs_mount *mp,
  1851. struct xfs_perag *pag)
  1852. {
  1853. unsigned int min_free;
  1854. /* space needed by-bno freespace btree */
  1855. min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
  1856. mp->m_ag_maxlevels);
  1857. /* space needed by-size freespace btree */
  1858. min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
  1859. mp->m_ag_maxlevels);
  1860. /* space needed reverse mapping used space btree */
  1861. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  1862. min_free += min_t(unsigned int,
  1863. pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
  1864. mp->m_rmap_maxlevels);
  1865. return min_free;
  1866. }
  1867. /*
  1868. * Check if the operation we are fixing up the freelist for should go ahead or
  1869. * not. If we are freeing blocks, we always allow it, otherwise the allocation
  1870. * is dependent on whether the size and shape of free space available will
  1871. * permit the requested allocation to take place.
  1872. */
  1873. static bool
  1874. xfs_alloc_space_available(
  1875. struct xfs_alloc_arg *args,
  1876. xfs_extlen_t min_free,
  1877. int flags)
  1878. {
  1879. struct xfs_perag *pag = args->pag;
  1880. xfs_extlen_t alloc_len, longest;
  1881. xfs_extlen_t reservation; /* blocks that are still reserved */
  1882. int available;
  1883. if (flags & XFS_ALLOC_FLAG_FREEING)
  1884. return true;
  1885. reservation = xfs_ag_resv_needed(pag, args->resv);
  1886. /* do we have enough contiguous free space for the allocation? */
  1887. alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
  1888. longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
  1889. if (longest < alloc_len)
  1890. return false;
  1891. /* do we have enough free space remaining for the allocation? */
  1892. available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
  1893. reservation - min_free - args->minleft);
  1894. if (available < (int)max(args->total, alloc_len))
  1895. return false;
  1896. /*
  1897. * Clamp maxlen to the amount of free space available for the actual
  1898. * extent allocation.
  1899. */
  1900. if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
  1901. args->maxlen = available;
  1902. ASSERT(args->maxlen > 0);
  1903. ASSERT(args->maxlen >= args->minlen);
  1904. }
  1905. return true;
  1906. }
  1907. int
  1908. xfs_free_agfl_block(
  1909. struct xfs_trans *tp,
  1910. xfs_agnumber_t agno,
  1911. xfs_agblock_t agbno,
  1912. struct xfs_buf *agbp,
  1913. struct xfs_owner_info *oinfo)
  1914. {
  1915. int error;
  1916. struct xfs_buf *bp;
  1917. error = xfs_free_ag_extent(tp, agbp, agno, agbno, 1, oinfo,
  1918. XFS_AG_RESV_AGFL);
  1919. if (error)
  1920. return error;
  1921. bp = xfs_btree_get_bufs(tp->t_mountp, tp, agno, agbno, 0);
  1922. if (!bp)
  1923. return -EFSCORRUPTED;
  1924. xfs_trans_binval(tp, bp);
  1925. return 0;
  1926. }
  1927. /*
  1928. * Check the agfl fields of the agf for inconsistency or corruption. The purpose
  1929. * is to detect an agfl header padding mismatch between current and early v5
  1930. * kernels. This problem manifests as a 1-slot size difference between the
  1931. * on-disk flcount and the active [first, last] range of a wrapped agfl. This
  1932. * may also catch variants of agfl count corruption unrelated to padding. Either
  1933. * way, we'll reset the agfl and warn the user.
  1934. *
  1935. * Return true if a reset is required before the agfl can be used, false
  1936. * otherwise.
  1937. */
  1938. static bool
  1939. xfs_agfl_needs_reset(
  1940. struct xfs_mount *mp,
  1941. struct xfs_agf *agf)
  1942. {
  1943. uint32_t f = be32_to_cpu(agf->agf_flfirst);
  1944. uint32_t l = be32_to_cpu(agf->agf_fllast);
  1945. uint32_t c = be32_to_cpu(agf->agf_flcount);
  1946. int agfl_size = xfs_agfl_size(mp);
  1947. int active;
  1948. /* no agfl header on v4 supers */
  1949. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1950. return false;
  1951. /*
  1952. * The agf read verifier catches severe corruption of these fields.
  1953. * Repeat some sanity checks to cover a packed -> unpacked mismatch if
  1954. * the verifier allows it.
  1955. */
  1956. if (f >= agfl_size || l >= agfl_size)
  1957. return true;
  1958. if (c > agfl_size)
  1959. return true;
  1960. /*
  1961. * Check consistency between the on-disk count and the active range. An
  1962. * agfl padding mismatch manifests as an inconsistent flcount.
  1963. */
  1964. if (c && l >= f)
  1965. active = l - f + 1;
  1966. else if (c)
  1967. active = agfl_size - f + l + 1;
  1968. else
  1969. active = 0;
  1970. return active != c;
  1971. }
  1972. /*
  1973. * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
  1974. * agfl content cannot be trusted. Warn the user that a repair is required to
  1975. * recover leaked blocks.
  1976. *
  1977. * The purpose of this mechanism is to handle filesystems affected by the agfl
  1978. * header padding mismatch problem. A reset keeps the filesystem online with a
  1979. * relatively minor free space accounting inconsistency rather than suffer the
  1980. * inevitable crash from use of an invalid agfl block.
  1981. */
  1982. static void
  1983. xfs_agfl_reset(
  1984. struct xfs_trans *tp,
  1985. struct xfs_buf *agbp,
  1986. struct xfs_perag *pag)
  1987. {
  1988. struct xfs_mount *mp = tp->t_mountp;
  1989. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  1990. ASSERT(pag->pagf_agflreset);
  1991. trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
  1992. xfs_warn(mp,
  1993. "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
  1994. "Please unmount and run xfs_repair.",
  1995. pag->pag_agno, pag->pagf_flcount);
  1996. agf->agf_flfirst = 0;
  1997. agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
  1998. agf->agf_flcount = 0;
  1999. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
  2000. XFS_AGF_FLCOUNT);
  2001. pag->pagf_flcount = 0;
  2002. pag->pagf_agflreset = false;
  2003. }
  2004. /*
  2005. * Defer an AGFL block free. This is effectively equivalent to
  2006. * xfs_bmap_add_free() with some special handling particular to AGFL blocks.
  2007. *
  2008. * Deferring AGFL frees helps prevent log reservation overruns due to too many
  2009. * allocation operations in a transaction. AGFL frees are prone to this problem
  2010. * because for one they are always freed one at a time. Further, an immediate
  2011. * AGFL block free can cause a btree join and require another block free before
  2012. * the real allocation can proceed. Deferring the free disconnects freeing up
  2013. * the AGFL slot from freeing the block.
  2014. */
  2015. STATIC void
  2016. xfs_defer_agfl_block(
  2017. struct xfs_trans *tp,
  2018. xfs_agnumber_t agno,
  2019. xfs_fsblock_t agbno,
  2020. struct xfs_owner_info *oinfo)
  2021. {
  2022. struct xfs_mount *mp = tp->t_mountp;
  2023. struct xfs_extent_free_item *new; /* new element */
  2024. ASSERT(xfs_bmap_free_item_zone != NULL);
  2025. ASSERT(oinfo != NULL);
  2026. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  2027. new->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
  2028. new->xefi_blockcount = 1;
  2029. new->xefi_oinfo = *oinfo;
  2030. trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
  2031. xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &new->xefi_list);
  2032. }
  2033. /*
  2034. * Decide whether to use this allocation group for this allocation.
  2035. * If so, fix up the btree freelist's size.
  2036. */
  2037. int /* error */
  2038. xfs_alloc_fix_freelist(
  2039. struct xfs_alloc_arg *args, /* allocation argument structure */
  2040. int flags) /* XFS_ALLOC_FLAG_... */
  2041. {
  2042. struct xfs_mount *mp = args->mp;
  2043. struct xfs_perag *pag = args->pag;
  2044. struct xfs_trans *tp = args->tp;
  2045. struct xfs_buf *agbp = NULL;
  2046. struct xfs_buf *agflbp = NULL;
  2047. struct xfs_alloc_arg targs; /* local allocation arguments */
  2048. xfs_agblock_t bno; /* freelist block */
  2049. xfs_extlen_t need; /* total blocks needed in freelist */
  2050. int error = 0;
  2051. if (!pag->pagf_init) {
  2052. error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
  2053. if (error)
  2054. goto out_no_agbp;
  2055. if (!pag->pagf_init) {
  2056. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  2057. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2058. goto out_agbp_relse;
  2059. }
  2060. }
  2061. /*
  2062. * If this is a metadata preferred pag and we are user data then try
  2063. * somewhere else if we are not being asked to try harder at this
  2064. * point
  2065. */
  2066. if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
  2067. (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
  2068. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2069. goto out_agbp_relse;
  2070. }
  2071. need = xfs_alloc_min_freelist(mp, pag);
  2072. if (!xfs_alloc_space_available(args, need, flags |
  2073. XFS_ALLOC_FLAG_CHECK))
  2074. goto out_agbp_relse;
  2075. /*
  2076. * Get the a.g. freespace buffer.
  2077. * Can fail if we're not blocking on locks, and it's held.
  2078. */
  2079. if (!agbp) {
  2080. error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
  2081. if (error)
  2082. goto out_no_agbp;
  2083. if (!agbp) {
  2084. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  2085. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2086. goto out_no_agbp;
  2087. }
  2088. }
  2089. /* reset a padding mismatched agfl before final free space check */
  2090. if (pag->pagf_agflreset)
  2091. xfs_agfl_reset(tp, agbp, pag);
  2092. /* If there isn't enough total space or single-extent, reject it. */
  2093. need = xfs_alloc_min_freelist(mp, pag);
  2094. if (!xfs_alloc_space_available(args, need, flags))
  2095. goto out_agbp_relse;
  2096. /*
  2097. * Make the freelist shorter if it's too long.
  2098. *
  2099. * Note that from this point onwards, we will always release the agf and
  2100. * agfl buffers on error. This handles the case where we error out and
  2101. * the buffers are clean or may not have been joined to the transaction
  2102. * and hence need to be released manually. If they have been joined to
  2103. * the transaction, then xfs_trans_brelse() will handle them
  2104. * appropriately based on the recursion count and dirty state of the
  2105. * buffer.
  2106. *
  2107. * XXX (dgc): When we have lots of free space, does this buy us
  2108. * anything other than extra overhead when we need to put more blocks
  2109. * back on the free list? Maybe we should only do this when space is
  2110. * getting low or the AGFL is more than half full?
  2111. *
  2112. * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
  2113. * big; the NORMAP flag prevents AGFL expand/shrink operations from
  2114. * updating the rmapbt. Both flags are used in xfs_repair while we're
  2115. * rebuilding the rmapbt, and neither are used by the kernel. They're
  2116. * both required to ensure that rmaps are correctly recorded for the
  2117. * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
  2118. * repair/rmap.c in xfsprogs for details.
  2119. */
  2120. memset(&targs, 0, sizeof(targs));
  2121. if (flags & XFS_ALLOC_FLAG_NORMAP)
  2122. xfs_rmap_skip_owner_update(&targs.oinfo);
  2123. else
  2124. xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
  2125. while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
  2126. error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
  2127. if (error)
  2128. goto out_agbp_relse;
  2129. /* defer agfl frees */
  2130. xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
  2131. }
  2132. targs.tp = tp;
  2133. targs.mp = mp;
  2134. targs.agbp = agbp;
  2135. targs.agno = args->agno;
  2136. targs.alignment = targs.minlen = targs.prod = 1;
  2137. targs.type = XFS_ALLOCTYPE_THIS_AG;
  2138. targs.pag = pag;
  2139. error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
  2140. if (error)
  2141. goto out_agbp_relse;
  2142. /* Make the freelist longer if it's too short. */
  2143. while (pag->pagf_flcount < need) {
  2144. targs.agbno = 0;
  2145. targs.maxlen = need - pag->pagf_flcount;
  2146. targs.resv = XFS_AG_RESV_AGFL;
  2147. /* Allocate as many blocks as possible at once. */
  2148. error = xfs_alloc_ag_vextent(&targs);
  2149. if (error)
  2150. goto out_agflbp_relse;
  2151. /*
  2152. * Stop if we run out. Won't happen if callers are obeying
  2153. * the restrictions correctly. Can happen for free calls
  2154. * on a completely full ag.
  2155. */
  2156. if (targs.agbno == NULLAGBLOCK) {
  2157. if (flags & XFS_ALLOC_FLAG_FREEING)
  2158. break;
  2159. goto out_agflbp_relse;
  2160. }
  2161. /*
  2162. * Put each allocated block on the list.
  2163. */
  2164. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  2165. error = xfs_alloc_put_freelist(tp, agbp,
  2166. agflbp, bno, 0);
  2167. if (error)
  2168. goto out_agflbp_relse;
  2169. }
  2170. }
  2171. xfs_trans_brelse(tp, agflbp);
  2172. args->agbp = agbp;
  2173. return 0;
  2174. out_agflbp_relse:
  2175. xfs_trans_brelse(tp, agflbp);
  2176. out_agbp_relse:
  2177. if (agbp)
  2178. xfs_trans_brelse(tp, agbp);
  2179. out_no_agbp:
  2180. args->agbp = NULL;
  2181. return error;
  2182. }
  2183. /*
  2184. * Get a block from the freelist.
  2185. * Returns with the buffer for the block gotten.
  2186. */
  2187. int /* error */
  2188. xfs_alloc_get_freelist(
  2189. xfs_trans_t *tp, /* transaction pointer */
  2190. xfs_buf_t *agbp, /* buffer containing the agf structure */
  2191. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  2192. int btreeblk) /* destination is a AGF btree */
  2193. {
  2194. xfs_agf_t *agf; /* a.g. freespace structure */
  2195. xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
  2196. xfs_agblock_t bno; /* block number returned */
  2197. __be32 *agfl_bno;
  2198. int error;
  2199. int logflags;
  2200. xfs_mount_t *mp = tp->t_mountp;
  2201. xfs_perag_t *pag; /* per allocation group data */
  2202. /*
  2203. * Freelist is empty, give up.
  2204. */
  2205. agf = XFS_BUF_TO_AGF(agbp);
  2206. if (!agf->agf_flcount) {
  2207. *bnop = NULLAGBLOCK;
  2208. return 0;
  2209. }
  2210. /*
  2211. * Read the array of free blocks.
  2212. */
  2213. error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
  2214. &agflbp);
  2215. if (error)
  2216. return error;
  2217. /*
  2218. * Get the block number and update the data structures.
  2219. */
  2220. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2221. bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
  2222. be32_add_cpu(&agf->agf_flfirst, 1);
  2223. xfs_trans_brelse(tp, agflbp);
  2224. if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
  2225. agf->agf_flfirst = 0;
  2226. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  2227. ASSERT(!pag->pagf_agflreset);
  2228. be32_add_cpu(&agf->agf_flcount, -1);
  2229. xfs_trans_agflist_delta(tp, -1);
  2230. pag->pagf_flcount--;
  2231. xfs_perag_put(pag);
  2232. logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
  2233. if (btreeblk) {
  2234. be32_add_cpu(&agf->agf_btreeblks, 1);
  2235. pag->pagf_btreeblks++;
  2236. logflags |= XFS_AGF_BTREEBLKS;
  2237. }
  2238. xfs_alloc_log_agf(tp, agbp, logflags);
  2239. *bnop = bno;
  2240. return 0;
  2241. }
  2242. /*
  2243. * Log the given fields from the agf structure.
  2244. */
  2245. void
  2246. xfs_alloc_log_agf(
  2247. xfs_trans_t *tp, /* transaction pointer */
  2248. xfs_buf_t *bp, /* buffer for a.g. freelist header */
  2249. int fields) /* mask of fields to be logged (XFS_AGF_...) */
  2250. {
  2251. int first; /* first byte offset */
  2252. int last; /* last byte offset */
  2253. static const short offsets[] = {
  2254. offsetof(xfs_agf_t, agf_magicnum),
  2255. offsetof(xfs_agf_t, agf_versionnum),
  2256. offsetof(xfs_agf_t, agf_seqno),
  2257. offsetof(xfs_agf_t, agf_length),
  2258. offsetof(xfs_agf_t, agf_roots[0]),
  2259. offsetof(xfs_agf_t, agf_levels[0]),
  2260. offsetof(xfs_agf_t, agf_flfirst),
  2261. offsetof(xfs_agf_t, agf_fllast),
  2262. offsetof(xfs_agf_t, agf_flcount),
  2263. offsetof(xfs_agf_t, agf_freeblks),
  2264. offsetof(xfs_agf_t, agf_longest),
  2265. offsetof(xfs_agf_t, agf_btreeblks),
  2266. offsetof(xfs_agf_t, agf_uuid),
  2267. offsetof(xfs_agf_t, agf_rmap_blocks),
  2268. offsetof(xfs_agf_t, agf_refcount_blocks),
  2269. offsetof(xfs_agf_t, agf_refcount_root),
  2270. offsetof(xfs_agf_t, agf_refcount_level),
  2271. /* needed so that we don't log the whole rest of the structure: */
  2272. offsetof(xfs_agf_t, agf_spare64),
  2273. sizeof(xfs_agf_t)
  2274. };
  2275. trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
  2276. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
  2277. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  2278. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  2279. }
  2280. /*
  2281. * Interface for inode allocation to force the pag data to be initialized.
  2282. */
  2283. int /* error */
  2284. xfs_alloc_pagf_init(
  2285. xfs_mount_t *mp, /* file system mount structure */
  2286. xfs_trans_t *tp, /* transaction pointer */
  2287. xfs_agnumber_t agno, /* allocation group number */
  2288. int flags) /* XFS_ALLOC_FLAGS_... */
  2289. {
  2290. xfs_buf_t *bp;
  2291. int error;
  2292. if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
  2293. return error;
  2294. if (bp)
  2295. xfs_trans_brelse(tp, bp);
  2296. return 0;
  2297. }
  2298. /*
  2299. * Put the block on the freelist for the allocation group.
  2300. */
  2301. int /* error */
  2302. xfs_alloc_put_freelist(
  2303. xfs_trans_t *tp, /* transaction pointer */
  2304. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  2305. xfs_buf_t *agflbp,/* buffer for a.g. free block array */
  2306. xfs_agblock_t bno, /* block being freed */
  2307. int btreeblk) /* block came from a AGF btree */
  2308. {
  2309. xfs_agf_t *agf; /* a.g. freespace structure */
  2310. __be32 *blockp;/* pointer to array entry */
  2311. int error;
  2312. int logflags;
  2313. xfs_mount_t *mp; /* mount structure */
  2314. xfs_perag_t *pag; /* per allocation group data */
  2315. __be32 *agfl_bno;
  2316. int startoff;
  2317. agf = XFS_BUF_TO_AGF(agbp);
  2318. mp = tp->t_mountp;
  2319. if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
  2320. be32_to_cpu(agf->agf_seqno), &agflbp)))
  2321. return error;
  2322. be32_add_cpu(&agf->agf_fllast, 1);
  2323. if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
  2324. agf->agf_fllast = 0;
  2325. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  2326. ASSERT(!pag->pagf_agflreset);
  2327. be32_add_cpu(&agf->agf_flcount, 1);
  2328. xfs_trans_agflist_delta(tp, 1);
  2329. pag->pagf_flcount++;
  2330. logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
  2331. if (btreeblk) {
  2332. be32_add_cpu(&agf->agf_btreeblks, -1);
  2333. pag->pagf_btreeblks--;
  2334. logflags |= XFS_AGF_BTREEBLKS;
  2335. }
  2336. xfs_perag_put(pag);
  2337. xfs_alloc_log_agf(tp, agbp, logflags);
  2338. ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
  2339. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2340. blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
  2341. *blockp = cpu_to_be32(bno);
  2342. startoff = (char *)blockp - (char *)agflbp->b_addr;
  2343. xfs_alloc_log_agf(tp, agbp, logflags);
  2344. xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
  2345. xfs_trans_log_buf(tp, agflbp, startoff,
  2346. startoff + sizeof(xfs_agblock_t) - 1);
  2347. return 0;
  2348. }
  2349. static xfs_failaddr_t
  2350. xfs_agf_verify(
  2351. struct xfs_buf *bp)
  2352. {
  2353. struct xfs_mount *mp = bp->b_target->bt_mount;
  2354. struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
  2355. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2356. if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
  2357. return __this_address;
  2358. if (!xfs_log_check_lsn(mp,
  2359. be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
  2360. return __this_address;
  2361. }
  2362. if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
  2363. XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
  2364. be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
  2365. be32_to_cpu(agf->agf_flfirst) < xfs_agfl_size(mp) &&
  2366. be32_to_cpu(agf->agf_fllast) < xfs_agfl_size(mp) &&
  2367. be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)))
  2368. return __this_address;
  2369. if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
  2370. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
  2371. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
  2372. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
  2373. return __this_address;
  2374. if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
  2375. (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
  2376. be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
  2377. return __this_address;
  2378. /*
  2379. * during growfs operations, the perag is not fully initialised,
  2380. * so we can't use it for any useful checking. growfs ensures we can't
  2381. * use it by using uncached buffers that don't have the perag attached
  2382. * so we can detect and avoid this problem.
  2383. */
  2384. if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
  2385. return __this_address;
  2386. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  2387. be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
  2388. return __this_address;
  2389. if (xfs_sb_version_hasreflink(&mp->m_sb) &&
  2390. (be32_to_cpu(agf->agf_refcount_level) < 1 ||
  2391. be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
  2392. return __this_address;
  2393. return NULL;
  2394. }
  2395. static void
  2396. xfs_agf_read_verify(
  2397. struct xfs_buf *bp)
  2398. {
  2399. struct xfs_mount *mp = bp->b_target->bt_mount;
  2400. xfs_failaddr_t fa;
  2401. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  2402. !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
  2403. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  2404. else {
  2405. fa = xfs_agf_verify(bp);
  2406. if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
  2407. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2408. }
  2409. }
  2410. static void
  2411. xfs_agf_write_verify(
  2412. struct xfs_buf *bp)
  2413. {
  2414. struct xfs_mount *mp = bp->b_target->bt_mount;
  2415. struct xfs_buf_log_item *bip = bp->b_log_item;
  2416. xfs_failaddr_t fa;
  2417. fa = xfs_agf_verify(bp);
  2418. if (fa) {
  2419. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2420. return;
  2421. }
  2422. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2423. return;
  2424. if (bip)
  2425. XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  2426. xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
  2427. }
  2428. const struct xfs_buf_ops xfs_agf_buf_ops = {
  2429. .name = "xfs_agf",
  2430. .verify_read = xfs_agf_read_verify,
  2431. .verify_write = xfs_agf_write_verify,
  2432. .verify_struct = xfs_agf_verify,
  2433. };
  2434. /*
  2435. * Read in the allocation group header (free/alloc section).
  2436. */
  2437. int /* error */
  2438. xfs_read_agf(
  2439. struct xfs_mount *mp, /* mount point structure */
  2440. struct xfs_trans *tp, /* transaction pointer */
  2441. xfs_agnumber_t agno, /* allocation group number */
  2442. int flags, /* XFS_BUF_ */
  2443. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2444. {
  2445. int error;
  2446. trace_xfs_read_agf(mp, agno);
  2447. ASSERT(agno != NULLAGNUMBER);
  2448. error = xfs_trans_read_buf(
  2449. mp, tp, mp->m_ddev_targp,
  2450. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  2451. XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
  2452. if (error)
  2453. return error;
  2454. if (!*bpp)
  2455. return 0;
  2456. ASSERT(!(*bpp)->b_error);
  2457. xfs_buf_set_ref(*bpp, XFS_AGF_REF);
  2458. return 0;
  2459. }
  2460. /*
  2461. * Read in the allocation group header (free/alloc section).
  2462. */
  2463. int /* error */
  2464. xfs_alloc_read_agf(
  2465. struct xfs_mount *mp, /* mount point structure */
  2466. struct xfs_trans *tp, /* transaction pointer */
  2467. xfs_agnumber_t agno, /* allocation group number */
  2468. int flags, /* XFS_ALLOC_FLAG_... */
  2469. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2470. {
  2471. struct xfs_agf *agf; /* ag freelist header */
  2472. struct xfs_perag *pag; /* per allocation group data */
  2473. int error;
  2474. trace_xfs_alloc_read_agf(mp, agno);
  2475. ASSERT(agno != NULLAGNUMBER);
  2476. error = xfs_read_agf(mp, tp, agno,
  2477. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
  2478. bpp);
  2479. if (error)
  2480. return error;
  2481. if (!*bpp)
  2482. return 0;
  2483. ASSERT(!(*bpp)->b_error);
  2484. agf = XFS_BUF_TO_AGF(*bpp);
  2485. pag = xfs_perag_get(mp, agno);
  2486. if (!pag->pagf_init) {
  2487. pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
  2488. pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
  2489. pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
  2490. pag->pagf_longest = be32_to_cpu(agf->agf_longest);
  2491. pag->pagf_levels[XFS_BTNUM_BNOi] =
  2492. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
  2493. pag->pagf_levels[XFS_BTNUM_CNTi] =
  2494. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
  2495. pag->pagf_levels[XFS_BTNUM_RMAPi] =
  2496. be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
  2497. pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
  2498. pag->pagf_init = 1;
  2499. pag->pagf_agflreset = xfs_agfl_needs_reset(mp, agf);
  2500. }
  2501. #ifdef DEBUG
  2502. else if (!XFS_FORCED_SHUTDOWN(mp)) {
  2503. ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
  2504. ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
  2505. ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
  2506. ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
  2507. ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
  2508. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
  2509. ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
  2510. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
  2511. }
  2512. #endif
  2513. xfs_perag_put(pag);
  2514. return 0;
  2515. }
  2516. /*
  2517. * Allocate an extent (variable-size).
  2518. * Depending on the allocation type, we either look in a single allocation
  2519. * group or loop over the allocation groups to find the result.
  2520. */
  2521. int /* error */
  2522. xfs_alloc_vextent(
  2523. struct xfs_alloc_arg *args) /* allocation argument structure */
  2524. {
  2525. xfs_agblock_t agsize; /* allocation group size */
  2526. int error;
  2527. int flags; /* XFS_ALLOC_FLAG_... locking flags */
  2528. struct xfs_mount *mp; /* mount structure pointer */
  2529. xfs_agnumber_t sagno; /* starting allocation group number */
  2530. xfs_alloctype_t type; /* input allocation type */
  2531. int bump_rotor = 0;
  2532. xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
  2533. mp = args->mp;
  2534. type = args->otype = args->type;
  2535. args->agbno = NULLAGBLOCK;
  2536. /*
  2537. * Just fix this up, for the case where the last a.g. is shorter
  2538. * (or there's only one a.g.) and the caller couldn't easily figure
  2539. * that out (xfs_bmap_alloc).
  2540. */
  2541. agsize = mp->m_sb.sb_agblocks;
  2542. if (args->maxlen > agsize)
  2543. args->maxlen = agsize;
  2544. if (args->alignment == 0)
  2545. args->alignment = 1;
  2546. ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
  2547. ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
  2548. ASSERT(args->minlen <= args->maxlen);
  2549. ASSERT(args->minlen <= agsize);
  2550. ASSERT(args->mod < args->prod);
  2551. if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
  2552. XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
  2553. args->minlen > args->maxlen || args->minlen > agsize ||
  2554. args->mod >= args->prod) {
  2555. args->fsbno = NULLFSBLOCK;
  2556. trace_xfs_alloc_vextent_badargs(args);
  2557. return 0;
  2558. }
  2559. switch (type) {
  2560. case XFS_ALLOCTYPE_THIS_AG:
  2561. case XFS_ALLOCTYPE_NEAR_BNO:
  2562. case XFS_ALLOCTYPE_THIS_BNO:
  2563. /*
  2564. * These three force us into a single a.g.
  2565. */
  2566. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2567. args->pag = xfs_perag_get(mp, args->agno);
  2568. error = xfs_alloc_fix_freelist(args, 0);
  2569. if (error) {
  2570. trace_xfs_alloc_vextent_nofix(args);
  2571. goto error0;
  2572. }
  2573. if (!args->agbp) {
  2574. trace_xfs_alloc_vextent_noagbp(args);
  2575. break;
  2576. }
  2577. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2578. if ((error = xfs_alloc_ag_vextent(args)))
  2579. goto error0;
  2580. break;
  2581. case XFS_ALLOCTYPE_START_BNO:
  2582. /*
  2583. * Try near allocation first, then anywhere-in-ag after
  2584. * the first a.g. fails.
  2585. */
  2586. if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
  2587. (mp->m_flags & XFS_MOUNT_32BITINODES)) {
  2588. args->fsbno = XFS_AGB_TO_FSB(mp,
  2589. ((mp->m_agfrotor / rotorstep) %
  2590. mp->m_sb.sb_agcount), 0);
  2591. bump_rotor = 1;
  2592. }
  2593. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2594. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2595. /* FALLTHROUGH */
  2596. case XFS_ALLOCTYPE_FIRST_AG:
  2597. /*
  2598. * Rotate through the allocation groups looking for a winner.
  2599. */
  2600. if (type == XFS_ALLOCTYPE_FIRST_AG) {
  2601. /*
  2602. * Start with allocation group given by bno.
  2603. */
  2604. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2605. args->type = XFS_ALLOCTYPE_THIS_AG;
  2606. sagno = 0;
  2607. flags = 0;
  2608. } else {
  2609. /*
  2610. * Start with the given allocation group.
  2611. */
  2612. args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2613. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2614. }
  2615. /*
  2616. * Loop over allocation groups twice; first time with
  2617. * trylock set, second time without.
  2618. */
  2619. for (;;) {
  2620. args->pag = xfs_perag_get(mp, args->agno);
  2621. error = xfs_alloc_fix_freelist(args, flags);
  2622. if (error) {
  2623. trace_xfs_alloc_vextent_nofix(args);
  2624. goto error0;
  2625. }
  2626. /*
  2627. * If we get a buffer back then the allocation will fly.
  2628. */
  2629. if (args->agbp) {
  2630. if ((error = xfs_alloc_ag_vextent(args)))
  2631. goto error0;
  2632. break;
  2633. }
  2634. trace_xfs_alloc_vextent_loopfailed(args);
  2635. /*
  2636. * Didn't work, figure out the next iteration.
  2637. */
  2638. if (args->agno == sagno &&
  2639. type == XFS_ALLOCTYPE_START_BNO)
  2640. args->type = XFS_ALLOCTYPE_THIS_AG;
  2641. /*
  2642. * For the first allocation, we can try any AG to get
  2643. * space. However, if we already have allocated a
  2644. * block, we don't want to try AGs whose number is below
  2645. * sagno. Otherwise, we may end up with out-of-order
  2646. * locking of AGF, which might cause deadlock.
  2647. */
  2648. if (++(args->agno) == mp->m_sb.sb_agcount) {
  2649. if (args->tp->t_firstblock != NULLFSBLOCK)
  2650. args->agno = sagno;
  2651. else
  2652. args->agno = 0;
  2653. }
  2654. /*
  2655. * Reached the starting a.g., must either be done
  2656. * or switch to non-trylock mode.
  2657. */
  2658. if (args->agno == sagno) {
  2659. if (flags == 0) {
  2660. args->agbno = NULLAGBLOCK;
  2661. trace_xfs_alloc_vextent_allfailed(args);
  2662. break;
  2663. }
  2664. flags = 0;
  2665. if (type == XFS_ALLOCTYPE_START_BNO) {
  2666. args->agbno = XFS_FSB_TO_AGBNO(mp,
  2667. args->fsbno);
  2668. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2669. }
  2670. }
  2671. xfs_perag_put(args->pag);
  2672. }
  2673. if (bump_rotor) {
  2674. if (args->agno == sagno)
  2675. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  2676. (mp->m_sb.sb_agcount * rotorstep);
  2677. else
  2678. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  2679. (mp->m_sb.sb_agcount * rotorstep);
  2680. }
  2681. break;
  2682. default:
  2683. ASSERT(0);
  2684. /* NOTREACHED */
  2685. }
  2686. if (args->agbno == NULLAGBLOCK)
  2687. args->fsbno = NULLFSBLOCK;
  2688. else {
  2689. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  2690. #ifdef DEBUG
  2691. ASSERT(args->len >= args->minlen);
  2692. ASSERT(args->len <= args->maxlen);
  2693. ASSERT(args->agbno % args->alignment == 0);
  2694. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
  2695. args->len);
  2696. #endif
  2697. /* Zero the extent if we were asked to do so */
  2698. if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
  2699. error = xfs_zero_extent(args->ip, args->fsbno, args->len);
  2700. if (error)
  2701. goto error0;
  2702. }
  2703. }
  2704. xfs_perag_put(args->pag);
  2705. return 0;
  2706. error0:
  2707. xfs_perag_put(args->pag);
  2708. return error;
  2709. }
  2710. /* Ensure that the freelist is at full capacity. */
  2711. int
  2712. xfs_free_extent_fix_freelist(
  2713. struct xfs_trans *tp,
  2714. xfs_agnumber_t agno,
  2715. struct xfs_buf **agbp)
  2716. {
  2717. struct xfs_alloc_arg args;
  2718. int error;
  2719. memset(&args, 0, sizeof(struct xfs_alloc_arg));
  2720. args.tp = tp;
  2721. args.mp = tp->t_mountp;
  2722. args.agno = agno;
  2723. /*
  2724. * validate that the block number is legal - the enables us to detect
  2725. * and handle a silent filesystem corruption rather than crashing.
  2726. */
  2727. if (args.agno >= args.mp->m_sb.sb_agcount)
  2728. return -EFSCORRUPTED;
  2729. args.pag = xfs_perag_get(args.mp, args.agno);
  2730. ASSERT(args.pag);
  2731. error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
  2732. if (error)
  2733. goto out;
  2734. *agbp = args.agbp;
  2735. out:
  2736. xfs_perag_put(args.pag);
  2737. return error;
  2738. }
  2739. /*
  2740. * Free an extent.
  2741. * Just break up the extent address and hand off to xfs_free_ag_extent
  2742. * after fixing up the freelist.
  2743. */
  2744. int /* error */
  2745. __xfs_free_extent(
  2746. struct xfs_trans *tp, /* transaction pointer */
  2747. xfs_fsblock_t bno, /* starting block number of extent */
  2748. xfs_extlen_t len, /* length of extent */
  2749. struct xfs_owner_info *oinfo, /* extent owner */
  2750. enum xfs_ag_resv_type type, /* block reservation type */
  2751. bool skip_discard)
  2752. {
  2753. struct xfs_mount *mp = tp->t_mountp;
  2754. struct xfs_buf *agbp;
  2755. xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
  2756. xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
  2757. int error;
  2758. unsigned int busy_flags = 0;
  2759. ASSERT(len != 0);
  2760. ASSERT(type != XFS_AG_RESV_AGFL);
  2761. if (XFS_TEST_ERROR(false, mp,
  2762. XFS_ERRTAG_FREE_EXTENT))
  2763. return -EIO;
  2764. error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
  2765. if (error)
  2766. return error;
  2767. XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
  2768. /* validate the extent size is legal now we have the agf locked */
  2769. XFS_WANT_CORRUPTED_GOTO(mp,
  2770. agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
  2771. err);
  2772. error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
  2773. if (error)
  2774. goto err;
  2775. if (skip_discard)
  2776. busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
  2777. xfs_extent_busy_insert(tp, agno, agbno, len, busy_flags);
  2778. return 0;
  2779. err:
  2780. xfs_trans_brelse(tp, agbp);
  2781. return error;
  2782. }
  2783. struct xfs_alloc_query_range_info {
  2784. xfs_alloc_query_range_fn fn;
  2785. void *priv;
  2786. };
  2787. /* Format btree record and pass to our callback. */
  2788. STATIC int
  2789. xfs_alloc_query_range_helper(
  2790. struct xfs_btree_cur *cur,
  2791. union xfs_btree_rec *rec,
  2792. void *priv)
  2793. {
  2794. struct xfs_alloc_query_range_info *query = priv;
  2795. struct xfs_alloc_rec_incore irec;
  2796. irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
  2797. irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
  2798. return query->fn(cur, &irec, query->priv);
  2799. }
  2800. /* Find all free space within a given range of blocks. */
  2801. int
  2802. xfs_alloc_query_range(
  2803. struct xfs_btree_cur *cur,
  2804. struct xfs_alloc_rec_incore *low_rec,
  2805. struct xfs_alloc_rec_incore *high_rec,
  2806. xfs_alloc_query_range_fn fn,
  2807. void *priv)
  2808. {
  2809. union xfs_btree_irec low_brec;
  2810. union xfs_btree_irec high_brec;
  2811. struct xfs_alloc_query_range_info query;
  2812. ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
  2813. low_brec.a = *low_rec;
  2814. high_brec.a = *high_rec;
  2815. query.priv = priv;
  2816. query.fn = fn;
  2817. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  2818. xfs_alloc_query_range_helper, &query);
  2819. }
  2820. /* Find all free space records. */
  2821. int
  2822. xfs_alloc_query_all(
  2823. struct xfs_btree_cur *cur,
  2824. xfs_alloc_query_range_fn fn,
  2825. void *priv)
  2826. {
  2827. struct xfs_alloc_query_range_info query;
  2828. ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
  2829. query.priv = priv;
  2830. query.fn = fn;
  2831. return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
  2832. }
  2833. /* Is there a record covering a given extent? */
  2834. int
  2835. xfs_alloc_has_record(
  2836. struct xfs_btree_cur *cur,
  2837. xfs_agblock_t bno,
  2838. xfs_extlen_t len,
  2839. bool *exists)
  2840. {
  2841. union xfs_btree_irec low;
  2842. union xfs_btree_irec high;
  2843. memset(&low, 0, sizeof(low));
  2844. low.a.ar_startblock = bno;
  2845. memset(&high, 0xFF, sizeof(high));
  2846. high.a.ar_startblock = bno + len - 1;
  2847. return xfs_btree_has_record(cur, &low, &high, exists);
  2848. }
  2849. /*
  2850. * Walk all the blocks in the AGFL. The @walk_fn can return any negative
  2851. * error code or XFS_BTREE_QUERY_RANGE_ABORT.
  2852. */
  2853. int
  2854. xfs_agfl_walk(
  2855. struct xfs_mount *mp,
  2856. struct xfs_agf *agf,
  2857. struct xfs_buf *agflbp,
  2858. xfs_agfl_walk_fn walk_fn,
  2859. void *priv)
  2860. {
  2861. __be32 *agfl_bno;
  2862. unsigned int i;
  2863. int error;
  2864. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2865. i = be32_to_cpu(agf->agf_flfirst);
  2866. /* Nothing to walk in an empty AGFL. */
  2867. if (agf->agf_flcount == cpu_to_be32(0))
  2868. return 0;
  2869. /* Otherwise, walk from first to last, wrapping as needed. */
  2870. for (;;) {
  2871. error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
  2872. if (error)
  2873. return error;
  2874. if (i == be32_to_cpu(agf->agf_fllast))
  2875. break;
  2876. if (++i == xfs_agfl_size(mp))
  2877. i = 0;
  2878. }
  2879. return 0;
  2880. }