xfs_alloc.c 72 KB

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