xfs_alloc.c 74 KB

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