blob: 4bb612b04f3c0db83e6fec83ae4ec62be238ac5a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void *afs_proc_cell_volumes_start(struct seq_file *p, loff_t *pos);
66static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
67 loff_t *pos);
68static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v);
69static int afs_proc_cell_volumes_show(struct seq_file *m, void *v);
70
James Morris88e9d342009-09-22 16:43:43 -070071static const struct seq_operations afs_proc_cell_volumes_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .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,
76};
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
79static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
80 loff_t *pos);
81static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
82static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
83
James Morris88e9d342009-09-22 16:43:43 -070084static const struct seq_operations afs_proc_cell_vlservers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .start = afs_proc_cell_vlservers_start,
86 .next = afs_proc_cell_vlservers_next,
87 .stop = afs_proc_cell_vlservers_stop,
88 .show = afs_proc_cell_vlservers_show,
89};
90
David Howellsd2ddc772017-11-02 15:27:50 +000091static void *afs_proc_servers_start(struct seq_file *p, loff_t *pos);
92static void *afs_proc_servers_next(struct seq_file *p, void *v,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 loff_t *pos);
David Howellsd2ddc772017-11-02 15:27:50 +000094static void afs_proc_servers_stop(struct seq_file *p, void *v);
95static int afs_proc_servers_show(struct seq_file *m, void *v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
David Howellsd2ddc772017-11-02 15:27:50 +000097static const struct seq_operations afs_proc_servers_ops = {
98 .start = afs_proc_servers_start,
99 .next = afs_proc_servers_next,
100 .stop = afs_proc_servers_stop,
101 .show = afs_proc_servers_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
David Howells6f8880d2018-04-09 21:12:31 +0100104static int afs_proc_sysname_open(struct inode *inode, struct file *file);
105static int afs_proc_sysname_release(struct inode *inode, struct file *file);
106static void *afs_proc_sysname_start(struct seq_file *p, loff_t *pos);
107static void *afs_proc_sysname_next(struct seq_file *p, void *v,
108 loff_t *pos);
109static void afs_proc_sysname_stop(struct seq_file *p, void *v);
110static int afs_proc_sysname_show(struct seq_file *m, void *v);
111static ssize_t afs_proc_sysname_write(struct file *file,
112 const char __user *buf,
113 size_t size, loff_t *_pos);
114
115static const struct seq_operations afs_proc_sysname_ops = {
116 .start = afs_proc_sysname_start,
117 .next = afs_proc_sysname_next,
118 .stop = afs_proc_sysname_stop,
119 .show = afs_proc_sysname_show,
120};
121
122static const struct file_operations afs_proc_sysname_fops = {
123 .open = afs_proc_sysname_open,
124 .read = seq_read,
125 .llseek = seq_lseek,
126 .release = afs_proc_sysname_release,
127 .write = afs_proc_sysname_write,
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * open "/proc/fs/afs/cells" which provides a summary of extant cells
132 */
133static int afs_proc_cells_open(struct inode *inode, struct file *file)
134{
Christoph Hellwig353861c2018-04-13 20:45:09 +0200135 return seq_open(file, &afs_proc_cells_ops);
David Howellsec268152007-04-26 15:49:28 -0700136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * set up the iterator to start reading from the cells list and return the
140 * first item
141 */
142static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100143 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
David Howellsf044c882017-11-02 15:27:45 +0000145 struct afs_net *net = afs_seq2net(m);
146
David Howells989782d2017-11-02 15:27:50 +0000147 rcu_read_lock();
David Howellsf044c882017-11-02 15:27:45 +0000148 return seq_list_start_head(&net->proc_cells, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * move to next cell in cells list
153 */
David Howellsf044c882017-11-02 15:27:45 +0000154static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
David Howellsf044c882017-11-02 15:27:45 +0000156 struct afs_net *net = afs_seq2net(m);
157
158 return seq_list_next(v, &net->proc_cells, pos);
David Howellsec268152007-04-26 15:49:28 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
162 * clean up after reading from the cells list
163 */
David Howellsf044c882017-11-02 15:27:45 +0000164static void afs_proc_cells_stop(struct seq_file *m, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100165 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
David Howells989782d2017-11-02 15:27:50 +0000167 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * display a header line followed by a load of cell lines
172 */
173static int afs_proc_cells_show(struct seq_file *m, void *v)
174{
175 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
David Howellsf044c882017-11-02 15:27:45 +0000176 struct afs_net *net = afs_seq2net(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
David Howellsf044c882017-11-02 15:27:45 +0000178 if (v == &net->proc_cells) {
David Howellsec268152007-04-26 15:49:28 -0700179 /* display header on line 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 seq_puts(m, "USE NAME\n");
181 return 0;
182 }
183
184 /* display one cell per line on subsequent lines */
David Howells989782d2017-11-02 15:27:50 +0000185 seq_printf(m, "%3u %s\n", atomic_read(&cell->usage), cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
David Howellsec268152007-04-26 15:49:28 -0700187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * handle writes to /proc/fs/afs/cells
191 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
192 */
193static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
194 size_t size, loff_t *_pos)
195{
David Howellsf044c882017-11-02 15:27:45 +0000196 struct afs_net *net = afs_proc2net(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 char *kbuf, *name, *args;
198 int ret;
199
200 /* start by dragging the command into memory */
201 if (size <= 1 || size >= PAGE_SIZE)
202 return -EINVAL;
203
Al Viro16e5c1f2015-12-24 00:06:05 -0500204 kbuf = memdup_user_nul(buf, size);
205 if (IS_ERR(kbuf))
206 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* trim to first NL */
209 name = memchr(kbuf, '\n', size);
210 if (name)
211 *name = 0;
212
213 /* split into command, name and argslist */
214 name = strchr(kbuf, ' ');
215 if (!name)
216 goto inval;
217 do {
218 *name++ = 0;
219 } while(*name == ' ');
220 if (!*name)
221 goto inval;
222
223 args = strchr(name, ' ');
224 if (!args)
225 goto inval;
226 do {
227 *args++ = 0;
228 } while(*args == ' ');
229 if (!*args)
230 goto inval;
231
232 /* determine command to perform */
233 _debug("cmd=%s name=%s args=%s", kbuf, name, args);
234
235 if (strcmp(kbuf, "add") == 0) {
236 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
David Howells989782d2017-11-02 15:27:50 +0000238 cell = afs_lookup_cell(net, name, strlen(name), args, true);
David Howells08e0e7c2007-04-26 15:55:03 -0700239 if (IS_ERR(cell)) {
240 ret = PTR_ERR(cell);
241 goto done;
242 }
243
David Howells17814ae2018-04-09 21:12:31 +0100244 if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
245 afs_put_cell(net, cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 printk("kAFS: Added new cell '%s'\n", name);
David Howellsec268152007-04-26 15:49:28 -0700247 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto inval;
249 }
250
251 ret = size;
252
David Howellsec268152007-04-26 15:49:28 -0700253done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 kfree(kbuf);
255 _leave(" = %d", ret);
256 return ret;
257
David Howellsec268152007-04-26 15:49:28 -0700258inval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ret = -EINVAL;
260 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
261 goto done;
David Howellsec268152007-04-26 15:49:28 -0700262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
265 size_t size, loff_t *_pos)
266{
David Howells37ab6362018-04-06 14:17:23 +0100267 struct afs_cell *cell;
268 struct afs_net *net = afs_proc2net(file);
269 unsigned int seq = 0;
270 char name[AFS_MAXCELLNAME + 1];
271 int len;
272
273 if (*_pos > 0)
274 return 0;
275 if (!net->ws_cell)
276 return 0;
277
278 rcu_read_lock();
279 do {
280 read_seqbegin_or_lock(&net->cells_lock, &seq);
281 len = 0;
282 cell = rcu_dereference_raw(net->ws_cell);
283 if (cell) {
284 len = cell->name_len;
285 memcpy(name, cell->name, len);
286 }
287 } while (need_seqretry(&net->cells_lock, seq));
288 done_seqretry(&net->cells_lock, seq);
289 rcu_read_unlock();
290
291 if (!len)
292 return 0;
293
294 name[len++] = '\n';
295 if (len > size)
296 len = size;
297 if (copy_to_user(buf, name, len) != 0)
298 return -EFAULT;
299 *_pos = 1;
300 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * handle writes to /proc/fs/afs/rootcell
305 * - to initialize rootcell: echo "cell.name:192.168.231.14"
306 */
307static ssize_t afs_proc_rootcell_write(struct file *file,
308 const char __user *buf,
309 size_t size, loff_t *_pos)
310{
David Howellsf044c882017-11-02 15:27:45 +0000311 struct afs_net *net = afs_proc2net(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 char *kbuf, *s;
313 int ret;
314
315 /* start by dragging the command into memory */
316 if (size <= 1 || size >= PAGE_SIZE)
317 return -EINVAL;
318
Al Viro16e5c1f2015-12-24 00:06:05 -0500319 kbuf = memdup_user_nul(buf, size);
320 if (IS_ERR(kbuf))
321 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David Howells6f8880d2018-04-09 21:12:31 +0100323 ret = -EINVAL;
324 if (kbuf[0] == '.')
325 goto out;
326 if (memchr(kbuf, '/', size))
327 goto out;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* trim to first NL */
330 s = memchr(kbuf, '\n', size);
331 if (s)
332 *s = 0;
333
334 /* determine command to perform */
335 _debug("rootcell=%s", kbuf);
336
David Howellsf044c882017-11-02 15:27:45 +0000337 ret = afs_cell_init(net, kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (ret >= 0)
339 ret = size; /* consume everything, always */
340
David Howells6f8880d2018-04-09 21:12:31 +0100341out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 _leave(" = %d", ret);
344 return ret;
David Howellsec268152007-04-26 15:49:28 -0700345}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * set up the iterator to start reading from the cells list and return the
349 * first item
350 */
351static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100352 __acquires(cell->proc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Christoph Hellwig353861c2018-04-13 20:45:09 +0200354 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 _enter("cell=%p pos=%Ld", cell, *_pos);
357
David Howellsd2ddc772017-11-02 15:27:50 +0000358 read_lock(&cell->proc_lock);
359 return seq_list_start_head(&cell->proc_volumes, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/*
363 * move to next cell in cells list
364 */
365static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
366 loff_t *_pos)
367{
Christoph Hellwig353861c2018-04-13 20:45:09 +0200368 struct afs_cell *cell = PDE_DATA(file_inode(p->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 _enter("cell=%p pos=%Ld", cell, *_pos);
David Howellsd2ddc772017-11-02 15:27:50 +0000371 return seq_list_next(v, &cell->proc_volumes, _pos);
David Howellsec268152007-04-26 15:49:28 -0700372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * clean up after reading from the cells list
376 */
377static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100378 __releases(cell->proc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Christoph Hellwig353861c2018-04-13 20:45:09 +0200380 struct afs_cell *cell = PDE_DATA(file_inode(p->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David Howellsd2ddc772017-11-02 15:27:50 +0000382 read_unlock(&cell->proc_lock);
David Howellsec268152007-04-26 15:49:28 -0700383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David Howellsd2ddc772017-11-02 15:27:50 +0000385static const char afs_vol_types[3][3] = {
386 [AFSVL_RWVOL] = "RW",
387 [AFSVL_ROVOL] = "RO",
388 [AFSVL_BACKVOL] = "BK",
David Howells08e0e7c2007-04-26 15:55:03 -0700389};
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * display a header line followed by a load of volume lines
393 */
394static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
395{
Christoph Hellwig353861c2018-04-13 20:45:09 +0200396 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
David Howellsd2ddc772017-11-02 15:27:50 +0000397 struct afs_volume *vol = list_entry(v, struct afs_volume, proc_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David Howellsd2ddc772017-11-02 15:27:50 +0000399 /* Display header on line 1 */
400 if (v == &cell->proc_volumes) {
401 seq_puts(m, "USE VID TY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403 }
404
David Howellsd2ddc772017-11-02 15:27:50 +0000405 seq_printf(m, "%3d %08x %s\n",
406 atomic_read(&vol->usage), vol->vid,
407 afs_vol_types[vol->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 return 0;
David Howellsec268152007-04-26 15:49:28 -0700410}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * set up the iterator to start reading from the cells list and return the
414 * first item
415 */
416static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100417 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
David Howells8b2a4642017-11-02 15:27:50 +0000419 struct afs_addr_list *alist;
Christoph Hellwig353861c2018-04-13 20:45:09 +0200420 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 loff_t pos = *_pos;
422
David Howells8b2a4642017-11-02 15:27:50 +0000423 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Howells8b2a4642017-11-02 15:27:50 +0000425 alist = rcu_dereference(cell->vl_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* allow for the header line */
428 if (!pos)
429 return (void *) 1;
430 pos--;
431
David Howells8b2a4642017-11-02 15:27:50 +0000432 if (!alist || pos >= alist->nr_addrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return NULL;
434
David Howells8b2a4642017-11-02 15:27:50 +0000435 return alist->addrs + pos;
David Howellsec268152007-04-26 15:49:28 -0700436}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
439 * move to next cell in cells list
440 */
441static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
442 loff_t *_pos)
443{
David Howells8b2a4642017-11-02 15:27:50 +0000444 struct afs_addr_list *alist;
Christoph Hellwig353861c2018-04-13 20:45:09 +0200445 struct afs_cell *cell = PDE_DATA(file_inode(p->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 loff_t pos;
447
David Howells8b2a4642017-11-02 15:27:50 +0000448 alist = rcu_dereference(cell->vl_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 pos = *_pos;
451 (*_pos)++;
David Howells8b2a4642017-11-02 15:27:50 +0000452 if (!alist || pos >= alist->nr_addrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return NULL;
454
David Howells8b2a4642017-11-02 15:27:50 +0000455 return alist->addrs + pos;
David Howellsec268152007-04-26 15:49:28 -0700456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * clean up after reading from the cells list
460 */
461static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100462 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
David Howells8b2a4642017-11-02 15:27:50 +0000464 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700465}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
468 * display a header line followed by a load of volume lines
469 */
470static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
471{
David Howells4d9df982017-11-02 15:27:47 +0000472 struct sockaddr_rxrpc *addr = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* display header on line 1 */
David Howells4d9df982017-11-02 15:27:47 +0000475 if (v == (void *)1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 seq_puts(m, "ADDRESS\n");
477 return 0;
478 }
479
480 /* display one cell per line on subsequent lines */
David Howells4d9df982017-11-02 15:27:47 +0000481 seq_printf(m, "%pISp\n", &addr->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
David Howellsec268152007-04-26 15:49:28 -0700483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
David Howellsd2ddc772017-11-02 15:27:50 +0000486 * Set up the iterator to start reading from the server list and return the
487 * first item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
David Howellsd2ddc772017-11-02 15:27:50 +0000489static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100490 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
David Howellsd2ddc772017-11-02 15:27:50 +0000492 struct afs_net *net = afs_seq2net(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David Howellsd2ddc772017-11-02 15:27:50 +0000494 rcu_read_lock();
495 return seq_hlist_start_head_rcu(&net->fs_proc, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700496}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
499 * move to next cell in cells list
500 */
David Howellsd2ddc772017-11-02 15:27:50 +0000501static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
David Howellsd2ddc772017-11-02 15:27:50 +0000503 struct afs_net *net = afs_seq2net(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
David Howellsd2ddc772017-11-02 15:27:50 +0000505 return seq_hlist_next_rcu(v, &net->fs_proc, _pos);
David Howellsec268152007-04-26 15:49:28 -0700506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
509 * clean up after reading from the cells list
510 */
David Howellsd2ddc772017-11-02 15:27:50 +0000511static void afs_proc_servers_stop(struct seq_file *p, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100512 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howellsd2ddc772017-11-02 15:27:50 +0000514 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/*
518 * display a header line followed by a load of volume lines
519 */
David Howellsd2ddc772017-11-02 15:27:50 +0000520static int afs_proc_servers_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
David Howellsd2ddc772017-11-02 15:27:50 +0000522 struct afs_server *server;
523 struct afs_addr_list *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Howellsd2ddc772017-11-02 15:27:50 +0000525 if (v == SEQ_START_TOKEN) {
526 seq_puts(m, "UUID USE ADDR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528 }
529
David Howellsd2ddc772017-11-02 15:27:50 +0000530 server = list_entry(v, struct afs_server, proc_link);
531 alist = rcu_dereference(server->addresses);
532 seq_printf(m, "%pU %3d %pISp\n",
533 &server->uuid,
534 atomic_read(&server->usage),
535 &alist->addrs[alist->index].transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
David Howellsec268152007-04-26 15:49:28 -0700537}
David Howells6f8880d2018-04-09 21:12:31 +0100538
539void afs_put_sysnames(struct afs_sysnames *sysnames)
540{
541 int i;
542
543 if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
544 for (i = 0; i < sysnames->nr; i++)
545 if (sysnames->subs[i] != afs_init_sysname &&
546 sysnames->subs[i] != sysnames->blank)
547 kfree(sysnames->subs[i]);
548 }
549}
550
551/*
552 * Handle opening of /proc/fs/afs/sysname. If it is opened for writing, we
553 * assume the caller wants to change the substitution list and we allocate a
554 * buffer to hold the list.
555 */
556static int afs_proc_sysname_open(struct inode *inode, struct file *file)
557{
558 struct afs_sysnames *sysnames;
559 struct seq_file *m;
560 int ret;
561
562 ret = seq_open(file, &afs_proc_sysname_ops);
563 if (ret < 0)
564 return ret;
565
566 if (file->f_mode & FMODE_WRITE) {
567 sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
568 if (!sysnames) {
569 seq_release(inode, file);
570 return -ENOMEM;
571 }
572
573 refcount_set(&sysnames->usage, 1);
574 m = file->private_data;
575 m->private = sysnames;
576 }
577
578 return 0;
579}
580
581/*
582 * Handle writes to /proc/fs/afs/sysname to set the @sys substitution.
583 */
584static ssize_t afs_proc_sysname_write(struct file *file,
585 const char __user *buf,
586 size_t size, loff_t *_pos)
587{
588 struct afs_sysnames *sysnames;
589 struct seq_file *m = file->private_data;
590 char *kbuf = NULL, *s, *p, *sub;
591 int ret, len;
592
593 sysnames = m->private;
594 if (!sysnames)
595 return -EINVAL;
596 if (sysnames->error)
597 return sysnames->error;
598
599 if (size >= PAGE_SIZE - 1) {
600 sysnames->error = -EINVAL;
601 return -EINVAL;
602 }
603 if (size == 0)
604 return 0;
605
606 kbuf = memdup_user_nul(buf, size);
607 if (IS_ERR(kbuf))
608 return PTR_ERR(kbuf);
609
610 inode_lock(file_inode(file));
611
612 p = kbuf;
613 while ((s = strsep(&p, " \t\n"))) {
614 len = strlen(s);
615 if (len == 0)
616 continue;
617 ret = -ENAMETOOLONG;
618 if (len >= AFSNAMEMAX)
619 goto error;
620
621 if (len >= 4 &&
622 s[len - 4] == '@' &&
623 s[len - 3] == 's' &&
624 s[len - 2] == 'y' &&
625 s[len - 1] == 's')
626 /* Protect against recursion */
627 goto invalid;
628
629 if (s[0] == '.' &&
630 (len < 2 || (len == 2 && s[1] == '.')))
631 goto invalid;
632
633 if (memchr(s, '/', len))
634 goto invalid;
635
636 ret = -EFBIG;
637 if (sysnames->nr >= AFS_NR_SYSNAME)
638 goto out;
639
640 if (strcmp(s, afs_init_sysname) == 0) {
641 sub = (char *)afs_init_sysname;
642 } else {
643 ret = -ENOMEM;
644 sub = kmemdup(s, len + 1, GFP_KERNEL);
645 if (!sub)
646 goto out;
647 }
648
649 sysnames->subs[sysnames->nr] = sub;
650 sysnames->nr++;
651 }
652
653 ret = size; /* consume everything, always */
654out:
655 inode_unlock(file_inode(file));
656 kfree(kbuf);
657 return ret;
658
659invalid:
660 ret = -EINVAL;
661error:
662 sysnames->error = ret;
663 goto out;
664}
665
666static int afs_proc_sysname_release(struct inode *inode, struct file *file)
667{
668 struct afs_sysnames *sysnames, *kill = NULL;
669 struct seq_file *m = file->private_data;
670 struct afs_net *net = afs_seq2net(m);
671
672 sysnames = m->private;
673 if (sysnames) {
674 if (!sysnames->error) {
675 kill = sysnames;
676 if (sysnames->nr == 0) {
677 sysnames->subs[0] = sysnames->blank;
678 sysnames->nr++;
679 }
680 write_lock(&net->sysnames_lock);
681 kill = net->sysnames;
682 net->sysnames = sysnames;
683 write_unlock(&net->sysnames_lock);
684 }
685 afs_put_sysnames(kill);
686 }
687
688 return seq_release(inode, file);
689}
690
691static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
692 __acquires(&net->sysnames_lock)
693{
694 struct afs_net *net = afs_seq2net(m);
695 struct afs_sysnames *names = net->sysnames;
696
697 read_lock(&net->sysnames_lock);
698
699 if (*pos >= names->nr)
700 return NULL;
701 return (void *)(unsigned long)(*pos + 1);
702}
703
704static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
705{
706 struct afs_net *net = afs_seq2net(m);
707 struct afs_sysnames *names = net->sysnames;
708
709 *pos += 1;
710 if (*pos >= names->nr)
711 return NULL;
712 return (void *)(unsigned long)(*pos + 1);
713}
714
715static void afs_proc_sysname_stop(struct seq_file *m, void *v)
716 __releases(&net->sysnames_lock)
717{
718 struct afs_net *net = afs_seq2net(m);
719
720 read_unlock(&net->sysnames_lock);
721}
722
723static int afs_proc_sysname_show(struct seq_file *m, void *v)
724{
725 struct afs_net *net = afs_seq2net(m);
726 struct afs_sysnames *sysnames = net->sysnames;
727 unsigned int i = (unsigned long)v - 1;
728
729 if (i < sysnames->nr)
730 seq_printf(m, "%s\n", sysnames->subs[i]);
731 return 0;
732}
David Howellsd55b4da2018-04-06 14:17:24 +0100733
734/*
735 * Display general per-net namespace statistics
736 */
737static int afs_proc_stats_show(struct seq_file *m, void *v)
738{
739 struct afs_net *net = afs_seq2net(m);
740
741 seq_puts(m, "kAFS statistics\n");
742
David Howellsf3ddee82018-04-06 14:17:25 +0100743 seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
David Howellsd55b4da2018-04-06 14:17:24 +0100744 atomic_read(&net->n_lookup),
745 atomic_read(&net->n_reval),
David Howellsf3ddee82018-04-06 14:17:25 +0100746 atomic_read(&net->n_inval),
747 atomic_read(&net->n_relpg));
David Howellsd55b4da2018-04-06 14:17:24 +0100748
749 seq_printf(m, "dir-data: rdpg=%u\n",
750 atomic_read(&net->n_read_dir));
David Howells63a46812018-04-06 14:17:25 +0100751
752 seq_printf(m, "dir-edit: cr=%u rm=%u\n",
753 atomic_read(&net->n_dir_cr),
754 atomic_read(&net->n_dir_rm));
David Howells76a5cb62018-04-06 14:17:26 +0100755
756 seq_printf(m, "file-rd : n=%u nb=%lu\n",
757 atomic_read(&net->n_fetches),
758 atomic_long_read(&net->n_fetch_bytes));
759 seq_printf(m, "file-wr : n=%u nb=%lu\n",
760 atomic_read(&net->n_stores),
761 atomic_long_read(&net->n_store_bytes));
David Howellsd55b4da2018-04-06 14:17:24 +0100762 return 0;
763}
David Howells10495a02018-05-18 11:46:14 +0100764
765/*
766 * initialise /proc/fs/afs/<cell>/
767 */
768int afs_proc_cell_setup(struct afs_net *net, struct afs_cell *cell)
769{
770 struct proc_dir_entry *dir;
771
772 _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
773
774 dir = proc_mkdir(cell->name, net->proc_afs);
775 if (!dir)
776 goto error_dir;
777
778 if (!proc_create_seq_data("vlservers", 0, dir,
779 &afs_proc_cell_vlservers_ops, cell))
780 goto error_tree;
781 if (!proc_create_seq_data("volumes", 0, dir,
782 &afs_proc_cell_volumes_ops, cell))
783 goto error_tree;
784
785 _leave(" = 0");
786 return 0;
787
788error_tree:
789 remove_proc_subtree(cell->name, net->proc_afs);
790error_dir:
791 _leave(" = -ENOMEM");
792 return -ENOMEM;
793}
794
795/*
796 * remove /proc/fs/afs/<cell>/
797 */
798void afs_proc_cell_remove(struct afs_net *net, struct afs_cell *cell)
799{
800 _enter("");
801
802 remove_proc_subtree(cell->name, net->proc_afs);
803
804 _leave("");
805}
806
807/*
808 * initialise the /proc/fs/afs/ directory
809 */
810int afs_proc_init(struct afs_net *net)
811{
812 _enter("");
813
814 net->proc_afs = proc_mkdir("fs/afs", NULL);
815 if (!net->proc_afs)
816 goto error_dir;
817
818 if (!proc_create("cells", 0644, net->proc_afs, &afs_proc_cells_fops) ||
819 !proc_create("rootcell", 0644, net->proc_afs, &afs_proc_rootcell_fops) ||
820 !proc_create_seq("servers", 0644, net->proc_afs, &afs_proc_servers_ops) ||
821 !proc_create_single("stats", 0644, net->proc_afs, afs_proc_stats_show) ||
822 !proc_create("sysname", 0644, net->proc_afs, &afs_proc_sysname_fops))
823 goto error_tree;
824
825 _leave(" = 0");
826 return 0;
827
828error_tree:
829 proc_remove(net->proc_afs);
830error_dir:
831 _leave(" = -ENOMEM");
832 return -ENOMEM;
833}
834
835/*
836 * clean up the /proc/fs/afs/ directory
837 */
838void afs_proc_cleanup(struct afs_net *net)
839{
840 proc_remove(net->proc_afs);
841 net->proc_afs = NULL;
842}