Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / lvfs / lvfs_linux.c
index 9bdcf35..1b4ab82 100644 (file)
@@ -1,26 +1,41 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  lustre/lib/lvfs_linux.c
- *  Lustre filesystem abstraction routines
+ * GPL HEADER START
  *
- *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
- *   Author: Andreas Dilger <adilger@clusterfs.com>
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program 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 version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre 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
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/lvfs/lvfs_linux.c
+ *
+ * Author: Andreas Dilger <adilger@clusterfs.com>
  */
 
 #ifndef EXPORT_SYMTAB
 #include <linux/pagemap.h>
 #include <linux/quotaops.h>
 #include <linux/version.h>
-#include <libcfs/kp30.h>
+#include <libcfs/libcfs.h>
 #include <lustre_fsfilt.h>
 #include <obd.h>
-#include <obd_class.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/lustre_compat25.h>
@@ -102,17 +116,6 @@ void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx,
         ASSERT_CTXT_MAGIC(new_ctx->magic);
         OBD_SET_CTXT_MAGIC(save);
 
-        /*
-        CDEBUG(D_INFO,
-               "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%.*s), pwdmnt %p:%d\n",
-               save, current, current->fs, current->fs->pwd,
-               atomic_read(&current->fs->pwd->d_count),
-               atomic_read(&current->fs->pwd->d_inode->i_count),
-               current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
-               current->fs->pwdmnt,
-               atomic_read(&current->fs->pwdmnt->mnt_count));
-        */
-
         save->fs = get_fs();
         LASSERT(atomic_read(&current->fs->pwd->d_count));
         LASSERT(atomic_read(&new_ctx->pwd->d_count));
@@ -147,39 +150,15 @@ void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx,
         current->fs->umask = 0; /* umask already applied on client */
         set_fs(new_ctx->fs);
         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
-
-        /*
-        CDEBUG(D_INFO,
-               "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%.*s), pwdmnt %p:%d\n",
-               new_ctx, current, current->fs, current->fs->pwd,
-               atomic_read(&current->fs->pwd->d_count),
-               atomic_read(&current->fs->pwd->d_inode->i_count),
-               current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
-               current->fs->pwdmnt,
-               atomic_read(&current->fs->pwdmnt->mnt_count));
-        */
 }
 EXPORT_SYMBOL(push_ctxt);
 
 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx,
               struct lvfs_ucred *uc)
 {
-        //printk("pc0");
         ASSERT_CTXT_MAGIC(saved->magic);
-        //printk("pc1");
         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
 
-        /*
-        CDEBUG(D_INFO,
-               " = pop  %p==%p = cur %p pwd %p:d%d:i%d (%.*s), pwdmnt %p:%d\n",
-               new_ctx, current, current->fs, current->fs->pwd,
-               atomic_read(&current->fs->pwd->d_count),
-               atomic_read(&current->fs->pwd->d_inode->i_count),
-               current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
-               current->fs->pwdmnt,
-               atomic_read(&current->fs->pwdmnt->mnt_count));
-        */
-
         LASSERTF(current->fs->pwd == new_ctx->pwd, "%p != %p\n",
                  current->fs->pwd, new_ctx->pwd);
         LASSERTF(current->fs->pwdmnt == new_ctx->pwdmnt, "%p != %p\n",
@@ -202,17 +181,6 @@ void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx,
                                uc->luc_identity ? uc->luc_identity->mi_ginfo :
                                                   NULL);
         }
-
-        /*
-        CDEBUG(D_INFO,
-               "= pop  %p->%p = cur fs %p pwd %p:d%d:i%d (%.*s), pwdmnt %p:%d\n",
-               saved, current, current->fs, current->fs->pwd,
-               atomic_read(&current->fs->pwd->d_count),
-               atomic_read(&current->fs->pwd->d_inode->i_count),
-               current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
-               current->fs->pwdmnt,
-               atomic_read(&current->fs->pwdmnt->mnt_count));
-        */
 }
 EXPORT_SYMBOL(pop_ctxt);
 
@@ -262,7 +230,8 @@ out_up:
 EXPORT_SYMBOL(simple_mknod);
 
 /* utility to make a directory */
-struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode, int fix)
+struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt, 
+                            const char *name, int mode, int fix)
 {
         struct dentry *dchild;
         int err = 0;
@@ -285,7 +254,7 @@ struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode, int fix)
 
                 /* Fixup directory permissions if necessary */
                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
-                        CDEBUG(D_CONFIG, 
+                        CDEBUG(D_CONFIG,
                                "fixing permissions on %s from %o to %o\n",
                                name, old_mode, mode);
                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
@@ -295,7 +264,7 @@ struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode, int fix)
                 GOTO(out_up, dchild);
         }
 
-        err = vfs_mkdir(dir->d_inode, dchild, mode);
+        err = ll_vfs_mkdir(dir->d_inode, dchild, mnt, mode);
         if (err)
                 GOTO(out_err, err);
 
@@ -310,28 +279,30 @@ out_up:
 EXPORT_SYMBOL(simple_mkdir);
 
 /* utility to rename a file */
-int lustre_rename(struct dentry *dir, char *oldname, char *newname)
+int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
+                  char *oldname, char *newname)
 {
         struct dentry *dchild_old, *dchild_new;
         int err = 0;
         ENTRY;
 
         ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
-        CDEBUG(D_INODE, "renaming file %.*s to %.*s\n", 
+        CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
                (int)strlen(oldname), oldname, (int)strlen(newname), newname);
 
         dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
         if (IS_ERR(dchild_old))
                 RETURN(PTR_ERR(dchild_old));
 
-        if (!dchild_old->d_inode) 
+        if (!dchild_old->d_inode)
                 GOTO(put_old, err = -ENOENT);
 
         dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
         if (IS_ERR(dchild_new))
                 GOTO(put_old, err = PTR_ERR(dchild_new));
 
-        err = vfs_rename(dir->d_inode, dchild_old, dir->d_inode, dchild_new);
+        err = ll_vfs_rename(dir->d_inode, dchild_old, mnt,
+                            dir->d_inode, dchild_new, mnt);
 
         dput(dchild_new);
 put_old:
@@ -391,6 +362,7 @@ int lustre_fsync(struct file *file)
 }
 EXPORT_SYMBOL(lustre_fsync);
 
+/* Note: dput(dchild) will be called if there is an error */
 struct l_file *l_dentry_open(struct lvfs_run_ctxt *ctxt, struct l_dentry *de,
                              int flags)
 {
@@ -450,188 +422,57 @@ long l_readdir(struct file *file, struct list_head *dentry_list)
 }
 EXPORT_SYMBOL(l_readdir);
 
-#if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
-static spinlock_t obd_memlist_lock = SPIN_LOCK_UNLOCKED;
-static struct hlist_head *obd_memtable = NULL;
-static unsigned long obd_memtable_size = 0;
-
-static int lvfs_memdbg_init(int size)
-{
-        struct hlist_head *head;
-        int i;
-
-        LASSERT(size > sizeof(sizeof(struct hlist_head)));
-        obd_memtable_size = size / sizeof(struct hlist_head);
-
-        CWARN("Allocating %lu memdbg entries.\n",
-              (unsigned long)obd_memtable_size);
-
-        LASSERT(obd_memtable == NULL);
-        obd_memtable = kmalloc(size, GFP_KERNEL);
-        if (!obd_memtable)
-                return -ENOMEM;
-
-        i = obd_memtable_size;
-        head = obd_memtable;
-        do {
-                INIT_HLIST_HEAD(head);
-                head++;
-                i--;
-        } while(i);
-
-        return 0;
-}
-
-static int lvfs_memdbg_cleanup(void)
+int l_notify_change(struct vfsmount *mnt, struct dentry *dchild,
+                 struct iattr *newattrs)
 {
-        struct hlist_node *node = NULL, *tmp = NULL;
-        struct hlist_head *head;
-        struct obd_mem_track *mt;
-        int i;
-
-        spin_lock(&obd_memlist_lock);
-        for (i = 0, head = obd_memtable; i < obd_memtable_size; i++, head++) {
-                hlist_for_each_safe(node, tmp, head) {
-                        mt = hlist_entry(node, struct obd_mem_track, mt_hash);
-                        hlist_del_init(&mt->mt_hash);
-                        kfree(mt);
-                }
-        }
-        spin_unlock(&obd_memlist_lock);
-        kfree(obd_memtable);
-        return 0;
-}
-
-static inline unsigned long const hashfn(void *ptr)
-{
-        return (unsigned long)ptr &
-                (obd_memtable_size - 1);
-}
-
-static void __lvfs_memdbg_insert(struct obd_mem_track *mt)
-{
-        struct hlist_head *head = obd_memtable +
-                hashfn(mt->mt_ptr);
-        hlist_add_head(&mt->mt_hash, head);
-}
+        int rc;
 
-void lvfs_memdbg_insert(struct obd_mem_track *mt)
-{
-        spin_lock(&obd_memlist_lock);
-        __lvfs_memdbg_insert(mt);
-        spin_unlock(&obd_memlist_lock);
-}
-EXPORT_SYMBOL(lvfs_memdbg_insert);
-
-static void __lvfs_memdbg_remove(struct obd_mem_track *mt)
-{
-        hlist_del_init(&mt->mt_hash);
-}
-
-void lvfs_memdbg_remove(struct obd_mem_track *mt)
-{
-        spin_lock(&obd_memlist_lock);
-        __lvfs_memdbg_remove(mt);
-        spin_unlock(&obd_memlist_lock);
-}
-EXPORT_SYMBOL(lvfs_memdbg_remove);
-
-static struct obd_mem_track *__lvfs_memdbg_find(void *ptr)
-{
-        struct hlist_node *node = NULL;
-        struct obd_mem_track *mt = NULL;
-        struct hlist_head *head;
-
-        head = obd_memtable + hashfn(ptr);
-
-        hlist_for_each(node, head) {
-                mt = hlist_entry(node, struct obd_mem_track, mt_hash);
-                if ((unsigned long)mt->mt_ptr == (unsigned long)ptr)
-                        break;
-                mt = NULL;
-        }
-        return mt;
-}
-
-struct obd_mem_track *lvfs_memdbg_find(void *ptr)
-{
-        struct obd_mem_track *mt;
-
-        spin_lock(&obd_memlist_lock);
-        mt = __lvfs_memdbg_find(ptr);
-        spin_unlock(&obd_memlist_lock);
-        
-        return mt;
-}
-EXPORT_SYMBOL(lvfs_memdbg_find);
-
-int lvfs_memdbg_check_insert(struct obd_mem_track *mt)
-{
-        struct obd_mem_track *tmp;
-        
-        spin_lock(&obd_memlist_lock);
-        tmp = __lvfs_memdbg_find(mt->mt_ptr);
-        if (tmp == NULL) {
-                __lvfs_memdbg_insert(mt);
-                spin_unlock(&obd_memlist_lock);
-                return 1;
-        }
-        spin_unlock(&obd_memlist_lock);
-        return 0;
+        LOCK_INODE_MUTEX(dchild->d_inode);
+#ifdef HAVE_SECURITY_PLUG
+        rc = notify_change(dchild, mnt, newattrs);
+#else
+        rc = notify_change(dchild, newattrs);
+#endif
+        UNLOCK_INODE_MUTEX(dchild->d_inode);
+        return rc;
 }
-EXPORT_SYMBOL(lvfs_memdbg_check_insert);
+EXPORT_SYMBOL(l_notify_change);
 
-struct obd_mem_track *
-lvfs_memdbg_check_remove(void *ptr)
+/* utility to truncate a file */
+int simple_truncate(struct dentry *dir, struct vfsmount *mnt, 
+                 char *name, loff_t length)
 {
-        struct obd_mem_track *mt;
-
-        spin_lock(&obd_memlist_lock);
-        mt = __lvfs_memdbg_find(ptr);
-        if (mt) {
-                __lvfs_memdbg_remove(mt);
-                spin_unlock(&obd_memlist_lock);
-                return mt;
-        }
-        spin_unlock(&obd_memlist_lock);
-        return NULL;
-}
-EXPORT_SYMBOL(lvfs_memdbg_check_remove);
-#endif
+        struct dentry *dchild;
+        struct iattr newattrs;
+        int err = 0;
+        ENTRY;
 
