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