xfs_alloc.c 82 KB

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