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