Whamcloud - gitweb
minor fix for snap_inode_init
[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 CONFIG_SNAPFS
243         if (SMFS_DO_COW(S2SMI(sb)))
244                 smfs_cow_cleanup(sb);
245 #endif
246         if (sb)
247                 sm_umount_cache(sb);
248         return;
249 }
250
251 static int smfs_fill_super(struct super_block *sb,
252                            void *data, int silent)
253 {
254         ino_t root_ino;
255         char *cache_data;
256
257         int iopen_nopriv = 0;
258         struct inode *root_inode = NULL;
259         int err = 0, do_rec = 0, cache_hook = 0, do_cow = 0;
260         char *devstr = NULL, *typestr = NULL, *opts = NULL;
261
262         ENTRY;
263
264         CDEBUG(D_SUPER, "mount opts: %s\n", data ?
265                (char *)data : "(none)");
266
267         init_option(data);
268
269         /* read and validate passed options. */
270         cache_data = smfs_options(data, &devstr, &typestr,
271                                   &do_rec, &cache_hook, &opts,
272                                   &iopen_nopriv, &do_cow);
273
274         if (*cache_data)
275                 CWARN("smfs_fill_super(): options parsing stoped at "
276                       "option %s\n", cache_data);
277
278         if (!typestr || !devstr) {
279                 CERROR("mount options name and dev mandatory\n");
280                 GOTO(out_err, err = -EINVAL);
281         }
282         err = sm_mount_cache(sb, devstr, typestr, opts, iopen_nopriv);
283         if (err) {
284                 CERROR("Can not mount %s as %s\n", devstr, typestr);
285                 GOTO(out_err, 0);
286         }
287
288         if (do_rec) smfs_rec_init(sb);
289         if (cache_hook) cache_space_hook_init(sb);
290         
291         dget(S2CSB(sb)->s_root);
292         root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
293         root_inode = iget(sb, root_ino);
294
295         CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
296                sb->s_op->read_inode, root_ino, root_inode);
297
298         sb->s_root = d_alloc_root(root_inode);
299
300         if (!sb->s_root) {
301                 sm_umount_cache(sb);
302                 GOTO(out_err, err=-EINVAL);
303         }
304 #if CONFIG_SNAPFS
305         if (do_cow) smfs_cow_init(sb);
306 #endif
307
308 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
309         CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
310                (ulong)sb, (ulong)&sb->u.generic_sbp);
311 #else
312         CDEBUG(D_SUPER, "sb %lx, &sb->s_fs_info: %lx\n",
313                (ulong)sb, (ulong)&sb->s_fs_info);
314 #endif
315
316 out_err:
317         cleanup_option();
318         return err;
319 }
320
321 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
322 static struct super_block *smfs_read_super(struct super_block *sb,
323                                            void *data, int silent)
324 {
325         int err;
326
327         err = smfs_fill_super(sb, data, silent);
328         if (err)
329                 return NULL;
330
331         return sb;
332 }
333 #else
334 struct super_block *smfs_get_sb(struct file_system_type *fs_type,
335                                 int flags, const char *dev_name, void *data)
336 {
337         return get_sb_nodev(fs_type, flags, data, smfs_fill_super);
338 }
339 #endif
340
341 static struct file_system_type smfs_type = {
342         .owner       = THIS_MODULE,
343         .name        = "smfs",
344 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
345         .read_super  = smfs_read_super,
346 #else
347         .get_sb      = smfs_get_sb,
348         .kill_sb     = kill_anon_super,
349 #endif
350 };
351
352 int init_smfs(void)
353 {
354         int err;
355
356         err = register_filesystem(&smfs_type);
357         if (err)
358                 CERROR("register_filesystem() failed, rc = %d\n", err);
359         return err;
360 }
361
362 int cleanup_smfs(void)
363 {
364         int err = 0;
365
366         err = unregister_filesystem(&smfs_type);
367         if (err)
368                 CERROR("unregister_filesystem() failed, rc = %d\n", err);
369         return 0;
370 }