xfs_rtalloc.c 35 KB

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