vserver 1.9.5.x5
[linux-2.6.git] / fs / ntfs / namei.c
index bc251eb..7c7e13b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
- *          project.
+ *          project.
  *
  * Copyright (c) 2001-2004 Anton Altaparmakov
  *
@@ -9,13 +9,13 @@
  * by the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * This program/include file is distributed in the hope that it will be 
- * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
+ * This program/include file is distributed in the hope that it will be
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program (in the main directory of the Linux-NTFS 
+ * along with this program (in the main directory of the Linux-NTFS
  * distribution in the file COPYING); if not, write to the Free Software
  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #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
  *    file name in the WIN32 namespace corresponding to the matched short file
  *    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.
  */
 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
                struct nameidata *nd)
 {
        ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
        struct inode *dent_inode;
-       uchar_t *uname;
+       ntfschar *uname;
        ntfs_name *name = NULL;
        MFT_REF mref;
        unsigned long dent_ino;
@@ -122,15 +127,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;
@@ -169,20 +175,22 @@ handle_name:
    {
        struct dentry *real_dent, *new_dent;
        MFT_RECORD *m;
-       attr_search_context *ctx;
+       ntfs_attr_search_ctx *ctx;
        ntfs_inode *ni = NTFS_I(dent_inode);
        int err;
        struct qstr nls_name;
 
        nls_name.name = NULL;
        if (name->type != FILE_NAME_DOS) {                      /* Case 2. */
+               ntfs_debug("Case 2.");
                nls_name.len = (unsigned)ntfs_ucstonls(vol,
-                               (uchar_t*)&name->name, name->len,
+                               (ntfschar*)&name->name, name->len,
                                (unsigned char**)&nls_name.name, 0);
                kfree(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. */
@@ -194,8 +202,8 @@ handle_name:
                        ctx = NULL;
                        goto err_out;
                }
-               ctx = get_attr_search_ctx(ni, m);
-               if (!ctx) {
+               ctx = ntfs_attr_get_search_ctx(ni, m);
+               if (unlikely(!ctx)) {
                        err = -ENOMEM;
                        goto err_out;
                }
@@ -203,12 +211,14 @@ handle_name:
                        ATTR_RECORD *a;
                        u32 val_len;
 
-                       if (!lookup_attr(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
-                                       ctx)) {
+                       err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
+                                       NULL, 0, ctx);
+                       if (unlikely(err)) {
                                ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
                                                "namespace counterpart to DOS "
                                                "file name. Run chkdsk.");
-                               err = -EIO;
+                               if (err == -ENOENT)
+                                       err = -EIO;
                                goto err_out;
                        }
                        /* Consistency checks. */
@@ -221,17 +231,17 @@ handle_name:
                                goto eio_err_out;
                        fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
                                        ctx->attr->data.resident.value_offset));
-                       if ((u32)(fn->file_name_length * sizeof(uchar_t) +
+                       if ((u32)(fn->file_name_length * sizeof(ntfschar) +
                                        sizeof(FILE_NAME_ATTR)) > val_len)
                                goto eio_err_out;
                } while (fn->file_name_type != FILE_NAME_WIN32);
 
                /* Convert the found WIN32 name to current NLS code page. */
                nls_name.len = (unsigned)ntfs_ucstonls(vol,
-                               (uchar_t*)&fn->file_name, fn->file_name_length,
+                               (ntfschar*)&fn->file_name, fn->file_name_length,
                                (unsigned char**)&nls_name.name, 0);
 
-               put_attr_search_ctx(ctx);
+               ntfs_attr_put_search_ctx(ctx);
                unmap_mft_record(ni);
        }
        m = NULL;
@@ -264,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
@@ -278,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;
        }
        /*
@@ -288,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);
@@ -301,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;
        }
        /*
@@ -320,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:
@@ -327,10 +347,11 @@ eio_err_out:
        err = -EIO;
 err_out:
        if (ctx)
-               put_attr_search_ctx(ctx);
+               ntfs_attr_put_search_ctx(ctx);
        if (m)
                unmap_mft_record(ni);
        iput(dent_inode);
+       ntfs_error(vol->sb, "Failed, returning error code %i.", err);
        return ERR_PTR(err);
    }
 }
@@ -364,32 +385,36 @@ struct dentry *ntfs_get_parent(struct dentry *child_dent)
        struct inode *vi = child_dent->d_inode;
        ntfs_inode *ni = NTFS_I(vi);
        MFT_RECORD *mrec;
-       attr_search_context *ctx;
+       ntfs_attr_search_ctx *ctx;
        ATTR_RECORD *attr;
        FILE_NAME_ATTR *fn;
        struct inode *parent_vi;
        struct dentry *parent_dent;
        unsigned long parent_ino;
+       int err;
 
        ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
        /* Get the mft record of the inode belonging to the child dentry. */
        mrec = map_mft_record(ni);
-       if (unlikely(IS_ERR(mrec)))
+       if (IS_ERR(mrec))
                return (struct dentry *)mrec;
        /* Find the first file name attribute in the mft record. */
-       ctx = get_attr_search_ctx(ni, mrec);
+       ctx = ntfs_attr_get_search_ctx(ni, mrec);
        if (unlikely(!ctx)) {
                unmap_mft_record(ni);
                return ERR_PTR(-ENOMEM);
        }
 try_next:
-       if (unlikely(!lookup_attr(AT_FILE_NAME, NULL, 0, IGNORE_CASE, 0,
-                       NULL, 0, ctx))) {
-               put_attr_search_ctx(ctx);
+       err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
+                       0, ctx);
+       if (unlikely(err)) {
+               ntfs_attr_put_search_ctx(ctx);
                unmap_mft_record(ni);
-               ntfs_error(vi->i_sb, "Inode 0x%lx does not have a file name "
-                               "attribute. Run chkdsk.", vi->i_ino);
-               return ERR_PTR(-ENOENT);
+               if (err == -ENOENT)
+                       ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
+                                       "file name attribute.  Run chkdsk.",
+                                       vi->i_ino);
+               return ERR_PTR(err);
        }
        attr = ctx->attr;
        if (unlikely(attr->non_resident))
@@ -402,11 +427,11 @@ try_next:
        /* Get the inode number of the parent directory. */
        parent_ino = MREF_LE(fn->parent_directory);
        /* Release the search context and the mft record of the child. */
-       put_attr_search_ctx(ctx);
+       ntfs_attr_put_search_ctx(ctx);
        unmap_mft_record(ni);
        /* Get the inode of the parent directory. */
        parent_vi = ntfs_iget(vi->i_sb, parent_ino);
-       if (unlikely(IS_ERR(parent_vi) || is_bad_inode(parent_vi))) {
+       if (IS_ERR(parent_vi) || unlikely(is_bad_inode(parent_vi))) {
                if (!IS_ERR(parent_vi))
                        iput(parent_vi);
                ntfs_error(vi->i_sb, "Failed to get parent directory inode "
@@ -449,7 +474,7 @@ struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
 
        ntfs_debug("Entering for inode 0x%lx, generation 0x%x.", ino, gen);
        vi = ntfs_iget(sb, ino);
-       if (unlikely(IS_ERR(vi))) {
+       if (IS_ERR(vi)) {
                ntfs_error(sb, "Failed to get inode 0x%lx.", ino);
                return (struct dentry *)vi;
        }