Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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