blob: 076ca50e99336fad10e256ddc3d103ae93fd527a [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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>
Artem Bityutskiy4d61db42008-12-18 14:06:51 +020035#include <linux/math64.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030036
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,
Artem Bityutskiy2acf8062008-12-09 11:04:40 -050040 * or committing. The below constant defines maximum number of times UBIFS
Artem Bityutskiy1e517642008-07-14 19:08:37 +030041 * repeats the operations.
42 */
Artem Bityutskiy2acf8062008-12-09 11:04:40 -050043#define MAX_MKSPC_RETRIES 3
Artem Bityutskiy1e517642008-07-14 19:08:37 +030044
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/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +030052 * 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
Jens Axboeb6e51312009-09-16 15:13:54 +020057 * of dirty inodes and their pages.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030058 *
59 * Note, this function synchronizes even VFS inodes which are locked
60 * (@i_mutex) by the caller of the budgeting function, because write-back does
61 * not touch @i_mutex.
62 */
Jens Axboeb6e51312009-09-16 15:13:54 +020063static void shrink_liability(struct ubifs_info *c, int nr_to_write)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030064{
Jens Axboeb6e51312009-09-16 15:13:54 +020065 writeback_inodes_sb(c->vfs_sb);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030066}
67
Artem Bityutskiy1e517642008-07-14 19:08:37 +030068/**
69 * run_gc - run garbage collector.
70 * @c: UBIFS file-system description object
71 *
72 * This function runs garbage collector to make some more free space. Returns
73 * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a
74 * negative error code in case of failure.
75 */
76static int run_gc(struct ubifs_info *c)
77{
78 int err, lnum;
79
80 /* Make some free space by garbage-collecting dirty space */
81 down_read(&c->commit_sem);
82 lnum = ubifs_garbage_collect(c, 1);
83 up_read(&c->commit_sem);
84 if (lnum < 0)
85 return lnum;
86
87 /* GC freed one LEB, return it to lprops */
88 dbg_budg("GC freed LEB %d", lnum);
89 err = ubifs_return_leb(c, lnum);
90 if (err)
91 return err;
92 return 0;
93}
94
95/**
Artem Bityutskiy2acf8062008-12-09 11:04:40 -050096 * get_liability - calculate current liability.
97 * @c: UBIFS file-system description object
98 *
99 * This function calculates and returns current UBIFS liability, i.e. the
100 * amount of bytes UBIFS has "promised" to write to the media.
101 */
102static long long get_liability(struct ubifs_info *c)
103{
104 long long liab;
105
106 spin_lock(&c->space_lock);
107 liab = c->budg_idx_growth + c->budg_data_growth + c->budg_dd_growth;
108 spin_unlock(&c->space_lock);
109 return liab;
110}
111
112/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300113 * make_free_space - make more free space on the file-system.
114 * @c: UBIFS file-system description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300115 *
116 * This function is called when an operation cannot be budgeted because there
117 * is supposedly no free space. But in most cases there is some free space:
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200118 * o budgeting is pessimistic, so it always budgets more than it is actually
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300119 * needed, so shrinking the liability is one way to make free space - the
120 * cached data will take less space then it was budgeted for;
121 * o GC may turn some dark space into free space (budgeting treats dark space
122 * as not available);
123 * o commit may free some LEB, i.e., turn freeable LEBs into free LEBs.
124 *
125 * So this function tries to do the above. Returns %-EAGAIN if some free space
126 * was presumably made and the caller has to re-try budgeting the operation.
127 * Returns %-ENOSPC if it couldn't do more free space, and other negative error
128 * codes on failures.
129 */
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500130static int make_free_space(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300131{
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500132 int err, retries = 0;
133 long long liab1, liab2;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300134
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500135 do {
136 liab1 = get_liability(c);
137 /*
138 * We probably have some dirty pages or inodes (liability), try
139 * to write them back.
140 */
141 dbg_budg("liability %lld, run write-back", liab1);
142 shrink_liability(c, NR_TO_WRITE);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300143
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500144 liab2 = get_liability(c);
145 if (liab2 < liab1)
146 return -EAGAIN;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300147
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500148 dbg_budg("new liability %lld (not shrinked)", liab2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300149
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500150 /* Liability did not shrink again, try GC */
151 dbg_budg("Run GC");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300152 err = run_gc(c);
153 if (!err)
154 return -EAGAIN;
155
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500156 if (err != -EAGAIN && err != -ENOSPC)
157 /* Some real error happened */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300158 return err;
159
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500160 dbg_budg("Run commit (retries %d)", retries);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300161 err = ubifs_run_commit(c);
162 if (err)
163 return err;
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500164 } while (retries++ < MAX_MKSPC_RETRIES);
165
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300166 return -ENOSPC;
167}
168
169/**
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200170 * ubifs_calc_min_idx_lebs - calculate amount of LEBs for the index.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300171 * @c: UBIFS file-system description object
172 *
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200173 * This function calculates and returns the number of LEBs which should be kept
174 * for index usage.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300175 */
176int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
177{
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200178 int idx_lebs;
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200179 long long idx_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300180
181 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
Adrian Hunter3a132522008-07-30 12:18:02 +0300182 /* And make sure we have thrice the index size of space reserved */
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200183 idx_size += idx_size << 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300184 /*
185 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
186 * pair, nor similarly the two variables for the new index size, so we
187 * have to do this costly 64-bit division on fast-path.
188 */
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200189 idx_lebs = div_u64(idx_size + c->idx_leb_size - 1, c->idx_leb_size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300190 /*
191 * The index head is not available for the in-the-gaps method, so add an
192 * extra LEB to compensate.
193 */
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200194 idx_lebs += 1;
195 if (idx_lebs < MIN_INDEX_LEBS)
196 idx_lebs = MIN_INDEX_LEBS;
197 return idx_lebs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300198}
199
200/**
201 * ubifs_calc_available - calculate available FS space.
202 * @c: UBIFS file-system description object
203 * @min_idx_lebs: minimum number of LEBs reserved for the index
204 *
205 * This function calculates and returns amount of FS space available for use.
206 */
207long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs)
208{
209 int subtract_lebs;
210 long long available;
211
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300212 available = c->main_bytes - c->lst.total_used;
213
214 /*
215 * Now 'available' contains theoretically available flash space
216 * assuming there is no index, so we have to subtract the space which
217 * is reserved for the index.
218 */
219 subtract_lebs = min_idx_lebs;
220
221 /* Take into account that GC reserves one LEB for its own needs */
222 subtract_lebs += 1;
223
224 /*
225 * The GC journal head LEB is not really accessible. And since
226 * different write types go to different heads, we may count only on
227 * one head's space.
228 */
229 subtract_lebs += c->jhead_cnt - 1;
230
231 /* We also reserve one LEB for deletions, which bypass budgeting */
232 subtract_lebs += 1;
233
234 available -= (long long)subtract_lebs * c->leb_size;
235
236 /* Subtract the dead space which is not available for use */
237 available -= c->lst.total_dead;
238
239 /*
240 * Subtract dark space, which might or might not be usable - it depends
241 * on the data which we have on the media and which will be written. If
242 * this is a lot of uncompressed or not-compressible data, the dark
243 * space cannot be used.
244 */
245 available -= c->lst.total_dark;
246
247 /*
248 * However, there is more dark space. The index may be bigger than
249 * @min_idx_lebs. Those extra LEBs are assumed to be available, but
250 * their dark space is not included in total_dark, so it is subtracted
251 * here.
252 */
253 if (c->lst.idx_lebs > min_idx_lebs) {
254 subtract_lebs = c->lst.idx_lebs - min_idx_lebs;
255 available -= subtract_lebs * c->dark_wm;
256 }
257
258 /* The calculations are rough and may end up with a negative number */
259 return available > 0 ? available : 0;
260}
261
262/**
263 * can_use_rp - check whether the user is allowed to use reserved pool.
264 * @c: UBIFS file-system description object
265 *
266 * UBIFS has so-called "reserved pool" which is flash space reserved
267 * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock.
268 * This function checks whether current user is allowed to use reserved pool.
269 * Returns %1 current user is allowed to use reserved pool and %0 otherwise.
270 */
271static int can_use_rp(struct ubifs_info *c)
272{
David Howells26bf1942008-11-14 10:39:03 +1100273 if (current_fsuid() == c->rp_uid || capable(CAP_SYS_RESOURCE) ||
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300274 (c->rp_gid != 0 && in_group_p(c->rp_gid)))
275 return 1;
276 return 0;
277}
278
279/**
280 * do_budget_space - reserve flash space for index and data growth.
281 * @c: UBIFS file-system description object
282 *
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200283 * This function makes sure UBIFS has enough free LEBs for index growth and
284 * data.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300285 *
Adrian Hunter3a132522008-07-30 12:18:02 +0300286 * When budgeting index space, UBIFS reserves thrice as many LEBs as the index
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300287 * would take if it was consolidated and written to the flash. This guarantees
288 * that the "in-the-gaps" commit method always succeeds and UBIFS will always
289 * be able to commit dirty index. So this function basically adds amount of
Artem Bityutskiyb364b412008-07-25 14:38:51 +0300290 * budgeted index space to the size of the current index, multiplies this by 3,
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200291 * and makes sure this does not exceed the amount of free LEBs.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300292 *
293 * Notes about @c->min_idx_lebs and @c->lst.idx_lebs variables:
294 * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might
295 * be large, because UBIFS does not do any index consolidation as long as
296 * there is free space. IOW, the index may take a lot of LEBs, but the LEBs
297 * will contain a lot of dirt.
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +0200298 * o @c->min_idx_lebs is the number of LEBS the index presumably takes. IOW,
299 * the index may be consolidated to take up to @c->min_idx_lebs LEBs.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300300 *
301 * This function returns zero in case of success, and %-ENOSPC in case of
302 * failure.
303 */
304static int do_budget_space(struct ubifs_info *c)
305{
306 long long outstanding, available;
307 int lebs, rsvd_idx_lebs, min_idx_lebs;
308
309 /* First budget index space */
310 min_idx_lebs = ubifs_calc_min_idx_lebs(c);
311
312 /* Now 'min_idx_lebs' contains number of LEBs to reserve */
313 if (min_idx_lebs > c->lst.idx_lebs)
314 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
315 else
316 rsvd_idx_lebs = 0;
317
318 /*
319 * The number of LEBs that are available to be used by the index is:
320 *
321 * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt -
322 * @c->lst.taken_empty_lebs
323 *
Artem Bityutskiy948cfb22008-08-20 11:56:33 +0300324 * @c->lst.empty_lebs are available because they are empty.
325 * @c->freeable_cnt are available because they contain only free and
326 * dirty space, @c->idx_gc_cnt are available because they are index
327 * LEBs that have been garbage collected and are awaiting the commit
328 * before they can be used. And the in-the-gaps method will grab these
329 * if it needs them. @c->lst.taken_empty_lebs are empty LEBs that have
330 * already been allocated for some purpose.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300331 *
Artem Bityutskiy948cfb22008-08-20 11:56:33 +0300332 * Note, @c->idx_gc_cnt is included to both @c->lst.empty_lebs (because
333 * these LEBs are empty) and to @c->lst.taken_empty_lebs (because they
334 * are taken until after the commit).
335 *
336 * Note, @c->lst.taken_empty_lebs may temporarily be higher by one
337 * because of the way we serialize LEB allocations and budgeting. See a
338 * comment in 'ubifs_find_free_space()'.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300339 */
340 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
341 c->lst.taken_empty_lebs;
342 if (unlikely(rsvd_idx_lebs > lebs)) {
343 dbg_budg("out of indexing space: min_idx_lebs %d (old %d), "
344 "rsvd_idx_lebs %d", min_idx_lebs, c->min_idx_lebs,
345 rsvd_idx_lebs);
346 return -ENOSPC;
347 }
348
349 available = ubifs_calc_available(c, min_idx_lebs);
350 outstanding = c->budg_data_growth + c->budg_dd_growth;
351
352 if (unlikely(available < outstanding)) {
353 dbg_budg("out of data space: available %lld, outstanding %lld",
354 available, outstanding);
355 return -ENOSPC;
356 }
357
358 if (available - outstanding <= c->rp_size && !can_use_rp(c))
359 return -ENOSPC;
360
361 c->min_idx_lebs = min_idx_lebs;
362 return 0;
363}
364
365/**
366 * calc_idx_growth - calculate approximate index growth from budgeting request.
367 * @c: UBIFS file-system description object
368 * @req: budgeting request
369 *
370 * For now we assume each new node adds one znode. But this is rather poor
371 * approximation, though.
372 */
373static int calc_idx_growth(const struct ubifs_info *c,
374 const struct ubifs_budget_req *req)
375{
376 int znodes;
377
378 znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) +
379 req->new_dent;
380 return znodes * c->max_idx_node_sz;
381}
382
383/**
384 * calc_data_growth - calculate approximate amount of new data from budgeting
385 * request.
386 * @c: UBIFS file-system description object
387 * @req: budgeting request
388 */
389static int calc_data_growth(const struct ubifs_info *c,
390 const struct ubifs_budget_req *req)
391{
392 int data_growth;
393
394 data_growth = req->new_ino ? c->inode_budget : 0;
395 if (req->new_page)
396 data_growth += c->page_budget;
397 if (req->new_dent)
398 data_growth += c->dent_budget;
399 data_growth += req->new_ino_d;
400 return data_growth;
401}
402
403/**
404 * calc_dd_growth - calculate approximate amount of data which makes other data
405 * dirty from budgeting request.
406 * @c: UBIFS file-system description object
407 * @req: budgeting request
408 */
409static int calc_dd_growth(const struct ubifs_info *c,
410 const struct ubifs_budget_req *req)
411{
412 int dd_growth;
413
414 dd_growth = req->dirtied_page ? c->page_budget : 0;
415
416 if (req->dirtied_ino)
417 dd_growth += c->inode_budget << (req->dirtied_ino - 1);
418 if (req->mod_dent)
419 dd_growth += c->dent_budget;
420 dd_growth += req->dirtied_ino_d;
421 return dd_growth;
422}
423
424/**
425 * ubifs_budget_space - ensure there is enough space to complete an operation.
426 * @c: UBIFS file-system description object
427 * @req: budget request
428 *
429 * This function allocates budget for an operation. It uses pessimistic
430 * approximation of how much flash space the operation needs. The goal of this
431 * function is to make sure UBIFS always has flash space to flush all dirty
432 * pages, dirty inodes, and dirty znodes (liability). This function may force
433 * commit, garbage-collection or write-back. Returns zero in case of success,
434 * %-ENOSPC if there is no free space and other negative error codes in case of
435 * failures.
436 */
437int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
438{
439 int uninitialized_var(cmt_retries), uninitialized_var(wb_retries);
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500440 int err, idx_growth, data_growth, dd_growth, retried = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300441
Artem Bityutskiy547000d2008-07-24 14:42:05 +0300442 ubifs_assert(req->new_page <= 1);
443 ubifs_assert(req->dirtied_page <= 1);
444 ubifs_assert(req->new_dent <= 1);
445 ubifs_assert(req->mod_dent <= 1);
446 ubifs_assert(req->new_ino <= 1);
447 ubifs_assert(req->new_ino_d <= UBIFS_MAX_INO_DATA);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300448 ubifs_assert(req->dirtied_ino <= 4);
449 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300450 ubifs_assert(!(req->new_ino_d & 7));
451 ubifs_assert(!(req->dirtied_ino_d & 7));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300452
453 data_growth = calc_data_growth(c, req);
454 dd_growth = calc_dd_growth(c, req);
455 if (!data_growth && !dd_growth)
456 return 0;
457 idx_growth = calc_idx_growth(c, req);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300458
459again:
460 spin_lock(&c->space_lock);
461 ubifs_assert(c->budg_idx_growth >= 0);
462 ubifs_assert(c->budg_data_growth >= 0);
463 ubifs_assert(c->budg_dd_growth >= 0);
464
465 if (unlikely(c->nospace) && (c->nospace_rp || !can_use_rp(c))) {
466 dbg_budg("no space");
467 spin_unlock(&c->space_lock);
468 return -ENOSPC;
469 }
470
471 c->budg_idx_growth += idx_growth;
472 c->budg_data_growth += data_growth;
473 c->budg_dd_growth += dd_growth;
474
475 err = do_budget_space(c);
476 if (likely(!err)) {
477 req->idx_growth = idx_growth;
478 req->data_growth = data_growth;
479 req->dd_growth = dd_growth;
480 spin_unlock(&c->space_lock);
481 return 0;
482 }
483
484 /* Restore the old values */
485 c->budg_idx_growth -= idx_growth;
486 c->budg_data_growth -= data_growth;
487 c->budg_dd_growth -= dd_growth;
488 spin_unlock(&c->space_lock);
489
490 if (req->fast) {
491 dbg_budg("no space for fast budgeting");
492 return err;
493 }
494
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500495 err = make_free_space(c);
496 cond_resched();
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300497 if (err == -EAGAIN) {
498 dbg_budg("try again");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300499 goto again;
500 } else if (err == -ENOSPC) {
Artem Bityutskiy2acf8062008-12-09 11:04:40 -0500501 if (!retried) {
502 retried = 1;
503 dbg_budg("-ENOSPC, but anyway try once again");
504 goto again;
505 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300506 dbg_budg("FS is full, -ENOSPC");
507 c->nospace = 1;
508 if (can_use_rp(c) || c->rp_size == 0)
509 c->nospace_rp = 1;
510 smp_wmb();
511 } else
512 ubifs_err("cannot budget space, error %d", err);
513 return err;
514}
515
516/**
517 * ubifs_release_budget - release budgeted free space.
518 * @c: UBIFS file-system description object
519 * @req: budget request
520 *
521 * This function releases the space budgeted by 'ubifs_budget_space()'. Note,
522 * since the index changes (which were budgeted for in @req->idx_growth) will
523 * only be written to the media on commit, this function moves the index budget
524 * from @c->budg_idx_growth to @c->budg_uncommitted_idx. The latter will be
525 * zeroed by the commit operation.
526 */
527void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
528{
Artem Bityutskiy547000d2008-07-24 14:42:05 +0300529 ubifs_assert(req->new_page <= 1);
530 ubifs_assert(req->dirtied_page <= 1);
531 ubifs_assert(req->new_dent <= 1);
532 ubifs_assert(req->mod_dent <= 1);
533 ubifs_assert(req->new_ino <= 1);
534 ubifs_assert(req->new_ino_d <= UBIFS_MAX_INO_DATA);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300535 ubifs_assert(req->dirtied_ino <= 4);
536 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300537 ubifs_assert(!(req->new_ino_d & 7));
538 ubifs_assert(!(req->dirtied_ino_d & 7));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300539 if (!req->recalculate) {
540 ubifs_assert(req->idx_growth >= 0);
541 ubifs_assert(req->data_growth >= 0);
542 ubifs_assert(req->dd_growth >= 0);
543 }
544
545 if (req->recalculate) {
546 req->data_growth = calc_data_growth(c, req);
547 req->dd_growth = calc_dd_growth(c, req);
548 req->idx_growth = calc_idx_growth(c, req);
549 }
550
551 if (!req->data_growth && !req->dd_growth)
552 return;
553
554 c->nospace = c->nospace_rp = 0;
555 smp_wmb();
556
557 spin_lock(&c->space_lock);
558 c->budg_idx_growth -= req->idx_growth;
559 c->budg_uncommitted_idx += req->idx_growth;
560 c->budg_data_growth -= req->data_growth;
561 c->budg_dd_growth -= req->dd_growth;
562 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
563
564 ubifs_assert(c->budg_idx_growth >= 0);
565 ubifs_assert(c->budg_data_growth >= 0);
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300566 ubifs_assert(c->budg_dd_growth >= 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300567 ubifs_assert(c->min_idx_lebs < c->main_lebs);
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300568 ubifs_assert(!(c->budg_idx_growth & 7));
569 ubifs_assert(!(c->budg_data_growth & 7));
570 ubifs_assert(!(c->budg_dd_growth & 7));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300571 spin_unlock(&c->space_lock);
572}
573
574/**
575 * ubifs_convert_page_budget - convert budget of a new page.
576 * @c: UBIFS file-system description object
577 *
578 * This function converts budget which was allocated for a new page of data to
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200579 * the budget of changing an existing page of data. The latter is smaller than
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300580 * the former, so this function only does simple re-calculation and does not
581 * involve any write-back.
582 */
583void ubifs_convert_page_budget(struct ubifs_info *c)
584{
585 spin_lock(&c->space_lock);
586 /* Release the index growth reservation */
587 c->budg_idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
588 /* Release the data growth reservation */
589 c->budg_data_growth -= c->page_budget;
590 /* Increase the dirty data growth reservation instead */
591 c->budg_dd_growth += c->page_budget;
592 /* And re-calculate the indexing space reservation */
593 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
594 spin_unlock(&c->space_lock);
595}
596
597/**
598 * ubifs_release_dirty_inode_budget - release dirty inode budget.
599 * @c: UBIFS file-system description object
600 * @ui: UBIFS inode to release the budget for
601 *
602 * This function releases budget corresponding to a dirty inode. It is usually
603 * called when after the inode has been written to the media and marked as
Adrian Hunter6d6cb0d2009-04-08 14:07:57 +0200604 * clean. It also causes the "no space" flags to be cleared.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300605 */
606void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
607 struct ubifs_inode *ui)
608{
Artem Bityutskiy182854b2008-07-18 18:54:29 +0300609 struct ubifs_budget_req req;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300610
Artem Bityutskiy182854b2008-07-18 18:54:29 +0300611 memset(&req, 0, sizeof(struct ubifs_budget_req));
Adrian Hunter6d6cb0d2009-04-08 14:07:57 +0200612 /* The "no space" flags will be cleared because dd_growth is > 0 */
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300613 req.dd_growth = c->inode_budget + ALIGN(ui->data_len, 8);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300614 ubifs_release_budget(c, &req);
615}
616
617/**
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300618 * ubifs_reported_space - calculate reported free space.
619 * @c: the UBIFS file-system description object
620 * @free: amount of free space
621 *
622 * This function calculates amount of free space which will be reported to
623 * user-space. User-space application tend to expect that if the file-system
624 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
625 * are able to write a file of size N. UBIFS attaches node headers to each data
Artem Bityutskiy80736d42008-12-30 17:44:02 +0200626 * node and it has to write indexing nodes as well. This introduces additional
627 * overhead, and UBIFS has to report slightly less free space to meet the above
628 * expectations.
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300629 *
630 * This function assumes free space is made up of uncompressed data nodes and
631 * full index nodes (one per data node, tripled because we always allow enough
632 * space to write the index thrice).
633 *
634 * Note, the calculation is pessimistic, which means that most of the time
635 * UBIFS reports less space than it actually has.
636 */
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200637long long ubifs_reported_space(const struct ubifs_info *c, long long free)
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300638{
Artem Bityutskiyf171d4d2008-09-03 16:17:14 +0300639 int divisor, factor, f;
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300640
641 /*
642 * Reported space size is @free * X, where X is UBIFS block size
643 * divided by UBIFS block size + all overhead one data block
644 * introduces. The overhead is the node header + indexing overhead.
645 *
Artem Bityutskiyf171d4d2008-09-03 16:17:14 +0300646 * Indexing overhead calculations are based on the following formula:
647 * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number
648 * of data nodes, f - fanout. Because effective UBIFS fanout is twice
649 * as less than maximum fanout, we assume that each data node
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300650 * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes.
Artem Bityutskiy80736d42008-12-30 17:44:02 +0200651 * Note, the multiplier 3 is because UBIFS reserves thrice as more space
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300652 * for the index.
653 */
Artem Bityutskiyf171d4d2008-09-03 16:17:14 +0300654 f = c->fanout > 3 ? c->fanout >> 1 : 2;
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300655 factor = UBIFS_BLOCK_SIZE;
656 divisor = UBIFS_MAX_DATA_NODE_SZ;
Artem Bityutskiyf171d4d2008-09-03 16:17:14 +0300657 divisor += (c->max_idx_node_sz * 3) / (f - 1);
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300658 free *= factor;
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200659 return div_u64(free, divisor);
Artem Bityutskiy4b5f2762008-08-25 16:15:56 +0300660}
661
662/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200663 * ubifs_get_free_space_nolock - return amount of free space.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300664 * @c: UBIFS file-system description object
665 *
Artem Bityutskiy7dad1812008-08-25 18:58:19 +0300666 * This function calculates amount of free space to report to user-space.
667 *
668 * Because UBIFS may introduce substantial overhead (the index, node headers,
Artem Bityutskiyfb1cd012009-03-16 09:56:57 +0200669 * 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
Artem Bityutskiy7dad1812008-08-25 18:58:19 +0300675 * 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.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300677 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200678long long ubifs_get_free_space_nolock(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300679{
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200680 int rsvd_idx_lebs, lebs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300681 long long available, outstanding, free;
682
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200683 ubifs_assert(c->min_idx_lebs == ubifs_calc_min_idx_lebs(c));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300684 outstanding = c->budg_data_growth + c->budg_dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200685 available = ubifs_calc_available(c, c->min_idx_lebs);
Artem Bityutskiy7dad1812008-08-25 18:58:19 +0300686
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 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200698 if (c->min_idx_lebs > c->lst.idx_lebs)
699 rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
Artem Bityutskiy7dad1812008-08-25 18:58:19 +0300700 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);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300706
707 if (available > outstanding)
708 free = ubifs_reported_space(c, available - outstanding);
709 else
710 free = 0;
711 return free;
712}
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200713
714/**
715 * ubifs_get_free_space - return amount of free space.
716 * @c: UBIFS file-system description object
717 *
Artem Bityutskiy055da1b2009-09-15 17:09:24 +0300718 * This function calculates and returns amount of free space to report to
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200719 * user-space.
720 */
721long 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}