xfs_alloc.c 82 KB

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