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