PCI: Documentation: fix minor PCIe HOWTO thinko
[linux-2.6/mini2440.git] / fs / ubifs / lpt_commit.c
blob3216a1f277f8940799e090cf50ff0c6bb566352c
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 "ubifs.h"
31 /**
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)
40 ubifs_assert(nnode);
41 while (1) {
42 int i, cont = 0;
44 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
45 struct ubifs_cnode *cnode;
47 cnode = nnode->nbranch[i].cnode;
48 if (cnode &&
49 test_bit(DIRTY_CNODE, &cnode->flags)) {
50 if (cnode->level == 0)
51 return cnode;
52 nnode = (struct ubifs_nnode *)cnode;
53 cont = 1;
54 break;
57 if (!cont)
58 return (struct ubifs_cnode *)nnode;
62 /**
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;
71 int i;
73 ubifs_assert(cnode);
74 nnode = cnode->parent;
75 if (!nnode)
76 return NULL;
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;
89 /**
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;
98 int cnt = 0;
100 if (!c->nroot)
101 return 0;
103 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
104 return 0;
106 c->lpt_cnext = first_dirty_cnode(c->nroot);
107 cnode = c->lpt_cnext;
108 if (!cnode)
109 return 0;
110 cnt += 1;
111 while (1) {
112 ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
113 __set_bit(COW_ZNODE, &cnode->flags);
114 cnext = next_dirty_cnode(cnode);
115 if (!cnext) {
116 cnode->cnext = c->lpt_cnext;
117 break;
119 cnode->cnext = cnext;
120 cnode = cnext;
121 cnt += 1;
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);
126 return cnt;
130 * upd_ltab - update LPT LEB properties.
131 * @c: UBIFS file-system description object
132 * @lnum: LEB number
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)
158 int i, n;
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)
163 continue;
164 if (c->ltab[i].free == c->leb_size) {
165 c->ltab[i].cmt = 1;
166 *lnum = i + c->lpt_first;
167 return 0;
171 for (i = 0; i < n; i++) {
172 if (c->ltab[i].tgc || c->ltab[i].cmt)
173 continue;
174 if (c->ltab[i].free == c->leb_size) {
175 c->ltab[i].cmt = 1;
176 *lnum = i + c->lpt_first;
177 return 0;
180 return -ENOSPC;
184 * layout_cnodes - layout cnodes for commit.
185 * @c: UBIFS file-system description object
187 * This function returns %0 on success and a negative error code on failure.
189 static int layout_cnodes(struct ubifs_info *c)
191 int lnum, offs, len, alen, done_lsave, done_ltab, err;
192 struct ubifs_cnode *cnode;
194 err = dbg_chk_lpt_sz(c, 0, 0);
195 if (err)
196 return err;
197 cnode = c->lpt_cnext;
198 if (!cnode)
199 return 0;
200 lnum = c->nhead_lnum;
201 offs = c->nhead_offs;
202 /* Try to place lsave and ltab nicely */
203 done_lsave = !c->big_lpt;
204 done_ltab = 0;
205 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
206 done_lsave = 1;
207 c->lsave_lnum = lnum;
208 c->lsave_offs = offs;
209 offs += c->lsave_sz;
210 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
213 if (offs + c->ltab_sz <= c->leb_size) {
214 done_ltab = 1;
215 c->ltab_lnum = lnum;
216 c->ltab_offs = offs;
217 offs += c->ltab_sz;
218 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
221 do {
222 if (cnode->level) {
223 len = c->nnode_sz;
224 c->dirty_nn_cnt -= 1;
225 } else {
226 len = c->pnode_sz;
227 c->dirty_pn_cnt -= 1;
229 while (offs + len > c->leb_size) {
230 alen = ALIGN(offs, c->min_io_size);
231 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
232 dbg_chk_lpt_sz(c, 2, alen - offs);
233 err = alloc_lpt_leb(c, &lnum);
234 if (err)
235 goto no_space;
236 offs = 0;
237 ubifs_assert(lnum >= c->lpt_first &&
238 lnum <= c->lpt_last);
239 /* Try to place lsave and ltab nicely */
240 if (!done_lsave) {
241 done_lsave = 1;
242 c->lsave_lnum = lnum;
243 c->lsave_offs = offs;
244 offs += c->lsave_sz;
245 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
246 continue;
248 if (!done_ltab) {
249 done_ltab = 1;
250 c->ltab_lnum = lnum;
251 c->ltab_offs = offs;
252 offs += c->ltab_sz;
253 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
254 continue;
256 break;
258 if (cnode->parent) {
259 cnode->parent->nbranch[cnode->iip].lnum = lnum;
260 cnode->parent->nbranch[cnode->iip].offs = offs;
261 } else {
262 c->lpt_lnum = lnum;
263 c->lpt_offs = offs;
265 offs += len;
266 dbg_chk_lpt_sz(c, 1, len);
267 cnode = cnode->cnext;
268 } while (cnode && cnode != c->lpt_cnext);
270 /* Make sure to place LPT's save table */
271 if (!done_lsave) {
272 if (offs + c->lsave_sz > c->leb_size) {
273 alen = ALIGN(offs, c->min_io_size);
274 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
275 dbg_chk_lpt_sz(c, 2, alen - offs);
276 err = alloc_lpt_leb(c, &lnum);
277 if (err)
278 goto no_space;
279 offs = 0;
280 ubifs_assert(lnum >= c->lpt_first &&
281 lnum <= c->lpt_last);
283 done_lsave = 1;
284 c->lsave_lnum = lnum;
285 c->lsave_offs = offs;
286 offs += c->lsave_sz;
287 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
290 /* Make sure to place LPT's own lprops table */
291 if (!done_ltab) {
292 if (offs + c->ltab_sz > c->leb_size) {
293 alen = ALIGN(offs, c->min_io_size);
294 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
295 dbg_chk_lpt_sz(c, 2, alen - offs);
296 err = alloc_lpt_leb(c, &lnum);
297 if (err)
298 goto no_space;
299 offs = 0;
300 ubifs_assert(lnum >= c->lpt_first &&
301 lnum <= c->lpt_last);
303 done_ltab = 1;
304 c->ltab_lnum = lnum;
305 c->ltab_offs = offs;
306 offs += c->ltab_sz;
307 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
310 alen = ALIGN(offs, c->min_io_size);
311 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
312 dbg_chk_lpt_sz(c, 4, alen - offs);
313 err = dbg_chk_lpt_sz(c, 3, alen);
314 if (err)
315 return err;
316 return 0;
318 no_space:
319 ubifs_err("LPT out of space");
320 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
321 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
322 dbg_dump_lpt_info(c);
323 dbg_dump_lpt_lebs(c);
324 dump_stack();
325 return err;
329 * realloc_lpt_leb - allocate an LPT LEB that is empty.
330 * @c: UBIFS file-system description object
331 * @lnum: LEB number is passed and returned here
333 * This function duplicates exactly the results of the function alloc_lpt_leb.
334 * It is used during end commit to reallocate the same LEB numbers that were
335 * allocated by alloc_lpt_leb during start commit.
337 * This function finds the next LEB that was allocated by the alloc_lpt_leb
338 * function starting from @lnum. If a LEB is found it is returned in @lnum and
339 * the function returns %0. Otherwise the function returns -ENOSPC.
340 * Note however, that LPT is designed never to run out of space.
342 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
344 int i, n;
346 n = *lnum - c->lpt_first + 1;
347 for (i = n; i < c->lpt_lebs; i++)
348 if (c->ltab[i].cmt) {
349 c->ltab[i].cmt = 0;
350 *lnum = i + c->lpt_first;
351 return 0;
354 for (i = 0; i < n; i++)
355 if (c->ltab[i].cmt) {
356 c->ltab[i].cmt = 0;
357 *lnum = i + c->lpt_first;
358 return 0;
360 return -ENOSPC;
364 * write_cnodes - write cnodes for commit.
365 * @c: UBIFS file-system description object
367 * This function returns %0 on success and a negative error code on failure.
369 static int write_cnodes(struct ubifs_info *c)
371 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
372 struct ubifs_cnode *cnode;
373 void *buf = c->lpt_buf;
375 cnode = c->lpt_cnext;
376 if (!cnode)
377 return 0;
378 lnum = c->nhead_lnum;
379 offs = c->nhead_offs;
380 from = offs;
381 /* Ensure empty LEB is unmapped */
382 if (offs == 0) {
383 err = ubifs_leb_unmap(c, lnum);
384 if (err)
385 return err;
387 /* Try to place lsave and ltab nicely */
388 done_lsave = !c->big_lpt;
389 done_ltab = 0;
390 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
391 done_lsave = 1;
392 ubifs_pack_lsave(c, buf + offs, c->lsave);
393 offs += c->lsave_sz;
394 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
397 if (offs + c->ltab_sz <= c->leb_size) {
398 done_ltab = 1;
399 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
400 offs += c->ltab_sz;
401 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
404 /* Loop for each cnode */
405 do {
406 if (cnode->level)
407 len = c->nnode_sz;
408 else
409 len = c->pnode_sz;
410 while (offs + len > c->leb_size) {
411 wlen = offs - from;
412 if (wlen) {
413 alen = ALIGN(wlen, c->min_io_size);
414 memset(buf + offs, 0xff, alen - wlen);
415 err = ubifs_leb_write(c, lnum, buf + from, from,
416 alen, UBI_SHORTTERM);
417 if (err)
418 return err;
419 dbg_chk_lpt_sz(c, 4, alen - wlen);
421 dbg_chk_lpt_sz(c, 2, 0);
422 err = realloc_lpt_leb(c, &lnum);
423 if (err)
424 goto no_space;
425 offs = 0;
426 from = 0;
427 ubifs_assert(lnum >= c->lpt_first &&
428 lnum <= c->lpt_last);
429 err = ubifs_leb_unmap(c, lnum);
430 if (err)
431 return err;
432 /* Try to place lsave and ltab nicely */
433 if (!done_lsave) {
434 done_lsave = 1;
435 ubifs_pack_lsave(c, buf + offs, c->lsave);
436 offs += c->lsave_sz;
437 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
438 continue;
440 if (!done_ltab) {
441 done_ltab = 1;
442 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
443 offs += c->ltab_sz;
444 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
445 continue;
447 break;
449 if (cnode->level)
450 ubifs_pack_nnode(c, buf + offs,
451 (struct ubifs_nnode *)cnode);
452 else
453 ubifs_pack_pnode(c, buf + offs,
454 (struct ubifs_pnode *)cnode);
456 * The reason for the barriers is the same as in case of TNC.
457 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
458 * 'dirty_cow_pnode()' are the functions for which this is
459 * important.
461 clear_bit(DIRTY_CNODE, &cnode->flags);
462 smp_mb__before_clear_bit();
463 clear_bit(COW_ZNODE, &cnode->flags);
464 smp_mb__after_clear_bit();
465 offs += len;
466 dbg_chk_lpt_sz(c, 1, len);
467 cnode = cnode->cnext;
468 } while (cnode && cnode != c->lpt_cnext);
470 /* Make sure to place LPT's save table */
471 if (!done_lsave) {
472 if (offs + c->lsave_sz > c->leb_size) {
473 wlen = offs - from;
474 alen = ALIGN(wlen, c->min_io_size);
475 memset(buf + offs, 0xff, alen - wlen);
476 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
477 UBI_SHORTTERM);
478 if (err)
479 return err;
480 dbg_chk_lpt_sz(c, 2, alen - wlen);
481 err = realloc_lpt_leb(c, &lnum);
482 if (err)
483 goto no_space;
484 offs = 0;
485 ubifs_assert(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 UBI_SHORTTERM);
505 if (err)
506 return err;
507 dbg_chk_lpt_sz(c, 2, alen - wlen);
508 err = realloc_lpt_leb(c, &lnum);
509 if (err)
510 goto no_space;
511 offs = 0;
512 ubifs_assert(lnum >= c->lpt_first &&
513 lnum <= c->lpt_last);
514 err = ubifs_leb_unmap(c, lnum);
515 if (err)
516 return err;
518 done_ltab = 1;
519 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
520 offs += c->ltab_sz;
521 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
524 /* Write remaining data in buffer */
525 wlen = offs - from;
526 alen = ALIGN(wlen, c->min_io_size);
527 memset(buf + offs, 0xff, alen - wlen);
528 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
529 if (err)
530 return err;
532 dbg_chk_lpt_sz(c, 4, alen - wlen);
533 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
534 if (err)
535 return err;
537 c->nhead_lnum = lnum;
538 c->nhead_offs = ALIGN(offs, c->min_io_size);
540 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
541 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
542 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
543 if (c->big_lpt)
544 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
546 return 0;
548 no_space:
549 ubifs_err("LPT out of space mismatch");
550 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
551 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
552 dbg_dump_lpt_info(c);
553 dbg_dump_lpt_lebs(c);
554 dump_stack();
555 return err;
559 * next_pnode_to_dirty - find next pnode to dirty.
560 * @c: UBIFS file-system description object
561 * @pnode: pnode
563 * This function returns the next pnode to dirty or %NULL if there are no more
564 * pnodes. Note that pnodes that have never been written (lnum == 0) are
565 * skipped.
567 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
568 struct ubifs_pnode *pnode)
570 struct ubifs_nnode *nnode;
571 int iip;
573 /* Try to go right */
574 nnode = pnode->parent;
575 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
576 if (nnode->nbranch[iip].lnum)
577 return ubifs_get_pnode(c, nnode, iip);
580 /* Go up while can't go right */
581 do {
582 iip = nnode->iip + 1;
583 nnode = nnode->parent;
584 if (!nnode)
585 return NULL;
586 for (; iip < UBIFS_LPT_FANOUT; iip++) {
587 if (nnode->nbranch[iip].lnum)
588 break;
590 } while (iip >= UBIFS_LPT_FANOUT);
592 /* Go right */
593 nnode = ubifs_get_nnode(c, nnode, iip);
594 if (IS_ERR(nnode))
595 return (void *)nnode;
597 /* Go down to level 1 */
598 while (nnode->level > 1) {
599 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
600 if (nnode->nbranch[iip].lnum)
601 break;
603 if (iip >= UBIFS_LPT_FANOUT) {
605 * Should not happen, but we need to keep going
606 * if it does.
608 iip = 0;
610 nnode = ubifs_get_nnode(c, nnode, iip);
611 if (IS_ERR(nnode))
612 return (void *)nnode;
615 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
616 if (nnode->nbranch[iip].lnum)
617 break;
618 if (iip >= UBIFS_LPT_FANOUT)
619 /* Should not happen, but we need to keep going if it does */
620 iip = 0;
621 return ubifs_get_pnode(c, nnode, iip);
625 * pnode_lookup - lookup a pnode in the LPT.
626 * @c: UBIFS file-system description object
627 * @i: pnode number (0 to main_lebs - 1)
629 * This function returns a pointer to the pnode on success or a negative
630 * error code on failure.
632 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
634 int err, h, iip, shft;
635 struct ubifs_nnode *nnode;
637 if (!c->nroot) {
638 err = ubifs_read_nnode(c, NULL, 0);
639 if (err)
640 return ERR_PTR(err);
642 i <<= UBIFS_LPT_FANOUT_SHIFT;
643 nnode = c->nroot;
644 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
645 for (h = 1; h < c->lpt_hght; h++) {
646 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
647 shft -= UBIFS_LPT_FANOUT_SHIFT;
648 nnode = ubifs_get_nnode(c, nnode, iip);
649 if (IS_ERR(nnode))
650 return ERR_PTR(PTR_ERR(nnode));
652 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
653 return ubifs_get_pnode(c, nnode, iip);
657 * add_pnode_dirt - add dirty space to LPT LEB properties.
658 * @c: UBIFS file-system description object
659 * @pnode: pnode for which to add dirt
661 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
663 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
664 c->pnode_sz);
668 * do_make_pnode_dirty - mark a pnode dirty.
669 * @c: UBIFS file-system description object
670 * @pnode: pnode to mark dirty
672 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
674 /* Assumes cnext list is empty i.e. not called during commit */
675 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
676 struct ubifs_nnode *nnode;
678 c->dirty_pn_cnt += 1;
679 add_pnode_dirt(c, pnode);
680 /* Mark parent and ancestors dirty too */
681 nnode = pnode->parent;
682 while (nnode) {
683 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
684 c->dirty_nn_cnt += 1;
685 ubifs_add_nnode_dirt(c, nnode);
686 nnode = nnode->parent;
687 } else
688 break;
694 * make_tree_dirty - mark the entire LEB properties tree dirty.
695 * @c: UBIFS file-system description object
697 * This function is used by the "small" LPT model to cause the entire LEB
698 * properties tree to be written. The "small" LPT model does not use LPT
699 * garbage collection because it is more efficient to write the entire tree
700 * (because it is small).
702 * This function returns %0 on success and a negative error code on failure.
704 static int make_tree_dirty(struct ubifs_info *c)
706 struct ubifs_pnode *pnode;
708 pnode = pnode_lookup(c, 0);
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->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);
816 list_for_each_entry(lprops, &c->empty_list, list) {
817 c->lsave[cnt++] = lprops->lnum;
818 if (cnt >= c->lsave_cnt)
819 return;
821 list_for_each_entry(lprops, &c->freeable_list, list) {
822 c->lsave[cnt++] = lprops->lnum;
823 if (cnt >= c->lsave_cnt)
824 return;
826 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
827 c->lsave[cnt++] = lprops->lnum;
828 if (cnt >= c->lsave_cnt)
829 return;
831 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
832 for (i = 0; i < heap->cnt; i++) {
833 c->lsave[cnt++] = heap->arr[i]->lnum;
834 if (cnt >= c->lsave_cnt)
835 return;
837 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
838 for (i = 0; i < heap->cnt; i++) {
839 c->lsave[cnt++] = heap->arr[i]->lnum;
840 if (cnt >= c->lsave_cnt)
841 return;
843 heap = &c->lpt_heap[LPROPS_FREE - 1];
844 for (i = 0; i < heap->cnt; i++) {
845 c->lsave[cnt++] = heap->arr[i]->lnum;
846 if (cnt >= c->lsave_cnt)
847 return;
849 /* Fill it up completely */
850 while (cnt < c->lsave_cnt)
851 c->lsave[cnt++] = c->main_first;
855 * nnode_lookup - lookup a nnode in the LPT.
856 * @c: UBIFS file-system description object
857 * @i: nnode number
859 * This function returns a pointer to the nnode on success or a negative
860 * error code on failure.
862 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
864 int err, iip;
865 struct ubifs_nnode *nnode;
867 if (!c->nroot) {
868 err = ubifs_read_nnode(c, NULL, 0);
869 if (err)
870 return ERR_PTR(err);
872 nnode = c->nroot;
873 while (1) {
874 iip = i & (UBIFS_LPT_FANOUT - 1);
875 i >>= UBIFS_LPT_FANOUT_SHIFT;
876 if (!i)
877 break;
878 nnode = ubifs_get_nnode(c, nnode, iip);
879 if (IS_ERR(nnode))
880 return nnode;
882 return nnode;
886 * make_nnode_dirty - find a nnode and, if found, make it dirty.
887 * @c: UBIFS file-system description object
888 * @node_num: nnode number of nnode to make dirty
889 * @lnum: LEB number where nnode was written
890 * @offs: offset where nnode was written
892 * This function is used by LPT garbage collection. LPT garbage collection is
893 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
894 * simply involves marking all the nodes in the LEB being garbage-collected as
895 * dirty. The dirty nodes are written next commit, after which the LEB is free
896 * to be reused.
898 * This function returns %0 on success and a negative error code on failure.
900 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
901 int offs)
903 struct ubifs_nnode *nnode;
905 nnode = nnode_lookup(c, node_num);
906 if (IS_ERR(nnode))
907 return PTR_ERR(nnode);
908 if (nnode->parent) {
909 struct ubifs_nbranch *branch;
911 branch = &nnode->parent->nbranch[nnode->iip];
912 if (branch->lnum != lnum || branch->offs != offs)
913 return 0; /* nnode is obsolete */
914 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
915 return 0; /* nnode is obsolete */
916 /* Assumes cnext list is empty i.e. not called during commit */
917 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
918 c->dirty_nn_cnt += 1;
919 ubifs_add_nnode_dirt(c, nnode);
920 /* Mark parent and ancestors dirty too */
921 nnode = nnode->parent;
922 while (nnode) {
923 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
924 c->dirty_nn_cnt += 1;
925 ubifs_add_nnode_dirt(c, nnode);
926 nnode = nnode->parent;
927 } else
928 break;
931 return 0;
935 * make_pnode_dirty - find a pnode and, if found, make it dirty.
936 * @c: UBIFS file-system description object
937 * @node_num: pnode number of pnode to make dirty
938 * @lnum: LEB number where pnode was written
939 * @offs: offset where pnode was written
941 * This function is used by LPT garbage collection. LPT garbage collection is
942 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
943 * simply involves marking all the nodes in the LEB being garbage-collected as
944 * dirty. The dirty nodes are written next commit, after which the LEB is free
945 * to be reused.
947 * This function returns %0 on success and a negative error code on failure.
949 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
950 int offs)
952 struct ubifs_pnode *pnode;
953 struct ubifs_nbranch *branch;
955 pnode = pnode_lookup(c, node_num);
956 if (IS_ERR(pnode))
957 return PTR_ERR(pnode);
958 branch = &pnode->parent->nbranch[pnode->iip];
959 if (branch->lnum != lnum || branch->offs != offs)
960 return 0;
961 do_make_pnode_dirty(c, pnode);
962 return 0;
966 * make_ltab_dirty - make ltab node dirty.
967 * @c: UBIFS file-system description object
968 * @lnum: LEB number where ltab was written
969 * @offs: offset where ltab was written
971 * This function is used by LPT garbage collection. LPT garbage collection is
972 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
973 * simply involves marking all the nodes in the LEB being garbage-collected as
974 * dirty. The dirty nodes are written next commit, after which the LEB is free
975 * to be reused.
977 * This function returns %0 on success and a negative error code on failure.
979 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
981 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
982 return 0; /* This ltab node is obsolete */
983 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
984 c->lpt_drty_flgs |= LTAB_DIRTY;
985 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
987 return 0;
991 * make_lsave_dirty - make lsave node dirty.
992 * @c: UBIFS file-system description object
993 * @lnum: LEB number where lsave was written
994 * @offs: offset where lsave was written
996 * This function is used by LPT garbage collection. LPT garbage collection is
997 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
998 * simply involves marking all the nodes in the LEB being garbage-collected as
999 * dirty. The dirty nodes are written next commit, after which the LEB is free
1000 * to be reused.
1002 * This function returns %0 on success and a negative error code on failure.
1004 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1006 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1007 return 0; /* This lsave node is obsolete */
1008 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1009 c->lpt_drty_flgs |= LSAVE_DIRTY;
1010 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1012 return 0;
1016 * make_node_dirty - make node dirty.
1017 * @c: UBIFS file-system description object
1018 * @node_type: LPT node type
1019 * @node_num: node number
1020 * @lnum: LEB number where node was written
1021 * @offs: offset where node was written
1023 * This function is used by LPT garbage collection. LPT garbage collection is
1024 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1025 * simply involves marking all the nodes in the LEB being garbage-collected as
1026 * dirty. The dirty nodes are written next commit, after which the LEB is free
1027 * to be reused.
1029 * This function returns %0 on success and a negative error code on failure.
1031 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1032 int lnum, int offs)
1034 switch (node_type) {
1035 case UBIFS_LPT_NNODE:
1036 return make_nnode_dirty(c, node_num, lnum, offs);
1037 case UBIFS_LPT_PNODE:
1038 return make_pnode_dirty(c, node_num, lnum, offs);
1039 case UBIFS_LPT_LTAB:
1040 return make_ltab_dirty(c, lnum, offs);
1041 case UBIFS_LPT_LSAVE:
1042 return make_lsave_dirty(c, lnum, offs);
1044 return -EINVAL;
1048 * get_lpt_node_len - return the length of a node based on its type.
1049 * @c: UBIFS file-system description object
1050 * @node_type: LPT node type
1052 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1054 switch (node_type) {
1055 case UBIFS_LPT_NNODE:
1056 return c->nnode_sz;
1057 case UBIFS_LPT_PNODE:
1058 return c->pnode_sz;
1059 case UBIFS_LPT_LTAB:
1060 return c->ltab_sz;
1061 case UBIFS_LPT_LSAVE:
1062 return c->lsave_sz;
1064 return 0;
1068 * get_pad_len - return the length of padding in a buffer.
1069 * @c: UBIFS file-system description object
1070 * @buf: buffer
1071 * @len: length of buffer
1073 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1075 int offs, pad_len;
1077 if (c->min_io_size == 1)
1078 return 0;
1079 offs = c->leb_size - len;
1080 pad_len = ALIGN(offs, c->min_io_size) - offs;
1081 return pad_len;
1085 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1086 * @c: UBIFS file-system description object
1087 * @buf: buffer
1088 * @node_num: node number is returned here
1090 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1091 int *node_num)
1093 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1094 int pos = 0, node_type;
1096 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1097 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1098 return node_type;
1102 * is_a_node - determine if a buffer contains a node.
1103 * @c: UBIFS file-system description object
1104 * @buf: buffer
1105 * @len: length of buffer
1107 * This function returns %1 if the buffer contains a node or %0 if it does not.
1109 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1111 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1112 int pos = 0, node_type, node_len;
1113 uint16_t crc, calc_crc;
1115 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1116 return 0;
1117 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1118 if (node_type == UBIFS_LPT_NOT_A_NODE)
1119 return 0;
1120 node_len = get_lpt_node_len(c, node_type);
1121 if (!node_len || node_len > len)
1122 return 0;
1123 pos = 0;
1124 addr = buf;
1125 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1126 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1127 node_len - UBIFS_LPT_CRC_BYTES);
1128 if (crc != calc_crc)
1129 return 0;
1130 return 1;
1134 * lpt_gc_lnum - garbage collect a LPT LEB.
1135 * @c: UBIFS file-system description object
1136 * @lnum: LEB number to garbage collect
1138 * LPT garbage collection is used only for the "big" LPT model
1139 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1140 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1141 * next commit, after which the LEB is free to be reused.
1143 * This function returns %0 on success and a negative error code on failure.
1145 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1147 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1148 void *buf = c->lpt_buf;
1150 dbg_lp("LEB %d", lnum);
1151 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1152 if (err) {
1153 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1154 return err;
1156 while (1) {
1157 if (!is_a_node(c, buf, len)) {
1158 int pad_len;
1160 pad_len = get_pad_len(c, buf, len);
1161 if (pad_len) {
1162 buf += pad_len;
1163 len -= pad_len;
1164 continue;
1166 return 0;
1168 node_type = get_lpt_node_type(c, buf, &node_num);
1169 node_len = get_lpt_node_len(c, node_type);
1170 offs = c->leb_size - len;
1171 ubifs_assert(node_len != 0);
1172 mutex_lock(&c->lp_mutex);
1173 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1174 mutex_unlock(&c->lp_mutex);
1175 if (err)
1176 return err;
1177 buf += node_len;
1178 len -= node_len;
1180 return 0;
1184 * lpt_gc - LPT garbage collection.
1185 * @c: UBIFS file-system description object
1187 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1188 * Returns %0 on success and a negative error code on failure.
1190 static int lpt_gc(struct ubifs_info *c)
1192 int i, lnum = -1, dirty = 0;
1194 mutex_lock(&c->lp_mutex);
1195 for (i = 0; i < c->lpt_lebs; i++) {
1196 ubifs_assert(!c->ltab[i].tgc);
1197 if (i + c->lpt_first == c->nhead_lnum ||
1198 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1199 continue;
1200 if (c->ltab[i].dirty > dirty) {
1201 dirty = c->ltab[i].dirty;
1202 lnum = i + c->lpt_first;
1205 mutex_unlock(&c->lp_mutex);
1206 if (lnum == -1)
1207 return -ENOSPC;
1208 return lpt_gc_lnum(c, lnum);
1212 * ubifs_lpt_start_commit - UBIFS commit starts.
1213 * @c: the UBIFS file-system description object
1215 * This function has to be called when UBIFS starts the commit operation.
1216 * This function "freezes" all currently dirty LEB properties and does not
1217 * change them anymore. Further changes are saved and tracked separately
1218 * because they are not part of this commit. This function returns zero in case
1219 * of success and a negative error code in case of failure.
1221 int ubifs_lpt_start_commit(struct ubifs_info *c)
1223 int err, cnt;
1225 dbg_lp("");
1227 mutex_lock(&c->lp_mutex);
1228 err = dbg_chk_lpt_free_spc(c);
1229 if (err)
1230 goto out;
1231 err = dbg_check_ltab(c);
1232 if (err)
1233 goto out;
1235 if (c->check_lpt_free) {
1237 * We ensure there is enough free space in
1238 * ubifs_lpt_post_commit() by marking nodes dirty. That
1239 * information is lost when we unmount, so we also need
1240 * to check free space once after mounting also.
1242 c->check_lpt_free = 0;
1243 while (need_write_all(c)) {
1244 mutex_unlock(&c->lp_mutex);
1245 err = lpt_gc(c);
1246 if (err)
1247 return err;
1248 mutex_lock(&c->lp_mutex);
1252 lpt_tgc_start(c);
1254 if (!c->dirty_pn_cnt) {
1255 dbg_cmt("no cnodes to commit");
1256 err = 0;
1257 goto out;
1260 if (!c->big_lpt && need_write_all(c)) {
1261 /* If needed, write everything */
1262 err = make_tree_dirty(c);
1263 if (err)
1264 goto out;
1265 lpt_tgc_start(c);
1268 if (c->big_lpt)
1269 populate_lsave(c);
1271 cnt = get_cnodes_to_commit(c);
1272 ubifs_assert(cnt != 0);
1274 err = layout_cnodes(c);
1275 if (err)
1276 goto out;
1278 /* Copy the LPT's own lprops for end commit to write */
1279 memcpy(c->ltab_cmt, c->ltab,
1280 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1281 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1283 out:
1284 mutex_unlock(&c->lp_mutex);
1285 return err;
1289 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1290 * @c: UBIFS file-system description object
1292 static void free_obsolete_cnodes(struct ubifs_info *c)
1294 struct ubifs_cnode *cnode, *cnext;
1296 cnext = c->lpt_cnext;
1297 if (!cnext)
1298 return;
1299 do {
1300 cnode = cnext;
1301 cnext = cnode->cnext;
1302 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1303 kfree(cnode);
1304 else
1305 cnode->cnext = NULL;
1306 } while (cnext != c->lpt_cnext);
1307 c->lpt_cnext = NULL;
1311 * ubifs_lpt_end_commit - finish the commit operation.
1312 * @c: the UBIFS file-system description object
1314 * This function has to be called when the commit operation finishes. It
1315 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1316 * the media. Returns zero in case of success and a negative error code in case
1317 * of failure.
1319 int ubifs_lpt_end_commit(struct ubifs_info *c)
1321 int err;
1323 dbg_lp("");
1325 if (!c->lpt_cnext)
1326 return 0;
1328 err = write_cnodes(c);
1329 if (err)
1330 return err;
1332 mutex_lock(&c->lp_mutex);
1333 free_obsolete_cnodes(c);
1334 mutex_unlock(&c->lp_mutex);
1336 return 0;
1340 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1341 * @c: UBIFS file-system description object
1343 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1344 * commit for the "big" LPT model.
1346 int ubifs_lpt_post_commit(struct ubifs_info *c)
1348 int err;
1350 mutex_lock(&c->lp_mutex);
1351 err = lpt_tgc_end(c);
1352 if (err)
1353 goto out;
1354 if (c->big_lpt)
1355 while (need_write_all(c)) {
1356 mutex_unlock(&c->lp_mutex);
1357 err = lpt_gc(c);
1358 if (err)
1359 return err;
1360 mutex_lock(&c->lp_mutex);
1362 out:
1363 mutex_unlock(&c->lp_mutex);
1364 return err;
1368 * first_nnode - find the first nnode in memory.
1369 * @c: UBIFS file-system description object
1370 * @hght: height of tree where nnode found is returned here
1372 * This function returns a pointer to the nnode found or %NULL if no nnode is
1373 * found. This function is a helper to 'ubifs_lpt_free()'.
1375 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1377 struct ubifs_nnode *nnode;
1378 int h, i, found;
1380 nnode = c->nroot;
1381 *hght = 0;
1382 if (!nnode)
1383 return NULL;
1384 for (h = 1; h < c->lpt_hght; h++) {
1385 found = 0;
1386 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1387 if (nnode->nbranch[i].nnode) {
1388 found = 1;
1389 nnode = nnode->nbranch[i].nnode;
1390 *hght = h;
1391 break;
1394 if (!found)
1395 break;
1397 return nnode;
1401 * next_nnode - find the next nnode in memory.
1402 * @c: UBIFS file-system description object
1403 * @nnode: nnode from which to start.
1404 * @hght: height of tree where nnode is, is passed and returned here
1406 * This function returns a pointer to the nnode found or %NULL if no nnode is
1407 * found. This function is a helper to 'ubifs_lpt_free()'.
1409 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1410 struct ubifs_nnode *nnode, int *hght)
1412 struct ubifs_nnode *parent;
1413 int iip, h, i, found;
1415 parent = nnode->parent;
1416 if (!parent)
1417 return NULL;
1418 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1419 *hght -= 1;
1420 return parent;
1422 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1423 nnode = parent->nbranch[iip].nnode;
1424 if (nnode)
1425 break;
1427 if (!nnode) {
1428 *hght -= 1;
1429 return parent;
1431 for (h = *hght + 1; h < c->lpt_hght; h++) {
1432 found = 0;
1433 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1434 if (nnode->nbranch[i].nnode) {
1435 found = 1;
1436 nnode = nnode->nbranch[i].nnode;
1437 *hght = h;
1438 break;
1441 if (!found)
1442 break;
1444 return nnode;
1448 * ubifs_lpt_free - free resources owned by the LPT.
1449 * @c: UBIFS file-system description object
1450 * @wr_only: free only resources used for writing
1452 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1454 struct ubifs_nnode *nnode;
1455 int i, hght;
1457 /* Free write-only things first */
1459 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1461 vfree(c->ltab_cmt);
1462 c->ltab_cmt = NULL;
1463 vfree(c->lpt_buf);
1464 c->lpt_buf = NULL;
1465 kfree(c->lsave);
1466 c->lsave = NULL;
1468 if (wr_only)
1469 return;
1471 /* Now free the rest */
1473 nnode = first_nnode(c, &hght);
1474 while (nnode) {
1475 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1476 kfree(nnode->nbranch[i].nnode);
1477 nnode = next_nnode(c, nnode, &hght);
1479 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1480 kfree(c->lpt_heap[i].arr);
1481 kfree(c->dirty_idx.arr);
1482 kfree(c->nroot);
1483 vfree(c->ltab);
1484 kfree(c->lpt_nod_buf);
1487 #ifdef CONFIG_UBIFS_FS_DEBUG
1490 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1491 * @buf: buffer
1492 * @len: buffer length
1494 static int dbg_is_all_ff(uint8_t *buf, int len)
1496 int i;
1498 for (i = 0; i < len; i++)
1499 if (buf[i] != 0xff)
1500 return 0;
1501 return 1;
1505 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1506 * @c: the UBIFS file-system description object
1507 * @lnum: LEB number where nnode was written
1508 * @offs: offset where nnode was written
1510 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1512 struct ubifs_nnode *nnode;
1513 int hght;
1515 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1516 nnode = first_nnode(c, &hght);
1517 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1518 struct ubifs_nbranch *branch;
1520 cond_resched();
1521 if (nnode->parent) {
1522 branch = &nnode->parent->nbranch[nnode->iip];
1523 if (branch->lnum != lnum || branch->offs != offs)
1524 continue;
1525 if (test_bit(DIRTY_CNODE, &nnode->flags))
1526 return 1;
1527 return 0;
1528 } else {
1529 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1530 continue;
1531 if (test_bit(DIRTY_CNODE, &nnode->flags))
1532 return 1;
1533 return 0;
1536 return 1;
1540 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1541 * @c: the UBIFS file-system description object
1542 * @lnum: LEB number where pnode was written
1543 * @offs: offset where pnode was written
1545 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1547 int i, cnt;
1549 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1550 for (i = 0; i < cnt; i++) {
1551 struct ubifs_pnode *pnode;
1552 struct ubifs_nbranch *branch;
1554 cond_resched();
1555 pnode = pnode_lookup(c, i);
1556 if (IS_ERR(pnode))
1557 return PTR_ERR(pnode);
1558 branch = &pnode->parent->nbranch[pnode->iip];
1559 if (branch->lnum != lnum || branch->offs != offs)
1560 continue;
1561 if (test_bit(DIRTY_CNODE, &pnode->flags))
1562 return 1;
1563 return 0;
1565 return 1;
1569 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1570 * @c: the UBIFS file-system description object
1571 * @lnum: LEB number where ltab node was written
1572 * @offs: offset where ltab node was written
1574 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1576 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1577 return 1;
1578 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1582 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1583 * @c: the UBIFS file-system description object
1584 * @lnum: LEB number where lsave node was written
1585 * @offs: offset where lsave node was written
1587 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1589 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1590 return 1;
1591 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1595 * dbg_is_node_dirty - determine if a node is dirty.
1596 * @c: the UBIFS file-system description object
1597 * @node_type: node type
1598 * @lnum: LEB number where node was written
1599 * @offs: offset where node was written
1601 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1602 int offs)
1604 switch (node_type) {
1605 case UBIFS_LPT_NNODE:
1606 return dbg_is_nnode_dirty(c, lnum, offs);
1607 case UBIFS_LPT_PNODE:
1608 return dbg_is_pnode_dirty(c, lnum, offs);
1609 case UBIFS_LPT_LTAB:
1610 return dbg_is_ltab_dirty(c, lnum, offs);
1611 case UBIFS_LPT_LSAVE:
1612 return dbg_is_lsave_dirty(c, lnum, offs);
1614 return 1;
1618 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1619 * @c: the UBIFS file-system description object
1620 * @lnum: LEB number where node was written
1621 * @offs: offset where node was written
1623 * This function returns %0 on success and a negative error code on failure.
1625 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1627 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1628 int ret;
1629 void *buf = c->dbg->buf;
1631 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1632 return 0;
1634 dbg_lp("LEB %d", lnum);
1635 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1636 if (err) {
1637 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1638 return err;
1640 while (1) {
1641 if (!is_a_node(c, buf, len)) {
1642 int i, pad_len;
1644 pad_len = get_pad_len(c, buf, len);
1645 if (pad_len) {
1646 buf += pad_len;
1647 len -= pad_len;
1648 dirty += pad_len;
1649 continue;
1651 if (!dbg_is_all_ff(buf, len)) {
1652 dbg_msg("invalid empty space in LEB %d at %d",
1653 lnum, c->leb_size - len);
1654 err = -EINVAL;
1656 i = lnum - c->lpt_first;
1657 if (len != c->ltab[i].free) {
1658 dbg_msg("invalid free space in LEB %d "
1659 "(free %d, expected %d)",
1660 lnum, len, c->ltab[i].free);
1661 err = -EINVAL;
1663 if (dirty != c->ltab[i].dirty) {
1664 dbg_msg("invalid dirty space in LEB %d "
1665 "(dirty %d, expected %d)",
1666 lnum, dirty, c->ltab[i].dirty);
1667 err = -EINVAL;
1669 return err;
1671 node_type = get_lpt_node_type(c, buf, &node_num);
1672 node_len = get_lpt_node_len(c, node_type);
1673 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1674 if (ret == 1)
1675 dirty += node_len;
1676 buf += node_len;
1677 len -= node_len;
1682 * dbg_check_ltab - check the free and dirty space in the ltab.
1683 * @c: the UBIFS file-system description object
1685 * This function returns %0 on success and a negative error code on failure.
1687 int dbg_check_ltab(struct ubifs_info *c)
1689 int lnum, err, i, cnt;
1691 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1692 return 0;
1694 /* Bring the entire tree into memory */
1695 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1696 for (i = 0; i < cnt; i++) {
1697 struct ubifs_pnode *pnode;
1699 pnode = pnode_lookup(c, i);
1700 if (IS_ERR(pnode))
1701 return PTR_ERR(pnode);
1702 cond_resched();
1705 /* Check nodes */
1706 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1707 if (err)
1708 return err;
1710 /* Check each LEB */
1711 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1712 err = dbg_check_ltab_lnum(c, lnum);
1713 if (err) {
1714 dbg_err("failed at LEB %d", lnum);
1715 return err;
1719 dbg_lp("succeeded");
1720 return 0;
1724 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1725 * @c: the UBIFS file-system description object
1727 * This function returns %0 on success and a negative error code on failure.
1729 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1731 long long free = 0;
1732 int i;
1734 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1735 return 0;
1737 for (i = 0; i < c->lpt_lebs; i++) {
1738 if (c->ltab[i].tgc || c->ltab[i].cmt)
1739 continue;
1740 if (i + c->lpt_first == c->nhead_lnum)
1741 free += c->leb_size - c->nhead_offs;
1742 else if (c->ltab[i].free == c->leb_size)
1743 free += c->leb_size;
1745 if (free < c->lpt_sz) {
1746 dbg_err("LPT space error: free %lld lpt_sz %lld",
1747 free, c->lpt_sz);
1748 dbg_dump_lpt_info(c);
1749 dbg_dump_lpt_lebs(c);
1750 dump_stack();
1751 return -EINVAL;
1753 return 0;
1757 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1758 * @c: the UBIFS file-system description object
1759 * @action: action
1760 * @len: length written
1762 * This function returns %0 on success and a negative error code on failure.
1764 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1766 struct ubifs_debug_info *d = c->dbg;
1767 long long chk_lpt_sz, lpt_sz;
1768 int err = 0;
1770 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1771 return 0;
1773 switch (action) {
1774 case 0:
1775 d->chk_lpt_sz = 0;
1776 d->chk_lpt_sz2 = 0;
1777 d->chk_lpt_lebs = 0;
1778 d->chk_lpt_wastage = 0;
1779 if (c->dirty_pn_cnt > c->pnode_cnt) {
1780 dbg_err("dirty pnodes %d exceed max %d",
1781 c->dirty_pn_cnt, c->pnode_cnt);
1782 err = -EINVAL;
1784 if (c->dirty_nn_cnt > c->nnode_cnt) {
1785 dbg_err("dirty nnodes %d exceed max %d",
1786 c->dirty_nn_cnt, c->nnode_cnt);
1787 err = -EINVAL;
1789 return err;
1790 case 1:
1791 d->chk_lpt_sz += len;
1792 return 0;
1793 case 2:
1794 d->chk_lpt_sz += len;
1795 d->chk_lpt_wastage += len;
1796 d->chk_lpt_lebs += 1;
1797 return 0;
1798 case 3:
1799 chk_lpt_sz = c->leb_size;
1800 chk_lpt_sz *= d->chk_lpt_lebs;
1801 chk_lpt_sz += len - c->nhead_offs;
1802 if (d->chk_lpt_sz != chk_lpt_sz) {
1803 dbg_err("LPT wrote %lld but space used was %lld",
1804 d->chk_lpt_sz, chk_lpt_sz);
1805 err = -EINVAL;
1807 if (d->chk_lpt_sz > c->lpt_sz) {
1808 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1809 d->chk_lpt_sz, c->lpt_sz);
1810 err = -EINVAL;
1812 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1813 dbg_err("LPT layout size %lld but wrote %lld",
1814 d->chk_lpt_sz, d->chk_lpt_sz2);
1815 err = -EINVAL;
1817 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1818 dbg_err("LPT new nhead offs: expected %d was %d",
1819 d->new_nhead_offs, len);
1820 err = -EINVAL;
1822 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1823 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1824 lpt_sz += c->ltab_sz;
1825 if (c->big_lpt)
1826 lpt_sz += c->lsave_sz;
1827 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1828 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1829 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1830 err = -EINVAL;
1832 if (err) {
1833 dbg_dump_lpt_info(c);
1834 dbg_dump_lpt_lebs(c);
1835 dump_stack();
1837 d->chk_lpt_sz2 = d->chk_lpt_sz;
1838 d->chk_lpt_sz = 0;
1839 d->chk_lpt_wastage = 0;
1840 d->chk_lpt_lebs = 0;
1841 d->new_nhead_offs = len;
1842 return err;
1843 case 4:
1844 d->chk_lpt_sz += len;
1845 d->chk_lpt_wastage += len;
1846 return 0;
1847 default:
1848 return -EINVAL;
1853 * dbg_dump_lpt_leb - dump an LPT LEB.
1854 * @c: UBIFS file-system description object
1855 * @lnum: LEB number to dump
1857 * This function dumps an LEB from LPT area. Nodes in this area are very
1858 * different to nodes in the main area (e.g., they do not have common headers,
1859 * they do not have 8-byte alignments, etc), so we have a separate function to
1860 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1862 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1864 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1865 void *buf = c->dbg->buf;
1867 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1868 current->pid, lnum);
1869 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1870 if (err) {
1871 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1872 return;
1874 while (1) {
1875 offs = c->leb_size - len;
1876 if (!is_a_node(c, buf, len)) {
1877 int pad_len;
1879 pad_len = get_pad_len(c, buf, len);
1880 if (pad_len) {
1881 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1882 lnum, offs, pad_len);
1883 buf += pad_len;
1884 len -= pad_len;
1885 continue;
1887 if (len)
1888 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1889 lnum, offs, len);
1890 break;
1893 node_type = get_lpt_node_type(c, buf, &node_num);
1894 switch (node_type) {
1895 case UBIFS_LPT_PNODE:
1897 node_len = c->pnode_sz;
1898 if (c->big_lpt)
1899 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1900 lnum, offs, node_num);
1901 else
1902 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1903 lnum, offs);
1904 break;
1906 case UBIFS_LPT_NNODE:
1908 int i;
1909 struct ubifs_nnode nnode;
1911 node_len = c->nnode_sz;
1912 if (c->big_lpt)
1913 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1914 lnum, offs, node_num);
1915 else
1916 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1917 lnum, offs);
1918 err = ubifs_unpack_nnode(c, buf, &nnode);
1919 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1920 printk("%d:%d", nnode.nbranch[i].lnum,
1921 nnode.nbranch[i].offs);
1922 if (i != UBIFS_LPT_FANOUT - 1)
1923 printk(", ");
1925 printk("\n");
1926 break;
1928 case UBIFS_LPT_LTAB:
1929 node_len = c->ltab_sz;
1930 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1931 lnum, offs);
1932 break;
1933 case UBIFS_LPT_LSAVE:
1934 node_len = c->lsave_sz;
1935 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1936 break;
1937 default:
1938 ubifs_err("LPT node type %d not recognized", node_type);
1939 return;
1942 buf += node_len;
1943 len -= node_len;
1946 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1947 current->pid, lnum);
1951 * dbg_dump_lpt_lebs - dump LPT lebs.
1952 * @c: UBIFS file-system description object
1954 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1955 * locked.
1957 void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1959 int i;
1961 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
1962 current->pid);
1963 for (i = 0; i < c->lpt_lebs; i++)
1964 dump_lpt_leb(c, i + c->lpt_first);
1965 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
1966 current->pid);
1969 #endif /* CONFIG_UBIFS_FS_DEBUG */