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