xfs_alloc.c 83 KB

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