📄 nftlcore.c
字号:
/* Linux driver for NAND Flash Translation Layer *//* (c) 1999 Machine Vision Holdings, Inc. *//* Author: David Woodhouse <dwmw2@infradead.org> *//* $Id: nftlcore.c,v 1.82 2001/10/02 15:05:11 dwmw2 Exp $ *//* The contents of this file are distributed under the GNU General Public License version 2. The author places no additional restrictions of any kind on it. */#define PRERELEASE#include <linux/config.h>#include <linux/kernel.h>#include <linux/module.h>#include <asm/errno.h>#include <asm/io.h>#include <asm/uaccess.h>#include <linux/miscdevice.h>#include <linux/pci.h>#include <linux/delay.h>#include <linux/slab.h>#include <linux/sched.h>#include <linux/init.h>#include <linux/blkpg.h>#ifdef CONFIG_KMOD#include <linux/kmod.h>#endif#include <linux/mtd/mtd.h>#include <linux/mtd/nftl.h>#include <linux/mtd/compatmac.h>/* maximum number of loops while examining next block, to have a chance to detect consistency problems (they should never happen because of the checks done in the mounting */#define MAX_LOOPS 10000/* NFTL block device stuff */#define MAJOR_NR NFTL_MAJOR#define DEVICE_REQUEST nftl_request#define DEVICE_OFF(device)#include <linux/blk.h>#include <linux/hdreg.h>/* Linux-specific block device functions *//* I _HATE_ the Linux block device setup more than anything else I've ever * encountered, except ... */static int nftl_sizes[256];static int nftl_blocksizes[256];/* .. for the Linux partition table handling. */struct hd_struct part_table[256];#if LINUX_VERSION_CODE < 0x20328static void dummy_init (struct gendisk *crap){}#endifstatic struct gendisk nftl_gendisk = { major: MAJOR_NR, major_name: "nftl", minor_shift: NFTL_PARTN_BITS, /* Bits to shift to get real from partition */ max_p: (1<<NFTL_PARTN_BITS)-1, /* Number of partitions per real */#if LINUX_VERSION_CODE < 0x20328 max_nr: MAX_NFTLS, /* maximum number of real */ init: dummy_init, /* init function */#endif part: part_table, /* hd struct */ sizes: nftl_sizes, /* block sizes */};struct NFTLrecord *NFTLs[MAX_NFTLS];static void NFTL_setup(struct mtd_info *mtd){ int i; struct NFTLrecord *nftl; unsigned long temp; int firstfree = -1; DEBUG(MTD_DEBUG_LEVEL1,"NFTL_setup\n"); for (i = 0; i < MAX_NFTLS; i++) { if (!NFTLs[i] && firstfree == -1) firstfree = i; else if (NFTLs[i] && NFTLs[i]->mtd == mtd) { /* This is a Spare Media Header for an NFTL we've already found */ DEBUG(MTD_DEBUG_LEVEL1, "MTD already mounted as NFTL\n"); return; } } if (firstfree == -1) { printk(KERN_WARNING "No more NFTL slot available\n"); return; } nftl = kmalloc(sizeof(struct NFTLrecord), GFP_KERNEL); if (!nftl) { printk(KERN_WARNING "Out of memory for NFTL data structures\n"); return; } init_MUTEX(&nftl->mutex); /* get physical parameters */ nftl->EraseSize = mtd->erasesize; nftl->nb_blocks = mtd->size / mtd->erasesize; nftl->mtd = mtd; if (NFTL_mount(nftl) < 0) { printk(KERN_WARNING "Could not mount NFTL device\n"); kfree(nftl); return; } /* OK, it's a new one. Set up all the data structures. */#ifdef PSYCHO_DEBUG printk("Found new NFTL nftl%c\n", firstfree + 'a');#endif /* linux stuff */ nftl->usecount = 0; nftl->cylinders = 1024; nftl->heads = 16; temp = nftl->cylinders * nftl->heads; nftl->sectors = nftl->nr_sects / temp; if (nftl->nr_sects % temp) { nftl->sectors++; temp = nftl->cylinders * nftl->sectors; nftl->heads = nftl->nr_sects / temp; if (nftl->nr_sects % temp) { nftl->heads++; temp = nftl->heads * nftl->sectors; nftl->cylinders = nftl->nr_sects / temp; } } if (nftl->nr_sects != nftl->heads * nftl->cylinders * nftl->sectors) { printk(KERN_WARNING "Cannot calculate an NFTL geometry to " "match size of 0x%lx.\n", nftl->nr_sects); printk(KERN_WARNING "Using C:%d H:%d S:%d (== 0x%lx sects)\n", nftl->cylinders, nftl->heads , nftl->sectors, (long)nftl->cylinders * (long)nftl->heads * (long)nftl->sectors ); /* Oh no we don't have nftl->nr_sects = nftl->heads * nftl->cylinders * nftl->sectors; */ } NFTLs[firstfree] = nftl; /* Finally, set up the block device sizes */ nftl_sizes[firstfree * 16] = nftl->nr_sects; //nftl_blocksizes[firstfree*16] = 512; part_table[firstfree * 16].nr_sects = nftl->nr_sects; nftl_gendisk.nr_real++; /* partition check ... */#if LINUX_VERSION_CODE < 0x20328 resetup_one_dev(&nftl_gendisk, firstfree);#else grok_partitions(&nftl_gendisk, firstfree, 1<<NFTL_PARTN_BITS, nftl->nr_sects);#endif}static void NFTL_unsetup(int i){ struct NFTLrecord *nftl = NFTLs[i]; DEBUG(MTD_DEBUG_LEVEL1, "NFTL_unsetup %d\n", i); NFTLs[i] = NULL; if (nftl->ReplUnitTable) kfree(nftl->ReplUnitTable); if (nftl->EUNtable) kfree(nftl->EUNtable); nftl_gendisk.nr_real--; kfree(nftl);}/* Search the MTD device for NFTL partitions */static void NFTL_notify_add(struct mtd_info *mtd){ DEBUG(MTD_DEBUG_LEVEL1, "NFTL_notify_add for %s\n", mtd->name); if (mtd) { if (!mtd->read_oob) { /* If this MTD doesn't have out-of-band data, then there's no point continuing */ DEBUG(MTD_DEBUG_LEVEL1, "No OOB data, quitting\n"); return; } DEBUG(MTD_DEBUG_LEVEL3, "mtd->read = %p, size = %d, erasesize = %d\n", mtd->read, mtd->size, mtd->erasesize); NFTL_setup(mtd); }}static void NFTL_notify_remove(struct mtd_info *mtd){ int i; for (i = 0; i < MAX_NFTLS; i++) { if (NFTLs[i] && NFTLs[i]->mtd == mtd) NFTL_unsetup(i); }}#ifdef CONFIG_NFTL_RW/* Actual NFTL access routines *//* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used * when the give Virtual Unit Chain */static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate ){ /* For a given Virtual Unit Chain: find or create a free block and add it to the chain */ /* We're passed the number of the last EUN in the chain, to save us from having to look it up again */ u16 pot = nftl->LastFreeEUN; int silly = nftl->nb_blocks; /* Normally, we force a fold to happen before we run out of free blocks completely */ if (!desperate && nftl->numfreeEUNs < 2) { DEBUG(MTD_DEBUG_LEVEL1, "NFTL_findfreeblock: there are too few free EUNs\n"); return 0xffff; } /* Scan for a free block */ do { if (nftl->ReplUnitTable[pot] == BLOCK_FREE) { nftl->LastFreeEUN = pot; nftl->numfreeEUNs--; return pot; } /* This will probably point to the MediaHdr unit itself, right at the beginning of the partition. But that unit (and the backup unit too) should have the UCI set up so that it's not selected for overwriting */ if (++pot > nftl->lastEUN) pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN); if (!silly--) { printk("Argh! No free blocks found! LastFreeEUN = %d, " "FirstEUN = %d\n", nftl->LastFreeEUN, le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN)); return 0xffff; } } while (pot != nftl->LastFreeEUN); return 0xffff;}static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock ){ u16 BlockMap[MAX_SECTORS_PER_UNIT]; unsigned char BlockLastState[MAX_SECTORS_PER_UNIT]; unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT]; unsigned int thisEUN; int block; int silly; unsigned int targetEUN; struct nftl_oob oob; int inplace = 1; size_t retlen; memset(BlockMap, 0xff, sizeof(BlockMap)); memset(BlockFreeFound, 0, sizeof(BlockFreeFound)); thisEUN = nftl->EUNtable[thisVUC]; if (thisEUN == BLOCK_NIL) { printk(KERN_WARNING "Trying to fold non-existent " "Virtual Unit Chain %d!\n", thisVUC); return BLOCK_NIL; } /* Scan to find the Erase Unit which holds the actual data for each 512-byte block within the Chain. */ silly = MAX_LOOPS; targetEUN = BLOCK_NIL; while (thisEUN <= nftl->lastEUN ) { unsigned int status, foldmark; targetEUN = thisEUN; for (block = 0; block < nftl->EraseSize / 512; block ++) { MTD_READOOB(nftl->mtd, (thisEUN * nftl->EraseSize) + (block * 512), 16 , &retlen, (char *)&oob); if (block == 2) { foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1; if (foldmark == FOLD_MARK_IN_PROGRESS) { DEBUG(MTD_DEBUG_LEVEL1, "Write Inhibited on EUN %d\n", thisEUN); inplace = 0; } else { /* There's no other reason not to do inplace, except ones that come later. So we don't need to preserve inplace */ inplace = 1; } } status = oob.b.Status | oob.b.Status1; BlockLastState[block] = status; switch(status) { case SECTOR_FREE: BlockFreeFound[block] = 1; break; case SECTOR_USED: if (!BlockFreeFound[block]) BlockMap[block] = thisEUN; else printk(KERN_WARNING "SECTOR_USED found after SECTOR_FREE " "in Virtual Unit Chain %d for block %d\n", thisVUC, block); break; case SECTOR_DELETED: if (!BlockFreeFound[block]) BlockMap[block] = BLOCK_NIL; else printk(KERN_WARNING "SECTOR_DELETED found after SECTOR_FREE " "in Virtual Unit Chain %d for block %d\n", thisVUC, block); break; case SECTOR_IGNORE: break; default: printk("Unknown status for block %d in EUN %d: %x\n", block, thisEUN, status); } } if (!silly--) { printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n", thisVUC); return BLOCK_NIL; } thisEUN = nftl->ReplUnitTable[thisEUN]; } if (inplace) { /* We're being asked to be a fold-in-place. Check that all blocks which actually have data associated with them (i.e. BlockMap[block] != BLOCK_NIL) are
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -