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"
49 struct list_head smfs_plg_list;
51 static struct smfs_super_info *smfs_init_smb(struct super_block *sb)
53 struct smfs_super_info *smb;
56 OBD_ALLOC(smb, sizeof(*smb));
64 static int smfs_init_fsfilt_ops(struct smfs_super_info *smb)
67 if (!smb->sm_cache_fsfilt) {
68 smb->sm_cache_fsfilt =
69 fsfilt_get_ops(smb->smsi_cache_ftype);
70 if (!smb->sm_cache_fsfilt) {
71 CERROR("Can not get %s fsfilt ops needed by kml\n",
72 smb->smsi_cache_ftype);
76 if (!smb->sm_fsfilt) {
78 fsfilt_get_ops(smb->smsi_ftype);
79 if (!smb->sm_fsfilt) {
80 CERROR("Can not get %s fsfilt ops needed by kml\n",
88 void smfs_cleanup_fsfilt_ops(struct smfs_super_info *smb)
90 if (smb->sm_cache_fsfilt)
91 fsfilt_put_ops(smb->sm_cache_fsfilt);
93 fsfilt_put_ops(smb->sm_fsfilt);
96 static int smfs_mount_cache(struct smfs_super_info *smb, char *devstr,
97 char *typestr, char *opts)
100 struct vfsmount *mnt;
103 typelen = strlen(typestr);
105 printk("smfs: mounting %s at %s\n", typestr, devstr);
106 mnt = do_kern_mount(typestr, 0, devstr, (void *)opts);
108 CERROR("do_kern_mount failed: rc = %ld\n",
110 GOTO(err_out, err = PTR_ERR(mnt));
113 smb->smsi_sb = mnt->mnt_sb;
116 smfs_init_sm_ops(smb);
118 OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
119 memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
121 OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
122 memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
124 err = smfs_init_fsfilt_ops(smb);
129 static int smfs_umount_cache(struct smfs_super_info *smb)
131 mntput(smb->smsi_mnt);
132 smfs_cleanup_sm_ops(smb);
133 smfs_cleanup_fsfilt_ops(smb);
135 if (smb->smsi_cache_ftype)
136 OBD_FREE(smb->smsi_cache_ftype,
137 strlen(smb->smsi_cache_ftype) + 1);
139 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
144 static int smfs_init_hook_ops(struct smfs_super_info *smb)
147 INIT_LIST_HEAD(&smb->smsi_hook_list);
148 INIT_LIST_HEAD(&smb->smsi_plg_list);
152 static void smfs_cleanup_hook_ops(struct smfs_super_info *smb)
154 struct list_head *hlist = &smb->smsi_hook_list;
157 while (!list_empty(hlist)) {
158 struct smfs_hook_ops *smfs_hops;
160 smfs_hops = list_entry(hlist->next, struct smfs_hook_ops,
162 CERROR("Unregister %s hook ops\n", smfs_hops->smh_name);
164 smfs_unregister_hook_ops(smb, smfs_hops->smh_name);
165 smfs_free_hook_ops(smfs_hops);
169 static void smfs_cleanup_smb(struct super_block *sb)
171 struct smfs_super_info *smb;
176 OBD_FREE(smb, sizeof(*smb));
180 void smfs_cleanup_hooks(struct smfs_super_info *smb)
183 if (SMFS_CACHE_HOOK(smb))
184 cache_space_hook_exit(smb);
185 if (SMFS_DO_REC(smb))
186 smfs_rec_cleanup(smb);
188 if (SMFS_DO_COW(smb))
189 smfs_cow_cleanup(smb);
191 smfs_cleanup_hook_ops(smb);
194 void smfs_put_super(struct super_block *sb)
196 struct smfs_super_info *smfs_info = S2SMI(sb);
198 smfs_cleanup_hooks(smfs_info);
201 smfs_umount_cache(smfs_info);
202 smfs_cleanup_smb(sb);
205 static int smfs_init_hooks(struct super_block *sb)
209 if (SMFS_DO_REC(S2SMI(sb)))
211 if (SMFS_CACHE_HOOK(S2SMI(sb)))
212 cache_space_hook_init(sb);
214 if (SMFS_DO_COW(S2SMI(sb)))
220 extern char* smfs_options(char*, char**, char**, char*, int *);
221 extern void cleanup_option(void);
223 int smfs_fill_super(struct super_block *sb, void *data, int silent)
225 struct inode *root_inode = NULL;
226 struct smfs_super_info *smb = NULL;
227 char *devstr = NULL, *typestr = NULL;
228 char *opts = NULL, *cache_data = NULL;
235 CDEBUG(D_SUPER, "mount opts: %s\n", data ?
236 (char *)data : "(none)");
238 smb = smfs_init_smb(sb);
242 page = __get_free_page(GFP_KERNEL);
244 GOTO(out_err, err = -ENOMEM);
246 memset((void *)page, 0, PAGE_SIZE);
249 cache_data = smfs_options(data, &devstr, &typestr, opts,
252 CWARN("smfs_fill_super(): options parsing stoped at "
253 "option %s\n", cache_data);
256 if (!typestr || !devstr) {
257 CERROR("mount options name and dev are mandatory\n");
259 GOTO(out_err, err = -EINVAL);
262 err = smfs_mount_cache(smb, devstr, typestr, opts);
266 CERROR("Can not mount %s as %s\n", devstr, typestr);
270 duplicate_sb(sb, smb->smsi_sb);
271 sb->s_bdev = smb->smsi_sb->s_bdev;
272 sm_set_sb_ops(smb->smsi_sb, sb);
274 err = smfs_init_hook_ops(smb);
276 CERROR("Can not init super hook ops err %d\n", err);
277 smfs_umount_cache(smb);
281 /* init the root_inode of smfs. */
282 //dget(S2CSB(sb)->s_root);
283 root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
284 root_inode = smfs_get_inode(sb, root_ino, NULL, 0);
286 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
287 sb->s_op->read_inode, root_ino, root_inode);
289 sb->s_root = d_alloc_root(root_inode);
292 smfs_umount_cache(smb);
293 GOTO(out_err, err = -ENOMEM);
296 err = smfs_init_hooks(sb);
298 smfs_umount_cache(smb);
301 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
302 CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
303 (ulong)sb, (ulong)&sb->u.generic_sbp);
305 CDEBUG(D_SUPER, "sb %lx(%p), &sb->s_fs_info: %lx\n",
306 (ulong)sb, smb->smsi_sb, (ulong)&sb->s_fs_info);
312 smfs_cleanup_smb(sb);
316 struct smfs_hook_ops *smfs_alloc_hook_ops(char *name, smfs_hook_func pre_hook,
317 smfs_hook_func post_hook)
319 struct smfs_hook_ops *smfs_hops = NULL;
322 OBD_ALLOC(smfs_hops, sizeof(struct smfs_hook_ops));
327 OBD_ALLOC(smfs_hops->smh_name, strlen(name) + 1);
329 if (!smfs_hops->smh_name) {
330 OBD_FREE(smfs_hops, sizeof(struct smfs_hook_ops));
334 memcpy(smfs_hops->smh_name, name, strlen(name));
336 smfs_hops->smh_post_op = post_hook;
337 smfs_hops->smh_pre_op = pre_hook;
342 void smfs_free_hook_ops(struct smfs_hook_ops *hops)
346 OBD_FREE(hops->smh_name, strlen(hops->smh_name) + 1);
348 OBD_FREE(hops, sizeof(struct smfs_hook_ops));
352 int smfs_register_hook_ops(struct smfs_super_info *smb,
353 struct smfs_hook_ops *smh_ops)
355 struct list_head *hlist = &smb->smsi_hook_list;
359 list_for_each(p, hlist) {
360 struct smfs_hook_ops *found;
361 found = list_entry(p, struct smfs_hook_ops, smh_list);
362 if (!strcmp(found->smh_name, smh_ops->smh_name)) {
363 CWARN("hook ops %s list reregister\n", smh_ops->smh_name);
367 list_add(&smh_ops->smh_list, hlist);
371 struct smfs_hook_ops *smfs_unregister_hook_ops(struct smfs_super_info *smb,
374 struct list_head *hlist = &smb->smsi_hook_list;
378 list_for_each(p, hlist) {
379 struct smfs_hook_ops *found;
381 found = list_entry(p, typeof(*found), smh_list);
382 if (!memcmp(found->smh_name, name, strlen(name))) {
391 void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
393 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
395 CDEBUG(D_INFO, "trans start %p\n", fsfilt->fs_start);
397 SMFS_TRANS_OP(inode, op);
399 /* There are some problem here. fs_start in fsfilt is used by lustre
400 * the journal blocks of write rec are not counted in FIXME later */
401 if (fsfilt->fs_start)
402 return fsfilt->fs_start(inode, op, desc_private, 0);
406 void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
408 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
413 CDEBUG(D_INFO, "trans commit %p\n", fsfilt->fs_commit);
415 if (fsfilt->fs_commit)
416 fsfilt->fs_commit(inode->i_sb, inode, handle, force_sync);
420 int smfs_register_plugin(struct super_block * sb, struct smfs_plugin * new_plugin)
422 struct smfs_plugin * plg = NULL;
423 struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
427 list_for_each_entry(plg, plist, plg_list) {
428 if (plg->plg_type == new_plugin->plg_type) {
429 CWARN("Plugin is already registered\n");
434 OBD_ALLOC(plg, sizeof(*plg));
436 CWARN("Cannot allocate memory for plugin\n");
440 memcpy(plg, new_plugin, sizeof(*plg));
441 list_add_tail(&plg->plg_list, plist);
446 void * smfs_deregister_plugin(struct super_block * sb, int type)
448 struct smfs_plugin * plg = NULL;
449 struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
453 list_for_each_entry(plg, plist, plg_list) {
454 if (plg->plg_type == type) {
455 list_del(&plg->plg_list);
456 priv = plg->plg_private;
457 OBD_FREE(plg, sizeof(*plg));