fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / ntfs / namei.c
index 70b503a..eddb224 100644 (file)
@@ -2,7 +2,7 @@
  * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
  *          project.
  *
- * Copyright (c) 2001-2004 Anton Altaparmakov
+ * Copyright (c) 2001-2006 Anton Altaparmakov
  *
  * This program/include file is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
 #include <linux/dcache.h>
 #include <linux/security.h>
 
-#include "ntfs.h"
+#include "attrib.h"
+#include "debug.h"
 #include "dir.h"
+#include "mft.h"
+#include "ntfs.h"
 
 /**
  * ntfs_lookup - find the inode represented by a dentry in a directory inode
@@ -93,7 +96,7 @@
  *    name. We then convert the name to the current NLS code page, and proceed
  *    searching for a dentry with this name, etc, as in case 2), above.
  *
- * Locking: Caller must hold i_sem on the directory.
+ * Locking: Caller must hold i_mutex on the directory.
  */
 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
                struct nameidata *nd)
@@ -112,7 +115,9 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
        uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
                        &uname);
        if (uname_len < 0) {
-               ntfs_error(vol->sb, "Failed to convert name to Unicode.");
+               if (uname_len != -ENAMETOOLONG)
+                       ntfs_error(vol->sb, "Failed to convert name to "
+                                       "Unicode.");
                return ERR_PTR(uname_len);
        }
        mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
@@ -124,15 +129,16 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
                dent_inode = ntfs_iget(vol->sb, dent_ino);
                if (likely(!IS_ERR(dent_inode))) {
                        /* Consistency check. */
-                       if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no ||
+                       if (is_bad_inode(dent_inode) || MSEQNO(mref) ==
+                                       NTFS_I(dent_inode)->seq_no ||
                                        dent_ino == FILE_MFT) {
                                /* Perfect WIN32/POSIX match. -- Case 1. */
                                if (!name) {
-                                       ntfs_debug("Done.");
+                                       ntfs_debug("Done.  (Case 1.)");
                                        return d_splice_alias(dent_inode, dent);
                                }
                                /*
-                                * We are too indented. Handle imperfect
+                                * We are too indented.  Handle imperfect
                                 * matches and short file names further below.
                                 */
                                goto handle_name;
@@ -149,12 +155,11 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
                        ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
                                        "error code %li.", dent_ino,
                                        PTR_ERR(dent_inode));
-               if (name)
-                       kfree(name);
+               kfree(name);
                /* Return the error code. */
                return (struct dentry *)dent_inode;
        }
-       /* It is guaranteed that name is no longer allocated at this point. */
+       /* It is guaranteed that @name is no longer allocated at this point. */
        if (MREF_ERR(mref) == -ENOENT) {
                ntfs_debug("Entry was not found, adding negative dentry.");
                /* The dcache will handle negative entries. */
@@ -165,7 +170,6 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
        ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
                        "code %i.", -MREF_ERR(mref));
        return ERR_PTR(MREF_ERR(mref));
-
        // TODO: Consider moving this lot to a separate function! (AIA)
 handle_name:
    {
@@ -178,6 +182,7 @@ handle_name:
 
        nls_name.name = NULL;
        if (name->type != FILE_NAME_DOS) {                      /* Case 2. */
+               ntfs_debug("Case 2.");
                nls_name.len = (unsigned)ntfs_ucstonls(vol,
                                (ntfschar*)&name->name, name->len,
                                (unsigned char**)&nls_name.name, 0);
@@ -185,6 +190,7 @@ handle_name:
        } else /* if (name->type == FILE_NAME_DOS) */ {         /* Case 3. */
                FILE_NAME_ATTR *fn;
 
+               ntfs_debug("Case 3.");
                kfree(name);
 
                /* Find the WIN32 name corresponding to the matched DOS name. */
@@ -249,7 +255,7 @@ handle_name:
        nls_name.hash = full_name_hash(nls_name.name, nls_name.len);
 
        /*
-        * Note: No need for dent->d_lock lock as i_sem is held on the
+        * Note: No need for dent->d_lock lock as i_mutex is held on the
         * parent inode.
         */
 
@@ -268,12 +274,17 @@ handle_name:
                        dput(real_dent);
                else
                        new_dent = real_dent;
+               ntfs_debug("Done.  (Created new dentry.)");
                return new_dent;
        }
        kfree(nls_name.name);
        /* Matching dentry exists, check if it is negative. */
        if (real_dent->d_inode) {
-               BUG_ON(real_dent->d_inode != dent_inode);
+               if (unlikely(real_dent->d_inode != dent_inode)) {
+                       /* This can happen because bad inodes are unhashed. */
+                       BUG_ON(!is_bad_inode(dent_inode));
+                       BUG_ON(!is_bad_inode(real_dent->d_inode));
+               }
                /*
                 * Already have the inode and the dentry attached, decrement
                 * the reference count to balance the ntfs_iget() we did
@@ -282,6 +293,7 @@ handle_name:
                 * about any NFS/disconnectedness issues here.
                 */
                iput(dent_inode);
+               ntfs_debug("Done.  (Already had inode and dentry.)");
                return real_dent;
        }
        /*
@@ -292,6 +304,7 @@ handle_name:
        if (!S_ISDIR(dent_inode->i_mode)) {
                /* Not a directory; everything is easy. */
                d_instantiate(real_dent, dent_inode);
+               ntfs_debug("Done.  (Already had negative file dentry.)");
                return real_dent;
        }
        spin_lock(&dcache_lock);
