Whamcloud - gitweb
b=19720
[fs/lustre-release.git] / lustre / lvfs / fsfilt.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_FILTER
41
42 #include <linux/fs.h>
43 #include <linux/jbd.h>
44 #include <linux/module.h>
45 #include <linux/kmod.h>
46 #include <linux/slab.h>
47 #include <libcfs/libcfs.h>
48 #include <lustre_fsfilt.h>
49
50 CFS_LIST_HEAD(fsfilt_types);
51
52 static struct fsfilt_operations *fsfilt_search_type(const char *type)
53 {
54         struct fsfilt_operations *found;
55         struct list_head *p;
56
57         list_for_each(p, &fsfilt_types) {
58                 found = list_entry(p, struct fsfilt_operations, fs_list);
59                 if (!strcmp(found->fs_type, type)) {
60                         return found;
61                 }
62         }
63         return NULL;
64 }
65
66 int fsfilt_register_ops(struct fsfilt_operations *fs_ops)
67 {
68         struct fsfilt_operations *found;
69
70         /* lock fsfilt_types list */
71         if ((found = fsfilt_search_type(fs_ops->fs_type))) {
72                 if (found != fs_ops) {
73                         CERROR("different operations for type %s\n",
74                                fs_ops->fs_type);
75                         /* unlock fsfilt_types list */
76                         RETURN(-EEXIST);
77                 }
78         } else {
79                 PORTAL_MODULE_USE;
80                 list_add(&fs_ops->fs_list, &fsfilt_types);
81         }
82
83         /* unlock fsfilt_types list */
84         return 0;
85 }
86
87 void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops)
88 {
89         struct list_head *p;
90
91         /* lock fsfilt_types list */
92         list_for_each(p, &fsfilt_types) {
93                 struct fsfilt_operations *found;
94
95                 found = list_entry(p, typeof(*found), fs_list);
96                 if (found == fs_ops) {
97                         list_del(p);
98                         PORTAL_MODULE_UNUSE;
99                         break;
100                 }
101         }
102         /* unlock fsfilt_types list */
103 }
104
105 struct fsfilt_operations *fsfilt_get_ops(const char *type)
106 {
107         struct fsfilt_operations *fs_ops;
108
109         /* lock fsfilt_types list */
110         if (!(fs_ops = fsfilt_search_type(type))) {
111                 char name[32];
112                 int rc;
113
114                 snprintf(name, sizeof(name) - 1, "fsfilt_%s", type);
115                 name[sizeof(name) - 1] = '\0';
116
117                 if (!(rc = request_module("%s", name))) {
118                         fs_ops = fsfilt_search_type(type);
119                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
120                         if (!fs_ops)
121                                 rc = -ENOENT;
122                 }
123
124                 if (rc) {
125                         CERROR("Can't find %s interface\n", name);
126                         RETURN(ERR_PTR(rc < 0 ? rc : -rc));
127                         /* unlock fsfilt_types list */
128                 }
129         }
130         try_module_get(fs_ops->fs_owner);
131         /* unlock fsfilt_types list */
132
133         return fs_ops;
134 }
135
136 void fsfilt_put_ops(struct fsfilt_operations *fs_ops)
137 {
138         module_put(fs_ops->fs_owner);
139 }
140
141
142 EXPORT_SYMBOL(fsfilt_register_ops);
143 EXPORT_SYMBOL(fsfilt_unregister_ops);
144 EXPORT_SYMBOL(fsfilt_get_ops);
145 EXPORT_SYMBOL(fsfilt_put_ops);