1 /* /proc interface for AFS
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/sched.h>
17 #include <linux/uaccess.h>
20 static inline struct afs_net *afs_seq2net(struct seq_file *m)
22 return afs_net(seq_file_net(m));
25 static inline struct afs_net *afs_seq2net_single(struct seq_file *m)
27 return afs_net(seq_file_single_net(m));
31 * Display the list of cells known to the namespace.
33 static int afs_proc_cells_show(struct seq_file *m, void *v)
35 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
36 struct afs_net *net = afs_seq2net(m);
38 if (v == &net->proc_cells) {
39 /* display header on line 1 */
40 seq_puts(m, "USE NAME\n");
44 /* display one cell per line on subsequent lines */
45 seq_printf(m, "%3u %s\n", atomic_read(&cell->usage), cell->name);
49 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
53 return seq_list_start_head(&afs_seq2net(m)->proc_cells, *_pos);
56 static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
58 return seq_list_next(v, &afs_seq2net(m)->proc_cells, pos);
61 static void afs_proc_cells_stop(struct seq_file *m, void *v)
67 static const struct seq_operations afs_proc_cells_ops = {
68 .start = afs_proc_cells_start,
69 .next = afs_proc_cells_next,
70 .stop = afs_proc_cells_stop,
71 .show = afs_proc_cells_show,
75 * handle writes to /proc/fs/afs/cells
76 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
78 static int afs_proc_cells_write(struct file *file, char *buf, size_t size)
80 struct seq_file *m = file->private_data;
81 struct afs_net *net = afs_seq2net(m);
85 /* trim to first NL */
86 name = memchr(buf, '\n', size);
90 /* split into command, name and argslist */
91 name = strchr(buf, ' ');
96 } while(*name == ' ');
100 args = strchr(name, ' ');
105 } while(*args == ' ');
109 /* determine command to perform */
110 _debug("cmd=%s name=%s args=%s", buf, name, args);
112 if (strcmp(buf, "add") == 0) {
113 struct afs_cell *cell;
115 cell = afs_lookup_cell(net, name, strlen(name), args, true);
121 if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
122 afs_put_cell(net, cell);
123 printk("kAFS: Added new cell '%s'\n", name);
131 _leave(" = %d", ret);
136 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
141 * Display the name of the current workstation cell.
143 static int afs_proc_rootcell_show(struct seq_file *m, void *v)
145 struct afs_cell *cell;
148 net = afs_seq2net_single(m);
149 if (rcu_access_pointer(net->ws_cell)) {
151 cell = rcu_dereference(net->ws_cell);
153 seq_printf(m, "%s\n", cell->name);
160 * Set the current workstation cell and optionally supply its list of volume
163 * echo "cell.name:192.168.231.14" >/proc/fs/afs/rootcell
165 static int afs_proc_rootcell_write(struct file *file, char *buf, size_t size)
167 struct seq_file *m = file->private_data;
168 struct afs_net *net = afs_seq2net_single(m);
175 if (memchr(buf, '/', size))
178 /* trim to first NL */
179 s = memchr(buf, '\n', size);
183 /* determine command to perform */
184 _debug("rootcell=%s", buf);
186 ret = afs_cell_init(net, buf);
189 _leave(" = %d", ret);
193 static const char afs_vol_types[3][3] = {
194 [AFSVL_RWVOL] = "RW",
195 [AFSVL_ROVOL] = "RO",
196 [AFSVL_BACKVOL] = "BK",
200 * Display the list of volumes known to a cell.
202 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
204 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
205 struct afs_volume *vol = list_entry(v, struct afs_volume, proc_link);
207 /* Display header on line 1 */
208 if (v == &cell->proc_volumes) {
209 seq_puts(m, "USE VID TY\n");
213 seq_printf(m, "%3d %08x %s\n",
214 atomic_read(&vol->usage), vol->vid,
215 afs_vol_types[vol->type]);
220 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
221 __acquires(cell->proc_lock)
223 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
225 read_lock(&cell->proc_lock);
226 return seq_list_start_head(&cell->proc_volumes, *_pos);
229 static void *afs_proc_cell_volumes_next(struct seq_file *m, void *v,
232 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
234 return seq_list_next(v, &cell->proc_volumes, _pos);
237 static void afs_proc_cell_volumes_stop(struct seq_file *m, void *v)
238 __releases(cell->proc_lock)
240 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
242 read_unlock(&cell->proc_lock);
245 static const struct seq_operations afs_proc_cell_volumes_ops = {
246 .start = afs_proc_cell_volumes_start,
247 .next = afs_proc_cell_volumes_next,
248 .stop = afs_proc_cell_volumes_stop,
249 .show = afs_proc_cell_volumes_show,
253 * Display the list of Volume Location servers we're using for a cell.
255 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
257 struct sockaddr_rxrpc *addr = v;
259 /* display header on line 1 */
260 if (v == (void *)1) {
261 seq_puts(m, "ADDRESS\n");
265 /* display one cell per line on subsequent lines */
266 seq_printf(m, "%pISp\n", &addr->transport);
270 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
273 struct afs_addr_list *alist;
274 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
279 alist = rcu_dereference(cell->vl_addrs);
281 /* allow for the header line */
286 if (!alist || pos >= alist->nr_addrs)
289 return alist->addrs + pos;
292 static void *afs_proc_cell_vlservers_next(struct seq_file *m, void *v,
295 struct afs_addr_list *alist;
296 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
299 alist = rcu_dereference(cell->vl_addrs);
303 if (!alist || pos >= alist->nr_addrs)
306 return alist->addrs + pos;
309 static void afs_proc_cell_vlservers_stop(struct seq_file *m, void *v)
315 static const struct seq_operations afs_proc_cell_vlservers_ops = {
316 .start = afs_proc_cell_vlservers_start,
317 .next = afs_proc_cell_vlservers_next,
318 .stop = afs_proc_cell_vlservers_stop,
319 .show = afs_proc_cell_vlservers_show,
323 * Display the list of fileservers we're using within a namespace.
325 static int afs_proc_servers_show(struct seq_file *m, void *v)
327 struct afs_server *server;
328 struct afs_addr_list *alist;
330 if (v == SEQ_START_TOKEN) {
331 seq_puts(m, "UUID USE ADDR\n");
335 server = list_entry(v, struct afs_server, proc_link);
336 alist = rcu_dereference(server->addresses);
337 seq_printf(m, "%pU %3d %pISp\n",
339 atomic_read(&server->usage),
340 &alist->addrs[alist->index].transport);
344 static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
348 return seq_hlist_start_head_rcu(&afs_seq2net(m)->fs_proc, *_pos);
351 static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
353 return seq_hlist_next_rcu(v, &afs_seq2net(m)->fs_proc, _pos);
356 static void afs_proc_servers_stop(struct seq_file *m, void *v)
362 static const struct seq_operations afs_proc_servers_ops = {
363 .start = afs_proc_servers_start,
364 .next = afs_proc_servers_next,
365 .stop = afs_proc_servers_stop,
366 .show = afs_proc_servers_show,
370 * Display the list of strings that may be substituted for the @sys pathname
373 static int afs_proc_sysname_show(struct seq_file *m, void *v)
375 struct afs_net *net = afs_seq2net(m);
376 struct afs_sysnames *sysnames = net->sysnames;
377 unsigned int i = (unsigned long)v - 1;
379 if (i < sysnames->nr)
380 seq_printf(m, "%s\n", sysnames->subs[i]);
384 static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
385 __acquires(&net->sysnames_lock)
387 struct afs_net *net = afs_seq2net(m);
388 struct afs_sysnames *names;
390 read_lock(&net->sysnames_lock);
392 names = net->sysnames;
393 if (*pos >= names->nr)
395 return (void *)(unsigned long)(*pos + 1);
398 static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
400 struct afs_net *net = afs_seq2net(m);
401 struct afs_sysnames *names = net->sysnames;
404 if (*pos >= names->nr)
406 return (void *)(unsigned long)(*pos + 1);
409 static void afs_proc_sysname_stop(struct seq_file *m, void *v)
410 __releases(&net->sysnames_lock)
412 struct afs_net *net = afs_seq2net(m);
414 read_unlock(&net->sysnames_lock);
417 static const struct seq_operations afs_proc_sysname_ops = {
418 .start = afs_proc_sysname_start,
419 .next = afs_proc_sysname_next,
420 .stop = afs_proc_sysname_stop,
421 .show = afs_proc_sysname_show,
425 * Allow the @sys substitution to be configured.
427 static int afs_proc_sysname_write(struct file *file, char *buf, size_t size)
429 struct afs_sysnames *sysnames, *kill;
430 struct seq_file *m = file->private_data;
431 struct afs_net *net = afs_seq2net(m);
435 sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
438 refcount_set(&sysnames->usage, 1);
442 while ((s = strsep(&p, " \t\n"))) {
447 if (len >= AFSNAMEMAX)
455 /* Protect against recursion */
459 (len < 2 || (len == 2 && s[1] == '.')))
462 if (memchr(s, '/', len))
466 if (sysnames->nr >= AFS_NR_SYSNAME)
469 if (strcmp(s, afs_init_sysname) == 0) {
470 sub = (char *)afs_init_sysname;
473 sub = kmemdup(s, len + 1, GFP_KERNEL);
478 sysnames->subs[sysnames->nr] = sub;
482 if (sysnames->nr == 0) {
483 sysnames->subs[0] = sysnames->blank;
487 write_lock(&net->sysnames_lock);
488 kill = net->sysnames;
489 net->sysnames = sysnames;
490 write_unlock(&net->sysnames_lock);
493 afs_put_sysnames(kill);
502 void afs_put_sysnames(struct afs_sysnames *sysnames)
506 if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
507 for (i = 0; i < sysnames->nr; i++)
508 if (sysnames->subs[i] != afs_init_sysname &&
509 sysnames->subs[i] != sysnames->blank)
510 kfree(sysnames->subs[i]);
515 * Display general per-net namespace statistics
517 static int afs_proc_stats_show(struct seq_file *m, void *v)
519 struct afs_net *net = afs_seq2net_single(m);
521 seq_puts(m, "kAFS statistics\n");
523 seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
524 atomic_read(&net->n_lookup),
525 atomic_read(&net->n_reval),
526 atomic_read(&net->n_inval),
527 atomic_read(&net->n_relpg));
529 seq_printf(m, "dir-data: rdpg=%u\n",
530 atomic_read(&net->n_read_dir));
532 seq_printf(m, "dir-edit: cr=%u rm=%u\n",
533 atomic_read(&net->n_dir_cr),
534 atomic_read(&net->n_dir_rm));
536 seq_printf(m, "file-rd : n=%u nb=%lu\n",
537 atomic_read(&net->n_fetches),
538 atomic_long_read(&net->n_fetch_bytes));
539 seq_printf(m, "file-wr : n=%u nb=%lu\n",
540 atomic_read(&net->n_stores),
541 atomic_long_read(&net->n_store_bytes));
546 * initialise /proc/fs/afs/<cell>/
548 int afs_proc_cell_setup(struct afs_cell *cell)
550 struct proc_dir_entry *dir;
551 struct afs_net *net = cell->net;
553 _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
555 dir = proc_net_mkdir(net->net, cell->name, net->proc_afs);
559 if (!proc_create_net_data("vlservers", 0444, dir,
560 &afs_proc_cell_vlservers_ops,
561 sizeof(struct seq_net_private),
563 !proc_create_net_data("volumes", 0444, dir,
564 &afs_proc_cell_volumes_ops,
565 sizeof(struct seq_net_private),
573 remove_proc_subtree(cell->name, net->proc_afs);
575 _leave(" = -ENOMEM");
580 * remove /proc/fs/afs/<cell>/
582 void afs_proc_cell_remove(struct afs_cell *cell)
584 struct afs_net *net = cell->net;
587 remove_proc_subtree(cell->name, net->proc_afs);
592 * initialise the /proc/fs/afs/ directory
594 int afs_proc_init(struct afs_net *net)
596 struct proc_dir_entry *p;
600 p = proc_net_mkdir(net->net, "afs", net->net->proc_net);
604 if (!proc_create_net_data_write("cells", 0644, p,
606 afs_proc_cells_write,
607 sizeof(struct seq_net_private),
609 !proc_create_net_single_write("rootcell", 0644, p,
610 afs_proc_rootcell_show,
611 afs_proc_rootcell_write,
613 !proc_create_net("servers", 0444, p, &afs_proc_servers_ops,
614 sizeof(struct seq_net_private)) ||
615 !proc_create_net_single("stats", 0444, p, afs_proc_stats_show, NULL) ||
616 !proc_create_net_data_write("sysname", 0644, p,
617 &afs_proc_sysname_ops,
618 afs_proc_sysname_write,
619 sizeof(struct seq_net_private),
630 _leave(" = -ENOMEM");
635 * clean up the /proc/fs/afs/ directory
637 void afs_proc_cleanup(struct afs_net *net)
639 proc_remove(net->proc_afs);
640 net->proc_afs = NULL;