budget.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Adrian Hunter
  20. * Artem Bityutskiy (Битюцкий Артём)
  21. */
  22. /*
  23. * This file implements the budgeting sub-system which is responsible for UBIFS
  24. * space management.
  25. *
  26. * Factors such as compression, wasted space at the ends of LEBs, space in other
  27. * journal heads, the effect of updates on the index, and so on, make it
  28. * impossible to accurately predict the amount of space needed. Consequently
  29. * approximations are used.
  30. */
  31. #include "ubifs.h"
  32. #include <linux/math64.h>
  33. /**
  34. * ubifs_calc_min_idx_lebs - calculate amount of eraseblocks for the index.
  35. * @c: UBIFS file-system description object
  36. *
  37. * This function calculates and returns the number of eraseblocks which should
  38. * be kept for index usage.
  39. */
  40. int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
  41. {
  42. int idx_lebs, eff_leb_size = c->leb_size - c->max_idx_node_sz;
  43. long long idx_size;
  44. idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
  45. /* And make sure we have thrice the index size of space reserved */
  46. idx_size = idx_size + (idx_size << 1);
  47. /*
  48. * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
  49. * pair, nor similarly the two variables for the new index size, so we
  50. * have to do this costly 64-bit division on fast-path.
  51. */
  52. idx_size += eff_leb_size - 1;
  53. idx_lebs = div_u64(idx_size, eff_leb_size);
  54. /*
  55. * The index head is not available for the in-the-gaps method, so add an
  56. * extra LEB to compensate.
  57. */
  58. idx_lebs += 1;
  59. if (idx_lebs < MIN_INDEX_LEBS)
  60. idx_lebs = MIN_INDEX_LEBS;
  61. return idx_lebs;
  62. }
  63. /**
  64. * ubifs_reported_space - calculate reported free space.
  65. * @c: the UBIFS file-system description object
  66. * @free: amount of free space
  67. *
  68. * This function calculates amount of free space which will be reported to
  69. * user-space. User-space application tend to expect that if the file-system
  70. * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
  71. * are able to write a file of size N. UBIFS attaches node headers to each data
  72. * node and it has to write indexing nodes as well. This introduces additional
  73. * overhead, and UBIFS has to report slightly less free space to meet the above
  74. * expectations.
  75. *
  76. * This function assumes free space is made up of uncompressed data nodes and
  77. * full index nodes (one per data node, tripled because we always allow enough
  78. * space to write the index thrice).
  79. *
  80. * Note, the calculation is pessimistic, which means that most of the time
  81. * UBIFS reports less space than it actually has.
  82. */
  83. long long ubifs_reported_space(const struct ubifs_info *c, long long free)
  84. {
  85. int divisor, factor, f;
  86. /*
  87. * Reported space size is @free * X, where X is UBIFS block size
  88. * divided by UBIFS block size + all overhead one data block
  89. * introduces. The overhead is the node header + indexing overhead.
  90. *
  91. * Indexing overhead calculations are based on the following formula:
  92. * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number
  93. * of data nodes, f - fanout. Because effective UBIFS fanout is twice
  94. * as less than maximum fanout, we assume that each data node
  95. * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes.
  96. * Note, the multiplier 3 is because UBIFS reserves thrice as more space
  97. * for the index.
  98. */
  99. f = c->fanout > 3 ? c->fanout >> 1 : 2;
  100. factor = UBIFS_BLOCK_SIZE;
  101. divisor = UBIFS_MAX_DATA_NODE_SZ;
  102. divisor += (c->max_idx_node_sz * 3) / (f - 1);
  103. free *= factor;
  104. return div_u64(free, divisor);
  105. }