Whamcloud - gitweb
LU-5710 all: second batch of corrected typos and grammar errors
[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, 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(), get_ds()),\
41                                               msg)
42 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), get_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 /* push / pop to root of obd store */
50 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
51 {
52         /* if there is underlaying dt_device then push_ctxt is not needed */
53         if (new_ctx->dt != NULL)
54                 return;
55
56         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
57         ASSERT_CTXT_MAGIC(new_ctx->magic);
58         OBD_SET_CTXT_MAGIC(save);
59
60         save->fs = get_fs();
61         LASSERT(ll_d_count(current->fs->pwd.dentry));
62         LASSERT(ll_d_count(new_ctx->pwd));
63         save->pwd = dget(current->fs->pwd.dentry);
64         save->pwdmnt = mntget(current->fs->pwd.mnt);
65         save->umask = current_umask();
66
67         LASSERT(save->pwd);
68         LASSERT(save->pwdmnt);
69         LASSERT(new_ctx->pwd);
70         LASSERT(new_ctx->pwdmnt);
71
72         current->fs->umask = 0; /* umask already applied on client */
73         set_fs(new_ctx->fs);
74         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
75 }
76 EXPORT_SYMBOL(push_ctxt);
77
78 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
79 {
80         /* if there is underlaying dt_device then pop_ctxt is not needed */
81         if (new_ctx->dt != NULL)
82                 return;
83
84         ASSERT_CTXT_MAGIC(saved->magic);
85         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
86
87         LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
88                  current->fs->pwd.dentry, new_ctx->pwd);
89         LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
90                  current->fs->pwd.mnt, new_ctx->pwdmnt);
91
92         set_fs(saved->fs);
93         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
94
95         dput(saved->pwd);
96         mntput(saved->pwdmnt);
97         current->fs->umask = saved->umask;
98 }
99 EXPORT_SYMBOL(pop_ctxt);
100
101 /* utility to rename a file */
102 int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
103                   char *oldname, char *newname)
104 {
105         struct dentry *dchild_old, *dchild_new;
106         int err = 0;
107         ENTRY;
108
109         ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
110         CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
111                (int)strlen(oldname), oldname, (int)strlen(newname), newname);
112
113         dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
114         if (IS_ERR(dchild_old))
115                 RETURN(PTR_ERR(dchild_old));
116
117         if (!dchild_old->d_inode)
118                 GOTO(put_old, err = -ENOENT);
119
120         dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
121         if (IS_ERR(dchild_new))
122                 GOTO(put_old, err = PTR_ERR(dchild_new));
123
124         err = ll_vfs_rename(dir->d_inode, dchild_old, dir->d_inode, dchild_new);
125
126         dput(dchild_new);
127 put_old:
128         dput(dchild_old);
129         RETURN(err);
130 }