blob: 476dcbb79713d20d12023dfb265a0ef62e48e6fc [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_seq2net(struct seq_file *m)
21{
David Howells5b86d4f2018-05-18 11:46:15 +010022 return afs_net(seq_file_net(m));
23}
24
25static inline struct afs_net *afs_seq2net_single(struct seq_file *m)
26{
27 return afs_net(seq_file_single_net(m));
David Howellsf044c882017-11-02 15:27:45 +000028}
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
David Howells5d9de252018-05-18 11:46:15 +010031 * Display the list of cells known to the namespace.
David Howellsf0691682018-05-18 11:46:14 +010032 */
33static int afs_proc_cells_show(struct seq_file *m, void *v)
34{
35 struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
36 struct afs_net *net = afs_seq2net(m);
37
38 if (v == &net->proc_cells) {
39 /* display header on line 1 */
40 seq_puts(m, "USE NAME\n");
41 return 0;
42 }
43
44 /* display one cell per line on subsequent lines */
45 seq_printf(m, "%3u %s\n", atomic_read(&cell->usage), cell->name);
46 return 0;
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +010050 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
David Howells989782d2017-11-02 15:27:50 +000052 rcu_read_lock();
David Howells5d9de252018-05-18 11:46:15 +010053 return seq_list_start_head(&afs_seq2net(m)->proc_cells, *_pos);
David Howellsec268152007-04-26 15:49:28 -070054}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Howellsf044c882017-11-02 15:27:45 +000056static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
David Howells5d9de252018-05-18 11:46:15 +010058 return seq_list_next(v, &afs_seq2net(m)->proc_cells, pos);
David Howellsec268152007-04-26 15:49:28 -070059}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David Howellsf044c882017-11-02 15:27:45 +000061static void afs_proc_cells_stop(struct seq_file *m, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +010062 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
David Howells989782d2017-11-02 15:27:50 +000064 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -070065}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David Howells5d9de252018-05-18 11:46:15 +010067static const struct seq_operations afs_proc_cells_ops = {
68 .start = afs_proc_cells_start,
69 .next = afs_proc_cells_next,
70 .stop = afs_proc_cells_stop,
71 .show = afs_proc_cells_show,
72};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * handle writes to /proc/fs/afs/cells
76 * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
77 */
David Howells5b86d4f2018-05-18 11:46:15 +010078static int afs_proc_cells_write(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
David Howells5b86d4f2018-05-18 11:46:15 +010080 struct seq_file *m = file->private_data;
81 struct afs_net *net = afs_seq2net(m);
82 char *name, *args;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int ret;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* trim to first NL */
David Howells5b86d4f2018-05-18 11:46:15 +010086 name = memchr(buf, '\n', size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (name)
88 *name = 0;
89
90 /* split into command, name and argslist */
David Howells5b86d4f2018-05-18 11:46:15 +010091 name = strchr(buf, ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!name)
93 goto inval;
94 do {
95 *name++ = 0;
96 } while(*name == ' ');
97 if (!*name)
98 goto inval;
99
100 args = strchr(name, ' ');
David Howellsecfe9512018-09-07 23:55:17 +0100101 if (args) {
102 do {
103 *args++ = 0;
104 } while(*args == ' ');
105 if (!*args)
106 goto inval;
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* determine command to perform */
David Howells5b86d4f2018-05-18 11:46:15 +0100110 _debug("cmd=%s name=%s args=%s", buf, name, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
David Howells5b86d4f2018-05-18 11:46:15 +0100112 if (strcmp(buf, "add") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David Howells989782d2017-11-02 15:27:50 +0000115 cell = afs_lookup_cell(net, name, strlen(name), args, true);
David Howells08e0e7c2007-04-26 15:55:03 -0700116 if (IS_ERR(cell)) {
117 ret = PTR_ERR(cell);
118 goto done;
119 }
120
David Howells17814ae2018-04-09 21:12:31 +0100121 if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
122 afs_put_cell(net, cell);
David Howellsec268152007-04-26 15:49:28 -0700123 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto inval;
125 }
126
David Howells5b86d4f2018-05-18 11:46:15 +0100127 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David Howellsec268152007-04-26 15:49:28 -0700129done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 _leave(" = %d", ret);
131 return ret;
132
David Howellsec268152007-04-26 15:49:28 -0700133inval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 ret = -EINVAL;
135 printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
136 goto done;
David Howellsec268152007-04-26 15:49:28 -0700137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
David Howells5d9de252018-05-18 11:46:15 +0100139/*
David Howells5b86d4f2018-05-18 11:46:15 +0100140 * Display the name of the current workstation cell.
David Howells5d9de252018-05-18 11:46:15 +0100141 */
David Howells5b86d4f2018-05-18 11:46:15 +0100142static int afs_proc_rootcell_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
David Howells37ab6362018-04-06 14:17:23 +0100144 struct afs_cell *cell;
David Howells5b86d4f2018-05-18 11:46:15 +0100145 struct afs_net *net;
David Howells37ab6362018-04-06 14:17:23 +0100146
David Howells5b86d4f2018-05-18 11:46:15 +0100147 net = afs_seq2net_single(m);
148 if (rcu_access_pointer(net->ws_cell)) {
149 rcu_read_lock();
150 cell = rcu_dereference(net->ws_cell);
151 if (cell)
152 seq_printf(m, "%s\n", cell->name);
153 rcu_read_unlock();
154 }
155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
David Howells5d9de252018-05-18 11:46:15 +0100159 * Set the current workstation cell and optionally supply its list of volume
160 * location servers.
161 *
162 * echo "cell.name:192.168.231.14" >/proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
David Howells5b86d4f2018-05-18 11:46:15 +0100164static int afs_proc_rootcell_write(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
David Howells5b86d4f2018-05-18 11:46:15 +0100166 struct seq_file *m = file->private_data;
167 struct afs_net *net = afs_seq2net_single(m);
168 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 int ret;
170
David Howells6f8880d2018-04-09 21:12:31 +0100171 ret = -EINVAL;
David Howells5b86d4f2018-05-18 11:46:15 +0100172 if (buf[0] == '.')
David Howells6f8880d2018-04-09 21:12:31 +0100173 goto out;
David Howells5b86d4f2018-05-18 11:46:15 +0100174 if (memchr(buf, '/', size))
David Howells6f8880d2018-04-09 21:12:31 +0100175 goto out;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* trim to first NL */
David Howells5b86d4f2018-05-18 11:46:15 +0100178 s = memchr(buf, '\n', size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (s)
180 *s = 0;
181
182 /* determine command to perform */
David Howells5b86d4f2018-05-18 11:46:15 +0100183 _debug("rootcell=%s", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Howells5b86d4f2018-05-18 11:46:15 +0100185 ret = afs_cell_init(net, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Howells6f8880d2018-04-09 21:12:31 +0100187out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 _leave(" = %d", ret);
189 return ret;
David Howellsec268152007-04-26 15:49:28 -0700190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David Howellsf0691682018-05-18 11:46:14 +0100192static const char afs_vol_types[3][3] = {
193 [AFSVL_RWVOL] = "RW",
194 [AFSVL_ROVOL] = "RO",
195 [AFSVL_BACKVOL] = "BK",
196};
197
198/*
David Howells5d9de252018-05-18 11:46:15 +0100199 * Display the list of volumes known to a cell.
David Howellsf0691682018-05-18 11:46:14 +0100200 */
201static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
202{
203 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
204 struct afs_volume *vol = list_entry(v, struct afs_volume, proc_link);
205
206 /* Display header on line 1 */
207 if (v == &cell->proc_volumes) {
208 seq_puts(m, "USE VID TY\n");
209 return 0;
210 }
211
212 seq_printf(m, "%3d %08x %s\n",
213 atomic_read(&vol->usage), vol->vid,
214 afs_vol_types[vol->type]);
215
216 return 0;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100220 __acquires(cell->proc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Christoph Hellwig353861c2018-04-13 20:45:09 +0200222 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David Howellsd2ddc772017-11-02 15:27:50 +0000224 read_lock(&cell->proc_lock);
225 return seq_list_start_head(&cell->proc_volumes, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
David Howells5b86d4f2018-05-18 11:46:15 +0100228static void *afs_proc_cell_volumes_next(struct seq_file *m, void *v,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 loff_t *_pos)
230{
David Howells5b86d4f2018-05-18 11:46:15 +0100231 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
David Howellsd2ddc772017-11-02 15:27:50 +0000233 return seq_list_next(v, &cell->proc_volumes, _pos);
David Howellsec268152007-04-26 15:49:28 -0700234}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
David Howells5b86d4f2018-05-18 11:46:15 +0100236static void afs_proc_cell_volumes_stop(struct seq_file *m, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100237 __releases(cell->proc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
David Howells5b86d4f2018-05-18 11:46:15 +0100239 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
David Howellsd2ddc772017-11-02 15:27:50 +0000241 read_unlock(&cell->proc_lock);
David Howellsec268152007-04-26 15:49:28 -0700242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
David Howells5d9de252018-05-18 11:46:15 +0100244static const struct seq_operations afs_proc_cell_volumes_ops = {
245 .start = afs_proc_cell_volumes_start,
246 .next = afs_proc_cell_volumes_next,
247 .stop = afs_proc_cell_volumes_stop,
248 .show = afs_proc_cell_volumes_show,
249};
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
David Howells5d9de252018-05-18 11:46:15 +0100252 * Display the list of Volume Location servers we're using for a cell.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
David Howellsf0691682018-05-18 11:46:14 +0100254static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
David Howellsf0691682018-05-18 11:46:14 +0100256 struct sockaddr_rxrpc *addr = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Howellsf0691682018-05-18 11:46:14 +0100258 /* display header on line 1 */
259 if (v == (void *)1) {
260 seq_puts(m, "ADDRESS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262 }
263
David Howellsf0691682018-05-18 11:46:14 +0100264 /* display one cell per line on subsequent lines */
265 seq_printf(m, "%pISp\n", &addr->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
David Howellsec268152007-04-26 15:49:28 -0700267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100270 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
David Howells8b2a4642017-11-02 15:27:50 +0000272 struct afs_addr_list *alist;
Christoph Hellwig353861c2018-04-13 20:45:09 +0200273 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 loff_t pos = *_pos;
275
David Howells8b2a4642017-11-02 15:27:50 +0000276 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Howells8b2a4642017-11-02 15:27:50 +0000278 alist = rcu_dereference(cell->vl_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* allow for the header line */
281 if (!pos)
282 return (void *) 1;
283 pos--;
284
David Howells8b2a4642017-11-02 15:27:50 +0000285 if (!alist || pos >= alist->nr_addrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return NULL;
287
David Howells8b2a4642017-11-02 15:27:50 +0000288 return alist->addrs + pos;
David Howellsec268152007-04-26 15:49:28 -0700289}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
David Howells5b86d4f2018-05-18 11:46:15 +0100291static void *afs_proc_cell_vlservers_next(struct seq_file *m, void *v,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 loff_t *_pos)
293{
David Howells8b2a4642017-11-02 15:27:50 +0000294 struct afs_addr_list *alist;
David Howells5b86d4f2018-05-18 11:46:15 +0100295 struct afs_cell *cell = PDE_DATA(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 loff_t pos;
297
David Howells8b2a4642017-11-02 15:27:50 +0000298 alist = rcu_dereference(cell->vl_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 pos = *_pos;
301 (*_pos)++;
David Howells8b2a4642017-11-02 15:27:50 +0000302 if (!alist || pos >= alist->nr_addrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return NULL;
304
David Howells8b2a4642017-11-02 15:27:50 +0000305 return alist->addrs + pos;
David Howellsec268152007-04-26 15:49:28 -0700306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David Howells5b86d4f2018-05-18 11:46:15 +0100308static void afs_proc_cell_vlservers_stop(struct seq_file *m, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100309 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
David Howells8b2a4642017-11-02 15:27:50 +0000311 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700312}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David Howells5d9de252018-05-18 11:46:15 +0100314static const struct seq_operations afs_proc_cell_vlservers_ops = {
315 .start = afs_proc_cell_vlservers_start,
316 .next = afs_proc_cell_vlservers_next,
317 .stop = afs_proc_cell_vlservers_stop,
318 .show = afs_proc_cell_vlservers_show,
319};
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
David Howells5d9de252018-05-18 11:46:15 +0100322 * Display the list of fileservers we're using within a namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
David Howellsf0691682018-05-18 11:46:14 +0100324static int afs_proc_servers_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
David Howellsf0691682018-05-18 11:46:14 +0100326 struct afs_server *server;
327 struct afs_addr_list *alist;
David Howells0aac4bce2018-06-02 22:20:31 +0100328 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David Howellsf0691682018-05-18 11:46:14 +0100330 if (v == SEQ_START_TOKEN) {
331 seq_puts(m, "UUID USE ADDR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333 }
334
David Howellsf0691682018-05-18 11:46:14 +0100335 server = list_entry(v, struct afs_server, proc_link);
336 alist = rcu_dereference(server->addresses);
David Howells0aac4bce2018-06-02 22:20:31 +0100337 seq_printf(m, "%pU %3d %pISpc%s\n",
David Howellsf0691682018-05-18 11:46:14 +0100338 &server->uuid,
339 atomic_read(&server->usage),
David Howells0aac4bce2018-06-02 22:20:31 +0100340 &alist->addrs[0].transport,
341 alist->index == 0 ? "*" : "");
342 for (i = 1; i < alist->nr_addrs; i++)
343 seq_printf(m, " %pISpc%s\n",
344 &alist->addrs[i].transport,
345 alist->index == i ? "*" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
David Howellsec268152007-04-26 15:49:28 -0700347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
David Howellsd2ddc772017-11-02 15:27:50 +0000349static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
David Howellsfe342cf2018-04-09 21:12:31 +0100350 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David Howellsd2ddc772017-11-02 15:27:50 +0000352 rcu_read_lock();
David Howells5d9de252018-05-18 11:46:15 +0100353 return seq_hlist_start_head_rcu(&afs_seq2net(m)->fs_proc, *_pos);
David Howellsec268152007-04-26 15:49:28 -0700354}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Howellsd2ddc772017-11-02 15:27:50 +0000356static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David Howells5d9de252018-05-18 11:46:15 +0100358 return seq_hlist_next_rcu(v, &afs_seq2net(m)->fs_proc, _pos);
David Howellsec268152007-04-26 15:49:28 -0700359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
David Howells5b86d4f2018-05-18 11:46:15 +0100361static void afs_proc_servers_stop(struct seq_file *m, void *v)
David Howellsfe342cf2018-04-09 21:12:31 +0100362 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
David Howellsd2ddc772017-11-02 15:27:50 +0000364 rcu_read_unlock();
David Howellsec268152007-04-26 15:49:28 -0700365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Howells5d9de252018-05-18 11:46:15 +0100367static const struct seq_operations afs_proc_servers_ops = {
368 .start = afs_proc_servers_start,
369 .next = afs_proc_servers_next,
370 .stop = afs_proc_servers_stop,
371 .show = afs_proc_servers_show,
372};
David Howells6f8880d2018-04-09 21:12:31 +0100373
David Howells6f8880d2018-04-09 21:12:31 +0100374/*
David Howells5d9de252018-05-18 11:46:15 +0100375 * Display the list of strings that may be substituted for the @sys pathname
376 * macro.
David Howells6f8880d2018-04-09 21:12:31 +0100377 */
David Howells5d9de252018-05-18 11:46:15 +0100378static int afs_proc_sysname_show(struct seq_file *m, void *v)
David Howells6f8880d2018-04-09 21:12:31 +0100379{
David Howells5d9de252018-05-18 11:46:15 +0100380 struct afs_net *net = afs_seq2net(m);
381 struct afs_sysnames *sysnames = net->sysnames;
382 unsigned int i = (unsigned long)v - 1;
David Howells6f8880d2018-04-09 21:12:31 +0100383
David Howells5d9de252018-05-18 11:46:15 +0100384 if (i < sysnames->nr)
385 seq_printf(m, "%s\n", sysnames->subs[i]);
David Howells6f8880d2018-04-09 21:12:31 +0100386 return 0;
387}
388
David Howells5d9de252018-05-18 11:46:15 +0100389static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
390 __acquires(&net->sysnames_lock)
391{
392 struct afs_net *net = afs_seq2net(m);
David Howells5b86d4f2018-05-18 11:46:15 +0100393 struct afs_sysnames *names;
David Howells5d9de252018-05-18 11:46:15 +0100394
395 read_lock(&net->sysnames_lock);
396
David Howells5b86d4f2018-05-18 11:46:15 +0100397 names = net->sysnames;
David Howells5d9de252018-05-18 11:46:15 +0100398 if (*pos >= names->nr)
399 return NULL;
400 return (void *)(unsigned long)(*pos + 1);
401}
402
403static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
404{
405 struct afs_net *net = afs_seq2net(m);
406 struct afs_sysnames *names = net->sysnames;
407
408 *pos += 1;
409 if (*pos >= names->nr)
410 return NULL;
411 return (void *)(unsigned long)(*pos + 1);
412}
413
414static void afs_proc_sysname_stop(struct seq_file *m, void *v)
415 __releases(&net->sysnames_lock)
416{
417 struct afs_net *net = afs_seq2net(m);
418
419 read_unlock(&net->sysnames_lock);
420}
421
422static const struct seq_operations afs_proc_sysname_ops = {
423 .start = afs_proc_sysname_start,
424 .next = afs_proc_sysname_next,
425 .stop = afs_proc_sysname_stop,
426 .show = afs_proc_sysname_show,
427};
428
David Howells6f8880d2018-04-09 21:12:31 +0100429/*
David Howells5d9de252018-05-18 11:46:15 +0100430 * Allow the @sys substitution to be configured.
David Howells6f8880d2018-04-09 21:12:31 +0100431 */
David Howells5b86d4f2018-05-18 11:46:15 +0100432static int afs_proc_sysname_write(struct file *file, char *buf, size_t size)
David Howells6f8880d2018-04-09 21:12:31 +0100433{
David Howells5b86d4f2018-05-18 11:46:15 +0100434 struct afs_sysnames *sysnames, *kill;
David Howells6f8880d2018-04-09 21:12:31 +0100435 struct seq_file *m = file->private_data;
David Howells5b86d4f2018-05-18 11:46:15 +0100436 struct afs_net *net = afs_seq2net(m);
437 char *s, *p, *sub;
David Howells6f8880d2018-04-09 21:12:31 +0100438 int ret, len;
439
David Howells5b86d4f2018-05-18 11:46:15 +0100440 sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
David Howells6f8880d2018-04-09 21:12:31 +0100441 if (!sysnames)
David Howells5b86d4f2018-05-18 11:46:15 +0100442 return -ENOMEM;
443 refcount_set(&sysnames->usage, 1);
444 kill = sysnames;
David Howells6f8880d2018-04-09 21:12:31 +0100445
David Howells5b86d4f2018-05-18 11:46:15 +0100446 p = buf;
David Howells6f8880d2018-04-09 21:12:31 +0100447 while ((s = strsep(&p, " \t\n"))) {
448 len = strlen(s);
449 if (len == 0)
450 continue;
451 ret = -ENAMETOOLONG;
452 if (len >= AFSNAMEMAX)
453 goto error;
454
455 if (len >= 4 &&
456 s[len - 4] == '@' &&
457 s[len - 3] == 's' &&
458 s[len - 2] == 'y' &&
459 s[len - 1] == 's')
460 /* Protect against recursion */
461 goto invalid;
462
463 if (s[0] == '.' &&
464 (len < 2 || (len == 2 && s[1] == '.')))
465 goto invalid;
466
467 if (memchr(s, '/', len))
468 goto invalid;
469
470 ret = -EFBIG;
471 if (sysnames->nr >= AFS_NR_SYSNAME)
472 goto out;
473
474 if (strcmp(s, afs_init_sysname) == 0) {
475 sub = (char *)afs_init_sysname;
476 } else {
477 ret = -ENOMEM;
478 sub = kmemdup(s, len + 1, GFP_KERNEL);
479 if (!sub)
480 goto out;
481 }
482
483 sysnames->subs[sysnames->nr] = sub;
484 sysnames->nr++;
485 }
486
David Howells5b86d4f2018-05-18 11:46:15 +0100487 if (sysnames->nr == 0) {
488 sysnames->subs[0] = sysnames->blank;
489 sysnames->nr++;
490 }
491
492 write_lock(&net->sysnames_lock);
493 kill = net->sysnames;
494 net->sysnames = sysnames;
495 write_unlock(&net->sysnames_lock);
496 ret = 0;
David Howells6f8880d2018-04-09 21:12:31 +0100497out:
David Howells5b86d4f2018-05-18 11:46:15 +0100498 afs_put_sysnames(kill);
David Howells6f8880d2018-04-09 21:12:31 +0100499 return ret;
500
501invalid:
502 ret = -EINVAL;
503error:
David Howells6f8880d2018-04-09 21:12:31 +0100504 goto out;
505}
506
David Howells5d9de252018-05-18 11:46:15 +0100507void afs_put_sysnames(struct afs_sysnames *sysnames)
508{
509 int i;
510
511 if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
512 for (i = 0; i < sysnames->nr; i++)
513 if (sysnames->subs[i] != afs_init_sysname &&
514 sysnames->subs[i] != sysnames->blank)
515 kfree(sysnames->subs[i]);
516 }
517}
518
David Howellsd55b4da2018-04-06 14:17:24 +0100519/*
520 * Display general per-net namespace statistics
521 */
522static int afs_proc_stats_show(struct seq_file *m, void *v)
523{
David Howells5b86d4f2018-05-18 11:46:15 +0100524 struct afs_net *net = afs_seq2net_single(m);
David Howellsd55b4da2018-04-06 14:17:24 +0100525
526 seq_puts(m, "kAFS statistics\n");
527
David Howellsf3ddee82018-04-06 14:17:25 +0100528 seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
David Howellsd55b4da2018-04-06 14:17:24 +0100529 atomic_read(&net->n_lookup),
530 atomic_read(&net->n_reval),
David Howellsf3ddee82018-04-06 14:17:25 +0100531 atomic_read(&net->n_inval),
532 atomic_read(&net->n_relpg));
David Howellsd55b4da2018-04-06 14:17:24 +0100533
534 seq_printf(m, "dir-data: rdpg=%u\n",
535 atomic_read(&net->n_read_dir));
David Howells63a46812018-04-06 14:17:25 +0100536
537 seq_printf(m, "dir-edit: cr=%u rm=%u\n",
538 atomic_read(&net->n_dir_cr),
539 atomic_read(&net->n_dir_rm));
David Howells76a5cb62018-04-06 14:17:26 +0100540
541 seq_printf(m, "file-rd : n=%u nb=%lu\n",
542 atomic_read(&net->n_fetches),
543 atomic_long_read(&net->n_fetch_bytes));
544 seq_printf(m, "file-wr : n=%u nb=%lu\n",
545 atomic_read(&net->n_stores),
546 atomic_long_read(&net->n_store_bytes));
David Howellsd55b4da2018-04-06 14:17:24 +0100547 return 0;
548}
David Howells10495a02018-05-18 11:46:14 +0100549
550/*
551 * initialise /proc/fs/afs/<cell>/
552 */
David Howells5b86d4f2018-05-18 11:46:15 +0100553int afs_proc_cell_setup(struct afs_cell *cell)
David Howells10495a02018-05-18 11:46:14 +0100554{
555 struct proc_dir_entry *dir;
David Howells5b86d4f2018-05-18 11:46:15 +0100556 struct afs_net *net = cell->net;
David Howells10495a02018-05-18 11:46:14 +0100557
558 _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
559
David Howells5b86d4f2018-05-18 11:46:15 +0100560 dir = proc_net_mkdir(net->net, cell->name, net->proc_afs);
David Howells10495a02018-05-18 11:46:14 +0100561 if (!dir)
562 goto error_dir;
563
David Howells5b86d4f2018-05-18 11:46:15 +0100564 if (!proc_create_net_data("vlservers", 0444, dir,
565 &afs_proc_cell_vlservers_ops,
566 sizeof(struct seq_net_private),
567 cell) ||
568 !proc_create_net_data("volumes", 0444, dir,
569 &afs_proc_cell_volumes_ops,
570 sizeof(struct seq_net_private),
571 cell))
David Howells10495a02018-05-18 11:46:14 +0100572 goto error_tree;
573
574 _leave(" = 0");
575 return 0;
576
577error_tree:
578 remove_proc_subtree(cell->name, net->proc_afs);
579error_dir:
580 _leave(" = -ENOMEM");
581 return -ENOMEM;
582}
583
584/*
585 * remove /proc/fs/afs/<cell>/
586 */
David Howells5b86d4f2018-05-18 11:46:15 +0100587void afs_proc_cell_remove(struct afs_cell *cell)
David Howells10495a02018-05-18 11:46:14 +0100588{
David Howells5b86d4f2018-05-18 11:46:15 +0100589 struct afs_net *net = cell->net;
590
David Howells10495a02018-05-18 11:46:14 +0100591 _enter("");
David Howells10495a02018-05-18 11:46:14 +0100592 remove_proc_subtree(cell->name, net->proc_afs);
David Howells10495a02018-05-18 11:46:14 +0100593 _leave("");
594}
595
596/*
597 * initialise the /proc/fs/afs/ directory
598 */
599int afs_proc_init(struct afs_net *net)
600{
David Howells5b86d4f2018-05-18 11:46:15 +0100601 struct proc_dir_entry *p;
602
David Howells10495a02018-05-18 11:46:14 +0100603 _enter("");
604
David Howells5b86d4f2018-05-18 11:46:15 +0100605 p = proc_net_mkdir(net->net, "afs", net->net->proc_net);
606 if (!p)
David Howells10495a02018-05-18 11:46:14 +0100607 goto error_dir;
608
David Howells5b86d4f2018-05-18 11:46:15 +0100609 if (!proc_create_net_data_write("cells", 0644, p,
610 &afs_proc_cells_ops,
611 afs_proc_cells_write,
612 sizeof(struct seq_net_private),
613 NULL) ||
614 !proc_create_net_single_write("rootcell", 0644, p,
615 afs_proc_rootcell_show,
616 afs_proc_rootcell_write,
617 NULL) ||
618 !proc_create_net("servers", 0444, p, &afs_proc_servers_ops,
619 sizeof(struct seq_net_private)) ||
620 !proc_create_net_single("stats", 0444, p, afs_proc_stats_show, NULL) ||
621 !proc_create_net_data_write("sysname", 0644, p,
622 &afs_proc_sysname_ops,
623 afs_proc_sysname_write,
624 sizeof(struct seq_net_private),
625 NULL))
David Howells10495a02018-05-18 11:46:14 +0100626 goto error_tree;
627
David Howells5b86d4f2018-05-18 11:46:15 +0100628 net->proc_afs = p;
David Howells10495a02018-05-18 11:46:14 +0100629 _leave(" = 0");
630 return 0;
631
632error_tree:
David Howells5b86d4f2018-05-18 11:46:15 +0100633 proc_remove(p);
David Howells10495a02018-05-18 11:46:14 +0100634error_dir:
635 _leave(" = -ENOMEM");
636 return -ENOMEM;
637}
638
639/*
640 * clean up the /proc/fs/afs/ directory
641 */
642void afs_proc_cleanup(struct afs_net *net)
643{
644 proc_remove(net->proc_afs);
645 net->proc_afs = NULL;
646}