xfs_alloc.c 69 KB

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