Skip to content

Commit 7669e8f

Browse files
Steven J. Magnanitorvalds
authored andcommitted
fat (exportfs): fix dentry reconnection
Maintain an index of directory inodes by starting cluster, so that fat_get_parent() can return the proper cached inode rather than inventing one that cannot be traced back to the filesystem root. Add a new msdos/vfat binary mount option "nfs" so that FAT filesystems that are _not_ exported via NFS are not saddled with maintenance of an index they will never use. Finally, simplify NFS file handle generation and lookups. An ext2-congruent implementation is adequate for FAT needs. Signed-off-by: Steven J. Magnani <steve@digidescorp.com> Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 21b6633 commit 7669e8f

6 files changed

Lines changed: 141 additions & 137 deletions

File tree

‎fs/fat/dir.c‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -872,21 +872,23 @@ static int fat_get_short_entry(struct inode *dir, loff_t *pos,
872872
}
873873

874874
/*
875-
* The ".." entry can not provide the "struct fat_slot_info" informations
876-
* for inode. So, this function provide the some informations only.
875+
* The ".." entry can not provide the "struct fat_slot_info" information
876+
* for inode, nor a usable i_pos. So, this function provides some information
877+
* only.
878+
*
879+
* Since this function walks through the on-disk inodes within a directory,
880+
* callers are responsible for taking any locks necessary to prevent the
881+
* directory from changing.
877882
*/
878883
int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
879-
struct msdos_dir_entry **de, loff_t *i_pos)
884+
struct msdos_dir_entry **de)
880885
{
881-
loff_t offset;
886+
loff_t offset = 0;
882887

883-
offset = 0;
884-
*bh = NULL;
888+
*de = NULL;
885889
while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
886-
if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
887-
*i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
890+
if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME))
888891
return 0;
889-
}
890892
}
891893
return -ENOENT;
892894
}

‎fs/fat/fat.h‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/string.h>
66
#include <linux/nls.h>
77
#include <linux/fs.h>
8+
#include <linux/hash.h>
89
#include <linux/mutex.h>
910
#include <linux/ratelimit.h>
1011
#include <linux/msdos_fs.h>
@@ -46,7 +47,8 @@ struct fat_mount_options {
4647
usefree:1, /* Use free_clusters for FAT32 */
4748
tz_utc:1, /* Filesystem timestamps are in UTC */
4849
rodir:1, /* allow ATTR_RO for directory */
49-
discard:1; /* Issue discard requests on deletions */
50+
discard:1, /* Issue discard requests on deletions */
51+
nfs:1; /* Do extra work needed for NFS export */
5052
};
5153

5254
#define FAT_HASH_BITS 8
@@ -88,6 +90,9 @@ struct msdos_sb_info {
8890

8991
spinlock_t inode_hash_lock;
9092
struct hlist_head inode_hashtable[FAT_HASH_SIZE];
93+
94+
spinlock_t dir_hash_lock;
95+
struct hlist_head dir_hashtable[FAT_HASH_SIZE];
9196
};
9297

9398
#define FAT_CACHE_VALID 0 /* special case for valid cache */
@@ -110,6 +115,7 @@ struct msdos_inode_info {
110115
int i_attrs; /* unused attribute bits */
111116
loff_t i_pos; /* on-disk position of directory entry or 0 */
112117
struct hlist_node i_fat_hash; /* hash by i_location */
118+
struct hlist_node i_dir_hash; /* hash by i_logstart */
113119
struct rw_semaphore truncate_lock; /* protect bmap against truncate */
114120
struct inode vfs_inode;
115121
};
@@ -262,7 +268,7 @@ extern int fat_subdirs(struct inode *dir);
262268
extern int fat_scan(struct inode *dir, const unsigned char *name,
263269
struct fat_slot_info *sinfo);
264270
extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
265-
struct msdos_dir_entry **de, loff_t *i_pos);
271+
struct msdos_dir_entry **de);
266272
extern int fat_alloc_new_dir(struct inode *dir, struct timespec *ts);
267273
extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
268274
struct fat_slot_info *sinfo);
@@ -341,18 +347,9 @@ extern int fat_fill_super(struct super_block *sb, void *data, int silent,
341347

342348
extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
343349
struct inode *i2);
344-
static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
345-
struct inode *inode)
350+
static inline unsigned long fat_dir_hash(int logstart)
346351
{
347-
loff_t i_pos;
348-
#if BITS_PER_LONG == 32
349-
spin_lock(&sbi->inode_hash_lock);
350-
#endif
351-
i_pos = MSDOS_I(inode)->i_pos;
352-
#if BITS_PER_LONG == 32
353-
spin_unlock(&sbi->inode_hash_lock);
354-
#endif
355-
return i_pos;
352+
return hash_32(logstart, FAT_HASH_BITS);
356353
}
357354

