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