blob: a08f1039909a76e6427cf9e64ddf16c30a1716c9 [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>
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +090011#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080012#include <linux/mount.h>
Chris Masonae78bf92006-09-29 02:00:03 -070013#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040014#include <linux/backing-dev.h>
Miklos Szeredib1da47e2008-07-01 15:01:28 +020015#include <linux/fsnotify.h>
16#include <linux/security.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080017#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Christoph Hellwig21bea492009-06-08 21:38:31 +090019static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
20{
21 u32 attr;
22
23 mutex_lock(&inode->i_mutex);
24 attr = fat_make_attrs(inode);
25 mutex_unlock(&inode->i_mutex);
26
27 return put_user(attr, user_attr);
28}
29
30static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
31{
Al Viro496ad9a2013-01-23 17:07:38 -050032 struct inode *inode = file_inode(file);
Christoph Hellwig21bea492009-06-08 21:38:31 +090033 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
34 int is_dir = S_ISDIR(inode->i_mode);
35 u32 attr, oldattr;
36 struct iattr ia;
37 int err;
38
39 err = get_user(attr, user_attr);
40 if (err)
41 goto out;
42
Al Viroa561be72011-11-23 11:57:51 -050043 err = mnt_want_write_file(file);
Christoph Hellwig21bea492009-06-08 21:38:31 +090044 if (err)
Jan Karae24f17d2012-06-12 16:20:31 +020045 goto out;
46 mutex_lock(&inode->i_mutex);
Christoph Hellwig21bea492009-06-08 21:38:31 +090047
48 /*
49 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
50 * prevents the user from turning us into a VFAT
51 * longname entry. Also, we obviously can't set
52 * any of the NTFS attributes in the high 24 bits.
53 */
54 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
55 /* Merge in ATTR_VOLUME and ATTR_DIR */
56 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
57 (is_dir ? ATTR_DIR : 0);
58 oldattr = fat_make_attrs(inode);
59
60 /* Equivalent to a chmod() */
61 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
62 ia.ia_ctime = current_fs_time(inode->i_sb);
63 if (is_dir)
64 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
65 else {
66 ia.ia_mode = fat_make_mode(sbi, attr,
67 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
68 }
69
70 /* The root directory has no attributes */
71 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
72 err = -EINVAL;
Jan Karae24f17d2012-06-12 16:20:31 +020073 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090074 }
75
76 if (sbi->options.sys_immutable &&
77 ((attr | oldattr) & ATTR_SYS) &&
78 !capable(CAP_LINUX_IMMUTABLE)) {
79 err = -EPERM;
Jan Karae24f17d2012-06-12 16:20:31 +020080 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090081 }
82
83 /*
84 * The security check is questionable... We single
85 * out the RO attribute for checking by the security
86 * module, just because it maps to a file mode.
87 */
88 err = security_inode_setattr(file->f_path.dentry, &ia);
89 if (err)
Jan Karae24f17d2012-06-12 16:20:31 +020090 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090091
92 /* This MUST be done before doing anything irreversible... */
93 err = fat_setattr(file->f_path.dentry, &ia);
94 if (err)
Jan Karae24f17d2012-06-12 16:20:31 +020095 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090096
97 fsnotify_change(file->f_path.dentry, ia.ia_valid);
98 if (sbi->options.sys_immutable) {
99 if (attr & ATTR_SYS)
100 inode->i_flags |= S_IMMUTABLE;
101 else
OGAWA Hirofumi1adffba2011-05-31 19:38:07 +0900102 inode->i_flags &= ~S_IMMUTABLE;
Christoph Hellwig21bea492009-06-08 21:38:31 +0900103 }
104
105 fat_save_attrs(inode, attr);
106 mark_inode_dirty(inode);
Christoph Hellwig21bea492009-06-08 21:38:31 +0900107out_unlock_inode:
108 mutex_unlock(&inode->i_mutex);
Jan Karae24f17d2012-06-12 16:20:31 +0200109 mnt_drop_write_file(file);
Christoph Hellwig21bea492009-06-08 21:38:31 +0900110out:
111 return err;
112}
113
Mike Lockwood6e5b93e2013-07-08 16:00:46 -0700114static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
115{
116 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
117 return put_user(sbi->vol_id, user_attr);
118}
119
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900120long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Al Viro496ad9a2013-01-23 17:07:38 -0500122 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 u32 __user *user_attr = (u32 __user *)arg;
124
125 switch (cmd) {
126 case FAT_IOCTL_GET_ATTRIBUTES:
Christoph Hellwig21bea492009-06-08 21:38:31 +0900127 return fat_ioctl_get_attributes(inode, user_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 case FAT_IOCTL_SET_ATTRIBUTES:
Christoph Hellwig21bea492009-06-08 21:38:31 +0900129 return fat_ioctl_set_attributes(filp, user_attr);
Mike Lockwood6e5b93e2013-07-08 16:00:46 -0700130 case FAT_IOCTL_GET_VOLUME_ID:
131 return fat_ioctl_get_volume_id(inode, user_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 default:
133 return -ENOTTY; /* Inappropriate ioctl for device */
134 }
135}
136
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900137#ifdef CONFIG_COMPAT
138static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
139 unsigned long arg)
140
141{
142 return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
143}
144#endif
145
Chris Masonae78bf92006-09-29 02:00:03 -0700146static int fat_file_release(struct inode *inode, struct file *filp)
147{
148 if ((filp->f_mode & FMODE_WRITE) &&
149 MSDOS_SB(inode->i_sb)->options.flush) {
150 fat_flush_inodes(inode->i_sb, inode, NULL);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200151 congestion_wait(BLK_RW_ASYNC, HZ/10);
Chris Masonae78bf92006-09-29 02:00:03 -0700152 }
153 return 0;
154}
155
Josef Bacik02c24a82011-07-16 20:44:56 -0400156int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Al Virob5224122009-06-07 13:44:36 -0400157{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200158 struct inode *inode = filp->f_mapping->host;
Al Virob5224122009-06-07 13:44:36 -0400159 int res, err;
160
Josef Bacik02c24a82011-07-16 20:44:56 -0400161 res = generic_file_fsync(filp, start, end, datasync);
Al Virob5224122009-06-07 13:44:36 -0400162 err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
163
164 return res ? res : err;
165}
166
167
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800168const struct file_operations fat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400170 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400171 .write_iter = generic_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .mmap = generic_file_mmap,
Chris Masonae78bf92006-09-29 02:00:03 -0700173 .release = fat_file_release,
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900174 .unlocked_ioctl = fat_generic_ioctl,
175#ifdef CONFIG_COMPAT
176 .compat_ioctl = fat_generic_compat_ioctl,
177#endif
Al Virob5224122009-06-07 13:44:36 -0400178 .fsync = fat_file_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200179 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800182static int fat_cont_expand(struct inode *inode, loff_t size)
183{
184 struct address_space *mapping = inode->i_mapping;
185 loff_t start = inode->i_size, count = size - inode->i_size;
186 int err;
187
188 err = generic_cont_expand_simple(inode, size);
189 if (err)
190 goto out;
191
192 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
193 mark_inode_dirty(inode);
Jan Kara2f3d6752009-08-17 17:00:02 +0200194 if (IS_SYNC(inode)) {
195 int err2;
196
197 /*
198 * Opencode syncing since we don't have a file open to use
199 * standard fsync path.
200 */
201 err = filemap_fdatawrite_range(mapping, start,
202 start + count - 1);
203 err2 = sync_mapping_buffers(mapping);
204 if (!err)
205 err = err2;
206 err2 = write_inode_now(inode, 1);
207 if (!err)
208 err = err2;
209 if (!err) {
210 err = filemap_fdatawait_range(mapping, start,
211 start + count - 1);
212 }
213 }
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800214out:
215 return err;
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* Free all clusters after the skip'th cluster. */
219static 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
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800227 fat_cache_inval_inode(inode);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 wait = IS_DIRSYNC(inode);
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800230 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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 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) {
Denis Karpov85c78592009-06-04 02:34:22 +0900267 fat_fs_error(sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 "%s: invalid cluster chain (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700269 __func__, MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000288void fat_truncate_blocks(struct inode *inode, loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Jonathan Corbet9c206162008-05-29 17:14:05 -0600290 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 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 */
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000298 if (MSDOS_I(inode)->mmu_private > offset)
299 MSDOS_I(inode)->mmu_private = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000301 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 fat_free(inode, nr_clusters);
Chris Masonae78bf92006-09-29 02:00:03 -0700304 fat_flush_inodes(inode->i_sb, inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800307int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
308{
David Howells2b0143b2015-03-17 22:25:59 +0000309 struct inode *inode = d_inode(dentry);
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800310 generic_fillattr(inode, stat);
311 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
Namjae Jeonea3983a2013-04-29 16:21:11 -0700312
313 if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
314 /* Use i_pos for ino. This is used as fileid of nfs. */
315 stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
316 }
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800317 return 0;
318}
319EXPORT_SYMBOL_GPL(fat_getattr);
320
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700321static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
322 struct inode *inode, umode_t *mode_ptr)
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700323{
Al Virodacd0e72011-07-26 03:21:30 -0400324 umode_t mask, perm;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700325
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700326 /*
327 * Note, the basic check is already done by a caller of
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800328 * (attr->ia_mode & ~FAT_VALID_MODE)
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700329 */
330
331 if (S_ISREG(inode->i_mode))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700332 mask = sbi->options.fs_fmask;
333 else
334 mask = sbi->options.fs_dmask;
335
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700336 perm = *mode_ptr & ~(S_IFMT | mask);
337
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700338 /*
339 * Of the r and x bits, all (subject to umask) must be present. Of the
340 * w bits, either all (subject to umask) or none must be present.
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800341 *
342 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700343 */
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700344 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700345 return -EPERM;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800346 if (fat_mode_can_hold_ro(inode)) {
347 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
348 return -EPERM;
349 } else {
350 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
351 return -EPERM;
352 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700353
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700354 *mode_ptr &= S_IFMT | perm;
355
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700356 return 0;
357}
358
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700359static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
360{
Al Virodacd0e72011-07-26 03:21:30 -0400361 umode_t allow_utime = sbi->options.allow_utime;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700362
Eric W. Biederman170782e2012-02-07 16:25:39 -0800363 if (!uid_eq(current_fsuid(), inode->i_uid)) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700364 if (in_group_p(inode->i_gid))
365 allow_utime >>= 3;
366 if (allow_utime & MAY_WRITE)
367 return 1;
368 }
369
370 /* use a default check */
371 return 0;
372}
373
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900374#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800375/* valid file mode bits */
376#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900377
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700378int fat_setattr(struct dentry *dentry, struct iattr *attr)
379{
380 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000381 struct inode *inode = d_inode(dentry);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700382 unsigned int ia_valid;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800383 int error;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700384
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700385 /* Check for setting the inode time. */
386 ia_valid = attr->ia_valid;
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900387 if (ia_valid & TIMES_SET_FLAGS) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700388 if (fat_allow_set_time(sbi, inode))
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900389 attr->ia_valid &= ~TIMES_SET_FLAGS;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700390 }
391
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700392 error = inode_change_ok(inode, attr);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700393 attr->ia_valid = ia_valid;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700394 if (error) {
395 if (sbi->options.quiet)
396 error = 0;
397 goto out;
398 }
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700399
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200400 /*
401 * Expand the file. Since inode_setattr() updates ->i_size
402 * before calling the ->truncate(), but FAT needs to fill the
403 * hole before it. XXX: this is no longer true with new truncate
404 * sequence.
405 */
406 if (attr->ia_valid & ATTR_SIZE) {
Christoph Hellwig562c72a2011-06-24 14:29:45 -0400407 inode_dio_wait(inode);
408
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200409 if (attr->ia_size > inode->i_size) {
410 error = fat_cont_expand(inode, attr->ia_size);
411 if (error || attr->ia_valid == ATTR_SIZE)
412 goto out;
413 attr->ia_valid &= ~ATTR_SIZE;
414 }
415 }
416
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700417 if (((attr->ia_valid & ATTR_UID) &&
Eric W. Biederman170782e2012-02-07 16:25:39 -0800418 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700419 ((attr->ia_valid & ATTR_GID) &&
Eric W. Biederman170782e2012-02-07 16:25:39 -0800420 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
OGAWA Hirofumie97e8de2008-04-28 02:16:26 -0700421 ((attr->ia_valid & ATTR_MODE) &&
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800422 (attr->ia_mode & ~FAT_VALID_MODE)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700423 error = -EPERM;
424
425 if (error) {
426 if (sbi->options.quiet)
427 error = 0;
428 goto out;
429 }
430
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700431 /*
432 * We don't return -EPERM here. Yes, strange, but this is too
433 * old behavior.
434 */
435 if (attr->ia_valid & ATTR_MODE) {
436 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
437 attr->ia_valid &= ~ATTR_MODE;
438 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700439
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000440 if (attr->ia_valid & ATTR_SIZE) {
Namjae Jeonc0ef0cc2014-12-12 16:57:26 -0800441 error = fat_block_truncate_page(inode, attr->ia_size);
442 if (error)
443 goto out;
Christoph Hellwig58268692011-06-24 14:29:40 -0400444 down_write(&MSDOS_I(inode)->truncate_lock);
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200445 truncate_setsize(inode, attr->ia_size);
446 fat_truncate_blocks(inode, attr->ia_size);
Christoph Hellwig58268692011-06-24 14:29:40 -0400447 up_write(&MSDOS_I(inode)->truncate_lock);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000448 }
449
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200450 setattr_copy(inode, attr);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000451 mark_inode_dirty(inode);
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700452out:
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700453 return error;
454}
455EXPORT_SYMBOL_GPL(fat_setattr);
456
Arjan van de Ven754661f2007-02-12 00:55:38 -0800457const struct inode_operations fat_file_inode_operations = {
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700458 .setattr = fat_setattr,
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800459 .getattr = fat_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460};