Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[safe/jmp/linux-2.6] / fs / fat / namei_vfat.c
index 419deab..6fcc7e7 100644 (file)
 #include <linux/jiffies.h>
 #include <linux/ctype.h>
 #include <linux/slab.h>
-#include <linux/smp_lock.h>
 #include <linux/buffer_head.h>
 #include <linux/namei.h>
 #include "fat.h"
 
-static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd)
+/*
+ * If new entry was created in the parent, it could create the 8.3
+ * alias (the shortname of logname).  So, the parent may have the
+ * negative-dentry which matches the created 8.3 alias.
+ *
+ * If it happened, the negative dentry isn't actually negative
+ * anymore.  So, drop it.
+ */
+static int vfat_revalidate_shortname(struct dentry *dentry)
 {
        int ret = 1;
-
-       if (!dentry->d_inode &&
-           nd && !(nd->flags & LOOKUP_CONTINUE) && (nd->flags & LOOKUP_CREATE))
-               /*
-                * negative dentry is dropped, in order to make sure
-                * to use the name which a user desires if this is
-                * create path.
-                */
+       spin_lock(&dentry->d_lock);
+       if (dentry->d_time != dentry->d_parent->d_inode->i_version)
                ret = 0;
-       else {
-               spin_lock(&dentry->d_lock);
-               if (dentry->d_time != dentry->d_parent->d_inode->i_version)
-                       ret = 0;
-               spin_unlock(&dentry->d_lock);
-       }
+       spin_unlock(&dentry->d_lock);
        return ret;
 }
 
+static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd)
+{
+       /* This is not negative dentry. Always valid. */
+       if (dentry->d_inode)
+               return 1;
+       return vfat_revalidate_shortname(dentry);
+}
+
+static int vfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
+{
+       /*
+        * This is not negative dentry. Always valid.
+        *
+        * Note, rename() to existing directory entry will have ->d_inode,
+        * and will use existing name which isn't specified name by user.
+        *
+        * We may be able to drop this positive dentry here. But dropping
+        * positive dentry isn't good idea. So it's unsupported like
+        * rename("filename", "FILENAME") for now.
+        */
+       if (dentry->d_inode)
+               return 1;
+
+       /*
+        * This may be nfsd (or something), anyway, we can't see the
+        * intent of this. So, since this can be for creation, drop it.
+        */
+       if (!nd)
+               return 0;
+
+       /*
+        * Drop the negative dentry, in order to make sure to use the
+        * case sensitive name which is specified by user if this is
+        * for creation.
+        */
+       if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
+               if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
+                       return 0;
+       }
+
+       return vfat_revalidate_shortname(dentry);
+}
+
 /* returns the length of a struct qstr, ignoring trailing dots */
 static unsigned int vfat_striptail_len(struct qstr *qstr)
 {
@@ -126,25 +165,16 @@ static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b)
        return 1;
 }
 
-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,
-       }
+static const struct dentry_operations vfat_ci_dentry_ops = {
+       .d_revalidate   = vfat_revalidate_ci,
+       .d_hash         = vfat_hashi,
+       .d_compare      = vfat_cmpi,
+};
+
+static const struct dentry_operations vfat_dentry_ops = {
+       .d_revalidate   = vfat_revalidate,
+       .d_hash         = vfat_hash,
+       .d_compare      = vfat_cmp,
 };
 
 /* Characters that are undesirable in an MS-DOS file name */
@@ -279,7 +309,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 {
        struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
        wchar_t *ip, *ext_start, *end, *name_start;
-       unsigned char base[9], ext[4], buf[8], *p;
+       unsigned char base[9], ext[4], buf[5], *p;
        unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
        int chl, chi;
        int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
@@ -437,7 +467,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
                        return 0;
        }
 