@@ -305,6 +318,7 @@ handle_name:
                real_dent->d_inode = dent_inode;
                spin_unlock(&dcache_lock);
                security_d_instantiate(real_dent, dent_inode);
+               ntfs_debug("Done.  (Already had negative directory dentry.)");
                return real_dent;
        }
        /*
@@ -324,6 +338,8 @@ handle_name:
        /* Throw away real_dent. */
        dput(real_dent);
        /* Use new_dent as the actual dentry. */
+       ntfs_debug("Done.  (Already had negative, disconnected directory "
+                       "dentry.)");
        return new_dent;
 
 eio_err_out:
@@ -335,6 +351,7 @@ err_out:
        if (m)
                unmap_mft_record(ni);
        iput(dent_inode);
+       ntfs_error(vol->sb, "Failed, returning error code %i.", err);
        return ERR_PTR(err);
    }
 }
@@ -358,12 +375,12 @@ struct inode_operations ntfs_dir_inode_ops = {
  * The code is based on the ext3 ->get_parent() implementation found in
  * fs/ext3/namei.c::ext3_get_parent().
  *
- * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_sem down.
+ * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_mutex down.
  *
  * Return the dentry of the parent directory on success or the error code on
  * error (IS_ERR() is true).
  */
-struct dentry *ntfs_get_parent(struct dentry *child_dent)
+static struct dentry *ntfs_get_parent(struct dentry *child_dent)
 {
        struct inode *vi = child_dent->d_inode;
        ntfs_inode *ni = NTFS_I(vi);
@@ -448,7 +465,7 @@ try_next:
  *
  * Return the dentry on success or the error code on error (IS_ERR() is true).
  */
-struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
+static struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
 {
        struct inode *vi;
        struct dentry *dent;
@@ -479,3 +496,30 @@ struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
        ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen);
        return dent;
 }
+
+/**
+ * Export operations allowing NFS exporting of mounted NTFS partitions.
+ *
+ * We use the default ->decode_fh() and ->encode_fh() for now.  Note that they
+ * use 32 bits to store the inode number which is an unsigned long so on 64-bit
+ * architectures is usually 64 bits so it would all fail horribly on huge
+ * volumes.  I guess we need to define our own encode and decode fh functions
+ * that store 64-bit inode numbers at some point but for now we will ignore the
+ * problem...
+ *
+ * We also use the default ->get_name() helper (used by ->decode_fh() via
+ * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
+ * independent.
+ *
+ * The default ->get_parent() just returns -EACCES so we have to provide our
+ * own and the default ->get_dentry() is incompatible with NTFS due to not
+ * allowing the inode number 0 which is used in NTFS for the system file $MFT
+ * and due to using iget() whereas NTFS needs ntfs_iget().
+ */
+struct export_operations ntfs_export_ops = {
+       .get_parent     = ntfs_get_parent,      /* Find the parent of a given
+                                                  directory. */
+       .get_dentry     = ntfs_get_dentry,      /* Find a dentry for the inode
+                                                  given a file handle
+                                                  sub-fragment. */
+};