proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /* /proc interface for AFS
  2. *
  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. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/sched.h>
  16. #include <linux/uaccess.h>
  17. #include "internal.h"
  18. static inline struct afs_net *afs_seq2net(struct seq_file *m)
  19. {
  20. return afs_net(seq_file_net(m));
  21. }
  22. static inline struct afs_net *afs_seq2net_single(struct seq_file *m)
  23. {
  24. return afs_net(seq_file_single_net(m));
  25. }
  26. /*
  27. * Display the list of cells known to the namespace.
  28. */
  29. static int afs_proc_cells_show(struct seq_file *m, void *v)
  30. {
  31. struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
  32. struct afs_net *net = afs_seq2net(m);
  33. if (v == &net->proc_cells) {
  34. /* display header on line 1 */
  35. seq_puts(m, "USE NAME\n");
  36. return 0;
  37. }
  38. /* display one cell per line on subsequent lines */
  39. seq_printf(m, "%3u %s\n", atomic_read(&cell->usage), cell->name);
  40. return 0;
  41. }
  42. static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
  43. __acquires(rcu)
  44. {
  45. rcu_read_lock();
  46. return seq_list_start_head(&afs_seq2net(m)->proc_cells, *_pos);
  47. }
  48. static void *afs_proc_cells_next(struct seq_file *m, void *v, loff_t *pos)
  49. {
  50. return seq_list_next(v, &afs_seq2net(m)->proc_cells, pos);
  51. }
  52. static void afs_proc_cells_stop(struct seq_file *m, void *v)
  53. __releases(rcu)
  54. {
  55. rcu_read_unlock();
  56. }
  57. static const struct seq_operations afs_proc_cells_ops = {
  58. .start = afs_proc_cells_start,
  59. .next = afs_proc_cells_next,
  60. .stop = afs_proc_cells_stop,
  61. .show = afs_proc_cells_show,
  62. };
  63. /*
  64. * handle writes to /proc/fs/afs/cells
  65. * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
  66. */
  67. static int afs_proc_cells_write(struct file *file, char *buf, size_t size)
  68. {
  69. struct seq_file *m = file->private_data;
  70. struct afs_net *net = afs_seq2net(m);
  71. char *name, *args;
  72. int ret;
  73. /* trim to first NL */
  74. name = memchr(buf, '\n', size);
  75. if (name)
  76. *name = 0;
  77. /* split into command, name and argslist */
  78. name = strchr(buf, ' ');
  79. if (!name)
  80. goto inval;
  81. do {
  82. *name++ = 0;
  83. } while(*name == ' ');
  84. if (!*name)
  85. goto inval;
  86. args = strchr(name, ' ');
  87. if (!args)
  88. goto inval;
  89. do {
  90. *args++ = 0;
  91. } while(*args == ' ');
  92. if (!*args)
  93. goto inval;
  94. /* determine command to perform */
  95. _debug("cmd=%s name=%s args=%s", buf, name, args);
  96. if (strcmp(buf, "add") == 0) {
  97. struct afs_cell *cell;
  98. cell = afs_lookup_cell(net, name, strlen(name), args, true);
  99. if (IS_ERR(cell)) {
  100. ret = PTR_ERR(cell);
  101. goto done;
  102. }
  103. if (test_and_set_bit(AFS_CELL_FL_NO_GC, &cell->flags))
  104. afs_put_cell(net, cell);
  105. printk("kAFS: Added new cell '%s'\n", name);
  106. } else {
  107. goto inval;
  108. }
  109. ret = 0;
  110. done:
  111. _leave(" = %d", ret);
  112. return ret;
  113. inval:
  114. ret = -EINVAL;
  115. printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
  116. goto done;
  117. }
  118. /*
  119. * Display the name of the current workstation cell.
  120. */
  121. static int afs_proc_rootcell_show(struct seq_file *m, void *v)
  122. {
  123. struct afs_cell *cell;
  124. struct afs_net *net;
  125. net = afs_seq2net_single(m);
  126. if (rcu_access_pointer(net->ws_cell)) {
  127. rcu_read_lock();
  128. cell = rcu_dereference(net->ws_cell);
  129. if (cell)
  130. seq_printf(m, "%s\n", cell->name);
  131. rcu_read_unlock();
  132. }
  133. return 0;
  134. }
  135. /*
  136. * Set the current workstation cell and optionally supply its list of volume
  137. * location servers.
  138. *
  139. * echo "cell.name:192.168.231.14" >/proc/fs/afs/rootcell
  140. */
  141. static int afs_proc_rootcell_write(struct file *file, char *buf, size_t size)
  142. {
  143. struct seq_file *m = file->private_data;
  144. struct afs_net *net = afs_seq2net_single(m);
  145. char *s;
  146. int ret;
  147. ret = -EINVAL;
  148. if (buf[0] == '.')
  149. goto out;
  150. if (memchr(buf, '/', size))
  151. goto out;
  152. /* trim to first NL */
  153. s = memchr(buf, '\n', size);
  154. if (s)
  155. *s = 0;
  156. /* determine command to perform */
  157. _debug("rootcell=%s", buf);
  158. ret = afs_cell_init(net, buf);
  159. out:
  160. _leave(" = %d", ret);
  161. return ret;
  162. }
  163. static const char afs_vol_types[3][3] = {
  164. [AFSVL_RWVOL] = "RW",
  165. [AFSVL_ROVOL] = "RO",
  166. [AFSVL_BACKVOL] = "BK",
  167. };
  168. /*
  169. * Display the list of volumes known to a cell.
  170. */
  171. static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
  172. {
  173. struct afs_cell *cell = PDE_DATA(file_inode(m->file));
  174. struct afs_volume *vol = list_entry(v, struct afs_volume, proc_link);
  175. /* Display header on line 1 */
  176. if (v == &cell->proc_volumes) {
  177. seq_puts(m, "USE VID TY\n");
  178. return 0;
  179. }
  180. seq_printf(m, "%3d %08x %s\n",
  181. atomic_read(&vol->usage), vol->vid,
  182. afs_vol_types[vol->type]);
  183. return 0;
  184. }
  185. static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
  186. __acquires(cell->proc_lock)
  187. {
  188. struct afs_cell *cell = PDE_DATA(file_inode(m->file));
  189. read_lock(&cell->proc_lock);
  190. return seq_list_start_head(&cell->proc_volumes, *_pos);
  191. }
  192. static void *afs_proc_cell_volumes_next(struct seq_file *m, void *v,
  193. loff_t *_pos)
  194. {
  195. struct afs_cell *cell = PDE_DATA(file_inode(m->file));
  196. return seq_list_next(v, &cell->proc_volumes, _pos);
  197. }
  198. static void afs_proc_cell_volumes_stop(struct seq_file *m, void *v)
  199. __releases(cell->proc_lock)
  200. {
  201. struct afs_cell *cell = PDE_DATA(file_inode(m->file));
  202. read_unlock(&cell->proc_lock);
  203. }
  204. static const struct seq_operations afs_proc_cell_volumes_ops = {
  205. .start = afs_proc_cell_volumes_start,
  206. .next = afs_proc_cell_volumes_next,
  207. .stop = afs_proc_cell_volumes_stop,
  208. .show = afs_proc_cell_volumes_show,
  209. };
  210. /*
  211. * Display the list of Volume Location servers we're using for a cell.
  212. */
  213. static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
  214. {
  215. struct sockaddr_rxrpc *addr = v;
  216. /* display header on line 1 */
  217. if (v == (void *)1) {
  218. seq_puts(m, "ADDRESS\n");
  219. return 0;
  220. }
  221. /* display one cell per line on subsequent lines */
  222. seq_printf(m, "%pISp\n", &addr->transport);
  223. return 0;
  224. }
  225. static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
  226. __acquires(rcu)
  227. {
  228. struct afs_addr_list *alist;
  229. struct afs_cell *cell = PDE_DATA(file_inode(m->file));
  230. loff_t pos = *_pos;
  231. rcu_read_lock();
  232. alist = rcu_dereference(cell->vl_addrs);
  233. /* allow for the header line */
  234. if (!pos)
  235. return (void *) 1;
  236. pos--;
  237. if (!alist || pos >= alist->nr_addrs)
  238. return NULL;
  239. return alist->addrs + pos;
  240. }
  241. static void *afs_proc_cell_vlservers_next(struct seq_file *m, void *v,
  242. loff_t *_pos)
  243. {
  244. struct afs_addr_list *alist;
  245. struct afs_cell *cell = PDE_DATA(file_inode(m->file));
  246. loff_t pos;
  247. alist = rcu_dereference(cell->vl_addrs);
  248. pos = *_pos;
  249. (*_pos)++;
  250. if (!alist || pos >= alist->nr_addrs)
  251. return NULL;
  252. return alist->addrs + pos;
  253. }
  254. static void afs_proc_cell_vlservers_stop(struct seq_file *m, void *v)
  255. __releases(rcu)
  256. {
  257. rcu_read_unlock();
  258. }
  259. static const struct seq_operations afs_proc_cell_vlservers_ops = {
  260. .start = afs_proc_cell_vlservers_start,
  261. .next = afs_proc_cell_vlservers_next,
  262. .stop = afs_proc_cell_vlservers_stop,
  263. .show = afs_proc_cell_vlservers_show,
  264. };
  265. /*
  266. * Display the list of fileservers we're using within a namespace.
  267. */
  268. static int afs_proc_servers_show(struct seq_file *m, void *v)
  269. {
  270. struct afs_server *server;
  271. struct afs_addr_list *alist;
  272. int i;
  273. if (v == SEQ_START_TOKEN) {
  274. seq_puts(m, "UUID USE ADDR\n");
  275. return 0;
  276. }
  277. server = list_entry(v, struct afs_server, proc_link);
  278. alist = rcu_dereference(server->addresses);
  279. seq_printf(m, "%pU %3d %pISpc%s\n",
  280. &server->uuid,
  281. atomic_read(&server->usage),
  282. &alist->addrs[0].transport,
  283. alist->index == 0 ? "*" : "");
  284. for (i = 1; i < alist->nr_addrs; i++)
  285. seq_printf(m, " %pISpc%s\n",
  286. &alist->addrs[i].transport,
  287. alist->index == i ? "*" : "");
  288. return 0;
  289. }
  290. static void *afs_proc_servers_start(struct seq_file *m, loff_t *_pos)
  291. __acquires(rcu)
  292. {
  293. rcu_read_lock();
  294. return seq_hlist_start_head_rcu(&afs_seq2net(m)->fs_proc, *_pos);
  295. }
  296. static void *afs_proc_servers_next(struct seq_file *m, void *v, loff_t *_pos)
  297. {
  298. return seq_hlist_next_rcu(v, &afs_seq2net(m)->fs_proc, _pos);
  299. }
  300. static void afs_proc_servers_stop(struct seq_file *m, void *v)
  301. __releases(rcu)
  302. {
  303. rcu_read_unlock();
  304. }
  305. static const struct seq_operations afs_proc_servers_ops = {
  306. .start = afs_proc_servers_start,
  307. .next = afs_proc_servers_next,
  308. .stop = afs_proc_servers_stop,
  309. .show = afs_proc_servers_show,
  310. };
  311. /*
  312. * Display the list of strings that may be substituted for the @sys pathname
  313. * macro.
  314. */
  315. static int afs_proc_sysname_show(struct seq_file *m, void *v)
  316. {
  317. struct afs_net *net = afs_seq2net(m);
  318. struct afs_sysnames *sysnames = net->sysnames;
  319. unsigned int i = (unsigned long)v - 1;
  320. if (i < sysnames->nr)
  321. seq_printf(m, "%s\n", sysnames->subs[i]);
  322. return 0;
  323. }
  324. static void *afs_proc_sysname_start(struct seq_file *m, loff_t *pos)
  325. __acquires(&net->sysnames_lock)
  326. {
  327. struct afs_net *net = afs_seq2net(m);
  328. struct afs_sysnames *names;
  329. read_lock(&net->sysnames_lock);
  330. names = net->sysnames;
  331. if (*pos >= names->nr)
  332. return NULL;
  333. return (void *)(unsigned long)(*pos + 1);
  334. }
  335. static void *afs_proc_sysname_next(struct seq_file *m, void *v, loff_t *pos)
  336. {
  337. struct afs_net *net = afs_seq2net(m);
  338. struct afs_sysnames *names = net->sysnames;
  339. *pos += 1;
  340. if (*pos >= names->nr)
  341. return NULL;
  342. return (void *)(unsigned long)(*pos + 1);
  343. }
  344. static void afs_proc_sysname_stop(struct seq_file *m, void *v)
  345. __releases(&net->sysnames_lock)
  346. {
  347. struct afs_net *net = afs_seq2net(m);
  348. read_unlock(&net->sysnames_lock);
  349. }
  350. static const struct seq_operations afs_proc_sysname_ops = {
  351. .start = afs_proc_sysname_start,
  352. .next = afs_proc_sysname_next,
  353. .stop = afs_proc_sysname_stop,
  354. .show = afs_proc_sysname_show,
  355. };
  356. /*
  357. * Allow the @sys substitution to be configured.
  358. */
  359. static int afs_proc_sysname_write(struct file *file, char *buf, size_t size)
  360. {
  361. struct afs_sysnames *sysnames, *kill;
  362. struct seq_file *m = file->private_data;
  363. struct afs_net *net = afs_seq2net(m);
  364. char *s, *p, *sub;
  365. int ret, len;
  366. sysnames = kzalloc(sizeof(*sysnames), GFP_KERNEL);
  367. if (!sysnames)
  368. return -ENOMEM;
  369. refcount_set(&sysnames->usage, 1);
  370. kill = sysnames;
  371. p = buf;
  372. while ((s = strsep(&p, " \t\n"))) {
  373. len = strlen(s);
  374. if (len == 0)
  375. continue;
  376. ret = -ENAMETOOLONG;
  377. if (len >= AFSNAMEMAX)
  378. goto error;
  379. if (len >= 4 &&
  380. s[len - 4] == '@' &&
  381. s[len - 3] == 's' &&
  382. s[len - 2] == 'y' &&
  383. s[len - 1] == 's')
  384. /* Protect against recursion */
  385. goto invalid;
  386. if (s[0] == '.' &&
  387. (len < 2 || (len == 2 && s[1] == '.')))
  388. goto invalid;
  389. if (memchr(s, '/', len))
  390. goto invalid;
  391. ret = -EFBIG;
  392. if (sysnames->nr >= AFS_NR_SYSNAME)
  393. goto out;
  394. if (strcmp(s, afs_init_sysname) == 0) {
  395. sub = (char *)afs_init_sysname;
  396. } else {
  397. ret = -ENOMEM;
  398. sub = kmemdup(s, len + 1, GFP_KERNEL);
  399. if (!sub)
  400. goto out;
  401. }
  402. sysnames->subs[sysnames->nr] = sub;
  403. sysnames->nr++;
  404. }
  405. if (sysnames->nr == 0) {
  406. sysnames->subs[0] = sysnames->blank;
  407. sysnames->nr++;
  408. }
  409. write_lock(&net->sysnames_lock);
  410. kill = net->sysnames;
  411. net->sysnames = sysnames;
  412. write_unlock(&net->sysnames_lock);
  413. ret = 0;
  414. out:
  415. afs_put_sysnames(kill);
  416. return ret;
  417. invalid:
  418. ret = -EINVAL;
  419. error:
  420. goto out;
  421. }
  422. void afs_put_sysnames(struct afs_sysnames *sysnames)
  423. {
  424. int i;
  425. if (sysnames && refcount_dec_and_test(&sysnames->usage)) {
  426. for (i = 0; i < sysnames->nr; i++)
  427. if (sysnames->subs[i] != afs_init_sysname &&
  428. sysnames->subs[i] != sysnames->blank)
  429. kfree(sysnames->subs[i]);
  430. }
  431. }
  432. /*
  433. * Display general per-net namespace statistics
  434. */
  435. static int afs_proc_stats_show(struct seq_file *m, void *v)
  436. {
  437. struct afs_net *net = afs_seq2net_single(m);
  438. seq_puts(m, "kAFS statistics\n");
  439. seq_printf(m, "dir-mgmt: look=%u reval=%u inval=%u relpg=%u\n",
  440. atomic_read(&net->n_lookup),
  441. atomic_read(&net->n_reval),
  442. atomic_read(&net->n_inval),
  443. atomic_read(&net->n_relpg));
  444. seq_printf(m, "dir-data: rdpg=%u\n",
  445. atomic_read(&net->n_read_dir));
  446. seq_printf(m, "dir-edit: cr=%u rm=%u\n",
  447. atomic_read(&net->n_dir_cr),
  448. atomic_read(&net->n_dir_rm));
  449. seq_printf(m, "file-rd : n=%u nb=%lu\n",
  450. atomic_read(&net->n_fetches),
  451. atomic_long_read(&net->n_fetch_bytes));
  452. seq_printf(m, "file-wr : n=%u nb=%lu\n",
  453. atomic_read(&net->n_stores),
  454. atomic_long_read(&net->n_store_bytes));
  455. return 0;
  456. }
  457. /*
  458. * initialise /proc/fs/afs/<cell>/
  459. */
  460. int afs_proc_cell_setup(struct afs_cell *cell)
  461. {
  462. struct proc_dir_entry *dir;
  463. struct afs_net *net = cell->net;
  464. _enter("%p{%s},%p", cell, cell->name, net->proc_afs);
  465. dir = proc_net_mkdir(net->net, cell->name, net->proc_afs);
  466. if (!dir)
  467. goto error_dir;
  468. if (!proc_create_net_data("vlservers", 0444, dir,
  469. &afs_proc_cell_vlservers_ops,
  470. sizeof(struct seq_net_private),
  471. cell) ||
  472. !proc_create_net_data("volumes", 0444, dir,
  473. &afs_proc_cell_volumes_ops,
  474. sizeof(struct seq_net_private),
  475. cell))
  476. goto error_tree;
  477. _leave(" = 0");
  478. return 0;
  479. error_tree:
  480. remove_proc_subtree(cell->name, net->proc_afs);
  481. error_dir:
  482. _leave(" = -ENOMEM");
  483. return -ENOMEM;
  484. }
  485. /*
  486. * remove /proc/fs/afs/<cell>/
  487. */
  488. void afs_proc_cell_remove(struct afs_cell *cell)
  489. {
  490. struct afs_net *net = cell->net;
  491. _enter("");
  492. remove_proc_subtree(cell->name, net->proc_afs);
  493. _leave("");
  494. }
  495. /*
  496. * initialise the /proc/fs/afs/ directory
  497. */
  498. int afs_proc_init(struct afs_net *net)
  499. {
  500. struct proc_dir_entry *p;
  501. _enter("");
  502. p = proc_net_mkdir(net->net, "afs", net->net->proc_net);
  503. if (!p)
  504. goto error_dir;
  505. if (!proc_create_net_data_write("cells", 0644, p,
  506. &afs_proc_cells_ops,
  507. afs_proc_cells_write,
  508. sizeof(struct seq_net_private),
  509. NULL) ||
  510. !proc_create_net_single_write("rootcell", 0644, p,
  511. afs_proc_rootcell_show,
  512. afs_proc_rootcell_write,
  513. NULL) ||
  514. !proc_create_net("servers", 0444, p, &afs_proc_servers_ops,
  515. sizeof(struct seq_net_private)) ||
  516. !proc_create_net_single("stats", 0444, p, afs_proc_stats_show, NULL) ||
  517. !proc_create_net_data_write("sysname", 0644, p,
  518. &afs_proc_sysname_ops,
  519. afs_proc_sysname_write,
  520. sizeof(struct seq_net_private),
  521. NULL))
  522. goto error_tree;
  523. net->proc_afs = p;
  524. _leave(" = 0");
  525. return 0;
  526. error_tree:
  527. proc_remove(p);
  528. error_dir:
  529. _leave(" = -ENOMEM");
  530. return -ENOMEM;
  531. }
  532. /*
  533. * clean up the /proc/fs/afs/ directory
  534. */
  535. void afs_proc_cleanup(struct afs_net *net)
  536. {
  537. proc_remove(net->proc_afs);
  538. net->proc_afs = NULL;
  539. }