xfs_alloc.c 81 KB

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