xref: /openbmc/linux/fs/ubifs/budget.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy  *
71e51764aSArtem Bityutskiy  * Authors: Adrian Hunter
81e51764aSArtem Bityutskiy  *          Artem Bityutskiy (Битюцкий Артём)
91e51764aSArtem Bityutskiy  */
101e51764aSArtem Bityutskiy 
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy  * This file implements the budgeting sub-system which is responsible for UBIFS
131e51764aSArtem Bityutskiy  * space management.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * Factors such as compression, wasted space at the ends of LEBs, space in other
161e51764aSArtem Bityutskiy  * journal heads, the effect of updates on the index, and so on, make it
171e51764aSArtem Bityutskiy  * impossible to accurately predict the amount of space needed. Consequently
181e51764aSArtem Bityutskiy  * approximations are used.
191e51764aSArtem Bityutskiy  */
201e51764aSArtem Bityutskiy 
211e51764aSArtem Bityutskiy #include "ubifs.h"
221e51764aSArtem Bityutskiy #include <linux/writeback.h>
234d61db4fSArtem Bityutskiy #include <linux/math64.h>
241e51764aSArtem Bityutskiy 
251e51764aSArtem Bityutskiy /*
261e51764aSArtem Bityutskiy  * When pessimistic budget calculations say that there is no enough space,
271e51764aSArtem Bityutskiy  * UBIFS starts writing back dirty inodes and pages, doing garbage collection,
282acf8067SArtem Bityutskiy  * or committing. The below constant defines maximum number of times UBIFS
291e51764aSArtem Bityutskiy  * repeats the operations.
301e51764aSArtem Bityutskiy  */
312acf8067SArtem Bityutskiy #define MAX_MKSPC_RETRIES 3
321e51764aSArtem Bityutskiy 
331e51764aSArtem Bityutskiy /*
341e51764aSArtem Bityutskiy  * The below constant defines amount of dirty pages which should be written
351e51764aSArtem Bityutskiy  * back at when trying to shrink the liability.
361e51764aSArtem Bityutskiy  */
371e51764aSArtem Bityutskiy #define NR_TO_WRITE 16
381e51764aSArtem Bityutskiy 
391e51764aSArtem Bityutskiy /**
401e51764aSArtem Bityutskiy  * shrink_liability - write-back some dirty pages/inodes.
411e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
421e51764aSArtem Bityutskiy  * @nr_to_write: how many dirty pages to write-back
431e51764aSArtem Bityutskiy  *
441e51764aSArtem Bityutskiy  * This function shrinks UBIFS liability by means of writing back some amount
45b6e51316SJens Axboe  * of dirty inodes and their pages.
461e51764aSArtem Bityutskiy  *
471e51764aSArtem Bityutskiy  * Note, this function synchronizes even VFS inodes which are locked
481e51764aSArtem Bityutskiy  * (@i_mutex) by the caller of the budgeting function, because write-back does
491e51764aSArtem Bityutskiy  * not touch @i_mutex.
501e51764aSArtem Bityutskiy  */
shrink_liability(struct ubifs_info * c,int nr_to_write)51b6e51316SJens Axboe static void shrink_liability(struct ubifs_info *c, int nr_to_write)
521e51764aSArtem Bityutskiy {
53cf37e972SChristoph Hellwig 	down_read(&c->vfs_sb->s_umount);
540af83abbSLiu Song 	writeback_inodes_sb_nr(c->vfs_sb, nr_to_write, WB_REASON_FS_FREE_SPACE);
55cf37e972SChristoph Hellwig 	up_read(&c->vfs_sb->s_umount);
561e51764aSArtem Bityutskiy }
571e51764aSArtem Bityutskiy 
581e51764aSArtem Bityutskiy /**
591e51764aSArtem Bityutskiy  * run_gc - run garbage collector.
601e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
611e51764aSArtem Bityutskiy  *
621e51764aSArtem Bityutskiy  * This function runs garbage collector to make some more free space. Returns
631e51764aSArtem Bityutskiy  * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a
641e51764aSArtem Bityutskiy  * negative error code in case of failure.
651e51764aSArtem Bityutskiy  */
run_gc(struct ubifs_info * c)661e51764aSArtem Bityutskiy static int run_gc(struct ubifs_info *c)
671e51764aSArtem Bityutskiy {
685bff56edSMinghao Chi 	int lnum;
691e51764aSArtem Bityutskiy 
701e51764aSArtem Bityutskiy 	/* Make some free space by garbage-collecting dirty space */
711e51764aSArtem Bityutskiy 	down_read(&c->commit_sem);
721e51764aSArtem Bityutskiy 	lnum = ubifs_garbage_collect(c, 1);
731e51764aSArtem Bityutskiy 	up_read(&c->commit_sem);
741e51764aSArtem Bityutskiy 	if (lnum < 0)
751e51764aSArtem Bityutskiy 		return lnum;
761e51764aSArtem Bityutskiy 
771e51764aSArtem Bityutskiy 	/* GC freed one LEB, return it to lprops */
781e51764aSArtem Bityutskiy 	dbg_budg("GC freed LEB %d", lnum);
795bff56edSMinghao Chi 	return ubifs_return_leb(c, lnum);
801e51764aSArtem Bityutskiy }
811e51764aSArtem Bityutskiy 
821e51764aSArtem Bityutskiy /**
832acf8067SArtem Bityutskiy  * get_liability - calculate current liability.
842acf8067SArtem Bityutskiy  * @c: UBIFS file-system description object
852acf8067SArtem Bityutskiy  *
862acf8067SArtem Bityutskiy  * This function calculates and returns current UBIFS liability, i.e. the
872acf8067SArtem Bityutskiy  * amount of bytes UBIFS has "promised" to write to the media.
882acf8067SArtem Bityutskiy  */
get_liability(struct ubifs_info * c)892acf8067SArtem Bityutskiy static long long get_liability(struct ubifs_info *c)
902acf8067SArtem Bityutskiy {
912acf8067SArtem Bityutskiy 	long long liab;
922acf8067SArtem Bityutskiy 
932acf8067SArtem Bityutskiy 	spin_lock(&c->space_lock);
94b137545cSArtem Bityutskiy 	liab = c->bi.idx_growth + c->bi.data_growth + c->bi.dd_growth;
952acf8067SArtem Bityutskiy 	spin_unlock(&c->space_lock);
962acf8067SArtem Bityutskiy 	return liab;
972acf8067SArtem Bityutskiy }
982acf8067SArtem Bityutskiy 
992acf8067SArtem Bityutskiy /**
1001e51764aSArtem Bityutskiy  * make_free_space - make more free space on the file-system.
1011e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1021e51764aSArtem Bityutskiy  *
1031e51764aSArtem Bityutskiy  * This function is called when an operation cannot be budgeted because there
1041e51764aSArtem Bityutskiy  * is supposedly no free space. But in most cases there is some free space:
105025dfdafSFrederik Schwarzer  *   o budgeting is pessimistic, so it always budgets more than it is actually
1061e51764aSArtem Bityutskiy  *     needed, so shrinking the liability is one way to make free space - the
1071e51764aSArtem Bityutskiy  *     cached data will take less space then it was budgeted for;
1081e51764aSArtem Bityutskiy  *   o GC may turn some dark space into free space (budgeting treats dark space
1091e51764aSArtem Bityutskiy  *     as not available);
1101e51764aSArtem Bityutskiy  *   o commit may free some LEB, i.e., turn freeable LEBs into free LEBs.
1111e51764aSArtem Bityutskiy  *
1121e51764aSArtem Bityutskiy  * So this function tries to do the above. Returns %-EAGAIN if some free space
1131e51764aSArtem Bityutskiy  * was presumably made and the caller has to re-try budgeting the operation.
1141e51764aSArtem Bityutskiy  * Returns %-ENOSPC if it couldn't do more free space, and other negative error
1151e51764aSArtem Bityutskiy  * codes on failures.
1161e51764aSArtem Bityutskiy  */
make_free_space(struct ubifs_info * c)1172acf8067SArtem Bityutskiy static int make_free_space(struct ubifs_info *c)
1181e51764aSArtem Bityutskiy {
1192acf8067SArtem Bityutskiy 	int err, retries = 0;
1202acf8067SArtem Bityutskiy 	long long liab1, liab2;
1211e51764aSArtem Bityutskiy 
1222acf8067SArtem Bityutskiy 	do {
1232acf8067SArtem Bityutskiy 		liab1 = get_liability(c);
1241e51764aSArtem Bityutskiy 		/*
1252acf8067SArtem Bityutskiy 		 * We probably have some dirty pages or inodes (liability), try
1262acf8067SArtem Bityutskiy 		 * to write them back.
1271e51764aSArtem Bityutskiy 		 */
1282acf8067SArtem Bityutskiy 		dbg_budg("liability %lld, run write-back", liab1);
1292acf8067SArtem Bityutskiy 		shrink_liability(c, NR_TO_WRITE);
1301e51764aSArtem Bityutskiy 
1312acf8067SArtem Bityutskiy 		liab2 = get_liability(c);
1322acf8067SArtem Bityutskiy 		if (liab2 < liab1)
1331e51764aSArtem Bityutskiy 			return -EAGAIN;
1341e51764aSArtem Bityutskiy 
13525985edcSLucas De Marchi 		dbg_budg("new liability %lld (not shrunk)", liab2);
1361e51764aSArtem Bityutskiy 
1372acf8067SArtem Bityutskiy 		/* Liability did not shrink again, try GC */
1382acf8067SArtem Bityutskiy 		dbg_budg("Run GC");
1391e51764aSArtem Bityutskiy 		err = run_gc(c);
1401e51764aSArtem Bityutskiy 		if (!err)
1411e51764aSArtem Bityutskiy 			return -EAGAIN;
1421e51764aSArtem Bityutskiy 
1432acf8067SArtem Bityutskiy 		if (err != -EAGAIN && err != -ENOSPC)
1442acf8067SArtem Bityutskiy 			/* Some real error happened */
1452acf8067SArtem Bityutskiy 			return err;
1462acf8067SArtem Bityutskiy 
1472acf8067SArtem Bityutskiy 		dbg_budg("Run commit (retries %d)", retries);
1481e51764aSArtem Bityutskiy 		err = ubifs_run_commit(c);
1491e51764aSArtem Bityutskiy 		if (err)
1501e51764aSArtem Bityutskiy 			return err;
1512acf8067SArtem Bityutskiy 	} while (retries++ < MAX_MKSPC_RETRIES);
1521e51764aSArtem Bityutskiy 
1531e51764aSArtem Bityutskiy 	return -ENOSPC;
1541e51764aSArtem Bityutskiy }
1551e51764aSArtem Bityutskiy 
1561e51764aSArtem Bityutskiy /**
157fb1cd01aSArtem Bityutskiy  * ubifs_calc_min_idx_lebs - calculate amount of LEBs for the index.
1581e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1591e51764aSArtem Bityutskiy  *
160fb1cd01aSArtem Bityutskiy  * This function calculates and returns the number of LEBs which should be kept
161fb1cd01aSArtem Bityutskiy  * for index usage.
1621e51764aSArtem Bityutskiy  */
ubifs_calc_min_idx_lebs(struct ubifs_info * c)1631e51764aSArtem Bityutskiy int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
1641e51764aSArtem Bityutskiy {
165fb1cd01aSArtem Bityutskiy 	int idx_lebs;
1664d61db4fSArtem Bityutskiy 	long long idx_size;
1671e51764aSArtem Bityutskiy 
168b137545cSArtem Bityutskiy 	idx_size = c->bi.old_idx_sz + c->bi.idx_growth + c->bi.uncommitted_idx;
1693a13252cSAdrian Hunter 	/* And make sure we have thrice the index size of space reserved */
170fb1cd01aSArtem Bityutskiy 	idx_size += idx_size << 1;
1711e51764aSArtem Bityutskiy 	/*
1721e51764aSArtem Bityutskiy 	 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
1731e51764aSArtem Bityutskiy 	 * pair, nor similarly the two variables for the new index size, so we
1741e51764aSArtem Bityutskiy 	 * have to do this costly 64-bit division on fast-path.
1751e51764aSArtem Bityutskiy 	 */
176fb1cd01aSArtem Bityutskiy 	idx_lebs = div_u64(idx_size + c->idx_leb_size - 1, c->idx_leb_size);
1771e51764aSArtem Bityutskiy 	/*
1781e51764aSArtem Bityutskiy 	 * The index head is not available for the in-the-gaps method, so add an
1791e51764aSArtem Bityutskiy 	 * extra LEB to compensate.
1801e51764aSArtem Bityutskiy 	 */
1814d61db4fSArtem Bityutskiy 	idx_lebs += 1;
1824d61db4fSArtem Bityutskiy 	if (idx_lebs < MIN_INDEX_LEBS)
1834d61db4fSArtem Bityutskiy 		idx_lebs = MIN_INDEX_LEBS;
1844d61db4fSArtem Bityutskiy 	return idx_lebs;
1851e51764aSArtem Bityutskiy }
1861e51764aSArtem Bityutskiy 
1871e51764aSArtem Bityutskiy /**
1881e51764aSArtem Bityutskiy  * ubifs_calc_available - calculate available FS space.
1891e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1901e51764aSArtem Bityutskiy  * @min_idx_lebs: minimum number of LEBs reserved for the index
1911e51764aSArtem Bityutskiy  *
1921e51764aSArtem Bityutskiy  * This function calculates and returns amount of FS space available for use.
1931e51764aSArtem Bityutskiy  */
ubifs_calc_available(const struct ubifs_info * c,int min_idx_lebs)1941e51764aSArtem Bityutskiy long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs)
1951e51764aSArtem Bityutskiy {
1961e51764aSArtem Bityutskiy 	int subtract_lebs;
1971e51764aSArtem Bityutskiy 	long long available;
1981e51764aSArtem Bityutskiy 
1991e51764aSArtem Bityutskiy 	available = c->main_bytes - c->lst.total_used;
2001e51764aSArtem Bityutskiy 
2011e51764aSArtem Bityutskiy 	/*
2021e51764aSArtem Bityutskiy 	 * Now 'available' contains theoretically available flash space
2031e51764aSArtem Bityutskiy 	 * assuming there is no index, so we have to subtract the space which
2041e51764aSArtem Bityutskiy 	 * is reserved for the index.
2051e51764aSArtem Bityutskiy 	 */
2061e51764aSArtem Bityutskiy 	subtract_lebs = min_idx_lebs;
2071e51764aSArtem Bityutskiy 
2081e51764aSArtem Bityutskiy 	/* Take into account that GC reserves one LEB for its own needs */
2091e51764aSArtem Bityutskiy 	subtract_lebs += 1;
2101e51764aSArtem Bityutskiy 
2111e51764aSArtem Bityutskiy 	/*
212*e874dcdeSZhihao Cheng 	 * Since different write types go to different heads, we should
213*e874dcdeSZhihao Cheng 	 * reserve one leb for each head.
2141e51764aSArtem Bityutskiy 	 */
215*e874dcdeSZhihao Cheng 	subtract_lebs += c->jhead_cnt;
2161e51764aSArtem Bityutskiy 
2171e51764aSArtem Bityutskiy 	/* We also reserve one LEB for deletions, which bypass budgeting */
2181e51764aSArtem Bityutskiy 	subtract_lebs += 1;
2191e51764aSArtem Bityutskiy 
2201e51764aSArtem Bityutskiy 	available -= (long long)subtract_lebs * c->leb_size;
2211e51764aSArtem Bityutskiy 
2221e51764aSArtem Bityutskiy 	/* Subtract the dead space which is not available for use */
2231e51764aSArtem Bityutskiy 	available -= c->lst.total_dead;
2241e51764aSArtem Bityutskiy 
2251e51764aSArtem Bityutskiy 	/*
2261e51764aSArtem Bityutskiy 	 * Subtract dark space, which might or might not be usable - it depends
2271e51764aSArtem Bityutskiy 	 * on the data which we have on the media and which will be written. If
2281e51764aSArtem Bityutskiy 	 * this is a lot of uncompressed or not-compressible data, the dark
2291e51764aSArtem Bityutskiy 	 * space cannot be used.
2301e51764aSArtem Bityutskiy 	 */
2311e51764aSArtem Bityutskiy 	available -= c->lst.total_dark;
2321e51764aSArtem Bityutskiy 
2331e51764aSArtem Bityutskiy 	/*
2341e51764aSArtem Bityutskiy 	 * However, there is more dark space. The index may be bigger than
2351e51764aSArtem Bityutskiy 	 * @min_idx_lebs. Those extra LEBs are assumed to be available, but
2361e51764aSArtem Bityutskiy 	 * their dark space is not included in total_dark, so it is subtracted
2371e51764aSArtem Bityutskiy 	 * here.
2381e51764aSArtem Bityutskiy 	 */
2391e51764aSArtem Bityutskiy 	if (c->lst.idx_lebs > min_idx_lebs) {
2401e51764aSArtem Bityutskiy 		subtract_lebs = c->lst.idx_lebs - min_idx_lebs;
2411e51764aSArtem Bityutskiy 		available -= subtract_lebs * c->dark_wm;
2421e51764aSArtem Bityutskiy 	}
2431e51764aSArtem Bityutskiy 
2441e51764aSArtem Bityutskiy 	/* The calculations are rough and may end up with a negative number */
2451e51764aSArtem Bityutskiy 	return available > 0 ? available : 0;
2461e51764aSArtem Bityutskiy }
2471e51764aSArtem Bityutskiy 
2481e51764aSArtem Bityutskiy /**
2491e51764aSArtem Bityutskiy  * can_use_rp - check whether the user is allowed to use reserved pool.
2501e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2511e51764aSArtem Bityutskiy  *
2521e51764aSArtem Bityutskiy  * UBIFS has so-called "reserved pool" which is flash space reserved
2531e51764aSArtem Bityutskiy  * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock.
2541e51764aSArtem Bityutskiy  * This function checks whether current user is allowed to use reserved pool.
2551e51764aSArtem Bityutskiy  * Returns %1  current user is allowed to use reserved pool and %0 otherwise.
2561e51764aSArtem Bityutskiy  */
can_use_rp(struct ubifs_info * c)2571e51764aSArtem Bityutskiy static int can_use_rp(struct ubifs_info *c)
2581e51764aSArtem Bityutskiy {
25939241bebSEric W. Biederman 	if (uid_eq(current_fsuid(), c->rp_uid) || capable(CAP_SYS_RESOURCE) ||
26039241bebSEric W. Biederman 	    (!gid_eq(c->rp_gid, GLOBAL_ROOT_GID) && in_group_p(c->rp_gid)))
2611e51764aSArtem Bityutskiy 		return 1;
2621e51764aSArtem Bityutskiy 	return 0;
2631e51764aSArtem Bityutskiy }
2641e51764aSArtem Bityutskiy 
2651e51764aSArtem Bityutskiy /**
2661e51764aSArtem Bityutskiy  * do_budget_space - reserve flash space for index and data growth.
2671e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2681e51764aSArtem Bityutskiy  *
269fb1cd01aSArtem Bityutskiy  * This function makes sure UBIFS has enough free LEBs for index growth and
270fb1cd01aSArtem Bityutskiy  * data.
2711e51764aSArtem Bityutskiy  *
2723a13252cSAdrian Hunter  * When budgeting index space, UBIFS reserves thrice as many LEBs as the index
2731e51764aSArtem Bityutskiy  * would take if it was consolidated and written to the flash. This guarantees
2741e51764aSArtem Bityutskiy  * that the "in-the-gaps" commit method always succeeds and UBIFS will always
2751e51764aSArtem Bityutskiy  * be able to commit dirty index. So this function basically adds amount of
276b364b41aSArtem Bityutskiy  * budgeted index space to the size of the current index, multiplies this by 3,
277fb1cd01aSArtem Bityutskiy  * and makes sure this does not exceed the amount of free LEBs.
2781e51764aSArtem Bityutskiy  *
279b137545cSArtem Bityutskiy  * Notes about @c->bi.min_idx_lebs and @c->lst.idx_lebs variables:
2801e51764aSArtem Bityutskiy  * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might
2811e51764aSArtem Bityutskiy  *    be large, because UBIFS does not do any index consolidation as long as
2821e51764aSArtem Bityutskiy  *    there is free space. IOW, the index may take a lot of LEBs, but the LEBs
2831e51764aSArtem Bityutskiy  *    will contain a lot of dirt.
284b137545cSArtem Bityutskiy  * o @c->bi.min_idx_lebs is the number of LEBS the index presumably takes. IOW,
285b137545cSArtem Bityutskiy  *    the index may be consolidated to take up to @c->bi.min_idx_lebs LEBs.
2861e51764aSArtem Bityutskiy  *
2871e51764aSArtem Bityutskiy  * This function returns zero in case of success, and %-ENOSPC in case of
2881e51764aSArtem Bityutskiy  * failure.
2891e51764aSArtem Bityutskiy  */
do_budget_space(struct ubifs_info * c)2901e51764aSArtem Bityutskiy static int do_budget_space(struct ubifs_info *c)
2911e51764aSArtem Bityutskiy {
2921e51764aSArtem Bityutskiy 	long long outstanding, available;
2931e51764aSArtem Bityutskiy 	int lebs, rsvd_idx_lebs, min_idx_lebs;
2941e51764aSArtem Bityutskiy 
2951e51764aSArtem Bityutskiy 	/* First budget index space */
2961e51764aSArtem Bityutskiy 	min_idx_lebs = ubifs_calc_min_idx_lebs(c);
2971e51764aSArtem Bityutskiy 
2981e51764aSArtem Bityutskiy 	/* Now 'min_idx_lebs' contains number of LEBs to reserve */
2991e51764aSArtem Bityutskiy 	if (min_idx_lebs > c->lst.idx_lebs)
3001e51764aSArtem Bityutskiy 		rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
3011e51764aSArtem Bityutskiy 	else
3021e51764aSArtem Bityutskiy 		rsvd_idx_lebs = 0;
3031e51764aSArtem Bityutskiy 
3041e51764aSArtem Bityutskiy 	/*
3051e51764aSArtem Bityutskiy 	 * The number of LEBs that are available to be used by the index is:
3061e51764aSArtem Bityutskiy 	 *
3071e51764aSArtem Bityutskiy 	 *    @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt -
3081e51764aSArtem Bityutskiy 	 *    @c->lst.taken_empty_lebs
3091e51764aSArtem Bityutskiy 	 *
310948cfb21SArtem Bityutskiy 	 * @c->lst.empty_lebs are available because they are empty.
311948cfb21SArtem Bityutskiy 	 * @c->freeable_cnt are available because they contain only free and
312948cfb21SArtem Bityutskiy 	 * dirty space, @c->idx_gc_cnt are available because they are index
313948cfb21SArtem Bityutskiy 	 * LEBs that have been garbage collected and are awaiting the commit
314948cfb21SArtem Bityutskiy 	 * before they can be used. And the in-the-gaps method will grab these
315948cfb21SArtem Bityutskiy 	 * if it needs them. @c->lst.taken_empty_lebs are empty LEBs that have
316948cfb21SArtem Bityutskiy 	 * already been allocated for some purpose.
3171e51764aSArtem Bityutskiy 	 *
318948cfb21SArtem Bityutskiy 	 * Note, @c->idx_gc_cnt is included to both @c->lst.empty_lebs (because
319948cfb21SArtem Bityutskiy 	 * these LEBs are empty) and to @c->lst.taken_empty_lebs (because they
320948cfb21SArtem Bityutskiy 	 * are taken until after the commit).
321948cfb21SArtem Bityutskiy 	 *
322948cfb21SArtem Bityutskiy 	 * Note, @c->lst.taken_empty_lebs may temporarily be higher by one
323948cfb21SArtem Bityutskiy 	 * because of the way we serialize LEB allocations and budgeting. See a
324948cfb21SArtem Bityutskiy 	 * comment in 'ubifs_find_free_space()'.
3251e51764aSArtem Bityutskiy 	 */
3261e51764aSArtem Bityutskiy 	lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
3271e51764aSArtem Bityutskiy 	       c->lst.taken_empty_lebs;
3281e51764aSArtem Bityutskiy 	if (unlikely(rsvd_idx_lebs > lebs)) {
32979fda517SArtem Bityutskiy 		dbg_budg("out of indexing space: min_idx_lebs %d (old %d), rsvd_idx_lebs %d",
33079fda517SArtem Bityutskiy 			 min_idx_lebs, c->bi.min_idx_lebs, rsvd_idx_lebs);
3311e51764aSArtem Bityutskiy 		return -ENOSPC;
3321e51764aSArtem Bityutskiy 	}
3331e51764aSArtem Bityutskiy 
3341e51764aSArtem Bityutskiy 	available = ubifs_calc_available(c, min_idx_lebs);
335b137545cSArtem Bityutskiy 	outstanding = c->bi.data_growth + c->bi.dd_growth;
3361e51764aSArtem Bityutskiy 
3371e51764aSArtem Bityutskiy 	if (unlikely(available < outstanding)) {
3381e51764aSArtem Bityutskiy 		dbg_budg("out of data space: available %lld, outstanding %lld",
3391e51764aSArtem Bityutskiy 			 available, outstanding);
3401e51764aSArtem Bityutskiy 		return -ENOSPC;
3411e51764aSArtem Bityutskiy 	}
3421e51764aSArtem Bityutskiy 
3431e51764aSArtem Bityutskiy 	if (available - outstanding <= c->rp_size && !can_use_rp(c))
3441e51764aSArtem Bityutskiy 		return -ENOSPC;
3451e51764aSArtem Bityutskiy 
346b137545cSArtem Bityutskiy 	c->bi.min_idx_lebs = min_idx_lebs;
3471e51764aSArtem Bityutskiy 	return 0;
3481e51764aSArtem Bityutskiy }
3491e51764aSArtem Bityutskiy 
3501e51764aSArtem Bityutskiy /**
3511e51764aSArtem Bityutskiy  * calc_idx_growth - calculate approximate index growth from budgeting request.
3521e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3531e51764aSArtem Bityutskiy  * @req: budgeting request
3541e51764aSArtem Bityutskiy  *
3551e51764aSArtem Bityutskiy  * For now we assume each new node adds one znode. But this is rather poor
3561e51764aSArtem Bityutskiy  * approximation, though.
3571e51764aSArtem Bityutskiy  */
calc_idx_growth(const struct ubifs_info * c,const struct ubifs_budget_req * req)3581e51764aSArtem Bityutskiy static int calc_idx_growth(const struct ubifs_info *c,
3591e51764aSArtem Bityutskiy 			   const struct ubifs_budget_req *req)
3601e51764aSArtem Bityutskiy {
3611e51764aSArtem Bityutskiy 	int znodes;
3621e51764aSArtem Bityutskiy 
3631e51764aSArtem Bityutskiy 	znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) +
3641e51764aSArtem Bityutskiy 		 req->new_dent;
3651e51764aSArtem Bityutskiy 	return znodes * c->max_idx_node_sz;
3661e51764aSArtem Bityutskiy }
3671e51764aSArtem Bityutskiy 
3681e51764aSArtem Bityutskiy /**
3691e51764aSArtem Bityutskiy  * calc_data_growth - calculate approximate amount of new data from budgeting
3701e51764aSArtem Bityutskiy  * request.
3711e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3721e51764aSArtem Bityutskiy  * @req: budgeting request
3731e51764aSArtem Bityutskiy  */
calc_data_growth(const struct ubifs_info * c,const struct ubifs_budget_req * req)3741e51764aSArtem Bityutskiy static int calc_data_growth(const struct ubifs_info *c,
3751e51764aSArtem Bityutskiy 			    const struct ubifs_budget_req *req)
3761e51764aSArtem Bityutskiy {
3771e51764aSArtem Bityutskiy 	int data_growth;
3781e51764aSArtem Bityutskiy 
379b137545cSArtem Bityutskiy 	data_growth = req->new_ino  ? c->bi.inode_budget : 0;
3801e51764aSArtem Bityutskiy 	if (req->new_page)
381b137545cSArtem Bityutskiy 		data_growth += c->bi.page_budget;
3821e51764aSArtem Bityutskiy 	if (req->new_dent)
383b137545cSArtem Bityutskiy 		data_growth += c->bi.dent_budget;
3841e51764aSArtem Bityutskiy 	data_growth += req->new_ino_d;
3851e51764aSArtem Bityutskiy 	return data_growth;
3861e51764aSArtem Bityutskiy }
3871e51764aSArtem Bityutskiy 
3881e51764aSArtem Bityutskiy /**
3891e51764aSArtem Bityutskiy  * calc_dd_growth - calculate approximate amount of data which makes other data
3901e51764aSArtem Bityutskiy  * dirty from budgeting request.
3911e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3921e51764aSArtem Bityutskiy  * @req: budgeting request
3931e51764aSArtem Bityutskiy  */
calc_dd_growth(const struct ubifs_info * c,const struct ubifs_budget_req * req)3941e51764aSArtem Bityutskiy static int calc_dd_growth(const struct ubifs_info *c,
3951e51764aSArtem Bityutskiy 			  const struct ubifs_budget_req *req)
3961e51764aSArtem Bityutskiy {
3971e51764aSArtem Bityutskiy 	int dd_growth;
3981e51764aSArtem Bityutskiy 
399b137545cSArtem Bityutskiy 	dd_growth = req->dirtied_page ? c->bi.page_budget : 0;
4001e51764aSArtem Bityutskiy 
4011e51764aSArtem Bityutskiy 	if (req->dirtied_ino)
402b248eaf0SZhihao Cheng 		dd_growth += c->bi.inode_budget * req->dirtied_ino;
4031e51764aSArtem Bityutskiy 	if (req->mod_dent)
404b137545cSArtem Bityutskiy 		dd_growth += c->bi.dent_budget;
4051e51764aSArtem Bityutskiy 	dd_growth += req->dirtied_ino_d;
4061e51764aSArtem Bityutskiy 	return dd_growth;
4071e51764aSArtem Bityutskiy }
4081e51764aSArtem Bityutskiy 
4091e51764aSArtem Bityutskiy /**
4101e51764aSArtem Bityutskiy  * ubifs_budget_space - ensure there is enough space to complete an operation.
4111e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4121e51764aSArtem Bityutskiy  * @req: budget request
4131e51764aSArtem Bityutskiy  *
4141e51764aSArtem Bityutskiy  * This function allocates budget for an operation. It uses pessimistic
4151e51764aSArtem Bityutskiy  * approximation of how much flash space the operation needs. The goal of this
4161e51764aSArtem Bityutskiy  * function is to make sure UBIFS always has flash space to flush all dirty
4171e51764aSArtem Bityutskiy  * pages, dirty inodes, and dirty znodes (liability). This function may force
4181e51764aSArtem Bityutskiy  * commit, garbage-collection or write-back. Returns zero in case of success,
4191e51764aSArtem Bityutskiy  * %-ENOSPC if there is no free space and other negative error codes in case of
4201e51764aSArtem Bityutskiy  * failures.
4211e51764aSArtem Bityutskiy  */
ubifs_budget_space(struct ubifs_info * c,struct ubifs_budget_req * req)4221e51764aSArtem Bityutskiy int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
4231e51764aSArtem Bityutskiy {
4242acf8067SArtem Bityutskiy 	int err, idx_growth, data_growth, dd_growth, retried = 0;
4251e51764aSArtem Bityutskiy 
4266eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_page <= 1);
4276eb61d58SRichard Weinberger 	ubifs_assert(c, req->dirtied_page <= 1);
4286eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_dent <= 1);
4296eb61d58SRichard Weinberger 	ubifs_assert(c, req->mod_dent <= 1);
4306eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_ino <= 1);
4316eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_ino_d <= UBIFS_MAX_INO_DATA);
4326eb61d58SRichard Weinberger 	ubifs_assert(c, req->dirtied_ino <= 4);
4336eb61d58SRichard Weinberger 	ubifs_assert(c, req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
4346eb61d58SRichard Weinberger 	ubifs_assert(c, !(req->new_ino_d & 7));
4356eb61d58SRichard Weinberger 	ubifs_assert(c, !(req->dirtied_ino_d & 7));
4361e51764aSArtem Bityutskiy 
4371e51764aSArtem Bityutskiy 	data_growth = calc_data_growth(c, req);
4381e51764aSArtem Bityutskiy 	dd_growth = calc_dd_growth(c, req);
4391e51764aSArtem Bityutskiy 	if (!data_growth && !dd_growth)
4401e51764aSArtem Bityutskiy 		return 0;
4411e51764aSArtem Bityutskiy 	idx_growth = calc_idx_growth(c, req);
4421e51764aSArtem Bityutskiy 
4431e51764aSArtem Bityutskiy again:
4441e51764aSArtem Bityutskiy 	spin_lock(&c->space_lock);
4456eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.idx_growth >= 0);
4466eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.data_growth >= 0);
4476eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.dd_growth >= 0);
4481e51764aSArtem Bityutskiy 
449b137545cSArtem Bityutskiy 	if (unlikely(c->bi.nospace) && (c->bi.nospace_rp || !can_use_rp(c))) {
4501e51764aSArtem Bityutskiy 		dbg_budg("no space");
4511e51764aSArtem Bityutskiy 		spin_unlock(&c->space_lock);
4521e51764aSArtem Bityutskiy 		return -ENOSPC;
4531e51764aSArtem Bityutskiy 	}
4541e51764aSArtem Bityutskiy 
455b137545cSArtem Bityutskiy 	c->bi.idx_growth += idx_growth;
456b137545cSArtem Bityutskiy 	c->bi.data_growth += data_growth;
457b137545cSArtem Bityutskiy 	c->bi.dd_growth += dd_growth;
4581e51764aSArtem Bityutskiy 
4591e51764aSArtem Bityutskiy 	err = do_budget_space(c);
4601e51764aSArtem Bityutskiy 	if (likely(!err)) {
4611e51764aSArtem Bityutskiy 		req->idx_growth = idx_growth;
4621e51764aSArtem Bityutskiy 		req->data_growth = data_growth;
4631e51764aSArtem Bityutskiy 		req->dd_growth = dd_growth;
4641e51764aSArtem Bityutskiy 		spin_unlock(&c->space_lock);
4651e51764aSArtem Bityutskiy 		return 0;
4661e51764aSArtem Bityutskiy 	}
4671e51764aSArtem Bityutskiy 
4681e51764aSArtem Bityutskiy 	/* Restore the old values */
469b137545cSArtem Bityutskiy 	c->bi.idx_growth -= idx_growth;
470b137545cSArtem Bityutskiy 	c->bi.data_growth -= data_growth;
471b137545cSArtem Bityutskiy 	c->bi.dd_growth -= dd_growth;
4721e51764aSArtem Bityutskiy 	spin_unlock(&c->space_lock);
4731e51764aSArtem Bityutskiy 
4741e51764aSArtem Bityutskiy 	if (req->fast) {
4751e51764aSArtem Bityutskiy 		dbg_budg("no space for fast budgeting");
4761e51764aSArtem Bityutskiy 		return err;
4771e51764aSArtem Bityutskiy 	}
4781e51764aSArtem Bityutskiy 
4792acf8067SArtem Bityutskiy 	err = make_free_space(c);
4802acf8067SArtem Bityutskiy 	cond_resched();
4811e51764aSArtem Bityutskiy 	if (err == -EAGAIN) {
4821e51764aSArtem Bityutskiy 		dbg_budg("try again");
4831e51764aSArtem Bityutskiy 		goto again;
4841e51764aSArtem Bityutskiy 	} else if (err == -ENOSPC) {
4852acf8067SArtem Bityutskiy 		if (!retried) {
4862acf8067SArtem Bityutskiy 			retried = 1;
4872acf8067SArtem Bityutskiy 			dbg_budg("-ENOSPC, but anyway try once again");
4882acf8067SArtem Bityutskiy 			goto again;
4892acf8067SArtem Bityutskiy 		}
4901e51764aSArtem Bityutskiy 		dbg_budg("FS is full, -ENOSPC");
491b137545cSArtem Bityutskiy 		c->bi.nospace = 1;
4921e51764aSArtem Bityutskiy 		if (can_use_rp(c) || c->rp_size == 0)
493b137545cSArtem Bityutskiy 			c->bi.nospace_rp = 1;
4941e51764aSArtem Bityutskiy 		smp_wmb();
4951e51764aSArtem Bityutskiy 	} else
496235c362bSSheng Yong 		ubifs_err(c, "cannot budget space, error %d", err);
4971e51764aSArtem Bityutskiy 	return err;
4981e51764aSArtem Bityutskiy }
4991e51764aSArtem Bityutskiy 
5001e51764aSArtem Bityutskiy /**
5011e51764aSArtem Bityutskiy  * ubifs_release_budget - release budgeted free space.
5021e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5031e51764aSArtem Bityutskiy  * @req: budget request
5041e51764aSArtem Bityutskiy  *
5051e51764aSArtem Bityutskiy  * This function releases the space budgeted by 'ubifs_budget_space()'. Note,
5061e51764aSArtem Bityutskiy  * since the index changes (which were budgeted for in @req->idx_growth) will
5071e51764aSArtem Bityutskiy  * only be written to the media on commit, this function moves the index budget
508b137545cSArtem Bityutskiy  * from @c->bi.idx_growth to @c->bi.uncommitted_idx. The latter will be zeroed
509b137545cSArtem Bityutskiy  * by the commit operation.
5101e51764aSArtem Bityutskiy  */
ubifs_release_budget(struct ubifs_info * c,struct ubifs_budget_req * req)5111e51764aSArtem Bityutskiy void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
5121e51764aSArtem Bityutskiy {
5136eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_page <= 1);
5146eb61d58SRichard Weinberger 	ubifs_assert(c, req->dirtied_page <= 1);
5156eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_dent <= 1);
5166eb61d58SRichard Weinberger 	ubifs_assert(c, req->mod_dent <= 1);
5176eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_ino <= 1);
5186eb61d58SRichard Weinberger 	ubifs_assert(c, req->new_ino_d <= UBIFS_MAX_INO_DATA);
5196eb61d58SRichard Weinberger 	ubifs_assert(c, req->dirtied_ino <= 4);
5206eb61d58SRichard Weinberger 	ubifs_assert(c, req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
5216eb61d58SRichard Weinberger 	ubifs_assert(c, !(req->new_ino_d & 7));
5226eb61d58SRichard Weinberger 	ubifs_assert(c, !(req->dirtied_ino_d & 7));
5231e51764aSArtem Bityutskiy 	if (!req->recalculate) {
5246eb61d58SRichard Weinberger 		ubifs_assert(c, req->idx_growth >= 0);
5256eb61d58SRichard Weinberger 		ubifs_assert(c, req->data_growth >= 0);
5266eb61d58SRichard Weinberger 		ubifs_assert(c, req->dd_growth >= 0);
5271e51764aSArtem Bityutskiy 	}
5281e51764aSArtem Bityutskiy 
5291e51764aSArtem Bityutskiy 	if (req->recalculate) {
5301e51764aSArtem Bityutskiy 		req->data_growth = calc_data_growth(c, req);
5311e51764aSArtem Bityutskiy 		req->dd_growth = calc_dd_growth(c, req);
5321e51764aSArtem Bityutskiy 		req->idx_growth = calc_idx_growth(c, req);
5331e51764aSArtem Bityutskiy 	}
5341e51764aSArtem Bityutskiy 
5351e51764aSArtem Bityutskiy 	if (!req->data_growth && !req->dd_growth)
5361e51764aSArtem Bityutskiy 		return;
5371e51764aSArtem Bityutskiy 
538b137545cSArtem Bityutskiy 	c->bi.nospace = c->bi.nospace_rp = 0;
5391e51764aSArtem Bityutskiy 	smp_wmb();
5401e51764aSArtem Bityutskiy 
5411e51764aSArtem Bityutskiy 	spin_lock(&c->space_lock);
542b137545cSArtem Bityutskiy 	c->bi.idx_growth -= req->idx_growth;
543b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx += req->idx_growth;
544b137545cSArtem Bityutskiy 	c->bi.data_growth -= req->data_growth;
545b137545cSArtem Bityutskiy 	c->bi.dd_growth -= req->dd_growth;
546b137545cSArtem Bityutskiy 	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
5471e51764aSArtem Bityutskiy 
5486eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.idx_growth >= 0);
5496eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.data_growth >= 0);
5506eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.dd_growth >= 0);
5516eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.min_idx_lebs < c->main_lebs);
5526eb61d58SRichard Weinberger 	ubifs_assert(c, !(c->bi.idx_growth & 7));
5536eb61d58SRichard Weinberger 	ubifs_assert(c, !(c->bi.data_growth & 7));
5546eb61d58SRichard Weinberger 	ubifs_assert(c, !(c->bi.dd_growth & 7));
5551e51764aSArtem Bityutskiy 	spin_unlock(&c->space_lock);
5561e51764aSArtem Bityutskiy }
5571e51764aSArtem Bityutskiy 
5581e51764aSArtem Bityutskiy /**
5591e51764aSArtem Bityutskiy  * ubifs_convert_page_budget - convert budget of a new page.
5601e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5611e51764aSArtem Bityutskiy  *
5621e51764aSArtem Bityutskiy  * This function converts budget which was allocated for a new page of data to
563025dfdafSFrederik Schwarzer  * the budget of changing an existing page of data. The latter is smaller than
5641e51764aSArtem Bityutskiy  * the former, so this function only does simple re-calculation and does not
5651e51764aSArtem Bityutskiy  * involve any write-back.
5661e51764aSArtem Bityutskiy  */
ubifs_convert_page_budget(struct ubifs_info * c)5671e51764aSArtem Bityutskiy void ubifs_convert_page_budget(struct ubifs_info *c)
5681e51764aSArtem Bityutskiy {
5691e51764aSArtem Bityutskiy 	spin_lock(&c->space_lock);
5701e51764aSArtem Bityutskiy 	/* Release the index growth reservation */
571b137545cSArtem Bityutskiy 	c->bi.idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
5721e51764aSArtem Bityutskiy 	/* Release the data growth reservation */
573b137545cSArtem Bityutskiy 	c->bi.data_growth -= c->bi.page_budget;
5741e51764aSArtem Bityutskiy 	/* Increase the dirty data growth reservation instead */
575b137545cSArtem Bityutskiy 	c->bi.dd_growth += c->bi.page_budget;
5761e51764aSArtem Bityutskiy 	/* And re-calculate the indexing space reservation */
577b137545cSArtem Bityutskiy 	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
5781e51764aSArtem Bityutskiy 	spin_unlock(&c->space_lock);
5791e51764aSArtem Bityutskiy }
5801e51764aSArtem Bityutskiy 
5811e51764aSArtem Bityutskiy /**
5821e51764aSArtem Bityutskiy  * ubifs_release_dirty_inode_budget - release dirty inode budget.
5831e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5841e51764aSArtem Bityutskiy  * @ui: UBIFS inode to release the budget for
5851e51764aSArtem Bityutskiy  *
5861e51764aSArtem Bityutskiy  * This function releases budget corresponding to a dirty inode. It is usually
5871e51764aSArtem Bityutskiy  * called when after the inode has been written to the media and marked as
5886d6cb0d6SAdrian Hunter  * clean. It also causes the "no space" flags to be cleared.
5891e51764aSArtem Bityutskiy  */
ubifs_release_dirty_inode_budget(struct ubifs_info * c,struct ubifs_inode * ui)5901e51764aSArtem Bityutskiy void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
5911e51764aSArtem Bityutskiy 				      struct ubifs_inode *ui)
5921e51764aSArtem Bityutskiy {
593182854b4SArtem Bityutskiy 	struct ubifs_budget_req req;
5941e51764aSArtem Bityutskiy 
595182854b4SArtem Bityutskiy 	memset(&req, 0, sizeof(struct ubifs_budget_req));
5966d6cb0d6SAdrian Hunter 	/* The "no space" flags will be cleared because dd_growth is > 0 */
597b137545cSArtem Bityutskiy 	req.dd_growth = c->bi.inode_budget + ALIGN(ui->data_len, 8);
5981e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
5991e51764aSArtem Bityutskiy }
6001e51764aSArtem Bityutskiy 
6011e51764aSArtem Bityutskiy /**
6024b5f2762SArtem Bityutskiy  * ubifs_reported_space - calculate reported free space.
6034b5f2762SArtem Bityutskiy  * @c: the UBIFS file-system description object
6044b5f2762SArtem Bityutskiy  * @free: amount of free space
6054b5f2762SArtem Bityutskiy  *
6064b5f2762SArtem Bityutskiy  * This function calculates amount of free space which will be reported to
6074b5f2762SArtem Bityutskiy  * user-space. User-space application tend to expect that if the file-system
6084b5f2762SArtem Bityutskiy  * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
6094b5f2762SArtem Bityutskiy  * are able to write a file of size N. UBIFS attaches node headers to each data
61080736d41SArtem Bityutskiy  * node and it has to write indexing nodes as well. This introduces additional
61180736d41SArtem Bityutskiy  * overhead, and UBIFS has to report slightly less free space to meet the above
61280736d41SArtem Bityutskiy  * expectations.
6134b5f2762SArtem Bityutskiy  *
6144b5f2762SArtem Bityutskiy  * This function assumes free space is made up of uncompressed data nodes and
6154b5f2762SArtem Bityutskiy  * full index nodes (one per data node, tripled because we always allow enough
6164b5f2762SArtem Bityutskiy  * space to write the index thrice).
6174b5f2762SArtem Bityutskiy  *
6184b5f2762SArtem Bityutskiy  * Note, the calculation is pessimistic, which means that most of the time
6194b5f2762SArtem Bityutskiy  * UBIFS reports less space than it actually has.
6204b5f2762SArtem Bityutskiy  */
ubifs_reported_space(const struct ubifs_info * c,long long free)6214d61db4fSArtem Bityutskiy long long ubifs_reported_space(const struct ubifs_info *c, long long free)
6224b5f2762SArtem Bityutskiy {
623f171d4d7SArtem Bityutskiy 	int divisor, factor, f;
6244b5f2762SArtem Bityutskiy 
6254b5f2762SArtem Bityutskiy 	/*
6264b5f2762SArtem Bityutskiy 	 * Reported space size is @free * X, where X is UBIFS block size
6274b5f2762SArtem Bityutskiy 	 * divided by UBIFS block size + all overhead one data block
6284b5f2762SArtem Bityutskiy 	 * introduces. The overhead is the node header + indexing overhead.
6294b5f2762SArtem Bityutskiy 	 *
630f171d4d7SArtem Bityutskiy 	 * Indexing overhead calculations are based on the following formula:
631f171d4d7SArtem Bityutskiy 	 * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number
632f171d4d7SArtem Bityutskiy 	 * of data nodes, f - fanout. Because effective UBIFS fanout is twice
633f171d4d7SArtem Bityutskiy 	 * as less than maximum fanout, we assume that each data node
6344b5f2762SArtem Bityutskiy 	 * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes.
63580736d41SArtem Bityutskiy 	 * Note, the multiplier 3 is because UBIFS reserves thrice as more space
6364b5f2762SArtem Bityutskiy 	 * for the index.
6374b5f2762SArtem Bityutskiy 	 */
638f171d4d7SArtem Bityutskiy 	f = c->fanout > 3 ? c->fanout >> 1 : 2;
6394b5f2762SArtem Bityutskiy 	factor = UBIFS_BLOCK_SIZE;
6404b5f2762SArtem Bityutskiy 	divisor = UBIFS_MAX_DATA_NODE_SZ;
641f171d4d7SArtem Bityutskiy 	divisor += (c->max_idx_node_sz * 3) / (f - 1);
6424b5f2762SArtem Bityutskiy 	free *= factor;
6434d61db4fSArtem Bityutskiy 	return div_u64(free, divisor);
6444b5f2762SArtem Bityutskiy }
6454b5f2762SArtem Bityutskiy 
6464b5f2762SArtem Bityutskiy /**
64784abf972SArtem Bityutskiy  * ubifs_get_free_space_nolock - return amount of free space.
6481e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6491e51764aSArtem Bityutskiy  *
6507dad181bSArtem Bityutskiy  * This function calculates amount of free space to report to user-space.
6517dad181bSArtem Bityutskiy  *
6527dad181bSArtem Bityutskiy  * Because UBIFS may introduce substantial overhead (the index, node headers,
653fb1cd01aSArtem Bityutskiy  * alignment, wastage at the end of LEBs, etc), it cannot report real amount of
654fb1cd01aSArtem Bityutskiy  * free flash space it has (well, because not all dirty space is reclaimable,
655fb1cd01aSArtem Bityutskiy  * UBIFS does not actually know the real amount). If UBIFS did so, it would
656fb1cd01aSArtem Bityutskiy  * bread user expectations about what free space is. Users seem to accustomed
657fb1cd01aSArtem Bityutskiy  * to assume that if the file-system reports N bytes of free space, they would
658fb1cd01aSArtem Bityutskiy  * be able to fit a file of N bytes to the FS. This almost works for
6597dad181bSArtem Bityutskiy  * traditional file-systems, because they have way less overhead than UBIFS.
6607dad181bSArtem Bityutskiy  * So, to keep users happy, UBIFS tries to take the overhead into account.
6611e51764aSArtem Bityutskiy  */
ubifs_get_free_space_nolock(struct ubifs_info * c)66284abf972SArtem Bityutskiy long long ubifs_get_free_space_nolock(struct ubifs_info *c)
6631e51764aSArtem Bityutskiy {
66484abf972SArtem Bityutskiy 	int rsvd_idx_lebs, lebs;
6651e51764aSArtem Bityutskiy 	long long available, outstanding, free;
6661e51764aSArtem Bityutskiy 
6676eb61d58SRichard Weinberger 	ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
668b137545cSArtem Bityutskiy 	outstanding = c->bi.data_growth + c->bi.dd_growth;
669b137545cSArtem Bityutskiy 	available = ubifs_calc_available(c, c->bi.min_idx_lebs);
6707dad181bSArtem Bityutskiy 
6717dad181bSArtem Bityutskiy 	/*
6727dad181bSArtem Bityutskiy 	 * When reporting free space to user-space, UBIFS guarantees that it is
6737dad181bSArtem Bityutskiy 	 * possible to write a file of free space size. This means that for
6747dad181bSArtem Bityutskiy 	 * empty LEBs we may use more precise calculations than
6757dad181bSArtem Bityutskiy 	 * 'ubifs_calc_available()' is using. Namely, we know that in empty
6767dad181bSArtem Bityutskiy 	 * LEBs we would waste only @c->leb_overhead bytes, not @c->dark_wm.
6777dad181bSArtem Bityutskiy 	 * Thus, amend the available space.
6787dad181bSArtem Bityutskiy 	 *
6797dad181bSArtem Bityutskiy 	 * Note, the calculations below are similar to what we have in
6807dad181bSArtem Bityutskiy 	 * 'do_budget_space()', so refer there for comments.
6817dad181bSArtem Bityutskiy 	 */
682b137545cSArtem Bityutskiy 	if (c->bi.min_idx_lebs > c->lst.idx_lebs)
683b137545cSArtem Bityutskiy 		rsvd_idx_lebs = c->bi.min_idx_lebs - c->lst.idx_lebs;
6847dad181bSArtem Bityutskiy 	else
6857dad181bSArtem Bityutskiy 		rsvd_idx_lebs = 0;
6867dad181bSArtem Bityutskiy 	lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
6877dad181bSArtem Bityutskiy 	       c->lst.taken_empty_lebs;
6887dad181bSArtem Bityutskiy 	lebs -= rsvd_idx_lebs;
6897dad181bSArtem Bityutskiy 	available += lebs * (c->dark_wm - c->leb_overhead);
6901e51764aSArtem Bityutskiy 
6911e51764aSArtem Bityutskiy 	if (available > outstanding)
6921e51764aSArtem Bityutskiy 		free = ubifs_reported_space(c, available - outstanding);
6931e51764aSArtem Bityutskiy 	else
6941e51764aSArtem Bityutskiy 		free = 0;
6951e51764aSArtem Bityutskiy 	return free;
6961e51764aSArtem Bityutskiy }
69784abf972SArtem Bityutskiy 
69884abf972SArtem Bityutskiy /**
69984abf972SArtem Bityutskiy  * ubifs_get_free_space - return amount of free space.
70084abf972SArtem Bityutskiy  * @c: UBIFS file-system description object
70184abf972SArtem Bityutskiy  *
702055da1b7SArtem Bityutskiy  * This function calculates and returns amount of free space to report to
70384abf972SArtem Bityutskiy  * user-space.
70484abf972SArtem Bityutskiy  */
ubifs_get_free_space(struct ubifs_info * c)70584abf972SArtem Bityutskiy long long ubifs_get_free_space(struct ubifs_info *c)
70684abf972SArtem Bityutskiy {
70784abf972SArtem Bityutskiy 	long long free;
70884abf972SArtem Bityutskiy 
70984abf972SArtem Bityutskiy 	spin_lock(&c->space_lock);
71084abf972SArtem Bityutskiy 	free = ubifs_get_free_space_nolock(c);
71184abf972SArtem Bityutskiy 	spin_unlock(&c->space_lock);
71284abf972SArtem Bityutskiy 
71384abf972SArtem Bityutskiy 	return free;
71484abf972SArtem Bityutskiy }
715