xfs_alloc.c 82 KB

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