Whamcloud - gitweb
LU-14073 ptlrpc: remove debugging assert using segment_eq()
[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         save->fs = get_fs();
76         LASSERT(ll_d_count(current->fs->pwd.dentry));
77         LASSERT(ll_d_count(new_ctx->pwd));
78         save->pwd = dget(current->fs->pwd.dentry);
79         save->pwdmnt = mntget(current->fs->pwd.mnt);
80         save->umask = current_umask();
81
82         LASSERT(save->pwd);
83         LASSERT(save->pwdmnt);
84         LASSERT(new_ctx->pwd);
85         LASSERT(new_ctx->pwdmnt);
86
87         current->fs->umask = 0; /* umask already applied on client */
88         set_fs(new_ctx->fs);
89         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
90 }
91 EXPORT_SYMBOL(push_ctxt);
92
93 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
94 {
95         /* if there is underlaying dt_device then pop_ctxt is not needed */
96         if (new_ctx->dt != NULL)
97                 return;
98
99         ASSERT_CTXT_MAGIC(saved->magic);
100
101         LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
102                  current->fs->pwd.dentry, new_ctx->pwd);
103         LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
104                  current->fs->pwd.mnt, new_ctx->pwdmnt);
105
106         set_fs(saved->fs);
107         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
108
109         dput(saved->pwd);
110         mntput(saved->pwdmnt);
111         current->fs->umask = saved->umask;
112 }
113 EXPORT_SYMBOL(pop_ctxt);