Whamcloud - gitweb
- Grammatical, LDLM updates to network.lyx
[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 /* OBD devices materialize in /proc as a directory:
24  *              /proc/lustre/obd/<number>
25  * when /dev/obd<number> is opened. When the device is closed, the
26  * directory entry disappears.
27  *
28  * For each open OBD device, code in this file also creates a file
29  * named <status>. "cat /proc/lustre/obd/<number>/status" gives
30  * information about the OBD device's configuration.
31  * The class driver manages the "status" entry.
32  *
33  * Other logical drivers can create their own entries. For example,
34  * the obdtrace driver creates /proc/lustre/obd/<obdid>/stats entry.
35  *
36  * This file defines three functions
37  *   proc_lustre_register_obd_device() - called at device attach time
38  *   proc_lustre_release_obd_device() - called at detach
39  *               proc_lustre_remove_obd_entry()
40  * that dynamically create/delete /proc/lustre/obd entries:
41  *
42  *     proc_lustre_register_obd_device() registers an obd device,
43  *     and, if this is the first OBD device, creates /proc/lustre/obd.
44  *
45  *     proc_lustre_release_obd_device() removes device information
46  *     from /proc/lustre/obd, and if this is the last OBD device
47  *     removes  /proc/lustre/obd.
48  *
49  *     proc_lustre_remove_obd_entry() removes a
50  *     /proc/lustre/obd/<obdid>/ entry by name. This is the only
51  *     function that is exported to other modules.
52  */
53
54 #define EXPORT_SYMTAB
55 #include <linux/config.h>
56 #include <linux/module.h>
57 #include <linux/version.h>
58 #include <linux/proc_fs.h>
59
60 #define DEBUG_SUBSYSTEM S_CLASS
61
62 #include <linux/obd_support.h>
63 #include <linux/obd_class.h>
64
65 #ifdef CONFIG_PROC_FS
66 extern struct proc_dir_entry proc_root;
67 static struct proc_dir_entry *proc_lustre_dir_entry = NULL;
68 static struct proc_dir_entry *proc_lustre_obd_dir_entry = NULL;
69
70 static int read_lustre_status(char *page, char **start, off_t offset,
71                               int count, int *eof, void *data)
72 {
73         struct obd_device * obddev = (struct obd_device *)data;
74         int p;
75
76 #warning FIXME: This function is madness, completely unsafe, a disaster waiting to happen.
77
78         p = sprintf(&page[0], "device=%d\n", obddev->obd_minor);
79         p += sprintf(&page[p], "name=%s\n", MKSTR(obddev->obd_name));
80         p += sprintf(&page[p], "uuid=%s\n", obddev->obd_uuid);
81         p += sprintf(&page[p], "attached=1\n");
82         p += sprintf(&page[p], "type=%s\n", MKSTR(obddev->obd_type->typ_name));
83
84         if (obddev->obd_flags & OBD_SET_UP)
85                 p += sprintf(&page[p], "setup=1\n");
86
87         /* print exports */
88         {
89                 struct list_head *lh;
90                 struct obd_export *export = NULL;
91
92                 lh = &obddev->obd_exports;
93                 while ((lh = lh->next) != &obddev->obd_exports) {
94                         p += sprintf(&page[p],
95                                   ((export == NULL) ? ", connections(" : ",") );
96                         export = list_entry(lh, struct obd_export, exp_chain);
97                         p += sprintf(&page[p], "%p", export);
98                 }
99                 if (export != 0) { /* there was at least one export */
100                         p += sprintf(&page[p], ")");
101                 }
102         }
103
104         p += sprintf(&page[p], "\n");
105
106         /* Compute eof and return value */
107         if (offset + count >= p) {
108                 *eof = 1;
109                 return (p - offset);
110         }
111         return count;
112 }
113
114 struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd)
115 {
116         struct proc_dir_entry *obd_dir;
117         struct proc_dir_entry *obd_status = NULL;
118
119         if (!proc_lustre_dir_entry) {
120                 proc_lustre_dir_entry = proc_mkdir("lustre", &proc_root);
121                 if (IS_ERR(proc_lustre_dir_entry))
122                         return 0;
123
124                 proc_lustre_obd_dir_entry =
125                         proc_mkdir("devices", proc_lustre_dir_entry);
126                 if (IS_ERR(proc_lustre_obd_dir_entry))
127                         return 0;
128         }
129         obd_dir = proc_mkdir(obd->obd_name, proc_lustre_obd_dir_entry);
130
131         if (obd_dir)
132                 obd_status = create_proc_entry("status", S_IRUSR | S_IFREG,
133                                                obd_dir);
134
135         if (obd_status) {
136                 obd_status->read_proc = read_lustre_status;
137                 obd_status->data = (void *)obd;
138         }
139
140         return obd_dir;
141 }
142
143 void proc_lustre_remove_obd_entry(const char *name, struct obd_device *obd)
144 {
145         struct proc_dir_entry *obd_entry = NULL;
146         struct proc_dir_entry *obd_dir = obd->obd_proc_entry;
147
148         remove_proc_entry(name, obd_dir);
149
150         while (obd_dir->subdir == NULL) {
151                 /* if we removed last entry in this directory, then
152                  * remove parent directory unless this is /proc itself */
153                 if (obd_dir == &proc_root)
154                         break;
155
156                 obd_entry = obd_dir;
157                 obd_dir = obd_dir->parent;
158
159                 /* If /proc/lustre/obd/foo or /proc/lustre/obd or
160                  * /proc/lustre is being removed, then reset internal
161                  * variables */
162
163                 if (obd_entry == obd->obd_proc_entry)
164                         obd->obd_proc_entry = NULL; /* /proc/lustre/obd/foo */
165                 else if (obd_entry == proc_lustre_obd_dir_entry)
166                         proc_lustre_obd_dir_entry = NULL; /* /proc/lustre/obd */
167                 else if (obd_entry == proc_lustre_dir_entry)
168                         proc_lustre_dir_entry = NULL; /* /proc/lustre */
169
170                 remove_proc_entry(obd_entry->name, obd_dir);
171         }
172 }
173
174 void proc_lustre_release_obd_device(struct obd_device *obd)
175 {
176         proc_lustre_remove_obd_entry("status", obd);
177 }
178
179
180 #else  /* CONFIG_PROC_FS */
181
182 struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd)
183 {
184         return 0;
185 }
186
187 void proc_lustre_remove_obd_entry(const char* name, struct obd_device *obd)
188 {
189 }
190
191 void proc_lustre_release_obd_device(struct obd_device *obd)
192 {
193 }
194
195 #endif   /* CONFIG_PROC_FS */
196
197 EXPORT_SYMBOL(proc_lustre_remove_obd_entry);