Whamcloud - gitweb
Update snapfs
[fs/lustre-release.git] / lustre / smfs / super.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/super.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #define DEBUG_SUBSYSTEM S_SM
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kmod.h>
30 #include <linux/init.h>
31 #include <linux/fs.h>
32 #include <linux/string.h>
33 #include <linux/mm.h>
34 #include <linux/utime.h>
35 #include <linux/file.h>
36 #include <linux/slab.h>
37 #include <linux/dcache.h>
38 #include <linux/loop.h>
39 #include <linux/errno.h>
40 #include <linux/obd_class.h>
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lib.h>
43 #include <linux/lustre_idl.h>
44 #include <linux/lustre_fsfilt.h>
45 #include <linux/lustre_smfs.h>
46 #include "smfs_internal.h"
47
48 static char *smfs_options(char *data, char **devstr, char **namestr,
49                           int *kml, int *cache, char **opts, int *iopen_nopriv,
50                           int *cow)
51 {
52         char *pos;
53         struct option *opt_value = NULL;
54
55         while (!(get_opt(&opt_value, &pos))) {
56                 if (!strcmp(opt_value->opt, "dev")) {
57                         if (devstr != NULL)
58                                 *devstr = opt_value->value;
59                 } else if (!strcmp(opt_value->opt, "type")) {
60                         if (namestr != NULL)
61                                 *namestr = opt_value->value;
62                 } else if (!strcmp(opt_value->opt, "kml")) {
63                         if (kml)
64                                 *kml = 1;
65                 } else if (!strcmp(opt_value->opt, "cache")) {
66                         if (cache)
67                                 *cache = 1;
68                 } else if (!strcmp(opt_value->opt, "options")) {
69                         if (opts != NULL)
70                                 *opts = opt_value->value;
71                 } else if (!strcmp(opt_value->opt, "iopen_nopriv")) {
72                         if (iopen_nopriv != NULL)
73                                 *iopen_nopriv = 1;
74                 } else if (!strcmp(opt_value->opt, "snap")) {
75                                 *cow = 1;
76                 } else {
77                         break;
78                 }
79         }
80         return pos;
81 }
82
83 struct vfsmount *get_vfsmount(struct super_block *sb)
84 {
85         struct vfsmount *rootmnt, *mnt, *ret = NULL;
86         struct list_head *end, *list;
87
88         rootmnt = mntget(current->fs->rootmnt);
89         end = list = &rootmnt->mnt_list;
90         do {
91                 mnt = list_entry(list, struct vfsmount, mnt_list);
92                 if (mnt->mnt_sb == sb) {
93                         ret = mnt;
94                         break;
95                 }
96                 list = list->next;
97         } while (end != list);
98
99         mntput(current->fs->rootmnt);
100         return ret;
101 }
102
103 struct super_block *smfs_get_sb_by_path(char *path, int len)
104 {
105         struct super_block *sb;
106         struct nameidata nd;
107         int error = 0;
108
109         ENTRY;
110
111 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
112         if (path_init(path, LOOKUP_FOLLOW, &nd)) {
113 #else
114         if (path_lookup(path, LOOKUP_FOLLOW, &nd)) {
115 #endif
116                 error = path_walk(path, &nd);
117                 if (error) {
118                         path_release(&nd);
119                         RETURN(NULL);
120                 }
121         } else {
122                 RETURN(NULL);
123         }
124
125         /* FIXME-WANGDI: add some check code here. */
126         sb = nd.dentry->d_sb;
127         path_release(&nd);
128         RETURN(sb);
129 }
130
131 static int smfs_init_fsfilt_ops(struct super_block *sb)
132 {
133         ENTRY;
134         if (!S2SMI(sb)->sm_cache_fsfilt) {
135                 S2SMI(sb)->sm_cache_fsfilt =
136                         fsfilt_get_ops(S2SMI(sb)->smsi_cache_ftype);
137                 if (!S2SMI(sb)->sm_cache_fsfilt) {
138                         CERROR("Can not get %s fsfilt ops needed by kml\n",
139                                S2SMI(sb)->smsi_cache_ftype);
140                         RETURN(-EINVAL);
141                 }
142         }
143         if (!S2SMI(sb)->sm_fsfilt) {
144                 S2SMI(sb)->sm_fsfilt =
145                         fsfilt_get_ops(S2SMI(sb)->smsi_ftype);
146                 if (!S2SMI(sb)->sm_fsfilt) {
147                         CERROR("Can not get %s fsfilt ops needed by kml\n",
148                                S2SMI(sb)->smsi_ftype);
149                         RETURN(-EINVAL);
150                 }
151         }
152         RETURN(0);
153 }
154
155 void smfs_cleanup_fsfilt_ops(struct super_block *sb)
156 {
157         if (S2SMI(sb)->sm_cache_fsfilt)
158                 fsfilt_put_ops(S2SMI(sb)->sm_cache_fsfilt);
159         if (S2SMI(sb)->sm_fsfilt)
160                 fsfilt_put_ops(S2SMI(sb)->sm_fsfilt);
161 }
162
163 static int sm_mount_cache(struct super_block *sb, char *devstr,
164                           char *typestr, char *opts, int iopen_nopriv)
165 {
166         struct smfs_super_info *smb;
167         int err = 0, typelen;
168         struct vfsmount *mnt;
169         unsigned long page;
170         ENTRY;
171
172         typelen = strlen(typestr);
173
174         page = __get_free_page(GFP_KERNEL);
175         if (!page)
176                 GOTO(err_out, err = -ENOMEM);
177
178         memset((void *)page, 0, PAGE_SIZE);
179
180         if (iopen_nopriv)
181                 sprintf((char *)page, "iopen_nopriv");
182
183         if (opts && strlen(opts)) {
184                 int n = strlen((char *)page);
185                 sprintf((char *)page + n, ",%s", opts);
186         }
187
188         printk("smfs: mounting %s at %s\n", typestr, devstr);
189
190         mnt = do_kern_mount(typestr, 0, devstr, (void *)page);
191         free_page(page);
192
193         if (IS_ERR(mnt)) {
194                 CERROR("do_kern_mount failed: rc = %ld\n", PTR_ERR(mnt));
195                 GOTO(err_out, err = PTR_ERR(mnt));
196         }
197         smb = S2SMI(sb);
198         smb->smsi_sb = mnt->mnt_sb;
199         smb->smsi_mnt = mnt;
200
201         smfs_init_sm_ops(smb);
202
203         OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
204         memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
205
206         OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
207         memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
208
209         duplicate_sb(sb, mnt->mnt_sb);
210         sm_set_sb_ops(mnt->mnt_sb, sb);
211         err = smfs_init_fsfilt_ops(sb);
212 err_out:
213         return err;
214 }
215
216 static int sm_umount_cache(struct super_block *sb)
217 {
218         struct smfs_super_info *smb = S2SMI(sb);
219
220         iput(S2CSB(sb)->s_root->d_inode);
221         dput(S2CSB(sb)->s_root);
222         mntput(smb->smsi_mnt);
223         smfs_cleanup_sm_ops(smb);
224         smfs_cleanup_fsfilt_ops(sb);
225
226         if (smb->smsi_cache_ftype)
227                 OBD_FREE(smb->smsi_cache_ftype,
228                          strlen(smb->smsi_cache_ftype) + 1);
229
230         if (smb->smsi_ftype)
231                 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
232
233         return 0;
234 }
235
236 void smfs_put_super(struct super_block *sb)
237 {
238         if (SMFS_CACHE_HOOK(S2SMI(sb)))
239                 cache_space_hook_exit(sb);
240         if (SMFS_DO_REC(S2SMI(sb)))
241                 smfs_rec_cleanup(sb);
242         if (sb)
243                 sm_umount_cache(sb);
244         return;
245 }
246
247 static int smfs_fill_super(struct super_block *sb,
248                            void *data, int silent)
249 {
250         ino_t root_ino;
251         char *cache_data;
252
253         int iopen_nopriv = 0;
254         struct inode *root_inode = NULL;
255         int err = 0, do_rec = 0, cache_hook = 0, do_cow = 0;
256         char *devstr = NULL, *typestr = NULL, *opts = NULL;
257
258         ENTRY;
259
260         CDEBUG(D_SUPER, "mount opts: %s\n", data ?
261                (char *)data : "(none)");
262
263         init_option(data);
264
265         /* read and validate passed options. */
266         cache_data = smfs_options(data, &devstr, &typestr,
267                                   &do_rec, &cache_hook, &opts,
268                                   &iopen_nopriv, &do_cow);
269
270         if (*cache_data)
271                 CWARN("smfs_fill_super(): options parsing stoped at "
272                       "option %s\n", cache_data);
273
274         if (!typestr || !devstr) {
275                 CERROR("mount options name and dev mandatory\n");
276                 GOTO(out_err, err = -EINVAL);
277         }
278         err = sm_mount_cache(sb, devstr, typestr, opts, iopen_nopriv);
279         if (err) {
280                 CERROR("Can not mount %s as %s\n", devstr, typestr);
281                 GOTO(out_err, 0);
282         }
283
284         if (do_rec) smfs_rec_init(sb);
285         if (cache_hook) cache_space_hook_init(sb);
286         
287         dget(S2CSB(sb)->s_root);
288         root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
289         root_inode = iget(sb, root_ino);
290
291         CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
292                sb->s_op->read_inode, root_ino, root_inode);
293
294         sb->s_root = d_alloc_root(root_inode);
295
296         if (!sb->s_root) {
297                 sm_umount_cache(sb);
298                 GOTO(out_err, err=-EINVAL);
299         }
300 #if CONFIG_SNAP
301         if (do_cow) smfs_cow_init(sb);
302 #endif
303
304 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
305         CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
306                (ulong)sb, (ulong)&sb->u.generic_sbp);
307 #else
308         CDEBUG(D_SUPER, "sb %lx, &sb->s_fs_info: %lx\n",
309                (ulong)sb, (ulong)&sb->s_fs_info);
310 #endif
311
312 out_err:
313         cleanup_option();
314         return err;
315 }
316
317 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
318 static struct super_block *smfs_read_super(struct super_block *sb,
319                                            void *data, int silent)
320 {
321         int err;
322
323         err = smfs_fill_super(sb, data, silent);
324         if (err)
325                 return NULL;
326
327         return sb;
328 }
329 #else
330 struct super_block *smfs_get_sb(struct file_system_type *fs_type,
331                                 int flags, const char *dev_name, void *data)
332 {
333         return get_sb_nodev(fs_type, flags, data, smfs_fill_super);
334 }
335 #endif
336
337 static struct file_system_type smfs_type = {
338         .owner       = THIS_MODULE,
339         .name        = "smfs",
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
341         .read_super  = smfs_read_super,
342 #else
343         .get_sb      = smfs_get_sb,
344         .kill_sb     = kill_anon_super,
345 #endif
346 };
347
348 int init_smfs(void)
349 {
350         int err;
351
352         err = register_filesystem(&smfs_type);
353         if (err)
354                 CERROR("register_filesystem() failed, rc = %d\n", err);
355         return err;
356 }
357
358 int cleanup_smfs(void)
359 {
360         int err = 0;
361
362         err = unregister_filesystem(&smfs_type);
363         if (err)
364                 CERROR("unregister_filesystem() failed, rc = %d\n", err);
365         return 0;
366 }