-void lvfs_memdbg_show(void)
-{
-#if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
-        struct hlist_node *node = NULL;
-        struct hlist_head *head;
-        struct obd_mem_track *mt;
-        int header = 0;
-#endif
-       
-#if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
-       int i;
-#endif
+        CDEBUG(D_INODE, "truncating file %.*s to %lld\n", (int)strlen(name),
+               name, (long long)length);
+        dchild = ll_lookup_one_len(name, dir, strlen(name));
+        if (IS_ERR(dchild))
+                GOTO(out, err = PTR_ERR(dchild));
 
-       
-#if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
-        spin_lock(&obd_memlist_lock);
-        for (i = 0, head = obd_memtable; i < obd_memtable_size; i++, head++) {
-                hlist_for_each(node, head) {
-                        if (header == 0) {
-                                CWARN("Abnormal memory activities:\n");
-                                header = 1;
-                        }
-                        mt = hlist_entry(node, struct obd_mem_track, mt_hash);
-                        CWARN("  [%s] ptr: 0x%p, size: %d, src at %s\n",
-                              ((mt->mt_flags & OBD_MT_WRONG_SIZE) ?
-                               "wrong size" : "leaked memory"),
-                              mt->mt_ptr, mt->mt_size, mt->mt_loc);
+        if (dchild->d_inode) {
+                int old_mode = dchild->d_inode->i_mode;
+                if (S_ISDIR(old_mode)) {
+                        CERROR("found %s (%lu/%u) is mode %o\n", name,
+                               dchild->d_inode->i_ino,
+                               dchild->d_inode->i_generation, old_mode);
+                        GOTO(out_dput, err = -EISDIR);
                 }
+
+                newattrs.ia_size = length;
+                newattrs.ia_valid = ATTR_SIZE;
+                err = l_notify_change(mnt, dchild, &newattrs);
         }
-        spin_unlock(&obd_memlist_lock);
-#endif
+        EXIT;
+out_dput:
+        dput(dchild);
+out:
+        return err;
 }
-EXPORT_SYMBOL(lvfs_memdbg_show);
+EXPORT_SYMBOL(simple_truncate);
 
 #ifdef LUSTRE_KERNEL_VERSION
 #ifndef HAVE_CLEAR_RDONLY_ON_PUT
@@ -642,7 +483,6 @@ int dev_check_rdonly(lvfs_sbdev_type dev);
 
 void __lvfs_set_rdonly(lvfs_sbdev_type dev, lvfs_sbdev_type jdev)
 {
-        lvfs_sbdev_sync(dev);
         if (jdev && (jdev != dev)) {
                 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
                        (long)jdev);
@@ -694,7 +534,7 @@ void obd_update_maxusage()
         if (max2 > obd_max_alloc)
                 obd_max_alloc = max2;
         spin_unlock(&obd_updatemax_lock);
-        
+
 }
 
 __u64 obd_memory_max(void)
@@ -728,7 +568,7 @@ EXPORT_SYMBOL(obd_memory);
 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
                           enum lprocfs_fields_flags field)
 {
-        __u64 ret = 0;
+        __s64 ret = 0;
         int centry;
 
         if (!lc)
@@ -741,7 +581,7 @@ __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
                                 ret = lc->lc_config;
                                 break;
                         case LPROCFS_FIELDS_FLAGS_SUM:
-                                ret = lc->lc_sum;
+                                ret = lc->lc_sum + lc->lc_sum_irq;
                                 break;
                         case LPROCFS_FIELDS_FLAGS_MIN:
                                 ret = lc->lc_min;
@@ -769,30 +609,6 @@ __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
 EXPORT_SYMBOL(lprocfs_read_helper);
 #endif /* LPROCFS */
 
-static int __init lvfs_linux_init(void)
-{
-        ENTRY;
-#if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
-        lvfs_memdbg_init(PAGE_SIZE);
-#endif
-        RETURN(0);
-}
-
-static void __exit lvfs_linux_exit(void)
-{
-        ENTRY;
-
-        lvfs_memdbg_show();
-        
-#if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
-        lvfs_memdbg_cleanup();
-#endif
-        EXIT;
-}
-
-MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
+MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
 MODULE_LICENSE("GPL");
-
-module_init(lvfs_linux_init);
-module_exit(lvfs_linux_exit);