1 /* proc.c: /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/sched.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
19 #include <asm/uaccess.h>
22 static struct proc_dir_entry *proc_afs;
25 static int afs_proc_cells_open(struct inode *inode, struct file *file);
26 static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
27 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
28 static void afs_proc_cells_stop(struct seq_file *p, void *v);
29 static int afs_proc_cells_show(struct seq_file *m, void *v);
30 static ssize_t afs_proc_cells_write(struct file *file, const char *buf,
31 size_t size, loff_t *_pos);
33 static struct seq_operations afs_proc_cells_ops = {
34 .start = afs_proc_cells_start,
35 .next = afs_proc_cells_next,
36 .stop = afs_proc_cells_stop,
37 .show = afs_proc_cells_show,
40 static struct file_operations afs_proc_cells_fops = {
41 .open = afs_proc_cells_open,
43 .write = afs_proc_cells_write,
45 .release = seq_release,
48 static int afs_proc_rootcell_open(struct inode *inode, struct file *file);
49 static int afs_proc_rootcell_release(struct inode *inode, struct file *file);
50 static ssize_t afs_proc_rootcell_read(struct file *file, char *buf,
51 size_t size, loff_t *_pos);
52 static ssize_t afs_proc_rootcell_write(struct file *file, const char *buf,
53 size_t size, loff_t *_pos);
55 static 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 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 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 struct file_operations afs_proc_cell_servers_fops = {
127 .open = afs_proc_cell_servers_open,
130 .release = afs_proc_cell_servers_release,
133 /*****************************************************************************/
135 * initialise the /proc/fs/afs/ directory
137 int afs_proc_init(void)
139 struct proc_dir_entry *p;
143 proc_afs = proc_mkdir("fs/afs", NULL);
146 proc_afs->owner = THIS_MODULE;
148 p = create_proc_entry("cells", 0, proc_afs);
151 p->proc_fops = &afs_proc_cells_fops;
152 p->owner = THIS_MODULE;
154 p = create_proc_entry("rootcell", 0, proc_afs);
157 p->proc_fops = &afs_proc_rootcell_fops;
158 p->owner = THIS_MODULE;
164 remove_proc_entry("cells", proc_afs);
166 remove_proc_entry("fs/afs", NULL);
168 _leave(" = -ENOMEM");
171 } /* end afs_proc_init() */
173 /*****************************************************************************/
175 * clean up the /proc/fs/afs/ directory
177 void afs_proc_cleanup(void)
179 remove_proc_entry("cells", proc_afs);
181 remove_proc_entry("fs/afs", NULL);
183 } /* end afs_proc_cleanup() */
185 /*****************************************************************************/
187 * open "/proc/fs/afs/cells" which provides a summary of extant cells
189 static int afs_proc_cells_open(struct inode *inode, struct file *file)
194 ret = seq_open(file, &afs_proc_cells_ops);
198 m = file->private_data;
199 m->private = PDE(inode)->data;
202 } /* end afs_proc_cells_open() */
204 /*****************************************************************************/
206 * set up the iterator to start reading from the cells list and return the
209 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
211 struct list_head *_p;
214 /* lock the list against modification */
215 down_read(&afs_proc_cells_sem);
217 /* allow for the header line */
222 /* find the n'th element in the list */
223 list_for_each(_p, &afs_proc_cells)
227 return _p != &afs_proc_cells ? _p : NULL;
228 } /* end afs_proc_cells_start() */
230 /*****************************************************************************/
232 * move to next cell in cells list
234 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos)
236 struct list_head *_p;
241 _p = v == (void *) 1 ? afs_proc_cells.next : _p->next;
243 return _p != &afs_proc_cells ? _p : NULL;
244 } /* end afs_proc_cells_next() */
246 /*****************************************************************************/
248 * clean up after reading from the cells list
250 static void afs_proc_cells_stop(struct seq_file *p, void *v)
252 up_read(&afs_proc_cells_sem);
254 } /* end afs_proc_cells_stop() */
256 /*****************************************************************************/
258 * display a header line followed by a load of cell lines
260 static int afs_proc_cells_show(struct seq_file *m, void *v)
262 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
264 /* display header on line 1 */
265 if (v == (void *) 1) {
266 seq_puts(m, "USE NAME\n");
270 /* display one cell per line on subsequent lines */
271 seq_printf(m, "%3d %s\n", atomic_read(&cell->usage), cell->name);
274 } /* end afs_proc_cells_show() */
276 /*****************************************************************************/
278 * handle writes to /proc/fs/afs/cells
279 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
281 static ssize_t afs_proc_cells_write(struct file *file, const char *buf,
282 size_t size, loff_t *_pos)
284 char *kbuf, *name, *args;
287 /* start by dragging the command into memory */
288 if (size <= 1 || size >= PAGE_SIZE)
291 kbuf = kmalloc(size + 1, GFP_KERNEL);
296 if (copy_from_user(kbuf, buf, size) != 0)
300 /* trim to first NL */
301 name = memchr(kbuf, '\n', size);
305 /* split into command, name and argslist */
306 name = strchr(kbuf, ' ');
311 } while(*name == ' ');
315 args = strchr(name, ' ');
320 } while(*args == ' ');
324 /* determine command to perform */
325 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
327 if (strcmp(kbuf, "add") == 0) {
328 struct afs_cell *cell;
329 ret = afs_cell_create(name, args, &cell);
333 printk("kAFS: Added new cell '%s'\n", name);
343 _leave(" = %d", ret);
348 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
350 } /* end afs_proc_cells_write() */
352 /*****************************************************************************/
354 * Stubs for /proc/fs/afs/rootcell
356 static int afs_proc_rootcell_open(struct inode *inode, struct file *file)
361 static int afs_proc_rootcell_release(struct inode *inode, struct file *file)
366 static ssize_t afs_proc_rootcell_read(struct file *file, char *buf,
367 size_t size, loff_t *_pos)
372 /*****************************************************************************/
374 * handle writes to /proc/fs/afs/rootcell
375 * - to initialize rootcell: echo "cell.name:192.168.231.14"
377 static ssize_t afs_proc_rootcell_write(struct file *file, const char *buf,
378 size_t size, loff_t *_pos)
383 /* start by dragging the command into memory */
384 if (size <= 1 || size >= PAGE_SIZE)
388 kbuf = kmalloc(size + 1, GFP_KERNEL);
393 if (copy_from_user(kbuf, buf, size) != 0)
397 /* trim to first NL */
398 s = memchr(kbuf, '\n', size);
402 /* determine command to perform */
403 _debug("rootcell=%s", kbuf);
405 ret = afs_cell_init(kbuf);
407 ret = size; /* consume everything, always */
412 _leave(" = %d", ret);
414 } /* end afs_proc_rootcell_write() */
416 /*****************************************************************************/
418 * initialise /proc/fs/afs/<cell>/
420 int afs_proc_cell_setup(struct afs_cell *cell)
422 struct proc_dir_entry *p;
424 _enter("%p{%s}", cell, cell->name);
426 cell->proc_dir = proc_mkdir(cell->name, proc_afs);
430 p = create_proc_entry("servers", 0, cell->proc_dir);
433 p->proc_fops = &afs_proc_cell_servers_fops;
434 p->owner = THIS_MODULE;
437 p = create_proc_entry("vlservers", 0, cell->proc_dir);
440 p->proc_fops = &afs_proc_cell_vlservers_fops;
441 p->owner = THIS_MODULE;
444 p = create_proc_entry("volumes", 0, cell->proc_dir);
446 goto error_vlservers;
447 p->proc_fops = &afs_proc_cell_volumes_fops;
448 p->owner = THIS_MODULE;
455 remove_proc_entry("vlservers", cell->proc_dir);
457 remove_proc_entry("servers", cell->proc_dir);
459 remove_proc_entry(cell->name, proc_afs);
460 _leave(" = -ENOMEM");
462 } /* end afs_proc_cell_setup() */
464 /*****************************************************************************/
466 * remove /proc/fs/afs/<cell>/
468 void afs_proc_cell_remove(struct afs_cell *cell)
472 remove_proc_entry("volumes", cell->proc_dir);
473 remove_proc_entry("vlservers", cell->proc_dir);
474 remove_proc_entry("servers", cell->proc_dir);
475 remove_proc_entry(cell->name, proc_afs);
478 } /* end afs_proc_cell_remove() */
480 /*****************************************************************************/
482 * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
484 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
486 struct afs_cell *cell;
490 cell = afs_get_cell_maybe((struct afs_cell **) &PDE(inode)->data);
494 ret = seq_open(file, &afs_proc_cell_volumes_ops);
498 m = file->private_data;
502 } /* end afs_proc_cell_volumes_open() */
504 /*****************************************************************************/
506 * close the file and release the ref to the cell
508 static int afs_proc_cell_volumes_release(struct inode *inode, struct file *file)
510 struct afs_cell *cell = PDE(inode)->data;
513 ret = seq_release(inode,file);
518 } /* end afs_proc_cell_volumes_release() */
520 /*****************************************************************************/
522 * set up the iterator to start reading from the cells list and return the
525 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
527 struct list_head *_p;
528 struct afs_cell *cell = m->private;
531 _enter("cell=%p pos=%Ld", cell, *_pos);
533 /* lock the list against modification */
534 down_read(&cell->vl_sem);
536 /* allow for the header line */
541 /* find the n'th element in the list */
542 list_for_each(_p, &cell->vl_list)
546 return _p != &cell->vl_list ? _p : NULL;
547 } /* end afs_proc_cell_volumes_start() */
549 /*****************************************************************************/
551 * move to next cell in cells list
553 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
556 struct list_head *_p;
557 struct afs_cell *cell = p->private;
559 _enter("cell=%p pos=%Ld", cell, *_pos);
564 _p = v == (void *) 1 ? cell->vl_list.next : _p->next;
566 return _p != &cell->vl_list ? _p : NULL;
567 } /* end afs_proc_cell_volumes_next() */
569 /*****************************************************************************/
571 * clean up after reading from the cells list
573 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
575 struct afs_cell *cell = p->private;
577 up_read(&cell->vl_sem);
579 } /* end afs_proc_cell_volumes_stop() */
581 /*****************************************************************************/
583 * display a header line followed by a load of volume lines
585 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
587 struct afs_vlocation *vlocation =
588 list_entry(v, struct afs_vlocation, link);
590 /* display header on line 1 */
591 if (v == (void *) 1) {
592 seq_puts(m, "USE VLID[0] VLID[1] VLID[2] NAME\n");
596 /* display one cell per line on subsequent lines */
597 seq_printf(m, "%3d %08x %08x %08x %s\n",
598 atomic_read(&vlocation->usage),
599 vlocation->vldb.vid[0],
600 vlocation->vldb.vid[1],
601 vlocation->vldb.vid[2],
606 } /* end afs_proc_cell_volumes_show() */
608 /*****************************************************************************/
610 * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
613 static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
615 struct afs_cell *cell;
619 cell = afs_get_cell_maybe((struct afs_cell**)&PDE(inode)->data);
623 ret = seq_open(file,&afs_proc_cell_vlservers_ops);
627 m = file->private_data;
631 } /* end afs_proc_cell_vlservers_open() */
633 /*****************************************************************************/
635 * close the file and release the ref to the cell
637 static int afs_proc_cell_vlservers_release(struct inode *inode,
640 struct afs_cell *cell = PDE(inode)->data;
643 ret = seq_release(inode,file);
648 } /* end afs_proc_cell_vlservers_release() */
650 /*****************************************************************************/
652 * set up the iterator to start reading from the cells list and return the
655 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
657 struct afs_cell *cell = m->private;
660 _enter("cell=%p pos=%Ld", cell, *_pos);
662 /* lock the list against modification */
663 down_read(&cell->vl_sem);
665 /* allow for the header line */
670 if (pos >= cell->vl_naddrs)
673 return &cell->vl_addrs[pos];
674 } /* end afs_proc_cell_vlservers_start() */
676 /*****************************************************************************/
678 * move to next cell in cells list
680 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
683 struct afs_cell *cell = p->private;
686 _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
690 if (pos >= cell->vl_naddrs)
693 return &cell->vl_addrs[pos];
694 } /* end afs_proc_cell_vlservers_next() */
696 /*****************************************************************************/
698 * clean up after reading from the cells list
700 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
702 struct afs_cell *cell = p->private;
704 up_read(&cell->vl_sem);
706 } /* end afs_proc_cell_vlservers_stop() */
708 /*****************************************************************************/
710 * display a header line followed by a load of volume lines
712 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
714 struct in_addr *addr = v;
716 /* display header on line 1 */
717 if (v == (struct in_addr *) 1) {
718 seq_puts(m, "ADDRESS\n");
722 /* display one cell per line on subsequent lines */
723 seq_printf(m, "%u.%u.%u.%u\n", NIPQUAD(addr->s_addr));
726 } /* end afs_proc_cell_vlservers_show() */
728 /*****************************************************************************/
730 * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
733 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
735 struct afs_cell *cell;
739 cell = afs_get_cell_maybe((struct afs_cell **) &PDE(inode)->data);
743 ret = seq_open(file, &afs_proc_cell_servers_ops);
747 m = file->private_data;
751 } /* end afs_proc_cell_servers_open() */
753 /*****************************************************************************/
755 * close the file and release the ref to the cell
757 static int afs_proc_cell_servers_release(struct inode *inode,
760 struct afs_cell *cell = PDE(inode)->data;
763 ret = seq_release(inode, file);
768 } /* end afs_proc_cell_servers_release() */
770 /*****************************************************************************/
772 * set up the iterator to start reading from the cells list and return the
775 static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
777 struct list_head *_p;
778 struct afs_cell *cell = m->private;
781 _enter("cell=%p pos=%Ld", cell, *_pos);
783 /* lock the list against modification */
784 read_lock(&cell->sv_lock);
786 /* allow for the header line */
791 /* find the n'th element in the list */
792 list_for_each(_p, &cell->sv_list)
796 return _p != &cell->sv_list ? _p : NULL;
797 } /* end afs_proc_cell_servers_start() */
799 /*****************************************************************************/
801 * move to next cell in cells list
803 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
806 struct list_head *_p;
807 struct afs_cell *cell = p->private;
809 _enter("cell=%p pos=%Ld", cell, *_pos);
814 _p = v == (void *) 1 ? cell->sv_list.next : _p->next;
816 return _p != &cell->sv_list ? _p : NULL;
817 } /* end afs_proc_cell_servers_next() */
819 /*****************************************************************************/
821 * clean up after reading from the cells list
823 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
825 struct afs_cell *cell = p->private;
827 read_unlock(&cell->sv_lock);
829 } /* end afs_proc_cell_servers_stop() */
831 /*****************************************************************************/
833 * display a header line followed by a load of volume lines
835 static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
837 struct afs_server *server = list_entry(v, struct afs_server, link);
840 /* display header on line 1 */
841 if (v == (void *) 1) {
842 seq_puts(m, "USE ADDR STATE\n");
846 /* display one cell per line on subsequent lines */
847 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(server->addr));
848 seq_printf(m, "%3d %-15.15s %5d\n",
849 atomic_read(&server->usage),
855 } /* end afs_proc_cell_servers_show() */