xfs_alloc.c 82 KB

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