Commit | Line | Data |
---|---|---|
98d91704 NJ |
1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* | |
3 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. | |
4 | */ | |
5 | ||
6 | #include <linux/slab.h> | |
7 | #include <linux/cred.h> | |
8 | #include <linux/buffer_head.h> | |
5267456e | 9 | #include <linux/blkdev.h> |
98d91704 NJ |
10 | |
11 | #include "exfat_raw.h" | |
12 | #include "exfat_fs.h" | |
13 | ||
14 | static int exfat_cont_expand(struct inode *inode, loff_t size) | |
15 | { | |
16 | struct address_space *mapping = inode->i_mapping; | |
17 | loff_t start = i_size_read(inode), count = size - i_size_read(inode); | |
18 | int err, err2; | |
19 | ||
20 | err = generic_cont_expand_simple(inode, size); | |
21 | if (err) | |
22 | return err; | |
23 | ||
24 | inode->i_ctime = inode->i_mtime = current_time(inode); | |
25 | mark_inode_dirty(inode); | |
26 | ||
27 | if (!IS_SYNC(inode)) | |
28 | return 0; | |
29 | ||
30 | err = filemap_fdatawrite_range(mapping, start, start + count - 1); | |
31 | err2 = sync_mapping_buffers(mapping); | |
32 | if (!err) | |
33 | err = err2; | |
34 | err2 = write_inode_now(inode, 1); | |
35 | if (!err) | |
36 | err = err2; | |
37 | if (err) | |
38 | return err; | |
39 | ||
40 | return filemap_fdatawait_range(mapping, start, start + count - 1); | |
41 | } | |
42 | ||
43 | static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode) | |
44 | { | |
45 | mode_t allow_utime = sbi->options.allow_utime; | |
46 | ||
47 | if (!uid_eq(current_fsuid(), inode->i_uid)) { | |
48 | if (in_group_p(inode->i_gid)) | |
49 | allow_utime >>= 3; | |
50 | if (allow_utime & MAY_WRITE) | |
51 | return true; | |
52 | } | |
53 | ||
54 | /* use a default check */ | |
55 | return false; | |
56 | } | |
57 | ||
58 | static int exfat_sanitize_mode(const struct exfat_sb_info *sbi, | |
59 | struct inode *inode, umode_t *mode_ptr) | |
60 | { | |
61 | mode_t i_mode, mask, perm; | |
62 | ||
63 | i_mode = inode->i_mode; | |
64 | ||
65 | mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ? | |
66 | sbi->options.fs_fmask : sbi->options.fs_dmask; | |
67 | perm = *mode_ptr & ~(S_IFMT | mask); | |
68 | ||
69 | /* Of the r and x bits, all (subject to umask) must be present.*/ | |
70 | if ((perm & 0555) != (i_mode & 0555)) | |
71 | return -EPERM; | |
72 | ||
73 | if (exfat_mode_can_hold_ro(inode)) { | |
74 | /* | |
75 | * Of the w bits, either all (subject to umask) or none must | |
76 | * be present. | |
77 | */ | |
78 | if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask))) | |
79 | return -EPERM; | |
80 | } else { | |
81 | /* | |
82 | * If exfat_mode_can_hold_ro(inode) is false, can't change | |
83 | * w bits. | |
84 | */ | |
85 | if ((perm & 0222) != (0222 & ~mask)) | |
86 | return -EPERM; | |
87 | } | |
88 | ||
89 | *mode_ptr &= S_IFMT | perm; | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
94 | /* resize the file length */ | |
95 | int __exfat_truncate(struct inode *inode, loff_t new_size) | |
96 | { | |
97 | unsigned int num_clusters_new, num_clusters_phys; | |
98 | unsigned int last_clu = EXFAT_FREE_CLUSTER; | |
99 | struct exfat_chain clu; | |
98d91704 NJ |
100 | struct super_block *sb = inode->i_sb; |
101 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
102 | struct exfat_inode_info *ei = EXFAT_I(inode); | |
98d91704 NJ |
103 | int evict = (ei->dir.dir == DIR_DELETED) ? 1 : 0; |
104 | ||
105 | /* check if the given file ID is opened */ | |
106 | if (ei->type != TYPE_FILE && ei->type != TYPE_DIR) | |
107 | return -EPERM; | |
108 | ||
7018ec68 | 109 | exfat_set_volume_dirty(sb); |
98d91704 NJ |
110 | |
111 | num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); | |
112 | num_clusters_phys = | |
113 | EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, sbi); | |
114 | ||
115 | exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); | |
116 | ||
117 | if (new_size > 0) { | |
118 | /* | |
119 | * Truncate FAT chain num_clusters after the first cluster | |
120 | * num_clusters = min(new, phys); | |
121 | */ | |
122 | unsigned int num_clusters = | |
123 | min(num_clusters_new, num_clusters_phys); | |
124 | ||
125 | /* | |
126 | * Follow FAT chain | |
127 | * (defensive coding - works fine even with corrupted FAT table | |
128 | */ | |
129 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { | |
130 | clu.dir += num_clusters; | |
131 | clu.size -= num_clusters; | |
132 | } else { | |
133 | while (num_clusters > 0) { | |
134 | last_clu = clu.dir; | |
135 | if (exfat_get_next_cluster(sb, &(clu.dir))) | |
136 | return -EIO; | |
137 | ||
138 | num_clusters--; | |
139 | clu.size--; | |
140 | } | |
141 | } | |
142 | } else { | |
143 | ei->flags = ALLOC_NO_FAT_CHAIN; | |
144 | ei->start_clu = EXFAT_EOF_CLUSTER; | |
145 | } | |
146 | ||
147 | i_size_write(inode, new_size); | |
148 | ||
149 | if (ei->type == TYPE_FILE) | |
150 | ei->attr |= ATTR_ARCHIVE; | |
151 | ||
152 | /* update the directory entry */ | |
153 | if (!evict) { | |
154 | struct timespec64 ts; | |
943af1fd TK |
155 | struct exfat_dentry *ep, *ep2; |
156 | struct exfat_entry_set_cache *es; | |
8b0c4717 | 157 | int err; |
98d91704 NJ |
158 | |
159 | es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, | |
943af1fd | 160 | ES_ALL_ENTRIES); |
98d91704 NJ |
161 | if (!es) |
162 | return -EIO; | |
943af1fd TK |
163 | ep = exfat_get_dentry_cached(es, 0); |
164 | ep2 = exfat_get_dentry_cached(es, 1); | |
98d91704 NJ |
165 | |
166 | ts = current_time(inode); | |
167 | exfat_set_entry_time(sbi, &ts, | |
168 | &ep->dentry.file.modify_tz, | |
169 | &ep->dentry.file.modify_time, | |
170 | &ep->dentry.file.modify_date, | |
ed0f84d3 | 171 | &ep->dentry.file.modify_time_cs); |
98d91704 NJ |
172 | ep->dentry.file.attr = cpu_to_le16(ei->attr); |
173 | ||
174 | /* File size should be zero if there is no cluster allocated */ | |
175 | if (ei->start_clu == EXFAT_EOF_CLUSTER) { | |
29bbb14b NJ |
176 | ep2->dentry.stream.valid_size = 0; |
177 | ep2->dentry.stream.size = 0; | |
98d91704 | 178 | } else { |
29bbb14b | 179 | ep2->dentry.stream.valid_size = cpu_to_le64(new_size); |
41e3928f | 180 | ep2->dentry.stream.size = ep2->dentry.stream.valid_size; |
98d91704 NJ |
181 | } |
182 | ||
183 | if (new_size == 0) { | |
184 | /* Any directory can not be truncated to zero */ | |
185 | WARN_ON(ei->type != TYPE_FILE); | |
186 | ||
187 | ep2->dentry.stream.flags = ALLOC_FAT_CHAIN; | |
188 | ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER; | |
189 | } | |
190 | ||
943af1fd | 191 | exfat_update_dir_chksum_with_entry_set(es); |
8b0c4717 TK |
192 | err = exfat_free_dentry_set(es, inode_needs_sync(inode)); |
193 | if (err) | |
194 | return err; | |
98d91704 NJ |
195 | } |
196 | ||
197 | /* cut off from the FAT chain */ | |
198 | if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER && | |
199 | last_clu != EXFAT_EOF_CLUSTER) { | |
200 | if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER)) | |
201 | return -EIO; | |
202 | } | |
203 | ||
204 | /* invalidate cache and free the clusters */ | |
205 | /* clear exfat cache */ | |
206 | exfat_cache_inval_inode(inode); | |
207 | ||
208 | /* hint information */ | |
209 | ei->hint_bmap.off = EXFAT_EOF_CLUSTER; | |
210 | ei->hint_bmap.clu = EXFAT_EOF_CLUSTER; | |
98d91704 NJ |
211 | |
212 | /* hint_stat will be used if this is directory. */ | |
213 | ei->hint_stat.eidx = 0; | |
214 | ei->hint_stat.clu = ei->start_clu; | |
215 | ei->hint_femp.eidx = EXFAT_HINT_NONE; | |
216 | ||
217 | /* free the clusters */ | |
218 | if (exfat_free_cluster(inode, &clu)) | |
219 | return -EIO; | |
220 | ||
7018ec68 | 221 | exfat_clear_volume_dirty(sb); |
98d91704 NJ |
222 | |
223 | return 0; | |
224 | } | |
225 | ||
226 | void exfat_truncate(struct inode *inode, loff_t size) | |
227 | { | |
228 | struct super_block *sb = inode->i_sb; | |
229 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
45882a6a | 230 | unsigned int blocksize = i_blocksize(inode); |
98d91704 NJ |
231 | loff_t aligned_size; |
232 | int err; | |
233 | ||
234 | mutex_lock(&sbi->s_lock); | |
235 | if (EXFAT_I(inode)->start_clu == 0) { | |
236 | /* | |
237 | * Empty start_clu != ~0 (not allocated) | |
238 | */ | |
239 | exfat_fs_error(sb, "tried to truncate zeroed cluster."); | |
240 | goto write_size; | |
241 | } | |
242 | ||
243 | err = __exfat_truncate(inode, i_size_read(inode)); | |
244 | if (err) | |
245 | goto write_size; | |
246 | ||
247 | inode->i_ctime = inode->i_mtime = current_time(inode); | |
248 | if (IS_DIRSYNC(inode)) | |
249 | exfat_sync_inode(inode); | |
250 | else | |
251 | mark_inode_dirty(inode); | |
252 | ||
253 | inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) & | |
254 | ~(sbi->cluster_size - 1)) >> inode->i_blkbits; | |
255 | write_size: | |
256 | aligned_size = i_size_read(inode); | |
257 | if (aligned_size & (blocksize - 1)) { | |
258 | aligned_size |= (blocksize - 1); | |
259 | aligned_size++; | |
260 | } | |
261 | ||
262 | if (EXFAT_I(inode)->i_size_ondisk > i_size_read(inode)) | |
263 | EXFAT_I(inode)->i_size_ondisk = aligned_size; | |
264 | ||
265 | if (EXFAT_I(inode)->i_size_aligned > i_size_read(inode)) | |
266 | EXFAT_I(inode)->i_size_aligned = aligned_size; | |
267 | mutex_unlock(&sbi->s_lock); | |
268 | } | |
269 | ||
270 | int exfat_getattr(const struct path *path, struct kstat *stat, | |
271 | unsigned int request_mask, unsigned int query_flags) | |
272 | { | |
273 | struct inode *inode = d_backing_inode(path->dentry); | |
274 | struct exfat_inode_info *ei = EXFAT_I(inode); | |
275 | ||
276 | generic_fillattr(inode, stat); | |
81df1ad4 | 277 | exfat_truncate_atime(&stat->atime); |
98d91704 NJ |
278 | stat->result_mask |= STATX_BTIME; |
279 | stat->btime.tv_sec = ei->i_crtime.tv_sec; | |
280 | stat->btime.tv_nsec = ei->i_crtime.tv_nsec; | |
281 | stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size; | |
282 | return 0; | |
283 | } | |
284 | ||
285 | int exfat_setattr(struct dentry *dentry, struct iattr *attr) | |
286 | { | |
287 | struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb); | |
288 | struct inode *inode = dentry->d_inode; | |
289 | unsigned int ia_valid; | |
290 | int error; | |
291 | ||
292 | if ((attr->ia_valid & ATTR_SIZE) && | |
293 | attr->ia_size > i_size_read(inode)) { | |
294 | error = exfat_cont_expand(inode, attr->ia_size); | |
295 | if (error || attr->ia_valid == ATTR_SIZE) | |
296 | return error; | |
297 | attr->ia_valid &= ~ATTR_SIZE; | |
298 | } | |
299 | ||
300 | /* Check for setting the inode time. */ | |
301 | ia_valid = attr->ia_valid; | |
302 | if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) && | |
303 | exfat_allow_set_time(sbi, inode)) { | |
304 | attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | | |
305 | ATTR_TIMES_SET); | |
306 | } | |
307 | ||
308 | error = setattr_prepare(dentry, attr); | |
309 | attr->ia_valid = ia_valid; | |
310 | if (error) | |
311 | goto out; | |
312 | ||
313 | if (((attr->ia_valid & ATTR_UID) && | |
314 | !uid_eq(attr->ia_uid, sbi->options.fs_uid)) || | |
315 | ((attr->ia_valid & ATTR_GID) && | |
316 | !gid_eq(attr->ia_gid, sbi->options.fs_gid)) || | |
317 | ((attr->ia_valid & ATTR_MODE) && | |
318 | (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) { | |
319 | error = -EPERM; | |
320 | goto out; | |
321 | } | |
322 | ||
323 | /* | |
324 | * We don't return -EPERM here. Yes, strange, but this is too | |
325 | * old behavior. | |
326 | */ | |
327 | if (attr->ia_valid & ATTR_MODE) { | |
328 | if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) | |
329 | attr->ia_valid &= ~ATTR_MODE; | |
330 | } | |
331 | ||
332 | if (attr->ia_valid & ATTR_SIZE) { | |
333 | error = exfat_block_truncate_page(inode, attr->ia_size); | |
334 | if (error) | |
335 | goto out; | |
336 | ||
337 | down_write(&EXFAT_I(inode)->truncate_lock); | |
338 | truncate_setsize(inode, attr->ia_size); | |
339 | exfat_truncate(inode, attr->ia_size); | |
340 | up_write(&EXFAT_I(inode)->truncate_lock); | |
341 | } | |
342 | ||
343 | setattr_copy(inode, attr); | |
81df1ad4 | 344 | exfat_truncate_atime(&inode->i_atime); |
98d91704 NJ |
345 | mark_inode_dirty(inode); |
346 | ||
347 | out: | |
348 | return error; | |
349 | } | |
350 | ||
5267456e SS |
351 | int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) |
352 | { | |
353 | struct inode *inode = filp->f_mapping->host; | |
354 | int err; | |
355 | ||
356 | err = __generic_file_fsync(filp, start, end, datasync); | |
357 | if (err) | |
358 | return err; | |
359 | ||
360 | err = sync_blockdev(inode->i_sb->s_bdev); | |
361 | if (err) | |
362 | return err; | |
363 | ||
364 | return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL); | |
365 | } | |
366 | ||
98d91704 | 367 | const struct file_operations exfat_file_operations = { |
03577948 ES |
368 | .llseek = generic_file_llseek, |
369 | .read_iter = generic_file_read_iter, | |
370 | .write_iter = generic_file_write_iter, | |
371 | .mmap = generic_file_mmap, | |
5267456e | 372 | .fsync = exfat_file_fsync, |
03577948 ES |
373 | .splice_read = generic_file_splice_read, |
374 | .splice_write = iter_file_splice_write, | |
98d91704 NJ |
375 | }; |
376 | ||
377 | const struct inode_operations exfat_file_inode_operations = { | |
378 | .setattr = exfat_setattr, | |
379 | .getattr = exfat_getattr, | |
380 | }; |