xfs_alloc.c 74 KB

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