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