[PATCH] fat: support a truncate() for expanding size (generic_cont_expand)
[linux-2.6/linux-2.6-openrd.git] / fs / fat / file.c
blob9b07c328a6fca012ea16b192966ae8a2d409cda2
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>
14 #include <linux/writeback.h>
16 int fat_generic_ioctl(struct inode *inode, struct file *filp,
17 unsigned int cmd, unsigned long arg)
19 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
20 u32 __user *user_attr = (u32 __user *)arg;
22 switch (cmd) {
23 case FAT_IOCTL_GET_ATTRIBUTES:
25 u32 attr;
27 if (inode->i_ino == MSDOS_ROOT_INO)
28 attr = ATTR_DIR;
29 else
30 attr = fat_attr(inode);
32 return put_user(attr, user_attr);
34 case FAT_IOCTL_SET_ATTRIBUTES:
36 u32 attr, oldattr;
37 int err, is_dir = S_ISDIR(inode->i_mode);
38 struct iattr ia;
40 err = get_user(attr, user_attr);
41 if (err)
42 return err;
44 down(&inode->i_sem);
46 if (IS_RDONLY(inode)) {
47 err = -EROFS;
48 goto up;
52 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
53 * prevents the user from turning us into a VFAT
54 * longname entry. Also, we obviously can't set
55 * any of the NTFS attributes in the high 24 bits.
57 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
58 /* Merge in ATTR_VOLUME and ATTR_DIR */
59 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
60 (is_dir ? ATTR_DIR : 0);
61 oldattr = fat_attr(inode);
63 /* Equivalent to a chmod() */
64 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
65 if (is_dir) {
66 ia.ia_mode = MSDOS_MKMODE(attr,
67 S_IRWXUGO & ~sbi->options.fs_dmask)
68 | S_IFDIR;
69 } else {
70 ia.ia_mode = MSDOS_MKMODE(attr,
71 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
72 & ~sbi->options.fs_fmask)
73 | S_IFREG;
76 /* The root directory has no attributes */
77 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
78 err = -EINVAL;
79 goto up;
82 if (sbi->options.sys_immutable) {
83 if ((attr | oldattr) & ATTR_SYS) {
84 if (!capable(CAP_LINUX_IMMUTABLE)) {
85 err = -EPERM;
86 goto up;
91 /* This MUST be done before doing anything irreversible... */
92 err = notify_change(filp->f_dentry, &ia);
93 if (err)
94 goto up;
96 if (sbi->options.sys_immutable) {
97 if (attr & ATTR_SYS)
98 inode->i_flags |= S_IMMUTABLE;
99 else
100 inode->i_flags &= S_IMMUTABLE;
103 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
104 mark_inode_dirty(inode);
106 up(&inode->i_sem);
107 return err;
109 default:
110 return -ENOTTY; /* Inappropriate ioctl for device */
114 struct file_operations fat_file_operations = {
115 .llseek = generic_file_llseek,
116 .read = do_sync_read,
117 .write = do_sync_write,
118 .readv = generic_file_readv,
119 .writev = generic_file_writev,
120 .aio_read = generic_file_aio_read,
121 .aio_write = generic_file_aio_write,
122 .mmap = generic_file_mmap,
123 .ioctl = fat_generic_ioctl,
124 .fsync = file_fsync,
125 .sendfile = generic_file_sendfile,
128 static int fat_cont_expand(struct inode *inode, loff_t size)
130 struct address_space *mapping = inode->i_mapping;
131 loff_t start = inode->i_size, count = size - inode->i_size;
132 int err;
134 err = generic_cont_expand_simple(inode, size);
135 if (err)
136 goto out;
138 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
139 mark_inode_dirty(inode);
140 if (IS_SYNC(inode))
141 err = sync_page_range_nolock(inode, mapping, start, count);
142 out:
143 return err;
146 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
148 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
149 struct inode *inode = dentry->d_inode;
150 int mask, error = 0;
152 lock_kernel();
155 * Expand the file. Since inode_setattr() updates ->i_size
156 * before calling the ->truncate(), but FAT needs to fill the
157 * hole before it.
159 if (attr->ia_valid & ATTR_SIZE) {
160 if (attr->ia_size > inode->i_size) {
161 error = fat_cont_expand(inode, attr->ia_size);
162 if (error || attr->ia_valid == ATTR_SIZE)
163 goto out;
164 attr->ia_valid &= ~ATTR_SIZE;
168 error = inode_change_ok(inode, attr);
169 if (error) {
170 if (sbi->options.quiet)
171 error = 0;
172 goto out;
174 if (((attr->ia_valid & ATTR_UID) &&
175 (attr->ia_uid != sbi->options.fs_uid)) ||
176 ((attr->ia_valid & ATTR_GID) &&
177 (attr->ia_gid != sbi->options.fs_gid)) ||
178 ((attr->ia_valid & ATTR_MODE) &&
179 (attr->ia_mode & ~MSDOS_VALID_MODE)))
180 error = -EPERM;
182 if (error) {
183 if (sbi->options.quiet)
184 error = 0;
185 goto out;
187 error = inode_setattr(inode, attr);
188 if (error)
189 goto out;
191 if (S_ISDIR(inode->i_mode))
192 mask = sbi->options.fs_dmask;
193 else
194 mask = sbi->options.fs_fmask;
195 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
196 out:
197 unlock_kernel();
198 return error;
201 EXPORT_SYMBOL_GPL(fat_notify_change);
203 /* Free all clusters after the skip'th cluster. */
204 static int fat_free(struct inode *inode, int skip)
206 struct super_block *sb = inode->i_sb;
207 int err, wait, free_start, i_start, i_logstart;
209 if (MSDOS_I(inode)->i_start == 0)
210 return 0;
213 * Write a new EOF, and get the remaining cluster chain for freeing.
215 wait = IS_DIRSYNC(inode);
216 if (skip) {
217 struct fat_entry fatent;
218 int ret, fclus, dclus;
220 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
221 if (ret < 0)
222 return ret;
223 else if (ret == FAT_ENT_EOF)
224 return 0;
226 fatent_init(&fatent);
227 ret = fat_ent_read(inode, &fatent, dclus);
228 if (ret == FAT_ENT_EOF) {
229 fatent_brelse(&fatent);
230 return 0;
231 } else if (ret == FAT_ENT_FREE) {
232 fat_fs_panic(sb,
233 "%s: invalid cluster chain (i_pos %lld)",
234 __FUNCTION__, MSDOS_I(inode)->i_pos);
235 ret = -EIO;
236 } else if (ret > 0) {
237 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
238 if (err)
239 ret = err;
241 fatent_brelse(&fatent);
242 if (ret < 0)
243 return ret;
245 free_start = ret;
246 i_start = i_logstart = 0;
247 fat_cache_inval_inode(inode);
248 } else {
249 fat_cache_inval_inode(inode);
251 i_start = free_start = MSDOS_I(inode)->i_start;
252 i_logstart = MSDOS_I(inode)->i_logstart;
253 MSDOS_I(inode)->i_start = 0;
254 MSDOS_I(inode)->i_logstart = 0;
256 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
257 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
258 if (wait) {
259 err = fat_sync_inode(inode);
260 if (err)
261 goto error;
262 } else
263 mark_inode_dirty(inode);
264 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
266 /* Freeing the remained cluster chain */
267 return fat_free_clusters(inode, free_start);
269 error:
270 if (i_start) {
271 MSDOS_I(inode)->i_start = i_start;
272 MSDOS_I(inode)->i_logstart = i_logstart;
274 return err;
277 void fat_truncate(struct inode *inode)
279 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
280 const unsigned int cluster_size = sbi->cluster_size;
281 int nr_clusters;
284 * This protects against truncating a file bigger than it was then
285 * trying to write into the hole.
287 if (MSDOS_I(inode)->mmu_private > inode->i_size)
288 MSDOS_I(inode)->mmu_private = inode->i_size;
290 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
292 lock_kernel();
293 fat_free(inode, nr_clusters);
294 unlock_kernel();
297 struct inode_operations fat_file_inode_operations = {
298 .truncate = fat_truncate,
299 .setattr = fat_notify_change,