1 /* Global fscache object list maintainer and viewer
3 * Copyright (C) 2009 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define FSCACHE_DEBUG_LEVEL COOKIE
13 #include <linux/module.h>
14 #include <linux/seq_file.h>
15 #include <linux/key.h>
16 #include <keys/user-type.h>
19 static struct rb_root fscache_object_list
;
20 static DEFINE_RWLOCK(fscache_object_list_lock
);
22 struct fscache_objlist_data
{
23 unsigned long config
; /* display configuration */
24 #define FSCACHE_OBJLIST_CONFIG_KEY 0x00000001 /* show object keys */
25 #define FSCACHE_OBJLIST_CONFIG_AUX 0x00000002 /* show object auxdata */
26 #define FSCACHE_OBJLIST_CONFIG_COOKIE 0x00000004 /* show objects with cookies */
27 #define FSCACHE_OBJLIST_CONFIG_NOCOOKIE 0x00000008 /* show objects without cookies */
28 #define FSCACHE_OBJLIST_CONFIG_BUSY 0x00000010 /* show busy objects */
29 #define FSCACHE_OBJLIST_CONFIG_IDLE 0x00000020 /* show idle objects */
30 #define FSCACHE_OBJLIST_CONFIG_PENDWR 0x00000040 /* show objects with pending writes */
31 #define FSCACHE_OBJLIST_CONFIG_NOPENDWR 0x00000080 /* show objects without pending writes */
32 #define FSCACHE_OBJLIST_CONFIG_READS 0x00000100 /* show objects with active reads */
33 #define FSCACHE_OBJLIST_CONFIG_NOREADS 0x00000200 /* show objects without active reads */
34 #define FSCACHE_OBJLIST_CONFIG_EVENTS 0x00000400 /* show objects with events */
35 #define FSCACHE_OBJLIST_CONFIG_NOEVENTS 0x00000800 /* show objects without no events */
36 #define FSCACHE_OBJLIST_CONFIG_WORK 0x00001000 /* show objects with slow work */
37 #define FSCACHE_OBJLIST_CONFIG_NOWORK 0x00002000 /* show objects without slow work */
39 u8 buf
[512]; /* key and aux data buffer */
43 * Add an object to the object list
44 * - we use the address of the fscache_object structure as the key into the
47 void fscache_objlist_add(struct fscache_object
*obj
)
49 struct fscache_object
*xobj
;
50 struct rb_node
**p
= &fscache_object_list
.rb_node
, *parent
= NULL
;
52 write_lock(&fscache_object_list_lock
);
56 xobj
= rb_entry(parent
, struct fscache_object
, objlist_link
);
66 rb_link_node(&obj
->objlist_link
, parent
, p
);
67 rb_insert_color(&obj
->objlist_link
, &fscache_object_list
);
69 write_unlock(&fscache_object_list_lock
);
73 * fscache_object_destroy - Note that a cache object is about to be destroyed
74 * @object: The object to be destroyed
76 * Note the imminent destruction and deallocation of a cache object record.
78 void fscache_object_destroy(struct fscache_object
*obj
)
80 write_lock(&fscache_object_list_lock
);
82 BUG_ON(RB_EMPTY_ROOT(&fscache_object_list
));
83 rb_erase(&obj
->objlist_link
, &fscache_object_list
);
85 write_unlock(&fscache_object_list_lock
);
87 EXPORT_SYMBOL(fscache_object_destroy
);
90 * find the object in the tree on or after the specified index
92 static struct fscache_object
*fscache_objlist_lookup(loff_t
*_pos
)
94 struct fscache_object
*pobj
, *obj
= NULL
, *minobj
= NULL
;
98 if (*_pos
>= (unsigned long) ERR_PTR(-ENOENT
))
102 /* banners (can't represent line 0 by pos 0 as that would involve
103 * returning a NULL pointer) */
105 return (struct fscache_object
*) ++(*_pos
);
107 return (struct fscache_object
*)pos
;
109 pobj
= (struct fscache_object
*)pos
;
110 p
= fscache_object_list
.rb_node
;
112 obj
= rb_entry(p
, struct fscache_object
, objlist_link
);
114 if (!minobj
|| minobj
> obj
)
117 } else if (pobj
> obj
) {
127 *_pos
= (unsigned long) ERR_PTR(-ENOENT
);
128 else if (minobj
!= obj
)
129 *_pos
= (unsigned long) minobj
;
134 * set up the iterator to start reading from the first line
136 static void *fscache_objlist_start(struct seq_file
*m
, loff_t
*_pos
)
137 __acquires(&fscache_object_list_lock
)
139 read_lock(&fscache_object_list_lock
);
140 return fscache_objlist_lookup(_pos
);
144 * move to the next line
146 static void *fscache_objlist_next(struct seq_file
*m
, void *v
, loff_t
*_pos
)
149 return fscache_objlist_lookup(_pos
);
153 * clean up after reading
155 static void fscache_objlist_stop(struct seq_file
*m
, void *v
)
156 __releases(&fscache_object_list_lock
)
158 read_unlock(&fscache_object_list_lock
);
164 static int fscache_objlist_show(struct seq_file
*m
, void *v
)
166 struct fscache_objlist_data
*data
= m
->private;
167 struct fscache_object
*obj
= v
;
168 unsigned long config
= data
->config
;
169 uint16_t keylen
, auxlen
;
170 char _type
[3], *type
;
172 u8
*buf
= data
->buf
, *p
;
174 if ((unsigned long) v
== 1) {
175 seq_puts(m
, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
177 " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
178 if (config
& (FSCACHE_OBJLIST_CONFIG_KEY
|
179 FSCACHE_OBJLIST_CONFIG_AUX
))
181 if (config
& FSCACHE_OBJLIST_CONFIG_KEY
)
182 seq_puts(m
, "OBJECT_KEY");
183 if ((config
& (FSCACHE_OBJLIST_CONFIG_KEY
|
184 FSCACHE_OBJLIST_CONFIG_AUX
)) ==
185 (FSCACHE_OBJLIST_CONFIG_KEY
| FSCACHE_OBJLIST_CONFIG_AUX
))
187 if (config
& FSCACHE_OBJLIST_CONFIG_AUX
)
188 seq_puts(m
, "AUX_DATA");
193 if ((unsigned long) v
== 2) {
194 seq_puts(m
, "======== ======== ==== ===== === === === == ====="
196 " | ================ == == ================");
197 if (config
& (FSCACHE_OBJLIST_CONFIG_KEY
|
198 FSCACHE_OBJLIST_CONFIG_AUX
))
199 seq_puts(m
, " ================");
204 /* filter out any unwanted objects */
205 #define FILTER(criterion, _yes, _no) \
207 unsigned long yes = FSCACHE_OBJLIST_CONFIG_##_yes; \
208 unsigned long no = FSCACHE_OBJLIST_CONFIG_##_no; \
210 if (!(config & yes)) \
213 if (!(config & no)) \
221 FILTER(obj
->state
!= FSCACHE_OBJECT_ACTIVE
||
223 obj
->n_obj_ops
!= 0 ||
225 !list_empty(&obj
->dependents
),
227 FILTER(test_bit(FSCACHE_OBJECT_PENDING_WRITE
, &obj
->flags
),
229 FILTER(atomic_read(&obj
->n_reads
),
231 FILTER(obj
->events
& obj
->event_mask
,
233 FILTER(obj
->work
.flags
& ~(1UL << SLOW_WORK_VERY_SLOW
),
238 "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %1lx %1lx | ",
240 obj
->parent
? obj
->parent
->debug_id
: -1,
241 fscache_object_states_short
[obj
->state
],
247 atomic_read(&obj
->n_reads
),
248 obj
->event_mask
& FSCACHE_OBJECT_EVENTS_MASK
,
256 spin_lock(&obj
->lock
);
258 switch (obj
->cookie
->def
->type
) {
266 sprintf(_type
, "%02u",
267 obj
->cookie
->def
->type
);
272 seq_printf(m
, "%-16s %s %2lx %16p",
273 obj
->cookie
->def
->name
,
276 obj
->cookie
->netfs_data
);
278 if (obj
->cookie
->def
->get_key
&&
279 config
& FSCACHE_OBJLIST_CONFIG_KEY
)
280 keylen
= obj
->cookie
->def
->get_key(
281 obj
->cookie
->netfs_data
,
284 if (obj
->cookie
->def
->get_aux
&&
285 config
& FSCACHE_OBJLIST_CONFIG_AUX
)
286 auxlen
= obj
->cookie
->def
->get_aux(
287 obj
->cookie
->netfs_data
,
288 buf
+ keylen
, 512 - keylen
);
292 spin_unlock(&obj
->lock
);
294 if (!no_cookie
&& (keylen
> 0 || auxlen
> 0)) {
296 for (p
= buf
; keylen
> 0; keylen
--)
297 seq_printf(m
, "%02x", *p
++);
299 if (config
& FSCACHE_OBJLIST_CONFIG_KEY
)
301 for (; auxlen
> 0; auxlen
--)
302 seq_printf(m
, "%02x", *p
++);
308 seq_printf(m
, "<no_cookie>\n");
314 static const struct seq_operations fscache_objlist_ops
= {
315 .start
= fscache_objlist_start
,
316 .stop
= fscache_objlist_stop
,
317 .next
= fscache_objlist_next
,
318 .show
= fscache_objlist_show
,
322 * get the configuration for filtering the list
324 static void fscache_objlist_config(struct fscache_objlist_data
*data
)
327 struct user_key_payload
*confkey
;
328 unsigned long config
;
333 key
= request_key(&key_type_user
, "fscache:objlist", NULL
);
340 confkey
= key
->payload
.data
;
343 for (len
= confkey
->datalen
- 1; len
>= 0; len
--) {
345 case 'K': config
|= FSCACHE_OBJLIST_CONFIG_KEY
; break;
346 case 'A': config
|= FSCACHE_OBJLIST_CONFIG_AUX
; break;
347 case 'C': config
|= FSCACHE_OBJLIST_CONFIG_COOKIE
; break;
348 case 'c': config
|= FSCACHE_OBJLIST_CONFIG_NOCOOKIE
; break;
349 case 'B': config
|= FSCACHE_OBJLIST_CONFIG_BUSY
; break;
350 case 'b': config
|= FSCACHE_OBJLIST_CONFIG_IDLE
; break;
351 case 'W': config
|= FSCACHE_OBJLIST_CONFIG_PENDWR
; break;
352 case 'w': config
|= FSCACHE_OBJLIST_CONFIG_NOPENDWR
; break;
353 case 'R': config
|= FSCACHE_OBJLIST_CONFIG_READS
; break;
354 case 'r': config
|= FSCACHE_OBJLIST_CONFIG_NOREADS
; break;
355 case 'S': config
|= FSCACHE_OBJLIST_CONFIG_WORK
; break;
356 case 's': config
|= FSCACHE_OBJLIST_CONFIG_NOWORK
; break;
363 if (!(config
& (FSCACHE_OBJLIST_CONFIG_COOKIE
| FSCACHE_OBJLIST_CONFIG_NOCOOKIE
)))
364 config
|= FSCACHE_OBJLIST_CONFIG_COOKIE
| FSCACHE_OBJLIST_CONFIG_NOCOOKIE
;
365 if (!(config
& (FSCACHE_OBJLIST_CONFIG_BUSY
| FSCACHE_OBJLIST_CONFIG_IDLE
)))
366 config
|= FSCACHE_OBJLIST_CONFIG_BUSY
| FSCACHE_OBJLIST_CONFIG_IDLE
;
367 if (!(config
& (FSCACHE_OBJLIST_CONFIG_PENDWR
| FSCACHE_OBJLIST_CONFIG_NOPENDWR
)))
368 config
|= FSCACHE_OBJLIST_CONFIG_PENDWR
| FSCACHE_OBJLIST_CONFIG_NOPENDWR
;
369 if (!(config
& (FSCACHE_OBJLIST_CONFIG_READS
| FSCACHE_OBJLIST_CONFIG_NOREADS
)))
370 config
|= FSCACHE_OBJLIST_CONFIG_READS
| FSCACHE_OBJLIST_CONFIG_NOREADS
;
371 if (!(config
& (FSCACHE_OBJLIST_CONFIG_EVENTS
| FSCACHE_OBJLIST_CONFIG_NOEVENTS
)))
372 config
|= FSCACHE_OBJLIST_CONFIG_EVENTS
| FSCACHE_OBJLIST_CONFIG_NOEVENTS
;
373 if (!(config
& (FSCACHE_OBJLIST_CONFIG_WORK
| FSCACHE_OBJLIST_CONFIG_NOWORK
)))
374 config
|= FSCACHE_OBJLIST_CONFIG_WORK
| FSCACHE_OBJLIST_CONFIG_NOWORK
;
376 data
->config
= config
;
381 data
->config
= ULONG_MAX
;
385 * open "/proc/fs/fscache/objects" to provide a list of active objects
386 * - can be configured by a user-defined key added to the caller's keyrings
388 static int fscache_objlist_open(struct inode
*inode
, struct file
*file
)
390 struct fscache_objlist_data
*data
;
394 ret
= seq_open(file
, &fscache_objlist_ops
);
398 m
= file
->private_data
;
400 /* buffer for key extraction */
401 data
= kmalloc(sizeof(struct fscache_objlist_data
), GFP_KERNEL
);
403 seq_release(inode
, file
);
407 /* get the configuration key */
408 fscache_objlist_config(data
);
417 static int fscache_objlist_release(struct inode
*inode
, struct file
*file
)
419 struct seq_file
*m
= file
->private_data
;
423 return seq_release(inode
, file
);
426 const struct file_operations fscache_objlist_fops
= {
427 .owner
= THIS_MODULE
,
428 .open
= fscache_objlist_open
,
431 .release
= fscache_objlist_release
,