Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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