object-list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/key.h>
  15. #include <keys/user-type.h>
  16. #include "internal.h"
  17. static struct rb_root fscache_object_list;
  18. static DEFINE_RWLOCK(fscache_object_list_lock);
  19. struct fscache_objlist_data {
  20. unsigned long config; /* display configuration */
  21. #define FSCACHE_OBJLIST_CONFIG_KEY 0x00000001 /* show object keys */
  22. #define FSCACHE_OBJLIST_CONFIG_AUX 0x00000002 /* show object auxdata */
  23. #define FSCACHE_OBJLIST_CONFIG_COOKIE 0x00000004 /* show objects with cookies */
  24. #define FSCACHE_OBJLIST_CONFIG_NOCOOKIE 0x00000008 /* show objects without cookies */
  25. #define FSCACHE_OBJLIST_CONFIG_BUSY 0x00000010 /* show busy objects */
  26. #define FSCACHE_OBJLIST_CONFIG_IDLE 0x00000020 /* show idle objects */
  27. #define FSCACHE_OBJLIST_CONFIG_PENDWR 0x00000040 /* show objects with pending writes */
  28. #define FSCACHE_OBJLIST_CONFIG_NOPENDWR 0x00000080 /* show objects without pending writes */
  29. #define FSCACHE_OBJLIST_CONFIG_READS 0x00000100 /* show objects with active reads */
  30. #define FSCACHE_OBJLIST_CONFIG_NOREADS 0x00000200 /* show objects without active reads */
  31. #define FSCACHE_OBJLIST_CONFIG_EVENTS 0x00000400 /* show objects with events */
  32. #define FSCACHE_OBJLIST_CONFIG_NOEVENTS 0x00000800 /* show objects without no events */
  33. #define FSCACHE_OBJLIST_CONFIG_WORK 0x00001000 /* show objects with slow work */
  34. #define FSCACHE_OBJLIST_CONFIG_NOWORK 0x00002000 /* show objects without slow work */
  35. u8 buf[512]; /* key and aux data buffer */
  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. write_lock(&fscache_object_list_lock);
  47. while (*p) {
  48. parent = *p;
  49. xobj = rb_entry(parent, struct fscache_object, objlist_link);
  50. if (obj < xobj)
  51. p = &(*p)->rb_left;
  52. else if (obj > xobj)
  53. p = &(*p)->rb_right;
  54. else
  55. BUG();
  56. }
  57. rb_link_node(&obj->objlist_link, parent, p);
  58. rb_insert_color(&obj->objlist_link, &fscache_object_list);
  59. write_unlock(&fscache_object_list_lock);
  60. }
  61. /**
  62. * fscache_object_destroy - Note that a cache object is about to be destroyed
  63. * @object: The object to be destroyed
  64. *
  65. * Note the imminent destruction and deallocation of a cache object record.
  66. */
  67. void fscache_object_destroy(struct fscache_object *obj)
  68. {
  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. EXPORT_SYMBOL(fscache_object_destroy);
  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, *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 *) ++(*_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. unsigned long config = data->config;
  147. uint16_t keylen, auxlen;
  148. char _type[3], *type;
  149. bool no_cookie;
  150. u8 *buf = data->buf, *p;
  151. if ((unsigned long) v == 1) {
  152. seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
  153. " EM EV F S"
  154. " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
  155. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  156. FSCACHE_OBJLIST_CONFIG_AUX))
  157. seq_puts(m, " ");
  158. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  159. seq_puts(m, "OBJECT_KEY");
  160. if ((config & (FSCACHE_OBJLIST_CONFIG_KEY |
  161. FSCACHE_OBJLIST_CONFIG_AUX)) ==
  162. (FSCACHE_OBJLIST_CONFIG_KEY | FSCACHE_OBJLIST_CONFIG_AUX))
  163. seq_puts(m, ", ");
  164. if (config & FSCACHE_OBJLIST_CONFIG_AUX)
  165. seq_puts(m, "AUX_DATA");
  166. seq_puts(m, "\n");
  167. return 0;
  168. }
  169. if ((unsigned long) v == 2) {
  170. seq_puts(m, "======== ======== ==== ===== === === === == ====="
  171. " == == = ="
  172. " | ================ == == ================");
  173. if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
  174. FSCACHE_OBJLIST_CONFIG_AUX))
  175. seq_puts(m, " ================");
  176. seq_puts(m, "\n");
  177. return 0;
  178. }
  179. /* filter out any unwanted objects */
  180. #define FILTER(criterion, _yes, _no) \
  181. do { \
  182. unsigned long yes = FSCACHE_OBJLIST_CONFIG_##_yes; \
  183. unsigned long no = FSCACHE_OBJLIST_CONFIG_##_no; \
  184. if (criterion) { \
  185. if (!(config & yes)) \
  186. return 0; \
  187. } else { \
  188. if (!(config & no)) \
  189. return 0; \
  190. } \
  191. } while(0)
  192. if (~config) {
  193. FILTER(obj->cookie,
  194. COOKIE, NOCOOKIE);
  195. FILTER(obj->state != FSCACHE_OBJECT_ACTIVE ||
  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(obj->work.flags & ~(1UL << SLOW_WORK_VERY_SLOW),
  208. WORK, NOWORK);
  209. }
  210. seq_printf(m,
  211. "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %1lx %1lx | ",
  212. obj->debug_id,
  213. obj->parent ? obj->parent->debug_id : -1,
  214. fscache_object_states_short[obj->state],
  215. obj->n_children,
  216. obj->n_ops,
  217. obj->n_obj_ops,
  218. obj->n_in_progress,
  219. obj->n_exclusive,
  220. atomic_read(&obj->n_reads),
  221. obj->event_mask & FSCACHE_OBJECT_EVENTS_MASK,
  222. obj->events,
  223. obj->flags,
  224. obj->work.flags);
  225. no_cookie = true;
  226. keylen = auxlen = 0;
  227. if (obj->cookie) {
  228. spin_lock(&obj->lock);
  229. if (obj->cookie) {
  230. switch (obj->cookie->def->type) {
  231. case 0:
  232. type = "IX";
  233. break;
  234. case 1:
  235. type = "DT";
  236. break;
  237. default:
  238. sprintf(_type, "%02u",
  239. obj->cookie->def->type);
  240. type = _type;
  241. break;
  242. }
  243. seq_printf(m, "%-16s %s %2lx %16p",
  244. obj->cookie->def->name,
  245. type,
  246. obj->cookie->flags,
  247. obj->cookie->netfs_data);
  248. if (obj->cookie->def->get_key &&
  249. config & FSCACHE_OBJLIST_CONFIG_KEY)
  250. keylen = obj->cookie->def->get_key(
  251. obj->cookie->netfs_data,
  252. buf, 400);
  253. if (obj->cookie->def->get_aux &&
  254. config & FSCACHE_OBJLIST_CONFIG_AUX)
  255. auxlen = obj->cookie->def->get_aux(
  256. obj->cookie->netfs_data,
  257. buf + keylen, 512 - keylen);
  258. no_cookie = false;
  259. }
  260. spin_unlock(&obj->lock);
  261. if (!no_cookie && (keylen > 0 || auxlen > 0)) {
  262. seq_printf(m, " ");
  263. for (p = buf; keylen > 0; keylen--)
  264. seq_printf(m, "%02x", *p++);
  265. if (auxlen > 0) {
  266. if (config & FSCACHE_OBJLIST_CONFIG_KEY)
  267. seq_printf(m, ", ");
  268. for (; auxlen > 0; auxlen--)
  269. seq_printf(m, "%02x", *p++);
  270. }
  271. }
  272. }
  273. if (no_cookie)
  274. seq_printf(m, "<no_cookie>\n");
  275. else
  276. seq_printf(m, "\n");
  277. return 0;
  278. }
  279. static const struct seq_operations fscache_objlist_ops = {
  280. .start = fscache_objlist_start,
  281. .stop = fscache_objlist_stop,
  282. .next = fscache_objlist_next,
  283. .show = fscache_objlist_show,
  284. };
  285. /*
  286. * get the configuration for filtering the list
  287. */
  288. static void fscache_objlist_config(struct fscache_objlist_data *data)
  289. {
  290. #ifdef CONFIG_KEYS
  291. struct user_key_payload *confkey;
  292. unsigned long config;
  293. struct key *key;
  294. const char *buf;
  295. int len;
  296. key = request_key(&key_type_user, "fscache:objlist", NULL);
  297. if (IS_ERR(key))
  298. goto no_config;
  299. config = 0;
  300. rcu_read_lock();
  301. confkey = key->payload.data;
  302. buf = confkey->data;
  303. for (len = confkey->datalen - 1; len >= 0; len--) {
  304. switch (buf[len]) {
  305. case 'K': config |= FSCACHE_OBJLIST_CONFIG_KEY; break;
  306. case 'A': config |= FSCACHE_OBJLIST_CONFIG_AUX; break;
  307. case 'C': config |= FSCACHE_OBJLIST_CONFIG_COOKIE; break;
  308. case 'c': config |= FSCACHE_OBJLIST_CONFIG_NOCOOKIE; break;
  309. case 'B': config |= FSCACHE_OBJLIST_CONFIG_BUSY; break;
  310. case 'b': config |= FSCACHE_OBJLIST_CONFIG_IDLE; break;
  311. case 'W': config |= FSCACHE_OBJLIST_CONFIG_PENDWR; break;
  312. case 'w': config |= FSCACHE_OBJLIST_CONFIG_NOPENDWR; break;
  313. case 'R': config |= FSCACHE_OBJLIST_CONFIG_READS; break;
  314. case 'r': config |= FSCACHE_OBJLIST_CONFIG_NOREADS; break;
  315. case 'S': config |= FSCACHE_OBJLIST_CONFIG_WORK; break;
  316. case 's': config |= FSCACHE_OBJLIST_CONFIG_NOWORK; break;
  317. }
  318. }
  319. rcu_read_unlock();
  320. key_put(key);
  321. if (!(config & (FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE)))
  322. config |= FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE;
  323. if (!(config & (FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE)))
  324. config |= FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE;
  325. if (!(config & (FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR)))
  326. config |= FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR;
  327. if (!(config & (FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS)))
  328. config |= FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS;
  329. if (!(config & (FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS)))
  330. config |= FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS;
  331. if (!(config & (FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK)))
  332. config |= FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK;
  333. data->config = config;
  334. return;
  335. no_config:
  336. #endif
  337. data->config = ULONG_MAX;
  338. }
  339. /*
  340. * open "/proc/fs/fscache/objects" to provide a list of active objects
  341. * - can be configured by a user-defined key added to the caller's keyrings
  342. */
  343. static int fscache_objlist_open(struct inode *inode, struct file *file)
  344. {
  345. struct fscache_objlist_data *data;
  346. struct seq_file *m;
  347. int ret;
  348. ret = seq_open(file, &fscache_objlist_ops);
  349. if (ret < 0)
  350. return ret;
  351. m = file->private_data;
  352. /* buffer for key extraction */
  353. data = kmalloc(sizeof(struct fscache_objlist_data), GFP_KERNEL);
  354. if (!data) {
  355. seq_release(inode, file);
  356. return -ENOMEM;
  357. }
  358. /* get the configuration key */
  359. fscache_objlist_config(data);
  360. m->private = data;
  361. return 0;
  362. }
  363. /*
  364. * clean up on close
  365. */
  366. static int fscache_objlist_release(struct inode *inode, struct file *file)
  367. {
  368. struct seq_file *m = file->private_data;
  369. kfree(m->private);
  370. m->private = NULL;
  371. return seq_release(inode, file);
  372. }
  373. const struct file_operations fscache_objlist_fops = {
  374. .owner = THIS_MODULE,
  375. .open = fscache_objlist_open,
  376. .read = seq_read,
  377. .llseek = seq_lseek,
  378. .release = fscache_objlist_release,
  379. };