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