LCOV - code coverage report
Current view: top level - libubifs - super.c (source / functions) Hit Total Coverage
Test: a simple test Lines: 221 267 82.8 %
Date: 2024-06-05 20:10:43 Functions: 18 18 100.0 %
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             :  *
       7             :  * Authors: Artem Bityutskiy (Битюцкий Артём)
       8             :  *          Adrian Hunter
       9             :  */
      10             : 
      11             : /*
      12             :  * This file implements UBIFS initialization and VFS superblock operations. Some
      13             :  * initialization stuff which is rather large and complex is placed at
      14             :  * corresponding subsystems, but most of it is here.
      15             :  */
      16             : 
      17             : #include <stdio.h>
      18             : #include <unistd.h>
      19             : #include <sys/stat.h>
      20             : #include <sys/types.h>
      21             : 
      22             : #include "linux_err.h"
      23             : #include "bitops.h"
      24             : #include "kmem.h"
      25             : #include "ubifs.h"
      26             : #include "defs.h"
      27             : #include "debug.h"
      28             : #include "key.h"
      29             : #include "misc.h"
      30             : 
      31             : atomic_long_t ubifs_clean_zn_cnt;
      32             : static const int default_debug_level = WARN_LEVEL;
      33             : 
      34             : /**
      35             :  * open_ubi - open the libubi.
      36             :  * @c: the UBIFS file-system description object
      37             :  * @node: name of the UBI volume character device to fetch information about
      38             :  *
      39             :  * This function opens libubi, and initialize device & volume information
      40             :  * according to @node. Returns %0 in case of success and %-1 in case of failure.
      41             :  */
      42        3132 : int open_ubi(struct ubifs_info *c, const char *node)
      43             : {
      44             :         struct stat st;
      45             : 
      46        3132 :         if (stat(node, &st))
      47             :                 return -1;
      48             : 
      49        2855 :         if (!S_ISCHR(st.st_mode)) {
      50           2 :                 errno = ENODEV;
      51           2 :                 return -1;
      52             :         }
      53             : 
      54        2853 :         c->libubi = libubi_open();
      55        2853 :         if (!c->libubi)
      56             :                 return -1;
      57        2829 :         if (ubi_get_vol_info(c->libubi, node, &c->vi))
      58             :                 goto out_err;
      59        2748 :         if (ubi_get_dev_info1(c->libubi, c->vi.dev_num, &c->di))
      60             :                 goto out_err;
      61             : 
      62             :         return 0;
      63             : 
      64         102 : out_err:
      65             :         close_ubi(c);
      66             :         return -1;
      67             : }
      68             : 
      69         640 : void close_ubi(struct ubifs_info *c)
      70             : {
      71        2713 :         if (c->libubi) {
      72        2457 :                 libubi_close(c->libubi);
      73        2457 :                 c->libubi = NULL;
      74             :         }
      75         640 : }
      76             : 
      77             : /**
      78             :  * open_target - open the output target.
      79             :  * @c: the UBIFS file-system description object
      80             :  *
      81             :  * Open the output target. The target can be an UBI volume
      82             :  * or a file.
      83             :  *
      84             :  * Returns %0 in case of success and a negative error code in case of failure.
      85             :  */
      86        2983 : int open_target(struct ubifs_info *c)
      87             : {
      88        2983 :         if (c->libubi) {
      89        2727 :                 c->dev_fd = open(c->dev_name, O_RDWR | O_EXCL);
      90             : 
      91        2727 :                 if (c->dev_fd == -1) {
      92           3 :                         ubifs_err(c, "cannot open the UBI volume. %s",
      93             :                                   strerror(errno));
      94           3 :                         return -errno;
      95             :                 }
      96        2724 :                 if (ubi_set_property(c->dev_fd, UBI_VOL_PROP_DIRECT_WRITE, 1)) {
      97           0 :                         close(c->dev_fd);
      98           0 :                         ubifs_err(c, "ubi_set_property(set direct_write) failed. %s",
      99             :                                   strerror(errno));
     100           0 :                         return -errno;
     101             :                 }
     102             :         } else {
     103         256 :                 c->dev_fd = open(c->dev_name, O_CREAT | O_RDWR | O_TRUNC,
     104             :                               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
     105         256 :                 if (c->dev_fd == -1) {
     106           0 :                         ubifs_err(c, "cannot create output file. %s",
     107             :                                   strerror(errno));
     108           0 :                         return -errno;
     109             :                 }
     110             :         }
     111             :         return 0;
     112             : }
     113             : 
     114             : /**
     115             :  * close_target - close the output target.
     116             :  * @c: the UBIFS file-system description object
     117             :  *
     118             :  * Close the output target. If the target was an UBI
     119             :  * volume, also close libubi.
     120             :  *
     121             :  * Returns %0 in case of success and a negative error code in case of failure.
     122             :  */
     123        2608 : int close_target(struct ubifs_info *c)
     124             : {
     125        2608 :         if (c->dev_fd >= 0) {
     126        2608 :                 if (c->libubi && ubi_set_property(c->dev_fd, UBI_VOL_PROP_DIRECT_WRITE, 0)) {
     127           0 :                         ubifs_err(c, "ubi_set_property(clear direct_write) failed. %s",
     128             :                                   strerror(errno));
     129           0 :                         return -errno;
     130             :                 }
     131        2608 :                 if (close(c->dev_fd) == -1) {
     132           0 :                         ubifs_err(c, "cannot close the target. %s",
     133             :                                   strerror(errno));
     134           0 :                         return -errno;
     135             :                 }
     136             :         }
     137             :         return 0;
     138             : }
     139             : 
     140             : /**
     141             :  * ubifs_open_volume - open UBI volume.
     142             :  * @c: the UBIFS file-system description object
     143             :  * @volume_name: the UBI volume name
     144             :  *
     145             :  * Open ubi volume. This function is implemented by open_ubi + open_target.
     146             :  *
     147             :  * Returns %0 in case of success and a negative error code in case of failure.
     148             :  */
     149        2492 : int ubifs_open_volume(struct ubifs_info *c, const char *volume_name)
     150             : {
     151             :         int err;
     152             : 
     153        2492 :         err = open_ubi(c, volume_name);
     154        2492 :         if (err) {
     155         149 :                 ubifs_err(c, "cannot open libubi. %s", strerror(errno));
     156             :                 return err;
     157             :         }
     158             : 
     159        2343 :         err = open_target(c);
     160        2343 :         if (err)
     161             :                 close_ubi(c);
     162             : 
     163             :         return err;
     164             : }
     165             : 
     166             : /**
     167             :  * ubifs_close_volume - close UBI volume.
     168             :  * @c: the UBIFS file-system description object
     169             :  *
     170             :  * Close ubi volume. This function is implemented by close_target + close_ubi.
     171             :  *
     172             :  * Returns %0 in case of success and a negative error code in case of failure.
     173             :  */
     174        1968 : int ubifs_close_volume(struct ubifs_info *c)
     175             : {
     176             :         int err;
     177             : 
     178        1968 :         err = close_target(c);
     179        1968 :         if (err)
     180             :                 return err;
     181             : 
     182             :         close_ubi(c);
     183             : 
     184             :         return 0;
     185             : }
     186             : 
     187             : /**
     188             :  * check_volume_empty - check if the UBI volume is empty.
     189             :  * @c: the UBIFS file-system description object
     190             :  *
     191             :  * This function checks if the UBI volume is empty by looking if its LEBs are
     192             :  * mapped or not.
     193             :  *
     194             :  * Returns %0 in case of success, %1 is the volume is not empty,
     195             :  * and a negative error code in case of failure.
     196             :  */
     197        2844 : int check_volume_empty(struct ubifs_info *c)
     198             : {
     199             :         int lnum, err;
     200             : 
     201      325788 :         for (lnum = 0; lnum < c->vi.rsvd_lebs; lnum++) {
     202      325276 :                 err = ubi_is_mapped(c->dev_fd, lnum);
     203      325276 :                 if (err < 0)
     204             :                         return err;
     205      325276 :                 if (err == 1)
     206             :                         return 1;
     207             :         }
     208             :         return 0;
     209             : }
     210             : 
     211        3157 : void init_ubifs_info(struct ubifs_info *c, int program_type)
     212             : {
     213        3157 :         spin_lock_init(&c->cnt_lock);
     214        3157 :         spin_lock_init(&c->cs_lock);
     215        3157 :         spin_lock_init(&c->buds_lock);
     216        3157 :         spin_lock_init(&c->space_lock);
     217        3157 :         spin_lock_init(&c->orphan_lock);
     218        3157 :         init_rwsem(&c->commit_sem);
     219        3157 :         mutex_init(&c->lp_mutex);
     220        3157 :         mutex_init(&c->tnc_mutex);
     221        3157 :         mutex_init(&c->log_mutex);
     222        3157 :         c->buds = RB_ROOT;
     223        3157 :         c->old_idx = RB_ROOT;
     224        3157 :         c->size_tree = RB_ROOT;
     225        3157 :         c->orph_tree = RB_ROOT;
     226        6314 :         INIT_LIST_HEAD(&c->idx_gc);
     227        6314 :         INIT_LIST_HEAD(&c->replay_list);
     228        6314 :         INIT_LIST_HEAD(&c->replay_buds);
     229        6314 :         INIT_LIST_HEAD(&c->uncat_list);
     230        6314 :         INIT_LIST_HEAD(&c->empty_list);
     231        6314 :         INIT_LIST_HEAD(&c->freeable_list);
     232        6314 :         INIT_LIST_HEAD(&c->frdi_idx_list);
     233        6314 :         INIT_LIST_HEAD(&c->unclean_leb_list);
     234        6314 :         INIT_LIST_HEAD(&c->old_buds);
     235        6314 :         INIT_LIST_HEAD(&c->orph_list);
     236        6314 :         INIT_LIST_HEAD(&c->orph_new);
     237        3157 :         c->no_chk_data_crc = 1;
     238             : 
     239        3157 :         c->highest_inum = UBIFS_FIRST_INO;
     240        3157 :         c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
     241             : 
     242        3157 :         c->program_type = program_type;
     243        3157 :         switch (c->program_type) {
     244         640 :         case MKFS_PROGRAM_TYPE:
     245         640 :                 c->program_name = MKFS_PROGRAM_NAME;
     246         640 :                 break;
     247        2517 :         case FSCK_PROGRAM_TYPE:
     248        2517 :                 c->program_name = FSCK_PROGRAM_NAME;
     249             :                 /* Always check crc for data node. */
     250        2517 :                 c->no_chk_data_crc = 0;
     251        2517 :                 break;
     252             :         default:
     253           0 :                 assert(0);
     254             :                 break;
     255             :         }
     256        3157 :         c->dev_fd = -1;
     257        3157 :         c->debug_level = default_debug_level;
     258        3157 : }
     259             : 
     260             : /**
     261             :  * init_constants_early - initialize UBIFS constants.
     262             :  * @c: UBIFS file-system description object
     263             :  *
     264             :  * This function initialize UBIFS constants which do not need the superblock to
     265             :  * be read. It also checks that the UBI volume satisfies basic UBIFS
     266             :  * requirements. Returns zero in case of success and a negative error code in
     267             :  * case of failure.
     268             :  */
     269        2340 : int init_constants_early(struct ubifs_info *c)
     270             : {
     271             : #define NOR_MAX_WRITESZ 64
     272        2340 :         if (c->vi.corrupted) {
     273           0 :                 ubifs_warn(c, "UBI volume is corrupted - read-only mode");
     274           0 :                 c->ro_media = 1;
     275             :         }
     276             : 
     277        2340 :         if (c->vi.type == UBI_STATIC_VOLUME) {
     278           0 :                 ubifs_msg(c, "static UBI volume - read-only mode");
     279           0 :                 c->ro_media = 1;
     280             :         }
     281             : 
     282        2340 :         c->max_inode_sz = key_max_inode_size(c);
     283        2340 :         c->leb_cnt = c->vi.rsvd_lebs;
     284        2340 :         c->leb_size = c->vi.leb_size;
     285        2340 :         c->half_leb_size = c->leb_size / 2;
     286        2340 :         c->min_io_size = c->di.min_io_size;
     287        2340 :         c->min_io_shift = fls(c->min_io_size) - 1;
     288        2340 :         if (c->min_io_size == 1)
     289             :                 /*
     290             :                  * Different from linux kernel, the max write size of nor flash
     291             :                  * is not exposed in sysfs, just reset @c->max_write_size.
     292             :                  */
     293        1024 :                 c->max_write_size = NOR_MAX_WRITESZ;
     294             :         else
     295        1316 :                 c->max_write_size = c->di.min_io_size;
     296        2340 :         c->max_write_shift = fls(c->max_write_size) - 1;
     297             : 
     298        2340 :         if (c->leb_size < UBIFS_MIN_LEB_SZ) {
     299           0 :                 ubifs_err(c, "too small LEBs (%d bytes), min. is %d bytes",
     300             :                           c->leb_size, UBIFS_MIN_LEB_SZ);
     301             :                 return -EINVAL;
     302             :         }
     303             : 
     304        2340 :         if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
     305           8 :                 ubifs_err(c, "too few LEBs (%d), min. is %d",
     306             :                           c->leb_cnt, UBIFS_MIN_LEB_CNT);
     307             :                 return -EINVAL;
     308             :         }
     309             : 
     310        4664 :         if (!is_power_of_2(c->min_io_size)) {
     311           0 :                 ubifs_err(c, "bad min. I/O size %d", c->min_io_size);
     312             :                 return -EINVAL;
     313             :         }
     314             : 
     315             :         /*
     316             :          * Maximum write size has to be greater or equivalent to min. I/O
     317             :          * size, and be multiple of min. I/O size.
     318             :          */
     319        4664 :         if (c->max_write_size < c->min_io_size ||
     320        2332 :             c->max_write_size % c->min_io_size ||
     321        4664 :             !is_power_of_2(c->max_write_size)) {
     322           0 :                 ubifs_err(c, "bad write buffer size %d for %d min. I/O unit",
     323             :                           c->max_write_size, c->min_io_size);
     324             :                 return -EINVAL;
     325             :         }
     326             : 
     327             :         /*
     328             :          * UBIFS aligns all node to 8-byte boundary, so to make function in
     329             :          * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
     330             :          * less than 8.
     331             :          */
     332        2332 :         if (c->min_io_size < 8) {
     333        1024 :                 c->min_io_size = 8;
     334        1024 :                 c->min_io_shift = 3;
     335        1024 :                 if (c->max_write_size < c->min_io_size) {
     336           0 :                         c->max_write_size = c->min_io_size;
     337           0 :                         c->max_write_shift = c->min_io_shift;
     338             :                 }
     339             :         }
     340             : 
     341        2332 :         c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
     342        2332 :         c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
     343             : 
     344             :         /*
     345             :          * Initialize node length ranges which are mostly needed for node
     346             :          * length validation.
     347             :          */
     348        2332 :         c->ranges[UBIFS_PAD_NODE].len  = UBIFS_PAD_NODE_SZ;
     349        2332 :         c->ranges[UBIFS_SB_NODE].len   = UBIFS_SB_NODE_SZ;
     350        2332 :         c->ranges[UBIFS_MST_NODE].len  = UBIFS_MST_NODE_SZ;
     351        2332 :         c->ranges[UBIFS_REF_NODE].len  = UBIFS_REF_NODE_SZ;
     352        2332 :         c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
     353        2332 :         c->ranges[UBIFS_CS_NODE].len   = UBIFS_CS_NODE_SZ;
     354        2332 :         c->ranges[UBIFS_AUTH_NODE].min_len = UBIFS_AUTH_NODE_SZ;
     355        2332 :         c->ranges[UBIFS_AUTH_NODE].max_len = UBIFS_AUTH_NODE_SZ +
     356             :                                 UBIFS_MAX_HMAC_LEN;
     357        2332 :         c->ranges[UBIFS_SIG_NODE].min_len = UBIFS_SIG_NODE_SZ;
     358        2332 :         c->ranges[UBIFS_SIG_NODE].max_len = c->leb_size - UBIFS_SB_NODE_SZ;
     359             : 
     360        2332 :         c->ranges[UBIFS_INO_NODE].min_len  = UBIFS_INO_NODE_SZ;
     361        2332 :         c->ranges[UBIFS_INO_NODE].max_len  = UBIFS_MAX_INO_NODE_SZ;
     362        2332 :         c->ranges[UBIFS_ORPH_NODE].min_len =
     363             :                                 UBIFS_ORPH_NODE_SZ + sizeof(__le64);
     364        2332 :         c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
     365        2332 :         c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
     366        2332 :         c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
     367        2332 :         c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
     368        2332 :         c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
     369        2332 :         c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
     370        2332 :         c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
     371             :         /*
     372             :          * Minimum indexing node size is amended later when superblock is
     373             :          * read and the key length is known.
     374             :          */
     375        2332 :         c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
     376             :         /*
     377             :          * Maximum indexing node size is amended later when superblock is
     378             :          * read and the fanout is known.
     379             :          */
     380        2332 :         c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
     381             : 
     382             :         /*
     383             :          * Initialize dead and dark LEB space watermarks. See gc.c for comments
     384             :          * about these values.
     385             :          */
     386        2332 :         c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
     387        2332 :         c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
     388             : 
     389             :         /*
     390             :          * Calculate how many bytes would be wasted at the end of LEB if it was
     391             :          * fully filled with data nodes of maximum size. This is used in
     392             :          * calculations when reporting free space.
     393             :          */
     394        2332 :         c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
     395             : 
     396             :         /* Log is ready, preserve one LEB for commits. */
     397        2332 :         c->min_log_bytes = c->leb_size;
     398             : 
     399        2332 :         return 0;
     400             : }
     401             : 
     402             : /**
     403             :  * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
     404             :  * @c: UBIFS file-system description object
     405             :  * @lnum: LEB the write-buffer was synchronized to
     406             :  * @free: how many free bytes left in this LEB
     407             :  * @pad: how many bytes were padded
     408             :  *
     409             :  * This is a callback function which is called by the I/O unit when the
     410             :  * write-buffer is synchronized. We need this to correctly maintain space
     411             :  * accounting in bud logical eraseblocks. This function returns zero in case of
     412             :  * success and a negative error code in case of failure.
     413             :  *
     414             :  * This function actually belongs to the journal, but we keep it here because
     415             :  * we want to keep it static.
     416             :  */
     417        5785 : static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
     418             : {
     419        5785 :         return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
     420             : }
     421             : 
     422             : /*
     423             :  * init_constants_sb - initialize UBIFS constants.
     424             :  * @c: UBIFS file-system description object
     425             :  *
     426             :  * This is a helper function which initializes various UBIFS constants after
     427             :  * the superblock has been read. It also checks various UBIFS parameters and
     428             :  * makes sure they are all right. Returns zero in case of success and a
     429             :  * negative error code in case of failure.
     430             :  */
     431        2284 : int init_constants_sb(struct ubifs_info *c)
     432             : {
     433             :         int tmp, err;
     434             :         long long tmp64;
     435             : 
     436        2284 :         c->main_bytes = (long long)c->main_lebs * c->leb_size;
     437        2284 :         c->max_znode_sz = sizeof(struct ubifs_znode) +
     438        2284 :                                 c->fanout * sizeof(struct ubifs_zbranch);
     439             : 
     440        2284 :         tmp = ubifs_idx_node_sz(c, 1);
     441        2284 :         c->ranges[UBIFS_IDX_NODE].min_len = tmp;
     442        2284 :         c->min_idx_node_sz = ALIGN(tmp, 8);
     443             : 
     444        4568 :         tmp = ubifs_idx_node_sz(c, c->fanout);
     445        2284 :         c->ranges[UBIFS_IDX_NODE].max_len = tmp;
     446        2284 :         c->max_idx_node_sz = ALIGN(tmp, 8);
     447             : 
     448             :         /* Make sure LEB size is large enough to fit full commit */
     449        2284 :         tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
     450        2284 :         tmp = ALIGN(tmp, c->min_io_size);
     451        2284 :         if (tmp > c->leb_size) {
     452           0 :                 ubifs_err(c, "too small LEB size %d, at least %d needed",
     453             :                           c->leb_size, tmp);
     454             :                 return -EINVAL;
     455             :         }
     456             : 
     457             :         /*
     458             :          * Make sure that the log is large enough to fit reference nodes for
     459             :          * all buds plus one reserved LEB.
     460             :          */
     461        2284 :         tmp64 = c->max_bud_bytes + c->leb_size - 1;
     462        4568 :         c->max_bud_cnt = div_u64(tmp64, c->leb_size);
     463        2284 :         tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
     464        2284 :         tmp /= c->leb_size;
     465        2284 :         tmp += 1;
     466        2284 :         if (c->log_lebs < tmp) {
     467           0 :                 ubifs_err(c, "too small log %d LEBs, required min. %d LEBs",
     468             :                           c->log_lebs, tmp);
     469             :                 return -EINVAL;
     470             :         }
     471             : 
     472             :         /*
     473             :          * When budgeting we assume worst-case scenarios when the pages are not
     474             :          * be compressed and direntries are of the maximum size.
     475             :          *
     476             :          * Note, data, which may be stored in inodes is budgeted separately, so
     477             :          * it is not included into 'c->bi.inode_budget'.
     478             :          */
     479        2284 :         c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
     480        2284 :         c->bi.inode_budget = UBIFS_INO_NODE_SZ;
     481        2284 :         c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ;
     482             : 
     483             :         /*
     484             :          * When the amount of flash space used by buds becomes
     485             :          * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
     486             :          * The writers are unblocked when the commit is finished. To avoid
     487             :          * writers to be blocked UBIFS initiates background commit in advance,
     488             :          * when number of bud bytes becomes above the limit defined below.
     489             :          */
     490        2284 :         c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
     491             : 
     492             :         /*
     493             :          * Ensure minimum journal size. All the bytes in the journal heads are
     494             :          * considered to be used, when calculating the current journal usage.
     495             :          * Consequently, if the journal is too small, UBIFS will treat it as
     496             :          * always full.
     497             :          */
     498        2284 :         tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
     499        2284 :         if (c->bg_bud_bytes < tmp64)
     500         298 :                 c->bg_bud_bytes = tmp64;
     501        2284 :         if (c->max_bud_bytes < tmp64 + c->leb_size)
     502         298 :                 c->max_bud_bytes = tmp64 + c->leb_size;
     503             : 
     504        2284 :         err = ubifs_calc_lpt_geom(c);
     505        2284 :         if (err)
     506             :                 return err;
     507             : 
     508             :         /* Initialize effective LEB size used in budgeting calculations */
     509        2284 :         c->idx_leb_size = c->leb_size - c->max_idx_node_sz;
     510        2284 :         return 0;
     511             : }
     512             : 
     513             : /*
     514             :  * init_constants_master - initialize UBIFS constants.
     515             :  * @c: UBIFS file-system description object
     516             :  *
     517             :  * This is a helper function which initializes various UBIFS constants after
     518             :  * the master node has been read. It also checks various UBIFS parameters and
     519             :  * makes sure they are all right.
     520             :  */
     521        2265 : void init_constants_master(struct ubifs_info *c)
     522             : {
     523        2265 :         c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
     524        2265 : }
     525             : 
     526             : /**
     527             :  * take_gc_lnum - reserve GC LEB.
     528             :  * @c: UBIFS file-system description object
     529             :  *
     530             :  * This function ensures that the LEB reserved for garbage collection is marked
     531             :  * as "taken" in lprops. We also have to set free space to LEB size and dirty
     532             :  * space to zero, because lprops may contain out-of-date information if the
     533             :  * file-system was un-mounted before it has been committed. This function
     534             :  * returns zero in case of success and a negative error code in case of
     535             :  * failure.
     536             :  */
     537         978 : int take_gc_lnum(struct ubifs_info *c)
     538             : {
     539             :         int err;
     540             : 
     541         978 :         if (c->gc_lnum == -1) {
     542           0 :                 ubifs_err(c, "no LEB for GC");
     543             :                 return -EINVAL;
     544             :         }
     545             : 
     546             :         /* And we have to tell lprops that this LEB is taken */
     547         978 :         err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
     548             :                                   LPROPS_TAKEN, 0, 0);
     549         978 :         return err;
     550             : }
     551             : 
     552             : /**
     553             :  * alloc_wbufs - allocate write-buffers.
     554             :  * @c: UBIFS file-system description object
     555             :  *
     556             :  * This helper function allocates and initializes UBIFS write-buffers. Returns
     557             :  * zero in case of success and %-ENOMEM in case of failure.
     558             :  */
     559        2284 : int alloc_wbufs(struct ubifs_info *c)
     560             : {
     561             :         int i, err;
     562             : 
     563        4568 :         c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead),
     564             :                             GFP_KERNEL);
     565        2284 :         if (!c->jheads)
     566             :                 return -ENOMEM;
     567             : 
     568             :         /* Initialize journal heads */
     569        6852 :         for (i = 0; i < c->jhead_cnt; i++) {
     570       13704 :                 INIT_LIST_HEAD(&c->jheads[i].buds_list);
     571        6852 :                 err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
     572        6852 :                 if (err)
     573             :                         goto out_wbuf;
     574             : 
     575        6852 :                 c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
     576        6852 :                 c->jheads[i].wbuf.jhead = i;
     577        6852 :                 c->jheads[i].grouped = 1;
     578        6852 :                 c->jheads[i].log_hash = ubifs_hash_get_desc(c);
     579       13704 :                 if (IS_ERR(c->jheads[i].log_hash)) {
     580           0 :                         err = PTR_ERR(c->jheads[i].log_hash);
     581             :                         goto out_log_hash;
     582             :                 }
     583             :         }
     584             : 
     585             :         /*
     586             :          * Garbage Collector head does not need to be synchronized by timer.
     587             :          * Also GC head nodes are not grouped.
     588             :          */
     589        2284 :         c->jheads[GCHD].grouped = 0;
     590             : 
     591        2284 :         return 0;
     592             : 
     593           0 : out_log_hash:
     594           0 :         kfree(c->jheads[i].wbuf.buf);
     595           0 :         kfree(c->jheads[i].wbuf.inodes);
     596             : 
     597           0 : out_wbuf:
     598           0 :         while (i--) {
     599           0 :                 kfree(c->jheads[i].wbuf.buf);
     600           0 :                 kfree(c->jheads[i].wbuf.inodes);
     601           0 :                 kfree(c->jheads[i].log_hash);
     602             :         }
     603           0 :         kfree(c->jheads);
     604           0 :         c->jheads = NULL;
     605             : 
     606           0 :         return err;
     607             : }
     608             : 
     609             : /**
     610             :  * free_wbufs - free write-buffers.
     611             :  * @c: UBIFS file-system description object
     612             :  */
     613        1960 : void free_wbufs(struct ubifs_info *c)
     614             : {
     615             :         int i;
     616             : 
     617        1960 :         if (c->jheads) {
     618        5880 :                 for (i = 0; i < c->jhead_cnt; i++) {
     619       11760 :                         kfree(c->jheads[i].wbuf.buf);
     620       11760 :                         kfree(c->jheads[i].wbuf.inodes);
     621       11760 :                         kfree(c->jheads[i].log_hash);
     622             :                 }
     623        3920 :                 kfree(c->jheads);
     624        1960 :                 c->jheads = NULL;
     625             :         }
     626        1960 : }
     627             : 
     628             : /**
     629             :  * free_orphans - free orphans.
     630             :  * @c: UBIFS file-system description object
     631             :  */
     632        1940 : void free_orphans(struct ubifs_info *c)
     633             : {
     634             :         struct ubifs_orphan *orph;
     635             : 
     636        3880 :         while (c->orph_dnext) {
     637           0 :                 orph = c->orph_dnext;
     638           0 :                 c->orph_dnext = orph->dnext;
     639           0 :                 list_del(&orph->list);
     640             :                 kfree(orph);
     641             :         }
     642             : 
     643        3880 :         while (!list_empty(&c->orph_list)) {
     644           0 :                 orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
     645           0 :                 list_del(&orph->list);
     646           0 :                 kfree(orph);
     647           0 :                 ubifs_err(c, "orphan list not empty at unmount");
     648             :         }
     649             : 
     650        1940 :         vfree(c->orph_buf);
     651        1940 :         c->orph_buf = NULL;
     652        1940 : }
     653             : 
     654             : /**
     655             :  * free_buds - free per-bud objects.
     656             :  * @c: UBIFS file-system description object
     657             :  * @delete_from_list: whether to delete the bud from list
     658             :  */
     659        2046 : void free_buds(struct ubifs_info *c, bool delete_from_list)
     660             : {
     661             :         struct ubifs_bud *bud, *n;
     662             : 
     663        5539 :         rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb) {
     664        3493 :                 if (delete_from_list)
     665         852 :                         list_del(&bud->list);
     666        6986 :                 kfree(bud->log_hash);
     667        3493 :                 kfree(bud);
     668             :         }
     669             : 
     670        2046 :         c->buds = RB_ROOT;
     671        2046 : }
     672             : 
     673             : /**
     674             :  * destroy_journal - destroy journal data structures.
     675             :  * @c: UBIFS file-system description object
     676             :  *
     677             :  * This function destroys journal data structures including those that may have
     678             :  * been created by recovery functions.
     679             :  */
     680        1951 : void destroy_journal(struct ubifs_info *c)
     681             : {
     682        5853 :         while (!list_empty(&c->unclean_leb_list)) {
     683             :                 struct ubifs_unclean_leb *ucleb;
     684             : 
     685           0 :                 ucleb = list_entry(c->unclean_leb_list.next,
     686             :                                    struct ubifs_unclean_leb, list);
     687           0 :                 list_del(&ucleb->list);
     688             :                 kfree(ucleb);
     689             :         }
     690        3902 :         while (!list_empty(&c->old_buds)) {
     691             :                 struct ubifs_bud *bud;
     692             : 
     693           0 :                 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
     694           0 :                 list_del(&bud->list);
     695           0 :                 kfree(bud->log_hash);
     696             :                 kfree(bud);
     697             :         }
     698        1951 :         ubifs_destroy_idx_gc(c);
     699        1951 :         ubifs_destroy_size_tree(c);
     700        1951 :         ubifs_tnc_close(c);
     701        1951 :         free_buds(c, false);
     702        1951 : }

Generated by: LCOV version 1.13