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