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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
69 #define current_ngroups current->group_info->ngroups
70 #define current_groups current->group_info->small_block
72 #define current_ngroups current->ngroups
73 #define current_groups current->groups
76 /* push / pop to root of obd store */
77 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
80 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
81 ASSERT_CTXT_MAGIC(new_ctx->magic);
82 OBD_SET_CTXT_MAGIC(save);
86 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
87 save, current, current->fs, current->fs->pwd,
88 atomic_read(¤t->fs->pwd->d_count),
89 atomic_read(¤t->fs->pwd->d_inode->i_count),
90 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
92 atomic_read(¤t->fs->pwdmnt->mnt_count));
96 LASSERT(atomic_read(¤t->fs->pwd->d_count));
97 LASSERT(atomic_read(&new_ctx->pwd->d_count));
98 save->pwd = dget(current->fs->pwd);
99 save->pwdmnt = mntget(current->fs->pwdmnt);
100 save->ngroups = current_ngroups;
103 LASSERT(save->pwdmnt);
104 LASSERT(new_ctx->pwd);
105 LASSERT(new_ctx->pwdmnt);
108 save->ouc.ouc_fsuid = current->fsuid;
109 save->ouc.ouc_fsgid = current->fsgid;
110 save->ouc.ouc_cap = current->cap_effective;
111 save->ouc.ouc_suppgid1 = current_groups[0];
112 save->ouc.ouc_suppgid2 = current_groups[1];
114 current->fsuid = uc->ouc_fsuid;
115 current->fsgid = uc->ouc_fsgid;
116 current->cap_effective = uc->ouc_cap;
119 if (uc->ouc_suppgid1 != -1)
120 current_groups[current_ngroups++] = uc->ouc_suppgid1;
121 if (uc->ouc_suppgid2 != -1)
122 current_groups[current_ngroups++] = uc->ouc_suppgid2;
125 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
129 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
130 new_ctx, current, current->fs, current->fs->pwd,
131 atomic_read(¤t->fs->pwd->d_count),
132 atomic_read(¤t->fs->pwd->d_inode->i_count),
133 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
135 atomic_read(¤t->fs->pwdmnt->mnt_count));
138 EXPORT_SYMBOL(push_ctxt);
140 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
141 struct obd_ucred *uc)
144 ASSERT_CTXT_MAGIC(saved->magic);
146 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
150 " = pop %p==%p = cur %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
151 new_ctx, current, current->fs, current->fs->pwd,
152 atomic_read(¤t->fs->pwd->d_count),
153 atomic_read(¤t->fs->pwd->d_inode->i_count),
154 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
156 atomic_read(¤t->fs->pwdmnt->mnt_count));
159 LASSERT(current->fs->pwd == new_ctx->pwd);
160 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
163 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
166 mntput(saved->pwdmnt);
168 current->fsuid = saved->ouc.ouc_fsuid;
169 current->fsgid = saved->ouc.ouc_fsgid;
170 current->cap_effective = saved->ouc.ouc_cap;
171 current_ngroups = saved->ngroups;
172 current_groups[0] = saved->ouc.ouc_suppgid1;
173 current_groups[1] = saved->ouc.ouc_suppgid2;
178 "= pop %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
179 saved, current, current->fs, current->fs->pwd,
180 atomic_read(¤t->fs->pwd->d_count),
181 atomic_read(¤t->fs->pwd->d_inode->i_count),
182 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
184 atomic_read(¤t->fs->pwdmnt->mnt_count));
187 EXPORT_SYMBOL(pop_ctxt);
189 /* utility to make a file */
190 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode, int fix)
192 struct dentry *dchild;
196 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
197 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
199 dchild = ll_lookup_one_len(name, dir, strlen(name));
201 GOTO(out_up, dchild);
203 if (dchild->d_inode) {
204 int old_mode = dchild->d_inode->i_mode;
205 if (!S_ISREG(old_mode))
206 GOTO(out_err, err = -EEXIST);
208 /* Fixup file permissions if necessary */
209 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
210 CWARN("fixing permissions on %s from %o to %o\n",
211 name, old_mode, mode);
212 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
213 (old_mode & ~S_IALLUGO);
214 mark_inode_dirty(dchild->d_inode);
216 GOTO(out_up, dchild);
219 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
228 dchild = ERR_PTR(err);
232 EXPORT_SYMBOL(simple_mknod);
234 /* utility to make a directory */
235 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode, int fix)
237 struct dentry *dchild;
241 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
242 CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
243 dchild = ll_lookup_one_len(name, dir, strlen(name));
245 GOTO(out_up, dchild);
247 if (dchild->d_inode) {
248 int old_mode = dchild->d_inode->i_mode;
249 if (!S_ISDIR(old_mode))
250 GOTO(out_err, err = -ENOTDIR);
252 /* Fixup directory permissions if necessary */
253 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
254 CWARN("fixing permissions on %s from %o to %o\n",
255 name, old_mode, mode);
256 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
257 (old_mode & ~S_IALLUGO);
258 mark_inode_dirty(dchild->d_inode);
260 GOTO(out_up, dchild);
263 err = vfs_mkdir(dir->d_inode, dchild, mode);
271 dchild = ERR_PTR(err);
275 EXPORT_SYMBOL(simple_mkdir);
278 * Read a file from within kernel context. Prior to calling this
279 * function we should already have done a push_ctxt().
281 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
283 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
284 if (!file || !file->f_op || !file->f_op->read || !off)
287 return file->f_op->read(file, buf, len, off);
289 EXPORT_SYMBOL(lustre_fread);
292 * Write a file from within kernel context. Prior to calling this
293 * function we should already have done a push_ctxt().
295 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
298 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
306 if (!file->f_op->write)
309 RETURN(file->f_op->write(file, buf, len, off));
311 EXPORT_SYMBOL(lustre_fwrite);
314 * Sync a file from within kernel context. Prior to calling this
315 * function we should already have done a push_ctxt().
317 int lustre_fsync(struct file *file)
320 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
321 if (!file || !file->f_op || !file->f_op->fsync)
324 RETURN(file->f_op->fsync(file, file->f_dentry, 0));
326 EXPORT_SYMBOL(lustre_fsync);
328 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
331 mntget(ctxt->pwdmnt);
332 return dentry_open(de, ctxt->pwdmnt, flags);
334 EXPORT_SYMBOL(l_dentry_open);
336 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
337 ino_t ino, unsigned int d_type)
339 struct l_linux_dirent *dirent;
340 struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
342 dirent = buf->lrc_dirent;
344 dirent->lld_off = offset;
346 OBD_ALLOC(dirent, sizeof(*dirent));
348 list_add_tail(&dirent->lld_list, buf->lrc_list);
350 buf->lrc_dirent = dirent;
351 dirent->lld_ino = ino;
352 LASSERT(sizeof(dirent->lld_name) >= namlen + 1);
353 memcpy(dirent->lld_name, name, namlen);
358 long l_readdir(struct file *file, struct list_head *dentry_list)
360 struct l_linux_dirent *lastdirent;
361 struct l_readdir_callback buf;
364 buf.lrc_dirent = NULL;
365 buf.lrc_list = dentry_list;
367 error = vfs_readdir(file, l_filldir, &buf);
371 lastdirent = buf.lrc_dirent;
373 lastdirent->lld_off = file->f_pos;
377 EXPORT_SYMBOL(l_readdir);
378 EXPORT_SYMBOL(obd_memory);
379 EXPORT_SYMBOL(obd_memmax);
381 static int __init lvfs_linux_init(void)
386 static void __exit lvfs_linux_exit(void)
391 leaked = atomic_read(&obd_memory);
392 CDEBUG(leaked ? D_ERROR : D_INFO,
393 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
398 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
399 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
400 MODULE_LICENSE("GPL");
402 module_init(lvfs_linux_init);
403 module_exit(lvfs_linux_exit);