]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ubifs/lpt.c
UBIFS: fix available blocks count
[mirror_ubuntu-artful-kernel.git] / fs / ubifs / lpt.c
CommitLineData
1e51764a
AB
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
45e12d90 39 * selected for garbage collection, which consists of marking the clean nodes in
1e51764a
AB
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 */
56static 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;
73944a6d
AH
112 if (c->big_lpt)
113 c->lpt_sz += c->lsave_sz;
1e51764a
AB
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 */
135int 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;
1e51764a
AB
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 */
174static 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 */
235static 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 */
285uint32_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;
727d2dc0
AH
290 uint32_t uninitialized_var(val);
291 const int bytes = (nrbits + b + 7) >> 3;
1e51764a
AB
292
293 ubifs_assert(nrbits > 0);
294 ubifs_assert(nrbits <= 32);
295 ubifs_assert(*pos >= 0);
296 ubifs_assert(*pos < 8);
297 if (b) {
727d2dc0
AH
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 }
1e51764a
AB
314 val <<= (8 - b);
315 val |= *p >> b;
316 nrbits += b;
727d2dc0
AH
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 }
1e51764a
AB
336 val <<= k;
337 val >>= k;
338 b = nrbits & 7;
727d2dc0 339 p += nrbits >> 3;
1e51764a
AB
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 */
352void 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 */
385void 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 */
417void 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 */
442void 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 */
464void 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 */
481static 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 */
496void 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 */
517static 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 */
534static 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 */
2ba5f7ae 560static int calc_nnode_num_from_parent(const struct ubifs_info *c,
1e51764a
AB
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 */
2ba5f7ae 585static int calc_pnode_num_from_parent(const struct ubifs_info *c,
1e51764a
AB
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 */
610int 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);
856out:
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 */
874static 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 */
898static 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 */
919static 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 */
946static 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 */
2ba5f7ae 968static int unpack_pnode(const struct ubifs_info *c, void *buf,
1e51764a
AB
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/**
2ba5f7ae 998 * ubifs_unpack_nnode - unpack a nnode.
1e51764a
AB
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 */
2ba5f7ae
AB
1005int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1006 struct ubifs_nnode *nnode)
1e51764a
AB
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 */
2ba5f7ae 1038static int unpack_ltab(const struct ubifs_info *c, void *buf)
1e51764a
AB
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 */
2ba5f7ae 1070static int unpack_lsave(const struct ubifs_info *c, void *buf)
1e51764a
AB
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 */
2ba5f7ae 1098static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1e51764a
AB
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 */
2ba5f7ae 1142static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1e51764a
AB
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 */
2ba5f7ae
AB
1176static void set_pnode_lnum(const struct ubifs_info *c,
1177 struct ubifs_pnode *pnode)
1e51764a
AB
1178{
1179 int i, lnum;
1180
1181 lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1182 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1183 if (lnum >= c->leb_cnt)
1184 return;
1185 pnode->lprops[i].lnum = lnum++;
1186 }
1187}
1188
1189/**
1190 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1191 * @c: UBIFS file-system description object
1192 * @parent: parent nnode (or NULL for the root)
1193 * @iip: index in parent
1194 *
1195 * This function returns %0 on success and a negative error code on failure.
1196 */
1197int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1198{
1199 struct ubifs_nbranch *branch = NULL;
1200 struct ubifs_nnode *nnode = NULL;
1201 void *buf = c->lpt_nod_buf;
1202 int err, lnum, offs;
1203
1204 if (parent) {
1205 branch = &parent->nbranch[iip];
1206 lnum = branch->lnum;
1207 offs = branch->offs;
1208 } else {
1209 lnum = c->lpt_lnum;
1210 offs = c->lpt_offs;
1211 }
1212 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1213 if (!nnode) {
1214 err = -ENOMEM;
1215 goto out;
1216 }
1217 if (lnum == 0) {
1218 /*
1219 * This nnode was not written which just means that the LEB
1220 * properties in the subtree below it describe empty LEBs. We
1221 * make the nnode as though we had read it, which in fact means
1222 * doing almost nothing.
1223 */
1224 if (c->big_lpt)
1225 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1226 } else {
1227 err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz);
1228 if (err)
1229 goto out;
2ba5f7ae 1230 err = ubifs_unpack_nnode(c, buf, nnode);
1e51764a
AB
1231 if (err)
1232 goto out;
1233 }
1234 err = validate_nnode(c, nnode, parent, iip);
1235 if (err)
1236 goto out;
1237 if (!c->big_lpt)
1238 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1239 if (parent) {
1240 branch->nnode = nnode;
1241 nnode->level = parent->level - 1;
1242 } else {
1243 c->nroot = nnode;
1244 nnode->level = c->lpt_hght;
1245 }
1246 nnode->parent = parent;
1247 nnode->iip = iip;
1248 return 0;
1249
1250out:
1251 ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
1252 kfree(nnode);
1253 return err;
1254}
1255
1256/**
1257 * read_pnode - read a pnode from flash and link it to the tree in memory.
1258 * @c: UBIFS file-system description object
1259 * @parent: parent nnode
1260 * @iip: index in parent
1261 *
1262 * This function returns %0 on success and a negative error code on failure.
1263 */
1264static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1265{
1266 struct ubifs_nbranch *branch;
1267 struct ubifs_pnode *pnode = NULL;
1268 void *buf = c->lpt_nod_buf;
1269 int err, lnum, offs;
1270
1271 branch = &parent->nbranch[iip];
1272 lnum = branch->lnum;
1273 offs = branch->offs;
1274 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1275 if (!pnode) {
1276 err = -ENOMEM;
1277 goto out;
1278 }
1279 if (lnum == 0) {
1280 /*
1281 * This pnode was not written which just means that the LEB
1282 * properties in it describe empty LEBs. We make the pnode as
1283 * though we had read it.
1284 */
1285 int i;
1286
1287 if (c->big_lpt)
1288 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1289 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1290 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1291
1292 lprops->free = c->leb_size;
1293 lprops->flags = ubifs_categorize_lprops(c, lprops);
1294 }
1295 } else {
1296 err = ubi_read(c->ubi, lnum, buf, offs, c->pnode_sz);
1297 if (err)
1298 goto out;
1299 err = unpack_pnode(c, buf, pnode);
1300 if (err)
1301 goto out;
1302 }
1303 err = validate_pnode(c, pnode, parent, iip);
1304 if (err)
1305 goto out;
1306 if (!c->big_lpt)
1307 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1308 branch->pnode = pnode;
1309 pnode->parent = parent;
1310 pnode->iip = iip;
1311 set_pnode_lnum(c, pnode);
1312 c->pnodes_have += 1;
1313 return 0;
1314
1315out:
1316 ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
1317 dbg_dump_pnode(c, pnode, parent, iip);
1318 dbg_msg("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1319 kfree(pnode);
1320 return err;
1321}
1322
1323/**
1324 * read_ltab - read LPT's own lprops table.
1325 * @c: UBIFS file-system description object
1326 *
1327 * This function returns %0 on success and a negative error code on failure.
1328 */
1329static int read_ltab(struct ubifs_info *c)
1330{
1331 int err;
1332 void *buf;
1333
1334 buf = vmalloc(c->ltab_sz);
1335 if (!buf)
1336 return -ENOMEM;
1337 err = ubi_read(c->ubi, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz);
1338 if (err)
1339 goto out;
1340 err = unpack_ltab(c, buf);
1341out:
1342 vfree(buf);
1343 return err;
1344}
1345
1346/**
1347 * read_lsave - read LPT's save table.
1348 * @c: UBIFS file-system description object
1349 *
1350 * This function returns %0 on success and a negative error code on failure.
1351 */
1352static int read_lsave(struct ubifs_info *c)
1353{
1354 int err, i;
1355 void *buf;
1356
1357 buf = vmalloc(c->lsave_sz);
1358 if (!buf)
1359 return -ENOMEM;
1360 err = ubi_read(c->ubi, c->lsave_lnum, buf, c->lsave_offs, c->lsave_sz);
1361 if (err)
1362 goto out;
1363 err = unpack_lsave(c, buf);
1364 if (err)
1365 goto out;
1366 for (i = 0; i < c->lsave_cnt; i++) {
1367 int lnum = c->lsave[i];
1368
1369 /*
1370 * Due to automatic resizing, the values in the lsave table
1371 * could be beyond the volume size - just ignore them.
1372 */
1373 if (lnum >= c->leb_cnt)
1374 continue;
1375 ubifs_lpt_lookup(c, lnum);
1376 }
1377out:
1378 vfree(buf);
1379 return err;
1380}
1381
1382/**
1383 * ubifs_get_nnode - get a nnode.
1384 * @c: UBIFS file-system description object
1385 * @parent: parent nnode (or NULL for the root)
1386 * @iip: index in parent
1387 *
1388 * This function returns a pointer to the nnode on success or a negative error
1389 * code on failure.
1390 */
1391struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1392 struct ubifs_nnode *parent, int iip)
1393{
1394 struct ubifs_nbranch *branch;
1395 struct ubifs_nnode *nnode;
1396 int err;
1397
1398 branch = &parent->nbranch[iip];
1399 nnode = branch->nnode;
1400 if (nnode)
1401 return nnode;
1402 err = ubifs_read_nnode(c, parent, iip);
1403 if (err)
1404 return ERR_PTR(err);
1405 return branch->nnode;
1406}
1407
1408/**
1409 * ubifs_get_pnode - get a pnode.
1410 * @c: UBIFS file-system description object
1411 * @parent: parent nnode
1412 * @iip: index in parent
1413 *
1414 * This function returns a pointer to the pnode on success or a negative error
1415 * code on failure.
1416 */
1417struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1418 struct ubifs_nnode *parent, int iip)
1419{
1420 struct ubifs_nbranch *branch;
1421 struct ubifs_pnode *pnode;
1422 int err;
1423
1424 branch = &parent->nbranch[iip];
1425 pnode = branch->pnode;
1426 if (pnode)
1427 return pnode;
1428 err = read_pnode(c, parent, iip);
1429 if (err)
1430 return ERR_PTR(err);
1431 update_cats(c, branch->pnode);
1432 return branch->pnode;
1433}
1434
1435/**
1436 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1437 * @c: UBIFS file-system description object
1438 * @lnum: LEB number to lookup
1439 *
1440 * This function returns a pointer to the LEB properties on success or a
1441 * negative error code on failure.
1442 */
1443struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1444{
1445 int err, i, h, iip, shft;
1446 struct ubifs_nnode *nnode;
1447 struct ubifs_pnode *pnode;
1448
1449 if (!c->nroot) {
1450 err = ubifs_read_nnode(c, NULL, 0);
1451 if (err)
1452 return ERR_PTR(err);
1453 }
1454 nnode = c->nroot;
1455 i = lnum - c->main_first;
1456 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1457 for (h = 1; h < c->lpt_hght; h++) {
1458 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1459 shft -= UBIFS_LPT_FANOUT_SHIFT;
1460 nnode = ubifs_get_nnode(c, nnode, iip);
1461 if (IS_ERR(nnode))
1462 return ERR_PTR(PTR_ERR(nnode));
1463 }
1464 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1465 shft -= UBIFS_LPT_FANOUT_SHIFT;
1466 pnode = ubifs_get_pnode(c, nnode, iip);
1467 if (IS_ERR(pnode))
1468 return ERR_PTR(PTR_ERR(pnode));
1469 iip = (i & (UBIFS_LPT_FANOUT - 1));
1470 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1471 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1472 pnode->lprops[iip].flags);
1473 return &pnode->lprops[iip];
1474}
1475
1476/**
1477 * dirty_cow_nnode - ensure a nnode is not being committed.
1478 * @c: UBIFS file-system description object
1479 * @nnode: nnode to check
1480 *
1481 * Returns dirtied nnode on success or negative error code on failure.
1482 */
1483static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1484 struct ubifs_nnode *nnode)
1485{
1486 struct ubifs_nnode *n;
1487 int i;
1488
1489 if (!test_bit(COW_CNODE, &nnode->flags)) {
1490 /* nnode is not being committed */
1491 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1492 c->dirty_nn_cnt += 1;
1493 ubifs_add_nnode_dirt(c, nnode);
1494 }
1495 return nnode;
1496 }
1497
1498 /* nnode is being committed, so copy it */
1499 n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1500 if (unlikely(!n))
1501 return ERR_PTR(-ENOMEM);
1502
1503 memcpy(n, nnode, sizeof(struct ubifs_nnode));
1504 n->cnext = NULL;
1505 __set_bit(DIRTY_CNODE, &n->flags);
1506 __clear_bit(COW_CNODE, &n->flags);
1507
1508 /* The children now have new parent */
1509 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1510 struct ubifs_nbranch *branch = &n->nbranch[i];
1511
1512 if (branch->cnode)
1513 branch->cnode->parent = n;
1514 }
1515
1516 ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
1517 __set_bit(OBSOLETE_CNODE, &nnode->flags);
1518
1519 c->dirty_nn_cnt += 1;
1520 ubifs_add_nnode_dirt(c, nnode);
1521 if (nnode->parent)
1522 nnode->parent->nbranch[n->iip].nnode = n;
1523 else
1524 c->nroot = n;
1525 return n;
1526}
1527
1528/**
1529 * dirty_cow_pnode - ensure a pnode is not being committed.
1530 * @c: UBIFS file-system description object
1531 * @pnode: pnode to check
1532 *
1533 * Returns dirtied pnode on success or negative error code on failure.
1534 */
1535static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1536 struct ubifs_pnode *pnode)
1537{
1538 struct ubifs_pnode *p;
1539
1540 if (!test_bit(COW_CNODE, &pnode->flags)) {
1541 /* pnode is not being committed */
1542 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1543 c->dirty_pn_cnt += 1;
1544 add_pnode_dirt(c, pnode);
1545 }
1546 return pnode;
1547 }
1548
1549 /* pnode is being committed, so copy it */
1550 p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1551 if (unlikely(!p))
1552 return ERR_PTR(-ENOMEM);
1553
1554 memcpy(p, pnode, sizeof(struct ubifs_pnode));
1555 p->cnext = NULL;
1556 __set_bit(DIRTY_CNODE, &p->flags);
1557 __clear_bit(COW_CNODE, &p->flags);
1558 replace_cats(c, pnode, p);
1559
1560 ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1561 __set_bit(OBSOLETE_CNODE, &pnode->flags);
1562
1563 c->dirty_pn_cnt += 1;
1564 add_pnode_dirt(c, pnode);
1565 pnode->parent->nbranch[p->iip].pnode = p;
1566 return p;
1567}
1568
1569/**
1570 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1571 * @c: UBIFS file-system description object
1572 * @lnum: LEB number to lookup
1573 *
1574 * This function returns a pointer to the LEB properties on success or a
1575 * negative error code on failure.
1576 */
1577struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1578{
1579 int err, i, h, iip, shft;
1580 struct ubifs_nnode *nnode;
1581 struct ubifs_pnode *pnode;
1582
1583 if (!c->nroot) {
1584 err = ubifs_read_nnode(c, NULL, 0);
1585 if (err)
1586 return ERR_PTR(err);
1587 }
1588 nnode = c->nroot;
1589 nnode = dirty_cow_nnode(c, nnode);
1590 if (IS_ERR(nnode))
1591 return ERR_PTR(PTR_ERR(nnode));
1592 i = lnum - c->main_first;
1593 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1594 for (h = 1; h < c->lpt_hght; h++) {
1595 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1596 shft -= UBIFS_LPT_FANOUT_SHIFT;
1597 nnode = ubifs_get_nnode(c, nnode, iip);
1598 if (IS_ERR(nnode))
1599 return ERR_PTR(PTR_ERR(nnode));
1600 nnode = dirty_cow_nnode(c, nnode);
1601 if (IS_ERR(nnode))
1602 return ERR_PTR(PTR_ERR(nnode));
1603 }
1604 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1605 shft -= UBIFS_LPT_FANOUT_SHIFT;
1606 pnode = ubifs_get_pnode(c, nnode, iip);
1607 if (IS_ERR(pnode))
1608 return ERR_PTR(PTR_ERR(pnode));
1609 pnode = dirty_cow_pnode(c, pnode);
1610 if (IS_ERR(pnode))
1611 return ERR_PTR(PTR_ERR(pnode));
1612 iip = (i & (UBIFS_LPT_FANOUT - 1));
1613 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1614 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1615 pnode->lprops[iip].flags);
1616 ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1617 return &pnode->lprops[iip];
1618}
1619
1620/**
1621 * lpt_init_rd - initialize the LPT for reading.
1622 * @c: UBIFS file-system description object
1623 *
1624 * This function returns %0 on success and a negative error code on failure.
1625 */
1626static int lpt_init_rd(struct ubifs_info *c)
1627{
1628 int err, i;
1629
1630 c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1631 if (!c->ltab)
1632 return -ENOMEM;
1633
1634 i = max_t(int, c->nnode_sz, c->pnode_sz);
1635 c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1636 if (!c->lpt_nod_buf)
1637 return -ENOMEM;
1638
1639 for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1640 c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
1641 GFP_KERNEL);
1642 if (!c->lpt_heap[i].arr)
1643 return -ENOMEM;
1644 c->lpt_heap[i].cnt = 0;
1645 c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1646 }
1647
1648 c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1649 if (!c->dirty_idx.arr)
1650 return -ENOMEM;
1651 c->dirty_idx.cnt = 0;
1652 c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1653
1654 err = read_ltab(c);
1655 if (err)
1656 return err;
1657
1658 dbg_lp("space_bits %d", c->space_bits);
1659 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1660 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1661 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1662 dbg_lp("pcnt_bits %d", c->pcnt_bits);
1663 dbg_lp("lnum_bits %d", c->lnum_bits);
1664 dbg_lp("pnode_sz %d", c->pnode_sz);
1665 dbg_lp("nnode_sz %d", c->nnode_sz);
1666 dbg_lp("ltab_sz %d", c->ltab_sz);
1667 dbg_lp("lsave_sz %d", c->lsave_sz);
1668 dbg_lp("lsave_cnt %d", c->lsave_cnt);
1669 dbg_lp("lpt_hght %d", c->lpt_hght);
1670 dbg_lp("big_lpt %d", c->big_lpt);
1671 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1672 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1673 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1674 if (c->big_lpt)
1675 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1676
1677 return 0;
1678}
1679
1680/**
1681 * lpt_init_wr - initialize the LPT for writing.
1682 * @c: UBIFS file-system description object
1683 *
1684 * 'lpt_init_rd()' must have been called already.
1685 *
1686 * This function returns %0 on success and a negative error code on failure.
1687 */
1688static int lpt_init_wr(struct ubifs_info *c)
1689{
1690 int err, i;
1691
1692 c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1693 if (!c->ltab_cmt)
1694 return -ENOMEM;
1695
1696 c->lpt_buf = vmalloc(c->leb_size);
1697 if (!c->lpt_buf)
1698 return -ENOMEM;
1699
1700 if (c->big_lpt) {
1701 c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1702 if (!c->lsave)
1703 return -ENOMEM;
1704 err = read_lsave(c);
1705 if (err)
1706 return err;
1707 }
1708
1709 for (i = 0; i < c->lpt_lebs; i++)
1710 if (c->ltab[i].free == c->leb_size) {
1711 err = ubifs_leb_unmap(c, i + c->lpt_first);
1712 if (err)
1713 return err;
1714 }
1715
1716 return 0;
1717}
1718
1719/**
1720 * ubifs_lpt_init - initialize the LPT.
1721 * @c: UBIFS file-system description object
1722 * @rd: whether to initialize lpt for reading
1723 * @wr: whether to initialize lpt for writing
1724 *
1725 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1726 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1727 * true.
1728 *
1729 * This function returns %0 on success and a negative error code on failure.
1730 */
1731int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1732{
1733 int err;
1734
1735 if (rd) {
1736 err = lpt_init_rd(c);
1737 if (err)
1738 return err;
1739 }
1740
1741 if (wr) {
1742 err = lpt_init_wr(c);
1743 if (err)
1744 return err;
1745 }
1746
1747 return 0;
1748}
1749
1750/**
1751 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1752 * @nnode: where to keep a nnode
1753 * @pnode: where to keep a pnode
1754 * @cnode: where to keep a cnode
1755 * @in_tree: is the node in the tree in memory
1756 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1757 * the tree
1758 * @ptr.pnode: ditto for pnode
1759 * @ptr.cnode: ditto for cnode
1760 */
1761struct lpt_scan_node {
1762 union {
1763 struct ubifs_nnode nnode;
1764 struct ubifs_pnode pnode;
1765 struct ubifs_cnode cnode;
1766 };
1767 int in_tree;
1768 union {
1769 struct ubifs_nnode *nnode;
1770 struct ubifs_pnode *pnode;
1771 struct ubifs_cnode *cnode;
1772 } ptr;
1773};
1774
1775/**
1776 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1777 * @c: the UBIFS file-system description object
1778 * @path: where to put the nnode
1779 * @parent: parent of the nnode
1780 * @iip: index in parent of the nnode
1781 *
1782 * This function returns a pointer to the nnode on success or a negative error
1783 * code on failure.
1784 */
1785static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1786 struct lpt_scan_node *path,
1787 struct ubifs_nnode *parent, int iip)
1788{
1789 struct ubifs_nbranch *branch;
1790 struct ubifs_nnode *nnode;
1791 void *buf = c->lpt_nod_buf;
1792 int err;
1793
1794 branch = &parent->nbranch[iip];
1795 nnode = branch->nnode;
1796 if (nnode) {
1797 path->in_tree = 1;
1798 path->ptr.nnode = nnode;
1799 return nnode;
1800 }
1801 nnode = &path->nnode;
1802 path->in_tree = 0;
1803 path->ptr.nnode = nnode;
1804 memset(nnode, 0, sizeof(struct ubifs_nnode));
1805 if (branch->lnum == 0) {
1806 /*
1807 * This nnode was not written which just means that the LEB
1808 * properties in the subtree below it describe empty LEBs. We
1809 * make the nnode as though we had read it, which in fact means
1810 * doing almost nothing.
1811 */
1812 if (c->big_lpt)
1813 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1814 } else {
1815 err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
1816 c->nnode_sz);
1817 if (err)
1818 return ERR_PTR(err);
2ba5f7ae 1819 err = ubifs_unpack_nnode(c, buf, nnode);
1e51764a
AB
1820 if (err)
1821 return ERR_PTR(err);
1822 }
1823 err = validate_nnode(c, nnode, parent, iip);
1824 if (err)
1825 return ERR_PTR(err);
1826 if (!c->big_lpt)
1827 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1828 nnode->level = parent->level - 1;
1829 nnode->parent = parent;
1830 nnode->iip = iip;
1831 return nnode;
1832}
1833
1834/**
1835 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1836 * @c: the UBIFS file-system description object
1837 * @path: where to put the pnode
1838 * @parent: parent of the pnode
1839 * @iip: index in parent of the pnode
1840 *
1841 * This function returns a pointer to the pnode on success or a negative error
1842 * code on failure.
1843 */
1844static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1845 struct lpt_scan_node *path,
1846 struct ubifs_nnode *parent, int iip)
1847{
1848 struct ubifs_nbranch *branch;
1849 struct ubifs_pnode *pnode;
1850 void *buf = c->lpt_nod_buf;
1851 int err;
1852
1853 branch = &parent->nbranch[iip];
1854 pnode = branch->pnode;
1855 if (pnode) {
1856 path->in_tree = 1;
1857 path->ptr.pnode = pnode;
1858 return pnode;
1859 }
1860 pnode = &path->pnode;
1861 path->in_tree = 0;
1862 path->ptr.pnode = pnode;
1863 memset(pnode, 0, sizeof(struct ubifs_pnode));
1864 if (branch->lnum == 0) {
1865 /*
1866 * This pnode was not written which just means that the LEB
1867 * properties in it describe empty LEBs. We make the pnode as
1868 * though we had read it.
1869 */
1870 int i;
1871
1872 if (c->big_lpt)
1873 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1874 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1875 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1876
1877 lprops->free = c->leb_size;
1878 lprops->flags = ubifs_categorize_lprops(c, lprops);
1879 }
1880 } else {
1881 ubifs_assert(branch->lnum >= c->lpt_first &&
1882 branch->lnum <= c->lpt_last);
1883 ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1884 err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
1885 c->pnode_sz);
1886 if (err)
1887 return ERR_PTR(err);
1888 err = unpack_pnode(c, buf, pnode);
1889 if (err)
1890 return ERR_PTR(err);
1891 }
1892 err = validate_pnode(c, pnode, parent, iip);
1893 if (err)
1894 return ERR_PTR(err);
1895 if (!c->big_lpt)
1896 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1897 pnode->parent = parent;
1898 pnode->iip = iip;
1899 set_pnode_lnum(c, pnode);
1900 return pnode;
1901}
1902
1903/**
1904 * ubifs_lpt_scan_nolock - scan the LPT.
1905 * @c: the UBIFS file-system description object
1906 * @start_lnum: LEB number from which to start scanning
1907 * @end_lnum: LEB number at which to stop scanning
1908 * @scan_cb: callback function called for each lprops
1909 * @data: data to be passed to the callback function
1910 *
1911 * This function returns %0 on success and a negative error code on failure.
1912 */
1913int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1914 ubifs_lpt_scan_callback scan_cb, void *data)
1915{
1916 int err = 0, i, h, iip, shft;
1917 struct ubifs_nnode *nnode;
1918 struct ubifs_pnode *pnode;
1919 struct lpt_scan_node *path;
1920
1921 if (start_lnum == -1) {
1922 start_lnum = end_lnum + 1;
1923 if (start_lnum >= c->leb_cnt)
1924 start_lnum = c->main_first;
1925 }
1926
1927 ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1928 ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1929
1930 if (!c->nroot) {
1931 err = ubifs_read_nnode(c, NULL, 0);
1932 if (err)
1933 return err;
1934 }
1935
1936 path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1937 GFP_NOFS);
1938 if (!path)
1939 return -ENOMEM;
1940
1941 path[0].ptr.nnode = c->nroot;
1942 path[0].in_tree = 1;
1943again:
1944 /* Descend to the pnode containing start_lnum */
1945 nnode = c->nroot;
1946 i = start_lnum - c->main_first;
1947 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1948 for (h = 1; h < c->lpt_hght; h++) {
1949 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1950 shft -= UBIFS_LPT_FANOUT_SHIFT;
1951 nnode = scan_get_nnode(c, path + h, nnode, iip);
1952 if (IS_ERR(nnode)) {
1953 err = PTR_ERR(nnode);
1954 goto out;
1955 }
1956 }
1957 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1958 shft -= UBIFS_LPT_FANOUT_SHIFT;
1959 pnode = scan_get_pnode(c, path + h, nnode, iip);
1960 if (IS_ERR(pnode)) {
1961 err = PTR_ERR(pnode);
1962 goto out;
1963 }
1964 iip = (i & (UBIFS_LPT_FANOUT - 1));
1965
1966 /* Loop for each lprops */
1967 while (1) {
1968 struct ubifs_lprops *lprops = &pnode->lprops[iip];
1969 int ret, lnum = lprops->lnum;
1970
1971 ret = scan_cb(c, lprops, path[h].in_tree, data);
1972 if (ret < 0) {
1973 err = ret;
1974 goto out;
1975 }
1976 if (ret & LPT_SCAN_ADD) {
1977 /* Add all the nodes in path to the tree in memory */
1978 for (h = 1; h < c->lpt_hght; h++) {
1979 const size_t sz = sizeof(struct ubifs_nnode);
1980 struct ubifs_nnode *parent;
1981
1982 if (path[h].in_tree)
1983 continue;
1984 nnode = kmalloc(sz, GFP_NOFS);
1985 if (!nnode) {
1986 err = -ENOMEM;
1987 goto out;
1988 }
1989 memcpy(nnode, &path[h].nnode, sz);
1990 parent = nnode->parent;
1991 parent->nbranch[nnode->iip].nnode = nnode;
1992 path[h].ptr.nnode = nnode;
1993 path[h].in_tree = 1;
1994 path[h + 1].cnode.parent = nnode;
1995 }
1996 if (path[h].in_tree)
1997 ubifs_ensure_cat(c, lprops);
1998 else {
1999 const size_t sz = sizeof(struct ubifs_pnode);
2000 struct ubifs_nnode *parent;
2001
2002 pnode = kmalloc(sz, GFP_NOFS);
2003 if (!pnode) {
2004 err = -ENOMEM;
2005 goto out;
2006 }
2007 memcpy(pnode, &path[h].pnode, sz);
2008 parent = pnode->parent;
2009 parent->nbranch[pnode->iip].pnode = pnode;
2010 path[h].ptr.pnode = pnode;
2011 path[h].in_tree = 1;
2012 update_cats(c, pnode);
2013 c->pnodes_have += 1;
2014 }
2015 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2016 c->nroot, 0, 0);
2017 if (err)
2018 goto out;
2019 err = dbg_check_cats(c);
2020 if (err)
2021 goto out;
2022 }
2023 if (ret & LPT_SCAN_STOP) {
2024 err = 0;
2025 break;
2026 }
2027 /* Get the next lprops */
2028 if (lnum == end_lnum) {
2029 /*
2030 * We got to the end without finding what we were
2031 * looking for
2032 */
2033 err = -ENOSPC;
2034 goto out;
2035 }
2036 if (lnum + 1 >= c->leb_cnt) {
2037 /* Wrap-around to the beginning */
2038 start_lnum = c->main_first;
2039 goto again;
2040 }
2041 if (iip + 1 < UBIFS_LPT_FANOUT) {
2042 /* Next lprops is in the same pnode */
2043 iip += 1;
2044 continue;
2045 }
2046 /* We need to get the next pnode. Go up until we can go right */
2047 iip = pnode->iip;
2048 while (1) {
2049 h -= 1;
2050 ubifs_assert(h >= 0);
2051 nnode = path[h].ptr.nnode;
2052 if (iip + 1 < UBIFS_LPT_FANOUT)
2053 break;
2054 iip = nnode->iip;
2055 }
2056 /* Go right */
2057 iip += 1;
2058 /* Descend to the pnode */
2059 h += 1;
2060 for (; h < c->lpt_hght; h++) {
2061 nnode = scan_get_nnode(c, path + h, nnode, iip);
2062 if (IS_ERR(nnode)) {
2063 err = PTR_ERR(nnode);
2064 goto out;
2065 }
2066 iip = 0;
2067 }
2068 pnode = scan_get_pnode(c, path + h, nnode, iip);
2069 if (IS_ERR(pnode)) {
2070 err = PTR_ERR(pnode);
2071 goto out;
2072 }
2073 iip = 0;
2074 }
2075out:
2076 kfree(path);
2077 return err;
2078}
2079
2080#ifdef CONFIG_UBIFS_FS_DEBUG
2081
2082/**
2083 * dbg_chk_pnode - check a pnode.
2084 * @c: the UBIFS file-system description object
2085 * @pnode: pnode to check
2086 * @col: pnode column
2087 *
2088 * This function returns %0 on success and a negative error code on failure.
2089 */
2090static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2091 int col)
2092{
2093 int i;
2094
2095 if (pnode->num != col) {
2096 dbg_err("pnode num %d expected %d parent num %d iip %d",
2097 pnode->num, col, pnode->parent->num, pnode->iip);
2098 return -EINVAL;
2099 }
2100 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2101 struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2102 int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2103 c->main_first;
2104 int found, cat = lprops->flags & LPROPS_CAT_MASK;
2105 struct ubifs_lpt_heap *heap;
2106 struct list_head *list = NULL;
2107
2108 if (lnum >= c->leb_cnt)
2109 continue;
2110 if (lprops->lnum != lnum) {
2111 dbg_err("bad LEB number %d expected %d",
2112 lprops->lnum, lnum);
2113 return -EINVAL;
2114 }
2115 if (lprops->flags & LPROPS_TAKEN) {
2116 if (cat != LPROPS_UNCAT) {
2117 dbg_err("LEB %d taken but not uncat %d",
2118 lprops->lnum, cat);
2119 return -EINVAL;
2120 }
2121 continue;
2122 }
2123 if (lprops->flags & LPROPS_INDEX) {
2124 switch (cat) {
2125 case LPROPS_UNCAT:
2126 case LPROPS_DIRTY_IDX:
2127 case LPROPS_FRDI_IDX:
2128 break;
2129 default:
2130 dbg_err("LEB %d index but cat %d",
2131 lprops->lnum, cat);
2132 return -EINVAL;
2133 }
2134 } else {
2135 switch (cat) {
2136 case LPROPS_UNCAT:
2137 case LPROPS_DIRTY:
2138 case LPROPS_FREE:
2139 case LPROPS_EMPTY:
2140 case LPROPS_FREEABLE:
2141 break;
2142 default:
2143 dbg_err("LEB %d not index but cat %d",
2144 lprops->lnum, cat);
2145 return -EINVAL;
2146 }
2147 }
2148 switch (cat) {
2149 case LPROPS_UNCAT:
2150 list = &c->uncat_list;
2151 break;
2152 case LPROPS_EMPTY:
2153 list = &c->empty_list;
2154 break;
2155 case LPROPS_FREEABLE:
2156 list = &c->freeable_list;
2157 break;
2158 case LPROPS_FRDI_IDX:
2159 list = &c->frdi_idx_list;
2160 break;
2161 }
2162 found = 0;
2163 switch (cat) {
2164 case LPROPS_DIRTY:
2165 case LPROPS_DIRTY_IDX:
2166 case LPROPS_FREE:
2167 heap = &c->lpt_heap[cat - 1];
2168 if (lprops->hpos < heap->cnt &&
2169 heap->arr[lprops->hpos] == lprops)
2170 found = 1;
2171 break;
2172 case LPROPS_UNCAT:
2173 case LPROPS_EMPTY:
2174 case LPROPS_FREEABLE:
2175 case LPROPS_FRDI_IDX:
2176 list_for_each_entry(lp, list, list)
2177 if (lprops == lp) {
2178 found = 1;
2179 break;
2180 }
2181 break;
2182 }
2183 if (!found) {
2184 dbg_err("LEB %d cat %d not found in cat heap/list",
2185 lprops->lnum, cat);
2186 return -EINVAL;
2187 }
2188 switch (cat) {
2189 case LPROPS_EMPTY:
2190 if (lprops->free != c->leb_size) {
2191 dbg_err("LEB %d cat %d free %d dirty %d",
2192 lprops->lnum, cat, lprops->free,
2193 lprops->dirty);
2194 return -EINVAL;
2195 }
2196 case LPROPS_FREEABLE:
2197 case LPROPS_FRDI_IDX:
2198 if (lprops->free + lprops->dirty != c->leb_size) {
2199 dbg_err("LEB %d cat %d free %d dirty %d",
2200 lprops->lnum, cat, lprops->free,
2201 lprops->dirty);
2202 return -EINVAL;
2203 }
2204 }
2205 }
2206 return 0;
2207}
2208
2209/**
2210 * dbg_check_lpt_nodes - check nnodes and pnodes.
2211 * @c: the UBIFS file-system description object
2212 * @cnode: next cnode (nnode or pnode) to check
2213 * @row: row of cnode (root is zero)
2214 * @col: column of cnode (leftmost is zero)
2215 *
2216 * This function returns %0 on success and a negative error code on failure.
2217 */
2218int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2219 int row, int col)
2220{
2221 struct ubifs_nnode *nnode, *nn;
2222 struct ubifs_cnode *cn;
2223 int num, iip = 0, err;
2224
2225 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
2226 return 0;
2227
2228 while (cnode) {
2229 ubifs_assert(row >= 0);
2230 nnode = cnode->parent;
2231 if (cnode->level) {
2232 /* cnode is a nnode */
2233 num = calc_nnode_num(row, col);
2234 if (cnode->num != num) {
2235 dbg_err("nnode num %d expected %d "
2236 "parent num %d iip %d", cnode->num, num,
2237 (nnode ? nnode->num : 0), cnode->iip);
2238 return -EINVAL;
2239 }
2240 nn = (struct ubifs_nnode *)cnode;
2241 while (iip < UBIFS_LPT_FANOUT) {
2242 cn = nn->nbranch[iip].cnode;
2243 if (cn) {
2244 /* Go down */
2245 row += 1;
2246 col <<= UBIFS_LPT_FANOUT_SHIFT;
2247 col += iip;
2248 iip = 0;
2249 cnode = cn;
2250 break;
2251 }
2252 /* Go right */
2253 iip += 1;
2254 }
2255 if (iip < UBIFS_LPT_FANOUT)
2256 continue;
2257 } else {
2258 struct ubifs_pnode *pnode;
2259
2260 /* cnode is a pnode */
2261 pnode = (struct ubifs_pnode *)cnode;
2262 err = dbg_chk_pnode(c, pnode, col);
2263 if (err)
2264 return err;
2265 }
2266 /* Go up and to the right */
2267 row -= 1;
2268 col >>= UBIFS_LPT_FANOUT_SHIFT;
2269 iip = cnode->iip + 1;
2270 cnode = (struct ubifs_cnode *)nnode;
2271 }
2272 return 0;
2273}
2274
2275#endif /* CONFIG_UBIFS_FS_DEBUG */