1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Aurhot: Andreas Dilger <adilger@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # define EXPORT_SYMTAB
28 #include <linux/version.h>
30 #include <asm/unistd.h>
32 #define DEBUG_SUBSYSTEM S_FILTER
34 #include <linux/obd.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_compat25.h>
38 /* Debugging check only needed during development */
40 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
41 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERT(!segment_eq(get_fs(), get_ds()))
42 # define ASSERT_KERNEL_CTXT(msg) LASSERT(segment_eq(get_fs(), get_ds()))
44 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
45 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
46 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
49 /* push / pop to root of obd store */
50 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
53 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
54 ASSERT_CTXT_MAGIC(new_ctx->magic);
55 OBD_SET_CTXT_MAGIC(save);
59 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
60 save, current, current->fs, current->fs->pwd,
61 atomic_read(¤t->fs->pwd->d_count),
62 atomic_read(¤t->fs->pwd->d_inode->i_count),
63 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
65 atomic_read(¤t->fs->pwdmnt->mnt_count));
69 LASSERT(atomic_read(¤t->fs->pwd->d_count));
70 LASSERT(atomic_read(&new_ctx->pwd->d_count));
71 save->pwd = dget(current->fs->pwd);
72 save->pwdmnt = mntget(current->fs->pwdmnt);
73 save->ngroups = current->ngroups;
76 LASSERT(save->pwdmnt);
77 LASSERT(new_ctx->pwd);
78 LASSERT(new_ctx->pwdmnt);
81 save->ouc.ouc_fsuid = current->fsuid;
82 save->ouc.ouc_fsgid = current->fsgid;
83 save->ouc.ouc_cap = current->cap_effective;
84 save->ouc.ouc_suppgid1 = current->groups[0];
85 save->ouc.ouc_suppgid2 = current->groups[1];
87 current->fsuid = uc->ouc_fsuid;
88 current->fsgid = uc->ouc_fsgid;
89 current->cap_effective = uc->ouc_cap;
92 if (uc->ouc_suppgid1 != -1)
93 current->groups[current->ngroups++] = uc->ouc_suppgid1;
94 if (uc->ouc_suppgid2 != -1)
95 current->groups[current->ngroups++] = uc->ouc_suppgid2;
98 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
102 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
103 new_ctx, current, current->fs, current->fs->pwd,
104 atomic_read(¤t->fs->pwd->d_count),
105 atomic_read(¤t->fs->pwd->d_inode->i_count),
106 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
108 atomic_read(¤t->fs->pwdmnt->mnt_count));
111 EXPORT_SYMBOL(push_ctxt);
113 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
114 struct obd_ucred *uc)
117 ASSERT_CTXT_MAGIC(saved->magic);
119 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
123 " = pop %p==%p = cur %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
124 new_ctx, current, current->fs, current->fs->pwd,
125 atomic_read(¤t->fs->pwd->d_count),
126 atomic_read(¤t->fs->pwd->d_inode->i_count),
127 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
129 atomic_read(¤t->fs->pwdmnt->mnt_count));
132 LASSERT(current->fs->pwd == new_ctx->pwd);
133 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
136 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
139 mntput(saved->pwdmnt);
141 current->fsuid = saved->ouc.ouc_fsuid;
142 current->fsgid = saved->ouc.ouc_fsgid;
143 current->cap_effective = saved->ouc.ouc_cap;
144 current->ngroups = saved->ngroups;
145 current->groups[0] = saved->ouc.ouc_suppgid1;
146 current->groups[1] = saved->ouc.ouc_suppgid2;
151 "= pop %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
152 saved, current, current->fs, current->fs->pwd,
153 atomic_read(¤t->fs->pwd->d_count),
154 atomic_read(¤t->fs->pwd->d_inode->i_count),
155 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
157 atomic_read(¤t->fs->pwdmnt->mnt_count));
160 EXPORT_SYMBOL(pop_ctxt);
162 /* utility to make a file */
163 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
165 struct dentry *dchild;
169 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
170 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
172 dchild = ll_lookup_one_len(name, dir, strlen(name));
174 GOTO(out_up, dchild);
176 if (dchild->d_inode) {
177 if (!S_ISREG(dchild->d_inode->i_mode))
178 GOTO(out_err, err = -EEXIST);
180 GOTO(out_up, dchild);
183 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG, NULL);
191 dchild = ERR_PTR(err);
195 EXPORT_SYMBOL(simple_mknod);
197 /* utility to make a directory */
198 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
200 struct dentry *dchild;
204 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
205 CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
206 dchild = ll_lookup_one_len(name, dir, strlen(name));
208 GOTO(out_up, dchild);
210 if (dchild->d_inode) {
211 if (!S_ISDIR(dchild->d_inode->i_mode))
212 GOTO(out_err, err = -ENOTDIR);
214 GOTO(out_up, dchild);
217 err = vfs_mkdir(dir->d_inode, dchild, mode);
225 dchild = ERR_PTR(err);
229 EXPORT_SYMBOL(simple_mkdir);
232 * Read a file from within kernel context. Prior to calling this
233 * function we should already have done a push_ctxt().
235 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
237 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
238 if (!file || !file->f_op || !file->f_op->read || !off)
241 return file->f_op->read(file, buf, len, off);
243 EXPORT_SYMBOL(lustre_fread);
246 * Write a file from within kernel context. Prior to calling this
247 * function we should already have done a push_ctxt().
249 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
252 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
260 if (!file->f_op->write)
263 RETURN(file->f_op->write(file, buf, len, off));
265 EXPORT_SYMBOL(lustre_fwrite);