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