xfs_alloc.c 88 KB

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