]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/fat/inode.c
Merge branch 'akpm-current/current'
[karo-tx-linux.git] / fs / fat / inode.c
1 /*
2  *  linux/fs/fat/inode.c
3  *
4  *  Written 1992,1993 by Werner Almesberger
5  *  VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
6  *  Rewritten for the constant inumbers support by Al Viro
7  *
8  *  Fixes:
9  *
10  *      Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/time.h>
16 #include <linux/slab.h>
17 #include <linux/seq_file.h>
18 #include <linux/pagemap.h>
19 #include <linux/mpage.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mount.h>
22 #include <linux/aio.h>
23 #include <linux/vfs.h>
24 #include <linux/parser.h>
25 #include <linux/uio.h>
26 #include <linux/writeback.h>
27 #include <linux/log2.h>
28 #include <linux/hash.h>
29 #include <linux/blkdev.h>
30 #include <asm/unaligned.h>
31 #include "fat.h"
32
33 #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
34 /* if user don't select VFAT, this is undefined. */
35 #define CONFIG_FAT_DEFAULT_IOCHARSET    ""
36 #endif
37
38 static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
39 static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
40
41
42 static int fat_add_cluster(struct inode *inode)
43 {
44         int err, cluster;
45
46         err = fat_alloc_clusters(inode, &cluster, 1);
47         if (err)
48                 return err;
49         /* FIXME: this cluster should be added after data of this
50          * cluster is writed */
51         err = fat_chain_add(inode, cluster, 1);
52         if (err)
53                 fat_free_clusters(inode, cluster);
54         return err;
55 }
56
57 static void check_fallocated_region(struct inode *inode, sector_t iblock,
58                 unsigned long *max_blocks, struct buffer_head *bh_result)
59 {
60         struct super_block *sb = inode->i_sb;
61         sector_t last_block, disk_block;
62         const unsigned long blocksize = sb->s_blocksize;
63         const unsigned char blocksize_bits = sb->s_blocksize_bits;
64
65         last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
66                 >> blocksize_bits;
67         disk_block = (MSDOS_I(inode)->i_disksize + (blocksize - 1))
68                 >> blocksize_bits;
69         if (iblock >= last_block && iblock <= disk_block) {
70                 MSDOS_I(inode)->mmu_private += *max_blocks << blocksize_bits;
71                 set_buffer_new(bh_result);
72         }
73
74 }
75
76 static inline int __fat_get_block(struct inode *inode, sector_t iblock,
77                                   unsigned long *max_blocks,
78                                   struct buffer_head *bh_result, int create)
79 {
80         struct super_block *sb = inode->i_sb;
81         struct msdos_sb_info *sbi = MSDOS_SB(sb);
82         unsigned long mapped_blocks;
83         sector_t phys;
84         int err, offset;
85
86         err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
87         if (err)
88                 return err;
89         if (phys) {
90                 *max_blocks = min(mapped_blocks, *max_blocks);
91                 if (create)
92                         check_fallocated_region(inode, iblock, max_blocks,
93                                 bh_result);
94                 map_bh(bh_result, sb, phys);
95                 return 0;
96         }
97         if (!create)
98                 return 0;
99
100         if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
101                 fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
102                         MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
103                 return -EIO;
104         }
105
106         offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
107         if (!offset) {
108                 /* TODO: multiple cluster allocation would be desirable. */
109                 err = fat_add_cluster(inode);
110                 if (err)
111                         return err;
112         }
113         /* available blocks on this cluster */
114         mapped_blocks = sbi->sec_per_clus - offset;
115
116         *max_blocks = min(mapped_blocks, *max_blocks);
117         MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
118         MSDOS_I(inode)->i_disksize = MSDOS_I(inode)->mmu_private;
119
120         err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
121         if (err)
122                 return err;
123
124         BUG_ON(!phys);
125         BUG_ON(*max_blocks != mapped_blocks);
126         set_buffer_new(bh_result);
127         map_bh(bh_result, sb, phys);
128
129         return 0;
130 }
131
132 static int fat_get_block(struct inode *inode, sector_t iblock,
133                          struct buffer_head *bh_result, int create)
134 {
135         struct super_block *sb = inode->i_sb;
136         unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
137         int err;
138
139         err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
140         if (err)
141                 return err;
142         bh_result->b_size = max_blocks << sb->s_blocksize_bits;
143         return 0;
144 }
145
146 static int fat_writepage(struct page *page, struct writeback_control *wbc)
147 {
148         return block_write_full_page(page, fat_get_block, wbc);
149 }
150
151 static int fat_writepages(struct address_space *mapping,
152                           struct writeback_control *wbc)
153 {
154         return mpage_writepages(mapping, wbc, fat_get_block);
155 }
156
157 static int fat_readpage(struct file *file, struct page *page)
158 {
159         return mpage_readpage(page, fat_get_block);
160 }
161
162 static int fat_readpages(struct file *file, struct address_space *mapping,
163                          struct list_head *pages, unsigned nr_pages)
164 {
165         return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
166 }
167
168 static void fat_write_failed(struct address_space *mapping, loff_t to)
169 {
170         struct inode *inode = mapping->host;
171
172         if (to > inode->i_size) {
173                 truncate_pagecache(inode, inode->i_size);
174                 fat_truncate_blocks(inode, inode->i_size);
175         }
176 }
177
178 static int fat_write_begin(struct file *file, struct address_space *mapping,
179                         loff_t pos, unsigned len, unsigned flags,
180                         struct page **pagep, void **fsdata)
181 {
182         int err;
183
184         *pagep = NULL;
185         err = cont_write_begin(file, mapping, pos, len, flags,
186                                 pagep, fsdata, fat_get_block,
187                                 &MSDOS_I(mapping->host)->mmu_private);
188         if (err < 0)
189                 fat_write_failed(mapping, pos + len);
190         return err;
191 }
192
193 static int fat_write_end(struct file *file, struct address_space *mapping,
194                         loff_t pos, unsigned len, unsigned copied,
195                         struct page *pagep, void *fsdata)
196 {
197         struct inode *inode = mapping->host;
198         int err;
199         err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
200         if (err < len)
201                 fat_write_failed(mapping, pos + len);
202         if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
203                 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
204                 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
205                 mark_inode_dirty(inode);
206         }
207         return err;
208 }
209
210 static ssize_t fat_direct_IO(int rw, struct kiocb *iocb,
211                              const struct iovec *iov,
212                              loff_t offset, unsigned long nr_segs)
213 {
214         struct file *file = iocb->ki_filp;
215         struct address_space *mapping = file->f_mapping;
216         struct inode *inode = mapping->host;
217         ssize_t ret;
218
219         if (rw == WRITE) {
220                 /*
221                  * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
222                  * so we need to update the ->mmu_private to block boundary.
223                  *
224                  * But we must fill the remaining area or hole by nul for
225                  * updating ->mmu_private.
226                  *
227                  * Return 0, and fallback to normal buffered write.
228                  */
229                 loff_t size = offset + iov_length(iov, nr_segs);
230                 if (MSDOS_I(inode)->mmu_private < size)
231                         return 0;
232
233                 /*
234                  * In case of writing in fallocated region, return 0 and
235                  * fallback to buffered write.
236                  */
237                 if (MSDOS_I(inode)->i_disksize > MSDOS_I(inode)->mmu_private)
238                         return 0;
239         }
240
241         /*
242          * FAT need to use the DIO_LOCKING for avoiding the race
243          * condition of fat_get_block() and ->truncate().
244          */
245         ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
246                                  fat_get_block);
247         if (ret < 0 && (rw & WRITE))
248                 fat_write_failed(mapping, offset + iov_length(iov, nr_segs));
249
250         return ret;
251 }
252
253 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
254 {
255         sector_t blocknr;
256
257         /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
258         down_read(&MSDOS_I(mapping->host)->truncate_lock);
259         /* To get block number beyond file size in fallocated region */
260         atomic_set(&MSDOS_I(mapping->host)->beyond_isize, 1);
261         blocknr = generic_block_bmap(mapping, block, fat_get_block);
262         atomic_set(&MSDOS_I(mapping->host)->beyond_isize, 0);
263         up_read(&MSDOS_I(mapping->host)->truncate_lock);
264
265         return blocknr;
266 }
267
268 static const struct address_space_operations fat_aops = {
269         .readpage       = fat_readpage,
270         .readpages      = fat_readpages,
271         .writepage      = fat_writepage,
272         .writepages     = fat_writepages,
273         .write_begin    = fat_write_begin,
274         .write_end      = fat_write_end,
275         .direct_IO      = fat_direct_IO,
276         .bmap           = _fat_bmap
277 };
278
279 /*
280  * New FAT inode stuff. We do the following:
281  *      a) i_ino is constant and has nothing with on-disk location.
282  *      b) FAT manages its own cache of directory entries.
283  *      c) *This* cache is indexed by on-disk location.
284  *      d) inode has an associated directory entry, all right, but
285  *              it may be unhashed.
286  *      e) currently entries are stored within struct inode. That should
287  *              change.
288  *      f) we deal with races in the following way:
289  *              1. readdir() and lookup() do FAT-dir-cache lookup.
290  *              2. rename() unhashes the F-d-c entry and rehashes it in
291  *                      a new place.
292  *              3. unlink() and rmdir() unhash F-d-c entry.
293  *              4. fat_write_inode() checks whether the thing is unhashed.
294  *                      If it is we silently return. If it isn't we do bread(),
295  *                      check if the location is still valid and retry if it
296  *                      isn't. Otherwise we do changes.
297  *              5. Spinlock is used to protect hash/unhash/location check/lookup
298  *              6. fat_evict_inode() unhashes the F-d-c entry.
299  *              7. lookup() and readdir() do igrab() if they find a F-d-c entry
300  *                      and consider negative result as cache miss.
301  */
302
303 static void fat_hash_init(struct super_block *sb)
304 {
305         struct msdos_sb_info *sbi = MSDOS_SB(sb);
306         int i;
307
308         spin_lock_init(&sbi->inode_hash_lock);
309         for (i = 0; i < FAT_HASH_SIZE; i++)
310                 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
311 }
312
313 static inline unsigned long fat_hash(loff_t i_pos)
314 {
315         return hash_32(i_pos, FAT_HASH_BITS);
316 }
317
318 static void dir_hash_init(struct super_block *sb)
319 {
320         struct msdos_sb_info *sbi = MSDOS_SB(sb);
321         int i;
322
323         spin_lock_init(&sbi->dir_hash_lock);
324         for (i = 0; i < FAT_HASH_SIZE; i++)
325                 INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
326 }
327
328 void fat_attach(struct inode *inode, loff_t i_pos)
329 {
330         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
331
332         if (inode->i_ino != MSDOS_ROOT_INO) {
333                 struct hlist_head *head =   sbi->inode_hashtable
334                                           + fat_hash(i_pos);
335
336                 spin_lock(&sbi->inode_hash_lock);
337                 MSDOS_I(inode)->i_pos = i_pos;
338                 hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
339                 spin_unlock(&sbi->inode_hash_lock);
340         }
341
342         /* If NFS support is enabled, cache the mapping of start cluster
343          * to directory inode. This is used during reconnection of
344          * dentries to the filesystem root.
345          */
346         if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
347                 struct hlist_head *d_head = sbi->dir_hashtable;
348                 d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
349
350                 spin_lock(&sbi->dir_hash_lock);
351                 hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
352                 spin_unlock(&sbi->dir_hash_lock);
353         }
354 }
355 EXPORT_SYMBOL_GPL(fat_attach);
356
357 void fat_detach(struct inode *inode)
358 {
359         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
360         spin_lock(&sbi->inode_hash_lock);
361         MSDOS_I(inode)->i_pos = 0;
362         hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
363         spin_unlock(&sbi->inode_hash_lock);
364
365         if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
366                 spin_lock(&sbi->dir_hash_lock);
367                 hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
368                 spin_unlock(&sbi->dir_hash_lock);
369         }
370 }
371 EXPORT_SYMBOL_GPL(fat_detach);
372
373 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
374 {
375         struct msdos_sb_info *sbi = MSDOS_SB(sb);
376         struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
377         struct msdos_inode_info *i;
378         struct inode *inode = NULL;
379
380         spin_lock(&sbi->inode_hash_lock);
381         hlist_for_each_entry(i, head, i_fat_hash) {
382                 BUG_ON(i->vfs_inode.i_sb != sb);
383                 if (i->i_pos != i_pos)
384                         continue;
385                 inode = igrab(&i->vfs_inode);
386                 if (inode)
387                         break;
388         }
389         spin_unlock(&sbi->inode_hash_lock);
390         return inode;
391 }
392
393 static int is_exec(unsigned char *extension)
394 {
395         unsigned char *exe_extensions = "EXECOMBAT", *walk;
396
397         for (walk = exe_extensions; *walk; walk += 3)
398                 if (!strncmp(extension, walk, 3))
399                         return 1;
400         return 0;
401 }
402
403 static int fat_calc_dir_size(struct inode *inode)
404 {
405         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
406         int ret, fclus, dclus;
407
408         inode->i_size = 0;
409         if (MSDOS_I(inode)->i_start == 0)
410                 return 0;
411
412         ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
413         if (ret < 0)
414                 return ret;
415         inode->i_size = (fclus + 1) << sbi->cluster_bits;
416
417         return 0;
418 }
419
420 /* doesn't deal with root inode */
421 int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
422 {
423         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
424         int error;
425
426         MSDOS_I(inode)->i_pos = 0;
427         inode->i_uid = sbi->options.fs_uid;
428         inode->i_gid = sbi->options.fs_gid;
429         inode->i_version++;
430         inode->i_generation = get_seconds();
431
432         if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
433                 inode->i_generation &= ~1;
434                 inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
435                 inode->i_op = sbi->dir_ops;
436                 inode->i_fop = &fat_dir_operations;
437
438                 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
439                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
440                 error = fat_calc_dir_size(inode);
441                 if (error < 0)
442                         return error;
443                 MSDOS_I(inode)->mmu_private = inode->i_size;
444                 MSDOS_I(inode)->i_disksize = inode->i_size;
445
446                 set_nlink(inode, fat_subdirs(inode));
447         } else { /* not a directory */
448                 inode->i_generation |= 1;
449                 inode->i_mode = fat_make_mode(sbi, de->attr,
450                         ((sbi->options.showexec && !is_exec(de->name + 8))
451                          ? S_IRUGO|S_IWUGO : S_IRWXUGO));
452                 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
453
454                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
455                 inode->i_size = le32_to_cpu(de->size);
456                 inode->i_op = &fat_file_inode_operations;
457                 inode->i_fop = &fat_file_operations;
458                 inode->i_mapping->a_ops = &fat_aops;
459                 MSDOS_I(inode)->mmu_private = inode->i_size;
460                 MSDOS_I(inode)->i_disksize = inode->i_size;
461         }
462         if (de->attr & ATTR_SYS) {
463                 if (sbi->options.sys_immutable)
464                         inode->i_flags |= S_IMMUTABLE;
465         }
466         fat_save_attrs(inode, de->attr);
467
468         inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
469                            & ~((loff_t)sbi->cluster_size - 1)) >> 9;
470
471         fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
472         if (sbi->options.isvfat) {
473                 fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
474                                   de->cdate, de->ctime_cs);
475                 fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
476         } else
477                 inode->i_ctime = inode->i_atime = inode->i_mtime;
478
479         return 0;
480 }
481
482 static inline void fat_lock_build_inode(struct msdos_sb_info *sbi)
483 {
484         if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
485                 mutex_lock(&sbi->nfs_build_inode_lock);
486 }
487
488 static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi)
489 {
490         if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
491                 mutex_unlock(&sbi->nfs_build_inode_lock);
492 }
493
494 struct inode *fat_build_inode(struct super_block *sb,
495                         struct msdos_dir_entry *de, loff_t i_pos)
496 {
497         struct inode *inode;
498         int err;
499
500         fat_lock_build_inode(MSDOS_SB(sb));
501         inode = fat_iget(sb, i_pos);
502         if (inode)
503                 goto out;
504         inode = new_inode(sb);
505         if (!inode) {
506                 inode = ERR_PTR(-ENOMEM);
507                 goto out;
508         }
509         inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
510         inode->i_version = 1;
511         err = fat_fill_inode(inode, de);
512         if (err) {
513                 iput(inode);
514                 inode = ERR_PTR(err);
515                 goto out;
516         }
517         fat_attach(inode, i_pos);
518         insert_inode_hash(inode);
519 out:
520         fat_unlock_build_inode(MSDOS_SB(sb));
521         return inode;
522 }
523
524 EXPORT_SYMBOL_GPL(fat_build_inode);
525
526 static void fat_evict_inode(struct inode *inode)
527 {
528         truncate_inode_pages(&inode->i_data, 0);
529         if (!inode->i_nlink) {
530                 inode->i_size = 0;
531                 fat_truncate_blocks(inode, 0);
532         } else {
533                 /* Release unwritten fallocated blocks on inode eviction. */
534                 if (MSDOS_I(inode)->mmu_private < MSDOS_I(inode)->i_disksize) {
535                         int err;
536                         fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
537                         /* Fallocate results in updating the i_start/iogstart
538                          * for the zero byte file. So, make it return to
539                          * original state during evict and commit it
540                          * synchrnously to avoid any corruption on the next
541                          * access to the cluster chain for the file.
542                          */
543                         err = fat_sync_inode(inode);
544                         if (err) {
545                                 fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
546                                 "update on disk inode for unused fallocated "
547                                 "blocks, inode could be corrupted. Please run "
548                                 "fsck");
549                         }
550                 }
551         }
552         invalidate_inode_buffers(inode);
553         clear_inode(inode);
554         fat_cache_inval_inode(inode);
555         fat_detach(inode);
556 }
557
558 static void fat_set_state(struct super_block *sb,
559                         unsigned int set, unsigned int force)
560 {
561         struct buffer_head *bh;
562         struct fat_boot_sector *b;
563         struct msdos_sb_info *sbi = sb->s_fs_info;
564
565         /* do not change any thing if mounted read only */
566         if ((sb->s_flags & MS_RDONLY) && !force)
567                 return;
568
569         /* do not change state if fs was dirty */
570         if (sbi->dirty) {
571                 /* warn only on set (mount). */
572                 if (set)
573                         fat_msg(sb, KERN_WARNING, "Volume was not properly "
574                                 "unmounted. Some data may be corrupt. "
575                                 "Please run fsck.");
576                 return;
577         }
578
579         bh = sb_bread(sb, 0);
580         if (bh == NULL) {
581                 fat_msg(sb, KERN_ERR, "unable to read boot sector "
582                         "to mark fs as dirty");
583                 return;
584         }
585
586         b = (struct fat_boot_sector *) bh->b_data;
587
588         if (sbi->fat_bits == 32) {
589                 if (set)
590                         b->fat32.state |= FAT_STATE_DIRTY;
591                 else
592                         b->fat32.state &= ~FAT_STATE_DIRTY;
593         } else /* fat 16 and 12 */ {
594                 if (set)
595                         b->fat16.state |= FAT_STATE_DIRTY;
596                 else
597                         b->fat16.state &= ~FAT_STATE_DIRTY;
598         }
599
600         mark_buffer_dirty(bh);
601         sync_dirty_buffer(bh);
602         brelse(bh);
603 }
604
605 static void delayed_free(struct rcu_head *p)
606 {
607         struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
608         unload_nls(sbi->nls_disk);
609         unload_nls(sbi->nls_io);
610         if (sbi->options.iocharset != fat_default_iocharset)
611                 kfree(sbi->options.iocharset);
612         kfree(sbi);
613 }
614
615 static void fat_put_super(struct super_block *sb)
616 {
617         struct msdos_sb_info *sbi = MSDOS_SB(sb);
618
619         fat_set_state(sb, 0, 0);
620
621         iput(sbi->fsinfo_inode);
622         iput(sbi->fat_inode);
623
624         call_rcu(&sbi->rcu, delayed_free);
625 }
626
627 static struct kmem_cache *fat_inode_cachep;
628
629 static struct inode *fat_alloc_inode(struct super_block *sb)
630 {
631         struct msdos_inode_info *ei;
632         ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
633         if (!ei)
634                 return NULL;
635
636         init_rwsem(&ei->truncate_lock);
637         return &ei->vfs_inode;
638 }
639
640 static void fat_i_callback(struct rcu_head *head)
641 {
642         struct inode *inode = container_of(head, struct inode, i_rcu);
643         kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
644 }
645
646 static void fat_destroy_inode(struct inode *inode)
647 {
648         call_rcu(&inode->i_rcu, fat_i_callback);
649 }
650
651 static void init_once(void *foo)
652 {
653         struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
654
655         spin_lock_init(&ei->cache_lru_lock);
656         ei->nr_caches = 0;
657         ei->cache_valid_id = FAT_CACHE_VALID + 1;
658         INIT_LIST_HEAD(&ei->cache_lru);
659         INIT_HLIST_NODE(&ei->i_fat_hash);
660         INIT_HLIST_NODE(&ei->i_dir_hash);
661         inode_init_once(&ei->vfs_inode);
662 }
663
664 static int __init fat_init_inodecache(void)
665 {
666         fat_inode_cachep = kmem_cache_create("fat_inode_cache",
667                                              sizeof(struct msdos_inode_info),
668                                              0, (SLAB_RECLAIM_ACCOUNT|
669                                                 SLAB_MEM_SPREAD),
670                                              init_once);
671         if (fat_inode_cachep == NULL)
672                 return -ENOMEM;
673         return 0;
674 }
675
676 static void __exit fat_destroy_inodecache(void)
677 {
678         /*
679          * Make sure all delayed rcu free inodes are flushed before we
680          * destroy cache.
681          */
682         rcu_barrier();
683         kmem_cache_destroy(fat_inode_cachep);
684 }
685
686 static int fat_remount(struct super_block *sb, int *flags, char *data)
687 {
688         int new_rdonly;
689         struct msdos_sb_info *sbi = MSDOS_SB(sb);
690         *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
691
692         /* make sure we update state on remount. */
693         new_rdonly = *flags & MS_RDONLY;
694         if (new_rdonly != (sb->s_flags & MS_RDONLY)) {
695                 if (new_rdonly)
696                         fat_set_state(sb, 0, 0);
697                 else
698                         fat_set_state(sb, 1, 1);
699         }
700         return 0;
701 }
702
703 static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
704 {
705         struct super_block *sb = dentry->d_sb;
706         struct msdos_sb_info *sbi = MSDOS_SB(sb);
707         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
708
709         /* If the count of free cluster is still unknown, counts it here. */
710         if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
711                 int err = fat_count_free_clusters(dentry->d_sb);
712                 if (err)
713                         return err;
714         }
715
716         buf->f_type = dentry->d_sb->s_magic;
717         buf->f_bsize = sbi->cluster_size;
718         buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
719         buf->f_bfree = sbi->free_clusters;
720         buf->f_bavail = sbi->free_clusters;
721         buf->f_fsid.val[0] = (u32)id;
722         buf->f_fsid.val[1] = (u32)(id >> 32);
723         buf->f_namelen =
724                 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
725
726         return 0;
727 }
728
729 static int __fat_write_inode(struct inode *inode, int wait)
730 {
731         struct super_block *sb = inode->i_sb;
732         struct msdos_sb_info *sbi = MSDOS_SB(sb);
733         struct buffer_head *bh;
734         struct msdos_dir_entry *raw_entry;
735         loff_t i_pos;
736         sector_t blocknr;
737         int err, offset;
738
739         if (inode->i_ino == MSDOS_ROOT_INO)
740                 return 0;
741
742 retry:
743         i_pos = fat_i_pos_read(sbi, inode);
744         if (!i_pos)
745                 return 0;
746
747         fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
748         bh = sb_bread(sb, blocknr);
749         if (!bh) {
750                 fat_msg(sb, KERN_ERR, "unable to read inode block "
751                        "for updating (i_pos %lld)", i_pos);
752                 return -EIO;
753         }
754         spin_lock(&sbi->inode_hash_lock);
755         if (i_pos != MSDOS_I(inode)->i_pos) {
756                 spin_unlock(&sbi->inode_hash_lock);
757                 brelse(bh);
758                 goto retry;
759         }
760
761         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
762         if (S_ISDIR(inode->i_mode))
763                 raw_entry->size = 0;
764         else
765                 raw_entry->size = cpu_to_le32(inode->i_size);
766         raw_entry->attr = fat_make_attrs(inode);
767         fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
768         fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
769                           &raw_entry->date, NULL);
770         if (sbi->options.isvfat) {
771                 __le16 atime;
772                 fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
773                                   &raw_entry->cdate, &raw_entry->ctime_cs);
774                 fat_time_unix2fat(sbi, &inode->i_atime, &atime,
775                                   &raw_entry->adate, NULL);
776         }
777         spin_unlock(&sbi->inode_hash_lock);
778         mark_buffer_dirty(bh);
779         err = 0;
780         if (wait)
781                 err = sync_dirty_buffer(bh);
782         brelse(bh);
783         return err;
784 }
785
786 static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
787 {
788         int err;
789
790         if (inode->i_ino == MSDOS_FSINFO_INO) {
791                 struct super_block *sb = inode->i_sb;
792
793                 mutex_lock(&MSDOS_SB(sb)->s_lock);
794                 err = fat_clusters_flush(sb);
795                 mutex_unlock(&MSDOS_SB(sb)->s_lock);
796         } else
797                 err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
798
799         return err;
800 }
801
802 int fat_sync_inode(struct inode *inode)
803 {
804         return __fat_write_inode(inode, 1);
805 }
806
807 EXPORT_SYMBOL_GPL(fat_sync_inode);
808
809 static int fat_show_options(struct seq_file *m, struct dentry *root);
810 static const struct super_operations fat_sops = {
811         .alloc_inode    = fat_alloc_inode,
812         .destroy_inode  = fat_destroy_inode,
813         .write_inode    = fat_write_inode,
814         .evict_inode    = fat_evict_inode,
815         .put_super      = fat_put_super,
816         .statfs         = fat_statfs,
817         .remount_fs     = fat_remount,
818
819         .show_options   = fat_show_options,
820 };
821
822 static int fat_show_options(struct seq_file *m, struct dentry *root)
823 {
824         struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
825         struct fat_mount_options *opts = &sbi->options;
826         int isvfat = opts->isvfat;
827
828         if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
829                 seq_printf(m, ",uid=%u",
830                                 from_kuid_munged(&init_user_ns, opts->fs_uid));
831         if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
832                 seq_printf(m, ",gid=%u",
833                                 from_kgid_munged(&init_user_ns, opts->fs_gid));
834         seq_printf(m, ",fmask=%04o", opts->fs_fmask);
835         seq_printf(m, ",dmask=%04o", opts->fs_dmask);
836         if (opts->allow_utime)
837                 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
838         if (sbi->nls_disk)
839                 /* strip "cp" prefix from displayed option */
840                 seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
841         if (isvfat) {
842                 if (sbi->nls_io)
843                         seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
844
845                 switch (opts->shortname) {
846                 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
847                         seq_puts(m, ",shortname=win95");
848                         break;
849                 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
850                         seq_puts(m, ",shortname=winnt");
851                         break;
852                 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
853                         seq_puts(m, ",shortname=mixed");
854                         break;
855                 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
856                         seq_puts(m, ",shortname=lower");
857                         break;
858                 default:
859                         seq_puts(m, ",shortname=unknown");
860                         break;
861                 }
862         }
863         if (opts->name_check != 'n')
864                 seq_printf(m, ",check=%c", opts->name_check);
865         if (opts->usefree)
866                 seq_puts(m, ",usefree");
867         if (opts->quiet)
868                 seq_puts(m, ",quiet");
869         if (opts->showexec)
870                 seq_puts(m, ",showexec");
871         if (opts->sys_immutable)
872                 seq_puts(m, ",sys_immutable");
873         if (!isvfat) {
874                 if (opts->dotsOK)
875                         seq_puts(m, ",dotsOK=yes");
876                 if (opts->nocase)
877                         seq_puts(m, ",nocase");
878         } else {
879                 if (opts->utf8)
880                         seq_puts(m, ",utf8");
881                 if (opts->unicode_xlate)
882                         seq_puts(m, ",uni_xlate");
883                 if (!opts->numtail)
884                         seq_puts(m, ",nonumtail");
885                 if (opts->rodir)
886                         seq_puts(m, ",rodir");
887         }
888         if (opts->flush)
889                 seq_puts(m, ",flush");
890         if (opts->tz_set) {
891                 if (opts->time_offset)
892                         seq_printf(m, ",time_offset=%d", opts->time_offset);
893                 else
894                         seq_puts(m, ",tz=UTC");
895         }
896         if (opts->errors == FAT_ERRORS_CONT)
897                 seq_puts(m, ",errors=continue");
898         else if (opts->errors == FAT_ERRORS_PANIC)
899                 seq_puts(m, ",errors=panic");
900         else
901                 seq_puts(m, ",errors=remount-ro");
902         if (opts->nfs == FAT_NFS_NOSTALE_RO)
903                 seq_puts(m, ",nfs=nostale_ro");
904         else if (opts->nfs)
905                 seq_puts(m, ",nfs=stale_rw");
906         if (opts->discard)
907                 seq_puts(m, ",discard");
908
909         return 0;
910 }
911
912 enum {
913         Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
914         Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
915         Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
916         Opt_immutable, Opt_dots, Opt_nodots,
917         Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
918         Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
919         Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
920         Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
921         Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset,
922         Opt_nfs_stale_rw, Opt_nfs_nostale_ro, Opt_err,
923 };
924
925 static const match_table_t fat_tokens = {
926         {Opt_check_r, "check=relaxed"},
927         {Opt_check_s, "check=strict"},
928         {Opt_check_n, "check=normal"},
929         {Opt_check_r, "check=r"},
930         {Opt_check_s, "check=s"},
931         {Opt_check_n, "check=n"},
932         {Opt_uid, "uid=%u"},
933         {Opt_gid, "gid=%u"},
934         {Opt_umask, "umask=%o"},
935         {Opt_dmask, "dmask=%o"},
936         {Opt_fmask, "fmask=%o"},
937         {Opt_allow_utime, "allow_utime=%o"},
938         {Opt_codepage, "codepage=%u"},
939         {Opt_usefree, "usefree"},
940         {Opt_nocase, "nocase"},
941         {Opt_quiet, "quiet"},
942         {Opt_showexec, "showexec"},
943         {Opt_debug, "debug"},
944         {Opt_immutable, "sys_immutable"},
945         {Opt_flush, "flush"},
946         {Opt_tz_utc, "tz=UTC"},
947         {Opt_time_offset, "time_offset=%d"},
948         {Opt_err_cont, "errors=continue"},
949         {Opt_err_panic, "errors=panic"},
950         {Opt_err_ro, "errors=remount-ro"},
951         {Opt_discard, "discard"},
952         {Opt_nfs_stale_rw, "nfs"},
953         {Opt_nfs_stale_rw, "nfs=stale_rw"},
954         {Opt_nfs_nostale_ro, "nfs=nostale_ro"},
955         {Opt_obsolete, "conv=binary"},
956         {Opt_obsolete, "conv=text"},
957         {Opt_obsolete, "conv=auto"},
958         {Opt_obsolete, "conv=b"},
959         {Opt_obsolete, "conv=t"},
960         {Opt_obsolete, "conv=a"},
961         {Opt_obsolete, "fat=%u"},
962         {Opt_obsolete, "blocksize=%u"},
963         {Opt_obsolete, "cvf_format=%20s"},
964         {Opt_obsolete, "cvf_options=%100s"},
965         {Opt_obsolete, "posix"},
966         {Opt_err, NULL},
967 };
968 static const match_table_t msdos_tokens = {
969         {Opt_nodots, "nodots"},
970         {Opt_nodots, "dotsOK=no"},
971         {Opt_dots, "dots"},
972         {Opt_dots, "dotsOK=yes"},
973         {Opt_err, NULL}
974 };
975 static const match_table_t vfat_tokens = {
976         {Opt_charset, "iocharset=%s"},
977         {Opt_shortname_lower, "shortname=lower"},
978         {Opt_shortname_win95, "shortname=win95"},
979         {Opt_shortname_winnt, "shortname=winnt"},
980         {Opt_shortname_mixed, "shortname=mixed"},
981         {Opt_utf8_no, "utf8=0"},                /* 0 or no or false */
982         {Opt_utf8_no, "utf8=no"},
983         {Opt_utf8_no, "utf8=false"},
984         {Opt_utf8_yes, "utf8=1"},               /* empty or 1 or yes or true */
985         {Opt_utf8_yes, "utf8=yes"},
986         {Opt_utf8_yes, "utf8=true"},
987         {Opt_utf8_yes, "utf8"},
988         {Opt_uni_xl_no, "uni_xlate=0"},         /* 0 or no or false */
989         {Opt_uni_xl_no, "uni_xlate=no"},
990         {Opt_uni_xl_no, "uni_xlate=false"},
991         {Opt_uni_xl_yes, "uni_xlate=1"},        /* empty or 1 or yes or true */
992         {Opt_uni_xl_yes, "uni_xlate=yes"},
993         {Opt_uni_xl_yes, "uni_xlate=true"},
994         {Opt_uni_xl_yes, "uni_xlate"},
995         {Opt_nonumtail_no, "nonumtail=0"},      /* 0 or no or false */
996         {Opt_nonumtail_no, "nonumtail=no"},
997         {Opt_nonumtail_no, "nonumtail=false"},
998         {Opt_nonumtail_yes, "nonumtail=1"},     /* empty or 1 or yes or true */
999         {Opt_nonumtail_yes, "nonumtail=yes"},
1000         {Opt_nonumtail_yes, "nonumtail=true"},
1001         {Opt_nonumtail_yes, "nonumtail"},
1002         {Opt_rodir, "rodir"},
1003         {Opt_err, NULL}
1004 };
1005
1006 static int parse_options(struct super_block *sb, char *options, int is_vfat,
1007                          int silent, int *debug, struct fat_mount_options *opts)
1008 {
1009         char *p;
1010         substring_t args[MAX_OPT_ARGS];
1011         int option;
1012         char *iocharset;
1013
1014         opts->isvfat = is_vfat;
1015
1016         opts->fs_uid = current_uid();
1017         opts->fs_gid = current_gid();
1018         opts->fs_fmask = opts->fs_dmask = current_umask();
1019         opts->allow_utime = -1;
1020         opts->codepage = fat_default_codepage;
1021         opts->iocharset = fat_default_iocharset;
1022         if (is_vfat) {
1023                 opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
1024                 opts->rodir = 0;
1025         } else {
1026                 opts->shortname = 0;
1027                 opts->rodir = 1;
1028         }
1029         opts->name_check = 'n';
1030         opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
1031         opts->utf8 = opts->unicode_xlate = 0;
1032         opts->numtail = 1;
1033         opts->usefree = opts->nocase = 0;
1034         opts->tz_set = 0;
1035         opts->nfs = 0;
1036         opts->errors = FAT_ERRORS_RO;
1037         *debug = 0;
1038
1039         if (!options)
1040                 goto out;
1041
1042         while ((p = strsep(&options, ",")) != NULL) {
1043                 int token;
1044                 if (!*p)
1045                         continue;
1046
1047                 token = match_token(p, fat_tokens, args);
1048                 if (token == Opt_err) {
1049                         if (is_vfat)
1050                                 token = match_token(p, vfat_tokens, args);
1051                         else
1052                                 token = match_token(p, msdos_tokens, args);
1053                 }
1054                 switch (token) {
1055                 case Opt_check_s:
1056                         opts->name_check = 's';
1057                         break;
1058                 case Opt_check_r:
1059                         opts->name_check = 'r';
1060                         break;
1061                 case Opt_check_n:
1062                         opts->name_check = 'n';
1063                         break;
1064                 case Opt_usefree:
1065                         opts->usefree = 1;
1066                         break;
1067                 case Opt_nocase:
1068                         if (!is_vfat)
1069                                 opts->nocase = 1;
1070                         else {
1071                                 /* for backward compatibility */
1072                                 opts->shortname = VFAT_SFN_DISPLAY_WIN95
1073                                         | VFAT_SFN_CREATE_WIN95;
1074                         }
1075                         break;
1076                 case Opt_quiet:
1077                         opts->quiet = 1;
1078                         break;
1079                 case Opt_showexec:
1080                         opts->showexec = 1;
1081                         break;
1082                 case Opt_debug:
1083                         *debug = 1;
1084                         break;
1085                 case Opt_immutable:
1086                         opts->sys_immutable = 1;
1087                         break;
1088                 case Opt_uid:
1089                         if (match_int(&args[0], &option))
1090                                 return -EINVAL;
1091                         opts->fs_uid = make_kuid(current_user_ns(), option);
1092                         if (!uid_valid(opts->fs_uid))
1093                                 return -EINVAL;
1094                         break;
1095                 case Opt_gid:
1096                         if (match_int(&args[0], &option))
1097                                 return -EINVAL;
1098                         opts->fs_gid = make_kgid(current_user_ns(), option);
1099                         if (!gid_valid(opts->fs_gid))
1100                                 return -EINVAL;
1101                         break;
1102                 case Opt_umask:
1103                         if (match_octal(&args[0], &option))
1104                                 return -EINVAL;
1105                         opts->fs_fmask = opts->fs_dmask = option;
1106                         break;
1107                 case Opt_dmask:
1108                         if (match_octal(&args[0], &option))
1109                                 return -EINVAL;
1110                         opts->fs_dmask = option;
1111                         break;
1112                 case Opt_fmask:
1113                         if (match_octal(&args[0], &option))
1114                                 return -EINVAL;
1115                         opts->fs_fmask = option;
1116                         break;
1117                 case Opt_allow_utime:
1118                         if (match_octal(&args[0], &option))
1119                                 return -EINVAL;
1120                         opts->allow_utime = option & (S_IWGRP | S_IWOTH);
1121                         break;
1122                 case Opt_codepage:
1123                         if (match_int(&args[0], &option))
1124                                 return -EINVAL;
1125                         opts->codepage = option;
1126                         break;
1127                 case Opt_flush:
1128                         opts->flush = 1;
1129                         break;
1130                 case Opt_time_offset:
1131                         if (match_int(&args[0], &option))
1132                                 return -EINVAL;
1133                         if (option < -12 * 60 || option > 12 * 60)
1134                                 return -EINVAL;
1135                         opts->tz_set = 1;
1136                         opts->time_offset = option;
1137                         break;
1138                 case Opt_tz_utc:
1139                         opts->tz_set = 1;
1140                         opts->time_offset = 0;
1141                         break;
1142                 case Opt_err_cont:
1143                         opts->errors = FAT_ERRORS_CONT;
1144                         break;
1145                 case Opt_err_panic:
1146                         opts->errors = FAT_ERRORS_PANIC;
1147                         break;
1148                 case Opt_err_ro:
1149                         opts->errors = FAT_ERRORS_RO;
1150                         break;
1151                 case Opt_nfs_stale_rw:
1152                         opts->nfs = FAT_NFS_STALE_RW;
1153                         break;
1154                 case Opt_nfs_nostale_ro:
1155                         opts->nfs = FAT_NFS_NOSTALE_RO;
1156                         break;
1157
1158                 /* msdos specific */
1159                 case Opt_dots:
1160                         opts->dotsOK = 1;
1161                         break;
1162                 case Opt_nodots:
1163                         opts->dotsOK = 0;
1164                         break;
1165
1166                 /* vfat specific */
1167                 case Opt_charset:
1168                         if (opts->iocharset != fat_default_iocharset)
1169                                 kfree(opts->iocharset);
1170                         iocharset = match_strdup(&args[0]);
1171                         if (!iocharset)
1172                                 return -ENOMEM;
1173                         opts->iocharset = iocharset;
1174                         break;
1175                 case Opt_shortname_lower:
1176                         opts->shortname = VFAT_SFN_DISPLAY_LOWER
1177                                         | VFAT_SFN_CREATE_WIN95;
1178                         break;
1179                 case Opt_shortname_win95:
1180                         opts->shortname = VFAT_SFN_DISPLAY_WIN95
1181                                         | VFAT_SFN_CREATE_WIN95;
1182                         break;
1183                 case Opt_shortname_winnt:
1184                         opts->shortname = VFAT_SFN_DISPLAY_WINNT
1185                                         | VFAT_SFN_CREATE_WINNT;
1186                         break;
1187                 case Opt_shortname_mixed:
1188                         opts->shortname = VFAT_SFN_DISPLAY_WINNT
1189                                         | VFAT_SFN_CREATE_WIN95;
1190                         break;
1191                 case Opt_utf8_no:               /* 0 or no or false */
1192                         opts->utf8 = 0;
1193                         break;
1194                 case Opt_utf8_yes:              /* empty or 1 or yes or true */
1195                         opts->utf8 = 1;
1196                         break;
1197                 case Opt_uni_xl_no:             /* 0 or no or false */
1198                         opts->unicode_xlate = 0;
1199                         break;
1200                 case Opt_uni_xl_yes:            /* empty or 1 or yes or true */
1201                         opts->unicode_xlate = 1;
1202                         break;
1203                 case Opt_nonumtail_no:          /* 0 or no or false */
1204                         opts->numtail = 1;      /* negated option */
1205                         break;
1206                 case Opt_nonumtail_yes:         /* empty or 1 or yes or true */
1207                         opts->numtail = 0;      /* negated option */
1208                         break;
1209                 case Opt_rodir:
1210                         opts->rodir = 1;
1211                         break;
1212                 case Opt_discard:
1213                         opts->discard = 1;
1214                         break;
1215
1216                 /* obsolete mount options */
1217                 case Opt_obsolete:
1218                         fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
1219                                "not supported now", p);
1220                         break;
1221                 /* unknown option */
1222                 default:
1223                         if (!silent) {
1224                                 fat_msg(sb, KERN_ERR,
1225                                        "Unrecognized mount option \"%s\" "
1226                                        "or missing value", p);
1227                         }
1228                         return -EINVAL;
1229                 }
1230         }
1231
1232 out:
1233         /* UTF-8 doesn't provide FAT semantics */
1234         if (!strcmp(opts->iocharset, "utf8")) {
1235                 fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
1236                        " for FAT filesystems, filesystem will be "
1237                        "case sensitive!");
1238         }
1239
1240         /* If user doesn't specify allow_utime, it's initialized from dmask. */
1241         if (opts->allow_utime == (unsigned short)-1)
1242                 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
1243         if (opts->unicode_xlate)
1244                 opts->utf8 = 0;
1245         if (opts->nfs == FAT_NFS_NOSTALE_RO) {
1246                 sb->s_flags |= MS_RDONLY;
1247                 sb->s_export_op = &fat_export_ops_nostale;
1248         }
1249
1250         return 0;
1251 }
1252
1253 static int fat_read_root(struct inode *inode)
1254 {
1255         struct super_block *sb = inode->i_sb;
1256         struct msdos_sb_info *sbi = MSDOS_SB(sb);
1257         int error;
1258
1259         MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
1260         inode->i_uid = sbi->options.fs_uid;
1261         inode->i_gid = sbi->options.fs_gid;
1262         inode->i_version++;
1263         inode->i_generation = 0;
1264         inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
1265         inode->i_op = sbi->dir_ops;
1266         inode->i_fop = &fat_dir_operations;
1267         if (sbi->fat_bits == 32) {
1268                 MSDOS_I(inode)->i_start = sbi->root_cluster;
1269                 error = fat_calc_dir_size(inode);
1270                 if (error < 0)
1271                         return error;
1272         } else {
1273                 MSDOS_I(inode)->i_start = 0;
1274                 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
1275         }
1276         inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1277                            & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1278         MSDOS_I(inode)->i_logstart = 0;
1279         MSDOS_I(inode)->mmu_private = inode->i_size;
1280         MSDOS_I(inode)->i_disksize = inode->i_size;
1281
1282         fat_save_attrs(inode, ATTR_DIR);
1283         inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
1284         inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1285         set_nlink(inode, fat_subdirs(inode)+2);
1286
1287         return 0;
1288 }
1289
1290 static unsigned long calc_fat_clusters(struct super_block *sb)
1291 {
1292         struct msdos_sb_info *sbi = MSDOS_SB(sb);
1293
1294         /* Divide first to avoid overflow */
1295         if (sbi->fat_bits != 12) {
1296                 unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
1297                 return ent_per_sec * sbi->fat_length;
1298         }
1299
1300         return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
1301 }
1302
1303 /*
1304  * Read the super block of an MS-DOS FS.
1305  */
1306 int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
1307                    void (*setup)(struct super_block *))
1308 {
1309         struct inode *root_inode = NULL, *fat_inode = NULL;
1310         struct inode *fsinfo_inode = NULL;
1311         struct buffer_head *bh;
1312         struct fat_boot_sector *b;
1313         struct msdos_sb_info *sbi;
1314         u16 logical_sector_size;
1315         u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
1316         int debug;
1317         unsigned int media;
1318         long error;
1319         char buf[50];
1320
1321         /*
1322          * GFP_KERNEL is ok here, because while we do hold the
1323          * supeblock lock, memory pressure can't call back into
1324          * the filesystem, since we're only just about to mount
1325          * it and have no inodes etc active!
1326          */
1327         sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
1328         if (!sbi)
1329                 return -ENOMEM;
1330         sb->s_fs_info = sbi;
1331
1332         sb->s_flags |= MS_NODIRATIME;
1333         sb->s_magic = MSDOS_SUPER_MAGIC;
1334         sb->s_op = &fat_sops;
1335         sb->s_export_op = &fat_export_ops;
1336         mutex_init(&sbi->nfs_build_inode_lock);
1337         ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1338                              DEFAULT_RATELIMIT_BURST);
1339
1340         error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
1341         if (error)
1342                 goto out_fail;
1343
1344         setup(sb); /* flavour-specific stuff that needs options */
1345
1346         error = -EIO;
1347         sb_min_blocksize(sb, 512);
1348         bh = sb_bread(sb, 0);
1349         if (bh == NULL) {
1350                 fat_msg(sb, KERN_ERR, "unable to read boot sector");
1351                 goto out_fail;
1352         }
1353
1354         b = (struct fat_boot_sector *) bh->b_data;
1355         if (!b->reserved) {
1356                 if (!silent)
1357                         fat_msg(sb, KERN_ERR, "bogus number of reserved sectors");
1358                 brelse(bh);
1359                 goto out_invalid;
1360         }
1361         if (!b->fats) {
1362                 if (!silent)
1363                         fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1364                 brelse(bh);
1365                 goto out_invalid;
1366         }
1367
1368         /*
1369          * Earlier we checked here that b->secs_track and b->head are nonzero,
1370          * but it turns out valid FAT filesystems can have zero there.
1371          */
1372
1373         media = b->media;
1374         if (!fat_valid_media(media)) {
1375                 if (!silent)
1376                         fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
1377                                media);
1378                 brelse(bh);
1379                 goto out_invalid;
1380         }
1381         logical_sector_size = get_unaligned_le16(&b->sector_size);
1382         if (!is_power_of_2(logical_sector_size)
1383             || (logical_sector_size < 512)
1384             || (logical_sector_size > 4096)) {
1385                 if (!silent)
1386                         fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
1387                                logical_sector_size);
1388                 brelse(bh);
1389                 goto out_invalid;
1390         }
1391         sbi->sec_per_clus = b->sec_per_clus;
1392         if (!is_power_of_2(sbi->sec_per_clus)) {
1393                 if (!silent)
1394                         fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
1395                                sbi->sec_per_clus);
1396                 brelse(bh);
1397                 goto out_invalid;
1398         }
1399
1400         if (logical_sector_size < sb->s_blocksize) {
1401                 fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1402                        " (logical sector size = %u)", logical_sector_size);
1403                 brelse(bh);
1404                 goto out_fail;
1405         }
1406         if (logical_sector_size > sb->s_blocksize) {
1407                 brelse(bh);
1408
1409                 if (!sb_set_blocksize(sb, logical_sector_size)) {
1410                         fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
1411                                logical_sector_size);
1412                         goto out_fail;
1413                 }
1414                 bh = sb_bread(sb, 0);
1415                 if (bh == NULL) {
1416                         fat_msg(sb, KERN_ERR, "unable to read boot sector"
1417                                " (logical sector size = %lu)",
1418                                sb->s_blocksize);
1419                         goto out_fail;
1420                 }
1421                 b = (struct fat_boot_sector *) bh->b_data;
1422         }
1423
1424         mutex_init(&sbi->s_lock);
1425         sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
1426         sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
1427         sbi->fats = b->fats;
1428         sbi->fat_bits = 0;              /* Don't know yet */
1429         sbi->fat_start = le16_to_cpu(b->reserved);
1430         sbi->fat_length = le16_to_cpu(b->fat_length);
1431         sbi->root_cluster = 0;
1432         sbi->free_clusters = -1;        /* Don't know yet */
1433         sbi->free_clus_valid = 0;
1434         sbi->prev_free = FAT_START_ENT;
1435         sb->s_maxbytes = 0xffffffff;
1436
1437         if (!sbi->fat_length && b->fat32.length) {
1438                 struct fat_boot_fsinfo *fsinfo;
1439                 struct buffer_head *fsinfo_bh;
1440
1441                 /* Must be FAT32 */
1442                 sbi->fat_bits = 32;
1443                 sbi->fat_length = le32_to_cpu(b->fat32.length);
1444                 sbi->root_cluster = le32_to_cpu(b->fat32.root_cluster);
1445
1446                 /* MC - if info_sector is 0, don't multiply by 0 */
1447                 sbi->fsinfo_sector = le16_to_cpu(b->fat32.info_sector);
1448                 if (sbi->fsinfo_sector == 0)
1449                         sbi->fsinfo_sector = 1;
1450
1451                 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
1452                 if (fsinfo_bh == NULL) {
1453                         fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1454                                " (sector = %lu)", sbi->fsinfo_sector);
1455                         brelse(bh);
1456                         goto out_fail;
1457                 }
1458
1459                 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1460                 if (!IS_FSINFO(fsinfo)) {
1461                         fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1462                                "0x%08x, 0x%08x (sector = %lu)",
1463                                le32_to_cpu(fsinfo->signature1),
1464                                le32_to_cpu(fsinfo->signature2),
1465                                sbi->fsinfo_sector);
1466                 } else {
1467                         if (sbi->options.usefree)
1468                                 sbi->free_clus_valid = 1;
1469                         sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
1470                         sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
1471                 }
1472
1473                 brelse(fsinfo_bh);
1474         }
1475
1476         /* interpret volume ID as a little endian 32 bit integer */
1477         if (sbi->fat_bits == 32)
1478                 sbi->vol_id = (((u32)b->fat32.vol_id[0]) |
1479                                         ((u32)b->fat32.vol_id[1] << 8) |
1480                                         ((u32)b->fat32.vol_id[2] << 16) |
1481                                         ((u32)b->fat32.vol_id[3] << 24));
1482         else /* fat 16 or 12 */
1483                 sbi->vol_id = (((u32)b->fat16.vol_id[0]) |
1484                                         ((u32)b->fat16.vol_id[1] << 8) |
1485                                         ((u32)b->fat16.vol_id[2] << 16) |
1486                                         ((u32)b->fat16.vol_id[3] << 24));
1487
1488         sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
1489         sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
1490
1491         sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1492         sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
1493         if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
1494                 if (!silent)
1495                         fat_msg(sb, KERN_ERR, "bogus directory-entries per block"
1496                                " (%u)", sbi->dir_entries);
1497                 brelse(bh);
1498                 goto out_invalid;
1499         }
1500
1501         rootdir_sectors = sbi->dir_entries
1502                 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
1503         sbi->data_start = sbi->dir_start + rootdir_sectors;
1504         total_sectors = get_unaligned_le16(&b->sectors);
1505         if (total_sectors == 0)
1506                 total_sectors = le32_to_cpu(b->total_sect);
1507
1508         total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
1509
1510         if (sbi->fat_bits != 32)
1511                 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
1512
1513         /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1514         if (sbi->fat_bits == 32)
1515                 sbi->dirty = b->fat32.state & FAT_STATE_DIRTY;
1516         else /* fat 16 or 12 */
1517                 sbi->dirty = b->fat16.state & FAT_STATE_DIRTY;
1518
1519         /* check that FAT table does not overflow */
1520         fat_clusters = calc_fat_clusters(sb);
1521         total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
1522         if (total_clusters > MAX_FAT(sb)) {
1523                 if (!silent)
1524                         fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
1525                                total_clusters);
1526                 brelse(bh);
1527                 goto out_invalid;
1528         }
1529
1530         sbi->max_cluster = total_clusters + FAT_START_ENT;
1531         /* check the free_clusters, it's not necessarily correct */
1532         if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
1533                 sbi->free_clusters = -1;
1534         /* check the prev_free, it's not necessarily correct */
1535         sbi->prev_free %= sbi->max_cluster;
1536         if (sbi->prev_free < FAT_START_ENT)
1537                 sbi->prev_free = FAT_START_ENT;
1538
1539         brelse(bh);
1540
1541         /* set up enough so that it can read an inode */
1542         fat_hash_init(sb);
1543         dir_hash_init(sb);
1544         fat_ent_access_init(sb);
1545
1546         /*
1547          * The low byte of FAT's first entry must have same value with
1548          * media-field.  But in real world, too many devices is
1549          * writing wrong value.  So, removed that validity check.
1550          *
1551          * if (FAT_FIRST_ENT(sb, media) != first)
1552          */
1553
1554         error = -EINVAL;
1555         sprintf(buf, "cp%d", sbi->options.codepage);
1556         sbi->nls_disk = load_nls(buf);
1557         if (!sbi->nls_disk) {
1558                 fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
1559                 goto out_fail;
1560         }
1561
1562         /* FIXME: utf8 is using iocharset for upper/lower conversion */
1563         if (sbi->options.isvfat) {
1564                 sbi->nls_io = load_nls(sbi->options.iocharset);
1565                 if (!sbi->nls_io) {
1566                         fat_msg(sb, KERN_ERR, "IO charset %s not found",
1567                                sbi->options.iocharset);
1568                         goto out_fail;
1569                 }
1570         }
1571
1572         error = -ENOMEM;
1573         fat_inode = new_inode(sb);
1574         if (!fat_inode)
1575                 goto out_fail;
1576         MSDOS_I(fat_inode)->i_pos = 0;
1577         sbi->fat_inode = fat_inode;
1578
1579         fsinfo_inode = new_inode(sb);
1580         if (!fsinfo_inode)
1581                 goto out_fail;
1582         fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
1583         sbi->fsinfo_inode = fsinfo_inode;
1584         insert_inode_hash(fsinfo_inode);
1585
1586         root_inode = new_inode(sb);
1587         if (!root_inode)
1588                 goto out_fail;
1589         root_inode->i_ino = MSDOS_ROOT_INO;
1590         root_inode->i_version = 1;
1591         error = fat_read_root(root_inode);
1592         if (error < 0) {
1593                 iput(root_inode);
1594                 goto out_fail;
1595         }
1596         error = -ENOMEM;
1597         insert_inode_hash(root_inode);
1598         fat_attach(root_inode, 0);
1599         sb->s_root = d_make_root(root_inode);
1600         if (!sb->s_root) {
1601                 fat_msg(sb, KERN_ERR, "get root inode failed");
1602                 goto out_fail;
1603         }
1604
1605         if (sbi->options.discard) {
1606                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1607                 if (!blk_queue_discard(q))
1608                         fat_msg(sb, KERN_WARNING,
1609                                         "mounting with \"discard\" option, but "
1610                                         "the device does not support discard");
1611         }
1612
1613         fat_set_state(sb, 1, 0);
1614         return 0;
1615
1616 out_invalid:
1617         error = -EINVAL;
1618         if (!silent)
1619                 fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
1620
1621 out_fail:
1622         if (fsinfo_inode)
1623                 iput(fsinfo_inode);
1624         if (fat_inode)
1625                 iput(fat_inode);
1626         unload_nls(sbi->nls_io);
1627         unload_nls(sbi->nls_disk);
1628         if (sbi->options.iocharset != fat_default_iocharset)
1629                 kfree(sbi->options.iocharset);
1630         sb->s_fs_info = NULL;
1631         kfree(sbi);
1632         return error;
1633 }
1634
1635 EXPORT_SYMBOL_GPL(fat_fill_super);
1636
1637 /*
1638  * helper function for fat_flush_inodes.  This writes both the inode
1639  * and the file data blocks, waiting for in flight data blocks before
1640  * the start of the call.  It does not wait for any io started
1641  * during the call
1642  */
1643 static int writeback_inode(struct inode *inode)
1644 {
1645
1646         int ret;
1647
1648         /* if we used wait=1, sync_inode_metadata waits for the io for the
1649         * inode to finish.  So wait=0 is sent down to sync_inode_metadata
1650         * and filemap_fdatawrite is used for the data blocks
1651         */
1652         ret = sync_inode_metadata(inode, 0);
1653         if (!ret)
1654                 ret = filemap_fdatawrite(inode->i_mapping);
1655         return ret;
1656 }
1657
1658 /*
1659  * write data and metadata corresponding to i1 and i2.  The io is
1660  * started but we do not wait for any of it to finish.
1661  *
1662  * filemap_flush is used for the block device, so if there is a dirty
1663  * page for a block already in flight, we will not wait and start the
1664  * io over again
1665  */
1666 int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1667 {
1668         int ret = 0;
1669         if (!MSDOS_SB(sb)->options.flush)
1670                 return 0;
1671         if (i1)
1672                 ret = writeback_inode(i1);
1673         if (!ret && i2)
1674                 ret = writeback_inode(i2);
1675         if (!ret) {
1676                 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
1677                 ret = filemap_flush(mapping);
1678         }
1679         return ret;
1680 }
1681 EXPORT_SYMBOL_GPL(fat_flush_inodes);
1682
1683 static int __init init_fat_fs(void)
1684 {
1685         int err;
1686
1687         err = fat_cache_init();
1688         if (err)
1689                 return err;
1690
1691         err = fat_init_inodecache();
1692         if (err)
1693                 goto failed;
1694
1695         return 0;
1696
1697 failed:
1698         fat_cache_destroy();
1699         return err;
1700 }
1701
1702 static void __exit exit_fat_fs(void)
1703 {
1704         fat_cache_destroy();
1705         fat_destroy_inodecache();
1706 }
1707
1708 module_init(init_fat_fs)
1709 module_exit(exit_fat_fs)
1710
1711 MODULE_LICENSE("GPL");