LCOV - code coverage report
Current view: top level - libubifs - io.c (source / functions) Hit Total Coverage
Test: a simple test Lines: 391 440 88.9 %
Date: 2024-06-05 20:10:43 Functions: 21 23 91.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13