ASoC: add missing clk_put to nuc900-ac97
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / lpt_commit.c
blob0c9c69bd983a94c0854d90508a15832b99b6fe0f
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 /**
33 * first_dirty_cnode - find first dirty cnode.
34 * @c: UBIFS file-system description object
35 * @nnode: nnode at which to start
37 * This function returns the first dirty cnode or %NULL if there is not one.
39 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
41 ubifs_assert(nnode);
42 while (1) {
43 int i, cont = 0;
45 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
46 struct ubifs_cnode *cnode;
48 cnode = nnode->nbranch[i].cnode;
49 if (cnode &&
50 test_bit(DIRTY_CNODE, &cnode->flags)) {
51 if (cnode->level == 0)
52 return cnode;
53 nnode = (struct ubifs_nnode *)cnode;
54 cont = 1;
55 break;
58 if (!cont)
59 return (struct ubifs_cnode *)nnode;
63 /**
64 * next_dirty_cnode - find next dirty cnode.
65 * @cnode: cnode from which to begin searching
67 * This function returns the next dirty cnode or %NULL if there is not one.
69 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
71 struct ubifs_nnode *nnode;
72 int i;
74 ubifs_assert(cnode);
75 nnode = cnode->parent;
76 if (!nnode)
77 return NULL;
78 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
79 cnode = nnode->nbranch[i].cnode;
80 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
81 if (cnode->level == 0)
82 return cnode; /* cnode is a pnode */
83 /* cnode is a nnode */
84 return first_dirty_cnode((struct ubifs_nnode *)cnode);
87 return (struct ubifs_cnode *)nnode;
90 /**
91 * get_cnodes_to_commit - create list of dirty cnodes to commit.
92 * @c: UBIFS file-system description object
94 * This function returns the number of cnodes to commit.
96 static int get_cnodes_to_commit(struct ubifs_info *c)
98 struct ubifs_cnode *cnode, *cnext;
99 int cnt = 0;
101 if (!c->nroot)
102 return 0;
104 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
105 return 0;
107 c->lpt_cnext = first_dirty_cnode(c->nroot);
108 cnode = c->lpt_cnext;
109 if (!cnode)
110 return 0;
111 cnt += 1;
112 while (1) {
113 ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
114 __set_bit(COW_ZNODE, &cnode->flags);
115 cnext = next_dirty_cnode(cnode);
116 if (!cnext) {
117 cnode->cnext = c->lpt_cnext;
118 break;
120 cnode->cnext = cnext;
121 cnode = cnext;
122 cnt += 1;
124 dbg_cmt("committing %d cnodes", cnt);
125 dbg_lp("committing %d cnodes", cnt);
126 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
127 return cnt;
131 * upd_ltab - update LPT LEB properties.
132 * @c: UBIFS file-system description object
133 * @lnum: LEB number
134 * @free: amount of free space
135 * @dirty: amount of dirty space to add
137 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
139 dbg_lp("LEB %d free %d dirty %d to %d +%d",
140 lnum, c->ltab[lnum - c->lpt_first].free,
141 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
142 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
143 c->ltab[lnum - c->lpt_first].free = free;
144 c->ltab[lnum - c->lpt_first].dirty += dirty;
148 * alloc_lpt_leb - allocate an LPT LEB that is empty.
149 * @c: UBIFS file-system description object
150 * @lnum: LEB number is passed and returned here
152 * This function finds the next empty LEB in the ltab starting from @lnum. If a
153 * an empty LEB is found it is returned in @lnum and the function returns %0.
154 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
155 * never to run out of space.
157 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
159 int i, n;
161 n = *lnum - c->lpt_first + 1;
162 for (i = n; i < c->lpt_lebs; i++) {
163 if (c->ltab[i].tgc || c->ltab[i].cmt)
164 continue;
165 if (c->ltab[i].free == c->leb_size) {
166 c->ltab[i].cmt = 1;
167 *lnum = i + c->lpt_first;
168 return 0;
172 for (i = 0; i < n; i++) {
173 if (c->ltab[i].tgc || c->ltab[i].cmt)
174 continue;
175 if (c->ltab[i].free == c->leb_size) {
176 c->ltab[i].cmt = 1;
177 *lnum = i + c->lpt_first;
178 return 0;
181 return -ENOSPC;
185 * layout_cnodes - layout cnodes for commit.
186 * @c: UBIFS file-system description object
188 * This function returns %0 on success and a negative error code on failure.
190 static int layout_cnodes(struct ubifs_info *c)
192 int lnum, offs, len, alen, done_lsave, done_ltab, err;
193 struct ubifs_cnode *cnode;
195 err = dbg_chk_lpt_sz(c, 0, 0);
196 if (err)
197 return err;
198 cnode = c->lpt_cnext;
199 if (!cnode)
200 return 0;
201 lnum = c->nhead_lnum;
202 offs = c->nhead_offs;
203 /* Try to place lsave and ltab nicely */
204 done_lsave = !c->big_lpt;
205 done_ltab = 0;
206 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
207 done_lsave = 1;
208 c->lsave_lnum = lnum;
209 c->lsave_offs = offs;
210 offs += c->lsave_sz;
211 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
214 if (offs + c->ltab_sz <= c->leb_size) {
215 done_ltab = 1;
216 c->ltab_lnum = lnum;
217 c->ltab_offs = offs;
218 offs += c->ltab_sz;
219 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
222 do {
223 if (cnode->level) {
224 len = c->nnode_sz;
225 c->dirty_nn_cnt -= 1;
226 } else {
227 len = c->pnode_sz;
228 c->dirty_pn_cnt -= 1;
230 while (offs + len > c->leb_size) {
231 alen = ALIGN(offs, c->min_io_size);
232 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
233 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
234 err = alloc_lpt_leb(c, &lnum);
235 if (err)
236 goto no_space;
237 offs = 0;
238 ubifs_assert(lnum >= c->lpt_first &&
239 lnum <= c->lpt_last);
240 /* Try to place lsave and ltab nicely */
241 if (!done_lsave) {
242 done_lsave = 1;
243 c->lsave_lnum = lnum;
244 c->lsave_offs = offs;
245 offs += c->lsave_sz;
246 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
247 continue;
249 if (!done_ltab) {
250 done_ltab = 1;
251 c->ltab_lnum = lnum;
252 c->ltab_offs = offs;
253 offs += c->ltab_sz;
254 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
255 continue;
257 break;
259 if (cnode->parent) {
260 cnode->parent->nbranch[cnode->iip].lnum = lnum;
261 cnode->parent->nbranch[cnode->iip].offs = offs;
262 } else {
263 c->lpt_lnum = lnum;
264 c->lpt_offs = offs;
266 offs += len;
267 dbg_chk_lpt_sz(c, 1, len);
268 cnode = cnode->cnext;
269 } while (cnode && cnode != c->lpt_cnext);
271 /* Make sure to place LPT's save table */
272 if (!done_lsave) {
273 if (offs + c->lsave_sz > c->leb_size) {
274 alen = ALIGN(offs, c->min_io_size);
275 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
276 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
277 err = alloc_lpt_leb(c, &lnum);
278 if (err)
279 goto no_space;
280 offs = 0;
281 ubifs_assert(lnum >= c->lpt_first &&
282 lnum <= c->lpt_last);
284 done_lsave = 1;
285 c->lsave_lnum = lnum;
286 c->lsave_offs = offs;
287 offs += c->lsave_sz;
288 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
291 /* Make sure to place LPT's own lprops table */
292 if (!done_ltab) {
293 if (offs + c->ltab_sz > c->leb_size) {
294 alen = ALIGN(offs, c->min_io_size);
295 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
296 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
297 err = alloc_lpt_leb(c, &lnum);
298 if (err)
299 goto no_space;
300 offs = 0;
301 ubifs_assert(lnum >= c->lpt_first &&
302 lnum <= c->lpt_last);
304 done_ltab = 1;
305 c->ltab_lnum = lnum;
306 c->ltab_offs = offs;
307 offs += c->ltab_sz;
308 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
311 alen = ALIGN(offs, c->min_io_size);
312 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
313 dbg_chk_lpt_sz(c, 4, alen - offs);
314 err = dbg_chk_lpt_sz(c, 3, alen);
315 if (err)
316 return err;
317 return 0;
319 no_space:
320 ubifs_err("LPT out of space");
321 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
322 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
323 dbg_dump_lpt_info(c);
324 dbg_dump_lpt_lebs(c);
325 dump_stack();
326 return err;
330 * realloc_lpt_leb - allocate an LPT LEB that is empty.
331 * @c: UBIFS file-system description object
332 * @lnum: LEB number is passed and returned here
334 * This function duplicates exactly the results of the function alloc_lpt_leb.
335 * It is used during end commit to reallocate the same LEB numbers that were
336 * allocated by alloc_lpt_leb during start commit.
338 * This function finds the next LEB that was allocated by the alloc_lpt_leb
339 * function starting from @lnum. If a LEB is found it is returned in @lnum and
340 * the function returns %0. Otherwise the function returns -ENOSPC.
341 * Note however, that LPT is designed never to run out of space.
343 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
345 int i, n;
347 n = *lnum - c->lpt_first + 1;
348 for (i = n; i < c->lpt_lebs; i++)
349 if (c->ltab[i].cmt) {
350 c->ltab[i].cmt = 0;
351 *lnum = i + c->lpt_first;
352 return 0;
355 for (i = 0; i < n; i++)
356 if (c->ltab[i].cmt) {
357 c->ltab[i].cmt = 0;
358 *lnum = i + c->lpt_first;
359 return 0;
361 return -ENOSPC;
365 * write_cnodes - write cnodes for commit.
366 * @c: UBIFS file-system description object
368 * This function returns %0 on success and a negative error code on failure.
370 static int write_cnodes(struct ubifs_info *c)
372 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
373 struct ubifs_cnode *cnode;
374 void *buf = c->lpt_buf;
376 cnode = c->lpt_cnext;
377 if (!cnode)
378 return 0;
379 lnum = c->nhead_lnum;
380 offs = c->nhead_offs;
381 from = offs;
382 /* Ensure empty LEB is unmapped */
383 if (offs == 0) {
384 err = ubifs_leb_unmap(c, lnum);
385 if (err)
386 return err;
388 /* Try to place lsave and ltab nicely */
389 done_lsave = !c->big_lpt;
390 done_ltab = 0;
391 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
392 done_lsave = 1;
393 ubifs_pack_lsave(c, buf + offs, c->lsave);
394 offs += c->lsave_sz;
395 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
398 if (offs + c->ltab_sz <= c->leb_size) {
399 done_ltab = 1;
400 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
401 offs += c->ltab_sz;
402 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
405 /* Loop for each cnode */
406 do {
407 if (cnode->level)
408 len = c->nnode_sz;
409 else
410 len = c->pnode_sz;
411 while (offs + len > c->leb_size) {
412 wlen = offs - from;
413 if (wlen) {
414 alen = ALIGN(wlen, c->min_io_size);
415 memset(buf + offs, 0xff, alen - wlen);
416 err = ubifs_leb_write(c, lnum, buf + from, from,
417 alen, UBI_SHORTTERM);
418 if (err)
419 return err;
421 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
422 err = realloc_lpt_leb(c, &lnum);
423 if (err)
424 goto no_space;
425 offs = from = 0;
426 ubifs_assert(lnum >= c->lpt_first &&
427 lnum <= c->lpt_last);
428 err = ubifs_leb_unmap(c, lnum);
429 if (err)
430 return err;
431 /* Try to place lsave and ltab nicely */
432 if (!done_lsave) {
433 done_lsave = 1;
434 ubifs_pack_lsave(c, buf + offs, c->lsave);
435 offs += c->lsave_sz;
436 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
437 continue;
439 if (!done_ltab) {
440 done_ltab = 1;
441 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
442 offs += c->ltab_sz;
443 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
444 continue;
446 break;
448 if (cnode->level)
449 ubifs_pack_nnode(c, buf + offs,
450 (struct ubifs_nnode *)cnode);
451 else
452 ubifs_pack_pnode(c, buf + offs,
453 (struct ubifs_pnode *)cnode);
455 * The reason for the barriers is the same as in case of TNC.
456 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
457 * 'dirty_cow_pnode()' are the functions for which this is
458 * important.
460 clear_bit(DIRTY_CNODE, &cnode->flags);
461 smp_mb__before_clear_bit();
462 clear_bit(COW_ZNODE, &cnode->flags);
463 smp_mb__after_clear_bit();
464 offs += len;
465 dbg_chk_lpt_sz(c, 1, len);
466 cnode = cnode->cnext;
467 } while (cnode && cnode != c->lpt_cnext);
469 /* Make sure to place LPT's save table */
470 if (!done_lsave) {
471 if (offs + c->lsave_sz > c->leb_size) {
472 wlen = offs - from;
473 alen = ALIGN(wlen, c->min_io_size);
474 memset(buf + offs, 0xff, alen - wlen);
475 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
476 UBI_SHORTTERM);
477 if (err)
478 return err;
479 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
480 err = realloc_lpt_leb(c, &lnum);
481 if (err)
482 goto no_space;
483 offs = from = 0;
484 ubifs_assert(lnum >= c->lpt_first &&
485 lnum <= c->lpt_last);
486 err = ubifs_leb_unmap(c, lnum);
487 if (err)
488 return err;
490 done_lsave = 1;
491 ubifs_pack_lsave(c, buf + offs, c->lsave);
492 offs += c->lsave_sz;
493 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
496 /* Make sure to place LPT's own lprops table */
497 if (!done_ltab) {
498 if (offs + c->ltab_sz > c->leb_size) {
499 wlen = offs - from;
500 alen = ALIGN(wlen, c->min_io_size);
501 memset(buf + offs, 0xff, alen - wlen);
502 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
503 UBI_SHORTTERM);
504 if (err)
505 return err;
506 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
507 err = realloc_lpt_leb(c, &lnum);
508 if (err)
509 goto no_space;
510 offs = from = 0;
511 ubifs_assert(lnum >= c->lpt_first &&
512 lnum <= c->lpt_last);
513 err = ubifs_leb_unmap(c, lnum);
514 if (err)
515 return err;
517 done_ltab = 1;
518 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
519 offs += c->ltab_sz;
520 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
523 /* Write remaining data in buffer */
524 wlen = offs - from;
525 alen = ALIGN(wlen, c->min_io_size);
526 memset(buf + offs, 0xff, alen - wlen);
527 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
528 if (err)
529 return err;
531 dbg_chk_lpt_sz(c, 4, alen - wlen);
532 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
533 if (err)
534 return err;
536 c->nhead_lnum = lnum;
537 c->nhead_offs = ALIGN(offs, c->min_io_size);
539 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
540 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
541 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
542 if (c->big_lpt)
543 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
545 return 0;
547 no_space:
548 ubifs_err("LPT out of space mismatch");
549 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
550 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
551 dbg_dump_lpt_info(c);
552 dbg_dump_lpt_lebs(c);
553 dump_stack();
554 return err;
558 * next_pnode_to_dirty - find next pnode to dirty.
559 * @c: UBIFS file-system description object
560 * @pnode: pnode
562 * This function returns the next pnode to dirty or %NULL if there are no more
563 * pnodes. Note that pnodes that have never been written (lnum == 0) are
564 * skipped.
566 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
567 struct ubifs_pnode *pnode)
569 struct ubifs_nnode *nnode;
570 int iip;
572 /* Try to go right */
573 nnode = pnode->parent;
574 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
575 if (nnode->nbranch[iip].lnum)
576 return ubifs_get_pnode(c, nnode, iip);
579 /* Go up while can't go right */
580 do {
581 iip = nnode->iip + 1;
582 nnode = nnode->parent;
583 if (!nnode)
584 return NULL;
585 for (; iip < UBIFS_LPT_FANOUT; iip++) {
586 if (nnode->nbranch[iip].lnum)
587 break;
589 } while (iip >= UBIFS_LPT_FANOUT);
591 /* Go right */
592 nnode = ubifs_get_nnode(c, nnode, iip);
593 if (IS_ERR(nnode))
594 return (void *)nnode;
596 /* Go down to level 1 */
597 while (nnode->level > 1) {
598 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
599 if (nnode->nbranch[iip].lnum)
600 break;
602 if (iip >= UBIFS_LPT_FANOUT) {
604 * Should not happen, but we need to keep going
605 * if it does.
607 iip = 0;
609 nnode = ubifs_get_nnode(c, nnode, iip);
610 if (IS_ERR(nnode))
611 return (void *)nnode;
614 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
615 if (nnode->nbranch[iip].lnum)
616 break;
617 if (iip >= UBIFS_LPT_FANOUT)
618 /* Should not happen, but we need to keep going if it does */
619 iip = 0;
620 return ubifs_get_pnode(c, nnode, iip);
624 * pnode_lookup - lookup a pnode in the LPT.
625 * @c: UBIFS file-system description object
626 * @i: pnode number (0 to main_lebs - 1)
628 * This function returns a pointer to the pnode on success or a negative
629 * error code on failure.
631 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
633 int err, h, iip, shft;
634 struct ubifs_nnode *nnode;
636 if (!c->nroot) {
637 err = ubifs_read_nnode(c, NULL, 0);
638 if (err)
639 return ERR_PTR(err);
641 i <<= UBIFS_LPT_FANOUT_SHIFT;
642 nnode = c->nroot;
643 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
644 for (h = 1; h < c->lpt_hght; h++) {
645 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
646 shft -= UBIFS_LPT_FANOUT_SHIFT;
647 nnode = ubifs_get_nnode(c, nnode, iip);
648 if (IS_ERR(nnode))
649 return ERR_CAST(nnode);
651 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
652 return ubifs_get_pnode(c, nnode, iip);
656 * add_pnode_dirt - add dirty space to LPT LEB properties.
657 * @c: UBIFS file-system description object
658 * @pnode: pnode for which to add dirt
660 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
662 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
663 c->pnode_sz);
667 * do_make_pnode_dirty - mark a pnode dirty.
668 * @c: UBIFS file-system description object
669 * @pnode: pnode to mark dirty
671 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
673 /* Assumes cnext list is empty i.e. not called during commit */
674 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
675 struct ubifs_nnode *nnode;
677 c->dirty_pn_cnt += 1;
678 add_pnode_dirt(c, pnode);
679 /* Mark parent and ancestors dirty too */
680 nnode = pnode->parent;
681 while (nnode) {
682 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
683 c->dirty_nn_cnt += 1;
684 ubifs_add_nnode_dirt(c, nnode);
685 nnode = nnode->parent;
686 } else
687 break;
693 * make_tree_dirty - mark the entire LEB properties tree dirty.
694 * @c: UBIFS file-system description object
696 * This function is used by the "small" LPT model to cause the entire LEB
697 * properties tree to be written. The "small" LPT model does not use LPT
698 * garbage collection because it is more efficient to write the entire tree
699 * (because it is small).
701 * This function returns %0 on success and a negative error code on failure.
703 static int make_tree_dirty(struct ubifs_info *c)
705 struct ubifs_pnode *pnode;
707 pnode = pnode_lookup(c, 0);
708 if (IS_ERR(pnode))
709 return PTR_ERR(pnode);
711 while (pnode) {
712 do_make_pnode_dirty(c, pnode);
713 pnode = next_pnode_to_dirty(c, pnode);
714 if (IS_ERR(pnode))
715 return PTR_ERR(pnode);
717 return 0;
721 * need_write_all - determine if the LPT area is running out of free space.
722 * @c: UBIFS file-system description object
724 * This function returns %1 if the LPT area is running out of free space and %0
725 * if it is not.
727 static int need_write_all(struct ubifs_info *c)
729 long long free = 0;
730 int i;
732 for (i = 0; i < c->lpt_lebs; i++) {
733 if (i + c->lpt_first == c->nhead_lnum)
734 free += c->leb_size - c->nhead_offs;
735 else if (c->ltab[i].free == c->leb_size)
736 free += c->leb_size;
737 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
738 free += c->leb_size;
740 /* Less than twice the size left */
741 if (free <= c->lpt_sz * 2)
742 return 1;
743 return 0;
747 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
748 * @c: UBIFS file-system description object
750 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
751 * free space and so may be reused as soon as the next commit is completed.
752 * This function is called during start commit to mark LPT LEBs for trivial GC.
754 static void lpt_tgc_start(struct ubifs_info *c)
756 int i;
758 for (i = 0; i < c->lpt_lebs; i++) {
759 if (i + c->lpt_first == c->nhead_lnum)
760 continue;
761 if (c->ltab[i].dirty > 0 &&
762 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
763 c->ltab[i].tgc = 1;
764 c->ltab[i].free = c->leb_size;
765 c->ltab[i].dirty = 0;
766 dbg_lp("LEB %d", i + c->lpt_first);
772 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
773 * @c: UBIFS file-system description object
775 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
776 * free space and so may be reused as soon as the next commit is completed.
777 * This function is called after the commit is completed (master node has been
778 * written) and un-maps LPT LEBs that were marked for trivial GC.
780 static int lpt_tgc_end(struct ubifs_info *c)
782 int i, err;
784 for (i = 0; i < c->lpt_lebs; i++)
785 if (c->ltab[i].tgc) {
786 err = ubifs_leb_unmap(c, i + c->lpt_first);
787 if (err)
788 return err;
789 c->ltab[i].tgc = 0;
790 dbg_lp("LEB %d", i + c->lpt_first);
792 return 0;
796 * populate_lsave - fill the lsave array with important LEB numbers.
797 * @c: the UBIFS file-system description object
799 * This function is only called for the "big" model. It records a small number
800 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
801 * most important to least important): empty, freeable, freeable index, dirty
802 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
803 * their pnodes into memory. That will stop us from having to scan the LPT
804 * straight away. For the "small" model we assume that scanning the LPT is no
805 * big deal.
807 static void populate_lsave(struct ubifs_info *c)
809 struct ubifs_lprops *lprops;
810 struct ubifs_lpt_heap *heap;
811 int i, cnt = 0;
813 ubifs_assert(c->big_lpt);
814 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
815 c->lpt_drty_flgs |= LSAVE_DIRTY;
816 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
818 list_for_each_entry(lprops, &c->empty_list, list) {
819 c->lsave[cnt++] = lprops->lnum;
820 if (cnt >= c->lsave_cnt)
821 return;
823 list_for_each_entry(lprops, &c->freeable_list, list) {
824 c->lsave[cnt++] = lprops->lnum;
825 if (cnt >= c->lsave_cnt)
826 return;
828 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
829 c->lsave[cnt++] = lprops->lnum;
830 if (cnt >= c->lsave_cnt)
831 return;
833 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
834 for (i = 0; i < heap->cnt; i++) {
835 c->lsave[cnt++] = heap->arr[i]->lnum;
836 if (cnt >= c->lsave_cnt)
837 return;
839 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
840 for (i = 0; i < heap->cnt; i++) {
841 c->lsave[cnt++] = heap->arr[i]->lnum;
842 if (cnt >= c->lsave_cnt)
843 return;
845 heap = &c->lpt_heap[LPROPS_FREE - 1];
846 for (i = 0; i < heap->cnt; i++) {
847 c->lsave[cnt++] = heap->arr[i]->lnum;
848 if (cnt >= c->lsave_cnt)
849 return;
851 /* Fill it up completely */
852 while (cnt < c->lsave_cnt)
853 c->lsave[cnt++] = c->main_first;
857 * nnode_lookup - lookup a nnode in the LPT.
858 * @c: UBIFS file-system description object
859 * @i: nnode number
861 * This function returns a pointer to the nnode on success or a negative
862 * error code on failure.
864 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
866 int err, iip;
867 struct ubifs_nnode *nnode;
869 if (!c->nroot) {
870 err = ubifs_read_nnode(c, NULL, 0);
871 if (err)
872 return ERR_PTR(err);
874 nnode = c->nroot;
875 while (1) {
876 iip = i & (UBIFS_LPT_FANOUT - 1);
877 i >>= UBIFS_LPT_FANOUT_SHIFT;
878 if (!i)
879 break;
880 nnode = ubifs_get_nnode(c, nnode, iip);
881 if (IS_ERR(nnode))
882 return nnode;
884 return nnode;
888 * make_nnode_dirty - find a nnode and, if found, make it dirty.
889 * @c: UBIFS file-system description object
890 * @node_num: nnode number of nnode to make dirty
891 * @lnum: LEB number where nnode was written
892 * @offs: offset where nnode was written
894 * This function is used by LPT garbage collection. LPT garbage collection is
895 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
896 * simply involves marking all the nodes in the LEB being garbage-collected as
897 * dirty. The dirty nodes are written next commit, after which the LEB is free
898 * to be reused.
900 * This function returns %0 on success and a negative error code on failure.
902 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
903 int offs)
905 struct ubifs_nnode *nnode;
907 nnode = nnode_lookup(c, node_num);
908 if (IS_ERR(nnode))
909 return PTR_ERR(nnode);
910 if (nnode->parent) {
911 struct ubifs_nbranch *branch;
913 branch = &nnode->parent->nbranch[nnode->iip];
914 if (branch->lnum != lnum || branch->offs != offs)
915 return 0; /* nnode is obsolete */
916 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
917 return 0; /* nnode is obsolete */
918 /* Assumes cnext list is empty i.e. not called during commit */
919 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
920 c->dirty_nn_cnt += 1;
921 ubifs_add_nnode_dirt(c, nnode);
922 /* Mark parent and ancestors dirty too */
923 nnode = nnode->parent;
924 while (nnode) {
925 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
926 c->dirty_nn_cnt += 1;
927 ubifs_add_nnode_dirt(c, nnode);
928 nnode = nnode->parent;
929 } else
930 break;
933 return 0;
937 * make_pnode_dirty - find a pnode and, if found, make it dirty.
938 * @c: UBIFS file-system description object
939 * @node_num: pnode number of pnode to make dirty
940 * @lnum: LEB number where pnode was written
941 * @offs: offset where pnode was written
943 * This function is used by LPT garbage collection. LPT garbage collection is
944 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
945 * simply involves marking all the nodes in the LEB being garbage-collected as
946 * dirty. The dirty nodes are written next commit, after which the LEB is free
947 * to be reused.
949 * This function returns %0 on success and a negative error code on failure.
951 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
952 int offs)
954 struct ubifs_pnode *pnode;
955 struct ubifs_nbranch *branch;
957 pnode = pnode_lookup(c, node_num);
958 if (IS_ERR(pnode))
959 return PTR_ERR(pnode);
960 branch = &pnode->parent->nbranch[pnode->iip];
961 if (branch->lnum != lnum || branch->offs != offs)
962 return 0;
963 do_make_pnode_dirty(c, pnode);
964 return 0;
968 * make_ltab_dirty - make ltab node dirty.
969 * @c: UBIFS file-system description object
970 * @lnum: LEB number where ltab was written
971 * @offs: offset where ltab was written
973 * This function is used by LPT garbage collection. LPT garbage collection is
974 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
975 * simply involves marking all the nodes in the LEB being garbage-collected as
976 * dirty. The dirty nodes are written next commit, after which the LEB is free
977 * to be reused.
979 * This function returns %0 on success and a negative error code on failure.
981 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
983 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
984 return 0; /* This ltab node is obsolete */
985 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
986 c->lpt_drty_flgs |= LTAB_DIRTY;
987 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
989 return 0;
993 * make_lsave_dirty - make lsave node dirty.
994 * @c: UBIFS file-system description object
995 * @lnum: LEB number where lsave was written
996 * @offs: offset where lsave was written
998 * This function is used by LPT garbage collection. LPT garbage collection is
999 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1000 * simply involves marking all the nodes in the LEB being garbage-collected as
1001 * dirty. The dirty nodes are written next commit, after which the LEB is free
1002 * to be reused.
1004 * This function returns %0 on success and a negative error code on failure.
1006 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1008 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1009 return 0; /* This lsave node is obsolete */
1010 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1011 c->lpt_drty_flgs |= LSAVE_DIRTY;
1012 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1014 return 0;
1018 * make_node_dirty - make node dirty.
1019 * @c: UBIFS file-system description object
1020 * @node_type: LPT node type
1021 * @node_num: node number
1022 * @lnum: LEB number where node was written
1023 * @offs: offset where node was written
1025 * This function is used by LPT garbage collection. LPT garbage collection is
1026 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1027 * simply involves marking all the nodes in the LEB being garbage-collected as
1028 * dirty. The dirty nodes are written next commit, after which the LEB is free
1029 * to be reused.
1031 * This function returns %0 on success and a negative error code on failure.
1033 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1034 int lnum, int offs)
1036 switch (node_type) {
1037 case UBIFS_LPT_NNODE:
1038 return make_nnode_dirty(c, node_num, lnum, offs);
1039 case UBIFS_LPT_PNODE:
1040 return make_pnode_dirty(c, node_num, lnum, offs);
1041 case UBIFS_LPT_LTAB:
1042 return make_ltab_dirty(c, lnum, offs);
1043 case UBIFS_LPT_LSAVE:
1044 return make_lsave_dirty(c, lnum, offs);
1046 return -EINVAL;
1050 * get_lpt_node_len - return the length of a node based on its type.
1051 * @c: UBIFS file-system description object
1052 * @node_type: LPT node type
1054 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1056 switch (node_type) {
1057 case UBIFS_LPT_NNODE:
1058 return c->nnode_sz;
1059 case UBIFS_LPT_PNODE:
1060 return c->pnode_sz;
1061 case UBIFS_LPT_LTAB:
1062 return c->ltab_sz;
1063 case UBIFS_LPT_LSAVE:
1064 return c->lsave_sz;
1066 return 0;
1070 * get_pad_len - return the length of padding in a buffer.
1071 * @c: UBIFS file-system description object
1072 * @buf: buffer
1073 * @len: length of buffer
1075 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1077 int offs, pad_len;
1079 if (c->min_io_size == 1)
1080 return 0;
1081 offs = c->leb_size - len;
1082 pad_len = ALIGN(offs, c->min_io_size) - offs;
1083 return pad_len;
1087 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1088 * @c: UBIFS file-system description object
1089 * @buf: buffer
1090 * @node_num: node number is returned here
1092 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1093 int *node_num)
1095 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1096 int pos = 0, node_type;
1098 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1099 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1100 return node_type;
1104 * is_a_node - determine if a buffer contains a node.
1105 * @c: UBIFS file-system description object
1106 * @buf: buffer
1107 * @len: length of buffer
1109 * This function returns %1 if the buffer contains a node or %0 if it does not.
1111 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1113 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1114 int pos = 0, node_type, node_len;
1115 uint16_t crc, calc_crc;
1117 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1118 return 0;
1119 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1120 if (node_type == UBIFS_LPT_NOT_A_NODE)
1121 return 0;
1122 node_len = get_lpt_node_len(c, node_type);
1123 if (!node_len || node_len > len)
1124 return 0;
1125 pos = 0;
1126 addr = buf;
1127 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1128 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1129 node_len - UBIFS_LPT_CRC_BYTES);
1130 if (crc != calc_crc)
1131 return 0;
1132 return 1;
1136 * lpt_gc_lnum - garbage collect a LPT LEB.
1137 * @c: UBIFS file-system description object
1138 * @lnum: LEB number to garbage collect
1140 * LPT garbage collection is used only for the "big" LPT model
1141 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1142 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1143 * next commit, after which the LEB is free to be reused.
1145 * This function returns %0 on success and a negative error code on failure.
1147 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1149 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1150 void *buf = c->lpt_buf;
1152 dbg_lp("LEB %d", lnum);
1153 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1154 if (err) {
1155 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1156 return err;
1158 while (1) {
1159 if (!is_a_node(c, buf, len)) {
1160 int pad_len;
1162 pad_len = get_pad_len(c, buf, len);
1163 if (pad_len) {
1164 buf += pad_len;
1165 len -= pad_len;
1166 continue;
1168 return 0;
1170 node_type = get_lpt_node_type(c, buf, &node_num);
1171 node_len = get_lpt_node_len(c, node_type);
1172 offs = c->leb_size - len;
1173 ubifs_assert(node_len != 0);
1174 mutex_lock(&c->lp_mutex);
1175 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1176 mutex_unlock(&c->lp_mutex);
1177 if (err)
1178 return err;
1179 buf += node_len;
1180 len -= node_len;
1182 return 0;
1186 * lpt_gc - LPT garbage collection.
1187 * @c: UBIFS file-system description object
1189 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1190 * Returns %0 on success and a negative error code on failure.
1192 static int lpt_gc(struct ubifs_info *c)
1194 int i, lnum = -1, dirty = 0;
1196 mutex_lock(&c->lp_mutex);
1197 for (i = 0; i < c->lpt_lebs; i++) {
1198 ubifs_assert(!c->ltab[i].tgc);
1199 if (i + c->lpt_first == c->nhead_lnum ||
1200 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1201 continue;
1202 if (c->ltab[i].dirty > dirty) {
1203 dirty = c->ltab[i].dirty;
1204 lnum = i + c->lpt_first;
1207 mutex_unlock(&c->lp_mutex);
1208 if (lnum == -1)
1209 return -ENOSPC;
1210 return lpt_gc_lnum(c, lnum);
1214 * ubifs_lpt_start_commit - UBIFS commit starts.
1215 * @c: the UBIFS file-system description object
1217 * This function has to be called when UBIFS starts the commit operation.
1218 * This function "freezes" all currently dirty LEB properties and does not
1219 * change them anymore. Further changes are saved and tracked separately
1220 * because they are not part of this commit. This function returns zero in case
1221 * of success and a negative error code in case of failure.
1223 int ubifs_lpt_start_commit(struct ubifs_info *c)
1225 int err, cnt;
1227 dbg_lp("");
1229 mutex_lock(&c->lp_mutex);
1230 err = dbg_chk_lpt_free_spc(c);
1231 if (err)
1232 goto out;
1233 err = dbg_check_ltab(c);
1234 if (err)
1235 goto out;
1237 if (c->check_lpt_free) {
1239 * We ensure there is enough free space in
1240 * ubifs_lpt_post_commit() by marking nodes dirty. That
1241 * information is lost when we unmount, so we also need
1242 * to check free space once after mounting also.
1244 c->check_lpt_free = 0;
1245 while (need_write_all(c)) {
1246 mutex_unlock(&c->lp_mutex);
1247 err = lpt_gc(c);
1248 if (err)
1249 return err;
1250 mutex_lock(&c->lp_mutex);
1254 lpt_tgc_start(c);
1256 if (!c->dirty_pn_cnt) {
1257 dbg_cmt("no cnodes to commit");
1258 err = 0;
1259 goto out;
1262 if (!c->big_lpt && need_write_all(c)) {
1263 /* If needed, write everything */
1264 err = make_tree_dirty(c);
1265 if (err)
1266 goto out;
1267 lpt_tgc_start(c);
1270 if (c->big_lpt)
1271 populate_lsave(c);
1273 cnt = get_cnodes_to_commit(c);
1274 ubifs_assert(cnt != 0);
1276 err = layout_cnodes(c);
1277 if (err)
1278 goto out;
1280 /* Copy the LPT's own lprops for end commit to write */
1281 memcpy(c->ltab_cmt, c->ltab,
1282 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1283 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1285 out:
1286 mutex_unlock(&c->lp_mutex);
1287 return err;
1291 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1292 * @c: UBIFS file-system description object
1294 static void free_obsolete_cnodes(struct ubifs_info *c)
1296 struct ubifs_cnode *cnode, *cnext;
1298 cnext = c->lpt_cnext;
1299 if (!cnext)
1300 return;
1301 do {
1302 cnode = cnext;
1303 cnext = cnode->cnext;
1304 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1305 kfree(cnode);
1306 else
1307 cnode->cnext = NULL;
1308 } while (cnext != c->lpt_cnext);
1309 c->lpt_cnext = NULL;
1313 * ubifs_lpt_end_commit - finish the commit operation.
1314 * @c: the UBIFS file-system description object
1316 * This function has to be called when the commit operation finishes. It
1317 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1318 * the media. Returns zero in case of success and a negative error code in case
1319 * of failure.
1321 int ubifs_lpt_end_commit(struct ubifs_info *c)
1323 int err;
1325 dbg_lp("");
1327 if (!c->lpt_cnext)
1328 return 0;
1330 err = write_cnodes(c);
1331 if (err)
1332 return err;
1334 mutex_lock(&c->lp_mutex);
1335 free_obsolete_cnodes(c);
1336 mutex_unlock(&c->lp_mutex);
1338 return 0;
1342 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1343 * @c: UBIFS file-system description object
1345 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1346 * commit for the "big" LPT model.
1348 int ubifs_lpt_post_commit(struct ubifs_info *c)
1350 int err;
1352 mutex_lock(&c->lp_mutex);
1353 err = lpt_tgc_end(c);
1354 if (err)
1355 goto out;
1356 if (c->big_lpt)
1357 while (need_write_all(c)) {
1358 mutex_unlock(&c->lp_mutex);
1359 err = lpt_gc(c);
1360 if (err)
1361 return err;
1362 mutex_lock(&c->lp_mutex);
1364 out:
1365 mutex_unlock(&c->lp_mutex);
1366 return err;
1370 * first_nnode - find the first nnode in memory.
1371 * @c: UBIFS file-system description object
1372 * @hght: height of tree where nnode found is returned here
1374 * This function returns a pointer to the nnode found or %NULL if no nnode is
1375 * found. This function is a helper to 'ubifs_lpt_free()'.
1377 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1379 struct ubifs_nnode *nnode;
1380 int h, i, found;
1382 nnode = c->nroot;
1383 *hght = 0;
1384 if (!nnode)
1385 return NULL;
1386 for (h = 1; h < c->lpt_hght; h++) {
1387 found = 0;
1388 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1389 if (nnode->nbranch[i].nnode) {
1390 found = 1;
1391 nnode = nnode->nbranch[i].nnode;
1392 *hght = h;
1393 break;
1396 if (!found)
1397 break;
1399 return nnode;
1403 * next_nnode - find the next nnode in memory.
1404 * @c: UBIFS file-system description object
1405 * @nnode: nnode from which to start.
1406 * @hght: height of tree where nnode is, is passed and returned here
1408 * This function returns a pointer to the nnode found or %NULL if no nnode is
1409 * found. This function is a helper to 'ubifs_lpt_free()'.
1411 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1412 struct ubifs_nnode *nnode, int *hght)
1414 struct ubifs_nnode *parent;
1415 int iip, h, i, found;
1417 parent = nnode->parent;
1418 if (!parent)
1419 return NULL;
1420 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1421 *hght -= 1;
1422 return parent;
1424 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1425 nnode = parent->nbranch[iip].nnode;
1426 if (nnode)
1427 break;
1429 if (!nnode) {
1430 *hght -= 1;
1431 return parent;
1433 for (h = *hght + 1; h < c->lpt_hght; h++) {
1434 found = 0;
1435 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1436 if (nnode->nbranch[i].nnode) {
1437 found = 1;
1438 nnode = nnode->nbranch[i].nnode;
1439 *hght = h;
1440 break;
1443 if (!found)
1444 break;
1446 return nnode;
1450 * ubifs_lpt_free - free resources owned by the LPT.
1451 * @c: UBIFS file-system description object
1452 * @wr_only: free only resources used for writing
1454 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1456 struct ubifs_nnode *nnode;
1457 int i, hght;
1459 /* Free write-only things first */
1461 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1463 vfree(c->ltab_cmt);
1464 c->ltab_cmt = NULL;
1465 vfree(c->lpt_buf);
1466 c->lpt_buf = NULL;
1467 kfree(c->lsave);
1468 c->lsave = NULL;
1470 if (wr_only)
1471 return;
1473 /* Now free the rest */
1475 nnode = first_nnode(c, &hght);
1476 while (nnode) {
1477 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1478 kfree(nnode->nbranch[i].nnode);
1479 nnode = next_nnode(c, nnode, &hght);
1481 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1482 kfree(c->lpt_heap[i].arr);
1483 kfree(c->dirty_idx.arr);
1484 kfree(c->nroot);
1485 vfree(c->ltab);
1486 kfree(c->lpt_nod_buf);
1489 #ifdef CONFIG_UBIFS_FS_DEBUG
1492 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1493 * @buf: buffer
1494 * @len: buffer length
1496 static int dbg_is_all_ff(uint8_t *buf, int len)
1498 int i;
1500 for (i = 0; i < len; i++)
1501 if (buf[i] != 0xff)
1502 return 0;
1503 return 1;
1507 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1508 * @c: the UBIFS file-system description object
1509 * @lnum: LEB number where nnode was written
1510 * @offs: offset where nnode was written
1512 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1514 struct ubifs_nnode *nnode;
1515 int hght;
1517 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1518 nnode = first_nnode(c, &hght);
1519 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1520 struct ubifs_nbranch *branch;
1522 cond_resched();
1523 if (nnode->parent) {
1524 branch = &nnode->parent->nbranch[nnode->iip];
1525 if (branch->lnum != lnum || branch->offs != offs)
1526 continue;
1527 if (test_bit(DIRTY_CNODE, &nnode->flags))
1528 return 1;
1529 return 0;
1530 } else {
1531 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1532 continue;
1533 if (test_bit(DIRTY_CNODE, &nnode->flags))
1534 return 1;
1535 return 0;
1538 return 1;
1542 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1543 * @c: the UBIFS file-system description object
1544 * @lnum: LEB number where pnode was written
1545 * @offs: offset where pnode was written
1547 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1549 int i, cnt;
1551 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1552 for (i = 0; i < cnt; i++) {
1553 struct ubifs_pnode *pnode;
1554 struct ubifs_nbranch *branch;
1556 cond_resched();
1557 pnode = pnode_lookup(c, i);
1558 if (IS_ERR(pnode))
1559 return PTR_ERR(pnode);
1560 branch = &pnode->parent->nbranch[pnode->iip];
1561 if (branch->lnum != lnum || branch->offs != offs)
1562 continue;
1563 if (test_bit(DIRTY_CNODE, &pnode->flags))
1564 return 1;
1565 return 0;
1567 return 1;
1571 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1572 * @c: the UBIFS file-system description object
1573 * @lnum: LEB number where ltab node was written
1574 * @offs: offset where ltab node was written
1576 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1578 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1579 return 1;
1580 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1584 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1585 * @c: the UBIFS file-system description object
1586 * @lnum: LEB number where lsave node was written
1587 * @offs: offset where lsave node was written
1589 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1591 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1592 return 1;
1593 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1597 * dbg_is_node_dirty - determine if a node is dirty.
1598 * @c: the UBIFS file-system description object
1599 * @node_type: node type
1600 * @lnum: LEB number where node was written
1601 * @offs: offset where node was written
1603 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1604 int offs)
1606 switch (node_type) {
1607 case UBIFS_LPT_NNODE:
1608 return dbg_is_nnode_dirty(c, lnum, offs);
1609 case UBIFS_LPT_PNODE:
1610 return dbg_is_pnode_dirty(c, lnum, offs);
1611 case UBIFS_LPT_LTAB:
1612 return dbg_is_ltab_dirty(c, lnum, offs);
1613 case UBIFS_LPT_LSAVE:
1614 return dbg_is_lsave_dirty(c, lnum, offs);
1616 return 1;
1620 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1621 * @c: the UBIFS file-system description object
1622 * @lnum: LEB number where node was written
1623 * @offs: offset where node was written
1625 * This function returns %0 on success and a negative error code on failure.
1627 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1629 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1630 int ret;
1631 void *buf, *p;
1633 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1634 return 0;
1636 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1637 if (!buf) {
1638 ubifs_err("cannot allocate memory for ltab checking");
1639 return 0;
1642 dbg_lp("LEB %d", lnum);
1643 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1644 if (err) {
1645 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1646 goto out;
1648 while (1) {
1649 if (!is_a_node(c, p, len)) {
1650 int i, pad_len;
1652 pad_len = get_pad_len(c, p, len);
1653 if (pad_len) {
1654 p += pad_len;
1655 len -= pad_len;
1656 dirty += pad_len;
1657 continue;
1659 if (!dbg_is_all_ff(p, len)) {
1660 dbg_msg("invalid empty space in LEB %d at %d",
1661 lnum, c->leb_size - len);
1662 err = -EINVAL;
1664 i = lnum - c->lpt_first;
1665 if (len != c->ltab[i].free) {
1666 dbg_msg("invalid free space in LEB %d "
1667 "(free %d, expected %d)",
1668 lnum, len, c->ltab[i].free);
1669 err = -EINVAL;
1671 if (dirty != c->ltab[i].dirty) {
1672 dbg_msg("invalid dirty space in LEB %d "
1673 "(dirty %d, expected %d)",
1674 lnum, dirty, c->ltab[i].dirty);
1675 err = -EINVAL;
1677 goto out;
1679 node_type = get_lpt_node_type(c, p, &node_num);
1680 node_len = get_lpt_node_len(c, node_type);
1681 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1682 if (ret == 1)
1683 dirty += node_len;
1684 p += node_len;
1685 len -= node_len;
1688 err = 0;
1689 out:
1690 vfree(buf);
1691 return err;
1695 * dbg_check_ltab - check the free and dirty space in the ltab.
1696 * @c: the UBIFS file-system description object
1698 * This function returns %0 on success and a negative error code on failure.
1700 int dbg_check_ltab(struct ubifs_info *c)
1702 int lnum, err, i, cnt;
1704 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1705 return 0;
1707 /* Bring the entire tree into memory */
1708 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1709 for (i = 0; i < cnt; i++) {
1710 struct ubifs_pnode *pnode;
1712 pnode = pnode_lookup(c, i);
1713 if (IS_ERR(pnode))
1714 return PTR_ERR(pnode);
1715 cond_resched();
1718 /* Check nodes */
1719 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1720 if (err)
1721 return err;
1723 /* Check each LEB */
1724 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1725 err = dbg_check_ltab_lnum(c, lnum);
1726 if (err) {
1727 dbg_err("failed at LEB %d", lnum);
1728 return err;
1732 dbg_lp("succeeded");
1733 return 0;
1737 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1738 * @c: the UBIFS file-system description object
1740 * This function returns %0 on success and a negative error code on failure.
1742 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1744 long long free = 0;
1745 int i;
1747 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1748 return 0;
1750 for (i = 0; i < c->lpt_lebs; i++) {
1751 if (c->ltab[i].tgc || c->ltab[i].cmt)
1752 continue;
1753 if (i + c->lpt_first == c->nhead_lnum)
1754 free += c->leb_size - c->nhead_offs;
1755 else if (c->ltab[i].free == c->leb_size)
1756 free += c->leb_size;
1758 if (free < c->lpt_sz) {
1759 dbg_err("LPT space error: free %lld lpt_sz %lld",
1760 free, c->lpt_sz);
1761 dbg_dump_lpt_info(c);
1762 dbg_dump_lpt_lebs(c);
1763 dump_stack();
1764 return -EINVAL;
1766 return 0;
1770 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1771 * @c: the UBIFS file-system description object
1772 * @action: what to do
1773 * @len: length written
1775 * This function returns %0 on success and a negative error code on failure.
1776 * The @action argument may be one of:
1777 * o %0 - LPT debugging checking starts, initialize debugging variables;
1778 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1779 * o %2 - switched to a different LEB and wasted @len bytes;
1780 * o %3 - check that we've written the right number of bytes.
1781 * o %4 - wasted @len bytes;
1783 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1785 struct ubifs_debug_info *d = c->dbg;
1786 long long chk_lpt_sz, lpt_sz;
1787 int err = 0;
1789 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1790 return 0;
1792 switch (action) {
1793 case 0:
1794 d->chk_lpt_sz = 0;
1795 d->chk_lpt_sz2 = 0;
1796 d->chk_lpt_lebs = 0;
1797 d->chk_lpt_wastage = 0;
1798 if (c->dirty_pn_cnt > c->pnode_cnt) {
1799 dbg_err("dirty pnodes %d exceed max %d",
1800 c->dirty_pn_cnt, c->pnode_cnt);
1801 err = -EINVAL;
1803 if (c->dirty_nn_cnt > c->nnode_cnt) {
1804 dbg_err("dirty nnodes %d exceed max %d",
1805 c->dirty_nn_cnt, c->nnode_cnt);
1806 err = -EINVAL;
1808 return err;
1809 case 1:
1810 d->chk_lpt_sz += len;
1811 return 0;
1812 case 2:
1813 d->chk_lpt_sz += len;
1814 d->chk_lpt_wastage += len;
1815 d->chk_lpt_lebs += 1;
1816 return 0;
1817 case 3:
1818 chk_lpt_sz = c->leb_size;
1819 chk_lpt_sz *= d->chk_lpt_lebs;
1820 chk_lpt_sz += len - c->nhead_offs;
1821 if (d->chk_lpt_sz != chk_lpt_sz) {
1822 dbg_err("LPT wrote %lld but space used was %lld",
1823 d->chk_lpt_sz, chk_lpt_sz);
1824 err = -EINVAL;
1826 if (d->chk_lpt_sz > c->lpt_sz) {
1827 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1828 d->chk_lpt_sz, c->lpt_sz);
1829 err = -EINVAL;
1831 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1832 dbg_err("LPT layout size %lld but wrote %lld",
1833 d->chk_lpt_sz, d->chk_lpt_sz2);
1834 err = -EINVAL;
1836 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1837 dbg_err("LPT new nhead offs: expected %d was %d",
1838 d->new_nhead_offs, len);
1839 err = -EINVAL;
1841 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1842 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1843 lpt_sz += c->ltab_sz;
1844 if (c->big_lpt)
1845 lpt_sz += c->lsave_sz;
1846 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1847 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1848 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1849 err = -EINVAL;
1851 if (err) {
1852 dbg_dump_lpt_info(c);
1853 dbg_dump_lpt_lebs(c);
1854 dump_stack();
1856 d->chk_lpt_sz2 = d->chk_lpt_sz;
1857 d->chk_lpt_sz = 0;
1858 d->chk_lpt_wastage = 0;
1859 d->chk_lpt_lebs = 0;
1860 d->new_nhead_offs = len;
1861 return err;
1862 case 4:
1863 d->chk_lpt_sz += len;
1864 d->chk_lpt_wastage += len;
1865 return 0;
1866 default:
1867 return -EINVAL;
1872 * dbg_dump_lpt_leb - dump an LPT LEB.
1873 * @c: UBIFS file-system description object
1874 * @lnum: LEB number to dump
1876 * This function dumps an LEB from LPT area. Nodes in this area are very
1877 * different to nodes in the main area (e.g., they do not have common headers,
1878 * they do not have 8-byte alignments, etc), so we have a separate function to
1879 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1881 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1883 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1884 void *buf, *p;
1886 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1887 current->pid, lnum);
1888 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1889 if (!buf) {
1890 ubifs_err("cannot allocate memory to dump LPT");
1891 return;
1894 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1895 if (err) {
1896 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1897 goto out;
1899 while (1) {
1900 offs = c->leb_size - len;
1901 if (!is_a_node(c, p, len)) {
1902 int pad_len;
1904 pad_len = get_pad_len(c, p, len);
1905 if (pad_len) {
1906 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1907 lnum, offs, pad_len);
1908 p += pad_len;
1909 len -= pad_len;
1910 continue;
1912 if (len)
1913 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1914 lnum, offs, len);
1915 break;
1918 node_type = get_lpt_node_type(c, p, &node_num);
1919 switch (node_type) {
1920 case UBIFS_LPT_PNODE:
1922 node_len = c->pnode_sz;
1923 if (c->big_lpt)
1924 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1925 lnum, offs, node_num);
1926 else
1927 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1928 lnum, offs);
1929 break;
1931 case UBIFS_LPT_NNODE:
1933 int i;
1934 struct ubifs_nnode nnode;
1936 node_len = c->nnode_sz;
1937 if (c->big_lpt)
1938 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1939 lnum, offs, node_num);
1940 else
1941 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1942 lnum, offs);
1943 err = ubifs_unpack_nnode(c, p, &nnode);
1944 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1945 printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
1946 nnode.nbranch[i].offs);
1947 if (i != UBIFS_LPT_FANOUT - 1)
1948 printk(KERN_CONT ", ");
1950 printk(KERN_CONT "\n");
1951 break;
1953 case UBIFS_LPT_LTAB:
1954 node_len = c->ltab_sz;
1955 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1956 lnum, offs);
1957 break;
1958 case UBIFS_LPT_LSAVE:
1959 node_len = c->lsave_sz;
1960 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1961 break;
1962 default:
1963 ubifs_err("LPT node type %d not recognized", node_type);
1964 goto out;
1967 p += node_len;
1968 len -= node_len;
1971 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1972 current->pid, lnum);
1973 out:
1974 vfree(buf);
1975 return;
1979 * dbg_dump_lpt_lebs - dump LPT lebs.
1980 * @c: UBIFS file-system description object
1982 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1983 * locked.
1985 void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1987 int i;
1989 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
1990 current->pid);
1991 for (i = 0; i < c->lpt_lebs; i++)
1992 dump_lpt_leb(c, i + c->lpt_first);
1993 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
1994 current->pid);
1997 #endif /* CONFIG_UBIFS_FS_DEBUG */