xfs_alloc.c 83 KB

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