358355
/* fat/misc.c */
@@ -382,10 +379,10 @@ void fat_cache_destroy(void);
382379

383380
/* fat/nfs.c */
384381
struct fid;
385-
extern int fat_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
386-
struct inode *parent);
387382
extern struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
388383
int fh_len, int fh_type);
384+
extern struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
385+
int fh_len, int fh_type);
389386
extern struct dentry *fat_get_parent(struct dentry *child_dir);
390387

391388
/* helper for printk */

‎fs/fat/inode.c‎

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,42 @@ static inline unsigned long fat_hash(loff_t i_pos)
281281
return hash_32(i_pos, FAT_HASH_BITS);
282282
}
283283

284+
static 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+
284294
void fat_attach(struct inode *inode, loff_t i_pos)
285295
{
286296
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
287-
struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
288297

289-
spin_lock(&sbi->inode_hash_lock);
290-
MSDOS_I(inode)->i_pos = i_pos;
291-
hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
292-
spin_unlock(&sbi->inode_hash_lock);
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+
}
293320
}
294321
EXPORT_SYMBOL_GPL(fat_attach);
295322

@@ -300,6 +327,12 @@ void fat_detach(struct inode *inode)
300327
MSDOS_I(inode)->i_pos = 0;
301328
hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
302329
spin_unlock(&sbi->inode_hash_lock);
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+
}
303336
}
304337
EXPORT_SYMBOL_GPL(fat_detach);
305338

@@ -504,6 +537,7 @@ static void init_once(void *foo)
504537
ei->cache_valid_id = FAT_CACHE_VALID + 1;
505538
INIT_LIST_HEAD(&ei->cache_lru);
506539
INIT_HLIST_NODE(&ei->i_fat_hash);
540+
INIT_HLIST_NODE(&ei->i_dir_hash);
507541
inode_init_once(&ei->vfs_inode);
508542
}
509543

@@ -562,6 +596,20 @@ static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
562596
return 0;
563597
}
564598

