MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / fat / prince-fat / fat-2.6.17 / file.c
blob1ee25232e6af3dd4cee629fdeba312f769cd8d02
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/smp_lock.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
17 int fat_generic_ioctl(struct inode *inode, struct file *filp,
18 unsigned int cmd, unsigned long arg)
20 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
21 u32 __user *user_attr = (u32 __user *)arg;
23 switch (cmd) {
24 case FAT_IOCTL_GET_ATTRIBUTES:
26 u32 attr;
28 if (inode->i_ino == MSDOS_ROOT_INO)
29 attr = ATTR_DIR;
30 else
31 attr = fat_attr(inode);
33 return put_user(attr, user_attr);
35 case FAT_IOCTL_SET_ATTRIBUTES:
37 u32 attr, oldattr;
38 int err, is_dir = S_ISDIR(inode->i_mode);
39 struct iattr ia;
41 err = get_user(attr, user_attr);
42 if (err)
43 return err;
45 mutex_lock(&inode->i_mutex);
47 if (IS_RDONLY(inode)) {
48 err = -EROFS;
49 goto up;
53 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
54 * prevents the user from turning us into a VFAT
55 * longname entry. Also, we obviously can't set
56 * any of the NTFS attributes in the high 24 bits.
58 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
59 /* Merge in ATTR_VOLUME and ATTR_DIR */
60 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
61 (is_dir ? ATTR_DIR : 0);
62 oldattr = fat_attr(inode);
64 /* Equivalent to a chmod() */
65 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
66 if (is_dir) {
67 ia.ia_mode = MSDOS_MKMODE(attr,
68 S_IRWXUGO & ~sbi->options.fs_dmask)
69 | S_IFDIR;
70 } else {
71 ia.ia_mode = MSDOS_MKMODE(attr,
72 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
73 & ~sbi->options.fs_fmask)
74 | S_IFREG;
77 /* The root directory has no attributes */
78 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
79 err = -EINVAL;
80 goto up;
83 if (sbi->options.sys_immutable) {
84 if ((attr | oldattr) & ATTR_SYS) {
85 if (!capable(CAP_LINUX_IMMUTABLE)) {
86 err = -EPERM;
87 goto up;
92 /* This MUST be done before doing anything irreversible... */
93 err = notify_change(filp->f_dentry, &ia);
94 if (err)
95 goto up;
97 if (sbi->options.sys_immutable) {
98 if (attr & ATTR_SYS)
99 inode->i_flags |= S_IMMUTABLE;
100 else
101 inode->i_flags &= S_IMMUTABLE;
104 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
105 mark_inode_dirty(inode);
107 mutex_unlock(&inode->i_mutex);
108 return err;
110 default:
111 return -ENOTTY; /* Inappropriate ioctl for device */
115 const struct file_operations fat_file_operations = {
116 .llseek = generic_file_llseek,
117 .read = do_sync_read,
118 .write = do_sync_write,
119 .readv = generic_file_readv,
120 .writev = generic_file_writev,
121 .aio_read = generic_file_aio_read,
122 .aio_write = generic_file_aio_write,
123 .mmap = generic_file_mmap,
124 .ioctl = fat_generic_ioctl,
125 .fsync = file_fsync,
126 .sendfile = generic_file_sendfile,
129 static int fat_cont_expand(struct inode *inode, loff_t size)
131 struct address_space *mapping = inode->i_mapping;
132 loff_t start = inode->i_size, count = size - inode->i_size;
133 int err;
135 err = generic_cont_expand_simple(inode, size);
136 if (err)
137 goto out;
139 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
140 mark_inode_dirty(inode);
141 if (IS_SYNC(inode))
142 err = sync_page_range_nolock(inode, mapping, start, count);
143 out:
144 return err;
147 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
149 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
150 struct inode *inode = dentry->d_inode;
151 int mask, error = 0;
153 lock_kernel();
156 * Expand the file. Since inode_setattr() updates ->i_size
157 * before calling the ->truncate(), but FAT needs to fill the
158 * hole before it.
160 if (attr->ia_valid & ATTR_SIZE) {
161 if (attr->ia_size > inode->i_size) {
162 error = fat_cont_expand(inode, attr->ia_size);
163 if (error || attr->ia_valid == ATTR_SIZE)
164 goto out;
165 attr->ia_valid &= ~ATTR_SIZE;
169 error = inode_change_ok(inode, attr);
170 if (error) {
171 if (sbi->options.quiet)
172 error = 0;
173 goto out;
175 if (((attr->ia_valid & ATTR_UID) &&
176 (attr->ia_uid != sbi->options.fs_uid)) ||
177 ((attr->ia_valid & ATTR_GID) &&
178 (attr->ia_gid != sbi->options.fs_gid)) ||
179 ((attr->ia_valid & ATTR_MODE) &&
180 (attr->ia_mode & ~MSDOS_VALID_MODE)))
181 error = -EPERM;
183 if (error) {
184 if (sbi->options.quiet)
185 error = 0;
186 goto out;
188 error = inode_setattr(inode, attr);
189 if (error)
190 goto out;
192 if (S_ISDIR(inode->i_mode))
193 mask = sbi->options.fs_dmask;
194 else
195 mask = sbi->options.fs_fmask;
196 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
197 out:
198 unlock_kernel();
199 return error;
202 EXPORT_SYMBOL_GPL(fat_notify_change);
204 /* Free all clusters after the skip'th cluster. */
205 static int fat_free(struct inode *inode, int skip)
207 struct super_block *sb = inode->i_sb;
208 int err, wait, free_start, i_start, i_logstart;
210 if (MSDOS_I(inode)->i_start == 0)
211 return 0;
213 fat_cache_inval_inode(inode);
215 wait = IS_DIRSYNC(inode);
216 i_start = free_start = MSDOS_I(inode)->i_start;
217 i_logstart = MSDOS_I(inode)->i_logstart;
219 /* First, we write the new file size. */
220 if (!skip) {
221 MSDOS_I(inode)->i_start = 0;
222 MSDOS_I(inode)->i_logstart = 0;
224 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
225 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
226 if (wait) {
227 err = fat_sync_inode(inode);
228 if (err) {
229 MSDOS_I(inode)->i_start = i_start;
230 MSDOS_I(inode)->i_logstart = i_logstart;
231 return err;
233 } else
234 mark_inode_dirty(inode);
236 /* Write a new EOF, and get the remaining cluster chain for freeing. */
237 if (skip) {
238 struct fat_entry fatent;
239 int ret, fclus, dclus;
241 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
242 if (ret < 0)
243 return ret;
244 else if (ret == FAT_ENT_EOF)
245 return 0;
247 fatent_init(&fatent);
248 ret = fat_ent_read(inode, &fatent, dclus);
249 if (ret == FAT_ENT_EOF) {
250 fatent_brelse(&fatent);
251 return 0;
252 } else if (ret == FAT_ENT_FREE) {
253 fat_fs_panic(sb,
254 "%s: invalid cluster chain (i_pos %lld)",
255 __FUNCTION__, MSDOS_I(inode)->i_pos);
256 ret = -EIO;
257 } else if (ret > 0) {
258 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
259 if (err)
260 ret = err;
262 fatent_brelse(&fatent);
263 if (ret < 0)
264 return ret;
266 free_start = ret;
268 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
270 /* Freeing the remained cluster chain */
271 return fat_free_clusters(inode, free_start);
274 void fat_truncate(struct inode *inode)
276 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
277 const unsigned int cluster_size = sbi->cluster_size;
278 int nr_clusters;
281 * This protects against truncating a file bigger than it was then
282 * trying to write into the hole.
284 if (MSDOS_I(inode)->mmu_private > inode->i_size)
285 MSDOS_I(inode)->mmu_private = inode->i_size;
287 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
289 lock_kernel();
290 fat_free(inode, nr_clusters);
291 unlock_kernel();
294 struct inode_operations fat_file_inode_operations = {
295 .truncate = fat_truncate,
296 .setattr = fat_notify_change,