xfs_alloc.c 75 KB

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