xref: /openbmc/linux/fs/ubifs/io.c (revision 2680d722)
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 {
642680d722SArtem Bityutskiy 	if (!c->ro_error) {
652680d722SArtem Bityutskiy 		c->ro_error = 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);
3622680d722SArtem Bityutskiy 	ubifs_assert(!c->ro_media);
3631e51764aSArtem Bityutskiy 
3642680d722SArtem Bityutskiy 	if (c->ro_error)
3651e51764aSArtem Bityutskiy 		return -EROFS;
3661e51764aSArtem Bityutskiy 
3671e51764aSArtem Bityutskiy 	ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
3681e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
3691e51764aSArtem Bityutskiy 			    c->min_io_size, wbuf->dtype);
3701e51764aSArtem Bityutskiy 	if (err) {
3711e51764aSArtem Bityutskiy 		ubifs_err("cannot write %d bytes to LEB %d:%d",
3721e51764aSArtem Bityutskiy 			  c->min_io_size, wbuf->lnum, wbuf->offs);
3731e51764aSArtem Bityutskiy 		dbg_dump_stack();
3741e51764aSArtem Bityutskiy 		return err;
3751e51764aSArtem Bityutskiy 	}
3761e51764aSArtem Bityutskiy 
3771e51764aSArtem Bityutskiy 	dirt = wbuf->avail;
3781e51764aSArtem Bityutskiy 
3791e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
3801e51764aSArtem Bityutskiy 	wbuf->offs += c->min_io_size;
3811e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size;
3821e51764aSArtem Bityutskiy 	wbuf->used = 0;
3831e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
3841e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
3851e51764aSArtem Bityutskiy 
3861e51764aSArtem Bityutskiy 	if (wbuf->sync_callback)
3871e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum,
3881e51764aSArtem Bityutskiy 					  c->leb_size - wbuf->offs, dirt);
3891e51764aSArtem Bityutskiy 	return err;
3901e51764aSArtem Bityutskiy }
3911e51764aSArtem Bityutskiy 
3921e51764aSArtem Bityutskiy /**
3931e51764aSArtem Bityutskiy  * ubifs_wbuf_seek_nolock - seek write-buffer.
3941e51764aSArtem Bityutskiy  * @wbuf: write-buffer
3951e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number to seek to
3961e51764aSArtem Bityutskiy  * @offs: logical eraseblock offset to seek to
3971e51764aSArtem Bityutskiy  * @dtype: data type
3981e51764aSArtem Bityutskiy  *
399cb54ef8bSArtem Bityutskiy  * This function targets the write-buffer to logical eraseblock @lnum:@offs.
4001e51764aSArtem Bityutskiy  * The write-buffer is synchronized if it is not empty. Returns zero in case of
4011e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
4021e51764aSArtem Bityutskiy  */
4031e51764aSArtem Bityutskiy int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
4041e51764aSArtem Bityutskiy 			   int dtype)
4051e51764aSArtem Bityutskiy {
4061e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
4071e51764aSArtem Bityutskiy 
40877a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
4091e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
4101e51764aSArtem Bityutskiy 	ubifs_assert(offs >= 0 && offs <= c->leb_size);
4111e51764aSArtem Bityutskiy 	ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
4121e51764aSArtem Bityutskiy 	ubifs_assert(lnum != wbuf->lnum);
4131e51764aSArtem Bityutskiy 
4141e51764aSArtem Bityutskiy 	if (wbuf->used > 0) {
4151e51764aSArtem Bityutskiy 		int err = ubifs_wbuf_sync_nolock(wbuf);
4161e51764aSArtem Bityutskiy 
4171e51764aSArtem Bityutskiy 		if (err)
4181e51764aSArtem Bityutskiy 			return err;
4191e51764aSArtem Bityutskiy 	}
4201e51764aSArtem Bityutskiy 
4211e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
4221e51764aSArtem Bityutskiy 	wbuf->lnum = lnum;
4231e51764aSArtem Bityutskiy 	wbuf->offs = offs;
4241e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size;
4251e51764aSArtem Bityutskiy 	wbuf->used = 0;
4261e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
4271e51764aSArtem Bityutskiy 	wbuf->dtype = dtype;
4281e51764aSArtem Bityutskiy 
4291e51764aSArtem Bityutskiy 	return 0;
4301e51764aSArtem Bityutskiy }
4311e51764aSArtem Bityutskiy 
4321e51764aSArtem Bityutskiy /**
4331e51764aSArtem Bityutskiy  * ubifs_bg_wbufs_sync - synchronize write-buffers.
4341e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4351e51764aSArtem Bityutskiy  *
4361e51764aSArtem Bityutskiy  * This function is called by background thread to synchronize write-buffers.
4371e51764aSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
4381e51764aSArtem Bityutskiy  * failure.
4391e51764aSArtem Bityutskiy  */
4401e51764aSArtem Bityutskiy int ubifs_bg_wbufs_sync(struct ubifs_info *c)
4411e51764aSArtem Bityutskiy {
4421e51764aSArtem Bityutskiy 	int err, i;
4431e51764aSArtem Bityutskiy 
4442680d722SArtem Bityutskiy 	ubifs_assert(!c->ro_media);
4451e51764aSArtem Bityutskiy 	if (!c->need_wbuf_sync)
4461e51764aSArtem Bityutskiy 		return 0;
4471e51764aSArtem Bityutskiy 	c->need_wbuf_sync = 0;
4481e51764aSArtem Bityutskiy 
4492680d722SArtem Bityutskiy 	if (c->ro_error) {
4501e51764aSArtem Bityutskiy 		err = -EROFS;
4511e51764aSArtem Bityutskiy 		goto out_timers;
4521e51764aSArtem Bityutskiy 	}
4531e51764aSArtem Bityutskiy 
4541e51764aSArtem Bityutskiy 	dbg_io("synchronize");
4551e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
4561e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
4571e51764aSArtem Bityutskiy 
4581e51764aSArtem Bityutskiy 		cond_resched();
4591e51764aSArtem Bityutskiy 
4601e51764aSArtem Bityutskiy 		/*
4611e51764aSArtem Bityutskiy 		 * If the mutex is locked then wbuf is being changed, so
4621e51764aSArtem Bityutskiy 		 * synchronization is not necessary.
4631e51764aSArtem Bityutskiy 		 */
4641e51764aSArtem Bityutskiy 		if (mutex_is_locked(&wbuf->io_mutex))
4651e51764aSArtem Bityutskiy 			continue;
4661e51764aSArtem Bityutskiy 
4671e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
4681e51764aSArtem Bityutskiy 		if (!wbuf->need_sync) {
4691e51764aSArtem Bityutskiy 			mutex_unlock(&wbuf->io_mutex);
4701e51764aSArtem Bityutskiy 			continue;
4711e51764aSArtem Bityutskiy 		}
4721e51764aSArtem Bityutskiy 
4731e51764aSArtem Bityutskiy 		err = ubifs_wbuf_sync_nolock(wbuf);
4741e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
4751e51764aSArtem Bityutskiy 		if (err) {
4761e51764aSArtem Bityutskiy 			ubifs_err("cannot sync write-buffer, error %d", err);
4771e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
4781e51764aSArtem Bityutskiy 			goto out_timers;
4791e51764aSArtem Bityutskiy 		}
4801e51764aSArtem Bityutskiy 	}
4811e51764aSArtem Bityutskiy 
4821e51764aSArtem Bityutskiy 	return 0;
4831e51764aSArtem Bityutskiy 
4841e51764aSArtem Bityutskiy out_timers:
4851e51764aSArtem Bityutskiy 	/* Cancel all timers to prevent repeated errors */
4861e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
4871e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
4881e51764aSArtem Bityutskiy 
4891e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
4901e51764aSArtem Bityutskiy 		cancel_wbuf_timer_nolock(wbuf);
4911e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
4921e51764aSArtem Bityutskiy 	}
4931e51764aSArtem Bityutskiy 	return err;
4941e51764aSArtem Bityutskiy }
4951e51764aSArtem Bityutskiy 
4961e51764aSArtem Bityutskiy /**
4971e51764aSArtem Bityutskiy  * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
4981e51764aSArtem Bityutskiy  * @wbuf: write-buffer
4991e51764aSArtem Bityutskiy  * @buf: node to write
5001e51764aSArtem Bityutskiy  * @len: node length
5011e51764aSArtem Bityutskiy  *
5021e51764aSArtem Bityutskiy  * This function writes data to flash via write-buffer @wbuf. This means that
5031e51764aSArtem Bityutskiy  * the last piece of the node won't reach the flash media immediately if it
5041e51764aSArtem Bityutskiy  * does not take whole minimal I/O unit. Instead, the node will sit in RAM
5051e51764aSArtem Bityutskiy  * until the write-buffer is synchronized (e.g., by timer).
5061e51764aSArtem Bityutskiy  *
5071e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
5081e51764aSArtem Bityutskiy  * case of failure. If the node cannot be written because there is no more
5091e51764aSArtem Bityutskiy  * space in this logical eraseblock, %-ENOSPC is returned.
5101e51764aSArtem Bityutskiy  */
5111e51764aSArtem Bityutskiy int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
5121e51764aSArtem Bityutskiy {
5131e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
5141e51764aSArtem Bityutskiy 	int err, written, n, aligned_len = ALIGN(len, 8), offs;
5151e51764aSArtem Bityutskiy 
51677a7ae58SArtem Bityutskiy 	dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
51777a7ae58SArtem Bityutskiy 	       dbg_ntype(((struct ubifs_ch *)buf)->node_type),
51877a7ae58SArtem Bityutskiy 	       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
5191e51764aSArtem Bityutskiy 	ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
5201e51764aSArtem Bityutskiy 	ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
5211e51764aSArtem Bityutskiy 	ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
5221e51764aSArtem Bityutskiy 	ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size);
5231e51764aSArtem Bityutskiy 	ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
5242680d722SArtem Bityutskiy 	ubifs_assert(!c->ro_media);
5251e51764aSArtem Bityutskiy 
5261e51764aSArtem Bityutskiy 	if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
5271e51764aSArtem Bityutskiy 		err = -ENOSPC;
5281e51764aSArtem Bityutskiy 		goto out;
5291e51764aSArtem Bityutskiy 	}
5301e51764aSArtem Bityutskiy 
5311e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
5321e51764aSArtem Bityutskiy 
5332680d722SArtem Bityutskiy 	if (c->ro_error)
5341e51764aSArtem Bityutskiy 		return -EROFS;
5351e51764aSArtem Bityutskiy 
5361e51764aSArtem Bityutskiy 	if (aligned_len <= wbuf->avail) {
5371e51764aSArtem Bityutskiy 		/*
5381e51764aSArtem Bityutskiy 		 * The node is not very large and fits entirely within
5391e51764aSArtem Bityutskiy 		 * write-buffer.
5401e51764aSArtem Bityutskiy 		 */
5411e51764aSArtem Bityutskiy 		memcpy(wbuf->buf + wbuf->used, buf, len);
5421e51764aSArtem Bityutskiy 
5431e51764aSArtem Bityutskiy 		if (aligned_len == wbuf->avail) {
54477a7ae58SArtem Bityutskiy 			dbg_io("flush jhead %s wbuf to LEB %d:%d",
54577a7ae58SArtem Bityutskiy 			       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
5461e51764aSArtem Bityutskiy 			err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
5471e51764aSArtem Bityutskiy 					    wbuf->offs, c->min_io_size,
5481e51764aSArtem Bityutskiy 					    wbuf->dtype);
5491e51764aSArtem Bityutskiy 			if (err)
5501e51764aSArtem Bityutskiy 				goto out;
5511e51764aSArtem Bityutskiy 
5521e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
5531e51764aSArtem Bityutskiy 			wbuf->offs += c->min_io_size;
5541e51764aSArtem Bityutskiy 			wbuf->avail = c->min_io_size;
5551e51764aSArtem Bityutskiy 			wbuf->used = 0;
5561e51764aSArtem Bityutskiy 			wbuf->next_ino = 0;
5571e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
5581e51764aSArtem Bityutskiy 		} else {
5591e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
5601e51764aSArtem Bityutskiy 			wbuf->avail -= aligned_len;
5611e51764aSArtem Bityutskiy 			wbuf->used += aligned_len;
5621e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
5631e51764aSArtem Bityutskiy 		}
5641e51764aSArtem Bityutskiy 
5651e51764aSArtem Bityutskiy 		goto exit;
5661e51764aSArtem Bityutskiy 	}
5671e51764aSArtem Bityutskiy 
5681e51764aSArtem Bityutskiy 	/*
5691e51764aSArtem Bityutskiy 	 * The node is large enough and does not fit entirely within current
5701e51764aSArtem Bityutskiy 	 * minimal I/O unit. We have to fill and flush write-buffer and switch
5711e51764aSArtem Bityutskiy 	 * to the next min. I/O unit.
5721e51764aSArtem Bityutskiy 	 */
57377a7ae58SArtem Bityutskiy 	dbg_io("flush jhead %s wbuf to LEB %d:%d",
57477a7ae58SArtem Bityutskiy 	       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
5751e51764aSArtem Bityutskiy 	memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
5761e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
5771e51764aSArtem Bityutskiy 			    c->min_io_size, wbuf->dtype);
5781e51764aSArtem Bityutskiy 	if (err)
5791e51764aSArtem Bityutskiy 		goto out;
5801e51764aSArtem Bityutskiy 
5811e51764aSArtem Bityutskiy 	offs = wbuf->offs + c->min_io_size;
5821e51764aSArtem Bityutskiy 	len -= wbuf->avail;
5831e51764aSArtem Bityutskiy 	aligned_len -= wbuf->avail;
5841e51764aSArtem Bityutskiy 	written = wbuf->avail;
5851e51764aSArtem Bityutskiy 
5861e51764aSArtem Bityutskiy 	/*
5871e51764aSArtem Bityutskiy 	 * The remaining data may take more whole min. I/O units, so write the
5881e51764aSArtem Bityutskiy 	 * remains multiple to min. I/O unit size directly to the flash media.
5891e51764aSArtem Bityutskiy 	 * We align node length to 8-byte boundary because we anyway flash wbuf
5901e51764aSArtem Bityutskiy 	 * if the remaining space is less than 8 bytes.
5911e51764aSArtem Bityutskiy 	 */
5921e51764aSArtem Bityutskiy 	n = aligned_len >> c->min_io_shift;
5931e51764aSArtem Bityutskiy 	if (n) {
5941e51764aSArtem Bityutskiy 		n <<= c->min_io_shift;
5951e51764aSArtem Bityutskiy 		dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
5961e51764aSArtem Bityutskiy 		err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
5971e51764aSArtem Bityutskiy 				    wbuf->dtype);
5981e51764aSArtem Bityutskiy 		if (err)
5991e51764aSArtem Bityutskiy 			goto out;
6001e51764aSArtem Bityutskiy 		offs += n;
6011e51764aSArtem Bityutskiy 		aligned_len -= n;
6021e51764aSArtem Bityutskiy 		len -= n;
6031e51764aSArtem Bityutskiy 		written += n;
6041e51764aSArtem Bityutskiy 	}
6051e51764aSArtem Bityutskiy 
6061e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
6071e51764aSArtem Bityutskiy 	if (aligned_len)
6081e51764aSArtem Bityutskiy 		/*
6091e51764aSArtem Bityutskiy 		 * And now we have what's left and what does not take whole
6101e51764aSArtem Bityutskiy 		 * min. I/O unit, so write it to the write-buffer and we are
6111e51764aSArtem Bityutskiy 		 * done.
6121e51764aSArtem Bityutskiy 		 */
6131e51764aSArtem Bityutskiy 		memcpy(wbuf->buf, buf + written, len);
6141e51764aSArtem Bityutskiy 
6151e51764aSArtem Bityutskiy 	wbuf->offs = offs;
6161e51764aSArtem Bityutskiy 	wbuf->used = aligned_len;
6171e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size - aligned_len;
6181e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
6191e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
6201e51764aSArtem Bityutskiy 
6211e51764aSArtem Bityutskiy exit:
6221e51764aSArtem Bityutskiy 	if (wbuf->sync_callback) {
6231e51764aSArtem Bityutskiy 		int free = c->leb_size - wbuf->offs - wbuf->used;
6241e51764aSArtem Bityutskiy 
6251e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
6261e51764aSArtem Bityutskiy 		if (err)
6271e51764aSArtem Bityutskiy 			goto out;
6281e51764aSArtem Bityutskiy 	}
6291e51764aSArtem Bityutskiy 
6301e51764aSArtem Bityutskiy 	if (wbuf->used)
6311e51764aSArtem Bityutskiy 		new_wbuf_timer_nolock(wbuf);
6321e51764aSArtem Bityutskiy 
6331e51764aSArtem Bityutskiy 	return 0;
6341e51764aSArtem Bityutskiy 
6351e51764aSArtem Bityutskiy out:
6361e51764aSArtem Bityutskiy 	ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
6371e51764aSArtem Bityutskiy 		  len, wbuf->lnum, wbuf->offs, err);
6381e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
6391e51764aSArtem Bityutskiy 	dbg_dump_stack();
6401e51764aSArtem Bityutskiy 	dbg_dump_leb(c, wbuf->lnum);
6411e51764aSArtem Bityutskiy 	return err;
6421e51764aSArtem Bityutskiy }
6431e51764aSArtem Bityutskiy 
6441e51764aSArtem Bityutskiy /**
6451e51764aSArtem Bityutskiy  * ubifs_write_node - write node to the media.
6461e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6471e51764aSArtem Bityutskiy  * @buf: the node to write
6481e51764aSArtem Bityutskiy  * @len: node length
6491e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
6501e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
6511e51764aSArtem Bityutskiy  * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
6521e51764aSArtem Bityutskiy  *
6531e51764aSArtem Bityutskiy  * This function automatically fills node magic number, assigns sequence
6541e51764aSArtem Bityutskiy  * number, and calculates node CRC checksum. The length of the @buf buffer has
6551e51764aSArtem Bityutskiy  * to be aligned to the minimal I/O unit size. This function automatically
6561e51764aSArtem Bityutskiy  * appends padding node and padding bytes if needed. Returns zero in case of
6571e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
6581e51764aSArtem Bityutskiy  */
6591e51764aSArtem Bityutskiy int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
6601e51764aSArtem Bityutskiy 		     int offs, int dtype)
6611e51764aSArtem Bityutskiy {
6621e51764aSArtem Bityutskiy 	int err, buf_len = ALIGN(len, c->min_io_size);
6631e51764aSArtem Bityutskiy 
6641e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
6651e51764aSArtem Bityutskiy 	       lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
6661e51764aSArtem Bityutskiy 	       buf_len);
6671e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
6681e51764aSArtem Bityutskiy 	ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
6692680d722SArtem Bityutskiy 	ubifs_assert(!c->ro_media);
6701e51764aSArtem Bityutskiy 
6712680d722SArtem Bityutskiy 	if (c->ro_error)
6721e51764aSArtem Bityutskiy 		return -EROFS;
6731e51764aSArtem Bityutskiy 
6741e51764aSArtem Bityutskiy 	ubifs_prepare_node(c, buf, len, 1);
6751e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
6761e51764aSArtem Bityutskiy 	if (err) {
6771e51764aSArtem Bityutskiy 		ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
6781e51764aSArtem Bityutskiy 			  buf_len, lnum, offs, err);
6791e51764aSArtem Bityutskiy 		dbg_dump_node(c, buf);
6801e51764aSArtem Bityutskiy 		dbg_dump_stack();
6811e51764aSArtem Bityutskiy 	}
6821e51764aSArtem Bityutskiy 
6831e51764aSArtem Bityutskiy 	return err;
6841e51764aSArtem Bityutskiy }
6851e51764aSArtem Bityutskiy 
6861e51764aSArtem Bityutskiy /**
6871e51764aSArtem Bityutskiy  * ubifs_read_node_wbuf - read node from the media or write-buffer.
6881e51764aSArtem Bityutskiy  * @wbuf: wbuf to check for un-written data
6891e51764aSArtem Bityutskiy  * @buf: buffer to read to
6901e51764aSArtem Bityutskiy  * @type: node type
6911e51764aSArtem Bityutskiy  * @len: node length
6921e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
6931e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
6941e51764aSArtem Bityutskiy  *
6951e51764aSArtem Bityutskiy  * This function reads a node of known type and length, checks it and stores
6961e51764aSArtem Bityutskiy  * in @buf. If the node partially or fully sits in the write-buffer, this
6971e51764aSArtem Bityutskiy  * function takes data from the buffer, otherwise it reads the flash media.
6981e51764aSArtem Bityutskiy  * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
6991e51764aSArtem Bityutskiy  * error code in case of failure.
7001e51764aSArtem Bityutskiy  */
7011e51764aSArtem Bityutskiy int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
7021e51764aSArtem Bityutskiy 			 int lnum, int offs)
7031e51764aSArtem Bityutskiy {
7041e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
7051e51764aSArtem Bityutskiy 	int err, rlen, overlap;
7061e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
7071e51764aSArtem Bityutskiy 
70877a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
70977a7ae58SArtem Bityutskiy 	       dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
7101e51764aSArtem Bityutskiy 	ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
7111e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
7121e51764aSArtem Bityutskiy 	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
7131e51764aSArtem Bityutskiy 
7141e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
7151e51764aSArtem Bityutskiy 	overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
7161e51764aSArtem Bityutskiy 	if (!overlap) {
7171e51764aSArtem Bityutskiy 		/* We may safely unlock the write-buffer and read the data */
7181e51764aSArtem Bityutskiy 		spin_unlock(&wbuf->lock);
7191e51764aSArtem Bityutskiy 		return ubifs_read_node(c, buf, type, len, lnum, offs);
7201e51764aSArtem Bityutskiy 	}
7211e51764aSArtem Bityutskiy 
7221e51764aSArtem Bityutskiy 	/* Don't read under wbuf */
7231e51764aSArtem Bityutskiy 	rlen = wbuf->offs - offs;
7241e51764aSArtem Bityutskiy 	if (rlen < 0)
7251e51764aSArtem Bityutskiy 		rlen = 0;
7261e51764aSArtem Bityutskiy 
7271e51764aSArtem Bityutskiy 	/* Copy the rest from the write-buffer */
7281e51764aSArtem Bityutskiy 	memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
7291e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
7301e51764aSArtem Bityutskiy 
7311e51764aSArtem Bityutskiy 	if (rlen > 0) {
7321e51764aSArtem Bityutskiy 		/* Read everything that goes before write-buffer */
7331e51764aSArtem Bityutskiy 		err = ubi_read(c->ubi, lnum, buf, offs, rlen);
7341e51764aSArtem Bityutskiy 		if (err && err != -EBADMSG) {
7351e51764aSArtem Bityutskiy 			ubifs_err("failed to read node %d from LEB %d:%d, "
7361e51764aSArtem Bityutskiy 				  "error %d", type, lnum, offs, err);
7371e51764aSArtem Bityutskiy 			dbg_dump_stack();
7381e51764aSArtem Bityutskiy 			return err;
7391e51764aSArtem Bityutskiy 		}
7401e51764aSArtem Bityutskiy 	}
7411e51764aSArtem Bityutskiy 
7421e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
7431e51764aSArtem Bityutskiy 		ubifs_err("bad node type (%d but expected %d)",
7441e51764aSArtem Bityutskiy 			  ch->node_type, type);
7451e51764aSArtem Bityutskiy 		goto out;
7461e51764aSArtem Bityutskiy 	}
7471e51764aSArtem Bityutskiy 
7482953e73fSAdrian Hunter 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
7491e51764aSArtem Bityutskiy 	if (err) {
7501e51764aSArtem Bityutskiy 		ubifs_err("expected node type %d", type);
7511e51764aSArtem Bityutskiy 		return err;
7521e51764aSArtem Bityutskiy 	}
7531e51764aSArtem Bityutskiy 
7541e51764aSArtem Bityutskiy 	rlen = le32_to_cpu(ch->len);
7551e51764aSArtem Bityutskiy 	if (rlen != len) {
7561e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d, expected %d", rlen, len);
7571e51764aSArtem Bityutskiy 		goto out;
7581e51764aSArtem Bityutskiy 	}
7591e51764aSArtem Bityutskiy 
7601e51764aSArtem Bityutskiy 	return 0;
7611e51764aSArtem Bityutskiy 
7621e51764aSArtem Bityutskiy out:
7631e51764aSArtem Bityutskiy 	ubifs_err("bad node at LEB %d:%d", lnum, offs);
7641e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
7651e51764aSArtem Bityutskiy 	dbg_dump_stack();
7661e51764aSArtem Bityutskiy 	return -EINVAL;
7671e51764aSArtem Bityutskiy }
7681e51764aSArtem Bityutskiy 
7691e51764aSArtem Bityutskiy /**
7701e51764aSArtem Bityutskiy  * ubifs_read_node - read node.
7711e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7721e51764aSArtem Bityutskiy  * @buf: buffer to read to
7731e51764aSArtem Bityutskiy  * @type: node type
7741e51764aSArtem Bityutskiy  * @len: node length (not aligned)
7751e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
7761e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
7771e51764aSArtem Bityutskiy  *
7781e51764aSArtem Bityutskiy  * This function reads a node of known type and and length, checks it and
7791e51764aSArtem Bityutskiy  * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
7801e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
7811e51764aSArtem Bityutskiy  */
7821e51764aSArtem Bityutskiy int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
7831e51764aSArtem Bityutskiy 		    int lnum, int offs)
7841e51764aSArtem Bityutskiy {
7851e51764aSArtem Bityutskiy 	int err, l;
7861e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
7871e51764aSArtem Bityutskiy 
7881e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
7891e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
7901e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
7911e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
7921e51764aSArtem Bityutskiy 	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
7931e51764aSArtem Bityutskiy 
7941e51764aSArtem Bityutskiy 	err = ubi_read(c->ubi, lnum, buf, offs, len);
7951e51764aSArtem Bityutskiy 	if (err && err != -EBADMSG) {
7961e51764aSArtem Bityutskiy 		ubifs_err("cannot read node %d from LEB %d:%d, error %d",
7971e51764aSArtem Bityutskiy 			  type, lnum, offs, err);
7981e51764aSArtem Bityutskiy 		return err;
7991e51764aSArtem Bityutskiy 	}
8001e51764aSArtem Bityutskiy 
8011e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
8021e51764aSArtem Bityutskiy 		ubifs_err("bad node type (%d but expected %d)",
8031e51764aSArtem Bityutskiy 			  ch->node_type, type);
8041e51764aSArtem Bityutskiy 		goto out;
8051e51764aSArtem Bityutskiy 	}
8061e51764aSArtem Bityutskiy 
8072953e73fSAdrian Hunter 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
8081e51764aSArtem Bityutskiy 	if (err) {
8091e51764aSArtem Bityutskiy 		ubifs_err("expected node type %d", type);
8101e51764aSArtem Bityutskiy 		return err;
8111e51764aSArtem Bityutskiy 	}
8121e51764aSArtem Bityutskiy 
8131e51764aSArtem Bityutskiy 	l = le32_to_cpu(ch->len);
8141e51764aSArtem Bityutskiy 	if (l != len) {
8151e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d, expected %d", l, len);
8161e51764aSArtem Bityutskiy 		goto out;
8171e51764aSArtem Bityutskiy 	}
8181e51764aSArtem Bityutskiy 
8191e51764aSArtem Bityutskiy 	return 0;
8201e51764aSArtem Bityutskiy 
8211e51764aSArtem Bityutskiy out:
8223a8fa0edSArtem Bityutskiy 	ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
8233a8fa0edSArtem Bityutskiy 		  ubi_is_mapped(c->ubi, lnum));
8241e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
8251e51764aSArtem Bityutskiy 	dbg_dump_stack();
8261e51764aSArtem Bityutskiy 	return -EINVAL;
8271e51764aSArtem Bityutskiy }
8281e51764aSArtem Bityutskiy 
8291e51764aSArtem Bityutskiy /**
8301e51764aSArtem Bityutskiy  * ubifs_wbuf_init - initialize write-buffer.
8311e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8321e51764aSArtem Bityutskiy  * @wbuf: write-buffer to initialize
8331e51764aSArtem Bityutskiy  *
834cb54ef8bSArtem Bityutskiy  * This function initializes write-buffer. Returns zero in case of success
8351e51764aSArtem Bityutskiy  * %-ENOMEM in case of failure.
8361e51764aSArtem Bityutskiy  */
8371e51764aSArtem Bityutskiy int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
8381e51764aSArtem Bityutskiy {
8391e51764aSArtem Bityutskiy 	size_t size;
8401e51764aSArtem Bityutskiy 
8411e51764aSArtem Bityutskiy 	wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
8421e51764aSArtem Bityutskiy 	if (!wbuf->buf)
8431e51764aSArtem Bityutskiy 		return -ENOMEM;
8441e51764aSArtem Bityutskiy 
8451e51764aSArtem Bityutskiy 	size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
8461e51764aSArtem Bityutskiy 	wbuf->inodes = kmalloc(size, GFP_KERNEL);
8471e51764aSArtem Bityutskiy 	if (!wbuf->inodes) {
8481e51764aSArtem Bityutskiy 		kfree(wbuf->buf);
8491e51764aSArtem Bityutskiy 		wbuf->buf = NULL;
8501e51764aSArtem Bityutskiy 		return -ENOMEM;
8511e51764aSArtem Bityutskiy 	}
8521e51764aSArtem Bityutskiy 
8531e51764aSArtem Bityutskiy 	wbuf->used = 0;
8541e51764aSArtem Bityutskiy 	wbuf->lnum = wbuf->offs = -1;
8551e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size;
8561e51764aSArtem Bityutskiy 	wbuf->dtype = UBI_UNKNOWN;
8571e51764aSArtem Bityutskiy 	wbuf->sync_callback = NULL;
8581e51764aSArtem Bityutskiy 	mutex_init(&wbuf->io_mutex);
8591e51764aSArtem Bityutskiy 	spin_lock_init(&wbuf->lock);
8601e51764aSArtem Bityutskiy 	wbuf->c = c;
8611e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
8621e51764aSArtem Bityutskiy 
863f2c5dbd7SArtem Bityutskiy 	hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
864f2c5dbd7SArtem Bityutskiy 	wbuf->timer.function = wbuf_timer_callback_nolock;
8652a35a3a8SArtem Bityutskiy 	wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
8662a35a3a8SArtem Bityutskiy 	wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
8672a35a3a8SArtem Bityutskiy 	wbuf->delta *= 1000000000ULL;
8682a35a3a8SArtem Bityutskiy 	ubifs_assert(wbuf->delta <= ULONG_MAX);
8691e51764aSArtem Bityutskiy 	return 0;
8701e51764aSArtem Bityutskiy }
8711e51764aSArtem Bityutskiy 
8721e51764aSArtem Bityutskiy /**
8731e51764aSArtem Bityutskiy  * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
8741e51764aSArtem Bityutskiy  * @wbuf: the write-buffer where to add
8751e51764aSArtem Bityutskiy  * @inum: the inode number
8761e51764aSArtem Bityutskiy  *
8771e51764aSArtem Bityutskiy  * This function adds an inode number to the inode array of the write-buffer.
8781e51764aSArtem Bityutskiy  */
8791e51764aSArtem Bityutskiy void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
8801e51764aSArtem Bityutskiy {
8811e51764aSArtem Bityutskiy 	if (!wbuf->buf)
8821e51764aSArtem Bityutskiy 		/* NOR flash or something similar */
8831e51764aSArtem Bityutskiy 		return;
8841e51764aSArtem Bityutskiy 
8851e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
8861e51764aSArtem Bityutskiy 	if (wbuf->used)
8871e51764aSArtem Bityutskiy 		wbuf->inodes[wbuf->next_ino++] = inum;
8881e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
8891e51764aSArtem Bityutskiy }
8901e51764aSArtem Bityutskiy 
8911e51764aSArtem Bityutskiy /**
8921e51764aSArtem Bityutskiy  * wbuf_has_ino - returns if the wbuf contains data from the inode.
8931e51764aSArtem Bityutskiy  * @wbuf: the write-buffer
8941e51764aSArtem Bityutskiy  * @inum: the inode number
8951e51764aSArtem Bityutskiy  *
8961e51764aSArtem Bityutskiy  * This function returns with %1 if the write-buffer contains some data from the
8971e51764aSArtem Bityutskiy  * given inode otherwise it returns with %0.
8981e51764aSArtem Bityutskiy  */
8991e51764aSArtem Bityutskiy static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
9001e51764aSArtem Bityutskiy {
9011e51764aSArtem Bityutskiy 	int i, ret = 0;
9021e51764aSArtem Bityutskiy 
9031e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
9041e51764aSArtem Bityutskiy 	for (i = 0; i < wbuf->next_ino; i++)
9051e51764aSArtem Bityutskiy 		if (inum == wbuf->inodes[i]) {
9061e51764aSArtem Bityutskiy 			ret = 1;
9071e51764aSArtem Bityutskiy 			break;
9081e51764aSArtem Bityutskiy 		}
9091e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
9101e51764aSArtem Bityutskiy 
9111e51764aSArtem Bityutskiy 	return ret;
9121e51764aSArtem Bityutskiy }
9131e51764aSArtem Bityutskiy 
9141e51764aSArtem Bityutskiy /**
9151e51764aSArtem Bityutskiy  * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
9161e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9171e51764aSArtem Bityutskiy  * @inode: inode to synchronize
9181e51764aSArtem Bityutskiy  *
9191e51764aSArtem Bityutskiy  * This function synchronizes write-buffers which contain nodes belonging to
9201e51764aSArtem Bityutskiy  * @inode. Returns zero in case of success and a negative error code in case of
9211e51764aSArtem Bityutskiy  * failure.
9221e51764aSArtem Bityutskiy  */
9231e51764aSArtem Bityutskiy int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
9241e51764aSArtem Bityutskiy {
9251e51764aSArtem Bityutskiy 	int i, err = 0;
9261e51764aSArtem Bityutskiy 
9271e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
9281e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
9291e51764aSArtem Bityutskiy 
9301e51764aSArtem Bityutskiy 		if (i == GCHD)
9311e51764aSArtem Bityutskiy 			/*
9321e51764aSArtem Bityutskiy 			 * GC head is special, do not look at it. Even if the
9331e51764aSArtem Bityutskiy 			 * head contains something related to this inode, it is
9341e51764aSArtem Bityutskiy 			 * a _copy_ of corresponding on-flash node which sits
9351e51764aSArtem Bityutskiy 			 * somewhere else.
9361e51764aSArtem Bityutskiy 			 */
9371e51764aSArtem Bityutskiy 			continue;
9381e51764aSArtem Bityutskiy 
9391e51764aSArtem Bityutskiy 		if (!wbuf_has_ino(wbuf, inode->i_ino))
9401e51764aSArtem Bityutskiy 			continue;
9411e51764aSArtem Bityutskiy 
9421e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
9431e51764aSArtem Bityutskiy 		if (wbuf_has_ino(wbuf, inode->i_ino))
9441e51764aSArtem Bityutskiy 			err = ubifs_wbuf_sync_nolock(wbuf);
9451e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
9461e51764aSArtem Bityutskiy 
9471e51764aSArtem Bityutskiy 		if (err) {
9481e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
9491e51764aSArtem Bityutskiy 			return err;
9501e51764aSArtem Bityutskiy 		}
9511e51764aSArtem Bityutskiy 	}
9521e51764aSArtem Bityutskiy 	return 0;
9531e51764aSArtem Bityutskiy }
954