nvme-core: declare local symbols static
[linux/fpc-iii.git] / fs / ubifs / lpt_commit.c
blob7ce30994bbbac726c30a16262cbfd4411243026e
1 /*
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
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.
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.
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
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements commit-related functionality of the LEB properties
25 * subsystem.
28 #include <linux/crc16.h>
29 #include <linux/slab.h>
30 #include <linux/random.h>
31 #include "ubifs.h"
33 static int dbg_populate_lsave(struct ubifs_info *c);
35 /**
36 * first_dirty_cnode - find first dirty cnode.
37 * @c: UBIFS file-system description object
38 * @nnode: nnode at which to start
40 * This function returns the first dirty cnode or %NULL if there is not one.
42 static struct ubifs_cnode *first_dirty_cnode(const struct ubifs_info *c, struct ubifs_nnode *nnode)
44 ubifs_assert(c, nnode);
45 while (1) {
46 int i, cont = 0;
48 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
49 struct ubifs_cnode *cnode;
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;
61 if (!cont)
62 return (struct ubifs_cnode *)nnode;
66 /**
67 * next_dirty_cnode - find next dirty cnode.
68 * @c: UBIFS file-system description object
69 * @cnode: cnode from which to begin searching
71 * This function returns the next dirty cnode or %NULL if there is not one.
73 static struct ubifs_cnode *next_dirty_cnode(const struct ubifs_info *c, struct ubifs_cnode *cnode)
75 struct ubifs_nnode *nnode;
76 int i;
78 ubifs_assert(c, cnode);
79 nnode = cnode->parent;
80 if (!nnode)
81 return NULL;
82 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
83 cnode = nnode->nbranch[i].cnode;
84 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
85 if (cnode->level == 0)
86 return cnode; /* cnode is a pnode */
87 /* cnode is a nnode */
88 return first_dirty_cnode(c, (struct ubifs_nnode *)cnode);
91 return (struct ubifs_cnode *)nnode;
94 /**
95 * get_cnodes_to_commit - create list of dirty cnodes to commit.
96 * @c: UBIFS file-system description object
98 * This function returns the number of cnodes to commit.
100 static int get_cnodes_to_commit(struct ubifs_info *c)
102 struct ubifs_cnode *cnode, *cnext;
103 int cnt = 0;
105 if (!c->nroot)
106 return 0;
108 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
109 return 0;
111 c->lpt_cnext = first_dirty_cnode(c, c->nroot);
112 cnode = c->lpt_cnext;
113 if (!cnode)
114 return 0;
115 cnt += 1;
116 while (1) {
117 ubifs_assert(c, !test_bit(COW_CNODE, &cnode->flags));
118 __set_bit(COW_CNODE, &cnode->flags);
119 cnext = next_dirty_cnode(c, cnode);
120 if (!cnext) {
121 cnode->cnext = c->lpt_cnext;
122 break;
124 cnode->cnext = cnext;
125 cnode = cnext;
126 cnt += 1;
128 dbg_cmt("committing %d cnodes", cnt);
129 dbg_lp("committing %d cnodes", cnt);
130 ubifs_assert(c, cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
131 return cnt;
135 * upd_ltab - update LPT LEB properties.
136 * @c: UBIFS file-system description object
137 * @lnum: LEB number
138 * @free: amount of free space
139 * @dirty: amount of dirty space to add
141 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
143 dbg_lp("LEB %d free %d dirty %d to %d +%d",
144 lnum, c->ltab[lnum - c->lpt_first].free,
145 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
146 ubifs_assert(c, lnum >= c->lpt_first && lnum <= c->lpt_last);
147 c->ltab[lnum - c->lpt_first].free = free;
148 c->ltab[lnum - c->lpt_first].dirty += dirty;
152 * alloc_lpt_leb - allocate an LPT LEB that is empty.
153 * @c: UBIFS file-system description object
154 * @lnum: LEB number is passed and returned here
156 * This function finds the next empty LEB in the ltab starting from @lnum. If a
157 * an empty LEB is found it is returned in @lnum and the function returns %0.
158 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
159 * never to run out of space.
161 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
163 int i, n;
165 n = *lnum - c->lpt_first + 1;
166 for (i = n; i < c->lpt_lebs; i++) {
167 if (c->ltab[i].tgc || c->ltab[i].cmt)
168 continue;
169 if (c->ltab[i].free == c->leb_size) {
170 c->ltab[i].cmt = 1;
171 *lnum = i + c->lpt_first;
172 return 0;
176 for (i = 0; i < n; i++) {
177 if (c->ltab[i].tgc || c->ltab[i].cmt)
178 continue;
179 if (c->ltab[i].free == c->leb_size) {
180 c->ltab[i].cmt = 1;
181 *lnum = i + c->lpt_first;
182 return 0;
185 return -ENOSPC;
189 * layout_cnodes - layout cnodes for commit.
190 * @c: UBIFS file-system description object
192 * This function returns %0 on success and a negative error code on failure.
194 static int layout_cnodes(struct ubifs_info *c)
196 int lnum, offs, len, alen, done_lsave, done_ltab, err;
197 struct ubifs_cnode *cnode;
199 err = dbg_chk_lpt_sz(c, 0, 0);
200 if (err)
201 return err;
202 cnode = c->lpt_cnext;
203 if (!cnode)
204 return 0;
205 lnum = c->nhead_lnum;
206 offs = c->nhead_offs;
207 /* Try to place lsave and ltab nicely */
208 done_lsave = !c->big_lpt;
209 done_ltab = 0;
210 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
211 done_lsave = 1;
212 c->lsave_lnum = lnum;
213 c->lsave_offs = offs;
214 offs += c->lsave_sz;
215 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
218 if (offs + c->ltab_sz <= c->leb_size) {
219 done_ltab = 1;
220 c->ltab_lnum = lnum;
221 c->ltab_offs = offs;
222 offs += c->ltab_sz;
223 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
226 do {
227 if (cnode->level) {
228 len = c->nnode_sz;
229 c->dirty_nn_cnt -= 1;
230 } else {
231 len = c->pnode_sz;
232 c->dirty_pn_cnt -= 1;
234 while (offs + len > c->leb_size) {
235 alen = ALIGN(offs, c->min_io_size);
236 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
237 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
238 err = alloc_lpt_leb(c, &lnum);
239 if (err)
240 goto no_space;
241 offs = 0;
242 ubifs_assert(c, lnum >= c->lpt_first &&
243 lnum <= c->lpt_last);
244 /* Try to place lsave and ltab nicely */
245 if (!done_lsave) {
246 done_lsave = 1;
247 c->lsave_lnum = lnum;
248 c->lsave_offs = offs;
249 offs += c->lsave_sz;
250 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
251 continue;
253 if (!done_ltab) {
254 done_ltab = 1;
255 c->ltab_lnum = lnum;
256 c->ltab_offs = offs;
257 offs += c->ltab_sz;
258 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
259 continue;
261 break;
263 if (cnode->parent) {
264 cnode->parent->nbranch[cnode->iip].lnum = lnum;
265 cnode->parent->nbranch[cnode->iip].offs = offs;
266 } else {
267 c->lpt_lnum = lnum;
268 c->lpt_offs = offs;
270 offs += len;
271 dbg_chk_lpt_sz(c, 1, len);
272 cnode = cnode->cnext;
273 } while (cnode && cnode != c->lpt_cnext);
275 /* Make sure to place LPT's save table */
276 if (!done_lsave) {
277 if (offs + c->lsave_sz > c->leb_size) {
278 alen = ALIGN(offs, c->min_io_size);
279 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
280 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
281 err = alloc_lpt_leb(c, &lnum);
282 if (err)
283 goto no_space;
284 offs = 0;
285 ubifs_assert(c, lnum >= c->lpt_first &&
286 lnum <= c->lpt_last);
288 done_lsave = 1;
289 c->lsave_lnum = lnum;
290 c->lsave_offs = offs;
291 offs += c->lsave_sz;
292 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
295 /* Make sure to place LPT's own lprops table */
296 if (!done_ltab) {
297 if (offs + c->ltab_sz > c->leb_size) {
298 alen = ALIGN(offs, c->min_io_size);
299 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
300 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
301 err = alloc_lpt_leb(c, &lnum);
302 if (err)
303 goto no_space;
304 offs = 0;
305 ubifs_assert(c, lnum >= c->lpt_first &&
306 lnum <= c->lpt_last);
308 c->ltab_lnum = lnum;
309 c->ltab_offs = offs;
310 offs += c->ltab_sz;
311 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
314 alen = ALIGN(offs, c->min_io_size);
315 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
316 dbg_chk_lpt_sz(c, 4, alen - offs);
317 err = dbg_chk_lpt_sz(c, 3, alen);
318 if (err)
319 return err;
320 return 0;
322 no_space:
323 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
324 lnum, offs, len, done_ltab, done_lsave);
325 ubifs_dump_lpt_info(c);
326 ubifs_dump_lpt_lebs(c);
327 dump_stack();
328 return err;
332 * realloc_lpt_leb - allocate an LPT LEB that is empty.
333 * @c: UBIFS file-system description object
334 * @lnum: LEB number is passed and returned here
336 * This function duplicates exactly the results of the function alloc_lpt_leb.
337 * It is used during end commit to reallocate the same LEB numbers that were
338 * allocated by alloc_lpt_leb during start commit.
340 * This function finds the next LEB that was allocated by the alloc_lpt_leb
341 * function starting from @lnum. If a LEB is found it is returned in @lnum and
342 * the function returns %0. Otherwise the function returns -ENOSPC.
343 * Note however, that LPT is designed never to run out of space.
345 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
347 int i, n;
349 n = *lnum - c->lpt_first + 1;
350 for (i = n; i < c->lpt_lebs; i++)
351 if (c->ltab[i].cmt) {
352 c->ltab[i].cmt = 0;
353 *lnum = i + c->lpt_first;
354 return 0;
357 for (i = 0; i < n; i++)
358 if (c->ltab[i].cmt) {
359 c->ltab[i].cmt = 0;
360 *lnum = i + c->lpt_first;
361 return 0;
363 return -ENOSPC;
367 * write_cnodes - write cnodes for commit.
368 * @c: UBIFS file-system description object
370 * This function returns %0 on success and a negative error code on failure.
372 static int write_cnodes(struct ubifs_info *c)
374 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
375 struct ubifs_cnode *cnode;
376 void *buf = c->lpt_buf;
378 cnode = c->lpt_cnext;
379 if (!cnode)
380 return 0;
381 lnum = c->nhead_lnum;
382 offs = c->nhead_offs;
383 from = offs;
384 /* Ensure empty LEB is unmapped */
385 if (offs == 0) {
386 err = ubifs_leb_unmap(c, lnum);
387 if (err)
388 return err;
390 /* Try to place lsave and ltab nicely */
391 done_lsave = !c->big_lpt;
392 done_ltab = 0;
393 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
394 done_lsave = 1;
395 ubifs_pack_lsave(c, buf + offs, c->lsave);
396 offs += c->lsave_sz;
397 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
400 if (offs + c->ltab_sz <= c->leb_size) {
401 done_ltab = 1;
402 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
403 offs += c->ltab_sz;
404 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
407 /* Loop for each cnode */
408 do {
409 if (cnode->level)
410 len = c->nnode_sz;
411 else
412 len = c->pnode_sz;
413 while (offs + len > c->leb_size) {
414 wlen = offs - from;
415 if (wlen) {
416 alen = ALIGN(wlen, c->min_io_size);
417 memset(buf + offs, 0xff, alen - wlen);
418 err = ubifs_leb_write(c, lnum, buf + from, from,
419 alen);
420 if (err)
421 return err;
423 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
424 err = realloc_lpt_leb(c, &lnum);
425 if (err)
426 goto no_space;
427 offs = from = 0;
428 ubifs_assert(c, lnum >= c->lpt_first &&
429 lnum <= c->lpt_last);
430 err = ubifs_leb_unmap(c, lnum);
431 if (err)
432 return err;
433 /* Try to place lsave and ltab nicely */
434 if (!done_lsave) {
435 done_lsave = 1;
436 ubifs_pack_lsave(c, buf + offs, c->lsave);
437 offs += c->lsave_sz;
438 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
439 continue;
441 if (!done_ltab) {
442 done_ltab = 1;
443 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
444 offs += c->ltab_sz;
445 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
446 continue;
448 break;
450 if (cnode->level)
451 ubifs_pack_nnode(c, buf + offs,
452 (struct ubifs_nnode *)cnode);
453 else
454 ubifs_pack_pnode(c, buf + offs,
455 (struct ubifs_pnode *)cnode);
457 * The reason for the barriers is the same as in case of TNC.
458 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
459 * 'dirty_cow_pnode()' are the functions for which this is
460 * important.
462 clear_bit(DIRTY_CNODE, &cnode->flags);
463 smp_mb__before_atomic();
464 clear_bit(COW_CNODE, &cnode->flags);
465 smp_mb__after_atomic();
466 offs += len;
467 dbg_chk_lpt_sz(c, 1, len);
468 cnode = cnode->cnext;
469 } while (cnode && cnode != c->lpt_cnext);
471 /* Make sure to place LPT's save table */
472 if (!done_lsave) {
473 if (offs + c->lsave_sz > c->leb_size) {
474 wlen = offs - from;
475 alen = ALIGN(wlen, c->min_io_size);
476 memset(buf + offs, 0xff, alen - wlen);
477 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
478 if (err)
479 return err;
480 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
481 err = realloc_lpt_leb(c, &lnum);
482 if (err)
483 goto no_space;
484 offs = from = 0;
485 ubifs_assert(c, lnum >= c->lpt_first &&
486 lnum <= c->lpt_last);
487 err = ubifs_leb_unmap(c, lnum);
488 if (err)
489 return err;
491 done_lsave = 1;
492 ubifs_pack_lsave(c, buf + offs, c->lsave);
493 offs += c->lsave_sz;
494 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
497 /* Make sure to place LPT's own lprops table */
498 if (!done_ltab) {
499 if (offs + c->ltab_sz > c->leb_size) {
500 wlen = offs - from;
501 alen = ALIGN(wlen, c->min_io_size);
502 memset(buf + offs, 0xff, alen - wlen);
503 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
504 if (err)
505 return err;
506 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
507 err = realloc_lpt_leb(c, &lnum);
508 if (err)
509 goto no_space;
510 offs = from = 0;
511 ubifs_assert(c, lnum >= c->lpt_first &&
512 lnum <= c->lpt_last);
513 err = ubifs_leb_unmap(c, lnum);
514 if (err)
515 return err;
517 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
518 offs += c->ltab_sz;
519 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
522 /* Write remaining data in buffer */
523 wlen = offs - from;
524 alen = ALIGN(wlen, c->min_io_size);
525 memset(buf + offs, 0xff, alen - wlen);
526 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
527 if (err)
528 return err;
530 dbg_chk_lpt_sz(c, 4, alen - wlen);
531 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
532 if (err)
533 return err;
535 c->nhead_lnum = lnum;
536 c->nhead_offs = ALIGN(offs, c->min_io_size);
538 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
539 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
540 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
541 if (c->big_lpt)
542 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
544 return 0;
546 no_space:
547 ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
548 lnum, offs, len, done_ltab, done_lsave);
549 ubifs_dump_lpt_info(c);
550 ubifs_dump_lpt_lebs(c);
551 dump_stack();
552 return err;
556 * next_pnode_to_dirty - find next pnode to dirty.
557 * @c: UBIFS file-system description object
558 * @pnode: pnode
560 * This function returns the next pnode to dirty or %NULL if there are no more
561 * pnodes. Note that pnodes that have never been written (lnum == 0) are
562 * skipped.
564 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
565 struct ubifs_pnode *pnode)
567 struct ubifs_nnode *nnode;
568 int iip;
570 /* Try to go right */
571 nnode = pnode->parent;
572 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
573 if (nnode->nbranch[iip].lnum)
574 return ubifs_get_pnode(c, nnode, iip);
577 /* Go up while can't go right */
578 do {
579 iip = nnode->iip + 1;
580 nnode = nnode->parent;
581 if (!nnode)
582 return NULL;
583 for (; iip < UBIFS_LPT_FANOUT; iip++) {
584 if (nnode->nbranch[iip].lnum)
585 break;
587 } while (iip >= UBIFS_LPT_FANOUT);
589 /* Go right */
590 nnode = ubifs_get_nnode(c, nnode, iip);
591 if (IS_ERR(nnode))
592 return (void *)nnode;
594 /* Go down to level 1 */
595 while (nnode->level > 1) {
596 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
597 if (nnode->nbranch[iip].lnum)
598 break;
600 if (iip >= UBIFS_LPT_FANOUT) {
602 * Should not happen, but we need to keep going
603 * if it does.
605 iip = 0;
607 nnode = ubifs_get_nnode(c, nnode, iip);
608 if (IS_ERR(nnode))
609 return (void *)nnode;
612 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
613 if (nnode->nbranch[iip].lnum)
614 break;
615 if (iip >= UBIFS_LPT_FANOUT)
616 /* Should not happen, but we need to keep going if it does */
617 iip = 0;
618 return ubifs_get_pnode(c, nnode, iip);
622 * pnode_lookup - lookup a pnode in the LPT.
623 * @c: UBIFS file-system description object
624 * @i: pnode number (0 to (main_lebs - 1) / UBIFS_LPT_FANOUT))
626 * This function returns a pointer to the pnode on success or a negative
627 * error code on failure.
629 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
631 int err, h, iip, shft;
632 struct ubifs_nnode *nnode;
634 if (!c->nroot) {
635 err = ubifs_read_nnode(c, NULL, 0);
636 if (err)
637 return ERR_PTR(err);
639 i <<= UBIFS_LPT_FANOUT_SHIFT;
640 nnode = c->nroot;
641 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
642 for (h = 1; h < c->lpt_hght; h++) {
643 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
644 shft -= UBIFS_LPT_FANOUT_SHIFT;
645 nnode = ubifs_get_nnode(c, nnode, iip);
646 if (IS_ERR(nnode))
647 return ERR_CAST(nnode);
649 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
650 return ubifs_get_pnode(c, nnode, iip);
654 * add_pnode_dirt - add dirty space to LPT LEB properties.
655 * @c: UBIFS file-system description object
656 * @pnode: pnode for which to add dirt
658 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
660 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
661 c->pnode_sz);
665 * do_make_pnode_dirty - mark a pnode dirty.
666 * @c: UBIFS file-system description object
667 * @pnode: pnode to mark dirty
669 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
671 /* Assumes cnext list is empty i.e. not called during commit */
672 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
673 struct ubifs_nnode *nnode;
675 c->dirty_pn_cnt += 1;
676 add_pnode_dirt(c, pnode);
677 /* Mark parent and ancestors dirty too */
678 nnode = pnode->parent;
679 while (nnode) {
680 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
681 c->dirty_nn_cnt += 1;
682 ubifs_add_nnode_dirt(c, nnode);
683 nnode = nnode->parent;
684 } else
685 break;
691 * make_tree_dirty - mark the entire LEB properties tree dirty.
692 * @c: UBIFS file-system description object
694 * This function is used by the "small" LPT model to cause the entire LEB
695 * properties tree to be written. The "small" LPT model does not use LPT
696 * garbage collection because it is more efficient to write the entire tree
697 * (because it is small).
699 * This function returns %0 on success and a negative error code on failure.
701 static int make_tree_dirty(struct ubifs_info *c)
703 struct ubifs_pnode *pnode;
705 pnode = pnode_lookup(c, 0);
706 if (IS_ERR(pnode))
707 return PTR_ERR(pnode);
709 while (pnode) {
710 do_make_pnode_dirty(c, pnode);
711 pnode = next_pnode_to_dirty(c, pnode);
712 if (IS_ERR(pnode))
713 return PTR_ERR(pnode);
715 return 0;
719 * need_write_all - determine if the LPT area is running out of free space.
720 * @c: UBIFS file-system description object
722 * This function returns %1 if the LPT area is running out of free space and %0
723 * if it is not.
725 static int need_write_all(struct ubifs_info *c)
727 long long free = 0;
728 int i;
730 for (i = 0; i < c->lpt_lebs; i++) {
731 if (i + c->lpt_first == c->nhead_lnum)
732 free += c->leb_size - c->nhead_offs;
733 else if (c->ltab[i].free == c->leb_size)
734 free += c->leb_size;
735 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
736 free += c->leb_size;
738 /* Less than twice the size left */
739 if (free <= c->lpt_sz * 2)
740 return 1;
741 return 0;
745 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
746 * @c: UBIFS file-system description object
748 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
749 * free space and so may be reused as soon as the next commit is completed.
750 * This function is called during start commit to mark LPT LEBs for trivial GC.
752 static void lpt_tgc_start(struct ubifs_info *c)
754 int i;
756 for (i = 0; i < c->lpt_lebs; i++) {
757 if (i + c->lpt_first == c->nhead_lnum)
758 continue;
759 if (c->ltab[i].dirty > 0 &&
760 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
761 c->ltab[i].tgc = 1;
762 c->ltab[i].free = c->leb_size;
763 c->ltab[i].dirty = 0;
764 dbg_lp("LEB %d", i + c->lpt_first);
770 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
771 * @c: UBIFS file-system description object
773 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
774 * free space and so may be reused as soon as the next commit is completed.
775 * This function is called after the commit is completed (master node has been
776 * written) and un-maps LPT LEBs that were marked for trivial GC.
778 static int lpt_tgc_end(struct ubifs_info *c)
780 int i, err;
782 for (i = 0; i < c->lpt_lebs; i++)
783 if (c->ltab[i].tgc) {
784 err = ubifs_leb_unmap(c, i + c->lpt_first);
785 if (err)
786 return err;
787 c->ltab[i].tgc = 0;
788 dbg_lp("LEB %d", i + c->lpt_first);
790 return 0;
794 * populate_lsave - fill the lsave array with important LEB numbers.
795 * @c: the UBIFS file-system description object
797 * This function is only called for the "big" model. It records a small number
798 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
799 * most important to least important): empty, freeable, freeable index, dirty
800 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
801 * their pnodes into memory. That will stop us from having to scan the LPT
802 * straight away. For the "small" model we assume that scanning the LPT is no
803 * big deal.
805 static void populate_lsave(struct ubifs_info *c)
807 struct ubifs_lprops *lprops;
808 struct ubifs_lpt_heap *heap;
809 int i, cnt = 0;
811 ubifs_assert(c, c->big_lpt);
812 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
813 c->lpt_drty_flgs |= LSAVE_DIRTY;
814 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
817 if (dbg_populate_lsave(c))
818 return;
820 list_for_each_entry(lprops, &c->empty_list, list) {
821 c->lsave[cnt++] = lprops->lnum;
822 if (cnt >= c->lsave_cnt)
823 return;
825 list_for_each_entry(lprops, &c->freeable_list, list) {
826 c->lsave[cnt++] = lprops->lnum;
827 if (cnt >= c->lsave_cnt)
828 return;
830 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
831 c->lsave[cnt++] = lprops->lnum;
832 if (cnt >= c->lsave_cnt)
833 return;
835 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
836 for (i = 0; i < heap->cnt; i++) {
837 c->lsave[cnt++] = heap->arr[i]->lnum;
838 if (cnt >= c->lsave_cnt)
839 return;
841 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
842 for (i = 0; i < heap->cnt; i++) {
843 c->lsave[cnt++] = heap->arr[i]->lnum;
844 if (cnt >= c->lsave_cnt)
845 return;
847 heap = &c->lpt_heap[LPROPS_FREE - 1];
848 for (i = 0; i < heap->cnt; i++) {
849 c->lsave[cnt++] = heap->arr[i]->lnum;
850 if (cnt >= c->lsave_cnt)
851 return;
853 /* Fill it up completely */
854 while (cnt < c->lsave_cnt)
855 c->lsave[cnt++] = c->main_first;
859 * nnode_lookup - lookup a nnode in the LPT.
860 * @c: UBIFS file-system description object
861 * @i: nnode number
863 * This function returns a pointer to the nnode on success or a negative
864 * error code on failure.
866 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
868 int err, iip;
869 struct ubifs_nnode *nnode;
871 if (!c->nroot) {
872 err = ubifs_read_nnode(c, NULL, 0);
873 if (err)
874 return ERR_PTR(err);
876 nnode = c->nroot;
877 while (1) {
878 iip = i & (UBIFS_LPT_FANOUT - 1);
879 i >>= UBIFS_LPT_FANOUT_SHIFT;
880 if (!i)
881 break;
882 nnode = ubifs_get_nnode(c, nnode, iip);
883 if (IS_ERR(nnode))
884 return nnode;
886 return nnode;
890 * make_nnode_dirty - find a nnode and, if found, make it dirty.
891 * @c: UBIFS file-system description object
892 * @node_num: nnode number of nnode to make dirty
893 * @lnum: LEB number where nnode was written
894 * @offs: offset where nnode was written
896 * This function is used by LPT garbage collection. LPT garbage collection is
897 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
898 * simply involves marking all the nodes in the LEB being garbage-collected as
899 * dirty. The dirty nodes are written next commit, after which the LEB is free
900 * to be reused.
902 * This function returns %0 on success and a negative error code on failure.
904 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
905 int offs)
907 struct ubifs_nnode *nnode;
909 nnode = nnode_lookup(c, node_num);
910 if (IS_ERR(nnode))
911 return PTR_ERR(nnode);
912 if (nnode->parent) {
913 struct ubifs_nbranch *branch;
915 branch = &nnode->parent->nbranch[nnode->iip];
916 if (branch->lnum != lnum || branch->offs != offs)
917 return 0; /* nnode is obsolete */
918 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
919 return 0; /* nnode is obsolete */
920 /* Assumes cnext list is empty i.e. not called during commit */
921 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
922 c->dirty_nn_cnt += 1;
923 ubifs_add_nnode_dirt(c, nnode);
924 /* Mark parent and ancestors dirty too */
925 nnode = nnode->parent;
926 while (nnode) {
927 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
928 c->dirty_nn_cnt += 1;
929 ubifs_add_nnode_dirt(c, nnode);
930 nnode = nnode->parent;
931 } else
932 break;
935 return 0;
939 * make_pnode_dirty - find a pnode and, if found, make it dirty.
940 * @c: UBIFS file-system description object
941 * @node_num: pnode number of pnode to make dirty
942 * @lnum: LEB number where pnode was written
943 * @offs: offset where pnode was written
945 * This function is used by LPT garbage collection. LPT garbage collection is
946 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
947 * simply involves marking all the nodes in the LEB being garbage-collected as
948 * dirty. The dirty nodes are written next commit, after which the LEB is free
949 * to be reused.
951 * This function returns %0 on success and a negative error code on failure.
953 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
954 int offs)
956 struct ubifs_pnode *pnode;
957 struct ubifs_nbranch *branch;
959 pnode = pnode_lookup(c, node_num);
960 if (IS_ERR(pnode))
961 return PTR_ERR(pnode);
962 branch = &pnode->parent->nbranch[pnode->iip];
963 if (branch->lnum != lnum || branch->offs != offs)
964 return 0;
965 do_make_pnode_dirty(c, pnode);
966 return 0;
970 * make_ltab_dirty - make ltab node dirty.
971 * @c: UBIFS file-system description object
972 * @lnum: LEB number where ltab was written
973 * @offs: offset where ltab was written
975 * This function is used by LPT garbage collection. LPT garbage collection is
976 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
977 * simply involves marking all the nodes in the LEB being garbage-collected as
978 * dirty. The dirty nodes are written next commit, after which the LEB is free
979 * to be reused.
981 * This function returns %0 on success and a negative error code on failure.
983 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
985 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
986 return 0; /* This ltab node is obsolete */
987 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
988 c->lpt_drty_flgs |= LTAB_DIRTY;
989 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
991 return 0;
995 * make_lsave_dirty - make lsave node dirty.
996 * @c: UBIFS file-system description object
997 * @lnum: LEB number where lsave was written
998 * @offs: offset where lsave was written
1000 * This function is used by LPT garbage collection. LPT garbage collection is
1001 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1002 * simply involves marking all the nodes in the LEB being garbage-collected as
1003 * dirty. The dirty nodes are written next commit, after which the LEB is free
1004 * to be reused.
1006 * This function returns %0 on success and a negative error code on failure.
1008 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1010 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1011 return 0; /* This lsave node is obsolete */
1012 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1013 c->lpt_drty_flgs |= LSAVE_DIRTY;
1014 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1016 return 0;
1020 * make_node_dirty - make node dirty.
1021 * @c: UBIFS file-system description object
1022 * @node_type: LPT node type
1023 * @node_num: node number
1024 * @lnum: LEB number where node was written
1025 * @offs: offset where node was written
1027 * This function is used by LPT garbage collection. LPT garbage collection is
1028 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1029 * simply involves marking all the nodes in the LEB being garbage-collected as
1030 * dirty. The dirty nodes are written next commit, after which the LEB is free
1031 * to be reused.
1033 * This function returns %0 on success and a negative error code on failure.
1035 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1036 int lnum, int offs)
1038 switch (node_type) {
1039 case UBIFS_LPT_NNODE:
1040 return make_nnode_dirty(c, node_num, lnum, offs);
1041 case UBIFS_LPT_PNODE:
1042 return make_pnode_dirty(c, node_num, lnum, offs);
1043 case UBIFS_LPT_LTAB:
1044 return make_ltab_dirty(c, lnum, offs);
1045 case UBIFS_LPT_LSAVE:
1046 return make_lsave_dirty(c, lnum, offs);
1048 return -EINVAL;
1052 * get_lpt_node_len - return the length of a node based on its type.
1053 * @c: UBIFS file-system description object
1054 * @node_type: LPT node type
1056 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1058 switch (node_type) {
1059 case UBIFS_LPT_NNODE:
1060 return c->nnode_sz;
1061 case UBIFS_LPT_PNODE:
1062 return c->pnode_sz;
1063 case UBIFS_LPT_LTAB:
1064 return c->ltab_sz;
1065 case UBIFS_LPT_LSAVE:
1066 return c->lsave_sz;
1068 return 0;
1072 * get_pad_len - return the length of padding in a buffer.
1073 * @c: UBIFS file-system description object
1074 * @buf: buffer
1075 * @len: length of buffer
1077 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1079 int offs, pad_len;
1081 if (c->min_io_size == 1)
1082 return 0;
1083 offs = c->leb_size - len;
1084 pad_len = ALIGN(offs, c->min_io_size) - offs;
1085 return pad_len;
1089 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1090 * @c: UBIFS file-system description object
1091 * @buf: buffer
1092 * @node_num: node number is returned here
1094 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1095 int *node_num)
1097 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1098 int pos = 0, node_type;
1100 node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS);
1101 *node_num = ubifs_unpack_bits(c, &addr, &pos, c->pcnt_bits);
1102 return node_type;
1106 * is_a_node - determine if a buffer contains a node.
1107 * @c: UBIFS file-system description object
1108 * @buf: buffer
1109 * @len: length of buffer
1111 * This function returns %1 if the buffer contains a node or %0 if it does not.
1113 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1115 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1116 int pos = 0, node_type, node_len;
1117 uint16_t crc, calc_crc;
1119 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1120 return 0;
1121 node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS);
1122 if (node_type == UBIFS_LPT_NOT_A_NODE)
1123 return 0;
1124 node_len = get_lpt_node_len(c, node_type);
1125 if (!node_len || node_len > len)
1126 return 0;
1127 pos = 0;
1128 addr = buf;
1129 crc = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_CRC_BITS);
1130 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1131 node_len - UBIFS_LPT_CRC_BYTES);
1132 if (crc != calc_crc)
1133 return 0;
1134 return 1;
1138 * lpt_gc_lnum - garbage collect a LPT LEB.
1139 * @c: UBIFS file-system description object
1140 * @lnum: LEB number to garbage collect
1142 * LPT garbage collection is used only for the "big" LPT model
1143 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1144 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1145 * next commit, after which the LEB is free to be reused.
1147 * This function returns %0 on success and a negative error code on failure.
1149 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1151 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1152 void *buf = c->lpt_buf;
1154 dbg_lp("LEB %d", lnum);
1156 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1157 if (err)
1158 return err;
1160 while (1) {
1161 if (!is_a_node(c, buf, len)) {
1162 int pad_len;
1164 pad_len = get_pad_len(c, buf, len);
1165 if (pad_len) {
1166 buf += pad_len;
1167 len -= pad_len;
1168 continue;
1170 return 0;
1172 node_type = get_lpt_node_type(c, buf, &node_num);
1173 node_len = get_lpt_node_len(c, node_type);
1174 offs = c->leb_size - len;
1175 ubifs_assert(c, node_len != 0);
1176 mutex_lock(&c->lp_mutex);
1177 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1178 mutex_unlock(&c->lp_mutex);
1179 if (err)
1180 return err;
1181 buf += node_len;
1182 len -= node_len;
1184 return 0;
1188 * lpt_gc - LPT garbage collection.
1189 * @c: UBIFS file-system description object
1191 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1192 * Returns %0 on success and a negative error code on failure.
1194 static int lpt_gc(struct ubifs_info *c)
1196 int i, lnum = -1, dirty = 0;
1198 mutex_lock(&c->lp_mutex);
1199 for (i = 0; i < c->lpt_lebs; i++) {
1200 ubifs_assert(c, !c->ltab[i].tgc);
1201 if (i + c->lpt_first == c->nhead_lnum ||
1202 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1203 continue;
1204 if (c->ltab[i].dirty > dirty) {
1205 dirty = c->ltab[i].dirty;
1206 lnum = i + c->lpt_first;
1209 mutex_unlock(&c->lp_mutex);
1210 if (lnum == -1)
1211 return -ENOSPC;
1212 return lpt_gc_lnum(c, lnum);
1216 * ubifs_lpt_start_commit - UBIFS commit starts.
1217 * @c: the UBIFS file-system description object
1219 * This function has to be called when UBIFS starts the commit operation.
1220 * This function "freezes" all currently dirty LEB properties and does not
1221 * change them anymore. Further changes are saved and tracked separately
1222 * because they are not part of this commit. This function returns zero in case
1223 * of success and a negative error code in case of failure.
1225 int ubifs_lpt_start_commit(struct ubifs_info *c)
1227 int err, cnt;
1229 dbg_lp("");
1231 mutex_lock(&c->lp_mutex);
1232 err = dbg_chk_lpt_free_spc(c);
1233 if (err)
1234 goto out;
1235 err = dbg_check_ltab(c);
1236 if (err)
1237 goto out;
1239 if (c->check_lpt_free) {
1241 * We ensure there is enough free space in
1242 * ubifs_lpt_post_commit() by marking nodes dirty. That
1243 * information is lost when we unmount, so we also need
1244 * to check free space once after mounting also.
1246 c->check_lpt_free = 0;
1247 while (need_write_all(c)) {
1248 mutex_unlock(&c->lp_mutex);
1249 err = lpt_gc(c);
1250 if (err)
1251 return err;
1252 mutex_lock(&c->lp_mutex);
1256 lpt_tgc_start(c);
1258 if (!c->dirty_pn_cnt) {
1259 dbg_cmt("no cnodes to commit");
1260 err = 0;
1261 goto out;
1264 if (!c->big_lpt && need_write_all(c)) {
1265 /* If needed, write everything */
1266 err = make_tree_dirty(c);
1267 if (err)
1268 goto out;
1269 lpt_tgc_start(c);
1272 if (c->big_lpt)
1273 populate_lsave(c);
1275 cnt = get_cnodes_to_commit(c);
1276 ubifs_assert(c, cnt != 0);
1278 err = layout_cnodes(c);
1279 if (err)
1280 goto out;
1282 /* Copy the LPT's own lprops for end commit to write */
1283 memcpy(c->ltab_cmt, c->ltab,
1284 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1285 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1287 out:
1288 mutex_unlock(&c->lp_mutex);
1289 return err;
1293 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1294 * @c: UBIFS file-system description object
1296 static void free_obsolete_cnodes(struct ubifs_info *c)
1298 struct ubifs_cnode *cnode, *cnext;
1300 cnext = c->lpt_cnext;
1301 if (!cnext)
1302 return;
1303 do {
1304 cnode = cnext;
1305 cnext = cnode->cnext;
1306 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1307 kfree(cnode);
1308 else
1309 cnode->cnext = NULL;
1310 } while (cnext != c->lpt_cnext);
1311 c->lpt_cnext = NULL;
1315 * ubifs_lpt_end_commit - finish the commit operation.
1316 * @c: the UBIFS file-system description object
1318 * This function has to be called when the commit operation finishes. It
1319 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1320 * the media. Returns zero in case of success and a negative error code in case
1321 * of failure.
1323 int ubifs_lpt_end_commit(struct ubifs_info *c)
1325 int err;
1327 dbg_lp("");
1329 if (!c->lpt_cnext)
1330 return 0;
1332 err = write_cnodes(c);
1333 if (err)
1334 return err;
1336 mutex_lock(&c->lp_mutex);
1337 free_obsolete_cnodes(c);
1338 mutex_unlock(&c->lp_mutex);
1340 return 0;
1344 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1345 * @c: UBIFS file-system description object
1347 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1348 * commit for the "big" LPT model.
1350 int ubifs_lpt_post_commit(struct ubifs_info *c)
1352 int err;
1354 mutex_lock(&c->lp_mutex);
1355 err = lpt_tgc_end(c);
1356 if (err)
1357 goto out;
1358 if (c->big_lpt)
1359 while (need_write_all(c)) {
1360 mutex_unlock(&c->lp_mutex);
1361 err = lpt_gc(c);
1362 if (err)
1363 return err;
1364 mutex_lock(&c->lp_mutex);
1366 out:
1367 mutex_unlock(&c->lp_mutex);
1368 return err;
1372 * first_nnode - find the first nnode in memory.
1373 * @c: UBIFS file-system description object
1374 * @hght: height of tree where nnode found is returned here
1376 * This function returns a pointer to the nnode found or %NULL if no nnode is
1377 * found. This function is a helper to 'ubifs_lpt_free()'.
1379 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1381 struct ubifs_nnode *nnode;
1382 int h, i, found;
1384 nnode = c->nroot;
1385 *hght = 0;
1386 if (!nnode)
1387 return NULL;
1388 for (h = 1; h < c->lpt_hght; h++) {
1389 found = 0;
1390 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1391 if (nnode->nbranch[i].nnode) {
1392 found = 1;
1393 nnode = nnode->nbranch[i].nnode;
1394 *hght = h;
1395 break;
1398 if (!found)
1399 break;
1401 return nnode;
1405 * next_nnode - find the next nnode in memory.
1406 * @c: UBIFS file-system description object
1407 * @nnode: nnode from which to start.
1408 * @hght: height of tree where nnode is, is passed and returned here
1410 * This function returns a pointer to the nnode found or %NULL if no nnode is
1411 * found. This function is a helper to 'ubifs_lpt_free()'.
1413 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1414 struct ubifs_nnode *nnode, int *hght)
1416 struct ubifs_nnode *parent;
1417 int iip, h, i, found;
1419 parent = nnode->parent;
1420 if (!parent)
1421 return NULL;
1422 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1423 *hght -= 1;
1424 return parent;
1426 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1427 nnode = parent->nbranch[iip].nnode;
1428 if (nnode)
1429 break;
1431 if (!nnode) {
1432 *hght -= 1;
1433 return parent;
1435 for (h = *hght + 1; h < c->lpt_hght; h++) {
1436 found = 0;
1437 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1438 if (nnode->nbranch[i].nnode) {
1439 found = 1;
1440 nnode = nnode->nbranch[i].nnode;
1441 *hght = h;
1442 break;
1445 if (!found)
1446 break;
1448 return nnode;
1452 * ubifs_lpt_free - free resources owned by the LPT.
1453 * @c: UBIFS file-system description object
1454 * @wr_only: free only resources used for writing
1456 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1458 struct ubifs_nnode *nnode;
1459 int i, hght;
1461 /* Free write-only things first */
1463 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1465 vfree(c->ltab_cmt);
1466 c->ltab_cmt = NULL;
1467 vfree(c->lpt_buf);
1468 c->lpt_buf = NULL;
1469 kfree(c->lsave);
1470 c->lsave = NULL;
1472 if (wr_only)
1473 return;
1475 /* Now free the rest */
1477 nnode = first_nnode(c, &hght);
1478 while (nnode) {
1479 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1480 kfree(nnode->nbranch[i].nnode);
1481 nnode = next_nnode(c, nnode, &hght);
1483 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1484 kfree(c->lpt_heap[i].arr);
1485 kfree(c->dirty_idx.arr);
1486 kfree(c->nroot);
1487 vfree(c->ltab);
1488 kfree(c->lpt_nod_buf);
1492 * Everything below is related to debugging.
1496 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1497 * @buf: buffer
1498 * @len: buffer length
1500 static int dbg_is_all_ff(uint8_t *buf, int len)
1502 int i;
1504 for (i = 0; i < len; i++)
1505 if (buf[i] != 0xff)
1506 return 0;
1507 return 1;
1511 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1512 * @c: the UBIFS file-system description object
1513 * @lnum: LEB number where nnode was written
1514 * @offs: offset where nnode was written
1516 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1518 struct ubifs_nnode *nnode;
1519 int hght;
1521 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1522 nnode = first_nnode(c, &hght);
1523 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1524 struct ubifs_nbranch *branch;
1526 cond_resched();
1527 if (nnode->parent) {
1528 branch = &nnode->parent->nbranch[nnode->iip];
1529 if (branch->lnum != lnum || branch->offs != offs)
1530 continue;
1531 if (test_bit(DIRTY_CNODE, &nnode->flags))
1532 return 1;
1533 return 0;
1534 } else {
1535 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1536 continue;
1537 if (test_bit(DIRTY_CNODE, &nnode->flags))
1538 return 1;
1539 return 0;
1542 return 1;
1546 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1547 * @c: the UBIFS file-system description object
1548 * @lnum: LEB number where pnode was written
1549 * @offs: offset where pnode was written
1551 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1553 int i, cnt;
1555 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1556 for (i = 0; i < cnt; i++) {
1557 struct ubifs_pnode *pnode;
1558 struct ubifs_nbranch *branch;
1560 cond_resched();
1561 pnode = pnode_lookup(c, i);
1562 if (IS_ERR(pnode))
1563 return PTR_ERR(pnode);
1564 branch = &pnode->parent->nbranch[pnode->iip];
1565 if (branch->lnum != lnum || branch->offs != offs)
1566 continue;
1567 if (test_bit(DIRTY_CNODE, &pnode->flags))
1568 return 1;
1569 return 0;
1571 return 1;
1575 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1576 * @c: the UBIFS file-system description object
1577 * @lnum: LEB number where ltab node was written
1578 * @offs: offset where ltab node was written
1580 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1582 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1583 return 1;
1584 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1588 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1589 * @c: the UBIFS file-system description object
1590 * @lnum: LEB number where lsave node was written
1591 * @offs: offset where lsave node was written
1593 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1595 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1596 return 1;
1597 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1601 * dbg_is_node_dirty - determine if a node is dirty.
1602 * @c: the UBIFS file-system description object
1603 * @node_type: node type
1604 * @lnum: LEB number where node was written
1605 * @offs: offset where node was written
1607 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1608 int offs)
1610 switch (node_type) {
1611 case UBIFS_LPT_NNODE:
1612 return dbg_is_nnode_dirty(c, lnum, offs);
1613 case UBIFS_LPT_PNODE:
1614 return dbg_is_pnode_dirty(c, lnum, offs);
1615 case UBIFS_LPT_LTAB:
1616 return dbg_is_ltab_dirty(c, lnum, offs);
1617 case UBIFS_LPT_LSAVE:
1618 return dbg_is_lsave_dirty(c, lnum, offs);
1620 return 1;
1624 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1625 * @c: the UBIFS file-system description object
1626 * @lnum: LEB number where node was written
1628 * This function returns %0 on success and a negative error code on failure.
1630 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1632 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1633 int ret;
1634 void *buf, *p;
1636 if (!dbg_is_chk_lprops(c))
1637 return 0;
1639 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1640 if (!buf) {
1641 ubifs_err(c, "cannot allocate memory for ltab checking");
1642 return 0;
1645 dbg_lp("LEB %d", lnum);
1647 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1648 if (err)
1649 goto out;
1651 while (1) {
1652 if (!is_a_node(c, p, len)) {
1653 int i, pad_len;
1655 pad_len = get_pad_len(c, p, len);
1656 if (pad_len) {
1657 p += pad_len;
1658 len -= pad_len;
1659 dirty += pad_len;
1660 continue;
1662 if (!dbg_is_all_ff(p, len)) {
1663 ubifs_err(c, "invalid empty space in LEB %d at %d",
1664 lnum, c->leb_size - len);
1665 err = -EINVAL;
1667 i = lnum - c->lpt_first;
1668 if (len != c->ltab[i].free) {
1669 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)",
1670 lnum, len, c->ltab[i].free);
1671 err = -EINVAL;
1673 if (dirty != c->ltab[i].dirty) {
1674 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)",
1675 lnum, dirty, c->ltab[i].dirty);
1676 err = -EINVAL;
1678 goto out;
1680 node_type = get_lpt_node_type(c, p, &node_num);
1681 node_len = get_lpt_node_len(c, node_type);
1682 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1683 if (ret == 1)
1684 dirty += node_len;
1685 p += node_len;
1686 len -= node_len;
1689 err = 0;
1690 out:
1691 vfree(buf);
1692 return err;
1696 * dbg_check_ltab - check the free and dirty space in the ltab.
1697 * @c: the UBIFS file-system description object
1699 * This function returns %0 on success and a negative error code on failure.
1701 int dbg_check_ltab(struct ubifs_info *c)
1703 int lnum, err, i, cnt;
1705 if (!dbg_is_chk_lprops(c))
1706 return 0;
1708 /* Bring the entire tree into memory */
1709 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1710 for (i = 0; i < cnt; i++) {
1711 struct ubifs_pnode *pnode;
1713 pnode = pnode_lookup(c, i);
1714 if (IS_ERR(pnode))
1715 return PTR_ERR(pnode);
1716 cond_resched();
1719 /* Check nodes */
1720 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1721 if (err)
1722 return err;
1724 /* Check each LEB */
1725 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1726 err = dbg_check_ltab_lnum(c, lnum);
1727 if (err) {
1728 ubifs_err(c, "failed at LEB %d", lnum);
1729 return err;
1733 dbg_lp("succeeded");
1734 return 0;
1738 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1739 * @c: the UBIFS file-system description object
1741 * This function returns %0 on success and a negative error code on failure.
1743 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1745 long long free = 0;
1746 int i;
1748 if (!dbg_is_chk_lprops(c))
1749 return 0;
1751 for (i = 0; i < c->lpt_lebs; i++) {
1752 if (c->ltab[i].tgc || c->ltab[i].cmt)
1753 continue;
1754 if (i + c->lpt_first == c->nhead_lnum)
1755 free += c->leb_size - c->nhead_offs;
1756 else if (c->ltab[i].free == c->leb_size)
1757 free += c->leb_size;
1759 if (free < c->lpt_sz) {
1760 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld",
1761 free, c->lpt_sz);
1762 ubifs_dump_lpt_info(c);
1763 ubifs_dump_lpt_lebs(c);
1764 dump_stack();
1765 return -EINVAL;
1767 return 0;
1771 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1772 * @c: the UBIFS file-system description object
1773 * @action: what to do
1774 * @len: length written
1776 * This function returns %0 on success and a negative error code on failure.
1777 * The @action argument may be one of:
1778 * o %0 - LPT debugging checking starts, initialize debugging variables;
1779 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1780 * o %2 - switched to a different LEB and wasted @len bytes;
1781 * o %3 - check that we've written the right number of bytes.
1782 * o %4 - wasted @len bytes;
1784 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1786 struct ubifs_debug_info *d = c->dbg;
1787 long long chk_lpt_sz, lpt_sz;
1788 int err = 0;
1790 if (!dbg_is_chk_lprops(c))
1791 return 0;
1793 switch (action) {
1794 case 0:
1795 d->chk_lpt_sz = 0;
1796 d->chk_lpt_sz2 = 0;
1797 d->chk_lpt_lebs = 0;
1798 d->chk_lpt_wastage = 0;
1799 if (c->dirty_pn_cnt > c->pnode_cnt) {
1800 ubifs_err(c, "dirty pnodes %d exceed max %d",
1801 c->dirty_pn_cnt, c->pnode_cnt);
1802 err = -EINVAL;
1804 if (c->dirty_nn_cnt > c->nnode_cnt) {
1805 ubifs_err(c, "dirty nnodes %d exceed max %d",
1806 c->dirty_nn_cnt, c->nnode_cnt);
1807 err = -EINVAL;
1809 return err;
1810 case 1:
1811 d->chk_lpt_sz += len;
1812 return 0;
1813 case 2:
1814 d->chk_lpt_sz += len;
1815 d->chk_lpt_wastage += len;
1816 d->chk_lpt_lebs += 1;
1817 return 0;
1818 case 3:
1819 chk_lpt_sz = c->leb_size;
1820 chk_lpt_sz *= d->chk_lpt_lebs;
1821 chk_lpt_sz += len - c->nhead_offs;
1822 if (d->chk_lpt_sz != chk_lpt_sz) {
1823 ubifs_err(c, "LPT wrote %lld but space used was %lld",
1824 d->chk_lpt_sz, chk_lpt_sz);
1825 err = -EINVAL;
1827 if (d->chk_lpt_sz > c->lpt_sz) {
1828 ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld",
1829 d->chk_lpt_sz, c->lpt_sz);
1830 err = -EINVAL;
1832 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1833 ubifs_err(c, "LPT layout size %lld but wrote %lld",
1834 d->chk_lpt_sz, d->chk_lpt_sz2);
1835 err = -EINVAL;
1837 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1838 ubifs_err(c, "LPT new nhead offs: expected %d was %d",
1839 d->new_nhead_offs, len);
1840 err = -EINVAL;
1842 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1843 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1844 lpt_sz += c->ltab_sz;
1845 if (c->big_lpt)
1846 lpt_sz += c->lsave_sz;
1847 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1848 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1849 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1850 err = -EINVAL;
1852 if (err) {
1853 ubifs_dump_lpt_info(c);
1854 ubifs_dump_lpt_lebs(c);
1855 dump_stack();
1857 d->chk_lpt_sz2 = d->chk_lpt_sz;
1858 d->chk_lpt_sz = 0;
1859 d->chk_lpt_wastage = 0;
1860 d->chk_lpt_lebs = 0;
1861 d->new_nhead_offs = len;
1862 return err;
1863 case 4:
1864 d->chk_lpt_sz += len;
1865 d->chk_lpt_wastage += len;
1866 return 0;
1867 default:
1868 return -EINVAL;
1873 * dump_lpt_leb - dump an LPT LEB.
1874 * @c: UBIFS file-system description object
1875 * @lnum: LEB number to dump
1877 * This function dumps an LEB from LPT area. Nodes in this area are very
1878 * different to nodes in the main area (e.g., they do not have common headers,
1879 * they do not have 8-byte alignments, etc), so we have a separate function to
1880 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1882 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1884 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1885 void *buf, *p;
1887 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
1888 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1889 if (!buf) {
1890 ubifs_err(c, "cannot allocate memory to dump LPT");
1891 return;
1894 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1895 if (err)
1896 goto out;
1898 while (1) {
1899 offs = c->leb_size - len;
1900 if (!is_a_node(c, p, len)) {
1901 int pad_len;
1903 pad_len = get_pad_len(c, p, len);
1904 if (pad_len) {
1905 pr_err("LEB %d:%d, pad %d bytes\n",
1906 lnum, offs, pad_len);
1907 p += pad_len;
1908 len -= pad_len;
1909 continue;
1911 if (len)
1912 pr_err("LEB %d:%d, free %d bytes\n",
1913 lnum, offs, len);
1914 break;
1917 node_type = get_lpt_node_type(c, p, &node_num);
1918 switch (node_type) {
1919 case UBIFS_LPT_PNODE:
1921 node_len = c->pnode_sz;
1922 if (c->big_lpt)
1923 pr_err("LEB %d:%d, pnode num %d\n",
1924 lnum, offs, node_num);
1925 else
1926 pr_err("LEB %d:%d, pnode\n", lnum, offs);
1927 break;
1929 case UBIFS_LPT_NNODE:
1931 int i;
1932 struct ubifs_nnode nnode;
1934 node_len = c->nnode_sz;
1935 if (c->big_lpt)
1936 pr_err("LEB %d:%d, nnode num %d, ",
1937 lnum, offs, node_num);
1938 else
1939 pr_err("LEB %d:%d, nnode, ",
1940 lnum, offs);
1941 err = ubifs_unpack_nnode(c, p, &nnode);
1942 if (err) {
1943 pr_err("failed to unpack_node, error %d\n",
1944 err);
1945 break;
1947 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1948 pr_cont("%d:%d", nnode.nbranch[i].lnum,
1949 nnode.nbranch[i].offs);
1950 if (i != UBIFS_LPT_FANOUT - 1)
1951 pr_cont(", ");
1953 pr_cont("\n");
1954 break;
1956 case UBIFS_LPT_LTAB:
1957 node_len = c->ltab_sz;
1958 pr_err("LEB %d:%d, ltab\n", lnum, offs);
1959 break;
1960 case UBIFS_LPT_LSAVE:
1961 node_len = c->lsave_sz;
1962 pr_err("LEB %d:%d, lsave len\n", lnum, offs);
1963 break;
1964 default:
1965 ubifs_err(c, "LPT node type %d not recognized", node_type);
1966 goto out;
1969 p += node_len;
1970 len -= node_len;
1973 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
1974 out:
1975 vfree(buf);
1976 return;
1980 * ubifs_dump_lpt_lebs - dump LPT lebs.
1981 * @c: UBIFS file-system description object
1983 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1984 * locked.
1986 void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
1988 int i;
1990 pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
1991 for (i = 0; i < c->lpt_lebs; i++)
1992 dump_lpt_leb(c, i + c->lpt_first);
1993 pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
1997 * dbg_populate_lsave - debugging version of 'populate_lsave()'
1998 * @c: UBIFS file-system description object
2000 * This is a debugging version for 'populate_lsave()' which populates lsave
2001 * with random LEBs instead of useful LEBs, which is good for test coverage.
2002 * Returns zero if lsave has not been populated (this debugging feature is
2003 * disabled) an non-zero if lsave has been populated.
2005 static int dbg_populate_lsave(struct ubifs_info *c)
2007 struct ubifs_lprops *lprops;
2008 struct ubifs_lpt_heap *heap;
2009 int i;
2011 if (!dbg_is_chk_gen(c))
2012 return 0;
2013 if (prandom_u32() & 3)
2014 return 0;
2016 for (i = 0; i < c->lsave_cnt; i++)
2017 c->lsave[i] = c->main_first;
2019 list_for_each_entry(lprops, &c->empty_list, list)
2020 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2021 list_for_each_entry(lprops, &c->freeable_list, list)
2022 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2023 list_for_each_entry(lprops, &c->frdi_idx_list, list)
2024 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2026 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2027 for (i = 0; i < heap->cnt; i++)
2028 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2029 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2030 for (i = 0; i < heap->cnt; i++)
2031 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2032 heap = &c->lpt_heap[LPROPS_FREE - 1];
2033 for (i = 0; i < heap->cnt; i++)
2034 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2036 return 1;