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 implements functions needed to recover from unclean un-mounts. 131e51764aSArtem Bityutskiy * When UBIFS is mounted, it checks a flag on the master node to determine if 14af901ca1SAndré Goddard Rosa * an un-mount was completed successfully. If not, the process of mounting 156fb4374fSArtem Bityutskiy * incorporates additional checking and fixing of on-flash data structures. 161e51764aSArtem Bityutskiy * UBIFS always cleans away all remnants of an unclean un-mount, so that 171e51764aSArtem Bityutskiy * errors do not accumulate. However UBIFS defers recovery if it is mounted 181e51764aSArtem Bityutskiy * read-only, and the flash is not modified in that case. 19be7b42a5SArtem Bityutskiy * 20be7b42a5SArtem Bityutskiy * The general UBIFS approach to the recovery is that it recovers from 21be7b42a5SArtem Bityutskiy * corruptions which could be caused by power cuts, but it refuses to recover 22be7b42a5SArtem Bityutskiy * from corruption caused by other reasons. And UBIFS tries to distinguish 23be7b42a5SArtem Bityutskiy * between these 2 reasons of corruptions and silently recover in the former 24be7b42a5SArtem Bityutskiy * case and loudly complain in the latter case. 25be7b42a5SArtem Bityutskiy * 26be7b42a5SArtem Bityutskiy * UBIFS writes only to erased LEBs, so it writes only to the flash space 27be7b42a5SArtem Bityutskiy * containing only 0xFFs. UBIFS also always writes strictly from the beginning 28be7b42a5SArtem Bityutskiy * of the LEB to the end. And UBIFS assumes that the underlying flash media 292765df7dSArtem Bityutskiy * writes in @c->max_write_size bytes at a time. 30be7b42a5SArtem Bityutskiy * 31be7b42a5SArtem Bityutskiy * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min. 32be7b42a5SArtem Bityutskiy * I/O unit corresponding to offset X to contain corrupted data, all the 33be7b42a5SArtem Bityutskiy * following min. I/O units have to contain empty space (all 0xFFs). If this is 34be7b42a5SArtem Bityutskiy * not true, the corruption cannot be the result of a power cut, and UBIFS 35be7b42a5SArtem Bityutskiy * refuses to mount. 361e51764aSArtem Bityutskiy */ 371e51764aSArtem Bityutskiy 381e51764aSArtem Bityutskiy #include <linux/crc32.h> 395a0e3ad6STejun Heo #include <linux/slab.h> 401e51764aSArtem Bityutskiy #include "ubifs.h" 411e51764aSArtem Bityutskiy 421e51764aSArtem Bityutskiy /** 431e51764aSArtem Bityutskiy * is_empty - determine whether a buffer is empty (contains all 0xff). 441e51764aSArtem Bityutskiy * @buf: buffer to clean 451e51764aSArtem Bityutskiy * @len: length of buffer 461e51764aSArtem Bityutskiy * 471e51764aSArtem Bityutskiy * This function returns %1 if the buffer is empty (contains all 0xff) otherwise 481e51764aSArtem Bityutskiy * %0 is returned. 491e51764aSArtem Bityutskiy */ 501e51764aSArtem Bityutskiy static int is_empty(void *buf, int len) 511e51764aSArtem Bityutskiy { 521e51764aSArtem Bityutskiy uint8_t *p = buf; 531e51764aSArtem Bityutskiy int i; 541e51764aSArtem Bityutskiy 551e51764aSArtem Bityutskiy for (i = 0; i < len; i++) 561e51764aSArtem Bityutskiy if (*p++ != 0xff) 571e51764aSArtem Bityutskiy return 0; 581e51764aSArtem Bityutskiy return 1; 591e51764aSArtem Bityutskiy } 601e51764aSArtem Bityutskiy 611e51764aSArtem Bityutskiy /** 6206112547SArtem Bityutskiy * first_non_ff - find offset of the first non-0xff byte. 6306112547SArtem Bityutskiy * @buf: buffer to search in 6406112547SArtem Bityutskiy * @len: length of buffer 6506112547SArtem Bityutskiy * 6606112547SArtem Bityutskiy * This function returns offset of the first non-0xff byte in @buf or %-1 if 6706112547SArtem Bityutskiy * the buffer contains only 0xff bytes. 6806112547SArtem Bityutskiy */ 6906112547SArtem Bityutskiy static int first_non_ff(void *buf, int len) 7006112547SArtem Bityutskiy { 7106112547SArtem Bityutskiy uint8_t *p = buf; 7206112547SArtem Bityutskiy int i; 7306112547SArtem Bityutskiy 7406112547SArtem Bityutskiy for (i = 0; i < len; i++) 7506112547SArtem Bityutskiy if (*p++ != 0xff) 7606112547SArtem Bityutskiy return i; 7706112547SArtem Bityutskiy return -1; 7806112547SArtem Bityutskiy } 7906112547SArtem Bityutskiy 8006112547SArtem Bityutskiy /** 811e51764aSArtem Bityutskiy * get_master_node - get the last valid master node allowing for corruption. 821e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 831e51764aSArtem Bityutskiy * @lnum: LEB number 841e51764aSArtem Bityutskiy * @pbuf: buffer containing the LEB read, is returned here 851e51764aSArtem Bityutskiy * @mst: master node, if found, is returned here 861e51764aSArtem Bityutskiy * @cor: corruption, if found, is returned here 871e51764aSArtem Bityutskiy * 881e51764aSArtem Bityutskiy * This function allocates a buffer, reads the LEB into it, and finds and 891e51764aSArtem Bityutskiy * returns the last valid master node allowing for one area of corruption. 901e51764aSArtem Bityutskiy * The corrupt area, if there is one, must be consistent with the assumption 911e51764aSArtem Bityutskiy * that it is the result of an unclean unmount while the master node was being 921e51764aSArtem Bityutskiy * written. Under those circumstances, it is valid to use the previously written 931e51764aSArtem Bityutskiy * master node. 941e51764aSArtem Bityutskiy * 951e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 961e51764aSArtem Bityutskiy */ 971e51764aSArtem Bityutskiy static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf, 981e51764aSArtem Bityutskiy struct ubifs_mst_node **mst, void **cor) 991e51764aSArtem Bityutskiy { 1001e51764aSArtem Bityutskiy const int sz = c->mst_node_alsz; 1011e51764aSArtem Bityutskiy int err, offs, len; 1021e51764aSArtem Bityutskiy void *sbuf, *buf; 1031e51764aSArtem Bityutskiy 1041e51764aSArtem Bityutskiy sbuf = vmalloc(c->leb_size); 1051e51764aSArtem Bityutskiy if (!sbuf) 1061e51764aSArtem Bityutskiy return -ENOMEM; 1071e51764aSArtem Bityutskiy 108d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0); 1091e51764aSArtem Bityutskiy if (err && err != -EBADMSG) 1101e51764aSArtem Bityutskiy goto out_free; 1111e51764aSArtem Bityutskiy 1121e51764aSArtem Bityutskiy /* Find the first position that is definitely not a node */ 1131e51764aSArtem Bityutskiy offs = 0; 1141e51764aSArtem Bityutskiy buf = sbuf; 1151e51764aSArtem Bityutskiy len = c->leb_size; 1161e51764aSArtem Bityutskiy while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) { 1171e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 1181e51764aSArtem Bityutskiy 1191e51764aSArtem Bityutskiy if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) 1201e51764aSArtem Bityutskiy break; 1211e51764aSArtem Bityutskiy offs += sz; 1221e51764aSArtem Bityutskiy buf += sz; 1231e51764aSArtem Bityutskiy len -= sz; 1241e51764aSArtem Bityutskiy } 1251e51764aSArtem Bityutskiy /* See if there was a valid master node before that */ 1261e51764aSArtem Bityutskiy if (offs) { 1271e51764aSArtem Bityutskiy int ret; 1281e51764aSArtem Bityutskiy 1291e51764aSArtem Bityutskiy offs -= sz; 1301e51764aSArtem Bityutskiy buf -= sz; 1311e51764aSArtem Bityutskiy len += sz; 1321e51764aSArtem Bityutskiy ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); 1331e51764aSArtem Bityutskiy if (ret != SCANNED_A_NODE && offs) { 1341e51764aSArtem Bityutskiy /* Could have been corruption so check one place back */ 1351e51764aSArtem Bityutskiy offs -= sz; 1361e51764aSArtem Bityutskiy buf -= sz; 1371e51764aSArtem Bityutskiy len += sz; 1381e51764aSArtem Bityutskiy ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); 1391e51764aSArtem Bityutskiy if (ret != SCANNED_A_NODE) 1401e51764aSArtem Bityutskiy /* 1411e51764aSArtem Bityutskiy * We accept only one area of corruption because 1421e51764aSArtem Bityutskiy * we are assuming that it was caused while 1431e51764aSArtem Bityutskiy * trying to write a master node. 1441e51764aSArtem Bityutskiy */ 1451e51764aSArtem Bityutskiy goto out_err; 1461e51764aSArtem Bityutskiy } 1471e51764aSArtem Bityutskiy if (ret == SCANNED_A_NODE) { 1481e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 1491e51764aSArtem Bityutskiy 1501e51764aSArtem Bityutskiy if (ch->node_type != UBIFS_MST_NODE) 1511e51764aSArtem Bityutskiy goto out_err; 1521e51764aSArtem Bityutskiy dbg_rcvry("found a master node at %d:%d", lnum, offs); 1531e51764aSArtem Bityutskiy *mst = buf; 1541e51764aSArtem Bityutskiy offs += sz; 1551e51764aSArtem Bityutskiy buf += sz; 1561e51764aSArtem Bityutskiy len -= sz; 1571e51764aSArtem Bityutskiy } 1581e51764aSArtem Bityutskiy } 1591e51764aSArtem Bityutskiy /* Check for corruption */ 1601e51764aSArtem Bityutskiy if (offs < c->leb_size) { 1611e51764aSArtem Bityutskiy if (!is_empty(buf, min_t(int, len, sz))) { 1621e51764aSArtem Bityutskiy *cor = buf; 1631e51764aSArtem Bityutskiy dbg_rcvry("found corruption at %d:%d", lnum, offs); 1641e51764aSArtem Bityutskiy } 1651e51764aSArtem Bityutskiy offs += sz; 1661e51764aSArtem Bityutskiy buf += sz; 1671e51764aSArtem Bityutskiy len -= sz; 1681e51764aSArtem Bityutskiy } 1691e51764aSArtem Bityutskiy /* Check remaining empty space */ 1701e51764aSArtem Bityutskiy if (offs < c->leb_size) 1711e51764aSArtem Bityutskiy if (!is_empty(buf, len)) 1721e51764aSArtem Bityutskiy goto out_err; 1731e51764aSArtem Bityutskiy *pbuf = sbuf; 1741e51764aSArtem Bityutskiy return 0; 1751e51764aSArtem Bityutskiy 1761e51764aSArtem Bityutskiy out_err: 1771e51764aSArtem Bityutskiy err = -EINVAL; 1781e51764aSArtem Bityutskiy out_free: 1791e51764aSArtem Bityutskiy vfree(sbuf); 1801e51764aSArtem Bityutskiy *mst = NULL; 1811e51764aSArtem Bityutskiy *cor = NULL; 1821e51764aSArtem Bityutskiy return err; 1831e51764aSArtem Bityutskiy } 1841e51764aSArtem Bityutskiy 1851e51764aSArtem Bityutskiy /** 1861e51764aSArtem Bityutskiy * write_rcvrd_mst_node - write recovered master node. 1871e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 1881e51764aSArtem Bityutskiy * @mst: master node 1891e51764aSArtem Bityutskiy * 1901e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 1911e51764aSArtem Bityutskiy */ 1921e51764aSArtem Bityutskiy static int write_rcvrd_mst_node(struct ubifs_info *c, 1931e51764aSArtem Bityutskiy struct ubifs_mst_node *mst) 1941e51764aSArtem Bityutskiy { 1951e51764aSArtem Bityutskiy int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz; 1960ecb9529SHarvey Harrison __le32 save_flags; 1971e51764aSArtem Bityutskiy 1981e51764aSArtem Bityutskiy dbg_rcvry("recovery"); 1991e51764aSArtem Bityutskiy 2001e51764aSArtem Bityutskiy save_flags = mst->flags; 2010ecb9529SHarvey Harrison mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY); 2021e51764aSArtem Bityutskiy 203625700ccSSascha Hauer err = ubifs_prepare_node_hmac(c, mst, UBIFS_MST_NODE_SZ, 204625700ccSSascha Hauer offsetof(struct ubifs_mst_node, hmac), 1); 205625700ccSSascha Hauer if (err) 206625700ccSSascha Hauer goto out; 207b36a261eSRichard Weinberger err = ubifs_leb_change(c, lnum, mst, sz); 2081e51764aSArtem Bityutskiy if (err) 2091e51764aSArtem Bityutskiy goto out; 210b36a261eSRichard Weinberger err = ubifs_leb_change(c, lnum + 1, mst, sz); 2111e51764aSArtem Bityutskiy if (err) 2121e51764aSArtem Bityutskiy goto out; 2131e51764aSArtem Bityutskiy out: 2141e51764aSArtem Bityutskiy mst->flags = save_flags; 2151e51764aSArtem Bityutskiy return err; 2161e51764aSArtem Bityutskiy } 2171e51764aSArtem Bityutskiy 2181e51764aSArtem Bityutskiy /** 2191e51764aSArtem Bityutskiy * ubifs_recover_master_node - recover the master node. 2201e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2211e51764aSArtem Bityutskiy * 2221e51764aSArtem Bityutskiy * This function recovers the master node from corruption that may occur due to 2231e51764aSArtem Bityutskiy * an unclean unmount. 2241e51764aSArtem Bityutskiy * 2251e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 2261e51764aSArtem Bityutskiy */ 2271e51764aSArtem Bityutskiy int ubifs_recover_master_node(struct ubifs_info *c) 2281e51764aSArtem Bityutskiy { 2291e51764aSArtem Bityutskiy void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL; 2301e51764aSArtem Bityutskiy struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst; 2311e51764aSArtem Bityutskiy const int sz = c->mst_node_alsz; 2321e51764aSArtem Bityutskiy int err, offs1, offs2; 2331e51764aSArtem Bityutskiy 2341e51764aSArtem Bityutskiy dbg_rcvry("recovery"); 2351e51764aSArtem Bityutskiy 2361e51764aSArtem Bityutskiy err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1); 2371e51764aSArtem Bityutskiy if (err) 2381e51764aSArtem Bityutskiy goto out_free; 2391e51764aSArtem Bityutskiy 2401e51764aSArtem Bityutskiy err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2); 2411e51764aSArtem Bityutskiy if (err) 2421e51764aSArtem Bityutskiy goto out_free; 2431e51764aSArtem Bityutskiy 2441e51764aSArtem Bityutskiy if (mst1) { 2451e51764aSArtem Bityutskiy offs1 = (void *)mst1 - buf1; 2461e51764aSArtem Bityutskiy if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) && 2471e51764aSArtem Bityutskiy (offs1 == 0 && !cor1)) { 2481e51764aSArtem Bityutskiy /* 2491e51764aSArtem Bityutskiy * mst1 was written by recovery at offset 0 with no 2501e51764aSArtem Bityutskiy * corruption. 2511e51764aSArtem Bityutskiy */ 2521e51764aSArtem Bityutskiy dbg_rcvry("recovery recovery"); 2531e51764aSArtem Bityutskiy mst = mst1; 2541e51764aSArtem Bityutskiy } else if (mst2) { 2551e51764aSArtem Bityutskiy offs2 = (void *)mst2 - buf2; 2561e51764aSArtem Bityutskiy if (offs1 == offs2) { 2571e51764aSArtem Bityutskiy /* Same offset, so must be the same */ 258625700ccSSascha Hauer if (ubifs_compare_master_node(c, mst1, mst2)) 2591e51764aSArtem Bityutskiy goto out_err; 2601e51764aSArtem Bityutskiy mst = mst1; 2611e51764aSArtem Bityutskiy } else if (offs2 + sz == offs1) { 2621e51764aSArtem Bityutskiy /* 1st LEB was written, 2nd was not */ 2631e51764aSArtem Bityutskiy if (cor1) 2641e51764aSArtem Bityutskiy goto out_err; 2651e51764aSArtem Bityutskiy mst = mst1; 26619495f70SAnatolij Gustschin } else if (offs1 == 0 && 26719495f70SAnatolij Gustschin c->leb_size - offs2 - sz < sz) { 2681e51764aSArtem Bityutskiy /* 1st LEB was unmapped and written, 2nd not */ 2691e51764aSArtem Bityutskiy if (cor1) 2701e51764aSArtem Bityutskiy goto out_err; 2711e51764aSArtem Bityutskiy mst = mst1; 2721e51764aSArtem Bityutskiy } else 2731e51764aSArtem Bityutskiy goto out_err; 2741e51764aSArtem Bityutskiy } else { 2751e51764aSArtem Bityutskiy /* 2761e51764aSArtem Bityutskiy * 2nd LEB was unmapped and about to be written, so 2771e51764aSArtem Bityutskiy * there must be only one master node in the first LEB 2781e51764aSArtem Bityutskiy * and no corruption. 2791e51764aSArtem Bityutskiy */ 2801e51764aSArtem Bityutskiy if (offs1 != 0 || cor1) 2811e51764aSArtem Bityutskiy goto out_err; 2821e51764aSArtem Bityutskiy mst = mst1; 2831e51764aSArtem Bityutskiy } 2841e51764aSArtem Bityutskiy } else { 2851e51764aSArtem Bityutskiy if (!mst2) 2861e51764aSArtem Bityutskiy goto out_err; 2871e51764aSArtem Bityutskiy /* 2881e51764aSArtem Bityutskiy * 1st LEB was unmapped and about to be written, so there must 2891e51764aSArtem Bityutskiy * be no room left in 2nd LEB. 2901e51764aSArtem Bityutskiy */ 2911e51764aSArtem Bityutskiy offs2 = (void *)mst2 - buf2; 2921e51764aSArtem Bityutskiy if (offs2 + sz + sz <= c->leb_size) 2931e51764aSArtem Bityutskiy goto out_err; 2941e51764aSArtem Bityutskiy mst = mst2; 2951e51764aSArtem Bityutskiy } 2961e51764aSArtem Bityutskiy 297235c362bSSheng Yong ubifs_msg(c, "recovered master node from LEB %d", 2981e51764aSArtem Bityutskiy (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1)); 2991e51764aSArtem Bityutskiy 3001e51764aSArtem Bityutskiy memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ); 3011e51764aSArtem Bityutskiy 3022ef13294SArtem Bityutskiy if (c->ro_mount) { 3031e51764aSArtem Bityutskiy /* Read-only mode. Keep a copy for switching to rw mode */ 3041e51764aSArtem Bityutskiy c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL); 3051e51764aSArtem Bityutskiy if (!c->rcvrd_mst_node) { 3061e51764aSArtem Bityutskiy err = -ENOMEM; 3071e51764aSArtem Bityutskiy goto out_free; 3081e51764aSArtem Bityutskiy } 3091e51764aSArtem Bityutskiy memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ); 3106e0d9fd3SArtem Bityutskiy 3116e0d9fd3SArtem Bityutskiy /* 3126e0d9fd3SArtem Bityutskiy * We had to recover the master node, which means there was an 3136e0d9fd3SArtem Bityutskiy * unclean reboot. However, it is possible that the master node 3146e0d9fd3SArtem Bityutskiy * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set. 3156e0d9fd3SArtem Bityutskiy * E.g., consider the following chain of events: 3166e0d9fd3SArtem Bityutskiy * 3176e0d9fd3SArtem Bityutskiy * 1. UBIFS was cleanly unmounted, so the master node is clean 3186e0d9fd3SArtem Bityutskiy * 2. UBIFS is being mounted R/W and starts changing the master 3196e0d9fd3SArtem Bityutskiy * node in the first (%UBIFS_MST_LNUM). A power cut happens, 3206e0d9fd3SArtem Bityutskiy * so this LEB ends up with some amount of garbage at the 3216e0d9fd3SArtem Bityutskiy * end. 3226e0d9fd3SArtem Bityutskiy * 3. UBIFS is being mounted R/O. We reach this place and 3236e0d9fd3SArtem Bityutskiy * recover the master node from the second LEB 3246e0d9fd3SArtem Bityutskiy * (%UBIFS_MST_LNUM + 1). But we cannot update the media 3256e0d9fd3SArtem Bityutskiy * because we are being mounted R/O. We have to defer the 3266e0d9fd3SArtem Bityutskiy * operation. 3276e0d9fd3SArtem Bityutskiy * 4. However, this master node (@c->mst_node) is marked as 3286e0d9fd3SArtem Bityutskiy * clean (since the step 1). And if we just return, the 3296e0d9fd3SArtem Bityutskiy * mount code will be confused and won't recover the master 3306e0d9fd3SArtem Bityutskiy * node when it is re-mounter R/W later. 3316e0d9fd3SArtem Bityutskiy * 3326e0d9fd3SArtem Bityutskiy * Thus, to force the recovery by marking the master node as 3336e0d9fd3SArtem Bityutskiy * dirty. 3346e0d9fd3SArtem Bityutskiy */ 3356e0d9fd3SArtem Bityutskiy c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); 3361e51764aSArtem Bityutskiy } else { 3371e51764aSArtem Bityutskiy /* Write the recovered master node */ 3381e51764aSArtem Bityutskiy c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1; 3391e51764aSArtem Bityutskiy err = write_rcvrd_mst_node(c, c->mst_node); 3401e51764aSArtem Bityutskiy if (err) 3411e51764aSArtem Bityutskiy goto out_free; 3421e51764aSArtem Bityutskiy } 3431e51764aSArtem Bityutskiy 3441e51764aSArtem Bityutskiy vfree(buf2); 3451e51764aSArtem Bityutskiy vfree(buf1); 3461e51764aSArtem Bityutskiy 3471e51764aSArtem Bityutskiy return 0; 3481e51764aSArtem Bityutskiy 3491e51764aSArtem Bityutskiy out_err: 3501e51764aSArtem Bityutskiy err = -EINVAL; 3511e51764aSArtem Bityutskiy out_free: 352235c362bSSheng Yong ubifs_err(c, "failed to recover master node"); 3531e51764aSArtem Bityutskiy if (mst1) { 354235c362bSSheng Yong ubifs_err(c, "dumping first master node"); 355edf6be24SArtem Bityutskiy ubifs_dump_node(c, mst1); 3561e51764aSArtem Bityutskiy } 3571e51764aSArtem Bityutskiy if (mst2) { 358235c362bSSheng Yong ubifs_err(c, "dumping second master node"); 359edf6be24SArtem Bityutskiy ubifs_dump_node(c, mst2); 3601e51764aSArtem Bityutskiy } 3611e51764aSArtem Bityutskiy vfree(buf2); 3621e51764aSArtem Bityutskiy vfree(buf1); 3631e51764aSArtem Bityutskiy return err; 3641e51764aSArtem Bityutskiy } 3651e51764aSArtem Bityutskiy 3661e51764aSArtem Bityutskiy /** 3671e51764aSArtem Bityutskiy * ubifs_write_rcvrd_mst_node - write the recovered master node. 3681e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3691e51764aSArtem Bityutskiy * 3701e51764aSArtem Bityutskiy * This function writes the master node that was recovered during mounting in 3711e51764aSArtem Bityutskiy * read-only mode and must now be written because we are remounting rw. 3721e51764aSArtem Bityutskiy * 3731e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 3741e51764aSArtem Bityutskiy */ 3751e51764aSArtem Bityutskiy int ubifs_write_rcvrd_mst_node(struct ubifs_info *c) 3761e51764aSArtem Bityutskiy { 3771e51764aSArtem Bityutskiy int err; 3781e51764aSArtem Bityutskiy 3791e51764aSArtem Bityutskiy if (!c->rcvrd_mst_node) 3801e51764aSArtem Bityutskiy return 0; 3811e51764aSArtem Bityutskiy c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); 3821e51764aSArtem Bityutskiy c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); 3831e51764aSArtem Bityutskiy err = write_rcvrd_mst_node(c, c->rcvrd_mst_node); 3841e51764aSArtem Bityutskiy if (err) 3851e51764aSArtem Bityutskiy return err; 3861e51764aSArtem Bityutskiy kfree(c->rcvrd_mst_node); 3871e51764aSArtem Bityutskiy c->rcvrd_mst_node = NULL; 3881e51764aSArtem Bityutskiy return 0; 3891e51764aSArtem Bityutskiy } 3901e51764aSArtem Bityutskiy 3911e51764aSArtem Bityutskiy /** 3921e51764aSArtem Bityutskiy * is_last_write - determine if an offset was in the last write to a LEB. 3931e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3941e51764aSArtem Bityutskiy * @buf: buffer to check 3951e51764aSArtem Bityutskiy * @offs: offset to check 3961e51764aSArtem Bityutskiy * 3971e51764aSArtem Bityutskiy * This function returns %1 if @offs was in the last write to the LEB whose data 3981e51764aSArtem Bityutskiy * is in @buf, otherwise %0 is returned. The determination is made by checking 3992765df7dSArtem Bityutskiy * for subsequent empty space starting from the next @c->max_write_size 4002765df7dSArtem Bityutskiy * boundary. 4011e51764aSArtem Bityutskiy */ 4021e51764aSArtem Bityutskiy static int is_last_write(const struct ubifs_info *c, void *buf, int offs) 4031e51764aSArtem Bityutskiy { 404428ff9d2SArtem Bityutskiy int empty_offs, check_len; 4051e51764aSArtem Bityutskiy uint8_t *p; 4061e51764aSArtem Bityutskiy 4071e51764aSArtem Bityutskiy /* 4082765df7dSArtem Bityutskiy * Round up to the next @c->max_write_size boundary i.e. @offs is in 4092765df7dSArtem Bityutskiy * the last wbuf written. After that should be empty space. 4101e51764aSArtem Bityutskiy */ 4112765df7dSArtem Bityutskiy empty_offs = ALIGN(offs + 1, c->max_write_size); 4121e51764aSArtem Bityutskiy check_len = c->leb_size - empty_offs; 4131e51764aSArtem Bityutskiy p = buf + empty_offs - offs; 414431102feSArtem Bityutskiy return is_empty(p, check_len); 4151e51764aSArtem Bityutskiy } 4161e51764aSArtem Bityutskiy 4171e51764aSArtem Bityutskiy /** 4181e51764aSArtem Bityutskiy * clean_buf - clean the data from an LEB sitting in a buffer. 4191e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4201e51764aSArtem Bityutskiy * @buf: buffer to clean 4211e51764aSArtem Bityutskiy * @lnum: LEB number to clean 4221e51764aSArtem Bityutskiy * @offs: offset from which to clean 4231e51764aSArtem Bityutskiy * @len: length of buffer 4241e51764aSArtem Bityutskiy * 4251e51764aSArtem Bityutskiy * This function pads up to the next min_io_size boundary (if there is one) and 4261e51764aSArtem Bityutskiy * sets empty space to all 0xff. @buf, @offs and @len are updated to the next 427428ff9d2SArtem Bityutskiy * @c->min_io_size boundary. 4281e51764aSArtem Bityutskiy */ 4291e51764aSArtem Bityutskiy static void clean_buf(const struct ubifs_info *c, void **buf, int lnum, 4301e51764aSArtem Bityutskiy int *offs, int *len) 4311e51764aSArtem Bityutskiy { 4321e51764aSArtem Bityutskiy int empty_offs, pad_len; 4331e51764aSArtem Bityutskiy 4341e51764aSArtem Bityutskiy dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs); 4351e51764aSArtem Bityutskiy 4366eb61d58SRichard Weinberger ubifs_assert(c, !(*offs & 7)); 4371e51764aSArtem Bityutskiy empty_offs = ALIGN(*offs, c->min_io_size); 4381e51764aSArtem Bityutskiy pad_len = empty_offs - *offs; 4391e51764aSArtem Bityutskiy ubifs_pad(c, *buf, pad_len); 4401e51764aSArtem Bityutskiy *offs += pad_len; 4411e51764aSArtem Bityutskiy *buf += pad_len; 4421e51764aSArtem Bityutskiy *len -= pad_len; 4431e51764aSArtem Bityutskiy memset(*buf, 0xff, c->leb_size - empty_offs); 4441e51764aSArtem Bityutskiy } 4451e51764aSArtem Bityutskiy 4461e51764aSArtem Bityutskiy /** 4471e51764aSArtem Bityutskiy * no_more_nodes - determine if there are no more nodes in a buffer. 4481e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4491e51764aSArtem Bityutskiy * @buf: buffer to check 4501e51764aSArtem Bityutskiy * @len: length of buffer 4511e51764aSArtem Bityutskiy * @lnum: LEB number of the LEB from which @buf was read 4521e51764aSArtem Bityutskiy * @offs: offset from which @buf was read 4531e51764aSArtem Bityutskiy * 454de097578SAdrian Hunter * This function ensures that the corrupted node at @offs is the last thing 455de097578SAdrian Hunter * written to a LEB. This function returns %1 if more data is not found and 456de097578SAdrian Hunter * %0 if more data is found. 4571e51764aSArtem Bityutskiy */ 4581e51764aSArtem Bityutskiy static int no_more_nodes(const struct ubifs_info *c, void *buf, int len, 4591e51764aSArtem Bityutskiy int lnum, int offs) 4601e51764aSArtem Bityutskiy { 4611e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 462de097578SAdrian Hunter int skip, dlen = le32_to_cpu(ch->len); 4631e51764aSArtem Bityutskiy 464de097578SAdrian Hunter /* Check for empty space after the corrupt node's common header */ 4652765df7dSArtem Bityutskiy skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs; 466de097578SAdrian Hunter if (is_empty(buf + skip, len - skip)) 467de097578SAdrian Hunter return 1; 4681e51764aSArtem Bityutskiy /* 469de097578SAdrian Hunter * The area after the common header size is not empty, so the common 470de097578SAdrian Hunter * header must be intact. Check it. 4711e51764aSArtem Bityutskiy */ 472de097578SAdrian Hunter if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) { 473de097578SAdrian Hunter dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs); 4741e51764aSArtem Bityutskiy return 0; 4751e51764aSArtem Bityutskiy } 476de097578SAdrian Hunter /* Now we know the corrupt node's length we can skip over it */ 4772765df7dSArtem Bityutskiy skip = ALIGN(offs + dlen, c->max_write_size) - offs; 478de097578SAdrian Hunter /* After which there should be empty space */ 479de097578SAdrian Hunter if (is_empty(buf + skip, len - skip)) 4801e51764aSArtem Bityutskiy return 1; 481de097578SAdrian Hunter dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip); 482de097578SAdrian Hunter return 0; 4831e51764aSArtem Bityutskiy } 4841e51764aSArtem Bityutskiy 4851e51764aSArtem Bityutskiy /** 4861e51764aSArtem Bityutskiy * fix_unclean_leb - fix an unclean LEB. 4871e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4881e51764aSArtem Bityutskiy * @sleb: scanned LEB information 4891e51764aSArtem Bityutskiy * @start: offset where scan started 4901e51764aSArtem Bityutskiy */ 4911e51764aSArtem Bityutskiy static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb, 4921e51764aSArtem Bityutskiy int start) 4931e51764aSArtem Bityutskiy { 4941e51764aSArtem Bityutskiy int lnum = sleb->lnum, endpt = start; 4951e51764aSArtem Bityutskiy 4961e51764aSArtem Bityutskiy /* Get the end offset of the last node we are keeping */ 4971e51764aSArtem Bityutskiy if (!list_empty(&sleb->nodes)) { 4981e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 4991e51764aSArtem Bityutskiy 5001e51764aSArtem Bityutskiy snod = list_entry(sleb->nodes.prev, 5011e51764aSArtem Bityutskiy struct ubifs_scan_node, list); 5021e51764aSArtem Bityutskiy endpt = snod->offs + snod->len; 5031e51764aSArtem Bityutskiy } 5041e51764aSArtem Bityutskiy 5052ef13294SArtem Bityutskiy if (c->ro_mount && !c->remounting_rw) { 5061e51764aSArtem Bityutskiy /* Add to recovery list */ 5071e51764aSArtem Bityutskiy struct ubifs_unclean_leb *ucleb; 5081e51764aSArtem Bityutskiy 5091e51764aSArtem Bityutskiy dbg_rcvry("need to fix LEB %d start %d endpt %d", 5101e51764aSArtem Bityutskiy lnum, start, sleb->endpt); 5111e51764aSArtem Bityutskiy ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS); 5121e51764aSArtem Bityutskiy if (!ucleb) 5131e51764aSArtem Bityutskiy return -ENOMEM; 5141e51764aSArtem Bityutskiy ucleb->lnum = lnum; 5151e51764aSArtem Bityutskiy ucleb->endpt = endpt; 5161e51764aSArtem Bityutskiy list_add_tail(&ucleb->list, &c->unclean_leb_list); 5171e51764aSArtem Bityutskiy } else { 5181e51764aSArtem Bityutskiy /* Write the fixed LEB back to flash */ 5191e51764aSArtem Bityutskiy int err; 5201e51764aSArtem Bityutskiy 5211e51764aSArtem Bityutskiy dbg_rcvry("fixing LEB %d start %d endpt %d", 5221e51764aSArtem Bityutskiy lnum, start, sleb->endpt); 5231e51764aSArtem Bityutskiy if (endpt == 0) { 5241e51764aSArtem Bityutskiy err = ubifs_leb_unmap(c, lnum); 5251e51764aSArtem Bityutskiy if (err) 5261e51764aSArtem Bityutskiy return err; 5271e51764aSArtem Bityutskiy } else { 5281e51764aSArtem Bityutskiy int len = ALIGN(endpt, c->min_io_size); 5291e51764aSArtem Bityutskiy 5301e51764aSArtem Bityutskiy if (start) { 531d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, sleb->buf, 0, 532d304820aSArtem Bityutskiy start, 1); 5331e51764aSArtem Bityutskiy if (err) 5341e51764aSArtem Bityutskiy return err; 5351e51764aSArtem Bityutskiy } 5361e51764aSArtem Bityutskiy /* Pad to min_io_size */ 5371e51764aSArtem Bityutskiy if (len > endpt) { 5381e51764aSArtem Bityutskiy int pad_len = len - ALIGN(endpt, 8); 5391e51764aSArtem Bityutskiy 5401e51764aSArtem Bityutskiy if (pad_len > 0) { 5411e51764aSArtem Bityutskiy void *buf = sleb->buf + len - pad_len; 5421e51764aSArtem Bityutskiy 5431e51764aSArtem Bityutskiy ubifs_pad(c, buf, pad_len); 5441e51764aSArtem Bityutskiy } 5451e51764aSArtem Bityutskiy } 546b36a261eSRichard Weinberger err = ubifs_leb_change(c, lnum, sleb->buf, len); 5471e51764aSArtem Bityutskiy if (err) 5481e51764aSArtem Bityutskiy return err; 5491e51764aSArtem Bityutskiy } 5501e51764aSArtem Bityutskiy } 5511e51764aSArtem Bityutskiy return 0; 5521e51764aSArtem Bityutskiy } 5531e51764aSArtem Bityutskiy 5541e51764aSArtem Bityutskiy /** 555da8b94eaSArtem Bityutskiy * drop_last_group - drop the last group of nodes. 5561e51764aSArtem Bityutskiy * @sleb: scanned LEB information 5571e51764aSArtem Bityutskiy * @offs: offset of dropped nodes is returned here 5581e51764aSArtem Bityutskiy * 559bbf2b37aSArtem Bityutskiy * This is a helper function for 'ubifs_recover_leb()' which drops the last 560da8b94eaSArtem Bityutskiy * group of nodes of the scanned LEB. 5611e51764aSArtem Bityutskiy */ 562da8b94eaSArtem Bityutskiy static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs) 5631e51764aSArtem Bityutskiy { 5641e51764aSArtem Bityutskiy while (!list_empty(&sleb->nodes)) { 5651e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 5661e51764aSArtem Bityutskiy struct ubifs_ch *ch; 5671e51764aSArtem Bityutskiy 5681e51764aSArtem Bityutskiy snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, 5691e51764aSArtem Bityutskiy list); 5701e51764aSArtem Bityutskiy ch = snod->node; 5711e51764aSArtem Bityutskiy if (ch->group_type != UBIFS_IN_NODE_GROUP) 572da8b94eaSArtem Bityutskiy break; 573da8b94eaSArtem Bityutskiy 574da8b94eaSArtem Bityutskiy dbg_rcvry("dropping grouped node at %d:%d", 575da8b94eaSArtem Bityutskiy sleb->lnum, snod->offs); 5761e51764aSArtem Bityutskiy *offs = snod->offs; 5771e51764aSArtem Bityutskiy list_del(&snod->list); 5781e51764aSArtem Bityutskiy kfree(snod); 5791e51764aSArtem Bityutskiy sleb->nodes_cnt -= 1; 5801e51764aSArtem Bityutskiy } 581da8b94eaSArtem Bityutskiy } 582da8b94eaSArtem Bityutskiy 583da8b94eaSArtem Bityutskiy /** 584da8b94eaSArtem Bityutskiy * drop_last_node - drop the last node. 585da8b94eaSArtem Bityutskiy * @sleb: scanned LEB information 586da8b94eaSArtem Bityutskiy * @offs: offset of dropped nodes is returned here 587da8b94eaSArtem Bityutskiy * 588da8b94eaSArtem Bityutskiy * This is a helper function for 'ubifs_recover_leb()' which drops the last 589da8b94eaSArtem Bityutskiy * node of the scanned LEB. 590da8b94eaSArtem Bityutskiy */ 591da8b94eaSArtem Bityutskiy static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs) 592da8b94eaSArtem Bityutskiy { 593da8b94eaSArtem Bityutskiy struct ubifs_scan_node *snod; 594da8b94eaSArtem Bityutskiy 595da8b94eaSArtem Bityutskiy if (!list_empty(&sleb->nodes)) { 596da8b94eaSArtem Bityutskiy snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, 597da8b94eaSArtem Bityutskiy list); 598da8b94eaSArtem Bityutskiy 59979fda517SArtem Bityutskiy dbg_rcvry("dropping last node at %d:%d", 60079fda517SArtem Bityutskiy sleb->lnum, snod->offs); 601da8b94eaSArtem Bityutskiy *offs = snod->offs; 602da8b94eaSArtem Bityutskiy list_del(&snod->list); 603da8b94eaSArtem Bityutskiy kfree(snod); 604da8b94eaSArtem Bityutskiy sleb->nodes_cnt -= 1; 605da8b94eaSArtem Bityutskiy } 6061e51764aSArtem Bityutskiy } 6071e51764aSArtem Bityutskiy 6081e51764aSArtem Bityutskiy /** 6091e51764aSArtem Bityutskiy * ubifs_recover_leb - scan and recover a LEB. 6101e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 6111e51764aSArtem Bityutskiy * @lnum: LEB number 6121e51764aSArtem Bityutskiy * @offs: offset 6131e51764aSArtem Bityutskiy * @sbuf: LEB-sized buffer to use 614efcfde54SArtem Bityutskiy * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not 615efcfde54SArtem Bityutskiy * belong to any journal head) 6161e51764aSArtem Bityutskiy * 6171e51764aSArtem Bityutskiy * This function does a scan of a LEB, but caters for errors that might have 6181e51764aSArtem Bityutskiy * been caused by the unclean unmount from which we are attempting to recover. 619f2b6521aSArtem Bityutskiy * Returns the scanned information on success and a negative error code on 620d685c412SSeunghun Lee * failure. 6211e51764aSArtem Bityutskiy */ 6221e51764aSArtem Bityutskiy struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum, 623efcfde54SArtem Bityutskiy int offs, void *sbuf, int jhead) 6241e51764aSArtem Bityutskiy { 625bbf2b37aSArtem Bityutskiy int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit; 626efcfde54SArtem Bityutskiy int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped; 6271e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 6281e51764aSArtem Bityutskiy void *buf = sbuf + offs; 6291e51764aSArtem Bityutskiy 630efcfde54SArtem Bityutskiy dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped); 6311e51764aSArtem Bityutskiy 6321e51764aSArtem Bityutskiy sleb = ubifs_start_scan(c, lnum, offs, sbuf); 6331e51764aSArtem Bityutskiy if (IS_ERR(sleb)) 6341e51764aSArtem Bityutskiy return sleb; 6351e51764aSArtem Bityutskiy 6366eb61d58SRichard Weinberger ubifs_assert(c, len >= 8); 6371e51764aSArtem Bityutskiy while (len >= 8) { 6381e51764aSArtem Bityutskiy dbg_scan("look at LEB %d:%d (%d bytes left)", 6391e51764aSArtem Bityutskiy lnum, offs, len); 6401e51764aSArtem Bityutskiy 6411e51764aSArtem Bityutskiy cond_resched(); 6421e51764aSArtem Bityutskiy 6431e51764aSArtem Bityutskiy /* 6441e51764aSArtem Bityutskiy * Scan quietly until there is an error from which we cannot 6451e51764aSArtem Bityutskiy * recover 6461e51764aSArtem Bityutskiy */ 647ab75950bSArtem Bityutskiy ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); 6481e51764aSArtem Bityutskiy if (ret == SCANNED_A_NODE) { 6491e51764aSArtem Bityutskiy /* A valid node, and not a padding node */ 6501e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 6511e51764aSArtem Bityutskiy int node_len; 6521e51764aSArtem Bityutskiy 6531e51764aSArtem Bityutskiy err = ubifs_add_snod(c, sleb, buf, offs); 6541e51764aSArtem Bityutskiy if (err) 6551e51764aSArtem Bityutskiy goto error; 6561e51764aSArtem Bityutskiy node_len = ALIGN(le32_to_cpu(ch->len), 8); 6571e51764aSArtem Bityutskiy offs += node_len; 6581e51764aSArtem Bityutskiy buf += node_len; 6591e51764aSArtem Bityutskiy len -= node_len; 66061799206SArtem Bityutskiy } else if (ret > 0) { 6611e51764aSArtem Bityutskiy /* Padding bytes or a valid padding node */ 6621e51764aSArtem Bityutskiy offs += ret; 6631e51764aSArtem Bityutskiy buf += ret; 6641e51764aSArtem Bityutskiy len -= ret; 66561799206SArtem Bityutskiy } else if (ret == SCANNED_EMPTY_SPACE || 66661799206SArtem Bityutskiy ret == SCANNED_GARBAGE || 66761799206SArtem Bityutskiy ret == SCANNED_A_BAD_PAD_NODE || 66861799206SArtem Bityutskiy ret == SCANNED_A_CORRUPT_NODE) { 66978437368SArtem Bityutskiy dbg_rcvry("found corruption (%d) at %d:%d", 67078437368SArtem Bityutskiy ret, lnum, offs); 6711e51764aSArtem Bityutskiy break; 67261799206SArtem Bityutskiy } else { 673235c362bSSheng Yong ubifs_err(c, "unexpected return value %d", ret); 674ed43f2f0SArtem Bityutskiy err = -EINVAL; 675ed43f2f0SArtem Bityutskiy goto error; 6761e51764aSArtem Bityutskiy } 6771e51764aSArtem Bityutskiy } 6781e51764aSArtem Bityutskiy 67961799206SArtem Bityutskiy if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) { 68043e07073SArtem Bityutskiy if (!is_last_write(c, buf, offs)) 68161799206SArtem Bityutskiy goto corrupted_rescan; 68261799206SArtem Bityutskiy } else if (ret == SCANNED_A_CORRUPT_NODE) { 68343e07073SArtem Bityutskiy if (!no_more_nodes(c, buf, len, lnum, offs)) 68461799206SArtem Bityutskiy goto corrupted_rescan; 68561799206SArtem Bityutskiy } else if (!is_empty(buf, len)) { 68643e07073SArtem Bityutskiy if (!is_last_write(c, buf, offs)) { 68706112547SArtem Bityutskiy int corruption = first_non_ff(buf, len); 68806112547SArtem Bityutskiy 689be7b42a5SArtem Bityutskiy /* 690be7b42a5SArtem Bityutskiy * See header comment for this file for more 691be7b42a5SArtem Bityutskiy * explanations about the reasons we have this check. 692be7b42a5SArtem Bityutskiy */ 693235c362bSSheng Yong ubifs_err(c, "corrupt empty space LEB %d:%d, corruption starts at %d", 69479fda517SArtem Bityutskiy lnum, offs, corruption); 69506112547SArtem Bityutskiy /* Make sure we dump interesting non-0xFF data */ 69610ac2797SArtem Bityutskiy offs += corruption; 69706112547SArtem Bityutskiy buf += corruption; 6981e51764aSArtem Bityutskiy goto corrupted; 6991e51764aSArtem Bityutskiy } 7001e51764aSArtem Bityutskiy } 7011e51764aSArtem Bityutskiy 702bbf2b37aSArtem Bityutskiy min_io_unit = round_down(offs, c->min_io_size); 703bbf2b37aSArtem Bityutskiy if (grouped) 704bbf2b37aSArtem Bityutskiy /* 705bbf2b37aSArtem Bityutskiy * If nodes are grouped, always drop the incomplete group at 706bbf2b37aSArtem Bityutskiy * the end. 707bbf2b37aSArtem Bityutskiy */ 708da8b94eaSArtem Bityutskiy drop_last_group(sleb, &offs); 709bbf2b37aSArtem Bityutskiy 710da8b94eaSArtem Bityutskiy if (jhead == GCHD) { 711bbf2b37aSArtem Bityutskiy /* 712da8b94eaSArtem Bityutskiy * If this LEB belongs to the GC head then while we are in the 713da8b94eaSArtem Bityutskiy * middle of the same min. I/O unit keep dropping nodes. So 714da8b94eaSArtem Bityutskiy * basically, what we want is to make sure that the last min. 715da8b94eaSArtem Bityutskiy * I/O unit where we saw the corruption is dropped completely 716da8b94eaSArtem Bityutskiy * with all the uncorrupted nodes which may possibly sit there. 717bbf2b37aSArtem Bityutskiy * 718da8b94eaSArtem Bityutskiy * In other words, let's name the min. I/O unit where the 719da8b94eaSArtem Bityutskiy * corruption starts B, and the previous min. I/O unit A. The 720da8b94eaSArtem Bityutskiy * below code tries to deal with a situation when half of B 721da8b94eaSArtem Bityutskiy * contains valid nodes or the end of a valid node, and the 722da8b94eaSArtem Bityutskiy * second half of B contains corrupted data or garbage. This 723da8b94eaSArtem Bityutskiy * means that UBIFS had been writing to B just before the power 724da8b94eaSArtem Bityutskiy * cut happened. I do not know how realistic is this scenario 725da8b94eaSArtem Bityutskiy * that half of the min. I/O unit had been written successfully 726da8b94eaSArtem Bityutskiy * and the other half not, but this is possible in our 'failure 727da8b94eaSArtem Bityutskiy * mode emulation' infrastructure at least. 728bbf2b37aSArtem Bityutskiy * 729da8b94eaSArtem Bityutskiy * So what is the problem, why we need to drop those nodes? Why 730da8b94eaSArtem Bityutskiy * can't we just clean-up the second half of B by putting a 731da8b94eaSArtem Bityutskiy * padding node there? We can, and this works fine with one 732da8b94eaSArtem Bityutskiy * exception which was reproduced with power cut emulation 733da8b94eaSArtem Bityutskiy * testing and happens extremely rarely. 734bbf2b37aSArtem Bityutskiy * 735da8b94eaSArtem Bityutskiy * Imagine the file-system is full, we run GC which starts 736da8b94eaSArtem Bityutskiy * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is 737da8b94eaSArtem Bityutskiy * the current GC head LEB). The @c->gc_lnum is -1, which means 738da8b94eaSArtem Bityutskiy * that GC will retain LEB X and will try to continue. Imagine 739da8b94eaSArtem Bityutskiy * that LEB X is currently the dirtiest LEB, and the amount of 740da8b94eaSArtem Bityutskiy * used space in LEB Y is exactly the same as amount of free 741da8b94eaSArtem Bityutskiy * space in LEB X. 742bbf2b37aSArtem Bityutskiy * 743da8b94eaSArtem Bityutskiy * And a power cut happens when nodes are moved from LEB X to 744da8b94eaSArtem Bityutskiy * LEB Y. We are here trying to recover LEB Y which is the GC 745da8b94eaSArtem Bityutskiy * head LEB. We find the min. I/O unit B as described above. 746da8b94eaSArtem Bityutskiy * Then we clean-up LEB Y by padding min. I/O unit. And later 747da8b94eaSArtem Bityutskiy * 'ubifs_rcvry_gc_commit()' function fails, because it cannot 748da8b94eaSArtem Bityutskiy * find a dirty LEB which could be GC'd into LEB Y! Even LEB X 749da8b94eaSArtem Bityutskiy * does not match because the amount of valid nodes there does 750da8b94eaSArtem Bityutskiy * not fit the free space in LEB Y any more! And this is 751bbf2b37aSArtem Bityutskiy * because of the padding node which we added to LEB Y. The 752da8b94eaSArtem Bityutskiy * user-visible effect of this which I once observed and 753da8b94eaSArtem Bityutskiy * analysed is that we cannot mount the file-system with 754da8b94eaSArtem Bityutskiy * -ENOSPC error. 755bbf2b37aSArtem Bityutskiy * 756da8b94eaSArtem Bityutskiy * So obviously, to make sure that situation does not happen we 757da8b94eaSArtem Bityutskiy * should free min. I/O unit B in LEB Y completely and the last 758da8b94eaSArtem Bityutskiy * used min. I/O unit in LEB Y should be A. This is basically 759da8b94eaSArtem Bityutskiy * what the below code tries to do. 760bbf2b37aSArtem Bityutskiy */ 761da8b94eaSArtem Bityutskiy while (offs > min_io_unit) 762da8b94eaSArtem Bityutskiy drop_last_node(sleb, &offs); 763da8b94eaSArtem Bityutskiy } 764bbf2b37aSArtem Bityutskiy 7651e51764aSArtem Bityutskiy buf = sbuf + offs; 7661e51764aSArtem Bityutskiy len = c->leb_size - offs; 7671e51764aSArtem Bityutskiy 7681e51764aSArtem Bityutskiy clean_buf(c, &buf, lnum, &offs, &len); 7691e51764aSArtem Bityutskiy ubifs_end_scan(c, sleb, lnum, offs); 7701e51764aSArtem Bityutskiy 7711e51764aSArtem Bityutskiy err = fix_unclean_leb(c, sleb, start); 7721e51764aSArtem Bityutskiy if (err) 7731e51764aSArtem Bityutskiy goto error; 7741e51764aSArtem Bityutskiy 7751e51764aSArtem Bityutskiy return sleb; 7761e51764aSArtem Bityutskiy 77761799206SArtem Bityutskiy corrupted_rescan: 77861799206SArtem Bityutskiy /* Re-scan the corrupted data with verbose messages */ 779235c362bSSheng Yong ubifs_err(c, "corruption %d", ret); 780be186cc4Sshengyong ubifs_scan_a_node(c, buf, len, lnum, offs, 0); 7811e51764aSArtem Bityutskiy corrupted: 7821e51764aSArtem Bityutskiy ubifs_scanned_corruption(c, lnum, offs, buf); 7831e51764aSArtem Bityutskiy err = -EUCLEAN; 7841e51764aSArtem Bityutskiy error: 785235c362bSSheng Yong ubifs_err(c, "LEB %d scanning failed", lnum); 7861e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 7871e51764aSArtem Bityutskiy return ERR_PTR(err); 7881e51764aSArtem Bityutskiy } 7891e51764aSArtem Bityutskiy 7901e51764aSArtem Bityutskiy /** 7911e51764aSArtem Bityutskiy * get_cs_sqnum - get commit start sequence number. 7921e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 7931e51764aSArtem Bityutskiy * @lnum: LEB number of commit start node 7941e51764aSArtem Bityutskiy * @offs: offset of commit start node 7951e51764aSArtem Bityutskiy * @cs_sqnum: commit start sequence number is returned here 7961e51764aSArtem Bityutskiy * 7971e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 7981e51764aSArtem Bityutskiy */ 7991e51764aSArtem Bityutskiy static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs, 8001e51764aSArtem Bityutskiy unsigned long long *cs_sqnum) 8011e51764aSArtem Bityutskiy { 8021e51764aSArtem Bityutskiy struct ubifs_cs_node *cs_node = NULL; 8031e51764aSArtem Bityutskiy int err, ret; 8041e51764aSArtem Bityutskiy 8051e51764aSArtem Bityutskiy dbg_rcvry("at %d:%d", lnum, offs); 8061e51764aSArtem Bityutskiy cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL); 8071e51764aSArtem Bityutskiy if (!cs_node) 8081e51764aSArtem Bityutskiy return -ENOMEM; 8091e51764aSArtem Bityutskiy if (c->leb_size - offs < UBIFS_CS_NODE_SZ) 8101e51764aSArtem Bityutskiy goto out_err; 811d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, (void *)cs_node, offs, 812d304820aSArtem Bityutskiy UBIFS_CS_NODE_SZ, 0); 8131e51764aSArtem Bityutskiy if (err && err != -EBADMSG) 8141e51764aSArtem Bityutskiy goto out_free; 8151e51764aSArtem Bityutskiy ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0); 8161e51764aSArtem Bityutskiy if (ret != SCANNED_A_NODE) { 817235c362bSSheng Yong ubifs_err(c, "Not a valid node"); 8181e51764aSArtem Bityutskiy goto out_err; 8191e51764aSArtem Bityutskiy } 8201e51764aSArtem Bityutskiy if (cs_node->ch.node_type != UBIFS_CS_NODE) { 821*7d8c811bSLiu Song ubifs_err(c, "Not a CS node, type is %d", cs_node->ch.node_type); 8221e51764aSArtem Bityutskiy goto out_err; 8231e51764aSArtem Bityutskiy } 8241e51764aSArtem Bityutskiy if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) { 825235c362bSSheng Yong ubifs_err(c, "CS node cmt_no %llu != current cmt_no %llu", 8261e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(cs_node->cmt_no), 8271e51764aSArtem Bityutskiy c->cmt_no); 8281e51764aSArtem Bityutskiy goto out_err; 8291e51764aSArtem Bityutskiy } 8301e51764aSArtem Bityutskiy *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum); 8311e51764aSArtem Bityutskiy dbg_rcvry("commit start sqnum %llu", *cs_sqnum); 8321e51764aSArtem Bityutskiy kfree(cs_node); 8331e51764aSArtem Bityutskiy return 0; 8341e51764aSArtem Bityutskiy 8351e51764aSArtem Bityutskiy out_err: 8361e51764aSArtem Bityutskiy err = -EINVAL; 8371e51764aSArtem Bityutskiy out_free: 838235c362bSSheng Yong ubifs_err(c, "failed to get CS sqnum"); 8391e51764aSArtem Bityutskiy kfree(cs_node); 8401e51764aSArtem Bityutskiy return err; 8411e51764aSArtem Bityutskiy } 8421e51764aSArtem Bityutskiy 8431e51764aSArtem Bityutskiy /** 8441e51764aSArtem Bityutskiy * ubifs_recover_log_leb - scan and recover a log LEB. 8451e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8461e51764aSArtem Bityutskiy * @lnum: LEB number 8471e51764aSArtem Bityutskiy * @offs: offset 8481e51764aSArtem Bityutskiy * @sbuf: LEB-sized buffer to use 8491e51764aSArtem Bityutskiy * 8501e51764aSArtem Bityutskiy * This function does a scan of a LEB, but caters for errors that might have 8517d08ae3cSArtem Bityutskiy * been caused by unclean reboots from which we are attempting to recover 8527d08ae3cSArtem Bityutskiy * (assume that only the last log LEB can be corrupted by an unclean reboot). 8531e51764aSArtem Bityutskiy * 8541e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 8551e51764aSArtem Bityutskiy */ 8561e51764aSArtem Bityutskiy struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum, 8571e51764aSArtem Bityutskiy int offs, void *sbuf) 8581e51764aSArtem Bityutskiy { 8591e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 8601e51764aSArtem Bityutskiy int next_lnum; 8611e51764aSArtem Bityutskiy 8621e51764aSArtem Bityutskiy dbg_rcvry("LEB %d", lnum); 8631e51764aSArtem Bityutskiy next_lnum = lnum + 1; 8641e51764aSArtem Bityutskiy if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs) 8651e51764aSArtem Bityutskiy next_lnum = UBIFS_LOG_LNUM; 8661e51764aSArtem Bityutskiy if (next_lnum != c->ltail_lnum) { 8671e51764aSArtem Bityutskiy /* 8681e51764aSArtem Bityutskiy * We can only recover at the end of the log, so check that the 8691e51764aSArtem Bityutskiy * next log LEB is empty or out of date. 8701e51764aSArtem Bityutskiy */ 871348709baSArtem Bityutskiy sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0); 8721e51764aSArtem Bityutskiy if (IS_ERR(sleb)) 8731e51764aSArtem Bityutskiy return sleb; 8741e51764aSArtem Bityutskiy if (sleb->nodes_cnt) { 8751e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 8761e51764aSArtem Bityutskiy unsigned long long cs_sqnum = c->cs_sqnum; 8771e51764aSArtem Bityutskiy 8781e51764aSArtem Bityutskiy snod = list_entry(sleb->nodes.next, 8791e51764aSArtem Bityutskiy struct ubifs_scan_node, list); 8801e51764aSArtem Bityutskiy if (cs_sqnum == 0) { 8811e51764aSArtem Bityutskiy int err; 8821e51764aSArtem Bityutskiy 8831e51764aSArtem Bityutskiy err = get_cs_sqnum(c, lnum, offs, &cs_sqnum); 8841e51764aSArtem Bityutskiy if (err) { 8851e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8861e51764aSArtem Bityutskiy return ERR_PTR(err); 8871e51764aSArtem Bityutskiy } 8881e51764aSArtem Bityutskiy } 8891e51764aSArtem Bityutskiy if (snod->sqnum > cs_sqnum) { 890235c362bSSheng Yong ubifs_err(c, "unrecoverable log corruption in LEB %d", 89179fda517SArtem Bityutskiy lnum); 8921e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8931e51764aSArtem Bityutskiy return ERR_PTR(-EUCLEAN); 8941e51764aSArtem Bityutskiy } 8951e51764aSArtem Bityutskiy } 8961e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 8971e51764aSArtem Bityutskiy } 898efcfde54SArtem Bityutskiy return ubifs_recover_leb(c, lnum, offs, sbuf, -1); 8991e51764aSArtem Bityutskiy } 9001e51764aSArtem Bityutskiy 9011e51764aSArtem Bityutskiy /** 9021e51764aSArtem Bityutskiy * recover_head - recover a head. 9031e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9041e51764aSArtem Bityutskiy * @lnum: LEB number of head to recover 9051e51764aSArtem Bityutskiy * @offs: offset of head to recover 9061e51764aSArtem Bityutskiy * @sbuf: LEB-sized buffer to use 9071e51764aSArtem Bityutskiy * 9081e51764aSArtem Bityutskiy * This function ensures that there is no data on the flash at a head location. 9091e51764aSArtem Bityutskiy * 9101e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 9111e51764aSArtem Bityutskiy */ 91283cef708SArtem Bityutskiy static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf) 9131e51764aSArtem Bityutskiy { 9142765df7dSArtem Bityutskiy int len = c->max_write_size, err; 9151e51764aSArtem Bityutskiy 9161e51764aSArtem Bityutskiy if (offs + len > c->leb_size) 9171e51764aSArtem Bityutskiy len = c->leb_size - offs; 9181e51764aSArtem Bityutskiy 9191e51764aSArtem Bityutskiy if (!len) 9201e51764aSArtem Bityutskiy return 0; 9211e51764aSArtem Bityutskiy 9221e51764aSArtem Bityutskiy /* Read at the head location and check it is empty flash */ 923d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1); 924431102feSArtem Bityutskiy if (err || !is_empty(sbuf, len)) { 9251e51764aSArtem Bityutskiy dbg_rcvry("cleaning head at %d:%d", lnum, offs); 9261e51764aSArtem Bityutskiy if (offs == 0) 9271e51764aSArtem Bityutskiy return ubifs_leb_unmap(c, lnum); 928d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1); 9291e51764aSArtem Bityutskiy if (err) 9301e51764aSArtem Bityutskiy return err; 931b36a261eSRichard Weinberger return ubifs_leb_change(c, lnum, sbuf, offs); 9321e51764aSArtem Bityutskiy } 9331e51764aSArtem Bityutskiy 9341e51764aSArtem Bityutskiy return 0; 9351e51764aSArtem Bityutskiy } 9361e51764aSArtem Bityutskiy 9371e51764aSArtem Bityutskiy /** 9381e51764aSArtem Bityutskiy * ubifs_recover_inl_heads - recover index and LPT heads. 9391e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9401e51764aSArtem Bityutskiy * @sbuf: LEB-sized buffer to use 9411e51764aSArtem Bityutskiy * 9421e51764aSArtem Bityutskiy * This function ensures that there is no data on the flash at the index and 9431e51764aSArtem Bityutskiy * LPT head locations. 9441e51764aSArtem Bityutskiy * 9451e51764aSArtem Bityutskiy * This deals with the recovery of a half-completed journal commit. UBIFS is 9461e51764aSArtem Bityutskiy * careful never to overwrite the last version of the index or the LPT. Because 9471e51764aSArtem Bityutskiy * the index and LPT are wandering trees, data from a half-completed commit will 9481e51764aSArtem Bityutskiy * not be referenced anywhere in UBIFS. The data will be either in LEBs that are 9491e51764aSArtem Bityutskiy * assumed to be empty and will be unmapped anyway before use, or in the index 9501e51764aSArtem Bityutskiy * and LPT heads. 9511e51764aSArtem Bityutskiy * 9521e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 9531e51764aSArtem Bityutskiy */ 95483cef708SArtem Bityutskiy int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf) 9551e51764aSArtem Bityutskiy { 9561e51764aSArtem Bityutskiy int err; 9571e51764aSArtem Bityutskiy 9586eb61d58SRichard Weinberger ubifs_assert(c, !c->ro_mount || c->remounting_rw); 9591e51764aSArtem Bityutskiy 9601e51764aSArtem Bityutskiy dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs); 9611e51764aSArtem Bityutskiy err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf); 9621e51764aSArtem Bityutskiy if (err) 9631e51764aSArtem Bityutskiy return err; 9641e51764aSArtem Bityutskiy 9651e51764aSArtem Bityutskiy dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs); 9661e51764aSArtem Bityutskiy 9678a87dc55SFabian Frederick return recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf); 9681e51764aSArtem Bityutskiy } 9691e51764aSArtem Bityutskiy 9701e51764aSArtem Bityutskiy /** 9711e51764aSArtem Bityutskiy * clean_an_unclean_leb - read and write a LEB to remove corruption. 9721e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9731e51764aSArtem Bityutskiy * @ucleb: unclean LEB information 9741e51764aSArtem Bityutskiy * @sbuf: LEB-sized buffer to use 9751e51764aSArtem Bityutskiy * 9761e51764aSArtem Bityutskiy * This function reads a LEB up to a point pre-determined by the mount recovery, 9771e51764aSArtem Bityutskiy * checks the nodes, and writes the result back to the flash, thereby cleaning 9781e51764aSArtem Bityutskiy * off any following corruption, or non-fatal ECC errors. 9791e51764aSArtem Bityutskiy * 9801e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 9811e51764aSArtem Bityutskiy */ 98283cef708SArtem Bityutskiy static int clean_an_unclean_leb(struct ubifs_info *c, 9831e51764aSArtem Bityutskiy struct ubifs_unclean_leb *ucleb, void *sbuf) 9841e51764aSArtem Bityutskiy { 9851e51764aSArtem Bityutskiy int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1; 9861e51764aSArtem Bityutskiy void *buf = sbuf; 9871e51764aSArtem Bityutskiy 9881e51764aSArtem Bityutskiy dbg_rcvry("LEB %d len %d", lnum, len); 9891e51764aSArtem Bityutskiy 9901e51764aSArtem Bityutskiy if (len == 0) { 9911e51764aSArtem Bityutskiy /* Nothing to read, just unmap it */ 9928a87dc55SFabian Frederick return ubifs_leb_unmap(c, lnum); 9931e51764aSArtem Bityutskiy } 9941e51764aSArtem Bityutskiy 995d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, buf, offs, len, 0); 9961e51764aSArtem Bityutskiy if (err && err != -EBADMSG) 9971e51764aSArtem Bityutskiy return err; 9981e51764aSArtem Bityutskiy 9991e51764aSArtem Bityutskiy while (len >= 8) { 10001e51764aSArtem Bityutskiy int ret; 10011e51764aSArtem Bityutskiy 10021e51764aSArtem Bityutskiy cond_resched(); 10031e51764aSArtem Bityutskiy 10041e51764aSArtem Bityutskiy /* Scan quietly until there is an error */ 10051e51764aSArtem Bityutskiy ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); 10061e51764aSArtem Bityutskiy 10071e51764aSArtem Bityutskiy if (ret == SCANNED_A_NODE) { 10081e51764aSArtem Bityutskiy /* A valid node, and not a padding node */ 10091e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 10101e51764aSArtem Bityutskiy int node_len; 10111e51764aSArtem Bityutskiy 10121e51764aSArtem Bityutskiy node_len = ALIGN(le32_to_cpu(ch->len), 8); 10131e51764aSArtem Bityutskiy offs += node_len; 10141e51764aSArtem Bityutskiy buf += node_len; 10151e51764aSArtem Bityutskiy len -= node_len; 10161e51764aSArtem Bityutskiy continue; 10171e51764aSArtem Bityutskiy } 10181e51764aSArtem Bityutskiy 10191e51764aSArtem Bityutskiy if (ret > 0) { 10201e51764aSArtem Bityutskiy /* Padding bytes or a valid padding node */ 10211e51764aSArtem Bityutskiy offs += ret; 10221e51764aSArtem Bityutskiy buf += ret; 10231e51764aSArtem Bityutskiy len -= ret; 10241e51764aSArtem Bityutskiy continue; 10251e51764aSArtem Bityutskiy } 10261e51764aSArtem Bityutskiy 10271e51764aSArtem Bityutskiy if (ret == SCANNED_EMPTY_SPACE) { 1028235c362bSSheng Yong ubifs_err(c, "unexpected empty space at %d:%d", 10291e51764aSArtem Bityutskiy lnum, offs); 10301e51764aSArtem Bityutskiy return -EUCLEAN; 10311e51764aSArtem Bityutskiy } 10321e51764aSArtem Bityutskiy 10331e51764aSArtem Bityutskiy if (quiet) { 10341e51764aSArtem Bityutskiy /* Redo the last scan but noisily */ 10351e51764aSArtem Bityutskiy quiet = 0; 10361e51764aSArtem Bityutskiy continue; 10371e51764aSArtem Bityutskiy } 10381e51764aSArtem Bityutskiy 10391e51764aSArtem Bityutskiy ubifs_scanned_corruption(c, lnum, offs, buf); 10401e51764aSArtem Bityutskiy return -EUCLEAN; 10411e51764aSArtem Bityutskiy } 10421e51764aSArtem Bityutskiy 10431e51764aSArtem Bityutskiy /* Pad to min_io_size */ 10441e51764aSArtem Bityutskiy len = ALIGN(ucleb->endpt, c->min_io_size); 10451e51764aSArtem Bityutskiy if (len > ucleb->endpt) { 10461e51764aSArtem Bityutskiy int pad_len = len - ALIGN(ucleb->endpt, 8); 10471e51764aSArtem Bityutskiy 10481e51764aSArtem Bityutskiy if (pad_len > 0) { 10491e51764aSArtem Bityutskiy buf = c->sbuf + len - pad_len; 10501e51764aSArtem Bityutskiy ubifs_pad(c, buf, pad_len); 10511e51764aSArtem Bityutskiy } 10521e51764aSArtem Bityutskiy } 10531e51764aSArtem Bityutskiy 10541e51764aSArtem Bityutskiy /* Write back the LEB atomically */ 1055b36a261eSRichard Weinberger err = ubifs_leb_change(c, lnum, sbuf, len); 10561e51764aSArtem Bityutskiy if (err) 10571e51764aSArtem Bityutskiy return err; 10581e51764aSArtem Bityutskiy 10591e51764aSArtem Bityutskiy dbg_rcvry("cleaned LEB %d", lnum); 10601e51764aSArtem Bityutskiy 10611e51764aSArtem Bityutskiy return 0; 10621e51764aSArtem Bityutskiy } 10631e51764aSArtem Bityutskiy 10641e51764aSArtem Bityutskiy /** 10651e51764aSArtem Bityutskiy * ubifs_clean_lebs - clean LEBs recovered during read-only mount. 10661e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 10671e51764aSArtem Bityutskiy * @sbuf: LEB-sized buffer to use 10681e51764aSArtem Bityutskiy * 10691e51764aSArtem Bityutskiy * This function cleans a LEB identified during recovery that needs to be 10701e51764aSArtem Bityutskiy * written but was not because UBIFS was mounted read-only. This happens when 10711e51764aSArtem Bityutskiy * remounting to read-write mode. 10721e51764aSArtem Bityutskiy * 10731e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 10741e51764aSArtem Bityutskiy */ 107583cef708SArtem Bityutskiy int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf) 10761e51764aSArtem Bityutskiy { 10771e51764aSArtem Bityutskiy dbg_rcvry("recovery"); 10781e51764aSArtem Bityutskiy while (!list_empty(&c->unclean_leb_list)) { 10791e51764aSArtem Bityutskiy struct ubifs_unclean_leb *ucleb; 10801e51764aSArtem Bityutskiy int err; 10811e51764aSArtem Bityutskiy 10821e51764aSArtem Bityutskiy ucleb = list_entry(c->unclean_leb_list.next, 10831e51764aSArtem Bityutskiy struct ubifs_unclean_leb, list); 10841e51764aSArtem Bityutskiy err = clean_an_unclean_leb(c, ucleb, sbuf); 10851e51764aSArtem Bityutskiy if (err) 10861e51764aSArtem Bityutskiy return err; 10871e51764aSArtem Bityutskiy list_del(&ucleb->list); 10881e51764aSArtem Bityutskiy kfree(ucleb); 10891e51764aSArtem Bityutskiy } 10901e51764aSArtem Bityutskiy return 0; 10911e51764aSArtem Bityutskiy } 10921e51764aSArtem Bityutskiy 10931e51764aSArtem Bityutskiy /** 109444744213SArtem Bityutskiy * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit. 109544744213SArtem Bityutskiy * @c: UBIFS file-system description object 109644744213SArtem Bityutskiy * 109744744213SArtem Bityutskiy * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty 109844744213SArtem Bityutskiy * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns 109944744213SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 110044744213SArtem Bityutskiy */ 110144744213SArtem Bityutskiy static int grab_empty_leb(struct ubifs_info *c) 110244744213SArtem Bityutskiy { 110344744213SArtem Bityutskiy int lnum, err; 110444744213SArtem Bityutskiy 110544744213SArtem Bityutskiy /* 110644744213SArtem Bityutskiy * Note, it is very important to first search for an empty LEB and then 110744744213SArtem Bityutskiy * run the commit, not vice-versa. The reason is that there might be 110844744213SArtem Bityutskiy * only one empty LEB at the moment, the one which has been the 110944744213SArtem Bityutskiy * @c->gc_lnum just before the power cut happened. During the regular 111044744213SArtem Bityutskiy * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no 111144744213SArtem Bityutskiy * one but GC can grab it. But at this moment this single empty LEB is 111244744213SArtem Bityutskiy * not marked as taken, so if we run commit - what happens? Right, the 111344744213SArtem Bityutskiy * commit will grab it and write the index there. Remember that the 111444744213SArtem Bityutskiy * index always expands as long as there is free space, and it only 111544744213SArtem Bityutskiy * starts consolidating when we run out of space. 111644744213SArtem Bityutskiy * 111744744213SArtem Bityutskiy * IOW, if we run commit now, we might not be able to find a free LEB 111844744213SArtem Bityutskiy * after this. 111944744213SArtem Bityutskiy */ 112044744213SArtem Bityutskiy lnum = ubifs_find_free_leb_for_idx(c); 112144744213SArtem Bityutskiy if (lnum < 0) { 1122235c362bSSheng Yong ubifs_err(c, "could not find an empty LEB"); 1123edf6be24SArtem Bityutskiy ubifs_dump_lprops(c); 1124edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &c->bi); 112544744213SArtem Bityutskiy return lnum; 112644744213SArtem Bityutskiy } 112744744213SArtem Bityutskiy 112844744213SArtem Bityutskiy /* Reset the index flag */ 112944744213SArtem Bityutskiy err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0, 113044744213SArtem Bityutskiy LPROPS_INDEX, 0); 113144744213SArtem Bityutskiy if (err) 113244744213SArtem Bityutskiy return err; 113344744213SArtem Bityutskiy 113444744213SArtem Bityutskiy c->gc_lnum = lnum; 113544744213SArtem Bityutskiy dbg_rcvry("found empty LEB %d, run commit", lnum); 113644744213SArtem Bityutskiy 113744744213SArtem Bityutskiy return ubifs_run_commit(c); 113844744213SArtem Bityutskiy } 113944744213SArtem Bityutskiy 114044744213SArtem Bityutskiy /** 11411e51764aSArtem Bityutskiy * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit. 11421e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11431e51764aSArtem Bityutskiy * 11441e51764aSArtem Bityutskiy * Out-of-place garbage collection requires always one empty LEB with which to 11451e51764aSArtem Bityutskiy * start garbage collection. The LEB number is recorded in c->gc_lnum and is 11461e51764aSArtem Bityutskiy * written to the master node on unmounting. In the case of an unclean unmount 11471e51764aSArtem Bityutskiy * the value of gc_lnum recorded in the master node is out of date and cannot 11481e51764aSArtem Bityutskiy * be used. Instead, recovery must allocate an empty LEB for this purpose. 11491e51764aSArtem Bityutskiy * However, there may not be enough empty space, in which case it must be 11501e51764aSArtem Bityutskiy * possible to GC the dirtiest LEB into the GC head LEB. 11511e51764aSArtem Bityutskiy * 11521e51764aSArtem Bityutskiy * This function also runs the commit which causes the TNC updates from 11531e51764aSArtem Bityutskiy * size-recovery and orphans to be written to the flash. That is important to 11541e51764aSArtem Bityutskiy * ensure correct replay order for subsequent mounts. 11551e51764aSArtem Bityutskiy * 11561e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure. 11571e51764aSArtem Bityutskiy */ 11581e51764aSArtem Bityutskiy int ubifs_rcvry_gc_commit(struct ubifs_info *c) 11591e51764aSArtem Bityutskiy { 11601e51764aSArtem Bityutskiy struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf; 11611e51764aSArtem Bityutskiy struct ubifs_lprops lp; 1162fe79c05fSArtem Bityutskiy int err; 11631e51764aSArtem Bityutskiy 1164c839e297SArtem Bityutskiy dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs); 1165c839e297SArtem Bityutskiy 11661e51764aSArtem Bityutskiy c->gc_lnum = -1; 1167c839e297SArtem Bityutskiy if (wbuf->lnum == -1 || wbuf->offs == c->leb_size) 116844744213SArtem Bityutskiy return grab_empty_leb(c); 1169fe79c05fSArtem Bityutskiy 11701e51764aSArtem Bityutskiy err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2); 11711e51764aSArtem Bityutskiy if (err) { 1172fe79c05fSArtem Bityutskiy if (err != -ENOSPC) 1173fe79c05fSArtem Bityutskiy return err; 1174fe79c05fSArtem Bityutskiy 11756fb4374fSArtem Bityutskiy dbg_rcvry("could not find a dirty LEB"); 117644744213SArtem Bityutskiy return grab_empty_leb(c); 11776fb4374fSArtem Bityutskiy } 11782405f594SArtem Bityutskiy 11796eb61d58SRichard Weinberger ubifs_assert(c, !(lp.flags & LPROPS_INDEX)); 11806eb61d58SRichard Weinberger ubifs_assert(c, lp.free + lp.dirty >= wbuf->offs); 11812405f594SArtem Bityutskiy 11821e51764aSArtem Bityutskiy /* 11831e51764aSArtem Bityutskiy * We run the commit before garbage collection otherwise subsequent 11841e51764aSArtem Bityutskiy * mounts will see the GC and orphan deletion in a different order. 11851e51764aSArtem Bityutskiy */ 11861e51764aSArtem Bityutskiy dbg_rcvry("committing"); 11871e51764aSArtem Bityutskiy err = ubifs_run_commit(c); 11881e51764aSArtem Bityutskiy if (err) 11891e51764aSArtem Bityutskiy return err; 1190fe79c05fSArtem Bityutskiy 1191fe79c05fSArtem Bityutskiy dbg_rcvry("GC'ing LEB %d", lp.lnum); 11921e51764aSArtem Bityutskiy mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); 11931e51764aSArtem Bityutskiy err = ubifs_garbage_collect_leb(c, &lp); 11941e51764aSArtem Bityutskiy if (err >= 0) { 11951e51764aSArtem Bityutskiy int err2 = ubifs_wbuf_sync_nolock(wbuf); 11961e51764aSArtem Bityutskiy 11971e51764aSArtem Bityutskiy if (err2) 11981e51764aSArtem Bityutskiy err = err2; 11991e51764aSArtem Bityutskiy } 12001e51764aSArtem Bityutskiy mutex_unlock(&wbuf->io_mutex); 12011e51764aSArtem Bityutskiy if (err < 0) { 1202235c362bSSheng Yong ubifs_err(c, "GC failed, error %d", err); 12031e51764aSArtem Bityutskiy if (err == -EAGAIN) 12041e51764aSArtem Bityutskiy err = -EINVAL; 12051e51764aSArtem Bityutskiy return err; 12061e51764aSArtem Bityutskiy } 1207fe79c05fSArtem Bityutskiy 12086eb61d58SRichard Weinberger ubifs_assert(c, err == LEB_RETAINED); 1209fe79c05fSArtem Bityutskiy if (err != LEB_RETAINED) 12101e51764aSArtem Bityutskiy return -EINVAL; 1211fe79c05fSArtem Bityutskiy 12121e51764aSArtem Bityutskiy err = ubifs_leb_unmap(c, c->gc_lnum); 12131e51764aSArtem Bityutskiy if (err) 12141e51764aSArtem Bityutskiy return err; 1215fe79c05fSArtem Bityutskiy 1216fe79c05fSArtem Bityutskiy dbg_rcvry("allocated LEB %d for GC", lp.lnum); 12171e51764aSArtem Bityutskiy return 0; 12181e51764aSArtem Bityutskiy } 12191e51764aSArtem Bityutskiy 12201e51764aSArtem Bityutskiy /** 12211e51764aSArtem Bityutskiy * struct size_entry - inode size information for recovery. 12221e51764aSArtem Bityutskiy * @rb: link in the RB-tree of sizes 12231e51764aSArtem Bityutskiy * @inum: inode number 12241e51764aSArtem Bityutskiy * @i_size: size on inode 12251e51764aSArtem Bityutskiy * @d_size: maximum size based on data nodes 12261e51764aSArtem Bityutskiy * @exists: indicates whether the inode exists 12271e51764aSArtem Bityutskiy * @inode: inode if pinned in memory awaiting rw mode to fix it 12281e51764aSArtem Bityutskiy */ 12291e51764aSArtem Bityutskiy struct size_entry { 12301e51764aSArtem Bityutskiy struct rb_node rb; 12311e51764aSArtem Bityutskiy ino_t inum; 12321e51764aSArtem Bityutskiy loff_t i_size; 12331e51764aSArtem Bityutskiy loff_t d_size; 12341e51764aSArtem Bityutskiy int exists; 12351e51764aSArtem Bityutskiy struct inode *inode; 12361e51764aSArtem Bityutskiy }; 12371e51764aSArtem Bityutskiy 12381e51764aSArtem Bityutskiy /** 12391e51764aSArtem Bityutskiy * add_ino - add an entry to the size tree. 12401e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12411e51764aSArtem Bityutskiy * @inum: inode number 12421e51764aSArtem Bityutskiy * @i_size: size on inode 12431e51764aSArtem Bityutskiy * @d_size: maximum size based on data nodes 12441e51764aSArtem Bityutskiy * @exists: indicates whether the inode exists 12451e51764aSArtem Bityutskiy */ 12461e51764aSArtem Bityutskiy static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size, 12471e51764aSArtem Bityutskiy loff_t d_size, int exists) 12481e51764aSArtem Bityutskiy { 12491e51764aSArtem Bityutskiy struct rb_node **p = &c->size_tree.rb_node, *parent = NULL; 12501e51764aSArtem Bityutskiy struct size_entry *e; 12511e51764aSArtem Bityutskiy 12521e51764aSArtem Bityutskiy while (*p) { 12531e51764aSArtem Bityutskiy parent = *p; 12541e51764aSArtem Bityutskiy e = rb_entry(parent, struct size_entry, rb); 12551e51764aSArtem Bityutskiy if (inum < e->inum) 12561e51764aSArtem Bityutskiy p = &(*p)->rb_left; 12571e51764aSArtem Bityutskiy else 12581e51764aSArtem Bityutskiy p = &(*p)->rb_right; 12591e51764aSArtem Bityutskiy } 12601e51764aSArtem Bityutskiy 12611e51764aSArtem Bityutskiy e = kzalloc(sizeof(struct size_entry), GFP_KERNEL); 12621e51764aSArtem Bityutskiy if (!e) 12631e51764aSArtem Bityutskiy return -ENOMEM; 12641e51764aSArtem Bityutskiy 12651e51764aSArtem Bityutskiy e->inum = inum; 12661e51764aSArtem Bityutskiy e->i_size = i_size; 12671e51764aSArtem Bityutskiy e->d_size = d_size; 12681e51764aSArtem Bityutskiy e->exists = exists; 12691e51764aSArtem Bityutskiy 12701e51764aSArtem Bityutskiy rb_link_node(&e->rb, parent, p); 12711e51764aSArtem Bityutskiy rb_insert_color(&e->rb, &c->size_tree); 12721e51764aSArtem Bityutskiy 12731e51764aSArtem Bityutskiy return 0; 12741e51764aSArtem Bityutskiy } 12751e51764aSArtem Bityutskiy 12761e51764aSArtem Bityutskiy /** 12771e51764aSArtem Bityutskiy * find_ino - find an entry on the size tree. 12781e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12791e51764aSArtem Bityutskiy * @inum: inode number 12801e51764aSArtem Bityutskiy */ 12811e51764aSArtem Bityutskiy static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum) 12821e51764aSArtem Bityutskiy { 12831e51764aSArtem Bityutskiy struct rb_node *p = c->size_tree.rb_node; 12841e51764aSArtem Bityutskiy struct size_entry *e; 12851e51764aSArtem Bityutskiy 12861e51764aSArtem Bityutskiy while (p) { 12871e51764aSArtem Bityutskiy e = rb_entry(p, struct size_entry, rb); 12881e51764aSArtem Bityutskiy if (inum < e->inum) 12891e51764aSArtem Bityutskiy p = p->rb_left; 12901e51764aSArtem Bityutskiy else if (inum > e->inum) 12911e51764aSArtem Bityutskiy p = p->rb_right; 12921e51764aSArtem Bityutskiy else 12931e51764aSArtem Bityutskiy return e; 12941e51764aSArtem Bityutskiy } 12951e51764aSArtem Bityutskiy return NULL; 12961e51764aSArtem Bityutskiy } 12971e51764aSArtem Bityutskiy 12981e51764aSArtem Bityutskiy /** 12991e51764aSArtem Bityutskiy * remove_ino - remove an entry from the size tree. 13001e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13011e51764aSArtem Bityutskiy * @inum: inode number 13021e51764aSArtem Bityutskiy */ 13031e51764aSArtem Bityutskiy static void remove_ino(struct ubifs_info *c, ino_t inum) 13041e51764aSArtem Bityutskiy { 13051e51764aSArtem Bityutskiy struct size_entry *e = find_ino(c, inum); 13061e51764aSArtem Bityutskiy 13071e51764aSArtem Bityutskiy if (!e) 13081e51764aSArtem Bityutskiy return; 13091e51764aSArtem Bityutskiy rb_erase(&e->rb, &c->size_tree); 13101e51764aSArtem Bityutskiy kfree(e); 13111e51764aSArtem Bityutskiy } 13121e51764aSArtem Bityutskiy 13131e51764aSArtem Bityutskiy /** 13141e51764aSArtem Bityutskiy * ubifs_destroy_size_tree - free resources related to the size tree. 13151e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13161e51764aSArtem Bityutskiy */ 13171e51764aSArtem Bityutskiy void ubifs_destroy_size_tree(struct ubifs_info *c) 13181e51764aSArtem Bityutskiy { 1319bb25e49fSCody P Schafer struct size_entry *e, *n; 13201e51764aSArtem Bityutskiy 1321bb25e49fSCody P Schafer rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) { 13221e51764aSArtem Bityutskiy iput(e->inode); 13231e51764aSArtem Bityutskiy kfree(e); 13241e51764aSArtem Bityutskiy } 1325bb25e49fSCody P Schafer 13261e51764aSArtem Bityutskiy c->size_tree = RB_ROOT; 13271e51764aSArtem Bityutskiy } 13281e51764aSArtem Bityutskiy 13291e51764aSArtem Bityutskiy /** 13301e51764aSArtem Bityutskiy * ubifs_recover_size_accum - accumulate inode sizes for recovery. 13311e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13321e51764aSArtem Bityutskiy * @key: node key 13331e51764aSArtem Bityutskiy * @deletion: node is for a deletion 13341e51764aSArtem Bityutskiy * @new_size: inode size 13351e51764aSArtem Bityutskiy * 13361e51764aSArtem Bityutskiy * This function has two purposes: 13371e51764aSArtem Bityutskiy * 1) to ensure there are no data nodes that fall outside the inode size 13381e51764aSArtem Bityutskiy * 2) to ensure there are no data nodes for inodes that do not exist 13391e51764aSArtem Bityutskiy * To accomplish those purposes, a rb-tree is constructed containing an entry 13401e51764aSArtem Bityutskiy * for each inode number in the journal that has not been deleted, and recording 13411e51764aSArtem Bityutskiy * the size from the inode node, the maximum size of any data node (also altered 13421e51764aSArtem Bityutskiy * by truncations) and a flag indicating a inode number for which no inode node 13431e51764aSArtem Bityutskiy * was present in the journal. 13441e51764aSArtem Bityutskiy * 13451e51764aSArtem Bityutskiy * Note that there is still the possibility that there are data nodes that have 13461e51764aSArtem Bityutskiy * been committed that are beyond the inode size, however the only way to find 13471e51764aSArtem Bityutskiy * them would be to scan the entire index. Alternatively, some provision could 13481e51764aSArtem Bityutskiy * be made to record the size of inodes at the start of commit, which would seem 13491e51764aSArtem Bityutskiy * very cumbersome for a scenario that is quite unlikely and the only negative 13501e51764aSArtem Bityutskiy * consequence of which is wasted space. 13511e51764aSArtem Bityutskiy * 13521e51764aSArtem Bityutskiy * This functions returns %0 on success and a negative error code on failure. 13531e51764aSArtem Bityutskiy */ 13541e51764aSArtem Bityutskiy int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key, 13551e51764aSArtem Bityutskiy int deletion, loff_t new_size) 13561e51764aSArtem Bityutskiy { 13571e51764aSArtem Bityutskiy ino_t inum = key_inum(c, key); 13581e51764aSArtem Bityutskiy struct size_entry *e; 13591e51764aSArtem Bityutskiy int err; 13601e51764aSArtem Bityutskiy 13611e51764aSArtem Bityutskiy switch (key_type(c, key)) { 13621e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 13631e51764aSArtem Bityutskiy if (deletion) 13641e51764aSArtem Bityutskiy remove_ino(c, inum); 13651e51764aSArtem Bityutskiy else { 13661e51764aSArtem Bityutskiy e = find_ino(c, inum); 13671e51764aSArtem Bityutskiy if (e) { 13681e51764aSArtem Bityutskiy e->i_size = new_size; 13691e51764aSArtem Bityutskiy e->exists = 1; 13701e51764aSArtem Bityutskiy } else { 13711e51764aSArtem Bityutskiy err = add_ino(c, inum, new_size, 0, 1); 13721e51764aSArtem Bityutskiy if (err) 13731e51764aSArtem Bityutskiy return err; 13741e51764aSArtem Bityutskiy } 13751e51764aSArtem Bityutskiy } 13761e51764aSArtem Bityutskiy break; 13771e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 13781e51764aSArtem Bityutskiy e = find_ino(c, inum); 13791e51764aSArtem Bityutskiy if (e) { 13801e51764aSArtem Bityutskiy if (new_size > e->d_size) 13811e51764aSArtem Bityutskiy e->d_size = new_size; 13821e51764aSArtem Bityutskiy } else { 13831e51764aSArtem Bityutskiy err = add_ino(c, inum, 0, new_size, 0); 13841e51764aSArtem Bityutskiy if (err) 13851e51764aSArtem Bityutskiy return err; 13861e51764aSArtem Bityutskiy } 13871e51764aSArtem Bityutskiy break; 13881e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 13891e51764aSArtem Bityutskiy e = find_ino(c, inum); 13901e51764aSArtem Bityutskiy if (e) 13911e51764aSArtem Bityutskiy e->d_size = new_size; 13921e51764aSArtem Bityutskiy break; 13931e51764aSArtem Bityutskiy } 13941e51764aSArtem Bityutskiy return 0; 13951e51764aSArtem Bityutskiy } 13961e51764aSArtem Bityutskiy 13971e51764aSArtem Bityutskiy /** 13981e51764aSArtem Bityutskiy * fix_size_in_place - fix inode size in place on flash. 13991e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 14001e51764aSArtem Bityutskiy * @e: inode size information for recovery 14011e51764aSArtem Bityutskiy */ 14021e51764aSArtem Bityutskiy static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e) 14031e51764aSArtem Bityutskiy { 14041e51764aSArtem Bityutskiy struct ubifs_ino_node *ino = c->sbuf; 14051e51764aSArtem Bityutskiy unsigned char *p; 14061e51764aSArtem Bityutskiy union ubifs_key key; 14071e51764aSArtem Bityutskiy int err, lnum, offs, len; 14081e51764aSArtem Bityutskiy loff_t i_size; 14091e51764aSArtem Bityutskiy uint32_t crc; 14101e51764aSArtem Bityutskiy 14111e51764aSArtem Bityutskiy /* Locate the inode node LEB number and offset */ 14121e51764aSArtem Bityutskiy ino_key_init(c, &key, e->inum); 14131e51764aSArtem Bityutskiy err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs); 14141e51764aSArtem Bityutskiy if (err) 14151e51764aSArtem Bityutskiy goto out; 14161e51764aSArtem Bityutskiy /* 14171e51764aSArtem Bityutskiy * If the size recorded on the inode node is greater than the size that 14181e51764aSArtem Bityutskiy * was calculated from nodes in the journal then don't change the inode. 14191e51764aSArtem Bityutskiy */ 14201e51764aSArtem Bityutskiy i_size = le64_to_cpu(ino->size); 14211e51764aSArtem Bityutskiy if (i_size >= e->d_size) 14221e51764aSArtem Bityutskiy return 0; 14231e51764aSArtem Bityutskiy /* Read the LEB */ 1424d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1); 14251e51764aSArtem Bityutskiy if (err) 14261e51764aSArtem Bityutskiy goto out; 14271e51764aSArtem Bityutskiy /* Change the size field and recalculate the CRC */ 14281e51764aSArtem Bityutskiy ino = c->sbuf + offs; 14291e51764aSArtem Bityutskiy ino->size = cpu_to_le64(e->d_size); 14301e51764aSArtem Bityutskiy len = le32_to_cpu(ino->ch.len); 14311e51764aSArtem Bityutskiy crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8); 14321e51764aSArtem Bityutskiy ino->ch.crc = cpu_to_le32(crc); 14331e51764aSArtem Bityutskiy /* Work out where data in the LEB ends and free space begins */ 14341e51764aSArtem Bityutskiy p = c->sbuf; 14351e51764aSArtem Bityutskiy len = c->leb_size - 1; 14361e51764aSArtem Bityutskiy while (p[len] == 0xff) 14371e51764aSArtem Bityutskiy len -= 1; 14381e51764aSArtem Bityutskiy len = ALIGN(len + 1, c->min_io_size); 14391e51764aSArtem Bityutskiy /* Atomically write the fixed LEB back again */ 1440b36a261eSRichard Weinberger err = ubifs_leb_change(c, lnum, c->sbuf, len); 14411e51764aSArtem Bityutskiy if (err) 14421e51764aSArtem Bityutskiy goto out; 1443e84461adSArtem Bityutskiy dbg_rcvry("inode %lu at %d:%d size %lld -> %lld", 1444e84461adSArtem Bityutskiy (unsigned long)e->inum, lnum, offs, i_size, e->d_size); 14451e51764aSArtem Bityutskiy return 0; 14461e51764aSArtem Bityutskiy 14471e51764aSArtem Bityutskiy out: 1448235c362bSSheng Yong ubifs_warn(c, "inode %lu failed to fix size %lld -> %lld error %d", 1449e84461adSArtem Bityutskiy (unsigned long)e->inum, e->i_size, e->d_size, err); 14501e51764aSArtem Bityutskiy return err; 14511e51764aSArtem Bityutskiy } 14521e51764aSArtem Bityutskiy 14531e51764aSArtem Bityutskiy /** 14541e76592fSSascha Hauer * inode_fix_size - fix inode size 14551e76592fSSascha Hauer * @c: UBIFS file-system description object 14561e76592fSSascha Hauer * @e: inode size information for recovery 14571e76592fSSascha Hauer */ 14581e76592fSSascha Hauer static int inode_fix_size(struct ubifs_info *c, struct size_entry *e) 14591e76592fSSascha Hauer { 14601e76592fSSascha Hauer struct inode *inode; 14611e76592fSSascha Hauer struct ubifs_inode *ui; 14621e76592fSSascha Hauer int err; 14631e76592fSSascha Hauer 14641e76592fSSascha Hauer if (c->ro_mount) 14651e76592fSSascha Hauer ubifs_assert(c, !e->inode); 14661e76592fSSascha Hauer 14671e76592fSSascha Hauer if (e->inode) { 14681e76592fSSascha Hauer /* Remounting rw, pick up inode we stored earlier */ 14691e76592fSSascha Hauer inode = e->inode; 14701e76592fSSascha Hauer } else { 14711e76592fSSascha Hauer inode = ubifs_iget(c->vfs_sb, e->inum); 14721e76592fSSascha Hauer if (IS_ERR(inode)) 14731e76592fSSascha Hauer return PTR_ERR(inode); 14741e76592fSSascha Hauer 14751e76592fSSascha Hauer if (inode->i_size >= e->d_size) { 14761e76592fSSascha Hauer /* 14771e76592fSSascha Hauer * The original inode in the index already has a size 14781e76592fSSascha Hauer * big enough, nothing to do 14791e76592fSSascha Hauer */ 14801e76592fSSascha Hauer iput(inode); 14811e76592fSSascha Hauer return 0; 14821e76592fSSascha Hauer } 14831e76592fSSascha Hauer 14841e76592fSSascha Hauer dbg_rcvry("ino %lu size %lld -> %lld", 14851e76592fSSascha Hauer (unsigned long)e->inum, 14861e76592fSSascha Hauer inode->i_size, e->d_size); 14871e76592fSSascha Hauer 14881e76592fSSascha Hauer ui = ubifs_inode(inode); 14891e76592fSSascha Hauer 14901e76592fSSascha Hauer inode->i_size = e->d_size; 14911e76592fSSascha Hauer ui->ui_size = e->d_size; 14921e76592fSSascha Hauer ui->synced_i_size = e->d_size; 14931e76592fSSascha Hauer 14941e76592fSSascha Hauer e->inode = inode; 14951e76592fSSascha Hauer } 14961e76592fSSascha Hauer 14971e76592fSSascha Hauer /* 14981e76592fSSascha Hauer * In readonly mode just keep the inode pinned in memory until we go 14991e76592fSSascha Hauer * readwrite. In readwrite mode write the inode to the journal with the 15001e76592fSSascha Hauer * fixed size. 15011e76592fSSascha Hauer */ 15021e76592fSSascha Hauer if (c->ro_mount) 15031e76592fSSascha Hauer return 0; 15041e76592fSSascha Hauer 15051e76592fSSascha Hauer err = ubifs_jnl_write_inode(c, inode); 15061e76592fSSascha Hauer 15071e76592fSSascha Hauer iput(inode); 15081e76592fSSascha Hauer 15091e76592fSSascha Hauer if (err) 15101e76592fSSascha Hauer return err; 15111e76592fSSascha Hauer 15121e76592fSSascha Hauer rb_erase(&e->rb, &c->size_tree); 15131e76592fSSascha Hauer kfree(e); 15141e76592fSSascha Hauer 15151e76592fSSascha Hauer return 0; 15161e76592fSSascha Hauer } 15171e76592fSSascha Hauer 15181e76592fSSascha Hauer /** 15191e51764aSArtem Bityutskiy * ubifs_recover_size - recover inode size. 15201e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15211e76592fSSascha Hauer * @in_place: If true, do a in-place size fixup 15221e51764aSArtem Bityutskiy * 15231e51764aSArtem Bityutskiy * This function attempts to fix inode size discrepancies identified by the 15241e51764aSArtem Bityutskiy * 'ubifs_recover_size_accum()' function. 15251e51764aSArtem Bityutskiy * 15261e51764aSArtem Bityutskiy * This functions returns %0 on success and a negative error code on failure. 15271e51764aSArtem Bityutskiy */ 15281e76592fSSascha Hauer int ubifs_recover_size(struct ubifs_info *c, bool in_place) 15291e51764aSArtem Bityutskiy { 15301e51764aSArtem Bityutskiy struct rb_node *this = rb_first(&c->size_tree); 15311e51764aSArtem Bityutskiy 15321e51764aSArtem Bityutskiy while (this) { 15331e51764aSArtem Bityutskiy struct size_entry *e; 15341e51764aSArtem Bityutskiy int err; 15351e51764aSArtem Bityutskiy 15361e51764aSArtem Bityutskiy e = rb_entry(this, struct size_entry, rb); 15371e76592fSSascha Hauer 15381e76592fSSascha Hauer this = rb_next(this); 15391e76592fSSascha Hauer 15401e51764aSArtem Bityutskiy if (!e->exists) { 15411e51764aSArtem Bityutskiy union ubifs_key key; 15421e51764aSArtem Bityutskiy 15431e51764aSArtem Bityutskiy ino_key_init(c, &key, e->inum); 15441e51764aSArtem Bityutskiy err = ubifs_tnc_lookup(c, &key, c->sbuf); 15451e51764aSArtem Bityutskiy if (err && err != -ENOENT) 15461e51764aSArtem Bityutskiy return err; 15471e51764aSArtem Bityutskiy if (err == -ENOENT) { 15481e51764aSArtem Bityutskiy /* Remove data nodes that have no inode */ 1549e84461adSArtem Bityutskiy dbg_rcvry("removing ino %lu", 1550e84461adSArtem Bityutskiy (unsigned long)e->inum); 15511e51764aSArtem Bityutskiy err = ubifs_tnc_remove_ino(c, e->inum); 15521e51764aSArtem Bityutskiy if (err) 15531e51764aSArtem Bityutskiy return err; 15541e51764aSArtem Bityutskiy } else { 15551e51764aSArtem Bityutskiy struct ubifs_ino_node *ino = c->sbuf; 15561e51764aSArtem Bityutskiy 15571e51764aSArtem Bityutskiy e->exists = 1; 15581e51764aSArtem Bityutskiy e->i_size = le64_to_cpu(ino->size); 15591e51764aSArtem Bityutskiy } 15601e51764aSArtem Bityutskiy } 156169f8a75aSArtem Bityutskiy 15621e51764aSArtem Bityutskiy if (e->exists && e->i_size < e->d_size) { 15631e76592fSSascha Hauer ubifs_assert(c, !(c->ro_mount && in_place)); 15641e51764aSArtem Bityutskiy 15651e76592fSSascha Hauer /* 15661e76592fSSascha Hauer * We found data that is outside the found inode size, 15671e76592fSSascha Hauer * fixup the inode size 15681e76592fSSascha Hauer */ 156969f8a75aSArtem Bityutskiy 15701e76592fSSascha Hauer if (in_place) { 15711e51764aSArtem Bityutskiy err = fix_size_in_place(c, e); 15721e51764aSArtem Bityutskiy if (err) 15731e51764aSArtem Bityutskiy return err; 15741e51764aSArtem Bityutskiy iput(e->inode); 15751e76592fSSascha Hauer } else { 15761e76592fSSascha Hauer err = inode_fix_size(c, e); 15771e76592fSSascha Hauer if (err) 15781e76592fSSascha Hauer return err; 15791e76592fSSascha Hauer continue; 15801e51764aSArtem Bityutskiy } 15811e51764aSArtem Bityutskiy } 158269f8a75aSArtem Bityutskiy 15831e51764aSArtem Bityutskiy rb_erase(&e->rb, &c->size_tree); 15841e51764aSArtem Bityutskiy kfree(e); 15851e51764aSArtem Bityutskiy } 158669f8a75aSArtem Bityutskiy 15871e51764aSArtem Bityutskiy return 0; 15881e51764aSArtem Bityutskiy } 1589