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