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