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 <asm/uaccess.h>
19 static struct proc_dir_entry *proc_afs;
22 static int afs_proc_cells_open(struct inode *inode, struct file *file);
23 static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
24 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
25 static void afs_proc_cells_stop(struct seq_file *p, void *v);
26 static int afs_proc_cells_show(struct seq_file *m, void *v);
27 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
28 size_t size, loff_t *_pos);
30 static struct seq_operations afs_proc_cells_ops = {
31 .start = afs_proc_cells_start,
32 .next = afs_proc_cells_next,
33 .stop = afs_proc_cells_stop,
34 .show = afs_proc_cells_show,
37 static const struct file_operations afs_proc_cells_fops = {
38 .open = afs_proc_cells_open,
40 .write = afs_proc_cells_write,
42 .release = seq_release,
45 static int afs_proc_rootcell_open(struct inode *inode, struct file *file);
46 static int afs_proc_rootcell_release(struct inode *inode, struct file *file);
47 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
48 size_t size, loff_t *_pos);
49 static ssize_t afs_proc_rootcell_write(struct file *file,
50 const char __user *buf,
51 size_t size, loff_t *_pos);
53 static const struct file_operations afs_proc_rootcell_fops = {
54 .open = afs_proc_rootcell_open,
55 .read = afs_proc_rootcell_read,
56 .write = afs_proc_rootcell_write,
58 .release = afs_proc_rootcell_release
61 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file);
62 static int afs_proc_cell_volumes_release(struct inode *inode,
64 static void *afs_proc_cell_volumes_start(struct seq_file *p, loff_t *pos);
65 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
67 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v);
68 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v);
70 static struct seq_operations afs_proc_cell_volumes_ops = {
71 .start = afs_proc_cell_volumes_start,
72 .next = afs_proc_cell_volumes_next,
73 .stop = afs_proc_cell_volumes_stop,
74 .show = afs_proc_cell_volumes_show,
77 static const struct file_operations afs_proc_cell_volumes_fops = {
78 .open = afs_proc_cell_volumes_open,
81 .release = afs_proc_cell_volumes_release,
84 static int afs_proc_cell_vlservers_open(struct inode *inode,
86 static int afs_proc_cell_vlservers_release(struct inode *inode,
88 static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
89 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
91 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
92 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
94 static struct seq_operations afs_proc_cell_vlservers_ops = {
95 .start = afs_proc_cell_vlservers_start,
96 .next = afs_proc_cell_vlservers_next,
97 .stop = afs_proc_cell_vlservers_stop,
98 .show = afs_proc_cell_vlservers_show,
101 static const struct file_operations afs_proc_cell_vlservers_fops = {
102 .open = afs_proc_cell_vlservers_open,
105 .release = afs_proc_cell_vlservers_release,
108 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file);
109 static int afs_proc_cell_servers_release(struct inode *inode,
111 static void *afs_proc_cell_servers_start(struct seq_file *p, loff_t *pos);
112 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
114 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v);
115 static int afs_proc_cell_servers_show(struct seq_file *m, void *v);
117 static struct seq_operations afs_proc_cell_servers_ops = {
118 .start = afs_proc_cell_servers_start,
119 .next = afs_proc_cell_servers_next,
120 .stop = afs_proc_cell_servers_stop,
121 .show = afs_proc_cell_servers_show,
124 static const struct file_operations afs_proc_cell_servers_fops = {
125 .open = afs_proc_cell_servers_open,
128 .release = afs_proc_cell_servers_release,
132 * initialise the /proc/fs/afs/ directory
134 int afs_proc_init(void)
136 struct proc_dir_entry *p;
140 proc_afs = proc_mkdir("fs/afs", NULL);
143 proc_afs->owner = THIS_MODULE;
145 p = create_proc_entry("cells", 0, proc_afs);
148 p->proc_fops = &afs_proc_cells_fops;
149 p->owner = THIS_MODULE;
151 p = create_proc_entry("rootcell", 0, proc_afs);
154 p->proc_fops = &afs_proc_rootcell_fops;
155 p->owner = THIS_MODULE;
161 remove_proc_entry("cells", proc_afs);
163 remove_proc_entry("fs/afs", NULL);
165 _leave(" = -ENOMEM");
170 * clean up the /proc/fs/afs/ directory
172 void afs_proc_cleanup(void)
174 remove_proc_entry("rootcell", proc_afs);
175 remove_proc_entry("cells", proc_afs);
176 remove_proc_entry("fs/afs", NULL);
180 * open "/proc/fs/afs/cells" which provides a summary of extant cells
182 static int afs_proc_cells_open(struct inode *inode, struct file *file)
187 ret = seq_open(file, &afs_proc_cells_ops);
191 m = file->private_data;
192 m->private = PDE(inode)->data;
198 * set up the iterator to start reading from the cells list and return the
201 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
203 struct list_head *_p;
206 /* lock the list against modification */
207 down_read(&afs_proc_cells_sem);
209 /* allow for the header line */
214 /* find the n'th element in the list */
215 list_for_each(_p, &afs_proc_cells)
219 return _p != &afs_proc_cells ? _p : NULL;
223 * move to next cell in cells list
225 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos)
227 struct list_head *_p;
232 _p = v == (void *) 1 ? afs_proc_cells.next : _p->next;
234 return _p != &afs_proc_cells ? _p : NULL;
238 * clean up after reading from the cells list
240 static void afs_proc_cells_stop(struct seq_file *p, void *v)
242 up_read(&afs_proc_cells_sem);
246 * display a header line followed by a load of cell lines
248 static int afs_proc_cells_show(struct seq_file *m, void *v)
250 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
252 if (v == (void *) 1) {
253 /* display header on line 1 */
254 seq_puts(m, "USE NAME\n");
258 /* display one cell per line on subsequent lines */
259 seq_printf(m, "%3d %s\n",
260 atomic_read(&cell->usage), cell->name);
265 * handle writes to /proc/fs/afs/cells
266 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
268 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
269 size_t size, loff_t *_pos)
271 char *kbuf, *name, *args;
274 /* start by dragging the command into memory */
275 if (size <= 1 || size >= PAGE_SIZE)
278 kbuf = kmalloc(size + 1, GFP_KERNEL);
283 if (copy_from_user(kbuf, buf, size) != 0)
287 /* trim to first NL */
288 name = memchr(kbuf, '\n', size);
292 /* split into command, name and argslist */
293 name = strchr(kbuf, ' ');
298 } while(*name == ' ');
302 args = strchr(name, ' ');
307 } while(*args == ' ');
311 /* determine command to perform */
312 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
314 if (strcmp(kbuf, "add") == 0) {
315 struct afs_cell *cell;
317 cell = afs_cell_create(name, args);
324 printk("kAFS: Added new cell '%s'\n", name);
333 _leave(" = %d", ret);
338 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
343 * Stubs for /proc/fs/afs/rootcell
345 static int afs_proc_rootcell_open(struct inode *inode, struct file *file)
350 static int afs_proc_rootcell_release(struct inode *inode, struct file *file)
355 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
356 size_t size, loff_t *_pos)
362 * handle writes to /proc/fs/afs/rootcell
363 * - to initialize rootcell: echo "cell.name:192.168.231.14"
365 static ssize_t afs_proc_rootcell_write(struct file *file,
366 const char __user *buf,
367 size_t size, loff_t *_pos)
372 /* start by dragging the command into memory */
373 if (size <= 1 || size >= PAGE_SIZE)
377 kbuf = kmalloc(size + 1, GFP_KERNEL);
382 if (copy_from_user(kbuf, buf, size) != 0)
386 /* trim to first NL */
387 s = memchr(kbuf, '\n', size);
391 /* determine command to perform */
392 _debug("rootcell=%s", kbuf);
394 ret = afs_cell_init(kbuf);
396 ret = size; /* consume everything, always */
401 _leave(" = %d", ret);
406 * initialise /proc/fs/afs/<cell>/
408 int afs_proc_cell_setup(struct afs_cell *cell)
410 struct proc_dir_entry *p;
412 _enter("%p{%s}", cell, cell->name);
414 cell->proc_dir = proc_mkdir(cell->name, proc_afs);
418 p = create_proc_entry("servers", 0, cell->proc_dir);
421 p->proc_fops = &afs_proc_cell_servers_fops;
422 p->owner = THIS_MODULE;
425 p = create_proc_entry("vlservers", 0, cell->proc_dir);
427 goto error_vlservers;
428 p->proc_fops = &afs_proc_cell_vlservers_fops;
429 p->owner = THIS_MODULE;
432 p = create_proc_entry("volumes", 0, cell->proc_dir);
435 p->proc_fops = &afs_proc_cell_volumes_fops;
436 p->owner = THIS_MODULE;
443 remove_proc_entry("vlservers", cell->proc_dir);
445 remove_proc_entry("servers", cell->proc_dir);
447 remove_proc_entry(cell->name, proc_afs);
449 _leave(" = -ENOMEM");
454 * remove /proc/fs/afs/<cell>/
456 void afs_proc_cell_remove(struct afs_cell *cell)
460 remove_proc_entry("volumes", cell->proc_dir);
461 remove_proc_entry("vlservers", cell->proc_dir);
462 remove_proc_entry("servers", cell->proc_dir);
463 remove_proc_entry(cell->name, proc_afs);
469 * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
471 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
473 struct afs_cell *cell;
477 cell = PDE(inode)->data;
481 ret = seq_open(file, &afs_proc_cell_volumes_ops);
485 m = file->private_data;
492 * close the file and release the ref to the cell
494 static int afs_proc_cell_volumes_release(struct inode *inode, struct file *file)
496 return seq_release(inode, file);
500 * set up the iterator to start reading from the cells list and return the
503 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
505 struct list_head *_p;
506 struct afs_cell *cell = m->private;
509 _enter("cell=%p pos=%Ld", cell, *_pos);
511 /* lock the list against modification */
512 down_read(&cell->vl_sem);
514 /* allow for the header line */
519 /* find the n'th element in the list */
520 list_for_each(_p, &cell->vl_list)
524 return _p != &cell->vl_list ? _p : NULL;
528 * move to next cell in cells list
530 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
533 struct list_head *_p;
534 struct afs_cell *cell = p->private;
536 _enter("cell=%p pos=%Ld", cell, *_pos);
541 _p = (v == (void *) 1) ? cell->vl_list.next : _p->next;
543 return (_p != &cell->vl_list) ? _p : NULL;
547 * clean up after reading from the cells list
549 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
551 struct afs_cell *cell = p->private;
553 up_read(&cell->vl_sem);
556 const char afs_vlocation_states[][4] = {
557 [AFS_VL_NEW] = "New",
558 [AFS_VL_CREATING] = "Crt",
559 [AFS_VL_VALID] = "Val",
560 [AFS_VL_NO_VOLUME] = "NoV",
561 [AFS_VL_UPDATING] = "Upd",
562 [AFS_VL_VOLUME_DELETED] = "Del",
563 [AFS_VL_UNCERTAIN] = "Unc",
567 * display a header line followed by a load of volume lines
569 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
571 struct afs_vlocation *vlocation =
572 list_entry(v, struct afs_vlocation, link);
574 /* display header on line 1 */
575 if (v == (void *) 1) {
576 seq_puts(m, "USE STT VLID[0] VLID[1] VLID[2] NAME\n");
580 /* display one cell per line on subsequent lines */
581 seq_printf(m, "%3d %s %08x %08x %08x %s\n",
582 atomic_read(&vlocation->usage),
583 afs_vlocation_states[vlocation->state],
584 vlocation->vldb.vid[0],
585 vlocation->vldb.vid[1],
586 vlocation->vldb.vid[2],
587 vlocation->vldb.name);
593 * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
596 static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
598 struct afs_cell *cell;
602 cell = PDE(inode)->data;
606 ret = seq_open(file, &afs_proc_cell_vlservers_ops);
610 m = file->private_data;
617 * close the file and release the ref to the cell
619 static int afs_proc_cell_vlservers_release(struct inode *inode,
622 return seq_release(inode, file);
626 * set up the iterator to start reading from the cells list and return the
629 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
631 struct afs_cell *cell = m->private;
634 _enter("cell=%p pos=%Ld", cell, *_pos);
636 /* lock the list against modification */
637 down_read(&cell->vl_sem);
639 /* allow for the header line */
644 if (pos >= cell->vl_naddrs)
647 return &cell->vl_addrs[pos];
651 * move to next cell in cells list
653 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
656 struct afs_cell *cell = p->private;
659 _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
663 if (pos >= cell->vl_naddrs)
666 return &cell->vl_addrs[pos];
670 * clean up after reading from the cells list
672 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
674 struct afs_cell *cell = p->private;
676 up_read(&cell->vl_sem);
680 * display a header line followed by a load of volume lines
682 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
684 struct in_addr *addr = v;
686 /* display header on line 1 */
687 if (v == (struct in_addr *) 1) {
688 seq_puts(m, "ADDRESS\n");
692 /* display one cell per line on subsequent lines */
693 seq_printf(m, "%u.%u.%u.%u\n", NIPQUAD(addr->s_addr));
698 * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
701 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
703 struct afs_cell *cell;
707 cell = PDE(inode)->data;
711 ret = seq_open(file, &afs_proc_cell_servers_ops);
715 m = file->private_data;
721 * close the file and release the ref to the cell
723 static int afs_proc_cell_servers_release(struct inode *inode,
726 return seq_release(inode, file);
730 * set up the iterator to start reading from the cells list and return the
733 static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
734 __acquires(m->private->servers_lock)
736 struct list_head *_p;
737 struct afs_cell *cell = m->private;
740 _enter("cell=%p pos=%Ld", cell, *_pos);
742 /* lock the list against modification */
743 read_lock(&cell->servers_lock);
745 /* allow for the header line */
750 /* find the n'th element in the list */
751 list_for_each(_p, &cell->servers)
755 return _p != &cell->servers ? _p : NULL;
759 * move to next cell in cells list
761 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
764 struct list_head *_p;
765 struct afs_cell *cell = p->private;
767 _enter("cell=%p pos=%Ld", cell, *_pos);
772 _p = v == (void *) 1 ? cell->servers.next : _p->next;
774 return _p != &cell->servers ? _p : NULL;
778 * clean up after reading from the cells list
780 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
781 __releases(p->private->servers_lock)
783 struct afs_cell *cell = p->private;
785 read_unlock(&cell->servers_lock);
789 * display a header line followed by a load of volume lines
791 static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
793 struct afs_server *server = list_entry(v, struct afs_server, link);
796 /* display header on line 1 */
797 if (v == (void *) 1) {
798 seq_puts(m, "USE ADDR STATE\n");
802 /* display one cell per line on subsequent lines */
803 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(server->addr));
804 seq_printf(m, "%3d %-15.15s %5d\n",
805 atomic_read(&server->usage), ipaddr, server->fs_state);