Whamcloud - gitweb
Branch b1_4
[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/obd_support.h>
30 #include <linux/lvfs.h>
31
32 struct dentry *lvfs_id2dentry(struct lvfs_run_ctxt *ctxt, __u64 id,
33                               __u32 gen, __u64 gr, void *data)
34 {
35         return ctxt->cb_ops.l_id2dentry(id, gen, gr, data);
36 }
37 EXPORT_SYMBOL(lvfs_id2dentry);
38
39 static struct list_head lvfs_context_list;
40
41 void lvfs_mount_list_init(void)
42 {
43         INIT_LIST_HEAD(&lvfs_context_list);
44 }
45
46 void lvfs_mount_list_cleanup(void)
47 {
48         struct list_head *tmp;
49
50         if (list_empty(&lvfs_context_list))
51                 return;
52
53         list_for_each(tmp, &lvfs_context_list) {
54                 struct lvfs_obd_ctxt *data = 
55                        list_entry(tmp, struct lvfs_obd_ctxt, loc_list);
56                 CERROR("device %s still mounted with refcount %d\n",
57                         data->loc_name, atomic_read(&data->loc_refcount));
58         }
59 }
60
61 static inline
62 struct lvfs_obd_ctxt *get_lvfs_mount(struct lvfs_obd_ctxt *lvfs_ctxt)
63 {
64         atomic_inc(&lvfs_ctxt->loc_refcount);
65         return lvfs_ctxt;
66 }
67
68 static struct lvfs_obd_ctxt *add_lvfs_mount(struct vfsmount *mnt, char *name)
69 {
70         struct lvfs_obd_ctxt *lvfs_ctxt;
71         ENTRY;
72
73         OBD_ALLOC(lvfs_ctxt, sizeof(*lvfs_ctxt));
74         if (!lvfs_ctxt) {
75                 CERROR("No Memory\n");
76                 RETURN(NULL);
77         }
78
79         if (name) {
80                 int length = strlen(name) + 1;
81
82                 OBD_ALLOC(lvfs_ctxt->loc_name, length);
83                 if (!lvfs_ctxt->loc_name) {
84                         CERROR("No Memory\n");
85                         OBD_FREE(lvfs_ctxt, sizeof(*lvfs_ctxt));
86                         RETURN(NULL);
87                 }
88                 memcpy(lvfs_ctxt->loc_name, name, length);
89         }
90         lvfs_ctxt->loc_mnt = mnt;
91         list_add(&lvfs_ctxt->loc_list, &lvfs_context_list);
92         atomic_set(&lvfs_ctxt->loc_refcount, 1);
93         RETURN(lvfs_ctxt);
94 }
95
96 void lvfs_umount_fs(struct lvfs_obd_ctxt *lvfs_ctxt)
97 {
98         if (lvfs_ctxt && atomic_dec_and_test(&lvfs_ctxt->loc_refcount)) {
99                 struct vfsmount *mnt = lvfs_ctxt->loc_mnt;
100                 ll_sbdev_type save_dev = ll_sbdev(mnt->mnt_sb);
101
102                 list_del(&lvfs_ctxt->loc_list);
103                 if (atomic_read(&mnt->mnt_count) > 2)
104                        CERROR("mount busy, mnt %p mnt_count %d != 2\n", mnt,
105                                atomic_read(&mnt->mnt_count));
106                 
107                 mntput(mnt);
108                 if (lvfs_ctxt->loc_name)
109                         OBD_FREE(lvfs_ctxt->loc_name, 
110                                  strlen(lvfs_ctxt->loc_name) + 1);
111                 OBD_FREE(lvfs_ctxt, sizeof(*lvfs_ctxt));
112                 ll_clear_rdonly(save_dev);
113         }
114 }
115 EXPORT_SYMBOL(lvfs_umount_fs);
116
117 int lvfs_mount_fs(char *name, char *fstype, char *options, int flags,
118                   struct lvfs_obd_ctxt **lvfs_ctxt)
119 {
120         struct vfsmount *mnt = NULL;
121         struct list_head *tmp;
122         int rc = 0;
123         ENTRY;
124
125         list_for_each(tmp, &lvfs_context_list) {
126                 struct lvfs_obd_ctxt *data =
127                                list_entry(tmp, struct lvfs_obd_ctxt, loc_list);
128                 if (strcmp(data->loc_name, name) == 0) {
129                        *lvfs_ctxt = get_lvfs_mount(data);
130                        RETURN(0);
131                 }
132         }
133         mnt = do_kern_mount(fstype, flags, name, options);
134
135         if (IS_ERR(mnt)) {
136                 rc = PTR_ERR(mnt);
137                 CERROR("do_kern_mount failed: rc = %d\n", rc);
138                 GOTO(out, rc);
139         }
140         CDEBUG(D_SUPER, "%s: mnt = %p\n", name, mnt);
141         /*add this lvfs context to the lvfs_mount_list*/
142         *lvfs_ctxt = add_lvfs_mount(mnt, name);
143         if (!*lvfs_ctxt) {
144                 mntput(mnt);
145                 CERROR("add_lvfs_mount failed\n");
146                 GOTO(out, rc = -EINVAL);
147         }
148 out:
149         RETURN(rc);
150 }
151 EXPORT_SYMBOL(lvfs_mount_fs);