Whamcloud - gitweb
- KML fixes and updates due to SMFS plugin API.
[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 int smfs_options(char *data, char **devstr, char **namestr, 
49                  char *ret, int *flags)  
50 {
51         char * temp;
52         char * pos = NULL, *next = NULL;
53                 
54         ENTRY;
55         
56         LASSERT(flags);
57         //allocate temporary buffer
58         OBD_ALLOC(temp, strlen(data) + 1);
59         if (!temp) {
60                 CERROR("Can not allocate memory for options\n");
61                 RETURN(-ENOMEM);
62         }
63         
64         memcpy(temp, data, strlen(data));
65         pos = temp;
66         
67         while (pos) {
68                 next = strchr(pos, ',');
69                 if (next) {
70                         *next = '\0';
71                         next++;
72                 }
73                 
74                 //now pos points to one-options string
75                 if (!strncmp(pos, "dev=", 4)) {
76                         if (devstr != NULL)
77                                 *devstr = pos + 4;
78                 } else if (!strncmp(pos, "type=", 5)) {
79                         if (namestr != NULL)
80                                 *namestr = pos + 5;
81                 } else if (!strcmp(pos, "kml")) {
82                         SMFS_SET(*flags, SMFS_PLG_KML);
83                 } else if (!strcmp(pos, "cache")) {
84                         SMFS_SET(*flags, SMFS_PLG_LRU);
85                 } else if (!strcmp(pos, "snap")) {
86                         SMFS_SET(*flags, SMFS_PLG_COW);
87                 } else {
88                         /* So it is wrong or backfs option,
89                          * let's save it
90                          */
91                         if (strlen(ret))
92                                 strcat(ret, ",");
93                         
94                         strcat(ret, pos);
95                 }
96                 
97                 pos = next;
98         }
99
100         //save dev & type for further use
101         if (*devstr)
102                 *devstr = strcpy(ret + strlen(ret) + 1, *devstr);
103         if (*namestr)
104                 *namestr = strcpy(*devstr + strlen(*devstr) + 1, *namestr);
105         
106         OBD_FREE(temp, strlen(data) + 1);
107         
108         RETURN(0);
109 }
110
111 static struct smfs_super_info *smfs_init_smb(struct super_block *sb)
112 {
113         struct smfs_super_info *smb;
114         ENTRY;
115
116         OBD_ALLOC(smb, sizeof(*smb));
117         if (!smb)
118                 RETURN(NULL);        
119         
120         S2FSI(sb) = smb;
121         INIT_LIST_HEAD(&smb->smsi_plg_list);
122         
123         RETURN(smb);        
124 }
125
126 static void smfs_cleanup_smb(struct smfs_super_info *smb)
127 {
128         ENTRY;
129
130         if (smb) 
131                 OBD_FREE(smb, sizeof(*smb));
132         EXIT;
133 }
134
135 static int smfs_init_fsfilt_ops(struct smfs_super_info *smb)
136 {
137         ENTRY;
138         if (!smb->sm_cache_fsfilt) {
139                 smb->sm_cache_fsfilt =
140                         fsfilt_get_ops(smb->smsi_cache_ftype);
141                 if (!smb->sm_cache_fsfilt) {
142                         CERROR("Can not get %s fsfilt ops needed by smfs\n",
143                                smb->smsi_cache_ftype);
144                         RETURN(-EINVAL);
145                 }
146         }
147         if (!smb->sm_fsfilt) {
148                 smb->sm_fsfilt =
149                         fsfilt_get_ops(smb->smsi_ftype);
150                 if (!smb->sm_fsfilt) {
151                         CERROR("Can not get %s fsfilt ops needed by smfs\n",
152                                smb->smsi_ftype);
153                         RETURN(-EINVAL);
154                 }
155         }
156         RETURN(0);
157 }
158
159 void smfs_cleanup_fsfilt_ops(struct smfs_super_info *smb)
160 {
161         if (smb->sm_cache_fsfilt)
162                 fsfilt_put_ops(smb->sm_cache_fsfilt);
163         if (smb->sm_fsfilt)
164                 fsfilt_put_ops(smb->sm_fsfilt);
165 }
166
167 static void smfs_filter_flags(struct filter_obd * filt, struct inode * o_dir)
168 {
169         struct dentry * dentry = NULL;
170         int i,j;
171         
172         CDEBUG(D_SUPER,"OST OBD post_setup\n");
173         /* enable plugins for all in O */
174         SMFS_SET(I2SMI(o_dir)->smi_flags, SMFS_PLG_ALL);
175         /* enable plugins for all already created d<n> dirs */
176         for (j = 1; j < filt->fo_group_count; j++) {
177                 for (i = 0; i < filt->fo_subdir_count; i++) {
178                         dentry = (filt->fo_subdirs + j)->dentry[i];
179                         SMFS_SET(I2SMI(dentry->d_inode)->smi_flags,
180                                          SMFS_PLG_ALL);
181                 }
182         }
183 }
184
185 static void smfs_mds_flags(struct mds_obd * mds, struct inode * root)
186 {
187         struct inode * pend = mds->mds_pending_dir->d_inode;
188         
189         CDEBUG(D_SUPER,"MDS OBD post_setup\n");
190         /* enable plugins for all in ROOT */        
191         SMFS_SET(I2SMI(root)->smi_flags, SMFS_PLG_ALL);
192         /* the same for PENDING */
193         SMFS_SET(I2SMI(pend)->smi_flags, SMFS_PLG_ALL);
194 }
195                         
196
197 int smfs_post_setup(struct obd_device *obd, struct vfsmount *mnt,
198                     struct dentry * root_dentry)
199 {
200         struct lvfs_run_ctxt saved, *current_ctxt = NULL;
201         struct smfs_super_info *smb = S2SMI(mnt->mnt_sb);
202         int rc = 0;
203         
204         ENTRY;
205  
206         OBD_ALLOC(current_ctxt, sizeof(*current_ctxt));
207         if (!current_ctxt)
208                 RETURN(-ENOMEM);
209         
210         OBD_SET_CTXT_MAGIC(current_ctxt);
211         
212         current_ctxt->pwdmnt = mnt;
213         current_ctxt->pwd = mnt->mnt_root;
214         current_ctxt->fs = get_ds();
215         smb->smsi_ctxt = current_ctxt;
216         
217         push_ctxt(&saved, smb->smsi_ctxt, NULL);
218
219         rc = smfs_llog_setup(smb);
220         if (!rc) {
221                 rc = SMFS_PLG_HELP(mnt->mnt_sb, PLG_START, NULL);
222         }
223
224         pop_ctxt(&saved, smb->smsi_ctxt, NULL);
225
226         /* connect KML ctxt to obd */
227         if (obd && smb->smsi_kml_log) {
228                 smb->smsi_kml_log->loc_idx = LLOG_REINT_ORIG_CTXT;
229                 smb->smsi_kml_log->loc_obd = obd;
230                 obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT] = smb->smsi_kml_log;
231         }
232         
233         /* enable plugins for directories on MDS or OST */
234         if (obd && obd->obd_type && obd->obd_type->typ_name) {
235                 if (!strcmp(obd->obd_type->typ_name, "obdfilter")) {
236                         struct filter_obd *filt = &obd->u.filter;
237  
238                         smfs_filter_flags(filt, root_dentry->d_inode);
239                 }
240                 else if (!strcmp(obd->obd_type->typ_name, "mds")) {
241                         struct mds_obd * mds = &obd->u.mds;
242                         
243                         smfs_mds_flags(mds, root_dentry->d_inode);
244                 }
245                 else
246                         CDEBUG(D_SUPER,"Unknown OBD (%s) post_setup\n",
247                                obd->obd_type->typ_name);
248         }
249
250         if (rc)
251                 OBD_FREE(current_ctxt, sizeof(*current_ctxt));
252   
253         RETURN(rc);
254 }
255
256 void smfs_post_cleanup(struct super_block *sb)
257 {
258         struct smfs_super_info *smb = S2SMI(sb);
259         
260         ENTRY;
261         
262         smfs_llog_cleanup(smb);
263         SMFS_PLG_HELP(sb, PLG_STOP, NULL);
264         
265         if (smb->smsi_ctxt)
266                 OBD_FREE(smb->smsi_ctxt, sizeof(struct lvfs_run_ctxt));
267         
268         EXIT;
269 }
270
271 static int smfs_mount_cache(struct smfs_super_info *smb, char *devstr, 
272                             char *typestr, char *opts)
273 {
274         int err = 0, typelen;
275         struct vfsmount *mnt;
276         ENTRY;
277
278         typelen = strlen(typestr);
279
280         CDEBUG(D_INODE, "smfs: mounting %s at %s\n", typestr, devstr);
281         mnt = do_kern_mount(typestr, 0, devstr, (void *)opts);
282         if (IS_ERR(mnt)) {
283                 CERROR("do_kern_mount failed: rc = %ld\n", 
284                        PTR_ERR(mnt));
285                 GOTO(err_out, err = PTR_ERR(mnt));
286         }
287
288         smb->smsi_sb = mnt->mnt_sb;
289         smb->smsi_mnt = mnt;
290
291         smfs_init_sm_ops(smb);
292
293         OBD_ALLOC(smb->smsi_cache_ftype, strlen(typestr) + 1);
294         memcpy(smb->smsi_cache_ftype, typestr, strlen(typestr));
295
296         OBD_ALLOC(smb->smsi_ftype, strlen(SMFS_TYPE) + 1);
297         memcpy(smb->smsi_ftype, SMFS_TYPE, strlen(SMFS_TYPE));
298         
299         err = smfs_init_fsfilt_ops(smb);
300 err_out:
301         RETURN(err);
302 }
303
304 static int smfs_umount_cache(struct smfs_super_info *smb)
305 {
306         mntput(smb->smsi_mnt);
307         smfs_cleanup_sm_ops(smb);
308         smfs_cleanup_fsfilt_ops(smb);
309
310         if (smb->smsi_cache_ftype)
311                 OBD_FREE(smb->smsi_cache_ftype,
312                          strlen(smb->smsi_cache_ftype) + 1);
313         if (smb->smsi_ftype)
314                 OBD_FREE(smb->smsi_ftype, strlen(smb->smsi_ftype) + 1);
315                
316         return 0;
317 }
318
319 /* This function initializes plugins in SMFS 
320  * @flags: are filled while options parsing 
321  * @sb: smfs super block
322  */
323
324 static int smfs_init_plugins(struct super_block * sb, int flags)
325 {
326         struct smfs_super_info * smb = S2SMI(sb);
327         
328         ENTRY;
329         
330         INIT_LIST_HEAD(&smb->smsi_plg_list);
331
332         if (SMFS_IS(flags, SMFS_PLG_KML)) 
333                 smfs_init_kml(sb);
334         if (SMFS_IS(flags, SMFS_PLG_LRU)) 
335                 smfs_init_lru(sb);
336 #if CONFIG_SNAPFS
337         if (SMFS_IS(flags, SMFS_PLG_COW)) 
338                 smfs_init_cow(sb);
339 #endif
340         RETURN(0); 
341 }
342
343 static void smfs_remove_plugins(struct super_block *sb)
344 {
345         ENTRY;
346
347         SMFS_PLG_HELP(sb, PLG_EXIT, (void*)sb);
348         
349         EXIT;
350 }
351
352 void smfs_put_super(struct super_block *sb)
353 {
354         struct smfs_super_info *smb = S2SMI(sb);
355         ENTRY;
356         smfs_remove_plugins(sb);
357         
358         dput(sb->s_root);
359         
360         if (smb->smsi_mnt)
361                 smfs_umount_cache(smb);
362         
363         smfs_cleanup_smb(smb);
364         EXIT;
365 }
366
367 int smfs_fill_super(struct super_block *sb, void *data, int silent)
368 {
369         struct inode *root_inode = NULL;
370         struct smfs_super_info *smb = NULL;
371         char *devstr = NULL, *typestr = NULL; 
372         char *opts = NULL;
373         int err = 0;
374         int flags = 0;
375         ino_t root_ino;
376
377         ENTRY;
378         
379         if (!data) {
380                 CERROR("no mount options. At least name and dev are needed\n");
381                 err = -EINVAL;
382                 goto out_err;
383         }
384
385         CDEBUG(D_SUPER, "mount opts: %s\n", (char *)data);
386
387         smb = smfs_init_smb(sb);
388         if (!smb)
389                 RETURN(-ENOMEM);
390         lock_kernel();
391         OBD_ALLOC(opts, strlen(data) + 1);
392         if (!opts) {
393                 err = -ENOMEM;
394                 goto out_err;
395         }
396         
397         err = smfs_options(data, &devstr, &typestr, opts, &flags);
398         if (err)
399                 goto out_err;
400                 
401         if (!typestr || !devstr) {
402                 CERROR("mount options name and dev are mandatory\n");
403                 err = -EINVAL;
404                 goto out_err;
405         }
406         
407         CDEBUG(D_SUPER, "backfs mount opts: %s\n", opts);
408
409         err = smfs_mount_cache(smb, devstr, typestr, opts);
410         if (err) {
411                 CERROR("Can not mount %s as %s\n", devstr, typestr);
412                 goto out_err;
413         }
414
415         OBD_FREE(opts, strlen(data) + 1);
416         opts = NULL;
417         
418         duplicate_sb(sb, smb->smsi_sb);
419         sb->s_bdev = smb->smsi_sb->s_bdev;
420         sm_set_sb_ops(smb->smsi_sb, sb);
421
422         /* init the root_inode of smfs. */ 
423         root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
424         root_inode = smfs_get_inode(sb, root_ino, NULL, 0);
425
426         CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
427                sb->s_op->read_inode, root_ino, root_inode);
428
429         sb->s_root = d_alloc_root(root_inode);
430         if (!sb->s_root) {
431                 err = -ENOMEM;
432                 goto out_err;
433         }
434         
435         /* all entries created until post_setup() should not be logged */
436         SMFS_CLEAR((I2SMI(root_inode))->smi_flags, SMFS_PLG_ALL);
437    
438 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
439         CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
440                (ulong)sb, (ulong)&sb->u.generic_sbp);
441 #else
442         CDEBUG(D_SUPER, "sb %lx(%p), &sb->s_fs_info: %lx\n",
443                (ulong)sb, smb->smsi_sb, (ulong)&sb->s_fs_info);
444 #endif
445         
446         smfs_init_plugins(sb, flags);
447         unlock_kernel();
448         RETURN (0);
449 out_err:
450         if (smb->smsi_mnt)
451                 smfs_umount_cache(smb);
452
453         if (opts)
454                 OBD_FREE(opts, strlen(data) + 1);
455
456         smfs_cleanup_smb(smb);
457         unlock_kernel();
458         RETURN(err);
459 }
460
461 void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
462 {
463         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
464
465         if (fsfilt->fs_start)
466                 return fsfilt->fs_start(inode, op, NULL, 0);
467         return NULL;
468 }
469
470 void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
471 {
472         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
473
474         if (handle && fsfilt->fs_commit)
475                 fsfilt->fs_commit(inode->i_sb, inode, handle, force_sync);
476 }
477 /* Plugin API */
478 int smfs_register_plugin(struct super_block * sb,
479                          struct smfs_plugin * new_plugin) 
480 {
481         struct smfs_super_info * smb = S2SMI(sb);
482         struct smfs_plugin * plg = NULL;
483         struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
484         
485         ENTRY;
486         
487         list_for_each_entry(plg, plist, plg_list) {
488                 if (plg->plg_type == new_plugin->plg_type) {
489                         CWARN("Plugin is already registered\n");
490                         RETURN(-EEXIST);
491                 }
492         }
493         
494         
495         if (SMFS_IS(smb->smsi_flags, new_plugin->plg_type)) {
496                 CWARN("Plugin is already registered\n");
497                 RETURN(-EEXIST);  
498         }
499                 
500         OBD_ALLOC(plg, sizeof(*plg));
501         if (!plg) {
502                 CWARN("Cannot allocate memory for plugin\n");
503                 RETURN(-ENOMEM);
504         }
505         
506         memcpy(plg, new_plugin, sizeof(*plg));
507         list_add_tail(&plg->plg_list, plist);
508         
509         RETURN(0);
510 }
511
512 void * smfs_deregister_plugin(struct super_block *sb, int type)
513 {
514         struct smfs_plugin * plg = NULL;
515         struct list_head * plist = &S2SMI(sb)->smsi_plg_list;
516         void * priv = NULL;
517         
518         ENTRY;
519
520         list_for_each_entry(plg, plist, plg_list) {
521                 if (plg->plg_type == type) {
522                         list_del(&plg->plg_list);
523                         priv = plg->plg_private;
524                         OBD_FREE(plg, sizeof(*plg));
525                         break;
526                 }
527         }
528                 
529         RETURN(priv);
530 }
531
532 void smfs_pre_hook (struct inode * inode, int op, void * msg) 
533 {
534         struct smfs_super_info *smb = S2SMI(inode->i_sb);    
535         struct smfs_inode_info *smi = I2SMI(inode);
536         struct list_head *hlist = &smb->smsi_plg_list;
537         struct smfs_plugin *plg;
538                 
539         //ENTRY;
540         LASSERT(op < HOOK_MAX);
541         //call hook operations
542         list_for_each_entry(plg, hlist, plg_list) {
543                 //check that plugin is active
544                 if(!SMFS_IS(smb->plg_flags, plg->plg_type))
545                         continue;
546                 //check that inode is allowed
547                 if (!SMFS_IS(smi->smi_flags, plg->plg_type))
548                         continue;
549                 
550                 if (plg->plg_pre_op)
551                         plg->plg_pre_op(op, inode, msg, 0, plg->plg_private);
552         }
553
554         //EXIT;
555 }
556
557 void smfs_post_hook (struct inode * inode, int op, void * msg, int ret)
558 {
559         struct smfs_super_info *smb = S2SMI(inode->i_sb);
560         struct smfs_inode_info *smi = I2SMI(inode);
561         struct list_head *hlist = &smb->smsi_plg_list;
562         struct smfs_plugin *plg;
563         
564         //ENTRY;
565         
566         list_for_each_entry(plg, hlist, plg_list) {
567                 //check that plugin is active
568                 if(!SMFS_IS(smb->plg_flags, plg->plg_type))
569                         continue;
570                 //check that inode is allowed
571                 if (!SMFS_IS(smi->smi_flags, plg->plg_type))
572                         continue;
573                 
574                 if (plg->plg_post_op)
575                         plg->plg_post_op(op, inode, msg, ret, plg->plg_private);
576         }
577
578         //EXIT;
579 }
580
581 int smfs_helper (struct super_block * sb, int op, void * msg) 
582 {
583         struct smfs_super_info *smb = S2SMI(sb);    
584         struct list_head *hlist = &smb->smsi_plg_list;
585         struct smfs_plugin *plg, *tmp;
586         int rc = 0;
587         
588         //ENTRY;
589         LASSERT(op < PLG_HELPER_MAX);
590         //call hook operations
591         list_for_each_entry_safe(plg, tmp, hlist, plg_list) {
592                 //check that plugin is active
593                 if(!SMFS_IS(smb->plg_flags, plg->plg_type) && (op != PLG_START))
594                         continue;
595                
596                 if (plg->plg_helper)
597                        rc += plg->plg_helper(op, sb, msg, plg->plg_private);
598         }
599
600         //EXIT;
601         
602         return rc;
603 }
604
605