blob: 15650cd5940426bdab161fe32c20c000650b9d20 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* /proc interface for AFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
13#include <linux/module.h>
14#include <linux/proc_fs.h>
15#include <linux/seq_file.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "internal.h"
19
David Howellsf044c882017-11-02 15:27:45 +000020static inline struct afs_net *afs_proc2net(struct file *f)
21{
22 return &__afs_net;
23}
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howellsf044c882017-11-02 15:27:45 +000025static inline struct afs_net *afs_seq2net(struct seq_file *m)
26{
27 return &__afs_net; // TODO: use seq_file_net(m)
28}
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static int afs_proc_cells_open(struct inode *inode, struct file *file);
31static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
32static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
33static void afs_proc_cells_stop(struct seq_file *p, void *v);
34static int afs_proc_cells_show(struct seq_file *m, void *v);
35static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
36 size_t size, loff_t *_pos);
37
James Morris88e9d342009-09-22 16:43:43 -070038static const struct seq_operations afs_proc_cells_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 .start = afs_proc_cells_start,
40 .next = afs_proc_cells_next,
41 .stop = afs_proc_cells_stop,
42 .show = afs_proc_cells_show,
43};
44
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080045static const struct file_operations afs_proc_cells_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .open = afs_proc_cells_open,
47 .read = seq_read,
48 .write = afs_proc_cells_write,
49 .llseek = seq_lseek,
50 .release = seq_release,
51};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
54 size_t size, loff_t *_pos);
55static ssize_t afs_proc_rootcell_write(struct file *file,
56 const char __user *buf,
57 size_t size, loff_t *_pos);
58
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080059static const struct file_operations afs_proc_rootcell_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .read = afs_proc_rootcell_read,
61 .write = afs_proc_rootcell_write,
62 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void *afs_proc_cell_volumes_start(struct seq_file *p, loff_t *pos);
67static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
68 loff_t *pos);
69static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v);
70static int afs_proc_cell_volumes_show(struct seq_file *m, void *v);
71
James Morris88e9d342009-09-22 16:43:43 -070072static const struct seq_operations afs_proc_cell_volumes_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .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,
77};
78
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080079static const struct file_operations afs_proc_cell_volumes_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .open = afs_proc_cell_volumes_open,
81 .read = seq_read,
82 .llseek = seq_lseek,
Al Virob42d5702013-11-22 01:53:47 -050083 .release = seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86static int afs_proc_cell_vlservers_open(struct inode *inode,
87 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
89static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
90 loff_t *pos);
91static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
92static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
93
James Morris88e9d342009-09-22 16:43:43 -070094static const struct seq_operations afs_proc_cell_vlservers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .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,
99};
100
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800101static const struct file_operations afs_proc_cell_vlservers_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .open = afs_proc_cell_vlservers_open,
103 .read = seq_read,
104 .llseek = seq_lseek,
Al Virob42d5702013-11-22 01:53:47 -0500105 .release = seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
David Howellsd2ddc772017-11-02 15:27:50 +0000108static int afs_proc_servers_open(struct inode *inode, struct file *file);
109static void *afs_proc_servers_start(struct seq_file *p, loff_t *pos);
110static void *afs_proc_servers_next(struct seq_file *p, void *v,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 loff_t *pos);
David Howellsd2ddc772017-11-02 15:27:50 +0000112static void afs_proc_servers_stop(struct seq_file *p, void *v);
113static int afs_proc_servers_show(struct seq_file *m, void *v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David Howellsd2ddc772017-11-02 15:27:50 +0000115static const struct seq_operations afs_proc_servers_ops = {
116 .start = afs_proc_servers_start,
117 .next = afs_proc_servers_next,
118 .stop = afs_proc_servers_stop,
119 .show = afs_proc_servers_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
David Howellsd2ddc772017-11-02 15:27:50 +0000122static const struct file_operations afs_proc_servers_fops = {
123 .open = afs_proc_servers_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .read = seq_read,
125 .llseek = seq_lseek,
Al Virob42d5702013-11-22 01:53:47 -0500126 .release = seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
David Howells6f8880d2018-04-09 21:12:31 +0100129static int afs_proc_sysname_open(struct inode *inode, struct file *file);
130static int afs_proc_sysname_release(struct inode *inode, struct file *file);
131static void *afs_proc_sysname_start(struct seq_file *p, loff_t *pos);
132static void *afs_proc_sysname_next(struct seq_file *p, void *v,
133 loff_t *pos);
134static void afs_proc_sysname_stop(struct seq_file *p, void *v);
135static int afs_proc_sysname_show(struct seq_file *m, void *v);
136static ssize_t afs_proc_sysname_write(struct file *file,
137 const char __user *buf,
138 size_t size, loff_t *_pos);
139
140static const struct seq_operations afs_proc_sysname_ops = {
141 .start = afs_proc_sysname_start,
142 .next = afs_proc_sysname_next,
143 .stop = afs_proc_sysname_stop,
144 .show = afs_proc_sysname_show,
145};
146
147static const struct file_operations afs_proc_sysname_fops = {
148 .open = afs_proc_sysname_open,
149 .read = seq_read,
150 .llseek = seq_lseek,
151 .release = afs_proc_sysname_release,
152 .write = afs_proc_sysname_write,
153};
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * initialise the /proc/fs/afs/ directory
157 */
David Howellsf044c882017-11-02 15:27:45 +0000158int afs_proc_init(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 _enter("");
161
David Howellsf044c882017-11-02 15:27:45 +0000162 net->proc_afs = proc_mkdir("fs/afs", NULL);
163 if (!net->proc_afs)
David Howellsec268152007-04-26 15:49:28 -0700164 goto error_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
David Howellsf044c882017-11-02 15:27:45 +0000166 if (!proc_create("cells", 0644, net->proc_afs, &afs_proc_cells_fops) ||
David Howellsd2ddc772017-11-02 15:27:50 +0000167 !proc_create("rootcell", 0644, net->proc_afs, &afs_proc_rootcell_fops) ||
David Howells6f8880d2018-04-09 21:12:31 +0100168 !proc_create("servers", 0644, net->proc_afs, &afs_proc_servers_fops) ||
169 !proc_create("sysname", 0644, net->proc_afs, &afs_proc_sysname_fops))
Al Virob42d5702013-11-22 01:53:47 -0500170 goto error_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 _leave(" = 0");
173 return 0;
174
Al Virob42d5702013-11-22 01:53:47 -0500175error_tree:
David Howellsf044c882017-11-02 15:27:45 +0000176 proc_remove(net->proc_afs);
David Howellsec268152007-04-26 15:49:28 -0700177error_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 _leave(" = -ENOMEM");
179 return -ENOMEM;
David Howellsec268152007-04-26 15:49:28 -0700180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * clean up the /proc/fs/afs/ directory
184 */
David Howellsf044c882017-11-02 15:27:45 +0000185void afs_proc_cleanup(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
David Howellsf044c882017-11-02 15:27:45 +0000187 proc_remove(net->proc_afs);
188 net->proc_afs = NULL;
David Howellsec268152007-04-26 15:49:28 -0700189}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * open "/proc/fs/afs/cells" which provides a summary of extant cells
193 */
194static int afs_proc_cells_open(struct inode *inode, struct file *file)
195{
196 struct seq_file *m;
197 int ret;
198
199 ret = seq_open(file, &afs_proc_cells_ops);
200 if (ret < 0)
201 return ret;
202
203 m = file->private_data;
Al Virod9dda782013-03-31 18:16:14 -0400204 m->private = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
David Howellsec268152007-04-26 15:49:28 -0700206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
209 * set up the iterator to start reading from the cells list and return the
210 * first item
211 */
212static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100213 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
David Howellsf044c882017-11-02 15:27:45 +0000215 struct afs_net *net = afs_seq2net(m);
216
David Howells989782d2017-11-02 15:27:50 +0000217 rcu_read_lock();
David Howellsf044c882017-11-02 15:27:45 +0000218 return seq_list_start_head(&net->proc_cells, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * move to next cell in cells list
223 */
David Howellsf044c882017-11-02 15:27:45 +0000224static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
David Howellsf044c882017-11-02 15:27:45 +0000226 struct afs_net *net = afs_seq2net(m);
227
228 return seq_list_next(v, &net->proc_cells, pos);
David Howellsec268152007-04-26 15:49:28 -0700229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * clean up after reading from the cells list
233 */
David Howellsf044c882017-11-02 15:27:45 +0000234static void afs_proc_cells_stop(struct seq_file *m, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100235 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
David Howells989782d2017-11-02 15:27:50 +0000237 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * display a header line followed by a load of cell lines
242 */
243static int afs_proc_cells_show(struct seq_file *m, void *v)
244{
245 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
David Howellsf044c882017-11-02 15:27:45 +0000246 struct afs_net *net = afs_seq2net(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David Howellsf044c882017-11-02 15:27:45 +0000248 if (v == &net->proc_cells) {
David Howellsec268152007-04-26 15:49:28 -0700249 /* display header on line 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 seq_puts(m, "USE NAME\n");
251 return 0;
252 }
253
254 /* display one cell per line on subsequent lines */
David Howells989782d2017-11-02 15:27:50 +0000255 seq_printf(m, "%3u %s\n", atomic_read(&cell->usage), cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
David Howellsec268152007-04-26 15:49:28 -0700257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * handle writes to /proc/fs/afs/cells
261 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
262 */
263static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
264 size_t size, loff_t *_pos)
265{
David Howellsf044c882017-11-02 15:27:45 +0000266 struct afs_net *net = afs_proc2net(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 char *kbuf, *name, *args;
268 int ret;
269
270 /* start by dragging the command into memory */
271 if (size <= 1 || size >= PAGE_SIZE)
272 return -EINVAL;
273
Al Viro16e5c1f2015-12-24 00:06:05 -0500274 kbuf = memdup_user_nul(buf, size);
275 if (IS_ERR(kbuf))
276 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* trim to first NL */
279 name = memchr(kbuf, '\n', size);
280 if (name)
281 *name = 0;
282
283 /* split into command, name and argslist */
284 name = strchr(kbuf, ' ');
285 if (!name)
286 goto inval;
287 do {
288 *name++ = 0;
289 } while(*name == ' ');
290 if (!*name)
291 goto inval;
292
293 args = strchr(name, ' ');
294 if (!args)
295 goto inval;
296 do {
297 *args++ = 0;
298 } while(*args == ' ');
299 if (!*args)
300 goto inval;
301
302 /* determine command to perform */
303 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
304
305 if (strcmp(kbuf, "add") == 0) {
306 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David Howells989782d2017-11-02 15:27:50 +0000308 cell = afs_lookup_cell(net, name, strlen(name), args, true);
David Howells08e0e7c2007-04-26 15:55:03 -0700309 if (IS_ERR(cell)) {
310 ret = PTR_ERR(cell);
311 goto done;
312 }
313
David Howells17814ae2018-04-09 21:12:31 +0100314 if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
315 afs_put_cell(net, cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 printk("kAFS: Added new cell '%s'\n", name);
David Howellsec268152007-04-26 15:49:28 -0700317 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto inval;
319 }
320
321 ret = size;
322
David Howellsec268152007-04-26 15:49:28 -0700323done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 kfree(kbuf);
325 _leave(" = %d", ret);
326 return ret;
327
David Howellsec268152007-04-26 15:49:28 -0700328inval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ret = -EINVAL;
330 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
331 goto done;
David Howellsec268152007-04-26 15:49:28 -0700332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
335 size_t size, loff_t *_pos)
336{
337 return 0;
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * handle writes to /proc/fs/afs/rootcell
342 * - to initialize rootcell: echo "cell.name:192.168.231.14"
343 */
344static ssize_t afs_proc_rootcell_write(struct file *file,
345 const char __user *buf,
346 size_t size, loff_t *_pos)
347{
David Howellsf044c882017-11-02 15:27:45 +0000348 struct afs_net *net = afs_proc2net(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 char *kbuf, *s;
350 int ret;
351
352 /* start by dragging the command into memory */
353 if (size <= 1 || size >= PAGE_SIZE)
354 return -EINVAL;
355
Al Viro16e5c1f2015-12-24 00:06:05 -0500356 kbuf = memdup_user_nul(buf, size);
357 if (IS_ERR(kbuf))
358 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Howells6f8880d2018-04-09 21:12:31 +0100360 ret = -EINVAL;
361 if (kbuf[0] == '.')
362 goto out;
363 if (memchr(kbuf, '/', size))
364 goto out;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* trim to first NL */
367 s = memchr(kbuf, '\n', size);
368 if (s)
369 *s = 0;
370
371 /* determine command to perform */
372 _debug("rootcell=%s", kbuf);
373
David Howellsf044c882017-11-02 15:27:45 +0000374 ret = afs_cell_init(net, kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (ret >= 0)
376 ret = size; /* consume everything, always */
377
David Howells6f8880d2018-04-09 21:12:31 +0100378out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 _leave(" = %d", ret);
381 return ret;
David Howellsec268152007-04-26 15:49:28 -0700382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
385 * initialise /proc/fs/afs/<cell>/
386 */
David Howellsf044c882017-11-02 15:27:45 +0000387int afs_proc_cell_setup(struct afs_net *net, struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Al Virob42d5702013-11-22 01:53:47 -0500389 struct proc_dir_entry *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David Howells989782d2017-11-02 15:27:50 +0000391 _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howellsf044c882017-11-02 15:27:45 +0000393 dir = proc_mkdir(cell->name, net->proc_afs);
Al Virob42d5702013-11-22 01:53:47 -0500394 if (!dir)
David Howellsec268152007-04-26 15:49:28 -0700395 goto error_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howellsd2ddc772017-11-02 15:27:50 +0000397 if (!proc_create_data("vlservers", 0, dir,
398 &afs_proc_cell_vlservers_fops, cell) ||
Al Virob42d5702013-11-22 01:53:47 -0500399 !proc_create_data("volumes", 0, dir,
David Howellsd2ddc772017-11-02 15:27:50 +0000400 &afs_proc_cell_volumes_fops, cell))
Al Virob42d5702013-11-22 01:53:47 -0500401 goto error_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 _leave(" = 0");
404 return 0;
405
Al Virob42d5702013-11-22 01:53:47 -0500406error_tree:
David Howellsf044c882017-11-02 15:27:45 +0000407 remove_proc_subtree(cell->name, net->proc_afs);
David Howellsec268152007-04-26 15:49:28 -0700408error_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 _leave(" = -ENOMEM");
410 return -ENOMEM;
David Howellsec268152007-04-26 15:49:28 -0700411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
414 * remove /proc/fs/afs/<cell>/
415 */
David Howellsf044c882017-11-02 15:27:45 +0000416void afs_proc_cell_remove(struct afs_net *net, struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 _enter("");
419
David Howellsf044c882017-11-02 15:27:45 +0000420 remove_proc_subtree(cell->name, net->proc_afs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
426 * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
427 */
428static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
429{
430 struct afs_cell *cell;
431 struct seq_file *m;
432 int ret;
433
Al Virod9dda782013-03-31 18:16:14 -0400434 cell = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!cell)
436 return -ENOENT;
437
438 ret = seq_open(file, &afs_proc_cell_volumes_ops);
439 if (ret < 0)
440 return ret;
441
442 m = file->private_data;
443 m->private = cell;
444
445 return 0;
David Howellsec268152007-04-26 15:49:28 -0700446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * set up the iterator to start reading from the cells list and return the
450 * first item
451 */
452static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100453 __acquires(cell->proc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct afs_cell *cell = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 _enter("cell=%p pos=%Ld", cell, *_pos);
458
David Howellsd2ddc772017-11-02 15:27:50 +0000459 read_lock(&cell->proc_lock);
460 return seq_list_start_head(&cell->proc_volumes, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*
464 * move to next cell in cells list
465 */
466static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
467 loff_t *_pos)
468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct afs_cell *cell = p->private;
470
471 _enter("cell=%p pos=%Ld", cell, *_pos);
David Howellsd2ddc772017-11-02 15:27:50 +0000472 return seq_list_next(v, &cell->proc_volumes, _pos);
David Howellsec268152007-04-26 15:49:28 -0700473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/*
476 * clean up after reading from the cells list
477 */
478static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100479 __releases(cell->proc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct afs_cell *cell = p->private;
482
David Howellsd2ddc772017-11-02 15:27:50 +0000483 read_unlock(&cell->proc_lock);
David Howellsec268152007-04-26 15:49:28 -0700484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David Howellsd2ddc772017-11-02 15:27:50 +0000486static const char afs_vol_types[3][3] = {
487 [AFSVL_RWVOL] = "RW",
488 [AFSVL_ROVOL] = "RO",
489 [AFSVL_BACKVOL] = "BK",
David Howells08e0e7c2007-04-26 15:55:03 -0700490};
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * display a header line followed by a load of volume lines
494 */
495static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
496{
Pavel Emelianova6a8bd62007-07-15 23:39:52 -0700497 struct afs_cell *cell = m->private;
David Howellsd2ddc772017-11-02 15:27:50 +0000498 struct afs_volume *vol = list_entry(v, struct afs_volume, proc_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David Howellsd2ddc772017-11-02 15:27:50 +0000500 /* Display header on line 1 */
501 if (v == &cell->proc_volumes) {
502 seq_puts(m, "USE VID TY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
504 }
505
David Howellsd2ddc772017-11-02 15:27:50 +0000506 seq_printf(m, "%3d %08x %s\n",
507 atomic_read(&vol->usage), vol->vid,
508 afs_vol_types[vol->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 return 0;
David Howellsec268152007-04-26 15:49:28 -0700511}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
514 * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
515 * location server
516 */
517static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
518{
519 struct afs_cell *cell;
520 struct seq_file *m;
521 int ret;
522
Al Virod9dda782013-03-31 18:16:14 -0400523 cell = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!cell)
525 return -ENOENT;
526
David Howells08e0e7c2007-04-26 15:55:03 -0700527 ret = seq_open(file, &afs_proc_cell_vlservers_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (ret<0)
529 return ret;
530
531 m = file->private_data;
532 m->private = cell;
533
534 return 0;
David Howellsec268152007-04-26 15:49:28 -0700535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * set up the iterator to start reading from the cells list and return the
539 * first item
540 */
541static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100542 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
David Howells8b2a4642017-11-02 15:27:50 +0000544 struct afs_addr_list *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct afs_cell *cell = m->private;
546 loff_t pos = *_pos;
547
David Howells8b2a4642017-11-02 15:27:50 +0000548 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
David Howells8b2a4642017-11-02 15:27:50 +0000550 alist = rcu_dereference(cell->vl_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* allow for the header line */
553 if (!pos)
554 return (void *) 1;
555 pos--;
556
David Howells8b2a4642017-11-02 15:27:50 +0000557 if (!alist || pos >= alist->nr_addrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return NULL;
559
David Howells8b2a4642017-11-02 15:27:50 +0000560 return alist->addrs + pos;
David Howellsec268152007-04-26 15:49:28 -0700561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * move to next cell in cells list
565 */
566static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
567 loff_t *_pos)
568{
David Howells8b2a4642017-11-02 15:27:50 +0000569 struct afs_addr_list *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct afs_cell *cell = p->private;
571 loff_t pos;
572
David Howells8b2a4642017-11-02 15:27:50 +0000573 alist = rcu_dereference(cell->vl_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 pos = *_pos;
576 (*_pos)++;
David Howells8b2a4642017-11-02 15:27:50 +0000577 if (!alist || pos >= alist->nr_addrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579
David Howells8b2a4642017-11-02 15:27:50 +0000580 return alist->addrs + pos;
David Howellsec268152007-04-26 15:49:28 -0700581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/*
584 * clean up after reading from the cells list
585 */
586static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100587 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
David Howells8b2a4642017-11-02 15:27:50 +0000589 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * display a header line followed by a load of volume lines
594 */
595static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
596{
David Howells4d9df982017-11-02 15:27:47 +0000597 struct sockaddr_rxrpc *addr = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* display header on line 1 */
David Howells4d9df982017-11-02 15:27:47 +0000600 if (v == (void *)1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 seq_puts(m, "ADDRESS\n");
602 return 0;
603 }
604
605 /* display one cell per line on subsequent lines */
David Howells4d9df982017-11-02 15:27:47 +0000606 seq_printf(m, "%pISp\n", &addr->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
David Howellsec268152007-04-26 15:49:28 -0700608}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
David Howellsd2ddc772017-11-02 15:27:50 +0000611 * open "/proc/fs/afs/servers" which provides a summary of active
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * servers
613 */
David Howellsd2ddc772017-11-02 15:27:50 +0000614static int afs_proc_servers_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
David Howellsd2ddc772017-11-02 15:27:50 +0000616 return seq_open(file, &afs_proc_servers_ops);
David Howellsec268152007-04-26 15:49:28 -0700617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
David Howellsd2ddc772017-11-02 15:27:50 +0000620 * Set up the iterator to start reading from the server list and return the
621 * first item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
David Howellsd2ddc772017-11-02 15:27:50 +0000623static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100624 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
David Howellsd2ddc772017-11-02 15:27:50 +0000626 struct afs_net *net = afs_seq2net(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
David Howellsd2ddc772017-11-02 15:27:50 +0000628 rcu_read_lock();
629 return seq_hlist_start_head_rcu(&net->fs_proc, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * move to next cell in cells list
634 */
David Howellsd2ddc772017-11-02 15:27:50 +0000635static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
David Howellsd2ddc772017-11-02 15:27:50 +0000637 struct afs_net *net = afs_seq2net(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Howellsd2ddc772017-11-02 15:27:50 +0000639 return seq_hlist_next_rcu(v, &net->fs_proc, _pos);
David Howellsec268152007-04-26 15:49:28 -0700640}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * clean up after reading from the cells list
644 */
David Howellsd2ddc772017-11-02 15:27:50 +0000645static void afs_proc_servers_stop(struct seq_file *p, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100646 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
David Howellsd2ddc772017-11-02 15:27:50 +0000648 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/*
652 * display a header line followed by a load of volume lines
653 */
David Howellsd2ddc772017-11-02 15:27:50 +0000654static int afs_proc_servers_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
David Howellsd2ddc772017-11-02 15:27:50 +0000656 struct afs_server *server;
657 struct afs_addr_list *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
David Howellsd2ddc772017-11-02 15:27:50 +0000659 if (v == SEQ_START_TOKEN) {
660 seq_puts(m, "UUID USE ADDR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return 0;
662 }
663
David Howellsd2ddc772017-11-02 15:27:50 +0000664 server = list_entry(v, struct afs_server, proc_link);
665 alist = rcu_dereference(server->addresses);
666 seq_printf(m, "%pU %3d %pISp\n",
667 &server->uuid,
668 atomic_read(&server->usage),
669 &alist->addrs[alist->index].transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
David Howellsec268152007-04-26 15:49:28 -0700671}
David Howells6f8880d2018-04-09 21:12:31 +0100672
673void afs_put_sysnames(struct afs_sysnames *sysnames)
674{
675 int i;
676
677 if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
678 for (i = 0; i < sysnames->nr; i++)
679 if (sysnames->subs[i] != afs_init_sysname &&
680 sysnames->subs[i] != sysnames->blank)
681 kfree(sysnames->subs[i]);
682 }
683}
684
685/*
686 * Handle opening of /proc/fs/afs/sysname. If it is opened for writing, we
687 * assume the caller wants to change the substitution list and we allocate a
688 * buffer to hold the list.
689 */
690static int afs_proc_sysname_open(struct inode *inode, struct file *file)
691{
692 struct afs_sysnames *sysnames;
693 struct seq_file *m;
694 int ret;
695
696 ret = seq_open(file, &afs_proc_sysname_ops);
697 if (ret < 0)
698 return ret;
699
700 if (file->f_mode & FMODE_WRITE) {
701 sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
702 if (!sysnames) {
703 seq_release(inode, file);
704 return -ENOMEM;
705 }
706
707 refcount_set(&sysnames->usage, 1);
708 m = file->private_data;
709 m->private = sysnames;
710 }
711
712 return 0;
713}
714
715/*
716 * Handle writes to /proc/fs/afs/sysname to set the @sys substitution.
717 */
718static ssize_t afs_proc_sysname_write(struct file *file,
719 const char __user *buf,
720 size_t size, loff_t *_pos)
721{
722 struct afs_sysnames *sysnames;
723 struct seq_file *m = file->private_data;
724 char *kbuf = NULL, *s, *p, *sub;
725 int ret, len;
726
727 sysnames = m->private;
728 if (!sysnames)
729 return -EINVAL;
730 if (sysnames->error)
731 return sysnames->error;
732
733 if (size >= PAGE_SIZE - 1) {
734 sysnames->error = -EINVAL;
735 return -EINVAL;
736 }
737 if (size == 0)
738 return 0;
739
740 kbuf = memdup_user_nul(buf, size);
741 if (IS_ERR(kbuf))
742 return PTR_ERR(kbuf);
743
744 inode_lock(file_inode(file));
745
746 p = kbuf;
747 while ((s = strsep(&p, " \t\n"))) {
748 len = strlen(s);
749 if (len == 0)
750 continue;
751 ret = -ENAMETOOLONG;
752 if (len >= AFSNAMEMAX)
753 goto error;
754
755 if (len >= 4 &&
756 s[len - 4] == '@' &&
757 s[len - 3] == 's' &&
758 s[len - 2] == 'y' &&
759 s[len - 1] == 's')
760 /* Protect against recursion */
761 goto invalid;
762
763 if (s[0] == '.' &&
764 (len < 2 || (len == 2 && s[1] == '.')))
765 goto invalid;
766
767 if (memchr(s, '/', len))
768 goto invalid;
769
770 ret = -EFBIG;
771 if (sysnames->nr >= AFS_NR_SYSNAME)
772 goto out;
773
774 if (strcmp(s, afs_init_sysname) == 0) {
775 sub = (char *)afs_init_sysname;
776 } else {
777 ret = -ENOMEM;
778 sub = kmemdup(s, len + 1, GFP_KERNEL);
779 if (!sub)
780 goto out;
781 }
782
783 sysnames->subs[sysnames->nr] = sub;
784 sysnames->nr++;
785 }
786
787 ret = size; /* consume everything, always */
788out:
789 inode_unlock(file_inode(file));
790 kfree(kbuf);
791 return ret;
792
793invalid:
794 ret = -EINVAL;
795error:
796 sysnames->error = ret;
797 goto out;
798}
799
800static int afs_proc_sysname_release(struct inode *inode, struct file *file)
801{
802 struct afs_sysnames *sysnames, *kill = NULL;
803 struct seq_file *m = file->private_data;
804 struct afs_net *net = afs_seq2net(m);
805
806 sysnames = m->private;
807 if (sysnames) {
808 if (!sysnames->error) {
809 kill = sysnames;
810 if (sysnames->nr == 0) {
811 sysnames->subs[0] = sysnames->blank;
812 sysnames->nr++;
813 }
814 write_lock(&net->sysnames_lock);
815 kill = net->sysnames;
816 net->sysnames = sysnames;
817 write_unlock(&net->sysnames_lock);
818 }
819 afs_put_sysnames(kill);
820 }
821
822 return seq_release(inode, file);
823}
824
825static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
826 __acquires(&net->sysnames_lock)
827{
828 struct afs_net *net = afs_seq2net(m);
829 struct afs_sysnames *names = net->sysnames;
830
831 read_lock(&net->sysnames_lock);
832
833 if (*pos >= names->nr)
834 return NULL;
835 return (void *)(unsigned long)(*pos + 1);
836}
837
838static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
839{
840 struct afs_net *net = afs_seq2net(m);
841 struct afs_sysnames *names = net->sysnames;
842
843 *pos += 1;
844 if (*pos >= names->nr)
845 return NULL;
846 return (void *)(unsigned long)(*pos + 1);
847}
848
849static void afs_proc_sysname_stop(struct seq_file *m, void *v)
850 __releases(&net->sysnames_lock)
851{
852 struct afs_net *net = afs_seq2net(m);
853
854 read_unlock(&net->sysnames_lock);
855}
856
857static int afs_proc_sysname_show(struct seq_file *m, void *v)
858{
859 struct afs_net *net = afs_seq2net(m);
860 struct afs_sysnames *sysnames = net->sysnames;
861 unsigned int i = (unsigned long)v - 1;
862
863 if (i < sysnames->nr)
864 seq_printf(m, "%s\n", sysnames->subs[i]);
865 return 0;
866}