xfs_alloc.c 75 KB

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