cf5497c3ae1b787a007304bb11cc06c0eeef45f8
[openafs.git] / src / afs / LINUX / osi_proc.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Linux module support routines.
12  *
13  */
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17 RCSID
18     ("$Header$");
19
20 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/nfsclient.h"
24 #include "h/unistd.h"           /* For syscall numbers. */
25 #include "h/mm.h"
26
27 #ifdef AFS_AMD64_LINUX20_ENV
28 #include <asm/ia32_unistd.h>
29 #endif
30
31 #include <linux/proc_fs.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/sched.h>
35 #include <linux/kernel.h>
36
37 #ifdef HAVE_KERNEL_LINUX_SEQ_FILE_H
38 #include <linux/seq_file.h>
39 #endif
40
41 struct proc_dir_entry *openafs_procfs;
42
43 #ifdef HAVE_KERNEL_LINUX_SEQ_FILE_H
44 static void *c_start(struct seq_file *m, loff_t *pos)
45 {
46         struct afs_q *cq, *tq;
47         loff_t n = 0;
48
49         AFS_GLOCK();
50         ObtainReadLock(&afs_xcell);
51         for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
52                 tq = QNext(cq);
53
54                 if (n++ == *pos)
55                         break;
56         }
57         if (cq == &CellLRU)
58                 cq = NULL;
59
60         AFS_GUNLOCK();
61         return cq;
62 }
63
64 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
65 {
66         struct afs_q *cq = p, *tq;
67
68         AFS_GLOCK();
69         (*pos)++;
70         tq = QNext(cq);
71
72         if (tq == &CellLRU)
73                 return NULL;
74
75         AFS_GUNLOCK();
76         return tq;
77 }
78
79 static void c_stop(struct seq_file *m, void *p)
80 {
81         AFS_GLOCK();
82         ReleaseReadLock(&afs_xcell);
83         AFS_GUNLOCK();
84 }
85
86 static int c_show(struct seq_file *m, void *p)
87 {
88         struct afs_q *cq = p;
89         struct cell *tc = QTOC(cq);
90         int j;
91
92         seq_printf(m, ">%s #(%d/%d)\n", tc->cellName,
93                    tc->cellNum, tc->cellIndex);
94
95         for (j = 0; j < MAXCELLHOSTS; j++) {
96                 afs_uint32 addr;
97
98                 if (!tc->cellHosts[j]) break;
99
100                 addr = tc->cellHosts[j]->addr->sa_ip;
101                 seq_printf(m, "%u.%u.%u.%u #%u.%u.%u.%u\n",
102                            NIPQUAD(addr), NIPQUAD(addr));
103         }
104
105         return 0;
106 }
107
108 static struct seq_operations afs_csdb_op = {
109         .start          = c_start,
110         .next           = c_next,
111         .stop           = c_stop,
112         .show           = c_show,
113 };
114
115 static int afs_csdb_open(struct inode *inode, struct file *file)
116 {
117         return seq_open(file, &afs_csdb_op);
118 }
119
120 static struct file_operations afs_csdb_operations = {
121         .open           = afs_csdb_open,
122         .read           = seq_read,
123         .llseek         = seq_lseek,
124         .release        = seq_release,
125 };
126
127
128 static void *uu_start(struct seq_file *m, loff_t *pos)
129 {
130     struct unixuser *tu;
131     loff_t n = 0;
132     afs_int32 i;
133
134     ObtainReadLock(&afs_xuser);
135     if (!*pos)
136         return (void *)(1);
137
138     for (i = 0; i < NUSERS; i++) {
139         for (tu = afs_users[i]; tu; tu = tu->next) {
140             if (++n == *pos)
141                 return tu;
142         }
143     }
144
145     return NULL;
146 }
147
148 static void *uu_next(struct seq_file *m, void *p, loff_t *pos)
149 {
150     struct unixuser *tu = p;
151     afs_int32 i = 0;
152
153     (*pos)++;
154     if (!p) return NULL;
155
156     if (p != (void *)1) {
157         if (tu->next) return tu->next;
158         i = UHash(tu->uid) + 1;
159     }
160
161     for (; i < NUSERS; i++)
162         if (afs_users[i]) return afs_users[i];
163     return NULL;
164 }
165
166 static void uu_stop(struct seq_file *m, void *p)
167 {
168     ReleaseReadLock(&afs_xuser);
169 }
170
171 static int uu_show(struct seq_file *m, void *p)
172 {
173     struct cell *tc = 0;
174     struct unixuser *tu = p;
175     char *cellname;
176
177     if (p == (void *)1) {
178         seq_printf(m, "%10s %4s %-6s  %-25s %10s",
179                    "UID/PAG", "Refs", "States", "Cell", "ViceID");
180         seq_printf(m, "  %10s %10s %10s %3s",
181                    "Tok Set", "Tok Begin", "Tok Expire", "vno");
182         seq_printf(m, "  %-15s %10s %10s %s\n",
183                    "NFS Client", "UID/PAG", "Client UID", "Sysname(s)");
184
185         return 0;
186     }
187
188     if (tu->cell == -1) {
189         cellname = "<default>";
190     } else {
191         tc = afs_GetCellStale(tu->cell, READ_LOCK);
192         if (tc) cellname = tc->cellName;
193         else cellname = "<unknown>";
194     }
195
196     seq_printf(m, "%10d %4d %04x    %-25s %10d",
197                tu->uid, tu->refCount, tu->states, cellname, tu->vid);
198
199     if (tc) afs_PutCell(tc, READ_LOCK);
200
201     if (tu->states & UHasTokens) {
202         seq_printf(m, "  %10d %10d %10d %3d",
203                    tu->tokenTime, tu->ct.BeginTimestamp, tu->ct.EndTimestamp,
204                    tu->ct.AuthHandle);
205     } else {
206         seq_printf(m, "  %-36s", "Tokens Not Set");
207     }
208
209     if (tu->exporter && tu->exporter->exp_type == EXP_NFS) {
210         struct nfsclientpag *np = (struct nfsclientpag *)(tu->exporter);
211         char ipaddr[16];
212         int i;
213
214         sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(np->host));
215         seq_printf(m, "  %-15s %10d %10d", ipaddr, np->uid, np->client_uid);
216         if (np->sysnamecount) {
217             for (i = 0; i < np->sysnamecount; i++)
218                 seq_printf(m, " %s", np->sysname[i]);
219         } else { 
220             seq_printf(m, " <no sysname list>");
221         }
222
223     } else if (tu->exporter) {
224         seq_printf(m, "  Unknown exporter type %d", tu->exporter->exp_type);
225     }
226     seq_printf(m, "\n");
227
228     return 0;
229 }
230
231 static struct seq_operations afs_unixuser_seqop = {
232     .start              = uu_start,
233     .next               = uu_next,
234     .stop               = uu_stop,
235     .show               = uu_show,
236 };
237
238 static int afs_unixuser_open(struct inode *inode, struct file *file)
239 {
240     return seq_open(file, &afs_unixuser_seqop);
241 }
242
243 static struct file_operations afs_unixuser_fops = {
244     .open               = afs_unixuser_open,
245     .read               = seq_read,
246     .llseek             = seq_lseek,
247     .release    = seq_release,
248 };
249
250
251 #else /* HAVE_KERNEL_LINUX_SEQ_FILE_H */
252
253 static int
254 csdbproc_info(char *buffer, char **start, off_t offset, int
255 length)
256 {
257     int len = 0;
258     off_t pos = 0;
259     int cnt;
260     struct afs_q *cq, *tq;
261     struct cell *tc;
262     char tbuffer[16];
263     /* 90 - 64 cellname, 10 for 32 bit num and index, plus
264        decor */
265     char temp[91];
266     afs_uint32 addr;
267     
268     ObtainReadLock(&afs_xcell);
269
270     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
271         tc = QTOC(cq); tq = QNext(cq);
272
273         pos += 90;
274
275         if (pos <= offset) {
276             len = 0;
277         } else {
278             sprintf(temp, ">%s #(%d/%d)\n", tc->cellName, 
279                     tc->cellNum, tc->cellIndex);
280             sprintf(buffer + len, "%-89s\n", temp);
281             len += 90;
282             if (pos >= offset+length) {
283                 ReleaseReadLock(&afs_xcell);
284                 goto done;
285             }
286         }
287
288         for (cnt = 0; cnt < MAXCELLHOSTS; cnt++) {
289             if (!tc->cellHosts[cnt]) break;
290             pos += 90;
291             if (pos <= offset) {
292                 len = 0;
293             } else {
294                 addr = ntohl(tc->cellHosts[cnt]->addr->sa_ip);
295                 sprintf(tbuffer, "%d.%d.%d.%d", 
296                         (int)((addr>>24) & 0xff),
297 (int)((addr>>16) & 0xff),
298                         (int)((addr>>8)  & 0xff), (int)( addr & 0xff));
299                 sprintf(temp, "%s #%s\n", tbuffer, tbuffer);
300                 sprintf(buffer + len, "%-89s\n", temp);
301                 len += 90;
302                 if (pos >= offset+length) {
303                     ReleaseReadLock(&afs_xcell);
304                     goto done;
305                 }
306             }
307         }
308     }
309
310     ReleaseReadLock(&afs_xcell);
311     
312 done:
313     *start = buffer + len - (pos - offset);
314     len = pos - offset;
315     if (len > length)
316         len = length;
317     return len;
318 }
319
320 #endif /* HAVE_KERNEL_LINUX_SEQ_FILE_H */
321
322 void
323 osi_proc_init(void)
324 {
325     struct proc_dir_entry *entry;
326 #if !defined(EXPORTED_PROC_ROOT_FS)
327     char path[64];
328 #endif
329     
330 #if defined(EXPORTED_PROC_ROOT_FS)
331     openafs_procfs = proc_mkdir(PROC_FSDIRNAME, proc_root_fs);
332 #else
333     sprintf(path, "fs/%s", PROC_FSDIRNAME);
334     openafs_procfs = proc_mkdir(path, NULL);
335 #endif
336 #ifdef HAVE_KERNEL_LINUX_SEQ_FILE_H
337     entry = create_proc_entry("unixusers", 0, openafs_procfs);
338     if (entry) {
339         entry->proc_fops = &afs_unixuser_fops;
340         entry->owner = THIS_MODULE;
341     }
342     entry = create_proc_entry(PROC_CELLSERVDB_NAME, 0, openafs_procfs);
343     if (entry)
344         entry->proc_fops = &afs_csdb_operations;
345 #else
346     entry = create_proc_info_entry(PROC_CELLSERVDB_NAME, (S_IFREG|S_IRUGO), openafs_procfs, csdbproc_info);
347 #endif
348     entry->owner = THIS_MODULE;
349 }
350
351 void
352 osi_proc_clean(void)
353 {
354 #if !defined(EXPORTED_PROC_ROOT_FS)
355     char path[64];
356 #endif
357
358     remove_proc_entry(PROC_CELLSERVDB_NAME, openafs_procfs);
359 #ifdef HAVE_KERNEL_LINUX_SEQ_FILE_H
360     remove_proc_entry("unixusers", openafs_procfs);
361 #endif
362 #if defined(EXPORTED_PROC_ROOT_FS)
363     remove_proc_entry(PROC_FSDIRNAME, proc_root_fs);
364 #else
365     sprintf(path, "fs/%s", PROC_FSDIRNAME);
366     remove_proc_entry(path, NULL);
367 #endif
368 }