ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / fat / file.c
1 /*
2  *  linux/fs/fat/file.c
3  *
4  *  Written 1992,1993 by Werner Almesberger
5  *
6  *  regular file handling primitives for fat-based filesystems
7  */
8
9 #include <linux/time.h>
10 #include <linux/msdos_fs.h>
11 #include <linux/smp_lock.h>
12 #include <linux/buffer_head.h>
13
14 static ssize_t fat_file_write(struct file *filp, const char __user *buf,
15                               size_t count, loff_t *ppos);
16
17 struct file_operations fat_file_operations = {
18         .llseek         = generic_file_llseek,
19         .read           = generic_file_read,
20         .write          = fat_file_write,
21         .mmap           = generic_file_mmap,
22         .fsync          = file_fsync,
23         .readv          = generic_file_readv,
24         .writev         = generic_file_writev,
25         .sendfile       = generic_file_sendfile,
26 };
27
28 struct inode_operations fat_file_inode_operations = {
29         .truncate       = fat_truncate,
30         .setattr        = fat_notify_change,
31 };
32
33 int fat_get_block(struct inode *inode, sector_t iblock,
34                   struct buffer_head *bh_result, int create)
35 {
36         struct super_block *sb = inode->i_sb;
37         sector_t phys;
38         int err;
39
40         err = fat_bmap(inode, iblock, &phys);
41         if (err)
42                 return err;
43         if (phys) {
44                 map_bh(bh_result, sb, phys);
45                 return 0;
46         }
47         if (!create)
48                 return 0;
49         if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
50                 BUG();
51                 return -EIO;
52         }
53         if (!((unsigned long)iblock & (MSDOS_SB(sb)->sec_per_clus - 1))) {
54                 int error;
55
56                 error = fat_add_cluster(inode);
57                 if (error < 0)
58                         return error;
59         }
60         MSDOS_I(inode)->mmu_private += sb->s_blocksize;
61         err = fat_bmap(inode, iblock, &phys);
62         if (err)
63                 return err;
64         if (!phys)
65                 BUG();
66         set_buffer_new(bh_result);
67         map_bh(bh_result, sb, phys);
68         return 0;
69 }
70
71 static ssize_t fat_file_write(struct file *filp, const char __user *buf,
72                               size_t count, loff_t *ppos)
73 {
74         struct inode *inode = filp->f_dentry->d_inode;
75         int retval;
76
77         retval = generic_file_write(filp, buf, count, ppos);
78         if (retval > 0) {
79                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
80                 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
81                 mark_inode_dirty(inode);
82         }
83         return retval;
84 }
85
86 void fat_truncate(struct inode *inode)
87 {
88         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
89         const unsigned int cluster_size = sbi->cluster_size;
90         int nr_clusters;
91
92         /* Why no return value?  Surely the disk could fail... */
93         if (IS_RDONLY (inode))
94                 return /* -EPERM */;
95         if (IS_IMMUTABLE(inode))
96                 return /* -EPERM */;
97
98         /* 
99          * This protects against truncating a file bigger than it was then
100          * trying to write into the hole.
101          */
102         if (MSDOS_I(inode)->mmu_private > inode->i_size)
103                 MSDOS_I(inode)->mmu_private = inode->i_size;
104
105         nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
106
107         lock_kernel();
108         fat_free(inode, nr_clusters);
109         MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
110         unlock_kernel();
111         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
112         mark_inode_dirty(inode);
113 }