xfs_alloc.c 80 KB

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