xfs_alloc.c 81 KB

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