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