xfs_rtalloc.c 38 KB

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