xfs_alloc.c 81 KB

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