From: Jan Kratochvil Subject: [PATCH] Case-preserving & case-insensitive filesystems, kernel 2.4.19-pre4+2.5.8 Date: Mon, 22 Apr 2002 02:32:53 +0200 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <20020422003253.GA13717@short.ucw.cz> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: (from root@localhost) by short.ucw.cz (8.9.3/8.9.3) id CAA13741 for linux-fsdevel@vger.kernel.org; Mon, 22 Apr 2002 02:32:53 +0200 To: linux-fsdevel@vger.kernel.org Content-Disposition: inline List-Id: linux-fsdevel.vger.kernel.org Hi, # Problem: /$ mount -t vfat /dev/hdc2 /hdc2 /$ cd /hdc2 /hdc2$ cat LONGFILENAME_ABCD cat: LONGFILENAME_ABCD: No such file or directory /hdc2$ touch LongFileName_aBcD /hdc2$ ls -l -rwxr-xr-x 1 root root 0 Jan 9 00:05 LONGFILENAME_ABCD /hdc2$ # Samba (and other?) software expects "LongFileName_aBcD" here My patches are provided on: http://short.vellum.cz/priv/vfat/ http://short.vellum.cz/priv/vfat/linux-2.4.19-pre4-vfat5.diff Linus kernel 2.4.19-pre4, tested for almost a month [included] http://short.vellum.cz/priv/vfat/linux-2.5.8-vfat5.diff Linus kernel 2.5.8, only simple port and tested once http://short.vellum.cz/priv/vfat/vfat_test_suite_tcl-2.4-fix1.diff update of: http://bmrc.berkeley.edu/people/chaffee/vfat_test_suite_tcl to get it working for testing of recent kernels Already solved in the past by Alexander Viro (1999-05-26) ("Vogon filesystems"): http://marc.theaimsgroup.com/?l=linux-fsdevel&m=92792807215416&w=2 obsoletes his previous patches (more or less, I hope): http://marc.theaimsgroup.com/?l=linux-fsdevel&m=92478709702476&w=2 http://marc.theaimsgroup.com/?l=linux-fsdevel&m=91894002829117&w=2 http://marc.theaimsgroup.com/?l=linux-kernel&m=91636997404967&w=2 Bug is present at least in: Alan Cox: 2.4.18-pre7-ac1, 2.4.19-pre4 Linus: 2.5.3, 2.5.8 David Jones: patch-2.5.2-dj6 (only read the diff, no filename-specifics) RedHat: kernel-2.4.9-21 Mandrake: kernel-2.4.8-34.1mdk Thanks to: developed in cooperation with Jan Kara bugreported and bravely tested by Jan Kolar Problem introduced in 2.1.x (dcache implementation) and it hits Samba servers trying to provide its share from vfat partition: When W32 client wants to create "long filename", it will first check for its existence (in upper-cased form) - it will fail but vfat (dentries) will cache the long filename. The problem is that the forthcoming file create (in different case) will create the file in its original upper-cased form! This results in all filenames always uppercased when created from W9x clients on (at least letter-connected) disk from Samba share running on Linux vfat. This is a problem while pushing Linux to business where managers want to keep their data (disks) w/o dependence on the new technology Linux (=to use vfat). The key point of the patch is the condition added to top of vfs_rename(): + if (old_dentry->d_inode == new_dentry->d_inode + && (!old_dentry->d_op->d_compare_rename || (old_dir != new_dir + || old_dentry->d_op->d_compare_rename(old_dentry, &old_dentry->d_name, &new_dentry->d_name)))) + return 0; Corresponds to the following sequence of conditions: inode != DO old-style filesystem return 0 (hard-linked) dir != return 0 (hard-linked across directories) anycase name compare NOT MATCH return 0 (hard-linked in the same directory) DO RENAME I solved it by 'virtual hardlinking' the file to all its name case-aliases. This should be safe for files but it may be a problem for directories - kernel currently doesn't expect directories to have hardlinks but for now I don't know about any specific problems of it. Currently it is protected by mandatory revalidation (vfat_revalidate()) of all dentries: +/* We have to always do the revalidate as after unlink (etc.) there still may + * exist other case-different dentries for the same inode. It would be also + * possible to discard such aliases by going through d_alias links during the + * unlink. "strictcase" doesn't have case-different dentries but "longna~1" + * style aliases still exist there. + */ It was patched with target to vfat (to be able to provide Samba shares from vfat volumes) but I also implemented it to affs as proof-of-concept for case-preserving & case-insensitive filesystems which are capable of hardlinks (as vfat cannot have valid hardlinks, of course). It is not optimal solution but a bunch of problems ceases to be. Anyway even the original vfat implementation uses dentries revalidation a lot, my patch just increases their amount. This revalidations can cost a lot during entries modified in big directories - it would be nice to do another optimizing-only patch to remove any revalidation by d_alias links utilization during dentry delete. I have such in-progress patch ready, it hooks on d_delete() and d_move() but I dropped it now to minimize the affected code and introduced bugs. All the sane (or case-preserving & case-insensitive but not yet patched) filesystems don't have dentry_operations->d_compare_rename() implemented and thus the patch doesn't affect them in any way. Patched filesystems MUST have d_hash() and d_compare() case sensitive but case-insensitive comparison must be implemented by d_compare_rename(). The filesystems usually have to be fixed for proper renaming between two case-insensitively matching entries in the same directory. I am not sure how to hehave during rename("LongFileName","longfi~1"). I consider these two names as hardlinks and thus "0" is returned and no action is done. Jan "Lace" Kratochvil, patch developed in cooperation with Jan Kara Inlined patch linux-2.4.19-pre4-vfat5.diff: (for other patches see URLs above) ------------------------------------------- diff -u -ru linux-2.4.19-pre4-clean/Documentation/filesystems/vfs.txt linux-2.4.19-pre4-vfat/Documentation/filesystems/vfs.txt --- linux-2.4.19-pre4-clean/Documentation/filesystems/vfs.txt Fri Aug 11 18:47:08 2000 +++ linux-2.4.19-pre4-vfat/Documentation/filesystems/vfs.txt Wed Mar 27 15:36:07 2002 @@ -405,6 +405,7 @@ int (*d_revalidate)(struct dentry *); int (*d_hash) (struct dentry *, struct qstr *); int (*d_compare) (struct dentry *, struct qstr *, struct qstr *); + int (*d_compare_rename) (struct dentry *, struct qstr *, struct qstr *); void (*d_delete)(struct dentry *); void (*d_release)(struct dentry *); void (*d_iput)(struct dentry *, struct inode *); @@ -417,7 +418,13 @@ d_hash: called when the VFS adds a dentry to the hash table - d_compare: called when a dentry should be compared with another + d_compare: called when a dentry should be compared with another. + Case-sensitive for all case-preserving filesystems, no matter if the + filesystem structure is case-sensitive itself + + d_compare_rename: compare equivalency of two dentries, no matter their case. + Optional - implement only for case-insensitive but case-preserving + filesystems, dentries always compared by d_compare when not provided d_delete: called when the last reference to a dentry is deleted. This means no-one is using the dentry, however it is diff -u -ru linux-2.4.19-pre4-clean/fs/affs/namei.c linux-2.4.19-pre4-vfat/fs/affs/namei.c --- linux-2.4.19-pre4-clean/fs/affs/namei.c Wed Oct 17 21:09:01 2001 +++ linux-2.4.19-pre4-vfat/fs/affs/namei.c Sat Mar 23 18:59:16 2002 @@ -24,24 +24,57 @@ extern struct inode_operations affs_symlink_inode_operations; -static int affs_toupper(int ch); -static int affs_hash_dentry(struct dentry *, struct qstr *); -static int affs_compare_dentry(struct dentry *, struct qstr *, struct qstr *); -static int affs_intl_toupper(int ch); -static int affs_intl_hash_dentry(struct dentry *, struct qstr *); -static int affs_intl_compare_dentry(struct dentry *, struct qstr *, struct qstr *); +static int affs_revalidate(struct dentry *dentry, int flags); +static int affs_strict_toupper(int ch); +static int affs_toupper(int ch); +static int affs_intl_toupper(int ch); +static int affs_hash_strictcase_dentry(struct dentry *, struct qstr *); +static int affs_compare_strictcase_dentry(struct dentry *, struct qstr *, struct qstr *); +static int affs_compare_anycase_dentry(struct dentry *, struct qstr *, struct qstr *); +static int affs_intl_compare_anycase_dentry(struct dentry *, struct qstr *, struct qstr *); + +/* We have to always do the revalidate as after unlink (etc.) there still may + * exist other case-different dentries for the same inode. It would be also + * possible to discard such aliases by going through d_alias links during the + * unlink. + */ struct dentry_operations affs_dentry_operations = { - d_hash: affs_hash_dentry, - d_compare: affs_compare_dentry, + d_revalidate: affs_revalidate, + d_hash: affs_hash_strictcase_dentry, + d_compare: affs_compare_strictcase_dentry, + d_compare_rename: affs_compare_anycase_dentry, }; struct dentry_operations affs_intl_dentry_operations = { - d_hash: affs_intl_hash_dentry, - d_compare: affs_intl_compare_dentry, + d_revalidate: affs_revalidate, + d_hash: affs_hash_strictcase_dentry, + d_compare: affs_compare_strictcase_dentry, + d_compare_rename: affs_intl_compare_anycase_dentry, }; +static int affs_revalidate(struct dentry *dentry, int flags) +{ + pr_debug("AFFS: revalidate(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name); + spin_lock(&dcache_lock); + if (dentry->d_parent == dentry /* root is always valid */ + || dentry->d_time == dentry->d_parent->d_inode->i_version) { + spin_unlock(&dcache_lock); + return 1; + } + spin_unlock(&dcache_lock); + return 0; +} + +/* toupper() for case-sensitive dentries matching */ + +static int +affs_strict_toupper(int ch) +{ + return ch; +} + /* Simple toupper() for DOS\1 */ static int @@ -90,14 +123,9 @@ } static int -affs_hash_dentry(struct dentry *dentry, struct qstr *qstr) +affs_hash_strictcase_dentry(struct dentry *dentry, struct qstr *qstr) { - return __affs_hash_dentry(dentry, qstr, affs_toupper); -} -static int -affs_intl_hash_dentry(struct dentry *dentry, struct qstr *qstr) -{ - return __affs_hash_dentry(dentry, qstr, affs_intl_toupper); + return __affs_hash_dentry(dentry, qstr, affs_strict_toupper); } static inline int @@ -133,12 +161,17 @@ } static int -affs_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b) +affs_compare_strictcase_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b) +{ + return __affs_compare_dentry(dentry, a, b, affs_strict_toupper); +} +static int +affs_compare_anycase_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b) { return __affs_compare_dentry(dentry, a, b, affs_toupper); } static int -affs_intl_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b) +affs_intl_compare_anycase_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b) { return __affs_compare_dentry(dentry, a, b, affs_intl_toupper); } @@ -239,6 +272,7 @@ return ERR_PTR(-EACCES); } dentry->d_op = AFFS_SB->s_flags & SF_INTL ? &affs_intl_dentry_operations : &affs_dentry_operations; + dentry->d_time = dentry->d_parent->d_inode->i_version; d_add(dentry, inode); return NULL; } @@ -282,6 +316,7 @@ iput(inode); return error; } + dentry->d_time = dentry->d_parent->d_inode->i_version; return 0; } @@ -311,6 +346,7 @@ iput(inode); return error; } + dentry->d_time = dentry->d_parent->d_inode->i_version; return 0; } @@ -437,7 +473,12 @@ goto done; /* Unlink destination if it already exists */ - if (new_dentry->d_inode) { + if (new_dentry->d_inode + /* Don't remove case-different aliases twice. + * Hardlinks cannot be passed here - checked at top of vfs_rename(). + */ + && old_dentry->d_inode != new_dentry->d_inode + ) { retval = affs_remove_header(new_dentry); if (retval) return retval; diff -u -ru linux-2.4.19-pre4-clean/fs/namei.c linux-2.4.19-pre4-vfat/fs/namei.c --- linux-2.4.19-pre4-clean/fs/namei.c Sat Mar 23 17:04:13 2002 +++ linux-2.4.19-pre4-vfat/fs/namei.c Sat Mar 23 18:19:28 2002 @@ -1714,9 +1714,6 @@ int error; struct inode *target; - if (old_dentry->d_inode == new_dentry->d_inode) - return 0; - error = may_delete(old_dir, old_dentry, 1); if (error) return error; @@ -1794,9 +1791,6 @@ { int error; - if (old_dentry->d_inode == new_dentry->d_inode) - return 0; - error = may_delete(old_dir, old_dentry, 0); if (error) return error; @@ -1835,6 +1829,12 @@ struct inode *new_dir, struct dentry *new_dentry) { int error; + + if (old_dentry->d_inode == new_dentry->d_inode + && (!old_dentry->d_op->d_compare_rename || (old_dir != new_dir + || old_dentry->d_op->d_compare_rename(old_dentry, &old_dentry->d_name, &new_dentry->d_name)))) + return 0; + if (S_ISDIR(old_dentry->d_inode->i_mode)) error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); else diff -u -ru linux-2.4.19-pre4-clean/fs/vfat/namei.c linux-2.4.19-pre4-vfat/fs/vfat/namei.c --- linux-2.4.19-pre4-clean/fs/vfat/namei.c Sat Mar 23 17:04:14 2002 +++ linux-2.4.19-pre4-vfat/fs/vfat/namei.c Sat Mar 23 19:05:35 2002 @@ -45,38 +45,35 @@ # define PRINTK3(x) #endif -static int vfat_hashi(struct dentry *parent, struct qstr *qstr); static int vfat_hash(struct dentry *parent, struct qstr *qstr); static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b); static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b); static int vfat_revalidate(struct dentry *dentry, int); -static struct dentry_operations vfat_dentry_ops[4] = { - { - d_hash: vfat_hashi, - d_compare: vfat_cmpi, - }, - { - d_revalidate: vfat_revalidate, - d_hash: vfat_hashi, - d_compare: vfat_cmpi, - }, - { - d_hash: vfat_hash, - d_compare: vfat_cmp, - }, - { - d_revalidate: vfat_revalidate, - d_hash: vfat_hash, - d_compare: vfat_cmp, - } +/* We have to always do the revalidate as after unlink (etc.) there still may + * exist other case-different dentries for the same inode. It would be also + * possible to discard such aliases by going through d_alias links during the + * unlink. "strictcase" doesn't have case-different dentries but "longna~1" + * style aliases still exist there. + */ +static struct dentry_operations vfat_dentry_ops_anycase = { + d_revalidate: vfat_revalidate, + d_hash: vfat_hash, + d_compare: vfat_cmp, + d_compare_rename: vfat_cmpi, +}; +static struct dentry_operations vfat_dentry_ops_strictcase = { + d_revalidate: vfat_revalidate, + d_hash: vfat_hash, + d_compare: vfat_cmp, }; static int vfat_revalidate(struct dentry *dentry, int flags) { PRINTK1(("vfat_revalidate: %s\n", dentry->d_name.name)); spin_lock(&dcache_lock); - if (dentry->d_time == dentry->d_parent->d_inode->i_version) { + if (dentry->d_parent == dentry /* root is always valid */ + || dentry->d_time == dentry->d_parent->d_inode->i_version) { spin_unlock(&dcache_lock); return 1; } @@ -220,32 +217,6 @@ } /* - * Compute the hash for the vfat name corresponding to the dentry. - * Note: if the name is invalid, we leave the hash code unchanged so - * that the existing dentry can be used. The vfat fs routines will - * return ENOENT or EINVAL as appropriate. - */ -static int vfat_hashi(struct dentry *dentry, struct qstr *qstr) -{ - struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io; - const char *name; - int len; - unsigned long hash; - - len = qstr->len; - name = qstr->name; - while (len && name[len-1] == '.') - len--; - - hash = init_name_hash(); - while (len--) - hash = partial_name_hash(vfat_tolower(t, *name++), hash); - qstr->hash = end_name_hash(hash); - - return 0; -} - -/* * Case insensitive compare of two vfat names. */ static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b) @@ -984,39 +955,24 @@ int res; struct vfat_slot_info sinfo; struct inode *inode; - struct dentry *alias; struct buffer_head *bh = NULL; struct msdos_dir_entry *de; - int table; PRINTK2(("vfat_lookup: name=%s, len=%d\n", dentry->d_name.name, dentry->d_name.len)); - table = (MSDOS_SB(dir->i_sb)->options.name_check == 's') ? 2 : 0; - dentry->d_op = &vfat_dentry_ops[table]; + dentry->d_op = (MSDOS_SB(dir->i_sb)->options.name_check == 's' + ? &vfat_dentry_ops_strictcase : &vfat_dentry_ops_anycase); inode = NULL; res = vfat_find(dir,&dentry->d_name,&sinfo,&bh,&de); - if (res < 0) { - table++; + if (res < 0) goto error; - } inode = fat_build_inode(dir->i_sb, de, sinfo.ino, &res); fat_brelse(dir->i_sb, bh); if (res) return ERR_PTR(res); - alias = d_find_alias(inode); - if (alias) { - if (d_invalidate(alias)==0) - dput(alias); - else { - iput(inode); - return alias; - } - - } error: - dentry->d_op = &vfat_dentry_ops[table]; dentry->d_time = dentry->d_parent->d_inode->i_version; d_add(dentry,inode); return NULL; @@ -1173,7 +1129,7 @@ int dotdot_ino; struct inode *old_inode, *new_inode; int res, is_dir; - struct vfat_slot_info old_sinfo,sinfo; + struct vfat_slot_info old_sinfo,new_sinfo; old_bh = new_bh = dotdot_bh = NULL; old_inode = old_dentry->d_inode; @@ -1188,10 +1144,15 @@ &dotdot_de,&dotdot_ino)) < 0) goto rename_done; - if (new_dentry->d_inode) { - res = vfat_find(new_dir,&new_dentry->d_name,&sinfo,&new_bh, + if (new_dentry->d_inode + /* Don't remove case-different aliases twice. + * Hardlinks cannot be passed here - checked at top of vfs_rename(). + */ + && old_dentry->d_inode != new_dentry->d_inode + ) { + res = vfat_find(new_dir,&new_dentry->d_name,&new_sinfo,&new_bh, &new_de); - if (res < 0 || MSDOS_I(new_inode)->i_location != sinfo.ino) { + if (res < 0 || MSDOS_I(new_inode)->i_location != new_sinfo.ino) { /* WTF??? Cry and fail. */ printk(KERN_WARNING "vfat_rename: fs corrupted\n"); goto rename_done; @@ -1202,11 +1163,10 @@ if (res) goto rename_done; } + /* releases new_bh */ + vfat_remove_entry(new_dir,&new_sinfo,new_bh,new_de); + new_bh=NULL; fat_detach(new_inode); - } else { - res = vfat_add_entry(new_dir,&new_dentry->d_name,is_dir,&sinfo, - &new_bh,&new_de); - if (res < 0) goto rename_done; } new_dir->i_version = ++event; @@ -1214,8 +1174,12 @@ /* releases old_bh */ vfat_remove_entry(old_dir,&old_sinfo,old_bh,old_de); old_bh=NULL; + res = vfat_add_entry(new_dir,&new_dentry->d_name,is_dir,&new_sinfo, + &new_bh,&new_de); + if (res < 0) goto rename_done; + fat_detach(old_inode); - fat_attach(old_inode, sinfo.ino); + fat_attach(old_inode, new_sinfo.ino); mark_inode_dirty(old_inode); old_dir->i_version = ++event; @@ -1276,11 +1240,8 @@ if (MSDOS_SB(sb)->options.posixfs) { MSDOS_SB(sb)->options.name_check = 's'; } - if (MSDOS_SB(sb)->options.name_check != 's') { - sb->s_root->d_op = &vfat_dentry_ops[0]; - } else { - sb->s_root->d_op = &vfat_dentry_ops[2]; - } + sb->s_root->d_op = (MSDOS_SB(sb)->options.name_check == 's' + ? &vfat_dentry_ops_strictcase : &vfat_dentry_ops_anycase); } return res; diff -u -ru linux-2.4.19-pre4-clean/include/linux/dcache.h linux-2.4.19-pre4-vfat/include/linux/dcache.h --- linux-2.4.19-pre4-clean/include/linux/dcache.h Wed Oct 17 21:09:41 2001 +++ linux-2.4.19-pre4-vfat/include/linux/dcache.h Sat Mar 23 17:15:14 2002 @@ -87,6 +87,7 @@ int (*d_revalidate)(struct dentry *, int); int (*d_hash) (struct dentry *, struct qstr *); int (*d_compare) (struct dentry *, struct qstr *, struct qstr *); + int (*d_compare_rename) (struct dentry *, struct qstr *, struct qstr *); int (*d_delete)(struct dentry *); void (*d_release)(struct dentry *); void (*d_iput)(struct dentry *, struct inode *);