xfs_alloc.c 81 KB

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