object-list.c 11 KB

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