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