我在用TI的dm368開發板,kernel是2.6.32.17,預設的flash檔案系統是jffs2,但是jffs2在大分區下,mount速度很慢,而且佔用ram較多,因此,我想使用ubifs看看效能是否會更好些。
ubifs的原理和配置過程,很多網頁都有介紹的,我給一個連結,大家可以看看,我就不轉載了,我重點說我移植過程中遇到並解決的問題。
http://bbs.chinaunix.net/thread-1954881-1-1.html
kerne的配置很簡單,2.6.32裡面都有,選上並重新編譯就好了。
ubiattach、ubimkvol等工具,TI的dvsdk裡面內建了,能用,我就偷了個懶,沒有重新編譯。
很輕鬆的照著網頁說明操作了下,mount 分區也成功了,複製檔案也成功了,很高興:)
ubiattach /dev/ubi_ctrl -m 3
ubimkvol /dev/ubi0 -N rootfs -s 480MiB
mount -t ubifs ubi0_0 /mnt/nand/ -o sync
但很快就遇到麻煩了,開發板關機重新啟動,我再mout ubi檔案系統就出錯了,提示了一堆錯誤,而且分區是空的,之前複製的檔案不見了。
問題如下:
ubiattach /dev/ubi_ctrl -m 3
UBI error: ubi_io_read: error -74while reading 64 bytes from PEB 1662:0, read 64 bytes
UBI error: ubi_io_read: error -74while reading 64 bytes from PEB 1663:0, read 64 bytes
為了分析問題,我把mtd和ubifs的debug log都開啟了,看到了一些資訊;
#define EBADMSG 74 /* Not a data message */
/*Nand returns -EBADMSG on ecc errors, but it returns
* the data. For our userspace tools it isimportant
* to dump areas with ecc errors !
* For kernel internal usage it also mightreturn -EUCLEAN
* to signal the caller that a bitflip hasoccured and has
* been corrected by the ECC algorithm.
* Userspace software which accesses NAND thisway
* must be aware of the fact that it deals withNAND
*/
nand_do_read_ops
stats= mtd->ecc_stats;
if(mtd->ecc_stats.failed - stats.failed)
return-EBADMSG;
nand_read_page_hwecc
stat= chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
if(stat < 0)
mtd->ecc_stats.failed++;
else
mtd->ecc_stats.corrected+= stat;
看樣子,應該是ECC出錯了,可是我用的是硬體ECC校正,怎麼可能幾乎全部的page都出現ecc校正錯誤了呢?
root@dm368-evm:~#flash_eraseall /dev/mtd3
root@dm368-evm:~# ubiattach/dev/ubi_ctrl -m 3
UBI: attaching mtd3 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 512 (aligned 512)
UBI: data offset: 2048
分析下UBI寫資料的過程,
UBI DBG (pid 1484): ubi_io_write:write 512 bytes to PEB 57:0
int ubi_io_write(struct ubi_device*ubi, const void *buf, int pnum, int offset,
int len)
addr= (loff_t)pnum * ubi->peb_size + offset;
err= ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
在io_init函數中可以看到幾個變數的賦值;
ubi->peb_size = ubi->mtd->erasesize;
ubi->peb_count = mtd_div_by_eb(ubi->mtd->size,ubi->mtd);
ubi->flash_size= ubi->mtd->size;
從debug log來看,這裡一次寫了512位元組,從某個block的起始page開始,offset是0;
那麼到了nand mtd底層驅動,行為就是;
nand_write->nand_do_write_ops:
subpage= column || (writelen & (mtd->writesize - 1));
這裡,subpage就是writelen,512;
也可以得知一點,ubifs沒有使用oob,這跟jffs2和yaffs2是不同的;
/*Partial page write ? */
if(unlikely(column || writelen < (mtd->writesize - 1))) {
cached= 0;
bytes= min_t(int, bytes - column, (int) writelen);
chip->pagebuf= -1;
memset(chip->buffers->databuf,0xff, mtd->writesize);
memcpy(&chip->buffers->databuf[column],buf, bytes);
wbuf= chip->buffers->databuf;
}
ret= chip->write_page(mtd, chip, wbuf, page, cached,
(ops->mode == MTD_OOB_RAW));
下面是write_page函數的代碼;
static int nand_write_page(structmtd_info *mtd, struct nand_chip *chip,
const uint8_t *buf, int page, int cached,int raw)
{
intstatus;
chip->cmdfunc(mtd,NAND_CMD_SEQIN, 0x00, page);
if(unlikely(raw))
chip->ecc.write_page_raw(mtd,chip, buf);
else
chip->ecc.write_page(mtd,chip, buf);
/*
* Cached progamming disabled for now, Not sureif its worth the
* trouble. The speed gain is not veryimpressive. (2.3->2.6Mib/s)
*/
cached= 0;
這裡需要注意的就是raw,如果是MTD_OOB_RAW,那麼不會做ECC校正,也不會把ECC碼寫入OOB;
如果是這樣,在read的時候也必須指定是MTD_OOB_RAW,不需要ECC校正;否則,就會出現我們最開始看到的錯誤;
if(mtd->ecc_stats.failed - stats.failed)
return-EBADMSG;
那麼,從這裡的情況來看,我們可能已經找到出錯的原因了;ubi使用了subpage write,而底層nand flash驅動實際上是不支援subpage write的,儘管ubi一次唯寫了512位元組,但這個page的其他部分已經不能再次寫入新的資料了。
從Nand_base.c(drivers\mtd\nand)來看,large page的nand flash,對subpage write的支援是不完善的,限制條件比較多,比如,不能是MLC的nand flash,不能用硬體ECC;
更嚴重的問題是代碼存在缺陷,在寫入部分data的時候,將其他部分的資料填充為0xff了,然後write整個page,並寫入全部ecc碼到oob,也許這就是前面ecc校正出錯的原因吧。
nand_do_write_ops()
/*Partial page write ? */
memset(chip->buffers->databuf, 0xff, mtd->writesize);
memcpy(&chip->buffers->databuf[column],buf, bytes);
因此,我想到的解決辦法就是在nand驅動中禁止subpage write。
第一步,在chip options中增加NAND_NO_SUBPAGE_WRITE;
static struct davinci_nand_pdatadavinci_nand_data = {
.options = NAND_USE_FLASH_BBT|NAND_NO_SUBPAGE_WRITE,
然後重新編譯下載kernel,但問題依舊;
root@dm368-evm:~# ubiattach/dev/ubi_ctrl -m 3
UBI: attaching mtd3 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 512 (aligned 512)
心想很奇怪,為什麼 sub-page size還是512?
回頭查看代碼,想看看sub page size是怎樣計算出來的,
if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
!(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
switch(chip->ecc.steps) {
case 2:
mtd->subpage_sft = 1;
break;
case 4:
case 8:
case 16:
mtd->subpage_sft = 2;
break;
}
}
chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
可是我已經給options增加了NAND_NO_SUBPAGE_WRITE啊?有些懷疑,我就在此處加了列印log,果然是這裡出了問題,
chip->options = 0x10101.
mtd->subpage_sft = 0.
chip->subpagesize = 512.
#define NAND_NO_SUBPAGE_WRITE 0x00000200
可是,這裡的options 明明是不對的!那我設定的NAND_NO_SUBPAGE_WRITE在哪裡丟掉了?
在下面的函數中有對chip->options賦值改變;
nand_get_flash_type()
printk(KERN_INFO"nand_get_flash_type 1, chip->options = 0x%x.\n",chip->options);
/*Get chip options, preserve non chip based options */
chip->options &= ~NAND_CHIPOPTIONS_MSK;
printk(KERN_INFO"nand_get_flash_type 2, chip->options = 0x%x.\n",chip->options);
chip->options|= type->options & NAND_CHIPOPTIONS_MSK;
printk(KERN_INFO"nand_get_flash_type 3, chip->options = 0x%x.\n",chip->options);
/*
* Set chip as a default. Board drivers canoverride it, if necessary
*/
chip->options|= NAND_NO_AUTOINCR;
/*Check if chip is a not a samsung device. Do not clear the
* options for chips which are not having anextended id.
*/
if(*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
chip->options&= ~NAND_SAMSUNG_LP_OPTIONS;
printk(KERN_INFO"nand_get_flash_type 4, chip->options = 0x%x.\n",chip->options);
nand_get_flash_type 1,chip->options = 0x10200.
nand_get_flash_type 2,chip->options = 0x10000.
nand_get_flash_type 3,chip->options = 0x1011c.
nand_get_flash_type 4,chip->options = 0x10101.
問題出在這裡,紅色的代碼!
/* Mask to zero out thechip options, which come from the id table */
#defineNAND_CHIPOPTIONS_MSK (0x0000ffff &~NAND_NO_AUTOINCR)
/* Chip can not autoincrement pages */
#define NAND_NO_AUTOINCR 0x00000001
找到問題了,解決辦法就有了,注釋掉紅色的那行代碼,就是他把NAND_NO_SUBPAGE_WRITE給丟掉了。
//chip->options &=~NAND_CHIPOPTIONS_MSK;
再重新編譯下載kernel,問題搞定了!
再次啟動開發板,載入ubi,一切正常了。
root@dm368-evm:/# ubiattach/dev/ubi_ctrl -m 3
UBI: attaching mtd3 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 126976 bytes
UBI: smallest flash I/O unit: 2048
UBI: VID header offset: 2048 (aligned 2048)
UBI: data offset: 4096
UBI: attached mtd3 to ubi0
UBI: MTD device name: "filesystem1"
UBI: MTD device size: 512 MiB
UBI: number of good PEBs: 4096
UBI: number of bad PEBs: 0
UBI: max. allowed volumes: 128
UBI: wear-leveling threshold: 4096
UBI: number of internal volumes: 1
UBI: number of user volumes: 1
UBI: available PEBs: 3639
UBI: total number of reserved PEBs:457
UBI: number of PEBs reserved for badPEB handling: 40
UBI: max/mean erase counter: 2/1
UBI: image sequence number: 0
UBI: background thread"ubi_bgt0d" started, PID 1483
UBI device number 0, total 4096 LEBs(520093696 bytes, 496.0 MiB), available 3639 LEBs (462065664 bytes, 440.7 MiB),LEB size 126976 bytes (124.0 KiB)
root@dm368-evm:/# mount -tubifs ubi0_0 /mnt/nand/ -o sync
UBIFS: mounted UBI device 0, volume0, name "rootfs"
UBIFS: file system size: 51171328 bytes (49972 KiB, 48 MiB, 403 LEBs)
UBIFS: journal size: 2539520 bytes (2480 KiB, 2 MiB, 20 LEBs)
UBIFS: media format: w4/r0 (latest is w4/r0)
UBIFS: default compressor: lzo
UBIFS: reserved for root: 2416947 bytes (2360 KiB)
root@dm368-evm:/# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 39544232 16239820 21295632 43% /
none 1024 24 1000 2% /dev
tmpfs 16384 20 16364 0% /var/volatile
tmpfs 21760 0 21760 0% /dev/shm
tmpfs 16384 0 16384 0% /media/ram
ubi0_0 45528 1528 41640 4% /mnt/nand
最後,總結下,就是要禁止nand flash驅動裡面的subpage write,讓 chip->subpagesize == mtd->writesize 就好了。
這是我的解決辦法。如果有人要使用subpage write,那麼一定要保證你的讀寫邏輯是正確的,要麼不用ecc,要麼各個subpage的ecc都應是正確的。