xref: /openbmc/linux/fs/ubifs/io.c (revision 42212523)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy  * Copyright (C) 2006, 2007 University of Szeged, Hungary
71e51764aSArtem Bityutskiy  *
81e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
91e51764aSArtem Bityutskiy  *          Adrian Hunter
101e51764aSArtem Bityutskiy  *          Zoltan Sogor
111e51764aSArtem Bityutskiy  */
121e51764aSArtem Bityutskiy 
131e51764aSArtem Bityutskiy /*
141e51764aSArtem Bityutskiy  * This file implements UBIFS I/O subsystem which provides various I/O-related
151e51764aSArtem Bityutskiy  * helper functions (reading/writing/checking/validating nodes) and implements
161e51764aSArtem Bityutskiy  * write-buffering support. Write buffers help to save space which otherwise
171e51764aSArtem Bityutskiy  * would have been wasted for padding to the nearest minimal I/O unit boundary.
181e51764aSArtem Bityutskiy  * Instead, data first goes to the write-buffer and is flushed when the
191e51764aSArtem Bityutskiy  * buffer is full or when it is not used for some time (by timer). This is
201e51764aSArtem Bityutskiy  * similar to the mechanism is used by JFFS2.
211e51764aSArtem Bityutskiy  *
226c7f74f7SArtem Bityutskiy  * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
236c7f74f7SArtem Bityutskiy  * write size (@c->max_write_size). The latter is the maximum amount of bytes
246c7f74f7SArtem Bityutskiy  * the underlying flash is able to program at a time, and writing in
256c7f74f7SArtem Bityutskiy  * @c->max_write_size units should presumably be faster. Obviously,
266c7f74f7SArtem Bityutskiy  * @c->min_io_size <= @c->max_write_size. Write-buffers are of
276c7f74f7SArtem Bityutskiy  * @c->max_write_size bytes in size for maximum performance. However, when a
286c7f74f7SArtem Bityutskiy  * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
296c7f74f7SArtem Bityutskiy  * boundary) which contains data is written, not the whole write-buffer,
306c7f74f7SArtem Bityutskiy  * because this is more space-efficient.
316c7f74f7SArtem Bityutskiy  *
326c7f74f7SArtem Bityutskiy  * This optimization adds few complications to the code. Indeed, on the one
336c7f74f7SArtem Bityutskiy  * hand, we want to write in optimal @c->max_write_size bytes chunks, which
346c7f74f7SArtem Bityutskiy  * also means aligning writes at the @c->max_write_size bytes offsets. On the
356c7f74f7SArtem Bityutskiy  * other hand, we do not want to waste space when synchronizing the write
366c7f74f7SArtem Bityutskiy  * buffer, so during synchronization we writes in smaller chunks. And this makes
376c7f74f7SArtem Bityutskiy  * the next write offset to be not aligned to @c->max_write_size bytes. So the
386c7f74f7SArtem Bityutskiy  * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
396c7f74f7SArtem Bityutskiy  * to @c->max_write_size bytes again. We do this by temporarily shrinking
406c7f74f7SArtem Bityutskiy  * write-buffer size (@wbuf->size).
416c7f74f7SArtem Bityutskiy  *
421e51764aSArtem Bityutskiy  * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
431e51764aSArtem Bityutskiy  * mutexes defined inside these objects. Since sometimes upper-level code
441e51764aSArtem Bityutskiy  * has to lock the write-buffer (e.g. journal space reservation code), many
451e51764aSArtem Bityutskiy  * functions related to write-buffers have "nolock" suffix which means that the
461e51764aSArtem Bityutskiy  * caller has to lock the write-buffer before calling this function.
471e51764aSArtem Bityutskiy  *
481e51764aSArtem Bityutskiy  * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
491e51764aSArtem Bityutskiy  * aligned, UBIFS starts the next node from the aligned address, and the padded
501e51764aSArtem Bityutskiy  * bytes may contain any rubbish. In other words, UBIFS does not put padding
511e51764aSArtem Bityutskiy  * bytes in those small gaps. Common headers of nodes store real node lengths,
521e51764aSArtem Bityutskiy  * not aligned lengths. Indexing nodes also store real lengths in branches.
531e51764aSArtem Bityutskiy  *
541e51764aSArtem Bityutskiy  * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
551e51764aSArtem Bityutskiy  * uses padding nodes or padding bytes, if the padding node does not fit.
561e51764aSArtem Bityutskiy  *
576c7f74f7SArtem Bityutskiy  * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
586c7f74f7SArtem Bityutskiy  * they are read from the flash media.
591e51764aSArtem Bityutskiy  */
601e51764aSArtem Bityutskiy 
611e51764aSArtem Bityutskiy #include <linux/crc32.h>
625a0e3ad6STejun Heo #include <linux/slab.h>
631e51764aSArtem Bityutskiy #include "ubifs.h"
641e51764aSArtem Bityutskiy 
651e51764aSArtem Bityutskiy /**
66ff46d7b3SAdrian Hunter  * ubifs_ro_mode - switch UBIFS to read read-only mode.
67ff46d7b3SAdrian Hunter  * @c: UBIFS file-system description object
68ff46d7b3SAdrian Hunter  * @err: error code which is the reason of switching to R/O mode
69ff46d7b3SAdrian Hunter  */
ubifs_ro_mode(struct ubifs_info * c,int err)70ff46d7b3SAdrian Hunter void ubifs_ro_mode(struct ubifs_info *c, int err)
71ff46d7b3SAdrian Hunter {
722680d722SArtem Bityutskiy 	if (!c->ro_error) {
732680d722SArtem Bityutskiy 		c->ro_error = 1;
74ccb3eba7SArtem Bityutskiy 		c->no_chk_data_crc = 0;
751751e8a6SLinus Torvalds 		c->vfs_sb->s_flags |= SB_RDONLY;
76235c362bSSheng Yong 		ubifs_warn(c, "switched to read-only mode, error %d", err);
77d033c98bSArtem Bityutskiy 		dump_stack();
78ff46d7b3SAdrian Hunter 	}
79ff46d7b3SAdrian Hunter }
80ff46d7b3SAdrian Hunter 
8183cef708SArtem Bityutskiy /*
8283cef708SArtem Bityutskiy  * Below are simple wrappers over UBI I/O functions which include some
8383cef708SArtem Bityutskiy  * additional checks and UBIFS debugging stuff. See corresponding UBI function
8483cef708SArtem Bityutskiy  * for more information.
8583cef708SArtem Bityutskiy  */
8683cef708SArtem Bityutskiy 
ubifs_leb_read(const struct ubifs_info * c,int lnum,void * buf,int offs,int len,int even_ebadmsg)8783cef708SArtem Bityutskiy int ubifs_leb_read(const struct ubifs_info *c, int lnum, void *buf, int offs,
8883cef708SArtem Bityutskiy 		   int len, int even_ebadmsg)
8983cef708SArtem Bityutskiy {
9083cef708SArtem Bityutskiy 	int err;
9183cef708SArtem Bityutskiy 
9283cef708SArtem Bityutskiy 	err = ubi_read(c->ubi, lnum, buf, offs, len);
9383cef708SArtem Bityutskiy 	/*
9483cef708SArtem Bityutskiy 	 * In case of %-EBADMSG print the error message only if the
9583cef708SArtem Bityutskiy 	 * @even_ebadmsg is true.
9683cef708SArtem Bityutskiy 	 */
9783cef708SArtem Bityutskiy 	if (err && (err != -EBADMSG || even_ebadmsg)) {
98235c362bSSheng Yong 		ubifs_err(c, "reading %d bytes from LEB %d:%d failed, error %d",
9983cef708SArtem Bityutskiy 			  len, lnum, offs, err);
1007c46d0aeSArtem Bityutskiy 		dump_stack();
10183cef708SArtem Bityutskiy 	}
10283cef708SArtem Bityutskiy 	return err;
10383cef708SArtem Bityutskiy }
10483cef708SArtem Bityutskiy 
ubifs_leb_write(struct ubifs_info * c,int lnum,const void * buf,int offs,int len)10583cef708SArtem Bityutskiy int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
106b36a261eSRichard Weinberger 		    int len)
10783cef708SArtem Bityutskiy {
10883cef708SArtem Bityutskiy 	int err;
10983cef708SArtem Bityutskiy 
1106eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
11183cef708SArtem Bityutskiy 	if (c->ro_error)
11283cef708SArtem Bityutskiy 		return -EROFS;
11383cef708SArtem Bityutskiy 	if (!dbg_is_tst_rcvry(c))
114b36a261eSRichard Weinberger 		err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
11583cef708SArtem Bityutskiy 	else
116b36a261eSRichard Weinberger 		err = dbg_leb_write(c, lnum, buf, offs, len);
11783cef708SArtem Bityutskiy 	if (err) {
118235c362bSSheng Yong 		ubifs_err(c, "writing %d bytes to LEB %d:%d failed, error %d",
11983cef708SArtem Bityutskiy 			  len, lnum, offs, err);
12083cef708SArtem Bityutskiy 		ubifs_ro_mode(c, err);
1217c46d0aeSArtem Bityutskiy 		dump_stack();
12283cef708SArtem Bityutskiy 	}
12383cef708SArtem Bityutskiy 	return err;
12483cef708SArtem Bityutskiy }
12583cef708SArtem Bityutskiy 
ubifs_leb_change(struct ubifs_info * c,int lnum,const void * buf,int len)126b36a261eSRichard Weinberger int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len)
12783cef708SArtem Bityutskiy {
12883cef708SArtem Bityutskiy 	int err;
12983cef708SArtem Bityutskiy 
1306eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
13183cef708SArtem Bityutskiy 	if (c->ro_error)
13283cef708SArtem Bityutskiy 		return -EROFS;
13383cef708SArtem Bityutskiy 	if (!dbg_is_tst_rcvry(c))
134b36a261eSRichard Weinberger 		err = ubi_leb_change(c->ubi, lnum, buf, len);
13583cef708SArtem Bityutskiy 	else
136b36a261eSRichard Weinberger 		err = dbg_leb_change(c, lnum, buf, len);
13783cef708SArtem Bityutskiy 	if (err) {
138235c362bSSheng Yong 		ubifs_err(c, "changing %d bytes in LEB %d failed, error %d",
13983cef708SArtem Bityutskiy 			  len, lnum, err);
14083cef708SArtem Bityutskiy 		ubifs_ro_mode(c, err);
1417c46d0aeSArtem Bityutskiy 		dump_stack();
14283cef708SArtem Bityutskiy 	}
14383cef708SArtem Bityutskiy 	return err;
14483cef708SArtem Bityutskiy }
14583cef708SArtem Bityutskiy 
ubifs_leb_unmap(struct ubifs_info * c,int lnum)14683cef708SArtem Bityutskiy int ubifs_leb_unmap(struct ubifs_info *c, int lnum)
14783cef708SArtem Bityutskiy {
14883cef708SArtem Bityutskiy 	int err;
14983cef708SArtem Bityutskiy 
1506eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
15183cef708SArtem Bityutskiy 	if (c->ro_error)
15283cef708SArtem Bityutskiy 		return -EROFS;
15383cef708SArtem Bityutskiy 	if (!dbg_is_tst_rcvry(c))
15483cef708SArtem Bityutskiy 		err = ubi_leb_unmap(c->ubi, lnum);
15583cef708SArtem Bityutskiy 	else
156f57cb188SArtem Bityutskiy 		err = dbg_leb_unmap(c, lnum);
15783cef708SArtem Bityutskiy 	if (err) {
158235c362bSSheng Yong 		ubifs_err(c, "unmap LEB %d failed, error %d", lnum, err);
15983cef708SArtem Bityutskiy 		ubifs_ro_mode(c, err);
1607c46d0aeSArtem Bityutskiy 		dump_stack();
16183cef708SArtem Bityutskiy 	}
16283cef708SArtem Bityutskiy 	return err;
16383cef708SArtem Bityutskiy }
16483cef708SArtem Bityutskiy 
ubifs_leb_map(struct ubifs_info * c,int lnum)165b36a261eSRichard Weinberger int ubifs_leb_map(struct ubifs_info *c, int lnum)
16683cef708SArtem Bityutskiy {
16783cef708SArtem Bityutskiy 	int err;
16883cef708SArtem Bityutskiy 
1696eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
17083cef708SArtem Bityutskiy 	if (c->ro_error)
17183cef708SArtem Bityutskiy 		return -EROFS;
17283cef708SArtem Bityutskiy 	if (!dbg_is_tst_rcvry(c))
173b36a261eSRichard Weinberger 		err = ubi_leb_map(c->ubi, lnum);
17483cef708SArtem Bityutskiy 	else
175b36a261eSRichard Weinberger 		err = dbg_leb_map(c, lnum);
17683cef708SArtem Bityutskiy 	if (err) {
177235c362bSSheng Yong 		ubifs_err(c, "mapping LEB %d failed, error %d", lnum, err);
17883cef708SArtem Bityutskiy 		ubifs_ro_mode(c, err);
1797c46d0aeSArtem Bityutskiy 		dump_stack();
18083cef708SArtem Bityutskiy 	}
18183cef708SArtem Bityutskiy 	return err;
18283cef708SArtem Bityutskiy }
18383cef708SArtem Bityutskiy 
ubifs_is_mapped(const struct ubifs_info * c,int lnum)18483cef708SArtem Bityutskiy int ubifs_is_mapped(const struct ubifs_info *c, int lnum)
18583cef708SArtem Bityutskiy {
18683cef708SArtem Bityutskiy 	int err;
18783cef708SArtem Bityutskiy 
18883cef708SArtem Bityutskiy 	err = ubi_is_mapped(c->ubi, lnum);
18983cef708SArtem Bityutskiy 	if (err < 0) {
190235c362bSSheng Yong 		ubifs_err(c, "ubi_is_mapped failed for LEB %d, error %d",
19183cef708SArtem Bityutskiy 			  lnum, err);
1927c46d0aeSArtem Bityutskiy 		dump_stack();
19383cef708SArtem Bityutskiy 	}
19483cef708SArtem Bityutskiy 	return err;
19583cef708SArtem Bityutskiy }
19683cef708SArtem Bityutskiy 
record_magic_error(struct ubifs_stats_info * stats)1972e3cbf42SStefan Schaeckeler static void record_magic_error(struct ubifs_stats_info *stats)
1982e3cbf42SStefan Schaeckeler {
1992e3cbf42SStefan Schaeckeler 	if (stats)
2002e3cbf42SStefan Schaeckeler 		stats->magic_errors++;
2012e3cbf42SStefan Schaeckeler }
2022e3cbf42SStefan Schaeckeler 
record_node_error(struct ubifs_stats_info * stats)2032e3cbf42SStefan Schaeckeler static void record_node_error(struct ubifs_stats_info *stats)
2042e3cbf42SStefan Schaeckeler {
2052e3cbf42SStefan Schaeckeler 	if (stats)
2062e3cbf42SStefan Schaeckeler 		stats->node_errors++;
2072e3cbf42SStefan Schaeckeler }
2082e3cbf42SStefan Schaeckeler 
record_crc_error(struct ubifs_stats_info * stats)2092e3cbf42SStefan Schaeckeler static void record_crc_error(struct ubifs_stats_info *stats)
2102e3cbf42SStefan Schaeckeler {
2112e3cbf42SStefan Schaeckeler 	if (stats)
2122e3cbf42SStefan Schaeckeler 		stats->crc_errors++;
2132e3cbf42SStefan Schaeckeler }
2142e3cbf42SStefan Schaeckeler 
215ff46d7b3SAdrian Hunter /**
2161e51764aSArtem Bityutskiy  * ubifs_check_node - check node.
2171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2181e51764aSArtem Bityutskiy  * @buf: node to check
219a33e30a0SZhihao Cheng  * @len: node length
2201e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
2211e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
2221e51764aSArtem Bityutskiy  * @quiet: print no messages
2236f7ab6d4SArtem Bityutskiy  * @must_chk_crc: indicates whether to always check the CRC
2241e51764aSArtem Bityutskiy  *
2251e51764aSArtem Bityutskiy  * This function checks node magic number and CRC checksum. This function also
2261e51764aSArtem Bityutskiy  * validates node length to prevent UBIFS from becoming crazy when an attacker
2271e51764aSArtem Bityutskiy  * feeds it a file-system image with incorrect nodes. For example, too large
2281e51764aSArtem Bityutskiy  * node length in the common header could cause UBIFS to read memory outside of
2291e51764aSArtem Bityutskiy  * allocated buffer when checking the CRC checksum.
2301e51764aSArtem Bityutskiy  *
2316f7ab6d4SArtem Bityutskiy  * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
2326f7ab6d4SArtem Bityutskiy  * true, which is controlled by corresponding UBIFS mount option. However, if
2336f7ab6d4SArtem Bityutskiy  * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
23418d1d7fbSArtem Bityutskiy  * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
23518d1d7fbSArtem Bityutskiy  * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
23618d1d7fbSArtem Bityutskiy  * is checked. This is because during mounting or re-mounting from R/O mode to
23718d1d7fbSArtem Bityutskiy  * R/W mode we may read journal nodes (when replying the journal or doing the
23818d1d7fbSArtem Bityutskiy  * recovery) and the journal nodes may potentially be corrupted, so checking is
23918d1d7fbSArtem Bityutskiy  * required.
2406f7ab6d4SArtem Bityutskiy  *
2416f7ab6d4SArtem Bityutskiy  * This function returns zero in case of success and %-EUCLEAN in case of bad
2426f7ab6d4SArtem Bityutskiy  * CRC or magic.
2431e51764aSArtem Bityutskiy  */
ubifs_check_node(const struct ubifs_info * c,const void * buf,int len,int lnum,int offs,int quiet,int must_chk_crc)244a33e30a0SZhihao Cheng int ubifs_check_node(const struct ubifs_info *c, const void *buf, int len,
245a33e30a0SZhihao Cheng 		     int lnum, int offs, int quiet, int must_chk_crc)
2461e51764aSArtem Bityutskiy {
247c8be0975SZhihao Cheng 	int err = -EINVAL, type, node_len;
2481e51764aSArtem Bityutskiy 	uint32_t crc, node_crc, magic;
2491e51764aSArtem Bityutskiy 	const struct ubifs_ch *ch = buf;
2501e51764aSArtem Bityutskiy 
2516eb61d58SRichard Weinberger 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
2526eb61d58SRichard Weinberger 	ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
2531e51764aSArtem Bityutskiy 
2541e51764aSArtem Bityutskiy 	magic = le32_to_cpu(ch->magic);
2551e51764aSArtem Bityutskiy 	if (magic != UBIFS_NODE_MAGIC) {
2561e51764aSArtem Bityutskiy 		if (!quiet)
257235c362bSSheng Yong 			ubifs_err(c, "bad magic %#08x, expected %#08x",
2581e51764aSArtem Bityutskiy 				  magic, UBIFS_NODE_MAGIC);
2592e3cbf42SStefan Schaeckeler 		record_magic_error(c->stats);
2601e51764aSArtem Bityutskiy 		err = -EUCLEAN;
2611e51764aSArtem Bityutskiy 		goto out;
2621e51764aSArtem Bityutskiy 	}
2631e51764aSArtem Bityutskiy 
2641e51764aSArtem Bityutskiy 	type = ch->node_type;
2651e51764aSArtem Bityutskiy 	if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
2661e51764aSArtem Bityutskiy 		if (!quiet)
267235c362bSSheng Yong 			ubifs_err(c, "bad node type %d", type);
2682e3cbf42SStefan Schaeckeler 		record_node_error(c->stats);
2691e51764aSArtem Bityutskiy 		goto out;
2701e51764aSArtem Bityutskiy 	}
2711e51764aSArtem Bityutskiy 
2721e51764aSArtem Bityutskiy 	node_len = le32_to_cpu(ch->len);
2731e51764aSArtem Bityutskiy 	if (node_len + offs > c->leb_size)
2741e51764aSArtem Bityutskiy 		goto out_len;
2751e51764aSArtem Bityutskiy 
2761e51764aSArtem Bityutskiy 	if (c->ranges[type].max_len == 0) {
2771e51764aSArtem Bityutskiy 		if (node_len != c->ranges[type].len)
2781e51764aSArtem Bityutskiy 			goto out_len;
2791e51764aSArtem Bityutskiy 	} else if (node_len < c->ranges[type].min_len ||
2801e51764aSArtem Bityutskiy 		   node_len > c->ranges[type].max_len)
2811e51764aSArtem Bityutskiy 		goto out_len;
2821e51764aSArtem Bityutskiy 
28318d1d7fbSArtem Bityutskiy 	if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
28418d1d7fbSArtem Bityutskiy 	    !c->remounting_rw && c->no_chk_data_crc)
2852953e73fSAdrian Hunter 		return 0;
2862953e73fSAdrian Hunter 
2871e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
2881e51764aSArtem Bityutskiy 	node_crc = le32_to_cpu(ch->crc);
2891e51764aSArtem Bityutskiy 	if (crc != node_crc) {
2901e51764aSArtem Bityutskiy 		if (!quiet)
291235c362bSSheng Yong 			ubifs_err(c, "bad CRC: calculated %#08x, read %#08x",
2921e51764aSArtem Bityutskiy 				  crc, node_crc);
2932e3cbf42SStefan Schaeckeler 		record_crc_error(c->stats);
2941e51764aSArtem Bityutskiy 		err = -EUCLEAN;
2951e51764aSArtem Bityutskiy 		goto out;
2961e51764aSArtem Bityutskiy 	}
2971e51764aSArtem Bityutskiy 
2981e51764aSArtem Bityutskiy 	return 0;
2991e51764aSArtem Bityutskiy 
3001e51764aSArtem Bityutskiy out_len:
3011e51764aSArtem Bityutskiy 	if (!quiet)
302235c362bSSheng Yong 		ubifs_err(c, "bad node length %d", node_len);
3031e51764aSArtem Bityutskiy out:
3041e51764aSArtem Bityutskiy 	if (!quiet) {
305235c362bSSheng Yong 		ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
306a33e30a0SZhihao Cheng 		ubifs_dump_node(c, buf, len);
3077c46d0aeSArtem Bityutskiy 		dump_stack();
3081e51764aSArtem Bityutskiy 	}
3091e51764aSArtem Bityutskiy 	return err;
3101e51764aSArtem Bityutskiy }
3111e51764aSArtem Bityutskiy 
3121e51764aSArtem Bityutskiy /**
3131e51764aSArtem Bityutskiy  * ubifs_pad - pad flash space.
3141e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3151e51764aSArtem Bityutskiy  * @buf: buffer to put padding to
3161e51764aSArtem Bityutskiy  * @pad: how many bytes to pad
3171e51764aSArtem Bityutskiy  *
3181e51764aSArtem Bityutskiy  * The flash media obliges us to write only in chunks of %c->min_io_size and
3191e51764aSArtem Bityutskiy  * when we have to write less data we add padding node to the write-buffer and
3201e51764aSArtem Bityutskiy  * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
3211e51764aSArtem Bityutskiy  * media is being scanned. If the amount of wasted space is not enough to fit a
3221e51764aSArtem Bityutskiy  * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
3231e51764aSArtem Bityutskiy  * pattern (%UBIFS_PADDING_BYTE).
3241e51764aSArtem Bityutskiy  *
3251e51764aSArtem Bityutskiy  * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
3261e51764aSArtem Bityutskiy  * used.
3271e51764aSArtem Bityutskiy  */
ubifs_pad(const struct ubifs_info * c,void * buf,int pad)3281e51764aSArtem Bityutskiy void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
3291e51764aSArtem Bityutskiy {
3301e51764aSArtem Bityutskiy 	uint32_t crc;
3311e51764aSArtem Bityutskiy 
33220f14311SRichard Weinberger 	ubifs_assert(c, pad >= 0);
3331e51764aSArtem Bityutskiy 
3341e51764aSArtem Bityutskiy 	if (pad >= UBIFS_PAD_NODE_SZ) {
3351e51764aSArtem Bityutskiy 		struct ubifs_ch *ch = buf;
3361e51764aSArtem Bityutskiy 		struct ubifs_pad_node *pad_node = buf;
3371e51764aSArtem Bityutskiy 
3381e51764aSArtem Bityutskiy 		ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
3391e51764aSArtem Bityutskiy 		ch->node_type = UBIFS_PAD_NODE;
3401e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_NO_NODE_GROUP;
3411e51764aSArtem Bityutskiy 		ch->padding[0] = ch->padding[1] = 0;
3421e51764aSArtem Bityutskiy 		ch->sqnum = 0;
3431e51764aSArtem Bityutskiy 		ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
3441e51764aSArtem Bityutskiy 		pad -= UBIFS_PAD_NODE_SZ;
3451e51764aSArtem Bityutskiy 		pad_node->pad_len = cpu_to_le32(pad);
3461e51764aSArtem Bityutskiy 		crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
3471e51764aSArtem Bityutskiy 		ch->crc = cpu_to_le32(crc);
3481e51764aSArtem Bityutskiy 		memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
3491e51764aSArtem Bityutskiy 	} else if (pad > 0)
3501e51764aSArtem Bityutskiy 		/* Too little space, padding node won't fit */
3511e51764aSArtem Bityutskiy 		memset(buf, UBIFS_PADDING_BYTE, pad);
3521e51764aSArtem Bityutskiy }
3531e51764aSArtem Bityutskiy 
3541e51764aSArtem Bityutskiy /**
3551e51764aSArtem Bityutskiy  * next_sqnum - get next sequence number.
3561e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3571e51764aSArtem Bityutskiy  */
next_sqnum(struct ubifs_info * c)3581e51764aSArtem Bityutskiy static unsigned long long next_sqnum(struct ubifs_info *c)
3591e51764aSArtem Bityutskiy {
3601e51764aSArtem Bityutskiy 	unsigned long long sqnum;
3611e51764aSArtem Bityutskiy 
3621e51764aSArtem Bityutskiy 	spin_lock(&c->cnt_lock);
3631e51764aSArtem Bityutskiy 	sqnum = ++c->max_sqnum;
3641e51764aSArtem Bityutskiy 	spin_unlock(&c->cnt_lock);
3651e51764aSArtem Bityutskiy 
3661e51764aSArtem Bityutskiy 	if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
3671e51764aSArtem Bityutskiy 		if (sqnum >= SQNUM_WATERMARK) {
368235c362bSSheng Yong 			ubifs_err(c, "sequence number overflow %llu, end of life",
3691e51764aSArtem Bityutskiy 				  sqnum);
3701e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, -EINVAL);
3711e51764aSArtem Bityutskiy 		}
372235c362bSSheng Yong 		ubifs_warn(c, "running out of sequence numbers, end of life soon");
3731e51764aSArtem Bityutskiy 	}
3741e51764aSArtem Bityutskiy 
3751e51764aSArtem Bityutskiy 	return sqnum;
3761e51764aSArtem Bityutskiy }
3771e51764aSArtem Bityutskiy 
ubifs_init_node(struct ubifs_info * c,void * node,int len,int pad)378dead9726SSascha Hauer void ubifs_init_node(struct ubifs_info *c, void *node, int len, int pad)
379dead9726SSascha Hauer {
380dead9726SSascha Hauer 	struct ubifs_ch *ch = node;
381dead9726SSascha Hauer 	unsigned long long sqnum = next_sqnum(c);
382dead9726SSascha Hauer 
383dead9726SSascha Hauer 	ubifs_assert(c, len >= UBIFS_CH_SZ);
384dead9726SSascha Hauer 
385dead9726SSascha Hauer 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
386dead9726SSascha Hauer 	ch->len = cpu_to_le32(len);
387dead9726SSascha Hauer 	ch->group_type = UBIFS_NO_NODE_GROUP;
388dead9726SSascha Hauer 	ch->sqnum = cpu_to_le64(sqnum);
389dead9726SSascha Hauer 	ch->padding[0] = ch->padding[1] = 0;
390dead9726SSascha Hauer 
391dead9726SSascha Hauer 	if (pad) {
392dead9726SSascha Hauer 		len = ALIGN(len, 8);
393dead9726SSascha Hauer 		pad = ALIGN(len, c->min_io_size) - len;
394dead9726SSascha Hauer 		ubifs_pad(c, node + len, pad);
395dead9726SSascha Hauer 	}
396dead9726SSascha Hauer }
397dead9726SSascha Hauer 
ubifs_crc_node(struct ubifs_info * c,void * node,int len)398dead9726SSascha Hauer void ubifs_crc_node(struct ubifs_info *c, void *node, int len)
399dead9726SSascha Hauer {
400dead9726SSascha Hauer 	struct ubifs_ch *ch = node;
401dead9726SSascha Hauer 	uint32_t crc;
402dead9726SSascha Hauer 
403dead9726SSascha Hauer 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
404dead9726SSascha Hauer 	ch->crc = cpu_to_le32(crc);
405dead9726SSascha Hauer }
406dead9726SSascha Hauer 
4071e51764aSArtem Bityutskiy /**
408a384b47eSSascha Hauer  * ubifs_prepare_node_hmac - prepare node to be written to flash.
409a384b47eSSascha Hauer  * @c: UBIFS file-system description object
410a384b47eSSascha Hauer  * @node: the node to pad
411a384b47eSSascha Hauer  * @len: node length
412a384b47eSSascha Hauer  * @hmac_offs: offset of the HMAC in the node
413a384b47eSSascha Hauer  * @pad: if the buffer has to be padded
414a384b47eSSascha Hauer  *
415a384b47eSSascha Hauer  * This function prepares node at @node to be written to the media - it
416a384b47eSSascha Hauer  * calculates node CRC, fills the common header, and adds proper padding up to
417a384b47eSSascha Hauer  * the next minimum I/O unit if @pad is not zero. if @hmac_offs is positive then
418a384b47eSSascha Hauer  * a HMAC is inserted into the node at the given offset.
419a384b47eSSascha Hauer  *
420a384b47eSSascha Hauer  * This function returns 0 for success or a negative error code otherwise.
421a384b47eSSascha Hauer  */
ubifs_prepare_node_hmac(struct ubifs_info * c,void * node,int len,int hmac_offs,int pad)422a384b47eSSascha Hauer int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
423a384b47eSSascha Hauer 			    int hmac_offs, int pad)
424a384b47eSSascha Hauer {
425a384b47eSSascha Hauer 	int err;
426a384b47eSSascha Hauer 
427a384b47eSSascha Hauer 	ubifs_init_node(c, node, len, pad);
428a384b47eSSascha Hauer 
429a384b47eSSascha Hauer 	if (hmac_offs > 0) {
430a384b47eSSascha Hauer 		err = ubifs_node_insert_hmac(c, node, len, hmac_offs);
431a384b47eSSascha Hauer 		if (err)
432a384b47eSSascha Hauer 			return err;
433a384b47eSSascha Hauer 	}
434a384b47eSSascha Hauer 
435a384b47eSSascha Hauer 	ubifs_crc_node(c, node, len);
436a384b47eSSascha Hauer 
437a384b47eSSascha Hauer 	return 0;
438a384b47eSSascha Hauer }
439a384b47eSSascha Hauer 
440a384b47eSSascha Hauer /**
4411e51764aSArtem Bityutskiy  * ubifs_prepare_node - prepare node to be written to flash.
4421e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4431e51764aSArtem Bityutskiy  * @node: the node to pad
4441e51764aSArtem Bityutskiy  * @len: node length
4451e51764aSArtem Bityutskiy  * @pad: if the buffer has to be padded
4461e51764aSArtem Bityutskiy  *
4471e51764aSArtem Bityutskiy  * This function prepares node at @node to be written to the media - it
4481e51764aSArtem Bityutskiy  * calculates node CRC, fills the common header, and adds proper padding up to
4491e51764aSArtem Bityutskiy  * the next minimum I/O unit if @pad is not zero.
4501e51764aSArtem Bityutskiy  */
ubifs_prepare_node(struct ubifs_info * c,void * node,int len,int pad)4511e51764aSArtem Bityutskiy void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
4521e51764aSArtem Bityutskiy {
453a384b47eSSascha Hauer 	/*
454a384b47eSSascha Hauer 	 * Deliberately ignore return value since this function can only fail
455a384b47eSSascha Hauer 	 * when a hmac offset is given.
456a384b47eSSascha Hauer 	 */
457a384b47eSSascha Hauer 	ubifs_prepare_node_hmac(c, node, len, 0, pad);
4581e51764aSArtem Bityutskiy }
4591e51764aSArtem Bityutskiy 
4601e51764aSArtem Bityutskiy /**
4611e51764aSArtem Bityutskiy  * ubifs_prep_grp_node - prepare node of a group to be written to flash.
4621e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4631e51764aSArtem Bityutskiy  * @node: the node to pad
4641e51764aSArtem Bityutskiy  * @len: node length
4651e51764aSArtem Bityutskiy  * @last: indicates the last node of the group
4661e51764aSArtem Bityutskiy  *
4671e51764aSArtem Bityutskiy  * This function prepares node at @node to be written to the media - it
4681e51764aSArtem Bityutskiy  * calculates node CRC and fills the common header.
4691e51764aSArtem Bityutskiy  */
ubifs_prep_grp_node(struct ubifs_info * c,void * node,int len,int last)4701e51764aSArtem Bityutskiy void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
4711e51764aSArtem Bityutskiy {
4721e51764aSArtem Bityutskiy 	uint32_t crc;
4731e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = node;
4741e51764aSArtem Bityutskiy 	unsigned long long sqnum = next_sqnum(c);
4751e51764aSArtem Bityutskiy 
4766eb61d58SRichard Weinberger 	ubifs_assert(c, len >= UBIFS_CH_SZ);
4771e51764aSArtem Bityutskiy 
4781e51764aSArtem Bityutskiy 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
4791e51764aSArtem Bityutskiy 	ch->len = cpu_to_le32(len);
4801e51764aSArtem Bityutskiy 	if (last)
4811e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
4821e51764aSArtem Bityutskiy 	else
4831e51764aSArtem Bityutskiy 		ch->group_type = UBIFS_IN_NODE_GROUP;
4841e51764aSArtem Bityutskiy 	ch->sqnum = cpu_to_le64(sqnum);
4851e51764aSArtem Bityutskiy 	ch->padding[0] = ch->padding[1] = 0;
4861e51764aSArtem Bityutskiy 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
4871e51764aSArtem Bityutskiy 	ch->crc = cpu_to_le32(crc);
4881e51764aSArtem Bityutskiy }
4891e51764aSArtem Bityutskiy 
4901e51764aSArtem Bityutskiy /**
491*42212523SYang Li  * wbuf_timer_callback_nolock - write-buffer timer callback function.
49239274a1eSFabian Frederick  * @timer: timer data (write-buffer descriptor)
4931e51764aSArtem Bityutskiy  *
4941e51764aSArtem Bityutskiy  * This function is called when the write-buffer timer expires.
4951e51764aSArtem Bityutskiy  */
wbuf_timer_callback_nolock(struct hrtimer * timer)496f2c5dbd7SArtem Bityutskiy static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
4971e51764aSArtem Bityutskiy {
498f2c5dbd7SArtem Bityutskiy 	struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
4991e51764aSArtem Bityutskiy 
50077a7ae58SArtem Bityutskiy 	dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
5011e51764aSArtem Bityutskiy 	wbuf->need_sync = 1;
5021e51764aSArtem Bityutskiy 	wbuf->c->need_wbuf_sync = 1;
5031e51764aSArtem Bityutskiy 	ubifs_wake_up_bgt(wbuf->c);
504f2c5dbd7SArtem Bityutskiy 	return HRTIMER_NORESTART;
5051e51764aSArtem Bityutskiy }
5061e51764aSArtem Bityutskiy 
5071e51764aSArtem Bityutskiy /**
508*42212523SYang Li  * new_wbuf_timer_nolock - start new write-buffer timer.
5096eb61d58SRichard Weinberger  * @c: UBIFS file-system description object
5101e51764aSArtem Bityutskiy  * @wbuf: write-buffer descriptor
5111e51764aSArtem Bityutskiy  */
new_wbuf_timer_nolock(struct ubifs_info * c,struct ubifs_wbuf * wbuf)5126eb61d58SRichard Weinberger static void new_wbuf_timer_nolock(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
5131e51764aSArtem Bityutskiy {
5141b7fc2c0SRafał Miłecki 	ktime_t softlimit = ms_to_ktime(dirty_writeback_interval * 10);
5151b7fc2c0SRafał Miłecki 	unsigned long long delta = dirty_writeback_interval;
516854826c9SRafał Miłecki 
5171b7fc2c0SRafał Miłecki 	/* centi to milli, milli to nano, then 10% */
5181b7fc2c0SRafał Miłecki 	delta *= 10ULL * NSEC_PER_MSEC / 10ULL;
519854826c9SRafał Miłecki 
5206eb61d58SRichard Weinberger 	ubifs_assert(c, !hrtimer_active(&wbuf->timer));
5216eb61d58SRichard Weinberger 	ubifs_assert(c, delta <= ULONG_MAX);
5221e51764aSArtem Bityutskiy 
5230b335b9dSArtem Bityutskiy 	if (wbuf->no_timer)
5241e51764aSArtem Bityutskiy 		return;
52577a7ae58SArtem Bityutskiy 	dbg_io("set timer for jhead %s, %llu-%llu millisecs",
52677a7ae58SArtem Bityutskiy 	       dbg_jhead(wbuf->jhead),
527854826c9SRafał Miłecki 	       div_u64(ktime_to_ns(softlimit), USEC_PER_SEC),
528854826c9SRafał Miłecki 	       div_u64(ktime_to_ns(softlimit) + delta, USEC_PER_SEC));
529854826c9SRafał Miłecki 	hrtimer_start_range_ns(&wbuf->timer, softlimit, delta,
530f2c5dbd7SArtem Bityutskiy 			       HRTIMER_MODE_REL);
5311e51764aSArtem Bityutskiy }
5321e51764aSArtem Bityutskiy 
5331e51764aSArtem Bityutskiy /**
534*42212523SYang Li  * cancel_wbuf_timer_nolock - cancel write-buffer timer.
5351e51764aSArtem Bityutskiy  * @wbuf: write-buffer descriptor
5361e51764aSArtem Bityutskiy  */
cancel_wbuf_timer_nolock(struct ubifs_wbuf * wbuf)5371e51764aSArtem Bityutskiy static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
5381e51764aSArtem Bityutskiy {
5390b335b9dSArtem Bityutskiy 	if (wbuf->no_timer)
5400b335b9dSArtem Bityutskiy 		return;
5411e51764aSArtem Bityutskiy 	wbuf->need_sync = 0;
542f2c5dbd7SArtem Bityutskiy 	hrtimer_cancel(&wbuf->timer);
5431e51764aSArtem Bityutskiy }
5441e51764aSArtem Bityutskiy 
5451e51764aSArtem Bityutskiy /**
5461e51764aSArtem Bityutskiy  * ubifs_wbuf_sync_nolock - synchronize write-buffer.
5471e51764aSArtem Bityutskiy  * @wbuf: write-buffer to synchronize
5481e51764aSArtem Bityutskiy  *
5491e51764aSArtem Bityutskiy  * This function synchronizes write-buffer @buf and returns zero in case of
5501e51764aSArtem Bityutskiy  * success or a negative error code in case of failure.
5516c7f74f7SArtem Bityutskiy  *
5526c7f74f7SArtem Bityutskiy  * Note, although write-buffers are of @c->max_write_size, this function does
5536c7f74f7SArtem Bityutskiy  * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
5546c7f74f7SArtem Bityutskiy  * if the write-buffer is only partially filled with data, only the used part
5556c7f74f7SArtem Bityutskiy  * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
5566c7f74f7SArtem Bityutskiy  * This way we waste less space.
5571e51764aSArtem Bityutskiy  */
ubifs_wbuf_sync_nolock(struct ubifs_wbuf * wbuf)5581e51764aSArtem Bityutskiy int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
5591e51764aSArtem Bityutskiy {
5601e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
5616c7f74f7SArtem Bityutskiy 	int err, dirt, sync_len;
5621e51764aSArtem Bityutskiy 
5631e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
5641e51764aSArtem Bityutskiy 	if (!wbuf->used || wbuf->lnum == -1)
5651e51764aSArtem Bityutskiy 		/* Write-buffer is empty or not seeked */
5661e51764aSArtem Bityutskiy 		return 0;
5671e51764aSArtem Bityutskiy 
56877a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, %d bytes, jhead %s",
56977a7ae58SArtem Bityutskiy 	       wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
5706eb61d58SRichard Weinberger 	ubifs_assert(c, !(wbuf->avail & 7));
5716eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->offs + wbuf->size <= c->leb_size);
5726eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->size >= c->min_io_size);
5736eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->size <= c->max_write_size);
5746eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->size % c->min_io_size == 0);
5756eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
5766c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs >= c->max_write_size)
5776eb61d58SRichard Weinberger 		ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size));
5781e51764aSArtem Bityutskiy 
5792680d722SArtem Bityutskiy 	if (c->ro_error)
5801e51764aSArtem Bityutskiy 		return -EROFS;
5811e51764aSArtem Bityutskiy 
5826c7f74f7SArtem Bityutskiy 	/*
5836c7f74f7SArtem Bityutskiy 	 * Do not write whole write buffer but write only the minimum necessary
5846c7f74f7SArtem Bityutskiy 	 * amount of min. I/O units.
5856c7f74f7SArtem Bityutskiy 	 */
5866c7f74f7SArtem Bityutskiy 	sync_len = ALIGN(wbuf->used, c->min_io_size);
5876c7f74f7SArtem Bityutskiy 	dirt = sync_len - wbuf->used;
5886c7f74f7SArtem Bityutskiy 	if (dirt)
5896c7f74f7SArtem Bityutskiy 		ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
590b36a261eSRichard Weinberger 	err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len);
591987226a5SArtem Bityutskiy 	if (err)
5921e51764aSArtem Bityutskiy 		return err;
5931e51764aSArtem Bityutskiy 
5941e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
5956c7f74f7SArtem Bityutskiy 	wbuf->offs += sync_len;
5966c7f74f7SArtem Bityutskiy 	/*
5976c7f74f7SArtem Bityutskiy 	 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
5986c7f74f7SArtem Bityutskiy 	 * But our goal is to optimize writes and make sure we write in
5996c7f74f7SArtem Bityutskiy 	 * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
6006c7f74f7SArtem Bityutskiy 	 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
6016c7f74f7SArtem Bityutskiy 	 * sure that @wbuf->offs + @wbuf->size is aligned to
6026c7f74f7SArtem Bityutskiy 	 * @c->max_write_size. This way we make sure that after next
6036c7f74f7SArtem Bityutskiy 	 * write-buffer flush we are again at the optimal offset (aligned to
6046c7f74f7SArtem Bityutskiy 	 * @c->max_write_size).
6056c7f74f7SArtem Bityutskiy 	 */
6066c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs < c->max_write_size)
6076c7f74f7SArtem Bityutskiy 		wbuf->size = c->leb_size - wbuf->offs;
6086c7f74f7SArtem Bityutskiy 	else if (wbuf->offs & (c->max_write_size - 1))
6096c7f74f7SArtem Bityutskiy 		wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
6106c7f74f7SArtem Bityutskiy 	else
6116c7f74f7SArtem Bityutskiy 		wbuf->size = c->max_write_size;
6126c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size;
6131e51764aSArtem Bityutskiy 	wbuf->used = 0;
6141e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
6151e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
6161e51764aSArtem Bityutskiy 
6171e51764aSArtem Bityutskiy 	if (wbuf->sync_callback)
6181e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum,
6191e51764aSArtem Bityutskiy 					  c->leb_size - wbuf->offs, dirt);
6201e51764aSArtem Bityutskiy 	return err;
6211e51764aSArtem Bityutskiy }
6221e51764aSArtem Bityutskiy 
6231e51764aSArtem Bityutskiy /**
6241e51764aSArtem Bityutskiy  * ubifs_wbuf_seek_nolock - seek write-buffer.
6251e51764aSArtem Bityutskiy  * @wbuf: write-buffer
6261e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number to seek to
6271e51764aSArtem Bityutskiy  * @offs: logical eraseblock offset to seek to
6281e51764aSArtem Bityutskiy  *
629cb54ef8bSArtem Bityutskiy  * This function targets the write-buffer to logical eraseblock @lnum:@offs.
630cb14a184SArtem Bityutskiy  * The write-buffer has to be empty. Returns zero in case of success and a
631cb14a184SArtem Bityutskiy  * negative error code in case of failure.
6321e51764aSArtem Bityutskiy  */
ubifs_wbuf_seek_nolock(struct ubifs_wbuf * wbuf,int lnum,int offs)633b36a261eSRichard Weinberger int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs)
6341e51764aSArtem Bityutskiy {
6351e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
6361e51764aSArtem Bityutskiy 
63777a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
6386eb61d58SRichard Weinberger 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt);
6396eb61d58SRichard Weinberger 	ubifs_assert(c, offs >= 0 && offs <= c->leb_size);
6406eb61d58SRichard Weinberger 	ubifs_assert(c, offs % c->min_io_size == 0 && !(offs & 7));
6416eb61d58SRichard Weinberger 	ubifs_assert(c, lnum != wbuf->lnum);
6426eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->used == 0);
6431e51764aSArtem Bityutskiy 
6441e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
6451e51764aSArtem Bityutskiy 	wbuf->lnum = lnum;
6461e51764aSArtem Bityutskiy 	wbuf->offs = offs;
6476c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs < c->max_write_size)
6486c7f74f7SArtem Bityutskiy 		wbuf->size = c->leb_size - wbuf->offs;
6496c7f74f7SArtem Bityutskiy 	else if (wbuf->offs & (c->max_write_size - 1))
6506c7f74f7SArtem Bityutskiy 		wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
6516c7f74f7SArtem Bityutskiy 	else
6526c7f74f7SArtem Bityutskiy 		wbuf->size = c->max_write_size;
6536c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size;
6541e51764aSArtem Bityutskiy 	wbuf->used = 0;
6551e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
6561e51764aSArtem Bityutskiy 
6571e51764aSArtem Bityutskiy 	return 0;
6581e51764aSArtem Bityutskiy }
6591e51764aSArtem Bityutskiy 
6601e51764aSArtem Bityutskiy /**
6611e51764aSArtem Bityutskiy  * ubifs_bg_wbufs_sync - synchronize write-buffers.
6621e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6631e51764aSArtem Bityutskiy  *
6641e51764aSArtem Bityutskiy  * This function is called by background thread to synchronize write-buffers.
6651e51764aSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
6661e51764aSArtem Bityutskiy  * failure.
6671e51764aSArtem Bityutskiy  */
ubifs_bg_wbufs_sync(struct ubifs_info * c)6681e51764aSArtem Bityutskiy int ubifs_bg_wbufs_sync(struct ubifs_info *c)
6691e51764aSArtem Bityutskiy {
6701e51764aSArtem Bityutskiy 	int err, i;
6711e51764aSArtem Bityutskiy 
6726eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
6731e51764aSArtem Bityutskiy 	if (!c->need_wbuf_sync)
6741e51764aSArtem Bityutskiy 		return 0;
6751e51764aSArtem Bityutskiy 	c->need_wbuf_sync = 0;
6761e51764aSArtem Bityutskiy 
6772680d722SArtem Bityutskiy 	if (c->ro_error) {
6781e51764aSArtem Bityutskiy 		err = -EROFS;
6791e51764aSArtem Bityutskiy 		goto out_timers;
6801e51764aSArtem Bityutskiy 	}
6811e51764aSArtem Bityutskiy 
6821e51764aSArtem Bityutskiy 	dbg_io("synchronize");
6831e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
6841e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
6851e51764aSArtem Bityutskiy 
6861e51764aSArtem Bityutskiy 		cond_resched();
6871e51764aSArtem Bityutskiy 
6881e51764aSArtem Bityutskiy 		/*
6891e51764aSArtem Bityutskiy 		 * If the mutex is locked then wbuf is being changed, so
6901e51764aSArtem Bityutskiy 		 * synchronization is not necessary.
6911e51764aSArtem Bityutskiy 		 */
6921e51764aSArtem Bityutskiy 		if (mutex_is_locked(&wbuf->io_mutex))
6931e51764aSArtem Bityutskiy 			continue;
6941e51764aSArtem Bityutskiy 
6951e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
6961e51764aSArtem Bityutskiy 		if (!wbuf->need_sync) {
6971e51764aSArtem Bityutskiy 			mutex_unlock(&wbuf->io_mutex);
6981e51764aSArtem Bityutskiy 			continue;
6991e51764aSArtem Bityutskiy 		}
7001e51764aSArtem Bityutskiy 
7011e51764aSArtem Bityutskiy 		err = ubifs_wbuf_sync_nolock(wbuf);
7021e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
7031e51764aSArtem Bityutskiy 		if (err) {
704235c362bSSheng Yong 			ubifs_err(c, "cannot sync write-buffer, error %d", err);
7051e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
7061e51764aSArtem Bityutskiy 			goto out_timers;
7071e51764aSArtem Bityutskiy 		}
7081e51764aSArtem Bityutskiy 	}
7091e51764aSArtem Bityutskiy 
7101e51764aSArtem Bityutskiy 	return 0;
7111e51764aSArtem Bityutskiy 
7121e51764aSArtem Bityutskiy out_timers:
7131e51764aSArtem Bityutskiy 	/* Cancel all timers to prevent repeated errors */
7141e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
7151e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
7161e51764aSArtem Bityutskiy 
7171e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
7181e51764aSArtem Bityutskiy 		cancel_wbuf_timer_nolock(wbuf);
7191e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
7201e51764aSArtem Bityutskiy 	}
7211e51764aSArtem Bityutskiy 	return err;
7221e51764aSArtem Bityutskiy }
7231e51764aSArtem Bityutskiy 
7241e51764aSArtem Bityutskiy /**
7251e51764aSArtem Bityutskiy  * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
7261e51764aSArtem Bityutskiy  * @wbuf: write-buffer
7271e51764aSArtem Bityutskiy  * @buf: node to write
7281e51764aSArtem Bityutskiy  * @len: node length
7291e51764aSArtem Bityutskiy  *
7301e51764aSArtem Bityutskiy  * This function writes data to flash via write-buffer @wbuf. This means that
7311e51764aSArtem Bityutskiy  * the last piece of the node won't reach the flash media immediately if it
7326c7f74f7SArtem Bityutskiy  * does not take whole max. write unit (@c->max_write_size). Instead, the node
7336c7f74f7SArtem Bityutskiy  * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
7346c7f74f7SArtem Bityutskiy  * because more data are appended to the write-buffer).
7351e51764aSArtem Bityutskiy  *
7361e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
7371e51764aSArtem Bityutskiy  * case of failure. If the node cannot be written because there is no more
7381e51764aSArtem Bityutskiy  * space in this logical eraseblock, %-ENOSPC is returned.
7391e51764aSArtem Bityutskiy  */
ubifs_wbuf_write_nolock(struct ubifs_wbuf * wbuf,void * buf,int len)7401e51764aSArtem Bityutskiy int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
7411e51764aSArtem Bityutskiy {
7421e51764aSArtem Bityutskiy 	struct ubifs_info *c = wbuf->c;
743a33e30a0SZhihao Cheng 	int err, n, written = 0, aligned_len = ALIGN(len, 8);
7441e51764aSArtem Bityutskiy 
74577a7ae58SArtem Bityutskiy 	dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
74677a7ae58SArtem Bityutskiy 	       dbg_ntype(((struct ubifs_ch *)buf)->node_type),
74777a7ae58SArtem Bityutskiy 	       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
7486eb61d58SRichard Weinberger 	ubifs_assert(c, len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
7496eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
7506eb61d58SRichard Weinberger 	ubifs_assert(c, !(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
7516eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->avail > 0 && wbuf->avail <= wbuf->size);
7526eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->size >= c->min_io_size);
7536eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->size <= c->max_write_size);
7546eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf->size % c->min_io_size == 0);
7556eb61d58SRichard Weinberger 	ubifs_assert(c, mutex_is_locked(&wbuf->io_mutex));
7566eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
7576eb61d58SRichard Weinberger 	ubifs_assert(c, !c->space_fixup);
7586c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs >= c->max_write_size)
7596eb61d58SRichard Weinberger 		ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size));
7601e51764aSArtem Bityutskiy 
7611e51764aSArtem Bityutskiy 	if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
7621e51764aSArtem Bityutskiy 		err = -ENOSPC;
7631e51764aSArtem Bityutskiy 		goto out;
7641e51764aSArtem Bityutskiy 	}
7651e51764aSArtem Bityutskiy 
7661e51764aSArtem Bityutskiy 	cancel_wbuf_timer_nolock(wbuf);
7671e51764aSArtem Bityutskiy 
7682680d722SArtem Bityutskiy 	if (c->ro_error)
7691e51764aSArtem Bityutskiy 		return -EROFS;
7701e51764aSArtem Bityutskiy 
7711e51764aSArtem Bityutskiy 	if (aligned_len <= wbuf->avail) {
7721e51764aSArtem Bityutskiy 		/*
7731e51764aSArtem Bityutskiy 		 * The node is not very large and fits entirely within
7741e51764aSArtem Bityutskiy 		 * write-buffer.
7751e51764aSArtem Bityutskiy 		 */
7761e51764aSArtem Bityutskiy 		memcpy(wbuf->buf + wbuf->used, buf, len);
77720f14311SRichard Weinberger 		if (aligned_len > len) {
77820f14311SRichard Weinberger 			ubifs_assert(c, aligned_len - len < 8);
77920f14311SRichard Weinberger 			ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len);
78020f14311SRichard Weinberger 		}
7811e51764aSArtem Bityutskiy 
7821e51764aSArtem Bityutskiy 		if (aligned_len == wbuf->avail) {
78377a7ae58SArtem Bityutskiy 			dbg_io("flush jhead %s wbuf to LEB %d:%d",
78477a7ae58SArtem Bityutskiy 			       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
785987226a5SArtem Bityutskiy 			err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf,
786b36a261eSRichard Weinberger 					      wbuf->offs, wbuf->size);
7871e51764aSArtem Bityutskiy 			if (err)
7881e51764aSArtem Bityutskiy 				goto out;
7891e51764aSArtem Bityutskiy 
7901e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
7916c7f74f7SArtem Bityutskiy 			wbuf->offs += wbuf->size;
7926c7f74f7SArtem Bityutskiy 			if (c->leb_size - wbuf->offs >= c->max_write_size)
7936c7f74f7SArtem Bityutskiy 				wbuf->size = c->max_write_size;
7946c7f74f7SArtem Bityutskiy 			else
7956c7f74f7SArtem Bityutskiy 				wbuf->size = c->leb_size - wbuf->offs;
7966c7f74f7SArtem Bityutskiy 			wbuf->avail = wbuf->size;
7971e51764aSArtem Bityutskiy 			wbuf->used = 0;
7981e51764aSArtem Bityutskiy 			wbuf->next_ino = 0;
7991e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
8001e51764aSArtem Bityutskiy 		} else {
8011e51764aSArtem Bityutskiy 			spin_lock(&wbuf->lock);
8021e51764aSArtem Bityutskiy 			wbuf->avail -= aligned_len;
8031e51764aSArtem Bityutskiy 			wbuf->used += aligned_len;
8041e51764aSArtem Bityutskiy 			spin_unlock(&wbuf->lock);
8051e51764aSArtem Bityutskiy 		}
8061e51764aSArtem Bityutskiy 
8071e51764aSArtem Bityutskiy 		goto exit;
8081e51764aSArtem Bityutskiy 	}
8091e51764aSArtem Bityutskiy 
8106c7f74f7SArtem Bityutskiy 	if (wbuf->used) {
8111e51764aSArtem Bityutskiy 		/*
8126c7f74f7SArtem Bityutskiy 		 * The node is large enough and does not fit entirely within
8136c7f74f7SArtem Bityutskiy 		 * current available space. We have to fill and flush
8146c7f74f7SArtem Bityutskiy 		 * write-buffer and switch to the next max. write unit.
8151e51764aSArtem Bityutskiy 		 */
81677a7ae58SArtem Bityutskiy 		dbg_io("flush jhead %s wbuf to LEB %d:%d",
81777a7ae58SArtem Bityutskiy 		       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
8181e51764aSArtem Bityutskiy 		memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
819987226a5SArtem Bityutskiy 		err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs,
820b36a261eSRichard Weinberger 				      wbuf->size);
8211e51764aSArtem Bityutskiy 		if (err)
8221e51764aSArtem Bityutskiy 			goto out;
8231e51764aSArtem Bityutskiy 
82412f33891SArtem Bityutskiy 		wbuf->offs += wbuf->size;
8251e51764aSArtem Bityutskiy 		len -= wbuf->avail;
8261e51764aSArtem Bityutskiy 		aligned_len -= wbuf->avail;
8276c7f74f7SArtem Bityutskiy 		written += wbuf->avail;
8286c7f74f7SArtem Bityutskiy 	} else if (wbuf->offs & (c->max_write_size - 1)) {
8296c7f74f7SArtem Bityutskiy 		/*
8306c7f74f7SArtem Bityutskiy 		 * The write-buffer offset is not aligned to
8316c7f74f7SArtem Bityutskiy 		 * @c->max_write_size and @wbuf->size is less than
8326c7f74f7SArtem Bityutskiy 		 * @c->max_write_size. Write @wbuf->size bytes to make sure the
8336c7f74f7SArtem Bityutskiy 		 * following writes are done in optimal @c->max_write_size
8346c7f74f7SArtem Bityutskiy 		 * chunks.
8356c7f74f7SArtem Bityutskiy 		 */
8366c7f74f7SArtem Bityutskiy 		dbg_io("write %d bytes to LEB %d:%d",
8376c7f74f7SArtem Bityutskiy 		       wbuf->size, wbuf->lnum, wbuf->offs);
838987226a5SArtem Bityutskiy 		err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs,
839b36a261eSRichard Weinberger 				      wbuf->size);
8406c7f74f7SArtem Bityutskiy 		if (err)
8416c7f74f7SArtem Bityutskiy 			goto out;
8426c7f74f7SArtem Bityutskiy 
84312f33891SArtem Bityutskiy 		wbuf->offs += wbuf->size;
8446c7f74f7SArtem Bityutskiy 		len -= wbuf->size;
8456c7f74f7SArtem Bityutskiy 		aligned_len -= wbuf->size;
8466c7f74f7SArtem Bityutskiy 		written += wbuf->size;
8476c7f74f7SArtem Bityutskiy 	}
8481e51764aSArtem Bityutskiy 
8491e51764aSArtem Bityutskiy 	/*
8506c7f74f7SArtem Bityutskiy 	 * The remaining data may take more whole max. write units, so write the
8516c7f74f7SArtem Bityutskiy 	 * remains multiple to max. write unit size directly to the flash media.
8521e51764aSArtem Bityutskiy 	 * We align node length to 8-byte boundary because we anyway flash wbuf
8531e51764aSArtem Bityutskiy 	 * if the remaining space is less than 8 bytes.
8541e51764aSArtem Bityutskiy 	 */
8556c7f74f7SArtem Bityutskiy 	n = aligned_len >> c->max_write_shift;
8561e51764aSArtem Bityutskiy 	if (n) {
8574f2262a3SZhihao Cheng 		int m = n - 1;
8584f2262a3SZhihao Cheng 
85912f33891SArtem Bityutskiy 		dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
86012f33891SArtem Bityutskiy 		       wbuf->offs);
8614f2262a3SZhihao Cheng 
8624f2262a3SZhihao Cheng 		if (m) {
8634f2262a3SZhihao Cheng 			/* '(n-1)<<c->max_write_shift < len' is always true. */
8644f2262a3SZhihao Cheng 			m <<= c->max_write_shift;
865987226a5SArtem Bityutskiy 			err = ubifs_leb_write(c, wbuf->lnum, buf + written,
8664f2262a3SZhihao Cheng 					      wbuf->offs, m);
8674f2262a3SZhihao Cheng 			if (err)
8684f2262a3SZhihao Cheng 				goto out;
8694f2262a3SZhihao Cheng 			wbuf->offs += m;
8704f2262a3SZhihao Cheng 			aligned_len -= m;
8714f2262a3SZhihao Cheng 			len -= m;
8724f2262a3SZhihao Cheng 			written += m;
8734f2262a3SZhihao Cheng 		}
8744f2262a3SZhihao Cheng 
8754f2262a3SZhihao Cheng 		/*
8764f2262a3SZhihao Cheng 		 * The non-written len of buf may be less than 'n' because
8774f2262a3SZhihao Cheng 		 * parameter 'len' is not 8 bytes aligned, so here we read
8784f2262a3SZhihao Cheng 		 * min(len, n) bytes from buf.
8794f2262a3SZhihao Cheng 		 */
8804f2262a3SZhihao Cheng 		n = 1 << c->max_write_shift;
8814f2262a3SZhihao Cheng 		memcpy(wbuf->buf, buf + written, min(len, n));
8824f2262a3SZhihao Cheng 		if (n > len) {
8834f2262a3SZhihao Cheng 			ubifs_assert(c, n - len < 8);
8844f2262a3SZhihao Cheng 			ubifs_pad(c, wbuf->buf + len, n - len);
8854f2262a3SZhihao Cheng 		}
8864f2262a3SZhihao Cheng 
8874f2262a3SZhihao Cheng 		err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n);
8881e51764aSArtem Bityutskiy 		if (err)
8891e51764aSArtem Bityutskiy 			goto out;
89012f33891SArtem Bityutskiy 		wbuf->offs += n;
8911e51764aSArtem Bityutskiy 		aligned_len -= n;
8924f2262a3SZhihao Cheng 		len -= min(len, n);
8931e51764aSArtem Bityutskiy 		written += n;
8941e51764aSArtem Bityutskiy 	}
8951e51764aSArtem Bityutskiy 
8961e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
89720f14311SRichard Weinberger 	if (aligned_len) {
8981e51764aSArtem Bityutskiy 		/*
8991e51764aSArtem Bityutskiy 		 * And now we have what's left and what does not take whole
9006c7f74f7SArtem Bityutskiy 		 * max. write unit, so write it to the write-buffer and we are
9011e51764aSArtem Bityutskiy 		 * done.
9021e51764aSArtem Bityutskiy 		 */
9031e51764aSArtem Bityutskiy 		memcpy(wbuf->buf, buf + written, len);
90420f14311SRichard Weinberger 		if (aligned_len > len) {
90520f14311SRichard Weinberger 			ubifs_assert(c, aligned_len - len < 8);
90620f14311SRichard Weinberger 			ubifs_pad(c, wbuf->buf + len, aligned_len - len);
90720f14311SRichard Weinberger 		}
90820f14311SRichard Weinberger 	}
9091e51764aSArtem Bityutskiy 
9106c7f74f7SArtem Bityutskiy 	if (c->leb_size - wbuf->offs >= c->max_write_size)
9116c7f74f7SArtem Bityutskiy 		wbuf->size = c->max_write_size;
9126c7f74f7SArtem Bityutskiy 	else
9136c7f74f7SArtem Bityutskiy 		wbuf->size = c->leb_size - wbuf->offs;
9146c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size - aligned_len;
9151e51764aSArtem Bityutskiy 	wbuf->used = aligned_len;
9161e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
9171e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
9181e51764aSArtem Bityutskiy 
9191e51764aSArtem Bityutskiy exit:
9201e51764aSArtem Bityutskiy 	if (wbuf->sync_callback) {
9211e51764aSArtem Bityutskiy 		int free = c->leb_size - wbuf->offs - wbuf->used;
9221e51764aSArtem Bityutskiy 
9231e51764aSArtem Bityutskiy 		err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
9241e51764aSArtem Bityutskiy 		if (err)
9251e51764aSArtem Bityutskiy 			goto out;
9261e51764aSArtem Bityutskiy 	}
9271e51764aSArtem Bityutskiy 
9281e51764aSArtem Bityutskiy 	if (wbuf->used)
9296eb61d58SRichard Weinberger 		new_wbuf_timer_nolock(c, wbuf);
9301e51764aSArtem Bityutskiy 
9311e51764aSArtem Bityutskiy 	return 0;
9321e51764aSArtem Bityutskiy 
9331e51764aSArtem Bityutskiy out:
934235c362bSSheng Yong 	ubifs_err(c, "cannot write %d bytes to LEB %d:%d, error %d",
9351e51764aSArtem Bityutskiy 		  len, wbuf->lnum, wbuf->offs, err);
936a33e30a0SZhihao Cheng 	ubifs_dump_node(c, buf, written + len);
9377c46d0aeSArtem Bityutskiy 	dump_stack();
938edf6be24SArtem Bityutskiy 	ubifs_dump_leb(c, wbuf->lnum);
9391e51764aSArtem Bityutskiy 	return err;
9401e51764aSArtem Bityutskiy }
9411e51764aSArtem Bityutskiy 
9421e51764aSArtem Bityutskiy /**
943a384b47eSSascha Hauer  * ubifs_write_node_hmac - write node to the media.
944a384b47eSSascha Hauer  * @c: UBIFS file-system description object
945a384b47eSSascha Hauer  * @buf: the node to write
946a384b47eSSascha Hauer  * @len: node length
947a384b47eSSascha Hauer  * @lnum: logical eraseblock number
948a384b47eSSascha Hauer  * @offs: offset within the logical eraseblock
949a384b47eSSascha Hauer  * @hmac_offs: offset of the HMAC within the node
950a384b47eSSascha Hauer  *
951a384b47eSSascha Hauer  * This function automatically fills node magic number, assigns sequence
952a384b47eSSascha Hauer  * number, and calculates node CRC checksum. The length of the @buf buffer has
953a384b47eSSascha Hauer  * to be aligned to the minimal I/O unit size. This function automatically
954a384b47eSSascha Hauer  * appends padding node and padding bytes if needed. Returns zero in case of
955a384b47eSSascha Hauer  * success and a negative error code in case of failure.
956a384b47eSSascha Hauer  */
ubifs_write_node_hmac(struct ubifs_info * c,void * buf,int len,int lnum,int offs,int hmac_offs)957a384b47eSSascha Hauer int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
958a384b47eSSascha Hauer 			  int offs, int hmac_offs)
959a384b47eSSascha Hauer {
960a384b47eSSascha Hauer 	int err, buf_len = ALIGN(len, c->min_io_size);
961a384b47eSSascha Hauer 
962a384b47eSSascha Hauer 	dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
963a384b47eSSascha Hauer 	       lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
964a384b47eSSascha Hauer 	       buf_len);
965a384b47eSSascha Hauer 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
966a384b47eSSascha Hauer 	ubifs_assert(c, offs % c->min_io_size == 0 && offs < c->leb_size);
967a384b47eSSascha Hauer 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
968a384b47eSSascha Hauer 	ubifs_assert(c, !c->space_fixup);
969a384b47eSSascha Hauer 
970a384b47eSSascha Hauer 	if (c->ro_error)
971a384b47eSSascha Hauer 		return -EROFS;
972a384b47eSSascha Hauer 
973a384b47eSSascha Hauer 	err = ubifs_prepare_node_hmac(c, buf, len, hmac_offs, 1);
974a384b47eSSascha Hauer 	if (err)
975a384b47eSSascha Hauer 		return err;
976a384b47eSSascha Hauer 
977a384b47eSSascha Hauer 	err = ubifs_leb_write(c, lnum, buf, offs, buf_len);
978a384b47eSSascha Hauer 	if (err)
979a33e30a0SZhihao Cheng 		ubifs_dump_node(c, buf, len);
980a384b47eSSascha Hauer 
981a384b47eSSascha Hauer 	return err;
982a384b47eSSascha Hauer }
983a384b47eSSascha Hauer 
984a384b47eSSascha Hauer /**
9851e51764aSArtem Bityutskiy  * ubifs_write_node - write node to the media.
9861e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9871e51764aSArtem Bityutskiy  * @buf: the node to write
9881e51764aSArtem Bityutskiy  * @len: node length
9891e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
9901e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
9911e51764aSArtem Bityutskiy  *
9921e51764aSArtem Bityutskiy  * This function automatically fills node magic number, assigns sequence
9931e51764aSArtem Bityutskiy  * number, and calculates node CRC checksum. The length of the @buf buffer has
9941e51764aSArtem Bityutskiy  * to be aligned to the minimal I/O unit size. This function automatically
9951e51764aSArtem Bityutskiy  * appends padding node and padding bytes if needed. Returns zero in case of
9961e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
9971e51764aSArtem Bityutskiy  */
ubifs_write_node(struct ubifs_info * c,void * buf,int len,int lnum,int offs)9981e51764aSArtem Bityutskiy int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
999b36a261eSRichard Weinberger 		     int offs)
10001e51764aSArtem Bityutskiy {
1001a384b47eSSascha Hauer 	return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1);
10021e51764aSArtem Bityutskiy }
10031e51764aSArtem Bityutskiy 
10041e51764aSArtem Bityutskiy /**
10051e51764aSArtem Bityutskiy  * ubifs_read_node_wbuf - read node from the media or write-buffer.
10061e51764aSArtem Bityutskiy  * @wbuf: wbuf to check for un-written data
10071e51764aSArtem Bityutskiy  * @buf: buffer to read to
10081e51764aSArtem Bityutskiy  * @type: node type
10091e51764aSArtem Bityutskiy  * @len: node length
10101e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
10111e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
10121e51764aSArtem Bityutskiy  *
10131e51764aSArtem Bityutskiy  * This function reads a node of known type and length, checks it and stores
10141e51764aSArtem Bityutskiy  * in @buf. If the node partially or fully sits in the write-buffer, this
10151e51764aSArtem Bityutskiy  * function takes data from the buffer, otherwise it reads the flash media.
10161e51764aSArtem Bityutskiy  * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
10171e51764aSArtem Bityutskiy  * error code in case of failure.
10181e51764aSArtem Bityutskiy  */
ubifs_read_node_wbuf(struct ubifs_wbuf * wbuf,void * buf,int type,int len,int lnum,int offs)10191e51764aSArtem Bityutskiy int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
10201e51764aSArtem Bityutskiy 			 int lnum, int offs)
10211e51764aSArtem Bityutskiy {
10221e51764aSArtem Bityutskiy 	const struct ubifs_info *c = wbuf->c;
10231e51764aSArtem Bityutskiy 	int err, rlen, overlap;
10241e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
10251e51764aSArtem Bityutskiy 
102677a7ae58SArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
102777a7ae58SArtem Bityutskiy 	       dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
10286eb61d58SRichard Weinberger 	ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
10296eb61d58SRichard Weinberger 	ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
10306eb61d58SRichard Weinberger 	ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT);
10311e51764aSArtem Bityutskiy 
10321e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
10331e51764aSArtem Bityutskiy 	overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
10341e51764aSArtem Bityutskiy 	if (!overlap) {
10351e51764aSArtem Bityutskiy 		/* We may safely unlock the write-buffer and read the data */
10361e51764aSArtem Bityutskiy 		spin_unlock(&wbuf->lock);
10371e51764aSArtem Bityutskiy 		return ubifs_read_node(c, buf, type, len, lnum, offs);
10381e51764aSArtem Bityutskiy 	}
10391e51764aSArtem Bityutskiy 
10401e51764aSArtem Bityutskiy 	/* Don't read under wbuf */
10411e51764aSArtem Bityutskiy 	rlen = wbuf->offs - offs;
10421e51764aSArtem Bityutskiy 	if (rlen < 0)
10431e51764aSArtem Bityutskiy 		rlen = 0;
10441e51764aSArtem Bityutskiy 
10451e51764aSArtem Bityutskiy 	/* Copy the rest from the write-buffer */
10461e51764aSArtem Bityutskiy 	memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
10471e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
10481e51764aSArtem Bityutskiy 
10491e51764aSArtem Bityutskiy 	if (rlen > 0) {
10501e51764aSArtem Bityutskiy 		/* Read everything that goes before write-buffer */
1051d304820aSArtem Bityutskiy 		err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
1052d304820aSArtem Bityutskiy 		if (err && err != -EBADMSG)
10531e51764aSArtem Bityutskiy 			return err;
10541e51764aSArtem Bityutskiy 	}
10551e51764aSArtem Bityutskiy 
10561e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
1057235c362bSSheng Yong 		ubifs_err(c, "bad node type (%d but expected %d)",
10581e51764aSArtem Bityutskiy 			  ch->node_type, type);
10591e51764aSArtem Bityutskiy 		goto out;
10601e51764aSArtem Bityutskiy 	}
10611e51764aSArtem Bityutskiy 
1062a33e30a0SZhihao Cheng 	err = ubifs_check_node(c, buf, len, lnum, offs, 0, 0);
10631e51764aSArtem Bityutskiy 	if (err) {
1064235c362bSSheng Yong 		ubifs_err(c, "expected node type %d", type);
10651e51764aSArtem Bityutskiy 		return err;
10661e51764aSArtem Bityutskiy 	}
10671e51764aSArtem Bityutskiy 
10681e51764aSArtem Bityutskiy 	rlen = le32_to_cpu(ch->len);
10691e51764aSArtem Bityutskiy 	if (rlen != len) {
1070235c362bSSheng Yong 		ubifs_err(c, "bad node length %d, expected %d", rlen, len);
10711e51764aSArtem Bityutskiy 		goto out;
10721e51764aSArtem Bityutskiy 	}
10731e51764aSArtem Bityutskiy 
10741e51764aSArtem Bityutskiy 	return 0;
10751e51764aSArtem Bityutskiy 
10761e51764aSArtem Bityutskiy out:
1077235c362bSSheng Yong 	ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
1078a33e30a0SZhihao Cheng 	ubifs_dump_node(c, buf, len);
10797c46d0aeSArtem Bityutskiy 	dump_stack();
10801e51764aSArtem Bityutskiy 	return -EINVAL;
10811e51764aSArtem Bityutskiy }
10821e51764aSArtem Bityutskiy 
10831e51764aSArtem Bityutskiy /**
10841e51764aSArtem Bityutskiy  * ubifs_read_node - read node.
10851e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10861e51764aSArtem Bityutskiy  * @buf: buffer to read to
10871e51764aSArtem Bityutskiy  * @type: node type
10881e51764aSArtem Bityutskiy  * @len: node length (not aligned)
10891e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
10901e51764aSArtem Bityutskiy  * @offs: offset within the logical eraseblock
10911e51764aSArtem Bityutskiy  *
1092b8f1da98SRandy Dunlap  * This function reads a node of known type and length, checks it and
10931e51764aSArtem Bityutskiy  * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
10941e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
10951e51764aSArtem Bityutskiy  */
ubifs_read_node(const struct ubifs_info * c,void * buf,int type,int len,int lnum,int offs)10961e51764aSArtem Bityutskiy int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
10971e51764aSArtem Bityutskiy 		    int lnum, int offs)
10981e51764aSArtem Bityutskiy {
10991e51764aSArtem Bityutskiy 	int err, l;
11001e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = buf;
11011e51764aSArtem Bityutskiy 
11021e51764aSArtem Bityutskiy 	dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
11036eb61d58SRichard Weinberger 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
11046eb61d58SRichard Weinberger 	ubifs_assert(c, len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
11056eb61d58SRichard Weinberger 	ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
11066eb61d58SRichard Weinberger 	ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT);
11071e51764aSArtem Bityutskiy 
1108d304820aSArtem Bityutskiy 	err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
1109d304820aSArtem Bityutskiy 	if (err && err != -EBADMSG)
11101e51764aSArtem Bityutskiy 		return err;
11111e51764aSArtem Bityutskiy 
11121e51764aSArtem Bityutskiy 	if (type != ch->node_type) {
111390bea5a3SDaniel Golle 		ubifs_errc(c, "bad node type (%d but expected %d)",
11141e51764aSArtem Bityutskiy 			   ch->node_type, type);
11151e51764aSArtem Bityutskiy 		goto out;
11161e51764aSArtem Bityutskiy 	}
11171e51764aSArtem Bityutskiy 
1118a33e30a0SZhihao Cheng 	err = ubifs_check_node(c, buf, len, lnum, offs, 0, 0);
11191e51764aSArtem Bityutskiy 	if (err) {
112090bea5a3SDaniel Golle 		ubifs_errc(c, "expected node type %d", type);
11211e51764aSArtem Bityutskiy 		return err;
11221e51764aSArtem Bityutskiy 	}
11231e51764aSArtem Bityutskiy 
11241e51764aSArtem Bityutskiy 	l = le32_to_cpu(ch->len);
11251e51764aSArtem Bityutskiy 	if (l != len) {
112690bea5a3SDaniel Golle 		ubifs_errc(c, "bad node length %d, expected %d", l, len);
11271e51764aSArtem Bityutskiy 		goto out;
11281e51764aSArtem Bityutskiy 	}
11291e51764aSArtem Bityutskiy 
11301e51764aSArtem Bityutskiy 	return 0;
11311e51764aSArtem Bityutskiy 
11321e51764aSArtem Bityutskiy out:
113390bea5a3SDaniel Golle 	ubifs_errc(c, "bad node at LEB %d:%d, LEB mapping status %d", lnum,
113490bea5a3SDaniel Golle 		   offs, ubi_is_mapped(c->ubi, lnum));
113590bea5a3SDaniel Golle 	if (!c->probing) {
1136a33e30a0SZhihao Cheng 		ubifs_dump_node(c, buf, len);
11377c46d0aeSArtem Bityutskiy 		dump_stack();
113890bea5a3SDaniel Golle 	}
11391e51764aSArtem Bityutskiy 	return -EINVAL;
11401e51764aSArtem Bityutskiy }
11411e51764aSArtem Bityutskiy 
11421e51764aSArtem Bityutskiy /**
11431e51764aSArtem Bityutskiy  * ubifs_wbuf_init - initialize write-buffer.
11441e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11451e51764aSArtem Bityutskiy  * @wbuf: write-buffer to initialize
11461e51764aSArtem Bityutskiy  *
1147cb54ef8bSArtem Bityutskiy  * This function initializes write-buffer. Returns zero in case of success
11481e51764aSArtem Bityutskiy  * %-ENOMEM in case of failure.
11491e51764aSArtem Bityutskiy  */
ubifs_wbuf_init(struct ubifs_info * c,struct ubifs_wbuf * wbuf)11501e51764aSArtem Bityutskiy int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
11511e51764aSArtem Bityutskiy {
11521e51764aSArtem Bityutskiy 	size_t size;
11531e51764aSArtem Bityutskiy 
11546c7f74f7SArtem Bityutskiy 	wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
11551e51764aSArtem Bityutskiy 	if (!wbuf->buf)
11561e51764aSArtem Bityutskiy 		return -ENOMEM;
11571e51764aSArtem Bityutskiy 
11586c7f74f7SArtem Bityutskiy 	size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
11591e51764aSArtem Bityutskiy 	wbuf->inodes = kmalloc(size, GFP_KERNEL);
11601e51764aSArtem Bityutskiy 	if (!wbuf->inodes) {
11611e51764aSArtem Bityutskiy 		kfree(wbuf->buf);
11621e51764aSArtem Bityutskiy 		wbuf->buf = NULL;
11631e51764aSArtem Bityutskiy 		return -ENOMEM;
11641e51764aSArtem Bityutskiy 	}
11651e51764aSArtem Bityutskiy 
11661e51764aSArtem Bityutskiy 	wbuf->used = 0;
11671e51764aSArtem Bityutskiy 	wbuf->lnum = wbuf->offs = -1;
11686c7f74f7SArtem Bityutskiy 	/*
11696c7f74f7SArtem Bityutskiy 	 * If the LEB starts at the max. write size aligned address, then
11706c7f74f7SArtem Bityutskiy 	 * write-buffer size has to be set to @c->max_write_size. Otherwise,
11716c7f74f7SArtem Bityutskiy 	 * set it to something smaller so that it ends at the closest max.
11726c7f74f7SArtem Bityutskiy 	 * write size boundary.
11736c7f74f7SArtem Bityutskiy 	 */
11746c7f74f7SArtem Bityutskiy 	size = c->max_write_size - (c->leb_start % c->max_write_size);
11756c7f74f7SArtem Bityutskiy 	wbuf->avail = wbuf->size = size;
11761e51764aSArtem Bityutskiy 	wbuf->sync_callback = NULL;
11771e51764aSArtem Bityutskiy 	mutex_init(&wbuf->io_mutex);
11781e51764aSArtem Bityutskiy 	spin_lock_init(&wbuf->lock);
11791e51764aSArtem Bityutskiy 	wbuf->c = c;
11801e51764aSArtem Bityutskiy 	wbuf->next_ino = 0;
11811e51764aSArtem Bityutskiy 
1182f2c5dbd7SArtem Bityutskiy 	hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1183f2c5dbd7SArtem Bityutskiy 	wbuf->timer.function = wbuf_timer_callback_nolock;
11841e51764aSArtem Bityutskiy 	return 0;
11851e51764aSArtem Bityutskiy }
11861e51764aSArtem Bityutskiy 
11871e51764aSArtem Bityutskiy /**
11881e51764aSArtem Bityutskiy  * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
11891e51764aSArtem Bityutskiy  * @wbuf: the write-buffer where to add
11901e51764aSArtem Bityutskiy  * @inum: the inode number
11911e51764aSArtem Bityutskiy  *
11921e51764aSArtem Bityutskiy  * This function adds an inode number to the inode array of the write-buffer.
11931e51764aSArtem Bityutskiy  */
ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf * wbuf,ino_t inum)11941e51764aSArtem Bityutskiy void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
11951e51764aSArtem Bityutskiy {
11961e51764aSArtem Bityutskiy 	if (!wbuf->buf)
11971e51764aSArtem Bityutskiy 		/* NOR flash or something similar */
11981e51764aSArtem Bityutskiy 		return;
11991e51764aSArtem Bityutskiy 
12001e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
12011e51764aSArtem Bityutskiy 	if (wbuf->used)
12021e51764aSArtem Bityutskiy 		wbuf->inodes[wbuf->next_ino++] = inum;
12031e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
12041e51764aSArtem Bityutskiy }
12051e51764aSArtem Bityutskiy 
12061e51764aSArtem Bityutskiy /**
12071e51764aSArtem Bityutskiy  * wbuf_has_ino - returns if the wbuf contains data from the inode.
12081e51764aSArtem Bityutskiy  * @wbuf: the write-buffer
12091e51764aSArtem Bityutskiy  * @inum: the inode number
12101e51764aSArtem Bityutskiy  *
12111e51764aSArtem Bityutskiy  * This function returns with %1 if the write-buffer contains some data from the
12121e51764aSArtem Bityutskiy  * given inode otherwise it returns with %0.
12131e51764aSArtem Bityutskiy  */
wbuf_has_ino(struct ubifs_wbuf * wbuf,ino_t inum)12141e51764aSArtem Bityutskiy static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
12151e51764aSArtem Bityutskiy {
12161e51764aSArtem Bityutskiy 	int i, ret = 0;
12171e51764aSArtem Bityutskiy 
12181e51764aSArtem Bityutskiy 	spin_lock(&wbuf->lock);
12191e51764aSArtem Bityutskiy 	for (i = 0; i < wbuf->next_ino; i++)
12201e51764aSArtem Bityutskiy 		if (inum == wbuf->inodes[i]) {
12211e51764aSArtem Bityutskiy 			ret = 1;
12221e51764aSArtem Bityutskiy 			break;
12231e51764aSArtem Bityutskiy 		}
12241e51764aSArtem Bityutskiy 	spin_unlock(&wbuf->lock);
12251e51764aSArtem Bityutskiy 
12261e51764aSArtem Bityutskiy 	return ret;
12271e51764aSArtem Bityutskiy }
12281e51764aSArtem Bityutskiy 
12291e51764aSArtem Bityutskiy /**
12301e51764aSArtem Bityutskiy  * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
12311e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
12321e51764aSArtem Bityutskiy  * @inode: inode to synchronize
12331e51764aSArtem Bityutskiy  *
12341e51764aSArtem Bityutskiy  * This function synchronizes write-buffers which contain nodes belonging to
12351e51764aSArtem Bityutskiy  * @inode. Returns zero in case of success and a negative error code in case of
12361e51764aSArtem Bityutskiy  * failure.
12371e51764aSArtem Bityutskiy  */
ubifs_sync_wbufs_by_inode(struct ubifs_info * c,struct inode * inode)12381e51764aSArtem Bityutskiy int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
12391e51764aSArtem Bityutskiy {
12401e51764aSArtem Bityutskiy 	int i, err = 0;
12411e51764aSArtem Bityutskiy 
12421e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
12431e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
12441e51764aSArtem Bityutskiy 
12451e51764aSArtem Bityutskiy 		if (i == GCHD)
12461e51764aSArtem Bityutskiy 			/*
12471e51764aSArtem Bityutskiy 			 * GC head is special, do not look at it. Even if the
12481e51764aSArtem Bityutskiy 			 * head contains something related to this inode, it is
12491e51764aSArtem Bityutskiy 			 * a _copy_ of corresponding on-flash node which sits
12501e51764aSArtem Bityutskiy 			 * somewhere else.
12511e51764aSArtem Bityutskiy 			 */
12521e51764aSArtem Bityutskiy 			continue;
12531e51764aSArtem Bityutskiy 
12541e51764aSArtem Bityutskiy 		if (!wbuf_has_ino(wbuf, inode->i_ino))
12551e51764aSArtem Bityutskiy 			continue;
12561e51764aSArtem Bityutskiy 
12571e51764aSArtem Bityutskiy 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
12581e51764aSArtem Bityutskiy 		if (wbuf_has_ino(wbuf, inode->i_ino))
12591e51764aSArtem Bityutskiy 			err = ubifs_wbuf_sync_nolock(wbuf);
12601e51764aSArtem Bityutskiy 		mutex_unlock(&wbuf->io_mutex);
12611e51764aSArtem Bityutskiy 
12621e51764aSArtem Bityutskiy 		if (err) {
12631e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
12641e51764aSArtem Bityutskiy 			return err;
12651e51764aSArtem Bityutskiy 		}
12661e51764aSArtem Bityutskiy 	}
12671e51764aSArtem Bityutskiy 	return 0;
12681e51764aSArtem Bityutskiy }
1269