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