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