Whamcloud - gitweb
Land b_smallfix onto HEAD (20040223_1817)
[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                 int old_mode = dchild->d_inode->i_mode;
197                 if (!S_ISREG(old_mode))
198                         GOTO(out_err, err = -EEXIST);
199
200                 /* Fixup file permissions if necessary */
201                 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
202                         CWARN("fixing permissions on %s from %o to %o\n",
203                               name, old_mode, mode);
204                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
205                                                   (old_mode & ~S_IALLUGO);
206                         mark_inode_dirty(dchild->d_inode);
207                 }
208                 GOTO(out_up, dchild);
209         }
210
211         err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
212                             NULL);
213         if (err)
214                 GOTO(out_err, err);
215
216         RETURN(dchild);
217
218 out_err:
219         dput(dchild);
220         dchild = ERR_PTR(err);
221 out_up:
222         return dchild;
223 }
224 EXPORT_SYMBOL(simple_mknod);
225
226 /* utility to make a directory */
227 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
228 {
229         struct dentry *dchild;
230         int err = 0;
231         ENTRY;
232
233         ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
234         CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
235         dchild = ll_lookup_one_len(name, dir, strlen(name));
236         if (IS_ERR(dchild))
237                 GOTO(out_up, dchild);
238
239         if (dchild->d_inode) {
240                 int old_mode = dchild->d_inode->i_mode;
241                 if (!S_ISDIR(old_mode))
242                         GOTO(out_err, err = -ENOTDIR);
243
244                 /* Fixup directory permissions if necessary */
245                 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
246                         CWARN("fixing permissions on %s from %o to %o\n",
247                               name, old_mode, mode);
248                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
249                                                   (old_mode & ~S_IALLUGO);
250                         mark_inode_dirty(dchild->d_inode);
251                 }
252                 GOTO(out_up, dchild);
253         }
254
255         err = vfs_mkdir(dir->d_inode, dchild, mode);
256         if (err)
257                 GOTO(out_err, err);
258
259         RETURN(dchild);
260
261 out_err:
262         dput(dchild);
263         dchild = ERR_PTR(err);
264 out_up:
265         return dchild;
266 }
267 EXPORT_SYMBOL(simple_mkdir);
268
269 /*
270  * Read a file from within kernel context.  Prior to calling this
271  * function we should already have done a push_ctxt().
272  */
273 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
274 {
275         ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
276         if (!file || !file->f_op || !file->f_op->read || !off)
277                 RETURN(-ENOSYS);
278
279         return file->f_op->read(file, buf, len, off);
280 }
281 EXPORT_SYMBOL(lustre_fread);
282
283 /*
284  * Write a file from within kernel context.  Prior to calling this
285  * function we should already have done a push_ctxt().
286  */
287 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
288 {
289         ENTRY;
290         ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
291         if (!file)
292                 RETURN(-ENOENT);
293         if (!file->f_op)
294                 RETURN(-ENOSYS);
295         if (!off)
296                 RETURN(-EINVAL);
297
298         if (!file->f_op->write)
299                 RETURN(-EROFS);
300
301         RETURN(file->f_op->write(file, buf, len, off));
302 }
303 EXPORT_SYMBOL(lustre_fwrite);
304
305 /*
306  * Sync a file from within kernel context.  Prior to calling this
307  * function we should already have done a push_ctxt().
308  */
309 int lustre_fsync(struct file *file)
310 {
311         ENTRY;
312         ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
313         if (!file || !file->f_op || !file->f_op->fsync)
314                 RETURN(-ENOSYS);
315
316         RETURN(file->f_op->fsync(file, file->f_dentry, 0));
317 }
318 EXPORT_SYMBOL(lustre_fsync);
319
320 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
321                              int flags)
322 {
323         mntget(ctxt->pwdmnt);
324         return dentry_open(de, ctxt->pwdmnt, flags);
325 }
326 EXPORT_SYMBOL(l_dentry_open);
327
328 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
329                      ino_t ino, unsigned int d_type)
330 {
331         struct l_linux_dirent *dirent;
332         struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
333         
334         dirent = buf->lrc_dirent;
335         if (dirent)
336                dirent->lld_off = offset; 
337
338         OBD_ALLOC(dirent, sizeof(*dirent));
339
340         list_add_tail(&dirent->lld_list, buf->lrc_list);
341
342         buf->lrc_dirent = dirent;
343         dirent->lld_ino = ino;
344         LASSERT(sizeof(dirent->lld_name) >= namlen + 1);
345         memcpy(dirent->lld_name, name, namlen);
346
347         return 0;
348 }
349
350 long l_readdir(struct file *file, struct list_head *dentry_list)
351 {
352         struct l_linux_dirent *lastdirent;
353         struct l_readdir_callback buf;
354         int error;
355
356         buf.lrc_dirent = NULL;
357         buf.lrc_list = dentry_list; 
358
359         error = vfs_readdir(file, l_filldir, &buf);
360         if (error < 0)
361                 return error;
362
363         lastdirent = buf.lrc_dirent;
364         if (lastdirent)
365                 lastdirent->lld_off = file->f_pos;
366
367         return 0; 
368 }
369 EXPORT_SYMBOL(l_readdir);
370 EXPORT_SYMBOL(obd_memory);
371 EXPORT_SYMBOL(obd_memmax);
372
373 static int __init lvfs_linux_init(void)
374 {
375         RETURN(0);
376 }
377
378 static void __exit lvfs_linux_exit(void)
379 {
380         int leaked;
381         ENTRY;
382
383         leaked = atomic_read(&obd_memory);
384         CDEBUG(leaked ? D_ERROR : D_INFO,
385                "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
386
387         return;
388 }
389
390 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
391 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
392 MODULE_LICENSE("GPL");
393
394 module_init(lvfs_linux_init);
395 module_exit(lvfs_linux_exit);