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_support.h>
24 #include <linux/obd.h>
25 #include <linux/lustre_mds.h>
26 #include <linux/lustre_lib.h>
27 #include <linux/lustre_net.h>
30 /* Debugging check only needed during development */
31 #define ASSERT_CTXT_MAGIC(magic) do { if ((magic) != OBD_RUN_CTXT_MAGIC) { \
32 CERROR("bad ctxt magic\n"); LBUG(); } } while(0)
33 #define ASSERT_NOT_KERNEL_CTXT(msg) do { if (segment_eq(get_fs(), get_ds())) { \
34 CERROR(msg); LBUG(); } } while(0)
35 #define ASSERT_KERNEL_CTXT(msg) do { if (!segment_eq(get_fs(), get_ds())) { \
36 CERROR(msg); LBUG(); } } while(0)
38 #define ASSERT_CTXT_MAGIC(magic) do {} while(0)
39 #define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
40 #define ASSERT_KERNEL_CTXT(msg) do {} while(0)
43 /* push / pop to root of obd store */
44 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
47 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
48 ASSERT_CTXT_MAGIC(new_ctx->magic);
49 OBD_SET_CTXT_MAGIC(save);
52 CDEBUG(D_INFO, "== push %p->%p == cur fs %p pwd %p (%*s), pwdmnt %p\n",
53 save, current, current->fs, current->fs->pwd,
54 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
59 save->pwd = dget(current->fs->pwd);
60 save->pwdmnt = mntget(current->fs->pwdmnt);
63 LASSERT(save->pwdmnt);
64 LASSERT(new_ctx->pwd);
65 LASSERT(new_ctx->pwdmnt);
68 save->fsuid = current->fsuid;
69 save->fsgid = current->fsgid;
70 save->cap = current->cap_effective;
72 current->fsuid = uc->ouc_fsuid;
73 current->fsgid = uc->ouc_fsgid;
74 current->cap_effective = uc->ouc_cap;
77 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
80 CDEBUG(D_INFO, "== push %p==%p == cur fs %p pwd %p (%*s), pwdmnt %p\n",
81 new_ctx, current, current->fs, current->fs->pwd,
82 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
87 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
91 ASSERT_CTXT_MAGIC(saved->magic);
93 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
96 CDEBUG(D_INFO, " == pop %p==%p == cur %p pwd %p (%*s), pwdmnt %p\n",
97 new_ctx, current, current->fs, current->fs->pwd,
98 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
102 LASSERT(current->fs->pwd == new_ctx->pwd);
103 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
108 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
113 mntput(saved->pwdmnt);
116 current->fsuid = saved->fsuid;
117 current->fsgid = saved->fsgid;
118 current->cap_effective = saved->cap;
122 CDEBUG(D_INFO, "== pop %p->%p == cur fs %p pwd %p (%*s), pwdmnt %p\n",
123 saved, current, current->fs, current->fs->pwd,
124 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
125 current->fs->pwdmnt);
129 /* utility to make a file */
130 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
132 struct dentry *dchild;
136 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
137 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
139 down(&dir->d_inode->i_sem);
140 dchild = lookup_one_len(name, dir, strlen(name));
142 GOTO(out, PTR_ERR(dchild));
144 if (dchild->d_inode) {
145 if ((dchild->d_inode->i_mode & S_IFMT) != S_IFREG)
146 GOTO(out, err = -EEXIST);
151 err = vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG);
154 up(&dir->d_inode->i_sem);
157 RETURN(ERR_PTR(err));
163 /* utility to make a directory */
164 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
166 struct dentry *dchild;
170 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
171 CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
172 down(&dir->d_inode->i_sem);
173 dchild = lookup_one_len(name, dir, strlen(name));
175 GOTO(out, PTR_ERR(dchild));
177 if (dchild->d_inode) {
178 if (!S_ISDIR(dchild->d_inode->i_mode))
179 GOTO(out, err = -ENOTDIR);
184 err = vfs_mkdir(dir->d_inode, dchild, mode);
187 up(&dir->d_inode->i_sem);
190 RETURN(ERR_PTR(err));
197 * Read a file from within kernel context. Prior to calling this
198 * function we should already have done a push_ctxt().
200 int lustre_fread(struct file *file, char *str, int len, loff_t *off)
202 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
203 if (!file || !file->f_op || !file->f_op->read || !off)
206 return file->f_op->read(file, str, len, off);
210 * Write a file from within kernel context. Prior to calling this
211 * function we should already have done a push_ctxt().
213 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off)
215 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
216 if (!file || !file->f_op || !off)
219 if (!file->f_op->write)
222 return file->f_op->write(file, str, len, off);
226 * Sync a file from within kernel context. Prior to calling this
227 * function we should already have done a push_ctxt().
229 int lustre_fsync(struct file *file)
231 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
232 if (!file || !file->f_op || !file->f_op->fsync)
235 return file->f_op->fsync(file, file->f_dentry, 0);