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