new buffering logic part 1
[cor_2_6_31.git] / fs / ubifs / lpt_commit.c
blob8cbfb82480252d3f3abebf0d3a14beb3cc2a9b4a
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, c->leb_size - 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, c->leb_size - 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, c->leb_size - 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;
420 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
421 err = realloc_lpt_leb(c, &lnum);
422 if (err)
423 goto no_space;
424 offs = from = 0;
425 ubifs_assert(lnum >= c->lpt_first &&
426 lnum <= c->lpt_last);
427 err = ubifs_leb_unmap(c, lnum);
428 if (err)
429 return err;
430 /* Try to place lsave and ltab nicely */
431 if (!done_lsave) {
432 done_lsave = 1;
433 ubifs_pack_lsave(c, buf + offs, c->lsave);
434 offs += c->lsave_sz;
435 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
436 continue;
438 if (!done_ltab) {
439 done_ltab = 1;
440 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
441 offs += c->ltab_sz;
442 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
443 continue;
445 break;
447 if (cnode->level)
448 ubifs_pack_nnode(c, buf + offs,
449 (struct ubifs_nnode *)cnode);
450 else
451 ubifs_pack_pnode(c, buf + offs,
452 (struct ubifs_pnode *)cnode);
454 * The reason for the barriers is the same as in case of TNC.
455 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
456 * 'dirty_cow_pnode()' are the functions for which this is
457 * important.
459 clear_bit(DIRTY_CNODE, &cnode->flags);
460 smp_mb__before_clear_bit();
461 clear_bit(COW_ZNODE, &cnode->flags);
462 smp_mb__after_clear_bit();
463 offs += len;
464 dbg_chk_lpt_sz(c, 1, len);
465 cnode = cnode->cnext;
466 } while (cnode && cnode != c->lpt_cnext);
468 /* Make sure to place LPT's save table */
469 if (!done_lsave) {
470 if (offs + c->lsave_sz > c->leb_size) {
471 wlen = offs - from;
472 alen = ALIGN(wlen, c->min_io_size);
473 memset(buf + offs, 0xff, alen - wlen);
474 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
475 UBI_SHORTTERM);
476 if (err)
477 return err;
478 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
479 err = realloc_lpt_leb(c, &lnum);
480 if (err)
481 goto no_space;
482 offs = from = 0;
483 ubifs_assert(lnum >= c->lpt_first &&
484 lnum <= c->lpt_last);
485 err = ubifs_leb_unmap(c, lnum);
486 if (err)
487 return err;
489 done_lsave = 1;
490 ubifs_pack_lsave(c, buf + offs, c->lsave);
491 offs += c->lsave_sz;
492 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
495 /* Make sure to place LPT's own lprops table */
496 if (!done_ltab) {
497 if (offs + c->ltab_sz > c->leb_size) {
498 wlen = offs - from;
499 alen = ALIGN(wlen, c->min_io_size);
500 memset(buf + offs, 0xff, alen - wlen);
501 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
502 UBI_SHORTTERM);
503 if (err)
504 return err;
505 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
506 err = realloc_lpt_leb(c, &lnum);
507 if (err)
508 goto no_space;
509 offs = from = 0;
510 ubifs_assert(lnum >= c->lpt_first &&
511 lnum <= c->lpt_last);
512 err = ubifs_leb_unmap(c, lnum);
513 if (err)
514 return err;
516 done_ltab = 1;
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, UBI_SHORTTERM);
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("LPT out of space mismatch");
548 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
549 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
550 dbg_dump_lpt_info(c);
551 dbg_dump_lpt_lebs(c);
552 dump_stack();
553 return err;
557 * next_pnode_to_dirty - find next pnode to dirty.
558 * @c: UBIFS file-system description object
559 * @pnode: pnode
561 * This function returns the next pnode to dirty or %NULL if there are no more
562 * pnodes. Note that pnodes that have never been written (lnum == 0) are
563 * skipped.
565 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
566 struct ubifs_pnode *pnode)
568 struct ubifs_nnode *nnode;
569 int iip;
571 /* Try to go right */
572 nnode = pnode->parent;
573 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
574 if (nnode->nbranch[iip].lnum)
575 return ubifs_get_pnode(c, nnode, iip);
578 /* Go up while can't go right */
579 do {
580 iip = nnode->iip + 1;
581 nnode = nnode->parent;
582 if (!nnode)
583 return NULL;
584 for (; iip < UBIFS_LPT_FANOUT; iip++) {
585 if (nnode->nbranch[iip].lnum)
586 break;
588 } while (iip >= UBIFS_LPT_FANOUT);
590 /* Go right */
591 nnode = ubifs_get_nnode(c, nnode, iip);
592 if (IS_ERR(nnode))
593 return (void *)nnode;
595 /* Go down to level 1 */
596 while (nnode->level > 1) {
597 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
598 if (nnode->nbranch[iip].lnum)
599 break;
601 if (iip >= UBIFS_LPT_FANOUT) {
603 * Should not happen, but we need to keep going
604 * if it does.
606 iip = 0;
608 nnode = ubifs_get_nnode(c, nnode, iip);
609 if (IS_ERR(nnode))
610 return (void *)nnode;
613 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
614 if (nnode->nbranch[iip].lnum)
615 break;
616 if (iip >= UBIFS_LPT_FANOUT)
617 /* Should not happen, but we need to keep going if it does */
618 iip = 0;
619 return ubifs_get_pnode(c, nnode, iip);
623 * pnode_lookup - lookup a pnode in the LPT.
624 * @c: UBIFS file-system description object
625 * @i: pnode number (0 to main_lebs - 1)
627 * This function returns a pointer to the pnode on success or a negative
628 * error code on failure.
630 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
632 int err, h, iip, shft;
633 struct ubifs_nnode *nnode;
635 if (!c->nroot) {
636 err = ubifs_read_nnode(c, NULL, 0);
637 if (err)
638 return ERR_PTR(err);
640 i <<= UBIFS_LPT_FANOUT_SHIFT;
641 nnode = c->nroot;
642 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
643 for (h = 1; h < c->lpt_hght; h++) {
644 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
645 shft -= UBIFS_LPT_FANOUT_SHIFT;
646 nnode = ubifs_get_nnode(c, nnode, iip);
647 if (IS_ERR(nnode))
648 return ERR_PTR(PTR_ERR(nnode));
650 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
651 return ubifs_get_pnode(c, nnode, iip);
655 * add_pnode_dirt - add dirty space to LPT LEB properties.
656 * @c: UBIFS file-system description object
657 * @pnode: pnode for which to add dirt
659 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
661 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
662 c->pnode_sz);
666 * do_make_pnode_dirty - mark a pnode dirty.
667 * @c: UBIFS file-system description object
668 * @pnode: pnode to mark dirty
670 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
672 /* Assumes cnext list is empty i.e. not called during commit */
673 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
674 struct ubifs_nnode *nnode;
676 c->dirty_pn_cnt += 1;
677 add_pnode_dirt(c, pnode);
678 /* Mark parent and ancestors dirty too */
679 nnode = pnode->parent;
680 while (nnode) {
681 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
682 c->dirty_nn_cnt += 1;
683 ubifs_add_nnode_dirt(c, nnode);
684 nnode = nnode->parent;
685 } else
686 break;
692 * make_tree_dirty - mark the entire LEB properties tree dirty.
693 * @c: UBIFS file-system description object
695 * This function is used by the "small" LPT model to cause the entire LEB
696 * properties tree to be written. The "small" LPT model does not use LPT
697 * garbage collection because it is more efficient to write the entire tree
698 * (because it is small).
700 * This function returns %0 on success and a negative error code on failure.
702 static int make_tree_dirty(struct ubifs_info *c)
704 struct ubifs_pnode *pnode;
706 pnode = pnode_lookup(c, 0);
707 while (pnode) {
708 do_make_pnode_dirty(c, pnode);
709 pnode = next_pnode_to_dirty(c, pnode);
710 if (IS_ERR(pnode))
711 return PTR_ERR(pnode);
713 return 0;
717 * need_write_all - determine if the LPT area is running out of free space.
718 * @c: UBIFS file-system description object
720 * This function returns %1 if the LPT area is running out of free space and %0
721 * if it is not.
723 static int need_write_all(struct ubifs_info *c)
725 long long free = 0;
726 int i;
728 for (i = 0; i < c->lpt_lebs; i++) {
729 if (i + c->lpt_first == c->nhead_lnum)
730 free += c->leb_size - c->nhead_offs;
731 else if (c->ltab[i].free == c->leb_size)
732 free += c->leb_size;
733 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
734 free += c->leb_size;
736 /* Less than twice the size left */
737 if (free <= c->lpt_sz * 2)
738 return 1;
739 return 0;
743 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
744 * @c: UBIFS file-system description object
746 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
747 * free space and so may be reused as soon as the next commit is completed.
748 * This function is called during start commit to mark LPT LEBs for trivial GC.
750 static void lpt_tgc_start(struct ubifs_info *c)
752 int i;
754 for (i = 0; i < c->lpt_lebs; i++) {
755 if (i + c->lpt_first == c->nhead_lnum)
756 continue;
757 if (c->ltab[i].dirty > 0 &&
758 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
759 c->ltab[i].tgc = 1;
760 c->ltab[i].free = c->leb_size;
761 c->ltab[i].dirty = 0;
762 dbg_lp("LEB %d", i + c->lpt_first);
768 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
769 * @c: UBIFS file-system description object
771 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
772 * free space and so may be reused as soon as the next commit is completed.
773 * This function is called after the commit is completed (master node has been
774 * written) and un-maps LPT LEBs that were marked for trivial GC.
776 static int lpt_tgc_end(struct ubifs_info *c)
778 int i, err;
780 for (i = 0; i < c->lpt_lebs; i++)
781 if (c->ltab[i].tgc) {
782 err = ubifs_leb_unmap(c, i + c->lpt_first);
783 if (err)
784 return err;
785 c->ltab[i].tgc = 0;
786 dbg_lp("LEB %d", i + c->lpt_first);
788 return 0;
792 * populate_lsave - fill the lsave array with important LEB numbers.
793 * @c: the UBIFS file-system description object
795 * This function is only called for the "big" model. It records a small number
796 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
797 * most important to least important): empty, freeable, freeable index, dirty
798 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
799 * their pnodes into memory. That will stop us from having to scan the LPT
800 * straight away. For the "small" model we assume that scanning the LPT is no
801 * big deal.
803 static void populate_lsave(struct ubifs_info *c)
805 struct ubifs_lprops *lprops;
806 struct ubifs_lpt_heap *heap;
807 int i, cnt = 0;
809 ubifs_assert(c->big_lpt);
810 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
811 c->lpt_drty_flgs |= LSAVE_DIRTY;
812 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
814 list_for_each_entry(lprops, &c->empty_list, list) {
815 c->lsave[cnt++] = lprops->lnum;
816 if (cnt >= c->lsave_cnt)
817 return;
819 list_for_each_entry(lprops, &c->freeable_list, list) {
820 c->lsave[cnt++] = lprops->lnum;
821 if (cnt >= c->lsave_cnt)
822 return;
824 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
825 c->lsave[cnt++] = lprops->lnum;
826 if (cnt >= c->lsave_cnt)
827 return;
829 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
830 for (i = 0; i < heap->cnt; i++) {
831 c->lsave[cnt++] = heap->arr[i]->lnum;
832 if (cnt >= c->lsave_cnt)
833 return;
835 heap = &c->lpt_heap[LPROPS_DIRTY - 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_FREE - 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 /* Fill it up completely */
848 while (cnt < c->lsave_cnt)
849 c->lsave[cnt++] = c->main_first;
853 * nnode_lookup - lookup a nnode in the LPT.
854 * @c: UBIFS file-system description object
855 * @i: nnode number
857 * This function returns a pointer to the nnode on success or a negative
858 * error code on failure.
860 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
862 int err, iip;
863 struct ubifs_nnode *nnode;
865 if (!c->nroot) {
866 err = ubifs_read_nnode(c, NULL, 0);
867 if (err)
868 return ERR_PTR(err);
870 nnode = c->nroot;
871 while (1) {
872 iip = i & (UBIFS_LPT_FANOUT - 1);
873 i >>= UBIFS_LPT_FANOUT_SHIFT;
874 if (!i)
875 break;
876 nnode = ubifs_get_nnode(c, nnode, iip);
877 if (IS_ERR(nnode))
878 return nnode;
880 return nnode;
884 * make_nnode_dirty - find a nnode and, if found, make it dirty.
885 * @c: UBIFS file-system description object
886 * @node_num: nnode number of nnode to make dirty
887 * @lnum: LEB number where nnode was written
888 * @offs: offset where nnode was written
890 * This function is used by LPT garbage collection. LPT garbage collection is
891 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
892 * simply involves marking all the nodes in the LEB being garbage-collected as
893 * dirty. The dirty nodes are written next commit, after which the LEB is free
894 * to be reused.
896 * This function returns %0 on success and a negative error code on failure.
898 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
899 int offs)
901 struct ubifs_nnode *nnode;
903 nnode = nnode_lookup(c, node_num);
904 if (IS_ERR(nnode))
905 return PTR_ERR(nnode);
906 if (nnode->parent) {
907 struct ubifs_nbranch *branch;
909 branch = &nnode->parent->nbranch[nnode->iip];
910 if (branch->lnum != lnum || branch->offs != offs)
911 return 0; /* nnode is obsolete */
912 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
913 return 0; /* nnode is obsolete */
914 /* Assumes cnext list is empty i.e. not called during commit */
915 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
916 c->dirty_nn_cnt += 1;
917 ubifs_add_nnode_dirt(c, nnode);
918 /* Mark parent and ancestors dirty too */
919 nnode = nnode->parent;
920 while (nnode) {
921 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
922 c->dirty_nn_cnt += 1;
923 ubifs_add_nnode_dirt(c, nnode);
924 nnode = nnode->parent;
925 } else
926 break;
929 return 0;
933 * make_pnode_dirty - find a pnode and, if found, make it dirty.
934 * @c: UBIFS file-system description object
935 * @node_num: pnode number of pnode to make dirty
936 * @lnum: LEB number where pnode was written
937 * @offs: offset where pnode was written
939 * This function is used by LPT garbage collection. LPT garbage collection is
940 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
941 * simply involves marking all the nodes in the LEB being garbage-collected as
942 * dirty. The dirty nodes are written next commit, after which the LEB is free
943 * to be reused.
945 * This function returns %0 on success and a negative error code on failure.
947 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
948 int offs)
950 struct ubifs_pnode *pnode;
951 struct ubifs_nbranch *branch;
953 pnode = pnode_lookup(c, node_num);
954 if (IS_ERR(pnode))
955 return PTR_ERR(pnode);
956 branch = &pnode->parent->nbranch[pnode->iip];
957 if (branch->lnum != lnum || branch->offs != offs)
958 return 0;
959 do_make_pnode_dirty(c, pnode);
960 return 0;
964 * make_ltab_dirty - make ltab node dirty.
965 * @c: UBIFS file-system description object
966 * @lnum: LEB number where ltab was written
967 * @offs: offset where ltab was written
969 * This function is used by LPT garbage collection. LPT garbage collection is
970 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
971 * simply involves marking all the nodes in the LEB being garbage-collected as
972 * dirty. The dirty nodes are written next commit, after which the LEB is free
973 * to be reused.
975 * This function returns %0 on success and a negative error code on failure.
977 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
979 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
980 return 0; /* This ltab node is obsolete */
981 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
982 c->lpt_drty_flgs |= LTAB_DIRTY;
983 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
985 return 0;
989 * make_lsave_dirty - make lsave node dirty.
990 * @c: UBIFS file-system description object
991 * @lnum: LEB number where lsave was written
992 * @offs: offset where lsave was written
994 * This function is used by LPT garbage collection. LPT garbage collection is
995 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
996 * simply involves marking all the nodes in the LEB being garbage-collected as
997 * dirty. The dirty nodes are written next commit, after which the LEB is free
998 * to be reused.
1000 * This function returns %0 on success and a negative error code on failure.
1002 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1004 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1005 return 0; /* This lsave node is obsolete */
1006 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1007 c->lpt_drty_flgs |= LSAVE_DIRTY;
1008 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1010 return 0;
1014 * make_node_dirty - make node dirty.
1015 * @c: UBIFS file-system description object
1016 * @node_type: LPT node type
1017 * @node_num: node number
1018 * @lnum: LEB number where node was written
1019 * @offs: offset where node was written
1021 * This function is used by LPT garbage collection. LPT garbage collection is
1022 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1023 * simply involves marking all the nodes in the LEB being garbage-collected as
1024 * dirty. The dirty nodes are written next commit, after which the LEB is free
1025 * to be reused.
1027 * This function returns %0 on success and a negative error code on failure.
1029 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1030 int lnum, int offs)
1032 switch (node_type) {
1033 case UBIFS_LPT_NNODE:
1034 return make_nnode_dirty(c, node_num, lnum, offs);
1035 case UBIFS_LPT_PNODE:
1036 return make_pnode_dirty(c, node_num, lnum, offs);
1037 case UBIFS_LPT_LTAB:
1038 return make_ltab_dirty(c, lnum, offs);
1039 case UBIFS_LPT_LSAVE:
1040 return make_lsave_dirty(c, lnum, offs);
1042 return -EINVAL;
1046 * get_lpt_node_len - return the length of a node based on its type.
1047 * @c: UBIFS file-system description object
1048 * @node_type: LPT node type
1050 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1052 switch (node_type) {
1053 case UBIFS_LPT_NNODE:
1054 return c->nnode_sz;
1055 case UBIFS_LPT_PNODE:
1056 return c->pnode_sz;
1057 case UBIFS_LPT_LTAB:
1058 return c->ltab_sz;
1059 case UBIFS_LPT_LSAVE:
1060 return c->lsave_sz;
1062 return 0;
1066 * get_pad_len - return the length of padding in a buffer.
1067 * @c: UBIFS file-system description object
1068 * @buf: buffer
1069 * @len: length of buffer
1071 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1073 int offs, pad_len;
1075 if (c->min_io_size == 1)
1076 return 0;
1077 offs = c->leb_size - len;
1078 pad_len = ALIGN(offs, c->min_io_size) - offs;
1079 return pad_len;
1083 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1084 * @c: UBIFS file-system description object
1085 * @buf: buffer
1086 * @node_num: node number is returned here
1088 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1089 int *node_num)
1091 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1092 int pos = 0, node_type;
1094 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1095 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1096 return node_type;
1100 * is_a_node - determine if a buffer contains a node.
1101 * @c: UBIFS file-system description object
1102 * @buf: buffer
1103 * @len: length of buffer
1105 * This function returns %1 if the buffer contains a node or %0 if it does not.
1107 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1109 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1110 int pos = 0, node_type, node_len;
1111 uint16_t crc, calc_crc;
1113 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1114 return 0;
1115 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1116 if (node_type == UBIFS_LPT_NOT_A_NODE)
1117 return 0;
1118 node_len = get_lpt_node_len(c, node_type);
1119 if (!node_len || node_len > len)
1120 return 0;
1121 pos = 0;
1122 addr = buf;
1123 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1124 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1125 node_len - UBIFS_LPT_CRC_BYTES);
1126 if (crc != calc_crc)
1127 return 0;
1128 return 1;
1132 * lpt_gc_lnum - garbage collect a LPT LEB.
1133 * @c: UBIFS file-system description object
1134 * @lnum: LEB number to garbage collect
1136 * LPT garbage collection is used only for the "big" LPT model
1137 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1138 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1139 * next commit, after which the LEB is free to be reused.
1141 * This function returns %0 on success and a negative error code on failure.
1143 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1145 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1146 void *buf = c->lpt_buf;
1148 dbg_lp("LEB %d", lnum);
1149 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1150 if (err) {
1151 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1152 return err;
1154 while (1) {
1155 if (!is_a_node(c, buf, len)) {
1156 int pad_len;
1158 pad_len = get_pad_len(c, buf, len);
1159 if (pad_len) {
1160 buf += pad_len;
1161 len -= pad_len;
1162 continue;
1164 return 0;
1166 node_type = get_lpt_node_type(c, buf, &node_num);
1167 node_len = get_lpt_node_len(c, node_type);
1168 offs = c->leb_size - len;
1169 ubifs_assert(node_len != 0);
1170 mutex_lock(&c->lp_mutex);
1171 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1172 mutex_unlock(&c->lp_mutex);
1173 if (err)
1174 return err;
1175 buf += node_len;
1176 len -= node_len;
1178 return 0;
1182 * lpt_gc - LPT garbage collection.
1183 * @c: UBIFS file-system description object
1185 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1186 * Returns %0 on success and a negative error code on failure.
1188 static int lpt_gc(struct ubifs_info *c)
1190 int i, lnum = -1, dirty = 0;
1192 mutex_lock(&c->lp_mutex);
1193 for (i = 0; i < c->lpt_lebs; i++) {
1194 ubifs_assert(!c->ltab[i].tgc);
1195 if (i + c->lpt_first == c->nhead_lnum ||
1196 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1197 continue;
1198 if (c->ltab[i].dirty > dirty) {
1199 dirty = c->ltab[i].dirty;
1200 lnum = i + c->lpt_first;
1203 mutex_unlock(&c->lp_mutex);
1204 if (lnum == -1)
1205 return -ENOSPC;
1206 return lpt_gc_lnum(c, lnum);
1210 * ubifs_lpt_start_commit - UBIFS commit starts.
1211 * @c: the UBIFS file-system description object
1213 * This function has to be called when UBIFS starts the commit operation.
1214 * This function "freezes" all currently dirty LEB properties and does not
1215 * change them anymore. Further changes are saved and tracked separately
1216 * because they are not part of this commit. This function returns zero in case
1217 * of success and a negative error code in case of failure.
1219 int ubifs_lpt_start_commit(struct ubifs_info *c)
1221 int err, cnt;
1223 dbg_lp("");
1225 mutex_lock(&c->lp_mutex);
1226 err = dbg_chk_lpt_free_spc(c);
1227 if (err)
1228 goto out;
1229 err = dbg_check_ltab(c);
1230 if (err)
1231 goto out;
1233 if (c->check_lpt_free) {
1235 * We ensure there is enough free space in
1236 * ubifs_lpt_post_commit() by marking nodes dirty. That
1237 * information is lost when we unmount, so we also need
1238 * to check free space once after mounting also.
1240 c->check_lpt_free = 0;
1241 while (need_write_all(c)) {
1242 mutex_unlock(&c->lp_mutex);
1243 err = lpt_gc(c);
1244 if (err)
1245 return err;
1246 mutex_lock(&c->lp_mutex);
1250 lpt_tgc_start(c);
1252 if (!c->dirty_pn_cnt) {
1253 dbg_cmt("no cnodes to commit");
1254 err = 0;
1255 goto out;
1258 if (!c->big_lpt && need_write_all(c)) {
1259 /* If needed, write everything */
1260 err = make_tree_dirty(c);
1261 if (err)
1262 goto out;
1263 lpt_tgc_start(c);
1266 if (c->big_lpt)
1267 populate_lsave(c);
1269 cnt = get_cnodes_to_commit(c);
1270 ubifs_assert(cnt != 0);
1272 err = layout_cnodes(c);
1273 if (err)
1274 goto out;
1276 /* Copy the LPT's own lprops for end commit to write */
1277 memcpy(c->ltab_cmt, c->ltab,
1278 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1279 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1281 out:
1282 mutex_unlock(&c->lp_mutex);
1283 return err;
1287 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1288 * @c: UBIFS file-system description object
1290 static void free_obsolete_cnodes(struct ubifs_info *c)
1292 struct ubifs_cnode *cnode, *cnext;
1294 cnext = c->lpt_cnext;
1295 if (!cnext)
1296 return;
1297 do {
1298 cnode = cnext;
1299 cnext = cnode->cnext;
1300 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1301 kfree(cnode);
1302 else
1303 cnode->cnext = NULL;
1304 } while (cnext != c->lpt_cnext);
1305 c->lpt_cnext = NULL;
1309 * ubifs_lpt_end_commit - finish the commit operation.
1310 * @c: the UBIFS file-system description object
1312 * This function has to be called when the commit operation finishes. It
1313 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1314 * the media. Returns zero in case of success and a negative error code in case
1315 * of failure.
1317 int ubifs_lpt_end_commit(struct ubifs_info *c)
1319 int err;
1321 dbg_lp("");
1323 if (!c->lpt_cnext)
1324 return 0;
1326 err = write_cnodes(c);
1327 if (err)
1328 return err;
1330 mutex_lock(&c->lp_mutex);
1331 free_obsolete_cnodes(c);
1332 mutex_unlock(&c->lp_mutex);
1334 return 0;
1338 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1339 * @c: UBIFS file-system description object
1341 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1342 * commit for the "big" LPT model.
1344 int ubifs_lpt_post_commit(struct ubifs_info *c)
1346 int err;
1348 mutex_lock(&c->lp_mutex);
1349 err = lpt_tgc_end(c);
1350 if (err)
1351 goto out;
1352 if (c->big_lpt)
1353 while (need_write_all(c)) {
1354 mutex_unlock(&c->lp_mutex);
1355 err = lpt_gc(c);
1356 if (err)
1357 return err;
1358 mutex_lock(&c->lp_mutex);
1360 out:
1361 mutex_unlock(&c->lp_mutex);
1362 return err;
1366 * first_nnode - find the first nnode in memory.
1367 * @c: UBIFS file-system description object
1368 * @hght: height of tree where nnode found is returned here
1370 * This function returns a pointer to the nnode found or %NULL if no nnode is
1371 * found. This function is a helper to 'ubifs_lpt_free()'.
1373 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1375 struct ubifs_nnode *nnode;
1376 int h, i, found;
1378 nnode = c->nroot;
1379 *hght = 0;
1380 if (!nnode)
1381 return NULL;
1382 for (h = 1; h < c->lpt_hght; h++) {
1383 found = 0;
1384 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1385 if (nnode->nbranch[i].nnode) {
1386 found = 1;
1387 nnode = nnode->nbranch[i].nnode;
1388 *hght = h;
1389 break;
1392 if (!found)
1393 break;
1395 return nnode;
1399 * next_nnode - find the next nnode in memory.
1400 * @c: UBIFS file-system description object
1401 * @nnode: nnode from which to start.
1402 * @hght: height of tree where nnode is, is passed and returned here
1404 * This function returns a pointer to the nnode found or %NULL if no nnode is
1405 * found. This function is a helper to 'ubifs_lpt_free()'.
1407 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1408 struct ubifs_nnode *nnode, int *hght)
1410 struct ubifs_nnode *parent;
1411 int iip, h, i, found;
1413 parent = nnode->parent;
1414 if (!parent)
1415 return NULL;
1416 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1417 *hght -= 1;
1418 return parent;
1420 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1421 nnode = parent->nbranch[iip].nnode;
1422 if (nnode)
1423 break;
1425 if (!nnode) {
1426 *hght -= 1;
1427 return parent;
1429 for (h = *hght + 1; h < c->lpt_hght; h++) {
1430 found = 0;
1431 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1432 if (nnode->nbranch[i].nnode) {
1433 found = 1;
1434 nnode = nnode->nbranch[i].nnode;
1435 *hght = h;
1436 break;
1439 if (!found)
1440 break;
1442 return nnode;
1446 * ubifs_lpt_free - free resources owned by the LPT.
1447 * @c: UBIFS file-system description object
1448 * @wr_only: free only resources used for writing
1450 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1452 struct ubifs_nnode *nnode;
1453 int i, hght;
1455 /* Free write-only things first */
1457 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1459 vfree(c->ltab_cmt);
1460 c->ltab_cmt = NULL;
1461 vfree(c->lpt_buf);
1462 c->lpt_buf = NULL;
1463 kfree(c->lsave);
1464 c->lsave = NULL;
1466 if (wr_only)
1467 return;
1469 /* Now free the rest */
1471 nnode = first_nnode(c, &hght);
1472 while (nnode) {
1473 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1474 kfree(nnode->nbranch[i].nnode);
1475 nnode = next_nnode(c, nnode, &hght);
1477 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1478 kfree(c->lpt_heap[i].arr);
1479 kfree(c->dirty_idx.arr);
1480 kfree(c->nroot);
1481 vfree(c->ltab);
1482 kfree(c->lpt_nod_buf);
1485 #ifdef CONFIG_UBIFS_FS_DEBUG
1488 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1489 * @buf: buffer
1490 * @len: buffer length
1492 static int dbg_is_all_ff(uint8_t *buf, int len)
1494 int i;
1496 for (i = 0; i < len; i++)
1497 if (buf[i] != 0xff)
1498 return 0;
1499 return 1;
1503 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1504 * @c: the UBIFS file-system description object
1505 * @lnum: LEB number where nnode was written
1506 * @offs: offset where nnode was written
1508 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1510 struct ubifs_nnode *nnode;
1511 int hght;
1513 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1514 nnode = first_nnode(c, &hght);
1515 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1516 struct ubifs_nbranch *branch;
1518 cond_resched();
1519 if (nnode->parent) {
1520 branch = &nnode->parent->nbranch[nnode->iip];
1521 if (branch->lnum != lnum || branch->offs != offs)
1522 continue;
1523 if (test_bit(DIRTY_CNODE, &nnode->flags))
1524 return 1;
1525 return 0;
1526 } else {
1527 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1528 continue;
1529 if (test_bit(DIRTY_CNODE, &nnode->flags))
1530 return 1;
1531 return 0;
1534 return 1;
1538 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1539 * @c: the UBIFS file-system description object
1540 * @lnum: LEB number where pnode was written
1541 * @offs: offset where pnode was written
1543 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1545 int i, cnt;
1547 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1548 for (i = 0; i < cnt; i++) {
1549 struct ubifs_pnode *pnode;
1550 struct ubifs_nbranch *branch;
1552 cond_resched();
1553 pnode = pnode_lookup(c, i);
1554 if (IS_ERR(pnode))
1555 return PTR_ERR(pnode);
1556 branch = &pnode->parent->nbranch[pnode->iip];
1557 if (branch->lnum != lnum || branch->offs != offs)
1558 continue;
1559 if (test_bit(DIRTY_CNODE, &pnode->flags))
1560 return 1;
1561 return 0;
1563 return 1;
1567 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1568 * @c: the UBIFS file-system description object
1569 * @lnum: LEB number where ltab node was written
1570 * @offs: offset where ltab node was written
1572 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1574 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1575 return 1;
1576 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1580 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1581 * @c: the UBIFS file-system description object
1582 * @lnum: LEB number where lsave node was written
1583 * @offs: offset where lsave node was written
1585 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1587 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1588 return 1;
1589 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1593 * dbg_is_node_dirty - determine if a node is dirty.
1594 * @c: the UBIFS file-system description object
1595 * @node_type: node type
1596 * @lnum: LEB number where node was written
1597 * @offs: offset where node was written
1599 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1600 int offs)
1602 switch (node_type) {
1603 case UBIFS_LPT_NNODE:
1604 return dbg_is_nnode_dirty(c, lnum, offs);
1605 case UBIFS_LPT_PNODE:
1606 return dbg_is_pnode_dirty(c, lnum, offs);
1607 case UBIFS_LPT_LTAB:
1608 return dbg_is_ltab_dirty(c, lnum, offs);
1609 case UBIFS_LPT_LSAVE:
1610 return dbg_is_lsave_dirty(c, lnum, offs);
1612 return 1;
1616 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1617 * @c: the UBIFS file-system description object
1618 * @lnum: LEB number where node was written
1619 * @offs: offset where node was written
1621 * This function returns %0 on success and a negative error code on failure.
1623 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1625 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1626 int ret;
1627 void *buf = c->dbg->buf;
1629 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1630 return 0;
1632 dbg_lp("LEB %d", lnum);
1633 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1634 if (err) {
1635 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1636 return err;
1638 while (1) {
1639 if (!is_a_node(c, buf, len)) {
1640 int i, pad_len;
1642 pad_len = get_pad_len(c, buf, len);
1643 if (pad_len) {
1644 buf += pad_len;
1645 len -= pad_len;
1646 dirty += pad_len;
1647 continue;
1649 if (!dbg_is_all_ff(buf, len)) {
1650 dbg_msg("invalid empty space in LEB %d at %d",
1651 lnum, c->leb_size - len);
1652 err = -EINVAL;
1654 i = lnum - c->lpt_first;
1655 if (len != c->ltab[i].free) {
1656 dbg_msg("invalid free space in LEB %d "
1657 "(free %d, expected %d)",
1658 lnum, len, c->ltab[i].free);
1659 err = -EINVAL;
1661 if (dirty != c->ltab[i].dirty) {
1662 dbg_msg("invalid dirty space in LEB %d "
1663 "(dirty %d, expected %d)",
1664 lnum, dirty, c->ltab[i].dirty);
1665 err = -EINVAL;
1667 return err;
1669 node_type = get_lpt_node_type(c, buf, &node_num);
1670 node_len = get_lpt_node_len(c, node_type);
1671 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1672 if (ret == 1)
1673 dirty += node_len;
1674 buf += node_len;
1675 len -= node_len;
1680 * dbg_check_ltab - check the free and dirty space in the ltab.
1681 * @c: the UBIFS file-system description object
1683 * This function returns %0 on success and a negative error code on failure.
1685 int dbg_check_ltab(struct ubifs_info *c)
1687 int lnum, err, i, cnt;
1689 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1690 return 0;
1692 /* Bring the entire tree into memory */
1693 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1694 for (i = 0; i < cnt; i++) {
1695 struct ubifs_pnode *pnode;
1697 pnode = pnode_lookup(c, i);
1698 if (IS_ERR(pnode))
1699 return PTR_ERR(pnode);
1700 cond_resched();
1703 /* Check nodes */
1704 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1705 if (err)
1706 return err;
1708 /* Check each LEB */
1709 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1710 err = dbg_check_ltab_lnum(c, lnum);
1711 if (err) {
1712 dbg_err("failed at LEB %d", lnum);
1713 return err;
1717 dbg_lp("succeeded");
1718 return 0;
1722 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1723 * @c: the UBIFS file-system description object
1725 * This function returns %0 on success and a negative error code on failure.
1727 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1729 long long free = 0;
1730 int i;
1732 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1733 return 0;
1735 for (i = 0; i < c->lpt_lebs; i++) {
1736 if (c->ltab[i].tgc || c->ltab[i].cmt)
1737 continue;
1738 if (i + c->lpt_first == c->nhead_lnum)
1739 free += c->leb_size - c->nhead_offs;
1740 else if (c->ltab[i].free == c->leb_size)
1741 free += c->leb_size;
1743 if (free < c->lpt_sz) {
1744 dbg_err("LPT space error: free %lld lpt_sz %lld",
1745 free, c->lpt_sz);
1746 dbg_dump_lpt_info(c);
1747 dbg_dump_lpt_lebs(c);
1748 dump_stack();
1749 return -EINVAL;
1751 return 0;
1755 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1756 * @c: the UBIFS file-system description object
1757 * @action: what to do
1758 * @len: length written
1760 * This function returns %0 on success and a negative error code on failure.
1761 * The @action argument may be one of:
1762 * o %0 - LPT debugging checking starts, initialize debugging variables;
1763 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1764 * o %2 - switched to a different LEB and wasted @len bytes;
1765 * o %3 - check that we've written the right number of bytes.
1766 * o %4 - wasted @len bytes;
1768 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1770 struct ubifs_debug_info *d = c->dbg;
1771 long long chk_lpt_sz, lpt_sz;
1772 int err = 0;
1774 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1775 return 0;
1777 switch (action) {
1778 case 0:
1779 d->chk_lpt_sz = 0;
1780 d->chk_lpt_sz2 = 0;
1781 d->chk_lpt_lebs = 0;
1782 d->chk_lpt_wastage = 0;
1783 if (c->dirty_pn_cnt > c->pnode_cnt) {
1784 dbg_err("dirty pnodes %d exceed max %d",
1785 c->dirty_pn_cnt, c->pnode_cnt);
1786 err = -EINVAL;
1788 if (c->dirty_nn_cnt > c->nnode_cnt) {
1789 dbg_err("dirty nnodes %d exceed max %d",
1790 c->dirty_nn_cnt, c->nnode_cnt);
1791 err = -EINVAL;
1793 return err;
1794 case 1:
1795 d->chk_lpt_sz += len;
1796 return 0;
1797 case 2:
1798 d->chk_lpt_sz += len;
1799 d->chk_lpt_wastage += len;
1800 d->chk_lpt_lebs += 1;
1801 return 0;
1802 case 3:
1803 chk_lpt_sz = c->leb_size;
1804 chk_lpt_sz *= d->chk_lpt_lebs;
1805 chk_lpt_sz += len - c->nhead_offs;
1806 if (d->chk_lpt_sz != chk_lpt_sz) {
1807 dbg_err("LPT wrote %lld but space used was %lld",
1808 d->chk_lpt_sz, chk_lpt_sz);
1809 err = -EINVAL;
1811 if (d->chk_lpt_sz > c->lpt_sz) {
1812 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1813 d->chk_lpt_sz, c->lpt_sz);
1814 err = -EINVAL;
1816 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1817 dbg_err("LPT layout size %lld but wrote %lld",
1818 d->chk_lpt_sz, d->chk_lpt_sz2);
1819 err = -EINVAL;
1821 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1822 dbg_err("LPT new nhead offs: expected %d was %d",
1823 d->new_nhead_offs, len);
1824 err = -EINVAL;
1826 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1827 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1828 lpt_sz += c->ltab_sz;
1829 if (c->big_lpt)
1830 lpt_sz += c->lsave_sz;
1831 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1832 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1833 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1834 err = -EINVAL;
1836 if (err) {
1837 dbg_dump_lpt_info(c);
1838 dbg_dump_lpt_lebs(c);
1839 dump_stack();
1841 d->chk_lpt_sz2 = d->chk_lpt_sz;
1842 d->chk_lpt_sz = 0;
1843 d->chk_lpt_wastage = 0;
1844 d->chk_lpt_lebs = 0;
1845 d->new_nhead_offs = len;
1846 return err;
1847 case 4:
1848 d->chk_lpt_sz += len;
1849 d->chk_lpt_wastage += len;
1850 return 0;
1851 default:
1852 return -EINVAL;
1857 * dbg_dump_lpt_leb - dump an LPT LEB.
1858 * @c: UBIFS file-system description object
1859 * @lnum: LEB number to dump
1861 * This function dumps an LEB from LPT area. Nodes in this area are very
1862 * different to nodes in the main area (e.g., they do not have common headers,
1863 * they do not have 8-byte alignments, etc), so we have a separate function to
1864 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1866 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1868 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1869 void *buf = c->dbg->buf;
1871 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1872 current->pid, lnum);
1873 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1874 if (err) {
1875 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1876 return;
1878 while (1) {
1879 offs = c->leb_size - len;
1880 if (!is_a_node(c, buf, len)) {
1881 int pad_len;
1883 pad_len = get_pad_len(c, buf, len);
1884 if (pad_len) {
1885 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1886 lnum, offs, pad_len);
1887 buf += pad_len;
1888 len -= pad_len;
1889 continue;
1891 if (len)
1892 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1893 lnum, offs, len);
1894 break;
1897 node_type = get_lpt_node_type(c, buf, &node_num);
1898 switch (node_type) {
1899 case UBIFS_LPT_PNODE:
1901 node_len = c->pnode_sz;
1902 if (c->big_lpt)
1903 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1904 lnum, offs, node_num);
1905 else
1906 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1907 lnum, offs);
1908 break;
1910 case UBIFS_LPT_NNODE:
1912 int i;
1913 struct ubifs_nnode nnode;
1915 node_len = c->nnode_sz;
1916 if (c->big_lpt)
1917 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1918 lnum, offs, node_num);
1919 else
1920 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1921 lnum, offs);
1922 err = ubifs_unpack_nnode(c, buf, &nnode);
1923 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1924 printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
1925 nnode.nbranch[i].offs);
1926 if (i != UBIFS_LPT_FANOUT - 1)
1927 printk(KERN_CONT ", ");
1929 printk(KERN_CONT "\n");
1930 break;
1932 case UBIFS_LPT_LTAB:
1933 node_len = c->ltab_sz;
1934 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1935 lnum, offs);
1936 break;
1937 case UBIFS_LPT_LSAVE:
1938 node_len = c->lsave_sz;
1939 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1940 break;
1941 default:
1942 ubifs_err("LPT node type %d not recognized", node_type);
1943 return;
1946 buf += node_len;
1947 len -= node_len;
1950 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1951 current->pid, lnum);
1955 * dbg_dump_lpt_lebs - dump LPT lebs.
1956 * @c: UBIFS file-system description object
1958 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1959 * locked.
1961 void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1963 int i;
1965 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
1966 current->pid);
1967 for (i = 0; i < c->lpt_lebs; i++)
1968 dump_lpt_leb(c, i + c->lpt_first);
1969 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
1970 current->pid);
1973 #endif /* CONFIG_UBIFS_FS_DEBUG */