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