599+
static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
600+
struct inode *inode)
601+
{
602+
loff_t i_pos;
603+
#if BITS_PER_LONG == 32
604+
spin_lock(&sbi->inode_hash_lock);
605+
#endif
606+
i_pos = MSDOS_I(inode)->i_pos;
607+
#if BITS_PER_LONG == 32
608+
spin_unlock(&sbi->inode_hash_lock);
609+
#endif
610+
return i_pos;
611+
}
612+
565613
static int __fat_write_inode(struct inode *inode, int wait)
566614
{
567615
struct super_block *sb = inode->i_sb;
@@ -655,8 +703,8 @@ static const struct super_operations fat_sops = {
655703
};
656704

657705
static const struct export_operations fat_export_ops = {
658-
.encode_fh = fat_encode_fh,
659706
.fh_to_dentry = fat_fh_to_dentry,
707+
.fh_to_parent = fat_fh_to_parent,
660708
.get_parent = fat_get_parent,
661709
};
662710

@@ -706,6 +754,8 @@ static int fat_show_options(struct seq_file *m, struct dentry *root)
706754
seq_puts(m, ",usefree");
707755
if (opts->quiet)
708756
seq_puts(m, ",quiet");
757+
if (opts->nfs)
758+
seq_puts(m, ",nfs");
709759
if (opts->showexec)
710760
seq_puts(m, ",showexec");
711761
if (opts->sys_immutable)
@@ -750,7 +800,7 @@ enum {
750800
Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
751801
Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
752802
Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
753-
Opt_err_panic, Opt_err_ro, Opt_discard, Opt_err,
803+
Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_err,
754804
};
755805

756806
static const match_table_t fat_tokens = {
@@ -779,6 +829,7 @@ static const match_table_t fat_tokens = {
779829
{Opt_err_panic, "errors=panic"},
780830
{Opt_err_ro, "errors=remount-ro"},
781831
{Opt_discard, "discard"},
832+
{Opt_nfs, "nfs"},
782833
{Opt_obsolete, "conv=binary"},
783834
{Opt_obsolete, "conv=text"},
784835
{Opt_obsolete, "conv=auto"},
@@ -859,6 +910,7 @@ static int parse_options(struct super_block *sb, char *options, int is_vfat,
859910
opts->numtail = 1;
860911
opts->usefree = opts->nocase = 0;
861912
opts->tz_utc = 0;
913+
opts->nfs = 0;
862914
opts->errors = FAT_ERRORS_RO;
863915
*debug = 0;
864916

@@ -1023,6 +1075,9 @@ static int parse_options(struct super_block *sb, char *options, int is_vfat,
10231075
case Opt_discard:
10241076
opts->discard = 1;
10251077
break;
1078+
case Opt_nfs:
1079+
opts->nfs = 1;
1080+
break;
10261081

10271082
/* obsolete mount options */
10281083
case Opt_obsolete:
@@ -1313,6 +1368,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
13131368

13141369
/* set up enough so that it can read an inode */
13151370
fat_hash_init(sb);
1371+
dir_hash_init(sb);
13161372
fat_ent_access_init(sb);
13171373

13181374
/*
@@ -1367,6 +1423,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
13671423
}
13681424
error = -ENOMEM;
13691425
insert_inode_hash(root_inode);
1426+
fat_attach(root_inode, 0);
13701427
sb->s_root = d_make_root(root_inode);
13711428
if (!sb->s_root) {
13721429
fat_msg(sb, KERN_ERR, "get root inode failed");

‎fs/fat/namei_msdos.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,
440440
struct inode *old_inode, *new_inode;
441441
struct fat_slot_info old_sinfo, sinfo;
442442
struct timespec ts;
443-
loff_t dotdot_i_pos, new_i_pos;
443+
loff_t new_i_pos;
444444
int err, old_attrs, is_dir, update_dotdot, corrupt = 0;
445445

446446
old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
@@ -456,8 +456,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,
456456
is_dir = S_ISDIR(old_inode->i_mode);
457457
update_dotdot = (is_dir && old_dir != new_dir);
458458
if (update_dotdot) {
459-
if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
460-
&dotdot_i_pos) < 0) {
459+
if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de)) {
461460
err = -EIO;
462461
goto out;
463462
}

‎fs/fat/namei_vfat.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
914914
struct inode *old_inode, *new_inode;
915915
struct fat_slot_info old_sinfo, sinfo;
916916
struct timespec ts;
917-
loff_t dotdot_i_pos, new_i_pos;
917+
loff_t new_i_pos;
918918
int err, is_dir, update_dotdot, corrupt = 0;
919919
struct super_block *sb = old_dir->i_sb;
920920

@@ -929,8 +929,7 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
929929
is_dir = S_ISDIR(old_inode->i_mode);
930930
update_dotdot = (is_dir && old_dir != new_dir);
931931
if (update_dotdot) {
932-
if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
933-
&dotdot_i_pos) < 0) {
932+
if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de)) {
934933
err = -EIO;
935934
goto out;
936935
}

0 commit comments

Comments
 (0)