]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/fat/file.c
Merge tag 'sound-4.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[mirror_ubuntu-bionic-kernel.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/compat.h>
12 #include <linux/mount.h>
13 #include <linux/blkdev.h>
14 #include <linux/backing-dev.h>
15 #include <linux/fsnotify.h>
16 #include <linux/security.h>
17 #include "fat.h"
18
19 static 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
30 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
31 {
32 struct inode *inode = file_inode(file);
33 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
43 err = mnt_want_write_file(file);
44 if (err)
45 goto out;
46 mutex_lock(&inode->i_mutex);
47
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;
73 goto out_unlock_inode;
74 }
75
76 if (sbi->options.sys_immutable &&
77 ((attr | oldattr) & ATTR_SYS) &&
78 !capable(CAP_LINUX_IMMUTABLE)) {
79 err = -EPERM;
80 goto out_unlock_inode;
81 }
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)
90 goto out_unlock_inode;
91
92 /* This MUST be done before doing anything irreversible... */
93 err = fat_setattr(file->f_path.dentry, &ia);
94 if (err)
95 goto out_unlock_inode;
96
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
102 inode->i_flags &= ~S_IMMUTABLE;
103 }
104
105 fat_save_attrs(inode, attr);
106 mark_inode_dirty(inode);
107 out_unlock_inode:
108 mutex_unlock(&inode->i_mutex);
109 mnt_drop_write_file(file);
110 out:
111 return err;
112 }
113
114 static 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
120 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
121 {
122 struct inode *inode = file_inode(filp);
123 u32 __user *user_attr = (u32 __user *)arg;
124
125 switch (cmd) {
126 case FAT_IOCTL_GET_ATTRIBUTES:
127 return fat_ioctl_get_attributes(inode, user_attr);
128 case FAT_IOCTL_SET_ATTRIBUTES:
129 return fat_ioctl_set_attributes(filp, user_attr);
130 case FAT_IOCTL_GET_VOLUME_ID:
131 return fat_ioctl_get_volume_id(inode, user_attr);
132 default:
133 return -ENOTTY; /* Inappropriate ioctl for device */
134 }
135 }
136
137 #ifdef CONFIG_COMPAT
138 static 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
146 static 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);
151 congestion_wait(BLK_RW_ASYNC, HZ/10);
152 }
153 return 0;
154 }
155
156 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
157 {
158 struct inode *inode = filp->f_mapping->host;
159 int res, err;
160
161 res = generic_file_fsync(filp, start, end, datasync);
162 err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
163
164 return res ? res : err;
165 }
166
167
168 const struct file_operations fat_file_operations = {
169 .llseek = generic_file_llseek,
170 .read_iter = generic_file_read_iter,
171 .write_iter = generic_file_write_iter,
172 .mmap = generic_file_mmap,
173 .release = fat_file_release,
174 .unlocked_ioctl = fat_generic_ioctl,
175 #ifdef CONFIG_COMPAT
176 .compat_ioctl = fat_generic_compat_ioctl,
177 #endif
178 .fsync = fat_file_fsync,
179 .splice_read = generic_file_splice_read,
180 };
181
182 static 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);
194 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 }
214 out:
215 return err;
216 }
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_error(sb,
268 "%s: invalid cluster chain (i_pos %lld)",
269 __func__, 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_blocks(struct inode *inode, loff_t offset)
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 > offset)
299 MSDOS_I(inode)->mmu_private = offset;
300
301 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
302
303 fat_free(inode, nr_clusters);
304 fat_flush_inodes(inode->i_sb, inode, NULL);
305 }
306
307 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
308 {
309 struct inode *inode = d_inode(dentry);
310 generic_fillattr(inode, stat);
311 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
312
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 }
317 return 0;
318 }
319 EXPORT_SYMBOL_GPL(fat_getattr);
320
321 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
322 struct inode *inode, umode_t *mode_ptr)
323 {
324 umode_t mask, perm;
325
326 /*
327 * Note, the basic check is already done by a caller of
328 * (attr->ia_mode & ~FAT_VALID_MODE)
329 */
330
331 if (S_ISREG(inode->i_mode))
332 mask = sbi->options.fs_fmask;
333 else
334 mask = sbi->options.fs_dmask;
335
336 perm = *mode_ptr & ~(S_IFMT | mask);
337
338 /*
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.
341 *
342 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
343 */
344 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
345 return -EPERM;
346 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 }
353
354 *mode_ptr &= S_IFMT | perm;
355
356 return 0;
357 }
358
359 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
360 {
361 umode_t allow_utime = sbi->options.allow_utime;
362
363 if (!uid_eq(current_fsuid(), inode->i_uid)) {
364 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
374 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
375 /* valid file mode bits */
376 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
377
378 int fat_setattr(struct dentry *dentry, struct iattr *attr)
379 {
380 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
381 struct inode *inode = d_inode(dentry);
382 unsigned int ia_valid;
383 int error;
384
385 /* Check for setting the inode time. */
386 ia_valid = attr->ia_valid;
387 if (ia_valid & TIMES_SET_FLAGS) {
388 if (fat_allow_set_time(sbi, inode))
389 attr->ia_valid &= ~TIMES_SET_FLAGS;
390 }
391
392 error = inode_change_ok(inode, attr);
393 attr->ia_valid = ia_valid;
394 if (error) {
395 if (sbi->options.quiet)
396 error = 0;
397 goto out;
398 }
399
400 /*
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) {
407 inode_dio_wait(inode);
408
409 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
417 if (((attr->ia_valid & ATTR_UID) &&
418 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
419 ((attr->ia_valid & ATTR_GID) &&
420 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
421 ((attr->ia_valid & ATTR_MODE) &&
422 (attr->ia_mode & ~FAT_VALID_MODE)))
423 error = -EPERM;
424
425 if (error) {
426 if (sbi->options.quiet)
427 error = 0;
428 goto out;
429 }
430
431 /*
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 }
439
440 if (attr->ia_valid & ATTR_SIZE) {
441 error = fat_block_truncate_page(inode, attr->ia_size);
442 if (error)
443 goto out;
444 down_write(&MSDOS_I(inode)->truncate_lock);
445 truncate_setsize(inode, attr->ia_size);
446 fat_truncate_blocks(inode, attr->ia_size);
447 up_write(&MSDOS_I(inode)->truncate_lock);
448 }
449
450 setattr_copy(inode, attr);
451 mark_inode_dirty(inode);
452 out:
453 return error;
454 }
455 EXPORT_SYMBOL_GPL(fat_setattr);
456
457 const struct inode_operations fat_file_inode_operations = {
458 .setattr = fat_setattr,
459 .getattr = fat_getattr,
460 };