proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 <asm/uaccess.h>
  17. #include "internal.h"
  18. static struct proc_dir_entry *proc_afs;
  19. static int afs_proc_cells_open(struct inode *inode, struct file *file);
  20. static void *afs_proc_cells_start(struct seq_file *p, loff_t *pos);
  21. static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos);
  22. static void afs_proc_cells_stop(struct seq_file *p, void *v);
  23. static int afs_proc_cells_show(struct seq_file *m, void *v);
  24. static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
  25. size_t size, loff_t *_pos);
  26. static const struct seq_operations afs_proc_cells_ops = {
  27. .start = afs_proc_cells_start,
  28. .next = afs_proc_cells_next,
  29. .stop = afs_proc_cells_stop,
  30. .show = afs_proc_cells_show,
  31. };
  32. static const struct file_operations afs_proc_cells_fops = {
  33. .open = afs_proc_cells_open,
  34. .read = seq_read,
  35. .write = afs_proc_cells_write,
  36. .llseek = seq_lseek,
  37. .release = seq_release,
  38. };
  39. static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
  40. size_t size, loff_t *_pos);
  41. static ssize_t afs_proc_rootcell_write(struct file *file,
  42. const char __user *buf,
  43. size_t size, loff_t *_pos);
  44. static const struct file_operations afs_proc_rootcell_fops = {
  45. .read = afs_proc_rootcell_read,
  46. .write = afs_proc_rootcell_write,
  47. .llseek = no_llseek,
  48. };
  49. static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file);
  50. static void *afs_proc_cell_volumes_start(struct seq_file *p, loff_t *pos);
  51. static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
  52. loff_t *pos);
  53. static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v);
  54. static int afs_proc_cell_volumes_show(struct seq_file *m, void *v);
  55. static const struct seq_operations afs_proc_cell_volumes_ops = {
  56. .start = afs_proc_cell_volumes_start,
  57. .next = afs_proc_cell_volumes_next,
  58. .stop = afs_proc_cell_volumes_stop,
  59. .show = afs_proc_cell_volumes_show,
  60. };
  61. static const struct file_operations afs_proc_cell_volumes_fops = {
  62. .open = afs_proc_cell_volumes_open,
  63. .read = seq_read,
  64. .llseek = seq_lseek,
  65. .release = seq_release,
  66. };
  67. static int afs_proc_cell_vlservers_open(struct inode *inode,
  68. struct file *file);
  69. static void *afs_proc_cell_vlservers_start(struct seq_file *p, loff_t *pos);
  70. static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
  71. loff_t *pos);
  72. static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v);
  73. static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v);
  74. static const struct seq_operations afs_proc_cell_vlservers_ops = {
  75. .start = afs_proc_cell_vlservers_start,
  76. .next = afs_proc_cell_vlservers_next,
  77. .stop = afs_proc_cell_vlservers_stop,
  78. .show = afs_proc_cell_vlservers_show,
  79. };
  80. static const struct file_operations afs_proc_cell_vlservers_fops = {
  81. .open = afs_proc_cell_vlservers_open,
  82. .read = seq_read,
  83. .llseek = seq_lseek,
  84. .release = seq_release,
  85. };
  86. static int afs_proc_cell_servers_open(struct inode *inode, struct file *file);
  87. static void *afs_proc_cell_servers_start(struct seq_file *p, loff_t *pos);
  88. static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
  89. loff_t *pos);
  90. static void afs_proc_cell_servers_stop(struct seq_file *p, void *v);
  91. static int afs_proc_cell_servers_show(struct seq_file *m, void *v);
  92. static const struct seq_operations afs_proc_cell_servers_ops = {
  93. .start = afs_proc_cell_servers_start,
  94. .next = afs_proc_cell_servers_next,
  95. .stop = afs_proc_cell_servers_stop,
  96. .show = afs_proc_cell_servers_show,
  97. };
  98. static const struct file_operations afs_proc_cell_servers_fops = {
  99. .open = afs_proc_cell_servers_open,
  100. .read = seq_read,
  101. .llseek = seq_lseek,
  102. .release = seq_release,
  103. };
  104. /*
  105. * initialise the /proc/fs/afs/ directory
  106. */
  107. int afs_proc_init(void)
  108. {
  109. _enter("");
  110. proc_afs = proc_mkdir("fs/afs", NULL);
  111. if (!proc_afs)
  112. goto error_dir;
  113. if (!proc_create("cells", 0644, proc_afs, &afs_proc_cells_fops) ||
  114. !proc_create("rootcell", 0644, proc_afs, &afs_proc_rootcell_fops))
  115. goto error_tree;
  116. _leave(" = 0");
  117. return 0;
  118. error_tree:
  119. remove_proc_subtree("fs/afs", NULL);
  120. error_dir:
  121. _leave(" = -ENOMEM");
  122. return -ENOMEM;
  123. }
  124. /*
  125. * clean up the /proc/fs/afs/ directory
  126. */
  127. void afs_proc_cleanup(void)
  128. {
  129. remove_proc_subtree("fs/afs", NULL);
  130. }
  131. /*
  132. * open "/proc/fs/afs/cells" which provides a summary of extant cells
  133. */
  134. static int afs_proc_cells_open(struct inode *inode, struct file *file)
  135. {
  136. struct seq_file *m;
  137. int ret;
  138. ret = seq_open(file, &afs_proc_cells_ops);
  139. if (ret < 0)
  140. return ret;
  141. m = file->private_data;
  142. m->private = PDE_DATA(inode);
  143. return 0;
  144. }
  145. /*
  146. * set up the iterator to start reading from the cells list and return the
  147. * first item
  148. */
  149. static void *afs_proc_cells_start(struct seq_file *m, loff_t *_pos)
  150. {
  151. /* lock the list against modification */
  152. down_read(&afs_proc_cells_sem);
  153. return seq_list_start_head(&afs_proc_cells, *_pos);
  154. }
  155. /*
  156. * move to next cell in cells list
  157. */
  158. static void *afs_proc_cells_next(struct seq_file *p, void *v, loff_t *pos)
  159. {
  160. return seq_list_next(v, &afs_proc_cells, pos);
  161. }
  162. /*
  163. * clean up after reading from the cells list
  164. */
  165. static void afs_proc_cells_stop(struct seq_file *p, void *v)
  166. {
  167. up_read(&afs_proc_cells_sem);
  168. }
  169. /*
  170. * display a header line followed by a load of cell lines
  171. */
  172. static int afs_proc_cells_show(struct seq_file *m, void *v)
  173. {
  174. struct afs_cell *cell = list_entry(v, struct afs_cell, proc_link);
  175. if (v == &afs_proc_cells) {
  176. /* display header on line 1 */
  177. seq_puts(m, "USE NAME\n");
  178. return 0;
  179. }
  180. /* display one cell per line on subsequent lines */
  181. seq_printf(m, "%3d %s\n",
  182. atomic_read(&cell->usage), cell->name);
  183. return 0;
  184. }
  185. /*
  186. * handle writes to /proc/fs/afs/cells
  187. * - to add cells: echo "add <cellname> <IP>[:<IP>][:<IP>]"
  188. */
  189. static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
  190. size_t size, loff_t *_pos)
  191. {
  192. char *kbuf, *name, *args;
  193. int ret;
  194. /* start by dragging the command into memory */
  195. if (size <= 1 || size >= PAGE_SIZE)
  196. return -EINVAL;
  197. kbuf = memdup_user_nul(buf, size);
  198. if (IS_ERR(kbuf))
  199. return PTR_ERR(kbuf);
  200. /* trim to first NL */
  201. name = memchr(kbuf, '\n', size);
  202. if (name)
  203. *name = 0;
  204. /* split into command, name and argslist */
  205. name = strchr(kbuf, ' ');
  206. if (!name)
  207. goto inval;
  208. do {
  209. *name++ = 0;
  210. } while(*name == ' ');
  211. if (!*name)
  212. goto inval;
  213. args = strchr(name, ' ');
  214. if (!args)
  215. goto inval;
  216. do {
  217. *args++ = 0;
  218. } while(*args == ' ');
  219. if (!*args)
  220. goto inval;
  221. /* determine command to perform */
  222. _debug("cmd=%s name=%s args=%s", kbuf, name, args);
  223. if (strcmp(kbuf, "add") == 0) {
  224. struct afs_cell *cell;
  225. cell = afs_cell_create(name, strlen(name), args, false);
  226. if (IS_ERR(cell)) {
  227. ret = PTR_ERR(cell);
  228. goto done;
  229. }
  230. afs_put_cell(cell);
  231. printk("kAFS: Added new cell '%s'\n", name);
  232. } else {
  233. goto inval;
  234. }
  235. ret = size;
  236. done:
  237. kfree(kbuf);
  238. _leave(" = %d", ret);
  239. return ret;
  240. inval:
  241. ret = -EINVAL;
  242. printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
  243. goto done;
  244. }
  245. static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
  246. size_t size, loff_t *_pos)
  247. {
  248. return 0;
  249. }
  250. /*
  251. * handle writes to /proc/fs/afs/rootcell
  252. * - to initialize rootcell: echo "cell.name:192.168.231.14"
  253. */
  254. static ssize_t afs_proc_rootcell_write(struct file *file,
  255. const char __user *buf,
  256. size_t size, loff_t *_pos)
  257. {
  258. char *kbuf, *s;
  259. int ret;
  260. /* start by dragging the command into memory */
  261. if (size <= 1 || size >= PAGE_SIZE)
  262. return -EINVAL;
  263. kbuf = memdup_user_nul(buf, size);
  264. if (IS_ERR(kbuf))
  265. return PTR_ERR(kbuf);
  266. /* trim to first NL */
  267. s = memchr(kbuf, '\n', size);
  268. if (s)
  269. *s = 0;
  270. /* determine command to perform */
  271. _debug("rootcell=%s", kbuf);
  272. ret = afs_cell_init(kbuf);
  273. if (ret >= 0)
  274. ret = size; /* consume everything, always */
  275. kfree(kbuf);
  276. _leave(" = %d", ret);
  277. return ret;
  278. }
  279. /*
  280. * initialise /proc/fs/afs/<cell>/
  281. */
  282. int afs_proc_cell_setup(struct afs_cell *cell)
  283. {
  284. struct proc_dir_entry *dir;
  285. _enter("%p{%s}", cell, cell->name);
  286. dir = proc_mkdir(cell->name, proc_afs);
  287. if (!dir)
  288. goto error_dir;
  289. if (!proc_create_data("servers", 0, dir,
  290. &afs_proc_cell_servers_fops, cell) ||
  291. !proc_create_data("vlservers", 0, dir,
  292. &afs_proc_cell_vlservers_fops, cell) ||
  293. !proc_create_data("volumes", 0, dir,
  294. &afs_proc_cell_volumes_fops, cell))
  295. goto error_tree;
  296. _leave(" = 0");
  297. return 0;
  298. error_tree:
  299. remove_proc_subtree(cell->name, proc_afs);
  300. error_dir:
  301. _leave(" = -ENOMEM");
  302. return -ENOMEM;
  303. }
  304. /*
  305. * remove /proc/fs/afs/<cell>/
  306. */
  307. void afs_proc_cell_remove(struct afs_cell *cell)
  308. {
  309. _enter("");
  310. remove_proc_subtree(cell->name, proc_afs);
  311. _leave("");
  312. }
  313. /*
  314. * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
  315. */
  316. static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
  317. {
  318. struct afs_cell *cell;
  319. struct seq_file *m;
  320. int ret;
  321. cell = PDE_DATA(inode);
  322. if (!cell)
  323. return -ENOENT;
  324. ret = seq_open(file, &afs_proc_cell_volumes_ops);
  325. if (ret < 0)
  326. return ret;
  327. m = file->private_data;
  328. m->private = cell;
  329. return 0;
  330. }
  331. /*
  332. * set up the iterator to start reading from the cells list and return the
  333. * first item
  334. */
  335. static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
  336. {
  337. struct afs_cell *cell = m->private;
  338. _enter("cell=%p pos=%Ld", cell, *_pos);
  339. /* lock the list against modification */
  340. down_read(&cell->vl_sem);
  341. return seq_list_start_head(&cell->vl_list, *_pos);
  342. }
  343. /*
  344. * move to next cell in cells list
  345. */
  346. static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
  347. loff_t *_pos)
  348. {
  349. struct afs_cell *cell = p->private;
  350. _enter("cell=%p pos=%Ld", cell, *_pos);
  351. return seq_list_next(v, &cell->vl_list, _pos);
  352. }
  353. /*
  354. * clean up after reading from the cells list
  355. */
  356. static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
  357. {
  358. struct afs_cell *cell = p->private;
  359. up_read(&cell->vl_sem);
  360. }
  361. static const char afs_vlocation_states[][4] = {
  362. [AFS_VL_NEW] = "New",
  363. [AFS_VL_CREATING] = "Crt",
  364. [AFS_VL_VALID] = "Val",
  365. [AFS_VL_NO_VOLUME] = "NoV",
  366. [AFS_VL_UPDATING] = "Upd",
  367. [AFS_VL_VOLUME_DELETED] = "Del",
  368. [AFS_VL_UNCERTAIN] = "Unc",
  369. };
  370. /*
  371. * display a header line followed by a load of volume lines
  372. */
  373. static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
  374. {
  375. struct afs_cell *cell = m->private;
  376. struct afs_vlocation *vlocation =
  377. list_entry(v, struct afs_vlocation, link);
  378. /* display header on line 1 */
  379. if (v == &cell->vl_list) {
  380. seq_puts(m, "USE STT VLID[0] VLID[1] VLID[2] NAME\n");
  381. return 0;
  382. }
  383. /* display one cell per line on subsequent lines */
  384. seq_printf(m, "%3d %s %08x %08x %08x %s\n",
  385. atomic_read(&vlocation->usage),
  386. afs_vlocation_states[vlocation->state],
  387. vlocation->vldb.vid[0],
  388. vlocation->vldb.vid[1],
  389. vlocation->vldb.vid[2],
  390. vlocation->vldb.name);
  391. return 0;
  392. }
  393. /*
  394. * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
  395. * location server
  396. */
  397. static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
  398. {
  399. struct afs_cell *cell;
  400. struct seq_file *m;
  401. int ret;
  402. cell = PDE_DATA(inode);
  403. if (!cell)
  404. return -ENOENT;
  405. ret = seq_open(file, &afs_proc_cell_vlservers_ops);
  406. if (ret<0)
  407. return ret;
  408. m = file->private_data;
  409. m->private = cell;
  410. return 0;
  411. }
  412. /*
  413. * set up the iterator to start reading from the cells list and return the
  414. * first item
  415. */
  416. static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
  417. {
  418. struct afs_cell *cell = m->private;
  419. loff_t pos = *_pos;
  420. _enter("cell=%p pos=%Ld", cell, *_pos);
  421. /* lock the list against modification */
  422. down_read(&cell->vl_sem);
  423. /* allow for the header line */
  424. if (!pos)
  425. return (void *) 1;
  426. pos--;
  427. if (pos >= cell->vl_naddrs)
  428. return NULL;
  429. return &cell->vl_addrs[pos];
  430. }
  431. /*
  432. * move to next cell in cells list
  433. */
  434. static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
  435. loff_t *_pos)
  436. {
  437. struct afs_cell *cell = p->private;
  438. loff_t pos;
  439. _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
  440. pos = *_pos;
  441. (*_pos)++;
  442. if (pos >= cell->vl_naddrs)
  443. return NULL;
  444. return &cell->vl_addrs[pos];
  445. }
  446. /*
  447. * clean up after reading from the cells list
  448. */
  449. static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
  450. {
  451. struct afs_cell *cell = p->private;
  452. up_read(&cell->vl_sem);
  453. }
  454. /*
  455. * display a header line followed by a load of volume lines
  456. */
  457. static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
  458. {
  459. struct in_addr *addr = v;
  460. /* display header on line 1 */
  461. if (v == (struct in_addr *) 1) {
  462. seq_puts(m, "ADDRESS\n");
  463. return 0;
  464. }
  465. /* display one cell per line on subsequent lines */
  466. seq_printf(m, "%pI4\n", &addr->s_addr);
  467. return 0;
  468. }
  469. /*
  470. * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
  471. * servers
  472. */
  473. static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
  474. {
  475. struct afs_cell *cell;
  476. struct seq_file *m;
  477. int ret;
  478. cell = PDE_DATA(inode);
  479. if (!cell)
  480. return -ENOENT;
  481. ret = seq_open(file, &afs_proc_cell_servers_ops);
  482. if (ret < 0)
  483. return ret;
  484. m = file->private_data;
  485. m->private = cell;
  486. return 0;
  487. }
  488. /*
  489. * set up the iterator to start reading from the cells list and return the
  490. * first item
  491. */
  492. static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
  493. __acquires(m->private->servers_lock)
  494. {
  495. struct afs_cell *cell = m->private;
  496. _enter("cell=%p pos=%Ld", cell, *_pos);
  497. /* lock the list against modification */
  498. read_lock(&cell->servers_lock);
  499. return seq_list_start_head(&cell->servers, *_pos);
  500. }
  501. /*
  502. * move to next cell in cells list
  503. */
  504. static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
  505. loff_t *_pos)
  506. {
  507. struct afs_cell *cell = p->private;
  508. _enter("cell=%p pos=%Ld", cell, *_pos);
  509. return seq_list_next(v, &cell->servers, _pos);
  510. }
  511. /*
  512. * clean up after reading from the cells list
  513. */
  514. static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
  515. __releases(p->private->servers_lock)
  516. {
  517. struct afs_cell *cell = p->private;
  518. read_unlock(&cell->servers_lock);
  519. }
  520. /*
  521. * display a header line followed by a load of volume lines
  522. */
  523. static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
  524. {
  525. struct afs_cell *cell = m->private;
  526. struct afs_server *server = list_entry(v, struct afs_server, link);
  527. char ipaddr[20];
  528. /* display header on line 1 */
  529. if (v == &cell->servers) {
  530. seq_puts(m, "USE ADDR STATE\n");
  531. return 0;
  532. }
  533. /* display one cell per line on subsequent lines */
  534. sprintf(ipaddr, "%pI4", &server->addr);
  535. seq_printf(m, "%3d %-15.15s %5d\n",
  536. atomic_read(&server->usage), ipaddr, server->fs_state);
  537. return 0;
  538. }