xref: /openbmc/u-boot/fs/ubifs/lpt.c (revision 47539e23)
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 the LEB properties tree (LPT) area. The LPT area
14  * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
15  * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
16  * between the log and the orphan area.
17  *
18  * The LPT area is like a miniature self-contained file system. It is required
19  * that it never runs out of space, is fast to access and update, and scales
20  * logarithmically. The LEB properties tree is implemented as a wandering tree
21  * much like the TNC, and the LPT area has its own garbage collection.
22  *
23  * The LPT has two slightly different forms called the "small model" and the
24  * "big model". The small model is used when the entire LEB properties table
25  * can be written into a single eraseblock. In that case, garbage collection
26  * consists of just writing the whole table, which therefore makes all other
27  * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
28  * selected for garbage collection, which consists of marking the clean nodes in
29  * that LEB as dirty, and then only the dirty nodes are written out. Also, in
30  * the case of the big model, a table of LEB numbers is saved so that the entire
31  * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
32  * mounted.
33  */
34 
35 #include "ubifs.h"
36 #define __UBOOT__
37 #ifndef __UBOOT__
38 #include <linux/crc16.h>
39 #include <linux/math64.h>
40 #include <linux/slab.h>
41 #else
42 #include <linux/compat.h>
43 #include <linux/err.h>
44 #include <ubi_uboot.h>
45 #include "crc16.h"
46 #endif
47 
48 /**
49  * do_calc_lpt_geom - calculate sizes for the LPT area.
50  * @c: the UBIFS file-system description object
51  *
52  * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
53  * properties of the flash and whether LPT is "big" (c->big_lpt).
54  */
55 static void do_calc_lpt_geom(struct ubifs_info *c)
56 {
57 	int i, n, bits, per_leb_wastage, max_pnode_cnt;
58 	long long sz, tot_wastage;
59 
60 	n = c->main_lebs + c->max_leb_cnt - c->leb_cnt;
61 	max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
62 
63 	c->lpt_hght = 1;
64 	n = UBIFS_LPT_FANOUT;
65 	while (n < max_pnode_cnt) {
66 		c->lpt_hght += 1;
67 		n <<= UBIFS_LPT_FANOUT_SHIFT;
68 	}
69 
70 	c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
71 
72 	n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT);
73 	c->nnode_cnt = n;
74 	for (i = 1; i < c->lpt_hght; i++) {
75 		n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
76 		c->nnode_cnt += n;
77 	}
78 
79 	c->space_bits = fls(c->leb_size) - 3;
80 	c->lpt_lnum_bits = fls(c->lpt_lebs);
81 	c->lpt_offs_bits = fls(c->leb_size - 1);
82 	c->lpt_spc_bits = fls(c->leb_size);
83 
84 	n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
85 	c->pcnt_bits = fls(n - 1);
86 
87 	c->lnum_bits = fls(c->max_leb_cnt - 1);
88 
89 	bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
90 	       (c->big_lpt ? c->pcnt_bits : 0) +
91 	       (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT;
92 	c->pnode_sz = (bits + 7) / 8;
93 
94 	bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
95 	       (c->big_lpt ? c->pcnt_bits : 0) +
96 	       (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT;
97 	c->nnode_sz = (bits + 7) / 8;
98 
99 	bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
100 	       c->lpt_lebs * c->lpt_spc_bits * 2;
101 	c->ltab_sz = (bits + 7) / 8;
102 
103 	bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
104 	       c->lnum_bits * c->lsave_cnt;
105 	c->lsave_sz = (bits + 7) / 8;
106 
107 	/* Calculate the minimum LPT size */
108 	c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
109 	c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
110 	c->lpt_sz += c->ltab_sz;
111 	if (c->big_lpt)
112 		c->lpt_sz += c->lsave_sz;
113 
114 	/* Add wastage */
115 	sz = c->lpt_sz;
116 	per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz);
117 	sz += per_leb_wastage;
118 	tot_wastage = per_leb_wastage;
119 	while (sz > c->leb_size) {
120 		sz += per_leb_wastage;
121 		sz -= c->leb_size;
122 		tot_wastage += per_leb_wastage;
123 	}
124 	tot_wastage += ALIGN(sz, c->min_io_size) - sz;
125 	c->lpt_sz += tot_wastage;
126 }
127 
128 /**
129  * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
130  * @c: the UBIFS file-system description object
131  *
132  * This function returns %0 on success and a negative error code on failure.
133  */
134 int ubifs_calc_lpt_geom(struct ubifs_info *c)
135 {
136 	int lebs_needed;
137 	long long sz;
138 
139 	do_calc_lpt_geom(c);
140 
141 	/* Verify that lpt_lebs is big enough */
142 	sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
143 	lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
144 	if (lebs_needed > c->lpt_lebs) {
145 		ubifs_err("too few LPT LEBs");
146 		return -EINVAL;
147 	}
148 
149 	/* Verify that ltab fits in a single LEB (since ltab is a single node */
150 	if (c->ltab_sz > c->leb_size) {
151 		ubifs_err("LPT ltab too big");
152 		return -EINVAL;
153 	}
154 
155 	c->check_lpt_free = c->big_lpt;
156 	return 0;
157 }
158 
159 /**
160  * calc_dflt_lpt_geom - calculate default LPT geometry.
161  * @c: the UBIFS file-system description object
162  * @main_lebs: number of main area LEBs is passed and returned here
163  * @big_lpt: whether the LPT area is "big" is returned here
164  *
165  * The size of the LPT area depends on parameters that themselves are dependent
166  * on the size of the LPT area. This function, successively recalculates the LPT
167  * area geometry until the parameters and resultant geometry are consistent.
168  *
169  * This function returns %0 on success and a negative error code on failure.
170  */
171 static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
172 			      int *big_lpt)
173 {
174 	int i, lebs_needed;
175 	long long sz;
176 
177 	/* Start by assuming the minimum number of LPT LEBs */
178 	c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
179 	c->main_lebs = *main_lebs - c->lpt_lebs;
180 	if (c->main_lebs <= 0)
181 		return -EINVAL;
182 
183 	/* And assume we will use the small LPT model */
184 	c->big_lpt = 0;
185 
186 	/*
187 	 * Calculate the geometry based on assumptions above and then see if it
188 	 * makes sense
189 	 */
190 	do_calc_lpt_geom(c);
191 
192 	/* Small LPT model must have lpt_sz < leb_size */
193 	if (c->lpt_sz > c->leb_size) {
194 		/* Nope, so try again using big LPT model */
195 		c->big_lpt = 1;
196 		do_calc_lpt_geom(c);
197 	}
198 
199 	/* Now check there are enough LPT LEBs */
200 	for (i = 0; i < 64 ; i++) {
201 		sz = c->lpt_sz * 4; /* Allow 4 times the size */
202 		lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
203 		if (lebs_needed > c->lpt_lebs) {
204 			/* Not enough LPT LEBs so try again with more */
205 			c->lpt_lebs = lebs_needed;
206 			c->main_lebs = *main_lebs - c->lpt_lebs;
207 			if (c->main_lebs <= 0)
208 				return -EINVAL;
209 			do_calc_lpt_geom(c);
210 			continue;
211 		}
212 		if (c->ltab_sz > c->leb_size) {
213 			ubifs_err("LPT ltab too big");
214 			return -EINVAL;
215 		}
216 		*main_lebs = c->main_lebs;
217 		*big_lpt = c->big_lpt;
218 		return 0;
219 	}
220 	return -EINVAL;
221 }
222 
223 /**
224  * pack_bits - pack bit fields end-to-end.
225  * @addr: address at which to pack (passed and next address returned)
226  * @pos: bit position at which to pack (passed and next position returned)
227  * @val: value to pack
228  * @nrbits: number of bits of value to pack (1-32)
229  */
230 static void pack_bits(uint8_t **addr, int *pos, uint32_t val, int nrbits)
231 {
232 	uint8_t *p = *addr;
233 	int b = *pos;
234 
235 	ubifs_assert(nrbits > 0);
236 	ubifs_assert(nrbits <= 32);
237 	ubifs_assert(*pos >= 0);
238 	ubifs_assert(*pos < 8);
239 	ubifs_assert((val >> nrbits) == 0 || nrbits == 32);
240 	if (b) {
241 		*p |= ((uint8_t)val) << b;
242 		nrbits += b;
243 		if (nrbits > 8) {
244 			*++p = (uint8_t)(val >>= (8 - b));
245 			if (nrbits > 16) {
246 				*++p = (uint8_t)(val >>= 8);
247 				if (nrbits > 24) {
248 					*++p = (uint8_t)(val >>= 8);
249 					if (nrbits > 32)
250 						*++p = (uint8_t)(val >>= 8);
251 				}
252 			}
253 		}
254 	} else {
255 		*p = (uint8_t)val;
256 		if (nrbits > 8) {
257 			*++p = (uint8_t)(val >>= 8);
258 			if (nrbits > 16) {
259 				*++p = (uint8_t)(val >>= 8);
260 				if (nrbits > 24)
261 					*++p = (uint8_t)(val >>= 8);
262 			}
263 		}
264 	}
265 	b = nrbits & 7;
266 	if (b == 0)
267 		p++;
268 	*addr = p;
269 	*pos = b;
270 }
271 
272 /**
273  * ubifs_unpack_bits - unpack bit fields.
274  * @addr: address at which to unpack (passed and next address returned)
275  * @pos: bit position at which to unpack (passed and next position returned)
276  * @nrbits: number of bits of value to unpack (1-32)
277  *
278  * This functions returns the value unpacked.
279  */
280 uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
281 {
282 	const int k = 32 - nrbits;
283 	uint8_t *p = *addr;
284 	int b = *pos;
285 	uint32_t uninitialized_var(val);
286 	const int bytes = (nrbits + b + 7) >> 3;
287 
288 	ubifs_assert(nrbits > 0);
289 	ubifs_assert(nrbits <= 32);
290 	ubifs_assert(*pos >= 0);
291 	ubifs_assert(*pos < 8);
292 	if (b) {
293 		switch (bytes) {
294 		case 2:
295 			val = p[1];
296 			break;
297 		case 3:
298 			val = p[1] | ((uint32_t)p[2] << 8);
299 			break;
300 		case 4:
301 			val = p[1] | ((uint32_t)p[2] << 8) |
302 				     ((uint32_t)p[3] << 16);
303 			break;
304 		case 5:
305 			val = p[1] | ((uint32_t)p[2] << 8) |
306 				     ((uint32_t)p[3] << 16) |
307 				     ((uint32_t)p[4] << 24);
308 		}
309 		val <<= (8 - b);
310 		val |= *p >> b;
311 		nrbits += b;
312 	} else {
313 		switch (bytes) {
314 		case 1:
315 			val = p[0];
316 			break;
317 		case 2:
318 			val = p[0] | ((uint32_t)p[1] << 8);
319 			break;
320 		case 3:
321 			val = p[0] | ((uint32_t)p[1] << 8) |
322 				     ((uint32_t)p[2] << 16);
323 			break;
324 		case 4:
325 			val = p[0] | ((uint32_t)p[1] << 8) |
326 				     ((uint32_t)p[2] << 16) |
327 				     ((uint32_t)p[3] << 24);
328 			break;
329 		}
330 	}
331 	val <<= k;
332 	val >>= k;
333 	b = nrbits & 7;
334 	p += nrbits >> 3;
335 	*addr = p;
336 	*pos = b;
337 	ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
338 	return val;
339 }
340 
341 /**
342  * ubifs_pack_pnode - pack all the bit fields of a pnode.
343  * @c: UBIFS file-system description object
344  * @buf: buffer into which to pack
345  * @pnode: pnode to pack
346  */
347 void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
348 		      struct ubifs_pnode *pnode)
349 {
350 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
351 	int i, pos = 0;
352 	uint16_t crc;
353 
354 	pack_bits(&addr, &pos, UBIFS_LPT_PNODE, UBIFS_LPT_TYPE_BITS);
355 	if (c->big_lpt)
356 		pack_bits(&addr, &pos, pnode->num, c->pcnt_bits);
357 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
358 		pack_bits(&addr, &pos, pnode->lprops[i].free >> 3,
359 			  c->space_bits);
360 		pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3,
361 			  c->space_bits);
362 		if (pnode->lprops[i].flags & LPROPS_INDEX)
363 			pack_bits(&addr, &pos, 1, 1);
364 		else
365 			pack_bits(&addr, &pos, 0, 1);
366 	}
367 	crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
368 		    c->pnode_sz - UBIFS_LPT_CRC_BYTES);
369 	addr = buf;
370 	pos = 0;
371 	pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
372 }
373 
374 /**
375  * ubifs_pack_nnode - pack all the bit fields of a nnode.
376  * @c: UBIFS file-system description object
377  * @buf: buffer into which to pack
378  * @nnode: nnode to pack
379  */
380 void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
381 		      struct ubifs_nnode *nnode)
382 {
383 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
384 	int i, pos = 0;
385 	uint16_t crc;
386 
387 	pack_bits(&addr, &pos, UBIFS_LPT_NNODE, UBIFS_LPT_TYPE_BITS);
388 	if (c->big_lpt)
389 		pack_bits(&addr, &pos, nnode->num, c->pcnt_bits);
390 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
391 		int lnum = nnode->nbranch[i].lnum;
392 
393 		if (lnum == 0)
394 			lnum = c->lpt_last + 1;
395 		pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits);
396 		pack_bits(&addr, &pos, nnode->nbranch[i].offs,
397 			  c->lpt_offs_bits);
398 	}
399 	crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
400 		    c->nnode_sz - UBIFS_LPT_CRC_BYTES);
401 	addr = buf;
402 	pos = 0;
403 	pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
404 }
405 
406 /**
407  * ubifs_pack_ltab - pack the LPT's own lprops table.
408  * @c: UBIFS file-system description object
409  * @buf: buffer into which to pack
410  * @ltab: LPT's own lprops table to pack
411  */
412 void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
413 		     struct ubifs_lpt_lprops *ltab)
414 {
415 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
416 	int i, pos = 0;
417 	uint16_t crc;
418 
419 	pack_bits(&addr, &pos, UBIFS_LPT_LTAB, UBIFS_LPT_TYPE_BITS);
420 	for (i = 0; i < c->lpt_lebs; i++) {
421 		pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits);
422 		pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits);
423 	}
424 	crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
425 		    c->ltab_sz - UBIFS_LPT_CRC_BYTES);
426 	addr = buf;
427 	pos = 0;
428 	pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
429 }
430 
431 /**
432  * ubifs_pack_lsave - pack the LPT's save table.
433  * @c: UBIFS file-system description object
434  * @buf: buffer into which to pack
435  * @lsave: LPT's save table to pack
436  */
437 void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave)
438 {
439 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
440 	int i, pos = 0;
441 	uint16_t crc;
442 
443 	pack_bits(&addr, &pos, UBIFS_LPT_LSAVE, UBIFS_LPT_TYPE_BITS);
444 	for (i = 0; i < c->lsave_cnt; i++)
445 		pack_bits(&addr, &pos, lsave[i], c->lnum_bits);
446 	crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
447 		    c->lsave_sz - UBIFS_LPT_CRC_BYTES);
448 	addr = buf;
449 	pos = 0;
450 	pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
451 }
452 
453 /**
454  * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
455  * @c: UBIFS file-system description object
456  * @lnum: LEB number to which to add dirty space
457  * @dirty: amount of dirty space to add
458  */
459 void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty)
460 {
461 	if (!dirty || !lnum)
462 		return;
463 	dbg_lp("LEB %d add %d to %d",
464 	       lnum, dirty, c->ltab[lnum - c->lpt_first].dirty);
465 	ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
466 	c->ltab[lnum - c->lpt_first].dirty += dirty;
467 }
468 
469 /**
470  * set_ltab - set LPT LEB properties.
471  * @c: UBIFS file-system description object
472  * @lnum: LEB number
473  * @free: amount of free space
474  * @dirty: amount of dirty space
475  */
476 static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
477 {
478 	dbg_lp("LEB %d free %d dirty %d to %d %d",
479 	       lnum, c->ltab[lnum - c->lpt_first].free,
480 	       c->ltab[lnum - c->lpt_first].dirty, free, dirty);
481 	ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
482 	c->ltab[lnum - c->lpt_first].free = free;
483 	c->ltab[lnum - c->lpt_first].dirty = dirty;
484 }
485 
486 /**
487  * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
488  * @c: UBIFS file-system description object
489  * @nnode: nnode for which to add dirt
490  */
491 void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode)
492 {
493 	struct ubifs_nnode *np = nnode->parent;
494 
495 	if (np)
496 		ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum,
497 				   c->nnode_sz);
498 	else {
499 		ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz);
500 		if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
501 			c->lpt_drty_flgs |= LTAB_DIRTY;
502 			ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
503 		}
504 	}
505 }
506 
507 /**
508  * add_pnode_dirt - add dirty space to LPT LEB properties.
509  * @c: UBIFS file-system description object
510  * @pnode: pnode for which to add dirt
511  */
512 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
513 {
514 	ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
515 			   c->pnode_sz);
516 }
517 
518 /**
519  * calc_nnode_num - calculate nnode number.
520  * @row: the row in the tree (root is zero)
521  * @col: the column in the row (leftmost is zero)
522  *
523  * The nnode number is a number that uniquely identifies a nnode and can be used
524  * easily to traverse the tree from the root to that nnode.
525  *
526  * This function calculates and returns the nnode number for the nnode at @row
527  * and @col.
528  */
529 static int calc_nnode_num(int row, int col)
530 {
531 	int num, bits;
532 
533 	num = 1;
534 	while (row--) {
535 		bits = (col & (UBIFS_LPT_FANOUT - 1));
536 		col >>= UBIFS_LPT_FANOUT_SHIFT;
537 		num <<= UBIFS_LPT_FANOUT_SHIFT;
538 		num |= bits;
539 	}
540 	return num;
541 }
542 
543 /**
544  * calc_nnode_num_from_parent - calculate nnode number.
545  * @c: UBIFS file-system description object
546  * @parent: parent nnode
547  * @iip: index in parent
548  *
549  * The nnode number is a number that uniquely identifies a nnode and can be used
550  * easily to traverse the tree from the root to that nnode.
551  *
552  * This function calculates and returns the nnode number based on the parent's
553  * nnode number and the index in parent.
554  */
555 static int calc_nnode_num_from_parent(const struct ubifs_info *c,
556 				      struct ubifs_nnode *parent, int iip)
557 {
558 	int num, shft;
559 
560 	if (!parent)
561 		return 1;
562 	shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
563 	num = parent->num ^ (1 << shft);
564 	num |= (UBIFS_LPT_FANOUT + iip) << shft;
565 	return num;
566 }
567 
568 /**
569  * calc_pnode_num_from_parent - calculate pnode number.
570  * @c: UBIFS file-system description object
571  * @parent: parent nnode
572  * @iip: index in parent
573  *
574  * The pnode number is a number that uniquely identifies a pnode and can be used
575  * easily to traverse the tree from the root to that pnode.
576  *
577  * This function calculates and returns the pnode number based on the parent's
578  * nnode number and the index in parent.
579  */
580 static int calc_pnode_num_from_parent(const struct ubifs_info *c,
581 				      struct ubifs_nnode *parent, int iip)
582 {
583 	int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
584 
585 	for (i = 0; i < n; i++) {
586 		num <<= UBIFS_LPT_FANOUT_SHIFT;
587 		num |= pnum & (UBIFS_LPT_FANOUT - 1);
588 		pnum >>= UBIFS_LPT_FANOUT_SHIFT;
589 	}
590 	num <<= UBIFS_LPT_FANOUT_SHIFT;
591 	num |= iip;
592 	return num;
593 }
594 
595 /**
596  * ubifs_create_dflt_lpt - create default LPT.
597  * @c: UBIFS file-system description object
598  * @main_lebs: number of main area LEBs is passed and returned here
599  * @lpt_first: LEB number of first LPT LEB
600  * @lpt_lebs: number of LEBs for LPT is passed and returned here
601  * @big_lpt: use big LPT model is passed and returned here
602  *
603  * This function returns %0 on success and a negative error code on failure.
604  */
605 int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
606 			  int *lpt_lebs, int *big_lpt)
607 {
608 	int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row;
609 	int blnum, boffs, bsz, bcnt;
610 	struct ubifs_pnode *pnode = NULL;
611 	struct ubifs_nnode *nnode = NULL;
612 	void *buf = NULL, *p;
613 	struct ubifs_lpt_lprops *ltab = NULL;
614 	int *lsave = NULL;
615 
616 	err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
617 	if (err)
618 		return err;
619 	*lpt_lebs = c->lpt_lebs;
620 
621 	/* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
622 	c->lpt_first = lpt_first;
623 	/* Needed by 'set_ltab()' */
624 	c->lpt_last = lpt_first + c->lpt_lebs - 1;
625 	/* Needed by 'ubifs_pack_lsave()' */
626 	c->main_first = c->leb_cnt - *main_lebs;
627 
628 	lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL);
629 	pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
630 	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
631 	buf = vmalloc(c->leb_size);
632 	ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
633 	if (!pnode || !nnode || !buf || !ltab || !lsave) {
634 		err = -ENOMEM;
635 		goto out;
636 	}
637 
638 	ubifs_assert(!c->ltab);
639 	c->ltab = ltab; /* Needed by set_ltab */
640 
641 	/* Initialize LPT's own lprops */
642 	for (i = 0; i < c->lpt_lebs; i++) {
643 		ltab[i].free = c->leb_size;
644 		ltab[i].dirty = 0;
645 		ltab[i].tgc = 0;
646 		ltab[i].cmt = 0;
647 	}
648 
649 	lnum = lpt_first;
650 	p = buf;
651 	/* Number of leaf nodes (pnodes) */
652 	cnt = c->pnode_cnt;
653 
654 	/*
655 	 * The first pnode contains the LEB properties for the LEBs that contain
656 	 * the root inode node and the root index node of the index tree.
657 	 */
658 	node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
659 	iopos = ALIGN(node_sz, c->min_io_size);
660 	pnode->lprops[0].free = c->leb_size - iopos;
661 	pnode->lprops[0].dirty = iopos - node_sz;
662 	pnode->lprops[0].flags = LPROPS_INDEX;
663 
664 	node_sz = UBIFS_INO_NODE_SZ;
665 	iopos = ALIGN(node_sz, c->min_io_size);
666 	pnode->lprops[1].free = c->leb_size - iopos;
667 	pnode->lprops[1].dirty = iopos - node_sz;
668 
669 	for (i = 2; i < UBIFS_LPT_FANOUT; i++)
670 		pnode->lprops[i].free = c->leb_size;
671 
672 	/* Add first pnode */
673 	ubifs_pack_pnode(c, p, pnode);
674 	p += c->pnode_sz;
675 	len = c->pnode_sz;
676 	pnode->num += 1;
677 
678 	/* Reset pnode values for remaining pnodes */
679 	pnode->lprops[0].free = c->leb_size;
680 	pnode->lprops[0].dirty = 0;
681 	pnode->lprops[0].flags = 0;
682 
683 	pnode->lprops[1].free = c->leb_size;
684 	pnode->lprops[1].dirty = 0;
685 
686 	/*
687 	 * To calculate the internal node branches, we keep information about
688 	 * the level below.
689 	 */
690 	blnum = lnum; /* LEB number of level below */
691 	boffs = 0; /* Offset of level below */
692 	bcnt = cnt; /* Number of nodes in level below */
693 	bsz = c->pnode_sz; /* Size of nodes in level below */
694 
695 	/* Add all remaining pnodes */
696 	for (i = 1; i < cnt; i++) {
697 		if (len + c->pnode_sz > c->leb_size) {
698 			alen = ALIGN(len, c->min_io_size);
699 			set_ltab(c, lnum, c->leb_size - alen, alen - len);
700 			memset(p, 0xff, alen - len);
701 			err = ubifs_leb_change(c, lnum++, buf, alen);
702 			if (err)
703 				goto out;
704 			p = buf;
705 			len = 0;
706 		}
707 		ubifs_pack_pnode(c, p, pnode);
708 		p += c->pnode_sz;
709 		len += c->pnode_sz;
710 		/*
711 		 * pnodes are simply numbered left to right starting at zero,
712 		 * which means the pnode number can be used easily to traverse
713 		 * down the tree to the corresponding pnode.
714 		 */
715 		pnode->num += 1;
716 	}
717 
718 	row = 0;
719 	for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
720 		row += 1;
721 	/* Add all nnodes, one level at a time */
722 	while (1) {
723 		/* Number of internal nodes (nnodes) at next level */
724 		cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
725 		for (i = 0; i < cnt; i++) {
726 			if (len + c->nnode_sz > c->leb_size) {
727 				alen = ALIGN(len, c->min_io_size);
728 				set_ltab(c, lnum, c->leb_size - alen,
729 					    alen - len);
730 				memset(p, 0xff, alen - len);
731 				err = ubifs_leb_change(c, lnum++, buf, alen);
732 				if (err)
733 					goto out;
734 				p = buf;
735 				len = 0;
736 			}
737 			/* Only 1 nnode at this level, so it is the root */
738 			if (cnt == 1) {
739 				c->lpt_lnum = lnum;
740 				c->lpt_offs = len;
741 			}
742 			/* Set branches to the level below */
743 			for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
744 				if (bcnt) {
745 					if (boffs + bsz > c->leb_size) {
746 						blnum += 1;
747 						boffs = 0;
748 					}
749 					nnode->nbranch[j].lnum = blnum;
750 					nnode->nbranch[j].offs = boffs;
751 					boffs += bsz;
752 					bcnt--;
753 				} else {
754 					nnode->nbranch[j].lnum = 0;
755 					nnode->nbranch[j].offs = 0;
756 				}
757 			}
758 			nnode->num = calc_nnode_num(row, i);
759 			ubifs_pack_nnode(c, p, nnode);
760 			p += c->nnode_sz;
761 			len += c->nnode_sz;
762 		}
763 		/* Only 1 nnode at this level, so it is the root */
764 		if (cnt == 1)
765 			break;
766 		/* Update the information about the level below */
767 		bcnt = cnt;
768 		bsz = c->nnode_sz;
769 		row -= 1;
770 	}
771 
772 	if (*big_lpt) {
773 		/* Need to add LPT's save table */
774 		if (len + c->lsave_sz > c->leb_size) {
775 			alen = ALIGN(len, c->min_io_size);
776 			set_ltab(c, lnum, c->leb_size - alen, alen - len);
777 			memset(p, 0xff, alen - len);
778 			err = ubifs_leb_change(c, lnum++, buf, alen);
779 			if (err)
780 				goto out;
781 			p = buf;
782 			len = 0;
783 		}
784 
785 		c->lsave_lnum = lnum;
786 		c->lsave_offs = len;
787 
788 		for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
789 			lsave[i] = c->main_first + i;
790 		for (; i < c->lsave_cnt; i++)
791 			lsave[i] = c->main_first;
792 
793 		ubifs_pack_lsave(c, p, lsave);
794 		p += c->lsave_sz;
795 		len += c->lsave_sz;
796 	}
797 
798 	/* Need to add LPT's own LEB properties table */
799 	if (len + c->ltab_sz > c->leb_size) {
800 		alen = ALIGN(len, c->min_io_size);
801 		set_ltab(c, lnum, c->leb_size - alen, alen - len);
802 		memset(p, 0xff, alen - len);
803 		err = ubifs_leb_change(c, lnum++, buf, alen);
804 		if (err)
805 			goto out;
806 		p = buf;
807 		len = 0;
808 	}
809 
810 	c->ltab_lnum = lnum;
811 	c->ltab_offs = len;
812 
813 	/* Update ltab before packing it */
814 	len += c->ltab_sz;
815 	alen = ALIGN(len, c->min_io_size);
816 	set_ltab(c, lnum, c->leb_size - alen, alen - len);
817 
818 	ubifs_pack_ltab(c, p, ltab);
819 	p += c->ltab_sz;
820 
821 	/* Write remaining buffer */
822 	memset(p, 0xff, alen - len);
823 	err = ubifs_leb_change(c, lnum, buf, alen);
824 	if (err)
825 		goto out;
826 
827 	c->nhead_lnum = lnum;
828 	c->nhead_offs = ALIGN(len, c->min_io_size);
829 
830 	dbg_lp("space_bits %d", c->space_bits);
831 	dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
832 	dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
833 	dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
834 	dbg_lp("pcnt_bits %d", c->pcnt_bits);
835 	dbg_lp("lnum_bits %d", c->lnum_bits);
836 	dbg_lp("pnode_sz %d", c->pnode_sz);
837 	dbg_lp("nnode_sz %d", c->nnode_sz);
838 	dbg_lp("ltab_sz %d", c->ltab_sz);
839 	dbg_lp("lsave_sz %d", c->lsave_sz);
840 	dbg_lp("lsave_cnt %d", c->lsave_cnt);
841 	dbg_lp("lpt_hght %d", c->lpt_hght);
842 	dbg_lp("big_lpt %d", c->big_lpt);
843 	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
844 	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
845 	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
846 	if (c->big_lpt)
847 		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
848 out:
849 	c->ltab = NULL;
850 	kfree(lsave);
851 	vfree(ltab);
852 	vfree(buf);
853 	kfree(nnode);
854 	kfree(pnode);
855 	return err;
856 }
857 
858 /**
859  * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
860  * @c: UBIFS file-system description object
861  * @pnode: pnode
862  *
863  * When a pnode is loaded into memory, the LEB properties it contains are added,
864  * by this function, to the LEB category lists and heaps.
865  */
866 static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
867 {
868 	int i;
869 
870 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
871 		int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
872 		int lnum = pnode->lprops[i].lnum;
873 
874 		if (!lnum)
875 			return;
876 		ubifs_add_to_cat(c, &pnode->lprops[i], cat);
877 	}
878 }
879 
880 /**
881  * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
882  * @c: UBIFS file-system description object
883  * @old_pnode: pnode copied
884  * @new_pnode: pnode copy
885  *
886  * During commit it is sometimes necessary to copy a pnode
887  * (see dirty_cow_pnode).  When that happens, references in
888  * category lists and heaps must be replaced.  This function does that.
889  */
890 static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
891 			 struct ubifs_pnode *new_pnode)
892 {
893 	int i;
894 
895 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
896 		if (!new_pnode->lprops[i].lnum)
897 			return;
898 		ubifs_replace_cat(c, &old_pnode->lprops[i],
899 				  &new_pnode->lprops[i]);
900 	}
901 }
902 
903 /**
904  * check_lpt_crc - check LPT node crc is correct.
905  * @c: UBIFS file-system description object
906  * @buf: buffer containing node
907  * @len: length of node
908  *
909  * This function returns %0 on success and a negative error code on failure.
910  */
911 static int check_lpt_crc(void *buf, int len)
912 {
913 	int pos = 0;
914 	uint8_t *addr = buf;
915 	uint16_t crc, calc_crc;
916 
917 	crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
918 	calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
919 			 len - UBIFS_LPT_CRC_BYTES);
920 	if (crc != calc_crc) {
921 		ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
922 			  calc_crc);
923 		dump_stack();
924 		return -EINVAL;
925 	}
926 	return 0;
927 }
928 
929 /**
930  * check_lpt_type - check LPT node type is correct.
931  * @c: UBIFS file-system description object
932  * @addr: address of type bit field is passed and returned updated here
933  * @pos: position of type bit field is passed and returned updated here
934  * @type: expected type
935  *
936  * This function returns %0 on success and a negative error code on failure.
937  */
938 static int check_lpt_type(uint8_t **addr, int *pos, int type)
939 {
940 	int node_type;
941 
942 	node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
943 	if (node_type != type) {
944 		ubifs_err("invalid type (%d) in LPT node type %d", node_type,
945 			  type);
946 		dump_stack();
947 		return -EINVAL;
948 	}
949 	return 0;
950 }
951 
952 /**
953  * unpack_pnode - unpack a pnode.
954  * @c: UBIFS file-system description object
955  * @buf: buffer containing packed pnode to unpack
956  * @pnode: pnode structure to fill
957  *
958  * This function returns %0 on success and a negative error code on failure.
959  */
960 static int unpack_pnode(const struct ubifs_info *c, void *buf,
961 			struct ubifs_pnode *pnode)
962 {
963 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
964 	int i, pos = 0, err;
965 
966 	err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
967 	if (err)
968 		return err;
969 	if (c->big_lpt)
970 		pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
971 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
972 		struct ubifs_lprops * const lprops = &pnode->lprops[i];
973 
974 		lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
975 		lprops->free <<= 3;
976 		lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
977 		lprops->dirty <<= 3;
978 
979 		if (ubifs_unpack_bits(&addr, &pos, 1))
980 			lprops->flags = LPROPS_INDEX;
981 		else
982 			lprops->flags = 0;
983 		lprops->flags |= ubifs_categorize_lprops(c, lprops);
984 	}
985 	err = check_lpt_crc(buf, c->pnode_sz);
986 	return err;
987 }
988 
989 /**
990  * ubifs_unpack_nnode - unpack a nnode.
991  * @c: UBIFS file-system description object
992  * @buf: buffer containing packed nnode to unpack
993  * @nnode: nnode structure to fill
994  *
995  * This function returns %0 on success and a negative error code on failure.
996  */
997 int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
998 		       struct ubifs_nnode *nnode)
999 {
1000 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1001 	int i, pos = 0, err;
1002 
1003 	err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
1004 	if (err)
1005 		return err;
1006 	if (c->big_lpt)
1007 		nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1008 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1009 		int lnum;
1010 
1011 		lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
1012 		       c->lpt_first;
1013 		if (lnum == c->lpt_last + 1)
1014 			lnum = 0;
1015 		nnode->nbranch[i].lnum = lnum;
1016 		nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
1017 						     c->lpt_offs_bits);
1018 	}
1019 	err = check_lpt_crc(buf, c->nnode_sz);
1020 	return err;
1021 }
1022 
1023 /**
1024  * unpack_ltab - unpack the LPT's own lprops table.
1025  * @c: UBIFS file-system description object
1026  * @buf: buffer from which to unpack
1027  *
1028  * This function returns %0 on success and a negative error code on failure.
1029  */
1030 static int unpack_ltab(const struct ubifs_info *c, void *buf)
1031 {
1032 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1033 	int i, pos = 0, err;
1034 
1035 	err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
1036 	if (err)
1037 		return err;
1038 	for (i = 0; i < c->lpt_lebs; i++) {
1039 		int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1040 		int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1041 
1042 		if (free < 0 || free > c->leb_size || dirty < 0 ||
1043 		    dirty > c->leb_size || free + dirty > c->leb_size)
1044 			return -EINVAL;
1045 
1046 		c->ltab[i].free = free;
1047 		c->ltab[i].dirty = dirty;
1048 		c->ltab[i].tgc = 0;
1049 		c->ltab[i].cmt = 0;
1050 	}
1051 	err = check_lpt_crc(buf, c->ltab_sz);
1052 	return err;
1053 }
1054 
1055 #ifndef __UBOOT__
1056 /**
1057  * unpack_lsave - unpack the LPT's save table.
1058  * @c: UBIFS file-system description object
1059  * @buf: buffer from which to unpack
1060  *
1061  * This function returns %0 on success and a negative error code on failure.
1062  */
1063 static int unpack_lsave(const struct ubifs_info *c, void *buf)
1064 {
1065 	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1066 	int i, pos = 0, err;
1067 
1068 	err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE);
1069 	if (err)
1070 		return err;
1071 	for (i = 0; i < c->lsave_cnt; i++) {
1072 		int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
1073 
1074 		if (lnum < c->main_first || lnum >= c->leb_cnt)
1075 			return -EINVAL;
1076 		c->lsave[i] = lnum;
1077 	}
1078 	err = check_lpt_crc(buf, c->lsave_sz);
1079 	return err;
1080 }
1081 #endif
1082 
1083 /**
1084  * validate_nnode - validate a nnode.
1085  * @c: UBIFS file-system description object
1086  * @nnode: nnode to validate
1087  * @parent: parent nnode (or NULL for the root nnode)
1088  * @iip: index in parent
1089  *
1090  * This function returns %0 on success and a negative error code on failure.
1091  */
1092 static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1093 			  struct ubifs_nnode *parent, int iip)
1094 {
1095 	int i, lvl, max_offs;
1096 
1097 	if (c->big_lpt) {
1098 		int num = calc_nnode_num_from_parent(c, parent, iip);
1099 
1100 		if (nnode->num != num)
1101 			return -EINVAL;
1102 	}
1103 	lvl = parent ? parent->level - 1 : c->lpt_hght;
1104 	if (lvl < 1)
1105 		return -EINVAL;
1106 	if (lvl == 1)
1107 		max_offs = c->leb_size - c->pnode_sz;
1108 	else
1109 		max_offs = c->leb_size - c->nnode_sz;
1110 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1111 		int lnum = nnode->nbranch[i].lnum;
1112 		int offs = nnode->nbranch[i].offs;
1113 
1114 		if (lnum == 0) {
1115 			if (offs != 0)
1116 				return -EINVAL;
1117 			continue;
1118 		}
1119 		if (lnum < c->lpt_first || lnum > c->lpt_last)
1120 			return -EINVAL;
1121 		if (offs < 0 || offs > max_offs)
1122 			return -EINVAL;
1123 	}
1124 	return 0;
1125 }
1126 
1127 /**
1128  * validate_pnode - validate a pnode.
1129  * @c: UBIFS file-system description object
1130  * @pnode: pnode to validate
1131  * @parent: parent nnode
1132  * @iip: index in parent
1133  *
1134  * This function returns %0 on success and a negative error code on failure.
1135  */
1136 static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1137 			  struct ubifs_nnode *parent, int iip)
1138 {
1139 	int i;
1140 
1141 	if (c->big_lpt) {
1142 		int num = calc_pnode_num_from_parent(c, parent, iip);
1143 
1144 		if (pnode->num != num)
1145 			return -EINVAL;
1146 	}
1147 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1148 		int free = pnode->lprops[i].free;
1149 		int dirty = pnode->lprops[i].dirty;
1150 
1151 		if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1152 		    (free & 7))
1153 			return -EINVAL;
1154 		if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1155 			return -EINVAL;
1156 		if (dirty + free > c->leb_size)
1157 			return -EINVAL;
1158 	}
1159 	return 0;
1160 }
1161 
1162 /**
1163  * set_pnode_lnum - set LEB numbers on a pnode.
1164  * @c: UBIFS file-system description object
1165  * @pnode: pnode to update
1166  *
1167  * This function calculates the LEB numbers for the LEB properties it contains
1168  * based on the pnode number.
1169  */
1170 static void set_pnode_lnum(const struct ubifs_info *c,
1171 			   struct ubifs_pnode *pnode)
1172 {
1173 	int i, lnum;
1174 
1175 	lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1176 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1177 		if (lnum >= c->leb_cnt)
1178 			return;
1179 		pnode->lprops[i].lnum = lnum++;
1180 	}
1181 }
1182 
1183 /**
1184  * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1185  * @c: UBIFS file-system description object
1186  * @parent: parent nnode (or NULL for the root)
1187  * @iip: index in parent
1188  *
1189  * This function returns %0 on success and a negative error code on failure.
1190  */
1191 int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1192 {
1193 	struct ubifs_nbranch *branch = NULL;
1194 	struct ubifs_nnode *nnode = NULL;
1195 	void *buf = c->lpt_nod_buf;
1196 	int err, lnum, offs;
1197 
1198 	if (parent) {
1199 		branch = &parent->nbranch[iip];
1200 		lnum = branch->lnum;
1201 		offs = branch->offs;
1202 	} else {
1203 		lnum = c->lpt_lnum;
1204 		offs = c->lpt_offs;
1205 	}
1206 	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1207 	if (!nnode) {
1208 		err = -ENOMEM;
1209 		goto out;
1210 	}
1211 	if (lnum == 0) {
1212 		/*
1213 		 * This nnode was not written which just means that the LEB
1214 		 * properties in the subtree below it describe empty LEBs. We
1215 		 * make the nnode as though we had read it, which in fact means
1216 		 * doing almost nothing.
1217 		 */
1218 		if (c->big_lpt)
1219 			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1220 	} else {
1221 		err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1);
1222 		if (err)
1223 			goto out;
1224 		err = ubifs_unpack_nnode(c, buf, nnode);
1225 		if (err)
1226 			goto out;
1227 	}
1228 	err = validate_nnode(c, nnode, parent, iip);
1229 	if (err)
1230 		goto out;
1231 	if (!c->big_lpt)
1232 		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1233 	if (parent) {
1234 		branch->nnode = nnode;
1235 		nnode->level = parent->level - 1;
1236 	} else {
1237 		c->nroot = nnode;
1238 		nnode->level = c->lpt_hght;
1239 	}
1240 	nnode->parent = parent;
1241 	nnode->iip = iip;
1242 	return 0;
1243 
1244 out:
1245 	ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
1246 	dump_stack();
1247 	kfree(nnode);
1248 	return err;
1249 }
1250 
1251 /**
1252  * read_pnode - read a pnode from flash and link it to the tree in memory.
1253  * @c: UBIFS file-system description object
1254  * @parent: parent nnode
1255  * @iip: index in parent
1256  *
1257  * This function returns %0 on success and a negative error code on failure.
1258  */
1259 static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1260 {
1261 	struct ubifs_nbranch *branch;
1262 	struct ubifs_pnode *pnode = NULL;
1263 	void *buf = c->lpt_nod_buf;
1264 	int err, lnum, offs;
1265 
1266 	branch = &parent->nbranch[iip];
1267 	lnum = branch->lnum;
1268 	offs = branch->offs;
1269 	pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1270 	if (!pnode)
1271 		return -ENOMEM;
1272 
1273 	if (lnum == 0) {
1274 		/*
1275 		 * This pnode was not written which just means that the LEB
1276 		 * properties in it describe empty LEBs. We make the pnode as
1277 		 * though we had read it.
1278 		 */
1279 		int i;
1280 
1281 		if (c->big_lpt)
1282 			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1283 		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1284 			struct ubifs_lprops * const lprops = &pnode->lprops[i];
1285 
1286 			lprops->free = c->leb_size;
1287 			lprops->flags = ubifs_categorize_lprops(c, lprops);
1288 		}
1289 	} else {
1290 		err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1);
1291 		if (err)
1292 			goto out;
1293 		err = unpack_pnode(c, buf, pnode);
1294 		if (err)
1295 			goto out;
1296 	}
1297 	err = validate_pnode(c, pnode, parent, iip);
1298 	if (err)
1299 		goto out;
1300 	if (!c->big_lpt)
1301 		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1302 	branch->pnode = pnode;
1303 	pnode->parent = parent;
1304 	pnode->iip = iip;
1305 	set_pnode_lnum(c, pnode);
1306 	c->pnodes_have += 1;
1307 	return 0;
1308 
1309 out:
1310 	ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
1311 	ubifs_dump_pnode(c, pnode, parent, iip);
1312 	dump_stack();
1313 	ubifs_err("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1314 	kfree(pnode);
1315 	return err;
1316 }
1317 
1318 /**
1319  * read_ltab - read LPT's own lprops table.
1320  * @c: UBIFS file-system description object
1321  *
1322  * This function returns %0 on success and a negative error code on failure.
1323  */
1324 static int read_ltab(struct ubifs_info *c)
1325 {
1326 	int err;
1327 	void *buf;
1328 
1329 	buf = vmalloc(c->ltab_sz);
1330 	if (!buf)
1331 		return -ENOMEM;
1332 	err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1);
1333 	if (err)
1334 		goto out;
1335 	err = unpack_ltab(c, buf);
1336 out:
1337 	vfree(buf);
1338 	return err;
1339 }
1340 
1341 #ifndef __UBOOT__
1342 /**
1343  * read_lsave - read LPT's save table.
1344  * @c: UBIFS file-system description object
1345  *
1346  * This function returns %0 on success and a negative error code on failure.
1347  */
1348 static int read_lsave(struct ubifs_info *c)
1349 {
1350 	int err, i;
1351 	void *buf;
1352 
1353 	buf = vmalloc(c->lsave_sz);
1354 	if (!buf)
1355 		return -ENOMEM;
1356 	err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs,
1357 			     c->lsave_sz, 1);
1358 	if (err)
1359 		goto out;
1360 	err = unpack_lsave(c, buf);
1361 	if (err)
1362 		goto out;
1363 	for (i = 0; i < c->lsave_cnt; i++) {
1364 		int lnum = c->lsave[i];
1365 		struct ubifs_lprops *lprops;
1366 
1367 		/*
1368 		 * Due to automatic resizing, the values in the lsave table
1369 		 * could be beyond the volume size - just ignore them.
1370 		 */
1371 		if (lnum >= c->leb_cnt)
1372 			continue;
1373 		lprops = ubifs_lpt_lookup(c, lnum);
1374 		if (IS_ERR(lprops)) {
1375 			err = PTR_ERR(lprops);
1376 			goto out;
1377 		}
1378 	}
1379 out:
1380 	vfree(buf);
1381 	return err;
1382 }
1383 #endif
1384 
1385 /**
1386  * ubifs_get_nnode - get a nnode.
1387  * @c: UBIFS file-system description object
1388  * @parent: parent nnode (or NULL for the root)
1389  * @iip: index in parent
1390  *
1391  * This function returns a pointer to the nnode on success or a negative error
1392  * code on failure.
1393  */
1394 struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1395 				    struct ubifs_nnode *parent, int iip)
1396 {
1397 	struct ubifs_nbranch *branch;
1398 	struct ubifs_nnode *nnode;
1399 	int err;
1400 
1401 	branch = &parent->nbranch[iip];
1402 	nnode = branch->nnode;
1403 	if (nnode)
1404 		return nnode;
1405 	err = ubifs_read_nnode(c, parent, iip);
1406 	if (err)
1407 		return ERR_PTR(err);
1408 	return branch->nnode;
1409 }
1410 
1411 /**
1412  * ubifs_get_pnode - get a pnode.
1413  * @c: UBIFS file-system description object
1414  * @parent: parent nnode
1415  * @iip: index in parent
1416  *
1417  * This function returns a pointer to the pnode on success or a negative error
1418  * code on failure.
1419  */
1420 struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1421 				    struct ubifs_nnode *parent, int iip)
1422 {
1423 	struct ubifs_nbranch *branch;
1424 	struct ubifs_pnode *pnode;
1425 	int err;
1426 
1427 	branch = &parent->nbranch[iip];
1428 	pnode = branch->pnode;
1429 	if (pnode)
1430 		return pnode;
1431 	err = read_pnode(c, parent, iip);
1432 	if (err)
1433 		return ERR_PTR(err);
1434 	update_cats(c, branch->pnode);
1435 	return branch->pnode;
1436 }
1437 
1438 /**
1439  * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1440  * @c: UBIFS file-system description object
1441  * @lnum: LEB number to lookup
1442  *
1443  * This function returns a pointer to the LEB properties on success or a
1444  * negative error code on failure.
1445  */
1446 struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1447 {
1448 	int err, i, h, iip, shft;
1449 	struct ubifs_nnode *nnode;
1450 	struct ubifs_pnode *pnode;
1451 
1452 	if (!c->nroot) {
1453 		err = ubifs_read_nnode(c, NULL, 0);
1454 		if (err)
1455 			return ERR_PTR(err);
1456 	}
1457 	nnode = c->nroot;
1458 	i = lnum - c->main_first;
1459 	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1460 	for (h = 1; h < c->lpt_hght; h++) {
1461 		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1462 		shft -= UBIFS_LPT_FANOUT_SHIFT;
1463 		nnode = ubifs_get_nnode(c, nnode, iip);
1464 		if (IS_ERR(nnode))
1465 			return ERR_CAST(nnode);
1466 	}
1467 	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1468 	shft -= UBIFS_LPT_FANOUT_SHIFT;
1469 	pnode = ubifs_get_pnode(c, nnode, iip);
1470 	if (IS_ERR(pnode))
1471 		return ERR_CAST(pnode);
1472 	iip = (i & (UBIFS_LPT_FANOUT - 1));
1473 	dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1474 	       pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1475 	       pnode->lprops[iip].flags);
1476 	return &pnode->lprops[iip];
1477 }
1478 
1479 /**
1480  * dirty_cow_nnode - ensure a nnode is not being committed.
1481  * @c: UBIFS file-system description object
1482  * @nnode: nnode to check
1483  *
1484  * Returns dirtied nnode on success or negative error code on failure.
1485  */
1486 static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1487 					   struct ubifs_nnode *nnode)
1488 {
1489 	struct ubifs_nnode *n;
1490 	int i;
1491 
1492 	if (!test_bit(COW_CNODE, &nnode->flags)) {
1493 		/* nnode is not being committed */
1494 		if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1495 			c->dirty_nn_cnt += 1;
1496 			ubifs_add_nnode_dirt(c, nnode);
1497 		}
1498 		return nnode;
1499 	}
1500 
1501 	/* nnode is being committed, so copy it */
1502 	n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1503 	if (unlikely(!n))
1504 		return ERR_PTR(-ENOMEM);
1505 
1506 	memcpy(n, nnode, sizeof(struct ubifs_nnode));
1507 	n->cnext = NULL;
1508 	__set_bit(DIRTY_CNODE, &n->flags);
1509 	__clear_bit(COW_CNODE, &n->flags);
1510 
1511 	/* The children now have new parent */
1512 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1513 		struct ubifs_nbranch *branch = &n->nbranch[i];
1514 
1515 		if (branch->cnode)
1516 			branch->cnode->parent = n;
1517 	}
1518 
1519 	ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
1520 	__set_bit(OBSOLETE_CNODE, &nnode->flags);
1521 
1522 	c->dirty_nn_cnt += 1;
1523 	ubifs_add_nnode_dirt(c, nnode);
1524 	if (nnode->parent)
1525 		nnode->parent->nbranch[n->iip].nnode = n;
1526 	else
1527 		c->nroot = n;
1528 	return n;
1529 }
1530 
1531 /**
1532  * dirty_cow_pnode - ensure a pnode is not being committed.
1533  * @c: UBIFS file-system description object
1534  * @pnode: pnode to check
1535  *
1536  * Returns dirtied pnode on success or negative error code on failure.
1537  */
1538 static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1539 					   struct ubifs_pnode *pnode)
1540 {
1541 	struct ubifs_pnode *p;
1542 
1543 	if (!test_bit(COW_CNODE, &pnode->flags)) {
1544 		/* pnode is not being committed */
1545 		if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1546 			c->dirty_pn_cnt += 1;
1547 			add_pnode_dirt(c, pnode);
1548 		}
1549 		return pnode;
1550 	}
1551 
1552 	/* pnode is being committed, so copy it */
1553 	p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1554 	if (unlikely(!p))
1555 		return ERR_PTR(-ENOMEM);
1556 
1557 	memcpy(p, pnode, sizeof(struct ubifs_pnode));
1558 	p->cnext = NULL;
1559 	__set_bit(DIRTY_CNODE, &p->flags);
1560 	__clear_bit(COW_CNODE, &p->flags);
1561 	replace_cats(c, pnode, p);
1562 
1563 	ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1564 	__set_bit(OBSOLETE_CNODE, &pnode->flags);
1565 
1566 	c->dirty_pn_cnt += 1;
1567 	add_pnode_dirt(c, pnode);
1568 	pnode->parent->nbranch[p->iip].pnode = p;
1569 	return p;
1570 }
1571 
1572 /**
1573  * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1574  * @c: UBIFS file-system description object
1575  * @lnum: LEB number to lookup
1576  *
1577  * This function returns a pointer to the LEB properties on success or a
1578  * negative error code on failure.
1579  */
1580 struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1581 {
1582 	int err, i, h, iip, shft;
1583 	struct ubifs_nnode *nnode;
1584 	struct ubifs_pnode *pnode;
1585 
1586 	if (!c->nroot) {
1587 		err = ubifs_read_nnode(c, NULL, 0);
1588 		if (err)
1589 			return ERR_PTR(err);
1590 	}
1591 	nnode = c->nroot;
1592 	nnode = dirty_cow_nnode(c, nnode);
1593 	if (IS_ERR(nnode))
1594 		return ERR_CAST(nnode);
1595 	i = lnum - c->main_first;
1596 	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1597 	for (h = 1; h < c->lpt_hght; h++) {
1598 		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1599 		shft -= UBIFS_LPT_FANOUT_SHIFT;
1600 		nnode = ubifs_get_nnode(c, nnode, iip);
1601 		if (IS_ERR(nnode))
1602 			return ERR_CAST(nnode);
1603 		nnode = dirty_cow_nnode(c, nnode);
1604 		if (IS_ERR(nnode))
1605 			return ERR_CAST(nnode);
1606 	}
1607 	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1608 	shft -= UBIFS_LPT_FANOUT_SHIFT;
1609 	pnode = ubifs_get_pnode(c, nnode, iip);
1610 	if (IS_ERR(pnode))
1611 		return ERR_CAST(pnode);
1612 	pnode = dirty_cow_pnode(c, pnode);
1613 	if (IS_ERR(pnode))
1614 		return ERR_CAST(pnode);
1615 	iip = (i & (UBIFS_LPT_FANOUT - 1));
1616 	dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1617 	       pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1618 	       pnode->lprops[iip].flags);
1619 	ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1620 	return &pnode->lprops[iip];
1621 }
1622 
1623 /**
1624  * lpt_init_rd - initialize the LPT for reading.
1625  * @c: UBIFS file-system description object
1626  *
1627  * This function returns %0 on success and a negative error code on failure.
1628  */
1629 static int lpt_init_rd(struct ubifs_info *c)
1630 {
1631 	int err, i;
1632 
1633 	c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1634 	if (!c->ltab)
1635 		return -ENOMEM;
1636 
1637 	i = max_t(int, c->nnode_sz, c->pnode_sz);
1638 	c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1639 	if (!c->lpt_nod_buf)
1640 		return -ENOMEM;
1641 
1642 	for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1643 		c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
1644 					     GFP_KERNEL);
1645 		if (!c->lpt_heap[i].arr)
1646 			return -ENOMEM;
1647 		c->lpt_heap[i].cnt = 0;
1648 		c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1649 	}
1650 
1651 	c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1652 	if (!c->dirty_idx.arr)
1653 		return -ENOMEM;
1654 	c->dirty_idx.cnt = 0;
1655 	c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1656 
1657 	err = read_ltab(c);
1658 	if (err)
1659 		return err;
1660 
1661 	dbg_lp("space_bits %d", c->space_bits);
1662 	dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1663 	dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1664 	dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1665 	dbg_lp("pcnt_bits %d", c->pcnt_bits);
1666 	dbg_lp("lnum_bits %d", c->lnum_bits);
1667 	dbg_lp("pnode_sz %d", c->pnode_sz);
1668 	dbg_lp("nnode_sz %d", c->nnode_sz);
1669 	dbg_lp("ltab_sz %d", c->ltab_sz);
1670 	dbg_lp("lsave_sz %d", c->lsave_sz);
1671 	dbg_lp("lsave_cnt %d", c->lsave_cnt);
1672 	dbg_lp("lpt_hght %d", c->lpt_hght);
1673 	dbg_lp("big_lpt %d", c->big_lpt);
1674 	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1675 	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1676 	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1677 	if (c->big_lpt)
1678 		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1679 
1680 	return 0;
1681 }
1682 
1683 #ifndef __UBOOT__
1684 /**
1685  * lpt_init_wr - initialize the LPT for writing.
1686  * @c: UBIFS file-system description object
1687  *
1688  * 'lpt_init_rd()' must have been called already.
1689  *
1690  * This function returns %0 on success and a negative error code on failure.
1691  */
1692 static int lpt_init_wr(struct ubifs_info *c)
1693 {
1694 	int err, i;
1695 
1696 	c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1697 	if (!c->ltab_cmt)
1698 		return -ENOMEM;
1699 
1700 	c->lpt_buf = vmalloc(c->leb_size);
1701 	if (!c->lpt_buf)
1702 		return -ENOMEM;
1703 
1704 	if (c->big_lpt) {
1705 		c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1706 		if (!c->lsave)
1707 			return -ENOMEM;
1708 		err = read_lsave(c);
1709 		if (err)
1710 			return err;
1711 	}
1712 
1713 	for (i = 0; i < c->lpt_lebs; i++)
1714 		if (c->ltab[i].free == c->leb_size) {
1715 			err = ubifs_leb_unmap(c, i + c->lpt_first);
1716 			if (err)
1717 				return err;
1718 		}
1719 
1720 	return 0;
1721 }
1722 #endif
1723 
1724 /**
1725  * ubifs_lpt_init - initialize the LPT.
1726  * @c: UBIFS file-system description object
1727  * @rd: whether to initialize lpt for reading
1728  * @wr: whether to initialize lpt for writing
1729  *
1730  * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1731  * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1732  * true.
1733  *
1734  * This function returns %0 on success and a negative error code on failure.
1735  */
1736 int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1737 {
1738 	int err;
1739 
1740 	if (rd) {
1741 		err = lpt_init_rd(c);
1742 		if (err)
1743 			goto out_err;
1744 	}
1745 
1746 #ifndef __UBOOT__
1747 	if (wr) {
1748 		err = lpt_init_wr(c);
1749 		if (err)
1750 			goto out_err;
1751 	}
1752 #endif
1753 
1754 	return 0;
1755 
1756 out_err:
1757 #ifndef __UBOOT__
1758 	if (wr)
1759 		ubifs_lpt_free(c, 1);
1760 #endif
1761 	if (rd)
1762 		ubifs_lpt_free(c, 0);
1763 	return err;
1764 }
1765 
1766 /**
1767  * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1768  * @nnode: where to keep a nnode
1769  * @pnode: where to keep a pnode
1770  * @cnode: where to keep a cnode
1771  * @in_tree: is the node in the tree in memory
1772  * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1773  * the tree
1774  * @ptr.pnode: ditto for pnode
1775  * @ptr.cnode: ditto for cnode
1776  */
1777 struct lpt_scan_node {
1778 	union {
1779 		struct ubifs_nnode nnode;
1780 		struct ubifs_pnode pnode;
1781 		struct ubifs_cnode cnode;
1782 	};
1783 	int in_tree;
1784 	union {
1785 		struct ubifs_nnode *nnode;
1786 		struct ubifs_pnode *pnode;
1787 		struct ubifs_cnode *cnode;
1788 	} ptr;
1789 };
1790 
1791 /**
1792  * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1793  * @c: the UBIFS file-system description object
1794  * @path: where to put the nnode
1795  * @parent: parent of the nnode
1796  * @iip: index in parent of the nnode
1797  *
1798  * This function returns a pointer to the nnode on success or a negative error
1799  * code on failure.
1800  */
1801 static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1802 					  struct lpt_scan_node *path,
1803 					  struct ubifs_nnode *parent, int iip)
1804 {
1805 	struct ubifs_nbranch *branch;
1806 	struct ubifs_nnode *nnode;
1807 	void *buf = c->lpt_nod_buf;
1808 	int err;
1809 
1810 	branch = &parent->nbranch[iip];
1811 	nnode = branch->nnode;
1812 	if (nnode) {
1813 		path->in_tree = 1;
1814 		path->ptr.nnode = nnode;
1815 		return nnode;
1816 	}
1817 	nnode = &path->nnode;
1818 	path->in_tree = 0;
1819 	path->ptr.nnode = nnode;
1820 	memset(nnode, 0, sizeof(struct ubifs_nnode));
1821 	if (branch->lnum == 0) {
1822 		/*
1823 		 * This nnode was not written which just means that the LEB
1824 		 * properties in the subtree below it describe empty LEBs. We
1825 		 * make the nnode as though we had read it, which in fact means
1826 		 * doing almost nothing.
1827 		 */
1828 		if (c->big_lpt)
1829 			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1830 	} else {
1831 		err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1832 				     c->nnode_sz, 1);
1833 		if (err)
1834 			return ERR_PTR(err);
1835 		err = ubifs_unpack_nnode(c, buf, nnode);
1836 		if (err)
1837 			return ERR_PTR(err);
1838 	}
1839 	err = validate_nnode(c, nnode, parent, iip);
1840 	if (err)
1841 		return ERR_PTR(err);
1842 	if (!c->big_lpt)
1843 		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1844 	nnode->level = parent->level - 1;
1845 	nnode->parent = parent;
1846 	nnode->iip = iip;
1847 	return nnode;
1848 }
1849 
1850 /**
1851  * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1852  * @c: the UBIFS file-system description object
1853  * @path: where to put the pnode
1854  * @parent: parent of the pnode
1855  * @iip: index in parent of the pnode
1856  *
1857  * This function returns a pointer to the pnode on success or a negative error
1858  * code on failure.
1859  */
1860 static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1861 					  struct lpt_scan_node *path,
1862 					  struct ubifs_nnode *parent, int iip)
1863 {
1864 	struct ubifs_nbranch *branch;
1865 	struct ubifs_pnode *pnode;
1866 	void *buf = c->lpt_nod_buf;
1867 	int err;
1868 
1869 	branch = &parent->nbranch[iip];
1870 	pnode = branch->pnode;
1871 	if (pnode) {
1872 		path->in_tree = 1;
1873 		path->ptr.pnode = pnode;
1874 		return pnode;
1875 	}
1876 	pnode = &path->pnode;
1877 	path->in_tree = 0;
1878 	path->ptr.pnode = pnode;
1879 	memset(pnode, 0, sizeof(struct ubifs_pnode));
1880 	if (branch->lnum == 0) {
1881 		/*
1882 		 * This pnode was not written which just means that the LEB
1883 		 * properties in it describe empty LEBs. We make the pnode as
1884 		 * though we had read it.
1885 		 */
1886 		int i;
1887 
1888 		if (c->big_lpt)
1889 			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1890 		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1891 			struct ubifs_lprops * const lprops = &pnode->lprops[i];
1892 
1893 			lprops->free = c->leb_size;
1894 			lprops->flags = ubifs_categorize_lprops(c, lprops);
1895 		}
1896 	} else {
1897 		ubifs_assert(branch->lnum >= c->lpt_first &&
1898 			     branch->lnum <= c->lpt_last);
1899 		ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1900 		err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1901 				     c->pnode_sz, 1);
1902 		if (err)
1903 			return ERR_PTR(err);
1904 		err = unpack_pnode(c, buf, pnode);
1905 		if (err)
1906 			return ERR_PTR(err);
1907 	}
1908 	err = validate_pnode(c, pnode, parent, iip);
1909 	if (err)
1910 		return ERR_PTR(err);
1911 	if (!c->big_lpt)
1912 		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1913 	pnode->parent = parent;
1914 	pnode->iip = iip;
1915 	set_pnode_lnum(c, pnode);
1916 	return pnode;
1917 }
1918 
1919 /**
1920  * ubifs_lpt_scan_nolock - scan the LPT.
1921  * @c: the UBIFS file-system description object
1922  * @start_lnum: LEB number from which to start scanning
1923  * @end_lnum: LEB number at which to stop scanning
1924  * @scan_cb: callback function called for each lprops
1925  * @data: data to be passed to the callback function
1926  *
1927  * This function returns %0 on success and a negative error code on failure.
1928  */
1929 int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1930 			  ubifs_lpt_scan_callback scan_cb, void *data)
1931 {
1932 	int err = 0, i, h, iip, shft;
1933 	struct ubifs_nnode *nnode;
1934 	struct ubifs_pnode *pnode;
1935 	struct lpt_scan_node *path;
1936 
1937 	if (start_lnum == -1) {
1938 		start_lnum = end_lnum + 1;
1939 		if (start_lnum >= c->leb_cnt)
1940 			start_lnum = c->main_first;
1941 	}
1942 
1943 	ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1944 	ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1945 
1946 	if (!c->nroot) {
1947 		err = ubifs_read_nnode(c, NULL, 0);
1948 		if (err)
1949 			return err;
1950 	}
1951 
1952 	path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1953 		       GFP_NOFS);
1954 	if (!path)
1955 		return -ENOMEM;
1956 
1957 	path[0].ptr.nnode = c->nroot;
1958 	path[0].in_tree = 1;
1959 again:
1960 	/* Descend to the pnode containing start_lnum */
1961 	nnode = c->nroot;
1962 	i = start_lnum - c->main_first;
1963 	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1964 	for (h = 1; h < c->lpt_hght; h++) {
1965 		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1966 		shft -= UBIFS_LPT_FANOUT_SHIFT;
1967 		nnode = scan_get_nnode(c, path + h, nnode, iip);
1968 		if (IS_ERR(nnode)) {
1969 			err = PTR_ERR(nnode);
1970 			goto out;
1971 		}
1972 	}
1973 	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1974 	shft -= UBIFS_LPT_FANOUT_SHIFT;
1975 	pnode = scan_get_pnode(c, path + h, nnode, iip);
1976 	if (IS_ERR(pnode)) {
1977 		err = PTR_ERR(pnode);
1978 		goto out;
1979 	}
1980 	iip = (i & (UBIFS_LPT_FANOUT - 1));
1981 
1982 	/* Loop for each lprops */
1983 	while (1) {
1984 		struct ubifs_lprops *lprops = &pnode->lprops[iip];
1985 		int ret, lnum = lprops->lnum;
1986 
1987 		ret = scan_cb(c, lprops, path[h].in_tree, data);
1988 		if (ret < 0) {
1989 			err = ret;
1990 			goto out;
1991 		}
1992 		if (ret & LPT_SCAN_ADD) {
1993 			/* Add all the nodes in path to the tree in memory */
1994 			for (h = 1; h < c->lpt_hght; h++) {
1995 				const size_t sz = sizeof(struct ubifs_nnode);
1996 				struct ubifs_nnode *parent;
1997 
1998 				if (path[h].in_tree)
1999 					continue;
2000 				nnode = kmemdup(&path[h].nnode, sz, GFP_NOFS);
2001 				if (!nnode) {
2002 					err = -ENOMEM;
2003 					goto out;
2004 				}
2005 				parent = nnode->parent;
2006 				parent->nbranch[nnode->iip].nnode = nnode;
2007 				path[h].ptr.nnode = nnode;
2008 				path[h].in_tree = 1;
2009 				path[h + 1].cnode.parent = nnode;
2010 			}
2011 			if (path[h].in_tree)
2012 				ubifs_ensure_cat(c, lprops);
2013 			else {
2014 				const size_t sz = sizeof(struct ubifs_pnode);
2015 				struct ubifs_nnode *parent;
2016 
2017 				pnode = kmemdup(&path[h].pnode, sz, GFP_NOFS);
2018 				if (!pnode) {
2019 					err = -ENOMEM;
2020 					goto out;
2021 				}
2022 				parent = pnode->parent;
2023 				parent->nbranch[pnode->iip].pnode = pnode;
2024 				path[h].ptr.pnode = pnode;
2025 				path[h].in_tree = 1;
2026 				update_cats(c, pnode);
2027 				c->pnodes_have += 1;
2028 			}
2029 			err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2030 						  c->nroot, 0, 0);
2031 			if (err)
2032 				goto out;
2033 			err = dbg_check_cats(c);
2034 			if (err)
2035 				goto out;
2036 		}
2037 		if (ret & LPT_SCAN_STOP) {
2038 			err = 0;
2039 			break;
2040 		}
2041 		/* Get the next lprops */
2042 		if (lnum == end_lnum) {
2043 			/*
2044 			 * We got to the end without finding what we were
2045 			 * looking for
2046 			 */
2047 			err = -ENOSPC;
2048 			goto out;
2049 		}
2050 		if (lnum + 1 >= c->leb_cnt) {
2051 			/* Wrap-around to the beginning */
2052 			start_lnum = c->main_first;
2053 			goto again;
2054 		}
2055 		if (iip + 1 < UBIFS_LPT_FANOUT) {
2056 			/* Next lprops is in the same pnode */
2057 			iip += 1;
2058 			continue;
2059 		}
2060 		/* We need to get the next pnode. Go up until we can go right */
2061 		iip = pnode->iip;
2062 		while (1) {
2063 			h -= 1;
2064 			ubifs_assert(h >= 0);
2065 			nnode = path[h].ptr.nnode;
2066 			if (iip + 1 < UBIFS_LPT_FANOUT)
2067 				break;
2068 			iip = nnode->iip;
2069 		}
2070 		/* Go right */
2071 		iip += 1;
2072 		/* Descend to the pnode */
2073 		h += 1;
2074 		for (; h < c->lpt_hght; h++) {
2075 			nnode = scan_get_nnode(c, path + h, nnode, iip);
2076 			if (IS_ERR(nnode)) {
2077 				err = PTR_ERR(nnode);
2078 				goto out;
2079 			}
2080 			iip = 0;
2081 		}
2082 		pnode = scan_get_pnode(c, path + h, nnode, iip);
2083 		if (IS_ERR(pnode)) {
2084 			err = PTR_ERR(pnode);
2085 			goto out;
2086 		}
2087 		iip = 0;
2088 	}
2089 out:
2090 	kfree(path);
2091 	return err;
2092 }
2093 
2094 /**
2095  * dbg_chk_pnode - check a pnode.
2096  * @c: the UBIFS file-system description object
2097  * @pnode: pnode to check
2098  * @col: pnode column
2099  *
2100  * This function returns %0 on success and a negative error code on failure.
2101  */
2102 static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2103 			 int col)
2104 {
2105 	int i;
2106 
2107 	if (pnode->num != col) {
2108 		ubifs_err("pnode num %d expected %d parent num %d iip %d",
2109 			  pnode->num, col, pnode->parent->num, pnode->iip);
2110 		return -EINVAL;
2111 	}
2112 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2113 		struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2114 		int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2115 			   c->main_first;
2116 		int found, cat = lprops->flags & LPROPS_CAT_MASK;
2117 		struct ubifs_lpt_heap *heap;
2118 		struct list_head *list = NULL;
2119 
2120 		if (lnum >= c->leb_cnt)
2121 			continue;
2122 		if (lprops->lnum != lnum) {
2123 			ubifs_err("bad LEB number %d expected %d",
2124 				  lprops->lnum, lnum);
2125 			return -EINVAL;
2126 		}
2127 		if (lprops->flags & LPROPS_TAKEN) {
2128 			if (cat != LPROPS_UNCAT) {
2129 				ubifs_err("LEB %d taken but not uncat %d",
2130 					  lprops->lnum, cat);
2131 				return -EINVAL;
2132 			}
2133 			continue;
2134 		}
2135 		if (lprops->flags & LPROPS_INDEX) {
2136 			switch (cat) {
2137 			case LPROPS_UNCAT:
2138 			case LPROPS_DIRTY_IDX:
2139 			case LPROPS_FRDI_IDX:
2140 				break;
2141 			default:
2142 				ubifs_err("LEB %d index but cat %d",
2143 					  lprops->lnum, cat);
2144 				return -EINVAL;
2145 			}
2146 		} else {
2147 			switch (cat) {
2148 			case LPROPS_UNCAT:
2149 			case LPROPS_DIRTY:
2150 			case LPROPS_FREE:
2151 			case LPROPS_EMPTY:
2152 			case LPROPS_FREEABLE:
2153 				break;
2154 			default:
2155 				ubifs_err("LEB %d not index but cat %d",
2156 					  lprops->lnum, cat);
2157 				return -EINVAL;
2158 			}
2159 		}
2160 		switch (cat) {
2161 		case LPROPS_UNCAT:
2162 			list = &c->uncat_list;
2163 			break;
2164 		case LPROPS_EMPTY:
2165 			list = &c->empty_list;
2166 			break;
2167 		case LPROPS_FREEABLE:
2168 			list = &c->freeable_list;
2169 			break;
2170 		case LPROPS_FRDI_IDX:
2171 			list = &c->frdi_idx_list;
2172 			break;
2173 		}
2174 		found = 0;
2175 		switch (cat) {
2176 		case LPROPS_DIRTY:
2177 		case LPROPS_DIRTY_IDX:
2178 		case LPROPS_FREE:
2179 			heap = &c->lpt_heap[cat - 1];
2180 			if (lprops->hpos < heap->cnt &&
2181 			    heap->arr[lprops->hpos] == lprops)
2182 				found = 1;
2183 			break;
2184 		case LPROPS_UNCAT:
2185 		case LPROPS_EMPTY:
2186 		case LPROPS_FREEABLE:
2187 		case LPROPS_FRDI_IDX:
2188 			list_for_each_entry(lp, list, list)
2189 				if (lprops == lp) {
2190 					found = 1;
2191 					break;
2192 				}
2193 			break;
2194 		}
2195 		if (!found) {
2196 			ubifs_err("LEB %d cat %d not found in cat heap/list",
2197 				  lprops->lnum, cat);
2198 			return -EINVAL;
2199 		}
2200 		switch (cat) {
2201 		case LPROPS_EMPTY:
2202 			if (lprops->free != c->leb_size) {
2203 				ubifs_err("LEB %d cat %d free %d dirty %d",
2204 					  lprops->lnum, cat, lprops->free,
2205 					  lprops->dirty);
2206 				return -EINVAL;
2207 			}
2208 		case LPROPS_FREEABLE:
2209 		case LPROPS_FRDI_IDX:
2210 			if (lprops->free + lprops->dirty != c->leb_size) {
2211 				ubifs_err("LEB %d cat %d free %d dirty %d",
2212 					  lprops->lnum, cat, lprops->free,
2213 					  lprops->dirty);
2214 				return -EINVAL;
2215 			}
2216 		}
2217 	}
2218 	return 0;
2219 }
2220 
2221 /**
2222  * dbg_check_lpt_nodes - check nnodes and pnodes.
2223  * @c: the UBIFS file-system description object
2224  * @cnode: next cnode (nnode or pnode) to check
2225  * @row: row of cnode (root is zero)
2226  * @col: column of cnode (leftmost is zero)
2227  *
2228  * This function returns %0 on success and a negative error code on failure.
2229  */
2230 int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2231 			int row, int col)
2232 {
2233 	struct ubifs_nnode *nnode, *nn;
2234 	struct ubifs_cnode *cn;
2235 	int num, iip = 0, err;
2236 
2237 	if (!dbg_is_chk_lprops(c))
2238 		return 0;
2239 
2240 	while (cnode) {
2241 		ubifs_assert(row >= 0);
2242 		nnode = cnode->parent;
2243 		if (cnode->level) {
2244 			/* cnode is a nnode */
2245 			num = calc_nnode_num(row, col);
2246 			if (cnode->num != num) {
2247 				ubifs_err("nnode num %d expected %d parent num %d iip %d",
2248 					  cnode->num, num,
2249 					  (nnode ? nnode->num : 0), cnode->iip);
2250 				return -EINVAL;
2251 			}
2252 			nn = (struct ubifs_nnode *)cnode;
2253 			while (iip < UBIFS_LPT_FANOUT) {
2254 				cn = nn->nbranch[iip].cnode;
2255 				if (cn) {
2256 					/* Go down */
2257 					row += 1;
2258 					col <<= UBIFS_LPT_FANOUT_SHIFT;
2259 					col += iip;
2260 					iip = 0;
2261 					cnode = cn;
2262 					break;
2263 				}
2264 				/* Go right */
2265 				iip += 1;
2266 			}
2267 			if (iip < UBIFS_LPT_FANOUT)
2268 				continue;
2269 		} else {
2270 			struct ubifs_pnode *pnode;
2271 
2272 			/* cnode is a pnode */
2273 			pnode = (struct ubifs_pnode *)cnode;
2274 			err = dbg_chk_pnode(c, pnode, col);
2275 			if (err)
2276 				return err;
2277 		}
2278 		/* Go up and to the right */
2279 		row -= 1;
2280 		col >>= UBIFS_LPT_FANOUT_SHIFT;
2281 		iip = cnode->iip + 1;
2282 		cnode = (struct ubifs_cnode *)nnode;
2283 	}
2284 	return 0;
2285 }
2286