xfs_alloc.c 83 KB

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