Whamcloud - gitweb
LU-10467 lustre: convert most users of LWI_TIMEOUT_INTERVAL()
[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 <libcfs/libcfs.h>
31 #include <lvfs.h>
32 #include <obd_class.h>
33
34 #include "ptlrpc_internal.h"
35
36 /* refine later and change to seqlock or simlar from libcfs */
37 /* Debugging check only needed during development */
38 #ifdef OBD_CTXT_DEBUG
39 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
40 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), KERNEL_DS),\
41                                               msg)
42 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), KERNEL_DS), msg)
43 #else
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)
47 #endif
48
49 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
50                                  struct dentry *dentry)
51 {
52         struct path path;
53         struct path old_pwd;
54
55         path.mnt = mnt;
56         path.dentry = dentry;
57         path_get(&path);
58         spin_lock(&fs->lock);
59         write_seqcount_begin(&fs->seq);
60         old_pwd = fs->pwd;
61         fs->pwd = path;
62         write_seqcount_end(&fs->seq);
63         spin_unlock(&fs->lock);
64
65         if (old_pwd.dentry)
66                 path_put(&old_pwd);
67 }
68
69 /* push / pop to root of obd store */
70 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
71 {
72         /* if there is underlaying dt_device then push_ctxt is not needed */
73         if (new_ctx->dt != NULL)
74                 return;
75
76         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
77         ASSERT_CTXT_MAGIC(new_ctx->magic);
78         OBD_SET_CTXT_MAGIC(save);
79
80         save->fs = get_fs();
81         LASSERT(ll_d_count(current->fs->pwd.dentry));
82         LASSERT(ll_d_count(new_ctx->pwd));
83         save->pwd = dget(current->fs->pwd.dentry);
84         save->pwdmnt = mntget(current->fs->pwd.mnt);
85         save->umask = current_umask();
86
87         LASSERT(save->pwd);
88         LASSERT(save->pwdmnt);
89         LASSERT(new_ctx->pwd);
90         LASSERT(new_ctx->pwdmnt);
91
92         current->fs->umask = 0; /* umask already applied on client */
93         set_fs(new_ctx->fs);
94         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
95 }
96 EXPORT_SYMBOL(push_ctxt);
97
98 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
99 {
100         /* if there is underlaying dt_device then pop_ctxt is not needed */
101         if (new_ctx->dt != NULL)
102                 return;
103
104         ASSERT_CTXT_MAGIC(saved->magic);
105         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
106
107         LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
108                  current->fs->pwd.dentry, new_ctx->pwd);
109         LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
110                  current->fs->pwd.mnt, new_ctx->pwdmnt);
111
112         set_fs(saved->fs);
113         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
114
115         dput(saved->pwd);
116         mntput(saved->pwdmnt);
117         current->fs->umask = saved->umask;
118 }
119 EXPORT_SYMBOL(pop_ctxt);