Whamcloud - gitweb
WARNING: we currently crash on unmount after the last phase of runtests.
[fs/lustre-release.git] / lustre / obdclass / proc_lustre.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * proc_lustre.c manages /proc/lustre
5  *
6  * Copyright (c) 2001 Rumi Zahir <rumi.zahir@intel.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 /* OBD devices materialize in /proc as a directory:
25  *              /proc/lustre/obd/<number>
26  * when /dev/obd<number> is opened. When the device is closed, the
27  * directory entry disappears.
28  *
29  * For each open OBD device, code in this file also creates a file
30  * named <status>. "cat /proc/lustre/obd/<number>/status" gives
31  * information about the OBD device's configuration.
32  * The class driver manages the "status" entry.
33  *
34  * Other logical drivers can create their own entries. For example,
35  * the obdtrace driver creates /proc/lustre/obd/<obdid>/stats entry.
36  *
37  * This file defines three functions
38  *   proc_lustre_register_obd_device() - called at device attach time
39  *   proc_lustre_release_obd_device() - called at detach
40  *               proc_lustre_remove_obd_entry()
41  * that dynamically create/delete /proc/lustre/obd entries:
42  *
43  *     proc_lustre_register_obd_device() registers an obd device,
44  *     and, if this is the first OBD device, creates /proc/lustre/obd.
45  *
46  *     proc_lustre_release_obd_device() removes device information
47  *     from /proc/lustre/obd, and if this is the last OBD device
48  *     removes  /proc/lustre/obd.
49  *
50  *     proc_lustre_remove_obd_entry() removes a
51  *     /proc/lustre/obd/<obdid>/ entry by name. This is the only
52  *     function that is exported to other modules.
53  */
54
55 #define EXPORT_SYMTAB
56 #include <linux/config.h>
57 #include <linux/module.h>
58 #include <linux/version.h>
59 #include <linux/proc_fs.h>
60
61 #define DEBUG_SUBSYSTEM S_CLASS
62
63 #include <linux/obd_support.h>
64 #include <linux/obd_class.h>
65
66 #ifdef CONFIG_PROC_FS
67 extern struct proc_dir_entry proc_root;
68 static struct proc_dir_entry *proc_lustre_dir_entry = NULL;
69 static struct proc_dir_entry *proc_lustre_obd_dir_entry = NULL;
70
71 static int read_lustre_status(char *page, char **start, off_t offset,
72                               int count, int *eof, void *data)
73 {
74         struct obd_device * obddev = (struct obd_device *)data;
75         int p;
76
77 #warning FIXME: This function is madness, completely unsafe, a disaster waiting to happen.
78
79         p = sprintf(&page[0], "device=%d\n", obddev->obd_minor);
80         p += sprintf(&page[p], "name=%s\n", MKSTR(obddev->obd_name));
81         p += sprintf(&page[p], "uuid=%s\n", obddev->obd_uuid);
82         p += sprintf(&page[p], "attached=1\n");
83         p += sprintf(&page[p], "type=%s\n", MKSTR(obddev->obd_type->typ_name));
84
85         if (obddev->obd_flags & OBD_SET_UP)
86                 p += sprintf(&page[p], "setup=1\n");
87
88         /* print exports */
89         {
90                 struct list_head *lh;
91                 struct obd_export *export = NULL;
92
93                 lh = &obddev->obd_exports;
94                 while ((lh = lh->next) != &obddev->obd_exports) {
95                         p += sprintf(&page[p],
96                                   ((export == NULL) ? ", connections(" : ",") );
97                         export = list_entry(lh, struct obd_export, 
98                                             exp_obd_chain);
99                         p += sprintf(&page[p], "%p", export);
100                 }
101                 if (export != 0) { /* there was at least one export */
102                         p += sprintf(&page[p], ")");
103                 }
104         }
105
106         p += sprintf(&page[p], "\n");
107
108         /* Compute eof and return value */
109         if (offset + count >= p) {
110                 *eof = 1;
111                 return (p - offset);
112         }
113         return count;
114 }
115
116 struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd)
117 {
118         struct proc_dir_entry *obd_dir;
119         struct proc_dir_entry *obd_status = NULL;
120
121         if (!proc_lustre_dir_entry) {
122                 proc_lustre_dir_entry = proc_mkdir("lustre", &proc_root);
123                 if (IS_ERR(proc_lustre_dir_entry))
124                         return 0;
125
126                 proc_lustre_obd_dir_entry =
127                         proc_mkdir("devices", proc_lustre_dir_entry);
128                 if (IS_ERR(proc_lustre_obd_dir_entry))
129                         return 0;
130         }
131         obd_dir = proc_mkdir(obd->obd_name, proc_lustre_obd_dir_entry);
132
133         if (obd_dir)
134                 obd_status = create_proc_entry("status", S_IRUSR | S_IFREG,
135                                                obd_dir);
136
137         if (obd_status) {
138                 obd_status->read_proc = read_lustre_status;
139                 obd_status->data = (void *)obd;
140         }
141
142         return obd_dir;
143 }
144
145 void proc_lustre_remove_obd_entry(const char *name, struct obd_device *obd)
146 {
147         struct proc_dir_entry *obd_entry = NULL;
148         struct proc_dir_entry *obd_dir = obd->obd_proc_entry;
149
150         remove_proc_entry(name, obd_dir);
151
152         while (obd_dir->subdir == NULL) {
153                 /* if we removed last entry in this directory, then
154                  * remove parent directory unless this is /proc itself */
155                 if (obd_dir == &proc_root)
156                         break;
157
158                 obd_entry = obd_dir;
159                 obd_dir = obd_dir->parent;
160
161                 /* If /proc/lustre/obd/foo or /proc/lustre/obd or
162                  * /proc/lustre is being removed, then reset internal
163                  * variables */
164
165                 if (obd_entry == obd->obd_proc_entry)
166                         obd->obd_proc_entry = NULL; /* /proc/lustre/obd/foo */
167                 else if (obd_entry == proc_lustre_obd_dir_entry)
168                         proc_lustre_obd_dir_entry = NULL; /* /proc/lustre/obd */
169                 else if (obd_entry == proc_lustre_dir_entry)
170                         proc_lustre_dir_entry = NULL; /* /proc/lustre */
171
172                 remove_proc_entry(obd_entry->name, obd_dir);
173         }
174 }
175
176 void proc_lustre_release_obd_device(struct obd_device *obd)
177 {
178         proc_lustre_remove_obd_entry("status", obd);
179 }
180
181
182 #else  /* CONFIG_PROC_FS */
183
184 struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd)
185 {
186         return 0;
187 }
188
189 void proc_lustre_remove_obd_entry(const char* name, struct obd_device *obd)
190 {
191 }
192
193 void proc_lustre_release_obd_device(struct obd_device *obd)
194 {
195 }
196
197 #endif   /* CONFIG_PROC_FS */
198
199 EXPORT_SYMBOL(proc_lustre_remove_obd_entry);