Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-only
2 : /*
3 : * This file is part of UBIFS.
4 : *
5 : * Copyright (C) 2006-2008 Nokia Corporation.
6 : *
7 : * Authors: Artem Bityutskiy (Битюцкий Артём)
8 : * Adrian Hunter
9 : */
10 :
11 : /*
12 : * This file is a part of UBIFS journal implementation and contains various
13 : * functions which manipulate the log. The log is a fixed area on the flash
14 : * which does not contain any data but refers to buds. The log is a part of the
15 : * journal.
16 : */
17 :
18 : #include "linux_err.h"
19 : #include "bitops.h"
20 : #include "kmem.h"
21 : #include "ubifs.h"
22 : #include "defs.h"
23 : #include "debug.h"
24 : #include "misc.h"
25 :
26 : static int dbg_check_bud_bytes(struct ubifs_info *c);
27 :
28 : /**
29 : * ubifs_search_bud - search bud LEB.
30 : * @c: UBIFS file-system description object
31 : * @lnum: logical eraseblock number to search
32 : *
33 : * This function searches bud LEB @lnum. Returns bud description object in case
34 : * of success and %NULL if there is no bud with this LEB number.
35 : */
36 13530 : struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
37 : {
38 : struct rb_node *p;
39 : struct ubifs_bud *bud;
40 :
41 13530 : spin_lock(&c->buds_lock);
42 13530 : p = c->buds.rb_node;
43 54392 : while (p) {
44 34412 : bud = rb_entry(p, struct ubifs_bud, rb);
45 34412 : if (lnum < bud->lnum)
46 12909 : p = p->rb_left;
47 21503 : else if (lnum > bud->lnum)
48 14423 : p = p->rb_right;
49 : else {
50 7080 : spin_unlock(&c->buds_lock);
51 7080 : return bud;
52 : }
53 : }
54 6450 : spin_unlock(&c->buds_lock);
55 6450 : return NULL;
56 : }
57 :
58 : /**
59 : * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
60 : * @c: UBIFS file-system description object
61 : * @lnum: logical eraseblock number to search
62 : *
63 : * This functions returns the wbuf for @lnum or %NULL if there is not one.
64 : */
65 2491640519 : struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
66 : {
67 : struct rb_node *p;
68 : struct ubifs_bud *bud;
69 : int jhead;
70 :
71 2491640519 : if (!c->jheads)
72 : return NULL;
73 :
74 2491640519 : spin_lock(&c->buds_lock);
75 2491640519 : p = c->buds.rb_node;
76 11359871683 : while (p) {
77 6380900906 : bud = rb_entry(p, struct ubifs_bud, rb);
78 6380900906 : if (lnum < bud->lnum)
79 2943460617 : p = p->rb_left;
80 3437440289 : else if (lnum > bud->lnum)
81 3433130028 : p = p->rb_right;
82 : else {
83 4310261 : jhead = bud->jhead;
84 4310261 : spin_unlock(&c->buds_lock);
85 4310261 : return &c->jheads[jhead].wbuf;
86 : }
87 : }
88 2487330258 : spin_unlock(&c->buds_lock);
89 2487330258 : return NULL;
90 : }
91 :
92 : /**
93 : * empty_log_bytes - calculate amount of empty space in the log.
94 : * @c: UBIFS file-system description object
95 : */
96 : static inline long long empty_log_bytes(const struct ubifs_info *c)
97 : {
98 : long long h, t;
99 :
100 12 : h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
101 12 : t = (long long)c->ltail_lnum * c->leb_size;
102 :
103 12 : if (h > t)
104 12 : return c->log_bytes - h + t;
105 0 : else if (h != t)
106 0 : return t - h;
107 0 : else if (c->lhead_lnum != c->ltail_lnum)
108 : return 0;
109 : else
110 0 : return c->log_bytes;
111 : }
112 :
113 : /**
114 : * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
115 : * @c: UBIFS file-system description object
116 : * @bud: the bud to add
117 : */
118 6462 : void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
119 : {
120 6462 : struct rb_node **p, *parent = NULL;
121 : struct ubifs_bud *b;
122 : struct ubifs_jhead *jhead;
123 :
124 6462 : spin_lock(&c->buds_lock);
125 6462 : p = &c->buds.rb_node;
126 27294 : while (*p) {
127 14370 : parent = *p;
128 14370 : b = rb_entry(parent, struct ubifs_bud, rb);
129 14370 : ubifs_assert(c, bud->lnum != b->lnum);
130 14370 : if (bud->lnum < b->lnum)
131 6632 : p = &(*p)->rb_left;
132 : else
133 7738 : p = &(*p)->rb_right;
134 : }
135 :
136 12924 : rb_link_node(&bud->rb, parent, p);
137 6462 : rb_insert_color(&bud->rb, &c->buds);
138 6462 : if (c->jheads) {
139 6462 : jhead = &c->jheads[bud->jhead];
140 6462 : list_add_tail(&bud->list, &jhead->buds_list);
141 : } else
142 0 : ubifs_assert(c, c->replaying && c->ro_mount);
143 :
144 : /*
145 : * Note, although this is a new bud, we anyway account this space now,
146 : * before any data has been written to it, because this is about to
147 : * guarantee fixed mount time, and this bud will anyway be read and
148 : * scanned.
149 : */
150 6462 : c->bud_bytes += c->leb_size - bud->start;
151 :
152 6462 : dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
153 : bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
154 6462 : spin_unlock(&c->buds_lock);
155 6462 : }
156 :
157 : /**
158 : * ubifs_add_bud_to_log - add a new bud to the log.
159 : * @c: UBIFS file-system description object
160 : * @jhead: journal head the bud belongs to
161 : * @lnum: LEB number of the bud
162 : * @offs: starting offset of the bud
163 : *
164 : * This function writes a reference node for the new bud LEB @lnum to the log,
165 : * and adds it to the buds trees. It also makes sure that log size does not
166 : * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
167 : * %-EAGAIN if commit is required, and a negative error code in case of
168 : * failure.
169 : */
170 12 : int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
171 : {
172 : int err;
173 : struct ubifs_bud *bud;
174 : struct ubifs_ref_node *ref;
175 :
176 12 : bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
177 12 : if (!bud)
178 : return -ENOMEM;
179 24 : ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
180 12 : if (!ref) {
181 0 : kfree(bud);
182 0 : return -ENOMEM;
183 : }
184 :
185 12 : mutex_lock(&c->log_mutex);
186 12 : ubifs_assert(c, !c->ro_media && !c->ro_mount);
187 12 : if (c->ro_error) {
188 : err = -EROFS;
189 : goto out_unlock;
190 : }
191 :
192 : /* Make sure we have enough space in the log */
193 12 : if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
194 0 : dbg_log("not enough log space - %lld, required %d",
195 : empty_log_bytes(c), c->min_log_bytes);
196 0 : ubifs_commit_required(c);
197 0 : err = -EAGAIN;
198 0 : goto out_unlock;
199 : }
200 :
201 : /*
202 : * Make sure the amount of space in buds will not exceed the
203 : * 'c->max_bud_bytes' limit, because we want to guarantee mount time
204 : * limits.
205 : *
206 : * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
207 : * because we are holding @c->log_mutex. All @c->bud_bytes take place
208 : * when both @c->log_mutex and @c->bud_bytes are locked.
209 : */
210 12 : if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
211 0 : dbg_log("bud bytes %lld (%lld max), require commit",
212 : c->bud_bytes, c->max_bud_bytes);
213 0 : ubifs_commit_required(c);
214 0 : err = -EAGAIN;
215 0 : goto out_unlock;
216 : }
217 :
218 : /*
219 : * If the journal is full enough - start background commit. Note, it is
220 : * OK to read 'c->cmt_state' without spinlock because integer reads
221 : * are atomic in the kernel.
222 : */
223 12 : if (c->bud_bytes >= c->bg_bud_bytes &&
224 0 : c->cmt_state == COMMIT_RESTING) {
225 0 : dbg_log("bud bytes %lld (%lld max), initiate BG commit",
226 : c->bud_bytes, c->max_bud_bytes);
227 0 : ubifs_request_bg_commit(c);
228 : }
229 :
230 12 : bud->lnum = lnum;
231 12 : bud->start = offs;
232 12 : bud->jhead = jhead;
233 12 : bud->log_hash = NULL;
234 :
235 12 : ref->ch.node_type = UBIFS_REF_NODE;
236 12 : ref->lnum = cpu_to_le32(bud->lnum);
237 12 : ref->offs = cpu_to_le32(bud->start);
238 12 : ref->jhead = cpu_to_le32(jhead);
239 :
240 12 : if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
241 0 : c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
242 0 : ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
243 0 : c->lhead_offs = 0;
244 : }
245 :
246 12 : if (c->lhead_offs == 0) {
247 : /* Must ensure next log LEB has been unmapped */
248 0 : err = ubifs_leb_unmap(c, c->lhead_lnum);
249 0 : if (err)
250 : goto out_unlock;
251 : }
252 :
253 12 : if (bud->start == 0) {
254 : /*
255 : * Before writing the LEB reference which refers an empty LEB
256 : * to the log, we have to make sure it is mapped, because
257 : * otherwise we'd risk to refer an LEB with garbage in case of
258 : * an unclean reboot, because the target LEB might have been
259 : * unmapped, but not yet physically erased.
260 : */
261 10 : err = ubifs_leb_map(c, bud->lnum);
262 10 : if (err)
263 : goto out_unlock;
264 : }
265 :
266 12 : dbg_log("write ref LEB %d:%d",
267 : c->lhead_lnum, c->lhead_offs);
268 12 : err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
269 : c->lhead_offs);
270 12 : if (err)
271 : goto out_unlock;
272 :
273 12 : err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ);
274 12 : if (err)
275 : goto out_unlock;
276 :
277 12 : err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash);
278 : if (err)
279 : goto out_unlock;
280 :
281 12 : c->lhead_offs += c->ref_node_alsz;
282 :
283 12 : ubifs_add_bud(c, bud);
284 :
285 12 : mutex_unlock(&c->log_mutex);
286 12 : kfree(ref);
287 12 : return 0;
288 :
289 0 : out_unlock:
290 0 : mutex_unlock(&c->log_mutex);
291 0 : kfree(ref);
292 0 : kfree(bud);
293 0 : return err;
294 : }
295 :
296 : /**
297 : * remove_buds - remove used buds.
298 : * @c: UBIFS file-system description object
299 : *
300 : * This function removes use buds from the buds tree. It does not remove the
301 : * buds which are pointed to by journal heads.
302 : */
303 3427 : static void remove_buds(struct ubifs_info *c)
304 : {
305 : struct rb_node *p;
306 :
307 6854 : ubifs_assert(c, list_empty(&c->old_buds));
308 3427 : c->cmt_bud_bytes = 0;
309 3427 : spin_lock(&c->buds_lock);
310 3427 : p = rb_first(&c->buds);
311 12427 : while (p) {
312 5573 : struct rb_node *p1 = p;
313 : struct ubifs_bud *bud;
314 : struct ubifs_wbuf *wbuf;
315 :
316 5573 : p = rb_next(p);
317 5573 : bud = rb_entry(p1, struct ubifs_bud, rb);
318 5573 : wbuf = &c->jheads[bud->jhead].wbuf;
319 :
320 5573 : if (wbuf->lnum == bud->lnum) {
321 : /*
322 : * Do not remove buds which are pointed to by journal
323 : * heads (non-closed buds).
324 : */
325 4130 : c->cmt_bud_bytes += wbuf->offs - bud->start;
326 4130 : dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
327 : bud->lnum, bud->start, dbg_jhead(bud->jhead),
328 : wbuf->offs - bud->start, c->cmt_bud_bytes);
329 4130 : bud->start = wbuf->offs;
330 : } else {
331 1443 : c->cmt_bud_bytes += c->leb_size - bud->start;
332 1443 : dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
333 : bud->lnum, bud->start, dbg_jhead(bud->jhead),
334 : c->leb_size - bud->start, c->cmt_bud_bytes);
335 1443 : rb_erase(p1, &c->buds);
336 : /*
337 : * If the commit does not finish, the recovery will need
338 : * to replay the journal, in which case the old buds
339 : * must be unchanged. Do not release them until post
340 : * commit i.e. do not allow them to be garbage
341 : * collected.
342 : */
343 1443 : list_move(&bud->list, &c->old_buds);
344 : }
345 : }
346 3427 : spin_unlock(&c->buds_lock);
347 3427 : }
348 :
349 : /**
350 : * ubifs_log_start_commit - start commit.
351 : * @c: UBIFS file-system description object
352 : * @ltail_lnum: return new log tail LEB number
353 : *
354 : * The commit operation starts with writing "commit start" node to the log and
355 : * reference nodes for all journal heads which will define new journal after
356 : * the commit has been finished. The commit start and reference nodes are
357 : * written in one go to the nearest empty log LEB (hence, when commit is
358 : * finished UBIFS may safely unmap all the previous log LEBs). This function
359 : * returns zero in case of success and a negative error code in case of
360 : * failure.
361 : */
362 3427 : int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
363 : {
364 : void *buf;
365 : struct ubifs_cs_node *cs;
366 : struct ubifs_ref_node *ref;
367 : int err, i, max_len, len;
368 :
369 3427 : err = dbg_check_bud_bytes(c);
370 : if (err)
371 : return err;
372 :
373 3427 : max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
374 3427 : max_len = ALIGN(max_len, c->min_io_size);
375 3427 : buf = cs = kmalloc(max_len, GFP_NOFS);
376 3427 : if (!buf)
377 : return -ENOMEM;
378 :
379 3427 : cs->ch.node_type = UBIFS_CS_NODE;
380 3427 : cs->cmt_no = cpu_to_le64(c->cmt_no);
381 3427 : ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
382 :
383 3427 : err = ubifs_shash_init(c, c->log_hash);
384 3427 : if (err)
385 : goto out;
386 :
387 3427 : err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ);
388 3427 : if (err < 0)
389 : goto out;
390 :
391 : /*
392 : * Note, we do not lock 'c->log_mutex' because this is the commit start
393 : * phase and we are exclusively using the log. And we do not lock
394 : * write-buffer because nobody can write to the file-system at this
395 : * phase.
396 : */
397 :
398 : len = UBIFS_CS_NODE_SZ;
399 10281 : for (i = 0; i < c->jhead_cnt; i++) {
400 10281 : int lnum = c->jheads[i].wbuf.lnum;
401 10281 : int offs = c->jheads[i].wbuf.offs;
402 :
403 10281 : if (lnum == -1 || offs == c->leb_size)
404 6248 : continue;
405 :
406 4033 : dbg_log("add ref to LEB %d:%d for jhead %s",
407 : lnum, offs, dbg_jhead(i));
408 4033 : ref = buf + len;
409 4033 : ref->ch.node_type = UBIFS_REF_NODE;
410 4033 : ref->lnum = cpu_to_le32(lnum);
411 4033 : ref->offs = cpu_to_le32(offs);
412 4033 : ref->jhead = cpu_to_le32(i);
413 :
414 4033 : ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
415 4033 : len += UBIFS_REF_NODE_SZ;
416 :
417 4033 : err = ubifs_shash_update(c, c->log_hash, ref,
418 : UBIFS_REF_NODE_SZ);
419 4033 : if (err)
420 : goto out;
421 : ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash);
422 : }
423 :
424 3427 : ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
425 :
426 : /* Switch to the next log LEB */
427 3427 : if (c->lhead_offs) {
428 6854 : c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
429 3427 : ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
430 3427 : c->lhead_offs = 0;
431 : }
432 :
433 : /* Must ensure next LEB has been unmapped */
434 3427 : err = ubifs_leb_unmap(c, c->lhead_lnum);
435 3427 : if (err)
436 : goto out;
437 :
438 3427 : len = ALIGN(len, c->min_io_size);
439 3427 : dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
440 3427 : err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
441 3427 : if (err)
442 : goto out;
443 :
444 3427 : *ltail_lnum = c->lhead_lnum;
445 :
446 3427 : c->lhead_offs += len;
447 3427 : ubifs_assert(c, c->lhead_offs < c->leb_size);
448 :
449 3427 : remove_buds(c);
450 :
451 : /*
452 : * We have started the commit and now users may use the rest of the log
453 : * for new writes.
454 : */
455 3427 : c->min_log_bytes = 0;
456 :
457 3427 : out:
458 3427 : kfree(buf);
459 3427 : return err;
460 : }
461 :
462 : /**
463 : * ubifs_log_end_commit - end commit.
464 : * @c: UBIFS file-system description object
465 : * @ltail_lnum: new log tail LEB number
466 : *
467 : * This function is called on when the commit operation was finished. It
468 : * moves log tail to new position and updates the master node so that it stores
469 : * the new log tail LEB number. Returns zero in case of success and a negative
470 : * error code in case of failure.
471 : */
472 3398 : int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
473 : {
474 : int err;
475 :
476 : /*
477 : * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
478 : * writes during commit. Its only short "commit" start phase when
479 : * writers are blocked.
480 : */
481 3398 : mutex_lock(&c->log_mutex);
482 :
483 3398 : dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
484 : c->ltail_lnum, ltail_lnum);
485 :
486 3398 : c->ltail_lnum = ltail_lnum;
487 : /*
488 : * The commit is finished and from now on it must be guaranteed that
489 : * there is always enough space for the next commit.
490 : */
491 3398 : c->min_log_bytes = c->leb_size;
492 :
493 3398 : spin_lock(&c->buds_lock);
494 3398 : c->bud_bytes -= c->cmt_bud_bytes;
495 3398 : spin_unlock(&c->buds_lock);
496 :
497 3398 : err = dbg_check_bud_bytes(c);
498 : if (err)
499 : goto out;
500 :
501 3398 : err = ubifs_write_master(c);
502 :
503 3398 : out:
504 3398 : mutex_unlock(&c->log_mutex);
505 3398 : return err;
506 : }
507 :
508 : /**
509 : * ubifs_log_post_commit - things to do after commit is completed.
510 : * @c: UBIFS file-system description object
511 : * @old_ltail_lnum: old log tail LEB number
512 : *
513 : * Release buds only after commit is completed, because they must be unchanged
514 : * if recovery is needed.
515 : *
516 : * Unmap log LEBs only after commit is completed, because they may be needed for
517 : * recovery.
518 : *
519 : * This function returns %0 on success and a negative error code on failure.
520 : */
521 3398 : int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
522 : {
523 3398 : int lnum, err = 0;
524 :
525 13080 : while (!list_empty(&c->old_buds)) {
526 : struct ubifs_bud *bud;
527 :
528 1443 : bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
529 2886 : err = ubifs_return_leb(c, bud->lnum);
530 1443 : if (err)
531 : return err;
532 2886 : list_del(&bud->list);
533 2886 : kfree(bud->log_hash);
534 : kfree(bud);
535 : }
536 3398 : mutex_lock(&c->log_mutex);
537 10436 : for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
538 : lnum = ubifs_next_log_lnum(c, lnum)) {
539 3640 : dbg_log("unmap log LEB %d", lnum);
540 3640 : err = ubifs_leb_unmap(c, lnum);
541 3640 : if (err)
542 : goto out;
543 : }
544 3398 : out:
545 3398 : mutex_unlock(&c->log_mutex);
546 3398 : return err;
547 : }
548 :
549 : /**
550 : * struct done_ref - references that have been done.
551 : * @rb: rb-tree node
552 : * @lnum: LEB number
553 : */
554 : struct done_ref {
555 : struct rb_node rb;
556 : int lnum;
557 : };
558 :
559 : /**
560 : * done_already - determine if a reference has been done already.
561 : * @done_tree: rb-tree to store references that have been done
562 : * @lnum: LEB number of reference
563 : *
564 : * This function returns %1 if the reference has been done, %0 if not, otherwise
565 : * a negative error code is returned.
566 : */
567 58 : static int done_already(struct rb_root *done_tree, int lnum)
568 : {
569 58 : struct rb_node **p = &done_tree->rb_node, *parent = NULL;
570 : struct done_ref *dr;
571 :
572 145 : while (*p) {
573 53 : parent = *p;
574 53 : dr = rb_entry(parent, struct done_ref, rb);
575 53 : if (lnum < dr->lnum)
576 16 : p = &(*p)->rb_left;
577 37 : else if (lnum > dr->lnum)
578 13 : p = &(*p)->rb_right;
579 : else
580 : return 1;
581 : }
582 :
583 34 : dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
584 34 : if (!dr)
585 : return -ENOMEM;
586 :
587 34 : dr->lnum = lnum;
588 :
589 68 : rb_link_node(&dr->rb, parent, p);
590 34 : rb_insert_color(&dr->rb, done_tree);
591 :
592 34 : return 0;
593 : }
594 :
595 : /**
596 : * destroy_done_tree - destroy the done tree.
597 : * @done_tree: done tree to destroy
598 : */
599 17 : static void destroy_done_tree(struct rb_root *done_tree)
600 : {
601 : struct done_ref *dr, *n;
602 :
603 51 : rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
604 34 : kfree(dr);
605 17 : }
606 :
607 : /**
608 : * add_node - add a node to the consolidated log.
609 : * @c: UBIFS file-system description object
610 : * @buf: buffer to which to add
611 : * @lnum: LEB number to which to write is passed and returned here
612 : * @offs: offset to where to write is passed and returned here
613 : * @node: node to add
614 : *
615 : * This function returns %0 on success and a negative error code on failure.
616 : */
617 51 : static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
618 : void *node)
619 : {
620 51 : struct ubifs_ch *ch = node;
621 51 : int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
622 :
623 51 : if (len > remains) {
624 0 : int sz = ALIGN(*offs, c->min_io_size), err;
625 :
626 0 : ubifs_pad(c, buf + *offs, sz - *offs);
627 0 : err = ubifs_leb_change(c, *lnum, buf, sz);
628 0 : if (err)
629 : return err;
630 0 : *lnum = ubifs_next_log_lnum(c, *lnum);
631 0 : *offs = 0;
632 : }
633 51 : memcpy(buf + *offs, node, len);
634 51 : *offs += ALIGN(len, 8);
635 51 : return 0;
636 : }
637 :
638 : /**
639 : * ubifs_consolidate_log - consolidate the log.
640 : * @c: UBIFS file-system description object
641 : *
642 : * Repeated failed commits could cause the log to be full, but at least 1 LEB is
643 : * needed for commit. This function rewrites the reference nodes in the log
644 : * omitting duplicates, and failed CS nodes, and leaving no gaps.
645 : *
646 : * This function returns %0 on success and a negative error code on failure.
647 : */
648 17 : int ubifs_consolidate_log(struct ubifs_info *c)
649 : {
650 : struct ubifs_scan_leb *sleb;
651 : struct ubifs_scan_node *snod;
652 17 : struct rb_root done_tree = RB_ROOT;
653 17 : int lnum, err, first = 1, write_lnum, offs = 0;
654 : void *buf;
655 :
656 17 : dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
657 : c->lhead_lnum);
658 17 : buf = vmalloc(c->leb_size);
659 17 : if (!buf)
660 : return -ENOMEM;
661 17 : lnum = c->ltail_lnum;
662 17 : write_lnum = lnum;
663 : while (1) {
664 55 : sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
665 55 : if (IS_ERR(sleb)) {
666 0 : err = PTR_ERR(sleb);
667 0 : goto out_free;
668 : }
669 168 : list_for_each_entry(snod, &sleb->nodes, list) {
670 113 : switch (snod->type) {
671 58 : case UBIFS_REF_NODE: {
672 58 : struct ubifs_ref_node *ref = snod->node;
673 58 : int ref_lnum = le32_to_cpu(ref->lnum);
674 :
675 58 : err = done_already(&done_tree, ref_lnum);
676 58 : if (err < 0)
677 : goto out_scan;
678 58 : if (err != 1) {
679 34 : err = add_node(c, buf, &write_lnum,
680 : &offs, snod->node);
681 34 : if (err)
682 : goto out_scan;
683 : }
684 : break;
685 : }
686 55 : case UBIFS_CS_NODE:
687 55 : if (!first)
688 : break;
689 17 : err = add_node(c, buf, &write_lnum, &offs,
690 : snod->node);
691 17 : if (err)
692 : goto out_scan;
693 : first = 0;
694 : break;
695 : }
696 : }
697 55 : ubifs_scan_destroy(sleb);
698 55 : if (lnum == c->lhead_lnum)
699 : break;
700 : lnum = ubifs_next_log_lnum(c, lnum);
701 : }
702 17 : if (offs) {
703 17 : int sz = ALIGN(offs, c->min_io_size);
704 :
705 17 : ubifs_pad(c, buf + offs, sz - offs);
706 17 : err = ubifs_leb_change(c, write_lnum, buf, sz);
707 17 : if (err)
708 : goto out_free;
709 17 : offs = ALIGN(offs, c->min_io_size);
710 : }
711 17 : destroy_done_tree(&done_tree);
712 17 : vfree(buf);
713 17 : if (write_lnum == c->lhead_lnum) {
714 0 : set_failure_reason_callback(c, FR_DATA_CORRUPTED);
715 0 : ubifs_err(c, "log is too full");
716 : return -EINVAL;
717 : }
718 : /* Unmap remaining LEBs */
719 : lnum = write_lnum;
720 : do {
721 38 : lnum = ubifs_next_log_lnum(c, lnum);
722 38 : err = ubifs_leb_unmap(c, lnum);
723 38 : if (err)
724 : return err;
725 38 : } while (lnum != c->lhead_lnum);
726 17 : c->lhead_lnum = write_lnum;
727 17 : c->lhead_offs = offs;
728 17 : dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
729 : return 0;
730 :
731 0 : out_scan:
732 0 : ubifs_scan_destroy(sleb);
733 0 : out_free:
734 0 : destroy_done_tree(&done_tree);
735 0 : vfree(buf);
736 0 : return err;
737 : }
738 :
739 : /**
740 : * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
741 : * @c: UBIFS file-system description object
742 : *
743 : * This function makes sure the amount of flash space used by closed buds
744 : * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
745 : * case of failure.
746 : */
747 : static int dbg_check_bud_bytes(__unused struct ubifs_info *c)
748 : {
749 : return 0;
750 : }
|