1 /* /proc interface for AFS
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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_proc2net(struct file *f)
25 static inline struct afs_net *afs_seq2net(struct seq_file *m)
27 return &__afs_net; // TODO: use seq_file_net(m)
30 static int afs_proc_cells_open(struct inode *inode, struct file *file);
31 static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
32 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
33 static void afs_proc_cells_stop(struct seq_file *p, void *v);
34 static int afs_proc_cells_show(struct seq_file *m, void *v);
35 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
36 size_t size, loff_t *_pos);
38 static const struct seq_operations afs_proc_cells_ops = {
39 .start = afs_proc_cells_start,
40 .next = afs_proc_cells_next,
41 .stop = afs_proc_cells_stop,
42 .show = afs_proc_cells_show,
45 static const struct file_operations afs_proc_cells_fops = {
46 .open = afs_proc_cells_open,
48 .write = afs_proc_cells_write,
50 .release = seq_release,
53 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
54 size_t size, loff_t *_pos);
55 static ssize_t afs_proc_rootcell_write(struct file *file,
56 const char __user *buf,
57 size_t size, loff_t *_pos);
59 static const struct file_operations afs_proc_rootcell_fops = {
60 .read = afs_proc_rootcell_read,
61 .write = afs_proc_rootcell_write,
65 static void *afs_proc_cell_volumes_start(struct seq_file *p, loff_t *pos);
66 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
68 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v);
69 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v);
71 static const struct seq_operations afs_proc_cell_volumes_ops = {
72 .start = afs_proc_cell_volumes_start,
73 .next = afs_proc_cell_volumes_next,
74 .stop = afs_proc_cell_volumes_stop,
75 .show = afs_proc_cell_volumes_show,
78 static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
79 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
81 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
82 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
84 static const struct seq_operations afs_proc_cell_vlservers_ops = {
85 .start = afs_proc_cell_vlservers_start,
86 .next = afs_proc_cell_vlservers_next,
87 .stop = afs_proc_cell_vlservers_stop,
88 .show = afs_proc_cell_vlservers_show,
91 static void *afs_proc_servers_start(struct seq_file *p, loff_t *pos);
92 static void *afs_proc_servers_next(struct seq_file *p, void *v,
94 static void afs_proc_servers_stop(struct seq_file *p, void *v);
95 static int afs_proc_servers_show(struct seq_file *m, void *v);
97 static const struct seq_operations afs_proc_servers_ops = {
98 .start = afs_proc_servers_start,
99 .next = afs_proc_servers_next,
100 .stop = afs_proc_servers_stop,
101 .show = afs_proc_servers_show,
104 static int afs_proc_sysname_open(struct inode *inode, struct file *file);
105 static int afs_proc_sysname_release(struct inode *inode, struct file *file);
106 static void *afs_proc_sysname_start(struct seq_file *p, loff_t *pos);
107 static void *afs_proc_sysname_next(struct seq_file *p, void *v,
109 static void afs_proc_sysname_stop(struct seq_file *p, void *v);
110 static int afs_proc_sysname_show(struct seq_file *m, void *v);
111 static ssize_t afs_proc_sysname_write(struct file *file,
112 const char __user *buf,
113 size_t size, loff_t *_pos);
115 static const struct seq_operations afs_proc_sysname_ops = {
116 .start = afs_proc_sysname_start,
117 .next = afs_proc_sysname_next,
118 .stop = afs_proc_sysname_stop,
119 .show = afs_proc_sysname_show,
122 static const struct file_operations afs_proc_sysname_fops = {
123 .open = afs_proc_sysname_open,
126 .release = afs_proc_sysname_release,
127 .write = afs_proc_sysname_write,
130 static int afs_proc_stats_show(struct seq_file *m, void *v);
133 * initialise the /proc/fs/afs/ directory
135 int afs_proc_init(struct afs_net *net)
139 net->proc_afs = proc_mkdir("fs/afs", NULL);
143 if (!proc_create("cells", 0644, net->proc_afs, &afs_proc_cells_fops) ||
144 !proc_create("rootcell", 0644, net->proc_afs, &afs_proc_rootcell_fops) ||
145 !proc_create_seq("servers", 0644, net->proc_afs, &afs_proc_servers_ops) ||
146 !proc_create_single("stats", 0644, net->proc_afs, afs_proc_stats_show) ||
147 !proc_create("sysname", 0644, net->proc_afs, &afs_proc_sysname_fops))
154 proc_remove(net->proc_afs);
156 _leave(" = -ENOMEM");
161 * clean up the /proc/fs/afs/ directory
163 void afs_proc_cleanup(struct afs_net *net)
165 proc_remove(net->proc_afs);
166 net->proc_afs = NULL;
170 * open "/proc/fs/afs/cells" which provides a summary of extant cells
172 static int afs_proc_cells_open(struct inode *inode, struct file *file)
174 return seq_open(file, &afs_proc_cells_ops);
178 * set up the iterator to start reading from the cells list and return the
181 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
184 struct afs_net *net = afs_seq2net(m);
187 return seq_list_start_head(&net->proc_cells, *_pos);
191 * move to next cell in cells list
193 static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
195 struct afs_net *net = afs_seq2net(m);
197 return seq_list_next(v, &net->proc_cells, pos);
201 * clean up after reading from the cells list
203 static void afs_proc_cells_stop(struct seq_file *m, void *v)
210 * display a header line followed by a load of cell lines
212 static int afs_proc_cells_show(struct seq_file *m, void *v)
214 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
215 struct afs_net *net = afs_seq2net(m);
217 if (v == &net->proc_cells) {
218 /* display header on line 1 */
219 seq_puts(m, "USE NAME\n");
223 /* display one cell per line on subsequent lines */
224 seq_printf(m, "%3u %s\n", atomic_read(&cell->usage), cell->name);
229 * handle writes to /proc/fs/afs/cells
230 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
232 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
233 size_t size, loff_t *_pos)
235 struct afs_net *net = afs_proc2net(file);
236 char *kbuf, *name, *args;
239 /* start by dragging the command into memory */
240 if (size <= 1 || size >= PAGE_SIZE)
243 kbuf = memdup_user_nul(buf, size);
245 return PTR_ERR(kbuf);
247 /* trim to first NL */
248 name = memchr(kbuf, '\n', size);
252 /* split into command, name and argslist */
253 name = strchr(kbuf, ' ');
258 } while(*name == ' ');
262 args = strchr(name, ' ');
267 } while(*args == ' ');
271 /* determine command to perform */
272 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
274 if (strcmp(kbuf, "add") == 0) {
275 struct afs_cell *cell;
277 cell = afs_lookup_cell(net, name, strlen(name), args, true);
283 if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
284 afs_put_cell(net, cell);
285 printk("kAFS: Added new cell '%s'\n", name);
294 _leave(" = %d", ret);
299 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
303 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
304 size_t size, loff_t *_pos)
306 struct afs_cell *cell;
307 struct afs_net *net = afs_proc2net(file);
308 unsigned int seq = 0;
309 char name[AFS_MAXCELLNAME + 1];
319 read_seqbegin_or_lock(&net->cells_lock, &seq);
321 cell = rcu_dereference_raw(net->ws_cell);
323 len = cell->name_len;
324 memcpy(name, cell->name, len);
326 } while (need_seqretry(&net->cells_lock, seq));
327 done_seqretry(&net->cells_lock, seq);
336 if (copy_to_user(buf, name, len) != 0)
343 * handle writes to /proc/fs/afs/rootcell
344 * - to initialize rootcell: echo "cell.name:192.168.231.14"
346 static ssize_t afs_proc_rootcell_write(struct file *file,
347 const char __user *buf,
348 size_t size, loff_t *_pos)
350 struct afs_net *net = afs_proc2net(file);
354 /* start by dragging the command into memory */
355 if (size <= 1 || size >= PAGE_SIZE)
358 kbuf = memdup_user_nul(buf, size);
360 return PTR_ERR(kbuf);
365 if (memchr(kbuf, '/', size))
368 /* trim to first NL */
369 s = memchr(kbuf, '\n', size);
373 /* determine command to perform */
374 _debug("rootcell=%s", kbuf);
376 ret = afs_cell_init(net, kbuf);
378 ret = size; /* consume everything, always */
382 _leave(" = %d", ret);
387 * initialise /proc/fs/afs/<cell>/
389 int afs_proc_cell_setup(struct afs_net *net, struct afs_cell *cell)
391 struct proc_dir_entry *dir;
393 _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
395 dir = proc_mkdir(cell->name, net->proc_afs);
399 if (!proc_create_seq_data("vlservers", 0, dir,
400 &afs_proc_cell_vlservers_ops, cell))
402 if (!proc_create_seq_data("volumes", 0, dir, &afs_proc_cell_volumes_ops,
410 remove_proc_subtree(cell->name, net->proc_afs);
412 _leave(" = -ENOMEM");
417 * remove /proc/fs/afs/<cell>/
419 void afs_proc_cell_remove(struct afs_net *net, struct afs_cell *cell)
423 remove_proc_subtree(cell->name, net->proc_afs);
429 * set up the iterator to start reading from the cells list and return the
432 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
433 __acquires(cell->proc_lock)
435 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
437 _enter("cell=%p pos=%Ld", cell, *_pos);
439 read_lock(&cell->proc_lock);
440 return seq_list_start_head(&cell->proc_volumes, *_pos);
444 * move to next cell in cells list
446 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
449 struct afs_cell *cell = PDE_DATA(file_inode(p->file));
451 _enter("cell=%p pos=%Ld", cell, *_pos);
452 return seq_list_next(v, &cell->proc_volumes, _pos);
456 * clean up after reading from the cells list
458 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
459 __releases(cell->proc_lock)
461 struct afs_cell *cell = PDE_DATA(file_inode(p->file));
463 read_unlock(&cell->proc_lock);
466 static const char afs_vol_types[3][3] = {
467 [AFSVL_RWVOL] = "RW",
468 [AFSVL_ROVOL] = "RO",
469 [AFSVL_BACKVOL] = "BK",
473 * display a header line followed by a load of volume lines
475 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
477 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
478 struct afs_volume *vol = list_entry(v, struct afs_volume, proc_link);
480 /* Display header on line 1 */
481 if (v == &cell->proc_volumes) {
482 seq_puts(m, "USE VID TY\n");
486 seq_printf(m, "%3d %08x %s\n",
487 atomic_read(&vol->usage), vol->vid,
488 afs_vol_types[vol->type]);
494 * set up the iterator to start reading from the cells list and return the
497 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
500 struct afs_addr_list *alist;
501 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
506 alist = rcu_dereference(cell->vl_addrs);
508 /* allow for the header line */
513 if (!alist || pos >= alist->nr_addrs)
516 return alist->addrs + pos;
520 * move to next cell in cells list
522 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
525 struct afs_addr_list *alist;
526 struct afs_cell *cell = PDE_DATA(file_inode(p->file));
529 alist = rcu_dereference(cell->vl_addrs);
533 if (!alist || pos >= alist->nr_addrs)
536 return alist->addrs + pos;
540 * clean up after reading from the cells list
542 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
549 * display a header line followed by a load of volume lines
551 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
553 struct sockaddr_rxrpc *addr = v;
555 /* display header on line 1 */
556 if (v == (void *)1) {
557 seq_puts(m, "ADDRESS\n");
561 /* display one cell per line on subsequent lines */
562 seq_printf(m, "%pISp\n", &addr->transport);
567 * Set up the iterator to start reading from the server list and return the
570 static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
573 struct afs_net *net = afs_seq2net(m);
576 return seq_hlist_start_head_rcu(&net->fs_proc, *_pos);
580 * move to next cell in cells list
582 static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
584 struct afs_net *net = afs_seq2net(m);
586 return seq_hlist_next_rcu(v, &net->fs_proc, _pos);
590 * clean up after reading from the cells list
592 static void afs_proc_servers_stop(struct seq_file *p, void *v)
599 * display a header line followed by a load of volume lines
601 static int afs_proc_servers_show(struct seq_file *m, void *v)
603 struct afs_server *server;
604 struct afs_addr_list *alist;
606 if (v == SEQ_START_TOKEN) {
607 seq_puts(m, "UUID USE ADDR\n");
611 server = list_entry(v, struct afs_server, proc_link);
612 alist = rcu_dereference(server->addresses);
613 seq_printf(m, "%pU %3d %pISp\n",
615 atomic_read(&server->usage),
616 &alist->addrs[alist->index].transport);
620 void afs_put_sysnames(struct afs_sysnames *sysnames)
624 if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
625 for (i = 0; i < sysnames->nr; i++)
626 if (sysnames->subs[i] != afs_init_sysname &&
627 sysnames->subs[i] != sysnames->blank)
628 kfree(sysnames->subs[i]);
633 * Handle opening of /proc/fs/afs/sysname. If it is opened for writing, we
634 * assume the caller wants to change the substitution list and we allocate a
635 * buffer to hold the list.
637 static int afs_proc_sysname_open(struct inode *inode, struct file *file)
639 struct afs_sysnames *sysnames;
643 ret = seq_open(file, &afs_proc_sysname_ops);
647 if (file->f_mode & FMODE_WRITE) {
648 sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
650 seq_release(inode, file);
654 refcount_set(&sysnames->usage, 1);
655 m = file->private_data;
656 m->private = sysnames;
663 * Handle writes to /proc/fs/afs/sysname to set the @sys substitution.
665 static ssize_t afs_proc_sysname_write(struct file *file,
666 const char __user *buf,
667 size_t size, loff_t *_pos)
669 struct afs_sysnames *sysnames;
670 struct seq_file *m = file->private_data;
671 char *kbuf = NULL, *s, *p, *sub;
674 sysnames = m->private;
678 return sysnames->error;
680 if (size >= PAGE_SIZE - 1) {
681 sysnames->error = -EINVAL;
687 kbuf = memdup_user_nul(buf, size);
689 return PTR_ERR(kbuf);
691 inode_lock(file_inode(file));
694 while ((s = strsep(&p, " \t\n"))) {
699 if (len >= AFSNAMEMAX)
707 /* Protect against recursion */
711 (len < 2 || (len == 2 && s[1] == '.')))
714 if (memchr(s, '/', len))
718 if (sysnames->nr >= AFS_NR_SYSNAME)
721 if (strcmp(s, afs_init_sysname) == 0) {
722 sub = (char *)afs_init_sysname;
725 sub = kmemdup(s, len + 1, GFP_KERNEL);
730 sysnames->subs[sysnames->nr] = sub;
734 ret = size; /* consume everything, always */
736 inode_unlock(file_inode(file));
743 sysnames->error = ret;
747 static int afs_proc_sysname_release(struct inode *inode, struct file *file)
749 struct afs_sysnames *sysnames, *kill = NULL;
750 struct seq_file *m = file->private_data;
751 struct afs_net *net = afs_seq2net(m);
753 sysnames = m->private;
755 if (!sysnames->error) {
757 if (sysnames->nr == 0) {
758 sysnames->subs[0] = sysnames->blank;
761 write_lock(&net->sysnames_lock);
762 kill = net->sysnames;
763 net->sysnames = sysnames;
764 write_unlock(&net->sysnames_lock);
766 afs_put_sysnames(kill);
769 return seq_release(inode, file);
772 static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
773 __acquires(&net->sysnames_lock)
775 struct afs_net *net = afs_seq2net(m);
776 struct afs_sysnames *names = net->sysnames;
778 read_lock(&net->sysnames_lock);
780 if (*pos >= names->nr)
782 return (void *)(unsigned long)(*pos + 1);
785 static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
787 struct afs_net *net = afs_seq2net(m);
788 struct afs_sysnames *names = net->sysnames;
791 if (*pos >= names->nr)
793 return (void *)(unsigned long)(*pos + 1);
796 static void afs_proc_sysname_stop(struct seq_file *m, void *v)
797 __releases(&net->sysnames_lock)
799 struct afs_net *net = afs_seq2net(m);
801 read_unlock(&net->sysnames_lock);
804 static int afs_proc_sysname_show(struct seq_file *m, void *v)
806 struct afs_net *net = afs_seq2net(m);
807 struct afs_sysnames *sysnames = net->sysnames;
808 unsigned int i = (unsigned long)v - 1;
810 if (i < sysnames->nr)
811 seq_printf(m, "%s\n", sysnames->subs[i]);
816 * Display general per-net namespace statistics
818 static int afs_proc_stats_show(struct seq_file *m, void *v)
820 struct afs_net *net = afs_seq2net(m);
822 seq_puts(m, "kAFS statistics\n");
824 seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
825 atomic_read(&net->n_lookup),
826 atomic_read(&net->n_reval),
827 atomic_read(&net->n_inval),
828 atomic_read(&net->n_relpg));
830 seq_printf(m, "dir-data: rdpg=%u\n",
831 atomic_read(&net->n_read_dir));
833 seq_printf(m, "dir-edit: cr=%u rm=%u\n",
834 atomic_read(&net->n_dir_cr),
835 atomic_read(&net->n_dir_rm));
837 seq_printf(m, "file-rd : n=%u nb=%lu\n",
838 atomic_read(&net->n_fetches),
839 atomic_long_read(&net->n_fetch_bytes));
840 seq_printf(m, "file-wr : n=%u nb=%lu\n",
841 atomic_read(&net->n_stores),
842 atomic_long_read(&net->n_store_bytes));