xfs_alloc.c 82 KB

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