xref: /OK3568_Linux_fs/kernel/fs/ubifs/io.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * This file is part of UBIFS.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2006-2008 Nokia Corporation.
6*4882a593Smuzhiyun  * Copyright (C) 2006, 2007 University of Szeged, Hungary
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Authors: Artem Bityutskiy (Битюцкий Артём)
9*4882a593Smuzhiyun  *          Adrian Hunter
10*4882a593Smuzhiyun  *          Zoltan Sogor
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * This file implements UBIFS I/O subsystem which provides various I/O-related
15*4882a593Smuzhiyun  * helper functions (reading/writing/checking/validating nodes) and implements
16*4882a593Smuzhiyun  * write-buffering support. Write buffers help to save space which otherwise
17*4882a593Smuzhiyun  * would have been wasted for padding to the nearest minimal I/O unit boundary.
18*4882a593Smuzhiyun  * Instead, data first goes to the write-buffer and is flushed when the
19*4882a593Smuzhiyun  * buffer is full or when it is not used for some time (by timer). This is
20*4882a593Smuzhiyun  * similar to the mechanism is used by JFFS2.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
23*4882a593Smuzhiyun  * write size (@c->max_write_size). The latter is the maximum amount of bytes
24*4882a593Smuzhiyun  * the underlying flash is able to program at a time, and writing in
25*4882a593Smuzhiyun  * @c->max_write_size units should presumably be faster. Obviously,
26*4882a593Smuzhiyun  * @c->min_io_size <= @c->max_write_size. Write-buffers are of
27*4882a593Smuzhiyun  * @c->max_write_size bytes in size for maximum performance. However, when a
28*4882a593Smuzhiyun  * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
29*4882a593Smuzhiyun  * boundary) which contains data is written, not the whole write-buffer,
30*4882a593Smuzhiyun  * because this is more space-efficient.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * This optimization adds few complications to the code. Indeed, on the one
33*4882a593Smuzhiyun  * hand, we want to write in optimal @c->max_write_size bytes chunks, which
34*4882a593Smuzhiyun  * also means aligning writes at the @c->max_write_size bytes offsets. On the
35*4882a593Smuzhiyun  * other hand, we do not want to waste space when synchronizing the write
36*4882a593Smuzhiyun  * buffer, so during synchronization we writes in smaller chunks. And this makes
37*4882a593Smuzhiyun  * the next write offset to be not aligned to @c->max_write_size bytes. So the
38*4882a593Smuzhiyun  * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
39*4882a593Smuzhiyun  * to @c->max_write_size bytes again. We do this by temporarily shrinking
40*4882a593Smuzhiyun  * write-buffer size (@wbuf->size).
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
43*4882a593Smuzhiyun  * mutexes defined inside these objects. Since sometimes upper-level code
44*4882a593Smuzhiyun  * has to lock the write-buffer (e.g. journal space reservation code), many
45*4882a593Smuzhiyun  * functions related to write-buffers have "nolock" suffix which means that the
46*4882a593Smuzhiyun  * caller has to lock the write-buffer before calling this function.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
49*4882a593Smuzhiyun  * aligned, UBIFS starts the next node from the aligned address, and the padded
50*4882a593Smuzhiyun  * bytes may contain any rubbish. In other words, UBIFS does not put padding
51*4882a593Smuzhiyun  * bytes in those small gaps. Common headers of nodes store real node lengths,
52*4882a593Smuzhiyun  * not aligned lengths. Indexing nodes also store real lengths in branches.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
55*4882a593Smuzhiyun  * uses padding nodes or padding bytes, if the padding node does not fit.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
58*4882a593Smuzhiyun  * they are read from the flash media.
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #include <linux/crc32.h>
62*4882a593Smuzhiyun #include <linux/slab.h>
63*4882a593Smuzhiyun #include "ubifs.h"
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /**
66*4882a593Smuzhiyun  * ubifs_ro_mode - switch UBIFS to read read-only mode.
67*4882a593Smuzhiyun  * @c: UBIFS file-system description object
68*4882a593Smuzhiyun  * @err: error code which is the reason of switching to R/O mode
69*4882a593Smuzhiyun  */
ubifs_ro_mode(struct ubifs_info * c,int err)70*4882a593Smuzhiyun void ubifs_ro_mode(struct ubifs_info *c, int err)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	if (!c->ro_error) {
73*4882a593Smuzhiyun 		c->ro_error = 1;
74*4882a593Smuzhiyun 		c->no_chk_data_crc = 0;
75*4882a593Smuzhiyun 		c->vfs_sb->s_flags |= SB_RDONLY;
76*4882a593Smuzhiyun 		ubifs_warn(c, "switched to read-only mode, error %d", err);
77*4882a593Smuzhiyun 		dump_stack();
78*4882a593Smuzhiyun 	}
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * Below are simple wrappers over UBI I/O functions which include some
83*4882a593Smuzhiyun  * additional checks and UBIFS debugging stuff. See corresponding UBI function
84*4882a593Smuzhiyun  * for more information.
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun 
ubifs_leb_read(const struct ubifs_info * c,int lnum,void * buf,int offs,int len,int even_ebadmsg)87*4882a593Smuzhiyun int ubifs_leb_read(const struct ubifs_info *c, int lnum, void *buf, int offs,
88*4882a593Smuzhiyun 		   int len, int even_ebadmsg)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	int err;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	err = ubi_read(c->ubi, lnum, buf, offs, len);
93*4882a593Smuzhiyun 	/*
94*4882a593Smuzhiyun 	 * In case of %-EBADMSG print the error message only if the
95*4882a593Smuzhiyun 	 * @even_ebadmsg is true.
96*4882a593Smuzhiyun 	 */
97*4882a593Smuzhiyun 	if (err && (err != -EBADMSG || even_ebadmsg)) {
98*4882a593Smuzhiyun 		ubifs_err(c, "reading %d bytes from LEB %d:%d failed, error %d",
99*4882a593Smuzhiyun 			  len, lnum, offs, err);
100*4882a593Smuzhiyun 		dump_stack();
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	return err;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
ubifs_leb_write(struct ubifs_info * c,int lnum,const void * buf,int offs,int len)105*4882a593Smuzhiyun int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
106*4882a593Smuzhiyun 		    int len)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	int err;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
111*4882a593Smuzhiyun 	if (c->ro_error)
112*4882a593Smuzhiyun 		return -EROFS;
113*4882a593Smuzhiyun 	if (!dbg_is_tst_rcvry(c))
114*4882a593Smuzhiyun 		err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
115*4882a593Smuzhiyun 	else
116*4882a593Smuzhiyun 		err = dbg_leb_write(c, lnum, buf, offs, len);
117*4882a593Smuzhiyun 	if (err) {
118*4882a593Smuzhiyun 		ubifs_err(c, "writing %d bytes to LEB %d:%d failed, error %d",
119*4882a593Smuzhiyun 			  len, lnum, offs, err);
120*4882a593Smuzhiyun 		ubifs_ro_mode(c, err);
121*4882a593Smuzhiyun 		dump_stack();
122*4882a593Smuzhiyun 	}
123*4882a593Smuzhiyun 	return err;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
ubifs_leb_change(struct ubifs_info * c,int lnum,const void * buf,int len)126*4882a593Smuzhiyun int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	int err;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
131*4882a593Smuzhiyun 	if (c->ro_error)
132*4882a593Smuzhiyun 		return -EROFS;
133*4882a593Smuzhiyun 	if (!dbg_is_tst_rcvry(c))
134*4882a593Smuzhiyun 		err = ubi_leb_change(c->ubi, lnum, buf, len);
135*4882a593Smuzhiyun 	else
136*4882a593Smuzhiyun 		err = dbg_leb_change(c, lnum, buf, len);
137*4882a593Smuzhiyun 	if (err) {
138*4882a593Smuzhiyun 		ubifs_err(c, "changing %d bytes in LEB %d failed, error %d",
139*4882a593Smuzhiyun 			  len, lnum, err);
140*4882a593Smuzhiyun 		ubifs_ro_mode(c, err);
141*4882a593Smuzhiyun 		dump_stack();
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 	return err;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
ubifs_leb_unmap(struct ubifs_info * c,int lnum)146*4882a593Smuzhiyun int ubifs_leb_unmap(struct ubifs_info *c, int lnum)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	int err;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
151*4882a593Smuzhiyun 	if (c->ro_error)
152*4882a593Smuzhiyun 		return -EROFS;
153*4882a593Smuzhiyun 	if (!dbg_is_tst_rcvry(c))
154*4882a593Smuzhiyun 		err = ubi_leb_unmap(c->ubi, lnum);
155*4882a593Smuzhiyun 	else
156*4882a593Smuzhiyun 		err = dbg_leb_unmap(c, lnum);
157*4882a593Smuzhiyun 	if (err) {
158*4882a593Smuzhiyun 		ubifs_err(c, "unmap LEB %d failed, error %d", lnum, err);
159*4882a593Smuzhiyun 		ubifs_ro_mode(c, err);
160*4882a593Smuzhiyun 		dump_stack();
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 	return err;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
ubifs_leb_map(struct ubifs_info * c,int lnum)165*4882a593Smuzhiyun int ubifs_leb_map(struct ubifs_info *c, int lnum)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	int err;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
170*4882a593Smuzhiyun 	if (c->ro_error)
171*4882a593Smuzhiyun 		return -EROFS;
172*4882a593Smuzhiyun 	if (!dbg_is_tst_rcvry(c))
173*4882a593Smuzhiyun 		err = ubi_leb_map(c->ubi, lnum);
174*4882a593Smuzhiyun 	else
175*4882a593Smuzhiyun 		err = dbg_leb_map(c, lnum);
176*4882a593Smuzhiyun 	if (err) {
177*4882a593Smuzhiyun 		ubifs_err(c, "mapping LEB %d failed, error %d", lnum, err);
178*4882a593Smuzhiyun 		ubifs_ro_mode(c, err);
179*4882a593Smuzhiyun 		dump_stack();
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 	return err;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
ubifs_is_mapped(const struct ubifs_info * c,int lnum)184*4882a593Smuzhiyun int ubifs_is_mapped(const struct ubifs_info *c, int lnum)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	int err;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	err = ubi_is_mapped(c->ubi, lnum);
189*4882a593Smuzhiyun 	if (err < 0) {
190*4882a593Smuzhiyun 		ubifs_err(c, "ubi_is_mapped failed for LEB %d, error %d",
191*4882a593Smuzhiyun 			  lnum, err);
192*4882a593Smuzhiyun 		dump_stack();
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 	return err;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /**
198*4882a593Smuzhiyun  * ubifs_check_node - check node.
199*4882a593Smuzhiyun  * @c: UBIFS file-system description object
200*4882a593Smuzhiyun  * @buf: node to check
201*4882a593Smuzhiyun  * @lnum: logical eraseblock number
202*4882a593Smuzhiyun  * @offs: offset within the logical eraseblock
203*4882a593Smuzhiyun  * @quiet: print no messages
204*4882a593Smuzhiyun  * @must_chk_crc: indicates whether to always check the CRC
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * This function checks node magic number and CRC checksum. This function also
207*4882a593Smuzhiyun  * validates node length to prevent UBIFS from becoming crazy when an attacker
208*4882a593Smuzhiyun  * feeds it a file-system image with incorrect nodes. For example, too large
209*4882a593Smuzhiyun  * node length in the common header could cause UBIFS to read memory outside of
210*4882a593Smuzhiyun  * allocated buffer when checking the CRC checksum.
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
213*4882a593Smuzhiyun  * true, which is controlled by corresponding UBIFS mount option. However, if
214*4882a593Smuzhiyun  * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
215*4882a593Smuzhiyun  * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
216*4882a593Smuzhiyun  * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
217*4882a593Smuzhiyun  * is checked. This is because during mounting or re-mounting from R/O mode to
218*4882a593Smuzhiyun  * R/W mode we may read journal nodes (when replying the journal or doing the
219*4882a593Smuzhiyun  * recovery) and the journal nodes may potentially be corrupted, so checking is
220*4882a593Smuzhiyun  * required.
221*4882a593Smuzhiyun  *
222*4882a593Smuzhiyun  * This function returns zero in case of success and %-EUCLEAN in case of bad
223*4882a593Smuzhiyun  * CRC or magic.
224*4882a593Smuzhiyun  */
ubifs_check_node(const struct ubifs_info * c,const void * buf,int lnum,int offs,int quiet,int must_chk_crc)225*4882a593Smuzhiyun int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
226*4882a593Smuzhiyun 		     int offs, int quiet, int must_chk_crc)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	int err = -EINVAL, type, node_len, dump_node = 1;
229*4882a593Smuzhiyun 	uint32_t crc, node_crc, magic;
230*4882a593Smuzhiyun 	const struct ubifs_ch *ch = buf;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
233*4882a593Smuzhiyun 	ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	magic = le32_to_cpu(ch->magic);
236*4882a593Smuzhiyun 	if (magic != UBIFS_NODE_MAGIC) {
237*4882a593Smuzhiyun 		if (!quiet)
238*4882a593Smuzhiyun 			ubifs_err(c, "bad magic %#08x, expected %#08x",
239*4882a593Smuzhiyun 				  magic, UBIFS_NODE_MAGIC);
240*4882a593Smuzhiyun 		err = -EUCLEAN;
241*4882a593Smuzhiyun 		goto out;
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	type = ch->node_type;
245*4882a593Smuzhiyun 	if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
246*4882a593Smuzhiyun 		if (!quiet)
247*4882a593Smuzhiyun 			ubifs_err(c, "bad node type %d", type);
248*4882a593Smuzhiyun 		goto out;
249*4882a593Smuzhiyun 	}
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	node_len = le32_to_cpu(ch->len);
252*4882a593Smuzhiyun 	if (node_len + offs > c->leb_size)
253*4882a593Smuzhiyun 		goto out_len;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (c->ranges[type].max_len == 0) {
256*4882a593Smuzhiyun 		if (node_len != c->ranges[type].len)
257*4882a593Smuzhiyun 			goto out_len;
258*4882a593Smuzhiyun 	} else if (node_len < c->ranges[type].min_len ||
259*4882a593Smuzhiyun 		   node_len > c->ranges[type].max_len)
260*4882a593Smuzhiyun 		goto out_len;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
263*4882a593Smuzhiyun 	    !c->remounting_rw && c->no_chk_data_crc)
264*4882a593Smuzhiyun 		return 0;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
267*4882a593Smuzhiyun 	node_crc = le32_to_cpu(ch->crc);
268*4882a593Smuzhiyun 	if (crc != node_crc) {
269*4882a593Smuzhiyun 		if (!quiet)
270*4882a593Smuzhiyun 			ubifs_err(c, "bad CRC: calculated %#08x, read %#08x",
271*4882a593Smuzhiyun 				  crc, node_crc);
272*4882a593Smuzhiyun 		err = -EUCLEAN;
273*4882a593Smuzhiyun 		goto out;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	return 0;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun out_len:
279*4882a593Smuzhiyun 	if (!quiet)
280*4882a593Smuzhiyun 		ubifs_err(c, "bad node length %d", node_len);
281*4882a593Smuzhiyun 	if (type == UBIFS_DATA_NODE && node_len > UBIFS_DATA_NODE_SZ)
282*4882a593Smuzhiyun 		dump_node = 0;
283*4882a593Smuzhiyun out:
284*4882a593Smuzhiyun 	if (!quiet) {
285*4882a593Smuzhiyun 		ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
286*4882a593Smuzhiyun 		if (dump_node) {
287*4882a593Smuzhiyun 			ubifs_dump_node(c, buf);
288*4882a593Smuzhiyun 		} else {
289*4882a593Smuzhiyun 			int safe_len = min3(node_len, c->leb_size - offs,
290*4882a593Smuzhiyun 				(int)UBIFS_MAX_DATA_NODE_SZ);
291*4882a593Smuzhiyun 			pr_err("\tprevent out-of-bounds memory access\n");
292*4882a593Smuzhiyun 			pr_err("\ttruncated data node length      %d\n", safe_len);
293*4882a593Smuzhiyun 			pr_err("\tcorrupted data node:\n");
294*4882a593Smuzhiyun 			print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
295*4882a593Smuzhiyun 					buf, safe_len, 0);
296*4882a593Smuzhiyun 		}
297*4882a593Smuzhiyun 		dump_stack();
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 	return err;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /**
303*4882a593Smuzhiyun  * ubifs_pad - pad flash space.
304*4882a593Smuzhiyun  * @c: UBIFS file-system description object
305*4882a593Smuzhiyun  * @buf: buffer to put padding to
306*4882a593Smuzhiyun  * @pad: how many bytes to pad
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * The flash media obliges us to write only in chunks of %c->min_io_size and
309*4882a593Smuzhiyun  * when we have to write less data we add padding node to the write-buffer and
310*4882a593Smuzhiyun  * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
311*4882a593Smuzhiyun  * media is being scanned. If the amount of wasted space is not enough to fit a
312*4882a593Smuzhiyun  * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
313*4882a593Smuzhiyun  * pattern (%UBIFS_PADDING_BYTE).
314*4882a593Smuzhiyun  *
315*4882a593Smuzhiyun  * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
316*4882a593Smuzhiyun  * used.
317*4882a593Smuzhiyun  */
ubifs_pad(const struct ubifs_info * c,void * buf,int pad)318*4882a593Smuzhiyun void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	uint32_t crc;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	ubifs_assert(c, pad >= 0);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (pad >= UBIFS_PAD_NODE_SZ) {
325*4882a593Smuzhiyun 		struct ubifs_ch *ch = buf;
326*4882a593Smuzhiyun 		struct ubifs_pad_node *pad_node = buf;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 		ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
329*4882a593Smuzhiyun 		ch->node_type = UBIFS_PAD_NODE;
330*4882a593Smuzhiyun 		ch->group_type = UBIFS_NO_NODE_GROUP;
331*4882a593Smuzhiyun 		ch->padding[0] = ch->padding[1] = 0;
332*4882a593Smuzhiyun 		ch->sqnum = 0;
333*4882a593Smuzhiyun 		ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
334*4882a593Smuzhiyun 		pad -= UBIFS_PAD_NODE_SZ;
335*4882a593Smuzhiyun 		pad_node->pad_len = cpu_to_le32(pad);
336*4882a593Smuzhiyun 		crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
337*4882a593Smuzhiyun 		ch->crc = cpu_to_le32(crc);
338*4882a593Smuzhiyun 		memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
339*4882a593Smuzhiyun 	} else if (pad > 0)
340*4882a593Smuzhiyun 		/* Too little space, padding node won't fit */
341*4882a593Smuzhiyun 		memset(buf, UBIFS_PADDING_BYTE, pad);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun  * next_sqnum - get next sequence number.
346*4882a593Smuzhiyun  * @c: UBIFS file-system description object
347*4882a593Smuzhiyun  */
next_sqnum(struct ubifs_info * c)348*4882a593Smuzhiyun static unsigned long long next_sqnum(struct ubifs_info *c)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	unsigned long long sqnum;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	spin_lock(&c->cnt_lock);
353*4882a593Smuzhiyun 	sqnum = ++c->max_sqnum;
354*4882a593Smuzhiyun 	spin_unlock(&c->cnt_lock);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
357*4882a593Smuzhiyun 		if (sqnum >= SQNUM_WATERMARK) {
358*4882a593Smuzhiyun 			ubifs_err(c, "sequence number overflow %llu, end of life",
359*4882a593Smuzhiyun 				  sqnum);
360*4882a593Smuzhiyun 			ubifs_ro_mode(c, -EINVAL);
361*4882a593Smuzhiyun 		}
362*4882a593Smuzhiyun 		ubifs_warn(c, "running out of sequence numbers, end of life soon");
363*4882a593Smuzhiyun 	}
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	return sqnum;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
ubifs_init_node(struct ubifs_info * c,void * node,int len,int pad)368*4882a593Smuzhiyun void ubifs_init_node(struct ubifs_info *c, void *node, int len, int pad)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	struct ubifs_ch *ch = node;
371*4882a593Smuzhiyun 	unsigned long long sqnum = next_sqnum(c);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	ubifs_assert(c, len >= UBIFS_CH_SZ);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
376*4882a593Smuzhiyun 	ch->len = cpu_to_le32(len);
377*4882a593Smuzhiyun 	ch->group_type = UBIFS_NO_NODE_GROUP;
378*4882a593Smuzhiyun 	ch->sqnum = cpu_to_le64(sqnum);
379*4882a593Smuzhiyun 	ch->padding[0] = ch->padding[1] = 0;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	if (pad) {
382*4882a593Smuzhiyun 		len = ALIGN(len, 8);
383*4882a593Smuzhiyun 		pad = ALIGN(len, c->min_io_size) - len;
384*4882a593Smuzhiyun 		ubifs_pad(c, node + len, pad);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
ubifs_crc_node(struct ubifs_info * c,void * node,int len)388*4882a593Smuzhiyun void ubifs_crc_node(struct ubifs_info *c, void *node, int len)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	struct ubifs_ch *ch = node;
391*4882a593Smuzhiyun 	uint32_t crc;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
394*4882a593Smuzhiyun 	ch->crc = cpu_to_le32(crc);
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun /**
398*4882a593Smuzhiyun  * ubifs_prepare_node_hmac - prepare node to be written to flash.
399*4882a593Smuzhiyun  * @c: UBIFS file-system description object
400*4882a593Smuzhiyun  * @node: the node to pad
401*4882a593Smuzhiyun  * @len: node length
402*4882a593Smuzhiyun  * @hmac_offs: offset of the HMAC in the node
403*4882a593Smuzhiyun  * @pad: if the buffer has to be padded
404*4882a593Smuzhiyun  *
405*4882a593Smuzhiyun  * This function prepares node at @node to be written to the media - it
406*4882a593Smuzhiyun  * calculates node CRC, fills the common header, and adds proper padding up to
407*4882a593Smuzhiyun  * the next minimum I/O unit if @pad is not zero. if @hmac_offs is positive then
408*4882a593Smuzhiyun  * a HMAC is inserted into the node at the given offset.
409*4882a593Smuzhiyun  *
410*4882a593Smuzhiyun  * This function returns 0 for success or a negative error code otherwise.
411*4882a593Smuzhiyun  */
ubifs_prepare_node_hmac(struct ubifs_info * c,void * node,int len,int hmac_offs,int pad)412*4882a593Smuzhiyun int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
413*4882a593Smuzhiyun 			    int hmac_offs, int pad)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	int err;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	ubifs_init_node(c, node, len, pad);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	if (hmac_offs > 0) {
420*4882a593Smuzhiyun 		err = ubifs_node_insert_hmac(c, node, len, hmac_offs);
421*4882a593Smuzhiyun 		if (err)
422*4882a593Smuzhiyun 			return err;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	ubifs_crc_node(c, node, len);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	return 0;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /**
431*4882a593Smuzhiyun  * ubifs_prepare_node - prepare node to be written to flash.
432*4882a593Smuzhiyun  * @c: UBIFS file-system description object
433*4882a593Smuzhiyun  * @node: the node to pad
434*4882a593Smuzhiyun  * @len: node length
435*4882a593Smuzhiyun  * @pad: if the buffer has to be padded
436*4882a593Smuzhiyun  *
437*4882a593Smuzhiyun  * This function prepares node at @node to be written to the media - it
438*4882a593Smuzhiyun  * calculates node CRC, fills the common header, and adds proper padding up to
439*4882a593Smuzhiyun  * the next minimum I/O unit if @pad is not zero.
440*4882a593Smuzhiyun  */
ubifs_prepare_node(struct ubifs_info * c,void * node,int len,int pad)441*4882a593Smuzhiyun void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	/*
444*4882a593Smuzhiyun 	 * Deliberately ignore return value since this function can only fail
445*4882a593Smuzhiyun 	 * when a hmac offset is given.
446*4882a593Smuzhiyun 	 */
447*4882a593Smuzhiyun 	ubifs_prepare_node_hmac(c, node, len, 0, pad);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun /**
451*4882a593Smuzhiyun  * ubifs_prep_grp_node - prepare node of a group to be written to flash.
452*4882a593Smuzhiyun  * @c: UBIFS file-system description object
453*4882a593Smuzhiyun  * @node: the node to pad
454*4882a593Smuzhiyun  * @len: node length
455*4882a593Smuzhiyun  * @last: indicates the last node of the group
456*4882a593Smuzhiyun  *
457*4882a593Smuzhiyun  * This function prepares node at @node to be written to the media - it
458*4882a593Smuzhiyun  * calculates node CRC and fills the common header.
459*4882a593Smuzhiyun  */
ubifs_prep_grp_node(struct ubifs_info * c,void * node,int len,int last)460*4882a593Smuzhiyun void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	uint32_t crc;
463*4882a593Smuzhiyun 	struct ubifs_ch *ch = node;
464*4882a593Smuzhiyun 	unsigned long long sqnum = next_sqnum(c);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	ubifs_assert(c, len >= UBIFS_CH_SZ);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
469*4882a593Smuzhiyun 	ch->len = cpu_to_le32(len);
470*4882a593Smuzhiyun 	if (last)
471*4882a593Smuzhiyun 		ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
472*4882a593Smuzhiyun 	else
473*4882a593Smuzhiyun 		ch->group_type = UBIFS_IN_NODE_GROUP;
474*4882a593Smuzhiyun 	ch->sqnum = cpu_to_le64(sqnum);
475*4882a593Smuzhiyun 	ch->padding[0] = ch->padding[1] = 0;
476*4882a593Smuzhiyun 	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
477*4882a593Smuzhiyun 	ch->crc = cpu_to_le32(crc);
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun /**
481*4882a593Smuzhiyun  * wbuf_timer_callback - write-buffer timer callback function.
482*4882a593Smuzhiyun  * @timer: timer data (write-buffer descriptor)
483*4882a593Smuzhiyun  *
484*4882a593Smuzhiyun  * This function is called when the write-buffer timer expires.
485*4882a593Smuzhiyun  */
wbuf_timer_callback_nolock(struct hrtimer * timer)486*4882a593Smuzhiyun static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
491*4882a593Smuzhiyun 	wbuf->need_sync = 1;
492*4882a593Smuzhiyun 	wbuf->c->need_wbuf_sync = 1;
493*4882a593Smuzhiyun 	ubifs_wake_up_bgt(wbuf->c);
494*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun /**
498*4882a593Smuzhiyun  * new_wbuf_timer - start new write-buffer timer.
499*4882a593Smuzhiyun  * @c: UBIFS file-system description object
500*4882a593Smuzhiyun  * @wbuf: write-buffer descriptor
501*4882a593Smuzhiyun  */
new_wbuf_timer_nolock(struct ubifs_info * c,struct ubifs_wbuf * wbuf)502*4882a593Smuzhiyun static void new_wbuf_timer_nolock(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	ktime_t softlimit = ms_to_ktime(dirty_writeback_interval * 10);
505*4882a593Smuzhiyun 	unsigned long long delta = dirty_writeback_interval;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	/* centi to milli, milli to nano, then 10% */
508*4882a593Smuzhiyun 	delta *= 10ULL * NSEC_PER_MSEC / 10ULL;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	ubifs_assert(c, !hrtimer_active(&wbuf->timer));
511*4882a593Smuzhiyun 	ubifs_assert(c, delta <= ULONG_MAX);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (wbuf->no_timer)
514*4882a593Smuzhiyun 		return;
515*4882a593Smuzhiyun 	dbg_io("set timer for jhead %s, %llu-%llu millisecs",
516*4882a593Smuzhiyun 	       dbg_jhead(wbuf->jhead),
517*4882a593Smuzhiyun 	       div_u64(ktime_to_ns(softlimit), USEC_PER_SEC),
518*4882a593Smuzhiyun 	       div_u64(ktime_to_ns(softlimit) + delta, USEC_PER_SEC));
519*4882a593Smuzhiyun 	hrtimer_start_range_ns(&wbuf->timer, softlimit, delta,
520*4882a593Smuzhiyun 			       HRTIMER_MODE_REL);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun /**
524*4882a593Smuzhiyun  * cancel_wbuf_timer - cancel write-buffer timer.
525*4882a593Smuzhiyun  * @wbuf: write-buffer descriptor
526*4882a593Smuzhiyun  */
cancel_wbuf_timer_nolock(struct ubifs_wbuf * wbuf)527*4882a593Smuzhiyun static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	if (wbuf->no_timer)
530*4882a593Smuzhiyun 		return;
531*4882a593Smuzhiyun 	wbuf->need_sync = 0;
532*4882a593Smuzhiyun 	hrtimer_cancel(&wbuf->timer);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun /**
536*4882a593Smuzhiyun  * ubifs_wbuf_sync_nolock - synchronize write-buffer.
537*4882a593Smuzhiyun  * @wbuf: write-buffer to synchronize
538*4882a593Smuzhiyun  *
539*4882a593Smuzhiyun  * This function synchronizes write-buffer @buf and returns zero in case of
540*4882a593Smuzhiyun  * success or a negative error code in case of failure.
541*4882a593Smuzhiyun  *
542*4882a593Smuzhiyun  * Note, although write-buffers are of @c->max_write_size, this function does
543*4882a593Smuzhiyun  * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
544*4882a593Smuzhiyun  * if the write-buffer is only partially filled with data, only the used part
545*4882a593Smuzhiyun  * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
546*4882a593Smuzhiyun  * This way we waste less space.
547*4882a593Smuzhiyun  */
ubifs_wbuf_sync_nolock(struct ubifs_wbuf * wbuf)548*4882a593Smuzhiyun int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	struct ubifs_info *c = wbuf->c;
551*4882a593Smuzhiyun 	int err, dirt, sync_len;
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	cancel_wbuf_timer_nolock(wbuf);
554*4882a593Smuzhiyun 	if (!wbuf->used || wbuf->lnum == -1)
555*4882a593Smuzhiyun 		/* Write-buffer is empty or not seeked */
556*4882a593Smuzhiyun 		return 0;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	dbg_io("LEB %d:%d, %d bytes, jhead %s",
559*4882a593Smuzhiyun 	       wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
560*4882a593Smuzhiyun 	ubifs_assert(c, !(wbuf->avail & 7));
561*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->offs + wbuf->size <= c->leb_size);
562*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->size >= c->min_io_size);
563*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->size <= c->max_write_size);
564*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->size % c->min_io_size == 0);
565*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
566*4882a593Smuzhiyun 	if (c->leb_size - wbuf->offs >= c->max_write_size)
567*4882a593Smuzhiyun 		ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size));
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	if (c->ro_error)
570*4882a593Smuzhiyun 		return -EROFS;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	/*
573*4882a593Smuzhiyun 	 * Do not write whole write buffer but write only the minimum necessary
574*4882a593Smuzhiyun 	 * amount of min. I/O units.
575*4882a593Smuzhiyun 	 */
576*4882a593Smuzhiyun 	sync_len = ALIGN(wbuf->used, c->min_io_size);
577*4882a593Smuzhiyun 	dirt = sync_len - wbuf->used;
578*4882a593Smuzhiyun 	if (dirt)
579*4882a593Smuzhiyun 		ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
580*4882a593Smuzhiyun 	err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len);
581*4882a593Smuzhiyun 	if (err)
582*4882a593Smuzhiyun 		return err;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	spin_lock(&wbuf->lock);
585*4882a593Smuzhiyun 	wbuf->offs += sync_len;
586*4882a593Smuzhiyun 	/*
587*4882a593Smuzhiyun 	 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
588*4882a593Smuzhiyun 	 * But our goal is to optimize writes and make sure we write in
589*4882a593Smuzhiyun 	 * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
590*4882a593Smuzhiyun 	 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
591*4882a593Smuzhiyun 	 * sure that @wbuf->offs + @wbuf->size is aligned to
592*4882a593Smuzhiyun 	 * @c->max_write_size. This way we make sure that after next
593*4882a593Smuzhiyun 	 * write-buffer flush we are again at the optimal offset (aligned to
594*4882a593Smuzhiyun 	 * @c->max_write_size).
595*4882a593Smuzhiyun 	 */
596*4882a593Smuzhiyun 	if (c->leb_size - wbuf->offs < c->max_write_size)
597*4882a593Smuzhiyun 		wbuf->size = c->leb_size - wbuf->offs;
598*4882a593Smuzhiyun 	else if (wbuf->offs & (c->max_write_size - 1))
599*4882a593Smuzhiyun 		wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
600*4882a593Smuzhiyun 	else
601*4882a593Smuzhiyun 		wbuf->size = c->max_write_size;
602*4882a593Smuzhiyun 	wbuf->avail = wbuf->size;
603*4882a593Smuzhiyun 	wbuf->used = 0;
604*4882a593Smuzhiyun 	wbuf->next_ino = 0;
605*4882a593Smuzhiyun 	spin_unlock(&wbuf->lock);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	if (wbuf->sync_callback)
608*4882a593Smuzhiyun 		err = wbuf->sync_callback(c, wbuf->lnum,
609*4882a593Smuzhiyun 					  c->leb_size - wbuf->offs, dirt);
610*4882a593Smuzhiyun 	return err;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /**
614*4882a593Smuzhiyun  * ubifs_wbuf_seek_nolock - seek write-buffer.
615*4882a593Smuzhiyun  * @wbuf: write-buffer
616*4882a593Smuzhiyun  * @lnum: logical eraseblock number to seek to
617*4882a593Smuzhiyun  * @offs: logical eraseblock offset to seek to
618*4882a593Smuzhiyun  *
619*4882a593Smuzhiyun  * This function targets the write-buffer to logical eraseblock @lnum:@offs.
620*4882a593Smuzhiyun  * The write-buffer has to be empty. Returns zero in case of success and a
621*4882a593Smuzhiyun  * negative error code in case of failure.
622*4882a593Smuzhiyun  */
ubifs_wbuf_seek_nolock(struct ubifs_wbuf * wbuf,int lnum,int offs)623*4882a593Smuzhiyun int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun 	const struct ubifs_info *c = wbuf->c;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
628*4882a593Smuzhiyun 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt);
629*4882a593Smuzhiyun 	ubifs_assert(c, offs >= 0 && offs <= c->leb_size);
630*4882a593Smuzhiyun 	ubifs_assert(c, offs % c->min_io_size == 0 && !(offs & 7));
631*4882a593Smuzhiyun 	ubifs_assert(c, lnum != wbuf->lnum);
632*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->used == 0);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	spin_lock(&wbuf->lock);
635*4882a593Smuzhiyun 	wbuf->lnum = lnum;
636*4882a593Smuzhiyun 	wbuf->offs = offs;
637*4882a593Smuzhiyun 	if (c->leb_size - wbuf->offs < c->max_write_size)
638*4882a593Smuzhiyun 		wbuf->size = c->leb_size - wbuf->offs;
639*4882a593Smuzhiyun 	else if (wbuf->offs & (c->max_write_size - 1))
640*4882a593Smuzhiyun 		wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
641*4882a593Smuzhiyun 	else
642*4882a593Smuzhiyun 		wbuf->size = c->max_write_size;
643*4882a593Smuzhiyun 	wbuf->avail = wbuf->size;
644*4882a593Smuzhiyun 	wbuf->used = 0;
645*4882a593Smuzhiyun 	spin_unlock(&wbuf->lock);
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	return 0;
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun /**
651*4882a593Smuzhiyun  * ubifs_bg_wbufs_sync - synchronize write-buffers.
652*4882a593Smuzhiyun  * @c: UBIFS file-system description object
653*4882a593Smuzhiyun  *
654*4882a593Smuzhiyun  * This function is called by background thread to synchronize write-buffers.
655*4882a593Smuzhiyun  * Returns zero in case of success and a negative error code in case of
656*4882a593Smuzhiyun  * failure.
657*4882a593Smuzhiyun  */
ubifs_bg_wbufs_sync(struct ubifs_info * c)658*4882a593Smuzhiyun int ubifs_bg_wbufs_sync(struct ubifs_info *c)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	int err, i;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
663*4882a593Smuzhiyun 	if (!c->need_wbuf_sync)
664*4882a593Smuzhiyun 		return 0;
665*4882a593Smuzhiyun 	c->need_wbuf_sync = 0;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	if (c->ro_error) {
668*4882a593Smuzhiyun 		err = -EROFS;
669*4882a593Smuzhiyun 		goto out_timers;
670*4882a593Smuzhiyun 	}
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	dbg_io("synchronize");
673*4882a593Smuzhiyun 	for (i = 0; i < c->jhead_cnt; i++) {
674*4882a593Smuzhiyun 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 		cond_resched();
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 		/*
679*4882a593Smuzhiyun 		 * If the mutex is locked then wbuf is being changed, so
680*4882a593Smuzhiyun 		 * synchronization is not necessary.
681*4882a593Smuzhiyun 		 */
682*4882a593Smuzhiyun 		if (mutex_is_locked(&wbuf->io_mutex))
683*4882a593Smuzhiyun 			continue;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
686*4882a593Smuzhiyun 		if (!wbuf->need_sync) {
687*4882a593Smuzhiyun 			mutex_unlock(&wbuf->io_mutex);
688*4882a593Smuzhiyun 			continue;
689*4882a593Smuzhiyun 		}
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 		err = ubifs_wbuf_sync_nolock(wbuf);
692*4882a593Smuzhiyun 		mutex_unlock(&wbuf->io_mutex);
693*4882a593Smuzhiyun 		if (err) {
694*4882a593Smuzhiyun 			ubifs_err(c, "cannot sync write-buffer, error %d", err);
695*4882a593Smuzhiyun 			ubifs_ro_mode(c, err);
696*4882a593Smuzhiyun 			goto out_timers;
697*4882a593Smuzhiyun 		}
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	return 0;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun out_timers:
703*4882a593Smuzhiyun 	/* Cancel all timers to prevent repeated errors */
704*4882a593Smuzhiyun 	for (i = 0; i < c->jhead_cnt; i++) {
705*4882a593Smuzhiyun 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
708*4882a593Smuzhiyun 		cancel_wbuf_timer_nolock(wbuf);
709*4882a593Smuzhiyun 		mutex_unlock(&wbuf->io_mutex);
710*4882a593Smuzhiyun 	}
711*4882a593Smuzhiyun 	return err;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /**
715*4882a593Smuzhiyun  * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
716*4882a593Smuzhiyun  * @wbuf: write-buffer
717*4882a593Smuzhiyun  * @buf: node to write
718*4882a593Smuzhiyun  * @len: node length
719*4882a593Smuzhiyun  *
720*4882a593Smuzhiyun  * This function writes data to flash via write-buffer @wbuf. This means that
721*4882a593Smuzhiyun  * the last piece of the node won't reach the flash media immediately if it
722*4882a593Smuzhiyun  * does not take whole max. write unit (@c->max_write_size). Instead, the node
723*4882a593Smuzhiyun  * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
724*4882a593Smuzhiyun  * because more data are appended to the write-buffer).
725*4882a593Smuzhiyun  *
726*4882a593Smuzhiyun  * This function returns zero in case of success and a negative error code in
727*4882a593Smuzhiyun  * case of failure. If the node cannot be written because there is no more
728*4882a593Smuzhiyun  * space in this logical eraseblock, %-ENOSPC is returned.
729*4882a593Smuzhiyun  */
ubifs_wbuf_write_nolock(struct ubifs_wbuf * wbuf,void * buf,int len)730*4882a593Smuzhiyun int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	struct ubifs_info *c = wbuf->c;
733*4882a593Smuzhiyun 	int err, written, n, aligned_len = ALIGN(len, 8);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
736*4882a593Smuzhiyun 	       dbg_ntype(((struct ubifs_ch *)buf)->node_type),
737*4882a593Smuzhiyun 	       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
738*4882a593Smuzhiyun 	ubifs_assert(c, len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
739*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
740*4882a593Smuzhiyun 	ubifs_assert(c, !(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
741*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->avail > 0 && wbuf->avail <= wbuf->size);
742*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->size >= c->min_io_size);
743*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->size <= c->max_write_size);
744*4882a593Smuzhiyun 	ubifs_assert(c, wbuf->size % c->min_io_size == 0);
745*4882a593Smuzhiyun 	ubifs_assert(c, mutex_is_locked(&wbuf->io_mutex));
746*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
747*4882a593Smuzhiyun 	ubifs_assert(c, !c->space_fixup);
748*4882a593Smuzhiyun 	if (c->leb_size - wbuf->offs >= c->max_write_size)
749*4882a593Smuzhiyun 		ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size));
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
752*4882a593Smuzhiyun 		err = -ENOSPC;
753*4882a593Smuzhiyun 		goto out;
754*4882a593Smuzhiyun 	}
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	cancel_wbuf_timer_nolock(wbuf);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	if (c->ro_error)
759*4882a593Smuzhiyun 		return -EROFS;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	if (aligned_len <= wbuf->avail) {
762*4882a593Smuzhiyun 		/*
763*4882a593Smuzhiyun 		 * The node is not very large and fits entirely within
764*4882a593Smuzhiyun 		 * write-buffer.
765*4882a593Smuzhiyun 		 */
766*4882a593Smuzhiyun 		memcpy(wbuf->buf + wbuf->used, buf, len);
767*4882a593Smuzhiyun 		if (aligned_len > len) {
768*4882a593Smuzhiyun 			ubifs_assert(c, aligned_len - len < 8);
769*4882a593Smuzhiyun 			ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len);
770*4882a593Smuzhiyun 		}
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		if (aligned_len == wbuf->avail) {
773*4882a593Smuzhiyun 			dbg_io("flush jhead %s wbuf to LEB %d:%d",
774*4882a593Smuzhiyun 			       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
775*4882a593Smuzhiyun 			err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf,
776*4882a593Smuzhiyun 					      wbuf->offs, wbuf->size);
777*4882a593Smuzhiyun 			if (err)
778*4882a593Smuzhiyun 				goto out;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 			spin_lock(&wbuf->lock);
781*4882a593Smuzhiyun 			wbuf->offs += wbuf->size;
782*4882a593Smuzhiyun 			if (c->leb_size - wbuf->offs >= c->max_write_size)
783*4882a593Smuzhiyun 				wbuf->size = c->max_write_size;
784*4882a593Smuzhiyun 			else
785*4882a593Smuzhiyun 				wbuf->size = c->leb_size - wbuf->offs;
786*4882a593Smuzhiyun 			wbuf->avail = wbuf->size;
787*4882a593Smuzhiyun 			wbuf->used = 0;
788*4882a593Smuzhiyun 			wbuf->next_ino = 0;
789*4882a593Smuzhiyun 			spin_unlock(&wbuf->lock);
790*4882a593Smuzhiyun 		} else {
791*4882a593Smuzhiyun 			spin_lock(&wbuf->lock);
792*4882a593Smuzhiyun 			wbuf->avail -= aligned_len;
793*4882a593Smuzhiyun 			wbuf->used += aligned_len;
794*4882a593Smuzhiyun 			spin_unlock(&wbuf->lock);
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		goto exit;
798*4882a593Smuzhiyun 	}
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	written = 0;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	if (wbuf->used) {
803*4882a593Smuzhiyun 		/*
804*4882a593Smuzhiyun 		 * The node is large enough and does not fit entirely within
805*4882a593Smuzhiyun 		 * current available space. We have to fill and flush
806*4882a593Smuzhiyun 		 * write-buffer and switch to the next max. write unit.
807*4882a593Smuzhiyun 		 */
808*4882a593Smuzhiyun 		dbg_io("flush jhead %s wbuf to LEB %d:%d",
809*4882a593Smuzhiyun 		       dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
810*4882a593Smuzhiyun 		memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
811*4882a593Smuzhiyun 		err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs,
812*4882a593Smuzhiyun 				      wbuf->size);
813*4882a593Smuzhiyun 		if (err)
814*4882a593Smuzhiyun 			goto out;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 		wbuf->offs += wbuf->size;
817*4882a593Smuzhiyun 		len -= wbuf->avail;
818*4882a593Smuzhiyun 		aligned_len -= wbuf->avail;
819*4882a593Smuzhiyun 		written += wbuf->avail;
820*4882a593Smuzhiyun 	} else if (wbuf->offs & (c->max_write_size - 1)) {
821*4882a593Smuzhiyun 		/*
822*4882a593Smuzhiyun 		 * The write-buffer offset is not aligned to
823*4882a593Smuzhiyun 		 * @c->max_write_size and @wbuf->size is less than
824*4882a593Smuzhiyun 		 * @c->max_write_size. Write @wbuf->size bytes to make sure the
825*4882a593Smuzhiyun 		 * following writes are done in optimal @c->max_write_size
826*4882a593Smuzhiyun 		 * chunks.
827*4882a593Smuzhiyun 		 */
828*4882a593Smuzhiyun 		dbg_io("write %d bytes to LEB %d:%d",
829*4882a593Smuzhiyun 		       wbuf->size, wbuf->lnum, wbuf->offs);
830*4882a593Smuzhiyun 		err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs,
831*4882a593Smuzhiyun 				      wbuf->size);
832*4882a593Smuzhiyun 		if (err)
833*4882a593Smuzhiyun 			goto out;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 		wbuf->offs += wbuf->size;
836*4882a593Smuzhiyun 		len -= wbuf->size;
837*4882a593Smuzhiyun 		aligned_len -= wbuf->size;
838*4882a593Smuzhiyun 		written += wbuf->size;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	/*
842*4882a593Smuzhiyun 	 * The remaining data may take more whole max. write units, so write the
843*4882a593Smuzhiyun 	 * remains multiple to max. write unit size directly to the flash media.
844*4882a593Smuzhiyun 	 * We align node length to 8-byte boundary because we anyway flash wbuf
845*4882a593Smuzhiyun 	 * if the remaining space is less than 8 bytes.
846*4882a593Smuzhiyun 	 */
847*4882a593Smuzhiyun 	n = aligned_len >> c->max_write_shift;
848*4882a593Smuzhiyun 	if (n) {
849*4882a593Smuzhiyun 		int m = n - 1;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 		dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
852*4882a593Smuzhiyun 		       wbuf->offs);
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 		if (m) {
855*4882a593Smuzhiyun 			/* '(n-1)<<c->max_write_shift < len' is always true. */
856*4882a593Smuzhiyun 			m <<= c->max_write_shift;
857*4882a593Smuzhiyun 			err = ubifs_leb_write(c, wbuf->lnum, buf + written,
858*4882a593Smuzhiyun 					      wbuf->offs, m);
859*4882a593Smuzhiyun 			if (err)
860*4882a593Smuzhiyun 				goto out;
861*4882a593Smuzhiyun 			wbuf->offs += m;
862*4882a593Smuzhiyun 			aligned_len -= m;
863*4882a593Smuzhiyun 			len -= m;
864*4882a593Smuzhiyun 			written += m;
865*4882a593Smuzhiyun 		}
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 		/*
868*4882a593Smuzhiyun 		 * The non-written len of buf may be less than 'n' because
869*4882a593Smuzhiyun 		 * parameter 'len' is not 8 bytes aligned, so here we read
870*4882a593Smuzhiyun 		 * min(len, n) bytes from buf.
871*4882a593Smuzhiyun 		 */
872*4882a593Smuzhiyun 		n = 1 << c->max_write_shift;
873*4882a593Smuzhiyun 		memcpy(wbuf->buf, buf + written, min(len, n));
874*4882a593Smuzhiyun 		if (n > len) {
875*4882a593Smuzhiyun 			ubifs_assert(c, n - len < 8);
876*4882a593Smuzhiyun 			ubifs_pad(c, wbuf->buf + len, n - len);
877*4882a593Smuzhiyun 		}
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 		err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n);
880*4882a593Smuzhiyun 		if (err)
881*4882a593Smuzhiyun 			goto out;
882*4882a593Smuzhiyun 		wbuf->offs += n;
883*4882a593Smuzhiyun 		aligned_len -= n;
884*4882a593Smuzhiyun 		len -= min(len, n);
885*4882a593Smuzhiyun 		written += n;
886*4882a593Smuzhiyun 	}
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	spin_lock(&wbuf->lock);
889*4882a593Smuzhiyun 	if (aligned_len) {
890*4882a593Smuzhiyun 		/*
891*4882a593Smuzhiyun 		 * And now we have what's left and what does not take whole
892*4882a593Smuzhiyun 		 * max. write unit, so write it to the write-buffer and we are
893*4882a593Smuzhiyun 		 * done.
894*4882a593Smuzhiyun 		 */
895*4882a593Smuzhiyun 		memcpy(wbuf->buf, buf + written, len);
896*4882a593Smuzhiyun 		if (aligned_len > len) {
897*4882a593Smuzhiyun 			ubifs_assert(c, aligned_len - len < 8);
898*4882a593Smuzhiyun 			ubifs_pad(c, wbuf->buf + len, aligned_len - len);
899*4882a593Smuzhiyun 		}
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	if (c->leb_size - wbuf->offs >= c->max_write_size)
903*4882a593Smuzhiyun 		wbuf->size = c->max_write_size;
904*4882a593Smuzhiyun 	else
905*4882a593Smuzhiyun 		wbuf->size = c->leb_size - wbuf->offs;
906*4882a593Smuzhiyun 	wbuf->avail = wbuf->size - aligned_len;
907*4882a593Smuzhiyun 	wbuf->used = aligned_len;
908*4882a593Smuzhiyun 	wbuf->next_ino = 0;
909*4882a593Smuzhiyun 	spin_unlock(&wbuf->lock);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun exit:
912*4882a593Smuzhiyun 	if (wbuf->sync_callback) {
913*4882a593Smuzhiyun 		int free = c->leb_size - wbuf->offs - wbuf->used;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 		err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
916*4882a593Smuzhiyun 		if (err)
917*4882a593Smuzhiyun 			goto out;
918*4882a593Smuzhiyun 	}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	if (wbuf->used)
921*4882a593Smuzhiyun 		new_wbuf_timer_nolock(c, wbuf);
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	return 0;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun out:
926*4882a593Smuzhiyun 	ubifs_err(c, "cannot write %d bytes to LEB %d:%d, error %d",
927*4882a593Smuzhiyun 		  len, wbuf->lnum, wbuf->offs, err);
928*4882a593Smuzhiyun 	ubifs_dump_node(c, buf);
929*4882a593Smuzhiyun 	dump_stack();
930*4882a593Smuzhiyun 	ubifs_dump_leb(c, wbuf->lnum);
931*4882a593Smuzhiyun 	return err;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun /**
935*4882a593Smuzhiyun  * ubifs_write_node_hmac - write node to the media.
936*4882a593Smuzhiyun  * @c: UBIFS file-system description object
937*4882a593Smuzhiyun  * @buf: the node to write
938*4882a593Smuzhiyun  * @len: node length
939*4882a593Smuzhiyun  * @lnum: logical eraseblock number
940*4882a593Smuzhiyun  * @offs: offset within the logical eraseblock
941*4882a593Smuzhiyun  * @hmac_offs: offset of the HMAC within the node
942*4882a593Smuzhiyun  *
943*4882a593Smuzhiyun  * This function automatically fills node magic number, assigns sequence
944*4882a593Smuzhiyun  * number, and calculates node CRC checksum. The length of the @buf buffer has
945*4882a593Smuzhiyun  * to be aligned to the minimal I/O unit size. This function automatically
946*4882a593Smuzhiyun  * appends padding node and padding bytes if needed. Returns zero in case of
947*4882a593Smuzhiyun  * success and a negative error code in case of failure.
948*4882a593Smuzhiyun  */
ubifs_write_node_hmac(struct ubifs_info * c,void * buf,int len,int lnum,int offs,int hmac_offs)949*4882a593Smuzhiyun int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
950*4882a593Smuzhiyun 			  int offs, int hmac_offs)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun 	int err, buf_len = ALIGN(len, c->min_io_size);
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
955*4882a593Smuzhiyun 	       lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
956*4882a593Smuzhiyun 	       buf_len);
957*4882a593Smuzhiyun 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
958*4882a593Smuzhiyun 	ubifs_assert(c, offs % c->min_io_size == 0 && offs < c->leb_size);
959*4882a593Smuzhiyun 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
960*4882a593Smuzhiyun 	ubifs_assert(c, !c->space_fixup);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	if (c->ro_error)
963*4882a593Smuzhiyun 		return -EROFS;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	err = ubifs_prepare_node_hmac(c, buf, len, hmac_offs, 1);
966*4882a593Smuzhiyun 	if (err)
967*4882a593Smuzhiyun 		return err;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	err = ubifs_leb_write(c, lnum, buf, offs, buf_len);
970*4882a593Smuzhiyun 	if (err)
971*4882a593Smuzhiyun 		ubifs_dump_node(c, buf);
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	return err;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun /**
977*4882a593Smuzhiyun  * ubifs_write_node - write node to the media.
978*4882a593Smuzhiyun  * @c: UBIFS file-system description object
979*4882a593Smuzhiyun  * @buf: the node to write
980*4882a593Smuzhiyun  * @len: node length
981*4882a593Smuzhiyun  * @lnum: logical eraseblock number
982*4882a593Smuzhiyun  * @offs: offset within the logical eraseblock
983*4882a593Smuzhiyun  *
984*4882a593Smuzhiyun  * This function automatically fills node magic number, assigns sequence
985*4882a593Smuzhiyun  * number, and calculates node CRC checksum. The length of the @buf buffer has
986*4882a593Smuzhiyun  * to be aligned to the minimal I/O unit size. This function automatically
987*4882a593Smuzhiyun  * appends padding node and padding bytes if needed. Returns zero in case of
988*4882a593Smuzhiyun  * success and a negative error code in case of failure.
989*4882a593Smuzhiyun  */
ubifs_write_node(struct ubifs_info * c,void * buf,int len,int lnum,int offs)990*4882a593Smuzhiyun int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
991*4882a593Smuzhiyun 		     int offs)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun 	return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1);
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun /**
997*4882a593Smuzhiyun  * ubifs_read_node_wbuf - read node from the media or write-buffer.
998*4882a593Smuzhiyun  * @wbuf: wbuf to check for un-written data
999*4882a593Smuzhiyun  * @buf: buffer to read to
1000*4882a593Smuzhiyun  * @type: node type
1001*4882a593Smuzhiyun  * @len: node length
1002*4882a593Smuzhiyun  * @lnum: logical eraseblock number
1003*4882a593Smuzhiyun  * @offs: offset within the logical eraseblock
1004*4882a593Smuzhiyun  *
1005*4882a593Smuzhiyun  * This function reads a node of known type and length, checks it and stores
1006*4882a593Smuzhiyun  * in @buf. If the node partially or fully sits in the write-buffer, this
1007*4882a593Smuzhiyun  * function takes data from the buffer, otherwise it reads the flash media.
1008*4882a593Smuzhiyun  * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
1009*4882a593Smuzhiyun  * error code in case of failure.
1010*4882a593Smuzhiyun  */
ubifs_read_node_wbuf(struct ubifs_wbuf * wbuf,void * buf,int type,int len,int lnum,int offs)1011*4882a593Smuzhiyun int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
1012*4882a593Smuzhiyun 			 int lnum, int offs)
1013*4882a593Smuzhiyun {
1014*4882a593Smuzhiyun 	const struct ubifs_info *c = wbuf->c;
1015*4882a593Smuzhiyun 	int err, rlen, overlap;
1016*4882a593Smuzhiyun 	struct ubifs_ch *ch = buf;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
1019*4882a593Smuzhiyun 	       dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
1020*4882a593Smuzhiyun 	ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1021*4882a593Smuzhiyun 	ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
1022*4882a593Smuzhiyun 	ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	spin_lock(&wbuf->lock);
1025*4882a593Smuzhiyun 	overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
1026*4882a593Smuzhiyun 	if (!overlap) {
1027*4882a593Smuzhiyun 		/* We may safely unlock the write-buffer and read the data */
1028*4882a593Smuzhiyun 		spin_unlock(&wbuf->lock);
1029*4882a593Smuzhiyun 		return ubifs_read_node(c, buf, type, len, lnum, offs);
1030*4882a593Smuzhiyun 	}
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 	/* Don't read under wbuf */
1033*4882a593Smuzhiyun 	rlen = wbuf->offs - offs;
1034*4882a593Smuzhiyun 	if (rlen < 0)
1035*4882a593Smuzhiyun 		rlen = 0;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	/* Copy the rest from the write-buffer */
1038*4882a593Smuzhiyun 	memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
1039*4882a593Smuzhiyun 	spin_unlock(&wbuf->lock);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	if (rlen > 0) {
1042*4882a593Smuzhiyun 		/* Read everything that goes before write-buffer */
1043*4882a593Smuzhiyun 		err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
1044*4882a593Smuzhiyun 		if (err && err != -EBADMSG)
1045*4882a593Smuzhiyun 			return err;
1046*4882a593Smuzhiyun 	}
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	if (type != ch->node_type) {
1049*4882a593Smuzhiyun 		ubifs_err(c, "bad node type (%d but expected %d)",
1050*4882a593Smuzhiyun 			  ch->node_type, type);
1051*4882a593Smuzhiyun 		goto out;
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
1055*4882a593Smuzhiyun 	if (err) {
1056*4882a593Smuzhiyun 		ubifs_err(c, "expected node type %d", type);
1057*4882a593Smuzhiyun 		return err;
1058*4882a593Smuzhiyun 	}
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	rlen = le32_to_cpu(ch->len);
1061*4882a593Smuzhiyun 	if (rlen != len) {
1062*4882a593Smuzhiyun 		ubifs_err(c, "bad node length %d, expected %d", rlen, len);
1063*4882a593Smuzhiyun 		goto out;
1064*4882a593Smuzhiyun 	}
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	return 0;
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun out:
1069*4882a593Smuzhiyun 	ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
1070*4882a593Smuzhiyun 	ubifs_dump_node(c, buf);
1071*4882a593Smuzhiyun 	dump_stack();
1072*4882a593Smuzhiyun 	return -EINVAL;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun /**
1076*4882a593Smuzhiyun  * ubifs_read_node - read node.
1077*4882a593Smuzhiyun  * @c: UBIFS file-system description object
1078*4882a593Smuzhiyun  * @buf: buffer to read to
1079*4882a593Smuzhiyun  * @type: node type
1080*4882a593Smuzhiyun  * @len: node length (not aligned)
1081*4882a593Smuzhiyun  * @lnum: logical eraseblock number
1082*4882a593Smuzhiyun  * @offs: offset within the logical eraseblock
1083*4882a593Smuzhiyun  *
1084*4882a593Smuzhiyun  * This function reads a node of known type and and length, checks it and
1085*4882a593Smuzhiyun  * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
1086*4882a593Smuzhiyun  * and a negative error code in case of failure.
1087*4882a593Smuzhiyun  */
ubifs_read_node(const struct ubifs_info * c,void * buf,int type,int len,int lnum,int offs)1088*4882a593Smuzhiyun int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
1089*4882a593Smuzhiyun 		    int lnum, int offs)
1090*4882a593Smuzhiyun {
1091*4882a593Smuzhiyun 	int err, l;
1092*4882a593Smuzhiyun 	struct ubifs_ch *ch = buf;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
1095*4882a593Smuzhiyun 	ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1096*4882a593Smuzhiyun 	ubifs_assert(c, len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
1097*4882a593Smuzhiyun 	ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
1098*4882a593Smuzhiyun 	ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT);
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
1101*4882a593Smuzhiyun 	if (err && err != -EBADMSG)
1102*4882a593Smuzhiyun 		return err;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	if (type != ch->node_type) {
1105*4882a593Smuzhiyun 		ubifs_errc(c, "bad node type (%d but expected %d)",
1106*4882a593Smuzhiyun 			   ch->node_type, type);
1107*4882a593Smuzhiyun 		goto out;
1108*4882a593Smuzhiyun 	}
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
1111*4882a593Smuzhiyun 	if (err) {
1112*4882a593Smuzhiyun 		ubifs_errc(c, "expected node type %d", type);
1113*4882a593Smuzhiyun 		return err;
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	l = le32_to_cpu(ch->len);
1117*4882a593Smuzhiyun 	if (l != len) {
1118*4882a593Smuzhiyun 		ubifs_errc(c, "bad node length %d, expected %d", l, len);
1119*4882a593Smuzhiyun 		goto out;
1120*4882a593Smuzhiyun 	}
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	return 0;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun out:
1125*4882a593Smuzhiyun 	ubifs_errc(c, "bad node at LEB %d:%d, LEB mapping status %d", lnum,
1126*4882a593Smuzhiyun 		   offs, ubi_is_mapped(c->ubi, lnum));
1127*4882a593Smuzhiyun 	if (!c->probing) {
1128*4882a593Smuzhiyun 		ubifs_dump_node(c, buf);
1129*4882a593Smuzhiyun 		dump_stack();
1130*4882a593Smuzhiyun 	}
1131*4882a593Smuzhiyun 	return -EINVAL;
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun /**
1135*4882a593Smuzhiyun  * ubifs_wbuf_init - initialize write-buffer.
1136*4882a593Smuzhiyun  * @c: UBIFS file-system description object
1137*4882a593Smuzhiyun  * @wbuf: write-buffer to initialize
1138*4882a593Smuzhiyun  *
1139*4882a593Smuzhiyun  * This function initializes write-buffer. Returns zero in case of success
1140*4882a593Smuzhiyun  * %-ENOMEM in case of failure.
1141*4882a593Smuzhiyun  */
ubifs_wbuf_init(struct ubifs_info * c,struct ubifs_wbuf * wbuf)1142*4882a593Smuzhiyun int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
1143*4882a593Smuzhiyun {
1144*4882a593Smuzhiyun 	size_t size;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
1147*4882a593Smuzhiyun 	if (!wbuf->buf)
1148*4882a593Smuzhiyun 		return -ENOMEM;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
1151*4882a593Smuzhiyun 	wbuf->inodes = kmalloc(size, GFP_KERNEL);
1152*4882a593Smuzhiyun 	if (!wbuf->inodes) {
1153*4882a593Smuzhiyun 		kfree(wbuf->buf);
1154*4882a593Smuzhiyun 		wbuf->buf = NULL;
1155*4882a593Smuzhiyun 		return -ENOMEM;
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	wbuf->used = 0;
1159*4882a593Smuzhiyun 	wbuf->lnum = wbuf->offs = -1;
1160*4882a593Smuzhiyun 	/*
1161*4882a593Smuzhiyun 	 * If the LEB starts at the max. write size aligned address, then
1162*4882a593Smuzhiyun 	 * write-buffer size has to be set to @c->max_write_size. Otherwise,
1163*4882a593Smuzhiyun 	 * set it to something smaller so that it ends at the closest max.
1164*4882a593Smuzhiyun 	 * write size boundary.
1165*4882a593Smuzhiyun 	 */
1166*4882a593Smuzhiyun 	size = c->max_write_size - (c->leb_start % c->max_write_size);
1167*4882a593Smuzhiyun 	wbuf->avail = wbuf->size = size;
1168*4882a593Smuzhiyun 	wbuf->sync_callback = NULL;
1169*4882a593Smuzhiyun 	mutex_init(&wbuf->io_mutex);
1170*4882a593Smuzhiyun 	spin_lock_init(&wbuf->lock);
1171*4882a593Smuzhiyun 	wbuf->c = c;
1172*4882a593Smuzhiyun 	wbuf->next_ino = 0;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1175*4882a593Smuzhiyun 	wbuf->timer.function = wbuf_timer_callback_nolock;
1176*4882a593Smuzhiyun 	return 0;
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun /**
1180*4882a593Smuzhiyun  * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
1181*4882a593Smuzhiyun  * @wbuf: the write-buffer where to add
1182*4882a593Smuzhiyun  * @inum: the inode number
1183*4882a593Smuzhiyun  *
1184*4882a593Smuzhiyun  * This function adds an inode number to the inode array of the write-buffer.
1185*4882a593Smuzhiyun  */
ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf * wbuf,ino_t inum)1186*4882a593Smuzhiyun void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun 	if (!wbuf->buf)
1189*4882a593Smuzhiyun 		/* NOR flash or something similar */
1190*4882a593Smuzhiyun 		return;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	spin_lock(&wbuf->lock);
1193*4882a593Smuzhiyun 	if (wbuf->used)
1194*4882a593Smuzhiyun 		wbuf->inodes[wbuf->next_ino++] = inum;
1195*4882a593Smuzhiyun 	spin_unlock(&wbuf->lock);
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun /**
1199*4882a593Smuzhiyun  * wbuf_has_ino - returns if the wbuf contains data from the inode.
1200*4882a593Smuzhiyun  * @wbuf: the write-buffer
1201*4882a593Smuzhiyun  * @inum: the inode number
1202*4882a593Smuzhiyun  *
1203*4882a593Smuzhiyun  * This function returns with %1 if the write-buffer contains some data from the
1204*4882a593Smuzhiyun  * given inode otherwise it returns with %0.
1205*4882a593Smuzhiyun  */
wbuf_has_ino(struct ubifs_wbuf * wbuf,ino_t inum)1206*4882a593Smuzhiyun static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	int i, ret = 0;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	spin_lock(&wbuf->lock);
1211*4882a593Smuzhiyun 	for (i = 0; i < wbuf->next_ino; i++)
1212*4882a593Smuzhiyun 		if (inum == wbuf->inodes[i]) {
1213*4882a593Smuzhiyun 			ret = 1;
1214*4882a593Smuzhiyun 			break;
1215*4882a593Smuzhiyun 		}
1216*4882a593Smuzhiyun 	spin_unlock(&wbuf->lock);
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	return ret;
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun /**
1222*4882a593Smuzhiyun  * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
1223*4882a593Smuzhiyun  * @c: UBIFS file-system description object
1224*4882a593Smuzhiyun  * @inode: inode to synchronize
1225*4882a593Smuzhiyun  *
1226*4882a593Smuzhiyun  * This function synchronizes write-buffers which contain nodes belonging to
1227*4882a593Smuzhiyun  * @inode. Returns zero in case of success and a negative error code in case of
1228*4882a593Smuzhiyun  * failure.
1229*4882a593Smuzhiyun  */
ubifs_sync_wbufs_by_inode(struct ubifs_info * c,struct inode * inode)1230*4882a593Smuzhiyun int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun 	int i, err = 0;
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	for (i = 0; i < c->jhead_cnt; i++) {
1235*4882a593Smuzhiyun 		struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 		if (i == GCHD)
1238*4882a593Smuzhiyun 			/*
1239*4882a593Smuzhiyun 			 * GC head is special, do not look at it. Even if the
1240*4882a593Smuzhiyun 			 * head contains something related to this inode, it is
1241*4882a593Smuzhiyun 			 * a _copy_ of corresponding on-flash node which sits
1242*4882a593Smuzhiyun 			 * somewhere else.
1243*4882a593Smuzhiyun 			 */
1244*4882a593Smuzhiyun 			continue;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 		if (!wbuf_has_ino(wbuf, inode->i_ino))
1247*4882a593Smuzhiyun 			continue;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 		mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1250*4882a593Smuzhiyun 		if (wbuf_has_ino(wbuf, inode->i_ino))
1251*4882a593Smuzhiyun 			err = ubifs_wbuf_sync_nolock(wbuf);
1252*4882a593Smuzhiyun 		mutex_unlock(&wbuf->io_mutex);
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 		if (err) {
1255*4882a593Smuzhiyun 			ubifs_ro_mode(c, err);
1256*4882a593Smuzhiyun 			return err;
1257*4882a593Smuzhiyun 		}
1258*4882a593Smuzhiyun 	}
1259*4882a593Smuzhiyun 	return 0;
1260*4882a593Smuzhiyun }
1261