11e51764aSArtem Bityutskiy /* 21e51764aSArtem Bityutskiy * This file is part of UBIFS. 31e51764aSArtem Bityutskiy * 41e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation. 51e51764aSArtem Bityutskiy * Copyright (C) 2006, 2007 University of Szeged, Hungary 61e51764aSArtem Bityutskiy * 71e51764aSArtem Bityutskiy * This program is free software; you can redistribute it and/or modify it 81e51764aSArtem Bityutskiy * under the terms of the GNU General Public License version 2 as published by 91e51764aSArtem Bityutskiy * the Free Software Foundation. 101e51764aSArtem Bityutskiy * 111e51764aSArtem Bityutskiy * This program is distributed in the hope that it will be useful, but WITHOUT 121e51764aSArtem Bityutskiy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 131e51764aSArtem Bityutskiy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 141e51764aSArtem Bityutskiy * more details. 151e51764aSArtem Bityutskiy * 161e51764aSArtem Bityutskiy * You should have received a copy of the GNU General Public License along with 171e51764aSArtem Bityutskiy * this program; if not, write to the Free Software Foundation, Inc., 51 181e51764aSArtem Bityutskiy * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 191e51764aSArtem Bityutskiy * 201e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём) 211e51764aSArtem Bityutskiy * Adrian Hunter 221e51764aSArtem Bityutskiy * Zoltan Sogor 231e51764aSArtem Bityutskiy */ 241e51764aSArtem Bityutskiy 251e51764aSArtem Bityutskiy /* 261e51764aSArtem Bityutskiy * This file implements UBIFS I/O subsystem which provides various I/O-related 271e51764aSArtem Bityutskiy * helper functions (reading/writing/checking/validating nodes) and implements 281e51764aSArtem Bityutskiy * write-buffering support. Write buffers help to save space which otherwise 291e51764aSArtem Bityutskiy * would have been wasted for padding to the nearest minimal I/O unit boundary. 301e51764aSArtem Bityutskiy * Instead, data first goes to the write-buffer and is flushed when the 311e51764aSArtem Bityutskiy * buffer is full or when it is not used for some time (by timer). This is 321e51764aSArtem Bityutskiy * similar to the mechanism is used by JFFS2. 331e51764aSArtem Bityutskiy * 341e51764aSArtem Bityutskiy * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by 351e51764aSArtem Bityutskiy * mutexes defined inside these objects. Since sometimes upper-level code 361e51764aSArtem Bityutskiy * has to lock the write-buffer (e.g. journal space reservation code), many 371e51764aSArtem Bityutskiy * functions related to write-buffers have "nolock" suffix which means that the 381e51764aSArtem Bityutskiy * caller has to lock the write-buffer before calling this function. 391e51764aSArtem Bityutskiy * 401e51764aSArtem Bityutskiy * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not 411e51764aSArtem Bityutskiy * aligned, UBIFS starts the next node from the aligned address, and the padded 421e51764aSArtem Bityutskiy * bytes may contain any rubbish. In other words, UBIFS does not put padding 431e51764aSArtem Bityutskiy * bytes in those small gaps. Common headers of nodes store real node lengths, 441e51764aSArtem Bityutskiy * not aligned lengths. Indexing nodes also store real lengths in branches. 451e51764aSArtem Bityutskiy * 461e51764aSArtem Bityutskiy * UBIFS uses padding when it pads to the next min. I/O unit. In this case it 471e51764aSArtem Bityutskiy * uses padding nodes or padding bytes, if the padding node does not fit. 481e51764aSArtem Bityutskiy * 491e51764aSArtem Bityutskiy * All UBIFS nodes are protected by CRC checksums and UBIFS checks all nodes 501e51764aSArtem Bityutskiy * every time they are read from the flash media. 511e51764aSArtem Bityutskiy */ 521e51764aSArtem Bityutskiy 531e51764aSArtem Bityutskiy #include <linux/crc32.h> 545a0e3ad6STejun Heo #include <linux/slab.h> 551e51764aSArtem Bityutskiy #include "ubifs.h" 561e51764aSArtem Bityutskiy 571e51764aSArtem Bityutskiy /** 58ff46d7b3SAdrian Hunter * ubifs_ro_mode - switch UBIFS to read read-only mode. 59ff46d7b3SAdrian Hunter * @c: UBIFS file-system description object 60ff46d7b3SAdrian Hunter * @err: error code which is the reason of switching to R/O mode 61ff46d7b3SAdrian Hunter */ 62ff46d7b3SAdrian Hunter void ubifs_ro_mode(struct ubifs_info *c, int err) 63ff46d7b3SAdrian Hunter { 64ff46d7b3SAdrian Hunter if (!c->ro_media) { 65ff46d7b3SAdrian Hunter c->ro_media = 1; 66ccb3eba7SArtem Bityutskiy c->no_chk_data_crc = 0; 672fde99cbSZhangJieJing c->vfs_sb->s_flags |= MS_RDONLY; 68ff46d7b3SAdrian Hunter ubifs_warn("switched to read-only mode, error %d", err); 69ff46d7b3SAdrian Hunter dbg_dump_stack(); 70ff46d7b3SAdrian Hunter } 71ff46d7b3SAdrian Hunter } 72ff46d7b3SAdrian Hunter 73ff46d7b3SAdrian Hunter /** 741e51764aSArtem Bityutskiy * ubifs_check_node - check node. 751e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 761e51764aSArtem Bityutskiy * @buf: node to check 771e51764aSArtem Bityutskiy * @lnum: logical eraseblock number 781e51764aSArtem Bityutskiy * @offs: offset within the logical eraseblock 791e51764aSArtem Bityutskiy * @quiet: print no messages 806f7ab6d4SArtem Bityutskiy * @must_chk_crc: indicates whether to always check the CRC 811e51764aSArtem Bityutskiy * 821e51764aSArtem Bityutskiy * This function checks node magic number and CRC checksum. This function also 831e51764aSArtem Bityutskiy * validates node length to prevent UBIFS from becoming crazy when an attacker 841e51764aSArtem Bityutskiy * feeds it a file-system image with incorrect nodes. For example, too large 851e51764aSArtem Bityutskiy * node length in the common header could cause UBIFS to read memory outside of 861e51764aSArtem Bityutskiy * allocated buffer when checking the CRC checksum. 871e51764aSArtem Bityutskiy * 886f7ab6d4SArtem Bityutskiy * This function may skip data nodes CRC checking if @c->no_chk_data_crc is 896f7ab6d4SArtem Bityutskiy * true, which is controlled by corresponding UBIFS mount option. However, if 906f7ab6d4SArtem Bityutskiy * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is 916f7ab6d4SArtem Bityutskiy * checked. Similarly, if @c->always_chk_crc is true, @c->no_chk_data_crc is 926f7ab6d4SArtem Bityutskiy * ignored and CRC is checked. 936f7ab6d4SArtem Bityutskiy * 946f7ab6d4SArtem Bityutskiy * This function returns zero in case of success and %-EUCLEAN in case of bad 956f7ab6d4SArtem Bityutskiy * CRC or magic. 961e51764aSArtem Bityutskiy */ 971e51764aSArtem Bityutskiy int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, 986f7ab6d4SArtem Bityutskiy int offs, int quiet, int must_chk_crc) 991e51764aSArtem Bityutskiy { 1001e51764aSArtem Bityutskiy int err = -EINVAL, type, node_len; 1011e51764aSArtem Bityutskiy uint32_t crc, node_crc, magic; 1021e51764aSArtem Bityutskiy const struct ubifs_ch *ch = buf; 1031e51764aSArtem Bityutskiy 1041e51764aSArtem Bityutskiy ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0); 1051e51764aSArtem Bityutskiy ubifs_assert(!(offs & 7) && offs < c->leb_size); 1061e51764aSArtem Bityutskiy 1071e51764aSArtem Bityutskiy magic = le32_to_cpu(ch->magic); 1081e51764aSArtem Bityutskiy if (magic != UBIFS_NODE_MAGIC) { 1091e51764aSArtem Bityutskiy if (!quiet) 1101e51764aSArtem Bityutskiy ubifs_err("bad magic %#08x, expected %#08x", 1111e51764aSArtem Bityutskiy magic, UBIFS_NODE_MAGIC); 1121e51764aSArtem Bityutskiy err = -EUCLEAN; 1131e51764aSArtem Bityutskiy goto out; 1141e51764aSArtem Bityutskiy } 1151e51764aSArtem Bityutskiy 1161e51764aSArtem Bityutskiy type = ch->node_type; 1171e51764aSArtem Bityutskiy if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) { 1181e51764aSArtem Bityutskiy if (!quiet) 1191e51764aSArtem Bityutskiy ubifs_err("bad node type %d", type); 1201e51764aSArtem Bityutskiy goto out; 1211e51764aSArtem Bityutskiy } 1221e51764aSArtem Bityutskiy 1231e51764aSArtem Bityutskiy node_len = le32_to_cpu(ch->len); 1241e51764aSArtem Bityutskiy if (node_len + offs > c->leb_size) 1251e51764aSArtem Bityutskiy goto out_len; 1261e51764aSArtem Bityutskiy 1271e51764aSArtem Bityutskiy if (c->ranges[type].max_len == 0) { 1281e51764aSArtem Bityutskiy if (node_len != c->ranges[type].len) 1291e51764aSArtem Bityutskiy goto out_len; 1301e51764aSArtem Bityutskiy } else if (node_len < c->ranges[type].min_len || 1311e51764aSArtem Bityutskiy node_len > c->ranges[type].max_len) 1321e51764aSArtem Bityutskiy goto out_len; 1331e51764aSArtem Bityutskiy 1346f7ab6d4SArtem Bityutskiy if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc && 1356f7ab6d4SArtem Bityutskiy c->no_chk_data_crc) 1362953e73fSAdrian Hunter return 0; 1372953e73fSAdrian Hunter 1381e51764aSArtem Bityutskiy crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); 1391e51764aSArtem Bityutskiy node_crc = le32_to_cpu(ch->crc); 1401e51764aSArtem Bityutskiy if (crc != node_crc) { 1411e51764aSArtem Bityutskiy if (!quiet) 1421e51764aSArtem Bityutskiy ubifs_err("bad CRC: calculated %#08x, read %#08x", 1431e51764aSArtem Bityutskiy crc, node_crc); 1441e51764aSArtem Bityutskiy err = -EUCLEAN; 1451e51764aSArtem Bityutskiy goto out; 1461e51764aSArtem Bityutskiy } 1471e51764aSArtem Bityutskiy 1481e51764aSArtem Bityutskiy return 0; 1491e51764aSArtem Bityutskiy 1501e51764aSArtem Bityutskiy out_len: 1511e51764aSArtem Bityutskiy if (!quiet) 1521e51764aSArtem Bityutskiy ubifs_err("bad node length %d", node_len); 1531e51764aSArtem Bityutskiy out: 1541e51764aSArtem Bityutskiy if (!quiet) { 1551e51764aSArtem Bityutskiy ubifs_err("bad node at LEB %d:%d", lnum, offs); 1561e51764aSArtem Bityutskiy dbg_dump_node(c, buf); 1571e51764aSArtem Bityutskiy dbg_dump_stack(); 1581e51764aSArtem Bityutskiy } 1591e51764aSArtem Bityutskiy return err; 1601e51764aSArtem Bityutskiy } 1611e51764aSArtem Bityutskiy 1621e51764aSArtem Bityutskiy /** 1631e51764aSArtem Bityutskiy * ubifs_pad - pad flash space. 1641e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 1651e51764aSArtem Bityutskiy * @buf: buffer to put padding to 1661e51764aSArtem Bityutskiy * @pad: how many bytes to pad 1671e51764aSArtem Bityutskiy * 1681e51764aSArtem Bityutskiy * The flash media obliges us to write only in chunks of %c->min_io_size and 1691e51764aSArtem Bityutskiy * when we have to write less data we add padding node to the write-buffer and 1701e51764aSArtem Bityutskiy * pad it to the next minimal I/O unit's boundary. Padding nodes help when the 1711e51764aSArtem Bityutskiy * media is being scanned. If the amount of wasted space is not enough to fit a 1721e51764aSArtem Bityutskiy * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes 1731e51764aSArtem Bityutskiy * pattern (%UBIFS_PADDING_BYTE). 1741e51764aSArtem Bityutskiy * 1751e51764aSArtem Bityutskiy * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is 1761e51764aSArtem Bityutskiy * used. 1771e51764aSArtem Bityutskiy */ 1781e51764aSArtem Bityutskiy void ubifs_pad(const struct ubifs_info *c, void *buf, int pad) 1791e51764aSArtem Bityutskiy { 1801e51764aSArtem Bityutskiy uint32_t crc; 1811e51764aSArtem Bityutskiy 1821e51764aSArtem Bityutskiy ubifs_assert(pad >= 0 && !(pad & 7)); 1831e51764aSArtem Bityutskiy 1841e51764aSArtem Bityutskiy if (pad >= UBIFS_PAD_NODE_SZ) { 1851e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 1861e51764aSArtem Bityutskiy struct ubifs_pad_node *pad_node = buf; 1871e51764aSArtem Bityutskiy 1881e51764aSArtem Bityutskiy ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); 1891e51764aSArtem Bityutskiy ch->node_type = UBIFS_PAD_NODE; 1901e51764aSArtem Bityutskiy ch->group_type = UBIFS_NO_NODE_GROUP; 1911e51764aSArtem Bityutskiy ch->padding[0] = ch->padding[1] = 0; 1921e51764aSArtem Bityutskiy ch->sqnum = 0; 1931e51764aSArtem Bityutskiy ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ); 1941e51764aSArtem Bityutskiy pad -= UBIFS_PAD_NODE_SZ; 1951e51764aSArtem Bityutskiy pad_node->pad_len = cpu_to_le32(pad); 1961e51764aSArtem Bityutskiy crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8); 1971e51764aSArtem Bityutskiy ch->crc = cpu_to_le32(crc); 1981e51764aSArtem Bityutskiy memset(buf + UBIFS_PAD_NODE_SZ, 0, pad); 1991e51764aSArtem Bityutskiy } else if (pad > 0) 2001e51764aSArtem Bityutskiy /* Too little space, padding node won't fit */ 2011e51764aSArtem Bityutskiy memset(buf, UBIFS_PADDING_BYTE, pad); 2021e51764aSArtem Bityutskiy } 2031e51764aSArtem Bityutskiy 2041e51764aSArtem Bityutskiy /** 2051e51764aSArtem Bityutskiy * next_sqnum - get next sequence number. 2061e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2071e51764aSArtem Bityutskiy */ 2081e51764aSArtem Bityutskiy static unsigned long long next_sqnum(struct ubifs_info *c) 2091e51764aSArtem Bityutskiy { 2101e51764aSArtem Bityutskiy unsigned long long sqnum; 2111e51764aSArtem Bityutskiy 2121e51764aSArtem Bityutskiy spin_lock(&c->cnt_lock); 2131e51764aSArtem Bityutskiy sqnum = ++c->max_sqnum; 2141e51764aSArtem Bityutskiy spin_unlock(&c->cnt_lock); 2151e51764aSArtem Bityutskiy 2161e51764aSArtem Bityutskiy if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) { 2171e51764aSArtem Bityutskiy if (sqnum >= SQNUM_WATERMARK) { 2181e51764aSArtem Bityutskiy ubifs_err("sequence number overflow %llu, end of life", 2191e51764aSArtem Bityutskiy sqnum); 2201e51764aSArtem Bityutskiy ubifs_ro_mode(c, -EINVAL); 2211e51764aSArtem Bityutskiy } 2221e51764aSArtem Bityutskiy ubifs_warn("running out of sequence numbers, end of life soon"); 2231e51764aSArtem Bityutskiy } 2241e51764aSArtem Bityutskiy 2251e51764aSArtem Bityutskiy return sqnum; 2261e51764aSArtem Bityutskiy } 2271e51764aSArtem Bityutskiy 2281e51764aSArtem Bityutskiy /** 2291e51764aSArtem Bityutskiy * ubifs_prepare_node - prepare node to be written to flash. 2301e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2311e51764aSArtem Bityutskiy * @node: the node to pad 2321e51764aSArtem Bityutskiy * @len: node length 2331e51764aSArtem Bityutskiy * @pad: if the buffer has to be padded 2341e51764aSArtem Bityutskiy * 2351e51764aSArtem Bityutskiy * This function prepares node at @node to be written to the media - it 2361e51764aSArtem Bityutskiy * calculates node CRC, fills the common header, and adds proper padding up to 2371e51764aSArtem Bityutskiy * the next minimum I/O unit if @pad is not zero. 2381e51764aSArtem Bityutskiy */ 2391e51764aSArtem Bityutskiy void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad) 2401e51764aSArtem Bityutskiy { 2411e51764aSArtem Bityutskiy uint32_t crc; 2421e51764aSArtem Bityutskiy struct ubifs_ch *ch = node; 2431e51764aSArtem Bityutskiy unsigned long long sqnum = next_sqnum(c); 2441e51764aSArtem Bityutskiy 2451e51764aSArtem Bityutskiy ubifs_assert(len >= UBIFS_CH_SZ); 2461e51764aSArtem Bityutskiy 2471e51764aSArtem Bityutskiy ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); 2481e51764aSArtem Bityutskiy ch->len = cpu_to_le32(len); 2491e51764aSArtem Bityutskiy ch->group_type = UBIFS_NO_NODE_GROUP; 2501e51764aSArtem Bityutskiy ch->sqnum = cpu_to_le64(sqnum); 2511e51764aSArtem Bityutskiy ch->padding[0] = ch->padding[1] = 0; 2521e51764aSArtem Bityutskiy crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); 2531e51764aSArtem Bityutskiy ch->crc = cpu_to_le32(crc); 2541e51764aSArtem Bityutskiy 2551e51764aSArtem Bityutskiy if (pad) { 2561e51764aSArtem Bityutskiy len = ALIGN(len, 8); 2571e51764aSArtem Bityutskiy pad = ALIGN(len, c->min_io_size) - len; 2581e51764aSArtem Bityutskiy ubifs_pad(c, node + len, pad); 2591e51764aSArtem Bityutskiy } 2601e51764aSArtem Bityutskiy } 2611e51764aSArtem Bityutskiy 2621e51764aSArtem Bityutskiy /** 2631e51764aSArtem Bityutskiy * ubifs_prep_grp_node - prepare node of a group to be written to flash. 2641e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2651e51764aSArtem Bityutskiy * @node: the node to pad 2661e51764aSArtem Bityutskiy * @len: node length 2671e51764aSArtem Bityutskiy * @last: indicates the last node of the group 2681e51764aSArtem Bityutskiy * 2691e51764aSArtem Bityutskiy * This function prepares node at @node to be written to the media - it 2701e51764aSArtem Bityutskiy * calculates node CRC and fills the common header. 2711e51764aSArtem Bityutskiy */ 2721e51764aSArtem Bityutskiy void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last) 2731e51764aSArtem Bityutskiy { 2741e51764aSArtem Bityutskiy uint32_t crc; 2751e51764aSArtem Bityutskiy struct ubifs_ch *ch = node; 2761e51764aSArtem Bityutskiy unsigned long long sqnum = next_sqnum(c); 2771e51764aSArtem Bityutskiy 2781e51764aSArtem Bityutskiy ubifs_assert(len >= UBIFS_CH_SZ); 2791e51764aSArtem Bityutskiy 2801e51764aSArtem Bityutskiy ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); 2811e51764aSArtem Bityutskiy ch->len = cpu_to_le32(len); 2821e51764aSArtem Bityutskiy if (last) 2831e51764aSArtem Bityutskiy ch->group_type = UBIFS_LAST_OF_NODE_GROUP; 2841e51764aSArtem Bityutskiy else 2851e51764aSArtem Bityutskiy ch->group_type = UBIFS_IN_NODE_GROUP; 2861e51764aSArtem Bityutskiy ch->sqnum = cpu_to_le64(sqnum); 2871e51764aSArtem Bityutskiy ch->padding[0] = ch->padding[1] = 0; 2881e51764aSArtem Bityutskiy crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); 2891e51764aSArtem Bityutskiy ch->crc = cpu_to_le32(crc); 2901e51764aSArtem Bityutskiy } 2911e51764aSArtem Bityutskiy 2921e51764aSArtem Bityutskiy /** 2931e51764aSArtem Bityutskiy * wbuf_timer_callback - write-buffer timer callback function. 2941e51764aSArtem Bityutskiy * @data: timer data (write-buffer descriptor) 2951e51764aSArtem Bityutskiy * 2961e51764aSArtem Bityutskiy * This function is called when the write-buffer timer expires. 2971e51764aSArtem Bityutskiy */ 298f2c5dbd7SArtem Bityutskiy static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer) 2991e51764aSArtem Bityutskiy { 300f2c5dbd7SArtem Bityutskiy struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer); 3011e51764aSArtem Bityutskiy 30277a7ae58SArtem Bityutskiy dbg_io("jhead %s", dbg_jhead(wbuf->jhead)); 3031e51764aSArtem Bityutskiy wbuf->need_sync = 1; 3041e51764aSArtem Bityutskiy wbuf->c->need_wbuf_sync = 1; 3051e51764aSArtem Bityutskiy ubifs_wake_up_bgt(wbuf->c); 306f2c5dbd7SArtem Bityutskiy return HRTIMER_NORESTART; 3071e51764aSArtem Bityutskiy } 3081e51764aSArtem Bityutskiy 3091e51764aSArtem Bityutskiy /** 3101e51764aSArtem Bityutskiy * new_wbuf_timer - start new write-buffer timer. 3111e51764aSArtem Bityutskiy * @wbuf: write-buffer descriptor 3121e51764aSArtem Bityutskiy */ 3131e51764aSArtem Bityutskiy static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf) 3141e51764aSArtem Bityutskiy { 315f2c5dbd7SArtem Bityutskiy ubifs_assert(!hrtimer_active(&wbuf->timer)); 3161e51764aSArtem Bityutskiy 3170b335b9dSArtem Bityutskiy if (wbuf->no_timer) 3181e51764aSArtem Bityutskiy return; 31977a7ae58SArtem Bityutskiy dbg_io("set timer for jhead %s, %llu-%llu millisecs", 32077a7ae58SArtem Bityutskiy dbg_jhead(wbuf->jhead), 32144737589SAdrian Hunter div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC), 32244737589SAdrian Hunter div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta, 32344737589SAdrian Hunter USEC_PER_SEC)); 324f2c5dbd7SArtem Bityutskiy hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta, 325f2c5dbd7SArtem Bityutskiy HRTIMER_MODE_REL); 3261e51764aSArtem Bityutskiy } 3271e51764aSArtem Bityutskiy 3281e51764aSArtem Bityutskiy /** 3291e51764aSArtem Bityutskiy * cancel_wbuf_timer - cancel write-buffer timer. 3301e51764aSArtem Bityutskiy * @wbuf: write-buffer descriptor 3311e51764aSArtem Bityutskiy */ 3321e51764aSArtem Bityutskiy static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf) 3331e51764aSArtem Bityutskiy { 3340b335b9dSArtem Bityutskiy if (wbuf->no_timer) 3350b335b9dSArtem Bityutskiy return; 3361e51764aSArtem Bityutskiy wbuf->need_sync = 0; 337f2c5dbd7SArtem Bityutskiy hrtimer_cancel(&wbuf->timer); 3381e51764aSArtem Bityutskiy } 3391e51764aSArtem Bityutskiy 3401e51764aSArtem Bityutskiy /** 3411e51764aSArtem Bityutskiy * ubifs_wbuf_sync_nolock - synchronize write-buffer. 3421e51764aSArtem Bityutskiy * @wbuf: write-buffer to synchronize 3431e51764aSArtem Bityutskiy * 3441e51764aSArtem Bityutskiy * This function synchronizes write-buffer @buf and returns zero in case of 3451e51764aSArtem Bityutskiy * success or a negative error code in case of failure. 3461e51764aSArtem Bityutskiy */ 3471e51764aSArtem Bityutskiy int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf) 3481e51764aSArtem Bityutskiy { 3491e51764aSArtem Bityutskiy struct ubifs_info *c = wbuf->c; 3501e51764aSArtem Bityutskiy int err, dirt; 3511e51764aSArtem Bityutskiy 3521e51764aSArtem Bityutskiy cancel_wbuf_timer_nolock(wbuf); 3531e51764aSArtem Bityutskiy if (!wbuf->used || wbuf->lnum == -1) 3541e51764aSArtem Bityutskiy /* Write-buffer is empty or not seeked */ 3551e51764aSArtem Bityutskiy return 0; 3561e51764aSArtem Bityutskiy 35777a7ae58SArtem Bityutskiy dbg_io("LEB %d:%d, %d bytes, jhead %s", 35877a7ae58SArtem Bityutskiy wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead)); 3591e51764aSArtem Bityutskiy ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY)); 3601e51764aSArtem Bityutskiy ubifs_assert(!(wbuf->avail & 7)); 3611e51764aSArtem Bityutskiy ubifs_assert(wbuf->offs + c->min_io_size <= c->leb_size); 3621e51764aSArtem Bityutskiy 3631e51764aSArtem Bityutskiy if (c->ro_media) 3641e51764aSArtem Bityutskiy return -EROFS; 3651e51764aSArtem Bityutskiy 3661e51764aSArtem Bityutskiy ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail); 3671e51764aSArtem Bityutskiy err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs, 3681e51764aSArtem Bityutskiy c->min_io_size, wbuf->dtype); 3691e51764aSArtem Bityutskiy if (err) { 3701e51764aSArtem Bityutskiy ubifs_err("cannot write %d bytes to LEB %d:%d", 3711e51764aSArtem Bityutskiy c->min_io_size, wbuf->lnum, wbuf->offs); 3721e51764aSArtem Bityutskiy dbg_dump_stack(); 3731e51764aSArtem Bityutskiy return err; 3741e51764aSArtem Bityutskiy } 3751e51764aSArtem Bityutskiy 3761e51764aSArtem Bityutskiy dirt = wbuf->avail; 3771e51764aSArtem Bityutskiy 3781e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 3791e51764aSArtem Bityutskiy wbuf->offs += c->min_io_size; 3801e51764aSArtem Bityutskiy wbuf->avail = c->min_io_size; 3811e51764aSArtem Bityutskiy wbuf->used = 0; 3821e51764aSArtem Bityutskiy wbuf->next_ino = 0; 3831e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 3841e51764aSArtem Bityutskiy 3851e51764aSArtem Bityutskiy if (wbuf->sync_callback) 3861e51764aSArtem Bityutskiy err = wbuf->sync_callback(c, wbuf->lnum, 3871e51764aSArtem Bityutskiy c->leb_size - wbuf->offs, dirt); 3881e51764aSArtem Bityutskiy return err; 3891e51764aSArtem Bityutskiy } 3901e51764aSArtem Bityutskiy 3911e51764aSArtem Bityutskiy /** 3921e51764aSArtem Bityutskiy * ubifs_wbuf_seek_nolock - seek write-buffer. 3931e51764aSArtem Bityutskiy * @wbuf: write-buffer 3941e51764aSArtem Bityutskiy * @lnum: logical eraseblock number to seek to 3951e51764aSArtem Bityutskiy * @offs: logical eraseblock offset to seek to 3961e51764aSArtem Bityutskiy * @dtype: data type 3971e51764aSArtem Bityutskiy * 398cb54ef8bSArtem Bityutskiy * This function targets the write-buffer to logical eraseblock @lnum:@offs. 3991e51764aSArtem Bityutskiy * The write-buffer is synchronized if it is not empty. Returns zero in case of 4001e51764aSArtem Bityutskiy * success and a negative error code in case of failure. 4011e51764aSArtem Bityutskiy */ 4021e51764aSArtem Bityutskiy int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs, 4031e51764aSArtem Bityutskiy int dtype) 4041e51764aSArtem Bityutskiy { 4051e51764aSArtem Bityutskiy const struct ubifs_info *c = wbuf->c; 4061e51764aSArtem Bityutskiy 40777a7ae58SArtem Bityutskiy dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead)); 4081e51764aSArtem Bityutskiy ubifs_assert(lnum >= 0 && lnum < c->leb_cnt); 4091e51764aSArtem Bityutskiy ubifs_assert(offs >= 0 && offs <= c->leb_size); 4101e51764aSArtem Bityutskiy ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7)); 4111e51764aSArtem Bityutskiy ubifs_assert(lnum != wbuf->lnum); 4121e51764aSArtem Bityutskiy 4131e51764aSArtem Bityutskiy if (wbuf->used > 0) { 4141e51764aSArtem Bityutskiy int err = ubifs_wbuf_sync_nolock(wbuf); 4151e51764aSArtem Bityutskiy 4161e51764aSArtem Bityutskiy if (err) 4171e51764aSArtem Bityutskiy return err; 4181e51764aSArtem Bityutskiy } 4191e51764aSArtem Bityutskiy 4201e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 4211e51764aSArtem Bityutskiy wbuf->lnum = lnum; 4221e51764aSArtem Bityutskiy wbuf->offs = offs; 4231e51764aSArtem Bityutskiy wbuf->avail = c->min_io_size; 4241e51764aSArtem Bityutskiy wbuf->used = 0; 4251e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 4261e51764aSArtem Bityutskiy wbuf->dtype = dtype; 4271e51764aSArtem Bityutskiy 4281e51764aSArtem Bityutskiy return 0; 4291e51764aSArtem Bityutskiy } 4301e51764aSArtem Bityutskiy 4311e51764aSArtem Bityutskiy /** 4321e51764aSArtem Bityutskiy * ubifs_bg_wbufs_sync - synchronize write-buffers. 4331e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 4341e51764aSArtem Bityutskiy * 4351e51764aSArtem Bityutskiy * This function is called by background thread to synchronize write-buffers. 4361e51764aSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 4371e51764aSArtem Bityutskiy * failure. 4381e51764aSArtem Bityutskiy */ 4391e51764aSArtem Bityutskiy int ubifs_bg_wbufs_sync(struct ubifs_info *c) 4401e51764aSArtem Bityutskiy { 4411e51764aSArtem Bityutskiy int err, i; 4421e51764aSArtem Bityutskiy 4431e51764aSArtem Bityutskiy if (!c->need_wbuf_sync) 4441e51764aSArtem Bityutskiy return 0; 4451e51764aSArtem Bityutskiy c->need_wbuf_sync = 0; 4461e51764aSArtem Bityutskiy 4471e51764aSArtem Bityutskiy if (c->ro_media) { 4481e51764aSArtem Bityutskiy err = -EROFS; 4491e51764aSArtem Bityutskiy goto out_timers; 4501e51764aSArtem Bityutskiy } 4511e51764aSArtem Bityutskiy 4521e51764aSArtem Bityutskiy dbg_io("synchronize"); 4531e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 4541e51764aSArtem Bityutskiy struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; 4551e51764aSArtem Bityutskiy 4561e51764aSArtem Bityutskiy cond_resched(); 4571e51764aSArtem Bityutskiy 4581e51764aSArtem Bityutskiy /* 4591e51764aSArtem Bityutskiy * If the mutex is locked then wbuf is being changed, so 4601e51764aSArtem Bityutskiy * synchronization is not necessary. 4611e51764aSArtem Bityutskiy */ 4621e51764aSArtem Bityutskiy if (mutex_is_locked(&wbuf->io_mutex)) 4631e51764aSArtem Bityutskiy continue; 4641e51764aSArtem Bityutskiy 4651e51764aSArtem Bityutskiy mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); 4661e51764aSArtem Bityutskiy if (!wbuf->need_sync) { 4671e51764aSArtem Bityutskiy mutex_unlock(&wbuf->io_mutex); 4681e51764aSArtem Bityutskiy continue; 4691e51764aSArtem Bityutskiy } 4701e51764aSArtem Bityutskiy 4711e51764aSArtem Bityutskiy err = ubifs_wbuf_sync_nolock(wbuf); 4721e51764aSArtem Bityutskiy mutex_unlock(&wbuf->io_mutex); 4731e51764aSArtem Bityutskiy if (err) { 4741e51764aSArtem Bityutskiy ubifs_err("cannot sync write-buffer, error %d", err); 4751e51764aSArtem Bityutskiy ubifs_ro_mode(c, err); 4761e51764aSArtem Bityutskiy goto out_timers; 4771e51764aSArtem Bityutskiy } 4781e51764aSArtem Bityutskiy } 4791e51764aSArtem Bityutskiy 4801e51764aSArtem Bityutskiy return 0; 4811e51764aSArtem Bityutskiy 4821e51764aSArtem Bityutskiy out_timers: 4831e51764aSArtem Bityutskiy /* Cancel all timers to prevent repeated errors */ 4841e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 4851e51764aSArtem Bityutskiy struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; 4861e51764aSArtem Bityutskiy 4871e51764aSArtem Bityutskiy mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); 4881e51764aSArtem Bityutskiy cancel_wbuf_timer_nolock(wbuf); 4891e51764aSArtem Bityutskiy mutex_unlock(&wbuf->io_mutex); 4901e51764aSArtem Bityutskiy } 4911e51764aSArtem Bityutskiy return err; 4921e51764aSArtem Bityutskiy } 4931e51764aSArtem Bityutskiy 4941e51764aSArtem Bityutskiy /** 4951e51764aSArtem Bityutskiy * ubifs_wbuf_write_nolock - write data to flash via write-buffer. 4961e51764aSArtem Bityutskiy * @wbuf: write-buffer 4971e51764aSArtem Bityutskiy * @buf: node to write 4981e51764aSArtem Bityutskiy * @len: node length 4991e51764aSArtem Bityutskiy * 5001e51764aSArtem Bityutskiy * This function writes data to flash via write-buffer @wbuf. This means that 5011e51764aSArtem Bityutskiy * the last piece of the node won't reach the flash media immediately if it 5021e51764aSArtem Bityutskiy * does not take whole minimal I/O unit. Instead, the node will sit in RAM 5031e51764aSArtem Bityutskiy * until the write-buffer is synchronized (e.g., by timer). 5041e51764aSArtem Bityutskiy * 5051e51764aSArtem Bityutskiy * This function returns zero in case of success and a negative error code in 5061e51764aSArtem Bityutskiy * case of failure. If the node cannot be written because there is no more 5071e51764aSArtem Bityutskiy * space in this logical eraseblock, %-ENOSPC is returned. 5081e51764aSArtem Bityutskiy */ 5091e51764aSArtem Bityutskiy int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len) 5101e51764aSArtem Bityutskiy { 5111e51764aSArtem Bityutskiy struct ubifs_info *c = wbuf->c; 5121e51764aSArtem Bityutskiy int err, written, n, aligned_len = ALIGN(len, 8), offs; 5131e51764aSArtem Bityutskiy 51477a7ae58SArtem Bityutskiy dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len, 51577a7ae58SArtem Bityutskiy dbg_ntype(((struct ubifs_ch *)buf)->node_type), 51677a7ae58SArtem Bityutskiy dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used); 5171e51764aSArtem Bityutskiy ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt); 5181e51764aSArtem Bityutskiy ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0); 5191e51764aSArtem Bityutskiy ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size); 5201e51764aSArtem Bityutskiy ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size); 5211e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&wbuf->io_mutex)); 5221e51764aSArtem Bityutskiy 5231e51764aSArtem Bityutskiy if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) { 5241e51764aSArtem Bityutskiy err = -ENOSPC; 5251e51764aSArtem Bityutskiy goto out; 5261e51764aSArtem Bityutskiy } 5271e51764aSArtem Bityutskiy 5281e51764aSArtem Bityutskiy cancel_wbuf_timer_nolock(wbuf); 5291e51764aSArtem Bityutskiy 5301e51764aSArtem Bityutskiy if (c->ro_media) 5311e51764aSArtem Bityutskiy return -EROFS; 5321e51764aSArtem Bityutskiy 5331e51764aSArtem Bityutskiy if (aligned_len <= wbuf->avail) { 5341e51764aSArtem Bityutskiy /* 5351e51764aSArtem Bityutskiy * The node is not very large and fits entirely within 5361e51764aSArtem Bityutskiy * write-buffer. 5371e51764aSArtem Bityutskiy */ 5381e51764aSArtem Bityutskiy memcpy(wbuf->buf + wbuf->used, buf, len); 5391e51764aSArtem Bityutskiy 5401e51764aSArtem Bityutskiy if (aligned_len == wbuf->avail) { 54177a7ae58SArtem Bityutskiy dbg_io("flush jhead %s wbuf to LEB %d:%d", 54277a7ae58SArtem Bityutskiy dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); 5431e51764aSArtem Bityutskiy err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, 5441e51764aSArtem Bityutskiy wbuf->offs, c->min_io_size, 5451e51764aSArtem Bityutskiy wbuf->dtype); 5461e51764aSArtem Bityutskiy if (err) 5471e51764aSArtem Bityutskiy goto out; 5481e51764aSArtem Bityutskiy 5491e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 5501e51764aSArtem Bityutskiy wbuf->offs += c->min_io_size; 5511e51764aSArtem Bityutskiy wbuf->avail = c->min_io_size; 5521e51764aSArtem Bityutskiy wbuf->used = 0; 5531e51764aSArtem Bityutskiy wbuf->next_ino = 0; 5541e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 5551e51764aSArtem Bityutskiy } else { 5561e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 5571e51764aSArtem Bityutskiy wbuf->avail -= aligned_len; 5581e51764aSArtem Bityutskiy wbuf->used += aligned_len; 5591e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 5601e51764aSArtem Bityutskiy } 5611e51764aSArtem Bityutskiy 5621e51764aSArtem Bityutskiy goto exit; 5631e51764aSArtem Bityutskiy } 5641e51764aSArtem Bityutskiy 5651e51764aSArtem Bityutskiy /* 5661e51764aSArtem Bityutskiy * The node is large enough and does not fit entirely within current 5671e51764aSArtem Bityutskiy * minimal I/O unit. We have to fill and flush write-buffer and switch 5681e51764aSArtem Bityutskiy * to the next min. I/O unit. 5691e51764aSArtem Bityutskiy */ 57077a7ae58SArtem Bityutskiy dbg_io("flush jhead %s wbuf to LEB %d:%d", 57177a7ae58SArtem Bityutskiy dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); 5721e51764aSArtem Bityutskiy memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail); 5731e51764aSArtem Bityutskiy err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs, 5741e51764aSArtem Bityutskiy c->min_io_size, wbuf->dtype); 5751e51764aSArtem Bityutskiy if (err) 5761e51764aSArtem Bityutskiy goto out; 5771e51764aSArtem Bityutskiy 5781e51764aSArtem Bityutskiy offs = wbuf->offs + c->min_io_size; 5791e51764aSArtem Bityutskiy len -= wbuf->avail; 5801e51764aSArtem Bityutskiy aligned_len -= wbuf->avail; 5811e51764aSArtem Bityutskiy written = wbuf->avail; 5821e51764aSArtem Bityutskiy 5831e51764aSArtem Bityutskiy /* 5841e51764aSArtem Bityutskiy * The remaining data may take more whole min. I/O units, so write the 5851e51764aSArtem Bityutskiy * remains multiple to min. I/O unit size directly to the flash media. 5861e51764aSArtem Bityutskiy * We align node length to 8-byte boundary because we anyway flash wbuf 5871e51764aSArtem Bityutskiy * if the remaining space is less than 8 bytes. 5881e51764aSArtem Bityutskiy */ 5891e51764aSArtem Bityutskiy n = aligned_len >> c->min_io_shift; 5901e51764aSArtem Bityutskiy if (n) { 5911e51764aSArtem Bityutskiy n <<= c->min_io_shift; 5921e51764aSArtem Bityutskiy dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs); 5931e51764aSArtem Bityutskiy err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n, 5941e51764aSArtem Bityutskiy wbuf->dtype); 5951e51764aSArtem Bityutskiy if (err) 5961e51764aSArtem Bityutskiy goto out; 5971e51764aSArtem Bityutskiy offs += n; 5981e51764aSArtem Bityutskiy aligned_len -= n; 5991e51764aSArtem Bityutskiy len -= n; 6001e51764aSArtem Bityutskiy written += n; 6011e51764aSArtem Bityutskiy } 6021e51764aSArtem Bityutskiy 6031e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 6041e51764aSArtem Bityutskiy if (aligned_len) 6051e51764aSArtem Bityutskiy /* 6061e51764aSArtem Bityutskiy * And now we have what's left and what does not take whole 6071e51764aSArtem Bityutskiy * min. I/O unit, so write it to the write-buffer and we are 6081e51764aSArtem Bityutskiy * done. 6091e51764aSArtem Bityutskiy */ 6101e51764aSArtem Bityutskiy memcpy(wbuf->buf, buf + written, len); 6111e51764aSArtem Bityutskiy 6121e51764aSArtem Bityutskiy wbuf->offs = offs; 6131e51764aSArtem Bityutskiy wbuf->used = aligned_len; 6141e51764aSArtem Bityutskiy wbuf->avail = c->min_io_size - aligned_len; 6151e51764aSArtem Bityutskiy wbuf->next_ino = 0; 6161e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 6171e51764aSArtem Bityutskiy 6181e51764aSArtem Bityutskiy exit: 6191e51764aSArtem Bityutskiy if (wbuf->sync_callback) { 6201e51764aSArtem Bityutskiy int free = c->leb_size - wbuf->offs - wbuf->used; 6211e51764aSArtem Bityutskiy 6221e51764aSArtem Bityutskiy err = wbuf->sync_callback(c, wbuf->lnum, free, 0); 6231e51764aSArtem Bityutskiy if (err) 6241e51764aSArtem Bityutskiy goto out; 6251e51764aSArtem Bityutskiy } 6261e51764aSArtem Bityutskiy 6271e51764aSArtem Bityutskiy if (wbuf->used) 6281e51764aSArtem Bityutskiy new_wbuf_timer_nolock(wbuf); 6291e51764aSArtem Bityutskiy 6301e51764aSArtem Bityutskiy return 0; 6311e51764aSArtem Bityutskiy 6321e51764aSArtem Bityutskiy out: 6331e51764aSArtem Bityutskiy ubifs_err("cannot write %d bytes to LEB %d:%d, error %d", 6341e51764aSArtem Bityutskiy len, wbuf->lnum, wbuf->offs, err); 6351e51764aSArtem Bityutskiy dbg_dump_node(c, buf); 6361e51764aSArtem Bityutskiy dbg_dump_stack(); 6371e51764aSArtem Bityutskiy dbg_dump_leb(c, wbuf->lnum); 6381e51764aSArtem Bityutskiy return err; 6391e51764aSArtem Bityutskiy } 6401e51764aSArtem Bityutskiy 6411e51764aSArtem Bityutskiy /** 6421e51764aSArtem Bityutskiy * ubifs_write_node - write node to the media. 6431e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 6441e51764aSArtem Bityutskiy * @buf: the node to write 6451e51764aSArtem Bityutskiy * @len: node length 6461e51764aSArtem Bityutskiy * @lnum: logical eraseblock number 6471e51764aSArtem Bityutskiy * @offs: offset within the logical eraseblock 6481e51764aSArtem Bityutskiy * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN) 6491e51764aSArtem Bityutskiy * 6501e51764aSArtem Bityutskiy * This function automatically fills node magic number, assigns sequence 6511e51764aSArtem Bityutskiy * number, and calculates node CRC checksum. The length of the @buf buffer has 6521e51764aSArtem Bityutskiy * to be aligned to the minimal I/O unit size. This function automatically 6531e51764aSArtem Bityutskiy * appends padding node and padding bytes if needed. Returns zero in case of 6541e51764aSArtem Bityutskiy * success and a negative error code in case of failure. 6551e51764aSArtem Bityutskiy */ 6561e51764aSArtem Bityutskiy int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum, 6571e51764aSArtem Bityutskiy int offs, int dtype) 6581e51764aSArtem Bityutskiy { 6591e51764aSArtem Bityutskiy int err, buf_len = ALIGN(len, c->min_io_size); 6601e51764aSArtem Bityutskiy 6611e51764aSArtem Bityutskiy dbg_io("LEB %d:%d, %s, length %d (aligned %d)", 6621e51764aSArtem Bityutskiy lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len, 6631e51764aSArtem Bityutskiy buf_len); 6641e51764aSArtem Bityutskiy ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0); 6651e51764aSArtem Bityutskiy ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size); 6661e51764aSArtem Bityutskiy 6671e51764aSArtem Bityutskiy if (c->ro_media) 6681e51764aSArtem Bityutskiy return -EROFS; 6691e51764aSArtem Bityutskiy 6701e51764aSArtem Bityutskiy ubifs_prepare_node(c, buf, len, 1); 6711e51764aSArtem Bityutskiy err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype); 6721e51764aSArtem Bityutskiy if (err) { 6731e51764aSArtem Bityutskiy ubifs_err("cannot write %d bytes to LEB %d:%d, error %d", 6741e51764aSArtem Bityutskiy buf_len, lnum, offs, err); 6751e51764aSArtem Bityutskiy dbg_dump_node(c, buf); 6761e51764aSArtem Bityutskiy dbg_dump_stack(); 6771e51764aSArtem Bityutskiy } 6781e51764aSArtem Bityutskiy 6791e51764aSArtem Bityutskiy return err; 6801e51764aSArtem Bityutskiy } 6811e51764aSArtem Bityutskiy 6821e51764aSArtem Bityutskiy /** 6831e51764aSArtem Bityutskiy * ubifs_read_node_wbuf - read node from the media or write-buffer. 6841e51764aSArtem Bityutskiy * @wbuf: wbuf to check for un-written data 6851e51764aSArtem Bityutskiy * @buf: buffer to read to 6861e51764aSArtem Bityutskiy * @type: node type 6871e51764aSArtem Bityutskiy * @len: node length 6881e51764aSArtem Bityutskiy * @lnum: logical eraseblock number 6891e51764aSArtem Bityutskiy * @offs: offset within the logical eraseblock 6901e51764aSArtem Bityutskiy * 6911e51764aSArtem Bityutskiy * This function reads a node of known type and length, checks it and stores 6921e51764aSArtem Bityutskiy * in @buf. If the node partially or fully sits in the write-buffer, this 6931e51764aSArtem Bityutskiy * function takes data from the buffer, otherwise it reads the flash media. 6941e51764aSArtem Bityutskiy * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative 6951e51764aSArtem Bityutskiy * error code in case of failure. 6961e51764aSArtem Bityutskiy */ 6971e51764aSArtem Bityutskiy int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len, 6981e51764aSArtem Bityutskiy int lnum, int offs) 6991e51764aSArtem Bityutskiy { 7001e51764aSArtem Bityutskiy const struct ubifs_info *c = wbuf->c; 7011e51764aSArtem Bityutskiy int err, rlen, overlap; 7021e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 7031e51764aSArtem Bityutskiy 70477a7ae58SArtem Bityutskiy dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs, 70577a7ae58SArtem Bityutskiy dbg_ntype(type), len, dbg_jhead(wbuf->jhead)); 7061e51764aSArtem Bityutskiy ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); 7071e51764aSArtem Bityutskiy ubifs_assert(!(offs & 7) && offs < c->leb_size); 7081e51764aSArtem Bityutskiy ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT); 7091e51764aSArtem Bityutskiy 7101e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 7111e51764aSArtem Bityutskiy overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); 7121e51764aSArtem Bityutskiy if (!overlap) { 7131e51764aSArtem Bityutskiy /* We may safely unlock the write-buffer and read the data */ 7141e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 7151e51764aSArtem Bityutskiy return ubifs_read_node(c, buf, type, len, lnum, offs); 7161e51764aSArtem Bityutskiy } 7171e51764aSArtem Bityutskiy 7181e51764aSArtem Bityutskiy /* Don't read under wbuf */ 7191e51764aSArtem Bityutskiy rlen = wbuf->offs - offs; 7201e51764aSArtem Bityutskiy if (rlen < 0) 7211e51764aSArtem Bityutskiy rlen = 0; 7221e51764aSArtem Bityutskiy 7231e51764aSArtem Bityutskiy /* Copy the rest from the write-buffer */ 7241e51764aSArtem Bityutskiy memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); 7251e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 7261e51764aSArtem Bityutskiy 7271e51764aSArtem Bityutskiy if (rlen > 0) { 7281e51764aSArtem Bityutskiy /* Read everything that goes before write-buffer */ 7291e51764aSArtem Bityutskiy err = ubi_read(c->ubi, lnum, buf, offs, rlen); 7301e51764aSArtem Bityutskiy if (err && err != -EBADMSG) { 7311e51764aSArtem Bityutskiy ubifs_err("failed to read node %d from LEB %d:%d, " 7321e51764aSArtem Bityutskiy "error %d", type, lnum, offs, err); 7331e51764aSArtem Bityutskiy dbg_dump_stack(); 7341e51764aSArtem Bityutskiy return err; 7351e51764aSArtem Bityutskiy } 7361e51764aSArtem Bityutskiy } 7371e51764aSArtem Bityutskiy 7381e51764aSArtem Bityutskiy if (type != ch->node_type) { 7391e51764aSArtem Bityutskiy ubifs_err("bad node type (%d but expected %d)", 7401e51764aSArtem Bityutskiy ch->node_type, type); 7411e51764aSArtem Bityutskiy goto out; 7421e51764aSArtem Bityutskiy } 7431e51764aSArtem Bityutskiy 7442953e73fSAdrian Hunter err = ubifs_check_node(c, buf, lnum, offs, 0, 0); 7451e51764aSArtem Bityutskiy if (err) { 7461e51764aSArtem Bityutskiy ubifs_err("expected node type %d", type); 7471e51764aSArtem Bityutskiy return err; 7481e51764aSArtem Bityutskiy } 7491e51764aSArtem Bityutskiy 7501e51764aSArtem Bityutskiy rlen = le32_to_cpu(ch->len); 7511e51764aSArtem Bityutskiy if (rlen != len) { 7521e51764aSArtem Bityutskiy ubifs_err("bad node length %d, expected %d", rlen, len); 7531e51764aSArtem Bityutskiy goto out; 7541e51764aSArtem Bityutskiy } 7551e51764aSArtem Bityutskiy 7561e51764aSArtem Bityutskiy return 0; 7571e51764aSArtem Bityutskiy 7581e51764aSArtem Bityutskiy out: 7591e51764aSArtem Bityutskiy ubifs_err("bad node at LEB %d:%d", lnum, offs); 7601e51764aSArtem Bityutskiy dbg_dump_node(c, buf); 7611e51764aSArtem Bityutskiy dbg_dump_stack(); 7621e51764aSArtem Bityutskiy return -EINVAL; 7631e51764aSArtem Bityutskiy } 7641e51764aSArtem Bityutskiy 7651e51764aSArtem Bityutskiy /** 7661e51764aSArtem Bityutskiy * ubifs_read_node - read node. 7671e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 7681e51764aSArtem Bityutskiy * @buf: buffer to read to 7691e51764aSArtem Bityutskiy * @type: node type 7701e51764aSArtem Bityutskiy * @len: node length (not aligned) 7711e51764aSArtem Bityutskiy * @lnum: logical eraseblock number 7721e51764aSArtem Bityutskiy * @offs: offset within the logical eraseblock 7731e51764aSArtem Bityutskiy * 7741e51764aSArtem Bityutskiy * This function reads a node of known type and and length, checks it and 7751e51764aSArtem Bityutskiy * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched 7761e51764aSArtem Bityutskiy * and a negative error code in case of failure. 7771e51764aSArtem Bityutskiy */ 7781e51764aSArtem Bityutskiy int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len, 7791e51764aSArtem Bityutskiy int lnum, int offs) 7801e51764aSArtem Bityutskiy { 7811e51764aSArtem Bityutskiy int err, l; 7821e51764aSArtem Bityutskiy struct ubifs_ch *ch = buf; 7831e51764aSArtem Bityutskiy 7841e51764aSArtem Bityutskiy dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len); 7851e51764aSArtem Bityutskiy ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0); 7861e51764aSArtem Bityutskiy ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size); 7871e51764aSArtem Bityutskiy ubifs_assert(!(offs & 7) && offs < c->leb_size); 7881e51764aSArtem Bityutskiy ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT); 7891e51764aSArtem Bityutskiy 7901e51764aSArtem Bityutskiy err = ubi_read(c->ubi, lnum, buf, offs, len); 7911e51764aSArtem Bityutskiy if (err && err != -EBADMSG) { 7921e51764aSArtem Bityutskiy ubifs_err("cannot read node %d from LEB %d:%d, error %d", 7931e51764aSArtem Bityutskiy type, lnum, offs, err); 7941e51764aSArtem Bityutskiy return err; 7951e51764aSArtem Bityutskiy } 7961e51764aSArtem Bityutskiy 7971e51764aSArtem Bityutskiy if (type != ch->node_type) { 7981e51764aSArtem Bityutskiy ubifs_err("bad node type (%d but expected %d)", 7991e51764aSArtem Bityutskiy ch->node_type, type); 8001e51764aSArtem Bityutskiy goto out; 8011e51764aSArtem Bityutskiy } 8021e51764aSArtem Bityutskiy 8032953e73fSAdrian Hunter err = ubifs_check_node(c, buf, lnum, offs, 0, 0); 8041e51764aSArtem Bityutskiy if (err) { 8051e51764aSArtem Bityutskiy ubifs_err("expected node type %d", type); 8061e51764aSArtem Bityutskiy return err; 8071e51764aSArtem Bityutskiy } 8081e51764aSArtem Bityutskiy 8091e51764aSArtem Bityutskiy l = le32_to_cpu(ch->len); 8101e51764aSArtem Bityutskiy if (l != len) { 8111e51764aSArtem Bityutskiy ubifs_err("bad node length %d, expected %d", l, len); 8121e51764aSArtem Bityutskiy goto out; 8131e51764aSArtem Bityutskiy } 8141e51764aSArtem Bityutskiy 8151e51764aSArtem Bityutskiy return 0; 8161e51764aSArtem Bityutskiy 8171e51764aSArtem Bityutskiy out: 8183a8fa0edSArtem Bityutskiy ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs, 8193a8fa0edSArtem Bityutskiy ubi_is_mapped(c->ubi, lnum)); 8201e51764aSArtem Bityutskiy dbg_dump_node(c, buf); 8211e51764aSArtem Bityutskiy dbg_dump_stack(); 8221e51764aSArtem Bityutskiy return -EINVAL; 8231e51764aSArtem Bityutskiy } 8241e51764aSArtem Bityutskiy 8251e51764aSArtem Bityutskiy /** 8261e51764aSArtem Bityutskiy * ubifs_wbuf_init - initialize write-buffer. 8271e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8281e51764aSArtem Bityutskiy * @wbuf: write-buffer to initialize 8291e51764aSArtem Bityutskiy * 830cb54ef8bSArtem Bityutskiy * This function initializes write-buffer. Returns zero in case of success 8311e51764aSArtem Bityutskiy * %-ENOMEM in case of failure. 8321e51764aSArtem Bityutskiy */ 8331e51764aSArtem Bityutskiy int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf) 8341e51764aSArtem Bityutskiy { 8351e51764aSArtem Bityutskiy size_t size; 8361e51764aSArtem Bityutskiy 8371e51764aSArtem Bityutskiy wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL); 8381e51764aSArtem Bityutskiy if (!wbuf->buf) 8391e51764aSArtem Bityutskiy return -ENOMEM; 8401e51764aSArtem Bityutskiy 8411e51764aSArtem Bityutskiy size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t); 8421e51764aSArtem Bityutskiy wbuf->inodes = kmalloc(size, GFP_KERNEL); 8431e51764aSArtem Bityutskiy if (!wbuf->inodes) { 8441e51764aSArtem Bityutskiy kfree(wbuf->buf); 8451e51764aSArtem Bityutskiy wbuf->buf = NULL; 8461e51764aSArtem Bityutskiy return -ENOMEM; 8471e51764aSArtem Bityutskiy } 8481e51764aSArtem Bityutskiy 8491e51764aSArtem Bityutskiy wbuf->used = 0; 8501e51764aSArtem Bityutskiy wbuf->lnum = wbuf->offs = -1; 8511e51764aSArtem Bityutskiy wbuf->avail = c->min_io_size; 8521e51764aSArtem Bityutskiy wbuf->dtype = UBI_UNKNOWN; 8531e51764aSArtem Bityutskiy wbuf->sync_callback = NULL; 8541e51764aSArtem Bityutskiy mutex_init(&wbuf->io_mutex); 8551e51764aSArtem Bityutskiy spin_lock_init(&wbuf->lock); 8561e51764aSArtem Bityutskiy wbuf->c = c; 8571e51764aSArtem Bityutskiy wbuf->next_ino = 0; 8581e51764aSArtem Bityutskiy 859f2c5dbd7SArtem Bityutskiy hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 860f2c5dbd7SArtem Bityutskiy wbuf->timer.function = wbuf_timer_callback_nolock; 8612a35a3a8SArtem Bityutskiy wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0); 8622a35a3a8SArtem Bityutskiy wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT; 8632a35a3a8SArtem Bityutskiy wbuf->delta *= 1000000000ULL; 8642a35a3a8SArtem Bityutskiy ubifs_assert(wbuf->delta <= ULONG_MAX); 8651e51764aSArtem Bityutskiy return 0; 8661e51764aSArtem Bityutskiy } 8671e51764aSArtem Bityutskiy 8681e51764aSArtem Bityutskiy /** 8691e51764aSArtem Bityutskiy * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array. 8701e51764aSArtem Bityutskiy * @wbuf: the write-buffer where to add 8711e51764aSArtem Bityutskiy * @inum: the inode number 8721e51764aSArtem Bityutskiy * 8731e51764aSArtem Bityutskiy * This function adds an inode number to the inode array of the write-buffer. 8741e51764aSArtem Bityutskiy */ 8751e51764aSArtem Bityutskiy void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum) 8761e51764aSArtem Bityutskiy { 8771e51764aSArtem Bityutskiy if (!wbuf->buf) 8781e51764aSArtem Bityutskiy /* NOR flash or something similar */ 8791e51764aSArtem Bityutskiy return; 8801e51764aSArtem Bityutskiy 8811e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 8821e51764aSArtem Bityutskiy if (wbuf->used) 8831e51764aSArtem Bityutskiy wbuf->inodes[wbuf->next_ino++] = inum; 8841e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 8851e51764aSArtem Bityutskiy } 8861e51764aSArtem Bityutskiy 8871e51764aSArtem Bityutskiy /** 8881e51764aSArtem Bityutskiy * wbuf_has_ino - returns if the wbuf contains data from the inode. 8891e51764aSArtem Bityutskiy * @wbuf: the write-buffer 8901e51764aSArtem Bityutskiy * @inum: the inode number 8911e51764aSArtem Bityutskiy * 8921e51764aSArtem Bityutskiy * This function returns with %1 if the write-buffer contains some data from the 8931e51764aSArtem Bityutskiy * given inode otherwise it returns with %0. 8941e51764aSArtem Bityutskiy */ 8951e51764aSArtem Bityutskiy static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum) 8961e51764aSArtem Bityutskiy { 8971e51764aSArtem Bityutskiy int i, ret = 0; 8981e51764aSArtem Bityutskiy 8991e51764aSArtem Bityutskiy spin_lock(&wbuf->lock); 9001e51764aSArtem Bityutskiy for (i = 0; i < wbuf->next_ino; i++) 9011e51764aSArtem Bityutskiy if (inum == wbuf->inodes[i]) { 9021e51764aSArtem Bityutskiy ret = 1; 9031e51764aSArtem Bityutskiy break; 9041e51764aSArtem Bityutskiy } 9051e51764aSArtem Bityutskiy spin_unlock(&wbuf->lock); 9061e51764aSArtem Bityutskiy 9071e51764aSArtem Bityutskiy return ret; 9081e51764aSArtem Bityutskiy } 9091e51764aSArtem Bityutskiy 9101e51764aSArtem Bityutskiy /** 9111e51764aSArtem Bityutskiy * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode. 9121e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9131e51764aSArtem Bityutskiy * @inode: inode to synchronize 9141e51764aSArtem Bityutskiy * 9151e51764aSArtem Bityutskiy * This function synchronizes write-buffers which contain nodes belonging to 9161e51764aSArtem Bityutskiy * @inode. Returns zero in case of success and a negative error code in case of 9171e51764aSArtem Bityutskiy * failure. 9181e51764aSArtem Bityutskiy */ 9191e51764aSArtem Bityutskiy int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode) 9201e51764aSArtem Bityutskiy { 9211e51764aSArtem Bityutskiy int i, err = 0; 9221e51764aSArtem Bityutskiy 9231e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 9241e51764aSArtem Bityutskiy struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; 9251e51764aSArtem Bityutskiy 9261e51764aSArtem Bityutskiy if (i == GCHD) 9271e51764aSArtem Bityutskiy /* 9281e51764aSArtem Bityutskiy * GC head is special, do not look at it. Even if the 9291e51764aSArtem Bityutskiy * head contains something related to this inode, it is 9301e51764aSArtem Bityutskiy * a _copy_ of corresponding on-flash node which sits 9311e51764aSArtem Bityutskiy * somewhere else. 9321e51764aSArtem Bityutskiy */ 9331e51764aSArtem Bityutskiy continue; 9341e51764aSArtem Bityutskiy 9351e51764aSArtem Bityutskiy if (!wbuf_has_ino(wbuf, inode->i_ino)) 9361e51764aSArtem Bityutskiy continue; 9371e51764aSArtem Bityutskiy 9381e51764aSArtem Bityutskiy mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); 9391e51764aSArtem Bityutskiy if (wbuf_has_ino(wbuf, inode->i_ino)) 9401e51764aSArtem Bityutskiy err = ubifs_wbuf_sync_nolock(wbuf); 9411e51764aSArtem Bityutskiy mutex_unlock(&wbuf->io_mutex); 9421e51764aSArtem Bityutskiy 9431e51764aSArtem Bityutskiy if (err) { 9441e51764aSArtem Bityutskiy ubifs_ro_mode(c, err); 9451e51764aSArtem Bityutskiy return err; 9461e51764aSArtem Bityutskiy } 9471e51764aSArtem Bityutskiy } 9481e51764aSArtem Bityutskiy return 0; 9491e51764aSArtem Bityutskiy } 950