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