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