xfs_alloc.c 83 KB

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