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 /* lock the list against modification */
205 down_read(&afs_proc_cells_sem);
206 return seq_list_start_head(&afs_proc_cells, *_pos);
210 * move to next cell in cells list
212 static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos)
214 return seq_list_next(v, &afs_proc_cells, pos);
218 * clean up after reading from the cells list
220 static void afs_proc_cells_stop(struct seq_file *p, void *v)
222 up_read(&afs_proc_cells_sem);
226 * display a header line followed by a load of cell lines
228 static int afs_proc_cells_show(struct seq_file *m, void *v)
230 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
232 if (v == &afs_proc_cells) {
233 /* display header on line 1 */
234 seq_puts(m, "USE NAME\n");
238 /* display one cell per line on subsequent lines */
239 seq_printf(m, "%3d %s\n",
240 atomic_read(&cell->usage), cell->name);
245 * handle writes to /proc/fs/afs/cells
246 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
248 static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
249 size_t size, loff_t *_pos)
251 char *kbuf, *name, *args;
254 /* start by dragging the command into memory */
255 if (size <= 1 || size >= PAGE_SIZE)
258 kbuf = kmalloc(size + 1, GFP_KERNEL);
263 if (copy_from_user(kbuf, buf, size) != 0)
267 /* trim to first NL */
268 name = memchr(kbuf, '\n', size);
272 /* split into command, name and argslist */
273 name = strchr(kbuf, ' ');
278 } while(*name == ' ');
282 args = strchr(name, ' ');
287 } while(*args == ' ');
291 /* determine command to perform */
292 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
294 if (strcmp(kbuf, "add") == 0) {
295 struct afs_cell *cell;
297 cell = afs_cell_create(name, args);
304 printk("kAFS: Added new cell '%s'\n", name);
313 _leave(" = %d", ret);
318 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
323 * Stubs for /proc/fs/afs/rootcell
325 static int afs_proc_rootcell_open(struct inode *inode, struct file *file)
330 static int afs_proc_rootcell_release(struct inode *inode, struct file *file)
335 static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
336 size_t size, loff_t *_pos)
342 * handle writes to /proc/fs/afs/rootcell
343 * - to initialize rootcell: echo "cell.name:192.168.231.14"
345 static ssize_t afs_proc_rootcell_write(struct file *file,
346 const char __user *buf,
347 size_t size, loff_t *_pos)
352 /* start by dragging the command into memory */
353 if (size <= 1 || size >= PAGE_SIZE)
357 kbuf = kmalloc(size + 1, GFP_KERNEL);
362 if (copy_from_user(kbuf, buf, size) != 0)
366 /* trim to first NL */
367 s = memchr(kbuf, '\n', size);
371 /* determine command to perform */
372 _debug("rootcell=%s", kbuf);
374 ret = afs_cell_init(kbuf);
376 ret = size; /* consume everything, always */
381 _leave(" = %d", ret);
386 * initialise /proc/fs/afs/<cell>/
388 int afs_proc_cell_setup(struct afs_cell *cell)
390 struct proc_dir_entry *p;
392 _enter("%p{%s}", cell, cell->name);
394 cell->proc_dir = proc_mkdir(cell->name, proc_afs);
398 p = create_proc_entry("servers", 0, cell->proc_dir);
401 p->proc_fops = &afs_proc_cell_servers_fops;
402 p->owner = THIS_MODULE;
405 p = create_proc_entry("vlservers", 0, cell->proc_dir);
407 goto error_vlservers;
408 p->proc_fops = &afs_proc_cell_vlservers_fops;
409 p->owner = THIS_MODULE;
412 p = create_proc_entry("volumes", 0, cell->proc_dir);
415 p->proc_fops = &afs_proc_cell_volumes_fops;
416 p->owner = THIS_MODULE;
423 remove_proc_entry("vlservers", cell->proc_dir);
425 remove_proc_entry("servers", cell->proc_dir);
427 remove_proc_entry(cell->name, proc_afs);
429 _leave(" = -ENOMEM");
434 * remove /proc/fs/afs/<cell>/
436 void afs_proc_cell_remove(struct afs_cell *cell)
440 remove_proc_entry("volumes", cell->proc_dir);
441 remove_proc_entry("vlservers", cell->proc_dir);
442 remove_proc_entry("servers", cell->proc_dir);
443 remove_proc_entry(cell->name, proc_afs);
449 * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
451 static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
453 struct afs_cell *cell;
457 cell = PDE(inode)->data;
461 ret = seq_open(file, &afs_proc_cell_volumes_ops);
465 m = file->private_data;
472 * close the file and release the ref to the cell
474 static int afs_proc_cell_volumes_release(struct inode *inode, struct file *file)
476 return seq_release(inode, file);
480 * set up the iterator to start reading from the cells list and return the
483 static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
485 struct afs_cell *cell = m->private;
487 _enter("cell=%p pos=%Ld", cell, *_pos);
489 /* lock the list against modification */
490 down_read(&cell->vl_sem);
491 return seq_list_start_head(&cell->vl_list, *_pos);
495 * move to next cell in cells list
497 static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
500 struct afs_cell *cell = p->private;
502 _enter("cell=%p pos=%Ld", cell, *_pos);
503 return seq_list_next(v, &cell->vl_list, _pos);
507 * clean up after reading from the cells list
509 static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
511 struct afs_cell *cell = p->private;
513 up_read(&cell->vl_sem);
516 static const char afs_vlocation_states[][4] = {
517 [AFS_VL_NEW] = "New",
518 [AFS_VL_CREATING] = "Crt",
519 [AFS_VL_VALID] = "Val",
520 [AFS_VL_NO_VOLUME] = "NoV",
521 [AFS_VL_UPDATING] = "Upd",
522 [AFS_VL_VOLUME_DELETED] = "Del",
523 [AFS_VL_UNCERTAIN] = "Unc",
527 * display a header line followed by a load of volume lines
529 static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
531 struct afs_cell *cell = m->private;
532 struct afs_vlocation *vlocation =
533 list_entry(v, struct afs_vlocation, link);
535 /* display header on line 1 */
536 if (v == &cell->vl_list) {
537 seq_puts(m, "USE STT VLID[0] VLID[1] VLID[2] NAME\n");
541 /* display one cell per line on subsequent lines */
542 seq_printf(m, "%3d %s %08x %08x %08x %s\n",
543 atomic_read(&vlocation->usage),
544 afs_vlocation_states[vlocation->state],
545 vlocation->vldb.vid[0],
546 vlocation->vldb.vid[1],
547 vlocation->vldb.vid[2],
548 vlocation->vldb.name);
554 * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
557 static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
559 struct afs_cell *cell;
563 cell = PDE(inode)->data;
567 ret = seq_open(file, &afs_proc_cell_vlservers_ops);
571 m = file->private_data;
578 * close the file and release the ref to the cell
580 static int afs_proc_cell_vlservers_release(struct inode *inode,
583 return seq_release(inode, file);
587 * set up the iterator to start reading from the cells list and return the
590 static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
592 struct afs_cell *cell = m->private;
595 _enter("cell=%p pos=%Ld", cell, *_pos);
597 /* lock the list against modification */
598 down_read(&cell->vl_sem);
600 /* allow for the header line */
605 if (pos >= cell->vl_naddrs)
608 return &cell->vl_addrs[pos];
612 * move to next cell in cells list
614 static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
617 struct afs_cell *cell = p->private;
620 _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
624 if (pos >= cell->vl_naddrs)
627 return &cell->vl_addrs[pos];
631 * clean up after reading from the cells list
633 static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
635 struct afs_cell *cell = p->private;
637 up_read(&cell->vl_sem);
641 * display a header line followed by a load of volume lines
643 static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
645 struct in_addr *addr = v;
647 /* display header on line 1 */
648 if (v == (struct in_addr *) 1) {
649 seq_puts(m, "ADDRESS\n");
653 /* display one cell per line on subsequent lines */
654 seq_printf(m, "%u.%u.%u.%u\n", NIPQUAD(addr->s_addr));
659 * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
662 static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
664 struct afs_cell *cell;
668 cell = PDE(inode)->data;
672 ret = seq_open(file, &afs_proc_cell_servers_ops);
676 m = file->private_data;
682 * close the file and release the ref to the cell
684 static int afs_proc_cell_servers_release(struct inode *inode,
687 return seq_release(inode, file);
691 * set up the iterator to start reading from the cells list and return the
694 static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
695 __acquires(m->private->servers_lock)
697 struct afs_cell *cell = m->private;
699 _enter("cell=%p pos=%Ld", cell, *_pos);
701 /* lock the list against modification */
702 read_lock(&cell->servers_lock);
703 return seq_list_start_head(&cell->servers, *_pos);
707 * move to next cell in cells list
709 static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
712 struct afs_cell *cell = p->private;
714 _enter("cell=%p pos=%Ld", cell, *_pos);
715 return seq_list_next(v, &cell->servers, _pos);
719 * clean up after reading from the cells list
721 static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
722 __releases(p->private->servers_lock)
724 struct afs_cell *cell = p->private;
726 read_unlock(&cell->servers_lock);
730 * display a header line followed by a load of volume lines
732 static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
734 struct afs_cell *cell = m->private;
735 struct afs_server *server = list_entry(v, struct afs_server, link);
738 /* display header on line 1 */
739 if (v == &cell->servers) {
740 seq_puts(m, "USE ADDR STATE\n");
744 /* display one cell per line on subsequent lines */
745 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(server->addr));
746 seq_printf(m, "%3d %-15.15s %5d\n",
747 atomic_read(&server->usage), ipaddr, server->fs_state);