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.
26 #include <linux/version.h>
28 #include <asm/unistd.h>
30 #define DEBUG_SUBSYSTEM S_FILTER
32 #include <linux/obd.h>
33 #include <linux/lustre_lib.h>
35 /* Debugging check only needed during development */
37 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
38 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERT(!segment_eq(get_fs(), get_ds()))
39 # define ASSERT_KERNEL_CTXT(msg) LASSERT(segment_eq(get_fs(), get_ds()))
41 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
42 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
43 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
46 /* push / pop to root of obd store */
47 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
50 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
51 ASSERT_CTXT_MAGIC(new_ctx->magic);
52 OBD_SET_CTXT_MAGIC(save);
56 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
57 save, current, current->fs, current->fs->pwd,
58 atomic_read(¤t->fs->pwd->d_count),
59 atomic_read(¤t->fs->pwd->d_inode->i_count),
60 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
62 atomic_read(¤t->fs->pwdmnt->mnt_count));
66 LASSERT(atomic_read(¤t->fs->pwd->d_count));
67 LASSERT(atomic_read(&new_ctx->pwd->d_count));
68 save->pwd = dget(current->fs->pwd);
69 save->pwdmnt = mntget(current->fs->pwdmnt);
70 save->ngroups = current->ngroups;
73 LASSERT(save->pwdmnt);
74 LASSERT(new_ctx->pwd);
75 LASSERT(new_ctx->pwdmnt);
78 save->ouc.ouc_fsuid = current->fsuid;
79 save->ouc.ouc_fsgid = current->fsgid;
80 save->ouc.ouc_cap = current->cap_effective;
81 save->ouc.ouc_suppgid1 = current->groups[0];
82 save->ouc.ouc_suppgid2 = current->groups[1];
84 current->fsuid = uc->ouc_fsuid;
85 current->fsgid = uc->ouc_fsgid;
86 current->cap_effective = uc->ouc_cap;
89 if (uc->ouc_suppgid1 != -1)
90 current->groups[current->ngroups++] = uc->ouc_suppgid1;
91 if (uc->ouc_suppgid2 != -1)
92 current->groups[current->ngroups++] = uc->ouc_suppgid2;
95 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
99 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
100 new_ctx, current, current->fs, current->fs->pwd,
101 atomic_read(¤t->fs->pwd->d_count),
102 atomic_read(¤t->fs->pwd->d_inode->i_count),
103 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
105 atomic_read(¤t->fs->pwdmnt->mnt_count));
108 EXPORT_SYMBOL(push_ctxt);
110 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
111 struct obd_ucred *uc)
114 ASSERT_CTXT_MAGIC(saved->magic);
116 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
120 " = pop %p==%p = cur %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
121 new_ctx, current, current->fs, current->fs->pwd,
122 atomic_read(¤t->fs->pwd->d_count),
123 atomic_read(¤t->fs->pwd->d_inode->i_count),
124 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
126 atomic_read(¤t->fs->pwdmnt->mnt_count));
129 LASSERT(current->fs->pwd == new_ctx->pwd);
130 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
133 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
136 mntput(saved->pwdmnt);
138 current->fsuid = saved->ouc.ouc_fsuid;
139 current->fsgid = saved->ouc.ouc_fsgid;
140 current->cap_effective = saved->ouc.ouc_cap;
141 current->ngroups = saved->ngroups;
142 current->groups[0] = saved->ouc.ouc_suppgid1;
143 current->groups[1] = saved->ouc.ouc_suppgid2;
148 "= pop %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
149 saved, current, current->fs, current->fs->pwd,
150 atomic_read(¤t->fs->pwd->d_count),
151 atomic_read(¤t->fs->pwd->d_inode->i_count),
152 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
154 atomic_read(¤t->fs->pwdmnt->mnt_count));
157 EXPORT_SYMBOL(pop_ctxt);
159 /* utility to make a file */
160 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
162 struct dentry *dchild;
166 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
167 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
169 dchild = ll_lookup_one_len(name, dir, strlen(name));
171 GOTO(out_up, dchild);
173 if (dchild->d_inode) {
174 if (!S_ISREG(dchild->d_inode->i_mode))
175 GOTO(out_err, err = -EEXIST);
177 GOTO(out_up, dchild);
180 err = vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG);
188 dchild = ERR_PTR(err);
192 EXPORT_SYMBOL(simple_mknod);
194 /* utility to make a directory */
195 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
197 struct dentry *dchild;
201 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
202 CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
203 dchild = ll_lookup_one_len(name, dir, strlen(name));
205 GOTO(out_up, dchild);
207 if (dchild->d_inode) {
208 if (!S_ISDIR(dchild->d_inode->i_mode))
209 GOTO(out_err, err = -ENOTDIR);
211 GOTO(out_up, dchild);
214 err = vfs_mkdir(dir->d_inode, dchild, mode);
222 dchild = ERR_PTR(err);
226 EXPORT_SYMBOL(simple_mkdir);
229 * Read a file from within kernel context. Prior to calling this
230 * function we should already have done a push_ctxt().
232 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
234 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
235 if (!file || !file->f_op || !file->f_op->read || !off)
238 return file->f_op->read(file, buf, len, off);
240 EXPORT_SYMBOL(lustre_fread);
243 * Write a file from within kernel context. Prior to calling this
244 * function we should already have done a push_ctxt().
246 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
249 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
257 if (!file->f_op->write)
260 RETURN(file->f_op->write(file, buf, len, off));
262 EXPORT_SYMBOL(lustre_fwrite);
265 * Sync a file from within kernel context. Prior to calling this
266 * function we should already have done a push_ctxt().
268 int lustre_fsync(struct file *file)
271 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
272 if (!file || !file->f_op || !file->f_op->fsync)
275 RETURN(file->f_op->fsync(file, file->f_dentry, 0));
277 EXPORT_SYMBOL(lustre_fsync);