xfs_rtalloc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_alloc.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_bmap_btree.h"
  18. #include "xfs_bmap_util.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_trans_space.h"
  21. #include "xfs_icache.h"
  22. #include "xfs_rtalloc.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_rtbitmap.h"
  25. #include "xfs_quota.h"
  26. #include "xfs_log_priv.h"
  27. #include "xfs_health.h"
  28. /*
  29. * Return whether there are any free extents in the size range given
  30. * by low and high, for the bitmap block bbno.
  31. */
  32. STATIC int
  33. xfs_rtany_summary(
  34. struct xfs_rtalloc_args *args,
  35. int low, /* low log2 extent size */
  36. int high, /* high log2 extent size */
  37. xfs_fileoff_t bbno, /* bitmap block number */
  38. int *maxlog) /* out: max log2 extent size free */
  39. {
  40. struct xfs_mount *mp = args->mp;
  41. int error;
  42. int log; /* loop counter, log2 of ext. size */
  43. xfs_suminfo_t sum; /* summary data */
  44. /* There are no extents at levels >= m_rsum_cache[bbno]. */
  45. if (mp->m_rsum_cache) {
  46. high = min(high, mp->m_rsum_cache[bbno] - 1);
  47. if (low > high) {
  48. *maxlog = -1;
  49. return 0;
  50. }
  51. }
  52. /*
  53. * Loop over logs of extent sizes.
  54. */
  55. for (log = high; log >= low; log--) {
  56. /*
  57. * Get one summary datum.
  58. */
  59. error = xfs_rtget_summary(args, log, bbno, &sum);
  60. if (error) {
  61. return error;
  62. }
  63. /*
  64. * If there are any, return success.
  65. */
  66. if (sum) {
  67. *maxlog = log;
  68. goto out;
  69. }
  70. }
  71. /*
  72. * Found nothing, return failure.
  73. */
  74. *maxlog = -1;
  75. out:
  76. /* There were no extents at levels > log. */
  77. if (mp->m_rsum_cache && log + 1 < mp->m_rsum_cache[bbno])
  78. mp->m_rsum_cache[bbno] = log + 1;
  79. return 0;
  80. }
  81. /*
  82. * Copy and transform the summary file, given the old and new
  83. * parameters in the mount structures.
  84. */
  85. STATIC int
  86. xfs_rtcopy_summary(
  87. struct xfs_rtalloc_args *oargs,
  88. struct xfs_rtalloc_args *nargs)
  89. {
  90. xfs_fileoff_t bbno; /* bitmap block number */
  91. int error;
  92. int log; /* summary level number (log length) */
  93. xfs_suminfo_t sum; /* summary data */
  94. for (log = oargs->mp->m_rsumlevels - 1; log >= 0; log--) {
  95. for (bbno = oargs->mp->m_sb.sb_rbmblocks - 1;
  96. (xfs_srtblock_t)bbno >= 0;
  97. bbno--) {
  98. error = xfs_rtget_summary(oargs, log, bbno, &sum);
  99. if (error)
  100. goto out;
  101. if (sum == 0)
  102. continue;
  103. error = xfs_rtmodify_summary(oargs, log, bbno, -sum);
  104. if (error)
  105. goto out;
  106. error = xfs_rtmodify_summary(nargs, log, bbno, sum);
  107. if (error)
  108. goto out;
  109. ASSERT(sum > 0);
  110. }
  111. }
  112. error = 0;
  113. out:
  114. xfs_rtbuf_cache_relse(oargs);
  115. return 0;
  116. }
  117. /*
  118. * Mark an extent specified by start and len allocated.
  119. * Updates all the summary information as well as the bitmap.
  120. */
  121. STATIC int
  122. xfs_rtallocate_range(
  123. struct xfs_rtalloc_args *args,
  124. xfs_rtxnum_t start, /* start rtext to allocate */
  125. xfs_rtxlen_t len) /* in/out: summary block number */
  126. {
  127. struct xfs_mount *mp = args->mp;
  128. xfs_rtxnum_t end; /* end of the allocated rtext */
  129. int error;
  130. xfs_rtxnum_t postblock = 0; /* first rtext allocated > end */
  131. xfs_rtxnum_t preblock = 0; /* first rtext allocated < start */
  132. end = start + len - 1;
  133. /*
  134. * Assume we're allocating out of the middle of a free extent.
  135. * We need to find the beginning and end of the extent so we can
  136. * properly update the summary.
  137. */
  138. error = xfs_rtfind_back(args, start, &preblock);
  139. if (error)
  140. return error;
  141. /*
  142. * Find the next allocated block (end of free extent).
  143. */
  144. error = xfs_rtfind_forw(args, end, mp->m_sb.sb_rextents - 1,
  145. &postblock);
  146. if (error)
  147. return error;
  148. /*
  149. * Decrement the summary information corresponding to the entire
  150. * (old) free extent.
  151. */
  152. error = xfs_rtmodify_summary(args,
  153. xfs_highbit64(postblock + 1 - preblock),
  154. xfs_rtx_to_rbmblock(mp, preblock), -1);
  155. if (error)
  156. return error;
  157. /*
  158. * If there are blocks not being allocated at the front of the
  159. * old extent, add summary data for them to be free.
  160. */
  161. if (preblock < start) {
  162. error = xfs_rtmodify_summary(args,
  163. xfs_highbit64(start - preblock),
  164. xfs_rtx_to_rbmblock(mp, preblock), 1);
  165. if (error)
  166. return error;
  167. }
  168. /*
  169. * If there are blocks not being allocated at the end of the
  170. * old extent, add summary data for them to be free.
  171. */
  172. if (postblock > end) {
  173. error = xfs_rtmodify_summary(args,
  174. xfs_highbit64(postblock - end),
  175. xfs_rtx_to_rbmblock(mp, end + 1), 1);
  176. if (error)
  177. return error;
  178. }
  179. /*
  180. * Modify the bitmap to mark this extent allocated.
  181. */
  182. return xfs_rtmodify_range(args, start, len, 0);
  183. }
  184. /* Reduce @rtxlen until it is a multiple of @prod. */
  185. static inline xfs_rtxlen_t
  186. xfs_rtalloc_align_len(
  187. xfs_rtxlen_t rtxlen,
  188. xfs_rtxlen_t prod)
  189. {
  190. if (unlikely(prod > 1))
  191. return rounddown(rtxlen, prod);
  192. return rtxlen;
  193. }
  194. /*
  195. * Make sure we don't run off the end of the rt volume. Be careful that
  196. * adjusting maxlen downwards doesn't cause us to fail the alignment checks.
  197. */
  198. static inline xfs_rtxlen_t
  199. xfs_rtallocate_clamp_len(
  200. struct xfs_mount *mp,
  201. xfs_rtxnum_t startrtx,
  202. xfs_rtxlen_t rtxlen,
  203. xfs_rtxlen_t prod)
  204. {
  205. xfs_rtxlen_t ret;
  206. ret = min(mp->m_sb.sb_rextents, startrtx + rtxlen) - startrtx;
  207. return xfs_rtalloc_align_len(ret, prod);
  208. }
  209. /*
  210. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  211. * bitmap block bbno. If we don't get maxlen then use prod to trim
  212. * the length, if given. Returns error; returns starting block in *rtx.
  213. * The lengths are all in rtextents.
  214. */
  215. STATIC int
  216. xfs_rtallocate_extent_block(
  217. struct xfs_rtalloc_args *args,
  218. xfs_fileoff_t bbno, /* bitmap block number */
  219. xfs_rtxlen_t minlen, /* minimum length to allocate */
  220. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  221. xfs_rtxlen_t *len, /* out: actual length allocated */
  222. xfs_rtxnum_t *nextp, /* out: next rtext to try */
  223. xfs_rtxlen_t prod, /* extent product factor */
  224. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  225. {
  226. struct xfs_mount *mp = args->mp;
  227. xfs_rtxnum_t besti = -1; /* best rtext found so far */
  228. xfs_rtxnum_t end; /* last rtext in chunk */
  229. xfs_rtxnum_t i; /* current rtext trying */
  230. xfs_rtxnum_t next; /* next rtext to try */
  231. xfs_rtxlen_t scanlen; /* number of free rtx to look for */
  232. xfs_rtxlen_t bestlen = 0; /* best length found so far */
  233. int stat; /* status from internal calls */
  234. int error;
  235. /*
  236. * Loop over all the extents starting in this bitmap block up to the
  237. * end of the rt volume, looking for one that's long enough.
  238. */
  239. end = min(mp->m_sb.sb_rextents, xfs_rbmblock_to_rtx(mp, bbno + 1)) - 1;
  240. for (i = xfs_rbmblock_to_rtx(mp, bbno); i <= end; i++) {
  241. /* Make sure we don't scan off the end of the rt volume. */
  242. scanlen = xfs_rtallocate_clamp_len(mp, i, maxlen, prod);
  243. if (scanlen < minlen)
  244. break;
  245. /*
  246. * See if there's a free extent of scanlen starting at i.
  247. * If it's not so then next will contain the first non-free.
  248. */
  249. error = xfs_rtcheck_range(args, i, scanlen, 1, &next, &stat);
  250. if (error)
  251. return error;
  252. if (stat) {
  253. /*
  254. * i to scanlen is all free, allocate and return that.
  255. */
  256. *len = scanlen;
  257. *rtx = i;
  258. return 0;
  259. }
  260. /*
  261. * In the case where we have a variable-sized allocation
  262. * request, figure out how big this free piece is,
  263. * and if it's big enough for the minimum, and the best
  264. * so far, remember it.
  265. */
  266. if (minlen < maxlen) {
  267. xfs_rtxnum_t thislen; /* this extent size */
  268. thislen = next - i;
  269. if (thislen >= minlen && thislen > bestlen) {
  270. besti = i;
  271. bestlen = thislen;
  272. }
  273. }
  274. /*
  275. * If not done yet, find the start of the next free space.
  276. */
  277. if (next >= end)
  278. break;
  279. error = xfs_rtfind_forw(args, next, end, &i);
  280. if (error)
  281. return error;
  282. }
  283. /* Searched the whole thing & didn't find a maxlen free extent. */
  284. if (besti == -1)
  285. goto nospace;
  286. /*
  287. * Ensure bestlen is a multiple of prod, but don't return a too-short
  288. * extent.
  289. */
  290. bestlen = xfs_rtalloc_align_len(bestlen, prod);
  291. if (bestlen < minlen)
  292. goto nospace;
  293. /*
  294. * Pick besti for bestlen & return that.
  295. */
  296. *len = bestlen;
  297. *rtx = besti;
  298. return 0;
  299. nospace:
  300. /* Allocation failed. Set *nextp to the next block to try. */
  301. *nextp = next;
  302. return -ENOSPC;
  303. }
  304. /*
  305. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  306. * bno. If we don't get maxlen then use prod to trim the length, if given.
  307. * Returns error; returns starting block in *rtx.
  308. * The lengths are all in rtextents.
  309. */
  310. STATIC int
  311. xfs_rtallocate_extent_exact(
  312. struct xfs_rtalloc_args *args,
  313. xfs_rtxnum_t start, /* starting rtext number to allocate */
  314. xfs_rtxlen_t minlen, /* minimum length to allocate */
  315. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  316. xfs_rtxlen_t *len, /* out: actual length allocated */
  317. xfs_rtxlen_t prod, /* extent product factor */
  318. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  319. {
  320. struct xfs_mount *mp = args->mp;
  321. xfs_rtxnum_t next; /* next rtext to try (dummy) */
  322. xfs_rtxlen_t alloclen; /* candidate length */
  323. xfs_rtxlen_t scanlen; /* number of free rtx to look for */
  324. int isfree; /* extent is free */
  325. int error;
  326. ASSERT(minlen % prod == 0);
  327. ASSERT(maxlen % prod == 0);
  328. /* Make sure we don't run off the end of the rt volume. */
  329. scanlen = xfs_rtallocate_clamp_len(mp, start, maxlen, prod);
  330. if (scanlen < minlen)
  331. return -ENOSPC;
  332. /* Check if the range in question (for scanlen) is free. */
  333. error = xfs_rtcheck_range(args, start, scanlen, 1, &next, &isfree);
  334. if (error)
  335. return error;
  336. if (isfree) {
  337. /* start to scanlen is all free; allocate it. */
  338. *len = scanlen;
  339. *rtx = start;
  340. return 0;
  341. }
  342. /*
  343. * If not, allocate what there is, if it's at least minlen.
  344. */
  345. alloclen = next - start;
  346. if (alloclen < minlen)
  347. return -ENOSPC;
  348. /* Ensure alloclen is a multiple of prod. */
  349. alloclen = xfs_rtalloc_align_len(alloclen, prod);
  350. if (alloclen < minlen)
  351. return -ENOSPC;
  352. *len = alloclen;
  353. *rtx = start;
  354. return 0;
  355. }
  356. /*
  357. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  358. * to start as possible. If we don't get maxlen then use prod to trim
  359. * the length, if given. The lengths are all in rtextents.
  360. */
  361. STATIC int
  362. xfs_rtallocate_extent_near(
  363. struct xfs_rtalloc_args *args,
  364. xfs_rtxnum_t start, /* starting rtext number to allocate */
  365. xfs_rtxlen_t minlen, /* minimum length to allocate */
  366. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  367. xfs_rtxlen_t *len, /* out: actual length allocated */
  368. xfs_rtxlen_t prod, /* extent product factor */
  369. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  370. {
  371. struct xfs_mount *mp = args->mp;
  372. int maxlog; /* max useful extent from summary */
  373. xfs_fileoff_t bbno; /* bitmap block number */
  374. int error;
  375. int i; /* bitmap block offset (loop control) */
  376. int j; /* secondary loop control */
  377. int log2len; /* log2 of minlen */
  378. xfs_rtxnum_t n; /* next rtext to try */
  379. ASSERT(minlen % prod == 0);
  380. ASSERT(maxlen % prod == 0);
  381. /*
  382. * If the block number given is off the end, silently set it to
  383. * the last block.
  384. */
  385. if (start >= mp->m_sb.sb_rextents)
  386. start = mp->m_sb.sb_rextents - 1;
  387. /*
  388. * Try the exact allocation first.
  389. */
  390. error = xfs_rtallocate_extent_exact(args, start, minlen, maxlen, len,
  391. prod, rtx);
  392. if (error != -ENOSPC)
  393. return error;
  394. bbno = xfs_rtx_to_rbmblock(mp, start);
  395. i = 0;
  396. j = -1;
  397. ASSERT(minlen != 0);
  398. log2len = xfs_highbit32(minlen);
  399. /*
  400. * Loop over all bitmap blocks (bbno + i is current block).
  401. */
  402. for (;;) {
  403. /*
  404. * Get summary information of extents of all useful levels
  405. * starting in this bitmap block.
  406. */
  407. error = xfs_rtany_summary(args, log2len, mp->m_rsumlevels - 1,
  408. bbno + i, &maxlog);
  409. if (error)
  410. return error;
  411. /*
  412. * If there are any useful extents starting here, try
  413. * allocating one.
  414. */
  415. if (maxlog >= 0) {
  416. xfs_extlen_t maxavail =
  417. min_t(xfs_rtblock_t, maxlen,
  418. (1ULL << (maxlog + 1)) - 1);
  419. /*
  420. * On the positive side of the starting location.
  421. */
  422. if (i >= 0) {
  423. /*
  424. * Try to allocate an extent starting in
  425. * this block.
  426. */
  427. error = xfs_rtallocate_extent_block(args,
  428. bbno + i, minlen, maxavail, len,
  429. &n, prod, rtx);
  430. if (error != -ENOSPC)
  431. return error;
  432. }
  433. /*
  434. * On the negative side of the starting location.
  435. */
  436. else { /* i < 0 */
  437. int maxblocks;
  438. /*
  439. * Loop backwards to find the end of the extent
  440. * we found in the realtime summary.
  441. *
  442. * maxblocks is the maximum possible number of
  443. * bitmap blocks from the start of the extent
  444. * to the end of the extent.
  445. */
  446. if (maxlog == 0)
  447. maxblocks = 0;
  448. else if (maxlog < mp->m_blkbit_log)
  449. maxblocks = 1;
  450. else
  451. maxblocks = 2 << (maxlog - mp->m_blkbit_log);
  452. /*
  453. * We need to check bbno + i + maxblocks down to
  454. * bbno + i. We already checked bbno down to
  455. * bbno + j + 1, so we don't need to check those
  456. * again.
  457. */
  458. j = min(i + maxblocks, j);
  459. for (; j >= i; j--) {
  460. error = xfs_rtallocate_extent_block(args,
  461. bbno + j, minlen,
  462. maxavail, len, &n, prod,
  463. rtx);
  464. if (error != -ENOSPC)
  465. return error;
  466. }
  467. }
  468. }
  469. /*
  470. * Loop control. If we were on the positive side, and there's
  471. * still more blocks on the negative side, go there.
  472. */
  473. if (i > 0 && (int)bbno - i >= 0)
  474. i = -i;
  475. /*
  476. * If positive, and no more negative, but there are more
  477. * positive, go there.
  478. */
  479. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  480. i++;
  481. /*
  482. * If negative or 0 (just started), and there are positive
  483. * blocks to go, go there. The 0 case moves to block 1.
  484. */
  485. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  486. i = 1 - i;
  487. /*
  488. * If negative or 0 and there are more negative blocks,
  489. * go there.
  490. */
  491. else if (i <= 0 && (int)bbno + i > 0)
  492. i--;
  493. /*
  494. * Must be done. Return failure.
  495. */
  496. else
  497. break;
  498. }
  499. return -ENOSPC;
  500. }
  501. static int
  502. xfs_rtalloc_sumlevel(
  503. struct xfs_rtalloc_args *args,
  504. int l, /* level number */
  505. xfs_rtxlen_t minlen, /* minimum length to allocate */
  506. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  507. xfs_rtxlen_t prod, /* extent product factor */
  508. xfs_rtxlen_t *len, /* out: actual length allocated */
  509. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  510. {
  511. xfs_fileoff_t i; /* bitmap block number */
  512. int error;
  513. for (i = 0; i < args->mp->m_sb.sb_rbmblocks; i++) {
  514. xfs_suminfo_t sum; /* summary information for extents */
  515. xfs_rtxnum_t n; /* next rtext to be tried */
  516. error = xfs_rtget_summary(args, l, i, &sum);
  517. if (error)
  518. return error;
  519. /*
  520. * Nothing there, on to the next block.
  521. */
  522. if (!sum)
  523. continue;
  524. /*
  525. * Try allocating the extent.
  526. */
  527. error = xfs_rtallocate_extent_block(args, i, minlen, maxlen,
  528. len, &n, prod, rtx);
  529. if (error != -ENOSPC)
  530. return error;
  531. /*
  532. * If the "next block to try" returned from the allocator is
  533. * beyond the next bitmap block, skip to that bitmap block.
  534. */
  535. if (xfs_rtx_to_rbmblock(args->mp, n) > i + 1)
  536. i = xfs_rtx_to_rbmblock(args->mp, n) - 1;
  537. }
  538. return -ENOSPC;
  539. }
  540. /*
  541. * Allocate an extent of length minlen<=len<=maxlen, with no position
  542. * specified. If we don't get maxlen then use prod to trim
  543. * the length, if given. The lengths are all in rtextents.
  544. */
  545. STATIC int
  546. xfs_rtallocate_extent_size(
  547. struct xfs_rtalloc_args *args,
  548. xfs_rtxlen_t minlen, /* minimum length to allocate */
  549. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  550. xfs_rtxlen_t *len, /* out: actual length allocated */
  551. xfs_rtxlen_t prod, /* extent product factor */
  552. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  553. {
  554. int error;
  555. int l; /* level number (loop control) */
  556. ASSERT(minlen % prod == 0);
  557. ASSERT(maxlen % prod == 0);
  558. ASSERT(maxlen != 0);
  559. /*
  560. * Loop over all the levels starting with maxlen.
  561. *
  562. * At each level, look at all the bitmap blocks, to see if there are
  563. * extents starting there that are long enough (>= maxlen).
  564. *
  565. * Note, only on the initial level can the allocation fail if the
  566. * summary says there's an extent.
  567. */
  568. for (l = xfs_highbit32(maxlen); l < args->mp->m_rsumlevels; l++) {
  569. error = xfs_rtalloc_sumlevel(args, l, minlen, maxlen, prod, len,
  570. rtx);
  571. if (error != -ENOSPC)
  572. return error;
  573. }
  574. /*
  575. * Didn't find any maxlen blocks. Try smaller ones, unless we are
  576. * looking for a fixed size extent.
  577. */
  578. if (minlen > --maxlen)
  579. return -ENOSPC;
  580. ASSERT(minlen != 0);
  581. ASSERT(maxlen != 0);
  582. /*
  583. * Loop over sizes, from maxlen down to minlen.
  584. *
  585. * This time, when we do the allocations, allow smaller ones to succeed,
  586. * but make sure the specified minlen/maxlen are in the possible range
  587. * for this summary level.
  588. */
  589. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  590. error = xfs_rtalloc_sumlevel(args, l,
  591. max_t(xfs_rtxlen_t, minlen, 1 << l),
  592. min_t(xfs_rtxlen_t, maxlen, (1 << (l + 1)) - 1),
  593. prod, len, rtx);
  594. if (error != -ENOSPC)
  595. return error;
  596. }
  597. return -ENOSPC;
  598. }
  599. static int
  600. xfs_alloc_rsum_cache(
  601. struct xfs_mount *mp,
  602. xfs_extlen_t rbmblocks)
  603. {
  604. /*
  605. * The rsum cache is initialized to the maximum value, which is
  606. * trivially an upper bound on the maximum level with any free extents.
  607. */
  608. mp->m_rsum_cache = kvmalloc(rbmblocks, GFP_KERNEL);
  609. if (!mp->m_rsum_cache)
  610. return -ENOMEM;
  611. memset(mp->m_rsum_cache, -1, rbmblocks);
  612. return 0;
  613. }
  614. /*
  615. * If we changed the rt extent size (meaning there was no rt volume previously)
  616. * and the root directory had EXTSZINHERIT and RTINHERIT set, it's possible
  617. * that the extent size hint on the root directory is no longer congruent with
  618. * the new rt extent size. Log the rootdir inode to fix this.
  619. */
  620. static int
  621. xfs_growfs_rt_fixup_extsize(
  622. struct xfs_mount *mp)
  623. {
  624. struct xfs_inode *ip = mp->m_rootip;
  625. struct xfs_trans *tp;
  626. int error = 0;
  627. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  628. if (!(ip->i_diflags & XFS_DIFLAG_RTINHERIT) ||
  629. !(ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT))
  630. goto out_iolock;
  631. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_ichange, 0, 0, false,
  632. &tp);
  633. if (error)
  634. goto out_iolock;
  635. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  636. error = xfs_trans_commit(tp);
  637. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  638. out_iolock:
  639. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  640. return error;
  641. }
  642. static int
  643. xfs_growfs_rt_bmblock(
  644. struct xfs_mount *mp,
  645. xfs_rfsblock_t nrblocks,
  646. xfs_agblock_t rextsize,
  647. xfs_fileoff_t bmbno)
  648. {
  649. struct xfs_inode *rbmip = mp->m_rbmip;
  650. struct xfs_inode *rsumip = mp->m_rsumip;
  651. struct xfs_rtalloc_args args = {
  652. .mp = mp,
  653. };
  654. struct xfs_rtalloc_args nargs = {
  655. };
  656. struct xfs_mount *nmp;
  657. xfs_rfsblock_t nrblocks_step;
  658. xfs_rtbxlen_t freed_rtx;
  659. int error;
  660. nrblocks_step = (bmbno + 1) * NBBY * mp->m_sb.sb_blocksize * rextsize;
  661. nmp = nargs.mp = kmemdup(mp, sizeof(*mp), GFP_KERNEL);
  662. if (!nmp)
  663. return -ENOMEM;
  664. /*
  665. * Calculate new sb and mount fields for this round.
  666. */
  667. nmp->m_sb.sb_rextsize = rextsize;
  668. xfs_mount_sb_set_rextsize(nmp, &nmp->m_sb);
  669. nmp->m_sb.sb_rbmblocks = bmbno + 1;
  670. nmp->m_sb.sb_rblocks = min(nrblocks, nrblocks_step);
  671. nmp->m_sb.sb_rextents = xfs_rtb_to_rtx(nmp, nmp->m_sb.sb_rblocks);
  672. nmp->m_sb.sb_rextslog = xfs_compute_rextslog(nmp->m_sb.sb_rextents);
  673. nmp->m_rsumlevels = nmp->m_sb.sb_rextslog + 1;
  674. nmp->m_rsumblocks = xfs_rtsummary_blockcount(mp, nmp->m_rsumlevels,
  675. nmp->m_sb.sb_rbmblocks);
  676. /*
  677. * Recompute the growfsrt reservation from the new rsumsize, so that the
  678. * transaction below use the new, potentially larger value.
  679. * */
  680. xfs_trans_resv_calc(nmp, &nmp->m_resv);
  681. error = xfs_trans_alloc(mp, &M_RES(nmp)->tr_growrtfree, 0, 0, 0,
  682. &args.tp);
  683. if (error)
  684. goto out_free;
  685. nargs.tp = args.tp;
  686. xfs_rtbitmap_lock(mp);
  687. xfs_rtbitmap_trans_join(args.tp);
  688. /*
  689. * Update the bitmap inode's size ondisk and incore. We need to update
  690. * the incore size so that inode inactivation won't punch what it thinks
  691. * are "posteof" blocks.
  692. */
  693. rbmip->i_disk_size = nmp->m_sb.sb_rbmblocks * nmp->m_sb.sb_blocksize;
  694. i_size_write(VFS_I(rbmip), rbmip->i_disk_size);
  695. xfs_trans_log_inode(args.tp, rbmip, XFS_ILOG_CORE);
  696. /*
  697. * Update the summary inode's size. We need to update the incore size
  698. * so that inode inactivation won't punch what it thinks are "posteof"
  699. * blocks.
  700. */
  701. rsumip->i_disk_size = nmp->m_rsumblocks * nmp->m_sb.sb_blocksize;
  702. i_size_write(VFS_I(rsumip), rsumip->i_disk_size);
  703. xfs_trans_log_inode(args.tp, rsumip, XFS_ILOG_CORE);
  704. /*
  705. * Copy summary data from old to new sizes when the real size (not
  706. * block-aligned) changes.
  707. */
  708. if (mp->m_sb.sb_rbmblocks != nmp->m_sb.sb_rbmblocks ||
  709. mp->m_rsumlevels != nmp->m_rsumlevels) {
  710. error = xfs_rtcopy_summary(&args, &nargs);
  711. if (error)
  712. goto out_cancel;
  713. }
  714. /*
  715. * Update superblock fields.
  716. */
  717. if (nmp->m_sb.sb_rextsize != mp->m_sb.sb_rextsize)
  718. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_REXTSIZE,
  719. nmp->m_sb.sb_rextsize - mp->m_sb.sb_rextsize);
  720. if (nmp->m_sb.sb_rbmblocks != mp->m_sb.sb_rbmblocks)
  721. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_RBMBLOCKS,
  722. nmp->m_sb.sb_rbmblocks - mp->m_sb.sb_rbmblocks);
  723. if (nmp->m_sb.sb_rblocks != mp->m_sb.sb_rblocks)
  724. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_RBLOCKS,
  725. nmp->m_sb.sb_rblocks - mp->m_sb.sb_rblocks);
  726. if (nmp->m_sb.sb_rextents != mp->m_sb.sb_rextents)
  727. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_REXTENTS,
  728. nmp->m_sb.sb_rextents - mp->m_sb.sb_rextents);
  729. if (nmp->m_sb.sb_rextslog != mp->m_sb.sb_rextslog)
  730. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_REXTSLOG,
  731. nmp->m_sb.sb_rextslog - mp->m_sb.sb_rextslog);
  732. /*
  733. * Free the new extent.
  734. */
  735. freed_rtx = nmp->m_sb.sb_rextents - mp->m_sb.sb_rextents;
  736. error = xfs_rtfree_range(&nargs, mp->m_sb.sb_rextents, freed_rtx);
  737. xfs_rtbuf_cache_relse(&nargs);
  738. if (error)
  739. goto out_cancel;
  740. /*
  741. * Mark more blocks free in the superblock.
  742. */
  743. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_FREXTENTS, freed_rtx);
  744. /*
  745. * Update the calculated values in the real mount structure.
  746. */
  747. mp->m_rsumlevels = nmp->m_rsumlevels;
  748. mp->m_rsumblocks = nmp->m_rsumblocks;
  749. xfs_mount_sb_set_rextsize(mp, &mp->m_sb);
  750. /*
  751. * Recompute the growfsrt reservation from the new rsumsize.
  752. */
  753. xfs_trans_resv_calc(mp, &mp->m_resv);
  754. error = xfs_trans_commit(args.tp);
  755. if (error)
  756. goto out_free;
  757. /*
  758. * Ensure the mount RT feature flag is now set.
  759. */
  760. mp->m_features |= XFS_FEAT_REALTIME;
  761. kfree(nmp);
  762. return 0;
  763. out_cancel:
  764. xfs_trans_cancel(args.tp);
  765. out_free:
  766. kfree(nmp);
  767. return error;
  768. }
  769. /*
  770. * Calculate the last rbmblock currently used.
  771. *
  772. * This also deals with the case where there were no rtextents before.
  773. */
  774. static xfs_fileoff_t
  775. xfs_last_rt_bmblock(
  776. struct xfs_mount *mp)
  777. {
  778. xfs_fileoff_t bmbno = mp->m_sb.sb_rbmblocks;
  779. /* Skip the current block if it is exactly full. */
  780. if (xfs_rtx_to_rbmword(mp, mp->m_sb.sb_rextents) != 0)
  781. bmbno--;
  782. return bmbno;
  783. }
  784. /*
  785. * Grow the realtime area of the filesystem.
  786. */
  787. int
  788. xfs_growfs_rt(
  789. xfs_mount_t *mp, /* mount point for filesystem */
  790. xfs_growfs_rt_t *in) /* growfs rt input struct */
  791. {
  792. xfs_fileoff_t bmbno; /* bitmap block number */
  793. struct xfs_buf *bp; /* temporary buffer */
  794. int error; /* error return value */
  795. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  796. xfs_rtxnum_t nrextents; /* new number of realtime extents */
  797. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  798. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  799. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  800. uint8_t *rsum_cache; /* old summary cache */
  801. xfs_agblock_t old_rextsize = mp->m_sb.sb_rextsize;
  802. if (!capable(CAP_SYS_ADMIN))
  803. return -EPERM;
  804. /* Needs to have been mounted with an rt device. */
  805. if (!XFS_IS_REALTIME_MOUNT(mp))
  806. return -EINVAL;
  807. if (!mutex_trylock(&mp->m_growlock))
  808. return -EWOULDBLOCK;
  809. /*
  810. * Mount should fail if the rt bitmap/summary files don't load, but
  811. * we'll check anyway.
  812. */
  813. error = -EINVAL;
  814. if (!mp->m_rbmip || !mp->m_rsumip)
  815. goto out_unlock;
  816. /* Shrink not supported. */
  817. if (in->newblocks <= mp->m_sb.sb_rblocks)
  818. goto out_unlock;
  819. /* Can only change rt extent size when adding rt volume. */
  820. if (mp->m_sb.sb_rblocks > 0 && in->extsize != mp->m_sb.sb_rextsize)
  821. goto out_unlock;
  822. /* Range check the extent size. */
  823. if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
  824. XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
  825. goto out_unlock;
  826. /* Unsupported realtime features. */
  827. error = -EOPNOTSUPP;
  828. if (xfs_has_rmapbt(mp) || xfs_has_reflink(mp) || xfs_has_quota(mp))
  829. goto out_unlock;
  830. error = xfs_sb_validate_fsb_count(&mp->m_sb, in->newblocks);
  831. if (error)
  832. goto out_unlock;
  833. /*
  834. * Read in the last block of the device, make sure it exists.
  835. */
  836. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  837. XFS_FSB_TO_BB(mp, in->newblocks - 1),
  838. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  839. if (error)
  840. goto out_unlock;
  841. xfs_buf_relse(bp);
  842. /*
  843. * Calculate new parameters. These are the final values to be reached.
  844. */
  845. nrextents = div_u64(in->newblocks, in->extsize);
  846. if (nrextents == 0) {
  847. error = -EINVAL;
  848. goto out_unlock;
  849. }
  850. nrbmblocks = xfs_rtbitmap_blockcount(mp, nrextents);
  851. nrsumblocks = xfs_rtsummary_blockcount(mp,
  852. xfs_compute_rextslog(nrextents) + 1, nrbmblocks);
  853. /*
  854. * New summary size can't be more than half the size of
  855. * the log. This prevents us from getting a log overflow,
  856. * since we'll log basically the whole summary file at once.
  857. */
  858. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1)) {
  859. error = -EINVAL;
  860. goto out_unlock;
  861. }
  862. /*
  863. * Get the old block counts for bitmap and summary inodes.
  864. * These can't change since other growfs callers are locked out.
  865. */
  866. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
  867. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
  868. /*
  869. * Allocate space to the bitmap and summary files, as necessary.
  870. */
  871. error = xfs_rtfile_initialize_blocks(mp->m_rbmip, rbmblocks,
  872. nrbmblocks, NULL);
  873. if (error)
  874. goto out_unlock;
  875. error = xfs_rtfile_initialize_blocks(mp->m_rsumip, rsumblocks,
  876. nrsumblocks, NULL);
  877. if (error)
  878. goto out_unlock;
  879. rsum_cache = mp->m_rsum_cache;
  880. if (nrbmblocks != mp->m_sb.sb_rbmblocks) {
  881. error = xfs_alloc_rsum_cache(mp, nrbmblocks);
  882. if (error)
  883. goto out_unlock;
  884. }
  885. /* Initialize the free space bitmap one bitmap block at a time. */
  886. for (bmbno = xfs_last_rt_bmblock(mp); bmbno < nrbmblocks; bmbno++) {
  887. error = xfs_growfs_rt_bmblock(mp, in->newblocks, in->extsize,
  888. bmbno);
  889. if (error)
  890. goto out_free;
  891. }
  892. if (old_rextsize != in->extsize) {
  893. error = xfs_growfs_rt_fixup_extsize(mp);
  894. if (error)
  895. goto out_free;
  896. }
  897. /* Update secondary superblocks now the physical grow has completed */
  898. error = xfs_update_secondary_sbs(mp);
  899. out_free:
  900. /*
  901. * If we had to allocate a new rsum_cache, we either need to free the
  902. * old one (if we succeeded) or free the new one and restore the old one
  903. * (if there was an error).
  904. */
  905. if (rsum_cache != mp->m_rsum_cache) {
  906. if (error) {
  907. kvfree(mp->m_rsum_cache);
  908. mp->m_rsum_cache = rsum_cache;
  909. } else {
  910. kvfree(rsum_cache);
  911. }
  912. }
  913. out_unlock:
  914. mutex_unlock(&mp->m_growlock);
  915. return error;
  916. }
  917. /*
  918. * Initialize realtime fields in the mount structure.
  919. */
  920. int /* error */
  921. xfs_rtmount_init(
  922. struct xfs_mount *mp) /* file system mount structure */
  923. {
  924. struct xfs_buf *bp; /* buffer for last block of subvolume */
  925. struct xfs_sb *sbp; /* filesystem superblock copy in mount */
  926. xfs_daddr_t d; /* address of last block of subvolume */
  927. int error;
  928. sbp = &mp->m_sb;
  929. if (sbp->sb_rblocks == 0)
  930. return 0;
  931. if (mp->m_rtdev_targp == NULL) {
  932. xfs_warn(mp,
  933. "Filesystem has a realtime volume, use rtdev=device option");
  934. return -ENODEV;
  935. }
  936. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  937. mp->m_rsumblocks = xfs_rtsummary_blockcount(mp, mp->m_rsumlevels,
  938. mp->m_sb.sb_rbmblocks);
  939. mp->m_rbmip = mp->m_rsumip = NULL;
  940. /*
  941. * Check that the realtime section is an ok size.
  942. */
  943. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  944. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  945. xfs_warn(mp, "realtime mount -- %llu != %llu",
  946. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  947. (unsigned long long) mp->m_sb.sb_rblocks);
  948. return -EFBIG;
  949. }
  950. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  951. d - XFS_FSB_TO_BB(mp, 1),
  952. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  953. if (error) {
  954. xfs_warn(mp, "realtime device size check failed");
  955. return error;
  956. }
  957. xfs_buf_relse(bp);
  958. return 0;
  959. }
  960. static int
  961. xfs_rtalloc_count_frextent(
  962. struct xfs_mount *mp,
  963. struct xfs_trans *tp,
  964. const struct xfs_rtalloc_rec *rec,
  965. void *priv)
  966. {
  967. uint64_t *valp = priv;
  968. *valp += rec->ar_extcount;
  969. return 0;
  970. }
  971. /*
  972. * Reinitialize the number of free realtime extents from the realtime bitmap.
  973. * Callers must ensure that there is no other activity in the filesystem.
  974. */
  975. int
  976. xfs_rtalloc_reinit_frextents(
  977. struct xfs_mount *mp)
  978. {
  979. uint64_t val = 0;
  980. int error;
  981. xfs_rtbitmap_lock_shared(mp, XFS_RBMLOCK_BITMAP);
  982. error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
  983. &val);
  984. xfs_rtbitmap_unlock_shared(mp, XFS_RBMLOCK_BITMAP);
  985. if (error)
  986. return error;
  987. spin_lock(&mp->m_sb_lock);
  988. mp->m_sb.sb_frextents = val;
  989. spin_unlock(&mp->m_sb_lock);
  990. percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
  991. return 0;
  992. }
  993. /*
  994. * Read in the bmbt of an rt metadata inode so that we never have to load them
  995. * at runtime. This enables the use of shared ILOCKs for rtbitmap scans. Use
  996. * an empty transaction to avoid deadlocking on loops in the bmbt.
  997. */
  998. static inline int
  999. xfs_rtmount_iread_extents(
  1000. struct xfs_inode *ip,
  1001. unsigned int lock_class)
  1002. {
  1003. struct xfs_trans *tp;
  1004. int error;
  1005. error = xfs_trans_alloc_empty(ip->i_mount, &tp);
  1006. if (error)
  1007. return error;
  1008. xfs_ilock(ip, XFS_ILOCK_EXCL | lock_class);
  1009. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1010. if (error)
  1011. goto out_unlock;
  1012. if (xfs_inode_has_attr_fork(ip)) {
  1013. error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
  1014. if (error)
  1015. goto out_unlock;
  1016. }
  1017. out_unlock:
  1018. xfs_iunlock(ip, XFS_ILOCK_EXCL | lock_class);
  1019. xfs_trans_cancel(tp);
  1020. return error;
  1021. }
  1022. /*
  1023. * Get the bitmap and summary inodes and the summary cache into the mount
  1024. * structure at mount time.
  1025. */
  1026. int /* error */
  1027. xfs_rtmount_inodes(
  1028. xfs_mount_t *mp) /* file system mount structure */
  1029. {
  1030. int error; /* error return value */
  1031. xfs_sb_t *sbp;
  1032. sbp = &mp->m_sb;
  1033. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
  1034. if (xfs_metadata_is_sick(error))
  1035. xfs_rt_mark_sick(mp, XFS_SICK_RT_BITMAP);
  1036. if (error)
  1037. return error;
  1038. ASSERT(mp->m_rbmip != NULL);
  1039. error = xfs_rtmount_iread_extents(mp->m_rbmip, XFS_ILOCK_RTBITMAP);
  1040. if (error)
  1041. goto out_rele_bitmap;
  1042. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
  1043. if (xfs_metadata_is_sick(error))
  1044. xfs_rt_mark_sick(mp, XFS_SICK_RT_SUMMARY);
  1045. if (error)
  1046. goto out_rele_bitmap;
  1047. ASSERT(mp->m_rsumip != NULL);
  1048. error = xfs_rtmount_iread_extents(mp->m_rsumip, XFS_ILOCK_RTSUM);
  1049. if (error)
  1050. goto out_rele_summary;
  1051. error = xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
  1052. if (error)
  1053. goto out_rele_summary;
  1054. return 0;
  1055. out_rele_summary:
  1056. xfs_irele(mp->m_rsumip);
  1057. out_rele_bitmap:
  1058. xfs_irele(mp->m_rbmip);
  1059. return error;
  1060. }
  1061. void
  1062. xfs_rtunmount_inodes(
  1063. struct xfs_mount *mp)
  1064. {
  1065. kvfree(mp->m_rsum_cache);
  1066. if (mp->m_rbmip)
  1067. xfs_irele(mp->m_rbmip);
  1068. if (mp->m_rsumip)
  1069. xfs_irele(mp->m_rsumip);
  1070. }
  1071. /*
  1072. * Pick an extent for allocation at the start of a new realtime file.
  1073. * Use the sequence number stored in the atime field of the bitmap inode.
  1074. * Translate this to a fraction of the rtextents, and return the product
  1075. * of rtextents and the fraction.
  1076. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  1077. */
  1078. static xfs_rtxnum_t
  1079. xfs_rtpick_extent(
  1080. xfs_mount_t *mp, /* file system mount point */
  1081. xfs_trans_t *tp, /* transaction pointer */
  1082. xfs_rtxlen_t len) /* allocation length (rtextents) */
  1083. {
  1084. xfs_rtxnum_t b; /* result rtext */
  1085. int log2; /* log of sequence number */
  1086. uint64_t resid; /* residual after log removed */
  1087. uint64_t seq; /* sequence number of file creation */
  1088. struct timespec64 ts; /* timespec in inode */
  1089. xfs_assert_ilocked(mp->m_rbmip, XFS_ILOCK_EXCL);
  1090. ts = inode_get_atime(VFS_I(mp->m_rbmip));
  1091. if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
  1092. mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
  1093. seq = 0;
  1094. } else {
  1095. seq = ts.tv_sec;
  1096. }
  1097. if ((log2 = xfs_highbit64(seq)) == -1)
  1098. b = 0;
  1099. else {
  1100. resid = seq - (1ULL << log2);
  1101. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  1102. (log2 + 1);
  1103. if (b >= mp->m_sb.sb_rextents)
  1104. div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
  1105. if (b + len > mp->m_sb.sb_rextents)
  1106. b = mp->m_sb.sb_rextents - len;
  1107. }
  1108. ts.tv_sec = seq + 1;
  1109. inode_set_atime_to_ts(VFS_I(mp->m_rbmip), ts);
  1110. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1111. return b;
  1112. }
  1113. static void
  1114. xfs_rtalloc_align_minmax(
  1115. xfs_rtxlen_t *raminlen,
  1116. xfs_rtxlen_t *ramaxlen,
  1117. xfs_rtxlen_t *prod)
  1118. {
  1119. xfs_rtxlen_t newmaxlen = *ramaxlen;
  1120. xfs_rtxlen_t newminlen = *raminlen;
  1121. xfs_rtxlen_t slack;
  1122. slack = newmaxlen % *prod;
  1123. if (slack)
  1124. newmaxlen -= slack;
  1125. slack = newminlen % *prod;
  1126. if (slack)
  1127. newminlen += *prod - slack;
  1128. /*
  1129. * If adjusting for extent size hint alignment produces an invalid
  1130. * min/max len combination, go ahead without it.
  1131. */
  1132. if (newmaxlen < newminlen) {
  1133. *prod = 1;
  1134. return;
  1135. }
  1136. *ramaxlen = newmaxlen;
  1137. *raminlen = newminlen;
  1138. }
  1139. static int
  1140. xfs_rtallocate(
  1141. struct xfs_trans *tp,
  1142. xfs_rtblock_t bno_hint,
  1143. xfs_rtxlen_t minlen,
  1144. xfs_rtxlen_t maxlen,
  1145. xfs_rtxlen_t prod,
  1146. bool wasdel,
  1147. bool initial_user_data,
  1148. bool *rtlocked,
  1149. xfs_rtblock_t *bno,
  1150. xfs_extlen_t *blen)
  1151. {
  1152. struct xfs_rtalloc_args args = {
  1153. .mp = tp->t_mountp,
  1154. .tp = tp,
  1155. };
  1156. xfs_rtxnum_t start = 0;
  1157. xfs_rtxnum_t rtx;
  1158. xfs_rtxlen_t len = 0;
  1159. int error = 0;
  1160. /*
  1161. * Lock out modifications to both the RT bitmap and summary inodes.
  1162. */
  1163. if (!*rtlocked) {
  1164. xfs_rtbitmap_lock(args.mp);
  1165. xfs_rtbitmap_trans_join(tp);
  1166. *rtlocked = true;
  1167. }
  1168. /*
  1169. * For an allocation to an empty file at offset 0, pick an extent that
  1170. * will space things out in the rt area.
  1171. */
  1172. if (bno_hint != NULLFSBLOCK)
  1173. start = xfs_rtb_to_rtx(args.mp, bno_hint);
  1174. else if (initial_user_data)
  1175. start = xfs_rtpick_extent(args.mp, tp, maxlen);
  1176. if (start) {
  1177. error = xfs_rtallocate_extent_near(&args, start, minlen, maxlen,
  1178. &len, prod, &rtx);
  1179. /*
  1180. * If we can't allocate near a specific rt extent, try again
  1181. * without locality criteria.
  1182. */
  1183. if (error == -ENOSPC) {
  1184. xfs_rtbuf_cache_relse(&args);
  1185. error = 0;
  1186. }
  1187. }
  1188. if (!error) {
  1189. error = xfs_rtallocate_extent_size(&args, minlen, maxlen, &len,
  1190. prod, &rtx);
  1191. }
  1192. if (error)
  1193. goto out_release;
  1194. error = xfs_rtallocate_range(&args, rtx, len);
  1195. if (error)
  1196. goto out_release;
  1197. xfs_trans_mod_sb(tp, wasdel ?
  1198. XFS_TRANS_SB_RES_FREXTENTS : XFS_TRANS_SB_FREXTENTS,
  1199. -(long)len);
  1200. *bno = xfs_rtx_to_rtb(args.mp, rtx);
  1201. *blen = xfs_rtxlen_to_extlen(args.mp, len);
  1202. out_release:
  1203. xfs_rtbuf_cache_relse(&args);
  1204. return error;
  1205. }
  1206. static int
  1207. xfs_rtallocate_align(
  1208. struct xfs_bmalloca *ap,
  1209. xfs_rtxlen_t *ralen,
  1210. xfs_rtxlen_t *raminlen,
  1211. xfs_rtxlen_t *prod,
  1212. bool *noalign)
  1213. {
  1214. struct xfs_mount *mp = ap->ip->i_mount;
  1215. xfs_fileoff_t orig_offset = ap->offset;
  1216. xfs_extlen_t minlen = mp->m_sb.sb_rextsize;
  1217. xfs_extlen_t align; /* minimum allocation alignment */
  1218. xfs_extlen_t mod; /* product factor for allocators */
  1219. int error;
  1220. if (*noalign) {
  1221. align = mp->m_sb.sb_rextsize;
  1222. } else {
  1223. align = xfs_get_extsz_hint(ap->ip);
  1224. if (!align)
  1225. align = 1;
  1226. if (align == mp->m_sb.sb_rextsize)
  1227. *noalign = true;
  1228. }
  1229. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 1,
  1230. ap->eof, 0, ap->conv, &ap->offset, &ap->length);
  1231. if (error)
  1232. return error;
  1233. ASSERT(ap->length);
  1234. ASSERT(xfs_extlen_to_rtxmod(mp, ap->length) == 0);
  1235. /*
  1236. * If we shifted the file offset downward to satisfy an extent size
  1237. * hint, increase minlen by that amount so that the allocator won't
  1238. * give us an allocation that's too short to cover at least one of the
  1239. * blocks that the caller asked for.
  1240. */
  1241. if (ap->offset != orig_offset)
  1242. minlen += orig_offset - ap->offset;
  1243. /*
  1244. * Set ralen to be the actual requested length in rtextents.
  1245. *
  1246. * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
  1247. * we rounded up to it, cut it back so it's valid again.
  1248. * Note that if it's a really large request (bigger than
  1249. * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
  1250. * adjust the starting point to match it.
  1251. */
  1252. *ralen = xfs_extlen_to_rtxlen(mp, min(ap->length, XFS_MAX_BMBT_EXTLEN));
  1253. *raminlen = max_t(xfs_rtxlen_t, 1, xfs_extlen_to_rtxlen(mp, minlen));
  1254. ASSERT(*raminlen > 0);
  1255. ASSERT(*raminlen <= *ralen);
  1256. /*
  1257. * Only bother calculating a real prod factor if offset & length are
  1258. * perfectly aligned, otherwise it will just get us in trouble.
  1259. */
  1260. div_u64_rem(ap->offset, align, &mod);
  1261. if (mod || ap->length % align)
  1262. *prod = 1;
  1263. else
  1264. *prod = xfs_extlen_to_rtxlen(mp, align);
  1265. if (*prod > 1)
  1266. xfs_rtalloc_align_minmax(raminlen, ralen, prod);
  1267. return 0;
  1268. }
  1269. int
  1270. xfs_bmap_rtalloc(
  1271. struct xfs_bmalloca *ap)
  1272. {
  1273. xfs_fileoff_t orig_offset = ap->offset;
  1274. xfs_rtxlen_t prod = 0; /* product factor for allocators */
  1275. xfs_rtxlen_t ralen = 0; /* realtime allocation length */
  1276. xfs_rtblock_t bno_hint = NULLRTBLOCK;
  1277. xfs_extlen_t orig_length = ap->length;
  1278. xfs_rtxlen_t raminlen;
  1279. bool rtlocked = false;
  1280. bool noalign = false;
  1281. bool initial_user_data =
  1282. ap->datatype & XFS_ALLOC_INITIAL_USER_DATA;
  1283. int error;
  1284. retry:
  1285. error = xfs_rtallocate_align(ap, &ralen, &raminlen, &prod, &noalign);
  1286. if (error)
  1287. return error;
  1288. if (xfs_bmap_adjacent(ap))
  1289. bno_hint = ap->blkno;
  1290. error = xfs_rtallocate(ap->tp, bno_hint, raminlen, ralen, prod,
  1291. ap->wasdel, initial_user_data, &rtlocked,
  1292. &ap->blkno, &ap->length);
  1293. if (error == -ENOSPC) {
  1294. if (!noalign) {
  1295. /*
  1296. * We previously enlarged the request length to try to
  1297. * satisfy an extent size hint. The allocator didn't
  1298. * return anything, so reset the parameters to the
  1299. * original values and try again without alignment
  1300. * criteria.
  1301. */
  1302. ap->offset = orig_offset;
  1303. ap->length = orig_length;
  1304. noalign = true;
  1305. goto retry;
  1306. }
  1307. ap->blkno = NULLFSBLOCK;
  1308. ap->length = 0;
  1309. return 0;
  1310. }
  1311. if (error)
  1312. return error;
  1313. xfs_bmap_alloc_account(ap);
  1314. return 0;
  1315. }