projects/linux·blob master · refs

2025 lines · 52.2 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/fs/fat/inode.c
4 *
5 * Written 1992,1993 by Werner Almesberger
6 * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
7 * Rewritten for the constant inumbers support by Al Viro
8 *
9 * Fixes:
10 *
11 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
12 */
13
14#include <linux/module.h>
15#include <linux/pagemap.h>
16#include <linux/mpage.h>
17#include <linux/vfs.h>
18#include <linux/seq_file.h>
19#include <linux/uio.h>
20#include <linux/blkdev.h>
21#include <linux/backing-dev.h>
22#include <linux/unaligned.h>
23#include <linux/random.h>
24#include <linux/iversion.h>
25#include <linux/fs_struct.h>
26#include "fat.h"
27
28#ifndef CONFIG_FAT_DEFAULT_IOCHARSET
29/* if user don't select VFAT, this is undefined. */
30#define CONFIG_FAT_DEFAULT_IOCHARSET ""
31#endif
32
33#define KB_IN_SECTORS 2
34
35/* DOS dates from 1980/1/1 through 2107/12/31 */
36#define FAT_DATE_MIN (0<<9 | 1<<5 | 1)
37#define FAT_DATE_MAX (127<<9 | 12<<5 | 31)
38#define FAT_TIME_MAX (23<<11 | 59<<5 | 29)
39
40/*
41 * A deserialized copy of the on-disk structure laid out in struct
42 * fat_boot_sector.
43 */
44struct fat_bios_param_block {
45 u16 fat_sector_size;
46 u8 fat_sec_per_clus;
47 u16 fat_reserved;
48 u8 fat_fats;
49 u16 fat_dir_entries;
50 u16 fat_sectors;
51 u16 fat_fat_length;
52 u32 fat_total_sect;
53
54 u8 fat16_state;
55 u32 fat16_vol_id;
56
57 u32 fat32_length;
58 u32 fat32_root_cluster;
59 u16 fat32_info_sector;
60 u8 fat32_state;
61 u32 fat32_vol_id;
62};
63
64static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
65static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
66
67static struct fat_floppy_defaults {
68 unsigned nr_sectors;
69 unsigned sec_per_clus;
70 unsigned dir_entries;
71 unsigned media;
72 unsigned fat_length;
73} floppy_defaults[] = {
74{
75 .nr_sectors = 160 * KB_IN_SECTORS,
76 .sec_per_clus = 1,
77 .dir_entries = 64,
78 .media = 0xFE,
79 .fat_length = 1,
80},
81{
82 .nr_sectors = 180 * KB_IN_SECTORS,
83 .sec_per_clus = 1,
84 .dir_entries = 64,
85 .media = 0xFC,
86 .fat_length = 2,
87},
88{
89 .nr_sectors = 320 * KB_IN_SECTORS,
90 .sec_per_clus = 2,
91 .dir_entries = 112,
92 .media = 0xFF,
93 .fat_length = 1,
94},
95{
96 .nr_sectors = 360 * KB_IN_SECTORS,
97 .sec_per_clus = 2,
98 .dir_entries = 112,
99 .media = 0xFD,
100 .fat_length = 2,
101},
102};
103
104int fat_add_cluster(struct inode *inode)
105{
106 int err, cluster;
107
108 err = fat_alloc_clusters(inode, &cluster, 1);
109 if (err)
110 return err;
111 /* FIXME: this cluster should be added after data of this
112 * cluster is writed */
113 err = fat_chain_add(inode, cluster, 1);
114 if (err)
115 fat_free_clusters(inode, cluster);
116 return err;
117}
118
119static inline int __fat_get_block(struct inode *inode, sector_t iblock,
120 unsigned long *max_blocks,
121 struct buffer_head *bh_result, int create)
122{
123 struct super_block *sb = inode->i_sb;
124 struct msdos_sb_info *sbi = MSDOS_SB(sb);
125 unsigned long mapped_blocks;
126 sector_t phys, last_block;
127 int err, offset;
128
129 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
130 if (err)
131 return err;
132 if (phys) {
133 map_bh(bh_result, sb, phys);
134 *max_blocks = min(mapped_blocks, *max_blocks);
135 return 0;
136 }
137 if (!create)
138 return 0;
139
140 if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
141 fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
142 MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
143 return -EIO;
144 }
145
146 last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9);
147 offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
148 /*
149 * allocate a cluster according to the following.
150 * 1) no more available blocks
151 * 2) not part of fallocate region
152 */
153 if (!offset && !(iblock < last_block)) {
154 /* TODO: multiple cluster allocation would be desirable. */
155 err = fat_add_cluster(inode);
156 if (err)
157 return err;
158 }
159 /* available blocks on this cluster */
160 mapped_blocks = sbi->sec_per_clus - offset;
161
162 *max_blocks = min(mapped_blocks, *max_blocks);
163 MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
164
165 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
166 if (err)
167 return err;
168 if (!phys) {
169 fat_fs_error(sb,
170 "invalid FAT chain (i_pos %lld, last_block %llu)",
171 MSDOS_I(inode)->i_pos,
172 (unsigned long long)last_block);
173 return -EIO;
174 }
175
176 BUG_ON(*max_blocks != mapped_blocks);
177 set_buffer_new(bh_result);
178 map_bh(bh_result, sb, phys);
179
180 return 0;
181}
182
183static int fat_get_block(struct inode *inode, sector_t iblock,
184 struct buffer_head *bh_result, int create)
185{
186 struct super_block *sb = inode->i_sb;
187 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
188 int err;
189
190 err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
191 if (err)
192 return err;
193 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
194 return 0;
195}
196
197static int fat_writepages(struct address_space *mapping,
198 struct writeback_control *wbc)
199{
200 return mpage_writepages(mapping, wbc, fat_get_block);
201}
202
203static int fat_read_folio(struct file *file, struct folio *folio)
204{
205 return mpage_read_folio(folio, fat_get_block);
206}
207
208static void fat_readahead(struct readahead_control *rac)
209{
210 mpage_readahead(rac, fat_get_block);
211}
212
213static void fat_write_failed(struct address_space *mapping, loff_t to)
214{
215 struct inode *inode = mapping->host;
216
217 if (to > inode->i_size) {
218 truncate_pagecache(inode, inode->i_size);
219 fat_truncate_blocks(inode, inode->i_size);
220 }
221}
222
223static int fat_write_begin(const struct kiocb *iocb,
224 struct address_space *mapping,
225 loff_t pos, unsigned len,
226 struct folio **foliop, void **fsdata)
227{
228 int err;
229
230 err = cont_write_begin(iocb, mapping, pos, len,
231 foliop, fsdata, fat_get_block,
232 &MSDOS_I(mapping->host)->mmu_private);
233 if (err < 0)
234 fat_write_failed(mapping, pos + len);
235 return err;
236}
237
238static int fat_write_end(const struct kiocb *iocb,
239 struct address_space *mapping,
240 loff_t pos, unsigned len, unsigned copied,
241 struct folio *folio, void *fsdata)
242{
243 struct inode *inode = mapping->host;
244 int err;
245 err = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
246 if (err < len)
247 fat_write_failed(mapping, pos + len);
248 if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
249 fat_truncate_time(inode, NULL, FAT_UPDATE_CMTIME);
250 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
251 mark_inode_dirty(inode);
252 }
253 return err;
254}
255
256static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
257{
258 struct file *file = iocb->ki_filp;
259 struct address_space *mapping = file->f_mapping;
260 struct inode *inode = mapping->host;
261 size_t count = iov_iter_count(iter);
262 loff_t offset = iocb->ki_pos;
263 ssize_t ret;
264
265 if (iov_iter_rw(iter) == WRITE) {
266 /*
267 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
268 * so we need to update the ->mmu_private to block boundary.
269 *
270 * But we must fill the remaining area or hole by nul for
271 * updating ->mmu_private.
272 *
273 * Return 0, and fallback to normal buffered write.
274 */
275 loff_t size = offset + count;
276 if (MSDOS_I(inode)->mmu_private < size)
277 return 0;
278 }
279
280 /*
281 * FAT need to use the DIO_LOCKING for avoiding the race
282 * condition of fat_get_block() and ->truncate().
283 */
284 ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block);
285 if (ret < 0 && iov_iter_rw(iter) == WRITE)
286 fat_write_failed(mapping, offset + count);
287
288 return ret;
289}
290
291static int fat_get_block_bmap(struct inode *inode, sector_t iblock,
292 struct buffer_head *bh_result, int create)
293{
294 struct super_block *sb = inode->i_sb;
295 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
296 int err;
297 sector_t bmap;
298 unsigned long mapped_blocks;
299
300 BUG_ON(create != 0);
301
302 err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true);
303 if (err)
304 return err;
305
306 if (bmap) {
307 map_bh(bh_result, sb, bmap);
308 max_blocks = min(mapped_blocks, max_blocks);
309 }
310
311 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
312
313 return 0;
314}
315
316static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
317{
318 sector_t blocknr;
319
320 /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
321 down_read(&MSDOS_I(mapping->host)->truncate_lock);
322 blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap);
323 up_read(&MSDOS_I(mapping->host)->truncate_lock);
324
325 return blocknr;
326}
327
328/*
329 * fat_block_truncate_page() zeroes out a mapping from file offset `from'
330 * up to the end of the block which corresponds to `from'.
331 * This is required during truncate to physically zeroout the tail end
332 * of that block so it doesn't yield old data if the file is later grown.
333 * Also, avoid causing failure from fsx for cases of "data past EOF"
334 */
335int fat_block_truncate_page(struct inode *inode, loff_t from)
336{
337 return block_truncate_page(inode->i_mapping, from, fat_get_block);
338}
339
340static const struct address_space_operations fat_aops = {
341 .dirty_folio = block_dirty_folio,
342 .invalidate_folio = block_invalidate_folio,
343 .read_folio = fat_read_folio,
344 .readahead = fat_readahead,
345 .writepages = fat_writepages,
346 .write_begin = fat_write_begin,
347 .write_end = fat_write_end,
348 .direct_IO = fat_direct_IO,
349 .bmap = _fat_bmap,
350 .migrate_folio = buffer_migrate_folio,
351};
352
353/*
354 * New FAT inode stuff. We do the following:
355 * a) i_ino is constant and has nothing with on-disk location.
356 * b) FAT manages its own cache of directory entries.
357 * c) *This* cache is indexed by on-disk location.
358 * d) inode has an associated directory entry, all right, but
359 * it may be unhashed.
360 * e) currently entries are stored within struct inode. That should
361 * change.
362 * f) we deal with races in the following way:
363 * 1. readdir() and lookup() do FAT-dir-cache lookup.
364 * 2. rename() unhashes the F-d-c entry and rehashes it in
365 * a new place.
366 * 3. unlink() and rmdir() unhash F-d-c entry.
367 * 4. fat_write_inode() checks whether the thing is unhashed.
368 * If it is we silently return. If it isn't we do bread(),
369 * check if the location is still valid and retry if it
370 * isn't. Otherwise we do changes.
371 * 5. Spinlock is used to protect hash/unhash/location check/lookup
372 * 6. fat_evict_inode() unhashes the F-d-c entry.
373 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
374 * and consider negative result as cache miss.
375 */
376
377static void fat_hash_init(struct super_block *sb)
378{
379 struct msdos_sb_info *sbi = MSDOS_SB(sb);
380 int i;
381
382 spin_lock_init(&sbi->inode_hash_lock);
383 for (i = 0; i < FAT_HASH_SIZE; i++)
384 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
385}
386
387static inline unsigned long fat_hash(loff_t i_pos)
388{
389 return hash_32(i_pos, FAT_HASH_BITS);
390}
391
392static void dir_hash_init(struct super_block *sb)
393{
394 struct msdos_sb_info *sbi = MSDOS_SB(sb);
395 int i;
396
397 spin_lock_init(&sbi->dir_hash_lock);
398 for (i = 0; i < FAT_HASH_SIZE; i++)
399 INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
400}
401
402void fat_attach(struct inode *inode, loff_t i_pos)
403{
404 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
405
406 if (inode->i_ino != MSDOS_ROOT_INO) {
407 struct hlist_head *head = sbi->inode_hashtable
408 + fat_hash(i_pos);
409
410 spin_lock(&sbi->inode_hash_lock);
411 MSDOS_I(inode)->i_pos = i_pos;
412 hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
413 spin_unlock(&sbi->inode_hash_lock);
414 }
415
416 /* If NFS support is enabled, cache the mapping of start cluster
417 * to directory inode. This is used during reconnection of
418 * dentries to the filesystem root.
419 */
420 if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
421 struct hlist_head *d_head = sbi->dir_hashtable;
422 d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
423
424 spin_lock(&sbi->dir_hash_lock);
425 hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
426 spin_unlock(&sbi->dir_hash_lock);
427 }
428}
429EXPORT_SYMBOL_GPL(fat_attach);
430
431void fat_detach(struct inode *inode)
432{
433 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
434 spin_lock(&sbi->inode_hash_lock);
435 MSDOS_I(inode)->i_pos = 0;
436 hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
437 spin_unlock(&sbi->inode_hash_lock);
438
439 if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
440 spin_lock(&sbi->dir_hash_lock);
441 hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
442 spin_unlock(&sbi->dir_hash_lock);
443 }
444}
445EXPORT_SYMBOL_GPL(fat_detach);
446
447struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
448{
449 struct msdos_sb_info *sbi = MSDOS_SB(sb);
450 struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
451 struct msdos_inode_info *i;
452 struct inode *inode = NULL;
453
454 spin_lock(&sbi->inode_hash_lock);
455 hlist_for_each_entry(i, head, i_fat_hash) {
456 BUG_ON(i->vfs_inode.i_sb != sb);
457 if (i->i_pos != i_pos)
458 continue;
459 inode = igrab(&i->vfs_inode);
460 if (inode)
461 break;
462 }
463 spin_unlock(&sbi->inode_hash_lock);
464 return inode;
465}
466
467static int is_exec(unsigned char *extension)
468{
469 unsigned char exe_extensions[] = "EXECOMBAT", *walk;
470
471 for (walk = exe_extensions; *walk; walk += 3)
472 if (!strncmp(extension, walk, 3))
473 return 1;
474 return 0;
475}
476
477static int fat_calc_dir_size(struct inode *inode)
478{
479 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
480 int ret, fclus, dclus;
481
482 inode->i_size = 0;
483 if (MSDOS_I(inode)->i_start == 0)
484 return 0;
485
486 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
487 if (ret < 0)
488 return ret;
489 inode->i_size = (fclus + 1) << sbi->cluster_bits;
490
491 return 0;
492}
493
494static int fat_validate_dir(struct inode *dir)
495{
496 struct super_block *sb = dir->i_sb;
497
498 if (dir->i_nlink < 2) {
499 /* Directory should have "."/".." entries at least. */
500 fat_fs_error(sb, "corrupted directory (invalid entries)");
501 return -EIO;
502 }
503 if (MSDOS_I(dir)->i_start == 0 ||
504 MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) {
505 /* Directory should point valid cluster. */
506 fat_fs_error(sb, "corrupted directory (invalid i_start)");
507 return -EIO;
508 }
509 return 0;
510}
511
512/* doesn't deal with root inode */
513int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
514{
515 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
516 struct timespec64 mtime;
517 int error;
518
519 MSDOS_I(inode)->i_pos = 0;
520 inode->i_uid = sbi->options.fs_uid;
521 inode->i_gid = sbi->options.fs_gid;
522 inode_inc_iversion(inode);
523 inode->i_generation = get_random_u32();
524
525 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
526 inode->i_generation &= ~1;
527 inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
528 inode->i_op = sbi->dir_ops;
529 inode->i_fop = &fat_dir_operations;
530
531 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
532 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
533 error = fat_calc_dir_size(inode);
534 if (error < 0)
535 return error;
536 MSDOS_I(inode)->mmu_private = inode->i_size;
537
538 set_nlink(inode, fat_subdirs(inode));
539
540 error = fat_validate_dir(inode);
541 if (error < 0)
542 return error;
543 } else { /* not a directory */
544 inode->i_generation |= 1;
545 inode->i_mode = fat_make_mode(sbi, de->attr,
546 ((sbi->options.showexec && !is_exec(de->name + 8))
547 ? S_IRUGO|S_IWUGO : S_IRWXUGO));
548 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
549
550 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
551 inode->i_size = le32_to_cpu(de->size);
552 inode->i_op = &fat_file_inode_operations;
553 inode->i_fop = &fat_file_operations;
554 inode->i_mapping->a_ops = &fat_aops;
555 MSDOS_I(inode)->mmu_private = inode->i_size;
556 }
557 if (de->attr & ATTR_SYS) {
558 if (sbi->options.sys_immutable)
559 inode->i_flags |= S_IMMUTABLE;
560 }
561 fat_save_attrs(inode, de->attr);
562
563 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
564 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
565
566 fat_time_fat2unix(sbi, &mtime, de->time, de->date, 0);
567 inode_set_mtime_to_ts(inode, mtime);
568 inode_set_ctime_to_ts(inode, mtime);
569 if (sbi->options.isvfat) {
570 struct timespec64 atime;
571
572 fat_time_fat2unix(sbi, &atime, 0, de->adate, 0);
573 inode_set_atime_to_ts(inode, atime);
574 fat_time_fat2unix(sbi, &MSDOS_I(inode)->i_crtime, de->ctime,
575 de->cdate, de->ctime_cs);
576 } else
577 inode_set_atime_to_ts(inode, fat_truncate_atime(sbi, &mtime));
578
579 return 0;
580}
581
582static inline void fat_lock_build_inode(struct msdos_sb_info *sbi)
583{
584 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
585 mutex_lock(&sbi->nfs_build_inode_lock);
586}
587
588static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi)
589{
590 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
591 mutex_unlock(&sbi->nfs_build_inode_lock);
592}
593
594struct inode *fat_build_inode(struct super_block *sb,
595 struct msdos_dir_entry *de, loff_t i_pos)
596{
597 struct inode *inode;
598 int err;
599
600 fat_lock_build_inode(MSDOS_SB(sb));
601 inode = fat_iget(sb, i_pos);
602 if (inode)
603 goto out;
604 inode = new_inode(sb);
605 if (!inode) {
606 inode = ERR_PTR(-ENOMEM);
607 goto out;
608 }
609 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
610 inode_set_iversion(inode, 1);
611 err = fat_fill_inode(inode, de);
612 if (err) {
613 iput(inode);
614 inode = ERR_PTR(err);
615 goto out;
616 }
617 fat_attach(inode, i_pos);
618 insert_inode_hash(inode);
619out:
620 fat_unlock_build_inode(MSDOS_SB(sb));
621 return inode;
622}
623
624EXPORT_SYMBOL_GPL(fat_build_inode);
625
626static int __fat_write_inode(struct inode *inode);
627
628static int fat_sync_inode_metadata(struct inode *inode,
629 struct writeback_control *wbc)
630{
631 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
632 struct buffer_head *bh;
633 loff_t i_pos;
634 sector_t blocknr;
635 int offset;
636
637 /* The root directory has no directory entry of its own. */
638 if (inode->i_ino == MSDOS_ROOT_INO)
639 goto sync_bhs;
640 i_pos = fat_i_pos_read(sbi, inode);
641 if (!i_pos)
642 goto sync_bhs;
643
644 fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
645 bh = sb_find_get_block_nonatomic(inode->i_sb, blocknr);
646 /*
647 * Buffer present? We leave buffer_dirty check for sync_dirty_buffer()
648 * for proper synchronization with ongoing IO.
649 */
650 if (bh && buffer_uptodate(bh)) {
651 sync_dirty_buffer(bh);
652 if (buffer_write_io_error(bh)) {
653 brelse(bh);
654 return -EIO;
655 }
656 }
657 brelse(bh);
658sync_bhs:
659 return mmb_sync(&MSDOS_I(inode)->i_metadata_bhs);
660}
661
662static void fat_free_eofblocks(struct inode *inode)
663{
664 /* Release unwritten fallocated blocks on inode eviction. */
665 if ((inode->i_blocks << 9) >
666 round_up(MSDOS_I(inode)->mmu_private,
667 MSDOS_SB(inode->i_sb)->cluster_size)) {
668 int err;
669
670 fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
671 /* Fallocate results in updating the i_start/iogstart
672 * for the zero byte file. So, make it return to
673 * original state during evict and commit it to avoid
674 * any corruption on the next access to the cluster
675 * chain for the file.
676 */
677 err = sync_inode_metadata(inode, inode_needs_sync(inode));
678 if (err) {
679 fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
680 "update on disk inode for unused "
681 "fallocated blocks, inode could be "
682 "corrupted. Please run fsck");
683 }
684
685 }
686}
687
688static void fat_evict_inode(struct inode *inode)
689{
690 truncate_inode_pages_final(&inode->i_data);
691 if (!inode->i_nlink) {
692 inode->i_size = 0;
693 fat_truncate_blocks(inode, 0);
694 } else {
695 mmb_sync(&MSDOS_I(inode)->i_metadata_bhs);
696 fat_free_eofblocks(inode);
697 }
698
699 mmb_invalidate(&MSDOS_I(inode)->i_metadata_bhs);
700 clear_inode(inode);
701 fat_cache_inval_inode(inode);
702 fat_detach(inode);
703}
704
705static void fat_set_state(struct super_block *sb,
706 unsigned int set, unsigned int force)
707{
708 struct buffer_head *bh;
709 struct fat_boot_sector *b;
710 struct msdos_sb_info *sbi = MSDOS_SB(sb);
711
712 /* do not change any thing if mounted read only */
713 if (sb_rdonly(sb) && !force)
714 return;
715
716 /* do not change state if fs was dirty */
717 if (sbi->dirty) {
718 /* warn only on set (mount). */
719 if (set)
720 fat_msg(sb, KERN_WARNING, "Volume was not properly "
721 "unmounted. Some data may be corrupt. "
722 "Please run fsck.");
723 return;
724 }
725
726 bh = sb_bread(sb, 0);
727 if (bh == NULL) {
728 fat_msg(sb, KERN_ERR, "unable to read boot sector "
729 "to mark fs as dirty");
730 return;
731 }
732
733 b = (struct fat_boot_sector *) bh->b_data;
734
735 if (is_fat32(sbi)) {
736 if (set)
737 b->fat32.state |= FAT_STATE_DIRTY;
738 else
739 b->fat32.state &= ~FAT_STATE_DIRTY;
740 } else /* fat 16 and 12 */ {
741 if (set)
742 b->fat16.state |= FAT_STATE_DIRTY;
743 else
744 b->fat16.state &= ~FAT_STATE_DIRTY;
745 }
746
747 mark_buffer_dirty(bh);
748 sync_dirty_buffer(bh);
749 brelse(bh);
750}
751
752static void fat_reset_iocharset(struct fat_mount_options *opts)
753{
754 if (opts->iocharset != fat_default_iocharset) {
755 /* Note: opts->iocharset can be NULL here */
756 kfree(opts->iocharset);
757 opts->iocharset = fat_default_iocharset;
758 }
759}
760
761static void delayed_free(struct rcu_head *p)
762{
763 struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
764 unload_nls(sbi->nls_disk);
765 unload_nls(sbi->nls_io);
766 fat_reset_iocharset(&sbi->options);
767 kfree(sbi);
768}
769
770static void fat_put_super(struct super_block *sb)
771{
772 struct msdos_sb_info *sbi = MSDOS_SB(sb);
773
774 fat_set_state(sb, 0, 0);
775
776 iput(sbi->fsinfo_inode);
777 iput(sbi->fat_inode);
778
779 call_rcu(&sbi->rcu, delayed_free);
780}
781
782static struct kmem_cache *fat_inode_cachep;
783
784static struct inode *fat_alloc_inode(struct super_block *sb)
785{
786 struct msdos_inode_info *ei;
787 ei = alloc_inode_sb(sb, fat_inode_cachep, GFP_NOFS);
788 if (!ei)
789 return NULL;
790
791 init_rwsem(&ei->truncate_lock);
792 /* Zeroing to allow iput() even if partial initialized inode. */
793 ei->mmu_private = 0;
794 ei->i_start = 0;
795 ei->i_logstart = 0;
796 ei->i_attrs = 0;
797 ei->i_pos = 0;
798 ei->i_crtime.tv_sec = 0;
799 ei->i_crtime.tv_nsec = 0;
800 mmb_init(&ei->i_metadata_bhs, &ei->vfs_inode.i_data);
801
802 return &ei->vfs_inode;
803}
804
805static void fat_free_inode(struct inode *inode)
806{
807 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
808}
809
810static void init_once(void *foo)
811{
812 struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
813
814 spin_lock_init(&ei->cache_lru_lock);
815 ei->nr_caches = 0;
816 ei->cache_valid_id = FAT_CACHE_VALID + 1;
817 INIT_LIST_HEAD(&ei->cache_lru);
818 INIT_HLIST_NODE(&ei->i_fat_hash);
819 INIT_HLIST_NODE(&ei->i_dir_hash);
820 inode_init_once(&ei->vfs_inode);
821}
822
823static int __init fat_init_inodecache(void)
824{
825 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
826 sizeof(struct msdos_inode_info),
827 0, (SLAB_RECLAIM_ACCOUNT|
828 SLAB_ACCOUNT),
829 init_once);
830 if (fat_inode_cachep == NULL)
831 return -ENOMEM;
832 return 0;
833}
834
835static void __exit fat_destroy_inodecache(void)
836{
837 /*
838 * Make sure all delayed rcu free inodes are flushed before we
839 * destroy cache.
840 */
841 rcu_barrier();
842 kmem_cache_destroy(fat_inode_cachep);
843}
844
845int fat_reconfigure(struct fs_context *fc)
846{
847 bool new_rdonly;
848 struct super_block *sb = fc->root->d_sb;
849 struct msdos_sb_info *sbi = MSDOS_SB(sb);
850 fc->sb_flags |= SB_NODIRATIME | (sbi->options.isvfat ? 0 : SB_NOATIME);
851
852 sync_filesystem(sb);
853
854 /* make sure we update state on remount. */
855 new_rdonly = fc->sb_flags & SB_RDONLY;
856 if (new_rdonly != sb_rdonly(sb)) {
857 if (new_rdonly)
858 fat_set_state(sb, 0, 0);
859 else
860 fat_set_state(sb, 1, 1);
861 }
862 return 0;
863}
864EXPORT_SYMBOL_GPL(fat_reconfigure);
865
866static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
867{
868 struct super_block *sb = dentry->d_sb;
869 struct msdos_sb_info *sbi = MSDOS_SB(sb);
870 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
871
872 /* If the count of free cluster is still unknown, counts it here. */
873 if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
874 int err = fat_count_free_clusters(dentry->d_sb);
875 if (err)
876 return err;
877 }
878
879 buf->f_type = dentry->d_sb->s_magic;
880 buf->f_bsize = sbi->cluster_size;
881 buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
882 buf->f_bfree = sbi->free_clusters;
883 buf->f_bavail = sbi->free_clusters;
884 buf->f_fsid = u64_to_fsid(id);
885 buf->f_namelen =
886 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
887
888 return 0;
889}
890
891static int __fat_write_inode(struct inode *inode)
892{
893 struct super_block *sb = inode->i_sb;
894 struct msdos_sb_info *sbi = MSDOS_SB(sb);
895 struct buffer_head *bh;
896 struct msdos_dir_entry *raw_entry;
897 struct timespec64 mtime;
898 loff_t i_pos;
899 sector_t blocknr;
900 int offset;
901
902 if (inode->i_ino == MSDOS_ROOT_INO) {
903 /* No entry to update but the metadata bh list may need syncing. */
904 set_inode_metadata_writeback(inode);
905 return 0;
906 }
907
908retry:
909 i_pos = fat_i_pos_read(sbi, inode);
910 if (!i_pos)
911 return 0;
912
913 fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
914 bh = sb_bread(sb, blocknr);
915 if (!bh) {
916 fat_msg(sb, KERN_ERR, "unable to read inode block "
917 "for updating (i_pos %lld)", i_pos);
918 return -EIO;
919 }
920 spin_lock(&sbi->inode_hash_lock);
921 if (i_pos != MSDOS_I(inode)->i_pos) {
922 spin_unlock(&sbi->inode_hash_lock);
923 brelse(bh);
924 goto retry;
925 }
926
927 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
928 if (S_ISDIR(inode->i_mode))
929 raw_entry->size = 0;
930 else
931 raw_entry->size = cpu_to_le32(inode->i_size);
932 raw_entry->attr = fat_make_attrs(inode);
933 fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
934 mtime = inode_get_mtime(inode);
935 fat_time_unix2fat(sbi, &mtime, &raw_entry->time,
936 &raw_entry->date, NULL);
937 if (sbi->options.isvfat) {
938 struct timespec64 ts = inode_get_atime(inode);
939 __le16 atime;
940
941 fat_time_unix2fat(sbi, &ts, &atime, &raw_entry->adate, NULL);
942 fat_time_unix2fat(sbi, &MSDOS_I(inode)->i_crtime, &raw_entry->ctime,
943 &raw_entry->cdate, &raw_entry->ctime_cs);
944 }
945 spin_unlock(&sbi->inode_hash_lock);
946 mark_buffer_dirty(bh);
947 brelse(bh);
948 set_inode_metadata_writeback(inode);
949 return 0;
950}
951
952static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
953{
954 int err;
955
956 if (inode->i_ino == MSDOS_FSINFO_INO) {
957 struct super_block *sb = inode->i_sb;
958
959 mutex_lock(&MSDOS_SB(sb)->s_lock);
960 err = fat_clusters_flush(sb);
961 mutex_unlock(&MSDOS_SB(sb)->s_lock);
962 } else
963 err = __fat_write_inode(inode);
964
965 return err;
966}
967
968static int fat_show_options(struct seq_file *m, struct dentry *root);
969static const struct super_operations fat_sops = {
970 .alloc_inode = fat_alloc_inode,
971 .free_inode = fat_free_inode,
972 .write_inode = fat_write_inode,
973 .sync_inode_metadata = fat_sync_inode_metadata,
974 .evict_inode = fat_evict_inode,
975 .put_super = fat_put_super,
976 .statfs = fat_statfs,
977 .show_options = fat_show_options,
978};
979
980static int fat_show_options(struct seq_file *m, struct dentry *root)
981{
982 struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
983 struct fat_mount_options *opts = &sbi->options;
984 int isvfat = opts->isvfat;
985
986 if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
987 seq_printf(m, ",uid=%u",
988 from_kuid_munged(&init_user_ns, opts->fs_uid));
989 if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
990 seq_printf(m, ",gid=%u",
991 from_kgid_munged(&init_user_ns, opts->fs_gid));
992 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
993 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
994 if (opts->allow_utime)
995 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
996 if (sbi->nls_disk)
997 /* strip "cp" prefix from displayed option */
998 seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
999 if (isvfat) {
1000 if (sbi->nls_io)
1001 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
1002
1003 switch (opts->shortname) {
1004 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
1005 seq_puts(m, ",shortname=win95");
1006 break;
1007 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
1008 seq_puts(m, ",shortname=winnt");
1009 break;
1010 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
1011 seq_puts(m, ",shortname=mixed");
1012 break;
1013 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
1014 seq_puts(m, ",shortname=lower");
1015 break;
1016 default:
1017 seq_puts(m, ",shortname=unknown");
1018 break;
1019 }
1020 }
1021 if (opts->name_check != 'n')
1022 seq_printf(m, ",check=%c", opts->name_check);
1023 if (opts->usefree)
1024 seq_puts(m, ",usefree");
1025 if (opts->quiet)
1026 seq_puts(m, ",quiet");
1027 if (opts->showexec)
1028 seq_puts(m, ",showexec");
1029 if (opts->sys_immutable)
1030 seq_puts(m, ",sys_immutable");
1031 if (!isvfat) {
1032 if (opts->dotsOK)
1033 seq_puts(m, ",dotsOK=yes");
1034 if (opts->nocase)
1035 seq_puts(m, ",nocase");
1036 } else {
1037 if (opts->utf8)
1038 seq_puts(m, ",utf8");
1039 if (opts->unicode_xlate)
1040 seq_puts(m, ",uni_xlate");
1041 if (!opts->numtail)
1042 seq_puts(m, ",nonumtail");
1043 if (opts->rodir)
1044 seq_puts(m, ",rodir");
1045 }
1046 if (opts->flush)
1047 seq_puts(m, ",flush");
1048 if (opts->tz_set) {
1049 if (opts->time_offset)
1050 seq_printf(m, ",time_offset=%d", opts->time_offset);
1051 else
1052 seq_puts(m, ",tz=UTC");
1053 }
1054 if (opts->errors == FAT_ERRORS_CONT)
1055 seq_puts(m, ",errors=continue");
1056 else if (opts->errors == FAT_ERRORS_PANIC)
1057 seq_puts(m, ",errors=panic");
1058 else
1059 seq_puts(m, ",errors=remount-ro");
1060 if (opts->nfs == FAT_NFS_NOSTALE_RO)
1061 seq_puts(m, ",nfs=nostale_ro");
1062 else if (opts->nfs)
1063 seq_puts(m, ",nfs=stale_rw");
1064 if (opts->discard)
1065 seq_puts(m, ",discard");
1066 if (opts->dos1xfloppy)
1067 seq_puts(m, ",dos1xfloppy");
1068
1069 return 0;
1070}
1071
1072enum {
1073 Opt_check, Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask,
1074 Opt_allow_utime, Opt_codepage, Opt_usefree, Opt_nocase, Opt_quiet,
1075 Opt_showexec, Opt_debug, Opt_immutable, Opt_dots, Opt_dotsOK,
1076 Opt_charset, Opt_shortname, Opt_utf8, Opt_utf8_bool,
1077 Opt_uni_xl, Opt_uni_xl_bool, Opt_nonumtail, Opt_nonumtail_bool,
1078 Opt_obsolete, Opt_flush, Opt_tz, Opt_rodir, Opt_errors, Opt_discard,
1079 Opt_nfs, Opt_nfs_enum, Opt_time_offset, Opt_dos1xfloppy,
1080};
1081
1082static const struct constant_table fat_param_check[] = {
1083 {"relaxed", 'r'},
1084 {"r", 'r'},
1085 {"strict", 's'},
1086 {"s", 's'},
1087 {"normal", 'n'},
1088 {"n", 'n'},
1089 {}
1090};
1091
1092static const struct constant_table fat_param_tz[] = {
1093 {"UTC", 0},
1094 {}
1095};
1096
1097static const struct constant_table fat_param_errors[] = {
1098 {"continue", FAT_ERRORS_CONT},
1099 {"panic", FAT_ERRORS_PANIC},
1100 {"remount-ro", FAT_ERRORS_RO},
1101 {}
1102};
1103
1104
1105static const struct constant_table fat_param_nfs[] = {
1106 {"stale_rw", FAT_NFS_STALE_RW},
1107 {"nostale_ro", FAT_NFS_NOSTALE_RO},
1108 {}
1109};
1110
1111/*
1112 * These are all obsolete but we still reject invalid options.
1113 * The corresponding values are therefore meaningless.
1114 */
1115static const struct constant_table fat_param_conv[] = {
1116 {"binary", 0},
1117 {"text", 0},
1118 {"auto", 0},
1119 {"b", 0},
1120 {"t", 0},
1121 {"a", 0},
1122 {}
1123};
1124
1125/* Core options. See below for vfat and msdos extras */
1126const struct fs_parameter_spec fat_param_spec[] = {
1127 fsparam_enum ("check", Opt_check, fat_param_check),
1128 fsparam_uid ("uid", Opt_uid),
1129 fsparam_gid ("gid", Opt_gid),
1130 fsparam_u32oct ("umask", Opt_umask),
1131 fsparam_u32oct ("dmask", Opt_dmask),
1132 fsparam_u32oct ("fmask", Opt_fmask),
1133 fsparam_u32oct ("allow_utime", Opt_allow_utime),
1134 fsparam_u32 ("codepage", Opt_codepage),
1135 fsparam_flag ("usefree", Opt_usefree),
1136 fsparam_flag ("nocase", Opt_nocase),
1137 fsparam_flag ("quiet", Opt_quiet),
1138 fsparam_flag ("showexec", Opt_showexec),
1139 fsparam_flag ("debug", Opt_debug),
1140 fsparam_flag ("sys_immutable", Opt_immutable),
1141 fsparam_flag ("flush", Opt_flush),
1142 fsparam_enum ("tz", Opt_tz, fat_param_tz),
1143 fsparam_s32 ("time_offset", Opt_time_offset),
1144 fsparam_enum ("errors", Opt_errors, fat_param_errors),
1145 fsparam_flag ("discard", Opt_discard),
1146 fsparam_flag ("nfs", Opt_nfs),
1147 fsparam_enum ("nfs", Opt_nfs_enum, fat_param_nfs),
1148 fsparam_flag ("dos1xfloppy", Opt_dos1xfloppy),
1149 __fsparam(fs_param_is_enum, "conv",
1150 Opt_obsolete, fs_param_deprecated, fat_param_conv),
1151 __fsparam(fs_param_is_u32, "fat",
1152 Opt_obsolete, fs_param_deprecated, NULL),
1153 __fsparam(fs_param_is_u32, "blocksize",
1154 Opt_obsolete, fs_param_deprecated, NULL),
1155 __fsparam(fs_param_is_string, "cvf_format",
1156 Opt_obsolete, fs_param_deprecated, NULL),
1157 __fsparam(fs_param_is_string, "cvf_options",
1158 Opt_obsolete, fs_param_deprecated, NULL),
1159 __fsparam(NULL, "posix",
1160 Opt_obsolete, fs_param_deprecated, NULL),
1161 {}
1162};
1163EXPORT_SYMBOL_GPL(fat_param_spec);
1164
1165static const struct fs_parameter_spec msdos_param_spec[] = {
1166 fsparam_flag_no ("dots", Opt_dots),
1167 fsparam_bool ("dotsOK", Opt_dotsOK),
1168 {}
1169};
1170
1171static const struct constant_table fat_param_shortname[] = {
1172 {"lower", VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95},
1173 {"win95", VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95},
1174 {"winnt", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT},
1175 {"mixed", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95},
1176 {}
1177};
1178
1179static const struct fs_parameter_spec vfat_param_spec[] = {
1180 fsparam_string ("iocharset", Opt_charset),
1181 fsparam_enum ("shortname", Opt_shortname, fat_param_shortname),
1182 fsparam_flag ("utf8", Opt_utf8),
1183 fsparam_bool ("utf8", Opt_utf8_bool),
1184 fsparam_flag ("uni_xlate", Opt_uni_xl),
1185 fsparam_bool ("uni_xlate", Opt_uni_xl_bool),
1186 fsparam_flag ("nonumtail", Opt_nonumtail),
1187 fsparam_bool ("nonumtail", Opt_nonumtail_bool),
1188 fsparam_flag ("rodir", Opt_rodir),
1189 {}
1190};
1191
1192int fat_parse_param(struct fs_context *fc, struct fs_parameter *param,
1193 bool is_vfat)
1194{
1195 struct fat_mount_options *opts = fc->fs_private;
1196 struct fs_parse_result result;
1197 int opt;
1198
1199 /* remount options have traditionally been ignored */
1200 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1201 return 0;
1202
1203 opt = fs_parse(fc, fat_param_spec, param, &result);
1204 /* If option not found in fat_param_spec, try vfat/msdos options */
1205 if (opt == -ENOPARAM) {
1206 if (is_vfat)
1207 opt = fs_parse(fc, vfat_param_spec, param, &result);
1208 else
1209 opt = fs_parse(fc, msdos_param_spec, param, &result);
1210 }
1211
1212 if (opt < 0)
1213 return opt;
1214
1215 switch (opt) {
1216 case Opt_check:
1217 opts->name_check = result.uint_32;
1218 break;
1219 case Opt_usefree:
1220 opts->usefree = 1;
1221 break;
1222 case Opt_nocase:
1223 if (!is_vfat)
1224 opts->nocase = 1;
1225 else {
1226 /* for backward compatibility */
1227 opts->shortname = VFAT_SFN_DISPLAY_WIN95
1228 | VFAT_SFN_CREATE_WIN95;
1229 }
1230 break;
1231 case Opt_quiet:
1232 opts->quiet = 1;
1233 break;
1234 case Opt_showexec:
1235 opts->showexec = 1;
1236 break;
1237 case Opt_debug:
1238 opts->debug = 1;
1239 break;
1240 case Opt_immutable:
1241 opts->sys_immutable = 1;
1242 break;
1243 case Opt_uid:
1244 opts->fs_uid = result.uid;
1245 break;
1246 case Opt_gid:
1247 opts->fs_gid = result.gid;
1248 break;
1249 case Opt_umask:
1250 opts->fs_fmask = opts->fs_dmask = result.uint_32;
1251 break;
1252 case Opt_dmask:
1253 opts->fs_dmask = result.uint_32;
1254 break;
1255 case Opt_fmask:
1256 opts->fs_fmask = result.uint_32;
1257 break;
1258 case Opt_allow_utime:
1259 opts->allow_utime = result.uint_32 & (S_IWGRP | S_IWOTH);
1260 break;
1261 case Opt_codepage:
1262 opts->codepage = result.uint_32;
1263 break;
1264 case Opt_flush:
1265 opts->flush = 1;
1266 break;
1267 case Opt_time_offset:
1268 /*
1269 * GMT+-12 zones may have DST corrections so at least
1270 * 13 hours difference is needed. Make the limit 24
1271 * just in case someone invents something unusual.
1272 */
1273 if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
1274 return -EINVAL;
1275 opts->tz_set = 1;
1276 opts->time_offset = result.int_32;
1277 break;
1278 case Opt_tz:
1279 opts->tz_set = 1;
1280 opts->time_offset = result.uint_32;
1281 break;
1282 case Opt_errors:
1283 opts->errors = result.uint_32;
1284 break;
1285 case Opt_nfs:
1286 opts->nfs = FAT_NFS_STALE_RW;
1287 break;
1288 case Opt_nfs_enum:
1289 opts->nfs = result.uint_32;
1290 break;
1291 case Opt_dos1xfloppy:
1292 opts->dos1xfloppy = 1;
1293 break;
1294
1295 /* msdos specific */
1296 case Opt_dots: /* dots / nodots */
1297 opts->dotsOK = !result.negated;
1298 break;
1299 case Opt_dotsOK: /* dotsOK = yes/no */
1300 opts->dotsOK = result.boolean;
1301 break;
1302
1303 /* vfat specific */
1304 case Opt_charset:
1305 fat_reset_iocharset(opts);
1306 opts->iocharset = param->string;
1307 param->string = NULL; /* Steal string */
1308 break;
1309 case Opt_shortname:
1310 opts->shortname = result.uint_32;
1311 break;
1312 case Opt_utf8:
1313 opts->utf8 = 1;
1314 break;
1315 case Opt_utf8_bool:
1316 opts->utf8 = result.boolean;
1317 break;
1318 case Opt_uni_xl:
1319 opts->unicode_xlate = 1;
1320 break;
1321 case Opt_uni_xl_bool:
1322 opts->unicode_xlate = result.boolean;
1323 break;
1324 case Opt_nonumtail:
1325 opts->numtail = 0; /* negated option */
1326 break;
1327 case Opt_nonumtail_bool:
1328 opts->numtail = !result.boolean; /* negated option */
1329 break;
1330 case Opt_rodir:
1331 opts->rodir = 1;
1332 break;
1333 case Opt_discard:
1334 opts->discard = 1;
1335 break;
1336
1337 /* obsolete mount options */
1338 case Opt_obsolete:
1339 printk(KERN_INFO "FAT-fs: \"%s\" option is obsolete, "
1340 "not supported now", param->key);
1341 break;
1342 default:
1343 return -EINVAL;
1344 }
1345
1346 return 0;
1347}
1348EXPORT_SYMBOL_GPL(fat_parse_param);
1349
1350static int fat_read_root(struct inode *inode)
1351{
1352 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
1353 int error;
1354
1355 MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
1356 inode->i_uid = sbi->options.fs_uid;
1357 inode->i_gid = sbi->options.fs_gid;
1358 inode_inc_iversion(inode);
1359 inode->i_generation = 0;
1360 inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
1361 inode->i_op = sbi->dir_ops;
1362 inode->i_fop = &fat_dir_operations;
1363 if (is_fat32(sbi)) {
1364 MSDOS_I(inode)->i_start = sbi->root_cluster;
1365 error = fat_calc_dir_size(inode);
1366 if (error < 0)
1367 return error;
1368 } else {
1369 MSDOS_I(inode)->i_start = 0;
1370 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
1371 }
1372 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1373 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1374 MSDOS_I(inode)->i_logstart = 0;
1375 MSDOS_I(inode)->mmu_private = inode->i_size;
1376
1377 fat_save_attrs(inode, ATTR_DIR);
1378 inode_set_mtime_to_ts(inode,
1379 inode_set_atime_to_ts(inode, inode_set_ctime(inode, 0, 0)));
1380 set_nlink(inode, fat_subdirs(inode)+2);
1381
1382 return 0;
1383}
1384
1385static unsigned long calc_fat_clusters(struct super_block *sb)
1386{
1387 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1388
1389 /* Divide first to avoid overflow */
1390 if (!is_fat12(sbi)) {
1391 unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
1392 return ent_per_sec * sbi->fat_length;
1393 }
1394
1395 return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
1396}
1397
1398static bool fat_bpb_is_zero(struct fat_boot_sector *b)
1399{
1400 if (get_unaligned_le16(&b->sector_size))
1401 return false;
1402 if (b->sec_per_clus)
1403 return false;
1404 if (b->reserved)
1405 return false;
1406 if (b->fats)
1407 return false;
1408 if (get_unaligned_le16(&b->dir_entries))
1409 return false;
1410 if (get_unaligned_le16(&b->sectors))
1411 return false;
1412 if (b->media)
1413 return false;
1414 if (b->fat_length)
1415 return false;
1416 if (b->secs_track)
1417 return false;
1418 if (b->heads)
1419 return false;
1420 return true;
1421}
1422
1423static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b,
1424 int silent, struct fat_bios_param_block *bpb)
1425{
1426 int error = -EINVAL;
1427
1428 /* Read in BPB ... */
1429 memset(bpb, 0, sizeof(*bpb));
1430 bpb->fat_sector_size = get_unaligned_le16(&b->sector_size);
1431 bpb->fat_sec_per_clus = b->sec_per_clus;
1432 bpb->fat_reserved = le16_to_cpu(b->reserved);
1433 bpb->fat_fats = b->fats;
1434 bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries);
1435 bpb->fat_sectors = get_unaligned_le16(&b->sectors);
1436 bpb->fat_fat_length = le16_to_cpu(b->fat_length);
1437 bpb->fat_total_sect = le32_to_cpu(b->total_sect);
1438
1439 bpb->fat16_state = b->fat16.state;
1440 bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id);
1441
1442 bpb->fat32_length = le32_to_cpu(b->fat32.length);
1443 bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster);
1444 bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector);
1445 bpb->fat32_state = b->fat32.state;
1446 bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id);
1447
1448 /* Validate this looks like a FAT filesystem BPB */
1449 if (!bpb->fat_reserved) {
1450 if (!silent)
1451 fat_msg(sb, KERN_ERR,
1452 "bogus number of reserved sectors");
1453 goto out;
1454 }
1455 if (!bpb->fat_fats) {
1456 if (!silent)
1457 fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1458 goto out;
1459 }
1460
1461 /*
1462 * Earlier we checked here that b->secs_track and b->head are nonzero,
1463 * but it turns out valid FAT filesystems can have zero there.
1464 */
1465
1466 if (!fat_valid_media(b->media)) {
1467 if (!silent)
1468 fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
1469 (unsigned)b->media);
1470 goto out;
1471 }
1472
1473 if (!is_power_of_2(bpb->fat_sector_size)
1474 || (bpb->fat_sector_size < 512)
1475 || (bpb->fat_sector_size > 4096)) {
1476 if (!silent)
1477 fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
1478 (unsigned)bpb->fat_sector_size);
1479 goto out;
1480 }
1481
1482 if (!is_power_of_2(bpb->fat_sec_per_clus)) {
1483 if (!silent)
1484 fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
1485 (unsigned)bpb->fat_sec_per_clus);
1486 goto out;
1487 }
1488
1489 if (bpb->fat_fat_length == 0 && bpb->fat32_length == 0) {
1490 if (!silent)
1491 fat_msg(sb, KERN_ERR, "bogus number of FAT sectors");
1492 goto out;
1493 }
1494
1495 error = 0;
1496
1497out:
1498 return error;
1499}
1500
1501static int fat_read_static_bpb(struct super_block *sb,
1502 struct fat_boot_sector *b, int silent,
1503 struct fat_bios_param_block *bpb)
1504{
1505 static const char *notdos1x = "This doesn't look like a DOS 1.x volume";
1506 sector_t bd_sects = bdev_nr_sectors(sb->s_bdev);
1507 struct fat_floppy_defaults *fdefaults = NULL;
1508 int error = -EINVAL;
1509 unsigned i;
1510
1511 /* 16-bit DOS 1.x reliably wrote bootstrap short-jmp or near-jmp code */
1512 if ((b->ignored[0] != 0xeb || b->ignored[2] != 0x90) &&
1513 (b->ignored[0] != 0xe9)) {
1514 if (!silent)
1515 fat_msg(sb, KERN_ERR,
1516 "%s; no bootstrapping code", notdos1x);
1517 goto out;
1518 }
1519
1520 /*
1521 * If any value in this region is non-zero, it isn't archaic
1522 * DOS.
1523 */
1524 if (!fat_bpb_is_zero(b)) {
1525 if (!silent)
1526 fat_msg(sb, KERN_ERR,
1527 "%s; DOS 2.x BPB is non-zero", notdos1x);
1528 goto out;
1529 }
1530
1531 for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) {
1532 if (floppy_defaults[i].nr_sectors == bd_sects) {
1533 fdefaults = &floppy_defaults[i];
1534 break;
1535 }
1536 }
1537
1538 if (fdefaults == NULL) {
1539 if (!silent)
1540 fat_msg(sb, KERN_WARNING,
1541 "This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)",
1542 (u64)bd_sects);
1543 goto out;
1544 }
1545
1546 if (!silent)
1547 fat_msg(sb, KERN_INFO,
1548 "This looks like a DOS 1.x volume; assuming default BPB values");
1549
1550 memset(bpb, 0, sizeof(*bpb));
1551 bpb->fat_sector_size = SECTOR_SIZE;
1552 bpb->fat_sec_per_clus = fdefaults->sec_per_clus;
1553 bpb->fat_reserved = 1;
1554 bpb->fat_fats = 2;
1555 bpb->fat_dir_entries = fdefaults->dir_entries;
1556 bpb->fat_sectors = fdefaults->nr_sectors;
1557 bpb->fat_fat_length = fdefaults->fat_length;
1558
1559 error = 0;
1560
1561out:
1562 return error;
1563}
1564
1565/*
1566 * Read the super block of an MS-DOS FS.
1567 */
1568int fat_fill_super(struct super_block *sb, struct fs_context *fc,
1569 void (*setup)(struct super_block *))
1570{
1571 struct fat_mount_options *opts = fc->fs_private;
1572 int silent = fc->sb_flags & SB_SILENT;
1573 struct inode *root_inode = NULL, *fat_inode = NULL;
1574 struct inode *fsinfo_inode = NULL;
1575 struct buffer_head *bh;
1576 struct fat_bios_param_block bpb;
1577 struct msdos_sb_info *sbi;
1578 u16 logical_sector_size;
1579 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
1580 long error;
1581 char buf[50];
1582 struct timespec64 ts;
1583
1584 /*
1585 * GFP_KERNEL is ok here, because while we do hold the
1586 * superblock lock, memory pressure can't call back into
1587 * the filesystem, since we're only just about to mount
1588 * it and have no inodes etc active!
1589 */
1590 sbi = kzalloc_obj(struct msdos_sb_info);
1591 if (!sbi)
1592 return -ENOMEM;
1593 sb->s_fs_info = sbi;
1594
1595 sb->s_flags |= SB_NODIRATIME;
1596 sb->s_magic = MSDOS_SUPER_MAGIC;
1597 sb->s_op = &fat_sops;
1598 sb->s_export_op = &fat_export_ops;
1599 /*
1600 * fat timestamps are complex and truncated by fat itself, so
1601 * we set 1 here to be fast
1602 */
1603 sb->s_time_gran = 1;
1604 mutex_init(&sbi->nfs_build_inode_lock);
1605 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1606 DEFAULT_RATELIMIT_BURST);
1607
1608 /* UTF-8 doesn't provide FAT semantics */
1609 if (!strcmp(opts->iocharset, "utf8")) {
1610 fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
1611 " for FAT filesystems, filesystem will be"
1612 " case sensitive!");
1613 }
1614
1615 /* If user doesn't specify allow_utime, it's initialized from dmask. */
1616 if (opts->allow_utime == (unsigned short)-1)
1617 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
1618 if (opts->unicode_xlate)
1619 opts->utf8 = 0;
1620 if (opts->nfs == FAT_NFS_NOSTALE_RO) {
1621 sb->s_flags |= SB_RDONLY;
1622 sb->s_export_op = &fat_export_ops_nostale;
1623 }
1624
1625 /* Apply parsed options to sbi (structure copy) */
1626 sbi->options = *opts;
1627 /* Transfer ownership of iocharset to sbi->options */
1628 opts->iocharset = NULL;
1629
1630 setup(sb); /* flavour-specific stuff that needs options */
1631
1632 error = -EINVAL;
1633 if (!sb_min_blocksize(sb, 512)) {
1634 fat_msg(sb, KERN_ERR, "unable to set blocksize");
1635 goto out_fail;
1636 }
1637 error = -EIO;
1638 bh = sb_bread(sb, 0);
1639 if (bh == NULL) {
1640 fat_msg(sb, KERN_ERR, "unable to read boot sector");
1641 goto out_fail;
1642 }
1643
1644 error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent,
1645 &bpb);
1646 if (error == -EINVAL && sbi->options.dos1xfloppy)
1647 error = fat_read_static_bpb(sb,
1648 (struct fat_boot_sector *)bh->b_data, silent, &bpb);
1649 brelse(bh);
1650
1651 if (error == -EINVAL)
1652 goto out_invalid;
1653 else if (error)
1654 goto out_fail;
1655
1656 logical_sector_size = bpb.fat_sector_size;
1657 sbi->sec_per_clus = bpb.fat_sec_per_clus;
1658
1659 error = -EIO;
1660 if (logical_sector_size < sb->s_blocksize) {
1661 fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1662 " (logical sector size = %u)", logical_sector_size);
1663 goto out_fail;
1664 }
1665
1666 if (logical_sector_size > sb->s_blocksize) {
1667 struct buffer_head *bh_resize;
1668
1669 if (!sb_set_blocksize(sb, logical_sector_size)) {
1670 fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
1671 logical_sector_size);
1672 goto out_fail;
1673 }
1674
1675 /* Verify that the larger boot sector is fully readable */
1676 bh_resize = sb_bread(sb, 0);
1677 if (bh_resize == NULL) {
1678 fat_msg(sb, KERN_ERR, "unable to read boot sector"
1679 " (logical sector size = %lu)",
1680 sb->s_blocksize);
1681 goto out_fail;
1682 }
1683 brelse(bh_resize);
1684 }
1685
1686 mutex_init(&sbi->s_lock);
1687 sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
1688 sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
1689 sbi->fats = bpb.fat_fats;
1690 sbi->fat_bits = 0; /* Don't know yet */
1691 sbi->fat_start = bpb.fat_reserved;
1692 sbi->fat_length = bpb.fat_fat_length;
1693 sbi->root_cluster = 0;
1694 sbi->free_clusters = -1; /* Don't know yet */
1695 sbi->free_clus_valid = 0;
1696 sbi->prev_free = FAT_START_ENT;
1697 sb->s_maxbytes = 0xffffffff;
1698 fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0);
1699 sb->s_time_min = ts.tv_sec;
1700
1701 fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX),
1702 cpu_to_le16(FAT_DATE_MAX), 0);
1703 sb->s_time_max = ts.tv_sec;
1704
1705 if (!sbi->fat_length && bpb.fat32_length) {
1706 struct fat_boot_fsinfo *fsinfo;
1707 struct buffer_head *fsinfo_bh;
1708
1709 /* Must be FAT32 */
1710 sbi->fat_bits = 32;
1711 sbi->fat_length = bpb.fat32_length;
1712 sbi->root_cluster = bpb.fat32_root_cluster;
1713
1714 /* MC - if info_sector is 0, don't multiply by 0 */
1715 sbi->fsinfo_sector = bpb.fat32_info_sector;
1716 if (sbi->fsinfo_sector == 0)
1717 sbi->fsinfo_sector = 1;
1718
1719 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
1720 if (fsinfo_bh == NULL) {
1721 fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1722 " (sector = %lu)", sbi->fsinfo_sector);
1723 goto out_fail;
1724 }
1725
1726 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1727 if (!IS_FSINFO(fsinfo)) {
1728 fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1729 "0x%08x, 0x%08x (sector = %lu)",
1730 le32_to_cpu(fsinfo->signature1),
1731 le32_to_cpu(fsinfo->signature2),
1732 sbi->fsinfo_sector);
1733 } else {
1734 if (sbi->options.usefree)
1735 sbi->free_clus_valid = 1;
1736 sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
1737 sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
1738 }
1739
1740 brelse(fsinfo_bh);
1741 }
1742
1743 /* interpret volume ID as a little endian 32 bit integer */
1744 if (is_fat32(sbi))
1745 sbi->vol_id = bpb.fat32_vol_id;
1746 else /* fat 16 or 12 */
1747 sbi->vol_id = bpb.fat16_vol_id;
1748
1749 __le32 vol_id_le = cpu_to_le32(sbi->vol_id);
1750 super_set_uuid(sb, (void *) &vol_id_le, sizeof(vol_id_le));
1751
1752 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
1753 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
1754
1755 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1756 sbi->dir_entries = bpb.fat_dir_entries;
1757 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
1758 if (!silent)
1759 fat_msg(sb, KERN_ERR, "bogus number of directory entries"
1760 " (%u)", sbi->dir_entries);
1761 goto out_invalid;
1762 }
1763
1764 rootdir_sectors = sbi->dir_entries
1765 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
1766 sbi->data_start = sbi->dir_start + rootdir_sectors;
1767 total_sectors = bpb.fat_sectors;
1768 if (total_sectors == 0)
1769 total_sectors = bpb.fat_total_sect;
1770
1771 if (total_sectors < sbi->data_start) {
1772 if (!silent)
1773 fat_msg(sb, KERN_ERR,
1774 "data area starts beyond volume (%lu > %u)",
1775 sbi->data_start, total_sectors);
1776 goto out_invalid;
1777 }
1778
1779 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
1780
1781 if (!is_fat32(sbi))
1782 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
1783
1784 /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1785 if (is_fat32(sbi))
1786 sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
1787 else /* fat 16 or 12 */
1788 sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;
1789
1790 /* check that FAT table does not overflow */
1791 fat_clusters = calc_fat_clusters(sb);
1792 total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
1793 if (total_clusters > max_fat(sb)) {
1794 if (!silent)
1795 fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
1796 total_clusters);
1797 goto out_invalid;
1798 }
1799
1800 sbi->max_cluster = total_clusters + FAT_START_ENT;
1801 /* check the free_clusters, it's not necessarily correct */
1802 if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
1803 sbi->free_clusters = -1;
1804 /* check the prev_free, it's not necessarily correct */
1805 sbi->prev_free %= sbi->max_cluster;
1806 if (sbi->prev_free < FAT_START_ENT)
1807 sbi->prev_free = FAT_START_ENT;
1808
1809 /* set up enough so that it can read an inode */
1810 fat_hash_init(sb);
1811 dir_hash_init(sb);
1812 fat_ent_access_init(sb);
1813
1814 /*
1815 * The low byte of the first FAT entry must have the same value as
1816 * the media field of the boot sector. But in real world, too many
1817 * devices are writing wrong values. So, removed that validity check.
1818 *
1819 * The removed check compared the first FAT entry to a value dependent
1820 * on the media field like this:
1821 * == (0x0F00 | media), for FAT12
1822 * == (0XFF00 | media), for FAT16
1823 * == (0x0FFFFF | media), for FAT32
1824 */
1825
1826 error = -EINVAL;
1827 scnprintf(buf, sizeof(buf), "cp%d", sbi->options.codepage);
1828 sbi->nls_disk = load_nls(buf);
1829 if (!sbi->nls_disk) {
1830 fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
1831 goto out_fail;
1832 }
1833
1834 /* FIXME: utf8 is using iocharset for upper/lower conversion */
1835 if (sbi->options.isvfat) {
1836 sbi->nls_io = load_nls(sbi->options.iocharset);
1837 if (!sbi->nls_io) {
1838 fat_msg(sb, KERN_ERR, "IO charset %s not found",
1839 sbi->options.iocharset);
1840 goto out_fail;
1841 }
1842 }
1843
1844 error = -ENOMEM;
1845 fat_inode = new_inode(sb);
1846 if (!fat_inode)
1847 goto out_fail;
1848 sbi->fat_inode = fat_inode;
1849
1850 fsinfo_inode = new_inode(sb);
1851 if (!fsinfo_inode)
1852 goto out_fail;
1853 fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
1854 sbi->fsinfo_inode = fsinfo_inode;
1855 insert_inode_hash(fsinfo_inode);
1856
1857 root_inode = new_inode(sb);
1858 if (!root_inode)
1859 goto out_fail;
1860 root_inode->i_ino = MSDOS_ROOT_INO;
1861 inode_set_iversion(root_inode, 1);
1862 error = fat_read_root(root_inode);
1863 if (error < 0) {
1864 iput(root_inode);
1865 goto out_fail;
1866 }
1867 error = -ENOMEM;
1868 insert_inode_hash(root_inode);
1869 fat_attach(root_inode, 0);
1870 sb->s_root = d_make_root(root_inode);
1871 if (!sb->s_root) {
1872 fat_msg(sb, KERN_ERR, "get root inode failed");
1873 goto out_fail;
1874 }
1875
1876 if (sbi->options.discard && !bdev_max_discard_sectors(sb->s_bdev))
1877 fat_msg(sb, KERN_WARNING,
1878 "mounting with \"discard\" option, but the device does not support discard");
1879
1880 fat_set_state(sb, 1, 0);
1881 return 0;
1882
1883out_invalid:
1884 error = -EINVAL;
1885 if (!silent)
1886 fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
1887
1888out_fail:
1889 iput(fsinfo_inode);
1890 iput(fat_inode);
1891 unload_nls(sbi->nls_io);
1892 unload_nls(sbi->nls_disk);
1893 fat_reset_iocharset(&sbi->options);
1894 sb->s_fs_info = NULL;
1895 kfree(sbi);
1896 return error;
1897}
1898
1899EXPORT_SYMBOL_GPL(fat_fill_super);
1900
1901/*
1902 * helper function for fat_flush_inodes. This writes both the inode
1903 * and the file data blocks, waiting for in flight data blocks before
1904 * the start of the call. It does not wait for any io started
1905 * during the call
1906 */
1907static int writeback_inode(struct inode *inode)
1908{
1909
1910 int ret;
1911
1912 /* if we used wait=1, sync_inode_metadata waits for the io for the
1913 * inode to finish. So wait=0 is sent down to sync_inode_metadata
1914 * and filemap_fdatawrite is used for the data blocks
1915 */
1916 ret = sync_inode_metadata(inode, 0);
1917 if (!ret)
1918 ret = filemap_fdatawrite(inode->i_mapping);
1919 return ret;
1920}
1921
1922/*
1923 * write data and metadata corresponding to i1 and i2. The io is
1924 * started but we do not wait for any of it to finish.
1925 *
1926 * filemap_flush is used for the block device, so if there is a dirty
1927 * page for a block already in flight, we will not wait and start the
1928 * io over again
1929 */
1930int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1931{
1932 int ret = 0;
1933 if (!MSDOS_SB(sb)->options.flush)
1934 return 0;
1935 if (i1)
1936 ret = writeback_inode(i1);
1937 if (!ret && i2)
1938 ret = writeback_inode(i2);
1939 if (!ret)
1940 ret = sync_blockdev_nowait(sb->s_bdev);
1941 return ret;
1942}
1943EXPORT_SYMBOL_GPL(fat_flush_inodes);
1944
1945int fat_init_fs_context(struct fs_context *fc, bool is_vfat)
1946{
1947 struct fat_mount_options *opts;
1948
1949 opts = kzalloc_obj(*opts);
1950 if (!opts)
1951 return -ENOMEM;
1952
1953 opts->isvfat = is_vfat;
1954 opts->fs_uid = current_uid();
1955 opts->fs_gid = current_gid();
1956 opts->fs_fmask = opts->fs_dmask = current_umask();
1957 opts->allow_utime = -1;
1958 opts->codepage = fat_default_codepage;
1959 fat_reset_iocharset(opts);
1960 if (is_vfat) {
1961 opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
1962 opts->rodir = 0;
1963 } else {
1964 opts->shortname = 0;
1965 opts->rodir = 1;
1966 }
1967 opts->name_check = 'n';
1968 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
1969 opts->unicode_xlate = 0;
1970 opts->numtail = 1;
1971 opts->usefree = opts->nocase = 0;
1972 opts->tz_set = 0;
1973 opts->nfs = 0;
1974 opts->errors = FAT_ERRORS_RO;
1975 opts->debug = 0;
1976
1977 opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat;
1978
1979 fc->fs_private = opts;
1980 /* fc->ops assigned by caller */
1981
1982 return 0;
1983}
1984EXPORT_SYMBOL_GPL(fat_init_fs_context);
1985
1986void fat_free_fc(struct fs_context *fc)
1987{
1988 struct fat_mount_options *opts = fc->fs_private;
1989
1990 if (opts->iocharset != fat_default_iocharset)
1991 kfree(opts->iocharset);
1992 kfree(fc->fs_private);
1993}
1994EXPORT_SYMBOL_GPL(fat_free_fc);
1995
1996static int __init init_fat_fs(void)
1997{
1998 int err;
1999
2000 err = fat_cache_init();
2001 if (err)
2002 return err;
2003
2004 err = fat_init_inodecache();
2005 if (err)
2006 goto failed;
2007
2008 return 0;
2009
2010failed:
2011 fat_cache_destroy();
2012 return err;
2013}
2014
2015static void __exit exit_fat_fs(void)
2016{
2017 fat_cache_destroy();
2018 fat_destroy_inodecache();
2019}
2020
2021module_init(init_fat_fs)
2022module_exit(exit_fat_fs)
2023
2024MODULE_DESCRIPTION("Core FAT filesystem support");
2025MODULE_LICENSE("GPL");