xfs_alloc.c 72 KB

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