xfs_alloc.c 76 KB

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