Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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  * Copyright (c) 2014, 2017, Intel Corporation.
25  */
26
27 #define DEBUG_SUBSYSTEM S_FILTER
28
29 #include <linux/fs.h>
30 #include <linux/fs_struct.h>
31 #include <libcfs/libcfs.h>
32 #include <lvfs.h>
33 #include <obd_class.h>
34
35 #include "ptlrpc_internal.h"
36
37 /* refine later and change to seqlock or simlar from libcfs */
38 /* Debugging check only needed during development */
39 #ifdef OBD_CTXT_DEBUG
40 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
41 #else
42 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
43 #endif
44
45 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
46                                  struct dentry *dentry)
47 {
48         struct path path;
49         struct path old_pwd;
50
51         path.mnt = mnt;
52         path.dentry = dentry;
53         path_get(&path);
54         spin_lock(&fs->lock);
55         write_seqcount_begin(&fs->seq);
56         old_pwd = fs->pwd;
57         fs->pwd = path;
58         write_seqcount_end(&fs->seq);
59         spin_unlock(&fs->lock);
60
61         if (old_pwd.dentry)
62                 path_put(&old_pwd);
63 }
64
65 /* push / pop to root of obd store */
66 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
67 {
68         /* if there is underlaying dt_device then push_ctxt is not needed */
69         if (new_ctx->dt != NULL)
70                 return;
71
72         ASSERT_CTXT_MAGIC(new_ctx->magic);
73         OBD_SET_CTXT_MAGIC(save);
74
75         LASSERT(ll_d_count(current->fs->pwd.dentry));
76         LASSERT(ll_d_count(new_ctx->pwd));
77         save->pwd = dget(current->fs->pwd.dentry);
78         save->pwdmnt = mntget(current->fs->pwd.mnt);
79         save->umask = current_umask();
80
81         LASSERT(save->pwd);
82         LASSERT(save->pwdmnt);
83         LASSERT(new_ctx->pwd);
84         LASSERT(new_ctx->pwdmnt);
85
86         current->fs->umask = 0; /* umask already applied on client */
87         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
88 }
89 EXPORT_SYMBOL(push_ctxt);
90
91 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
92 {
93         /* if there is underlaying dt_device then pop_ctxt is not needed */
94         if (new_ctx->dt != NULL)
95                 return;
96
97         ASSERT_CTXT_MAGIC(saved->magic);
98
99         LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%px != %px\n",
100                  current->fs->pwd.dentry, new_ctx->pwd);
101         LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%px != %px\n",
102                  current->fs->pwd.mnt, new_ctx->pwdmnt);
103
104         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
105
106         dput(saved->pwd);
107         mntput(saved->pwdmnt);
108         current->fs->umask = saved->umask;
109 }
110 EXPORT_SYMBOL(pop_ctxt);