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