xfs_alloc.c 72 KB

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