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