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 */
44 struct 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
64 static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
65 static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
66
67 static 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
fat_add_cluster(struct inode * inode)104 int 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
__fat_get_block(struct inode * inode,sector_t iblock,unsigned long * max_blocks,struct buffer_head * bh_result,int create)119 static 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
fat_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)183 static 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
fat_writepages(struct address_space * mapping,struct writeback_control * wbc)197 static int fat_writepages(struct address_space *mapping,
198 struct writeback_control *wbc)
199 {
200 return mpage_writepages(mapping, wbc, fat_get_block);
201 }
202
fat_read_folio(struct file * file,struct folio * folio)203 static int fat_read_folio(struct file *file, struct folio *folio)
204 {
205 return mpage_read_folio(folio, fat_get_block);
206 }
207
fat_readahead(struct readahead_control * rac)208 static void fat_readahead(struct readahead_control *rac)
209 {
210 mpage_readahead(rac, fat_get_block);
211 }
212
fat_write_failed(struct address_space * mapping,loff_t to)213 static 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
fat_write_begin(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned len,struct folio ** foliop,void ** fsdata)223 static 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
fat_write_end(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct folio * folio,void * fsdata)238 static 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, S_CTIME|S_MTIME);
250 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
251 mark_inode_dirty(inode);
252 }
253 return err;
254 }
255
fat_direct_IO(struct kiocb * iocb,struct iov_iter * iter)256 static 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
fat_get_block_bmap(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)291 static 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
_fat_bmap(struct address_space * mapping,sector_t block)316 static 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 */
fat_block_truncate_page(struct inode * inode,loff_t from)335 int 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
340 static 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
fat_hash_init(struct super_block * sb)377 static 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
fat_hash(loff_t i_pos)387 static inline unsigned long fat_hash(loff_t i_pos)
388 {
389 return hash_32(i_pos, FAT_HASH_BITS);
390 }
391
dir_hash_init(struct super_block * sb)392 static 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
fat_attach(struct inode * inode,loff_t i_pos)402 void 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 }
429 EXPORT_SYMBOL_GPL(fat_attach);
430
fat_detach(struct inode * inode)431 void 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 }
445 EXPORT_SYMBOL_GPL(fat_detach);
446
fat_iget(struct super_block * sb,loff_t i_pos)447 struct 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
is_exec(unsigned char * extension)467 static 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
fat_calc_dir_size(struct inode * inode)477 static 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
fat_validate_dir(struct inode * dir)494 static 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 */
fat_fill_inode(struct inode * inode,struct msdos_dir_entry * de)513 int 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
fat_lock_build_inode(struct msdos_sb_info * sbi)582 static 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
fat_unlock_build_inode(struct msdos_sb_info * sbi)588 static 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
fat_build_inode(struct super_block * sb,struct msdos_dir_entry * de,loff_t i_pos)594 struct 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);
619 out:
620 fat_unlock_build_inode(MSDOS_SB(sb));
621 return inode;
622 }
623
624 EXPORT_SYMBOL_GPL(fat_build_inode);
625
626 static int __fat_write_inode(struct inode *inode, int wait);
627
fat_free_eofblocks(struct inode * inode)628 static void fat_free_eofblocks(struct inode *inode)
629 {
630 /* Release unwritten fallocated blocks on inode eviction. */
631 if ((inode->i_blocks << 9) >
632 round_up(MSDOS_I(inode)->mmu_private,
633 MSDOS_SB(inode->i_sb)->cluster_size)) {
634 int err;
635
636 fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
637 /* Fallocate results in updating the i_start/iogstart
638 * for the zero byte file. So, make it return to
639 * original state during evict and commit it to avoid
640 * any corruption on the next access to the cluster
641 * chain for the file.
642 */
643 err = __fat_write_inode(inode, inode_needs_sync(inode));
644 if (err) {
645 fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
646 "update on disk inode for unused "
647 "fallocated blocks, inode could be "
648 "corrupted. Please run fsck");
649 }
650
651 }
652 }
653
fat_evict_inode(struct inode * inode)654 static void fat_evict_inode(struct inode *inode)
655 {
656 truncate_inode_pages_final(&inode->i_data);
657 if (!inode->i_nlink) {
658 inode->i_size = 0;
659 fat_truncate_blocks(inode, 0);
660 } else
661 fat_free_eofblocks(inode);
662
663 invalidate_inode_buffers(inode);
664 clear_inode(inode);
665 fat_cache_inval_inode(inode);
666 fat_detach(inode);
667 }
668
fat_set_state(struct super_block * sb,unsigned int set,unsigned int force)669 static void fat_set_state(struct super_block *sb,
670 unsigned int set, unsigned int force)
671 {
672 struct buffer_head *bh;
673 struct fat_boot_sector *b;
674 struct msdos_sb_info *sbi = MSDOS_SB(sb);
675
676 /* do not change any thing if mounted read only */
677 if (sb_rdonly(sb) && !force)
678 return;
679
680 /* do not change state if fs was dirty */
681 if (sbi->dirty) {
682 /* warn only on set (mount). */
683 if (set)
684 fat_msg(sb, KERN_WARNING, "Volume was not properly "
685 "unmounted. Some data may be corrupt. "
686 "Please run fsck.");
687 return;
688 }
689
690 bh = sb_bread(sb, 0);
691 if (bh == NULL) {
692 fat_msg(sb, KERN_ERR, "unable to read boot sector "
693 "to mark fs as dirty");
694 return;
695 }
696
697 b = (struct fat_boot_sector *) bh->b_data;
698
699 if (is_fat32(sbi)) {
700 if (set)
701 b->fat32.state |= FAT_STATE_DIRTY;
702 else
703 b->fat32.state &= ~FAT_STATE_DIRTY;
704 } else /* fat 16 and 12 */ {
705 if (set)
706 b->fat16.state |= FAT_STATE_DIRTY;
707 else
708 b->fat16.state &= ~FAT_STATE_DIRTY;
709 }
710
711 mark_buffer_dirty(bh);
712 sync_dirty_buffer(bh);
713 brelse(bh);
714 }
715
fat_reset_iocharset(struct fat_mount_options * opts)716 static void fat_reset_iocharset(struct fat_mount_options *opts)
717 {
718 if (opts->iocharset != fat_default_iocharset) {
719 /* Note: opts->iocharset can be NULL here */
720 kfree(opts->iocharset);
721 opts->iocharset = fat_default_iocharset;
722 }
723 }
724
delayed_free(struct rcu_head * p)725 static void delayed_free(struct rcu_head *p)
726 {
727 struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
728 unload_nls(sbi->nls_disk);
729 unload_nls(sbi->nls_io);
730 fat_reset_iocharset(&sbi->options);
731 kfree(sbi);
732 }
733
fat_put_super(struct super_block * sb)734 static void fat_put_super(struct super_block *sb)
735 {
736 struct msdos_sb_info *sbi = MSDOS_SB(sb);
737
738 fat_set_state(sb, 0, 0);
739
740 iput(sbi->fsinfo_inode);
741 iput(sbi->fat_inode);
742
743 call_rcu(&sbi->rcu, delayed_free);
744 }
745
746 static struct kmem_cache *fat_inode_cachep;
747
fat_alloc_inode(struct super_block * sb)748 static struct inode *fat_alloc_inode(struct super_block *sb)
749 {
750 struct msdos_inode_info *ei;
751 ei = alloc_inode_sb(sb, fat_inode_cachep, GFP_NOFS);
752 if (!ei)
753 return NULL;
754
755 init_rwsem(&ei->truncate_lock);
756 /* Zeroing to allow iput() even if partial initialized inode. */
757 ei->mmu_private = 0;
758 ei->i_start = 0;
759 ei->i_logstart = 0;
760 ei->i_attrs = 0;
761 ei->i_pos = 0;
762 ei->i_crtime.tv_sec = 0;
763 ei->i_crtime.tv_nsec = 0;
764
765 return &ei->vfs_inode;
766 }
767
fat_free_inode(struct inode * inode)768 static void fat_free_inode(struct inode *inode)
769 {
770 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
771 }
772
init_once(void * foo)773 static void init_once(void *foo)
774 {
775 struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
776
777 spin_lock_init(&ei->cache_lru_lock);
778 ei->nr_caches = 0;
779 ei->cache_valid_id = FAT_CACHE_VALID + 1;
780 INIT_LIST_HEAD(&ei->cache_lru);
781 INIT_HLIST_NODE(&ei->i_fat_hash);
782 INIT_HLIST_NODE(&ei->i_dir_hash);
783 inode_init_once(&ei->vfs_inode);
784 }
785
fat_init_inodecache(void)786 static int __init fat_init_inodecache(void)
787 {
788 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
789 sizeof(struct msdos_inode_info),
790 0, (SLAB_RECLAIM_ACCOUNT|
791 SLAB_ACCOUNT),
792 init_once);
793 if (fat_inode_cachep == NULL)
794 return -ENOMEM;
795 return 0;
796 }
797
fat_destroy_inodecache(void)798 static void __exit fat_destroy_inodecache(void)
799 {
800 /*
801 * Make sure all delayed rcu free inodes are flushed before we
802 * destroy cache.
803 */
804 rcu_barrier();
805 kmem_cache_destroy(fat_inode_cachep);
806 }
807
fat_reconfigure(struct fs_context * fc)808 int fat_reconfigure(struct fs_context *fc)
809 {
810 bool new_rdonly;
811 struct super_block *sb = fc->root->d_sb;
812 struct msdos_sb_info *sbi = MSDOS_SB(sb);
813 fc->sb_flags |= SB_NODIRATIME | (sbi->options.isvfat ? 0 : SB_NOATIME);
814
815 sync_filesystem(sb);
816
817 /* make sure we update state on remount. */
818 new_rdonly = fc->sb_flags & SB_RDONLY;
819 if (new_rdonly != sb_rdonly(sb)) {
820 if (new_rdonly)
821 fat_set_state(sb, 0, 0);
822 else
823 fat_set_state(sb, 1, 1);
824 }
825 return 0;
826 }
827 EXPORT_SYMBOL_GPL(fat_reconfigure);
828
fat_statfs(struct dentry * dentry,struct kstatfs * buf)829 static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
830 {
831 struct super_block *sb = dentry->d_sb;
832 struct msdos_sb_info *sbi = MSDOS_SB(sb);
833 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
834
835 /* If the count of free cluster is still unknown, counts it here. */
836 if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
837 int err = fat_count_free_clusters(dentry->d_sb);
838 if (err)
839 return err;
840 }
841
842 buf->f_type = dentry->d_sb->s_magic;
843 buf->f_bsize = sbi->cluster_size;
844 buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
845 buf->f_bfree = sbi->free_clusters;
846 buf->f_bavail = sbi->free_clusters;
847 buf->f_fsid = u64_to_fsid(id);
848 buf->f_namelen =
849 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
850
851 return 0;
852 }
853
__fat_write_inode(struct inode * inode,int wait)854 static int __fat_write_inode(struct inode *inode, int wait)
855 {
856 struct super_block *sb = inode->i_sb;
857 struct msdos_sb_info *sbi = MSDOS_SB(sb);
858 struct buffer_head *bh;
859 struct msdos_dir_entry *raw_entry;
860 struct timespec64 mtime;
861 loff_t i_pos;
862 sector_t blocknr;
863 int err, offset;
864
865 if (inode->i_ino == MSDOS_ROOT_INO)
866 return 0;
867
868 retry:
869 i_pos = fat_i_pos_read(sbi, inode);
870 if (!i_pos)
871 return 0;
872
873 fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
874 bh = sb_bread(sb, blocknr);
875 if (!bh) {
876 fat_msg(sb, KERN_ERR, "unable to read inode block "
877 "for updating (i_pos %lld)", i_pos);
878 return -EIO;
879 }
880 spin_lock(&sbi->inode_hash_lock);
881 if (i_pos != MSDOS_I(inode)->i_pos) {
882 spin_unlock(&sbi->inode_hash_lock);
883 brelse(bh);
884 goto retry;
885 }
886
887 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
888 if (S_ISDIR(inode->i_mode))
889 raw_entry->size = 0;
890 else
891 raw_entry->size = cpu_to_le32(inode->i_size);
892 raw_entry->attr = fat_make_attrs(inode);
893 fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
894 mtime = inode_get_mtime(inode);
895 fat_time_unix2fat(sbi, &mtime, &raw_entry->time,
896 &raw_entry->date, NULL);
897 if (sbi->options.isvfat) {
898 struct timespec64 ts = inode_get_atime(inode);
899 __le16 atime;
900
901 fat_time_unix2fat(sbi, &ts, &atime, &raw_entry->adate, NULL);
902 fat_time_unix2fat(sbi, &MSDOS_I(inode)->i_crtime, &raw_entry->ctime,
903 &raw_entry->cdate, &raw_entry->ctime_cs);
904 }
905 spin_unlock(&sbi->inode_hash_lock);
906 mark_buffer_dirty(bh);
907 err = 0;
908 if (wait)
909 err = sync_dirty_buffer(bh);
910 brelse(bh);
911 return err;
912 }
913
fat_write_inode(struct inode * inode,struct writeback_control * wbc)914 static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
915 {
916 int err;
917
918 if (inode->i_ino == MSDOS_FSINFO_INO) {
919 struct super_block *sb = inode->i_sb;
920
921 mutex_lock(&MSDOS_SB(sb)->s_lock);
922 err = fat_clusters_flush(sb);
923 mutex_unlock(&MSDOS_SB(sb)->s_lock);
924 } else
925 err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
926
927 return err;
928 }
929
fat_sync_inode(struct inode * inode)930 int fat_sync_inode(struct inode *inode)
931 {
932 return __fat_write_inode(inode, 1);
933 }
934
935 EXPORT_SYMBOL_GPL(fat_sync_inode);
936
937 static int fat_show_options(struct seq_file *m, struct dentry *root);
938 static const struct super_operations fat_sops = {
939 .alloc_inode = fat_alloc_inode,
940 .free_inode = fat_free_inode,
941 .write_inode = fat_write_inode,
942 .evict_inode = fat_evict_inode,
943 .put_super = fat_put_super,
944 .statfs = fat_statfs,
945 .show_options = fat_show_options,
946 };
947
fat_show_options(struct seq_file * m,struct dentry * root)948 static int fat_show_options(struct seq_file *m, struct dentry *root)
949 {
950 struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
951 struct fat_mount_options *opts = &sbi->options;
952 int isvfat = opts->isvfat;
953
954 if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
955 seq_printf(m, ",uid=%u",
956 from_kuid_munged(&init_user_ns, opts->fs_uid));
957 if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
958 seq_printf(m, ",gid=%u",
959 from_kgid_munged(&init_user_ns, opts->fs_gid));
960 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
961 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
962 if (opts->allow_utime)
963 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
964 if (sbi->nls_disk)
965 /* strip "cp" prefix from displayed option */
966 seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
967 if (isvfat) {
968 if (sbi->nls_io)
969 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
970
971 switch (opts->shortname) {
972 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
973 seq_puts(m, ",shortname=win95");
974 break;
975 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
976 seq_puts(m, ",shortname=winnt");
977 break;
978 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
979 seq_puts(m, ",shortname=mixed");
980 break;
981 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
982 seq_puts(m, ",shortname=lower");
983 break;
984 default:
985 seq_puts(m, ",shortname=unknown");
986 break;
987 }
988 }
989 if (opts->name_check != 'n')
990 seq_printf(m, ",check=%c", opts->name_check);
991 if (opts->usefree)
992 seq_puts(m, ",usefree");
993 if (opts->quiet)
994 seq_puts(m, ",quiet");
995 if (opts->showexec)
996 seq_puts(m, ",showexec");
997 if (opts->sys_immutable)
998 seq_puts(m, ",sys_immutable");
999 if (!isvfat) {
1000 if (opts->dotsOK)
1001 seq_puts(m, ",dotsOK=yes");
1002 if (opts->nocase)
1003 seq_puts(m, ",nocase");
1004 } else {
1005 if (opts->utf8)
1006 seq_puts(m, ",utf8");
1007 if (opts->unicode_xlate)
1008 seq_puts(m, ",uni_xlate");
1009 if (!opts->numtail)
1010 seq_puts(m, ",nonumtail");
1011 if (opts->rodir)
1012 seq_puts(m, ",rodir");
1013 }
1014 if (opts->flush)
1015 seq_puts(m, ",flush");
1016 if (opts->tz_set) {
1017 if (opts->time_offset)
1018 seq_printf(m, ",time_offset=%d", opts->time_offset);
1019 else
1020 seq_puts(m, ",tz=UTC");
1021 }
1022 if (opts->errors == FAT_ERRORS_CONT)
1023 seq_puts(m, ",errors=continue");
1024 else if (opts->errors == FAT_ERRORS_PANIC)
1025 seq_puts(m, ",errors=panic");
1026 else
1027 seq_puts(m, ",errors=remount-ro");
1028 if (opts->nfs == FAT_NFS_NOSTALE_RO)
1029 seq_puts(m, ",nfs=nostale_ro");
1030 else if (opts->nfs)
1031 seq_puts(m, ",nfs=stale_rw");
1032 if (opts->discard)
1033 seq_puts(m, ",discard");
1034 if (opts->dos1xfloppy)
1035 seq_puts(m, ",dos1xfloppy");
1036
1037 return 0;
1038 }
1039
1040 enum {
1041 Opt_check, Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask,
1042 Opt_allow_utime, Opt_codepage, Opt_usefree, Opt_nocase, Opt_quiet,
1043 Opt_showexec, Opt_debug, Opt_immutable, Opt_dots, Opt_dotsOK,
1044 Opt_charset, Opt_shortname, Opt_utf8, Opt_utf8_bool,
1045 Opt_uni_xl, Opt_uni_xl_bool, Opt_nonumtail, Opt_nonumtail_bool,
1046 Opt_obsolete, Opt_flush, Opt_tz, Opt_rodir, Opt_errors, Opt_discard,
1047 Opt_nfs, Opt_nfs_enum, Opt_time_offset, Opt_dos1xfloppy,
1048 };
1049
1050 static const struct constant_table fat_param_check[] = {
1051 {"relaxed", 'r'},
1052 {"r", 'r'},
1053 {"strict", 's'},
1054 {"s", 's'},
1055 {"normal", 'n'},
1056 {"n", 'n'},
1057 {}
1058 };
1059
1060 static const struct constant_table fat_param_tz[] = {
1061 {"UTC", 0},
1062 {}
1063 };
1064
1065 static const struct constant_table fat_param_errors[] = {
1066 {"continue", FAT_ERRORS_CONT},
1067 {"panic", FAT_ERRORS_PANIC},
1068 {"remount-ro", FAT_ERRORS_RO},
1069 {}
1070 };
1071
1072
1073 static const struct constant_table fat_param_nfs[] = {
1074 {"stale_rw", FAT_NFS_STALE_RW},
1075 {"nostale_ro", FAT_NFS_NOSTALE_RO},
1076 {}
1077 };
1078
1079 /*
1080 * These are all obsolete but we still reject invalid options.
1081 * The corresponding values are therefore meaningless.
1082 */
1083 static const struct constant_table fat_param_conv[] = {
1084 {"binary", 0},
1085 {"text", 0},
1086 {"auto", 0},
1087 {"b", 0},
1088 {"t", 0},
1089 {"a", 0},
1090 {}
1091 };
1092
1093 /* Core options. See below for vfat and msdos extras */
1094 const struct fs_parameter_spec fat_param_spec[] = {
1095 fsparam_enum ("check", Opt_check, fat_param_check),
1096 fsparam_uid ("uid", Opt_uid),
1097 fsparam_gid ("gid", Opt_gid),
1098 fsparam_u32oct ("umask", Opt_umask),
1099 fsparam_u32oct ("dmask", Opt_dmask),
1100 fsparam_u32oct ("fmask", Opt_fmask),
1101 fsparam_u32oct ("allow_utime", Opt_allow_utime),
1102 fsparam_u32 ("codepage", Opt_codepage),
1103 fsparam_flag ("usefree", Opt_usefree),
1104 fsparam_flag ("nocase", Opt_nocase),
1105 fsparam_flag ("quiet", Opt_quiet),
1106 fsparam_flag ("showexec", Opt_showexec),
1107 fsparam_flag ("debug", Opt_debug),
1108 fsparam_flag ("sys_immutable", Opt_immutable),
1109 fsparam_flag ("flush", Opt_flush),
1110 fsparam_enum ("tz", Opt_tz, fat_param_tz),
1111 fsparam_s32 ("time_offset", Opt_time_offset),
1112 fsparam_enum ("errors", Opt_errors, fat_param_errors),
1113 fsparam_flag ("discard", Opt_discard),
1114 fsparam_flag ("nfs", Opt_nfs),
1115 fsparam_enum ("nfs", Opt_nfs_enum, fat_param_nfs),
1116 fsparam_flag ("dos1xfloppy", Opt_dos1xfloppy),
1117 __fsparam(fs_param_is_enum, "conv",
1118 Opt_obsolete, fs_param_deprecated, fat_param_conv),
1119 __fsparam(fs_param_is_u32, "fat",
1120 Opt_obsolete, fs_param_deprecated, NULL),
1121 __fsparam(fs_param_is_u32, "blocksize",
1122 Opt_obsolete, fs_param_deprecated, NULL),
1123 __fsparam(fs_param_is_string, "cvf_format",
1124 Opt_obsolete, fs_param_deprecated, NULL),
1125 __fsparam(fs_param_is_string, "cvf_options",
1126 Opt_obsolete, fs_param_deprecated, NULL),
1127 __fsparam(NULL, "posix",
1128 Opt_obsolete, fs_param_deprecated, NULL),
1129 {}
1130 };
1131 EXPORT_SYMBOL_GPL(fat_param_spec);
1132
1133 static const struct fs_parameter_spec msdos_param_spec[] = {
1134 fsparam_flag_no ("dots", Opt_dots),
1135 fsparam_bool ("dotsOK", Opt_dotsOK),
1136 {}
1137 };
1138
1139 static const struct constant_table fat_param_shortname[] = {
1140 {"lower", VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95},
1141 {"win95", VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95},
1142 {"winnt", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT},
1143 {"mixed", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95},
1144 {}
1145 };
1146
1147 static const struct fs_parameter_spec vfat_param_spec[] = {
1148 fsparam_string ("iocharset", Opt_charset),
1149 fsparam_enum ("shortname", Opt_shortname, fat_param_shortname),
1150 fsparam_flag ("utf8", Opt_utf8),
1151 fsparam_bool ("utf8", Opt_utf8_bool),
1152 fsparam_flag ("uni_xlate", Opt_uni_xl),
1153 fsparam_bool ("uni_xlate", Opt_uni_xl_bool),
1154 fsparam_flag ("nonumtail", Opt_nonumtail),
1155 fsparam_bool ("nonumtail", Opt_nonumtail_bool),
1156 fsparam_flag ("rodir", Opt_rodir),
1157 {}
1158 };
1159
fat_parse_param(struct fs_context * fc,struct fs_parameter * param,bool is_vfat)1160 int fat_parse_param(struct fs_context *fc, struct fs_parameter *param,
1161 bool is_vfat)
1162 {
1163 struct fat_mount_options *opts = fc->fs_private;
1164 struct fs_parse_result result;
1165 int opt;
1166
1167 /* remount options have traditionally been ignored */
1168 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1169 return 0;
1170
1171 opt = fs_parse(fc, fat_param_spec, param, &result);
1172 /* If option not found in fat_param_spec, try vfat/msdos options */
1173 if (opt == -ENOPARAM) {
1174 if (is_vfat)
1175 opt = fs_parse(fc, vfat_param_spec, param, &result);
1176 else
1177 opt = fs_parse(fc, msdos_param_spec, param, &result);
1178 }
1179
1180 if (opt < 0)
1181 return opt;
1182
1183 switch (opt) {
1184 case Opt_check:
1185 opts->name_check = result.uint_32;
1186 break;
1187 case Opt_usefree:
1188 opts->usefree = 1;
1189 break;
1190 case Opt_nocase:
1191 if (!is_vfat)
1192 opts->nocase = 1;
1193 else {
1194 /* for backward compatibility */
1195 opts->shortname = VFAT_SFN_DISPLAY_WIN95
1196 | VFAT_SFN_CREATE_WIN95;
1197 }
1198 break;
1199 case Opt_quiet:
1200 opts->quiet = 1;
1201 break;
1202 case Opt_showexec:
1203 opts->showexec = 1;
1204 break;
1205 case Opt_debug:
1206 opts->debug = 1;
1207 break;
1208 case Opt_immutable:
1209 opts->sys_immutable = 1;
1210 break;
1211 case Opt_uid:
1212 opts->fs_uid = result.uid;
1213 break;
1214 case Opt_gid:
1215 opts->fs_gid = result.gid;
1216 break;
1217 case Opt_umask:
1218 opts->fs_fmask = opts->fs_dmask = result.uint_32;
1219 break;
1220 case Opt_dmask:
1221 opts->fs_dmask = result.uint_32;
1222 break;
1223 case Opt_fmask:
1224 opts->fs_fmask = result.uint_32;
1225 break;
1226 case Opt_allow_utime:
1227 opts->allow_utime = result.uint_32 & (S_IWGRP | S_IWOTH);
1228 break;
1229 case Opt_codepage:
1230 opts->codepage = result.uint_32;
1231 break;
1232 case Opt_flush:
1233 opts->flush = 1;
1234 break;
1235 case Opt_time_offset:
1236 /*
1237 * GMT+-12 zones may have DST corrections so at least
1238 * 13 hours difference is needed. Make the limit 24
1239 * just in case someone invents something unusual.
1240 */
1241 if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
1242 return -EINVAL;
1243 opts->tz_set = 1;
1244 opts->time_offset = result.int_32;
1245 break;
1246 case Opt_tz:
1247 opts->tz_set = 1;
1248 opts->time_offset = result.uint_32;
1249 break;
1250 case Opt_errors:
1251 opts->errors = result.uint_32;
1252 break;
1253 case Opt_nfs:
1254 opts->nfs = FAT_NFS_STALE_RW;
1255 break;
1256 case Opt_nfs_enum:
1257 opts->nfs = result.uint_32;
1258 break;
1259 case Opt_dos1xfloppy:
1260 opts->dos1xfloppy = 1;
1261 break;
1262
1263 /* msdos specific */
1264 case Opt_dots: /* dots / nodots */
1265 opts->dotsOK = !result.negated;
1266 break;
1267 case Opt_dotsOK: /* dotsOK = yes/no */
1268 opts->dotsOK = result.boolean;
1269 break;
1270
1271 /* vfat specific */
1272 case Opt_charset:
1273 fat_reset_iocharset(opts);
1274 opts->iocharset = param->string;
1275 param->string = NULL; /* Steal string */
1276 break;
1277 case Opt_shortname:
1278 opts->shortname = result.uint_32;
1279 break;
1280 case Opt_utf8:
1281 opts->utf8 = 1;
1282 break;
1283 case Opt_utf8_bool:
1284 opts->utf8 = result.boolean;
1285 break;
1286 case Opt_uni_xl:
1287 opts->unicode_xlate = 1;
1288 break;
1289 case Opt_uni_xl_bool:
1290 opts->unicode_xlate = result.boolean;
1291 break;
1292 case Opt_nonumtail:
1293 opts->numtail = 0; /* negated option */
1294 break;
1295 case Opt_nonumtail_bool:
1296 opts->numtail = !result.boolean; /* negated option */
1297 break;
1298 case Opt_rodir:
1299 opts->rodir = 1;
1300 break;
1301 case Opt_discard:
1302 opts->discard = 1;
1303 break;
1304
1305 /* obsolete mount options */
1306 case Opt_obsolete:
1307 printk(KERN_INFO "FAT-fs: \"%s\" option is obsolete, "
1308 "not supported now", param->key);
1309 break;
1310 default:
1311 return -EINVAL;
1312 }
1313
1314 return 0;
1315 }
1316 EXPORT_SYMBOL_GPL(fat_parse_param);
1317
fat_read_root(struct inode * inode)1318 static int fat_read_root(struct inode *inode)
1319 {
1320 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
1321 int error;
1322
1323 MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
1324 inode->i_uid = sbi->options.fs_uid;
1325 inode->i_gid = sbi->options.fs_gid;
1326 inode_inc_iversion(inode);
1327 inode->i_generation = 0;
1328 inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
1329 inode->i_op = sbi->dir_ops;
1330 inode->i_fop = &fat_dir_operations;
1331 if (is_fat32(sbi)) {
1332 MSDOS_I(inode)->i_start = sbi->root_cluster;
1333 error = fat_calc_dir_size(inode);
1334 if (error < 0)
1335 return error;
1336 } else {
1337 MSDOS_I(inode)->i_start = 0;
1338 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
1339 }
1340 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1341 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1342 MSDOS_I(inode)->i_logstart = 0;
1343 MSDOS_I(inode)->mmu_private = inode->i_size;
1344
1345 fat_save_attrs(inode, ATTR_DIR);
1346 inode_set_mtime_to_ts(inode,
1347 inode_set_atime_to_ts(inode, inode_set_ctime(inode, 0, 0)));
1348 set_nlink(inode, fat_subdirs(inode)+2);
1349
1350 return 0;
1351 }
1352
calc_fat_clusters(struct super_block * sb)1353 static unsigned long calc_fat_clusters(struct super_block *sb)
1354 {
1355 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1356
1357 /* Divide first to avoid overflow */
1358 if (!is_fat12(sbi)) {
1359 unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
1360 return ent_per_sec * sbi->fat_length;
1361 }
1362
1363 return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
1364 }
1365
fat_bpb_is_zero(struct fat_boot_sector * b)1366 static bool fat_bpb_is_zero(struct fat_boot_sector *b)
1367 {
1368 if (get_unaligned_le16(&b->sector_size))
1369 return false;
1370 if (b->sec_per_clus)
1371 return false;
1372 if (b->reserved)
1373 return false;
1374 if (b->fats)
1375 return false;
1376 if (get_unaligned_le16(&b->dir_entries))
1377 return false;
1378 if (get_unaligned_le16(&b->sectors))
1379 return false;
1380 if (b->media)
1381 return false;
1382 if (b->fat_length)
1383 return false;
1384 if (b->secs_track)
1385 return false;
1386 if (b->heads)
1387 return false;
1388 return true;
1389 }
1390
fat_read_bpb(struct super_block * sb,struct fat_boot_sector * b,int silent,struct fat_bios_param_block * bpb)1391 static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b,
1392 int silent, struct fat_bios_param_block *bpb)
1393 {
1394 int error = -EINVAL;
1395
1396 /* Read in BPB ... */
1397 memset(bpb, 0, sizeof(*bpb));
1398 bpb->fat_sector_size = get_unaligned_le16(&b->sector_size);
1399 bpb->fat_sec_per_clus = b->sec_per_clus;
1400 bpb->fat_reserved = le16_to_cpu(b->reserved);
1401 bpb->fat_fats = b->fats;
1402 bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries);
1403 bpb->fat_sectors = get_unaligned_le16(&b->sectors);
1404 bpb->fat_fat_length = le16_to_cpu(b->fat_length);
1405 bpb->fat_total_sect = le32_to_cpu(b->total_sect);
1406
1407 bpb->fat16_state = b->fat16.state;
1408 bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id);
1409
1410 bpb->fat32_length = le32_to_cpu(b->fat32.length);
1411 bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster);
1412 bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector);
1413 bpb->fat32_state = b->fat32.state;
1414 bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id);
1415
1416 /* Validate this looks like a FAT filesystem BPB */
1417 if (!bpb->fat_reserved) {
1418 if (!silent)
1419 fat_msg(sb, KERN_ERR,
1420 "bogus number of reserved sectors");
1421 goto out;
1422 }
1423 if (!bpb->fat_fats) {
1424 if (!silent)
1425 fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1426 goto out;
1427 }
1428
1429 /*
1430 * Earlier we checked here that b->secs_track and b->head are nonzero,
1431 * but it turns out valid FAT filesystems can have zero there.
1432 */
1433
1434 if (!fat_valid_media(b->media)) {
1435 if (!silent)
1436 fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
1437 (unsigned)b->media);
1438 goto out;
1439 }
1440
1441 if (!is_power_of_2(bpb->fat_sector_size)
1442 || (bpb->fat_sector_size < 512)
1443 || (bpb->fat_sector_size > 4096)) {
1444 if (!silent)
1445 fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
1446 (unsigned)bpb->fat_sector_size);
1447 goto out;
1448 }
1449
1450 if (!is_power_of_2(bpb->fat_sec_per_clus)) {
1451 if (!silent)
1452 fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
1453 (unsigned)bpb->fat_sec_per_clus);
1454 goto out;
1455 }
1456
1457 if (bpb->fat_fat_length == 0 && bpb->fat32_length == 0) {
1458 if (!silent)
1459 fat_msg(sb, KERN_ERR, "bogus number of FAT sectors");
1460 goto out;
1461 }
1462
1463 error = 0;
1464
1465 out:
1466 return error;
1467 }
1468
fat_read_static_bpb(struct super_block * sb,struct fat_boot_sector * b,int silent,struct fat_bios_param_block * bpb)1469 static int fat_read_static_bpb(struct super_block *sb,
1470 struct fat_boot_sector *b, int silent,
1471 struct fat_bios_param_block *bpb)
1472 {
1473 static const char *notdos1x = "This doesn't look like a DOS 1.x volume";
1474 sector_t bd_sects = bdev_nr_sectors(sb->s_bdev);
1475 struct fat_floppy_defaults *fdefaults = NULL;
1476 int error = -EINVAL;
1477 unsigned i;
1478
1479 /* 16-bit DOS 1.x reliably wrote bootstrap short-jmp code */
1480 if (b->ignored[0] != 0xeb || b->ignored[2] != 0x90) {
1481 if (!silent)
1482 fat_msg(sb, KERN_ERR,
1483 "%s; no bootstrapping code", notdos1x);
1484 goto out;
1485 }
1486
1487 /*
1488 * If any value in this region is non-zero, it isn't archaic
1489 * DOS.
1490 */
1491 if (!fat_bpb_is_zero(b)) {
1492 if (!silent)
1493 fat_msg(sb, KERN_ERR,
1494 "%s; DOS 2.x BPB is non-zero", notdos1x);
1495 goto out;
1496 }
1497
1498 for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) {
1499 if (floppy_defaults[i].nr_sectors == bd_sects) {
1500 fdefaults = &floppy_defaults[i];
1501 break;
1502 }
1503 }
1504
1505 if (fdefaults == NULL) {
1506 if (!silent)
1507 fat_msg(sb, KERN_WARNING,
1508 "This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)",
1509 (u64)bd_sects);
1510 goto out;
1511 }
1512
1513 if (!silent)
1514 fat_msg(sb, KERN_INFO,
1515 "This looks like a DOS 1.x volume; assuming default BPB values");
1516
1517 memset(bpb, 0, sizeof(*bpb));
1518 bpb->fat_sector_size = SECTOR_SIZE;
1519 bpb->fat_sec_per_clus = fdefaults->sec_per_clus;
1520 bpb->fat_reserved = 1;
1521 bpb->fat_fats = 2;
1522 bpb->fat_dir_entries = fdefaults->dir_entries;
1523 bpb->fat_sectors = fdefaults->nr_sectors;
1524 bpb->fat_fat_length = fdefaults->fat_length;
1525
1526 error = 0;
1527
1528 out:
1529 return error;
1530 }
1531
1532 /*
1533 * Read the super block of an MS-DOS FS.
1534 */
fat_fill_super(struct super_block * sb,struct fs_context * fc,void (* setup)(struct super_block *))1535 int fat_fill_super(struct super_block *sb, struct fs_context *fc,
1536 void (*setup)(struct super_block *))
1537 {
1538 struct fat_mount_options *opts = fc->fs_private;
1539 int silent = fc->sb_flags & SB_SILENT;
1540 struct inode *root_inode = NULL, *fat_inode = NULL;
1541 struct inode *fsinfo_inode = NULL;
1542 struct buffer_head *bh;
1543 struct fat_bios_param_block bpb;
1544 struct msdos_sb_info *sbi;
1545 u16 logical_sector_size;
1546 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
1547 long error;
1548 char buf[50];
1549 struct timespec64 ts;
1550
1551 /*
1552 * GFP_KERNEL is ok here, because while we do hold the
1553 * superblock lock, memory pressure can't call back into
1554 * the filesystem, since we're only just about to mount
1555 * it and have no inodes etc active!
1556 */
1557 sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
1558 if (!sbi)
1559 return -ENOMEM;
1560 sb->s_fs_info = sbi;
1561
1562 sb->s_flags |= SB_NODIRATIME;
1563 sb->s_magic = MSDOS_SUPER_MAGIC;
1564 sb->s_op = &fat_sops;
1565 sb->s_export_op = &fat_export_ops;
1566 /*
1567 * fat timestamps are complex and truncated by fat itself, so
1568 * we set 1 here to be fast
1569 */
1570 sb->s_time_gran = 1;
1571 mutex_init(&sbi->nfs_build_inode_lock);
1572 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1573 DEFAULT_RATELIMIT_BURST);
1574
1575 /* UTF-8 doesn't provide FAT semantics */
1576 if (!strcmp(opts->iocharset, "utf8")) {
1577 fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
1578 " for FAT filesystems, filesystem will be"
1579 " case sensitive!");
1580 }
1581
1582 /* If user doesn't specify allow_utime, it's initialized from dmask. */
1583 if (opts->allow_utime == (unsigned short)-1)
1584 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
1585 if (opts->unicode_xlate)
1586 opts->utf8 = 0;
1587 if (opts->nfs == FAT_NFS_NOSTALE_RO) {
1588 sb->s_flags |= SB_RDONLY;
1589 sb->s_export_op = &fat_export_ops_nostale;
1590 }
1591
1592 /* Apply parsed options to sbi (structure copy) */
1593 sbi->options = *opts;
1594 /* Transfer ownership of iocharset to sbi->options */
1595 opts->iocharset = NULL;
1596
1597 setup(sb); /* flavour-specific stuff that needs options */
1598
1599 error = -EINVAL;
1600 if (!sb_min_blocksize(sb, 512)) {
1601 fat_msg(sb, KERN_ERR, "unable to set blocksize");
1602 goto out_fail;
1603 }
1604 error = -EIO;
1605 bh = sb_bread(sb, 0);
1606 if (bh == NULL) {
1607 fat_msg(sb, KERN_ERR, "unable to read boot sector");
1608 goto out_fail;
1609 }
1610
1611 error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent,
1612 &bpb);
1613 if (error == -EINVAL && sbi->options.dos1xfloppy)
1614 error = fat_read_static_bpb(sb,
1615 (struct fat_boot_sector *)bh->b_data, silent, &bpb);
1616 brelse(bh);
1617
1618 if (error == -EINVAL)
1619 goto out_invalid;
1620 else if (error)
1621 goto out_fail;
1622
1623 logical_sector_size = bpb.fat_sector_size;
1624 sbi->sec_per_clus = bpb.fat_sec_per_clus;
1625
1626 error = -EIO;
1627 if (logical_sector_size < sb->s_blocksize) {
1628 fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1629 " (logical sector size = %u)", logical_sector_size);
1630 goto out_fail;
1631 }
1632
1633 if (logical_sector_size > sb->s_blocksize) {
1634 struct buffer_head *bh_resize;
1635
1636 if (!sb_set_blocksize(sb, logical_sector_size)) {
1637 fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
1638 logical_sector_size);
1639 goto out_fail;
1640 }
1641
1642 /* Verify that the larger boot sector is fully readable */
1643 bh_resize = sb_bread(sb, 0);
1644 if (bh_resize == NULL) {
1645 fat_msg(sb, KERN_ERR, "unable to read boot sector"
1646 " (logical sector size = %lu)",
1647 sb->s_blocksize);
1648 goto out_fail;
1649 }
1650 brelse(bh_resize);
1651 }
1652
1653 mutex_init(&sbi->s_lock);
1654 sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
1655 sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
1656 sbi->fats = bpb.fat_fats;
1657 sbi->fat_bits = 0; /* Don't know yet */
1658 sbi->fat_start = bpb.fat_reserved;
1659 sbi->fat_length = bpb.fat_fat_length;
1660 sbi->root_cluster = 0;
1661 sbi->free_clusters = -1; /* Don't know yet */
1662 sbi->free_clus_valid = 0;
1663 sbi->prev_free = FAT_START_ENT;
1664 sb->s_maxbytes = 0xffffffff;
1665 fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0);
1666 sb->s_time_min = ts.tv_sec;
1667
1668 fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX),
1669 cpu_to_le16(FAT_DATE_MAX), 0);
1670 sb->s_time_max = ts.tv_sec;
1671
1672 if (!sbi->fat_length && bpb.fat32_length) {
1673 struct fat_boot_fsinfo *fsinfo;
1674 struct buffer_head *fsinfo_bh;
1675
1676 /* Must be FAT32 */
1677 sbi->fat_bits = 32;
1678 sbi->fat_length = bpb.fat32_length;
1679 sbi->root_cluster = bpb.fat32_root_cluster;
1680
1681 /* MC - if info_sector is 0, don't multiply by 0 */
1682 sbi->fsinfo_sector = bpb.fat32_info_sector;
1683 if (sbi->fsinfo_sector == 0)
1684 sbi->fsinfo_sector = 1;
1685
1686 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
1687 if (fsinfo_bh == NULL) {
1688 fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1689 " (sector = %lu)", sbi->fsinfo_sector);
1690 goto out_fail;
1691 }
1692
1693 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1694 if (!IS_FSINFO(fsinfo)) {
1695 fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1696 "0x%08x, 0x%08x (sector = %lu)",
1697 le32_to_cpu(fsinfo->signature1),
1698 le32_to_cpu(fsinfo->signature2),
1699 sbi->fsinfo_sector);
1700 } else {
1701 if (sbi->options.usefree)
1702 sbi->free_clus_valid = 1;
1703 sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
1704 sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
1705 }
1706
1707 brelse(fsinfo_bh);
1708 }
1709
1710 /* interpret volume ID as a little endian 32 bit integer */
1711 if (is_fat32(sbi))
1712 sbi->vol_id = bpb.fat32_vol_id;
1713 else /* fat 16 or 12 */
1714 sbi->vol_id = bpb.fat16_vol_id;
1715
1716 __le32 vol_id_le = cpu_to_le32(sbi->vol_id);
1717 super_set_uuid(sb, (void *) &vol_id_le, sizeof(vol_id_le));
1718
1719 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
1720 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
1721
1722 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1723 sbi->dir_entries = bpb.fat_dir_entries;
1724 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
1725 if (!silent)
1726 fat_msg(sb, KERN_ERR, "bogus number of directory entries"
1727 " (%u)", sbi->dir_entries);
1728 goto out_invalid;
1729 }
1730
1731 rootdir_sectors = sbi->dir_entries
1732 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
1733 sbi->data_start = sbi->dir_start + rootdir_sectors;
1734 total_sectors = bpb.fat_sectors;
1735 if (total_sectors == 0)
1736 total_sectors = bpb.fat_total_sect;
1737
1738 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
1739
1740 if (!is_fat32(sbi))
1741 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
1742
1743 /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1744 if (is_fat32(sbi))
1745 sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
1746 else /* fat 16 or 12 */
1747 sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;
1748
1749 /* check that FAT table does not overflow */
1750 fat_clusters = calc_fat_clusters(sb);
1751 total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
1752 if (total_clusters > max_fat(sb)) {
1753 if (!silent)
1754 fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
1755 total_clusters);
1756 goto out_invalid;
1757 }
1758
1759 sbi->max_cluster = total_clusters + FAT_START_ENT;
1760 /* check the free_clusters, it's not necessarily correct */
1761 if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
1762 sbi->free_clusters = -1;
1763 /* check the prev_free, it's not necessarily correct */
1764 sbi->prev_free %= sbi->max_cluster;
1765 if (sbi->prev_free < FAT_START_ENT)
1766 sbi->prev_free = FAT_START_ENT;
1767
1768 /* set up enough so that it can read an inode */
1769 fat_hash_init(sb);
1770 dir_hash_init(sb);
1771 fat_ent_access_init(sb);
1772
1773 /*
1774 * The low byte of the first FAT entry must have the same value as
1775 * the media field of the boot sector. But in real world, too many
1776 * devices are writing wrong values. So, removed that validity check.
1777 *
1778 * The removed check compared the first FAT entry to a value dependent
1779 * on the media field like this:
1780 * == (0x0F00 | media), for FAT12
1781 * == (0XFF00 | media), for FAT16
1782 * == (0x0FFFFF | media), for FAT32
1783 */
1784
1785 error = -EINVAL;
1786 sprintf(buf, "cp%d", sbi->options.codepage);
1787 sbi->nls_disk = load_nls(buf);
1788 if (!sbi->nls_disk) {
1789 fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
1790 goto out_fail;
1791 }
1792
1793 /* FIXME: utf8 is using iocharset for upper/lower conversion */
1794 if (sbi->options.isvfat) {
1795 sbi->nls_io = load_nls(sbi->options.iocharset);
1796 if (!sbi->nls_io) {
1797 fat_msg(sb, KERN_ERR, "IO charset %s not found",
1798 sbi->options.iocharset);
1799 goto out_fail;
1800 }
1801 }
1802
1803 error = -ENOMEM;
1804 fat_inode = new_inode(sb);
1805 if (!fat_inode)
1806 goto out_fail;
1807 sbi->fat_inode = fat_inode;
1808
1809 fsinfo_inode = new_inode(sb);
1810 if (!fsinfo_inode)
1811 goto out_fail;
1812 fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
1813 sbi->fsinfo_inode = fsinfo_inode;
1814 insert_inode_hash(fsinfo_inode);
1815
1816 root_inode = new_inode(sb);
1817 if (!root_inode)
1818 goto out_fail;
1819 root_inode->i_ino = MSDOS_ROOT_INO;
1820 inode_set_iversion(root_inode, 1);
1821 error = fat_read_root(root_inode);
1822 if (error < 0) {
1823 iput(root_inode);
1824 goto out_fail;
1825 }
1826 error = -ENOMEM;
1827 insert_inode_hash(root_inode);
1828 fat_attach(root_inode, 0);
1829 sb->s_root = d_make_root(root_inode);
1830 if (!sb->s_root) {
1831 fat_msg(sb, KERN_ERR, "get root inode failed");
1832 goto out_fail;
1833 }
1834
1835 if (sbi->options.discard && !bdev_max_discard_sectors(sb->s_bdev))
1836 fat_msg(sb, KERN_WARNING,
1837 "mounting with \"discard\" option, but the device does not support discard");
1838
1839 fat_set_state(sb, 1, 0);
1840 return 0;
1841
1842 out_invalid:
1843 error = -EINVAL;
1844 if (!silent)
1845 fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
1846
1847 out_fail:
1848 iput(fsinfo_inode);
1849 iput(fat_inode);
1850 unload_nls(sbi->nls_io);
1851 unload_nls(sbi->nls_disk);
1852 fat_reset_iocharset(&sbi->options);
1853 sb->s_fs_info = NULL;
1854 kfree(sbi);
1855 return error;
1856 }
1857
1858 EXPORT_SYMBOL_GPL(fat_fill_super);
1859
1860 /*
1861 * helper function for fat_flush_inodes. This writes both the inode
1862 * and the file data blocks, waiting for in flight data blocks before
1863 * the start of the call. It does not wait for any io started
1864 * during the call
1865 */
writeback_inode(struct inode * inode)1866 static int writeback_inode(struct inode *inode)
1867 {
1868
1869 int ret;
1870
1871 /* if we used wait=1, sync_inode_metadata waits for the io for the
1872 * inode to finish. So wait=0 is sent down to sync_inode_metadata
1873 * and filemap_fdatawrite is used for the data blocks
1874 */
1875 ret = sync_inode_metadata(inode, 0);
1876 if (!ret)
1877 ret = filemap_fdatawrite(inode->i_mapping);
1878 return ret;
1879 }
1880
1881 /*
1882 * write data and metadata corresponding to i1 and i2. The io is
1883 * started but we do not wait for any of it to finish.
1884 *
1885 * filemap_flush is used for the block device, so if there is a dirty
1886 * page for a block already in flight, we will not wait and start the
1887 * io over again
1888 */
fat_flush_inodes(struct super_block * sb,struct inode * i1,struct inode * i2)1889 int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1890 {
1891 int ret = 0;
1892 if (!MSDOS_SB(sb)->options.flush)
1893 return 0;
1894 if (i1)
1895 ret = writeback_inode(i1);
1896 if (!ret && i2)
1897 ret = writeback_inode(i2);
1898 if (!ret)
1899 ret = sync_blockdev_nowait(sb->s_bdev);
1900 return ret;
1901 }
1902 EXPORT_SYMBOL_GPL(fat_flush_inodes);
1903
fat_init_fs_context(struct fs_context * fc,bool is_vfat)1904 int fat_init_fs_context(struct fs_context *fc, bool is_vfat)
1905 {
1906 struct fat_mount_options *opts;
1907
1908 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1909 if (!opts)
1910 return -ENOMEM;
1911
1912 opts->isvfat = is_vfat;
1913 opts->fs_uid = current_uid();
1914 opts->fs_gid = current_gid();
1915 opts->fs_fmask = opts->fs_dmask = current_umask();
1916 opts->allow_utime = -1;
1917 opts->codepage = fat_default_codepage;
1918 fat_reset_iocharset(opts);
1919 if (is_vfat) {
1920 opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
1921 opts->rodir = 0;
1922 } else {
1923 opts->shortname = 0;
1924 opts->rodir = 1;
1925 }
1926 opts->name_check = 'n';
1927 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
1928 opts->unicode_xlate = 0;
1929 opts->numtail = 1;
1930 opts->usefree = opts->nocase = 0;
1931 opts->tz_set = 0;
1932 opts->nfs = 0;
1933 opts->errors = FAT_ERRORS_RO;
1934 opts->debug = 0;
1935
1936 opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat;
1937
1938 fc->fs_private = opts;
1939 /* fc->ops assigned by caller */
1940
1941 return 0;
1942 }
1943 EXPORT_SYMBOL_GPL(fat_init_fs_context);
1944
fat_free_fc(struct fs_context * fc)1945 void fat_free_fc(struct fs_context *fc)
1946 {
1947 struct fat_mount_options *opts = fc->fs_private;
1948
1949 if (opts->iocharset != fat_default_iocharset)
1950 kfree(opts->iocharset);
1951 kfree(fc->fs_private);
1952 }
1953 EXPORT_SYMBOL_GPL(fat_free_fc);
1954
init_fat_fs(void)1955 static int __init init_fat_fs(void)
1956 {
1957 int err;
1958
1959 err = fat_cache_init();
1960 if (err)
1961 return err;
1962
1963 err = fat_init_inodecache();
1964 if (err)
1965 goto failed;
1966
1967 return 0;
1968
1969 failed:
1970 fat_cache_destroy();
1971 return err;
1972 }
1973
exit_fat_fs(void)1974 static void __exit exit_fat_fs(void)
1975 {
1976 fat_cache_destroy();
1977 fat_destroy_inodecache();
1978 }
1979
1980 module_init(init_fat_fs)
1981 module_exit(exit_fat_fs)
1982
1983 MODULE_DESCRIPTION("Core FAT filesystem support");
1984 MODULE_LICENSE("GPL");
1985