xfs_alloc.c 82 KB

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