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