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