^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) International Business Machines Corp., 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) Nokia Corporation, 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Artem Bityutskiy (Битюцкий Артём),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Frank Haverkamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This file includes UBI initialization and building of UBI devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * When UBI is initialized, it attaches all the MTD devices specified as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * module load parameters or the kernel boot parameters. If MTD devices were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * specified, UBI does not attach any MTD device, but it is possible to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * later using the "UBI control device".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mtd/partitions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "ubi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Maximum length of the 'mtd=' parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define MTD_PARAM_LEN_MAX 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Maximum number of comma-separated items in the 'mtd=' parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define MTD_PARAM_MAX_COUNT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Maximum value for the number of bad PEBs per 1024 PEBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define MAX_MTD_UBI_BEB_LIMIT 768
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #ifdef CONFIG_MTD_UBI_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define ubi_is_module() 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define ubi_is_module() 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * struct mtd_dev_param - MTD device parameter description data structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @name: MTD character device node path, MTD device name, or MTD device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @vid_hdr_offs: VID header offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct mtd_dev_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) char name[MTD_PARAM_LEN_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int vid_hdr_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int max_beb_per1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* Numbers of elements set in the @mtd_dev_param array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int mtd_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* MTD devices specification parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* UBI module parameter to enable fastmap automatically on non-fastmap images */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static bool fm_autoconvert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static bool fm_debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* Slab cache for wear-leveling entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct kmem_cache *ubi_wl_entry_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* UBI control character device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static struct miscdevice ubi_ctrl_cdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .name = "ubi_ctrl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .fops = &ubi_ctrl_cdev_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* All UBI devices in system */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* Serializes UBI devices creations and removals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) DEFINE_MUTEX(ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* Protects @ubi_devices and @ubi->ref_count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static DEFINE_SPINLOCK(ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* "Show" method for files in '/<sysfs>/class/ubi/' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* UBI version attribute ('/<sysfs>/class/ubi/version') */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static ssize_t version_show(struct class *class, struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return sprintf(buf, "%d\n", UBI_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static CLASS_ATTR_RO(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static struct attribute *ubi_class_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) &class_attr_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ATTRIBUTE_GROUPS(ubi_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct class ubi_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .name = UBI_NAME_STR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .class_groups = ubi_class_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static ssize_t dev_attribute_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct device_attribute *attr, char *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static struct device_attribute dev_eraseblock_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct device_attribute dev_avail_eraseblocks =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static struct device_attribute dev_total_eraseblocks =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct device_attribute dev_volumes_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static struct device_attribute dev_max_ec =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static struct device_attribute dev_reserved_for_bad =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static struct device_attribute dev_bad_peb_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static struct device_attribute dev_max_vol_count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct device_attribute dev_min_io_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static struct device_attribute dev_bgt_enabled =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static struct device_attribute dev_mtd_num =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct device_attribute dev_ro_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) __ATTR(ro_mode, S_IRUGO, dev_attribute_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * ubi_volume_notify - send a volume change notification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * @vol: volume description object of the changed volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * This is a helper function which notifies all subscribers about a volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * change event (creation, removal, re-sizing, re-naming, updating). Returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * zero in case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct ubi_notification nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ubi_do_get_device_info(ubi, &nt.di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ubi_do_get_volume_info(ubi, vol, &nt.vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) switch (ntype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) case UBI_VOLUME_ADDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) case UBI_VOLUME_REMOVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) case UBI_VOLUME_RESIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) case UBI_VOLUME_RENAMED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = ubi_update_fastmap(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * ubi_notify_all - send a notification to all volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @nb: the notifier to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * This function walks all volumes of UBI device @ubi and sends the @ntype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * notification for each volume. If @nb is %NULL, then all registered notifiers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * are called, otherwise only the @nb notifier is called. Returns the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * sent notifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct ubi_notification nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int i, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ubi_do_get_device_info(ubi, &nt.di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) mutex_lock(&ubi->device_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) for (i = 0; i < ubi->vtbl_slots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * Since the @ubi->device is locked, and we are not going to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * change @ubi->volumes, we do not have to lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @ubi->volumes_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!ubi->volumes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) nb->notifier_call(nb, ntype, &nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) blocking_notifier_call_chain(&ubi_notifiers, ntype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) &nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) mutex_unlock(&ubi->device_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * ubi_enumerate_volumes - send "add" notification for all existing volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * @nb: the notifier to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * This function walks all UBI devices and volumes and sends the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * registered notifiers are called, otherwise only the @nb notifier is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * Returns the number of sent notifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int ubi_enumerate_volumes(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int i, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Since the @ubi_devices_mutex is locked, and we are not going to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * change @ubi_devices, we do not have to lock @ubi_devices_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) for (i = 0; i < UBI_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct ubi_device *ubi = ubi_devices[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * ubi_get_device - get UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @ubi_num: UBI device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * This function returns UBI device description object for UBI device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * @ubi_num, or %NULL if the device does not exist. This function increases the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * device reference count to prevent removal of the device. In other words, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * device cannot be removed if its reference count is not zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct ubi_device *ubi_get_device(int ubi_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) spin_lock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) ubi = ubi_devices[ubi_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (ubi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ubi_assert(ubi->ref_count >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ubi->ref_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) get_device(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * ubi_put_device - drop an UBI device reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) void ubi_put_device(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) spin_lock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ubi->ref_count -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) put_device(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * ubi_get_by_major - get UBI device by character device major number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @major: major number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * This function is similar to 'ubi_get_device()', but it searches the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * by its major number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct ubi_device *ubi_get_by_major(int major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) spin_lock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) for (i = 0; i < UBI_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ubi = ubi_devices[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (ubi && MAJOR(ubi->cdev.dev) == major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ubi_assert(ubi->ref_count >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ubi->ref_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) get_device(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * ubi_major2num - get UBI device number by character device major number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * @major: major number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * This function searches UBI device number object by its major number. If UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * device was not found, this function returns -ENODEV, otherwise the UBI device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * number is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int ubi_major2num(int major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int i, ubi_num = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) spin_lock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) for (i = 0; i < UBI_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct ubi_device *ubi = ubi_devices[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (ubi && MAJOR(ubi->cdev.dev) == major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ubi_num = ubi->ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static ssize_t dev_attribute_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * The below code looks weird, but it actually makes sense. We get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * UBI device reference from the contained 'struct ubi_device'. But it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * is unclear if the device was removed or not yet. Indeed, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * device was removed before we increased its reference count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * 'ubi_get_device()' will return -ENODEV and we fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Remember, 'struct ubi_device' is freed in the release function, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * we still can use 'ubi->ubi_num'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ubi = container_of(dev, struct ubi_device, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (attr == &dev_eraseblock_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ret = sprintf(buf, "%d\n", ubi->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) else if (attr == &dev_avail_eraseblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = sprintf(buf, "%d\n", ubi->avail_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) else if (attr == &dev_total_eraseblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ret = sprintf(buf, "%d\n", ubi->good_peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) else if (attr == &dev_volumes_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) else if (attr == &dev_max_ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ret = sprintf(buf, "%d\n", ubi->max_ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) else if (attr == &dev_reserved_for_bad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) else if (attr == &dev_bad_peb_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) else if (attr == &dev_max_vol_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) else if (attr == &dev_min_io_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = sprintf(buf, "%d\n", ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) else if (attr == &dev_bgt_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) ret = sprintf(buf, "%d\n", ubi->thread_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) else if (attr == &dev_mtd_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret = sprintf(buf, "%d\n", ubi->mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) else if (attr == &dev_ro_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = sprintf(buf, "%d\n", ubi->ro_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static struct attribute *ubi_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) &dev_eraseblock_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) &dev_avail_eraseblocks.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) &dev_total_eraseblocks.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) &dev_volumes_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) &dev_max_ec.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) &dev_reserved_for_bad.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) &dev_bad_peb_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) &dev_max_vol_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) &dev_min_io_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) &dev_bgt_enabled.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) &dev_mtd_num.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) &dev_ro_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ATTRIBUTE_GROUPS(ubi_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static void dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) kfree(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * kill_volumes - destroy all user volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static void kill_volumes(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) for (i = 0; i < ubi->vtbl_slots; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (ubi->volumes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ubi_free_volume(ubi, ubi->volumes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * uif_init - initialize user interfaces for an UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * This function initializes various user interfaces for an UBI device. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * initialization fails at an early stage, this function frees all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * resources it allocated, returns an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static int uif_init(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) dev_t dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * Major numbers for the UBI character devices are allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * dynamically. Major numbers of volume character devices are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * equivalent to ones of the corresponding UBI character device. Minor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * numbers of UBI character devices are 0, while minor numbers of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * volume character devices start from 1. Thus, we allocate one major
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * number and ubi->vtbl_slots + 1 minor numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ubi_err(ubi, "cannot register UBI character devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) ubi->dev.devt = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ubi_assert(MINOR(dev) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) cdev_init(&ubi->cdev, &ubi_cdev_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) ubi->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dev_set_name(&ubi->dev, UBI_NAME_STR "%d", ubi->ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) err = cdev_device_add(&ubi->cdev, &ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) goto out_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) for (i = 0; i < ubi->vtbl_slots; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (ubi->volumes[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) err = ubi_add_volume(ubi, ubi->volumes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ubi_err(ubi, "cannot add volume %d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) goto out_volumes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) out_volumes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) kill_volumes(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) cdev_device_del(&ubi->cdev, &ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) out_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) ubi_err(ubi, "cannot initialize UBI %s, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ubi->ubi_name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * uif_close - close user interfaces for an UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * Note, since this function un-registers UBI volume device objects (@vol->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * the memory allocated voe the volumes is freed as well (in the release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * function).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static void uif_close(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) kill_volumes(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) cdev_device_del(&ubi->cdev, &ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * ubi_free_volumes_from - free volumes from specific index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * @from: the start index used for volume free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static void ubi_free_volumes_from(struct ubi_device *ubi, int from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (!ubi->volumes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) ubi_eba_replace_table(ubi->volumes[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) kfree(ubi->volumes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) ubi->volumes[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * ubi_free_all_volumes - free all volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) void ubi_free_all_volumes(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) ubi_free_volumes_from(ubi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * ubi_free_internal_volumes - free internal volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) void ubi_free_internal_volumes(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) ubi_free_volumes_from(ubi, ubi->vtbl_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) int limit, device_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) uint64_t device_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (!max_beb_per1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * Since max_beb_per1024 has not been set by the user in either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * the cmdline or Kconfig, use mtd_max_bad_blocks to set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * limit if it is supported by the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) limit = mtd_max_bad_blocks(ubi->mtd, 0, ubi->mtd->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (limit < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * Here we are using size of the entire flash chip and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * not just the MTD partition size because the maximum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * number of bad eraseblocks is a percentage of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * whole device and bad eraseblocks are not fairly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * distributed over the flash chip. So the worst case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * is that all the bad eraseblocks of the chip are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * the MTD partition we are attaching (ubi->mtd).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) device_size = mtd_get_device_size(ubi->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) limit = mult_frac(device_pebs, max_beb_per1024, 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) /* Round it up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) limit += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * io_init - initialize I/O sub-system for a given UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * assumed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * o EC header is always at offset zero - this cannot be changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * o VID header starts just after the EC header at the closest address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * aligned to @io->hdrs_min_io_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * o data starts just after the VID header at the closest address aligned to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * @io->min_io_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static int io_init(struct ubi_device *ubi, int max_beb_per1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (ubi->mtd->numeraseregions != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * Some flashes have several erase regions. Different regions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * may have different eraseblock size and other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * characteristics. It looks like mostly multi-region flashes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * have one "main" region and one or more small regions to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * store boot loader code or boot parameters or whatever. I
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * guess we should just pick the largest region. But this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * not implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) ubi_err(ubi, "multiple regions, not implemented");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (ubi->vid_hdr_offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * Note, in this implementation we support MTD devices with 0x7FFFFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * physical eraseblocks maximum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) ubi->peb_size = ubi->mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ubi->flash_size = ubi->mtd->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (mtd_can_have_bb(ubi->mtd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) ubi->bad_allowed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (ubi->mtd->type == MTD_NORFLASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) ubi_assert(ubi->mtd->writesize == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) ubi->nor_flash = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) ubi->min_io_size = ubi->mtd->writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * Make sure minimal I/O unit is power of 2. Note, there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * fundamental reason for this assumption. It is just an optimization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * which allows us to avoid costly division operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (!is_power_of_2(ubi->min_io_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) ubi_assert(ubi->hdrs_min_io_size > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ubi->max_write_size = ubi->mtd->writebufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * Maximum write size has to be greater or equivalent to min. I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * size, and be multiple of min. I/O size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (ubi->max_write_size < ubi->min_io_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) ubi->max_write_size % ubi->min_io_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) !is_power_of_2(ubi->max_write_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ubi->max_write_size, ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) /* Calculate default aligned sizes of EC and VID headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) dbg_gen("min_io_size %d", ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) dbg_gen("max_write_size %d", ubi->max_write_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (ubi->vid_hdr_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /* Default offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) ubi->ec_hdr_alsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ~(ubi->hdrs_min_io_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) ubi->vid_hdr_shift = ubi->vid_hdr_offset -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ubi->vid_hdr_aloffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* Similar for the data offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) dbg_gen("leb_start %d", ubi->leb_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /* The shift must be aligned to 32-bit boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (ubi->vid_hdr_shift % 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) ubi_err(ubi, "unaligned VID header shift %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ubi->vid_hdr_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* Check sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) ubi->leb_start & (ubi->min_io_size - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) ubi->vid_hdr_offset, ubi->leb_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * Set maximum amount of physical erroneous eraseblocks to be 10%.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * Erroneous PEB are those which have read errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) ubi->max_erroneous = ubi->peb_count / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (ubi->max_erroneous < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) ubi->max_erroneous = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) dbg_gen("max_erroneous %d", ubi->max_erroneous);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * It may happen that EC and VID headers are situated in one minimal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * I/O unit. In this case we can only accept this UBI image in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * read-only mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) ubi->ro_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ubi->leb_size = ubi->peb_size - ubi->leb_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) ubi->mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) ubi->ro_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * unfortunately, MTD does not provide this information. We should loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * over all physical eraseblocks and invoke mtd->block_is_bad() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * each physical eraseblock. So, we leave @ubi->bad_peb_count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * uninitialized so far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * autoresize - re-size the volume which has the "auto-resize" flag set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * @vol_id: ID of the volume to re-size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * the volume table to the largest possible size. See comments in ubi-header.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * for more description of the flag. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) static int autoresize(struct ubi_device *ubi, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) struct ubi_volume_desc desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) struct ubi_volume *vol = ubi->volumes[vol_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) int err, old_reserved_pebs = vol->reserved_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (ubi->ro_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) ubi_warn(ubi, "skip auto-resize because of R/O mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * Clear the auto-resize flag in the volume in-memory copy of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * volume table, and 'ubi_resize_volume()' will propagate this change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * to the flash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (ubi->avail_pebs == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) struct ubi_vtbl_record vtbl_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * No available PEBs to re-size the volume, clear the flag on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * flash and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) vtbl_rec = ubi->vtbl[vol_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) desc.vol = vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) err = ubi_resize_volume(&desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) old_reserved_pebs + ubi->avail_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) ubi_err(ubi, "cannot auto-resize volume %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * ubi_attach_mtd_dev - attach an MTD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * @mtd: MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * @ubi_num: number to assign to the new UBI device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * @vid_hdr_offset: VID header offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * which case this function finds a vacant device number and assigns it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * automatically. Returns the new UBI device number in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * Note, the invocations of this function has to be serialized by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * @ubi_devices_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) int vid_hdr_offset, int max_beb_per1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (!max_beb_per1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * Check if we already have the same MTD device attached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * Note, this function assumes that UBI devices creations and deletions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * are serialized, so it does not take the &ubi_devices_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) for (i = 0; i < UBI_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) ubi = ubi_devices[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) if (ubi && mtd->index == ubi->mtd->index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) pr_err("ubi: mtd%d is already attached to ubi%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) mtd->index, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * Make sure this MTD device is not emulated on top of an UBI volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * already. Well, generally this recursion works fine, but there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * different problems like the UBI module takes a reference to itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * by attaching (and thus, opening) the emulated MTD device. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * results in inability to unload the module. And in general it makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * no sense to attach emulated MTD devices, so we prohibit this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (mtd->type == MTD_UBIVOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) pr_err("ubi: refuse attaching mtd%d - it is already emulated on top of UBI\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * Both UBI and UBIFS have been designed for SLC NAND and NOR flashes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * MLC NAND is different and needs special care, otherwise UBI or UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * will die soon and you will lose all your data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * Relax this rule if the partition we're attaching to operates in SLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (mtd->type == MTD_MLCNANDFLASH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) !(mtd->flags & MTD_SLC_ON_MLC_EMULATION)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) pr_err("ubi: refuse attaching mtd%d - MLC NAND is not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (ubi_num == UBI_DEV_NUM_AUTO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) /* Search for an empty slot in the @ubi_devices array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (!ubi_devices[ubi_num])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (ubi_num == UBI_MAX_DEVICES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) pr_err("ubi: only %d UBI devices may be created\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) UBI_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) if (ubi_num >= UBI_MAX_DEVICES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) /* Make sure ubi_num is not busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (ubi_devices[ubi_num]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) pr_err("ubi: ubi%i already exists\n", ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) device_initialize(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) ubi->dev.release = dev_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) ubi->dev.class = &ubi_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) ubi->dev.groups = ubi_dev_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ubi->mtd = mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) ubi->ubi_num = ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) ubi->vid_hdr_offset = vid_hdr_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) ubi->autoresize_vol_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) ubi->fm_pool.used = ubi->fm_pool.size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * fm_pool.max_size is 5% of the total number of PEBs but it's also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) UBI_FM_MIN_POOL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) ubi->fm_disabled = !fm_autoconvert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) if (fm_debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) ubi_enable_dbg_chk_fastmap(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) <= UBI_FM_MAX_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) UBI_FM_MAX_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) ubi->fm_disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) ubi_msg(ubi, "default fastmap WL pool size: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) ubi->fm_wl_pool.max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) ubi->fm_disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) mutex_init(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) mutex_init(&ubi->ckvol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) mutex_init(&ubi->device_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) spin_lock_init(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) init_rwsem(&ubi->fm_protect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) init_rwsem(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) ubi_msg(ubi, "attaching mtd%d", mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) err = io_init(ubi, max_beb_per1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) ubi->peb_buf = vmalloc(ubi->peb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) if (!ubi->peb_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) ubi->fm_size = ubi_calc_fm_size(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) ubi->fm_buf = vzalloc(ubi->fm_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (!ubi->fm_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) err = ubi_attach(ubi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) ubi_err(ubi, "failed to attach mtd%d, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) mtd->index, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (ubi->autoresize_vol_id != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) err = autoresize(ubi, ubi->autoresize_vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) goto out_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) err = uif_init(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) goto out_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) err = ubi_debugfs_init_dev(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) goto out_uif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (IS_ERR(ubi->bgt_thread)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) err = PTR_ERR(ubi->bgt_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) ubi_err(ubi, "cannot spawn \"%s\", error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) ubi->bgt_name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) goto out_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) mtd->index, mtd->name, ubi->flash_size >> 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) ubi->vtbl_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) ubi->image_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) * The below lock makes sure we do not race with 'ubi_thread()' which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) spin_lock(&ubi->wl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) ubi->thread_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) wake_up_process(ubi->bgt_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) spin_unlock(&ubi->wl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ubi_devices[ubi_num] = ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) out_debugfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) ubi_debugfs_exit_dev(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) out_uif:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) uif_close(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) out_detach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) ubi_wl_close(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) ubi_free_all_volumes(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) vfree(ubi->vtbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) vfree(ubi->peb_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) vfree(ubi->fm_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) put_device(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) * ubi_detach_mtd_dev - detach an MTD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) * @ubi_num: UBI device number to detach from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) * @anyway: detach MTD even if device reference count is not zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * This function destroys an UBI device number @ubi_num and detaches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * underlying MTD device. Returns zero in case of success and %-EBUSY if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * Note, the invocations of this function has to be serialized by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * @ubi_devices_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) int ubi_detach_mtd_dev(int ubi_num, int anyway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) ubi = ubi_get_device(ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) spin_lock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) put_device(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) ubi->ref_count -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) if (ubi->ref_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) if (!anyway) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /* This may only happen if there is a bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) ubi_err(ubi, "%s reference count %d, destroy anyway",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) ubi->ubi_name, ubi->ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) ubi_devices[ubi_num] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) spin_unlock(&ubi_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) ubi_assert(ubi_num == ubi->ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /* If we don't write a new fastmap at detach time we lose all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * EC updates that have been made since the last written fastmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * In case of fastmap debugging we omit the update to simulate an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) * unclean shutdown. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (!ubi_dbg_chk_fastmap(ubi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) ubi_update_fastmap(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) * Before freeing anything, we have to stop the background thread to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * prevent it from doing anything on this device while we are freeing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (ubi->bgt_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) kthread_stop(ubi->bgt_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) cancel_work_sync(&ubi->fm_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) ubi_debugfs_exit_dev(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) uif_close(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) ubi_wl_close(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) ubi_free_internal_volumes(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) vfree(ubi->vtbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) vfree(ubi->peb_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) vfree(ubi->fm_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) put_mtd_device(ubi->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) put_device(&ubi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) * open_mtd_by_chdev - open an MTD device by its character device node path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) * @mtd_dev: MTD character device node path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) * This helper function opens an MTD device by its character node device path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) * Returns MTD device description object in case of success and a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) * error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) int err, minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) /* Probably this is an MTD character device node path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) /* MTD device number is defined by the major / minor numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (MAJOR(stat.rdev) != MTD_CHAR_MAJOR || !S_ISCHR(stat.mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) minor = MINOR(stat.rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (minor & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * Just do not think the "/dev/mtdrX" devices support is need,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * so do not support them to avoid doing extra work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) return get_mtd_device(NULL, minor / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) * open_mtd_device - open MTD device by name, character device path, or number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) * @mtd_dev: name, character device node path, or MTD device device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) * This function tries to open and MTD device described by @mtd_dev string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * which is first treated as ASCII MTD device number, and if it is not true, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * is treated as MTD device name, and if that is also not true, it is treated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * as MTD character device node path. Returns MTD device description object in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) int mtd_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) mtd_num = simple_strtoul(mtd_dev, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) if (*endp != '\0' || mtd_dev == endp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * This does not look like an ASCII integer, probably this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) * MTD device name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) mtd = get_mtd_device_nm(mtd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (PTR_ERR(mtd) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) /* Probably this is an MTD character device node path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) mtd = open_mtd_by_chdev(mtd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) mtd = get_mtd_device(NULL, mtd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) return mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) static int __init ubi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) int err, i, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) /* Ensure that EC and VID headers have correct size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) if (mtd_devs > UBI_MAX_DEVICES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) pr_err("UBI error: too many MTD devices, maximum is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) UBI_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) /* Create base sysfs directory and sysfs files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) err = class_register(&ubi_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) err = misc_register(&ubi_ctrl_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) pr_err("UBI error: cannot register device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) sizeof(struct ubi_wl_entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (!ubi_wl_entry_slab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) goto out_dev_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) err = ubi_debugfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) goto out_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /* Attach MTD devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) for (i = 0; i < mtd_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) struct mtd_dev_param *p = &mtd_dev_param[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) mtd = open_mtd_device(p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if (IS_ERR(mtd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) err = PTR_ERR(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) pr_err("UBI error: cannot open mtd %s, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) p->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /* See comment below re-ubi_is_module(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) if (ubi_is_module())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) goto out_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) mutex_lock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) err = ubi_attach_mtd_dev(mtd, p->ubi_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) p->vid_hdr_offs, p->max_beb_per1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) mutex_unlock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) pr_err("UBI error: cannot attach mtd%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) put_mtd_device(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) * Originally UBI stopped initializing on any error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) * However, later on it was found out that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) * behavior is not very good when UBI is compiled into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) * the kernel and the MTD devices to attach are passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) * through the command line. Indeed, UBI failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) * stopped whole boot sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * To fix this, we changed the behavior for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * non-module case, but preserved the old behavior for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) * the module case, just for compatibility. This is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) * little inconsistent, though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) if (ubi_is_module())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) goto out_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) err = ubiblock_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) pr_err("UBI error: block: cannot initialize, error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) /* See comment above re-ubi_is_module(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) if (ubi_is_module())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) goto out_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) out_detach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) for (k = 0; k < i; k++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) if (ubi_devices[k]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) mutex_lock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) mutex_unlock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) ubi_debugfs_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) out_slab:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) kmem_cache_destroy(ubi_wl_entry_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) out_dev_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) misc_deregister(&ubi_ctrl_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) class_unregister(&ubi_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) pr_err("UBI error: cannot initialize UBI, error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) late_initcall(ubi_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) static void __exit ubi_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) ubiblock_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) for (i = 0; i < UBI_MAX_DEVICES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (ubi_devices[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) mutex_lock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) mutex_unlock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) ubi_debugfs_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) kmem_cache_destroy(ubi_wl_entry_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) misc_deregister(&ubi_ctrl_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) class_unregister(&ubi_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) module_exit(ubi_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) * bytes_str_to_int - convert a number of bytes string into an integer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) * @str: the string to convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) * This function returns positive resulting integer in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) static int bytes_str_to_int(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) unsigned long result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) result = simple_strtoul(str, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) if (str == endp || result >= INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) switch (*endp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) case 'G':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) result *= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) case 'M':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) result *= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) case 'K':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) result *= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) if (endp[1] == 'i' && endp[2] == 'B')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) endp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) case '\0':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) * @val: the parameter value to parse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) * @kp: not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) * case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) static int ubi_mtd_param_parse(const char *val, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) int i, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) struct mtd_dev_param *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) char buf[MTD_PARAM_LEN_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) char *pbuf = &buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) char *tokens[MTD_PARAM_MAX_COUNT], *token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (mtd_devs == UBI_MAX_DEVICES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) pr_err("UBI error: too many parameters, max. is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) UBI_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) len = strnlen(val, MTD_PARAM_LEN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) if (len == MTD_PARAM_LEN_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) val, MTD_PARAM_LEN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) strcpy(buf, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) /* Get rid of the final newline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (buf[len - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) tokens[i] = strsep(&pbuf, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (pbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) pr_err("UBI error: too many arguments at \"%s\"\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) p = &mtd_dev_param[mtd_devs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) strcpy(&p->name[0], tokens[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) token = tokens[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) if (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) p->vid_hdr_offs = bytes_str_to_int(token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (p->vid_hdr_offs < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) return p->vid_hdr_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) token = tokens[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) int err = kstrtoint(token, 10, &p->max_beb_per1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) token = tokens[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) int err = kstrtoint(token, 10, &p->ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) pr_err("UBI error: bad value for ubi_num parameter: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) p->ubi_num = UBI_DEV_NUM_AUTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) mtd_devs += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) "Multiple \"mtd\" parameters may be specified.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) "Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) "Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and reserve 25*nand_size_in_blocks/1024 erase blocks for bad block handling.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) module_param(fm_autoconvert, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) module_param(fm_debug, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) MODULE_VERSION(__stringify(UBI_VERSION));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) MODULE_DESCRIPTION("UBI - Unsorted Block Images");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) MODULE_AUTHOR("Artem Bityutskiy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);