xfs_rtalloc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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 /* error */
  740. xfs_growfs_rt_alloc(
  741. xfs_mount_t *mp, /* file system mount point */
  742. xfs_extlen_t oblocks, /* old count of blocks */
  743. xfs_extlen_t nblocks, /* new count of blocks */
  744. xfs_inode_t *ip) /* inode (bitmap/summary) */
  745. {
  746. xfs_fileoff_t bno; /* block number in file */
  747. xfs_buf_t *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. xfs_bmap_free_t flist; /* list of freed blocks */
  753. xfs_fsblock_t fsbno; /* filesystem block for bno */
  754. xfs_bmbt_irec_t map; /* block map output */
  755. int nmap; /* number of block maps */
  756. int resblks; /* space reservation */
  757. /*
  758. * Allocate space to the file, as necessary.
  759. */
  760. while (oblocks < nblocks) {
  761. int cancelflags = 0;
  762. xfs_trans_t *tp;
  763. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
  764. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  765. /*
  766. * Reserve space & log for one extent added to the file.
  767. */
  768. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtalloc,
  769. resblks, 0);
  770. if (error)
  771. goto error_cancel;
  772. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  773. /*
  774. * Lock the inode.
  775. */
  776. xfs_ilock(ip, XFS_ILOCK_EXCL);
  777. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  778. xfs_bmap_init(&flist, &firstblock);
  779. /*
  780. * Allocate blocks to the bitmap file.
  781. */
  782. nmap = 1;
  783. cancelflags |= XFS_TRANS_ABORT;
  784. error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
  785. XFS_BMAPI_METADATA, &firstblock,
  786. resblks, &map, &nmap, &flist);
  787. if (!error && nmap < 1)
  788. error = -ENOSPC;
  789. if (error)
  790. goto error_cancel;
  791. /*
  792. * Free any blocks freed up in the transaction, then commit.
  793. */
  794. error = xfs_bmap_finish(&tp, &flist, &committed);
  795. if (error)
  796. goto error_cancel;
  797. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  798. if (error)
  799. goto error;
  800. /*
  801. * Now we need to clear the allocated blocks.
  802. * Do this one block per transaction, to keep it simple.
  803. */
  804. cancelflags = 0;
  805. for (bno = map.br_startoff, fsbno = map.br_startblock;
  806. bno < map.br_startoff + map.br_blockcount;
  807. bno++, fsbno++) {
  808. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
  809. /*
  810. * Reserve log for one block zeroing.
  811. */
  812. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
  813. 0, 0);
  814. if (error)
  815. goto error_cancel;
  816. /*
  817. * Lock the bitmap inode.
  818. */
  819. xfs_ilock(ip, XFS_ILOCK_EXCL);
  820. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  821. /*
  822. * Get a buffer for the block.
  823. */
  824. d = XFS_FSB_TO_DADDR(mp, fsbno);
  825. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  826. mp->m_bsize, 0);
  827. if (bp == NULL) {
  828. error = -EIO;
  829. error_cancel:
  830. xfs_trans_cancel(tp, cancelflags);
  831. goto error;
  832. }
  833. memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
  834. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  835. /*
  836. * Commit the transaction.
  837. */
  838. error = xfs_trans_commit(tp, 0);
  839. if (error)
  840. goto error;
  841. }
  842. /*
  843. * Go on to the next extent, if any.
  844. */
  845. oblocks = map.br_startoff + map.br_blockcount;
  846. }
  847. return 0;
  848. error:
  849. return error;
  850. }
  851. /*
  852. * Visible (exported) functions.
  853. */
  854. /*
  855. * Grow the realtime area of the filesystem.
  856. */
  857. int
  858. xfs_growfs_rt(
  859. xfs_mount_t *mp, /* mount point for filesystem */
  860. xfs_growfs_rt_t *in) /* growfs rt input struct */
  861. {
  862. xfs_rtblock_t bmbno; /* bitmap block number */
  863. xfs_buf_t *bp; /* temporary buffer */
  864. int error; /* error return value */
  865. xfs_mount_t *nmp; /* new (fake) mount structure */
  866. xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
  867. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  868. xfs_rtblock_t nrextents; /* new number of realtime extents */
  869. uint8_t nrextslog; /* new log2 of sb_rextents */
  870. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  871. uint nrsumlevels; /* new rt summary levels */
  872. uint nrsumsize; /* new size of rt summary, bytes */
  873. xfs_sb_t *nsbp; /* new superblock */
  874. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  875. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  876. xfs_sb_t *sbp; /* old superblock */
  877. xfs_fsblock_t sumbno; /* summary block number */
  878. sbp = &mp->m_sb;
  879. /*
  880. * Initial error checking.
  881. */
  882. if (!capable(CAP_SYS_ADMIN))
  883. return -EPERM;
  884. if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
  885. (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
  886. (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
  887. return -EINVAL;
  888. if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
  889. return error;
  890. /*
  891. * Read in the last block of the device, make sure it exists.
  892. */
  893. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  894. XFS_FSB_TO_BB(mp, nrblocks - 1),
  895. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  896. if (error)
  897. return error;
  898. xfs_buf_relse(bp);
  899. /*
  900. * Calculate new parameters. These are the final values to be reached.
  901. */
  902. nrextents = nrblocks;
  903. do_div(nrextents, in->extsize);
  904. nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
  905. nrextslog = xfs_highbit32(nrextents);
  906. nrsumlevels = nrextslog + 1;
  907. nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
  908. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  909. nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  910. /*
  911. * New summary size can't be more than half the size of
  912. * the log. This prevents us from getting a log overflow,
  913. * since we'll log basically the whole summary file at once.
  914. */
  915. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
  916. return -EINVAL;
  917. /*
  918. * Get the old block counts for bitmap and summary inodes.
  919. * These can't change since other growfs callers are locked out.
  920. */
  921. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
  922. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
  923. /*
  924. * Allocate space to the bitmap and summary files, as necessary.
  925. */
  926. error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
  927. if (error)
  928. return error;
  929. error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
  930. if (error)
  931. return error;
  932. /*
  933. * Allocate a new (fake) mount/sb.
  934. */
  935. nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
  936. /*
  937. * Loop over the bitmap blocks.
  938. * We will do everything one bitmap block at a time.
  939. * Skip the current block if it is exactly full.
  940. * This also deals with the case where there were no rtextents before.
  941. */
  942. for (bmbno = sbp->sb_rbmblocks -
  943. ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
  944. bmbno < nrbmblocks;
  945. bmbno++) {
  946. xfs_trans_t *tp;
  947. int cancelflags = 0;
  948. *nmp = *mp;
  949. nsbp = &nmp->m_sb;
  950. /*
  951. * Calculate new sb and mount fields for this round.
  952. */
  953. nsbp->sb_rextsize = in->extsize;
  954. nsbp->sb_rbmblocks = bmbno + 1;
  955. nsbp->sb_rblocks =
  956. XFS_RTMIN(nrblocks,
  957. nsbp->sb_rbmblocks * NBBY *
  958. nsbp->sb_blocksize * nsbp->sb_rextsize);
  959. nsbp->sb_rextents = nsbp->sb_rblocks;
  960. do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
  961. ASSERT(nsbp->sb_rextents != 0);
  962. nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
  963. nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
  964. nrsumsize =
  965. (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
  966. nsbp->sb_rbmblocks;
  967. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  968. nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  969. /*
  970. * Start a transaction, get the log reservation.
  971. */
  972. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
  973. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtfree,
  974. 0, 0);
  975. if (error)
  976. goto error_cancel;
  977. /*
  978. * Lock out other callers by grabbing the bitmap inode lock.
  979. */
  980. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  981. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  982. /*
  983. * Update the bitmap inode's size.
  984. */
  985. mp->m_rbmip->i_d.di_size =
  986. nsbp->sb_rbmblocks * nsbp->sb_blocksize;
  987. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  988. cancelflags |= XFS_TRANS_ABORT;
  989. /*
  990. * Get the summary inode into the transaction.
  991. */
  992. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
  993. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  994. /*
  995. * Update the summary inode's size.
  996. */
  997. mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
  998. xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
  999. /*
  1000. * Copy summary data from old to new sizes.
  1001. * Do this when the real size (not block-aligned) changes.
  1002. */
  1003. if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
  1004. mp->m_rsumlevels != nmp->m_rsumlevels) {
  1005. error = xfs_rtcopy_summary(mp, nmp, tp);
  1006. if (error)
  1007. goto error_cancel;
  1008. }
  1009. /*
  1010. * Update superblock fields.
  1011. */
  1012. if (nsbp->sb_rextsize != sbp->sb_rextsize)
  1013. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  1014. nsbp->sb_rextsize - sbp->sb_rextsize);
  1015. if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
  1016. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  1017. nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
  1018. if (nsbp->sb_rblocks != sbp->sb_rblocks)
  1019. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  1020. nsbp->sb_rblocks - sbp->sb_rblocks);
  1021. if (nsbp->sb_rextents != sbp->sb_rextents)
  1022. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  1023. nsbp->sb_rextents - sbp->sb_rextents);
  1024. if (nsbp->sb_rextslog != sbp->sb_rextslog)
  1025. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  1026. nsbp->sb_rextslog - sbp->sb_rextslog);
  1027. /*
  1028. * Free new extent.
  1029. */
  1030. bp = NULL;
  1031. error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
  1032. nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
  1033. if (error) {
  1034. error_cancel:
  1035. xfs_trans_cancel(tp, cancelflags);
  1036. break;
  1037. }
  1038. /*
  1039. * Mark more blocks free in the superblock.
  1040. */
  1041. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
  1042. nsbp->sb_rextents - sbp->sb_rextents);
  1043. /*
  1044. * Update mp values into the real mp structure.
  1045. */
  1046. mp->m_rsumlevels = nrsumlevels;
  1047. mp->m_rsumsize = nrsumsize;
  1048. error = xfs_trans_commit(tp, 0);
  1049. if (error)
  1050. break;
  1051. }
  1052. /*
  1053. * Free the fake mp structure.
  1054. */
  1055. kmem_free(nmp);
  1056. return error;
  1057. }
  1058. /*
  1059. * Allocate an extent in the realtime subvolume, with the usual allocation
  1060. * parameters. The length units are all in realtime extents, as is the
  1061. * result block number.
  1062. */
  1063. int /* error */
  1064. xfs_rtallocate_extent(
  1065. xfs_trans_t *tp, /* transaction pointer */
  1066. xfs_rtblock_t bno, /* starting block number to allocate */
  1067. xfs_extlen_t minlen, /* minimum length to allocate */
  1068. xfs_extlen_t maxlen, /* maximum length to allocate */
  1069. xfs_extlen_t *len, /* out: actual length allocated */
  1070. xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
  1071. int wasdel, /* was a delayed allocation extent */
  1072. xfs_extlen_t prod, /* extent product factor */
  1073. xfs_rtblock_t *rtblock) /* out: start block allocated */
  1074. {
  1075. xfs_mount_t *mp = tp->t_mountp;
  1076. int error; /* error value */
  1077. xfs_rtblock_t r; /* result allocated block */
  1078. xfs_fsblock_t sb; /* summary file block number */
  1079. xfs_buf_t *sumbp; /* summary file block buffer */
  1080. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  1081. ASSERT(minlen > 0 && minlen <= maxlen);
  1082. /*
  1083. * If prod is set then figure out what to do to minlen and maxlen.
  1084. */
  1085. if (prod > 1) {
  1086. xfs_extlen_t i;
  1087. if ((i = maxlen % prod))
  1088. maxlen -= i;
  1089. if ((i = minlen % prod))
  1090. minlen += prod - i;
  1091. if (maxlen < minlen) {
  1092. *rtblock = NULLRTBLOCK;
  1093. return 0;
  1094. }
  1095. }
  1096. sumbp = NULL;
  1097. /*
  1098. * Allocate by size, or near another block, or exactly at some block.
  1099. */
  1100. switch (type) {
  1101. case XFS_ALLOCTYPE_ANY_AG:
  1102. error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
  1103. &sumbp, &sb, prod, &r);
  1104. break;
  1105. case XFS_ALLOCTYPE_NEAR_BNO:
  1106. error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
  1107. len, &sumbp, &sb, prod, &r);
  1108. break;
  1109. case XFS_ALLOCTYPE_THIS_BNO:
  1110. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
  1111. len, &sumbp, &sb, prod, &r);
  1112. break;
  1113. default:
  1114. error = -EIO;
  1115. ASSERT(0);
  1116. }
  1117. if (error)
  1118. return error;
  1119. /*
  1120. * If it worked, update the superblock.
  1121. */
  1122. if (r != NULLRTBLOCK) {
  1123. long slen = (long)*len;
  1124. ASSERT(*len >= minlen && *len <= maxlen);
  1125. if (wasdel)
  1126. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
  1127. else
  1128. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
  1129. }
  1130. *rtblock = r;
  1131. return 0;
  1132. }
  1133. /*
  1134. * Initialize realtime fields in the mount structure.
  1135. */
  1136. int /* error */
  1137. xfs_rtmount_init(
  1138. struct xfs_mount *mp) /* file system mount structure */
  1139. {
  1140. struct xfs_buf *bp; /* buffer for last block of subvolume */
  1141. struct xfs_sb *sbp; /* filesystem superblock copy in mount */
  1142. xfs_daddr_t d; /* address of last block of subvolume */
  1143. int error;
  1144. sbp = &mp->m_sb;
  1145. if (sbp->sb_rblocks == 0)
  1146. return 0;
  1147. if (mp->m_rtdev_targp == NULL) {
  1148. xfs_warn(mp,
  1149. "Filesystem has a realtime volume, use rtdev=device option");
  1150. return -ENODEV;
  1151. }
  1152. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  1153. mp->m_rsumsize =
  1154. (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
  1155. sbp->sb_rbmblocks;
  1156. mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
  1157. mp->m_rbmip = mp->m_rsumip = NULL;
  1158. /*
  1159. * Check that the realtime section is an ok size.
  1160. */
  1161. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  1162. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  1163. xfs_warn(mp, "realtime mount -- %llu != %llu",
  1164. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  1165. (unsigned long long) mp->m_sb.sb_rblocks);
  1166. return -EFBIG;
  1167. }
  1168. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  1169. d - XFS_FSB_TO_BB(mp, 1),
  1170. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  1171. if (error) {
  1172. xfs_warn(mp, "realtime device size check failed");
  1173. return error;
  1174. }
  1175. xfs_buf_relse(bp);
  1176. return 0;
  1177. }
  1178. /*
  1179. * Get the bitmap and summary inodes into the mount structure
  1180. * at mount time.
  1181. */
  1182. int /* error */
  1183. xfs_rtmount_inodes(
  1184. xfs_mount_t *mp) /* file system mount structure */
  1185. {
  1186. int error; /* error return value */
  1187. xfs_sb_t *sbp;
  1188. sbp = &mp->m_sb;
  1189. if (sbp->sb_rbmino == NULLFSINO)
  1190. return 0;
  1191. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
  1192. if (error)
  1193. return error;
  1194. ASSERT(mp->m_rbmip != NULL);
  1195. ASSERT(sbp->sb_rsumino != NULLFSINO);
  1196. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
  1197. if (error) {
  1198. IRELE(mp->m_rbmip);
  1199. return error;
  1200. }
  1201. ASSERT(mp->m_rsumip != NULL);
  1202. return 0;
  1203. }
  1204. void
  1205. xfs_rtunmount_inodes(
  1206. struct xfs_mount *mp)
  1207. {
  1208. if (mp->m_rbmip)
  1209. IRELE(mp->m_rbmip);
  1210. if (mp->m_rsumip)
  1211. IRELE(mp->m_rsumip);
  1212. }
  1213. /*
  1214. * Pick an extent for allocation at the start of a new realtime file.
  1215. * Use the sequence number stored in the atime field of the bitmap inode.
  1216. * Translate this to a fraction of the rtextents, and return the product
  1217. * of rtextents and the fraction.
  1218. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  1219. */
  1220. int /* error */
  1221. xfs_rtpick_extent(
  1222. xfs_mount_t *mp, /* file system mount point */
  1223. xfs_trans_t *tp, /* transaction pointer */
  1224. xfs_extlen_t len, /* allocation length (rtextents) */
  1225. xfs_rtblock_t *pick) /* result rt extent */
  1226. {
  1227. xfs_rtblock_t b; /* result block */
  1228. int log2; /* log of sequence number */
  1229. __uint64_t resid; /* residual after log removed */
  1230. __uint64_t seq; /* sequence number of file creation */
  1231. __uint64_t *seqp; /* pointer to seqno in inode */
  1232. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  1233. seqp = (__uint64_t *)&mp->m_rbmip->i_d.di_atime;
  1234. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
  1235. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  1236. *seqp = 0;
  1237. }
  1238. seq = *seqp;
  1239. if ((log2 = xfs_highbit64(seq)) == -1)
  1240. b = 0;
  1241. else {
  1242. resid = seq - (1ULL << log2);
  1243. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  1244. (log2 + 1);
  1245. if (b >= mp->m_sb.sb_rextents)
  1246. b = do_mod(b, mp->m_sb.sb_rextents);
  1247. if (b + len > mp->m_sb.sb_rextents)
  1248. b = mp->m_sb.sb_rextents - len;
  1249. }
  1250. *seqp = seq + 1;
  1251. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1252. *pick = b;
  1253. return 0;
  1254. }