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/math64.h> 35 36/** 37 * ubifs_calc_min_idx_lebs - calculate amount of eraseblocks for the index. 38 * @c: UBIFS file-system description object 39 * 40 * This function calculates and returns the number of eraseblocks which should 41 * be kept for index usage. 42 */ 43int ubifs_calc_min_idx_lebs(struct ubifs_info *c) 44{ 45 int idx_lebs, eff_leb_size = c->leb_size - c->max_idx_node_sz; 46 long long idx_size; 47 48 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx; 49 50 /* And make sure we have thrice the index size of space reserved */ 51 idx_size = idx_size + (idx_size << 1); 52 53 /* 54 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes' 55 * pair, nor similarly the two variables for the new index size, so we 56 * have to do this costly 64-bit division on fast-path. 57 */ 58 idx_size += eff_leb_size - 1; 59 idx_lebs = div_u64(idx_size, eff_leb_size); 60 /* 61 * The index head is not available for the in-the-gaps method, so add an 62 * extra LEB to compensate. 63 */ 64 idx_lebs += 1; 65 if (idx_lebs < MIN_INDEX_LEBS) 66 idx_lebs = MIN_INDEX_LEBS; 67 return idx_lebs; 68} 69 70/** 71 * ubifs_reported_space - calculate reported free space. 72 * @c: the UBIFS file-system description object 73 * @free: amount of free space 74 * 75 * This function calculates amount of free space which will be reported to 76 * user-space. User-space application tend to expect that if the file-system 77 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they 78 * are able to write a file of size N. UBIFS attaches node headers to each data 79 * node and it has to write indexing nodes as well. This introduces additional 80 * overhead, and UBIFS has to report slightly less free space to meet the above 81 * expectations. 82 * 83 * This function assumes free space is made up of uncompressed data nodes and 84 * full index nodes (one per data node, tripled because we always allow enough 85 * space to write the index thrice). 86 * 87 * Note, the calculation is pessimistic, which means that most of the time 88 * UBIFS reports less space than it actually has. 89 */ 90long long ubifs_reported_space(const struct ubifs_info *c, long long free) 91{ 92 int divisor, factor, f; 93 94 /* 95 * Reported space size is @free * X, where X is UBIFS block size 96 * divided by UBIFS block size + all overhead one data block 97 * introduces. The overhead is the node header + indexing overhead. 98 * 99 * Indexing overhead calculations are based on the following formula: 100 * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number 101 * of data nodes, f - fanout. Because effective UBIFS fanout is twice 102 * as less than maximum fanout, we assume that each data node 103 * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes. 104 * Note, the multiplier 3 is because UBIFS reserves thrice as more space 105 * for the index. 106 */ 107 f = c->fanout > 3 ? c->fanout >> 1 : 2; 108 factor = UBIFS_BLOCK_SIZE; 109 divisor = UBIFS_MAX_DATA_NODE_SZ; 110 divisor += (c->max_idx_node_sz * 3) / (f - 1); 111 free *= factor; 112 return div_u64(free, divisor); 113} 114