RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / fat / file.c
blob964a3432afe709001d4dbb7071c736d957da0bf4
1 /*
2 * linux/fs/fat/file.c
4 * Written 1992,1993 by Werner Almesberger
6 * regular file handling primitives for fat-based filesystems
7 */
9 #include <linux/capability.h>
10 #include <linux/module.h>
11 #include <linux/time.h>
12 #include <linux/msdos_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/writeback.h>
15 #include <linux/backing-dev.h>
16 #include <linux/blkdev.h>
18 int fat_generic_ioctl(struct inode *inode, struct file *filp,
19 unsigned int cmd, unsigned long arg)
21 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
22 u32 __user *user_attr = (u32 __user *)arg;
24 switch (cmd) {
25 case FAT_IOCTL_GET_ATTRIBUTES:
27 u32 attr;
29 if (inode->i_ino == MSDOS_ROOT_INO)
30 attr = ATTR_DIR;
31 else
32 attr = fat_attr(inode);
34 return put_user(attr, user_attr);
36 case FAT_IOCTL_SET_ATTRIBUTES:
38 u32 attr, oldattr;
39 int err, is_dir = S_ISDIR(inode->i_mode);
40 struct iattr ia;
42 err = get_user(attr, user_attr);
43 if (err)
44 return err;
46 mutex_lock(&inode->i_mutex);
48 if (IS_RDONLY(inode)) {
49 err = -EROFS;
50 goto up;
54 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
55 * prevents the user from turning us into a VFAT
56 * longname entry. Also, we obviously can't set
57 * any of the NTFS attributes in the high 24 bits.
59 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
60 /* Merge in ATTR_VOLUME and ATTR_DIR */
61 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
62 (is_dir ? ATTR_DIR : 0);
63 oldattr = fat_attr(inode);
65 /* Equivalent to a chmod() */
66 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
67 if (is_dir) {
68 ia.ia_mode = MSDOS_MKMODE(attr,
69 S_IRWXUGO & ~sbi->options.fs_dmask)
70 | S_IFDIR;
71 } else {
72 ia.ia_mode = MSDOS_MKMODE(attr,
73 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
74 & ~sbi->options.fs_fmask)
75 | S_IFREG;
78 /* The root directory has no attributes */
79 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
80 err = -EINVAL;
81 goto up;
84 if (sbi->options.sys_immutable) {
85 if ((attr | oldattr) & ATTR_SYS) {
86 if (!capable(CAP_LINUX_IMMUTABLE)) {
87 err = -EPERM;
88 goto up;
93 /* This MUST be done before doing anything irreversible... */
94 err = notify_change(filp->f_path.dentry, &ia);
95 if (err)
96 goto up;
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;
105 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
106 mark_inode_dirty(inode);
108 mutex_unlock(&inode->i_mutex);
109 return err;
111 default:
112 return -ENOTTY; /* Inappropriate ioctl for device */
116 static int fat_file_release(struct inode *inode, struct file *filp)
118 if ((filp->f_mode & FMODE_WRITE) &&
119 MSDOS_SB(inode->i_sb)->options.flush) {
120 fat_flush_inodes(inode->i_sb, inode, NULL);
121 congestion_wait(WRITE, HZ/10);
123 return 0;
126 const struct file_operations fat_file_operations = {
127 .llseek = generic_file_llseek,
128 .read = do_sync_read,
129 .write = do_sync_write,
130 .aio_read = generic_file_aio_read,
131 .aio_write = generic_file_aio_write,
132 .mmap = generic_file_mmap,
133 .release = fat_file_release,
134 .ioctl = fat_generic_ioctl,
135 .fsync = file_fsync,
136 .splice_read = generic_file_splice_read,
139 static int fat_cont_expand(struct inode *inode, loff_t size)
141 struct address_space *mapping = inode->i_mapping;
142 loff_t start = inode->i_size, count = size - inode->i_size;
143 int err;
145 err = generic_cont_expand_simple(inode, size);
146 if (err)
147 goto out;
149 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
150 mark_inode_dirty(inode);
151 if (IS_SYNC(inode))
152 err = sync_page_range_nolock(inode, mapping, start, count);
153 out:
154 return err;
157 /* Free all clusters after the skip'th cluster. */
158 static int fat_free(struct inode *inode, int skip)
160 struct super_block *sb = inode->i_sb;
161 int err, wait, free_start, i_start, i_logstart;
163 if (MSDOS_I(inode)->i_start == 0)
164 return 0;
166 fat_cache_inval_inode(inode);
168 wait = IS_DIRSYNC(inode);
169 i_start = free_start = MSDOS_I(inode)->i_start;
170 i_logstart = MSDOS_I(inode)->i_logstart;
172 /* First, we write the new file size. */
173 if (!skip) {
174 MSDOS_I(inode)->i_start = 0;
175 MSDOS_I(inode)->i_logstart = 0;
177 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
178 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
179 if (wait) {
180 err = fat_sync_inode(inode);
181 if (err) {
182 MSDOS_I(inode)->i_start = i_start;
183 MSDOS_I(inode)->i_logstart = i_logstart;
184 return err;
186 } else
187 mark_inode_dirty(inode);
189 /* Write a new EOF, and get the remaining cluster chain for freeing. */
190 if (skip) {
191 struct fat_entry fatent;
192 int ret, fclus, dclus;
194 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
195 if (ret < 0)
196 return ret;
197 else if (ret == FAT_ENT_EOF)
198 return 0;
200 fatent_init(&fatent);
201 ret = fat_ent_read(inode, &fatent, dclus);
202 if (ret == FAT_ENT_EOF) {
203 fatent_brelse(&fatent);
204 return 0;
205 } else if (ret == FAT_ENT_FREE) {
206 fat_fs_panic(sb,
207 "%s: invalid cluster chain (i_pos %lld)",
208 __FUNCTION__, MSDOS_I(inode)->i_pos);
209 ret = -EIO;
210 } else if (ret > 0) {
211 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
212 if (err)
213 ret = err;
215 fatent_brelse(&fatent);
216 if (ret < 0)
217 return ret;
219 free_start = ret;
221 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
223 /* Freeing the remained cluster chain */
224 return fat_free_clusters(inode, free_start);
227 void fat_truncate(struct inode *inode)
229 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
230 const unsigned int cluster_size = sbi->cluster_size;
231 int nr_clusters;
234 * This protects against truncating a file bigger than it was then
235 * trying to write into the hole.
237 if (MSDOS_I(inode)->mmu_private > inode->i_size)
238 MSDOS_I(inode)->mmu_private = inode->i_size;
240 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
242 fat_free(inode, nr_clusters);
243 fat_flush_inodes(inode->i_sb, inode, NULL);
246 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
248 struct inode *inode = dentry->d_inode;
249 generic_fillattr(inode, stat);
250 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
251 return 0;
253 EXPORT_SYMBOL_GPL(fat_getattr);
255 static int fat_check_mode(const struct msdos_sb_info *sbi, struct inode *inode,
256 mode_t mode)
258 mode_t mask, req = mode & ~S_IFMT;
260 if (S_ISREG(mode))
261 mask = sbi->options.fs_fmask;
262 else
263 mask = sbi->options.fs_dmask;
266 * Of the r and x bits, all (subject to umask) must be present. Of the
267 * w bits, either all (subject to umask) or none must be present.
269 req &= ~mask;
270 if ((req & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
271 return -EPERM;
272 if ((req & S_IWUGO) && ((req & S_IWUGO) != (S_IWUGO & ~mask)))
273 return -EPERM;
275 return 0;
278 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
280 mode_t allow_utime = sbi->options.allow_utime;
282 if (current->fsuid != inode->i_uid) {
283 if (in_group_p(inode->i_gid))
284 allow_utime >>= 3;
285 if (allow_utime & MAY_WRITE)
286 return 1;
289 /* use a default check */
290 return 0;
293 int fat_setattr(struct dentry *dentry, struct iattr *attr)
295 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
296 struct inode *inode = dentry->d_inode;
297 int mask, error = 0;
298 unsigned int ia_valid;
301 * Expand the file. Since inode_setattr() updates ->i_size
302 * before calling the ->truncate(), but FAT needs to fill the
303 * hole before it.
305 if (attr->ia_valid & ATTR_SIZE) {
306 if (attr->ia_size > inode->i_size) {
307 #if 1
308 /* FAT cannot truncate to a longer file */
309 error = -EPERM;
310 goto out;
311 #else
312 error = fat_cont_expand(inode, attr->ia_size);
313 if (error || attr->ia_valid == ATTR_SIZE)
314 goto out;
315 attr->ia_valid &= ~ATTR_SIZE;
316 #endif
320 /* Check for setting the inode time. */
321 ia_valid = attr->ia_valid;
322 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
323 if (fat_allow_set_time(sbi, inode))
324 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
327 error = inode_change_ok(inode, attr);
328 attr->ia_valid = ia_valid;
329 if (error) {
330 if (sbi->options.quiet)
331 error = 0;
332 goto out;
334 if (((attr->ia_valid & ATTR_UID) &&
335 (attr->ia_uid != sbi->options.fs_uid)) ||
336 ((attr->ia_valid & ATTR_GID) &&
337 (attr->ia_gid != sbi->options.fs_gid)) ||
338 ((attr->ia_valid & ATTR_MODE) &&
339 fat_check_mode(sbi, inode, attr->ia_mode) < 0))
340 error = -EPERM;
342 if (error) {
343 if (sbi->options.quiet)
344 error = 0;
345 goto out;
348 error = inode_setattr(inode, attr);
349 if (error)
350 goto out;
352 if (S_ISDIR(inode->i_mode))
353 mask = sbi->options.fs_dmask;
354 else
355 mask = sbi->options.fs_fmask;
356 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
357 out:
358 return error;
360 EXPORT_SYMBOL_GPL(fat_setattr);
362 const struct inode_operations fat_file_inode_operations = {
363 .truncate = fat_truncate,
364 .setattr = fat_setattr,
365 .getattr = fat_getattr,