// SPDX-License-Identifier: GPL-2.0-only /* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation. * Copyright (C) 2006, 2007 University of Szeged, Hungary * * Authors: Artem Bityutskiy (Битюцкий Артём) * Adrian Hunter * Zoltan Sogor
*/
/* * This file implements UBIFS I/O subsystem which provides various I/O-related * helper functions (reading/writing/checking/validating nodes) and implements * write-buffering support. Write buffers help to save space which otherwise * would have been wasted for padding to the nearest minimal I/O unit boundary. * Instead, data first goes to the write-buffer and is flushed when the * buffer is full or when it is not used for some time (by timer). This is * similar to the mechanism is used by JFFS2. * * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum * write size (@c->max_write_size). The latter is the maximum amount of bytes * the underlying flash is able to program at a time, and writing in * @c->max_write_size units should presumably be faster. Obviously, * @c->min_io_size <= @c->max_write_size. Write-buffers are of * @c->max_write_size bytes in size for maximum performance. However, when a * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size * boundary) which contains data is written, not the whole write-buffer, * because this is more space-efficient. * * This optimization adds few complications to the code. Indeed, on the one * hand, we want to write in optimal @c->max_write_size bytes chunks, which * also means aligning writes at the @c->max_write_size bytes offsets. On the * other hand, we do not want to waste space when synchronizing the write * buffer, so during synchronization we writes in smaller chunks. And this makes * the next write offset to be not aligned to @c->max_write_size bytes. So the * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned * to @c->max_write_size bytes again. We do this by temporarily shrinking * write-buffer size (@wbuf->size). * * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by * mutexes defined inside these objects. Since sometimes upper-level code * has to lock the write-buffer (e.g. journal space reservation code), many * functions related to write-buffers have "nolock" suffix which means that the * caller has to lock the write-buffer before calling this function. * * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not * aligned, UBIFS starts the next node from the aligned address, and the padded * bytes may contain any rubbish. In other words, UBIFS does not put padding * bytes in those small gaps. Common headers of nodes store real node lengths, * not aligned lengths. Indexing nodes also store real lengths in branches. * * UBIFS uses padding when it pads to the next min. I/O unit. In this case it * uses padding nodes or padding bytes, if the padding node does not fit. * * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when * they are read from the flash media.
*/
/** * ubifs_ro_mode - switch UBIFS to read read-only mode. * @c: UBIFS file-system description object * @err: error code which is the reason of switching to R/O mode
*/ void ubifs_ro_mode(struct ubifs_info *c, int err)
{ if (!c->ro_error) {
c->ro_error = 1;
c->no_chk_data_crc = 0;
c->vfs_sb->s_flags |= SB_RDONLY;
ubifs_warn(c, "switched to read-only mode, error %d", err);
dump_stack();
}
}
/* * Below are simple wrappers over UBI I/O functions which include some * additional checks and UBIFS debugging stuff. See corresponding UBI function * for more information.
*/
int ubifs_leb_read(conststruct ubifs_info *c, int lnum, void *buf, int offs, int len, int even_ebadmsg)
{ int err;
err = ubi_read(c->ubi, lnum, buf, offs, len); /* * In case of %-EBADMSG print the error message only if the * @even_ebadmsg is true.
*/ if (err && (err != -EBADMSG || even_ebadmsg)) {
ubifs_err(c, "reading %d bytes from LEB %d:%d failed, error %d",
len, lnum, offs, err);
dump_stack();
} return err;
}
int ubifs_leb_write(struct ubifs_info *c, int lnum, constvoid *buf, int offs, int len)
{ int err;
staticvoid record_magic_error(struct ubifs_stats_info *stats)
{ if (stats)
stats->magic_errors++;
}
staticvoid record_node_error(struct ubifs_stats_info *stats)
{ if (stats)
stats->node_errors++;
}
staticvoid record_crc_error(struct ubifs_stats_info *stats)
{ if (stats)
stats->crc_errors++;
}
/** * ubifs_check_node - check node. * @c: UBIFS file-system description object * @buf: node to check * @len: node length * @lnum: logical eraseblock number * @offs: offset within the logical eraseblock * @quiet: print no messages * @must_chk_crc: indicates whether to always check the CRC * * This function checks node magic number and CRC checksum. This function also * validates node length to prevent UBIFS from becoming crazy when an attacker * feeds it a file-system image with incorrect nodes. For example, too large * node length in the common header could cause UBIFS to read memory outside of * allocated buffer when checking the CRC checksum. * * This function may skip data nodes CRC checking if @c->no_chk_data_crc is * true, which is controlled by corresponding UBIFS mount option. However, if * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC * is checked. This is because during mounting or re-mounting from R/O mode to * R/W mode we may read journal nodes (when replying the journal or doing the * recovery) and the journal nodes may potentially be corrupted, so checking is * required. * * This function returns zero in case of success and %-EUCLEAN in case of bad * CRC or magic.
*/ int ubifs_check_node(conststruct ubifs_info *c, constvoid *buf, int len, int lnum, int offs, int quiet, int must_chk_crc)
{ int err = -EINVAL, type, node_len;
uint32_t crc, node_crc, magic; conststruct ubifs_ch *ch = buf;
type = ch->node_type; if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) { if (!quiet)
ubifs_err(c, "bad node type %d", type);
record_node_error(c->stats); goto out;
}
out_len: if (!quiet)
ubifs_err(c, "bad node length %d", node_len);
out: if (!quiet) {
ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
ubifs_dump_node(c, buf, len);
dump_stack();
} return err;
}
/** * ubifs_pad - pad flash space. * @c: UBIFS file-system description object * @buf: buffer to put padding to * @pad: how many bytes to pad * * The flash media obliges us to write only in chunks of %c->min_io_size and * when we have to write less data we add padding node to the write-buffer and * pad it to the next minimal I/O unit's boundary. Padding nodes help when the * media is being scanned. If the amount of wasted space is not enough to fit a * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes * pattern (%UBIFS_PADDING_BYTE). * * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is * used.
*/ void ubifs_pad(conststruct ubifs_info *c, void *buf, int pad)
{
uint32_t crc;
if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) { if (sqnum >= SQNUM_WATERMARK) {
ubifs_err(c, "sequence number overflow %llu, end of life",
sqnum);
ubifs_ro_mode(c, -EINVAL);
}
ubifs_warn(c, "running out of sequence numbers, end of life soon");
}
return sqnum;
}
void ubifs_init_node(struct ubifs_info *c, void *node, int len, int pad)
{ struct ubifs_ch *ch = node; unsignedlonglong sqnum = next_sqnum(c);
/** * ubifs_prepare_node_hmac - prepare node to be written to flash. * @c: UBIFS file-system description object * @node: the node to pad * @len: node length * @hmac_offs: offset of the HMAC in the node * @pad: if the buffer has to be padded * * This function prepares node at @node to be written to the media - it * calculates node CRC, fills the common header, and adds proper padding up to * the next minimum I/O unit if @pad is not zero. if @hmac_offs is positive then * a HMAC is inserted into the node at the given offset. * * This function returns 0 for success or a negative error code otherwise.
*/ int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len, int hmac_offs, int pad)
{ int err;
ubifs_init_node(c, node, len, pad);
if (hmac_offs > 0) {
err = ubifs_node_insert_hmac(c, node, len, hmac_offs); if (err) return err;
}
ubifs_crc_node(c, node, len);
return 0;
}
/** * ubifs_prepare_node - prepare node to be written to flash. * @c: UBIFS file-system description object * @node: the node to pad * @len: node length * @pad: if the buffer has to be padded * * This function prepares node at @node to be written to the media - it * calculates node CRC, fills the common header, and adds proper padding up to * the next minimum I/O unit if @pad is not zero.
*/ void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
{ /* * Deliberately ignore return value since this function can only fail * when a hmac offset is given.
*/
ubifs_prepare_node_hmac(c, node, len, 0, pad);
}
/** * ubifs_prep_grp_node - prepare node of a group to be written to flash. * @c: UBIFS file-system description object * @node: the node to pad * @len: node length * @last: indicates the last node of the group * * This function prepares node at @node to be written to the media - it * calculates node CRC and fills the common header.
*/ void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
{
uint32_t crc; struct ubifs_ch *ch = node; unsignedlonglong sqnum = next_sqnum(c);
/** * ubifs_wbuf_sync_nolock - synchronize write-buffer. * @wbuf: write-buffer to synchronize * * This function synchronizes write-buffer @buf and returns zero in case of * success or a negative error code in case of failure. * * Note, although write-buffers are of @c->max_write_size, this function does * not necessarily writes all @c->max_write_size bytes to the flash. Instead, * if the write-buffer is only partially filled with data, only the used part * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized. * This way we waste less space.
*/ int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
{ struct ubifs_info *c = wbuf->c; int err, dirt, sync_len;
cancel_wbuf_timer_nolock(wbuf); if (!wbuf->used || wbuf->lnum == -1) /* Write-buffer is empty or not seeked */ return 0;
/* * Do not write whole write buffer but write only the minimum necessary * amount of min. I/O units.
*/
sync_len = ALIGN(wbuf->used, c->min_io_size);
dirt = sync_len - wbuf->used; if (dirt)
ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len); if (err) return err;
spin_lock(&wbuf->lock);
wbuf->offs += sync_len; /* * Now @wbuf->offs is not necessarily aligned to @c->max_write_size. * But our goal is to optimize writes and make sure we write in * @c->max_write_size chunks and to @c->max_write_size-aligned offset. * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make * sure that @wbuf->offs + @wbuf->size is aligned to * @c->max_write_size. This way we make sure that after next * write-buffer flush we are again at the optimal offset (aligned to * @c->max_write_size).
*/ if (c->leb_size - wbuf->offs < c->max_write_size)
wbuf->size = c->leb_size - wbuf->offs; elseif (wbuf->offs & (c->max_write_size - 1))
wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; else
wbuf->size = c->max_write_size;
wbuf->avail = wbuf->size;
wbuf->used = 0;
wbuf->next_ino = 0;
spin_unlock(&wbuf->lock);
/** * ubifs_wbuf_seek_nolock - seek write-buffer. * @wbuf: write-buffer * @lnum: logical eraseblock number to seek to * @offs: logical eraseblock offset to seek to * * This function targets the write-buffer to logical eraseblock @lnum:@offs. * The write-buffer has to be empty. Returns zero in case of success and a * negative error code in case of failure.
*/ int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs)
{ conststruct ubifs_info *c = wbuf->c;
/** * ubifs_bg_wbufs_sync - synchronize write-buffers. * @c: UBIFS file-system description object * * This function is called by background thread to synchronize write-buffers. * Returns zero in case of success and a negative error code in case of * failure.
*/ int ubifs_bg_wbufs_sync(struct ubifs_info *c)
{ int err, i;
/** * ubifs_wbuf_write_nolock - write data to flash via write-buffer. * @wbuf: write-buffer * @buf: node to write * @len: node length * * This function writes data to flash via write-buffer @wbuf. This means that * the last piece of the node won't reach the flash media immediately if it * does not take whole max. write unit (@c->max_write_size). Instead, the node * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or * because more data are appended to the write-buffer). * * This function returns zero in case of success and a negative error code in * case of failure. If the node cannot be written because there is no more * space in this logical eraseblock, %-ENOSPC is returned.
*/ int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
{ struct ubifs_info *c = wbuf->c; int err, n, written = 0, aligned_len = ALIGN(len, 8);
if (aligned_len <= wbuf->avail) { /* * The node is not very large and fits entirely within * write-buffer.
*/
memcpy(wbuf->buf + wbuf->used, buf, len); if (aligned_len > len) {
ubifs_assert(c, aligned_len - len < 8);
ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len);
}
if (aligned_len == wbuf->avail) {
dbg_io("flush jhead %s wbuf to LEB %d:%d",
dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf,
wbuf->offs, wbuf->size); if (err) goto out;
if (wbuf->used) { /* * The node is large enough and does not fit entirely within * current available space. We have to fill and flush * write-buffer and switch to the next max. write unit.
*/
dbg_io("flush jhead %s wbuf to LEB %d:%d",
dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs,
wbuf->size); if (err) goto out;
wbuf->offs += wbuf->size;
len -= wbuf->avail;
aligned_len -= wbuf->avail;
written += wbuf->avail;
} elseif (wbuf->offs & (c->max_write_size - 1)) { /* * The write-buffer offset is not aligned to * @c->max_write_size and @wbuf->size is less than * @c->max_write_size. Write @wbuf->size bytes to make sure the * following writes are done in optimal @c->max_write_size * chunks.
*/
dbg_io("write %d bytes to LEB %d:%d",
wbuf->size, wbuf->lnum, wbuf->offs);
err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs,
wbuf->size); if (err) goto out;
wbuf->offs += wbuf->size;
len -= wbuf->size;
aligned_len -= wbuf->size;
written += wbuf->size;
}
/* * The remaining data may take more whole max. write units, so write the * remains multiple to max. write unit size directly to the flash media. * We align node length to 8-byte boundary because we anyway flash wbuf * if the remaining space is less than 8 bytes.
*/
n = aligned_len >> c->max_write_shift; if (n) { int m = n - 1;
dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
wbuf->offs);
if (m) { /* '(n-1)<<c->max_write_shift < len' is always true. */
m <<= c->max_write_shift;
err = ubifs_leb_write(c, wbuf->lnum, buf + written,
wbuf->offs, m); if (err) goto out;
wbuf->offs += m;
aligned_len -= m;
len -= m;
written += m;
}
/* * The non-written len of buf may be less than 'n' because * parameter 'len' is not 8 bytes aligned, so here we read * min(len, n) bytes from buf.
*/
n = 1 << c->max_write_shift;
memcpy(wbuf->buf, buf + written, min(len, n)); if (n > len) {
ubifs_assert(c, n - len < 8);
ubifs_pad(c, wbuf->buf + len, n - len);
}
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n); if (err) goto out;
wbuf->offs += n;
aligned_len -= n;
len -= min(len, n);
written += n;
}
spin_lock(&wbuf->lock); if (aligned_len) { /* * And now we have what's left and what does not take whole * max. write unit, so write it to the write-buffer and we are * done.
*/
memcpy(wbuf->buf, buf + written, len); if (aligned_len > len) {
ubifs_assert(c, aligned_len - len < 8);
ubifs_pad(c, wbuf->buf + len, aligned_len - len);
}
}
/** * ubifs_write_node_hmac - write node to the media. * @c: UBIFS file-system description object * @buf: the node to write * @len: node length * @lnum: logical eraseblock number * @offs: offset within the logical eraseblock * @hmac_offs: offset of the HMAC within the node * * This function automatically fills node magic number, assigns sequence * number, and calculates node CRC checksum. The length of the @buf buffer has * to be aligned to the minimal I/O unit size. This function automatically * appends padding node and padding bytes if needed. Returns zero in case of * success and a negative error code in case of failure.
*/ int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum, int offs, int hmac_offs)
{ int err, buf_len = ALIGN(len, c->min_io_size);
/** * ubifs_write_node - write node to the media. * @c: UBIFS file-system description object * @buf: the node to write * @len: node length * @lnum: logical eraseblock number * @offs: offset within the logical eraseblock * * This function automatically fills node magic number, assigns sequence * number, and calculates node CRC checksum. The length of the @buf buffer has * to be aligned to the minimal I/O unit size. This function automatically * appends padding node and padding bytes if needed. Returns zero in case of * success and a negative error code in case of failure.
*/ int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum, int offs)
{ return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1);
}
/** * ubifs_read_node_wbuf - read node from the media or write-buffer. * @wbuf: wbuf to check for un-written data * @buf: buffer to read to * @type: node type * @len: node length * @lnum: logical eraseblock number * @offs: offset within the logical eraseblock * * This function reads a node of known type and length, checks it and stores * in @buf. If the node partially or fully sits in the write-buffer, this * function takes data from the buffer, otherwise it reads the flash media. * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative * error code in case of failure.
*/ int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len, int lnum, int offs)
{ conststruct ubifs_info *c = wbuf->c; int err, rlen, overlap; struct ubifs_ch *ch = buf;
spin_lock(&wbuf->lock);
overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); if (!overlap) { /* We may safely unlock the write-buffer and read the data */
spin_unlock(&wbuf->lock); return ubifs_read_node(c, buf, type, len, lnum, offs);
}
/* Don't read under wbuf */
rlen = wbuf->offs - offs; if (rlen < 0)
rlen = 0;
/* Copy the rest from the write-buffer */
memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
spin_unlock(&wbuf->lock);
if (rlen > 0) { /* Read everything that goes before write-buffer */
err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0); if (err && err != -EBADMSG) return err;
}
if (type != ch->node_type) {
ubifs_err(c, "bad node type (%d but expected %d)",
ch->node_type, type); goto out;
}
/** * ubifs_read_node - read node. * @c: UBIFS file-system description object * @buf: buffer to read to * @type: node type * @len: node length (not aligned) * @lnum: logical eraseblock number * @offs: offset within the logical eraseblock * * This function reads a node of known type and length, checks it and * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched * and a negative error code in case of failure.
*/ int ubifs_read_node(conststruct ubifs_info *c, void *buf, int type, int len, int lnum, int offs)
{ int err, l; struct ubifs_ch *ch = buf;
l = le32_to_cpu(ch->len); if (l != len) {
ubifs_errc(c, "bad node length %d, expected %d", l, len); goto out;
}
return 0;
out:
ubifs_errc(c, "bad node at LEB %d:%d, LEB mapping status %d", lnum,
offs, ubi_is_mapped(c->ubi, lnum)); if (!c->probing) {
ubifs_dump_node(c, buf, len);
dump_stack();
} return -EINVAL;
}
/** * ubifs_wbuf_init - initialize write-buffer. * @c: UBIFS file-system description object * @wbuf: write-buffer to initialize * * This function initializes write-buffer. Returns zero in case of success * %-ENOMEM in case of failure.
*/ int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
{
size_t size;
wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL); if (!wbuf->buf) return -ENOMEM;
wbuf->used = 0;
wbuf->lnum = wbuf->offs = -1; /* * If the LEB starts at the max. write size aligned address, then * write-buffer size has to be set to @c->max_write_size. Otherwise, * set it to something smaller so that it ends at the closest max. * write size boundary.
*/
size = c->max_write_size - (c->leb_start % c->max_write_size);
wbuf->avail = wbuf->size = size;
wbuf->sync_callback = NULL;
mutex_init(&wbuf->io_mutex);
spin_lock_init(&wbuf->lock);
wbuf->c = c;
wbuf->next_ino = 0;
/** * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array. * @wbuf: the write-buffer where to add * @inum: the inode number * * This function adds an inode number to the inode array of the write-buffer.
*/ void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
{ if (!wbuf->buf) /* NOR flash or something similar */ return;
spin_lock(&wbuf->lock); if (wbuf->used)
wbuf->inodes[wbuf->next_ino++] = inum;
spin_unlock(&wbuf->lock);
}
/** * wbuf_has_ino - returns if the wbuf contains data from the inode. * @wbuf: the write-buffer * @inum: the inode number * * This function returns with %1 if the write-buffer contains some data from the * given inode otherwise it returns with %0.
*/ staticint wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
{ int i, ret = 0;
spin_lock(&wbuf->lock); for (i = 0; i < wbuf->next_ino; i++) if (inum == wbuf->inodes[i]) {
ret = 1; break;
}
spin_unlock(&wbuf->lock);
return ret;
}
/** * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode. * @c: UBIFS file-system description object * @inode: inode to synchronize * * This function synchronizes write-buffers which contain nodes belonging to * @inode. Returns zero in case of success and a negative error code in case of * failure.
*/ int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
{ int i, err = 0;
for (i = 0; i < c->jhead_cnt; i++) { struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
if (i == GCHD) /* * GC head is special, do not look at it. Even if the * head contains something related to this inode, it is * a _copy_ of corresponding on-flash node which sits * somewhere else.
*/ continue;
if (!wbuf_has_ino(wbuf, inode->i_ino)) continue;
mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); if (wbuf_has_ino(wbuf, inode->i_ino))
err = ubifs_wbuf_sync_nolock(wbuf);
mutex_unlock(&wbuf->io_mutex);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.