xfs_alloc.c 86 KB

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