xfs_alloc.c 73 KB

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