-       i = jiffies & 0xffff;
+       i = jiffies;
        sz = (jiffies >> 16) & 0x7;
        if (baselen > 2) {
                baselen = numtail2_baselen;
@@ -446,7 +476,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
        name_res[baselen + 4] = '~';
        name_res[baselen + 5] = '1' + sz;
        while (1) {
-               sprintf(buf, "%04X", i);
+               snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
                memcpy(&name_res[baselen], buf, 4);
                if (vfat_find_form(dir, name_res) < 0)
                        break;
@@ -469,24 +499,17 @@ xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
        int charlen;
 
        if (utf8) {
-               int name_len = strlen(name);
-
-               *outlen = utf8_mbstowcs((wchar_t *)outname, name, PATH_MAX);
-
-               /*
-                * We stripped '.'s before and set len appropriately,
-                * but utf8_mbstowcs doesn't care about len
-                */
-               *outlen -= (name_len - len);
-
-               if (*outlen > 255)
+               *outlen = utf8s_to_utf16s(name, len, (wchar_t *)outname);
+               if (*outlen < 0)
+                       return *outlen;
+               else if (*outlen > FAT_LFN_LEN)
                        return -ENAMETOOLONG;
 
                op = &outname[*outlen * sizeof(wchar_t)];
        } else {
                if (nls) {
                        for (i = 0, ip = name, op = outname, *outlen = 0;
-                            i < len && *outlen <= 255;
+                            i < len && *outlen <= FAT_LFN_LEN;
                             *outlen += 1)
                        {
                                if (escape && (*ip == ':')) {
@@ -526,7 +549,7 @@ xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
                                return -ENAMETOOLONG;
                } else {
                        for (i = 0, ip = name, op = outname, *outlen = 0;
-                            i < len && *outlen <= 255;
+                            i < len && *outlen <= FAT_LFN_LEN;
                             i++, *outlen += 1)
                        {
                                *op++ = *ip++;
@@ -678,6 +701,15 @@ static int vfat_find(struct inode *dir, struct qstr *qname,
        return fat_search_long(dir, qname->name, len, sinfo);
 }
 
+/*
+ * (nfsd's) anonymous disconnected dentry?
+ * NOTE: !IS_ROOT() is not anonymous (I.e. d_splice_alias() did the job).
+ */
+static int vfat_d_anon_disconn(struct dentry *dentry)
+{
+       return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
+}
+
 static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
                                  struct nameidata *nd)
 {
@@ -685,45 +717,51 @@ static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
        struct fat_slot_info sinfo;
        struct inode *inode;
        struct dentry *alias;
-       int err, table;
+       int err;
 
        lock_super(sb);
-       table = (MSDOS_SB(sb)->options.name_check == 's') ? 2 : 0;
-       dentry->d_op = &vfat_dentry_ops[table];
 
        err = vfat_find(dir, &dentry->d_name, &sinfo);
        if (err) {
                if (err == -ENOENT) {
-                       table++;
                        inode = NULL;
                        goto out;
                }
                goto error;
        }
+
        inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
        brelse(sinfo.bh);
        if (IS_ERR(inode)) {
                err = PTR_ERR(inode);
                goto error;
        }
+
        alias = d_find_alias(inode);
-       if (alias) {
-               if (d_invalidate(alias) == 0)
-                       dput(alias);
-               else {
-                       iput(inode);
-                       unlock_super(sb);
-                       return alias;
-               }
+       if (alias && !vfat_d_anon_disconn(alias)) {
+               /*
+                * This inode has non anonymous-DCACHE_DISCONNECTED
+                * dentry. This means, the user did ->lookup() by an
+                * another name (longname vs 8.3 alias of it) in past.
+                *
+                * Switch to new one for reason of locality if possible.
+                */
+               BUG_ON(d_unhashed(alias));
+               if (!S_ISDIR(inode->i_mode))
+                       d_move(alias, dentry);
+               iput(inode);
+               unlock_super(sb);
+               return alias;
+       } else
+               dput(alias);
 
-       }
 out:
        unlock_super(sb);
-       dentry->d_op = &vfat_dentry_ops[table];
+       dentry->d_op = sb->s_root->d_op;
        dentry->d_time = dentry->d_parent->d_inode->i_version;
        dentry = d_splice_alias(inode, dentry);
        if (dentry) {
-               dentry->d_op = &vfat_dentry_ops[table];
+               dentry->d_op = sb->s_root->d_op;
                dentry->d_time = dentry->d_parent->d_inode->i_version;
        }
        return dentry;
@@ -930,7 +968,7 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
                int start = MSDOS_I(new_dir)->i_logstart;
                dotdot_de->start = cpu_to_le16(start);
                dotdot_de->starthi = cpu_to_le16(start >> 16);
-               mark_buffer_dirty(dotdot_bh);
+               mark_buffer_dirty_inode(dotdot_bh, old_inode);
                if (IS_DIRSYNC(new_dir)) {
                        err = sync_dirty_buffer(dotdot_bh);
                        if (err)
@@ -974,7 +1012,7 @@ error_dotdot:
                int start = MSDOS_I(old_dir)->i_logstart;
                dotdot_de->start = cpu_to_le16(start);
                dotdot_de->starthi = cpu_to_le16(start >> 16);
-               mark_buffer_dirty(dotdot_bh);
+               mark_buffer_dirty_inode(dotdot_bh, old_inode);
                corrupt |= sync_dirty_buffer(dotdot_bh);
        }
 error_inode:
@@ -995,7 +1033,7 @@ error_inode:
                sinfo.bh = NULL;
        }
        if (corrupt < 0) {
-               fat_fs_panic(new_dir->i_sb,
+               fat_fs_error(new_dir->i_sb,
                             "%s: Filesystem corrupted (i_pos %lld)",
                             __func__, sinfo.i_pos);
        }
@@ -1022,9 +1060,9 @@ static int vfat_fill_super(struct super_block *sb, void *data, int silent)
                return res;
 
        if (MSDOS_SB(sb)->options.name_check != 's')
-               sb->s_root->d_op = &vfat_dentry_ops[0];
+               sb->s_root->d_op = &vfat_ci_dentry_ops;
        else
-               sb->s_root->d_op = &vfat_dentry_ops[2];
+               sb->s_root->d_op = &vfat_dentry_ops;
 
        return 0;
 }