4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2014, 2017, Intel Corporation.
27 #define DEBUG_SUBSYSTEM S_FILTER
30 #include <linux/fs_struct.h>
31 #include <libcfs/libcfs.h>
33 #include <obd_class.h>
35 #include "ptlrpc_internal.h"
37 /* refine later and change to seqlock or simlar from libcfs */
38 /* Debugging check only needed during development */
40 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
41 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), KERNEL_DS),\
43 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), KERNEL_DS), msg)
45 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
46 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
47 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
50 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
51 struct dentry *dentry)
60 write_seqcount_begin(&fs->seq);
63 write_seqcount_end(&fs->seq);
64 spin_unlock(&fs->lock);
70 /* push / pop to root of obd store */
71 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
73 /* if there is underlaying dt_device then push_ctxt is not needed */
74 if (new_ctx->dt != NULL)
77 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
78 ASSERT_CTXT_MAGIC(new_ctx->magic);
79 OBD_SET_CTXT_MAGIC(save);
82 LASSERT(ll_d_count(current->fs->pwd.dentry));
83 LASSERT(ll_d_count(new_ctx->pwd));
84 save->pwd = dget(current->fs->pwd.dentry);
85 save->pwdmnt = mntget(current->fs->pwd.mnt);
86 save->umask = current_umask();
89 LASSERT(save->pwdmnt);
90 LASSERT(new_ctx->pwd);
91 LASSERT(new_ctx->pwdmnt);
93 current->fs->umask = 0; /* umask already applied on client */
95 ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
97 EXPORT_SYMBOL(push_ctxt);
99 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
101 /* if there is underlaying dt_device then pop_ctxt is not needed */
102 if (new_ctx->dt != NULL)
105 ASSERT_CTXT_MAGIC(saved->magic);
106 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
108 LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
109 current->fs->pwd.dentry, new_ctx->pwd);
110 LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
111 current->fs->pwd.mnt, new_ctx->pwdmnt);
114 ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
117 mntput(saved->pwdmnt);
118 current->fs->umask = saved->umask;
120 EXPORT_SYMBOL(pop_ctxt);