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 int old_mode = dchild->d_inode->i_mode;
197 if (!S_ISREG(old_mode))
198 GOTO(out_err, err = -EEXIST);
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);
208 GOTO(out_up, dchild);
211 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
220 dchild = ERR_PTR(err);
224 EXPORT_SYMBOL(simple_mknod);
226 /* utility to make a directory */
227 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
229 struct dentry *dchild;
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));
237 GOTO(out_up, dchild);
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);
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);
252 GOTO(out_up, dchild);
255 err = vfs_mkdir(dir->d_inode, dchild, mode);
263 dchild = ERR_PTR(err);
267 EXPORT_SYMBOL(simple_mkdir);
270 * Read a file from within kernel context. Prior to calling this
271 * function we should already have done a push_ctxt().
273 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
275 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
276 if (!file || !file->f_op || !file->f_op->read || !off)
279 return file->f_op->read(file, buf, len, off);
281 EXPORT_SYMBOL(lustre_fread);
284 * Write a file from within kernel context. Prior to calling this
285 * function we should already have done a push_ctxt().
287 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
290 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
298 if (!file->f_op->write)
301 RETURN(file->f_op->write(file, buf, len, off));
303 EXPORT_SYMBOL(lustre_fwrite);
306 * Sync a file from within kernel context. Prior to calling this
307 * function we should already have done a push_ctxt().
309 int lustre_fsync(struct file *file)
312 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
313 if (!file || !file->f_op || !file->f_op->fsync)
316 RETURN(file->f_op->fsync(file, file->f_dentry, 0));
318 EXPORT_SYMBOL(lustre_fsync);
320 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
323 mntget(ctxt->pwdmnt);
324 return dentry_open(de, ctxt->pwdmnt, flags);
326 EXPORT_SYMBOL(l_dentry_open);
328 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
329 ino_t ino, unsigned int d_type)
331 struct l_linux_dirent *dirent;
332 struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
334 dirent = buf->lrc_dirent;
336 dirent->lld_off = offset;
338 OBD_ALLOC(dirent, sizeof(*dirent));
340 list_add_tail(&dirent->lld_list, buf->lrc_list);
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);
350 long l_readdir(struct file *file, struct list_head *dentry_list)
352 struct l_linux_dirent *lastdirent;
353 struct l_readdir_callback buf;
356 buf.lrc_dirent = NULL;
357 buf.lrc_list = dentry_list;
359 error = vfs_readdir(file, l_filldir, &buf);
363 lastdirent = buf.lrc_dirent;
365 lastdirent->lld_off = file->f_pos;
369 EXPORT_SYMBOL(l_readdir);
370 EXPORT_SYMBOL(obd_memory);
371 EXPORT_SYMBOL(obd_memmax);
373 static int __init lvfs_linux_init(void)
378 static void __exit lvfs_linux_exit(void)
383 leaked = atomic_read(&obd_memory);
384 CDEBUG(leaked ? D_ERROR : D_INFO,
385 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
390 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
391 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
392 MODULE_LICENSE("GPL");
394 module_init(lvfs_linux_init);
395 module_exit(lvfs_linux_exit);