aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorChristopher Kenna <cjk@cs.unc.edu>2012-09-28 13:46:28 -0400
committerChristopher Kenna <cjk@cs.unc.edu>2012-09-28 14:50:15 -0400
commitdaa22703f14c007e93b464c45fa60019a36f546d (patch)
treea1a130b6e128dc9d57c35c026977e1b4953105e1 /fs
parent5aa287dcf1b5879aa0150b0511833c52885f5b4c (diff)
Apply k4412 kernel from HardKernel for ODROID-X.
Diffstat (limited to 'fs')
-rw-r--r--fs/Kconfig4
-rw-r--r--fs/Makefile3
-rw-r--r--fs/aio.c4
-rw-r--r--fs/block_dev.c23
-rw-r--r--fs/btrfs/extent-tree.c3
-rw-r--r--fs/buffer.c2
-rw-r--r--fs/ext4/inode.c8
-rw-r--r--fs/fat/dir.c9
-rw-r--r--fs/fat/fat.h1
-rw-r--r--fs/fat/inode.c9
-rw-r--r--fs/fs-writeback.c438
-rw-r--r--fs/fuse/dev.c6
-rw-r--r--fs/inode.c5
-rw-r--r--fs/internal.h3
-rw-r--r--fs/ioprio.c9
-rw-r--r--fs/nfs/write.c3
-rw-r--r--fs/partitions/check.c11
-rw-r--r--fs/proc/base.c144
-rw-r--r--fs/quota/quota.c2
-rw-r--r--fs/splice.c4
-rw-r--r--fs/super.c37
-rw-r--r--fs/sync.c31
-rw-r--r--fs/ubifs/budget.c2
-rw-r--r--fs/yaffs2/Kconfig161
-rw-r--r--fs/yaffs2/Makefile17
-rw-r--r--fs/yaffs2/yaffs_allocator.c396
-rw-r--r--fs/yaffs2/yaffs_allocator.h30
-rw-r--r--fs/yaffs2/yaffs_attribs.c124
-rw-r--r--fs/yaffs2/yaffs_attribs.h28
-rw-r--r--fs/yaffs2/yaffs_bitmap.c98
-rw-r--r--fs/yaffs2/yaffs_bitmap.h33
-rw-r--r--fs/yaffs2/yaffs_checkptrw.c415
-rw-r--r--fs/yaffs2/yaffs_checkptrw.h33
-rw-r--r--fs/yaffs2/yaffs_ecc.c298
-rw-r--r--fs/yaffs2/yaffs_ecc.h44
-rw-r--r--fs/yaffs2/yaffs_getblockinfo.h35
-rw-r--r--fs/yaffs2/yaffs_guts.c5164
-rw-r--r--fs/yaffs2/yaffs_guts.h915
-rw-r--r--fs/yaffs2/yaffs_linux.h41
-rw-r--r--fs/yaffs2/yaffs_mtdif.c54
-rw-r--r--fs/yaffs2/yaffs_mtdif.h23
-rw-r--r--fs/yaffs2/yaffs_mtdif1.c330
-rw-r--r--fs/yaffs2/yaffs_mtdif1.h29
-rw-r--r--fs/yaffs2/yaffs_mtdif2.c225
-rw-r--r--fs/yaffs2/yaffs_mtdif2.h29
-rw-r--r--fs/yaffs2/yaffs_nameval.c201
-rw-r--r--fs/yaffs2/yaffs_nameval.h28
-rw-r--r--fs/yaffs2/yaffs_nand.c127
-rw-r--r--fs/yaffs2/yaffs_nand.h38
-rw-r--r--fs/yaffs2/yaffs_packedtags1.c53
-rw-r--r--fs/yaffs2/yaffs_packedtags1.h39
-rw-r--r--fs/yaffs2/yaffs_packedtags2.c196
-rw-r--r--fs/yaffs2/yaffs_packedtags2.h47
-rw-r--r--fs/yaffs2/yaffs_tagscompat.c422
-rw-r--r--fs/yaffs2/yaffs_tagscompat.h36
-rw-r--r--fs/yaffs2/yaffs_tagsvalidity.c27
-rw-r--r--fs/yaffs2/yaffs_tagsvalidity.h23
-rw-r--r--fs/yaffs2/yaffs_trace.h57
-rw-r--r--fs/yaffs2/yaffs_verify.c535
-rw-r--r--fs/yaffs2/yaffs_verify.h43
-rw-r--r--fs/yaffs2/yaffs_vfs.c2792
-rw-r--r--fs/yaffs2/yaffs_yaffs1.c433
-rw-r--r--fs/yaffs2/yaffs_yaffs1.h22
-rw-r--r--fs/yaffs2/yaffs_yaffs2.c1598
-rw-r--r--fs/yaffs2/yaffs_yaffs2.h39
-rw-r--r--fs/yaffs2/yportenv.h70
66 files changed, 15836 insertions, 273 deletions
diff --git a/fs/Kconfig b/fs/Kconfig
index 19891aab9c6..88701cc00e0 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -192,6 +192,10 @@ source "fs/hfsplus/Kconfig"
192source "fs/befs/Kconfig" 192source "fs/befs/Kconfig"
193source "fs/bfs/Kconfig" 193source "fs/bfs/Kconfig"
194source "fs/efs/Kconfig" 194source "fs/efs/Kconfig"
195
196# Patched by YAFFS
197source "fs/yaffs2/Kconfig"
198
195source "fs/jffs2/Kconfig" 199source "fs/jffs2/Kconfig"
196# UBIFS File system configuration 200# UBIFS File system configuration
197source "fs/ubifs/Kconfig" 201source "fs/ubifs/Kconfig"
diff --git a/fs/Makefile b/fs/Makefile
index fb68c2b8cf8..2999b4d4b26 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -124,3 +124,6 @@ obj-$(CONFIG_GFS2_FS) += gfs2/
124obj-$(CONFIG_EXOFS_FS) += exofs/ 124obj-$(CONFIG_EXOFS_FS) += exofs/
125obj-$(CONFIG_CEPH_FS) += ceph/ 125obj-$(CONFIG_CEPH_FS) += ceph/
126obj-$(CONFIG_PSTORE) += pstore/ 126obj-$(CONFIG_PSTORE) += pstore/
127
128# Patched by YAFFS
129obj-$(CONFIG_YAFFS_FS) += yaffs2/
diff --git a/fs/aio.c b/fs/aio.c
index 278ed7dc71b..99bb74461b0 100644
--- a/fs/aio.c
+++ b/fs/aio.c
@@ -1618,7 +1618,6 @@ long do_io_submit(aio_context_t ctx_id, long nr,
1618 struct kioctx *ctx; 1618 struct kioctx *ctx;
1619 long ret = 0; 1619 long ret = 0;
1620 int i; 1620 int i;
1621 struct blk_plug plug;
1622 1621
1623 if (unlikely(nr < 0)) 1622 if (unlikely(nr < 0))
1624 return -EINVAL; 1623 return -EINVAL;
@@ -1635,8 +1634,6 @@ long do_io_submit(aio_context_t ctx_id, long nr,
1635 return -EINVAL; 1634 return -EINVAL;
1636 } 1635 }
1637 1636
1638 blk_start_plug(&plug);
1639
1640 /* 1637 /*
1641 * AKPM: should this return a partial result if some of the IOs were 1638 * AKPM: should this return a partial result if some of the IOs were
1642 * successfully submitted? 1639 * successfully submitted?
@@ -1659,7 +1656,6 @@ long do_io_submit(aio_context_t ctx_id, long nr,
1659 if (ret) 1656 if (ret)
1660 break; 1657 break;
1661 } 1658 }
1662 blk_finish_plug(&plug);
1663 1659
1664 put_ioctx(ctx); 1660 put_ioctx(ctx);
1665 return i ? i : ret; 1661 return i ? i : ret;
diff --git a/fs/block_dev.c b/fs/block_dev.c
index a580028e2fb..8d78633c00b 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -44,24 +44,28 @@ inline struct block_device *I_BDEV(struct inode *inode)
44{ 44{
45 return &BDEV_I(inode)->bdev; 45 return &BDEV_I(inode)->bdev;
46} 46}
47
48EXPORT_SYMBOL(I_BDEV); 47EXPORT_SYMBOL(I_BDEV);
49 48
50/* 49/*
51 * move the inode from it's current bdi to the a new bdi. if the inode is dirty 50 * Move the inode from its current bdi to a new bdi. If the inode is dirty we
52 * we need to move it onto the dirty list of @dst so that the inode is always 51 * need to move it onto the dirty list of @dst so that the inode is always on
53 * on the right list. 52 * the right list.
54 */ 53 */
55static void bdev_inode_switch_bdi(struct inode *inode, 54static void bdev_inode_switch_bdi(struct inode *inode,
56 struct backing_dev_info *dst) 55 struct backing_dev_info *dst)
57{ 56{
58 spin_lock(&inode_wb_list_lock); 57 struct backing_dev_info *old = inode->i_data.backing_dev_info;
58
59 if (unlikely(dst == old)) /* deadlock avoidance */
60 return;
61 bdi_lock_two(&old->wb, &dst->wb);
59 spin_lock(&inode->i_lock); 62 spin_lock(&inode->i_lock);
60 inode->i_data.backing_dev_info = dst; 63 inode->i_data.backing_dev_info = dst;
61 if (inode->i_state & I_DIRTY) 64 if (inode->i_state & I_DIRTY)
62 list_move(&inode->i_wb_list, &dst->wb.b_dirty); 65 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
63 spin_unlock(&inode->i_lock); 66 spin_unlock(&inode->i_lock);
64 spin_unlock(&inode_wb_list_lock); 67 spin_unlock(&old->wb.list_lock);
68 spin_unlock(&dst->wb.list_lock);
65} 69}
66 70
67sector_t blkdev_max_block(struct block_device *bdev) 71sector_t blkdev_max_block(struct block_device *bdev)
@@ -1429,11 +1433,6 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1429 WARN_ON_ONCE(bdev->bd_holders); 1433 WARN_ON_ONCE(bdev->bd_holders);
1430 sync_blockdev(bdev); 1434 sync_blockdev(bdev);
1431 kill_bdev(bdev); 1435 kill_bdev(bdev);
1432 /* ->release can cause the old bdi to disappear,
1433 * so must switch it out first
1434 */
1435 bdev_inode_switch_bdi(bdev->bd_inode,
1436 &default_backing_dev_info);
1437 } 1436 }
1438 if (bdev->bd_contains == bdev) { 1437 if (bdev->bd_contains == bdev) {
1439 if (disk->fops->release) 1438 if (disk->fops->release)
@@ -1445,6 +1444,8 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1445 disk_put_part(bdev->bd_part); 1444 disk_put_part(bdev->bd_part);
1446 bdev->bd_part = NULL; 1445 bdev->bd_part = NULL;
1447 bdev->bd_disk = NULL; 1446 bdev->bd_disk = NULL;
1447 bdev_inode_switch_bdi(bdev->bd_inode,
1448 &default_backing_dev_info);
1448 if (bdev != bdev->bd_contains) 1449 if (bdev != bdev->bd_contains)
1449 victim = bdev->bd_contains; 1450 victim = bdev->bd_contains;
1450 bdev->bd_contains = NULL; 1451 bdev->bd_contains = NULL;
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 7e20a65d2d4..f88d5363114 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -3329,7 +3329,8 @@ static int shrink_delalloc(struct btrfs_trans_handle *trans,
3329 smp_mb(); 3329 smp_mb();
3330 nr_pages = min_t(unsigned long, nr_pages, 3330 nr_pages = min_t(unsigned long, nr_pages,
3331 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); 3331 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3332 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); 3332 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages,
3333 WB_REASON_FS_FREE_SPACE);
3333 3334
3334 spin_lock(&space_info->lock); 3335 spin_lock(&space_info->lock);
3335 if (reserved > space_info->bytes_reserved) 3336 if (reserved > space_info->bytes_reserved)
diff --git a/fs/buffer.c b/fs/buffer.c
index d42162672c7..d7586936b25 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -285,7 +285,7 @@ static void free_more_memory(void)
285 struct zone *zone; 285 struct zone *zone;
286 int nid; 286 int nid;
287 287
288 wakeup_flusher_threads(1024); 288 wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);
289 yield(); 289 yield();
290 290
291 for_each_online_node(nid) { 291 for_each_online_node(nid) {
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 18fee6daecd..00a5acdd654 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2687,8 +2687,12 @@ static int ext4_writepage(struct page *page,
2687 * We don't want to do block allocation, so redirty 2687 * We don't want to do block allocation, so redirty
2688 * the page and return. We may reach here when we do 2688 * the page and return. We may reach here when we do
2689 * a journal commit via journal_submit_inode_data_buffers. 2689 * a journal commit via journal_submit_inode_data_buffers.
2690 * We can also reach here via shrink_page_list 2690 * We can also reach here via shrink_page_list but it
2691 * should never be for direct reclaim so warn if that
2692 * happens
2691 */ 2693 */
2694 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
2695 PF_MEMALLOC);
2692 goto redirty_page; 2696 goto redirty_page;
2693 } 2697 }
2694 if (commit_write) 2698 if (commit_write)
@@ -3117,7 +3121,7 @@ static int ext4_nonda_switch(struct super_block *sb)
3117 * start pushing delalloc when 1/2 of free blocks are dirty. 3121 * start pushing delalloc when 1/2 of free blocks are dirty.
3118 */ 3122 */
3119 if (free_blocks < 2 * dirty_blocks) 3123 if (free_blocks < 2 * dirty_blocks)
3120 writeback_inodes_sb_if_idle(sb); 3124 writeback_inodes_sb_if_idle(sb, WB_REASON_FS_FREE_SPACE);
3121 3125
3122 return 0; 3126 return 0;
3123} 3127}
diff --git a/fs/fat/dir.c b/fs/fat/dir.c
index 4ad64732cbc..dc563788fa8 100644
--- a/fs/fat/dir.c
+++ b/fs/fat/dir.c
@@ -754,6 +754,13 @@ static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
754 return ret; 754 return ret;
755} 755}
756 756
757static int fat_ioctl_volume_id(struct inode *dir)
758{
759 struct super_block *sb = dir->i_sb;
760 struct msdos_sb_info *sbi = MSDOS_SB(sb);
761 return sbi->vol_id;
762}
763
757static long fat_dir_ioctl(struct file *filp, unsigned int cmd, 764static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
758 unsigned long arg) 765 unsigned long arg)
759{ 766{
@@ -770,6 +777,8 @@ static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
770 short_only = 0; 777 short_only = 0;
771 both = 1; 778 both = 1;
772 break; 779 break;
780 case VFAT_IOCTL_GET_VOLUME_ID:
781 return fat_ioctl_volume_id(inode);
773 default: 782 default:
774 return fat_generic_ioctl(filp, cmd, arg); 783 return fat_generic_ioctl(filp, cmd, arg);
775 } 784 }
diff --git a/fs/fat/fat.h b/fs/fat/fat.h
index 8276cc282de..74fe5d3ed01 100644
--- a/fs/fat/fat.h
+++ b/fs/fat/fat.h
@@ -78,6 +78,7 @@ struct msdos_sb_info {
78 const void *dir_ops; /* Opaque; default directory operations */ 78 const void *dir_ops; /* Opaque; default directory operations */
79 int dir_per_block; /* dir entries per block */ 79 int dir_per_block; /* dir entries per block */
80 int dir_per_block_bits; /* log2(dir_per_block) */ 80 int dir_per_block_bits; /* log2(dir_per_block) */
81 unsigned long vol_id; /* volume ID */
81 82
82 int fatent_shift; 83 int fatent_shift;
83 struct fatent_operations *fatent_ops; 84 struct fatent_operations *fatent_ops;
diff --git a/fs/fat/inode.c b/fs/fat/inode.c
index cb8d8391ac0..9836839e0eb 100644
--- a/fs/fat/inode.c
+++ b/fs/fat/inode.c
@@ -1245,6 +1245,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
1245 struct inode *root_inode = NULL, *fat_inode = NULL; 1245 struct inode *root_inode = NULL, *fat_inode = NULL;
1246 struct buffer_head *bh; 1246 struct buffer_head *bh;
1247 struct fat_boot_sector *b; 1247 struct fat_boot_sector *b;
1248 struct fat_boot_bsx *bsx;
1248 struct msdos_sb_info *sbi; 1249 struct msdos_sb_info *sbi;
1249 u16 logical_sector_size; 1250 u16 logical_sector_size;
1250 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors; 1251 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
@@ -1390,6 +1391,8 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
1390 goto out_fail; 1391 goto out_fail;
1391 } 1392 }
1392 1393
1394 bsx = (struct fat_boot_bsx *)(bh->b_data + FAT32_BSX_OFFSET);
1395
1393 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data; 1396 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1394 if (!IS_FSINFO(fsinfo)) { 1397 if (!IS_FSINFO(fsinfo)) {
1395 fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: " 1398 fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
@@ -1405,8 +1408,14 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
1405 } 1408 }
1406 1409
1407 brelse(fsinfo_bh); 1410 brelse(fsinfo_bh);
1411 } else {
1412 bsx = (struct fat_boot_bsx *)(bh->b_data + FAT16_BSX_OFFSET);
1408 } 1413 }
1409 1414
1415 /* interpret volume ID as a little endian 32 bit integer */
1416 sbi->vol_id = (((u32)bsx->vol_id[0]) | ((u32)bsx->vol_id[1] << 8) |
1417 ((u32)bsx->vol_id[2] << 16) | ((u32)bsx->vol_id[3] << 24));
1418
1410 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry); 1419 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
1411 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; 1420 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
1412 1421
diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index fe190a8b0bc..1d9ebbbd343 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -35,16 +35,29 @@
35struct wb_writeback_work { 35struct wb_writeback_work {
36 long nr_pages; 36 long nr_pages;
37 struct super_block *sb; 37 struct super_block *sb;
38 unsigned long *older_than_this;
38 enum writeback_sync_modes sync_mode; 39 enum writeback_sync_modes sync_mode;
39 unsigned int tagged_writepages:1; 40 unsigned int tagged_writepages:1;
40 unsigned int for_kupdate:1; 41 unsigned int for_kupdate:1;
41 unsigned int range_cyclic:1; 42 unsigned int range_cyclic:1;
42 unsigned int for_background:1; 43 unsigned int for_background:1;
44 enum wb_reason reason; /* why was writeback initiated? */
43 45
44 struct list_head list; /* pending work list */ 46 struct list_head list; /* pending work list */
45 struct completion *done; /* set if the caller waits */ 47 struct completion *done; /* set if the caller waits */
46}; 48};
47 49
50const char *wb_reason_name[] = {
51 [WB_REASON_BACKGROUND] = "background",
52 [WB_REASON_TRY_TO_FREE_PAGES] = "try_to_free_pages",
53 [WB_REASON_SYNC] = "sync",
54 [WB_REASON_PERIODIC] = "periodic",
55 [WB_REASON_LAPTOP_TIMER] = "laptop_timer",
56 [WB_REASON_FREE_MORE_MEM] = "free_more_memory",
57 [WB_REASON_FS_FREE_SPACE] = "fs_free_space",
58 [WB_REASON_FORKER_THREAD] = "forker_thread"
59};
60
48/* 61/*
49 * Include the creation of the trace points after defining the 62 * Include the creation of the trace points after defining the
50 * wb_writeback_work structure so that the definition remains local to this 63 * wb_writeback_work structure so that the definition remains local to this
@@ -114,7 +127,7 @@ static void bdi_queue_work(struct backing_dev_info *bdi,
114 127
115static void 128static void
116__bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 129__bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
117 bool range_cyclic) 130 bool range_cyclic, enum wb_reason reason)
118{ 131{
119 struct wb_writeback_work *work; 132 struct wb_writeback_work *work;
120 133
@@ -134,6 +147,7 @@ __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
134 work->sync_mode = WB_SYNC_NONE; 147 work->sync_mode = WB_SYNC_NONE;
135 work->nr_pages = nr_pages; 148 work->nr_pages = nr_pages;
136 work->range_cyclic = range_cyclic; 149 work->range_cyclic = range_cyclic;
150 work->reason = reason;
137 151
138 bdi_queue_work(bdi, work); 152 bdi_queue_work(bdi, work);
139} 153}
@@ -149,9 +163,10 @@ __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
149 * completion. Caller need not hold sb s_umount semaphore. 163 * completion. Caller need not hold sb s_umount semaphore.
150 * 164 *
151 */ 165 */
152void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages) 166void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
167 enum wb_reason reason)
153{ 168{
154 __bdi_start_writeback(bdi, nr_pages, true); 169 __bdi_start_writeback(bdi, nr_pages, true, reason);
155} 170}
156 171
157/** 172/**
@@ -181,12 +196,13 @@ void bdi_start_background_writeback(struct backing_dev_info *bdi)
181 */ 196 */
182void inode_wb_list_del(struct inode *inode) 197void inode_wb_list_del(struct inode *inode)
183{ 198{
184 spin_lock(&inode_wb_list_lock); 199 struct backing_dev_info *bdi = inode_to_bdi(inode);
200
201 spin_lock(&bdi->wb.list_lock);
185 list_del_init(&inode->i_wb_list); 202 list_del_init(&inode->i_wb_list);
186 spin_unlock(&inode_wb_list_lock); 203 spin_unlock(&bdi->wb.list_lock);
187} 204}
188 205
189
190/* 206/*
191 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 207 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
192 * furthest end of its superblock's dirty-inode list. 208 * furthest end of its superblock's dirty-inode list.
@@ -196,11 +212,9 @@ void inode_wb_list_del(struct inode *inode)
196 * the case then the inode must have been redirtied while it was being written 212 * the case then the inode must have been redirtied while it was being written
197 * out and we don't reset its dirtied_when. 213 * out and we don't reset its dirtied_when.
198 */ 214 */
199static void redirty_tail(struct inode *inode) 215static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
200{ 216{
201 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 217 assert_spin_locked(&wb->list_lock);
202
203 assert_spin_locked(&inode_wb_list_lock);
204 if (!list_empty(&wb->b_dirty)) { 218 if (!list_empty(&wb->b_dirty)) {
205 struct inode *tail; 219 struct inode *tail;
206 220
@@ -214,11 +228,9 @@ static void redirty_tail(struct inode *inode)
214/* 228/*
215 * requeue inode for re-scanning after bdi->b_io list is exhausted. 229 * requeue inode for re-scanning after bdi->b_io list is exhausted.
216 */ 230 */
217static void requeue_io(struct inode *inode) 231static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
218{ 232{
219 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 233 assert_spin_locked(&wb->list_lock);
220
221 assert_spin_locked(&inode_wb_list_lock);
222 list_move(&inode->i_wb_list, &wb->b_more_io); 234 list_move(&inode->i_wb_list, &wb->b_more_io);
223} 235}
224 236
@@ -226,7 +238,7 @@ static void inode_sync_complete(struct inode *inode)
226{ 238{
227 /* 239 /*
228 * Prevent speculative execution through 240 * Prevent speculative execution through
229 * spin_unlock(&inode_wb_list_lock); 241 * spin_unlock(&wb->list_lock);
230 */ 242 */
231 243
232 smp_mb(); 244 smp_mb();
@@ -251,15 +263,16 @@ static bool inode_dirtied_after(struct inode *inode, unsigned long t)
251/* 263/*
252 * Move expired dirty inodes from @delaying_queue to @dispatch_queue. 264 * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
253 */ 265 */
254static void move_expired_inodes(struct list_head *delaying_queue, 266static int move_expired_inodes(struct list_head *delaying_queue,
255 struct list_head *dispatch_queue, 267 struct list_head *dispatch_queue,
256 unsigned long *older_than_this) 268 unsigned long *older_than_this)
257{ 269{
258 LIST_HEAD(tmp); 270 LIST_HEAD(tmp);
259 struct list_head *pos, *node; 271 struct list_head *pos, *node;
260 struct super_block *sb = NULL; 272 struct super_block *sb = NULL;
261 struct inode *inode; 273 struct inode *inode;
262 int do_sb_sort = 0; 274 int do_sb_sort = 0;
275 int moved = 0;
263 276
264 while (!list_empty(delaying_queue)) { 277 while (!list_empty(delaying_queue)) {
265 inode = wb_inode(delaying_queue->prev); 278 inode = wb_inode(delaying_queue->prev);
@@ -270,12 +283,13 @@ static void move_expired_inodes(struct list_head *delaying_queue,
270 do_sb_sort = 1; 283 do_sb_sort = 1;
271 sb = inode->i_sb; 284 sb = inode->i_sb;
272 list_move(&inode->i_wb_list, &tmp); 285 list_move(&inode->i_wb_list, &tmp);
286 moved++;
273 } 287 }
274 288
275 /* just one sb in list, splice to dispatch_queue and we're done */ 289 /* just one sb in list, splice to dispatch_queue and we're done */
276 if (!do_sb_sort) { 290 if (!do_sb_sort) {
277 list_splice(&tmp, dispatch_queue); 291 list_splice(&tmp, dispatch_queue);
278 return; 292 goto out;
279 } 293 }
280 294
281 /* Move inodes from one superblock together */ 295 /* Move inodes from one superblock together */
@@ -287,6 +301,8 @@ static void move_expired_inodes(struct list_head *delaying_queue,
287 list_move(&inode->i_wb_list, dispatch_queue); 301 list_move(&inode->i_wb_list, dispatch_queue);
288 } 302 }
289 } 303 }
304out:
305 return moved;
290} 306}
291 307
292/* 308/*
@@ -302,9 +318,11 @@ static void move_expired_inodes(struct list_head *delaying_queue,
302 */ 318 */
303static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this) 319static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
304{ 320{
305 assert_spin_locked(&inode_wb_list_lock); 321 int moved;
322 assert_spin_locked(&wb->list_lock);
306 list_splice_init(&wb->b_more_io, &wb->b_io); 323 list_splice_init(&wb->b_more_io, &wb->b_io);
307 move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this); 324 moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
325 trace_writeback_queue_io(wb, older_than_this, moved);
308} 326}
309 327
310static int write_inode(struct inode *inode, struct writeback_control *wbc) 328static int write_inode(struct inode *inode, struct writeback_control *wbc)
@@ -317,7 +335,8 @@ static int write_inode(struct inode *inode, struct writeback_control *wbc)
317/* 335/*
318 * Wait for writeback on an inode to complete. 336 * Wait for writeback on an inode to complete.
319 */ 337 */
320static void inode_wait_for_writeback(struct inode *inode) 338static void inode_wait_for_writeback(struct inode *inode,
339 struct bdi_writeback *wb)
321{ 340{
322 DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 341 DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
323 wait_queue_head_t *wqh; 342 wait_queue_head_t *wqh;
@@ -325,15 +344,15 @@ static void inode_wait_for_writeback(struct inode *inode)
325 wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 344 wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
326 while (inode->i_state & I_SYNC) { 345 while (inode->i_state & I_SYNC) {
327 spin_unlock(&inode->i_lock); 346 spin_unlock(&inode->i_lock);
328 spin_unlock(&inode_wb_list_lock); 347 spin_unlock(&wb->list_lock);
329 __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); 348 __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
330 spin_lock(&inode_wb_list_lock); 349 spin_lock(&wb->list_lock);
331 spin_lock(&inode->i_lock); 350 spin_lock(&inode->i_lock);
332 } 351 }
333} 352}
334 353
335/* 354/*
336 * Write out an inode's dirty pages. Called under inode_wb_list_lock and 355 * Write out an inode's dirty pages. Called under wb->list_lock and
337 * inode->i_lock. Either the caller has an active reference on the inode or 356 * inode->i_lock. Either the caller has an active reference on the inode or
338 * the inode has I_WILL_FREE set. 357 * the inode has I_WILL_FREE set.
339 * 358 *
@@ -344,13 +363,15 @@ static void inode_wait_for_writeback(struct inode *inode)
344 * livelocks, etc. 363 * livelocks, etc.
345 */ 364 */
346static int 365static int
347writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 366writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
367 struct writeback_control *wbc)
348{ 368{
349 struct address_space *mapping = inode->i_mapping; 369 struct address_space *mapping = inode->i_mapping;
370 long nr_to_write = wbc->nr_to_write;
350 unsigned dirty; 371 unsigned dirty;
351 int ret; 372 int ret;
352 373
353 assert_spin_locked(&inode_wb_list_lock); 374 assert_spin_locked(&wb->list_lock);
354 assert_spin_locked(&inode->i_lock); 375 assert_spin_locked(&inode->i_lock);
355 376
356 if (!atomic_read(&inode->i_count)) 377 if (!atomic_read(&inode->i_count))
@@ -368,14 +389,16 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
368 * completed a full scan of b_io. 389 * completed a full scan of b_io.
369 */ 390 */
370 if (wbc->sync_mode != WB_SYNC_ALL) { 391 if (wbc->sync_mode != WB_SYNC_ALL) {
371 requeue_io(inode); 392 requeue_io(inode, wb);
393 trace_writeback_single_inode_requeue(inode, wbc,
394 nr_to_write);
372 return 0; 395 return 0;
373 } 396 }
374 397
375 /* 398 /*
376 * It's a data-integrity sync. We must wait. 399 * It's a data-integrity sync. We must wait.
377 */ 400 */
378 inode_wait_for_writeback(inode); 401 inode_wait_for_writeback(inode, wb);
379 } 402 }
380 403
381 BUG_ON(inode->i_state & I_SYNC); 404 BUG_ON(inode->i_state & I_SYNC);
@@ -384,7 +407,7 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
384 inode->i_state |= I_SYNC; 407 inode->i_state |= I_SYNC;
385 inode->i_state &= ~I_DIRTY_PAGES; 408 inode->i_state &= ~I_DIRTY_PAGES;
386 spin_unlock(&inode->i_lock); 409 spin_unlock(&inode->i_lock);
387 spin_unlock(&inode_wb_list_lock); 410 spin_unlock(&wb->list_lock);
388 411
389 ret = do_writepages(mapping, wbc); 412 ret = do_writepages(mapping, wbc);
390 413
@@ -415,7 +438,7 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
415 ret = err; 438 ret = err;
416 } 439 }
417 440
418 spin_lock(&inode_wb_list_lock); 441 spin_lock(&wb->list_lock);
419 spin_lock(&inode->i_lock); 442 spin_lock(&inode->i_lock);
420 inode->i_state &= ~I_SYNC; 443 inode->i_state &= ~I_SYNC;
421 if (!(inode->i_state & I_FREEING)) { 444 if (!(inode->i_state & I_FREEING)) {
@@ -438,7 +461,7 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
438 /* 461 /*
439 * slice used up: queue for next turn 462 * slice used up: queue for next turn
440 */ 463 */
441 requeue_io(inode); 464 requeue_io(inode, wb);
442 } else { 465 } else {
443 /* 466 /*
444 * Writeback blocked by something other than 467 * Writeback blocked by something other than
@@ -447,7 +470,7 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
447 * retrying writeback of the dirty page/inode 470 * retrying writeback of the dirty page/inode
448 * that cannot be performed immediately. 471 * that cannot be performed immediately.
449 */ 472 */
450 redirty_tail(inode); 473 redirty_tail(inode, wb);
451 } 474 }
452 } else if (inode->i_state & I_DIRTY) { 475 } else if (inode->i_state & I_DIRTY) {
453 /* 476 /*
@@ -456,7 +479,7 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
456 * submission or metadata updates after data IO 479 * submission or metadata updates after data IO
457 * completion. 480 * completion.
458 */ 481 */
459 redirty_tail(inode); 482 redirty_tail(inode, wb);
460 } else { 483 } else {
461 /* 484 /*
462 * The inode is clean. At this point we either have 485 * The inode is clean. At this point we either have
@@ -467,33 +490,39 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
467 } 490 }
468 } 491 }
469 inode_sync_complete(inode); 492 inode_sync_complete(inode);
493 trace_writeback_single_inode(inode, wbc, nr_to_write);
470 return ret; 494 return ret;
471} 495}
472 496
473/* 497static long writeback_chunk_size(struct backing_dev_info *bdi,
474 * For background writeback the caller does not have the sb pinned 498 struct wb_writeback_work *work)
475 * before calling writeback. So make sure that we do pin it, so it doesn't
476 * go away while we are writing inodes from it.
477 */
478static bool pin_sb_for_writeback(struct super_block *sb)
479{ 499{
480 spin_lock(&sb_lock); 500 long pages;
481 if (list_empty(&sb->s_instances)) {
482 spin_unlock(&sb_lock);
483 return false;
484 }
485
486 sb->s_count++;
487 spin_unlock(&sb_lock);
488 501
489 if (down_read_trylock(&sb->s_umount)) { 502 /*
490 if (sb->s_root) 503 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
491 return true; 504 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
492 up_read(&sb->s_umount); 505 * here avoids calling into writeback_inodes_wb() more than once.
506 *
507 * The intended call sequence for WB_SYNC_ALL writeback is:
508 *
509 * wb_writeback()
510 * writeback_sb_inodes() <== called only once
511 * write_cache_pages() <== called once for each inode
512 * (quickly) tag currently dirty pages
513 * (maybe slowly) sync all tagged pages
514 */
515 if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
516 pages = LONG_MAX;
517 else {
518 pages = min(bdi->avg_write_bandwidth / 2,
519 global_dirty_limit / DIRTY_SCOPE);
520 pages = min(pages, work->nr_pages);
521 pages = round_down(pages + MIN_WRITEBACK_PAGES,
522 MIN_WRITEBACK_PAGES);
493 } 523 }
494 524
495 put_super(sb); 525 return pages;
496 return false;
497} 526}
498 527
499/* 528/*
@@ -503,24 +532,36 @@ static bool pin_sb_for_writeback(struct super_block *sb)
503 * inodes. Otherwise write only ones which go sequentially 532 * inodes. Otherwise write only ones which go sequentially
504 * in reverse order. 533 * in reverse order.
505 * 534 *
506 * Return 1, if the caller writeback routine should be 535 * Return the number of pages and/or inodes written.
507 * interrupted. Otherwise return 0.
508 */ 536 */
509static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb, 537static long writeback_sb_inodes(struct super_block *sb,
510 struct writeback_control *wbc, bool only_this_sb) 538 struct bdi_writeback *wb,
539 struct wb_writeback_work *work)
511{ 540{
541 struct writeback_control wbc = {
542 .sync_mode = work->sync_mode,
543 .tagged_writepages = work->tagged_writepages,
544 .for_kupdate = work->for_kupdate,
545 .for_background = work->for_background,
546 .range_cyclic = work->range_cyclic,
547 .range_start = 0,
548 .range_end = LLONG_MAX,
549 };
550 unsigned long start_time = jiffies;
551 long write_chunk;
552 long wrote = 0; /* count both pages and inodes */
553
512 while (!list_empty(&wb->b_io)) { 554 while (!list_empty(&wb->b_io)) {
513 long pages_skipped;
514 struct inode *inode = wb_inode(wb->b_io.prev); 555 struct inode *inode = wb_inode(wb->b_io.prev);
515 556
516 if (inode->i_sb != sb) { 557 if (inode->i_sb != sb) {
517 if (only_this_sb) { 558 if (work->sb) {
518 /* 559 /*
519 * We only want to write back data for this 560 * We only want to write back data for this
520 * superblock, move all inodes not belonging 561 * superblock, move all inodes not belonging
521 * to it back onto the dirty list. 562 * to it back onto the dirty list.
522 */ 563 */
523 redirty_tail(inode); 564 redirty_tail(inode, wb);
524 continue; 565 continue;
525 } 566 }
526 567
@@ -529,7 +570,7 @@ static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
529 * Bounce back to the caller to unpin this and 570 * Bounce back to the caller to unpin this and
530 * pin the next superblock. 571 * pin the next superblock.
531 */ 572 */
532 return 0; 573 break;
533 } 574 }
534 575
535 /* 576 /*
@@ -540,104 +581,124 @@ static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
540 spin_lock(&inode->i_lock); 581 spin_lock(&inode->i_lock);
541 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 582 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
542 spin_unlock(&inode->i_lock); 583 spin_unlock(&inode->i_lock);
543 requeue_io(inode); 584 redirty_tail(inode, wb);
544 continue; 585 continue;
545 } 586 }
546
547 /*
548 * Was this inode dirtied after sync_sb_inodes was called?
549 * This keeps sync from extra jobs and livelock.
550 */
551 if (inode_dirtied_after(inode, wbc->wb_start)) {
552 spin_unlock(&inode->i_lock);
553 return 1;
554 }
555
556 __iget(inode); 587 __iget(inode);
588 write_chunk = writeback_chunk_size(wb->bdi, work);
589 wbc.nr_to_write = write_chunk;
590 wbc.pages_skipped = 0;
591
592 writeback_single_inode(inode, wb, &wbc);
557 593
558 pages_skipped = wbc->pages_skipped; 594 work->nr_pages -= write_chunk - wbc.nr_to_write;
559 writeback_single_inode(inode, wbc); 595 wrote += write_chunk - wbc.nr_to_write;
560 if (wbc->pages_skipped != pages_skipped) { 596 if (!(inode->i_state & I_DIRTY))
597 wrote++;
598 if (wbc.pages_skipped) {
561 /* 599 /*
562 * writeback is not making progress due to locked 600 * writeback is not making progress due to locked
563 * buffers. Skip this inode for now. 601 * buffers. Skip this inode for now.
564 */ 602 */
565 redirty_tail(inode); 603 redirty_tail(inode, wb);
566 } 604 }
567 spin_unlock(&inode->i_lock); 605 spin_unlock(&inode->i_lock);
568 spin_unlock(&inode_wb_list_lock); 606 spin_unlock(&wb->list_lock);
569 iput(inode); 607 iput(inode);
570 cond_resched(); 608 cond_resched();
571 spin_lock(&inode_wb_list_lock); 609 spin_lock(&wb->list_lock);
572 if (wbc->nr_to_write <= 0) { 610 /*
573 wbc->more_io = 1; 611 * bail out to wb_writeback() often enough to check
574 return 1; 612 * background threshold and other termination conditions.
613 */
614 if (wrote) {
615 if (time_is_before_jiffies(start_time + HZ / 10UL))
616 break;
617 if (work->nr_pages <= 0)
618 break;
575 } 619 }
576 if (!list_empty(&wb->b_more_io))
577 wbc->more_io = 1;
578 } 620 }
579 /* b_io is empty */ 621 return wrote;
580 return 1;
581} 622}
582 623
583void writeback_inodes_wb(struct bdi_writeback *wb, 624static long __writeback_inodes_wb(struct bdi_writeback *wb,
584 struct writeback_control *wbc) 625 struct wb_writeback_work *work)
585{ 626{
586 int ret = 0; 627 unsigned long start_time = jiffies;
587 628 long wrote = 0;
588 if (!wbc->wb_start)
589 wbc->wb_start = jiffies; /* livelock avoidance */
590 spin_lock(&inode_wb_list_lock);
591 if (!wbc->for_kupdate || list_empty(&wb->b_io))
592 queue_io(wb, wbc->older_than_this);
593 629
594 while (!list_empty(&wb->b_io)) { 630 while (!list_empty(&wb->b_io)) {
595 struct inode *inode = wb_inode(wb->b_io.prev); 631 struct inode *inode = wb_inode(wb->b_io.prev);
596 struct super_block *sb = inode->i_sb; 632 struct super_block *sb = inode->i_sb;
597 633
598 if (!pin_sb_for_writeback(sb)) { 634 if (!grab_super_passive(sb)) {
599 requeue_io(inode); 635 /*
636 * grab_super_passive() may fail consistently due to
637 * s_umount being grabbed by someone else. Don't use
638 * requeue_io() to avoid busy retrying the inode/sb.
639 */
640 redirty_tail(inode, wb);
600 continue; 641 continue;
601 } 642 }
602 ret = writeback_sb_inodes(sb, wb, wbc, false); 643 wrote += writeback_sb_inodes(sb, wb, work);
603 drop_super(sb); 644 drop_super(sb);
604 645
605 if (ret) 646 /* refer to the same tests at the end of writeback_sb_inodes */
606 break; 647 if (wrote) {
648 if (time_is_before_jiffies(start_time + HZ / 10UL))
649 break;
650 if (work->nr_pages <= 0)
651 break;
652 }
607 } 653 }
608 spin_unlock(&inode_wb_list_lock);
609 /* Leave any unwritten inodes on b_io */ 654 /* Leave any unwritten inodes on b_io */
655 return wrote;
610} 656}
611 657
612static void __writeback_inodes_sb(struct super_block *sb, 658long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
613 struct bdi_writeback *wb, struct writeback_control *wbc) 659 enum wb_reason reason)
614{ 660{
615 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 661 struct wb_writeback_work work = {
662 .nr_pages = nr_pages,
663 .sync_mode = WB_SYNC_NONE,
664 .range_cyclic = 1,
665 .reason = reason,
666 };
616 667
617 spin_lock(&inode_wb_list_lock); 668 spin_lock(&wb->list_lock);
618 if (!wbc->for_kupdate || list_empty(&wb->b_io)) 669 if (list_empty(&wb->b_io))
619 queue_io(wb, wbc->older_than_this); 670 queue_io(wb, NULL);
620 writeback_sb_inodes(sb, wb, wbc, true); 671 __writeback_inodes_wb(wb, &work);
621 spin_unlock(&inode_wb_list_lock); 672 spin_unlock(&wb->list_lock);
622}
623 673
624/* 674 return nr_pages - work.nr_pages;
625 * The maximum number of pages to writeout in a single bdi flush/kupdate 675}
626 * operation. We do this so we don't hold I_SYNC against an inode for
627 * enormous amounts of time, which would block a userspace task which has
628 * been forced to throttle against that inode. Also, the code reevaluates
629 * the dirty each time it has written this many pages.
630 */
631#define MAX_WRITEBACK_PAGES 1024
632 676
633static inline bool over_bground_thresh(void) 677static bool over_bground_thresh(struct backing_dev_info *bdi)
634{ 678{
635 unsigned long background_thresh, dirty_thresh; 679 unsigned long background_thresh, dirty_thresh;
636 680
637 global_dirty_limits(&background_thresh, &dirty_thresh); 681 global_dirty_limits(&background_thresh, &dirty_thresh);
638 682
639 return (global_page_state(NR_FILE_DIRTY) + 683 if (global_page_state(NR_FILE_DIRTY) +
640 global_page_state(NR_UNSTABLE_NFS) > background_thresh); 684 global_page_state(NR_UNSTABLE_NFS) > background_thresh)
685 return true;
686
687 if (bdi_stat(bdi, BDI_RECLAIMABLE) >
688 bdi_dirty_limit(bdi, background_thresh))
689 return true;
690
691 return false;
692}
693
694/*
695 * Called under wb->list_lock. If there are multiple wb per bdi,
696 * only the flusher working on the first wb should do it.
697 */
698static void wb_update_bandwidth(struct bdi_writeback *wb,
699 unsigned long start_time)
700{
701 __bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
641} 702}
642 703
643/* 704/*
@@ -658,46 +719,16 @@ static inline bool over_bground_thresh(void)
658static long wb_writeback(struct bdi_writeback *wb, 719static long wb_writeback(struct bdi_writeback *wb,
659 struct wb_writeback_work *work) 720 struct wb_writeback_work *work)
660{ 721{
661 struct writeback_control wbc = { 722 unsigned long wb_start = jiffies;
662 .sync_mode = work->sync_mode, 723 long nr_pages = work->nr_pages;
663 .tagged_writepages = work->tagged_writepages,
664 .older_than_this = NULL,
665 .for_kupdate = work->for_kupdate,
666 .for_background = work->for_background,
667 .range_cyclic = work->range_cyclic,
668 };
669 unsigned long oldest_jif; 724 unsigned long oldest_jif;
670 long wrote = 0;
671 long write_chunk = MAX_WRITEBACK_PAGES;
672 struct inode *inode; 725 struct inode *inode;
726 long progress;
673 727
674 if (wbc.for_kupdate) { 728 oldest_jif = jiffies;
675 wbc.older_than_this = &oldest_jif; 729 work->older_than_this = &oldest_jif;
676 oldest_jif = jiffies -
677 msecs_to_jiffies(dirty_expire_interval * 10);
678 }
679 if (!wbc.range_cyclic) {
680 wbc.range_start = 0;
681 wbc.range_end = LLONG_MAX;
682 }
683
684 /*
685 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
686 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
687 * here avoids calling into writeback_inodes_wb() more than once.
688 *
689 * The intended call sequence for WB_SYNC_ALL writeback is:
690 *
691 * wb_writeback()
692 * __writeback_inodes_sb() <== called only once
693 * write_cache_pages() <== called once for each inode
694 * (quickly) tag currently dirty pages
695 * (maybe slowly) sync all tagged pages
696 */
697 if (wbc.sync_mode == WB_SYNC_ALL || wbc.tagged_writepages)
698 write_chunk = LONG_MAX;
699 730
700 wbc.wb_start = jiffies; /* livelock avoidance */ 731 spin_lock(&wb->list_lock);
701 for (;;) { 732 for (;;) {
702 /* 733 /*
703 * Stop writeback when nr_pages has been consumed 734 * Stop writeback when nr_pages has been consumed
@@ -719,55 +750,57 @@ static long wb_writeback(struct bdi_writeback *wb,
719 * For background writeout, stop when we are below the 750 * For background writeout, stop when we are below the
720 * background dirty threshold 751 * background dirty threshold
721 */ 752 */
722 if (work->for_background && !over_bground_thresh()) 753 if (work->for_background && !over_bground_thresh(wb->bdi))
723 break; 754 break;
724 755
725 wbc.more_io = 0; 756 if (work->for_kupdate) {
726 wbc.nr_to_write = write_chunk; 757 oldest_jif = jiffies -
727 wbc.pages_skipped = 0; 758 msecs_to_jiffies(dirty_expire_interval * 10);
759 work->older_than_this = &oldest_jif;
760 }
728 761
729 trace_wbc_writeback_start(&wbc, wb->bdi); 762 trace_writeback_start(wb->bdi, work);
763 if (list_empty(&wb->b_io))
764 queue_io(wb, work->older_than_this);
730 if (work->sb) 765 if (work->sb)
731 __writeback_inodes_sb(work->sb, wb, &wbc); 766 progress = writeback_sb_inodes(work->sb, wb, work);
732 else 767 else
733 writeback_inodes_wb(wb, &wbc); 768 progress = __writeback_inodes_wb(wb, work);
734 trace_wbc_writeback_written(&wbc, wb->bdi); 769 trace_writeback_written(wb->bdi, work);
735 770
736 work->nr_pages -= write_chunk - wbc.nr_to_write; 771 wb_update_bandwidth(wb, wb_start);
737 wrote += write_chunk - wbc.nr_to_write;
738 772
739 /* 773 /*
740 * If we consumed everything, see if we have more 774 * Did we write something? Try for more
775 *
776 * Dirty inodes are moved to b_io for writeback in batches.
777 * The completion of the current batch does not necessarily
778 * mean the overall work is done. So we keep looping as long
779 * as made some progress on cleaning pages or inodes.
741 */ 780 */
742 if (wbc.nr_to_write <= 0) 781 if (progress)
743 continue; 782 continue;
744 /* 783 /*
745 * Didn't write everything and we don't have more IO, bail 784 * No more inodes for IO, bail
746 */ 785 */
747 if (!wbc.more_io) 786 if (list_empty(&wb->b_more_io))
748 break; 787 break;
749 /* 788 /*
750 * Did we write something? Try for more
751 */
752 if (wbc.nr_to_write < write_chunk)
753 continue;
754 /*
755 * Nothing written. Wait for some inode to 789 * Nothing written. Wait for some inode to
756 * become available for writeback. Otherwise 790 * become available for writeback. Otherwise
757 * we'll just busyloop. 791 * we'll just busyloop.
758 */ 792 */
759 spin_lock(&inode_wb_list_lock);
760 if (!list_empty(&wb->b_more_io)) { 793 if (!list_empty(&wb->b_more_io)) {
794 trace_writeback_wait(wb->bdi, work);
761 inode = wb_inode(wb->b_more_io.prev); 795 inode = wb_inode(wb->b_more_io.prev);
762 trace_wbc_writeback_wait(&wbc, wb->bdi);
763 spin_lock(&inode->i_lock); 796 spin_lock(&inode->i_lock);
764 inode_wait_for_writeback(inode); 797 inode_wait_for_writeback(inode, wb);
765 spin_unlock(&inode->i_lock); 798 spin_unlock(&inode->i_lock);
766 } 799 }
767 spin_unlock(&inode_wb_list_lock);
768 } 800 }
801 spin_unlock(&wb->list_lock);
769 802
770 return wrote; 803 return nr_pages - work->nr_pages;
771} 804}
772 805
773/* 806/*
@@ -801,13 +834,14 @@ static unsigned long get_nr_dirty_pages(void)
801 834
802static long wb_check_background_flush(struct bdi_writeback *wb) 835static long wb_check_background_flush(struct bdi_writeback *wb)
803{ 836{
804 if (over_bground_thresh()) { 837 if (over_bground_thresh(wb->bdi)) {
805 838
806 struct wb_writeback_work work = { 839 struct wb_writeback_work work = {
807 .nr_pages = LONG_MAX, 840 .nr_pages = LONG_MAX,
808 .sync_mode = WB_SYNC_NONE, 841 .sync_mode = WB_SYNC_NONE,
809 .for_background = 1, 842 .for_background = 1,
810 .range_cyclic = 1, 843 .range_cyclic = 1,
844 .reason = WB_REASON_BACKGROUND,
811 }; 845 };
812 846
813 return wb_writeback(wb, &work); 847 return wb_writeback(wb, &work);
@@ -841,6 +875,7 @@ static long wb_check_old_data_flush(struct bdi_writeback *wb)
841 .sync_mode = WB_SYNC_NONE, 875 .sync_mode = WB_SYNC_NONE,
842 .for_kupdate = 1, 876 .for_kupdate = 1,
843 .range_cyclic = 1, 877 .range_cyclic = 1,
878 .reason = WB_REASON_PERIODIC,
844 }; 879 };
845 880
846 return wb_writeback(wb, &work); 881 return wb_writeback(wb, &work);
@@ -959,7 +994,7 @@ int bdi_writeback_thread(void *data)
959 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 994 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
960 * the whole world. 995 * the whole world.
961 */ 996 */
962void wakeup_flusher_threads(long nr_pages) 997void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
963{ 998{
964 struct backing_dev_info *bdi; 999 struct backing_dev_info *bdi;
965 1000
@@ -972,7 +1007,7 @@ void wakeup_flusher_threads(long nr_pages)
972 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1007 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
973 if (!bdi_has_dirty_io(bdi)) 1008 if (!bdi_has_dirty_io(bdi))
974 continue; 1009 continue;
975 __bdi_start_writeback(bdi, nr_pages, false); 1010 __bdi_start_writeback(bdi, nr_pages, false, reason);
976 } 1011 }
977 rcu_read_unlock(); 1012 rcu_read_unlock();
978} 1013}
@@ -1098,10 +1133,10 @@ void __mark_inode_dirty(struct inode *inode, int flags)
1098 } 1133 }
1099 1134
1100 spin_unlock(&inode->i_lock); 1135 spin_unlock(&inode->i_lock);
1101 spin_lock(&inode_wb_list_lock); 1136 spin_lock(&bdi->wb.list_lock);
1102 inode->dirtied_when = jiffies; 1137 inode->dirtied_when = jiffies;
1103 list_move(&inode->i_wb_list, &bdi->wb.b_dirty); 1138 list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1104 spin_unlock(&inode_wb_list_lock); 1139 spin_unlock(&bdi->wb.list_lock);
1105 1140
1106 if (wakeup_bdi) 1141 if (wakeup_bdi)
1107 bdi_wakeup_thread_delayed(bdi); 1142 bdi_wakeup_thread_delayed(bdi);
@@ -1193,7 +1228,9 @@ static void wait_sb_inodes(struct super_block *sb)
1193 * on how many (if any) will be written, and this function does not wait 1228 * on how many (if any) will be written, and this function does not wait
1194 * for IO completion of submitted IO. 1229 * for IO completion of submitted IO.
1195 */ 1230 */
1196void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr) 1231void writeback_inodes_sb_nr(struct super_block *sb,
1232 unsigned long nr,
1233 enum wb_reason reason)
1197{ 1234{
1198 DECLARE_COMPLETION_ONSTACK(done); 1235 DECLARE_COMPLETION_ONSTACK(done);
1199 struct wb_writeback_work work = { 1236 struct wb_writeback_work work = {
@@ -1202,6 +1239,7 @@ void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr)
1202 .tagged_writepages = 1, 1239 .tagged_writepages = 1,
1203 .done = &done, 1240 .done = &done,
1204 .nr_pages = nr, 1241 .nr_pages = nr,
1242 .reason = reason,
1205 }; 1243 };
1206 1244
1207 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1245 WARN_ON(!rwsem_is_locked(&sb->s_umount));
@@ -1218,9 +1256,9 @@ EXPORT_SYMBOL(writeback_inodes_sb_nr);
1218 * on how many (if any) will be written, and this function does not wait 1256 * on how many (if any) will be written, and this function does not wait
1219 * for IO completion of submitted IO. 1257 * for IO completion of submitted IO.
1220 */ 1258 */
1221void writeback_inodes_sb(struct super_block *sb) 1259void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
1222{ 1260{
1223 return writeback_inodes_sb_nr(sb, get_nr_dirty_pages()); 1261 return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
1224} 1262}
1225EXPORT_SYMBOL(writeback_inodes_sb); 1263EXPORT_SYMBOL(writeback_inodes_sb);
1226 1264
@@ -1231,11 +1269,11 @@ EXPORT_SYMBOL(writeback_inodes_sb);
1231 * Invoke writeback_inodes_sb if no writeback is currently underway. 1269 * Invoke writeback_inodes_sb if no writeback is currently underway.
1232 * Returns 1 if writeback was started, 0 if not. 1270 * Returns 1 if writeback was started, 0 if not.
1233 */ 1271 */
1234int writeback_inodes_sb_if_idle(struct super_block *sb) 1272int writeback_inodes_sb_if_idle(struct super_block *sb, enum wb_reason reason)
1235{ 1273{
1236 if (!writeback_in_progress(sb->s_bdi)) { 1274 if (!writeback_in_progress(sb->s_bdi)) {
1237 down_read(&sb->s_umount); 1275 down_read(&sb->s_umount);
1238 writeback_inodes_sb(sb); 1276 writeback_inodes_sb(sb, reason);
1239 up_read(&sb->s_umount); 1277 up_read(&sb->s_umount);
1240 return 1; 1278 return 1;
1241 } else 1279 } else
@@ -1252,11 +1290,12 @@ EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
1252 * Returns 1 if writeback was started, 0 if not. 1290 * Returns 1 if writeback was started, 0 if not.
1253 */ 1291 */
1254int writeback_inodes_sb_nr_if_idle(struct super_block *sb, 1292int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
1255 unsigned long nr) 1293 unsigned long nr,
1294 enum wb_reason reason)
1256{ 1295{
1257 if (!writeback_in_progress(sb->s_bdi)) { 1296 if (!writeback_in_progress(sb->s_bdi)) {
1258 down_read(&sb->s_umount); 1297 down_read(&sb->s_umount);
1259 writeback_inodes_sb_nr(sb, nr); 1298 writeback_inodes_sb_nr(sb, nr, reason);
1260 up_read(&sb->s_umount); 1299 up_read(&sb->s_umount);
1261 return 1; 1300 return 1;
1262 } else 1301 } else
@@ -1280,6 +1319,7 @@ void sync_inodes_sb(struct super_block *sb)
1280 .nr_pages = LONG_MAX, 1319 .nr_pages = LONG_MAX,
1281 .range_cyclic = 0, 1320 .range_cyclic = 0,
1282 .done = &done, 1321 .done = &done,
1322 .reason = WB_REASON_SYNC,
1283 }; 1323 };
1284 1324
1285 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1325 WARN_ON(!rwsem_is_locked(&sb->s_umount));
@@ -1303,6 +1343,7 @@ EXPORT_SYMBOL(sync_inodes_sb);
1303 */ 1343 */
1304int write_inode_now(struct inode *inode, int sync) 1344int write_inode_now(struct inode *inode, int sync)
1305{ 1345{
1346 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1306 int ret; 1347 int ret;
1307 struct writeback_control wbc = { 1348 struct writeback_control wbc = {
1308 .nr_to_write = LONG_MAX, 1349 .nr_to_write = LONG_MAX,
@@ -1315,11 +1356,11 @@ int write_inode_now(struct inode *inode, int sync)
1315 wbc.nr_to_write = 0; 1356 wbc.nr_to_write = 0;
1316 1357
1317 might_sleep(); 1358 might_sleep();
1318 spin_lock(&inode_wb_list_lock); 1359 spin_lock(&wb->list_lock);
1319 spin_lock(&inode->i_lock); 1360 spin_lock(&inode->i_lock);
1320 ret = writeback_single_inode(inode, &wbc); 1361 ret = writeback_single_inode(inode, wb, &wbc);
1321 spin_unlock(&inode->i_lock); 1362 spin_unlock(&inode->i_lock);
1322 spin_unlock(&inode_wb_list_lock); 1363 spin_unlock(&wb->list_lock);
1323 if (sync) 1364 if (sync)
1324 inode_sync_wait(inode); 1365 inode_sync_wait(inode);
1325 return ret; 1366 return ret;
@@ -1339,13 +1380,14 @@ EXPORT_SYMBOL(write_inode_now);
1339 */ 1380 */
1340int sync_inode(struct inode *inode, struct writeback_control *wbc) 1381int sync_inode(struct inode *inode, struct writeback_control *wbc)
1341{ 1382{
1383 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1342 int ret; 1384 int ret;
1343 1385
1344 spin_lock(&inode_wb_list_lock); 1386 spin_lock(&wb->list_lock);
1345 spin_lock(&inode->i_lock); 1387 spin_lock(&inode->i_lock);
1346 ret = writeback_single_inode(inode, wbc); 1388 ret = writeback_single_inode(inode, wb, wbc);
1347 spin_unlock(&inode->i_lock); 1389 spin_unlock(&inode->i_lock);
1348 spin_unlock(&inode_wb_list_lock); 1390 spin_unlock(&wb->list_lock);
1349 return ret; 1391 return ret;
1350} 1392}
1351EXPORT_SYMBOL(sync_inode); 1393EXPORT_SYMBOL(sync_inode);
diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
index 2aaf3eaaf13..c858b5c8320 100644
--- a/fs/fuse/dev.c
+++ b/fs/fuse/dev.c
@@ -19,6 +19,7 @@
19#include <linux/pipe_fs_i.h> 19#include <linux/pipe_fs_i.h>
20#include <linux/swap.h> 20#include <linux/swap.h>
21#include <linux/splice.h> 21#include <linux/splice.h>
22#include <linux/freezer.h>
22 23
23MODULE_ALIAS_MISCDEV(FUSE_MINOR); 24MODULE_ALIAS_MISCDEV(FUSE_MINOR);
24MODULE_ALIAS("devname:fuse"); 25MODULE_ALIAS("devname:fuse");
@@ -387,7 +388,10 @@ __acquires(fc->lock)
387 * Wait it out. 388 * Wait it out.
388 */ 389 */
389 spin_unlock(&fc->lock); 390 spin_unlock(&fc->lock);
390 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED); 391
392 while (req->state != FUSE_REQ_FINISHED)
393 wait_event_freezable(req->waitq,
394 req->state == FUSE_REQ_FINISHED);
391 spin_lock(&fc->lock); 395 spin_lock(&fc->lock);
392 396
393 if (!req->aborted) 397 if (!req->aborted)
diff --git a/fs/inode.c b/fs/inode.c
index 43566d17d1b..09f334bf27d 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -37,7 +37,7 @@
37 * inode_lru, inode->i_lru 37 * inode_lru, inode->i_lru
38 * inode_sb_list_lock protects: 38 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list 39 * sb->s_inodes, inode->i_sb_list
40 * inode_wb_list_lock protects: 40 * bdi->wb.list_lock protects:
41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list 41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
42 * inode_hash_lock protects: 42 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash 43 * inode_hashtable, inode->i_hash
@@ -48,7 +48,7 @@
48 * inode->i_lock 48 * inode->i_lock
49 * inode_lru_lock 49 * inode_lru_lock
50 * 50 *
51 * inode_wb_list_lock 51 * bdi->wb.list_lock
52 * inode->i_lock 52 * inode->i_lock
53 * 53 *
54 * inode_hash_lock 54 * inode_hash_lock
@@ -68,7 +68,6 @@ static LIST_HEAD(inode_lru);
68static DEFINE_SPINLOCK(inode_lru_lock); 68static DEFINE_SPINLOCK(inode_lru_lock);
69 69
70__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock); 70__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
71__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
72 71
73/* 72/*
74 * iprune_sem provides exclusion between the icache shrinking and the 73 * iprune_sem provides exclusion between the icache shrinking and the
diff --git a/fs/internal.h b/fs/internal.h
index b29c46e4e32..c905f59dbbe 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -97,6 +97,7 @@ extern struct file *get_empty_filp(void);
97 * super.c 97 * super.c
98 */ 98 */
99extern int do_remount_sb(struct super_block *, int, void *, int); 99extern int do_remount_sb(struct super_block *, int, void *, int);
100extern bool grab_super_passive(struct super_block *sb);
100extern void __put_super(struct super_block *sb); 101extern void __put_super(struct super_block *sb);
101extern void put_super(struct super_block *sb); 102extern void put_super(struct super_block *sb);
102extern struct dentry *mount_fs(struct file_system_type *, 103extern struct dentry *mount_fs(struct file_system_type *,
@@ -135,3 +136,5 @@ extern void inode_wb_list_del(struct inode *inode);
135extern int get_nr_dirty_inodes(void); 136extern int get_nr_dirty_inodes(void);
136extern void evict_inodes(struct super_block *); 137extern void evict_inodes(struct super_block *);
137extern int invalidate_inodes(struct super_block *, bool); 138extern int invalidate_inodes(struct super_block *, bool);
139
140extern struct dentry *__d_alloc(struct super_block *, const struct qstr *);
diff --git a/fs/ioprio.c b/fs/ioprio.c
index 7da2a06508e..95a6c2b04e0 100644
--- a/fs/ioprio.c
+++ b/fs/ioprio.c
@@ -30,7 +30,7 @@
30 30
31int set_task_ioprio(struct task_struct *task, int ioprio) 31int set_task_ioprio(struct task_struct *task, int ioprio)
32{ 32{
33 int err; 33 int err, i;
34 struct io_context *ioc; 34 struct io_context *ioc;
35 const struct cred *cred = current_cred(), *tcred; 35 const struct cred *cred = current_cred(), *tcred;
36 36
@@ -60,12 +60,17 @@ int set_task_ioprio(struct task_struct *task, int ioprio)
60 err = -ENOMEM; 60 err = -ENOMEM;
61 break; 61 break;
62 } 62 }
63 /* let other ioc users see the new values */
64 smp_wmb();
63 task->io_context = ioc; 65 task->io_context = ioc;
64 } while (1); 66 } while (1);
65 67
66 if (!err) { 68 if (!err) {
67 ioc->ioprio = ioprio; 69 ioc->ioprio = ioprio;
68 ioc->ioprio_changed = 1; 70 /* make sure schedulers see the new ioprio value */
71 wmb();
72 for (i = 0; i < IOC_IOPRIO_CHANGED_BITS; i++)
73 set_bit(i, ioc->ioprio_changed);
69 } 74 }
70 75
71 task_unlock(task); 76 task_unlock(task);
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index 58bb9994b94..d5ef7e933b7 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -1571,8 +1571,7 @@ int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1571 int status; 1571 int status;
1572 bool sync = true; 1572 bool sync = true;
1573 1573
1574 if (wbc->sync_mode == WB_SYNC_NONE || wbc->nonblocking || 1574 if (wbc->sync_mode == WB_SYNC_NONE || wbc->for_background)
1575 wbc->for_background)
1576 sync = false; 1575 sync = false;
1577 1576
1578 status = pnfs_layoutcommit_inode(inode, sync); 1577 status = pnfs_layoutcommit_inode(inode, sync);
diff --git a/fs/partitions/check.c b/fs/partitions/check.c
index 96d13c81eae..811960a5ef6 100644
--- a/fs/partitions/check.c
+++ b/fs/partitions/check.c
@@ -366,10 +366,21 @@ static void part_release(struct device *dev)
366 kfree(p); 366 kfree(p);
367} 367}
368 368
369static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
370{
371 struct hd_struct *part = dev_to_part(dev);
372
373 add_uevent_var(env, "PARTN=%u", part->partno);
374 if (part->info && part->info->volname[0])
375 add_uevent_var(env, "PARTNAME=%s", part->info->volname);
376 return 0;
377}
378
369struct device_type part_type = { 379struct device_type part_type = {
370 .name = "partition", 380 .name = "partition",
371 .groups = part_attr_groups, 381 .groups = part_attr_groups,
372 .release = part_release, 382 .release = part_release,
383 .uevent = part_uevent,
373}; 384};
374 385
375static void delete_partition_rcu_cb(struct rcu_head *head) 386static void delete_partition_rcu_cb(struct rcu_head *head)
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 22794e8ab99..600faf5ba2f 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -133,6 +133,12 @@ struct pid_entry {
133 NULL, &proc_single_file_operations, \ 133 NULL, &proc_single_file_operations, \
134 { .proc_show = show } ) 134 { .proc_show = show } )
135 135
136/* ANDROID is for special files in /proc. */
137#define ANDROID(NAME, MODE, OTYPE) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 &proc_##OTYPE##_inode_operations, \
140 &proc_##OTYPE##_operations, {})
141
136/* 142/*
137 * Count the number of hardlinks for the pid_entry table, excluding the . 143 * Count the number of hardlinks for the pid_entry table, excluding the .
138 * and .. links. 144 * and .. links.
@@ -205,7 +211,8 @@ static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
205 211
206 mm = get_task_mm(task); 212 mm = get_task_mm(task);
207 if (mm && mm != current->mm && 213 if (mm && mm != current->mm &&
208 !ptrace_may_access(task, mode)) { 214 !ptrace_may_access(task, PTRACE_MODE_READ) &&
215 !capable(CAP_SYS_RESOURCE)) {
209 mmput(mm); 216 mmput(mm);
210 mm = ERR_PTR(-EACCES); 217 mm = ERR_PTR(-EACCES);
211 } 218 }
@@ -789,13 +796,13 @@ static int mem_open(struct inode* inode, struct file* file)
789 return 0; 796 return 0;
790} 797}
791 798
792static ssize_t mem_rw(struct file *file, char __user *buf, 799static ssize_t mem_read(struct file * file, char __user * buf,
793 size_t count, loff_t *ppos, int write) 800 size_t count, loff_t *ppos)
794{ 801{
795 struct mm_struct *mm = file->private_data; 802 int ret;
796 unsigned long addr = *ppos;
797 ssize_t copied;
798 char *page; 803 char *page;
804 unsigned long src = *ppos;
805 struct mm_struct *mm = file->private_data;
799 806
800 if (!mm) 807 if (!mm)
801 return 0; 808 return 0;
@@ -804,54 +811,80 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
804 if (!page) 811 if (!page)
805 return -ENOMEM; 812 return -ENOMEM;
806 813
807 copied = 0; 814 ret = 0;
808 if (!atomic_inc_not_zero(&mm->mm_users)) 815
809 goto free;
810
811 while (count > 0) { 816 while (count > 0) {
812 int this_len = min_t(int, count, PAGE_SIZE); 817 int this_len, retval;
813
814 if (write && copy_from_user(page, buf, this_len)) {
815 copied = -EFAULT;
816 break;
817 }
818 818
819 this_len = access_remote_vm(mm, addr, page, this_len, write); 819 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
820 if (!this_len) { 820 retval = access_remote_vm(mm, src, page, this_len, 0);
821 if (!copied) 821 if (!retval) {
822 copied = -EIO; 822 if (!ret)
823 ret = -EIO;
823 break; 824 break;
824 } 825 }
825 826
826 if (!write && copy_to_user(buf, page, this_len)) { 827 if (copy_to_user(buf, page, retval)) {
827 copied = -EFAULT; 828 ret = -EFAULT;
828 break; 829 break;
829 } 830 }
830 831
831 buf += this_len; 832 ret += retval;
832 addr += this_len; 833 src += retval;
833 copied += this_len; 834 buf += retval;
834 count -= this_len; 835 count -= retval;
835 } 836 }
836 *ppos = addr; 837 *ppos = src;
837 838
838 mmput(mm);
839free:
840 free_page((unsigned long) page); 839 free_page((unsigned long) page);
841 return copied; 840 return ret;
842} 841}
843 842
844static ssize_t mem_read(struct file *file, char __user *buf, 843#define mem_write NULL
845 size_t count, loff_t *ppos)
846{
847 return mem_rw(file, buf, count, ppos, 0);
848}
849 844
850static ssize_t mem_write(struct file *file, const char __user *buf, 845#ifndef mem_write
846/* This is a security hazard */
847static ssize_t mem_write(struct file * file, const char __user *buf,
851 size_t count, loff_t *ppos) 848 size_t count, loff_t *ppos)
852{ 849{
853 return mem_rw(file, (char __user*)buf, count, ppos, 1); 850 int copied;
851 char *page;
852 unsigned long dst = *ppos;
853 struct mm_struct *mm = file->private_data;
854
855 if (!mm)
856 return 0;
857
858 page = (char *)__get_free_page(GFP_TEMPORARY);
859 if (!page)
860 return -ENOMEM;
861
862 copied = 0;
863 while (count > 0) {
864 int this_len, retval;
865
866 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
867 if (copy_from_user(page, buf, this_len)) {
868 copied = -EFAULT;
869 break;
870 }
871 retval = access_remote_vm(mm, dst, page, this_len, 1);
872 if (!retval) {
873 if (!copied)
874 copied = -EIO;
875 break;
876 }
877 copied += retval;
878 buf += retval;
879 dst += retval;
880 count -= retval;
881 }
882 *ppos = dst;
883
884 free_page((unsigned long) page);
885 return copied;
854} 886}
887#endif
855 888
856loff_t mem_lseek(struct file *file, loff_t offset, int orig) 889loff_t mem_lseek(struct file *file, loff_t offset, int orig)
857{ 890{
@@ -1060,6 +1093,39 @@ out:
1060 return err < 0 ? err : count; 1093 return err < 0 ? err : count;
1061} 1094}
1062 1095
1096static int oom_adjust_permission(struct inode *inode, int mask,
1097 unsigned int flags)
1098{
1099 uid_t uid;
1100 struct task_struct *p;
1101
1102 if (flags & IPERM_FLAG_RCU)
1103 return -ECHILD;
1104
1105 p = get_proc_task(inode);
1106 if(p) {
1107 uid = task_uid(p);
1108 put_task_struct(p);
1109 }
1110
1111 /*
1112 * System Server (uid == 1000) is granted access to oom_adj of all
1113 * android applications (uid > 10000) as and services (uid >= 1000)
1114 */
1115 if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
1116 if (inode->i_mode >> 6 & mask) {
1117 return 0;
1118 }
1119 }
1120
1121 /* Fall back to default. */
1122 return generic_permission(inode, mask, flags, NULL);
1123}
1124
1125static const struct inode_operations proc_oom_adjust_inode_operations = {
1126 .permission = oom_adjust_permission,
1127};
1128
1063static const struct file_operations proc_oom_adjust_operations = { 1129static const struct file_operations proc_oom_adjust_operations = {
1064 .read = oom_adjust_read, 1130 .read = oom_adjust_read,
1065 .write = oom_adjust_write, 1131 .write = oom_adjust_write,
@@ -2766,7 +2832,7 @@ static const struct pid_entry tgid_base_stuff[] = {
2766 REG("cgroup", S_IRUGO, proc_cgroup_operations), 2832 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2767#endif 2833#endif
2768 INF("oom_score", S_IRUGO, proc_oom_score), 2834 INF("oom_score", S_IRUGO, proc_oom_score),
2769 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 2835 ANDROID("oom_adj",S_IRUGO|S_IWUSR, oom_adjust),
2770 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 2836 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2771#ifdef CONFIG_AUDITSYSCALL 2837#ifdef CONFIG_AUDITSYSCALL
2772 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2838 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
diff --git a/fs/quota/quota.c b/fs/quota/quota.c
index 10b6be3ca28..4bae57fc603 100644
--- a/fs/quota/quota.c
+++ b/fs/quota/quota.c
@@ -286,7 +286,7 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
286 /* caller already holds s_umount */ 286 /* caller already holds s_umount */
287 if (sb->s_flags & MS_RDONLY) 287 if (sb->s_flags & MS_RDONLY)
288 return -EROFS; 288 return -EROFS;
289 writeback_inodes_sb(sb); 289 writeback_inodes_sb(sb, WB_REASON_SYNC);
290 return 0; 290 return 0;
291 default: 291 default:
292 return -EINVAL; 292 return -EINVAL;
diff --git a/fs/splice.c b/fs/splice.c
index 9d890085f9b..6d0dfb89c75 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -133,7 +133,7 @@ error:
133 return err; 133 return err;
134} 134}
135 135
136static const struct pipe_buf_operations page_cache_pipe_buf_ops = { 136const struct pipe_buf_operations page_cache_pipe_buf_ops = {
137 .can_merge = 0, 137 .can_merge = 0,
138 .map = generic_pipe_buf_map, 138 .map = generic_pipe_buf_map,
139 .unmap = generic_pipe_buf_unmap, 139 .unmap = generic_pipe_buf_unmap,
@@ -265,7 +265,7 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
265 return ret; 265 return ret;
266} 266}
267 267
268static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) 268void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
269{ 269{
270 page_cache_release(spd->pages[i]); 270 page_cache_release(spd->pages[i]);
271} 271}
diff --git a/fs/super.c b/fs/super.c
index caf4dfa28ee..e2cf58cad6a 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -241,17 +241,48 @@ static int grab_super(struct super_block *s) __releases(sb_lock)
241} 241}
242 242
243/* 243/*
244 * grab_super_passive - acquire a passive reference
245 * @s: reference we are trying to grab
246 *
247 * Tries to acquire a passive reference. This is used in places where we
248 * cannot take an active reference but we need to ensure that the
249 * superblock does not go away while we are working on it. It returns
250 * false if a reference was not gained, and returns true with the s_umount
251 * lock held in read mode if a reference is gained. On successful return,
252 * the caller must drop the s_umount lock and the passive reference when
253 * done.
254 */
255bool grab_super_passive(struct super_block *sb)
256{
257 spin_lock(&sb_lock);
258 if (list_empty(&sb->s_instances)) {
259 spin_unlock(&sb_lock);
260 return false;
261 }
262
263 sb->s_count++;
264 spin_unlock(&sb_lock);
265
266 if (down_read_trylock(&sb->s_umount)) {
267 if (sb->s_root)
268 return true;
269 up_read(&sb->s_umount);
270 }
271
272 put_super(sb);
273 return false;
274}
275
276/*
244 * Superblock locking. We really ought to get rid of these two. 277 * Superblock locking. We really ought to get rid of these two.
245 */ 278 */
246void lock_super(struct super_block * sb) 279void lock_super(struct super_block * sb)
247{ 280{
248 get_fs_excl();
249 mutex_lock(&sb->s_lock); 281 mutex_lock(&sb->s_lock);
250} 282}
251 283
252void unlock_super(struct super_block * sb) 284void unlock_super(struct super_block * sb)
253{ 285{
254 put_fs_excl();
255 mutex_unlock(&sb->s_lock); 286 mutex_unlock(&sb->s_lock);
256} 287}
257 288
@@ -822,7 +853,7 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,
822 } else { 853 } else {
823 char b[BDEVNAME_SIZE]; 854 char b[BDEVNAME_SIZE];
824 855
825 s->s_flags = flags | MS_NOSEC; 856 s->s_flags = flags;
826 s->s_mode = mode; 857 s->s_mode = mode;
827 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 858 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
828 sb_set_blocksize(s, block_size(bdev)); 859 sb_set_blocksize(s, block_size(bdev));
diff --git a/fs/sync.c b/fs/sync.c
index c38ec163da6..2385e128a35 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -18,6 +18,9 @@
18#include <linux/backing-dev.h> 18#include <linux/backing-dev.h>
19#include "internal.h" 19#include "internal.h"
20 20
21bool fsync_enabled = true;
22module_param(fsync_enabled, bool, 0755);
23
21#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \ 24#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
22 SYNC_FILE_RANGE_WAIT_AFTER) 25 SYNC_FILE_RANGE_WAIT_AFTER)
23 26
@@ -43,7 +46,7 @@ static int __sync_filesystem(struct super_block *sb, int wait)
43 if (wait) 46 if (wait)
44 sync_inodes_sb(sb); 47 sync_inodes_sb(sb);
45 else 48 else
46 writeback_inodes_sb(sb); 49 writeback_inodes_sb(sb, WB_REASON_SYNC);
47 50
48 if (sb->s_op->sync_fs) 51 if (sb->s_op->sync_fs)
49 sb->s_op->sync_fs(sb, wait); 52 sb->s_op->sync_fs(sb, wait);
@@ -98,7 +101,7 @@ static void sync_filesystems(int wait)
98 */ 101 */
99SYSCALL_DEFINE0(sync) 102SYSCALL_DEFINE0(sync)
100{ 103{
101 wakeup_flusher_threads(0); 104 wakeup_flusher_threads(0, WB_REASON_SYNC);
102 sync_filesystems(0); 105 sync_filesystems(0);
103 sync_filesystems(1); 106 sync_filesystems(1);
104 if (unlikely(laptop_mode)) 107 if (unlikely(laptop_mode))
@@ -139,6 +142,9 @@ SYSCALL_DEFINE1(syncfs, int, fd)
139 int ret; 142 int ret;
140 int fput_needed; 143 int fput_needed;
141 144
145 if (!fsync_enabled)
146 return 0;
147
142 file = fget_light(fd, &fput_needed); 148 file = fget_light(fd, &fput_needed);
143 if (!file) 149 if (!file)
144 return -EBADF; 150 return -EBADF;
@@ -168,6 +174,9 @@ int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
168 struct address_space *mapping = file->f_mapping; 174 struct address_space *mapping = file->f_mapping;
169 int err, ret; 175 int err, ret;
170 176
177 if (!fsync_enabled)
178 return 0;
179
171 if (!file->f_op || !file->f_op->fsync) { 180 if (!file->f_op || !file->f_op->fsync) {
172 ret = -EINVAL; 181 ret = -EINVAL;
173 goto out; 182 goto out;
@@ -200,6 +209,9 @@ EXPORT_SYMBOL(vfs_fsync_range);
200 */ 209 */
201int vfs_fsync(struct file *file, int datasync) 210int vfs_fsync(struct file *file, int datasync)
202{ 211{
212 if (!fsync_enabled)
213 return 0;
214
203 return vfs_fsync_range(file, 0, LLONG_MAX, datasync); 215 return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
204} 216}
205EXPORT_SYMBOL(vfs_fsync); 217EXPORT_SYMBOL(vfs_fsync);
@@ -209,6 +221,9 @@ static int do_fsync(unsigned int fd, int datasync)
209 struct file *file; 221 struct file *file;
210 int ret = -EBADF; 222 int ret = -EBADF;
211 223
224 if (!fsync_enabled)
225 return 0;
226
212 file = fget(fd); 227 file = fget(fd);
213 if (file) { 228 if (file) {
214 ret = vfs_fsync(file, datasync); 229 ret = vfs_fsync(file, datasync);
@@ -219,11 +234,17 @@ static int do_fsync(unsigned int fd, int datasync)
219 234
220SYSCALL_DEFINE1(fsync, unsigned int, fd) 235SYSCALL_DEFINE1(fsync, unsigned int, fd)
221{ 236{
237 if (!fsync_enabled)
238 return 0;
239
222 return do_fsync(fd, 0); 240 return do_fsync(fd, 0);
223} 241}
224 242
225SYSCALL_DEFINE1(fdatasync, unsigned int, fd) 243SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
226{ 244{
245 if (!fsync_enabled)
246 return 0;
247
227 return do_fsync(fd, 1); 248 return do_fsync(fd, 1);
228} 249}
229 250
@@ -237,6 +258,9 @@ SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
237 */ 258 */
238int generic_write_sync(struct file *file, loff_t pos, loff_t count) 259int generic_write_sync(struct file *file, loff_t pos, loff_t count)
239{ 260{
261 if (!fsync_enabled)
262 return 0;
263
240 if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host)) 264 if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
241 return 0; 265 return 0;
242 return vfs_fsync_range(file, pos, pos + count - 1, 266 return vfs_fsync_range(file, pos, pos + count - 1,
@@ -301,6 +325,9 @@ SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
301 int fput_needed; 325 int fput_needed;
302 umode_t i_mode; 326 umode_t i_mode;
303 327
328 if (!fsync_enabled)
329 return 0;
330
304 ret = -EINVAL; 331 ret = -EINVAL;
305 if (flags & ~VALID_FLAGS) 332 if (flags & ~VALID_FLAGS)
306 goto out; 333 goto out;
diff --git a/fs/ubifs/budget.c b/fs/ubifs/budget.c
index 315de66e52b..bc4f94b2870 100644
--- a/fs/ubifs/budget.c
+++ b/fs/ubifs/budget.c
@@ -63,7 +63,7 @@
63static void shrink_liability(struct ubifs_info *c, int nr_to_write) 63static void shrink_liability(struct ubifs_info *c, int nr_to_write)
64{ 64{
65 down_read(&c->vfs_sb->s_umount); 65 down_read(&c->vfs_sb->s_umount);
66 writeback_inodes_sb(c->vfs_sb); 66 writeback_inodes_sb(c->vfs_sb, WB_REASON_FS_FREE_SPACE);
67 up_read(&c->vfs_sb->s_umount); 67 up_read(&c->vfs_sb->s_umount);
68} 68}
69 69
diff --git a/fs/yaffs2/Kconfig b/fs/yaffs2/Kconfig
new file mode 100644
index 00000000000..63541405999
--- /dev/null
+++ b/fs/yaffs2/Kconfig
@@ -0,0 +1,161 @@
1#
2# YAFFS file system configurations
3#
4
5config YAFFS_FS
6 tristate "YAFFS2 file system support"
7 default n
8 depends on MTD_BLOCK
9 select YAFFS_YAFFS1
10 select YAFFS_YAFFS2
11 help
12 YAFFS2, or Yet Another Flash Filing System, is a filing system
13 optimised for NAND Flash chips.
14
15 To compile the YAFFS2 file system support as a module, choose M
16 here: the module will be called yaffs2.
17
18 If unsure, say N.
19
20 Further information on YAFFS2 is available at
21 <http://www.aleph1.co.uk/yaffs/>.
22
23config YAFFS_YAFFS1
24 bool "512 byte / page devices"
25 depends on YAFFS_FS
26 default y
27 help
28 Enable YAFFS1 support -- yaffs for 512 byte / page devices
29
30 Not needed for 2K-page devices.
31
32 If unsure, say Y.
33
34config YAFFS_9BYTE_TAGS
35 bool "Use older-style on-NAND data format with pageStatus byte"
36 depends on YAFFS_YAFFS1
37 default n
38 help
39
40 Older-style on-NAND data format has a "pageStatus" byte to record
41 chunk/page state. This byte is zero when the page is discarded.
42 Choose this option if you have existing on-NAND data using this
43 format that you need to continue to support. New data written
44 also uses the older-style format. Note: Use of this option
45 generally requires that MTD's oob layout be adjusted to use the
46 older-style format. See notes on tags formats and MTD versions
47 in yaffs_mtdif1.c.
48
49 If unsure, say N.
50
51config YAFFS_DOES_ECC
52 bool "Lets Yaffs do its own ECC"
53 depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
54 default n
55 help
56 This enables Yaffs to use its own ECC functions instead of using
57 the ones from the generic MTD-NAND driver.
58
59 If unsure, say N.
60
61config YAFFS_ECC_WRONG_ORDER
62 bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
63 depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
64 default n
65 help
66 This makes yaffs_ecc.c use the same ecc byte order as Steven
67 Hill's nand_ecc.c. If not set, then you get the same ecc byte
68 order as SmartMedia.
69
70 If unsure, say N.
71
72config YAFFS_YAFFS2
73 bool "2048 byte (or larger) / page devices"
74 depends on YAFFS_FS
75 default y
76 help
77 Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
78
79 If unsure, say Y.
80
81config YAFFS_AUTO_YAFFS2
82 bool "Autoselect yaffs2 format"
83 depends on YAFFS_YAFFS2
84 default y
85 help
86 Without this, you need to explicitely use yaffs2 as the file
87 system type. With this, you can say "yaffs" and yaffs or yaffs2
88 will be used depending on the device page size (yaffs on
89 512-byte page devices, yaffs2 on 2K page devices).
90
91 If unsure, say Y.
92
93config YAFFS_DISABLE_TAGS_ECC
94 bool "Disable YAFFS from doing ECC on tags by default"
95 depends on YAFFS_FS && YAFFS_YAFFS2
96 default n
97 help
98 This defaults Yaffs to using its own ECC calculations on tags instead of
99 just relying on the MTD.
100 This behavior can also be overridden with tags_ecc_on and
101 tags_ecc_off mount options.
102
103 If unsure, say N.
104
105config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
106 bool "Force chunk erase check"
107 depends on YAFFS_FS
108 default n
109 help
110 Normally YAFFS only checks chunks before writing until an erased
111 chunk is found. This helps to detect any partially written
112 chunks that might have happened due to power loss.
113
114 Enabling this forces on the test that chunks are erased in flash
115 before writing to them. This takes more time but is potentially
116 a bit more secure.
117
118 Suggest setting Y during development and ironing out driver
119 issues etc. Suggest setting to N if you want faster writing.
120
121 If unsure, say Y.
122
123config YAFFS_EMPTY_LOST_AND_FOUND
124 bool "Empty lost and found on boot"
125 depends on YAFFS_FS
126 default n
127 help
128 If this is enabled then the contents of lost and found is
129 automatically dumped at mount.
130
131 If unsure, say N.
132
133config YAFFS_DISABLE_BLOCK_REFRESHING
134 bool "Disable yaffs2 block refreshing"
135 depends on YAFFS_FS
136 default n
137 help
138 If this is set, then block refreshing is disabled.
139 Block refreshing infrequently refreshes the oldest block in
140 a yaffs2 file system. This mechanism helps to refresh flash to
141 mitigate against data loss. This is particularly useful for MLC.
142
143 If unsure, say N.
144
145config YAFFS_DISABLE_BACKGROUND
146 bool "Disable yaffs2 background processing"
147 depends on YAFFS_FS
148 default n
149 help
150 If this is set, then background processing is disabled.
151 Background processing makes many foreground activities faster.
152
153 If unsure, say N.
154
155config YAFFS_XATTR
156 bool "Enable yaffs2 xattr support"
157 depends on YAFFS_FS
158 default y
159 help
160 If this is set then yaffs2 will provide xattr support.
161 If unsure, say Y.
diff --git a/fs/yaffs2/Makefile b/fs/yaffs2/Makefile
new file mode 100644
index 00000000000..e63a28aa3ed
--- /dev/null
+++ b/fs/yaffs2/Makefile
@@ -0,0 +1,17 @@
1#
2# Makefile for the linux YAFFS filesystem routines.
3#
4
5obj-$(CONFIG_YAFFS_FS) += yaffs.o
6
7yaffs-y := yaffs_ecc.o yaffs_vfs.o yaffs_guts.o yaffs_checkptrw.o
8yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o
9yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
10yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
11yaffs-y += yaffs_nameval.o yaffs_attribs.o
12yaffs-y += yaffs_allocator.o
13yaffs-y += yaffs_yaffs1.o
14yaffs-y += yaffs_yaffs2.o
15yaffs-y += yaffs_bitmap.o
16yaffs-y += yaffs_verify.o
17
diff --git a/fs/yaffs2/yaffs_allocator.c b/fs/yaffs2/yaffs_allocator.c
new file mode 100644
index 00000000000..f9cd5becd8f
--- /dev/null
+++ b/fs/yaffs2/yaffs_allocator.c
@@ -0,0 +1,396 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_allocator.h"
15#include "yaffs_guts.h"
16#include "yaffs_trace.h"
17#include "yportenv.h"
18
19#ifdef CONFIG_YAFFS_KMALLOC_ALLOCATOR
20
21void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
22{
23 dev = dev;
24}
25
26void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
27{
28 dev = dev;
29}
30
31struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
32{
33 return (struct yaffs_tnode *)kmalloc(dev->tnode_size, GFP_NOFS);
34}
35
36void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
37{
38 dev = dev;
39 kfree(tn);
40}
41
42void yaffs_init_raw_objs(struct yaffs_dev *dev)
43{
44 dev = dev;
45}
46
47void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
48{
49 dev = dev;
50}
51
52struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
53{
54 dev = dev;
55 return (struct yaffs_obj *)kmalloc(sizeof(struct yaffs_obj));
56}
57
58void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
59{
60
61 dev = dev;
62 kfree(obj);
63}
64
65#else
66
67struct yaffs_tnode_list {
68 struct yaffs_tnode_list *next;
69 struct yaffs_tnode *tnodes;
70};
71
72struct yaffs_obj_list {
73 struct yaffs_obj_list *next;
74 struct yaffs_obj *objects;
75};
76
77struct yaffs_allocator {
78 int n_tnodes_created;
79 struct yaffs_tnode *free_tnodes;
80 int n_free_tnodes;
81 struct yaffs_tnode_list *alloc_tnode_list;
82
83 int n_obj_created;
84 struct yaffs_obj *free_objs;
85 int n_free_objects;
86
87 struct yaffs_obj_list *allocated_obj_list;
88};
89
90static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
91{
92
93 struct yaffs_allocator *allocator =
94 (struct yaffs_allocator *)dev->allocator;
95
96 struct yaffs_tnode_list *tmp;
97
98 if (!allocator) {
99 YBUG();
100 return;
101 }
102
103 while (allocator->alloc_tnode_list) {
104 tmp = allocator->alloc_tnode_list->next;
105
106 kfree(allocator->alloc_tnode_list->tnodes);
107 kfree(allocator->alloc_tnode_list);
108 allocator->alloc_tnode_list = tmp;
109
110 }
111
112 allocator->free_tnodes = NULL;
113 allocator->n_free_tnodes = 0;
114 allocator->n_tnodes_created = 0;
115}
116
117static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
118{
119 struct yaffs_allocator *allocator = dev->allocator;
120
121 if (allocator) {
122 allocator->alloc_tnode_list = NULL;
123 allocator->free_tnodes = NULL;
124 allocator->n_free_tnodes = 0;
125 allocator->n_tnodes_created = 0;
126 } else {
127 YBUG();
128 }
129}
130
131static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
132{
133 struct yaffs_allocator *allocator =
134 (struct yaffs_allocator *)dev->allocator;
135 int i;
136 struct yaffs_tnode *new_tnodes;
137 u8 *mem;
138 struct yaffs_tnode *curr;
139 struct yaffs_tnode *next;
140 struct yaffs_tnode_list *tnl;
141
142 if (!allocator) {
143 YBUG();
144 return YAFFS_FAIL;
145 }
146
147 if (n_tnodes < 1)
148 return YAFFS_OK;
149
150 /* make these things */
151
152 new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS);
153 mem = (u8 *) new_tnodes;
154
155 if (!new_tnodes) {
156 yaffs_trace(YAFFS_TRACE_ERROR,
157 "yaffs: Could not allocate Tnodes");
158 return YAFFS_FAIL;
159 }
160
161 /* New hookup for wide tnodes */
162 for (i = 0; i < n_tnodes - 1; i++) {
163 curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
164 next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
165 curr->internal[0] = next;
166 }
167
168 curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
169 curr->internal[0] = allocator->free_tnodes;
170 allocator->free_tnodes = (struct yaffs_tnode *)mem;
171
172 allocator->n_free_tnodes += n_tnodes;
173 allocator->n_tnodes_created += n_tnodes;
174
175 /* Now add this bunch of tnodes to a list for freeing up.
176 * NB If we can't add this to the management list it isn't fatal
177 * but it just means we can't free this bunch of tnodes later.
178 */
179
180 tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS);
181 if (!tnl) {
182 yaffs_trace(YAFFS_TRACE_ERROR,
183 "Could not add tnodes to management list");
184 return YAFFS_FAIL;
185 } else {
186 tnl->tnodes = new_tnodes;
187 tnl->next = allocator->alloc_tnode_list;
188 allocator->alloc_tnode_list = tnl;
189 }
190
191 yaffs_trace(YAFFS_TRACE_ALLOCATE,"Tnodes added");
192
193 return YAFFS_OK;
194}
195
196struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
197{
198 struct yaffs_allocator *allocator =
199 (struct yaffs_allocator *)dev->allocator;
200 struct yaffs_tnode *tn = NULL;
201
202 if (!allocator) {
203 YBUG();
204 return NULL;
205 }
206
207 /* If there are none left make more */
208 if (!allocator->free_tnodes)
209 yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
210
211 if (allocator->free_tnodes) {
212 tn = allocator->free_tnodes;
213 allocator->free_tnodes = allocator->free_tnodes->internal[0];
214 allocator->n_free_tnodes--;
215 }
216
217 return tn;
218}
219
220/* FreeTnode frees up a tnode and puts it back on the free list */
221void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
222{
223 struct yaffs_allocator *allocator = dev->allocator;
224
225 if (!allocator) {
226 YBUG();
227 return;
228 }
229
230 if (tn) {
231 tn->internal[0] = allocator->free_tnodes;
232 allocator->free_tnodes = tn;
233 allocator->n_free_tnodes++;
234 }
235 dev->checkpoint_blocks_required = 0; /* force recalculation */
236}
237
238static void yaffs_init_raw_objs(struct yaffs_dev *dev)
239{
240 struct yaffs_allocator *allocator = dev->allocator;
241
242 if (allocator) {
243 allocator->allocated_obj_list = NULL;
244 allocator->free_objs = NULL;
245 allocator->n_free_objects = 0;
246 } else {
247 YBUG();
248 }
249}
250
251static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
252{
253 struct yaffs_allocator *allocator = dev->allocator;
254 struct yaffs_obj_list *tmp;
255
256 if (!allocator) {
257 YBUG();
258 return;
259 }
260
261 while (allocator->allocated_obj_list) {
262 tmp = allocator->allocated_obj_list->next;
263 kfree(allocator->allocated_obj_list->objects);
264 kfree(allocator->allocated_obj_list);
265
266 allocator->allocated_obj_list = tmp;
267 }
268
269 allocator->free_objs = NULL;
270 allocator->n_free_objects = 0;
271 allocator->n_obj_created = 0;
272}
273
274static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
275{
276 struct yaffs_allocator *allocator = dev->allocator;
277
278 int i;
279 struct yaffs_obj *new_objs;
280 struct yaffs_obj_list *list;
281
282 if (!allocator) {
283 YBUG();
284 return YAFFS_FAIL;
285 }
286
287 if (n_obj < 1)
288 return YAFFS_OK;
289
290 /* make these things */
291 new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS);
292 list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS);
293
294 if (!new_objs || !list) {
295 if (new_objs) {
296 kfree(new_objs);
297 new_objs = NULL;
298 }
299 if (list) {
300 kfree(list);
301 list = NULL;
302 }
303 yaffs_trace(YAFFS_TRACE_ALLOCATE,
304 "Could not allocate more objects");
305 return YAFFS_FAIL;
306 }
307
308 /* Hook them into the free list */
309 for (i = 0; i < n_obj - 1; i++) {
310 new_objs[i].siblings.next =
311 (struct list_head *)(&new_objs[i + 1]);
312 }
313
314 new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs;
315 allocator->free_objs = new_objs;
316 allocator->n_free_objects += n_obj;
317 allocator->n_obj_created += n_obj;
318
319 /* Now add this bunch of Objects to a list for freeing up. */
320
321 list->objects = new_objs;
322 list->next = allocator->allocated_obj_list;
323 allocator->allocated_obj_list = list;
324
325 return YAFFS_OK;
326}
327
328struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
329{
330 struct yaffs_obj *obj = NULL;
331 struct yaffs_allocator *allocator = dev->allocator;
332
333 if (!allocator) {
334 YBUG();
335 return obj;
336 }
337
338 /* If there are none left make more */
339 if (!allocator->free_objs)
340 yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
341
342 if (allocator->free_objs) {
343 obj = allocator->free_objs;
344 allocator->free_objs =
345 (struct yaffs_obj *)(allocator->free_objs->siblings.next);
346 allocator->n_free_objects--;
347 }
348
349 return obj;
350}
351
352void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
353{
354
355 struct yaffs_allocator *allocator = dev->allocator;
356
357 if (!allocator)
358 YBUG();
359 else {
360 /* Link into the free list. */
361 obj->siblings.next = (struct list_head *)(allocator->free_objs);
362 allocator->free_objs = obj;
363 allocator->n_free_objects++;
364 }
365}
366
367void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
368{
369 if (dev->allocator) {
370 yaffs_deinit_raw_tnodes(dev);
371 yaffs_deinit_raw_objs(dev);
372
373 kfree(dev->allocator);
374 dev->allocator = NULL;
375 } else {
376 YBUG();
377 }
378}
379
380void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
381{
382 struct yaffs_allocator *allocator;
383
384 if (!dev->allocator) {
385 allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS);
386 if (allocator) {
387 dev->allocator = allocator;
388 yaffs_init_raw_tnodes(dev);
389 yaffs_init_raw_objs(dev);
390 }
391 } else {
392 YBUG();
393 }
394}
395
396#endif
diff --git a/fs/yaffs2/yaffs_allocator.h b/fs/yaffs2/yaffs_allocator.h
new file mode 100644
index 00000000000..4d5f2aec89f
--- /dev/null
+++ b/fs/yaffs2/yaffs_allocator.h
@@ -0,0 +1,30 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_ALLOCATOR_H__
17#define __YAFFS_ALLOCATOR_H__
18
19#include "yaffs_guts.h"
20
21void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev);
22void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev);
23
24struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev);
25void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn);
26
27struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev);
28void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj);
29
30#endif
diff --git a/fs/yaffs2/yaffs_attribs.c b/fs/yaffs2/yaffs_attribs.c
new file mode 100644
index 00000000000..9b47d376310
--- /dev/null
+++ b/fs/yaffs2/yaffs_attribs.c
@@ -0,0 +1,124 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_guts.h"
15#include "yaffs_attribs.h"
16
17void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
18{
19 obj->yst_uid = oh->yst_uid;
20 obj->yst_gid = oh->yst_gid;
21 obj->yst_atime = oh->yst_atime;
22 obj->yst_mtime = oh->yst_mtime;
23 obj->yst_ctime = oh->yst_ctime;
24 obj->yst_rdev = oh->yst_rdev;
25}
26
27void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
28{
29 oh->yst_uid = obj->yst_uid;
30 oh->yst_gid = obj->yst_gid;
31 oh->yst_atime = obj->yst_atime;
32 oh->yst_mtime = obj->yst_mtime;
33 oh->yst_ctime = obj->yst_ctime;
34 oh->yst_rdev = obj->yst_rdev;
35
36}
37
38void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
39{
40 obj->yst_mtime = Y_CURRENT_TIME;
41 if (do_a)
42 obj->yst_atime = obj->yst_mtime;
43 if (do_c)
44 obj->yst_ctime = obj->yst_mtime;
45}
46
47void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
48{
49 yaffs_load_current_time(obj, 1, 1);
50 obj->yst_rdev = rdev;
51 obj->yst_uid = uid;
52 obj->yst_gid = gid;
53}
54
55loff_t yaffs_get_file_size(struct yaffs_obj *obj)
56{
57 YCHAR *alias = NULL;
58 obj = yaffs_get_equivalent_obj(obj);
59
60 switch (obj->variant_type) {
61 case YAFFS_OBJECT_TYPE_FILE:
62 return obj->variant.file_variant.file_size;
63 case YAFFS_OBJECT_TYPE_SYMLINK:
64 alias = obj->variant.symlink_variant.alias;
65 if (!alias)
66 return 0;
67 return strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
68 default:
69 return 0;
70 }
71}
72
73int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
74{
75 unsigned int valid = attr->ia_valid;
76
77 if (valid & ATTR_MODE)
78 obj->yst_mode = attr->ia_mode;
79 if (valid & ATTR_UID)
80 obj->yst_uid = attr->ia_uid;
81 if (valid & ATTR_GID)
82 obj->yst_gid = attr->ia_gid;
83
84 if (valid & ATTR_ATIME)
85 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
86 if (valid & ATTR_CTIME)
87 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
88 if (valid & ATTR_MTIME)
89 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
90
91 if (valid & ATTR_SIZE)
92 yaffs_resize_file(obj, attr->ia_size);
93
94 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
95
96 return YAFFS_OK;
97
98}
99
100int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
101{
102 unsigned int valid = 0;
103
104 attr->ia_mode = obj->yst_mode;
105 valid |= ATTR_MODE;
106 attr->ia_uid = obj->yst_uid;
107 valid |= ATTR_UID;
108 attr->ia_gid = obj->yst_gid;
109 valid |= ATTR_GID;
110
111 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
112 valid |= ATTR_ATIME;
113 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
114 valid |= ATTR_CTIME;
115 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
116 valid |= ATTR_MTIME;
117
118 attr->ia_size = yaffs_get_file_size(obj);
119 valid |= ATTR_SIZE;
120
121 attr->ia_valid = valid;
122
123 return YAFFS_OK;
124}
diff --git a/fs/yaffs2/yaffs_attribs.h b/fs/yaffs2/yaffs_attribs.h
new file mode 100644
index 00000000000..33d541d6944
--- /dev/null
+++ b/fs/yaffs2/yaffs_attribs.h
@@ -0,0 +1,28 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_ATTRIBS_H__
17#define __YAFFS_ATTRIBS_H__
18
19#include "yaffs_guts.h"
20
21void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh);
22void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj);
23void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev);
24void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c);
25int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr);
26int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr);
27
28#endif
diff --git a/fs/yaffs2/yaffs_bitmap.c b/fs/yaffs2/yaffs_bitmap.c
new file mode 100644
index 00000000000..7df42cd0066
--- /dev/null
+++ b/fs/yaffs2/yaffs_bitmap.c
@@ -0,0 +1,98 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_bitmap.h"
15#include "yaffs_trace.h"
16/*
17 * Chunk bitmap manipulations
18 */
19
20static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
21{
22 if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
23 yaffs_trace(YAFFS_TRACE_ERROR,
24 "BlockBits block %d is not valid",
25 blk);
26 YBUG();
27 }
28 return dev->chunk_bits +
29 (dev->chunk_bit_stride * (blk - dev->internal_start_block));
30}
31
32void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
33{
34 if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
35 chunk < 0 || chunk >= dev->param.chunks_per_block) {
36 yaffs_trace(YAFFS_TRACE_ERROR,
37 "Chunk Id (%d:%d) invalid",
38 blk, chunk);
39 YBUG();
40 }
41}
42
43void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
44{
45 u8 *blk_bits = yaffs_block_bits(dev, blk);
46
47 memset(blk_bits, 0, dev->chunk_bit_stride);
48}
49
50void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
51{
52 u8 *blk_bits = yaffs_block_bits(dev, blk);
53
54 yaffs_verify_chunk_bit_id(dev, blk, chunk);
55
56 blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
57}
58
59void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
60{
61 u8 *blk_bits = yaffs_block_bits(dev, blk);
62
63 yaffs_verify_chunk_bit_id(dev, blk, chunk);
64
65 blk_bits[chunk / 8] |= (1 << (chunk & 7));
66}
67
68int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
69{
70 u8 *blk_bits = yaffs_block_bits(dev, blk);
71 yaffs_verify_chunk_bit_id(dev, blk, chunk);
72
73 return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
74}
75
76int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
77{
78 u8 *blk_bits = yaffs_block_bits(dev, blk);
79 int i;
80 for (i = 0; i < dev->chunk_bit_stride; i++) {
81 if (*blk_bits)
82 return 1;
83 blk_bits++;
84 }
85 return 0;
86}
87
88int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
89{
90 u8 *blk_bits = yaffs_block_bits(dev, blk);
91 int i;
92 int n = 0;
93
94 for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++)
95 n += hweight8(*blk_bits);
96
97 return n;
98}
diff --git a/fs/yaffs2/yaffs_bitmap.h b/fs/yaffs2/yaffs_bitmap.h
new file mode 100644
index 00000000000..cf9ea58da0d
--- /dev/null
+++ b/fs/yaffs2/yaffs_bitmap.h
@@ -0,0 +1,33 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16/*
17 * Chunk bitmap manipulations
18 */
19
20#ifndef __YAFFS_BITMAP_H__
21#define __YAFFS_BITMAP_H__
22
23#include "yaffs_guts.h"
24
25void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk);
26void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk);
27void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
28void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
29int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
30int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk);
31int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk);
32
33#endif
diff --git a/fs/yaffs2/yaffs_checkptrw.c b/fs/yaffs2/yaffs_checkptrw.c
new file mode 100644
index 00000000000..4e40f437e65
--- /dev/null
+++ b/fs/yaffs2/yaffs_checkptrw.c
@@ -0,0 +1,415 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_checkptrw.h"
15#include "yaffs_getblockinfo.h"
16
17static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
18{
19 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
20
21 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
22 "checkpt blocks_avail = %d", blocks_avail);
23
24 return (blocks_avail <= 0) ? 0 : 1;
25}
26
27static int yaffs_checkpt_erase(struct yaffs_dev *dev)
28{
29 int i;
30
31 if (!dev->param.erase_fn)
32 return 0;
33 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
34 "checking blocks %d to %d",
35 dev->internal_start_block, dev->internal_end_block);
36
37 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
38 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
39 if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
40 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
41 "erasing checkpt block %d", i);
42
43 dev->n_erasures++;
44
45 if (dev->param.
46 erase_fn(dev,
47 i - dev->block_offset /* realign */ )) {
48 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
49 dev->n_erased_blocks++;
50 dev->n_free_chunks +=
51 dev->param.chunks_per_block;
52 } else {
53 dev->param.bad_block_fn(dev, i);
54 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
55 }
56 }
57 }
58
59 dev->blocks_in_checkpt = 0;
60
61 return 1;
62}
63
64static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
65{
66 int i;
67 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
68 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
69 "allocating checkpt block: erased %d reserved %d avail %d next %d ",
70 dev->n_erased_blocks, dev->param.n_reserved_blocks,
71 blocks_avail, dev->checkpt_next_block);
72
73 if (dev->checkpt_next_block >= 0 &&
74 dev->checkpt_next_block <= dev->internal_end_block &&
75 blocks_avail > 0) {
76
77 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
78 i++) {
79 struct yaffs_block_info *bi =
80 yaffs_get_block_info(dev, i);
81 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
82 dev->checkpt_next_block = i + 1;
83 dev->checkpt_cur_block = i;
84 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
85 "allocating checkpt block %d", i);
86 return;
87 }
88 }
89 }
90 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
91
92 dev->checkpt_next_block = -1;
93 dev->checkpt_cur_block = -1;
94}
95
96static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
97{
98 int i;
99 struct yaffs_ext_tags tags;
100
101 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
102 "find next checkpt block: start: blocks %d next %d",
103 dev->blocks_in_checkpt, dev->checkpt_next_block);
104
105 if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
106 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
107 i++) {
108 int chunk = i * dev->param.chunks_per_block;
109 int realigned_chunk = chunk - dev->chunk_offset;
110
111 dev->param.read_chunk_tags_fn(dev, realigned_chunk,
112 NULL, &tags);
113 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
114 "find next checkpt block: search: block %d oid %d seq %d eccr %d",
115 i, tags.obj_id, tags.seq_number,
116 tags.ecc_result);
117
118 if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
119 /* Right kind of block */
120 dev->checkpt_next_block = tags.obj_id;
121 dev->checkpt_cur_block = i;
122 dev->checkpt_block_list[dev->
123 blocks_in_checkpt] = i;
124 dev->blocks_in_checkpt++;
125 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
126 "found checkpt block %d", i);
127 return;
128 }
129 }
130
131 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
132
133 dev->checkpt_next_block = -1;
134 dev->checkpt_cur_block = -1;
135}
136
137int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
138{
139
140 dev->checkpt_open_write = writing;
141
142 /* Got the functions we need? */
143 if (!dev->param.write_chunk_tags_fn ||
144 !dev->param.read_chunk_tags_fn ||
145 !dev->param.erase_fn || !dev->param.bad_block_fn)
146 return 0;
147
148 if (writing && !yaffs2_checkpt_space_ok(dev))
149 return 0;
150
151 if (!dev->checkpt_buffer)
152 dev->checkpt_buffer =
153 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
154 if (!dev->checkpt_buffer)
155 return 0;
156
157 dev->checkpt_page_seq = 0;
158 dev->checkpt_byte_count = 0;
159 dev->checkpt_sum = 0;
160 dev->checkpt_xor = 0;
161 dev->checkpt_cur_block = -1;
162 dev->checkpt_cur_chunk = -1;
163 dev->checkpt_next_block = dev->internal_start_block;
164
165 /* Erase all the blocks in the checkpoint area */
166 if (writing) {
167 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
168 dev->checkpt_byte_offs = 0;
169 return yaffs_checkpt_erase(dev);
170 } else {
171 int i;
172 /* Set to a value that will kick off a read */
173 dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
174 /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
175 * going to be way more than we need */
176 dev->blocks_in_checkpt = 0;
177 dev->checkpt_max_blocks =
178 (dev->internal_end_block - dev->internal_start_block) / 16 +
179 2;
180 dev->checkpt_block_list =
181 kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
182 if (!dev->checkpt_block_list)
183 return 0;
184
185 for (i = 0; i < dev->checkpt_max_blocks; i++)
186 dev->checkpt_block_list[i] = -1;
187 }
188
189 return 1;
190}
191
192int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
193{
194 u32 composite_sum;
195 composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
196 *sum = composite_sum;
197 return 1;
198}
199
200static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
201{
202 int chunk;
203 int realigned_chunk;
204
205 struct yaffs_ext_tags tags;
206
207 if (dev->checkpt_cur_block < 0) {
208 yaffs2_checkpt_find_erased_block(dev);
209 dev->checkpt_cur_chunk = 0;
210 }
211
212 if (dev->checkpt_cur_block < 0)
213 return 0;
214
215 tags.is_deleted = 0;
216 tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
217 tags.chunk_id = dev->checkpt_page_seq + 1;
218 tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
219 tags.n_bytes = dev->data_bytes_per_chunk;
220 if (dev->checkpt_cur_chunk == 0) {
221 /* First chunk we write for the block? Set block state to
222 checkpoint */
223 struct yaffs_block_info *bi =
224 yaffs_get_block_info(dev, dev->checkpt_cur_block);
225 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
226 dev->blocks_in_checkpt++;
227 }
228
229 chunk =
230 dev->checkpt_cur_block * dev->param.chunks_per_block +
231 dev->checkpt_cur_chunk;
232
233 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
234 "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
235 chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
236 tags.obj_id, tags.chunk_id);
237
238 realigned_chunk = chunk - dev->chunk_offset;
239
240 dev->n_page_writes++;
241
242 dev->param.write_chunk_tags_fn(dev, realigned_chunk,
243 dev->checkpt_buffer, &tags);
244 dev->checkpt_byte_offs = 0;
245 dev->checkpt_page_seq++;
246 dev->checkpt_cur_chunk++;
247 if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
248 dev->checkpt_cur_chunk = 0;
249 dev->checkpt_cur_block = -1;
250 }
251 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
252
253 return 1;
254}
255
256int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
257{
258 int i = 0;
259 int ok = 1;
260
261 u8 *data_bytes = (u8 *) data;
262
263 if (!dev->checkpt_buffer)
264 return 0;
265
266 if (!dev->checkpt_open_write)
267 return -1;
268
269 while (i < n_bytes && ok) {
270 dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
271 dev->checkpt_sum += *data_bytes;
272 dev->checkpt_xor ^= *data_bytes;
273
274 dev->checkpt_byte_offs++;
275 i++;
276 data_bytes++;
277 dev->checkpt_byte_count++;
278
279 if (dev->checkpt_byte_offs < 0 ||
280 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
281 ok = yaffs2_checkpt_flush_buffer(dev);
282 }
283
284 return i;
285}
286
287int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
288{
289 int i = 0;
290 int ok = 1;
291 struct yaffs_ext_tags tags;
292
293 int chunk;
294 int realigned_chunk;
295
296 u8 *data_bytes = (u8 *) data;
297
298 if (!dev->checkpt_buffer)
299 return 0;
300
301 if (dev->checkpt_open_write)
302 return -1;
303
304 while (i < n_bytes && ok) {
305
306 if (dev->checkpt_byte_offs < 0 ||
307 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
308
309 if (dev->checkpt_cur_block < 0) {
310 yaffs2_checkpt_find_block(dev);
311 dev->checkpt_cur_chunk = 0;
312 }
313
314 if (dev->checkpt_cur_block < 0)
315 ok = 0;
316 else {
317 chunk = dev->checkpt_cur_block *
318 dev->param.chunks_per_block +
319 dev->checkpt_cur_chunk;
320
321 realigned_chunk = chunk - dev->chunk_offset;
322
323 dev->n_page_reads++;
324
325 /* read in the next chunk */
326 dev->param.read_chunk_tags_fn(dev,
327 realigned_chunk,
328 dev->
329 checkpt_buffer,
330 &tags);
331
332 if (tags.chunk_id != (dev->checkpt_page_seq + 1)
333 || tags.ecc_result > YAFFS_ECC_RESULT_FIXED
334 || tags.seq_number !=
335 YAFFS_SEQUENCE_CHECKPOINT_DATA)
336 ok = 0;
337
338 dev->checkpt_byte_offs = 0;
339 dev->checkpt_page_seq++;
340 dev->checkpt_cur_chunk++;
341
342 if (dev->checkpt_cur_chunk >=
343 dev->param.chunks_per_block)
344 dev->checkpt_cur_block = -1;
345 }
346 }
347
348 if (ok) {
349 *data_bytes =
350 dev->checkpt_buffer[dev->checkpt_byte_offs];
351 dev->checkpt_sum += *data_bytes;
352 dev->checkpt_xor ^= *data_bytes;
353 dev->checkpt_byte_offs++;
354 i++;
355 data_bytes++;
356 dev->checkpt_byte_count++;
357 }
358 }
359
360 return i;
361}
362
363int yaffs_checkpt_close(struct yaffs_dev *dev)
364{
365
366 if (dev->checkpt_open_write) {
367 if (dev->checkpt_byte_offs != 0)
368 yaffs2_checkpt_flush_buffer(dev);
369 } else if (dev->checkpt_block_list) {
370 int i;
371 for (i = 0;
372 i < dev->blocks_in_checkpt
373 && dev->checkpt_block_list[i] >= 0; i++) {
374 int blk = dev->checkpt_block_list[i];
375 struct yaffs_block_info *bi = NULL;
376 if (dev->internal_start_block <= blk
377 && blk <= dev->internal_end_block)
378 bi = yaffs_get_block_info(dev, blk);
379 if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
380 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
381 else {
382 /* Todo this looks odd... */
383 }
384 }
385 kfree(dev->checkpt_block_list);
386 dev->checkpt_block_list = NULL;
387 }
388
389 dev->n_free_chunks -=
390 dev->blocks_in_checkpt * dev->param.chunks_per_block;
391 dev->n_erased_blocks -= dev->blocks_in_checkpt;
392
393 yaffs_trace(YAFFS_TRACE_CHECKPOINT,"checkpoint byte count %d",
394 dev->checkpt_byte_count);
395
396 if (dev->checkpt_buffer) {
397 /* free the buffer */
398 kfree(dev->checkpt_buffer);
399 dev->checkpt_buffer = NULL;
400 return 1;
401 } else {
402 return 0;
403 }
404}
405
406int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
407{
408 /* Erase the checkpoint data */
409
410 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
411 "checkpoint invalidate of %d blocks",
412 dev->blocks_in_checkpt);
413
414 return yaffs_checkpt_erase(dev);
415}
diff --git a/fs/yaffs2/yaffs_checkptrw.h b/fs/yaffs2/yaffs_checkptrw.h
new file mode 100644
index 00000000000..361c6067717
--- /dev/null
+++ b/fs/yaffs2/yaffs_checkptrw.h
@@ -0,0 +1,33 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_CHECKPTRW_H__
17#define __YAFFS_CHECKPTRW_H__
18
19#include "yaffs_guts.h"
20
21int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing);
22
23int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes);
24
25int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes);
26
27int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum);
28
29int yaffs_checkpt_close(struct yaffs_dev *dev);
30
31int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev);
32
33#endif
diff --git a/fs/yaffs2/yaffs_ecc.c b/fs/yaffs2/yaffs_ecc.c
new file mode 100644
index 00000000000..e95a8069a8c
--- /dev/null
+++ b/fs/yaffs2/yaffs_ecc.c
@@ -0,0 +1,298 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/*
15 * This code implements the ECC algorithm used in SmartMedia.
16 *
17 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
18 * The two unused bit are set to 1.
19 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
20 * blocks are used on a 512-byte NAND page.
21 *
22 */
23
24/* Table generated by gen-ecc.c
25 * Using a table means we do not have to calculate p1..p4 and p1'..p4'
26 * for each byte of data. These are instead provided in a table in bits7..2.
27 * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
28 * this bytes influence on the line parity.
29 */
30
31#include "yportenv.h"
32
33#include "yaffs_ecc.h"
34
35static const unsigned char column_parity_table[] = {
36 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
37 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
38 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
39 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
40 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
41 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
42 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
43 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
44 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
45 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
46 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
47 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
48 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
49 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
50 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
51 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
52 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
53 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
54 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
55 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
56 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
57 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
58 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
59 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
60 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
61 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
62 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
63 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
64 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
65 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
66 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
67 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
68};
69
70
71/* Calculate the ECC for a 256-byte block of data */
72void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
73{
74 unsigned int i;
75
76 unsigned char col_parity = 0;
77 unsigned char line_parity = 0;
78 unsigned char line_parity_prime = 0;
79 unsigned char t;
80 unsigned char b;
81
82 for (i = 0; i < 256; i++) {
83 b = column_parity_table[*data++];
84 col_parity ^= b;
85
86 if (b & 0x01) { /* odd number of bits in the byte */
87 line_parity ^= i;
88 line_parity_prime ^= ~i;
89 }
90 }
91
92 ecc[2] = (~col_parity) | 0x03;
93
94 t = 0;
95 if (line_parity & 0x80)
96 t |= 0x80;
97 if (line_parity_prime & 0x80)
98 t |= 0x40;
99 if (line_parity & 0x40)
100 t |= 0x20;
101 if (line_parity_prime & 0x40)
102 t |= 0x10;
103 if (line_parity & 0x20)
104 t |= 0x08;
105 if (line_parity_prime & 0x20)
106 t |= 0x04;
107 if (line_parity & 0x10)
108 t |= 0x02;
109 if (line_parity_prime & 0x10)
110 t |= 0x01;
111 ecc[1] = ~t;
112
113 t = 0;
114 if (line_parity & 0x08)
115 t |= 0x80;
116 if (line_parity_prime & 0x08)
117 t |= 0x40;
118 if (line_parity & 0x04)
119 t |= 0x20;
120 if (line_parity_prime & 0x04)
121 t |= 0x10;
122 if (line_parity & 0x02)
123 t |= 0x08;
124 if (line_parity_prime & 0x02)
125 t |= 0x04;
126 if (line_parity & 0x01)
127 t |= 0x02;
128 if (line_parity_prime & 0x01)
129 t |= 0x01;
130 ecc[0] = ~t;
131
132#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
133 /* Swap the bytes into the wrong order */
134 t = ecc[0];
135 ecc[0] = ecc[1];
136 ecc[1] = t;
137#endif
138}
139
140/* Correct the ECC on a 256 byte block of data */
141
142int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
143 const unsigned char *test_ecc)
144{
145 unsigned char d0, d1, d2; /* deltas */
146
147 d0 = read_ecc[0] ^ test_ecc[0];
148 d1 = read_ecc[1] ^ test_ecc[1];
149 d2 = read_ecc[2] ^ test_ecc[2];
150
151 if ((d0 | d1 | d2) == 0)
152 return 0; /* no error */
153
154 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
155 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
156 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
157 /* Single bit (recoverable) error in data */
158
159 unsigned byte;
160 unsigned bit;
161
162#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
163 /* swap the bytes to correct for the wrong order */
164 unsigned char t;
165
166 t = d0;
167 d0 = d1;
168 d1 = t;
169#endif
170
171 bit = byte = 0;
172
173 if (d1 & 0x80)
174 byte |= 0x80;
175 if (d1 & 0x20)
176 byte |= 0x40;
177 if (d1 & 0x08)
178 byte |= 0x20;
179 if (d1 & 0x02)
180 byte |= 0x10;
181 if (d0 & 0x80)
182 byte |= 0x08;
183 if (d0 & 0x20)
184 byte |= 0x04;
185 if (d0 & 0x08)
186 byte |= 0x02;
187 if (d0 & 0x02)
188 byte |= 0x01;
189
190 if (d2 & 0x80)
191 bit |= 0x04;
192 if (d2 & 0x20)
193 bit |= 0x02;
194 if (d2 & 0x08)
195 bit |= 0x01;
196
197 data[byte] ^= (1 << bit);
198
199 return 1; /* Corrected the error */
200 }
201
202 if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
203 /* Reccoverable error in ecc */
204
205 read_ecc[0] = test_ecc[0];
206 read_ecc[1] = test_ecc[1];
207 read_ecc[2] = test_ecc[2];
208
209 return 1; /* Corrected the error */
210 }
211
212 /* Unrecoverable error */
213
214 return -1;
215
216}
217
218/*
219 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
220 */
221void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
222 struct yaffs_ecc_other *ecc_other)
223{
224 unsigned int i;
225
226 unsigned char col_parity = 0;
227 unsigned line_parity = 0;
228 unsigned line_parity_prime = 0;
229 unsigned char b;
230
231 for (i = 0; i < n_bytes; i++) {
232 b = column_parity_table[*data++];
233 col_parity ^= b;
234
235 if (b & 0x01) {
236 /* odd number of bits in the byte */
237 line_parity ^= i;
238 line_parity_prime ^= ~i;
239 }
240
241 }
242
243 ecc_other->col_parity = (col_parity >> 2) & 0x3f;
244 ecc_other->line_parity = line_parity;
245 ecc_other->line_parity_prime = line_parity_prime;
246}
247
248int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
249 struct yaffs_ecc_other *read_ecc,
250 const struct yaffs_ecc_other *test_ecc)
251{
252 unsigned char delta_col; /* column parity delta */
253 unsigned delta_line; /* line parity delta */
254 unsigned delta_line_prime; /* line parity delta */
255 unsigned bit;
256
257 delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
258 delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
259 delta_line_prime =
260 read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
261
262 if ((delta_col | delta_line | delta_line_prime) == 0)
263 return 0; /* no error */
264
265 if (delta_line == ~delta_line_prime &&
266 (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
267 /* Single bit (recoverable) error in data */
268
269 bit = 0;
270
271 if (delta_col & 0x20)
272 bit |= 0x04;
273 if (delta_col & 0x08)
274 bit |= 0x02;
275 if (delta_col & 0x02)
276 bit |= 0x01;
277
278 if (delta_line >= n_bytes)
279 return -1;
280
281 data[delta_line] ^= (1 << bit);
282
283 return 1; /* corrected */
284 }
285
286 if ((hweight32(delta_line) +
287 hweight32(delta_line_prime) +
288 hweight8(delta_col)) == 1) {
289 /* Reccoverable error in ecc */
290
291 *read_ecc = *test_ecc;
292 return 1; /* corrected */
293 }
294
295 /* Unrecoverable error */
296
297 return -1;
298}
diff --git a/fs/yaffs2/yaffs_ecc.h b/fs/yaffs2/yaffs_ecc.h
new file mode 100644
index 00000000000..b0c461d699e
--- /dev/null
+++ b/fs/yaffs2/yaffs_ecc.h
@@ -0,0 +1,44 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16/*
17 * This code implements the ECC algorithm used in SmartMedia.
18 *
19 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
20 * The two unused bit are set to 1.
21 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
22 * blocks are used on a 512-byte NAND page.
23 *
24 */
25
26#ifndef __YAFFS_ECC_H__
27#define __YAFFS_ECC_H__
28
29struct yaffs_ecc_other {
30 unsigned char col_parity;
31 unsigned line_parity;
32 unsigned line_parity_prime;
33};
34
35void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc);
36int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
37 const unsigned char *test_ecc);
38
39void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
40 struct yaffs_ecc_other *ecc);
41int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
42 struct yaffs_ecc_other *read_ecc,
43 const struct yaffs_ecc_other *test_ecc);
44#endif
diff --git a/fs/yaffs2/yaffs_getblockinfo.h b/fs/yaffs2/yaffs_getblockinfo.h
new file mode 100644
index 00000000000..d87acbde997
--- /dev/null
+++ b/fs/yaffs2/yaffs_getblockinfo.h
@@ -0,0 +1,35 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_GETBLOCKINFO_H__
17#define __YAFFS_GETBLOCKINFO_H__
18
19#include "yaffs_guts.h"
20#include "yaffs_trace.h"
21
22/* Function to manipulate block info */
23static inline struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev
24 *dev, int blk)
25{
26 if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
27 yaffs_trace(YAFFS_TRACE_ERROR,
28 "**>> yaffs: get_block_info block %d is not valid",
29 blk);
30 YBUG();
31 }
32 return &dev->block_info[blk - dev->internal_start_block];
33}
34
35#endif
diff --git a/fs/yaffs2/yaffs_guts.c b/fs/yaffs2/yaffs_guts.c
new file mode 100644
index 00000000000..f4ae9deed72
--- /dev/null
+++ b/fs/yaffs2/yaffs_guts.c
@@ -0,0 +1,5164 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yportenv.h"
15#include "yaffs_trace.h"
16
17#include "yaffs_guts.h"
18#include "yaffs_tagsvalidity.h"
19#include "yaffs_getblockinfo.h"
20
21#include "yaffs_tagscompat.h"
22
23#include "yaffs_nand.h"
24
25#include "yaffs_yaffs1.h"
26#include "yaffs_yaffs2.h"
27#include "yaffs_bitmap.h"
28#include "yaffs_verify.h"
29
30#include "yaffs_nand.h"
31#include "yaffs_packedtags2.h"
32
33#include "yaffs_nameval.h"
34#include "yaffs_allocator.h"
35
36#include "yaffs_attribs.h"
37
38/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
39#define YAFFS_GC_GOOD_ENOUGH 2
40#define YAFFS_GC_PASSIVE_THRESHOLD 4
41
42#include "yaffs_ecc.h"
43
44/* Forward declarations */
45
46static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
47 const u8 * buffer, int n_bytes, int use_reserve);
48
49
50
51/* Function to calculate chunk and offset */
52
53static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
54 int *chunk_out, u32 * offset_out)
55{
56 int chunk;
57 u32 offset;
58
59 chunk = (u32) (addr >> dev->chunk_shift);
60
61 if (dev->chunk_div == 1) {
62 /* easy power of 2 case */
63 offset = (u32) (addr & dev->chunk_mask);
64 } else {
65 /* Non power-of-2 case */
66
67 loff_t chunk_base;
68
69 chunk /= dev->chunk_div;
70
71 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
72 offset = (u32) (addr - chunk_base);
73 }
74
75 *chunk_out = chunk;
76 *offset_out = offset;
77}
78
79/* Function to return the number of shifts for a power of 2 greater than or
80 * equal to the given number
81 * Note we don't try to cater for all possible numbers and this does not have to
82 * be hellishly efficient.
83 */
84
85static u32 calc_shifts_ceiling(u32 x)
86{
87 int extra_bits;
88 int shifts;
89
90 shifts = extra_bits = 0;
91
92 while (x > 1) {
93 if (x & 1)
94 extra_bits++;
95 x >>= 1;
96 shifts++;
97 }
98
99 if (extra_bits)
100 shifts++;
101
102 return shifts;
103}
104
105/* Function to return the number of shifts to get a 1 in bit 0
106 */
107
108static u32 calc_shifts(u32 x)
109{
110 u32 shifts;
111
112 shifts = 0;
113
114 if (!x)
115 return 0;
116
117 while (!(x & 1)) {
118 x >>= 1;
119 shifts++;
120 }
121
122 return shifts;
123}
124
125/*
126 * Temporary buffer manipulations.
127 */
128
129static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
130{
131 int i;
132 u8 *buf = (u8 *) 1;
133
134 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
135
136 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
137 dev->temp_buffer[i].line = 0; /* not in use */
138 dev->temp_buffer[i].buffer = buf =
139 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
140 }
141
142 return buf ? YAFFS_OK : YAFFS_FAIL;
143}
144
145u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
146{
147 int i, j;
148
149 dev->temp_in_use++;
150 if (dev->temp_in_use > dev->max_temp)
151 dev->max_temp = dev->temp_in_use;
152
153 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
154 if (dev->temp_buffer[i].line == 0) {
155 dev->temp_buffer[i].line = line_no;
156 if ((i + 1) > dev->max_temp) {
157 dev->max_temp = i + 1;
158 for (j = 0; j <= i; j++)
159 dev->temp_buffer[j].max_line =
160 dev->temp_buffer[j].line;
161 }
162
163 return dev->temp_buffer[i].buffer;
164 }
165 }
166
167 yaffs_trace(YAFFS_TRACE_BUFFERS,
168 "Out of temp buffers at line %d, other held by lines:",
169 line_no);
170 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
171 yaffs_trace(YAFFS_TRACE_BUFFERS," %d", dev->temp_buffer[i].line);
172
173 /*
174 * If we got here then we have to allocate an unmanaged one
175 * This is not good.
176 */
177
178 dev->unmanaged_buffer_allocs++;
179 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
180
181}
182
183void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
184{
185 int i;
186
187 dev->temp_in_use--;
188
189 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
190 if (dev->temp_buffer[i].buffer == buffer) {
191 dev->temp_buffer[i].line = 0;
192 return;
193 }
194 }
195
196 if (buffer) {
197 /* assume it is an unmanaged one. */
198 yaffs_trace(YAFFS_TRACE_BUFFERS,
199 "Releasing unmanaged temp buffer in line %d",
200 line_no);
201 kfree(buffer);
202 dev->unmanaged_buffer_deallocs++;
203 }
204
205}
206
207/*
208 * Determine if we have a managed buffer.
209 */
210int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
211{
212 int i;
213
214 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
215 if (dev->temp_buffer[i].buffer == buffer)
216 return 1;
217 }
218
219 for (i = 0; i < dev->param.n_caches; i++) {
220 if (dev->cache[i].data == buffer)
221 return 1;
222 }
223
224 if (buffer == dev->checkpt_buffer)
225 return 1;
226
227 yaffs_trace(YAFFS_TRACE_ALWAYS,
228 "yaffs: unmaged buffer detected.");
229 return 0;
230}
231
232/*
233 * Functions for robustisizing TODO
234 *
235 */
236
237static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
238 const u8 * data,
239 const struct yaffs_ext_tags *tags)
240{
241 dev = dev;
242 nand_chunk = nand_chunk;
243 data = data;
244 tags = tags;
245}
246
247static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
248 const struct yaffs_ext_tags *tags)
249{
250 dev = dev;
251 nand_chunk = nand_chunk;
252 tags = tags;
253}
254
255void yaffs_handle_chunk_error(struct yaffs_dev *dev,
256 struct yaffs_block_info *bi)
257{
258 if (!bi->gc_prioritise) {
259 bi->gc_prioritise = 1;
260 dev->has_pending_prioritised_gc = 1;
261 bi->chunk_error_strikes++;
262
263 if (bi->chunk_error_strikes > 3) {
264 bi->needs_retiring = 1; /* Too many stikes, so retire this */
265 yaffs_trace(YAFFS_TRACE_ALWAYS, "yaffs: Block struck out");
266
267 }
268 }
269}
270
271static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
272 int erased_ok)
273{
274 int flash_block = nand_chunk / dev->param.chunks_per_block;
275 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
276
277 yaffs_handle_chunk_error(dev, bi);
278
279 if (erased_ok) {
280 /* Was an actual write failure, so mark the block for retirement */
281 bi->needs_retiring = 1;
282 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
283 "**>> Block %d needs retiring", flash_block);
284 }
285
286 /* Delete the chunk */
287 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
288 yaffs_skip_rest_of_block(dev);
289}
290
291/*
292 * Verification code
293 */
294
295/*
296 * Simple hash function. Needs to have a reasonable spread
297 */
298
299static inline int yaffs_hash_fn(int n)
300{
301 n = abs(n);
302 return n % YAFFS_NOBJECT_BUCKETS;
303}
304
305/*
306 * Access functions to useful fake objects.
307 * Note that root might have a presence in NAND if permissions are set.
308 */
309
310struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
311{
312 return dev->root_dir;
313}
314
315struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
316{
317 return dev->lost_n_found;
318}
319
320/*
321 * Erased NAND checking functions
322 */
323
324int yaffs_check_ff(u8 * buffer, int n_bytes)
325{
326 /* Horrible, slow implementation */
327 while (n_bytes--) {
328 if (*buffer != 0xFF)
329 return 0;
330 buffer++;
331 }
332 return 1;
333}
334
335static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
336{
337 int retval = YAFFS_OK;
338 u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
339 struct yaffs_ext_tags tags;
340 int result;
341
342 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
343
344 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
345 retval = YAFFS_FAIL;
346
347 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
348 tags.chunk_used) {
349 yaffs_trace(YAFFS_TRACE_NANDACCESS, "Chunk %d not erased", nand_chunk);
350 retval = YAFFS_FAIL;
351 }
352
353 yaffs_release_temp_buffer(dev, data, __LINE__);
354
355 return retval;
356
357}
358
359static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
360 int nand_chunk,
361 const u8 * data,
362 struct yaffs_ext_tags *tags)
363{
364 int retval = YAFFS_OK;
365 struct yaffs_ext_tags temp_tags;
366 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
367 int result;
368
369 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
370 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
371 temp_tags.obj_id != tags->obj_id ||
372 temp_tags.chunk_id != tags->chunk_id ||
373 temp_tags.n_bytes != tags->n_bytes)
374 retval = YAFFS_FAIL;
375
376 yaffs_release_temp_buffer(dev, buffer, __LINE__);
377
378 return retval;
379}
380
381
382int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
383{
384 int reserved_chunks;
385 int reserved_blocks = dev->param.n_reserved_blocks;
386 int checkpt_blocks;
387
388 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
389
390 reserved_chunks =
391 ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
392
393 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
394}
395
396static int yaffs_find_alloc_block(struct yaffs_dev *dev)
397{
398 int i;
399
400 struct yaffs_block_info *bi;
401
402 if (dev->n_erased_blocks < 1) {
403 /* Hoosterman we've got a problem.
404 * Can't get space to gc
405 */
406 yaffs_trace(YAFFS_TRACE_ERROR,
407 "yaffs tragedy: no more erased blocks" );
408
409 return -1;
410 }
411
412 /* Find an empty block. */
413
414 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
415 dev->alloc_block_finder++;
416 if (dev->alloc_block_finder < dev->internal_start_block
417 || dev->alloc_block_finder > dev->internal_end_block) {
418 dev->alloc_block_finder = dev->internal_start_block;
419 }
420
421 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
422
423 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
424 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
425 dev->seq_number++;
426 bi->seq_number = dev->seq_number;
427 dev->n_erased_blocks--;
428 yaffs_trace(YAFFS_TRACE_ALLOCATE,
429 "Allocated block %d, seq %d, %d left" ,
430 dev->alloc_block_finder, dev->seq_number,
431 dev->n_erased_blocks);
432 return dev->alloc_block_finder;
433 }
434 }
435
436 yaffs_trace(YAFFS_TRACE_ALWAYS,
437 "yaffs tragedy: no more erased blocks, but there should have been %d",
438 dev->n_erased_blocks);
439
440 return -1;
441}
442
443static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
444 struct yaffs_block_info **block_ptr)
445{
446 int ret_val;
447 struct yaffs_block_info *bi;
448
449 if (dev->alloc_block < 0) {
450 /* Get next block to allocate off */
451 dev->alloc_block = yaffs_find_alloc_block(dev);
452 dev->alloc_page = 0;
453 }
454
455 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
456 /* Not enough space to allocate unless we're allowed to use the reserve. */
457 return -1;
458 }
459
460 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
461 && dev->alloc_page == 0)
462 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
463
464 /* Next page please.... */
465 if (dev->alloc_block >= 0) {
466 bi = yaffs_get_block_info(dev, dev->alloc_block);
467
468 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
469 dev->alloc_page;
470 bi->pages_in_use++;
471 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
472
473 dev->alloc_page++;
474
475 dev->n_free_chunks--;
476
477 /* If the block is full set the state to full */
478 if (dev->alloc_page >= dev->param.chunks_per_block) {
479 bi->block_state = YAFFS_BLOCK_STATE_FULL;
480 dev->alloc_block = -1;
481 }
482
483 if (block_ptr)
484 *block_ptr = bi;
485
486 return ret_val;
487 }
488
489 yaffs_trace(YAFFS_TRACE_ERROR, "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" );
490
491 return -1;
492}
493
494static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
495{
496 int n;
497
498 n = dev->n_erased_blocks * dev->param.chunks_per_block;
499
500 if (dev->alloc_block > 0)
501 n += (dev->param.chunks_per_block - dev->alloc_page);
502
503 return n;
504
505}
506
507/*
508 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
509 * if we don't want to write to it.
510 */
511void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
512{
513 if (dev->alloc_block > 0) {
514 struct yaffs_block_info *bi =
515 yaffs_get_block_info(dev, dev->alloc_block);
516 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
517 bi->block_state = YAFFS_BLOCK_STATE_FULL;
518 dev->alloc_block = -1;
519 }
520 }
521}
522
523static int yaffs_write_new_chunk(struct yaffs_dev *dev,
524 const u8 * data,
525 struct yaffs_ext_tags *tags, int use_reserver)
526{
527 int attempts = 0;
528 int write_ok = 0;
529 int chunk;
530
531 yaffs2_checkpt_invalidate(dev);
532
533 do {
534 struct yaffs_block_info *bi = 0;
535 int erased_ok = 0;
536
537 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
538 if (chunk < 0) {
539 /* no space */
540 break;
541 }
542
543 /* First check this chunk is erased, if it needs
544 * checking. The checking policy (unless forced
545 * always on) is as follows:
546 *
547 * Check the first page we try to write in a block.
548 * If the check passes then we don't need to check any
549 * more. If the check fails, we check again...
550 * If the block has been erased, we don't need to check.
551 *
552 * However, if the block has been prioritised for gc,
553 * then we think there might be something odd about
554 * this block and stop using it.
555 *
556 * Rationale: We should only ever see chunks that have
557 * not been erased if there was a partially written
558 * chunk due to power loss. This checking policy should
559 * catch that case with very few checks and thus save a
560 * lot of checks that are most likely not needed.
561 *
562 * Mods to the above
563 * If an erase check fails or the write fails we skip the
564 * rest of the block.
565 */
566
567 /* let's give it a try */
568 attempts++;
569
570 if (dev->param.always_check_erased)
571 bi->skip_erased_check = 0;
572
573 if (!bi->skip_erased_check) {
574 erased_ok = yaffs_check_chunk_erased(dev, chunk);
575 if (erased_ok != YAFFS_OK) {
576 yaffs_trace(YAFFS_TRACE_ERROR,
577 "**>> yaffs chunk %d was not erased",
578 chunk);
579
580 /* If not erased, delete this one,
581 * skip rest of block and
582 * try another chunk */
583 yaffs_chunk_del(dev, chunk, 1, __LINE__);
584 yaffs_skip_rest_of_block(dev);
585 continue;
586 }
587 }
588
589 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
590
591 if (!bi->skip_erased_check)
592 write_ok =
593 yaffs_verify_chunk_written(dev, chunk, data, tags);
594
595 if (write_ok != YAFFS_OK) {
596 /* Clean up aborted write, skip to next block and
597 * try another chunk */
598 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
599 continue;
600 }
601
602 bi->skip_erased_check = 1;
603
604 /* Copy the data into the robustification buffer */
605 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
606
607 } while (write_ok != YAFFS_OK &&
608 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
609
610 if (!write_ok)
611 chunk = -1;
612
613 if (attempts > 1) {
614 yaffs_trace(YAFFS_TRACE_ERROR,
615 "**>> yaffs write required %d attempts",
616 attempts);
617 dev->n_retired_writes += (attempts - 1);
618 }
619
620 return chunk;
621}
622
623/*
624 * Block retiring for handling a broken block.
625 */
626
627static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
628{
629 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
630
631 yaffs2_checkpt_invalidate(dev);
632
633 yaffs2_clear_oldest_dirty_seq(dev, bi);
634
635 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
636 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
637 yaffs_trace(YAFFS_TRACE_ALWAYS,
638 "yaffs: Failed to mark bad and erase block %d",
639 flash_block);
640 } else {
641 struct yaffs_ext_tags tags;
642 int chunk_id =
643 flash_block * dev->param.chunks_per_block;
644
645 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
646
647 memset(buffer, 0xff, dev->data_bytes_per_chunk);
648 yaffs_init_tags(&tags);
649 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
650 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
651 dev->chunk_offset,
652 buffer,
653 &tags) != YAFFS_OK)
654 yaffs_trace(YAFFS_TRACE_ALWAYS,
655 "yaffs: Failed to write bad block marker to block %d",
656 flash_block);
657
658 yaffs_release_temp_buffer(dev, buffer, __LINE__);
659 }
660 }
661
662 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
663 bi->gc_prioritise = 0;
664 bi->needs_retiring = 0;
665
666 dev->n_retired_blocks++;
667}
668
669/*---------------- Name handling functions ------------*/
670
671static u16 yaffs_calc_name_sum(const YCHAR * name)
672{
673 u16 sum = 0;
674 u16 i = 1;
675
676 const YUCHAR *bname = (const YUCHAR *)name;
677 if (bname) {
678 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
679
680 /* 0x1f mask is case insensitive */
681 sum += ((*bname) & 0x1f) * i;
682 i++;
683 bname++;
684 }
685 }
686 return sum;
687}
688
689void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
690{
691#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
692 memset(obj->short_name, 0, sizeof(obj->short_name));
693 if (name &&
694 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
695 YAFFS_SHORT_NAME_LENGTH)
696 strcpy(obj->short_name, name);
697 else
698 obj->short_name[0] = _Y('\0');
699#endif
700 obj->sum = yaffs_calc_name_sum(name);
701}
702
703void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
704 const struct yaffs_obj_hdr *oh)
705{
706#ifdef CONFIG_YAFFS_AUTO_UNICODE
707 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
708 memset(tmp_name, 0, sizeof(tmp_name));
709 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
710 YAFFS_MAX_NAME_LENGTH + 1);
711 yaffs_set_obj_name(obj, tmp_name);
712#else
713 yaffs_set_obj_name(obj, oh->name);
714#endif
715}
716
717/*-------------------- TNODES -------------------
718
719 * List of spare tnodes
720 * The list is hooked together using the first pointer
721 * in the tnode.
722 */
723
724struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
725{
726 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
727 if (tn) {
728 memset(tn, 0, dev->tnode_size);
729 dev->n_tnodes++;
730 }
731
732 dev->checkpoint_blocks_required = 0; /* force recalculation */
733
734 return tn;
735}
736
737/* FreeTnode frees up a tnode and puts it back on the free list */
738static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
739{
740 yaffs_free_raw_tnode(dev, tn);
741 dev->n_tnodes--;
742 dev->checkpoint_blocks_required = 0; /* force recalculation */
743}
744
745static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
746{
747 yaffs_deinit_raw_tnodes_and_objs(dev);
748 dev->n_obj = 0;
749 dev->n_tnodes = 0;
750}
751
752void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
753 unsigned pos, unsigned val)
754{
755 u32 *map = (u32 *) tn;
756 u32 bit_in_map;
757 u32 bit_in_word;
758 u32 word_in_map;
759 u32 mask;
760
761 pos &= YAFFS_TNODES_LEVEL0_MASK;
762 val >>= dev->chunk_grp_bits;
763
764 bit_in_map = pos * dev->tnode_width;
765 word_in_map = bit_in_map / 32;
766 bit_in_word = bit_in_map & (32 - 1);
767
768 mask = dev->tnode_mask << bit_in_word;
769
770 map[word_in_map] &= ~mask;
771 map[word_in_map] |= (mask & (val << bit_in_word));
772
773 if (dev->tnode_width > (32 - bit_in_word)) {
774 bit_in_word = (32 - bit_in_word);
775 word_in_map++;;
776 mask =
777 dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
778 map[word_in_map] &= ~mask;
779 map[word_in_map] |= (mask & (val >> bit_in_word));
780 }
781}
782
783u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
784 unsigned pos)
785{
786 u32 *map = (u32 *) tn;
787 u32 bit_in_map;
788 u32 bit_in_word;
789 u32 word_in_map;
790 u32 val;
791
792 pos &= YAFFS_TNODES_LEVEL0_MASK;
793
794 bit_in_map = pos * dev->tnode_width;
795 word_in_map = bit_in_map / 32;
796 bit_in_word = bit_in_map & (32 - 1);
797
798 val = map[word_in_map] >> bit_in_word;
799
800 if (dev->tnode_width > (32 - bit_in_word)) {
801 bit_in_word = (32 - bit_in_word);
802 word_in_map++;;
803 val |= (map[word_in_map] << bit_in_word);
804 }
805
806 val &= dev->tnode_mask;
807 val <<= dev->chunk_grp_bits;
808
809 return val;
810}
811
812/* ------------------- End of individual tnode manipulation -----------------*/
813
814/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
815 * The look up tree is represented by the top tnode and the number of top_level
816 * in the tree. 0 means only the level 0 tnode is in the tree.
817 */
818
819/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
820struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
821 struct yaffs_file_var *file_struct,
822 u32 chunk_id)
823{
824 struct yaffs_tnode *tn = file_struct->top;
825 u32 i;
826 int required_depth;
827 int level = file_struct->top_level;
828
829 dev = dev;
830
831 /* Check sane level and chunk Id */
832 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
833 return NULL;
834
835 if (chunk_id > YAFFS_MAX_CHUNK_ID)
836 return NULL;
837
838 /* First check we're tall enough (ie enough top_level) */
839
840 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
841 required_depth = 0;
842 while (i) {
843 i >>= YAFFS_TNODES_INTERNAL_BITS;
844 required_depth++;
845 }
846
847 if (required_depth > file_struct->top_level)
848 return NULL; /* Not tall enough, so we can't find it */
849
850 /* Traverse down to level 0 */
851 while (level > 0 && tn) {
852 tn = tn->internal[(chunk_id >>
853 (YAFFS_TNODES_LEVEL0_BITS +
854 (level - 1) *
855 YAFFS_TNODES_INTERNAL_BITS)) &
856 YAFFS_TNODES_INTERNAL_MASK];
857 level--;
858 }
859
860 return tn;
861}
862
863/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
864 * This happens in two steps:
865 * 1. If the tree isn't tall enough, then make it taller.
866 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
867 *
868 * Used when modifying the tree.
869 *
870 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
871 * be plugged into the ttree.
872 */
873
874struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
875 struct yaffs_file_var *file_struct,
876 u32 chunk_id,
877 struct yaffs_tnode *passed_tn)
878{
879 int required_depth;
880 int i;
881 int l;
882 struct yaffs_tnode *tn;
883
884 u32 x;
885
886 /* Check sane level and page Id */
887 if (file_struct->top_level < 0
888 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
889 return NULL;
890
891 if (chunk_id > YAFFS_MAX_CHUNK_ID)
892 return NULL;
893
894 /* First check we're tall enough (ie enough top_level) */
895
896 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
897 required_depth = 0;
898 while (x) {
899 x >>= YAFFS_TNODES_INTERNAL_BITS;
900 required_depth++;
901 }
902
903 if (required_depth > file_struct->top_level) {
904 /* Not tall enough, gotta make the tree taller */
905 for (i = file_struct->top_level; i < required_depth; i++) {
906
907 tn = yaffs_get_tnode(dev);
908
909 if (tn) {
910 tn->internal[0] = file_struct->top;
911 file_struct->top = tn;
912 file_struct->top_level++;
913 } else {
914 yaffs_trace(YAFFS_TRACE_ERROR, "yaffs: no more tnodes");
915 return NULL;
916 }
917 }
918 }
919
920 /* Traverse down to level 0, adding anything we need */
921
922 l = file_struct->top_level;
923 tn = file_struct->top;
924
925 if (l > 0) {
926 while (l > 0 && tn) {
927 x = (chunk_id >>
928 (YAFFS_TNODES_LEVEL0_BITS +
929 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
930 YAFFS_TNODES_INTERNAL_MASK;
931
932 if ((l > 1) && !tn->internal[x]) {
933 /* Add missing non-level-zero tnode */
934 tn->internal[x] = yaffs_get_tnode(dev);
935 if (!tn->internal[x])
936 return NULL;
937 } else if (l == 1) {
938 /* Looking from level 1 at level 0 */
939 if (passed_tn) {
940 /* If we already have one, then release it. */
941 if (tn->internal[x])
942 yaffs_free_tnode(dev,
943 tn->
944 internal[x]);
945 tn->internal[x] = passed_tn;
946
947 } else if (!tn->internal[x]) {
948 /* Don't have one, none passed in */
949 tn->internal[x] = yaffs_get_tnode(dev);
950 if (!tn->internal[x])
951 return NULL;
952 }
953 }
954
955 tn = tn->internal[x];
956 l--;
957 }
958 } else {
959 /* top is level 0 */
960 if (passed_tn) {
961 memcpy(tn, passed_tn,
962 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
963 yaffs_free_tnode(dev, passed_tn);
964 }
965 }
966
967 return tn;
968}
969
970static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
971 int chunk_obj)
972{
973 return (tags->chunk_id == chunk_obj &&
974 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
975
976}
977
978static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
979 struct yaffs_ext_tags *tags, int obj_id,
980 int inode_chunk)
981{
982 int j;
983
984 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
985 if (yaffs_check_chunk_bit
986 (dev, the_chunk / dev->param.chunks_per_block,
987 the_chunk % dev->param.chunks_per_block)) {
988
989 if (dev->chunk_grp_size == 1)
990 return the_chunk;
991 else {
992 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
993 tags);
994 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
995 /* found it; */
996 return the_chunk;
997 }
998 }
999 }
1000 the_chunk++;
1001 }
1002 return -1;
1003}
1004
1005static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1006 struct yaffs_ext_tags *tags)
1007{
1008 /*Get the Tnode, then get the level 0 offset chunk offset */
1009 struct yaffs_tnode *tn;
1010 int the_chunk = -1;
1011 struct yaffs_ext_tags local_tags;
1012 int ret_val = -1;
1013
1014 struct yaffs_dev *dev = in->my_dev;
1015
1016 if (!tags) {
1017 /* Passed a NULL, so use our own tags space */
1018 tags = &local_tags;
1019 }
1020
1021 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1022
1023 if (tn) {
1024 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1025
1026 ret_val =
1027 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1028 inode_chunk);
1029 }
1030 return ret_val;
1031}
1032
1033static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1034 struct yaffs_ext_tags *tags)
1035{
1036 /* Get the Tnode, then get the level 0 offset chunk offset */
1037 struct yaffs_tnode *tn;
1038 int the_chunk = -1;
1039 struct yaffs_ext_tags local_tags;
1040
1041 struct yaffs_dev *dev = in->my_dev;
1042 int ret_val = -1;
1043
1044 if (!tags) {
1045 /* Passed a NULL, so use our own tags space */
1046 tags = &local_tags;
1047 }
1048
1049 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1050
1051 if (tn) {
1052
1053 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1054
1055 ret_val =
1056 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1057 inode_chunk);
1058
1059 /* Delete the entry in the filestructure (if found) */
1060 if (ret_val != -1)
1061 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1062 }
1063
1064 return ret_val;
1065}
1066
1067int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1068 int nand_chunk, int in_scan)
1069{
1070 /* NB in_scan is zero unless scanning.
1071 * For forward scanning, in_scan is > 0;
1072 * for backward scanning in_scan is < 0
1073 *
1074 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1075 */
1076
1077 struct yaffs_tnode *tn;
1078 struct yaffs_dev *dev = in->my_dev;
1079 int existing_cunk;
1080 struct yaffs_ext_tags existing_tags;
1081 struct yaffs_ext_tags new_tags;
1082 unsigned existing_serial, new_serial;
1083
1084 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1085 /* Just ignore an attempt at putting a chunk into a non-file during scanning
1086 * If it is not during Scanning then something went wrong!
1087 */
1088 if (!in_scan) {
1089 yaffs_trace(YAFFS_TRACE_ERROR,
1090 "yaffs tragedy:attempt to put data chunk into a non-file"
1091 );
1092 YBUG();
1093 }
1094
1095 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1096 return YAFFS_OK;
1097 }
1098
1099 tn = yaffs_add_find_tnode_0(dev,
1100 &in->variant.file_variant,
1101 inode_chunk, NULL);
1102 if (!tn)
1103 return YAFFS_FAIL;
1104
1105 if (!nand_chunk)
1106 /* Dummy insert, bail now */
1107 return YAFFS_OK;
1108
1109 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1110
1111 if (in_scan != 0) {
1112 /* If we're scanning then we need to test for duplicates
1113 * NB This does not need to be efficient since it should only ever
1114 * happen when the power fails during a write, then only one
1115 * chunk should ever be affected.
1116 *
1117 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
1118 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
1119 */
1120
1121 if (existing_cunk > 0) {
1122 /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
1123 * thus we have to do a FindChunkInFile to get the real chunk id.
1124 *
1125 * We have a duplicate now we need to decide which one to use:
1126 *
1127 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
1128 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
1129 * YAFFS1: Get both sets of tags and compare serial numbers.
1130 */
1131
1132 if (in_scan > 0) {
1133 /* Only do this for forward scanning */
1134 yaffs_rd_chunk_tags_nand(dev,
1135 nand_chunk,
1136 NULL, &new_tags);
1137
1138 /* Do a proper find */
1139 existing_cunk =
1140 yaffs_find_chunk_in_file(in, inode_chunk,
1141 &existing_tags);
1142 }
1143
1144 if (existing_cunk <= 0) {
1145 /*Hoosterman - how did this happen? */
1146
1147 yaffs_trace(YAFFS_TRACE_ERROR,
1148 "yaffs tragedy: existing chunk < 0 in scan"
1149 );
1150
1151 }
1152
1153 /* NB The deleted flags should be false, otherwise the chunks will
1154 * not be loaded during a scan
1155 */
1156
1157 if (in_scan > 0) {
1158 new_serial = new_tags.serial_number;
1159 existing_serial = existing_tags.serial_number;
1160 }
1161
1162 if ((in_scan > 0) &&
1163 (existing_cunk <= 0 ||
1164 ((existing_serial + 1) & 3) == new_serial)) {
1165 /* Forward scanning.
1166 * Use new
1167 * Delete the old one and drop through to update the tnode
1168 */
1169 yaffs_chunk_del(dev, existing_cunk, 1,
1170 __LINE__);
1171 } else {
1172 /* Backward scanning or we want to use the existing one
1173 * Use existing.
1174 * Delete the new one and return early so that the tnode isn't changed
1175 */
1176 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1177 return YAFFS_OK;
1178 }
1179 }
1180
1181 }
1182
1183 if (existing_cunk == 0)
1184 in->n_data_chunks++;
1185
1186 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1187
1188 return YAFFS_OK;
1189}
1190
1191static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1192{
1193 struct yaffs_block_info *the_block;
1194 unsigned block_no;
1195
1196 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1197
1198 block_no = chunk / dev->param.chunks_per_block;
1199 the_block = yaffs_get_block_info(dev, block_no);
1200 if (the_block) {
1201 the_block->soft_del_pages++;
1202 dev->n_free_chunks++;
1203 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1204 }
1205}
1206
1207/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1208 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1209 * of the tnode.
1210 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1211 */
1212
1213static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1214 u32 level, int chunk_offset)
1215{
1216 int i;
1217 int the_chunk;
1218 int all_done = 1;
1219 struct yaffs_dev *dev = in->my_dev;
1220
1221 if (tn) {
1222 if (level > 0) {
1223
1224 for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
1225 i--) {
1226 if (tn->internal[i]) {
1227 all_done =
1228 yaffs_soft_del_worker(in,
1229 tn->internal
1230 [i],
1231 level - 1,
1232 (chunk_offset
1233 <<
1234 YAFFS_TNODES_INTERNAL_BITS)
1235 + i);
1236 if (all_done) {
1237 yaffs_free_tnode(dev,
1238 tn->internal
1239 [i]);
1240 tn->internal[i] = NULL;
1241 } else {
1242 /* Hoosterman... how could this happen? */
1243 }
1244 }
1245 }
1246 return (all_done) ? 1 : 0;
1247 } else if (level == 0) {
1248
1249 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1250 the_chunk = yaffs_get_group_base(dev, tn, i);
1251 if (the_chunk) {
1252 /* Note this does not find the real chunk, only the chunk group.
1253 * We make an assumption that a chunk group is not larger than
1254 * a block.
1255 */
1256 yaffs_soft_del_chunk(dev, the_chunk);
1257 yaffs_load_tnode_0(dev, tn, i, 0);
1258 }
1259
1260 }
1261 return 1;
1262
1263 }
1264
1265 }
1266
1267 return 1;
1268
1269}
1270
1271static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1272{
1273 struct yaffs_dev *dev = obj->my_dev;
1274 struct yaffs_obj *parent;
1275
1276 yaffs_verify_obj_in_dir(obj);
1277 parent = obj->parent;
1278
1279 yaffs_verify_dir(parent);
1280
1281 if (dev && dev->param.remove_obj_fn)
1282 dev->param.remove_obj_fn(obj);
1283
1284 list_del_init(&obj->siblings);
1285 obj->parent = NULL;
1286
1287 yaffs_verify_dir(parent);
1288}
1289
1290void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1291{
1292 if (!directory) {
1293 yaffs_trace(YAFFS_TRACE_ALWAYS,
1294 "tragedy: Trying to add an object to a null pointer directory"
1295 );
1296 YBUG();
1297 return;
1298 }
1299 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1300 yaffs_trace(YAFFS_TRACE_ALWAYS,
1301 "tragedy: Trying to add an object to a non-directory"
1302 );
1303 YBUG();
1304 }
1305
1306 if (obj->siblings.prev == NULL) {
1307 /* Not initialised */
1308 YBUG();
1309 }
1310
1311 yaffs_verify_dir(directory);
1312
1313 yaffs_remove_obj_from_dir(obj);
1314
1315 /* Now add it */
1316 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1317 obj->parent = directory;
1318
1319 if (directory == obj->my_dev->unlinked_dir
1320 || directory == obj->my_dev->del_dir) {
1321 obj->unlinked = 1;
1322 obj->my_dev->n_unlinked_files++;
1323 obj->rename_allowed = 0;
1324 }
1325
1326 yaffs_verify_dir(directory);
1327 yaffs_verify_obj_in_dir(obj);
1328}
1329
1330static int yaffs_change_obj_name(struct yaffs_obj *obj,
1331 struct yaffs_obj *new_dir,
1332 const YCHAR * new_name, int force, int shadows)
1333{
1334 int unlink_op;
1335 int del_op;
1336
1337 struct yaffs_obj *existing_target;
1338
1339 if (new_dir == NULL)
1340 new_dir = obj->parent; /* use the old directory */
1341
1342 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1343 yaffs_trace(YAFFS_TRACE_ALWAYS,
1344 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1345 );
1346 YBUG();
1347 }
1348
1349 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1350 if (obj->my_dev->param.is_yaffs2)
1351 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1352 else
1353 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1354 && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1355
1356 del_op = (new_dir == obj->my_dev->del_dir);
1357
1358 existing_target = yaffs_find_by_name(new_dir, new_name);
1359
1360 /* If the object is a file going into the unlinked directory,
1361 * then it is OK to just stuff it in since duplicate names are allowed.
1362 * else only proceed if the new name does not exist and if we're putting
1363 * it into a directory.
1364 */
1365 if ((unlink_op ||
1366 del_op ||
1367 force ||
1368 (shadows > 0) ||
1369 !existing_target) &&
1370 new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1371 yaffs_set_obj_name(obj, new_name);
1372 obj->dirty = 1;
1373
1374 yaffs_add_obj_to_dir(new_dir, obj);
1375
1376 if (unlink_op)
1377 obj->unlinked = 1;
1378
1379 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1380 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
1381 0)
1382 return YAFFS_OK;
1383 }
1384
1385 return YAFFS_FAIL;
1386}
1387
1388/*------------------------ Short Operations Cache ----------------------------------------
1389 * In many situations where there is no high level buffering a lot of
1390 * reads might be short sequential reads, and a lot of writes may be short
1391 * sequential writes. eg. scanning/writing a jpeg file.
1392 * In these cases, a short read/write cache can provide a huge perfomance
1393 * benefit with dumb-as-a-rock code.
1394 * In Linux, the page cache provides read buffering and the short op cache
1395 * provides write buffering.
1396 *
1397 * There are a limited number (~10) of cache chunks per device so that we don't
1398 * need a very intelligent search.
1399 */
1400
1401static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1402{
1403 struct yaffs_dev *dev = obj->my_dev;
1404 int i;
1405 struct yaffs_cache *cache;
1406 int n_caches = obj->my_dev->param.n_caches;
1407
1408 for (i = 0; i < n_caches; i++) {
1409 cache = &dev->cache[i];
1410 if (cache->object == obj && cache->dirty)
1411 return 1;
1412 }
1413
1414 return 0;
1415}
1416
1417static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1418{
1419 struct yaffs_dev *dev = obj->my_dev;
1420 int lowest = -99; /* Stop compiler whining. */
1421 int i;
1422 struct yaffs_cache *cache;
1423 int chunk_written = 0;
1424 int n_caches = obj->my_dev->param.n_caches;
1425
1426 if (n_caches > 0) {
1427 do {
1428 cache = NULL;
1429
1430 /* Find the dirty cache for this object with the lowest chunk id. */
1431 for (i = 0; i < n_caches; i++) {
1432 if (dev->cache[i].object == obj &&
1433 dev->cache[i].dirty) {
1434 if (!cache
1435 || dev->cache[i].chunk_id <
1436 lowest) {
1437 cache = &dev->cache[i];
1438 lowest = cache->chunk_id;
1439 }
1440 }
1441 }
1442
1443 if (cache && !cache->locked) {
1444 /* Write it out and free it up */
1445
1446 chunk_written =
1447 yaffs_wr_data_obj(cache->object,
1448 cache->chunk_id,
1449 cache->data,
1450 cache->n_bytes, 1);
1451 cache->dirty = 0;
1452 cache->object = NULL;
1453 }
1454
1455 } while (cache && chunk_written > 0);
1456
1457 if (cache)
1458 /* Hoosterman, disk full while writing cache out. */
1459 yaffs_trace(YAFFS_TRACE_ERROR,
1460 "yaffs tragedy: no space during cache write");
1461
1462 }
1463
1464}
1465
1466/*yaffs_flush_whole_cache(dev)
1467 *
1468 *
1469 */
1470
1471void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1472{
1473 struct yaffs_obj *obj;
1474 int n_caches = dev->param.n_caches;
1475 int i;
1476
1477 /* Find a dirty object in the cache and flush it...
1478 * until there are no further dirty objects.
1479 */
1480 do {
1481 obj = NULL;
1482 for (i = 0; i < n_caches && !obj; i++) {
1483 if (dev->cache[i].object && dev->cache[i].dirty)
1484 obj = dev->cache[i].object;
1485
1486 }
1487 if (obj)
1488 yaffs_flush_file_cache(obj);
1489
1490 } while (obj);
1491
1492}
1493
1494/* Grab us a cache chunk for use.
1495 * First look for an empty one.
1496 * Then look for the least recently used non-dirty one.
1497 * Then look for the least recently used dirty one...., flush and look again.
1498 */
1499static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1500{
1501 int i;
1502
1503 if (dev->param.n_caches > 0) {
1504 for (i = 0; i < dev->param.n_caches; i++) {
1505 if (!dev->cache[i].object)
1506 return &dev->cache[i];
1507 }
1508 }
1509
1510 return NULL;
1511}
1512
1513static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1514{
1515 struct yaffs_cache *cache;
1516 struct yaffs_obj *the_obj;
1517 int usage;
1518 int i;
1519 int pushout;
1520
1521 if (dev->param.n_caches > 0) {
1522 /* Try find a non-dirty one... */
1523
1524 cache = yaffs_grab_chunk_worker(dev);
1525
1526 if (!cache) {
1527 /* They were all dirty, find the last recently used object and flush
1528 * its cache, then find again.
1529 * NB what's here is not very accurate, we actually flush the object
1530 * the last recently used page.
1531 */
1532
1533 /* With locking we can't assume we can use entry zero */
1534
1535 the_obj = NULL;
1536 usage = -1;
1537 cache = NULL;
1538 pushout = -1;
1539
1540 for (i = 0; i < dev->param.n_caches; i++) {
1541 if (dev->cache[i].object &&
1542 !dev->cache[i].locked &&
1543 (dev->cache[i].last_use < usage
1544 || !cache)) {
1545 usage = dev->cache[i].last_use;
1546 the_obj = dev->cache[i].object;
1547 cache = &dev->cache[i];
1548 pushout = i;
1549 }
1550 }
1551
1552 if (!cache || cache->dirty) {
1553 /* Flush and try again */
1554 yaffs_flush_file_cache(the_obj);
1555 cache = yaffs_grab_chunk_worker(dev);
1556 }
1557
1558 }
1559 return cache;
1560 } else {
1561 return NULL;
1562 }
1563}
1564
1565/* Find a cached chunk */
1566static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1567 int chunk_id)
1568{
1569 struct yaffs_dev *dev = obj->my_dev;
1570 int i;
1571 if (dev->param.n_caches > 0) {
1572 for (i = 0; i < dev->param.n_caches; i++) {
1573 if (dev->cache[i].object == obj &&
1574 dev->cache[i].chunk_id == chunk_id) {
1575 dev->cache_hits++;
1576
1577 return &dev->cache[i];
1578 }
1579 }
1580 }
1581 return NULL;
1582}
1583
1584/* Mark the chunk for the least recently used algorithym */
1585static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1586 int is_write)
1587{
1588
1589 if (dev->param.n_caches > 0) {
1590 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
1591 /* Reset the cache usages */
1592 int i;
1593 for (i = 1; i < dev->param.n_caches; i++)
1594 dev->cache[i].last_use = 0;
1595
1596 dev->cache_last_use = 0;
1597 }
1598
1599 dev->cache_last_use++;
1600
1601 cache->last_use = dev->cache_last_use;
1602
1603 if (is_write)
1604 cache->dirty = 1;
1605 }
1606}
1607
1608/* Invalidate a single cache page.
1609 * Do this when a whole page gets written,
1610 * ie the short cache for this page is no longer valid.
1611 */
1612static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1613{
1614 if (object->my_dev->param.n_caches > 0) {
1615 struct yaffs_cache *cache =
1616 yaffs_find_chunk_cache(object, chunk_id);
1617
1618 if (cache)
1619 cache->object = NULL;
1620 }
1621}
1622
1623/* Invalidate all the cache pages associated with this object
1624 * Do this whenever ther file is deleted or resized.
1625 */
1626static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1627{
1628 int i;
1629 struct yaffs_dev *dev = in->my_dev;
1630
1631 if (dev->param.n_caches > 0) {
1632 /* Invalidate it. */
1633 for (i = 0; i < dev->param.n_caches; i++) {
1634 if (dev->cache[i].object == in)
1635 dev->cache[i].object = NULL;
1636 }
1637 }
1638}
1639
1640static void yaffs_unhash_obj(struct yaffs_obj *obj)
1641{
1642 int bucket;
1643 struct yaffs_dev *dev = obj->my_dev;
1644
1645 /* If it is still linked into the bucket list, free from the list */
1646 if (!list_empty(&obj->hash_link)) {
1647 list_del_init(&obj->hash_link);
1648 bucket = yaffs_hash_fn(obj->obj_id);
1649 dev->obj_bucket[bucket].count--;
1650 }
1651}
1652
1653/* FreeObject frees up a Object and puts it back on the free list */
1654static void yaffs_free_obj(struct yaffs_obj *obj)
1655{
1656 struct yaffs_dev *dev = obj->my_dev;
1657
1658 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1659 obj, obj->my_inode);
1660
1661 if (!obj)
1662 YBUG();
1663 if (obj->parent)
1664 YBUG();
1665 if (!list_empty(&obj->siblings))
1666 YBUG();
1667
1668 if (obj->my_inode) {
1669 /* We're still hooked up to a cached inode.
1670 * Don't delete now, but mark for later deletion
1671 */
1672 obj->defered_free = 1;
1673 return;
1674 }
1675
1676 yaffs_unhash_obj(obj);
1677
1678 yaffs_free_raw_obj(dev, obj);
1679 dev->n_obj--;
1680 dev->checkpoint_blocks_required = 0; /* force recalculation */
1681}
1682
1683void yaffs_handle_defered_free(struct yaffs_obj *obj)
1684{
1685 if (obj->defered_free)
1686 yaffs_free_obj(obj);
1687}
1688
1689static int yaffs_generic_obj_del(struct yaffs_obj *in)
1690{
1691
1692 /* First off, invalidate the file's data in the cache, without flushing. */
1693 yaffs_invalidate_whole_cache(in);
1694
1695 if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
1696 /* Move to the unlinked directory so we have a record that it was deleted. */
1697 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1698 0);
1699
1700 }
1701
1702 yaffs_remove_obj_from_dir(in);
1703 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1704 in->hdr_chunk = 0;
1705
1706 yaffs_free_obj(in);
1707 return YAFFS_OK;
1708
1709}
1710
1711static void yaffs_soft_del_file(struct yaffs_obj *obj)
1712{
1713 if (obj->deleted &&
1714 obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1715 if (obj->n_data_chunks <= 0) {
1716 /* Empty file with no duplicate object headers,
1717 * just delete it immediately */
1718 yaffs_free_tnode(obj->my_dev,
1719 obj->variant.file_variant.top);
1720 obj->variant.file_variant.top = NULL;
1721 yaffs_trace(YAFFS_TRACE_TRACING,
1722 "yaffs: Deleting empty file %d",
1723 obj->obj_id);
1724 yaffs_generic_obj_del(obj);
1725 } else {
1726 yaffs_soft_del_worker(obj,
1727 obj->variant.file_variant.top,
1728 obj->variant.
1729 file_variant.top_level, 0);
1730 obj->soft_del = 1;
1731 }
1732 }
1733}
1734
1735/* Pruning removes any part of the file structure tree that is beyond the
1736 * bounds of the file (ie that does not point to chunks).
1737 *
1738 * A file should only get pruned when its size is reduced.
1739 *
1740 * Before pruning, the chunks must be pulled from the tree and the
1741 * level 0 tnode entries must be zeroed out.
1742 * Could also use this for file deletion, but that's probably better handled
1743 * by a special case.
1744 *
1745 * This function is recursive. For levels > 0 the function is called again on
1746 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1747 * If there is no data in a subtree then it is pruned.
1748 */
1749
1750static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1751 struct yaffs_tnode *tn, u32 level,
1752 int del0)
1753{
1754 int i;
1755 int has_data;
1756
1757 if (tn) {
1758 has_data = 0;
1759
1760 if (level > 0) {
1761 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1762 if (tn->internal[i]) {
1763 tn->internal[i] =
1764 yaffs_prune_worker(dev,
1765 tn->internal[i],
1766 level - 1,
1767 (i ==
1768 0) ? del0 : 1);
1769 }
1770
1771 if (tn->internal[i])
1772 has_data++;
1773 }
1774 } else {
1775 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1776 u32 *map = (u32 *) tn;
1777
1778 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1779 if (map[i])
1780 has_data++;
1781 }
1782 }
1783
1784 if (has_data == 0 && del0) {
1785 /* Free and return NULL */
1786
1787 yaffs_free_tnode(dev, tn);
1788 tn = NULL;
1789 }
1790
1791 }
1792
1793 return tn;
1794
1795}
1796
1797static int yaffs_prune_tree(struct yaffs_dev *dev,
1798 struct yaffs_file_var *file_struct)
1799{
1800 int i;
1801 int has_data;
1802 int done = 0;
1803 struct yaffs_tnode *tn;
1804
1805 if (file_struct->top_level > 0) {
1806 file_struct->top =
1807 yaffs_prune_worker(dev, file_struct->top,
1808 file_struct->top_level, 0);
1809
1810 /* Now we have a tree with all the non-zero branches NULL but the height
1811 * is the same as it was.
1812 * Let's see if we can trim internal tnodes to shorten the tree.
1813 * We can do this if only the 0th element in the tnode is in use
1814 * (ie all the non-zero are NULL)
1815 */
1816
1817 while (file_struct->top_level && !done) {
1818 tn = file_struct->top;
1819
1820 has_data = 0;
1821 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1822 if (tn->internal[i])
1823 has_data++;
1824 }
1825
1826 if (!has_data) {
1827 file_struct->top = tn->internal[0];
1828 file_struct->top_level--;
1829 yaffs_free_tnode(dev, tn);
1830 } else {
1831 done = 1;
1832 }
1833 }
1834 }
1835
1836 return YAFFS_OK;
1837}
1838
1839/*-------------------- End of File Structure functions.-------------------*/
1840
1841/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1842static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1843{
1844 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1845
1846 if (obj) {
1847 dev->n_obj++;
1848
1849 /* Now sweeten it up... */
1850
1851 memset(obj, 0, sizeof(struct yaffs_obj));
1852 obj->being_created = 1;
1853
1854 obj->my_dev = dev;
1855 obj->hdr_chunk = 0;
1856 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1857 INIT_LIST_HEAD(&(obj->hard_links));
1858 INIT_LIST_HEAD(&(obj->hash_link));
1859 INIT_LIST_HEAD(&obj->siblings);
1860
1861 /* Now make the directory sane */
1862 if (dev->root_dir) {
1863 obj->parent = dev->root_dir;
1864 list_add(&(obj->siblings),
1865 &dev->root_dir->variant.dir_variant.children);
1866 }
1867
1868 /* Add it to the lost and found directory.
1869 * NB Can't put root or lost-n-found in lost-n-found so
1870 * check if lost-n-found exists first
1871 */
1872 if (dev->lost_n_found)
1873 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1874
1875 obj->being_created = 0;
1876 }
1877
1878 dev->checkpoint_blocks_required = 0; /* force recalculation */
1879
1880 return obj;
1881}
1882
1883static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1884{
1885 int i;
1886 int l = 999;
1887 int lowest = 999999;
1888
1889 /* Search for the shortest list or one that
1890 * isn't too long.
1891 */
1892
1893 for (i = 0; i < 10 && lowest > 4; i++) {
1894 dev->bucket_finder++;
1895 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1896 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1897 lowest = dev->obj_bucket[dev->bucket_finder].count;
1898 l = dev->bucket_finder;
1899 }
1900
1901 }
1902
1903 return l;
1904}
1905
1906static int yaffs_new_obj_id(struct yaffs_dev *dev)
1907{
1908 int bucket = yaffs_find_nice_bucket(dev);
1909
1910 /* Now find an object value that has not already been taken
1911 * by scanning the list.
1912 */
1913
1914 int found = 0;
1915 struct list_head *i;
1916
1917 u32 n = (u32) bucket;
1918
1919 /* yaffs_check_obj_hash_sane(); */
1920
1921 while (!found) {
1922 found = 1;
1923 n += YAFFS_NOBJECT_BUCKETS;
1924 if (1 || dev->obj_bucket[bucket].count > 0) {
1925 list_for_each(i, &dev->obj_bucket[bucket].list) {
1926 /* If there is already one in the list */
1927 if (i && list_entry(i, struct yaffs_obj,
1928 hash_link)->obj_id == n) {
1929 found = 0;
1930 }
1931 }
1932 }
1933 }
1934
1935 return n;
1936}
1937
1938static void yaffs_hash_obj(struct yaffs_obj *in)
1939{
1940 int bucket = yaffs_hash_fn(in->obj_id);
1941 struct yaffs_dev *dev = in->my_dev;
1942
1943 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1944 dev->obj_bucket[bucket].count++;
1945}
1946
1947struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1948{
1949 int bucket = yaffs_hash_fn(number);
1950 struct list_head *i;
1951 struct yaffs_obj *in;
1952
1953 list_for_each(i, &dev->obj_bucket[bucket].list) {
1954 /* Look if it is in the list */
1955 if (i) {
1956 in = list_entry(i, struct yaffs_obj, hash_link);
1957 if (in->obj_id == number) {
1958
1959 /* Don't tell the VFS about this one if it is defered free */
1960 if (in->defered_free)
1961 return NULL;
1962
1963 return in;
1964 }
1965 }
1966 }
1967
1968 return NULL;
1969}
1970
1971struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1972 enum yaffs_obj_type type)
1973{
1974 struct yaffs_obj *the_obj = NULL;
1975 struct yaffs_tnode *tn = NULL;
1976
1977 if (number < 0)
1978 number = yaffs_new_obj_id(dev);
1979
1980 if (type == YAFFS_OBJECT_TYPE_FILE) {
1981 tn = yaffs_get_tnode(dev);
1982 if (!tn)
1983 return NULL;
1984 }
1985
1986 the_obj = yaffs_alloc_empty_obj(dev);
1987 if (!the_obj) {
1988 if (tn)
1989 yaffs_free_tnode(dev, tn);
1990 return NULL;
1991 }
1992
1993 if (the_obj) {
1994 the_obj->fake = 0;
1995 the_obj->rename_allowed = 1;
1996 the_obj->unlink_allowed = 1;
1997 the_obj->obj_id = number;
1998 yaffs_hash_obj(the_obj);
1999 the_obj->variant_type = type;
2000 yaffs_load_current_time(the_obj, 1, 1);
2001
2002 switch (type) {
2003 case YAFFS_OBJECT_TYPE_FILE:
2004 the_obj->variant.file_variant.file_size = 0;
2005 the_obj->variant.file_variant.scanned_size = 0;
2006 the_obj->variant.file_variant.shrink_size = ~0; /* max */
2007 the_obj->variant.file_variant.top_level = 0;
2008 the_obj->variant.file_variant.top = tn;
2009 break;
2010 case YAFFS_OBJECT_TYPE_DIRECTORY:
2011 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2012 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2013 break;
2014 case YAFFS_OBJECT_TYPE_SYMLINK:
2015 case YAFFS_OBJECT_TYPE_HARDLINK:
2016 case YAFFS_OBJECT_TYPE_SPECIAL:
2017 /* No action required */
2018 break;
2019 case YAFFS_OBJECT_TYPE_UNKNOWN:
2020 /* todo this should not happen */
2021 break;
2022 }
2023 }
2024
2025 return the_obj;
2026}
2027
2028static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2029 int number, u32 mode)
2030{
2031
2032 struct yaffs_obj *obj =
2033 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2034 if (obj) {
2035 obj->fake = 1; /* it is fake so it might have no NAND presence... */
2036 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
2037 obj->unlink_allowed = 0; /* ... or unlink it */
2038 obj->deleted = 0;
2039 obj->unlinked = 0;
2040 obj->yst_mode = mode;
2041 obj->my_dev = dev;
2042 obj->hdr_chunk = 0; /* Not a valid chunk. */
2043 }
2044
2045 return obj;
2046
2047}
2048
2049
2050static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2051{
2052 int i;
2053
2054 dev->n_obj = 0;
2055 dev->n_tnodes = 0;
2056
2057 yaffs_init_raw_tnodes_and_objs(dev);
2058
2059 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2060 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2061 dev->obj_bucket[i].count = 0;
2062 }
2063}
2064
2065struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2066 int number,
2067 enum yaffs_obj_type type)
2068{
2069 struct yaffs_obj *the_obj = NULL;
2070
2071 if (number > 0)
2072 the_obj = yaffs_find_by_number(dev, number);
2073
2074 if (!the_obj)
2075 the_obj = yaffs_new_obj(dev, number, type);
2076
2077 return the_obj;
2078
2079}
2080
2081YCHAR *yaffs_clone_str(const YCHAR * str)
2082{
2083 YCHAR *new_str = NULL;
2084 int len;
2085
2086 if (!str)
2087 str = _Y("");
2088
2089 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2090 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2091 if (new_str) {
2092 strncpy(new_str, str, len);
2093 new_str[len] = 0;
2094 }
2095 return new_str;
2096
2097}
2098/*
2099 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2100 * link (ie. name) is created or deleted in the directory.
2101 *
2102 * ie.
2103 * create dir/a : update dir's mtime/ctime
2104 * rm dir/a: update dir's mtime/ctime
2105 * modify dir/a: don't update dir's mtimme/ctime
2106 *
2107 * This can be handled immediately or defered. Defering helps reduce the number
2108 * of updates when many files in a directory are changed within a brief period.
2109 *
2110 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2111 * called periodically.
2112 */
2113
2114static void yaffs_update_parent(struct yaffs_obj *obj)
2115{
2116 struct yaffs_dev *dev;
2117 if (!obj)
2118 return;
2119 dev = obj->my_dev;
2120 obj->dirty = 1;
2121 yaffs_load_current_time(obj, 0, 1);
2122 if (dev->param.defered_dir_update) {
2123 struct list_head *link = &obj->variant.dir_variant.dirty;
2124
2125 if (list_empty(link)) {
2126 list_add(link, &dev->dirty_dirs);
2127 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2128 "Added object %d to dirty directories",
2129 obj->obj_id);
2130 }
2131
2132 } else {
2133 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2134 }
2135}
2136
2137void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2138{
2139 struct list_head *link;
2140 struct yaffs_obj *obj;
2141 struct yaffs_dir_var *d_s;
2142 union yaffs_obj_var *o_v;
2143
2144 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2145
2146 while (!list_empty(&dev->dirty_dirs)) {
2147 link = dev->dirty_dirs.next;
2148 list_del_init(link);
2149
2150 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2151 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2152 obj = list_entry(o_v, struct yaffs_obj, variant);
2153
2154 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2155 obj->obj_id);
2156
2157 if (obj->dirty)
2158 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2159 }
2160}
2161
2162/*
2163 * Mknod (create) a new object.
2164 * equiv_obj only has meaning for a hard link;
2165 * alias_str only has meaning for a symlink.
2166 * rdev only has meaning for devices (a subset of special objects)
2167 */
2168
2169static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2170 struct yaffs_obj *parent,
2171 const YCHAR * name,
2172 u32 mode,
2173 u32 uid,
2174 u32 gid,
2175 struct yaffs_obj *equiv_obj,
2176 const YCHAR * alias_str, u32 rdev)
2177{
2178 struct yaffs_obj *in;
2179 YCHAR *str = NULL;
2180
2181 struct yaffs_dev *dev = parent->my_dev;
2182
2183 /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
2184 if (yaffs_find_by_name(parent, name))
2185 return NULL;
2186
2187 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2188 str = yaffs_clone_str(alias_str);
2189 if (!str)
2190 return NULL;
2191 }
2192
2193 in = yaffs_new_obj(dev, -1, type);
2194
2195 if (!in) {
2196 if (str)
2197 kfree(str);
2198 return NULL;
2199 }
2200
2201 if (in) {
2202 in->hdr_chunk = 0;
2203 in->valid = 1;
2204 in->variant_type = type;
2205
2206 in->yst_mode = mode;
2207
2208 yaffs_attribs_init(in, gid, uid, rdev);
2209
2210 in->n_data_chunks = 0;
2211
2212 yaffs_set_obj_name(in, name);
2213 in->dirty = 1;
2214
2215 yaffs_add_obj_to_dir(parent, in);
2216
2217 in->my_dev = parent->my_dev;
2218
2219 switch (type) {
2220 case YAFFS_OBJECT_TYPE_SYMLINK:
2221 in->variant.symlink_variant.alias = str;
2222 break;
2223 case YAFFS_OBJECT_TYPE_HARDLINK:
2224 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2225 in->variant.hardlink_variant.equiv_id =
2226 equiv_obj->obj_id;
2227 list_add(&in->hard_links, &equiv_obj->hard_links);
2228 break;
2229 case YAFFS_OBJECT_TYPE_FILE:
2230 case YAFFS_OBJECT_TYPE_DIRECTORY:
2231 case YAFFS_OBJECT_TYPE_SPECIAL:
2232 case YAFFS_OBJECT_TYPE_UNKNOWN:
2233 /* do nothing */
2234 break;
2235 }
2236
2237 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2238 /* Could not create the object header, fail the creation */
2239 yaffs_del_obj(in);
2240 in = NULL;
2241 }
2242
2243 yaffs_update_parent(parent);
2244 }
2245
2246 return in;
2247}
2248
2249struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2250 const YCHAR * name, u32 mode, u32 uid,
2251 u32 gid)
2252{
2253 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2254 uid, gid, NULL, NULL, 0);
2255}
2256
2257struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
2258 u32 mode, u32 uid, u32 gid)
2259{
2260 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2261 mode, uid, gid, NULL, NULL, 0);
2262}
2263
2264struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2265 const YCHAR * name, u32 mode, u32 uid,
2266 u32 gid, u32 rdev)
2267{
2268 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2269 uid, gid, NULL, NULL, rdev);
2270}
2271
2272struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2273 const YCHAR * name, u32 mode, u32 uid,
2274 u32 gid, const YCHAR * alias)
2275{
2276 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2277 uid, gid, NULL, alias, 0);
2278}
2279
2280/* yaffs_link_obj returns the object id of the equivalent object.*/
2281struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2282 struct yaffs_obj *equiv_obj)
2283{
2284 /* Get the real object in case we were fed a hard link as an equivalent object */
2285 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2286
2287 if (yaffs_create_obj
2288 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2289 equiv_obj, NULL, 0)) {
2290 return equiv_obj;
2291 } else {
2292 return NULL;
2293 }
2294
2295}
2296
2297
2298
2299/*------------------------- Block Management and Page Allocation ----------------*/
2300
2301static int yaffs_init_blocks(struct yaffs_dev *dev)
2302{
2303 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2304
2305 dev->block_info = NULL;
2306 dev->chunk_bits = NULL;
2307
2308 dev->alloc_block = -1; /* force it to get a new one */
2309
2310 /* If the first allocation strategy fails, thry the alternate one */
2311 dev->block_info =
2312 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2313 if (!dev->block_info) {
2314 dev->block_info =
2315 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2316 dev->block_info_alt = 1;
2317 } else {
2318 dev->block_info_alt = 0;
2319 }
2320
2321 if (dev->block_info) {
2322 /* Set up dynamic blockinfo stuff. Round up bytes. */
2323 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2324 dev->chunk_bits =
2325 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2326 if (!dev->chunk_bits) {
2327 dev->chunk_bits =
2328 vmalloc(dev->chunk_bit_stride * n_blocks);
2329 dev->chunk_bits_alt = 1;
2330 } else {
2331 dev->chunk_bits_alt = 0;
2332 }
2333 }
2334
2335 if (dev->block_info && dev->chunk_bits) {
2336 memset(dev->block_info, 0,
2337 n_blocks * sizeof(struct yaffs_block_info));
2338 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2339 return YAFFS_OK;
2340 }
2341
2342 return YAFFS_FAIL;
2343}
2344
2345static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2346{
2347 if (dev->block_info_alt && dev->block_info)
2348 vfree(dev->block_info);
2349 else if (dev->block_info)
2350 kfree(dev->block_info);
2351
2352 dev->block_info_alt = 0;
2353
2354 dev->block_info = NULL;
2355
2356 if (dev->chunk_bits_alt && dev->chunk_bits)
2357 vfree(dev->chunk_bits);
2358 else if (dev->chunk_bits)
2359 kfree(dev->chunk_bits);
2360 dev->chunk_bits_alt = 0;
2361 dev->chunk_bits = NULL;
2362}
2363
2364void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2365{
2366 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2367
2368 int erased_ok = 0;
2369
2370 /* If the block is still healthy erase it and mark as clean.
2371 * If the block has had a data failure, then retire it.
2372 */
2373
2374 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2375 "yaffs_block_became_dirty block %d state %d %s",
2376 block_no, bi->block_state,
2377 (bi->needs_retiring) ? "needs retiring" : "");
2378
2379 yaffs2_clear_oldest_dirty_seq(dev, bi);
2380
2381 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2382
2383 /* If this is the block being garbage collected then stop gc'ing this block */
2384 if (block_no == dev->gc_block)
2385 dev->gc_block = 0;
2386
2387 /* If this block is currently the best candidate for gc then drop as a candidate */
2388 if (block_no == dev->gc_dirtiest) {
2389 dev->gc_dirtiest = 0;
2390 dev->gc_pages_in_use = 0;
2391 }
2392
2393 if (!bi->needs_retiring) {
2394 yaffs2_checkpt_invalidate(dev);
2395 erased_ok = yaffs_erase_block(dev, block_no);
2396 if (!erased_ok) {
2397 dev->n_erase_failures++;
2398 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2399 "**>> Erasure failed %d", block_no);
2400 }
2401 }
2402
2403 if (erased_ok &&
2404 ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
2405 || !yaffs_skip_verification(dev))) {
2406 int i;
2407 for (i = 0; i < dev->param.chunks_per_block; i++) {
2408 if (!yaffs_check_chunk_erased
2409 (dev, block_no * dev->param.chunks_per_block + i)) {
2410 yaffs_trace(YAFFS_TRACE_ERROR,
2411 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2412 block_no, i);
2413 }
2414 }
2415 }
2416
2417 if (erased_ok) {
2418 /* Clean it up... */
2419 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2420 bi->seq_number = 0;
2421 dev->n_erased_blocks++;
2422 bi->pages_in_use = 0;
2423 bi->soft_del_pages = 0;
2424 bi->has_shrink_hdr = 0;
2425 bi->skip_erased_check = 1; /* Clean, so no need to check */
2426 bi->gc_prioritise = 0;
2427 yaffs_clear_chunk_bits(dev, block_no);
2428
2429 yaffs_trace(YAFFS_TRACE_ERASE,
2430 "Erased block %d", block_no);
2431 } else {
2432 /* We lost a block of free space */
2433 dev->n_free_chunks -= dev->param.chunks_per_block;
2434 yaffs_retire_block(dev, block_no);
2435 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2436 "**>> Block %d retired", block_no);
2437 }
2438}
2439
2440
2441
2442static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2443{
2444 int old_chunk;
2445 int new_chunk;
2446 int mark_flash;
2447 int ret_val = YAFFS_OK;
2448 int i;
2449 int is_checkpt_block;
2450 int matching_chunk;
2451 int max_copies;
2452
2453 int chunks_before = yaffs_get_erased_chunks(dev);
2454 int chunks_after;
2455
2456 struct yaffs_ext_tags tags;
2457
2458 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2459
2460 struct yaffs_obj *object;
2461
2462 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2463
2464 yaffs_trace(YAFFS_TRACE_TRACING,
2465 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2466 block, bi->pages_in_use, bi->has_shrink_hdr,
2467 whole_block);
2468
2469 /*yaffs_verify_free_chunks(dev); */
2470
2471 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2472 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2473
2474 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2475
2476 dev->gc_disable = 1;
2477
2478 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2479 yaffs_trace(YAFFS_TRACE_TRACING,
2480 "Collecting block %d that has no chunks in use",
2481 block);
2482 yaffs_block_became_dirty(dev, block);
2483 } else {
2484
2485 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2486
2487 yaffs_verify_blk(dev, bi, block);
2488
2489 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2490 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2491
2492 for ( /* init already done */ ;
2493 ret_val == YAFFS_OK &&
2494 dev->gc_chunk < dev->param.chunks_per_block &&
2495 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2496 max_copies > 0; dev->gc_chunk++, old_chunk++) {
2497 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2498
2499 /* This page is in use and might need to be copied off */
2500
2501 max_copies--;
2502
2503 mark_flash = 1;
2504
2505 yaffs_init_tags(&tags);
2506
2507 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2508 buffer, &tags);
2509
2510 object = yaffs_find_by_number(dev, tags.obj_id);
2511
2512 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2513 "Collecting chunk in block %d, %d %d %d ",
2514 dev->gc_chunk, tags.obj_id,
2515 tags.chunk_id, tags.n_bytes);
2516
2517 if (object && !yaffs_skip_verification(dev)) {
2518 if (tags.chunk_id == 0)
2519 matching_chunk =
2520 object->hdr_chunk;
2521 else if (object->soft_del)
2522 matching_chunk = old_chunk; /* Defeat the test */
2523 else
2524 matching_chunk =
2525 yaffs_find_chunk_in_file
2526 (object, tags.chunk_id,
2527 NULL);
2528
2529 if (old_chunk != matching_chunk)
2530 yaffs_trace(YAFFS_TRACE_ERROR,
2531 "gc: page in gc mismatch: %d %d %d %d",
2532 old_chunk,
2533 matching_chunk,
2534 tags.obj_id,
2535 tags.chunk_id);
2536
2537 }
2538
2539 if (!object) {
2540 yaffs_trace(YAFFS_TRACE_ERROR,
2541 "page %d in gc has no object: %d %d %d ",
2542 old_chunk,
2543 tags.obj_id, tags.chunk_id,
2544 tags.n_bytes);
2545 }
2546
2547 if (object &&
2548 object->deleted &&
2549 object->soft_del && tags.chunk_id != 0) {
2550 /* Data chunk in a soft deleted file, throw it away
2551 * It's a soft deleted data chunk,
2552 * No need to copy this, just forget about it and
2553 * fix up the object.
2554 */
2555
2556 /* Free chunks already includes softdeleted chunks.
2557 * How ever this chunk is going to soon be really deleted
2558 * which will increment free chunks.
2559 * We have to decrement free chunks so this works out properly.
2560 */
2561 dev->n_free_chunks--;
2562 bi->soft_del_pages--;
2563
2564 object->n_data_chunks--;
2565
2566 if (object->n_data_chunks <= 0) {
2567 /* remeber to clean up the object */
2568 dev->gc_cleanup_list[dev->
2569 n_clean_ups]
2570 = tags.obj_id;
2571 dev->n_clean_ups++;
2572 }
2573 mark_flash = 0;
2574 } else if (0) {
2575 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2576 /* Deleted object header with no data chunks.
2577 * Can be discarded and the file deleted.
2578 */
2579 object->hdr_chunk = 0;
2580 yaffs_free_tnode(object->my_dev,
2581 object->
2582 variant.file_variant.
2583 top);
2584 object->variant.file_variant.top = NULL;
2585 yaffs_generic_obj_del(object);
2586
2587 } else if (object) {
2588 /* It's either a data chunk in a live file or
2589 * an ObjectHeader, so we're interested in it.
2590 * NB Need to keep the ObjectHeaders of deleted files
2591 * until the whole file has been deleted off
2592 */
2593 tags.serial_number++;
2594
2595 dev->n_gc_copies++;
2596
2597 if (tags.chunk_id == 0) {
2598 /* It is an object Id,
2599 * We need to nuke the shrinkheader flags first
2600 * Also need to clean up shadowing.
2601 * We no longer want the shrink_header flag since its work is done
2602 * and if it is left in place it will mess up scanning.
2603 */
2604
2605 struct yaffs_obj_hdr *oh;
2606 oh = (struct yaffs_obj_hdr *)
2607 buffer;
2608
2609 oh->is_shrink = 0;
2610 tags.extra_is_shrink = 0;
2611
2612 oh->shadows_obj = 0;
2613 oh->inband_shadowed_obj_id = 0;
2614 tags.extra_shadows = 0;
2615
2616 /* Update file size */
2617 if (object->variant_type ==
2618 YAFFS_OBJECT_TYPE_FILE) {
2619 oh->file_size =
2620 object->variant.
2621 file_variant.
2622 file_size;
2623 tags.extra_length =
2624 oh->file_size;
2625 }
2626
2627 yaffs_verify_oh(object, oh,
2628 &tags, 1);
2629 new_chunk =
2630 yaffs_write_new_chunk(dev,
2631 (u8 *)
2632 oh,
2633 &tags,
2634 1);
2635 } else {
2636 new_chunk =
2637 yaffs_write_new_chunk(dev,
2638 buffer,
2639 &tags,
2640 1);
2641 }
2642
2643 if (new_chunk < 0) {
2644 ret_val = YAFFS_FAIL;
2645 } else {
2646
2647 /* Ok, now fix up the Tnodes etc. */
2648
2649 if (tags.chunk_id == 0) {
2650 /* It's a header */
2651 object->hdr_chunk =
2652 new_chunk;
2653 object->serial =
2654 tags.serial_number;
2655 } else {
2656 /* It's a data chunk */
2657 int ok;
2658 ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
2659 }
2660 }
2661 }
2662
2663 if (ret_val == YAFFS_OK)
2664 yaffs_chunk_del(dev, old_chunk,
2665 mark_flash, __LINE__);
2666
2667 }
2668 }
2669
2670 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2671
2672 }
2673
2674 yaffs_verify_collected_blk(dev, bi, block);
2675
2676 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2677 /*
2678 * The gc did not complete. Set block state back to FULL
2679 * because checkpointing does not restore gc.
2680 */
2681 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2682 } else {
2683 /* The gc completed. */
2684 /* Do any required cleanups */
2685 for (i = 0; i < dev->n_clean_ups; i++) {
2686 /* Time to delete the file too */
2687 object =
2688 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2689 if (object) {
2690 yaffs_free_tnode(dev,
2691 object->variant.
2692 file_variant.top);
2693 object->variant.file_variant.top = NULL;
2694 yaffs_trace(YAFFS_TRACE_GC,
2695 "yaffs: About to finally delete object %d",
2696 object->obj_id);
2697 yaffs_generic_obj_del(object);
2698 object->my_dev->n_deleted_files--;
2699 }
2700
2701 }
2702
2703 chunks_after = yaffs_get_erased_chunks(dev);
2704 if (chunks_before >= chunks_after)
2705 yaffs_trace(YAFFS_TRACE_GC,
2706 "gc did not increase free chunks before %d after %d",
2707 chunks_before, chunks_after);
2708 dev->gc_block = 0;
2709 dev->gc_chunk = 0;
2710 dev->n_clean_ups = 0;
2711 }
2712
2713 dev->gc_disable = 0;
2714
2715 return ret_val;
2716}
2717
2718/*
2719 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2720 * for garbage collection.
2721 */
2722
2723static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2724 int aggressive, int background)
2725{
2726 int i;
2727 int iterations;
2728 unsigned selected = 0;
2729 int prioritised = 0;
2730 int prioritised_exist = 0;
2731 struct yaffs_block_info *bi;
2732 int threshold;
2733
2734 /* First let's see if we need to grab a prioritised block */
2735 if (dev->has_pending_prioritised_gc && !aggressive) {
2736 dev->gc_dirtiest = 0;
2737 bi = dev->block_info;
2738 for (i = dev->internal_start_block;
2739 i <= dev->internal_end_block && !selected; i++) {
2740
2741 if (bi->gc_prioritise) {
2742 prioritised_exist = 1;
2743 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2744 yaffs_block_ok_for_gc(dev, bi)) {
2745 selected = i;
2746 prioritised = 1;
2747 }
2748 }
2749 bi++;
2750 }
2751
2752 /*
2753 * If there is a prioritised block and none was selected then
2754 * this happened because there is at least one old dirty block gumming
2755 * up the works. Let's gc the oldest dirty block.
2756 */
2757
2758 if (prioritised_exist &&
2759 !selected && dev->oldest_dirty_block > 0)
2760 selected = dev->oldest_dirty_block;
2761
2762 if (!prioritised_exist) /* None found, so we can clear this */
2763 dev->has_pending_prioritised_gc = 0;
2764 }
2765
2766 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2767 * search harder.
2768 * else (we're doing a leasurely gc), then we only bother to do this if the
2769 * block has only a few pages in use.
2770 */
2771
2772 if (!selected) {
2773 int pages_used;
2774 int n_blocks =
2775 dev->internal_end_block - dev->internal_start_block + 1;
2776 if (aggressive) {
2777 threshold = dev->param.chunks_per_block;
2778 iterations = n_blocks;
2779 } else {
2780 int max_threshold;
2781
2782 if (background)
2783 max_threshold = dev->param.chunks_per_block / 2;
2784 else
2785 max_threshold = dev->param.chunks_per_block / 8;
2786
2787 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2788 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2789
2790 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2791 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2792 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2793 if (threshold > max_threshold)
2794 threshold = max_threshold;
2795
2796 iterations = n_blocks / 16 + 1;
2797 if (iterations > 100)
2798 iterations = 100;
2799 }
2800
2801 for (i = 0;
2802 i < iterations &&
2803 (dev->gc_dirtiest < 1 ||
2804 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
2805 dev->gc_block_finder++;
2806 if (dev->gc_block_finder < dev->internal_start_block ||
2807 dev->gc_block_finder > dev->internal_end_block)
2808 dev->gc_block_finder =
2809 dev->internal_start_block;
2810
2811 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2812
2813 pages_used = bi->pages_in_use - bi->soft_del_pages;
2814
2815 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2816 pages_used < dev->param.chunks_per_block &&
2817 (dev->gc_dirtiest < 1
2818 || pages_used < dev->gc_pages_in_use)
2819 && yaffs_block_ok_for_gc(dev, bi)) {
2820 dev->gc_dirtiest = dev->gc_block_finder;
2821 dev->gc_pages_in_use = pages_used;
2822 }
2823 }
2824
2825 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2826 selected = dev->gc_dirtiest;
2827 }
2828
2829 /*
2830 * If nothing has been selected for a while, try selecting the oldest dirty
2831 * because that's gumming up the works.
2832 */
2833
2834 if (!selected && dev->param.is_yaffs2 &&
2835 dev->gc_not_done >= (background ? 10 : 20)) {
2836 yaffs2_find_oldest_dirty_seq(dev);
2837 if (dev->oldest_dirty_block > 0) {
2838 selected = dev->oldest_dirty_block;
2839 dev->gc_dirtiest = selected;
2840 dev->oldest_dirty_gc_count++;
2841 bi = yaffs_get_block_info(dev, selected);
2842 dev->gc_pages_in_use =
2843 bi->pages_in_use - bi->soft_del_pages;
2844 } else {
2845 dev->gc_not_done = 0;
2846 }
2847 }
2848
2849 if (selected) {
2850 yaffs_trace(YAFFS_TRACE_GC,
2851 "GC Selected block %d with %d free, prioritised:%d",
2852 selected,
2853 dev->param.chunks_per_block - dev->gc_pages_in_use,
2854 prioritised);
2855
2856 dev->n_gc_blocks++;
2857 if (background)
2858 dev->bg_gcs++;
2859
2860 dev->gc_dirtiest = 0;
2861 dev->gc_pages_in_use = 0;
2862 dev->gc_not_done = 0;
2863 if (dev->refresh_skip > 0)
2864 dev->refresh_skip--;
2865 } else {
2866 dev->gc_not_done++;
2867 yaffs_trace(YAFFS_TRACE_GC,
2868 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2869 dev->gc_block_finder, dev->gc_not_done, threshold,
2870 dev->gc_dirtiest, dev->gc_pages_in_use,
2871 dev->oldest_dirty_block, background ? " bg" : "");
2872 }
2873
2874 return selected;
2875}
2876
2877/* New garbage collector
2878 * If we're very low on erased blocks then we do aggressive garbage collection
2879 * otherwise we do "leasurely" garbage collection.
2880 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2881 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2882 *
2883 * The idea is to help clear out space in a more spread-out manner.
2884 * Dunno if it really does anything useful.
2885 */
2886static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2887{
2888 int aggressive = 0;
2889 int gc_ok = YAFFS_OK;
2890 int max_tries = 0;
2891 int min_erased;
2892 int erased_chunks;
2893 int checkpt_block_adjust;
2894
2895 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2896 return YAFFS_OK;
2897
2898 if (dev->gc_disable) {
2899 /* Bail out so we don't get recursive gc */
2900 return YAFFS_OK;
2901 }
2902
2903 /* This loop should pass the first time.
2904 * We'll only see looping here if the collection does not increase space.
2905 */
2906
2907 do {
2908 max_tries++;
2909
2910 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2911
2912 min_erased =
2913 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2914 erased_chunks =
2915 dev->n_erased_blocks * dev->param.chunks_per_block;
2916
2917 /* If we need a block soon then do aggressive gc. */
2918 if (dev->n_erased_blocks < min_erased)
2919 aggressive = 1;
2920 else {
2921 if (!background
2922 && erased_chunks > (dev->n_free_chunks / 4))
2923 break;
2924
2925 if (dev->gc_skip > 20)
2926 dev->gc_skip = 20;
2927 if (erased_chunks < dev->n_free_chunks / 2 ||
2928 dev->gc_skip < 1 || background)
2929 aggressive = 0;
2930 else {
2931 dev->gc_skip--;
2932 break;
2933 }
2934 }
2935
2936 dev->gc_skip = 5;
2937
2938 /* If we don't already have a block being gc'd then see if we should start another */
2939
2940 if (dev->gc_block < 1 && !aggressive) {
2941 dev->gc_block = yaffs2_find_refresh_block(dev);
2942 dev->gc_chunk = 0;
2943 dev->n_clean_ups = 0;
2944 }
2945 if (dev->gc_block < 1) {
2946 dev->gc_block =
2947 yaffs_find_gc_block(dev, aggressive, background);
2948 dev->gc_chunk = 0;
2949 dev->n_clean_ups = 0;
2950 }
2951
2952 if (dev->gc_block > 0) {
2953 dev->all_gcs++;
2954 if (!aggressive)
2955 dev->passive_gc_count++;
2956
2957 yaffs_trace(YAFFS_TRACE_GC,
2958 "yaffs: GC n_erased_blocks %d aggressive %d",
2959 dev->n_erased_blocks, aggressive);
2960
2961 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2962 }
2963
2964 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
2965 && dev->gc_block > 0) {
2966 yaffs_trace(YAFFS_TRACE_GC,
2967 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2968 dev->n_erased_blocks, max_tries,
2969 dev->gc_block);
2970 }
2971 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2972 (dev->gc_block > 0) && (max_tries < 2));
2973
2974 return aggressive ? gc_ok : YAFFS_OK;
2975}
2976
2977/*
2978 * yaffs_bg_gc()
2979 * Garbage collects. Intended to be called from a background thread.
2980 * Returns non-zero if at least half the free chunks are erased.
2981 */
2982int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2983{
2984 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2985
2986 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2987
2988 yaffs_check_gc(dev, 1);
2989 return erased_chunks > dev->n_free_chunks / 2;
2990}
2991
2992/*-------------------- Data file manipulation -----------------*/
2993
2994static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2995{
2996 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2997
2998 if (nand_chunk >= 0)
2999 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
3000 buffer, NULL);
3001 else {
3002 yaffs_trace(YAFFS_TRACE_NANDACCESS,
3003 "Chunk %d not found zero instead",
3004 nand_chunk);
3005 /* get sane (zero) data if you read a hole */
3006 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
3007 return 0;
3008 }
3009
3010}
3011
3012void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
3013 int lyn)
3014{
3015 int block;
3016 int page;
3017 struct yaffs_ext_tags tags;
3018 struct yaffs_block_info *bi;
3019
3020 if (chunk_id <= 0)
3021 return;
3022
3023 dev->n_deletions++;
3024 block = chunk_id / dev->param.chunks_per_block;
3025 page = chunk_id % dev->param.chunks_per_block;
3026
3027 if (!yaffs_check_chunk_bit(dev, block, page))
3028 yaffs_trace(YAFFS_TRACE_VERIFY,
3029 "Deleting invalid chunk %d", chunk_id);
3030
3031 bi = yaffs_get_block_info(dev, block);
3032
3033 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3034
3035 yaffs_trace(YAFFS_TRACE_DELETION,
3036 "line %d delete of chunk %d",
3037 lyn, chunk_id);
3038
3039 if (!dev->param.is_yaffs2 && mark_flash &&
3040 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3041
3042 yaffs_init_tags(&tags);
3043
3044 tags.is_deleted = 1;
3045
3046 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3047 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3048 } else {
3049 dev->n_unmarked_deletions++;
3050 }
3051
3052 /* Pull out of the management area.
3053 * If the whole block became dirty, this will kick off an erasure.
3054 */
3055 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3056 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3057 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3058 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3059 dev->n_free_chunks++;
3060
3061 yaffs_clear_chunk_bit(dev, block, page);
3062
3063 bi->pages_in_use--;
3064
3065 if (bi->pages_in_use == 0 &&
3066 !bi->has_shrink_hdr &&
3067 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3068 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3069 yaffs_block_became_dirty(dev, block);
3070 }
3071
3072 }
3073
3074}
3075
3076static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3077 const u8 * buffer, int n_bytes, int use_reserve)
3078{
3079 /* Find old chunk Need to do this to get serial number
3080 * Write new one and patch into tree.
3081 * Invalidate old tags.
3082 */
3083
3084 int prev_chunk_id;
3085 struct yaffs_ext_tags prev_tags;
3086
3087 int new_chunk_id;
3088 struct yaffs_ext_tags new_tags;
3089
3090 struct yaffs_dev *dev = in->my_dev;
3091
3092 yaffs_check_gc(dev, 0);
3093
3094 /* Get the previous chunk at this location in the file if it exists.
3095 * If it does not exist then put a zero into the tree. This creates
3096 * the tnode now, rather than later when it is harder to clean up.
3097 */
3098 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3099 if (prev_chunk_id < 1 &&
3100 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3101 return 0;
3102
3103 /* Set up new tags */
3104 yaffs_init_tags(&new_tags);
3105
3106 new_tags.chunk_id = inode_chunk;
3107 new_tags.obj_id = in->obj_id;
3108 new_tags.serial_number =
3109 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3110 new_tags.n_bytes = n_bytes;
3111
3112 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3113 yaffs_trace(YAFFS_TRACE_ERROR,
3114 "Writing %d bytes to chunk!!!!!!!!!",
3115 n_bytes);
3116 YBUG();
3117 }
3118
3119 new_chunk_id =
3120 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3121
3122 if (new_chunk_id > 0) {
3123 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3124
3125 if (prev_chunk_id > 0)
3126 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3127
3128 yaffs_verify_file_sane(in);
3129 }
3130 return new_chunk_id;
3131
3132}
3133
3134
3135
3136static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3137 const YCHAR * name, const void *value, int size,
3138 int flags)
3139{
3140 struct yaffs_xattr_mod xmod;
3141
3142 int result;
3143
3144 xmod.set = set;
3145 xmod.name = name;
3146 xmod.data = value;
3147 xmod.size = size;
3148 xmod.flags = flags;
3149 xmod.result = -ENOSPC;
3150
3151 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3152
3153 if (result > 0)
3154 return xmod.result;
3155 else
3156 return -ENOSPC;
3157}
3158
3159static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3160 struct yaffs_xattr_mod *xmod)
3161{
3162 int retval = 0;
3163 int x_offs = sizeof(struct yaffs_obj_hdr);
3164 struct yaffs_dev *dev = obj->my_dev;
3165 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3166
3167 char *x_buffer = buffer + x_offs;
3168
3169 if (xmod->set)
3170 retval =
3171 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3172 xmod->size, xmod->flags);
3173 else
3174 retval = nval_del(x_buffer, x_size, xmod->name);
3175
3176 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3177 obj->xattr_known = 1;
3178
3179 xmod->result = retval;
3180
3181 return retval;
3182}
3183
3184static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
3185 void *value, int size)
3186{
3187 char *buffer = NULL;
3188 int result;
3189 struct yaffs_ext_tags tags;
3190 struct yaffs_dev *dev = obj->my_dev;
3191 int x_offs = sizeof(struct yaffs_obj_hdr);
3192 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3193
3194 char *x_buffer;
3195
3196 int retval = 0;
3197
3198 if (obj->hdr_chunk < 1)
3199 return -ENODATA;
3200
3201 /* If we know that the object has no xattribs then don't do all the
3202 * reading and parsing.
3203 */
3204 if (obj->xattr_known && !obj->has_xattr) {
3205 if (name)
3206 return -ENODATA;
3207 else
3208 return 0;
3209 }
3210
3211 buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
3212 if (!buffer)
3213 return -ENOMEM;
3214
3215 result =
3216 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3217
3218 if (result != YAFFS_OK)
3219 retval = -ENOENT;
3220 else {
3221 x_buffer = buffer + x_offs;
3222
3223 if (!obj->xattr_known) {
3224 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3225 obj->xattr_known = 1;
3226 }
3227
3228 if (name)
3229 retval = nval_get(x_buffer, x_size, name, value, size);
3230 else
3231 retval = nval_list(x_buffer, x_size, value, size);
3232 }
3233 yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
3234 return retval;
3235}
3236
3237int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3238 const void *value, int size, int flags)
3239{
3240 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3241}
3242
3243int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3244{
3245 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3246}
3247
3248int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3249 int size)
3250{
3251 return yaffs_do_xattrib_fetch(obj, name, value, size);
3252}
3253
3254int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3255{
3256 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3257}
3258
3259static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3260{
3261 u8 *chunk_data;
3262 struct yaffs_obj_hdr *oh;
3263 struct yaffs_dev *dev;
3264 struct yaffs_ext_tags tags;
3265 int result;
3266 int alloc_failed = 0;
3267
3268 if (!in)
3269 return;
3270
3271 dev = in->my_dev;
3272
3273 if (in->lazy_loaded && in->hdr_chunk > 0) {
3274 in->lazy_loaded = 0;
3275 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
3276
3277 result =
3278 yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
3279 &tags);
3280 oh = (struct yaffs_obj_hdr *)chunk_data;
3281
3282 in->yst_mode = oh->yst_mode;
3283 yaffs_load_attribs(in, oh);
3284 yaffs_set_obj_name_from_oh(in, oh);
3285
3286 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3287 in->variant.symlink_variant.alias =
3288 yaffs_clone_str(oh->alias);
3289 if (!in->variant.symlink_variant.alias)
3290 alloc_failed = 1; /* Not returned to caller */
3291 }
3292
3293 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
3294 }
3295}
3296
3297static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
3298 const YCHAR * oh_name, int buff_size)
3299{
3300#ifdef CONFIG_YAFFS_AUTO_UNICODE
3301 if (dev->param.auto_unicode) {
3302 if (*oh_name) {
3303 /* It is an ASCII name, do an ASCII to
3304 * unicode conversion */
3305 const char *ascii_oh_name = (const char *)oh_name;
3306 int n = buff_size - 1;
3307 while (n > 0 && *ascii_oh_name) {
3308 *name = *ascii_oh_name;
3309 name++;
3310 ascii_oh_name++;
3311 n--;
3312 }
3313 } else {
3314 strncpy(name, oh_name + 1, buff_size - 1);
3315 }
3316 } else {
3317#else
3318 {
3319#endif
3320 strncpy(name, oh_name, buff_size - 1);
3321 }
3322}
3323
3324static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
3325 const YCHAR * name)
3326{
3327#ifdef CONFIG_YAFFS_AUTO_UNICODE
3328
3329 int is_ascii;
3330 YCHAR *w;
3331
3332 if (dev->param.auto_unicode) {
3333
3334 is_ascii = 1;
3335 w = name;
3336
3337 /* Figure out if the name will fit in ascii character set */
3338 while (is_ascii && *w) {
3339 if ((*w) & 0xff00)
3340 is_ascii = 0;
3341 w++;
3342 }
3343
3344 if (is_ascii) {
3345 /* It is an ASCII name, so do a unicode to ascii conversion */
3346 char *ascii_oh_name = (char *)oh_name;
3347 int n = YAFFS_MAX_NAME_LENGTH - 1;
3348 while (n > 0 && *name) {
3349 *ascii_oh_name = *name;
3350 name++;
3351 ascii_oh_name++;
3352 n--;
3353 }
3354 } else {
3355 /* It is a unicode name, so save starting at the second YCHAR */
3356 *oh_name = 0;
3357 strncpy(oh_name + 1, name,
3358 YAFFS_MAX_NAME_LENGTH - 2);
3359 }
3360 } else {
3361#else
3362 {
3363#endif
3364 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3365 }
3366
3367}
3368
3369/* UpdateObjectHeader updates the header on NAND for an object.
3370 * If name is not NULL, then that new name is used.
3371 */
3372int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
3373 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3374{
3375
3376 struct yaffs_block_info *bi;
3377
3378 struct yaffs_dev *dev = in->my_dev;
3379
3380 int prev_chunk_id;
3381 int ret_val = 0;
3382 int result = 0;
3383
3384 int new_chunk_id;
3385 struct yaffs_ext_tags new_tags;
3386 struct yaffs_ext_tags old_tags;
3387 const YCHAR *alias = NULL;
3388
3389 u8 *buffer = NULL;
3390 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3391
3392 struct yaffs_obj_hdr *oh = NULL;
3393
3394 strcpy(old_name, _Y("silly old name"));
3395
3396 if (!in->fake || in == dev->root_dir ||
3397 force || xmod) {
3398
3399 yaffs_check_gc(dev, 0);
3400 yaffs_check_obj_details_loaded(in);
3401
3402 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3403 oh = (struct yaffs_obj_hdr *)buffer;
3404
3405 prev_chunk_id = in->hdr_chunk;
3406
3407 if (prev_chunk_id > 0) {
3408 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3409 buffer, &old_tags);
3410
3411 yaffs_verify_oh(in, oh, &old_tags, 0);
3412
3413 memcpy(old_name, oh->name, sizeof(oh->name));
3414 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
3415 } else {
3416 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3417 }
3418
3419 oh->type = in->variant_type;
3420 oh->yst_mode = in->yst_mode;
3421 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3422
3423 yaffs_load_attribs_oh(oh, in);
3424
3425 if (in->parent)
3426 oh->parent_obj_id = in->parent->obj_id;
3427 else
3428 oh->parent_obj_id = 0;
3429
3430 if (name && *name) {
3431 memset(oh->name, 0, sizeof(oh->name));
3432 yaffs_load_oh_from_name(dev, oh->name, name);
3433 } else if (prev_chunk_id > 0) {
3434 memcpy(oh->name, old_name, sizeof(oh->name));
3435 } else {
3436 memset(oh->name, 0, sizeof(oh->name));
3437 }
3438
3439 oh->is_shrink = is_shrink;
3440
3441 switch (in->variant_type) {
3442 case YAFFS_OBJECT_TYPE_UNKNOWN:
3443 /* Should not happen */
3444 break;
3445 case YAFFS_OBJECT_TYPE_FILE:
3446 oh->file_size =
3447 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
3448 || oh->parent_obj_id ==
3449 YAFFS_OBJECTID_UNLINKED) ? 0 : in->
3450 variant.file_variant.file_size;
3451 break;
3452 case YAFFS_OBJECT_TYPE_HARDLINK:
3453 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3454 break;
3455 case YAFFS_OBJECT_TYPE_SPECIAL:
3456 /* Do nothing */
3457 break;
3458 case YAFFS_OBJECT_TYPE_DIRECTORY:
3459 /* Do nothing */
3460 break;
3461 case YAFFS_OBJECT_TYPE_SYMLINK:
3462 alias = in->variant.symlink_variant.alias;
3463 if (!alias)
3464 alias = _Y("no alias");
3465 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3466 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3467 break;
3468 }
3469
3470 /* process any xattrib modifications */
3471 if (xmod)
3472 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3473
3474 /* Tags */
3475 yaffs_init_tags(&new_tags);
3476 in->serial++;
3477 new_tags.chunk_id = 0;
3478 new_tags.obj_id = in->obj_id;
3479 new_tags.serial_number = in->serial;
3480
3481 /* Add extra info for file header */
3482
3483 new_tags.extra_available = 1;
3484 new_tags.extra_parent_id = oh->parent_obj_id;
3485 new_tags.extra_length = oh->file_size;
3486 new_tags.extra_is_shrink = oh->is_shrink;
3487 new_tags.extra_equiv_id = oh->equiv_id;
3488 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3489 new_tags.extra_obj_type = in->variant_type;
3490
3491 yaffs_verify_oh(in, oh, &new_tags, 1);
3492
3493 /* Create new chunk in NAND */
3494 new_chunk_id =
3495 yaffs_write_new_chunk(dev, buffer, &new_tags,
3496 (prev_chunk_id > 0) ? 1 : 0);
3497
3498 if (new_chunk_id >= 0) {
3499
3500 in->hdr_chunk = new_chunk_id;
3501
3502 if (prev_chunk_id > 0) {
3503 yaffs_chunk_del(dev, prev_chunk_id, 1,
3504 __LINE__);
3505 }
3506
3507 if (!yaffs_obj_cache_dirty(in))
3508 in->dirty = 0;
3509
3510 /* If this was a shrink, then mark the block that the chunk lives on */
3511 if (is_shrink) {
3512 bi = yaffs_get_block_info(in->my_dev,
3513 new_chunk_id /
3514 in->my_dev->param.
3515 chunks_per_block);
3516 bi->has_shrink_hdr = 1;
3517 }
3518
3519 }
3520
3521 ret_val = new_chunk_id;
3522
3523 }
3524
3525 if (buffer)
3526 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3527
3528 return ret_val;
3529}
3530
3531/*--------------------- File read/write ------------------------
3532 * Read and write have very similar structures.
3533 * In general the read/write has three parts to it
3534 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3535 * Some complete chunks
3536 * An incomplete chunk to end off with
3537 *
3538 * Curve-balls: the first chunk might also be the last chunk.
3539 */
3540
3541int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3542{
3543
3544 int chunk;
3545 u32 start;
3546 int n_copy;
3547 int n = n_bytes;
3548 int n_done = 0;
3549 struct yaffs_cache *cache;
3550
3551 struct yaffs_dev *dev;
3552
3553 dev = in->my_dev;
3554
3555 while (n > 0) {
3556 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3557 /* start = offset % dev->data_bytes_per_chunk; */
3558 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3559 chunk++;
3560
3561 /* OK now check for the curveball where the start and end are in
3562 * the same chunk.
3563 */
3564 if ((start + n) < dev->data_bytes_per_chunk)
3565 n_copy = n;
3566 else
3567 n_copy = dev->data_bytes_per_chunk - start;
3568
3569 cache = yaffs_find_chunk_cache(in, chunk);
3570
3571 /* If the chunk is already in the cache or it is less than a whole chunk
3572 * or we're using inband tags then use the cache (if there is caching)
3573 * else bypass the cache.
3574 */
3575 if (cache || n_copy != dev->data_bytes_per_chunk
3576 || dev->param.inband_tags) {
3577 if (dev->param.n_caches > 0) {
3578
3579 /* If we can't find the data in the cache, then load it up. */
3580
3581 if (!cache) {
3582 cache =
3583 yaffs_grab_chunk_cache(in->my_dev);
3584 cache->object = in;
3585 cache->chunk_id = chunk;
3586 cache->dirty = 0;
3587 cache->locked = 0;
3588 yaffs_rd_data_obj(in, chunk,
3589 cache->data);
3590 cache->n_bytes = 0;
3591 }
3592
3593 yaffs_use_cache(dev, cache, 0);
3594
3595 cache->locked = 1;
3596
3597 memcpy(buffer, &cache->data[start], n_copy);
3598
3599 cache->locked = 0;
3600 } else {
3601 /* Read into the local buffer then copy.. */
3602
3603 u8 *local_buffer =
3604 yaffs_get_temp_buffer(dev, __LINE__);
3605 yaffs_rd_data_obj(in, chunk, local_buffer);
3606
3607 memcpy(buffer, &local_buffer[start], n_copy);
3608
3609 yaffs_release_temp_buffer(dev, local_buffer,
3610 __LINE__);
3611 }
3612
3613 } else {
3614
3615 /* A full chunk. Read directly into the supplied buffer. */
3616 yaffs_rd_data_obj(in, chunk, buffer);
3617
3618 }
3619
3620 n -= n_copy;
3621 offset += n_copy;
3622 buffer += n_copy;
3623 n_done += n_copy;
3624
3625 }
3626
3627 return n_done;
3628}
3629
3630int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3631 int n_bytes, int write_trhrough)
3632{
3633
3634 int chunk;
3635 u32 start;
3636 int n_copy;
3637 int n = n_bytes;
3638 int n_done = 0;
3639 int n_writeback;
3640 int start_write = offset;
3641 int chunk_written = 0;
3642 u32 n_bytes_read;
3643 u32 chunk_start;
3644
3645 struct yaffs_dev *dev;
3646
3647 dev = in->my_dev;
3648
3649 while (n > 0 && chunk_written >= 0) {
3650 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3651
3652 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3653 start >= dev->data_bytes_per_chunk) {
3654 yaffs_trace(YAFFS_TRACE_ERROR,
3655 "AddrToChunk of offset %d gives chunk %d start %d",
3656 (int)offset, chunk, start);
3657 }
3658 chunk++; /* File pos to chunk in file offset */
3659
3660 /* OK now check for the curveball where the start and end are in
3661 * the same chunk.
3662 */
3663
3664 if ((start + n) < dev->data_bytes_per_chunk) {
3665 n_copy = n;
3666
3667 /* Now folks, to calculate how many bytes to write back....
3668 * If we're overwriting and not writing to then end of file then
3669 * we need to write back as much as was there before.
3670 */
3671
3672 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3673
3674 if (chunk_start > in->variant.file_variant.file_size)
3675 n_bytes_read = 0; /* Past end of file */
3676 else
3677 n_bytes_read =
3678 in->variant.file_variant.file_size -
3679 chunk_start;
3680
3681 if (n_bytes_read > dev->data_bytes_per_chunk)
3682 n_bytes_read = dev->data_bytes_per_chunk;
3683
3684 n_writeback =
3685 (n_bytes_read >
3686 (start + n)) ? n_bytes_read : (start + n);
3687
3688 if (n_writeback < 0
3689 || n_writeback > dev->data_bytes_per_chunk)
3690 YBUG();
3691
3692 } else {
3693 n_copy = dev->data_bytes_per_chunk - start;
3694 n_writeback = dev->data_bytes_per_chunk;
3695 }
3696
3697 if (n_copy != dev->data_bytes_per_chunk
3698 || dev->param.inband_tags) {
3699 /* An incomplete start or end chunk (or maybe both start and end chunk),
3700 * or we're using inband tags, so we want to use the cache buffers.
3701 */
3702 if (dev->param.n_caches > 0) {
3703 struct yaffs_cache *cache;
3704 /* If we can't find the data in the cache, then load the cache */
3705 cache = yaffs_find_chunk_cache(in, chunk);
3706
3707 if (!cache
3708 && yaffs_check_alloc_available(dev, 1)) {
3709 cache = yaffs_grab_chunk_cache(dev);
3710 cache->object = in;
3711 cache->chunk_id = chunk;
3712 cache->dirty = 0;
3713 cache->locked = 0;
3714 yaffs_rd_data_obj(in, chunk,
3715 cache->data);
3716 } else if (cache &&
3717 !cache->dirty &&
3718 !yaffs_check_alloc_available(dev,
3719 1)) {
3720 /* Drop the cache if it was a read cache item and
3721 * no space check has been made for it.
3722 */
3723 cache = NULL;
3724 }
3725
3726 if (cache) {
3727 yaffs_use_cache(dev, cache, 1);
3728 cache->locked = 1;
3729
3730 memcpy(&cache->data[start], buffer,
3731 n_copy);
3732
3733 cache->locked = 0;
3734 cache->n_bytes = n_writeback;
3735
3736 if (write_trhrough) {
3737 chunk_written =
3738 yaffs_wr_data_obj
3739 (cache->object,
3740 cache->chunk_id,
3741 cache->data,
3742 cache->n_bytes, 1);
3743 cache->dirty = 0;
3744 }
3745
3746 } else {
3747 chunk_written = -1; /* fail the write */
3748 }
3749 } else {
3750 /* An incomplete start or end chunk (or maybe both start and end chunk)
3751 * Read into the local buffer then copy, then copy over and write back.
3752 */
3753
3754 u8 *local_buffer =
3755 yaffs_get_temp_buffer(dev, __LINE__);
3756
3757 yaffs_rd_data_obj(in, chunk, local_buffer);
3758
3759 memcpy(&local_buffer[start], buffer, n_copy);
3760
3761 chunk_written =
3762 yaffs_wr_data_obj(in, chunk,
3763 local_buffer,
3764 n_writeback, 0);
3765
3766 yaffs_release_temp_buffer(dev, local_buffer,
3767 __LINE__);
3768
3769 }
3770
3771 } else {
3772 /* A full chunk. Write directly from the supplied buffer. */
3773
3774 chunk_written =
3775 yaffs_wr_data_obj(in, chunk, buffer,
3776 dev->data_bytes_per_chunk, 0);
3777
3778 /* Since we've overwritten the cached data, we better invalidate it. */
3779 yaffs_invalidate_chunk_cache(in, chunk);
3780 }
3781
3782 if (chunk_written >= 0) {
3783 n -= n_copy;
3784 offset += n_copy;
3785 buffer += n_copy;
3786 n_done += n_copy;
3787 }
3788
3789 }
3790
3791 /* Update file object */
3792
3793 if ((start_write + n_done) > in->variant.file_variant.file_size)
3794 in->variant.file_variant.file_size = (start_write + n_done);
3795
3796 in->dirty = 1;
3797
3798 return n_done;
3799}
3800
3801int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3802 int n_bytes, int write_trhrough)
3803{
3804 yaffs2_handle_hole(in, offset);
3805 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3806}
3807
3808/* ---------------------- File resizing stuff ------------------ */
3809
3810static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3811{
3812
3813 struct yaffs_dev *dev = in->my_dev;
3814 int old_size = in->variant.file_variant.file_size;
3815
3816 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3817
3818 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3819 dev->data_bytes_per_chunk;
3820 int i;
3821 int chunk_id;
3822
3823 /* Delete backwards so that we don't end up with holes if
3824 * power is lost part-way through the operation.
3825 */
3826 for (i = last_del; i >= start_del; i--) {
3827 /* NB this could be optimised somewhat,
3828 * eg. could retrieve the tags and write them without
3829 * using yaffs_chunk_del
3830 */
3831
3832 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3833 if (chunk_id > 0) {
3834 if (chunk_id <
3835 (dev->internal_start_block *
3836 dev->param.chunks_per_block)
3837 || chunk_id >=
3838 ((dev->internal_end_block +
3839 1) * dev->param.chunks_per_block)) {
3840 yaffs_trace(YAFFS_TRACE_ALWAYS,
3841 "Found daft chunk_id %d for %d",
3842 chunk_id, i);
3843 } else {
3844 in->n_data_chunks--;
3845 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3846 }
3847 }
3848 }
3849
3850}
3851
3852void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3853{
3854 int new_full;
3855 u32 new_partial;
3856 struct yaffs_dev *dev = obj->my_dev;
3857
3858 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3859
3860 yaffs_prune_chunks(obj, new_size);
3861
3862 if (new_partial != 0) {
3863 int last_chunk = 1 + new_full;
3864 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3865
3866 /* Rewrite the last chunk with its new size and zero pad */
3867 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3868 memset(local_buffer + new_partial, 0,
3869 dev->data_bytes_per_chunk - new_partial);
3870
3871 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3872 new_partial, 1);
3873
3874 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3875 }
3876
3877 obj->variant.file_variant.file_size = new_size;
3878
3879 yaffs_prune_tree(dev, &obj->variant.file_variant);
3880}
3881
3882int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3883{
3884 struct yaffs_dev *dev = in->my_dev;
3885 int old_size = in->variant.file_variant.file_size;
3886
3887 yaffs_flush_file_cache(in);
3888 yaffs_invalidate_whole_cache(in);
3889
3890 yaffs_check_gc(dev, 0);
3891
3892 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3893 return YAFFS_FAIL;
3894
3895 if (new_size == old_size)
3896 return YAFFS_OK;
3897
3898 if (new_size > old_size) {
3899 yaffs2_handle_hole(in, new_size);
3900 in->variant.file_variant.file_size = new_size;
3901 } else {
3902 /* new_size < old_size */
3903 yaffs_resize_file_down(in, new_size);
3904 }
3905
3906 /* Write a new object header to reflect the resize.
3907 * show we've shrunk the file, if need be
3908 * Do this only if the file is not in the deleted directories
3909 * and is not shadowed.
3910 */
3911 if (in->parent &&
3912 !in->is_shadowed &&
3913 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3914 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3915 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3916
3917 return YAFFS_OK;
3918}
3919
3920int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3921{
3922 int ret_val;
3923 if (in->dirty) {
3924 yaffs_flush_file_cache(in);
3925 if (data_sync) /* Only sync data */
3926 ret_val = YAFFS_OK;
3927 else {
3928 if (update_time)
3929 yaffs_load_current_time(in, 0, 0);
3930
3931 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3932 0) ? YAFFS_OK : YAFFS_FAIL;
3933 }
3934 } else {
3935 ret_val = YAFFS_OK;
3936 }
3937
3938 return ret_val;
3939
3940}
3941
3942
3943/* yaffs_del_file deletes the whole file data
3944 * and the inode associated with the file.
3945 * It does not delete the links associated with the file.
3946 */
3947static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3948{
3949
3950 int ret_val;
3951 int del_now = 0;
3952 struct yaffs_dev *dev = in->my_dev;
3953
3954 if (!in->my_inode)
3955 del_now = 1;
3956
3957 if (del_now) {
3958 ret_val =
3959 yaffs_change_obj_name(in, in->my_dev->del_dir,
3960 _Y("deleted"), 0, 0);
3961 yaffs_trace(YAFFS_TRACE_TRACING,
3962 "yaffs: immediate deletion of file %d",
3963 in->obj_id);
3964 in->deleted = 1;
3965 in->my_dev->n_deleted_files++;
3966 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3967 yaffs_resize_file(in, 0);
3968 yaffs_soft_del_file(in);
3969 } else {
3970 ret_val =
3971 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3972 _Y("unlinked"), 0, 0);
3973 }
3974
3975 return ret_val;
3976}
3977
3978int yaffs_del_file(struct yaffs_obj *in)
3979{
3980 int ret_val = YAFFS_OK;
3981 int deleted; /* Need to cache value on stack if in is freed */
3982 struct yaffs_dev *dev = in->my_dev;
3983
3984 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3985 yaffs_resize_file(in, 0);
3986
3987 if (in->n_data_chunks > 0) {
3988 /* Use soft deletion if there is data in the file.
3989 * That won't be the case if it has been resized to zero.
3990 */
3991 if (!in->unlinked)
3992 ret_val = yaffs_unlink_file_if_needed(in);
3993
3994 deleted = in->deleted;
3995
3996 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3997 in->deleted = 1;
3998 deleted = 1;
3999 in->my_dev->n_deleted_files++;
4000 yaffs_soft_del_file(in);
4001 }
4002 return deleted ? YAFFS_OK : YAFFS_FAIL;
4003 } else {
4004 /* The file has no data chunks so we toss it immediately */
4005 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
4006 in->variant.file_variant.top = NULL;
4007 yaffs_generic_obj_del(in);
4008
4009 return YAFFS_OK;
4010 }
4011}
4012
4013int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
4014{
4015 return (obj &&
4016 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
4017 !(list_empty(&obj->variant.dir_variant.children));
4018}
4019
4020static int yaffs_del_dir(struct yaffs_obj *obj)
4021{
4022 /* First check that the directory is empty. */
4023 if (yaffs_is_non_empty_dir(obj))
4024 return YAFFS_FAIL;
4025
4026 return yaffs_generic_obj_del(obj);
4027}
4028
4029static int yaffs_del_symlink(struct yaffs_obj *in)
4030{
4031 if (in->variant.symlink_variant.alias)
4032 kfree(in->variant.symlink_variant.alias);
4033 in->variant.symlink_variant.alias = NULL;
4034
4035 return yaffs_generic_obj_del(in);
4036}
4037
4038static int yaffs_del_link(struct yaffs_obj *in)
4039{
4040 /* remove this hardlink from the list assocaited with the equivalent
4041 * object
4042 */
4043 list_del_init(&in->hard_links);
4044 return yaffs_generic_obj_del(in);
4045}
4046
4047int yaffs_del_obj(struct yaffs_obj *obj)
4048{
4049 int ret_val = -1;
4050 switch (obj->variant_type) {
4051 case YAFFS_OBJECT_TYPE_FILE:
4052 ret_val = yaffs_del_file(obj);
4053 break;
4054 case YAFFS_OBJECT_TYPE_DIRECTORY:
4055 if (!list_empty(&obj->variant.dir_variant.dirty)) {
4056 yaffs_trace(YAFFS_TRACE_BACKGROUND,
4057 "Remove object %d from dirty directories",
4058 obj->obj_id);
4059 list_del_init(&obj->variant.dir_variant.dirty);
4060 }
4061 return yaffs_del_dir(obj);
4062 break;
4063 case YAFFS_OBJECT_TYPE_SYMLINK:
4064 ret_val = yaffs_del_symlink(obj);
4065 break;
4066 case YAFFS_OBJECT_TYPE_HARDLINK:
4067 ret_val = yaffs_del_link(obj);
4068 break;
4069 case YAFFS_OBJECT_TYPE_SPECIAL:
4070 ret_val = yaffs_generic_obj_del(obj);
4071 break;
4072 case YAFFS_OBJECT_TYPE_UNKNOWN:
4073 ret_val = 0;
4074 break; /* should not happen. */
4075 }
4076
4077 return ret_val;
4078}
4079
4080static int yaffs_unlink_worker(struct yaffs_obj *obj)
4081{
4082
4083 int del_now = 0;
4084
4085 if (!obj->my_inode)
4086 del_now = 1;
4087
4088 if (obj)
4089 yaffs_update_parent(obj->parent);
4090
4091 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4092 return yaffs_del_link(obj);
4093 } else if (!list_empty(&obj->hard_links)) {
4094 /* Curve ball: We're unlinking an object that has a hardlink.
4095 *
4096 * This problem arises because we are not strictly following
4097 * The Linux link/inode model.
4098 *
4099 * We can't really delete the object.
4100 * Instead, we do the following:
4101 * - Select a hardlink.
4102 * - Unhook it from the hard links
4103 * - Move it from its parent directory (so that the rename can work)
4104 * - Rename the object to the hardlink's name.
4105 * - Delete the hardlink
4106 */
4107
4108 struct yaffs_obj *hl;
4109 struct yaffs_obj *parent;
4110 int ret_val;
4111 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4112
4113 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4114 hard_links);
4115
4116 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4117 parent = hl->parent;
4118
4119 list_del_init(&hl->hard_links);
4120
4121 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4122
4123 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4124
4125 if (ret_val == YAFFS_OK)
4126 ret_val = yaffs_generic_obj_del(hl);
4127
4128 return ret_val;
4129
4130 } else if (del_now) {
4131 switch (obj->variant_type) {
4132 case YAFFS_OBJECT_TYPE_FILE:
4133 return yaffs_del_file(obj);
4134 break;
4135 case YAFFS_OBJECT_TYPE_DIRECTORY:
4136 list_del_init(&obj->variant.dir_variant.dirty);
4137 return yaffs_del_dir(obj);
4138 break;
4139 case YAFFS_OBJECT_TYPE_SYMLINK:
4140 return yaffs_del_symlink(obj);
4141 break;
4142 case YAFFS_OBJECT_TYPE_SPECIAL:
4143 return yaffs_generic_obj_del(obj);
4144 break;
4145 case YAFFS_OBJECT_TYPE_HARDLINK:
4146 case YAFFS_OBJECT_TYPE_UNKNOWN:
4147 default:
4148 return YAFFS_FAIL;
4149 }
4150 } else if (yaffs_is_non_empty_dir(obj)) {
4151 return YAFFS_FAIL;
4152 } else {
4153 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4154 _Y("unlinked"), 0, 0);
4155 }
4156}
4157
4158static int yaffs_unlink_obj(struct yaffs_obj *obj)
4159{
4160
4161 if (obj && obj->unlink_allowed)
4162 return yaffs_unlink_worker(obj);
4163
4164 return YAFFS_FAIL;
4165
4166}
4167
4168int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
4169{
4170 struct yaffs_obj *obj;
4171
4172 obj = yaffs_find_by_name(dir, name);
4173 return yaffs_unlink_obj(obj);
4174}
4175
4176/* Note:
4177 * If old_name is NULL then we take old_dir as the object to be renamed.
4178 */
4179int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
4180 struct yaffs_obj *new_dir, const YCHAR * new_name)
4181{
4182 struct yaffs_obj *obj = NULL;
4183 struct yaffs_obj *existing_target = NULL;
4184 int force = 0;
4185 int result;
4186 struct yaffs_dev *dev;
4187
4188 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4189 YBUG();
4190 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4191 YBUG();
4192
4193 dev = old_dir->my_dev;
4194
4195#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4196 /* Special case for case insemsitive systems.
4197 * While look-up is case insensitive, the name isn't.
4198 * Therefore we might want to change x.txt to X.txt
4199 */
4200 if (old_dir == new_dir &&
4201 old_name && new_name &&
4202 strcmp(old_name, new_name) == 0)
4203 force = 1;
4204#endif
4205
4206 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4207 YAFFS_MAX_NAME_LENGTH)
4208 /* ENAMETOOLONG */
4209 return YAFFS_FAIL;
4210
4211 if(old_name)
4212 obj = yaffs_find_by_name(old_dir, old_name);
4213 else{
4214 obj = old_dir;
4215 old_dir = obj->parent;
4216 }
4217
4218
4219 if (obj && obj->rename_allowed) {
4220
4221 /* Now do the handling for an existing target, if there is one */
4222
4223 existing_target = yaffs_find_by_name(new_dir, new_name);
4224 if (yaffs_is_non_empty_dir(existing_target)){
4225 return YAFFS_FAIL; /* ENOTEMPTY */
4226 } else if (existing_target && existing_target != obj) {
4227 /* Nuke the target first, using shadowing,
4228 * but only if it isn't the same object.
4229 *
4230 * Note we must disable gc otherwise it can mess up the shadowing.
4231 *
4232 */
4233 dev->gc_disable = 1;
4234 yaffs_change_obj_name(obj, new_dir, new_name, force,
4235 existing_target->obj_id);
4236 existing_target->is_shadowed = 1;
4237 yaffs_unlink_obj(existing_target);
4238 dev->gc_disable = 0;
4239 }
4240
4241 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4242
4243 yaffs_update_parent(old_dir);
4244 if (new_dir != old_dir)
4245 yaffs_update_parent(new_dir);
4246
4247 return result;
4248 }
4249 return YAFFS_FAIL;
4250}
4251
4252/*----------------------- Initialisation Scanning ---------------------- */
4253
4254void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4255 int backward_scanning)
4256{
4257 struct yaffs_obj *obj;
4258
4259 if (!backward_scanning) {
4260 /* Handle YAFFS1 forward scanning case
4261 * For YAFFS1 we always do the deletion
4262 */
4263
4264 } else {
4265 /* Handle YAFFS2 case (backward scanning)
4266 * If the shadowed object exists then ignore.
4267 */
4268 obj = yaffs_find_by_number(dev, obj_id);
4269 if (obj)
4270 return;
4271 }
4272
4273 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4274 * We put it in unlinked dir to be cleaned up after the scanning
4275 */
4276 obj =
4277 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4278 if (!obj)
4279 return;
4280 obj->is_shadowed = 1;
4281 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4282 obj->variant.file_variant.shrink_size = 0;
4283 obj->valid = 1; /* So that we don't read any other info for this file */
4284
4285}
4286
4287void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
4288{
4289 struct yaffs_obj *hl;
4290 struct yaffs_obj *in;
4291
4292 while (hard_list) {
4293 hl = hard_list;
4294 hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
4295
4296 in = yaffs_find_by_number(dev,
4297 hl->variant.
4298 hardlink_variant.equiv_id);
4299
4300 if (in) {
4301 /* Add the hardlink pointers */
4302 hl->variant.hardlink_variant.equiv_obj = in;
4303 list_add(&hl->hard_links, &in->hard_links);
4304 } else {
4305 /* Todo Need to report/handle this better.
4306 * Got a problem... hardlink to a non-existant object
4307 */
4308 hl->variant.hardlink_variant.equiv_obj = NULL;
4309 INIT_LIST_HEAD(&hl->hard_links);
4310
4311 }
4312 }
4313}
4314
4315static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4316{
4317 /*
4318 * Sort out state of unlinked and deleted objects after scanning.
4319 */
4320 struct list_head *i;
4321 struct list_head *n;
4322 struct yaffs_obj *l;
4323
4324 if (dev->read_only)
4325 return;
4326
4327 /* Soft delete all the unlinked files */
4328 list_for_each_safe(i, n,
4329 &dev->unlinked_dir->variant.dir_variant.children) {
4330 if (i) {
4331 l = list_entry(i, struct yaffs_obj, siblings);
4332 yaffs_del_obj(l);
4333 }
4334 }
4335
4336 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4337 if (i) {
4338 l = list_entry(i, struct yaffs_obj, siblings);
4339 yaffs_del_obj(l);
4340 }
4341 }
4342
4343}
4344
4345/*
4346 * This code iterates through all the objects making sure that they are rooted.
4347 * Any unrooted objects are re-rooted in lost+found.
4348 * An object needs to be in one of:
4349 * - Directly under deleted, unlinked
4350 * - Directly or indirectly under root.
4351 *
4352 * Note:
4353 * This code assumes that we don't ever change the current relationships between
4354 * directories:
4355 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4356 * lost-n-found->parent == root_dir
4357 *
4358 * This fixes the problem where directories might have inadvertently been deleted
4359 * leaving the object "hanging" without being rooted in the directory tree.
4360 */
4361
4362static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4363{
4364 return (obj == dev->del_dir ||
4365 obj == dev->unlinked_dir || obj == dev->root_dir);
4366}
4367
4368static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4369{
4370 struct yaffs_obj *obj;
4371 struct yaffs_obj *parent;
4372 int i;
4373 struct list_head *lh;
4374 struct list_head *n;
4375 int depth_limit;
4376 int hanging;
4377
4378 if (dev->read_only)
4379 return;
4380
4381 /* Iterate through the objects in each hash entry,
4382 * looking at each object.
4383 * Make sure it is rooted.
4384 */
4385
4386 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4387 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4388 if (lh) {
4389 obj =
4390 list_entry(lh, struct yaffs_obj, hash_link);
4391 parent = obj->parent;
4392
4393 if (yaffs_has_null_parent(dev, obj)) {
4394 /* These directories are not hanging */
4395 hanging = 0;
4396 } else if (!parent
4397 || parent->variant_type !=
4398 YAFFS_OBJECT_TYPE_DIRECTORY) {
4399 hanging = 1;
4400 } else if (yaffs_has_null_parent(dev, parent)) {
4401 hanging = 0;
4402 } else {
4403 /*
4404 * Need to follow the parent chain to see if it is hanging.
4405 */
4406 hanging = 0;
4407 depth_limit = 100;
4408
4409 while (parent != dev->root_dir &&
4410 parent->parent &&
4411 parent->parent->variant_type ==
4412 YAFFS_OBJECT_TYPE_DIRECTORY
4413 && depth_limit > 0) {
4414 parent = parent->parent;
4415 depth_limit--;
4416 }
4417 if (parent != dev->root_dir)
4418 hanging = 1;
4419 }
4420 if (hanging) {
4421 yaffs_trace(YAFFS_TRACE_SCAN,
4422 "Hanging object %d moved to lost and found",
4423 obj->obj_id);
4424 yaffs_add_obj_to_dir(dev->lost_n_found,
4425 obj);
4426 }
4427 }
4428 }
4429 }
4430}
4431
4432/*
4433 * Delete directory contents for cleaning up lost and found.
4434 */
4435static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4436{
4437 struct yaffs_obj *obj;
4438 struct list_head *lh;
4439 struct list_head *n;
4440
4441 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4442 YBUG();
4443
4444 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4445 if (lh) {
4446 obj = list_entry(lh, struct yaffs_obj, siblings);
4447 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4448 yaffs_del_dir_contents(obj);
4449
4450 yaffs_trace(YAFFS_TRACE_SCAN,
4451 "Deleting lost_found object %d",
4452 obj->obj_id);
4453
4454 /* Need to use UnlinkObject since Delete would not handle
4455 * hardlinked objects correctly.
4456 */
4457 yaffs_unlink_obj(obj);
4458 }
4459 }
4460
4461}
4462
4463static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4464{
4465 yaffs_del_dir_contents(dev->lost_n_found);
4466}
4467
4468
4469struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4470 const YCHAR * name)
4471{
4472 int sum;
4473
4474 struct list_head *i;
4475 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4476
4477 struct yaffs_obj *l;
4478
4479 if (!name)
4480 return NULL;
4481
4482 if (!directory) {
4483 yaffs_trace(YAFFS_TRACE_ALWAYS,
4484 "tragedy: yaffs_find_by_name: null pointer directory"
4485 );
4486 YBUG();
4487 return NULL;
4488 }
4489 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4490 yaffs_trace(YAFFS_TRACE_ALWAYS,
4491 "tragedy: yaffs_find_by_name: non-directory"
4492 );
4493 YBUG();
4494 }
4495
4496 sum = yaffs_calc_name_sum(name);
4497
4498 list_for_each(i, &directory->variant.dir_variant.children) {
4499 if (i) {
4500 l = list_entry(i, struct yaffs_obj, siblings);
4501
4502 if (l->parent != directory)
4503 YBUG();
4504
4505 yaffs_check_obj_details_loaded(l);
4506
4507 /* Special case for lost-n-found */
4508 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4509 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4510 return l;
4511 } else if (l->sum == sum
4512 || l->hdr_chunk <= 0) {
4513 /* LostnFound chunk called Objxxx
4514 * Do a real check
4515 */
4516 yaffs_get_obj_name(l, buffer,
4517 YAFFS_MAX_NAME_LENGTH + 1);
4518 if (strncmp
4519 (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4520 return l;
4521 }
4522 }
4523 }
4524
4525 return NULL;
4526}
4527
4528/* GetEquivalentObject dereferences any hard links to get to the
4529 * actual object.
4530 */
4531
4532struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4533{
4534 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4535 /* We want the object id of the equivalent object, not this one */
4536 obj = obj->variant.hardlink_variant.equiv_obj;
4537 yaffs_check_obj_details_loaded(obj);
4538 }
4539 return obj;
4540}
4541
4542/*
4543 * A note or two on object names.
4544 * * If the object name is missing, we then make one up in the form objnnn
4545 *
4546 * * ASCII names are stored in the object header's name field from byte zero
4547 * * Unicode names are historically stored starting from byte zero.
4548 *
4549 * Then there are automatic Unicode names...
4550 * The purpose of these is to save names in a way that can be read as
4551 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4552 * system to share files.
4553 *
4554 * These automatic unicode are stored slightly differently...
4555 * - If the name can fit in the ASCII character space then they are saved as
4556 * ascii names as per above.
4557 * - If the name needs Unicode then the name is saved in Unicode
4558 * starting at oh->name[1].
4559
4560 */
4561static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
4562 int buffer_size)
4563{
4564 /* Create an object name if we could not find one. */
4565 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4566 YCHAR local_name[20];
4567 YCHAR num_string[20];
4568 YCHAR *x = &num_string[19];
4569 unsigned v = obj->obj_id;
4570 num_string[19] = 0;
4571 while (v > 0) {
4572 x--;
4573 *x = '0' + (v % 10);
4574 v /= 10;
4575 }
4576 /* make up a name */
4577 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4578 strcat(local_name, x);
4579 strncpy(name, local_name, buffer_size - 1);
4580 }
4581}
4582
4583int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
4584{
4585 memset(name, 0, buffer_size * sizeof(YCHAR));
4586
4587 yaffs_check_obj_details_loaded(obj);
4588
4589 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4590 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4591 }
4592#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4593 else if (obj->short_name[0]) {
4594 strcpy(name, obj->short_name);
4595 }
4596#endif
4597 else if (obj->hdr_chunk > 0) {
4598 int result;
4599 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4600
4601 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4602
4603 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4604
4605 if (obj->hdr_chunk > 0) {
4606 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4607 obj->hdr_chunk,
4608 buffer, NULL);
4609 }
4610 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4611 buffer_size);
4612
4613 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4614 }
4615
4616 yaffs_fix_null_name(obj, name, buffer_size);
4617
4618 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4619}
4620
4621int yaffs_get_obj_length(struct yaffs_obj *obj)
4622{
4623 /* Dereference any hard linking */
4624 obj = yaffs_get_equivalent_obj(obj);
4625
4626 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4627 return obj->variant.file_variant.file_size;
4628 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4629 if (!obj->variant.symlink_variant.alias)
4630 return 0;
4631 return strnlen(obj->variant.symlink_variant.alias,
4632 YAFFS_MAX_ALIAS_LENGTH);
4633 } else {
4634 /* Only a directory should drop through to here */
4635 return obj->my_dev->data_bytes_per_chunk;
4636 }
4637}
4638
4639int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4640{
4641 int count = 0;
4642 struct list_head *i;
4643
4644 if (!obj->unlinked)
4645 count++; /* the object itself */
4646
4647 list_for_each(i, &obj->hard_links)
4648 count++; /* add the hard links; */
4649
4650 return count;
4651}
4652
4653int yaffs_get_obj_inode(struct yaffs_obj *obj)
4654{
4655 obj = yaffs_get_equivalent_obj(obj);
4656
4657 return obj->obj_id;
4658}
4659
4660unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4661{
4662 obj = yaffs_get_equivalent_obj(obj);
4663
4664 switch (obj->variant_type) {
4665 case YAFFS_OBJECT_TYPE_FILE:
4666 return DT_REG;
4667 break;
4668 case YAFFS_OBJECT_TYPE_DIRECTORY:
4669 return DT_DIR;
4670 break;
4671 case YAFFS_OBJECT_TYPE_SYMLINK:
4672 return DT_LNK;
4673 break;
4674 case YAFFS_OBJECT_TYPE_HARDLINK:
4675 return DT_REG;
4676 break;
4677 case YAFFS_OBJECT_TYPE_SPECIAL:
4678 if (S_ISFIFO(obj->yst_mode))
4679 return DT_FIFO;
4680 if (S_ISCHR(obj->yst_mode))
4681 return DT_CHR;
4682 if (S_ISBLK(obj->yst_mode))
4683 return DT_BLK;
4684 if (S_ISSOCK(obj->yst_mode))
4685 return DT_SOCK;
4686 default:
4687 return DT_REG;
4688 break;
4689 }
4690}
4691
4692YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4693{
4694 obj = yaffs_get_equivalent_obj(obj);
4695 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4696 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4697 else
4698 return yaffs_clone_str(_Y(""));
4699}
4700
4701/*--------------------------- Initialisation code -------------------------- */
4702
4703static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4704{
4705
4706 /* Common functions, gotta have */
4707 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4708 return 0;
4709
4710#ifdef CONFIG_YAFFS_YAFFS2
4711
4712 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4713 if (dev->param.write_chunk_tags_fn &&
4714 dev->param.read_chunk_tags_fn &&
4715 !dev->param.write_chunk_fn &&
4716 !dev->param.read_chunk_fn &&
4717 dev->param.bad_block_fn && dev->param.query_block_fn)
4718 return 1;
4719#endif
4720
4721 /* Can use the "spare" style interface for yaffs1 */
4722 if (!dev->param.is_yaffs2 &&
4723 !dev->param.write_chunk_tags_fn &&
4724 !dev->param.read_chunk_tags_fn &&
4725 dev->param.write_chunk_fn &&
4726 dev->param.read_chunk_fn &&
4727 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4728 return 1;
4729
4730 return 0; /* bad */
4731}
4732
4733static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4734{
4735 /* Initialise the unlinked, deleted, root and lost and found directories */
4736
4737 dev->lost_n_found = dev->root_dir = NULL;
4738 dev->unlinked_dir = dev->del_dir = NULL;
4739
4740 dev->unlinked_dir =
4741 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4742
4743 dev->del_dir =
4744 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4745
4746 dev->root_dir =
4747 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4748 YAFFS_ROOT_MODE | S_IFDIR);
4749 dev->lost_n_found =
4750 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4751 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4752
4753 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4754 && dev->del_dir) {
4755 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4756 return YAFFS_OK;
4757 }
4758
4759 return YAFFS_FAIL;
4760}
4761
4762int yaffs_guts_initialise(struct yaffs_dev *dev)
4763{
4764 int init_failed = 0;
4765 unsigned x;
4766 int bits;
4767
4768 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()" );
4769
4770 /* Check stuff that must be set */
4771
4772 if (!dev) {
4773 yaffs_trace(YAFFS_TRACE_ALWAYS,
4774 "yaffs: Need a device"
4775 );
4776 return YAFFS_FAIL;
4777 }
4778
4779 dev->internal_start_block = dev->param.start_block;
4780 dev->internal_end_block = dev->param.end_block;
4781 dev->block_offset = 0;
4782 dev->chunk_offset = 0;
4783 dev->n_free_chunks = 0;
4784
4785 dev->gc_block = 0;
4786
4787 if (dev->param.start_block == 0) {
4788 dev->internal_start_block = dev->param.start_block + 1;
4789 dev->internal_end_block = dev->param.end_block + 1;
4790 dev->block_offset = 1;
4791 dev->chunk_offset = dev->param.chunks_per_block;
4792 }
4793
4794 /* Check geometry parameters. */
4795
4796 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4797 dev->param.total_bytes_per_chunk < 1024) ||
4798 (!dev->param.is_yaffs2 &&
4799 dev->param.total_bytes_per_chunk < 512) ||
4800 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4801 dev->param.chunks_per_block < 2 ||
4802 dev->param.n_reserved_blocks < 2 ||
4803 dev->internal_start_block <= 0 ||
4804 dev->internal_end_block <= 0 ||
4805 dev->internal_end_block <=
4806 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4807 ) {
4808 /* otherwise it is too small */
4809 yaffs_trace(YAFFS_TRACE_ALWAYS,
4810 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4811 dev->param.total_bytes_per_chunk,
4812 dev->param.is_yaffs2 ? "2" : "",
4813 dev->param.inband_tags);
4814 return YAFFS_FAIL;
4815 }
4816
4817 if (yaffs_init_nand(dev) != YAFFS_OK) {
4818 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4819 return YAFFS_FAIL;
4820 }
4821
4822 /* Sort out space for inband tags, if required */
4823 if (dev->param.inband_tags)
4824 dev->data_bytes_per_chunk =
4825 dev->param.total_bytes_per_chunk -
4826 sizeof(struct yaffs_packed_tags2_tags_only);
4827 else
4828 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4829
4830 /* Got the right mix of functions? */
4831 if (!yaffs_check_dev_fns(dev)) {
4832 /* Function missing */
4833 yaffs_trace(YAFFS_TRACE_ALWAYS,
4834 "device function(s) missing or wrong");
4835
4836 return YAFFS_FAIL;
4837 }
4838
4839 if (dev->is_mounted) {
4840 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4841 return YAFFS_FAIL;
4842 }
4843
4844 /* Finished with most checks. One or two more checks happen later on too. */
4845
4846 dev->is_mounted = 1;
4847
4848 /* OK now calculate a few things for the device */
4849
4850 /*
4851 * Calculate all the chunk size manipulation numbers:
4852 */
4853 x = dev->data_bytes_per_chunk;
4854 /* We always use dev->chunk_shift and dev->chunk_div */
4855 dev->chunk_shift = calc_shifts(x);
4856 x >>= dev->chunk_shift;
4857 dev->chunk_div = x;
4858 /* We only use chunk mask if chunk_div is 1 */
4859 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4860
4861 /*
4862 * Calculate chunk_grp_bits.
4863 * We need to find the next power of 2 > than internal_end_block
4864 */
4865
4866 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4867
4868 bits = calc_shifts_ceiling(x);
4869
4870 /* Set up tnode width if wide tnodes are enabled. */
4871 if (!dev->param.wide_tnodes_disabled) {
4872 /* bits must be even so that we end up with 32-bit words */
4873 if (bits & 1)
4874 bits++;
4875 if (bits < 16)
4876 dev->tnode_width = 16;
4877 else
4878 dev->tnode_width = bits;
4879 } else {
4880 dev->tnode_width = 16;
4881 }
4882
4883 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4884
4885 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4886 * so if the bitwidth of the
4887 * chunk range we're using is greater than 16 we need
4888 * to figure out chunk shift and chunk_grp_size
4889 */
4890
4891 if (bits <= dev->tnode_width)
4892 dev->chunk_grp_bits = 0;
4893 else
4894 dev->chunk_grp_bits = bits - dev->tnode_width;
4895
4896 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4897 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4898 dev->tnode_size = sizeof(struct yaffs_tnode);
4899
4900 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4901
4902 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4903 /* We have a problem because the soft delete won't work if
4904 * the chunk group size > chunks per block.
4905 * This can be remedied by using larger "virtual blocks".
4906 */
4907 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4908
4909 return YAFFS_FAIL;
4910 }
4911
4912 /* OK, we've finished verifying the device, lets continue with initialisation */
4913
4914 /* More device initialisation */
4915 dev->all_gcs = 0;
4916 dev->passive_gc_count = 0;
4917 dev->oldest_dirty_gc_count = 0;
4918 dev->bg_gcs = 0;
4919 dev->gc_block_finder = 0;
4920 dev->buffered_block = -1;
4921 dev->doing_buffered_block_rewrite = 0;
4922 dev->n_deleted_files = 0;
4923 dev->n_bg_deletions = 0;
4924 dev->n_unlinked_files = 0;
4925 dev->n_ecc_fixed = 0;
4926 dev->n_ecc_unfixed = 0;
4927 dev->n_tags_ecc_fixed = 0;
4928 dev->n_tags_ecc_unfixed = 0;
4929 dev->n_erase_failures = 0;
4930 dev->n_erased_blocks = 0;
4931 dev->gc_disable = 0;
4932 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
4933 INIT_LIST_HEAD(&dev->dirty_dirs);
4934 dev->oldest_dirty_seq = 0;
4935 dev->oldest_dirty_block = 0;
4936
4937 /* Initialise temporary buffers and caches. */
4938 if (!yaffs_init_tmp_buffers(dev))
4939 init_failed = 1;
4940
4941 dev->cache = NULL;
4942 dev->gc_cleanup_list = NULL;
4943
4944 if (!init_failed && dev->param.n_caches > 0) {
4945 int i;
4946 void *buf;
4947 int cache_bytes =
4948 dev->param.n_caches * sizeof(struct yaffs_cache);
4949
4950 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4951 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4952
4953 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4954
4955 buf = (u8 *) dev->cache;
4956
4957 if (dev->cache)
4958 memset(dev->cache, 0, cache_bytes);
4959
4960 for (i = 0; i < dev->param.n_caches && buf; i++) {
4961 dev->cache[i].object = NULL;
4962 dev->cache[i].last_use = 0;
4963 dev->cache[i].dirty = 0;
4964 dev->cache[i].data = buf =
4965 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4966 }
4967 if (!buf)
4968 init_failed = 1;
4969
4970 dev->cache_last_use = 0;
4971 }
4972
4973 dev->cache_hits = 0;
4974
4975 if (!init_failed) {
4976 dev->gc_cleanup_list =
4977 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4978 GFP_NOFS);
4979 if (!dev->gc_cleanup_list)
4980 init_failed = 1;
4981 }
4982
4983 if (dev->param.is_yaffs2)
4984 dev->param.use_header_file_size = 1;
4985
4986 if (!init_failed && !yaffs_init_blocks(dev))
4987 init_failed = 1;
4988
4989 yaffs_init_tnodes_and_objs(dev);
4990
4991 if (!init_failed && !yaffs_create_initial_dir(dev))
4992 init_failed = 1;
4993
4994 if (!init_failed) {
4995 /* Now scan the flash. */
4996 if (dev->param.is_yaffs2) {
4997 if (yaffs2_checkpt_restore(dev)) {
4998 yaffs_check_obj_details_loaded(dev->root_dir);
4999 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
5000 "yaffs: restored from checkpoint"
5001 );
5002 } else {
5003
5004 /* Clean up the mess caused by an aborted checkpoint load
5005 * and scan backwards.
5006 */
5007 yaffs_deinit_blocks(dev);
5008
5009 yaffs_deinit_tnodes_and_objs(dev);
5010
5011 dev->n_erased_blocks = 0;
5012 dev->n_free_chunks = 0;
5013 dev->alloc_block = -1;
5014 dev->alloc_page = -1;
5015 dev->n_deleted_files = 0;
5016 dev->n_unlinked_files = 0;
5017 dev->n_bg_deletions = 0;
5018
5019 if (!init_failed && !yaffs_init_blocks(dev))
5020 init_failed = 1;
5021
5022 yaffs_init_tnodes_and_objs(dev);
5023
5024 if (!init_failed
5025 && !yaffs_create_initial_dir(dev))
5026 init_failed = 1;
5027
5028 if (!init_failed && !yaffs2_scan_backwards(dev))
5029 init_failed = 1;
5030 }
5031 } else if (!yaffs1_scan(dev)) {
5032 init_failed = 1;
5033 }
5034
5035 yaffs_strip_deleted_objs(dev);
5036 yaffs_fix_hanging_objs(dev);
5037 if (dev->param.empty_lost_n_found)
5038 yaffs_empty_l_n_f(dev);
5039 }
5040
5041 if (init_failed) {
5042 /* Clean up the mess */
5043 yaffs_trace(YAFFS_TRACE_TRACING,
5044 "yaffs: yaffs_guts_initialise() aborted.");
5045
5046 yaffs_deinitialise(dev);
5047 return YAFFS_FAIL;
5048 }
5049
5050 /* Zero out stats */
5051 dev->n_page_reads = 0;
5052 dev->n_page_writes = 0;
5053 dev->n_erasures = 0;
5054 dev->n_gc_copies = 0;
5055 dev->n_retired_writes = 0;
5056
5057 dev->n_retired_blocks = 0;
5058
5059 yaffs_verify_free_chunks(dev);
5060 yaffs_verify_blocks(dev);
5061
5062 /* Clean up any aborted checkpoint data */
5063 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5064 yaffs2_checkpt_invalidate(dev);
5065
5066 yaffs_trace(YAFFS_TRACE_TRACING,
5067 "yaffs: yaffs_guts_initialise() done.");
5068 return YAFFS_OK;
5069
5070}
5071
5072void yaffs_deinitialise(struct yaffs_dev *dev)
5073{
5074 if (dev->is_mounted) {
5075 int i;
5076
5077 yaffs_deinit_blocks(dev);
5078 yaffs_deinit_tnodes_and_objs(dev);
5079 if (dev->param.n_caches > 0 && dev->cache) {
5080
5081 for (i = 0; i < dev->param.n_caches; i++) {
5082 if (dev->cache[i].data)
5083 kfree(dev->cache[i].data);
5084 dev->cache[i].data = NULL;
5085 }
5086
5087 kfree(dev->cache);
5088 dev->cache = NULL;
5089 }
5090
5091 kfree(dev->gc_cleanup_list);
5092
5093 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5094 kfree(dev->temp_buffer[i].buffer);
5095
5096 dev->is_mounted = 0;
5097
5098 if (dev->param.deinitialise_flash_fn)
5099 dev->param.deinitialise_flash_fn(dev);
5100 }
5101}
5102
5103int yaffs_count_free_chunks(struct yaffs_dev *dev)
5104{
5105 int n_free = 0;
5106 int b;
5107
5108 struct yaffs_block_info *blk;
5109
5110 blk = dev->block_info;
5111 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5112 switch (blk->block_state) {
5113 case YAFFS_BLOCK_STATE_EMPTY:
5114 case YAFFS_BLOCK_STATE_ALLOCATING:
5115 case YAFFS_BLOCK_STATE_COLLECTING:
5116 case YAFFS_BLOCK_STATE_FULL:
5117 n_free +=
5118 (dev->param.chunks_per_block - blk->pages_in_use +
5119 blk->soft_del_pages);
5120 break;
5121 default:
5122 break;
5123 }
5124 blk++;
5125 }
5126
5127 return n_free;
5128}
5129
5130int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5131{
5132 /* This is what we report to the outside world */
5133
5134 int n_free;
5135 int n_dirty_caches;
5136 int blocks_for_checkpt;
5137 int i;
5138
5139 n_free = dev->n_free_chunks;
5140 n_free += dev->n_deleted_files;
5141
5142 /* Now count the number of dirty chunks in the cache and subtract those */
5143
5144 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5145 if (dev->cache[i].dirty)
5146 n_dirty_caches++;
5147 }
5148
5149 n_free -= n_dirty_caches;
5150
5151 n_free -=
5152 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5153
5154 /* Now we figure out how much to reserve for the checkpoint and report that... */
5155 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5156
5157 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
5158
5159 if (n_free < 0)
5160 n_free = 0;
5161
5162 return n_free;
5163
5164}
diff --git a/fs/yaffs2/yaffs_guts.h b/fs/yaffs2/yaffs_guts.h
new file mode 100644
index 00000000000..307eba28676
--- /dev/null
+++ b/fs/yaffs2/yaffs_guts.h
@@ -0,0 +1,915 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_GUTS_H__
17#define __YAFFS_GUTS_H__
18
19#include "yportenv.h"
20
21#define YAFFS_OK 1
22#define YAFFS_FAIL 0
23
24/* Give us a Y=0x59,
25 * Give us an A=0x41,
26 * Give us an FF=0xFF
27 * Give us an S=0x53
28 * And what have we got...
29 */
30#define YAFFS_MAGIC 0x5941FF53
31
32#define YAFFS_NTNODES_LEVEL0 16
33#define YAFFS_TNODES_LEVEL0_BITS 4
34#define YAFFS_TNODES_LEVEL0_MASK 0xf
35
36#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
37#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
38#define YAFFS_TNODES_INTERNAL_MASK 0x7
39#define YAFFS_TNODES_MAX_LEVEL 6
40
41#ifndef CONFIG_YAFFS_NO_YAFFS1
42#define YAFFS_BYTES_PER_SPARE 16
43#define YAFFS_BYTES_PER_CHUNK 512
44#define YAFFS_CHUNK_SIZE_SHIFT 9
45#define YAFFS_CHUNKS_PER_BLOCK 32
46#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
47#endif
48
49#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
50#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
51
52#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
53
54#define YAFFS_ALLOCATION_NOBJECTS 100
55#define YAFFS_ALLOCATION_NTNODES 100
56#define YAFFS_ALLOCATION_NLINKS 100
57
58#define YAFFS_NOBJECT_BUCKETS 256
59
60#define YAFFS_OBJECT_SPACE 0x40000
61#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE -1)
62
63#define YAFFS_CHECKPOINT_VERSION 4
64
65#ifdef CONFIG_YAFFS_UNICODE
66#define YAFFS_MAX_NAME_LENGTH 127
67#define YAFFS_MAX_ALIAS_LENGTH 79
68#else
69#define YAFFS_MAX_NAME_LENGTH 255
70#define YAFFS_MAX_ALIAS_LENGTH 159
71#endif
72
73#define YAFFS_SHORT_NAME_LENGTH 15
74
75/* Some special object ids for pseudo objects */
76#define YAFFS_OBJECTID_ROOT 1
77#define YAFFS_OBJECTID_LOSTNFOUND 2
78#define YAFFS_OBJECTID_UNLINKED 3
79#define YAFFS_OBJECTID_DELETED 4
80
81/* Pseudo object ids for checkpointing */
82#define YAFFS_OBJECTID_SB_HEADER 0x10
83#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
84#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
85
86#define YAFFS_MAX_SHORT_OP_CACHES 20
87
88#define YAFFS_N_TEMP_BUFFERS 6
89
90/* We limit the number attempts at sucessfully saving a chunk of data.
91 * Small-page devices have 32 pages per block; large-page devices have 64.
92 * Default to something in the order of 5 to 10 blocks worth of chunks.
93 */
94#define YAFFS_WR_ATTEMPTS (5*64)
95
96/* Sequence numbers are used in YAFFS2 to determine block allocation order.
97 * The range is limited slightly to help distinguish bad numbers from good.
98 * This also allows us to perhaps in the future use special numbers for
99 * special purposes.
100 * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
101 * and is a larger number than the lifetime of a 2GB device.
102 */
103#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
104#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
105
106/* Special sequence number for bad block that failed to be marked bad */
107#define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000
108
109/* ChunkCache is used for short read/write operations.*/
110struct yaffs_cache {
111 struct yaffs_obj *object;
112 int chunk_id;
113 int last_use;
114 int dirty;
115 int n_bytes; /* Only valid if the cache is dirty */
116 int locked; /* Can't push out or flush while locked. */
117 u8 *data;
118};
119
120/* Tags structures in RAM
121 * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
122 * the structure size will get blown out.
123 */
124
125#ifndef CONFIG_YAFFS_NO_YAFFS1
126struct yaffs_tags {
127 unsigned chunk_id:20;
128 unsigned serial_number:2;
129 unsigned n_bytes_lsb:10;
130 unsigned obj_id:18;
131 unsigned ecc:12;
132 unsigned n_bytes_msb:2;
133};
134
135union yaffs_tags_union {
136 struct yaffs_tags as_tags;
137 u8 as_bytes[8];
138};
139
140#endif
141
142/* Stuff used for extended tags in YAFFS2 */
143
144enum yaffs_ecc_result {
145 YAFFS_ECC_RESULT_UNKNOWN,
146 YAFFS_ECC_RESULT_NO_ERROR,
147 YAFFS_ECC_RESULT_FIXED,
148 YAFFS_ECC_RESULT_UNFIXED
149};
150
151enum yaffs_obj_type {
152 YAFFS_OBJECT_TYPE_UNKNOWN,
153 YAFFS_OBJECT_TYPE_FILE,
154 YAFFS_OBJECT_TYPE_SYMLINK,
155 YAFFS_OBJECT_TYPE_DIRECTORY,
156 YAFFS_OBJECT_TYPE_HARDLINK,
157 YAFFS_OBJECT_TYPE_SPECIAL
158};
159
160#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
161
162struct yaffs_ext_tags {
163
164 unsigned validity0;
165 unsigned chunk_used; /* Status of the chunk: used or unused */
166 unsigned obj_id; /* If 0 then this is not part of an object (unused) */
167 unsigned chunk_id; /* If 0 then this is a header, else a data chunk */
168 unsigned n_bytes; /* Only valid for data chunks */
169
170 /* The following stuff only has meaning when we read */
171 enum yaffs_ecc_result ecc_result;
172 unsigned block_bad;
173
174 /* YAFFS 1 stuff */
175 unsigned is_deleted; /* The chunk is marked deleted */
176 unsigned serial_number; /* Yaffs1 2-bit serial number */
177
178 /* YAFFS2 stuff */
179 unsigned seq_number; /* The sequence number of this block */
180
181 /* Extra info if this is an object header (YAFFS2 only) */
182
183 unsigned extra_available; /* There is extra info available if this is not zero */
184 unsigned extra_parent_id; /* The parent object */
185 unsigned extra_is_shrink; /* Is it a shrink header? */
186 unsigned extra_shadows; /* Does this shadow another object? */
187
188 enum yaffs_obj_type extra_obj_type; /* What object type? */
189
190 unsigned extra_length; /* Length if it is a file */
191 unsigned extra_equiv_id; /* Equivalent object Id if it is a hard link */
192
193 unsigned validity1;
194
195};
196
197/* Spare structure for YAFFS1 */
198struct yaffs_spare {
199 u8 tb0;
200 u8 tb1;
201 u8 tb2;
202 u8 tb3;
203 u8 page_status; /* set to 0 to delete the chunk */
204 u8 block_status;
205 u8 tb4;
206 u8 tb5;
207 u8 ecc1[3];
208 u8 tb6;
209 u8 tb7;
210 u8 ecc2[3];
211};
212
213/*Special structure for passing through to mtd */
214struct yaffs_nand_spare {
215 struct yaffs_spare spare;
216 int eccres1;
217 int eccres2;
218};
219
220/* Block data in RAM */
221
222enum yaffs_block_state {
223 YAFFS_BLOCK_STATE_UNKNOWN = 0,
224
225 YAFFS_BLOCK_STATE_SCANNING,
226 /* Being scanned */
227
228 YAFFS_BLOCK_STATE_NEEDS_SCANNING,
229 /* The block might have something on it (ie it is allocating or full, perhaps empty)
230 * but it needs to be scanned to determine its true state.
231 * This state is only valid during scanning.
232 * NB We tolerate empty because the pre-scanner might be incapable of deciding
233 * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
234 */
235
236 YAFFS_BLOCK_STATE_EMPTY,
237 /* This block is empty */
238
239 YAFFS_BLOCK_STATE_ALLOCATING,
240 /* This block is partially allocated.
241 * At least one page holds valid data.
242 * This is the one currently being used for page
243 * allocation. Should never be more than one of these.
244 * If a block is only partially allocated at mount it is treated as full.
245 */
246
247 YAFFS_BLOCK_STATE_FULL,
248 /* All the pages in this block have been allocated.
249 * If a block was only partially allocated when mounted we treat
250 * it as fully allocated.
251 */
252
253 YAFFS_BLOCK_STATE_DIRTY,
254 /* The block was full and now all chunks have been deleted.
255 * Erase me, reuse me.
256 */
257
258 YAFFS_BLOCK_STATE_CHECKPOINT,
259 /* This block is assigned to holding checkpoint data. */
260
261 YAFFS_BLOCK_STATE_COLLECTING,
262 /* This block is being garbage collected */
263
264 YAFFS_BLOCK_STATE_DEAD
265 /* This block has failed and is not in use */
266};
267
268#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
269
270struct yaffs_block_info {
271
272 int soft_del_pages:10; /* number of soft deleted pages */
273 int pages_in_use:10; /* number of pages in use */
274 unsigned block_state:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
275 u32 needs_retiring:1; /* Data has failed on this block, need to get valid data off */
276 /* and retire the block. */
277 u32 skip_erased_check:1; /* If this is set we can skip the erased check on this block */
278 u32 gc_prioritise:1; /* An ECC check or blank check has failed on this block.
279 It should be prioritised for GC */
280 u32 chunk_error_strikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
281
282#ifdef CONFIG_YAFFS_YAFFS2
283 u32 has_shrink_hdr:1; /* This block has at least one shrink object header */
284 u32 seq_number; /* block sequence number for yaffs2 */
285#endif
286
287};
288
289/* -------------------------- Object structure -------------------------------*/
290/* This is the object structure as stored on NAND */
291
292struct yaffs_obj_hdr {
293 enum yaffs_obj_type type;
294
295 /* Apply to everything */
296 int parent_obj_id;
297 u16 sum_no_longer_used; /* checksum of name. No longer used */
298 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
299
300 /* The following apply to directories, files, symlinks - not hard links */
301 u32 yst_mode; /* protection */
302
303 u32 yst_uid;
304 u32 yst_gid;
305 u32 yst_atime;
306 u32 yst_mtime;
307 u32 yst_ctime;
308
309 /* File size applies to files only */
310 int file_size;
311
312 /* Equivalent object id applies to hard links only. */
313 int equiv_id;
314
315 /* Alias is for symlinks only. */
316 YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
317
318 u32 yst_rdev; /* device stuff for block and char devices (major/min) */
319
320 u32 win_ctime[2];
321 u32 win_atime[2];
322 u32 win_mtime[2];
323
324 u32 inband_shadowed_obj_id;
325 u32 inband_is_shrink;
326
327 u32 reserved[2];
328 int shadows_obj; /* This object header shadows the specified object if > 0 */
329
330 /* is_shrink applies to object headers written when we shrink the file (ie resize) */
331 u32 is_shrink;
332
333};
334
335/*--------------------------- Tnode -------------------------- */
336
337struct yaffs_tnode {
338 struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
339};
340
341/*------------------------ Object -----------------------------*/
342/* An object can be one of:
343 * - a directory (no data, has children links
344 * - a regular file (data.... not prunes :->).
345 * - a symlink [symbolic link] (the alias).
346 * - a hard link
347 */
348
349struct yaffs_file_var {
350 u32 file_size;
351 u32 scanned_size;
352 u32 shrink_size;
353 int top_level;
354 struct yaffs_tnode *top;
355};
356
357struct yaffs_dir_var {
358 struct list_head children; /* list of child links */
359 struct list_head dirty; /* Entry for list of dirty directories */
360};
361
362struct yaffs_symlink_var {
363 YCHAR *alias;
364};
365
366struct yaffs_hardlink_var {
367 struct yaffs_obj *equiv_obj;
368 u32 equiv_id;
369};
370
371union yaffs_obj_var {
372 struct yaffs_file_var file_variant;
373 struct yaffs_dir_var dir_variant;
374 struct yaffs_symlink_var symlink_variant;
375 struct yaffs_hardlink_var hardlink_variant;
376};
377
378struct yaffs_obj {
379 u8 deleted:1; /* This should only apply to unlinked files. */
380 u8 soft_del:1; /* it has also been soft deleted */
381 u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory. */
382 u8 fake:1; /* A fake object has no presence on NAND. */
383 u8 rename_allowed:1; /* Some objects are not allowed to be renamed. */
384 u8 unlink_allowed:1;
385 u8 dirty:1; /* the object needs to be written to flash */
386 u8 valid:1; /* When the file system is being loaded up, this
387 * object might be created before the data
388 * is available (ie. file data records appear before the header).
389 */
390 u8 lazy_loaded:1; /* This object has been lazy loaded and is missing some detail */
391
392 u8 defered_free:1; /* For Linux kernel. Object is removed from NAND, but is
393 * still in the inode cache. Free of object is defered.
394 * until the inode is released.
395 */
396 u8 being_created:1; /* This object is still being created so skip some checks. */
397 u8 is_shadowed:1; /* This object is shadowed on the way to being renamed. */
398
399 u8 xattr_known:1; /* We know if this has object has xattribs or not. */
400 u8 has_xattr:1; /* This object has xattribs. Valid if xattr_known. */
401
402 u8 serial; /* serial number of chunk in NAND. Cached here */
403 u16 sum; /* sum of the name to speed searching */
404
405 struct yaffs_dev *my_dev; /* The device I'm on */
406
407 struct list_head hash_link; /* list of objects in this hash bucket */
408
409 struct list_head hard_links; /* all the equivalent hard linked objects */
410
411 /* directory structure stuff */
412 /* also used for linking up the free list */
413 struct yaffs_obj *parent;
414 struct list_head siblings;
415
416 /* Where's my object header in NAND? */
417 int hdr_chunk;
418
419 int n_data_chunks; /* Number of data chunks attached to the file. */
420
421 u32 obj_id; /* the object id value */
422
423 u32 yst_mode;
424
425#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
426 YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
427#endif
428
429#ifdef CONFIG_YAFFS_WINCE
430 u32 win_ctime[2];
431 u32 win_mtime[2];
432 u32 win_atime[2];
433#else
434 u32 yst_uid;
435 u32 yst_gid;
436 u32 yst_atime;
437 u32 yst_mtime;
438 u32 yst_ctime;
439#endif
440
441 u32 yst_rdev;
442
443 void *my_inode;
444
445 enum yaffs_obj_type variant_type;
446
447 union yaffs_obj_var variant;
448
449};
450
451struct yaffs_obj_bucket {
452 struct list_head list;
453 int count;
454};
455
456/* yaffs_checkpt_obj holds the definition of an object as dumped
457 * by checkpointing.
458 */
459
460struct yaffs_checkpt_obj {
461 int struct_type;
462 u32 obj_id;
463 u32 parent_id;
464 int hdr_chunk;
465 enum yaffs_obj_type variant_type:3;
466 u8 deleted:1;
467 u8 soft_del:1;
468 u8 unlinked:1;
469 u8 fake:1;
470 u8 rename_allowed:1;
471 u8 unlink_allowed:1;
472 u8 serial;
473 int n_data_chunks;
474 u32 size_or_equiv_obj;
475};
476
477/*--------------------- Temporary buffers ----------------
478 *
479 * These are chunk-sized working buffers. Each device has a few
480 */
481
482struct yaffs_buffer {
483 u8 *buffer;
484 int line; /* track from whence this buffer was allocated */
485 int max_line;
486};
487
488/*----------------- Device ---------------------------------*/
489
490struct yaffs_param {
491 const YCHAR *name;
492
493 /*
494 * Entry parameters set up way early. Yaffs sets up the rest.
495 * The structure should be zeroed out before use so that unused
496 * and defualt values are zero.
497 */
498
499 int inband_tags; /* Use unband tags */
500 u32 total_bytes_per_chunk; /* Should be >= 512, does not need to be a power of 2 */
501 int chunks_per_block; /* does not need to be a power of 2 */
502 int spare_bytes_per_chunk; /* spare area size */
503 int start_block; /* Start block we're allowed to use */
504 int end_block; /* End block we're allowed to use */
505 int n_reserved_blocks; /* We want this tuneable so that we can reduce */
506 /* reserved blocks on NOR and RAM. */
507
508 int n_caches; /* If <= 0, then short op caching is disabled, else
509 * the number of short op caches (don't use too many).
510 * 10 to 20 is a good bet.
511 */
512 int use_nand_ecc; /* Flag to decide whether or not to use NANDECC on data (yaffs1) */
513 int no_tags_ecc; /* Flag to decide whether or not to do ECC on packed tags (yaffs2) */
514
515 int is_yaffs2; /* Use yaffs2 mode on this device */
516
517 int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
518
519 int refresh_period; /* How often we should check to do a block refresh */
520
521 /* Checkpoint control. Can be set before or after initialisation */
522 u8 skip_checkpt_rd;
523 u8 skip_checkpt_wr;
524
525 int enable_xattr; /* Enable xattribs */
526
527 /* NAND access functions (Must be set before calling YAFFS) */
528
529 int (*write_chunk_fn) (struct yaffs_dev * dev,
530 int nand_chunk, const u8 * data,
531 const struct yaffs_spare * spare);
532 int (*read_chunk_fn) (struct yaffs_dev * dev,
533 int nand_chunk, u8 * data,
534 struct yaffs_spare * spare);
535 int (*erase_fn) (struct yaffs_dev * dev, int flash_block);
536 int (*initialise_flash_fn) (struct yaffs_dev * dev);
537 int (*deinitialise_flash_fn) (struct yaffs_dev * dev);
538
539#ifdef CONFIG_YAFFS_YAFFS2
540 int (*write_chunk_tags_fn) (struct yaffs_dev * dev,
541 int nand_chunk, const u8 * data,
542 const struct yaffs_ext_tags * tags);
543 int (*read_chunk_tags_fn) (struct yaffs_dev * dev,
544 int nand_chunk, u8 * data,
545 struct yaffs_ext_tags * tags);
546 int (*bad_block_fn) (struct yaffs_dev * dev, int block_no);
547 int (*query_block_fn) (struct yaffs_dev * dev, int block_no,
548 enum yaffs_block_state * state,
549 u32 * seq_number);
550#endif
551
552 /* The remove_obj_fn function must be supplied by OS flavours that
553 * need it.
554 * yaffs direct uses it to implement the faster readdir.
555 * Linux uses it to protect the directory during unlocking.
556 */
557 void (*remove_obj_fn) (struct yaffs_obj * obj);
558
559 /* Callback to mark the superblock dirty */
560 void (*sb_dirty_fn) (struct yaffs_dev * dev);
561
562 /* Callback to control garbage collection. */
563 unsigned (*gc_control) (struct yaffs_dev * dev);
564
565 /* Debug control flags. Don't use unless you know what you're doing */
566 int use_header_file_size; /* Flag to determine if we should use file sizes from the header */
567 int disable_lazy_load; /* Disable lazy loading on this device */
568 int wide_tnodes_disabled; /* Set to disable wide tnodes */
569 int disable_soft_del; /* yaffs 1 only: Set to disable the use of softdeletion. */
570
571 int defered_dir_update; /* Set to defer directory updates */
572
573#ifdef CONFIG_YAFFS_AUTO_UNICODE
574 int auto_unicode;
575#endif
576 int always_check_erased; /* Force chunk erased check always on */
577};
578
579struct yaffs_dev {
580 struct yaffs_param param;
581
582 /* Context storage. Holds extra OS specific data for this device */
583
584 void *os_context;
585 void *driver_context;
586
587 struct list_head dev_list;
588
589 /* Runtime parameters. Set up by YAFFS. */
590 int data_bytes_per_chunk;
591
592 /* Non-wide tnode stuff */
593 u16 chunk_grp_bits; /* Number of bits that need to be resolved if
594 * the tnodes are not wide enough.
595 */
596 u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
597
598 /* Stuff to support wide tnodes */
599 u32 tnode_width;
600 u32 tnode_mask;
601 u32 tnode_size;
602
603 /* Stuff for figuring out file offset to chunk conversions */
604 u32 chunk_shift; /* Shift value */
605 u32 chunk_div; /* Divisor after shifting: 1 for power-of-2 sizes */
606 u32 chunk_mask; /* Mask to use for power-of-2 case */
607
608 int is_mounted;
609 int read_only;
610 int is_checkpointed;
611
612 /* Stuff to support block offsetting to support start block zero */
613 int internal_start_block;
614 int internal_end_block;
615 int block_offset;
616 int chunk_offset;
617
618 /* Runtime checkpointing stuff */
619 int checkpt_page_seq; /* running sequence number of checkpoint pages */
620 int checkpt_byte_count;
621 int checkpt_byte_offs;
622 u8 *checkpt_buffer;
623 int checkpt_open_write;
624 int blocks_in_checkpt;
625 int checkpt_cur_chunk;
626 int checkpt_cur_block;
627 int checkpt_next_block;
628 int *checkpt_block_list;
629 int checkpt_max_blocks;
630 u32 checkpt_sum;
631 u32 checkpt_xor;
632
633 int checkpoint_blocks_required; /* Number of blocks needed to store current checkpoint set */
634
635 /* Block Info */
636 struct yaffs_block_info *block_info;
637 u8 *chunk_bits; /* bitmap of chunks in use */
638 unsigned block_info_alt:1; /* was allocated using alternative strategy */
639 unsigned chunk_bits_alt:1; /* was allocated using alternative strategy */
640 int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
641 * Must be consistent with chunks_per_block.
642 */
643
644 int n_erased_blocks;
645 int alloc_block; /* Current block being allocated off */
646 u32 alloc_page;
647 int alloc_block_finder; /* Used to search for next allocation block */
648
649 /* Object and Tnode memory management */
650 void *allocator;
651 int n_obj;
652 int n_tnodes;
653
654 int n_hardlinks;
655
656 struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
657 u32 bucket_finder;
658
659 int n_free_chunks;
660
661 /* Garbage collection control */
662 u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
663 u32 n_clean_ups;
664
665 unsigned has_pending_prioritised_gc; /* We think this device might have pending prioritised gcs */
666 unsigned gc_disable;
667 unsigned gc_block_finder;
668 unsigned gc_dirtiest;
669 unsigned gc_pages_in_use;
670 unsigned gc_not_done;
671 unsigned gc_block;
672 unsigned gc_chunk;
673 unsigned gc_skip;
674
675 /* Special directories */
676 struct yaffs_obj *root_dir;
677 struct yaffs_obj *lost_n_found;
678
679 /* Buffer areas for storing data to recover from write failures TODO
680 * u8 buffered_data[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
681 * struct yaffs_spare buffered_spare[YAFFS_CHUNKS_PER_BLOCK];
682 */
683
684 int buffered_block; /* Which block is buffered here? */
685 int doing_buffered_block_rewrite;
686
687 struct yaffs_cache *cache;
688 int cache_last_use;
689
690 /* Stuff for background deletion and unlinked files. */
691 struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted files live. */
692 struct yaffs_obj *del_dir; /* Directory where deleted objects are sent to disappear. */
693 struct yaffs_obj *unlinked_deletion; /* Current file being background deleted. */
694 int n_deleted_files; /* Count of files awaiting deletion; */
695 int n_unlinked_files; /* Count of unlinked files. */
696 int n_bg_deletions; /* Count of background deletions. */
697
698 /* Temporary buffer management */
699 struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
700 int max_temp;
701 int temp_in_use;
702 int unmanaged_buffer_allocs;
703 int unmanaged_buffer_deallocs;
704
705 /* yaffs2 runtime stuff */
706 unsigned seq_number; /* Sequence number of currently allocating block */
707 unsigned oldest_dirty_seq;
708 unsigned oldest_dirty_block;
709
710 /* Block refreshing */
711 int refresh_skip; /* A skip down counter. Refresh happens when this gets to zero. */
712
713 /* Dirty directory handling */
714 struct list_head dirty_dirs; /* List of dirty directories */
715
716 /* Statistcs */
717 u32 n_page_writes;
718 u32 n_page_reads;
719 u32 n_erasures;
720 u32 n_erase_failures;
721 u32 n_gc_copies;
722 u32 all_gcs;
723 u32 passive_gc_count;
724 u32 oldest_dirty_gc_count;
725 u32 n_gc_blocks;
726 u32 bg_gcs;
727 u32 n_retired_writes;
728 u32 n_retired_blocks;
729 u32 n_ecc_fixed;
730 u32 n_ecc_unfixed;
731 u32 n_tags_ecc_fixed;
732 u32 n_tags_ecc_unfixed;
733 u32 n_deletions;
734 u32 n_unmarked_deletions;
735 u32 refresh_count;
736 u32 cache_hits;
737
738};
739
740/* The CheckpointDevice structure holds the device information that changes at runtime and
741 * must be preserved over unmount/mount cycles.
742 */
743struct yaffs_checkpt_dev {
744 int struct_type;
745 int n_erased_blocks;
746 int alloc_block; /* Current block being allocated off */
747 u32 alloc_page;
748 int n_free_chunks;
749
750 int n_deleted_files; /* Count of files awaiting deletion; */
751 int n_unlinked_files; /* Count of unlinked files. */
752 int n_bg_deletions; /* Count of background deletions. */
753
754 /* yaffs2 runtime stuff */
755 unsigned seq_number; /* Sequence number of currently allocating block */
756
757};
758
759struct yaffs_checkpt_validity {
760 int struct_type;
761 u32 magic;
762 u32 version;
763 u32 head;
764};
765
766struct yaffs_shadow_fixer {
767 int obj_id;
768 int shadowed_id;
769 struct yaffs_shadow_fixer *next;
770};
771
772/* Structure for doing xattr modifications */
773struct yaffs_xattr_mod {
774 int set; /* If 0 then this is a deletion */
775 const YCHAR *name;
776 const void *data;
777 int size;
778 int flags;
779 int result;
780};
781
782/*----------------------- YAFFS Functions -----------------------*/
783
784int yaffs_guts_initialise(struct yaffs_dev *dev);
785void yaffs_deinitialise(struct yaffs_dev *dev);
786
787int yaffs_get_n_free_chunks(struct yaffs_dev *dev);
788
789int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
790 struct yaffs_obj *new_dir, const YCHAR * new_name);
791
792int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name);
793int yaffs_del_obj(struct yaffs_obj *obj);
794
795int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size);
796int yaffs_get_obj_length(struct yaffs_obj *obj);
797int yaffs_get_obj_inode(struct yaffs_obj *obj);
798unsigned yaffs_get_obj_type(struct yaffs_obj *obj);
799int yaffs_get_obj_link_count(struct yaffs_obj *obj);
800
801/* File operations */
802int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset,
803 int n_bytes);
804int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset,
805 int n_bytes, int write_trhrough);
806int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size);
807
808struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
809 const YCHAR * name, u32 mode, u32 uid,
810 u32 gid);
811
812int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync);
813
814/* Flushing and checkpointing */
815void yaffs_flush_whole_cache(struct yaffs_dev *dev);
816
817int yaffs_checkpoint_save(struct yaffs_dev *dev);
818int yaffs_checkpoint_restore(struct yaffs_dev *dev);
819
820/* Directory operations */
821struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
822 u32 mode, u32 uid, u32 gid);
823struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir,
824 const YCHAR * name);
825struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number);
826
827/* Link operations */
828struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
829 struct yaffs_obj *equiv_obj);
830
831struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj);
832
833/* Symlink operations */
834struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
835 const YCHAR * name, u32 mode, u32 uid,
836 u32 gid, const YCHAR * alias);
837YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj);
838
839/* Special inodes (fifos, sockets and devices) */
840struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
841 const YCHAR * name, u32 mode, u32 uid,
842 u32 gid, u32 rdev);
843
844int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
845 const void *value, int size, int flags);
846int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
847 int size);
848int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size);
849int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name);
850
851/* Special directories */
852struct yaffs_obj *yaffs_root(struct yaffs_dev *dev);
853struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev);
854
855void yaffs_handle_defered_free(struct yaffs_obj *obj);
856
857void yaffs_update_dirty_dirs(struct yaffs_dev *dev);
858
859int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency);
860
861/* Debug dump */
862int yaffs_dump_obj(struct yaffs_obj *obj);
863
864void yaffs_guts_test(struct yaffs_dev *dev);
865
866/* A few useful functions to be used within the core files*/
867void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
868 int lyn);
869int yaffs_check_ff(u8 * buffer, int n_bytes);
870void yaffs_handle_chunk_error(struct yaffs_dev *dev,
871 struct yaffs_block_info *bi);
872
873u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev, int line_no);
874void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no);
875
876struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
877 int number,
878 enum yaffs_obj_type type);
879int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
880 int nand_chunk, int in_scan);
881void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name);
882void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
883 const struct yaffs_obj_hdr *oh);
884void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj);
885YCHAR *yaffs_clone_str(const YCHAR * str);
886void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list);
887void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no);
888int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name,
889 int force, int is_shrink, int shadows,
890 struct yaffs_xattr_mod *xop);
891void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
892 int backward_scanning);
893int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks);
894struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev);
895struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
896 struct yaffs_file_var *file_struct,
897 u32 chunk_id,
898 struct yaffs_tnode *passed_tn);
899
900int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
901 int n_bytes, int write_trhrough);
902void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size);
903void yaffs_skip_rest_of_block(struct yaffs_dev *dev);
904
905int yaffs_count_free_chunks(struct yaffs_dev *dev);
906
907struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
908 struct yaffs_file_var *file_struct,
909 u32 chunk_id);
910
911u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
912 unsigned pos);
913
914int yaffs_is_non_empty_dir(struct yaffs_obj *obj);
915#endif
diff --git a/fs/yaffs2/yaffs_linux.h b/fs/yaffs2/yaffs_linux.h
new file mode 100644
index 00000000000..3b508cbc4e8
--- /dev/null
+++ b/fs/yaffs2/yaffs_linux.h
@@ -0,0 +1,41 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_LINUX_H__
17#define __YAFFS_LINUX_H__
18
19#include "yportenv.h"
20
21struct yaffs_linux_context {
22 struct list_head context_list; /* List of these we have mounted */
23 struct yaffs_dev *dev;
24 struct super_block *super;
25 struct task_struct *bg_thread; /* Background thread for this device */
26 int bg_running;
27 struct mutex gross_lock; /* Gross locking mutex*/
28 u8 *spare_buffer; /* For mtdif2 use. Don't know the size of the buffer
29 * at compile time so we have to allocate it.
30 */
31 struct list_head search_contexts;
32 void (*put_super_fn) (struct super_block * sb);
33
34 struct task_struct *readdir_process;
35 unsigned mount_id;
36};
37
38#define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
39#define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
40
41#endif
diff --git a/fs/yaffs2/yaffs_mtdif.c b/fs/yaffs2/yaffs_mtdif.c
new file mode 100644
index 00000000000..7cf53b3d91b
--- /dev/null
+++ b/fs/yaffs2/yaffs_mtdif.c
@@ -0,0 +1,54 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yportenv.h"
15
16#include "yaffs_mtdif.h"
17
18#include "linux/mtd/mtd.h"
19#include "linux/types.h"
20#include "linux/time.h"
21#include "linux/mtd/nand.h"
22
23#include "yaffs_linux.h"
24
25int nandmtd_erase_block(struct yaffs_dev *dev, int block_no)
26{
27 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
28 u32 addr =
29 ((loff_t) block_no) * dev->param.total_bytes_per_chunk
30 * dev->param.chunks_per_block;
31 struct erase_info ei;
32
33 int retval = 0;
34
35 ei.mtd = mtd;
36 ei.addr = addr;
37 ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
38 ei.time = 1000;
39 ei.retries = 2;
40 ei.callback = NULL;
41 ei.priv = (u_long) dev;
42
43 retval = mtd->erase(mtd, &ei);
44
45 if (retval == 0)
46 return YAFFS_OK;
47 else
48 return YAFFS_FAIL;
49}
50
51int nandmtd_initialise(struct yaffs_dev *dev)
52{
53 return YAFFS_OK;
54}
diff --git a/fs/yaffs2/yaffs_mtdif.h b/fs/yaffs2/yaffs_mtdif.h
new file mode 100644
index 00000000000..666507417fe
--- /dev/null
+++ b/fs/yaffs2/yaffs_mtdif.h
@@ -0,0 +1,23 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_MTDIF_H__
17#define __YAFFS_MTDIF_H__
18
19#include "yaffs_guts.h"
20
21int nandmtd_erase_block(struct yaffs_dev *dev, int block_no);
22int nandmtd_initialise(struct yaffs_dev *dev);
23#endif
diff --git a/fs/yaffs2/yaffs_mtdif1.c b/fs/yaffs2/yaffs_mtdif1.c
new file mode 100644
index 00000000000..51083695eb3
--- /dev/null
+++ b/fs/yaffs2/yaffs_mtdif1.c
@@ -0,0 +1,330 @@
1/*
2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/*
15 * This module provides the interface between yaffs_nand.c and the
16 * MTD API. This version is used when the MTD interface supports the
17 * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
18 * and we have small-page NAND device.
19 *
20 * These functions are invoked via function pointers in yaffs_nand.c.
21 * This replaces functionality provided by functions in yaffs_mtdif.c
22 * and the yaffs_tags compatability functions in yaffs_tagscompat.c that are
23 * called in yaffs_mtdif.c when the function pointers are NULL.
24 * We assume the MTD layer is performing ECC (use_nand_ecc is true).
25 */
26
27#include "yportenv.h"
28#include "yaffs_trace.h"
29#include "yaffs_guts.h"
30#include "yaffs_packedtags1.h"
31#include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */
32#include "yaffs_linux.h"
33
34#include "linux/kernel.h"
35#include "linux/version.h"
36#include "linux/types.h"
37#include "linux/mtd/mtd.h"
38
39#ifndef CONFIG_YAFFS_9BYTE_TAGS
40# define YTAG1_SIZE 8
41#else
42# define YTAG1_SIZE 9
43#endif
44
45/* Write a chunk (page) of data to NAND.
46 *
47 * Caller always provides ExtendedTags data which are converted to a more
48 * compact (packed) form for storage in NAND. A mini-ECC runs over the
49 * contents of the tags meta-data; used to valid the tags when read.
50 *
51 * - Pack ExtendedTags to packed_tags1 form
52 * - Compute mini-ECC for packed_tags1
53 * - Write data and packed tags to NAND.
54 *
55 * Note: Due to the use of the packed_tags1 meta-data which does not include
56 * a full sequence number (as found in the larger packed_tags2 form) it is
57 * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
58 * discarded and dirty. This is not ideal: newer NAND parts are supposed
59 * to be written just once. When Yaffs performs this operation, this
60 * function is called with a NULL data pointer -- calling MTD write_oob
61 * without data is valid usage (2.6.17).
62 *
63 * Any underlying MTD error results in YAFFS_FAIL.
64 * Returns YAFFS_OK or YAFFS_FAIL.
65 */
66int nandmtd1_write_chunk_tags(struct yaffs_dev *dev,
67 int nand_chunk, const u8 * data,
68 const struct yaffs_ext_tags *etags)
69{
70 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
71 int chunk_bytes = dev->data_bytes_per_chunk;
72 loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
73 struct mtd_oob_ops ops;
74 struct yaffs_packed_tags1 pt1;
75 int retval;
76
77 /* we assume that packed_tags1 and struct yaffs_tags are compatible */
78 compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12);
79 compile_time_assertion(sizeof(struct yaffs_tags) == 8);
80
81 yaffs_pack_tags1(&pt1, etags);
82 yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
83
84 /* When deleting a chunk, the upper layer provides only skeletal
85 * etags, one with is_deleted set. However, we need to update the
86 * tags, not erase them completely. So we use the NAND write property
87 * that only zeroed-bits stick and set tag bytes to all-ones and
88 * zero just the (not) deleted bit.
89 */
90#ifndef CONFIG_YAFFS_9BYTE_TAGS
91 if (etags->is_deleted) {
92 memset(&pt1, 0xff, 8);
93 /* clear delete status bit to indicate deleted */
94 pt1.deleted = 0;
95 }
96#else
97 ((u8 *) & pt1)[8] = 0xff;
98 if (etags->is_deleted) {
99 memset(&pt1, 0xff, 8);
100 /* zero page_status byte to indicate deleted */
101 ((u8 *) & pt1)[8] = 0;
102 }
103#endif
104
105 memset(&ops, 0, sizeof(ops));
106 ops.mode = MTD_OOB_AUTO;
107 ops.len = (data) ? chunk_bytes : 0;
108 ops.ooblen = YTAG1_SIZE;
109 ops.datbuf = (u8 *) data;
110 ops.oobbuf = (u8 *) & pt1;
111
112 retval = mtd->write_oob(mtd, addr, &ops);
113 if (retval) {
114 yaffs_trace(YAFFS_TRACE_MTD,
115 "write_oob failed, chunk %d, mtd error %d",
116 nand_chunk, retval);
117 }
118 return retval ? YAFFS_FAIL : YAFFS_OK;
119}
120
121/* Return with empty ExtendedTags but add ecc_result.
122 */
123static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval)
124{
125 if (etags) {
126 memset(etags, 0, sizeof(*etags));
127 etags->ecc_result = ecc_result;
128 }
129 return retval;
130}
131
132/* Read a chunk (page) from NAND.
133 *
134 * Caller expects ExtendedTags data to be usable even on error; that is,
135 * all members except ecc_result and block_bad are zeroed.
136 *
137 * - Check ECC results for data (if applicable)
138 * - Check for blank/erased block (return empty ExtendedTags if blank)
139 * - Check the packed_tags1 mini-ECC (correct if necessary/possible)
140 * - Convert packed_tags1 to ExtendedTags
141 * - Update ecc_result and block_bad members to refect state.
142 *
143 * Returns YAFFS_OK or YAFFS_FAIL.
144 */
145int nandmtd1_read_chunk_tags(struct yaffs_dev *dev,
146 int nand_chunk, u8 * data,
147 struct yaffs_ext_tags *etags)
148{
149 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
150 int chunk_bytes = dev->data_bytes_per_chunk;
151 loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
152 int eccres = YAFFS_ECC_RESULT_NO_ERROR;
153 struct mtd_oob_ops ops;
154 struct yaffs_packed_tags1 pt1;
155 int retval;
156 int deleted;
157
158 memset(&ops, 0, sizeof(ops));
159 ops.mode = MTD_OOB_AUTO;
160 ops.len = (data) ? chunk_bytes : 0;
161 ops.ooblen = YTAG1_SIZE;
162 ops.datbuf = data;
163 ops.oobbuf = (u8 *) & pt1;
164
165 /* Read page and oob using MTD.
166 * Check status and determine ECC result.
167 */
168 retval = mtd->read_oob(mtd, addr, &ops);
169 if (retval) {
170 yaffs_trace(YAFFS_TRACE_MTD,
171 "read_oob failed, chunk %d, mtd error %d",
172 nand_chunk, retval);
173 }
174
175 switch (retval) {
176 case 0:
177 /* no error */
178 break;
179
180 case -EUCLEAN:
181 /* MTD's ECC fixed the data */
182 eccres = YAFFS_ECC_RESULT_FIXED;
183 dev->n_ecc_fixed++;
184 break;
185
186 case -EBADMSG:
187 /* MTD's ECC could not fix the data */
188 dev->n_ecc_unfixed++;
189 /* fall into... */
190 default:
191 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
192 etags->block_bad = (mtd->block_isbad) (mtd, addr);
193 return YAFFS_FAIL;
194 }
195
196 /* Check for a blank/erased chunk.
197 */
198 if (yaffs_check_ff((u8 *) & pt1, 8)) {
199 /* when blank, upper layers want ecc_result to be <= NO_ERROR */
200 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
201 }
202#ifndef CONFIG_YAFFS_9BYTE_TAGS
203 /* Read deleted status (bit) then return it to it's non-deleted
204 * state before performing tags mini-ECC check. pt1.deleted is
205 * inverted.
206 */
207 deleted = !pt1.deleted;
208 pt1.deleted = 1;
209#else
210 deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7);
211#endif
212
213 /* Check the packed tags mini-ECC and correct if necessary/possible.
214 */
215 retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1);
216 switch (retval) {
217 case 0:
218 /* no tags error, use MTD result */
219 break;
220 case 1:
221 /* recovered tags-ECC error */
222 dev->n_tags_ecc_fixed++;
223 if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
224 eccres = YAFFS_ECC_RESULT_FIXED;
225 break;
226 default:
227 /* unrecovered tags-ECC error */
228 dev->n_tags_ecc_unfixed++;
229 return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
230 }
231
232 /* Unpack the tags to extended form and set ECC result.
233 * [set should_be_ff just to keep yaffs_unpack_tags1 happy]
234 */
235 pt1.should_be_ff = 0xFFFFFFFF;
236 yaffs_unpack_tags1(etags, &pt1);
237 etags->ecc_result = eccres;
238
239 /* Set deleted state */
240 etags->is_deleted = deleted;
241 return YAFFS_OK;
242}
243
244/* Mark a block bad.
245 *
246 * This is a persistant state.
247 * Use of this function should be rare.
248 *
249 * Returns YAFFS_OK or YAFFS_FAIL.
250 */
251int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no)
252{
253 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
254 int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
255 int retval;
256
257 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
258 "marking block %d bad", block_no);
259
260 retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no);
261 return (retval) ? YAFFS_FAIL : YAFFS_OK;
262}
263
264/* Check any MTD prerequists.
265 *
266 * Returns YAFFS_OK or YAFFS_FAIL.
267 */
268static int nandmtd1_test_prerequists(struct mtd_info *mtd)
269{
270 /* 2.6.18 has mtd->ecclayout->oobavail */
271 /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
272 int oobavail = mtd->ecclayout->oobavail;
273
274 if (oobavail < YTAG1_SIZE) {
275 yaffs_trace(YAFFS_TRACE_ERROR,
276 "mtd device has only %d bytes for tags, need %d",
277 oobavail, YTAG1_SIZE);
278 return YAFFS_FAIL;
279 }
280 return YAFFS_OK;
281}
282
283/* Query for the current state of a specific block.
284 *
285 * Examine the tags of the first chunk of the block and return the state:
286 * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
287 * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
288 * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
289 *
290 * Always returns YAFFS_OK.
291 */
292int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
293 enum yaffs_block_state *state_ptr, u32 * seq_ptr)
294{
295 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
296 int chunk_num = block_no * dev->param.chunks_per_block;
297 loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk;
298 struct yaffs_ext_tags etags;
299 int state = YAFFS_BLOCK_STATE_DEAD;
300 int seqnum = 0;
301 int retval;
302
303 /* We don't yet have a good place to test for MTD config prerequists.
304 * Do it here as we are called during the initial scan.
305 */
306 if (nandmtd1_test_prerequists(mtd) != YAFFS_OK)
307 return YAFFS_FAIL;
308
309 retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags);
310 etags.block_bad = (mtd->block_isbad) (mtd, addr);
311 if (etags.block_bad) {
312 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
313 "block %d is marked bad", block_no);
314 state = YAFFS_BLOCK_STATE_DEAD;
315 } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
316 /* bad tags, need to look more closely */
317 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
318 } else if (etags.chunk_used) {
319 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
320 seqnum = etags.seq_number;
321 } else {
322 state = YAFFS_BLOCK_STATE_EMPTY;
323 }
324
325 *state_ptr = state;
326 *seq_ptr = seqnum;
327
328 /* query always succeeds */
329 return YAFFS_OK;
330}
diff --git a/fs/yaffs2/yaffs_mtdif1.h b/fs/yaffs2/yaffs_mtdif1.h
new file mode 100644
index 00000000000..07ce4524f0f
--- /dev/null
+++ b/fs/yaffs2/yaffs_mtdif1.h
@@ -0,0 +1,29 @@
1/*
2 * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License version 2.1 as
9 * published by the Free Software Foundation.
10 *
11 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12 */
13
14#ifndef __YAFFS_MTDIF1_H__
15#define __YAFFS_MTDIF1_H__
16
17int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
18 const u8 * data,
19 const struct yaffs_ext_tags *tags);
20
21int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
22 u8 * data, struct yaffs_ext_tags *tags);
23
24int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no);
25
26int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
27 enum yaffs_block_state *state, u32 * seq_number);
28
29#endif
diff --git a/fs/yaffs2/yaffs_mtdif2.c b/fs/yaffs2/yaffs_mtdif2.c
new file mode 100644
index 00000000000..d1643df2c38
--- /dev/null
+++ b/fs/yaffs2/yaffs_mtdif2.c
@@ -0,0 +1,225 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* mtd interface for YAFFS2 */
15
16#include "yportenv.h"
17#include "yaffs_trace.h"
18
19#include "yaffs_mtdif2.h"
20
21#include "linux/mtd/mtd.h"
22#include "linux/types.h"
23#include "linux/time.h"
24
25#include "yaffs_packedtags2.h"
26
27#include "yaffs_linux.h"
28
29/* NB For use with inband tags....
30 * We assume that the data buffer is of size total_bytes_per_chunk so that we can also
31 * use it to load the tags.
32 */
33int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
34 const u8 * data,
35 const struct yaffs_ext_tags *tags)
36{
37 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
38 struct mtd_oob_ops ops;
39 int retval = 0;
40
41 loff_t addr;
42
43 struct yaffs_packed_tags2 pt;
44
45 int packed_tags_size =
46 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
47 void *packed_tags_ptr =
48 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
49
50 yaffs_trace(YAFFS_TRACE_MTD,
51 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
52 nand_chunk, data, tags);
53
54 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
55
56 /* For yaffs2 writing there must be both data and tags.
57 * If we're using inband tags, then the tags are stuffed into
58 * the end of the data buffer.
59 */
60 if (!data || !tags)
61 BUG();
62 else if (dev->param.inband_tags) {
63 struct yaffs_packed_tags2_tags_only *pt2tp;
64 pt2tp =
65 (struct yaffs_packed_tags2_tags_only *)(data +
66 dev->
67 data_bytes_per_chunk);
68 yaffs_pack_tags2_tags_only(pt2tp, tags);
69 } else {
70 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
71 }
72
73 ops.mode = MTD_OOB_AUTO;
74 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
75 ops.len = dev->param.total_bytes_per_chunk;
76 ops.ooboffs = 0;
77 ops.datbuf = (u8 *) data;
78 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
79 retval = mtd->write_oob(mtd, addr, &ops);
80
81 if (retval == 0)
82 return YAFFS_OK;
83 else
84 return YAFFS_FAIL;
85}
86
87int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
88 u8 * data, struct yaffs_ext_tags *tags)
89{
90 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
91 struct mtd_oob_ops ops;
92
93 size_t dummy;
94 int retval = 0;
95 int local_data = 0;
96
97 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
98
99 struct yaffs_packed_tags2 pt;
100
101 int packed_tags_size =
102 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
103 void *packed_tags_ptr =
104 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
105
106 yaffs_trace(YAFFS_TRACE_MTD,
107 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
108 nand_chunk, data, tags);
109
110 if (dev->param.inband_tags) {
111
112 if (!data) {
113 local_data = 1;
114 data = yaffs_get_temp_buffer(dev, __LINE__);
115 }
116
117 }
118
119 if (dev->param.inband_tags || (data && !tags))
120 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
121 &dummy, data);
122 else if (tags) {
123 ops.mode = MTD_OOB_AUTO;
124 ops.ooblen = packed_tags_size;
125 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
126 ops.ooboffs = 0;
127 ops.datbuf = data;
128 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
129 retval = mtd->read_oob(mtd, addr, &ops);
130 }
131
132 if (dev->param.inband_tags) {
133 if (tags) {
134 struct yaffs_packed_tags2_tags_only *pt2tp;
135 pt2tp =
136 (struct yaffs_packed_tags2_tags_only *)&data[dev->
137 data_bytes_per_chunk];
138 yaffs_unpack_tags2_tags_only(tags, pt2tp);
139 }
140 } else {
141 if (tags) {
142 memcpy(packed_tags_ptr,
143 yaffs_dev_to_lc(dev)->spare_buffer,
144 packed_tags_size);
145 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
146 }
147 }
148
149 if (local_data)
150 yaffs_release_temp_buffer(dev, data, __LINE__);
151
152 if (tags && retval == -EBADMSG
153 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
154 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
155 dev->n_ecc_unfixed++;
156 }
157 if (tags && retval == -EUCLEAN
158 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
159 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
160 dev->n_ecc_fixed++;
161 }
162 if (retval == 0)
163 return YAFFS_OK;
164 else
165 return YAFFS_FAIL;
166}
167
168int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
169{
170 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
171 int retval;
172 yaffs_trace(YAFFS_TRACE_MTD,
173 "nandmtd2_mark_block_bad %d", block_no);
174
175 retval =
176 mtd->block_markbad(mtd,
177 block_no * dev->param.chunks_per_block *
178 dev->param.total_bytes_per_chunk);
179
180 if (retval == 0)
181 return YAFFS_OK;
182 else
183 return YAFFS_FAIL;
184
185}
186
187int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
188 enum yaffs_block_state *state, u32 * seq_number)
189{
190 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
191 int retval;
192
193 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no);
194 retval =
195 mtd->block_isbad(mtd,
196 block_no * dev->param.chunks_per_block *
197 dev->param.total_bytes_per_chunk);
198
199 if (retval) {
200 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
201
202 *state = YAFFS_BLOCK_STATE_DEAD;
203 *seq_number = 0;
204 } else {
205 struct yaffs_ext_tags t;
206 nandmtd2_read_chunk_tags(dev, block_no *
207 dev->param.chunks_per_block, NULL, &t);
208
209 if (t.chunk_used) {
210 *seq_number = t.seq_number;
211 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
212 } else {
213 *seq_number = 0;
214 *state = YAFFS_BLOCK_STATE_EMPTY;
215 }
216 }
217 yaffs_trace(YAFFS_TRACE_MTD,
218 "block is bad seq %d state %d", *seq_number, *state);
219
220 if (retval == 0)
221 return YAFFS_OK;
222 else
223 return YAFFS_FAIL;
224}
225
diff --git a/fs/yaffs2/yaffs_mtdif2.h b/fs/yaffs2/yaffs_mtdif2.h
new file mode 100644
index 00000000000..d82112610d0
--- /dev/null
+++ b/fs/yaffs2/yaffs_mtdif2.h
@@ -0,0 +1,29 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_MTDIF2_H__
17#define __YAFFS_MTDIF2_H__
18
19#include "yaffs_guts.h"
20int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
21 const u8 * data,
22 const struct yaffs_ext_tags *tags);
23int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
24 u8 * data, struct yaffs_ext_tags *tags);
25int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no);
26int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
27 enum yaffs_block_state *state, u32 * seq_number);
28
29#endif
diff --git a/fs/yaffs2/yaffs_nameval.c b/fs/yaffs2/yaffs_nameval.c
new file mode 100644
index 00000000000..daa36f989d3
--- /dev/null
+++ b/fs/yaffs2/yaffs_nameval.c
@@ -0,0 +1,201 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/*
15 * This simple implementation of a name-value store assumes a small number of values and fits
16 * into a small finite buffer.
17 *
18 * Each attribute is stored as a record:
19 * sizeof(int) bytes record size.
20 * strnlen+1 bytes name null terminated.
21 * nbytes value.
22 * ----------
23 * total size stored in record size
24 *
25 * This code has not been tested with unicode yet.
26 */
27
28#include "yaffs_nameval.h"
29
30#include "yportenv.h"
31
32static int nval_find(const char *xb, int xb_size, const YCHAR * name,
33 int *exist_size)
34{
35 int pos = 0;
36 int size;
37
38 memcpy(&size, xb, sizeof(int));
39 while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
40 if (strncmp
41 ((YCHAR *) (xb + pos + sizeof(int)), name, size) == 0) {
42 if (exist_size)
43 *exist_size = size;
44 return pos;
45 }
46 pos += size;
47 if (pos < xb_size - sizeof(int))
48 memcpy(&size, xb + pos, sizeof(int));
49 else
50 size = 0;
51 }
52 if (exist_size)
53 *exist_size = 0;
54 return -1;
55}
56
57static int nval_used(const char *xb, int xb_size)
58{
59 int pos = 0;
60 int size;
61
62 memcpy(&size, xb + pos, sizeof(int));
63 while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
64 pos += size;
65 if (pos < xb_size - sizeof(int))
66 memcpy(&size, xb + pos, sizeof(int));
67 else
68 size = 0;
69 }
70 return pos;
71}
72
73int nval_del(char *xb, int xb_size, const YCHAR * name)
74{
75 int pos = nval_find(xb, xb_size, name, NULL);
76 int size;
77
78 if (pos >= 0 && pos < xb_size) {
79 /* Find size, shift rest over this record, then zero out the rest of buffer */
80 memcpy(&size, xb + pos, sizeof(int));
81 memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
82 memset(xb + (xb_size - size), 0, size);
83 return 0;
84 } else {
85 return -ENODATA;
86 }
87}
88
89int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
90 int bsize, int flags)
91{
92 int pos;
93 int namelen = strnlen(name, xb_size);
94 int reclen;
95 int size_exist = 0;
96 int space;
97 int start;
98
99 pos = nval_find(xb, xb_size, name, &size_exist);
100
101 if (flags & XATTR_CREATE && pos >= 0)
102 return -EEXIST;
103 if (flags & XATTR_REPLACE && pos < 0)
104 return -ENODATA;
105
106 start = nval_used(xb, xb_size);
107 space = xb_size - start + size_exist;
108
109 reclen = (sizeof(int) + namelen + 1 + bsize);
110
111 if (reclen > space)
112 return -ENOSPC;
113
114 if (pos >= 0) {
115 nval_del(xb, xb_size, name);
116 start = nval_used(xb, xb_size);
117 }
118
119 pos = start;
120
121 memcpy(xb + pos, &reclen, sizeof(int));
122 pos += sizeof(int);
123 strncpy((YCHAR *) (xb + pos), name, reclen);
124 pos += (namelen + 1);
125 memcpy(xb + pos, buf, bsize);
126 return 0;
127}
128
129int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
130 int bsize)
131{
132 int pos = nval_find(xb, xb_size, name, NULL);
133 int size;
134
135 if (pos >= 0 && pos < xb_size) {
136
137 memcpy(&size, xb + pos, sizeof(int));
138 pos += sizeof(int); /* advance past record length */
139 size -= sizeof(int);
140
141 /* Advance over name string */
142 while (xb[pos] && size > 0 && pos < xb_size) {
143 pos++;
144 size--;
145 }
146 /*Advance over NUL */
147 pos++;
148 size--;
149
150 if (size <= bsize) {
151 memcpy(buf, xb + pos, size);
152 return size;
153 }
154
155 }
156 if (pos >= 0)
157 return -ERANGE;
158 else
159 return -ENODATA;
160}
161
162int nval_list(const char *xb, int xb_size, char *buf, int bsize)
163{
164 int pos = 0;
165 int size;
166 int name_len;
167 int ncopied = 0;
168 int filled = 0;
169
170 memcpy(&size, xb + pos, sizeof(int));
171 while (size > sizeof(int) && size <= xb_size && (pos + size) < xb_size
172 && !filled) {
173 pos += sizeof(int);
174 size -= sizeof(int);
175 name_len = strnlen((YCHAR *) (xb + pos), size);
176 if (ncopied + name_len + 1 < bsize) {
177 memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
178 buf += name_len;
179 *buf = '\0';
180 buf++;
181 if (sizeof(YCHAR) > 1) {
182 *buf = '\0';
183 buf++;
184 }
185 ncopied += (name_len + 1);
186 } else {
187 filled = 1;
188 }
189 pos += size;
190 if (pos < xb_size - sizeof(int))
191 memcpy(&size, xb + pos, sizeof(int));
192 else
193 size = 0;
194 }
195 return ncopied;
196}
197
198int nval_hasvalues(const char *xb, int xb_size)
199{
200 return nval_used(xb, xb_size) > 0;
201}
diff --git a/fs/yaffs2/yaffs_nameval.h b/fs/yaffs2/yaffs_nameval.h
new file mode 100644
index 00000000000..2bb02b62762
--- /dev/null
+++ b/fs/yaffs2/yaffs_nameval.h
@@ -0,0 +1,28 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __NAMEVAL_H__
17#define __NAMEVAL_H__
18
19#include "yportenv.h"
20
21int nval_del(char *xb, int xb_size, const YCHAR * name);
22int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
23 int bsize, int flags);
24int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
25 int bsize);
26int nval_list(const char *xb, int xb_size, char *buf, int bsize);
27int nval_hasvalues(const char *xb, int xb_size);
28#endif
diff --git a/fs/yaffs2/yaffs_nand.c b/fs/yaffs2/yaffs_nand.c
new file mode 100644
index 00000000000..e816cabf43f
--- /dev/null
+++ b/fs/yaffs2/yaffs_nand.c
@@ -0,0 +1,127 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_nand.h"
15#include "yaffs_tagscompat.h"
16#include "yaffs_tagsvalidity.h"
17
18#include "yaffs_getblockinfo.h"
19
20int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
21 u8 * buffer, struct yaffs_ext_tags *tags)
22{
23 int result;
24 struct yaffs_ext_tags local_tags;
25
26 int realigned_chunk = nand_chunk - dev->chunk_offset;
27
28 dev->n_page_reads++;
29
30 /* If there are no tags provided, use local tags to get prioritised gc working */
31 if (!tags)
32 tags = &local_tags;
33
34 if (dev->param.read_chunk_tags_fn)
35 result =
36 dev->param.read_chunk_tags_fn(dev, realigned_chunk, buffer,
37 tags);
38 else
39 result = yaffs_tags_compat_rd(dev,
40 realigned_chunk, buffer, tags);
41 if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
42
43 struct yaffs_block_info *bi;
44 bi = yaffs_get_block_info(dev,
45 nand_chunk /
46 dev->param.chunks_per_block);
47 yaffs_handle_chunk_error(dev, bi);
48 }
49
50 return result;
51}
52
53int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
54 int nand_chunk,
55 const u8 * buffer, struct yaffs_ext_tags *tags)
56{
57
58 dev->n_page_writes++;
59
60 nand_chunk -= dev->chunk_offset;
61
62 if (tags) {
63 tags->seq_number = dev->seq_number;
64 tags->chunk_used = 1;
65 if (!yaffs_validate_tags(tags)) {
66 yaffs_trace(YAFFS_TRACE_ERROR, "Writing uninitialised tags");
67 YBUG();
68 }
69 yaffs_trace(YAFFS_TRACE_WRITE,
70 "Writing chunk %d tags %d %d",
71 nand_chunk, tags->obj_id, tags->chunk_id);
72 } else {
73 yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags");
74 YBUG();
75 }
76
77 if (dev->param.write_chunk_tags_fn)
78 return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
79 tags);
80 else
81 return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags);
82}
83
84int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
85{
86 block_no -= dev->block_offset;
87
88 if (dev->param.bad_block_fn)
89 return dev->param.bad_block_fn(dev, block_no);
90 else
91 return yaffs_tags_compat_mark_bad(dev, block_no);
92}
93
94int yaffs_query_init_block_state(struct yaffs_dev *dev,
95 int block_no,
96 enum yaffs_block_state *state,
97 u32 * seq_number)
98{
99 block_no -= dev->block_offset;
100
101 if (dev->param.query_block_fn)
102 return dev->param.query_block_fn(dev, block_no, state,
103 seq_number);
104 else
105 return yaffs_tags_compat_query_block(dev, block_no,
106 state, seq_number);
107}
108
109int yaffs_erase_block(struct yaffs_dev *dev, int flash_block)
110{
111 int result;
112
113 flash_block -= dev->block_offset;
114
115 dev->n_erasures++;
116
117 result = dev->param.erase_fn(dev, flash_block);
118
119 return result;
120}
121
122int yaffs_init_nand(struct yaffs_dev *dev)
123{
124 if (dev->param.initialise_flash_fn)
125 return dev->param.initialise_flash_fn(dev);
126 return YAFFS_OK;
127}
diff --git a/fs/yaffs2/yaffs_nand.h b/fs/yaffs2/yaffs_nand.h
new file mode 100644
index 00000000000..543f1987124
--- /dev/null
+++ b/fs/yaffs2/yaffs_nand.h
@@ -0,0 +1,38 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_NAND_H__
17#define __YAFFS_NAND_H__
18#include "yaffs_guts.h"
19
20int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
21 u8 * buffer, struct yaffs_ext_tags *tags);
22
23int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
24 int nand_chunk,
25 const u8 * buffer, struct yaffs_ext_tags *tags);
26
27int yaffs_mark_bad(struct yaffs_dev *dev, int block_no);
28
29int yaffs_query_init_block_state(struct yaffs_dev *dev,
30 int block_no,
31 enum yaffs_block_state *state,
32 unsigned *seq_number);
33
34int yaffs_erase_block(struct yaffs_dev *dev, int flash_block);
35
36int yaffs_init_nand(struct yaffs_dev *dev);
37
38#endif
diff --git a/fs/yaffs2/yaffs_packedtags1.c b/fs/yaffs2/yaffs_packedtags1.c
new file mode 100644
index 00000000000..a77f0954fc1
--- /dev/null
+++ b/fs/yaffs2/yaffs_packedtags1.c
@@ -0,0 +1,53 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_packedtags1.h"
15#include "yportenv.h"
16
17void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
18 const struct yaffs_ext_tags *t)
19{
20 pt->chunk_id = t->chunk_id;
21 pt->serial_number = t->serial_number;
22 pt->n_bytes = t->n_bytes;
23 pt->obj_id = t->obj_id;
24 pt->ecc = 0;
25 pt->deleted = (t->is_deleted) ? 0 : 1;
26 pt->unused_stuff = 0;
27 pt->should_be_ff = 0xFFFFFFFF;
28
29}
30
31void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
32 const struct yaffs_packed_tags1 *pt)
33{
34 static const u8 all_ff[] =
35 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 0xff
37 };
38
39 if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) {
40 t->block_bad = 0;
41 if (pt->should_be_ff != 0xFFFFFFFF)
42 t->block_bad = 1;
43 t->chunk_used = 1;
44 t->obj_id = pt->obj_id;
45 t->chunk_id = pt->chunk_id;
46 t->n_bytes = pt->n_bytes;
47 t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
48 t->is_deleted = (pt->deleted) ? 0 : 1;
49 t->serial_number = pt->serial_number;
50 } else {
51 memset(t, 0, sizeof(struct yaffs_ext_tags));
52 }
53}
diff --git a/fs/yaffs2/yaffs_packedtags1.h b/fs/yaffs2/yaffs_packedtags1.h
new file mode 100644
index 00000000000..d6861ff505e
--- /dev/null
+++ b/fs/yaffs2/yaffs_packedtags1.h
@@ -0,0 +1,39 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
17
18#ifndef __YAFFS_PACKEDTAGS1_H__
19#define __YAFFS_PACKEDTAGS1_H__
20
21#include "yaffs_guts.h"
22
23struct yaffs_packed_tags1 {
24 unsigned chunk_id:20;
25 unsigned serial_number:2;
26 unsigned n_bytes:10;
27 unsigned obj_id:18;
28 unsigned ecc:12;
29 unsigned deleted:1;
30 unsigned unused_stuff:1;
31 unsigned should_be_ff;
32
33};
34
35void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
36 const struct yaffs_ext_tags *t);
37void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
38 const struct yaffs_packed_tags1 *pt);
39#endif
diff --git a/fs/yaffs2/yaffs_packedtags2.c b/fs/yaffs2/yaffs_packedtags2.c
new file mode 100644
index 00000000000..8e7fea3d286
--- /dev/null
+++ b/fs/yaffs2/yaffs_packedtags2.c
@@ -0,0 +1,196 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_packedtags2.h"
15#include "yportenv.h"
16#include "yaffs_trace.h"
17#include "yaffs_tagsvalidity.h"
18
19/* This code packs a set of extended tags into a binary structure for
20 * NAND storage
21 */
22
23/* Some of the information is "extra" struff which can be packed in to
24 * speed scanning
25 * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
26 */
27
28/* Extra flags applied to chunk_id */
29
30#define EXTRA_HEADER_INFO_FLAG 0x80000000
31#define EXTRA_SHRINK_FLAG 0x40000000
32#define EXTRA_SHADOWS_FLAG 0x20000000
33#define EXTRA_SPARE_FLAGS 0x10000000
34
35#define ALL_EXTRA_FLAGS 0xF0000000
36
37/* Also, the top 4 bits of the object Id are set to the object type. */
38#define EXTRA_OBJECT_TYPE_SHIFT (28)
39#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
40
41static void yaffs_dump_packed_tags2_tags_only(const struct
42 yaffs_packed_tags2_tags_only *ptt)
43{
44 yaffs_trace(YAFFS_TRACE_MTD,
45 "packed tags obj %d chunk %d byte %d seq %d",
46 ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
47}
48
49static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
50{
51 yaffs_dump_packed_tags2_tags_only(&pt->t);
52}
53
54static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
55{
56 yaffs_trace(YAFFS_TRACE_MTD,
57 "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
58 t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
59 t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
60 t->seq_number);
61
62}
63
64void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
65 const struct yaffs_ext_tags *t)
66{
67 ptt->chunk_id = t->chunk_id;
68 ptt->seq_number = t->seq_number;
69 ptt->n_bytes = t->n_bytes;
70 ptt->obj_id = t->obj_id;
71
72 if (t->chunk_id == 0 && t->extra_available) {
73 /* Store the extra header info instead */
74 /* We save the parent object in the chunk_id */
75 ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
76 if (t->extra_is_shrink)
77 ptt->chunk_id |= EXTRA_SHRINK_FLAG;
78 if (t->extra_shadows)
79 ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
80
81 ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
82 ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
83
84 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
85 ptt->n_bytes = t->extra_equiv_id;
86 else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
87 ptt->n_bytes = t->extra_length;
88 else
89 ptt->n_bytes = 0;
90 }
91
92 yaffs_dump_packed_tags2_tags_only(ptt);
93 yaffs_dump_tags2(t);
94}
95
96void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
97 const struct yaffs_ext_tags *t, int tags_ecc)
98{
99 yaffs_pack_tags2_tags_only(&pt->t, t);
100
101 if (tags_ecc)
102 yaffs_ecc_calc_other((unsigned char *)&pt->t,
103 sizeof(struct
104 yaffs_packed_tags2_tags_only),
105 &pt->ecc);
106}
107
108void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
109 struct yaffs_packed_tags2_tags_only *ptt)
110{
111
112 memset(t, 0, sizeof(struct yaffs_ext_tags));
113
114 yaffs_init_tags(t);
115
116 if (ptt->seq_number != 0xFFFFFFFF) {
117 t->block_bad = 0;
118 t->chunk_used = 1;
119 t->obj_id = ptt->obj_id;
120 t->chunk_id = ptt->chunk_id;
121 t->n_bytes = ptt->n_bytes;
122 t->is_deleted = 0;
123 t->serial_number = 0;
124 t->seq_number = ptt->seq_number;
125
126 /* Do extra header info stuff */
127
128 if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
129 t->chunk_id = 0;
130 t->n_bytes = 0;
131
132 t->extra_available = 1;
133 t->extra_parent_id =
134 ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
135 t->extra_is_shrink =
136 (ptt->chunk_id & EXTRA_SHRINK_FLAG) ? 1 : 0;
137 t->extra_shadows =
138 (ptt->chunk_id & EXTRA_SHADOWS_FLAG) ? 1 : 0;
139 t->extra_obj_type =
140 ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
141 t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
142
143 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
144 t->extra_equiv_id = ptt->n_bytes;
145 else
146 t->extra_length = ptt->n_bytes;
147 }
148 }
149
150 yaffs_dump_packed_tags2_tags_only(ptt);
151 yaffs_dump_tags2(t);
152
153}
154
155void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
156 int tags_ecc)
157{
158
159 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
160
161 if (pt->t.seq_number != 0xFFFFFFFF && tags_ecc) {
162 /* Chunk is in use and we need to do ECC */
163
164 struct yaffs_ecc_other ecc;
165 int result;
166 yaffs_ecc_calc_other((unsigned char *)&pt->t,
167 sizeof(struct
168 yaffs_packed_tags2_tags_only),
169 &ecc);
170 result =
171 yaffs_ecc_correct_other((unsigned char *)&pt->t,
172 sizeof(struct
173 yaffs_packed_tags2_tags_only),
174 &pt->ecc, &ecc);
175 switch (result) {
176 case 0:
177 ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
178 break;
179 case 1:
180 ecc_result = YAFFS_ECC_RESULT_FIXED;
181 break;
182 case -1:
183 ecc_result = YAFFS_ECC_RESULT_UNFIXED;
184 break;
185 default:
186 ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
187 }
188 }
189
190 yaffs_unpack_tags2_tags_only(t, &pt->t);
191
192 t->ecc_result = ecc_result;
193
194 yaffs_dump_packed_tags2(pt);
195 yaffs_dump_tags2(t);
196}
diff --git a/fs/yaffs2/yaffs_packedtags2.h b/fs/yaffs2/yaffs_packedtags2.h
new file mode 100644
index 00000000000..f3296697bc0
--- /dev/null
+++ b/fs/yaffs2/yaffs_packedtags2.h
@@ -0,0 +1,47 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
17
18#ifndef __YAFFS_PACKEDTAGS2_H__
19#define __YAFFS_PACKEDTAGS2_H__
20
21#include "yaffs_guts.h"
22#include "yaffs_ecc.h"
23
24struct yaffs_packed_tags2_tags_only {
25 unsigned seq_number;
26 unsigned obj_id;
27 unsigned chunk_id;
28 unsigned n_bytes;
29};
30
31struct yaffs_packed_tags2 {
32 struct yaffs_packed_tags2_tags_only t;
33 struct yaffs_ecc_other ecc;
34};
35
36/* Full packed tags with ECC, used for oob tags */
37void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
38 const struct yaffs_ext_tags *t, int tags_ecc);
39void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
40 int tags_ecc);
41
42/* Only the tags part (no ECC for use with inband tags */
43void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *pt,
44 const struct yaffs_ext_tags *t);
45void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
46 struct yaffs_packed_tags2_tags_only *pt);
47#endif
diff --git a/fs/yaffs2/yaffs_tagscompat.c b/fs/yaffs2/yaffs_tagscompat.c
new file mode 100644
index 00000000000..7578075d9ac
--- /dev/null
+++ b/fs/yaffs2/yaffs_tagscompat.c
@@ -0,0 +1,422 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_guts.h"
15#include "yaffs_tagscompat.h"
16#include "yaffs_ecc.h"
17#include "yaffs_getblockinfo.h"
18#include "yaffs_trace.h"
19
20static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
21
22
23/********** Tags ECC calculations *********/
24
25void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare)
26{
27 yaffs_ecc_cacl(data, spare->ecc1);
28 yaffs_ecc_cacl(&data[256], spare->ecc2);
29}
30
31void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
32{
33 /* Calculate an ecc */
34
35 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
36 unsigned i, j;
37 unsigned ecc = 0;
38 unsigned bit = 0;
39
40 tags->ecc = 0;
41
42 for (i = 0; i < 8; i++) {
43 for (j = 1; j & 0xff; j <<= 1) {
44 bit++;
45 if (b[i] & j)
46 ecc ^= bit;
47 }
48 }
49
50 tags->ecc = ecc;
51
52}
53
54int yaffs_check_tags_ecc(struct yaffs_tags *tags)
55{
56 unsigned ecc = tags->ecc;
57
58 yaffs_calc_tags_ecc(tags);
59
60 ecc ^= tags->ecc;
61
62 if (ecc && ecc <= 64) {
63 /* TODO: Handle the failure better. Retire? */
64 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
65
66 ecc--;
67
68 b[ecc / 8] ^= (1 << (ecc & 7));
69
70 /* Now recvalc the ecc */
71 yaffs_calc_tags_ecc(tags);
72
73 return 1; /* recovered error */
74 } else if (ecc) {
75 /* Wierd ecc failure value */
76 /* TODO Need to do somethiong here */
77 return -1; /* unrecovered error */
78 }
79
80 return 0;
81}
82
83/********** Tags **********/
84
85static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
86 struct yaffs_tags *tags_ptr)
87{
88 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
89
90 yaffs_calc_tags_ecc(tags_ptr);
91
92 spare_ptr->tb0 = tu->as_bytes[0];
93 spare_ptr->tb1 = tu->as_bytes[1];
94 spare_ptr->tb2 = tu->as_bytes[2];
95 spare_ptr->tb3 = tu->as_bytes[3];
96 spare_ptr->tb4 = tu->as_bytes[4];
97 spare_ptr->tb5 = tu->as_bytes[5];
98 spare_ptr->tb6 = tu->as_bytes[6];
99 spare_ptr->tb7 = tu->as_bytes[7];
100}
101
102static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
103 struct yaffs_spare *spare_ptr,
104 struct yaffs_tags *tags_ptr)
105{
106 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
107 int result;
108
109 tu->as_bytes[0] = spare_ptr->tb0;
110 tu->as_bytes[1] = spare_ptr->tb1;
111 tu->as_bytes[2] = spare_ptr->tb2;
112 tu->as_bytes[3] = spare_ptr->tb3;
113 tu->as_bytes[4] = spare_ptr->tb4;
114 tu->as_bytes[5] = spare_ptr->tb5;
115 tu->as_bytes[6] = spare_ptr->tb6;
116 tu->as_bytes[7] = spare_ptr->tb7;
117
118 result = yaffs_check_tags_ecc(tags_ptr);
119 if (result > 0)
120 dev->n_tags_ecc_fixed++;
121 else if (result < 0)
122 dev->n_tags_ecc_unfixed++;
123}
124
125static void yaffs_spare_init(struct yaffs_spare *spare)
126{
127 memset(spare, 0xFF, sizeof(struct yaffs_spare));
128}
129
130static int yaffs_wr_nand(struct yaffs_dev *dev,
131 int nand_chunk, const u8 * data,
132 struct yaffs_spare *spare)
133{
134 if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
135 yaffs_trace(YAFFS_TRACE_ERROR,
136 "**>> yaffs chunk %d is not valid",
137 nand_chunk);
138 return YAFFS_FAIL;
139 }
140
141 return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
142}
143
144static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
145 int nand_chunk,
146 u8 * data,
147 struct yaffs_spare *spare,
148 enum yaffs_ecc_result *ecc_result,
149 int correct_errors)
150{
151 int ret_val;
152 struct yaffs_spare local_spare;
153
154 if (!spare && data) {
155 /* If we don't have a real spare, then we use a local one. */
156 /* Need this for the calculation of the ecc */
157 spare = &local_spare;
158 }
159
160 if (!dev->param.use_nand_ecc) {
161 ret_val =
162 dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
163 if (data && correct_errors) {
164 /* Do ECC correction */
165 /* Todo handle any errors */
166 int ecc_result1, ecc_result2;
167 u8 calc_ecc[3];
168
169 yaffs_ecc_cacl(data, calc_ecc);
170 ecc_result1 =
171 yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
172 yaffs_ecc_cacl(&data[256], calc_ecc);
173 ecc_result2 =
174 yaffs_ecc_correct(&data[256], spare->ecc2,
175 calc_ecc);
176
177 if (ecc_result1 > 0) {
178 yaffs_trace(YAFFS_TRACE_ERROR,
179 "**>>yaffs ecc error fix performed on chunk %d:0",
180 nand_chunk);
181 dev->n_ecc_fixed++;
182 } else if (ecc_result1 < 0) {
183 yaffs_trace(YAFFS_TRACE_ERROR,
184 "**>>yaffs ecc error unfixed on chunk %d:0",
185 nand_chunk);
186 dev->n_ecc_unfixed++;
187 }
188
189 if (ecc_result2 > 0) {
190 yaffs_trace(YAFFS_TRACE_ERROR,
191 "**>>yaffs ecc error fix performed on chunk %d:1",
192 nand_chunk);
193 dev->n_ecc_fixed++;
194 } else if (ecc_result2 < 0) {
195 yaffs_trace(YAFFS_TRACE_ERROR,
196 "**>>yaffs ecc error unfixed on chunk %d:1",
197 nand_chunk);
198 dev->n_ecc_unfixed++;
199 }
200
201 if (ecc_result1 || ecc_result2) {
202 /* We had a data problem on this page */
203 yaffs_handle_rd_data_error(dev, nand_chunk);
204 }
205
206 if (ecc_result1 < 0 || ecc_result2 < 0)
207 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
208 else if (ecc_result1 > 0 || ecc_result2 > 0)
209 *ecc_result = YAFFS_ECC_RESULT_FIXED;
210 else
211 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
212 }
213 } else {
214 /* Must allocate enough memory for spare+2*sizeof(int) */
215 /* for ecc results from device. */
216 struct yaffs_nand_spare nspare;
217
218 memset(&nspare, 0, sizeof(nspare));
219
220 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
221 (struct yaffs_spare *)
222 &nspare);
223 memcpy(spare, &nspare, sizeof(struct yaffs_spare));
224 if (data && correct_errors) {
225 if (nspare.eccres1 > 0) {
226 yaffs_trace(YAFFS_TRACE_ERROR,
227 "**>>mtd ecc error fix performed on chunk %d:0",
228 nand_chunk);
229 } else if (nspare.eccres1 < 0) {
230 yaffs_trace(YAFFS_TRACE_ERROR,
231 "**>>mtd ecc error unfixed on chunk %d:0",
232 nand_chunk);
233 }
234
235 if (nspare.eccres2 > 0) {
236 yaffs_trace(YAFFS_TRACE_ERROR,
237 "**>>mtd ecc error fix performed on chunk %d:1",
238 nand_chunk);
239 } else if (nspare.eccres2 < 0) {
240 yaffs_trace(YAFFS_TRACE_ERROR,
241 "**>>mtd ecc error unfixed on chunk %d:1",
242 nand_chunk);
243 }
244
245 if (nspare.eccres1 || nspare.eccres2) {
246 /* We had a data problem on this page */
247 yaffs_handle_rd_data_error(dev, nand_chunk);
248 }
249
250 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
251 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
252 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
253 *ecc_result = YAFFS_ECC_RESULT_FIXED;
254 else
255 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
256
257 }
258 }
259 return ret_val;
260}
261
262/*
263 * Functions for robustisizing
264 */
265
266static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
267{
268 int flash_block = nand_chunk / dev->param.chunks_per_block;
269
270 /* Mark the block for retirement */
271 yaffs_get_block_info(dev,
272 flash_block + dev->block_offset)->needs_retiring =
273 1;
274 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
275 "**>>Block %d marked for retirement",
276 flash_block);
277
278 /* TODO:
279 * Just do a garbage collection on the affected block
280 * then retire the block
281 * NB recursion
282 */
283}
284
285int yaffs_tags_compat_wr(struct yaffs_dev *dev,
286 int nand_chunk,
287 const u8 * data, const struct yaffs_ext_tags *ext_tags)
288{
289 struct yaffs_spare spare;
290 struct yaffs_tags tags;
291
292 yaffs_spare_init(&spare);
293
294 if (ext_tags->is_deleted)
295 spare.page_status = 0;
296 else {
297 tags.obj_id = ext_tags->obj_id;
298 tags.chunk_id = ext_tags->chunk_id;
299
300 tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
301
302 if (dev->data_bytes_per_chunk >= 1024)
303 tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
304 else
305 tags.n_bytes_msb = 3;
306
307 tags.serial_number = ext_tags->serial_number;
308
309 if (!dev->param.use_nand_ecc && data)
310 yaffs_calc_ecc(data, &spare);
311
312 yaffs_load_tags_to_spare(&spare, &tags);
313
314 }
315
316 return yaffs_wr_nand(dev, nand_chunk, data, &spare);
317}
318
319int yaffs_tags_compat_rd(struct yaffs_dev *dev,
320 int nand_chunk,
321 u8 * data, struct yaffs_ext_tags *ext_tags)
322{
323
324 struct yaffs_spare spare;
325 struct yaffs_tags tags;
326 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
327
328 static struct yaffs_spare spare_ff;
329 static int init;
330
331 if (!init) {
332 memset(&spare_ff, 0xFF, sizeof(spare_ff));
333 init = 1;
334 }
335
336 if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) {
337 /* ext_tags may be NULL */
338 if (ext_tags) {
339
340 int deleted =
341 (hweight8(spare.page_status) < 7) ? 1 : 0;
342
343 ext_tags->is_deleted = deleted;
344 ext_tags->ecc_result = ecc_result;
345 ext_tags->block_bad = 0; /* We're reading it */
346 /* therefore it is not a bad block */
347 ext_tags->chunk_used =
348 (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
349 0) ? 1 : 0;
350
351 if (ext_tags->chunk_used) {
352 yaffs_get_tags_from_spare(dev, &spare, &tags);
353
354 ext_tags->obj_id = tags.obj_id;
355 ext_tags->chunk_id = tags.chunk_id;
356 ext_tags->n_bytes = tags.n_bytes_lsb;
357
358 if (dev->data_bytes_per_chunk >= 1024)
359 ext_tags->n_bytes |=
360 (((unsigned)tags.
361 n_bytes_msb) << 10);
362
363 ext_tags->serial_number = tags.serial_number;
364 }
365 }
366
367 return YAFFS_OK;
368 } else {
369 return YAFFS_FAIL;
370 }
371}
372
373int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
374{
375
376 struct yaffs_spare spare;
377
378 memset(&spare, 0xff, sizeof(struct yaffs_spare));
379
380 spare.block_status = 'Y';
381
382 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
383 &spare);
384 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
385 NULL, &spare);
386
387 return YAFFS_OK;
388
389}
390
391int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
392 int block_no,
393 enum yaffs_block_state *state,
394 u32 * seq_number)
395{
396
397 struct yaffs_spare spare0, spare1;
398 static struct yaffs_spare spare_ff;
399 static int init;
400 enum yaffs_ecc_result dummy;
401
402 if (!init) {
403 memset(&spare_ff, 0xFF, sizeof(spare_ff));
404 init = 1;
405 }
406
407 *seq_number = 0;
408
409 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
410 &spare0, &dummy, 1);
411 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
412 NULL, &spare1, &dummy, 1);
413
414 if (hweight8(spare0.block_status & spare1.block_status) < 7)
415 *state = YAFFS_BLOCK_STATE_DEAD;
416 else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
417 *state = YAFFS_BLOCK_STATE_EMPTY;
418 else
419 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
420
421 return YAFFS_OK;
422}
diff --git a/fs/yaffs2/yaffs_tagscompat.h b/fs/yaffs2/yaffs_tagscompat.h
new file mode 100644
index 00000000000..8cd35dcd3ca
--- /dev/null
+++ b/fs/yaffs2/yaffs_tagscompat.h
@@ -0,0 +1,36 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_TAGSCOMPAT_H__
17#define __YAFFS_TAGSCOMPAT_H__
18
19#include "yaffs_guts.h"
20int yaffs_tags_compat_wr(struct yaffs_dev *dev,
21 int nand_chunk,
22 const u8 * data, const struct yaffs_ext_tags *tags);
23int yaffs_tags_compat_rd(struct yaffs_dev *dev,
24 int nand_chunk,
25 u8 * data, struct yaffs_ext_tags *tags);
26int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no);
27int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
28 int block_no,
29 enum yaffs_block_state *state,
30 u32 * seq_number);
31
32void yaffs_calc_tags_ecc(struct yaffs_tags *tags);
33int yaffs_check_tags_ecc(struct yaffs_tags *tags);
34int yaffs_count_bits(u8 byte);
35
36#endif
diff --git a/fs/yaffs2/yaffs_tagsvalidity.c b/fs/yaffs2/yaffs_tagsvalidity.c
new file mode 100644
index 00000000000..4358d79d4be
--- /dev/null
+++ b/fs/yaffs2/yaffs_tagsvalidity.c
@@ -0,0 +1,27 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_tagsvalidity.h"
15
16void yaffs_init_tags(struct yaffs_ext_tags *tags)
17{
18 memset(tags, 0, sizeof(struct yaffs_ext_tags));
19 tags->validity0 = 0xAAAAAAAA;
20 tags->validity1 = 0x55555555;
21}
22
23int yaffs_validate_tags(struct yaffs_ext_tags *tags)
24{
25 return (tags->validity0 == 0xAAAAAAAA && tags->validity1 == 0x55555555);
26
27}
diff --git a/fs/yaffs2/yaffs_tagsvalidity.h b/fs/yaffs2/yaffs_tagsvalidity.h
new file mode 100644
index 00000000000..36a021fc8fa
--- /dev/null
+++ b/fs/yaffs2/yaffs_tagsvalidity.h
@@ -0,0 +1,23 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_TAGS_VALIDITY_H__
17#define __YAFFS_TAGS_VALIDITY_H__
18
19#include "yaffs_guts.h"
20
21void yaffs_init_tags(struct yaffs_ext_tags *tags);
22int yaffs_validate_tags(struct yaffs_ext_tags *tags);
23#endif
diff --git a/fs/yaffs2/yaffs_trace.h b/fs/yaffs2/yaffs_trace.h
new file mode 100644
index 00000000000..6273dbf9f63
--- /dev/null
+++ b/fs/yaffs2/yaffs_trace.h
@@ -0,0 +1,57 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YTRACE_H__
17#define __YTRACE_H__
18
19extern unsigned int yaffs_trace_mask;
20extern unsigned int yaffs_wr_attempts;
21
22/*
23 * Tracing flags.
24 * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
25 */
26
27#define YAFFS_TRACE_OS 0x00000002
28#define YAFFS_TRACE_ALLOCATE 0x00000004
29#define YAFFS_TRACE_SCAN 0x00000008
30#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
31#define YAFFS_TRACE_ERASE 0x00000020
32#define YAFFS_TRACE_GC 0x00000040
33#define YAFFS_TRACE_WRITE 0x00000080
34#define YAFFS_TRACE_TRACING 0x00000100
35#define YAFFS_TRACE_DELETION 0x00000200
36#define YAFFS_TRACE_BUFFERS 0x00000400
37#define YAFFS_TRACE_NANDACCESS 0x00000800
38#define YAFFS_TRACE_GC_DETAIL 0x00001000
39#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
40#define YAFFS_TRACE_MTD 0x00004000
41#define YAFFS_TRACE_CHECKPOINT 0x00008000
42
43#define YAFFS_TRACE_VERIFY 0x00010000
44#define YAFFS_TRACE_VERIFY_NAND 0x00020000
45#define YAFFS_TRACE_VERIFY_FULL 0x00040000
46#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
47
48#define YAFFS_TRACE_SYNC 0x00100000
49#define YAFFS_TRACE_BACKGROUND 0x00200000
50#define YAFFS_TRACE_LOCK 0x00400000
51#define YAFFS_TRACE_MOUNT 0x00800000
52
53#define YAFFS_TRACE_ERROR 0x40000000
54#define YAFFS_TRACE_BUG 0x80000000
55#define YAFFS_TRACE_ALWAYS 0xF0000000
56
57#endif
diff --git a/fs/yaffs2/yaffs_verify.c b/fs/yaffs2/yaffs_verify.c
new file mode 100644
index 00000000000..738c7f69a5e
--- /dev/null
+++ b/fs/yaffs2/yaffs_verify.c
@@ -0,0 +1,535 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_verify.h"
15#include "yaffs_trace.h"
16#include "yaffs_bitmap.h"
17#include "yaffs_getblockinfo.h"
18#include "yaffs_nand.h"
19
20int yaffs_skip_verification(struct yaffs_dev *dev)
21{
22 dev = dev;
23 return !(yaffs_trace_mask &
24 (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
25}
26
27static int yaffs_skip_full_verification(struct yaffs_dev *dev)
28{
29 dev = dev;
30 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
31}
32
33static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
34{
35 dev = dev;
36 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
37}
38
39static const char *block_state_name[] = {
40 "Unknown",
41 "Needs scanning",
42 "Scanning",
43 "Empty",
44 "Allocating",
45 "Full",
46 "Dirty",
47 "Checkpoint",
48 "Collecting",
49 "Dead"
50};
51
52void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
53{
54 int actually_used;
55 int in_use;
56
57 if (yaffs_skip_verification(dev))
58 return;
59
60 /* Report illegal runtime states */
61 if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
62 yaffs_trace(YAFFS_TRACE_VERIFY,
63 "Block %d has undefined state %d",
64 n, bi->block_state);
65
66 switch (bi->block_state) {
67 case YAFFS_BLOCK_STATE_UNKNOWN:
68 case YAFFS_BLOCK_STATE_SCANNING:
69 case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
70 yaffs_trace(YAFFS_TRACE_VERIFY,
71 "Block %d has bad run-state %s",
72 n, block_state_name[bi->block_state]);
73 }
74
75 /* Check pages in use and soft deletions are legal */
76
77 actually_used = bi->pages_in_use - bi->soft_del_pages;
78
79 if (bi->pages_in_use < 0
80 || bi->pages_in_use > dev->param.chunks_per_block
81 || bi->soft_del_pages < 0
82 || bi->soft_del_pages > dev->param.chunks_per_block
83 || actually_used < 0 || actually_used > dev->param.chunks_per_block)
84 yaffs_trace(YAFFS_TRACE_VERIFY,
85 "Block %d has illegal values pages_in_used %d soft_del_pages %d",
86 n, bi->pages_in_use, bi->soft_del_pages);
87
88 /* Check chunk bitmap legal */
89 in_use = yaffs_count_chunk_bits(dev, n);
90 if (in_use != bi->pages_in_use)
91 yaffs_trace(YAFFS_TRACE_VERIFY,
92 "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
93 n, bi->pages_in_use, in_use);
94
95}
96
97void yaffs_verify_collected_blk(struct yaffs_dev *dev,
98 struct yaffs_block_info *bi, int n)
99{
100 yaffs_verify_blk(dev, bi, n);
101
102 /* After collection the block should be in the erased state */
103
104 if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
105 bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
106 yaffs_trace(YAFFS_TRACE_ERROR,
107 "Block %d is in state %d after gc, should be erased",
108 n, bi->block_state);
109 }
110}
111
112void yaffs_verify_blocks(struct yaffs_dev *dev)
113{
114 int i;
115 int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
116 int illegal_states = 0;
117
118 if (yaffs_skip_verification(dev))
119 return;
120
121 memset(state_count, 0, sizeof(state_count));
122
123 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
124 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
125 yaffs_verify_blk(dev, bi, i);
126
127 if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
128 state_count[bi->block_state]++;
129 else
130 illegal_states++;
131 }
132
133 yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
134
135 yaffs_trace(YAFFS_TRACE_VERIFY,
136 "%d blocks have illegal states",
137 illegal_states);
138 if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
139 yaffs_trace(YAFFS_TRACE_VERIFY,
140 "Too many allocating blocks");
141
142 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
143 yaffs_trace(YAFFS_TRACE_VERIFY,
144 "%s %d blocks",
145 block_state_name[i], state_count[i]);
146
147 if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
148 yaffs_trace(YAFFS_TRACE_VERIFY,
149 "Checkpoint block count wrong dev %d count %d",
150 dev->blocks_in_checkpt,
151 state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
152
153 if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
154 yaffs_trace(YAFFS_TRACE_VERIFY,
155 "Erased block count wrong dev %d count %d",
156 dev->n_erased_blocks,
157 state_count[YAFFS_BLOCK_STATE_EMPTY]);
158
159 if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
160 yaffs_trace(YAFFS_TRACE_VERIFY,
161 "Too many collecting blocks %d (max is 1)",
162 state_count[YAFFS_BLOCK_STATE_COLLECTING]);
163}
164
165/*
166 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
167 * case those tests will not be performed.
168 */
169void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
170 struct yaffs_ext_tags *tags, int parent_check)
171{
172 if (obj && yaffs_skip_verification(obj->my_dev))
173 return;
174
175 if (!(tags && obj && oh)) {
176 yaffs_trace(YAFFS_TRACE_VERIFY,
177 "Verifying object header tags %p obj %p oh %p",
178 tags, obj, oh);
179 return;
180 }
181
182 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
183 oh->type > YAFFS_OBJECT_TYPE_MAX)
184 yaffs_trace(YAFFS_TRACE_VERIFY,
185 "Obj %d header type is illegal value 0x%x",
186 tags->obj_id, oh->type);
187
188 if (tags->obj_id != obj->obj_id)
189 yaffs_trace(YAFFS_TRACE_VERIFY,
190 "Obj %d header mismatch obj_id %d",
191 tags->obj_id, obj->obj_id);
192
193 /*
194 * Check that the object's parent ids match if parent_check requested.
195 *
196 * Tests do not apply to the root object.
197 */
198
199 if (parent_check && tags->obj_id > 1 && !obj->parent)
200 yaffs_trace(YAFFS_TRACE_VERIFY,
201 "Obj %d header mismatch parent_id %d obj->parent is NULL",
202 tags->obj_id, oh->parent_obj_id);
203
204 if (parent_check && obj->parent &&
205 oh->parent_obj_id != obj->parent->obj_id &&
206 (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
207 obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
208 yaffs_trace(YAFFS_TRACE_VERIFY,
209 "Obj %d header mismatch parent_id %d parent_obj_id %d",
210 tags->obj_id, oh->parent_obj_id,
211 obj->parent->obj_id);
212
213 if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
214 yaffs_trace(YAFFS_TRACE_VERIFY,
215 "Obj %d header name is NULL",
216 obj->obj_id);
217
218 if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Trashed name */
219 yaffs_trace(YAFFS_TRACE_VERIFY,
220 "Obj %d header name is 0xFF",
221 obj->obj_id);
222}
223
224void yaffs_verify_file(struct yaffs_obj *obj)
225{
226 int required_depth;
227 int actual_depth;
228 u32 last_chunk;
229 u32 x;
230 u32 i;
231 struct yaffs_dev *dev;
232 struct yaffs_ext_tags tags;
233 struct yaffs_tnode *tn;
234 u32 obj_id;
235
236 if (!obj)
237 return;
238
239 if (yaffs_skip_verification(obj->my_dev))
240 return;
241
242 dev = obj->my_dev;
243 obj_id = obj->obj_id;
244
245 /* Check file size is consistent with tnode depth */
246 last_chunk =
247 obj->variant.file_variant.file_size / dev->data_bytes_per_chunk + 1;
248 x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
249 required_depth = 0;
250 while (x > 0) {
251 x >>= YAFFS_TNODES_INTERNAL_BITS;
252 required_depth++;
253 }
254
255 actual_depth = obj->variant.file_variant.top_level;
256
257 /* Check that the chunks in the tnode tree are all correct.
258 * We do this by scanning through the tnode tree and
259 * checking the tags for every chunk match.
260 */
261
262 if (yaffs_skip_nand_verification(dev))
263 return;
264
265 for (i = 1; i <= last_chunk; i++) {
266 tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
267
268 if (tn) {
269 u32 the_chunk = yaffs_get_group_base(dev, tn, i);
270 if (the_chunk > 0) {
271 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
272 &tags);
273 if (tags.obj_id != obj_id || tags.chunk_id != i)
274 yaffs_trace(YAFFS_TRACE_VERIFY,
275 "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
276 obj_id, i, the_chunk,
277 tags.obj_id, tags.chunk_id);
278 }
279 }
280 }
281}
282
283void yaffs_verify_link(struct yaffs_obj *obj)
284{
285 if (obj && yaffs_skip_verification(obj->my_dev))
286 return;
287
288 /* Verify sane equivalent object */
289}
290
291void yaffs_verify_symlink(struct yaffs_obj *obj)
292{
293 if (obj && yaffs_skip_verification(obj->my_dev))
294 return;
295
296 /* Verify symlink string */
297}
298
299void yaffs_verify_special(struct yaffs_obj *obj)
300{
301 if (obj && yaffs_skip_verification(obj->my_dev))
302 return;
303}
304
305void yaffs_verify_obj(struct yaffs_obj *obj)
306{
307 struct yaffs_dev *dev;
308
309 u32 chunk_min;
310 u32 chunk_max;
311
312 u32 chunk_id_ok;
313 u32 chunk_in_range;
314 u32 chunk_wrongly_deleted;
315 u32 chunk_valid;
316
317 if (!obj)
318 return;
319
320 if (obj->being_created)
321 return;
322
323 dev = obj->my_dev;
324
325 if (yaffs_skip_verification(dev))
326 return;
327
328 /* Check sane object header chunk */
329
330 chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
331 chunk_max =
332 (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
333
334 chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
335 ((unsigned)(obj->hdr_chunk)) <= chunk_max);
336 chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
337 chunk_valid = chunk_in_range &&
338 yaffs_check_chunk_bit(dev,
339 obj->hdr_chunk / dev->param.chunks_per_block,
340 obj->hdr_chunk % dev->param.chunks_per_block);
341 chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
342
343 if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
344 yaffs_trace(YAFFS_TRACE_VERIFY,
345 "Obj %d has chunk_id %d %s %s",
346 obj->obj_id, obj->hdr_chunk,
347 chunk_id_ok ? "" : ",out of range",
348 chunk_wrongly_deleted ? ",marked as deleted" : "");
349
350 if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
351 struct yaffs_ext_tags tags;
352 struct yaffs_obj_hdr *oh;
353 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
354
355 oh = (struct yaffs_obj_hdr *)buffer;
356
357 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
358
359 yaffs_verify_oh(obj, oh, &tags, 1);
360
361 yaffs_release_temp_buffer(dev, buffer, __LINE__);
362 }
363
364 /* Verify it has a parent */
365 if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
366 yaffs_trace(YAFFS_TRACE_VERIFY,
367 "Obj %d has parent pointer %p which does not look like an object",
368 obj->obj_id, obj->parent);
369 }
370
371 /* Verify parent is a directory */
372 if (obj->parent
373 && obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
374 yaffs_trace(YAFFS_TRACE_VERIFY,
375 "Obj %d's parent is not a directory (type %d)",
376 obj->obj_id, obj->parent->variant_type);
377 }
378
379 switch (obj->variant_type) {
380 case YAFFS_OBJECT_TYPE_FILE:
381 yaffs_verify_file(obj);
382 break;
383 case YAFFS_OBJECT_TYPE_SYMLINK:
384 yaffs_verify_symlink(obj);
385 break;
386 case YAFFS_OBJECT_TYPE_DIRECTORY:
387 yaffs_verify_dir(obj);
388 break;
389 case YAFFS_OBJECT_TYPE_HARDLINK:
390 yaffs_verify_link(obj);
391 break;
392 case YAFFS_OBJECT_TYPE_SPECIAL:
393 yaffs_verify_special(obj);
394 break;
395 case YAFFS_OBJECT_TYPE_UNKNOWN:
396 default:
397 yaffs_trace(YAFFS_TRACE_VERIFY,
398 "Obj %d has illegaltype %d",
399 obj->obj_id, obj->variant_type);
400 break;
401 }
402}
403
404void yaffs_verify_objects(struct yaffs_dev *dev)
405{
406 struct yaffs_obj *obj;
407 int i;
408 struct list_head *lh;
409
410 if (yaffs_skip_verification(dev))
411 return;
412
413 /* Iterate through the objects in each hash entry */
414
415 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
416 list_for_each(lh, &dev->obj_bucket[i].list) {
417 if (lh) {
418 obj =
419 list_entry(lh, struct yaffs_obj, hash_link);
420 yaffs_verify_obj(obj);
421 }
422 }
423 }
424}
425
426void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
427{
428 struct list_head *lh;
429 struct yaffs_obj *list_obj;
430
431 int count = 0;
432
433 if (!obj) {
434 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
435 YBUG();
436 return;
437 }
438
439 if (yaffs_skip_verification(obj->my_dev))
440 return;
441
442 if (!obj->parent) {
443 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent" );
444 YBUG();
445 return;
446 }
447
448 if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
449 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
450 YBUG();
451 }
452
453 /* Iterate through the objects in each hash entry */
454
455 list_for_each(lh, &obj->parent->variant.dir_variant.children) {
456 if (lh) {
457 list_obj = list_entry(lh, struct yaffs_obj, siblings);
458 yaffs_verify_obj(list_obj);
459 if (obj == list_obj)
460 count++;
461 }
462 }
463
464 if (count != 1) {
465 yaffs_trace(YAFFS_TRACE_ALWAYS,
466 "Object in directory %d times",
467 count);
468 YBUG();
469 }
470}
471
472void yaffs_verify_dir(struct yaffs_obj *directory)
473{
474 struct list_head *lh;
475 struct yaffs_obj *list_obj;
476
477 if (!directory) {
478 YBUG();
479 return;
480 }
481
482 if (yaffs_skip_full_verification(directory->my_dev))
483 return;
484
485 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
486 yaffs_trace(YAFFS_TRACE_ALWAYS,
487 "Directory has wrong type: %d",
488 directory->variant_type);
489 YBUG();
490 }
491
492 /* Iterate through the objects in each hash entry */
493
494 list_for_each(lh, &directory->variant.dir_variant.children) {
495 if (lh) {
496 list_obj = list_entry(lh, struct yaffs_obj, siblings);
497 if (list_obj->parent != directory) {
498 yaffs_trace(YAFFS_TRACE_ALWAYS,
499 "Object in directory list has wrong parent %p",
500 list_obj->parent);
501 YBUG();
502 }
503 yaffs_verify_obj_in_dir(list_obj);
504 }
505 }
506}
507
508static int yaffs_free_verification_failures;
509
510void yaffs_verify_free_chunks(struct yaffs_dev *dev)
511{
512 int counted;
513 int difference;
514
515 if (yaffs_skip_verification(dev))
516 return;
517
518 counted = yaffs_count_free_chunks(dev);
519
520 difference = dev->n_free_chunks - counted;
521
522 if (difference) {
523 yaffs_trace(YAFFS_TRACE_ALWAYS,
524 "Freechunks verification failure %d %d %d",
525 dev->n_free_chunks, counted, difference);
526 yaffs_free_verification_failures++;
527 }
528}
529
530int yaffs_verify_file_sane(struct yaffs_obj *in)
531{
532 in = in;
533 return YAFFS_OK;
534}
535
diff --git a/fs/yaffs2/yaffs_verify.h b/fs/yaffs2/yaffs_verify.h
new file mode 100644
index 00000000000..cc6f8899930
--- /dev/null
+++ b/fs/yaffs2/yaffs_verify.h
@@ -0,0 +1,43 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_VERIFY_H__
17#define __YAFFS_VERIFY_H__
18
19#include "yaffs_guts.h"
20
21void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi,
22 int n);
23void yaffs_verify_collected_blk(struct yaffs_dev *dev,
24 struct yaffs_block_info *bi, int n);
25void yaffs_verify_blocks(struct yaffs_dev *dev);
26
27void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
28 struct yaffs_ext_tags *tags, int parent_check);
29void yaffs_verify_file(struct yaffs_obj *obj);
30void yaffs_verify_link(struct yaffs_obj *obj);
31void yaffs_verify_symlink(struct yaffs_obj *obj);
32void yaffs_verify_special(struct yaffs_obj *obj);
33void yaffs_verify_obj(struct yaffs_obj *obj);
34void yaffs_verify_objects(struct yaffs_dev *dev);
35void yaffs_verify_obj_in_dir(struct yaffs_obj *obj);
36void yaffs_verify_dir(struct yaffs_obj *directory);
37void yaffs_verify_free_chunks(struct yaffs_dev *dev);
38
39int yaffs_verify_file_sane(struct yaffs_obj *obj);
40
41int yaffs_skip_verification(struct yaffs_dev *dev);
42
43#endif
diff --git a/fs/yaffs2/yaffs_vfs.c b/fs/yaffs2/yaffs_vfs.c
new file mode 100644
index 00000000000..d5b87531400
--- /dev/null
+++ b/fs/yaffs2/yaffs_vfs.c
@@ -0,0 +1,2792 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 * Acknowledgements:
9 * Luc van OostenRyck for numerous patches.
10 * Nick Bane for numerous patches.
11 * Nick Bane for 2.5/2.6 integration.
12 * Andras Toth for mknod rdev issue.
13 * Michael Fischer for finding the problem with inode inconsistency.
14 * Some code bodily lifted from JFFS
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
20
21/*
22 *
23 * This is the file system front-end to YAFFS that hooks it up to
24 * the VFS.
25 *
26 * Special notes:
27 * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
28 * this superblock
29 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
30 * superblock
31 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
32 */
33
34/*
35 * NB There are two variants of Linux VFS glue code. This variant supports
36 * a single version and should not include any multi-version code.
37 */
38#include <linux/version.h>
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/slab.h>
43#include <linux/init.h>
44#include <linux/fs.h>
45#include <linux/proc_fs.h>
46#include <linux/smp_lock.h>
47#include <linux/pagemap.h>
48#include <linux/mtd/mtd.h>
49#include <linux/interrupt.h>
50#include <linux/string.h>
51#include <linux/ctype.h>
52#include <linux/namei.h>
53#include <linux/exportfs.h>
54#include <linux/kthread.h>
55#include <linux/delay.h>
56#include <linux/freezer.h>
57
58#include <asm/div64.h>
59
60#include <linux/statfs.h>
61
62#define UnlockPage(p) unlock_page(p)
63#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
64
65#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
66
67#define YPROC_ROOT NULL
68
69#define Y_INIT_TIMER(a) init_timer_on_stack(a)
70
71#define WRITE_SIZE_STR "writesize"
72#define WRITE_SIZE(mtd) ((mtd)->writesize)
73
74static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
75{
76 uint64_t result = partition_size;
77 do_div(result, block_size);
78 return (uint32_t) result;
79}
80
81#include <linux/uaccess.h>
82#include <linux/mtd/mtd.h>
83
84#include "yportenv.h"
85#include "yaffs_trace.h"
86#include "yaffs_guts.h"
87#include "yaffs_attribs.h"
88
89#include "yaffs_linux.h"
90
91#include "yaffs_mtdif.h"
92#include "yaffs_mtdif1.h"
93#include "yaffs_mtdif2.h"
94
95unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
96unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
97unsigned int yaffs_auto_checkpoint = 1;
98unsigned int yaffs_gc_control = 1;
99unsigned int yaffs_bg_enable = 1;
100
101/* Module Parameters */
102module_param(yaffs_trace_mask, uint, 0644);
103module_param(yaffs_wr_attempts, uint, 0644);
104module_param(yaffs_auto_checkpoint, uint, 0644);
105module_param(yaffs_gc_control, uint, 0644);
106module_param(yaffs_bg_enable, uint, 0644);
107
108
109#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
110#define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
111#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
112#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
113
114#define update_dir_time(dir) do {\
115 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
116 } while(0)
117
118
119static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
120{
121 return yaffs_gc_control;
122}
123
124static void yaffs_gross_lock(struct yaffs_dev *dev)
125{
126 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
127 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
128 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
129}
130
131static void yaffs_gross_unlock(struct yaffs_dev *dev)
132{
133 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
134 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
135}
136
137static void yaffs_fill_inode_from_obj(struct inode *inode,
138 struct yaffs_obj *obj);
139
140static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
141{
142 struct inode *inode;
143 struct yaffs_obj *obj;
144 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
145
146 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
147
148 inode = iget_locked(sb, ino);
149 if (!inode)
150 return ERR_PTR(-ENOMEM);
151 if (!(inode->i_state & I_NEW))
152 return inode;
153
154 /* NB This is called as a side effect of other functions, but
155 * we had to release the lock to prevent deadlocks, so
156 * need to lock again.
157 */
158
159 yaffs_gross_lock(dev);
160
161 obj = yaffs_find_by_number(dev, inode->i_ino);
162
163 yaffs_fill_inode_from_obj(inode, obj);
164
165 yaffs_gross_unlock(dev);
166
167 unlock_new_inode(inode);
168 return inode;
169}
170
171struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
172 struct yaffs_obj *obj)
173{
174 struct inode *inode;
175
176 if (!sb) {
177 yaffs_trace(YAFFS_TRACE_OS,
178 "yaffs_get_inode for NULL super_block!!");
179 return NULL;
180
181 }
182
183 if (!obj) {
184 yaffs_trace(YAFFS_TRACE_OS,
185 "yaffs_get_inode for NULL object!!");
186 return NULL;
187
188 }
189
190 yaffs_trace(YAFFS_TRACE_OS,
191 "yaffs_get_inode for object %d",
192 obj->obj_id);
193
194 inode = yaffs_iget(sb, obj->obj_id);
195 if (IS_ERR(inode))
196 return NULL;
197
198 /* NB Side effect: iget calls back to yaffs_read_inode(). */
199 /* iget also increments the inode's i_count */
200 /* NB You can't be holding gross_lock or deadlock will happen! */
201
202 return inode;
203}
204
205static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
206 dev_t rdev)
207{
208 struct inode *inode;
209
210 struct yaffs_obj *obj = NULL;
211 struct yaffs_dev *dev;
212
213 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
214
215 int error = -ENOSPC;
216 uid_t uid = current->cred->fsuid;
217 gid_t gid =
218 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
219
220 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
221 mode |= S_ISGID;
222
223 if (parent) {
224 yaffs_trace(YAFFS_TRACE_OS,
225 "yaffs_mknod: parent object %d type %d",
226 parent->obj_id, parent->variant_type);
227 } else {
228 yaffs_trace(YAFFS_TRACE_OS,
229 "yaffs_mknod: could not get parent object");
230 return -EPERM;
231 }
232
233 yaffs_trace(YAFFS_TRACE_OS,
234 "yaffs_mknod: making oject for %s, mode %x dev %x",
235 dentry->d_name.name, mode, rdev);
236
237 dev = parent->my_dev;
238
239 yaffs_gross_lock(dev);
240
241 switch (mode & S_IFMT) {
242 default:
243 /* Special (socket, fifo, device...) */
244 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
245 obj =
246 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
247 gid, old_encode_dev(rdev));
248 break;
249 case S_IFREG: /* file */
250 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
251 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
252 gid);
253 break;
254 case S_IFDIR: /* directory */
255 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
256 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
257 uid, gid);
258 break;
259 case S_IFLNK: /* symlink */
260 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
261 obj = NULL; /* Do we ever get here? */
262 break;
263 }
264
265 /* Can not call yaffs_get_inode() with gross lock held */
266 yaffs_gross_unlock(dev);
267
268 if (obj) {
269 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
270 d_instantiate(dentry, inode);
271 update_dir_time(dir);
272 yaffs_trace(YAFFS_TRACE_OS,
273 "yaffs_mknod created object %d count = %d",
274 obj->obj_id, atomic_read(&inode->i_count));
275 error = 0;
276 yaffs_fill_inode_from_obj(dir, parent);
277 } else {
278 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
279 error = -ENOMEM;
280 }
281
282 return error;
283}
284
285static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
286{
287 return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
288}
289
290static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
291 struct nameidata *n)
292{
293 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
294}
295
296static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
297 struct dentry *dentry)
298{
299 struct inode *inode = old_dentry->d_inode;
300 struct yaffs_obj *obj = NULL;
301 struct yaffs_obj *link = NULL;
302 struct yaffs_dev *dev;
303
304 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
305
306 obj = yaffs_inode_to_obj(inode);
307 dev = obj->my_dev;
308
309 yaffs_gross_lock(dev);
310
311 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
312 link =
313 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
314 obj);
315
316 if (link) {
317 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
318 d_instantiate(dentry, old_dentry->d_inode);
319 atomic_inc(&old_dentry->d_inode->i_count);
320 yaffs_trace(YAFFS_TRACE_OS,
321 "yaffs_link link count %d i_count %d",
322 old_dentry->d_inode->i_nlink,
323 atomic_read(&old_dentry->d_inode->i_count));
324 }
325
326 yaffs_gross_unlock(dev);
327
328 if (link) {
329 update_dir_time(dir);
330 return 0;
331 }
332
333 return -EPERM;
334}
335
336static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
337 const char *symname)
338{
339 struct yaffs_obj *obj;
340 struct yaffs_dev *dev;
341 uid_t uid = current->cred->fsuid;
342 gid_t gid =
343 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
344
345 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
346
347 dev = yaffs_inode_to_obj(dir)->my_dev;
348 yaffs_gross_lock(dev);
349 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
350 S_IFLNK | S_IRWXUGO, uid, gid, symname);
351 yaffs_gross_unlock(dev);
352
353 if (obj) {
354 struct inode *inode;
355
356 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
357 d_instantiate(dentry, inode);
358 update_dir_time(dir);
359 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
360 return 0;
361 } else {
362 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
363 }
364
365 return -ENOMEM;
366}
367
368static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
369 struct nameidata *n)
370{
371 struct yaffs_obj *obj;
372 struct inode *inode = NULL;
373
374 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
375
376 if (current != yaffs_dev_to_lc(dev)->readdir_process)
377 yaffs_gross_lock(dev);
378
379 yaffs_trace(YAFFS_TRACE_OS,
380 "yaffs_lookup for %d:%s",
381 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
382
383 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
384
385 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
386
387 /* Can't hold gross lock when calling yaffs_get_inode() */
388 if (current != yaffs_dev_to_lc(dev)->readdir_process)
389 yaffs_gross_unlock(dev);
390
391 if (obj) {
392 yaffs_trace(YAFFS_TRACE_OS,
393 "yaffs_lookup found %d", obj->obj_id);
394
395 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
396
397 if (inode) {
398 yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
399 d_add(dentry, inode);
400 /* return dentry; */
401 return NULL;
402 }
403
404 } else {
405 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
406
407 }
408
409 d_add(dentry, inode);
410
411 return NULL;
412}
413
414static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
415{
416 int ret_val;
417
418 struct yaffs_dev *dev;
419 struct yaffs_obj *obj;
420
421 yaffs_trace(YAFFS_TRACE_OS,
422 "yaffs_unlink %d:%s",
423 (int)(dir->i_ino), dentry->d_name.name);
424 obj = yaffs_inode_to_obj(dir);
425 dev = obj->my_dev;
426
427 yaffs_gross_lock(dev);
428
429 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
430
431 if (ret_val == YAFFS_OK) {
432 dentry->d_inode->i_nlink--;
433 dir->i_version++;
434 yaffs_gross_unlock(dev);
435 mark_inode_dirty(dentry->d_inode);
436 update_dir_time(dir);
437 return 0;
438 }
439 yaffs_gross_unlock(dev);
440 return -ENOTEMPTY;
441}
442
443static int yaffs_sync_object(struct file *file, int datasync)
444{
445
446 struct yaffs_obj *obj;
447 struct yaffs_dev *dev;
448 struct dentry *dentry = file->f_path.dentry;
449
450 obj = yaffs_dentry_to_obj(dentry);
451
452 dev = obj->my_dev;
453
454 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
455 yaffs_gross_lock(dev);
456 yaffs_flush_file(obj, 1, datasync);
457 yaffs_gross_unlock(dev);
458 return 0;
459}
460/*
461 * The VFS layer already does all the dentry stuff for rename.
462 *
463 * NB: POSIX says you can rename an object over an old object of the same name
464 */
465static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
466 struct inode *new_dir, struct dentry *new_dentry)
467{
468 struct yaffs_dev *dev;
469 int ret_val = YAFFS_FAIL;
470 struct yaffs_obj *target;
471
472 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
473 dev = yaffs_inode_to_obj(old_dir)->my_dev;
474
475 yaffs_gross_lock(dev);
476
477 /* Check if the target is an existing directory that is not empty. */
478 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
479 new_dentry->d_name.name);
480
481 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
482 !list_empty(&target->variant.dir_variant.children)) {
483
484 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
485
486 ret_val = YAFFS_FAIL;
487 } else {
488 /* Now does unlinking internally using shadowing mechanism */
489 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
490
491 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
492 old_dentry->d_name.name,
493 yaffs_inode_to_obj(new_dir),
494 new_dentry->d_name.name);
495 }
496 yaffs_gross_unlock(dev);
497
498 if (ret_val == YAFFS_OK) {
499 if (target) {
500 new_dentry->d_inode->i_nlink--;
501 mark_inode_dirty(new_dentry->d_inode);
502 }
503
504 update_dir_time(old_dir);
505 if (old_dir != new_dir)
506 update_dir_time(new_dir);
507 return 0;
508 } else {
509 return -ENOTEMPTY;
510 }
511}
512
513static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
514{
515 struct inode *inode = dentry->d_inode;
516 int error = 0;
517 struct yaffs_dev *dev;
518
519 yaffs_trace(YAFFS_TRACE_OS,
520 "yaffs_setattr of object %d",
521 yaffs_inode_to_obj(inode)->obj_id);
522
523 /* Fail if a requested resize >= 2GB */
524 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
525 error = -EINVAL;
526
527 if (error == 0)
528 error = inode_change_ok(inode, attr);
529 if (error == 0) {
530 int result;
531 if (!error) {
532 setattr_copy(inode, attr);
533 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
534 if (attr->ia_valid & ATTR_SIZE) {
535 truncate_setsize(inode, attr->ia_size);
536 inode->i_blocks = (inode->i_size + 511) >> 9;
537 }
538 }
539 dev = yaffs_inode_to_obj(inode)->my_dev;
540 if (attr->ia_valid & ATTR_SIZE) {
541 yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
542 (int)(attr->ia_size),
543 (int)(attr->ia_size));
544 }
545 yaffs_gross_lock(dev);
546 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
547 if (result == YAFFS_OK) {
548 error = 0;
549 } else {
550 error = -EPERM;
551 }
552 yaffs_gross_unlock(dev);
553
554 }
555
556 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
557
558 return error;
559}
560
561#ifdef CONFIG_YAFFS_XATTR
562static int yaffs_setxattr(struct dentry *dentry, const char *name,
563 const void *value, size_t size, int flags)
564{
565 struct inode *inode = dentry->d_inode;
566 int error = 0;
567 struct yaffs_dev *dev;
568 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
569
570 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
571
572 if (error == 0) {
573 int result;
574 dev = obj->my_dev;
575 yaffs_gross_lock(dev);
576 result = yaffs_set_xattrib(obj, name, value, size, flags);
577 if (result == YAFFS_OK)
578 error = 0;
579 else if (result < 0)
580 error = result;
581 yaffs_gross_unlock(dev);
582
583 }
584 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
585
586 return error;
587}
588
589static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
590 size_t size)
591{
592 struct inode *inode = dentry->d_inode;
593 int error = 0;
594 struct yaffs_dev *dev;
595 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
596
597 yaffs_trace(YAFFS_TRACE_OS,
598 "yaffs_getxattr \"%s\" from object %d",
599 name, obj->obj_id);
600
601 if (error == 0) {
602 dev = obj->my_dev;
603 yaffs_gross_lock(dev);
604 error = yaffs_get_xattrib(obj, name, buff, size);
605 yaffs_gross_unlock(dev);
606
607 }
608 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
609
610 return error;
611}
612
613static int yaffs_removexattr(struct dentry *dentry, const char *name)
614{
615 struct inode *inode = dentry->d_inode;
616 int error = 0;
617 struct yaffs_dev *dev;
618 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
619
620 yaffs_trace(YAFFS_TRACE_OS,
621 "yaffs_removexattr of object %d", obj->obj_id);
622
623 if (error == 0) {
624 int result;
625 dev = obj->my_dev;
626 yaffs_gross_lock(dev);
627 result = yaffs_remove_xattrib(obj, name);
628 if (result == YAFFS_OK)
629 error = 0;
630 else if (result < 0)
631 error = result;
632 yaffs_gross_unlock(dev);
633
634 }
635 yaffs_trace(YAFFS_TRACE_OS,
636 "yaffs_removexattr done returning %d", error);
637
638 return error;
639}
640
641static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
642{
643 struct inode *inode = dentry->d_inode;
644 int error = 0;
645 struct yaffs_dev *dev;
646 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
647
648 yaffs_trace(YAFFS_TRACE_OS,
649 "yaffs_listxattr of object %d", obj->obj_id);
650
651 if (error == 0) {
652 dev = obj->my_dev;
653 yaffs_gross_lock(dev);
654 error = yaffs_list_xattrib(obj, buff, size);
655 yaffs_gross_unlock(dev);
656
657 }
658 yaffs_trace(YAFFS_TRACE_OS,
659 "yaffs_listxattr done returning %d", error);
660
661 return error;
662}
663
664#endif
665
666static const struct inode_operations yaffs_dir_inode_operations = {
667 .create = yaffs_create,
668 .lookup = yaffs_lookup,
669 .link = yaffs_link,
670 .unlink = yaffs_unlink,
671 .symlink = yaffs_symlink,
672 .mkdir = yaffs_mkdir,
673 .rmdir = yaffs_unlink,
674 .mknod = yaffs_mknod,
675 .rename = yaffs_rename,
676 .setattr = yaffs_setattr,
677#ifdef CONFIG_YAFFS_XATTR
678 .setxattr = yaffs_setxattr,
679 .getxattr = yaffs_getxattr,
680 .listxattr = yaffs_listxattr,
681 .removexattr = yaffs_removexattr,
682#endif
683};
684/*-----------------------------------------------------------------*/
685/* Directory search context allows us to unlock access to yaffs during
686 * filldir without causing problems with the directory being modified.
687 * This is similar to the tried and tested mechanism used in yaffs direct.
688 *
689 * A search context iterates along a doubly linked list of siblings in the
690 * directory. If the iterating object is deleted then this would corrupt
691 * the list iteration, likely causing a crash. The search context avoids
692 * this by using the remove_obj_fn to move the search context to the
693 * next object before the object is deleted.
694 *
695 * Many readdirs (and thus seach conexts) may be alive simulateously so
696 * each struct yaffs_dev has a list of these.
697 *
698 * A seach context lives for the duration of a readdir.
699 *
700 * All these functions must be called while yaffs is locked.
701 */
702
703struct yaffs_search_context {
704 struct yaffs_dev *dev;
705 struct yaffs_obj *dir_obj;
706 struct yaffs_obj *next_return;
707 struct list_head others;
708};
709
710/*
711 * yaffs_new_search() creates a new search context, initialises it and
712 * adds it to the device's search context list.
713 *
714 * Called at start of readdir.
715 */
716static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
717{
718 struct yaffs_dev *dev = dir->my_dev;
719 struct yaffs_search_context *sc =
720 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
721 if (sc) {
722 sc->dir_obj = dir;
723 sc->dev = dev;
724 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
725 sc->next_return = NULL;
726 else
727 sc->next_return =
728 list_entry(dir->variant.dir_variant.children.next,
729 struct yaffs_obj, siblings);
730 INIT_LIST_HEAD(&sc->others);
731 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
732 }
733 return sc;
734}
735
736/*
737 * yaffs_search_end() disposes of a search context and cleans up.
738 */
739static void yaffs_search_end(struct yaffs_search_context *sc)
740{
741 if (sc) {
742 list_del(&sc->others);
743 kfree(sc);
744 }
745}
746
747/*
748 * yaffs_search_advance() moves a search context to the next object.
749 * Called when the search iterates or when an object removal causes
750 * the search context to be moved to the next object.
751 */
752static void yaffs_search_advance(struct yaffs_search_context *sc)
753{
754 if (!sc)
755 return;
756
757 if (sc->next_return == NULL ||
758 list_empty(&sc->dir_obj->variant.dir_variant.children))
759 sc->next_return = NULL;
760 else {
761 struct list_head *next = sc->next_return->siblings.next;
762
763 if (next == &sc->dir_obj->variant.dir_variant.children)
764 sc->next_return = NULL; /* end of list */
765 else
766 sc->next_return =
767 list_entry(next, struct yaffs_obj, siblings);
768 }
769}
770
771/*
772 * yaffs_remove_obj_callback() is called when an object is unlinked.
773 * We check open search contexts and advance any which are currently
774 * on the object being iterated.
775 */
776static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
777{
778
779 struct list_head *i;
780 struct yaffs_search_context *sc;
781 struct list_head *search_contexts =
782 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
783
784 /* Iterate through the directory search contexts.
785 * If any are currently on the object being removed, then advance
786 * the search context to the next object to prevent a hanging pointer.
787 */
788 list_for_each(i, search_contexts) {
789 if (i) {
790 sc = list_entry(i, struct yaffs_search_context, others);
791 if (sc->next_return == obj)
792 yaffs_search_advance(sc);
793 }
794 }
795
796}
797
798static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
799{
800 struct yaffs_obj *obj;
801 struct yaffs_dev *dev;
802 struct yaffs_search_context *sc;
803 struct inode *inode = f->f_dentry->d_inode;
804 unsigned long offset, curoffs;
805 struct yaffs_obj *l;
806 int ret_val = 0;
807
808 char name[YAFFS_MAX_NAME_LENGTH + 1];
809
810 obj = yaffs_dentry_to_obj(f->f_dentry);
811 dev = obj->my_dev;
812
813 yaffs_gross_lock(dev);
814
815 yaffs_dev_to_lc(dev)->readdir_process = current;
816
817 offset = f->f_pos;
818
819 sc = yaffs_new_search(obj);
820 if (!sc) {
821 ret_val = -ENOMEM;
822 goto out;
823 }
824
825 yaffs_trace(YAFFS_TRACE_OS,
826 "yaffs_readdir: starting at %d", (int)offset);
827
828 if (offset == 0) {
829 yaffs_trace(YAFFS_TRACE_OS,
830 "yaffs_readdir: entry . ino %d",
831 (int)inode->i_ino);
832 yaffs_gross_unlock(dev);
833 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
834 yaffs_gross_lock(dev);
835 goto out;
836 }
837 yaffs_gross_lock(dev);
838 offset++;
839 f->f_pos++;
840 }
841 if (offset == 1) {
842 yaffs_trace(YAFFS_TRACE_OS,
843 "yaffs_readdir: entry .. ino %d",
844 (int)f->f_dentry->d_parent->d_inode->i_ino);
845 yaffs_gross_unlock(dev);
846 if (filldir(dirent, "..", 2, offset,
847 f->f_dentry->d_parent->d_inode->i_ino,
848 DT_DIR) < 0) {
849 yaffs_gross_lock(dev);
850 goto out;
851 }
852 yaffs_gross_lock(dev);
853 offset++;
854 f->f_pos++;
855 }
856
857 curoffs = 1;
858
859 /* If the directory has changed since the open or last call to
860 readdir, rewind to after the 2 canned entries. */
861 if (f->f_version != inode->i_version) {
862 offset = 2;
863 f->f_pos = offset;
864 f->f_version = inode->i_version;
865 }
866
867 while (sc->next_return) {
868 curoffs++;
869 l = sc->next_return;
870 if (curoffs >= offset) {
871 int this_inode = yaffs_get_obj_inode(l);
872 int this_type = yaffs_get_obj_type(l);
873
874 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
875 yaffs_trace(YAFFS_TRACE_OS,
876 "yaffs_readdir: %s inode %d",
877 name, yaffs_get_obj_inode(l));
878
879 yaffs_gross_unlock(dev);
880
881 if (filldir(dirent,
882 name,
883 strlen(name),
884 offset, this_inode, this_type) < 0) {
885 yaffs_gross_lock(dev);
886 goto out;
887 }
888
889 yaffs_gross_lock(dev);
890
891 offset++;
892 f->f_pos++;
893 }
894 yaffs_search_advance(sc);
895 }
896
897out:
898 yaffs_search_end(sc);
899 yaffs_dev_to_lc(dev)->readdir_process = NULL;
900 yaffs_gross_unlock(dev);
901
902 return ret_val;
903}
904
905static const struct file_operations yaffs_dir_operations = {
906 .read = generic_read_dir,
907 .readdir = yaffs_readdir,
908 .fsync = yaffs_sync_object,
909 .llseek = generic_file_llseek,
910};
911
912
913
914static int yaffs_file_flush(struct file *file, fl_owner_t id)
915{
916 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
917
918 struct yaffs_dev *dev = obj->my_dev;
919
920 yaffs_trace(YAFFS_TRACE_OS,
921 "yaffs_file_flush object %d (%s)",
922 obj->obj_id, obj->dirty ? "dirty" : "clean");
923
924 yaffs_gross_lock(dev);
925
926 yaffs_flush_file(obj, 1, 0);
927
928 yaffs_gross_unlock(dev);
929
930 return 0;
931}
932
933static const struct file_operations yaffs_file_operations = {
934 .read = do_sync_read,
935 .write = do_sync_write,
936 .aio_read = generic_file_aio_read,
937 .aio_write = generic_file_aio_write,
938 .mmap = generic_file_mmap,
939 .flush = yaffs_file_flush,
940 .fsync = yaffs_sync_object,
941 .splice_read = generic_file_splice_read,
942 .splice_write = generic_file_splice_write,
943 .llseek = generic_file_llseek,
944};
945
946
947/* ExportFS support */
948static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
949 uint32_t generation)
950{
951 return yaffs_iget(sb, ino);
952}
953
954static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
955 struct fid *fid, int fh_len,
956 int fh_type)
957{
958 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
959 yaffs2_nfs_get_inode);
960}
961
962static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
963 struct fid *fid, int fh_len,
964 int fh_type)
965{
966 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
967 yaffs2_nfs_get_inode);
968}
969
970struct dentry *yaffs2_get_parent(struct dentry *dentry)
971{
972
973 struct super_block *sb = dentry->d_inode->i_sb;
974 struct dentry *parent = ERR_PTR(-ENOENT);
975 struct inode *inode;
976 unsigned long parent_ino;
977 struct yaffs_obj *d_obj;
978 struct yaffs_obj *parent_obj;
979
980 d_obj = yaffs_inode_to_obj(dentry->d_inode);
981
982 if (d_obj) {
983 parent_obj = d_obj->parent;
984 if (parent_obj) {
985 parent_ino = yaffs_get_obj_inode(parent_obj);
986 inode = yaffs_iget(sb, parent_ino);
987
988 if (IS_ERR(inode)) {
989 parent = ERR_CAST(inode);
990 } else {
991 parent = d_obtain_alias(inode);
992 if (!IS_ERR(parent)) {
993 parent = ERR_PTR(-ENOMEM);
994 iput(inode);
995 }
996 }
997 }
998 }
999
1000 return parent;
1001}
1002
1003/* Just declare a zero structure as a NULL value implies
1004 * using the default functions of exportfs.
1005 */
1006
1007static struct export_operations yaffs_export_ops = {
1008 .fh_to_dentry = yaffs2_fh_to_dentry,
1009 .fh_to_parent = yaffs2_fh_to_parent,
1010 .get_parent = yaffs2_get_parent,
1011};
1012
1013
1014/*-----------------------------------------------------------------*/
1015
1016static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1017 int buflen)
1018{
1019 unsigned char *alias;
1020 int ret;
1021
1022 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1023
1024 yaffs_gross_lock(dev);
1025
1026 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1027
1028 yaffs_gross_unlock(dev);
1029
1030 if (!alias)
1031 return -ENOMEM;
1032
1033 ret = vfs_readlink(dentry, buffer, buflen, alias);
1034 kfree(alias);
1035 return ret;
1036}
1037
1038static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1039{
1040 unsigned char *alias;
1041 void *ret;
1042 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1043
1044 yaffs_gross_lock(dev);
1045
1046 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1047 yaffs_gross_unlock(dev);
1048
1049 if (!alias) {
1050 ret = ERR_PTR(-ENOMEM);
1051 goto out;
1052 }
1053
1054 nd_set_link(nd, alias);
1055 ret = (void *)alias;
1056out:
1057 return ret;
1058}
1059
1060void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1061{
1062 kfree(alias);
1063}
1064
1065
1066static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
1067{
1068 /* Clear the association between the inode and
1069 * the struct yaffs_obj.
1070 */
1071 obj->my_inode = NULL;
1072 yaffs_inode_to_obj_lv(inode) = NULL;
1073
1074 /* If the object freeing was deferred, then the real
1075 * free happens now.
1076 * This should fix the inode inconsistency problem.
1077 */
1078 yaffs_handle_defered_free(obj);
1079}
1080
1081/* yaffs_evict_inode combines into one operation what was previously done in
1082 * yaffs_clear_inode() and yaffs_delete_inode()
1083 *
1084 */
1085static void yaffs_evict_inode(struct inode *inode)
1086{
1087 struct yaffs_obj *obj;
1088 struct yaffs_dev *dev;
1089 int deleteme = 0;
1090
1091 obj = yaffs_inode_to_obj(inode);
1092
1093 yaffs_trace(YAFFS_TRACE_OS,
1094 "yaffs_evict_inode: ino %d, count %d %s",
1095 (int)inode->i_ino,
1096 atomic_read(&inode->i_count),
1097 obj ? "object exists" : "null object");
1098
1099 if (!inode->i_nlink && !is_bad_inode(inode))
1100 deleteme = 1;
1101 truncate_inode_pages(&inode->i_data, 0);
1102 end_writeback(inode);
1103
1104 if (deleteme && obj) {
1105 dev = obj->my_dev;
1106 yaffs_gross_lock(dev);
1107 yaffs_del_obj(obj);
1108 yaffs_gross_unlock(dev);
1109 }
1110 if (obj) {
1111 dev = obj->my_dev;
1112 yaffs_gross_lock(dev);
1113 yaffs_unstitch_obj(inode, obj);
1114 yaffs_gross_unlock(dev);
1115 }
1116
1117}
1118
1119static void yaffs_touch_super(struct yaffs_dev *dev)
1120{
1121 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
1122
1123 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
1124 if (sb)
1125 sb->s_dirt = 1;
1126}
1127
1128static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1129{
1130 /* Lifted from jffs2 */
1131
1132 struct yaffs_obj *obj;
1133 unsigned char *pg_buf;
1134 int ret;
1135
1136 struct yaffs_dev *dev;
1137
1138 yaffs_trace(YAFFS_TRACE_OS,
1139 "yaffs_readpage_nolock at %08x, size %08x",
1140 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1141 (unsigned)PAGE_CACHE_SIZE);
1142
1143 obj = yaffs_dentry_to_obj(f->f_dentry);
1144
1145 dev = obj->my_dev;
1146
1147 BUG_ON(!PageLocked(pg));
1148
1149 pg_buf = kmap(pg);
1150 /* FIXME: Can kmap fail? */
1151
1152 yaffs_gross_lock(dev);
1153
1154 ret = yaffs_file_rd(obj, pg_buf,
1155 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1156
1157 yaffs_gross_unlock(dev);
1158
1159 if (ret >= 0)
1160 ret = 0;
1161
1162 if (ret) {
1163 ClearPageUptodate(pg);
1164 SetPageError(pg);
1165 } else {
1166 SetPageUptodate(pg);
1167 ClearPageError(pg);
1168 }
1169
1170 flush_dcache_page(pg);
1171 kunmap(pg);
1172
1173 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1174 return ret;
1175}
1176
1177static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1178{
1179 int ret = yaffs_readpage_nolock(f, pg);
1180 UnlockPage(pg);
1181 return ret;
1182}
1183
1184static int yaffs_readpage(struct file *f, struct page *pg)
1185{
1186 int ret;
1187
1188 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1189 ret = yaffs_readpage_unlock(f, pg);
1190 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1191 return ret;
1192}
1193
1194/* writepage inspired by/stolen from smbfs */
1195
1196static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1197{
1198 struct yaffs_dev *dev;
1199 struct address_space *mapping = page->mapping;
1200 struct inode *inode;
1201 unsigned long end_index;
1202 char *buffer;
1203 struct yaffs_obj *obj;
1204 int n_written = 0;
1205 unsigned n_bytes;
1206 loff_t i_size;
1207
1208 if (!mapping)
1209 BUG();
1210 inode = mapping->host;
1211 if (!inode)
1212 BUG();
1213 i_size = i_size_read(inode);
1214
1215 end_index = i_size >> PAGE_CACHE_SHIFT;
1216
1217 if (page->index < end_index)
1218 n_bytes = PAGE_CACHE_SIZE;
1219 else {
1220 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1221
1222 if (page->index > end_index || !n_bytes) {
1223 yaffs_trace(YAFFS_TRACE_OS,
1224 "yaffs_writepage at %08x, inode size = %08x!!!",
1225 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1226 (unsigned)inode->i_size);
1227 yaffs_trace(YAFFS_TRACE_OS,
1228 " -> don't care!!");
1229
1230 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1231 set_page_writeback(page);
1232 unlock_page(page);
1233 end_page_writeback(page);
1234 return 0;
1235 }
1236 }
1237
1238 if (n_bytes != PAGE_CACHE_SIZE)
1239 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1240
1241 get_page(page);
1242
1243 buffer = kmap(page);
1244
1245 obj = yaffs_inode_to_obj(inode);
1246 dev = obj->my_dev;
1247 yaffs_gross_lock(dev);
1248
1249 yaffs_trace(YAFFS_TRACE_OS,
1250 "yaffs_writepage at %08x, size %08x",
1251 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1252 yaffs_trace(YAFFS_TRACE_OS,
1253 "writepag0: obj = %05x, ino = %05x",
1254 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1255
1256 n_written = yaffs_wr_file(obj, buffer,
1257 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1258
1259 yaffs_touch_super(dev);
1260
1261 yaffs_trace(YAFFS_TRACE_OS,
1262 "writepag1: obj = %05x, ino = %05x",
1263 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1264
1265 yaffs_gross_unlock(dev);
1266
1267 kunmap(page);
1268 set_page_writeback(page);
1269 unlock_page(page);
1270 end_page_writeback(page);
1271 put_page(page);
1272
1273 return (n_written == n_bytes) ? 0 : -ENOSPC;
1274}
1275
1276/* Space holding and freeing is done to ensure we have space available for
1277 * write_begin/end.
1278 * For now we just assume few parallel writes and check against a small
1279 * number.
1280 * Todo: need to do this with a counter to handle parallel reads better.
1281 */
1282
1283static ssize_t yaffs_hold_space(struct file *f)
1284{
1285 struct yaffs_obj *obj;
1286 struct yaffs_dev *dev;
1287
1288 int n_free_chunks;
1289
1290 obj = yaffs_dentry_to_obj(f->f_dentry);
1291
1292 dev = obj->my_dev;
1293
1294 yaffs_gross_lock(dev);
1295
1296 n_free_chunks = yaffs_get_n_free_chunks(dev);
1297
1298 yaffs_gross_unlock(dev);
1299
1300 return (n_free_chunks > 20) ? 1 : 0;
1301}
1302
1303static void yaffs_release_space(struct file *f)
1304{
1305 struct yaffs_obj *obj;
1306 struct yaffs_dev *dev;
1307
1308 obj = yaffs_dentry_to_obj(f->f_dentry);
1309
1310 dev = obj->my_dev;
1311
1312 yaffs_gross_lock(dev);
1313
1314 yaffs_gross_unlock(dev);
1315}
1316
1317static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1318 loff_t pos, unsigned len, unsigned flags,
1319 struct page **pagep, void **fsdata)
1320{
1321 struct page *pg = NULL;
1322 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1323
1324 int ret = 0;
1325 int space_held = 0;
1326
1327 /* Get a page */
1328 pg = grab_cache_page_write_begin(mapping, index, flags);
1329
1330 *pagep = pg;
1331 if (!pg) {
1332 ret = -ENOMEM;
1333 goto out;
1334 }
1335 yaffs_trace(YAFFS_TRACE_OS,
1336 "start yaffs_write_begin index %d(%x) uptodate %d",
1337 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
1338
1339 /* Get fs space */
1340 space_held = yaffs_hold_space(filp);
1341
1342 if (!space_held) {
1343 ret = -ENOSPC;
1344 goto out;
1345 }
1346
1347 /* Update page if required */
1348
1349 if (!Page_Uptodate(pg))
1350 ret = yaffs_readpage_nolock(filp, pg);
1351
1352 if (ret)
1353 goto out;
1354
1355 /* Happy path return */
1356 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1357
1358 return 0;
1359
1360out:
1361 yaffs_trace(YAFFS_TRACE_OS,
1362 "end yaffs_write_begin fail returning %d", ret);
1363 if (space_held)
1364 yaffs_release_space(filp);
1365 if (pg) {
1366 unlock_page(pg);
1367 page_cache_release(pg);
1368 }
1369 return ret;
1370}
1371
1372static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1373 loff_t * pos)
1374{
1375 struct yaffs_obj *obj;
1376 int n_written, ipos;
1377 struct inode *inode;
1378 struct yaffs_dev *dev;
1379
1380 obj = yaffs_dentry_to_obj(f->f_dentry);
1381
1382 dev = obj->my_dev;
1383
1384 yaffs_gross_lock(dev);
1385
1386 inode = f->f_dentry->d_inode;
1387
1388 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1389 ipos = inode->i_size;
1390 else
1391 ipos = *pos;
1392
1393 if (!obj)
1394 yaffs_trace(YAFFS_TRACE_OS,
1395 "yaffs_file_write: hey obj is null!");
1396 else
1397 yaffs_trace(YAFFS_TRACE_OS,
1398 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1399 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1400
1401 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1402
1403 yaffs_touch_super(dev);
1404
1405 yaffs_trace(YAFFS_TRACE_OS,
1406 "yaffs_file_write: %d(%x) bytes written",
1407 (unsigned)n, (unsigned)n);
1408
1409 if (n_written > 0) {
1410 ipos += n_written;
1411 *pos = ipos;
1412 if (ipos > inode->i_size) {
1413 inode->i_size = ipos;
1414 inode->i_blocks = (ipos + 511) >> 9;
1415
1416 yaffs_trace(YAFFS_TRACE_OS,
1417 "yaffs_file_write size updated to %d bytes, %d blocks",
1418 ipos, (int)(inode->i_blocks));
1419 }
1420
1421 }
1422 yaffs_gross_unlock(dev);
1423 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1424}
1425
1426static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1427 loff_t pos, unsigned len, unsigned copied,
1428 struct page *pg, void *fsdadata)
1429{
1430 int ret = 0;
1431 void *addr, *kva;
1432 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1433
1434 kva = kmap(pg);
1435 addr = kva + offset_into_page;
1436
1437 yaffs_trace(YAFFS_TRACE_OS,
1438 "yaffs_write_end addr %p pos %x n_bytes %d",
1439 addr, (unsigned)pos, copied);
1440
1441 ret = yaffs_file_write(filp, addr, copied, &pos);
1442
1443 if (ret != copied) {
1444 yaffs_trace(YAFFS_TRACE_OS,
1445 "yaffs_write_end not same size ret %d copied %d",
1446 ret, copied);
1447 SetPageError(pg);
1448 }
1449
1450 kunmap(pg);
1451
1452 yaffs_release_space(filp);
1453 unlock_page(pg);
1454 page_cache_release(pg);
1455 return ret;
1456}
1457
1458static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1459{
1460 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1461 struct super_block *sb = dentry->d_sb;
1462
1463 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
1464
1465 yaffs_gross_lock(dev);
1466
1467 buf->f_type = YAFFS_MAGIC;
1468 buf->f_bsize = sb->s_blocksize;
1469 buf->f_namelen = 255;
1470
1471 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1472 /* Do this if chunk size is not a power of 2 */
1473
1474 uint64_t bytes_in_dev;
1475 uint64_t bytes_free;
1476
1477 bytes_in_dev =
1478 ((uint64_t)
1479 ((dev->param.end_block - dev->param.start_block +
1480 1))) * ((uint64_t) (dev->param.chunks_per_block *
1481 dev->data_bytes_per_chunk));
1482
1483 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
1484 buf->f_blocks = bytes_in_dev;
1485
1486 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1487 ((uint64_t) (dev->data_bytes_per_chunk));
1488
1489 do_div(bytes_free, sb->s_blocksize);
1490
1491 buf->f_bfree = bytes_free;
1492
1493 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1494
1495 buf->f_blocks =
1496 (dev->param.end_block - dev->param.start_block + 1) *
1497 dev->param.chunks_per_block /
1498 (sb->s_blocksize / dev->data_bytes_per_chunk);
1499 buf->f_bfree =
1500 yaffs_get_n_free_chunks(dev) /
1501 (sb->s_blocksize / dev->data_bytes_per_chunk);
1502 } else {
1503 buf->f_blocks =
1504 (dev->param.end_block - dev->param.start_block + 1) *
1505 dev->param.chunks_per_block *
1506 (dev->data_bytes_per_chunk / sb->s_blocksize);
1507
1508 buf->f_bfree =
1509 yaffs_get_n_free_chunks(dev) *
1510 (dev->data_bytes_per_chunk / sb->s_blocksize);
1511 }
1512
1513 buf->f_files = 0;
1514 buf->f_ffree = 0;
1515 buf->f_bavail = buf->f_bfree;
1516
1517 yaffs_gross_unlock(dev);
1518 return 0;
1519}
1520
1521static void yaffs_flush_inodes(struct super_block *sb)
1522{
1523 struct inode *iptr;
1524 struct yaffs_obj *obj;
1525
1526 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1527 obj = yaffs_inode_to_obj(iptr);
1528 if (obj) {
1529 yaffs_trace(YAFFS_TRACE_OS,
1530 "flushing obj %d", obj->obj_id);
1531 yaffs_flush_file(obj, 1, 0);
1532 }
1533 }
1534}
1535
1536static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1537{
1538 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1539 if (!dev)
1540 return;
1541
1542 yaffs_flush_inodes(sb);
1543 yaffs_update_dirty_dirs(dev);
1544 yaffs_flush_whole_cache(dev);
1545 if (do_checkpoint)
1546 yaffs_checkpoint_save(dev);
1547}
1548
1549static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1550{
1551 unsigned erased_chunks =
1552 dev->n_erased_blocks * dev->param.chunks_per_block;
1553 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1554 unsigned scattered = 0; /* Free chunks not in an erased block */
1555
1556 if (erased_chunks < dev->n_free_chunks)
1557 scattered = (dev->n_free_chunks - erased_chunks);
1558
1559 if (!context->bg_running)
1560 return 0;
1561 else if (scattered < (dev->param.chunks_per_block * 2))
1562 return 0;
1563 else if (erased_chunks > dev->n_free_chunks / 2)
1564 return 0;
1565 else if (erased_chunks > dev->n_free_chunks / 4)
1566 return 1;
1567 else
1568 return 2;
1569}
1570
1571static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1572{
1573
1574 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1575 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1576 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1577 int do_checkpoint;
1578
1579 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1580 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
1581 gc_urgent,
1582 sb->s_dirt ? "dirty" : "clean",
1583 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1584 oneshot_checkpoint ? " one-shot" : "");
1585
1586 yaffs_gross_lock(dev);
1587 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1588 oneshot_checkpoint) && !dev->is_checkpointed;
1589
1590 if (sb->s_dirt || do_checkpoint) {
1591 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1592 sb->s_dirt = 0;
1593 if (oneshot_checkpoint)
1594 yaffs_auto_checkpoint &= ~4;
1595 }
1596 yaffs_gross_unlock(dev);
1597
1598 return 0;
1599}
1600
1601/*
1602 * yaffs background thread functions .
1603 * yaffs_bg_thread_fn() the thread function
1604 * yaffs_bg_start() launches the background thread.
1605 * yaffs_bg_stop() cleans up the background thread.
1606 *
1607 * NB:
1608 * The thread should only run after the yaffs is initialised
1609 * The thread should be stopped before yaffs is unmounted.
1610 * The thread should not do any writing while the fs is in read only.
1611 */
1612
1613void yaffs_background_waker(unsigned long data)
1614{
1615 wake_up_process((struct task_struct *)data);
1616}
1617
1618static int yaffs_bg_thread_fn(void *data)
1619{
1620 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1621 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1622 unsigned long now = jiffies;
1623 unsigned long next_dir_update = now;
1624 unsigned long next_gc = now;
1625 unsigned long expires;
1626 unsigned int urgency;
1627
1628 int gc_result;
1629 struct timer_list timer;
1630
1631 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1632 "yaffs_background starting for dev %p", (void *)dev);
1633
1634 set_freezable();
1635 while (context->bg_running) {
1636 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1637
1638 if (kthread_should_stop())
1639 break;
1640
1641 if (try_to_freeze())
1642 continue;
1643
1644 yaffs_gross_lock(dev);
1645
1646 now = jiffies;
1647
1648 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1649 yaffs_update_dirty_dirs(dev);
1650 next_dir_update = now + HZ;
1651 }
1652
1653 if (time_after(now, next_gc) && yaffs_bg_enable) {
1654 if (!dev->is_checkpointed) {
1655 urgency = yaffs_bg_gc_urgency(dev);
1656 gc_result = yaffs_bg_gc(dev, urgency);
1657 if (urgency > 1)
1658 next_gc = now + HZ / 20 + 1;
1659 else if (urgency > 0)
1660 next_gc = now + HZ / 10 + 1;
1661 else
1662 next_gc = now + HZ * 2;
1663 } else {
1664 /*
1665 * gc not running so set to next_dir_update
1666 * to cut down on wake ups
1667 */
1668 next_gc = next_dir_update;
1669 }
1670 }
1671 yaffs_gross_unlock(dev);
1672 expires = next_dir_update;
1673 if (time_before(next_gc, expires))
1674 expires = next_gc;
1675 if (time_before(expires, now))
1676 expires = now + HZ;
1677
1678 Y_INIT_TIMER(&timer);
1679 timer.expires = expires + 1;
1680 timer.data = (unsigned long)current;
1681 timer.function = yaffs_background_waker;
1682
1683 set_current_state(TASK_INTERRUPTIBLE);
1684 add_timer(&timer);
1685 schedule();
1686 del_timer_sync(&timer);
1687 }
1688
1689 return 0;
1690}
1691
1692static int yaffs_bg_start(struct yaffs_dev *dev)
1693{
1694 int retval = 0;
1695 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1696
1697 if (dev->read_only)
1698 return -1;
1699
1700 context->bg_running = 1;
1701
1702 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1703 (void *)dev, "yaffs-bg-%d",
1704 context->mount_id);
1705
1706 if (IS_ERR(context->bg_thread)) {
1707 retval = PTR_ERR(context->bg_thread);
1708 context->bg_thread = NULL;
1709 context->bg_running = 0;
1710 }
1711 return retval;
1712}
1713
1714static void yaffs_bg_stop(struct yaffs_dev *dev)
1715{
1716 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1717
1718 ctxt->bg_running = 0;
1719
1720 if (ctxt->bg_thread) {
1721 kthread_stop(ctxt->bg_thread);
1722 ctxt->bg_thread = NULL;
1723 }
1724}
1725
1726static void yaffs_write_super(struct super_block *sb)
1727{
1728 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1729
1730 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1731 "yaffs_write_super%s",
1732 request_checkpoint ? " checkpt" : "");
1733
1734 yaffs_do_sync_fs(sb, request_checkpoint);
1735
1736}
1737
1738static int yaffs_sync_fs(struct super_block *sb, int wait)
1739{
1740 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1741
1742 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1743 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1744
1745 yaffs_do_sync_fs(sb, request_checkpoint);
1746
1747 return 0;
1748}
1749
1750
1751static LIST_HEAD(yaffs_context_list);
1752struct mutex yaffs_context_lock;
1753
1754
1755
1756struct yaffs_options {
1757 int inband_tags;
1758 int skip_checkpoint_read;
1759 int skip_checkpoint_write;
1760 int no_cache;
1761 int tags_ecc_on;
1762 int tags_ecc_overridden;
1763 int lazy_loading_enabled;
1764 int lazy_loading_overridden;
1765 int empty_lost_and_found;
1766 int empty_lost_and_found_overridden;
1767};
1768
1769#define MAX_OPT_LEN 30
1770static int yaffs_parse_options(struct yaffs_options *options,
1771 const char *options_str)
1772{
1773 char cur_opt[MAX_OPT_LEN + 1];
1774 int p;
1775 int error = 0;
1776
1777 /* Parse through the options which is a comma seperated list */
1778
1779 while (options_str && *options_str && !error) {
1780 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1781 p = 0;
1782
1783 while (*options_str == ',')
1784 options_str++;
1785
1786 while (*options_str && *options_str != ',') {
1787 if (p < MAX_OPT_LEN) {
1788 cur_opt[p] = *options_str;
1789 p++;
1790 }
1791 options_str++;
1792 }
1793
1794 if (!strcmp(cur_opt, "inband-tags")) {
1795 options->inband_tags = 1;
1796 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1797 options->tags_ecc_on = 0;
1798 options->tags_ecc_overridden = 1;
1799 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1800 options->tags_ecc_on = 1;
1801 options->tags_ecc_overridden = 1;
1802 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1803 options->lazy_loading_enabled = 0;
1804 options->lazy_loading_overridden = 1;
1805 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1806 options->lazy_loading_enabled = 1;
1807 options->lazy_loading_overridden = 1;
1808 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1809 options->empty_lost_and_found = 0;
1810 options->empty_lost_and_found_overridden = 1;
1811 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1812 options->empty_lost_and_found = 1;
1813 options->empty_lost_and_found_overridden = 1;
1814 } else if (!strcmp(cur_opt, "no-cache")) {
1815 options->no_cache = 1;
1816 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1817 options->skip_checkpoint_read = 1;
1818 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1819 options->skip_checkpoint_write = 1;
1820 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1821 options->skip_checkpoint_read = 1;
1822 options->skip_checkpoint_write = 1;
1823 } else {
1824 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1825 cur_opt);
1826 error = 1;
1827 }
1828 }
1829
1830 return error;
1831}
1832
1833static struct address_space_operations yaffs_file_address_operations = {
1834 .readpage = yaffs_readpage,
1835 .writepage = yaffs_writepage,
1836 .write_begin = yaffs_write_begin,
1837 .write_end = yaffs_write_end,
1838};
1839
1840
1841
1842static const struct inode_operations yaffs_file_inode_operations = {
1843 .setattr = yaffs_setattr,
1844#ifdef CONFIG_YAFFS_XATTR
1845 .setxattr = yaffs_setxattr,
1846 .getxattr = yaffs_getxattr,
1847 .listxattr = yaffs_listxattr,
1848 .removexattr = yaffs_removexattr,
1849#endif
1850};
1851
1852static const struct inode_operations yaffs_symlink_inode_operations = {
1853 .readlink = yaffs_readlink,
1854 .follow_link = yaffs_follow_link,
1855 .put_link = yaffs_put_link,
1856 .setattr = yaffs_setattr,
1857#ifdef CONFIG_YAFFS_XATTR
1858 .setxattr = yaffs_setxattr,
1859 .getxattr = yaffs_getxattr,
1860 .listxattr = yaffs_listxattr,
1861 .removexattr = yaffs_removexattr,
1862#endif
1863};
1864
1865static void yaffs_fill_inode_from_obj(struct inode *inode,
1866 struct yaffs_obj *obj)
1867{
1868 if (inode && obj) {
1869
1870 /* Check mode against the variant type and attempt to repair if broken. */
1871 u32 mode = obj->yst_mode;
1872 switch (obj->variant_type) {
1873 case YAFFS_OBJECT_TYPE_FILE:
1874 if (!S_ISREG(mode)) {
1875 obj->yst_mode &= ~S_IFMT;
1876 obj->yst_mode |= S_IFREG;
1877 }
1878
1879 break;
1880 case YAFFS_OBJECT_TYPE_SYMLINK:
1881 if (!S_ISLNK(mode)) {
1882 obj->yst_mode &= ~S_IFMT;
1883 obj->yst_mode |= S_IFLNK;
1884 }
1885
1886 break;
1887 case YAFFS_OBJECT_TYPE_DIRECTORY:
1888 if (!S_ISDIR(mode)) {
1889 obj->yst_mode &= ~S_IFMT;
1890 obj->yst_mode |= S_IFDIR;
1891 }
1892
1893 break;
1894 case YAFFS_OBJECT_TYPE_UNKNOWN:
1895 case YAFFS_OBJECT_TYPE_HARDLINK:
1896 case YAFFS_OBJECT_TYPE_SPECIAL:
1897 default:
1898 /* TODO? */
1899 break;
1900 }
1901
1902 inode->i_flags |= S_NOATIME;
1903
1904 inode->i_ino = obj->obj_id;
1905 inode->i_mode = obj->yst_mode;
1906 inode->i_uid = obj->yst_uid;
1907 inode->i_gid = obj->yst_gid;
1908
1909 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1910
1911 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1912 inode->i_atime.tv_nsec = 0;
1913 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1914 inode->i_mtime.tv_nsec = 0;
1915 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1916 inode->i_ctime.tv_nsec = 0;
1917 inode->i_size = yaffs_get_obj_length(obj);
1918 inode->i_blocks = (inode->i_size + 511) >> 9;
1919
1920 inode->i_nlink = yaffs_get_obj_link_count(obj);
1921
1922 yaffs_trace(YAFFS_TRACE_OS,
1923 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1924 inode->i_mode, inode->i_uid, inode->i_gid,
1925 (int)inode->i_size, atomic_read(&inode->i_count));
1926
1927 switch (obj->yst_mode & S_IFMT) {
1928 default: /* fifo, device or socket */
1929 init_special_inode(inode, obj->yst_mode,
1930 old_decode_dev(obj->yst_rdev));
1931 break;
1932 case S_IFREG: /* file */
1933 inode->i_op = &yaffs_file_inode_operations;
1934 inode->i_fop = &yaffs_file_operations;
1935 inode->i_mapping->a_ops =
1936 &yaffs_file_address_operations;
1937 break;
1938 case S_IFDIR: /* directory */
1939 inode->i_op = &yaffs_dir_inode_operations;
1940 inode->i_fop = &yaffs_dir_operations;
1941 break;
1942 case S_IFLNK: /* symlink */
1943 inode->i_op = &yaffs_symlink_inode_operations;
1944 break;
1945 }
1946
1947 yaffs_inode_to_obj_lv(inode) = obj;
1948
1949 obj->my_inode = inode;
1950
1951 } else {
1952 yaffs_trace(YAFFS_TRACE_OS,
1953 "yaffs_fill_inode invalid parameters");
1954 }
1955}
1956
1957static void yaffs_put_super(struct super_block *sb)
1958{
1959 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1960
1961 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1962
1963 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1964 "Shutting down yaffs background thread");
1965 yaffs_bg_stop(dev);
1966 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1967 "yaffs background thread shut down");
1968
1969 yaffs_gross_lock(dev);
1970
1971 yaffs_flush_super(sb, 1);
1972
1973 if (yaffs_dev_to_lc(dev)->put_super_fn)
1974 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1975
1976 yaffs_deinitialise(dev);
1977
1978 yaffs_gross_unlock(dev);
1979 mutex_lock(&yaffs_context_lock);
1980 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1981 mutex_unlock(&yaffs_context_lock);
1982
1983 if (yaffs_dev_to_lc(dev)->spare_buffer) {
1984 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1985 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1986 }
1987
1988 kfree(dev);
1989}
1990
1991static void yaffs_mtd_put_super(struct super_block *sb)
1992{
1993 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1994
1995 if (mtd->sync)
1996 mtd->sync(mtd);
1997
1998 put_mtd_device(mtd);
1999}
2000
2001static const struct super_operations yaffs_super_ops = {
2002 .statfs = yaffs_statfs,
2003 .put_super = yaffs_put_super,
2004 .evict_inode = yaffs_evict_inode,
2005 .sync_fs = yaffs_sync_fs,
2006 .write_super = yaffs_write_super,
2007};
2008
2009static struct super_block *yaffs_internal_read_super(int yaffs_version,
2010 struct super_block *sb,
2011 void *data, int silent)
2012{
2013 int n_blocks;
2014 struct inode *inode = NULL;
2015 struct dentry *root;
2016 struct yaffs_dev *dev = 0;
2017 char devname_buf[BDEVNAME_SIZE + 1];
2018 struct mtd_info *mtd;
2019 int err;
2020 char *data_str = (char *)data;
2021 struct yaffs_linux_context *context = NULL;
2022 struct yaffs_param *param;
2023
2024 int read_only = 0;
2025
2026 struct yaffs_options options;
2027
2028 unsigned mount_id;
2029 int found;
2030 struct yaffs_linux_context *context_iterator;
2031 struct list_head *l;
2032
2033 sb->s_magic = YAFFS_MAGIC;
2034 sb->s_op = &yaffs_super_ops;
2035 sb->s_flags |= MS_NOATIME;
2036
2037 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2038
2039 sb->s_export_op = &yaffs_export_ops;
2040
2041 if (!sb)
2042 printk(KERN_INFO "yaffs: sb is NULL\n");
2043 else if (!sb->s_dev)
2044 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2045 else if (!yaffs_devname(sb, devname_buf))
2046 printk(KERN_INFO "yaffs: devname is NULL\n");
2047 else
2048 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2049 sb->s_dev,
2050 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2051
2052 if (!data_str)
2053 data_str = "";
2054
2055 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2056
2057 memset(&options, 0, sizeof(options));
2058
2059 if (yaffs_parse_options(&options, data_str)) {
2060 /* Option parsing failed */
2061 return NULL;
2062 }
2063
2064 sb->s_blocksize = PAGE_CACHE_SIZE;
2065 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2066
2067 yaffs_trace(YAFFS_TRACE_OS,
2068 "yaffs_read_super: Using yaffs%d", yaffs_version);
2069 yaffs_trace(YAFFS_TRACE_OS,
2070 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2071
2072 yaffs_trace(YAFFS_TRACE_ALWAYS,
2073 "Attempting MTD mount of %u.%u,\"%s\"",
2074 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2075 yaffs_devname(sb, devname_buf));
2076
2077 /* Check it's an mtd device..... */
2078 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2079 return NULL; /* This isn't an mtd device */
2080
2081 /* Get the device */
2082 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2083 if (!mtd) {
2084 yaffs_trace(YAFFS_TRACE_ALWAYS,
2085 "MTD device #%u doesn't appear to exist",
2086 MINOR(sb->s_dev));
2087 return NULL;
2088 }
2089 /* Check it's NAND */
2090 if (mtd->type != MTD_NANDFLASH) {
2091 yaffs_trace(YAFFS_TRACE_ALWAYS,
2092 "MTD device is not NAND it's type %d",
2093 mtd->type);
2094 return NULL;
2095 }
2096
2097 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2098 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2099 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2100 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2101 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2102 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2103 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2104 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2105 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2106 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2107 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2108
2109#ifdef CONFIG_YAFFS_AUTO_YAFFS2
2110
2111 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2112 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2113 yaffs_version = 2;
2114 }
2115
2116 /* Added NCB 26/5/2006 for completeness */
2117 if (yaffs_version == 2 && !options.inband_tags
2118 && WRITE_SIZE(mtd) == 512) {
2119 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2120 yaffs_version = 1;
2121 }
2122#endif
2123
2124 if (yaffs_version == 2) {
2125 /* Check for version 2 style functions */
2126 if (!mtd->erase ||
2127 !mtd->block_isbad ||
2128 !mtd->block_markbad ||
2129 !mtd->read ||
2130 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2131 yaffs_trace(YAFFS_TRACE_ALWAYS,
2132 "MTD device does not support required functions");
2133 return NULL;
2134 }
2135
2136 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2137 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2138 !options.inband_tags) {
2139 yaffs_trace(YAFFS_TRACE_ALWAYS,
2140 "MTD device does not have the right page sizes");
2141 return NULL;
2142 }
2143 } else {
2144 /* Check for V1 style functions */
2145 if (!mtd->erase ||
2146 !mtd->read ||
2147 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2148 yaffs_trace(YAFFS_TRACE_ALWAYS,
2149 "MTD device does not support required functions");
2150 return NULL;
2151 }
2152
2153 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2154 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2155 yaffs_trace(YAFFS_TRACE_ALWAYS,
2156 "MTD device does not support have the right page sizes");
2157 return NULL;
2158 }
2159 }
2160
2161 /* OK, so if we got here, we have an MTD that's NAND and looks
2162 * like it has the right capabilities
2163 * Set the struct yaffs_dev up for mtd
2164 */
2165
2166 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2167 read_only = 1;
2168 printk(KERN_INFO
2169 "yaffs: mtd is read only, setting superblock read only");
2170 sb->s_flags |= MS_RDONLY;
2171 }
2172
2173 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2174 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2175
2176 if (!dev || !context) {
2177 if (dev)
2178 kfree(dev);
2179 if (context)
2180 kfree(context);
2181 dev = NULL;
2182 context = NULL;
2183 }
2184
2185 if (!dev) {
2186 /* Deep shit could not allocate device structure */
2187 yaffs_trace(YAFFS_TRACE_ALWAYS,
2188 "yaffs_read_super failed trying to allocate yaffs_dev");
2189 return NULL;
2190 }
2191 memset(dev, 0, sizeof(struct yaffs_dev));
2192 param = &(dev->param);
2193
2194 memset(context, 0, sizeof(struct yaffs_linux_context));
2195 dev->os_context = context;
2196 INIT_LIST_HEAD(&(context->context_list));
2197 context->dev = dev;
2198 context->super = sb;
2199
2200 dev->read_only = read_only;
2201
2202 sb->s_fs_info = dev;
2203
2204 dev->driver_context = mtd;
2205 param->name = mtd->name;
2206
2207 /* Set up the memory size parameters.... */
2208
2209 n_blocks =
2210 YCALCBLOCKS(mtd->size,
2211 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2212
2213 param->start_block = 0;
2214 param->end_block = n_blocks - 1;
2215 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2216 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2217 param->n_reserved_blocks = 5;
2218 param->n_caches = (options.no_cache) ? 0 : 10;
2219 param->inband_tags = options.inband_tags;
2220
2221#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2222 param->disable_lazy_load = 1;
2223#endif
2224#ifdef CONFIG_YAFFS_XATTR
2225 param->enable_xattr = 1;
2226#endif
2227 if (options.lazy_loading_overridden)
2228 param->disable_lazy_load = !options.lazy_loading_enabled;
2229
2230#ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2231 param->no_tags_ecc = 1;
2232#endif
2233
2234#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2235#else
2236 param->defered_dir_update = 1;
2237#endif
2238
2239 if (options.tags_ecc_overridden)
2240 param->no_tags_ecc = !options.tags_ecc_on;
2241
2242#ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2243 param->empty_lost_n_found = 1;
2244#endif
2245
2246#ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2247 param->refresh_period = 0;
2248#else
2249 param->refresh_period = 500;
2250#endif
2251
2252#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2253 param->always_check_erased = 1;
2254#endif
2255
2256 if (options.empty_lost_and_found_overridden)
2257 param->empty_lost_n_found = options.empty_lost_and_found;
2258
2259 /* ... and the functions. */
2260 if (yaffs_version == 2) {
2261 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2262 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2263 param->bad_block_fn = nandmtd2_mark_block_bad;
2264 param->query_block_fn = nandmtd2_query_block;
2265 yaffs_dev_to_lc(dev)->spare_buffer =
2266 kmalloc(mtd->oobsize, GFP_NOFS);
2267 param->is_yaffs2 = 1;
2268 param->total_bytes_per_chunk = mtd->writesize;
2269 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2270 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2271
2272 param->start_block = 0;
2273 param->end_block = n_blocks - 1;
2274 } else {
2275 /* use the MTD interface in yaffs_mtdif1.c */
2276 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2277 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2278 param->bad_block_fn = nandmtd1_mark_block_bad;
2279 param->query_block_fn = nandmtd1_query_block;
2280 param->is_yaffs2 = 0;
2281 }
2282 /* ... and common functions */
2283 param->erase_fn = nandmtd_erase_block;
2284 param->initialise_flash_fn = nandmtd_initialise;
2285
2286 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2287
2288 param->sb_dirty_fn = yaffs_touch_super;
2289 param->gc_control = yaffs_gc_control_callback;
2290
2291 yaffs_dev_to_lc(dev)->super = sb;
2292
2293#ifndef CONFIG_YAFFS_DOES_ECC
2294 param->use_nand_ecc = 1;
2295#endif
2296
2297 param->skip_checkpt_rd = options.skip_checkpoint_read;
2298 param->skip_checkpt_wr = options.skip_checkpoint_write;
2299
2300 mutex_lock(&yaffs_context_lock);
2301 /* Get a mount id */
2302 found = 0;
2303 for (mount_id = 0; !found; mount_id++) {
2304 found = 1;
2305 list_for_each(l, &yaffs_context_list) {
2306 context_iterator =
2307 list_entry(l, struct yaffs_linux_context,
2308 context_list);
2309 if (context_iterator->mount_id == mount_id)
2310 found = 0;
2311 }
2312 }
2313 context->mount_id = mount_id;
2314
2315 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2316 &yaffs_context_list);
2317 mutex_unlock(&yaffs_context_lock);
2318
2319 /* Directory search handling... */
2320 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2321 param->remove_obj_fn = yaffs_remove_obj_callback;
2322
2323 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2324
2325 yaffs_gross_lock(dev);
2326
2327 err = yaffs_guts_initialise(dev);
2328
2329 yaffs_trace(YAFFS_TRACE_OS,
2330 "yaffs_read_super: guts initialised %s",
2331 (err == YAFFS_OK) ? "OK" : "FAILED");
2332
2333 if (err == YAFFS_OK)
2334 yaffs_bg_start(dev);
2335
2336 if (!context->bg_thread)
2337 param->defered_dir_update = 0;
2338
2339 /* Release lock before yaffs_get_inode() */
2340 yaffs_gross_unlock(dev);
2341
2342 /* Create root inode */
2343 if (err == YAFFS_OK)
2344 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2345
2346 if (!inode)
2347 return NULL;
2348
2349 inode->i_op = &yaffs_dir_inode_operations;
2350 inode->i_fop = &yaffs_dir_operations;
2351
2352 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2353
2354 root = d_alloc_root(inode);
2355
2356 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2357
2358 if (!root) {
2359 iput(inode);
2360 return NULL;
2361 }
2362 sb->s_root = root;
2363 sb->s_dirt = !dev->is_checkpointed;
2364 yaffs_trace(YAFFS_TRACE_ALWAYS,
2365 "yaffs_read_super: is_checkpointed %d",
2366 dev->is_checkpointed);
2367
2368 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2369 return sb;
2370}
2371
2372static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2373 int silent)
2374{
2375 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2376}
2377
2378static int yaffs_read_super(struct file_system_type *fs,
2379 int flags, const char *dev_name,
2380 void *data, struct vfsmount *mnt)
2381{
2382
2383 return get_sb_bdev(fs, flags, dev_name, data,
2384 yaffs_internal_read_super_mtd, mnt);
2385}
2386
2387static struct file_system_type yaffs_fs_type = {
2388 .owner = THIS_MODULE,
2389 .name = "yaffs",
2390 .get_sb = yaffs_read_super,
2391 .kill_sb = kill_block_super,
2392 .fs_flags = FS_REQUIRES_DEV,
2393};
2394
2395#ifdef CONFIG_YAFFS_YAFFS2
2396
2397static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2398 int silent)
2399{
2400 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2401}
2402
2403static int yaffs2_read_super(struct file_system_type *fs,
2404 int flags, const char *dev_name, void *data,
2405 struct vfsmount *mnt)
2406{
2407 return get_sb_bdev(fs, flags, dev_name, data,
2408 yaffs2_internal_read_super_mtd, mnt);
2409}
2410
2411static struct file_system_type yaffs2_fs_type = {
2412 .owner = THIS_MODULE,
2413 .name = "yaffs2",
2414 .get_sb = yaffs2_read_super,
2415 .kill_sb = kill_block_super,
2416 .fs_flags = FS_REQUIRES_DEV,
2417};
2418#endif /* CONFIG_YAFFS_YAFFS2 */
2419
2420static struct proc_dir_entry *my_proc_entry;
2421
2422static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2423{
2424 struct yaffs_param *param = &dev->param;
2425 buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2426 buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2427 buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2428 param->total_bytes_per_chunk);
2429 buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2430 param->use_nand_ecc);
2431 buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2432 buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2433 buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2434 buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2435 param->empty_lost_n_found);
2436 buf += sprintf(buf, "disable_lazy_load..... %d\n",
2437 param->disable_lazy_load);
2438 buf += sprintf(buf, "refresh_period........ %d\n",
2439 param->refresh_period);
2440 buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2441 buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2442 param->n_reserved_blocks);
2443 buf += sprintf(buf, "always_check_erased... %d\n",
2444 param->always_check_erased);
2445
2446 return buf;
2447}
2448
2449static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2450{
2451 buf +=
2452 sprintf(buf, "data_bytes_per_chunk.. %d\n",
2453 dev->data_bytes_per_chunk);
2454 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2455 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2456 buf +=
2457 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2458 buf +=
2459 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2460 buf += sprintf(buf, "\n");
2461 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2462 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2463 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2464 buf += sprintf(buf, "\n");
2465 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2466 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2467 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2468 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2469 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2470 buf +=
2471 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2472 buf +=
2473 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2474 dev->oldest_dirty_gc_count);
2475 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2476 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2477 buf +=
2478 sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2479 buf +=
2480 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2481 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2482 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2483 buf +=
2484 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2485 buf +=
2486 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2487 dev->n_tags_ecc_unfixed);
2488 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2489 buf +=
2490 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2491 buf +=
2492 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2493 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2494 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2495
2496 return buf;
2497}
2498
2499static int yaffs_proc_read(char *page,
2500 char **start,
2501 off_t offset, int count, int *eof, void *data)
2502{
2503 struct list_head *item;
2504 char *buf = page;
2505 int step = offset;
2506 int n = 0;
2507
2508 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2509 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2510 * This also assumes the user has posted a read buffer large
2511 * enough to hold the complete output; but that's life in /proc.
2512 */
2513
2514 *(int *)start = 1;
2515
2516 /* Print header first */
2517 if (step == 0)
2518 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2519 else if (step == 1)
2520 buf += sprintf(buf, "\n");
2521 else {
2522 step -= 2;
2523
2524 mutex_lock(&yaffs_context_lock);
2525
2526 /* Locate and print the Nth entry. Order N-squared but N is small. */
2527 list_for_each(item, &yaffs_context_list) {
2528 struct yaffs_linux_context *dc =
2529 list_entry(item, struct yaffs_linux_context,
2530 context_list);
2531 struct yaffs_dev *dev = dc->dev;
2532
2533 if (n < (step & ~1)) {
2534 n += 2;
2535 continue;
2536 }
2537 if ((step & 1) == 0) {
2538 buf +=
2539 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2540 dev->param.name);
2541 buf = yaffs_dump_dev_part0(buf, dev);
2542 } else {
2543 buf = yaffs_dump_dev_part1(buf, dev);
2544 }
2545
2546 break;
2547 }
2548 mutex_unlock(&yaffs_context_lock);
2549 }
2550
2551 return buf - page < count ? buf - page : count;
2552}
2553
2554
2555/**
2556 * Set the verbosity of the warnings and error messages.
2557 *
2558 * Note that the names can only be a..z or _ with the current code.
2559 */
2560
2561static struct {
2562 char *mask_name;
2563 unsigned mask_bitfield;
2564} mask_flags[] = {
2565 {"allocate", YAFFS_TRACE_ALLOCATE},
2566 {"always", YAFFS_TRACE_ALWAYS},
2567 {"background", YAFFS_TRACE_BACKGROUND},
2568 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2569 {"buffers", YAFFS_TRACE_BUFFERS},
2570 {"bug", YAFFS_TRACE_BUG},
2571 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2572 {"deletion", YAFFS_TRACE_DELETION},
2573 {"erase", YAFFS_TRACE_ERASE},
2574 {"error", YAFFS_TRACE_ERROR},
2575 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2576 {"gc", YAFFS_TRACE_GC},
2577 {"lock", YAFFS_TRACE_LOCK},
2578 {"mtd", YAFFS_TRACE_MTD},
2579 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2580 {"os", YAFFS_TRACE_OS},
2581 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2582 {"scan", YAFFS_TRACE_SCAN},
2583 {"mount", YAFFS_TRACE_MOUNT},
2584 {"tracing", YAFFS_TRACE_TRACING},
2585 {"sync", YAFFS_TRACE_SYNC},
2586 {"write", YAFFS_TRACE_WRITE},
2587 {"verify", YAFFS_TRACE_VERIFY},
2588 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2589 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2590 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2591 {"all", 0xffffffff},
2592 {"none", 0},
2593 {NULL, 0},
2594};
2595
2596#define MAX_MASK_NAME_LENGTH 40
2597static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2598 unsigned long count, void *data)
2599{
2600 unsigned rg = 0, mask_bitfield;
2601 char *end;
2602 char *mask_name;
2603 const char *x;
2604 char substring[MAX_MASK_NAME_LENGTH + 1];
2605 int i;
2606 int done = 0;
2607 int add, len = 0;
2608 int pos = 0;
2609
2610 rg = yaffs_trace_mask;
2611
2612 while (!done && (pos < count)) {
2613 done = 1;
2614 while ((pos < count) && isspace(buf[pos]))
2615 pos++;
2616
2617 switch (buf[pos]) {
2618 case '+':
2619 case '-':
2620 case '=':
2621 add = buf[pos];
2622 pos++;
2623 break;
2624
2625 default:
2626 add = ' ';
2627 break;
2628 }
2629 mask_name = NULL;
2630
2631 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2632
2633 if (end > buf + pos) {
2634 mask_name = "numeral";
2635 len = end - (buf + pos);
2636 pos += len;
2637 done = 0;
2638 } else {
2639 for (x = buf + pos, i = 0;
2640 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2641 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2642 substring[i] = *x;
2643 substring[i] = '\0';
2644
2645 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2646 if (strcmp(substring, mask_flags[i].mask_name)
2647 == 0) {
2648 mask_name = mask_flags[i].mask_name;
2649 mask_bitfield =
2650 mask_flags[i].mask_bitfield;
2651 done = 0;
2652 break;
2653 }
2654 }
2655 }
2656
2657 if (mask_name != NULL) {
2658 done = 0;
2659 switch (add) {
2660 case '-':
2661 rg &= ~mask_bitfield;
2662 break;
2663 case '+':
2664 rg |= mask_bitfield;
2665 break;
2666 case '=':
2667 rg = mask_bitfield;
2668 break;
2669 default:
2670 rg |= mask_bitfield;
2671 break;
2672 }
2673 }
2674 }
2675
2676 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2677
2678 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2679
2680 if (rg & YAFFS_TRACE_ALWAYS) {
2681 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2682 char flag;
2683 flag = ((rg & mask_flags[i].mask_bitfield) ==
2684 mask_flags[i].mask_bitfield) ? '+' : '-';
2685 printk(KERN_DEBUG "%c%s\n", flag,
2686 mask_flags[i].mask_name);
2687 }
2688 }
2689
2690 return count;
2691}
2692
2693static int yaffs_proc_write(struct file *file, const char *buf,
2694 unsigned long count, void *data)
2695{
2696 return yaffs_proc_write_trace_options(file, buf, count, data);
2697}
2698
2699/* Stuff to handle installation of file systems */
2700struct file_system_to_install {
2701 struct file_system_type *fst;
2702 int installed;
2703};
2704
2705static struct file_system_to_install fs_to_install[] = {
2706 {&yaffs_fs_type, 0},
2707 {&yaffs2_fs_type, 0},
2708 {NULL, 0}
2709};
2710
2711static int __init init_yaffs_fs(void)
2712{
2713 int error = 0;
2714 struct file_system_to_install *fsinst;
2715
2716 yaffs_trace(YAFFS_TRACE_ALWAYS,
2717 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2718
2719#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2720 yaffs_trace(YAFFS_TRACE_ALWAYS,
2721 "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n");
2722#endif
2723
2724 mutex_init(&yaffs_context_lock);
2725
2726 /* Install the proc_fs entries */
2727 my_proc_entry = create_proc_entry("yaffs",
2728 S_IRUGO | S_IFREG, YPROC_ROOT);
2729
2730 if (my_proc_entry) {
2731 my_proc_entry->write_proc = yaffs_proc_write;
2732 my_proc_entry->read_proc = yaffs_proc_read;
2733 my_proc_entry->data = NULL;
2734 } else {
2735 return -ENOMEM;
2736 }
2737
2738
2739 /* Now add the file system entries */
2740
2741 fsinst = fs_to_install;
2742
2743 while (fsinst->fst && !error) {
2744 error = register_filesystem(fsinst->fst);
2745 if (!error)
2746 fsinst->installed = 1;
2747 fsinst++;
2748 }
2749
2750 /* Any errors? uninstall */
2751 if (error) {
2752 fsinst = fs_to_install;
2753
2754 while (fsinst->fst) {
2755 if (fsinst->installed) {
2756 unregister_filesystem(fsinst->fst);
2757 fsinst->installed = 0;
2758 }
2759 fsinst++;
2760 }
2761 }
2762
2763 return error;
2764}
2765
2766static void __exit exit_yaffs_fs(void)
2767{
2768
2769 struct file_system_to_install *fsinst;
2770
2771 yaffs_trace(YAFFS_TRACE_ALWAYS,
2772 "yaffs built " __DATE__ " " __TIME__ " removing.");
2773
2774 remove_proc_entry("yaffs", YPROC_ROOT);
2775
2776 fsinst = fs_to_install;
2777
2778 while (fsinst->fst) {
2779 if (fsinst->installed) {
2780 unregister_filesystem(fsinst->fst);
2781 fsinst->installed = 0;
2782 }
2783 fsinst++;
2784 }
2785}
2786
2787module_init(init_yaffs_fs)
2788 module_exit(exit_yaffs_fs)
2789
2790 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2791MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
2792MODULE_LICENSE("GPL");
diff --git a/fs/yaffs2/yaffs_yaffs1.c b/fs/yaffs2/yaffs_yaffs1.c
new file mode 100644
index 00000000000..9eb60308254
--- /dev/null
+++ b/fs/yaffs2/yaffs_yaffs1.c
@@ -0,0 +1,433 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_yaffs1.h"
15#include "yportenv.h"
16#include "yaffs_trace.h"
17#include "yaffs_bitmap.h"
18#include "yaffs_getblockinfo.h"
19#include "yaffs_nand.h"
20#include "yaffs_attribs.h"
21
22int yaffs1_scan(struct yaffs_dev *dev)
23{
24 struct yaffs_ext_tags tags;
25 int blk;
26 int result;
27
28 int chunk;
29 int c;
30 int deleted;
31 enum yaffs_block_state state;
32 struct yaffs_obj *hard_list = NULL;
33 struct yaffs_block_info *bi;
34 u32 seq_number;
35 struct yaffs_obj_hdr *oh;
36 struct yaffs_obj *in;
37 struct yaffs_obj *parent;
38
39 int alloc_failed = 0;
40
41 struct yaffs_shadow_fixer *shadow_fixers = NULL;
42
43 u8 *chunk_data;
44
45 yaffs_trace(YAFFS_TRACE_SCAN,
46 "yaffs1_scan starts intstartblk %d intendblk %d...",
47 dev->internal_start_block, dev->internal_end_block);
48
49 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
50
51 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
52
53 /* Scan all the blocks to determine their state */
54 bi = dev->block_info;
55 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
56 blk++) {
57 yaffs_clear_chunk_bits(dev, blk);
58 bi->pages_in_use = 0;
59 bi->soft_del_pages = 0;
60
61 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
62
63 bi->block_state = state;
64 bi->seq_number = seq_number;
65
66 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
67 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
68
69 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
70 "Block scanning block %d state %d seq %d",
71 blk, state, seq_number);
72
73 if (state == YAFFS_BLOCK_STATE_DEAD) {
74 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
75 "block %d is bad", blk);
76 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
77 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
78 dev->n_erased_blocks++;
79 dev->n_free_chunks += dev->param.chunks_per_block;
80 }
81 bi++;
82 }
83
84 /* For each block.... */
85 for (blk = dev->internal_start_block;
86 !alloc_failed && blk <= dev->internal_end_block; blk++) {
87
88 cond_resched();
89
90 bi = yaffs_get_block_info(dev, blk);
91 state = bi->block_state;
92
93 deleted = 0;
94
95 /* For each chunk in each block that needs scanning.... */
96 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
97 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
98 /* Read the tags and decide what to do */
99 chunk = blk * dev->param.chunks_per_block + c;
100
101 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
102 &tags);
103
104 /* Let's have a good look at this chunk... */
105
106 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
107 || tags.is_deleted) {
108 /* YAFFS1 only...
109 * A deleted chunk
110 */
111 deleted++;
112 dev->n_free_chunks++;
113 /*T((" %d %d deleted\n",blk,c)); */
114 } else if (!tags.chunk_used) {
115 /* An unassigned chunk in the block
116 * This means that either the block is empty or
117 * this is the one being allocated from
118 */
119
120 if (c == 0) {
121 /* We're looking at the first chunk in the block so the block is unused */
122 state = YAFFS_BLOCK_STATE_EMPTY;
123 dev->n_erased_blocks++;
124 } else {
125 /* this is the block being allocated from */
126 yaffs_trace(YAFFS_TRACE_SCAN,
127 " Allocating from %d %d",
128 blk, c);
129 state = YAFFS_BLOCK_STATE_ALLOCATING;
130 dev->alloc_block = blk;
131 dev->alloc_page = c;
132 dev->alloc_block_finder = blk;
133 /* Set block finder here to encourage the allocator to go forth from here. */
134
135 }
136
137 dev->n_free_chunks +=
138 (dev->param.chunks_per_block - c);
139 } else if (tags.chunk_id > 0) {
140 /* chunk_id > 0 so it is a data chunk... */
141 unsigned int endpos;
142
143 yaffs_set_chunk_bit(dev, blk, c);
144 bi->pages_in_use++;
145
146 in = yaffs_find_or_create_by_number(dev,
147 tags.obj_id,
148 YAFFS_OBJECT_TYPE_FILE);
149 /* PutChunkIntoFile checks for a clash (two data chunks with
150 * the same chunk_id).
151 */
152
153 if (!in)
154 alloc_failed = 1;
155
156 if (in) {
157 if (!yaffs_put_chunk_in_file
158 (in, tags.chunk_id, chunk, 1))
159 alloc_failed = 1;
160 }
161
162 endpos =
163 (tags.chunk_id -
164 1) * dev->data_bytes_per_chunk +
165 tags.n_bytes;
166 if (in
167 && in->variant_type ==
168 YAFFS_OBJECT_TYPE_FILE
169 && in->variant.file_variant.scanned_size <
170 endpos) {
171 in->variant.file_variant.scanned_size =
172 endpos;
173 if (!dev->param.use_header_file_size) {
174 in->variant.
175 file_variant.file_size =
176 in->variant.
177 file_variant.scanned_size;
178 }
179
180 }
181 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
182 } else {
183 /* chunk_id == 0, so it is an ObjectHeader.
184 * Thus, we read in the object header and make the object
185 */
186 yaffs_set_chunk_bit(dev, blk, c);
187 bi->pages_in_use++;
188
189 result = yaffs_rd_chunk_tags_nand(dev, chunk,
190 chunk_data,
191 NULL);
192
193 oh = (struct yaffs_obj_hdr *)chunk_data;
194
195 in = yaffs_find_by_number(dev, tags.obj_id);
196 if (in && in->variant_type != oh->type) {
197 /* This should not happen, but somehow
198 * Wev'e ended up with an obj_id that has been reused but not yet
199 * deleted, and worse still it has changed type. Delete the old object.
200 */
201
202 yaffs_del_obj(in);
203
204 in = 0;
205 }
206
207 in = yaffs_find_or_create_by_number(dev,
208 tags.obj_id,
209 oh->type);
210
211 if (!in)
212 alloc_failed = 1;
213
214 if (in && oh->shadows_obj > 0) {
215
216 struct yaffs_shadow_fixer *fixer;
217 fixer =
218 kmalloc(sizeof
219 (struct yaffs_shadow_fixer),
220 GFP_NOFS);
221 if (fixer) {
222 fixer->next = shadow_fixers;
223 shadow_fixers = fixer;
224 fixer->obj_id = tags.obj_id;
225 fixer->shadowed_id =
226 oh->shadows_obj;
227 yaffs_trace(YAFFS_TRACE_SCAN,
228 " Shadow fixer: %d shadows %d",
229 fixer->obj_id,
230 fixer->shadowed_id);
231
232 }
233
234 }
235
236 if (in && in->valid) {
237 /* We have already filled this one. We have a duplicate and need to resolve it. */
238
239 unsigned existing_serial = in->serial;
240 unsigned new_serial =
241 tags.serial_number;
242
243 if (((existing_serial + 1) & 3) ==
244 new_serial) {
245 /* Use new one - destroy the exisiting one */
246 yaffs_chunk_del(dev,
247 in->hdr_chunk,
248 1, __LINE__);
249 in->valid = 0;
250 } else {
251 /* Use existing - destroy this one. */
252 yaffs_chunk_del(dev, chunk, 1,
253 __LINE__);
254 }
255 }
256
257 if (in && !in->valid &&
258 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
259 tags.obj_id ==
260 YAFFS_OBJECTID_LOSTNFOUND)) {
261 /* We only load some info, don't fiddle with directory structure */
262 in->valid = 1;
263 in->variant_type = oh->type;
264
265 in->yst_mode = oh->yst_mode;
266 yaffs_load_attribs(in, oh);
267 in->hdr_chunk = chunk;
268 in->serial = tags.serial_number;
269
270 } else if (in && !in->valid) {
271 /* we need to load this info */
272
273 in->valid = 1;
274 in->variant_type = oh->type;
275
276 in->yst_mode = oh->yst_mode;
277 yaffs_load_attribs(in, oh);
278 in->hdr_chunk = chunk;
279 in->serial = tags.serial_number;
280
281 yaffs_set_obj_name_from_oh(in, oh);
282 in->dirty = 0;
283
284 /* directory stuff...
285 * hook up to parent
286 */
287
288 parent =
289 yaffs_find_or_create_by_number
290 (dev, oh->parent_obj_id,
291 YAFFS_OBJECT_TYPE_DIRECTORY);
292 if (!parent)
293 alloc_failed = 1;
294 if (parent && parent->variant_type ==
295 YAFFS_OBJECT_TYPE_UNKNOWN) {
296 /* Set up as a directory */
297 parent->variant_type =
298 YAFFS_OBJECT_TYPE_DIRECTORY;
299 INIT_LIST_HEAD(&parent->
300 variant.dir_variant.children);
301 } else if (!parent
302 || parent->variant_type !=
303 YAFFS_OBJECT_TYPE_DIRECTORY) {
304 /* Hoosterman, another problem....
305 * We're trying to use a non-directory as a directory
306 */
307
308 yaffs_trace(YAFFS_TRACE_ERROR,
309 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
310 );
311 parent = dev->lost_n_found;
312 }
313
314 yaffs_add_obj_to_dir(parent, in);
315
316 if (0 && (parent == dev->del_dir ||
317 parent ==
318 dev->unlinked_dir)) {
319 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
320 dev->n_deleted_files++;
321 }
322 /* Note re hardlinks.
323 * Since we might scan a hardlink before its equivalent object is scanned
324 * we put them all in a list.
325 * After scanning is complete, we should have all the objects, so we run through this
326 * list and fix up all the chains.
327 */
328
329 switch (in->variant_type) {
330 case YAFFS_OBJECT_TYPE_UNKNOWN:
331 /* Todo got a problem */
332 break;
333 case YAFFS_OBJECT_TYPE_FILE:
334 if (dev->param.
335 use_header_file_size)
336
337 in->variant.
338 file_variant.file_size
339 = oh->file_size;
340
341 break;
342 case YAFFS_OBJECT_TYPE_HARDLINK:
343 in->variant.
344 hardlink_variant.equiv_id =
345 oh->equiv_id;
346 in->hard_links.next =
347 (struct list_head *)
348 hard_list;
349 hard_list = in;
350 break;
351 case YAFFS_OBJECT_TYPE_DIRECTORY:
352 /* Do nothing */
353 break;
354 case YAFFS_OBJECT_TYPE_SPECIAL:
355 /* Do nothing */
356 break;
357 case YAFFS_OBJECT_TYPE_SYMLINK:
358 in->variant.symlink_variant.
359 alias =
360 yaffs_clone_str(oh->alias);
361 if (!in->variant.
362 symlink_variant.alias)
363 alloc_failed = 1;
364 break;
365 }
366
367 }
368 }
369 }
370
371 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
372 /* If we got this far while scanning, then the block is fully allocated. */
373 state = YAFFS_BLOCK_STATE_FULL;
374 }
375
376 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
377 /* If the block was partially allocated then treat it as fully allocated. */
378 state = YAFFS_BLOCK_STATE_FULL;
379 dev->alloc_block = -1;
380 }
381
382 bi->block_state = state;
383
384 /* Now let's see if it was dirty */
385 if (bi->pages_in_use == 0 &&
386 !bi->has_shrink_hdr &&
387 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
388 yaffs_block_became_dirty(dev, blk);
389 }
390
391 }
392
393 /* Ok, we've done all the scanning.
394 * Fix up the hard link chains.
395 * We should now have scanned all the objects, now it's time to add these
396 * hardlinks.
397 */
398
399 yaffs_link_fixup(dev, hard_list);
400
401 /* Fix up any shadowed objects */
402 {
403 struct yaffs_shadow_fixer *fixer;
404 struct yaffs_obj *obj;
405
406 while (shadow_fixers) {
407 fixer = shadow_fixers;
408 shadow_fixers = fixer->next;
409 /* Complete the rename transaction by deleting the shadowed object
410 * then setting the object header to unshadowed.
411 */
412 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
413 if (obj)
414 yaffs_del_obj(obj);
415
416 obj = yaffs_find_by_number(dev, fixer->obj_id);
417
418 if (obj)
419 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
420
421 kfree(fixer);
422 }
423 }
424
425 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
426
427 if (alloc_failed)
428 return YAFFS_FAIL;
429
430 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
431
432 return YAFFS_OK;
433}
diff --git a/fs/yaffs2/yaffs_yaffs1.h b/fs/yaffs2/yaffs_yaffs1.h
new file mode 100644
index 00000000000..db23e04973b
--- /dev/null
+++ b/fs/yaffs2/yaffs_yaffs1.h
@@ -0,0 +1,22 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_YAFFS1_H__
17#define __YAFFS_YAFFS1_H__
18
19#include "yaffs_guts.h"
20int yaffs1_scan(struct yaffs_dev *dev);
21
22#endif
diff --git a/fs/yaffs2/yaffs_yaffs2.c b/fs/yaffs2/yaffs_yaffs2.c
new file mode 100644
index 00000000000..33397af7003
--- /dev/null
+++ b/fs/yaffs2/yaffs_yaffs2.c
@@ -0,0 +1,1598 @@
1/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_guts.h"
15#include "yaffs_trace.h"
16#include "yaffs_yaffs2.h"
17#include "yaffs_checkptrw.h"
18#include "yaffs_bitmap.h"
19#include "yaffs_nand.h"
20#include "yaffs_getblockinfo.h"
21#include "yaffs_verify.h"
22#include "yaffs_attribs.h"
23
24/*
25 * Checkpoints are really no benefit on very small partitions.
26 *
27 * To save space on small partitions don't bother with checkpoints unless
28 * the partition is at least this big.
29 */
30#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
31
32#define YAFFS_SMALL_HOLE_THRESHOLD 4
33
34/*
35 * Oldest Dirty Sequence Number handling.
36 */
37
38/* yaffs_calc_oldest_dirty_seq()
39 * yaffs2_find_oldest_dirty_seq()
40 * Calculate the oldest dirty sequence number if we don't know it.
41 */
42void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
43{
44 int i;
45 unsigned seq;
46 unsigned block_no = 0;
47 struct yaffs_block_info *b;
48
49 if (!dev->param.is_yaffs2)
50 return;
51
52 /* Find the oldest dirty sequence number. */
53 seq = dev->seq_number + 1;
54 b = dev->block_info;
55 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
56 if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
57 (b->pages_in_use - b->soft_del_pages) <
58 dev->param.chunks_per_block && b->seq_number < seq) {
59 seq = b->seq_number;
60 block_no = i;
61 }
62 b++;
63 }
64
65 if (block_no) {
66 dev->oldest_dirty_seq = seq;
67 dev->oldest_dirty_block = block_no;
68 }
69
70}
71
72void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
73{
74 if (!dev->param.is_yaffs2)
75 return;
76
77 if (!dev->oldest_dirty_seq)
78 yaffs_calc_oldest_dirty_seq(dev);
79}
80
81/*
82 * yaffs_clear_oldest_dirty_seq()
83 * Called when a block is erased or marked bad. (ie. when its seq_number
84 * becomes invalid). If the value matches the oldest then we clear
85 * dev->oldest_dirty_seq to force its recomputation.
86 */
87void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
88 struct yaffs_block_info *bi)
89{
90
91 if (!dev->param.is_yaffs2)
92 return;
93
94 if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
95 dev->oldest_dirty_seq = 0;
96 dev->oldest_dirty_block = 0;
97 }
98}
99
100/*
101 * yaffs2_update_oldest_dirty_seq()
102 * Update the oldest dirty sequence number whenever we dirty a block.
103 * Only do this if the oldest_dirty_seq is actually being tracked.
104 */
105void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
106 struct yaffs_block_info *bi)
107{
108 if (!dev->param.is_yaffs2)
109 return;
110
111 if (dev->oldest_dirty_seq) {
112 if (dev->oldest_dirty_seq > bi->seq_number) {
113 dev->oldest_dirty_seq = bi->seq_number;
114 dev->oldest_dirty_block = block_no;
115 }
116 }
117}
118
119int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
120{
121
122 if (!dev->param.is_yaffs2)
123 return 1; /* disqualification only applies to yaffs2. */
124
125 if (!bi->has_shrink_hdr)
126 return 1; /* can gc */
127
128 yaffs2_find_oldest_dirty_seq(dev);
129
130 /* Can't do gc of this block if there are any blocks older than this one that have
131 * discarded pages.
132 */
133 return (bi->seq_number <= dev->oldest_dirty_seq);
134}
135
136/*
137 * yaffs2_find_refresh_block()
138 * periodically finds the oldest full block by sequence number for refreshing.
139 * Only for yaffs2.
140 */
141u32 yaffs2_find_refresh_block(struct yaffs_dev * dev)
142{
143 u32 b;
144
145 u32 oldest = 0;
146 u32 oldest_seq = 0;
147
148 struct yaffs_block_info *bi;
149
150 if (!dev->param.is_yaffs2)
151 return oldest;
152
153 /*
154 * If refresh period < 10 then refreshing is disabled.
155 */
156 if (dev->param.refresh_period < 10)
157 return oldest;
158
159 /*
160 * Fix broken values.
161 */
162 if (dev->refresh_skip > dev->param.refresh_period)
163 dev->refresh_skip = dev->param.refresh_period;
164
165 if (dev->refresh_skip > 0)
166 return oldest;
167
168 /*
169 * Refresh skip is now zero.
170 * We'll do a refresh this time around....
171 * Update the refresh skip and find the oldest block.
172 */
173 dev->refresh_skip = dev->param.refresh_period;
174 dev->refresh_count++;
175 bi = dev->block_info;
176 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
177
178 if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
179
180 if (oldest < 1 || bi->seq_number < oldest_seq) {
181 oldest = b;
182 oldest_seq = bi->seq_number;
183 }
184 }
185 bi++;
186 }
187
188 if (oldest > 0) {
189 yaffs_trace(YAFFS_TRACE_GC,
190 "GC refresh count %d selected block %d with seq_number %d",
191 dev->refresh_count, oldest, oldest_seq);
192 }
193
194 return oldest;
195}
196
197int yaffs2_checkpt_required(struct yaffs_dev *dev)
198{
199 int nblocks;
200
201 if (!dev->param.is_yaffs2)
202 return 0;
203
204 nblocks = dev->internal_end_block - dev->internal_start_block + 1;
205
206 return !dev->param.skip_checkpt_wr &&
207 !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
208}
209
210int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
211{
212 int retval;
213
214 if (!dev->param.is_yaffs2)
215 return 0;
216
217 if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
218 /* Not a valid value so recalculate */
219 int n_bytes = 0;
220 int n_blocks;
221 int dev_blocks =
222 (dev->param.end_block - dev->param.start_block + 1);
223
224 n_bytes += sizeof(struct yaffs_checkpt_validity);
225 n_bytes += sizeof(struct yaffs_checkpt_dev);
226 n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
227 n_bytes += dev_blocks * dev->chunk_bit_stride;
228 n_bytes +=
229 (sizeof(struct yaffs_checkpt_obj) +
230 sizeof(u32)) * (dev->n_obj);
231 n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes);
232 n_bytes += sizeof(struct yaffs_checkpt_validity);
233 n_bytes += sizeof(u32); /* checksum */
234
235 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
236
237 n_blocks =
238 (n_bytes /
239 (dev->data_bytes_per_chunk *
240 dev->param.chunks_per_block)) + 3;
241
242 dev->checkpoint_blocks_required = n_blocks;
243 }
244
245 retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
246 if (retval < 0)
247 retval = 0;
248 return retval;
249}
250
251/*--------------------- Checkpointing --------------------*/
252
253static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
254{
255 struct yaffs_checkpt_validity cp;
256
257 memset(&cp, 0, sizeof(cp));
258
259 cp.struct_type = sizeof(cp);
260 cp.magic = YAFFS_MAGIC;
261 cp.version = YAFFS_CHECKPOINT_VERSION;
262 cp.head = (head) ? 1 : 0;
263
264 return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
265}
266
267static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
268{
269 struct yaffs_checkpt_validity cp;
270 int ok;
271
272 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
273
274 if (ok)
275 ok = (cp.struct_type == sizeof(cp)) &&
276 (cp.magic == YAFFS_MAGIC) &&
277 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
278 (cp.head == ((head) ? 1 : 0));
279 return ok ? 1 : 0;
280}
281
282static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
283 struct yaffs_dev *dev)
284{
285 cp->n_erased_blocks = dev->n_erased_blocks;
286 cp->alloc_block = dev->alloc_block;
287 cp->alloc_page = dev->alloc_page;
288 cp->n_free_chunks = dev->n_free_chunks;
289
290 cp->n_deleted_files = dev->n_deleted_files;
291 cp->n_unlinked_files = dev->n_unlinked_files;
292 cp->n_bg_deletions = dev->n_bg_deletions;
293 cp->seq_number = dev->seq_number;
294
295}
296
297static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
298 struct yaffs_checkpt_dev *cp)
299{
300 dev->n_erased_blocks = cp->n_erased_blocks;
301 dev->alloc_block = cp->alloc_block;
302 dev->alloc_page = cp->alloc_page;
303 dev->n_free_chunks = cp->n_free_chunks;
304
305 dev->n_deleted_files = cp->n_deleted_files;
306 dev->n_unlinked_files = cp->n_unlinked_files;
307 dev->n_bg_deletions = cp->n_bg_deletions;
308 dev->seq_number = cp->seq_number;
309}
310
311static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
312{
313 struct yaffs_checkpt_dev cp;
314 u32 n_bytes;
315 u32 n_blocks =
316 (dev->internal_end_block - dev->internal_start_block + 1);
317
318 int ok;
319
320 /* Write device runtime values */
321 yaffs2_dev_to_checkpt_dev(&cp, dev);
322 cp.struct_type = sizeof(cp);
323
324 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
325
326 /* Write block info */
327 if (ok) {
328 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
329 ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
330 n_bytes);
331 }
332
333 /* Write chunk bits */
334 if (ok) {
335 n_bytes = n_blocks * dev->chunk_bit_stride;
336 ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
337 n_bytes);
338 }
339 return ok ? 1 : 0;
340
341}
342
343static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
344{
345 struct yaffs_checkpt_dev cp;
346 u32 n_bytes;
347 u32 n_blocks =
348 (dev->internal_end_block - dev->internal_start_block + 1);
349
350 int ok;
351
352 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
353 if (!ok)
354 return 0;
355
356 if (cp.struct_type != sizeof(cp))
357 return 0;
358
359 yaffs_checkpt_dev_to_dev(dev, &cp);
360
361 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
362
363 ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
364
365 if (!ok)
366 return 0;
367 n_bytes = n_blocks * dev->chunk_bit_stride;
368
369 ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
370
371 return ok ? 1 : 0;
372}
373
374static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
375 struct yaffs_obj *obj)
376{
377
378 cp->obj_id = obj->obj_id;
379 cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
380 cp->hdr_chunk = obj->hdr_chunk;
381 cp->variant_type = obj->variant_type;
382 cp->deleted = obj->deleted;
383 cp->soft_del = obj->soft_del;
384 cp->unlinked = obj->unlinked;
385 cp->fake = obj->fake;
386 cp->rename_allowed = obj->rename_allowed;
387 cp->unlink_allowed = obj->unlink_allowed;
388 cp->serial = obj->serial;
389 cp->n_data_chunks = obj->n_data_chunks;
390
391 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
392 cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
393 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
394 cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
395}
396
397static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
398 struct yaffs_checkpt_obj *cp)
399{
400
401 struct yaffs_obj *parent;
402
403 if (obj->variant_type != cp->variant_type) {
404 yaffs_trace(YAFFS_TRACE_ERROR,
405 "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
406 cp->obj_id, cp->variant_type, cp->hdr_chunk,
407 obj->variant_type);
408 return 0;
409 }
410
411 obj->obj_id = cp->obj_id;
412
413 if (cp->parent_id)
414 parent = yaffs_find_or_create_by_number(obj->my_dev,
415 cp->parent_id,
416 YAFFS_OBJECT_TYPE_DIRECTORY);
417 else
418 parent = NULL;
419
420 if (parent) {
421 if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
422 yaffs_trace(YAFFS_TRACE_ALWAYS,
423 "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
424 cp->obj_id, cp->parent_id,
425 cp->variant_type, cp->hdr_chunk,
426 parent->variant_type);
427 return 0;
428 }
429 yaffs_add_obj_to_dir(parent, obj);
430 }
431
432 obj->hdr_chunk = cp->hdr_chunk;
433 obj->variant_type = cp->variant_type;
434 obj->deleted = cp->deleted;
435 obj->soft_del = cp->soft_del;
436 obj->unlinked = cp->unlinked;
437 obj->fake = cp->fake;
438 obj->rename_allowed = cp->rename_allowed;
439 obj->unlink_allowed = cp->unlink_allowed;
440 obj->serial = cp->serial;
441 obj->n_data_chunks = cp->n_data_chunks;
442
443 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
444 obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
445 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
446 obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
447
448 if (obj->hdr_chunk > 0)
449 obj->lazy_loaded = 1;
450 return 1;
451}
452
453static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
454 struct yaffs_tnode *tn, u32 level,
455 int chunk_offset)
456{
457 int i;
458 struct yaffs_dev *dev = in->my_dev;
459 int ok = 1;
460
461 if (tn) {
462 if (level > 0) {
463
464 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
465 if (tn->internal[i]) {
466 ok = yaffs2_checkpt_tnode_worker(in,
467 tn->
468 internal
469 [i],
470 level -
471 1,
472 (chunk_offset
473 <<
474 YAFFS_TNODES_INTERNAL_BITS)
475 + i);
476 }
477 }
478 } else if (level == 0) {
479 u32 base_offset =
480 chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
481 ok = (yaffs2_checkpt_wr
482 (dev, &base_offset,
483 sizeof(base_offset)) == sizeof(base_offset));
484 if (ok)
485 ok = (yaffs2_checkpt_wr
486 (dev, tn,
487 dev->tnode_size) == dev->tnode_size);
488 }
489 }
490
491 return ok;
492
493}
494
495static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
496{
497 u32 end_marker = ~0;
498 int ok = 1;
499
500 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
501 ok = yaffs2_checkpt_tnode_worker(obj,
502 obj->variant.file_variant.top,
503 obj->variant.file_variant.
504 top_level, 0);
505 if (ok)
506 ok = (yaffs2_checkpt_wr
507 (obj->my_dev, &end_marker,
508 sizeof(end_marker)) == sizeof(end_marker));
509 }
510
511 return ok ? 1 : 0;
512}
513
514static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
515{
516 u32 base_chunk;
517 int ok = 1;
518 struct yaffs_dev *dev = obj->my_dev;
519 struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
520 struct yaffs_tnode *tn;
521 int nread = 0;
522
523 ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
524 sizeof(base_chunk));
525
526 while (ok && (~base_chunk)) {
527 nread++;
528 /* Read level 0 tnode */
529
530 tn = yaffs_get_tnode(dev);
531 if (tn) {
532 ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
533 dev->tnode_size);
534 } else {
535 ok = 0;
536 }
537
538 if (tn && ok)
539 ok = yaffs_add_find_tnode_0(dev,
540 file_stuct_ptr,
541 base_chunk, tn) ? 1 : 0;
542
543 if (ok)
544 ok = (yaffs2_checkpt_rd
545 (dev, &base_chunk,
546 sizeof(base_chunk)) == sizeof(base_chunk));
547
548 }
549
550 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
551 "Checkpoint read tnodes %d records, last %d. ok %d",
552 nread, base_chunk, ok);
553
554 return ok ? 1 : 0;
555}
556
557static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
558{
559 struct yaffs_obj *obj;
560 struct yaffs_checkpt_obj cp;
561 int i;
562 int ok = 1;
563 struct list_head *lh;
564
565 /* Iterate through the objects in each hash entry,
566 * dumping them to the checkpointing stream.
567 */
568
569 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
570 list_for_each(lh, &dev->obj_bucket[i].list) {
571 if (lh) {
572 obj =
573 list_entry(lh, struct yaffs_obj, hash_link);
574 if (!obj->defered_free) {
575 yaffs2_obj_checkpt_obj(&cp, obj);
576 cp.struct_type = sizeof(cp);
577
578 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
579 "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
580 cp.obj_id, cp.parent_id,
581 cp.variant_type, cp.hdr_chunk, obj);
582
583 ok = (yaffs2_checkpt_wr
584 (dev, &cp,
585 sizeof(cp)) == sizeof(cp));
586
587 if (ok
588 && obj->variant_type ==
589 YAFFS_OBJECT_TYPE_FILE)
590 ok = yaffs2_wr_checkpt_tnodes
591 (obj);
592 }
593 }
594 }
595 }
596
597 /* Dump end of list */
598 memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
599 cp.struct_type = sizeof(cp);
600
601 if (ok)
602 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
603
604 return ok ? 1 : 0;
605}
606
607static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
608{
609 struct yaffs_obj *obj;
610 struct yaffs_checkpt_obj cp;
611 int ok = 1;
612 int done = 0;
613 struct yaffs_obj *hard_list = NULL;
614
615 while (ok && !done) {
616 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
617 if (cp.struct_type != sizeof(cp)) {
618 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
619 "struct size %d instead of %d ok %d",
620 cp.struct_type, (int)sizeof(cp), ok);
621 ok = 0;
622 }
623
624 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
625 "Checkpoint read object %d parent %d type %d chunk %d ",
626 cp.obj_id, cp.parent_id, cp.variant_type,
627 cp.hdr_chunk);
628
629 if (ok && cp.obj_id == ~0) {
630 done = 1;
631 } else if (ok) {
632 obj =
633 yaffs_find_or_create_by_number(dev, cp.obj_id,
634 cp.variant_type);
635 if (obj) {
636 ok = taffs2_checkpt_obj_to_obj(obj, &cp);
637 if (!ok)
638 break;
639 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
640 ok = yaffs2_rd_checkpt_tnodes(obj);
641 } else if (obj->variant_type ==
642 YAFFS_OBJECT_TYPE_HARDLINK) {
643 obj->hard_links.next =
644 (struct list_head *)hard_list;
645 hard_list = obj;
646 }
647 } else {
648 ok = 0;
649 }
650 }
651 }
652
653 if (ok)
654 yaffs_link_fixup(dev, hard_list);
655
656 return ok ? 1 : 0;
657}
658
659static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
660{
661 u32 checkpt_sum;
662 int ok;
663
664 yaffs2_get_checkpt_sum(dev, &checkpt_sum);
665
666 ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
667 sizeof(checkpt_sum));
668
669 if (!ok)
670 return 0;
671
672 return 1;
673}
674
675static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
676{
677 u32 checkpt_sum0;
678 u32 checkpt_sum1;
679 int ok;
680
681 yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
682
683 ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
684 sizeof(checkpt_sum1));
685
686 if (!ok)
687 return 0;
688
689 if (checkpt_sum0 != checkpt_sum1)
690 return 0;
691
692 return 1;
693}
694
695static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
696{
697 int ok = 1;
698
699 if (!yaffs2_checkpt_required(dev)) {
700 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
701 "skipping checkpoint write");
702 ok = 0;
703 }
704
705 if (ok)
706 ok = yaffs2_checkpt_open(dev, 1);
707
708 if (ok) {
709 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
710 "write checkpoint validity");
711 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
712 }
713 if (ok) {
714 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
715 "write checkpoint device");
716 ok = yaffs2_wr_checkpt_dev(dev);
717 }
718 if (ok) {
719 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
720 "write checkpoint objects");
721 ok = yaffs2_wr_checkpt_objs(dev);
722 }
723 if (ok) {
724 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
725 "write checkpoint validity");
726 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
727 }
728
729 if (ok)
730 ok = yaffs2_wr_checkpt_sum(dev);
731
732 if (!yaffs_checkpt_close(dev))
733 ok = 0;
734
735 if (ok)
736 dev->is_checkpointed = 1;
737 else
738 dev->is_checkpointed = 0;
739
740 return dev->is_checkpointed;
741}
742
743static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
744{
745 int ok = 1;
746
747 if (!dev->param.is_yaffs2)
748 ok = 0;
749
750 if (ok && dev->param.skip_checkpt_rd) {
751 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
752 "skipping checkpoint read");
753 ok = 0;
754 }
755
756 if (ok)
757 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
758
759 if (ok) {
760 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
761 "read checkpoint validity");
762 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
763 }
764 if (ok) {
765 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
766 "read checkpoint device");
767 ok = yaffs2_rd_checkpt_dev(dev);
768 }
769 if (ok) {
770 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
771 "read checkpoint objects");
772 ok = yaffs2_rd_checkpt_objs(dev);
773 }
774 if (ok) {
775 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
776 "read checkpoint validity");
777 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
778 }
779
780 if (ok) {
781 ok = yaffs2_rd_checkpt_sum(dev);
782 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
783 "read checkpoint checksum %d", ok);
784 }
785
786 if (!yaffs_checkpt_close(dev))
787 ok = 0;
788
789 if (ok)
790 dev->is_checkpointed = 1;
791 else
792 dev->is_checkpointed = 0;
793
794 return ok ? 1 : 0;
795
796}
797
798void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
799{
800 if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
801 dev->is_checkpointed = 0;
802 yaffs2_checkpt_invalidate_stream(dev);
803 }
804 if (dev->param.sb_dirty_fn)
805 dev->param.sb_dirty_fn(dev);
806}
807
808int yaffs_checkpoint_save(struct yaffs_dev *dev)
809{
810
811 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
812 "save entry: is_checkpointed %d",
813 dev->is_checkpointed);
814
815 yaffs_verify_objects(dev);
816 yaffs_verify_blocks(dev);
817 yaffs_verify_free_chunks(dev);
818
819 if (!dev->is_checkpointed) {
820 yaffs2_checkpt_invalidate(dev);
821 yaffs2_wr_checkpt_data(dev);
822 }
823
824 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
825 "save exit: is_checkpointed %d",
826 dev->is_checkpointed);
827
828 return dev->is_checkpointed;
829}
830
831int yaffs2_checkpt_restore(struct yaffs_dev *dev)
832{
833 int retval;
834 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
835 "restore entry: is_checkpointed %d",
836 dev->is_checkpointed);
837
838 retval = yaffs2_rd_checkpt_data(dev);
839
840 if (dev->is_checkpointed) {
841 yaffs_verify_objects(dev);
842 yaffs_verify_blocks(dev);
843 yaffs_verify_free_chunks(dev);
844 }
845
846 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
847 "restore exit: is_checkpointed %d",
848 dev->is_checkpointed);
849
850 return retval;
851}
852
853int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
854{
855 /* if new_size > old_file_size.
856 * We're going to be writing a hole.
857 * If the hole is small then write zeros otherwise write a start of hole marker.
858 */
859
860 loff_t old_file_size;
861 int increase;
862 int small_hole;
863 int result = YAFFS_OK;
864 struct yaffs_dev *dev = NULL;
865
866 u8 *local_buffer = NULL;
867
868 int small_increase_ok = 0;
869
870 if (!obj)
871 return YAFFS_FAIL;
872
873 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
874 return YAFFS_FAIL;
875
876 dev = obj->my_dev;
877
878 /* Bail out if not yaffs2 mode */
879 if (!dev->param.is_yaffs2)
880 return YAFFS_OK;
881
882 old_file_size = obj->variant.file_variant.file_size;
883
884 if (new_size <= old_file_size)
885 return YAFFS_OK;
886
887 increase = new_size - old_file_size;
888
889 if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
890 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
891 small_hole = 1;
892 else
893 small_hole = 0;
894
895 if (small_hole)
896 local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
897
898 if (local_buffer) {
899 /* fill hole with zero bytes */
900 int pos = old_file_size;
901 int this_write;
902 int written;
903 memset(local_buffer, 0, dev->data_bytes_per_chunk);
904 small_increase_ok = 1;
905
906 while (increase > 0 && small_increase_ok) {
907 this_write = increase;
908 if (this_write > dev->data_bytes_per_chunk)
909 this_write = dev->data_bytes_per_chunk;
910 written =
911 yaffs_do_file_wr(obj, local_buffer, pos, this_write,
912 0);
913 if (written == this_write) {
914 pos += this_write;
915 increase -= this_write;
916 } else {
917 small_increase_ok = 0;
918 }
919 }
920
921 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
922
923 /* If we were out of space then reverse any chunks we've added */
924 if (!small_increase_ok)
925 yaffs_resize_file_down(obj, old_file_size);
926 }
927
928 if (!small_increase_ok &&
929 obj->parent &&
930 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
931 obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
932 /* Write a hole start header with the old file size */
933 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
934 }
935
936 return result;
937
938}
939
940struct yaffs_block_index {
941 int seq;
942 int block;
943};
944
945static int yaffs2_ybicmp(const void *a, const void *b)
946{
947 int aseq = ((struct yaffs_block_index *)a)->seq;
948 int bseq = ((struct yaffs_block_index *)b)->seq;
949 int ablock = ((struct yaffs_block_index *)a)->block;
950 int bblock = ((struct yaffs_block_index *)b)->block;
951 if (aseq == bseq)
952 return ablock - bblock;
953 else
954 return aseq - bseq;
955}
956
957int yaffs2_scan_backwards(struct yaffs_dev *dev)
958{
959 struct yaffs_ext_tags tags;
960 int blk;
961 int block_iter;
962 int start_iter;
963 int end_iter;
964 int n_to_scan = 0;
965
966 int chunk;
967 int result;
968 int c;
969 int deleted;
970 enum yaffs_block_state state;
971 struct yaffs_obj *hard_list = NULL;
972 struct yaffs_block_info *bi;
973 u32 seq_number;
974 struct yaffs_obj_hdr *oh;
975 struct yaffs_obj *in;
976 struct yaffs_obj *parent;
977 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
978 int is_unlinked;
979 u8 *chunk_data;
980
981 int file_size;
982 int is_shrink;
983 int found_chunks;
984 int equiv_id;
985 int alloc_failed = 0;
986
987 struct yaffs_block_index *block_index = NULL;
988 int alt_block_index = 0;
989
990 yaffs_trace(YAFFS_TRACE_SCAN,
991 "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
992 dev->internal_start_block, dev->internal_end_block);
993
994 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
995
996 block_index = kmalloc(n_blocks * sizeof(struct yaffs_block_index),
997 GFP_NOFS);
998
999 if (!block_index) {
1000 block_index =
1001 vmalloc(n_blocks * sizeof(struct yaffs_block_index));
1002 alt_block_index = 1;
1003 }
1004
1005 if (!block_index) {
1006 yaffs_trace(YAFFS_TRACE_SCAN,
1007 "yaffs2_scan_backwards() could not allocate block index!"
1008 );
1009 return YAFFS_FAIL;
1010 }
1011
1012 dev->blocks_in_checkpt = 0;
1013
1014 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
1015
1016 /* Scan all the blocks to determine their state */
1017 bi = dev->block_info;
1018 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1019 blk++) {
1020 yaffs_clear_chunk_bits(dev, blk);
1021 bi->pages_in_use = 0;
1022 bi->soft_del_pages = 0;
1023
1024 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1025
1026 bi->block_state = state;
1027 bi->seq_number = seq_number;
1028
1029 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1030 bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
1031 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1032 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
1033
1034 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
1035 "Block scanning block %d state %d seq %d",
1036 blk, state, seq_number);
1037
1038 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1039 dev->blocks_in_checkpt++;
1040
1041 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
1042 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
1043 "block %d is bad", blk);
1044 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
1045 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
1046 dev->n_erased_blocks++;
1047 dev->n_free_chunks += dev->param.chunks_per_block;
1048 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1049
1050 /* Determine the highest sequence number */
1051 if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1052 seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1053
1054 block_index[n_to_scan].seq = seq_number;
1055 block_index[n_to_scan].block = blk;
1056
1057 n_to_scan++;
1058
1059 if (seq_number >= dev->seq_number)
1060 dev->seq_number = seq_number;
1061 } else {
1062 /* TODO: Nasty sequence number! */
1063 yaffs_trace(YAFFS_TRACE_SCAN,
1064 "Block scanning block %d has bad sequence number %d",
1065 blk, seq_number);
1066
1067 }
1068 }
1069 bi++;
1070 }
1071
1072 yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan);
1073
1074 cond_resched();
1075
1076 /* Sort the blocks by sequence number */
1077 sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1078 yaffs2_ybicmp, NULL);
1079
1080 cond_resched();
1081
1082 yaffs_trace(YAFFS_TRACE_SCAN, "...done");
1083
1084 /* Now scan the blocks looking at the data. */
1085 start_iter = 0;
1086 end_iter = n_to_scan - 1;
1087 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
1088
1089 /* For each block.... backwards */
1090 for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
1091 block_iter--) {
1092 /* Cooperative multitasking! This loop can run for so
1093 long that watchdog timers expire. */
1094 cond_resched();
1095
1096 /* get the block to scan in the correct order */
1097 blk = block_index[block_iter].block;
1098
1099 bi = yaffs_get_block_info(dev, blk);
1100
1101 state = bi->block_state;
1102
1103 deleted = 0;
1104
1105 /* For each chunk in each block that needs scanning.... */
1106 found_chunks = 0;
1107 for (c = dev->param.chunks_per_block - 1;
1108 !alloc_failed && c >= 0 &&
1109 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1110 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
1111 /* Scan backwards...
1112 * Read the tags and decide what to do
1113 */
1114
1115 chunk = blk * dev->param.chunks_per_block + c;
1116
1117 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
1118 &tags);
1119
1120 /* Let's have a good look at this chunk... */
1121
1122 if (!tags.chunk_used) {
1123 /* An unassigned chunk in the block.
1124 * If there are used chunks after this one, then
1125 * it is a chunk that was skipped due to failing the erased
1126 * check. Just skip it so that it can be deleted.
1127 * But, more typically, We get here when this is an unallocated
1128 * chunk and his means that either the block is empty or
1129 * this is the one being allocated from
1130 */
1131
1132 if (found_chunks) {
1133 /* This is a chunk that was skipped due to failing the erased check */
1134 } else if (c == 0) {
1135 /* We're looking at the first chunk in the block so the block is unused */
1136 state = YAFFS_BLOCK_STATE_EMPTY;
1137 dev->n_erased_blocks++;
1138 } else {
1139 if (state ==
1140 YAFFS_BLOCK_STATE_NEEDS_SCANNING
1141 || state ==
1142 YAFFS_BLOCK_STATE_ALLOCATING) {
1143 if (dev->seq_number ==
1144 bi->seq_number) {
1145 /* this is the block being allocated from */
1146
1147 yaffs_trace(YAFFS_TRACE_SCAN,
1148 " Allocating from %d %d",
1149 blk, c);
1150
1151 state =
1152 YAFFS_BLOCK_STATE_ALLOCATING;
1153 dev->alloc_block = blk;
1154 dev->alloc_page = c;
1155 dev->
1156 alloc_block_finder =
1157 blk;
1158 } else {
1159 /* This is a partially written block that is not
1160 * the current allocation block.
1161 */
1162
1163 yaffs_trace(YAFFS_TRACE_SCAN,
1164 "Partially written block %d detected",
1165 blk);
1166 }
1167 }
1168 }
1169
1170 dev->n_free_chunks++;
1171
1172 } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
1173 yaffs_trace(YAFFS_TRACE_SCAN,
1174 " Unfixed ECC in chunk(%d:%d), chunk ignored",
1175 blk, c);
1176
1177 dev->n_free_chunks++;
1178
1179 } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1180 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1181 (tags.chunk_id > 0
1182 && tags.n_bytes > dev->data_bytes_per_chunk)
1183 || tags.seq_number != bi->seq_number) {
1184 yaffs_trace(YAFFS_TRACE_SCAN,
1185 "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
1186 blk, c, tags.obj_id,
1187 tags.chunk_id, tags.n_bytes);
1188
1189 dev->n_free_chunks++;
1190
1191 } else if (tags.chunk_id > 0) {
1192 /* chunk_id > 0 so it is a data chunk... */
1193 unsigned int endpos;
1194 u32 chunk_base =
1195 (tags.chunk_id -
1196 1) * dev->data_bytes_per_chunk;
1197
1198 found_chunks = 1;
1199
1200 yaffs_set_chunk_bit(dev, blk, c);
1201 bi->pages_in_use++;
1202
1203 in = yaffs_find_or_create_by_number(dev,
1204 tags.obj_id,
1205 YAFFS_OBJECT_TYPE_FILE);
1206 if (!in) {
1207 /* Out of memory */
1208 alloc_failed = 1;
1209 }
1210
1211 if (in &&
1212 in->variant_type == YAFFS_OBJECT_TYPE_FILE
1213 && chunk_base <
1214 in->variant.file_variant.shrink_size) {
1215 /* This has not been invalidated by a resize */
1216 if (!yaffs_put_chunk_in_file
1217 (in, tags.chunk_id, chunk, -1)) {
1218 alloc_failed = 1;
1219 }
1220
1221 /* File size is calculated by looking at the data chunks if we have not
1222 * seen an object header yet. Stop this practice once we find an object header.
1223 */
1224 endpos = chunk_base + tags.n_bytes;
1225
1226 if (!in->valid && /* have not got an object header yet */
1227 in->variant.file_variant.
1228 scanned_size < endpos) {
1229 in->variant.file_variant.
1230 scanned_size = endpos;
1231 in->variant.file_variant.
1232 file_size = endpos;
1233 }
1234
1235 } else if (in) {
1236 /* This chunk has been invalidated by a resize, or a past file deletion
1237 * so delete the chunk*/
1238 yaffs_chunk_del(dev, chunk, 1,
1239 __LINE__);
1240
1241 }
1242 } else {
1243 /* chunk_id == 0, so it is an ObjectHeader.
1244 * Thus, we read in the object header and make the object
1245 */
1246 found_chunks = 1;
1247
1248 yaffs_set_chunk_bit(dev, blk, c);
1249 bi->pages_in_use++;
1250
1251 oh = NULL;
1252 in = NULL;
1253
1254 if (tags.extra_available) {
1255 in = yaffs_find_or_create_by_number(dev,
1256 tags.
1257 obj_id,
1258 tags.
1259 extra_obj_type);
1260 if (!in)
1261 alloc_failed = 1;
1262 }
1263
1264 if (!in ||
1265 (!in->valid && dev->param.disable_lazy_load)
1266 || tags.extra_shadows || (!in->valid
1267 && (tags.obj_id ==
1268 YAFFS_OBJECTID_ROOT
1269 || tags.
1270 obj_id ==
1271 YAFFS_OBJECTID_LOSTNFOUND)))
1272 {
1273
1274 /* If we don't have valid info then we need to read the chunk
1275 * TODO In future we can probably defer reading the chunk and
1276 * living with invalid data until needed.
1277 */
1278
1279 result = yaffs_rd_chunk_tags_nand(dev,
1280 chunk,
1281 chunk_data,
1282 NULL);
1283
1284 oh = (struct yaffs_obj_hdr *)chunk_data;
1285
1286 if (dev->param.inband_tags) {
1287 /* Fix up the header if they got corrupted by inband tags */
1288 oh->shadows_obj =
1289 oh->inband_shadowed_obj_id;
1290 oh->is_shrink =
1291 oh->inband_is_shrink;
1292 }
1293
1294 if (!in) {
1295 in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
1296 if (!in)
1297 alloc_failed = 1;
1298 }
1299
1300 }
1301
1302 if (!in) {
1303 /* TODO Hoosterman we have a problem! */
1304 yaffs_trace(YAFFS_TRACE_ERROR,
1305 "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
1306 tags.obj_id, chunk);
1307 continue;
1308 }
1309
1310 if (in->valid) {
1311 /* We have already filled this one.
1312 * We have a duplicate that will be discarded, but
1313 * we first have to suck out resize info if it is a file.
1314 */
1315
1316 if ((in->variant_type ==
1317 YAFFS_OBJECT_TYPE_FILE) && ((oh
1318 &&
1319 oh->
1320 type
1321 ==
1322 YAFFS_OBJECT_TYPE_FILE)
1323 ||
1324 (tags.
1325 extra_available
1326 &&
1327 tags.
1328 extra_obj_type
1329 ==
1330 YAFFS_OBJECT_TYPE_FILE)))
1331 {
1332 u32 this_size =
1333 (oh) ? oh->
1334 file_size :
1335 tags.extra_length;
1336 u32 parent_obj_id =
1337 (oh) ? oh->parent_obj_id :
1338 tags.extra_parent_id;
1339
1340 is_shrink =
1341 (oh) ? oh->
1342 is_shrink :
1343 tags.extra_is_shrink;
1344
1345 /* If it is deleted (unlinked at start also means deleted)
1346 * we treat the file size as being zeroed at this point.
1347 */
1348 if (parent_obj_id ==
1349 YAFFS_OBJECTID_DELETED
1350 || parent_obj_id ==
1351 YAFFS_OBJECTID_UNLINKED) {
1352 this_size = 0;
1353 is_shrink = 1;
1354 }
1355
1356 if (is_shrink
1357 && in->variant.file_variant.
1358 shrink_size > this_size)
1359 in->variant.
1360 file_variant.
1361 shrink_size =
1362 this_size;
1363
1364 if (is_shrink)
1365 bi->has_shrink_hdr = 1;
1366
1367 }
1368 /* Use existing - destroy this one. */
1369 yaffs_chunk_del(dev, chunk, 1,
1370 __LINE__);
1371
1372 }
1373
1374 if (!in->valid && in->variant_type !=
1375 (oh ? oh->type : tags.extra_obj_type))
1376 yaffs_trace(YAFFS_TRACE_ERROR,
1377 "yaffs tragedy: Bad object type, %d != %d, for object %d at chunk %d during scan",
1378 oh ?
1379 oh->type : tags.extra_obj_type,
1380 in->variant_type, tags.obj_id,
1381 chunk);
1382
1383 if (!in->valid &&
1384 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1385 tags.obj_id ==
1386 YAFFS_OBJECTID_LOSTNFOUND)) {
1387 /* We only load some info, don't fiddle with directory structure */
1388 in->valid = 1;
1389
1390 if (oh) {
1391
1392 in->yst_mode = oh->yst_mode;
1393 yaffs_load_attribs(in, oh);
1394 in->lazy_loaded = 0;
1395 } else {
1396 in->lazy_loaded = 1;
1397 }
1398 in->hdr_chunk = chunk;
1399
1400 } else if (!in->valid) {
1401 /* we need to load this info */
1402
1403 in->valid = 1;
1404 in->hdr_chunk = chunk;
1405
1406 if (oh) {
1407 in->variant_type = oh->type;
1408
1409 in->yst_mode = oh->yst_mode;
1410 yaffs_load_attribs(in, oh);
1411
1412 if (oh->shadows_obj > 0)
1413 yaffs_handle_shadowed_obj
1414 (dev,
1415 oh->shadows_obj,
1416 1);
1417
1418 yaffs_set_obj_name_from_oh(in,
1419 oh);
1420 parent =
1421 yaffs_find_or_create_by_number
1422 (dev, oh->parent_obj_id,
1423 YAFFS_OBJECT_TYPE_DIRECTORY);
1424
1425 file_size = oh->file_size;
1426 is_shrink = oh->is_shrink;
1427 equiv_id = oh->equiv_id;
1428
1429 } else {
1430 in->variant_type =
1431 tags.extra_obj_type;
1432 parent =
1433 yaffs_find_or_create_by_number
1434 (dev, tags.extra_parent_id,
1435 YAFFS_OBJECT_TYPE_DIRECTORY);
1436 file_size = tags.extra_length;
1437 is_shrink =
1438 tags.extra_is_shrink;
1439 equiv_id = tags.extra_equiv_id;
1440 in->lazy_loaded = 1;
1441
1442 }
1443 in->dirty = 0;
1444
1445 if (!parent)
1446 alloc_failed = 1;
1447
1448 /* directory stuff...
1449 * hook up to parent
1450 */
1451
1452 if (parent && parent->variant_type ==
1453 YAFFS_OBJECT_TYPE_UNKNOWN) {
1454 /* Set up as a directory */
1455 parent->variant_type =
1456 YAFFS_OBJECT_TYPE_DIRECTORY;
1457 INIT_LIST_HEAD(&parent->
1458 variant.dir_variant.children);
1459 } else if (!parent
1460 || parent->variant_type !=
1461 YAFFS_OBJECT_TYPE_DIRECTORY) {
1462 /* Hoosterman, another problem....
1463 * We're trying to use a non-directory as a directory
1464 */
1465
1466 yaffs_trace(YAFFS_TRACE_ERROR,
1467 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1468 );
1469 parent = dev->lost_n_found;
1470 }
1471
1472 yaffs_add_obj_to_dir(parent, in);
1473
1474 is_unlinked = (parent == dev->del_dir)
1475 || (parent == dev->unlinked_dir);
1476
1477 if (is_shrink) {
1478 /* Mark the block as having a shrink header */
1479 bi->has_shrink_hdr = 1;
1480 }
1481
1482 /* Note re hardlinks.
1483 * Since we might scan a hardlink before its equivalent object is scanned
1484 * we put them all in a list.
1485 * After scanning is complete, we should have all the objects, so we run
1486 * through this list and fix up all the chains.
1487 */
1488
1489 switch (in->variant_type) {
1490 case YAFFS_OBJECT_TYPE_UNKNOWN:
1491 /* Todo got a problem */
1492 break;
1493 case YAFFS_OBJECT_TYPE_FILE:
1494
1495 if (in->variant.
1496 file_variant.scanned_size <
1497 file_size) {
1498 /* This covers the case where the file size is greater
1499 * than where the data is
1500 * This will happen if the file is resized to be larger
1501 * than its current data extents.
1502 */
1503 in->variant.
1504 file_variant.
1505 file_size =
1506 file_size;
1507 in->variant.
1508 file_variant.
1509 scanned_size =
1510 file_size;
1511 }
1512
1513 if (in->variant.file_variant.
1514 shrink_size > file_size)
1515 in->variant.
1516 file_variant.
1517 shrink_size =
1518 file_size;
1519
1520 break;
1521 case YAFFS_OBJECT_TYPE_HARDLINK:
1522 if (!is_unlinked) {
1523 in->variant.
1524 hardlink_variant.
1525 equiv_id = equiv_id;
1526 in->hard_links.next =
1527 (struct list_head *)
1528 hard_list;
1529 hard_list = in;
1530 }
1531 break;
1532 case YAFFS_OBJECT_TYPE_DIRECTORY:
1533 /* Do nothing */
1534 break;
1535 case YAFFS_OBJECT_TYPE_SPECIAL:
1536 /* Do nothing */
1537 break;
1538 case YAFFS_OBJECT_TYPE_SYMLINK:
1539 if (oh) {
1540 in->variant.
1541 symlink_variant.
1542 alias =
1543 yaffs_clone_str(oh->
1544 alias);
1545 if (!in->variant.
1546 symlink_variant.
1547 alias)
1548 alloc_failed =
1549 1;
1550 }
1551 break;
1552 }
1553
1554 }
1555
1556 }
1557
1558 } /* End of scanning for each chunk */
1559
1560 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1561 /* If we got this far while scanning, then the block is fully allocated. */
1562 state = YAFFS_BLOCK_STATE_FULL;
1563 }
1564
1565 bi->block_state = state;
1566
1567 /* Now let's see if it was dirty */
1568 if (bi->pages_in_use == 0 &&
1569 !bi->has_shrink_hdr &&
1570 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1571 yaffs_block_became_dirty(dev, blk);
1572 }
1573
1574 }
1575
1576 yaffs_skip_rest_of_block(dev);
1577
1578 if (alt_block_index)
1579 vfree(block_index);
1580 else
1581 kfree(block_index);
1582
1583 /* Ok, we've done all the scanning.
1584 * Fix up the hard link chains.
1585 * We should now have scanned all the objects, now it's time to add these
1586 * hardlinks.
1587 */
1588 yaffs_link_fixup(dev, hard_list);
1589
1590 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
1591
1592 if (alloc_failed)
1593 return YAFFS_FAIL;
1594
1595 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
1596
1597 return YAFFS_OK;
1598}
diff --git a/fs/yaffs2/yaffs_yaffs2.h b/fs/yaffs2/yaffs_yaffs2.h
new file mode 100644
index 00000000000..e1a9287fc50
--- /dev/null
+++ b/fs/yaffs2/yaffs_yaffs2.h
@@ -0,0 +1,39 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YAFFS_YAFFS2_H__
17#define __YAFFS_YAFFS2_H__
18
19#include "yaffs_guts.h"
20
21void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev);
22void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev);
23void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
24 struct yaffs_block_info *bi);
25void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
26 struct yaffs_block_info *bi);
27int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi);
28u32 yaffs2_find_refresh_block(struct yaffs_dev *dev);
29int yaffs2_checkpt_required(struct yaffs_dev *dev);
30int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev);
31
32void yaffs2_checkpt_invalidate(struct yaffs_dev *dev);
33int yaffs2_checkpt_save(struct yaffs_dev *dev);
34int yaffs2_checkpt_restore(struct yaffs_dev *dev);
35
36int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size);
37int yaffs2_scan_backwards(struct yaffs_dev *dev);
38
39#endif
diff --git a/fs/yaffs2/yportenv.h b/fs/yaffs2/yportenv.h
new file mode 100644
index 00000000000..8183425448c
--- /dev/null
+++ b/fs/yaffs2/yportenv.h
@@ -0,0 +1,70 @@
1/*
2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
12 *
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14 */
15
16#ifndef __YPORTENV_LINUX_H__
17#define __YPORTENV_LINUX_H__
18
19#include <linux/version.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/sched.h>
23#include <linux/string.h>
24#include <linux/slab.h>
25#include <linux/vmalloc.h>
26#include <linux/xattr.h>
27#include <linux/list.h>
28#include <linux/types.h>
29#include <linux/fs.h>
30#include <linux/stat.h>
31#include <linux/sort.h>
32#include <linux/bitops.h>
33
34#define YCHAR char
35#define YUCHAR unsigned char
36#define _Y(x) x
37
38#define YAFFS_LOSTNFOUND_NAME "lost+found"
39#define YAFFS_LOSTNFOUND_PREFIX "obj"
40
41
42#define YAFFS_ROOT_MODE 0755
43#define YAFFS_LOSTNFOUND_MODE 0700
44
45#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
46#define Y_TIME_CONVERT(x) (x).tv_sec
47
48#define compile_time_assertion(assertion) \
49 ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
50
51
52#ifndef Y_DUMP_STACK
53#define Y_DUMP_STACK() dump_stack()
54#endif
55
56#define yaffs_trace(msk, fmt, ...) do { \
57 if(yaffs_trace_mask & (msk)) \
58 printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__); \
59} while(0)
60
61#ifndef YBUG
62#define YBUG() do {\
63 yaffs_trace(YAFFS_TRACE_BUG,\
64 "bug " __FILE__ " %d",\
65 __LINE__);\
66 Y_DUMP_STACK();\
67} while (0)
68#endif
69
70#endif