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];
113 save->ouc.ouc_umask = current->fs->umask;
115 current->fsuid = uc->ouc_fsuid;
116 current->fsgid = uc->ouc_fsgid;
117 current->cap_effective = uc->ouc_cap;
119 current->fs->umask = 0; /* umask already applied on client */
121 if (uc->ouc_suppgid1 != -1)
122 current_groups[current_ngroups++] = uc->ouc_suppgid1;
123 if (uc->ouc_suppgid2 != -1)
124 current_groups[current_ngroups++] = uc->ouc_suppgid2;
127 set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
131 "= push %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
132 new_ctx, current, current->fs, current->fs->pwd,
133 atomic_read(¤t->fs->pwd->d_count),
134 atomic_read(¤t->fs->pwd->d_inode->i_count),
135 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
137 atomic_read(¤t->fs->pwdmnt->mnt_count));
140 EXPORT_SYMBOL(push_ctxt);
142 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
143 struct obd_ucred *uc)
146 ASSERT_CTXT_MAGIC(saved->magic);
148 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
152 " = pop %p==%p = cur %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
153 new_ctx, current, current->fs, current->fs->pwd,
154 atomic_read(¤t->fs->pwd->d_count),
155 atomic_read(¤t->fs->pwd->d_inode->i_count),
156 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
158 atomic_read(¤t->fs->pwdmnt->mnt_count));
161 LASSERT(current->fs->pwd == new_ctx->pwd);
162 LASSERT(current->fs->pwdmnt == new_ctx->pwdmnt);
165 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
168 mntput(saved->pwdmnt);
170 current->fsuid = saved->ouc.ouc_fsuid;
171 current->fsgid = saved->ouc.ouc_fsgid;
172 current->cap_effective = saved->ouc.ouc_cap;
173 current_ngroups = saved->ngroups;
174 current_groups[0] = saved->ouc.ouc_suppgid1;
175 current_groups[1] = saved->ouc.ouc_suppgid2;
176 current->fs->umask = saved->ouc.ouc_umask;
181 "= pop %p->%p = cur fs %p pwd %p:d%d:i%d (%*s), pwdmnt %p:%d\n",
182 saved, current, current->fs, current->fs->pwd,
183 atomic_read(¤t->fs->pwd->d_count),
184 atomic_read(¤t->fs->pwd->d_inode->i_count),
185 current->fs->pwd->d_name.len, current->fs->pwd->d_name.name,
187 atomic_read(¤t->fs->pwdmnt->mnt_count));
190 EXPORT_SYMBOL(pop_ctxt);
192 /* utility to make a file */
193 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode, int fix)
195 struct dentry *dchild;
199 ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
200 CDEBUG(D_INODE, "creating file %*s\n", (int)strlen(name), name);
202 dchild = ll_lookup_one_len(name, dir, strlen(name));
204 GOTO(out_up, dchild);
206 if (dchild->d_inode) {
207 int old_mode = dchild->d_inode->i_mode;
208 if (!S_ISREG(old_mode))
209 GOTO(out_err, err = -EEXIST);
211 /* Fixup file permissions if necessary */
212 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
213 CWARN("fixing permissions on %s from %o to %o\n",
214 name, old_mode, mode);
215 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
216 (old_mode & ~S_IALLUGO);
217 mark_inode_dirty(dchild->d_inode);
219 GOTO(out_up, dchild);
222 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
231 dchild = ERR_PTR(err);
235 EXPORT_SYMBOL(simple_mknod);
237 /* utility to make a directory */
238 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode, int fix)
240 struct dentry *dchild;
244 ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
245 CDEBUG(D_INODE, "creating directory %*s\n", (int)strlen(name), name);
246 dchild = ll_lookup_one_len(name, dir, strlen(name));
248 GOTO(out_up, dchild);
250 if (dchild->d_inode) {
251 int old_mode = dchild->d_inode->i_mode;
252 if (!S_ISDIR(old_mode))
253 GOTO(out_err, err = -ENOTDIR);
255 /* Fixup directory permissions if necessary */
256 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
257 CWARN("fixing permissions on %s from %o to %o\n",
258 name, old_mode, mode);
259 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
260 (old_mode & ~S_IALLUGO);
261 mark_inode_dirty(dchild->d_inode);
263 GOTO(out_up, dchild);
266 err = vfs_mkdir(dir->d_inode, dchild, mode);
274 dchild = ERR_PTR(err);
278 EXPORT_SYMBOL(simple_mkdir);
281 * Read a file from within kernel context. Prior to calling this
282 * function we should already have done a push_ctxt().
284 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
286 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
287 if (!file || !file->f_op || !file->f_op->read || !off)
290 return file->f_op->read(file, buf, len, off);
292 EXPORT_SYMBOL(lustre_fread);
295 * Write a file from within kernel context. Prior to calling this
296 * function we should already have done a push_ctxt().
298 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
301 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
309 if (!file->f_op->write)
312 RETURN(file->f_op->write(file, buf, len, off));
314 EXPORT_SYMBOL(lustre_fwrite);
317 * Sync a file from within kernel context. Prior to calling this
318 * function we should already have done a push_ctxt().
320 int lustre_fsync(struct file *file)
323 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
324 if (!file || !file->f_op || !file->f_op->fsync)
327 RETURN(file->f_op->fsync(file, file->f_dentry, 0));
329 EXPORT_SYMBOL(lustre_fsync);
331 struct l_file *l_dentry_open(struct obd_run_ctxt *ctxt, struct l_dentry *de,
334 mntget(ctxt->pwdmnt);
335 return dentry_open(de, ctxt->pwdmnt, flags);
337 EXPORT_SYMBOL(l_dentry_open);
339 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
340 ino_t ino, unsigned int d_type)
342 struct l_linux_dirent *dirent;
343 struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
345 dirent = buf->lrc_dirent;
347 dirent->lld_off = offset;
349 OBD_ALLOC(dirent, sizeof(*dirent));
351 list_add_tail(&dirent->lld_list, buf->lrc_list);
353 buf->lrc_dirent = dirent;
354 dirent->lld_ino = ino;
355 LASSERT(sizeof(dirent->lld_name) >= namlen + 1);
356 memcpy(dirent->lld_name, name, namlen);
361 long l_readdir(struct file *file, struct list_head *dentry_list)
363 struct l_linux_dirent *lastdirent;
364 struct l_readdir_callback buf;
367 buf.lrc_dirent = NULL;
368 buf.lrc_list = dentry_list;
370 error = vfs_readdir(file, l_filldir, &buf);
374 lastdirent = buf.lrc_dirent;
376 lastdirent->lld_off = file->f_pos;
380 EXPORT_SYMBOL(l_readdir);
381 EXPORT_SYMBOL(obd_memory);
382 EXPORT_SYMBOL(obd_memmax);
384 static int __init lvfs_linux_init(void)
389 static void __exit lvfs_linux_exit(void)
394 leaked = atomic_read(&obd_memory);
395 CDEBUG(leaked ? D_ERROR : D_INFO,
396 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
401 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
402 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
403 MODULE_LICENSE("GPL");
405 module_init(lvfs_linux_init);
406 module_exit(lvfs_linux_exit);