xref: /openbmc/linux/fs/ubifs/io.c (revision 4f1ab9b0)
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  *
346c7f74f7SArtem Bityutskiy  * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
356c7f74f7SArtem Bityutskiy  * write size (@c->max_write_size). The latter is the maximum amount of bytes
366c7f74f7SArtem Bityutskiy  * the underlying flash is able to program at a time, and writing in
376c7f74f7SArtem Bityutskiy  * @c->max_write_size units should presumably be faster. Obviously,
386c7f74f7SArtem Bityutskiy  * @c->min_io_size <= @c->max_write_size. Write-buffers are of
396c7f74f7SArtem Bityutskiy  * @c->max_write_size bytes in size for maximum performance. However, when a
406c7f74f7SArtem Bityutskiy  * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
416c7f74f7SArtem Bityutskiy  * boundary) which contains data is written, not the whole write-buffer,
426c7f74f7SArtem Bityutskiy  * because this is more space-efficient.
436c7f74f7SArtem Bityutskiy  *
446c7f74f7SArtem Bityutskiy  * This optimization adds few complications to the code. Indeed, on the one
456c7f74f7SArtem Bityutskiy  * hand, we want to write in optimal @c->max_write_size bytes chunks, which
466c7f74f7SArtem Bityutskiy  * also means aligning writes at the @c->max_write_size bytes offsets. On the
476c7f74f7SArtem Bityutskiy  * other hand, we do not want to waste space when synchronizing the write
486c7f74f7SArtem Bityutskiy  * buffer, so during synchronization we writes in smaller chunks. And this makes
496c7f74f7SArtem Bityutskiy  * the next write offset to be not aligned to @c->max_write_size bytes. So the
506c7f74f7SArtem Bityutskiy  * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
516c7f74f7SArtem Bityutskiy  * to @c->max_write_size bytes again. We do this by temporarily shrinking
526c7f74f7SArtem Bityutskiy  * write-buffer size (@wbuf->size).
536c7f74f7SArtem Bityutskiy  *
541e51764aSArtem Bityutskiy  * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
551e51764aSArtem Bityutskiy  * mutexes defined inside these objects. Since sometimes upper-level code
561e51764aSArtem Bityutskiy  * has to lock the write-buffer (e.g. journal space reservation code), many
571e51764aSArtem Bityutskiy  * functions related to write-buffers have "nolock" suffix which means that the
581e51764aSArtem Bityutskiy  * caller has to lock the write-buffer before calling this function.
591e51764aSArtem Bityutskiy  *
601e51764aSArtem Bityutskiy  * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
611e51764aSArtem Bityutskiy  * aligned, UBIFS starts the next node from the aligned address, and the padded
621e51764aSArtem Bityutskiy  * bytes may contain any rubbish. In other words, UBIFS does not put padding
631e51764aSArtem Bityutskiy  * bytes in those small gaps. Common headers of nodes store real node lengths,
641e51764aSArtem Bityutskiy  * not aligned lengths. Indexing nodes also store real lengths in branches.
651e51764aSArtem Bityutskiy  *
661e51764aSArtem Bityutskiy  * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
671e51764aSArtem Bityutskiy  * uses padding nodes or padding bytes, if the padding node does not fit.
681e51764aSArtem Bityutskiy  *
696c7f74f7SArtem Bityutskiy  * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
706c7f74f7SArtem Bityutskiy  * they are read from the flash media.
711e51764aSArtem Bityutskiy  */
721e51764aSArtem Bityutskiy 
731e51764aSArtem Bityutskiy #include <linux/crc32.h>
745a0e3ad6STejun Heo #include <linux/slab.h>
751e51764aSArtem Bityutskiy #include "ubifs.h"
761e51764aSArtem Bityutskiy 
771e51764aSArtem Bityutskiy /**
78ff46d7b3SAdrian Hunter  * ubifs_ro_mode - switch UBIFS to read read-only mode.
79ff46d7b3SAdrian Hunter  * @c: UBIFS file-system description object
80ff46d7b3SAdrian Hunter  * @err: error code which is the reason of switching to R/O mode
81ff46d7b3SAdrian Hunter  */
82ff46d7b3SAdrian Hunter void ubifs_ro_mode(struct ubifs_info *c, int err)
83ff46d7b3SAdrian Hunter {
842680d722SArtem Bityutskiy 	if (!c->ro_error) {
852680d722SArtem Bityutskiy 		c->ro_error = 1;
86ccb3eba7SArtem Bityutskiy 		c->no_chk_data_crc = 0;
872fde99cbSZhangJieJing 		c->vfs_sb->s_flags |= MS_RDONLY;
88ff46d7b3SAdrian Hunter 		ubifs_warn("switched to read-only mode, error %d", err);
89ff46d7b3SAdrian Hunter 		dbg_dump_stack();
90ff46d7b3SAdrian Hunter 	}
91ff46d7b3SAdrian Hunter }
92ff46d7b3SAdrian Hunter 
93ff46d7b3SAdrian Hunter /**
941e51764aSArtem Bityutskiy  * ubifs_check_node - check node.
951e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
961e51764aSArtem Bityutskiy  * @buf: node to check
971e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
981e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
991e51764aSArtem Bityutskiy  * @quiet: print no messages
1006f7ab6d4SArtem Bityutskiy  * @must_chk_crc: indicates whether to always check the CRC
1011e51764aSArtem Bityutskiy  *
1021e51764aSArtem Bityutskiy  * This function checks node magic number and CRC checksum. This function also
1031e51764aSArtem Bityutskiy  * validates node length to prevent UBIFS from becoming crazy when an attacker
1041e51764aSArtem Bityutskiy  * feeds it a file-system image with incorrect nodes. For example, too large
1051e51764aSArtem Bityutskiy  * node length in the common header could cause UBIFS to read memory outside of
1061e51764aSArtem Bityutskiy  * allocated buffer when checking the CRC checksum.
1071e51764aSArtem Bityutskiy  *
1086f7ab6d4SArtem Bityutskiy  * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
1096f7ab6d4SArtem Bityutskiy  * true, which is controlled by corresponding UBIFS mount option. However, if
1106f7ab6d4SArtem Bityutskiy  * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
11118d1d7fbSArtem Bityutskiy  * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
11218d1d7fbSArtem Bityutskiy  * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
11318d1d7fbSArtem Bityutskiy  * is checked. This is because during mounting or re-mounting from R/O mode to
11418d1d7fbSArtem Bityutskiy  * R/W mode we may read journal nodes (when replying the journal or doing the
11518d1d7fbSArtem Bityutskiy  * recovery) and the journal nodes may potentially be corrupted, so checking is
11618d1d7fbSArtem Bityutskiy  * required.
1176f7ab6d4SArtem Bityutskiy  *
1186f7ab6d4SArtem Bityutskiy  * This function returns zero in case of success and %-EUCLEAN in case of bad
1196f7ab6d4SArtem Bityutskiy  * CRC or magic.
1201e51764aSArtem Bityutskiy  */
1211e51764aSArtem Bityutskiy int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
1226f7ab6d4SArtem Bityutskiy 		     int offs, int quiet, int must_chk_crc)
1231e51764aSArtem Bityutskiy {
1241e51764aSArtem Bityutskiy 	int err = -EINVAL, type, node_len;
1251e51764aSArtem Bityutskiy 	uint32_t crc, node_crc, magic;
1261e51764aSArtem Bityutskiy 	const struct ubifs_ch *ch = buf;
1271e51764aSArtem Bityutskiy 
1281e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1291e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
1301e51764aSArtem Bityutskiy 
1311e51764aSArtem Bityutskiy 	magic = le32_to_cpu(ch->magic);
1321e51764aSArtem Bityutskiy 	if (magic != UBIFS_NODE_MAGIC) {
1331e51764aSArtem Bityutskiy 		if (!quiet)
1341e51764aSArtem Bityutskiy 			ubifs_err("bad magic %#08x, expected %#08x",
1351e51764aSArtem Bityutskiy 				  magic, UBIFS_NODE_MAGIC);
1361e51764aSArtem Bityutskiy 		err = -EUCLEAN;
1371e51764aSArtem Bityutskiy 		goto out;
1381e51764aSArtem Bityutskiy 	}
1391e51764aSArtem Bityutskiy 
1401e51764aSArtem Bityutskiy 	type = ch->node_type;
1411e51764aSArtem Bityutskiy 	if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
1421e51764aSArtem Bityutskiy 		if (!quiet)
1431e51764aSArtem Bityutskiy 			ubifs_err("bad node type %d", type);
1441e51764aSArtem Bityutskiy 		goto out;
1451e51764aSArtem Bityutskiy 	}
1461e51764aSArtem Bityutskiy 
1471e51764aSArtem Bityutskiy 	node_len = le32_to_cpu(ch->len);
1481e51764aSArtem Bityutskiy 	if (node_len + offs > c->leb_size)
1491e51764aSArtem Bityutskiy 		goto out_len;
1501e51764aSArtem Bityutskiy 
1511e51764aSArtem Bityutskiy 	if (c->ranges[type].max_len == 0) {
1521e51764aSArtem Bityutskiy 		if (node_len != c->ranges[type].len)
1531e51764aSArtem Bityutskiy 			goto out_len;
1541e51764aSArtem Bityutskiy 	} else if (node_len < c->ranges[type].min_len ||
1551e51764aSArtem Bityutskiy 		   node_len > c->ranges[type].max_len)
1561e51764aSArtem Bityutskiy 		goto out_len;
1571e51764aSArtem Bityutskiy 
15818d1d7fbSArtem Bityutskiy 	if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
15918d1d7fbSArtem Bityutskiy 	    !c->remounting_rw && c->no_chk_data_crc)
1602953e73fSAdrian Hunter 		return 0;
1612953e73fSAdrian Hunter 
1621e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
1631e51764aSArtem Bityutskiy 	node_crc = le32_to_cpu(ch->crc);
1641e51764aSArtem Bityutskiy 	if (crc != node_crc) {
1651e51764aSArtem Bityutskiy 		if (!quiet)
1661e51764aSArtem Bityutskiy 			ubifs_err("bad CRC: calculated %#08x, read %#08x",
1671e51764aSArtem Bityutskiy 				  crc, node_crc);
1681e51764aSArtem Bityutskiy 		err = -EUCLEAN;
1691e51764aSArtem Bityutskiy 		goto out;
1701e51764aSArtem Bityutskiy 	}
1711e51764aSArtem Bityutskiy 
1721e51764aSArtem Bityutskiy 	return 0;
1731e51764aSArtem Bityutskiy 
1741e51764aSArtem Bityutskiy out_len:
1751e51764aSArtem Bityutskiy 	if (!quiet)
1761e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d", node_len);
1771e51764aSArtem Bityutskiy out:
1781e51764aSArtem Bityutskiy 	if (!quiet) {
1791e51764aSArtem Bityutskiy 		ubifs_err("bad node at LEB %d:%d", lnum, offs);
1801e51764aSArtem Bityutskiy 		dbg_dump_node(c, buf);
1811e51764aSArtem Bityutskiy 		dbg_dump_stack();
1821e51764aSArtem Bityutskiy 	}
1831e51764aSArtem Bityutskiy 	return err;
1841e51764aSArtem Bityutskiy }
1851e51764aSArtem Bityutskiy 
1861e51764aSArtem Bityutskiy /**
1871e51764aSArtem Bityutskiy  * ubifs_pad - pad flash space.
1881e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1891e51764aSArtem Bityutskiy  * @buf: buffer to put padding to
1901e51764aSArtem Bityutskiy  * @pad: how many bytes to pad
1911e51764aSArtem Bityutskiy  *
1921e51764aSArtem Bityutskiy  * The flash media obliges us to write only in chunks of %c->min_io_size and
1931e51764aSArtem Bityutskiy  * when we have to write less data we add padding node to the write-buffer and
1941e51764aSArtem Bityutskiy  * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
1951e51764aSArtem Bityutskiy  * media is being scanned. If the amount of wasted space is not enough to fit a
1961e51764aSArtem Bityutskiy  * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
1971e51764aSArtem Bityutskiy  * pattern (%UBIFS_PADDING_BYTE).
1981e51764aSArtem Bityutskiy  *
1991e51764aSArtem Bityutskiy  * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
2001e51764aSArtem Bityutskiy  * used.
2011e51764aSArtem Bityutskiy  */
2021e51764aSArtem Bityutskiy void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
2031e51764aSArtem Bityutskiy {
2041e51764aSArtem Bityutskiy 	uint32_t crc;
2051e51764aSArtem Bityutskiy 
2061e51764aSArtem Bityutskiy 	ubifs_assert(pad >= 0 && !(pad & 7));
2071e51764aSArtem Bityutskiy 
2081e51764aSArtem Bityutskiy 	if (pad >= UBIFS_PAD_NODE_SZ) {
2091e51764aSArtem Bityutskiy 		struct ubifs_ch *ch = buf;
2101e51764aSArtem Bityutskiy 		struct ubifs_pad_node *pad_node = buf;
2111e51764aSArtem Bityutskiy 
2121e51764aSArtem Bityutskiy 		ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
2131e51764aSArtem Bityutskiy 		ch->node_type = UBIFS_PAD_NODE;
2141e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_NO_NODE_GROUP;
2151e51764aSArtem Bityutskiy 		ch->padding[0] = ch->padding[1] = 0;
2161e51764aSArtem Bityutskiy 		ch->sqnum = 0;
2171e51764aSArtem Bityutskiy 		ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
2181e51764aSArtem Bityutskiy 		pad -= UBIFS_PAD_NODE_SZ;
2191e51764aSArtem Bityutskiy 		pad_node->pad_len = cpu_to_le32(pad);
2201e51764aSArtem Bityutskiy 		crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
2211e51764aSArtem Bityutskiy 		ch->crc = cpu_to_le32(crc);
2221e51764aSArtem Bityutskiy 		memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
2231e51764aSArtem Bityutskiy 	} else if (pad > 0)
2241e51764aSArtem Bityutskiy 		/* Too little space, padding node won't fit */
2251e51764aSArtem Bityutskiy 		memset(buf, UBIFS_PADDING_BYTE, pad);
2261e51764aSArtem Bityutskiy }
2271e51764aSArtem Bityutskiy 
2281e51764aSArtem Bityutskiy /**
2291e51764aSArtem Bityutskiy  * next_sqnum - get next sequence number.
2301e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2311e51764aSArtem Bityutskiy  */
2321e51764aSArtem Bityutskiy static unsigned long long next_sqnum(struct ubifs_info *c)
2331e51764aSArtem Bityutskiy {
2341e51764aSArtem Bityutskiy 	unsigned long long sqnum;
2351e51764aSArtem Bityutskiy 
2361e51764aSArtem Bityutskiy 	spin_lock(&c->cnt_lock);
2371e51764aSArtem Bityutskiy 	sqnum = ++c->max_sqnum;
2381e51764aSArtem Bityutskiy 	spin_unlock(&c->cnt_lock);
2391e51764aSArtem Bityutskiy 
2401e51764aSArtem Bityutskiy 	if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
2411e51764aSArtem Bityutskiy 		if (sqnum >= SQNUM_WATERMARK) {
2421e51764aSArtem Bityutskiy 			ubifs_err("sequence number overflow %llu, end of life",
2431e51764aSArtem Bityutskiy 				  sqnum);
2441e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, -EINVAL);
2451e51764aSArtem Bityutskiy 		}
2461e51764aSArtem Bityutskiy 		ubifs_warn("running out of sequence numbers, end of life soon");
2471e51764aSArtem Bityutskiy 	}
2481e51764aSArtem Bityutskiy 
2491e51764aSArtem Bityutskiy 	return sqnum;
2501e51764aSArtem Bityutskiy }
2511e51764aSArtem Bityutskiy 
2521e51764aSArtem Bityutskiy /**
2531e51764aSArtem Bityutskiy  * ubifs_prepare_node - prepare node to be written to flash.
2541e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2551e51764aSArtem Bityutskiy  * @node: the node to pad
2561e51764aSArtem Bityutskiy  * @len: node length
2571e51764aSArtem Bityutskiy  * @pad: if the buffer has to be padded
2581e51764aSArtem Bityutskiy  *
2591e51764aSArtem Bityutskiy  * This function prepares node at @node to be written to the media - it
2601e51764aSArtem Bityutskiy  * calculates node CRC, fills the common header, and adds proper padding up to
2611e51764aSArtem Bityutskiy  * the next minimum I/O unit if @pad is not zero.
2621e51764aSArtem Bityutskiy  */
2631e51764aSArtem Bityutskiy void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
2641e51764aSArtem Bityutskiy {
2651e51764aSArtem Bityutskiy 	uint32_t crc;
2661e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = node;
2671e51764aSArtem Bityutskiy 	unsigned long long sqnum = next_sqnum(c);
2681e51764aSArtem Bityutskiy 
2691e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ);
2701e51764aSArtem Bityutskiy 
2711e51764aSArtem Bityutskiy 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
2721e51764aSArtem Bityutskiy 	ch->len = cpu_to_le32(len);
2731e51764aSArtem Bityutskiy 	ch->group_type = UBIFS_NO_NODE_GROUP;
2741e51764aSArtem Bityutskiy 	ch->sqnum = cpu_to_le64(sqnum);
2751e51764aSArtem Bityutskiy 	ch->padding[0] = ch->padding[1] = 0;
2761e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
2771e51764aSArtem Bityutskiy 	ch->crc = cpu_to_le32(crc);
2781e51764aSArtem Bityutskiy 
2791e51764aSArtem Bityutskiy 	if (pad) {
2801e51764aSArtem Bityutskiy 		len = ALIGN(len, 8);
2811e51764aSArtem Bityutskiy 		pad = ALIGN(len, c->min_io_size) - len;
2821e51764aSArtem Bityutskiy 		ubifs_pad(c, node + len, pad);
2831e51764aSArtem Bityutskiy 	}
2841e51764aSArtem Bityutskiy }
2851e51764aSArtem Bityutskiy 
2861e51764aSArtem Bityutskiy /**
2871e51764aSArtem Bityutskiy  * ubifs_prep_grp_node - prepare node of a group to be written to flash.
2881e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2891e51764aSArtem Bityutskiy  * @node: the node to pad
2901e51764aSArtem Bityutskiy  * @len: node length
2911e51764aSArtem Bityutskiy  * @last: indicates the last node of the group
2921e51764aSArtem Bityutskiy  *
2931e51764aSArtem Bityutskiy  * This function prepares node at @node to be written to the media - it
2941e51764aSArtem Bityutskiy  * calculates node CRC and fills the common header.
2951e51764aSArtem Bityutskiy  */
2961e51764aSArtem Bityutskiy void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
2971e51764aSArtem Bityutskiy {
2981e51764aSArtem Bityutskiy 	uint32_t crc;
2991e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = node;
3001e51764aSArtem Bityutskiy 	unsigned long long sqnum = next_sqnum(c);
3011e51764aSArtem Bityutskiy 
3021e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ);
3031e51764aSArtem Bityutskiy 
3041e51764aSArtem Bityutskiy 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
3051e51764aSArtem Bityutskiy 	ch->len = cpu_to_le32(len);
3061e51764aSArtem Bityutskiy 	if (last)
3071e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
3081e51764aSArtem Bityutskiy 	else
3091e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_IN_NODE_GROUP;
3101e51764aSArtem Bityutskiy 	ch->sqnum = cpu_to_le64(sqnum);
3111e51764aSArtem Bityutskiy 	ch->padding[0] = ch->padding[1] = 0;
3121e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
3131e51764aSArtem Bityutskiy 	ch->crc = cpu_to_le32(crc);
3141e51764aSArtem Bityutskiy }
3151e51764aSArtem Bityutskiy 
3161e51764aSArtem Bityutskiy /**
3171e51764aSArtem Bityutskiy  * wbuf_timer_callback - write-buffer timer callback function.
3181e51764aSArtem Bityutskiy  * @data: timer data (write-buffer descriptor)
3191e51764aSArtem Bityutskiy  *
3201e51764aSArtem Bityutskiy  * This function is called when the write-buffer timer expires.
3211e51764aSArtem Bityutskiy  */
322f2c5dbd7SArtem Bityutskiy static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
3231e51764aSArtem Bityutskiy {
324f2c5dbd7SArtem Bityutskiy 	struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
3251e51764aSArtem Bityutskiy 
32677a7ae58SArtem Bityutskiy 	dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
3271e51764aSArtem Bityutskiy 	wbuf->need_sync = 1;
3281e51764aSArtem Bityutskiy 	wbuf->c->need_wbuf_sync = 1;
3291e51764aSArtem Bityutskiy 	ubifs_wake_up_bgt(wbuf->c);
330f2c5dbd7SArtem Bityutskiy 	return HRTIMER_NORESTART;
3311e51764aSArtem Bityutskiy }
3321e51764aSArtem Bityutskiy 
3331e51764aSArtem Bityutskiy /**
3341e51764aSArtem Bityutskiy  * new_wbuf_timer - start new write-buffer timer.
3351e51764aSArtem Bityutskiy  * @wbuf: write-buffer descriptor
3361e51764aSArtem Bityutskiy  */
3371e51764aSArtem Bityutskiy static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
3381e51764aSArtem Bityutskiy {
339f2c5dbd7SArtem Bityutskiy 	ubifs_assert(!hrtimer_active(&wbuf->timer));
3401e51764aSArtem Bityutskiy 
3410b335b9dSArtem Bityutskiy 	if (wbuf->no_timer)
3421e51764aSArtem Bityutskiy 		return;
34377a7ae58SArtem Bityutskiy 	dbg_io("set timer for jhead %s, %llu-%llu millisecs",
34477a7ae58SArtem Bityutskiy 	       dbg_jhead(wbuf->jhead),
34544737589SAdrian Hunter 	       div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
34644737589SAdrian Hunter 	       div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
34744737589SAdrian Hunter 		       USEC_PER_SEC));
348f2c5dbd7SArtem Bityutskiy 	hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
349f2c5dbd7SArtem Bityutskiy 			       HRTIMER_MODE_REL);
3501e51764aSArtem Bityutskiy }
3511e51764aSArtem Bityutskiy 
3521e51764aSArtem Bityutskiy /**
3531e51764aSArtem Bityutskiy  * cancel_wbuf_timer - cancel write-buffer timer.
3541e51764aSArtem Bityutskiy  * @wbuf: write-buffer descriptor
3551e51764aSArtem Bityutskiy  */
3561e51764aSArtem Bityutskiy static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
3571e51764aSArtem Bityutskiy {
3580b335b9dSArtem Bityutskiy 	if (wbuf->no_timer)
3590b335b9dSArtem Bityutskiy 		return;
3601e51764aSArtem Bityutskiy 	wbuf->need_sync = 0;
361f2c5dbd7SArtem Bityutskiy 	hrtimer_cancel(&wbuf->timer);
3621e51764aSArtem Bityutskiy }
3631e51764aSArtem Bityutskiy 
3641e51764aSArtem Bityutskiy /**
3651e51764aSArtem Bityutskiy  * ubifs_wbuf_sync_nolock - synchronize write-buffer.
3661e51764aSArtem Bityutskiy  * @wbuf: write-buffer to synchronize
3671e51764aSArtem Bityutskiy  *
3681e51764aSArtem Bityutskiy  * This function synchronizes write-buffer @buf and returns zero in case of
3691e51764aSArtem Bityutskiy  * success or a negative error code in case of failure.
3706c7f74f7SArtem Bityutskiy  *
3716c7f74f7SArtem Bityutskiy  * Note, although write-buffers are of @c->max_write_size, this function does
3726c7f74f7SArtem Bityutskiy  * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
3736c7f74f7SArtem Bityutskiy  * if the write-buffer is only partially filled with data, only the used part
3746c7f74f7SArtem Bityutskiy  * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
3756c7f74f7SArtem Bityutskiy  * This way we waste less space.
3761e51764aSArtem Bityutskiy  */
3771e51764aSArtem Bityutskiy int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
3781e51764aSArtem Bityutskiy {
3791e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
3806c7f74f7SArtem Bityutskiy 	int err, dirt, sync_len;
3811e51764aSArtem Bityutskiy 
3821e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
3831e51764aSArtem Bityutskiy 	if (!wbuf->used || wbuf->lnum == -1)
3841e51764aSArtem Bityutskiy 		/* Write-buffer is empty or not seeked */
3851e51764aSArtem Bityutskiy 		return 0;
3861e51764aSArtem Bityutskiy 
38777a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, %d bytes, jhead %s",
38877a7ae58SArtem Bityutskiy 	       wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
3891e51764aSArtem Bityutskiy 	ubifs_assert(!(wbuf->avail & 7));
3903c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
3913c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->size >= c->min_io_size);
3923c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->size <= c->max_write_size);
3933c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->size % c->min_io_size == 0);
3942ef13294SArtem Bityutskiy 	ubifs_assert(!c->ro_media && !c->ro_mount);
3956c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs >= c->max_write_size)
3966c7f74f7SArtem Bityutskiy 		ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
3971e51764aSArtem Bityutskiy 
3982680d722SArtem Bityutskiy 	if (c->ro_error)
3991e51764aSArtem Bityutskiy 		return -EROFS;
4001e51764aSArtem Bityutskiy 
4016c7f74f7SArtem Bityutskiy 	/*
4026c7f74f7SArtem Bityutskiy 	 * Do not write whole write buffer but write only the minimum necessary
4036c7f74f7SArtem Bityutskiy 	 * amount of min. I/O units.
4046c7f74f7SArtem Bityutskiy 	 */
4056c7f74f7SArtem Bityutskiy 	sync_len = ALIGN(wbuf->used, c->min_io_size);
4066c7f74f7SArtem Bityutskiy 	dirt = sync_len - wbuf->used;
4076c7f74f7SArtem Bityutskiy 	if (dirt)
4086c7f74f7SArtem Bityutskiy 		ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
4091e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
4106c7f74f7SArtem Bityutskiy 			    sync_len, wbuf->dtype);
4111e51764aSArtem Bityutskiy 	if (err) {
4121e51764aSArtem Bityutskiy 		ubifs_err("cannot write %d bytes to LEB %d:%d",
4136c7f74f7SArtem Bityutskiy 			  sync_len, wbuf->lnum, wbuf->offs);
4141e51764aSArtem Bityutskiy 		dbg_dump_stack();
4151e51764aSArtem Bityutskiy 		return err;
4161e51764aSArtem Bityutskiy 	}
4171e51764aSArtem Bityutskiy 
4181e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
4196c7f74f7SArtem Bityutskiy 	wbuf->offs += sync_len;
4206c7f74f7SArtem Bityutskiy 	/*
4216c7f74f7SArtem Bityutskiy 	 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
4226c7f74f7SArtem Bityutskiy 	 * But our goal is to optimize writes and make sure we write in
4236c7f74f7SArtem Bityutskiy 	 * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
4246c7f74f7SArtem Bityutskiy 	 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
4256c7f74f7SArtem Bityutskiy 	 * sure that @wbuf->offs + @wbuf->size is aligned to
4266c7f74f7SArtem Bityutskiy 	 * @c->max_write_size. This way we make sure that after next
4276c7f74f7SArtem Bityutskiy 	 * write-buffer flush we are again at the optimal offset (aligned to
4286c7f74f7SArtem Bityutskiy 	 * @c->max_write_size).
4296c7f74f7SArtem Bityutskiy 	 */
4306c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs < c->max_write_size)
4316c7f74f7SArtem Bityutskiy 		wbuf->size = c->leb_size - wbuf->offs;
4326c7f74f7SArtem Bityutskiy 	else if (wbuf->offs & (c->max_write_size - 1))
4336c7f74f7SArtem Bityutskiy 		wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
4346c7f74f7SArtem Bityutskiy 	else
4356c7f74f7SArtem Bityutskiy 		wbuf->size = c->max_write_size;
4366c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size;
4371e51764aSArtem Bityutskiy 	wbuf->used = 0;
4381e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
4391e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
4401e51764aSArtem Bityutskiy 
4411e51764aSArtem Bityutskiy 	if (wbuf->sync_callback)
4421e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum,
4431e51764aSArtem Bityutskiy 					  c->leb_size - wbuf->offs, dirt);
4441e51764aSArtem Bityutskiy 	return err;
4451e51764aSArtem Bityutskiy }
4461e51764aSArtem Bityutskiy 
4471e51764aSArtem Bityutskiy /**
4481e51764aSArtem Bityutskiy  * ubifs_wbuf_seek_nolock - seek write-buffer.
4491e51764aSArtem Bityutskiy  * @wbuf: write-buffer
4501e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number to seek to
4511e51764aSArtem Bityutskiy  * @offs: logical eraseblock offset to seek to
4521e51764aSArtem Bityutskiy  * @dtype: data type
4531e51764aSArtem Bityutskiy  *
454cb54ef8bSArtem Bityutskiy  * This function targets the write-buffer to logical eraseblock @lnum:@offs.
455cb14a184SArtem Bityutskiy  * The write-buffer has to be empty. Returns zero in case of success and a
456cb14a184SArtem Bityutskiy  * negative error code in case of failure.
4571e51764aSArtem Bityutskiy  */
4581e51764aSArtem Bityutskiy int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
4591e51764aSArtem Bityutskiy 			   int dtype)
4601e51764aSArtem Bityutskiy {
4611e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
4621e51764aSArtem Bityutskiy 
46377a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
4641e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
4651e51764aSArtem Bityutskiy 	ubifs_assert(offs >= 0 && offs <= c->leb_size);
4661e51764aSArtem Bityutskiy 	ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
4671e51764aSArtem Bityutskiy 	ubifs_assert(lnum != wbuf->lnum);
468cb14a184SArtem Bityutskiy 	ubifs_assert(wbuf->used == 0);
4691e51764aSArtem Bityutskiy 
4701e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
4711e51764aSArtem Bityutskiy 	wbuf->lnum = lnum;
4721e51764aSArtem Bityutskiy 	wbuf->offs = offs;
4736c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs < c->max_write_size)
4746c7f74f7SArtem Bityutskiy 		wbuf->size = c->leb_size - wbuf->offs;
4756c7f74f7SArtem Bityutskiy 	else if (wbuf->offs & (c->max_write_size - 1))
4766c7f74f7SArtem Bityutskiy 		wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
4776c7f74f7SArtem Bityutskiy 	else
4786c7f74f7SArtem Bityutskiy 		wbuf->size = c->max_write_size;
4796c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size;
4801e51764aSArtem Bityutskiy 	wbuf->used = 0;
4811e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
4821e51764aSArtem Bityutskiy 	wbuf->dtype = dtype;
4831e51764aSArtem Bityutskiy 
4841e51764aSArtem Bityutskiy 	return 0;
4851e51764aSArtem Bityutskiy }
4861e51764aSArtem Bityutskiy 
4871e51764aSArtem Bityutskiy /**
4881e51764aSArtem Bityutskiy  * ubifs_bg_wbufs_sync - synchronize write-buffers.
4891e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4901e51764aSArtem Bityutskiy  *
4911e51764aSArtem Bityutskiy  * This function is called by background thread to synchronize write-buffers.
4921e51764aSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
4931e51764aSArtem Bityutskiy  * failure.
4941e51764aSArtem Bityutskiy  */
4951e51764aSArtem Bityutskiy int ubifs_bg_wbufs_sync(struct ubifs_info *c)
4961e51764aSArtem Bityutskiy {
4971e51764aSArtem Bityutskiy 	int err, i;
4981e51764aSArtem Bityutskiy 
4992ef13294SArtem Bityutskiy 	ubifs_assert(!c->ro_media && !c->ro_mount);
5001e51764aSArtem Bityutskiy 	if (!c->need_wbuf_sync)
5011e51764aSArtem Bityutskiy 		return 0;
5021e51764aSArtem Bityutskiy 	c->need_wbuf_sync = 0;
5031e51764aSArtem Bityutskiy 
5042680d722SArtem Bityutskiy 	if (c->ro_error) {
5051e51764aSArtem Bityutskiy 		err = -EROFS;
5061e51764aSArtem Bityutskiy 		goto out_timers;
5071e51764aSArtem Bityutskiy 	}
5081e51764aSArtem Bityutskiy 
5091e51764aSArtem Bityutskiy 	dbg_io("synchronize");
5101e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
5111e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
5121e51764aSArtem Bityutskiy 
5131e51764aSArtem Bityutskiy 		cond_resched();
5141e51764aSArtem Bityutskiy 
5151e51764aSArtem Bityutskiy 		/*
5161e51764aSArtem Bityutskiy 		 * If the mutex is locked then wbuf is being changed, so
5171e51764aSArtem Bityutskiy 		 * synchronization is not necessary.
5181e51764aSArtem Bityutskiy 		 */
5191e51764aSArtem Bityutskiy 		if (mutex_is_locked(&wbuf->io_mutex))
5201e51764aSArtem Bityutskiy 			continue;
5211e51764aSArtem Bityutskiy 
5221e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
5231e51764aSArtem Bityutskiy 		if (!wbuf->need_sync) {
5241e51764aSArtem Bityutskiy 			mutex_unlock(&wbuf->io_mutex);
5251e51764aSArtem Bityutskiy 			continue;
5261e51764aSArtem Bityutskiy 		}
5271e51764aSArtem Bityutskiy 
5281e51764aSArtem Bityutskiy 		err = ubifs_wbuf_sync_nolock(wbuf);
5291e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
5301e51764aSArtem Bityutskiy 		if (err) {
5311e51764aSArtem Bityutskiy 			ubifs_err("cannot sync write-buffer, error %d", err);
5321e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
5331e51764aSArtem Bityutskiy 			goto out_timers;
5341e51764aSArtem Bityutskiy 		}
5351e51764aSArtem Bityutskiy 	}
5361e51764aSArtem Bityutskiy 
5371e51764aSArtem Bityutskiy 	return 0;
5381e51764aSArtem Bityutskiy 
5391e51764aSArtem Bityutskiy out_timers:
5401e51764aSArtem Bityutskiy 	/* Cancel all timers to prevent repeated errors */
5411e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
5421e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
5431e51764aSArtem Bityutskiy 
5441e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
5451e51764aSArtem Bityutskiy 		cancel_wbuf_timer_nolock(wbuf);
5461e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
5471e51764aSArtem Bityutskiy 	}
5481e51764aSArtem Bityutskiy 	return err;
5491e51764aSArtem Bityutskiy }
5501e51764aSArtem Bityutskiy 
5511e51764aSArtem Bityutskiy /**
5521e51764aSArtem Bityutskiy  * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
5531e51764aSArtem Bityutskiy  * @wbuf: write-buffer
5541e51764aSArtem Bityutskiy  * @buf: node to write
5551e51764aSArtem Bityutskiy  * @len: node length
5561e51764aSArtem Bityutskiy  *
5571e51764aSArtem Bityutskiy  * This function writes data to flash via write-buffer @wbuf. This means that
5581e51764aSArtem Bityutskiy  * the last piece of the node won't reach the flash media immediately if it
5596c7f74f7SArtem Bityutskiy  * does not take whole max. write unit (@c->max_write_size). Instead, the node
5606c7f74f7SArtem Bityutskiy  * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
5616c7f74f7SArtem Bityutskiy  * because more data are appended to the write-buffer).
5621e51764aSArtem Bityutskiy  *
5631e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
5641e51764aSArtem Bityutskiy  * case of failure. If the node cannot be written because there is no more
5651e51764aSArtem Bityutskiy  * space in this logical eraseblock, %-ENOSPC is returned.
5661e51764aSArtem Bityutskiy  */
5671e51764aSArtem Bityutskiy int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
5681e51764aSArtem Bityutskiy {
5691e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
57012f33891SArtem Bityutskiy 	int err, written, n, aligned_len = ALIGN(len, 8);
5711e51764aSArtem Bityutskiy 
57277a7ae58SArtem Bityutskiy 	dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
57377a7ae58SArtem Bityutskiy 	       dbg_ntype(((struct ubifs_ch *)buf)->node_type),
57477a7ae58SArtem Bityutskiy 	       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
5751e51764aSArtem Bityutskiy 	ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
5761e51764aSArtem Bityutskiy 	ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
5771e51764aSArtem Bityutskiy 	ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
5783c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
5793c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->size >= c->min_io_size);
5803c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->size <= c->max_write_size);
5813c89f396SArtem Bityutskiy 	ubifs_assert(wbuf->size % c->min_io_size == 0);
5821e51764aSArtem Bityutskiy 	ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
5832ef13294SArtem Bityutskiy 	ubifs_assert(!c->ro_media && !c->ro_mount);
5844f1ab9b0SBen Gardiner 	ubifs_assert(!c->space_fixup);
5856c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs >= c->max_write_size)
5866c7f74f7SArtem Bityutskiy 		ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
5871e51764aSArtem Bityutskiy 
5881e51764aSArtem Bityutskiy 	if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
5891e51764aSArtem Bityutskiy 		err = -ENOSPC;
5901e51764aSArtem Bityutskiy 		goto out;
5911e51764aSArtem Bityutskiy 	}
5921e51764aSArtem Bityutskiy 
5931e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
5941e51764aSArtem Bityutskiy 
5952680d722SArtem Bityutskiy 	if (c->ro_error)
5961e51764aSArtem Bityutskiy 		return -EROFS;
5971e51764aSArtem Bityutskiy 
5981e51764aSArtem Bityutskiy 	if (aligned_len <= wbuf->avail) {
5991e51764aSArtem Bityutskiy 		/*
6001e51764aSArtem Bityutskiy 		 * The node is not very large and fits entirely within
6011e51764aSArtem Bityutskiy 		 * write-buffer.
6021e51764aSArtem Bityutskiy 		 */
6031e51764aSArtem Bityutskiy 		memcpy(wbuf->buf + wbuf->used, buf, len);
6041e51764aSArtem Bityutskiy 
6051e51764aSArtem Bityutskiy 		if (aligned_len == wbuf->avail) {
60677a7ae58SArtem Bityutskiy 			dbg_io("flush jhead %s wbuf to LEB %d:%d",
60777a7ae58SArtem Bityutskiy 			       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
6081e51764aSArtem Bityutskiy 			err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
6093c89f396SArtem Bityutskiy 					    wbuf->offs, wbuf->size,
6101e51764aSArtem Bityutskiy 					    wbuf->dtype);
6111e51764aSArtem Bityutskiy 			if (err)
6121e51764aSArtem Bityutskiy 				goto out;
6131e51764aSArtem Bityutskiy 
6141e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
6156c7f74f7SArtem Bityutskiy 			wbuf->offs += wbuf->size;
6166c7f74f7SArtem Bityutskiy 			if (c->leb_size - wbuf->offs >= c->max_write_size)
6176c7f74f7SArtem Bityutskiy 				wbuf->size = c->max_write_size;
6186c7f74f7SArtem Bityutskiy 			else
6196c7f74f7SArtem Bityutskiy 				wbuf->size = c->leb_size - wbuf->offs;
6206c7f74f7SArtem Bityutskiy 			wbuf->avail = wbuf->size;
6211e51764aSArtem Bityutskiy 			wbuf->used = 0;
6221e51764aSArtem Bityutskiy 			wbuf->next_ino = 0;
6231e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
6241e51764aSArtem Bityutskiy 		} else {
6251e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
6261e51764aSArtem Bityutskiy 			wbuf->avail -= aligned_len;
6271e51764aSArtem Bityutskiy 			wbuf->used += aligned_len;
6281e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
6291e51764aSArtem Bityutskiy 		}
6301e51764aSArtem Bityutskiy 
6311e51764aSArtem Bityutskiy 		goto exit;
6321e51764aSArtem Bityutskiy 	}
6331e51764aSArtem Bityutskiy 
6346c7f74f7SArtem Bityutskiy 	written = 0;
6356c7f74f7SArtem Bityutskiy 
6366c7f74f7SArtem Bityutskiy 	if (wbuf->used) {
6371e51764aSArtem Bityutskiy 		/*
6386c7f74f7SArtem Bityutskiy 		 * The node is large enough and does not fit entirely within
6396c7f74f7SArtem Bityutskiy 		 * current available space. We have to fill and flush
6406c7f74f7SArtem Bityutskiy 		 * write-buffer and switch to the next max. write unit.
6411e51764aSArtem Bityutskiy 		 */
64277a7ae58SArtem Bityutskiy 		dbg_io("flush jhead %s wbuf to LEB %d:%d",
64377a7ae58SArtem Bityutskiy 		       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
6441e51764aSArtem Bityutskiy 		memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
6451e51764aSArtem Bityutskiy 		err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
6463c89f396SArtem Bityutskiy 				    wbuf->size, wbuf->dtype);
6471e51764aSArtem Bityutskiy 		if (err)
6481e51764aSArtem Bityutskiy 			goto out;
6491e51764aSArtem Bityutskiy 
65012f33891SArtem Bityutskiy 		wbuf->offs += wbuf->size;
6511e51764aSArtem Bityutskiy 		len -= wbuf->avail;
6521e51764aSArtem Bityutskiy 		aligned_len -= wbuf->avail;
6536c7f74f7SArtem Bityutskiy 		written += wbuf->avail;
6546c7f74f7SArtem Bityutskiy 	} else if (wbuf->offs & (c->max_write_size - 1)) {
6556c7f74f7SArtem Bityutskiy 		/*
6566c7f74f7SArtem Bityutskiy 		 * The write-buffer offset is not aligned to
6576c7f74f7SArtem Bityutskiy 		 * @c->max_write_size and @wbuf->size is less than
6586c7f74f7SArtem Bityutskiy 		 * @c->max_write_size. Write @wbuf->size bytes to make sure the
6596c7f74f7SArtem Bityutskiy 		 * following writes are done in optimal @c->max_write_size
6606c7f74f7SArtem Bityutskiy 		 * chunks.
6616c7f74f7SArtem Bityutskiy 		 */
6626c7f74f7SArtem Bityutskiy 		dbg_io("write %d bytes to LEB %d:%d",
6636c7f74f7SArtem Bityutskiy 		       wbuf->size, wbuf->lnum, wbuf->offs);
6646c7f74f7SArtem Bityutskiy 		err = ubi_leb_write(c->ubi, wbuf->lnum, buf, wbuf->offs,
6656c7f74f7SArtem Bityutskiy 				    wbuf->size, wbuf->dtype);
6666c7f74f7SArtem Bityutskiy 		if (err)
6676c7f74f7SArtem Bityutskiy 			goto out;
6686c7f74f7SArtem Bityutskiy 
66912f33891SArtem Bityutskiy 		wbuf->offs += wbuf->size;
6706c7f74f7SArtem Bityutskiy 		len -= wbuf->size;
6716c7f74f7SArtem Bityutskiy 		aligned_len -= wbuf->size;
6726c7f74f7SArtem Bityutskiy 		written += wbuf->size;
6736c7f74f7SArtem Bityutskiy 	}
6741e51764aSArtem Bityutskiy 
6751e51764aSArtem Bityutskiy 	/*
6766c7f74f7SArtem Bityutskiy 	 * The remaining data may take more whole max. write units, so write the
6776c7f74f7SArtem Bityutskiy 	 * remains multiple to max. write unit size directly to the flash media.
6781e51764aSArtem Bityutskiy 	 * We align node length to 8-byte boundary because we anyway flash wbuf
6791e51764aSArtem Bityutskiy 	 * if the remaining space is less than 8 bytes.
6801e51764aSArtem Bityutskiy 	 */
6816c7f74f7SArtem Bityutskiy 	n = aligned_len >> c->max_write_shift;
6821e51764aSArtem Bityutskiy 	if (n) {
6836c7f74f7SArtem Bityutskiy 		n <<= c->max_write_shift;
68412f33891SArtem Bityutskiy 		dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
68512f33891SArtem Bityutskiy 		       wbuf->offs);
68612f33891SArtem Bityutskiy 		err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written,
68712f33891SArtem Bityutskiy 				    wbuf->offs, n, wbuf->dtype);
6881e51764aSArtem Bityutskiy 		if (err)
6891e51764aSArtem Bityutskiy 			goto out;
69012f33891SArtem Bityutskiy 		wbuf->offs += n;
6911e51764aSArtem Bityutskiy 		aligned_len -= n;
6921e51764aSArtem Bityutskiy 		len -= n;
6931e51764aSArtem Bityutskiy 		written += n;
6941e51764aSArtem Bityutskiy 	}
6951e51764aSArtem Bityutskiy 
6961e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
6971e51764aSArtem Bityutskiy 	if (aligned_len)
6981e51764aSArtem Bityutskiy 		/*
6991e51764aSArtem Bityutskiy 		 * And now we have what's left and what does not take whole
7006c7f74f7SArtem Bityutskiy 		 * max. write unit, so write it to the write-buffer and we are
7011e51764aSArtem Bityutskiy 		 * done.
7021e51764aSArtem Bityutskiy 		 */
7031e51764aSArtem Bityutskiy 		memcpy(wbuf->buf, buf + written, len);
7041e51764aSArtem Bityutskiy 
7056c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs >= c->max_write_size)
7066c7f74f7SArtem Bityutskiy 		wbuf->size = c->max_write_size;
7076c7f74f7SArtem Bityutskiy 	else
7086c7f74f7SArtem Bityutskiy 		wbuf->size = c->leb_size - wbuf->offs;
7096c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size - aligned_len;
7101e51764aSArtem Bityutskiy 	wbuf->used = aligned_len;
7111e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
7121e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
7131e51764aSArtem Bityutskiy 
7141e51764aSArtem Bityutskiy exit:
7151e51764aSArtem Bityutskiy 	if (wbuf->sync_callback) {
7161e51764aSArtem Bityutskiy 		int free = c->leb_size - wbuf->offs - wbuf->used;
7171e51764aSArtem Bityutskiy 
7181e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
7191e51764aSArtem Bityutskiy 		if (err)
7201e51764aSArtem Bityutskiy 			goto out;
7211e51764aSArtem Bityutskiy 	}
7221e51764aSArtem Bityutskiy 
7231e51764aSArtem Bityutskiy 	if (wbuf->used)
7241e51764aSArtem Bityutskiy 		new_wbuf_timer_nolock(wbuf);
7251e51764aSArtem Bityutskiy 
7261e51764aSArtem Bityutskiy 	return 0;
7271e51764aSArtem Bityutskiy 
7281e51764aSArtem Bityutskiy out:
7291e51764aSArtem Bityutskiy 	ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
7301e51764aSArtem Bityutskiy 		  len, wbuf->lnum, wbuf->offs, err);
7311e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
7321e51764aSArtem Bityutskiy 	dbg_dump_stack();
7331e51764aSArtem Bityutskiy 	dbg_dump_leb(c, wbuf->lnum);
7341e51764aSArtem Bityutskiy 	return err;
7351e51764aSArtem Bityutskiy }
7361e51764aSArtem Bityutskiy 
7371e51764aSArtem Bityutskiy /**
7381e51764aSArtem Bityutskiy  * ubifs_write_node - write node to the media.
7391e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7401e51764aSArtem Bityutskiy  * @buf: the node to write
7411e51764aSArtem Bityutskiy  * @len: node length
7421e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
7431e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
7441e51764aSArtem Bityutskiy  * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
7451e51764aSArtem Bityutskiy  *
7461e51764aSArtem Bityutskiy  * This function automatically fills node magic number, assigns sequence
7471e51764aSArtem Bityutskiy  * number, and calculates node CRC checksum. The length of the @buf buffer has
7481e51764aSArtem Bityutskiy  * to be aligned to the minimal I/O unit size. This function automatically
7491e51764aSArtem Bityutskiy  * appends padding node and padding bytes if needed. Returns zero in case of
7501e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
7511e51764aSArtem Bityutskiy  */
7521e51764aSArtem Bityutskiy int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
7531e51764aSArtem Bityutskiy 		     int offs, int dtype)
7541e51764aSArtem Bityutskiy {
7551e51764aSArtem Bityutskiy 	int err, buf_len = ALIGN(len, c->min_io_size);
7561e51764aSArtem Bityutskiy 
7571e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
7581e51764aSArtem Bityutskiy 	       lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
7591e51764aSArtem Bityutskiy 	       buf_len);
7601e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
7611e51764aSArtem Bityutskiy 	ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
7622ef13294SArtem Bityutskiy 	ubifs_assert(!c->ro_media && !c->ro_mount);
7634f1ab9b0SBen Gardiner 	ubifs_assert(!c->space_fixup);
7641e51764aSArtem Bityutskiy 
7652680d722SArtem Bityutskiy 	if (c->ro_error)
7661e51764aSArtem Bityutskiy 		return -EROFS;
7671e51764aSArtem Bityutskiy 
7681e51764aSArtem Bityutskiy 	ubifs_prepare_node(c, buf, len, 1);
7691e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
7701e51764aSArtem Bityutskiy 	if (err) {
7711e51764aSArtem Bityutskiy 		ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
7721e51764aSArtem Bityutskiy 			  buf_len, lnum, offs, err);
7731e51764aSArtem Bityutskiy 		dbg_dump_node(c, buf);
7741e51764aSArtem Bityutskiy 		dbg_dump_stack();
7751e51764aSArtem Bityutskiy 	}
7761e51764aSArtem Bityutskiy 
7771e51764aSArtem Bityutskiy 	return err;
7781e51764aSArtem Bityutskiy }
7791e51764aSArtem Bityutskiy 
7801e51764aSArtem Bityutskiy /**
7811e51764aSArtem Bityutskiy  * ubifs_read_node_wbuf - read node from the media or write-buffer.
7821e51764aSArtem Bityutskiy  * @wbuf: wbuf to check for un-written data
7831e51764aSArtem Bityutskiy  * @buf: buffer to read to
7841e51764aSArtem Bityutskiy  * @type: node type
7851e51764aSArtem Bityutskiy  * @len: node length
7861e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
7871e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
7881e51764aSArtem Bityutskiy  *
7891e51764aSArtem Bityutskiy  * This function reads a node of known type and length, checks it and stores
7901e51764aSArtem Bityutskiy  * in @buf. If the node partially or fully sits in the write-buffer, this
7911e51764aSArtem Bityutskiy  * function takes data from the buffer, otherwise it reads the flash media.
7921e51764aSArtem Bityutskiy  * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
7931e51764aSArtem Bityutskiy  * error code in case of failure.
7941e51764aSArtem Bityutskiy  */
7951e51764aSArtem Bityutskiy int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
7961e51764aSArtem Bityutskiy 			 int lnum, int offs)
7971e51764aSArtem Bityutskiy {
7981e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
7991e51764aSArtem Bityutskiy 	int err, rlen, overlap;
8001e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
8011e51764aSArtem Bityutskiy 
80277a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
80377a7ae58SArtem Bityutskiy 	       dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
8041e51764aSArtem Bityutskiy 	ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
8051e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
8061e51764aSArtem Bityutskiy 	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
8071e51764aSArtem Bityutskiy 
8081e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
8091e51764aSArtem Bityutskiy 	overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
8101e51764aSArtem Bityutskiy 	if (!overlap) {
8111e51764aSArtem Bityutskiy 		/* We may safely unlock the write-buffer and read the data */
8121e51764aSArtem Bityutskiy 		spin_unlock(&wbuf->lock);
8131e51764aSArtem Bityutskiy 		return ubifs_read_node(c, buf, type, len, lnum, offs);
8141e51764aSArtem Bityutskiy 	}
8151e51764aSArtem Bityutskiy 
8161e51764aSArtem Bityutskiy 	/* Don't read under wbuf */
8171e51764aSArtem Bityutskiy 	rlen = wbuf->offs - offs;
8181e51764aSArtem Bityutskiy 	if (rlen < 0)
8191e51764aSArtem Bityutskiy 		rlen = 0;
8201e51764aSArtem Bityutskiy 
8211e51764aSArtem Bityutskiy 	/* Copy the rest from the write-buffer */
8221e51764aSArtem Bityutskiy 	memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
8231e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
8241e51764aSArtem Bityutskiy 
8251e51764aSArtem Bityutskiy 	if (rlen > 0) {
8261e51764aSArtem Bityutskiy 		/* Read everything that goes before write-buffer */
8271e51764aSArtem Bityutskiy 		err = ubi_read(c->ubi, lnum, buf, offs, rlen);
8281e51764aSArtem Bityutskiy 		if (err && err != -EBADMSG) {
8291e51764aSArtem Bityutskiy 			ubifs_err("failed to read node %d from LEB %d:%d, "
8301e51764aSArtem Bityutskiy 				  "error %d", type, lnum, offs, err);
8311e51764aSArtem Bityutskiy 			dbg_dump_stack();
8321e51764aSArtem Bityutskiy 			return err;
8331e51764aSArtem Bityutskiy 		}
8341e51764aSArtem Bityutskiy 	}
8351e51764aSArtem Bityutskiy 
8361e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
8371e51764aSArtem Bityutskiy 		ubifs_err("bad node type (%d but expected %d)",
8381e51764aSArtem Bityutskiy 			  ch->node_type, type);
8391e51764aSArtem Bityutskiy 		goto out;
8401e51764aSArtem Bityutskiy 	}
8411e51764aSArtem Bityutskiy 
8422953e73fSAdrian Hunter 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
8431e51764aSArtem Bityutskiy 	if (err) {
8441e51764aSArtem Bityutskiy 		ubifs_err("expected node type %d", type);
8451e51764aSArtem Bityutskiy 		return err;
8461e51764aSArtem Bityutskiy 	}
8471e51764aSArtem Bityutskiy 
8481e51764aSArtem Bityutskiy 	rlen = le32_to_cpu(ch->len);
8491e51764aSArtem Bityutskiy 	if (rlen != len) {
8501e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d, expected %d", rlen, len);
8511e51764aSArtem Bityutskiy 		goto out;
8521e51764aSArtem Bityutskiy 	}
8531e51764aSArtem Bityutskiy 
8541e51764aSArtem Bityutskiy 	return 0;
8551e51764aSArtem Bityutskiy 
8561e51764aSArtem Bityutskiy out:
8571e51764aSArtem Bityutskiy 	ubifs_err("bad node at LEB %d:%d", lnum, offs);
8581e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
8591e51764aSArtem Bityutskiy 	dbg_dump_stack();
8601e51764aSArtem Bityutskiy 	return -EINVAL;
8611e51764aSArtem Bityutskiy }
8621e51764aSArtem Bityutskiy 
8631e51764aSArtem Bityutskiy /**
8641e51764aSArtem Bityutskiy  * ubifs_read_node - read node.
8651e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8661e51764aSArtem Bityutskiy  * @buf: buffer to read to
8671e51764aSArtem Bityutskiy  * @type: node type
8681e51764aSArtem Bityutskiy  * @len: node length (not aligned)
8691e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
8701e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
8711e51764aSArtem Bityutskiy  *
8721e51764aSArtem Bityutskiy  * This function reads a node of known type and and length, checks it and
8731e51764aSArtem Bityutskiy  * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
8741e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
8751e51764aSArtem Bityutskiy  */
8761e51764aSArtem Bityutskiy int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
8771e51764aSArtem Bityutskiy 		    int lnum, int offs)
8781e51764aSArtem Bityutskiy {
8791e51764aSArtem Bityutskiy 	int err, l;
8801e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
8811e51764aSArtem Bityutskiy 
8821e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
8831e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
8841e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
8851e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
8861e51764aSArtem Bityutskiy 	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
8871e51764aSArtem Bityutskiy 
8881e51764aSArtem Bityutskiy 	err = ubi_read(c->ubi, lnum, buf, offs, len);
8891e51764aSArtem Bityutskiy 	if (err && err != -EBADMSG) {
8901e51764aSArtem Bityutskiy 		ubifs_err("cannot read node %d from LEB %d:%d, error %d",
8911e51764aSArtem Bityutskiy 			  type, lnum, offs, err);
8921e51764aSArtem Bityutskiy 		return err;
8931e51764aSArtem Bityutskiy 	}
8941e51764aSArtem Bityutskiy 
8951e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
8961e51764aSArtem Bityutskiy 		ubifs_err("bad node type (%d but expected %d)",
8971e51764aSArtem Bityutskiy 			  ch->node_type, type);
8981e51764aSArtem Bityutskiy 		goto out;
8991e51764aSArtem Bityutskiy 	}
9001e51764aSArtem Bityutskiy 
9012953e73fSAdrian Hunter 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
9021e51764aSArtem Bityutskiy 	if (err) {
9031e51764aSArtem Bityutskiy 		ubifs_err("expected node type %d", type);
9041e51764aSArtem Bityutskiy 		return err;
9051e51764aSArtem Bityutskiy 	}
9061e51764aSArtem Bityutskiy 
9071e51764aSArtem Bityutskiy 	l = le32_to_cpu(ch->len);
9081e51764aSArtem Bityutskiy 	if (l != len) {
9091e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d, expected %d", l, len);
9101e51764aSArtem Bityutskiy 		goto out;
9111e51764aSArtem Bityutskiy 	}
9121e51764aSArtem Bityutskiy 
9131e51764aSArtem Bityutskiy 	return 0;
9141e51764aSArtem Bityutskiy 
9151e51764aSArtem Bityutskiy out:
9163a8fa0edSArtem Bityutskiy 	ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
9173a8fa0edSArtem Bityutskiy 		  ubi_is_mapped(c->ubi, lnum));
9181e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
9191e51764aSArtem Bityutskiy 	dbg_dump_stack();
9201e51764aSArtem Bityutskiy 	return -EINVAL;
9211e51764aSArtem Bityutskiy }
9221e51764aSArtem Bityutskiy 
9231e51764aSArtem Bityutskiy /**
9241e51764aSArtem Bityutskiy  * ubifs_wbuf_init - initialize write-buffer.
9251e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9261e51764aSArtem Bityutskiy  * @wbuf: write-buffer to initialize
9271e51764aSArtem Bityutskiy  *
928cb54ef8bSArtem Bityutskiy  * This function initializes write-buffer. Returns zero in case of success
9291e51764aSArtem Bityutskiy  * %-ENOMEM in case of failure.
9301e51764aSArtem Bityutskiy  */
9311e51764aSArtem Bityutskiy int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
9321e51764aSArtem Bityutskiy {
9331e51764aSArtem Bityutskiy 	size_t size;
9341e51764aSArtem Bityutskiy 
9356c7f74f7SArtem Bityutskiy 	wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
9361e51764aSArtem Bityutskiy 	if (!wbuf->buf)
9371e51764aSArtem Bityutskiy 		return -ENOMEM;
9381e51764aSArtem Bityutskiy 
9396c7f74f7SArtem Bityutskiy 	size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
9401e51764aSArtem Bityutskiy 	wbuf->inodes = kmalloc(size, GFP_KERNEL);
9411e51764aSArtem Bityutskiy 	if (!wbuf->inodes) {
9421e51764aSArtem Bityutskiy 		kfree(wbuf->buf);
9431e51764aSArtem Bityutskiy 		wbuf->buf = NULL;
9441e51764aSArtem Bityutskiy 		return -ENOMEM;
9451e51764aSArtem Bityutskiy 	}
9461e51764aSArtem Bityutskiy 
9471e51764aSArtem Bityutskiy 	wbuf->used = 0;
9481e51764aSArtem Bityutskiy 	wbuf->lnum = wbuf->offs = -1;
9496c7f74f7SArtem Bityutskiy 	/*
9506c7f74f7SArtem Bityutskiy 	 * If the LEB starts at the max. write size aligned address, then
9516c7f74f7SArtem Bityutskiy 	 * write-buffer size has to be set to @c->max_write_size. Otherwise,
9526c7f74f7SArtem Bityutskiy 	 * set it to something smaller so that it ends at the closest max.
9536c7f74f7SArtem Bityutskiy 	 * write size boundary.
9546c7f74f7SArtem Bityutskiy 	 */
9556c7f74f7SArtem Bityutskiy 	size = c->max_write_size - (c->leb_start % c->max_write_size);
9566c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size = size;
9571e51764aSArtem Bityutskiy 	wbuf->dtype = UBI_UNKNOWN;
9581e51764aSArtem Bityutskiy 	wbuf->sync_callback = NULL;
9591e51764aSArtem Bityutskiy 	mutex_init(&wbuf->io_mutex);
9601e51764aSArtem Bityutskiy 	spin_lock_init(&wbuf->lock);
9611e51764aSArtem Bityutskiy 	wbuf->c = c;
9621e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
9631e51764aSArtem Bityutskiy 
964f2c5dbd7SArtem Bityutskiy 	hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
965f2c5dbd7SArtem Bityutskiy 	wbuf->timer.function = wbuf_timer_callback_nolock;
9662a35a3a8SArtem Bityutskiy 	wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
9672a35a3a8SArtem Bityutskiy 	wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
9682a35a3a8SArtem Bityutskiy 	wbuf->delta *= 1000000000ULL;
9692a35a3a8SArtem Bityutskiy 	ubifs_assert(wbuf->delta <= ULONG_MAX);
9701e51764aSArtem Bityutskiy 	return 0;
9711e51764aSArtem Bityutskiy }
9721e51764aSArtem Bityutskiy 
9731e51764aSArtem Bityutskiy /**
9741e51764aSArtem Bityutskiy  * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
9751e51764aSArtem Bityutskiy  * @wbuf: the write-buffer where to add
9761e51764aSArtem Bityutskiy  * @inum: the inode number
9771e51764aSArtem Bityutskiy  *
9781e51764aSArtem Bityutskiy  * This function adds an inode number to the inode array of the write-buffer.
9791e51764aSArtem Bityutskiy  */
9801e51764aSArtem Bityutskiy void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
9811e51764aSArtem Bityutskiy {
9821e51764aSArtem Bityutskiy 	if (!wbuf->buf)
9831e51764aSArtem Bityutskiy 		/* NOR flash or something similar */
9841e51764aSArtem Bityutskiy 		return;
9851e51764aSArtem Bityutskiy 
9861e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
9871e51764aSArtem Bityutskiy 	if (wbuf->used)
9881e51764aSArtem Bityutskiy 		wbuf->inodes[wbuf->next_ino++] = inum;
9891e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
9901e51764aSArtem Bityutskiy }
9911e51764aSArtem Bityutskiy 
9921e51764aSArtem Bityutskiy /**
9931e51764aSArtem Bityutskiy  * wbuf_has_ino - returns if the wbuf contains data from the inode.
9941e51764aSArtem Bityutskiy  * @wbuf: the write-buffer
9951e51764aSArtem Bityutskiy  * @inum: the inode number
9961e51764aSArtem Bityutskiy  *
9971e51764aSArtem Bityutskiy  * This function returns with %1 if the write-buffer contains some data from the
9981e51764aSArtem Bityutskiy  * given inode otherwise it returns with %0.
9991e51764aSArtem Bityutskiy  */
10001e51764aSArtem Bityutskiy static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
10011e51764aSArtem Bityutskiy {
10021e51764aSArtem Bityutskiy 	int i, ret = 0;
10031e51764aSArtem Bityutskiy 
10041e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
10051e51764aSArtem Bityutskiy 	for (i = 0; i < wbuf->next_ino; i++)
10061e51764aSArtem Bityutskiy 		if (inum == wbuf->inodes[i]) {
10071e51764aSArtem Bityutskiy 			ret = 1;
10081e51764aSArtem Bityutskiy 			break;
10091e51764aSArtem Bityutskiy 		}
10101e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
10111e51764aSArtem Bityutskiy 
10121e51764aSArtem Bityutskiy 	return ret;
10131e51764aSArtem Bityutskiy }
10141e51764aSArtem Bityutskiy 
10151e51764aSArtem Bityutskiy /**
10161e51764aSArtem Bityutskiy  * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
10171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10181e51764aSArtem Bityutskiy  * @inode: inode to synchronize
10191e51764aSArtem Bityutskiy  *
10201e51764aSArtem Bityutskiy  * This function synchronizes write-buffers which contain nodes belonging to
10211e51764aSArtem Bityutskiy  * @inode. Returns zero in case of success and a negative error code in case of
10221e51764aSArtem Bityutskiy  * failure.
10231e51764aSArtem Bityutskiy  */
10241e51764aSArtem Bityutskiy int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
10251e51764aSArtem Bityutskiy {
10261e51764aSArtem Bityutskiy 	int i, err = 0;
10271e51764aSArtem Bityutskiy 
10281e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
10291e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
10301e51764aSArtem Bityutskiy 
10311e51764aSArtem Bityutskiy 		if (i == GCHD)
10321e51764aSArtem Bityutskiy 			/*
10331e51764aSArtem Bityutskiy 			 * GC head is special, do not look at it. Even if the
10341e51764aSArtem Bityutskiy 			 * head contains something related to this inode, it is
10351e51764aSArtem Bityutskiy 			 * a _copy_ of corresponding on-flash node which sits
10361e51764aSArtem Bityutskiy 			 * somewhere else.
10371e51764aSArtem Bityutskiy 			 */
10381e51764aSArtem Bityutskiy 			continue;
10391e51764aSArtem Bityutskiy 
10401e51764aSArtem Bityutskiy 		if (!wbuf_has_ino(wbuf, inode->i_ino))
10411e51764aSArtem Bityutskiy 			continue;
10421e51764aSArtem Bityutskiy 
10431e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
10441e51764aSArtem Bityutskiy 		if (wbuf_has_ino(wbuf, inode->i_ino))
10451e51764aSArtem Bityutskiy 			err = ubifs_wbuf_sync_nolock(wbuf);
10461e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
10471e51764aSArtem Bityutskiy 
10481e51764aSArtem Bityutskiy 		if (err) {
10491e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
10501e51764aSArtem Bityutskiy 			return err;
10511e51764aSArtem Bityutskiy 		}
10521e51764aSArtem Bityutskiy 	}
10531e51764aSArtem Bityutskiy 	return 0;
10541e51764aSArtem Bityutskiy }
1055