Whamcloud - gitweb
- landed b_hd_cray_merge3
[fs/lustre-release.git] / lustre / lvfs / lvfs_common.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25
26 #define DEBUG_SUBSYSTEM S_FILTER
27
28 #include <linux/obd.h>
29 #include <linux/lvfs.h>
30
31 struct dentry *lvfs_id2dentry(struct lvfs_run_ctxt *ctxt, __u64 id,
32                               __u32 gen, __u64 gr, void *data)
33 {
34         return ctxt->cb_ops.l_id2dentry(id, gen, gr, data);
35 }
36 EXPORT_SYMBOL(lvfs_id2dentry);
37
38 static struct list_head lvfs_context_list;
39
40 void lvfs_mount_list_init(void)
41 {
42         INIT_LIST_HEAD(&lvfs_context_list);
43 }
44
45 void lvfs_mount_list_cleanup(void)
46 {
47         struct list_head *tmp;
48
49         if (list_empty(&lvfs_context_list))
50                 return;
51
52         list_for_each(tmp, &lvfs_context_list) {
53                 struct lvfs_obd_ctxt *data = 
54                        list_entry(tmp, struct lvfs_obd_ctxt, loc_list);
55                 CERROR("device %s still mounted with refcount %d\n",
56                         data->loc_name, atomic_read(&data->loc_refcount));
57         }
58 }
59
60 static inline
61 struct lvfs_obd_ctxt *get_lvfs_mount(struct lvfs_obd_ctxt *lvfs_ctxt)
62 {
63         atomic_inc(&lvfs_ctxt->loc_refcount);
64         return lvfs_ctxt;
65 }
66
67 static struct lvfs_obd_ctxt *add_lvfs_mount(struct vfsmount *mnt, char *name)
68 {
69         struct lvfs_obd_ctxt *lvfs_ctxt;
70         ENTRY;
71
72         OBD_ALLOC(lvfs_ctxt, sizeof(*lvfs_ctxt));
73         if (!lvfs_ctxt) {
74                 CERROR("No Memory\n");
75                 RETURN(NULL);
76         }
77
78         if (name) {
79                 int length = strlen(name) + 1;
80
81                 OBD_ALLOC(lvfs_ctxt->loc_name, length);
82                 if (!lvfs_ctxt->loc_name) {
83                         CERROR("No Memory\n");
84                         OBD_FREE(lvfs_ctxt, sizeof(*lvfs_ctxt));
85                         RETURN(NULL);
86                 }
87                 memcpy(lvfs_ctxt->loc_name, name, length);
88         }
89         lvfs_ctxt->loc_mnt = mnt;
90         list_add(&lvfs_ctxt->loc_list, &lvfs_context_list);
91         atomic_set(&lvfs_ctxt->loc_refcount, 1);
92         RETURN(lvfs_ctxt);
93 }
94
95 void lvfs_umount_fs(struct lvfs_obd_ctxt *lvfs_ctxt)
96 {
97         if (lvfs_ctxt && atomic_dec_and_test(&lvfs_ctxt->loc_refcount)) {
98                 struct vfsmount *mnt = lvfs_ctxt->loc_mnt;
99
100                 list_del(&lvfs_ctxt->loc_list);
101                 if (atomic_read(&mnt->mnt_count) > 2)
102                        CERROR("mount busy, mnt %p mnt_count %d != 2\n", mnt,
103                                atomic_read(&mnt->mnt_count));
104                 
105                 mntput(mnt);
106                 
107                 if (lvfs_ctxt->loc_name)
108                         OBD_FREE(lvfs_ctxt->loc_name, 
109                                  strlen(lvfs_ctxt->loc_name) + 1);
110                 OBD_FREE(lvfs_ctxt, sizeof(*lvfs_ctxt));
111                 dev_clear_rdonly(2);
112         }
113 }
114 EXPORT_SYMBOL(lvfs_umount_fs);
115
116 int lvfs_mount_fs(char *name, char *fstype, char *options, int flags,
117                   struct lvfs_obd_ctxt **lvfs_ctxt)
118 {
119         struct vfsmount *mnt = NULL;
120         struct list_head *tmp;
121         int rc = 0;
122         ENTRY;
123
124         list_for_each(tmp, &lvfs_context_list) {
125                 struct lvfs_obd_ctxt *data =
126                                list_entry(tmp, struct lvfs_obd_ctxt, loc_list);
127                 if (strcmp(data->loc_name, name) == 0) {
128                        *lvfs_ctxt = get_lvfs_mount(data);
129                        RETURN(0);
130                 }
131         }
132         mnt = do_kern_mount(fstype, flags, name, options);
133
134         if (IS_ERR(mnt)) {
135                 rc = PTR_ERR(mnt);
136                 CERROR("do_kern_mount failed: rc = %d\n", rc);
137                 GOTO(out, rc);
138         }
139         CDEBUG(D_SUPER, "%s: mnt = %p\n", name, mnt);
140         /*add this lvfs context to the lvfs_mount_list*/
141         *lvfs_ctxt = add_lvfs_mount(mnt, name);
142         if (!*lvfs_ctxt) {
143                 mntput(mnt);
144                 CERROR("add_lvfs_mount failed\n");
145                 GOTO(out, rc = -EINVAL);
146         }
147 out:
148         RETURN(rc);
149 }
150 EXPORT_SYMBOL(lvfs_mount_fs);