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