xfs_alloc.c 74 KB

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