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

📄 eba.c

📁 基于linux-2.6.28的mtd驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * 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 (Битюцкий Артём) *//* * The UBI Eraseblock Association (EBA) sub-system. * * This sub-system is responsible for I/O to/from logical eraseblock. * * Although in this implementation the EBA table is fully kept and managed in * RAM, which assumes poor scalability, it might be (partially) maintained on * flash in future implementations. * * The EBA sub-system implements per-logical eraseblock locking. Before * accessing a logical eraseblock it is locked for reading or writing. The * per-logical eraseblock locking is implemented by means of the lock tree. The * lock tree is an RB-tree which refers all the currently locked logical * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects. * They are indexed by (@vol_id, @lnum) pairs. * * EBA also maintains the global sequence counter which is incremented each * time a logical eraseblock is mapped to a physical eraseblock and it is * stored in the volume identifier header. This means that each VID header has * a unique sequence number. The sequence number is only increased an we assume * 64 bits is enough to never overflow. */#include <linux/slab.h>#include <linux/crc32.h>#include <linux/err.h>#include "ubi.h"/* Number of physical eraseblocks reserved for atomic LEB change operation */#define EBA_RESERVED_PEBS 1/** * next_sqnum - get next sequence number. * @ubi: UBI device description object * * This function returns next sequence number to use, which is just the current * global sequence counter value. It also increases the global sequence * counter. */static unsigned long long next_sqnum(struct ubi_device *ubi){	unsigned long long sqnum;	spin_lock(&ubi->ltree_lock);	sqnum = ubi->global_sqnum++;	spin_unlock(&ubi->ltree_lock);	return sqnum;}/** * ubi_get_compat - get compatibility flags of a volume. * @ubi: UBI device description object * @vol_id: volume ID * * This function returns compatibility flags for an internal volume. User * volumes have no compatibility flags, so %0 is returned. */static int ubi_get_compat(const struct ubi_device *ubi, int vol_id){	if (vol_id == UBI_LAYOUT_VOLUME_ID)		return UBI_LAYOUT_VOLUME_COMPAT;	return 0;}/** * ltree_lookup - look up the lock tree. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number * * This function returns a pointer to the corresponding &struct ubi_ltree_entry * object if the logical eraseblock is locked and %NULL if it is not. * @ubi->ltree_lock has to be locked. */static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,					    int lnum){	struct rb_node *p;	p = ubi->ltree.rb_node;	while (p) {		struct ubi_ltree_entry *le;		le = rb_entry(p, struct ubi_ltree_entry, rb);		if (vol_id < le->vol_id)			p = p->rb_left;		else if (vol_id > le->vol_id)			p = p->rb_right;		else {			if (lnum < le->lnum)				p = p->rb_left;			else if (lnum > le->lnum)				p = p->rb_right;			else				return le;		}	}	return NULL;}/** * ltree_add_entry - add new entry to the lock tree. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number * * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the * lock tree. If such entry is already there, its usage counter is increased. * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation * failed. */static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,					       int vol_id, int lnum){	struct ubi_ltree_entry *le, *le1, *le_free;	le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);	if (!le)		return ERR_PTR(-ENOMEM);	le->users = 0;	init_rwsem(&le->mutex);	le->vol_id = vol_id;	le->lnum = lnum;	spin_lock(&ubi->ltree_lock);	le1 = ltree_lookup(ubi, vol_id, lnum);	if (le1) {		/*		 * This logical eraseblock is already locked. The newly		 * allocated lock entry is not needed.		 */		le_free = le;		le = le1;	} else {		struct rb_node **p, *parent = NULL;		/*		 * No lock entry, add the newly allocated one to the		 * @ubi->ltree RB-tree.		 */		le_free = NULL;		p = &ubi->ltree.rb_node;		while (*p) {			parent = *p;			le1 = rb_entry(parent, struct ubi_ltree_entry, rb);			if (vol_id < le1->vol_id)				p = &(*p)->rb_left;			else if (vol_id > le1->vol_id)				p = &(*p)->rb_right;			else {				ubi_assert(lnum != le1->lnum);				if (lnum < le1->lnum)					p = &(*p)->rb_left;				else					p = &(*p)->rb_right;			}		}		rb_link_node(&le->rb, parent, p);		rb_insert_color(&le->rb, &ubi->ltree);	}	le->users += 1;	spin_unlock(&ubi->ltree_lock);	kfree(le_free);	return le;}/** * leb_read_lock - lock logical eraseblock for reading. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number * * This function locks a logical eraseblock for reading. Returns zero in case * of success and a negative error code in case of failure. */static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum){	struct ubi_ltree_entry *le;	le = ltree_add_entry(ubi, vol_id, lnum);	if (IS_ERR(le))		return PTR_ERR(le);	down_read(&le->mutex);	return 0;}/** * leb_read_unlock - unlock logical eraseblock. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number */static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum){	struct ubi_ltree_entry *le;	spin_lock(&ubi->ltree_lock);	le = ltree_lookup(ubi, vol_id, lnum);	le->users -= 1;	ubi_assert(le->users >= 0);	up_read(&le->mutex);	if (le->users == 0) {		rb_erase(&le->rb, &ubi->ltree);		kfree(le);	}	spin_unlock(&ubi->ltree_lock);}/** * leb_write_lock - lock logical eraseblock for writing. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number * * This function locks a logical eraseblock for writing. Returns zero in case * of success and a negative error code in case of failure. */static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum){	struct ubi_ltree_entry *le;	le = ltree_add_entry(ubi, vol_id, lnum);	if (IS_ERR(le))		return PTR_ERR(le);	down_write(&le->mutex);	return 0;}/** * leb_write_lock - lock logical eraseblock for writing. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number * * This function locks a logical eraseblock for writing if there is no * contention and does nothing if there is contention. Returns %0 in case of * success, %1 in case of contention, and and a negative error code in case of * failure. */static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum){	struct ubi_ltree_entry *le;	le = ltree_add_entry(ubi, vol_id, lnum);	if (IS_ERR(le))		return PTR_ERR(le);	if (down_write_trylock(&le->mutex))		return 0;	/* Contention, cancel */	spin_lock(&ubi->ltree_lock);	le->users -= 1;	ubi_assert(le->users >= 0);	if (le->users == 0) {		rb_erase(&le->rb, &ubi->ltree);		kfree(le);	}	spin_unlock(&ubi->ltree_lock);	return 1;}/** * leb_write_unlock - unlock logical eraseblock. * @ubi: UBI device description object * @vol_id: volume ID * @lnum: logical eraseblock number */static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum){	struct ubi_ltree_entry *le;	spin_lock(&ubi->ltree_lock);	le = ltree_lookup(ubi, vol_id, lnum);	le->users -= 1;	ubi_assert(le->users >= 0);	up_write(&le->mutex);	if (le->users == 0) {		rb_erase(&le->rb, &ubi->ltree);		kfree(le);	}	spin_unlock(&ubi->ltree_lock);}/** * ubi_eba_unmap_leb - un-map logical eraseblock. * @ubi: UBI device description object * @vol: volume description object * @lnum: logical eraseblock number * * This function un-maps logical eraseblock @lnum and schedules corresponding * physical eraseblock for erasure. Returns zero in case of success and a * negative error code in case of failure. */int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,		      int lnum){	int err, pnum, vol_id = vol->vol_id;	if (ubi->ro_mode)		return -EROFS;	err = leb_write_lock(ubi, vol_id, lnum);	if (err)		return err;	pnum = vol->eba_tbl[lnum];	if (pnum < 0)		/* This logical eraseblock is already unmapped */		goto out_unlock;	dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);	vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;	err = ubi_wl_put_peb(ubi, pnum, 0);out_unlock:	leb_write_unlock(ubi, vol_id, lnum);	return err;}/** * ubi_eba_read_leb - read data. * @ubi: UBI device description object * @vol: volume description object * @lnum: logical eraseblock number * @buf: buffer to store the read data * @offset: offset from where to read * @len: how many bytes to read * @check: data CRC check flag * * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF * bytes. The @check flag only makes sense for static volumes and forces * eraseblock data CRC checking. * * In case of success this function returns zero. In case of a static volume, * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be * returned for any volume type if an ECC error was detected by the MTD device * driver. Other negative error cored may be returned in case of other errors. */int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,		     void *buf, int offset, int len, int check){	int err, pnum, scrub = 0, vol_id = vol->vol_id;	struct ubi_vid_hdr *vid_hdr;	uint32_t uninitialized_var(crc);	err = leb_read_lock(ubi, vol_id, lnum);	if (err)		return err;	pnum = vol->eba_tbl[lnum];	if (pnum < 0) {		/*		 * The logical eraseblock is not mapped, fill the whole buffer		 * with 0xFF bytes. The exception is static volumes for which		 * it is an error to read unmapped logical eraseblocks.		 */		dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",			len, offset, vol_id, lnum);		leb_read_unlock(ubi, vol_id, lnum);		ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);		memset(buf, 0xFF, len);		return 0;	}	dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",		len, offset, vol_id, lnum, pnum);	if (vol->vol_type == UBI_DYNAMIC_VOLUME)		check = 0;retry:	if (check) {		vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);		if (!vid_hdr) {			err = -ENOMEM;			goto out_unlock;

⌨️ 快捷键说明

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