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