Whamcloud - gitweb
LU-5478 utils: get rid of obd_* typedefs
[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
24 #define DEBUG_SUBSYSTEM S_FILTER
25
26 #include <linux/fs.h>
27 #include <libcfs/libcfs.h>
28 #include <lvfs.h>
29 #include <obd_class.h>
30
31 #include "ptlrpc_internal.h"
32
33 /* refine later and change to seqlock or simlar from libcfs */
34 /* Debugging check only needed during development */
35 #ifdef OBD_CTXT_DEBUG
36 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
37 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), get_ds()),\
38                                               msg)
39 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), get_ds()), msg)
40 #else
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)
44 #endif
45
46 /* push / pop to root of obd store */
47 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
48 {
49         /* if there is underlaying dt_device then push_ctxt is not needed */
50         if (new_ctx->dt != NULL)
51                 return;
52
53         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
54         ASSERT_CTXT_MAGIC(new_ctx->magic);
55         OBD_SET_CTXT_MAGIC(save);
56
57         save->fs = get_fs();
58         LASSERT(ll_d_count(current->fs->pwd.dentry));
59         LASSERT(ll_d_count(new_ctx->pwd));
60         save->pwd = dget(current->fs->pwd.dentry);
61         save->pwdmnt = mntget(current->fs->pwd.mnt);
62         save->umask = current_umask();
63
64         LASSERT(save->pwd);
65         LASSERT(save->pwdmnt);
66         LASSERT(new_ctx->pwd);
67         LASSERT(new_ctx->pwdmnt);
68
69         current->fs->umask = 0; /* umask already applied on client */
70         set_fs(new_ctx->fs);
71         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
72 }
73 EXPORT_SYMBOL(push_ctxt);
74
75 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
76 {
77         /* if there is underlaying dt_device then pop_ctxt is not needed */
78         if (new_ctx->dt != NULL)
79                 return;
80
81         ASSERT_CTXT_MAGIC(saved->magic);
82         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
83
84         LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
85                  current->fs->pwd.dentry, new_ctx->pwd);
86         LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
87                  current->fs->pwd.mnt, new_ctx->pwdmnt);
88
89         set_fs(saved->fs);
90         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
91
92         dput(saved->pwd);
93         mntput(saved->pwdmnt);
94         current->fs->umask = saved->umask;
95 }
96 EXPORT_SYMBOL(pop_ctxt);
97
98 /* utility to rename a file */
99 int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
100                   char *oldname, char *newname)
101 {
102         struct dentry *dchild_old, *dchild_new;
103         int err = 0;
104         ENTRY;
105
106         ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
107         CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
108                (int)strlen(oldname), oldname, (int)strlen(newname), newname);
109
110         dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
111         if (IS_ERR(dchild_old))
112                 RETURN(PTR_ERR(dchild_old));
113
114         if (!dchild_old->d_inode)
115                 GOTO(put_old, err = -ENOENT);
116
117         dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
118         if (IS_ERR(dchild_new))
119                 GOTO(put_old, err = PTR_ERR(dchild_new));
120
121         err = ll_vfs_rename(dir->d_inode, dchild_old, dir->d_inode, dchild_new);
122
123         dput(dchild_new);
124 put_old:
125         dput(dchild_old);
126         RETURN(err);
127 }