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