Whamcloud - gitweb
Landing b_recovery
[fs/lustre-release.git] / lustre / lvfs / lvfs_linux.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_ext3.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #ifndef EXPORT_SYMTAB
27 # define EXPORT_SYMTAB
28 #endif
29
30 #define DEBUG_SUBSYSTEM S_FILTER
31
32 #include <linux/version.h>
33 #include <linux/fs.h>
34 #include <asm/unistd.h>
35 #include <linux/jbd.h>
36 #include <linux/slab.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/version.h>
40 #include <linux/kp30.h>
41 #include <linux/lustre_fsfilt.h>
42 #include <linux/obd.h>
43 #include <linux/obd_class.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/lustre_compat25.h>
47 #include <linux/lvfs.h>
48 #include "lvfs_internal.h"
49
50 #include <linux/obd.h>
51 #include <linux/lustre_lib.h>
52
53 atomic_t obd_memory;
54 int obd_memmax;
55
56
57 /* Debugging check only needed during development */
58 #ifdef OBD_CTXT_DEBUG
59 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
60 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERT(!segment_eq(get_fs(), get_ds()))
61 # define ASSERT_KERNEL_CTXT(msg) LASSERT(segment_eq(get_fs(), get_ds()))
62 #else
63 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
64 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
65 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
66 #endif
67
68 /* push / pop to root of obd store */
69 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
70                struct obd_ucred *uc)
71 {
72         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
73         ASSERT_CTXT_MAGIC(new_ctx->magic);
74         OBD_SET_CTXT_MAGIC(save);
75
76         /*
77         CDEBUG(D_INFO,
78                "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
79                save, current, current->fs, current->fs->pwd,
80                atomic_read(&current->fs->pwd->d_count),
81                atomic_read(&current->fs->pwd->d_inode->i_count),
82                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
83                current->fs->pwdmnt,
84                atomic_read(&current->fs->pwdmnt->mnt_count));
85         */
86
87         save->fs = get_fs();
88         LASSERT(atomic_read(&current->fs->pwd->d_count));
89         LASSERT(atomic_read(&new_ctx->pwd->d_count));
90         save->pwd = dget(current->fs->pwd);
91         save->pwdmnt = mntget(current->fs->pwdmnt);
92         save->ngroups = current->ngroups;
93
94         LASSERT(save->pwd);
95         LASSERT(save->pwdmnt);
96         LASSERT(new_ctx->pwd);
97         LASSERT(new_ctx->pwdmnt);
98
99         if (uc) {
100                 save->ouc.ouc_fsuid = current->fsuid;
101                 save->ouc.ouc_fsgid = current->fsgid;
102                 save->ouc.ouc_cap = current->cap_effective;
103                 save->ouc.ouc_suppgid1 = current->groups[0];
104                 save->ouc.ouc_suppgid2 = current->groups[1];
105
106                 current->fsuid = uc->ouc_fsuid;
107                 current->fsgid = uc->ouc_fsgid;
108                 current->cap_effective = uc->ouc_cap;
109                 current->ngroups = 0;
110
111                 if (uc->ouc_suppgid1 != -1)
112                         current->groups[current->ngroups++] = uc->ouc_suppgid1;
113                 if (uc->ouc_suppgid2 != -1)
114                         current->groups[current->ngroups++] = uc->ouc_suppgid2;
115         }
116         set_fs(new_ctx->fs);
117         set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
118
119         /*
120         CDEBUG(D_INFO,
121                "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
122                new_ctx, current, current->fs, current->fs->pwd,
123                atomic_read(&current->fs->pwd->d_count),
124                atomic_read(&current->fs->pwd->d_inode->i_count),
125                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
126                current->fs->pwdmnt,
127                atomic_read(&current->fs->pwdmnt->mnt_count));
128         */
129 }
130 EXPORT_SYMBOL(push_ctxt);
131
132 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
133               struct obd_ucred *uc)
134 {
135         //printk("pc0");
136         ASSERT_CTXT_MAGIC(saved->magic);
137         //printk("pc1");
138         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
139
140         /*
141         CDEBUG(D_INFO,
142                " = pop  %p==%p = cur %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
143                new_ctx, current, current->fs, current->fs->pwd,
144                atomic_read(&current->fs->pwd->d_count),
145                atomic_read(&current->fs->pwd->d_inode->i_count),
146                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
147                current->fs->pwdmnt,
148                atomic_read(&current->fs->pwdmnt->mnt_count));
149         */
150
151         LASSERT(current->fs->pwd == new_ctx->pwd);
152         LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
153
154         set_fs(saved->fs);
155         set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
156
157         dput(saved->pwd);
158         mntput(saved->pwdmnt);
159         if (uc) {
160                 current->fsuid = saved->ouc.ouc_fsuid;
161                 current->fsgid = saved->ouc.ouc_fsgid;
162                 current->cap_effective = saved->ouc.ouc_cap;
163                 current->ngroups = saved->ngroups;
164                 current->groups[0] = saved->ouc.ouc_suppgid1;
165                 current->groups[1] = saved->ouc.ouc_suppgid2;
166         }
167
168         /*
169         CDEBUG(D_INFO,
170                "= pop  %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
171                saved, current, current->fs, current->fs->pwd,
172                atomic_read(&current->fs->pwd->d_count),
173                atomic_read(&current->fs->pwd->d_inode->i_count),
174                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
175                current->fs->pwdmnt,
176                atomic_read(&current->fs->pwdmnt->mnt_count));
177         */
178 }
179 EXPORT_SYMBOL(pop_ctxt);
180
181 /* utility to make a file */
182 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
183 {
184         struct dentry *dchild;
185         int err = 0;
186         ENTRY;
187
188         ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
189         CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
190
191         dchild = ll_lookup_one_len(name, dir, strlen(name));
192         if (IS_ERR(dchild))
193                 GOTO(out_up, dchild);
194
195         if (dchild->d_inode) {
196                 if (!S_ISREG(dchild->d_inode->i_mode))
197                         GOTO(out_err, err = -EEXIST);
198
199                 GOTO(out_up, dchild);
200         }
201
202         err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
203                             NULL);
204         if (err)
205                 GOTO(out_err, err);
206
207         RETURN(dchild);
208
209 out_err:
210         dput(dchild);
211         dchild = ERR_PTR(err);
212 out_up:
213         return dchild;
214 }
215 EXPORT_SYMBOL(simple_mknod);
216
217 /* utility to make a directory */
218 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
219 {
220         struct dentry *dchild;
221         int err = 0;
222         ENTRY;
223
224         ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
225         CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
226         dchild = ll_lookup_one_len(name, dir, strlen(name));
227         if (IS_ERR(dchild))
228                 GOTO(out_up, dchild);
229
230         if (dchild->d_inode) {
231                 if (!S_ISDIR(dchild->d_inode->i_mode))
232                         GOTO(out_err, err = -ENOTDIR);
233
234                 GOTO(out_up, dchild);
235         }
236
237         err = vfs_mkdir(dir->d_inode, dchild, mode);
238         if (err)
239                 GOTO(out_err, err);
240
241         RETURN(dchild);
242
243 out_err:
244         dput(dchild);
245         dchild = ERR_PTR(err);
246 out_up:
247         return dchild;
248 }
249 EXPORT_SYMBOL(simple_mkdir);
250
251 /*
252  * Read a file from within kernel context.  Prior to calling this
253  * function we should already have done a push_ctxt().
254  */
255 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
256 {
257         ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
258         if (!file || !file->f_op || !file->f_op->read || !off)
259                 RETURN(-ENOSYS);
260
261         return file->f_op->read(file, buf, len, off);
262 }
263 EXPORT_SYMBOL(lustre_fread);
264
265 /*
266  * Write a file from within kernel context.  Prior to calling this
267  * function we should already have done a push_ctxt().
268  */
269 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
270 {
271         ENTRY;
272         ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
273         if (!file)
274                 RETURN(-ENOENT);
275         if (!file->f_op)
276                 RETURN(-ENOSYS);
277         if (!off)
278                 RETURN(-EINVAL);
279
280         if (!file->f_op->write)
281                 RETURN(-EROFS);
282
283         RETURN(file->f_op->write(file, buf, len, off));
284 }
285 EXPORT_SYMBOL(lustre_fwrite);
286
287 /*
288  * Sync a file from within kernel context.  Prior to calling this
289  * function we should already have done a push_ctxt().
290  */
291 int lustre_fsync(struct file *file)
292 {
293         ENTRY;
294         ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
295         if (!file || !file->f_op || !file->f_op->fsync)
296                 RETURN(-ENOSYS);
297
298         RETURN(file->f_op->fsync(file, file->f_dentry, 0));
299 }
300 EXPORT_SYMBOL(lustre_fsync);
301
302 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
303                              int flags)
304 {
305         mntget(ctxt->pwdmnt);
306         return dentry_open(de, ctxt->pwdmnt, flags);
307 }
308 EXPORT_SYMBOL(l_dentry_open);
309
310 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
311                      ino_t ino, unsigned int d_type)
312 {
313         struct l_linux_dirent *dirent;
314         struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
315         
316         dirent = buf->lrc_dirent;
317         if (dirent)
318                dirent->lld_off = offset; 
319
320         OBD_ALLOC(dirent, sizeof(*dirent));
321
322         list_add_tail(&dirent->lld_list, buf->lrc_list);
323
324         buf->lrc_dirent = dirent;
325         dirent->lld_ino = ino;
326         LASSERT(sizeof(dirent->lld_name) >= namlen + 1);
327         memcpy(dirent->lld_name, name, namlen);
328
329         return 0;
330 }
331
332 long l_readdir(struct file *file, struct list_head *dentry_list)
333 {
334         struct l_linux_dirent *lastdirent;
335         struct l_readdir_callback buf;
336         int error;
337
338         buf.lrc_dirent = NULL;
339         buf.lrc_list = dentry_list; 
340
341         error = vfs_readdir(file, l_filldir, &buf);
342         if (error < 0)
343                 return error;
344
345         lastdirent = buf.lrc_dirent;
346         if (lastdirent)
347                 lastdirent->lld_off = file->f_pos;
348
349         return 0; 
350 }
351 EXPORT_SYMBOL(l_readdir);
352 EXPORT_SYMBOL(obd_memory);
353 EXPORT_SYMBOL(obd_memmax);
354
355 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
356
357 static int __init lvfs_linux_init(void)
358 {
359         RETURN(0);
360 }
361
362 static void __exit lvfs_linux_exit(void)
363 {
364         int leaked;
365         ENTRY;
366
367         leaked = atomic_read(&obd_memory);
368         CDEBUG(leaked ? D_ERROR : D_INFO,
369                "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
370
371         return;
372 }
373
374 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
375 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
376 MODULE_LICENSE("GPL");
377
378 module_init(lvfs_linux_init);
379 module_exit(lvfs_linux_exit);
380
381 #else
382
383 #warning "lvfs_linux_init() and fsfilt_ext3_exit() aren't called on 2.6. MUST be fixed"
384
385
386 #endif