Whamcloud - gitweb
29ffc95b70dc170b7fc5fca231cc20748e6d55ab
[fs/lustre-release.git] / lustre / ptlrpc / sec_ctx.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  *
19  * http://www.gnu.org/licenses/gpl-2.0.html
20  *
21  * GPL HEADER END
22  */
23 #ifdef __KERNEL__
24
25 #define DEBUG_SUBSYSTEM S_FILTER
26
27 #include <linux/fs.h>
28 #include <libcfs/libcfs.h>
29 #include <lvfs.h>
30
31 /* refine later and change to seqlock or simlar from libcfs */
32 /* Debugging check only needed during development */
33 #ifdef OBD_CTXT_DEBUG
34 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
35 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), get_ds()),\
36                                               msg)
37 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), get_ds()), msg)
38 #else
39 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
40 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
41 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
42 #endif
43
44 /* push / pop to root of obd store */
45 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
46 {
47         /* if there is underlaying dt_device then push_ctxt is not needed */
48         if (new_ctx->dt != NULL)
49                 return;
50
51         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
52         ASSERT_CTXT_MAGIC(new_ctx->magic);
53         OBD_SET_CTXT_MAGIC(save);
54
55         save->fs = get_fs();
56         LASSERT(d_count(current->fs->pwd.dentry));
57         LASSERT(d_count(new_ctx->pwd));
58         save->pwd = dget(current->fs->pwd.dentry);
59         save->pwdmnt = mntget(current->fs->pwd.mnt);
60         save->umask = current_umask();
61
62         LASSERT(save->pwd);
63         LASSERT(save->pwdmnt);
64         LASSERT(new_ctx->pwd);
65         LASSERT(new_ctx->pwdmnt);
66
67         current->fs->umask = 0; /* umask already applied on client */
68         set_fs(new_ctx->fs);
69         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
70 }
71 EXPORT_SYMBOL(push_ctxt);
72
73 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
74 {
75         /* if there is underlaying dt_device then pop_ctxt is not needed */
76         if (new_ctx->dt != NULL)
77                 return;
78
79         ASSERT_CTXT_MAGIC(saved->magic);
80         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
81
82         LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
83                  current->fs->pwd.dentry, new_ctx->pwd);
84         LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
85                  current->fs->pwd.mnt, new_ctx->pwdmnt);
86
87         set_fs(saved->fs);
88         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
89
90         dput(saved->pwd);
91         mntput(saved->pwdmnt);
92         current->fs->umask = saved->umask;
93 }
94 EXPORT_SYMBOL(pop_ctxt);
95
96 /* utility to rename a file */
97 int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
98                   char *oldname, char *newname)
99 {
100         struct dentry *dchild_old, *dchild_new;
101         int err = 0;
102         ENTRY;
103
104         ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
105         CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
106                (int)strlen(oldname), oldname, (int)strlen(newname), newname);
107
108         dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
109         if (IS_ERR(dchild_old))
110                 RETURN(PTR_ERR(dchild_old));
111
112         if (!dchild_old->d_inode)
113                 GOTO(put_old, err = -ENOENT);
114
115         dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
116         if (IS_ERR(dchild_new))
117                 GOTO(put_old, err = PTR_ERR(dchild_new));
118
119         err = vfs_rename(dir->d_inode, dchild_old, dir->d_inode, dchild_new);
120
121         dput(dchild_new);
122 put_old:
123         dput(dchild_old);
124         RETURN(err);
125 }
126 #endif /* __KERNEL__ */