Linux 6.7-rc7
[linux-modified.git] / fs / exfat / inode.c
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
17 #include "exfat_raw.h"
18 #include "exfat_fs.h"
19
20 int __exfat_write_inode(struct inode *inode, int sync)
21 {
22         unsigned long long on_disk_size;
23         struct exfat_dentry *ep, *ep2;
24         struct exfat_entry_set_cache es;
25         struct super_block *sb = inode->i_sb;
26         struct exfat_sb_info *sbi = EXFAT_SB(sb);
27         struct exfat_inode_info *ei = EXFAT_I(inode);
28         bool is_dir = (ei->type == TYPE_DIR) ? true : false;
29         struct timespec64 ts;
30
31         if (inode->i_ino == EXFAT_ROOT_INO)
32                 return 0;
33
34         /*
35          * If the inode is already unlinked, there is no need for updating it.
36          */
37         if (ei->dir.dir == DIR_DELETED)
38                 return 0;
39
40         if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
41                 return 0;
42
43         exfat_set_volume_dirty(sb);
44
45         /* get the directory entry of given file or directory */
46         if (exfat_get_dentry_set(&es, sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES))
47                 return -EIO;
48         ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
49         ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
50
51         ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
52
53         /* set FILE_INFO structure using the acquired struct exfat_dentry */
54         exfat_set_entry_time(sbi, &ei->i_crtime,
55                         &ep->dentry.file.create_tz,
56                         &ep->dentry.file.create_time,
57                         &ep->dentry.file.create_date,
58                         &ep->dentry.file.create_time_cs);
59         ts = inode_get_mtime(inode);
60         exfat_set_entry_time(sbi, &ts,
61                              &ep->dentry.file.modify_tz,
62                              &ep->dentry.file.modify_time,
63                              &ep->dentry.file.modify_date,
64                              &ep->dentry.file.modify_time_cs);
65         ts = inode_get_atime(inode);
66         exfat_set_entry_time(sbi, &ts,
67                              &ep->dentry.file.access_tz,
68                              &ep->dentry.file.access_time,
69                              &ep->dentry.file.access_date,
70                              NULL);
71
72         /* File size should be zero if there is no cluster allocated */
73         on_disk_size = i_size_read(inode);
74
75         if (ei->start_clu == EXFAT_EOF_CLUSTER)
76                 on_disk_size = 0;
77
78         ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size);
79         ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
80         if (on_disk_size) {
81                 ep2->dentry.stream.flags = ei->flags;
82                 ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu);
83         } else {
84                 ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
85                 ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
86         }
87
88         exfat_update_dir_chksum_with_entry_set(&es);
89         return exfat_put_dentry_set(&es, sync);
90 }
91
92 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
93 {
94         int ret;
95
96         mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
97         ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
98         mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
99
100         return ret;
101 }
102
103 void exfat_sync_inode(struct inode *inode)
104 {
105         lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
106         __exfat_write_inode(inode, 1);
107 }
108
109 /*
110  * Input: inode, (logical) clu_offset, target allocation area
111  * Output: errcode, cluster number
112  * *clu = (~0), if it's unable to allocate a new cluster
113  */
114 static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
115                 unsigned int *clu, int create)
116 {
117         int ret;
118         unsigned int last_clu;
119         struct exfat_chain new_clu;
120         struct super_block *sb = inode->i_sb;
121         struct exfat_sb_info *sbi = EXFAT_SB(sb);
122         struct exfat_inode_info *ei = EXFAT_I(inode);
123         unsigned int local_clu_offset = clu_offset;
124         unsigned int num_to_be_allocated = 0, num_clusters = 0;
125
126         if (ei->i_size_ondisk > 0)
127                 num_clusters =
128                         EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
129
130         if (clu_offset >= num_clusters)
131                 num_to_be_allocated = clu_offset - num_clusters + 1;
132
133         if (!create && (num_to_be_allocated > 0)) {
134                 *clu = EXFAT_EOF_CLUSTER;
135                 return 0;
136         }
137
138         *clu = last_clu = ei->start_clu;
139
140         if (ei->flags == ALLOC_NO_FAT_CHAIN) {
141                 if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
142                         last_clu += clu_offset - 1;
143
144                         if (clu_offset == num_clusters)
145                                 *clu = EXFAT_EOF_CLUSTER;
146                         else
147                                 *clu += clu_offset;
148                 }
149         } else if (ei->type == TYPE_FILE) {
150                 unsigned int fclus = 0;
151                 int err = exfat_get_cluster(inode, clu_offset,
152                                 &fclus, clu, &last_clu, 1);
153                 if (err)
154                         return -EIO;
155
156                 clu_offset -= fclus;
157         } else {
158                 /* hint information */
159                 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
160                     ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
161                         clu_offset -= ei->hint_bmap.off;
162                         /* hint_bmap.clu should be valid */
163                         WARN_ON(ei->hint_bmap.clu < 2);
164                         *clu = ei->hint_bmap.clu;
165                 }
166
167                 while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
168                         last_clu = *clu;
169                         if (exfat_get_next_cluster(sb, clu))
170                                 return -EIO;
171                         clu_offset--;
172                 }
173         }
174
175         if (*clu == EXFAT_EOF_CLUSTER) {
176                 exfat_set_volume_dirty(sb);
177
178                 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
179                                 EXFAT_EOF_CLUSTER : last_clu + 1;
180                 new_clu.size = 0;
181                 new_clu.flags = ei->flags;
182
183                 /* allocate a cluster */
184                 if (num_to_be_allocated < 1) {
185                         /* Broken FAT (i_sze > allocated FAT) */
186                         exfat_fs_error(sb, "broken FAT chain.");
187                         return -EIO;
188                 }
189
190                 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
191                                 inode_needs_sync(inode));
192                 if (ret)
193                         return ret;
194
195                 if (new_clu.dir == EXFAT_EOF_CLUSTER ||
196                     new_clu.dir == EXFAT_FREE_CLUSTER) {
197                         exfat_fs_error(sb,
198                                 "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
199                                 last_clu, new_clu.dir);
200                         return -EIO;
201                 }
202
203                 /* append to the FAT chain */
204                 if (last_clu == EXFAT_EOF_CLUSTER) {
205                         if (new_clu.flags == ALLOC_FAT_CHAIN)
206                                 ei->flags = ALLOC_FAT_CHAIN;
207                         ei->start_clu = new_clu.dir;
208                 } else {
209                         if (new_clu.flags != ei->flags) {
210                                 /* no-fat-chain bit is disabled,
211                                  * so fat-chain should be synced with
212                                  * alloc-bitmap
213                                  */
214                                 exfat_chain_cont_cluster(sb, ei->start_clu,
215                                         num_clusters);
216                                 ei->flags = ALLOC_FAT_CHAIN;
217                         }
218                         if (new_clu.flags == ALLOC_FAT_CHAIN)
219                                 if (exfat_ent_set(sb, last_clu, new_clu.dir))
220                                         return -EIO;
221                 }
222
223                 num_clusters += num_to_be_allocated;
224                 *clu = new_clu.dir;
225
226                 inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
227
228                 /*
229                  * Move *clu pointer along FAT chains (hole care) because the
230                  * caller of this function expect *clu to be the last cluster.
231                  * This only works when num_to_be_allocated >= 2,
232                  * *clu = (the first cluster of the allocated chain) =>
233                  * (the last cluster of ...)
234                  */
235                 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
236                         *clu += num_to_be_allocated - 1;
237                 } else {
238                         while (num_to_be_allocated > 1) {
239                                 if (exfat_get_next_cluster(sb, clu))
240                                         return -EIO;
241                                 num_to_be_allocated--;
242                         }
243                 }
244
245         }
246
247         /* hint information */
248         ei->hint_bmap.off = local_clu_offset;
249         ei->hint_bmap.clu = *clu;
250
251         return 0;
252 }
253
254 static int exfat_map_new_buffer(struct exfat_inode_info *ei,
255                 struct buffer_head *bh, loff_t pos)
256 {
257         if (buffer_delay(bh) && pos > ei->i_size_aligned)
258                 return -EIO;
259         set_buffer_new(bh);
260
261         /*
262          * Adjust i_size_aligned if i_size_ondisk is bigger than it.
263          */
264         if (ei->i_size_ondisk > ei->i_size_aligned)
265                 ei->i_size_aligned = ei->i_size_ondisk;
266         return 0;
267 }
268
269 static int exfat_get_block(struct inode *inode, sector_t iblock,
270                 struct buffer_head *bh_result, int create)
271 {
272         struct exfat_inode_info *ei = EXFAT_I(inode);
273         struct super_block *sb = inode->i_sb;
274         struct exfat_sb_info *sbi = EXFAT_SB(sb);
275         unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
276         int err = 0;
277         unsigned long mapped_blocks = 0;
278         unsigned int cluster, sec_offset;
279         sector_t last_block;
280         sector_t phys = 0;
281         loff_t pos;
282
283         mutex_lock(&sbi->s_lock);
284         last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
285         if (iblock >= last_block && !create)
286                 goto done;
287
288         /* Is this block already allocated? */
289         err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
290                         &cluster, create);
291         if (err) {
292                 if (err != -ENOSPC)
293                         exfat_fs_error_ratelimit(sb,
294                                 "failed to bmap (inode : %p iblock : %llu, err : %d)",
295                                 inode, (unsigned long long)iblock, err);
296                 goto unlock_ret;
297         }
298
299         if (cluster == EXFAT_EOF_CLUSTER)
300                 goto done;
301
302         /* sector offset in cluster */
303         sec_offset = iblock & (sbi->sect_per_clus - 1);
304
305         phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
306         mapped_blocks = sbi->sect_per_clus - sec_offset;
307         max_blocks = min(mapped_blocks, max_blocks);
308
309         /* Treat newly added block / cluster */
310         if (iblock < last_block)
311                 create = 0;
312
313         if (create || buffer_delay(bh_result)) {
314                 pos = EXFAT_BLK_TO_B((iblock + 1), sb);
315                 if (ei->i_size_ondisk < pos)
316                         ei->i_size_ondisk = pos;
317         }
318
319         if (create) {
320                 err = exfat_map_new_buffer(ei, bh_result, pos);
321                 if (err) {
322                         exfat_fs_error(sb,
323                                         "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n",
324                                         pos, ei->i_size_aligned);
325                         goto unlock_ret;
326                 }
327         }
328
329         if (buffer_delay(bh_result))
330                 clear_buffer_delay(bh_result);
331         map_bh(bh_result, sb, phys);
332 done:
333         bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
334 unlock_ret:
335         mutex_unlock(&sbi->s_lock);
336         return err;
337 }
338
339 static int exfat_read_folio(struct file *file, struct folio *folio)
340 {
341         return mpage_read_folio(folio, exfat_get_block);
342 }
343
344 static void exfat_readahead(struct readahead_control *rac)
345 {
346         mpage_readahead(rac, exfat_get_block);
347 }
348
349 static int exfat_writepages(struct address_space *mapping,
350                 struct writeback_control *wbc)
351 {
352         return mpage_writepages(mapping, wbc, exfat_get_block);
353 }
354
355 static void exfat_write_failed(struct address_space *mapping, loff_t to)
356 {
357         struct inode *inode = mapping->host;
358
359         if (to > i_size_read(inode)) {
360                 truncate_pagecache(inode, i_size_read(inode));
361                 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
362                 exfat_truncate(inode);
363         }
364 }
365
366 static int exfat_write_begin(struct file *file, struct address_space *mapping,
367                 loff_t pos, unsigned int len,
368                 struct page **pagep, void **fsdata)
369 {
370         int ret;
371
372         *pagep = NULL;
373         ret = cont_write_begin(file, mapping, pos, len, pagep, fsdata,
374                                exfat_get_block,
375                                &EXFAT_I(mapping->host)->i_size_ondisk);
376
377         if (ret < 0)
378                 exfat_write_failed(mapping, pos+len);
379
380         return ret;
381 }
382
383 static int exfat_write_end(struct file *file, struct address_space *mapping,
384                 loff_t pos, unsigned int len, unsigned int copied,
385                 struct page *pagep, void *fsdata)
386 {
387         struct inode *inode = mapping->host;
388         struct exfat_inode_info *ei = EXFAT_I(inode);
389         int err;
390
391         err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
392
393         if (ei->i_size_aligned < i_size_read(inode)) {
394                 exfat_fs_error(inode->i_sb,
395                         "invalid size(size(%llu) > aligned(%llu)\n",
396                         i_size_read(inode), ei->i_size_aligned);
397                 return -EIO;
398         }
399
400         if (err < len)
401                 exfat_write_failed(mapping, pos+len);
402
403         if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
404                 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
405                 ei->attr |= EXFAT_ATTR_ARCHIVE;
406                 mark_inode_dirty(inode);
407         }
408
409         return err;
410 }
411
412 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
413 {
414         struct address_space *mapping = iocb->ki_filp->f_mapping;
415         struct inode *inode = mapping->host;
416         loff_t size = iocb->ki_pos + iov_iter_count(iter);
417         int rw = iov_iter_rw(iter);
418         ssize_t ret;
419
420         if (rw == WRITE) {
421                 /*
422                  * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
423                  * so we need to update the ->i_size_aligned to block boundary.
424                  *
425                  * But we must fill the remaining area or hole by nul for
426                  * updating ->i_size_aligned
427                  *
428                  * Return 0, and fallback to normal buffered write.
429                  */
430                 if (EXFAT_I(inode)->i_size_aligned < size)
431                         return 0;
432         }
433
434         /*
435          * Need to use the DIO_LOCKING for avoiding the race
436          * condition of exfat_get_block() and ->truncate().
437          */
438         ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
439         if (ret < 0 && (rw & WRITE))
440                 exfat_write_failed(mapping, size);
441         return ret;
442 }
443
444 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
445 {
446         sector_t blocknr;
447
448         /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
449         down_read(&EXFAT_I(mapping->host)->truncate_lock);
450         blocknr = generic_block_bmap(mapping, block, exfat_get_block);
451         up_read(&EXFAT_I(mapping->host)->truncate_lock);
452         return blocknr;
453 }
454
455 /*
456  * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
457  * up to the end of the block which corresponds to `from'.
458  * This is required during truncate to physically zeroout the tail end
459  * of that block so it doesn't yield old data if the file is later grown.
460  * Also, avoid causing failure from fsx for cases of "data past EOF"
461  */
462 int exfat_block_truncate_page(struct inode *inode, loff_t from)
463 {
464         return block_truncate_page(inode->i_mapping, from, exfat_get_block);
465 }
466
467 static const struct address_space_operations exfat_aops = {
468         .dirty_folio    = block_dirty_folio,
469         .invalidate_folio = block_invalidate_folio,
470         .read_folio     = exfat_read_folio,
471         .readahead      = exfat_readahead,
472         .writepages     = exfat_writepages,
473         .write_begin    = exfat_write_begin,
474         .write_end      = exfat_write_end,
475         .direct_IO      = exfat_direct_IO,
476         .bmap           = exfat_aop_bmap,
477         .migrate_folio  = buffer_migrate_folio,
478 };
479
480 static inline unsigned long exfat_hash(loff_t i_pos)
481 {
482         return hash_32(i_pos, EXFAT_HASH_BITS);
483 }
484
485 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
486 {
487         struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
488         struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
489
490         spin_lock(&sbi->inode_hash_lock);
491         EXFAT_I(inode)->i_pos = i_pos;
492         hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
493         spin_unlock(&sbi->inode_hash_lock);
494 }
495
496 void exfat_unhash_inode(struct inode *inode)
497 {
498         struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
499
500         spin_lock(&sbi->inode_hash_lock);
501         hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
502         EXFAT_I(inode)->i_pos = 0;
503         spin_unlock(&sbi->inode_hash_lock);
504 }
505
506 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
507 {
508         struct exfat_sb_info *sbi = EXFAT_SB(sb);
509         struct exfat_inode_info *info;
510         struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
511         struct inode *inode = NULL;
512
513         spin_lock(&sbi->inode_hash_lock);
514         hlist_for_each_entry(info, head, i_hash_fat) {
515                 WARN_ON(info->vfs_inode.i_sb != sb);
516
517                 if (i_pos != info->i_pos)
518                         continue;
519                 inode = igrab(&info->vfs_inode);
520                 if (inode)
521                         break;
522         }
523         spin_unlock(&sbi->inode_hash_lock);
524         return inode;
525 }
526
527 /* doesn't deal with root inode */
528 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
529 {
530         struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
531         struct exfat_inode_info *ei = EXFAT_I(inode);
532         loff_t size = info->size;
533
534         ei->dir = info->dir;
535         ei->entry = info->entry;
536         ei->attr = info->attr;
537         ei->start_clu = info->start_clu;
538         ei->flags = info->flags;
539         ei->type = info->type;
540
541         ei->version = 0;
542         ei->hint_stat.eidx = 0;
543         ei->hint_stat.clu = info->start_clu;
544         ei->hint_femp.eidx = EXFAT_HINT_NONE;
545         ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
546         ei->i_pos = 0;
547
548         inode->i_uid = sbi->options.fs_uid;
549         inode->i_gid = sbi->options.fs_gid;
550         inode_inc_iversion(inode);
551         inode->i_generation = get_random_u32();
552
553         if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
554                 inode->i_generation &= ~1;
555                 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
556                 inode->i_op = &exfat_dir_inode_operations;
557                 inode->i_fop = &exfat_dir_operations;
558                 set_nlink(inode, info->num_subdirs);
559         } else { /* regular file */
560                 inode->i_generation |= 1;
561                 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
562                 inode->i_op = &exfat_file_inode_operations;
563                 inode->i_fop = &exfat_file_operations;
564                 inode->i_mapping->a_ops = &exfat_aops;
565                 inode->i_mapping->nrpages = 0;
566         }
567
568         i_size_write(inode, size);
569
570         /* ondisk and aligned size should be aligned with block size */
571         if (size & (inode->i_sb->s_blocksize - 1)) {
572                 size |= (inode->i_sb->s_blocksize - 1);
573                 size++;
574         }
575
576         ei->i_size_aligned = size;
577         ei->i_size_ondisk = size;
578
579         exfat_save_attr(inode, info->attr);
580
581         inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
582         inode_set_mtime_to_ts(inode, info->mtime);
583         inode_set_ctime_to_ts(inode, info->mtime);
584         ei->i_crtime = info->crtime;
585         inode_set_atime_to_ts(inode, info->atime);
586
587         return 0;
588 }
589
590 struct inode *exfat_build_inode(struct super_block *sb,
591                 struct exfat_dir_entry *info, loff_t i_pos)
592 {
593         struct inode *inode;
594         int err;
595
596         inode = exfat_iget(sb, i_pos);
597         if (inode)
598                 goto out;
599         inode = new_inode(sb);
600         if (!inode) {
601                 inode = ERR_PTR(-ENOMEM);
602                 goto out;
603         }
604         inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
605         inode_set_iversion(inode, 1);
606         err = exfat_fill_inode(inode, info);
607         if (err) {
608                 iput(inode);
609                 inode = ERR_PTR(err);
610                 goto out;
611         }
612         exfat_hash_inode(inode, i_pos);
613         insert_inode_hash(inode);
614 out:
615         return inode;
616 }
617
618 void exfat_evict_inode(struct inode *inode)
619 {
620         truncate_inode_pages(&inode->i_data, 0);
621
622         if (!inode->i_nlink) {
623                 i_size_write(inode, 0);
624                 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
625                 __exfat_truncate(inode);
626                 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
627         }
628
629         invalidate_inode_buffers(inode);
630         clear_inode(inode);
631         exfat_cache_inval_inode(inode);
632         exfat_unhash_inode(inode);
633 }