projects/linux·blob master · refs

459 lines · 12.0 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6#include <linux/init.h>
7#include <linux/buffer_head.h>
8#include <linux/mpage.h>
9#include <linux/bio.h>
10#include <linux/blkdev.h>
11#include <linux/time.h>
12#include <linux/writeback.h>
13#include <linux/uio.h>
14#include <linux/random.h>
15#include <linux/iversion.h>
16#include <linux/iomap.h>
17
18#include "exfat_raw.h"
19#include "exfat_fs.h"
20#include "iomap.h"
21
22int __exfat_write_inode(struct inode *inode, int sync)
23{
24 unsigned long long on_disk_size;
25 unsigned long long on_disk_valid_size;
26 struct exfat_dentry *ep, *ep2;
27 struct exfat_entry_set_cache es;
28 struct super_block *sb = inode->i_sb;
29 struct exfat_sb_info *sbi = EXFAT_SB(sb);
30 struct exfat_inode_info *ei = EXFAT_I(inode);
31 bool is_dir = (ei->type == TYPE_DIR);
32 struct timespec64 ts;
33
34 if (inode->i_ino == EXFAT_ROOT_INO)
35 return 0;
36
37 /*
38 * If the inode is already unlinked, there is no need for updating it.
39 */
40 if (ei->dir.dir == DIR_DELETED)
41 return 0;
42
43 if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
44 return 0;
45
46 exfat_set_volume_dirty(sb);
47
48 /* get the directory entry of given file or directory */
49 if (exfat_get_dentry_set_by_ei(&es, sb, ei))
50 return -EIO;
51 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
52 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
53
54 ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
55
56 /* set FILE_INFO structure using the acquired struct exfat_dentry */
57 exfat_set_entry_time(sbi, &ei->i_crtime,
58 &ep->dentry.file.create_tz,
59 &ep->dentry.file.create_time,
60 &ep->dentry.file.create_date,
61 &ep->dentry.file.create_time_cs);
62 ts = inode_get_mtime(inode);
63 exfat_set_entry_time(sbi, &ts,
64 &ep->dentry.file.modify_tz,
65 &ep->dentry.file.modify_time,
66 &ep->dentry.file.modify_date,
67 &ep->dentry.file.modify_time_cs);
68 ts = inode_get_atime(inode);
69 exfat_set_entry_time(sbi, &ts,
70 &ep->dentry.file.access_tz,
71 &ep->dentry.file.access_time,
72 &ep->dentry.file.access_date,
73 NULL);
74
75 /*
76 * During a DIO write, valid_size is updated eagerly in iomap_end (so
77 * that concurrent buffered reads see IOMAP_MAPPED) while i_size is
78 * updated asynchronously in end_io. The FAT chain was already
79 * extended to cover ceil(valid_size/cluster_size) clusters. Use the
80 * maximum so the on-disk size field always covers the FAT chain,
81 * preventing fsck from reporting "more clusters are allocated".
82 */
83 on_disk_size = max_t(unsigned long long, i_size_read(inode),
84 ei->valid_size);
85
86 if (ei->start_clu == EXFAT_EOF_CLUSTER)
87 on_disk_size = 0;
88 /*
89 * valid_size on disk must reflect only confirmed data (up to i_size)
90 * and must not exceed on_disk_size.
91 */
92 on_disk_valid_size = min_t(unsigned long long, ei->valid_size,
93 i_size_read(inode));
94 if (ei->start_clu == EXFAT_EOF_CLUSTER)
95 on_disk_valid_size = 0;
96
97 ep2->dentry.stream.size = cpu_to_le64(on_disk_size);
98 ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_valid_size);
99
100 if (on_disk_size) {
101 ep2->dentry.stream.flags = ei->flags;
102 ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu);
103 } else {
104 ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
105 ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
106 }
107
108 exfat_update_dir_chksum(&es);
109 return exfat_put_dentry_set(&es, sync);
110}
111
112int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
113{
114 int ret;
115
116 if (unlikely(exfat_forced_shutdown(inode->i_sb)))
117 return -EIO;
118
119 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
120 ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
121 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
122
123 return ret;
124}
125
126void exfat_sync_inode(struct inode *inode)
127{
128 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
129 __exfat_write_inode(inode, 1);
130}
131
132/*
133 * Input: inode, (logical) clu_offset, target allocation area
134 * Output: errcode, cluster number
135 * *clu = (~0), if it's unable to allocate a new cluster
136 */
137int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
138 unsigned int *clu, unsigned int *count, int create,
139 bool *balloc)
140{
141 int ret;
142 unsigned int last_clu;
143 struct exfat_chain new_clu;
144 struct super_block *sb = inode->i_sb;
145 struct exfat_sb_info *sbi = EXFAT_SB(sb);
146 struct exfat_inode_info *ei = EXFAT_I(inode);
147 unsigned int local_clu_offset = clu_offset;
148 unsigned int num_to_be_allocated = 0, num_clusters;
149
150 num_clusters = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode));
151 if (clu_offset > num_clusters ||
152 *count > num_clusters - clu_offset)
153 num_to_be_allocated = clu_offset + *count - num_clusters;
154
155 if (!create && (num_to_be_allocated > 0)) {
156 *clu = EXFAT_EOF_CLUSTER;
157 return 0;
158 }
159
160 *clu = last_clu = ei->start_clu;
161
162 if (*clu == EXFAT_EOF_CLUSTER) {
163 *count = 0;
164 } else if (ei->flags == ALLOC_NO_FAT_CHAIN) {
165 last_clu += num_clusters - 1;
166 if (clu_offset < num_clusters) {
167 *clu += clu_offset;
168 *count = min(num_clusters - clu_offset, *count);
169 } else {
170 *clu = EXFAT_EOF_CLUSTER;
171 *count = 0;
172 }
173 } else {
174 int err = exfat_get_cluster(inode, clu_offset,
175 clu, count, &last_clu);
176 if (err)
177 return -EIO;
178 }
179
180 if (*clu == EXFAT_EOF_CLUSTER) {
181 exfat_set_volume_dirty(sb);
182
183 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
184 EXFAT_EOF_CLUSTER : last_clu + 1;
185 new_clu.size = 0;
186 new_clu.flags = ei->flags;
187
188 /* allocate a cluster */
189 if (num_to_be_allocated < 1) {
190 /* Broken FAT (i_sze > allocated FAT) */
191 exfat_fs_error(sb, "broken FAT chain.");
192 return -EIO;
193 }
194
195 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
196 inode_needs_sync(inode), true);
197 if (ret)
198 return ret;
199
200 if (new_clu.dir == EXFAT_EOF_CLUSTER ||
201 new_clu.dir == EXFAT_FREE_CLUSTER) {
202 exfat_fs_error(sb,
203 "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
204 last_clu, new_clu.dir);
205 return -EIO;
206 }
207
208 /* append to the FAT chain */
209 if (last_clu == EXFAT_EOF_CLUSTER) {
210 if (new_clu.flags == ALLOC_FAT_CHAIN)
211 ei->flags = ALLOC_FAT_CHAIN;
212 ei->start_clu = new_clu.dir;
213 } else {
214 if (new_clu.flags != ei->flags) {
215 /* no-fat-chain bit is disabled,
216 * so fat-chain should be synced with
217 * alloc-bitmap
218 */
219 if (exfat_chain_cont_cluster(sb, ei->start_clu,
220 num_clusters))
221 return -EIO;
222 ei->flags = ALLOC_FAT_CHAIN;
223 }
224 if (new_clu.flags == ALLOC_FAT_CHAIN)
225 if (exfat_ent_set(sb, last_clu, new_clu.dir))
226 return -EIO;
227 }
228
229 *clu = new_clu.dir;
230 *count = new_clu.size;
231
232 inode->i_blocks += exfat_cluster_to_sectors(sbi, new_clu.size);
233 if (balloc)
234 *balloc = true;
235 }
236
237 /* hint information */
238 ei->hint_bmap.off = local_clu_offset;
239 ei->hint_bmap.clu = *clu;
240
241 return 0;
242}
243
244static int exfat_read_folio(struct file *file, struct folio *folio)
245{
246 struct iomap_read_folio_ctx ctx = {
247 .cur_folio = folio,
248 .ops = &exfat_iomap_bio_read_ops,
249 };
250
251 iomap_read_folio(&exfat_iomap_ops, &ctx, NULL);
252 return 0;
253}
254
255static void exfat_readahead(struct readahead_control *rac)
256{
257 struct address_space *mapping = rac->mapping;
258 struct inode *inode = mapping->host;
259 struct exfat_inode_info *ei = EXFAT_I(inode);
260 loff_t pos = readahead_pos(rac);
261 struct iomap_read_folio_ctx ctx = {
262 .ops = &exfat_iomap_bio_read_ops,
263 .rac = rac,
264 };
265
266 /* Range cross valid_size, read it page by page. */
267 if (ei->valid_size < i_size_read(inode) &&
268 pos <= ei->valid_size &&
269 ei->valid_size < pos + readahead_length(rac))
270 return;
271
272 iomap_readahead(&exfat_iomap_ops, &ctx, NULL);
273}
274
275static int exfat_writepages(struct address_space *mapping,
276 struct writeback_control *wbc)
277{
278 struct iomap_writepage_ctx wpc = {
279 .inode = mapping->host,
280 .wbc = wbc,
281 .ops = &exfat_writeback_ops,
282 };
283
284 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
285 return -EIO;
286
287 return iomap_writepages(&wpc);
288}
289
290static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
291{
292 sector_t blocknr;
293
294 inode_lock_shared(mapping->host);
295 blocknr = iomap_bmap(mapping, block, &exfat_iomap_ops);
296 inode_unlock_shared(mapping->host);
297 return blocknr;
298}
299
300static const struct address_space_operations exfat_aops = {
301 .read_folio = exfat_read_folio,
302 .readahead = exfat_readahead,
303 .writepages = exfat_writepages,
304 .dirty_folio = iomap_dirty_folio,
305 .bmap = exfat_aop_bmap,
306 .migrate_folio = filemap_migrate_folio,
307 .is_partially_uptodate = iomap_is_partially_uptodate,
308 .error_remove_folio = generic_error_remove_folio,
309 .release_folio = iomap_release_folio,
310 .invalidate_folio = iomap_invalidate_folio,
311 .swap_activate = exfat_iomap_swap_activate,
312};
313
314static inline unsigned long exfat_hash(loff_t i_pos)
315{
316 return hash_32(i_pos, EXFAT_HASH_BITS);
317}
318
319void exfat_hash_inode(struct inode *inode, loff_t i_pos)
320{
321 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
322 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
323
324 spin_lock(&sbi->inode_hash_lock);
325 EXFAT_I(inode)->i_pos = i_pos;
326 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
327 spin_unlock(&sbi->inode_hash_lock);
328}
329
330void exfat_unhash_inode(struct inode *inode)
331{
332 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
333
334 spin_lock(&sbi->inode_hash_lock);
335 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
336 EXFAT_I(inode)->i_pos = 0;
337 spin_unlock(&sbi->inode_hash_lock);
338}
339
340struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
341{
342 struct exfat_sb_info *sbi = EXFAT_SB(sb);
343 struct exfat_inode_info *info;
344 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
345 struct inode *inode = NULL;
346
347 spin_lock(&sbi->inode_hash_lock);
348 hlist_for_each_entry(info, head, i_hash_fat) {
349 WARN_ON(info->vfs_inode.i_sb != sb);
350
351 if (i_pos != info->i_pos)
352 continue;
353 inode = igrab(&info->vfs_inode);
354 if (inode)
355 break;
356 }
357 spin_unlock(&sbi->inode_hash_lock);
358 return inode;
359}
360
361/* doesn't deal with root inode */
362static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
363{
364 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
365 struct exfat_inode_info *ei = EXFAT_I(inode);
366 loff_t size = info->size;
367
368 ei->dir = info->dir;
369 ei->entry = info->entry;
370 ei->attr = info->attr;
371 ei->start_clu = info->start_clu;
372 ei->flags = info->flags;
373 ei->type = info->type;
374 ei->valid_size = info->valid_size;
375 ei->zeroed_size = info->valid_size;
376
377 ei->version = 0;
378 ei->hint_stat.eidx = 0;
379 ei->hint_stat.clu = info->start_clu;
380 ei->hint_femp.eidx = EXFAT_HINT_NONE;
381 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
382 ei->i_pos = 0;
383
384 inode->i_uid = sbi->options.fs_uid;
385 inode->i_gid = sbi->options.fs_gid;
386 inode_inc_iversion(inode);
387 inode->i_generation = get_random_u32();
388
389 if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
390 inode->i_generation &= ~1;
391 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
392 inode->i_op = &exfat_dir_inode_operations;
393 inode->i_fop = &exfat_dir_operations;
394 set_nlink(inode, info->num_subdirs);
395 } else { /* regular file */
396 inode->i_generation |= 1;
397 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
398 inode->i_op = &exfat_file_inode_operations;
399 inode->i_fop = &exfat_file_operations;
400 inode->i_mapping->a_ops = &exfat_aops;
401 inode->i_mapping->nrpages = 0;
402 }
403
404 i_size_write(inode, size);
405
406 exfat_save_attr(inode, info->attr);
407
408 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
409 inode_set_mtime_to_ts(inode, info->mtime);
410 inode_set_ctime_to_ts(inode, info->mtime);
411 ei->i_crtime = info->crtime;
412 inode_set_atime_to_ts(inode, info->atime);
413
414 return 0;
415}
416
417struct inode *exfat_build_inode(struct super_block *sb,
418 struct exfat_dir_entry *info, loff_t i_pos)
419{
420 struct inode *inode;
421 int err;
422
423 inode = exfat_iget(sb, i_pos);
424 if (inode)
425 goto out;
426 inode = new_inode(sb);
427 if (!inode) {
428 inode = ERR_PTR(-ENOMEM);
429 goto out;
430 }
431 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
432 inode_set_iversion(inode, 1);
433 err = exfat_fill_inode(inode, info);
434 if (err) {
435 iput(inode);
436 inode = ERR_PTR(err);
437 goto out;
438 }
439 exfat_hash_inode(inode, i_pos);
440 insert_inode_hash(inode);
441out:
442 return inode;
443}
444
445void exfat_evict_inode(struct inode *inode)
446{
447 truncate_inode_pages_final(&inode->i_data);
448
449 if (!inode->i_nlink) {
450 i_size_write(inode, 0);
451 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
452 __exfat_truncate(inode);
453 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
454 }
455
456 clear_inode(inode);
457 exfat_cache_inval_inode(inode);
458 exfat_unhash_inode(inode);
459}