Skip to content

Commit d7dab39

Browse files
Eric Sandeenjankara
authored andcommitted
ext3: return 32/64-bit dir name hash according to usage type
This is based on commit d1f5273 ext4: return 32/64-bit dir name hash according to usage type by Fan Yong <yong.fan@whamcloud.com> Traditionally ext2/3/4 has returned a 32-bit hash value from llseek() to appease NFSv2, which can only handle a 32-bit cookie for seekdir() and telldir(). However, this causes problems if there are 32-bit hash collisions, since the NFSv2 server can get stuck resending the same entries from the directory repeatedly. Allow ext3 to return a full 64-bit hash (both major and minor) for telldir to decrease the chance of hash collisions. This patch does implement a new ext3_dir_llseek op, because with 64-bit hashes, nfs will attempt to seek to a hash "offset" which is much larger than ext3's s_maxbytes. So for dx dirs, we call generic_file_llseek_size() with the appropriate max hash value as the maximum seekable size. Otherwise we just pass through to generic_file_llseek(). Patch-updated-by: Bernd Schubert <bernd.schubert@itwm.fraunhofer.de> Patch-updated-by: Eric Sandeen <sandeen@redhat.com> (blame us if something is not correct) Signed-off-by: Eric Sandeen <sandeen@redhat.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent a80b12c commit d7dab39

3 files changed

Lines changed: 129 additions & 48 deletions

File tree

‎fs/ext3/dir.c‎

Lines changed: 122 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,15 @@
2121
*
2222
*/
2323

24+
#include <linux/compat.h>
2425
#include "ext3.h"
2526

2627
static unsigned char ext3_filetype_table[] = {
2728
DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2829
};
2930

30-
static int ext3_readdir(struct file *, void *, filldir_t);
3131
static int ext3_dx_readdir(struct file * filp,
3232
void * dirent, filldir_t filldir);
33-
static int ext3_release_dir (struct inode * inode,
34-
struct file * filp);
35-
36-
const struct file_operations ext3_dir_operations = {
37-
.llseek = generic_file_llseek,
38-
.read = generic_read_dir,
39-
.readdir = ext3_readdir, /* we take BKL. needed?*/
40-
.unlocked_ioctl = ext3_ioctl,
41-
#ifdef CONFIG_COMPAT
42-
.compat_ioctl = ext3_compat_ioctl,
43-
#endif
44-
.fsync = ext3_sync_file, /* BKL held */
45-
.release = ext3_release_dir,
46-
};
47-
4833

4934
static unsigned char get_dtype(struct super_block *sb, int filetype)
5035
{
@@ -55,6 +40,25 @@ static unsigned char get_dtype(struct super_block *sb, int filetype)
5540
return (ext3_filetype_table[filetype]);
5641
}
5742

43+
/**
44+
* Check if the given dir-inode refers to an htree-indexed directory
45+
* (or a directory which chould potentially get coverted to use htree
46+
* indexing).
47+
*
48+
* Return 1 if it is a dx dir, 0 if not
49+
*/
50+
static int is_dx_dir(struct inode *inode)
51+
{
52+
struct super_block *sb = inode->i_sb;
53+
54+
if (EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
55+
EXT3_FEATURE_COMPAT_DIR_INDEX) &&
56+
((EXT3_I(inode)->i_flags & EXT3_INDEX_FL) ||
57+
((inode->i_size >> sb->s_blocksize_bits) == 1)))
58+
return 1;
59+
60+
return 0;
61+
}
5862

