blob: 771326b8047e0ff5e3483f885c1edc30a604471a [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>
13#include <linux/msdos_fs.h>
14#include <linux/smp_lock.h>
15#include <linux/buffer_head.h>
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -080016#include <linux/writeback.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070017#include <linux/backing-dev.h>
Chris Masonae78bf92006-09-29 02:00:03 -070018#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020int fat_generic_ioctl(struct inode *inode, struct file *filp,
21 unsigned int cmd, unsigned long arg)
22{
23 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
24 u32 __user *user_attr = (u32 __user *)arg;
25
26 switch (cmd) {
27 case FAT_IOCTL_GET_ATTRIBUTES:
28 {
29 u32 attr;
30
31 if (inode->i_ino == MSDOS_ROOT_INO)
32 attr = ATTR_DIR;
33 else
34 attr = fat_attr(inode);
35
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);
64 oldattr = fat_attr(inode);
65
66 /* Equivalent to a chmod() */
67 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
68 if (is_dir) {
69 ia.ia_mode = MSDOS_MKMODE(attr,
70 S_IRWXUGO & ~sbi->options.fs_dmask)
71 | S_IFDIR;
72 } else {
73 ia.ia_mode = MSDOS_MKMODE(attr,
74 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
75 & ~sbi->options.fs_fmask)
76 | S_IFREG;
77 }
78
79 /* The root directory has no attributes */
80 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
81 err = -EINVAL;
82 goto up;
83 }
84
85 if (sbi->options.sys_immutable) {
86 if ((attr | oldattr) & ATTR_SYS) {
87 if (!capable(CAP_LINUX_IMMUTABLE)) {
88 err = -EPERM;
89 goto up;
90 }
91 }
92 }
93
94 /* This MUST be done before doing anything irreversible... */
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -080095 err = notify_change(filp->f_path.dentry, &ia);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (err)
97 goto up;
98
99 if (sbi->options.sys_immutable) {
100 if (attr & ATTR_SYS)
101 inode->i_flags |= S_IMMUTABLE;
102 else
103 inode->i_flags &= S_IMMUTABLE;
104 }
105
106 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
107 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800108up:
109 mnt_drop_write(filp->f_path.mnt);
110up_no_drop_write:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800111 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return err;
113 }
114 default:
115 return -ENOTTY; /* Inappropriate ioctl for device */
116 }
117}
118
Chris Masonae78bf92006-09-29 02:00:03 -0700119static int fat_file_release(struct inode *inode, struct file *filp)
120{
121 if ((filp->f_mode & FMODE_WRITE) &&
122 MSDOS_SB(inode->i_sb)->options.flush) {
123 fat_flush_inodes(inode->i_sb, inode, NULL);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700124 congestion_wait(WRITE, HZ/10);
Chris Masonae78bf92006-09-29 02:00:03 -0700125 }
126 return 0;
127}
128
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800129const struct file_operations fat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .llseek = generic_file_llseek,
131 .read = do_sync_read,
132 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .aio_read = generic_file_aio_read,
OGAWA Hirofumief402262005-09-16 19:28:13 -0700134 .aio_write = generic_file_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .mmap = generic_file_mmap,
Chris Masonae78bf92006-09-29 02:00:03 -0700136 .release = fat_file_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .ioctl = fat_generic_ioctl,
138 .fsync = file_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200139 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
OGAWA Hirofumi05eb0b52006-01-08 01:02:13 -0800142static int fat_cont_expand(struct inode *inode, loff_t size)
143{
144 struct address_space *mapping = inode->i_mapping;
145 loff_t start = inode->i_size, count = size - inode->i_size;
146 int err;
147
148 err = generic_cont_expand_simple(inode, size);
149 if (err)
150 goto out;
151
152 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
153 mark_inode_dirty(inode);
154 if (IS_SYNC(inode))
155 err = sync_page_range_nolock(inode, mapping, start, count);
156out:
157 return err;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Free all clusters after the skip'th cluster. */
161static int fat_free(struct inode *inode, int skip)
162{
163 struct super_block *sb = inode->i_sb;
164 int err, wait, free_start, i_start, i_logstart;
165
166 if (MSDOS_I(inode)->i_start == 0)
167 return 0;
168
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800169 fat_cache_inval_inode(inode);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 wait = IS_DIRSYNC(inode);
OGAWA Hirofumi3b641402006-02-03 03:04:44 -0800172 i_start = free_start = MSDOS_I(inode)->i_start;
173 i_logstart = MSDOS_I(inode)->i_logstart;
174
175 /* First, we write the new file size. */
176 if (!skip) {
177 MSDOS_I(inode)->i_start = 0;
178 MSDOS_I(inode)->i_logstart = 0;
179 }
180 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
181 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
182 if (wait) {
183 err = fat_sync_inode(inode);
184 if (err) {
185 MSDOS_I(inode)->i_start = i_start;
186 MSDOS_I(inode)->i_logstart = i_logstart;
187 return err;
188 }
189 } else
190 mark_inode_dirty(inode);
191
192 /* Write a new EOF, and get the remaining cluster chain for freeing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (skip) {
194 struct fat_entry fatent;
195 int ret, fclus, dclus;
196
197 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
198 if (ret < 0)
199 return ret;
200 else if (ret == FAT_ENT_EOF)
201 return 0;
202
203 fatent_init(&fatent);
204 ret = fat_ent_read(inode, &fatent, dclus);
205 if (ret == FAT_ENT_EOF) {
206 fatent_brelse(&fatent);
207 return 0;
208 } else if (ret == FAT_ENT_FREE) {
209 fat_fs_panic(sb,
210 "%s: invalid cluster chain (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700211 __func__, MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ret = -EIO;
213 } else if (ret > 0) {
214 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
215 if (err)
216 ret = err;
217 }
218 fatent_brelse(&fatent);
219 if (ret < 0)
220 return ret;
221
222 free_start = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
225
226 /* Freeing the remained cluster chain */
227 return fat_free_clusters(inode, free_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230void fat_truncate(struct inode *inode)
231{
232 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
233 const unsigned int cluster_size = sbi->cluster_size;
234 int nr_clusters;
235
236 /*
237 * This protects against truncating a file bigger than it was then
238 * trying to write into the hole.
239 */
240 if (MSDOS_I(inode)->mmu_private > inode->i_size)
241 MSDOS_I(inode)->mmu_private = inode->i_size;
242
243 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
244
245 lock_kernel();
246 fat_free(inode, nr_clusters);
247 unlock_kernel();
Chris Masonae78bf92006-09-29 02:00:03 -0700248 fat_flush_inodes(inode->i_sb, inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800251int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
252{
253 struct inode *inode = dentry->d_inode;
254 generic_fillattr(inode, stat);
255 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
256 return 0;
257}
258EXPORT_SYMBOL_GPL(fat_getattr);
259
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700260static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
261 struct inode *inode, umode_t *mode_ptr)
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700262{
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700263 mode_t mask, perm;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700264
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700265 /*
266 * Note, the basic check is already done by a caller of
267 * (attr->ia_mode & ~MSDOS_VALID_MODE)
268 */
269
270 if (S_ISREG(inode->i_mode))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700271 mask = sbi->options.fs_fmask;
272 else
273 mask = sbi->options.fs_dmask;
274
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700275 perm = *mode_ptr & ~(S_IFMT | mask);
276
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700277 /*
278 * Of the r and x bits, all (subject to umask) must be present. Of the
279 * w bits, either all (subject to umask) or none must be present.
280 */
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700281 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700282 return -EPERM;
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700283 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700284 return -EPERM;
285
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700286 *mode_ptr &= S_IFMT | perm;
287
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700288 return 0;
289}
290
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700291static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
292{
293 mode_t allow_utime = sbi->options.allow_utime;
294
295 if (current->fsuid != inode->i_uid) {
296 if (in_group_p(inode->i_gid))
297 allow_utime >>= 3;
298 if (allow_utime & MAY_WRITE)
299 return 1;
300 }
301
302 /* use a default check */
303 return 0;
304}
305
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700306int fat_setattr(struct dentry *dentry, struct iattr *attr)
307{
308 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
309 struct inode *inode = dentry->d_inode;
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700310 int error = 0;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700311 unsigned int ia_valid;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700312
313 lock_kernel();
314
315 /*
316 * Expand the file. Since inode_setattr() updates ->i_size
317 * before calling the ->truncate(), but FAT needs to fill the
318 * hole before it.
319 */
320 if (attr->ia_valid & ATTR_SIZE) {
321 if (attr->ia_size > inode->i_size) {
322 error = fat_cont_expand(inode, attr->ia_size);
323 if (error || attr->ia_valid == ATTR_SIZE)
324 goto out;
325 attr->ia_valid &= ~ATTR_SIZE;
326 }
327 }
328
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700329 /* Check for setting the inode time. */
330 ia_valid = attr->ia_valid;
331 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
332 if (fat_allow_set_time(sbi, inode))
333 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
334 }
335
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700336 error = inode_change_ok(inode, attr);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700337 attr->ia_valid = ia_valid;
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700338 if (error) {
339 if (sbi->options.quiet)
340 error = 0;
341 goto out;
342 }
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700343
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700344 if (((attr->ia_valid & ATTR_UID) &&
345 (attr->ia_uid != sbi->options.fs_uid)) ||
346 ((attr->ia_valid & ATTR_GID) &&
OGAWA Hirofumie97e8de2008-04-28 02:16:26 -0700347 (attr->ia_gid != sbi->options.fs_gid)) ||
348 ((attr->ia_valid & ATTR_MODE) &&
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700349 (attr->ia_mode & ~MSDOS_VALID_MODE)))
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700350 error = -EPERM;
351
352 if (error) {
353 if (sbi->options.quiet)
354 error = 0;
355 goto out;
356 }
357
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700358 /*
359 * We don't return -EPERM here. Yes, strange, but this is too
360 * old behavior.
361 */
362 if (attr->ia_valid & ATTR_MODE) {
363 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
364 attr->ia_valid &= ~ATTR_MODE;
365 }
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700366
OGAWA Hirofumi2d518f82008-06-12 15:21:28 -0700367 error = inode_setattr(inode, attr);
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700368out:
369 unlock_kernel();
370 return error;
371}
372EXPORT_SYMBOL_GPL(fat_setattr);
373
Arjan van de Ven754661f2007-02-12 00:55:38 -0800374const struct inode_operations fat_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .truncate = fat_truncate,
OGAWA Hirofumi1278fdd2008-04-28 02:16:25 -0700376 .setattr = fat_setattr,
OGAWA Hirofumida63fc72006-11-16 01:19:28 -0800377 .getattr = fat_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};