Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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 /* Debugging check only needed during development */
54 #ifdef OBD_CTXT_DEBUG
55 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
56 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERT(!segment_eq(get_fs(), get_ds()))
57 # define ASSERT_KERNEL_CTXT(msg) LASSERT(segment_eq(get_fs(), get_ds()))
58 #else
59 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
60 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
61 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
62 #endif
63
64 /* push / pop to root of obd store */
65 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
66                struct obd_ucred *uc)
67 {
68         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
69         ASSERT_CTXT_MAGIC(new_ctx->magic);
70         OBD_SET_CTXT_MAGIC(save);
71
72         /*
73         CDEBUG(D_INFO,
74                "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
75                save, current, current->fs, current->fs->pwd,
76                atomic_read(&current->fs->pwd->d_count),
77                atomic_read(&current->fs->pwd->d_inode->i_count),
78                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
79                current->fs->pwdmnt,
80                atomic_read(&current->fs->pwdmnt->mnt_count));
81         */
82
83         save->fs = get_fs();
84         LASSERT(atomic_read(&current->fs->pwd->d_count));
85         LASSERT(atomic_read(&new_ctx->pwd->d_count));
86         save->pwd = dget(current->fs->pwd);
87         save->pwdmnt = mntget(current->fs->pwdmnt);
88         save->ngroups = current->ngroups;
89
90         LASSERT(save->pwd);
91         LASSERT(save->pwdmnt);
92         LASSERT(new_ctx->pwd);
93         LASSERT(new_ctx->pwdmnt);
94
95         if (uc) {
96                 save->ouc.ouc_fsuid = current->fsuid;
97                 save->ouc.ouc_fsgid = current->fsgid;
98                 save->ouc.ouc_cap = current->cap_effective;
99                 save->ouc.ouc_suppgid1 = current->groups[0];
100                 save->ouc.ouc_suppgid2 = current->groups[1];
101
102                 current->fsuid = uc->ouc_fsuid;
103                 current->fsgid = uc->ouc_fsgid;
104                 current->cap_effective = uc->ouc_cap;
105                 current->ngroups = 0;
106
107                 if (uc->ouc_suppgid1 != -1)
108                         current->groups[current->ngroups++] = uc->ouc_suppgid1;
109                 if (uc->ouc_suppgid2 != -1)
110                         current->groups[current->ngroups++] = uc->ouc_suppgid2;
111         }
112         set_fs(new_ctx->fs);
113         set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
114
115         /*
116         CDEBUG(D_INFO,
117                "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
118                new_ctx, current, current->fs, current->fs->pwd,
119                atomic_read(&current->fs->pwd->d_count),
120                atomic_read(&current->fs->pwd->d_inode->i_count),
121                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
122                current->fs->pwdmnt,
123                atomic_read(&current->fs->pwdmnt->mnt_count));
124         */
125 }
126 EXPORT_SYMBOL(push_ctxt);
127
128 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
129               struct obd_ucred *uc)
130 {
131         //printk("pc0");
132         ASSERT_CTXT_MAGIC(saved->magic);
133         //printk("pc1");
134         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
135
136         /*
137         CDEBUG(D_INFO,
138                " = pop  %p==%p = cur %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
139                new_ctx, current, current->fs, current->fs->pwd,
140                atomic_read(&current->fs->pwd->d_count),
141                atomic_read(&current->fs->pwd->d_inode->i_count),
142                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
143                current->fs->pwdmnt,
144                atomic_read(&current->fs->pwdmnt->mnt_count));
145         */
146
147         LASSERT(current->fs->pwd == new_ctx->pwd);
148         LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
149
150         set_fs(saved->fs);
151         set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
152
153         dput(saved->pwd);
154         mntput(saved->pwdmnt);
155         if (uc) {
156                 current->fsuid = saved->ouc.ouc_fsuid;
157                 current->fsgid = saved->ouc.ouc_fsgid;
158                 current->cap_effective = saved->ouc.ouc_cap;
159                 current->ngroups = saved->ngroups;
160                 current->groups[0] = saved->ouc.ouc_suppgid1;
161                 current->groups[1] = saved->ouc.ouc_suppgid2;
162         }
163
164         /*
165         CDEBUG(D_INFO,
166                "= pop  %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
167                saved, current, current->fs, current->fs->pwd,
168                atomic_read(&current->fs->pwd->d_count),
169                atomic_read(&current->fs->pwd->d_inode->i_count),
170                current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
171                current->fs->pwdmnt,
172                atomic_read(&current->fs->pwdmnt->mnt_count));
173         */
174 }
175 EXPORT_SYMBOL(pop_ctxt);
176
177 /* utility to make a file */
178 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
179 {
180         struct dentry *dchild;
181         int err = 0;
182         ENTRY;
183
184         ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
185         CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
186
187         dchild = ll_lookup_one_len(name, dir, strlen(name));
188         if (IS_ERR(dchild))
189                 GOTO(out_up, dchild);
190
191         if (dchild->d_inode) {
192                 if (!S_ISREG(dchild->d_inode->i_mode))
193                         GOTO(out_err, err = -EEXIST);
194
195                 GOTO(out_up, dchild);
196         }
197
198         err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
199                             NULL);
200         if (err)
201                 GOTO(out_err, err);
202
203         RETURN(dchild);
204
205 out_err:
206         dput(dchild);
207         dchild = ERR_PTR(err);
208 out_up:
209         return dchild;
210 }
211 EXPORT_SYMBOL(simple_mknod);
212
213 /* utility to make a directory */
214 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
215 {
216         struct dentry *dchild;
217         int err = 0;
218         ENTRY;
219
220         ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
221         CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
222         dchild = ll_lookup_one_len(name, dir, strlen(name));
223         if (IS_ERR(dchild))
224                 GOTO(out_up, dchild);
225
226         if (dchild->d_inode) {
227                 if (!S_ISDIR(dchild->d_inode->i_mode))
228                         GOTO(out_err, err = -ENOTDIR);
229
230                 GOTO(out_up, dchild);
231         }
232
233         err = vfs_mkdir(dir->d_inode, dchild, mode);
234         if (err)
235                 GOTO(out_err, err);
236
237         RETURN(dchild);
238
239 out_err:
240         dput(dchild);
241         dchild = ERR_PTR(err);
242 out_up:
243         return dchild;
244 }
245 EXPORT_SYMBOL(simple_mkdir);
246
247 /*
248  * Read a file from within kernel context.  Prior to calling this
249  * function we should already have done a push_ctxt().
250  */
251 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
252 {
253         ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
254         if (!file || !file->f_op || !file->f_op->read || !off)
255                 RETURN(-ENOSYS);
256
257         return file->f_op->read(file, buf, len, off);
258 }
259 EXPORT_SYMBOL(lustre_fread);
260
261 /*
262  * Write a file from within kernel context.  Prior to calling this
263  * function we should already have done a push_ctxt().
264  */
265 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
266 {
267         ENTRY;
268         ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
269         if (!file)
270                 RETURN(-ENOENT);
271         if (!file->f_op)
272                 RETURN(-ENOSYS);
273         if (!off)
274                 RETURN(-EINVAL);
275
276         if (!file->f_op->write)
277                 RETURN(-EROFS);
278
279         RETURN(file->f_op->write(file, buf, len, off));
280 }
281 EXPORT_SYMBOL(lustre_fwrite);
282
283 /*
284  * Sync a file from within kernel context.  Prior to calling this
285  * function we should already have done a push_ctxt().
286  */
287 int lustre_fsync(struct file *file)
288 {
289         ENTRY;
290         ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
291         if (!file || !file->f_op || !file->f_op->fsync)
292                 RETURN(-ENOSYS);
293
294         RETURN(file->f_op->fsync(file, file->f_dentry, 0));
295 }
296 EXPORT_SYMBOL(lustre_fsync);
297
298 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
299                              int flags)
300 {
301         mntget(ctxt->pwdmnt);
302         return dentry_open(de, ctxt->pwdmnt, flags);
303 }
304 EXPORT_SYMBOL(l_dentry_open);
305
306 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
307                      ino_t ino, unsigned int d_type)
308 {
309         struct l_linux_dirent *dirent;
310         struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
311         int reclen = size_round(offsetof(struct l_linux_dirent, d_name) + namlen + 1);
312         
313         buf->error = -EINVAL;
314         if (reclen > buf->count)
315                 return -EINVAL;
316         dirent = buf->previous;
317         if (dirent)
318                dirent->d_off = offset; 
319         dirent = buf->current_dir;
320         buf->previous = dirent;
321         dirent->d_ino = ino;
322         dirent->d_reclen = reclen;
323         memcpy(dirent->d_name, name, namlen);
324         ((char *)dirent) += reclen;
325         buf->current_dir = dirent;
326         buf->count -= reclen; 
327         return 0;
328 }
329
330 long l_readdir(struct file * file, void * dirent, unsigned int count)
331 {
332         struct l_linux_dirent * lastdirent;
333         struct l_readdir_callback buf;
334         int error;
335
336         buf.current_dir = (struct l_linux_dirent *)dirent;
337         buf.previous = NULL;
338         buf.count = count;
339         buf.error = 0;
340
341         error = vfs_readdir(file, l_filldir, &buf);
342         if (error < 0)
343                 return error;
344         error = buf.error;
345         lastdirent = buf.previous;
346
347         if (lastdirent) {
348                 lastdirent->d_off = file->f_pos;
349                 error = count - buf.count;        
350         }
351         return error; 
352 }
353 EXPORT_SYMBOL(l_readdir);
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
365         return;
366 }
367
368 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
369 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
370 MODULE_LICENSE("GPL");
371
372 module_init(lvfs_linux_init);
373 module_exit(lvfs_linux_exit);
374
375 #else
376
377 #warning "lvfs_linux_init() and fsfilt_ext3_exit() aren't called on 2.6. MUST be fixed"
378
379
380 #endif