1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2004 Cluster File Systems, Inc.
9 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
25 #define DEBUG_SUBSYSTEM S_SM
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kmod.h>
30 #include <linux/init.h>
32 #include <linux/string.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"
48 static char *smfs_options(char *data, char **devstr, char **namestr,
49 int *kml, int *cache, char **opts, int *iopen_nopriv,
53 struct option *opt_value = NULL;
55 while (!(get_opt(&opt_value, &pos))) {
56 if (!strcmp(opt_value->opt, "dev")) {
58 *devstr = opt_value->value;
59 } else if (!strcmp(opt_value->opt, "type")) {
61 *namestr = opt_value->value;
62 } else if (!strcmp(opt_value->opt, "kml")) {
65 } else if (!strcmp(opt_value->opt, "cache")) {
68 } else if (!strcmp(opt_value->opt, "options")) {
70 *opts = opt_value->value;
71 } else if (!strcmp(opt_value->opt, "iopen_nopriv")) {
72 if (iopen_nopriv != NULL)
74 } else if (!strcmp(opt_value->opt, "snap")) {
83 struct vfsmount *get_vfsmount(struct super_block *sb)
85 struct vfsmount *rootmnt, *mnt, *ret = NULL;
86 struct list_head *end, *list;
88 rootmnt = mntget(current->fs->rootmnt);
89 end = list = &rootmnt->mnt_list;
91 mnt = list_entry(list, struct vfsmount, mnt_list);
92 if (mnt->mnt_sb == sb) {
97 } while (end != list);
99 mntput(current->fs->rootmnt);
103 struct super_block *smfs_get_sb_by_path(char *path, int len)
105 struct super_block *sb;
111 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
112 if (path_init(path, LOOKUP_FOLLOW, &nd)) {
114 if (path_lookup(path, LOOKUP_FOLLOW, &nd)) {
116 error = path_walk(path, &nd);
125 /* FIXME-WANGDI: add some check code here. */
126 sb = nd.dentry->d_sb;
131 static int smfs_init_fsfilt_ops(struct super_block *sb)
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);
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);
155 void smfs_cleanup_fsfilt_ops(struct super_block *sb)
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);
163 static int sm_mount_cache(struct super_block *sb, char *devstr,
164 char *typestr, char *opts, int iopen_nopriv)
166 struct smfs_super_info *smb;
167 int err = 0, typelen;
168 struct vfsmount *mnt;
172 typelen = strlen(typestr);
174 page = __get_free_page(GFP_KERNEL);
176 GOTO(err_out, err = -ENOMEM);
178 memset((void *)page, 0, PAGE_SIZE);
181 sprintf((char *)page, "iopen_nopriv");
183 if (opts && strlen(opts)) {
184 int n = strlen((char *)page);
185 sprintf((char *)page + n, ",%s", opts);
188 printk("smfs: mounting %s at %s\n", typestr, devstr);
190 mnt = do_kern_mount(typestr, 0, devstr, (void *)page);
194 CERROR("do_kern_mount failed: rc = %ld\n", PTR_ERR(mnt));
195 GOTO(err_out, err = PTR_ERR(mnt));
198 smb->smsi_sb = mnt->mnt_sb;
201 smfs_init_sm_ops(smb);
203 OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
204 memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
206 OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
207 memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
209 duplicate_sb(sb, mnt->mnt_sb);
210 sm_set_sb_ops(mnt->mnt_sb, sb);
211 err = smfs_init_fsfilt_ops(sb);
216 static int sm_umount_cache(struct super_block *sb)
218 struct smfs_super_info *smb = S2SMI(sb);
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);
226 if (smb->smsi_cache_ftype)
227 OBD_FREE(smb->smsi_cache_ftype,
228 strlen(smb->smsi_cache_ftype) + 1);
231 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
236 void smfs_put_super(struct super_block *sb)
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);
247 static int smfs_fill_super(struct super_block *sb,
248 void *data, int silent)
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;
260 CDEBUG(D_SUPER, "mount opts: %s\n", data ?
261 (char *)data : "(none)");
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);
271 CWARN("smfs_fill_super(): options parsing stoped at "
272 "option %s\n", cache_data);
274 if (!typestr || !devstr) {
275 CERROR("mount options name and dev mandatory\n");
276 GOTO(out_err, err = -EINVAL);
278 err = sm_mount_cache(sb, devstr, typestr, opts, iopen_nopriv);
280 CERROR("Can not mount %s as %s\n", devstr, typestr);
284 if (do_rec) smfs_rec_init(sb);
285 if (cache_hook) cache_space_hook_init(sb);
287 dget(S2CSB(sb)->s_root);
288 root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
289 root_inode = iget(sb, root_ino);
291 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
292 sb->s_op->read_inode, root_ino, root_inode);
294 sb->s_root = d_alloc_root(root_inode);
298 GOTO(out_err, err=-EINVAL);
301 if (do_cow) smfs_cow_init(sb);
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);
308 CDEBUG(D_SUPER, "sb %lx, &sb->s_fs_info: %lx\n",
309 (ulong)sb, (ulong)&sb->s_fs_info);
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)
323 err = smfs_fill_super(sb, data, silent);
330 struct super_block *smfs_get_sb(struct file_system_type *fs_type,
331 int flags, const char *dev_name, void *data)
333 return get_sb_nodev(fs_type, flags, data, smfs_fill_super);
337 static struct file_system_type smfs_type = {
338 .owner = THIS_MODULE,
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
341 .read_super = smfs_read_super,
343 .get_sb = smfs_get_sb,
344 .kill_sb = kill_anon_super,
352 err = register_filesystem(&smfs_type);
354 CERROR("register_filesystem() failed, rc = %d\n", err);
358 int cleanup_smfs(void)
362 err = unregister_filesystem(&smfs_type);
364 CERROR("unregister_filesystem() failed, rc = %d\n", err);