xfs_alloc.c 69 KB

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