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
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
28 #include <linux/crc16.h>
32 * first_dirty_cnode - find first dirty cnode.
33 * @c: UBIFS file-system description object
34 * @nnode: nnode at which to start
36 * This function returns the first dirty cnode or %NULL if there is not one.
38 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
44 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
45 struct ubifs_cnode *cnode;
47 cnode = nnode->nbranch[i].cnode;
49 test_bit(DIRTY_CNODE, &cnode->flags)) {
50 if (cnode->level == 0)
52 nnode = (struct ubifs_nnode *)cnode;
58 return (struct ubifs_cnode *)nnode;
63 * next_dirty_cnode - find next dirty cnode.
64 * @cnode: cnode from which to begin searching
66 * This function returns the next dirty cnode or %NULL if there is not one.
68 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
70 struct ubifs_nnode *nnode;
74 nnode = cnode->parent;
77 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
78 cnode = nnode->nbranch[i].cnode;
79 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
80 if (cnode->level == 0)
81 return cnode; /* cnode is a pnode */
82 /* cnode is a nnode */
83 return first_dirty_cnode((struct ubifs_nnode *)cnode);
86 return (struct ubifs_cnode *)nnode;
90 * get_cnodes_to_commit - create list of dirty cnodes to commit.
91 * @c: UBIFS file-system description object
93 * This function returns the number of cnodes to commit.
95 static int get_cnodes_to_commit(struct ubifs_info *c)
97 struct ubifs_cnode *cnode, *cnext;
103 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
106 c->lpt_cnext = first_dirty_cnode(c->nroot);
107 cnode = c->lpt_cnext;
112 ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
113 __set_bit(COW_ZNODE, &cnode->flags);
114 cnext = next_dirty_cnode(cnode);
116 cnode->cnext = c->lpt_cnext;
119 cnode->cnext = cnext;
123 dbg_cmt("committing %d cnodes", cnt);
124 dbg_lp("committing %d cnodes", cnt);
125 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
130 * upd_ltab - update LPT LEB properties.
131 * @c: UBIFS file-system description object
133 * @free: amount of free space
134 * @dirty: amount of dirty space to add
136 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
138 dbg_lp("LEB %d free %d dirty %d to %d +%d",
139 lnum, c->ltab[lnum - c->lpt_first].free,
140 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
141 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
142 c->ltab[lnum - c->lpt_first].free = free;
143 c->ltab[lnum - c->lpt_first].dirty += dirty;
147 * alloc_lpt_leb - allocate an LPT LEB that is empty.
148 * @c: UBIFS file-system description object
149 * @lnum: LEB number is passed and returned here
151 * This function finds the next empty LEB in the ltab starting from @lnum. If a
152 * an empty LEB is found it is returned in @lnum and the function returns %0.
153 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
154 * never to run out of space.
156 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
160 n = *lnum - c->lpt_first + 1;
161 for (i = n; i < c->lpt_lebs; i++) {
162 if (c->ltab[i].tgc || c->ltab[i].cmt)
164 if (c->ltab[i].free == c->leb_size) {
166 *lnum = i + c->lpt_first;
171 for (i = 0; i < n; i++) {
172 if (c->ltab[i].tgc || c->ltab[i].cmt)
174 if (c->ltab[i].free == c->leb_size) {
176 *lnum = i + c->lpt_first;
180 dbg_err("last LEB %d", *lnum);
186 * layout_cnodes - layout cnodes for commit.
187 * @c: UBIFS file-system description object
189 * This function returns %0 on success and a negative error code on failure.
191 static int layout_cnodes(struct ubifs_info *c)
193 int lnum, offs, len, alen, done_lsave, done_ltab, err;
194 struct ubifs_cnode *cnode;
196 cnode = c->lpt_cnext;
199 lnum = c->nhead_lnum;
200 offs = c->nhead_offs;
201 /* Try to place lsave and ltab nicely */
202 done_lsave = !c->big_lpt;
204 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
206 c->lsave_lnum = lnum;
207 c->lsave_offs = offs;
211 if (offs + c->ltab_sz <= c->leb_size) {
221 c->dirty_nn_cnt -= 1;
224 c->dirty_pn_cnt -= 1;
226 while (offs + len > c->leb_size) {
227 alen = ALIGN(offs, c->min_io_size);
228 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
229 err = alloc_lpt_leb(c, &lnum);
233 ubifs_assert(lnum >= c->lpt_first &&
234 lnum <= c->lpt_last);
235 /* Try to place lsave and ltab nicely */
238 c->lsave_lnum = lnum;
239 c->lsave_offs = offs;
253 cnode->parent->nbranch[cnode->iip].lnum = lnum;
254 cnode->parent->nbranch[cnode->iip].offs = offs;
260 cnode = cnode->cnext;
261 } while (cnode && cnode != c->lpt_cnext);
263 /* Make sure to place LPT's save table */
265 if (offs + c->lsave_sz > c->leb_size) {
266 alen = ALIGN(offs, c->min_io_size);
267 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
268 err = alloc_lpt_leb(c, &lnum);
272 ubifs_assert(lnum >= c->lpt_first &&
273 lnum <= c->lpt_last);
276 c->lsave_lnum = lnum;
277 c->lsave_offs = offs;
281 /* Make sure to place LPT's own lprops table */
283 if (offs + c->ltab_sz > c->leb_size) {
284 alen = ALIGN(offs, c->min_io_size);
285 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
286 err = alloc_lpt_leb(c, &lnum);
290 ubifs_assert(lnum >= c->lpt_first &&
291 lnum <= c->lpt_last);
299 alen = ALIGN(offs, c->min_io_size);
300 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
305 * realloc_lpt_leb - allocate an LPT LEB that is empty.
306 * @c: UBIFS file-system description object
307 * @lnum: LEB number is passed and returned here
309 * This function duplicates exactly the results of the function alloc_lpt_leb.
310 * It is used during end commit to reallocate the same LEB numbers that were
311 * allocated by alloc_lpt_leb during start commit.
313 * This function finds the next LEB that was allocated by the alloc_lpt_leb
314 * function starting from @lnum. If a LEB is found it is returned in @lnum and
315 * the function returns %0. Otherwise the function returns -ENOSPC.
316 * Note however, that LPT is designed never to run out of space.
318 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
322 n = *lnum - c->lpt_first + 1;
323 for (i = n; i < c->lpt_lebs; i++)
324 if (c->ltab[i].cmt) {
326 *lnum = i + c->lpt_first;
330 for (i = 0; i < n; i++)
331 if (c->ltab[i].cmt) {
333 *lnum = i + c->lpt_first;
336 dbg_err("last LEB %d", *lnum);
342 * write_cnodes - write cnodes for commit.
343 * @c: UBIFS file-system description object
345 * This function returns %0 on success and a negative error code on failure.
347 static int write_cnodes(struct ubifs_info *c)
349 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
350 struct ubifs_cnode *cnode;
351 void *buf = c->lpt_buf;
353 cnode = c->lpt_cnext;
356 lnum = c->nhead_lnum;
357 offs = c->nhead_offs;
359 /* Ensure empty LEB is unmapped */
361 err = ubifs_leb_unmap(c, lnum);
365 /* Try to place lsave and ltab nicely */
366 done_lsave = !c->big_lpt;
368 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
370 ubifs_pack_lsave(c, buf + offs, c->lsave);
374 if (offs + c->ltab_sz <= c->leb_size) {
376 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
380 /* Loop for each cnode */
386 while (offs + len > c->leb_size) {
389 alen = ALIGN(wlen, c->min_io_size);
390 memset(buf + offs, 0xff, alen - wlen);
391 err = ubifs_leb_write(c, lnum, buf + from, from,
392 alen, UBI_SHORTTERM);
396 err = realloc_lpt_leb(c, &lnum);
401 ubifs_assert(lnum >= c->lpt_first &&
402 lnum <= c->lpt_last);
403 err = ubifs_leb_unmap(c, lnum);
406 /* Try to place lsave and ltab nicely */
409 ubifs_pack_lsave(c, buf + offs, c->lsave);
415 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
422 ubifs_pack_nnode(c, buf + offs,
423 (struct ubifs_nnode *)cnode);
425 ubifs_pack_pnode(c, buf + offs,
426 (struct ubifs_pnode *)cnode);
428 * The reason for the barriers is the same as in case of TNC.
429 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
430 * 'dirty_cow_pnode()' are the functions for which this is
433 clear_bit(DIRTY_CNODE, &cnode->flags);
434 smp_mb__before_clear_bit();
435 clear_bit(COW_ZNODE, &cnode->flags);
436 smp_mb__after_clear_bit();
438 cnode = cnode->cnext;
439 } while (cnode && cnode != c->lpt_cnext);
441 /* Make sure to place LPT's save table */
443 if (offs + c->lsave_sz > c->leb_size) {
445 alen = ALIGN(wlen, c->min_io_size);
446 memset(buf + offs, 0xff, alen - wlen);
447 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
451 err = realloc_lpt_leb(c, &lnum);
455 ubifs_assert(lnum >= c->lpt_first &&
456 lnum <= c->lpt_last);
457 err = ubifs_leb_unmap(c, lnum);
462 ubifs_pack_lsave(c, buf + offs, c->lsave);
466 /* Make sure to place LPT's own lprops table */
468 if (offs + c->ltab_sz > c->leb_size) {
470 alen = ALIGN(wlen, c->min_io_size);
471 memset(buf + offs, 0xff, alen - wlen);
472 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
476 err = realloc_lpt_leb(c, &lnum);
480 ubifs_assert(lnum >= c->lpt_first &&
481 lnum <= c->lpt_last);
482 err = ubifs_leb_unmap(c, lnum);
487 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
491 /* Write remaining data in buffer */
493 alen = ALIGN(wlen, c->min_io_size);
494 memset(buf + offs, 0xff, alen - wlen);
495 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
498 c->nhead_lnum = lnum;
499 c->nhead_offs = ALIGN(offs, c->min_io_size);
501 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
502 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
503 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
505 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
510 * next_pnode - find next pnode.
511 * @c: UBIFS file-system description object
514 * This function returns the next pnode or %NULL if there are no more pnodes.
516 static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
517 struct ubifs_pnode *pnode)
519 struct ubifs_nnode *nnode;
522 /* Try to go right */
523 nnode = pnode->parent;
524 iip = pnode->iip + 1;
525 if (iip < UBIFS_LPT_FANOUT) {
526 /* We assume here that LEB zero is never an LPT LEB */
527 if (nnode->nbranch[iip].lnum)
528 return ubifs_get_pnode(c, nnode, iip);
533 /* Go up while can't go right */
535 iip = nnode->iip + 1;
536 nnode = nnode->parent;
539 /* We assume here that LEB zero is never an LPT LEB */
540 } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
543 nnode = ubifs_get_nnode(c, nnode, iip);
545 return (void *)nnode;
547 /* Go down to level 1 */
548 while (nnode->level > 1) {
549 nnode = ubifs_get_nnode(c, nnode, 0);
551 return (void *)nnode;
554 return ubifs_get_pnode(c, nnode, 0);
558 * pnode_lookup - lookup a pnode in the LPT.
559 * @c: UBIFS file-system description object
560 * @i: pnode number (0 to main_lebs - 1)
562 * This function returns a pointer to the pnode on success or a negative
563 * error code on failure.
565 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
567 int err, h, iip, shft;
568 struct ubifs_nnode *nnode;
571 err = ubifs_read_nnode(c, NULL, 0);
575 i <<= UBIFS_LPT_FANOUT_SHIFT;
577 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
578 for (h = 1; h < c->lpt_hght; h++) {
579 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
580 shft -= UBIFS_LPT_FANOUT_SHIFT;
581 nnode = ubifs_get_nnode(c, nnode, iip);
583 return ERR_PTR(PTR_ERR(nnode));
585 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
586 return ubifs_get_pnode(c, nnode, iip);
590 * add_pnode_dirt - add dirty space to LPT LEB properties.
591 * @c: UBIFS file-system description object
592 * @pnode: pnode for which to add dirt
594 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
596 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
601 * do_make_pnode_dirty - mark a pnode dirty.
602 * @c: UBIFS file-system description object
603 * @pnode: pnode to mark dirty
605 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
607 /* Assumes cnext list is empty i.e. not called during commit */
608 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
609 struct ubifs_nnode *nnode;
611 c->dirty_pn_cnt += 1;
612 add_pnode_dirt(c, pnode);
613 /* Mark parent and ancestors dirty too */
614 nnode = pnode->parent;
616 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
617 c->dirty_nn_cnt += 1;
618 ubifs_add_nnode_dirt(c, nnode);
619 nnode = nnode->parent;
627 * make_tree_dirty - mark the entire LEB properties tree dirty.
628 * @c: UBIFS file-system description object
630 * This function is used by the "small" LPT model to cause the entire LEB
631 * properties tree to be written. The "small" LPT model does not use LPT
632 * garbage collection because it is more efficient to write the entire tree
633 * (because it is small).
635 * This function returns %0 on success and a negative error code on failure.
637 static int make_tree_dirty(struct ubifs_info *c)
639 struct ubifs_pnode *pnode;
641 pnode = pnode_lookup(c, 0);
643 do_make_pnode_dirty(c, pnode);
644 pnode = next_pnode(c, pnode);
646 return PTR_ERR(pnode);
652 * need_write_all - determine if the LPT area is running out of free space.
653 * @c: UBIFS file-system description object
655 * This function returns %1 if the LPT area is running out of free space and %0
658 static int need_write_all(struct ubifs_info *c)
663 for (i = 0; i < c->lpt_lebs; i++) {
664 if (i + c->lpt_first == c->nhead_lnum)
665 free += c->leb_size - c->nhead_offs;
666 else if (c->ltab[i].free == c->leb_size)
668 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
671 /* Less than twice the size left */
672 if (free <= c->lpt_sz * 2)
678 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
679 * @c: UBIFS file-system description object
681 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
682 * free space and so may be reused as soon as the next commit is completed.
683 * This function is called during start commit to mark LPT LEBs for trivial GC.
685 static void lpt_tgc_start(struct ubifs_info *c)
689 for (i = 0; i < c->lpt_lebs; i++) {
690 if (i + c->lpt_first == c->nhead_lnum)
692 if (c->ltab[i].dirty > 0 &&
693 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
695 c->ltab[i].free = c->leb_size;
696 c->ltab[i].dirty = 0;
697 dbg_lp("LEB %d", i + c->lpt_first);
703 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
704 * @c: UBIFS file-system description object
706 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
707 * free space and so may be reused as soon as the next commit is completed.
708 * This function is called after the commit is completed (master node has been
709 * written) and unmaps LPT LEBs that were marked for trivial GC.
711 static int lpt_tgc_end(struct ubifs_info *c)
715 for (i = 0; i < c->lpt_lebs; i++)
716 if (c->ltab[i].tgc) {
717 err = ubifs_leb_unmap(c, i + c->lpt_first);
721 dbg_lp("LEB %d", i + c->lpt_first);
727 * populate_lsave - fill the lsave array with important LEB numbers.
728 * @c: the UBIFS file-system description object
730 * This function is only called for the "big" model. It records a small number
731 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
732 * most important to least important): empty, freeable, freeable index, dirty
733 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
734 * their pnodes into memory. That will stop us from having to scan the LPT
735 * straight away. For the "small" model we assume that scanning the LPT is no
738 static void populate_lsave(struct ubifs_info *c)
740 struct ubifs_lprops *lprops;
741 struct ubifs_lpt_heap *heap;
744 ubifs_assert(c->big_lpt);
745 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
746 c->lpt_drty_flgs |= LSAVE_DIRTY;
747 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
749 list_for_each_entry(lprops, &c->empty_list, list) {
750 c->lsave[cnt++] = lprops->lnum;
751 if (cnt >= c->lsave_cnt)
754 list_for_each_entry(lprops, &c->freeable_list, list) {
755 c->lsave[cnt++] = lprops->lnum;
756 if (cnt >= c->lsave_cnt)
759 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
760 c->lsave[cnt++] = lprops->lnum;
761 if (cnt >= c->lsave_cnt)
764 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
765 for (i = 0; i < heap->cnt; i++) {
766 c->lsave[cnt++] = heap->arr[i]->lnum;
767 if (cnt >= c->lsave_cnt)
770 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
771 for (i = 0; i < heap->cnt; i++) {
772 c->lsave[cnt++] = heap->arr[i]->lnum;
773 if (cnt >= c->lsave_cnt)
776 heap = &c->lpt_heap[LPROPS_FREE - 1];
777 for (i = 0; i < heap->cnt; i++) {
778 c->lsave[cnt++] = heap->arr[i]->lnum;
779 if (cnt >= c->lsave_cnt)
782 /* Fill it up completely */
783 while (cnt < c->lsave_cnt)
784 c->lsave[cnt++] = c->main_first;
788 * nnode_lookup - lookup a nnode in the LPT.
789 * @c: UBIFS file-system description object
792 * This function returns a pointer to the nnode on success or a negative
793 * error code on failure.
795 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
798 struct ubifs_nnode *nnode;
801 err = ubifs_read_nnode(c, NULL, 0);
807 iip = i & (UBIFS_LPT_FANOUT - 1);
808 i >>= UBIFS_LPT_FANOUT_SHIFT;
811 nnode = ubifs_get_nnode(c, nnode, iip);
819 * make_nnode_dirty - find a nnode and, if found, make it dirty.
820 * @c: UBIFS file-system description object
821 * @node_num: nnode number of nnode to make dirty
822 * @lnum: LEB number where nnode was written
823 * @offs: offset where nnode was written
825 * This function is used by LPT garbage collection. LPT garbage collection is
826 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
827 * simply involves marking all the nodes in the LEB being garbage-collected as
828 * dirty. The dirty nodes are written next commit, after which the LEB is free
831 * This function returns %0 on success and a negative error code on failure.
833 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
836 struct ubifs_nnode *nnode;
838 nnode = nnode_lookup(c, node_num);
840 return PTR_ERR(nnode);
842 struct ubifs_nbranch *branch;
844 branch = &nnode->parent->nbranch[nnode->iip];
845 if (branch->lnum != lnum || branch->offs != offs)
846 return 0; /* nnode is obsolete */
847 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
848 return 0; /* nnode is obsolete */
849 /* Assumes cnext list is empty i.e. not called during commit */
850 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
851 c->dirty_nn_cnt += 1;
852 ubifs_add_nnode_dirt(c, nnode);
853 /* Mark parent and ancestors dirty too */
854 nnode = nnode->parent;
856 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
857 c->dirty_nn_cnt += 1;
858 ubifs_add_nnode_dirt(c, nnode);
859 nnode = nnode->parent;
868 * make_pnode_dirty - find a pnode and, if found, make it dirty.
869 * @c: UBIFS file-system description object
870 * @node_num: pnode number of pnode to make dirty
871 * @lnum: LEB number where pnode was written
872 * @offs: offset where pnode was written
874 * This function is used by LPT garbage collection. LPT garbage collection is
875 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
876 * simply involves marking all the nodes in the LEB being garbage-collected as
877 * dirty. The dirty nodes are written next commit, after which the LEB is free
880 * This function returns %0 on success and a negative error code on failure.
882 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
885 struct ubifs_pnode *pnode;
886 struct ubifs_nbranch *branch;
888 pnode = pnode_lookup(c, node_num);
890 return PTR_ERR(pnode);
891 branch = &pnode->parent->nbranch[pnode->iip];
892 if (branch->lnum != lnum || branch->offs != offs)
894 do_make_pnode_dirty(c, pnode);
899 * make_ltab_dirty - make ltab node dirty.
900 * @c: UBIFS file-system description object
901 * @lnum: LEB number where ltab was written
902 * @offs: offset where ltab was written
904 * This function is used by LPT garbage collection. LPT garbage collection is
905 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
906 * simply involves marking all the nodes in the LEB being garbage-collected as
907 * dirty. The dirty nodes are written next commit, after which the LEB is free
910 * This function returns %0 on success and a negative error code on failure.
912 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
914 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
915 return 0; /* This ltab node is obsolete */
916 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
917 c->lpt_drty_flgs |= LTAB_DIRTY;
918 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
924 * make_lsave_dirty - make lsave node dirty.
925 * @c: UBIFS file-system description object
926 * @lnum: LEB number where lsave was written
927 * @offs: offset where lsave was written
929 * This function is used by LPT garbage collection. LPT garbage collection is
930 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
931 * simply involves marking all the nodes in the LEB being garbage-collected as
932 * dirty. The dirty nodes are written next commit, after which the LEB is free
935 * This function returns %0 on success and a negative error code on failure.
937 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
939 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
940 return 0; /* This lsave node is obsolete */
941 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
942 c->lpt_drty_flgs |= LSAVE_DIRTY;
943 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
949 * make_node_dirty - make node dirty.
950 * @c: UBIFS file-system description object
951 * @node_type: LPT node type
952 * @node_num: node number
953 * @lnum: LEB number where node was written
954 * @offs: offset where node was written
956 * This function is used by LPT garbage collection. LPT garbage collection is
957 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
958 * simply involves marking all the nodes in the LEB being garbage-collected as
959 * dirty. The dirty nodes are written next commit, after which the LEB is free
962 * This function returns %0 on success and a negative error code on failure.
964 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
968 case UBIFS_LPT_NNODE:
969 return make_nnode_dirty(c, node_num, lnum, offs);
970 case UBIFS_LPT_PNODE:
971 return make_pnode_dirty(c, node_num, lnum, offs);
973 return make_ltab_dirty(c, lnum, offs);
974 case UBIFS_LPT_LSAVE:
975 return make_lsave_dirty(c, lnum, offs);
981 * get_lpt_node_len - return the length of a node based on its type.
982 * @c: UBIFS file-system description object
983 * @node_type: LPT node type
985 static int get_lpt_node_len(struct ubifs_info *c, int node_type)
988 case UBIFS_LPT_NNODE:
990 case UBIFS_LPT_PNODE:
994 case UBIFS_LPT_LSAVE:
1001 * get_pad_len - return the length of padding in a buffer.
1002 * @c: UBIFS file-system description object
1004 * @len: length of buffer
1006 static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
1010 if (c->min_io_size == 1)
1012 offs = c->leb_size - len;
1013 pad_len = ALIGN(offs, c->min_io_size) - offs;
1018 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1019 * @c: UBIFS file-system description object
1021 * @node_num: node number is returned here
1023 static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
1025 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1026 int pos = 0, node_type;
1028 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1029 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1034 * is_a_node - determine if a buffer contains a node.
1035 * @c: UBIFS file-system description object
1037 * @len: length of buffer
1039 * This function returns %1 if the buffer contains a node or %0 if it does not.
1041 static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
1043 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1044 int pos = 0, node_type, node_len;
1045 uint16_t crc, calc_crc;
1047 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1048 if (node_type == UBIFS_LPT_NOT_A_NODE)
1050 node_len = get_lpt_node_len(c, node_type);
1051 if (!node_len || node_len > len)
1055 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1056 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1057 node_len - UBIFS_LPT_CRC_BYTES);
1058 if (crc != calc_crc)
1065 * lpt_gc_lnum - garbage collect a LPT LEB.
1066 * @c: UBIFS file-system description object
1067 * @lnum: LEB number to garbage collect
1069 * LPT garbage collection is used only for the "big" LPT model
1070 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1071 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1072 * next commit, after which the LEB is free to be reused.
1074 * This function returns %0 on success and a negative error code on failure.
1076 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1078 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1079 void *buf = c->lpt_buf;
1081 dbg_lp("LEB %d", lnum);
1082 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1084 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1088 if (!is_a_node(c, buf, len)) {
1091 pad_len = get_pad_len(c, buf, len);
1099 node_type = get_lpt_node_type(c, buf, &node_num);
1100 node_len = get_lpt_node_len(c, node_type);
1101 offs = c->leb_size - len;
1102 ubifs_assert(node_len != 0);
1103 mutex_lock(&c->lp_mutex);
1104 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1105 mutex_unlock(&c->lp_mutex);
1115 * lpt_gc - LPT garbage collection.
1116 * @c: UBIFS file-system description object
1118 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1119 * Returns %0 on success and a negative error code on failure.
1121 static int lpt_gc(struct ubifs_info *c)
1123 int i, lnum = -1, dirty = 0;
1125 mutex_lock(&c->lp_mutex);
1126 for (i = 0; i < c->lpt_lebs; i++) {
1127 ubifs_assert(!c->ltab[i].tgc);
1128 if (i + c->lpt_first == c->nhead_lnum ||
1129 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1131 if (c->ltab[i].dirty > dirty) {
1132 dirty = c->ltab[i].dirty;
1133 lnum = i + c->lpt_first;
1136 mutex_unlock(&c->lp_mutex);
1139 return lpt_gc_lnum(c, lnum);
1143 * ubifs_lpt_start_commit - UBIFS commit starts.
1144 * @c: the UBIFS file-system description object
1146 * This function has to be called when UBIFS starts the commit operation.
1147 * This function "freezes" all currently dirty LEB properties and does not
1148 * change them anymore. Further changes are saved and tracked separately
1149 * because they are not part of this commit. This function returns zero in case
1150 * of success and a negative error code in case of failure.
1152 int ubifs_lpt_start_commit(struct ubifs_info *c)
1158 mutex_lock(&c->lp_mutex);
1159 err = dbg_check_ltab(c);
1163 if (c->check_lpt_free) {
1165 * We ensure there is enough free space in
1166 * ubifs_lpt_post_commit() by marking nodes dirty. That
1167 * information is lost when we unmount, so we also need
1168 * to check free space once after mounting also.
1170 c->check_lpt_free = 0;
1171 while (need_write_all(c)) {
1172 mutex_unlock(&c->lp_mutex);
1176 mutex_lock(&c->lp_mutex);
1182 if (!c->dirty_pn_cnt) {
1183 dbg_cmt("no cnodes to commit");
1188 if (!c->big_lpt && need_write_all(c)) {
1189 /* If needed, write everything */
1190 err = make_tree_dirty(c);
1199 cnt = get_cnodes_to_commit(c);
1200 ubifs_assert(cnt != 0);
1202 err = layout_cnodes(c);
1206 /* Copy the LPT's own lprops for end commit to write */
1207 memcpy(c->ltab_cmt, c->ltab,
1208 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1209 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1212 mutex_unlock(&c->lp_mutex);
1217 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1218 * @c: UBIFS file-system description object
1220 static void free_obsolete_cnodes(struct ubifs_info *c)
1222 struct ubifs_cnode *cnode, *cnext;
1224 cnext = c->lpt_cnext;
1229 cnext = cnode->cnext;
1230 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1233 cnode->cnext = NULL;
1234 } while (cnext != c->lpt_cnext);
1235 c->lpt_cnext = NULL;
1239 * ubifs_lpt_end_commit - finish the commit operation.
1240 * @c: the UBIFS file-system description object
1242 * This function has to be called when the commit operation finishes. It
1243 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1244 * the media. Returns zero in case of success and a negative error code in case
1247 int ubifs_lpt_end_commit(struct ubifs_info *c)
1256 err = write_cnodes(c);
1260 mutex_lock(&c->lp_mutex);
1261 free_obsolete_cnodes(c);
1262 mutex_unlock(&c->lp_mutex);
1268 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1269 * @c: UBIFS file-system description object
1271 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1272 * commit for the "big" LPT model.
1274 int ubifs_lpt_post_commit(struct ubifs_info *c)
1278 mutex_lock(&c->lp_mutex);
1279 err = lpt_tgc_end(c);
1283 while (need_write_all(c)) {
1284 mutex_unlock(&c->lp_mutex);
1288 mutex_lock(&c->lp_mutex);
1291 mutex_unlock(&c->lp_mutex);
1296 * first_nnode - find the first nnode in memory.
1297 * @c: UBIFS file-system description object
1298 * @hght: height of tree where nnode found is returned here
1300 * This function returns a pointer to the nnode found or %NULL if no nnode is
1301 * found. This function is a helper to 'ubifs_lpt_free()'.
1303 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1305 struct ubifs_nnode *nnode;
1312 for (h = 1; h < c->lpt_hght; h++) {
1314 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1315 if (nnode->nbranch[i].nnode) {
1317 nnode = nnode->nbranch[i].nnode;
1329 * next_nnode - find the next nnode in memory.
1330 * @c: UBIFS file-system description object
1331 * @nnode: nnode from which to start.
1332 * @hght: height of tree where nnode is, is passed and returned here
1334 * This function returns a pointer to the nnode found or %NULL if no nnode is
1335 * found. This function is a helper to 'ubifs_lpt_free()'.
1337 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1338 struct ubifs_nnode *nnode, int *hght)
1340 struct ubifs_nnode *parent;
1341 int iip, h, i, found;
1343 parent = nnode->parent;
1346 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1350 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1351 nnode = parent->nbranch[iip].nnode;
1359 for (h = *hght + 1; h < c->lpt_hght; h++) {
1361 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1362 if (nnode->nbranch[i].nnode) {
1364 nnode = nnode->nbranch[i].nnode;
1376 * ubifs_lpt_free - free resources owned by the LPT.
1377 * @c: UBIFS file-system description object
1378 * @wr_only: free only resources used for writing
1380 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1382 struct ubifs_nnode *nnode;
1385 /* Free write-only things first */
1387 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1399 /* Now free the rest */
1401 nnode = first_nnode(c, &hght);
1403 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1404 kfree(nnode->nbranch[i].nnode);
1405 nnode = next_nnode(c, nnode, &hght);
1407 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1408 kfree(c->lpt_heap[i].arr);
1409 kfree(c->dirty_idx.arr);
1412 kfree(c->lpt_nod_buf);
1415 #ifdef CONFIG_UBIFS_FS_DEBUG
1418 * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
1420 * @len: buffer length
1422 static int dbg_is_all_ff(uint8_t *buf, int len)
1426 for (i = 0; i < len; i++)
1433 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1434 * @c: the UBIFS file-system description object
1435 * @lnum: LEB number where nnode was written
1436 * @offs: offset where nnode was written
1438 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1440 struct ubifs_nnode *nnode;
1443 /* Entire tree is in memory so first_nnode / next_nnode are ok */
1444 nnode = first_nnode(c, &hght);
1445 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1446 struct ubifs_nbranch *branch;
1449 if (nnode->parent) {
1450 branch = &nnode->parent->nbranch[nnode->iip];
1451 if (branch->lnum != lnum || branch->offs != offs)
1453 if (test_bit(DIRTY_CNODE, &nnode->flags))
1457 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1459 if (test_bit(DIRTY_CNODE, &nnode->flags))
1468 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1469 * @c: the UBIFS file-system description object
1470 * @lnum: LEB number where pnode was written
1471 * @offs: offset where pnode was written
1473 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1477 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1478 for (i = 0; i < cnt; i++) {
1479 struct ubifs_pnode *pnode;
1480 struct ubifs_nbranch *branch;
1483 pnode = pnode_lookup(c, i);
1485 return PTR_ERR(pnode);
1486 branch = &pnode->parent->nbranch[pnode->iip];
1487 if (branch->lnum != lnum || branch->offs != offs)
1489 if (test_bit(DIRTY_CNODE, &pnode->flags))
1497 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1498 * @c: the UBIFS file-system description object
1499 * @lnum: LEB number where ltab node was written
1500 * @offs: offset where ltab node was written
1502 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1504 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1506 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1510 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1511 * @c: the UBIFS file-system description object
1512 * @lnum: LEB number where lsave node was written
1513 * @offs: offset where lsave node was written
1515 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1517 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1519 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1523 * dbg_is_node_dirty - determine if a node is dirty.
1524 * @c: the UBIFS file-system description object
1525 * @node_type: node type
1526 * @lnum: LEB number where node was written
1527 * @offs: offset where node was written
1529 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1532 switch (node_type) {
1533 case UBIFS_LPT_NNODE:
1534 return dbg_is_nnode_dirty(c, lnum, offs);
1535 case UBIFS_LPT_PNODE:
1536 return dbg_is_pnode_dirty(c, lnum, offs);
1537 case UBIFS_LPT_LTAB:
1538 return dbg_is_ltab_dirty(c, lnum, offs);
1539 case UBIFS_LPT_LSAVE:
1540 return dbg_is_lsave_dirty(c, lnum, offs);
1546 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1547 * @c: the UBIFS file-system description object
1548 * @lnum: LEB number where node was written
1549 * @offs: offset where node was written
1551 * This function returns %0 on success and a negative error code on failure.
1553 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1555 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1557 void *buf = c->dbg_buf;
1559 dbg_lp("LEB %d", lnum);
1560 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1562 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1566 if (!is_a_node(c, buf, len)) {
1569 pad_len = get_pad_len(c, buf, len);
1576 if (!dbg_is_all_ff(buf, len)) {
1577 dbg_msg("invalid empty space in LEB %d at %d",
1578 lnum, c->leb_size - len);
1581 i = lnum - c->lpt_first;
1582 if (len != c->ltab[i].free) {
1583 dbg_msg("invalid free space in LEB %d "
1584 "(free %d, expected %d)",
1585 lnum, len, c->ltab[i].free);
1588 if (dirty != c->ltab[i].dirty) {
1589 dbg_msg("invalid dirty space in LEB %d "
1590 "(dirty %d, expected %d)",
1591 lnum, dirty, c->ltab[i].dirty);
1596 node_type = get_lpt_node_type(c, buf, &node_num);
1597 node_len = get_lpt_node_len(c, node_type);
1598 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1607 * dbg_check_ltab - check the free and dirty space in the ltab.
1608 * @c: the UBIFS file-system description object
1610 * This function returns %0 on success and a negative error code on failure.
1612 int dbg_check_ltab(struct ubifs_info *c)
1614 int lnum, err, i, cnt;
1616 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1619 /* Bring the entire tree into memory */
1620 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1621 for (i = 0; i < cnt; i++) {
1622 struct ubifs_pnode *pnode;
1624 pnode = pnode_lookup(c, i);
1626 return PTR_ERR(pnode);
1631 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1635 /* Check each LEB */
1636 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1637 err = dbg_check_ltab_lnum(c, lnum);
1639 dbg_err("failed at LEB %d", lnum);
1644 dbg_lp("succeeded");
1648 #endif /* CONFIG_UBIFS_FS_DEBUG */