xfs_alloc.c 74 KB

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