1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/lib/fsfilt_ext3.c
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
27 # define EXPORT_SYMTAB
30 #define DEBUG_SUBSYSTEM S_FILTER
32 #include <linux/version.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"
50 #include <linux/obd.h>
51 #include <linux/lustre_lib.h>
57 /* Debugging check only needed during development */
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()))
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)
68 /* push / pop to root of obd store */
69 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
72 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
73 ASSERT_CTXT_MAGIC(new_ctx->magic);
74 OBD_SET_CTXT_MAGIC(save);
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(¤t->fs->pwd->d_count),
81 atomic_read(¤t->fs->pwd->d_inode->i_count),
82 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
84 atomic_read(¤t->fs->pwdmnt->mnt_count));
88 LASSERT(atomic_read(¤t->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;
95 LASSERT(save->pwdmnt);
96 LASSERT(new_ctx->pwd);
97 LASSERT(new_ctx->pwdmnt);
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];
106 current->fsuid = uc->ouc_fsuid;
107 current->fsgid = uc->ouc_fsgid;
108 current->cap_effective = uc->ouc_cap;
109 current->ngroups = 0;
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;
117 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
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(¤t->fs->pwd->d_count),
124 atomic_read(¤t->fs->pwd->d_inode->i_count),
125 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
127 atomic_read(¤t->fs->pwdmnt->mnt_count));
130 EXPORT_SYMBOL(push_ctxt);
132 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
133 struct obd_ucred *uc)
136 ASSERT_CTXT_MAGIC(saved->magic);
138 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
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(¤t->fs->pwd->d_count),
145 atomic_read(¤t->fs->pwd->d_inode->i_count),
146 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
148 atomic_read(¤t->fs->pwdmnt->mnt_count));
151 LASSERT(current->fs->pwd == new_ctx->pwd);
152 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
155 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
158 mntput(saved->pwdmnt);
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;
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(¤t->fs->pwd->d_count),
173 atomic_read(¤t->fs->pwd->d_inode->i_count),
174 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
176 atomic_read(¤t->fs->pwdmnt->mnt_count));
179 EXPORT_SYMBOL(pop_ctxt);
181 /* utility to make a file */
182 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
184 struct dentry *dchild;
188 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
189 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
191 dchild = ll_lookup_one_len(name, dir, strlen(name));
193 GOTO(out_up, dchild);
195 if (dchild->d_inode) {
196 if (!S_ISREG(dchild->d_inode->i_mode))
197 GOTO(out_err, err = -EEXIST);
199 GOTO(out_up, dchild);
202 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
211 dchild = ERR_PTR(err);
215 EXPORT_SYMBOL(simple_mknod);
217 /* utility to make a directory */
218 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
220 struct dentry *dchild;
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));
228 GOTO(out_up, dchild);
230 if (dchild->d_inode) {
231 if (!S_ISDIR(dchild->d_inode->i_mode))
232 GOTO(out_err, err = -ENOTDIR);
234 GOTO(out_up, dchild);
237 err = vfs_mkdir(dir->d_inode, dchild, mode);
245 dchild = ERR_PTR(err);
249 EXPORT_SYMBOL(simple_mkdir);
252 * Read a file from within kernel context. Prior to calling this
253 * function we should already have done a push_ctxt().
255 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
257 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
258 if (!file || !file->f_op || !file->f_op->read || !off)
261 return file->f_op->read(file, buf, len, off);
263 EXPORT_SYMBOL(lustre_fread);
266 * Write a file from within kernel context. Prior to calling this
267 * function we should already have done a push_ctxt().
269 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
272 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
280 if (!file->f_op->write)
283 RETURN(file->f_op->write(file, buf, len, off));
285 EXPORT_SYMBOL(lustre_fwrite);
288 * Sync a file from within kernel context. Prior to calling this
289 * function we should already have done a push_ctxt().
291 int lustre_fsync(struct file *file)
294 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
295 if (!file || !file->f_op || !file->f_op->fsync)
298 RETURN(file->f_op->fsync(file, file->f_dentry, 0));
300 EXPORT_SYMBOL(lustre_fsync);
302 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
305 mntget(ctxt->pwdmnt);
306 return dentry_open(de, ctxt->pwdmnt, flags);
308 EXPORT_SYMBOL(l_dentry_open);
310 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
311 ino_t ino, unsigned int d_type)
313 struct l_linux_dirent *dirent;
314 struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
316 dirent = buf->lrc_dirent;
318 dirent->lld_off = offset;
320 OBD_ALLOC(dirent, sizeof(*dirent));
322 list_add_tail(&dirent->lld_list, buf->lrc_list);
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);
332 long l_readdir(struct file *file, struct list_head *dentry_list)
334 struct l_linux_dirent *lastdirent;
335 struct l_readdir_callback buf;
338 buf.lrc_dirent = NULL;
339 buf.lrc_list = dentry_list;
341 error = vfs_readdir(file, l_filldir, &buf);
345 lastdirent = buf.lrc_dirent;
347 lastdirent->lld_off = file->f_pos;
351 EXPORT_SYMBOL(l_readdir);
352 EXPORT_SYMBOL(obd_memory);
353 EXPORT_SYMBOL(obd_memmax);
355 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
357 static int __init lvfs_linux_init(void)
362 static void __exit lvfs_linux_exit(void)
367 leaked = atomic_read(&obd_memory);
368 CDEBUG(leaked ? D_ERROR : D_INFO,
369 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
374 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
375 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
376 MODULE_LICENSE("GPL");
378 module_init(lvfs_linux_init);
379 module_exit(lvfs_linux_exit);
383 #warning "lvfs_linux_init() and fsfilt_ext3_exit() aren't called on 2.6. MUST be fixed"