blob: 9b94094c70105edf02278008719c6331809cfb27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Randy Dunlap16f7e0f2006-01-11 12:17:46 -08009#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080011#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/buffer_head.h>
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -080014#include <linux/writeback.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070015#include <linux/backing-dev.h>
Chris Masonae78bf92006-09-29 02:00:03 -070016#include <linux/blkdev.h>
Miklos Szeredib1da47e2008-07-01 15:01:28 +020017#include <linux/fsnotify.h>
18#include <linux/security.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080019#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Christoph Hellwig21bea492009-06-08 21:38:31 +090021static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
22{
23 u32 attr;
24
25 mutex_lock(&inode->i_mutex);
26 attr = fat_make_attrs(inode);
27 mutex_unlock(&inode->i_mutex);
28
29 return put_user(attr, user_attr);
30}
31
32static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
33{
34 struct inode *inode = file->f_path.dentry->d_inode;
35 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
36 int is_dir = S_ISDIR(inode->i_mode);
37 u32 attr, oldattr;
38 struct iattr ia;
39 int err;
40
41 err = get_user(attr, user_attr);
42 if (err)
43 goto out;
44
45 mutex_lock(&inode->i_mutex);
46 err = mnt_want_write(file->f_path.mnt);
47 if (err)
48 goto out_unlock_inode;
49
50 /*
51 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
52 * prevents the user from turning us into a VFAT
53 * longname entry. Also, we obviously can't set
54 * any of the NTFS attributes in the high 24 bits.
55 */
56 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
57 /* Merge in ATTR_VOLUME and ATTR_DIR */
58 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
59 (is_dir ? ATTR_DIR : 0);
60 oldattr = fat_make_attrs(inode);
61
62 /* Equivalent to a chmod() */
63 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
64 ia.ia_ctime = current_fs_time(inode->i_sb);
65 if (is_dir)
66 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
67 else {
68 ia.ia_mode = fat_make_mode(sbi, attr,
69 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
70 }
71
72 /* The root directory has no attributes */
73 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
74 err = -EINVAL;
75 goto out_drop_write;
76 }
77
78 if (sbi->options.sys_immutable &&
79 ((attr | oldattr) & ATTR_SYS) &&
80 !capable(CAP_LINUX_IMMUTABLE)) {
81 err = -EPERM;
82 goto out_drop_write;
83 }
84
85 /*
86 * The security check is questionable... We single
87 * out the RO attribute for checking by the security
88 * module, just because it maps to a file mode.
89 */
90 err = security_inode_setattr(file->f_path.dentry, &ia);
91 if (err)
92 goto out_drop_write;
93
94 /* This MUST be done before doing anything irreversible... */
95 err = fat_setattr(file->f_path.dentry, &ia);
96 if (err)
97 goto out_drop_write;
98
99 fsnotify_change(file->f_path.dentry, ia.ia_valid);
100 if (sbi->options.sys_immutable) {
101 if (attr & ATTR_SYS)
102 inode->i_flags |= S_IMMUTABLE;
103 else
104 inode->i_flags &= S_IMMUTABLE;
105 }
106
107 fat_save_attrs(inode, attr);
108 mark_inode_dirty(inode);
109out_drop_write:
110 mnt_drop_write(file->f_path.mnt);
111out_unlock_inode:
112 mutex_unlock(&inode->i_mutex);
113out:
114 return err;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117int fat_generic_ioctl(struct inode *inode, struct file *filp,
118 unsigned int cmd, unsigned long arg)
119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 u32 __user *user_attr = (u32 __user *)arg;
121
122 switch (cmd) {
123 case FAT_IOCTL_GET_ATTRIBUTES:
Christoph Hellwig21bea492009-06-08 21:38:31 +0900124 return fat_ioctl_get_attributes(inode, user_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 case FAT_IOCTL_SET_ATTRIBUTES:
Christoph Hellwig21bea492009-06-08 21:38:31 +0900126 return fat_ioctl_set_attributes(filp, user_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 default:
128 return -ENOTTY; /* Inappropriate ioctl for device */
129 }
130}
131
Chris Masonae78bf92006-09-29 02:00:03 -0700132static int fat_file_release(struct inode *inode, struct file *filp)
133{
134 if ((filp->f_mode & FMODE_WRITE) &&
135 MSDOS_SB(inode->i_sb)->options.flush) {
136 fat_flush_inodes(inode->i_sb, inode, NULL);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700137 congestion_wait(WRITE, HZ/10);
Chris Masonae78bf92006-09-29 02:00:03 -0700138 }
139 return 0;
140}
141
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800142const struct file_operations fat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .llseek = generic_file_llseek,
144 .read = do_sync_read,
145 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .aio_read = generic_file_aio_read,
OGAWA Hirofumief402262005-09-16 19:28:13 -0700147 .aio_write = generic_file_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .mmap = generic_file_mmap,
Chris Masonae78bf92006-09-29 02:00:03 -0700149 .release = fat_file_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .ioctl = fat_generic_ioctl,
151 .fsync = file_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200152 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800155static int fat_cont_expand(struct inode *inode, loff_t size)
156{
157 struct address_space *mapping = inode->i_mapping;
158 loff_t start = inode->i_size, count = size - inode->i_size;
159 int err;
160
161 err = generic_cont_expand_simple(inode, size);
162 if (err)
163 goto out;
164
165 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
166 mark_inode_dirty(inode);
167 if (IS_SYNC(inode))
168 err = sync_page_range_nolock(inode, mapping, start, count);
169out:
170 return err;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* Free all clusters after the skip'th cluster. */
174static int fat_free(struct inode *inode, int skip)
175{
176 struct super_block *sb = inode->i_sb;
177 int err, wait, free_start, i_start, i_logstart;
178
179 if (MSDOS_I(inode)->i_start == 0)
180 return 0;
181
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800182 fat_cache_inval_inode(inode);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 wait = IS_DIRSYNC(inode);
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800185 i_start = free_start = MSDOS_I(inode)->i_start;
186 i_logstart = MSDOS_I(inode)->i_logstart;
187
188 /* First, we write the new file size. */
189 if (!skip) {
190 MSDOS_I(inode)->i_start = 0;
191 MSDOS_I(inode)->i_logstart = 0;
192 }
193 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
194 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
195 if (wait) {
196 err = fat_sync_inode(inode);
197 if (err) {
198 MSDOS_I(inode)->i_start = i_start;
199 MSDOS_I(inode)->i_logstart = i_logstart;
200 return err;
201 }
202 } else
203 mark_inode_dirty(inode);
204
205 /* Write a new EOF, and get the remaining cluster chain for freeing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (skip) {
207 struct fat_entry fatent;
208 int ret, fclus, dclus;
209
210 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
211 if (ret < 0)
212 return ret;
213 else if (ret == FAT_ENT_EOF)
214 return 0;
215
216 fatent_init(&fatent);
217 ret = fat_ent_read(inode, &fatent, dclus);
218 if (ret == FAT_ENT_EOF) {
219 fatent_brelse(&fatent);
220 return 0;
221 } else if (ret == FAT_ENT_FREE) {
Denis Karpov85c78592009-06-04 02:34:22 +0900222 fat_fs_error(sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 "%s: invalid cluster chain (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700224 __func__, MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ret = -EIO;
226 } else if (ret > 0) {
227 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
228 if (err)
229 ret = err;
230 }
231 fatent_brelse(&fatent);
232 if (ret < 0)
233 return ret;
234
235 free_start = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
238
239 /* Freeing the remained cluster chain */
240 return fat_free_clusters(inode, free_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243void fat_truncate(struct inode *inode)
244{
Jonathan Corbet9c206162008-05-29 17:14:05 -0600245 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 const unsigned int cluster_size = sbi->cluster_size;
247 int nr_clusters;
248
249 /*
250 * This protects against truncating a file bigger than it was then
251 * trying to write into the hole.
252 */
253 if (MSDOS_I(inode)->mmu_private > inode->i_size)
254 MSDOS_I(inode)->mmu_private = inode->i_size;
255
256 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 fat_free(inode, nr_clusters);
Chris Masonae78bf92006-09-29 02:00:03 -0700259 fat_flush_inodes(inode->i_sb, inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800262int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
263{
264 struct inode *inode = dentry->d_inode;
265 generic_fillattr(inode, stat);
266 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
267 return 0;
268}
269EXPORT_SYMBOL_GPL(fat_getattr);
270
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700271static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
272 struct inode *inode, umode_t *mode_ptr)
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700273{
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700274 mode_t mask, perm;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700275
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700276 /*
277 * Note, the basic check is already done by a caller of
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800278 * (attr->ia_mode & ~FAT_VALID_MODE)
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700279 */
280
281 if (S_ISREG(inode->i_mode))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700282 mask = sbi->options.fs_fmask;
283 else
284 mask = sbi->options.fs_dmask;
285
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700286 perm = *mode_ptr & ~(S_IFMT | mask);
287
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700288 /*
289 * Of the r and x bits, all (subject to umask) must be present. Of the
290 * w bits, either all (subject to umask) or none must be present.
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800291 *
292 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700293 */
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700294 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700295 return -EPERM;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800296 if (fat_mode_can_hold_ro(inode)) {
297 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
298 return -EPERM;
299 } else {
300 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
301 return -EPERM;
302 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700303
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700304 *mode_ptr &= S_IFMT | perm;
305
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700306 return 0;
307}
308
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700309static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
310{
311 mode_t allow_utime = sbi->options.allow_utime;
312
David Howellsf0ce7ee2008-11-14 10:38:52 +1100313 if (current_fsuid() != inode->i_uid) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700314 if (in_group_p(inode->i_gid))
315 allow_utime >>= 3;
316 if (allow_utime & MAY_WRITE)
317 return 1;
318 }
319
320 /* use a default check */
321 return 0;
322}
323
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900324#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800325/* valid file mode bits */
326#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900327
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700328int fat_setattr(struct dentry *dentry, struct iattr *attr)
329{
330 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
331 struct inode *inode = dentry->d_inode;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700332 unsigned int ia_valid;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800333 int error;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700334
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700335 /*
336 * Expand the file. Since inode_setattr() updates ->i_size
337 * before calling the ->truncate(), but FAT needs to fill the
338 * hole before it.
339 */
340 if (attr->ia_valid & ATTR_SIZE) {
341 if (attr->ia_size > inode->i_size) {
342 error = fat_cont_expand(inode, attr->ia_size);
343 if (error || attr->ia_valid == ATTR_SIZE)
344 goto out;
345 attr->ia_valid &= ~ATTR_SIZE;
346 }
347 }
348
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700349 /* Check for setting the inode time. */
350 ia_valid = attr->ia_valid;
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900351 if (ia_valid & TIMES_SET_FLAGS) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700352 if (fat_allow_set_time(sbi, inode))
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900353 attr->ia_valid &= ~TIMES_SET_FLAGS;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700354 }
355
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700356 error = inode_change_ok(inode, attr);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700357 attr->ia_valid = ia_valid;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700358 if (error) {
359 if (sbi->options.quiet)
360 error = 0;
361 goto out;
362 }
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700363
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700364 if (((attr->ia_valid & ATTR_UID) &&
365 (attr->ia_uid != sbi->options.fs_uid)) ||
366 ((attr->ia_valid & ATTR_GID) &&
OGAWA Hirofumie97e8de2008-04-28 02:16:26 -0700367 (attr->ia_gid != sbi->options.fs_gid)) ||
368 ((attr->ia_valid & ATTR_MODE) &&
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800369 (attr->ia_mode & ~FAT_VALID_MODE)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700370 error = -EPERM;
371
372 if (error) {
373 if (sbi->options.quiet)
374 error = 0;
375 goto out;
376 }
377
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700378 /*
379 * We don't return -EPERM here. Yes, strange, but this is too
380 * old behavior.
381 */
382 if (attr->ia_valid & ATTR_MODE) {
383 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
384 attr->ia_valid &= ~ATTR_MODE;
385 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700386
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800387 if (attr->ia_valid)
388 error = inode_setattr(inode, attr);
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700389out:
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700390 return error;
391}
392EXPORT_SYMBOL_GPL(fat_setattr);
393
Arjan van de Ven754661f2007-02-12 00:55:38 -0800394const struct inode_operations fat_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .truncate = fat_truncate,
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700396 .setattr = fat_setattr,
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800397 .getattr = fat_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};