Whamcloud - gitweb
- landed b_hd_cray_merge3
[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
49 struct list_head smfs_plg_list;
50
51 static struct smfs_super_info *smfs_init_smb(struct super_block *sb)
52 {
53         struct smfs_super_info *smb;
54         ENTRY;
55
56         OBD_ALLOC(smb, sizeof(*smb));
57         if (!smb)
58                 RETURN(NULL);        
59         
60         S2FSI(sb) = smb;
61         RETURN(smb);        
62 }
63
64 static int smfs_init_fsfilt_ops(struct smfs_super_info *smb)
65 {
66         ENTRY;
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);
73                         RETURN(-EINVAL);
74                 }
75         }
76         if (!smb->sm_fsfilt) {
77                 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",
81                                smb->smsi_ftype);
82                         RETURN(-EINVAL);
83                 }
84         }
85         RETURN(0);
86 }
87
88 void smfs_cleanup_fsfilt_ops(struct smfs_super_info *smb)
89 {
90         if (smb->sm_cache_fsfilt)
91                 fsfilt_put_ops(smb->sm_cache_fsfilt);
92         if (smb->sm_fsfilt)
93                 fsfilt_put_ops(smb->sm_fsfilt);
94 }
95
96 static int smfs_mount_cache(struct smfs_super_info *smb, char *devstr, 
97                             char *typestr, char *opts)
98 {
99         int err = 0, typelen;
100         struct vfsmount *mnt;
101         ENTRY;
102
103         typelen = strlen(typestr);
104
105         printk("smfs: mounting %s at %s\n", typestr, devstr);
106         mnt = do_kern_mount(typestr, 0, devstr, (void *)opts);
107         if (IS_ERR(mnt)) {
108                 CERROR("do_kern_mount failed: rc = %ld\n", 
109                        PTR_ERR(mnt));
110                 GOTO(err_out, err = PTR_ERR(mnt));
111         }
112
113         smb->smsi_sb = mnt->mnt_sb;
114         smb->smsi_mnt = mnt;
115
116         smfs_init_sm_ops(smb);
117
118         OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
119         memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
120
121         OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
122         memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
123         
124         err = smfs_init_fsfilt_ops(smb);
125 err_out:
126         RETURN(err);
127 }
128
129 static int smfs_umount_cache(struct smfs_super_info *smb)
130 {
131         mntput(smb->smsi_mnt);
132         smfs_cleanup_sm_ops(smb);
133         smfs_cleanup_fsfilt_ops(smb);
134
135         if (smb->smsi_cache_ftype)
136                 OBD_FREE(smb->smsi_cache_ftype,
137                          strlen(smb->smsi_cache_ftype) + 1);
138         if (smb->smsi_ftype)
139                 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
140                
141         return 0;
142 }
143
144 static int smfs_init_hook_ops(struct smfs_super_info *smb)
145 {
146         ENTRY;
147         INIT_LIST_HEAD(&smb->smsi_hook_list);
148         INIT_LIST_HEAD(&smb->smsi_plg_list);
149         RETURN(0); 
150 }
151
152 static void smfs_cleanup_hook_ops(struct smfs_super_info *smb)
153 {
154         struct list_head *hlist = &smb->smsi_hook_list;
155         ENTRY;
156
157         while (!list_empty(hlist)) {
158                 struct smfs_hook_ops *smfs_hops;
159                 
160                 smfs_hops = list_entry(hlist->next, struct smfs_hook_ops, 
161                                        smh_list);
162                 CERROR("Unregister %s hook ops\n", smfs_hops->smh_name);         
163                 
164                 smfs_unregister_hook_ops(smb, smfs_hops->smh_name);
165                 smfs_free_hook_ops(smfs_hops); 
166         } 
167         EXIT;
168 }
169 static void smfs_cleanup_smb(struct super_block *sb)
170 {
171         struct smfs_super_info *smb;
172         ENTRY;
173
174         smb = S2SMI(sb);
175         if (smb) 
176                 OBD_FREE(smb, sizeof(*smb));
177         EXIT;
178 }
179
180 void smfs_cleanup_hooks(struct smfs_super_info *smb)
181 {
182         
183         if (SMFS_CACHE_HOOK(smb))
184                 cache_space_hook_exit(smb);
185         if (SMFS_DO_REC(smb))
186                 smfs_rec_cleanup(smb);
187 #if CONFIG_SNAPFS
188         if (SMFS_DO_COW(smb))
189                 smfs_cow_cleanup(smb);
190 #endif  
191         smfs_cleanup_hook_ops(smb);
192 }
193
194 void smfs_put_super(struct super_block *sb)
195 {
196         struct smfs_super_info *smfs_info = S2SMI(sb);
197
198         smfs_cleanup_hooks(smfs_info);
199         
200         if (sb)
201                 smfs_umount_cache(smfs_info);
202         smfs_cleanup_smb(sb); 
203 }
204
205 static int smfs_init_hooks(struct super_block *sb)
206
207         ENTRY;
208  
209         if (SMFS_DO_REC(S2SMI(sb))) 
210                 smfs_rec_init(sb);
211         if (SMFS_CACHE_HOOK(S2SMI(sb))) 
212                 cache_space_hook_init(sb);
213 #if CONFIG_SNAPFS
214         if (SMFS_DO_COW(S2SMI(sb))) 
215                 smfs_cow_init(sb);
216 #endif
217         RETURN(0);
218 }
219
220 extern char* smfs_options(char*, char**, char**, char*, int *);
221 extern void cleanup_option(void);
222
223 int smfs_fill_super(struct super_block *sb, void *data, int silent)
224 {
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;
229         unsigned long page;
230         int err = 0; 
231         ino_t root_ino;
232
233         ENTRY;
234
235         CDEBUG(D_SUPER, "mount opts: %s\n", data ? 
236                (char *)data : "(none)");
237
238         smb = smfs_init_smb(sb);
239         if (!smb)
240                 RETURN(-ENOMEM);
241  
242         page = __get_free_page(GFP_KERNEL);
243         if (!page)
244                 GOTO(out_err, err = -ENOMEM);
245         
246         memset((void *)page, 0, PAGE_SIZE);
247         opts = (char *)page;
248
249         cache_data = smfs_options(data, &devstr, &typestr, opts, 
250                                   &smb->smsi_flags); 
251         if (*cache_data) {
252                 CWARN("smfs_fill_super(): options parsing stoped at "
253                       "option %s\n", cache_data);
254         }
255
256         if (!typestr || !devstr) {
257                 CERROR("mount options name and dev are mandatory\n");
258                 free_page(page);
259                 GOTO(out_err, err = -EINVAL);
260         }
261         
262         err = smfs_mount_cache(smb, devstr, typestr, opts);
263         free_page(page);
264         
265         if (err) {
266                 CERROR("Can not mount %s as %s\n", devstr, typestr);
267                 GOTO(out_err, err);
268         }
269
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);
273
274         err = smfs_init_hook_ops(smb);
275         if (err) {
276                 CERROR("Can not init super hook ops err %d\n", err);
277                 smfs_umount_cache(smb);
278                 GOTO(out_err, err);
279         }
280         
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);
285
286         CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
287                sb->s_op->read_inode, root_ino, root_inode);
288
289         sb->s_root = d_alloc_root(root_inode);
290
291         if (!sb->s_root) {
292                 smfs_umount_cache(smb);
293                 GOTO(out_err, err = -ENOMEM);
294         }
295         
296         err = smfs_init_hooks(sb);  
297         if (err) {
298                 smfs_umount_cache(smb);
299                 GOTO(out_err, err);
300         }       
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);
304 #else
305         CDEBUG(D_SUPER, "sb %lx(%p), &sb->s_fs_info: %lx\n",
306                (ulong)sb, smb->smsi_sb, (ulong)&sb->s_fs_info);
307 #endif
308         
309 out_err:
310         cleanup_option();
311         if (err)
312                 smfs_cleanup_smb(sb);
313         return err;
314 }
315
316 struct smfs_hook_ops *smfs_alloc_hook_ops(char *name, smfs_hook_func pre_hook, 
317                                           smfs_hook_func post_hook)
318 {
319         struct smfs_hook_ops *smfs_hops = NULL;
320         
321         ENTRY;
322         OBD_ALLOC(smfs_hops, sizeof(struct smfs_hook_ops));
323
324         if (!smfs_hops)
325                 RETURN(NULL);
326  
327         OBD_ALLOC(smfs_hops->smh_name, strlen(name) + 1);
328         
329         if (!smfs_hops->smh_name) { 
330                 OBD_FREE(smfs_hops, sizeof(struct smfs_hook_ops));
331                 RETURN(NULL);
332         }
333         
334         memcpy(smfs_hops->smh_name, name, strlen(name));  
335        
336         smfs_hops->smh_post_op = post_hook;  
337         smfs_hops->smh_pre_op = pre_hook;  
338         
339         RETURN(smfs_hops); 
340 }
341
342 void smfs_free_hook_ops(struct smfs_hook_ops *hops)
343 {
344         if (hops) {
345                 if (hops->smh_name){
346                         OBD_FREE(hops->smh_name, strlen(hops->smh_name) + 1);
347                 }
348                 OBD_FREE(hops, sizeof(struct smfs_hook_ops));
349         }
350 }
351
352 int smfs_register_hook_ops(struct smfs_super_info *smb, 
353                            struct smfs_hook_ops *smh_ops)
354 {
355         struct list_head *hlist = &smb->smsi_hook_list;
356         struct list_head *p;
357         ENTRY;
358  
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);
364                         RETURN(0);
365                 }
366         }
367         list_add(&smh_ops->smh_list, hlist);
368         RETURN(0);
369
370
371 struct smfs_hook_ops *smfs_unregister_hook_ops(struct smfs_super_info *smb, 
372                                                char *name)
373 {
374         struct list_head *hlist = &smb->smsi_hook_list;
375         struct list_head *p;
376         ENTRY;      
377  
378         list_for_each(p, hlist) {
379                 struct smfs_hook_ops *found;
380
381                 found = list_entry(p, typeof(*found), smh_list);
382                 if (!memcmp(found->smh_name, name, strlen(name))) {
383                         list_del(p);
384                         RETURN(found);
385                 }
386         }
387
388         RETURN(NULL);
389 }
390
391 void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
392 {
393         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
394
395         CDEBUG(D_INFO, "trans start %p\n", fsfilt->fs_start);
396
397         SMFS_TRANS_OP(inode, op);
398         
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);
403         return NULL;
404 }
405
406 void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
407 {
408         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
409
410         if (!handle)
411                 return;
412
413         CDEBUG(D_INFO, "trans commit %p\n", fsfilt->fs_commit);
414
415         if (fsfilt->fs_commit)
416                 fsfilt->fs_commit(inode->i_sb, inode, handle, force_sync);
417 }
418
419
420 int smfs_register_plugin(struct super_block * sb, struct smfs_plugin * new_plugin) 
421 {
422         struct smfs_plugin * plg = NULL;
423         struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
424         
425         ENTRY;
426         
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");
430                         RETURN(-EEXIST);
431                 }
432         }
433         
434         OBD_ALLOC(plg, sizeof(*plg));
435         if (!plg) {
436                 CWARN("Cannot allocate memory for plugin\n");
437                 RETURN(-ENOMEM);
438         }
439         
440         memcpy(plg, new_plugin, sizeof(*plg));
441         list_add_tail(&plg->plg_list, plist);
442                 
443         RETURN(0);
444 }
445
446 void * smfs_deregister_plugin(struct super_block * sb, int type)
447 {
448         struct smfs_plugin * plg = NULL;
449         struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
450         void * priv = NULL;
451         
452         ENTRY;
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));
458                         break;
459                 }
460         }
461                 
462         RETURN(priv);
463 }
464
465