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