Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of UBIFS. |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 6 | * |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 7 | * Authors: Adrian Hunter |
| 8 | * Artem Bityutskiy (Битюцкий Артём) |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This file implements the budgeting sub-system which is responsible for UBIFS |
| 13 | * space management. |
| 14 | * |
| 15 | * Factors such as compression, wasted space at the ends of LEBs, space in other |
| 16 | * journal heads, the effect of updates on the index, and so on, make it |
| 17 | * impossible to accurately predict the amount of space needed. Consequently |
| 18 | * approximations are used. |
| 19 | */ |
| 20 | |
| 21 | #include "ubifs.h" |
| 22 | #include <linux/writeback.h> |
Artem Bityutskiy | 4d61db4 | 2008-12-18 14:06:51 +0200 | [diff] [blame] | 23 | #include <linux/math64.h> |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * When pessimistic budget calculations say that there is no enough space, |
| 27 | * UBIFS starts writing back dirty inodes and pages, doing garbage collection, |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 28 | * or committing. The below constant defines maximum number of times UBIFS |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 29 | * repeats the operations. |
| 30 | */ |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 31 | #define MAX_MKSPC_RETRIES 3 |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * The below constant defines amount of dirty pages which should be written |
| 35 | * back at when trying to shrink the liability. |
| 36 | */ |
| 37 | #define NR_TO_WRITE 16 |
| 38 | |
| 39 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 40 | * shrink_liability - write-back some dirty pages/inodes. |
| 41 | * @c: UBIFS file-system description object |
| 42 | * @nr_to_write: how many dirty pages to write-back |
| 43 | * |
| 44 | * This function shrinks UBIFS liability by means of writing back some amount |
Jens Axboe | b6e5131 | 2009-09-16 15:13:54 +0200 | [diff] [blame] | 45 | * of dirty inodes and their pages. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 46 | * |
| 47 | * Note, this function synchronizes even VFS inodes which are locked |
| 48 | * (@i_mutex) by the caller of the budgeting function, because write-back does |
| 49 | * not touch @i_mutex. |
| 50 | */ |
Jens Axboe | b6e5131 | 2009-09-16 15:13:54 +0200 | [diff] [blame] | 51 | static void shrink_liability(struct ubifs_info *c, int nr_to_write) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 52 | { |
Christoph Hellwig | cf37e97 | 2010-06-08 18:14:51 +0200 | [diff] [blame] | 53 | down_read(&c->vfs_sb->s_umount); |
Liu Song | 0af83ab | 2019-08-06 22:21:40 +0800 | [diff] [blame] | 54 | writeback_inodes_sb_nr(c->vfs_sb, nr_to_write, WB_REASON_FS_FREE_SPACE); |
Christoph Hellwig | cf37e97 | 2010-06-08 18:14:51 +0200 | [diff] [blame] | 55 | up_read(&c->vfs_sb->s_umount); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 56 | } |
| 57 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 58 | /** |
| 59 | * run_gc - run garbage collector. |
| 60 | * @c: UBIFS file-system description object |
| 61 | * |
| 62 | * This function runs garbage collector to make some more free space. Returns |
| 63 | * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a |
| 64 | * negative error code in case of failure. |
| 65 | */ |
| 66 | static int run_gc(struct ubifs_info *c) |
| 67 | { |
| 68 | int err, lnum; |
| 69 | |
| 70 | /* Make some free space by garbage-collecting dirty space */ |
| 71 | down_read(&c->commit_sem); |
| 72 | lnum = ubifs_garbage_collect(c, 1); |
| 73 | up_read(&c->commit_sem); |
| 74 | if (lnum < 0) |
| 75 | return lnum; |
| 76 | |
| 77 | /* GC freed one LEB, return it to lprops */ |
| 78 | dbg_budg("GC freed LEB %d", lnum); |
| 79 | err = ubifs_return_leb(c, lnum); |
| 80 | if (err) |
| 81 | return err; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /** |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 86 | * get_liability - calculate current liability. |
| 87 | * @c: UBIFS file-system description object |
| 88 | * |
| 89 | * This function calculates and returns current UBIFS liability, i.e. the |
| 90 | * amount of bytes UBIFS has "promised" to write to the media. |
| 91 | */ |
| 92 | static long long get_liability(struct ubifs_info *c) |
| 93 | { |
| 94 | long long liab; |
| 95 | |
| 96 | spin_lock(&c->space_lock); |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 97 | liab = c->bi.idx_growth + c->bi.data_growth + c->bi.dd_growth; |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 98 | spin_unlock(&c->space_lock); |
| 99 | return liab; |
| 100 | } |
| 101 | |
| 102 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 103 | * make_free_space - make more free space on the file-system. |
| 104 | * @c: UBIFS file-system description object |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 105 | * |
| 106 | * This function is called when an operation cannot be budgeted because there |
| 107 | * is supposedly no free space. But in most cases there is some free space: |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 108 | * o budgeting is pessimistic, so it always budgets more than it is actually |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 109 | * needed, so shrinking the liability is one way to make free space - the |
| 110 | * cached data will take less space then it was budgeted for; |
| 111 | * o GC may turn some dark space into free space (budgeting treats dark space |
| 112 | * as not available); |
| 113 | * o commit may free some LEB, i.e., turn freeable LEBs into free LEBs. |
| 114 | * |
| 115 | * So this function tries to do the above. Returns %-EAGAIN if some free space |
| 116 | * was presumably made and the caller has to re-try budgeting the operation. |
| 117 | * Returns %-ENOSPC if it couldn't do more free space, and other negative error |
| 118 | * codes on failures. |
| 119 | */ |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 120 | static int make_free_space(struct ubifs_info *c) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 121 | { |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 122 | int err, retries = 0; |
| 123 | long long liab1, liab2; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 124 | |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 125 | do { |
| 126 | liab1 = get_liability(c); |
| 127 | /* |
| 128 | * We probably have some dirty pages or inodes (liability), try |
| 129 | * to write them back. |
| 130 | */ |
| 131 | dbg_budg("liability %lld, run write-back", liab1); |
| 132 | shrink_liability(c, NR_TO_WRITE); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 133 | |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 134 | liab2 = get_liability(c); |
| 135 | if (liab2 < liab1) |
| 136 | return -EAGAIN; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 137 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 138 | dbg_budg("new liability %lld (not shrunk)", liab2); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 139 | |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 140 | /* Liability did not shrink again, try GC */ |
| 141 | dbg_budg("Run GC"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 142 | err = run_gc(c); |
| 143 | if (!err) |
| 144 | return -EAGAIN; |
| 145 | |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 146 | if (err != -EAGAIN && err != -ENOSPC) |
| 147 | /* Some real error happened */ |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 148 | return err; |
| 149 | |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 150 | dbg_budg("Run commit (retries %d)", retries); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 151 | err = ubifs_run_commit(c); |
| 152 | if (err) |
| 153 | return err; |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 154 | } while (retries++ < MAX_MKSPC_RETRIES); |
| 155 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 156 | return -ENOSPC; |
| 157 | } |
| 158 | |
| 159 | /** |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 160 | * ubifs_calc_min_idx_lebs - calculate amount of LEBs for the index. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 161 | * @c: UBIFS file-system description object |
| 162 | * |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 163 | * This function calculates and returns the number of LEBs which should be kept |
| 164 | * for index usage. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 165 | */ |
| 166 | int ubifs_calc_min_idx_lebs(struct ubifs_info *c) |
| 167 | { |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 168 | int idx_lebs; |
Artem Bityutskiy | 4d61db4 | 2008-12-18 14:06:51 +0200 | [diff] [blame] | 169 | long long idx_size; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 170 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 171 | idx_size = c->bi.old_idx_sz + c->bi.idx_growth + c->bi.uncommitted_idx; |
Adrian Hunter | 3a13252c | 2008-07-30 12:18:02 +0300 | [diff] [blame] | 172 | /* And make sure we have thrice the index size of space reserved */ |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 173 | idx_size += idx_size << 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 174 | /* |
| 175 | * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes' |
| 176 | * pair, nor similarly the two variables for the new index size, so we |
| 177 | * have to do this costly 64-bit division on fast-path. |
| 178 | */ |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 179 | idx_lebs = div_u64(idx_size + c->idx_leb_size - 1, c->idx_leb_size); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 180 | /* |
| 181 | * The index head is not available for the in-the-gaps method, so add an |
| 182 | * extra LEB to compensate. |
| 183 | */ |
Artem Bityutskiy | 4d61db4 | 2008-12-18 14:06:51 +0200 | [diff] [blame] | 184 | idx_lebs += 1; |
| 185 | if (idx_lebs < MIN_INDEX_LEBS) |
| 186 | idx_lebs = MIN_INDEX_LEBS; |
| 187 | return idx_lebs; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * ubifs_calc_available - calculate available FS space. |
| 192 | * @c: UBIFS file-system description object |
| 193 | * @min_idx_lebs: minimum number of LEBs reserved for the index |
| 194 | * |
| 195 | * This function calculates and returns amount of FS space available for use. |
| 196 | */ |
| 197 | long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs) |
| 198 | { |
| 199 | int subtract_lebs; |
| 200 | long long available; |
| 201 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 202 | available = c->main_bytes - c->lst.total_used; |
| 203 | |
| 204 | /* |
| 205 | * Now 'available' contains theoretically available flash space |
| 206 | * assuming there is no index, so we have to subtract the space which |
| 207 | * is reserved for the index. |
| 208 | */ |
| 209 | subtract_lebs = min_idx_lebs; |
| 210 | |
| 211 | /* Take into account that GC reserves one LEB for its own needs */ |
| 212 | subtract_lebs += 1; |
| 213 | |
| 214 | /* |
| 215 | * The GC journal head LEB is not really accessible. And since |
| 216 | * different write types go to different heads, we may count only on |
| 217 | * one head's space. |
| 218 | */ |
| 219 | subtract_lebs += c->jhead_cnt - 1; |
| 220 | |
| 221 | /* We also reserve one LEB for deletions, which bypass budgeting */ |
| 222 | subtract_lebs += 1; |
| 223 | |
| 224 | available -= (long long)subtract_lebs * c->leb_size; |
| 225 | |
| 226 | /* Subtract the dead space which is not available for use */ |
| 227 | available -= c->lst.total_dead; |
| 228 | |
| 229 | /* |
| 230 | * Subtract dark space, which might or might not be usable - it depends |
| 231 | * on the data which we have on the media and which will be written. If |
| 232 | * this is a lot of uncompressed or not-compressible data, the dark |
| 233 | * space cannot be used. |
| 234 | */ |
| 235 | available -= c->lst.total_dark; |
| 236 | |
| 237 | /* |
| 238 | * However, there is more dark space. The index may be bigger than |
| 239 | * @min_idx_lebs. Those extra LEBs are assumed to be available, but |
| 240 | * their dark space is not included in total_dark, so it is subtracted |
| 241 | * here. |
| 242 | */ |
| 243 | if (c->lst.idx_lebs > min_idx_lebs) { |
| 244 | subtract_lebs = c->lst.idx_lebs - min_idx_lebs; |
| 245 | available -= subtract_lebs * c->dark_wm; |
| 246 | } |
| 247 | |
| 248 | /* The calculations are rough and may end up with a negative number */ |
| 249 | return available > 0 ? available : 0; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * can_use_rp - check whether the user is allowed to use reserved pool. |
| 254 | * @c: UBIFS file-system description object |
| 255 | * |
| 256 | * UBIFS has so-called "reserved pool" which is flash space reserved |
| 257 | * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock. |
| 258 | * This function checks whether current user is allowed to use reserved pool. |
| 259 | * Returns %1 current user is allowed to use reserved pool and %0 otherwise. |
| 260 | */ |
| 261 | static int can_use_rp(struct ubifs_info *c) |
| 262 | { |
Eric W. Biederman | 39241be | 2012-02-07 15:50:56 -0800 | [diff] [blame] | 263 | if (uid_eq(current_fsuid(), c->rp_uid) || capable(CAP_SYS_RESOURCE) || |
| 264 | (!gid_eq(c->rp_gid, GLOBAL_ROOT_GID) && in_group_p(c->rp_gid))) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 265 | return 1; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * do_budget_space - reserve flash space for index and data growth. |
| 271 | * @c: UBIFS file-system description object |
| 272 | * |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 273 | * This function makes sure UBIFS has enough free LEBs for index growth and |
| 274 | * data. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 275 | * |
Adrian Hunter | 3a13252c | 2008-07-30 12:18:02 +0300 | [diff] [blame] | 276 | * When budgeting index space, UBIFS reserves thrice as many LEBs as the index |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 277 | * would take if it was consolidated and written to the flash. This guarantees |
| 278 | * that the "in-the-gaps" commit method always succeeds and UBIFS will always |
| 279 | * be able to commit dirty index. So this function basically adds amount of |
Artem Bityutskiy | b364b41 | 2008-07-25 14:38:51 +0300 | [diff] [blame] | 280 | * budgeted index space to the size of the current index, multiplies this by 3, |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 281 | * and makes sure this does not exceed the amount of free LEBs. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 282 | * |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 283 | * Notes about @c->bi.min_idx_lebs and @c->lst.idx_lebs variables: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 284 | * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might |
| 285 | * be large, because UBIFS does not do any index consolidation as long as |
| 286 | * there is free space. IOW, the index may take a lot of LEBs, but the LEBs |
| 287 | * will contain a lot of dirt. |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 288 | * o @c->bi.min_idx_lebs is the number of LEBS the index presumably takes. IOW, |
| 289 | * the index may be consolidated to take up to @c->bi.min_idx_lebs LEBs. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 290 | * |
| 291 | * This function returns zero in case of success, and %-ENOSPC in case of |
| 292 | * failure. |
| 293 | */ |
| 294 | static int do_budget_space(struct ubifs_info *c) |
| 295 | { |
| 296 | long long outstanding, available; |
| 297 | int lebs, rsvd_idx_lebs, min_idx_lebs; |
| 298 | |
| 299 | /* First budget index space */ |
| 300 | min_idx_lebs = ubifs_calc_min_idx_lebs(c); |
| 301 | |
| 302 | /* Now 'min_idx_lebs' contains number of LEBs to reserve */ |
| 303 | if (min_idx_lebs > c->lst.idx_lebs) |
| 304 | rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs; |
| 305 | else |
| 306 | rsvd_idx_lebs = 0; |
| 307 | |
| 308 | /* |
| 309 | * The number of LEBs that are available to be used by the index is: |
| 310 | * |
| 311 | * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt - |
| 312 | * @c->lst.taken_empty_lebs |
| 313 | * |
Artem Bityutskiy | 948cfb2 | 2008-08-20 11:56:33 +0300 | [diff] [blame] | 314 | * @c->lst.empty_lebs are available because they are empty. |
| 315 | * @c->freeable_cnt are available because they contain only free and |
| 316 | * dirty space, @c->idx_gc_cnt are available because they are index |
| 317 | * LEBs that have been garbage collected and are awaiting the commit |
| 318 | * before they can be used. And the in-the-gaps method will grab these |
| 319 | * if it needs them. @c->lst.taken_empty_lebs are empty LEBs that have |
| 320 | * already been allocated for some purpose. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 321 | * |
Artem Bityutskiy | 948cfb2 | 2008-08-20 11:56:33 +0300 | [diff] [blame] | 322 | * Note, @c->idx_gc_cnt is included to both @c->lst.empty_lebs (because |
| 323 | * these LEBs are empty) and to @c->lst.taken_empty_lebs (because they |
| 324 | * are taken until after the commit). |
| 325 | * |
| 326 | * Note, @c->lst.taken_empty_lebs may temporarily be higher by one |
| 327 | * because of the way we serialize LEB allocations and budgeting. See a |
| 328 | * comment in 'ubifs_find_free_space()'. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 329 | */ |
| 330 | lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - |
| 331 | c->lst.taken_empty_lebs; |
| 332 | if (unlikely(rsvd_idx_lebs > lebs)) { |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 333 | dbg_budg("out of indexing space: min_idx_lebs %d (old %d), rsvd_idx_lebs %d", |
| 334 | min_idx_lebs, c->bi.min_idx_lebs, rsvd_idx_lebs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 335 | return -ENOSPC; |
| 336 | } |
| 337 | |
| 338 | available = ubifs_calc_available(c, min_idx_lebs); |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 339 | outstanding = c->bi.data_growth + c->bi.dd_growth; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 340 | |
| 341 | if (unlikely(available < outstanding)) { |
| 342 | dbg_budg("out of data space: available %lld, outstanding %lld", |
| 343 | available, outstanding); |
| 344 | return -ENOSPC; |
| 345 | } |
| 346 | |
| 347 | if (available - outstanding <= c->rp_size && !can_use_rp(c)) |
| 348 | return -ENOSPC; |
| 349 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 350 | c->bi.min_idx_lebs = min_idx_lebs; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * calc_idx_growth - calculate approximate index growth from budgeting request. |
| 356 | * @c: UBIFS file-system description object |
| 357 | * @req: budgeting request |
| 358 | * |
| 359 | * For now we assume each new node adds one znode. But this is rather poor |
| 360 | * approximation, though. |
| 361 | */ |
| 362 | static int calc_idx_growth(const struct ubifs_info *c, |
| 363 | const struct ubifs_budget_req *req) |
| 364 | { |
| 365 | int znodes; |
| 366 | |
| 367 | znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) + |
| 368 | req->new_dent; |
| 369 | return znodes * c->max_idx_node_sz; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * calc_data_growth - calculate approximate amount of new data from budgeting |
| 374 | * request. |
| 375 | * @c: UBIFS file-system description object |
| 376 | * @req: budgeting request |
| 377 | */ |
| 378 | static int calc_data_growth(const struct ubifs_info *c, |
| 379 | const struct ubifs_budget_req *req) |
| 380 | { |
| 381 | int data_growth; |
| 382 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 383 | data_growth = req->new_ino ? c->bi.inode_budget : 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 384 | if (req->new_page) |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 385 | data_growth += c->bi.page_budget; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 386 | if (req->new_dent) |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 387 | data_growth += c->bi.dent_budget; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 388 | data_growth += req->new_ino_d; |
| 389 | return data_growth; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * calc_dd_growth - calculate approximate amount of data which makes other data |
| 394 | * dirty from budgeting request. |
| 395 | * @c: UBIFS file-system description object |
| 396 | * @req: budgeting request |
| 397 | */ |
| 398 | static int calc_dd_growth(const struct ubifs_info *c, |
| 399 | const struct ubifs_budget_req *req) |
| 400 | { |
| 401 | int dd_growth; |
| 402 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 403 | dd_growth = req->dirtied_page ? c->bi.page_budget : 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 404 | |
| 405 | if (req->dirtied_ino) |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 406 | dd_growth += c->bi.inode_budget << (req->dirtied_ino - 1); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 407 | if (req->mod_dent) |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 408 | dd_growth += c->bi.dent_budget; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 409 | dd_growth += req->dirtied_ino_d; |
| 410 | return dd_growth; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * ubifs_budget_space - ensure there is enough space to complete an operation. |
| 415 | * @c: UBIFS file-system description object |
| 416 | * @req: budget request |
| 417 | * |
| 418 | * This function allocates budget for an operation. It uses pessimistic |
| 419 | * approximation of how much flash space the operation needs. The goal of this |
| 420 | * function is to make sure UBIFS always has flash space to flush all dirty |
| 421 | * pages, dirty inodes, and dirty znodes (liability). This function may force |
| 422 | * commit, garbage-collection or write-back. Returns zero in case of success, |
| 423 | * %-ENOSPC if there is no free space and other negative error codes in case of |
| 424 | * failures. |
| 425 | */ |
| 426 | int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req) |
| 427 | { |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 428 | int err, idx_growth, data_growth, dd_growth, retried = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 429 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 430 | ubifs_assert(c, req->new_page <= 1); |
| 431 | ubifs_assert(c, req->dirtied_page <= 1); |
| 432 | ubifs_assert(c, req->new_dent <= 1); |
| 433 | ubifs_assert(c, req->mod_dent <= 1); |
| 434 | ubifs_assert(c, req->new_ino <= 1); |
| 435 | ubifs_assert(c, req->new_ino_d <= UBIFS_MAX_INO_DATA); |
| 436 | ubifs_assert(c, req->dirtied_ino <= 4); |
| 437 | ubifs_assert(c, req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4); |
| 438 | ubifs_assert(c, !(req->new_ino_d & 7)); |
| 439 | ubifs_assert(c, !(req->dirtied_ino_d & 7)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 440 | |
| 441 | data_growth = calc_data_growth(c, req); |
| 442 | dd_growth = calc_dd_growth(c, req); |
| 443 | if (!data_growth && !dd_growth) |
| 444 | return 0; |
| 445 | idx_growth = calc_idx_growth(c, req); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 446 | |
| 447 | again: |
| 448 | spin_lock(&c->space_lock); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 449 | ubifs_assert(c, c->bi.idx_growth >= 0); |
| 450 | ubifs_assert(c, c->bi.data_growth >= 0); |
| 451 | ubifs_assert(c, c->bi.dd_growth >= 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 452 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 453 | if (unlikely(c->bi.nospace) && (c->bi.nospace_rp || !can_use_rp(c))) { |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 454 | dbg_budg("no space"); |
| 455 | spin_unlock(&c->space_lock); |
| 456 | return -ENOSPC; |
| 457 | } |
| 458 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 459 | c->bi.idx_growth += idx_growth; |
| 460 | c->bi.data_growth += data_growth; |
| 461 | c->bi.dd_growth += dd_growth; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 462 | |
| 463 | err = do_budget_space(c); |
| 464 | if (likely(!err)) { |
| 465 | req->idx_growth = idx_growth; |
| 466 | req->data_growth = data_growth; |
| 467 | req->dd_growth = dd_growth; |
| 468 | spin_unlock(&c->space_lock); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* Restore the old values */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 473 | c->bi.idx_growth -= idx_growth; |
| 474 | c->bi.data_growth -= data_growth; |
| 475 | c->bi.dd_growth -= dd_growth; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 476 | spin_unlock(&c->space_lock); |
| 477 | |
| 478 | if (req->fast) { |
| 479 | dbg_budg("no space for fast budgeting"); |
| 480 | return err; |
| 481 | } |
| 482 | |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 483 | err = make_free_space(c); |
| 484 | cond_resched(); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 485 | if (err == -EAGAIN) { |
| 486 | dbg_budg("try again"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 487 | goto again; |
| 488 | } else if (err == -ENOSPC) { |
Artem Bityutskiy | 2acf806 | 2008-12-09 11:04:40 -0500 | [diff] [blame] | 489 | if (!retried) { |
| 490 | retried = 1; |
| 491 | dbg_budg("-ENOSPC, but anyway try once again"); |
| 492 | goto again; |
| 493 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 494 | dbg_budg("FS is full, -ENOSPC"); |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 495 | c->bi.nospace = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 496 | if (can_use_rp(c) || c->rp_size == 0) |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 497 | c->bi.nospace_rp = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 498 | smp_wmb(); |
| 499 | } else |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 500 | ubifs_err(c, "cannot budget space, error %d", err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 501 | return err; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * ubifs_release_budget - release budgeted free space. |
| 506 | * @c: UBIFS file-system description object |
| 507 | * @req: budget request |
| 508 | * |
| 509 | * This function releases the space budgeted by 'ubifs_budget_space()'. Note, |
| 510 | * since the index changes (which were budgeted for in @req->idx_growth) will |
| 511 | * only be written to the media on commit, this function moves the index budget |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 512 | * from @c->bi.idx_growth to @c->bi.uncommitted_idx. The latter will be zeroed |
| 513 | * by the commit operation. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 514 | */ |
| 515 | void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req) |
| 516 | { |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 517 | ubifs_assert(c, req->new_page <= 1); |
| 518 | ubifs_assert(c, req->dirtied_page <= 1); |
| 519 | ubifs_assert(c, req->new_dent <= 1); |
| 520 | ubifs_assert(c, req->mod_dent <= 1); |
| 521 | ubifs_assert(c, req->new_ino <= 1); |
| 522 | ubifs_assert(c, req->new_ino_d <= UBIFS_MAX_INO_DATA); |
| 523 | ubifs_assert(c, req->dirtied_ino <= 4); |
| 524 | ubifs_assert(c, req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4); |
| 525 | ubifs_assert(c, !(req->new_ino_d & 7)); |
| 526 | ubifs_assert(c, !(req->dirtied_ino_d & 7)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 527 | if (!req->recalculate) { |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 528 | ubifs_assert(c, req->idx_growth >= 0); |
| 529 | ubifs_assert(c, req->data_growth >= 0); |
| 530 | ubifs_assert(c, req->dd_growth >= 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (req->recalculate) { |
| 534 | req->data_growth = calc_data_growth(c, req); |
| 535 | req->dd_growth = calc_dd_growth(c, req); |
| 536 | req->idx_growth = calc_idx_growth(c, req); |
| 537 | } |
| 538 | |
| 539 | if (!req->data_growth && !req->dd_growth) |
| 540 | return; |
| 541 | |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 542 | c->bi.nospace = c->bi.nospace_rp = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 543 | smp_wmb(); |
| 544 | |
| 545 | spin_lock(&c->space_lock); |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 546 | c->bi.idx_growth -= req->idx_growth; |
| 547 | c->bi.uncommitted_idx += req->idx_growth; |
| 548 | c->bi.data_growth -= req->data_growth; |
| 549 | c->bi.dd_growth -= req->dd_growth; |
| 550 | c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 551 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 552 | ubifs_assert(c, c->bi.idx_growth >= 0); |
| 553 | ubifs_assert(c, c->bi.data_growth >= 0); |
| 554 | ubifs_assert(c, c->bi.dd_growth >= 0); |
| 555 | ubifs_assert(c, c->bi.min_idx_lebs < c->main_lebs); |
| 556 | ubifs_assert(c, !(c->bi.idx_growth & 7)); |
| 557 | ubifs_assert(c, !(c->bi.data_growth & 7)); |
| 558 | ubifs_assert(c, !(c->bi.dd_growth & 7)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 559 | spin_unlock(&c->space_lock); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * ubifs_convert_page_budget - convert budget of a new page. |
| 564 | * @c: UBIFS file-system description object |
| 565 | * |
| 566 | * This function converts budget which was allocated for a new page of data to |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 567 | * the budget of changing an existing page of data. The latter is smaller than |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 568 | * the former, so this function only does simple re-calculation and does not |
| 569 | * involve any write-back. |
| 570 | */ |
| 571 | void ubifs_convert_page_budget(struct ubifs_info *c) |
| 572 | { |
| 573 | spin_lock(&c->space_lock); |
| 574 | /* Release the index growth reservation */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 575 | c->bi.idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 576 | /* Release the data growth reservation */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 577 | c->bi.data_growth -= c->bi.page_budget; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 578 | /* Increase the dirty data growth reservation instead */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 579 | c->bi.dd_growth += c->bi.page_budget; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 580 | /* And re-calculate the indexing space reservation */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 581 | c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 582 | spin_unlock(&c->space_lock); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * ubifs_release_dirty_inode_budget - release dirty inode budget. |
| 587 | * @c: UBIFS file-system description object |
| 588 | * @ui: UBIFS inode to release the budget for |
| 589 | * |
| 590 | * This function releases budget corresponding to a dirty inode. It is usually |
| 591 | * called when after the inode has been written to the media and marked as |
Adrian Hunter | 6d6cb0d | 2009-04-08 14:07:57 +0200 | [diff] [blame] | 592 | * clean. It also causes the "no space" flags to be cleared. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 593 | */ |
| 594 | void ubifs_release_dirty_inode_budget(struct ubifs_info *c, |
| 595 | struct ubifs_inode *ui) |
| 596 | { |
Artem Bityutskiy | 182854b | 2008-07-18 18:54:29 +0300 | [diff] [blame] | 597 | struct ubifs_budget_req req; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 598 | |
Artem Bityutskiy | 182854b | 2008-07-18 18:54:29 +0300 | [diff] [blame] | 599 | memset(&req, 0, sizeof(struct ubifs_budget_req)); |
Adrian Hunter | 6d6cb0d | 2009-04-08 14:07:57 +0200 | [diff] [blame] | 600 | /* The "no space" flags will be cleared because dd_growth is > 0 */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 601 | req.dd_growth = c->bi.inode_budget + ALIGN(ui->data_len, 8); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 602 | ubifs_release_budget(c, &req); |
| 603 | } |
| 604 | |
| 605 | /** |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 606 | * ubifs_reported_space - calculate reported free space. |
| 607 | * @c: the UBIFS file-system description object |
| 608 | * @free: amount of free space |
| 609 | * |
| 610 | * This function calculates amount of free space which will be reported to |
| 611 | * user-space. User-space application tend to expect that if the file-system |
| 612 | * (e.g., via the 'statfs()' call) reports that it has N bytes available, they |
| 613 | * are able to write a file of size N. UBIFS attaches node headers to each data |
Artem Bityutskiy | 80736d4 | 2008-12-30 17:44:02 +0200 | [diff] [blame] | 614 | * node and it has to write indexing nodes as well. This introduces additional |
| 615 | * overhead, and UBIFS has to report slightly less free space to meet the above |
| 616 | * expectations. |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 617 | * |
| 618 | * This function assumes free space is made up of uncompressed data nodes and |
| 619 | * full index nodes (one per data node, tripled because we always allow enough |
| 620 | * space to write the index thrice). |
| 621 | * |
| 622 | * Note, the calculation is pessimistic, which means that most of the time |
| 623 | * UBIFS reports less space than it actually has. |
| 624 | */ |
Artem Bityutskiy | 4d61db4 | 2008-12-18 14:06:51 +0200 | [diff] [blame] | 625 | long long ubifs_reported_space(const struct ubifs_info *c, long long free) |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 626 | { |
Artem Bityutskiy | f171d4d | 2008-09-03 16:17:14 +0300 | [diff] [blame] | 627 | int divisor, factor, f; |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * Reported space size is @free * X, where X is UBIFS block size |
| 631 | * divided by UBIFS block size + all overhead one data block |
| 632 | * introduces. The overhead is the node header + indexing overhead. |
| 633 | * |
Artem Bityutskiy | f171d4d | 2008-09-03 16:17:14 +0300 | [diff] [blame] | 634 | * Indexing overhead calculations are based on the following formula: |
| 635 | * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number |
| 636 | * of data nodes, f - fanout. Because effective UBIFS fanout is twice |
| 637 | * as less than maximum fanout, we assume that each data node |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 638 | * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes. |
Artem Bityutskiy | 80736d4 | 2008-12-30 17:44:02 +0200 | [diff] [blame] | 639 | * Note, the multiplier 3 is because UBIFS reserves thrice as more space |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 640 | * for the index. |
| 641 | */ |
Artem Bityutskiy | f171d4d | 2008-09-03 16:17:14 +0300 | [diff] [blame] | 642 | f = c->fanout > 3 ? c->fanout >> 1 : 2; |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 643 | factor = UBIFS_BLOCK_SIZE; |
| 644 | divisor = UBIFS_MAX_DATA_NODE_SZ; |
Artem Bityutskiy | f171d4d | 2008-09-03 16:17:14 +0300 | [diff] [blame] | 645 | divisor += (c->max_idx_node_sz * 3) / (f - 1); |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 646 | free *= factor; |
Artem Bityutskiy | 4d61db4 | 2008-12-18 14:06:51 +0200 | [diff] [blame] | 647 | return div_u64(free, divisor); |
Artem Bityutskiy | 4b5f276 | 2008-08-25 16:15:56 +0300 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /** |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 651 | * ubifs_get_free_space_nolock - return amount of free space. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 652 | * @c: UBIFS file-system description object |
| 653 | * |
Artem Bityutskiy | 7dad181 | 2008-08-25 18:58:19 +0300 | [diff] [blame] | 654 | * This function calculates amount of free space to report to user-space. |
| 655 | * |
| 656 | * Because UBIFS may introduce substantial overhead (the index, node headers, |
Artem Bityutskiy | fb1cd01 | 2009-03-16 09:56:57 +0200 | [diff] [blame] | 657 | * alignment, wastage at the end of LEBs, etc), it cannot report real amount of |
| 658 | * free flash space it has (well, because not all dirty space is reclaimable, |
| 659 | * UBIFS does not actually know the real amount). If UBIFS did so, it would |
| 660 | * bread user expectations about what free space is. Users seem to accustomed |
| 661 | * to assume that if the file-system reports N bytes of free space, they would |
| 662 | * be able to fit a file of N bytes to the FS. This almost works for |
Artem Bityutskiy | 7dad181 | 2008-08-25 18:58:19 +0300 | [diff] [blame] | 663 | * traditional file-systems, because they have way less overhead than UBIFS. |
| 664 | * So, to keep users happy, UBIFS tries to take the overhead into account. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 665 | */ |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 666 | long long ubifs_get_free_space_nolock(struct ubifs_info *c) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 667 | { |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 668 | int rsvd_idx_lebs, lebs; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 669 | long long available, outstanding, free; |
| 670 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 671 | ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c)); |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 672 | outstanding = c->bi.data_growth + c->bi.dd_growth; |
| 673 | available = ubifs_calc_available(c, c->bi.min_idx_lebs); |
Artem Bityutskiy | 7dad181 | 2008-08-25 18:58:19 +0300 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * When reporting free space to user-space, UBIFS guarantees that it is |
| 677 | * possible to write a file of free space size. This means that for |
| 678 | * empty LEBs we may use more precise calculations than |
| 679 | * 'ubifs_calc_available()' is using. Namely, we know that in empty |
| 680 | * LEBs we would waste only @c->leb_overhead bytes, not @c->dark_wm. |
| 681 | * Thus, amend the available space. |
| 682 | * |
| 683 | * Note, the calculations below are similar to what we have in |
| 684 | * 'do_budget_space()', so refer there for comments. |
| 685 | */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 686 | if (c->bi.min_idx_lebs > c->lst.idx_lebs) |
| 687 | rsvd_idx_lebs = c->bi.min_idx_lebs - c->lst.idx_lebs; |
Artem Bityutskiy | 7dad181 | 2008-08-25 18:58:19 +0300 | [diff] [blame] | 688 | else |
| 689 | rsvd_idx_lebs = 0; |
| 690 | lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - |
| 691 | c->lst.taken_empty_lebs; |
| 692 | lebs -= rsvd_idx_lebs; |
| 693 | available += lebs * (c->dark_wm - c->leb_overhead); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 694 | |
| 695 | if (available > outstanding) |
| 696 | free = ubifs_reported_space(c, available - outstanding); |
| 697 | else |
| 698 | free = 0; |
| 699 | return free; |
| 700 | } |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 701 | |
| 702 | /** |
| 703 | * ubifs_get_free_space - return amount of free space. |
| 704 | * @c: UBIFS file-system description object |
| 705 | * |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 706 | * This function calculates and returns amount of free space to report to |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 707 | * user-space. |
| 708 | */ |
| 709 | long long ubifs_get_free_space(struct ubifs_info *c) |
| 710 | { |
| 711 | long long free; |
| 712 | |
| 713 | spin_lock(&c->space_lock); |
| 714 | free = ubifs_get_free_space_nolock(c); |
| 715 | spin_unlock(&c->space_lock); |
| 716 | |
| 717 | return free; |
| 718 | } |