xfs_alloc.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623
  1. /*
  2. * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. /*
  33. * Free space allocation for XFS.
  34. */
  35. #include "xfs.h"
  36. #include "xfs_macros.h"
  37. #include "xfs_types.h"
  38. #include "xfs_inum.h"
  39. #include "xfs_log.h"
  40. #include "xfs_trans.h"
  41. #include "xfs_sb.h"
  42. #include "xfs_ag.h"
  43. #include "xfs_dir.h"
  44. #include "xfs_dmapi.h"
  45. #include "xfs_mount.h"
  46. #include "xfs_alloc_btree.h"
  47. #include "xfs_bmap_btree.h"
  48. #include "xfs_ialloc_btree.h"
  49. #include "xfs_btree.h"
  50. #include "xfs_ialloc.h"
  51. #include "xfs_alloc.h"
  52. #include "xfs_bit.h"
  53. #include "xfs_error.h"
  54. #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
  55. #define XFSA_FIXUP_BNO_OK 1
  56. #define XFSA_FIXUP_CNT_OK 2
  57. STATIC int
  58. xfs_alloc_search_busy(xfs_trans_t *tp,
  59. xfs_agnumber_t agno,
  60. xfs_agblock_t bno,
  61. xfs_extlen_t len);
  62. #if defined(XFS_ALLOC_TRACE)
  63. ktrace_t *xfs_alloc_trace_buf;
  64. #define TRACE_ALLOC(s,a) \
  65. xfs_alloc_trace_alloc(fname, s, a, __LINE__)
  66. #define TRACE_FREE(s,a,b,x,f) \
  67. xfs_alloc_trace_free(fname, s, mp, a, b, x, f, __LINE__)
  68. #define TRACE_MODAGF(s,a,f) \
  69. xfs_alloc_trace_modagf(fname, s, mp, a, f, __LINE__)
  70. #define TRACE_BUSY(fname,s,ag,agb,l,sl,tp) \
  71. xfs_alloc_trace_busy(fname, s, mp, ag, agb, l, sl, tp, XFS_ALLOC_KTRACE_BUSY, __LINE__)
  72. #define TRACE_UNBUSY(fname,s,ag,sl,tp) \
  73. xfs_alloc_trace_busy(fname, s, mp, ag, -1, -1, sl, tp, XFS_ALLOC_KTRACE_UNBUSY, __LINE__)
  74. #define TRACE_BUSYSEARCH(fname,s,ag,agb,l,sl,tp) \
  75. xfs_alloc_trace_busy(fname, s, mp, ag, agb, l, sl, tp, XFS_ALLOC_KTRACE_BUSYSEARCH, __LINE__)
  76. #else
  77. #define TRACE_ALLOC(s,a)
  78. #define TRACE_FREE(s,a,b,x,f)
  79. #define TRACE_MODAGF(s,a,f)
  80. #define TRACE_BUSY(s,a,ag,agb,l,sl,tp)
  81. #define TRACE_UNBUSY(fname,s,ag,sl,tp)
  82. #define TRACE_BUSYSEARCH(fname,s,ag,agb,l,sl,tp)
  83. #endif /* XFS_ALLOC_TRACE */
  84. /*
  85. * Prototypes for per-ag allocation routines
  86. */
  87. STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
  88. STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
  89. STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
  90. STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
  91. xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
  92. /*
  93. * Internal functions.
  94. */
  95. /*
  96. * Compute aligned version of the found extent.
  97. * Takes alignment and min length into account.
  98. */
  99. STATIC int /* success (>= minlen) */
  100. xfs_alloc_compute_aligned(
  101. xfs_agblock_t foundbno, /* starting block in found extent */
  102. xfs_extlen_t foundlen, /* length in found extent */
  103. xfs_extlen_t alignment, /* alignment for allocation */
  104. xfs_extlen_t minlen, /* minimum length for allocation */
  105. xfs_agblock_t *resbno, /* result block number */
  106. xfs_extlen_t *reslen) /* result length */
  107. {
  108. xfs_agblock_t bno;
  109. xfs_extlen_t diff;
  110. xfs_extlen_t len;
  111. if (alignment > 1 && foundlen >= minlen) {
  112. bno = roundup(foundbno, alignment);
  113. diff = bno - foundbno;
  114. len = diff >= foundlen ? 0 : foundlen - diff;
  115. } else {
  116. bno = foundbno;
  117. len = foundlen;
  118. }
  119. *resbno = bno;
  120. *reslen = len;
  121. return len >= minlen;
  122. }
  123. /*
  124. * Compute best start block and diff for "near" allocations.
  125. * freelen >= wantlen already checked by caller.
  126. */
  127. STATIC xfs_extlen_t /* difference value (absolute) */
  128. xfs_alloc_compute_diff(
  129. xfs_agblock_t wantbno, /* target starting block */
  130. xfs_extlen_t wantlen, /* target length */
  131. xfs_extlen_t alignment, /* target alignment */
  132. xfs_agblock_t freebno, /* freespace's starting block */
  133. xfs_extlen_t freelen, /* freespace's length */
  134. xfs_agblock_t *newbnop) /* result: best start block from free */
  135. {
  136. xfs_agblock_t freeend; /* end of freespace extent */
  137. xfs_agblock_t newbno1; /* return block number */
  138. xfs_agblock_t newbno2; /* other new block number */
  139. xfs_extlen_t newlen1=0; /* length with newbno1 */
  140. xfs_extlen_t newlen2=0; /* length with newbno2 */
  141. xfs_agblock_t wantend; /* end of target extent */
  142. ASSERT(freelen >= wantlen);
  143. freeend = freebno + freelen;
  144. wantend = wantbno + wantlen;
  145. if (freebno >= wantbno) {
  146. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  147. newbno1 = NULLAGBLOCK;
  148. } else if (freeend >= wantend && alignment > 1) {
  149. newbno1 = roundup(wantbno, alignment);
  150. newbno2 = newbno1 - alignment;
  151. if (newbno1 >= freeend)
  152. newbno1 = NULLAGBLOCK;
  153. else
  154. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  155. if (newbno2 < freebno)
  156. newbno2 = NULLAGBLOCK;
  157. else
  158. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  159. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  160. if (newlen1 < newlen2 ||
  161. (newlen1 == newlen2 &&
  162. XFS_ABSDIFF(newbno1, wantbno) >
  163. XFS_ABSDIFF(newbno2, wantbno)))
  164. newbno1 = newbno2;
  165. } else if (newbno2 != NULLAGBLOCK)
  166. newbno1 = newbno2;
  167. } else if (freeend >= wantend) {
  168. newbno1 = wantbno;
  169. } else if (alignment > 1) {
  170. newbno1 = roundup(freeend - wantlen, alignment);
  171. if (newbno1 > freeend - wantlen &&
  172. newbno1 - alignment >= freebno)
  173. newbno1 -= alignment;
  174. else if (newbno1 >= freeend)
  175. newbno1 = NULLAGBLOCK;
  176. } else
  177. newbno1 = freeend - wantlen;
  178. *newbnop = newbno1;
  179. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  180. }
  181. /*
  182. * Fix up the length, based on mod and prod.
  183. * len should be k * prod + mod for some k.
  184. * If len is too small it is returned unchanged.
  185. * If len hits maxlen it is left alone.
  186. */
  187. STATIC void
  188. xfs_alloc_fix_len(
  189. xfs_alloc_arg_t *args) /* allocation argument structure */
  190. {
  191. xfs_extlen_t k;
  192. xfs_extlen_t rlen;
  193. ASSERT(args->mod < args->prod);
  194. rlen = args->len;
  195. ASSERT(rlen >= args->minlen);
  196. ASSERT(rlen <= args->maxlen);
  197. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  198. (args->mod == 0 && rlen < args->prod))
  199. return;
  200. k = rlen % args->prod;
  201. if (k == args->mod)
  202. return;
  203. if (k > args->mod) {
  204. if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
  205. return;
  206. } else {
  207. if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
  208. (int)args->minlen)
  209. return;
  210. }
  211. ASSERT(rlen >= args->minlen);
  212. ASSERT(rlen <= args->maxlen);
  213. args->len = rlen;
  214. }
  215. /*
  216. * Fix up length if there is too little space left in the a.g.
  217. * Return 1 if ok, 0 if too little, should give up.
  218. */
  219. STATIC int
  220. xfs_alloc_fix_minleft(
  221. xfs_alloc_arg_t *args) /* allocation argument structure */
  222. {
  223. xfs_agf_t *agf; /* a.g. freelist header */
  224. int diff; /* free space difference */
  225. if (args->minleft == 0)
  226. return 1;
  227. agf = XFS_BUF_TO_AGF(args->agbp);
  228. diff = INT_GET(agf->agf_freeblks, ARCH_CONVERT)
  229. + INT_GET(agf->agf_flcount, ARCH_CONVERT)
  230. - args->len - args->minleft;
  231. if (diff >= 0)
  232. return 1;
  233. args->len += diff; /* shrink the allocated space */
  234. if (args->len >= args->minlen)
  235. return 1;
  236. args->agbno = NULLAGBLOCK;
  237. return 0;
  238. }
  239. /*
  240. * Update the two btrees, logically removing from freespace the extent
  241. * starting at rbno, rlen blocks. The extent is contained within the
  242. * actual (current) free extent fbno for flen blocks.
  243. * Flags are passed in indicating whether the cursors are set to the
  244. * relevant records.
  245. */
  246. STATIC int /* error code */
  247. xfs_alloc_fixup_trees(
  248. xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
  249. xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
  250. xfs_agblock_t fbno, /* starting block of free extent */
  251. xfs_extlen_t flen, /* length of free extent */
  252. xfs_agblock_t rbno, /* starting block of returned extent */
  253. xfs_extlen_t rlen, /* length of returned extent */
  254. int flags) /* flags, XFSA_FIXUP_... */
  255. {
  256. int error; /* error code */
  257. int i; /* operation results */
  258. xfs_agblock_t nfbno1; /* first new free startblock */
  259. xfs_agblock_t nfbno2; /* second new free startblock */
  260. xfs_extlen_t nflen1=0; /* first new free length */
  261. xfs_extlen_t nflen2=0; /* second new free length */
  262. /*
  263. * Look up the record in the by-size tree if necessary.
  264. */
  265. if (flags & XFSA_FIXUP_CNT_OK) {
  266. #ifdef DEBUG
  267. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  268. return error;
  269. XFS_WANT_CORRUPTED_RETURN(
  270. i == 1 && nfbno1 == fbno && nflen1 == flen);
  271. #endif
  272. } else {
  273. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  274. return error;
  275. XFS_WANT_CORRUPTED_RETURN(i == 1);
  276. }
  277. /*
  278. * Look up the record in the by-block tree if necessary.
  279. */
  280. if (flags & XFSA_FIXUP_BNO_OK) {
  281. #ifdef DEBUG
  282. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  283. return error;
  284. XFS_WANT_CORRUPTED_RETURN(
  285. i == 1 && nfbno1 == fbno && nflen1 == flen);
  286. #endif
  287. } else {
  288. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  289. return error;
  290. XFS_WANT_CORRUPTED_RETURN(i == 1);
  291. }
  292. #ifdef DEBUG
  293. {
  294. xfs_alloc_block_t *bnoblock;
  295. xfs_alloc_block_t *cntblock;
  296. if (bno_cur->bc_nlevels == 1 &&
  297. cnt_cur->bc_nlevels == 1) {
  298. bnoblock = XFS_BUF_TO_ALLOC_BLOCK(bno_cur->bc_bufs[0]);
  299. cntblock = XFS_BUF_TO_ALLOC_BLOCK(cnt_cur->bc_bufs[0]);
  300. XFS_WANT_CORRUPTED_RETURN(
  301. INT_GET(bnoblock->bb_numrecs, ARCH_CONVERT) == INT_GET(cntblock->bb_numrecs, ARCH_CONVERT));
  302. }
  303. }
  304. #endif
  305. /*
  306. * Deal with all four cases: the allocated record is contained
  307. * within the freespace record, so we can have new freespace
  308. * at either (or both) end, or no freespace remaining.
  309. */
  310. if (rbno == fbno && rlen == flen)
  311. nfbno1 = nfbno2 = NULLAGBLOCK;
  312. else if (rbno == fbno) {
  313. nfbno1 = rbno + rlen;
  314. nflen1 = flen - rlen;
  315. nfbno2 = NULLAGBLOCK;
  316. } else if (rbno + rlen == fbno + flen) {
  317. nfbno1 = fbno;
  318. nflen1 = flen - rlen;
  319. nfbno2 = NULLAGBLOCK;
  320. } else {
  321. nfbno1 = fbno;
  322. nflen1 = rbno - fbno;
  323. nfbno2 = rbno + rlen;
  324. nflen2 = (fbno + flen) - nfbno2;
  325. }
  326. /*
  327. * Delete the entry from the by-size btree.
  328. */
  329. if ((error = xfs_alloc_delete(cnt_cur, &i)))
  330. return error;
  331. XFS_WANT_CORRUPTED_RETURN(i == 1);
  332. /*
  333. * Add new by-size btree entry(s).
  334. */
  335. if (nfbno1 != NULLAGBLOCK) {
  336. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  337. return error;
  338. XFS_WANT_CORRUPTED_RETURN(i == 0);
  339. if ((error = xfs_alloc_insert(cnt_cur, &i)))
  340. return error;
  341. XFS_WANT_CORRUPTED_RETURN(i == 1);
  342. }
  343. if (nfbno2 != NULLAGBLOCK) {
  344. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  345. return error;
  346. XFS_WANT_CORRUPTED_RETURN(i == 0);
  347. if ((error = xfs_alloc_insert(cnt_cur, &i)))
  348. return error;
  349. XFS_WANT_CORRUPTED_RETURN(i == 1);
  350. }
  351. /*
  352. * Fix up the by-block btree entry(s).
  353. */
  354. if (nfbno1 == NULLAGBLOCK) {
  355. /*
  356. * No remaining freespace, just delete the by-block tree entry.
  357. */
  358. if ((error = xfs_alloc_delete(bno_cur, &i)))
  359. return error;
  360. XFS_WANT_CORRUPTED_RETURN(i == 1);
  361. } else {
  362. /*
  363. * Update the by-block entry to start later|be shorter.
  364. */
  365. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  366. return error;
  367. }
  368. if (nfbno2 != NULLAGBLOCK) {
  369. /*
  370. * 2 resulting free entries, need to add one.
  371. */
  372. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  373. return error;
  374. XFS_WANT_CORRUPTED_RETURN(i == 0);
  375. if ((error = xfs_alloc_insert(bno_cur, &i)))
  376. return error;
  377. XFS_WANT_CORRUPTED_RETURN(i == 1);
  378. }
  379. return 0;
  380. }
  381. /*
  382. * Read in the allocation group free block array.
  383. */
  384. STATIC int /* error */
  385. xfs_alloc_read_agfl(
  386. xfs_mount_t *mp, /* mount point structure */
  387. xfs_trans_t *tp, /* transaction pointer */
  388. xfs_agnumber_t agno, /* allocation group number */
  389. xfs_buf_t **bpp) /* buffer for the ag free block array */
  390. {
  391. xfs_buf_t *bp; /* return value */
  392. int error;
  393. ASSERT(agno != NULLAGNUMBER);
  394. error = xfs_trans_read_buf(
  395. mp, tp, mp->m_ddev_targp,
  396. XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
  397. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  398. if (error)
  399. return error;
  400. ASSERT(bp);
  401. ASSERT(!XFS_BUF_GETERROR(bp));
  402. XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
  403. *bpp = bp;
  404. return 0;
  405. }
  406. #if defined(XFS_ALLOC_TRACE)
  407. /*
  408. * Add an allocation trace entry for an alloc call.
  409. */
  410. STATIC void
  411. xfs_alloc_trace_alloc(
  412. char *name, /* function tag string */
  413. char *str, /* additional string */
  414. xfs_alloc_arg_t *args, /* allocation argument structure */
  415. int line) /* source line number */
  416. {
  417. ktrace_enter(xfs_alloc_trace_buf,
  418. (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
  419. (void *)name,
  420. (void *)str,
  421. (void *)args->mp,
  422. (void *)(__psunsigned_t)args->agno,
  423. (void *)(__psunsigned_t)args->agbno,
  424. (void *)(__psunsigned_t)args->minlen,
  425. (void *)(__psunsigned_t)args->maxlen,
  426. (void *)(__psunsigned_t)args->mod,
  427. (void *)(__psunsigned_t)args->prod,
  428. (void *)(__psunsigned_t)args->minleft,
  429. (void *)(__psunsigned_t)args->total,
  430. (void *)(__psunsigned_t)args->alignment,
  431. (void *)(__psunsigned_t)args->len,
  432. (void *)((((__psint_t)args->type) << 16) |
  433. (__psint_t)args->otype),
  434. (void *)(__psint_t)((args->wasdel << 3) |
  435. (args->wasfromfl << 2) |
  436. (args->isfl << 1) |
  437. (args->userdata << 0)));
  438. }
  439. /*
  440. * Add an allocation trace entry for a free call.
  441. */
  442. STATIC void
  443. xfs_alloc_trace_free(
  444. char *name, /* function tag string */
  445. char *str, /* additional string */
  446. xfs_mount_t *mp, /* file system mount point */
  447. xfs_agnumber_t agno, /* allocation group number */
  448. xfs_agblock_t agbno, /* a.g. relative block number */
  449. xfs_extlen_t len, /* length of extent */
  450. int isfl, /* set if is freelist allocation/free */
  451. int line) /* source line number */
  452. {
  453. ktrace_enter(xfs_alloc_trace_buf,
  454. (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
  455. (void *)name,
  456. (void *)str,
  457. (void *)mp,
  458. (void *)(__psunsigned_t)agno,
  459. (void *)(__psunsigned_t)agbno,
  460. (void *)(__psunsigned_t)len,
  461. (void *)(__psint_t)isfl,
  462. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  463. }
  464. /*
  465. * Add an allocation trace entry for modifying an agf.
  466. */
  467. STATIC void
  468. xfs_alloc_trace_modagf(
  469. char *name, /* function tag string */
  470. char *str, /* additional string */
  471. xfs_mount_t *mp, /* file system mount point */
  472. xfs_agf_t *agf, /* new agf value */
  473. int flags, /* logging flags for agf */
  474. int line) /* source line number */
  475. {
  476. ktrace_enter(xfs_alloc_trace_buf,
  477. (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
  478. (void *)name,
  479. (void *)str,
  480. (void *)mp,
  481. (void *)(__psint_t)flags,
  482. (void *)(__psunsigned_t)INT_GET(agf->agf_seqno, ARCH_CONVERT),
  483. (void *)(__psunsigned_t)INT_GET(agf->agf_length, ARCH_CONVERT),
  484. (void *)(__psunsigned_t)INT_GET(agf->agf_roots[XFS_BTNUM_BNO],
  485. ARCH_CONVERT),
  486. (void *)(__psunsigned_t)INT_GET(agf->agf_roots[XFS_BTNUM_CNT],
  487. ARCH_CONVERT),
  488. (void *)(__psunsigned_t)INT_GET(agf->agf_levels[XFS_BTNUM_BNO],
  489. ARCH_CONVERT),
  490. (void *)(__psunsigned_t)INT_GET(agf->agf_levels[XFS_BTNUM_CNT],
  491. ARCH_CONVERT),
  492. (void *)(__psunsigned_t)INT_GET(agf->agf_flfirst, ARCH_CONVERT),
  493. (void *)(__psunsigned_t)INT_GET(agf->agf_fllast, ARCH_CONVERT),
  494. (void *)(__psunsigned_t)INT_GET(agf->agf_flcount, ARCH_CONVERT),
  495. (void *)(__psunsigned_t)INT_GET(agf->agf_freeblks, ARCH_CONVERT),
  496. (void *)(__psunsigned_t)INT_GET(agf->agf_longest, ARCH_CONVERT));
  497. }
  498. STATIC void
  499. xfs_alloc_trace_busy(
  500. char *name, /* function tag string */
  501. char *str, /* additional string */
  502. xfs_mount_t *mp, /* file system mount poing */
  503. xfs_agnumber_t agno, /* allocation group number */
  504. xfs_agblock_t agbno, /* a.g. relative block number */
  505. xfs_extlen_t len, /* length of extent */
  506. int slot, /* perag Busy slot */
  507. xfs_trans_t *tp,
  508. int trtype, /* type: add, delete, search */
  509. int line) /* source line number */
  510. {
  511. ktrace_enter(xfs_alloc_trace_buf,
  512. (void *)(__psint_t)(trtype | (line << 16)),
  513. (void *)name,
  514. (void *)str,
  515. (void *)mp,
  516. (void *)(__psunsigned_t)agno,
  517. (void *)(__psunsigned_t)agbno,
  518. (void *)(__psunsigned_t)len,
  519. (void *)(__psint_t)slot,
  520. (void *)tp,
  521. NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  522. }
  523. #endif /* XFS_ALLOC_TRACE */
  524. /*
  525. * Allocation group level functions.
  526. */
  527. /*
  528. * Allocate a variable extent in the allocation group agno.
  529. * Type and bno are used to determine where in the allocation group the
  530. * extent will start.
  531. * Extent's length (returned in *len) will be between minlen and maxlen,
  532. * and of the form k * prod + mod unless there's nothing that large.
  533. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  534. */
  535. STATIC int /* error */
  536. xfs_alloc_ag_vextent(
  537. xfs_alloc_arg_t *args) /* argument structure for allocation */
  538. {
  539. int error=0;
  540. #ifdef XFS_ALLOC_TRACE
  541. static char fname[] = "xfs_alloc_ag_vextent";
  542. #endif
  543. ASSERT(args->minlen > 0);
  544. ASSERT(args->maxlen > 0);
  545. ASSERT(args->minlen <= args->maxlen);
  546. ASSERT(args->mod < args->prod);
  547. ASSERT(args->alignment > 0);
  548. /*
  549. * Branch to correct routine based on the type.
  550. */
  551. args->wasfromfl = 0;
  552. switch (args->type) {
  553. case XFS_ALLOCTYPE_THIS_AG:
  554. error = xfs_alloc_ag_vextent_size(args);
  555. break;
  556. case XFS_ALLOCTYPE_NEAR_BNO:
  557. error = xfs_alloc_ag_vextent_near(args);
  558. break;
  559. case XFS_ALLOCTYPE_THIS_BNO:
  560. error = xfs_alloc_ag_vextent_exact(args);
  561. break;
  562. default:
  563. ASSERT(0);
  564. /* NOTREACHED */
  565. }
  566. if (error)
  567. return error;
  568. /*
  569. * If the allocation worked, need to change the agf structure
  570. * (and log it), and the superblock.
  571. */
  572. if (args->agbno != NULLAGBLOCK) {
  573. xfs_agf_t *agf; /* allocation group freelist header */
  574. #ifdef XFS_ALLOC_TRACE
  575. xfs_mount_t *mp = args->mp;
  576. #endif
  577. long slen = (long)args->len;
  578. ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
  579. ASSERT(!(args->wasfromfl) || !args->isfl);
  580. ASSERT(args->agbno % args->alignment == 0);
  581. if (!(args->wasfromfl)) {
  582. agf = XFS_BUF_TO_AGF(args->agbp);
  583. INT_MOD(agf->agf_freeblks, ARCH_CONVERT, -(args->len));
  584. xfs_trans_agblocks_delta(args->tp,
  585. -((long)(args->len)));
  586. args->pag->pagf_freeblks -= args->len;
  587. ASSERT(INT_GET(agf->agf_freeblks, ARCH_CONVERT)
  588. <= INT_GET(agf->agf_length, ARCH_CONVERT));
  589. TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
  590. xfs_alloc_log_agf(args->tp, args->agbp,
  591. XFS_AGF_FREEBLKS);
  592. /* search the busylist for these blocks */
  593. xfs_alloc_search_busy(args->tp, args->agno,
  594. args->agbno, args->len);
  595. }
  596. if (!args->isfl)
  597. xfs_trans_mod_sb(args->tp,
  598. args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
  599. XFS_TRANS_SB_FDBLOCKS, -slen);
  600. XFS_STATS_INC(xs_allocx);
  601. XFS_STATS_ADD(xs_allocb, args->len);
  602. }
  603. return 0;
  604. }
  605. /*
  606. * Allocate a variable extent at exactly agno/bno.
  607. * Extent's length (returned in *len) will be between minlen and maxlen,
  608. * and of the form k * prod + mod unless there's nothing that large.
  609. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  610. */
  611. STATIC int /* error */
  612. xfs_alloc_ag_vextent_exact(
  613. xfs_alloc_arg_t *args) /* allocation argument structure */
  614. {
  615. xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
  616. xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
  617. xfs_agblock_t end; /* end of allocated extent */
  618. int error;
  619. xfs_agblock_t fbno; /* start block of found extent */
  620. xfs_agblock_t fend; /* end block of found extent */
  621. xfs_extlen_t flen; /* length of found extent */
  622. #ifdef XFS_ALLOC_TRACE
  623. static char fname[] = "xfs_alloc_ag_vextent_exact";
  624. #endif
  625. int i; /* success/failure of operation */
  626. xfs_agblock_t maxend; /* end of maximal extent */
  627. xfs_agblock_t minend; /* end of minimal extent */
  628. xfs_extlen_t rlen; /* length of returned extent */
  629. ASSERT(args->alignment == 1);
  630. /*
  631. * Allocate/initialize a cursor for the by-number freespace btree.
  632. */
  633. bno_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
  634. args->agno, XFS_BTNUM_BNO, NULL, 0);
  635. /*
  636. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  637. * Look for the closest free block <= bno, it must contain bno
  638. * if any free block does.
  639. */
  640. if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
  641. goto error0;
  642. if (!i) {
  643. /*
  644. * Didn't find it, return null.
  645. */
  646. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  647. args->agbno = NULLAGBLOCK;
  648. return 0;
  649. }
  650. /*
  651. * Grab the freespace record.
  652. */
  653. if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
  654. goto error0;
  655. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  656. ASSERT(fbno <= args->agbno);
  657. minend = args->agbno + args->minlen;
  658. maxend = args->agbno + args->maxlen;
  659. fend = fbno + flen;
  660. /*
  661. * Give up if the freespace isn't long enough for the minimum request.
  662. */
  663. if (fend < minend) {
  664. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  665. args->agbno = NULLAGBLOCK;
  666. return 0;
  667. }
  668. /*
  669. * End of extent will be smaller of the freespace end and the
  670. * maximal requested end.
  671. */
  672. end = XFS_AGBLOCK_MIN(fend, maxend);
  673. /*
  674. * Fix the length according to mod and prod if given.
  675. */
  676. args->len = end - args->agbno;
  677. xfs_alloc_fix_len(args);
  678. if (!xfs_alloc_fix_minleft(args)) {
  679. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  680. return 0;
  681. }
  682. rlen = args->len;
  683. ASSERT(args->agbno + rlen <= fend);
  684. end = args->agbno + rlen;
  685. /*
  686. * We are allocating agbno for rlen [agbno .. end]
  687. * Allocate/initialize a cursor for the by-size btree.
  688. */
  689. cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
  690. args->agno, XFS_BTNUM_CNT, NULL, 0);
  691. ASSERT(args->agbno + args->len <=
  692. INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
  693. ARCH_CONVERT));
  694. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  695. args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
  696. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  697. goto error0;
  698. }
  699. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  700. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  701. TRACE_ALLOC("normal", args);
  702. args->wasfromfl = 0;
  703. return 0;
  704. error0:
  705. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  706. TRACE_ALLOC("error", args);
  707. return error;
  708. }
  709. /*
  710. * Allocate a variable extent near bno in the allocation group agno.
  711. * Extent's length (returned in len) will be between minlen and maxlen,
  712. * and of the form k * prod + mod unless there's nothing that large.
  713. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  714. */
  715. STATIC int /* error */
  716. xfs_alloc_ag_vextent_near(
  717. xfs_alloc_arg_t *args) /* allocation argument structure */
  718. {
  719. xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
  720. xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
  721. xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
  722. #ifdef XFS_ALLOC_TRACE
  723. static char fname[] = "xfs_alloc_ag_vextent_near";
  724. #endif
  725. xfs_agblock_t gtbno; /* start bno of right side entry */
  726. xfs_agblock_t gtbnoa; /* aligned ... */
  727. xfs_extlen_t gtdiff; /* difference to right side entry */
  728. xfs_extlen_t gtlen; /* length of right side entry */
  729. xfs_extlen_t gtlena; /* aligned ... */
  730. xfs_agblock_t gtnew; /* useful start bno of right side */
  731. int error; /* error code */
  732. int i; /* result code, temporary */
  733. int j; /* result code, temporary */
  734. xfs_agblock_t ltbno; /* start bno of left side entry */
  735. xfs_agblock_t ltbnoa; /* aligned ... */
  736. xfs_extlen_t ltdiff; /* difference to left side entry */
  737. /*REFERENCED*/
  738. xfs_agblock_t ltend; /* end bno of left side entry */
  739. xfs_extlen_t ltlen; /* length of left side entry */
  740. xfs_extlen_t ltlena; /* aligned ... */
  741. xfs_agblock_t ltnew; /* useful start bno of left side */
  742. xfs_extlen_t rlen; /* length of returned extent */
  743. #if defined(DEBUG) && defined(__KERNEL__)
  744. /*
  745. * Randomly don't execute the first algorithm.
  746. */
  747. int dofirst; /* set to do first algorithm */
  748. dofirst = random() & 1;
  749. #endif
  750. /*
  751. * Get a cursor for the by-size btree.
  752. */
  753. cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
  754. args->agno, XFS_BTNUM_CNT, NULL, 0);
  755. ltlen = 0;
  756. bno_cur_lt = bno_cur_gt = NULL;
  757. /*
  758. * See if there are any free extents as big as maxlen.
  759. */
  760. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
  761. goto error0;
  762. /*
  763. * If none, then pick up the last entry in the tree unless the
  764. * tree is empty.
  765. */
  766. if (!i) {
  767. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
  768. &ltlen, &i)))
  769. goto error0;
  770. if (i == 0 || ltlen == 0) {
  771. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  772. return 0;
  773. }
  774. ASSERT(i == 1);
  775. }
  776. args->wasfromfl = 0;
  777. /*
  778. * First algorithm.
  779. * If the requested extent is large wrt the freespaces available
  780. * in this a.g., then the cursor will be pointing to a btree entry
  781. * near the right edge of the tree. If it's in the last btree leaf
  782. * block, then we just examine all the entries in that block
  783. * that are big enough, and pick the best one.
  784. * This is written as a while loop so we can break out of it,
  785. * but we never loop back to the top.
  786. */
  787. while (xfs_btree_islastblock(cnt_cur, 0)) {
  788. xfs_extlen_t bdiff;
  789. int besti=0;
  790. xfs_extlen_t blen=0;
  791. xfs_agblock_t bnew=0;
  792. #if defined(DEBUG) && defined(__KERNEL__)
  793. if (!dofirst)
  794. break;
  795. #endif
  796. /*
  797. * Start from the entry that lookup found, sequence through
  798. * all larger free blocks. If we're actually pointing at a
  799. * record smaller than maxlen, go to the start of this block,
  800. * and skip all those smaller than minlen.
  801. */
  802. if (ltlen || args->alignment > 1) {
  803. cnt_cur->bc_ptrs[0] = 1;
  804. do {
  805. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
  806. &ltlen, &i)))
  807. goto error0;
  808. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  809. if (ltlen >= args->minlen)
  810. break;
  811. if ((error = xfs_alloc_increment(cnt_cur, 0, &i)))
  812. goto error0;
  813. } while (i);
  814. ASSERT(ltlen >= args->minlen);
  815. if (!i)
  816. break;
  817. }
  818. i = cnt_cur->bc_ptrs[0];
  819. for (j = 1, blen = 0, bdiff = 0;
  820. !error && j && (blen < args->maxlen || bdiff > 0);
  821. error = xfs_alloc_increment(cnt_cur, 0, &j)) {
  822. /*
  823. * For each entry, decide if it's better than
  824. * the previous best entry.
  825. */
  826. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  827. goto error0;
  828. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  829. if (!xfs_alloc_compute_aligned(ltbno, ltlen,
  830. args->alignment, args->minlen,
  831. &ltbnoa, &ltlena))
  832. continue;
  833. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  834. xfs_alloc_fix_len(args);
  835. ASSERT(args->len >= args->minlen);
  836. if (args->len < blen)
  837. continue;
  838. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  839. args->alignment, ltbno, ltlen, &ltnew);
  840. if (ltnew != NULLAGBLOCK &&
  841. (args->len > blen || ltdiff < bdiff)) {
  842. bdiff = ltdiff;
  843. bnew = ltnew;
  844. blen = args->len;
  845. besti = cnt_cur->bc_ptrs[0];
  846. }
  847. }
  848. /*
  849. * It didn't work. We COULD be in a case where
  850. * there's a good record somewhere, so try again.
  851. */
  852. if (blen == 0)
  853. break;
  854. /*
  855. * Point at the best entry, and retrieve it again.
  856. */
  857. cnt_cur->bc_ptrs[0] = besti;
  858. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  859. goto error0;
  860. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  861. ltend = ltbno + ltlen;
  862. ASSERT(ltend <= INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
  863. ARCH_CONVERT));
  864. args->len = blen;
  865. if (!xfs_alloc_fix_minleft(args)) {
  866. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  867. TRACE_ALLOC("nominleft", args);
  868. return 0;
  869. }
  870. blen = args->len;
  871. /*
  872. * We are allocating starting at bnew for blen blocks.
  873. */
  874. args->agbno = bnew;
  875. ASSERT(bnew >= ltbno);
  876. ASSERT(bnew + blen <= ltend);
  877. /*
  878. * Set up a cursor for the by-bno tree.
  879. */
  880. bno_cur_lt = xfs_btree_init_cursor(args->mp, args->tp,
  881. args->agbp, args->agno, XFS_BTNUM_BNO, NULL, 0);
  882. /*
  883. * Fix up the btree entries.
  884. */
  885. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
  886. ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
  887. goto error0;
  888. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  889. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  890. TRACE_ALLOC("first", args);
  891. return 0;
  892. }
  893. /*
  894. * Second algorithm.
  895. * Search in the by-bno tree to the left and to the right
  896. * simultaneously, until in each case we find a space big enough,
  897. * or run into the edge of the tree. When we run into the edge,
  898. * we deallocate that cursor.
  899. * If both searches succeed, we compare the two spaces and pick
  900. * the better one.
  901. * With alignment, it's possible for both to fail; the upper
  902. * level algorithm that picks allocation groups for allocations
  903. * is not supposed to do this.
  904. */
  905. /*
  906. * Allocate and initialize the cursor for the leftward search.
  907. */
  908. bno_cur_lt = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
  909. args->agno, XFS_BTNUM_BNO, NULL, 0);
  910. /*
  911. * Lookup <= bno to find the leftward search's starting point.
  912. */
  913. if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
  914. goto error0;
  915. if (!i) {
  916. /*
  917. * Didn't find anything; use this cursor for the rightward
  918. * search.
  919. */
  920. bno_cur_gt = bno_cur_lt;
  921. bno_cur_lt = NULL;
  922. }
  923. /*
  924. * Found something. Duplicate the cursor for the rightward search.
  925. */
  926. else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
  927. goto error0;
  928. /*
  929. * Increment the cursor, so we will point at the entry just right
  930. * of the leftward entry if any, or to the leftmost entry.
  931. */
  932. if ((error = xfs_alloc_increment(bno_cur_gt, 0, &i)))
  933. goto error0;
  934. if (!i) {
  935. /*
  936. * It failed, there are no rightward entries.
  937. */
  938. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
  939. bno_cur_gt = NULL;
  940. }
  941. /*
  942. * Loop going left with the leftward cursor, right with the
  943. * rightward cursor, until either both directions give up or
  944. * we find an entry at least as big as minlen.
  945. */
  946. do {
  947. if (bno_cur_lt) {
  948. if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
  949. goto error0;
  950. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  951. if (xfs_alloc_compute_aligned(ltbno, ltlen,
  952. args->alignment, args->minlen,
  953. &ltbnoa, &ltlena))
  954. break;
  955. if ((error = xfs_alloc_decrement(bno_cur_lt, 0, &i)))
  956. goto error0;
  957. if (!i) {
  958. xfs_btree_del_cursor(bno_cur_lt,
  959. XFS_BTREE_NOERROR);
  960. bno_cur_lt = NULL;
  961. }
  962. }
  963. if (bno_cur_gt) {
  964. if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
  965. goto error0;
  966. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  967. if (xfs_alloc_compute_aligned(gtbno, gtlen,
  968. args->alignment, args->minlen,
  969. &gtbnoa, &gtlena))
  970. break;
  971. if ((error = xfs_alloc_increment(bno_cur_gt, 0, &i)))
  972. goto error0;
  973. if (!i) {
  974. xfs_btree_del_cursor(bno_cur_gt,
  975. XFS_BTREE_NOERROR);
  976. bno_cur_gt = NULL;
  977. }
  978. }
  979. } while (bno_cur_lt || bno_cur_gt);
  980. /*
  981. * Got both cursors still active, need to find better entry.
  982. */
  983. if (bno_cur_lt && bno_cur_gt) {
  984. /*
  985. * Left side is long enough, look for a right side entry.
  986. */
  987. if (ltlena >= args->minlen) {
  988. /*
  989. * Fix up the length.
  990. */
  991. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  992. xfs_alloc_fix_len(args);
  993. rlen = args->len;
  994. ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
  995. args->alignment, ltbno, ltlen, &ltnew);
  996. /*
  997. * Not perfect.
  998. */
  999. if (ltdiff) {
  1000. /*
  1001. * Look until we find a better one, run out of
  1002. * space, or run off the end.
  1003. */
  1004. while (bno_cur_lt && bno_cur_gt) {
  1005. if ((error = xfs_alloc_get_rec(
  1006. bno_cur_gt, &gtbno,
  1007. &gtlen, &i)))
  1008. goto error0;
  1009. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1010. xfs_alloc_compute_aligned(gtbno, gtlen,
  1011. args->alignment, args->minlen,
  1012. &gtbnoa, &gtlena);
  1013. /*
  1014. * The left one is clearly better.
  1015. */
  1016. if (gtbnoa >= args->agbno + ltdiff) {
  1017. xfs_btree_del_cursor(
  1018. bno_cur_gt,
  1019. XFS_BTREE_NOERROR);
  1020. bno_cur_gt = NULL;
  1021. break;
  1022. }
  1023. /*
  1024. * If we reach a big enough entry,
  1025. * compare the two and pick the best.
  1026. */
  1027. if (gtlena >= args->minlen) {
  1028. args->len =
  1029. XFS_EXTLEN_MIN(gtlena,
  1030. args->maxlen);
  1031. xfs_alloc_fix_len(args);
  1032. rlen = args->len;
  1033. gtdiff = xfs_alloc_compute_diff(
  1034. args->agbno, rlen,
  1035. args->alignment,
  1036. gtbno, gtlen, &gtnew);
  1037. /*
  1038. * Right side is better.
  1039. */
  1040. if (gtdiff < ltdiff) {
  1041. xfs_btree_del_cursor(
  1042. bno_cur_lt,
  1043. XFS_BTREE_NOERROR);
  1044. bno_cur_lt = NULL;
  1045. }
  1046. /*
  1047. * Left side is better.
  1048. */
  1049. else {
  1050. xfs_btree_del_cursor(
  1051. bno_cur_gt,
  1052. XFS_BTREE_NOERROR);
  1053. bno_cur_gt = NULL;
  1054. }
  1055. break;
  1056. }
  1057. /*
  1058. * Fell off the right end.
  1059. */
  1060. if ((error = xfs_alloc_increment(
  1061. bno_cur_gt, 0, &i)))
  1062. goto error0;
  1063. if (!i) {
  1064. xfs_btree_del_cursor(
  1065. bno_cur_gt,
  1066. XFS_BTREE_NOERROR);
  1067. bno_cur_gt = NULL;
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. /*
  1073. * The left side is perfect, trash the right side.
  1074. */
  1075. else {
  1076. xfs_btree_del_cursor(bno_cur_gt,
  1077. XFS_BTREE_NOERROR);
  1078. bno_cur_gt = NULL;
  1079. }
  1080. }
  1081. /*
  1082. * It's the right side that was found first, look left.
  1083. */
  1084. else {
  1085. /*
  1086. * Fix up the length.
  1087. */
  1088. args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
  1089. xfs_alloc_fix_len(args);
  1090. rlen = args->len;
  1091. gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
  1092. args->alignment, gtbno, gtlen, &gtnew);
  1093. /*
  1094. * Right side entry isn't perfect.
  1095. */
  1096. if (gtdiff) {
  1097. /*
  1098. * Look until we find a better one, run out of
  1099. * space, or run off the end.
  1100. */
  1101. while (bno_cur_lt && bno_cur_gt) {
  1102. if ((error = xfs_alloc_get_rec(
  1103. bno_cur_lt, &ltbno,
  1104. &ltlen, &i)))
  1105. goto error0;
  1106. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1107. xfs_alloc_compute_aligned(ltbno, ltlen,
  1108. args->alignment, args->minlen,
  1109. &ltbnoa, &ltlena);
  1110. /*
  1111. * The right one is clearly better.
  1112. */
  1113. if (ltbnoa <= args->agbno - gtdiff) {
  1114. xfs_btree_del_cursor(
  1115. bno_cur_lt,
  1116. XFS_BTREE_NOERROR);
  1117. bno_cur_lt = NULL;
  1118. break;
  1119. }
  1120. /*
  1121. * If we reach a big enough entry,
  1122. * compare the two and pick the best.
  1123. */
  1124. if (ltlena >= args->minlen) {
  1125. args->len = XFS_EXTLEN_MIN(
  1126. ltlena, args->maxlen);
  1127. xfs_alloc_fix_len(args);
  1128. rlen = args->len;
  1129. ltdiff = xfs_alloc_compute_diff(
  1130. args->agbno, rlen,
  1131. args->alignment,
  1132. ltbno, ltlen, &ltnew);
  1133. /*
  1134. * Left side is better.
  1135. */
  1136. if (ltdiff < gtdiff) {
  1137. xfs_btree_del_cursor(
  1138. bno_cur_gt,
  1139. XFS_BTREE_NOERROR);
  1140. bno_cur_gt = NULL;
  1141. }
  1142. /*
  1143. * Right side is better.
  1144. */
  1145. else {
  1146. xfs_btree_del_cursor(
  1147. bno_cur_lt,
  1148. XFS_BTREE_NOERROR);
  1149. bno_cur_lt = NULL;
  1150. }
  1151. break;
  1152. }
  1153. /*
  1154. * Fell off the left end.
  1155. */
  1156. if ((error = xfs_alloc_decrement(
  1157. bno_cur_lt, 0, &i)))
  1158. goto error0;
  1159. if (!i) {
  1160. xfs_btree_del_cursor(bno_cur_lt,
  1161. XFS_BTREE_NOERROR);
  1162. bno_cur_lt = NULL;
  1163. break;
  1164. }
  1165. }
  1166. }
  1167. /*
  1168. * The right side is perfect, trash the left side.
  1169. */
  1170. else {
  1171. xfs_btree_del_cursor(bno_cur_lt,
  1172. XFS_BTREE_NOERROR);
  1173. bno_cur_lt = NULL;
  1174. }
  1175. }
  1176. }
  1177. /*
  1178. * If we couldn't get anything, give up.
  1179. */
  1180. if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
  1181. TRACE_ALLOC("neither", args);
  1182. args->agbno = NULLAGBLOCK;
  1183. return 0;
  1184. }
  1185. /*
  1186. * At this point we have selected a freespace entry, either to the
  1187. * left or to the right. If it's on the right, copy all the
  1188. * useful variables to the "left" set so we only have one
  1189. * copy of this code.
  1190. */
  1191. if (bno_cur_gt) {
  1192. bno_cur_lt = bno_cur_gt;
  1193. bno_cur_gt = NULL;
  1194. ltbno = gtbno;
  1195. ltbnoa = gtbnoa;
  1196. ltlen = gtlen;
  1197. ltlena = gtlena;
  1198. j = 1;
  1199. } else
  1200. j = 0;
  1201. /*
  1202. * Fix up the length and compute the useful address.
  1203. */
  1204. ltend = ltbno + ltlen;
  1205. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1206. xfs_alloc_fix_len(args);
  1207. if (!xfs_alloc_fix_minleft(args)) {
  1208. TRACE_ALLOC("nominleft", args);
  1209. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1210. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1211. return 0;
  1212. }
  1213. rlen = args->len;
  1214. (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
  1215. ltlen, &ltnew);
  1216. ASSERT(ltnew >= ltbno);
  1217. ASSERT(ltnew + rlen <= ltend);
  1218. ASSERT(ltnew + rlen <= INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
  1219. ARCH_CONVERT));
  1220. args->agbno = ltnew;
  1221. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
  1222. ltnew, rlen, XFSA_FIXUP_BNO_OK)))
  1223. goto error0;
  1224. TRACE_ALLOC(j ? "gt" : "lt", args);
  1225. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1226. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1227. return 0;
  1228. error0:
  1229. TRACE_ALLOC("error", args);
  1230. if (cnt_cur != NULL)
  1231. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1232. if (bno_cur_lt != NULL)
  1233. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
  1234. if (bno_cur_gt != NULL)
  1235. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
  1236. return error;
  1237. }
  1238. /*
  1239. * Allocate a variable extent anywhere in the allocation group agno.
  1240. * Extent's length (returned in len) will be between minlen and maxlen,
  1241. * and of the form k * prod + mod unless there's nothing that large.
  1242. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1243. */
  1244. STATIC int /* error */
  1245. xfs_alloc_ag_vextent_size(
  1246. xfs_alloc_arg_t *args) /* allocation argument structure */
  1247. {
  1248. xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
  1249. xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
  1250. int error; /* error result */
  1251. xfs_agblock_t fbno; /* start of found freespace */
  1252. xfs_extlen_t flen; /* length of found freespace */
  1253. #ifdef XFS_ALLOC_TRACE
  1254. static char fname[] = "xfs_alloc_ag_vextent_size";
  1255. #endif
  1256. int i; /* temp status variable */
  1257. xfs_agblock_t rbno; /* returned block number */
  1258. xfs_extlen_t rlen; /* length of returned extent */
  1259. /*
  1260. * Allocate and initialize a cursor for the by-size btree.
  1261. */
  1262. cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
  1263. args->agno, XFS_BTNUM_CNT, NULL, 0);
  1264. bno_cur = NULL;
  1265. /*
  1266. * Look for an entry >= maxlen+alignment-1 blocks.
  1267. */
  1268. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1269. args->maxlen + args->alignment - 1, &i)))
  1270. goto error0;
  1271. /*
  1272. * If none, then pick up the last entry in the tree unless the
  1273. * tree is empty.
  1274. */
  1275. if (!i) {
  1276. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
  1277. &flen, &i)))
  1278. goto error0;
  1279. if (i == 0 || flen == 0) {
  1280. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1281. TRACE_ALLOC("noentry", args);
  1282. return 0;
  1283. }
  1284. ASSERT(i == 1);
  1285. }
  1286. /*
  1287. * There's a freespace as big as maxlen+alignment-1, get it.
  1288. */
  1289. else {
  1290. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
  1291. goto error0;
  1292. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1293. }
  1294. /*
  1295. * In the first case above, we got the last entry in the
  1296. * by-size btree. Now we check to see if the space hits maxlen
  1297. * once aligned; if not, we search left for something better.
  1298. * This can't happen in the second case above.
  1299. */
  1300. xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
  1301. &rbno, &rlen);
  1302. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1303. XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
  1304. (rlen <= flen && rbno + rlen <= fbno + flen), error0);
  1305. if (rlen < args->maxlen) {
  1306. xfs_agblock_t bestfbno;
  1307. xfs_extlen_t bestflen;
  1308. xfs_agblock_t bestrbno;
  1309. xfs_extlen_t bestrlen;
  1310. bestrlen = rlen;
  1311. bestrbno = rbno;
  1312. bestflen = flen;
  1313. bestfbno = fbno;
  1314. for (;;) {
  1315. if ((error = xfs_alloc_decrement(cnt_cur, 0, &i)))
  1316. goto error0;
  1317. if (i == 0)
  1318. break;
  1319. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1320. &i)))
  1321. goto error0;
  1322. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1323. if (flen < bestrlen)
  1324. break;
  1325. xfs_alloc_compute_aligned(fbno, flen, args->alignment,
  1326. args->minlen, &rbno, &rlen);
  1327. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1328. XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
  1329. (rlen <= flen && rbno + rlen <= fbno + flen),
  1330. error0);
  1331. if (rlen > bestrlen) {
  1332. bestrlen = rlen;
  1333. bestrbno = rbno;
  1334. bestflen = flen;
  1335. bestfbno = fbno;
  1336. if (rlen == args->maxlen)
  1337. break;
  1338. }
  1339. }
  1340. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1341. &i)))
  1342. goto error0;
  1343. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1344. rlen = bestrlen;
  1345. rbno = bestrbno;
  1346. flen = bestflen;
  1347. fbno = bestfbno;
  1348. }
  1349. args->wasfromfl = 0;
  1350. /*
  1351. * Fix up the length.
  1352. */
  1353. args->len = rlen;
  1354. xfs_alloc_fix_len(args);
  1355. if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
  1356. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1357. TRACE_ALLOC("nominleft", args);
  1358. args->agbno = NULLAGBLOCK;
  1359. return 0;
  1360. }
  1361. rlen = args->len;
  1362. XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
  1363. /*
  1364. * Allocate and initialize a cursor for the by-block tree.
  1365. */
  1366. bno_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
  1367. args->agno, XFS_BTNUM_BNO, NULL, 0);
  1368. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1369. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1370. goto error0;
  1371. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1372. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1373. cnt_cur = bno_cur = NULL;
  1374. args->len = rlen;
  1375. args->agbno = rbno;
  1376. XFS_WANT_CORRUPTED_GOTO(
  1377. args->agbno + args->len <=
  1378. INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
  1379. ARCH_CONVERT),
  1380. error0);
  1381. TRACE_ALLOC("normal", args);
  1382. return 0;
  1383. error0:
  1384. TRACE_ALLOC("error", args);
  1385. if (cnt_cur)
  1386. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1387. if (bno_cur)
  1388. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1389. return error;
  1390. }
  1391. /*
  1392. * Deal with the case where only small freespaces remain.
  1393. * Either return the contents of the last freespace record,
  1394. * or allocate space from the freelist if there is nothing in the tree.
  1395. */
  1396. STATIC int /* error */
  1397. xfs_alloc_ag_vextent_small(
  1398. xfs_alloc_arg_t *args, /* allocation argument structure */
  1399. xfs_btree_cur_t *ccur, /* by-size cursor */
  1400. xfs_agblock_t *fbnop, /* result block number */
  1401. xfs_extlen_t *flenp, /* result length */
  1402. int *stat) /* status: 0-freelist, 1-normal/none */
  1403. {
  1404. int error;
  1405. xfs_agblock_t fbno;
  1406. xfs_extlen_t flen;
  1407. #ifdef XFS_ALLOC_TRACE
  1408. static char fname[] = "xfs_alloc_ag_vextent_small";
  1409. #endif
  1410. int i;
  1411. if ((error = xfs_alloc_decrement(ccur, 0, &i)))
  1412. goto error0;
  1413. if (i) {
  1414. if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
  1415. goto error0;
  1416. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1417. }
  1418. /*
  1419. * Nothing in the btree, try the freelist. Make sure
  1420. * to respect minleft even when pulling from the
  1421. * freelist.
  1422. */
  1423. else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
  1424. (INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_flcount,
  1425. ARCH_CONVERT) > args->minleft)) {
  1426. if ((error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno)))
  1427. goto error0;
  1428. if (fbno != NULLAGBLOCK) {
  1429. if (args->userdata) {
  1430. xfs_buf_t *bp;
  1431. bp = xfs_btree_get_bufs(args->mp, args->tp,
  1432. args->agno, fbno, 0);
  1433. xfs_trans_binval(args->tp, bp);
  1434. }
  1435. args->len = 1;
  1436. args->agbno = fbno;
  1437. XFS_WANT_CORRUPTED_GOTO(
  1438. args->agbno + args->len <=
  1439. INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
  1440. ARCH_CONVERT),
  1441. error0);
  1442. args->wasfromfl = 1;
  1443. TRACE_ALLOC("freelist", args);
  1444. *stat = 0;
  1445. return 0;
  1446. }
  1447. /*
  1448. * Nothing in the freelist.
  1449. */
  1450. else
  1451. flen = 0;
  1452. }
  1453. /*
  1454. * Can't allocate from the freelist for some reason.
  1455. */
  1456. else
  1457. flen = 0;
  1458. /*
  1459. * Can't do the allocation, give up.
  1460. */
  1461. if (flen < args->minlen) {
  1462. args->agbno = NULLAGBLOCK;
  1463. TRACE_ALLOC("notenough", args);
  1464. flen = 0;
  1465. }
  1466. *fbnop = fbno;
  1467. *flenp = flen;
  1468. *stat = 1;
  1469. TRACE_ALLOC("normal", args);
  1470. return 0;
  1471. error0:
  1472. TRACE_ALLOC("error", args);
  1473. return error;
  1474. }
  1475. /*
  1476. * Free the extent starting at agno/bno for length.
  1477. */
  1478. STATIC int /* error */
  1479. xfs_free_ag_extent(
  1480. xfs_trans_t *tp, /* transaction pointer */
  1481. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  1482. xfs_agnumber_t agno, /* allocation group number */
  1483. xfs_agblock_t bno, /* starting block number */
  1484. xfs_extlen_t len, /* length of extent */
  1485. int isfl) /* set if is freelist blocks - no sb acctg */
  1486. {
  1487. xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
  1488. xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
  1489. int error; /* error return value */
  1490. #ifdef XFS_ALLOC_TRACE
  1491. static char fname[] = "xfs_free_ag_extent";
  1492. #endif
  1493. xfs_agblock_t gtbno; /* start of right neighbor block */
  1494. xfs_extlen_t gtlen; /* length of right neighbor block */
  1495. int haveleft; /* have a left neighbor block */
  1496. int haveright; /* have a right neighbor block */
  1497. int i; /* temp, result code */
  1498. xfs_agblock_t ltbno; /* start of left neighbor block */
  1499. xfs_extlen_t ltlen; /* length of left neighbor block */
  1500. xfs_mount_t *mp; /* mount point struct for filesystem */
  1501. xfs_agblock_t nbno; /* new starting block of freespace */
  1502. xfs_extlen_t nlen; /* new length of freespace */
  1503. mp = tp->t_mountp;
  1504. /*
  1505. * Allocate and initialize a cursor for the by-block btree.
  1506. */
  1507. bno_cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO, NULL,
  1508. 0);
  1509. cnt_cur = NULL;
  1510. /*
  1511. * Look for a neighboring block on the left (lower block numbers)
  1512. * that is contiguous with this space.
  1513. */
  1514. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1515. goto error0;
  1516. if (haveleft) {
  1517. /*
  1518. * There is a block to our left.
  1519. */
  1520. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1521. goto error0;
  1522. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1523. /*
  1524. * It's not contiguous, though.
  1525. */
  1526. if (ltbno + ltlen < bno)
  1527. haveleft = 0;
  1528. else {
  1529. /*
  1530. * If this failure happens the request to free this
  1531. * space was invalid, it's (partly) already free.
  1532. * Very bad.
  1533. */
  1534. XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
  1535. }
  1536. }
  1537. /*
  1538. * Look for a neighboring block on the right (higher block numbers)
  1539. * that is contiguous with this space.
  1540. */
  1541. if ((error = xfs_alloc_increment(bno_cur, 0, &haveright)))
  1542. goto error0;
  1543. if (haveright) {
  1544. /*
  1545. * There is a block to our right.
  1546. */
  1547. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1548. goto error0;
  1549. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1550. /*
  1551. * It's not contiguous, though.
  1552. */
  1553. if (bno + len < gtbno)
  1554. haveright = 0;
  1555. else {
  1556. /*
  1557. * If this failure happens the request to free this
  1558. * space was invalid, it's (partly) already free.
  1559. * Very bad.
  1560. */
  1561. XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
  1562. }
  1563. }
  1564. /*
  1565. * Now allocate and initialize a cursor for the by-size tree.
  1566. */
  1567. cnt_cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT, NULL,
  1568. 0);
  1569. /*
  1570. * Have both left and right contiguous neighbors.
  1571. * Merge all three into a single free block.
  1572. */
  1573. if (haveleft && haveright) {
  1574. /*
  1575. * Delete the old by-size entry on the left.
  1576. */
  1577. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1578. goto error0;
  1579. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1580. if ((error = xfs_alloc_delete(cnt_cur, &i)))
  1581. goto error0;
  1582. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1583. /*
  1584. * Delete the old by-size entry on the right.
  1585. */
  1586. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1587. goto error0;
  1588. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1589. if ((error = xfs_alloc_delete(cnt_cur, &i)))
  1590. goto error0;
  1591. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1592. /*
  1593. * Delete the old by-block entry for the right block.
  1594. */
  1595. if ((error = xfs_alloc_delete(bno_cur, &i)))
  1596. goto error0;
  1597. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1598. /*
  1599. * Move the by-block cursor back to the left neighbor.
  1600. */
  1601. if ((error = xfs_alloc_decrement(bno_cur, 0, &i)))
  1602. goto error0;
  1603. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1604. #ifdef DEBUG
  1605. /*
  1606. * Check that this is the right record: delete didn't
  1607. * mangle the cursor.
  1608. */
  1609. {
  1610. xfs_agblock_t xxbno;
  1611. xfs_extlen_t xxlen;
  1612. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  1613. &i)))
  1614. goto error0;
  1615. XFS_WANT_CORRUPTED_GOTO(
  1616. i == 1 && xxbno == ltbno && xxlen == ltlen,
  1617. error0);
  1618. }
  1619. #endif
  1620. /*
  1621. * Update remaining by-block entry to the new, joined block.
  1622. */
  1623. nbno = ltbno;
  1624. nlen = len + ltlen + gtlen;
  1625. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1626. goto error0;
  1627. }
  1628. /*
  1629. * Have only a left contiguous neighbor.
  1630. * Merge it together with the new freespace.
  1631. */
  1632. else if (haveleft) {
  1633. /*
  1634. * Delete the old by-size entry on the left.
  1635. */
  1636. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1637. goto error0;
  1638. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1639. if ((error = xfs_alloc_delete(cnt_cur, &i)))
  1640. goto error0;
  1641. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1642. /*
  1643. * Back up the by-block cursor to the left neighbor, and
  1644. * update its length.
  1645. */
  1646. if ((error = xfs_alloc_decrement(bno_cur, 0, &i)))
  1647. goto error0;
  1648. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1649. nbno = ltbno;
  1650. nlen = len + ltlen;
  1651. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1652. goto error0;
  1653. }
  1654. /*
  1655. * Have only a right contiguous neighbor.
  1656. * Merge it together with the new freespace.
  1657. */
  1658. else if (haveright) {
  1659. /*
  1660. * Delete the old by-size entry on the right.
  1661. */
  1662. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1663. goto error0;
  1664. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1665. if ((error = xfs_alloc_delete(cnt_cur, &i)))
  1666. goto error0;
  1667. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1668. /*
  1669. * Update the starting block and length of the right
  1670. * neighbor in the by-block tree.
  1671. */
  1672. nbno = bno;
  1673. nlen = len + gtlen;
  1674. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1675. goto error0;
  1676. }
  1677. /*
  1678. * No contiguous neighbors.
  1679. * Insert the new freespace into the by-block tree.
  1680. */
  1681. else {
  1682. nbno = bno;
  1683. nlen = len;
  1684. if ((error = xfs_alloc_insert(bno_cur, &i)))
  1685. goto error0;
  1686. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1687. }
  1688. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1689. bno_cur = NULL;
  1690. /*
  1691. * In all cases we need to insert the new freespace in the by-size tree.
  1692. */
  1693. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  1694. goto error0;
  1695. XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
  1696. if ((error = xfs_alloc_insert(cnt_cur, &i)))
  1697. goto error0;
  1698. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1699. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1700. cnt_cur = NULL;
  1701. /*
  1702. * Update the freespace totals in the ag and superblock.
  1703. */
  1704. {
  1705. xfs_agf_t *agf;
  1706. xfs_perag_t *pag; /* per allocation group data */
  1707. agf = XFS_BUF_TO_AGF(agbp);
  1708. pag = &mp->m_perag[agno];
  1709. INT_MOD(agf->agf_freeblks, ARCH_CONVERT, len);
  1710. xfs_trans_agblocks_delta(tp, len);
  1711. pag->pagf_freeblks += len;
  1712. XFS_WANT_CORRUPTED_GOTO(
  1713. INT_GET(agf->agf_freeblks, ARCH_CONVERT)
  1714. <= INT_GET(agf->agf_length, ARCH_CONVERT),
  1715. error0);
  1716. TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
  1717. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  1718. if (!isfl)
  1719. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
  1720. XFS_STATS_INC(xs_freex);
  1721. XFS_STATS_ADD(xs_freeb, len);
  1722. }
  1723. TRACE_FREE(haveleft ?
  1724. (haveright ? "both" : "left") :
  1725. (haveright ? "right" : "none"),
  1726. agno, bno, len, isfl);
  1727. /*
  1728. * Since blocks move to the free list without the coordination
  1729. * used in xfs_bmap_finish, we can't allow block to be available
  1730. * for reallocation and non-transaction writing (user data)
  1731. * until we know that the transaction that moved it to the free
  1732. * list is permanently on disk. We track the blocks by declaring
  1733. * these blocks as "busy"; the busy list is maintained on a per-ag
  1734. * basis and each transaction records which entries should be removed
  1735. * when the iclog commits to disk. If a busy block is allocated,
  1736. * the iclog is pushed up to the LSN that freed the block.
  1737. */
  1738. xfs_alloc_mark_busy(tp, agno, bno, len);
  1739. return 0;
  1740. error0:
  1741. TRACE_FREE("error", agno, bno, len, isfl);
  1742. if (bno_cur)
  1743. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1744. if (cnt_cur)
  1745. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1746. return error;
  1747. }
  1748. /*
  1749. * Visible (exported) allocation/free functions.
  1750. * Some of these are used just by xfs_alloc_btree.c and this file.
  1751. */
  1752. /*
  1753. * Compute and fill in value of m_ag_maxlevels.
  1754. */
  1755. void
  1756. xfs_alloc_compute_maxlevels(
  1757. xfs_mount_t *mp) /* file system mount structure */
  1758. {
  1759. int level;
  1760. uint maxblocks;
  1761. uint maxleafents;
  1762. int minleafrecs;
  1763. int minnoderecs;
  1764. maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
  1765. minleafrecs = mp->m_alloc_mnr[0];
  1766. minnoderecs = mp->m_alloc_mnr[1];
  1767. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1768. for (level = 1; maxblocks > 1; level++)
  1769. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1770. mp->m_ag_maxlevels = level;
  1771. }
  1772. /*
  1773. * Decide whether to use this allocation group for this allocation.
  1774. * If so, fix up the btree freelist's size.
  1775. */
  1776. STATIC int /* error */
  1777. xfs_alloc_fix_freelist(
  1778. xfs_alloc_arg_t *args, /* allocation argument structure */
  1779. int flags) /* XFS_ALLOC_FLAG_... */
  1780. {
  1781. xfs_buf_t *agbp; /* agf buffer pointer */
  1782. xfs_agf_t *agf; /* a.g. freespace structure pointer */
  1783. xfs_buf_t *agflbp;/* agfl buffer pointer */
  1784. xfs_agblock_t bno; /* freelist block */
  1785. xfs_extlen_t delta; /* new blocks needed in freelist */
  1786. int error; /* error result code */
  1787. xfs_extlen_t longest;/* longest extent in allocation group */
  1788. xfs_mount_t *mp; /* file system mount point structure */
  1789. xfs_extlen_t need; /* total blocks needed in freelist */
  1790. xfs_perag_t *pag; /* per-ag information structure */
  1791. xfs_alloc_arg_t targs; /* local allocation arguments */
  1792. xfs_trans_t *tp; /* transaction pointer */
  1793. mp = args->mp;
  1794. pag = args->pag;
  1795. tp = args->tp;
  1796. if (!pag->pagf_init) {
  1797. if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
  1798. &agbp)))
  1799. return error;
  1800. if (!pag->pagf_init) {
  1801. args->agbp = NULL;
  1802. return 0;
  1803. }
  1804. } else
  1805. agbp = NULL;
  1806. /* If this is a metadata prefered pag and we are user data
  1807. * then try somewhere else if we are not being asked to
  1808. * try harder at this point
  1809. */
  1810. if (pag->pagf_metadata && args->userdata && flags) {
  1811. args->agbp = NULL;
  1812. return 0;
  1813. }
  1814. need = XFS_MIN_FREELIST_PAG(pag, mp);
  1815. delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
  1816. /*
  1817. * If it looks like there isn't a long enough extent, or enough
  1818. * total blocks, reject it.
  1819. */
  1820. longest = (pag->pagf_longest > delta) ?
  1821. (pag->pagf_longest - delta) :
  1822. (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
  1823. if (args->minlen + args->alignment + args->minalignslop - 1 > longest ||
  1824. (args->minleft &&
  1825. (int)(pag->pagf_freeblks + pag->pagf_flcount -
  1826. need - args->total) <
  1827. (int)args->minleft)) {
  1828. if (agbp)
  1829. xfs_trans_brelse(tp, agbp);
  1830. args->agbp = NULL;
  1831. return 0;
  1832. }
  1833. /*
  1834. * Get the a.g. freespace buffer.
  1835. * Can fail if we're not blocking on locks, and it's held.
  1836. */
  1837. if (agbp == NULL) {
  1838. if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
  1839. &agbp)))
  1840. return error;
  1841. if (agbp == NULL) {
  1842. args->agbp = NULL;
  1843. return 0;
  1844. }
  1845. }
  1846. /*
  1847. * Figure out how many blocks we should have in the freelist.
  1848. */
  1849. agf = XFS_BUF_TO_AGF(agbp);
  1850. need = XFS_MIN_FREELIST(agf, mp);
  1851. delta = need > INT_GET(agf->agf_flcount, ARCH_CONVERT) ?
  1852. (need - INT_GET(agf->agf_flcount, ARCH_CONVERT)) : 0;
  1853. /*
  1854. * If there isn't enough total or single-extent, reject it.
  1855. */
  1856. longest = INT_GET(agf->agf_longest, ARCH_CONVERT);
  1857. longest = (longest > delta) ? (longest - delta) :
  1858. (INT_GET(agf->agf_flcount, ARCH_CONVERT) > 0 || longest > 0);
  1859. if (args->minlen + args->alignment + args->minalignslop - 1 > longest ||
  1860. (args->minleft &&
  1861. (int)(INT_GET(agf->agf_freeblks, ARCH_CONVERT) +
  1862. INT_GET(agf->agf_flcount, ARCH_CONVERT) - need - args->total) <
  1863. (int)args->minleft)) {
  1864. xfs_trans_brelse(tp, agbp);
  1865. args->agbp = NULL;
  1866. return 0;
  1867. }
  1868. /*
  1869. * Make the freelist shorter if it's too long.
  1870. */
  1871. while (INT_GET(agf->agf_flcount, ARCH_CONVERT) > need) {
  1872. xfs_buf_t *bp;
  1873. if ((error = xfs_alloc_get_freelist(tp, agbp, &bno)))
  1874. return error;
  1875. if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
  1876. return error;
  1877. bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
  1878. xfs_trans_binval(tp, bp);
  1879. }
  1880. /*
  1881. * Initialize the args structure.
  1882. */
  1883. targs.tp = tp;
  1884. targs.mp = mp;
  1885. targs.agbp = agbp;
  1886. targs.agno = args->agno;
  1887. targs.mod = targs.minleft = targs.wasdel = targs.userdata =
  1888. targs.minalignslop = 0;
  1889. targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
  1890. targs.type = XFS_ALLOCTYPE_THIS_AG;
  1891. targs.pag = pag;
  1892. if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
  1893. return error;
  1894. /*
  1895. * Make the freelist longer if it's too short.
  1896. */
  1897. while (INT_GET(agf->agf_flcount, ARCH_CONVERT) < need) {
  1898. targs.agbno = 0;
  1899. targs.maxlen = need - INT_GET(agf->agf_flcount, ARCH_CONVERT);
  1900. /*
  1901. * Allocate as many blocks as possible at once.
  1902. */
  1903. if ((error = xfs_alloc_ag_vextent(&targs)))
  1904. return error;
  1905. /*
  1906. * Stop if we run out. Won't happen if callers are obeying
  1907. * the restrictions correctly. Can happen for free calls
  1908. * on a completely full ag.
  1909. */
  1910. if (targs.agbno == NULLAGBLOCK)
  1911. break;
  1912. /*
  1913. * Put each allocated block on the list.
  1914. */
  1915. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  1916. if ((error = xfs_alloc_put_freelist(tp, agbp, agflbp,
  1917. bno)))
  1918. return error;
  1919. }
  1920. }
  1921. args->agbp = agbp;
  1922. return 0;
  1923. }
  1924. /*
  1925. * Get a block from the freelist.
  1926. * Returns with the buffer for the block gotten.
  1927. */
  1928. int /* error */
  1929. xfs_alloc_get_freelist(
  1930. xfs_trans_t *tp, /* transaction pointer */
  1931. xfs_buf_t *agbp, /* buffer containing the agf structure */
  1932. xfs_agblock_t *bnop) /* block address retrieved from freelist */
  1933. {
  1934. xfs_agf_t *agf; /* a.g. freespace structure */
  1935. xfs_agfl_t *agfl; /* a.g. freelist structure */
  1936. xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
  1937. xfs_agblock_t bno; /* block number returned */
  1938. int error;
  1939. #ifdef XFS_ALLOC_TRACE
  1940. static char fname[] = "xfs_alloc_get_freelist";
  1941. #endif
  1942. xfs_mount_t *mp; /* mount structure */
  1943. xfs_perag_t *pag; /* per allocation group data */
  1944. agf = XFS_BUF_TO_AGF(agbp);
  1945. /*
  1946. * Freelist is empty, give up.
  1947. */
  1948. if (!agf->agf_flcount) {
  1949. *bnop = NULLAGBLOCK;
  1950. return 0;
  1951. }
  1952. /*
  1953. * Read the array of free blocks.
  1954. */
  1955. mp = tp->t_mountp;
  1956. if ((error = xfs_alloc_read_agfl(mp, tp,
  1957. INT_GET(agf->agf_seqno, ARCH_CONVERT), &agflbp)))
  1958. return error;
  1959. agfl = XFS_BUF_TO_AGFL(agflbp);
  1960. /*
  1961. * Get the block number and update the data structures.
  1962. */
  1963. bno = INT_GET(agfl->agfl_bno[INT_GET(agf->agf_flfirst, ARCH_CONVERT)], ARCH_CONVERT);
  1964. INT_MOD(agf->agf_flfirst, ARCH_CONVERT, 1);
  1965. xfs_trans_brelse(tp, agflbp);
  1966. if (INT_GET(agf->agf_flfirst, ARCH_CONVERT) == XFS_AGFL_SIZE(mp))
  1967. agf->agf_flfirst = 0;
  1968. pag = &mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)];
  1969. INT_MOD(agf->agf_flcount, ARCH_CONVERT, -1);
  1970. xfs_trans_agflist_delta(tp, -1);
  1971. pag->pagf_flcount--;
  1972. TRACE_MODAGF(NULL, agf, XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT);
  1973. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT);
  1974. *bnop = bno;
  1975. /*
  1976. * As blocks are freed, they are added to the per-ag busy list
  1977. * and remain there until the freeing transaction is committed to
  1978. * disk. Now that we have allocated blocks, this list must be
  1979. * searched to see if a block is being reused. If one is, then
  1980. * the freeing transaction must be pushed to disk NOW by forcing
  1981. * to disk all iclogs up that transaction's LSN.
  1982. */
  1983. xfs_alloc_search_busy(tp, INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
  1984. return 0;
  1985. }
  1986. /*
  1987. * Log the given fields from the agf structure.
  1988. */
  1989. void
  1990. xfs_alloc_log_agf(
  1991. xfs_trans_t *tp, /* transaction pointer */
  1992. xfs_buf_t *bp, /* buffer for a.g. freelist header */
  1993. int fields) /* mask of fields to be logged (XFS_AGF_...) */
  1994. {
  1995. int first; /* first byte offset */
  1996. int last; /* last byte offset */
  1997. static const short offsets[] = {
  1998. offsetof(xfs_agf_t, agf_magicnum),
  1999. offsetof(xfs_agf_t, agf_versionnum),
  2000. offsetof(xfs_agf_t, agf_seqno),
  2001. offsetof(xfs_agf_t, agf_length),
  2002. offsetof(xfs_agf_t, agf_roots[0]),
  2003. offsetof(xfs_agf_t, agf_levels[0]),
  2004. offsetof(xfs_agf_t, agf_flfirst),
  2005. offsetof(xfs_agf_t, agf_fllast),
  2006. offsetof(xfs_agf_t, agf_flcount),
  2007. offsetof(xfs_agf_t, agf_freeblks),
  2008. offsetof(xfs_agf_t, agf_longest),
  2009. sizeof(xfs_agf_t)
  2010. };
  2011. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  2012. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  2013. }
  2014. /*
  2015. * Interface for inode allocation to force the pag data to be initialized.
  2016. */
  2017. int /* error */
  2018. xfs_alloc_pagf_init(
  2019. xfs_mount_t *mp, /* file system mount structure */
  2020. xfs_trans_t *tp, /* transaction pointer */
  2021. xfs_agnumber_t agno, /* allocation group number */
  2022. int flags) /* XFS_ALLOC_FLAGS_... */
  2023. {
  2024. xfs_buf_t *bp;
  2025. int error;
  2026. if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
  2027. return error;
  2028. if (bp)
  2029. xfs_trans_brelse(tp, bp);
  2030. return 0;
  2031. }
  2032. /*
  2033. * Put the block on the freelist for the allocation group.
  2034. */
  2035. int /* error */
  2036. xfs_alloc_put_freelist(
  2037. xfs_trans_t *tp, /* transaction pointer */
  2038. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  2039. xfs_buf_t *agflbp,/* buffer for a.g. free block array */
  2040. xfs_agblock_t bno) /* block being freed */
  2041. {
  2042. xfs_agf_t *agf; /* a.g. freespace structure */
  2043. xfs_agfl_t *agfl; /* a.g. free block array */
  2044. xfs_agblock_t *blockp;/* pointer to array entry */
  2045. int error;
  2046. #ifdef XFS_ALLOC_TRACE
  2047. static char fname[] = "xfs_alloc_put_freelist";
  2048. #endif
  2049. xfs_mount_t *mp; /* mount structure */
  2050. xfs_perag_t *pag; /* per allocation group data */
  2051. agf = XFS_BUF_TO_AGF(agbp);
  2052. mp = tp->t_mountp;
  2053. if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
  2054. INT_GET(agf->agf_seqno, ARCH_CONVERT), &agflbp)))
  2055. return error;
  2056. agfl = XFS_BUF_TO_AGFL(agflbp);
  2057. INT_MOD(agf->agf_fllast, ARCH_CONVERT, 1);
  2058. if (INT_GET(agf->agf_fllast, ARCH_CONVERT) == XFS_AGFL_SIZE(mp))
  2059. agf->agf_fllast = 0;
  2060. pag = &mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)];
  2061. INT_MOD(agf->agf_flcount, ARCH_CONVERT, 1);
  2062. xfs_trans_agflist_delta(tp, 1);
  2063. pag->pagf_flcount++;
  2064. ASSERT(INT_GET(agf->agf_flcount, ARCH_CONVERT) <= XFS_AGFL_SIZE(mp));
  2065. blockp = &agfl->agfl_bno[INT_GET(agf->agf_fllast, ARCH_CONVERT)];
  2066. INT_SET(*blockp, ARCH_CONVERT, bno);
  2067. TRACE_MODAGF(NULL, agf, XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
  2068. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
  2069. xfs_trans_log_buf(tp, agflbp,
  2070. (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
  2071. (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
  2072. sizeof(xfs_agblock_t) - 1));
  2073. return 0;
  2074. }
  2075. /*
  2076. * Read in the allocation group header (free/alloc section).
  2077. */
  2078. int /* error */
  2079. xfs_alloc_read_agf(
  2080. xfs_mount_t *mp, /* mount point structure */
  2081. xfs_trans_t *tp, /* transaction pointer */
  2082. xfs_agnumber_t agno, /* allocation group number */
  2083. int flags, /* XFS_ALLOC_FLAG_... */
  2084. xfs_buf_t **bpp) /* buffer for the ag freelist header */
  2085. {
  2086. xfs_agf_t *agf; /* ag freelist header */
  2087. int agf_ok; /* set if agf is consistent */
  2088. xfs_buf_t *bp; /* return value */
  2089. xfs_perag_t *pag; /* per allocation group data */
  2090. int error;
  2091. ASSERT(agno != NULLAGNUMBER);
  2092. error = xfs_trans_read_buf(
  2093. mp, tp, mp->m_ddev_targp,
  2094. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  2095. XFS_FSS_TO_BB(mp, 1),
  2096. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0U,
  2097. &bp);
  2098. if (error)
  2099. return error;
  2100. ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  2101. if (!bp) {
  2102. *bpp = NULL;
  2103. return 0;
  2104. }
  2105. /*
  2106. * Validate the magic number of the agf block.
  2107. */
  2108. agf = XFS_BUF_TO_AGF(bp);
  2109. agf_ok =
  2110. INT_GET(agf->agf_magicnum, ARCH_CONVERT) == XFS_AGF_MAGIC &&
  2111. XFS_AGF_GOOD_VERSION(
  2112. INT_GET(agf->agf_versionnum, ARCH_CONVERT)) &&
  2113. INT_GET(agf->agf_freeblks, ARCH_CONVERT) <=
  2114. INT_GET(agf->agf_length, ARCH_CONVERT) &&
  2115. INT_GET(agf->agf_flfirst, ARCH_CONVERT) < XFS_AGFL_SIZE(mp) &&
  2116. INT_GET(agf->agf_fllast, ARCH_CONVERT) < XFS_AGFL_SIZE(mp) &&
  2117. INT_GET(agf->agf_flcount, ARCH_CONVERT) <= XFS_AGFL_SIZE(mp);
  2118. if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
  2119. XFS_RANDOM_ALLOC_READ_AGF))) {
  2120. XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
  2121. XFS_ERRLEVEL_LOW, mp, agf);
  2122. xfs_trans_brelse(tp, bp);
  2123. return XFS_ERROR(EFSCORRUPTED);
  2124. }
  2125. pag = &mp->m_perag[agno];
  2126. if (!pag->pagf_init) {
  2127. pag->pagf_freeblks = INT_GET(agf->agf_freeblks, ARCH_CONVERT);
  2128. pag->pagf_flcount = INT_GET(agf->agf_flcount, ARCH_CONVERT);
  2129. pag->pagf_longest = INT_GET(agf->agf_longest, ARCH_CONVERT);
  2130. pag->pagf_levels[XFS_BTNUM_BNOi] =
  2131. INT_GET(agf->agf_levels[XFS_BTNUM_BNOi], ARCH_CONVERT);
  2132. pag->pagf_levels[XFS_BTNUM_CNTi] =
  2133. INT_GET(agf->agf_levels[XFS_BTNUM_CNTi], ARCH_CONVERT);
  2134. spinlock_init(&pag->pagb_lock, "xfspagb");
  2135. pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
  2136. sizeof(xfs_perag_busy_t), KM_SLEEP);
  2137. pag->pagf_init = 1;
  2138. }
  2139. #ifdef DEBUG
  2140. else if (!XFS_FORCED_SHUTDOWN(mp)) {
  2141. ASSERT(pag->pagf_freeblks == INT_GET(agf->agf_freeblks, ARCH_CONVERT));
  2142. ASSERT(pag->pagf_flcount == INT_GET(agf->agf_flcount, ARCH_CONVERT));
  2143. ASSERT(pag->pagf_longest == INT_GET(agf->agf_longest, ARCH_CONVERT));
  2144. ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
  2145. INT_GET(agf->agf_levels[XFS_BTNUM_BNOi], ARCH_CONVERT));
  2146. ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
  2147. INT_GET(agf->agf_levels[XFS_BTNUM_CNTi], ARCH_CONVERT));
  2148. }
  2149. #endif
  2150. XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGF, XFS_AGF_REF);
  2151. *bpp = bp;
  2152. return 0;
  2153. }
  2154. /*
  2155. * Allocate an extent (variable-size).
  2156. * Depending on the allocation type, we either look in a single allocation
  2157. * group or loop over the allocation groups to find the result.
  2158. */
  2159. int /* error */
  2160. xfs_alloc_vextent(
  2161. xfs_alloc_arg_t *args) /* allocation argument structure */
  2162. {
  2163. xfs_agblock_t agsize; /* allocation group size */
  2164. int error;
  2165. int flags; /* XFS_ALLOC_FLAG_... locking flags */
  2166. #ifdef XFS_ALLOC_TRACE
  2167. static char fname[] = "xfs_alloc_vextent";
  2168. #endif
  2169. xfs_extlen_t minleft;/* minimum left value, temp copy */
  2170. xfs_mount_t *mp; /* mount structure pointer */
  2171. xfs_agnumber_t sagno; /* starting allocation group number */
  2172. xfs_alloctype_t type; /* input allocation type */
  2173. int bump_rotor = 0;
  2174. int no_min = 0;
  2175. xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
  2176. mp = args->mp;
  2177. type = args->otype = args->type;
  2178. args->agbno = NULLAGBLOCK;
  2179. /*
  2180. * Just fix this up, for the case where the last a.g. is shorter
  2181. * (or there's only one a.g.) and the caller couldn't easily figure
  2182. * that out (xfs_bmap_alloc).
  2183. */
  2184. agsize = mp->m_sb.sb_agblocks;
  2185. if (args->maxlen > agsize)
  2186. args->maxlen = agsize;
  2187. if (args->alignment == 0)
  2188. args->alignment = 1;
  2189. ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
  2190. ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
  2191. ASSERT(args->minlen <= args->maxlen);
  2192. ASSERT(args->minlen <= agsize);
  2193. ASSERT(args->mod < args->prod);
  2194. if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
  2195. XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
  2196. args->minlen > args->maxlen || args->minlen > agsize ||
  2197. args->mod >= args->prod) {
  2198. args->fsbno = NULLFSBLOCK;
  2199. TRACE_ALLOC("badargs", args);
  2200. return 0;
  2201. }
  2202. minleft = args->minleft;
  2203. switch (type) {
  2204. case XFS_ALLOCTYPE_THIS_AG:
  2205. case XFS_ALLOCTYPE_NEAR_BNO:
  2206. case XFS_ALLOCTYPE_THIS_BNO:
  2207. /*
  2208. * These three force us into a single a.g.
  2209. */
  2210. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2211. down_read(&mp->m_peraglock);
  2212. args->pag = &mp->m_perag[args->agno];
  2213. args->minleft = 0;
  2214. error = xfs_alloc_fix_freelist(args, 0);
  2215. args->minleft = minleft;
  2216. if (error) {
  2217. TRACE_ALLOC("nofix", args);
  2218. goto error0;
  2219. }
  2220. if (!args->agbp) {
  2221. up_read(&mp->m_peraglock);
  2222. TRACE_ALLOC("noagbp", args);
  2223. break;
  2224. }
  2225. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2226. if ((error = xfs_alloc_ag_vextent(args)))
  2227. goto error0;
  2228. up_read(&mp->m_peraglock);
  2229. break;
  2230. case XFS_ALLOCTYPE_START_BNO:
  2231. /*
  2232. * Try near allocation first, then anywhere-in-ag after
  2233. * the first a.g. fails.
  2234. */
  2235. if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
  2236. (mp->m_flags & XFS_MOUNT_32BITINODES)) {
  2237. args->fsbno = XFS_AGB_TO_FSB(mp,
  2238. ((mp->m_agfrotor / rotorstep) %
  2239. mp->m_sb.sb_agcount), 0);
  2240. bump_rotor = 1;
  2241. }
  2242. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2243. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2244. /* FALLTHROUGH */
  2245. case XFS_ALLOCTYPE_ANY_AG:
  2246. case XFS_ALLOCTYPE_START_AG:
  2247. case XFS_ALLOCTYPE_FIRST_AG:
  2248. /*
  2249. * Rotate through the allocation groups looking for a winner.
  2250. */
  2251. if (type == XFS_ALLOCTYPE_ANY_AG) {
  2252. /*
  2253. * Start with the last place we left off.
  2254. */
  2255. args->agno = sagno = (mp->m_agfrotor / rotorstep) %
  2256. mp->m_sb.sb_agcount;
  2257. args->type = XFS_ALLOCTYPE_THIS_AG;
  2258. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2259. } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
  2260. /*
  2261. * Start with allocation group given by bno.
  2262. */
  2263. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2264. args->type = XFS_ALLOCTYPE_THIS_AG;
  2265. sagno = 0;
  2266. flags = 0;
  2267. } else {
  2268. if (type == XFS_ALLOCTYPE_START_AG)
  2269. args->type = XFS_ALLOCTYPE_THIS_AG;
  2270. /*
  2271. * Start with the given allocation group.
  2272. */
  2273. args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2274. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2275. }
  2276. /*
  2277. * Loop over allocation groups twice; first time with
  2278. * trylock set, second time without.
  2279. */
  2280. down_read(&mp->m_peraglock);
  2281. for (;;) {
  2282. args->pag = &mp->m_perag[args->agno];
  2283. if (no_min) args->minleft = 0;
  2284. error = xfs_alloc_fix_freelist(args, flags);
  2285. args->minleft = minleft;
  2286. if (error) {
  2287. TRACE_ALLOC("nofix", args);
  2288. goto error0;
  2289. }
  2290. /*
  2291. * If we get a buffer back then the allocation will fly.
  2292. */
  2293. if (args->agbp) {
  2294. if ((error = xfs_alloc_ag_vextent(args)))
  2295. goto error0;
  2296. break;
  2297. }
  2298. TRACE_ALLOC("loopfailed", args);
  2299. /*
  2300. * Didn't work, figure out the next iteration.
  2301. */
  2302. if (args->agno == sagno &&
  2303. type == XFS_ALLOCTYPE_START_BNO)
  2304. args->type = XFS_ALLOCTYPE_THIS_AG;
  2305. if (++(args->agno) == mp->m_sb.sb_agcount)
  2306. args->agno = 0;
  2307. /*
  2308. * Reached the starting a.g., must either be done
  2309. * or switch to non-trylock mode.
  2310. */
  2311. if (args->agno == sagno) {
  2312. if (no_min == 1) {
  2313. args->agbno = NULLAGBLOCK;
  2314. TRACE_ALLOC("allfailed", args);
  2315. break;
  2316. }
  2317. if (flags == 0) {
  2318. no_min = 1;
  2319. } else {
  2320. flags = 0;
  2321. if (type == XFS_ALLOCTYPE_START_BNO) {
  2322. args->agbno = XFS_FSB_TO_AGBNO(mp,
  2323. args->fsbno);
  2324. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2325. }
  2326. }
  2327. }
  2328. }
  2329. up_read(&mp->m_peraglock);
  2330. if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
  2331. if (args->agno == sagno)
  2332. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  2333. (mp->m_sb.sb_agcount * rotorstep);
  2334. else
  2335. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  2336. (mp->m_sb.sb_agcount * rotorstep);
  2337. }
  2338. break;
  2339. default:
  2340. ASSERT(0);
  2341. /* NOTREACHED */
  2342. }
  2343. if (args->agbno == NULLAGBLOCK)
  2344. args->fsbno = NULLFSBLOCK;
  2345. else {
  2346. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  2347. #ifdef DEBUG
  2348. ASSERT(args->len >= args->minlen);
  2349. ASSERT(args->len <= args->maxlen);
  2350. ASSERT(args->agbno % args->alignment == 0);
  2351. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
  2352. args->len);
  2353. #endif
  2354. }
  2355. return 0;
  2356. error0:
  2357. up_read(&mp->m_peraglock);
  2358. return error;
  2359. }
  2360. /*
  2361. * Free an extent.
  2362. * Just break up the extent address and hand off to xfs_free_ag_extent
  2363. * after fixing up the freelist.
  2364. */
  2365. int /* error */
  2366. xfs_free_extent(
  2367. xfs_trans_t *tp, /* transaction pointer */
  2368. xfs_fsblock_t bno, /* starting block number of extent */
  2369. xfs_extlen_t len) /* length of extent */
  2370. {
  2371. #ifdef DEBUG
  2372. xfs_agf_t *agf; /* a.g. freespace header */
  2373. #endif
  2374. xfs_alloc_arg_t args; /* allocation argument structure */
  2375. int error;
  2376. ASSERT(len != 0);
  2377. args.tp = tp;
  2378. args.mp = tp->t_mountp;
  2379. args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
  2380. ASSERT(args.agno < args.mp->m_sb.sb_agcount);
  2381. args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
  2382. args.alignment = 1;
  2383. args.minlen = args.minleft = args.minalignslop = 0;
  2384. down_read(&args.mp->m_peraglock);
  2385. args.pag = &args.mp->m_perag[args.agno];
  2386. if ((error = xfs_alloc_fix_freelist(&args, 0)))
  2387. goto error0;
  2388. #ifdef DEBUG
  2389. ASSERT(args.agbp != NULL);
  2390. agf = XFS_BUF_TO_AGF(args.agbp);
  2391. ASSERT(args.agbno + len <= INT_GET(agf->agf_length, ARCH_CONVERT));
  2392. #endif
  2393. error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno,
  2394. len, 0);
  2395. error0:
  2396. up_read(&args.mp->m_peraglock);
  2397. return error;
  2398. }
  2399. /*
  2400. * AG Busy list management
  2401. * The busy list contains block ranges that have been freed but whose
  2402. * transacations have not yet hit disk. If any block listed in a busy
  2403. * list is reused, the transaction that freed it must be forced to disk
  2404. * before continuing to use the block.
  2405. *
  2406. * xfs_alloc_mark_busy - add to the per-ag busy list
  2407. * xfs_alloc_clear_busy - remove an item from the per-ag busy list
  2408. */
  2409. void
  2410. xfs_alloc_mark_busy(xfs_trans_t *tp,
  2411. xfs_agnumber_t agno,
  2412. xfs_agblock_t bno,
  2413. xfs_extlen_t len)
  2414. {
  2415. xfs_mount_t *mp;
  2416. xfs_perag_busy_t *bsy;
  2417. int n;
  2418. SPLDECL(s);
  2419. mp = tp->t_mountp;
  2420. s = mutex_spinlock(&mp->m_perag[agno].pagb_lock);
  2421. /* search pagb_list for an open slot */
  2422. for (bsy = mp->m_perag[agno].pagb_list, n = 0;
  2423. n < XFS_PAGB_NUM_SLOTS;
  2424. bsy++, n++) {
  2425. if (bsy->busy_tp == NULL) {
  2426. break;
  2427. }
  2428. }
  2429. if (n < XFS_PAGB_NUM_SLOTS) {
  2430. bsy = &mp->m_perag[agno].pagb_list[n];
  2431. mp->m_perag[agno].pagb_count++;
  2432. TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
  2433. bsy->busy_start = bno;
  2434. bsy->busy_length = len;
  2435. bsy->busy_tp = tp;
  2436. xfs_trans_add_busy(tp, agno, n);
  2437. } else {
  2438. TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
  2439. /*
  2440. * The busy list is full! Since it is now not possible to
  2441. * track the free block, make this a synchronous transaction
  2442. * to insure that the block is not reused before this
  2443. * transaction commits.
  2444. */
  2445. xfs_trans_set_sync(tp);
  2446. }
  2447. mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
  2448. }
  2449. void
  2450. xfs_alloc_clear_busy(xfs_trans_t *tp,
  2451. xfs_agnumber_t agno,
  2452. int idx)
  2453. {
  2454. xfs_mount_t *mp;
  2455. xfs_perag_busy_t *list;
  2456. SPLDECL(s);
  2457. mp = tp->t_mountp;
  2458. s = mutex_spinlock(&mp->m_perag[agno].pagb_lock);
  2459. list = mp->m_perag[agno].pagb_list;
  2460. ASSERT(idx < XFS_PAGB_NUM_SLOTS);
  2461. if (list[idx].busy_tp == tp) {
  2462. TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
  2463. list[idx].busy_tp = NULL;
  2464. mp->m_perag[agno].pagb_count--;
  2465. } else {
  2466. TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
  2467. }
  2468. mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
  2469. }
  2470. /*
  2471. * returns non-zero if any of (agno,bno):len is in a busy list
  2472. */
  2473. STATIC int
  2474. xfs_alloc_search_busy(xfs_trans_t *tp,
  2475. xfs_agnumber_t agno,
  2476. xfs_agblock_t bno,
  2477. xfs_extlen_t len)
  2478. {
  2479. xfs_mount_t *mp;
  2480. xfs_perag_busy_t *bsy;
  2481. int n;
  2482. xfs_agblock_t uend, bend;
  2483. xfs_lsn_t lsn;
  2484. int cnt;
  2485. SPLDECL(s);
  2486. mp = tp->t_mountp;
  2487. s = mutex_spinlock(&mp->m_perag[agno].pagb_lock);
  2488. cnt = mp->m_perag[agno].pagb_count;
  2489. uend = bno + len - 1;
  2490. /* search pagb_list for this slot, skipping open slots */
  2491. for (bsy = mp->m_perag[agno].pagb_list, n = 0;
  2492. cnt; bsy++, n++) {
  2493. /*
  2494. * (start1,length1) within (start2, length2)
  2495. */
  2496. if (bsy->busy_tp != NULL) {
  2497. bend = bsy->busy_start + bsy->busy_length - 1;
  2498. if ((bno > bend) ||
  2499. (uend < bsy->busy_start)) {
  2500. cnt--;
  2501. } else {
  2502. TRACE_BUSYSEARCH("xfs_alloc_search_busy",
  2503. "found1", agno, bno, len, n,
  2504. tp);
  2505. break;
  2506. }
  2507. }
  2508. }
  2509. /*
  2510. * If a block was found, force the log through the LSN of the
  2511. * transaction that freed the block
  2512. */
  2513. if (cnt) {
  2514. TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, n, tp);
  2515. lsn = bsy->busy_tp->t_commit_lsn;
  2516. mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
  2517. xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
  2518. } else {
  2519. TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, n, tp);
  2520. n = -1;
  2521. mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
  2522. }
  2523. return n;
  2524. }