blob: e955a56b4e5ea0455c27782ce754d0734691ef49 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021int fat_generic_ioctl(struct inode *inode, struct file *filp,
22 unsigned int cmd, unsigned long arg)
23{
24 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
25 u32 __user *user_attr = (u32 __user *)arg;
26
27 switch (cmd) {
28 case FAT_IOCTL_GET_ATTRIBUTES:
29 {
30 u32 attr;
31
OGAWA Hirofumi91834822008-11-06 12:53:54 -080032 mutex_lock(&inode->i_mutex);
33 attr = fat_make_attrs(inode);
34 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 return put_user(attr, user_attr);
37 }
38 case FAT_IOCTL_SET_ATTRIBUTES:
39 {
40 u32 attr, oldattr;
41 int err, is_dir = S_ISDIR(inode->i_mode);
42 struct iattr ia;
43
44 err = get_user(attr, user_attr);
45 if (err)
46 return err;
47
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080048 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Dave Hansen42a74f22008-02-15 14:37:46 -080050 err = mnt_want_write(filp->f_path.mnt);
51 if (err)
52 goto up_no_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /*
55 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
56 * prevents the user from turning us into a VFAT
57 * longname entry. Also, we obviously can't set
58 * any of the NTFS attributes in the high 24 bits.
59 */
60 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
61 /* Merge in ATTR_VOLUME and ATTR_DIR */
62 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
63 (is_dir ? ATTR_DIR : 0);
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -080064 oldattr = fat_make_attrs(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* Equivalent to a chmod() */
67 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
Miklos Szeredib1da47e2008-07-01 15:01:28 +020068 ia.ia_ctime = current_fs_time(inode->i_sb);
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -080069 if (is_dir)
70 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
71 else {
72 ia.ia_mode = fat_make_mode(sbi, attr,
73 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
76 /* The root directory has no attributes */
77 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
78 err = -EINVAL;
79 goto up;
80 }
81
82 if (sbi->options.sys_immutable) {
83 if ((attr | oldattr) & ATTR_SYS) {
84 if (!capable(CAP_LINUX_IMMUTABLE)) {
85 err = -EPERM;
86 goto up;
87 }
88 }
89 }
90
Miklos Szeredib1da47e2008-07-01 15:01:28 +020091 /*
92 * The security check is questionable... We single
93 * out the RO attribute for checking by the security
94 * module, just because it maps to a file mode.
95 */
96 err = security_inode_setattr(filp->f_path.dentry, &ia);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (err)
98 goto up;
99
Miklos Szeredib1da47e2008-07-01 15:01:28 +0200100 /* This MUST be done before doing anything irreversible... */
101 err = fat_setattr(filp->f_path.dentry, &ia);
102 if (err)
103 goto up;
104
105 fsnotify_change(filp->f_path.dentry, ia.ia_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (sbi->options.sys_immutable) {
107 if (attr & ATTR_SYS)
108 inode->i_flags |= S_IMMUTABLE;
109 else
110 inode->i_flags &= S_IMMUTABLE;
111 }
112
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800113 fat_save_attrs(inode, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800115up:
116 mnt_drop_write(filp->f_path.mnt);
117up_no_drop_write:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800118 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return err;
120 }
121 default:
122 return -ENOTTY; /* Inappropriate ioctl for device */
123 }
124}
125
Chris Masonae78bf92006-09-29 02:00:03 -0700126static int fat_file_release(struct inode *inode, struct file *filp)
127{
128 if ((filp->f_mode & FMODE_WRITE) &&
129 MSDOS_SB(inode->i_sb)->options.flush) {
130 fat_flush_inodes(inode->i_sb, inode, NULL);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700131 congestion_wait(WRITE, HZ/10);
Chris Masonae78bf92006-09-29 02:00:03 -0700132 }
133 return 0;
134}
135
Al Virob5224122009-06-07 13:44:36 -0400136int fat_file_fsync(struct file *filp, struct dentry *dentry, int datasync)
137{
138 struct inode *inode = dentry->d_inode;
139 int res, err;
140
141 res = simple_fsync(filp, dentry, datasync);
142 err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
143
144 return res ? res : err;
145}
146
147
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800148const struct file_operations fat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .llseek = generic_file_llseek,
150 .read = do_sync_read,
151 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .aio_read = generic_file_aio_read,
OGAWA Hirofumief402262005-09-16 19:28:13 -0700153 .aio_write = generic_file_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .mmap = generic_file_mmap,
Chris Masonae78bf92006-09-29 02:00:03 -0700155 .release = fat_file_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .ioctl = fat_generic_ioctl,
Al Virob5224122009-06-07 13:44:36 -0400157 .fsync = fat_file_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200158 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800161static int fat_cont_expand(struct inode *inode, loff_t size)
162{
163 struct address_space *mapping = inode->i_mapping;
164 loff_t start = inode->i_size, count = size - inode->i_size;
165 int err;
166
167 err = generic_cont_expand_simple(inode, size);
168 if (err)
169 goto out;
170
171 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
172 mark_inode_dirty(inode);
173 if (IS_SYNC(inode))
174 err = sync_page_range_nolock(inode, mapping, start, count);
175out:
176 return err;
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Free all clusters after the skip'th cluster. */
180static int fat_free(struct inode *inode, int skip)
181{
182 struct super_block *sb = inode->i_sb;
183 int err, wait, free_start, i_start, i_logstart;
184
185 if (MSDOS_I(inode)->i_start == 0)
186 return 0;
187
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800188 fat_cache_inval_inode(inode);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 wait = IS_DIRSYNC(inode);
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800191 i_start = free_start = MSDOS_I(inode)->i_start;
192 i_logstart = MSDOS_I(inode)->i_logstart;
193
194 /* First, we write the new file size. */
195 if (!skip) {
196 MSDOS_I(inode)->i_start = 0;
197 MSDOS_I(inode)->i_logstart = 0;
198 }
199 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
200 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
201 if (wait) {
202 err = fat_sync_inode(inode);
203 if (err) {
204 MSDOS_I(inode)->i_start = i_start;
205 MSDOS_I(inode)->i_logstart = i_logstart;
206 return err;
207 }
208 } else
209 mark_inode_dirty(inode);
210
211 /* Write a new EOF, and get the remaining cluster chain for freeing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (skip) {
213 struct fat_entry fatent;
214 int ret, fclus, dclus;
215
216 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
217 if (ret < 0)
218 return ret;
219 else if (ret == FAT_ENT_EOF)
220 return 0;
221
222 fatent_init(&fatent);
223 ret = fat_ent_read(inode, &fatent, dclus);
224 if (ret == FAT_ENT_EOF) {
225 fatent_brelse(&fatent);
226 return 0;
227 } else if (ret == FAT_ENT_FREE) {
228 fat_fs_panic(sb,
229 "%s: invalid cluster chain (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700230 __func__, MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret = -EIO;
232 } else if (ret > 0) {
233 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
234 if (err)
235 ret = err;
236 }
237 fatent_brelse(&fatent);
238 if (ret < 0)
239 return ret;
240
241 free_start = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
244
245 /* Freeing the remained cluster chain */
246 return fat_free_clusters(inode, free_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249void fat_truncate(struct inode *inode)
250{
Jonathan Corbet9c206162008-05-29 17:14:05 -0600251 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 const unsigned int cluster_size = sbi->cluster_size;
253 int nr_clusters;
254
255 /*
256 * This protects against truncating a file bigger than it was then
257 * trying to write into the hole.
258 */
259 if (MSDOS_I(inode)->mmu_private > inode->i_size)
260 MSDOS_I(inode)->mmu_private = inode->i_size;
261
262 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 fat_free(inode, nr_clusters);
Chris Masonae78bf92006-09-29 02:00:03 -0700265 fat_flush_inodes(inode->i_sb, inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800268int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
269{
270 struct inode *inode = dentry->d_inode;
271 generic_fillattr(inode, stat);
272 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
273 return 0;
274}
275EXPORT_SYMBOL_GPL(fat_getattr);
276
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700277static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
278 struct inode *inode, umode_t *mode_ptr)
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700279{
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700280 mode_t mask, perm;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700281
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700282 /*
283 * Note, the basic check is already done by a caller of
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800284 * (attr->ia_mode & ~FAT_VALID_MODE)
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700285 */
286
287 if (S_ISREG(inode->i_mode))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700288 mask = sbi->options.fs_fmask;
289 else
290 mask = sbi->options.fs_dmask;
291
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700292 perm = *mode_ptr & ~(S_IFMT | mask);
293
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700294 /*
295 * Of the r and x bits, all (subject to umask) must be present. Of the
296 * w bits, either all (subject to umask) or none must be present.
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800297 *
298 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700299 */
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700300 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700301 return -EPERM;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800302 if (fat_mode_can_hold_ro(inode)) {
303 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
304 return -EPERM;
305 } else {
306 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
307 return -EPERM;
308 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700309
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700310 *mode_ptr &= S_IFMT | perm;
311
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700312 return 0;
313}
314
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700315static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
316{
317 mode_t allow_utime = sbi->options.allow_utime;
318
David Howellsf0ce7ee2008-11-14 10:38:52 +1100319 if (current_fsuid() != inode->i_uid) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700320 if (in_group_p(inode->i_gid))
321 allow_utime >>= 3;
322 if (allow_utime & MAY_WRITE)
323 return 1;
324 }
325
326 /* use a default check */
327 return 0;
328}
329
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900330#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800331/* valid file mode bits */
332#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900333
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700334int fat_setattr(struct dentry *dentry, struct iattr *attr)
335{
336 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
337 struct inode *inode = dentry->d_inode;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700338 unsigned int ia_valid;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800339 int error;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700340
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700341 /*
342 * Expand the file. Since inode_setattr() updates ->i_size
343 * before calling the ->truncate(), but FAT needs to fill the
344 * hole before it.
345 */
346 if (attr->ia_valid & ATTR_SIZE) {
347 if (attr->ia_size > inode->i_size) {
348 error = fat_cont_expand(inode, attr->ia_size);
349 if (error || attr->ia_valid == ATTR_SIZE)
350 goto out;
351 attr->ia_valid &= ~ATTR_SIZE;
352 }
353 }
354
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700355 /* Check for setting the inode time. */
356 ia_valid = attr->ia_valid;
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900357 if (ia_valid & TIMES_SET_FLAGS) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700358 if (fat_allow_set_time(sbi, inode))
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900359 attr->ia_valid &= ~TIMES_SET_FLAGS;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700360 }
361
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700362 error = inode_change_ok(inode, attr);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700363 attr->ia_valid = ia_valid;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700364 if (error) {
365 if (sbi->options.quiet)
366 error = 0;
367 goto out;
368 }
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700369
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700370 if (((attr->ia_valid & ATTR_UID) &&
371 (attr->ia_uid != sbi->options.fs_uid)) ||
372 ((attr->ia_valid & ATTR_GID) &&
OGAWA Hirofumie97e8de2008-04-28 02:16:26 -0700373 (attr->ia_gid != sbi->options.fs_gid)) ||
374 ((attr->ia_valid & ATTR_MODE) &&
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800375 (attr->ia_mode & ~FAT_VALID_MODE)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700376 error = -EPERM;
377
378 if (error) {
379 if (sbi->options.quiet)
380 error = 0;
381 goto out;
382 }
383
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700384 /*
385 * We don't return -EPERM here. Yes, strange, but this is too
386 * old behavior.
387 */
388 if (attr->ia_valid & ATTR_MODE) {
389 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
390 attr->ia_valid &= ~ATTR_MODE;
391 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700392
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800393 if (attr->ia_valid)
394 error = inode_setattr(inode, attr);
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700395out:
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700396 return error;
397}
398EXPORT_SYMBOL_GPL(fat_setattr);
399
Arjan van de Ven754661f2007-02-12 00:55:38 -0800400const struct inode_operations fat_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .truncate = fat_truncate,
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700402 .setattr = fat_setattr,
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800403 .getattr = fat_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};