xfs_rtalloc.c 35 KB

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