xfs_alloc.c 72 KB

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