Merge tag 'for-linus' of git://github.com/openrisc/linux
[linux-2.6-microblaze.git] / fs / afs / proc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* /proc interface for AFS
3  *
4  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/proc_fs.h>
11 #include <linux/seq_file.h>
12 #include <linux/sched.h>
13 #include <linux/uaccess.h>
14 #include "internal.h"
15
16 struct afs_vl_seq_net_private {
17         struct seq_net_private          seq;    /* Must be first */
18         struct afs_vlserver_list        *vllist;
19 };
20
21 static inline struct afs_net *afs_seq2net(struct seq_file *m)
22 {
23         return afs_net(seq_file_net(m));
24 }
25
26 static inline struct afs_net *afs_seq2net_single(struct seq_file *m)
27 {
28         return afs_net(seq_file_single_net(m));
29 }
30
31 /*
32  * Display the list of cells known to the namespace.
33  */
34 static int afs_proc_cells_show(struct seq_file *m, void *v)
35 {
36         struct afs_vlserver_list *vllist;
37         struct afs_cell *cell;
38
39         if (v == SEQ_START_TOKEN) {
40                 /* display header on line 1 */
41                 seq_puts(m, "USE    TTL SV ST NAME\n");
42                 return 0;
43         }
44
45         cell = list_entry(v, struct afs_cell, proc_link);
46         vllist = rcu_dereference(cell->vl_servers);
47
48         /* display one cell per line on subsequent lines */
49         seq_printf(m, "%3u %6lld %2u %2u %s\n",
50                    atomic_read(&cell->usage),
51                    cell->dns_expiry - ktime_get_real_seconds(),
52                    vllist->nr_servers,
53                    cell->state,
54                    cell->name);
55         return 0;
56 }
57
58 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
59         __acquires(rcu)
60 {
61         rcu_read_lock();
62         return seq_hlist_start_head_rcu(&afs_seq2net(m)->proc_cells, *_pos);
63 }
64
65 static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
66 {
67         return seq_hlist_next_rcu(v, &afs_seq2net(m)->proc_cells, pos);
68 }
69
70 static void afs_proc_cells_stop(struct seq_file *m, void *v)
71         __releases(rcu)
72 {
73         rcu_read_unlock();
74 }
75
76 static const struct seq_operations afs_proc_cells_ops = {
77         .start  = afs_proc_cells_start,
78         .next   = afs_proc_cells_next,
79         .stop   = afs_proc_cells_stop,
80         .show   = afs_proc_cells_show,
81 };
82
83 /*
84  * handle writes to /proc/fs/afs/cells
85  * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
86  */
87 static int afs_proc_cells_write(struct file *file, char *buf, size_t size)
88 {
89         struct seq_file *m = file->private_data;
90         struct afs_net *net = afs_seq2net(m);
91         char *name, *args;
92         int ret;
93
94         /* trim to first NL */
95         name = memchr(buf, '\n', size);
96         if (name)
97                 *name = 0;
98
99         /* split into command, name and argslist */
100         name = strchr(buf, ' ');
101         if (!name)
102                 goto inval;
103         do {
104                 *name++ = 0;
105         } while(*name == ' ');
106         if (!*name)
107                 goto inval;
108
109         args = strchr(name, ' ');
110         if (args) {
111                 do {
112                         *args++ = 0;
113                 } while(*args == ' ');
114                 if (!*args)
115                         goto inval;
116         }
117
118         /* determine command to perform */
119         _debug("cmd=%s name=%s args=%s", buf, name, args);
120
121         if (strcmp(buf, "add") == 0) {
122                 struct afs_cell *cell;
123
124                 cell = afs_lookup_cell(net, name, strlen(name), args, true);
125                 if (IS_ERR(cell)) {
126                         ret = PTR_ERR(cell);
127                         goto done;
128                 }
129
130                 if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
131                         afs_put_cell(net, cell);
132         } else {
133                 goto inval;
134         }
135
136         ret = 0;
137
138 done:
139         _leave(" = %d", ret);
140         return ret;
141
142 inval:
143         ret = -EINVAL;
144         printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
145         goto done;
146 }
147
148 /*
149  * Display the name of the current workstation cell.
150  */
151 static int afs_proc_rootcell_show(struct seq_file *m, void *v)
152 {
153         struct afs_cell *cell;
154         struct afs_net *net;
155
156         net = afs_seq2net_single(m);
157         if (rcu_access_pointer(net->ws_cell)) {
158                 rcu_read_lock();
159                 cell = rcu_dereference(net->ws_cell);
160                 if (cell)
161                         seq_printf(m, "%s\n", cell->name);
162                 rcu_read_unlock();
163         }
164         return 0;
165 }
166
167 /*
168  * Set the current workstation cell and optionally supply its list of volume
169  * location servers.
170  *
171  *      echo "cell.name:192.168.231.14" >/proc/fs/afs/rootcell
172  */
173 static int afs_proc_rootcell_write(struct file *file, char *buf, size_t size)
174 {
175         struct seq_file *m = file->private_data;
176         struct afs_net *net = afs_seq2net_single(m);
177         char *s;
178         int ret;
179
180         ret = -EINVAL;
181         if (buf[0] == '.')
182                 goto out;
183         if (memchr(buf, '/', size))
184                 goto out;
185
186         /* trim to first NL */
187         s = memchr(buf, '\n', size);
188         if (s)
189                 *s = 0;
190
191         /* determine command to perform */
192         _debug("rootcell=%s", buf);
193
194         ret = afs_cell_init(net, buf);
195
196 out:
197         _leave(" = %d", ret);
198         return ret;
199 }
200
201 static const char afs_vol_types[3][3] = {
202         [AFSVL_RWVOL]   = "RW",
203         [AFSVL_ROVOL]   = "RO",
204         [AFSVL_BACKVOL] = "BK",
205 };
206
207 /*
208  * Display the list of volumes known to a cell.
209  */
210 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
211 {
212         struct afs_volume *vol = hlist_entry(v, struct afs_volume, proc_link);
213
214         /* Display header on line 1 */
215         if (v == SEQ_START_TOKEN) {
216                 seq_puts(m, "USE VID      TY NAME\n");
217                 return 0;
218         }
219
220         seq_printf(m, "%3d %08llx %s %s\n",
221                    atomic_read(&vol->usage), vol->vid,
222                    afs_vol_types[vol->type],
223                    vol->name);
224
225         return 0;
226 }
227
228 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
229         __acquires(cell->proc_lock)
230 {
231         struct afs_cell *cell = PDE_DATA(file_inode(m->file));
232
233         rcu_read_lock();
234         return seq_hlist_start_head_rcu(&cell->proc_volumes, *_pos);
235 }
236
237 static void *afs_proc_cell_volumes_next(struct seq_file *m, void *v,
238                                         loff_t *_pos)
239 {
240         struct afs_cell *cell = PDE_DATA(file_inode(m->file));
241
242         return seq_hlist_next_rcu(v, &cell->proc_volumes, _pos);
243 }
244
245 static void afs_proc_cell_volumes_stop(struct seq_file *m, void *v)
246         __releases(cell->proc_lock)
247 {
248         rcu_read_unlock();
249 }
250
251 static const struct seq_operations afs_proc_cell_volumes_ops = {
252         .start  = afs_proc_cell_volumes_start,
253         .next   = afs_proc_cell_volumes_next,
254         .stop   = afs_proc_cell_volumes_stop,
255         .show   = afs_proc_cell_volumes_show,
256 };
257
258 static const char *const dns_record_sources[NR__dns_record_source + 1] = {
259         [DNS_RECORD_UNAVAILABLE]        = "unav",
260         [DNS_RECORD_FROM_CONFIG]        = "cfg",
261         [DNS_RECORD_FROM_DNS_A]         = "A",
262         [DNS_RECORD_FROM_DNS_AFSDB]     = "AFSDB",
263         [DNS_RECORD_FROM_DNS_SRV]       = "SRV",
264         [DNS_RECORD_FROM_NSS]           = "nss",
265         [NR__dns_record_source]         = "[weird]"
266 };
267
268 static const char *const dns_lookup_statuses[NR__dns_lookup_status + 1] = {
269         [DNS_LOOKUP_NOT_DONE]           = "no-lookup",
270         [DNS_LOOKUP_GOOD]               = "good",
271         [DNS_LOOKUP_GOOD_WITH_BAD]      = "good/bad",
272         [DNS_LOOKUP_BAD]                = "bad",
273         [DNS_LOOKUP_GOT_NOT_FOUND]      = "not-found",
274         [DNS_LOOKUP_GOT_LOCAL_FAILURE]  = "local-failure",
275         [DNS_LOOKUP_GOT_TEMP_FAILURE]   = "temp-failure",
276         [DNS_LOOKUP_GOT_NS_FAILURE]     = "ns-failure",
277         [NR__dns_lookup_status]         = "[weird]"
278 };
279
280 /*
281  * Display the list of Volume Location servers we're using for a cell.
282  */
283 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
284 {
285         const struct afs_vl_seq_net_private *priv = m->private;
286         const struct afs_vlserver_list *vllist = priv->vllist;
287         const struct afs_vlserver_entry *entry;
288         const struct afs_vlserver *vlserver;
289         const struct afs_addr_list *alist;
290         int i;
291
292         if (v == SEQ_START_TOKEN) {
293                 seq_printf(m, "# source %s, status %s\n",
294                            dns_record_sources[vllist ? vllist->source : 0],
295                            dns_lookup_statuses[vllist ? vllist->status : 0]);
296                 return 0;
297         }
298
299         entry = v;
300         vlserver = entry->server;
301         alist = rcu_dereference(vlserver->addresses);
302
303         seq_printf(m, "%s [p=%hu w=%hu s=%s,%s]:\n",
304                    vlserver->name, entry->priority, entry->weight,
305                    dns_record_sources[alist ? alist->source : entry->source],
306                    dns_lookup_statuses[alist ? alist->status : entry->status]);
307         if (alist) {
308                 for (i = 0; i < alist->nr_addrs; i++)
309                         seq_printf(m, " %c %pISpc\n",
310                                    alist->preferred == i ? '>' : '-',
311                                    &alist->addrs[i].transport);
312         }
313         seq_printf(m, " info: fl=%lx rtt=%d\n", vlserver->flags, vlserver->rtt);
314         seq_printf(m, " probe: fl=%x e=%d ac=%d out=%d\n",
315                    vlserver->probe.flags, vlserver->probe.error,
316                    vlserver->probe.abort_code,
317                    atomic_read(&vlserver->probe_outstanding));
318         return 0;
319 }
320
321 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
322         __acquires(rcu)
323 {
324         struct afs_vl_seq_net_private *priv = m->private;
325         struct afs_vlserver_list *vllist;
326         struct afs_cell *cell = PDE_DATA(file_inode(m->file));
327         loff_t pos = *_pos;
328
329         rcu_read_lock();
330
331         vllist = rcu_dereference(cell->vl_servers);
332         priv->vllist = vllist;
333
334         if (pos < 0)
335                 *_pos = pos = 0;
336         if (pos == 0)
337                 return SEQ_START_TOKEN;
338
339         if (pos - 1 >= vllist->nr_servers)
340                 return NULL;
341
342         return &vllist->servers[pos - 1];
343 }
344
345 static void *afs_proc_cell_vlservers_next(struct seq_file *m, void *v,
346                                           loff_t *_pos)
347 {
348         struct afs_vl_seq_net_private *priv = m->private;
349         struct afs_vlserver_list *vllist = priv->vllist;
350         loff_t pos;
351
352         pos = *_pos;
353         pos++;
354         *_pos = pos;
355         if (!vllist || pos - 1 >= vllist->nr_servers)
356                 return NULL;
357
358         return &vllist->servers[pos - 1];
359 }
360
361 static void afs_proc_cell_vlservers_stop(struct seq_file *m, void *v)
362         __releases(rcu)
363 {
364         rcu_read_unlock();
365 }
366
367 static const struct seq_operations afs_proc_cell_vlservers_ops = {
368         .start  = afs_proc_cell_vlservers_start,
369         .next   = afs_proc_cell_vlservers_next,
370         .stop   = afs_proc_cell_vlservers_stop,
371         .show   = afs_proc_cell_vlservers_show,
372 };
373
374 /*
375  * Display the list of fileservers we're using within a namespace.
376  */
377 static int afs_proc_servers_show(struct seq_file *m, void *v)
378 {
379         struct afs_server *server;
380         struct afs_addr_list *alist;
381         int i;
382
383         if (v == SEQ_START_TOKEN) {
384                 seq_puts(m, "UUID                                 REF ACT\n");
385                 return 0;
386         }
387
388         server = list_entry(v, struct afs_server, proc_link);
389         alist = rcu_dereference(server->addresses);
390         seq_printf(m, "%pU %3d %3d\n",
391                    &server->uuid,
392                    atomic_read(&server->ref),
393                    atomic_read(&server->active));
394         seq_printf(m, "  - info: fl=%lx rtt=%u brk=%x\n",
395                    server->flags, server->rtt, server->cb_s_break);
396         seq_printf(m, "  - probe: last=%d out=%d\n",
397                    (int)(jiffies - server->probed_at) / HZ,
398                    atomic_read(&server->probe_outstanding));
399         seq_printf(m, "  - ALIST v=%u rsp=%lx f=%lx\n",
400                    alist->version, alist->responded, alist->failed);
401         for (i = 0; i < alist->nr_addrs; i++)
402                 seq_printf(m, "    [%x] %pISpc%s\n",
403                            i, &alist->addrs[i].transport,
404                            alist->preferred == i ? "*" : "");
405         return 0;
406 }
407
408 static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
409         __acquires(rcu)
410 {
411         rcu_read_lock();
412         return seq_hlist_start_head_rcu(&afs_seq2net(m)->fs_proc, *_pos);
413 }
414
415 static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
416 {
417         return seq_hlist_next_rcu(v, &afs_seq2net(m)->fs_proc, _pos);
418 }
419
420 static void afs_proc_servers_stop(struct seq_file *m, void *v)
421         __releases(rcu)
422 {
423         rcu_read_unlock();
424 }
425
426 static const struct seq_operations afs_proc_servers_ops = {
427         .start  = afs_proc_servers_start,
428         .next   = afs_proc_servers_next,
429         .stop   = afs_proc_servers_stop,
430         .show   = afs_proc_servers_show,
431 };
432
433 /*
434  * Display the list of strings that may be substituted for the @sys pathname
435  * macro.
436  */
437 static int afs_proc_sysname_show(struct seq_file *m, void *v)
438 {
439         struct afs_net *net = afs_seq2net(m);
440         struct afs_sysnames *sysnames = net->sysnames;
441         unsigned int i = (unsigned long)v - 1;
442
443         if (i < sysnames->nr)
444                 seq_printf(m, "%s\n", sysnames->subs[i]);
445         return 0;
446 }
447
448 static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
449         __acquires(&net->sysnames_lock)
450 {
451         struct afs_net *net = afs_seq2net(m);
452         struct afs_sysnames *names;
453
454         read_lock(&net->sysnames_lock);
455
456         names = net->sysnames;
457         if (*pos >= names->nr)
458                 return NULL;
459         return (void *)(unsigned long)(*pos + 1);
460 }
461
462 static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
463 {
464         struct afs_net *net = afs_seq2net(m);
465         struct afs_sysnames *names = net->sysnames;
466
467         *pos += 1;
468         if (*pos >= names->nr)
469                 return NULL;
470         return (void *)(unsigned long)(*pos + 1);
471 }
472
473 static void afs_proc_sysname_stop(struct seq_file *m, void *v)
474         __releases(&net->sysnames_lock)
475 {
476         struct afs_net *net = afs_seq2net(m);
477
478         read_unlock(&net->sysnames_lock);
479 }
480
481 static const struct seq_operations afs_proc_sysname_ops = {
482         .start  = afs_proc_sysname_start,
483         .next   = afs_proc_sysname_next,
484         .stop   = afs_proc_sysname_stop,
485         .show   = afs_proc_sysname_show,
486 };
487
488 /*
489  * Allow the @sys substitution to be configured.
490  */
491 static int afs_proc_sysname_write(struct file *file, char *buf, size_t size)
492 {
493         struct afs_sysnames *sysnames, *kill;
494         struct seq_file *m = file->private_data;
495         struct afs_net *net = afs_seq2net(m);
496         char *s, *p, *sub;
497         int ret, len;
498
499         sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
500         if (!sysnames)
501                 return -ENOMEM;
502         refcount_set(&sysnames->usage, 1);
503         kill = sysnames;
504
505         p = buf;
506         while ((s = strsep(&p, " \t\n"))) {
507                 len = strlen(s);
508                 if (len == 0)
509                         continue;
510                 ret = -ENAMETOOLONG;
511                 if (len >= AFSNAMEMAX)
512                         goto error;
513
514                 if (len >= 4 &&
515                     s[len - 4] == '@' &&
516                     s[len - 3] == 's' &&
517                     s[len - 2] == 'y' &&
518                     s[len - 1] == 's')
519                         /* Protect against recursion */
520                         goto invalid;
521
522                 if (s[0] == '.' &&
523                     (len < 2 || (len == 2 && s[1] == '.')))
524                         goto invalid;
525
526                 if (memchr(s, '/', len))
527                         goto invalid;
528
529                 ret = -EFBIG;
530                 if (sysnames->nr >= AFS_NR_SYSNAME)
531                         goto out;
532
533                 if (strcmp(s, afs_init_sysname) == 0) {
534                         sub = (char *)afs_init_sysname;
535                 } else {
536                         ret = -ENOMEM;
537                         sub = kmemdup(s, len + 1, GFP_KERNEL);
538                         if (!sub)
539                                 goto out;
540                 }
541
542                 sysnames->subs[sysnames->nr] = sub;
543                 sysnames->nr++;
544         }
545
546         if (sysnames->nr == 0) {
547                 sysnames->subs[0] = sysnames->blank;
548                 sysnames->nr++;
549         }
550
551         write_lock(&net->sysnames_lock);
552         kill = net->sysnames;
553         net->sysnames = sysnames;
554         write_unlock(&net->sysnames_lock);
555         ret = 0;
556 out:
557         afs_put_sysnames(kill);
558         return ret;
559
560 invalid:
561         ret = -EINVAL;
562 error:
563         goto out;
564 }
565
566 void afs_put_sysnames(struct afs_sysnames *sysnames)
567 {
568         int i;
569
570         if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
571                 for (i = 0; i < sysnames->nr; i++)
572                         if (sysnames->subs[i] != afs_init_sysname &&
573                             sysnames->subs[i] != sysnames->blank)
574                                 kfree(sysnames->subs[i]);
575                 kfree(sysnames);
576         }
577 }
578
579 /*
580  * Display general per-net namespace statistics
581  */
582 static int afs_proc_stats_show(struct seq_file *m, void *v)
583 {
584         struct afs_net *net = afs_seq2net_single(m);
585
586         seq_puts(m, "kAFS statistics\n");
587
588         seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
589                    atomic_read(&net->n_lookup),
590                    atomic_read(&net->n_reval),
591                    atomic_read(&net->n_inval),
592                    atomic_read(&net->n_relpg));
593
594         seq_printf(m, "dir-data: rdpg=%u\n",
595                    atomic_read(&net->n_read_dir));
596
597         seq_printf(m, "dir-edit: cr=%u rm=%u\n",
598                    atomic_read(&net->n_dir_cr),
599                    atomic_read(&net->n_dir_rm));
600
601         seq_printf(m, "file-rd : n=%u nb=%lu\n",
602                    atomic_read(&net->n_fetches),
603                    atomic_long_read(&net->n_fetch_bytes));
604         seq_printf(m, "file-wr : n=%u nb=%lu\n",
605                    atomic_read(&net->n_stores),
606                    atomic_long_read(&net->n_store_bytes));
607         return 0;
608 }
609
610 /*
611  * initialise /proc/fs/afs/<cell>/
612  */
613 int afs_proc_cell_setup(struct afs_cell *cell)
614 {
615         struct proc_dir_entry *dir;
616         struct afs_net *net = cell->net;
617
618         _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
619
620         dir = proc_net_mkdir(net->net, cell->name, net->proc_afs);
621         if (!dir)
622                 goto error_dir;
623
624         if (!proc_create_net_data("vlservers", 0444, dir,
625                                   &afs_proc_cell_vlservers_ops,
626                                   sizeof(struct afs_vl_seq_net_private),
627                                   cell) ||
628             !proc_create_net_data("volumes", 0444, dir,
629                                   &afs_proc_cell_volumes_ops,
630                                   sizeof(struct seq_net_private),
631                                   cell))
632                 goto error_tree;
633
634         _leave(" = 0");
635         return 0;
636
637 error_tree:
638         remove_proc_subtree(cell->name, net->proc_afs);
639 error_dir:
640         _leave(" = -ENOMEM");
641         return -ENOMEM;
642 }
643
644 /*
645  * remove /proc/fs/afs/<cell>/
646  */
647 void afs_proc_cell_remove(struct afs_cell *cell)
648 {
649         struct afs_net *net = cell->net;
650
651         _enter("");
652         remove_proc_subtree(cell->name, net->proc_afs);
653         _leave("");
654 }
655
656 /*
657  * initialise the /proc/fs/afs/ directory
658  */
659 int afs_proc_init(struct afs_net *net)
660 {
661         struct proc_dir_entry *p;
662
663         _enter("");
664
665         p = proc_net_mkdir(net->net, "afs", net->net->proc_net);
666         if (!p)
667                 goto error_dir;
668
669         if (!proc_create_net_data_write("cells", 0644, p,
670                                         &afs_proc_cells_ops,
671                                         afs_proc_cells_write,
672                                         sizeof(struct seq_net_private),
673                                         NULL) ||
674             !proc_create_net_single_write("rootcell", 0644, p,
675                                           afs_proc_rootcell_show,
676                                           afs_proc_rootcell_write,
677                                           NULL) ||
678             !proc_create_net("servers", 0444, p, &afs_proc_servers_ops,
679                              sizeof(struct seq_net_private)) ||
680             !proc_create_net_single("stats", 0444, p, afs_proc_stats_show, NULL) ||
681             !proc_create_net_data_write("sysname", 0644, p,
682                                         &afs_proc_sysname_ops,
683                                         afs_proc_sysname_write,
684                                         sizeof(struct seq_net_private),
685                                         NULL))
686                 goto error_tree;
687
688         net->proc_afs = p;
689         _leave(" = 0");
690         return 0;
691
692 error_tree:
693         proc_remove(p);
694 error_dir:
695         _leave(" = -ENOMEM");
696         return -ENOMEM;
697 }
698
699 /*
700  * clean up the /proc/fs/afs/ directory
701  */
702 void afs_proc_cleanup(struct afs_net *net)
703 {
704         proc_remove(net->proc_afs);
705         net->proc_afs = NULL;
706 }