xref: /linux/fs/afs/proc.c (revision aa23aa55166c2865ac430168c4b9d405cf8c6980)
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\n");
217 		return 0;
218 	}
219 
220 	seq_printf(m, "%3d %08llx %s\n",
221 		   atomic_read(&vol->usage), vol->vid,
222 		   afs_vol_types[vol->type]);
223 
224 	return 0;
225 }
226 
227 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
228 	__acquires(cell->proc_lock)
229 {
230 	struct afs_cell *cell = PDE_DATA(file_inode(m->file));
231 
232 	read_lock(&cell->proc_lock);
233 	return seq_list_start_head(&cell->proc_volumes, *_pos);
234 }
235 
236 static void *afs_proc_cell_volumes_next(struct seq_file *m, void *v,
237 					loff_t *_pos)
238 {
239 	struct afs_cell *cell = PDE_DATA(file_inode(m->file));
240 
241 	return seq_list_next(v, &cell->proc_volumes, _pos);
242 }
243 
244 static void afs_proc_cell_volumes_stop(struct seq_file *m, void *v)
245 	__releases(cell->proc_lock)
246 {
247 	struct afs_cell *cell = PDE_DATA(file_inode(m->file));
248 
249 	read_unlock(&cell->proc_lock);
250 }
251 
252 static const struct seq_operations afs_proc_cell_volumes_ops = {
253 	.start	= afs_proc_cell_volumes_start,
254 	.next	= afs_proc_cell_volumes_next,
255 	.stop	= afs_proc_cell_volumes_stop,
256 	.show	= afs_proc_cell_volumes_show,
257 };
258 
259 static const char *const dns_record_sources[NR__dns_record_source + 1] = {
260 	[DNS_RECORD_UNAVAILABLE]	= "unav",
261 	[DNS_RECORD_FROM_CONFIG]	= "cfg",
262 	[DNS_RECORD_FROM_DNS_A]		= "A",
263 	[DNS_RECORD_FROM_DNS_AFSDB]	= "AFSDB",
264 	[DNS_RECORD_FROM_DNS_SRV]	= "SRV",
265 	[DNS_RECORD_FROM_NSS]		= "nss",
266 	[NR__dns_record_source]		= "[weird]"
267 };
268 
269 static const char *const dns_lookup_statuses[NR__dns_lookup_status + 1] = {
270 	[DNS_LOOKUP_NOT_DONE]		= "no-lookup",
271 	[DNS_LOOKUP_GOOD]		= "good",
272 	[DNS_LOOKUP_GOOD_WITH_BAD]	= "good/bad",
273 	[DNS_LOOKUP_BAD]		= "bad",
274 	[DNS_LOOKUP_GOT_NOT_FOUND]	= "not-found",
275 	[DNS_LOOKUP_GOT_LOCAL_FAILURE]	= "local-failure",
276 	[DNS_LOOKUP_GOT_TEMP_FAILURE]	= "temp-failure",
277 	[DNS_LOOKUP_GOT_NS_FAILURE]	= "ns-failure",
278 	[NR__dns_lookup_status]		= "[weird]"
279 };
280 
281 /*
282  * Display the list of Volume Location servers we're using for a cell.
283  */
284 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
285 {
286 	const struct afs_vl_seq_net_private *priv = m->private;
287 	const struct afs_vlserver_list *vllist = priv->vllist;
288 	const struct afs_vlserver_entry *entry;
289 	const struct afs_vlserver *vlserver;
290 	const struct afs_addr_list *alist;
291 	int i;
292 
293 	if (v == SEQ_START_TOKEN) {
294 		seq_printf(m, "# source %s, status %s\n",
295 			   dns_record_sources[vllist ? vllist->source : 0],
296 			   dns_lookup_statuses[vllist ? vllist->status : 0]);
297 		return 0;
298 	}
299 
300 	entry = v;
301 	vlserver = entry->server;
302 	alist = rcu_dereference(vlserver->addresses);
303 
304 	seq_printf(m, "%s [p=%hu w=%hu s=%s,%s]:\n",
305 		   vlserver->name, entry->priority, entry->weight,
306 		   dns_record_sources[alist ? alist->source : entry->source],
307 		   dns_lookup_statuses[alist ? alist->status : entry->status]);
308 	if (alist) {
309 		for (i = 0; i < alist->nr_addrs; i++)
310 			seq_printf(m, " %c %pISpc\n",
311 				   alist->preferred == i ? '>' : '-',
312 				   &alist->addrs[i].transport);
313 	}
314 	return 0;
315 }
316 
317 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
318 	__acquires(rcu)
319 {
320 	struct afs_vl_seq_net_private *priv = m->private;
321 	struct afs_vlserver_list *vllist;
322 	struct afs_cell *cell = PDE_DATA(file_inode(m->file));
323 	loff_t pos = *_pos;
324 
325 	rcu_read_lock();
326 
327 	vllist = rcu_dereference(cell->vl_servers);
328 	priv->vllist = vllist;
329 
330 	if (pos < 0)
331 		*_pos = pos = 0;
332 	if (pos == 0)
333 		return SEQ_START_TOKEN;
334 
335 	if (pos - 1 >= vllist->nr_servers)
336 		return NULL;
337 
338 	return &vllist->servers[pos - 1];
339 }
340 
341 static void *afs_proc_cell_vlservers_next(struct seq_file *m, void *v,
342 					  loff_t *_pos)
343 {
344 	struct afs_vl_seq_net_private *priv = m->private;
345 	struct afs_vlserver_list *vllist = priv->vllist;
346 	loff_t pos;
347 
348 	pos = *_pos;
349 	pos++;
350 	*_pos = pos;
351 	if (!vllist || pos - 1 >= vllist->nr_servers)
352 		return NULL;
353 
354 	return &vllist->servers[pos - 1];
355 }
356 
357 static void afs_proc_cell_vlservers_stop(struct seq_file *m, void *v)
358 	__releases(rcu)
359 {
360 	rcu_read_unlock();
361 }
362 
363 static const struct seq_operations afs_proc_cell_vlservers_ops = {
364 	.start	= afs_proc_cell_vlservers_start,
365 	.next	= afs_proc_cell_vlservers_next,
366 	.stop	= afs_proc_cell_vlservers_stop,
367 	.show	= afs_proc_cell_vlservers_show,
368 };
369 
370 /*
371  * Display the list of fileservers we're using within a namespace.
372  */
373 static int afs_proc_servers_show(struct seq_file *m, void *v)
374 {
375 	struct afs_server *server;
376 	struct afs_addr_list *alist;
377 	int i;
378 
379 	if (v == SEQ_START_TOKEN) {
380 		seq_puts(m, "UUID                                 USE ADDR\n");
381 		return 0;
382 	}
383 
384 	server = list_entry(v, struct afs_server, proc_link);
385 	alist = rcu_dereference(server->addresses);
386 	seq_printf(m, "%pU %3d %pISpc%s\n",
387 		   &server->uuid,
388 		   atomic_read(&server->usage),
389 		   &alist->addrs[0].transport,
390 		   alist->preferred == 0 ? "*" : "");
391 	for (i = 1; i < alist->nr_addrs; i++)
392 		seq_printf(m, "                                         %pISpc%s\n",
393 			   &alist->addrs[i].transport,
394 			   alist->preferred == i ? "*" : "");
395 	return 0;
396 }
397 
398 static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
399 	__acquires(rcu)
400 {
401 	rcu_read_lock();
402 	return seq_hlist_start_head_rcu(&afs_seq2net(m)->fs_proc, *_pos);
403 }
404 
405 static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
406 {
407 	return seq_hlist_next_rcu(v, &afs_seq2net(m)->fs_proc, _pos);
408 }
409 
410 static void afs_proc_servers_stop(struct seq_file *m, void *v)
411 	__releases(rcu)
412 {
413 	rcu_read_unlock();
414 }
415 
416 static const struct seq_operations afs_proc_servers_ops = {
417 	.start	= afs_proc_servers_start,
418 	.next	= afs_proc_servers_next,
419 	.stop	= afs_proc_servers_stop,
420 	.show	= afs_proc_servers_show,
421 };
422 
423 /*
424  * Display the list of strings that may be substituted for the @sys pathname
425  * macro.
426  */
427 static int afs_proc_sysname_show(struct seq_file *m, void *v)
428 {
429 	struct afs_net *net = afs_seq2net(m);
430 	struct afs_sysnames *sysnames = net->sysnames;
431 	unsigned int i = (unsigned long)v - 1;
432 
433 	if (i < sysnames->nr)
434 		seq_printf(m, "%s\n", sysnames->subs[i]);
435 	return 0;
436 }
437 
438 static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
439 	__acquires(&net->sysnames_lock)
440 {
441 	struct afs_net *net = afs_seq2net(m);
442 	struct afs_sysnames *names;
443 
444 	read_lock(&net->sysnames_lock);
445 
446 	names = net->sysnames;
447 	if (*pos >= names->nr)
448 		return NULL;
449 	return (void *)(unsigned long)(*pos + 1);
450 }
451 
452 static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
453 {
454 	struct afs_net *net = afs_seq2net(m);
455 	struct afs_sysnames *names = net->sysnames;
456 
457 	*pos += 1;
458 	if (*pos >= names->nr)
459 		return NULL;
460 	return (void *)(unsigned long)(*pos + 1);
461 }
462 
463 static void afs_proc_sysname_stop(struct seq_file *m, void *v)
464 	__releases(&net->sysnames_lock)
465 {
466 	struct afs_net *net = afs_seq2net(m);
467 
468 	read_unlock(&net->sysnames_lock);
469 }
470 
471 static const struct seq_operations afs_proc_sysname_ops = {
472 	.start	= afs_proc_sysname_start,
473 	.next	= afs_proc_sysname_next,
474 	.stop	= afs_proc_sysname_stop,
475 	.show	= afs_proc_sysname_show,
476 };
477 
478 /*
479  * Allow the @sys substitution to be configured.
480  */
481 static int afs_proc_sysname_write(struct file *file, char *buf, size_t size)
482 {
483 	struct afs_sysnames *sysnames, *kill;
484 	struct seq_file *m = file->private_data;
485 	struct afs_net *net = afs_seq2net(m);
486 	char *s, *p, *sub;
487 	int ret, len;
488 
489 	sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
490 	if (!sysnames)
491 		return -ENOMEM;
492 	refcount_set(&sysnames->usage, 1);
493 	kill = sysnames;
494 
495 	p = buf;
496 	while ((s = strsep(&p, " \t\n"))) {
497 		len = strlen(s);
498 		if (len == 0)
499 			continue;
500 		ret = -ENAMETOOLONG;
501 		if (len >= AFSNAMEMAX)
502 			goto error;
503 
504 		if (len >= 4 &&
505 		    s[len - 4] == '@' &&
506 		    s[len - 3] == 's' &&
507 		    s[len - 2] == 'y' &&
508 		    s[len - 1] == 's')
509 			/* Protect against recursion */
510 			goto invalid;
511 
512 		if (s[0] == '.' &&
513 		    (len < 2 || (len == 2 && s[1] == '.')))
514 			goto invalid;
515 
516 		if (memchr(s, '/', len))
517 			goto invalid;
518 
519 		ret = -EFBIG;
520 		if (sysnames->nr >= AFS_NR_SYSNAME)
521 			goto out;
522 
523 		if (strcmp(s, afs_init_sysname) == 0) {
524 			sub = (char *)afs_init_sysname;
525 		} else {
526 			ret = -ENOMEM;
527 			sub = kmemdup(s, len + 1, GFP_KERNEL);
528 			if (!sub)
529 				goto out;
530 		}
531 
532 		sysnames->subs[sysnames->nr] = sub;
533 		sysnames->nr++;
534 	}
535 
536 	if (sysnames->nr == 0) {
537 		sysnames->subs[0] = sysnames->blank;
538 		sysnames->nr++;
539 	}
540 
541 	write_lock(&net->sysnames_lock);
542 	kill = net->sysnames;
543 	net->sysnames = sysnames;
544 	write_unlock(&net->sysnames_lock);
545 	ret = 0;
546 out:
547 	afs_put_sysnames(kill);
548 	return ret;
549 
550 invalid:
551 	ret = -EINVAL;
552 error:
553 	goto out;
554 }
555 
556 void afs_put_sysnames(struct afs_sysnames *sysnames)
557 {
558 	int i;
559 
560 	if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
561 		for (i = 0; i < sysnames->nr; i++)
562 			if (sysnames->subs[i] != afs_init_sysname &&
563 			    sysnames->subs[i] != sysnames->blank)
564 				kfree(sysnames->subs[i]);
565 	}
566 }
567 
568 /*
569  * Display general per-net namespace statistics
570  */
571 static int afs_proc_stats_show(struct seq_file *m, void *v)
572 {
573 	struct afs_net *net = afs_seq2net_single(m);
574 
575 	seq_puts(m, "kAFS statistics\n");
576 
577 	seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
578 		   atomic_read(&net->n_lookup),
579 		   atomic_read(&net->n_reval),
580 		   atomic_read(&net->n_inval),
581 		   atomic_read(&net->n_relpg));
582 
583 	seq_printf(m, "dir-data: rdpg=%u\n",
584 		   atomic_read(&net->n_read_dir));
585 
586 	seq_printf(m, "dir-edit: cr=%u rm=%u\n",
587 		   atomic_read(&net->n_dir_cr),
588 		   atomic_read(&net->n_dir_rm));
589 
590 	seq_printf(m, "file-rd : n=%u nb=%lu\n",
591 		   atomic_read(&net->n_fetches),
592 		   atomic_long_read(&net->n_fetch_bytes));
593 	seq_printf(m, "file-wr : n=%u nb=%lu\n",
594 		   atomic_read(&net->n_stores),
595 		   atomic_long_read(&net->n_store_bytes));
596 	return 0;
597 }
598 
599 /*
600  * initialise /proc/fs/afs/<cell>/
601  */
602 int afs_proc_cell_setup(struct afs_cell *cell)
603 {
604 	struct proc_dir_entry *dir;
605 	struct afs_net *net = cell->net;
606 
607 	_enter("%p{%s},%p", cell, cell->name, net->proc_afs);
608 
609 	dir = proc_net_mkdir(net->net, cell->name, net->proc_afs);
610 	if (!dir)
611 		goto error_dir;
612 
613 	if (!proc_create_net_data("vlservers", 0444, dir,
614 				  &afs_proc_cell_vlservers_ops,
615 				  sizeof(struct afs_vl_seq_net_private),
616 				  cell) ||
617 	    !proc_create_net_data("volumes", 0444, dir,
618 				  &afs_proc_cell_volumes_ops,
619 				  sizeof(struct seq_net_private),
620 				  cell))
621 		goto error_tree;
622 
623 	_leave(" = 0");
624 	return 0;
625 
626 error_tree:
627 	remove_proc_subtree(cell->name, net->proc_afs);
628 error_dir:
629 	_leave(" = -ENOMEM");
630 	return -ENOMEM;
631 }
632 
633 /*
634  * remove /proc/fs/afs/<cell>/
635  */
636 void afs_proc_cell_remove(struct afs_cell *cell)
637 {
638 	struct afs_net *net = cell->net;
639 
640 	_enter("");
641 	remove_proc_subtree(cell->name, net->proc_afs);
642 	_leave("");
643 }
644 
645 /*
646  * initialise the /proc/fs/afs/ directory
647  */
648 int afs_proc_init(struct afs_net *net)
649 {
650 	struct proc_dir_entry *p;
651 
652 	_enter("");
653 
654 	p = proc_net_mkdir(net->net, "afs", net->net->proc_net);
655 	if (!p)
656 		goto error_dir;
657 
658 	if (!proc_create_net_data_write("cells", 0644, p,
659 					&afs_proc_cells_ops,
660 					afs_proc_cells_write,
661 					sizeof(struct seq_net_private),
662 					NULL) ||
663 	    !proc_create_net_single_write("rootcell", 0644, p,
664 					  afs_proc_rootcell_show,
665 					  afs_proc_rootcell_write,
666 					  NULL) ||
667 	    !proc_create_net("servers", 0444, p, &afs_proc_servers_ops,
668 			     sizeof(struct seq_net_private)) ||
669 	    !proc_create_net_single("stats", 0444, p, afs_proc_stats_show, NULL) ||
670 	    !proc_create_net_data_write("sysname", 0644, p,
671 					&afs_proc_sysname_ops,
672 					afs_proc_sysname_write,
673 					sizeof(struct seq_net_private),
674 					NULL))
675 		goto error_tree;
676 
677 	net->proc_afs = p;
678 	_leave(" = 0");
679 	return 0;
680 
681 error_tree:
682 	proc_remove(p);
683 error_dir:
684 	_leave(" = -ENOMEM");
685 	return -ENOMEM;
686 }
687 
688 /*
689  * clean up the /proc/fs/afs/ directory
690  */
691 void afs_proc_cleanup(struct afs_net *net)
692 {
693 	proc_remove(net->proc_afs);
694 	net->proc_afs = NULL;
695 }
696