xfs_alloc.c 72 KB

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