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