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/obd.h>
24 #include <linux/lustre_mds.h>
25 #include <linux/lustre_lib.h>
26 #include <linux/lustre_net.h>
29 /* Debugging check only needed during development */
30 #define ASSERT_CTXT_MAGIC(magic) do { if ((magic) != OBD_RUN_CTXT_MAGIC) { \
31 CERROR("bad ctxt magic\n"); LBUG(); } } while(0)
32 #define ASSERT_NOT_KERNEL_CTXT(msg) do { if (segment_eq(get_fs(), get_ds())) { \
33 CERROR(msg); LBUG(); } } while(0)
34 #define ASSERT_KERNEL_CTXT(msg) do { if (!segment_eq(get_fs(), get_ds())) { \
35 CERROR(msg); LBUG(); } } while(0)
37 #define ASSERT_CTXT_MAGIC(magic) do {} while(0)
38 #define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
39 #define ASSERT_KERNEL_CTXT(msg) do {} while(0)
42 /* push / pop to root of obd store */
43 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new)
45 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
46 ASSERT_CTXT_MAGIC(new->magic);
47 OBD_SET_CTXT_MAGIC(save);
49 save->pwd = dget(current->fs->pwd);
50 save->pwdmnt = mntget(current->fs->pwdmnt);
53 LASSERT(save->pwdmnt);
58 set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
61 void pop_ctxt(struct obd_run_ctxt *saved)
63 ASSERT_CTXT_MAGIC(saved->magic);
64 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
67 LASSERT(saved->pwdmnt);
68 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
71 mntput(saved->pwdmnt);
74 /* utility to make a directory */
75 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
77 struct dentry *dchild;
81 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
82 CDEBUG(D_INODE, "creating directory %*s\n", strlen(name), name);
83 dchild = lookup_one_len(name, dir, strlen(name));
87 if (dchild->d_inode) {
88 if (!S_ISDIR(dchild->d_inode->i_mode))
89 GOTO(out, err = -ENOTDIR);
94 err = vfs_mkdir(dir->d_inode, dchild, mode);
106 * Read a file from within kernel context. Prior to calling this
107 * function we should already have done a push_ctxt().
109 int lustre_fread(struct file *file, char *str, int len, loff_t *off)
111 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
112 if (!file || !file->f_op || !file->f_op->read || !off)
115 return file->f_op->read(file, str, len, off);
119 * Write a file from within kernel context. Prior to calling this
120 * function we should already have done a push_ctxt().
122 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off)
124 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
125 if (!file || !file->f_op || !off)
128 if (!file->f_op->write)
131 return file->f_op->write(file, str, len, off);
135 * Sync a file from within kernel context. Prior to calling this
136 * function we should already have done a push_ctxt().
138 int lustre_fsync(struct file *file)
140 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
141 if (!file || !file->f_op || !file->f_op->fsync)
144 return file->f_op->fsync(file, file->f_dentry, 0);