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 char *opts, int *flags)
51 struct option *opt_value = NULL;
54 LASSERT(opts && flags);
56 while (!(get_opt(&opt_value, &pos))) {
57 if (!strcmp(opt_value->opt, "dev")) {
59 *devstr = opt_value->value;
60 } else if (!strcmp(opt_value->opt, "type")) {
62 *namestr = opt_value->value;
63 } else if (!strcmp(opt_value->opt, "kml")) {
65 } else if (!strcmp(opt_value->opt, "cache")) {
66 *flags |= SM_CACHE_HOOK;
67 } else if (!strcmp(opt_value->opt, "snap")) {
69 } else if (!strcmp(opt_value->opt, "options")) {
70 if (strlen(opts) == 0)
71 sprintf((char *)opts + strlen(opts), "%s",
74 sprintf((char *)opts + strlen(opts), ",%s",
77 /*FIXME:WANGDI How about the opt_value->value*/
78 if (strlen(opts) == 0)
79 sprintf((char *)opts + strlen(opts), "%s",
82 sprintf((char *)opts + strlen(opts), ",%s",
88 static struct smfs_super_info *smfs_init_smb(struct super_block *sb)
90 struct smfs_super_info *smb;
93 OBD_ALLOC(smb, sizeof(*smb));
100 static int smfs_init_fsfilt_ops(struct smfs_super_info *smb)
103 if (!smb->sm_cache_fsfilt) {
104 smb->sm_cache_fsfilt = fsfilt_get_ops(smb->smsi_cache_ftype);
105 if (!smb->sm_cache_fsfilt) {
106 CERROR("Can not get %s fsfilt ops needed by kml\n",
107 smb->smsi_cache_ftype);
111 if (!smb->sm_fsfilt) {
112 smb->sm_fsfilt = fsfilt_get_ops(smb->smsi_ftype);
113 if (!smb->sm_fsfilt) {
114 CERROR("Can not get %s fsfilt ops needed by kml\n",
122 void smfs_cleanup_fsfilt_ops(struct smfs_super_info *smb)
124 if (smb->sm_cache_fsfilt)
125 fsfilt_put_ops(smb->sm_cache_fsfilt);
127 fsfilt_put_ops(smb->sm_fsfilt);
130 static int smfs_mount_cache(struct smfs_super_info *smb, char *devstr,
131 char *typestr, char *opts)
133 int err = 0, typelen;
134 struct vfsmount *mnt;
137 typelen = strlen(typestr);
139 printk("smfs: mounting %s at %s\n", typestr, devstr);
141 mnt = do_kern_mount(typestr, 0, devstr, (void *)opts);
144 CERROR("do_kern_mount failed: rc = %ld\n", PTR_ERR(mnt));
145 GOTO(err_out, err = PTR_ERR(mnt));
148 smb->smsi_sb = mnt->mnt_sb;
151 smfs_init_sm_ops(smb);
153 OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
154 memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
156 OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
157 memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
159 err = smfs_init_fsfilt_ops(smb);
164 static int smfs_umount_cache(struct smfs_super_info *smb)
166 struct dentry *root = smb->smsi_sb->s_root;
169 if (atomic_read(&root->d_inode->i_count) == 0)
170 igrab(root->d_inode);
172 mntput(smb->smsi_mnt);
173 smfs_cleanup_sm_ops(smb);
174 smfs_cleanup_fsfilt_ops(smb);
176 if (smb->smsi_cache_ftype)
177 OBD_FREE(smb->smsi_cache_ftype,
178 strlen(smb->smsi_cache_ftype) + 1);
180 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
185 static int smfs_init_hook_ops(struct smfs_super_info *smb)
189 INIT_LIST_HEAD(&smb->smsi_hook_list);
193 static void smfs_cleanup_hook_ops(struct smfs_super_info *smb)
195 struct list_head *hlist = &smb->smsi_hook_list;
198 while (!list_empty(hlist)) {
199 struct smfs_hook_ops *smfs_hops;
201 smfs_hops = list_entry(hlist->next, struct smfs_hook_ops,
203 CERROR("Unregister %s hook ops\n", smfs_hops->smh_name);
205 smfs_unregister_hook_ops(smb, smfs_hops->smh_name);
206 smfs_free_hook_ops(smfs_hops);
212 static void smfs_cleanup_smb(struct super_block *sb)
214 struct smfs_super_info *smb;
219 OBD_FREE(smb, sizeof(*smb));
223 void smfs_cleanup_hooks(struct smfs_super_info *smb)
226 if (SMFS_CACHE_HOOK(smb))
227 cache_space_hook_exit(smb);
228 if (SMFS_DO_REC(smb))
229 smfs_rec_cleanup(smb);
231 if (SMFS_DO_COW(smb))
232 smfs_cow_cleanup(smb);
234 smfs_cleanup_hook_ops(smb);
237 void smfs_put_super(struct super_block *sb)
239 struct smfs_super_info *smfs_info = S2SMI(sb);
241 smfs_cleanup_hooks(smfs_info);
244 smfs_umount_cache(smfs_info);
245 smfs_cleanup_smb(sb);
249 static int smfs_init_hooks(struct super_block *sb)
253 if (SMFS_DO_REC(S2SMI(sb)))
255 if (SMFS_CACHE_HOOK(S2SMI(sb)))
256 cache_space_hook_init(sb);
258 if (SMFS_DO_COW(S2SMI(sb)))
264 int smfs_fill_super(struct super_block *sb, void *data, int silent)
266 struct inode *root_inode = NULL;
267 struct smfs_super_info *smb = NULL;
268 char *devstr = NULL, *typestr = NULL;
269 char *opts = NULL, *cache_data = NULL;
276 CDEBUG(D_SUPER, "mount opts: %s\n", data ? (char *)data : "(none)");
278 smb = smfs_init_smb(sb);
282 page = __get_free_page(GFP_KERNEL);
284 GOTO(out_err, err = -ENOMEM);
286 memset((void *)page, 0, PAGE_SIZE);
290 cache_data = smfs_options(data, &devstr, &typestr, opts,
293 CWARN("smfs_fill_super(): options parsing stoped at "
294 "option %s\n", cache_data);
296 if (!typestr || !devstr) {
297 CERROR("mount options name and dev mandatory\n");
299 GOTO(out_err, err = -EINVAL);
301 err = smfs_mount_cache(smb, devstr, typestr, opts);
306 CERROR("Can not mount %s as %s\n", devstr, typestr);
310 duplicate_sb(sb, smb->smsi_sb);
311 sm_set_sb_ops(smb->smsi_sb, sb);
313 err = smfs_init_hook_ops(smb);
315 CERROR("Can not init super hook ops err %d\n", err);
318 /*init the root_inode of smfs*/
319 dget(S2CSB(sb)->s_root);
320 root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
321 root_inode = smfs_get_inode(sb, root_ino, NULL, 0);
323 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
324 sb->s_op->read_inode, root_ino, root_inode);
326 sb->s_root = d_alloc_root(root_inode);
329 smfs_umount_cache(smb);
330 GOTO(out_err, err=-EINVAL);
333 err = smfs_init_hooks(sb);
335 smfs_umount_cache(smb);
336 GOTO(out_err, err=-EINVAL);
338 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
339 CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
340 (ulong)sb, (ulong)&sb->u.generic_sbp);
342 CDEBUG(D_SUPER, "sb %lx, &sb->s_fs_info: %lx\n",
343 (ulong)sb, (ulong)&sb->s_fs_info);
348 smfs_cleanup_smb(sb);
351 struct smfs_hook_ops *smfs_alloc_hook_ops(char *name, smfs_hook_func pre_hook,
352 smfs_hook_func post_hook)
354 struct smfs_hook_ops *smfs_hops = NULL;
357 OBD_ALLOC(smfs_hops, sizeof(struct smfs_hook_ops));
362 OBD_ALLOC(smfs_hops->smh_name, strlen(name) + 1);
364 if (!smfs_hops->smh_name) {
365 OBD_FREE(smfs_hops, sizeof(struct smfs_hook_ops));
369 memcpy(smfs_hops->smh_name, name, strlen(name));
371 smfs_hops->smh_post_op = post_hook;
372 smfs_hops->smh_pre_op = pre_hook;
377 void smfs_free_hook_ops(struct smfs_hook_ops *hops)
381 OBD_FREE(hops->smh_name, strlen(hops->smh_name) + 1);
383 OBD_FREE(hops, sizeof(struct smfs_hook_ops));
387 int smfs_register_hook_ops(struct smfs_super_info *smb,
388 struct smfs_hook_ops *smh_ops)
390 struct list_head *hlist = &smb->smsi_hook_list;
394 list_for_each(p, hlist) {
395 struct smfs_hook_ops *found;
396 found = list_entry(p, struct smfs_hook_ops, smh_list);
397 if (!strcmp(found->smh_name, smh_ops->smh_name)) {
398 CWARN("hook ops %s list reregister\n", smh_ops->smh_name);
402 list_add(&smh_ops->smh_list, hlist);
405 struct smfs_hook_ops *smfs_unregister_hook_ops(struct smfs_super_info *smb,
408 struct list_head *hlist = &smb->smsi_hook_list;
412 list_for_each(p, hlist) {
413 struct smfs_hook_ops *found;
415 found = list_entry(p, typeof(*found), smh_list);
416 if (!memcmp(found->smh_name, name, strlen(name))) {
424 void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
426 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
428 CDEBUG(D_INFO, "trans start %p\n", fsfilt->fs_start);
430 SMFS_TRANS_OP(inode, op);
432 /* There are some problem here. fs_start in fsfilt is used by lustre
433 * the journal blocks of write rec are not counted in FIXME later */
434 if (fsfilt->fs_start)
435 return fsfilt->fs_start(inode, op, desc_private, 0);
439 void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
441 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
446 CDEBUG(D_INFO, "trans commit %p\n", fsfilt->fs_commit);
448 if (fsfilt->fs_commit)
449 fsfilt->fs_commit(inode->i_sb, inode, handle, force_sync);