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 LT |
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> | |
05eb0b51 | 15 | #include <linux/writeback.h> |
1da177e4 | 16 | |
1da177e4 LT |
17 | int fat_generic_ioctl(struct inode *inode, struct file *filp, |
18 | unsigned int cmd, unsigned long arg) | |
19 | { | |
20 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | |
21 | u32 __user *user_attr = (u32 __user *)arg; | |
22 | ||
23 | switch (cmd) { | |
24 | case FAT_IOCTL_GET_ATTRIBUTES: | |
25 | { | |
26 | u32 attr; | |
27 | ||
28 | if (inode->i_ino == MSDOS_ROOT_INO) | |
29 | attr = ATTR_DIR; | |
30 | else | |
31 | attr = fat_attr(inode); | |
32 | ||
33 | return put_user(attr, user_attr); | |
34 | } | |
35 | case FAT_IOCTL_SET_ATTRIBUTES: | |
36 | { | |
37 | u32 attr, oldattr; | |
38 | int err, is_dir = S_ISDIR(inode->i_mode); | |
39 | struct iattr ia; | |
40 | ||
41 | err = get_user(attr, user_attr); | |
42 | if (err) | |
43 | return err; | |
44 | ||
1b1dcc1b | 45 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
46 | |
47 | if (IS_RDONLY(inode)) { | |
48 | err = -EROFS; | |
49 | goto up; | |
50 | } | |
51 | ||
52 | /* | |
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. | |
57 | */ | |
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); | |
63 | ||
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; | |
75 | } | |
76 | ||
77 | /* The root directory has no attributes */ | |
78 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { | |
79 | err = -EINVAL; | |
80 | goto up; | |
81 | } | |
82 | ||
83 | if (sbi->options.sys_immutable) { | |
84 | if ((attr | oldattr) & ATTR_SYS) { | |
85 | if (!capable(CAP_LINUX_IMMUTABLE)) { | |
86 | err = -EPERM; | |
87 | goto up; | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | /* This MUST be done before doing anything irreversible... */ | |
93 | err = notify_change(filp->f_dentry, &ia); | |
94 | if (err) | |
95 | goto up; | |
96 | ||
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; | |
102 | } | |
103 | ||
104 | MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED; | |
105 | mark_inode_dirty(inode); | |
106 | up: | |
1b1dcc1b | 107 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
108 | return err; |
109 | } | |
110 | default: | |
111 | return -ENOTTY; /* Inappropriate ioctl for device */ | |
112 | } | |
113 | } | |
114 | ||
115 | 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, | |
ef402268 | 120 | .writev = generic_file_writev, |
1da177e4 | 121 | .aio_read = generic_file_aio_read, |
ef402268 | 122 | .aio_write = generic_file_aio_write, |
1da177e4 LT |
123 | .mmap = generic_file_mmap, |
124 | .ioctl = fat_generic_ioctl, | |
125 | .fsync = file_fsync, | |
126 | .sendfile = generic_file_sendfile, | |
127 | }; | |
128 | ||
05eb0b51 OH |
129 | static int fat_cont_expand(struct inode *inode, loff_t size) |
130 | { | |
131 | struct address_space *mapping = inode->i_mapping; | |
132 | loff_t start = inode->i_size, count = size - inode->i_size; | |
133 | int err; | |
134 | ||
135 | err = generic_cont_expand_simple(inode, size); | |
136 | if (err) | |
137 | goto out; | |
138 | ||
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; | |
145 | } | |
146 | ||
1da177e4 LT |
147 | int fat_notify_change(struct dentry *dentry, struct iattr *attr) |
148 | { | |
149 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); | |
150 | struct inode *inode = dentry->d_inode; | |
151 | int mask, error = 0; | |
152 | ||
153 | lock_kernel(); | |
154 | ||
05eb0b51 OH |
155 | /* |
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. | |
159 | */ | |
1da177e4 LT |
160 | if (attr->ia_valid & ATTR_SIZE) { |
161 | if (attr->ia_size > inode->i_size) { | |
05eb0b51 OH |
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; | |
1da177e4 LT |
166 | } |
167 | } | |
168 | ||
169 | error = inode_change_ok(inode, attr); | |
170 | if (error) { | |
171 | if (sbi->options.quiet) | |
172 | error = 0; | |
173 | goto out; | |
174 | } | |
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; | |
182 | ||
183 | if (error) { | |
184 | if (sbi->options.quiet) | |
185 | error = 0; | |
186 | goto out; | |
187 | } | |
188 | error = inode_setattr(inode, attr); | |
189 | if (error) | |
190 | goto out; | |
191 | ||
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; | |
200 | } | |
201 | ||
7c709d00 | 202 | EXPORT_SYMBOL_GPL(fat_notify_change); |
1da177e4 LT |
203 | |
204 | /* Free all clusters after the skip'th cluster. */ | |
205 | static int fat_free(struct inode *inode, int skip) | |
206 | { | |
207 | struct super_block *sb = inode->i_sb; | |
208 | int err, wait, free_start, i_start, i_logstart; | |
209 | ||
210 | if (MSDOS_I(inode)->i_start == 0) | |
211 | return 0; | |
212 | ||
3b641407 OH |
213 | fat_cache_inval_inode(inode); |
214 | ||
1da177e4 | 215 | wait = IS_DIRSYNC(inode); |
3b641407 OH |
216 | i_start = free_start = MSDOS_I(inode)->i_start; |
217 | i_logstart = MSDOS_I(inode)->i_logstart; | |
218 | ||
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; | |
223 | } | |
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; | |
232 | } | |
233 | } else | |
234 | mark_inode_dirty(inode); | |
235 | ||
236 | /* Write a new EOF, and get the remaining cluster chain for freeing. */ | |
1da177e4 LT |
237 | if (skip) { |
238 | struct fat_entry fatent; | |
239 | int ret, fclus, dclus; | |
240 | ||
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; | |
246 | ||
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; | |
261 | } | |
262 | fatent_brelse(&fatent); | |
263 | if (ret < 0) | |
264 | return ret; | |
265 | ||
266 | free_start = ret; | |
1da177e4 | 267 | } |
1da177e4 LT |
268 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); |
269 | ||
270 | /* Freeing the remained cluster chain */ | |
271 | return fat_free_clusters(inode, free_start); | |
1da177e4 LT |
272 | } |
273 | ||
274 | void fat_truncate(struct inode *inode) | |
275 | { | |
276 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | |
277 | const unsigned int cluster_size = sbi->cluster_size; | |
278 | int nr_clusters; | |
279 | ||
280 | /* | |
281 | * This protects against truncating a file bigger than it was then | |
282 | * trying to write into the hole. | |
283 | */ | |
284 | if (MSDOS_I(inode)->mmu_private > inode->i_size) | |
285 | MSDOS_I(inode)->mmu_private = inode->i_size; | |
286 | ||
287 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; | |
288 | ||
289 | lock_kernel(); | |
290 | fat_free(inode, nr_clusters); | |
291 | unlock_kernel(); | |
292 | } | |
293 | ||
294 | struct inode_operations fat_file_inode_operations = { | |
295 | .truncate = fat_truncate, | |
296 | .setattr = fat_notify_change, | |
297 | }; |