xfs_alloc.c 72 KB

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