xfs_alloc.c 79 KB

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