// SPDX-License-Identifier: GPL-2.0-only /* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation. * * Authors: Artem Bityutskiy (Битюцкий Артём) * Adrian Hunter
*/
/* * This file implements UBIFS initialization and VFS superblock operations. Some * initialization stuff which is rather large and complex is placed at * corresponding subsystems, but most of it is here.
*/
/** * validate_inode - validate inode. * @c: UBIFS file-system description object * @inode: the inode to validate * * This is a helper function for 'ubifs_iget()' which validates various fields * of a newly built inode to make sure they contain sane values and prevent * possible vulnerabilities. Returns zero if the inode is all right and * a non-zero error code if not.
*/ staticint validate_inode(struct ubifs_info *c, conststruct inode *inode)
{ int err; conststruct ubifs_inode *ui = ubifs_inode(inode);
if (inode->i_size > c->max_inode_sz) {
ubifs_err(c, "inode is too large (%lld)",
(longlong)inode->i_size); return 1;
}
if (ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
ubifs_err(c, "unknown compression type %d", ui->compr_type); return 2;
}
if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX) return 3;
if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA) return 4;
if (ui->xattr && !S_ISREG(inode->i_mode)) return 5;
if (!ubifs_compr_present(c, ui->compr_type)) {
ubifs_warn(c, "inode %lu uses '%s' compression, but it was not compiled in",
inode->i_ino, ubifs_compr_name(c, ui->compr_type));
}
/* * Note, Linux write-back code calls this without 'i_mutex'.
*/ staticint ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)
{ int err = 0; struct ubifs_info *c = inode->i_sb->s_fs_info; struct ubifs_inode *ui = ubifs_inode(inode);
ubifs_assert(c, !ui->xattr); if (is_bad_inode(inode)) return 0;
mutex_lock(&ui->ui_mutex); /* * Due to races between write-back forced by budgeting * (see 'sync_some_inodes()') and background write-back, the inode may * have already been synchronized, do not do this again. This might * also happen if it was synchronized in an VFS operation, e.g. * 'ubifs_link()'.
*/ if (!ui->dirty) {
mutex_unlock(&ui->ui_mutex); return 0;
}
/* * As an optimization, do not write orphan inodes to the media just * because this is not needed.
*/
dbg_gen("inode %lu, mode %#x, nlink %u",
inode->i_ino, (int)inode->i_mode, inode->i_nlink); if (inode->i_nlink) {
err = ubifs_jnl_write_inode(c, inode); if (err)
ubifs_err(c, "can't write inode %lu, error %d",
inode->i_ino, err); else
err = dbg_check_inode_size(c, inode, ui->ui_size);
}
if (ui->xattr) /* * Extended attribute inode deletions are fully handled in * 'ubifs_removexattr()'. These inodes are special and have * limited usage, so there is nothing to do here.
*/ goto out;
ui->ui_size = inode->i_size = 0;
err = ubifs_jnl_delete_inode(c, inode); if (err) /* * Worst case we have a lost orphan inode wasting space, so a * simple error message is OK here.
*/
ubifs_err(c, "can't delete inode %lu, error %d",
inode->i_ino, err);
out: if (ui->dirty)
ubifs_release_dirty_inode_budget(c, ui); else { /* We've deleted something - clean the "no space" flags */
c->bi.nospace = c->bi.nospace_rp = 0;
smp_wmb();
}
done:
clear_inode(inode);
fscrypt_put_encryption_info(inode);
}
staticint ubifs_sync_fs(struct super_block *sb, int wait)
{ int i, err; struct ubifs_info *c = sb->s_fs_info;
/* * Zero @wait is just an advisory thing to help the file system shove * lots of data into the queues, and there will be the second * '->sync_fs()' call, with non-zero @wait.
*/ if (!wait) return 0;
/* * Synchronize write buffers, because 'ubifs_run_commit()' does not * do this if it waits for an already running commit.
*/ for (i = 0; i < c->jhead_cnt; i++) {
err = ubifs_wbuf_sync(&c->jheads[i].wbuf); if (err) return err;
}
/* * Strictly speaking, it is not necessary to commit the journal here, * synchronizing write-buffers would be enough. But committing makes * UBIFS free space predictions much more accurate, so we want to let * the user be able to get more accurate results of 'statfs()' after * they synchronize the file system.
*/
err = ubifs_run_commit(c); if (err) return err;
return ubi_sync(c->vi.ubi_num);
}
/** * init_constants_early - initialize UBIFS constants. * @c: UBIFS file-system description object * * This function initialize UBIFS constants which do not need the superblock to * be read. It also checks that the UBI volume satisfies basic UBIFS * requirements. Returns zero in case of success and a negative error code in * case of failure.
*/ staticint init_constants_early(struct ubifs_info *c)
{ if (c->vi.corrupted) {
ubifs_warn(c, "UBI volume is corrupted - read-only mode");
c->ro_media = 1;
}
if (c->di.ro_mode) {
ubifs_msg(c, "read-only UBI device");
c->ro_media = 1;
}
if (c->vi.vol_type == UBI_STATIC_VOLUME) {
ubifs_msg(c, "static UBI volume - read-only mode");
c->ro_media = 1;
}
if (c->leb_size < UBIFS_MIN_LEB_SZ) {
ubifs_errc(c, "too small LEBs (%d bytes), min. is %d bytes",
c->leb_size, UBIFS_MIN_LEB_SZ); return -EINVAL;
}
if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
ubifs_errc(c, "too few LEBs (%d), min. is %d",
c->leb_cnt, UBIFS_MIN_LEB_CNT); return -EINVAL;
}
if (!is_power_of_2(c->min_io_size)) {
ubifs_errc(c, "bad min. I/O size %d", c->min_io_size); return -EINVAL;
}
/* * Maximum write size has to be greater or equivalent to min. I/O * size, and be multiple of min. I/O size.
*/ if (c->max_write_size < c->min_io_size ||
c->max_write_size % c->min_io_size ||
!is_power_of_2(c->max_write_size)) {
ubifs_errc(c, "bad write buffer size %d for %d min. I/O unit",
c->max_write_size, c->min_io_size); return -EINVAL;
}
/* * UBIFS aligns all node to 8-byte boundary, so to make function in * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is * less than 8.
*/ if (c->min_io_size < 8) {
c->min_io_size = 8;
c->min_io_shift = 3; if (c->max_write_size < c->min_io_size) {
c->max_write_size = c->min_io_size;
c->max_write_shift = c->min_io_shift;
}
}
c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
c->ranges[UBIFS_ORPH_NODE].min_len =
UBIFS_ORPH_NODE_SZ + sizeof(__le64);
c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ; /* * Minimum indexing node size is amended later when superblock is * read and the key length is known.
*/
c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ; /* * Maximum indexing node size is amended later when superblock is * read and the fanout is known.
*/
c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
/* * Initialize dead and dark LEB space watermarks. See gc.c for comments * about these values.
*/
c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
/* * Calculate how many bytes would be wasted at the end of LEB if it was * fully filled with data nodes of maximum size. This is used in * calculations when reporting free space.
*/
c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
/* Buffer size for bulk-reads */
c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ; if (c->max_bu_buf_len > c->leb_size)
c->max_bu_buf_len = c->leb_size;
/* Log is ready, preserve one LEB for commits. */
c->min_log_bytes = c->leb_size;
return 0;
}
/** * bud_wbuf_callback - bud LEB write-buffer synchronization call-back. * @c: UBIFS file-system description object * @lnum: LEB the write-buffer was synchronized to * @free: how many free bytes left in this LEB * @pad: how many bytes were padded * * This is a callback function which is called by the I/O unit when the * write-buffer is synchronized. We need this to correctly maintain space * accounting in bud logical eraseblocks. This function returns zero in case of * success and a negative error code in case of failure. * * This function actually belongs to the journal, but we keep it here because * we want to keep it static.
*/ staticint bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
{ return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
}
/* * init_constants_sb - initialize UBIFS constants. * @c: UBIFS file-system description object * * This is a helper function which initializes various UBIFS constants after * the superblock has been read. It also checks various UBIFS parameters and * makes sure they are all right. Returns zero in case of success and a * negative error code in case of failure.
*/ staticint init_constants_sb(struct ubifs_info *c)
{ int tmp, err; longlong tmp64;
/* Make sure LEB size is large enough to fit full commit */
tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
tmp = ALIGN(tmp, c->min_io_size); if (tmp > c->leb_size) {
ubifs_err(c, "too small LEB size %d, at least %d needed",
c->leb_size, tmp); return -EINVAL;
}
/* * Make sure that the log is large enough to fit reference nodes for * all buds plus one reserved LEB.
*/
tmp64 = c->max_bud_bytes + c->leb_size - 1;
c->max_bud_cnt = div_u64(tmp64, c->leb_size);
tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
tmp /= c->leb_size;
tmp += 1; if (c->log_lebs < tmp) {
ubifs_err(c, "too small log %d LEBs, required min. %d LEBs",
c->log_lebs, tmp); return -EINVAL;
}
/* * When budgeting we assume worst-case scenarios when the pages are not * be compressed and direntries are of the maximum size. * * Note, data, which may be stored in inodes is budgeted separately, so * it is not included into 'c->bi.inode_budget'.
*/
c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
c->bi.inode_budget = UBIFS_INO_NODE_SZ;
c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ;
/* * When the amount of flash space used by buds becomes * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit. * The writers are unblocked when the commit is finished. To avoid * writers to be blocked UBIFS initiates background commit in advance, * when number of bud bytes becomes above the limit defined below.
*/
c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
/* * Ensure minimum journal size. All the bytes in the journal heads are * considered to be used, when calculating the current journal usage. * Consequently, if the journal is too small, UBIFS will treat it as * always full.
*/
tmp64 = (longlong)(c->jhead_cnt + 1) * c->leb_size + 1; if (c->bg_bud_bytes < tmp64)
c->bg_bud_bytes = tmp64; if (c->max_bud_bytes < tmp64 + c->leb_size)
c->max_bud_bytes = tmp64 + c->leb_size;
err = ubifs_calc_lpt_geom(c); if (err) return err;
/* Initialize effective LEB size used in budgeting calculations */
c->idx_leb_size = c->leb_size - c->max_idx_node_sz; return 0;
}
/* * init_constants_master - initialize UBIFS constants. * @c: UBIFS file-system description object * * This is a helper function which initializes various UBIFS constants after * the master node has been read. It also checks various UBIFS parameters and * makes sure they are all right.
*/ staticvoid init_constants_master(struct ubifs_info *c)
{ longlong tmp64;
/* * Calculate total amount of FS blocks. This number is not used * internally because it does not make much sense for UBIFS, but it is * necessary to report something for the 'statfs()' call. * * Subtract the LEB reserved for GC, the LEB which is reserved for * deletions, minimum LEBs for the index, the LEBs which are reserved * for each journal head.
*/
tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt;
tmp64 *= (longlong)c->leb_size - c->leb_overhead;
tmp64 = ubifs_reported_space(c, tmp64);
c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
}
/** * take_gc_lnum - reserve GC LEB. * @c: UBIFS file-system description object * * This function ensures that the LEB reserved for garbage collection is marked * as "taken" in lprops. We also have to set free space to LEB size and dirty * space to zero, because lprops may contain out-of-date information if the * file-system was un-mounted before it has been committed. This function * returns zero in case of success and a negative error code in case of * failure.
*/ staticint take_gc_lnum(struct ubifs_info *c)
{ int err;
if (c->gc_lnum == -1) {
ubifs_err(c, "no LEB for GC"); return -EINVAL;
}
/* And we have to tell lprops that this LEB is taken */
err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
LPROPS_TAKEN, 0, 0); return err;
}
/** * alloc_wbufs - allocate write-buffers. * @c: UBIFS file-system description object * * This helper function allocates and initializes UBIFS write-buffers. Returns * zero in case of success and %-ENOMEM in case of failure.
*/ staticint alloc_wbufs(struct ubifs_info *c)
{ int i, err;
c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead),
GFP_KERNEL); if (!c->jheads) return -ENOMEM;
/* Initialize journal heads */ for (i = 0; i < c->jhead_cnt; i++) {
INIT_LIST_HEAD(&c->jheads[i].buds_list);
err = ubifs_wbuf_init(c, &c->jheads[i].wbuf); if (err) goto out_wbuf;
/* * Garbage Collector head does not need to be synchronized by timer. * Also GC head nodes are not grouped.
*/
c->jheads[GCHD].wbuf.no_timer = 1;
c->jheads[GCHD].grouped = 0;
while (!list_empty(&c->orph_list)) {
orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
list_del(&orph->list);
kfree(orph);
ubifs_err(c, "orphan list not empty at unmount");
}
rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb) {
kfree(bud->log_hash);
kfree(bud);
}
}
/** * check_volume_empty - check if the UBI volume is empty. * @c: UBIFS file-system description object * * This function checks if the UBIFS volume is empty by looking if its LEBs are * mapped or not. The result of checking is stored in the @c->empty variable. * Returns zero in case of success and a negative error code in case of * failure.
*/ staticint check_volume_empty(struct ubifs_info *c)
{ int lnum, err;
/* * UBIFS mount options. * * Opt_fast_unmount: do not run a journal commit before un-mounting * Opt_norm_unmount: run a journal commit before un-mounting * Opt_bulk_read: enable bulk-reads * Opt_no_bulk_read: disable bulk-reads * Opt_chk_data_crc: check CRCs when reading data nodes * Opt_no_chk_data_crc: do not check CRCs when reading data nodes * Opt_override_compr: override default compressor * Opt_assert: set ubifs_assert() action * Opt_auth_key: The key name used for authentication * Opt_auth_hash_name: The hash type used for authentication * Opt_err: just end of array marker
*/ enum {
Opt_fast_unmount,
Opt_norm_unmount,
Opt_bulk_read,
Opt_no_bulk_read,
Opt_chk_data_crc,
Opt_no_chk_data_crc,
Opt_override_compr,
Opt_assert,
Opt_auth_key,
Opt_auth_hash_name,
Opt_ignore,
};
/** * ubifs_parse_param - parse a parameter. * @fc: the filesystem context * @param: the parameter to parse * * This function parses UBIFS mount options and returns zero in case success * and a negative error code in case of failure.
*/ staticint ubifs_parse_param(struct fs_context *fc, struct fs_parameter *param)
{ struct ubifs_fs_context *ctx = fc->fs_private; struct fs_parse_result result; bool is_remount = (fc->purpose & FS_CONTEXT_FOR_RECONFIGURE); int opt;
switch (opt) { /* * %Opt_fast_unmount and %Opt_norm_unmount options are ignored. * We accept them in order to be backward-compatible. But this * should be removed at some point.
*/ case Opt_fast_unmount:
ctx->mount_opts.unmount_mode = 2; break; case Opt_norm_unmount:
ctx->mount_opts.unmount_mode = 1; break; case Opt_bulk_read:
ctx->mount_opts.bulk_read = 2;
ctx->bulk_read = 1; break; case Opt_no_bulk_read:
ctx->mount_opts.bulk_read = 1;
ctx->bulk_read = 0; break; case Opt_chk_data_crc:
ctx->mount_opts.chk_data_crc = 2;
ctx->no_chk_data_crc = 0; break; case Opt_no_chk_data_crc:
ctx->mount_opts.chk_data_crc = 1;
ctx->no_chk_data_crc = 1; break; case Opt_override_compr:
ctx->mount_opts.compr_type = result.uint_32;
ctx->mount_opts.override_compr = 1;
ctx->default_compr = ctx->mount_opts.compr_type; break; case Opt_assert:
ctx->assert_action = result.uint_32; break; case Opt_auth_key: if (!is_remount) {
kfree(ctx->auth_key_name);
ctx->auth_key_name = param->string;
param->string = NULL;
} break; case Opt_auth_hash_name: if (!is_remount) {
kfree(ctx->auth_hash_name);
ctx->auth_hash_name = param->string;
param->string = NULL;
} break; case Opt_ignore: break;
}
return 0;
}
/* * ubifs_release_options - release mount parameters which have been dumped. * @c: UBIFS file-system description object
*/ staticvoid ubifs_release_options(struct ubifs_info *c)
{
kfree(c->auth_key_name);
c->auth_key_name = NULL;
kfree(c->auth_hash_name);
c->auth_hash_name = NULL;
}
/** * destroy_journal - destroy journal data structures. * @c: UBIFS file-system description object * * This function destroys journal data structures including those that may have * been created by recovery functions.
*/ staticvoid destroy_journal(struct ubifs_info *c)
{ while (!list_empty(&c->unclean_leb_list)) { struct ubifs_unclean_leb *ucleb;
/* Just disable bulk-read */
ubifs_warn(c, "cannot allocate %d bytes of memory for bulk-read, disabling it",
c->max_bu_buf_len);
c->mount_opts.bulk_read = 1;
c->bulk_read = 0; return;
}
}
/** * check_free_space - check if there is enough free space to mount. * @c: UBIFS file-system description object * * This function makes sure UBIFS has enough free space to be mounted in * read/write mode. UBIFS must always have some free space to allow deletions.
*/ staticint check_free_space(struct ubifs_info *c)
{
ubifs_assert(c, c->dark_wm > 0); if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) {
ubifs_err(c, "insufficient free space to mount in R/W mode");
ubifs_dump_budg(c, &c->bi);
ubifs_dump_lprops(c); return -ENOSPC;
} return 0;
}
/** * mount_ubifs - mount UBIFS file-system. * @c: UBIFS file-system description object * * This function mounts UBIFS file system. Returns zero in case of success and * a negative error code in case of failure.
*/ staticint mount_ubifs(struct ubifs_info *c)
{ int err; longlong x, y;
size_t sz;
c->ro_mount = !!sb_rdonly(c->vfs_sb); /* Suppress error messages while probing if SB_SILENT is set */
c->probing = !!(c->vfs_sb->s_flags & SB_SILENT);
err = init_constants_early(c); if (err) return err;
err = ubifs_debugging_init(c); if (err) return err;
err = ubifs_sysfs_register(c); if (err) goto out_debugging;
err = check_volume_empty(c); if (err) goto out_free;
if (c->empty && (c->ro_mount || c->ro_media)) { /* * This UBI volume is empty, and read-only, or the file system * is mounted read-only - we cannot format it.
*/
ubifs_err(c, "can't format empty UBI volume: read-only %s",
c->ro_media ? "UBI volume" : "mount");
err = -EROFS; goto out_free;
}
if (c->ro_media && !c->ro_mount) {
ubifs_err(c, "cannot mount read-write - read-only media");
err = -EROFS; goto out_free;
}
/* * The requirement for the buffer is that it should fit indexing B-tree * height amount of integers. We assume the height if the TNC tree will * never exceed 64.
*/
err = -ENOMEM;
c->bottom_up_buf = kmalloc_array(BOTTOM_UP_HEIGHT, sizeof(int),
GFP_KERNEL); if (!c->bottom_up_buf) goto out_free;
c->sbuf = vmalloc(c->leb_size); if (!c->sbuf) goto out_free;
if (!c->ro_mount) {
c->ileb_buf = vmalloc(c->leb_size); if (!c->ileb_buf) goto out_free;
}
if (c->bulk_read == 1)
bu_init(c);
if (!c->ro_mount) {
c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
UBIFS_CIPHER_BLOCK_SIZE,
GFP_KERNEL); if (!c->write_reserve_buf) goto out_free;
}
c->mounting = 1;
if (c->auth_key_name) { if (IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION)) {
err = ubifs_init_authentication(c); if (err) goto out_free;
} else {
ubifs_err(c, "auth_key_name, but UBIFS is built without" " authentication support");
err = -EINVAL; goto out_free;
}
}
err = ubifs_read_superblock(c); if (err) goto out_auth;
c->probing = 0;
/* * Make sure the compressor which is set as default in the superblock * or overridden by mount options is actually compiled in.
*/ if (!ubifs_compr_present(c, c->default_compr)) {
ubifs_err(c, "'compressor \"%s\" is not compiled in",
ubifs_compr_name(c, c->default_compr));
err = -ENOTSUPP; goto out_auth;
}
err = init_constants_sb(c); if (err) goto out_auth;
if (c->need_recovery && !c->ro_mount) {
err = ubifs_recover_inl_heads(c, c->sbuf); if (err) goto out_master;
}
err = ubifs_lpt_init(c, 1, !c->ro_mount); if (err) goto out_master;
if (!c->ro_mount && c->space_fixup) {
err = ubifs_fixup_free_space(c); if (err) goto out_lpt;
}
if (!c->ro_mount && !c->need_recovery) { /* * Set the "dirty" flag so that if we reboot uncleanly we * will notice this immediately on the next mount.
*/
c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
err = ubifs_write_master(c); if (err) goto out_lpt;
}
/* * Handle offline signed images: Now that the master node is * written and its validation no longer depends on the hash * in the superblock, we can update the offline signed * superblock with a HMAC version,
*/ if (ubifs_authenticated(c) && ubifs_hmac_zero(c, c->sup_node->hmac)) {
err = ubifs_hmac_wkm(c, c->sup_node->hmac_wkm); if (err) goto out_lpt;
c->superblock_need_write = 1;
}
if (!c->ro_mount && c->superblock_need_write) {
err = ubifs_write_sb_node(c, c->sup_node); if (err) goto out_lpt;
c->superblock_need_write = 0;
}
err = dbg_check_idx_size(c, c->bi.old_idx_sz); if (err) goto out_lpt;
err = ubifs_replay_journal(c); if (err) goto out_journal;
/* Calculate 'min_idx_lebs' after journal replay */
c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
err = ubifs_mount_orphans(c, c->need_recovery, c->ro_mount); if (err) goto out_orphans;
if (!c->ro_mount) { int lnum;
err = check_free_space(c); if (err) goto out_orphans;
/* Check for enough log space */
lnum = c->lhead_lnum + 1; if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
lnum = UBIFS_LOG_LNUM; if (lnum == c->ltail_lnum) {
err = ubifs_consolidate_log(c); if (err) goto out_orphans;
}
if (c->need_recovery) { if (!ubifs_authenticated(c)) {
err = ubifs_recover_size(c, true); if (err) goto out_orphans;
}
err = ubifs_rcvry_gc_commit(c); if (err) goto out_orphans;
if (ubifs_authenticated(c)) {
err = ubifs_recover_size(c, false); if (err) goto out_orphans;
}
} else {
err = take_gc_lnum(c); if (err) goto out_orphans;
/* * GC LEB may contain garbage if there was an unclean * reboot, and it should be un-mapped.
*/
err = ubifs_leb_unmap(c, c->gc_lnum); if (err) goto out_orphans;
}
err = dbg_check_lprops(c); if (err) goto out_orphans;
} elseif (c->need_recovery) {
err = ubifs_recover_size(c, false); if (err) goto out_orphans;
} else { /* * Even if we mount read-only, we have to set space in GC LEB * to proper value because this affects UBIFS free space * reporting. We do not want to have a situation when * re-mounting from R/O to R/W changes amount of free space.
*/
err = take_gc_lnum(c); if (err) goto out_orphans;
}
if (c->need_recovery) { if (c->ro_mount)
ubifs_msg(c, "recovery deferred"); else {
c->need_recovery = 0;
ubifs_msg(c, "recovery completed"); /* * GC LEB has to be empty and taken at this point. But * the journal head LEBs may also be accounted as * "empty taken" if they are empty.
*/
ubifs_assert(c, c->lst.taken_empty_lebs > 0);
}
} else
ubifs_assert(c, c->lst.taken_empty_lebs > 0);
err = dbg_check_filesystem(c); if (err) goto out_infos;
dbg_debugfs_init_fs(c);
c->mounting = 0;
ubifs_msg(c, "UBIFS: mounted UBI device %d, volume %d, name \"%s\"%s",
c->vi.ubi_num, c->vi.vol_id, c->vi.name,
c->ro_mount ? ", R/O mode" : "");
x = (longlong)c->main_lebs * c->leb_size;
y = (longlong)c->log_lebs * c->leb_size + c->max_bud_bytes;
ubifs_msg(c, "LEB size: %d bytes (%d KiB), min./max. I/O unit sizes: %d bytes/%d bytes",
c->leb_size, c->leb_size >> 10, c->min_io_size,
c->max_write_size);
ubifs_msg(c, "FS size: %lld bytes (%lld MiB, %d LEBs), max %d LEBs, journal size %lld bytes (%lld MiB, %d LEBs)",
x, x >> 20, c->main_lebs, c->max_leb_cnt,
y, y >> 20, c->log_lebs + c->max_bud_cnt);
ubifs_msg(c, "reserved for root: %llu bytes (%llu KiB)",
c->report_rp_size, c->report_rp_size >> 10);
ubifs_msg(c, "media format: w%d/r%d (latest is w%d/r%d), UUID %pUB%s",
c->fmt_version, c->ro_compat_version,
UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION, c->uuid,
c->big_lpt ? ", big LPT model" : ", small LPT model");
/** * ubifs_umount - un-mount UBIFS file-system. * @c: UBIFS file-system description object * * Note, this function is called to free allocated resourced when un-mounting, * as well as free resources when an error occurred while we were half way * through mounting (error path cleanup function). So it has to make sure the * resource was actually allocated before freeing it.
*/ staticvoid ubifs_umount(struct ubifs_info *c)
{
dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
c->vi.vol_id);
/** * ubifs_remount_rw - re-mount in read-write mode. * @c: UBIFS file-system description object * * UBIFS avoids allocating many unnecessary resources when mounted in read-only * mode. This function allocates the needed resources and re-mounts UBIFS in * read-write mode.
*/ staticint ubifs_remount_rw(struct ubifs_info *c)
{ int err, lnum;
if (c->rw_incompat) {
ubifs_err(c, "the file-system is not R/W-compatible");
ubifs_msg(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
c->fmt_version, c->ro_compat_version,
UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION); return -EROFS;
}
if (c->need_recovery) {
c->need_recovery = 0;
ubifs_msg(c, "deferred recovery completed");
} else { /* * Do not run the debugging space check if the were doing * recovery, because when we saved the information we had the * file-system in a state where the TNC and lprops has been * modified in memory, but all the I/O operations (including a * commit) were deferred. So the file-system was in * "non-committed" state. Now the file-system is in committed * state, and of course the amount of free space will change * because, for example, the old index size was imprecise.
*/
err = dbg_check_space_info(c);
}
/** * ubifs_remount_ro - re-mount in read-only mode. * @c: UBIFS file-system description object * * We assume VFS has stopped writing. Possibly the background thread could be * running a commit, however kthread_stop will wait in that case.
*/ staticvoid ubifs_remount_ro(struct ubifs_info *c)
{ int i, err;
ubifs_msg(c, "un-mount UBI device %d", c->vi.ubi_num);
/* * The following asserts are only valid if there has not been a failure * of the media. For example, there will be dirty inodes if we failed * to write them back because of I/O errors.
*/ if (!c->ro_error) {
ubifs_assert(c, c->bi.idx_growth == 0);
ubifs_assert(c, c->bi.dd_growth == 0);
ubifs_assert(c, c->bi.data_growth == 0);
}
/* * The 'c->umount_lock' prevents races between UBIFS memory shrinker * and file system un-mount. Namely, it prevents the shrinker from * picking this superblock for shrinking - it will be just skipped if * the mutex is locked.
*/
mutex_lock(&c->umount_mutex); if (!c->ro_mount) { /* * First of all kill the background thread to make sure it does * not interfere with un-mounting and freeing resources.
*/ if (c->bgt) {
kthread_stop(c->bgt);
c->bgt = NULL;
}
/* * On fatal errors c->ro_error is set to 1, in which case we do * not write the master node.
*/ if (!c->ro_error) { int err;
/* Synchronize write-buffers */ for (i = 0; i < c->jhead_cnt; i++) {
err = ubifs_wbuf_sync(&c->jheads[i].wbuf); if (err)
ubifs_ro_mode(c, err);
}
/* * We are being cleanly unmounted which means the * orphans were killed - indicate this in the master * node. Also save the reserved GC LEB number.
*/
c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
err = ubifs_write_master(c); if (err) /* * Recovery will attempt to fix the master area * next mount, so we just print a message and * continue to unmount normally.
*/
ubifs_err(c, "failed to write master node, error %d",
err);
} else { for (i = 0; i < c->jhead_cnt; i++) /* Make sure write-buffer timers are canceled */
hrtimer_cancel(&c->jheads[i].wbuf.timer);
}
}
/** * open_ubi - parse UBI device name string and open the UBI device. * @fc: The filesystem context * @mode: UBI volume open mode * * The primary method of mounting UBIFS is by specifying the UBI volume * character device node path. However, UBIFS may also be mounted without any * character device node using one of the following methods: * * o ubiX_Y - mount UBI device number X, volume Y; * o ubiY - mount UBI device number 0, volume Y; * o ubiX:NAME - mount UBI device X, volume with name NAME; * o ubi:NAME - mount UBI device 0, volume with name NAME. * * Alternative '!' separator may be used instead of ':' (because some shells * like busybox may interpret ':' as an NFS host name separator). This function * returns UBI volume description object in case of success and a negative * error code in case of failure.
*/ staticstruct ubi_volume_desc *open_ubi(struct fs_context *fc, int mode)
{ struct ubi_volume_desc *ubi; constchar *name = fc->source; int dev, vol; char *endptr;
/* First, try to open using the device node path method */
ubi = ubi_open_volume_path(name, mode); if (!IS_ERR(ubi)) return ubi;
/* Try the "nodev" method */ if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i') goto invalid_source;
/* * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For * UBIFS, I/O is not deferred, it is done immediately in read_folio, * which means the user would have to wait not just for their own I/O * but the read-ahead I/O as well i.e. completely pointless. * * Read-ahead will be disabled because @sb->s_bdi->ra_pages is 0. Also * @sb->s_bdi->capabilities are initialized to 0 so there won't be any * writeback happening.
*/
err = super_setup_bdi_name(sb, "ubifs_%d_%d", c->vi.ubi_num,
c->vi.vol_id); if (err) goto out_close;
sb->s_bdi->ra_pages = 0;
sb->s_bdi->io_pages = 0;
/* * Get UBI device number and volume ID. Mount it read-only so far * because this might be a new mount point, and UBI allows only one * read-write user at a time.
*/
ubi = open_ubi(fc, UBI_READONLY); if (IS_ERR(ubi)) {
err = PTR_ERR(ubi); if (!(fc->sb_flags & SB_SILENT))
pr_err("UBIFS error (pid: %d): cannot open \"%s\", error %d",
current->pid, fc->source, err); return err;
}
c = alloc_ubifs_info(ubi); if (!c) {
err = -ENOMEM; goto out_close;
}
fc->s_fs_info = c;
if (sb->s_root) { struct ubifs_info *c1 = sb->s_fs_info;
kfree(c); /* A new mount point for already mounted UBIFS */
dbg_gen("this ubi volume is already mounted"); if (!!(fc->sb_flags & SB_RDONLY) != c1->ro_mount) {
err = -EBUSY; goto out_deact;
}
} else {
err = ubifs_fill_super(sb, fc); if (err) goto out_deact; /* We do not support atime */
sb->s_flags |= SB_ACTIVE; if (IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
ubifs_msg(c, "full atime support is enabled."); else
sb->s_flags |= SB_NOATIME;
}
/* 'fill_super()' opens ubi again so we must close it here */
ubi_close_volume(ubi);
/* * We use 2 bit wide bit-fields to store compression type, which should * be amended if more compressors are added. The bit-fields are: * @compr_type in 'struct ubifs_inode', @default_compr in * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
*/
BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
/* * We require that PAGE_SIZE is greater-than-or-equal-to * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
*/ if (PAGE_SIZE < UBIFS_BLOCK_SIZE) {
pr_err("UBIFS error (pid %d): VFS page cache size is %u bytes, but UBIFS requires at least 4096 bytes",
current->pid, (unsignedint)PAGE_SIZE); return -EINVAL;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.