Whamcloud - gitweb
d38b3f2297b4b9ff11ae132cf6940732a734cfa2
[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         LASSERT(save->pwd);
52         LASSERT(save->pwdmnt);
53         LASSERT(new->pwd);
54         LASSERT(new->pwdmnt);
55
56         set_fs(new->fs);
57         set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
58 }
59
60 void pop_ctxt(struct obd_run_ctxt *saved)
61 {
62         ASSERT_CTXT_MAGIC(saved->magic);
63         ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
64         set_fs(saved->fs);
65         LASSERT(saved->pwd);
66         LASSERT(saved->pwdmnt);
67         set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
68
69         dput(saved->pwd);
70         mntput(saved->pwdmnt);
71 }
72
73 /* utility to make a directory */
74 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode)
75 {
76         struct dentry *dchild;
77         int err;
78         ENTRY;
79
80         ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
81         CDEBUG(D_INODE, "creating directory %*s\n", strlen(name), name);
82         dchild = lookup_one_len(name, dir, strlen(name));
83         if (IS_ERR(dchild))
84                 RETURN(dchild);
85
86         if (dchild->d_inode) {
87                 if (!S_ISDIR(dchild->d_inode->i_mode))
88                         GOTO(out, err = -ENOTDIR);
89
90                 RETURN(dchild);
91         }
92
93         err = vfs_mkdir(dir->d_inode, dchild, mode);
94         EXIT;
95 out:
96         if (err) {
97                 dput(dchild);
98                 RETURN(ERR_PTR(err));
99         }
100
101         RETURN(dchild);
102 }
103
104 /*
105  * Read a file from within kernel context.  Prior to calling this
106  * function we should already have done a push_ctxt().
107  */
108 int lustre_fread(struct file *file, char *str, int len, loff_t *off)
109 {
110         ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
111         if (!file || !file->f_op || !file->f_op->read || !off)
112                 RETURN(-ENOSYS);
113
114         return file->f_op->read(file, str, len, off);
115 }
116
117 /*
118  * Write a file from within kernel context.  Prior to calling this
119  * function we should already have done a push_ctxt().
120  */
121 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off)
122 {
123         ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
124         if (!file || !file->f_op || !off)
125                 RETURN(-ENOSYS);
126
127         if (!file->f_op->write)
128                 RETURN(-EROFS);
129
130         return file->f_op->write(file, str, len, off);
131 }
132
133 /*
134  * Sync a file from within kernel context.  Prior to calling this
135  * function we should already have done a push_ctxt().
136  */
137 int lustre_fsync(struct file *file)
138 {
139         ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
140         if (!file || !file->f_op || !file->f_op->fsync)
141                 RETURN(-ENOSYS);
142
143         return file->f_op->fsync(file, file->f_dentry, 0);
144 }