blob: 442d50a0e33e6a37daf2c315a2a424f43ddfcdaa [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>
Miklos Szeredib1da47e2008-07-01 15:01:28 +020014#include <linux/fsnotify.h>
15#include <linux/security.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080016#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Christoph Hellwig21bea492009-06-08 21:38:31 +090018static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
19{
20 u32 attr;
21
22 mutex_lock(&inode->i_mutex);
23 attr = fat_make_attrs(inode);
24 mutex_unlock(&inode->i_mutex);
25
26 return put_user(attr, user_attr);
27}
28
29static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
30{
Al Viro496ad9a2013-01-23 17:07:38 -050031 struct inode *inode = file_inode(file);
Christoph Hellwig21bea492009-06-08 21:38:31 +090032 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
33 int is_dir = S_ISDIR(inode->i_mode);
34 u32 attr, oldattr;
35 struct iattr ia;
36 int err;
37
38 err = get_user(attr, user_attr);
39 if (err)
40 goto out;
41
Al Viroa561be72011-11-23 11:57:51 -050042 err = mnt_want_write_file(file);
Christoph Hellwig21bea492009-06-08 21:38:31 +090043 if (err)
Jan Karae24f17d2012-06-12 16:20:31 +020044 goto out;
45 mutex_lock(&inode->i_mutex);
Christoph Hellwig21bea492009-06-08 21:38:31 +090046
47 /*
48 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
49 * prevents the user from turning us into a VFAT
50 * longname entry. Also, we obviously can't set
51 * any of the NTFS attributes in the high 24 bits.
52 */
53 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
54 /* Merge in ATTR_VOLUME and ATTR_DIR */
55 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
56 (is_dir ? ATTR_DIR : 0);
57 oldattr = fat_make_attrs(inode);
58
59 /* Equivalent to a chmod() */
60 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
61 ia.ia_ctime = current_fs_time(inode->i_sb);
62 if (is_dir)
63 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
64 else {
65 ia.ia_mode = fat_make_mode(sbi, attr,
66 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
67 }
68
69 /* The root directory has no attributes */
70 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
71 err = -EINVAL;
Jan Karae24f17d2012-06-12 16:20:31 +020072 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090073 }
74
75 if (sbi->options.sys_immutable &&
76 ((attr | oldattr) & ATTR_SYS) &&
77 !capable(CAP_LINUX_IMMUTABLE)) {
78 err = -EPERM;
Jan Karae24f17d2012-06-12 16:20:31 +020079 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090080 }
81
82 /*
83 * The security check is questionable... We single
84 * out the RO attribute for checking by the security
85 * module, just because it maps to a file mode.
86 */
87 err = security_inode_setattr(file->f_path.dentry, &ia);
88 if (err)
Jan Karae24f17d2012-06-12 16:20:31 +020089 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090090
91 /* This MUST be done before doing anything irreversible... */
92 err = fat_setattr(file->f_path.dentry, &ia);
93 if (err)
Jan Karae24f17d2012-06-12 16:20:31 +020094 goto out_unlock_inode;
Christoph Hellwig21bea492009-06-08 21:38:31 +090095
96 fsnotify_change(file->f_path.dentry, ia.ia_valid);
97 if (sbi->options.sys_immutable) {
98 if (attr & ATTR_SYS)
99 inode->i_flags |= S_IMMUTABLE;
100 else
OGAWA Hirofumi1adffba2011-05-31 19:38:07 +0900101 inode->i_flags &= ~S_IMMUTABLE;
Christoph Hellwig21bea492009-06-08 21:38:31 +0900102 }
103
104 fat_save_attrs(inode, attr);
105 mark_inode_dirty(inode);
Christoph Hellwig21bea492009-06-08 21:38:31 +0900106out_unlock_inode:
107 mutex_unlock(&inode->i_mutex);
Jan Karae24f17d2012-06-12 16:20:31 +0200108 mnt_drop_write_file(file);
Christoph Hellwig21bea492009-06-08 21:38:31 +0900109out:
110 return err;
111}
112
Mike Lockwood6e5b93e2013-07-08 16:00:46 -0700113static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
114{
115 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
116 return put_user(sbi->vol_id, user_attr);
117}
118
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900119long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Al Viro496ad9a2013-01-23 17:07:38 -0500121 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 u32 __user *user_attr = (u32 __user *)arg;
123
124 switch (cmd) {
125 case FAT_IOCTL_GET_ATTRIBUTES:
Christoph Hellwig21bea492009-06-08 21:38:31 +0900126 return fat_ioctl_get_attributes(inode, user_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 case FAT_IOCTL_SET_ATTRIBUTES:
Christoph Hellwig21bea492009-06-08 21:38:31 +0900128 return fat_ioctl_set_attributes(filp, user_attr);
Mike Lockwood6e5b93e2013-07-08 16:00:46 -0700129 case FAT_IOCTL_GET_VOLUME_ID:
130 return fat_ioctl_get_volume_id(inode, user_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 default:
132 return -ENOTTY; /* Inappropriate ioctl for device */
133 }
134}
135
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900136#ifdef CONFIG_COMPAT
137static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
138 unsigned long arg)
139
140{
141 return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
142}
143#endif
144
Chris Masonae78bf92006-09-29 02:00:03 -0700145static int fat_file_release(struct inode *inode, struct file *filp)
146{
147 if ((filp->f_mode & FMODE_WRITE) &&
148 MSDOS_SB(inode->i_sb)->options.flush) {
149 fat_flush_inodes(inode->i_sb, inode, NULL);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200150 congestion_wait(BLK_RW_ASYNC, HZ/10);
Chris Masonae78bf92006-09-29 02:00:03 -0700151 }
152 return 0;
153}
154
Josef Bacik02c24a82011-07-16 20:44:56 -0400155int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Al Virob5224122009-06-07 13:44:36 -0400156{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200157 struct inode *inode = filp->f_mapping->host;
Al Virob5224122009-06-07 13:44:36 -0400158 int res, err;
159
Josef Bacik02c24a82011-07-16 20:44:56 -0400160 res = generic_file_fsync(filp, start, end, datasync);
Al Virob5224122009-06-07 13:44:36 -0400161 err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
162
163 return res ? res : err;
164}
165
166
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800167const struct file_operations fat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400169 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400170 .write_iter = generic_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .mmap = generic_file_mmap,
Chris Masonae78bf92006-09-29 02:00:03 -0700172 .release = fat_file_release,
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900173 .unlocked_ioctl = fat_generic_ioctl,
174#ifdef CONFIG_COMPAT
175 .compat_ioctl = fat_generic_compat_ioctl,
176#endif
Al Virob5224122009-06-07 13:44:36 -0400177 .fsync = fat_file_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200178 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800181static int fat_cont_expand(struct inode *inode, loff_t size)
182{
183 struct address_space *mapping = inode->i_mapping;
184 loff_t start = inode->i_size, count = size - inode->i_size;
185 int err;
186
187 err = generic_cont_expand_simple(inode, size);
188 if (err)
189 goto out;
190
191 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
192 mark_inode_dirty(inode);
Jan Kara2f3d6752009-08-17 17:00:02 +0200193 if (IS_SYNC(inode)) {
194 int err2;
195
196 /*
197 * Opencode syncing since we don't have a file open to use
198 * standard fsync path.
199 */
200 err = filemap_fdatawrite_range(mapping, start,
201 start + count - 1);
202 err2 = sync_mapping_buffers(mapping);
203 if (!err)
204 err = err2;
205 err2 = write_inode_now(inode, 1);
206 if (!err)
207 err = err2;
208 if (!err) {
209 err = filemap_fdatawait_range(mapping, start,
210 start + count - 1);
211 }
212 }
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800213out:
214 return err;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* Free all clusters after the skip'th cluster. */
218static int fat_free(struct inode *inode, int skip)
219{
220 struct super_block *sb = inode->i_sb;
221 int err, wait, free_start, i_start, i_logstart;
222
223 if (MSDOS_I(inode)->i_start == 0)
224 return 0;
225
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800226 fat_cache_inval_inode(inode);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 wait = IS_DIRSYNC(inode);
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800229 i_start = free_start = MSDOS_I(inode)->i_start;
230 i_logstart = MSDOS_I(inode)->i_logstart;
231
232 /* First, we write the new file size. */
233 if (!skip) {
234 MSDOS_I(inode)->i_start = 0;
235 MSDOS_I(inode)->i_logstart = 0;
236 }
237 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
238 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
239 if (wait) {
240 err = fat_sync_inode(inode);
241 if (err) {
242 MSDOS_I(inode)->i_start = i_start;
243 MSDOS_I(inode)->i_logstart = i_logstart;
244 return err;
245 }
246 } else
247 mark_inode_dirty(inode);
248
249 /* Write a new EOF, and get the remaining cluster chain for freeing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (skip) {
251 struct fat_entry fatent;
252 int ret, fclus, dclus;
253
254 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
255 if (ret < 0)
256 return ret;
257 else if (ret == FAT_ENT_EOF)
258 return 0;
259
260 fatent_init(&fatent);
261 ret = fat_ent_read(inode, &fatent, dclus);
262 if (ret == FAT_ENT_EOF) {
263 fatent_brelse(&fatent);
264 return 0;
265 } else if (ret == FAT_ENT_FREE) {
Denis Karpov85c78592009-06-04 02:34:22 +0900266 fat_fs_error(sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 "%s: invalid cluster chain (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700268 __func__, MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = -EIO;
270 } else if (ret > 0) {
271 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
272 if (err)
273 ret = err;
274 }
275 fatent_brelse(&fatent);
276 if (ret < 0)
277 return ret;
278
279 free_start = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
282
283 /* Freeing the remained cluster chain */
284 return fat_free_clusters(inode, free_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000287void fat_truncate_blocks(struct inode *inode, loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jonathan Corbet9c206162008-05-29 17:14:05 -0600289 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 const unsigned int cluster_size = sbi->cluster_size;
291 int nr_clusters;
292
293 /*
294 * This protects against truncating a file bigger than it was then
295 * trying to write into the hole.
296 */
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000297 if (MSDOS_I(inode)->mmu_private > offset)
298 MSDOS_I(inode)->mmu_private = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000300 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 fat_free(inode, nr_clusters);
Chris Masonae78bf92006-09-29 02:00:03 -0700303 fat_flush_inodes(inode->i_sb, inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800306int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
307{
David Howells2b0143b2015-03-17 22:25:59 +0000308 struct inode *inode = d_inode(dentry);
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800309 generic_fillattr(inode, stat);
310 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
Namjae Jeonea3983a2013-04-29 16:21:11 -0700311
312 if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
313 /* Use i_pos for ino. This is used as fileid of nfs. */
314 stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
315 }
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800316 return 0;
317}
318EXPORT_SYMBOL_GPL(fat_getattr);
319
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700320static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
321 struct inode *inode, umode_t *mode_ptr)
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700322{
Al Virodacd0e72011-07-26 03:21:30 -0400323 umode_t mask, perm;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700324
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700325 /*
326 * Note, the basic check is already done by a caller of
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800327 * (attr->ia_mode & ~FAT_VALID_MODE)
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700328 */
329
330 if (S_ISREG(inode->i_mode))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700331 mask = sbi->options.fs_fmask;
332 else
333 mask = sbi->options.fs_dmask;
334
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700335 perm = *mode_ptr & ~(S_IFMT | mask);
336
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700337 /*
338 * Of the r and x bits, all (subject to umask) must be present. Of the
339 * w bits, either all (subject to umask) or none must be present.
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800340 *
341 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700342 */
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700343 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700344 return -EPERM;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800345 if (fat_mode_can_hold_ro(inode)) {
346 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
347 return -EPERM;
348 } else {
349 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
350 return -EPERM;
351 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700352
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700353 *mode_ptr &= S_IFMT | perm;
354
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700355 return 0;
356}
357
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700358static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
359{
Al Virodacd0e72011-07-26 03:21:30 -0400360 umode_t allow_utime = sbi->options.allow_utime;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700361
Eric W. Biederman170782e2012-02-07 16:25:39 -0800362 if (!uid_eq(current_fsuid(), inode->i_uid)) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700363 if (in_group_p(inode->i_gid))
364 allow_utime >>= 3;
365 if (allow_utime & MAY_WRITE)
366 return 1;
367 }
368
369 /* use a default check */
370 return 0;
371}
372
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900373#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800374/* valid file mode bits */
375#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900376
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700377int fat_setattr(struct dentry *dentry, struct iattr *attr)
378{
379 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000380 struct inode *inode = d_inode(dentry);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700381 unsigned int ia_valid;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800382 int error;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700383
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700384 /* Check for setting the inode time. */
385 ia_valid = attr->ia_valid;
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900386 if (ia_valid & TIMES_SET_FLAGS) {
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700387 if (fat_allow_set_time(sbi, inode))
OGAWA Hirofumi17263842008-08-02 13:59:37 +0900388 attr->ia_valid &= ~TIMES_SET_FLAGS;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700389 }
390
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700391 error = inode_change_ok(inode, attr);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700392 attr->ia_valid = ia_valid;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700393 if (error) {
394 if (sbi->options.quiet)
395 error = 0;
396 goto out;
397 }
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700398
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200399 /*
400 * Expand the file. Since inode_setattr() updates ->i_size
401 * before calling the ->truncate(), but FAT needs to fill the
402 * hole before it. XXX: this is no longer true with new truncate
403 * sequence.
404 */
405 if (attr->ia_valid & ATTR_SIZE) {
Christoph Hellwig562c72a2011-06-24 14:29:45 -0400406 inode_dio_wait(inode);
407
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200408 if (attr->ia_size > inode->i_size) {
409 error = fat_cont_expand(inode, attr->ia_size);
410 if (error || attr->ia_valid == ATTR_SIZE)
411 goto out;
412 attr->ia_valid &= ~ATTR_SIZE;
413 }
414 }
415
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700416 if (((attr->ia_valid & ATTR_UID) &&
Eric W. Biederman170782e2012-02-07 16:25:39 -0800417 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700418 ((attr->ia_valid & ATTR_GID) &&
Eric W. Biederman170782e2012-02-07 16:25:39 -0800419 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
OGAWA Hirofumie97e8de2008-04-28 02:16:26 -0700420 ((attr->ia_valid & ATTR_MODE) &&
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800421 (attr->ia_mode & ~FAT_VALID_MODE)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700422 error = -EPERM;
423
424 if (error) {
425 if (sbi->options.quiet)
426 error = 0;
427 goto out;
428 }
429
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700430 /*
431 * We don't return -EPERM here. Yes, strange, but this is too
432 * old behavior.
433 */
434 if (attr->ia_valid & ATTR_MODE) {
435 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
436 attr->ia_valid &= ~ATTR_MODE;
437 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700438
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000439 if (attr->ia_valid & ATTR_SIZE) {
Namjae Jeonc0ef0cc2014-12-12 16:57:26 -0800440 error = fat_block_truncate_page(inode, attr->ia_size);
441 if (error)
442 goto out;
Christoph Hellwig58268692011-06-24 14:29:40 -0400443 down_write(&MSDOS_I(inode)->truncate_lock);
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200444 truncate_setsize(inode, attr->ia_size);
445 fat_truncate_blocks(inode, attr->ia_size);
Christoph Hellwig58268692011-06-24 14:29:40 -0400446 up_write(&MSDOS_I(inode)->truncate_lock);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000447 }
448
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200449 setattr_copy(inode, attr);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000450 mark_inode_dirty(inode);
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700451out:
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700452 return error;
453}
454EXPORT_SYMBOL_GPL(fat_setattr);
455
Arjan van de Ven754661f2007-02-12 00:55:38 -0800456const struct inode_operations fat_file_inode_operations = {
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700457 .setattr = fat_setattr,
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800458 .getattr = fat_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459};