Whamcloud - gitweb
a4f9340f245127b28f55f3ff70f11f619893e451
[fs/lustre-release.git] / lustre / lvfs / lvfs_linux.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  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lvfs/lvfs_linux.c
37  *
38  * Author: Andreas Dilger <adilger@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include <linux/version.h>
44 #include <linux/fs.h>
45 #include <asm/unistd.h>
46 #include <linux/slab.h>
47 #include <linux/pagemap.h>
48 #include <linux/quotaops.h>
49 #include <linux/version.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre_fsfilt.h>
52 #include <obd.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/lustre_compat25.h>
56 #include <lvfs.h>
57
58 #include <obd.h>
59 #include <lustre_lib.h>
60
61 struct lprocfs_stats *obd_memory = NULL;
62 EXPORT_SYMBOL(obd_memory);
63 /* refine later and change to seqlock or simlar from libcfs */
64
65 /* Debugging check only needed during development */
66 #ifdef OBD_CTXT_DEBUG
67 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
68 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), get_ds()),\
69                                               msg)
70 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), get_ds()), msg)
71 #else
72 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
73 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
74 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
75 #endif
76
77 static void push_group_info(struct lvfs_run_ctxt *save,
78                             struct group_info *ginfo)
79 {
80         if (!ginfo) {
81                 save->ngroups = current_ngroups;
82                 current_ngroups = 0;
83         } else {
84                 struct cred *cred;
85                 task_lock(current);
86                 save->group_info = current_cred()->group_info;
87                 if ((cred = prepare_creds())) {
88                         cred->group_info = ginfo;
89                         commit_creds(cred);
90                 }
91                 task_unlock(current);
92         }
93 }
94
95 static void pop_group_info(struct lvfs_run_ctxt *save,
96                            struct group_info *ginfo)
97 {
98         if (!ginfo) {
99                 current_ngroups = save->ngroups;
100         } else {
101                 struct cred *cred;
102                 task_lock(current);
103                 if ((cred = prepare_creds())) {
104                         cred->group_info = save->group_info;
105                         commit_creds(cred);
106                 }
107                 task_unlock(current);
108         }
109 }
110
111 /* push / pop to root of obd store */
112 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx,
113                struct lvfs_ucred *uc)
114 {
115         /* if there is underlaying dt_device then push_ctxt is not needed */
116         if (new_ctx->dt != NULL)
117                 return;
118
119         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
120         ASSERT_CTXT_MAGIC(new_ctx->magic);
121         OBD_SET_CTXT_MAGIC(save);
122
123         save->fs = get_fs();
124         LASSERT(d_refcount(cfs_fs_pwd(current->fs)));
125         LASSERT(d_refcount(new_ctx->pwd));
126         save->pwd = dget(cfs_fs_pwd(current->fs));
127         save->pwdmnt = mntget(cfs_fs_mnt(current->fs));
128         save->luc.luc_umask = cfs_curproc_umask();
129         save->ngroups = current_cred()->group_info->ngroups;
130
131         LASSERT(save->pwd);
132         LASSERT(save->pwdmnt);
133         LASSERT(new_ctx->pwd);
134         LASSERT(new_ctx->pwdmnt);
135
136         if (uc) {
137                 struct cred *cred;
138                 save->luc.luc_uid = current_uid();
139                 save->luc.luc_gid = current_gid();
140                 save->luc.luc_fsuid = current_fsuid();
141                 save->luc.luc_fsgid = current_fsgid();
142                 save->luc.luc_cap = current_cap();
143
144                 if ((cred = prepare_creds())) {
145                         cred->uid = uc->luc_uid;
146                         cred->gid = uc->luc_gid;
147                         cred->fsuid = uc->luc_fsuid;
148                         cred->fsgid = uc->luc_fsgid;
149                         cred->cap_effective = uc->luc_cap;
150                         commit_creds(cred);
151                 }
152
153                 push_group_info(save,
154                                 uc->luc_ginfo ?:
155                                 uc->luc_identity ? uc->luc_identity->mi_ginfo :
156                                                    NULL);
157         }
158         current->fs->umask = 0; /* umask already applied on client */
159         set_fs(new_ctx->fs);
160         ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
161 }
162 EXPORT_SYMBOL(push_ctxt);
163
164 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx,
165               struct lvfs_ucred *uc)
166 {
167         /* if there is underlaying dt_device then pop_ctxt is not needed */
168         if (new_ctx->dt != NULL)
169                 return;
170
171         ASSERT_CTXT_MAGIC(saved->magic);
172         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
173
174         LASSERTF(cfs_fs_pwd(current->fs) == new_ctx->pwd, "%p != %p\n",
175                  cfs_fs_pwd(current->fs), new_ctx->pwd);
176         LASSERTF(cfs_fs_mnt(current->fs) == new_ctx->pwdmnt, "%p != %p\n",
177                  cfs_fs_mnt(current->fs), new_ctx->pwdmnt);
178
179         set_fs(saved->fs);
180         ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
181
182         dput(saved->pwd);
183         mntput(saved->pwdmnt);
184         current->fs->umask = saved->luc.luc_umask;
185         if (uc) {
186                 struct cred *cred;
187                 if ((cred = prepare_creds())) {
188                         cred->uid = saved->luc.luc_uid;
189                         cred->gid = saved->luc.luc_gid;
190                         cred->fsuid = saved->luc.luc_fsuid;
191                         cred->fsgid = saved->luc.luc_fsgid;
192                         cred->cap_effective = saved->luc.luc_cap;
193                         commit_creds(cred);
194                 }
195
196                 pop_group_info(saved,
197                                uc->luc_ginfo ?:
198                                uc->luc_identity ? uc->luc_identity->mi_ginfo :
199                                                   NULL);
200         }
201 }
202 EXPORT_SYMBOL(pop_ctxt);
203
204 /* utility to rename a file */
205 int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
206                   char *oldname, char *newname)
207 {
208         struct dentry *dchild_old, *dchild_new;
209         int err = 0;
210         ENTRY;
211
212         ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
213         CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
214                (int)strlen(oldname), oldname, (int)strlen(newname), newname);
215
216         dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
217         if (IS_ERR(dchild_old))
218                 RETURN(PTR_ERR(dchild_old));
219
220         if (!dchild_old->d_inode)
221                 GOTO(put_old, err = -ENOENT);
222
223         dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
224         if (IS_ERR(dchild_new))
225                 GOTO(put_old, err = PTR_ERR(dchild_new));
226
227         err = ll_vfs_rename(dir->d_inode, dchild_old, mnt,
228                             dir->d_inode, dchild_new, mnt);
229
230         dput(dchild_new);
231 put_old:
232         dput(dchild_old);
233         RETURN(err);
234 }
235 EXPORT_SYMBOL(lustre_rename);
236
237 /* Note: dput(dchild) will be called if there is an error */
238 struct l_file *l_dentry_open(struct lvfs_run_ctxt *ctxt, struct l_dentry *de,
239                              int flags)
240 {
241         mntget(ctxt->pwdmnt);
242         return ll_dentry_open(de, ctxt->pwdmnt, flags, current_cred());
243 }
244 EXPORT_SYMBOL(l_dentry_open);
245
246 #ifdef LPROCFS
247 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
248                           struct lprocfs_counter_header *header,
249                           enum lprocfs_stats_flags flags,
250                           enum lprocfs_fields_flags field)
251 {
252         __s64 ret = 0;
253
254         if (lc == NULL || header == NULL)
255                 RETURN(0);
256
257         switch (field) {
258                 case LPROCFS_FIELDS_FLAGS_CONFIG:
259                         ret = header->lc_config;
260                         break;
261                 case LPROCFS_FIELDS_FLAGS_SUM:
262                         ret = lc->lc_sum;
263                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
264                                 ret += lc->lc_sum_irq;
265                         break;
266                 case LPROCFS_FIELDS_FLAGS_MIN:
267                         ret = lc->lc_min;
268                         break;
269                 case LPROCFS_FIELDS_FLAGS_MAX:
270                         ret = lc->lc_max;
271                         break;
272                 case LPROCFS_FIELDS_FLAGS_AVG:
273                         ret = (lc->lc_max - lc->lc_min) / 2;
274                         break;
275                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
276                         ret = lc->lc_sumsquare;
277                         break;
278                 case LPROCFS_FIELDS_FLAGS_COUNT:
279                         ret = lc->lc_count;
280                         break;
281                 default:
282                         break;
283         };
284
285         RETURN(ret);
286 }
287 EXPORT_SYMBOL(lprocfs_read_helper);
288 #endif /* LPROCFS */
289
290 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
291 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
292 MODULE_LICENSE("GPL");