]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
16f7e0fe | 9 | #include <linux/capability.h> |
1da177e4 | 10 | #include <linux/module.h> |
42a74f20 | 11 | #include <linux/mount.h> |
1da177e4 LT |
12 | #include <linux/time.h> |
13 | #include <linux/msdos_fs.h> | |
14 | #include <linux/smp_lock.h> | |
15 | #include <linux/buffer_head.h> | |
05eb0b51 | 16 | #include <linux/writeback.h> |
3fcfab16 | 17 | #include <linux/backing-dev.h> |
ae78bf9c | 18 | #include <linux/blkdev.h> |
1da177e4 | 19 | |
1da177e4 LT |
20 | int 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 | ||
1b1dcc1b | 48 | mutex_lock(&inode->i_mutex); |
1da177e4 | 49 | |
42a74f20 DH |
50 | err = mnt_want_write(filp->f_path.mnt); |
51 | if (err) | |
52 | goto up_no_drop_write; | |
1da177e4 LT |
53 | |
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... */ | |
dba32306 | 95 | err = notify_change(filp->f_path.dentry, &ia); |
1da177e4 LT |
96 | 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); | |
42a74f20 DH |
108 | up: |
109 | mnt_drop_write(filp->f_path.mnt); | |
110 | up_no_drop_write: | |
1b1dcc1b | 111 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
112 | return err; |
113 | } | |
114 | default: | |
115 | return -ENOTTY; /* Inappropriate ioctl for device */ | |
116 | } | |
117 | } | |
118 | ||
ae78bf9c CM |
119 | static 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); | |
3fcfab16 | 124 | congestion_wait(WRITE, HZ/10); |
ae78bf9c CM |
125 | } |
126 | return 0; | |
127 | } | |
128 | ||
4b6f5d20 | 129 | const struct file_operations fat_file_operations = { |
1da177e4 LT |
130 | .llseek = generic_file_llseek, |
131 | .read = do_sync_read, | |
132 | .write = do_sync_write, | |
1da177e4 | 133 | .aio_read = generic_file_aio_read, |
ef402268 | 134 | .aio_write = generic_file_aio_write, |
1da177e4 | 135 | .mmap = generic_file_mmap, |
ae78bf9c | 136 | .release = fat_file_release, |
1da177e4 LT |
137 | .ioctl = fat_generic_ioctl, |
138 | .fsync = file_fsync, | |
5ffc4ef4 | 139 | .splice_read = generic_file_splice_read, |
1da177e4 LT |
140 | }; |
141 | ||
05eb0b51 OH |
142 | static 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); | |
156 | out: | |
157 | return err; | |
158 | } | |
159 | ||
1da177e4 LT |
160 | /* Free all clusters after the skip'th cluster. */ |
161 | static 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 | ||
3b641407 OH |
169 | fat_cache_inval_inode(inode); |
170 | ||
1da177e4 | 171 | wait = IS_DIRSYNC(inode); |
3b641407 OH |
172 | 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. */ | |
1da177e4 LT |
193 | 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)", | |
8e24eea7 | 211 | __func__, MSDOS_I(inode)->i_pos); |
1da177e4 LT |
212 | 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; | |
1da177e4 | 223 | } |
1da177e4 LT |
224 | 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); | |
1da177e4 LT |
228 | } |
229 | ||
230 | void fat_truncate(struct inode *inode) | |
231 | { | |
8f593427 LT |
232 | struct super_block *sb = inode->i_sb; |
233 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | |
1da177e4 LT |
234 | const unsigned int cluster_size = sbi->cluster_size; |
235 | int nr_clusters; | |
236 | ||
237 | /* | |
238 | * This protects against truncating a file bigger than it was then | |
239 | * trying to write into the hole. | |
240 | */ | |
241 | if (MSDOS_I(inode)->mmu_private > inode->i_size) | |
242 | MSDOS_I(inode)->mmu_private = inode->i_size; | |
243 | ||
244 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; | |
245 | ||
8f593427 | 246 | lock_super(sb); |
1da177e4 | 247 | fat_free(inode, nr_clusters); |
8f593427 | 248 | unlock_super(sb); |
ae78bf9c | 249 | fat_flush_inodes(inode->i_sb, inode, NULL); |
1da177e4 LT |
250 | } |
251 | ||
da63fc7c OH |
252 | int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
253 | { | |
254 | struct inode *inode = dentry->d_inode; | |
255 | generic_fillattr(inode, stat); | |
256 | stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size; | |
257 | return 0; | |
258 | } | |
259 | EXPORT_SYMBOL_GPL(fat_getattr); | |
260 | ||
e97e8de3 OH |
261 | static int fat_check_mode(const struct msdos_sb_info *sbi, struct inode *inode, |
262 | mode_t mode) | |
1278fdd3 OH |
263 | { |
264 | mode_t mask, req = mode & ~S_IFMT; | |
265 | ||
266 | if (S_ISREG(mode)) | |
267 | mask = sbi->options.fs_fmask; | |
268 | else | |
269 | mask = sbi->options.fs_dmask; | |
270 | ||
271 | /* | |
272 | * Of the r and x bits, all (subject to umask) must be present. Of the | |
273 | * w bits, either all (subject to umask) or none must be present. | |
274 | */ | |
275 | req &= ~mask; | |
e97e8de3 | 276 | if ((req & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) |
1278fdd3 OH |
277 | return -EPERM; |
278 | if ((req & S_IWUGO) && ((req & S_IWUGO) != (S_IWUGO & ~mask))) | |
279 | return -EPERM; | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
1ae43f82 OH |
284 | static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode) |
285 | { | |
286 | mode_t allow_utime = sbi->options.allow_utime; | |
287 | ||
288 | if (current->fsuid != inode->i_uid) { | |
289 | if (in_group_p(inode->i_gid)) | |
290 | allow_utime >>= 3; | |
291 | if (allow_utime & MAY_WRITE) | |
292 | return 1; | |
293 | } | |
294 | ||
295 | /* use a default check */ | |
296 | return 0; | |
297 | } | |
298 | ||
1278fdd3 OH |
299 | int fat_setattr(struct dentry *dentry, struct iattr *attr) |
300 | { | |
8f593427 | 301 | struct super_block *sb = dentry->d_sb; |
1278fdd3 OH |
302 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); |
303 | struct inode *inode = dentry->d_inode; | |
304 | int mask, error = 0; | |
1ae43f82 | 305 | unsigned int ia_valid; |
1278fdd3 | 306 | |
8f593427 | 307 | lock_super(sb); |
1278fdd3 OH |
308 | |
309 | /* | |
310 | * Expand the file. Since inode_setattr() updates ->i_size | |
311 | * before calling the ->truncate(), but FAT needs to fill the | |
312 | * hole before it. | |
313 | */ | |
314 | if (attr->ia_valid & ATTR_SIZE) { | |
315 | if (attr->ia_size > inode->i_size) { | |
316 | error = fat_cont_expand(inode, attr->ia_size); | |
317 | if (error || attr->ia_valid == ATTR_SIZE) | |
318 | goto out; | |
319 | attr->ia_valid &= ~ATTR_SIZE; | |
320 | } | |
321 | } | |
322 | ||
1ae43f82 OH |
323 | /* Check for setting the inode time. */ |
324 | ia_valid = attr->ia_valid; | |
325 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) { | |
326 | if (fat_allow_set_time(sbi, inode)) | |
327 | attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET); | |
328 | } | |
329 | ||
1278fdd3 | 330 | error = inode_change_ok(inode, attr); |
1ae43f82 | 331 | attr->ia_valid = ia_valid; |
1278fdd3 OH |
332 | if (error) { |
333 | if (sbi->options.quiet) | |
334 | error = 0; | |
335 | goto out; | |
336 | } | |
337 | if (((attr->ia_valid & ATTR_UID) && | |
338 | (attr->ia_uid != sbi->options.fs_uid)) || | |
339 | ((attr->ia_valid & ATTR_GID) && | |
e97e8de3 OH |
340 | (attr->ia_gid != sbi->options.fs_gid)) || |
341 | ((attr->ia_valid & ATTR_MODE) && | |
342 | fat_check_mode(sbi, inode, attr->ia_mode) < 0)) | |
1278fdd3 OH |
343 | error = -EPERM; |
344 | ||
345 | if (error) { | |
346 | if (sbi->options.quiet) | |
347 | error = 0; | |
348 | goto out; | |
349 | } | |
350 | ||
1278fdd3 OH |
351 | error = inode_setattr(inode, attr); |
352 | if (error) | |
353 | goto out; | |
354 | ||
355 | if (S_ISDIR(inode->i_mode)) | |
356 | mask = sbi->options.fs_dmask; | |
357 | else | |
358 | mask = sbi->options.fs_fmask; | |
359 | inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask); | |
360 | out: | |
8f593427 | 361 | unlock_super(sb); |
1278fdd3 OH |
362 | return error; |
363 | } | |
364 | EXPORT_SYMBOL_GPL(fat_setattr); | |
365 | ||
754661f1 | 366 | const struct inode_operations fat_file_inode_operations = { |
1da177e4 | 367 | .truncate = fat_truncate, |
1278fdd3 | 368 | .setattr = fat_setattr, |
da63fc7c | 369 | .getattr = fat_getattr, |
1da177e4 | 370 | }; |