xfs_alloc.c 82 KB

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