1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
29eefe2a2SStefan Roese /*
39eefe2a2SStefan Roese * This file is part of UBIFS.
49eefe2a2SStefan Roese *
59eefe2a2SStefan Roese * Copyright (C) 2006-2008 Nokia Corporation.
69eefe2a2SStefan Roese *
79eefe2a2SStefan Roese * Authors: Adrian Hunter
89eefe2a2SStefan Roese * Artem Bityutskiy (Битюцкий Артём)
99eefe2a2SStefan Roese */
109eefe2a2SStefan Roese
119eefe2a2SStefan Roese /*
129eefe2a2SStefan Roese * This file contains journal replay code. It runs when the file-system is being
139eefe2a2SStefan Roese * mounted and requires no locking.
149eefe2a2SStefan Roese *
159eefe2a2SStefan Roese * The larger is the journal, the longer it takes to scan it, so the longer it
169eefe2a2SStefan Roese * takes to mount UBIFS. This is why the journal has limited size which may be
179eefe2a2SStefan Roese * changed depending on the system requirements. But a larger journal gives
189eefe2a2SStefan Roese * faster I/O speed because it writes the index less frequently. So this is a
199eefe2a2SStefan Roese * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
209eefe2a2SStefan Roese * larger is the journal, the more memory its index may consume.
219eefe2a2SStefan Roese */
229eefe2a2SStefan Roese
23ff94bc40SHeiko Schocher #ifdef __UBOOT__
24ff94bc40SHeiko Schocher #include <linux/compat.h>
25ff94bc40SHeiko Schocher #include <linux/err.h>
26ff94bc40SHeiko Schocher #endif
279eefe2a2SStefan Roese #include "ubifs.h"
2884b8bf6dSMasahiro Yamada #include <linux/bug.h>
29ff94bc40SHeiko Schocher #include <linux/list_sort.h>
309eefe2a2SStefan Roese
319eefe2a2SStefan Roese /**
32ff94bc40SHeiko Schocher * struct replay_entry - replay list entry.
339eefe2a2SStefan Roese * @lnum: logical eraseblock number of the node
349eefe2a2SStefan Roese * @offs: node offset
359eefe2a2SStefan Roese * @len: node length
36ff94bc40SHeiko Schocher * @deletion: non-zero if this entry corresponds to a node deletion
379eefe2a2SStefan Roese * @sqnum: node sequence number
38ff94bc40SHeiko Schocher * @list: links the replay list
399eefe2a2SStefan Roese * @key: node key
409eefe2a2SStefan Roese * @nm: directory entry name
419eefe2a2SStefan Roese * @old_size: truncation old size
429eefe2a2SStefan Roese * @new_size: truncation new size
439eefe2a2SStefan Roese *
44ff94bc40SHeiko Schocher * The replay process first scans all buds and builds the replay list, then
45ff94bc40SHeiko Schocher * sorts the replay list in nodes sequence number order, and then inserts all
46ff94bc40SHeiko Schocher * the replay entries to the TNC.
479eefe2a2SStefan Roese */
489eefe2a2SStefan Roese struct replay_entry {
499eefe2a2SStefan Roese int lnum;
509eefe2a2SStefan Roese int offs;
519eefe2a2SStefan Roese int len;
52ff94bc40SHeiko Schocher unsigned int deletion:1;
539eefe2a2SStefan Roese unsigned long long sqnum;
54ff94bc40SHeiko Schocher struct list_head list;
559eefe2a2SStefan Roese union ubifs_key key;
569eefe2a2SStefan Roese union {
579eefe2a2SStefan Roese struct qstr nm;
589eefe2a2SStefan Roese struct {
599eefe2a2SStefan Roese loff_t old_size;
609eefe2a2SStefan Roese loff_t new_size;
619eefe2a2SStefan Roese };
629eefe2a2SStefan Roese };
639eefe2a2SStefan Roese };
649eefe2a2SStefan Roese
659eefe2a2SStefan Roese /**
669eefe2a2SStefan Roese * struct bud_entry - entry in the list of buds to replay.
679eefe2a2SStefan Roese * @list: next bud in the list
689eefe2a2SStefan Roese * @bud: bud description object
699eefe2a2SStefan Roese * @sqnum: reference node sequence number
70ff94bc40SHeiko Schocher * @free: free bytes in the bud
71ff94bc40SHeiko Schocher * @dirty: dirty bytes in the bud
729eefe2a2SStefan Roese */
739eefe2a2SStefan Roese struct bud_entry {
749eefe2a2SStefan Roese struct list_head list;
759eefe2a2SStefan Roese struct ubifs_bud *bud;
769eefe2a2SStefan Roese unsigned long long sqnum;
77ff94bc40SHeiko Schocher int free;
78ff94bc40SHeiko Schocher int dirty;
799eefe2a2SStefan Roese };
809eefe2a2SStefan Roese
819eefe2a2SStefan Roese /**
829eefe2a2SStefan Roese * set_bud_lprops - set free and dirty space used by a bud.
839eefe2a2SStefan Roese * @c: UBIFS file-system description object
84ff94bc40SHeiko Schocher * @b: bud entry which describes the bud
85ff94bc40SHeiko Schocher *
86ff94bc40SHeiko Schocher * This function makes sure the LEB properties of bud @b are set correctly
87ff94bc40SHeiko Schocher * after the replay. Returns zero in case of success and a negative error code
88ff94bc40SHeiko Schocher * in case of failure.
899eefe2a2SStefan Roese */
set_bud_lprops(struct ubifs_info * c,struct bud_entry * b)90ff94bc40SHeiko Schocher static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
919eefe2a2SStefan Roese {
929eefe2a2SStefan Roese const struct ubifs_lprops *lp;
939eefe2a2SStefan Roese int err = 0, dirty;
949eefe2a2SStefan Roese
959eefe2a2SStefan Roese ubifs_get_lprops(c);
969eefe2a2SStefan Roese
97ff94bc40SHeiko Schocher lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
989eefe2a2SStefan Roese if (IS_ERR(lp)) {
999eefe2a2SStefan Roese err = PTR_ERR(lp);
1009eefe2a2SStefan Roese goto out;
1019eefe2a2SStefan Roese }
1029eefe2a2SStefan Roese
1039eefe2a2SStefan Roese dirty = lp->dirty;
104ff94bc40SHeiko Schocher if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
1059eefe2a2SStefan Roese /*
1069eefe2a2SStefan Roese * The LEB was added to the journal with a starting offset of
1079eefe2a2SStefan Roese * zero which means the LEB must have been empty. The LEB
108ff94bc40SHeiko Schocher * property values should be @lp->free == @c->leb_size and
109ff94bc40SHeiko Schocher * @lp->dirty == 0, but that is not the case. The reason is that
110ff94bc40SHeiko Schocher * the LEB had been garbage collected before it became the bud,
111ff94bc40SHeiko Schocher * and there was not commit inbetween. The garbage collector
112ff94bc40SHeiko Schocher * resets the free and dirty space without recording it
113ff94bc40SHeiko Schocher * anywhere except lprops, so if there was no commit then
114ff94bc40SHeiko Schocher * lprops does not have that information.
1159eefe2a2SStefan Roese *
1169eefe2a2SStefan Roese * We do not need to adjust free space because the scan has told
1179eefe2a2SStefan Roese * us the exact value which is recorded in the replay entry as
118ff94bc40SHeiko Schocher * @b->free.
1199eefe2a2SStefan Roese *
1209eefe2a2SStefan Roese * However we do need to subtract from the dirty space the
1219eefe2a2SStefan Roese * amount of space that the garbage collector reclaimed, which
1229eefe2a2SStefan Roese * is the whole LEB minus the amount of space that was free.
1239eefe2a2SStefan Roese */
124ff94bc40SHeiko Schocher dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1259eefe2a2SStefan Roese lp->free, lp->dirty);
126ff94bc40SHeiko Schocher dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1279eefe2a2SStefan Roese lp->free, lp->dirty);
1289eefe2a2SStefan Roese dirty -= c->leb_size - lp->free;
1299eefe2a2SStefan Roese /*
1309eefe2a2SStefan Roese * If the replay order was perfect the dirty space would now be
131ff94bc40SHeiko Schocher * zero. The order is not perfect because the journal heads
1329eefe2a2SStefan Roese * race with each other. This is not a problem but is does mean
1339eefe2a2SStefan Roese * that the dirty space may temporarily exceed c->leb_size
1349eefe2a2SStefan Roese * during the replay.
1359eefe2a2SStefan Roese */
1369eefe2a2SStefan Roese if (dirty != 0)
137ff94bc40SHeiko Schocher dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
138ff94bc40SHeiko Schocher b->bud->lnum, lp->free, lp->dirty, b->free,
139ff94bc40SHeiko Schocher b->dirty);
1409eefe2a2SStefan Roese }
141ff94bc40SHeiko Schocher lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
1429eefe2a2SStefan Roese lp->flags | LPROPS_TAKEN, 0);
1439eefe2a2SStefan Roese if (IS_ERR(lp)) {
1449eefe2a2SStefan Roese err = PTR_ERR(lp);
1459eefe2a2SStefan Roese goto out;
1469eefe2a2SStefan Roese }
147ff94bc40SHeiko Schocher
148ff94bc40SHeiko Schocher /* Make sure the journal head points to the latest bud */
149ff94bc40SHeiko Schocher err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
150ff94bc40SHeiko Schocher b->bud->lnum, c->leb_size - b->free);
151ff94bc40SHeiko Schocher
1529eefe2a2SStefan Roese out:
1539eefe2a2SStefan Roese ubifs_release_lprops(c);
1549eefe2a2SStefan Roese return err;
1559eefe2a2SStefan Roese }
1569eefe2a2SStefan Roese
1579eefe2a2SStefan Roese /**
158ff94bc40SHeiko Schocher * set_buds_lprops - set free and dirty space for all replayed buds.
159ff94bc40SHeiko Schocher * @c: UBIFS file-system description object
160ff94bc40SHeiko Schocher *
161ff94bc40SHeiko Schocher * This function sets LEB properties for all replayed buds. Returns zero in
162ff94bc40SHeiko Schocher * case of success and a negative error code in case of failure.
163ff94bc40SHeiko Schocher */
set_buds_lprops(struct ubifs_info * c)164ff94bc40SHeiko Schocher static int set_buds_lprops(struct ubifs_info *c)
165ff94bc40SHeiko Schocher {
166ff94bc40SHeiko Schocher struct bud_entry *b;
167ff94bc40SHeiko Schocher int err;
168ff94bc40SHeiko Schocher
169ff94bc40SHeiko Schocher list_for_each_entry(b, &c->replay_buds, list) {
170ff94bc40SHeiko Schocher err = set_bud_lprops(c, b);
171ff94bc40SHeiko Schocher if (err)
172ff94bc40SHeiko Schocher return err;
173ff94bc40SHeiko Schocher }
174ff94bc40SHeiko Schocher
175ff94bc40SHeiko Schocher return 0;
176ff94bc40SHeiko Schocher }
177ff94bc40SHeiko Schocher
178ff94bc40SHeiko Schocher /**
1799eefe2a2SStefan Roese * trun_remove_range - apply a replay entry for a truncation to the TNC.
1809eefe2a2SStefan Roese * @c: UBIFS file-system description object
1819eefe2a2SStefan Roese * @r: replay entry of truncation
1829eefe2a2SStefan Roese */
trun_remove_range(struct ubifs_info * c,struct replay_entry * r)1839eefe2a2SStefan Roese static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
1849eefe2a2SStefan Roese {
1859eefe2a2SStefan Roese unsigned min_blk, max_blk;
1869eefe2a2SStefan Roese union ubifs_key min_key, max_key;
1879eefe2a2SStefan Roese ino_t ino;
1889eefe2a2SStefan Roese
1899eefe2a2SStefan Roese min_blk = r->new_size / UBIFS_BLOCK_SIZE;
1909eefe2a2SStefan Roese if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
1919eefe2a2SStefan Roese min_blk += 1;
1929eefe2a2SStefan Roese
1939eefe2a2SStefan Roese max_blk = r->old_size / UBIFS_BLOCK_SIZE;
1949eefe2a2SStefan Roese if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
1959eefe2a2SStefan Roese max_blk -= 1;
1969eefe2a2SStefan Roese
1979eefe2a2SStefan Roese ino = key_inum(c, &r->key);
1989eefe2a2SStefan Roese
1999eefe2a2SStefan Roese data_key_init(c, &min_key, ino, min_blk);
2009eefe2a2SStefan Roese data_key_init(c, &max_key, ino, max_blk);
2019eefe2a2SStefan Roese
2029eefe2a2SStefan Roese return ubifs_tnc_remove_range(c, &min_key, &max_key);
2039eefe2a2SStefan Roese }
2049eefe2a2SStefan Roese
2059eefe2a2SStefan Roese /**
2069eefe2a2SStefan Roese * apply_replay_entry - apply a replay entry to the TNC.
2079eefe2a2SStefan Roese * @c: UBIFS file-system description object
2089eefe2a2SStefan Roese * @r: replay entry to apply
2099eefe2a2SStefan Roese *
2109eefe2a2SStefan Roese * Apply a replay entry to the TNC.
2119eefe2a2SStefan Roese */
apply_replay_entry(struct ubifs_info * c,struct replay_entry * r)2129eefe2a2SStefan Roese static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
2139eefe2a2SStefan Roese {
214ff94bc40SHeiko Schocher int err;
2159eefe2a2SStefan Roese
216ff94bc40SHeiko Schocher dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
217ff94bc40SHeiko Schocher r->lnum, r->offs, r->len, r->deletion, r->sqnum);
2189eefe2a2SStefan Roese
2199eefe2a2SStefan Roese /* Set c->replay_sqnum to help deal with dangling branches. */
2209eefe2a2SStefan Roese c->replay_sqnum = r->sqnum;
2219eefe2a2SStefan Roese
222ff94bc40SHeiko Schocher if (is_hash_key(c, &r->key)) {
223ff94bc40SHeiko Schocher if (r->deletion)
2249eefe2a2SStefan Roese err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
2259eefe2a2SStefan Roese else
2269eefe2a2SStefan Roese err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
2279eefe2a2SStefan Roese r->len, &r->nm);
2289eefe2a2SStefan Roese } else {
229ff94bc40SHeiko Schocher if (r->deletion)
2309eefe2a2SStefan Roese switch (key_type(c, &r->key)) {
2319eefe2a2SStefan Roese case UBIFS_INO_KEY:
2329eefe2a2SStefan Roese {
2339eefe2a2SStefan Roese ino_t inum = key_inum(c, &r->key);
2349eefe2a2SStefan Roese
2359eefe2a2SStefan Roese err = ubifs_tnc_remove_ino(c, inum);
2369eefe2a2SStefan Roese break;
2379eefe2a2SStefan Roese }
2389eefe2a2SStefan Roese case UBIFS_TRUN_KEY:
2399eefe2a2SStefan Roese err = trun_remove_range(c, r);
2409eefe2a2SStefan Roese break;
2419eefe2a2SStefan Roese default:
2429eefe2a2SStefan Roese err = ubifs_tnc_remove(c, &r->key);
2439eefe2a2SStefan Roese break;
2449eefe2a2SStefan Roese }
2459eefe2a2SStefan Roese else
2469eefe2a2SStefan Roese err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
2479eefe2a2SStefan Roese r->len);
2489eefe2a2SStefan Roese if (err)
2499eefe2a2SStefan Roese return err;
2509eefe2a2SStefan Roese
2519eefe2a2SStefan Roese if (c->need_recovery)
252ff94bc40SHeiko Schocher err = ubifs_recover_size_accum(c, &r->key, r->deletion,
2539eefe2a2SStefan Roese r->new_size);
2549eefe2a2SStefan Roese }
2559eefe2a2SStefan Roese
2569eefe2a2SStefan Roese return err;
2579eefe2a2SStefan Roese }
2589eefe2a2SStefan Roese
2599eefe2a2SStefan Roese /**
260ff94bc40SHeiko Schocher * replay_entries_cmp - compare 2 replay entries.
261ff94bc40SHeiko Schocher * @priv: UBIFS file-system description object
262ff94bc40SHeiko Schocher * @a: first replay entry
263ff94bc40SHeiko Schocher * @a: second replay entry
2649eefe2a2SStefan Roese *
265ff94bc40SHeiko Schocher * This is a comparios function for 'list_sort()' which compares 2 replay
266ff94bc40SHeiko Schocher * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
267ff94bc40SHeiko Schocher * greater sequence number and %-1 otherwise.
2689eefe2a2SStefan Roese */
replay_entries_cmp(void * priv,struct list_head * a,struct list_head * b)269ff94bc40SHeiko Schocher static int replay_entries_cmp(void *priv, struct list_head *a,
270ff94bc40SHeiko Schocher struct list_head *b)
2719eefe2a2SStefan Roese {
272ff94bc40SHeiko Schocher struct replay_entry *ra, *rb;
2739eefe2a2SStefan Roese
274ff94bc40SHeiko Schocher cond_resched();
275ff94bc40SHeiko Schocher if (a == b)
276ff94bc40SHeiko Schocher return 0;
277ff94bc40SHeiko Schocher
278ff94bc40SHeiko Schocher ra = list_entry(a, struct replay_entry, list);
279ff94bc40SHeiko Schocher rb = list_entry(b, struct replay_entry, list);
280ff94bc40SHeiko Schocher ubifs_assert(ra->sqnum != rb->sqnum);
281ff94bc40SHeiko Schocher if (ra->sqnum > rb->sqnum)
282ff94bc40SHeiko Schocher return 1;
283ff94bc40SHeiko Schocher return -1;
2849eefe2a2SStefan Roese }
2859eefe2a2SStefan Roese
2869eefe2a2SStefan Roese /**
287ff94bc40SHeiko Schocher * apply_replay_list - apply the replay list to the TNC.
2889eefe2a2SStefan Roese * @c: UBIFS file-system description object
2899eefe2a2SStefan Roese *
290ff94bc40SHeiko Schocher * Apply all entries in the replay list to the TNC. Returns zero in case of
291ff94bc40SHeiko Schocher * success and a negative error code in case of failure.
2929eefe2a2SStefan Roese */
apply_replay_list(struct ubifs_info * c)293ff94bc40SHeiko Schocher static int apply_replay_list(struct ubifs_info *c)
2949eefe2a2SStefan Roese {
2959eefe2a2SStefan Roese struct replay_entry *r;
2969eefe2a2SStefan Roese int err;
2979eefe2a2SStefan Roese
298ff94bc40SHeiko Schocher list_sort(c, &c->replay_list, &replay_entries_cmp);
299ff94bc40SHeiko Schocher
300ff94bc40SHeiko Schocher list_for_each_entry(r, &c->replay_list, list) {
3019eefe2a2SStefan Roese cond_resched();
3029eefe2a2SStefan Roese
3039eefe2a2SStefan Roese err = apply_replay_entry(c, r);
3049eefe2a2SStefan Roese if (err)
3059eefe2a2SStefan Roese return err;
3069eefe2a2SStefan Roese }
307ff94bc40SHeiko Schocher
3089eefe2a2SStefan Roese return 0;
3099eefe2a2SStefan Roese }
3109eefe2a2SStefan Roese
3119eefe2a2SStefan Roese /**
312ff94bc40SHeiko Schocher * destroy_replay_list - destroy the replay.
313ff94bc40SHeiko Schocher * @c: UBIFS file-system description object
314ff94bc40SHeiko Schocher *
315ff94bc40SHeiko Schocher * Destroy the replay list.
316ff94bc40SHeiko Schocher */
destroy_replay_list(struct ubifs_info * c)317ff94bc40SHeiko Schocher static void destroy_replay_list(struct ubifs_info *c)
318ff94bc40SHeiko Schocher {
319ff94bc40SHeiko Schocher struct replay_entry *r, *tmp;
320ff94bc40SHeiko Schocher
321ff94bc40SHeiko Schocher list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
322ff94bc40SHeiko Schocher if (is_hash_key(c, &r->key))
323ff94bc40SHeiko Schocher kfree(r->nm.name);
324ff94bc40SHeiko Schocher list_del(&r->list);
325ff94bc40SHeiko Schocher kfree(r);
326ff94bc40SHeiko Schocher }
327ff94bc40SHeiko Schocher }
328ff94bc40SHeiko Schocher
329ff94bc40SHeiko Schocher /**
330ff94bc40SHeiko Schocher * insert_node - insert a node to the replay list
3319eefe2a2SStefan Roese * @c: UBIFS file-system description object
3329eefe2a2SStefan Roese * @lnum: node logical eraseblock number
3339eefe2a2SStefan Roese * @offs: node offset
3349eefe2a2SStefan Roese * @len: node length
3359eefe2a2SStefan Roese * @key: node key
3369eefe2a2SStefan Roese * @sqnum: sequence number
3379eefe2a2SStefan Roese * @deletion: non-zero if this is a deletion
3389eefe2a2SStefan Roese * @used: number of bytes in use in a LEB
3399eefe2a2SStefan Roese * @old_size: truncation old size
3409eefe2a2SStefan Roese * @new_size: truncation new size
3419eefe2a2SStefan Roese *
342ff94bc40SHeiko Schocher * This function inserts a scanned non-direntry node to the replay list. The
343ff94bc40SHeiko Schocher * replay list contains @struct replay_entry elements, and we sort this list in
344ff94bc40SHeiko Schocher * sequence number order before applying it. The replay list is applied at the
345ff94bc40SHeiko Schocher * very end of the replay process. Since the list is sorted in sequence number
346ff94bc40SHeiko Schocher * order, the older modifications are applied first. This function returns zero
347ff94bc40SHeiko Schocher * in case of success and a negative error code in case of failure.
3489eefe2a2SStefan Roese */
insert_node(struct ubifs_info * c,int lnum,int offs,int len,union ubifs_key * key,unsigned long long sqnum,int deletion,int * used,loff_t old_size,loff_t new_size)3499eefe2a2SStefan Roese static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
3509eefe2a2SStefan Roese union ubifs_key *key, unsigned long long sqnum,
3519eefe2a2SStefan Roese int deletion, int *used, loff_t old_size,
3529eefe2a2SStefan Roese loff_t new_size)
3539eefe2a2SStefan Roese {
3549eefe2a2SStefan Roese struct replay_entry *r;
3559eefe2a2SStefan Roese
356ff94bc40SHeiko Schocher dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
357ff94bc40SHeiko Schocher
3589eefe2a2SStefan Roese if (key_inum(c, key) >= c->highest_inum)
3599eefe2a2SStefan Roese c->highest_inum = key_inum(c, key);
3609eefe2a2SStefan Roese
3619eefe2a2SStefan Roese r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
3629eefe2a2SStefan Roese if (!r)
3639eefe2a2SStefan Roese return -ENOMEM;
3649eefe2a2SStefan Roese
3659eefe2a2SStefan Roese if (!deletion)
3669eefe2a2SStefan Roese *used += ALIGN(len, 8);
3679eefe2a2SStefan Roese r->lnum = lnum;
3689eefe2a2SStefan Roese r->offs = offs;
3699eefe2a2SStefan Roese r->len = len;
370ff94bc40SHeiko Schocher r->deletion = !!deletion;
3719eefe2a2SStefan Roese r->sqnum = sqnum;
372ff94bc40SHeiko Schocher key_copy(c, key, &r->key);
3739eefe2a2SStefan Roese r->old_size = old_size;
3749eefe2a2SStefan Roese r->new_size = new_size;
3759eefe2a2SStefan Roese
376ff94bc40SHeiko Schocher list_add_tail(&r->list, &c->replay_list);
3779eefe2a2SStefan Roese return 0;
3789eefe2a2SStefan Roese }
3799eefe2a2SStefan Roese
3809eefe2a2SStefan Roese /**
381ff94bc40SHeiko Schocher * insert_dent - insert a directory entry node into the replay list.
3829eefe2a2SStefan Roese * @c: UBIFS file-system description object
3839eefe2a2SStefan Roese * @lnum: node logical eraseblock number
3849eefe2a2SStefan Roese * @offs: node offset
3859eefe2a2SStefan Roese * @len: node length
3869eefe2a2SStefan Roese * @key: node key
3879eefe2a2SStefan Roese * @name: directory entry name
3889eefe2a2SStefan Roese * @nlen: directory entry name length
3899eefe2a2SStefan Roese * @sqnum: sequence number
3909eefe2a2SStefan Roese * @deletion: non-zero if this is a deletion
3919eefe2a2SStefan Roese * @used: number of bytes in use in a LEB
3929eefe2a2SStefan Roese *
393ff94bc40SHeiko Schocher * This function inserts a scanned directory entry node or an extended
394ff94bc40SHeiko Schocher * attribute entry to the replay list. Returns zero in case of success and a
395ff94bc40SHeiko Schocher * negative error code in case of failure.
3969eefe2a2SStefan Roese */
insert_dent(struct ubifs_info * c,int lnum,int offs,int len,union ubifs_key * key,const char * name,int nlen,unsigned long long sqnum,int deletion,int * used)3979eefe2a2SStefan Roese static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
3989eefe2a2SStefan Roese union ubifs_key *key, const char *name, int nlen,
3999eefe2a2SStefan Roese unsigned long long sqnum, int deletion, int *used)
4009eefe2a2SStefan Roese {
4019eefe2a2SStefan Roese struct replay_entry *r;
4029eefe2a2SStefan Roese char *nbuf;
4039eefe2a2SStefan Roese
404ff94bc40SHeiko Schocher dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
4059eefe2a2SStefan Roese if (key_inum(c, key) >= c->highest_inum)
4069eefe2a2SStefan Roese c->highest_inum = key_inum(c, key);
4079eefe2a2SStefan Roese
4089eefe2a2SStefan Roese r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4099eefe2a2SStefan Roese if (!r)
4109eefe2a2SStefan Roese return -ENOMEM;
411ff94bc40SHeiko Schocher
4129eefe2a2SStefan Roese nbuf = kmalloc(nlen + 1, GFP_KERNEL);
4139eefe2a2SStefan Roese if (!nbuf) {
4149eefe2a2SStefan Roese kfree(r);
4159eefe2a2SStefan Roese return -ENOMEM;
4169eefe2a2SStefan Roese }
4179eefe2a2SStefan Roese
4189eefe2a2SStefan Roese if (!deletion)
4199eefe2a2SStefan Roese *used += ALIGN(len, 8);
4209eefe2a2SStefan Roese r->lnum = lnum;
4219eefe2a2SStefan Roese r->offs = offs;
4229eefe2a2SStefan Roese r->len = len;
423ff94bc40SHeiko Schocher r->deletion = !!deletion;
4249eefe2a2SStefan Roese r->sqnum = sqnum;
425ff94bc40SHeiko Schocher key_copy(c, key, &r->key);
4269eefe2a2SStefan Roese r->nm.len = nlen;
4279eefe2a2SStefan Roese memcpy(nbuf, name, nlen);
4289eefe2a2SStefan Roese nbuf[nlen] = '\0';
4299eefe2a2SStefan Roese r->nm.name = nbuf;
4309eefe2a2SStefan Roese
431ff94bc40SHeiko Schocher list_add_tail(&r->list, &c->replay_list);
4329eefe2a2SStefan Roese return 0;
4339eefe2a2SStefan Roese }
4349eefe2a2SStefan Roese
4359eefe2a2SStefan Roese /**
4369eefe2a2SStefan Roese * ubifs_validate_entry - validate directory or extended attribute entry node.
4379eefe2a2SStefan Roese * @c: UBIFS file-system description object
4389eefe2a2SStefan Roese * @dent: the node to validate
4399eefe2a2SStefan Roese *
4409eefe2a2SStefan Roese * This function validates directory or extended attribute entry node @dent.
4419eefe2a2SStefan Roese * Returns zero if the node is all right and a %-EINVAL if not.
4429eefe2a2SStefan Roese */
ubifs_validate_entry(struct ubifs_info * c,const struct ubifs_dent_node * dent)4439eefe2a2SStefan Roese int ubifs_validate_entry(struct ubifs_info *c,
4449eefe2a2SStefan Roese const struct ubifs_dent_node *dent)
4459eefe2a2SStefan Roese {
4469eefe2a2SStefan Roese int key_type = key_type_flash(c, dent->key);
4479eefe2a2SStefan Roese int nlen = le16_to_cpu(dent->nlen);
4489eefe2a2SStefan Roese
4499eefe2a2SStefan Roese if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
4509eefe2a2SStefan Roese dent->type >= UBIFS_ITYPES_CNT ||
4519eefe2a2SStefan Roese nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
452ff94bc40SHeiko Schocher strnlen(dent->name, nlen) != nlen ||
4539eefe2a2SStefan Roese le64_to_cpu(dent->inum) > MAX_INUM) {
4540195a7bbSHeiko Schocher ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
4559eefe2a2SStefan Roese "directory entry" : "extended attribute entry");
4569eefe2a2SStefan Roese return -EINVAL;
4579eefe2a2SStefan Roese }
4589eefe2a2SStefan Roese
4599eefe2a2SStefan Roese if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
4600195a7bbSHeiko Schocher ubifs_err(c, "bad key type %d", key_type);
4619eefe2a2SStefan Roese return -EINVAL;
4629eefe2a2SStefan Roese }
4639eefe2a2SStefan Roese
4649eefe2a2SStefan Roese return 0;
4659eefe2a2SStefan Roese }
4669eefe2a2SStefan Roese
467ff94bc40SHeiko Schocher /**
468ff94bc40SHeiko Schocher * is_last_bud - check if the bud is the last in the journal head.
469ff94bc40SHeiko Schocher * @c: UBIFS file-system description object
470ff94bc40SHeiko Schocher * @bud: bud description object
471ff94bc40SHeiko Schocher *
472ff94bc40SHeiko Schocher * This function checks if bud @bud is the last bud in its journal head. This
473ff94bc40SHeiko Schocher * information is then used by 'replay_bud()' to decide whether the bud can
474ff94bc40SHeiko Schocher * have corruptions or not. Indeed, only last buds can be corrupted by power
475ff94bc40SHeiko Schocher * cuts. Returns %1 if this is the last bud, and %0 if not.
476ff94bc40SHeiko Schocher */
is_last_bud(struct ubifs_info * c,struct ubifs_bud * bud)477ff94bc40SHeiko Schocher static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
478ff94bc40SHeiko Schocher {
479ff94bc40SHeiko Schocher struct ubifs_jhead *jh = &c->jheads[bud->jhead];
480ff94bc40SHeiko Schocher struct ubifs_bud *next;
481ff94bc40SHeiko Schocher uint32_t data;
482ff94bc40SHeiko Schocher int err;
483ff94bc40SHeiko Schocher
484ff94bc40SHeiko Schocher if (list_is_last(&bud->list, &jh->buds_list))
485ff94bc40SHeiko Schocher return 1;
486ff94bc40SHeiko Schocher
487ff94bc40SHeiko Schocher /*
488ff94bc40SHeiko Schocher * The following is a quirk to make sure we work correctly with UBIFS
489ff94bc40SHeiko Schocher * images used with older UBIFS.
490ff94bc40SHeiko Schocher *
491ff94bc40SHeiko Schocher * Normally, the last bud will be the last in the journal head's list
492ff94bc40SHeiko Schocher * of bud. However, there is one exception if the UBIFS image belongs
493ff94bc40SHeiko Schocher * to older UBIFS. This is fairly unlikely: one would need to use old
494ff94bc40SHeiko Schocher * UBIFS, then have a power cut exactly at the right point, and then
495ff94bc40SHeiko Schocher * try to mount this image with new UBIFS.
496ff94bc40SHeiko Schocher *
497ff94bc40SHeiko Schocher * The exception is: it is possible to have 2 buds A and B, A goes
498ff94bc40SHeiko Schocher * before B, and B is the last, bud B is contains no data, and bud A is
499ff94bc40SHeiko Schocher * corrupted at the end. The reason is that in older versions when the
500ff94bc40SHeiko Schocher * journal code switched the next bud (from A to B), it first added a
501ff94bc40SHeiko Schocher * log reference node for the new bud (B), and only after this it
502ff94bc40SHeiko Schocher * synchronized the write-buffer of current bud (A). But later this was
503ff94bc40SHeiko Schocher * changed and UBIFS started to always synchronize the write-buffer of
504ff94bc40SHeiko Schocher * the bud (A) before writing the log reference for the new bud (B).
505ff94bc40SHeiko Schocher *
506ff94bc40SHeiko Schocher * But because older UBIFS always synchronized A's write-buffer before
507ff94bc40SHeiko Schocher * writing to B, we can recognize this exceptional situation but
508ff94bc40SHeiko Schocher * checking the contents of bud B - if it is empty, then A can be
509ff94bc40SHeiko Schocher * treated as the last and we can recover it.
510ff94bc40SHeiko Schocher *
511ff94bc40SHeiko Schocher * TODO: remove this piece of code in a couple of years (today it is
512ff94bc40SHeiko Schocher * 16.05.2011).
513ff94bc40SHeiko Schocher */
514ff94bc40SHeiko Schocher next = list_entry(bud->list.next, struct ubifs_bud, list);
515ff94bc40SHeiko Schocher if (!list_is_last(&next->list, &jh->buds_list))
516ff94bc40SHeiko Schocher return 0;
517ff94bc40SHeiko Schocher
518ff94bc40SHeiko Schocher err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
519ff94bc40SHeiko Schocher if (err)
520ff94bc40SHeiko Schocher return 0;
521ff94bc40SHeiko Schocher
522ff94bc40SHeiko Schocher return data == 0xFFFFFFFF;
523ff94bc40SHeiko Schocher }
524ff94bc40SHeiko Schocher
5259eefe2a2SStefan Roese /**
5269eefe2a2SStefan Roese * replay_bud - replay a bud logical eraseblock.
5279eefe2a2SStefan Roese * @c: UBIFS file-system description object
528ff94bc40SHeiko Schocher * @b: bud entry which describes the bud
5299eefe2a2SStefan Roese *
530ff94bc40SHeiko Schocher * This function replays bud @bud, recovers it if needed, and adds all nodes
531ff94bc40SHeiko Schocher * from this bud to the replay list. Returns zero in case of success and a
532ff94bc40SHeiko Schocher * negative error code in case of failure.
5339eefe2a2SStefan Roese */
replay_bud(struct ubifs_info * c,struct bud_entry * b)534ff94bc40SHeiko Schocher static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
5359eefe2a2SStefan Roese {
536ff94bc40SHeiko Schocher int is_last = is_last_bud(c, b->bud);
537ff94bc40SHeiko Schocher int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
5389eefe2a2SStefan Roese struct ubifs_scan_leb *sleb;
5399eefe2a2SStefan Roese struct ubifs_scan_node *snod;
5409eefe2a2SStefan Roese
541ff94bc40SHeiko Schocher dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
542ff94bc40SHeiko Schocher lnum, b->bud->jhead, offs, is_last);
543ff94bc40SHeiko Schocher
544ff94bc40SHeiko Schocher if (c->need_recovery && is_last)
545ff94bc40SHeiko Schocher /*
546ff94bc40SHeiko Schocher * Recover only last LEBs in the journal heads, because power
547ff94bc40SHeiko Schocher * cuts may cause corruptions only in these LEBs, because only
548ff94bc40SHeiko Schocher * these LEBs could possibly be written to at the power cut
549ff94bc40SHeiko Schocher * time.
550ff94bc40SHeiko Schocher */
551ff94bc40SHeiko Schocher sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
5529eefe2a2SStefan Roese else
553ff94bc40SHeiko Schocher sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
5549eefe2a2SStefan Roese if (IS_ERR(sleb))
5559eefe2a2SStefan Roese return PTR_ERR(sleb);
5569eefe2a2SStefan Roese
5579eefe2a2SStefan Roese /*
5589eefe2a2SStefan Roese * The bud does not have to start from offset zero - the beginning of
5599eefe2a2SStefan Roese * the 'lnum' LEB may contain previously committed data. One of the
5609eefe2a2SStefan Roese * things we have to do in replay is to correctly update lprops with
5619eefe2a2SStefan Roese * newer information about this LEB.
5629eefe2a2SStefan Roese *
5639eefe2a2SStefan Roese * At this point lprops thinks that this LEB has 'c->leb_size - offs'
5649eefe2a2SStefan Roese * bytes of free space because it only contain information about
5659eefe2a2SStefan Roese * committed data.
5669eefe2a2SStefan Roese *
5679eefe2a2SStefan Roese * But we know that real amount of free space is 'c->leb_size -
5689eefe2a2SStefan Roese * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
5699eefe2a2SStefan Roese * 'sleb->endpt' is used by bud data. We have to correctly calculate
5709eefe2a2SStefan Roese * how much of these data are dirty and update lprops with this
5719eefe2a2SStefan Roese * information.
5729eefe2a2SStefan Roese *
5739eefe2a2SStefan Roese * The dirt in that LEB region is comprised of padding nodes, deletion
5749eefe2a2SStefan Roese * nodes, truncation nodes and nodes which are obsoleted by subsequent
5759eefe2a2SStefan Roese * nodes in this LEB. So instead of calculating clean space, we
5769eefe2a2SStefan Roese * calculate used space ('used' variable).
5779eefe2a2SStefan Roese */
5789eefe2a2SStefan Roese
5799eefe2a2SStefan Roese list_for_each_entry(snod, &sleb->nodes, list) {
5809eefe2a2SStefan Roese int deletion = 0;
5819eefe2a2SStefan Roese
5829eefe2a2SStefan Roese cond_resched();
5839eefe2a2SStefan Roese
5849eefe2a2SStefan Roese if (snod->sqnum >= SQNUM_WATERMARK) {
5850195a7bbSHeiko Schocher ubifs_err(c, "file system's life ended");
5869eefe2a2SStefan Roese goto out_dump;
5879eefe2a2SStefan Roese }
5889eefe2a2SStefan Roese
5899eefe2a2SStefan Roese if (snod->sqnum > c->max_sqnum)
5909eefe2a2SStefan Roese c->max_sqnum = snod->sqnum;
5919eefe2a2SStefan Roese
5929eefe2a2SStefan Roese switch (snod->type) {
5939eefe2a2SStefan Roese case UBIFS_INO_NODE:
5949eefe2a2SStefan Roese {
5959eefe2a2SStefan Roese struct ubifs_ino_node *ino = snod->node;
5969eefe2a2SStefan Roese loff_t new_size = le64_to_cpu(ino->size);
5979eefe2a2SStefan Roese
5989eefe2a2SStefan Roese if (le32_to_cpu(ino->nlink) == 0)
5999eefe2a2SStefan Roese deletion = 1;
6009eefe2a2SStefan Roese err = insert_node(c, lnum, snod->offs, snod->len,
6019eefe2a2SStefan Roese &snod->key, snod->sqnum, deletion,
6029eefe2a2SStefan Roese &used, 0, new_size);
6039eefe2a2SStefan Roese break;
6049eefe2a2SStefan Roese }
6059eefe2a2SStefan Roese case UBIFS_DATA_NODE:
6069eefe2a2SStefan Roese {
6079eefe2a2SStefan Roese struct ubifs_data_node *dn = snod->node;
6089eefe2a2SStefan Roese loff_t new_size = le32_to_cpu(dn->size) +
6099eefe2a2SStefan Roese key_block(c, &snod->key) *
6109eefe2a2SStefan Roese UBIFS_BLOCK_SIZE;
6119eefe2a2SStefan Roese
6129eefe2a2SStefan Roese err = insert_node(c, lnum, snod->offs, snod->len,
6139eefe2a2SStefan Roese &snod->key, snod->sqnum, deletion,
6149eefe2a2SStefan Roese &used, 0, new_size);
6159eefe2a2SStefan Roese break;
6169eefe2a2SStefan Roese }
6179eefe2a2SStefan Roese case UBIFS_DENT_NODE:
6189eefe2a2SStefan Roese case UBIFS_XENT_NODE:
6199eefe2a2SStefan Roese {
6209eefe2a2SStefan Roese struct ubifs_dent_node *dent = snod->node;
6219eefe2a2SStefan Roese
6229eefe2a2SStefan Roese err = ubifs_validate_entry(c, dent);
6239eefe2a2SStefan Roese if (err)
6249eefe2a2SStefan Roese goto out_dump;
6259eefe2a2SStefan Roese
6269eefe2a2SStefan Roese err = insert_dent(c, lnum, snod->offs, snod->len,
627ff94bc40SHeiko Schocher &snod->key, dent->name,
6289eefe2a2SStefan Roese le16_to_cpu(dent->nlen), snod->sqnum,
6299eefe2a2SStefan Roese !le64_to_cpu(dent->inum), &used);
6309eefe2a2SStefan Roese break;
6319eefe2a2SStefan Roese }
6329eefe2a2SStefan Roese case UBIFS_TRUN_NODE:
6339eefe2a2SStefan Roese {
6349eefe2a2SStefan Roese struct ubifs_trun_node *trun = snod->node;
6359eefe2a2SStefan Roese loff_t old_size = le64_to_cpu(trun->old_size);
6369eefe2a2SStefan Roese loff_t new_size = le64_to_cpu(trun->new_size);
6379eefe2a2SStefan Roese union ubifs_key key;
6389eefe2a2SStefan Roese
6399eefe2a2SStefan Roese /* Validate truncation node */
6409eefe2a2SStefan Roese if (old_size < 0 || old_size > c->max_inode_sz ||
6419eefe2a2SStefan Roese new_size < 0 || new_size > c->max_inode_sz ||
6429eefe2a2SStefan Roese old_size <= new_size) {
6430195a7bbSHeiko Schocher ubifs_err(c, "bad truncation node");
6449eefe2a2SStefan Roese goto out_dump;
6459eefe2a2SStefan Roese }
6469eefe2a2SStefan Roese
6479eefe2a2SStefan Roese /*
6489eefe2a2SStefan Roese * Create a fake truncation key just to use the same
6499eefe2a2SStefan Roese * functions which expect nodes to have keys.
6509eefe2a2SStefan Roese */
6519eefe2a2SStefan Roese trun_key_init(c, &key, le32_to_cpu(trun->inum));
6529eefe2a2SStefan Roese err = insert_node(c, lnum, snod->offs, snod->len,
6539eefe2a2SStefan Roese &key, snod->sqnum, 1, &used,
6549eefe2a2SStefan Roese old_size, new_size);
6559eefe2a2SStefan Roese break;
6569eefe2a2SStefan Roese }
6579eefe2a2SStefan Roese default:
6580195a7bbSHeiko Schocher ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
6599eefe2a2SStefan Roese snod->type, lnum, snod->offs);
6609eefe2a2SStefan Roese err = -EINVAL;
6619eefe2a2SStefan Roese goto out_dump;
6629eefe2a2SStefan Roese }
6639eefe2a2SStefan Roese if (err)
6649eefe2a2SStefan Roese goto out;
6659eefe2a2SStefan Roese }
6669eefe2a2SStefan Roese
667ff94bc40SHeiko Schocher ubifs_assert(ubifs_search_bud(c, lnum));
6689eefe2a2SStefan Roese ubifs_assert(sleb->endpt - offs >= used);
6699eefe2a2SStefan Roese ubifs_assert(sleb->endpt % c->min_io_size == 0);
6709eefe2a2SStefan Roese
671ff94bc40SHeiko Schocher b->dirty = sleb->endpt - offs - used;
672ff94bc40SHeiko Schocher b->free = c->leb_size - sleb->endpt;
673ff94bc40SHeiko Schocher dbg_mnt("bud LEB %d replied: dirty %d, free %d",
674ff94bc40SHeiko Schocher lnum, b->dirty, b->free);
6759eefe2a2SStefan Roese
6769eefe2a2SStefan Roese out:
6779eefe2a2SStefan Roese ubifs_scan_destroy(sleb);
6789eefe2a2SStefan Roese return err;
6799eefe2a2SStefan Roese
6809eefe2a2SStefan Roese out_dump:
6810195a7bbSHeiko Schocher ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
682ff94bc40SHeiko Schocher ubifs_dump_node(c, snod->node);
6839eefe2a2SStefan Roese ubifs_scan_destroy(sleb);
6849eefe2a2SStefan Roese return -EINVAL;
6859eefe2a2SStefan Roese }
6869eefe2a2SStefan Roese
6879eefe2a2SStefan Roese /**
6889eefe2a2SStefan Roese * replay_buds - replay all buds.
6899eefe2a2SStefan Roese * @c: UBIFS file-system description object
6909eefe2a2SStefan Roese *
6919eefe2a2SStefan Roese * This function returns zero in case of success and a negative error code in
6929eefe2a2SStefan Roese * case of failure.
6939eefe2a2SStefan Roese */
replay_buds(struct ubifs_info * c)6949eefe2a2SStefan Roese static int replay_buds(struct ubifs_info *c)
6959eefe2a2SStefan Roese {
6969eefe2a2SStefan Roese struct bud_entry *b;
697ff94bc40SHeiko Schocher int err;
698ff94bc40SHeiko Schocher unsigned long long prev_sqnum = 0;
6999eefe2a2SStefan Roese
7009eefe2a2SStefan Roese list_for_each_entry(b, &c->replay_buds, list) {
701ff94bc40SHeiko Schocher err = replay_bud(c, b);
7029eefe2a2SStefan Roese if (err)
7039eefe2a2SStefan Roese return err;
704ff94bc40SHeiko Schocher
705ff94bc40SHeiko Schocher ubifs_assert(b->sqnum > prev_sqnum);
706ff94bc40SHeiko Schocher prev_sqnum = b->sqnum;
7079eefe2a2SStefan Roese }
7089eefe2a2SStefan Roese
7099eefe2a2SStefan Roese return 0;
7109eefe2a2SStefan Roese }
7119eefe2a2SStefan Roese
7129eefe2a2SStefan Roese /**
7139eefe2a2SStefan Roese * destroy_bud_list - destroy the list of buds to replay.
7149eefe2a2SStefan Roese * @c: UBIFS file-system description object
7159eefe2a2SStefan Roese */
destroy_bud_list(struct ubifs_info * c)7169eefe2a2SStefan Roese static void destroy_bud_list(struct ubifs_info *c)
7179eefe2a2SStefan Roese {
7189eefe2a2SStefan Roese struct bud_entry *b;
7199eefe2a2SStefan Roese
7209eefe2a2SStefan Roese while (!list_empty(&c->replay_buds)) {
7219eefe2a2SStefan Roese b = list_entry(c->replay_buds.next, struct bud_entry, list);
7229eefe2a2SStefan Roese list_del(&b->list);
7239eefe2a2SStefan Roese kfree(b);
7249eefe2a2SStefan Roese }
7259eefe2a2SStefan Roese }
7269eefe2a2SStefan Roese
7279eefe2a2SStefan Roese /**
7289eefe2a2SStefan Roese * add_replay_bud - add a bud to the list of buds to replay.
7299eefe2a2SStefan Roese * @c: UBIFS file-system description object
7309eefe2a2SStefan Roese * @lnum: bud logical eraseblock number to replay
7319eefe2a2SStefan Roese * @offs: bud start offset
7329eefe2a2SStefan Roese * @jhead: journal head to which this bud belongs
7339eefe2a2SStefan Roese * @sqnum: reference node sequence number
7349eefe2a2SStefan Roese *
7359eefe2a2SStefan Roese * This function returns zero in case of success and a negative error code in
7369eefe2a2SStefan Roese * case of failure.
7379eefe2a2SStefan Roese */
add_replay_bud(struct ubifs_info * c,int lnum,int offs,int jhead,unsigned long long sqnum)7389eefe2a2SStefan Roese static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
7399eefe2a2SStefan Roese unsigned long long sqnum)
7409eefe2a2SStefan Roese {
7419eefe2a2SStefan Roese struct ubifs_bud *bud;
7429eefe2a2SStefan Roese struct bud_entry *b;
7439eefe2a2SStefan Roese
7449eefe2a2SStefan Roese dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
7459eefe2a2SStefan Roese
7469eefe2a2SStefan Roese bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
7479eefe2a2SStefan Roese if (!bud)
7489eefe2a2SStefan Roese return -ENOMEM;
7499eefe2a2SStefan Roese
7509eefe2a2SStefan Roese b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
7519eefe2a2SStefan Roese if (!b) {
7529eefe2a2SStefan Roese kfree(bud);
7539eefe2a2SStefan Roese return -ENOMEM;
7549eefe2a2SStefan Roese }
7559eefe2a2SStefan Roese
7569eefe2a2SStefan Roese bud->lnum = lnum;
7579eefe2a2SStefan Roese bud->start = offs;
7589eefe2a2SStefan Roese bud->jhead = jhead;
7599eefe2a2SStefan Roese ubifs_add_bud(c, bud);
7609eefe2a2SStefan Roese
7619eefe2a2SStefan Roese b->bud = bud;
7629eefe2a2SStefan Roese b->sqnum = sqnum;
7639eefe2a2SStefan Roese list_add_tail(&b->list, &c->replay_buds);
7649eefe2a2SStefan Roese
7659eefe2a2SStefan Roese return 0;
7669eefe2a2SStefan Roese }
7679eefe2a2SStefan Roese
7689eefe2a2SStefan Roese /**
7699eefe2a2SStefan Roese * validate_ref - validate a reference node.
7709eefe2a2SStefan Roese * @c: UBIFS file-system description object
7719eefe2a2SStefan Roese * @ref: the reference node to validate
7729eefe2a2SStefan Roese * @ref_lnum: LEB number of the reference node
7739eefe2a2SStefan Roese * @ref_offs: reference node offset
7749eefe2a2SStefan Roese *
7759eefe2a2SStefan Roese * This function returns %1 if a bud reference already exists for the LEB. %0 is
7769eefe2a2SStefan Roese * returned if the reference node is new, otherwise %-EINVAL is returned if
7779eefe2a2SStefan Roese * validation failed.
7789eefe2a2SStefan Roese */
validate_ref(struct ubifs_info * c,const struct ubifs_ref_node * ref)7799eefe2a2SStefan Roese static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
7809eefe2a2SStefan Roese {
7819eefe2a2SStefan Roese struct ubifs_bud *bud;
7829eefe2a2SStefan Roese int lnum = le32_to_cpu(ref->lnum);
7839eefe2a2SStefan Roese unsigned int offs = le32_to_cpu(ref->offs);
7849eefe2a2SStefan Roese unsigned int jhead = le32_to_cpu(ref->jhead);
7859eefe2a2SStefan Roese
7869eefe2a2SStefan Roese /*
7879eefe2a2SStefan Roese * ref->offs may point to the end of LEB when the journal head points
7889eefe2a2SStefan Roese * to the end of LEB and we write reference node for it during commit.
7899eefe2a2SStefan Roese * So this is why we require 'offs > c->leb_size'.
7909eefe2a2SStefan Roese */
7919eefe2a2SStefan Roese if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
7929eefe2a2SStefan Roese lnum < c->main_first || offs > c->leb_size ||
7939eefe2a2SStefan Roese offs & (c->min_io_size - 1))
7949eefe2a2SStefan Roese return -EINVAL;
7959eefe2a2SStefan Roese
7969eefe2a2SStefan Roese /* Make sure we have not already looked at this bud */
7979eefe2a2SStefan Roese bud = ubifs_search_bud(c, lnum);
7989eefe2a2SStefan Roese if (bud) {
7999eefe2a2SStefan Roese if (bud->jhead == jhead && bud->start <= offs)
8009eefe2a2SStefan Roese return 1;
8010195a7bbSHeiko Schocher ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
8029eefe2a2SStefan Roese return -EINVAL;
8039eefe2a2SStefan Roese }
8049eefe2a2SStefan Roese
8059eefe2a2SStefan Roese return 0;
8069eefe2a2SStefan Roese }
8079eefe2a2SStefan Roese
8089eefe2a2SStefan Roese /**
8099eefe2a2SStefan Roese * replay_log_leb - replay a log logical eraseblock.
8109eefe2a2SStefan Roese * @c: UBIFS file-system description object
8119eefe2a2SStefan Roese * @lnum: log logical eraseblock to replay
8129eefe2a2SStefan Roese * @offs: offset to start replaying from
8139eefe2a2SStefan Roese * @sbuf: scan buffer
8149eefe2a2SStefan Roese *
8159eefe2a2SStefan Roese * This function replays a log LEB and returns zero in case of success, %1 if
8169eefe2a2SStefan Roese * this is the last LEB in the log, and a negative error code in case of
8179eefe2a2SStefan Roese * failure.
8189eefe2a2SStefan Roese */
replay_log_leb(struct ubifs_info * c,int lnum,int offs,void * sbuf)8199eefe2a2SStefan Roese static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
8209eefe2a2SStefan Roese {
8219eefe2a2SStefan Roese int err;
8229eefe2a2SStefan Roese struct ubifs_scan_leb *sleb;
8239eefe2a2SStefan Roese struct ubifs_scan_node *snod;
8249eefe2a2SStefan Roese const struct ubifs_cs_node *node;
8259eefe2a2SStefan Roese
8269eefe2a2SStefan Roese dbg_mnt("replay log LEB %d:%d", lnum, offs);
827ff94bc40SHeiko Schocher sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
8289eefe2a2SStefan Roese if (IS_ERR(sleb)) {
829ff94bc40SHeiko Schocher if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
830ff94bc40SHeiko Schocher return PTR_ERR(sleb);
831ff94bc40SHeiko Schocher /*
832ff94bc40SHeiko Schocher * Note, the below function will recover this log LEB only if
833ff94bc40SHeiko Schocher * it is the last, because unclean reboots can possibly corrupt
834ff94bc40SHeiko Schocher * only the tail of the log.
835ff94bc40SHeiko Schocher */
8369eefe2a2SStefan Roese sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
8379eefe2a2SStefan Roese if (IS_ERR(sleb))
8389eefe2a2SStefan Roese return PTR_ERR(sleb);
8399eefe2a2SStefan Roese }
8409eefe2a2SStefan Roese
8419eefe2a2SStefan Roese if (sleb->nodes_cnt == 0) {
8429eefe2a2SStefan Roese err = 1;
8439eefe2a2SStefan Roese goto out;
8449eefe2a2SStefan Roese }
8459eefe2a2SStefan Roese
8469eefe2a2SStefan Roese node = sleb->buf;
8479eefe2a2SStefan Roese snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
8489eefe2a2SStefan Roese if (c->cs_sqnum == 0) {
8499eefe2a2SStefan Roese /*
8509eefe2a2SStefan Roese * This is the first log LEB we are looking at, make sure that
8519eefe2a2SStefan Roese * the first node is a commit start node. Also record its
8529eefe2a2SStefan Roese * sequence number so that UBIFS can determine where the log
8539eefe2a2SStefan Roese * ends, because all nodes which were have higher sequence
8549eefe2a2SStefan Roese * numbers.
8559eefe2a2SStefan Roese */
8569eefe2a2SStefan Roese if (snod->type != UBIFS_CS_NODE) {
8570195a7bbSHeiko Schocher ubifs_err(c, "first log node at LEB %d:%d is not CS node",
8589eefe2a2SStefan Roese lnum, offs);
8599eefe2a2SStefan Roese goto out_dump;
8609eefe2a2SStefan Roese }
8619eefe2a2SStefan Roese if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
8620195a7bbSHeiko Schocher ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
8639eefe2a2SStefan Roese lnum, offs,
8649eefe2a2SStefan Roese (unsigned long long)le64_to_cpu(node->cmt_no),
8659eefe2a2SStefan Roese c->cmt_no);
8669eefe2a2SStefan Roese goto out_dump;
8679eefe2a2SStefan Roese }
8689eefe2a2SStefan Roese
8699eefe2a2SStefan Roese c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
8709eefe2a2SStefan Roese dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
8719eefe2a2SStefan Roese }
8729eefe2a2SStefan Roese
8739eefe2a2SStefan Roese if (snod->sqnum < c->cs_sqnum) {
8749eefe2a2SStefan Roese /*
8759eefe2a2SStefan Roese * This means that we reached end of log and now
8769eefe2a2SStefan Roese * look to the older log data, which was already
8779eefe2a2SStefan Roese * committed but the eraseblock was not erased (UBIFS
8789eefe2a2SStefan Roese * only un-maps it). So this basically means we have to
8799eefe2a2SStefan Roese * exit with "end of log" code.
8809eefe2a2SStefan Roese */
8819eefe2a2SStefan Roese err = 1;
8829eefe2a2SStefan Roese goto out;
8839eefe2a2SStefan Roese }
8849eefe2a2SStefan Roese
8859eefe2a2SStefan Roese /* Make sure the first node sits at offset zero of the LEB */
8869eefe2a2SStefan Roese if (snod->offs != 0) {
8870195a7bbSHeiko Schocher ubifs_err(c, "first node is not at zero offset");
8889eefe2a2SStefan Roese goto out_dump;
8899eefe2a2SStefan Roese }
8909eefe2a2SStefan Roese
8919eefe2a2SStefan Roese list_for_each_entry(snod, &sleb->nodes, list) {
8929eefe2a2SStefan Roese cond_resched();
8939eefe2a2SStefan Roese
8949eefe2a2SStefan Roese if (snod->sqnum >= SQNUM_WATERMARK) {
8950195a7bbSHeiko Schocher ubifs_err(c, "file system's life ended");
8969eefe2a2SStefan Roese goto out_dump;
8979eefe2a2SStefan Roese }
8989eefe2a2SStefan Roese
8999eefe2a2SStefan Roese if (snod->sqnum < c->cs_sqnum) {
9000195a7bbSHeiko Schocher ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
9019eefe2a2SStefan Roese snod->sqnum, c->cs_sqnum);
9029eefe2a2SStefan Roese goto out_dump;
9039eefe2a2SStefan Roese }
9049eefe2a2SStefan Roese
9059eefe2a2SStefan Roese if (snod->sqnum > c->max_sqnum)
9069eefe2a2SStefan Roese c->max_sqnum = snod->sqnum;
9079eefe2a2SStefan Roese
9089eefe2a2SStefan Roese switch (snod->type) {
9099eefe2a2SStefan Roese case UBIFS_REF_NODE: {
9109eefe2a2SStefan Roese const struct ubifs_ref_node *ref = snod->node;
9119eefe2a2SStefan Roese
9129eefe2a2SStefan Roese err = validate_ref(c, ref);
9139eefe2a2SStefan Roese if (err == 1)
9149eefe2a2SStefan Roese break; /* Already have this bud */
9159eefe2a2SStefan Roese if (err)
9169eefe2a2SStefan Roese goto out_dump;
9179eefe2a2SStefan Roese
9189eefe2a2SStefan Roese err = add_replay_bud(c, le32_to_cpu(ref->lnum),
9199eefe2a2SStefan Roese le32_to_cpu(ref->offs),
9209eefe2a2SStefan Roese le32_to_cpu(ref->jhead),
9219eefe2a2SStefan Roese snod->sqnum);
9229eefe2a2SStefan Roese if (err)
9239eefe2a2SStefan Roese goto out;
9249eefe2a2SStefan Roese
9259eefe2a2SStefan Roese break;
9269eefe2a2SStefan Roese }
9279eefe2a2SStefan Roese case UBIFS_CS_NODE:
9289eefe2a2SStefan Roese /* Make sure it sits at the beginning of LEB */
9299eefe2a2SStefan Roese if (snod->offs != 0) {
9300195a7bbSHeiko Schocher ubifs_err(c, "unexpected node in log");
9319eefe2a2SStefan Roese goto out_dump;
9329eefe2a2SStefan Roese }
9339eefe2a2SStefan Roese break;
9349eefe2a2SStefan Roese default:
9350195a7bbSHeiko Schocher ubifs_err(c, "unexpected node in log");
9369eefe2a2SStefan Roese goto out_dump;
9379eefe2a2SStefan Roese }
9389eefe2a2SStefan Roese }
9399eefe2a2SStefan Roese
9409eefe2a2SStefan Roese if (sleb->endpt || c->lhead_offs >= c->leb_size) {
9419eefe2a2SStefan Roese c->lhead_lnum = lnum;
9429eefe2a2SStefan Roese c->lhead_offs = sleb->endpt;
9439eefe2a2SStefan Roese }
9449eefe2a2SStefan Roese
9459eefe2a2SStefan Roese err = !sleb->endpt;
9469eefe2a2SStefan Roese out:
9479eefe2a2SStefan Roese ubifs_scan_destroy(sleb);
9489eefe2a2SStefan Roese return err;
9499eefe2a2SStefan Roese
9509eefe2a2SStefan Roese out_dump:
9510195a7bbSHeiko Schocher ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
9529eefe2a2SStefan Roese lnum, offs + snod->offs);
953ff94bc40SHeiko Schocher ubifs_dump_node(c, snod->node);
9549eefe2a2SStefan Roese ubifs_scan_destroy(sleb);
9559eefe2a2SStefan Roese return -EINVAL;
9569eefe2a2SStefan Roese }
9579eefe2a2SStefan Roese
9589eefe2a2SStefan Roese /**
9599eefe2a2SStefan Roese * take_ihead - update the status of the index head in lprops to 'taken'.
9609eefe2a2SStefan Roese * @c: UBIFS file-system description object
9619eefe2a2SStefan Roese *
9629eefe2a2SStefan Roese * This function returns the amount of free space in the index head LEB or a
9639eefe2a2SStefan Roese * negative error code.
9649eefe2a2SStefan Roese */
take_ihead(struct ubifs_info * c)9659eefe2a2SStefan Roese static int take_ihead(struct ubifs_info *c)
9669eefe2a2SStefan Roese {
9679eefe2a2SStefan Roese const struct ubifs_lprops *lp;
9689eefe2a2SStefan Roese int err, free;
9699eefe2a2SStefan Roese
9709eefe2a2SStefan Roese ubifs_get_lprops(c);
9719eefe2a2SStefan Roese
9729eefe2a2SStefan Roese lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
9739eefe2a2SStefan Roese if (IS_ERR(lp)) {
9749eefe2a2SStefan Roese err = PTR_ERR(lp);
9759eefe2a2SStefan Roese goto out;
9769eefe2a2SStefan Roese }
9779eefe2a2SStefan Roese
9789eefe2a2SStefan Roese free = lp->free;
9799eefe2a2SStefan Roese
9809eefe2a2SStefan Roese lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
9819eefe2a2SStefan Roese lp->flags | LPROPS_TAKEN, 0);
9829eefe2a2SStefan Roese if (IS_ERR(lp)) {
9839eefe2a2SStefan Roese err = PTR_ERR(lp);
9849eefe2a2SStefan Roese goto out;
9859eefe2a2SStefan Roese }
9869eefe2a2SStefan Roese
9879eefe2a2SStefan Roese err = free;
9889eefe2a2SStefan Roese out:
9899eefe2a2SStefan Roese ubifs_release_lprops(c);
9909eefe2a2SStefan Roese return err;
9919eefe2a2SStefan Roese }
9929eefe2a2SStefan Roese
9939eefe2a2SStefan Roese /**
9949eefe2a2SStefan Roese * ubifs_replay_journal - replay journal.
9959eefe2a2SStefan Roese * @c: UBIFS file-system description object
9969eefe2a2SStefan Roese *
9979eefe2a2SStefan Roese * This function scans the journal, replays and cleans it up. It makes sure all
9989eefe2a2SStefan Roese * memory data structures related to uncommitted journal are built (dirty TNC
9999eefe2a2SStefan Roese * tree, tree of buds, modified lprops, etc).
10009eefe2a2SStefan Roese */
ubifs_replay_journal(struct ubifs_info * c)10019eefe2a2SStefan Roese int ubifs_replay_journal(struct ubifs_info *c)
10029eefe2a2SStefan Roese {
1003ff94bc40SHeiko Schocher int err, lnum, free;
10049eefe2a2SStefan Roese
10059eefe2a2SStefan Roese BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
10069eefe2a2SStefan Roese
10079eefe2a2SStefan Roese /* Update the status of the index head in lprops to 'taken' */
1008ff94bc40SHeiko Schocher free = take_ihead(c);
1009ff94bc40SHeiko Schocher if (free < 0)
1010ff94bc40SHeiko Schocher return free; /* Error code */
10119eefe2a2SStefan Roese
1012ff94bc40SHeiko Schocher if (c->ihead_offs != c->leb_size - free) {
10130195a7bbSHeiko Schocher ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
10149eefe2a2SStefan Roese c->ihead_offs);
10159eefe2a2SStefan Roese return -EINVAL;
10169eefe2a2SStefan Roese }
10179eefe2a2SStefan Roese
10189eefe2a2SStefan Roese dbg_mnt("start replaying the journal");
10199eefe2a2SStefan Roese c->replaying = 1;
10209eefe2a2SStefan Roese lnum = c->ltail_lnum = c->lhead_lnum;
10219eefe2a2SStefan Roese
1022ff94bc40SHeiko Schocher do {
1023ff94bc40SHeiko Schocher err = replay_log_leb(c, lnum, 0, c->sbuf);
10240195a7bbSHeiko Schocher if (err == 1) {
10250195a7bbSHeiko Schocher if (lnum != c->lhead_lnum)
10269eefe2a2SStefan Roese /* We hit the end of the log */
10279eefe2a2SStefan Roese break;
10280195a7bbSHeiko Schocher
10290195a7bbSHeiko Schocher /*
10300195a7bbSHeiko Schocher * The head of the log must always start with the
10310195a7bbSHeiko Schocher * "commit start" node on a properly formatted UBIFS.
10320195a7bbSHeiko Schocher * But we found no nodes at all, which means that
10330195a7bbSHeiko Schocher * someting went wrong and we cannot proceed mounting
10340195a7bbSHeiko Schocher * the file-system.
10350195a7bbSHeiko Schocher */
10360195a7bbSHeiko Schocher ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
10370195a7bbSHeiko Schocher lnum, 0);
10380195a7bbSHeiko Schocher err = -EINVAL;
10390195a7bbSHeiko Schocher }
10409eefe2a2SStefan Roese if (err)
10419eefe2a2SStefan Roese goto out;
1042ff94bc40SHeiko Schocher lnum = ubifs_next_log_lnum(c, lnum);
1043ff94bc40SHeiko Schocher } while (lnum != c->ltail_lnum);
10449eefe2a2SStefan Roese
10459eefe2a2SStefan Roese err = replay_buds(c);
10469eefe2a2SStefan Roese if (err)
10479eefe2a2SStefan Roese goto out;
10489eefe2a2SStefan Roese
1049ff94bc40SHeiko Schocher err = apply_replay_list(c);
10509eefe2a2SStefan Roese if (err)
10519eefe2a2SStefan Roese goto out;
10529eefe2a2SStefan Roese
1053ff94bc40SHeiko Schocher err = set_buds_lprops(c);
1054ff94bc40SHeiko Schocher if (err)
1055ff94bc40SHeiko Schocher goto out;
1056ff94bc40SHeiko Schocher
1057ff94bc40SHeiko Schocher /*
1058ff94bc40SHeiko Schocher * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1059ff94bc40SHeiko Schocher * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1060ff94bc40SHeiko Schocher * depend on it. This means we have to initialize it to make sure
1061ff94bc40SHeiko Schocher * budgeting works properly.
1062ff94bc40SHeiko Schocher */
1063ff94bc40SHeiko Schocher c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1064ff94bc40SHeiko Schocher c->bi.uncommitted_idx *= c->max_idx_node_sz;
1065ff94bc40SHeiko Schocher
10669eefe2a2SStefan Roese ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1067ff94bc40SHeiko Schocher dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1068ff94bc40SHeiko Schocher c->lhead_lnum, c->lhead_offs, c->max_sqnum,
10699eefe2a2SStefan Roese (unsigned long)c->highest_inum);
10709eefe2a2SStefan Roese out:
1071ff94bc40SHeiko Schocher destroy_replay_list(c);
10729eefe2a2SStefan Roese destroy_bud_list(c);
10739eefe2a2SStefan Roese c->replaying = 0;
10749eefe2a2SStefan Roese return err;
10759eefe2a2SStefan Roese }
1076