11e51764aSArtem Bityutskiy /* 21e51764aSArtem Bityutskiy * This file is part of UBIFS. 31e51764aSArtem Bityutskiy * 41e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation. 51e51764aSArtem Bityutskiy * 61e51764aSArtem Bityutskiy * This program is free software; you can redistribute it and/or modify it 71e51764aSArtem Bityutskiy * under the terms of the GNU General Public License version 2 as published by 81e51764aSArtem Bityutskiy * the Free Software Foundation. 91e51764aSArtem Bityutskiy * 101e51764aSArtem Bityutskiy * This program is distributed in the hope that it will be useful, but WITHOUT 111e51764aSArtem Bityutskiy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 121e51764aSArtem Bityutskiy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 131e51764aSArtem Bityutskiy * more details. 141e51764aSArtem Bityutskiy * 151e51764aSArtem Bityutskiy * You should have received a copy of the GNU General Public License along with 161e51764aSArtem Bityutskiy * this program; if not, write to the Free Software Foundation, Inc., 51 171e51764aSArtem Bityutskiy * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 181e51764aSArtem Bityutskiy * 191e51764aSArtem Bityutskiy * Authors: Adrian Hunter 201e51764aSArtem Bityutskiy * Artem Bityutskiy (Битюцкий Артём) 211e51764aSArtem Bityutskiy */ 221e51764aSArtem Bityutskiy 231e51764aSArtem Bityutskiy /* 241e51764aSArtem Bityutskiy * This file contains journal replay code. It runs when the file-system is being 251e51764aSArtem Bityutskiy * mounted and requires no locking. 261e51764aSArtem Bityutskiy * 271e51764aSArtem Bityutskiy * The larger is the journal, the longer it takes to scan it, so the longer it 281e51764aSArtem Bityutskiy * takes to mount UBIFS. This is why the journal has limited size which may be 291e51764aSArtem Bityutskiy * changed depending on the system requirements. But a larger journal gives 301e51764aSArtem Bityutskiy * faster I/O speed because it writes the index less frequently. So this is a 311e51764aSArtem Bityutskiy * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the 321e51764aSArtem Bityutskiy * larger is the journal, the more memory its index may consume. 331e51764aSArtem Bityutskiy */ 341e51764aSArtem Bityutskiy 351e51764aSArtem Bityutskiy #include "ubifs.h" 36debf12d5SArtem Bityutskiy #include <linux/list_sort.h> 371e51764aSArtem Bityutskiy 381e51764aSArtem Bityutskiy /** 39debf12d5SArtem Bityutskiy * struct replay_entry - replay list entry. 401e51764aSArtem Bityutskiy * @lnum: logical eraseblock number of the node 411e51764aSArtem Bityutskiy * @offs: node offset 421e51764aSArtem Bityutskiy * @len: node length 43074bcb9bSArtem Bityutskiy * @deletion: non-zero if this entry corresponds to a node deletion 441e51764aSArtem Bityutskiy * @sqnum: node sequence number 45debf12d5SArtem Bityutskiy * @list: links the replay list 461e51764aSArtem Bityutskiy * @key: node key 471e51764aSArtem Bityutskiy * @nm: directory entry name 481e51764aSArtem Bityutskiy * @old_size: truncation old size 491e51764aSArtem Bityutskiy * @new_size: truncation new size 501e51764aSArtem Bityutskiy * 51debf12d5SArtem Bityutskiy * The replay process first scans all buds and builds the replay list, then 52debf12d5SArtem Bityutskiy * sorts the replay list in nodes sequence number order, and then inserts all 53debf12d5SArtem Bityutskiy * the replay entries to the TNC. 541e51764aSArtem Bityutskiy */ 551e51764aSArtem Bityutskiy struct replay_entry { 561e51764aSArtem Bityutskiy int lnum; 571e51764aSArtem Bityutskiy int offs; 581e51764aSArtem Bityutskiy int len; 59074bcb9bSArtem Bityutskiy unsigned int deletion:1; 601e51764aSArtem Bityutskiy unsigned long long sqnum; 61debf12d5SArtem Bityutskiy struct list_head list; 621e51764aSArtem Bityutskiy union ubifs_key key; 631e51764aSArtem Bityutskiy union { 641e51764aSArtem Bityutskiy struct qstr nm; 651e51764aSArtem Bityutskiy struct { 661e51764aSArtem Bityutskiy loff_t old_size; 671e51764aSArtem Bityutskiy loff_t new_size; 681e51764aSArtem Bityutskiy }; 691e51764aSArtem Bityutskiy }; 701e51764aSArtem Bityutskiy }; 711e51764aSArtem Bityutskiy 721e51764aSArtem Bityutskiy /** 731e51764aSArtem Bityutskiy * struct bud_entry - entry in the list of buds to replay. 741e51764aSArtem Bityutskiy * @list: next bud in the list 751e51764aSArtem Bityutskiy * @bud: bud description object 761e51764aSArtem Bityutskiy * @sqnum: reference node sequence number 77af1dd412SArtem Bityutskiy * @free: free bytes in the bud 78af1dd412SArtem Bityutskiy * @dirty: dirty bytes in the bud 791e51764aSArtem Bityutskiy */ 801e51764aSArtem Bityutskiy struct bud_entry { 811e51764aSArtem Bityutskiy struct list_head list; 821e51764aSArtem Bityutskiy struct ubifs_bud *bud; 831e51764aSArtem Bityutskiy unsigned long long sqnum; 84af1dd412SArtem Bityutskiy int free; 85af1dd412SArtem Bityutskiy int dirty; 861e51764aSArtem Bityutskiy }; 871e51764aSArtem Bityutskiy 881e51764aSArtem Bityutskiy /** 891e51764aSArtem Bityutskiy * set_bud_lprops - set free and dirty space used by a bud. 901e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 91074bcb9bSArtem Bityutskiy * @b: bud entry which describes the bud 92074bcb9bSArtem Bityutskiy * 93074bcb9bSArtem Bityutskiy * This function makes sure the LEB properties of bud @b are set correctly 94074bcb9bSArtem Bityutskiy * after the replay. Returns zero in case of success and a negative error code 95074bcb9bSArtem Bityutskiy * in case of failure. 961e51764aSArtem Bityutskiy */ 97074bcb9bSArtem Bityutskiy static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b) 981e51764aSArtem Bityutskiy { 991e51764aSArtem Bityutskiy const struct ubifs_lprops *lp; 1001e51764aSArtem Bityutskiy int err = 0, dirty; 1011e51764aSArtem Bityutskiy 1021e51764aSArtem Bityutskiy ubifs_get_lprops(c); 1031e51764aSArtem Bityutskiy 104074bcb9bSArtem Bityutskiy lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum); 1051e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 1061e51764aSArtem Bityutskiy err = PTR_ERR(lp); 1071e51764aSArtem Bityutskiy goto out; 1081e51764aSArtem Bityutskiy } 1091e51764aSArtem Bityutskiy 1101e51764aSArtem Bityutskiy dirty = lp->dirty; 111074bcb9bSArtem Bityutskiy if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) { 1121e51764aSArtem Bityutskiy /* 1131e51764aSArtem Bityutskiy * The LEB was added to the journal with a starting offset of 1141e51764aSArtem Bityutskiy * zero which means the LEB must have been empty. The LEB 115074bcb9bSArtem Bityutskiy * property values should be @lp->free == @c->leb_size and 116074bcb9bSArtem Bityutskiy * @lp->dirty == 0, but that is not the case. The reason is that 1177a9c3e39SArtem Bityutskiy * the LEB had been garbage collected before it became the bud, 1187a9c3e39SArtem Bityutskiy * and there was not commit inbetween. The garbage collector 1197a9c3e39SArtem Bityutskiy * resets the free and dirty space without recording it 1207a9c3e39SArtem Bityutskiy * anywhere except lprops, so if there was no commit then 1217a9c3e39SArtem Bityutskiy * lprops does not have that information. 1221e51764aSArtem Bityutskiy * 1231e51764aSArtem Bityutskiy * We do not need to adjust free space because the scan has told 1241e51764aSArtem Bityutskiy * us the exact value which is recorded in the replay entry as 125074bcb9bSArtem Bityutskiy * @b->free. 1261e51764aSArtem Bityutskiy * 1271e51764aSArtem Bityutskiy * However we do need to subtract from the dirty space the 1281e51764aSArtem Bityutskiy * amount of space that the garbage collector reclaimed, which 1291e51764aSArtem Bityutskiy * is the whole LEB minus the amount of space that was free. 1301e51764aSArtem Bityutskiy */ 131074bcb9bSArtem Bityutskiy dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum, 1321e51764aSArtem Bityutskiy lp->free, lp->dirty); 133074bcb9bSArtem Bityutskiy dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum, 1341e51764aSArtem Bityutskiy lp->free, lp->dirty); 1351e51764aSArtem Bityutskiy dirty -= c->leb_size - lp->free; 1361e51764aSArtem Bityutskiy /* 1371e51764aSArtem Bityutskiy * If the replay order was perfect the dirty space would now be 1387d4e9ccbSArtem Bityutskiy * zero. The order is not perfect because the journal heads 1391e51764aSArtem Bityutskiy * race with each other. This is not a problem but is does mean 1401e51764aSArtem Bityutskiy * that the dirty space may temporarily exceed c->leb_size 1411e51764aSArtem Bityutskiy * during the replay. 1421e51764aSArtem Bityutskiy */ 1431e51764aSArtem Bityutskiy if (dirty != 0) 1441e51764aSArtem Bityutskiy dbg_msg("LEB %d lp: %d free %d dirty " 145074bcb9bSArtem Bityutskiy "replay: %d free %d dirty", b->bud->lnum, 146074bcb9bSArtem Bityutskiy lp->free, lp->dirty, b->free, b->dirty); 1471e51764aSArtem Bityutskiy } 148074bcb9bSArtem Bityutskiy lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty, 1491e51764aSArtem Bityutskiy lp->flags | LPROPS_TAKEN, 0); 1501e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 1511e51764aSArtem Bityutskiy err = PTR_ERR(lp); 1521e51764aSArtem Bityutskiy goto out; 1531e51764aSArtem Bityutskiy } 15452c6e6f9SArtem Bityutskiy 15552c6e6f9SArtem Bityutskiy /* Make sure the journal head points to the latest bud */ 156074bcb9bSArtem Bityutskiy err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf, 157074bcb9bSArtem Bityutskiy b->bud->lnum, c->leb_size - b->free, 158074bcb9bSArtem Bityutskiy UBI_SHORTTERM); 15952c6e6f9SArtem Bityutskiy 1601e51764aSArtem Bityutskiy out: 1611e51764aSArtem Bityutskiy ubifs_release_lprops(c); 1621e51764aSArtem Bityutskiy return err; 1631e51764aSArtem Bityutskiy } 1641e51764aSArtem Bityutskiy 1651e51764aSArtem Bityutskiy /** 166074bcb9bSArtem Bityutskiy * set_buds_lprops - set free and dirty space for all replayed buds. 167074bcb9bSArtem Bityutskiy * @c: UBIFS file-system description object 168074bcb9bSArtem Bityutskiy * 169074bcb9bSArtem Bityutskiy * This function sets LEB properties for all replayed buds. Returns zero in 170074bcb9bSArtem Bityutskiy * case of success and a negative error code in case of failure. 171074bcb9bSArtem Bityutskiy */ 172074bcb9bSArtem Bityutskiy static int set_buds_lprops(struct ubifs_info *c) 173074bcb9bSArtem Bityutskiy { 174074bcb9bSArtem Bityutskiy struct bud_entry *b; 175074bcb9bSArtem Bityutskiy int err; 176074bcb9bSArtem Bityutskiy 177074bcb9bSArtem Bityutskiy list_for_each_entry(b, &c->replay_buds, list) { 178074bcb9bSArtem Bityutskiy err = set_bud_lprops(c, b); 179074bcb9bSArtem Bityutskiy if (err) 180074bcb9bSArtem Bityutskiy return err; 181074bcb9bSArtem Bityutskiy } 182074bcb9bSArtem Bityutskiy 183074bcb9bSArtem Bityutskiy return 0; 184074bcb9bSArtem Bityutskiy } 185074bcb9bSArtem Bityutskiy 186074bcb9bSArtem Bityutskiy /** 1871e51764aSArtem Bityutskiy * trun_remove_range - apply a replay entry for a truncation to the TNC. 1881e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 1891e51764aSArtem Bityutskiy * @r: replay entry of truncation 1901e51764aSArtem Bityutskiy */ 1911e51764aSArtem Bityutskiy static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r) 1921e51764aSArtem Bityutskiy { 1931e51764aSArtem Bityutskiy unsigned min_blk, max_blk; 1941e51764aSArtem Bityutskiy union ubifs_key min_key, max_key; 1951e51764aSArtem Bityutskiy ino_t ino; 1961e51764aSArtem Bityutskiy 1971e51764aSArtem Bityutskiy min_blk = r->new_size / UBIFS_BLOCK_SIZE; 1981e51764aSArtem Bityutskiy if (r->new_size & (UBIFS_BLOCK_SIZE - 1)) 1991e51764aSArtem Bityutskiy min_blk += 1; 2001e51764aSArtem Bityutskiy 2011e51764aSArtem Bityutskiy max_blk = r->old_size / UBIFS_BLOCK_SIZE; 2021e51764aSArtem Bityutskiy if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0) 2031e51764aSArtem Bityutskiy max_blk -= 1; 2041e51764aSArtem Bityutskiy 2051e51764aSArtem Bityutskiy ino = key_inum(c, &r->key); 2061e51764aSArtem Bityutskiy 2071e51764aSArtem Bityutskiy data_key_init(c, &min_key, ino, min_blk); 2081e51764aSArtem Bityutskiy data_key_init(c, &max_key, ino, max_blk); 2091e51764aSArtem Bityutskiy 2101e51764aSArtem Bityutskiy return ubifs_tnc_remove_range(c, &min_key, &max_key); 2111e51764aSArtem Bityutskiy } 2121e51764aSArtem Bityutskiy 2131e51764aSArtem Bityutskiy /** 2141e51764aSArtem Bityutskiy * apply_replay_entry - apply a replay entry to the TNC. 2151e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2161e51764aSArtem Bityutskiy * @r: replay entry to apply 2171e51764aSArtem Bityutskiy * 2181e51764aSArtem Bityutskiy * Apply a replay entry to the TNC. 2191e51764aSArtem Bityutskiy */ 2201e51764aSArtem Bityutskiy static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r) 2211e51764aSArtem Bityutskiy { 222074bcb9bSArtem Bityutskiy int err; 2231e51764aSArtem Bityutskiy 224074bcb9bSArtem Bityutskiy dbg_mnt("LEB %d:%d len %d deletion %d sqnum %llu %s", r->lnum, 225074bcb9bSArtem Bityutskiy r->offs, r->len, r->deletion, r->sqnum, DBGKEY(&r->key)); 2261e51764aSArtem Bityutskiy 2271e51764aSArtem Bityutskiy /* Set c->replay_sqnum to help deal with dangling branches. */ 2281e51764aSArtem Bityutskiy c->replay_sqnum = r->sqnum; 2291e51764aSArtem Bityutskiy 230074bcb9bSArtem Bityutskiy if (is_hash_key(c, &r->key)) { 231074bcb9bSArtem Bityutskiy if (r->deletion) 2321e51764aSArtem Bityutskiy err = ubifs_tnc_remove_nm(c, &r->key, &r->nm); 2331e51764aSArtem Bityutskiy else 2341e51764aSArtem Bityutskiy err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs, 2351e51764aSArtem Bityutskiy r->len, &r->nm); 2361e51764aSArtem Bityutskiy } else { 237074bcb9bSArtem Bityutskiy if (r->deletion) 2381e51764aSArtem Bityutskiy switch (key_type(c, &r->key)) { 2391e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 2401e51764aSArtem Bityutskiy { 2411e51764aSArtem Bityutskiy ino_t inum = key_inum(c, &r->key); 2421e51764aSArtem Bityutskiy 2431e51764aSArtem Bityutskiy err = ubifs_tnc_remove_ino(c, inum); 2441e51764aSArtem Bityutskiy break; 2451e51764aSArtem Bityutskiy } 2461e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 2471e51764aSArtem Bityutskiy err = trun_remove_range(c, r); 2481e51764aSArtem Bityutskiy break; 2491e51764aSArtem Bityutskiy default: 2501e51764aSArtem Bityutskiy err = ubifs_tnc_remove(c, &r->key); 2511e51764aSArtem Bityutskiy break; 2521e51764aSArtem Bityutskiy } 2531e51764aSArtem Bityutskiy else 2541e51764aSArtem Bityutskiy err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs, 2551e51764aSArtem Bityutskiy r->len); 2561e51764aSArtem Bityutskiy if (err) 2571e51764aSArtem Bityutskiy return err; 2581e51764aSArtem Bityutskiy 2591e51764aSArtem Bityutskiy if (c->need_recovery) 260074bcb9bSArtem Bityutskiy err = ubifs_recover_size_accum(c, &r->key, r->deletion, 2611e51764aSArtem Bityutskiy r->new_size); 2621e51764aSArtem Bityutskiy } 2631e51764aSArtem Bityutskiy 2641e51764aSArtem Bityutskiy return err; 2651e51764aSArtem Bityutskiy } 2661e51764aSArtem Bityutskiy 2671e51764aSArtem Bityutskiy /** 268debf12d5SArtem Bityutskiy * replay_entries_cmp - compare 2 replay entries. 269debf12d5SArtem Bityutskiy * @priv: UBIFS file-system description object 270debf12d5SArtem Bityutskiy * @a: first replay entry 271debf12d5SArtem Bityutskiy * @a: second replay entry 2721e51764aSArtem Bityutskiy * 273debf12d5SArtem Bityutskiy * This is a comparios function for 'list_sort()' which compares 2 replay 274debf12d5SArtem Bityutskiy * entries @a and @b by comparing their sequence numer. Returns %1 if @a has 275debf12d5SArtem Bityutskiy * greater sequence number and %-1 otherwise. 2761e51764aSArtem Bityutskiy */ 277debf12d5SArtem Bityutskiy static int replay_entries_cmp(void *priv, struct list_head *a, 278debf12d5SArtem Bityutskiy struct list_head *b) 2791e51764aSArtem Bityutskiy { 280debf12d5SArtem Bityutskiy struct replay_entry *ra, *rb; 2811e51764aSArtem Bityutskiy 282debf12d5SArtem Bityutskiy cond_resched(); 283debf12d5SArtem Bityutskiy if (a == b) 284debf12d5SArtem Bityutskiy return 0; 285debf12d5SArtem Bityutskiy 286debf12d5SArtem Bityutskiy ra = list_entry(a, struct replay_entry, list); 287debf12d5SArtem Bityutskiy rb = list_entry(b, struct replay_entry, list); 288debf12d5SArtem Bityutskiy ubifs_assert(ra->sqnum != rb->sqnum); 289debf12d5SArtem Bityutskiy if (ra->sqnum > rb->sqnum) 290debf12d5SArtem Bityutskiy return 1; 291debf12d5SArtem Bityutskiy return -1; 2921e51764aSArtem Bityutskiy } 2931e51764aSArtem Bityutskiy 2941e51764aSArtem Bityutskiy /** 295debf12d5SArtem Bityutskiy * apply_replay_list - apply the replay list to the TNC. 2961e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2971e51764aSArtem Bityutskiy * 298debf12d5SArtem Bityutskiy * Apply all entries in the replay list to the TNC. Returns zero in case of 299debf12d5SArtem Bityutskiy * success and a negative error code in case of failure. 3001e51764aSArtem Bityutskiy */ 301debf12d5SArtem Bityutskiy static int apply_replay_list(struct ubifs_info *c) 3021e51764aSArtem Bityutskiy { 3031e51764aSArtem Bityutskiy struct replay_entry *r; 3041e51764aSArtem Bityutskiy int err; 3051e51764aSArtem Bityutskiy 306debf12d5SArtem Bityutskiy list_sort(c, &c->replay_list, &replay_entries_cmp); 307debf12d5SArtem Bityutskiy 308debf12d5SArtem Bityutskiy list_for_each_entry(r, &c->replay_list, list) { 3091e51764aSArtem Bityutskiy cond_resched(); 3101e51764aSArtem Bityutskiy 3111e51764aSArtem Bityutskiy err = apply_replay_entry(c, r); 3121e51764aSArtem Bityutskiy if (err) 3131e51764aSArtem Bityutskiy return err; 3141e51764aSArtem Bityutskiy } 315debf12d5SArtem Bityutskiy 3161e51764aSArtem Bityutskiy return 0; 3171e51764aSArtem Bityutskiy } 3181e51764aSArtem Bityutskiy 3191e51764aSArtem Bityutskiy /** 320debf12d5SArtem Bityutskiy * destroy_replay_list - destroy the replay. 321debf12d5SArtem Bityutskiy * @c: UBIFS file-system description object 322debf12d5SArtem Bityutskiy * 323debf12d5SArtem Bityutskiy * Destroy the replay list. 324debf12d5SArtem Bityutskiy */ 325debf12d5SArtem Bityutskiy static void destroy_replay_list(struct ubifs_info *c) 326debf12d5SArtem Bityutskiy { 327debf12d5SArtem Bityutskiy struct replay_entry *r, *tmp; 328debf12d5SArtem Bityutskiy 329debf12d5SArtem Bityutskiy list_for_each_entry_safe(r, tmp, &c->replay_list, list) { 330debf12d5SArtem Bityutskiy if (is_hash_key(c, &r->key)) 331debf12d5SArtem Bityutskiy kfree(r->nm.name); 332debf12d5SArtem Bityutskiy list_del(&r->list); 333debf12d5SArtem Bityutskiy kfree(r); 334debf12d5SArtem Bityutskiy } 335debf12d5SArtem Bityutskiy } 336debf12d5SArtem Bityutskiy 337debf12d5SArtem Bityutskiy /** 338debf12d5SArtem Bityutskiy * insert_node - insert a node to the replay list 3391e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3401e51764aSArtem Bityutskiy * @lnum: node logical eraseblock number 3411e51764aSArtem Bityutskiy * @offs: node offset 3421e51764aSArtem Bityutskiy * @len: node length 3431e51764aSArtem Bityutskiy * @key: node key 3441e51764aSArtem Bityutskiy * @sqnum: sequence number 3451e51764aSArtem Bityutskiy * @deletion: non-zero if this is a deletion 3461e51764aSArtem Bityutskiy * @used: number of bytes in use in a LEB 3471e51764aSArtem Bityutskiy * @old_size: truncation old size 3481e51764aSArtem Bityutskiy * @new_size: truncation new size 3491e51764aSArtem Bityutskiy * 350debf12d5SArtem Bityutskiy * This function inserts a scanned non-direntry node to the replay list. The 351debf12d5SArtem Bityutskiy * replay list contains @struct replay_entry elements, and we sort this list in 352debf12d5SArtem Bityutskiy * sequence number order before applying it. The replay list is applied at the 353debf12d5SArtem Bityutskiy * very end of the replay process. Since the list is sorted in sequence number 354debf12d5SArtem Bityutskiy * order, the older modifications are applied first. This function returns zero 355debf12d5SArtem Bityutskiy * in case of success and a negative error code in case of failure. 3561e51764aSArtem Bityutskiy */ 3571e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len, 3581e51764aSArtem Bityutskiy union ubifs_key *key, unsigned long long sqnum, 3591e51764aSArtem Bityutskiy int deletion, int *used, loff_t old_size, 3601e51764aSArtem Bityutskiy loff_t new_size) 3611e51764aSArtem Bityutskiy { 3621e51764aSArtem Bityutskiy struct replay_entry *r; 3631e51764aSArtem Bityutskiy 364debf12d5SArtem Bityutskiy dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key)); 365debf12d5SArtem Bityutskiy 3661e51764aSArtem Bityutskiy if (key_inum(c, key) >= c->highest_inum) 3671e51764aSArtem Bityutskiy c->highest_inum = key_inum(c, key); 3681e51764aSArtem Bityutskiy 3691e51764aSArtem Bityutskiy r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL); 3701e51764aSArtem Bityutskiy if (!r) 3711e51764aSArtem Bityutskiy return -ENOMEM; 3721e51764aSArtem Bityutskiy 3731e51764aSArtem Bityutskiy if (!deletion) 3741e51764aSArtem Bityutskiy *used += ALIGN(len, 8); 3751e51764aSArtem Bityutskiy r->lnum = lnum; 3761e51764aSArtem Bityutskiy r->offs = offs; 3771e51764aSArtem Bityutskiy r->len = len; 378074bcb9bSArtem Bityutskiy r->deletion = !!deletion; 3791e51764aSArtem Bityutskiy r->sqnum = sqnum; 380074bcb9bSArtem Bityutskiy key_copy(c, key, &r->key); 3811e51764aSArtem Bityutskiy r->old_size = old_size; 3821e51764aSArtem Bityutskiy r->new_size = new_size; 3831e51764aSArtem Bityutskiy 384debf12d5SArtem Bityutskiy list_add_tail(&r->list, &c->replay_list); 3851e51764aSArtem Bityutskiy return 0; 3861e51764aSArtem Bityutskiy } 3871e51764aSArtem Bityutskiy 3881e51764aSArtem Bityutskiy /** 389debf12d5SArtem Bityutskiy * insert_dent - insert a directory entry node into the replay list. 3901e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3911e51764aSArtem Bityutskiy * @lnum: node logical eraseblock number 3921e51764aSArtem Bityutskiy * @offs: node offset 3931e51764aSArtem Bityutskiy * @len: node length 3941e51764aSArtem Bityutskiy * @key: node key 3951e51764aSArtem Bityutskiy * @name: directory entry name 3961e51764aSArtem Bityutskiy * @nlen: directory entry name length 3971e51764aSArtem Bityutskiy * @sqnum: sequence number 3981e51764aSArtem Bityutskiy * @deletion: non-zero if this is a deletion 3991e51764aSArtem Bityutskiy * @used: number of bytes in use in a LEB 4001e51764aSArtem Bityutskiy * 401debf12d5SArtem Bityutskiy * This function inserts a scanned directory entry node or an extended 402debf12d5SArtem Bityutskiy * attribute entry to the replay list. Returns zero in case of success and a 403debf12d5SArtem Bityutskiy * negative error code in case of failure. 4041e51764aSArtem Bityutskiy */ 4051e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len, 4061e51764aSArtem Bityutskiy union ubifs_key *key, const char *name, int nlen, 4071e51764aSArtem Bityutskiy unsigned long long sqnum, int deletion, int *used) 4081e51764aSArtem Bityutskiy { 4091e51764aSArtem Bityutskiy struct replay_entry *r; 4101e51764aSArtem Bityutskiy char *nbuf; 4111e51764aSArtem Bityutskiy 412debf12d5SArtem Bityutskiy dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key)); 4131e51764aSArtem Bityutskiy if (key_inum(c, key) >= c->highest_inum) 4141e51764aSArtem Bityutskiy c->highest_inum = key_inum(c, key); 4151e51764aSArtem Bityutskiy 4161e51764aSArtem Bityutskiy r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL); 4171e51764aSArtem Bityutskiy if (!r) 4181e51764aSArtem Bityutskiy return -ENOMEM; 419debf12d5SArtem Bityutskiy 4201e51764aSArtem Bityutskiy nbuf = kmalloc(nlen + 1, GFP_KERNEL); 4211e51764aSArtem Bityutskiy if (!nbuf) { 4221e51764aSArtem Bityutskiy kfree(r); 4231e51764aSArtem Bityutskiy return -ENOMEM; 4241e51764aSArtem Bityutskiy } 4251e51764aSArtem Bityutskiy 4261e51764aSArtem Bityutskiy if (!deletion) 4271e51764aSArtem Bityutskiy *used += ALIGN(len, 8); 4281e51764aSArtem Bityutskiy r->lnum = lnum; 4291e51764aSArtem Bityutskiy r->offs = offs; 4301e51764aSArtem Bityutskiy r->len = len; 431074bcb9bSArtem Bityutskiy r->deletion = !!deletion; 4321e51764aSArtem Bityutskiy r->sqnum = sqnum; 433074bcb9bSArtem Bityutskiy key_copy(c, key, &r->key); 4341e51764aSArtem Bityutskiy r->nm.len = nlen; 4351e51764aSArtem Bityutskiy memcpy(nbuf, name, nlen); 4361e51764aSArtem Bityutskiy nbuf[nlen] = '\0'; 4371e51764aSArtem Bityutskiy r->nm.name = nbuf; 4381e51764aSArtem Bityutskiy 439debf12d5SArtem Bityutskiy list_add_tail(&r->list, &c->replay_list); 4401e51764aSArtem Bityutskiy return 0; 4411e51764aSArtem Bityutskiy } 4421e51764aSArtem Bityutskiy 4431e51764aSArtem Bityutskiy /** 4441e51764aSArtem Bityutskiy * ubifs_validate_entry - validate directory or extended attribute entry node. 4451e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4461e51764aSArtem Bityutskiy * @dent: the node to validate 4471e51764aSArtem Bityutskiy * 4481e51764aSArtem Bityutskiy * This function validates directory or extended attribute entry node @dent. 4491e51764aSArtem Bityutskiy * Returns zero if the node is all right and a %-EINVAL if not. 4501e51764aSArtem Bityutskiy */ 4511e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c, 4521e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent) 4531e51764aSArtem Bityutskiy { 4541e51764aSArtem Bityutskiy int key_type = key_type_flash(c, dent->key); 4551e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 4561e51764aSArtem Bityutskiy 4571e51764aSArtem Bityutskiy if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 || 4581e51764aSArtem Bityutskiy dent->type >= UBIFS_ITYPES_CNT || 4591e51764aSArtem Bityutskiy nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 || 4601e51764aSArtem Bityutskiy strnlen(dent->name, nlen) != nlen || 4611e51764aSArtem Bityutskiy le64_to_cpu(dent->inum) > MAX_INUM) { 4621e51764aSArtem Bityutskiy ubifs_err("bad %s node", key_type == UBIFS_DENT_KEY ? 4631e51764aSArtem Bityutskiy "directory entry" : "extended attribute entry"); 4641e51764aSArtem Bityutskiy return -EINVAL; 4651e51764aSArtem Bityutskiy } 4661e51764aSArtem Bityutskiy 4671e51764aSArtem Bityutskiy if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) { 4681e51764aSArtem Bityutskiy ubifs_err("bad key type %d", key_type); 4691e51764aSArtem Bityutskiy return -EINVAL; 4701e51764aSArtem Bityutskiy } 4711e51764aSArtem Bityutskiy 4721e51764aSArtem Bityutskiy return 0; 4731e51764aSArtem Bityutskiy } 4741e51764aSArtem Bityutskiy 4751e51764aSArtem Bityutskiy /** 4761e51764aSArtem Bityutskiy * replay_bud - replay a bud logical eraseblock. 4771e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 478e76a4526SArtem Bityutskiy * @b: bud entry which describes the bud 4791e51764aSArtem Bityutskiy * 480e76a4526SArtem Bityutskiy * This function replays bud @bud, recovers it if needed, and adds all nodes 481e76a4526SArtem Bityutskiy * from this bud to the replay list. Returns zero in case of success and a 482e76a4526SArtem Bityutskiy * negative error code in case of failure. 4831e51764aSArtem Bityutskiy */ 484e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b) 4851e51764aSArtem Bityutskiy { 486e76a4526SArtem Bityutskiy int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start; 487e76a4526SArtem Bityutskiy int jhead = b->bud->jhead; 4881e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 4891e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 4901e51764aSArtem Bityutskiy 491c839e297SArtem Bityutskiy dbg_mnt("replay bud LEB %d, head %d, offs %d", lnum, jhead, offs); 492e76a4526SArtem Bityutskiy 4931e51764aSArtem Bityutskiy if (c->need_recovery) 4941e51764aSArtem Bityutskiy sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, jhead != GCHD); 4951e51764aSArtem Bityutskiy else 496348709baSArtem Bityutskiy sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0); 4971e51764aSArtem Bityutskiy if (IS_ERR(sleb)) 4981e51764aSArtem Bityutskiy return PTR_ERR(sleb); 4991e51764aSArtem Bityutskiy 5001e51764aSArtem Bityutskiy /* 5011e51764aSArtem Bityutskiy * The bud does not have to start from offset zero - the beginning of 5021e51764aSArtem Bityutskiy * the 'lnum' LEB may contain previously committed data. One of the 5031e51764aSArtem Bityutskiy * things we have to do in replay is to correctly update lprops with 5041e51764aSArtem Bityutskiy * newer information about this LEB. 5051e51764aSArtem Bityutskiy * 5061e51764aSArtem Bityutskiy * At this point lprops thinks that this LEB has 'c->leb_size - offs' 5071e51764aSArtem Bityutskiy * bytes of free space because it only contain information about 5081e51764aSArtem Bityutskiy * committed data. 5091e51764aSArtem Bityutskiy * 5101e51764aSArtem Bityutskiy * But we know that real amount of free space is 'c->leb_size - 5111e51764aSArtem Bityutskiy * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and 5121e51764aSArtem Bityutskiy * 'sleb->endpt' is used by bud data. We have to correctly calculate 5131e51764aSArtem Bityutskiy * how much of these data are dirty and update lprops with this 5141e51764aSArtem Bityutskiy * information. 5151e51764aSArtem Bityutskiy * 5161e51764aSArtem Bityutskiy * The dirt in that LEB region is comprised of padding nodes, deletion 5171e51764aSArtem Bityutskiy * nodes, truncation nodes and nodes which are obsoleted by subsequent 5181e51764aSArtem Bityutskiy * nodes in this LEB. So instead of calculating clean space, we 5191e51764aSArtem Bityutskiy * calculate used space ('used' variable). 5201e51764aSArtem Bityutskiy */ 5211e51764aSArtem Bityutskiy 5221e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 5231e51764aSArtem Bityutskiy int deletion = 0; 5241e51764aSArtem Bityutskiy 5251e51764aSArtem Bityutskiy cond_resched(); 5261e51764aSArtem Bityutskiy 5271e51764aSArtem Bityutskiy if (snod->sqnum >= SQNUM_WATERMARK) { 5281e51764aSArtem Bityutskiy ubifs_err("file system's life ended"); 5291e51764aSArtem Bityutskiy goto out_dump; 5301e51764aSArtem Bityutskiy } 5311e51764aSArtem Bityutskiy 5321e51764aSArtem Bityutskiy if (snod->sqnum > c->max_sqnum) 5331e51764aSArtem Bityutskiy c->max_sqnum = snod->sqnum; 5341e51764aSArtem Bityutskiy 5351e51764aSArtem Bityutskiy switch (snod->type) { 5361e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 5371e51764aSArtem Bityutskiy { 5381e51764aSArtem Bityutskiy struct ubifs_ino_node *ino = snod->node; 5391e51764aSArtem Bityutskiy loff_t new_size = le64_to_cpu(ino->size); 5401e51764aSArtem Bityutskiy 5411e51764aSArtem Bityutskiy if (le32_to_cpu(ino->nlink) == 0) 5421e51764aSArtem Bityutskiy deletion = 1; 5431e51764aSArtem Bityutskiy err = insert_node(c, lnum, snod->offs, snod->len, 5441e51764aSArtem Bityutskiy &snod->key, snod->sqnum, deletion, 5451e51764aSArtem Bityutskiy &used, 0, new_size); 5461e51764aSArtem Bityutskiy break; 5471e51764aSArtem Bityutskiy } 5481e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 5491e51764aSArtem Bityutskiy { 5501e51764aSArtem Bityutskiy struct ubifs_data_node *dn = snod->node; 5511e51764aSArtem Bityutskiy loff_t new_size = le32_to_cpu(dn->size) + 5521e51764aSArtem Bityutskiy key_block(c, &snod->key) * 5531e51764aSArtem Bityutskiy UBIFS_BLOCK_SIZE; 5541e51764aSArtem Bityutskiy 5551e51764aSArtem Bityutskiy err = insert_node(c, lnum, snod->offs, snod->len, 5561e51764aSArtem Bityutskiy &snod->key, snod->sqnum, deletion, 5571e51764aSArtem Bityutskiy &used, 0, new_size); 5581e51764aSArtem Bityutskiy break; 5591e51764aSArtem Bityutskiy } 5601e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 5611e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 5621e51764aSArtem Bityutskiy { 5631e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = snod->node; 5641e51764aSArtem Bityutskiy 5651e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 5661e51764aSArtem Bityutskiy if (err) 5671e51764aSArtem Bityutskiy goto out_dump; 5681e51764aSArtem Bityutskiy 5691e51764aSArtem Bityutskiy err = insert_dent(c, lnum, snod->offs, snod->len, 5701e51764aSArtem Bityutskiy &snod->key, dent->name, 5711e51764aSArtem Bityutskiy le16_to_cpu(dent->nlen), snod->sqnum, 5721e51764aSArtem Bityutskiy !le64_to_cpu(dent->inum), &used); 5731e51764aSArtem Bityutskiy break; 5741e51764aSArtem Bityutskiy } 5751e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 5761e51764aSArtem Bityutskiy { 5771e51764aSArtem Bityutskiy struct ubifs_trun_node *trun = snod->node; 5781e51764aSArtem Bityutskiy loff_t old_size = le64_to_cpu(trun->old_size); 5791e51764aSArtem Bityutskiy loff_t new_size = le64_to_cpu(trun->new_size); 5801e51764aSArtem Bityutskiy union ubifs_key key; 5811e51764aSArtem Bityutskiy 5821e51764aSArtem Bityutskiy /* Validate truncation node */ 5831e51764aSArtem Bityutskiy if (old_size < 0 || old_size > c->max_inode_sz || 5841e51764aSArtem Bityutskiy new_size < 0 || new_size > c->max_inode_sz || 5851e51764aSArtem Bityutskiy old_size <= new_size) { 5861e51764aSArtem Bityutskiy ubifs_err("bad truncation node"); 5871e51764aSArtem Bityutskiy goto out_dump; 5881e51764aSArtem Bityutskiy } 5891e51764aSArtem Bityutskiy 5901e51764aSArtem Bityutskiy /* 5911e51764aSArtem Bityutskiy * Create a fake truncation key just to use the same 5921e51764aSArtem Bityutskiy * functions which expect nodes to have keys. 5931e51764aSArtem Bityutskiy */ 5941e51764aSArtem Bityutskiy trun_key_init(c, &key, le32_to_cpu(trun->inum)); 5951e51764aSArtem Bityutskiy err = insert_node(c, lnum, snod->offs, snod->len, 5961e51764aSArtem Bityutskiy &key, snod->sqnum, 1, &used, 5971e51764aSArtem Bityutskiy old_size, new_size); 5981e51764aSArtem Bityutskiy break; 5991e51764aSArtem Bityutskiy } 6001e51764aSArtem Bityutskiy default: 6011e51764aSArtem Bityutskiy ubifs_err("unexpected node type %d in bud LEB %d:%d", 6021e51764aSArtem Bityutskiy snod->type, lnum, snod->offs); 6031e51764aSArtem Bityutskiy err = -EINVAL; 6041e51764aSArtem Bityutskiy goto out_dump; 6051e51764aSArtem Bityutskiy } 6061e51764aSArtem Bityutskiy if (err) 6071e51764aSArtem Bityutskiy goto out; 6081e51764aSArtem Bityutskiy } 6091e51764aSArtem Bityutskiy 610*c49139d8SArtem Bityutskiy ubifs_assert(ubifs_search_bud(c, lnum)); 6111e51764aSArtem Bityutskiy ubifs_assert(sleb->endpt - offs >= used); 6121e51764aSArtem Bityutskiy ubifs_assert(sleb->endpt % c->min_io_size == 0); 6131e51764aSArtem Bityutskiy 614e76a4526SArtem Bityutskiy b->dirty = sleb->endpt - offs - used; 615e76a4526SArtem Bityutskiy b->free = c->leb_size - sleb->endpt; 616e76a4526SArtem Bityutskiy dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum, b->dirty, b->free); 6171e51764aSArtem Bityutskiy 6181e51764aSArtem Bityutskiy out: 6191e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 6201e51764aSArtem Bityutskiy return err; 6211e51764aSArtem Bityutskiy 6221e51764aSArtem Bityutskiy out_dump: 6231e51764aSArtem Bityutskiy ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs); 6241e51764aSArtem Bityutskiy dbg_dump_node(c, snod->node); 6251e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 6261e51764aSArtem Bityutskiy return -EINVAL; 6271e51764aSArtem Bityutskiy } 6281e51764aSArtem Bityutskiy 6291e51764aSArtem Bityutskiy /** 6301e51764aSArtem Bityutskiy * replay_buds - replay all buds. 6311e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 6321e51764aSArtem Bityutskiy * 6331e51764aSArtem Bityutskiy * This function returns zero in case of success and a negative error code in 6341e51764aSArtem Bityutskiy * case of failure. 6351e51764aSArtem Bityutskiy */ 6361e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c) 6371e51764aSArtem Bityutskiy { 6381e51764aSArtem Bityutskiy struct bud_entry *b; 639074bcb9bSArtem Bityutskiy int err; 6407703f09dSArtem Bityutskiy unsigned long long prev_sqnum = 0; 6411e51764aSArtem Bityutskiy 6421e51764aSArtem Bityutskiy list_for_each_entry(b, &c->replay_buds, list) { 643e76a4526SArtem Bityutskiy err = replay_bud(c, b); 6441e51764aSArtem Bityutskiy if (err) 6451e51764aSArtem Bityutskiy return err; 6467703f09dSArtem Bityutskiy 6477703f09dSArtem Bityutskiy ubifs_assert(b->sqnum > prev_sqnum); 6487703f09dSArtem Bityutskiy prev_sqnum = b->sqnum; 6491e51764aSArtem Bityutskiy } 6501e51764aSArtem Bityutskiy 6511e51764aSArtem Bityutskiy return 0; 6521e51764aSArtem Bityutskiy } 6531e51764aSArtem Bityutskiy 6541e51764aSArtem Bityutskiy /** 6551e51764aSArtem Bityutskiy * destroy_bud_list - destroy the list of buds to replay. 6561e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 6571e51764aSArtem Bityutskiy */ 6581e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c) 6591e51764aSArtem Bityutskiy { 6601e51764aSArtem Bityutskiy struct bud_entry *b; 6611e51764aSArtem Bityutskiy 6621e51764aSArtem Bityutskiy while (!list_empty(&c->replay_buds)) { 6631e51764aSArtem Bityutskiy b = list_entry(c->replay_buds.next, struct bud_entry, list); 6641e51764aSArtem Bityutskiy list_del(&b->list); 6651e51764aSArtem Bityutskiy kfree(b); 6661e51764aSArtem Bityutskiy } 6671e51764aSArtem Bityutskiy } 6681e51764aSArtem Bityutskiy 6691e51764aSArtem Bityutskiy /** 6701e51764aSArtem Bityutskiy * add_replay_bud - add a bud to the list of buds to replay. 6711e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 6721e51764aSArtem Bityutskiy * @lnum: bud logical eraseblock number to replay 6731e51764aSArtem Bityutskiy * @offs: bud start offset 6741e51764aSArtem Bityutskiy * @jhead: journal head to which this bud belongs 6751e51764aSArtem Bityutskiy * @sqnum: reference node sequence number 6761e51764aSArtem Bityutskiy * 6771e51764aSArtem Bityutskiy * This function returns zero in case of success and a negative error code in 6781e51764aSArtem Bityutskiy * case of failure. 6791e51764aSArtem Bityutskiy */ 6801e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead, 6811e51764aSArtem Bityutskiy unsigned long long sqnum) 6821e51764aSArtem Bityutskiy { 6831e51764aSArtem Bityutskiy struct ubifs_bud *bud; 6841e51764aSArtem Bityutskiy struct bud_entry *b; 6851e51764aSArtem Bityutskiy 6861e51764aSArtem Bityutskiy dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead); 6871e51764aSArtem Bityutskiy 6881e51764aSArtem Bityutskiy bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL); 6891e51764aSArtem Bityutskiy if (!bud) 6901e51764aSArtem Bityutskiy return -ENOMEM; 6911e51764aSArtem Bityutskiy 6921e51764aSArtem Bityutskiy b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL); 6931e51764aSArtem Bityutskiy if (!b) { 6941e51764aSArtem Bityutskiy kfree(bud); 6951e51764aSArtem Bityutskiy return -ENOMEM; 6961e51764aSArtem Bityutskiy } 6971e51764aSArtem Bityutskiy 6981e51764aSArtem Bityutskiy bud->lnum = lnum; 6991e51764aSArtem Bityutskiy bud->start = offs; 7001e51764aSArtem Bityutskiy bud->jhead = jhead; 7011e51764aSArtem Bityutskiy ubifs_add_bud(c, bud); 7021e51764aSArtem Bityutskiy 7031e51764aSArtem Bityutskiy b->bud = bud; 7041e51764aSArtem Bityutskiy b->sqnum = sqnum; 7051e51764aSArtem Bityutskiy list_add_tail(&b->list, &c->replay_buds); 7061e51764aSArtem Bityutskiy 7071e51764aSArtem Bityutskiy return 0; 7081e51764aSArtem Bityutskiy } 7091e51764aSArtem Bityutskiy 7101e51764aSArtem Bityutskiy /** 7111e51764aSArtem Bityutskiy * validate_ref - validate a reference node. 7121e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 7131e51764aSArtem Bityutskiy * @ref: the reference node to validate 7141e51764aSArtem Bityutskiy * @ref_lnum: LEB number of the reference node 7151e51764aSArtem Bityutskiy * @ref_offs: reference node offset 7161e51764aSArtem Bityutskiy * 7171e51764aSArtem Bityutskiy * This function returns %1 if a bud reference already exists for the LEB. %0 is 7181e51764aSArtem Bityutskiy * returned if the reference node is new, otherwise %-EINVAL is returned if 7191e51764aSArtem Bityutskiy * validation failed. 7201e51764aSArtem Bityutskiy */ 7211e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref) 7221e51764aSArtem Bityutskiy { 7231e51764aSArtem Bityutskiy struct ubifs_bud *bud; 7241e51764aSArtem Bityutskiy int lnum = le32_to_cpu(ref->lnum); 7251e51764aSArtem Bityutskiy unsigned int offs = le32_to_cpu(ref->offs); 7261e51764aSArtem Bityutskiy unsigned int jhead = le32_to_cpu(ref->jhead); 7271e51764aSArtem Bityutskiy 7281e51764aSArtem Bityutskiy /* 7291e51764aSArtem Bityutskiy * ref->offs may point to the end of LEB when the journal head points 7301e51764aSArtem Bityutskiy * to the end of LEB and we write reference node for it during commit. 7311e51764aSArtem Bityutskiy * So this is why we require 'offs > c->leb_size'. 7321e51764aSArtem Bityutskiy */ 7331e51764aSArtem Bityutskiy if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt || 7341e51764aSArtem Bityutskiy lnum < c->main_first || offs > c->leb_size || 7351e51764aSArtem Bityutskiy offs & (c->min_io_size - 1)) 7361e51764aSArtem Bityutskiy return -EINVAL; 7371e51764aSArtem Bityutskiy 7381e51764aSArtem Bityutskiy /* Make sure we have not already looked at this bud */ 7391e51764aSArtem Bityutskiy bud = ubifs_search_bud(c, lnum); 7401e51764aSArtem Bityutskiy if (bud) { 7411e51764aSArtem Bityutskiy if (bud->jhead == jhead && bud->start <= offs) 7421e51764aSArtem Bityutskiy return 1; 7431e51764aSArtem Bityutskiy ubifs_err("bud at LEB %d:%d was already referred", lnum, offs); 7441e51764aSArtem Bityutskiy return -EINVAL; 7451e51764aSArtem Bityutskiy } 7461e51764aSArtem Bityutskiy 7471e51764aSArtem Bityutskiy return 0; 7481e51764aSArtem Bityutskiy } 7491e51764aSArtem Bityutskiy 7501e51764aSArtem Bityutskiy /** 7511e51764aSArtem Bityutskiy * replay_log_leb - replay a log logical eraseblock. 7521e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 7531e51764aSArtem Bityutskiy * @lnum: log logical eraseblock to replay 7541e51764aSArtem Bityutskiy * @offs: offset to start replaying from 7551e51764aSArtem Bityutskiy * @sbuf: scan buffer 7561e51764aSArtem Bityutskiy * 7571e51764aSArtem Bityutskiy * This function replays a log LEB and returns zero in case of success, %1 if 7581e51764aSArtem Bityutskiy * this is the last LEB in the log, and a negative error code in case of 7591e51764aSArtem Bityutskiy * failure. 7601e51764aSArtem Bityutskiy */ 7611e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf) 7621e51764aSArtem Bityutskiy { 7631e51764aSArtem Bityutskiy int err; 7641e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 7651e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 7661e51764aSArtem Bityutskiy const struct ubifs_cs_node *node; 7671e51764aSArtem Bityutskiy 7681e51764aSArtem Bityutskiy dbg_mnt("replay log LEB %d:%d", lnum, offs); 769348709baSArtem Bityutskiy sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery); 7701e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 771ed43f2f0SArtem Bityutskiy if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery) 772ed43f2f0SArtem Bityutskiy return PTR_ERR(sleb); 7737d08ae3cSArtem Bityutskiy /* 7747d08ae3cSArtem Bityutskiy * Note, the below function will recover this log LEB only if 7757d08ae3cSArtem Bityutskiy * it is the last, because unclean reboots can possibly corrupt 7767d08ae3cSArtem Bityutskiy * only the tail of the log. 7777d08ae3cSArtem Bityutskiy */ 7781e51764aSArtem Bityutskiy sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf); 7791e51764aSArtem Bityutskiy if (IS_ERR(sleb)) 7801e51764aSArtem Bityutskiy return PTR_ERR(sleb); 7811e51764aSArtem Bityutskiy } 7821e51764aSArtem Bityutskiy 7831e51764aSArtem Bityutskiy if (sleb->nodes_cnt == 0) { 7841e51764aSArtem Bityutskiy err = 1; 7851e51764aSArtem Bityutskiy goto out; 7861e51764aSArtem Bityutskiy } 7871e51764aSArtem Bityutskiy 7881e51764aSArtem Bityutskiy node = sleb->buf; 7891e51764aSArtem Bityutskiy snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list); 7901e51764aSArtem Bityutskiy if (c->cs_sqnum == 0) { 7911e51764aSArtem Bityutskiy /* 7921e51764aSArtem Bityutskiy * This is the first log LEB we are looking at, make sure that 7931e51764aSArtem Bityutskiy * the first node is a commit start node. Also record its 7941e51764aSArtem Bityutskiy * sequence number so that UBIFS can determine where the log 7951e51764aSArtem Bityutskiy * ends, because all nodes which were have higher sequence 7961e51764aSArtem Bityutskiy * numbers. 7971e51764aSArtem Bityutskiy */ 7981e51764aSArtem Bityutskiy if (snod->type != UBIFS_CS_NODE) { 7991e51764aSArtem Bityutskiy dbg_err("first log node at LEB %d:%d is not CS node", 8001e51764aSArtem Bityutskiy lnum, offs); 8011e51764aSArtem Bityutskiy goto out_dump; 8021e51764aSArtem Bityutskiy } 8031e51764aSArtem Bityutskiy if (le64_to_cpu(node->cmt_no) != c->cmt_no) { 8041e51764aSArtem Bityutskiy dbg_err("first CS node at LEB %d:%d has wrong " 8051e51764aSArtem Bityutskiy "commit number %llu expected %llu", 8061e51764aSArtem Bityutskiy lnum, offs, 8071e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(node->cmt_no), 8081e51764aSArtem Bityutskiy c->cmt_no); 8091e51764aSArtem Bityutskiy goto out_dump; 8101e51764aSArtem Bityutskiy } 8111e51764aSArtem Bityutskiy 8121e51764aSArtem Bityutskiy c->cs_sqnum = le64_to_cpu(node->ch.sqnum); 8131e51764aSArtem Bityutskiy dbg_mnt("commit start sqnum %llu", c->cs_sqnum); 8141e51764aSArtem Bityutskiy } 8151e51764aSArtem Bityutskiy 8161e51764aSArtem Bityutskiy if (snod->sqnum < c->cs_sqnum) { 8171e51764aSArtem Bityutskiy /* 8181e51764aSArtem Bityutskiy * This means that we reached end of log and now 8191e51764aSArtem Bityutskiy * look to the older log data, which was already 8201e51764aSArtem Bityutskiy * committed but the eraseblock was not erased (UBIFS 8216edbfafdSArtem Bityutskiy * only un-maps it). So this basically means we have to 8221e51764aSArtem Bityutskiy * exit with "end of log" code. 8231e51764aSArtem Bityutskiy */ 8241e51764aSArtem Bityutskiy err = 1; 8251e51764aSArtem Bityutskiy goto out; 8261e51764aSArtem Bityutskiy } 8271e51764aSArtem Bityutskiy 8281e51764aSArtem Bityutskiy /* Make sure the first node sits at offset zero of the LEB */ 8291e51764aSArtem Bityutskiy if (snod->offs != 0) { 8301e51764aSArtem Bityutskiy dbg_err("first node is not at zero offset"); 8311e51764aSArtem Bityutskiy goto out_dump; 8321e51764aSArtem Bityutskiy } 8331e51764aSArtem Bityutskiy 8341e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 8351e51764aSArtem Bityutskiy cond_resched(); 8361e51764aSArtem Bityutskiy 8371e51764aSArtem Bityutskiy if (snod->sqnum >= SQNUM_WATERMARK) { 8381e51764aSArtem Bityutskiy ubifs_err("file system's life ended"); 8391e51764aSArtem Bityutskiy goto out_dump; 8401e51764aSArtem Bityutskiy } 8411e51764aSArtem Bityutskiy 8421e51764aSArtem Bityutskiy if (snod->sqnum < c->cs_sqnum) { 8431e51764aSArtem Bityutskiy dbg_err("bad sqnum %llu, commit sqnum %llu", 8441e51764aSArtem Bityutskiy snod->sqnum, c->cs_sqnum); 8451e51764aSArtem Bityutskiy goto out_dump; 8461e51764aSArtem Bityutskiy } 8471e51764aSArtem Bityutskiy 8481e51764aSArtem Bityutskiy if (snod->sqnum > c->max_sqnum) 8491e51764aSArtem Bityutskiy c->max_sqnum = snod->sqnum; 8501e51764aSArtem Bityutskiy 8511e51764aSArtem Bityutskiy switch (snod->type) { 8521e51764aSArtem Bityutskiy case UBIFS_REF_NODE: { 8531e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = snod->node; 8541e51764aSArtem Bityutskiy 8551e51764aSArtem Bityutskiy err = validate_ref(c, ref); 8561e51764aSArtem Bityutskiy if (err == 1) 8571e51764aSArtem Bityutskiy break; /* Already have this bud */ 8581e51764aSArtem Bityutskiy if (err) 8591e51764aSArtem Bityutskiy goto out_dump; 8601e51764aSArtem Bityutskiy 8611e51764aSArtem Bityutskiy err = add_replay_bud(c, le32_to_cpu(ref->lnum), 8621e51764aSArtem Bityutskiy le32_to_cpu(ref->offs), 8631e51764aSArtem Bityutskiy le32_to_cpu(ref->jhead), 8641e51764aSArtem Bityutskiy snod->sqnum); 8651e51764aSArtem Bityutskiy if (err) 8661e51764aSArtem Bityutskiy goto out; 8671e51764aSArtem Bityutskiy 8681e51764aSArtem Bityutskiy break; 8691e51764aSArtem Bityutskiy } 8701e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 8711e51764aSArtem Bityutskiy /* Make sure it sits at the beginning of LEB */ 8721e51764aSArtem Bityutskiy if (snod->offs != 0) { 8731e51764aSArtem Bityutskiy ubifs_err("unexpected node in log"); 8741e51764aSArtem Bityutskiy goto out_dump; 8751e51764aSArtem Bityutskiy } 8761e51764aSArtem Bityutskiy break; 8771e51764aSArtem Bityutskiy default: 8781e51764aSArtem Bityutskiy ubifs_err("unexpected node in log"); 8791e51764aSArtem Bityutskiy goto out_dump; 8801e51764aSArtem Bityutskiy } 8811e51764aSArtem Bityutskiy } 8821e51764aSArtem Bityutskiy 8831e51764aSArtem Bityutskiy if (sleb->endpt || c->lhead_offs >= c->leb_size) { 8841e51764aSArtem Bityutskiy c->lhead_lnum = lnum; 8851e51764aSArtem Bityutskiy c->lhead_offs = sleb->endpt; 8861e51764aSArtem Bityutskiy } 8871e51764aSArtem Bityutskiy 8881e51764aSArtem Bityutskiy err = !sleb->endpt; 8891e51764aSArtem Bityutskiy out: 8901e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8911e51764aSArtem Bityutskiy return err; 8921e51764aSArtem Bityutskiy 8931e51764aSArtem Bityutskiy out_dump: 894681947d2SAdrian Hunter ubifs_err("log error detected while replaying the log at LEB %d:%d", 8951e51764aSArtem Bityutskiy lnum, offs + snod->offs); 8961e51764aSArtem Bityutskiy dbg_dump_node(c, snod->node); 8971e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8981e51764aSArtem Bityutskiy return -EINVAL; 8991e51764aSArtem Bityutskiy } 9001e51764aSArtem Bityutskiy 9011e51764aSArtem Bityutskiy /** 9021e51764aSArtem Bityutskiy * take_ihead - update the status of the index head in lprops to 'taken'. 9031e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9041e51764aSArtem Bityutskiy * 9051e51764aSArtem Bityutskiy * This function returns the amount of free space in the index head LEB or a 9061e51764aSArtem Bityutskiy * negative error code. 9071e51764aSArtem Bityutskiy */ 9081e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c) 9091e51764aSArtem Bityutskiy { 9101e51764aSArtem Bityutskiy const struct ubifs_lprops *lp; 9111e51764aSArtem Bityutskiy int err, free; 9121e51764aSArtem Bityutskiy 9131e51764aSArtem Bityutskiy ubifs_get_lprops(c); 9141e51764aSArtem Bityutskiy 9151e51764aSArtem Bityutskiy lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum); 9161e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 9171e51764aSArtem Bityutskiy err = PTR_ERR(lp); 9181e51764aSArtem Bityutskiy goto out; 9191e51764aSArtem Bityutskiy } 9201e51764aSArtem Bityutskiy 9211e51764aSArtem Bityutskiy free = lp->free; 9221e51764aSArtem Bityutskiy 9231e51764aSArtem Bityutskiy lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC, 9241e51764aSArtem Bityutskiy lp->flags | LPROPS_TAKEN, 0); 9251e51764aSArtem Bityutskiy if (IS_ERR(lp)) { 9261e51764aSArtem Bityutskiy err = PTR_ERR(lp); 9271e51764aSArtem Bityutskiy goto out; 9281e51764aSArtem Bityutskiy } 9291e51764aSArtem Bityutskiy 9301e51764aSArtem Bityutskiy err = free; 9311e51764aSArtem Bityutskiy out: 9321e51764aSArtem Bityutskiy ubifs_release_lprops(c); 9331e51764aSArtem Bityutskiy return err; 9341e51764aSArtem Bityutskiy } 9351e51764aSArtem Bityutskiy 9361e51764aSArtem Bityutskiy /** 9371e51764aSArtem Bityutskiy * ubifs_replay_journal - replay journal. 9381e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9391e51764aSArtem Bityutskiy * 9401e51764aSArtem Bityutskiy * This function scans the journal, replays and cleans it up. It makes sure all 9411e51764aSArtem Bityutskiy * memory data structures related to uncommitted journal are built (dirty TNC 9421e51764aSArtem Bityutskiy * tree, tree of buds, modified lprops, etc). 9431e51764aSArtem Bityutskiy */ 9441e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c) 9451e51764aSArtem Bityutskiy { 9461e51764aSArtem Bityutskiy int err, i, lnum, offs, free; 9471e51764aSArtem Bityutskiy 9481e51764aSArtem Bityutskiy BUILD_BUG_ON(UBIFS_TRUN_KEY > 5); 9491e51764aSArtem Bityutskiy 9501e51764aSArtem Bityutskiy /* Update the status of the index head in lprops to 'taken' */ 9511e51764aSArtem Bityutskiy free = take_ihead(c); 9521e51764aSArtem Bityutskiy if (free < 0) 9531e51764aSArtem Bityutskiy return free; /* Error code */ 9541e51764aSArtem Bityutskiy 9551e51764aSArtem Bityutskiy if (c->ihead_offs != c->leb_size - free) { 9561e51764aSArtem Bityutskiy ubifs_err("bad index head LEB %d:%d", c->ihead_lnum, 9571e51764aSArtem Bityutskiy c->ihead_offs); 9581e51764aSArtem Bityutskiy return -EINVAL; 9591e51764aSArtem Bityutskiy } 9601e51764aSArtem Bityutskiy 9611e51764aSArtem Bityutskiy dbg_mnt("start replaying the journal"); 9621e51764aSArtem Bityutskiy c->replaying = 1; 9631e51764aSArtem Bityutskiy lnum = c->ltail_lnum = c->lhead_lnum; 9641e51764aSArtem Bityutskiy offs = c->lhead_offs; 9651e51764aSArtem Bityutskiy 9661e51764aSArtem Bityutskiy for (i = 0; i < c->log_lebs; i++, lnum++) { 9671e51764aSArtem Bityutskiy if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) { 9681e51764aSArtem Bityutskiy /* 9691e51764aSArtem Bityutskiy * The log is logically circular, we reached the last 9701e51764aSArtem Bityutskiy * LEB, switch to the first one. 9711e51764aSArtem Bityutskiy */ 9721e51764aSArtem Bityutskiy lnum = UBIFS_LOG_LNUM; 9731e51764aSArtem Bityutskiy offs = 0; 9741e51764aSArtem Bityutskiy } 9756599fcbdSArtem Bityutskiy err = replay_log_leb(c, lnum, offs, c->sbuf); 9761e51764aSArtem Bityutskiy if (err == 1) 9771e51764aSArtem Bityutskiy /* We hit the end of the log */ 9781e51764aSArtem Bityutskiy break; 9791e51764aSArtem Bityutskiy if (err) 9801e51764aSArtem Bityutskiy goto out; 9811e51764aSArtem Bityutskiy offs = 0; 9821e51764aSArtem Bityutskiy } 9831e51764aSArtem Bityutskiy 9841e51764aSArtem Bityutskiy err = replay_buds(c); 9851e51764aSArtem Bityutskiy if (err) 9861e51764aSArtem Bityutskiy goto out; 9871e51764aSArtem Bityutskiy 988debf12d5SArtem Bityutskiy err = apply_replay_list(c); 9891e51764aSArtem Bityutskiy if (err) 9901e51764aSArtem Bityutskiy goto out; 9911e51764aSArtem Bityutskiy 992074bcb9bSArtem Bityutskiy err = set_buds_lprops(c); 993074bcb9bSArtem Bityutskiy if (err) 994074bcb9bSArtem Bityutskiy goto out; 995074bcb9bSArtem Bityutskiy 9966edbfafdSArtem Bityutskiy /* 997b137545cSArtem Bityutskiy * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable 998b137545cSArtem Bityutskiy * to roughly estimate index growth. Things like @c->bi.min_idx_lebs 9996edbfafdSArtem Bityutskiy * depend on it. This means we have to initialize it to make sure 10006edbfafdSArtem Bityutskiy * budgeting works properly. 10016edbfafdSArtem Bityutskiy */ 1002b137545cSArtem Bityutskiy c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt); 1003b137545cSArtem Bityutskiy c->bi.uncommitted_idx *= c->max_idx_node_sz; 10046edbfafdSArtem Bityutskiy 10051e51764aSArtem Bityutskiy ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery); 10061e51764aSArtem Bityutskiy dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, " 10071e51764aSArtem Bityutskiy "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum, 1008e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 10091e51764aSArtem Bityutskiy out: 1010debf12d5SArtem Bityutskiy destroy_replay_list(c); 10111e51764aSArtem Bityutskiy destroy_bud_list(c); 10121e51764aSArtem Bityutskiy c->replaying = 0; 10131e51764aSArtem Bityutskiy return err; 10141e51764aSArtem Bityutskiy } 1015