xref: /openbmc/linux/fs/ubifs/io.c (revision cb54ef8b)
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>
541e51764aSArtem Bityutskiy #include "ubifs.h"
551e51764aSArtem Bityutskiy 
561e51764aSArtem Bityutskiy /**
57ff46d7b3SAdrian Hunter  * ubifs_ro_mode - switch UBIFS to read read-only mode.
58ff46d7b3SAdrian Hunter  * @c: UBIFS file-system description object
59ff46d7b3SAdrian Hunter  * @err: error code which is the reason of switching to R/O mode
60ff46d7b3SAdrian Hunter  */
61ff46d7b3SAdrian Hunter void ubifs_ro_mode(struct ubifs_info *c, int err)
62ff46d7b3SAdrian Hunter {
63ff46d7b3SAdrian Hunter 	if (!c->ro_media) {
64ff46d7b3SAdrian Hunter 		c->ro_media = 1;
65ccb3eba7SArtem Bityutskiy 		c->no_chk_data_crc = 0;
66ff46d7b3SAdrian Hunter 		ubifs_warn("switched to read-only mode, error %d", err);
67ff46d7b3SAdrian Hunter 		dbg_dump_stack();
68ff46d7b3SAdrian Hunter 	}
69ff46d7b3SAdrian Hunter }
70ff46d7b3SAdrian Hunter 
71ff46d7b3SAdrian Hunter /**
721e51764aSArtem Bityutskiy  * ubifs_check_node - check node.
731e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
741e51764aSArtem Bityutskiy  * @buf: node to check
751e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
761e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
771e51764aSArtem Bityutskiy  * @quiet: print no messages
786f7ab6d4SArtem Bityutskiy  * @must_chk_crc: indicates whether to always check the CRC
791e51764aSArtem Bityutskiy  *
801e51764aSArtem Bityutskiy  * This function checks node magic number and CRC checksum. This function also
811e51764aSArtem Bityutskiy  * validates node length to prevent UBIFS from becoming crazy when an attacker
821e51764aSArtem Bityutskiy  * feeds it a file-system image with incorrect nodes. For example, too large
831e51764aSArtem Bityutskiy  * node length in the common header could cause UBIFS to read memory outside of
841e51764aSArtem Bityutskiy  * allocated buffer when checking the CRC checksum.
851e51764aSArtem Bityutskiy  *
866f7ab6d4SArtem Bityutskiy  * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
876f7ab6d4SArtem Bityutskiy  * true, which is controlled by corresponding UBIFS mount option. However, if
886f7ab6d4SArtem Bityutskiy  * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
896f7ab6d4SArtem Bityutskiy  * checked. Similarly, if @c->always_chk_crc is true, @c->no_chk_data_crc is
906f7ab6d4SArtem Bityutskiy  * ignored and CRC is checked.
916f7ab6d4SArtem Bityutskiy  *
926f7ab6d4SArtem Bityutskiy  * This function returns zero in case of success and %-EUCLEAN in case of bad
936f7ab6d4SArtem Bityutskiy  * CRC or magic.
941e51764aSArtem Bityutskiy  */
951e51764aSArtem Bityutskiy int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
966f7ab6d4SArtem Bityutskiy 		     int offs, int quiet, int must_chk_crc)
971e51764aSArtem Bityutskiy {
981e51764aSArtem Bityutskiy 	int err = -EINVAL, type, node_len;
991e51764aSArtem Bityutskiy 	uint32_t crc, node_crc, magic;
1001e51764aSArtem Bityutskiy 	const struct ubifs_ch *ch = buf;
1011e51764aSArtem Bityutskiy 
1021e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1031e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
1041e51764aSArtem Bityutskiy 
1051e51764aSArtem Bityutskiy 	magic = le32_to_cpu(ch->magic);
1061e51764aSArtem Bityutskiy 	if (magic != UBIFS_NODE_MAGIC) {
1071e51764aSArtem Bityutskiy 		if (!quiet)
1081e51764aSArtem Bityutskiy 			ubifs_err("bad magic %#08x, expected %#08x",
1091e51764aSArtem Bityutskiy 				  magic, UBIFS_NODE_MAGIC);
1101e51764aSArtem Bityutskiy 		err = -EUCLEAN;
1111e51764aSArtem Bityutskiy 		goto out;
1121e51764aSArtem Bityutskiy 	}
1131e51764aSArtem Bityutskiy 
1141e51764aSArtem Bityutskiy 	type = ch->node_type;
1151e51764aSArtem Bityutskiy 	if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
1161e51764aSArtem Bityutskiy 		if (!quiet)
1171e51764aSArtem Bityutskiy 			ubifs_err("bad node type %d", type);
1181e51764aSArtem Bityutskiy 		goto out;
1191e51764aSArtem Bityutskiy 	}
1201e51764aSArtem Bityutskiy 
1211e51764aSArtem Bityutskiy 	node_len = le32_to_cpu(ch->len);
1221e51764aSArtem Bityutskiy 	if (node_len + offs > c->leb_size)
1231e51764aSArtem Bityutskiy 		goto out_len;
1241e51764aSArtem Bityutskiy 
1251e51764aSArtem Bityutskiy 	if (c->ranges[type].max_len == 0) {
1261e51764aSArtem Bityutskiy 		if (node_len != c->ranges[type].len)
1271e51764aSArtem Bityutskiy 			goto out_len;
1281e51764aSArtem Bityutskiy 	} else if (node_len < c->ranges[type].min_len ||
1291e51764aSArtem Bityutskiy 		   node_len > c->ranges[type].max_len)
1301e51764aSArtem Bityutskiy 		goto out_len;
1311e51764aSArtem Bityutskiy 
1326f7ab6d4SArtem Bityutskiy 	if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc &&
1336f7ab6d4SArtem Bityutskiy 	     c->no_chk_data_crc)
1342953e73fSAdrian Hunter 		return 0;
1352953e73fSAdrian Hunter 
1361e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
1371e51764aSArtem Bityutskiy 	node_crc = le32_to_cpu(ch->crc);
1381e51764aSArtem Bityutskiy 	if (crc != node_crc) {
1391e51764aSArtem Bityutskiy 		if (!quiet)
1401e51764aSArtem Bityutskiy 			ubifs_err("bad CRC: calculated %#08x, read %#08x",
1411e51764aSArtem Bityutskiy 				  crc, node_crc);
1421e51764aSArtem Bityutskiy 		err = -EUCLEAN;
1431e51764aSArtem Bityutskiy 		goto out;
1441e51764aSArtem Bityutskiy 	}
1451e51764aSArtem Bityutskiy 
1461e51764aSArtem Bityutskiy 	return 0;
1471e51764aSArtem Bityutskiy 
1481e51764aSArtem Bityutskiy out_len:
1491e51764aSArtem Bityutskiy 	if (!quiet)
1501e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d", node_len);
1511e51764aSArtem Bityutskiy out:
1521e51764aSArtem Bityutskiy 	if (!quiet) {
1531e51764aSArtem Bityutskiy 		ubifs_err("bad node at LEB %d:%d", lnum, offs);
1541e51764aSArtem Bityutskiy 		dbg_dump_node(c, buf);
1551e51764aSArtem Bityutskiy 		dbg_dump_stack();
1561e51764aSArtem Bityutskiy 	}
1571e51764aSArtem Bityutskiy 	return err;
1581e51764aSArtem Bityutskiy }
1591e51764aSArtem Bityutskiy 
1601e51764aSArtem Bityutskiy /**
1611e51764aSArtem Bityutskiy  * ubifs_pad - pad flash space.
1621e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1631e51764aSArtem Bityutskiy  * @buf: buffer to put padding to
1641e51764aSArtem Bityutskiy  * @pad: how many bytes to pad
1651e51764aSArtem Bityutskiy  *
1661e51764aSArtem Bityutskiy  * The flash media obliges us to write only in chunks of %c->min_io_size and
1671e51764aSArtem Bityutskiy  * when we have to write less data we add padding node to the write-buffer and
1681e51764aSArtem Bityutskiy  * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
1691e51764aSArtem Bityutskiy  * media is being scanned. If the amount of wasted space is not enough to fit a
1701e51764aSArtem Bityutskiy  * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
1711e51764aSArtem Bityutskiy  * pattern (%UBIFS_PADDING_BYTE).
1721e51764aSArtem Bityutskiy  *
1731e51764aSArtem Bityutskiy  * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
1741e51764aSArtem Bityutskiy  * used.
1751e51764aSArtem Bityutskiy  */
1761e51764aSArtem Bityutskiy void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
1771e51764aSArtem Bityutskiy {
1781e51764aSArtem Bityutskiy 	uint32_t crc;
1791e51764aSArtem Bityutskiy 
1801e51764aSArtem Bityutskiy 	ubifs_assert(pad >= 0 && !(pad & 7));
1811e51764aSArtem Bityutskiy 
1821e51764aSArtem Bityutskiy 	if (pad >= UBIFS_PAD_NODE_SZ) {
1831e51764aSArtem Bityutskiy 		struct ubifs_ch *ch = buf;
1841e51764aSArtem Bityutskiy 		struct ubifs_pad_node *pad_node = buf;
1851e51764aSArtem Bityutskiy 
1861e51764aSArtem Bityutskiy 		ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
1871e51764aSArtem Bityutskiy 		ch->node_type = UBIFS_PAD_NODE;
1881e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_NO_NODE_GROUP;
1891e51764aSArtem Bityutskiy 		ch->padding[0] = ch->padding[1] = 0;
1901e51764aSArtem Bityutskiy 		ch->sqnum = 0;
1911e51764aSArtem Bityutskiy 		ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
1921e51764aSArtem Bityutskiy 		pad -= UBIFS_PAD_NODE_SZ;
1931e51764aSArtem Bityutskiy 		pad_node->pad_len = cpu_to_le32(pad);
1941e51764aSArtem Bityutskiy 		crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
1951e51764aSArtem Bityutskiy 		ch->crc = cpu_to_le32(crc);
1961e51764aSArtem Bityutskiy 		memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
1971e51764aSArtem Bityutskiy 	} else if (pad > 0)
1981e51764aSArtem Bityutskiy 		/* Too little space, padding node won't fit */
1991e51764aSArtem Bityutskiy 		memset(buf, UBIFS_PADDING_BYTE, pad);
2001e51764aSArtem Bityutskiy }
2011e51764aSArtem Bityutskiy 
2021e51764aSArtem Bityutskiy /**
2031e51764aSArtem Bityutskiy  * next_sqnum - get next sequence number.
2041e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2051e51764aSArtem Bityutskiy  */
2061e51764aSArtem Bityutskiy static unsigned long long next_sqnum(struct ubifs_info *c)
2071e51764aSArtem Bityutskiy {
2081e51764aSArtem Bityutskiy 	unsigned long long sqnum;
2091e51764aSArtem Bityutskiy 
2101e51764aSArtem Bityutskiy 	spin_lock(&c->cnt_lock);
2111e51764aSArtem Bityutskiy 	sqnum = ++c->max_sqnum;
2121e51764aSArtem Bityutskiy 	spin_unlock(&c->cnt_lock);
2131e51764aSArtem Bityutskiy 
2141e51764aSArtem Bityutskiy 	if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
2151e51764aSArtem Bityutskiy 		if (sqnum >= SQNUM_WATERMARK) {
2161e51764aSArtem Bityutskiy 			ubifs_err("sequence number overflow %llu, end of life",
2171e51764aSArtem Bityutskiy 				  sqnum);
2181e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, -EINVAL);
2191e51764aSArtem Bityutskiy 		}
2201e51764aSArtem Bityutskiy 		ubifs_warn("running out of sequence numbers, end of life soon");
2211e51764aSArtem Bityutskiy 	}
2221e51764aSArtem Bityutskiy 
2231e51764aSArtem Bityutskiy 	return sqnum;
2241e51764aSArtem Bityutskiy }
2251e51764aSArtem Bityutskiy 
2261e51764aSArtem Bityutskiy /**
2271e51764aSArtem Bityutskiy  * ubifs_prepare_node - prepare node to be written to flash.
2281e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2291e51764aSArtem Bityutskiy  * @node: the node to pad
2301e51764aSArtem Bityutskiy  * @len: node length
2311e51764aSArtem Bityutskiy  * @pad: if the buffer has to be padded
2321e51764aSArtem Bityutskiy  *
2331e51764aSArtem Bityutskiy  * This function prepares node at @node to be written to the media - it
2341e51764aSArtem Bityutskiy  * calculates node CRC, fills the common header, and adds proper padding up to
2351e51764aSArtem Bityutskiy  * the next minimum I/O unit if @pad is not zero.
2361e51764aSArtem Bityutskiy  */
2371e51764aSArtem Bityutskiy void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
2381e51764aSArtem Bityutskiy {
2391e51764aSArtem Bityutskiy 	uint32_t crc;
2401e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = node;
2411e51764aSArtem Bityutskiy 	unsigned long long sqnum = next_sqnum(c);
2421e51764aSArtem Bityutskiy 
2431e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ);
2441e51764aSArtem Bityutskiy 
2451e51764aSArtem Bityutskiy 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
2461e51764aSArtem Bityutskiy 	ch->len = cpu_to_le32(len);
2471e51764aSArtem Bityutskiy 	ch->group_type = UBIFS_NO_NODE_GROUP;
2481e51764aSArtem Bityutskiy 	ch->sqnum = cpu_to_le64(sqnum);
2491e51764aSArtem Bityutskiy 	ch->padding[0] = ch->padding[1] = 0;
2501e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
2511e51764aSArtem Bityutskiy 	ch->crc = cpu_to_le32(crc);
2521e51764aSArtem Bityutskiy 
2531e51764aSArtem Bityutskiy 	if (pad) {
2541e51764aSArtem Bityutskiy 		len = ALIGN(len, 8);
2551e51764aSArtem Bityutskiy 		pad = ALIGN(len, c->min_io_size) - len;
2561e51764aSArtem Bityutskiy 		ubifs_pad(c, node + len, pad);
2571e51764aSArtem Bityutskiy 	}
2581e51764aSArtem Bityutskiy }
2591e51764aSArtem Bityutskiy 
2601e51764aSArtem Bityutskiy /**
2611e51764aSArtem Bityutskiy  * ubifs_prep_grp_node - prepare node of a group to be written to flash.
2621e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2631e51764aSArtem Bityutskiy  * @node: the node to pad
2641e51764aSArtem Bityutskiy  * @len: node length
2651e51764aSArtem Bityutskiy  * @last: indicates the last node of the group
2661e51764aSArtem Bityutskiy  *
2671e51764aSArtem Bityutskiy  * This function prepares node at @node to be written to the media - it
2681e51764aSArtem Bityutskiy  * calculates node CRC and fills the common header.
2691e51764aSArtem Bityutskiy  */
2701e51764aSArtem Bityutskiy void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
2711e51764aSArtem Bityutskiy {
2721e51764aSArtem Bityutskiy 	uint32_t crc;
2731e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = node;
2741e51764aSArtem Bityutskiy 	unsigned long long sqnum = next_sqnum(c);
2751e51764aSArtem Bityutskiy 
2761e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ);
2771e51764aSArtem Bityutskiy 
2781e51764aSArtem Bityutskiy 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
2791e51764aSArtem Bityutskiy 	ch->len = cpu_to_le32(len);
2801e51764aSArtem Bityutskiy 	if (last)
2811e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
2821e51764aSArtem Bityutskiy 	else
2831e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_IN_NODE_GROUP;
2841e51764aSArtem Bityutskiy 	ch->sqnum = cpu_to_le64(sqnum);
2851e51764aSArtem Bityutskiy 	ch->padding[0] = ch->padding[1] = 0;
2861e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
2871e51764aSArtem Bityutskiy 	ch->crc = cpu_to_le32(crc);
2881e51764aSArtem Bityutskiy }
2891e51764aSArtem Bityutskiy 
2901e51764aSArtem Bityutskiy /**
2911e51764aSArtem Bityutskiy  * wbuf_timer_callback - write-buffer timer callback function.
2921e51764aSArtem Bityutskiy  * @data: timer data (write-buffer descriptor)
2931e51764aSArtem Bityutskiy  *
2941e51764aSArtem Bityutskiy  * This function is called when the write-buffer timer expires.
2951e51764aSArtem Bityutskiy  */
296f2c5dbd7SArtem Bityutskiy static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
2971e51764aSArtem Bityutskiy {
298f2c5dbd7SArtem Bityutskiy 	struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
2991e51764aSArtem Bityutskiy 
30070aee2f1SArtem Bityutskiy 	dbg_io("jhead %d", wbuf->jhead);
3011e51764aSArtem Bityutskiy 	wbuf->need_sync = 1;
3021e51764aSArtem Bityutskiy 	wbuf->c->need_wbuf_sync = 1;
3031e51764aSArtem Bityutskiy 	ubifs_wake_up_bgt(wbuf->c);
304f2c5dbd7SArtem Bityutskiy 	return HRTIMER_NORESTART;
3051e51764aSArtem Bityutskiy }
3061e51764aSArtem Bityutskiy 
3071e51764aSArtem Bityutskiy /**
3081e51764aSArtem Bityutskiy  * new_wbuf_timer - start new write-buffer timer.
3091e51764aSArtem Bityutskiy  * @wbuf: write-buffer descriptor
3101e51764aSArtem Bityutskiy  */
3111e51764aSArtem Bityutskiy static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
3121e51764aSArtem Bityutskiy {
313f2c5dbd7SArtem Bityutskiy 	ubifs_assert(!hrtimer_active(&wbuf->timer));
3141e51764aSArtem Bityutskiy 
3150b335b9dSArtem Bityutskiy 	if (wbuf->no_timer)
3161e51764aSArtem Bityutskiy 		return;
31770aee2f1SArtem Bityutskiy 	dbg_io("set timer for jhead %d, %llu-%llu millisecs", wbuf->jhead,
31870aee2f1SArtem Bityutskiy 	       ktime_to_ns(wbuf->softlimit)/USEC_PER_SEC,
31970aee2f1SArtem Bityutskiy 	       (ktime_to_ns(wbuf->softlimit) + wbuf->delta)/USEC_PER_SEC);
320f2c5dbd7SArtem Bityutskiy 	hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
321f2c5dbd7SArtem Bityutskiy 			       HRTIMER_MODE_REL);
3221e51764aSArtem Bityutskiy }
3231e51764aSArtem Bityutskiy 
3241e51764aSArtem Bityutskiy /**
3251e51764aSArtem Bityutskiy  * cancel_wbuf_timer - cancel write-buffer timer.
3261e51764aSArtem Bityutskiy  * @wbuf: write-buffer descriptor
3271e51764aSArtem Bityutskiy  */
3281e51764aSArtem Bityutskiy static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
3291e51764aSArtem Bityutskiy {
3300b335b9dSArtem Bityutskiy 	if (wbuf->no_timer)
3310b335b9dSArtem Bityutskiy 		return;
3321e51764aSArtem Bityutskiy 	wbuf->need_sync = 0;
333f2c5dbd7SArtem Bityutskiy 	hrtimer_cancel(&wbuf->timer);
3341e51764aSArtem Bityutskiy }
3351e51764aSArtem Bityutskiy 
3361e51764aSArtem Bityutskiy /**
3371e51764aSArtem Bityutskiy  * ubifs_wbuf_sync_nolock - synchronize write-buffer.
3381e51764aSArtem Bityutskiy  * @wbuf: write-buffer to synchronize
3391e51764aSArtem Bityutskiy  *
3401e51764aSArtem Bityutskiy  * This function synchronizes write-buffer @buf and returns zero in case of
3411e51764aSArtem Bityutskiy  * success or a negative error code in case of failure.
3421e51764aSArtem Bityutskiy  */
3431e51764aSArtem Bityutskiy int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
3441e51764aSArtem Bityutskiy {
3451e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
3461e51764aSArtem Bityutskiy 	int err, dirt;
3471e51764aSArtem Bityutskiy 
3481e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
3491e51764aSArtem Bityutskiy 	if (!wbuf->used || wbuf->lnum == -1)
3501e51764aSArtem Bityutskiy 		/* Write-buffer is empty or not seeked */
3511e51764aSArtem Bityutskiy 		return 0;
3521e51764aSArtem Bityutskiy 
35370aee2f1SArtem Bityutskiy 	dbg_io("LEB %d:%d, %d bytes, jhead %d",
35470aee2f1SArtem Bityutskiy 	       wbuf->lnum, wbuf->offs, wbuf->used, wbuf->jhead);
3551e51764aSArtem Bityutskiy 	ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
3561e51764aSArtem Bityutskiy 	ubifs_assert(!(wbuf->avail & 7));
3571e51764aSArtem Bityutskiy 	ubifs_assert(wbuf->offs + c->min_io_size <= c->leb_size);
3581e51764aSArtem Bityutskiy 
3591e51764aSArtem Bityutskiy 	if (c->ro_media)
3601e51764aSArtem Bityutskiy 		return -EROFS;
3611e51764aSArtem Bityutskiy 
3621e51764aSArtem Bityutskiy 	ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
3631e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
3641e51764aSArtem Bityutskiy 			    c->min_io_size, wbuf->dtype);
3651e51764aSArtem Bityutskiy 	if (err) {
3661e51764aSArtem Bityutskiy 		ubifs_err("cannot write %d bytes to LEB %d:%d",
3671e51764aSArtem Bityutskiy 			  c->min_io_size, wbuf->lnum, wbuf->offs);
3681e51764aSArtem Bityutskiy 		dbg_dump_stack();
3691e51764aSArtem Bityutskiy 		return err;
3701e51764aSArtem Bityutskiy 	}
3711e51764aSArtem Bityutskiy 
3721e51764aSArtem Bityutskiy 	dirt = wbuf->avail;
3731e51764aSArtem Bityutskiy 
3741e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
3751e51764aSArtem Bityutskiy 	wbuf->offs += c->min_io_size;
3761e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size;
3771e51764aSArtem Bityutskiy 	wbuf->used = 0;
3781e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
3791e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
3801e51764aSArtem Bityutskiy 
3811e51764aSArtem Bityutskiy 	if (wbuf->sync_callback)
3821e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum,
3831e51764aSArtem Bityutskiy 					  c->leb_size - wbuf->offs, dirt);
3841e51764aSArtem Bityutskiy 	return err;
3851e51764aSArtem Bityutskiy }
3861e51764aSArtem Bityutskiy 
3871e51764aSArtem Bityutskiy /**
3881e51764aSArtem Bityutskiy  * ubifs_wbuf_seek_nolock - seek write-buffer.
3891e51764aSArtem Bityutskiy  * @wbuf: write-buffer
3901e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number to seek to
3911e51764aSArtem Bityutskiy  * @offs: logical eraseblock offset to seek to
3921e51764aSArtem Bityutskiy  * @dtype: data type
3931e51764aSArtem Bityutskiy  *
394cb54ef8bSArtem Bityutskiy  * This function targets the write-buffer to logical eraseblock @lnum:@offs.
3951e51764aSArtem Bityutskiy  * The write-buffer is synchronized if it is not empty. Returns zero in case of
3961e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
3971e51764aSArtem Bityutskiy  */
3981e51764aSArtem Bityutskiy int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
3991e51764aSArtem Bityutskiy 			   int dtype)
4001e51764aSArtem Bityutskiy {
4011e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
4021e51764aSArtem Bityutskiy 
40370aee2f1SArtem Bityutskiy 	dbg_io("LEB %d:%d, jhead %d", lnum, offs, wbuf->jhead);
4041e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
4051e51764aSArtem Bityutskiy 	ubifs_assert(offs >= 0 && offs <= c->leb_size);
4061e51764aSArtem Bityutskiy 	ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
4071e51764aSArtem Bityutskiy 	ubifs_assert(lnum != wbuf->lnum);
4081e51764aSArtem Bityutskiy 
4091e51764aSArtem Bityutskiy 	if (wbuf->used > 0) {
4101e51764aSArtem Bityutskiy 		int err = ubifs_wbuf_sync_nolock(wbuf);
4111e51764aSArtem Bityutskiy 
4121e51764aSArtem Bityutskiy 		if (err)
4131e51764aSArtem Bityutskiy 			return err;
4141e51764aSArtem Bityutskiy 	}
4151e51764aSArtem Bityutskiy 
4161e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
4171e51764aSArtem Bityutskiy 	wbuf->lnum = lnum;
4181e51764aSArtem Bityutskiy 	wbuf->offs = offs;
4191e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size;
4201e51764aSArtem Bityutskiy 	wbuf->used = 0;
4211e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
4221e51764aSArtem Bityutskiy 	wbuf->dtype = dtype;
4231e51764aSArtem Bityutskiy 
4241e51764aSArtem Bityutskiy 	return 0;
4251e51764aSArtem Bityutskiy }
4261e51764aSArtem Bityutskiy 
4271e51764aSArtem Bityutskiy /**
4281e51764aSArtem Bityutskiy  * ubifs_bg_wbufs_sync - synchronize write-buffers.
4291e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4301e51764aSArtem Bityutskiy  *
4311e51764aSArtem Bityutskiy  * This function is called by background thread to synchronize write-buffers.
4321e51764aSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
4331e51764aSArtem Bityutskiy  * failure.
4341e51764aSArtem Bityutskiy  */
4351e51764aSArtem Bityutskiy int ubifs_bg_wbufs_sync(struct ubifs_info *c)
4361e51764aSArtem Bityutskiy {
4371e51764aSArtem Bityutskiy 	int err, i;
4381e51764aSArtem Bityutskiy 
4391e51764aSArtem Bityutskiy 	if (!c->need_wbuf_sync)
4401e51764aSArtem Bityutskiy 		return 0;
4411e51764aSArtem Bityutskiy 	c->need_wbuf_sync = 0;
4421e51764aSArtem Bityutskiy 
4431e51764aSArtem Bityutskiy 	if (c->ro_media) {
4441e51764aSArtem Bityutskiy 		err = -EROFS;
4451e51764aSArtem Bityutskiy 		goto out_timers;
4461e51764aSArtem Bityutskiy 	}
4471e51764aSArtem Bityutskiy 
4481e51764aSArtem Bityutskiy 	dbg_io("synchronize");
4491e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
4501e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
4511e51764aSArtem Bityutskiy 
4521e51764aSArtem Bityutskiy 		cond_resched();
4531e51764aSArtem Bityutskiy 
4541e51764aSArtem Bityutskiy 		/*
4551e51764aSArtem Bityutskiy 		 * If the mutex is locked then wbuf is being changed, so
4561e51764aSArtem Bityutskiy 		 * synchronization is not necessary.
4571e51764aSArtem Bityutskiy 		 */
4581e51764aSArtem Bityutskiy 		if (mutex_is_locked(&wbuf->io_mutex))
4591e51764aSArtem Bityutskiy 			continue;
4601e51764aSArtem Bityutskiy 
4611e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
4621e51764aSArtem Bityutskiy 		if (!wbuf->need_sync) {
4631e51764aSArtem Bityutskiy 			mutex_unlock(&wbuf->io_mutex);
4641e51764aSArtem Bityutskiy 			continue;
4651e51764aSArtem Bityutskiy 		}
4661e51764aSArtem Bityutskiy 
4671e51764aSArtem Bityutskiy 		err = ubifs_wbuf_sync_nolock(wbuf);
4681e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
4691e51764aSArtem Bityutskiy 		if (err) {
4701e51764aSArtem Bityutskiy 			ubifs_err("cannot sync write-buffer, error %d", err);
4711e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
4721e51764aSArtem Bityutskiy 			goto out_timers;
4731e51764aSArtem Bityutskiy 		}
4741e51764aSArtem Bityutskiy 	}
4751e51764aSArtem Bityutskiy 
4761e51764aSArtem Bityutskiy 	return 0;
4771e51764aSArtem Bityutskiy 
4781e51764aSArtem Bityutskiy out_timers:
4791e51764aSArtem Bityutskiy 	/* Cancel all timers to prevent repeated errors */
4801e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
4811e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
4821e51764aSArtem Bityutskiy 
4831e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
4841e51764aSArtem Bityutskiy 		cancel_wbuf_timer_nolock(wbuf);
4851e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
4861e51764aSArtem Bityutskiy 	}
4871e51764aSArtem Bityutskiy 	return err;
4881e51764aSArtem Bityutskiy }
4891e51764aSArtem Bityutskiy 
4901e51764aSArtem Bityutskiy /**
4911e51764aSArtem Bityutskiy  * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
4921e51764aSArtem Bityutskiy  * @wbuf: write-buffer
4931e51764aSArtem Bityutskiy  * @buf: node to write
4941e51764aSArtem Bityutskiy  * @len: node length
4951e51764aSArtem Bityutskiy  *
4961e51764aSArtem Bityutskiy  * This function writes data to flash via write-buffer @wbuf. This means that
4971e51764aSArtem Bityutskiy  * the last piece of the node won't reach the flash media immediately if it
4981e51764aSArtem Bityutskiy  * does not take whole minimal I/O unit. Instead, the node will sit in RAM
4991e51764aSArtem Bityutskiy  * until the write-buffer is synchronized (e.g., by timer).
5001e51764aSArtem Bityutskiy  *
5011e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
5021e51764aSArtem Bityutskiy  * case of failure. If the node cannot be written because there is no more
5031e51764aSArtem Bityutskiy  * space in this logical eraseblock, %-ENOSPC is returned.
5041e51764aSArtem Bityutskiy  */
5051e51764aSArtem Bityutskiy int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
5061e51764aSArtem Bityutskiy {
5071e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
5081e51764aSArtem Bityutskiy 	int err, written, n, aligned_len = ALIGN(len, 8), offs;
5091e51764aSArtem Bityutskiy 
51070aee2f1SArtem Bityutskiy 	dbg_io("%d bytes (%s) to jhead %d wbuf at LEB %d:%d", len,
51170aee2f1SArtem Bityutskiy 	       dbg_ntype(((struct ubifs_ch *)buf)->node_type), wbuf->jhead,
51270aee2f1SArtem Bityutskiy 	       wbuf->lnum, wbuf->offs + wbuf->used);
5131e51764aSArtem Bityutskiy 	ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
5141e51764aSArtem Bityutskiy 	ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
5151e51764aSArtem Bityutskiy 	ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
5161e51764aSArtem Bityutskiy 	ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size);
5171e51764aSArtem Bityutskiy 	ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
5181e51764aSArtem Bityutskiy 
5191e51764aSArtem Bityutskiy 	if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
5201e51764aSArtem Bityutskiy 		err = -ENOSPC;
5211e51764aSArtem Bityutskiy 		goto out;
5221e51764aSArtem Bityutskiy 	}
5231e51764aSArtem Bityutskiy 
5241e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
5251e51764aSArtem Bityutskiy 
5261e51764aSArtem Bityutskiy 	if (c->ro_media)
5271e51764aSArtem Bityutskiy 		return -EROFS;
5281e51764aSArtem Bityutskiy 
5291e51764aSArtem Bityutskiy 	if (aligned_len <= wbuf->avail) {
5301e51764aSArtem Bityutskiy 		/*
5311e51764aSArtem Bityutskiy 		 * The node is not very large and fits entirely within
5321e51764aSArtem Bityutskiy 		 * write-buffer.
5331e51764aSArtem Bityutskiy 		 */
5341e51764aSArtem Bityutskiy 		memcpy(wbuf->buf + wbuf->used, buf, len);
5351e51764aSArtem Bityutskiy 
5361e51764aSArtem Bityutskiy 		if (aligned_len == wbuf->avail) {
53770aee2f1SArtem Bityutskiy 			dbg_io("flush jhead %d wbuf to LEB %d:%d",
53870aee2f1SArtem Bityutskiy 			       wbuf->jhead, wbuf->lnum, wbuf->offs);
5391e51764aSArtem Bityutskiy 			err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
5401e51764aSArtem Bityutskiy 					    wbuf->offs, c->min_io_size,
5411e51764aSArtem Bityutskiy 					    wbuf->dtype);
5421e51764aSArtem Bityutskiy 			if (err)
5431e51764aSArtem Bityutskiy 				goto out;
5441e51764aSArtem Bityutskiy 
5451e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
5461e51764aSArtem Bityutskiy 			wbuf->offs += c->min_io_size;
5471e51764aSArtem Bityutskiy 			wbuf->avail = c->min_io_size;
5481e51764aSArtem Bityutskiy 			wbuf->used = 0;
5491e51764aSArtem Bityutskiy 			wbuf->next_ino = 0;
5501e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
5511e51764aSArtem Bityutskiy 		} else {
5521e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
5531e51764aSArtem Bityutskiy 			wbuf->avail -= aligned_len;
5541e51764aSArtem Bityutskiy 			wbuf->used += aligned_len;
5551e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
5561e51764aSArtem Bityutskiy 		}
5571e51764aSArtem Bityutskiy 
5581e51764aSArtem Bityutskiy 		goto exit;
5591e51764aSArtem Bityutskiy 	}
5601e51764aSArtem Bityutskiy 
5611e51764aSArtem Bityutskiy 	/*
5621e51764aSArtem Bityutskiy 	 * The node is large enough and does not fit entirely within current
5631e51764aSArtem Bityutskiy 	 * minimal I/O unit. We have to fill and flush write-buffer and switch
5641e51764aSArtem Bityutskiy 	 * to the next min. I/O unit.
5651e51764aSArtem Bityutskiy 	 */
56670aee2f1SArtem Bityutskiy 	dbg_io("flush jhead %d wbuf to LEB %d:%d",
56770aee2f1SArtem Bityutskiy 	       wbuf->jhead, wbuf->lnum, wbuf->offs);
5681e51764aSArtem Bityutskiy 	memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
5691e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
5701e51764aSArtem Bityutskiy 			    c->min_io_size, wbuf->dtype);
5711e51764aSArtem Bityutskiy 	if (err)
5721e51764aSArtem Bityutskiy 		goto out;
5731e51764aSArtem Bityutskiy 
5741e51764aSArtem Bityutskiy 	offs = wbuf->offs + c->min_io_size;
5751e51764aSArtem Bityutskiy 	len -= wbuf->avail;
5761e51764aSArtem Bityutskiy 	aligned_len -= wbuf->avail;
5771e51764aSArtem Bityutskiy 	written = wbuf->avail;
5781e51764aSArtem Bityutskiy 
5791e51764aSArtem Bityutskiy 	/*
5801e51764aSArtem Bityutskiy 	 * The remaining data may take more whole min. I/O units, so write the
5811e51764aSArtem Bityutskiy 	 * remains multiple to min. I/O unit size directly to the flash media.
5821e51764aSArtem Bityutskiy 	 * We align node length to 8-byte boundary because we anyway flash wbuf
5831e51764aSArtem Bityutskiy 	 * if the remaining space is less than 8 bytes.
5841e51764aSArtem Bityutskiy 	 */
5851e51764aSArtem Bityutskiy 	n = aligned_len >> c->min_io_shift;
5861e51764aSArtem Bityutskiy 	if (n) {
5871e51764aSArtem Bityutskiy 		n <<= c->min_io_shift;
5881e51764aSArtem Bityutskiy 		dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
5891e51764aSArtem Bityutskiy 		err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
5901e51764aSArtem Bityutskiy 				    wbuf->dtype);
5911e51764aSArtem Bityutskiy 		if (err)
5921e51764aSArtem Bityutskiy 			goto out;
5931e51764aSArtem Bityutskiy 		offs += n;
5941e51764aSArtem Bityutskiy 		aligned_len -= n;
5951e51764aSArtem Bityutskiy 		len -= n;
5961e51764aSArtem Bityutskiy 		written += n;
5971e51764aSArtem Bityutskiy 	}
5981e51764aSArtem Bityutskiy 
5991e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
6001e51764aSArtem Bityutskiy 	if (aligned_len)
6011e51764aSArtem Bityutskiy 		/*
6021e51764aSArtem Bityutskiy 		 * And now we have what's left and what does not take whole
6031e51764aSArtem Bityutskiy 		 * min. I/O unit, so write it to the write-buffer and we are
6041e51764aSArtem Bityutskiy 		 * done.
6051e51764aSArtem Bityutskiy 		 */
6061e51764aSArtem Bityutskiy 		memcpy(wbuf->buf, buf + written, len);
6071e51764aSArtem Bityutskiy 
6081e51764aSArtem Bityutskiy 	wbuf->offs = offs;
6091e51764aSArtem Bityutskiy 	wbuf->used = aligned_len;
6101e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size - aligned_len;
6111e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
6121e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
6131e51764aSArtem Bityutskiy 
6141e51764aSArtem Bityutskiy exit:
6151e51764aSArtem Bityutskiy 	if (wbuf->sync_callback) {
6161e51764aSArtem Bityutskiy 		int free = c->leb_size - wbuf->offs - wbuf->used;
6171e51764aSArtem Bityutskiy 
6181e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
6191e51764aSArtem Bityutskiy 		if (err)
6201e51764aSArtem Bityutskiy 			goto out;
6211e51764aSArtem Bityutskiy 	}
6221e51764aSArtem Bityutskiy 
6231e51764aSArtem Bityutskiy 	if (wbuf->used)
6241e51764aSArtem Bityutskiy 		new_wbuf_timer_nolock(wbuf);
6251e51764aSArtem Bityutskiy 
6261e51764aSArtem Bityutskiy 	return 0;
6271e51764aSArtem Bityutskiy 
6281e51764aSArtem Bityutskiy out:
6291e51764aSArtem Bityutskiy 	ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
6301e51764aSArtem Bityutskiy 		  len, wbuf->lnum, wbuf->offs, err);
6311e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
6321e51764aSArtem Bityutskiy 	dbg_dump_stack();
6331e51764aSArtem Bityutskiy 	dbg_dump_leb(c, wbuf->lnum);
6341e51764aSArtem Bityutskiy 	return err;
6351e51764aSArtem Bityutskiy }
6361e51764aSArtem Bityutskiy 
6371e51764aSArtem Bityutskiy /**
6381e51764aSArtem Bityutskiy  * ubifs_write_node - write node to the media.
6391e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6401e51764aSArtem Bityutskiy  * @buf: the node to write
6411e51764aSArtem Bityutskiy  * @len: node length
6421e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
6431e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
6441e51764aSArtem Bityutskiy  * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
6451e51764aSArtem Bityutskiy  *
6461e51764aSArtem Bityutskiy  * This function automatically fills node magic number, assigns sequence
6471e51764aSArtem Bityutskiy  * number, and calculates node CRC checksum. The length of the @buf buffer has
6481e51764aSArtem Bityutskiy  * to be aligned to the minimal I/O unit size. This function automatically
6491e51764aSArtem Bityutskiy  * appends padding node and padding bytes if needed. Returns zero in case of
6501e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
6511e51764aSArtem Bityutskiy  */
6521e51764aSArtem Bityutskiy int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
6531e51764aSArtem Bityutskiy 		     int offs, int dtype)
6541e51764aSArtem Bityutskiy {
6551e51764aSArtem Bityutskiy 	int err, buf_len = ALIGN(len, c->min_io_size);
6561e51764aSArtem Bityutskiy 
6571e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
6581e51764aSArtem Bityutskiy 	       lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
6591e51764aSArtem Bityutskiy 	       buf_len);
6601e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
6611e51764aSArtem Bityutskiy 	ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
6621e51764aSArtem Bityutskiy 
6631e51764aSArtem Bityutskiy 	if (c->ro_media)
6641e51764aSArtem Bityutskiy 		return -EROFS;
6651e51764aSArtem Bityutskiy 
6661e51764aSArtem Bityutskiy 	ubifs_prepare_node(c, buf, len, 1);
6671e51764aSArtem Bityutskiy 	err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
6681e51764aSArtem Bityutskiy 	if (err) {
6691e51764aSArtem Bityutskiy 		ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
6701e51764aSArtem Bityutskiy 			  buf_len, lnum, offs, err);
6711e51764aSArtem Bityutskiy 		dbg_dump_node(c, buf);
6721e51764aSArtem Bityutskiy 		dbg_dump_stack();
6731e51764aSArtem Bityutskiy 	}
6741e51764aSArtem Bityutskiy 
6751e51764aSArtem Bityutskiy 	return err;
6761e51764aSArtem Bityutskiy }
6771e51764aSArtem Bityutskiy 
6781e51764aSArtem Bityutskiy /**
6791e51764aSArtem Bityutskiy  * ubifs_read_node_wbuf - read node from the media or write-buffer.
6801e51764aSArtem Bityutskiy  * @wbuf: wbuf to check for un-written data
6811e51764aSArtem Bityutskiy  * @buf: buffer to read to
6821e51764aSArtem Bityutskiy  * @type: node type
6831e51764aSArtem Bityutskiy  * @len: node length
6841e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
6851e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
6861e51764aSArtem Bityutskiy  *
6871e51764aSArtem Bityutskiy  * This function reads a node of known type and length, checks it and stores
6881e51764aSArtem Bityutskiy  * in @buf. If the node partially or fully sits in the write-buffer, this
6891e51764aSArtem Bityutskiy  * function takes data from the buffer, otherwise it reads the flash media.
6901e51764aSArtem Bityutskiy  * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
6911e51764aSArtem Bityutskiy  * error code in case of failure.
6921e51764aSArtem Bityutskiy  */
6931e51764aSArtem Bityutskiy int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
6941e51764aSArtem Bityutskiy 			 int lnum, int offs)
6951e51764aSArtem Bityutskiy {
6961e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
6971e51764aSArtem Bityutskiy 	int err, rlen, overlap;
6981e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
6991e51764aSArtem Bityutskiy 
70070aee2f1SArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d, jhead %d", lnum, offs,
70170aee2f1SArtem Bityutskiy 	       dbg_ntype(type), len, wbuf->jhead);
7021e51764aSArtem Bityutskiy 	ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
7031e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
7041e51764aSArtem Bityutskiy 	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
7051e51764aSArtem Bityutskiy 
7061e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
7071e51764aSArtem Bityutskiy 	overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
7081e51764aSArtem Bityutskiy 	if (!overlap) {
7091e51764aSArtem Bityutskiy 		/* We may safely unlock the write-buffer and read the data */
7101e51764aSArtem Bityutskiy 		spin_unlock(&wbuf->lock);
7111e51764aSArtem Bityutskiy 		return ubifs_read_node(c, buf, type, len, lnum, offs);
7121e51764aSArtem Bityutskiy 	}
7131e51764aSArtem Bityutskiy 
7141e51764aSArtem Bityutskiy 	/* Don't read under wbuf */
7151e51764aSArtem Bityutskiy 	rlen = wbuf->offs - offs;
7161e51764aSArtem Bityutskiy 	if (rlen < 0)
7171e51764aSArtem Bityutskiy 		rlen = 0;
7181e51764aSArtem Bityutskiy 
7191e51764aSArtem Bityutskiy 	/* Copy the rest from the write-buffer */
7201e51764aSArtem Bityutskiy 	memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
7211e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
7221e51764aSArtem Bityutskiy 
7231e51764aSArtem Bityutskiy 	if (rlen > 0) {
7241e51764aSArtem Bityutskiy 		/* Read everything that goes before write-buffer */
7251e51764aSArtem Bityutskiy 		err = ubi_read(c->ubi, lnum, buf, offs, rlen);
7261e51764aSArtem Bityutskiy 		if (err && err != -EBADMSG) {
7271e51764aSArtem Bityutskiy 			ubifs_err("failed to read node %d from LEB %d:%d, "
7281e51764aSArtem Bityutskiy 				  "error %d", type, lnum, offs, err);
7291e51764aSArtem Bityutskiy 			dbg_dump_stack();
7301e51764aSArtem Bityutskiy 			return err;
7311e51764aSArtem Bityutskiy 		}
7321e51764aSArtem Bityutskiy 	}
7331e51764aSArtem Bityutskiy 
7341e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
7351e51764aSArtem Bityutskiy 		ubifs_err("bad node type (%d but expected %d)",
7361e51764aSArtem Bityutskiy 			  ch->node_type, type);
7371e51764aSArtem Bityutskiy 		goto out;
7381e51764aSArtem Bityutskiy 	}
7391e51764aSArtem Bityutskiy 
7402953e73fSAdrian Hunter 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
7411e51764aSArtem Bityutskiy 	if (err) {
7421e51764aSArtem Bityutskiy 		ubifs_err("expected node type %d", type);
7431e51764aSArtem Bityutskiy 		return err;
7441e51764aSArtem Bityutskiy 	}
7451e51764aSArtem Bityutskiy 
7461e51764aSArtem Bityutskiy 	rlen = le32_to_cpu(ch->len);
7471e51764aSArtem Bityutskiy 	if (rlen != len) {
7481e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d, expected %d", rlen, len);
7491e51764aSArtem Bityutskiy 		goto out;
7501e51764aSArtem Bityutskiy 	}
7511e51764aSArtem Bityutskiy 
7521e51764aSArtem Bityutskiy 	return 0;
7531e51764aSArtem Bityutskiy 
7541e51764aSArtem Bityutskiy out:
7551e51764aSArtem Bityutskiy 	ubifs_err("bad node at LEB %d:%d", lnum, offs);
7561e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
7571e51764aSArtem Bityutskiy 	dbg_dump_stack();
7581e51764aSArtem Bityutskiy 	return -EINVAL;
7591e51764aSArtem Bityutskiy }
7601e51764aSArtem Bityutskiy 
7611e51764aSArtem Bityutskiy /**
7621e51764aSArtem Bityutskiy  * ubifs_read_node - read node.
7631e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7641e51764aSArtem Bityutskiy  * @buf: buffer to read to
7651e51764aSArtem Bityutskiy  * @type: node type
7661e51764aSArtem Bityutskiy  * @len: node length (not aligned)
7671e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
7681e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
7691e51764aSArtem Bityutskiy  *
7701e51764aSArtem Bityutskiy  * This function reads a node of known type and and length, checks it and
7711e51764aSArtem Bityutskiy  * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
7721e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
7731e51764aSArtem Bityutskiy  */
7741e51764aSArtem Bityutskiy int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
7751e51764aSArtem Bityutskiy 		    int lnum, int offs)
7761e51764aSArtem Bityutskiy {
7771e51764aSArtem Bityutskiy 	int err, l;
7781e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
7791e51764aSArtem Bityutskiy 
7801e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
7811e51764aSArtem Bityutskiy 	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
7821e51764aSArtem Bityutskiy 	ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
7831e51764aSArtem Bityutskiy 	ubifs_assert(!(offs & 7) && offs < c->leb_size);
7841e51764aSArtem Bityutskiy 	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
7851e51764aSArtem Bityutskiy 
7861e51764aSArtem Bityutskiy 	err = ubi_read(c->ubi, lnum, buf, offs, len);
7871e51764aSArtem Bityutskiy 	if (err && err != -EBADMSG) {
7881e51764aSArtem Bityutskiy 		ubifs_err("cannot read node %d from LEB %d:%d, error %d",
7891e51764aSArtem Bityutskiy 			  type, lnum, offs, err);
7901e51764aSArtem Bityutskiy 		return err;
7911e51764aSArtem Bityutskiy 	}
7921e51764aSArtem Bityutskiy 
7931e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
7941e51764aSArtem Bityutskiy 		ubifs_err("bad node type (%d but expected %d)",
7951e51764aSArtem Bityutskiy 			  ch->node_type, type);
7961e51764aSArtem Bityutskiy 		goto out;
7971e51764aSArtem Bityutskiy 	}
7981e51764aSArtem Bityutskiy 
7992953e73fSAdrian Hunter 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
8001e51764aSArtem Bityutskiy 	if (err) {
8011e51764aSArtem Bityutskiy 		ubifs_err("expected node type %d", type);
8021e51764aSArtem Bityutskiy 		return err;
8031e51764aSArtem Bityutskiy 	}
8041e51764aSArtem Bityutskiy 
8051e51764aSArtem Bityutskiy 	l = le32_to_cpu(ch->len);
8061e51764aSArtem Bityutskiy 	if (l != len) {
8071e51764aSArtem Bityutskiy 		ubifs_err("bad node length %d, expected %d", l, len);
8081e51764aSArtem Bityutskiy 		goto out;
8091e51764aSArtem Bityutskiy 	}
8101e51764aSArtem Bityutskiy 
8111e51764aSArtem Bityutskiy 	return 0;
8121e51764aSArtem Bityutskiy 
8131e51764aSArtem Bityutskiy out:
8141e51764aSArtem Bityutskiy 	ubifs_err("bad node at LEB %d:%d", lnum, offs);
8151e51764aSArtem Bityutskiy 	dbg_dump_node(c, buf);
8161e51764aSArtem Bityutskiy 	dbg_dump_stack();
8171e51764aSArtem Bityutskiy 	return -EINVAL;
8181e51764aSArtem Bityutskiy }
8191e51764aSArtem Bityutskiy 
8201e51764aSArtem Bityutskiy /**
8211e51764aSArtem Bityutskiy  * ubifs_wbuf_init - initialize write-buffer.
8221e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8231e51764aSArtem Bityutskiy  * @wbuf: write-buffer to initialize
8241e51764aSArtem Bityutskiy  *
825cb54ef8bSArtem Bityutskiy  * This function initializes write-buffer. Returns zero in case of success
8261e51764aSArtem Bityutskiy  * %-ENOMEM in case of failure.
8271e51764aSArtem Bityutskiy  */
8281e51764aSArtem Bityutskiy int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
8291e51764aSArtem Bityutskiy {
8301e51764aSArtem Bityutskiy 	size_t size;
8311e51764aSArtem Bityutskiy 
8321e51764aSArtem Bityutskiy 	wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
8331e51764aSArtem Bityutskiy 	if (!wbuf->buf)
8341e51764aSArtem Bityutskiy 		return -ENOMEM;
8351e51764aSArtem Bityutskiy 
8361e51764aSArtem Bityutskiy 	size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
8371e51764aSArtem Bityutskiy 	wbuf->inodes = kmalloc(size, GFP_KERNEL);
8381e51764aSArtem Bityutskiy 	if (!wbuf->inodes) {
8391e51764aSArtem Bityutskiy 		kfree(wbuf->buf);
8401e51764aSArtem Bityutskiy 		wbuf->buf = NULL;
8411e51764aSArtem Bityutskiy 		return -ENOMEM;
8421e51764aSArtem Bityutskiy 	}
8431e51764aSArtem Bityutskiy 
8441e51764aSArtem Bityutskiy 	wbuf->used = 0;
8451e51764aSArtem Bityutskiy 	wbuf->lnum = wbuf->offs = -1;
8461e51764aSArtem Bityutskiy 	wbuf->avail = c->min_io_size;
8471e51764aSArtem Bityutskiy 	wbuf->dtype = UBI_UNKNOWN;
8481e51764aSArtem Bityutskiy 	wbuf->sync_callback = NULL;
8491e51764aSArtem Bityutskiy 	mutex_init(&wbuf->io_mutex);
8501e51764aSArtem Bityutskiy 	spin_lock_init(&wbuf->lock);
8511e51764aSArtem Bityutskiy 	wbuf->c = c;
8521e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
8531e51764aSArtem Bityutskiy 
854f2c5dbd7SArtem Bityutskiy 	hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
855f2c5dbd7SArtem Bityutskiy 	wbuf->timer.function = wbuf_timer_callback_nolock;
8562a35a3a8SArtem Bityutskiy 	wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
8572a35a3a8SArtem Bityutskiy 	wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
8582a35a3a8SArtem Bityutskiy 	wbuf->delta *= 1000000000ULL;
8592a35a3a8SArtem Bityutskiy 	ubifs_assert(wbuf->delta <= ULONG_MAX);
8601e51764aSArtem Bityutskiy 	return 0;
8611e51764aSArtem Bityutskiy }
8621e51764aSArtem Bityutskiy 
8631e51764aSArtem Bityutskiy /**
8641e51764aSArtem Bityutskiy  * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
8651e51764aSArtem Bityutskiy  * @wbuf: the write-buffer where to add
8661e51764aSArtem Bityutskiy  * @inum: the inode number
8671e51764aSArtem Bityutskiy  *
8681e51764aSArtem Bityutskiy  * This function adds an inode number to the inode array of the write-buffer.
8691e51764aSArtem Bityutskiy  */
8701e51764aSArtem Bityutskiy void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
8711e51764aSArtem Bityutskiy {
8721e51764aSArtem Bityutskiy 	if (!wbuf->buf)
8731e51764aSArtem Bityutskiy 		/* NOR flash or something similar */
8741e51764aSArtem Bityutskiy 		return;
8751e51764aSArtem Bityutskiy 
8761e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
8771e51764aSArtem Bityutskiy 	if (wbuf->used)
8781e51764aSArtem Bityutskiy 		wbuf->inodes[wbuf->next_ino++] = inum;
8791e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
8801e51764aSArtem Bityutskiy }
8811e51764aSArtem Bityutskiy 
8821e51764aSArtem Bityutskiy /**
8831e51764aSArtem Bityutskiy  * wbuf_has_ino - returns if the wbuf contains data from the inode.
8841e51764aSArtem Bityutskiy  * @wbuf: the write-buffer
8851e51764aSArtem Bityutskiy  * @inum: the inode number
8861e51764aSArtem Bityutskiy  *
8871e51764aSArtem Bityutskiy  * This function returns with %1 if the write-buffer contains some data from the
8881e51764aSArtem Bityutskiy  * given inode otherwise it returns with %0.
8891e51764aSArtem Bityutskiy  */
8901e51764aSArtem Bityutskiy static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
8911e51764aSArtem Bityutskiy {
8921e51764aSArtem Bityutskiy 	int i, ret = 0;
8931e51764aSArtem Bityutskiy 
8941e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
8951e51764aSArtem Bityutskiy 	for (i = 0; i < wbuf->next_ino; i++)
8961e51764aSArtem Bityutskiy 		if (inum == wbuf->inodes[i]) {
8971e51764aSArtem Bityutskiy 			ret = 1;
8981e51764aSArtem Bityutskiy 			break;
8991e51764aSArtem Bityutskiy 		}
9001e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
9011e51764aSArtem Bityutskiy 
9021e51764aSArtem Bityutskiy 	return ret;
9031e51764aSArtem Bityutskiy }
9041e51764aSArtem Bityutskiy 
9051e51764aSArtem Bityutskiy /**
9061e51764aSArtem Bityutskiy  * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
9071e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9081e51764aSArtem Bityutskiy  * @inode: inode to synchronize
9091e51764aSArtem Bityutskiy  *
9101e51764aSArtem Bityutskiy  * This function synchronizes write-buffers which contain nodes belonging to
9111e51764aSArtem Bityutskiy  * @inode. Returns zero in case of success and a negative error code in case of
9121e51764aSArtem Bityutskiy  * failure.
9131e51764aSArtem Bityutskiy  */
9141e51764aSArtem Bityutskiy int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
9151e51764aSArtem Bityutskiy {
9161e51764aSArtem Bityutskiy 	int i, err = 0;
9171e51764aSArtem Bityutskiy 
9181e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
9191e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
9201e51764aSArtem Bityutskiy 
9211e51764aSArtem Bityutskiy 		if (i == GCHD)
9221e51764aSArtem Bityutskiy 			/*
9231e51764aSArtem Bityutskiy 			 * GC head is special, do not look at it. Even if the
9241e51764aSArtem Bityutskiy 			 * head contains something related to this inode, it is
9251e51764aSArtem Bityutskiy 			 * a _copy_ of corresponding on-flash node which sits
9261e51764aSArtem Bityutskiy 			 * somewhere else.
9271e51764aSArtem Bityutskiy 			 */
9281e51764aSArtem Bityutskiy 			continue;
9291e51764aSArtem Bityutskiy 
9301e51764aSArtem Bityutskiy 		if (!wbuf_has_ino(wbuf, inode->i_ino))
9311e51764aSArtem Bityutskiy 			continue;
9321e51764aSArtem Bityutskiy 
9331e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
9341e51764aSArtem Bityutskiy 		if (wbuf_has_ino(wbuf, inode->i_ino))
9351e51764aSArtem Bityutskiy 			err = ubifs_wbuf_sync_nolock(wbuf);
9361e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
9371e51764aSArtem Bityutskiy 
9381e51764aSArtem Bityutskiy 		if (err) {
9391e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
9401e51764aSArtem Bityutskiy 			return err;
9411e51764aSArtem Bityutskiy 		}
9421e51764aSArtem Bityutskiy 	}
9431e51764aSArtem Bityutskiy 	return 0;
9441e51764aSArtem Bityutskiy }
945