Line data Source code
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/mount.h>
12 : #include <linux/time.h>
13 : #include <linux/buffer_head.h>
14 : #include <linux/writeback.h>
15 : #include <linux/backing-dev.h>
16 : #include <linux/blkdev.h>
17 : #include <linux/fsnotify.h>
18 : #include <linux/security.h>
19 : #include "fat.h"
20 :
21 : static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
22 : {
23 2 : u32 attr;
24 2 :
25 4 : mutex_lock(&inode->i_mutex);
26 8 : attr = fat_make_attrs(inode);
27 2 : mutex_unlock(&inode->i_mutex);
28 :
29 20 : return put_user(attr, user_attr);
30 2 : }
31 :
32 : static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
33 : {
34 4 : struct inode *inode = file->f_path.dentry->d_inode;
35 8 : struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
36 6 : int is_dir = S_ISDIR(inode->i_mode);
37 2 : u32 attr, oldattr;
38 2 : struct iattr ia;
39 2 : int err;
40 2 :
41 22 : err = get_user(attr, user_attr);
42 8 : if (err)
43 4 : goto out;
44 2 :
45 4 : mutex_lock(&inode->i_mutex);
46 4 : err = mnt_want_write(file->f_path.mnt);
47 6 : if (err)
48 4 : 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 2 : attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
57 : /* Merge in ATTR_VOLUME and ATTR_DIR */
58 16 : attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
59 : (is_dir ? ATTR_DIR : 0);
60 6 : oldattr = fat_make_attrs(inode);
61 :
62 : /* Equivalent to a chmod() */
63 2 : ia.ia_valid = ATTR_MODE | ATTR_CTIME;
64 2 : ia.ia_ctime = current_fs_time(inode->i_sb);
65 4 : if (is_dir)
66 6 : ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
67 : else {
68 8 : 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 16 : if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
74 4 : err = -EINVAL;
75 4 : goto out_drop_write;
76 : }
77 :
78 28 : if (sbi->options.sys_immutable &&
79 : ((attr | oldattr) & ATTR_SYS) &&
80 : !capable(CAP_LINUX_IMMUTABLE)) {
81 4 : err = -EPERM;
82 4 : 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 8 : err = security_inode_setattr(file->f_path.dentry, &ia);
91 4 : if (err)
92 2 : goto out_drop_write;
93 :
94 : /* This MUST be done before doing anything irreversible... */
95 10 : err = fat_setattr(file->f_path.dentry, &ia);
96 4 : if (err)
97 2 : goto out_drop_write;
98 :
99 6 : fsnotify_change(file->f_path.dentry, ia.ia_valid);
100 4 : if (sbi->options.sys_immutable) {
101 4 : if (attr & ATTR_SYS)
102 2 : inode->i_flags |= S_IMMUTABLE;
103 : else
104 2 : inode->i_flags &= ~S_IMMUTABLE;
105 : }
106 :
107 6 : fat_save_attrs(inode, attr);
108 4 : mark_inode_dirty(inode);
109 : out_drop_write:
110 12 : mnt_drop_write(file->f_path.mnt);
111 : out_unlock_inode:
112 22 : mutex_unlock(&inode->i_mutex);
113 : out:
114 24 : return err;
115 : }
116 :
117 : int fat_generic_ioctl(struct inode *inode, struct file *filp,
118 : unsigned int cmd, unsigned long arg)
119 : {
120 4 : u32 __user *user_attr = (u32 __user *)arg;
121 2 :
122 2 : switch (cmd) {
123 6 : case FAT_IOCTL_GET_ATTRIBUTES:
124 6 : return fat_ioctl_get_attributes(inode, user_attr);
125 8 : case FAT_IOCTL_SET_ATTRIBUTES:
126 16 : return fat_ioctl_set_attributes(filp, user_attr);
127 4 : default:
128 4 : return -ENOTTY; /* Inappropriate ioctl for device */
129 : }
130 : }
131 :
132 : static int fat_file_release(struct inode *inode, struct file *filp)
133 : {
134 7 : if ((filp->f_mode & FMODE_WRITE) &&
135 : MSDOS_SB(inode->i_sb)->options.flush) {
136 4 : fat_flush_inodes(inode->i_sb, inode, NULL);
137 1 : congestion_wait(BLK_RW_ASYNC, HZ/10);
138 : }
139 3 : return 0;
140 : }
141 :
142 : int fat_file_fsync(struct file *filp, struct dentry *dentry, int datasync)
143 : {
144 4 : struct inode *inode = dentry->d_inode;
145 2 : int res, err;
146 2 :
147 4 : res = simple_fsync(filp, dentry, datasync);
148 6 : err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
149 :
150 12 : return res ? res : err;
151 : }
152 :
153 :
154 1 : const struct file_operations fat_file_operations = {
155 : .llseek = generic_file_llseek,
156 : .read = do_sync_read,
157 : .write = do_sync_write,
158 : .aio_read = generic_file_aio_read,
159 : .aio_write = generic_file_aio_write,
160 : .mmap = generic_file_mmap,
161 : .release = fat_file_release,
162 : .ioctl = fat_generic_ioctl,
163 : .fsync = fat_file_fsync,
164 : .splice_read = generic_file_splice_read,
165 : };
166 :
167 : static int fat_cont_expand(struct inode *inode, loff_t size)
168 : {
169 6 : struct address_space *mapping = inode->i_mapping;
170 9 : loff_t start = inode->i_size, count = size - inode->i_size;
171 3 : int err;
172 3 :
173 6 : err = generic_cont_expand_simple(inode, size);
174 9 : if (err)
175 6 : goto out;
176 3 :
177 18 : inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
178 6 : mark_inode_dirty(inode);
179 15 : if (IS_SYNC(inode)) {
180 : int err2;
181 :
182 : /*
183 : * Opencode syncing since we don't have a file open to use
184 : * standard fsync path.
185 : */
186 3 : err = filemap_fdatawrite_range(mapping, start,
187 : start + count - 1);
188 3 : err2 = sync_mapping_buffers(mapping);
189 6 : if (!err)
190 3 : err = err2;
191 3 : err2 = write_inode_now(inode, 1);
192 6 : if (!err)
193 3 : err = err2;
194 6 : if (!err) {
195 3 : err = filemap_fdatawait_range(mapping, start,
196 : start + count - 1);
197 : }
198 : }
199 : out:
200 6 : return err;
201 : }
202 :
203 3 : /* Free all clusters after the skip'th cluster. */
204 : static int fat_free(struct inode *inode, int skip)
205 : {
206 8 : struct super_block *sb = inode->i_sb;
207 4 : int err, wait, free_start, i_start, i_logstart;
208 4 :
209 20 : if (MSDOS_I(inode)->i_start == 0)
210 8 : return 0;
211 4 :
212 12 : fat_cache_inval_inode(inode);
213 4 :
214 28 : wait = IS_DIRSYNC(inode);
215 20 : i_start = free_start = MSDOS_I(inode)->i_start;
216 16 : i_logstart = MSDOS_I(inode)->i_logstart;
217 4 :
218 4 : /* First, we write the new file size. */
219 12 : if (!skip) {
220 16 : MSDOS_I(inode)->i_start = 0;
221 16 : MSDOS_I(inode)->i_logstart = 0;
222 4 : }
223 24 : MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
224 28 : inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
225 12 : if (wait) {
226 12 : err = fat_sync_inode(inode);
227 12 : if (err) {
228 16 : MSDOS_I(inode)->i_start = i_start;
229 16 : MSDOS_I(inode)->i_logstart = i_logstart;
230 4 : return err;
231 : }
232 : } else
233 8 : mark_inode_dirty(inode);
234 :
235 : /* Write a new EOF, and get the remaining cluster chain for freeing. */
236 16 : if (skip) {
237 : struct fat_entry fatent;
238 : int ret, fclus, dclus;
239 :
240 24 : ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
241 8 : if (ret < 0)
242 4 : return ret;
243 8 : else if (ret == FAT_ENT_EOF)
244 4 : return 0;
245 :
246 8 : fatent_init(&fatent);
247 16 : ret = fat_ent_read(inode, &fatent, dclus);
248 8 : if (ret == FAT_ENT_EOF) {
249 8 : fatent_brelse(&fatent);
250 4 : return 0;
251 8 : } else if (ret == FAT_ENT_FREE) {
252 16 : fat_fs_error(sb,
253 : "%s: invalid cluster chain (i_pos %lld)",
254 : __func__, MSDOS_I(inode)->i_pos);
255 4 : ret = -EIO;
256 8 : } else if (ret > 0) {
257 12 : err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
258 8 : if (err)
259 4 : ret = err;
260 : }
261 24 : fatent_brelse(&fatent);
262 8 : if (ret < 0)
263 4 : return ret;
264 :
265 4 : free_start = ret;
266 : }
267 32 : inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
268 :
269 : /* Freeing the remained cluster chain */
270 12 : return fat_free_clusters(inode, free_start);
271 : }
272 :
273 : void fat_truncate(struct inode *inode)
274 : {
275 8 : struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
276 4 : const unsigned int cluster_size = sbi->cluster_size;
277 2 : int nr_clusters;
278 2 :
279 2 : /*
280 2 : * This protects against truncating a file bigger than it was then
281 : * trying to write into the hole.
282 : */
283 8 : if (MSDOS_I(inode)->mmu_private > inode->i_size)
284 6 : MSDOS_I(inode)->mmu_private = inode->i_size;
285 :
286 8 : nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
287 :
288 28 : fat_free(inode, nr_clusters);
289 8 : fat_flush_inodes(inode->i_sb, inode, NULL);
290 2 : }
291 :
292 : int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
293 : {
294 2 : struct inode *inode = dentry->d_inode;
295 2 : generic_fillattr(inode, stat);
296 4 : stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
297 1 : return 0;
298 : }
299 : EXPORT_SYMBOL_GPL(fat_getattr);
300 :
301 : static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
302 : struct inode *inode, umode_t *mode_ptr)
303 : {
304 9 : mode_t mask, perm;
305 9 :
306 9 : /*
307 : * Note, the basic check is already done by a caller of
308 : * (attr->ia_mode & ~FAT_VALID_MODE)
309 : */
310 :
311 27 : if (S_ISREG(inode->i_mode))
312 9 : mask = sbi->options.fs_fmask;
313 : else
314 9 : mask = sbi->options.fs_dmask;
315 :
316 9 : perm = *mode_ptr & ~(S_IFMT | mask);
317 :
318 : /*
319 : * Of the r and x bits, all (subject to umask) must be present. Of the
320 : * w bits, either all (subject to umask) or none must be present.
321 : *
322 : * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
323 : */
324 27 : if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
325 9 : return -EPERM;
326 36 : if (fat_mode_can_hold_ro(inode)) {
327 36 : if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
328 9 : return -EPERM;
329 : } else {
330 18 : if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
331 9 : return -EPERM;
332 : }
333 :
334 9 : *mode_ptr &= S_IFMT | perm;
335 :
336 9 : return 0;
337 : }
338 :
339 : static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
340 : {
341 12 : mode_t allow_utime = sbi->options.allow_utime;
342 6 :
343 36 : if (current_fsuid() != inode->i_uid) {
344 18 : if (in_group_p(inode->i_gid))
345 6 : allow_utime >>= 3;
346 12 : if (allow_utime & MAY_WRITE)
347 6 : return 1;
348 : }
349 :
350 : /* use a default check */
351 6 : return 0;
352 : }
353 :
354 : #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
355 : /* valid file mode bits */
356 : #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
357 :
358 : int fat_setattr(struct dentry *dentry, struct iattr *attr)
359 : {
360 12 : struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
361 6 : struct inode *inode = dentry->d_inode;
362 3 : unsigned int ia_valid;
363 3 : int error;
364 3 :
365 3 : /*
366 3 : * Expand the file. Since inode_setattr() updates ->i_size
367 : * before calling the ->truncate(), but FAT needs to fill the
368 : * hole before it.
369 : */
370 6 : if (attr->ia_valid & ATTR_SIZE) {
371 6 : if (attr->ia_size > inode->i_size) {
372 9 : error = fat_cont_expand(inode, attr->ia_size);
373 12 : if (error || attr->ia_valid == ATTR_SIZE)
374 3 : goto out;
375 3 : attr->ia_valid &= ~ATTR_SIZE;
376 : }
377 : }
378 :
379 : /* Check for setting the inode time. */
380 6 : ia_valid = attr->ia_valid;
381 12 : if (ia_valid & TIMES_SET_FLAGS) {
382 18 : if (fat_allow_set_time(sbi, inode))
383 3 : attr->ia_valid &= ~TIMES_SET_FLAGS;
384 : }
385 :
386 9 : error = inode_change_ok(inode, attr);
387 9 : attr->ia_valid = ia_valid;
388 18 : if (error) {
389 18 : if (sbi->options.quiet)
390 9 : error = 0;
391 9 : goto out;
392 : }
393 :
394 126 : if (((attr->ia_valid & ATTR_UID) &&
395 : (attr->ia_uid != sbi->options.fs_uid)) ||
396 : ((attr->ia_valid & ATTR_GID) &&
397 : (attr->ia_gid != sbi->options.fs_gid)) ||
398 : ((attr->ia_valid & ATTR_MODE) &&
399 : (attr->ia_mode & ~FAT_VALID_MODE)))
400 9 : error = -EPERM;
401 :
402 18 : if (error) {
403 18 : if (sbi->options.quiet)
404 9 : error = 0;
405 9 : goto out;
406 : }
407 :
408 : /*
409 : * We don't return -EPERM here. Yes, strange, but this is too
410 : * old behavior.
411 : */
412 27 : if (attr->ia_valid & ATTR_MODE) {
413 33 : if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
414 3 : attr->ia_valid &= ~ATTR_MODE;
415 : }
416 :
417 24 : if (attr->ia_valid)
418 12 : error = inode_setattr(inode, attr);
419 : out:
420 12 : return error;
421 : }
422 : EXPORT_SYMBOL_GPL(fat_setattr);
423 12 :
424 1 : const struct inode_operations fat_file_inode_operations = {
425 : .truncate = fat_truncate,
426 : .setattr = fat_setattr,
427 : .getattr = fat_getattr,
428 : };
|