Whamcloud - gitweb
Install and distribute lconf and lmc.
[fs/lustre-release.git] / lustre / lib / simple.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lib/simple.c
5  *
6  * Copyright (C) 2002  Cluster File Systems, Inc.
7  *
8  * This code is issued under the GNU General Public License.
9  * See the file COPYING in this distribution
10  *
11  * by Peter Braam <braam@clusterfs.com>
12  * and Andreas Dilger <adilger@clusterfs.com>
13  */
14
15 #define EXPORT_SYMTAB
16
17 #include <linux/version.h>
18 #include <linux/fs.h>
19 #include <asm/unistd.h>
20
21 #define DEBUG_SUBSYSTEM S_FILTER
22
23 #include <linux/lustre_mds.h>
24 #include <linux/lustre_lib.h>
25 #include <linux/lustre_net.h>
26
27 #ifdef OBD_CTXT_DEBUG
28 /* Debugging check only needed during development */
29 #define ASSERT_CTXT_MAGIC(magic) do { if ((magic) != OBD_RUN_CTXT_MAGIC) { \
30                                 CERROR("bad ctxt magic\n"); LBUG(); } } while(0)
31 #define ASSERT_NOT_KERNEL_CTXT(msg) do { if (segment_eq(get_fs(), get_ds())) { \
32                                         CERROR(msg); LBUG(); } } while(0)
33 #define ASSERT_KERNEL_CTXT(msg) do { if (!segment_eq(get_fs(), get_ds())) { \
34                                         CERROR(msg); LBUG(); } } while(0)
35 #else
36 #define ASSERT_CTXT_MAGIC(magic) do {} while(0)
37 #define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
38 #define ASSERT_KERNEL_CTXT(msg) do {} while(0)
39 #endif
40
41 /* push / pop to root of obd store */
42 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new)
43 {
44         //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
45         ASSERT_CTXT_MAGIC(new->magic);
46         OBD_SET_CTXT_MAGIC(save);
47         save->fs = get_fs();
48         save->pwd = dget(current->fs->pwd);
49         save->pwdmnt = mntget(current->fs->pwdmnt);
50
51         set_fs(new->fs);
52         set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
53 }
54
55 void pop_ctxt(struct obd_run_ctxt *saved)
56 {
57         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
58         ASSERT_CTXT_MAGIC(saved->magic);
59         set_fs(saved->fs);
60         set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
61
62         dput(saved->pwd);
63         mntput(saved->pwdmnt);
64 }
65
66 /* utility to make a directory */
67 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
68 {
69         struct dentry *dchild;
70         int err;
71         ENTRY;
72
73         ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
74         CDEBUG(D_INODE, "creating directory %*s\n", strlen(name), name);
75         dchild = lookup_one_len(name, dir, strlen(name));
76         if (IS_ERR(dchild))
77                 RETURN(dchild);
78
79         if (dchild->d_inode) {
80                 if (!S_ISDIR(dchild->d_inode->i_mode))
81                         GOTO(out, err = -ENOTDIR);
82
83                 RETURN(dchild);
84         }
85
86         err = vfs_mkdir(dir->d_inode, dchild, mode);
87         EXIT;
88 out:
89         if (err) {
90                 dput(dchild);
91                 RETURN(ERR_PTR(err));
92         }
93
94         RETURN(dchild);
95 }
96
97 /*
98  * Read a file from within kernel context.  Prior to calling this
99  * function we should already have done a push_ctxt().
100  */
101 int lustre_fread(struct file *file, char *str, int len, loff_t *off)
102 {
103         ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
104         if (!file || !file->f_op || !file->f_op->read || !off)
105                 RETURN(-ENOSYS);
106
107         return file->f_op->read(file, str, len, off);
108 }
109
110 /*
111  * Write a file from within kernel context.  Prior to calling this
112  * function we should already have done a push_ctxt().
113  */
114 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off)
115 {
116         ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
117         if (!file || !file->f_op || !off)
118                 RETURN(-ENOSYS);
119
120         if (!file->f_op->write)
121                 RETURN(-EROFS);
122
123         return file->f_op->write(file, str, len, off);
124 }
125
126 /*
127  * Sync a file from within kernel context.  Prior to calling this
128  * function we should already have done a push_ctxt().
129  */
130 int lustre_fsync(struct file *file)
131 {
132         ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
133         if (!file || !file->f_op || !file->f_op->fsync)
134                 RETURN(-ENOSYS);
135
136         return file->f_op->fsync(file, file->f_dentry, 0);
137 }