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 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. 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 *)(long)++(*_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(work_busy(&obj->work), WORK, NOWORK);
  209. }
  210. seq_printf(m,
  211. "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %1lx %1x | ",
  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. work_busy(&obj->work));
  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. };