blob: 4ff901632b26a194790054d0f29a94f633c6f8c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pagemap.h>
OGAWA Hirofumia5425d22006-01-08 01:02:10 -080019#include <linux/mpage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/buffer_head.h>
21#include <linux/mount.h>
22#include <linux/vfs.h>
23#include <linux/parser.h>
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080024#include <linux/uio.h>
Chris Masonae78bf92006-09-29 02:00:03 -070025#include <linux/writeback.h>
Vignesh Babu BMe7d709c2007-05-08 00:24:27 -070026#include <linux/log2.h>
OGAWA Hirofumid3dfa822008-11-06 12:53:49 -080027#include <linux/hash.h>
Namjae Jeonf5621462012-12-17 16:02:56 -080028#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/unaligned.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080030#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
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
OGAWA Hirofumi6a1d9802006-09-27 01:50:45 -070056static inline int __fat_get_block(struct inode *inode, sector_t iblock,
57 unsigned long *max_blocks,
58 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct super_block *sb = inode->i_sb;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080061 struct msdos_sb_info *sbi = MSDOS_SB(sb);
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080062 unsigned long mapped_blocks;
OGAWA Hirofumi6a1d9802006-09-27 01:50:45 -070063 sector_t phys;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080064 int err, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
OGAWA Hirofumi2bdf67e2008-11-06 12:53:57 -080066 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (err)
68 return err;
69 if (phys) {
70 map_bh(bh_result, sb, phys);
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080071 *max_blocks = min(mapped_blocks, *max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return 0;
73 }
74 if (!create)
75 return 0;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
Denis Karpov85c78592009-06-04 02:34:22 +090078 fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
OGAWA Hirofumi6a1d9802006-09-27 01:50:45 -070079 MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EIO;
81 }
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080082
83 offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
84 if (!offset) {
85 /* TODO: multiple cluster allocation would be desirable. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 err = fat_add_cluster(inode);
87 if (err)
88 return err;
89 }
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080090 /* 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
OGAWA Hirofumi2bdf67e2008-11-06 12:53:57 -080096 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (err)
98 return err;
OGAWA Hirofumi6a1d9802006-09-27 01:50:45 -070099
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800100 BUG_ON(!phys);
101 BUG_ON(*max_blocks != mapped_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 set_buffer_new(bh_result);
103 map_bh(bh_result, sb, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800105 return 0;
106}
107
108static int fat_get_block(struct inode *inode, sector_t iblock,
109 struct buffer_head *bh_result, int create)
110{
OGAWA Hirofumi6a1d9802006-09-27 01:50:45 -0700111 struct super_block *sb = inode->i_sb;
112 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
113 int err;
114
115 err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
116 if (err)
117 return err;
118 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
119 return 0;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int fat_writepage(struct page *page, struct writeback_control *wbc)
123{
124 return block_write_full_page(page, fat_get_block, wbc);
125}
126
OGAWA Hirofumia5425d22006-01-08 01:02:10 -0800127static int fat_writepages(struct address_space *mapping,
128 struct writeback_control *wbc)
129{
130 return mpage_writepages(mapping, wbc, fat_get_block);
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int fat_readpage(struct file *file, struct page *page)
134{
OGAWA Hirofumia5425d22006-01-08 01:02:10 -0800135 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000144static 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
Nick Piggind7777a22007-10-16 01:25:08 -0700154static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000158 int err;
159
Nick Piggind7777a22007-10-16 01:25:08 -0700160 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +0200161 err = cont_write_begin(file, mapping, pos, len, flags,
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000162 pagep, fsdata, fat_get_block,
Nick Piggind7777a22007-10-16 01:25:08 -0700163 &MSDOS_I(mapping->host)->mmu_private);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000164 if (err < 0)
165 fat_write_failed(mapping, pos + len);
166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Nick Piggind7777a22007-10-16 01:25:08 -0700169static 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)
OGAWA Hirofumief402262005-09-16 19:28:13 -0700172{
Nick Piggind7777a22007-10-16 01:25:08 -0700173 struct inode *inode = mapping->host;
174 int err;
175 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000176 if (err < len)
177 fat_write_failed(mapping, pos + len);
Nick Piggind7777a22007-10-16 01:25:08 -0700178 if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
OGAWA Hirofumief402262005-09-16 19:28:13 -0700179 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
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800186static 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;
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000191 struct address_space *mapping = file->f_mapping;
192 struct inode *inode = mapping->host;
193 ssize_t ret;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800194
195 if (rw == WRITE) {
196 /*
Nick Piggin4e02ed42008-10-29 14:00:55 -0700197 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800198 * 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.
OGAWA Hirofumi94412a92007-02-20 13:57:55 -0800202 *
203 * Return 0, and fallback to normal buffered write.
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800204 */
205 loff_t size = offset + iov_length(iov, nr_segs);
206 if (MSDOS_I(inode)->mmu_private < size)
OGAWA Hirofumi94412a92007-02-20 13:57:55 -0800207 return 0;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800208 }
209
210 /*
211 * FAT need to use the DIO_LOCKING for avoiding the race
212 * condition of fat_get_block() and ->truncate().
213 */
Christoph Hellwigaacfc19c2011-06-24 14:29:47 -0400214 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
215 fat_get_block);
npiggin@suse.de459f6ed2010-05-27 01:05:38 +1000216 if (ret < 0 && (rw & WRITE))
217 fat_write_failed(mapping, offset + iov_length(iov, nr_segs));
218
219 return ret;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
223{
OGAWA Hirofumifa93ca12008-11-06 12:53:56 -0800224 sector_t blocknr;
225
226 /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
Christoph Hellwig58268692011-06-24 14:29:40 -0400227 down_read(&MSDOS_I(mapping->host)->truncate_lock);
OGAWA Hirofumifa93ca12008-11-06 12:53:56 -0800228 blocknr = generic_block_bmap(mapping, block, fat_get_block);
Christoph Hellwig58268692011-06-24 14:29:40 -0400229 up_read(&MSDOS_I(mapping->host)->truncate_lock);
OGAWA Hirofumifa93ca12008-11-06 12:53:56 -0800230
231 return blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700234static const struct address_space_operations fat_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .readpage = fat_readpage,
OGAWA Hirofumia5425d22006-01-08 01:02:10 -0800236 .readpages = fat_readpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .writepage = fat_writepage,
OGAWA Hirofumia5425d22006-01-08 01:02:10 -0800238 .writepages = fat_writepages,
Nick Piggind7777a22007-10-16 01:25:08 -0700239 .write_begin = fat_write_begin,
240 .write_end = fat_write_end,
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800241 .direct_IO = fat_direct_IO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .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
Al Virodeee3ce2010-06-05 19:28:32 -0400264 * 6. fat_evict_inode() unhashes the F-d-c entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * 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
OGAWA Hirofumid3dfa822008-11-06 12:53:49 -0800279static inline unsigned long fat_hash(loff_t i_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
OGAWA Hirofumid3dfa822008-11-06 12:53:49 -0800281 return hash_32(i_pos, FAT_HASH_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700284static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294void fat_attach(struct inode *inode, loff_t i_pos)
295{
OGAWA Hirofumid3dfa822008-11-06 12:53:49 -0800296 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700298 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800321EXPORT_SYMBOL_GPL(fat_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
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);
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700330
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800337EXPORT_SYMBOL_GPL(fat_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
340{
341 struct msdos_sb_info *sbi = MSDOS_SB(sb);
OGAWA Hirofumid3dfa822008-11-06 12:53:49 -0800342 struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct msdos_inode_info *i;
344 struct inode *inode = NULL;
345
346 spin_lock(&sbi->inode_hash_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800347 hlist_for_each_entry(i, head, i_fat_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 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 */
Namjae Jeonf1e6fb02013-04-29 16:21:14 -0700387int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
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;
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800400 inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 inode->i_op = sbi->dir_ops;
402 inode->i_fop = &fat_dir_operations;
403
Steven J. Magnania943ed72012-07-30 14:42:13 -0700404 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 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
Miklos Szeredibfe86842011-10-28 14:13:29 +0200411 set_nlink(inode, fat_subdirs(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } else { /* not a directory */
413 inode->i_generation |= 1;
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800414 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));
Steven J. Magnania943ed72012-07-30 14:42:13 -0700417 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
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 }
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800430 fat_save_attrs(inode, de->attr);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
433 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800434
435 fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (sbi->options.isvfat) {
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800437 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 } else
Stephane Kardas62a36c42005-09-21 09:55:45 -0700441 inode->i_ctime = inode->i_atime = inode->i_mtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return 0;
444}
445
Namjae Jeon8fceb4e2013-04-29 16:21:12 -0700446static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458struct 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
Namjae Jeon8fceb4e2013-04-29 16:21:12 -0700464 fat_lock_build_inode(MSDOS_SB(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 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:
Namjae Jeon8fceb4e2013-04-29 16:21:12 -0700484 fat_unlock_build_inode(MSDOS_SB(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return inode;
486}
487
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800488EXPORT_SYMBOL_GPL(fat_build_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Al Virodeee3ce2010-06-05 19:28:32 -0400490static void fat_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Mark Fashehfef26652005-09-09 13:01:31 -0700492 truncate_inode_pages(&inode->i_data, 0);
Al Virodeee3ce2010-06-05 19:28:32 -0400493 if (!inode->i_nlink) {
494 inode->i_size = 0;
495 fat_truncate_blocks(inode, 0);
496 }
497 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200498 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 fat_cache_inval_inode(inode);
OGAWA Hirofumia993b542008-11-06 12:53:50 -0800500 fat_detach(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Oleksij Rempelb88a10582013-02-27 17:03:09 -0800503static 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
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800550static void fat_put_super(struct super_block *sb)
551{
552 struct msdos_sb_info *sbi = MSDOS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Oleksij Rempelb88a10582013-02-27 17:03:09 -0800554 fat_set_state(sb, 0, 0);
555
Artem Bityutskiy020ac5b2012-05-31 16:26:12 -0700556 iput(sbi->fsinfo_inode);
Al Virob5224122009-06-07 13:44:36 -0400557 iput(sbi->fat_inode);
558
Thomas Gleixner6d729e42009-08-16 21:05:08 +0000559 unload_nls(sbi->nls_disk);
560 unload_nls(sbi->nls_io);
561
562 if (sbi->options.iocharset != fat_default_iocharset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 kfree(sbi->options.iocharset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 sb->s_fs_info = NULL;
566 kfree(sbi);
567}
568
Christoph Lametere18b8902006-12-06 20:33:20 -0800569static struct kmem_cache *fat_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571static struct inode *fat_alloc_inode(struct super_block *sb)
572{
573 struct msdos_inode_info *ei;
Linus Torvalds8f593422008-05-19 19:53:01 -0700574 ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!ei)
576 return NULL;
Christoph Hellwig58268692011-06-24 14:29:40 -0400577
578 init_rwsem(&ei->truncate_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return &ei->vfs_inode;
580}
581
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100582static void fat_i_callback(struct rcu_head *head)
583{
584 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100585 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588static void fat_destroy_inode(struct inode *inode)
589{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100590 call_rcu(&inode->i_rcu, fat_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700593static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
596
Christoph Lametera35afb82007-05-16 22:10:57 -0700597 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);
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700602 INIT_HLIST_NODE(&ei->i_dir_hash);
Christoph Lametera35afb82007-05-16 22:10:57 -0700603 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
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),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800610 0, (SLAB_RECLAIM_ACCOUNT|
611 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900612 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (fat_inode_cachep == NULL)
614 return -ENOMEM;
615 return 0;
616}
617
618static void __exit fat_destroy_inodecache(void)
619{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000620 /*
621 * Make sure all delayed rcu free inodes are flushed before we
622 * destroy cache.
623 */
624 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700625 kmem_cache_destroy(fat_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static int fat_remount(struct super_block *sb, int *flags, char *data)
629{
Oleksij Rempelb88a10582013-02-27 17:03:09 -0800630 int new_rdonly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct msdos_sb_info *sbi = MSDOS_SB(sb);
632 *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
Oleksij Rempelb88a10582013-02-27 17:03:09 -0800633
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
643}
644
David Howells726c3342006-06-23 02:02:58 -0700645static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Coly Liaac49b72009-04-02 16:59:35 -0700647 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /* If the count of free cluster is still unknown, counts it here. */
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700652 if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
David Howells726c3342006-06-23 02:02:58 -0700653 int err = fat_count_free_clusters(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (err)
655 return err;
656 }
657
David Howells726c3342006-06-23 02:02:58 -0700658 buf->f_type = dentry->d_sb->s_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 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;
Coly Liaac49b72009-04-02 16:59:35 -0700663 buf->f_fsid.val[0] = (u32)id;
664 buf->f_fsid.val[1] = (u32)(id >> 32);
OGAWA Hirofumif68e5422011-04-12 21:08:39 +0900665 buf->f_namelen =
666 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 return 0;
669}
670
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100671static int __fat_write_inode(struct inode *inode, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
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;
Namjae Jeone22a4442013-04-29 16:21:10 -0700678 sector_t blocknr;
679 int err, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
OGAWA Hirofumi9ca59f42008-11-06 12:53:57 -0800681 if (inode->i_ino == MSDOS_ROOT_INO)
682 return 0;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684retry:
OGAWA Hirofumi9ca59f42008-11-06 12:53:57 -0800685 i_pos = fat_i_pos_read(sbi, inode);
686 if (!i_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688
Namjae Jeone22a4442013-04-29 16:21:10 -0700689 fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
690 bh = sb_bread(sb, blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!bh) {
Alexey Fisher869f58c2011-04-12 21:08:38 +0900692 fat_msg(sb, KERN_ERR, "unable to read inode block "
693 "for updating (i_pos %lld)", i_pos);
Linus Torvalds5f22ca92008-08-20 08:31:19 -0700694 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto retry;
701 }
702
Namjae Jeone22a4442013-04-29 16:21:10 -0700703 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (S_ISDIR(inode->i_mode))
705 raw_entry->size = 0;
706 else
707 raw_entry->size = cpu_to_le32(inode->i_size);
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -0800708 raw_entry->attr = fat_make_attrs(inode);
Steven J. Magnania943ed72012-07-30 14:42:13 -0700709 fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800710 fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
711 &raw_entry->date, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (sbi->options.isvfat) {
Stephane Kardas62a36c42005-09-21 09:55:45 -0700713 __le16 atime;
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800714 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 spin_unlock(&sbi->inode_hash_lock);
720 mark_buffer_dirty(bh);
Linus Torvalds5f22ca92008-08-20 08:31:19 -0700721 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (wait)
723 err = sync_dirty_buffer(bh);
724 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return err;
726}
727
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100728static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
729{
Artem Bityutskiy78491182012-05-31 16:26:13 -0700730 int err;
731
732 if (inode->i_ino == MSDOS_FSINFO_INO) {
733 struct super_block *sb = inode->i_sb;
734
Marco Stornellie40b34c2012-10-06 12:40:03 +0200735 mutex_lock(&MSDOS_SB(sb)->s_lock);
Artem Bityutskiy78491182012-05-31 16:26:13 -0700736 err = fat_clusters_flush(sb);
Marco Stornellie40b34c2012-10-06 12:40:03 +0200737 mutex_unlock(&MSDOS_SB(sb)->s_lock);
Artem Bityutskiy78491182012-05-31 16:26:13 -0700738 } else
739 err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
740
741 return err;
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744int fat_sync_inode(struct inode *inode)
745{
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100746 return __fat_write_inode(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800749EXPORT_SYMBOL_GPL(fat_sync_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Al Viro34c80b12011-12-08 21:32:45 -0500751static int fat_show_options(struct seq_file *m, struct dentry *root);
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800752static const struct super_operations fat_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 .alloc_inode = fat_alloc_inode,
754 .destroy_inode = fat_destroy_inode,
755 .write_inode = fat_write_inode,
Al Virodeee3ce2010-06-05 19:28:32 -0400756 .evict_inode = fat_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .put_super = fat_put_super,
758 .statfs = fat_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .remount_fs = fat_remount,
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .show_options = fat_show_options,
762};
763
Al Viro34c80b12011-12-08 21:32:45 -0500764static int fat_show_options(struct seq_file *m, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Al Viro34c80b12011-12-08 21:32:45 -0500766 struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct fat_mount_options *opts = &sbi->options;
768 int isvfat = opts->isvfat;
769
Eric W. Biederman170782e2012-02-07 16:25:39 -0800770 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
777 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700778 if (opts->allow_utime)
779 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (sbi->nls_disk)
Dave Reisnerc6c20372012-12-17 16:03:01 -0800781 /* strip "cp" prefix from displayed option */
782 seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 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:
Paul Wise95523472009-08-01 21:30:31 +0900798 seq_puts(m, ",shortname=lower");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 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);
OGAWA Hirofumi28ec0392007-05-08 00:31:01 -0700807 if (opts->usefree)
808 seq_puts(m, ",usefree");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 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");
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800827 if (opts->rodir)
828 seq_puts(m, ",rodir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800830 if (opts->flush)
Miklos Szeredic1fca3b2008-02-08 04:21:42 -0800831 seq_puts(m, ",flush");
Jan Kara58156c82012-12-17 16:02:58 -0800832 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 }
Denis Karpov85c78592009-06-04 02:34:22 +0900838 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");
Namjae Jeon2628b7a2013-04-29 16:21:08 -0700844 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");
Christoph Hellwig681142f2009-11-21 20:28:52 +0900848 if (opts->discard)
849 seq_puts(m, ",discard");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 return 0;
852}
853
854enum {
855 Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700856 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 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,
Geert Uytterhoevend7a83c02011-10-31 20:50:45 +0100862 Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
Jan Kara58156c82012-12-17 16:02:58 -0800863 Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset,
Namjae Jeon2628b7a2013-04-29 16:21:08 -0700864 Opt_nfs_stale_rw, Opt_nfs_nostale_ro, Opt_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
Steven Whitehousea447c092008-10-13 10:46:57 +0100867static const match_table_t fat_tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 {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"},
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700879 {Opt_allow_utime, "allow_utime=%o"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 {Opt_codepage, "codepage=%u"},
OGAWA Hirofumi28ec0392007-05-08 00:31:01 -0700881 {Opt_usefree, "usefree"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 {Opt_nocase, "nocase"},
883 {Opt_quiet, "quiet"},
884 {Opt_showexec, "showexec"},
885 {Opt_debug, "debug"},
886 {Opt_immutable, "sys_immutable"},
Denis Karpov85c78592009-06-04 02:34:22 +0900887 {Opt_flush, "flush"},
888 {Opt_tz_utc, "tz=UTC"},
Jan Kara58156c82012-12-17 16:02:58 -0800889 {Opt_time_offset, "time_offset=%d"},
Denis Karpov85c78592009-06-04 02:34:22 +0900890 {Opt_err_cont, "errors=continue"},
891 {Opt_err_panic, "errors=panic"},
892 {Opt_err_ro, "errors=remount-ro"},
Christoph Hellwig681142f2009-11-21 20:28:52 +0900893 {Opt_discard, "discard"},
Namjae Jeon2628b7a2013-04-29 16:21:08 -0700894 {Opt_nfs_stale_rw, "nfs"},
895 {Opt_nfs_stale_rw, "nfs=stale_rw"},
896 {Opt_nfs_nostale_ro, "nfs=nostale_ro"},
Geert Uytterhoevend7a83c02011-10-31 20:50:45 +0100897 {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"},
Chris Masonae78bf92006-09-29 02:00:03 -0700908 {Opt_err, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909};
Steven Whitehousea447c092008-10-13 10:46:57 +0100910static const match_table_t msdos_tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 {Opt_nodots, "nodots"},
912 {Opt_nodots, "dotsOK=no"},
913 {Opt_dots, "dots"},
914 {Opt_dots, "dotsOK=yes"},
915 {Opt_err, NULL}
916};
Steven Whitehousea447c092008-10-13 10:46:57 +0100917static const match_table_t vfat_tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 {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"},
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800944 {Opt_rodir, "rodir"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 {Opt_err, NULL}
946};
947
Alexey Fisher869f58c2011-04-12 21:08:38 +0900948static int parse_options(struct super_block *sb, char *options, int is_vfat,
949 int silent, int *debug, struct fat_mount_options *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 char *p;
952 substring_t args[MAX_OPT_ARGS];
953 int option;
954 char *iocharset;
955
956 opts->isvfat = is_vfat;
957
David Howellsf0ce7ee2008-11-14 10:38:52 +1100958 opts->fs_uid = current_uid();
959 opts->fs_gid = current_gid();
OGAWA Hirofumi3e107602009-06-20 21:50:07 +0900960 opts->fs_fmask = opts->fs_dmask = current_umask();
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -0700961 opts->allow_utime = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 opts->codepage = fat_default_codepage;
963 opts->iocharset = fat_default_iocharset;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800964 if (is_vfat) {
Paul Wise95523472009-08-01 21:30:31 +0900965 opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800966 opts->rodir = 0;
967 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 opts->shortname = 0;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -0800969 opts->rodir = 1;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 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;
OGAWA Hirofumi28ec0392007-05-08 00:31:01 -0700975 opts->usefree = opts->nocase = 0;
Jan Kara58156c82012-12-17 16:02:58 -0800976 opts->tz_set = 0;
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700977 opts->nfs = 0;
Denis Karpov85c78592009-06-04 02:34:22 +0900978 opts->errors = FAT_ERRORS_RO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 *debug = 0;
980
981 if (!options)
OGAWA Hirofumi8d44d972008-07-25 01:46:41 -0700982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
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;
OGAWA Hirofumi28ec0392007-05-08 00:31:01 -07001006 case Opt_usefree:
1007 opts->usefree = 1;
1008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 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))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001032 return -EINVAL;
Eric W. Biederman170782e2012-02-07 16:25:39 -08001033 opts->fs_uid = make_kuid(current_user_ns(), option);
1034 if (!uid_valid(opts->fs_uid))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001035 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037 case Opt_gid:
1038 if (match_int(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001039 return -EINVAL;
Eric W. Biederman170782e2012-02-07 16:25:39 -08001040 opts->fs_gid = make_kgid(current_user_ns(), option);
1041 if (!gid_valid(opts->fs_gid))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001042 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 case Opt_umask:
1045 if (match_octal(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001046 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 opts->fs_fmask = opts->fs_dmask = option;
1048 break;
1049 case Opt_dmask:
1050 if (match_octal(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 opts->fs_dmask = option;
1053 break;
1054 case Opt_fmask:
1055 if (match_octal(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001056 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 opts->fs_fmask = option;
1058 break;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -07001059 case Opt_allow_utime:
1060 if (match_octal(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001061 return -EINVAL;
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -07001062 opts->allow_utime = option & (S_IWGRP | S_IWOTH);
1063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 case Opt_codepage:
1065 if (match_int(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001066 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 opts->codepage = option;
1068 break;
Chris Masonae78bf92006-09-29 02:00:03 -07001069 case Opt_flush:
1070 opts->flush = 1;
1071 break;
Jan Kara58156c82012-12-17 16:02:58 -08001072 case Opt_time_offset:
1073 if (match_int(&args[0], &option))
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001074 return -EINVAL;
Jan Kara58156c82012-12-17 16:02:58 -08001075 if (option < -12 * 60 || option > 12 * 60)
Jan Kara5b3d5ae2012-12-17 16:02:59 -08001076 return -EINVAL;
Jan Kara58156c82012-12-17 16:02:58 -08001077 opts->tz_set = 1;
1078 opts->time_offset = option;
1079 break;
Joe Petersonb271e062008-07-25 01:46:47 -07001080 case Opt_tz_utc:
Jan Kara58156c82012-12-17 16:02:58 -08001081 opts->tz_set = 1;
1082 opts->time_offset = 0;
Joe Petersonb271e062008-07-25 01:46:47 -07001083 break;
Denis Karpov85c78592009-06-04 02:34:22 +09001084 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;
Namjae Jeon2628b7a2013-04-29 16:21:08 -07001093 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
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;
OGAWA Hirofumidfc209c2008-11-06 12:53:55 -08001151 case Opt_rodir:
1152 opts->rodir = 1;
1153 break;
Christoph Hellwig681142f2009-11-21 20:28:52 +09001154 case Opt_discard:
1155 opts->discard = 1;
1156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* obsolete mount options */
Geert Uytterhoevend7a83c02011-10-31 20:50:45 +01001159 case Opt_obsolete:
Alexey Fisher869f58c2011-04-12 21:08:38 +09001160 fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
1161 "not supported now", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 break;
1163 /* unknown option */
1164 default:
Christoph Hellwig41a34a42005-11-08 21:34:57 -08001165 if (!silent) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001166 fat_msg(sb, KERN_ERR,
1167 "Unrecognized mount option \"%s\" "
1168 "or missing value", p);
Christoph Hellwig41a34a42005-11-08 21:34:57 -08001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return -EINVAL;
1171 }
1172 }
OGAWA Hirofumi8d44d972008-07-25 01:46:41 -07001173
1174out:
Alexey Dobriyan4de151d2006-03-22 00:13:35 +01001175 /* UTF-8 doesn't provide FAT semantics */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!strcmp(opts->iocharset, "utf8")) {
Mihai Moldovan186b5372011-08-17 19:10:08 +09001177 fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
OGAWA Hirofumi8d44d972008-07-25 01:46:41 -07001178 " for FAT filesystems, filesystem will be "
Mihai Moldovan186b5372011-08-17 19:10:08 +09001179 "case sensitive!");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
OGAWA Hirofumi1ae43f82008-04-28 02:16:26 -07001182 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (opts->unicode_xlate)
1186 opts->utf8 = 0;
Namjae Jeonea3983a2013-04-29 16:21:11 -07001187 if (opts->nfs == FAT_NFS_NOSTALE_RO) {
Namjae Jeon2628b7a2013-04-29 16:21:08 -07001188 sb->s_flags |= MS_RDONLY;
Namjae Jeonea3983a2013-04-29 16:21:11 -07001189 sb->s_export_op = &fat_export_ops_nostale;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
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
Namjae Jeonea3983a2013-04-29 16:21:11 -07001201 MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 inode->i_uid = sbi->options.fs_uid;
1203 inode->i_gid = sbi->options.fs_gid;
1204 inode->i_version++;
1205 inode->i_generation = 0;
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -08001206 inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 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
OGAWA Hirofumi9c0aa1b2008-11-06 12:53:54 -08001223 fat_save_attrs(inode, ATTR_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 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;
Miklos Szeredibfe86842011-10-28 14:13:29 +02001226 set_nlink(inode, fat_subdirs(inode)+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 return 0;
1229}
1230
1231/*
1232 * Read the super block of an MS-DOS FS.
1233 */
OGAWA Hirofumi384f5c92011-04-12 21:08:37 +09001234int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
Al Viro3d239852010-12-18 10:44:00 -05001235 void (*setup)(struct super_block *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Al Virob5224122009-06-07 13:44:36 -04001237 struct inode *root_inode = NULL, *fat_inode = NULL;
Artem Bityutskiy020ac5b2012-05-31 16:26:12 -07001238 struct inode *fsinfo_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 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
Linus Torvalds8f593422008-05-19 19:53:01 -07001249 /*
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 */
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -07001255 sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!sbi)
1257 return -ENOMEM;
1258 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
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;
Namjae Jeon8fceb4e2013-04-29 16:21:12 -07001264 mutex_init(&sbi->nfs_build_inode_lock);
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -07001265 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1266 DEFAULT_RATELIMIT_BURST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Alexey Fisher869f58c2011-04-12 21:08:38 +09001268 error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (error)
1270 goto out_fail;
1271
Al Viro3d239852010-12-18 10:44:00 -05001272 setup(sb); /* flavour-specific stuff that needs options */
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 error = -EIO;
1275 sb_min_blocksize(sb, 512);
1276 bh = sb_bread(sb, 0);
1277 if (bh == NULL) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001278 fat_msg(sb, KERN_ERR, "unable to read boot sector");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 goto out_fail;
1280 }
1281
1282 b = (struct fat_boot_sector *) bh->b_data;
1283 if (!b->reserved) {
1284 if (!silent)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001285 fat_msg(sb, KERN_ERR, "bogus number of reserved sectors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 brelse(bh);
1287 goto out_invalid;
1288 }
1289 if (!b->fats) {
1290 if (!silent)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001291 fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 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;
Andrew Morton73f20e52008-04-28 02:16:30 -07001302 if (!fat_valid_media(media)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!silent)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001304 fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 media);
1306 brelse(bh);
1307 goto out_invalid;
1308 }
Harvey Harrison803f4452008-04-29 01:03:43 -07001309 logical_sector_size = get_unaligned_le16(&b->sector_size);
Vignesh Babu BMe7d709c2007-05-08 00:24:27 -07001310 if (!is_power_of_2(logical_sector_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 || (logical_sector_size < 512)
Olof Johansson9f354852008-04-28 02:16:32 -07001312 || (logical_sector_size > 4096)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!silent)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001314 fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 logical_sector_size);
1316 brelse(bh);
1317 goto out_invalid;
1318 }
1319 sbi->sec_per_clus = b->sec_per_clus;
Vignesh Babu BMe7d709c2007-05-08 00:24:27 -07001320 if (!is_power_of_2(sbi->sec_per_clus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (!silent)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001322 fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 sbi->sec_per_clus);
1324 brelse(bh);
1325 goto out_invalid;
1326 }
1327
1328 if (logical_sector_size < sb->s_blocksize) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001329 fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1330 " (logical sector size = %u)", logical_sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 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)) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001338 fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 logical_sector_size);
1340 goto out_fail;
1341 }
1342 bh = sb_bread(sb, 0);
1343 if (bh == NULL) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001344 fat_msg(sb, KERN_ERR, "unable to read boot sector"
1345 " (logical sector size = %lu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 sb->s_blocksize);
1347 goto out_fail;
1348 }
1349 b = (struct fat_boot_sector *) bh->b_data;
1350 }
1351
Marco Stornellie40b34c2012-10-06 12:40:03 +02001352 mutex_init(&sbi->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 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 */
OGAWA Hirofumi606e4232008-04-28 02:16:27 -07001361 sbi->free_clus_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sbi->prev_free = FAT_START_ENT;
Namjae Jeon710d4402011-08-17 19:10:09 +09001363 sb->s_maxbytes = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Oleksij Rempel6b464192013-02-27 17:03:07 -08001365 if (!sbi->fat_length && b->fat32.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct fat_boot_fsinfo *fsinfo;
1367 struct buffer_head *fsinfo_bh;
1368
1369 /* Must be FAT32 */
1370 sbi->fat_bits = 32;
Oleksij Rempel6b464192013-02-27 17:03:07 -08001371 sbi->fat_length = le32_to_cpu(b->fat32.length);
1372 sbi->root_cluster = le32_to_cpu(b->fat32.root_cluster);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* MC - if info_sector is 0, don't multiply by 0 */
Oleksij Rempel6b464192013-02-27 17:03:07 -08001375 sbi->fsinfo_sector = le16_to_cpu(b->fat32.info_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 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) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001381 fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1382 " (sector = %lu)", sbi->fsinfo_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 brelse(bh);
1384 goto out_fail;
1385 }
1386
1387 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1388 if (!IS_FSINFO(fsinfo)) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001389 fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1390 "0x%08x, 0x%08x (sector = %lu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 le32_to_cpu(fsinfo->signature1),
1392 le32_to_cpu(fsinfo->signature2),
1393 sbi->fsinfo_sector);
1394 } else {
OGAWA Hirofumi28ec0392007-05-08 00:31:01 -07001395 if (sbi->options.usefree)
OGAWA Hirofumi606e4232008-04-28 02:16:27 -07001396 sbi->free_clus_valid = 1;
1397 sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 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;
Harvey Harrison803f4452008-04-29 01:03:43 -07001408 sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
1410 if (!silent)
Ravishankar Nc39540c2012-12-20 15:05:46 -08001411 fat_msg(sb, KERN_ERR, "bogus directory-entries per block"
Alexey Fisher869f58c2011-04-12 21:08:38 +09001412 " (%u)", sbi->dir_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 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;
Harvey Harrison803f4452008-04-29 01:03:43 -07001420 total_sectors = get_unaligned_le16(&b->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 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
Oleksij Rempelb88a10582013-02-27 17:03:09 -08001429 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* 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)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001440 fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 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);
Steven J. Magnani7669e8f2012-10-04 17:14:45 -07001459 dir_hash_init(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 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) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001474 fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 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) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001482 fat_msg(sb, KERN_ERR, "IO charset %s not found",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 sbi->options.iocharset);
1484 goto out_fail;
1485 }
1486 }
1487
1488 error = -ENOMEM;
Al Virob5224122009-06-07 13:44:36 -04001489 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;
Artem Bityutskiy020ac5b2012-05-31 16:26:12 -07001494
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 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);
Al Viro1688f862012-02-12 22:06:33 -05001508 if (error < 0) {
1509 iput(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 goto out_fail;
Al Viro1688f862012-02-12 22:06:33 -05001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 error = -ENOMEM;
1513 insert_inode_hash(root_inode);
Steven J. Magnani7669e8f2012-10-04 17:14:45 -07001514 fat_attach(root_inode, 0);
Al Viro1688f862012-02-12 22:06:33 -05001515 sb->s_root = d_make_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (!sb->s_root) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001517 fat_msg(sb, KERN_ERR, "get root inode failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 goto out_fail;
1519 }
1520
Namjae Jeonf5621462012-12-17 16:02:56 -08001521 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
Oleksij Rempelb88a10582013-02-27 17:03:09 -08001529 fat_set_state(sb, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 0;
1531
1532out_invalid:
1533 error = -EINVAL;
1534 if (!silent)
Alexey Fisher869f58c2011-04-12 21:08:38 +09001535 fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537out_fail:
Artem Bityutskiy020ac5b2012-05-31 16:26:12 -07001538 if (fsinfo_inode)
1539 iput(fsinfo_inode);
Al Virob5224122009-06-07 13:44:36 -04001540 if (fat_inode)
1541 iput(fat_inode);
OGAWA Hirofumi1bdb6f92010-03-16 05:48:09 +09001542 unload_nls(sbi->nls_io);
1543 unload_nls(sbi->nls_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 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
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001551EXPORT_SYMBOL_GPL(fat_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Chris Masonae78bf92006-09-29 02:00:03 -07001553/*
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;
Namjae Jeon14864652012-10-04 17:15:04 -07001563
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
Chris Masonae78bf92006-09-29 02:00:03 -07001566 * and filemap_fdatawrite is used for the data blocks
1567 */
Namjae Jeon14864652012-10-04 17:15:04 -07001568 ret = sync_inode_metadata(inode, 0);
Chris Masonae78bf92006-09-29 02:00:03 -07001569 if (!ret)
Namjae Jeon14864652012-10-04 17:15:04 -07001570 ret = filemap_fdatawrite(inode->i_mapping);
Chris Masonae78bf92006-09-29 02:00:03 -07001571 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);
Eric Sesterhenn97e860d2006-10-11 01:21:59 -07001591 if (!ret) {
Chris Masonae78bf92006-09-29 02:00:03 -07001592 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static int __init init_fat_fs(void)
1600{
Pekka J Enberg532a39a2005-06-30 02:59:01 -07001601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Pekka J Enberg532a39a2005-06-30 02:59:01 -07001603 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
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");