xref: /openbmc/u-boot/fs/ubifs/lpt_commit.c (revision afee3fb8)
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: Adrian Hunter
9  *          Artem Bityutskiy (Битюцкий Артём)
10  */
11 
12 /*
13  * This file implements commit-related functionality of the LEB properties
14  * subsystem.
15  */
16 
17 #ifndef __UBOOT__
18 #include <linux/crc16.h>
19 #include <linux/slab.h>
20 #include <linux/random.h>
21 #else
22 #include <linux/compat.h>
23 #include <linux/err.h>
24 #include "crc16.h"
25 #endif
26 #include "ubifs.h"
27 
28 #ifndef __UBOOT__
29 static int dbg_populate_lsave(struct ubifs_info *c);
30 #endif
31 
32 /**
33  * first_dirty_cnode - find first dirty cnode.
34  * @c: UBIFS file-system description object
35  * @nnode: nnode at which to start
36  *
37  * This function returns the first dirty cnode or %NULL if there is not one.
38  */
39 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
40 {
41 	ubifs_assert(nnode);
42 	while (1) {
43 		int i, cont = 0;
44 
45 		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
46 			struct ubifs_cnode *cnode;
47 
48 			cnode = nnode->nbranch[i].cnode;
49 			if (cnode &&
50 			    test_bit(DIRTY_CNODE, &cnode->flags)) {
51 				if (cnode->level == 0)
52 					return cnode;
53 				nnode = (struct ubifs_nnode *)cnode;
54 				cont = 1;
55 				break;
56 			}
57 		}
58 		if (!cont)
59 			return (struct ubifs_cnode *)nnode;
60 	}
61 }
62 
63 /**
64  * next_dirty_cnode - find next dirty cnode.
65  * @cnode: cnode from which to begin searching
66  *
67  * This function returns the next dirty cnode or %NULL if there is not one.
68  */
69 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
70 {
71 	struct ubifs_nnode *nnode;
72 	int i;
73 
74 	ubifs_assert(cnode);
75 	nnode = cnode->parent;
76 	if (!nnode)
77 		return NULL;
78 	for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
79 		cnode = nnode->nbranch[i].cnode;
80 		if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
81 			if (cnode->level == 0)
82 				return cnode; /* cnode is a pnode */
83 			/* cnode is a nnode */
84 			return first_dirty_cnode((struct ubifs_nnode *)cnode);
85 		}
86 	}
87 	return (struct ubifs_cnode *)nnode;
88 }
89 
90 /**
91  * get_cnodes_to_commit - create list of dirty cnodes to commit.
92  * @c: UBIFS file-system description object
93  *
94  * This function returns the number of cnodes to commit.
95  */
96 static int get_cnodes_to_commit(struct ubifs_info *c)
97 {
98 	struct ubifs_cnode *cnode, *cnext;
99 	int cnt = 0;
100 
101 	if (!c->nroot)
102 		return 0;
103 
104 	if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
105 		return 0;
106 
107 	c->lpt_cnext = first_dirty_cnode(c->nroot);
108 	cnode = c->lpt_cnext;
109 	if (!cnode)
110 		return 0;
111 	cnt += 1;
112 	while (1) {
113 		ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
114 		__set_bit(COW_CNODE, &cnode->flags);
115 		cnext = next_dirty_cnode(cnode);
116 		if (!cnext) {
117 			cnode->cnext = c->lpt_cnext;
118 			break;
119 		}
120 		cnode->cnext = cnext;
121 		cnode = cnext;
122 		cnt += 1;
123 	}
124 	dbg_cmt("committing %d cnodes", cnt);
125 	dbg_lp("committing %d cnodes", cnt);
126 	ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
127 	return cnt;
128 }
129 
130 /**
131  * upd_ltab - update LPT LEB properties.
132  * @c: UBIFS file-system description object
133  * @lnum: LEB number
134  * @free: amount of free space
135  * @dirty: amount of dirty space to add
136  */
137 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
138 {
139 	dbg_lp("LEB %d free %d dirty %d to %d +%d",
140 	       lnum, c->ltab[lnum - c->lpt_first].free,
141 	       c->ltab[lnum - c->lpt_first].dirty, free, dirty);
142 	ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
143 	c->ltab[lnum - c->lpt_first].free = free;
144 	c->ltab[lnum - c->lpt_first].dirty += dirty;
145 }
146 
147 /**
148  * alloc_lpt_leb - allocate an LPT LEB that is empty.
149  * @c: UBIFS file-system description object
150  * @lnum: LEB number is passed and returned here
151  *
152  * This function finds the next empty LEB in the ltab starting from @lnum. If a
153  * an empty LEB is found it is returned in @lnum and the function returns %0.
154  * Otherwise the function returns -ENOSPC.  Note however, that LPT is designed
155  * never to run out of space.
156  */
157 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
158 {
159 	int i, n;
160 
161 	n = *lnum - c->lpt_first + 1;
162 	for (i = n; i < c->lpt_lebs; i++) {
163 		if (c->ltab[i].tgc || c->ltab[i].cmt)
164 			continue;
165 		if (c->ltab[i].free == c->leb_size) {
166 			c->ltab[i].cmt = 1;
167 			*lnum = i + c->lpt_first;
168 			return 0;
169 		}
170 	}
171 
172 	for (i = 0; i < n; i++) {
173 		if (c->ltab[i].tgc || c->ltab[i].cmt)
174 			continue;
175 		if (c->ltab[i].free == c->leb_size) {
176 			c->ltab[i].cmt = 1;
177 			*lnum = i + c->lpt_first;
178 			return 0;
179 		}
180 	}
181 	return -ENOSPC;
182 }
183 
184 /**
185  * layout_cnodes - layout cnodes for commit.
186  * @c: UBIFS file-system description object
187  *
188  * This function returns %0 on success and a negative error code on failure.
189  */
190 static int layout_cnodes(struct ubifs_info *c)
191 {
192 	int lnum, offs, len, alen, done_lsave, done_ltab, err;
193 	struct ubifs_cnode *cnode;
194 
195 	err = dbg_chk_lpt_sz(c, 0, 0);
196 	if (err)
197 		return err;
198 	cnode = c->lpt_cnext;
199 	if (!cnode)
200 		return 0;
201 	lnum = c->nhead_lnum;
202 	offs = c->nhead_offs;
203 	/* Try to place lsave and ltab nicely */
204 	done_lsave = !c->big_lpt;
205 	done_ltab = 0;
206 	if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
207 		done_lsave = 1;
208 		c->lsave_lnum = lnum;
209 		c->lsave_offs = offs;
210 		offs += c->lsave_sz;
211 		dbg_chk_lpt_sz(c, 1, c->lsave_sz);
212 	}
213 
214 	if (offs + c->ltab_sz <= c->leb_size) {
215 		done_ltab = 1;
216 		c->ltab_lnum = lnum;
217 		c->ltab_offs = offs;
218 		offs += c->ltab_sz;
219 		dbg_chk_lpt_sz(c, 1, c->ltab_sz);
220 	}
221 
222 	do {
223 		if (cnode->level) {
224 			len = c->nnode_sz;
225 			c->dirty_nn_cnt -= 1;
226 		} else {
227 			len = c->pnode_sz;
228 			c->dirty_pn_cnt -= 1;
229 		}
230 		while (offs + len > c->leb_size) {
231 			alen = ALIGN(offs, c->min_io_size);
232 			upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
233 			dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
234 			err = alloc_lpt_leb(c, &lnum);
235 			if (err)
236 				goto no_space;
237 			offs = 0;
238 			ubifs_assert(lnum >= c->lpt_first &&
239 				     lnum <= c->lpt_last);
240 			/* Try to place lsave and ltab nicely */
241 			if (!done_lsave) {
242 				done_lsave = 1;
243 				c->lsave_lnum = lnum;
244 				c->lsave_offs = offs;
245 				offs += c->lsave_sz;
246 				dbg_chk_lpt_sz(c, 1, c->lsave_sz);
247 				continue;
248 			}
249 			if (!done_ltab) {
250 				done_ltab = 1;
251 				c->ltab_lnum = lnum;
252 				c->ltab_offs = offs;
253 				offs += c->ltab_sz;
254 				dbg_chk_lpt_sz(c, 1, c->ltab_sz);
255 				continue;
256 			}
257 			break;
258 		}
259 		if (cnode->parent) {
260 			cnode->parent->nbranch[cnode->iip].lnum = lnum;
261 			cnode->parent->nbranch[cnode->iip].offs = offs;
262 		} else {
263 			c->lpt_lnum = lnum;
264 			c->lpt_offs = offs;
265 		}
266 		offs += len;
267 		dbg_chk_lpt_sz(c, 1, len);
268 		cnode = cnode->cnext;
269 	} while (cnode && cnode != c->lpt_cnext);
270 
271 	/* Make sure to place LPT's save table */
272 	if (!done_lsave) {
273 		if (offs + c->lsave_sz > c->leb_size) {
274 			alen = ALIGN(offs, c->min_io_size);
275 			upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
276 			dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
277 			err = alloc_lpt_leb(c, &lnum);
278 			if (err)
279 				goto no_space;
280 			offs = 0;
281 			ubifs_assert(lnum >= c->lpt_first &&
282 				     lnum <= c->lpt_last);
283 		}
284 		done_lsave = 1;
285 		c->lsave_lnum = lnum;
286 		c->lsave_offs = offs;
287 		offs += c->lsave_sz;
288 		dbg_chk_lpt_sz(c, 1, c->lsave_sz);
289 	}
290 
291 	/* Make sure to place LPT's own lprops table */
292 	if (!done_ltab) {
293 		if (offs + c->ltab_sz > c->leb_size) {
294 			alen = ALIGN(offs, c->min_io_size);
295 			upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
296 			dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
297 			err = alloc_lpt_leb(c, &lnum);
298 			if (err)
299 				goto no_space;
300 			offs = 0;
301 			ubifs_assert(lnum >= c->lpt_first &&
302 				     lnum <= c->lpt_last);
303 		}
304 		done_ltab = 1;
305 		c->ltab_lnum = lnum;
306 		c->ltab_offs = offs;
307 		offs += c->ltab_sz;
308 		dbg_chk_lpt_sz(c, 1, c->ltab_sz);
309 	}
310 
311 	alen = ALIGN(offs, c->min_io_size);
312 	upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
313 	dbg_chk_lpt_sz(c, 4, alen - offs);
314 	err = dbg_chk_lpt_sz(c, 3, alen);
315 	if (err)
316 		return err;
317 	return 0;
318 
319 no_space:
320 	ubifs_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
321 		  lnum, offs, len, done_ltab, done_lsave);
322 	ubifs_dump_lpt_info(c);
323 	ubifs_dump_lpt_lebs(c);
324 	dump_stack();
325 	return err;
326 }
327 
328 #ifndef __UBOOT__
329 /**
330  * realloc_lpt_leb - allocate an LPT LEB that is empty.
331  * @c: UBIFS file-system description object
332  * @lnum: LEB number is passed and returned here
333  *
334  * This function duplicates exactly the results of the function alloc_lpt_leb.
335  * It is used during end commit to reallocate the same LEB numbers that were
336  * allocated by alloc_lpt_leb during start commit.
337  *
338  * This function finds the next LEB that was allocated by the alloc_lpt_leb
339  * function starting from @lnum. If a LEB is found it is returned in @lnum and
340  * the function returns %0. Otherwise the function returns -ENOSPC.
341  * Note however, that LPT is designed never to run out of space.
342  */
343 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
344 {
345 	int i, n;
346 
347 	n = *lnum - c->lpt_first + 1;
348 	for (i = n; i < c->lpt_lebs; i++)
349 		if (c->ltab[i].cmt) {
350 			c->ltab[i].cmt = 0;
351 			*lnum = i + c->lpt_first;
352 			return 0;
353 		}
354 
355 	for (i = 0; i < n; i++)
356 		if (c->ltab[i].cmt) {
357 			c->ltab[i].cmt = 0;
358 			*lnum = i + c->lpt_first;
359 			return 0;
360 		}
361 	return -ENOSPC;
362 }
363 
364 /**
365  * write_cnodes - write cnodes for commit.
366  * @c: UBIFS file-system description object
367  *
368  * This function returns %0 on success and a negative error code on failure.
369  */
370 static int write_cnodes(struct ubifs_info *c)
371 {
372 	int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
373 	struct ubifs_cnode *cnode;
374 	void *buf = c->lpt_buf;
375 
376 	cnode = c->lpt_cnext;
377 	if (!cnode)
378 		return 0;
379 	lnum = c->nhead_lnum;
380 	offs = c->nhead_offs;
381 	from = offs;
382 	/* Ensure empty LEB is unmapped */
383 	if (offs == 0) {
384 		err = ubifs_leb_unmap(c, lnum);
385 		if (err)
386 			return err;
387 	}
388 	/* Try to place lsave and ltab nicely */
389 	done_lsave = !c->big_lpt;
390 	done_ltab = 0;
391 	if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
392 		done_lsave = 1;
393 		ubifs_pack_lsave(c, buf + offs, c->lsave);
394 		offs += c->lsave_sz;
395 		dbg_chk_lpt_sz(c, 1, c->lsave_sz);
396 	}
397 
398 	if (offs + c->ltab_sz <= c->leb_size) {
399 		done_ltab = 1;
400 		ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
401 		offs += c->ltab_sz;
402 		dbg_chk_lpt_sz(c, 1, c->ltab_sz);
403 	}
404 
405 	/* Loop for each cnode */
406 	do {
407 		if (cnode->level)
408 			len = c->nnode_sz;
409 		else
410 			len = c->pnode_sz;
411 		while (offs + len > c->leb_size) {
412 			wlen = offs - from;
413 			if (wlen) {
414 				alen = ALIGN(wlen, c->min_io_size);
415 				memset(buf + offs, 0xff, alen - wlen);
416 				err = ubifs_leb_write(c, lnum, buf + from, from,
417 						       alen);
418 				if (err)
419 					return err;
420 			}
421 			dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
422 			err = realloc_lpt_leb(c, &lnum);
423 			if (err)
424 				goto no_space;
425 			offs = from = 0;
426 			ubifs_assert(lnum >= c->lpt_first &&
427 				     lnum <= c->lpt_last);
428 			err = ubifs_leb_unmap(c, lnum);
429 			if (err)
430 				return err;
431 			/* Try to place lsave and ltab nicely */
432 			if (!done_lsave) {
433 				done_lsave = 1;
434 				ubifs_pack_lsave(c, buf + offs, c->lsave);
435 				offs += c->lsave_sz;
436 				dbg_chk_lpt_sz(c, 1, c->lsave_sz);
437 				continue;
438 			}
439 			if (!done_ltab) {
440 				done_ltab = 1;
441 				ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
442 				offs += c->ltab_sz;
443 				dbg_chk_lpt_sz(c, 1, c->ltab_sz);
444 				continue;
445 			}
446 			break;
447 		}
448 		if (cnode->level)
449 			ubifs_pack_nnode(c, buf + offs,
450 					 (struct ubifs_nnode *)cnode);
451 		else
452 			ubifs_pack_pnode(c, buf + offs,
453 					 (struct ubifs_pnode *)cnode);
454 		/*
455 		 * The reason for the barriers is the same as in case of TNC.
456 		 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
457 		 * 'dirty_cow_pnode()' are the functions for which this is
458 		 * important.
459 		 */
460 		clear_bit(DIRTY_CNODE, &cnode->flags);
461 		smp_mb__before_clear_bit();
462 		clear_bit(COW_CNODE, &cnode->flags);
463 		smp_mb__after_clear_bit();
464 		offs += len;
465 		dbg_chk_lpt_sz(c, 1, len);
466 		cnode = cnode->cnext;
467 	} while (cnode && cnode != c->lpt_cnext);
468 
469 	/* Make sure to place LPT's save table */
470 	if (!done_lsave) {
471 		if (offs + c->lsave_sz > c->leb_size) {
472 			wlen = offs - from;
473 			alen = ALIGN(wlen, c->min_io_size);
474 			memset(buf + offs, 0xff, alen - wlen);
475 			err = ubifs_leb_write(c, lnum, buf + from, from, alen);
476 			if (err)
477 				return err;
478 			dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
479 			err = realloc_lpt_leb(c, &lnum);
480 			if (err)
481 				goto no_space;
482 			offs = from = 0;
483 			ubifs_assert(lnum >= c->lpt_first &&
484 				     lnum <= c->lpt_last);
485 			err = ubifs_leb_unmap(c, lnum);
486 			if (err)
487 				return err;
488 		}
489 		done_lsave = 1;
490 		ubifs_pack_lsave(c, buf + offs, c->lsave);
491 		offs += c->lsave_sz;
492 		dbg_chk_lpt_sz(c, 1, c->lsave_sz);
493 	}
494 
495 	/* Make sure to place LPT's own lprops table */
496 	if (!done_ltab) {
497 		if (offs + c->ltab_sz > c->leb_size) {
498 			wlen = offs - from;
499 			alen = ALIGN(wlen, c->min_io_size);
500 			memset(buf + offs, 0xff, alen - wlen);
501 			err = ubifs_leb_write(c, lnum, buf + from, from, alen);
502 			if (err)
503 				return err;
504 			dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
505 			err = realloc_lpt_leb(c, &lnum);
506 			if (err)
507 				goto no_space;
508 			offs = from = 0;
509 			ubifs_assert(lnum >= c->lpt_first &&
510 				     lnum <= c->lpt_last);
511 			err = ubifs_leb_unmap(c, lnum);
512 			if (err)
513 				return err;
514 		}
515 		done_ltab = 1;
516 		ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
517 		offs += c->ltab_sz;
518 		dbg_chk_lpt_sz(c, 1, c->ltab_sz);
519 	}
520 
521 	/* Write remaining data in buffer */
522 	wlen = offs - from;
523 	alen = ALIGN(wlen, c->min_io_size);
524 	memset(buf + offs, 0xff, alen - wlen);
525 	err = ubifs_leb_write(c, lnum, buf + from, from, alen);
526 	if (err)
527 		return err;
528 
529 	dbg_chk_lpt_sz(c, 4, alen - wlen);
530 	err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
531 	if (err)
532 		return err;
533 
534 	c->nhead_lnum = lnum;
535 	c->nhead_offs = ALIGN(offs, c->min_io_size);
536 
537 	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
538 	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
539 	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
540 	if (c->big_lpt)
541 		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
542 
543 	return 0;
544 
545 no_space:
546 	ubifs_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
547 		  lnum, offs, len, done_ltab, done_lsave);
548 	ubifs_dump_lpt_info(c);
549 	ubifs_dump_lpt_lebs(c);
550 	dump_stack();
551 	return err;
552 }
553 #endif
554 
555 /**
556  * next_pnode_to_dirty - find next pnode to dirty.
557  * @c: UBIFS file-system description object
558  * @pnode: pnode
559  *
560  * This function returns the next pnode to dirty or %NULL if there are no more
561  * pnodes.  Note that pnodes that have never been written (lnum == 0) are
562  * skipped.
563  */
564 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
565 					       struct ubifs_pnode *pnode)
566 {
567 	struct ubifs_nnode *nnode;
568 	int iip;
569 
570 	/* Try to go right */
571 	nnode = pnode->parent;
572 	for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
573 		if (nnode->nbranch[iip].lnum)
574 			return ubifs_get_pnode(c, nnode, iip);
575 	}
576 
577 	/* Go up while can't go right */
578 	do {
579 		iip = nnode->iip + 1;
580 		nnode = nnode->parent;
581 		if (!nnode)
582 			return NULL;
583 		for (; iip < UBIFS_LPT_FANOUT; iip++) {
584 			if (nnode->nbranch[iip].lnum)
585 				break;
586 		}
587 	} while (iip >= UBIFS_LPT_FANOUT);
588 
589 	/* Go right */
590 	nnode = ubifs_get_nnode(c, nnode, iip);
591 	if (IS_ERR(nnode))
592 		return (void *)nnode;
593 
594 	/* Go down to level 1 */
595 	while (nnode->level > 1) {
596 		for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
597 			if (nnode->nbranch[iip].lnum)
598 				break;
599 		}
600 		if (iip >= UBIFS_LPT_FANOUT) {
601 			/*
602 			 * Should not happen, but we need to keep going
603 			 * if it does.
604 			 */
605 			iip = 0;
606 		}
607 		nnode = ubifs_get_nnode(c, nnode, iip);
608 		if (IS_ERR(nnode))
609 			return (void *)nnode;
610 	}
611 
612 	for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
613 		if (nnode->nbranch[iip].lnum)
614 			break;
615 	if (iip >= UBIFS_LPT_FANOUT)
616 		/* Should not happen, but we need to keep going if it does */
617 		iip = 0;
618 	return ubifs_get_pnode(c, nnode, iip);
619 }
620 
621 /**
622  * pnode_lookup - lookup a pnode in the LPT.
623  * @c: UBIFS file-system description object
624  * @i: pnode number (0 to main_lebs - 1)
625  *
626  * This function returns a pointer to the pnode on success or a negative
627  * error code on failure.
628  */
629 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
630 {
631 	int err, h, iip, shft;
632 	struct ubifs_nnode *nnode;
633 
634 	if (!c->nroot) {
635 		err = ubifs_read_nnode(c, NULL, 0);
636 		if (err)
637 			return ERR_PTR(err);
638 	}
639 	i <<= UBIFS_LPT_FANOUT_SHIFT;
640 	nnode = c->nroot;
641 	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
642 	for (h = 1; h < c->lpt_hght; h++) {
643 		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
644 		shft -= UBIFS_LPT_FANOUT_SHIFT;
645 		nnode = ubifs_get_nnode(c, nnode, iip);
646 		if (IS_ERR(nnode))
647 			return ERR_CAST(nnode);
648 	}
649 	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
650 	return ubifs_get_pnode(c, nnode, iip);
651 }
652 
653 /**
654  * add_pnode_dirt - add dirty space to LPT LEB properties.
655  * @c: UBIFS file-system description object
656  * @pnode: pnode for which to add dirt
657  */
658 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
659 {
660 	ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
661 			   c->pnode_sz);
662 }
663 
664 /**
665  * do_make_pnode_dirty - mark a pnode dirty.
666  * @c: UBIFS file-system description object
667  * @pnode: pnode to mark dirty
668  */
669 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
670 {
671 	/* Assumes cnext list is empty i.e. not called during commit */
672 	if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
673 		struct ubifs_nnode *nnode;
674 
675 		c->dirty_pn_cnt += 1;
676 		add_pnode_dirt(c, pnode);
677 		/* Mark parent and ancestors dirty too */
678 		nnode = pnode->parent;
679 		while (nnode) {
680 			if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
681 				c->dirty_nn_cnt += 1;
682 				ubifs_add_nnode_dirt(c, nnode);
683 				nnode = nnode->parent;
684 			} else
685 				break;
686 		}
687 	}
688 }
689 
690 /**
691  * make_tree_dirty - mark the entire LEB properties tree dirty.
692  * @c: UBIFS file-system description object
693  *
694  * This function is used by the "small" LPT model to cause the entire LEB
695  * properties tree to be written.  The "small" LPT model does not use LPT
696  * garbage collection because it is more efficient to write the entire tree
697  * (because it is small).
698  *
699  * This function returns %0 on success and a negative error code on failure.
700  */
701 static int make_tree_dirty(struct ubifs_info *c)
702 {
703 	struct ubifs_pnode *pnode;
704 
705 	pnode = pnode_lookup(c, 0);
706 	if (IS_ERR(pnode))
707 		return PTR_ERR(pnode);
708 
709 	while (pnode) {
710 		do_make_pnode_dirty(c, pnode);
711 		pnode = next_pnode_to_dirty(c, pnode);
712 		if (IS_ERR(pnode))
713 			return PTR_ERR(pnode);
714 	}
715 	return 0;
716 }
717 
718 /**
719  * need_write_all - determine if the LPT area is running out of free space.
720  * @c: UBIFS file-system description object
721  *
722  * This function returns %1 if the LPT area is running out of free space and %0
723  * if it is not.
724  */
725 static int need_write_all(struct ubifs_info *c)
726 {
727 	long long free = 0;
728 	int i;
729 
730 	for (i = 0; i < c->lpt_lebs; i++) {
731 		if (i + c->lpt_first == c->nhead_lnum)
732 			free += c->leb_size - c->nhead_offs;
733 		else if (c->ltab[i].free == c->leb_size)
734 			free += c->leb_size;
735 		else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
736 			free += c->leb_size;
737 	}
738 	/* Less than twice the size left */
739 	if (free <= c->lpt_sz * 2)
740 		return 1;
741 	return 0;
742 }
743 
744 /**
745  * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
746  * @c: UBIFS file-system description object
747  *
748  * LPT trivial garbage collection is where a LPT LEB contains only dirty and
749  * free space and so may be reused as soon as the next commit is completed.
750  * This function is called during start commit to mark LPT LEBs for trivial GC.
751  */
752 static void lpt_tgc_start(struct ubifs_info *c)
753 {
754 	int i;
755 
756 	for (i = 0; i < c->lpt_lebs; i++) {
757 		if (i + c->lpt_first == c->nhead_lnum)
758 			continue;
759 		if (c->ltab[i].dirty > 0 &&
760 		    c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
761 			c->ltab[i].tgc = 1;
762 			c->ltab[i].free = c->leb_size;
763 			c->ltab[i].dirty = 0;
764 			dbg_lp("LEB %d", i + c->lpt_first);
765 		}
766 	}
767 }
768 
769 /**
770  * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
771  * @c: UBIFS file-system description object
772  *
773  * LPT trivial garbage collection is where a LPT LEB contains only dirty and
774  * free space and so may be reused as soon as the next commit is completed.
775  * This function is called after the commit is completed (master node has been
776  * written) and un-maps LPT LEBs that were marked for trivial GC.
777  */
778 static int lpt_tgc_end(struct ubifs_info *c)
779 {
780 	int i, err;
781 
782 	for (i = 0; i < c->lpt_lebs; i++)
783 		if (c->ltab[i].tgc) {
784 			err = ubifs_leb_unmap(c, i + c->lpt_first);
785 			if (err)
786 				return err;
787 			c->ltab[i].tgc = 0;
788 			dbg_lp("LEB %d", i + c->lpt_first);
789 		}
790 	return 0;
791 }
792 
793 /**
794  * populate_lsave - fill the lsave array with important LEB numbers.
795  * @c: the UBIFS file-system description object
796  *
797  * This function is only called for the "big" model. It records a small number
798  * of LEB numbers of important LEBs.  Important LEBs are ones that are (from
799  * most important to least important): empty, freeable, freeable index, dirty
800  * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
801  * their pnodes into memory.  That will stop us from having to scan the LPT
802  * straight away. For the "small" model we assume that scanning the LPT is no
803  * big deal.
804  */
805 static void populate_lsave(struct ubifs_info *c)
806 {
807 	struct ubifs_lprops *lprops;
808 	struct ubifs_lpt_heap *heap;
809 	int i, cnt = 0;
810 
811 	ubifs_assert(c->big_lpt);
812 	if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
813 		c->lpt_drty_flgs |= LSAVE_DIRTY;
814 		ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
815 	}
816 
817 #ifndef __UBOOT__
818 	if (dbg_populate_lsave(c))
819 		return;
820 #endif
821 
822 	list_for_each_entry(lprops, &c->empty_list, list) {
823 		c->lsave[cnt++] = lprops->lnum;
824 		if (cnt >= c->lsave_cnt)
825 			return;
826 	}
827 	list_for_each_entry(lprops, &c->freeable_list, list) {
828 		c->lsave[cnt++] = lprops->lnum;
829 		if (cnt >= c->lsave_cnt)
830 			return;
831 	}
832 	list_for_each_entry(lprops, &c->frdi_idx_list, list) {
833 		c->lsave[cnt++] = lprops->lnum;
834 		if (cnt >= c->lsave_cnt)
835 			return;
836 	}
837 	heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
838 	for (i = 0; i < heap->cnt; i++) {
839 		c->lsave[cnt++] = heap->arr[i]->lnum;
840 		if (cnt >= c->lsave_cnt)
841 			return;
842 	}
843 	heap = &c->lpt_heap[LPROPS_DIRTY - 1];
844 	for (i = 0; i < heap->cnt; i++) {
845 		c->lsave[cnt++] = heap->arr[i]->lnum;
846 		if (cnt >= c->lsave_cnt)
847 			return;
848 	}
849 	heap = &c->lpt_heap[LPROPS_FREE - 1];
850 	for (i = 0; i < heap->cnt; i++) {
851 		c->lsave[cnt++] = heap->arr[i]->lnum;
852 		if (cnt >= c->lsave_cnt)
853 			return;
854 	}
855 	/* Fill it up completely */
856 	while (cnt < c->lsave_cnt)
857 		c->lsave[cnt++] = c->main_first;
858 }
859 
860 /**
861  * nnode_lookup - lookup a nnode in the LPT.
862  * @c: UBIFS file-system description object
863  * @i: nnode number
864  *
865  * This function returns a pointer to the nnode on success or a negative
866  * error code on failure.
867  */
868 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
869 {
870 	int err, iip;
871 	struct ubifs_nnode *nnode;
872 
873 	if (!c->nroot) {
874 		err = ubifs_read_nnode(c, NULL, 0);
875 		if (err)
876 			return ERR_PTR(err);
877 	}
878 	nnode = c->nroot;
879 	while (1) {
880 		iip = i & (UBIFS_LPT_FANOUT - 1);
881 		i >>= UBIFS_LPT_FANOUT_SHIFT;
882 		if (!i)
883 			break;
884 		nnode = ubifs_get_nnode(c, nnode, iip);
885 		if (IS_ERR(nnode))
886 			return nnode;
887 	}
888 	return nnode;
889 }
890 
891 /**
892  * make_nnode_dirty - find a nnode and, if found, make it dirty.
893  * @c: UBIFS file-system description object
894  * @node_num: nnode number of nnode to make dirty
895  * @lnum: LEB number where nnode was written
896  * @offs: offset where nnode was written
897  *
898  * This function is used by LPT garbage collection.  LPT garbage collection is
899  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
900  * simply involves marking all the nodes in the LEB being garbage-collected as
901  * dirty.  The dirty nodes are written next commit, after which the LEB is free
902  * to be reused.
903  *
904  * This function returns %0 on success and a negative error code on failure.
905  */
906 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
907 			    int offs)
908 {
909 	struct ubifs_nnode *nnode;
910 
911 	nnode = nnode_lookup(c, node_num);
912 	if (IS_ERR(nnode))
913 		return PTR_ERR(nnode);
914 	if (nnode->parent) {
915 		struct ubifs_nbranch *branch;
916 
917 		branch = &nnode->parent->nbranch[nnode->iip];
918 		if (branch->lnum != lnum || branch->offs != offs)
919 			return 0; /* nnode is obsolete */
920 	} else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
921 			return 0; /* nnode is obsolete */
922 	/* Assumes cnext list is empty i.e. not called during commit */
923 	if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
924 		c->dirty_nn_cnt += 1;
925 		ubifs_add_nnode_dirt(c, nnode);
926 		/* Mark parent and ancestors dirty too */
927 		nnode = nnode->parent;
928 		while (nnode) {
929 			if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
930 				c->dirty_nn_cnt += 1;
931 				ubifs_add_nnode_dirt(c, nnode);
932 				nnode = nnode->parent;
933 			} else
934 				break;
935 		}
936 	}
937 	return 0;
938 }
939 
940 /**
941  * make_pnode_dirty - find a pnode and, if found, make it dirty.
942  * @c: UBIFS file-system description object
943  * @node_num: pnode number of pnode to make dirty
944  * @lnum: LEB number where pnode was written
945  * @offs: offset where pnode was written
946  *
947  * This function is used by LPT garbage collection.  LPT garbage collection is
948  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
949  * simply involves marking all the nodes in the LEB being garbage-collected as
950  * dirty.  The dirty nodes are written next commit, after which the LEB is free
951  * to be reused.
952  *
953  * This function returns %0 on success and a negative error code on failure.
954  */
955 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
956 			    int offs)
957 {
958 	struct ubifs_pnode *pnode;
959 	struct ubifs_nbranch *branch;
960 
961 	pnode = pnode_lookup(c, node_num);
962 	if (IS_ERR(pnode))
963 		return PTR_ERR(pnode);
964 	branch = &pnode->parent->nbranch[pnode->iip];
965 	if (branch->lnum != lnum || branch->offs != offs)
966 		return 0;
967 	do_make_pnode_dirty(c, pnode);
968 	return 0;
969 }
970 
971 /**
972  * make_ltab_dirty - make ltab node dirty.
973  * @c: UBIFS file-system description object
974  * @lnum: LEB number where ltab was written
975  * @offs: offset where ltab was written
976  *
977  * This function is used by LPT garbage collection.  LPT garbage collection is
978  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
979  * simply involves marking all the nodes in the LEB being garbage-collected as
980  * dirty.  The dirty nodes are written next commit, after which the LEB is free
981  * to be reused.
982  *
983  * This function returns %0 on success and a negative error code on failure.
984  */
985 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
986 {
987 	if (lnum != c->ltab_lnum || offs != c->ltab_offs)
988 		return 0; /* This ltab node is obsolete */
989 	if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
990 		c->lpt_drty_flgs |= LTAB_DIRTY;
991 		ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
992 	}
993 	return 0;
994 }
995 
996 /**
997  * make_lsave_dirty - make lsave node dirty.
998  * @c: UBIFS file-system description object
999  * @lnum: LEB number where lsave was written
1000  * @offs: offset where lsave was written
1001  *
1002  * This function is used by LPT garbage collection.  LPT garbage collection is
1003  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
1004  * simply involves marking all the nodes in the LEB being garbage-collected as
1005  * dirty.  The dirty nodes are written next commit, after which the LEB is free
1006  * to be reused.
1007  *
1008  * This function returns %0 on success and a negative error code on failure.
1009  */
1010 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1011 {
1012 	if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1013 		return 0; /* This lsave node is obsolete */
1014 	if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1015 		c->lpt_drty_flgs |= LSAVE_DIRTY;
1016 		ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1017 	}
1018 	return 0;
1019 }
1020 
1021 /**
1022  * make_node_dirty - make node dirty.
1023  * @c: UBIFS file-system description object
1024  * @node_type: LPT node type
1025  * @node_num: node number
1026  * @lnum: LEB number where node was written
1027  * @offs: offset where node was written
1028  *
1029  * This function is used by LPT garbage collection.  LPT garbage collection is
1030  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
1031  * simply involves marking all the nodes in the LEB being garbage-collected as
1032  * dirty.  The dirty nodes are written next commit, after which the LEB is free
1033  * to be reused.
1034  *
1035  * This function returns %0 on success and a negative error code on failure.
1036  */
1037 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1038 			   int lnum, int offs)
1039 {
1040 	switch (node_type) {
1041 	case UBIFS_LPT_NNODE:
1042 		return make_nnode_dirty(c, node_num, lnum, offs);
1043 	case UBIFS_LPT_PNODE:
1044 		return make_pnode_dirty(c, node_num, lnum, offs);
1045 	case UBIFS_LPT_LTAB:
1046 		return make_ltab_dirty(c, lnum, offs);
1047 	case UBIFS_LPT_LSAVE:
1048 		return make_lsave_dirty(c, lnum, offs);
1049 	}
1050 	return -EINVAL;
1051 }
1052 
1053 /**
1054  * get_lpt_node_len - return the length of a node based on its type.
1055  * @c: UBIFS file-system description object
1056  * @node_type: LPT node type
1057  */
1058 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1059 {
1060 	switch (node_type) {
1061 	case UBIFS_LPT_NNODE:
1062 		return c->nnode_sz;
1063 	case UBIFS_LPT_PNODE:
1064 		return c->pnode_sz;
1065 	case UBIFS_LPT_LTAB:
1066 		return c->ltab_sz;
1067 	case UBIFS_LPT_LSAVE:
1068 		return c->lsave_sz;
1069 	}
1070 	return 0;
1071 }
1072 
1073 /**
1074  * get_pad_len - return the length of padding in a buffer.
1075  * @c: UBIFS file-system description object
1076  * @buf: buffer
1077  * @len: length of buffer
1078  */
1079 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1080 {
1081 	int offs, pad_len;
1082 
1083 	if (c->min_io_size == 1)
1084 		return 0;
1085 	offs = c->leb_size - len;
1086 	pad_len = ALIGN(offs, c->min_io_size) - offs;
1087 	return pad_len;
1088 }
1089 
1090 /**
1091  * get_lpt_node_type - return type (and node number) of a node in a buffer.
1092  * @c: UBIFS file-system description object
1093  * @buf: buffer
1094  * @node_num: node number is returned here
1095  */
1096 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1097 			     int *node_num)
1098 {
1099 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1100 	int pos = 0, node_type;
1101 
1102 	node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1103 	*node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1104 	return node_type;
1105 }
1106 
1107 /**
1108  * is_a_node - determine if a buffer contains a node.
1109  * @c: UBIFS file-system description object
1110  * @buf: buffer
1111  * @len: length of buffer
1112  *
1113  * This function returns %1 if the buffer contains a node or %0 if it does not.
1114  */
1115 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1116 {
1117 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1118 	int pos = 0, node_type, node_len;
1119 	uint16_t crc, calc_crc;
1120 
1121 	if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1122 		return 0;
1123 	node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1124 	if (node_type == UBIFS_LPT_NOT_A_NODE)
1125 		return 0;
1126 	node_len = get_lpt_node_len(c, node_type);
1127 	if (!node_len || node_len > len)
1128 		return 0;
1129 	pos = 0;
1130 	addr = buf;
1131 	crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1132 	calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1133 			 node_len - UBIFS_LPT_CRC_BYTES);
1134 	if (crc != calc_crc)
1135 		return 0;
1136 	return 1;
1137 }
1138 
1139 /**
1140  * lpt_gc_lnum - garbage collect a LPT LEB.
1141  * @c: UBIFS file-system description object
1142  * @lnum: LEB number to garbage collect
1143  *
1144  * LPT garbage collection is used only for the "big" LPT model
1145  * (c->big_lpt == 1).  Garbage collection simply involves marking all the nodes
1146  * in the LEB being garbage-collected as dirty.  The dirty nodes are written
1147  * next commit, after which the LEB is free to be reused.
1148  *
1149  * This function returns %0 on success and a negative error code on failure.
1150  */
1151 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1152 {
1153 	int err, len = c->leb_size, node_type, node_num, node_len, offs;
1154 	void *buf = c->lpt_buf;
1155 
1156 	dbg_lp("LEB %d", lnum);
1157 
1158 	err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1159 	if (err)
1160 		return err;
1161 
1162 	while (1) {
1163 		if (!is_a_node(c, buf, len)) {
1164 			int pad_len;
1165 
1166 			pad_len = get_pad_len(c, buf, len);
1167 			if (pad_len) {
1168 				buf += pad_len;
1169 				len -= pad_len;
1170 				continue;
1171 			}
1172 			return 0;
1173 		}
1174 		node_type = get_lpt_node_type(c, buf, &node_num);
1175 		node_len = get_lpt_node_len(c, node_type);
1176 		offs = c->leb_size - len;
1177 		ubifs_assert(node_len != 0);
1178 		mutex_lock(&c->lp_mutex);
1179 		err = make_node_dirty(c, node_type, node_num, lnum, offs);
1180 		mutex_unlock(&c->lp_mutex);
1181 		if (err)
1182 			return err;
1183 		buf += node_len;
1184 		len -= node_len;
1185 	}
1186 	return 0;
1187 }
1188 
1189 /**
1190  * lpt_gc - LPT garbage collection.
1191  * @c: UBIFS file-system description object
1192  *
1193  * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1194  * Returns %0 on success and a negative error code on failure.
1195  */
1196 static int lpt_gc(struct ubifs_info *c)
1197 {
1198 	int i, lnum = -1, dirty = 0;
1199 
1200 	mutex_lock(&c->lp_mutex);
1201 	for (i = 0; i < c->lpt_lebs; i++) {
1202 		ubifs_assert(!c->ltab[i].tgc);
1203 		if (i + c->lpt_first == c->nhead_lnum ||
1204 		    c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1205 			continue;
1206 		if (c->ltab[i].dirty > dirty) {
1207 			dirty = c->ltab[i].dirty;
1208 			lnum = i + c->lpt_first;
1209 		}
1210 	}
1211 	mutex_unlock(&c->lp_mutex);
1212 	if (lnum == -1)
1213 		return -ENOSPC;
1214 	return lpt_gc_lnum(c, lnum);
1215 }
1216 
1217 /**
1218  * ubifs_lpt_start_commit - UBIFS commit starts.
1219  * @c: the UBIFS file-system description object
1220  *
1221  * This function has to be called when UBIFS starts the commit operation.
1222  * This function "freezes" all currently dirty LEB properties and does not
1223  * change them anymore. Further changes are saved and tracked separately
1224  * because they are not part of this commit. This function returns zero in case
1225  * of success and a negative error code in case of failure.
1226  */
1227 int ubifs_lpt_start_commit(struct ubifs_info *c)
1228 {
1229 	int err, cnt;
1230 
1231 	dbg_lp("");
1232 
1233 	mutex_lock(&c->lp_mutex);
1234 	err = dbg_chk_lpt_free_spc(c);
1235 	if (err)
1236 		goto out;
1237 	err = dbg_check_ltab(c);
1238 	if (err)
1239 		goto out;
1240 
1241 	if (c->check_lpt_free) {
1242 		/*
1243 		 * We ensure there is enough free space in
1244 		 * ubifs_lpt_post_commit() by marking nodes dirty. That
1245 		 * information is lost when we unmount, so we also need
1246 		 * to check free space once after mounting also.
1247 		 */
1248 		c->check_lpt_free = 0;
1249 		while (need_write_all(c)) {
1250 			mutex_unlock(&c->lp_mutex);
1251 			err = lpt_gc(c);
1252 			if (err)
1253 				return err;
1254 			mutex_lock(&c->lp_mutex);
1255 		}
1256 	}
1257 
1258 	lpt_tgc_start(c);
1259 
1260 	if (!c->dirty_pn_cnt) {
1261 		dbg_cmt("no cnodes to commit");
1262 		err = 0;
1263 		goto out;
1264 	}
1265 
1266 	if (!c->big_lpt && need_write_all(c)) {
1267 		/* If needed, write everything */
1268 		err = make_tree_dirty(c);
1269 		if (err)
1270 			goto out;
1271 		lpt_tgc_start(c);
1272 	}
1273 
1274 	if (c->big_lpt)
1275 		populate_lsave(c);
1276 
1277 	cnt = get_cnodes_to_commit(c);
1278 	ubifs_assert(cnt != 0);
1279 
1280 	err = layout_cnodes(c);
1281 	if (err)
1282 		goto out;
1283 
1284 	/* Copy the LPT's own lprops for end commit to write */
1285 	memcpy(c->ltab_cmt, c->ltab,
1286 	       sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1287 	c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1288 
1289 out:
1290 	mutex_unlock(&c->lp_mutex);
1291 	return err;
1292 }
1293 
1294 /**
1295  * free_obsolete_cnodes - free obsolete cnodes for commit end.
1296  * @c: UBIFS file-system description object
1297  */
1298 static void free_obsolete_cnodes(struct ubifs_info *c)
1299 {
1300 	struct ubifs_cnode *cnode, *cnext;
1301 
1302 	cnext = c->lpt_cnext;
1303 	if (!cnext)
1304 		return;
1305 	do {
1306 		cnode = cnext;
1307 		cnext = cnode->cnext;
1308 		if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1309 			kfree(cnode);
1310 		else
1311 			cnode->cnext = NULL;
1312 	} while (cnext != c->lpt_cnext);
1313 	c->lpt_cnext = NULL;
1314 }
1315 
1316 #ifndef __UBOOT__
1317 /**
1318  * ubifs_lpt_end_commit - finish the commit operation.
1319  * @c: the UBIFS file-system description object
1320  *
1321  * This function has to be called when the commit operation finishes. It
1322  * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1323  * the media. Returns zero in case of success and a negative error code in case
1324  * of failure.
1325  */
1326 int ubifs_lpt_end_commit(struct ubifs_info *c)
1327 {
1328 	int err;
1329 
1330 	dbg_lp("");
1331 
1332 	if (!c->lpt_cnext)
1333 		return 0;
1334 
1335 	err = write_cnodes(c);
1336 	if (err)
1337 		return err;
1338 
1339 	mutex_lock(&c->lp_mutex);
1340 	free_obsolete_cnodes(c);
1341 	mutex_unlock(&c->lp_mutex);
1342 
1343 	return 0;
1344 }
1345 #endif
1346 
1347 /**
1348  * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1349  * @c: UBIFS file-system description object
1350  *
1351  * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1352  * commit for the "big" LPT model.
1353  */
1354 int ubifs_lpt_post_commit(struct ubifs_info *c)
1355 {
1356 	int err;
1357 
1358 	mutex_lock(&c->lp_mutex);
1359 	err = lpt_tgc_end(c);
1360 	if (err)
1361 		goto out;
1362 	if (c->big_lpt)
1363 		while (need_write_all(c)) {
1364 			mutex_unlock(&c->lp_mutex);
1365 			err = lpt_gc(c);
1366 			if (err)
1367 				return err;
1368 			mutex_lock(&c->lp_mutex);
1369 		}
1370 out:
1371 	mutex_unlock(&c->lp_mutex);
1372 	return err;
1373 }
1374 
1375 /**
1376  * first_nnode - find the first nnode in memory.
1377  * @c: UBIFS file-system description object
1378  * @hght: height of tree where nnode found is returned here
1379  *
1380  * This function returns a pointer to the nnode found or %NULL if no nnode is
1381  * found. This function is a helper to 'ubifs_lpt_free()'.
1382  */
1383 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1384 {
1385 	struct ubifs_nnode *nnode;
1386 	int h, i, found;
1387 
1388 	nnode = c->nroot;
1389 	*hght = 0;
1390 	if (!nnode)
1391 		return NULL;
1392 	for (h = 1; h < c->lpt_hght; h++) {
1393 		found = 0;
1394 		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1395 			if (nnode->nbranch[i].nnode) {
1396 				found = 1;
1397 				nnode = nnode->nbranch[i].nnode;
1398 				*hght = h;
1399 				break;
1400 			}
1401 		}
1402 		if (!found)
1403 			break;
1404 	}
1405 	return nnode;
1406 }
1407 
1408 /**
1409  * next_nnode - find the next nnode in memory.
1410  * @c: UBIFS file-system description object
1411  * @nnode: nnode from which to start.
1412  * @hght: height of tree where nnode is, is passed and returned here
1413  *
1414  * This function returns a pointer to the nnode found or %NULL if no nnode is
1415  * found. This function is a helper to 'ubifs_lpt_free()'.
1416  */
1417 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1418 				      struct ubifs_nnode *nnode, int *hght)
1419 {
1420 	struct ubifs_nnode *parent;
1421 	int iip, h, i, found;
1422 
1423 	parent = nnode->parent;
1424 	if (!parent)
1425 		return NULL;
1426 	if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1427 		*hght -= 1;
1428 		return parent;
1429 	}
1430 	for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1431 		nnode = parent->nbranch[iip].nnode;
1432 		if (nnode)
1433 			break;
1434 	}
1435 	if (!nnode) {
1436 		*hght -= 1;
1437 		return parent;
1438 	}
1439 	for (h = *hght + 1; h < c->lpt_hght; h++) {
1440 		found = 0;
1441 		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1442 			if (nnode->nbranch[i].nnode) {
1443 				found = 1;
1444 				nnode = nnode->nbranch[i].nnode;
1445 				*hght = h;
1446 				break;
1447 			}
1448 		}
1449 		if (!found)
1450 			break;
1451 	}
1452 	return nnode;
1453 }
1454 
1455 /**
1456  * ubifs_lpt_free - free resources owned by the LPT.
1457  * @c: UBIFS file-system description object
1458  * @wr_only: free only resources used for writing
1459  */
1460 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1461 {
1462 	struct ubifs_nnode *nnode;
1463 	int i, hght;
1464 
1465 	/* Free write-only things first */
1466 
1467 	free_obsolete_cnodes(c); /* Leftover from a failed commit */
1468 
1469 	vfree(c->ltab_cmt);
1470 	c->ltab_cmt = NULL;
1471 	vfree(c->lpt_buf);
1472 	c->lpt_buf = NULL;
1473 	kfree(c->lsave);
1474 	c->lsave = NULL;
1475 
1476 	if (wr_only)
1477 		return;
1478 
1479 	/* Now free the rest */
1480 
1481 	nnode = first_nnode(c, &hght);
1482 	while (nnode) {
1483 		for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1484 			kfree(nnode->nbranch[i].nnode);
1485 		nnode = next_nnode(c, nnode, &hght);
1486 	}
1487 	for (i = 0; i < LPROPS_HEAP_CNT; i++)
1488 		kfree(c->lpt_heap[i].arr);
1489 	kfree(c->dirty_idx.arr);
1490 	kfree(c->nroot);
1491 	vfree(c->ltab);
1492 	kfree(c->lpt_nod_buf);
1493 }
1494 
1495 #ifndef __UBOOT__
1496 /*
1497  * Everything below is related to debugging.
1498  */
1499 
1500 /**
1501  * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1502  * @buf: buffer
1503  * @len: buffer length
1504  */
1505 static int dbg_is_all_ff(uint8_t *buf, int len)
1506 {
1507 	int i;
1508 
1509 	for (i = 0; i < len; i++)
1510 		if (buf[i] != 0xff)
1511 			return 0;
1512 	return 1;
1513 }
1514 
1515 /**
1516  * dbg_is_nnode_dirty - determine if a nnode is dirty.
1517  * @c: the UBIFS file-system description object
1518  * @lnum: LEB number where nnode was written
1519  * @offs: offset where nnode was written
1520  */
1521 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1522 {
1523 	struct ubifs_nnode *nnode;
1524 	int hght;
1525 
1526 	/* Entire tree is in memory so first_nnode / next_nnode are OK */
1527 	nnode = first_nnode(c, &hght);
1528 	for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1529 		struct ubifs_nbranch *branch;
1530 
1531 		cond_resched();
1532 		if (nnode->parent) {
1533 			branch = &nnode->parent->nbranch[nnode->iip];
1534 			if (branch->lnum != lnum || branch->offs != offs)
1535 				continue;
1536 			if (test_bit(DIRTY_CNODE, &nnode->flags))
1537 				return 1;
1538 			return 0;
1539 		} else {
1540 			if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1541 				continue;
1542 			if (test_bit(DIRTY_CNODE, &nnode->flags))
1543 				return 1;
1544 			return 0;
1545 		}
1546 	}
1547 	return 1;
1548 }
1549 
1550 /**
1551  * dbg_is_pnode_dirty - determine if a pnode is dirty.
1552  * @c: the UBIFS file-system description object
1553  * @lnum: LEB number where pnode was written
1554  * @offs: offset where pnode was written
1555  */
1556 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1557 {
1558 	int i, cnt;
1559 
1560 	cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1561 	for (i = 0; i < cnt; i++) {
1562 		struct ubifs_pnode *pnode;
1563 		struct ubifs_nbranch *branch;
1564 
1565 		cond_resched();
1566 		pnode = pnode_lookup(c, i);
1567 		if (IS_ERR(pnode))
1568 			return PTR_ERR(pnode);
1569 		branch = &pnode->parent->nbranch[pnode->iip];
1570 		if (branch->lnum != lnum || branch->offs != offs)
1571 			continue;
1572 		if (test_bit(DIRTY_CNODE, &pnode->flags))
1573 			return 1;
1574 		return 0;
1575 	}
1576 	return 1;
1577 }
1578 
1579 /**
1580  * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1581  * @c: the UBIFS file-system description object
1582  * @lnum: LEB number where ltab node was written
1583  * @offs: offset where ltab node was written
1584  */
1585 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1586 {
1587 	if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1588 		return 1;
1589 	return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1590 }
1591 
1592 /**
1593  * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1594  * @c: the UBIFS file-system description object
1595  * @lnum: LEB number where lsave node was written
1596  * @offs: offset where lsave node was written
1597  */
1598 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1599 {
1600 	if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1601 		return 1;
1602 	return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1603 }
1604 
1605 /**
1606  * dbg_is_node_dirty - determine if a node is dirty.
1607  * @c: the UBIFS file-system description object
1608  * @node_type: node type
1609  * @lnum: LEB number where node was written
1610  * @offs: offset where node was written
1611  */
1612 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1613 			     int offs)
1614 {
1615 	switch (node_type) {
1616 	case UBIFS_LPT_NNODE:
1617 		return dbg_is_nnode_dirty(c, lnum, offs);
1618 	case UBIFS_LPT_PNODE:
1619 		return dbg_is_pnode_dirty(c, lnum, offs);
1620 	case UBIFS_LPT_LTAB:
1621 		return dbg_is_ltab_dirty(c, lnum, offs);
1622 	case UBIFS_LPT_LSAVE:
1623 		return dbg_is_lsave_dirty(c, lnum, offs);
1624 	}
1625 	return 1;
1626 }
1627 
1628 /**
1629  * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1630  * @c: the UBIFS file-system description object
1631  * @lnum: LEB number where node was written
1632  * @offs: offset where node was written
1633  *
1634  * This function returns %0 on success and a negative error code on failure.
1635  */
1636 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1637 {
1638 	int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1639 	int ret;
1640 	void *buf, *p;
1641 
1642 	if (!dbg_is_chk_lprops(c))
1643 		return 0;
1644 
1645 	buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1646 	if (!buf) {
1647 		ubifs_err("cannot allocate memory for ltab checking");
1648 		return 0;
1649 	}
1650 
1651 	dbg_lp("LEB %d", lnum);
1652 
1653 	err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1654 	if (err)
1655 		goto out;
1656 
1657 	while (1) {
1658 		if (!is_a_node(c, p, len)) {
1659 			int i, pad_len;
1660 
1661 			pad_len = get_pad_len(c, p, len);
1662 			if (pad_len) {
1663 				p += pad_len;
1664 				len -= pad_len;
1665 				dirty += pad_len;
1666 				continue;
1667 			}
1668 			if (!dbg_is_all_ff(p, len)) {
1669 				ubifs_err("invalid empty space in LEB %d at %d",
1670 					  lnum, c->leb_size - len);
1671 				err = -EINVAL;
1672 			}
1673 			i = lnum - c->lpt_first;
1674 			if (len != c->ltab[i].free) {
1675 				ubifs_err("invalid free space in LEB %d (free %d, expected %d)",
1676 					  lnum, len, c->ltab[i].free);
1677 				err = -EINVAL;
1678 			}
1679 			if (dirty != c->ltab[i].dirty) {
1680 				ubifs_err("invalid dirty space in LEB %d (dirty %d, expected %d)",
1681 					  lnum, dirty, c->ltab[i].dirty);
1682 				err = -EINVAL;
1683 			}
1684 			goto out;
1685 		}
1686 		node_type = get_lpt_node_type(c, p, &node_num);
1687 		node_len = get_lpt_node_len(c, node_type);
1688 		ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1689 		if (ret == 1)
1690 			dirty += node_len;
1691 		p += node_len;
1692 		len -= node_len;
1693 	}
1694 
1695 	err = 0;
1696 out:
1697 	vfree(buf);
1698 	return err;
1699 }
1700 
1701 /**
1702  * dbg_check_ltab - check the free and dirty space in the ltab.
1703  * @c: the UBIFS file-system description object
1704  *
1705  * This function returns %0 on success and a negative error code on failure.
1706  */
1707 int dbg_check_ltab(struct ubifs_info *c)
1708 {
1709 	int lnum, err, i, cnt;
1710 
1711 	if (!dbg_is_chk_lprops(c))
1712 		return 0;
1713 
1714 	/* Bring the entire tree into memory */
1715 	cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1716 	for (i = 0; i < cnt; i++) {
1717 		struct ubifs_pnode *pnode;
1718 
1719 		pnode = pnode_lookup(c, i);
1720 		if (IS_ERR(pnode))
1721 			return PTR_ERR(pnode);
1722 		cond_resched();
1723 	}
1724 
1725 	/* Check nodes */
1726 	err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1727 	if (err)
1728 		return err;
1729 
1730 	/* Check each LEB */
1731 	for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1732 		err = dbg_check_ltab_lnum(c, lnum);
1733 		if (err) {
1734 			ubifs_err("failed at LEB %d", lnum);
1735 			return err;
1736 		}
1737 	}
1738 
1739 	dbg_lp("succeeded");
1740 	return 0;
1741 }
1742 
1743 /**
1744  * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1745  * @c: the UBIFS file-system description object
1746  *
1747  * This function returns %0 on success and a negative error code on failure.
1748  */
1749 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1750 {
1751 	long long free = 0;
1752 	int i;
1753 
1754 	if (!dbg_is_chk_lprops(c))
1755 		return 0;
1756 
1757 	for (i = 0; i < c->lpt_lebs; i++) {
1758 		if (c->ltab[i].tgc || c->ltab[i].cmt)
1759 			continue;
1760 		if (i + c->lpt_first == c->nhead_lnum)
1761 			free += c->leb_size - c->nhead_offs;
1762 		else if (c->ltab[i].free == c->leb_size)
1763 			free += c->leb_size;
1764 	}
1765 	if (free < c->lpt_sz) {
1766 		ubifs_err("LPT space error: free %lld lpt_sz %lld",
1767 			  free, c->lpt_sz);
1768 		ubifs_dump_lpt_info(c);
1769 		ubifs_dump_lpt_lebs(c);
1770 		dump_stack();
1771 		return -EINVAL;
1772 	}
1773 	return 0;
1774 }
1775 
1776 /**
1777  * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1778  * @c: the UBIFS file-system description object
1779  * @action: what to do
1780  * @len: length written
1781  *
1782  * This function returns %0 on success and a negative error code on failure.
1783  * The @action argument may be one of:
1784  *   o %0 - LPT debugging checking starts, initialize debugging variables;
1785  *   o %1 - wrote an LPT node, increase LPT size by @len bytes;
1786  *   o %2 - switched to a different LEB and wasted @len bytes;
1787  *   o %3 - check that we've written the right number of bytes.
1788  *   o %4 - wasted @len bytes;
1789  */
1790 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1791 {
1792 	struct ubifs_debug_info *d = c->dbg;
1793 	long long chk_lpt_sz, lpt_sz;
1794 	int err = 0;
1795 
1796 	if (!dbg_is_chk_lprops(c))
1797 		return 0;
1798 
1799 	switch (action) {
1800 	case 0:
1801 		d->chk_lpt_sz = 0;
1802 		d->chk_lpt_sz2 = 0;
1803 		d->chk_lpt_lebs = 0;
1804 		d->chk_lpt_wastage = 0;
1805 		if (c->dirty_pn_cnt > c->pnode_cnt) {
1806 			ubifs_err("dirty pnodes %d exceed max %d",
1807 				  c->dirty_pn_cnt, c->pnode_cnt);
1808 			err = -EINVAL;
1809 		}
1810 		if (c->dirty_nn_cnt > c->nnode_cnt) {
1811 			ubifs_err("dirty nnodes %d exceed max %d",
1812 				  c->dirty_nn_cnt, c->nnode_cnt);
1813 			err = -EINVAL;
1814 		}
1815 		return err;
1816 	case 1:
1817 		d->chk_lpt_sz += len;
1818 		return 0;
1819 	case 2:
1820 		d->chk_lpt_sz += len;
1821 		d->chk_lpt_wastage += len;
1822 		d->chk_lpt_lebs += 1;
1823 		return 0;
1824 	case 3:
1825 		chk_lpt_sz = c->leb_size;
1826 		chk_lpt_sz *= d->chk_lpt_lebs;
1827 		chk_lpt_sz += len - c->nhead_offs;
1828 		if (d->chk_lpt_sz != chk_lpt_sz) {
1829 			ubifs_err("LPT wrote %lld but space used was %lld",
1830 				  d->chk_lpt_sz, chk_lpt_sz);
1831 			err = -EINVAL;
1832 		}
1833 		if (d->chk_lpt_sz > c->lpt_sz) {
1834 			ubifs_err("LPT wrote %lld but lpt_sz is %lld",
1835 				  d->chk_lpt_sz, c->lpt_sz);
1836 			err = -EINVAL;
1837 		}
1838 		if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1839 			ubifs_err("LPT layout size %lld but wrote %lld",
1840 				  d->chk_lpt_sz, d->chk_lpt_sz2);
1841 			err = -EINVAL;
1842 		}
1843 		if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1844 			ubifs_err("LPT new nhead offs: expected %d was %d",
1845 				  d->new_nhead_offs, len);
1846 			err = -EINVAL;
1847 		}
1848 		lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1849 		lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1850 		lpt_sz += c->ltab_sz;
1851 		if (c->big_lpt)
1852 			lpt_sz += c->lsave_sz;
1853 		if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1854 			ubifs_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1855 				  d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1856 			err = -EINVAL;
1857 		}
1858 		if (err) {
1859 			ubifs_dump_lpt_info(c);
1860 			ubifs_dump_lpt_lebs(c);
1861 			dump_stack();
1862 		}
1863 		d->chk_lpt_sz2 = d->chk_lpt_sz;
1864 		d->chk_lpt_sz = 0;
1865 		d->chk_lpt_wastage = 0;
1866 		d->chk_lpt_lebs = 0;
1867 		d->new_nhead_offs = len;
1868 		return err;
1869 	case 4:
1870 		d->chk_lpt_sz += len;
1871 		d->chk_lpt_wastage += len;
1872 		return 0;
1873 	default:
1874 		return -EINVAL;
1875 	}
1876 }
1877 
1878 /**
1879  * ubifs_dump_lpt_leb - dump an LPT LEB.
1880  * @c: UBIFS file-system description object
1881  * @lnum: LEB number to dump
1882  *
1883  * This function dumps an LEB from LPT area. Nodes in this area are very
1884  * different to nodes in the main area (e.g., they do not have common headers,
1885  * they do not have 8-byte alignments, etc), so we have a separate function to
1886  * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1887  */
1888 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1889 {
1890 	int err, len = c->leb_size, node_type, node_num, node_len, offs;
1891 	void *buf, *p;
1892 
1893 	pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
1894 	buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1895 	if (!buf) {
1896 		ubifs_err("cannot allocate memory to dump LPT");
1897 		return;
1898 	}
1899 
1900 	err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1901 	if (err)
1902 		goto out;
1903 
1904 	while (1) {
1905 		offs = c->leb_size - len;
1906 		if (!is_a_node(c, p, len)) {
1907 			int pad_len;
1908 
1909 			pad_len = get_pad_len(c, p, len);
1910 			if (pad_len) {
1911 				pr_err("LEB %d:%d, pad %d bytes\n",
1912 				       lnum, offs, pad_len);
1913 				p += pad_len;
1914 				len -= pad_len;
1915 				continue;
1916 			}
1917 			if (len)
1918 				pr_err("LEB %d:%d, free %d bytes\n",
1919 				       lnum, offs, len);
1920 			break;
1921 		}
1922 
1923 		node_type = get_lpt_node_type(c, p, &node_num);
1924 		switch (node_type) {
1925 		case UBIFS_LPT_PNODE:
1926 		{
1927 			node_len = c->pnode_sz;
1928 			if (c->big_lpt)
1929 				pr_err("LEB %d:%d, pnode num %d\n",
1930 				       lnum, offs, node_num);
1931 			else
1932 				pr_err("LEB %d:%d, pnode\n", lnum, offs);
1933 			break;
1934 		}
1935 		case UBIFS_LPT_NNODE:
1936 		{
1937 			int i;
1938 			struct ubifs_nnode nnode;
1939 
1940 			node_len = c->nnode_sz;
1941 			if (c->big_lpt)
1942 				pr_err("LEB %d:%d, nnode num %d, ",
1943 				       lnum, offs, node_num);
1944 			else
1945 				pr_err("LEB %d:%d, nnode, ",
1946 				       lnum, offs);
1947 			err = ubifs_unpack_nnode(c, p, &nnode);
1948 			for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1949 				pr_cont("%d:%d", nnode.nbranch[i].lnum,
1950 				       nnode.nbranch[i].offs);
1951 				if (i != UBIFS_LPT_FANOUT - 1)
1952 					pr_cont(", ");
1953 			}
1954 			pr_cont("\n");
1955 			break;
1956 		}
1957 		case UBIFS_LPT_LTAB:
1958 			node_len = c->ltab_sz;
1959 			pr_err("LEB %d:%d, ltab\n", lnum, offs);
1960 			break;
1961 		case UBIFS_LPT_LSAVE:
1962 			node_len = c->lsave_sz;
1963 			pr_err("LEB %d:%d, lsave len\n", lnum, offs);
1964 			break;
1965 		default:
1966 			ubifs_err("LPT node type %d not recognized", node_type);
1967 			goto out;
1968 		}
1969 
1970 		p += node_len;
1971 		len -= node_len;
1972 	}
1973 
1974 	pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
1975 out:
1976 	vfree(buf);
1977 	return;
1978 }
1979 
1980 /**
1981  * ubifs_dump_lpt_lebs - dump LPT lebs.
1982  * @c: UBIFS file-system description object
1983  *
1984  * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1985  * locked.
1986  */
1987 void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
1988 {
1989 	int i;
1990 
1991 	pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
1992 	for (i = 0; i < c->lpt_lebs; i++)
1993 		dump_lpt_leb(c, i + c->lpt_first);
1994 	pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
1995 }
1996 
1997 /**
1998  * dbg_populate_lsave - debugging version of 'populate_lsave()'
1999  * @c: UBIFS file-system description object
2000  *
2001  * This is a debugging version for 'populate_lsave()' which populates lsave
2002  * with random LEBs instead of useful LEBs, which is good for test coverage.
2003  * Returns zero if lsave has not been populated (this debugging feature is
2004  * disabled) an non-zero if lsave has been populated.
2005  */
2006 static int dbg_populate_lsave(struct ubifs_info *c)
2007 {
2008 	struct ubifs_lprops *lprops;
2009 	struct ubifs_lpt_heap *heap;
2010 	int i;
2011 
2012 	if (!dbg_is_chk_gen(c))
2013 		return 0;
2014 	if (prandom_u32() & 3)
2015 		return 0;
2016 
2017 	for (i = 0; i < c->lsave_cnt; i++)
2018 		c->lsave[i] = c->main_first;
2019 
2020 	list_for_each_entry(lprops, &c->empty_list, list)
2021 		c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2022 	list_for_each_entry(lprops, &c->freeable_list, list)
2023 		c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2024 	list_for_each_entry(lprops, &c->frdi_idx_list, list)
2025 		c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2026 
2027 	heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2028 	for (i = 0; i < heap->cnt; i++)
2029 		c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2030 	heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2031 	for (i = 0; i < heap->cnt; i++)
2032 		c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2033 	heap = &c->lpt_heap[LPROPS_FREE - 1];
2034 	for (i = 0; i < heap->cnt; i++)
2035 		c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2036 
2037 	return 1;
2038 }
2039 #endif
2040