xfs_alloc.c 74 KB

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