Whamcloud - gitweb
update .snap on smfs
[fs/lustre-release.git] / lustre / smfs / smfs_lib.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                           char *opts, int *flags)  
50 {
51         struct option *opt_value = NULL;
52         char   *pos;
53         
54         LASSERT(opts && flags);
55
56         while (!(get_opt(&opt_value, &pos))) {
57                 if (!strcmp(opt_value->opt, "dev")) {
58                         if (devstr != NULL)
59                                 *devstr = opt_value->value;
60                 } else if (!strcmp(opt_value->opt, "type")) {
61                         if (namestr != NULL)
62                                 *namestr = opt_value->value;
63                 } else if (!strcmp(opt_value->opt, "kml")) {
64                         *flags |= SM_DO_REC;
65                 } else if (!strcmp(opt_value->opt, "cache")) {
66                         *flags |= SM_CACHE_HOOK;
67                 } else if (!strcmp(opt_value->opt, "snap")) {
68                         *flags |= SM_DO_COW;
69                 } else if (!strcmp(opt_value->opt, "options")) {
70                         if (strlen(opts) == 0)
71                                 sprintf((char *)opts + strlen(opts), "%s",
72                                         opt_value->value);
73                         else  
74                                 sprintf((char *)opts + strlen(opts), ",%s",
75                                         opt_value->value);
76                 } else {
77                         /*FIXME:WANGDI How about the opt_value->value*/
78                         if (strlen(opts) == 0)
79                                 sprintf((char *)opts + strlen(opts), "%s",
80                                         opt_value->opt);
81                         else  
82                                 sprintf((char *)opts + strlen(opts), ",%s",
83                                         opt_value->opt);
84                 }
85         }
86         return pos;
87 }
88 static struct smfs_super_info *smfs_init_smb(struct super_block *sb)
89 {
90         struct smfs_super_info *smb;
91         ENTRY;
92
93         OBD_ALLOC(smb, sizeof(*smb));
94         if (!smb)
95                 RETURN(NULL);        
96         
97         S2SMI(sb) = smb;
98         RETURN(smb);        
99 }
100 static int smfs_init_fsfilt_ops(struct smfs_super_info *smb)
101 {
102         ENTRY;
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);
108                         RETURN(-EINVAL);
109                 }
110         }
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",
115                                smb->smsi_ftype);
116                         RETURN(-EINVAL);
117                 }
118         }
119         RETURN(0);
120 }
121
122 void smfs_cleanup_fsfilt_ops(struct smfs_super_info *smb)
123 {
124         if (smb->sm_cache_fsfilt)
125                 fsfilt_put_ops(smb->sm_cache_fsfilt);
126         if (smb->sm_fsfilt)
127                 fsfilt_put_ops(smb->sm_fsfilt);
128 }
129
130 static int smfs_mount_cache(struct smfs_super_info *smb, char *devstr, 
131                             char *typestr, char *opts)
132 {
133         int err = 0, typelen;
134         struct vfsmount *mnt;
135         ENTRY;
136
137         typelen = strlen(typestr);
138
139         printk("smfs: mounting %s at %s\n", typestr, devstr);
140
141         mnt = do_kern_mount(typestr, 0, devstr, (void *)opts);
142
143         if (IS_ERR(mnt)) {
144                 CERROR("do_kern_mount failed: rc = %ld\n", PTR_ERR(mnt));
145                 GOTO(err_out, err = PTR_ERR(mnt));
146         }
147
148         smb->smsi_sb = mnt->mnt_sb;
149         smb->smsi_mnt = mnt;
150
151         smfs_init_sm_ops(smb);
152
153         OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
154         memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
155
156         OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
157         memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
158         
159         err = smfs_init_fsfilt_ops(smb);
160 err_out:
161         RETURN(err);
162 }
163
164 static int smfs_umount_cache(struct smfs_super_info *smb)
165 {
166         mntput(smb->smsi_mnt);
167         smfs_cleanup_sm_ops(smb);
168         smfs_cleanup_fsfilt_ops(smb);
169
170         if (smb->smsi_cache_ftype)
171                 OBD_FREE(smb->smsi_cache_ftype,
172                          strlen(smb->smsi_cache_ftype) + 1);
173         if (smb->smsi_ftype)
174                 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
175                
176         return 0;
177 }
178
179 static int smfs_init_hook_ops(struct smfs_super_info *smb)
180 {
181         ENTRY;
182
183         INIT_LIST_HEAD(&smb->smsi_hook_list);
184
185         RETURN(0); 
186 }
187 static void smfs_cleanup_hook_ops(struct smfs_super_info *smb)
188 {
189         struct list_head *hlist = &smb->smsi_hook_list;
190         ENTRY;
191
192         while (!list_empty(hlist)) {
193                 struct smfs_hook_ops *smfs_hops;
194                 
195                 smfs_hops = list_entry(hlist->next, struct smfs_hook_ops, 
196                                        smh_list);
197                 CERROR("Unregister %s hook ops\n", smfs_hops->smh_name);         
198                 
199                 smfs_unregister_hook_ops(smb, smfs_hops->smh_name);
200                 smfs_free_hook_ops(smfs_hops); 
201         } 
202         EXIT;
203         return;        
204 }
205
206 static void smfs_cleanup_smb(struct super_block *sb)
207 {
208         struct smfs_super_info *smb;
209         ENTRY;
210
211         smb = S2SMI(sb);
212         if (smb) 
213                 OBD_FREE(smb, sizeof(*smb));
214         EXIT;
215         return;
216 }
217 void smfs_cleanup_hooks(struct smfs_super_info *smb)
218 {
219         
220         if (SMFS_CACHE_HOOK(smb))
221                 cache_space_hook_exit(smb);
222         if (SMFS_DO_REC(smb))
223                 smfs_rec_cleanup(smb);
224 #if CONFIG_SNAPFS
225         if (SMFS_DO_COW(smb))
226                 smfs_cow_cleanup(smb);
227 #endif  
228         smfs_cleanup_hook_ops(smb);
229 }
230
231 void smfs_put_super(struct super_block *sb)
232 {
233         struct smfs_super_info *smfs_info = S2SMI(sb);
234
235 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
236         smfs_cleanup_hooks(smfs_info);
237 #endif
238         if (sb)
239                 smfs_umount_cache(smfs_info);
240         smfs_cleanup_smb(sb); 
241         return;
242 }
243
244 static int smfs_init_hooks(struct super_block *sb)
245
246         ENTRY;
247  
248         if (SMFS_DO_REC(S2SMI(sb))) 
249                 smfs_rec_init(sb);
250         if (SMFS_CACHE_HOOK(S2SMI(sb))) 
251                 cache_space_hook_init(sb);
252 #if CONFIG_SNAPFS
253         if (SMFS_DO_COW(S2SMI(sb))) 
254                 smfs_cow_init(sb);
255 #endif
256         RETURN(0);
257 }
258
259 int smfs_fill_super(struct super_block *sb, void *data, int silent)
260 {
261         struct inode *root_inode = NULL;
262         struct smfs_super_info *smb = NULL;
263         char *devstr = NULL, *typestr = NULL; 
264         char *opts = NULL, *cache_data = NULL;
265         unsigned long page;
266         int err = 0; 
267         ino_t root_ino;
268
269         ENTRY;
270
271         CDEBUG(D_SUPER, "mount opts: %s\n", data ?  (char *)data : "(none)");
272
273         smb = smfs_init_smb(sb);
274         if (!smb)
275                 RETURN(-ENOMEM);
276  
277         page = __get_free_page(GFP_KERNEL);
278         if (!page)
279                 GOTO(out_err, err = -ENOMEM);
280                                                                                                                                                                                                      
281         memset((void *)page, 0, PAGE_SIZE);
282         opts = (char *)page;
283
284         init_option(data);
285         cache_data = smfs_options(data, &devstr, &typestr, opts, 
286                                   &smb->smsi_flags); 
287         if (*cache_data)
288                 CWARN("smfs_fill_super(): options parsing stoped at "
289                       "option %s\n", cache_data);
290
291         if (!typestr || !devstr) {
292                 CERROR("mount options name and dev mandatory\n");
293                 free_page(page);
294                 GOTO(out_err, err = -EINVAL);
295         }
296         err = smfs_mount_cache(smb, devstr, typestr, opts);
297         
298         free_page(page);
299         
300         if (err) {
301                 CERROR("Can not mount %s as %s\n", devstr, typestr);
302                 GOTO(out_err, 0);
303         }
304
305         duplicate_sb(sb, smb->smsi_sb);
306         sm_set_sb_ops(smb->smsi_sb, sb);
307
308         err = smfs_init_hook_ops(smb);
309         if (err) {
310                 CERROR("Can not init super hook ops err %d\n", err);
311                 GOTO(out_err, 0);
312         }
313         /*init the root_inode of smfs*/ 
314         dget(S2CSB(sb)->s_root);
315         root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
316         root_inode = smfs_get_inode(sb, root_ino, NULL, 0);
317
318         CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
319                sb->s_op->read_inode, root_ino, root_inode);
320
321         sb->s_root = d_alloc_root(root_inode);
322
323         if (!sb->s_root) {
324                 smfs_umount_cache(smb);
325                 GOTO(out_err, err=-EINVAL);
326         }
327         
328         err = smfs_init_hooks(sb);  
329         if (err) {
330                 smfs_umount_cache(smb);
331                 GOTO(out_err, err=-EINVAL);        
332         }       
333 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
334         CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
335                (ulong)sb, (ulong)&sb->u.generic_sbp);
336 #else
337         CDEBUG(D_SUPER, "sb %lx, &sb->s_fs_info: %lx\n",
338                (ulong)sb, (ulong)&sb->s_fs_info);
339 #endif
340 out_err:
341         cleanup_option();
342         if (err)
343                 smfs_cleanup_smb(sb);
344         return err;
345 }
346 struct smfs_hook_ops *smfs_alloc_hook_ops(char *name, smfs_hook_func pre_hook, 
347                                           smfs_hook_func post_hook)
348 {
349         struct smfs_hook_ops *smfs_hops = NULL;
350         
351         ENTRY;
352         OBD_ALLOC(smfs_hops, sizeof(struct smfs_hook_ops));
353
354         if (!smfs_hops)
355                 RETURN(NULL);
356  
357         OBD_ALLOC(smfs_hops->smh_name, strlen(name) + 1);
358         
359         if (!smfs_hops->smh_name) { 
360                 OBD_FREE(smfs_hops, sizeof(struct smfs_hook_ops));
361                 RETURN(NULL);
362         }
363         
364         memcpy(smfs_hops->smh_name, name, strlen(name));  
365        
366         smfs_hops->smh_post_op = post_hook;  
367         smfs_hops->smh_pre_op = pre_hook;  
368         
369         RETURN(smfs_hops); 
370 }
371
372 void smfs_free_hook_ops(struct smfs_hook_ops *hops)
373 {
374         if (hops) {
375                 if (hops->smh_name){
376                         OBD_FREE(hops->smh_name, strlen(hops->smh_name) + 1);
377                 }
378                 OBD_FREE(hops, sizeof(struct smfs_hook_ops));
379         }
380 }
381
382 int smfs_register_hook_ops(struct smfs_super_info *smb, 
383                            struct smfs_hook_ops *smh_ops)
384 {
385         struct list_head *hlist = &smb->smsi_hook_list;
386         struct list_head *p;
387         ENTRY;
388  
389         list_for_each(p, hlist) {
390                 struct smfs_hook_ops *found;               
391                 found = list_entry(p, struct smfs_hook_ops, smh_list);
392                 if (!strcmp(found->smh_name, smh_ops->smh_name)) {
393                         CWARN("hook ops %s list  reregister\n", smh_ops->smh_name);
394                         RETURN(0);
395                 }
396         }
397         list_add(&smh_ops->smh_list, hlist);
398         RETURN(0);
399
400 struct smfs_hook_ops *smfs_unregister_hook_ops(struct smfs_super_info *smb, 
401                                                char *name)
402 {
403         struct list_head *hlist = &smb->smsi_hook_list;
404         struct list_head *p;
405         ENTRY;      
406  
407         list_for_each(p, hlist) {
408                 struct smfs_hook_ops *found;
409
410                 found = list_entry(p, typeof(*found), smh_list);
411                 if (!memcmp(found->smh_name, name, strlen(name))) {
412                         list_del(p);
413                         RETURN(found);
414                 }
415         } 
416         RETURN(NULL);
417 }
418
419 void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
420 {
421         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
422
423         CDEBUG(D_INFO, "trans start %p\n", fsfilt->fs_start);
424
425         SMFS_TRANS_OP(inode, op);
426         
427         /* There are some problem here. fs_start in fsfilt is used by lustre
428          * the journal blocks of write rec are not counted in FIXME later */
429         if (fsfilt->fs_start)
430                 return fsfilt->fs_start(inode, op, desc_private, 0);
431         return NULL;
432 }
433
434 void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
435 {
436         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
437
438         if (!handle)
439                 return;
440
441         CDEBUG(D_INFO, "trans commit %p\n", fsfilt->fs_commit);
442
443         if (fsfilt->fs_commit)
444                 fsfilt->fs_commit(inode->i_sb, inode, handle, force_sync);
445 }
446