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