1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Copyright (C) 2002 Cluster File Systems, Inc.
8 * This code is issued under the GNU General Public License.
9 * See the file COPYING in this distribution
11 * by Peter Braam <braam@clusterfs.com>
12 * and Andreas Dilger <adilger@clusterfs.com>
17 #include <linux/version.h>
19 #include <asm/unistd.h>
21 #define DEBUG_SUBSYSTEM S_FILTER
23 #include <linux/lustre_mds.h>
24 #include <linux/lustre_lib.h>
25 #include <linux/lustre_net.h>
28 /* Debugging check only needed during development */
29 #define ASSERT_CTXT_MAGIC(magic) do { if ((magic) != OBD_RUN_CTXT_MAGIC) { \
30 CERROR("bad ctxt magic\n"); LBUG(); } } while(0)
31 #define ASSERT_NOT_KERNEL_CTXT(msg) do { if (segment_eq(get_fs(), get_ds())) { \
32 CERROR(msg); LBUG(); } } while(0)
33 #define ASSERT_KERNEL_CTXT(msg) do { if (!segment_eq(get_fs(), get_ds())) { \
34 CERROR(msg); LBUG(); } } while(0)
36 #define ASSERT_CTXT_MAGIC(magic) do {} while(0)
37 #define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
38 #define ASSERT_KERNEL_CTXT(msg) do {} while(0)
41 /* push / pop to root of obd store */
42 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new)
44 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
45 ASSERT_CTXT_MAGIC(new->magic);
46 OBD_SET_CTXT_MAGIC(save);
48 save->pwd = dget(current->fs->pwd);
49 save->pwdmnt = mntget(current->fs->pwdmnt);
52 set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
55 void pop_ctxt(struct obd_run_ctxt *saved)
57 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
58 ASSERT_CTXT_MAGIC(saved->magic);
60 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
63 mntput(saved->pwdmnt);
66 /* utility to make a directory */
67 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
69 struct dentry *dchild;
73 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
74 CDEBUG(D_INODE, "creating directory %*s\n", strlen(name), name);
75 dchild = lookup_one_len(name, dir, strlen(name));
79 if (dchild->d_inode) {
80 if (!S_ISDIR(dchild->d_inode->i_mode))
81 GOTO(out, err = -ENOTDIR);
86 err = vfs_mkdir(dir->d_inode, dchild, mode);
98 * Read a file from within kernel context. Prior to calling this
99 * function we should already have done a push_ctxt().
101 int lustre_fread(struct file *file, char *str, int len, loff_t *off)
103 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
104 if (!file || !file->f_op || !file->f_op->read || !off)
107 return file->f_op->read(file, str, len, off);
111 * Write a file from within kernel context. Prior to calling this
112 * function we should already have done a push_ctxt().
114 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off)
116 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
117 if (!file || !file->f_op || !off)
120 if (!file->f_op->write)
123 return file->f_op->write(file, str, len, off);
127 * Sync a file from within kernel context. Prior to calling this
128 * function we should already have done a push_ctxt().
130 int lustre_fsync(struct file *file)
132 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
133 if (!file || !file->f_op || !file->f_op->fsync)
136 return file->f_op->fsync(file, file->f_dentry, 0);