proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 = kmalloc(size + 1, GFP_KERNEL);
  198. if (!kbuf)
  199. return -ENOMEM;
  200. ret = -EFAULT;
  201. if (copy_from_user(kbuf, buf, size) != 0)
  202. goto done;
  203. kbuf[size] = 0;
  204. /* trim to first NL */
  205. name = memchr(kbuf, '\n', size);
  206. if (name)
  207. *name = 0;
  208. /* split into command, name and argslist */
  209. name = strchr(kbuf, ' ');
  210. if (!name)
  211. goto inval;
  212. do {
  213. *name++ = 0;
  214. } while(*name == ' ');
  215. if (!*name)
  216. goto inval;
  217. args = strchr(name, ' ');
  218. if (!args)
  219. goto inval;
  220. do {
  221. *args++ = 0;
  222. } while(*args == ' ');
  223. if (!*args)
  224. goto inval;
  225. /* determine command to perform */
  226. _debug("cmd=%s name=%s args=%s", kbuf, name, args);
  227. if (strcmp(kbuf, "add") == 0) {
  228. struct afs_cell *cell;
  229. cell = afs_cell_create(name, strlen(name), args, false);
  230. if (IS_ERR(cell)) {
  231. ret = PTR_ERR(cell);
  232. goto done;
  233. }
  234. afs_put_cell(cell);
  235. printk("kAFS: Added new cell '%s'\n", name);
  236. } else {
  237. goto inval;
  238. }
  239. ret = size;
  240. done:
  241. kfree(kbuf);
  242. _leave(" = %d", ret);
  243. return ret;
  244. inval:
  245. ret = -EINVAL;
  246. printk("kAFS: Invalid Command on /proc/fs/afs/cells file\n");
  247. goto done;
  248. }
  249. static ssize_t afs_proc_rootcell_read(struct file *file, char __user *buf,
  250. size_t size, loff_t *_pos)
  251. {
  252. return 0;
  253. }
  254. /*
  255. * handle writes to /proc/fs/afs/rootcell
  256. * - to initialize rootcell: echo "cell.name:192.168.231.14"
  257. */
  258. static ssize_t afs_proc_rootcell_write(struct file *file,
  259. const char __user *buf,
  260. size_t size, loff_t *_pos)
  261. {
  262. char *kbuf, *s;
  263. int ret;
  264. /* start by dragging the command into memory */
  265. if (size <= 1 || size >= PAGE_SIZE)
  266. return -EINVAL;
  267. ret = -ENOMEM;
  268. kbuf = kmalloc(size + 1, GFP_KERNEL);
  269. if (!kbuf)
  270. goto nomem;
  271. ret = -EFAULT;
  272. if (copy_from_user(kbuf, buf, size) != 0)
  273. goto infault;
  274. kbuf[size] = 0;
  275. /* trim to first NL */
  276. s = memchr(kbuf, '\n', size);
  277. if (s)
  278. *s = 0;
  279. /* determine command to perform */
  280. _debug("rootcell=%s", kbuf);
  281. ret = afs_cell_init(kbuf);
  282. if (ret >= 0)
  283. ret = size; /* consume everything, always */
  284. infault:
  285. kfree(kbuf);
  286. nomem:
  287. _leave(" = %d", ret);
  288. return ret;
  289. }
  290. /*
  291. * initialise /proc/fs/afs/<cell>/
  292. */
  293. int afs_proc_cell_setup(struct afs_cell *cell)
  294. {
  295. struct proc_dir_entry *dir;
  296. _enter("%p{%s}", cell, cell->name);
  297. dir = proc_mkdir(cell->name, proc_afs);
  298. if (!dir)
  299. goto error_dir;
  300. if (!proc_create_data("servers", 0, dir,
  301. &afs_proc_cell_servers_fops, cell) ||
  302. !proc_create_data("vlservers", 0, dir,
  303. &afs_proc_cell_vlservers_fops, cell) ||
  304. !proc_create_data("volumes", 0, dir,
  305. &afs_proc_cell_volumes_fops, cell))
  306. goto error_tree;
  307. _leave(" = 0");
  308. return 0;
  309. error_tree:
  310. remove_proc_subtree(cell->name, proc_afs);
  311. error_dir:
  312. _leave(" = -ENOMEM");
  313. return -ENOMEM;
  314. }
  315. /*
  316. * remove /proc/fs/afs/<cell>/
  317. */
  318. void afs_proc_cell_remove(struct afs_cell *cell)
  319. {
  320. _enter("");
  321. remove_proc_subtree(cell->name, proc_afs);
  322. _leave("");
  323. }
  324. /*
  325. * open "/proc/fs/afs/<cell>/volumes" which provides a summary of extant cells
  326. */
  327. static int afs_proc_cell_volumes_open(struct inode *inode, struct file *file)
  328. {
  329. struct afs_cell *cell;
  330. struct seq_file *m;
  331. int ret;
  332. cell = PDE_DATA(inode);
  333. if (!cell)
  334. return -ENOENT;
  335. ret = seq_open(file, &afs_proc_cell_volumes_ops);
  336. if (ret < 0)
  337. return ret;
  338. m = file->private_data;
  339. m->private = cell;
  340. return 0;
  341. }
  342. /*
  343. * set up the iterator to start reading from the cells list and return the
  344. * first item
  345. */
  346. static void *afs_proc_cell_volumes_start(struct seq_file *m, loff_t *_pos)
  347. {
  348. struct afs_cell *cell = m->private;
  349. _enter("cell=%p pos=%Ld", cell, *_pos);
  350. /* lock the list against modification */
  351. down_read(&cell->vl_sem);
  352. return seq_list_start_head(&cell->vl_list, *_pos);
  353. }
  354. /*
  355. * move to next cell in cells list
  356. */
  357. static void *afs_proc_cell_volumes_next(struct seq_file *p, void *v,
  358. loff_t *_pos)
  359. {
  360. struct afs_cell *cell = p->private;
  361. _enter("cell=%p pos=%Ld", cell, *_pos);
  362. return seq_list_next(v, &cell->vl_list, _pos);
  363. }
  364. /*
  365. * clean up after reading from the cells list
  366. */
  367. static void afs_proc_cell_volumes_stop(struct seq_file *p, void *v)
  368. {
  369. struct afs_cell *cell = p->private;
  370. up_read(&cell->vl_sem);
  371. }
  372. static const char afs_vlocation_states[][4] = {
  373. [AFS_VL_NEW] = "New",
  374. [AFS_VL_CREATING] = "Crt",
  375. [AFS_VL_VALID] = "Val",
  376. [AFS_VL_NO_VOLUME] = "NoV",
  377. [AFS_VL_UPDATING] = "Upd",
  378. [AFS_VL_VOLUME_DELETED] = "Del",
  379. [AFS_VL_UNCERTAIN] = "Unc",
  380. };
  381. /*
  382. * display a header line followed by a load of volume lines
  383. */
  384. static int afs_proc_cell_volumes_show(struct seq_file *m, void *v)
  385. {
  386. struct afs_cell *cell = m->private;
  387. struct afs_vlocation *vlocation =
  388. list_entry(v, struct afs_vlocation, link);
  389. /* display header on line 1 */
  390. if (v == &cell->vl_list) {
  391. seq_puts(m, "USE STT VLID[0] VLID[1] VLID[2] NAME\n");
  392. return 0;
  393. }
  394. /* display one cell per line on subsequent lines */
  395. seq_printf(m, "%3d %s %08x %08x %08x %s\n",
  396. atomic_read(&vlocation->usage),
  397. afs_vlocation_states[vlocation->state],
  398. vlocation->vldb.vid[0],
  399. vlocation->vldb.vid[1],
  400. vlocation->vldb.vid[2],
  401. vlocation->vldb.name);
  402. return 0;
  403. }
  404. /*
  405. * open "/proc/fs/afs/<cell>/vlservers" which provides a list of volume
  406. * location server
  407. */
  408. static int afs_proc_cell_vlservers_open(struct inode *inode, struct file *file)
  409. {
  410. struct afs_cell *cell;
  411. struct seq_file *m;
  412. int ret;
  413. cell = PDE_DATA(inode);
  414. if (!cell)
  415. return -ENOENT;
  416. ret = seq_open(file, &afs_proc_cell_vlservers_ops);
  417. if (ret<0)
  418. return ret;
  419. m = file->private_data;
  420. m->private = cell;
  421. return 0;
  422. }
  423. /*
  424. * set up the iterator to start reading from the cells list and return the
  425. * first item
  426. */
  427. static void *afs_proc_cell_vlservers_start(struct seq_file *m, loff_t *_pos)
  428. {
  429. struct afs_cell *cell = m->private;
  430. loff_t pos = *_pos;
  431. _enter("cell=%p pos=%Ld", cell, *_pos);
  432. /* lock the list against modification */
  433. down_read(&cell->vl_sem);
  434. /* allow for the header line */
  435. if (!pos)
  436. return (void *) 1;
  437. pos--;
  438. if (pos >= cell->vl_naddrs)
  439. return NULL;
  440. return &cell->vl_addrs[pos];
  441. }
  442. /*
  443. * move to next cell in cells list
  444. */
  445. static void *afs_proc_cell_vlservers_next(struct seq_file *p, void *v,
  446. loff_t *_pos)
  447. {
  448. struct afs_cell *cell = p->private;
  449. loff_t pos;
  450. _enter("cell=%p{nad=%u} pos=%Ld", cell, cell->vl_naddrs, *_pos);
  451. pos = *_pos;
  452. (*_pos)++;
  453. if (pos >= cell->vl_naddrs)
  454. return NULL;
  455. return &cell->vl_addrs[pos];
  456. }
  457. /*
  458. * clean up after reading from the cells list
  459. */
  460. static void afs_proc_cell_vlservers_stop(struct seq_file *p, void *v)
  461. {
  462. struct afs_cell *cell = p->private;
  463. up_read(&cell->vl_sem);
  464. }
  465. /*
  466. * display a header line followed by a load of volume lines
  467. */
  468. static int afs_proc_cell_vlservers_show(struct seq_file *m, void *v)
  469. {
  470. struct in_addr *addr = v;
  471. /* display header on line 1 */
  472. if (v == (struct in_addr *) 1) {
  473. seq_puts(m, "ADDRESS\n");
  474. return 0;
  475. }
  476. /* display one cell per line on subsequent lines */
  477. seq_printf(m, "%pI4\n", &addr->s_addr);
  478. return 0;
  479. }
  480. /*
  481. * open "/proc/fs/afs/<cell>/servers" which provides a summary of active
  482. * servers
  483. */
  484. static int afs_proc_cell_servers_open(struct inode *inode, struct file *file)
  485. {
  486. struct afs_cell *cell;
  487. struct seq_file *m;
  488. int ret;
  489. cell = PDE_DATA(inode);
  490. if (!cell)
  491. return -ENOENT;
  492. ret = seq_open(file, &afs_proc_cell_servers_ops);
  493. if (ret < 0)
  494. return ret;
  495. m = file->private_data;
  496. m->private = cell;
  497. return 0;
  498. }
  499. /*
  500. * set up the iterator to start reading from the cells list and return the
  501. * first item
  502. */
  503. static void *afs_proc_cell_servers_start(struct seq_file *m, loff_t *_pos)
  504. __acquires(m->private->servers_lock)
  505. {
  506. struct afs_cell *cell = m->private;
  507. _enter("cell=%p pos=%Ld", cell, *_pos);
  508. /* lock the list against modification */
  509. read_lock(&cell->servers_lock);
  510. return seq_list_start_head(&cell->servers, *_pos);
  511. }
  512. /*
  513. * move to next cell in cells list
  514. */
  515. static void *afs_proc_cell_servers_next(struct seq_file *p, void *v,
  516. loff_t *_pos)
  517. {
  518. struct afs_cell *cell = p->private;
  519. _enter("cell=%p pos=%Ld", cell, *_pos);
  520. return seq_list_next(v, &cell->servers, _pos);
  521. }
  522. /*
  523. * clean up after reading from the cells list
  524. */
  525. static void afs_proc_cell_servers_stop(struct seq_file *p, void *v)
  526. __releases(p->private->servers_lock)
  527. {
  528. struct afs_cell *cell = p->private;
  529. read_unlock(&cell->servers_lock);
  530. }
  531. /*
  532. * display a header line followed by a load of volume lines
  533. */
  534. static int afs_proc_cell_servers_show(struct seq_file *m, void *v)
  535. {
  536. struct afs_cell *cell = m->private;
  537. struct afs_server *server = list_entry(v, struct afs_server, link);
  538. char ipaddr[20];
  539. /* display header on line 1 */
  540. if (v == &cell->servers) {
  541. seq_puts(m, "USE ADDR STATE\n");
  542. return 0;
  543. }
  544. /* display one cell per line on subsequent lines */
  545. sprintf(ipaddr, "%pI4", &server->addr);
  546. seq_printf(m, "%3d %-15.15s %5d\n",
  547. atomic_read(&server->usage), ipaddr, server->fs_state);
  548. return 0;
  549. }