blob: e1959efad64fa0f4101541683e8c422d27a37aa7 [file] [log] [blame]
David Howells4fbf4292009-11-19 18:11:04 +00001/* 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
12#define FSCACHE_DEBUG_LEVEL COOKIE
13#include <linux/module.h>
14#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Howells4fbf4292009-11-19 18:11:04 +000016#include <linux/key.h>
17#include <keys/user-type.h>
18#include "internal.h"
19
20static struct rb_root fscache_object_list;
21static DEFINE_RWLOCK(fscache_object_list_lock);
22
23struct fscache_objlist_data {
24 unsigned long config; /* display configuration */
25#define FSCACHE_OBJLIST_CONFIG_KEY 0x00000001 /* show object keys */
26#define FSCACHE_OBJLIST_CONFIG_AUX 0x00000002 /* show object auxdata */
27#define FSCACHE_OBJLIST_CONFIG_COOKIE 0x00000004 /* show objects with cookies */
28#define FSCACHE_OBJLIST_CONFIG_NOCOOKIE 0x00000008 /* show objects without cookies */
29#define FSCACHE_OBJLIST_CONFIG_BUSY 0x00000010 /* show busy objects */
30#define FSCACHE_OBJLIST_CONFIG_IDLE 0x00000020 /* show idle objects */
31#define FSCACHE_OBJLIST_CONFIG_PENDWR 0x00000040 /* show objects with pending writes */
32#define FSCACHE_OBJLIST_CONFIG_NOPENDWR 0x00000080 /* show objects without pending writes */
33#define FSCACHE_OBJLIST_CONFIG_READS 0x00000100 /* show objects with active reads */
34#define FSCACHE_OBJLIST_CONFIG_NOREADS 0x00000200 /* show objects without active reads */
35#define FSCACHE_OBJLIST_CONFIG_EVENTS 0x00000400 /* show objects with events */
36#define FSCACHE_OBJLIST_CONFIG_NOEVENTS 0x00000800 /* show objects without no events */
Tejun Heo8b8edef2010-07-20 22:09:01 +020037#define FSCACHE_OBJLIST_CONFIG_WORK 0x00001000 /* show objects with work */
38#define FSCACHE_OBJLIST_CONFIG_NOWORK 0x00002000 /* show objects without work */
David Howells4fbf4292009-11-19 18:11:04 +000039
40 u8 buf[512]; /* key and aux data buffer */
41};
42
43/*
44 * Add an object to the object list
45 * - we use the address of the fscache_object structure as the key into the
46 * tree
47 */
48void fscache_objlist_add(struct fscache_object *obj)
49{
50 struct fscache_object *xobj;
51 struct rb_node **p = &fscache_object_list.rb_node, *parent = NULL;
52
53 write_lock(&fscache_object_list_lock);
54
55 while (*p) {
56 parent = *p;
57 xobj = rb_entry(parent, struct fscache_object, objlist_link);
58
59 if (obj < xobj)
60 p = &(*p)->rb_left;
61 else if (obj > xobj)
62 p = &(*p)->rb_right;
63 else
64 BUG();
65 }
66
67 rb_link_node(&obj->objlist_link, parent, p);
68 rb_insert_color(&obj->objlist_link, &fscache_object_list);
69
70 write_unlock(&fscache_object_list_lock);
71}
72
David Howells13627292013-05-10 19:50:26 +010073/*
74 * Remove an object from the object list.
David Howells4fbf4292009-11-19 18:11:04 +000075 */
David Howells13627292013-05-10 19:50:26 +010076void fscache_objlist_remove(struct fscache_object *obj)
David Howells4fbf4292009-11-19 18:11:04 +000077{
78 write_lock(&fscache_object_list_lock);
79
80 BUG_ON(RB_EMPTY_ROOT(&fscache_object_list));
81 rb_erase(&obj->objlist_link, &fscache_object_list);
82
83 write_unlock(&fscache_object_list_lock);
84}
David Howells4fbf4292009-11-19 18:11:04 +000085
86/*
87 * find the object in the tree on or after the specified index
88 */
89static struct fscache_object *fscache_objlist_lookup(loff_t *_pos)
90{
David Howellsea58ceb2009-12-15 16:47:46 -080091 struct fscache_object *pobj, *obj = NULL, *minobj = NULL;
David Howells4fbf4292009-11-19 18:11:04 +000092 struct rb_node *p;
93 unsigned long pos;
94
95 if (*_pos >= (unsigned long) ERR_PTR(-ENOENT))
96 return NULL;
97 pos = *_pos;
98
99 /* banners (can't represent line 0 by pos 0 as that would involve
100 * returning a NULL pointer) */
101 if (pos == 0)
Andrew Mortoncc68e3b2010-05-25 23:43:02 -0700102 return (struct fscache_object *)(long)++(*_pos);
David Howells4fbf4292009-11-19 18:11:04 +0000103 if (pos < 3)
104 return (struct fscache_object *)pos;
105
106 pobj = (struct fscache_object *)pos;
107 p = fscache_object_list.rb_node;
108 while (p) {
109 obj = rb_entry(p, struct fscache_object, objlist_link);
110 if (pobj < obj) {
111 if (!minobj || minobj > obj)
112 minobj = obj;
113 p = p->rb_left;
114 } else if (pobj > obj) {
115 p = p->rb_right;
116 } else {
117 minobj = obj;
118 break;
119 }
120 obj = NULL;
121 }
122
123 if (!minobj)
124 *_pos = (unsigned long) ERR_PTR(-ENOENT);
125 else if (minobj != obj)
126 *_pos = (unsigned long) minobj;
127 return minobj;
128}
129
130/*
131 * set up the iterator to start reading from the first line
132 */
133static void *fscache_objlist_start(struct seq_file *m, loff_t *_pos)
134 __acquires(&fscache_object_list_lock)
135{
136 read_lock(&fscache_object_list_lock);
137 return fscache_objlist_lookup(_pos);
138}
139
140/*
141 * move to the next line
142 */
143static void *fscache_objlist_next(struct seq_file *m, void *v, loff_t *_pos)
144{
145 (*_pos)++;
146 return fscache_objlist_lookup(_pos);
147}
148
149/*
150 * clean up after reading
151 */
152static void fscache_objlist_stop(struct seq_file *m, void *v)
153 __releases(&fscache_object_list_lock)
154{
155 read_unlock(&fscache_object_list_lock);
156}
157
158/*
159 * display an object
160 */
161static int fscache_objlist_show(struct seq_file *m, void *v)
162{
163 struct fscache_objlist_data *data = m->private;
164 struct fscache_object *obj = v;
David Howells13627292013-05-10 19:50:26 +0100165 struct fscache_cookie *cookie;
David Howells4fbf4292009-11-19 18:11:04 +0000166 unsigned long config = data->config;
David Howells4fbf4292009-11-19 18:11:04 +0000167 char _type[3], *type;
David Howells4fbf4292009-11-19 18:11:04 +0000168 u8 *buf = data->buf, *p;
169
170 if ((unsigned long) v == 1) {
171 seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
David Howellscaaef692013-05-10 19:50:26 +0100172 " EM EV FL S"
David Howells4fbf4292009-11-19 18:11:04 +0000173 " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
174 if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
175 FSCACHE_OBJLIST_CONFIG_AUX))
176 seq_puts(m, " ");
177 if (config & FSCACHE_OBJLIST_CONFIG_KEY)
178 seq_puts(m, "OBJECT_KEY");
179 if ((config & (FSCACHE_OBJLIST_CONFIG_KEY |
180 FSCACHE_OBJLIST_CONFIG_AUX)) ==
181 (FSCACHE_OBJLIST_CONFIG_KEY | FSCACHE_OBJLIST_CONFIG_AUX))
182 seq_puts(m, ", ");
183 if (config & FSCACHE_OBJLIST_CONFIG_AUX)
184 seq_puts(m, "AUX_DATA");
185 seq_puts(m, "\n");
186 return 0;
187 }
188
189 if ((unsigned long) v == 2) {
190 seq_puts(m, "======== ======== ==== ===== === === === == ====="
David Howellscaaef692013-05-10 19:50:26 +0100191 " == == == ="
David Howells4fbf4292009-11-19 18:11:04 +0000192 " | ================ == == ================");
193 if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
194 FSCACHE_OBJLIST_CONFIG_AUX))
195 seq_puts(m, " ================");
196 seq_puts(m, "\n");
197 return 0;
198 }
199
200 /* filter out any unwanted objects */
201#define FILTER(criterion, _yes, _no) \
202 do { \
203 unsigned long yes = FSCACHE_OBJLIST_CONFIG_##_yes; \
204 unsigned long no = FSCACHE_OBJLIST_CONFIG_##_no; \
205 if (criterion) { \
206 if (!(config & yes)) \
207 return 0; \
208 } else { \
209 if (!(config & no)) \
210 return 0; \
211 } \
212 } while(0)
213
David Howells13627292013-05-10 19:50:26 +0100214 cookie = obj->cookie;
David Howells4fbf4292009-11-19 18:11:04 +0000215 if (~config) {
David Howells13627292013-05-10 19:50:26 +0100216 FILTER(cookie->def,
David Howells4fbf4292009-11-19 18:11:04 +0000217 COOKIE, NOCOOKIE);
David Howellscaaef692013-05-10 19:50:26 +0100218 FILTER(fscache_object_is_active(obj) ||
David Howells4fbf4292009-11-19 18:11:04 +0000219 obj->n_ops != 0 ||
220 obj->n_obj_ops != 0 ||
221 obj->flags ||
222 !list_empty(&obj->dependents),
223 BUSY, IDLE);
224 FILTER(test_bit(FSCACHE_OBJECT_PENDING_WRITE, &obj->flags),
225 PENDWR, NOPENDWR);
226 FILTER(atomic_read(&obj->n_reads),
227 READS, NOREADS);
228 FILTER(obj->events & obj->event_mask,
229 EVENTS, NOEVENTS);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200230 FILTER(work_busy(&obj->work), WORK, NOWORK);
David Howells4fbf4292009-11-19 18:11:04 +0000231 }
232
233 seq_printf(m,
David Howellscaaef692013-05-10 19:50:26 +0100234 "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %2lx %1x | ",
David Howells4fbf4292009-11-19 18:11:04 +0000235 obj->debug_id,
236 obj->parent ? obj->parent->debug_id : -1,
David Howellscaaef692013-05-10 19:50:26 +0100237 obj->state->short_name,
David Howells4fbf4292009-11-19 18:11:04 +0000238 obj->n_children,
239 obj->n_ops,
240 obj->n_obj_ops,
241 obj->n_in_progress,
242 obj->n_exclusive,
243 atomic_read(&obj->n_reads),
David Howellsc2d35bf2012-12-05 13:34:47 +0000244 obj->event_mask,
David Howells4fbf4292009-11-19 18:11:04 +0000245 obj->events,
246 obj->flags,
Tejun Heo8b8edef2010-07-20 22:09:01 +0200247 work_busy(&obj->work));
David Howells4fbf4292009-11-19 18:11:04 +0000248
David Howells13627292013-05-10 19:50:26 +0100249 if (fscache_use_cookie(obj)) {
250 uint16_t keylen = 0, auxlen = 0;
David Howells4fbf4292009-11-19 18:11:04 +0000251
David Howells13627292013-05-10 19:50:26 +0100252 switch (cookie->def->type) {
253 case 0:
254 type = "IX";
255 break;
256 case 1:
257 type = "DT";
258 break;
259 default:
260 sprintf(_type, "%02u", cookie->def->type);
261 type = _type;
262 break;
David Howells4fbf4292009-11-19 18:11:04 +0000263 }
David Howells4fbf4292009-11-19 18:11:04 +0000264
David Howells13627292013-05-10 19:50:26 +0100265 seq_printf(m, "%-16s %s %2lx %16p",
266 cookie->def->name,
267 type,
268 cookie->flags,
269 cookie->netfs_data);
270
271 if (cookie->def->get_key &&
272 config & FSCACHE_OBJLIST_CONFIG_KEY)
273 keylen = cookie->def->get_key(cookie->netfs_data,
274 buf, 400);
275
276 if (cookie->def->get_aux &&
277 config & FSCACHE_OBJLIST_CONFIG_AUX)
278 auxlen = cookie->def->get_aux(cookie->netfs_data,
279 buf + keylen, 512 - keylen);
280 fscache_unuse_cookie(obj);
281
282 if (keylen > 0 || auxlen > 0) {
David Howells4fbf4292009-11-19 18:11:04 +0000283 seq_printf(m, " ");
284 for (p = buf; keylen > 0; keylen--)
285 seq_printf(m, "%02x", *p++);
286 if (auxlen > 0) {
287 if (config & FSCACHE_OBJLIST_CONFIG_KEY)
288 seq_printf(m, ", ");
289 for (; auxlen > 0; auxlen--)
290 seq_printf(m, "%02x", *p++);
291 }
292 }
David Howells4fbf4292009-11-19 18:11:04 +0000293
David Howells4fbf4292009-11-19 18:11:04 +0000294 seq_printf(m, "\n");
David Howells13627292013-05-10 19:50:26 +0100295 } else {
296 seq_printf(m, "<no_netfs>\n");
297 }
David Howells4fbf4292009-11-19 18:11:04 +0000298 return 0;
299}
300
301static const struct seq_operations fscache_objlist_ops = {
302 .start = fscache_objlist_start,
303 .stop = fscache_objlist_stop,
304 .next = fscache_objlist_next,
305 .show = fscache_objlist_show,
306};
307
308/*
309 * get the configuration for filtering the list
310 */
311static void fscache_objlist_config(struct fscache_objlist_data *data)
312{
313#ifdef CONFIG_KEYS
314 struct user_key_payload *confkey;
315 unsigned long config;
316 struct key *key;
317 const char *buf;
318 int len;
319
320 key = request_key(&key_type_user, "fscache:objlist", NULL);
321 if (IS_ERR(key))
322 goto no_config;
323
324 config = 0;
325 rcu_read_lock();
326
327 confkey = key->payload.data;
328 buf = confkey->data;
329
330 for (len = confkey->datalen - 1; len >= 0; len--) {
331 switch (buf[len]) {
332 case 'K': config |= FSCACHE_OBJLIST_CONFIG_KEY; break;
333 case 'A': config |= FSCACHE_OBJLIST_CONFIG_AUX; break;
334 case 'C': config |= FSCACHE_OBJLIST_CONFIG_COOKIE; break;
335 case 'c': config |= FSCACHE_OBJLIST_CONFIG_NOCOOKIE; break;
336 case 'B': config |= FSCACHE_OBJLIST_CONFIG_BUSY; break;
337 case 'b': config |= FSCACHE_OBJLIST_CONFIG_IDLE; break;
338 case 'W': config |= FSCACHE_OBJLIST_CONFIG_PENDWR; break;
339 case 'w': config |= FSCACHE_OBJLIST_CONFIG_NOPENDWR; break;
340 case 'R': config |= FSCACHE_OBJLIST_CONFIG_READS; break;
341 case 'r': config |= FSCACHE_OBJLIST_CONFIG_NOREADS; break;
342 case 'S': config |= FSCACHE_OBJLIST_CONFIG_WORK; break;
343 case 's': config |= FSCACHE_OBJLIST_CONFIG_NOWORK; break;
344 }
345 }
346
347 rcu_read_unlock();
348 key_put(key);
349
350 if (!(config & (FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE)))
351 config |= FSCACHE_OBJLIST_CONFIG_COOKIE | FSCACHE_OBJLIST_CONFIG_NOCOOKIE;
352 if (!(config & (FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE)))
353 config |= FSCACHE_OBJLIST_CONFIG_BUSY | FSCACHE_OBJLIST_CONFIG_IDLE;
354 if (!(config & (FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR)))
355 config |= FSCACHE_OBJLIST_CONFIG_PENDWR | FSCACHE_OBJLIST_CONFIG_NOPENDWR;
356 if (!(config & (FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS)))
357 config |= FSCACHE_OBJLIST_CONFIG_READS | FSCACHE_OBJLIST_CONFIG_NOREADS;
358 if (!(config & (FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS)))
359 config |= FSCACHE_OBJLIST_CONFIG_EVENTS | FSCACHE_OBJLIST_CONFIG_NOEVENTS;
360 if (!(config & (FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK)))
361 config |= FSCACHE_OBJLIST_CONFIG_WORK | FSCACHE_OBJLIST_CONFIG_NOWORK;
362
363 data->config = config;
364 return;
365
366no_config:
367#endif
368 data->config = ULONG_MAX;
369}
370
371/*
372 * open "/proc/fs/fscache/objects" to provide a list of active objects
373 * - can be configured by a user-defined key added to the caller's keyrings
374 */
375static int fscache_objlist_open(struct inode *inode, struct file *file)
376{
377 struct fscache_objlist_data *data;
378 struct seq_file *m;
379 int ret;
380
381 ret = seq_open(file, &fscache_objlist_ops);
382 if (ret < 0)
383 return ret;
384
385 m = file->private_data;
386
387 /* buffer for key extraction */
388 data = kmalloc(sizeof(struct fscache_objlist_data), GFP_KERNEL);
389 if (!data) {
390 seq_release(inode, file);
391 return -ENOMEM;
392 }
393
394 /* get the configuration key */
395 fscache_objlist_config(data);
396
397 m->private = data;
398 return 0;
399}
400
401/*
402 * clean up on close
403 */
404static int fscache_objlist_release(struct inode *inode, struct file *file)
405{
406 struct seq_file *m = file->private_data;
407
408 kfree(m->private);
409 m->private = NULL;
410 return seq_release(inode, file);
411}
412
413const struct file_operations fscache_objlist_fops = {
414 .owner = THIS_MODULE,
415 .open = fscache_objlist_open,
416 .read = seq_read,
417 .llseek = seq_lseek,
418 .release = fscache_objlist_release,
419};