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