5963
int ext3_check_dir_entry (const char * function, struct inode * dir,
6064
struct ext3_dir_entry_2 * de,
@@ -94,18 +98,13 @@ static int ext3_readdir(struct file * filp,
9498
unsigned long offset;
9599
int i, stored;
96100
struct ext3_dir_entry_2 *de;
97-
struct super_block *sb;
98101
int err;
99102
struct inode *inode = filp->f_path.dentry->d_inode;
103+
struct super_block *sb = inode->i_sb;
100104
int ret = 0;
101105
int dir_has_error = 0;
102106

103-
sb = inode->i_sb;
104-
105-
if (EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
106-
EXT3_FEATURE_COMPAT_DIR_INDEX) &&
107-
((EXT3_I(inode)->i_flags & EXT3_INDEX_FL) ||
108-
((inode->i_size >> sb->s_blocksize_bits) == 1))) {
107+
if (is_dx_dir(inode)) {
109108
err = ext3_dx_readdir(filp, dirent, filldir);
110109
if (err != ERR_BAD_DX_DIR) {
111110
ret = err;
@@ -227,22 +226,87 @@ static int ext3_readdir(struct file * filp,
227226
return ret;
228227
}
229228

229+
static inline int is_32bit_api(void)
230+
{
231+
#ifdef CONFIG_COMPAT
232+
return is_compat_task();
233+
#else
234+
return (BITS_PER_LONG == 32);
235+
#endif
236+
}
237+
230238
/*
231239
* These functions convert from the major/minor hash to an f_pos
232-
* value.
240+
* value for dx directories
233241
*
234-
* Currently we only use major hash numer. This is unfortunate, but
235-
* on 32-bit machines, the same VFS interface is used for lseek and
236-
* llseek, so if we use the 64 bit offset, then the 32-bit versions of
237-
* lseek/telldir/seekdir will blow out spectacularly, and from within
238-
* the ext2 low-level routine, we don't know if we're being called by
239-
* a 64-bit version of the system call or the 32-bit version of the
240-
* system call. Worse yet, NFSv2 only allows for a 32-bit readdir
241-
* cookie. Sigh.
242+
* Upper layer (for example NFS) should specify FMODE_32BITHASH or
243+
* FMODE_64BITHASH explicitly. On the other hand, we allow ext3 to be mounted
244+
* directly on both 32-bit and 64-bit nodes, under such case, neither
245+
* FMODE_32BITHASH nor FMODE_64BITHASH is specified.
242246
*/
243-
#define hash2pos(major, minor) (major >> 1)
244-
#define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
245-
#define pos2min_hash(pos) (0)
247+
static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
248+
{
249+
if ((filp->f_mode & FMODE_32BITHASH) ||
250+
(!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
251+
return major >> 1;
252+
else
253+
return ((__u64)(major >> 1) << 32) | (__u64)minor;
254+
}
255+
256+
static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
257+
{
258+
if ((filp->f_mode & FMODE_32BITHASH) ||
259+
(!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
260+
return (pos << 1) & 0xffffffff;
261+
else
262+
return ((pos >> 32) << 1) & 0xffffffff;
263+
}
264+
265+
static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
266+
{
267+
if ((filp->f_mode & FMODE_32BITHASH) ||
268+
(!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
269+
return 0;
270+
else
271+
return pos & 0xffffffff;
272+
}
273+
274+
/*
275+
* Return 32- or 64-bit end-of-file for dx directories
276+
*/
277+
static inline loff_t ext3_get_htree_eof(struct file *filp)
278+
{
279+
if ((filp->f_mode & FMODE_32BITHASH) ||
280+
(!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
281+
return EXT3_HTREE_EOF_32BIT;
282+
else
283+
return EXT3_HTREE_EOF_64BIT;
284+
}
285+
286+
287+
/*
288+
* ext3_dir_llseek() calls generic_file_llseek[_size]() to handle both
289+
* non-htree and htree directories, where the "offset" is in terms
290+
* of the filename hash value instead of the byte offset.
291+
*
292+
* Because we may return a 64-bit hash that is well beyond s_maxbytes,
293+
* we need to pass the max hash as the maximum allowable offset in
294+
* the htree directory case.
295+
*
296+
* NOTE: offsets obtained *before* ext3_set_inode_flag(dir, EXT3_INODE_INDEX)
297+
* will be invalid once the directory was converted into a dx directory
298+
*/
299+
loff_t ext3_dir_llseek(struct file *file, loff_t offset, int origin)
300+
{
301+
struct inode *inode = file->f_mapping->host;
302+
int dx_dir = is_dx_dir(inode);
303+
304+
if (likely(dx_dir))
305+
return generic_file_llseek_size(file, offset, origin,
306+
ext3_get_htree_eof(file));
307+
else
308+
return generic_file_llseek(file, offset, origin);
309+
}
246310

247311
/*
248312
* This structure holds the nodes of the red-black tree used to store
@@ -303,15 +367,16 @@ static void free_rb_tree_fname(struct rb_root *root)
303367
}
304368

305369

306-
static struct dir_private_info *ext3_htree_create_dir_info(loff_t pos)
370+
static struct dir_private_info *ext3_htree_create_dir_info(struct file *filp,
371+
loff_t pos)
307372
{
308373
struct dir_private_info *p;
309374

310375
p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
311376
if (!p)
312377
return NULL;
313-
p->curr_hash = pos2maj_hash(pos);
314-
p->curr_minor_hash = pos2min_hash(pos);
378+
p->curr_hash = pos2maj_hash(filp, pos);
379+
p->curr_minor_hash = pos2min_hash(filp, pos);
315380
return p;
316381
}
317382

@@ -401,7 +466,7 @@ static int call_filldir(struct file * filp, void * dirent,
401466
printk("call_filldir: called with null fname?!?\n");
402467
return 0;
403468
}
404-
curr_pos = hash2pos(fname->hash, fname->minor_hash);
469+
curr_pos = hash2pos(filp, fname->hash, fname->minor_hash);
405470
while (fname) {
406471
error = filldir(dirent, fname->name,
407472
fname->name_len, curr_pos,
@@ -426,22 +491,22 @@ static int ext3_dx_readdir(struct file * filp,
426491
int ret;
427492

428493
if (!info) {
429-
info = ext3_htree_create_dir_info(filp->f_pos);
494+
info = ext3_htree_create_dir_info(filp, filp->f_pos);
430495
if (!info)
431496
return -ENOMEM;
432497
filp->private_data = info;
433498
}
434499

435-
if (filp->f_pos == EXT3_HTREE_EOF)
500+
if (filp->f_pos == ext3_get_htree_eof(filp))
436501
return 0; /* EOF */
437502

438503
/* Some one has messed with f_pos; reset the world */
439504
if (info->last_pos != filp->f_pos) {
440505
free_rb_tree_fname(&info->root);
441506
info->curr_node = NULL;
442507
info->extra_fname = NULL;
443-
info->curr_hash = pos2maj_hash(filp->f_pos);
444-
info->curr_minor_hash = pos2min_hash(filp->f_pos);
508+
info->curr_hash = pos2maj_hash(filp, filp->f_pos);
509+
info->curr_minor_hash = pos2min_hash(filp, filp->f_pos);
445510
}
446511

447512
/*
@@ -473,7 +538,7 @@ static int ext3_dx_readdir(struct file * filp,
473538
if (ret < 0)
474539
return ret;
475540
if (ret == 0) {
476-
filp->f_pos = EXT3_HTREE_EOF;
541+
filp->f_pos = ext3_get_htree_eof(filp);
477542
break;
478543
}
479544
info->curr_node = rb_first(&info->root);
@@ -493,7 +558,7 @@ static int ext3_dx_readdir(struct file * filp,
493558
info->curr_minor_hash = fname->minor_hash;
494559
} else {
495560
if (info->next_hash == ~0) {
496-
filp->f_pos = EXT3_HTREE_EOF;
561+
filp->f_pos = ext3_get_htree_eof(filp);
497562
break;
498563
}
499564
info->curr_hash = info->next_hash;
@@ -512,3 +577,15 @@ static int ext3_release_dir (struct inode * inode, struct file * filp)
512577

513578
return 0;
514579
}
580+
581+
const struct file_operations ext3_dir_operations = {
582+
.llseek = ext3_dir_llseek,
583+
.read = generic_read_dir,
584+
.readdir = ext3_readdir,
585+
.unlocked_ioctl = ext3_ioctl,
586+
#ifdef CONFIG_COMPAT
587+
.compat_ioctl = ext3_compat_ioctl,
588+
#endif
589+
.fsync = ext3_sync_file,
590+
.release = ext3_release_dir,
591+
};

‎fs/ext3/ext3.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,11 @@ struct dx_hash_info
920920
u32 *seed;
921921
};
922922

923-
#define EXT3_HTREE_EOF 0x7fffffff
923+
924+
/* 32 and 64 bit signed EOF for dx directories */
925+
#define EXT3_HTREE_EOF_32BIT ((1UL << (32 - 1)) - 1)
926+
#define EXT3_HTREE_EOF_64BIT ((1ULL << (64 - 1)) - 1)
927+
924928

925929
/*
926930
* Control parameters used by ext3_htree_next_block

‎fs/ext3/hash.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
198198
return -1;
199199
}
200200
hash = hash & ~1;
201-
if (hash == (EXT3_HTREE_EOF << 1))
202-
hash = (EXT3_HTREE_EOF-1) << 1;
201+
if (hash == (EXT3_HTREE_EOF_32BIT << 1))
202+
hash = (EXT3_HTREE_EOF_32BIT - 1) << 1;
203203
hinfo->hash = hash;
204204
hinfo->minor_hash = minor_hash;
205205
return 0;

0 commit comments

Comments
 (0)