Whamcloud - gitweb
b=15253 add failover nidlist to proc import
[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/module.h>
44 #include <linux/kmod.h>
45 #include <linux/slab.h>
46 #include <libcfs/kp30.h>
47 #include <lustre_fsfilt.h>
48
49 LIST_HEAD(fsfilt_types);
50
51 static struct fsfilt_operations *fsfilt_search_type(const char *type)
52 {
53         struct fsfilt_operations *found;
54         struct list_head *p;
55
56         list_for_each(p, &fsfilt_types) {
57                 found = list_entry(p, struct fsfilt_operations, fs_list);
58                 if (!strcmp(found->fs_type, type)) {
59                         return found;
60                 }
61         }
62         return NULL;
63 }
64
65 int fsfilt_register_ops(struct fsfilt_operations *fs_ops)
66 {
67         struct fsfilt_operations *found;
68
69         /* lock fsfilt_types list */
70         if ((found = fsfilt_search_type(fs_ops->fs_type))) {
71                 if (found != fs_ops) {
72                         CERROR("different operations for type %s\n",
73                                fs_ops->fs_type);
74                         /* unlock fsfilt_types list */
75                         RETURN(-EEXIST);
76                 }
77         } else {
78                 PORTAL_MODULE_USE;
79                 list_add(&fs_ops->fs_list, &fsfilt_types);
80         }
81
82         /* unlock fsfilt_types list */
83         return 0;
84 }
85
86 void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops)
87 {
88         struct list_head *p;
89
90         /* lock fsfilt_types list */
91         list_for_each(p, &fsfilt_types) {
92                 struct fsfilt_operations *found;
93
94                 found = list_entry(p, typeof(*found), fs_list);
95                 if (found == fs_ops) {
96                         list_del(p);
97                         PORTAL_MODULE_UNUSE;
98                         break;
99                 }
100         }
101         /* unlock fsfilt_types list */
102 }
103
104 struct fsfilt_operations *fsfilt_get_ops(const char *type)
105 {
106         struct fsfilt_operations *fs_ops;
107
108         /* lock fsfilt_types list */
109         if (!(fs_ops = fsfilt_search_type(type))) {
110                 char name[32];
111                 int rc;
112
113                 snprintf(name, sizeof(name) - 1, "fsfilt_%s", type);
114                 name[sizeof(name) - 1] = '\0';
115
116                 if (!(rc = request_module("%s", name))) {
117                         fs_ops = fsfilt_search_type(type);
118                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
119                         if (!fs_ops)
120                                 rc = -ENOENT;
121                 }
122
123                 if (rc) {
124                         CERROR("Can't find %s interface\n", name);
125                         RETURN(ERR_PTR(rc < 0 ? rc : -rc));
126                         /* unlock fsfilt_types list */
127                 }
128         }
129         try_module_get(fs_ops->fs_owner);
130         /* unlock fsfilt_types list */
131
132         return fs_ops;
133 }
134
135 void fsfilt_put_ops(struct fsfilt_operations *fs_ops)
136 {
137         module_put(fs_ops->fs_owner);
138 }
139
140
141 EXPORT_SYMBOL(fsfilt_register_ops);
142 EXPORT_SYMBOL(fsfilt_unregister_ops);
143 EXPORT_SYMBOL(fsfilt_get_ops);
144 EXPORT_SYMBOL(fsfilt_put_ops);