12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21e51764aSArtem Bityutskiy /* 31e51764aSArtem Bityutskiy * This file is part of UBIFS. 41e51764aSArtem Bityutskiy * 51e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation. 61e51764aSArtem Bityutskiy * 71e51764aSArtem Bityutskiy * Authors: Adrian Hunter 81e51764aSArtem Bityutskiy * Artem Bityutskiy (Битюцкий Артём) 91e51764aSArtem Bityutskiy */ 101e51764aSArtem Bityutskiy 111e51764aSArtem Bityutskiy /* 121e51764aSArtem Bityutskiy * This file contains journal replay code. It runs when the file-system is being 131e51764aSArtem Bityutskiy * mounted and requires no locking. 141e51764aSArtem Bityutskiy * 151e51764aSArtem Bityutskiy * The larger is the journal, the longer it takes to scan it, so the longer it 161e51764aSArtem Bityutskiy * takes to mount UBIFS. This is why the journal has limited size which may be 171e51764aSArtem Bityutskiy * changed depending on the system requirements. But a larger journal gives 181e51764aSArtem Bityutskiy * faster I/O speed because it writes the index less frequently. So this is a 191e51764aSArtem Bityutskiy * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the 201e51764aSArtem Bityutskiy * larger is the journal, the more memory its index may consume. 211e51764aSArtem Bityutskiy */ 221e51764aSArtem Bityutskiy 231e51764aSArtem Bityutskiy #include "ubifs.h" 24debf12d5SArtem Bityutskiy #include <linux/list_sort.h> 25da8ef65fSSascha Hauer #include <crypto/hash.h> 26da8ef65fSSascha Hauer #include <crypto/algapi.h> 271e51764aSArtem Bityutskiy 281e51764aSArtem Bityutskiy /** 29debf12d5SArtem Bityutskiy * struct replay_entry - replay list entry. 301e51764aSArtem Bityutskiy * @lnum: logical eraseblock number of the node 311e51764aSArtem Bityutskiy * @offs: node offset 321e51764aSArtem Bityutskiy * @len: node length 33074bcb9bSArtem Bityutskiy * @deletion: non-zero if this entry corresponds to a node deletion 341e51764aSArtem Bityutskiy * @sqnum: node sequence number 35debf12d5SArtem Bityutskiy * @list: links the replay list 361e51764aSArtem Bityutskiy * @key: node key 371e51764aSArtem Bityutskiy * @nm: directory entry name 381e51764aSArtem Bityutskiy * @old_size: truncation old size 391e51764aSArtem Bityutskiy * @new_size: truncation new size 401e51764aSArtem Bityutskiy * 41debf12d5SArtem Bityutskiy * The replay process first scans all buds and builds the replay list, then 42debf12d5SArtem Bityutskiy * sorts the replay list in nodes sequence number order, and then inserts all 43debf12d5SArtem Bityutskiy * the replay entries to the TNC. 441e51764aSArtem Bityutskiy */ 451e51764aSArtem Bityutskiy struct replay_entry { 461e51764aSArtem Bityutskiy int lnum; 471e51764aSArtem Bityutskiy int offs; 481e51764aSArtem Bityutskiy int len; 4916a26b20SSascha Hauer u8 hash[UBIFS_HASH_ARR_SZ]; 50074bcb9bSArtem Bityutskiy unsigned int deletion:1; 511e51764aSArtem Bityutskiy unsigned long long sqnum; 52debf12d5SArtem Bityutskiy struct list_head list; 531e51764aSArtem Bityutskiy union ubifs_key key; 541e51764aSArtem Bityutskiy union { 55f4f61d2cSRichard Weinberger struct fscrypt_name nm; 561e51764aSArtem Bityutskiy struct { 571e51764aSArtem Bityutskiy loff_t old_size; 581e51764aSArtem Bityutskiy loff_t new_size; 591e51764aSArtem Bityutskiy }; 601e51764aSArtem Bityutskiy }; 611e51764aSArtem Bityutskiy }; 621e51764aSArtem Bityutskiy 631e51764aSArtem Bityutskiy /** 641e51764aSArtem Bityutskiy * struct bud_entry - entry in the list of buds to replay. 651e51764aSArtem Bityutskiy * @list: next bud in the list 661e51764aSArtem Bityutskiy * @bud: bud description object 671e51764aSArtem Bityutskiy * @sqnum: reference node sequence number 68af1dd412SArtem Bityutskiy * @free: free bytes in the bud 69af1dd412SArtem Bityutskiy * @dirty: dirty bytes in the bud 701e51764aSArtem Bityutskiy */ 711e51764aSArtem Bityutskiy struct bud_entry { 721e51764aSArtem Bityutskiy struct list_head list; 731e51764aSArtem Bityutskiy struct ubifs_bud *bud; 741e51764aSArtem Bityutskiy unsigned long long sqnum; 75af1dd412SArtem Bityutskiy int free; 76af1dd412SArtem Bityutskiy int dirty; 771e51764aSArtem Bityutskiy }; 781e51764aSArtem Bityutskiy 791e51764aSArtem Bityutskiy /** 801e51764aSArtem Bityutskiy * set_bud_lprops - set free and dirty space used by a bud. 811e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 82074bcb9bSArtem Bityutskiy * @b: bud entry which describes the bud 83074bcb9bSArtem Bityutskiy * 84074bcb9bSArtem Bityutskiy * This function makes sure the LEB properties of bud @b are set correctly 85074bcb9bSArtem Bityutskiy * after the replay. Returns zero in case of success and a negative error code 86074bcb9bSArtem Bityutskiy * in case of failure. 871e51764aSArtem Bityutskiy */ 88074bcb9bSArtem Bityutskiy static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b) 891e51764aSArtem Bityutskiy { 901e51764aSArtem Bityutskiy const struct ubifs_lprops *lp; 911e51764aSArtem Bityutskiy int err = 0, dirty; 921e51764aSArtem Bityutskiy 931e51764aSArtem Bityutskiy ubifs_get_lprops(c); 941e51764aSArtem Bityutskiy 95074bcb9bSArtem Bityutskiy lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum); 961e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 971e51764aSArtem Bityutskiy err = PTR_ERR(lp); 981e51764aSArtem Bityutskiy goto out; 991e51764aSArtem Bityutskiy } 1001e51764aSArtem Bityutskiy 1011e51764aSArtem Bityutskiy dirty = lp->dirty; 102074bcb9bSArtem Bityutskiy if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) { 1031e51764aSArtem Bityutskiy /* 1041e51764aSArtem Bityutskiy * The LEB was added to the journal with a starting offset of 1051e51764aSArtem Bityutskiy * zero which means the LEB must have been empty. The LEB 106074bcb9bSArtem Bityutskiy * property values should be @lp->free == @c->leb_size and 107074bcb9bSArtem Bityutskiy * @lp->dirty == 0, but that is not the case. The reason is that 1087a9c3e39SArtem Bityutskiy * the LEB had been garbage collected before it became the bud, 1097a9c3e39SArtem Bityutskiy * and there was not commit inbetween. The garbage collector 1107a9c3e39SArtem Bityutskiy * resets the free and dirty space without recording it 1117a9c3e39SArtem Bityutskiy * anywhere except lprops, so if there was no commit then 1127a9c3e39SArtem Bityutskiy * lprops does not have that information. 1131e51764aSArtem Bityutskiy * 1141e51764aSArtem Bityutskiy * We do not need to adjust free space because the scan has told 1151e51764aSArtem Bityutskiy * us the exact value which is recorded in the replay entry as 116074bcb9bSArtem Bityutskiy * @b->free. 1171e51764aSArtem Bityutskiy * 1181e51764aSArtem Bityutskiy * However we do need to subtract from the dirty space the 1191e51764aSArtem Bityutskiy * amount of space that the garbage collector reclaimed, which 1201e51764aSArtem Bityutskiy * is the whole LEB minus the amount of space that was free. 1211e51764aSArtem Bityutskiy */ 122074bcb9bSArtem Bityutskiy dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum, 1231e51764aSArtem Bityutskiy lp->free, lp->dirty); 124074bcb9bSArtem Bityutskiy dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum, 1251e51764aSArtem Bityutskiy lp->free, lp->dirty); 1261e51764aSArtem Bityutskiy dirty -= c->leb_size - lp->free; 1271e51764aSArtem Bityutskiy /* 1281e51764aSArtem Bityutskiy * If the replay order was perfect the dirty space would now be 1297d4e9ccbSArtem Bityutskiy * zero. The order is not perfect because the journal heads 1301e51764aSArtem Bityutskiy * race with each other. This is not a problem but is does mean 1311e51764aSArtem Bityutskiy * that the dirty space may temporarily exceed c->leb_size 1321e51764aSArtem Bityutskiy * during the replay. 1331e51764aSArtem Bityutskiy */ 1341e51764aSArtem Bityutskiy if (dirty != 0) 1353668b70fSArtem Bityutskiy dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty", 13679fda517SArtem Bityutskiy b->bud->lnum, lp->free, lp->dirty, b->free, 13779fda517SArtem Bityutskiy b->dirty); 1381e51764aSArtem Bityutskiy } 139074bcb9bSArtem Bityutskiy lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty, 1401e51764aSArtem Bityutskiy lp->flags | LPROPS_TAKEN, 0); 1411e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 1421e51764aSArtem Bityutskiy err = PTR_ERR(lp); 1431e51764aSArtem Bityutskiy goto out; 1441e51764aSArtem Bityutskiy } 14552c6e6f9SArtem Bityutskiy 14652c6e6f9SArtem Bityutskiy /* Make sure the journal head points to the latest bud */ 147074bcb9bSArtem Bityutskiy err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf, 148b36a261eSRichard Weinberger b->bud->lnum, c->leb_size - b->free); 14952c6e6f9SArtem Bityutskiy 1501e51764aSArtem Bityutskiy out: 1511e51764aSArtem Bityutskiy ubifs_release_lprops(c); 1521e51764aSArtem Bityutskiy return err; 1531e51764aSArtem Bityutskiy } 1541e51764aSArtem Bityutskiy 1551e51764aSArtem Bityutskiy /** 156074bcb9bSArtem Bityutskiy * set_buds_lprops - set free and dirty space for all replayed buds. 157074bcb9bSArtem Bityutskiy * @c: UBIFS file-system description object 158074bcb9bSArtem Bityutskiy * 159074bcb9bSArtem Bityutskiy * This function sets LEB properties for all replayed buds. Returns zero in 160074bcb9bSArtem Bityutskiy * case of success and a negative error code in case of failure. 161074bcb9bSArtem Bityutskiy */ 162074bcb9bSArtem Bityutskiy static int set_buds_lprops(struct ubifs_info *c) 163074bcb9bSArtem Bityutskiy { 164074bcb9bSArtem Bityutskiy struct bud_entry *b; 165074bcb9bSArtem Bityutskiy int err; 166074bcb9bSArtem Bityutskiy 167074bcb9bSArtem Bityutskiy list_for_each_entry(b, &c->replay_buds, list) { 168074bcb9bSArtem Bityutskiy err = set_bud_lprops(c, b); 169074bcb9bSArtem Bityutskiy if (err) 170074bcb9bSArtem Bityutskiy return err; 171074bcb9bSArtem Bityutskiy } 172074bcb9bSArtem Bityutskiy 173074bcb9bSArtem Bityutskiy return 0; 174074bcb9bSArtem Bityutskiy } 175074bcb9bSArtem Bityutskiy 176074bcb9bSArtem Bityutskiy /** 1771e51764aSArtem Bityutskiy * trun_remove_range - apply a replay entry for a truncation to the TNC. 1781e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 1791e51764aSArtem Bityutskiy * @r: replay entry of truncation 1801e51764aSArtem Bityutskiy */ 1811e51764aSArtem Bityutskiy static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r) 1821e51764aSArtem Bityutskiy { 1831e51764aSArtem Bityutskiy unsigned min_blk, max_blk; 1841e51764aSArtem Bityutskiy union ubifs_key min_key, max_key; 1851e51764aSArtem Bityutskiy ino_t ino; 1861e51764aSArtem Bityutskiy 1871e51764aSArtem Bityutskiy min_blk = r->new_size / UBIFS_BLOCK_SIZE; 1881e51764aSArtem Bityutskiy if (r->new_size & (UBIFS_BLOCK_SIZE - 1)) 1891e51764aSArtem Bityutskiy min_blk += 1; 1901e51764aSArtem Bityutskiy 1911e51764aSArtem Bityutskiy max_blk = r->old_size / UBIFS_BLOCK_SIZE; 1921e51764aSArtem Bityutskiy if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0) 1931e51764aSArtem Bityutskiy max_blk -= 1; 1941e51764aSArtem Bityutskiy 1951e51764aSArtem Bityutskiy ino = key_inum(c, &r->key); 1961e51764aSArtem Bityutskiy 1971e51764aSArtem Bityutskiy data_key_init(c, &min_key, ino, min_blk); 1981e51764aSArtem Bityutskiy data_key_init(c, &max_key, ino, max_blk); 1991e51764aSArtem Bityutskiy 2001e51764aSArtem Bityutskiy return ubifs_tnc_remove_range(c, &min_key, &max_key); 2011e51764aSArtem Bityutskiy } 2021e51764aSArtem Bityutskiy 2031e51764aSArtem Bityutskiy /** 204e58725d5SRichard Weinberger * inode_still_linked - check whether inode in question will be re-linked. 205e58725d5SRichard Weinberger * @c: UBIFS file-system description object 206e58725d5SRichard Weinberger * @rino: replay entry to test 207e58725d5SRichard Weinberger * 208e58725d5SRichard Weinberger * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1. 209e58725d5SRichard Weinberger * This case needs special care, otherwise all references to the inode will 210e58725d5SRichard Weinberger * be removed upon the first replay entry of an inode with link count 0 211e58725d5SRichard Weinberger * is found. 212e58725d5SRichard Weinberger */ 213e58725d5SRichard Weinberger static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino) 214e58725d5SRichard Weinberger { 215e58725d5SRichard Weinberger struct replay_entry *r; 216e58725d5SRichard Weinberger 217e58725d5SRichard Weinberger ubifs_assert(c, rino->deletion); 218e58725d5SRichard Weinberger ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY); 219e58725d5SRichard Weinberger 220e58725d5SRichard Weinberger /* 221e58725d5SRichard Weinberger * Find the most recent entry for the inode behind @rino and check 222e58725d5SRichard Weinberger * whether it is a deletion. 223e58725d5SRichard Weinberger */ 224e58725d5SRichard Weinberger list_for_each_entry_reverse(r, &c->replay_list, list) { 225e58725d5SRichard Weinberger ubifs_assert(c, r->sqnum >= rino->sqnum); 226e58725d5SRichard Weinberger if (key_inum(c, &r->key) == key_inum(c, &rino->key)) 227e58725d5SRichard Weinberger return r->deletion == 0; 228e58725d5SRichard Weinberger 229e58725d5SRichard Weinberger } 230e58725d5SRichard Weinberger 231e58725d5SRichard Weinberger ubifs_assert(c, 0); 232e58725d5SRichard Weinberger return false; 233e58725d5SRichard Weinberger } 234e58725d5SRichard Weinberger 235e58725d5SRichard Weinberger /** 2361e51764aSArtem Bityutskiy * apply_replay_entry - apply a replay entry to the TNC. 2371e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2381e51764aSArtem Bityutskiy * @r: replay entry to apply 2391e51764aSArtem Bityutskiy * 2401e51764aSArtem Bityutskiy * Apply a replay entry to the TNC. 2411e51764aSArtem Bityutskiy */ 2421e51764aSArtem Bityutskiy static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r) 2431e51764aSArtem Bityutskiy { 244074bcb9bSArtem Bityutskiy int err; 2451e51764aSArtem Bityutskiy 246515315a1SArtem Bityutskiy dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ", 247515315a1SArtem Bityutskiy r->lnum, r->offs, r->len, r->deletion, r->sqnum); 2481e51764aSArtem Bityutskiy 249074bcb9bSArtem Bityutskiy if (is_hash_key(c, &r->key)) { 250074bcb9bSArtem Bityutskiy if (r->deletion) 2511e51764aSArtem Bityutskiy err = ubifs_tnc_remove_nm(c, &r->key, &r->nm); 2521e51764aSArtem Bityutskiy else 2531e51764aSArtem Bityutskiy err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs, 25416a26b20SSascha Hauer r->len, r->hash, &r->nm); 2551e51764aSArtem Bityutskiy } else { 256074bcb9bSArtem Bityutskiy if (r->deletion) 2571e51764aSArtem Bityutskiy switch (key_type(c, &r->key)) { 2581e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 2591e51764aSArtem Bityutskiy { 2601e51764aSArtem Bityutskiy ino_t inum = key_inum(c, &r->key); 2611e51764aSArtem Bityutskiy 262e58725d5SRichard Weinberger if (inode_still_linked(c, r)) { 263e58725d5SRichard Weinberger err = 0; 264e58725d5SRichard Weinberger break; 265e58725d5SRichard Weinberger } 266e58725d5SRichard Weinberger 2671e51764aSArtem Bityutskiy err = ubifs_tnc_remove_ino(c, inum); 2681e51764aSArtem Bityutskiy break; 2691e51764aSArtem Bityutskiy } 2701e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 2711e51764aSArtem Bityutskiy err = trun_remove_range(c, r); 2721e51764aSArtem Bityutskiy break; 2731e51764aSArtem Bityutskiy default: 2741e51764aSArtem Bityutskiy err = ubifs_tnc_remove(c, &r->key); 2751e51764aSArtem Bityutskiy break; 2761e51764aSArtem Bityutskiy } 2771e51764aSArtem Bityutskiy else 2781e51764aSArtem Bityutskiy err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs, 27916a26b20SSascha Hauer r->len, r->hash); 2801e51764aSArtem Bityutskiy if (err) 2811e51764aSArtem Bityutskiy return err; 2821e51764aSArtem Bityutskiy 2831e51764aSArtem Bityutskiy if (c->need_recovery) 284074bcb9bSArtem Bityutskiy err = ubifs_recover_size_accum(c, &r->key, r->deletion, 2851e51764aSArtem Bityutskiy r->new_size); 2861e51764aSArtem Bityutskiy } 2871e51764aSArtem Bityutskiy 2881e51764aSArtem Bityutskiy return err; 2891e51764aSArtem Bityutskiy } 2901e51764aSArtem Bityutskiy 2911e51764aSArtem Bityutskiy /** 292debf12d5SArtem Bityutskiy * replay_entries_cmp - compare 2 replay entries. 293debf12d5SArtem Bityutskiy * @priv: UBIFS file-system description object 294debf12d5SArtem Bityutskiy * @a: first replay entry 295ec037dfcSJulia Lawall * @b: second replay entry 2961e51764aSArtem Bityutskiy * 297debf12d5SArtem Bityutskiy * This is a comparios function for 'list_sort()' which compares 2 replay 298debf12d5SArtem Bityutskiy * entries @a and @b by comparing their sequence numer. Returns %1 if @a has 299debf12d5SArtem Bityutskiy * greater sequence number and %-1 otherwise. 3001e51764aSArtem Bityutskiy */ 301debf12d5SArtem Bityutskiy static int replay_entries_cmp(void *priv, struct list_head *a, 302debf12d5SArtem Bityutskiy struct list_head *b) 3031e51764aSArtem Bityutskiy { 3046eb61d58SRichard Weinberger struct ubifs_info *c = priv; 305debf12d5SArtem Bityutskiy struct replay_entry *ra, *rb; 3061e51764aSArtem Bityutskiy 307debf12d5SArtem Bityutskiy cond_resched(); 308debf12d5SArtem Bityutskiy if (a == b) 309debf12d5SArtem Bityutskiy return 0; 310debf12d5SArtem Bityutskiy 311debf12d5SArtem Bityutskiy ra = list_entry(a, struct replay_entry, list); 312debf12d5SArtem Bityutskiy rb = list_entry(b, struct replay_entry, list); 3136eb61d58SRichard Weinberger ubifs_assert(c, ra->sqnum != rb->sqnum); 314debf12d5SArtem Bityutskiy if (ra->sqnum > rb->sqnum) 315debf12d5SArtem Bityutskiy return 1; 316debf12d5SArtem Bityutskiy return -1; 3171e51764aSArtem Bityutskiy } 3181e51764aSArtem Bityutskiy 3191e51764aSArtem Bityutskiy /** 320debf12d5SArtem Bityutskiy * apply_replay_list - apply the replay list to the TNC. 3211e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3221e51764aSArtem Bityutskiy * 323debf12d5SArtem Bityutskiy * Apply all entries in the replay list to the TNC. Returns zero in case of 324debf12d5SArtem Bityutskiy * success and a negative error code in case of failure. 3251e51764aSArtem Bityutskiy */ 326debf12d5SArtem Bityutskiy static int apply_replay_list(struct ubifs_info *c) 3271e51764aSArtem Bityutskiy { 3281e51764aSArtem Bityutskiy struct replay_entry *r; 3291e51764aSArtem Bityutskiy int err; 3301e51764aSArtem Bityutskiy 331debf12d5SArtem Bityutskiy list_sort(c, &c->replay_list, &replay_entries_cmp); 332debf12d5SArtem Bityutskiy 333debf12d5SArtem Bityutskiy list_for_each_entry(r, &c->replay_list, list) { 3341e51764aSArtem Bityutskiy cond_resched(); 3351e51764aSArtem Bityutskiy 3361e51764aSArtem Bityutskiy err = apply_replay_entry(c, r); 3371e51764aSArtem Bityutskiy if (err) 3381e51764aSArtem Bityutskiy return err; 3391e51764aSArtem Bityutskiy } 340debf12d5SArtem Bityutskiy 3411e51764aSArtem Bityutskiy return 0; 3421e51764aSArtem Bityutskiy } 3431e51764aSArtem Bityutskiy 3441e51764aSArtem Bityutskiy /** 345debf12d5SArtem Bityutskiy * destroy_replay_list - destroy the replay. 346debf12d5SArtem Bityutskiy * @c: UBIFS file-system description object 347debf12d5SArtem Bityutskiy * 348debf12d5SArtem Bityutskiy * Destroy the replay list. 349debf12d5SArtem Bityutskiy */ 350debf12d5SArtem Bityutskiy static void destroy_replay_list(struct ubifs_info *c) 351debf12d5SArtem Bityutskiy { 352debf12d5SArtem Bityutskiy struct replay_entry *r, *tmp; 353debf12d5SArtem Bityutskiy 354debf12d5SArtem Bityutskiy list_for_each_entry_safe(r, tmp, &c->replay_list, list) { 355debf12d5SArtem Bityutskiy if (is_hash_key(c, &r->key)) 356f4f61d2cSRichard Weinberger kfree(fname_name(&r->nm)); 357debf12d5SArtem Bityutskiy list_del(&r->list); 358debf12d5SArtem Bityutskiy kfree(r); 359debf12d5SArtem Bityutskiy } 360debf12d5SArtem Bityutskiy } 361debf12d5SArtem Bityutskiy 362debf12d5SArtem Bityutskiy /** 363debf12d5SArtem Bityutskiy * insert_node - insert a node to the replay list 3641e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3651e51764aSArtem Bityutskiy * @lnum: node logical eraseblock number 3661e51764aSArtem Bityutskiy * @offs: node offset 3671e51764aSArtem Bityutskiy * @len: node length 3681e51764aSArtem Bityutskiy * @key: node key 3691e51764aSArtem Bityutskiy * @sqnum: sequence number 3701e51764aSArtem Bityutskiy * @deletion: non-zero if this is a deletion 3711e51764aSArtem Bityutskiy * @used: number of bytes in use in a LEB 3721e51764aSArtem Bityutskiy * @old_size: truncation old size 3731e51764aSArtem Bityutskiy * @new_size: truncation new size 3741e51764aSArtem Bityutskiy * 375debf12d5SArtem Bityutskiy * This function inserts a scanned non-direntry node to the replay list. The 376debf12d5SArtem Bityutskiy * replay list contains @struct replay_entry elements, and we sort this list in 377debf12d5SArtem Bityutskiy * sequence number order before applying it. The replay list is applied at the 378debf12d5SArtem Bityutskiy * very end of the replay process. Since the list is sorted in sequence number 379debf12d5SArtem Bityutskiy * order, the older modifications are applied first. This function returns zero 380debf12d5SArtem Bityutskiy * in case of success and a negative error code in case of failure. 3811e51764aSArtem Bityutskiy */ 3821e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len, 38316a26b20SSascha Hauer const u8 *hash, union ubifs_key *key, 38416a26b20SSascha Hauer unsigned long long sqnum, int deletion, int *used, 38516a26b20SSascha Hauer loff_t old_size, loff_t new_size) 3861e51764aSArtem Bityutskiy { 3871e51764aSArtem Bityutskiy struct replay_entry *r; 3881e51764aSArtem Bityutskiy 389515315a1SArtem Bityutskiy dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs); 390debf12d5SArtem Bityutskiy 3911e51764aSArtem Bityutskiy if (key_inum(c, key) >= c->highest_inum) 3921e51764aSArtem Bityutskiy c->highest_inum = key_inum(c, key); 3931e51764aSArtem Bityutskiy 3941e51764aSArtem Bityutskiy r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL); 3951e51764aSArtem Bityutskiy if (!r) 3961e51764aSArtem Bityutskiy return -ENOMEM; 3971e51764aSArtem Bityutskiy 3981e51764aSArtem Bityutskiy if (!deletion) 3991e51764aSArtem Bityutskiy *used += ALIGN(len, 8); 4001e51764aSArtem Bityutskiy r->lnum = lnum; 4011e51764aSArtem Bityutskiy r->offs = offs; 4021e51764aSArtem Bityutskiy r->len = len; 40316a26b20SSascha Hauer ubifs_copy_hash(c, hash, r->hash); 404074bcb9bSArtem Bityutskiy r->deletion = !!deletion; 4051e51764aSArtem Bityutskiy r->sqnum = sqnum; 406074bcb9bSArtem Bityutskiy key_copy(c, key, &r->key); 4071e51764aSArtem Bityutskiy r->old_size = old_size; 4081e51764aSArtem Bityutskiy r->new_size = new_size; 4091e51764aSArtem Bityutskiy 410debf12d5SArtem Bityutskiy list_add_tail(&r->list, &c->replay_list); 4111e51764aSArtem Bityutskiy return 0; 4121e51764aSArtem Bityutskiy } 4131e51764aSArtem Bityutskiy 4141e51764aSArtem Bityutskiy /** 415debf12d5SArtem Bityutskiy * insert_dent - insert a directory entry node into the replay list. 4161e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4171e51764aSArtem Bityutskiy * @lnum: node logical eraseblock number 4181e51764aSArtem Bityutskiy * @offs: node offset 4191e51764aSArtem Bityutskiy * @len: node length 4201e51764aSArtem Bityutskiy * @key: node key 4211e51764aSArtem Bityutskiy * @name: directory entry name 4221e51764aSArtem Bityutskiy * @nlen: directory entry name length 4231e51764aSArtem Bityutskiy * @sqnum: sequence number 4241e51764aSArtem Bityutskiy * @deletion: non-zero if this is a deletion 4251e51764aSArtem Bityutskiy * @used: number of bytes in use in a LEB 4261e51764aSArtem Bityutskiy * 427debf12d5SArtem Bityutskiy * This function inserts a scanned directory entry node or an extended 428debf12d5SArtem Bityutskiy * attribute entry to the replay list. Returns zero in case of success and a 429debf12d5SArtem Bityutskiy * negative error code in case of failure. 4301e51764aSArtem Bityutskiy */ 4311e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len, 43216a26b20SSascha Hauer const u8 *hash, union ubifs_key *key, 43316a26b20SSascha Hauer const char *name, int nlen, unsigned long long sqnum, 43416a26b20SSascha Hauer int deletion, int *used) 4351e51764aSArtem Bityutskiy { 4361e51764aSArtem Bityutskiy struct replay_entry *r; 4371e51764aSArtem Bityutskiy char *nbuf; 4381e51764aSArtem Bityutskiy 439515315a1SArtem Bityutskiy dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs); 4401e51764aSArtem Bityutskiy if (key_inum(c, key) >= c->highest_inum) 4411e51764aSArtem Bityutskiy c->highest_inum = key_inum(c, key); 4421e51764aSArtem Bityutskiy 4431e51764aSArtem Bityutskiy r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL); 4441e51764aSArtem Bityutskiy if (!r) 4451e51764aSArtem Bityutskiy return -ENOMEM; 446debf12d5SArtem Bityutskiy 4471e51764aSArtem Bityutskiy nbuf = kmalloc(nlen + 1, GFP_KERNEL); 4481e51764aSArtem Bityutskiy if (!nbuf) { 4491e51764aSArtem Bityutskiy kfree(r); 4501e51764aSArtem Bityutskiy return -ENOMEM; 4511e51764aSArtem Bityutskiy } 4521e51764aSArtem Bityutskiy 4531e51764aSArtem Bityutskiy if (!deletion) 4541e51764aSArtem Bityutskiy *used += ALIGN(len, 8); 4551e51764aSArtem Bityutskiy r->lnum = lnum; 4561e51764aSArtem Bityutskiy r->offs = offs; 4571e51764aSArtem Bityutskiy r->len = len; 45816a26b20SSascha Hauer ubifs_copy_hash(c, hash, r->hash); 459074bcb9bSArtem Bityutskiy r->deletion = !!deletion; 4601e51764aSArtem Bityutskiy r->sqnum = sqnum; 461074bcb9bSArtem Bityutskiy key_copy(c, key, &r->key); 462f4f61d2cSRichard Weinberger fname_len(&r->nm) = nlen; 4631e51764aSArtem Bityutskiy memcpy(nbuf, name, nlen); 4641e51764aSArtem Bityutskiy nbuf[nlen] = '\0'; 465f4f61d2cSRichard Weinberger fname_name(&r->nm) = nbuf; 4661e51764aSArtem Bityutskiy 467debf12d5SArtem Bityutskiy list_add_tail(&r->list, &c->replay_list); 4681e51764aSArtem Bityutskiy return 0; 4691e51764aSArtem Bityutskiy } 4701e51764aSArtem Bityutskiy 4711e51764aSArtem Bityutskiy /** 4721e51764aSArtem Bityutskiy * ubifs_validate_entry - validate directory or extended attribute entry node. 4731e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4741e51764aSArtem Bityutskiy * @dent: the node to validate 4751e51764aSArtem Bityutskiy * 4761e51764aSArtem Bityutskiy * This function validates directory or extended attribute entry node @dent. 4771e51764aSArtem Bityutskiy * Returns zero if the node is all right and a %-EINVAL if not. 4781e51764aSArtem Bityutskiy */ 4791e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c, 4801e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent) 4811e51764aSArtem Bityutskiy { 4821e51764aSArtem Bityutskiy int key_type = key_type_flash(c, dent->key); 4831e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 4841e51764aSArtem Bityutskiy 4851e51764aSArtem Bityutskiy if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 || 4861e51764aSArtem Bityutskiy dent->type >= UBIFS_ITYPES_CNT || 4871e51764aSArtem Bityutskiy nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 || 488304790c0SRichard Weinberger (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) || 4891e51764aSArtem Bityutskiy le64_to_cpu(dent->inum) > MAX_INUM) { 490235c362bSSheng Yong ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ? 4911e51764aSArtem Bityutskiy "directory entry" : "extended attribute entry"); 4921e51764aSArtem Bityutskiy return -EINVAL; 4931e51764aSArtem Bityutskiy } 4941e51764aSArtem Bityutskiy 4951e51764aSArtem Bityutskiy if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) { 496235c362bSSheng Yong ubifs_err(c, "bad key type %d", key_type); 4971e51764aSArtem Bityutskiy return -EINVAL; 4981e51764aSArtem Bityutskiy } 4991e51764aSArtem Bityutskiy 5001e51764aSArtem Bityutskiy return 0; 5011e51764aSArtem Bityutskiy } 5021e51764aSArtem Bityutskiy 5031e51764aSArtem Bityutskiy /** 50491c66083SArtem Bityutskiy * is_last_bud - check if the bud is the last in the journal head. 50591c66083SArtem Bityutskiy * @c: UBIFS file-system description object 50691c66083SArtem Bityutskiy * @bud: bud description object 50791c66083SArtem Bityutskiy * 50891c66083SArtem Bityutskiy * This function checks if bud @bud is the last bud in its journal head. This 50991c66083SArtem Bityutskiy * information is then used by 'replay_bud()' to decide whether the bud can 51091c66083SArtem Bityutskiy * have corruptions or not. Indeed, only last buds can be corrupted by power 51191c66083SArtem Bityutskiy * cuts. Returns %1 if this is the last bud, and %0 if not. 51291c66083SArtem Bityutskiy */ 51391c66083SArtem Bityutskiy static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud) 51491c66083SArtem Bityutskiy { 51591c66083SArtem Bityutskiy struct ubifs_jhead *jh = &c->jheads[bud->jhead]; 51691c66083SArtem Bityutskiy struct ubifs_bud *next; 51791c66083SArtem Bityutskiy uint32_t data; 51891c66083SArtem Bityutskiy int err; 51991c66083SArtem Bityutskiy 52091c66083SArtem Bityutskiy if (list_is_last(&bud->list, &jh->buds_list)) 52191c66083SArtem Bityutskiy return 1; 52291c66083SArtem Bityutskiy 52391c66083SArtem Bityutskiy /* 52491c66083SArtem Bityutskiy * The following is a quirk to make sure we work correctly with UBIFS 52591c66083SArtem Bityutskiy * images used with older UBIFS. 52691c66083SArtem Bityutskiy * 52791c66083SArtem Bityutskiy * Normally, the last bud will be the last in the journal head's list 52891c66083SArtem Bityutskiy * of bud. However, there is one exception if the UBIFS image belongs 52991c66083SArtem Bityutskiy * to older UBIFS. This is fairly unlikely: one would need to use old 53091c66083SArtem Bityutskiy * UBIFS, then have a power cut exactly at the right point, and then 53191c66083SArtem Bityutskiy * try to mount this image with new UBIFS. 53291c66083SArtem Bityutskiy * 53391c66083SArtem Bityutskiy * The exception is: it is possible to have 2 buds A and B, A goes 53491c66083SArtem Bityutskiy * before B, and B is the last, bud B is contains no data, and bud A is 53591c66083SArtem Bityutskiy * corrupted at the end. The reason is that in older versions when the 53691c66083SArtem Bityutskiy * journal code switched the next bud (from A to B), it first added a 53791c66083SArtem Bityutskiy * log reference node for the new bud (B), and only after this it 53891c66083SArtem Bityutskiy * synchronized the write-buffer of current bud (A). But later this was 53991c66083SArtem Bityutskiy * changed and UBIFS started to always synchronize the write-buffer of 54091c66083SArtem Bityutskiy * the bud (A) before writing the log reference for the new bud (B). 54191c66083SArtem Bityutskiy * 54291c66083SArtem Bityutskiy * But because older UBIFS always synchronized A's write-buffer before 54391c66083SArtem Bityutskiy * writing to B, we can recognize this exceptional situation but 54491c66083SArtem Bityutskiy * checking the contents of bud B - if it is empty, then A can be 54591c66083SArtem Bityutskiy * treated as the last and we can recover it. 54691c66083SArtem Bityutskiy * 54791c66083SArtem Bityutskiy * TODO: remove this piece of code in a couple of years (today it is 54891c66083SArtem Bityutskiy * 16.05.2011). 54991c66083SArtem Bityutskiy */ 55091c66083SArtem Bityutskiy next = list_entry(bud->list.next, struct ubifs_bud, list); 55191c66083SArtem Bityutskiy if (!list_is_last(&next->list, &jh->buds_list)) 55291c66083SArtem Bityutskiy return 0; 55391c66083SArtem Bityutskiy 554d304820aSArtem Bityutskiy err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1); 55591c66083SArtem Bityutskiy if (err) 55691c66083SArtem Bityutskiy return 0; 55791c66083SArtem Bityutskiy 55891c66083SArtem Bityutskiy return data == 0xFFFFFFFF; 55991c66083SArtem Bityutskiy } 56091c66083SArtem Bityutskiy 561*f80df385SEric Biggers /* authenticate_sleb_hash is split out for stack usage */ 562eb66eff6SArnd Bergmann static int authenticate_sleb_hash(struct ubifs_info *c, struct shash_desc *log_hash, u8 *hash) 563eb66eff6SArnd Bergmann { 564eb66eff6SArnd Bergmann SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm); 565eb66eff6SArnd Bergmann 566eb66eff6SArnd Bergmann hash_desc->tfm = c->hash_tfm; 567eb66eff6SArnd Bergmann 568eb66eff6SArnd Bergmann ubifs_shash_copy_state(c, log_hash, hash_desc); 569eb66eff6SArnd Bergmann return crypto_shash_final(hash_desc, hash); 570eb66eff6SArnd Bergmann } 571eb66eff6SArnd Bergmann 57291c66083SArtem Bityutskiy /** 573da8ef65fSSascha Hauer * authenticate_sleb - authenticate one scan LEB 574da8ef65fSSascha Hauer * @c: UBIFS file-system description object 575da8ef65fSSascha Hauer * @sleb: the scan LEB to authenticate 576da8ef65fSSascha Hauer * @log_hash: 577da8ef65fSSascha Hauer * @is_last: if true, this is is the last LEB 578da8ef65fSSascha Hauer * 579da8ef65fSSascha Hauer * This function iterates over the buds of a single LEB authenticating all buds 580da8ef65fSSascha Hauer * with the authentication nodes on this LEB. Authentication nodes are written 581da8ef65fSSascha Hauer * after some buds and contain a HMAC covering the authentication node itself 582da8ef65fSSascha Hauer * and the buds between the last authentication node and the current 583da8ef65fSSascha Hauer * authentication node. It can happen that the last buds cannot be authenticated 584da8ef65fSSascha Hauer * because a powercut happened when some nodes were written but not the 585da8ef65fSSascha Hauer * corresponding authentication node. This function returns the number of nodes 586da8ef65fSSascha Hauer * that could be authenticated or a negative error code. 587da8ef65fSSascha Hauer */ 588da8ef65fSSascha Hauer static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb, 589da8ef65fSSascha Hauer struct shash_desc *log_hash, int is_last) 590da8ef65fSSascha Hauer { 591da8ef65fSSascha Hauer int n_not_auth = 0; 592da8ef65fSSascha Hauer struct ubifs_scan_node *snod; 593da8ef65fSSascha Hauer int n_nodes = 0; 594da8ef65fSSascha Hauer int err; 595da8ef65fSSascha Hauer u8 *hash, *hmac; 596da8ef65fSSascha Hauer 597da8ef65fSSascha Hauer if (!ubifs_authenticated(c)) 598da8ef65fSSascha Hauer return sleb->nodes_cnt; 599da8ef65fSSascha Hauer 600da8ef65fSSascha Hauer hash = kmalloc(crypto_shash_descsize(c->hash_tfm), GFP_NOFS); 601da8ef65fSSascha Hauer hmac = kmalloc(c->hmac_desc_len, GFP_NOFS); 602da8ef65fSSascha Hauer if (!hash || !hmac) { 603da8ef65fSSascha Hauer err = -ENOMEM; 604da8ef65fSSascha Hauer goto out; 605da8ef65fSSascha Hauer } 606da8ef65fSSascha Hauer 607da8ef65fSSascha Hauer list_for_each_entry(snod, &sleb->nodes, list) { 608da8ef65fSSascha Hauer 609da8ef65fSSascha Hauer n_nodes++; 610da8ef65fSSascha Hauer 611da8ef65fSSascha Hauer if (snod->type == UBIFS_AUTH_NODE) { 612da8ef65fSSascha Hauer struct ubifs_auth_node *auth = snod->node; 613da8ef65fSSascha Hauer 614eb66eff6SArnd Bergmann err = authenticate_sleb_hash(c, log_hash, hash); 615da8ef65fSSascha Hauer if (err) 616da8ef65fSSascha Hauer goto out; 617da8ef65fSSascha Hauer 618*f80df385SEric Biggers err = crypto_shash_tfm_digest(c->hmac_tfm, hash, 619*f80df385SEric Biggers c->hash_len, hmac); 620da8ef65fSSascha Hauer if (err) 621da8ef65fSSascha Hauer goto out; 622da8ef65fSSascha Hauer 623da8ef65fSSascha Hauer err = ubifs_check_hmac(c, auth->hmac, hmac); 624da8ef65fSSascha Hauer if (err) { 625da8ef65fSSascha Hauer err = -EPERM; 626da8ef65fSSascha Hauer goto out; 627da8ef65fSSascha Hauer } 628da8ef65fSSascha Hauer n_not_auth = 0; 629da8ef65fSSascha Hauer } else { 630da8ef65fSSascha Hauer err = crypto_shash_update(log_hash, snod->node, 631da8ef65fSSascha Hauer snod->len); 632da8ef65fSSascha Hauer if (err) 633da8ef65fSSascha Hauer goto out; 634da8ef65fSSascha Hauer n_not_auth++; 635da8ef65fSSascha Hauer } 636da8ef65fSSascha Hauer } 637da8ef65fSSascha Hauer 638da8ef65fSSascha Hauer /* 639da8ef65fSSascha Hauer * A powercut can happen when some nodes were written, but not yet 640da8ef65fSSascha Hauer * the corresponding authentication node. This may only happen on 641da8ef65fSSascha Hauer * the last bud though. 642da8ef65fSSascha Hauer */ 643da8ef65fSSascha Hauer if (n_not_auth) { 644da8ef65fSSascha Hauer if (is_last) { 645da8ef65fSSascha Hauer dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them", 646da8ef65fSSascha Hauer n_not_auth, sleb->lnum); 647da8ef65fSSascha Hauer err = 0; 648da8ef65fSSascha Hauer } else { 649da8ef65fSSascha Hauer dbg_mnt("%d unauthenticated nodes found on non-last LEB %d", 650da8ef65fSSascha Hauer n_not_auth, sleb->lnum); 651da8ef65fSSascha Hauer err = -EPERM; 652da8ef65fSSascha Hauer } 653da8ef65fSSascha Hauer } else { 654da8ef65fSSascha Hauer err = 0; 655da8ef65fSSascha Hauer } 656da8ef65fSSascha Hauer out: 657da8ef65fSSascha Hauer kfree(hash); 658da8ef65fSSascha Hauer kfree(hmac); 659da8ef65fSSascha Hauer 660da8ef65fSSascha Hauer return err ? err : n_nodes - n_not_auth; 661da8ef65fSSascha Hauer } 662da8ef65fSSascha Hauer 663da8ef65fSSascha Hauer /** 6641e51764aSArtem Bityutskiy * replay_bud - replay a bud logical eraseblock. 6651e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 666e76a4526SArtem Bityutskiy * @b: bud entry which describes the bud 6671e51764aSArtem Bityutskiy * 668e76a4526SArtem Bityutskiy * This function replays bud @bud, recovers it if needed, and adds all nodes 669e76a4526SArtem Bityutskiy * from this bud to the replay list. Returns zero in case of success and a 670e76a4526SArtem Bityutskiy * negative error code in case of failure. 6711e51764aSArtem Bityutskiy */ 672e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b) 6731e51764aSArtem Bityutskiy { 67491c66083SArtem Bityutskiy int is_last = is_last_bud(c, b->bud); 675e76a4526SArtem Bityutskiy int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start; 676da8ef65fSSascha Hauer int n_nodes, n = 0; 6771e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 6781e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 6791e51764aSArtem Bityutskiy 68091c66083SArtem Bityutskiy dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d", 68191c66083SArtem Bityutskiy lnum, b->bud->jhead, offs, is_last); 682e76a4526SArtem Bityutskiy 68391c66083SArtem Bityutskiy if (c->need_recovery && is_last) 68491c66083SArtem Bityutskiy /* 68591c66083SArtem Bityutskiy * Recover only last LEBs in the journal heads, because power 68691c66083SArtem Bityutskiy * cuts may cause corruptions only in these LEBs, because only 68791c66083SArtem Bityutskiy * these LEBs could possibly be written to at the power cut 68891c66083SArtem Bityutskiy * time. 68991c66083SArtem Bityutskiy */ 690efcfde54SArtem Bityutskiy sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead); 6911e51764aSArtem Bityutskiy else 692348709baSArtem Bityutskiy sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0); 6931e51764aSArtem Bityutskiy if (IS_ERR(sleb)) 6941e51764aSArtem Bityutskiy return PTR_ERR(sleb); 6951e51764aSArtem Bityutskiy 696da8ef65fSSascha Hauer n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last); 697da8ef65fSSascha Hauer if (n_nodes < 0) { 698da8ef65fSSascha Hauer err = n_nodes; 699da8ef65fSSascha Hauer goto out; 700da8ef65fSSascha Hauer } 701da8ef65fSSascha Hauer 702da8ef65fSSascha Hauer ubifs_shash_copy_state(c, b->bud->log_hash, 703da8ef65fSSascha Hauer c->jheads[b->bud->jhead].log_hash); 704da8ef65fSSascha Hauer 7051e51764aSArtem Bityutskiy /* 7061e51764aSArtem Bityutskiy * The bud does not have to start from offset zero - the beginning of 7071e51764aSArtem Bityutskiy * the 'lnum' LEB may contain previously committed data. One of the 7081e51764aSArtem Bityutskiy * things we have to do in replay is to correctly update lprops with 7091e51764aSArtem Bityutskiy * newer information about this LEB. 7101e51764aSArtem Bityutskiy * 7111e51764aSArtem Bityutskiy * At this point lprops thinks that this LEB has 'c->leb_size - offs' 7121e51764aSArtem Bityutskiy * bytes of free space because it only contain information about 7131e51764aSArtem Bityutskiy * committed data. 7141e51764aSArtem Bityutskiy * 7151e51764aSArtem Bityutskiy * But we know that real amount of free space is 'c->leb_size - 7161e51764aSArtem Bityutskiy * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and 7171e51764aSArtem Bityutskiy * 'sleb->endpt' is used by bud data. We have to correctly calculate 7181e51764aSArtem Bityutskiy * how much of these data are dirty and update lprops with this 7191e51764aSArtem Bityutskiy * information. 7201e51764aSArtem Bityutskiy * 7211e51764aSArtem Bityutskiy * The dirt in that LEB region is comprised of padding nodes, deletion 7221e51764aSArtem Bityutskiy * nodes, truncation nodes and nodes which are obsoleted by subsequent 7231e51764aSArtem Bityutskiy * nodes in this LEB. So instead of calculating clean space, we 7241e51764aSArtem Bityutskiy * calculate used space ('used' variable). 7251e51764aSArtem Bityutskiy */ 7261e51764aSArtem Bityutskiy 7271e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 72816a26b20SSascha Hauer u8 hash[UBIFS_HASH_ARR_SZ]; 7291e51764aSArtem Bityutskiy int deletion = 0; 7301e51764aSArtem Bityutskiy 7311e51764aSArtem Bityutskiy cond_resched(); 7321e51764aSArtem Bityutskiy 7331e51764aSArtem Bityutskiy if (snod->sqnum >= SQNUM_WATERMARK) { 734235c362bSSheng Yong ubifs_err(c, "file system's life ended"); 7351e51764aSArtem Bityutskiy goto out_dump; 7361e51764aSArtem Bityutskiy } 7371e51764aSArtem Bityutskiy 73816a26b20SSascha Hauer ubifs_node_calc_hash(c, snod->node, hash); 73916a26b20SSascha Hauer 7401e51764aSArtem Bityutskiy if (snod->sqnum > c->max_sqnum) 7411e51764aSArtem Bityutskiy c->max_sqnum = snod->sqnum; 7421e51764aSArtem Bityutskiy 7431e51764aSArtem Bityutskiy switch (snod->type) { 7441e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 7451e51764aSArtem Bityutskiy { 7461e51764aSArtem Bityutskiy struct ubifs_ino_node *ino = snod->node; 7471e51764aSArtem Bityutskiy loff_t new_size = le64_to_cpu(ino->size); 7481e51764aSArtem Bityutskiy 7491e51764aSArtem Bityutskiy if (le32_to_cpu(ino->nlink) == 0) 7501e51764aSArtem Bityutskiy deletion = 1; 75116a26b20SSascha Hauer err = insert_node(c, lnum, snod->offs, snod->len, hash, 7521e51764aSArtem Bityutskiy &snod->key, snod->sqnum, deletion, 7531e51764aSArtem Bityutskiy &used, 0, new_size); 7541e51764aSArtem Bityutskiy break; 7551e51764aSArtem Bityutskiy } 7561e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 7571e51764aSArtem Bityutskiy { 7581e51764aSArtem Bityutskiy struct ubifs_data_node *dn = snod->node; 7591e51764aSArtem Bityutskiy loff_t new_size = le32_to_cpu(dn->size) + 7601e51764aSArtem Bityutskiy key_block(c, &snod->key) * 7611e51764aSArtem Bityutskiy UBIFS_BLOCK_SIZE; 7621e51764aSArtem Bityutskiy 76316a26b20SSascha Hauer err = insert_node(c, lnum, snod->offs, snod->len, hash, 7641e51764aSArtem Bityutskiy &snod->key, snod->sqnum, deletion, 7651e51764aSArtem Bityutskiy &used, 0, new_size); 7661e51764aSArtem Bityutskiy break; 7671e51764aSArtem Bityutskiy } 7681e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 7691e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 7701e51764aSArtem Bityutskiy { 7711e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = snod->node; 7721e51764aSArtem Bityutskiy 7731e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 7741e51764aSArtem Bityutskiy if (err) 7751e51764aSArtem Bityutskiy goto out_dump; 7761e51764aSArtem Bityutskiy 77716a26b20SSascha Hauer err = insert_dent(c, lnum, snod->offs, snod->len, hash, 7781e51764aSArtem Bityutskiy &snod->key, dent->name, 7791e51764aSArtem Bityutskiy le16_to_cpu(dent->nlen), snod->sqnum, 7801e51764aSArtem Bityutskiy !le64_to_cpu(dent->inum), &used); 7811e51764aSArtem Bityutskiy break; 7821e51764aSArtem Bityutskiy } 7831e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 7841e51764aSArtem Bityutskiy { 7851e51764aSArtem Bityutskiy struct ubifs_trun_node *trun = snod->node; 7861e51764aSArtem Bityutskiy loff_t old_size = le64_to_cpu(trun->old_size); 7871e51764aSArtem Bityutskiy loff_t new_size = le64_to_cpu(trun->new_size); 7881e51764aSArtem Bityutskiy union ubifs_key key; 7891e51764aSArtem Bityutskiy 7901e51764aSArtem Bityutskiy /* Validate truncation node */ 7911e51764aSArtem Bityutskiy if (old_size < 0 || old_size > c->max_inode_sz || 7921e51764aSArtem Bityutskiy new_size < 0 || new_size > c->max_inode_sz || 7931e51764aSArtem Bityutskiy old_size <= new_size) { 794235c362bSSheng Yong ubifs_err(c, "bad truncation node"); 7951e51764aSArtem Bityutskiy goto out_dump; 7961e51764aSArtem Bityutskiy } 7971e51764aSArtem Bityutskiy 7981e51764aSArtem Bityutskiy /* 7991e51764aSArtem Bityutskiy * Create a fake truncation key just to use the same 8001e51764aSArtem Bityutskiy * functions which expect nodes to have keys. 8011e51764aSArtem Bityutskiy */ 8021e51764aSArtem Bityutskiy trun_key_init(c, &key, le32_to_cpu(trun->inum)); 80316a26b20SSascha Hauer err = insert_node(c, lnum, snod->offs, snod->len, hash, 8041e51764aSArtem Bityutskiy &key, snod->sqnum, 1, &used, 8051e51764aSArtem Bityutskiy old_size, new_size); 8061e51764aSArtem Bityutskiy break; 8071e51764aSArtem Bityutskiy } 8086a98bc46SSascha Hauer case UBIFS_AUTH_NODE: 8096a98bc46SSascha Hauer break; 8101e51764aSArtem Bityutskiy default: 811235c362bSSheng Yong ubifs_err(c, "unexpected node type %d in bud LEB %d:%d", 8121e51764aSArtem Bityutskiy snod->type, lnum, snod->offs); 8131e51764aSArtem Bityutskiy err = -EINVAL; 8141e51764aSArtem Bityutskiy goto out_dump; 8151e51764aSArtem Bityutskiy } 8161e51764aSArtem Bityutskiy if (err) 8171e51764aSArtem Bityutskiy goto out; 818da8ef65fSSascha Hauer 819da8ef65fSSascha Hauer n++; 820da8ef65fSSascha Hauer if (n == n_nodes) 821da8ef65fSSascha Hauer break; 8221e51764aSArtem Bityutskiy } 8231e51764aSArtem Bityutskiy 8246eb61d58SRichard Weinberger ubifs_assert(c, ubifs_search_bud(c, lnum)); 8256eb61d58SRichard Weinberger ubifs_assert(c, sleb->endpt - offs >= used); 8266eb61d58SRichard Weinberger ubifs_assert(c, sleb->endpt % c->min_io_size == 0); 8271e51764aSArtem Bityutskiy 828e76a4526SArtem Bityutskiy b->dirty = sleb->endpt - offs - used; 829e76a4526SArtem Bityutskiy b->free = c->leb_size - sleb->endpt; 83079fda517SArtem Bityutskiy dbg_mnt("bud LEB %d replied: dirty %d, free %d", 83179fda517SArtem Bityutskiy lnum, b->dirty, b->free); 8321e51764aSArtem Bityutskiy 8331e51764aSArtem Bityutskiy out: 8341e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8351e51764aSArtem Bityutskiy return err; 8361e51764aSArtem Bityutskiy 8371e51764aSArtem Bityutskiy out_dump: 838235c362bSSheng Yong ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs); 839edf6be24SArtem Bityutskiy ubifs_dump_node(c, snod->node); 8401e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8411e51764aSArtem Bityutskiy return -EINVAL; 8421e51764aSArtem Bityutskiy } 8431e51764aSArtem Bityutskiy 8441e51764aSArtem Bityutskiy /** 8451e51764aSArtem Bityutskiy * replay_buds - replay all buds. 8461e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8471e51764aSArtem Bityutskiy * 8481e51764aSArtem Bityutskiy * This function returns zero in case of success and a negative error code in 8491e51764aSArtem Bityutskiy * case of failure. 8501e51764aSArtem Bityutskiy */ 8511e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c) 8521e51764aSArtem Bityutskiy { 8531e51764aSArtem Bityutskiy struct bud_entry *b; 854074bcb9bSArtem Bityutskiy int err; 8557703f09dSArtem Bityutskiy unsigned long long prev_sqnum = 0; 8561e51764aSArtem Bityutskiy 8571e51764aSArtem Bityutskiy list_for_each_entry(b, &c->replay_buds, list) { 858e76a4526SArtem Bityutskiy err = replay_bud(c, b); 8591e51764aSArtem Bityutskiy if (err) 8601e51764aSArtem Bityutskiy return err; 8617703f09dSArtem Bityutskiy 8626eb61d58SRichard Weinberger ubifs_assert(c, b->sqnum > prev_sqnum); 8637703f09dSArtem Bityutskiy prev_sqnum = b->sqnum; 8641e51764aSArtem Bityutskiy } 8651e51764aSArtem Bityutskiy 8661e51764aSArtem Bityutskiy return 0; 8671e51764aSArtem Bityutskiy } 8681e51764aSArtem Bityutskiy 8691e51764aSArtem Bityutskiy /** 8701e51764aSArtem Bityutskiy * destroy_bud_list - destroy the list of buds to replay. 8711e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8721e51764aSArtem Bityutskiy */ 8731e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c) 8741e51764aSArtem Bityutskiy { 8751e51764aSArtem Bityutskiy struct bud_entry *b; 8761e51764aSArtem Bityutskiy 8771e51764aSArtem Bityutskiy while (!list_empty(&c->replay_buds)) { 8781e51764aSArtem Bityutskiy b = list_entry(c->replay_buds.next, struct bud_entry, list); 8791e51764aSArtem Bityutskiy list_del(&b->list); 8801e51764aSArtem Bityutskiy kfree(b); 8811e51764aSArtem Bityutskiy } 8821e51764aSArtem Bityutskiy } 8831e51764aSArtem Bityutskiy 8841e51764aSArtem Bityutskiy /** 8851e51764aSArtem Bityutskiy * add_replay_bud - add a bud to the list of buds to replay. 8861e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8871e51764aSArtem Bityutskiy * @lnum: bud logical eraseblock number to replay 8881e51764aSArtem Bityutskiy * @offs: bud start offset 8891e51764aSArtem Bityutskiy * @jhead: journal head to which this bud belongs 8901e51764aSArtem Bityutskiy * @sqnum: reference node sequence number 8911e51764aSArtem Bityutskiy * 8921e51764aSArtem Bityutskiy * This function returns zero in case of success and a negative error code in 8931e51764aSArtem Bityutskiy * case of failure. 8941e51764aSArtem Bityutskiy */ 8951e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead, 8961e51764aSArtem Bityutskiy unsigned long long sqnum) 8971e51764aSArtem Bityutskiy { 8981e51764aSArtem Bityutskiy struct ubifs_bud *bud; 8991e51764aSArtem Bityutskiy struct bud_entry *b; 900da8ef65fSSascha Hauer int err; 9011e51764aSArtem Bityutskiy 9021e51764aSArtem Bityutskiy dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead); 9031e51764aSArtem Bityutskiy 9041e51764aSArtem Bityutskiy bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL); 9051e51764aSArtem Bityutskiy if (!bud) 9061e51764aSArtem Bityutskiy return -ENOMEM; 9071e51764aSArtem Bityutskiy 9081e51764aSArtem Bityutskiy b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL); 9091e51764aSArtem Bityutskiy if (!b) { 910da8ef65fSSascha Hauer err = -ENOMEM; 911da8ef65fSSascha Hauer goto out; 9121e51764aSArtem Bityutskiy } 9131e51764aSArtem Bityutskiy 9141e51764aSArtem Bityutskiy bud->lnum = lnum; 9151e51764aSArtem Bityutskiy bud->start = offs; 9161e51764aSArtem Bityutskiy bud->jhead = jhead; 917da8ef65fSSascha Hauer bud->log_hash = ubifs_hash_get_desc(c); 918da8ef65fSSascha Hauer if (IS_ERR(bud->log_hash)) { 919da8ef65fSSascha Hauer err = PTR_ERR(bud->log_hash); 920da8ef65fSSascha Hauer goto out; 921da8ef65fSSascha Hauer } 922da8ef65fSSascha Hauer 923da8ef65fSSascha Hauer ubifs_shash_copy_state(c, c->log_hash, bud->log_hash); 924da8ef65fSSascha Hauer 9251e51764aSArtem Bityutskiy ubifs_add_bud(c, bud); 9261e51764aSArtem Bityutskiy 9271e51764aSArtem Bityutskiy b->bud = bud; 9281e51764aSArtem Bityutskiy b->sqnum = sqnum; 9291e51764aSArtem Bityutskiy list_add_tail(&b->list, &c->replay_buds); 9301e51764aSArtem Bityutskiy 9311e51764aSArtem Bityutskiy return 0; 932da8ef65fSSascha Hauer out: 933da8ef65fSSascha Hauer kfree(bud); 934da8ef65fSSascha Hauer kfree(b); 935da8ef65fSSascha Hauer 936da8ef65fSSascha Hauer return err; 9371e51764aSArtem Bityutskiy } 9381e51764aSArtem Bityutskiy 9391e51764aSArtem Bityutskiy /** 9401e51764aSArtem Bityutskiy * validate_ref - validate a reference node. 9411e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9421e51764aSArtem Bityutskiy * @ref: the reference node to validate 9431e51764aSArtem Bityutskiy * @ref_lnum: LEB number of the reference node 9441e51764aSArtem Bityutskiy * @ref_offs: reference node offset 9451e51764aSArtem Bityutskiy * 9461e51764aSArtem Bityutskiy * This function returns %1 if a bud reference already exists for the LEB. %0 is 9471e51764aSArtem Bityutskiy * returned if the reference node is new, otherwise %-EINVAL is returned if 9481e51764aSArtem Bityutskiy * validation failed. 9491e51764aSArtem Bityutskiy */ 9501e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref) 9511e51764aSArtem Bityutskiy { 9521e51764aSArtem Bityutskiy struct ubifs_bud *bud; 9531e51764aSArtem Bityutskiy int lnum = le32_to_cpu(ref->lnum); 9541e51764aSArtem Bityutskiy unsigned int offs = le32_to_cpu(ref->offs); 9551e51764aSArtem Bityutskiy unsigned int jhead = le32_to_cpu(ref->jhead); 9561e51764aSArtem Bityutskiy 9571e51764aSArtem Bityutskiy /* 9581e51764aSArtem Bityutskiy * ref->offs may point to the end of LEB when the journal head points 9591e51764aSArtem Bityutskiy * to the end of LEB and we write reference node for it during commit. 9601e51764aSArtem Bityutskiy * So this is why we require 'offs > c->leb_size'. 9611e51764aSArtem Bityutskiy */ 9621e51764aSArtem Bityutskiy if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt || 9631e51764aSArtem Bityutskiy lnum < c->main_first || offs > c->leb_size || 9641e51764aSArtem Bityutskiy offs & (c->min_io_size - 1)) 9651e51764aSArtem Bityutskiy return -EINVAL; 9661e51764aSArtem Bityutskiy 9671e51764aSArtem Bityutskiy /* Make sure we have not already looked at this bud */ 9681e51764aSArtem Bityutskiy bud = ubifs_search_bud(c, lnum); 9691e51764aSArtem Bityutskiy if (bud) { 9701e51764aSArtem Bityutskiy if (bud->jhead == jhead && bud->start <= offs) 9711e51764aSArtem Bityutskiy return 1; 972235c362bSSheng Yong ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs); 9731e51764aSArtem Bityutskiy return -EINVAL; 9741e51764aSArtem Bityutskiy } 9751e51764aSArtem Bityutskiy 9761e51764aSArtem Bityutskiy return 0; 9771e51764aSArtem Bityutskiy } 9781e51764aSArtem Bityutskiy 9791e51764aSArtem Bityutskiy /** 9801e51764aSArtem Bityutskiy * replay_log_leb - replay a log logical eraseblock. 9811e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9821e51764aSArtem Bityutskiy * @lnum: log logical eraseblock to replay 9831e51764aSArtem Bityutskiy * @offs: offset to start replaying from 9841e51764aSArtem Bityutskiy * @sbuf: scan buffer 9851e51764aSArtem Bityutskiy * 9861e51764aSArtem Bityutskiy * This function replays a log LEB and returns zero in case of success, %1 if 9871e51764aSArtem Bityutskiy * this is the last LEB in the log, and a negative error code in case of 9881e51764aSArtem Bityutskiy * failure. 9891e51764aSArtem Bityutskiy */ 9901e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf) 9911e51764aSArtem Bityutskiy { 9921e51764aSArtem Bityutskiy int err; 9931e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 9941e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 9951e51764aSArtem Bityutskiy const struct ubifs_cs_node *node; 9961e51764aSArtem Bityutskiy 9971e51764aSArtem Bityutskiy dbg_mnt("replay log LEB %d:%d", lnum, offs); 998348709baSArtem Bityutskiy sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery); 9991e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 1000ed43f2f0SArtem Bityutskiy if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery) 1001ed43f2f0SArtem Bityutskiy return PTR_ERR(sleb); 10027d08ae3cSArtem Bityutskiy /* 10037d08ae3cSArtem Bityutskiy * Note, the below function will recover this log LEB only if 10047d08ae3cSArtem Bityutskiy * it is the last, because unclean reboots can possibly corrupt 10057d08ae3cSArtem Bityutskiy * only the tail of the log. 10067d08ae3cSArtem Bityutskiy */ 10071e51764aSArtem Bityutskiy sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf); 10081e51764aSArtem Bityutskiy if (IS_ERR(sleb)) 10091e51764aSArtem Bityutskiy return PTR_ERR(sleb); 10101e51764aSArtem Bityutskiy } 10111e51764aSArtem Bityutskiy 10121e51764aSArtem Bityutskiy if (sleb->nodes_cnt == 0) { 10131e51764aSArtem Bityutskiy err = 1; 10141e51764aSArtem Bityutskiy goto out; 10151e51764aSArtem Bityutskiy } 10161e51764aSArtem Bityutskiy 10171e51764aSArtem Bityutskiy node = sleb->buf; 10181e51764aSArtem Bityutskiy snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list); 10191e51764aSArtem Bityutskiy if (c->cs_sqnum == 0) { 10201e51764aSArtem Bityutskiy /* 10211e51764aSArtem Bityutskiy * This is the first log LEB we are looking at, make sure that 10221e51764aSArtem Bityutskiy * the first node is a commit start node. Also record its 10231e51764aSArtem Bityutskiy * sequence number so that UBIFS can determine where the log 10241e51764aSArtem Bityutskiy * ends, because all nodes which were have higher sequence 10251e51764aSArtem Bityutskiy * numbers. 10261e51764aSArtem Bityutskiy */ 10271e51764aSArtem Bityutskiy if (snod->type != UBIFS_CS_NODE) { 1028235c362bSSheng Yong ubifs_err(c, "first log node at LEB %d:%d is not CS node", 10291e51764aSArtem Bityutskiy lnum, offs); 10301e51764aSArtem Bityutskiy goto out_dump; 10311e51764aSArtem Bityutskiy } 10321e51764aSArtem Bityutskiy if (le64_to_cpu(node->cmt_no) != c->cmt_no) { 1033235c362bSSheng Yong ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu", 10341e51764aSArtem Bityutskiy lnum, offs, 10351e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(node->cmt_no), 10361e51764aSArtem Bityutskiy c->cmt_no); 10371e51764aSArtem Bityutskiy goto out_dump; 10381e51764aSArtem Bityutskiy } 10391e51764aSArtem Bityutskiy 10401e51764aSArtem Bityutskiy c->cs_sqnum = le64_to_cpu(node->ch.sqnum); 10411e51764aSArtem Bityutskiy dbg_mnt("commit start sqnum %llu", c->cs_sqnum); 1042da8ef65fSSascha Hauer 1043da8ef65fSSascha Hauer err = ubifs_shash_init(c, c->log_hash); 1044da8ef65fSSascha Hauer if (err) 1045da8ef65fSSascha Hauer goto out; 1046da8ef65fSSascha Hauer 1047da8ef65fSSascha Hauer err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ); 1048da8ef65fSSascha Hauer if (err < 0) 1049da8ef65fSSascha Hauer goto out; 10501e51764aSArtem Bityutskiy } 10511e51764aSArtem Bityutskiy 10521e51764aSArtem Bityutskiy if (snod->sqnum < c->cs_sqnum) { 10531e51764aSArtem Bityutskiy /* 10541e51764aSArtem Bityutskiy * This means that we reached end of log and now 10551e51764aSArtem Bityutskiy * look to the older log data, which was already 10561e51764aSArtem Bityutskiy * committed but the eraseblock was not erased (UBIFS 10576edbfafdSArtem Bityutskiy * only un-maps it). So this basically means we have to 10581e51764aSArtem Bityutskiy * exit with "end of log" code. 10591e51764aSArtem Bityutskiy */ 10601e51764aSArtem Bityutskiy err = 1; 10611e51764aSArtem Bityutskiy goto out; 10621e51764aSArtem Bityutskiy } 10631e51764aSArtem Bityutskiy 10641e51764aSArtem Bityutskiy /* Make sure the first node sits at offset zero of the LEB */ 10651e51764aSArtem Bityutskiy if (snod->offs != 0) { 1066235c362bSSheng Yong ubifs_err(c, "first node is not at zero offset"); 10671e51764aSArtem Bityutskiy goto out_dump; 10681e51764aSArtem Bityutskiy } 10691e51764aSArtem Bityutskiy 10701e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 10711e51764aSArtem Bityutskiy cond_resched(); 10721e51764aSArtem Bityutskiy 10731e51764aSArtem Bityutskiy if (snod->sqnum >= SQNUM_WATERMARK) { 1074235c362bSSheng Yong ubifs_err(c, "file system's life ended"); 10751e51764aSArtem Bityutskiy goto out_dump; 10761e51764aSArtem Bityutskiy } 10771e51764aSArtem Bityutskiy 10781e51764aSArtem Bityutskiy if (snod->sqnum < c->cs_sqnum) { 1079235c362bSSheng Yong ubifs_err(c, "bad sqnum %llu, commit sqnum %llu", 10801e51764aSArtem Bityutskiy snod->sqnum, c->cs_sqnum); 10811e51764aSArtem Bityutskiy goto out_dump; 10821e51764aSArtem Bityutskiy } 10831e51764aSArtem Bityutskiy 10841e51764aSArtem Bityutskiy if (snod->sqnum > c->max_sqnum) 10851e51764aSArtem Bityutskiy c->max_sqnum = snod->sqnum; 10861e51764aSArtem Bityutskiy 10871e51764aSArtem Bityutskiy switch (snod->type) { 10881e51764aSArtem Bityutskiy case UBIFS_REF_NODE: { 10891e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = snod->node; 10901e51764aSArtem Bityutskiy 10911e51764aSArtem Bityutskiy err = validate_ref(c, ref); 10921e51764aSArtem Bityutskiy if (err == 1) 10931e51764aSArtem Bityutskiy break; /* Already have this bud */ 10941e51764aSArtem Bityutskiy if (err) 10951e51764aSArtem Bityutskiy goto out_dump; 10961e51764aSArtem Bityutskiy 1097da8ef65fSSascha Hauer err = ubifs_shash_update(c, c->log_hash, ref, 1098da8ef65fSSascha Hauer UBIFS_REF_NODE_SZ); 1099da8ef65fSSascha Hauer if (err) 1100da8ef65fSSascha Hauer goto out; 1101da8ef65fSSascha Hauer 11021e51764aSArtem Bityutskiy err = add_replay_bud(c, le32_to_cpu(ref->lnum), 11031e51764aSArtem Bityutskiy le32_to_cpu(ref->offs), 11041e51764aSArtem Bityutskiy le32_to_cpu(ref->jhead), 11051e51764aSArtem Bityutskiy snod->sqnum); 11061e51764aSArtem Bityutskiy if (err) 11071e51764aSArtem Bityutskiy goto out; 11081e51764aSArtem Bityutskiy 11091e51764aSArtem Bityutskiy break; 11101e51764aSArtem Bityutskiy } 11111e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 11121e51764aSArtem Bityutskiy /* Make sure it sits at the beginning of LEB */ 11131e51764aSArtem Bityutskiy if (snod->offs != 0) { 1114235c362bSSheng Yong ubifs_err(c, "unexpected node in log"); 11151e51764aSArtem Bityutskiy goto out_dump; 11161e51764aSArtem Bityutskiy } 11171e51764aSArtem Bityutskiy break; 11181e51764aSArtem Bityutskiy default: 1119235c362bSSheng Yong ubifs_err(c, "unexpected node in log"); 11201e51764aSArtem Bityutskiy goto out_dump; 11211e51764aSArtem Bityutskiy } 11221e51764aSArtem Bityutskiy } 11231e51764aSArtem Bityutskiy 11241e51764aSArtem Bityutskiy if (sleb->endpt || c->lhead_offs >= c->leb_size) { 11251e51764aSArtem Bityutskiy c->lhead_lnum = lnum; 11261e51764aSArtem Bityutskiy c->lhead_offs = sleb->endpt; 11271e51764aSArtem Bityutskiy } 11281e51764aSArtem Bityutskiy 11291e51764aSArtem Bityutskiy err = !sleb->endpt; 11301e51764aSArtem Bityutskiy out: 11311e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 11321e51764aSArtem Bityutskiy return err; 11331e51764aSArtem Bityutskiy 11341e51764aSArtem Bityutskiy out_dump: 1135235c362bSSheng Yong ubifs_err(c, "log error detected while replaying the log at LEB %d:%d", 11361e51764aSArtem Bityutskiy lnum, offs + snod->offs); 1137edf6be24SArtem Bityutskiy ubifs_dump_node(c, snod->node); 11381e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 11391e51764aSArtem Bityutskiy return -EINVAL; 11401e51764aSArtem Bityutskiy } 11411e51764aSArtem Bityutskiy 11421e51764aSArtem Bityutskiy /** 11431e51764aSArtem Bityutskiy * take_ihead - update the status of the index head in lprops to 'taken'. 11441e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11451e51764aSArtem Bityutskiy * 11461e51764aSArtem Bityutskiy * This function returns the amount of free space in the index head LEB or a 11471e51764aSArtem Bityutskiy * negative error code. 11481e51764aSArtem Bityutskiy */ 11491e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c) 11501e51764aSArtem Bityutskiy { 11511e51764aSArtem Bityutskiy const struct ubifs_lprops *lp; 11521e51764aSArtem Bityutskiy int err, free; 11531e51764aSArtem Bityutskiy 11541e51764aSArtem Bityutskiy ubifs_get_lprops(c); 11551e51764aSArtem Bityutskiy 11561e51764aSArtem Bityutskiy lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum); 11571e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 11581e51764aSArtem Bityutskiy err = PTR_ERR(lp); 11591e51764aSArtem Bityutskiy goto out; 11601e51764aSArtem Bityutskiy } 11611e51764aSArtem Bityutskiy 11621e51764aSArtem Bityutskiy free = lp->free; 11631e51764aSArtem Bityutskiy 11641e51764aSArtem Bityutskiy lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC, 11651e51764aSArtem Bityutskiy lp->flags | LPROPS_TAKEN, 0); 11661e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 11671e51764aSArtem Bityutskiy err = PTR_ERR(lp); 11681e51764aSArtem Bityutskiy goto out; 11691e51764aSArtem Bityutskiy } 11701e51764aSArtem Bityutskiy 11711e51764aSArtem Bityutskiy err = free; 11721e51764aSArtem Bityutskiy out: 11731e51764aSArtem Bityutskiy ubifs_release_lprops(c); 11741e51764aSArtem Bityutskiy return err; 11751e51764aSArtem Bityutskiy } 11761e51764aSArtem Bityutskiy 11771e51764aSArtem Bityutskiy /** 11781e51764aSArtem Bityutskiy * ubifs_replay_journal - replay journal. 11791e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11801e51764aSArtem Bityutskiy * 11811e51764aSArtem Bityutskiy * This function scans the journal, replays and cleans it up. It makes sure all 11821e51764aSArtem Bityutskiy * memory data structures related to uncommitted journal are built (dirty TNC 11831e51764aSArtem Bityutskiy * tree, tree of buds, modified lprops, etc). 11841e51764aSArtem Bityutskiy */ 11851e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c) 11861e51764aSArtem Bityutskiy { 1187d51f17eaSArtem Bityutskiy int err, lnum, free; 11881e51764aSArtem Bityutskiy 11891e51764aSArtem Bityutskiy BUILD_BUG_ON(UBIFS_TRUN_KEY > 5); 11901e51764aSArtem Bityutskiy 11911e51764aSArtem Bityutskiy /* Update the status of the index head in lprops to 'taken' */ 11921e51764aSArtem Bityutskiy free = take_ihead(c); 11931e51764aSArtem Bityutskiy if (free < 0) 11941e51764aSArtem Bityutskiy return free; /* Error code */ 11951e51764aSArtem Bityutskiy 11961e51764aSArtem Bityutskiy if (c->ihead_offs != c->leb_size - free) { 1197235c362bSSheng Yong ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum, 11981e51764aSArtem Bityutskiy c->ihead_offs); 11991e51764aSArtem Bityutskiy return -EINVAL; 12001e51764aSArtem Bityutskiy } 12011e51764aSArtem Bityutskiy 12021e51764aSArtem Bityutskiy dbg_mnt("start replaying the journal"); 12031e51764aSArtem Bityutskiy c->replaying = 1; 12041e51764aSArtem Bityutskiy lnum = c->ltail_lnum = c->lhead_lnum; 12051e51764aSArtem Bityutskiy 1206d51f17eaSArtem Bityutskiy do { 1207d51f17eaSArtem Bityutskiy err = replay_log_leb(c, lnum, 0, c->sbuf); 120888cff0f0Shujianyang if (err == 1) { 120988cff0f0Shujianyang if (lnum != c->lhead_lnum) 12101e51764aSArtem Bityutskiy /* We hit the end of the log */ 12111e51764aSArtem Bityutskiy break; 121288cff0f0Shujianyang 121388cff0f0Shujianyang /* 121488cff0f0Shujianyang * The head of the log must always start with the 121588cff0f0Shujianyang * "commit start" node on a properly formatted UBIFS. 121688cff0f0Shujianyang * But we found no nodes at all, which means that 1217c7e593b3SSascha Hauer * something went wrong and we cannot proceed mounting 121888cff0f0Shujianyang * the file-system. 121988cff0f0Shujianyang */ 1220235c362bSSheng Yong ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted", 122188cff0f0Shujianyang lnum, 0); 122288cff0f0Shujianyang err = -EINVAL; 122388cff0f0Shujianyang } 12241e51764aSArtem Bityutskiy if (err) 12251e51764aSArtem Bityutskiy goto out; 1226d51f17eaSArtem Bityutskiy lnum = ubifs_next_log_lnum(c, lnum); 1227c212f402SArtem Bityutskiy } while (lnum != c->ltail_lnum); 12281e51764aSArtem Bityutskiy 12291e51764aSArtem Bityutskiy err = replay_buds(c); 12301e51764aSArtem Bityutskiy if (err) 12311e51764aSArtem Bityutskiy goto out; 12321e51764aSArtem Bityutskiy 1233debf12d5SArtem Bityutskiy err = apply_replay_list(c); 12341e51764aSArtem Bityutskiy if (err) 12351e51764aSArtem Bityutskiy goto out; 12361e51764aSArtem Bityutskiy 1237074bcb9bSArtem Bityutskiy err = set_buds_lprops(c); 1238074bcb9bSArtem Bityutskiy if (err) 1239074bcb9bSArtem Bityutskiy goto out; 1240074bcb9bSArtem Bityutskiy 12416edbfafdSArtem Bityutskiy /* 1242b137545cSArtem Bityutskiy * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable 1243b137545cSArtem Bityutskiy * to roughly estimate index growth. Things like @c->bi.min_idx_lebs 12446edbfafdSArtem Bityutskiy * depend on it. This means we have to initialize it to make sure 12456edbfafdSArtem Bityutskiy * budgeting works properly. 12466edbfafdSArtem Bityutskiy */ 1247b137545cSArtem Bityutskiy c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt); 1248b137545cSArtem Bityutskiy c->bi.uncommitted_idx *= c->max_idx_node_sz; 12496edbfafdSArtem Bityutskiy 12506eb61d58SRichard Weinberger ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery); 125179fda517SArtem Bityutskiy dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu", 125279fda517SArtem Bityutskiy c->lhead_lnum, c->lhead_offs, c->max_sqnum, 1253e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 12541e51764aSArtem Bityutskiy out: 1255debf12d5SArtem Bityutskiy destroy_replay_list(c); 12561e51764aSArtem Bityutskiy destroy_bud_list(c); 12571e51764aSArtem Bityutskiy c->replaying = 0; 12581e51764aSArtem Bityutskiy return err; 12591e51764aSArtem Bityutskiy } 1260