xfs_rtalloc.c 34 KB

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