qlcnic: fix chip reset logic
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / lpt_commit.c
blobdfcb5748a7dc14db8695f694e021f4c1a52fefcc
1 /*
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements commit-related functionality of the LEB properties
25 * subsystem.
28 #include <linux/crc16.h>
29 #include <linux/slab.h>
30 #include "ubifs.h"
32 #ifdef CONFIG_UBIFS_FS_DEBUG
33 static int dbg_populate_lsave(struct ubifs_info *c);
34 #else
35 #define dbg_populate_lsave(c) 0
36 #endif
38 /**
39 * first_dirty_cnode - find first dirty cnode.
40 * @c: UBIFS file-system description object
41 * @nnode: nnode at which to start
43 * This function returns the first dirty cnode or %NULL if there is not one.
45 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
47 ubifs_assert(nnode);
48 while (1) {
49 int i, cont = 0;
51 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
52 struct ubifs_cnode *cnode;
54 cnode = nnode->nbranch[i].cnode;
55 if (cnode &&
56 test_bit(DIRTY_CNODE, &cnode->flags)) {
57 if (cnode->level == 0)
58 return cnode;
59 nnode = (struct ubifs_nnode *)cnode;
60 cont = 1;
61 break;
64 if (!cont)
65 return (struct ubifs_cnode *)nnode;
69 /**
70 * next_dirty_cnode - find next dirty cnode.
71 * @cnode: cnode from which to begin searching
73 * This function returns the next dirty cnode or %NULL if there is not one.
75 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
77 struct ubifs_nnode *nnode;
78 int i;
80 ubifs_assert(cnode);
81 nnode = cnode->parent;
82 if (!nnode)
83 return NULL;
84 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
85 cnode = nnode->nbranch[i].cnode;
86 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
87 if (cnode->level == 0)
88 return cnode; /* cnode is a pnode */
89 /* cnode is a nnode */
90 return first_dirty_cnode((struct ubifs_nnode *)cnode);
93 return (struct ubifs_cnode *)nnode;
96 /**
97 * get_cnodes_to_commit - create list of dirty cnodes to commit.
98 * @c: UBIFS file-system description object
100 * This function returns the number of cnodes to commit.
102 static int get_cnodes_to_commit(struct ubifs_info *c)
104 struct ubifs_cnode *cnode, *cnext;
105 int cnt = 0;
107 if (!c->nroot)
108 return 0;
110 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
111 return 0;
113 c->lpt_cnext = first_dirty_cnode(c->nroot);
114 cnode = c->lpt_cnext;
115 if (!cnode)
116 return 0;
117 cnt += 1;
118 while (1) {
119 ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
120 __set_bit(COW_ZNODE, &cnode->flags);
121 cnext = next_dirty_cnode(cnode);
122 if (!cnext) {
123 cnode->cnext = c->lpt_cnext;
124 break;
126 cnode->cnext = cnext;
127 cnode = cnext;
128 cnt += 1;
130 dbg_cmt("committing %d cnodes", cnt);
131 dbg_lp("committing %d cnodes", cnt);
132 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
133 return cnt;
137 * upd_ltab - update LPT LEB properties.
138 * @c: UBIFS file-system description object
139 * @lnum: LEB number
140 * @free: amount of free space
141 * @dirty: amount of dirty space to add
143 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
145 dbg_lp("LEB %d free %d dirty %d to %d +%d",
146 lnum, c->ltab[lnum - c->lpt_first].free,
147 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
148 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
149 c->ltab[lnum - c->lpt_first].free = free;
150 c->ltab[lnum - c->lpt_first].dirty += dirty;
154 * alloc_lpt_leb - allocate an LPT LEB that is empty.
155 * @c: UBIFS file-system description object
156 * @lnum: LEB number is passed and returned here
158 * This function finds the next empty LEB in the ltab starting from @lnum. If a
159 * an empty LEB is found it is returned in @lnum and the function returns %0.
160 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
161 * never to run out of space.
163 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
165 int i, n;
167 n = *lnum - c->lpt_first + 1;
168 for (i = n; i < c->lpt_lebs; i++) {
169 if (c->ltab[i].tgc || c->ltab[i].cmt)
170 continue;
171 if (c->ltab[i].free == c->leb_size) {
172 c->ltab[i].cmt = 1;
173 *lnum = i + c->lpt_first;
174 return 0;
178 for (i = 0; i < n; i++) {
179 if (c->ltab[i].tgc || c->ltab[i].cmt)
180 continue;
181 if (c->ltab[i].free == c->leb_size) {
182 c->ltab[i].cmt = 1;
183 *lnum = i + c->lpt_first;
184 return 0;
187 return -ENOSPC;
191 * layout_cnodes - layout cnodes for commit.
192 * @c: UBIFS file-system description object
194 * This function returns %0 on success and a negative error code on failure.
196 static int layout_cnodes(struct ubifs_info *c)
198 int lnum, offs, len, alen, done_lsave, done_ltab, err;
199 struct ubifs_cnode *cnode;
201 err = dbg_chk_lpt_sz(c, 0, 0);
202 if (err)
203 return err;
204 cnode = c->lpt_cnext;
205 if (!cnode)
206 return 0;
207 lnum = c->nhead_lnum;
208 offs = c->nhead_offs;
209 /* Try to place lsave and ltab nicely */
210 done_lsave = !c->big_lpt;
211 done_ltab = 0;
212 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
213 done_lsave = 1;
214 c->lsave_lnum = lnum;
215 c->lsave_offs = offs;
216 offs += c->lsave_sz;
217 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
220 if (offs + c->ltab_sz <= c->leb_size) {
221 done_ltab = 1;
222 c->ltab_lnum = lnum;
223 c->ltab_offs = offs;
224 offs += c->ltab_sz;
225 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
228 do {
229 if (cnode->level) {
230 len = c->nnode_sz;
231 c->dirty_nn_cnt -= 1;
232 } else {
233 len = c->pnode_sz;
234 c->dirty_pn_cnt -= 1;
236 while (offs + len > c->leb_size) {
237 alen = ALIGN(offs, c->min_io_size);
238 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
239 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
240 err = alloc_lpt_leb(c, &lnum);
241 if (err)
242 goto no_space;
243 offs = 0;
244 ubifs_assert(lnum >= c->lpt_first &&
245 lnum <= c->lpt_last);
246 /* Try to place lsave and ltab nicely */
247 if (!done_lsave) {
248 done_lsave = 1;
249 c->lsave_lnum = lnum;
250 c->lsave_offs = offs;
251 offs += c->lsave_sz;
252 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
253 continue;
255 if (!done_ltab) {
256 done_ltab = 1;
257 c->ltab_lnum = lnum;
258 c->ltab_offs = offs;
259 offs += c->ltab_sz;
260 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
261 continue;
263 break;
265 if (cnode->parent) {
266 cnode->parent->nbranch[cnode->iip].lnum = lnum;
267 cnode->parent->nbranch[cnode->iip].offs = offs;
268 } else {
269 c->lpt_lnum = lnum;
270 c->lpt_offs = offs;
272 offs += len;
273 dbg_chk_lpt_sz(c, 1, len);
274 cnode = cnode->cnext;
275 } while (cnode && cnode != c->lpt_cnext);
277 /* Make sure to place LPT's save table */
278 if (!done_lsave) {
279 if (offs + c->lsave_sz > c->leb_size) {
280 alen = ALIGN(offs, c->min_io_size);
281 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
282 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
283 err = alloc_lpt_leb(c, &lnum);
284 if (err)
285 goto no_space;
286 offs = 0;
287 ubifs_assert(lnum >= c->lpt_first &&
288 lnum <= c->lpt_last);
290 done_lsave = 1;
291 c->lsave_lnum = lnum;
292 c->lsave_offs = offs;
293 offs += c->lsave_sz;
294 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
297 /* Make sure to place LPT's own lprops table */
298 if (!done_ltab) {
299 if (offs + c->ltab_sz > c->leb_size) {
300 alen = ALIGN(offs, c->min_io_size);
301 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
302 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
303 err = alloc_lpt_leb(c, &lnum);
304 if (err)
305 goto no_space;
306 offs = 0;
307 ubifs_assert(lnum >= c->lpt_first &&
308 lnum <= c->lpt_last);
310 done_ltab = 1;
311 c->ltab_lnum = lnum;
312 c->ltab_offs = offs;
313 offs += c->ltab_sz;
314 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
317 alen = ALIGN(offs, c->min_io_size);
318 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
319 dbg_chk_lpt_sz(c, 4, alen - offs);
320 err = dbg_chk_lpt_sz(c, 3, alen);
321 if (err)
322 return err;
323 return 0;
325 no_space:
326 ubifs_err("LPT out of space");
327 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
328 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
329 dbg_dump_lpt_info(c);
330 dbg_dump_lpt_lebs(c);
331 dump_stack();
332 return err;
336 * realloc_lpt_leb - allocate an LPT LEB that is empty.
337 * @c: UBIFS file-system description object
338 * @lnum: LEB number is passed and returned here
340 * This function duplicates exactly the results of the function alloc_lpt_leb.
341 * It is used during end commit to reallocate the same LEB numbers that were
342 * allocated by alloc_lpt_leb during start commit.
344 * This function finds the next LEB that was allocated by the alloc_lpt_leb
345 * function starting from @lnum. If a LEB is found it is returned in @lnum and
346 * the function returns %0. Otherwise the function returns -ENOSPC.
347 * Note however, that LPT is designed never to run out of space.
349 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
351 int i, n;
353 n = *lnum - c->lpt_first + 1;
354 for (i = n; i < c->lpt_lebs; i++)
355 if (c->ltab[i].cmt) {
356 c->ltab[i].cmt = 0;
357 *lnum = i + c->lpt_first;
358 return 0;
361 for (i = 0; i < n; i++)
362 if (c->ltab[i].cmt) {
363 c->ltab[i].cmt = 0;
364 *lnum = i + c->lpt_first;
365 return 0;
367 return -ENOSPC;
371 * write_cnodes - write cnodes for commit.
372 * @c: UBIFS file-system description object
374 * This function returns %0 on success and a negative error code on failure.
376 static int write_cnodes(struct ubifs_info *c)
378 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
379 struct ubifs_cnode *cnode;
380 void *buf = c->lpt_buf;
382 cnode = c->lpt_cnext;
383 if (!cnode)
384 return 0;
385 lnum = c->nhead_lnum;
386 offs = c->nhead_offs;
387 from = offs;
388 /* Ensure empty LEB is unmapped */
389 if (offs == 0) {
390 err = ubifs_leb_unmap(c, lnum);
391 if (err)
392 return err;
394 /* Try to place lsave and ltab nicely */
395 done_lsave = !c->big_lpt;
396 done_ltab = 0;
397 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
398 done_lsave = 1;
399 ubifs_pack_lsave(c, buf + offs, c->lsave);
400 offs += c->lsave_sz;
401 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
404 if (offs + c->ltab_sz <= c->leb_size) {
405 done_ltab = 1;
406 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
407 offs += c->ltab_sz;
408 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
411 /* Loop for each cnode */
412 do {
413 if (cnode->level)
414 len = c->nnode_sz;
415 else
416 len = c->pnode_sz;
417 while (offs + len > c->leb_size) {
418 wlen = offs - from;
419 if (wlen) {
420 alen = ALIGN(wlen, c->min_io_size);
421 memset(buf + offs, 0xff, alen - wlen);
422 err = ubifs_leb_write(c, lnum, buf + from, from,
423 alen, UBI_SHORTTERM);
424 if (err)
425 return err;
427 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
428 err = realloc_lpt_leb(c, &lnum);
429 if (err)
430 goto no_space;
431 offs = from = 0;
432 ubifs_assert(lnum >= c->lpt_first &&
433 lnum <= c->lpt_last);
434 err = ubifs_leb_unmap(c, lnum);
435 if (err)
436 return err;
437 /* Try to place lsave and ltab nicely */
438 if (!done_lsave) {
439 done_lsave = 1;
440 ubifs_pack_lsave(c, buf + offs, c->lsave);
441 offs += c->lsave_sz;
442 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
443 continue;
445 if (!done_ltab) {
446 done_ltab = 1;
447 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
448 offs += c->ltab_sz;
449 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
450 continue;
452 break;
454 if (cnode->level)
455 ubifs_pack_nnode(c, buf + offs,
456 (struct ubifs_nnode *)cnode);
457 else
458 ubifs_pack_pnode(c, buf + offs,
459 (struct ubifs_pnode *)cnode);
461 * The reason for the barriers is the same as in case of TNC.
462 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
463 * 'dirty_cow_pnode()' are the functions for which this is
464 * important.
466 clear_bit(DIRTY_CNODE, &cnode->flags);
467 smp_mb__before_clear_bit();
468 clear_bit(COW_ZNODE, &cnode->flags);
469 smp_mb__after_clear_bit();
470 offs += len;
471 dbg_chk_lpt_sz(c, 1, len);
472 cnode = cnode->cnext;
473 } while (cnode && cnode != c->lpt_cnext);
475 /* Make sure to place LPT's save table */
476 if (!done_lsave) {
477 if (offs + c->lsave_sz > c->leb_size) {
478 wlen = offs - from;
479 alen = ALIGN(wlen, c->min_io_size);
480 memset(buf + offs, 0xff, alen - wlen);
481 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
482 UBI_SHORTTERM);
483 if (err)
484 return err;
485 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
486 err = realloc_lpt_leb(c, &lnum);
487 if (err)
488 goto no_space;
489 offs = from = 0;
490 ubifs_assert(lnum >= c->lpt_first &&
491 lnum <= c->lpt_last);
492 err = ubifs_leb_unmap(c, lnum);
493 if (err)
494 return err;
496 done_lsave = 1;
497 ubifs_pack_lsave(c, buf + offs, c->lsave);
498 offs += c->lsave_sz;
499 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
502 /* Make sure to place LPT's own lprops table */
503 if (!done_ltab) {
504 if (offs + c->ltab_sz > c->leb_size) {
505 wlen = offs - from;
506 alen = ALIGN(wlen, c->min_io_size);
507 memset(buf + offs, 0xff, alen - wlen);
508 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
509 UBI_SHORTTERM);
510 if (err)
511 return err;
512 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
513 err = realloc_lpt_leb(c, &lnum);
514 if (err)
515 goto no_space;
516 offs = from = 0;
517 ubifs_assert(lnum >= c->lpt_first &&
518 lnum <= c->lpt_last);
519 err = ubifs_leb_unmap(c, lnum);
520 if (err)
521 return err;
523 done_ltab = 1;
524 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
525 offs += c->ltab_sz;
526 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
529 /* Write remaining data in buffer */
530 wlen = offs - from;
531 alen = ALIGN(wlen, c->min_io_size);
532 memset(buf + offs, 0xff, alen - wlen);
533 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
534 if (err)
535 return err;
537 dbg_chk_lpt_sz(c, 4, alen - wlen);
538 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
539 if (err)
540 return err;
542 c->nhead_lnum = lnum;
543 c->nhead_offs = ALIGN(offs, c->min_io_size);
545 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
546 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
547 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
548 if (c->big_lpt)
549 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
551 return 0;
553 no_space:
554 ubifs_err("LPT out of space mismatch");
555 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
556 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
557 dbg_dump_lpt_info(c);
558 dbg_dump_lpt_lebs(c);
559 dump_stack();
560 return err;
564 * next_pnode_to_dirty - find next pnode to dirty.
565 * @c: UBIFS file-system description object
566 * @pnode: pnode
568 * This function returns the next pnode to dirty or %NULL if there are no more
569 * pnodes. Note that pnodes that have never been written (lnum == 0) are
570 * skipped.
572 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
573 struct ubifs_pnode *pnode)
575 struct ubifs_nnode *nnode;
576 int iip;
578 /* Try to go right */
579 nnode = pnode->parent;
580 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
581 if (nnode->nbranch[iip].lnum)
582 return ubifs_get_pnode(c, nnode, iip);
585 /* Go up while can't go right */
586 do {
587 iip = nnode->iip + 1;
588 nnode = nnode->parent;
589 if (!nnode)
590 return NULL;
591 for (; iip < UBIFS_LPT_FANOUT; iip++) {
592 if (nnode->nbranch[iip].lnum)
593 break;
595 } while (iip >= UBIFS_LPT_FANOUT);
597 /* Go right */
598 nnode = ubifs_get_nnode(c, nnode, iip);
599 if (IS_ERR(nnode))
600 return (void *)nnode;
602 /* Go down to level 1 */
603 while (nnode->level > 1) {
604 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
605 if (nnode->nbranch[iip].lnum)
606 break;
608 if (iip >= UBIFS_LPT_FANOUT) {
610 * Should not happen, but we need to keep going
611 * if it does.
613 iip = 0;
615 nnode = ubifs_get_nnode(c, nnode, iip);
616 if (IS_ERR(nnode))
617 return (void *)nnode;
620 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
621 if (nnode->nbranch[iip].lnum)
622 break;
623 if (iip >= UBIFS_LPT_FANOUT)
624 /* Should not happen, but we need to keep going if it does */
625 iip = 0;
626 return ubifs_get_pnode(c, nnode, iip);
630 * pnode_lookup - lookup a pnode in the LPT.
631 * @c: UBIFS file-system description object
632 * @i: pnode number (0 to main_lebs - 1)
634 * This function returns a pointer to the pnode on success or a negative
635 * error code on failure.
637 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
639 int err, h, iip, shft;
640 struct ubifs_nnode *nnode;
642 if (!c->nroot) {
643 err = ubifs_read_nnode(c, NULL, 0);
644 if (err)
645 return ERR_PTR(err);
647 i <<= UBIFS_LPT_FANOUT_SHIFT;
648 nnode = c->nroot;
649 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
650 for (h = 1; h < c->lpt_hght; h++) {
651 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
652 shft -= UBIFS_LPT_FANOUT_SHIFT;
653 nnode = ubifs_get_nnode(c, nnode, iip);
654 if (IS_ERR(nnode))
655 return ERR_CAST(nnode);
657 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
658 return ubifs_get_pnode(c, nnode, iip);
662 * add_pnode_dirt - add dirty space to LPT LEB properties.
663 * @c: UBIFS file-system description object
664 * @pnode: pnode for which to add dirt
666 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
668 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
669 c->pnode_sz);
673 * do_make_pnode_dirty - mark a pnode dirty.
674 * @c: UBIFS file-system description object
675 * @pnode: pnode to mark dirty
677 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
679 /* Assumes cnext list is empty i.e. not called during commit */
680 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
681 struct ubifs_nnode *nnode;
683 c->dirty_pn_cnt += 1;
684 add_pnode_dirt(c, pnode);
685 /* Mark parent and ancestors dirty too */
686 nnode = pnode->parent;
687 while (nnode) {
688 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
689 c->dirty_nn_cnt += 1;
690 ubifs_add_nnode_dirt(c, nnode);
691 nnode = nnode->parent;
692 } else
693 break;
699 * make_tree_dirty - mark the entire LEB properties tree dirty.
700 * @c: UBIFS file-system description object
702 * This function is used by the "small" LPT model to cause the entire LEB
703 * properties tree to be written. The "small" LPT model does not use LPT
704 * garbage collection because it is more efficient to write the entire tree
705 * (because it is small).
707 * This function returns %0 on success and a negative error code on failure.
709 static int make_tree_dirty(struct ubifs_info *c)
711 struct ubifs_pnode *pnode;
713 pnode = pnode_lookup(c, 0);
714 if (IS_ERR(pnode))
715 return PTR_ERR(pnode);
717 while (pnode) {
718 do_make_pnode_dirty(c, pnode);
719 pnode = next_pnode_to_dirty(c, pnode);
720 if (IS_ERR(pnode))
721 return PTR_ERR(pnode);
723 return 0;
727 * need_write_all - determine if the LPT area is running out of free space.
728 * @c: UBIFS file-system description object
730 * This function returns %1 if the LPT area is running out of free space and %0
731 * if it is not.
733 static int need_write_all(struct ubifs_info *c)
735 long long free = 0;
736 int i;
738 for (i = 0; i < c->lpt_lebs; i++) {
739 if (i + c->lpt_first == c->nhead_lnum)
740 free += c->leb_size - c->nhead_offs;
741 else if (c->ltab[i].free == c->leb_size)
742 free += c->leb_size;
743 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
744 free += c->leb_size;
746 /* Less than twice the size left */
747 if (free <= c->lpt_sz * 2)
748 return 1;
749 return 0;
753 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
754 * @c: UBIFS file-system description object
756 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
757 * free space and so may be reused as soon as the next commit is completed.
758 * This function is called during start commit to mark LPT LEBs for trivial GC.
760 static void lpt_tgc_start(struct ubifs_info *c)
762 int i;
764 for (i = 0; i < c->lpt_lebs; i++) {
765 if (i + c->lpt_first == c->nhead_lnum)
766 continue;
767 if (c->ltab[i].dirty > 0 &&
768 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
769 c->ltab[i].tgc = 1;
770 c->ltab[i].free = c->leb_size;
771 c->ltab[i].dirty = 0;
772 dbg_lp("LEB %d", i + c->lpt_first);
778 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
779 * @c: UBIFS file-system description object
781 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
782 * free space and so may be reused as soon as the next commit is completed.
783 * This function is called after the commit is completed (master node has been
784 * written) and un-maps LPT LEBs that were marked for trivial GC.
786 static int lpt_tgc_end(struct ubifs_info *c)
788 int i, err;
790 for (i = 0; i < c->lpt_lebs; i++)
791 if (c->ltab[i].tgc) {
792 err = ubifs_leb_unmap(c, i + c->lpt_first);
793 if (err)
794 return err;
795 c->ltab[i].tgc = 0;
796 dbg_lp("LEB %d", i + c->lpt_first);
798 return 0;
802 * populate_lsave - fill the lsave array with important LEB numbers.
803 * @c: the UBIFS file-system description object
805 * This function is only called for the "big" model. It records a small number
806 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
807 * most important to least important): empty, freeable, freeable index, dirty
808 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
809 * their pnodes into memory. That will stop us from having to scan the LPT
810 * straight away. For the "small" model we assume that scanning the LPT is no
811 * big deal.
813 static void populate_lsave(struct ubifs_info *c)
815 struct ubifs_lprops *lprops;
816 struct ubifs_lpt_heap *heap;
817 int i, cnt = 0;
819 ubifs_assert(c->big_lpt);
820 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
821 c->lpt_drty_flgs |= LSAVE_DIRTY;
822 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
825 if (dbg_populate_lsave(c))
826 return;
828 list_for_each_entry(lprops, &c->empty_list, list) {
829 c->lsave[cnt++] = lprops->lnum;
830 if (cnt >= c->lsave_cnt)
831 return;
833 list_for_each_entry(lprops, &c->freeable_list, list) {
834 c->lsave[cnt++] = lprops->lnum;
835 if (cnt >= c->lsave_cnt)
836 return;
838 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
839 c->lsave[cnt++] = lprops->lnum;
840 if (cnt >= c->lsave_cnt)
841 return;
843 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 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 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
850 for (i = 0; i < heap->cnt; i++) {
851 c->lsave[cnt++] = heap->arr[i]->lnum;
852 if (cnt >= c->lsave_cnt)
853 return;
855 heap = &c->lpt_heap[LPROPS_FREE - 1];
856 for (i = 0; i < heap->cnt; i++) {
857 c->lsave[cnt++] = heap->arr[i]->lnum;
858 if (cnt >= c->lsave_cnt)
859 return;
861 /* Fill it up completely */
862 while (cnt < c->lsave_cnt)
863 c->lsave[cnt++] = c->main_first;
867 * nnode_lookup - lookup a nnode in the LPT.
868 * @c: UBIFS file-system description object
869 * @i: nnode number
871 * This function returns a pointer to the nnode on success or a negative
872 * error code on failure.
874 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
876 int err, iip;
877 struct ubifs_nnode *nnode;
879 if (!c->nroot) {
880 err = ubifs_read_nnode(c, NULL, 0);
881 if (err)
882 return ERR_PTR(err);
884 nnode = c->nroot;
885 while (1) {
886 iip = i & (UBIFS_LPT_FANOUT - 1);
887 i >>= UBIFS_LPT_FANOUT_SHIFT;
888 if (!i)
889 break;
890 nnode = ubifs_get_nnode(c, nnode, iip);
891 if (IS_ERR(nnode))
892 return nnode;
894 return nnode;
898 * make_nnode_dirty - find a nnode and, if found, make it dirty.
899 * @c: UBIFS file-system description object
900 * @node_num: nnode number of nnode to make dirty
901 * @lnum: LEB number where nnode was written
902 * @offs: offset where nnode was written
904 * This function is used by LPT garbage collection. LPT garbage collection is
905 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
906 * simply involves marking all the nodes in the LEB being garbage-collected as
907 * dirty. The dirty nodes are written next commit, after which the LEB is free
908 * to be reused.
910 * This function returns %0 on success and a negative error code on failure.
912 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
913 int offs)
915 struct ubifs_nnode *nnode;
917 nnode = nnode_lookup(c, node_num);
918 if (IS_ERR(nnode))
919 return PTR_ERR(nnode);
920 if (nnode->parent) {
921 struct ubifs_nbranch *branch;
923 branch = &nnode->parent->nbranch[nnode->iip];
924 if (branch->lnum != lnum || branch->offs != offs)
925 return 0; /* nnode is obsolete */
926 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
927 return 0; /* nnode is obsolete */
928 /* Assumes cnext list is empty i.e. not called during commit */
929 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
930 c->dirty_nn_cnt += 1;
931 ubifs_add_nnode_dirt(c, nnode);
932 /* Mark parent and ancestors dirty too */
933 nnode = nnode->parent;
934 while (nnode) {
935 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
936 c->dirty_nn_cnt += 1;
937 ubifs_add_nnode_dirt(c, nnode);
938 nnode = nnode->parent;
939 } else
940 break;
943 return 0;
947 * make_pnode_dirty - find a pnode and, if found, make it dirty.
948 * @c: UBIFS file-system description object
949 * @node_num: pnode number of pnode to make dirty
950 * @lnum: LEB number where pnode was written
951 * @offs: offset where pnode was written
953 * This function is used by LPT garbage collection. LPT garbage collection is
954 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
955 * simply involves marking all the nodes in the LEB being garbage-collected as
956 * dirty. The dirty nodes are written next commit, after which the LEB is free
957 * to be reused.
959 * This function returns %0 on success and a negative error code on failure.
961 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
962 int offs)
964 struct ubifs_pnode *pnode;
965 struct ubifs_nbranch *branch;
967 pnode = pnode_lookup(c, node_num);
968 if (IS_ERR(pnode))
969 return PTR_ERR(pnode);
970 branch = &pnode->parent->nbranch[pnode->iip];
971 if (branch->lnum != lnum || branch->offs != offs)
972 return 0;
973 do_make_pnode_dirty(c, pnode);
974 return 0;
978 * make_ltab_dirty - make ltab node dirty.
979 * @c: UBIFS file-system description object
980 * @lnum: LEB number where ltab was written
981 * @offs: offset where ltab was written
983 * This function is used by LPT garbage collection. LPT garbage collection is
984 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
985 * simply involves marking all the nodes in the LEB being garbage-collected as
986 * dirty. The dirty nodes are written next commit, after which the LEB is free
987 * to be reused.
989 * This function returns %0 on success and a negative error code on failure.
991 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
993 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
994 return 0; /* This ltab node is obsolete */
995 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
996 c->lpt_drty_flgs |= LTAB_DIRTY;
997 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
999 return 0;
1003 * make_lsave_dirty - make lsave node dirty.
1004 * @c: UBIFS file-system description object
1005 * @lnum: LEB number where lsave was written
1006 * @offs: offset where lsave was written
1008 * This function is used by LPT garbage collection. LPT garbage collection is
1009 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1010 * simply involves marking all the nodes in the LEB being garbage-collected as
1011 * dirty. The dirty nodes are written next commit, after which the LEB is free
1012 * to be reused.
1014 * This function returns %0 on success and a negative error code on failure.
1016 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1018 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1019 return 0; /* This lsave node is obsolete */
1020 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1021 c->lpt_drty_flgs |= LSAVE_DIRTY;
1022 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1024 return 0;
1028 * make_node_dirty - make node dirty.
1029 * @c: UBIFS file-system description object
1030 * @node_type: LPT node type
1031 * @node_num: node number
1032 * @lnum: LEB number where node was written
1033 * @offs: offset where node was written
1035 * This function is used by LPT garbage collection. LPT garbage collection is
1036 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1037 * simply involves marking all the nodes in the LEB being garbage-collected as
1038 * dirty. The dirty nodes are written next commit, after which the LEB is free
1039 * to be reused.
1041 * This function returns %0 on success and a negative error code on failure.
1043 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1044 int lnum, int offs)
1046 switch (node_type) {
1047 case UBIFS_LPT_NNODE:
1048 return make_nnode_dirty(c, node_num, lnum, offs);
1049 case UBIFS_LPT_PNODE:
1050 return make_pnode_dirty(c, node_num, lnum, offs);
1051 case UBIFS_LPT_LTAB:
1052 return make_ltab_dirty(c, lnum, offs);
1053 case UBIFS_LPT_LSAVE:
1054 return make_lsave_dirty(c, lnum, offs);
1056 return -EINVAL;
1060 * get_lpt_node_len - return the length of a node based on its type.
1061 * @c: UBIFS file-system description object
1062 * @node_type: LPT node type
1064 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1066 switch (node_type) {
1067 case UBIFS_LPT_NNODE:
1068 return c->nnode_sz;
1069 case UBIFS_LPT_PNODE:
1070 return c->pnode_sz;
1071 case UBIFS_LPT_LTAB:
1072 return c->ltab_sz;
1073 case UBIFS_LPT_LSAVE:
1074 return c->lsave_sz;
1076 return 0;
1080 * get_pad_len - return the length of padding in a buffer.
1081 * @c: UBIFS file-system description object
1082 * @buf: buffer
1083 * @len: length of buffer
1085 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1087 int offs, pad_len;
1089 if (c->min_io_size == 1)
1090 return 0;
1091 offs = c->leb_size - len;
1092 pad_len = ALIGN(offs, c->min_io_size) - offs;
1093 return pad_len;
1097 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1098 * @c: UBIFS file-system description object
1099 * @buf: buffer
1100 * @node_num: node number is returned here
1102 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1103 int *node_num)
1105 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1106 int pos = 0, node_type;
1108 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1109 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1110 return node_type;
1114 * is_a_node - determine if a buffer contains a node.
1115 * @c: UBIFS file-system description object
1116 * @buf: buffer
1117 * @len: length of buffer
1119 * This function returns %1 if the buffer contains a node or %0 if it does not.
1121 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1123 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1124 int pos = 0, node_type, node_len;
1125 uint16_t crc, calc_crc;
1127 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1128 return 0;
1129 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1130 if (node_type == UBIFS_LPT_NOT_A_NODE)
1131 return 0;
1132 node_len = get_lpt_node_len(c, node_type);
1133 if (!node_len || node_len > len)
1134 return 0;
1135 pos = 0;
1136 addr = buf;
1137 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1138 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1139 node_len - UBIFS_LPT_CRC_BYTES);
1140 if (crc != calc_crc)
1141 return 0;
1142 return 1;
1146 * lpt_gc_lnum - garbage collect a LPT LEB.
1147 * @c: UBIFS file-system description object
1148 * @lnum: LEB number to garbage collect
1150 * LPT garbage collection is used only for the "big" LPT model
1151 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1152 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1153 * next commit, after which the LEB is free to be reused.
1155 * This function returns %0 on success and a negative error code on failure.
1157 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1159 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1160 void *buf = c->lpt_buf;
1162 dbg_lp("LEB %d", lnum);
1163 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1164 if (err) {
1165 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1166 return err;
1168 while (1) {
1169 if (!is_a_node(c, buf, len)) {
1170 int pad_len;
1172 pad_len = get_pad_len(c, buf, len);
1173 if (pad_len) {
1174 buf += pad_len;
1175 len -= pad_len;
1176 continue;
1178 return 0;
1180 node_type = get_lpt_node_type(c, buf, &node_num);
1181 node_len = get_lpt_node_len(c, node_type);
1182 offs = c->leb_size - len;
1183 ubifs_assert(node_len != 0);
1184 mutex_lock(&c->lp_mutex);
1185 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1186 mutex_unlock(&c->lp_mutex);
1187 if (err)
1188 return err;
1189 buf += node_len;
1190 len -= node_len;
1192 return 0;
1196 * lpt_gc - LPT garbage collection.
1197 * @c: UBIFS file-system description object
1199 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1200 * Returns %0 on success and a negative error code on failure.
1202 static int lpt_gc(struct ubifs_info *c)
1204 int i, lnum = -1, dirty = 0;
1206 mutex_lock(&c->lp_mutex);
1207 for (i = 0; i < c->lpt_lebs; i++) {
1208 ubifs_assert(!c->ltab[i].tgc);
1209 if (i + c->lpt_first == c->nhead_lnum ||
1210 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1211 continue;
1212 if (c->ltab[i].dirty > dirty) {
1213 dirty = c->ltab[i].dirty;
1214 lnum = i + c->lpt_first;
1217 mutex_unlock(&c->lp_mutex);
1218 if (lnum == -1)
1219 return -ENOSPC;
1220 return lpt_gc_lnum(c, lnum);
1224 * ubifs_lpt_start_commit - UBIFS commit starts.
1225 * @c: the UBIFS file-system description object
1227 * This function has to be called when UBIFS starts the commit operation.
1228 * This function "freezes" all currently dirty LEB properties and does not
1229 * change them anymore. Further changes are saved and tracked separately
1230 * because they are not part of this commit. This function returns zero in case
1231 * of success and a negative error code in case of failure.
1233 int ubifs_lpt_start_commit(struct ubifs_info *c)
1235 int err, cnt;
1237 dbg_lp("");
1239 mutex_lock(&c->lp_mutex);
1240 err = dbg_chk_lpt_free_spc(c);
1241 if (err)
1242 goto out;
1243 err = dbg_check_ltab(c);
1244 if (err)
1245 goto out;
1247 if (c->check_lpt_free) {
1249 * We ensure there is enough free space in
1250 * ubifs_lpt_post_commit() by marking nodes dirty. That
1251 * information is lost when we unmount, so we also need
1252 * to check free space once after mounting also.
1254 c->check_lpt_free = 0;
1255 while (need_write_all(c)) {
1256 mutex_unlock(&c->lp_mutex);
1257 err = lpt_gc(c);
1258 if (err)
1259 return err;
1260 mutex_lock(&c->lp_mutex);
1264 lpt_tgc_start(c);
1266 if (!c->dirty_pn_cnt) {
1267 dbg_cmt("no cnodes to commit");
1268 err = 0;
1269 goto out;
1272 if (!c->big_lpt && need_write_all(c)) {
1273 /* If needed, write everything */
1274 err = make_tree_dirty(c);
1275 if (err)
1276 goto out;
1277 lpt_tgc_start(c);
1280 if (c->big_lpt)
1281 populate_lsave(c);
1283 cnt = get_cnodes_to_commit(c);
1284 ubifs_assert(cnt != 0);
1286 err = layout_cnodes(c);
1287 if (err)
1288 goto out;
1290 /* Copy the LPT's own lprops for end commit to write */
1291 memcpy(c->ltab_cmt, c->ltab,
1292 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1293 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1295 out:
1296 mutex_unlock(&c->lp_mutex);
1297 return err;
1301 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1302 * @c: UBIFS file-system description object
1304 static void free_obsolete_cnodes(struct ubifs_info *c)
1306 struct ubifs_cnode *cnode, *cnext;
1308 cnext = c->lpt_cnext;
1309 if (!cnext)
1310 return;
1311 do {
1312 cnode = cnext;
1313 cnext = cnode->cnext;
1314 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1315 kfree(cnode);
1316 else
1317 cnode->cnext = NULL;
1318 } while (cnext != c->lpt_cnext);
1319 c->lpt_cnext = NULL;
1323 * ubifs_lpt_end_commit - finish the commit operation.
1324 * @c: the UBIFS file-system description object
1326 * This function has to be called when the commit operation finishes. It
1327 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1328 * the media. Returns zero in case of success and a negative error code in case
1329 * of failure.
1331 int ubifs_lpt_end_commit(struct ubifs_info *c)
1333 int err;
1335 dbg_lp("");
1337 if (!c->lpt_cnext)
1338 return 0;
1340 err = write_cnodes(c);
1341 if (err)
1342 return err;
1344 mutex_lock(&c->lp_mutex);
1345 free_obsolete_cnodes(c);
1346 mutex_unlock(&c->lp_mutex);
1348 return 0;
1352 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1353 * @c: UBIFS file-system description object
1355 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1356 * commit for the "big" LPT model.
1358 int ubifs_lpt_post_commit(struct ubifs_info *c)
1360 int err;
1362 mutex_lock(&c->lp_mutex);
1363 err = lpt_tgc_end(c);
1364 if (err)
1365 goto out;
1366 if (c->big_lpt)
1367 while (need_write_all(c)) {
1368 mutex_unlock(&c->lp_mutex);
1369 err = lpt_gc(c);
1370 if (err)
1371 return err;
1372 mutex_lock(&c->lp_mutex);
1374 out:
1375 mutex_unlock(&c->lp_mutex);
1376 return err;
1380 * first_nnode - find the first nnode in memory.
1381 * @c: UBIFS file-system description object
1382 * @hght: height of tree where nnode found is returned here
1384 * This function returns a pointer to the nnode found or %NULL if no nnode is
1385 * found. This function is a helper to 'ubifs_lpt_free()'.
1387 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1389 struct ubifs_nnode *nnode;
1390 int h, i, found;
1392 nnode = c->nroot;
1393 *hght = 0;
1394 if (!nnode)
1395 return NULL;
1396 for (h = 1; h < c->lpt_hght; h++) {
1397 found = 0;
1398 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1399 if (nnode->nbranch[i].nnode) {
1400 found = 1;
1401 nnode = nnode->nbranch[i].nnode;
1402 *hght = h;
1403 break;
1406 if (!found)
1407 break;
1409 return nnode;
1413 * next_nnode - find the next nnode in memory.
1414 * @c: UBIFS file-system description object
1415 * @nnode: nnode from which to start.
1416 * @hght: height of tree where nnode is, is passed and returned here
1418 * This function returns a pointer to the nnode found or %NULL if no nnode is
1419 * found. This function is a helper to 'ubifs_lpt_free()'.
1421 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1422 struct ubifs_nnode *nnode, int *hght)
1424 struct ubifs_nnode *parent;
1425 int iip, h, i, found;
1427 parent = nnode->parent;
1428 if (!parent)
1429 return NULL;
1430 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1431 *hght -= 1;
1432 return parent;
1434 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1435 nnode = parent->nbranch[iip].nnode;
1436 if (nnode)
1437 break;
1439 if (!nnode) {
1440 *hght -= 1;
1441 return parent;
1443 for (h = *hght + 1; h < c->lpt_hght; h++) {
1444 found = 0;
1445 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1446 if (nnode->nbranch[i].nnode) {
1447 found = 1;
1448 nnode = nnode->nbranch[i].nnode;
1449 *hght = h;
1450 break;
1453 if (!found)
1454 break;
1456 return nnode;
1460 * ubifs_lpt_free - free resources owned by the LPT.
1461 * @c: UBIFS file-system description object
1462 * @wr_only: free only resources used for writing
1464 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1466 struct ubifs_nnode *nnode;
1467 int i, hght;
1469 /* Free write-only things first */
1471 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1473 vfree(c->ltab_cmt);
1474 c->ltab_cmt = NULL;
1475 vfree(c->lpt_buf);
1476 c->lpt_buf = NULL;
1477 kfree(c->lsave);
1478 c->lsave = NULL;
1480 if (wr_only)
1481 return;
1483 /* Now free the rest */
1485 nnode = first_nnode(c, &hght);
1486 while (nnode) {
1487 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1488 kfree(nnode->nbranch[i].nnode);
1489 nnode = next_nnode(c, nnode, &hght);
1491 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1492 kfree(c->lpt_heap[i].arr);
1493 kfree(c->dirty_idx.arr);
1494 kfree(c->nroot);
1495 vfree(c->ltab);
1496 kfree(c->lpt_nod_buf);
1499 #ifdef CONFIG_UBIFS_FS_DEBUG
1502 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1503 * @buf: buffer
1504 * @len: buffer length
1506 static int dbg_is_all_ff(uint8_t *buf, int len)
1508 int i;
1510 for (i = 0; i < len; i++)
1511 if (buf[i] != 0xff)
1512 return 0;
1513 return 1;
1517 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1518 * @c: the UBIFS file-system description object
1519 * @lnum: LEB number where nnode was written
1520 * @offs: offset where nnode was written
1522 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1524 struct ubifs_nnode *nnode;
1525 int hght;
1527 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1528 nnode = first_nnode(c, &hght);
1529 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1530 struct ubifs_nbranch *branch;
1532 cond_resched();
1533 if (nnode->parent) {
1534 branch = &nnode->parent->nbranch[nnode->iip];
1535 if (branch->lnum != lnum || branch->offs != offs)
1536 continue;
1537 if (test_bit(DIRTY_CNODE, &nnode->flags))
1538 return 1;
1539 return 0;
1540 } else {
1541 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1542 continue;
1543 if (test_bit(DIRTY_CNODE, &nnode->flags))
1544 return 1;
1545 return 0;
1548 return 1;
1552 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1553 * @c: the UBIFS file-system description object
1554 * @lnum: LEB number where pnode was written
1555 * @offs: offset where pnode was written
1557 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1559 int i, cnt;
1561 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1562 for (i = 0; i < cnt; i++) {
1563 struct ubifs_pnode *pnode;
1564 struct ubifs_nbranch *branch;
1566 cond_resched();
1567 pnode = pnode_lookup(c, i);
1568 if (IS_ERR(pnode))
1569 return PTR_ERR(pnode);
1570 branch = &pnode->parent->nbranch[pnode->iip];
1571 if (branch->lnum != lnum || branch->offs != offs)
1572 continue;
1573 if (test_bit(DIRTY_CNODE, &pnode->flags))
1574 return 1;
1575 return 0;
1577 return 1;
1581 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1582 * @c: the UBIFS file-system description object
1583 * @lnum: LEB number where ltab node was written
1584 * @offs: offset where ltab node was written
1586 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1588 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1589 return 1;
1590 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1594 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1595 * @c: the UBIFS file-system description object
1596 * @lnum: LEB number where lsave node was written
1597 * @offs: offset where lsave node was written
1599 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1601 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1602 return 1;
1603 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1607 * dbg_is_node_dirty - determine if a node is dirty.
1608 * @c: the UBIFS file-system description object
1609 * @node_type: node type
1610 * @lnum: LEB number where node was written
1611 * @offs: offset where node was written
1613 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1614 int offs)
1616 switch (node_type) {
1617 case UBIFS_LPT_NNODE:
1618 return dbg_is_nnode_dirty(c, lnum, offs);
1619 case UBIFS_LPT_PNODE:
1620 return dbg_is_pnode_dirty(c, lnum, offs);
1621 case UBIFS_LPT_LTAB:
1622 return dbg_is_ltab_dirty(c, lnum, offs);
1623 case UBIFS_LPT_LSAVE:
1624 return dbg_is_lsave_dirty(c, lnum, offs);
1626 return 1;
1630 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1631 * @c: the UBIFS file-system description object
1632 * @lnum: LEB number where node was written
1633 * @offs: offset where node was written
1635 * This function returns %0 on success and a negative error code on failure.
1637 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1639 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1640 int ret;
1641 void *buf, *p;
1643 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1644 return 0;
1646 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1647 if (!buf) {
1648 ubifs_err("cannot allocate memory for ltab checking");
1649 return 0;
1652 dbg_lp("LEB %d", lnum);
1653 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1654 if (err) {
1655 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1656 goto out;
1658 while (1) {
1659 if (!is_a_node(c, p, len)) {
1660 int i, pad_len;
1662 pad_len = get_pad_len(c, p, len);
1663 if (pad_len) {
1664 p += pad_len;
1665 len -= pad_len;
1666 dirty += pad_len;
1667 continue;
1669 if (!dbg_is_all_ff(p, len)) {
1670 dbg_msg("invalid empty space in LEB %d at %d",
1671 lnum, c->leb_size - len);
1672 err = -EINVAL;
1674 i = lnum - c->lpt_first;
1675 if (len != c->ltab[i].free) {
1676 dbg_msg("invalid free space in LEB %d "
1677 "(free %d, expected %d)",
1678 lnum, len, c->ltab[i].free);
1679 err = -EINVAL;
1681 if (dirty != c->ltab[i].dirty) {
1682 dbg_msg("invalid dirty space in LEB %d "
1683 "(dirty %d, expected %d)",
1684 lnum, dirty, c->ltab[i].dirty);
1685 err = -EINVAL;
1687 goto out;
1689 node_type = get_lpt_node_type(c, p, &node_num);
1690 node_len = get_lpt_node_len(c, node_type);
1691 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1692 if (ret == 1)
1693 dirty += node_len;
1694 p += node_len;
1695 len -= node_len;
1698 err = 0;
1699 out:
1700 vfree(buf);
1701 return err;
1705 * dbg_check_ltab - check the free and dirty space in the ltab.
1706 * @c: the UBIFS file-system description object
1708 * This function returns %0 on success and a negative error code on failure.
1710 int dbg_check_ltab(struct ubifs_info *c)
1712 int lnum, err, i, cnt;
1714 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1715 return 0;
1717 /* Bring the entire tree into memory */
1718 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1719 for (i = 0; i < cnt; i++) {
1720 struct ubifs_pnode *pnode;
1722 pnode = pnode_lookup(c, i);
1723 if (IS_ERR(pnode))
1724 return PTR_ERR(pnode);
1725 cond_resched();
1728 /* Check nodes */
1729 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1730 if (err)
1731 return err;
1733 /* Check each LEB */
1734 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1735 err = dbg_check_ltab_lnum(c, lnum);
1736 if (err) {
1737 dbg_err("failed at LEB %d", lnum);
1738 return err;
1742 dbg_lp("succeeded");
1743 return 0;
1747 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1748 * @c: the UBIFS file-system description object
1750 * This function returns %0 on success and a negative error code on failure.
1752 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1754 long long free = 0;
1755 int i;
1757 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1758 return 0;
1760 for (i = 0; i < c->lpt_lebs; i++) {
1761 if (c->ltab[i].tgc || c->ltab[i].cmt)
1762 continue;
1763 if (i + c->lpt_first == c->nhead_lnum)
1764 free += c->leb_size - c->nhead_offs;
1765 else if (c->ltab[i].free == c->leb_size)
1766 free += c->leb_size;
1768 if (free < c->lpt_sz) {
1769 dbg_err("LPT space error: free %lld lpt_sz %lld",
1770 free, c->lpt_sz);
1771 dbg_dump_lpt_info(c);
1772 dbg_dump_lpt_lebs(c);
1773 dump_stack();
1774 return -EINVAL;
1776 return 0;
1780 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1781 * @c: the UBIFS file-system description object
1782 * @action: what to do
1783 * @len: length written
1785 * This function returns %0 on success and a negative error code on failure.
1786 * The @action argument may be one of:
1787 * o %0 - LPT debugging checking starts, initialize debugging variables;
1788 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1789 * o %2 - switched to a different LEB and wasted @len bytes;
1790 * o %3 - check that we've written the right number of bytes.
1791 * o %4 - wasted @len bytes;
1793 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1795 struct ubifs_debug_info *d = c->dbg;
1796 long long chk_lpt_sz, lpt_sz;
1797 int err = 0;
1799 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1800 return 0;
1802 switch (action) {
1803 case 0:
1804 d->chk_lpt_sz = 0;
1805 d->chk_lpt_sz2 = 0;
1806 d->chk_lpt_lebs = 0;
1807 d->chk_lpt_wastage = 0;
1808 if (c->dirty_pn_cnt > c->pnode_cnt) {
1809 dbg_err("dirty pnodes %d exceed max %d",
1810 c->dirty_pn_cnt, c->pnode_cnt);
1811 err = -EINVAL;
1813 if (c->dirty_nn_cnt > c->nnode_cnt) {
1814 dbg_err("dirty nnodes %d exceed max %d",
1815 c->dirty_nn_cnt, c->nnode_cnt);
1816 err = -EINVAL;
1818 return err;
1819 case 1:
1820 d->chk_lpt_sz += len;
1821 return 0;
1822 case 2:
1823 d->chk_lpt_sz += len;
1824 d->chk_lpt_wastage += len;
1825 d->chk_lpt_lebs += 1;
1826 return 0;
1827 case 3:
1828 chk_lpt_sz = c->leb_size;
1829 chk_lpt_sz *= d->chk_lpt_lebs;
1830 chk_lpt_sz += len - c->nhead_offs;
1831 if (d->chk_lpt_sz != chk_lpt_sz) {
1832 dbg_err("LPT wrote %lld but space used was %lld",
1833 d->chk_lpt_sz, chk_lpt_sz);
1834 err = -EINVAL;
1836 if (d->chk_lpt_sz > c->lpt_sz) {
1837 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1838 d->chk_lpt_sz, c->lpt_sz);
1839 err = -EINVAL;
1841 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1842 dbg_err("LPT layout size %lld but wrote %lld",
1843 d->chk_lpt_sz, d->chk_lpt_sz2);
1844 err = -EINVAL;
1846 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1847 dbg_err("LPT new nhead offs: expected %d was %d",
1848 d->new_nhead_offs, len);
1849 err = -EINVAL;
1851 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1852 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1853 lpt_sz += c->ltab_sz;
1854 if (c->big_lpt)
1855 lpt_sz += c->lsave_sz;
1856 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1857 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1858 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1859 err = -EINVAL;
1861 if (err) {
1862 dbg_dump_lpt_info(c);
1863 dbg_dump_lpt_lebs(c);
1864 dump_stack();
1866 d->chk_lpt_sz2 = d->chk_lpt_sz;
1867 d->chk_lpt_sz = 0;
1868 d->chk_lpt_wastage = 0;
1869 d->chk_lpt_lebs = 0;
1870 d->new_nhead_offs = len;
1871 return err;
1872 case 4:
1873 d->chk_lpt_sz += len;
1874 d->chk_lpt_wastage += len;
1875 return 0;
1876 default:
1877 return -EINVAL;
1882 * dbg_dump_lpt_leb - dump an LPT LEB.
1883 * @c: UBIFS file-system description object
1884 * @lnum: LEB number to dump
1886 * This function dumps an LEB from LPT area. Nodes in this area are very
1887 * different to nodes in the main area (e.g., they do not have common headers,
1888 * they do not have 8-byte alignments, etc), so we have a separate function to
1889 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1891 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1893 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1894 void *buf, *p;
1896 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1897 current->pid, lnum);
1898 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1899 if (!buf) {
1900 ubifs_err("cannot allocate memory to dump LPT");
1901 return;
1904 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1905 if (err) {
1906 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1907 goto out;
1909 while (1) {
1910 offs = c->leb_size - len;
1911 if (!is_a_node(c, p, len)) {
1912 int pad_len;
1914 pad_len = get_pad_len(c, p, len);
1915 if (pad_len) {
1916 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1917 lnum, offs, pad_len);
1918 p += pad_len;
1919 len -= pad_len;
1920 continue;
1922 if (len)
1923 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1924 lnum, offs, len);
1925 break;
1928 node_type = get_lpt_node_type(c, p, &node_num);
1929 switch (node_type) {
1930 case UBIFS_LPT_PNODE:
1932 node_len = c->pnode_sz;
1933 if (c->big_lpt)
1934 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1935 lnum, offs, node_num);
1936 else
1937 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1938 lnum, offs);
1939 break;
1941 case UBIFS_LPT_NNODE:
1943 int i;
1944 struct ubifs_nnode nnode;
1946 node_len = c->nnode_sz;
1947 if (c->big_lpt)
1948 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1949 lnum, offs, node_num);
1950 else
1951 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1952 lnum, offs);
1953 err = ubifs_unpack_nnode(c, p, &nnode);
1954 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1955 printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
1956 nnode.nbranch[i].offs);
1957 if (i != UBIFS_LPT_FANOUT - 1)
1958 printk(KERN_CONT ", ");
1960 printk(KERN_CONT "\n");
1961 break;
1963 case UBIFS_LPT_LTAB:
1964 node_len = c->ltab_sz;
1965 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1966 lnum, offs);
1967 break;
1968 case UBIFS_LPT_LSAVE:
1969 node_len = c->lsave_sz;
1970 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1971 break;
1972 default:
1973 ubifs_err("LPT node type %d not recognized", node_type);
1974 goto out;
1977 p += node_len;
1978 len -= node_len;
1981 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1982 current->pid, lnum);
1983 out:
1984 vfree(buf);
1985 return;
1989 * dbg_dump_lpt_lebs - dump LPT lebs.
1990 * @c: UBIFS file-system description object
1992 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1993 * locked.
1995 void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1997 int i;
1999 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
2000 current->pid);
2001 for (i = 0; i < c->lpt_lebs; i++)
2002 dump_lpt_leb(c, i + c->lpt_first);
2003 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
2004 current->pid);
2008 * dbg_populate_lsave - debugging version of 'populate_lsave()'
2009 * @c: UBIFS file-system description object
2011 * This is a debugging version for 'populate_lsave()' which populates lsave
2012 * with random LEBs instead of useful LEBs, which is good for test coverage.
2013 * Returns zero if lsave has not been populated (this debugging feature is
2014 * disabled) an non-zero if lsave has been populated.
2016 static int dbg_populate_lsave(struct ubifs_info *c)
2018 struct ubifs_lprops *lprops;
2019 struct ubifs_lpt_heap *heap;
2020 int i;
2022 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2023 return 0;
2024 if (random32() & 3)
2025 return 0;
2027 for (i = 0; i < c->lsave_cnt; i++)
2028 c->lsave[i] = c->main_first;
2030 list_for_each_entry(lprops, &c->empty_list, list)
2031 c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
2032 list_for_each_entry(lprops, &c->freeable_list, list)
2033 c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
2034 list_for_each_entry(lprops, &c->frdi_idx_list, list)
2035 c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
2037 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2038 for (i = 0; i < heap->cnt; i++)
2039 c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
2040 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2041 for (i = 0; i < heap->cnt; i++)
2042 c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
2043 heap = &c->lpt_heap[LPROPS_FREE - 1];
2044 for (i = 0; i < heap->cnt; i++)
2045 c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
2047 return 1;
2050 #endif /* CONFIG_UBIFS_FS_DEBUG */