- 2.6.17 port work build breaks, but the patch set is relativly stable
[linux-flexiantxendom0-3.2.10.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/capability.h>
10 #include <linux/module.h>
11 #include <linux/time.h>
12 #include <linux/msdos_fs.h>
13 #include <linux/smp_lock.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/blkdev.h>
17
18 int fat_generic_ioctl(struct inode *inode, struct file *filp,
19                       unsigned int cmd, unsigned long arg)
20 {
21         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
22         u32 __user *user_attr = (u32 __user *)arg;
23
24         switch (cmd) {
25         case FAT_IOCTL_GET_ATTRIBUTES:
26         {
27                 u32 attr;
28
29                 if (inode->i_ino == MSDOS_ROOT_INO)
30                         attr = ATTR_DIR;
31                 else
32                         attr = fat_attr(inode);
33
34                 return put_user(attr, user_attr);
35         }
36         case FAT_IOCTL_SET_ATTRIBUTES:
37         {
38                 u32 attr, oldattr;
39                 int err, is_dir = S_ISDIR(inode->i_mode);
40                 struct iattr ia;
41
42                 err = get_user(attr, user_attr);
43                 if (err)
44                         return err;
45
46                 mutex_lock(&inode->i_mutex);
47
48                 if (IS_RDONLY(inode)) {
49                         err = -EROFS;
50                         goto up;
51                 }
52
53                 /*
54                  * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
55                  * prevents the user from turning us into a VFAT
56                  * longname entry.  Also, we obviously can't set
57                  * any of the NTFS attributes in the high 24 bits.
58                  */
59                 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
60                 /* Merge in ATTR_VOLUME and ATTR_DIR */
61                 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
62                         (is_dir ? ATTR_DIR : 0);
63                 oldattr = fat_attr(inode);
64
65                 /* Equivalent to a chmod() */
66                 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
67                 if (is_dir) {
68                         ia.ia_mode = MSDOS_MKMODE(attr,
69                                 S_IRWXUGO & ~sbi->options.fs_dmask)
70                                 | S_IFDIR;
71                 } else {
72                         ia.ia_mode = MSDOS_MKMODE(attr,
73                                 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
74                                 & ~sbi->options.fs_fmask)
75                                 | S_IFREG;
76                 }
77
78                 /* The root directory has no attributes */
79                 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
80                         err = -EINVAL;
81                         goto up;
82                 }
83
84                 if (sbi->options.sys_immutable) {
85                         if ((attr | oldattr) & ATTR_SYS) {
86                                 if (!capable(CAP_LINUX_IMMUTABLE)) {
87                                         err = -EPERM;
88                                         goto up;
89                                 }
90                         }
91                 }
92
93                 /* This MUST be done before doing anything irreversible... */
94                 err = notify_change(filp->f_dentry, &ia);
95                 if (err)
96                         goto up;
97
98                 if (sbi->options.sys_immutable) {
99                         if (attr & ATTR_SYS)
100                                 inode->i_flags |= S_IMMUTABLE;
101                         else
102                                 inode->i_flags &= S_IMMUTABLE;
103                 }
104
105                 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
106                 mark_inode_dirty(inode);
107         up:
108                 mutex_unlock(&inode->i_mutex);
109                 return err;
110         }
111         default:
112                 return -ENOTTY; /* Inappropriate ioctl for device */
113         }
114 }
115
116 static int
117 fat_file_release(struct inode *inode, struct file *filp)
118 {
119         if ((filp->f_mode & FMODE_WRITE) &&
120              MSDOS_SB(inode->i_sb)->options.flush) {
121                 writeback_inode(inode);
122                 writeback_bdev(inode->i_sb);
123                 blk_congestion_wait(WRITE, HZ/10);
124         }
125         return 0;
126 }
127
128 const struct file_operations fat_file_operations = {
129         .llseek         = generic_file_llseek,
130         .read           = do_sync_read,
131         .write          = do_sync_write,
132         .readv          = generic_file_readv,
133         .writev         = generic_file_writev,
134         .aio_read       = generic_file_aio_read,
135         .aio_write      = generic_file_aio_write,
136         .mmap           = generic_file_mmap,
137         .release        = fat_file_release,
138         .ioctl          = fat_generic_ioctl,
139         .fsync          = file_fsync,
140         .sendfile       = generic_file_sendfile,
141 };
142
143 static int fat_cont_expand(struct inode *inode, loff_t size)
144 {
145         struct address_space *mapping = inode->i_mapping;
146         loff_t start = inode->i_size, count = size - inode->i_size;
147         int err;
148
149         err = generic_cont_expand_simple(inode, size);
150         if (err)
151                 goto out;
152
153         inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
154         mark_inode_dirty(inode);
155         if (IS_SYNC(inode))
156                 err = sync_page_range_nolock(inode, mapping, start, count);
157 out:
158         return err;
159 }
160
161 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
162 {
163         struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
164         struct inode *inode = dentry->d_inode;
165         int mask, error = 0;
166
167         lock_kernel();
168
169         /*
170          * Expand the file. Since inode_setattr() updates ->i_size
171          * before calling the ->truncate(), but FAT needs to fill the
172          * hole before it.
173          */
174         if (attr->ia_valid & ATTR_SIZE) {
175                 if (attr->ia_size > inode->i_size) {
176                         error = fat_cont_expand(inode, attr->ia_size);
177                         if (error || attr->ia_valid == ATTR_SIZE)
178                                 goto out;
179                         attr->ia_valid &= ~ATTR_SIZE;
180                 }
181         }
182
183         error = inode_change_ok(inode, attr);
184         if (error) {
185                 if (sbi->options.quiet)
186                         error = 0;
187                 goto out;
188         }
189         if (((attr->ia_valid & ATTR_UID) &&
190              (attr->ia_uid != sbi->options.fs_uid)) ||
191             ((attr->ia_valid & ATTR_GID) &&
192              (attr->ia_gid != sbi->options.fs_gid)) ||
193             ((attr->ia_valid & ATTR_MODE) &&
194              (attr->ia_mode & ~MSDOS_VALID_MODE)))
195                 error = -EPERM;
196
197         if (error) {
198                 if (sbi->options.quiet)
199                         error = 0;
200                 goto out;
201         }
202         error = inode_setattr(inode, attr);
203         if (error)
204                 goto out;
205
206         if (S_ISDIR(inode->i_mode))
207                 mask = sbi->options.fs_dmask;
208         else
209                 mask = sbi->options.fs_fmask;
210         inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
211 out:
212         unlock_kernel();
213         return error;
214 }
215
216 EXPORT_SYMBOL_GPL(fat_notify_change);
217
218 /* Free all clusters after the skip'th cluster. */
219 static int fat_free(struct inode *inode, int skip)
220 {
221         struct super_block *sb = inode->i_sb;
222         int err, wait, free_start, i_start, i_logstart;
223
224         if (MSDOS_I(inode)->i_start == 0)
225                 return 0;
226
227         fat_cache_inval_inode(inode);
228
229         wait = IS_DIRSYNC(inode);
230         i_start = free_start = MSDOS_I(inode)->i_start;
231         i_logstart = MSDOS_I(inode)->i_logstart;
232
233         /* First, we write the new file size. */
234         if (!skip) {
235                 MSDOS_I(inode)->i_start = 0;
236                 MSDOS_I(inode)->i_logstart = 0;
237         }
238         MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
239         inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
240         if (wait) {
241                 err = fat_sync_inode(inode);
242                 if (err) {
243                         MSDOS_I(inode)->i_start = i_start;
244                         MSDOS_I(inode)->i_logstart = i_logstart;
245                         return err;
246                 }
247         } else
248                 mark_inode_dirty(inode);
249
250         /* Write a new EOF, and get the remaining cluster chain for freeing. */
251         if (skip) {
252                 struct fat_entry fatent;
253                 int ret, fclus, dclus;
254
255                 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
256                 if (ret < 0)
257                         return ret;
258                 else if (ret == FAT_ENT_EOF)
259                         return 0;
260
261                 fatent_init(&fatent);
262                 ret = fat_ent_read(inode, &fatent, dclus);
263                 if (ret == FAT_ENT_EOF) {
264                         fatent_brelse(&fatent);
265                         return 0;
266                 } else if (ret == FAT_ENT_FREE) {
267                         fat_fs_panic(sb,
268                                      "%s: invalid cluster chain (i_pos %lld)",
269                                      __FUNCTION__, MSDOS_I(inode)->i_pos);
270                         ret = -EIO;
271                 } else if (ret > 0) {
272                         err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
273                         if (err)
274                                 ret = err;
275                 }
276                 fatent_brelse(&fatent);
277                 if (ret < 0)
278                         return ret;
279
280                 free_start = ret;
281         }
282         inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
283
284         /* Freeing the remained cluster chain */
285         return fat_free_clusters(inode, free_start);
286 }
287
288 void fat_truncate(struct inode *inode)
289 {
290         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
291         const unsigned int cluster_size = sbi->cluster_size;
292         int nr_clusters;
293
294         /*
295          * This protects against truncating a file bigger than it was then
296          * trying to write into the hole.
297          */
298         if (MSDOS_I(inode)->mmu_private > inode->i_size)
299                 MSDOS_I(inode)->mmu_private = inode->i_size;
300
301         nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
302
303         lock_kernel();
304         fat_free(inode, nr_clusters);
305         unlock_kernel();
306         if (MSDOS_SB(inode->i_sb)->options.flush) {
307                 writeback_inode(inode);
308                 writeback_bdev(inode->i_sb);
309         }
310 }
311
312 struct inode_operations fat_file_inode_operations = {
313         .truncate       = fat_truncate,
314         .setattr        = fat_notify_change,
315 };