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