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 *);
222 int smfs_fill_super(struct super_block *sb, void *data, int silent)
224 struct inode *root_inode = NULL;
225 struct smfs_super_info *smb = NULL;
226 char *devstr = NULL, *typestr = NULL;
227 char *opts = NULL, *cache_data = NULL;
234 CDEBUG(D_SUPER, "mount opts: %s\n", data ?
235 (char *)data : "(none)");
237 smb = smfs_init_smb(sb);
241 page = __get_free_page(GFP_KERNEL);
243 GOTO(out_err, err = -ENOMEM);
245 memset((void *)page, 0, PAGE_SIZE);
248 cache_data = smfs_options(data, &devstr, &typestr, opts,
251 CWARN("smfs_fill_super(): options parsing stoped at "
252 "option %s\n", cache_data);
255 if (!typestr || !devstr) {
256 CERROR("mount options name and dev are mandatory\n");
258 GOTO(out_err, err = -EINVAL);
261 err = smfs_mount_cache(smb, devstr, typestr, opts);
265 CERROR("Can not mount %s as %s\n", devstr, typestr);
269 duplicate_sb(sb, smb->smsi_sb);
270 sb->s_bdev = smb->smsi_sb->s_bdev;
271 sm_set_sb_ops(smb->smsi_sb, sb);
273 err = smfs_init_hook_ops(smb);
275 CERROR("Can not init super hook ops err %d\n", err);
276 smfs_umount_cache(smb);
280 /* init the root_inode of smfs. */
281 //dget(S2CSB(sb)->s_root);
282 root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
283 root_inode = smfs_get_inode(sb, root_ino, NULL, 0);
285 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
286 sb->s_op->read_inode, root_ino, root_inode);
288 sb->s_root = d_alloc_root(root_inode);
291 smfs_umount_cache(smb);
292 GOTO(out_err, err = -ENOMEM);
295 err = smfs_init_hooks(sb);
297 smfs_umount_cache(smb);
300 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
301 CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
302 (ulong)sb, (ulong)&sb->u.generic_sbp);
304 CDEBUG(D_SUPER, "sb %lx(%p), &sb->s_fs_info: %lx\n",
305 (ulong)sb, smb->smsi_sb, (ulong)&sb->s_fs_info);
311 smfs_cleanup_smb(sb);
315 struct smfs_hook_ops *smfs_alloc_hook_ops(char *name, smfs_hook_func pre_hook,
316 smfs_hook_func post_hook)
318 struct smfs_hook_ops *smfs_hops = NULL;
321 OBD_ALLOC(smfs_hops, sizeof(struct smfs_hook_ops));
326 OBD_ALLOC(smfs_hops->smh_name, strlen(name) + 1);
328 if (!smfs_hops->smh_name) {
329 OBD_FREE(smfs_hops, sizeof(struct smfs_hook_ops));
333 memcpy(smfs_hops->smh_name, name, strlen(name));
335 smfs_hops->smh_post_op = post_hook;
336 smfs_hops->smh_pre_op = pre_hook;
341 void smfs_free_hook_ops(struct smfs_hook_ops *hops)
345 OBD_FREE(hops->smh_name, strlen(hops->smh_name) + 1);
347 OBD_FREE(hops, sizeof(struct smfs_hook_ops));
351 int smfs_register_hook_ops(struct smfs_super_info *smb,
352 struct smfs_hook_ops *smh_ops)
354 struct list_head *hlist = &smb->smsi_hook_list;
358 list_for_each(p, hlist) {
359 struct smfs_hook_ops *found;
360 found = list_entry(p, struct smfs_hook_ops, smh_list);
361 if (!strcmp(found->smh_name, smh_ops->smh_name)) {
362 CWARN("hook ops %s list reregister\n", smh_ops->smh_name);
366 list_add(&smh_ops->smh_list, hlist);
370 struct smfs_hook_ops *smfs_unregister_hook_ops(struct smfs_super_info *smb,
373 struct list_head *hlist = &smb->smsi_hook_list;
377 list_for_each(p, hlist) {
378 struct smfs_hook_ops *found;
380 found = list_entry(p, typeof(*found), smh_list);
381 if (!memcmp(found->smh_name, name, strlen(name))) {
390 void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
392 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
394 CDEBUG(D_INFO, "trans start %p\n", fsfilt->fs_start);
396 SMFS_TRANS_OP(inode, op);
398 /* There are some problem here. fs_start in fsfilt is used by lustre
399 * the journal blocks of write rec are not counted in FIXME later */
400 if (fsfilt->fs_start)
401 return fsfilt->fs_start(inode, op, desc_private, 0);
405 void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
407 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
412 CDEBUG(D_INFO, "trans commit %p\n", fsfilt->fs_commit);
414 if (fsfilt->fs_commit)
415 fsfilt->fs_commit(inode->i_sb, inode, handle, force_sync);
419 int smfs_register_plugin(struct super_block * sb, struct smfs_plugin * new_plugin)
421 struct smfs_plugin * plg = NULL;
422 struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
426 list_for_each_entry(plg, plist, plg_list) {
427 if (plg->plg_type == new_plugin->plg_type) {
428 CWARN("Plugin is already registered\n");
433 OBD_ALLOC(plg, sizeof(*plg));
435 CWARN("Cannot allocate memory for plugin\n");
439 memcpy(plg, new_plugin, sizeof(*plg));
440 list_add_tail(&plg->plg_list, plist);
445 void * smfs_deregister_plugin(struct super_block * sb, int type)
447 struct smfs_plugin * plg = NULL;
448 struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
452 list_for_each_entry(plg, plist, plg_list) {
453 if (plg->plg_type == type) {
454 list_del(&plg->plg_list);
455 priv = plg->plg_private;
456 OBD_FREE(plg, sizeof(*plg));