object-list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Global fscache object list maintainer and viewer
  2. *
  3. * Copyright (C) 2009 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define FSCACHE_DEBUG_LEVEL COOKIE
  12. #include <linux/module.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/key.h>
  16. #include <keys/user-type.h>
  17. #include "internal.h"
  18. static struct rb_root fscache_object_list;
  19. static DEFINE_RWLOCK(fscache_object_list_lock);
  20. struct fscache_objlist_data {
  21. unsigned long config; /* display configuration */
  22. #define FSCACHE_OBJLIST_CONFIG_KEY 0x00000001 /* show object keys */
  23. #define FSCACHE_OBJLIST_CONFIG_AUX 0x00000002 /* show object auxdata */
  24. #define FSCACHE_OBJLIST_CONFIG_COOKIE 0x00000004 /* show objects with cookies */
  25. #define FSCACHE_OBJLIST_CONFIG_NOCOOKIE 0x00000008 /* show objects without cookies */
  26. #define FSCACHE_OBJLIST_CONFIG_BUSY 0x00000010 /* show busy objects */
  27. #define FSCACHE_OBJLIST_CONFIG_IDLE 0x00000020 /* show idle objects */
  28. #define FSCACHE_OBJLIST_CONFIG_PENDWR 0x00000040 /* show objects with pending writes */
  29. #define FSCACHE_OBJLIST_CONFIG_NOPENDWR 0x00000080 /* show objects without pending writes */
  30. #define FSCACHE_OBJLIST_CONFIG_READS 0x00000100 /* show objects with active reads */
  31. #define FSCACHE_OBJLIST_CONFIG_NOREADS 0x00000200 /* show objects without active reads */
  32. #define FSCACHE_OBJLIST_CONFIG_EVENTS 0x00000400 /* show objects with events */
  33. #define FSCACHE_OBJLIST_CONFIG_NOEVENTS 0x00000800 /* show objects without no events */
  34. #define FSCACHE_OBJLIST_CONFIG_WORK 0x00001000 /* show objects with work */
  35. #define FSCACHE_OBJLIST_CONFIG_NOWORK 0x00002000 /* show objects without work */
  36. u8 buf[512]; /* key and aux data buffer */
  37. };
  38. /*
  39. * Add an object to the object list
  40. * - we use the address of the fscache_object structure as the key into the
  41. * tree
  42. */
  43. void fscache_objlist_add(struct fscache_object *obj)
  44. {
  45. struct fscache_object *xobj;
  46. struct rb_node **p = &fscache_object_list.rb_node, *parent = NULL;
  47. ASSERT(RB_EMPTY_NODE(&obj->objlist_link));
  48. write_lock(&fscache_object_list_lock);
  49. while (*p) {
  50. parent = *p;
  51. xobj = rb_entry(parent, struct fscache_object, objlist_link);
  52. if (obj < xobj)
  53. p = &(*p)->rb_left;
  54. else if (obj > xobj)
  55. p = &(*p)->rb_right;
  56. else
  57. BUG();
  58. }
  59. rb_link_node(&obj->objlist_link, parent, p);
  60. rb_insert_color(&obj->objlist_link, &fscache_object_list);
  61. write_unlock(&fscache_object_list_lock);
  62. }
  63. /*
  64. * Remove an object from the object list.
  65. */
  66. void fscache_objlist_remove(struct fscache_object *obj)
  67. {
  68. if (RB_EMPTY_NODE(&obj->objlist_link))
  69. return;
  70. write_lock(&fscache_object_list_lock);
  71. BUG_ON(RB_EMPTY_ROOT(&fscache_object_list));
  72. rb_erase(&obj->objlist_link, &fscache_object_list);
  73. write_unlock(&fscache_object_list_lock);
  74. }
  75. /*
  76. * find the object in the tree on or after the specified index
  77. */
  78. static struct fscache_object *fscache_objlist_lookup(loff_t *_pos)
  79. {
  80. struct fscache_object *pobj, *obj = NULL, *minobj = NULL;
  81. struct rb_node *p;
  82. unsigned long pos;
  83. if (*_pos >= (unsigned long) ERR_PTR(-ENOENT))
  84. return NULL;
  85. pos = *_pos;
  86. /* banners (can't represent line 0 by pos 0 as that would involve
  87. * returning a NULL pointer) */
  88. if (pos == 0)
  89. return (struct fscache_object *)(long)++(*_pos);
  90. if (pos < 3)
  91. return (struct fscache_object *)pos;
  92. pobj = (struct fscache_object *)pos;
  93. p = fscache_object_list.rb_node;
  94. while (p) {
  95. obj = rb_entry(p, struct fscache_object, objlist_link);
  96. if (pobj < obj) {
  97. if (!minobj || minobj > obj)
  98. minobj = obj;
  99. p = p->rb_left;
  100. } else if (pobj > obj) {
  101. p = p->rb_right;
  102. } else {
  103. minobj = obj;
  104. break;
  105. }
  106. obj = NULL;
  107. }
  108. if (!minobj)
  109. *_pos = (unsigned long) ERR_PTR(-ENOENT);
  110. else if (minobj != obj)
  111. *_pos = (unsigned long) minobj;
  112. return minobj;
  113. }
  114. /*
  115. * set up the iterator to start reading from the first line
  116. */
  117. static void *fscache_objlist_start(struct seq_file *m, loff_t *_pos)
  118. __acquires(&fscache_object_list_lock)
  119. {
  120. read_lock(&fscache_object_list_lock);
  121. return fscache_objlist_lookup(_pos);
  122. }
  123. /*
  124. * move to the next line
  125. */
  126. static void *fscache_objlist_next(struct seq_file *m, void *v, loff_t *_pos)
  127. {
  128. (*_pos)++;
  129. return fscache_objlist_lookup(_pos);
  130. }
  131. /*
  132. * clean up after reading
  133. */
  134. static void fscache_objlist_stop(struct seq_file *m, void *v)
  135. __releases(&fscache_object_list_lock)
  136. {
  137. read_unlock(&fscache_object_list_lock);
  138. }
  139. /*
  140. * display an object
  141. */
  142. static int fscache_objlist_show(struct seq_file *m, void *v)
  143. {
  144. struct fscache_objlist_data *data = m->private;
  145. struct fscache_object *obj = v;
  146. struct fscache_cookie *cookie;
  147. unsigned long config = data->config;
  148. char _type[3], *type;
  149. u8 *buf = data->buf, *p;
  150. if ((unsigned long) v == 1) {
  151. seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
  152. " EM EV FL S"
  153. " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
  154. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  155. FSCACHE_OBJLIST_CONFIG_AUX))
  156. seq_puts(m, " ");
  157. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  158. seq_puts(m, "OBJECT_KEY");
  159. if ((config & (FSCACHE_OBJLIST_CONFIG_KEY |
  160. FSCACHE_OBJLIST_CONFIG_AUX)) ==
  161. (FSCACHE_OBJLIST_CONFIG_KEY | FSCACHE_OBJLIST_CONFIG_AUX))
  162. seq_puts(m, ", ");
  163. if (config & FSCACHE_OBJLIST_CONFIG_AUX)
  164. seq_puts(m, "AUX_DATA");
  165. seq_puts(m, "\n");
  166. return 0;
  167. }
  168. if ((unsigned long) v == 2) {
  169. seq_puts(m, "======== ======== ==== ===== === === === == ====="
  170. " == == == ="
  171. " | ================ == == ================");
  172. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  173. FSCACHE_OBJLIST_CONFIG_AUX))
  174. seq_puts(m, " ================");
  175. seq_puts(m, "\n");
  176. return 0;
  177. }
  178. /* filter out any unwanted objects */
  179. #define FILTER(criterion, _yes, _no) \
  180. do { \
  181. unsigned long yes = FSCACHE_OBJLIST_CONFIG_##_yes; \
  182. unsigned long no = FSCACHE_OBJLIST_CONFIG_##_no; \
  183. if (criterion) { \
  184. if (!(config & yes)) \
  185. return 0; \
  186. } else { \
  187. if (!(config & no)) \
  188. return 0; \
  189. } \
  190. } while(0)
  191. cookie = obj->cookie;
  192. if (~config) {
  193. FILTER(cookie->def,
  194. COOKIE, NOCOOKIE);
  195. FILTER(fscache_object_is_active(obj) ||
  196. obj->n_ops != 0 ||
  197. obj->n_obj_ops != 0 ||
  198. obj->flags ||
  199. !list_empty(&obj->dependents),
  200. BUSY, IDLE);
  201. FILTER(test_bit(FSCACHE_OBJECT_PENDING_WRITE, &obj->flags),
  202. PENDWR, NOPENDWR);
  203. FILTER(atomic_read(&obj->n_reads),
  204. READS, NOREADS);
  205. FILTER(obj->events & obj->event_mask,
  206. EVENTS, NOEVENTS);
  207. FILTER(work_busy(&obj->work), WORK, NOWORK);
  208. }
  209. seq_printf(m,
  210. "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %2lx %1x | ",
  211. obj->debug_id,
  212. obj->parent ? obj->parent->debug_id : -1,
  213. obj->state->short_name,
  214. obj->n_children,
  215. obj->n_ops,
  216. obj->n_obj_ops,
  217. obj->n_in_progress,
  218. obj->n_exclusive,
  219. atomic_read(&obj->n_reads),
  220. obj->event_mask,
  221. obj->events,
  222. obj->flags,
  223. work_busy(&obj->work));
  224. if (fscache_use_cookie(obj)) {
  225. uint16_t keylen = 0, auxlen = 0;
  226. switch (cookie->def->type) {
  227. case 0:
  228. type = "IX";
  229. break;
  230. case 1:
  231. type = "DT";
  232. break;
  233. default:
  234. sprintf(_type, "%02u", cookie->def->type);
  235. type = _type;
  236. break;
  237. }
  238. seq_printf(m, "%-16s %s %2lx %16p",
  239. cookie->def->name,
  240. type,
  241. cookie->flags,
  242. cookie->netfs_data);
  243. if (cookie->def->get_key &&
  244. config & FSCACHE_OBJLIST_CONFIG_KEY)
  245. keylen = cookie->def->get_key(cookie->netfs_data,
  246. buf, 400);
  247. if (cookie->def->get_aux &&
  248. config & FSCACHE_OBJLIST_CONFIG_AUX)
  249. auxlen = cookie->def->get_aux(cookie->netfs_data,
  250. buf + keylen, 512 - keylen);
  251. fscache_unuse_cookie(obj);
  252. if (keylen > 0 || auxlen > 0) {
  253. seq_puts(m, " ");
  254. for (p = buf; keylen > 0; keylen--)
  255. seq_printf(m, "%02x", *p++);
  256. if (auxlen > 0) {
  257. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  258. seq_puts(m, ", ");
  259. for (; auxlen > 0; auxlen--)
  260. seq_printf(m, "%02x", *p++);
  261. }
  262. }
  263. seq_puts(m, "\n");
  264. } else {
  265. seq_puts(m, "<no_netfs>\n");
  266. }
  267. return 0;
  268. }
  269. static const struct seq_operations fscache_objlist_ops = {
  270. .start = fscache_objlist_start,
  271. .stop = fscache_objlist_stop,
  272. .next = fscache_objlist_next,
  273. .show = fscache_objlist_show,
  274. };
  275. /*
  276. * get the configuration for filtering the list
  277. */
  278. static void fscache_objlist_config(struct fscache_objlist_data *data)
  279. {
  280. #ifdef CONFIG_KEYS
  281. const struct user_key_payload *confkey;
  282. unsigned long config;
  283. struct key *key;
  284. const char *buf;
  285. int len;
  286. key = request_key(&key_type_user, "fscache:objlist", NULL);
  287. if (IS_ERR(key))
  288. goto no_config;
  289. config = 0;
  290. rcu_read_lock();
  291. confkey = user_key_payload(key);
  292. if (!confkey) {
  293. /* key was revoked */
  294. rcu_read_unlock();
  295. key_put(key);
  296. goto no_config;
  297. }
  298. buf = confkey->data;
  299. for (len = confkey->datalen - 1; len >= 0; len--) {
  300. switch (buf[len]) {
  301. case 'K': config |= FSCACHE_OBJLIST_CONFIG_KEY; break;
  302. case 'A': config |= FSCACHE_OBJLIST_CONFIG_AUX; break;
  303. case 'C': config |= FSCACHE_OBJLIST_CONFIG_COOKIE; break;
  304. case 'c': config |= FSCACHE_OBJLIST_CONFIG_NOCOOKIE; break;
  305. case 'B': config |= FSCACHE_OBJLIST_CONFIG_BUSY; break;
  306. case 'b': config |= FSCACHE_OBJLIST_CONFIG_IDLE; break;
  307. case 'W': config |= FSCACHE_OBJLIST_CONFIG_PENDWR; break;
  308. case 'w': config |= FSCACHE_OBJLIST_CONFIG_NOPENDWR; break;
  309. case 'R': config |= FSCACHE_OBJLIST_CONFIG_READS; break;
  310. case 'r': config |= FSCACHE_OBJLIST_CONFIG_NOREADS; break;
  311. case 'S': config |= FSCACHE_OBJLIST_CONFIG_WORK; break;
  312. case 's': config |= FSCACHE_OBJLIST_CONFIG_NOWORK; break;
  313. }
  314. }
  315. rcu_read_unlock();
  316. key_put(key);
  317. if (!(config & (FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE)))
  318. config |= FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE;
  319. if (!(config & (FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE)))
  320. config |= FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE;
  321. if (!(config & (FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR)))
  322. config |= FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR;
  323. if (!(config & (FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS)))
  324. config |= FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS;
  325. if (!(config & (FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS)))
  326. config |= FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS;
  327. if (!(config & (FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK)))
  328. config |= FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK;
  329. data->config = config;
  330. return;
  331. no_config:
  332. #endif
  333. data->config = ULONG_MAX;
  334. }
  335. /*
  336. * open "/proc/fs/fscache/objects" to provide a list of active objects
  337. * - can be configured by a user-defined key added to the caller's keyrings
  338. */
  339. static int fscache_objlist_open(struct inode *inode, struct file *file)
  340. {
  341. struct fscache_objlist_data *data;
  342. data = __seq_open_private(file, &fscache_objlist_ops, sizeof(*data));
  343. if (!data)
  344. return -ENOMEM;
  345. /* get the configuration key */
  346. fscache_objlist_config(data);
  347. return 0;
  348. }
  349. /*
  350. * clean up on close
  351. */
  352. static int fscache_objlist_release(struct inode *inode, struct file *file)
  353. {
  354. struct seq_file *m = file->private_data;
  355. kfree(m->private);
  356. m->private = NULL;
  357. return seq_release(inode, file);
  358. }
  359. const struct file_operations fscache_objlist_fops = {
  360. .owner = THIS_MODULE,
  361. .open = fscache_objlist_open,
  362. .read = seq_read,
  363. .llseek = seq_lseek,
  364. .release = fscache_objlist_release,
  365. };