[PATCH] NFSv4: Add {get,set,list}xattr methods for nfs4
[wrt350n-kernel.git] / fs / fat / file.c
blob62ffa91394008e9e0af8d014d10a71108767d451
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/module.h>
10 #include <linux/time.h>
11 #include <linux/msdos_fs.h>
12 #include <linux/smp_lock.h>
13 #include <linux/buffer_head.h>
15 static ssize_t fat_file_aio_write(struct kiocb *iocb, const char __user *buf,
16 size_t count, loff_t pos)
18 struct inode *inode = iocb->ki_filp->f_dentry->d_inode;
19 int retval;
21 retval = generic_file_aio_write(iocb, buf, count, pos);
22 if (retval > 0) {
23 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
24 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
25 mark_inode_dirty(inode);
26 // check the locking rules
27 // if (IS_SYNC(inode))
28 // fat_sync_inode(inode);
30 return retval;
33 static ssize_t fat_file_writev(struct file *filp, const struct iovec *iov,
34 unsigned long nr_segs, loff_t *ppos)
36 struct inode *inode = filp->f_dentry->d_inode;
37 int retval;
39 retval = generic_file_writev(filp, iov, nr_segs, ppos);
40 if (retval > 0) {
41 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
42 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
43 mark_inode_dirty(inode);
45 return retval;
48 int fat_generic_ioctl(struct inode *inode, struct file *filp,
49 unsigned int cmd, unsigned long arg)
51 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
52 u32 __user *user_attr = (u32 __user *)arg;
54 switch (cmd) {
55 case FAT_IOCTL_GET_ATTRIBUTES:
57 u32 attr;
59 if (inode->i_ino == MSDOS_ROOT_INO)
60 attr = ATTR_DIR;
61 else
62 attr = fat_attr(inode);
64 return put_user(attr, user_attr);
66 case FAT_IOCTL_SET_ATTRIBUTES:
68 u32 attr, oldattr;
69 int err, is_dir = S_ISDIR(inode->i_mode);
70 struct iattr ia;
72 err = get_user(attr, user_attr);
73 if (err)
74 return err;
76 down(&inode->i_sem);
78 if (IS_RDONLY(inode)) {
79 err = -EROFS;
80 goto up;
84 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
85 * prevents the user from turning us into a VFAT
86 * longname entry. Also, we obviously can't set
87 * any of the NTFS attributes in the high 24 bits.
89 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
90 /* Merge in ATTR_VOLUME and ATTR_DIR */
91 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
92 (is_dir ? ATTR_DIR : 0);
93 oldattr = fat_attr(inode);
95 /* Equivalent to a chmod() */
96 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
97 if (is_dir) {
98 ia.ia_mode = MSDOS_MKMODE(attr,
99 S_IRWXUGO & ~sbi->options.fs_dmask)
100 | S_IFDIR;
101 } else {
102 ia.ia_mode = MSDOS_MKMODE(attr,
103 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
104 & ~sbi->options.fs_fmask)
105 | S_IFREG;
108 /* The root directory has no attributes */
109 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
110 err = -EINVAL;
111 goto up;
114 if (sbi->options.sys_immutable) {
115 if ((attr | oldattr) & ATTR_SYS) {
116 if (!capable(CAP_LINUX_IMMUTABLE)) {
117 err = -EPERM;
118 goto up;
123 /* This MUST be done before doing anything irreversible... */
124 err = notify_change(filp->f_dentry, &ia);
125 if (err)
126 goto up;
128 if (sbi->options.sys_immutable) {
129 if (attr & ATTR_SYS)
130 inode->i_flags |= S_IMMUTABLE;
131 else
132 inode->i_flags &= S_IMMUTABLE;
135 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
136 mark_inode_dirty(inode);
138 up(&inode->i_sem);
139 return err;
141 default:
142 return -ENOTTY; /* Inappropriate ioctl for device */
146 struct file_operations fat_file_operations = {
147 .llseek = generic_file_llseek,
148 .read = do_sync_read,
149 .write = do_sync_write,
150 .readv = generic_file_readv,
151 .writev = fat_file_writev,
152 .aio_read = generic_file_aio_read,
153 .aio_write = fat_file_aio_write,
154 .mmap = generic_file_mmap,
155 .ioctl = fat_generic_ioctl,
156 .fsync = file_fsync,
157 .sendfile = generic_file_sendfile,
160 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
162 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
163 struct inode *inode = dentry->d_inode;
164 int mask, error = 0;
166 lock_kernel();
168 /* FAT cannot truncate to a longer file */
169 if (attr->ia_valid & ATTR_SIZE) {
170 if (attr->ia_size > inode->i_size) {
171 error = -EPERM;
172 goto out;
176 error = inode_change_ok(inode, attr);
177 if (error) {
178 if (sbi->options.quiet)
179 error = 0;
180 goto out;
182 if (((attr->ia_valid & ATTR_UID) &&
183 (attr->ia_uid != sbi->options.fs_uid)) ||
184 ((attr->ia_valid & ATTR_GID) &&
185 (attr->ia_gid != sbi->options.fs_gid)) ||
186 ((attr->ia_valid & ATTR_MODE) &&
187 (attr->ia_mode & ~MSDOS_VALID_MODE)))
188 error = -EPERM;
190 if (error) {
191 if (sbi->options.quiet)
192 error = 0;
193 goto out;
195 error = inode_setattr(inode, attr);
196 if (error)
197 goto out;
199 if (S_ISDIR(inode->i_mode))
200 mask = sbi->options.fs_dmask;
201 else
202 mask = sbi->options.fs_fmask;
203 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
204 out:
205 unlock_kernel();
206 return error;
209 EXPORT_SYMBOL(fat_notify_change);
211 /* Free all clusters after the skip'th cluster. */
212 static int fat_free(struct inode *inode, int skip)
214 struct super_block *sb = inode->i_sb;
215 int err, wait, free_start, i_start, i_logstart;
217 if (MSDOS_I(inode)->i_start == 0)
218 return 0;
221 * Write a new EOF, and get the remaining cluster chain for freeing.
223 wait = IS_DIRSYNC(inode);
224 if (skip) {
225 struct fat_entry fatent;
226 int ret, fclus, dclus;
228 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
229 if (ret < 0)
230 return ret;
231 else if (ret == FAT_ENT_EOF)
232 return 0;
234 fatent_init(&fatent);
235 ret = fat_ent_read(inode, &fatent, dclus);
236 if (ret == FAT_ENT_EOF) {
237 fatent_brelse(&fatent);
238 return 0;
239 } else if (ret == FAT_ENT_FREE) {
240 fat_fs_panic(sb,
241 "%s: invalid cluster chain (i_pos %lld)",
242 __FUNCTION__, MSDOS_I(inode)->i_pos);
243 ret = -EIO;
244 } else if (ret > 0) {
245 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
246 if (err)
247 ret = err;
249 fatent_brelse(&fatent);
250 if (ret < 0)
251 return ret;
253 free_start = ret;
254 i_start = i_logstart = 0;
255 fat_cache_inval_inode(inode);
256 } else {
257 fat_cache_inval_inode(inode);
259 i_start = free_start = MSDOS_I(inode)->i_start;
260 i_logstart = MSDOS_I(inode)->i_logstart;
261 MSDOS_I(inode)->i_start = 0;
262 MSDOS_I(inode)->i_logstart = 0;
264 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
265 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
266 if (wait) {
267 err = fat_sync_inode(inode);
268 if (err)
269 goto error;
270 } else
271 mark_inode_dirty(inode);
272 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
274 /* Freeing the remained cluster chain */
275 return fat_free_clusters(inode, free_start);
277 error:
278 if (i_start) {
279 MSDOS_I(inode)->i_start = i_start;
280 MSDOS_I(inode)->i_logstart = i_logstart;
282 return err;
285 void fat_truncate(struct inode *inode)
287 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
288 const unsigned int cluster_size = sbi->cluster_size;
289 int nr_clusters;
292 * This protects against truncating a file bigger than it was then
293 * trying to write into the hole.
295 if (MSDOS_I(inode)->mmu_private > inode->i_size)
296 MSDOS_I(inode)->mmu_private = inode->i_size;
298 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
300 lock_kernel();
301 fat_free(inode, nr_clusters);
302 unlock_kernel();
305 struct inode_operations fat_file_inode_operations = {
306 .truncate = fat_truncate,
307 .setattr = fat_notify_change,