Whamcloud - gitweb
6224cfcc79e908d94bb132ccc13125d30d30f2db
[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, exp_chain);
98                         p += sprintf(&page[p], "%p", export);
99                 }
100                 if (export != 0) { /* there was at least one export */
101                         p += sprintf(&page[p], ")");
102                 }
103         }
104
105         p += sprintf(&page[p], "\n");
106
107         /* Compute eof and return value */
108         if (offset + count >= p) {
109                 *eof = 1;
110                 return (p - offset);
111         }
112         return count;
113 }
114
115 struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd)
116 {
117         struct proc_dir_entry *obd_dir;
118         struct proc_dir_entry *obd_status = NULL;
119
120         if (!proc_lustre_dir_entry) {
121                 proc_lustre_dir_entry = proc_mkdir("lustre", &proc_root);
122                 if (IS_ERR(proc_lustre_dir_entry))
123                         return 0;
124
125                 proc_lustre_obd_dir_entry =
126                         proc_mkdir("devices", proc_lustre_dir_entry);
127                 if (IS_ERR(proc_lustre_obd_dir_entry))
128                         return 0;
129         }
130         obd_dir = proc_mkdir(obd->obd_name, proc_lustre_obd_dir_entry);
131
132         if (obd_dir)
133                 obd_status = create_proc_entry("status", S_IRUSR | S_IFREG,
134                                                obd_dir);
135
136         if (obd_status) {
137                 obd_status->read_proc = read_lustre_status;
138                 obd_status->data = (void *)obd;
139         }
140
141         return obd_dir;
142 }
143
144 void proc_lustre_remove_obd_entry(const char *name, struct obd_device *obd)
145 {
146         struct proc_dir_entry *obd_entry = NULL;
147         struct proc_dir_entry *obd_dir = obd->obd_proc_entry;
148
149         remove_proc_entry(name, obd_dir);
150
151         while (obd_dir->subdir == NULL) {
152                 /* if we removed last entry in this directory, then
153                  * remove parent directory unless this is /proc itself */
154                 if (obd_dir == &proc_root)
155                         break;
156
157                 obd_entry = obd_dir;
158                 obd_dir = obd_dir->parent;
159
160                 /* If /proc/lustre/obd/foo or /proc/lustre/obd or
161                  * /proc/lustre is being removed, then reset internal
162                  * variables */
163
164                 if (obd_entry == obd->obd_proc_entry)
165                         obd->obd_proc_entry = NULL; /* /proc/lustre/obd/foo */
166                 else if (obd_entry == proc_lustre_obd_dir_entry)
167                         proc_lustre_obd_dir_entry = NULL; /* /proc/lustre/obd */
168                 else if (obd_entry == proc_lustre_dir_entry)
169                         proc_lustre_dir_entry = NULL; /* /proc/lustre */
170
171                 remove_proc_entry(obd_entry->name, obd_dir);
172         }
173 }
174
175 void proc_lustre_release_obd_device(struct obd_device *obd)
176 {
177         proc_lustre_remove_obd_entry("status", obd);
178 }
179
180
181 #else  /* CONFIG_PROC_FS */
182
183 struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd)
184 {
185         return 0;
186 }
187
188 void proc_lustre_remove_obd_entry(const char* name, struct obd_device *obd)
189 {
190 }
191
192 void proc_lustre_release_obd_device(struct obd_device *obd)
193 {
194 }
195
196 #endif   /* CONFIG_PROC_FS */
197
198 EXPORT_SYMBOL(proc_lustre_remove_obd_entry);