⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmt.c

📁 基于linux-2.6.28的mtd驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) International Business Machines Corp., 2006 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation;  either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Artem Bityutskiy (Битюцкий Артём) *//* * This file contains implementation of volume creation, deletion, updating and * resizing. */#include <linux/err.h>#include <asm/div64.h>#include "ubi.h"#ifdef CONFIG_MTD_UBI_DEBUG_PARANOIDstatic int paranoid_check_volumes(struct ubi_device *ubi);#else#define paranoid_check_volumes(ubi) 0#endifstatic ssize_t vol_attribute_show(struct device *dev,				  struct device_attribute *attr, char *buf);/* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */static struct device_attribute attr_vol_reserved_ebs =	__ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_type =	__ATTR(type, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_name =	__ATTR(name, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_corrupted =	__ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_alignment =	__ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_usable_eb_size =	__ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_data_bytes =	__ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);static struct device_attribute attr_vol_upd_marker =	__ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);/* * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'. * * Consider a situation: * A. process 1 opens a sysfs file related to volume Y, say *    /<sysfs>/class/ubi/ubiX_Y/reserved_ebs; * B. process 2 removes volume Y; * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file; * * In this situation, this function will return %-ENODEV because it will find * out that the volume was removed from the @ubi->volumes array. */static ssize_t vol_attribute_show(struct device *dev,				  struct device_attribute *attr, char *buf){	int ret;	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);	struct ubi_device *ubi;	ubi = ubi_get_device(vol->ubi->ubi_num);	if (!ubi)		return -ENODEV;	spin_lock(&ubi->volumes_lock);	if (!ubi->volumes[vol->vol_id]) {		spin_unlock(&ubi->volumes_lock);		ubi_put_device(ubi);		return -ENODEV;	}	/* Take a reference to prevent volume removal */	vol->ref_count += 1;	spin_unlock(&ubi->volumes_lock);	if (attr == &attr_vol_reserved_ebs)		ret = sprintf(buf, "%d\n", vol->reserved_pebs);	else if (attr == &attr_vol_type) {		const char *tp;		if (vol->vol_type == UBI_DYNAMIC_VOLUME)			tp = "dynamic";		else			tp = "static";		ret = sprintf(buf, "%s\n", tp);	} else if (attr == &attr_vol_name)		ret = sprintf(buf, "%s\n", vol->name);	else if (attr == &attr_vol_corrupted)		ret = sprintf(buf, "%d\n", vol->corrupted);	else if (attr == &attr_vol_alignment)		ret = sprintf(buf, "%d\n", vol->alignment);	else if (attr == &attr_vol_usable_eb_size)		ret = sprintf(buf, "%d\n", vol->usable_leb_size);	else if (attr == &attr_vol_data_bytes)		ret = sprintf(buf, "%lld\n", vol->used_bytes);	else if (attr == &attr_vol_upd_marker)		ret = sprintf(buf, "%d\n", vol->upd_marker);	else		/* This must be a bug */		ret = -EINVAL;	/* We've done the operation, drop volume and UBI device references */	spin_lock(&ubi->volumes_lock);	vol->ref_count -= 1;	ubi_assert(vol->ref_count >= 0);	spin_unlock(&ubi->volumes_lock);	ubi_put_device(ubi);	return ret;}/* Release method for volume devices */static void vol_release(struct device *dev){	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);	kfree(vol->eba_tbl);	kfree(vol);}/** * volume_sysfs_init - initialize sysfs for new volume. * @ubi: UBI device description object * @vol: volume description object * * This function returns zero in case of success and a negative error code in * case of failure. * * Note, this function does not free allocated resources in case of failure - * the caller does it. This is because this would cause release() here and the * caller would oops. */static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol){	int err;	err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_type);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_name);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_corrupted);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_alignment);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_data_bytes);	if (err)		return err;	err = device_create_file(&vol->dev, &attr_vol_upd_marker);	return err;}/** * volume_sysfs_close - close sysfs for a volume. * @vol: volume description object */static void volume_sysfs_close(struct ubi_volume *vol){	device_remove_file(&vol->dev, &attr_vol_upd_marker);	device_remove_file(&vol->dev, &attr_vol_data_bytes);	device_remove_file(&vol->dev, &attr_vol_usable_eb_size);	device_remove_file(&vol->dev, &attr_vol_alignment);	device_remove_file(&vol->dev, &attr_vol_corrupted);	device_remove_file(&vol->dev, &attr_vol_name);	device_remove_file(&vol->dev, &attr_vol_type);	device_remove_file(&vol->dev, &attr_vol_reserved_ebs);	device_unregister(&vol->dev);}/** * ubi_create_volume - create volume. * @ubi: UBI device description object * @req: volume creation request * * This function creates volume described by @req. If @req->vol_id id * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume * and saves it in @req->vol_id. Returns zero in case of success and a negative * error code in case of failure. Note, the caller has to have the * @ubi->volumes_mutex locked. */int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req){	int i, err, vol_id = req->vol_id, do_free = 1;	struct ubi_volume *vol;	struct ubi_vtbl_record vtbl_rec;	uint64_t bytes;	dev_t dev;	if (ubi->ro_mode)		return -EROFS;	vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);	if (!vol)		return -ENOMEM;	spin_lock(&ubi->volumes_lock);	if (vol_id == UBI_VOL_NUM_AUTO) {		/* Find unused volume ID */		dbg_gen("search for vacant volume ID");		for (i = 0; i < ubi->vtbl_slots; i++)			if (!ubi->volumes[i]) {				vol_id = i;				break;			}		if (vol_id == UBI_VOL_NUM_AUTO) {			dbg_err("out of volume IDs");			err = -ENFILE;			goto out_unlock;		}		req->vol_id = vol_id;	}	dbg_gen("volume ID %d, %llu bytes, type %d, name %s",		vol_id, (unsigned long long)req->bytes,		(int)req->vol_type, req->name);	/* Ensure that this volume does not exist */	err = -EEXIST;	if (ubi->volumes[vol_id]) {		dbg_err("volume %d already exists", vol_id);		goto out_unlock;	}	/* Ensure that the name is unique */	for (i = 0; i < ubi->vtbl_slots; i++)		if (ubi->volumes[i] &&		    ubi->volumes[i]->name_len == req->name_len &&		    !strcmp(ubi->volumes[i]->name, req->name)) {			dbg_err("volume \"%s\" exists (ID %d)", req->name, i);			goto out_unlock;		}	/* Calculate how many eraseblocks are requested */	vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;	bytes = req->bytes;	if (do_div(bytes, vol->usable_leb_size))		vol->reserved_pebs = 1;	vol->reserved_pebs += bytes;	/* Reserve physical eraseblocks */	if (vol->reserved_pebs > ubi->avail_pebs) {		dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);		err = -ENOSPC;		goto out_unlock;	}	ubi->avail_pebs -= vol->reserved_pebs;	ubi->rsvd_pebs += vol->reserved_pebs;	spin_unlock(&ubi->volumes_lock);	vol->vol_id    = vol_id;	vol->alignment = req->alignment;	vol->data_pad  = ubi->leb_size % vol->alignment;	vol->vol_type  = req->vol_type;	vol->name_len  = req->name_len;	memcpy(vol->name, req->name, vol->name_len);	vol->ubi = ubi;	/*	 * Finish all pending erases because there may be some LEBs belonging	 * to the same volume ID.	 */	err = ubi_wl_flush(ubi);	if (err)		goto out_acc;	vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);	if (!vol->eba_tbl) {		err = -ENOMEM;		goto out_acc;	}	for (i = 0; i < vol->reserved_pebs; i++)		vol->eba_tbl[i] = UBI_LEB_UNMAPPED;	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {		vol->used_ebs = vol->reserved_pebs;		vol->last_eb_bytes = vol->usable_leb_size;		vol->used_bytes =			(long long)vol->used_ebs * vol->usable_leb_size;	} else {		bytes = vol->used_bytes;		vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);		vol->used_ebs = bytes;		if (vol->last_eb_bytes)			vol->used_ebs += 1;		else			vol->last_eb_bytes = vol->usable_leb_size;	}	/* Register character device for the volume */	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);	vol->cdev.owner = THIS_MODULE;	dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);	err = cdev_add(&vol->cdev, dev, 1);	if (err) {		ubi_err("cannot add character device");		goto out_mapping;	}	err = ubi_create_gluebi(ubi, vol);	if (err)		goto out_cdev;	vol->dev.release = vol_release;	vol->dev.parent = &ubi->dev;	vol->dev.devt = dev;	vol->dev.class = ubi_class;	sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);	err = device_register(&vol->dev);	if (err) {		ubi_err("cannot register device");		goto out_gluebi;	}	err = volume_sysfs_init(ubi, vol);	if (err)		goto out_sysfs;	/* Fill volume table record */	memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));	vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);	vtbl_rec.alignment     = cpu_to_be32(vol->alignment);	vtbl_rec.data_pad      = cpu_to_be32(vol->data_pad);	vtbl_rec.name_len      = cpu_to_be16(vol->name_len);	if (vol->vol_type == UBI_DYNAMIC_VOLUME)		vtbl_rec.vol_type = UBI_VID_DYNAMIC;	else		vtbl_rec.vol_type = UBI_VID_STATIC;	memcpy(vtbl_rec.name, vol->name, vol->name_len);	err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);	if (err)		goto out_sysfs;	spin_lock(&ubi->volumes_lock);	ubi->volumes[vol_id] = vol;	ubi->vol_count += 1;	spin_unlock(&ubi->volumes_lock);	err = paranoid_check_volumes(ubi);	return err;out_sysfs:	/*	 * We have registered our device, we should not free the volume	 * description object in this function in case of an error - it is	 * freed by the release function.	 *	 * Get device reference to prevent the release function from being	 * called just after sysfs has been closed.	 */	do_free = 0;	get_device(&vol->dev);	volume_sysfs_close(vol);out_gluebi:	if (ubi_destroy_gluebi(vol))		dbg_err("cannot destroy gluebi for volume %d:%d",			ubi->ubi_num, vol_id);out_cdev:	cdev_del(&vol->cdev);out_mapping:	if (do_free)		kfree(vol->eba_tbl);out_acc:	spin_lock(&ubi->volumes_lock);	ubi->rsvd_pebs -= vol->reserved_pebs;	ubi->avail_pebs += vol->reserved_pebs;out_unlock:	spin_unlock(&ubi->volumes_lock);	if (do_free)		kfree(vol);	else		put_device(&vol->dev);	ubi_err("cannot create volume %d, error %d", vol_id, err);	return err;}/** * ubi_remove_volume - remove volume. * @desc: volume descriptor * @no_vtbl: do not change volume table if not zero * * This function removes volume described by @desc. The volume has to be opened * in "exclusive" mode. Returns zero in case of success and a negative error * code in case of failure. The caller has to have the @ubi->volumes_mutex * locked. */int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl){	struct ubi_volume *vol = desc->vol;	struct ubi_device *ubi = vol->ubi;	int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;	dbg_gen("remove UBI volume %d", vol_id);	ubi_assert(desc->mode == UBI_EXCLUSIVE);	ubi_assert(vol == ubi->volumes[vol_id]);	if (ubi->ro_mode)		return -EROFS;	spin_lock(&ubi->volumes_lock);	if (vol->ref_count > 1) {		/*		 * The volume is busy, probably someone is reading one of its		 * sysfs files.		 */		err = -EBUSY;		goto out_unlock;	}	ubi->volumes[vol_id] = NULL;	spin_unlock(&ubi->volumes_lock);	err = ubi_destroy_gluebi(vol);	if (err)		goto out_err;	if (!no_vtbl) {		err = ubi_change_vtbl_record(ubi, vol_id, NULL);		if (err)			goto out_err;	}	for (i = 0; i < vol->reserved_pebs; i++) {		err = ubi_eba_unmap_leb(ubi, vol, i);		if (err)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -