object-list.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. };
  37. /*
  38. * Add an object to the object list
  39. * - we use the address of the fscache_object structure as the key into the
  40. * tree
  41. */
  42. void fscache_objlist_add(struct fscache_object *obj)
  43. {
  44. struct fscache_object *xobj;
  45. struct rb_node **p = &fscache_object_list.rb_node, *parent = NULL;
  46. ASSERT(RB_EMPTY_NODE(&obj->objlist_link));
  47. write_lock(&fscache_object_list_lock);
  48. while (*p) {
  49. parent = *p;
  50. xobj = rb_entry(parent, struct fscache_object, objlist_link);
  51. if (obj < xobj)
  52. p = &(*p)->rb_left;
  53. else if (obj > xobj)
  54. p = &(*p)->rb_right;
  55. else
  56. BUG();
  57. }
  58. rb_link_node(&obj->objlist_link, parent, p);
  59. rb_insert_color(&obj->objlist_link, &fscache_object_list);
  60. write_unlock(&fscache_object_list_lock);
  61. }
  62. /*
  63. * Remove an object from the object list.
  64. */
  65. void fscache_objlist_remove(struct fscache_object *obj)
  66. {
  67. if (RB_EMPTY_NODE(&obj->objlist_link))
  68. return;
  69. write_lock(&fscache_object_list_lock);
  70. BUG_ON(RB_EMPTY_ROOT(&fscache_object_list));
  71. rb_erase(&obj->objlist_link, &fscache_object_list);
  72. write_unlock(&fscache_object_list_lock);
  73. }
  74. /*
  75. * find the object in the tree on or after the specified index
  76. */
  77. static struct fscache_object *fscache_objlist_lookup(loff_t *_pos)
  78. {
  79. struct fscache_object *pobj, *obj = NULL, *minobj = NULL;
  80. struct rb_node *p;
  81. unsigned long pos;
  82. if (*_pos >= (unsigned long) ERR_PTR(-ENOENT))
  83. return NULL;
  84. pos = *_pos;
  85. /* banners (can't represent line 0 by pos 0 as that would involve
  86. * returning a NULL pointer) */
  87. if (pos == 0)
  88. return (struct fscache_object *)(long)++(*_pos);
  89. if (pos < 3)
  90. return (struct fscache_object *)pos;
  91. pobj = (struct fscache_object *)pos;
  92. p = fscache_object_list.rb_node;
  93. while (p) {
  94. obj = rb_entry(p, struct fscache_object, objlist_link);
  95. if (pobj < obj) {
  96. if (!minobj || minobj > obj)
  97. minobj = obj;
  98. p = p->rb_left;
  99. } else if (pobj > obj) {
  100. p = p->rb_right;
  101. } else {
  102. minobj = obj;
  103. break;
  104. }
  105. obj = NULL;
  106. }
  107. if (!minobj)
  108. *_pos = (unsigned long) ERR_PTR(-ENOENT);
  109. else if (minobj != obj)
  110. *_pos = (unsigned long) minobj;
  111. return minobj;
  112. }
  113. /*
  114. * set up the iterator to start reading from the first line
  115. */
  116. static void *fscache_objlist_start(struct seq_file *m, loff_t *_pos)
  117. __acquires(&fscache_object_list_lock)
  118. {
  119. read_lock(&fscache_object_list_lock);
  120. return fscache_objlist_lookup(_pos);
  121. }
  122. /*
  123. * move to the next line
  124. */
  125. static void *fscache_objlist_next(struct seq_file *m, void *v, loff_t *_pos)
  126. {
  127. (*_pos)++;
  128. return fscache_objlist_lookup(_pos);
  129. }
  130. /*
  131. * clean up after reading
  132. */
  133. static void fscache_objlist_stop(struct seq_file *m, void *v)
  134. __releases(&fscache_object_list_lock)
  135. {
  136. read_unlock(&fscache_object_list_lock);
  137. }
  138. /*
  139. * display an object
  140. */
  141. static int fscache_objlist_show(struct seq_file *m, void *v)
  142. {
  143. struct fscache_objlist_data *data = m->private;
  144. struct fscache_object *obj = v;
  145. struct fscache_cookie *cookie;
  146. unsigned long config = data->config;
  147. char _type[3], *type;
  148. u8 *p;
  149. if ((unsigned long) v == 1) {
  150. seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
  151. " EM EV FL S"
  152. " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
  153. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  154. FSCACHE_OBJLIST_CONFIG_AUX))
  155. seq_puts(m, " ");
  156. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  157. seq_puts(m, "OBJECT_KEY");
  158. if ((config & (FSCACHE_OBJLIST_CONFIG_KEY |
  159. FSCACHE_OBJLIST_CONFIG_AUX)) ==
  160. (FSCACHE_OBJLIST_CONFIG_KEY | FSCACHE_OBJLIST_CONFIG_AUX))
  161. seq_puts(m, ", ");
  162. if (config & FSCACHE_OBJLIST_CONFIG_AUX)
  163. seq_puts(m, "AUX_DATA");
  164. seq_puts(m, "\n");
  165. return 0;
  166. }
  167. if ((unsigned long) v == 2) {
  168. seq_puts(m, "======== ======== ==== ===== === === === == ====="
  169. " == == == ="
  170. " | ================ == == ================");
  171. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  172. FSCACHE_OBJLIST_CONFIG_AUX))
  173. seq_puts(m, " ================");
  174. seq_puts(m, "\n");
  175. return 0;
  176. }
  177. /* filter out any unwanted objects */
  178. #define FILTER(criterion, _yes, _no) \
  179. do { \
  180. unsigned long yes = FSCACHE_OBJLIST_CONFIG_##_yes; \
  181. unsigned long no = FSCACHE_OBJLIST_CONFIG_##_no; \
  182. if (criterion) { \
  183. if (!(config & yes)) \
  184. return 0; \
  185. } else { \
  186. if (!(config & no)) \
  187. return 0; \
  188. } \
  189. } while(0)
  190. cookie = obj->cookie;
  191. if (~config) {
  192. FILTER(cookie->def,
  193. COOKIE, NOCOOKIE);
  194. FILTER(fscache_object_is_active(obj) ||
  195. obj->n_ops != 0 ||
  196. obj->n_obj_ops != 0 ||
  197. obj->flags ||
  198. !list_empty(&obj->dependents),
  199. BUSY, IDLE);
  200. FILTER(test_bit(FSCACHE_OBJECT_PENDING_WRITE, &obj->flags),
  201. PENDWR, NOPENDWR);
  202. FILTER(atomic_read(&obj->n_reads),
  203. READS, NOREADS);
  204. FILTER(obj->events & obj->event_mask,
  205. EVENTS, NOEVENTS);
  206. FILTER(work_busy(&obj->work), WORK, NOWORK);
  207. }
  208. seq_printf(m,
  209. "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %2lx %1x | ",
  210. obj->debug_id,
  211. obj->parent ? obj->parent->debug_id : -1,
  212. obj->state->short_name,
  213. obj->n_children,
  214. obj->n_ops,
  215. obj->n_obj_ops,
  216. obj->n_in_progress,
  217. obj->n_exclusive,
  218. atomic_read(&obj->n_reads),
  219. obj->event_mask,
  220. obj->events,
  221. obj->flags,
  222. work_busy(&obj->work));
  223. if (fscache_use_cookie(obj)) {
  224. uint16_t keylen = 0, auxlen = 0;
  225. switch (cookie->type) {
  226. case 0:
  227. type = "IX";
  228. break;
  229. case 1:
  230. type = "DT";
  231. break;
  232. default:
  233. snprintf(_type, sizeof(_type), "%02u",
  234. cookie->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 (config & FSCACHE_OBJLIST_CONFIG_KEY)
  244. keylen = cookie->key_len;
  245. if (config & FSCACHE_OBJLIST_CONFIG_AUX)
  246. auxlen = cookie->aux_len;
  247. if (keylen > 0 || auxlen > 0) {
  248. seq_puts(m, " ");
  249. p = keylen <= sizeof(cookie->inline_key) ?
  250. cookie->inline_key : cookie->key;
  251. for (; keylen > 0; keylen--)
  252. seq_printf(m, "%02x", *p++);
  253. if (auxlen > 0) {
  254. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  255. seq_puts(m, ", ");
  256. p = auxlen <= sizeof(cookie->inline_aux) ?
  257. cookie->inline_aux : cookie->aux;
  258. for (; auxlen > 0; auxlen--)
  259. seq_printf(m, "%02x", *p++);
  260. }
  261. }
  262. seq_puts(m, "\n");
  263. fscache_unuse_cookie(obj);
  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_rcu(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. .open = fscache_objlist_open,
  361. .read = seq_read,
  362. .llseek = seq_lseek,
  363. .release = fscache_objlist_release,
  364. };