xfs_alloc.c 72 KB

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