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>
53 /* Debugging check only needed during development */
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()))
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)
64 /* push / pop to root of obd store */
65 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
68 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
69 ASSERT_CTXT_MAGIC(new_ctx->magic);
70 OBD_SET_CTXT_MAGIC(save);
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(¤t->fs->pwd->d_count),
77 atomic_read(¤t->fs->pwd->d_inode->i_count),
78 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
80 atomic_read(¤t->fs->pwdmnt->mnt_count));
84 LASSERT(atomic_read(¤t->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;
91 LASSERT(save->pwdmnt);
92 LASSERT(new_ctx->pwd);
93 LASSERT(new_ctx->pwdmnt);
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];
102 current->fsuid = uc->ouc_fsuid;
103 current->fsgid = uc->ouc_fsgid;
104 current->cap_effective = uc->ouc_cap;
105 current->ngroups = 0;
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;
113 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
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(¤t->fs->pwd->d_count),
120 atomic_read(¤t->fs->pwd->d_inode->i_count),
121 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
123 atomic_read(¤t->fs->pwdmnt->mnt_count));
126 EXPORT_SYMBOL(push_ctxt);
128 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
129 struct obd_ucred *uc)
132 ASSERT_CTXT_MAGIC(saved->magic);
134 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
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(¤t->fs->pwd->d_count),
141 atomic_read(¤t->fs->pwd->d_inode->i_count),
142 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
144 atomic_read(¤t->fs->pwdmnt->mnt_count));
147 LASSERT(current->fs->pwd == new_ctx->pwd);
148 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
151 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
154 mntput(saved->pwdmnt);
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;
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(¤t->fs->pwd->d_count),
169 atomic_read(¤t->fs->pwd->d_inode->i_count),
170 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
172 atomic_read(¤t->fs->pwdmnt->mnt_count));
175 EXPORT_SYMBOL(pop_ctxt);
177 /* utility to make a file */
178 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode)
180 struct dentry *dchild;
184 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
185 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
187 dchild = ll_lookup_one_len(name, dir, strlen(name));
189 GOTO(out_up, dchild);
191 if (dchild->d_inode) {
192 if (!S_ISREG(dchild->d_inode->i_mode))
193 GOTO(out_err, err = -EEXIST);
195 GOTO(out_up, dchild);
198 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
207 dchild = ERR_PTR(err);
211 EXPORT_SYMBOL(simple_mknod);
213 /* utility to make a directory */
214 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
216 struct dentry *dchild;
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));
224 GOTO(out_up, dchild);
226 if (dchild->d_inode) {
227 if (!S_ISDIR(dchild->d_inode->i_mode))
228 GOTO(out_err, err = -ENOTDIR);
230 GOTO(out_up, dchild);
233 err = vfs_mkdir(dir->d_inode, dchild, mode);
241 dchild = ERR_PTR(err);
245 EXPORT_SYMBOL(simple_mkdir);
248 * Read a file from within kernel context. Prior to calling this
249 * function we should already have done a push_ctxt().
251 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
253 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
254 if (!file || !file->f_op || !file->f_op->read || !off)
257 return file->f_op->read(file, buf, len, off);
259 EXPORT_SYMBOL(lustre_fread);
262 * Write a file from within kernel context. Prior to calling this
263 * function we should already have done a push_ctxt().
265 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
268 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
276 if (!file->f_op->write)
279 RETURN(file->f_op->write(file, buf, len, off));
281 EXPORT_SYMBOL(lustre_fwrite);
284 * Sync a file from within kernel context. Prior to calling this
285 * function we should already have done a push_ctxt().
287 int lustre_fsync(struct file *file)
290 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
291 if (!file || !file->f_op || !file->f_op->fsync)
294 RETURN(file->f_op->fsync(file, file->f_dentry, 0));
296 EXPORT_SYMBOL(lustre_fsync);
298 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
301 mntget(ctxt->pwdmnt);
302 return dentry_open(de, ctxt->pwdmnt, flags);
304 EXPORT_SYMBOL(l_dentry_open);
306 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
307 ino_t ino, unsigned int d_type)
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);
313 buf->error = -EINVAL;
314 if (reclen > buf->count)
316 dirent = buf->previous;
318 dirent->d_off = offset;
319 dirent = buf->current_dir;
320 buf->previous = dirent;
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;
330 long l_readdir(struct file * file, void * dirent, unsigned int count)
332 struct l_linux_dirent * lastdirent;
333 struct l_readdir_callback buf;
336 buf.current_dir = (struct l_linux_dirent *)dirent;
341 error = vfs_readdir(file, l_filldir, &buf);
345 lastdirent = buf.previous;
348 lastdirent->d_off = file->f_pos;
349 error = count - buf.count;
353 EXPORT_SYMBOL(l_readdir);
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)
368 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
369 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
370 MODULE_LICENSE("GPL");
372 module_init(lvfs_linux_init);
373 module_exit(lvfs_linux_exit);
377 #warning "lvfs_linux_init() and fsfilt_ext3_exit() aren't called on 2.6. MUST be fixed"