object-list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 slow work */
  35. #define FSCACHE_OBJLIST_CONFIG_NOWORK 0x00002000 /* show objects without slow 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. 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. * fscache_object_destroy - Note that a cache object is about to be destroyed
  64. * @object: The object to be destroyed
  65. *
  66. * Note the imminent destruction and deallocation of a cache object record.
  67. */
  68. void fscache_object_destroy(struct fscache_object *obj)
  69. {
  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. EXPORT_SYMBOL(fscache_object_destroy);
  76. /*
  77. * find the object in the tree on or after the specified index
  78. */
  79. static struct fscache_object *fscache_objlist_lookup(loff_t *_pos)
  80. {
  81. struct fscache_object *pobj, *obj = NULL, *minobj = NULL;
  82. struct rb_node *p;
  83. unsigned long pos;
  84. if (*_pos >= (unsigned long) ERR_PTR(-ENOENT))
  85. return NULL;
  86. pos = *_pos;
  87. /* banners (can't represent line 0 by pos 0 as that would involve
  88. * returning a NULL pointer) */
  89. if (pos == 0)
  90. return (struct fscache_object *) ++(*_pos);
  91. if (pos < 3)
  92. return (struct fscache_object *)pos;
  93. pobj = (struct fscache_object *)pos;
  94. p = fscache_object_list.rb_node;
  95. while (p) {
  96. obj = rb_entry(p, struct fscache_object, objlist_link);
  97. if (pobj < obj) {
  98. if (!minobj || minobj > obj)
  99. minobj = obj;
  100. p = p->rb_left;
  101. } else if (pobj > obj) {
  102. p = p->rb_right;
  103. } else {
  104. minobj = obj;
  105. break;
  106. }
  107. obj = NULL;
  108. }
  109. if (!minobj)
  110. *_pos = (unsigned long) ERR_PTR(-ENOENT);
  111. else if (minobj != obj)
  112. *_pos = (unsigned long) minobj;
  113. return minobj;
  114. }
  115. /*
  116. * set up the iterator to start reading from the first line
  117. */
  118. static void *fscache_objlist_start(struct seq_file *m, loff_t *_pos)
  119. __acquires(&fscache_object_list_lock)
  120. {
  121. read_lock(&fscache_object_list_lock);
  122. return fscache_objlist_lookup(_pos);
  123. }
  124. /*
  125. * move to the next line
  126. */
  127. static void *fscache_objlist_next(struct seq_file *m, void *v, loff_t *_pos)
  128. {
  129. (*_pos)++;
  130. return fscache_objlist_lookup(_pos);
  131. }
  132. /*
  133. * clean up after reading
  134. */
  135. static void fscache_objlist_stop(struct seq_file *m, void *v)
  136. __releases(&fscache_object_list_lock)
  137. {
  138. read_unlock(&fscache_object_list_lock);
  139. }
  140. /*
  141. * display an object
  142. */
  143. static int fscache_objlist_show(struct seq_file *m, void *v)
  144. {
  145. struct fscache_objlist_data *data = m->private;
  146. struct fscache_object *obj = v;
  147. unsigned long config = data->config;
  148. uint16_t keylen, auxlen;
  149. char _type[3], *type;
  150. bool no_cookie;
  151. u8 *buf = data->buf, *p;
  152. if ((unsigned long) v == 1) {
  153. seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
  154. " EM EV F S"
  155. " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
  156. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  157. FSCACHE_OBJLIST_CONFIG_AUX))
  158. seq_puts(m, " ");
  159. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  160. seq_puts(m, "OBJECT_KEY");
  161. if ((config & (FSCACHE_OBJLIST_CONFIG_KEY |
  162. FSCACHE_OBJLIST_CONFIG_AUX)) ==
  163. (FSCACHE_OBJLIST_CONFIG_KEY | FSCACHE_OBJLIST_CONFIG_AUX))
  164. seq_puts(m, ", ");
  165. if (config & FSCACHE_OBJLIST_CONFIG_AUX)
  166. seq_puts(m, "AUX_DATA");
  167. seq_puts(m, "\n");
  168. return 0;
  169. }
  170. if ((unsigned long) v == 2) {
  171. seq_puts(m, "======== ======== ==== ===== === === === == ====="
  172. " == == = ="
  173. " | ================ == == ================");
  174. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  175. FSCACHE_OBJLIST_CONFIG_AUX))
  176. seq_puts(m, " ================");
  177. seq_puts(m, "\n");
  178. return 0;
  179. }
  180. /* filter out any unwanted objects */
  181. #define FILTER(criterion, _yes, _no) \
  182. do { \
  183. unsigned long yes = FSCACHE_OBJLIST_CONFIG_##_yes; \
  184. unsigned long no = FSCACHE_OBJLIST_CONFIG_##_no; \
  185. if (criterion) { \
  186. if (!(config & yes)) \
  187. return 0; \
  188. } else { \
  189. if (!(config & no)) \
  190. return 0; \
  191. } \
  192. } while(0)
  193. if (~config) {
  194. FILTER(obj->cookie,
  195. COOKIE, NOCOOKIE);
  196. FILTER(obj->state != FSCACHE_OBJECT_ACTIVE ||
  197. obj->n_ops != 0 ||
  198. obj->n_obj_ops != 0 ||
  199. obj->flags ||
  200. !list_empty(&obj->dependents),
  201. BUSY, IDLE);
  202. FILTER(test_bit(FSCACHE_OBJECT_PENDING_WRITE, &obj->flags),
  203. PENDWR, NOPENDWR);
  204. FILTER(atomic_read(&obj->n_reads),
  205. READS, NOREADS);
  206. FILTER(obj->events & obj->event_mask,
  207. EVENTS, NOEVENTS);
  208. FILTER(obj->work.flags & ~(1UL << SLOW_WORK_VERY_SLOW),
  209. WORK, NOWORK);
  210. }
  211. seq_printf(m,
  212. "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %1lx %1lx | ",
  213. obj->debug_id,
  214. obj->parent ? obj->parent->debug_id : -1,
  215. fscache_object_states_short[obj->state],
  216. obj->n_children,
  217. obj->n_ops,
  218. obj->n_obj_ops,
  219. obj->n_in_progress,
  220. obj->n_exclusive,
  221. atomic_read(&obj->n_reads),
  222. obj->event_mask & FSCACHE_OBJECT_EVENTS_MASK,
  223. obj->events,
  224. obj->flags,
  225. obj->work.flags);
  226. no_cookie = true;
  227. keylen = auxlen = 0;
  228. if (obj->cookie) {
  229. spin_lock(&obj->lock);
  230. if (obj->cookie) {
  231. switch (obj->cookie->def->type) {
  232. case 0:
  233. type = "IX";
  234. break;
  235. case 1:
  236. type = "DT";
  237. break;
  238. default:
  239. sprintf(_type, "%02u",
  240. obj->cookie->def->type);
  241. type = _type;
  242. break;
  243. }
  244. seq_printf(m, "%-16s %s %2lx %16p",
  245. obj->cookie->def->name,
  246. type,
  247. obj->cookie->flags,
  248. obj->cookie->netfs_data);
  249. if (obj->cookie->def->get_key &&
  250. config & FSCACHE_OBJLIST_CONFIG_KEY)
  251. keylen = obj->cookie->def->get_key(
  252. obj->cookie->netfs_data,
  253. buf, 400);
  254. if (obj->cookie->def->get_aux &&
  255. config & FSCACHE_OBJLIST_CONFIG_AUX)
  256. auxlen = obj->cookie->def->get_aux(
  257. obj->cookie->netfs_data,
  258. buf + keylen, 512 - keylen);
  259. no_cookie = false;
  260. }
  261. spin_unlock(&obj->lock);
  262. if (!no_cookie && (keylen > 0 || auxlen > 0)) {
  263. seq_printf(m, " ");
  264. for (p = buf; keylen > 0; keylen--)
  265. seq_printf(m, "%02x", *p++);
  266. if (auxlen > 0) {
  267. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  268. seq_printf(m, ", ");
  269. for (; auxlen > 0; auxlen--)
  270. seq_printf(m, "%02x", *p++);
  271. }
  272. }
  273. }
  274. if (no_cookie)
  275. seq_printf(m, "<no_cookie>\n");
  276. else
  277. seq_printf(m, "\n");
  278. return 0;
  279. }
  280. static const struct seq_operations fscache_objlist_ops = {
  281. .start = fscache_objlist_start,
  282. .stop = fscache_objlist_stop,
  283. .next = fscache_objlist_next,
  284. .show = fscache_objlist_show,
  285. };
  286. /*
  287. * get the configuration for filtering the list
  288. */
  289. static void fscache_objlist_config(struct fscache_objlist_data *data)
  290. {
  291. #ifdef CONFIG_KEYS
  292. struct user_key_payload *confkey;
  293. unsigned long config;
  294. struct key *key;
  295. const char *buf;
  296. int len;
  297. key = request_key(&key_type_user, "fscache:objlist", NULL);
  298. if (IS_ERR(key))
  299. goto no_config;
  300. config = 0;
  301. rcu_read_lock();
  302. confkey = key->payload.data;
  303. buf = confkey->data;
  304. for (len = confkey->datalen - 1; len >= 0; len--) {
  305. switch (buf[len]) {
  306. case 'K': config |= FSCACHE_OBJLIST_CONFIG_KEY; break;
  307. case 'A': config |= FSCACHE_OBJLIST_CONFIG_AUX; break;
  308. case 'C': config |= FSCACHE_OBJLIST_CONFIG_COOKIE; break;
  309. case 'c': config |= FSCACHE_OBJLIST_CONFIG_NOCOOKIE; break;
  310. case 'B': config |= FSCACHE_OBJLIST_CONFIG_BUSY; break;
  311. case 'b': config |= FSCACHE_OBJLIST_CONFIG_IDLE; break;
  312. case 'W': config |= FSCACHE_OBJLIST_CONFIG_PENDWR; break;
  313. case 'w': config |= FSCACHE_OBJLIST_CONFIG_NOPENDWR; break;
  314. case 'R': config |= FSCACHE_OBJLIST_CONFIG_READS; break;
  315. case 'r': config |= FSCACHE_OBJLIST_CONFIG_NOREADS; break;
  316. case 'S': config |= FSCACHE_OBJLIST_CONFIG_WORK; break;
  317. case 's': config |= FSCACHE_OBJLIST_CONFIG_NOWORK; break;
  318. }
  319. }
  320. rcu_read_unlock();
  321. key_put(key);
  322. if (!(config & (FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE)))
  323. config |= FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE;
  324. if (!(config & (FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE)))
  325. config |= FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE;
  326. if (!(config & (FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR)))
  327. config |= FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR;
  328. if (!(config & (FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS)))
  329. config |= FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS;
  330. if (!(config & (FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS)))
  331. config |= FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS;
  332. if (!(config & (FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK)))
  333. config |= FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK;
  334. data->config = config;
  335. return;
  336. no_config:
  337. #endif
  338. data->config = ULONG_MAX;
  339. }
  340. /*
  341. * open "/proc/fs/fscache/objects" to provide a list of active objects
  342. * - can be configured by a user-defined key added to the caller's keyrings
  343. */
  344. static int fscache_objlist_open(struct inode *inode, struct file *file)
  345. {
  346. struct fscache_objlist_data *data;
  347. struct seq_file *m;
  348. int ret;
  349. ret = seq_open(file, &fscache_objlist_ops);
  350. if (ret < 0)
  351. return ret;
  352. m = file->private_data;
  353. /* buffer for key extraction */
  354. data = kmalloc(sizeof(struct fscache_objlist_data), GFP_KERNEL);
  355. if (!data) {
  356. seq_release(inode, file);
  357. return -ENOMEM;
  358. }
  359. /* get the configuration key */
  360. fscache_objlist_config(data);
  361. m->private = data;
  362. return 0;
  363. }
  364. /*
  365. * clean up on close
  366. */
  367. static int fscache_objlist_release(struct inode *inode, struct file *file)
  368. {
  369. struct seq_file *m = file->private_data;
  370. kfree(m->private);
  371. m->private = NULL;
  372. return seq_release(inode, file);
  373. }
  374. const struct file_operations fscache_objlist_fops = {
  375. .owner = THIS_MODULE,
  376. .open = fscache_objlist_open,
  377. .read = seq_read,
  378. .llseek = seq_lseek,
  379. .release = fscache_objlist_release,
  380. };