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 <asm/uaccess.h>
20 static struct proc_dir_entry *proc_afs;
23 static int afs_proc_cells_open(struct inode *inode, struct file *file);
24 static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
25 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
26 static void afs_proc_cells_stop(struct seq_file *p, void *v);
27 static int afs_proc_cells_show(struct seq_file *m, void *v);
28 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
29 size_t size, loff_t *_pos);
31 static struct seq_operations afs_proc_cells_ops = {
32 .start = afs_proc_cells_start,
33 .next = afs_proc_cells_next,
34 .stop = afs_proc_cells_stop,
35 .show = afs_proc_cells_show,
38 static const struct file_operations afs_proc_cells_fops = {
39 .open = afs_proc_cells_open,
41 .write = afs_proc_cells_write,
43 .release = seq_release,
46 static int afs_proc_rootcell_open(struct inode *inode, struct file *file);
47 static int afs_proc_rootcell_release(struct inode *inode, struct file *file);
48 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
49 size_t size, loff_t *_pos);
50 static ssize_t afs_proc_rootcell_write(struct file *file,
51 const char __user *buf,
52 size_t size, loff_t *_pos);
54 static const struct file_operations afs_proc_rootcell_fops = {
55 .open = afs_proc_rootcell_open,
56 .read = afs_proc_rootcell_read,
57 .write = afs_proc_rootcell_write,
59 .release = afs_proc_rootcell_release
62 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file);
63 static int afs_proc_cell_volumes_release(struct inode *inode,
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 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 const struct file_operations afs_proc_cell_volumes_fops = {
79 .open = afs_proc_cell_volumes_open,
82 .release = afs_proc_cell_volumes_release,
85 static int afs_proc_cell_vlservers_open(struct inode *inode,
87 static int afs_proc_cell_vlservers_release(struct inode *inode,
89 static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
90 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
92 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
93 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
95 static struct seq_operations afs_proc_cell_vlservers_ops = {
96 .start = afs_proc_cell_vlservers_start,
97 .next = afs_proc_cell_vlservers_next,
98 .stop = afs_proc_cell_vlservers_stop,
99 .show = afs_proc_cell_vlservers_show,
102 static const struct file_operations afs_proc_cell_vlservers_fops = {
103 .open = afs_proc_cell_vlservers_open,
106 .release = afs_proc_cell_vlservers_release,
109 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file);
110 static int afs_proc_cell_servers_release(struct inode *inode,
112 static void *afs_proc_cell_servers_start(struct seq_file *p, loff_t *pos);
113 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
115 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v);
116 static int afs_proc_cell_servers_show(struct seq_file *m, void *v);
118 static struct seq_operations afs_proc_cell_servers_ops = {
119 .start = afs_proc_cell_servers_start,
120 .next = afs_proc_cell_servers_next,
121 .stop = afs_proc_cell_servers_stop,
122 .show = afs_proc_cell_servers_show,
125 static const struct file_operations afs_proc_cell_servers_fops = {
126 .open = afs_proc_cell_servers_open,
129 .release = afs_proc_cell_servers_release,
133 * initialise the /proc/fs/afs/ directory
135 int afs_proc_init(void)
137 struct proc_dir_entry *p;
141 proc_afs = proc_mkdir("fs/afs", NULL);
144 proc_afs->owner = THIS_MODULE;
146 p = create_proc_entry("cells", 0, proc_afs);
149 p->proc_fops = &afs_proc_cells_fops;
150 p->owner = THIS_MODULE;
152 p = create_proc_entry("rootcell", 0, proc_afs);
155 p->proc_fops = &afs_proc_rootcell_fops;
156 p->owner = THIS_MODULE;
162 remove_proc_entry("cells", proc_afs);
164 remove_proc_entry("fs/afs", NULL);
166 _leave(" = -ENOMEM");
171 * clean up the /proc/fs/afs/ directory
173 void afs_proc_cleanup(void)
175 remove_proc_entry("rootcell", proc_afs);
176 remove_proc_entry("cells", proc_afs);
177 remove_proc_entry("fs/afs", NULL);
181 * open "/proc/fs/afs/cells" which provides a summary of extant cells
183 static int afs_proc_cells_open(struct inode *inode, struct file *file)
188 ret = seq_open(file, &afs_proc_cells_ops);
192 m = file->private_data;
193 m->private = PDE(inode)->data;
199 * set up the iterator to start reading from the cells list and return the
202 static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
204 struct list_head *_p;
207 /* lock the list against modification */
208 down_read(&afs_proc_cells_sem);
210 /* allow for the header line */
215 /* find the n'th element in the list */
216 list_for_each(_p, &afs_proc_cells)
220 return _p != &afs_proc_cells ? _p : NULL;
224 * move to next cell in cells list
226 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos)
228 struct list_head *_p;
233 _p = v == (void *) 1 ? afs_proc_cells.next : _p->next;
235 return _p != &afs_proc_cells ? _p : NULL;
239 * clean up after reading from the cells list
241 static void afs_proc_cells_stop(struct seq_file *p, void *v)
243 up_read(&afs_proc_cells_sem);
247 * display a header line followed by a load of cell lines
249 static int afs_proc_cells_show(struct seq_file *m, void *v)
251 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
253 if (v == (void *) 1) {
254 /* display header on line 1 */
255 seq_puts(m, "USE NAME\n");
259 /* display one cell per line on subsequent lines */
260 seq_printf(m, "%3d %s\n",
261 atomic_read(&cell->usage), cell->name);
266 * handle writes to /proc/fs/afs/cells
267 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
269 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
270 size_t size, loff_t *_pos)
272 char *kbuf, *name, *args;
275 /* start by dragging the command into memory */
276 if (size <= 1 || size >= PAGE_SIZE)
279 kbuf = kmalloc(size + 1, GFP_KERNEL);
284 if (copy_from_user(kbuf, buf, size) != 0)
288 /* trim to first NL */
289 name = memchr(kbuf, '\n', size);
293 /* split into command, name and argslist */
294 name = strchr(kbuf, ' ');
299 } while(*name == ' ');
303 args = strchr(name, ' ');
308 } while(*args == ' ');
312 /* determine command to perform */
313 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
315 if (strcmp(kbuf, "add") == 0) {
316 struct afs_cell *cell;
318 cell = afs_cell_create(name, args);
325 printk("kAFS: Added new cell '%s'\n", name);
334 _leave(" = %d", ret);
339 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
344 * Stubs for /proc/fs/afs/rootcell
346 static int afs_proc_rootcell_open(struct inode *inode, struct file *file)
351 static int afs_proc_rootcell_release(struct inode *inode, struct file *file)
356 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
357 size_t size, loff_t *_pos)
363 * handle writes to /proc/fs/afs/rootcell
364 * - to initialize rootcell: echo "cell.name:192.168.231.14"
366 static ssize_t afs_proc_rootcell_write(struct file *file,
367 const char __user *buf,
368 size_t size, loff_t *_pos)
373 /* start by dragging the command into memory */
374 if (size <= 1 || size >= PAGE_SIZE)
378 kbuf = kmalloc(size + 1, GFP_KERNEL);
383 if (copy_from_user(kbuf, buf, size) != 0)
387 /* trim to first NL */
388 s = memchr(kbuf, '\n', size);
392 /* determine command to perform */
393 _debug("rootcell=%s", kbuf);
395 ret = afs_cell_init(kbuf);
397 ret = size; /* consume everything, always */
402 _leave(" = %d", ret);
407 * initialise /proc/fs/afs/<cell>/
409 int afs_proc_cell_setup(struct afs_cell *cell)
411 struct proc_dir_entry *p;
413 _enter("%p{%s}", cell, cell->name);
415 cell->proc_dir = proc_mkdir(cell->name, proc_afs);
419 p = create_proc_entry("servers", 0, cell->proc_dir);
422 p->proc_fops = &afs_proc_cell_servers_fops;
423 p->owner = THIS_MODULE;
426 p = create_proc_entry("vlservers", 0, cell->proc_dir);
428 goto error_vlservers;
429 p->proc_fops = &afs_proc_cell_vlservers_fops;
430 p->owner = THIS_MODULE;
433 p = create_proc_entry("volumes", 0, cell->proc_dir);
436 p->proc_fops = &afs_proc_cell_volumes_fops;
437 p->owner = THIS_MODULE;
444 remove_proc_entry("vlservers", cell->proc_dir);
446 remove_proc_entry("servers", cell->proc_dir);
448 remove_proc_entry(cell->name, proc_afs);
450 _leave(" = -ENOMEM");
455 * remove /proc/fs/afs/<cell>/
457 void afs_proc_cell_remove(struct afs_cell *cell)
461 remove_proc_entry("volumes", cell->proc_dir);
462 remove_proc_entry("vlservers", cell->proc_dir);
463 remove_proc_entry("servers", cell->proc_dir);
464 remove_proc_entry(cell->name, proc_afs);
470 * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
472 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
474 struct afs_cell *cell;
478 cell = PDE(inode)->data;
482 ret = seq_open(file, &afs_proc_cell_volumes_ops);
486 m = file->private_data;
493 * close the file and release the ref to the cell
495 static int afs_proc_cell_volumes_release(struct inode *inode, struct file *file)
497 return seq_release(inode, file);
501 * set up the iterator to start reading from the cells list and return the
504 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
506 struct list_head *_p;
507 struct afs_cell *cell = m->private;
510 _enter("cell=%p pos=%Ld", cell, *_pos);
512 /* lock the list against modification */
513 down_read(&cell->vl_sem);
515 /* allow for the header line */
520 /* find the n'th element in the list */
521 list_for_each(_p, &cell->vl_list)
525 return _p != &cell->vl_list ? _p : NULL;
529 * move to next cell in cells list
531 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
534 struct list_head *_p;
535 struct afs_cell *cell = p->private;
537 _enter("cell=%p pos=%Ld", cell, *_pos);
542 _p = (v == (void *) 1) ? cell->vl_list.next : _p->next;
544 return (_p != &cell->vl_list) ? _p : NULL;
548 * clean up after reading from the cells list
550 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
552 struct afs_cell *cell = p->private;
554 up_read(&cell->vl_sem);
557 const char afs_vlocation_states[][4] = {
558 [AFS_VL_NEW] = "New",
559 [AFS_VL_CREATING] = "Crt",
560 [AFS_VL_VALID] = "Val",
561 [AFS_VL_NO_VOLUME] = "NoV",
562 [AFS_VL_UPDATING] = "Upd",
563 [AFS_VL_VOLUME_DELETED] = "Del",
564 [AFS_VL_UNCERTAIN] = "Unc",
568 * display a header line followed by a load of volume lines
570 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
572 struct afs_vlocation *vlocation =
573 list_entry(v, struct afs_vlocation, link);
575 /* display header on line 1 */
576 if (v == (void *) 1) {
577 seq_puts(m, "USE STT VLID[0] VLID[1] VLID[2] NAME\n");
581 /* display one cell per line on subsequent lines */
582 seq_printf(m, "%3d %s %08x %08x %08x %s\n",
583 atomic_read(&vlocation->usage),
584 afs_vlocation_states[vlocation->state],
585 vlocation->vldb.vid[0],
586 vlocation->vldb.vid[1],
587 vlocation->vldb.vid[2],
588 vlocation->vldb.name);
594 * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
597 static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
599 struct afs_cell *cell;
603 cell = PDE(inode)->data;
607 ret = seq_open(file, &afs_proc_cell_vlservers_ops);
611 m = file->private_data;
618 * close the file and release the ref to the cell
620 static int afs_proc_cell_vlservers_release(struct inode *inode,
623 return seq_release(inode, file);
627 * set up the iterator to start reading from the cells list and return the
630 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
632 struct afs_cell *cell = m->private;
635 _enter("cell=%p pos=%Ld", cell, *_pos);
637 /* lock the list against modification */
638 down_read(&cell->vl_sem);
640 /* allow for the header line */
645 if (pos >= cell->vl_naddrs)
648 return &cell->vl_addrs[pos];
652 * move to next cell in cells list
654 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
657 struct afs_cell *cell = p->private;
660 _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
664 if (pos >= cell->vl_naddrs)
667 return &cell->vl_addrs[pos];
671 * clean up after reading from the cells list
673 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
675 struct afs_cell *cell = p->private;
677 up_read(&cell->vl_sem);
681 * display a header line followed by a load of volume lines
683 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
685 struct in_addr *addr = v;
687 /* display header on line 1 */
688 if (v == (struct in_addr *) 1) {
689 seq_puts(m, "ADDRESS\n");
693 /* display one cell per line on subsequent lines */
694 seq_printf(m, "%u.%u.%u.%u\n", NIPQUAD(addr->s_addr));
699 * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
702 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
704 struct afs_cell *cell;
708 cell = PDE(inode)->data;
712 ret = seq_open(file, &afs_proc_cell_servers_ops);
716 m = file->private_data;
722 * close the file and release the ref to the cell
724 static int afs_proc_cell_servers_release(struct inode *inode,
727 return seq_release(inode, file);
731 * set up the iterator to start reading from the cells list and return the
734 static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
735 __acquires(m->private->servers_lock)
737 struct list_head *_p;
738 struct afs_cell *cell = m->private;
741 _enter("cell=%p pos=%Ld", cell, *_pos);
743 /* lock the list against modification */
744 read_lock(&cell->servers_lock);
746 /* allow for the header line */
751 /* find the n'th element in the list */
752 list_for_each(_p, &cell->servers)
756 return _p != &cell->servers ? _p : NULL;
760 * move to next cell in cells list
762 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
765 struct list_head *_p;
766 struct afs_cell *cell = p->private;
768 _enter("cell=%p pos=%Ld", cell, *_pos);
773 _p = v == (void *) 1 ? cell->servers.next : _p->next;
775 return _p != &cell->servers ? _p : NULL;
779 * clean up after reading from the cells list
781 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
782 __releases(p->private->servers_lock)
784 struct afs_cell *cell = p->private;
786 read_unlock(&cell->servers_lock);
790 * display a header line followed by a load of volume lines
792 static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
794 struct afs_server *server = list_entry(v, struct afs_server, link);
797 /* display header on line 1 */
798 if (v == (void *) 1) {
799 seq_puts(m, "USE ADDR STATE\n");
803 /* display one cell per line on subsequent lines */
804 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(server->addr));
805 seq_printf(m, "%3d %-15.15s %5d\n",
806 atomic_read(&server->usage), ipaddr, server->fs_state);