![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0-only 0002 /* 0003 * This file is part of UBIFS. 0004 * 0005 * Copyright (C) 2006-2008 Nokia Corporation. 0006 * 0007 * Authors: Adrian Hunter 0008 * Artem Bityutskiy (Битюцкий Артём) 0009 */ 0010 0011 /* 0012 * This file implements the budgeting sub-system which is responsible for UBIFS 0013 * space management. 0014 * 0015 * Factors such as compression, wasted space at the ends of LEBs, space in other 0016 * journal heads, the effect of updates on the index, and so on, make it 0017 * impossible to accurately predict the amount of space needed. Consequently 0018 * approximations are used. 0019 */ 0020 0021 #include "ubifs.h" 0022 #include <linux/writeback.h> 0023 #include <linux/math64.h> 0024 0025 /* 0026 * When pessimistic budget calculations say that there is no enough space, 0027 * UBIFS starts writing back dirty inodes and pages, doing garbage collection, 0028 * or committing. The below constant defines maximum number of times UBIFS 0029 * repeats the operations. 0030 */ 0031 #define MAX_MKSPC_RETRIES 3 0032 0033 /* 0034 * The below constant defines amount of dirty pages which should be written 0035 * back at when trying to shrink the liability. 0036 */ 0037 #define NR_TO_WRITE 16 0038 0039 /** 0040 * shrink_liability - write-back some dirty pages/inodes. 0041 * @c: UBIFS file-system description object 0042 * @nr_to_write: how many dirty pages to write-back 0043 * 0044 * This function shrinks UBIFS liability by means of writing back some amount 0045 * of dirty inodes and their pages. 0046 * 0047 * Note, this function synchronizes even VFS inodes which are locked 0048 * (@i_mutex) by the caller of the budgeting function, because write-back does 0049 * not touch @i_mutex. 0050 */ 0051 static void shrink_liability(struct ubifs_info *c, int nr_to_write) 0052 { 0053 down_read(&c->vfs_sb->s_umount); 0054 writeback_inodes_sb_nr(c->vfs_sb, nr_to_write, WB_REASON_FS_FREE_SPACE); 0055 up_read(&c->vfs_sb->s_umount); 0056 } 0057 0058 /** 0059 * run_gc - run garbage collector. 0060 * @c: UBIFS file-system description object 0061 * 0062 * This function runs garbage collector to make some more free space. Returns 0063 * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a 0064 * negative error code in case of failure. 0065 */ 0066 static int run_gc(struct ubifs_info *c) 0067 { 0068 int lnum; 0069 0070 /* Make some free space by garbage-collecting dirty space */ 0071 down_read(&c->commit_sem); 0072 lnum = ubifs_garbage_collect(c, 1); 0073 up_read(&c->commit_sem); 0074 if (lnum < 0) 0075 return lnum; 0076 0077 /* GC freed one LEB, return it to lprops */ 0078 dbg_budg("GC freed LEB %d", lnum); 0079 return ubifs_return_leb(c, lnum); 0080 } 0081 0082 /** 0083 * get_liability - calculate current liability. 0084 * @c: UBIFS file-system description object 0085 * 0086 * This function calculates and returns current UBIFS liability, i.e. the 0087 * amount of bytes UBIFS has "promised" to write to the media. 0088 */ 0089 static long long get_liability(struct ubifs_info *c) 0090 { 0091 long long liab; 0092 0093 spin_lock(&c->space_lock); 0094 liab = c->bi.idx_growth + c->bi.data_growth + c->bi.dd_growth; 0095 spin_unlock(&c->space_lock); 0096 return liab; 0097 } 0098 0099 /** 0100 * make_free_space - make more free space on the file-system. 0101 * @c: UBIFS file-system description object 0102 * 0103 * This function is called when an operation cannot be budgeted because there 0104 * is supposedly no free space. But in most cases there is some free space: 0105 * o budgeting is pessimistic, so it always budgets more than it is actually 0106 * needed, so shrinking the liability is one way to make free space - the 0107 * cached data will take less space then it was budgeted for; 0108 * o GC may turn some dark space into free space (budgeting treats dark space 0109 * as not available); 0110 * o commit may free some LEB, i.e., turn freeable LEBs into free LEBs. 0111 * 0112 * So this function tries to do the above. Returns %-EAGAIN if some free space 0113 * was presumably made and the caller has to re-try budgeting the operation. 0114 * Returns %-ENOSPC if it couldn't do more free space, and other negative error 0115 * codes on failures. 0116 */ 0117 static int make_free_space(struct ubifs_info *c) 0118 { 0119 int err, retries = 0; 0120 long long liab1, liab2; 0121 0122 do { 0123 liab1 = get_liability(c); 0124 /* 0125 * We probably have some dirty pages or inodes (liability), try 0126 * to write them back. 0127 */ 0128 dbg_budg("liability %lld, run write-back", liab1); 0129 shrink_liability(c, NR_TO_WRITE); 0130 0131 liab2 = get_liability(c); 0132 if (liab2 < liab1) 0133 return -EAGAIN; 0134 0135 dbg_budg("new liability %lld (not shrunk)", liab2); 0136 0137 /* Liability did not shrink again, try GC */ 0138 dbg_budg("Run GC"); 0139 err = run_gc(c); 0140 if (!err) 0141 return -EAGAIN; 0142 0143 if (err != -EAGAIN && err != -ENOSPC) 0144 /* Some real error happened */ 0145 return err; 0146 0147 dbg_budg("Run commit (retries %d)", retries); 0148 err = ubifs_run_commit(c); 0149 if (err) 0150 return err; 0151 } while (retries++ < MAX_MKSPC_RETRIES); 0152 0153 return -ENOSPC; 0154 } 0155 0156 /** 0157 * ubifs_calc_min_idx_lebs - calculate amount of LEBs for the index. 0158 * @c: UBIFS file-system description object 0159 * 0160 * This function calculates and returns the number of LEBs which should be kept 0161 * for index usage. 0162 */ 0163 int ubifs_calc_min_idx_lebs(struct ubifs_info *c) 0164 { 0165 int idx_lebs; 0166 long long idx_size; 0167 0168 idx_size = c->bi.old_idx_sz + c->bi.idx_growth + c->bi.uncommitted_idx; 0169 /* And make sure we have thrice the index size of space reserved */ 0170 idx_size += idx_size << 1; 0171 /* 0172 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes' 0173 * pair, nor similarly the two variables for the new index size, so we 0174 * have to do this costly 64-bit division on fast-path. 0175 */ 0176 idx_lebs = div_u64(idx_size + c->idx_leb_size - 1, c->idx_leb_size); 0177 /* 0178 * The index head is not available for the in-the-gaps method, so add an 0179 * extra LEB to compensate. 0180 */ 0181 idx_lebs += 1; 0182 if (idx_lebs < MIN_INDEX_LEBS) 0183 idx_lebs = MIN_INDEX_LEBS; 0184 return idx_lebs; 0185 } 0186 0187 /** 0188 * ubifs_calc_available - calculate available FS space. 0189 * @c: UBIFS file-system description object 0190 * @min_idx_lebs: minimum number of LEBs reserved for the index 0191 * 0192 * This function calculates and returns amount of FS space available for use. 0193 */ 0194 long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs) 0195 { 0196 int subtract_lebs; 0197 long long available; 0198 0199 available = c->main_bytes - c->lst.total_used; 0200 0201 /* 0202 * Now 'available' contains theoretically available flash space 0203 * assuming there is no index, so we have to subtract the space which 0204 * is reserved for the index. 0205 */ 0206 subtract_lebs = min_idx_lebs; 0207 0208 /* Take into account that GC reserves one LEB for its own needs */ 0209 subtract_lebs += 1; 0210 0211 /* 0212 * The GC journal head LEB is not really accessible. And since 0213 * different write types go to different heads, we may count only on 0214 * one head's space. 0215 */ 0216 subtract_lebs += c->jhead_cnt - 1; 0217 0218 /* We also reserve one LEB for deletions, which bypass budgeting */ 0219 subtract_lebs += 1; 0220 0221 available -= (long long)subtract_lebs * c->leb_size; 0222 0223 /* Subtract the dead space which is not available for use */ 0224 available -= c->lst.total_dead; 0225 0226 /* 0227 * Subtract dark space, which might or might not be usable - it depends 0228 * on the data which we have on the media and which will be written. If 0229 * this is a lot of uncompressed or not-compressible data, the dark 0230 * space cannot be used. 0231 */ 0232 available -= c->lst.total_dark; 0233 0234 /* 0235 * However, there is more dark space. The index may be bigger than 0236 * @min_idx_lebs. Those extra LEBs are assumed to be available, but 0237 * their dark space is not included in total_dark, so it is subtracted 0238 * here. 0239 */ 0240 if (c->lst.idx_lebs > min_idx_lebs) { 0241 subtract_lebs = c->lst.idx_lebs - min_idx_lebs; 0242 available -= subtract_lebs * c->dark_wm; 0243 } 0244 0245 /* The calculations are rough and may end up with a negative number */ 0246 return available > 0 ? available : 0; 0247 } 0248 0249 /** 0250 * can_use_rp - check whether the user is allowed to use reserved pool. 0251 * @c: UBIFS file-system description object 0252 * 0253 * UBIFS has so-called "reserved pool" which is flash space reserved 0254 * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock. 0255 * This function checks whether current user is allowed to use reserved pool. 0256 * Returns %1 current user is allowed to use reserved pool and %0 otherwise. 0257 */ 0258 static int can_use_rp(struct ubifs_info *c) 0259 { 0260 if (uid_eq(current_fsuid(), c->rp_uid) || capable(CAP_SYS_RESOURCE) || 0261 (!gid_eq(c->rp_gid, GLOBAL_ROOT_GID) && in_group_p(c->rp_gid))) 0262 return 1; 0263 return 0; 0264 } 0265 0266 /** 0267 * do_budget_space - reserve flash space for index and data growth. 0268 * @c: UBIFS file-system description object 0269 * 0270 * This function makes sure UBIFS has enough free LEBs for index growth and 0271 * data. 0272 * 0273 * When budgeting index space, UBIFS reserves thrice as many LEBs as the index 0274 * would take if it was consolidated and written to the flash. This guarantees 0275 * that the "in-the-gaps" commit method always succeeds and UBIFS will always 0276 * be able to commit dirty index. So this function basically adds amount of 0277 * budgeted index space to the size of the current index, multiplies this by 3, 0278 * and makes sure this does not exceed the amount of free LEBs. 0279 * 0280 * Notes about @c->bi.min_idx_lebs and @c->lst.idx_lebs variables: 0281 * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might 0282 * be large, because UBIFS does not do any index consolidation as long as 0283 * there is free space. IOW, the index may take a lot of LEBs, but the LEBs 0284 * will contain a lot of dirt. 0285 * o @c->bi.min_idx_lebs is the number of LEBS the index presumably takes. IOW, 0286 * the index may be consolidated to take up to @c->bi.min_idx_lebs LEBs. 0287 * 0288 * This function returns zero in case of success, and %-ENOSPC in case of 0289 * failure. 0290 */ 0291 static int do_budget_space(struct ubifs_info *c) 0292 { 0293 long long outstanding, available; 0294 int lebs, rsvd_idx_lebs, min_idx_lebs; 0295 0296 /* First budget index space */ 0297 min_idx_lebs = ubifs_calc_min_idx_lebs(c); 0298 0299 /* Now 'min_idx_lebs' contains number of LEBs to reserve */ 0300 if (min_idx_lebs > c->lst.idx_lebs) 0301 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs; 0302 else 0303 rsvd_idx_lebs = 0; 0304 0305 /* 0306 * The number of LEBs that are available to be used by the index is: 0307 * 0308 * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt - 0309 * @c->lst.taken_empty_lebs 0310 * 0311 * @c->lst.empty_lebs are available because they are empty. 0312 * @c->freeable_cnt are available because they contain only free and 0313 * dirty space, @c->idx_gc_cnt are available because they are index 0314 * LEBs that have been garbage collected and are awaiting the commit 0315 * before they can be used. And the in-the-gaps method will grab these 0316 * if it needs them. @c->lst.taken_empty_lebs are empty LEBs that have 0317 * already been allocated for some purpose. 0318 * 0319 * Note, @c->idx_gc_cnt is included to both @c->lst.empty_lebs (because 0320 * these LEBs are empty) and to @c->lst.taken_empty_lebs (because they 0321 * are taken until after the commit). 0322 * 0323 * Note, @c->lst.taken_empty_lebs may temporarily be higher by one 0324 * because of the way we serialize LEB allocations and budgeting. See a 0325 * comment in 'ubifs_find_free_space()'. 0326 */ 0327 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - 0328 c->lst.taken_empty_lebs; 0329 if (unlikely(rsvd_idx_lebs > lebs)) { 0330 dbg_budg("out of indexing space: min_idx_lebs %d (old %d), rsvd_idx_lebs %d", 0331 min_idx_lebs, c->bi.min_idx_lebs, rsvd_idx_lebs); 0332 return -ENOSPC; 0333 } 0334 0335 available = ubifs_calc_available(c, min_idx_lebs); 0336 outstanding = c->bi.data_growth + c->bi.dd_growth; 0337 0338 if (unlikely(available < outstanding)) { 0339 dbg_budg("out of data space: available %lld, outstanding %lld", 0340 available, outstanding); 0341 return -ENOSPC; 0342 } 0343 0344 if (available - outstanding <= c->rp_size && !can_use_rp(c)) 0345 return -ENOSPC; 0346 0347 c->bi.min_idx_lebs = min_idx_lebs; 0348 return 0; 0349 } 0350 0351 /** 0352 * calc_idx_growth - calculate approximate index growth from budgeting request. 0353 * @c: UBIFS file-system description object 0354 * @req: budgeting request 0355 * 0356 * For now we assume each new node adds one znode. But this is rather poor 0357 * approximation, though. 0358 */ 0359 static int calc_idx_growth(const struct ubifs_info *c, 0360 const struct ubifs_budget_req *req) 0361 { 0362 int znodes; 0363 0364 znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) + 0365 req->new_dent; 0366 return znodes * c->max_idx_node_sz; 0367 } 0368 0369 /** 0370 * calc_data_growth - calculate approximate amount of new data from budgeting 0371 * request. 0372 * @c: UBIFS file-system description object 0373 * @req: budgeting request 0374 */ 0375 static int calc_data_growth(const struct ubifs_info *c, 0376 const struct ubifs_budget_req *req) 0377 { 0378 int data_growth; 0379 0380 data_growth = req->new_ino ? c->bi.inode_budget : 0; 0381 if (req->new_page) 0382 data_growth += c->bi.page_budget; 0383 if (req->new_dent) 0384 data_growth += c->bi.dent_budget; 0385 data_growth += req->new_ino_d; 0386 return data_growth; 0387 } 0388 0389 /** 0390 * calc_dd_growth - calculate approximate amount of data which makes other data 0391 * dirty from budgeting request. 0392 * @c: UBIFS file-system description object 0393 * @req: budgeting request 0394 */ 0395 static int calc_dd_growth(const struct ubifs_info *c, 0396 const struct ubifs_budget_req *req) 0397 { 0398 int dd_growth; 0399 0400 dd_growth = req->dirtied_page ? c->bi.page_budget : 0; 0401 0402 if (req->dirtied_ino) 0403 dd_growth += c->bi.inode_budget << (req->dirtied_ino - 1); 0404 if (req->mod_dent) 0405 dd_growth += c->bi.dent_budget; 0406 dd_growth += req->dirtied_ino_d; 0407 return dd_growth; 0408 } 0409 0410 /** 0411 * ubifs_budget_space - ensure there is enough space to complete an operation. 0412 * @c: UBIFS file-system description object 0413 * @req: budget request 0414 * 0415 * This function allocates budget for an operation. It uses pessimistic 0416 * approximation of how much flash space the operation needs. The goal of this 0417 * function is to make sure UBIFS always has flash space to flush all dirty 0418 * pages, dirty inodes, and dirty znodes (liability). This function may force 0419 * commit, garbage-collection or write-back. Returns zero in case of success, 0420 * %-ENOSPC if there is no free space and other negative error codes in case of 0421 * failures. 0422 */ 0423 int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req) 0424 { 0425 int err, idx_growth, data_growth, dd_growth, retried = 0; 0426 0427 ubifs_assert(c, req->new_page <= 1); 0428 ubifs_assert(c, req->dirtied_page <= 1); 0429 ubifs_assert(c, req->new_dent <= 1); 0430 ubifs_assert(c, req->mod_dent <= 1); 0431 ubifs_assert(c, req->new_ino <= 1); 0432 ubifs_assert(c, req->new_ino_d <= UBIFS_MAX_INO_DATA); 0433 ubifs_assert(c, req->dirtied_ino <= 4); 0434 ubifs_assert(c, req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4); 0435 ubifs_assert(c, !(req->new_ino_d & 7)); 0436 ubifs_assert(c, !(req->dirtied_ino_d & 7)); 0437 0438 data_growth = calc_data_growth(c, req); 0439 dd_growth = calc_dd_growth(c, req); 0440 if (!data_growth && !dd_growth) 0441 return 0; 0442 idx_growth = calc_idx_growth(c, req); 0443 0444 again: 0445 spin_lock(&c->space_lock); 0446 ubifs_assert(c, c->bi.idx_growth >= 0); 0447 ubifs_assert(c, c->bi.data_growth >= 0); 0448 ubifs_assert(c, c->bi.dd_growth >= 0); 0449 0450 if (unlikely(c->bi.nospace) && (c->bi.nospace_rp || !can_use_rp(c))) { 0451 dbg_budg("no space"); 0452 spin_unlock(&c->space_lock); 0453 return -ENOSPC; 0454 } 0455 0456 c->bi.idx_growth += idx_growth; 0457 c->bi.data_growth += data_growth; 0458 c->bi.dd_growth += dd_growth; 0459 0460 err = do_budget_space(c); 0461 if (likely(!err)) { 0462 req->idx_growth = idx_growth; 0463 req->data_growth = data_growth; 0464 req->dd_growth = dd_growth; 0465 spin_unlock(&c->space_lock); 0466 return 0; 0467 } 0468 0469 /* Restore the old values */ 0470 c->bi.idx_growth -= idx_growth; 0471 c->bi.data_growth -= data_growth; 0472 c->bi.dd_growth -= dd_growth; 0473 spin_unlock(&c->space_lock); 0474 0475 if (req->fast) { 0476 dbg_budg("no space for fast budgeting"); 0477 return err; 0478 } 0479 0480 err = make_free_space(c); 0481 cond_resched(); 0482 if (err == -EAGAIN) { 0483 dbg_budg("try again"); 0484 goto again; 0485 } else if (err == -ENOSPC) { 0486 if (!retried) { 0487 retried = 1; 0488 dbg_budg("-ENOSPC, but anyway try once again"); 0489 goto again; 0490 } 0491 dbg_budg("FS is full, -ENOSPC"); 0492 c->bi.nospace = 1; 0493 if (can_use_rp(c) || c->rp_size == 0) 0494 c->bi.nospace_rp = 1; 0495 smp_wmb(); 0496 } else 0497 ubifs_err(c, "cannot budget space, error %d", err); 0498 return err; 0499 } 0500 0501 /** 0502 * ubifs_release_budget - release budgeted free space. 0503 * @c: UBIFS file-system description object 0504 * @req: budget request 0505 * 0506 * This function releases the space budgeted by 'ubifs_budget_space()'. Note, 0507 * since the index changes (which were budgeted for in @req->idx_growth) will 0508 * only be written to the media on commit, this function moves the index budget 0509 * from @c->bi.idx_growth to @c->bi.uncommitted_idx. The latter will be zeroed 0510 * by the commit operation. 0511 */ 0512 void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req) 0513 { 0514 ubifs_assert(c, req->new_page <= 1); 0515 ubifs_assert(c, req->dirtied_page <= 1); 0516 ubifs_assert(c, req->new_dent <= 1); 0517 ubifs_assert(c, req->mod_dent <= 1); 0518 ubifs_assert(c, req->new_ino <= 1); 0519 ubifs_assert(c, req->new_ino_d <= UBIFS_MAX_INO_DATA); 0520 ubifs_assert(c, req->dirtied_ino <= 4); 0521 ubifs_assert(c, req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4); 0522 ubifs_assert(c, !(req->new_ino_d & 7)); 0523 ubifs_assert(c, !(req->dirtied_ino_d & 7)); 0524 if (!req->recalculate) { 0525 ubifs_assert(c, req->idx_growth >= 0); 0526 ubifs_assert(c, req->data_growth >= 0); 0527 ubifs_assert(c, req->dd_growth >= 0); 0528 } 0529 0530 if (req->recalculate) { 0531 req->data_growth = calc_data_growth(c, req); 0532 req->dd_growth = calc_dd_growth(c, req); 0533 req->idx_growth = calc_idx_growth(c, req); 0534 } 0535 0536 if (!req->data_growth && !req->dd_growth) 0537 return; 0538 0539 c->bi.nospace = c->bi.nospace_rp = 0; 0540 smp_wmb(); 0541 0542 spin_lock(&c->space_lock); 0543 c->bi.idx_growth -= req->idx_growth; 0544 c->bi.uncommitted_idx += req->idx_growth; 0545 c->bi.data_growth -= req->data_growth; 0546 c->bi.dd_growth -= req->dd_growth; 0547 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); 0548 0549 ubifs_assert(c, c->bi.idx_growth >= 0); 0550 ubifs_assert(c, c->bi.data_growth >= 0); 0551 ubifs_assert(c, c->bi.dd_growth >= 0); 0552 ubifs_assert(c, c->bi.min_idx_lebs < c->main_lebs); 0553 ubifs_assert(c, !(c->bi.idx_growth & 7)); 0554 ubifs_assert(c, !(c->bi.data_growth & 7)); 0555 ubifs_assert(c, !(c->bi.dd_growth & 7)); 0556 spin_unlock(&c->space_lock); 0557 } 0558 0559 /** 0560 * ubifs_convert_page_budget - convert budget of a new page. 0561 * @c: UBIFS file-system description object 0562 * 0563 * This function converts budget which was allocated for a new page of data to 0564 * the budget of changing an existing page of data. The latter is smaller than 0565 * the former, so this function only does simple re-calculation and does not 0566 * involve any write-back. 0567 */ 0568 void ubifs_convert_page_budget(struct ubifs_info *c) 0569 { 0570 spin_lock(&c->space_lock); 0571 /* Release the index growth reservation */ 0572 c->bi.idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT; 0573 /* Release the data growth reservation */ 0574 c->bi.data_growth -= c->bi.page_budget; 0575 /* Increase the dirty data growth reservation instead */ 0576 c->bi.dd_growth += c->bi.page_budget; 0577 /* And re-calculate the indexing space reservation */ 0578 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); 0579 spin_unlock(&c->space_lock); 0580 } 0581 0582 /** 0583 * ubifs_release_dirty_inode_budget - release dirty inode budget. 0584 * @c: UBIFS file-system description object 0585 * @ui: UBIFS inode to release the budget for 0586 * 0587 * This function releases budget corresponding to a dirty inode. It is usually 0588 * called when after the inode has been written to the media and marked as 0589 * clean. It also causes the "no space" flags to be cleared. 0590 */ 0591 void ubifs_release_dirty_inode_budget(struct ubifs_info *c, 0592 struct ubifs_inode *ui) 0593 { 0594 struct ubifs_budget_req req; 0595 0596 memset(&req, 0, sizeof(struct ubifs_budget_req)); 0597 /* The "no space" flags will be cleared because dd_growth is > 0 */ 0598 req.dd_growth = c->bi.inode_budget + ALIGN(ui->data_len, 8); 0599 ubifs_release_budget(c, &req); 0600 } 0601 0602 /** 0603 * ubifs_reported_space - calculate reported free space. 0604 * @c: the UBIFS file-system description object 0605 * @free: amount of free space 0606 * 0607 * This function calculates amount of free space which will be reported to 0608 * user-space. User-space application tend to expect that if the file-system 0609 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they 0610 * are able to write a file of size N. UBIFS attaches node headers to each data 0611 * node and it has to write indexing nodes as well. This introduces additional 0612 * overhead, and UBIFS has to report slightly less free space to meet the above 0613 * expectations. 0614 * 0615 * This function assumes free space is made up of uncompressed data nodes and 0616 * full index nodes (one per data node, tripled because we always allow enough 0617 * space to write the index thrice). 0618 * 0619 * Note, the calculation is pessimistic, which means that most of the time 0620 * UBIFS reports less space than it actually has. 0621 */ 0622 long long ubifs_reported_space(const struct ubifs_info *c, long long free) 0623 { 0624 int divisor, factor, f; 0625 0626 /* 0627 * Reported space size is @free * X, where X is UBIFS block size 0628 * divided by UBIFS block size + all overhead one data block 0629 * introduces. The overhead is the node header + indexing overhead. 0630 * 0631 * Indexing overhead calculations are based on the following formula: 0632 * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number 0633 * of data nodes, f - fanout. Because effective UBIFS fanout is twice 0634 * as less than maximum fanout, we assume that each data node 0635 * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes. 0636 * Note, the multiplier 3 is because UBIFS reserves thrice as more space 0637 * for the index. 0638 */ 0639 f = c->fanout > 3 ? c->fanout >> 1 : 2; 0640 factor = UBIFS_BLOCK_SIZE; 0641 divisor = UBIFS_MAX_DATA_NODE_SZ; 0642 divisor += (c->max_idx_node_sz * 3) / (f - 1); 0643 free *= factor; 0644 return div_u64(free, divisor); 0645 } 0646 0647 /** 0648 * ubifs_get_free_space_nolock - return amount of free space. 0649 * @c: UBIFS file-system description object 0650 * 0651 * This function calculates amount of free space to report to user-space. 0652 * 0653 * Because UBIFS may introduce substantial overhead (the index, node headers, 0654 * alignment, wastage at the end of LEBs, etc), it cannot report real amount of 0655 * free flash space it has (well, because not all dirty space is reclaimable, 0656 * UBIFS does not actually know the real amount). If UBIFS did so, it would 0657 * bread user expectations about what free space is. Users seem to accustomed 0658 * to assume that if the file-system reports N bytes of free space, they would 0659 * be able to fit a file of N bytes to the FS. This almost works for 0660 * traditional file-systems, because they have way less overhead than UBIFS. 0661 * So, to keep users happy, UBIFS tries to take the overhead into account. 0662 */ 0663 long long ubifs_get_free_space_nolock(struct ubifs_info *c) 0664 { 0665 int rsvd_idx_lebs, lebs; 0666 long long available, outstanding, free; 0667 0668 ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c)); 0669 outstanding = c->bi.data_growth + c->bi.dd_growth; 0670 available = ubifs_calc_available(c, c->bi.min_idx_lebs); 0671 0672 /* 0673 * When reporting free space to user-space, UBIFS guarantees that it is 0674 * possible to write a file of free space size. This means that for 0675 * empty LEBs we may use more precise calculations than 0676 * 'ubifs_calc_available()' is using. Namely, we know that in empty 0677 * LEBs we would waste only @c->leb_overhead bytes, not @c->dark_wm. 0678 * Thus, amend the available space. 0679 * 0680 * Note, the calculations below are similar to what we have in 0681 * 'do_budget_space()', so refer there for comments. 0682 */ 0683 if (c->bi.min_idx_lebs > c->lst.idx_lebs) 0684 rsvd_idx_lebs = c->bi.min_idx_lebs - c->lst.idx_lebs; 0685 else 0686 rsvd_idx_lebs = 0; 0687 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - 0688 c->lst.taken_empty_lebs; 0689 lebs -= rsvd_idx_lebs; 0690 available += lebs * (c->dark_wm - c->leb_overhead); 0691 0692 if (available > outstanding) 0693 free = ubifs_reported_space(c, available - outstanding); 0694 else 0695 free = 0; 0696 return free; 0697 } 0698 0699 /** 0700 * ubifs_get_free_space - return amount of free space. 0701 * @c: UBIFS file-system description object 0702 * 0703 * This function calculates and returns amount of free space to report to 0704 * user-space. 0705 */ 0706 long long ubifs_get_free_space(struct ubifs_info *c) 0707 { 0708 long long free; 0709 0710 spin_lock(&c->space_lock); 0711 free = ubifs_get_free_space_nolock(c); 0712 spin_unlock(&c->space_lock); 0713 0714 return free; 0715 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |