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>
18 #include <asm/uaccess.h>
21 static struct proc_dir_entry *proc_afs;
24 static int afs_proc_cells_open(struct inode *inode, struct file *file);
25 static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
26 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
27 static void afs_proc_cells_stop(struct seq_file *p, void *v);
28 static int afs_proc_cells_show(struct seq_file *m, void *v);
29 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
30 size_t size, loff_t *_pos);
32 static struct seq_operations afs_proc_cells_ops = {
33 .start = afs_proc_cells_start,
34 .next = afs_proc_cells_next,
35 .stop = afs_proc_cells_stop,
36 .show = afs_proc_cells_show,
39 static const struct file_operations afs_proc_cells_fops = {
40 .open = afs_proc_cells_open,
42 .write = afs_proc_cells_write,
44 .release = seq_release,
47 static int afs_proc_rootcell_open(struct inode *inode, struct file *file);
48 static int afs_proc_rootcell_release(struct inode *inode, struct file *file);
49 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
50 size_t size, loff_t *_pos);
51 static ssize_t afs_proc_rootcell_write(struct file *file,
52 const char __user *buf,
53 size_t size, loff_t *_pos);
55 static const struct file_operations afs_proc_rootcell_fops = {
56 .open = afs_proc_rootcell_open,
57 .read = afs_proc_rootcell_read,
58 .write = afs_proc_rootcell_write,
60 .release = afs_proc_rootcell_release
63 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file);
64 static int afs_proc_cell_volumes_release(struct inode *inode,
66 static void *afs_proc_cell_volumes_start(struct seq_file *p, loff_t *pos);
67 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
69 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v);
70 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v);
72 static struct seq_operations afs_proc_cell_volumes_ops = {
73 .start = afs_proc_cell_volumes_start,
74 .next = afs_proc_cell_volumes_next,
75 .stop = afs_proc_cell_volumes_stop,
76 .show = afs_proc_cell_volumes_show,
79 static const struct file_operations afs_proc_cell_volumes_fops = {
80 .open = afs_proc_cell_volumes_open,
83 .release = afs_proc_cell_volumes_release,
86 static int afs_proc_cell_vlservers_open(struct inode *inode,
88 static int afs_proc_cell_vlservers_release(struct inode *inode,
90 static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
91 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
93 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
94 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
96 static struct seq_operations afs_proc_cell_vlservers_ops = {
97 .start = afs_proc_cell_vlservers_start,
98 .next = afs_proc_cell_vlservers_next,
99 .stop = afs_proc_cell_vlservers_stop,
100 .show = afs_proc_cell_vlservers_show,
103 static const struct file_operations afs_proc_cell_vlservers_fops = {
104 .open = afs_proc_cell_vlservers_open,
107 .release = afs_proc_cell_vlservers_release,
110 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file);
111 static int afs_proc_cell_servers_release(struct inode *inode,
113 static void *afs_proc_cell_servers_start(struct seq_file *p, loff_t *pos);
114 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
116 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v);
117 static int afs_proc_cell_servers_show(struct seq_file *m, void *v);
119 static struct seq_operations afs_proc_cell_servers_ops = {
120 .start = afs_proc_cell_servers_start,
121 .next = afs_proc_cell_servers_next,
122 .stop = afs_proc_cell_servers_stop,
123 .show = afs_proc_cell_servers_show,
126 static const struct file_operations afs_proc_cell_servers_fops = {
127 .open = afs_proc_cell_servers_open,
130 .release = afs_proc_cell_servers_release,
134 * initialise the /proc/fs/afs/ directory
136 int afs_proc_init(void)
138 struct proc_dir_entry *p;
142 proc_afs = proc_mkdir("fs/afs", NULL);
145 proc_afs->owner = THIS_MODULE;
147 p = create_proc_entry("cells", 0, proc_afs);
150 p->proc_fops = &afs_proc_cells_fops;
151 p->owner = THIS_MODULE;
153 p = create_proc_entry("rootcell", 0, proc_afs);
156 p->proc_fops = &afs_proc_rootcell_fops;
157 p->owner = THIS_MODULE;
163 remove_proc_entry("cells", proc_afs);
165 remove_proc_entry("fs/afs", NULL);
167 _leave(" = -ENOMEM");
172 * clean up the /proc/fs/afs/ directory
174 void afs_proc_cleanup(void)
176 remove_proc_entry("rootcell", proc_afs);
177 remove_proc_entry("cells", proc_afs);
178 remove_proc_entry("fs/afs", NULL);
182 * open "/proc/fs/afs/cells" which provides a summary of extant cells
184 static int afs_proc_cells_open(struct inode *inode, struct file *file)
189 ret = seq_open(file, &afs_proc_cells_ops);
193 m = file->private_data;
194 m->private = PDE(inode)->data;
200 * set up the iterator to start reading from the cells list and return the
203 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
205 struct list_head *_p;
208 /* lock the list against modification */
209 down_read(&afs_proc_cells_sem);
211 /* allow for the header line */
216 /* find the n'th element in the list */
217 list_for_each(_p, &afs_proc_cells)
221 return _p != &afs_proc_cells ? _p : NULL;
225 * move to next cell in cells list
227 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos)
229 struct list_head *_p;
234 _p = v == (void *) 1 ? afs_proc_cells.next : _p->next;
236 return _p != &afs_proc_cells ? _p : NULL;
240 * clean up after reading from the cells list
242 static void afs_proc_cells_stop(struct seq_file *p, void *v)
244 up_read(&afs_proc_cells_sem);
248 * display a header line followed by a load of cell lines
250 static int afs_proc_cells_show(struct seq_file *m, void *v)
252 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
254 if (v == (void *) 1) {
255 /* display header on line 1 */
256 seq_puts(m, "USE NAME\n");
260 /* display one cell per line on subsequent lines */
261 seq_printf(m, "%3d %s\n",
262 atomic_read(&cell->usage), cell->name);
267 * handle writes to /proc/fs/afs/cells
268 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
270 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
271 size_t size, loff_t *_pos)
273 char *kbuf, *name, *args;
276 /* start by dragging the command into memory */
277 if (size <= 1 || size >= PAGE_SIZE)
280 kbuf = kmalloc(size + 1, GFP_KERNEL);
285 if (copy_from_user(kbuf, buf, size) != 0)
289 /* trim to first NL */
290 name = memchr(kbuf, '\n', size);
294 /* split into command, name and argslist */
295 name = strchr(kbuf, ' ');
300 } while(*name == ' ');
304 args = strchr(name, ' ');
309 } while(*args == ' ');
313 /* determine command to perform */
314 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
316 if (strcmp(kbuf, "add") == 0) {
317 struct afs_cell *cell;
318 ret = afs_cell_create(name, args, &cell);
322 printk("kAFS: Added new cell '%s'\n", name);
331 _leave(" = %d", ret);
336 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
341 * Stubs for /proc/fs/afs/rootcell
343 static int afs_proc_rootcell_open(struct inode *inode, struct file *file)
348 static int afs_proc_rootcell_release(struct inode *inode, struct file *file)
353 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
354 size_t size, loff_t *_pos)
360 * handle writes to /proc/fs/afs/rootcell
361 * - to initialize rootcell: echo "cell.name:192.168.231.14"
363 static ssize_t afs_proc_rootcell_write(struct file *file,
364 const char __user *buf,
365 size_t size, loff_t *_pos)
370 /* start by dragging the command into memory */
371 if (size <= 1 || size >= PAGE_SIZE)
375 kbuf = kmalloc(size + 1, GFP_KERNEL);
380 if (copy_from_user(kbuf, buf, size) != 0)
384 /* trim to first NL */
385 s = memchr(kbuf, '\n', size);
389 /* determine command to perform */
390 _debug("rootcell=%s", kbuf);
392 ret = afs_cell_init(kbuf);
394 ret = size; /* consume everything, always */
399 _leave(" = %d", ret);
404 * initialise /proc/fs/afs/<cell>/
406 int afs_proc_cell_setup(struct afs_cell *cell)
408 struct proc_dir_entry *p;
410 _enter("%p{%s}", cell, cell->name);
412 cell->proc_dir = proc_mkdir(cell->name, proc_afs);
416 p = create_proc_entry("servers", 0, cell->proc_dir);
419 p->proc_fops = &afs_proc_cell_servers_fops;
420 p->owner = THIS_MODULE;
423 p = create_proc_entry("vlservers", 0, cell->proc_dir);
425 goto error_vlservers;
426 p->proc_fops = &afs_proc_cell_vlservers_fops;
427 p->owner = THIS_MODULE;
430 p = create_proc_entry("volumes", 0, cell->proc_dir);
433 p->proc_fops = &afs_proc_cell_volumes_fops;
434 p->owner = THIS_MODULE;
441 remove_proc_entry("vlservers", cell->proc_dir);
443 remove_proc_entry("servers", cell->proc_dir);
445 remove_proc_entry(cell->name, proc_afs);
447 _leave(" = -ENOMEM");
452 * remove /proc/fs/afs/<cell>/
454 void afs_proc_cell_remove(struct afs_cell *cell)
458 remove_proc_entry("volumes", cell->proc_dir);
459 remove_proc_entry("vlservers", cell->proc_dir);
460 remove_proc_entry("servers", cell->proc_dir);
461 remove_proc_entry(cell->name, proc_afs);
467 * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
469 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
471 struct afs_cell *cell;
475 cell = afs_get_cell_maybe((struct afs_cell **) &PDE(inode)->data);
479 ret = seq_open(file, &afs_proc_cell_volumes_ops);
483 m = file->private_data;
490 * close the file and release the ref to the cell
492 static int afs_proc_cell_volumes_release(struct inode *inode, struct file *file)
494 struct afs_cell *cell = PDE(inode)->data;
497 ret = seq_release(inode, file);
504 * set up the iterator to start reading from the cells list and return the
507 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
509 struct list_head *_p;
510 struct afs_cell *cell = m->private;
513 _enter("cell=%p pos=%Ld", cell, *_pos);
515 /* lock the list against modification */
516 down_read(&cell->vl_sem);
518 /* allow for the header line */
523 /* find the n'th element in the list */
524 list_for_each(_p, &cell->vl_list)
528 return _p != &cell->vl_list ? _p : NULL;
532 * move to next cell in cells list
534 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
537 struct list_head *_p;
538 struct afs_cell *cell = p->private;
540 _enter("cell=%p pos=%Ld", cell, *_pos);
545 _p = (v == (void *) 1) ? cell->vl_list.next : _p->next;
547 return (_p != &cell->vl_list) ? _p : NULL;
551 * clean up after reading from the cells list
553 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
555 struct afs_cell *cell = p->private;
557 up_read(&cell->vl_sem);
561 * display a header line followed by a load of volume lines
563 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
565 struct afs_vlocation *vlocation =
566 list_entry(v, struct afs_vlocation, link);
568 /* display header on line 1 */
569 if (v == (void *) 1) {
570 seq_puts(m, "USE VLID[0] VLID[1] VLID[2] NAME\n");
574 /* display one cell per line on subsequent lines */
575 seq_printf(m, "%3d %08x %08x %08x %s\n",
576 atomic_read(&vlocation->usage),
577 vlocation->vldb.vid[0],
578 vlocation->vldb.vid[1],
579 vlocation->vldb.vid[2],
580 vlocation->vldb.name);
586 * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
589 static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
591 struct afs_cell *cell;
595 cell = afs_get_cell_maybe((struct afs_cell**)&PDE(inode)->data);
599 ret = seq_open(file,&afs_proc_cell_vlservers_ops);
603 m = file->private_data;
610 * close the file and release the ref to the cell
612 static int afs_proc_cell_vlservers_release(struct inode *inode,
615 struct afs_cell *cell = PDE(inode)->data;
618 ret = seq_release(inode,file);
625 * set up the iterator to start reading from the cells list and return the
628 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
630 struct afs_cell *cell = m->private;
633 _enter("cell=%p pos=%Ld", cell, *_pos);
635 /* lock the list against modification */
636 down_read(&cell->vl_sem);
638 /* allow for the header line */
643 if (pos >= cell->vl_naddrs)
646 return &cell->vl_addrs[pos];
650 * move to next cell in cells list
652 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
655 struct afs_cell *cell = p->private;
658 _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
662 if (pos >= cell->vl_naddrs)
665 return &cell->vl_addrs[pos];
669 * clean up after reading from the cells list
671 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
673 struct afs_cell *cell = p->private;
675 up_read(&cell->vl_sem);
679 * display a header line followed by a load of volume lines
681 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
683 struct in_addr *addr = v;
685 /* display header on line 1 */
686 if (v == (struct in_addr *) 1) {
687 seq_puts(m, "ADDRESS\n");
691 /* display one cell per line on subsequent lines */
692 seq_printf(m, "%u.%u.%u.%u\n", NIPQUAD(addr->s_addr));
697 * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
700 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
702 struct afs_cell *cell;
706 cell = afs_get_cell_maybe((struct afs_cell **) &PDE(inode)->data);
710 ret = seq_open(file, &afs_proc_cell_servers_ops);
714 m = file->private_data;
720 * close the file and release the ref to the cell
722 static int afs_proc_cell_servers_release(struct inode *inode,
725 struct afs_cell *cell = PDE(inode)->data;
728 ret = seq_release(inode, file);
735 * set up the iterator to start reading from the cells list and return the
738 static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
739 __acquires(m->private->sv_lock)
741 struct list_head *_p;
742 struct afs_cell *cell = m->private;
745 _enter("cell=%p pos=%Ld", cell, *_pos);
747 /* lock the list against modification */
748 read_lock(&cell->sv_lock);
750 /* allow for the header line */
755 /* find the n'th element in the list */
756 list_for_each(_p, &cell->sv_list)
760 return _p != &cell->sv_list ? _p : NULL;
764 * move to next cell in cells list
766 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
769 struct list_head *_p;
770 struct afs_cell *cell = p->private;
772 _enter("cell=%p pos=%Ld", cell, *_pos);
777 _p = v == (void *) 1 ? cell->sv_list.next : _p->next;
779 return _p != &cell->sv_list ? _p : NULL;
783 * clean up after reading from the cells list
785 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
786 __releases(p->private->sv_lock)
788 struct afs_cell *cell = p->private;
790 read_unlock(&cell->sv_lock);
794 * display a header line followed by a load of volume lines
796 static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
798 struct afs_server *server = list_entry(v, struct afs_server, link);
801 /* display header on line 1 */
802 if (v == (void *) 1) {
803 seq_puts(m, "USE ADDR STATE\n");
807 /* display one cell per line on subsequent lines */
808 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(server->addr));
809 seq_printf(m, "%3d %-15.15s %5d\n",
810 atomic_read(&server->usage), ipaddr, server->fs_state);