xref: /linux/drivers/md/bcache/debug.c (revision cafe563591446cf80bfbc2fe3bc72a2e36cf1060)
1*cafe5635SKent Overstreet /*
2*cafe5635SKent Overstreet  * Assorted bcache debug code
3*cafe5635SKent Overstreet  *
4*cafe5635SKent Overstreet  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5*cafe5635SKent Overstreet  * Copyright 2012 Google, Inc.
6*cafe5635SKent Overstreet  */
7*cafe5635SKent Overstreet 
8*cafe5635SKent Overstreet #include "bcache.h"
9*cafe5635SKent Overstreet #include "btree.h"
10*cafe5635SKent Overstreet #include "debug.h"
11*cafe5635SKent Overstreet #include "request.h"
12*cafe5635SKent Overstreet 
13*cafe5635SKent Overstreet #include <linux/console.h>
14*cafe5635SKent Overstreet #include <linux/debugfs.h>
15*cafe5635SKent Overstreet #include <linux/module.h>
16*cafe5635SKent Overstreet #include <linux/random.h>
17*cafe5635SKent Overstreet #include <linux/seq_file.h>
18*cafe5635SKent Overstreet 
19*cafe5635SKent Overstreet static struct dentry *debug;
20*cafe5635SKent Overstreet 
21*cafe5635SKent Overstreet const char *bch_ptr_status(struct cache_set *c, const struct bkey *k)
22*cafe5635SKent Overstreet {
23*cafe5635SKent Overstreet 	unsigned i;
24*cafe5635SKent Overstreet 
25*cafe5635SKent Overstreet 	for (i = 0; i < KEY_PTRS(k); i++)
26*cafe5635SKent Overstreet 		if (ptr_available(c, k, i)) {
27*cafe5635SKent Overstreet 			struct cache *ca = PTR_CACHE(c, k, i);
28*cafe5635SKent Overstreet 			size_t bucket = PTR_BUCKET_NR(c, k, i);
29*cafe5635SKent Overstreet 			size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
30*cafe5635SKent Overstreet 
31*cafe5635SKent Overstreet 			if (KEY_SIZE(k) + r > c->sb.bucket_size)
32*cafe5635SKent Overstreet 				return "bad, length too big";
33*cafe5635SKent Overstreet 			if (bucket <  ca->sb.first_bucket)
34*cafe5635SKent Overstreet 				return "bad, short offset";
35*cafe5635SKent Overstreet 			if (bucket >= ca->sb.nbuckets)
36*cafe5635SKent Overstreet 				return "bad, offset past end of device";
37*cafe5635SKent Overstreet 			if (ptr_stale(c, k, i))
38*cafe5635SKent Overstreet 				return "stale";
39*cafe5635SKent Overstreet 		}
40*cafe5635SKent Overstreet 
41*cafe5635SKent Overstreet 	if (!bkey_cmp(k, &ZERO_KEY))
42*cafe5635SKent Overstreet 		return "bad, null key";
43*cafe5635SKent Overstreet 	if (!KEY_PTRS(k))
44*cafe5635SKent Overstreet 		return "bad, no pointers";
45*cafe5635SKent Overstreet 	if (!KEY_SIZE(k))
46*cafe5635SKent Overstreet 		return "zeroed key";
47*cafe5635SKent Overstreet 	return "";
48*cafe5635SKent Overstreet }
49*cafe5635SKent Overstreet 
50*cafe5635SKent Overstreet struct keyprint_hack bch_pkey(const struct bkey *k)
51*cafe5635SKent Overstreet {
52*cafe5635SKent Overstreet 	unsigned i = 0;
53*cafe5635SKent Overstreet 	struct keyprint_hack r;
54*cafe5635SKent Overstreet 	char *out = r.s, *end = r.s + KEYHACK_SIZE;
55*cafe5635SKent Overstreet 
56*cafe5635SKent Overstreet #define p(...)	(out += scnprintf(out, end - out, __VA_ARGS__))
57*cafe5635SKent Overstreet 
58*cafe5635SKent Overstreet 	p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_OFFSET(k), KEY_SIZE(k));
59*cafe5635SKent Overstreet 
60*cafe5635SKent Overstreet 	if (KEY_PTRS(k))
61*cafe5635SKent Overstreet 		while (1) {
62*cafe5635SKent Overstreet 			p("%llu:%llu gen %llu",
63*cafe5635SKent Overstreet 			  PTR_DEV(k, i), PTR_OFFSET(k, i), PTR_GEN(k, i));
64*cafe5635SKent Overstreet 
65*cafe5635SKent Overstreet 			if (++i == KEY_PTRS(k))
66*cafe5635SKent Overstreet 				break;
67*cafe5635SKent Overstreet 
68*cafe5635SKent Overstreet 			p(", ");
69*cafe5635SKent Overstreet 		}
70*cafe5635SKent Overstreet 
71*cafe5635SKent Overstreet 	p("]");
72*cafe5635SKent Overstreet 
73*cafe5635SKent Overstreet 	if (KEY_DIRTY(k))
74*cafe5635SKent Overstreet 		p(" dirty");
75*cafe5635SKent Overstreet 	if (KEY_CSUM(k))
76*cafe5635SKent Overstreet 		p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]);
77*cafe5635SKent Overstreet #undef p
78*cafe5635SKent Overstreet 	return r;
79*cafe5635SKent Overstreet }
80*cafe5635SKent Overstreet 
81*cafe5635SKent Overstreet struct keyprint_hack bch_pbtree(const struct btree *b)
82*cafe5635SKent Overstreet {
83*cafe5635SKent Overstreet 	struct keyprint_hack r;
84*cafe5635SKent Overstreet 
85*cafe5635SKent Overstreet 	snprintf(r.s, 40, "%li level %i/%i", PTR_BUCKET_NR(b->c, &b->key, 0),
86*cafe5635SKent Overstreet 		 b->level, b->c->root ? b->c->root->level : -1);
87*cafe5635SKent Overstreet 	return r;
88*cafe5635SKent Overstreet }
89*cafe5635SKent Overstreet 
90*cafe5635SKent Overstreet #if defined(CONFIG_BCACHE_DEBUG) || defined(CONFIG_BCACHE_EDEBUG)
91*cafe5635SKent Overstreet 
92*cafe5635SKent Overstreet static bool skipped_backwards(struct btree *b, struct bkey *k)
93*cafe5635SKent Overstreet {
94*cafe5635SKent Overstreet 	return bkey_cmp(k, (!b->level)
95*cafe5635SKent Overstreet 			? &START_KEY(bkey_next(k))
96*cafe5635SKent Overstreet 			: bkey_next(k)) > 0;
97*cafe5635SKent Overstreet }
98*cafe5635SKent Overstreet 
99*cafe5635SKent Overstreet static void dump_bset(struct btree *b, struct bset *i)
100*cafe5635SKent Overstreet {
101*cafe5635SKent Overstreet 	struct bkey *k;
102*cafe5635SKent Overstreet 	unsigned j;
103*cafe5635SKent Overstreet 
104*cafe5635SKent Overstreet 	for (k = i->start; k < end(i); k = bkey_next(k)) {
105*cafe5635SKent Overstreet 		printk(KERN_ERR "block %zu key %zi/%u: %s", index(i, b),
106*cafe5635SKent Overstreet 		       (uint64_t *) k - i->d, i->keys, pkey(k));
107*cafe5635SKent Overstreet 
108*cafe5635SKent Overstreet 		for (j = 0; j < KEY_PTRS(k); j++) {
109*cafe5635SKent Overstreet 			size_t n = PTR_BUCKET_NR(b->c, k, j);
110*cafe5635SKent Overstreet 			printk(" bucket %zu", n);
111*cafe5635SKent Overstreet 
112*cafe5635SKent Overstreet 			if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
113*cafe5635SKent Overstreet 				printk(" prio %i",
114*cafe5635SKent Overstreet 				       PTR_BUCKET(b->c, k, j)->prio);
115*cafe5635SKent Overstreet 		}
116*cafe5635SKent Overstreet 
117*cafe5635SKent Overstreet 		printk(" %s\n", bch_ptr_status(b->c, k));
118*cafe5635SKent Overstreet 
119*cafe5635SKent Overstreet 		if (bkey_next(k) < end(i) &&
120*cafe5635SKent Overstreet 		    skipped_backwards(b, k))
121*cafe5635SKent Overstreet 			printk(KERN_ERR "Key skipped backwards\n");
122*cafe5635SKent Overstreet 	}
123*cafe5635SKent Overstreet }
124*cafe5635SKent Overstreet 
125*cafe5635SKent Overstreet #endif
126*cafe5635SKent Overstreet 
127*cafe5635SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
128*cafe5635SKent Overstreet 
129*cafe5635SKent Overstreet void bch_btree_verify(struct btree *b, struct bset *new)
130*cafe5635SKent Overstreet {
131*cafe5635SKent Overstreet 	struct btree *v = b->c->verify_data;
132*cafe5635SKent Overstreet 	struct closure cl;
133*cafe5635SKent Overstreet 	closure_init_stack(&cl);
134*cafe5635SKent Overstreet 
135*cafe5635SKent Overstreet 	if (!b->c->verify)
136*cafe5635SKent Overstreet 		return;
137*cafe5635SKent Overstreet 
138*cafe5635SKent Overstreet 	closure_wait_event(&b->io.wait, &cl,
139*cafe5635SKent Overstreet 			   atomic_read(&b->io.cl.remaining) == -1);
140*cafe5635SKent Overstreet 
141*cafe5635SKent Overstreet 	mutex_lock(&b->c->verify_lock);
142*cafe5635SKent Overstreet 
143*cafe5635SKent Overstreet 	bkey_copy(&v->key, &b->key);
144*cafe5635SKent Overstreet 	v->written = 0;
145*cafe5635SKent Overstreet 	v->level = b->level;
146*cafe5635SKent Overstreet 
147*cafe5635SKent Overstreet 	bch_btree_read(v);
148*cafe5635SKent Overstreet 	closure_wait_event(&v->io.wait, &cl,
149*cafe5635SKent Overstreet 			   atomic_read(&b->io.cl.remaining) == -1);
150*cafe5635SKent Overstreet 
151*cafe5635SKent Overstreet 	if (new->keys != v->sets[0].data->keys ||
152*cafe5635SKent Overstreet 	    memcmp(new->start,
153*cafe5635SKent Overstreet 		   v->sets[0].data->start,
154*cafe5635SKent Overstreet 		   (void *) end(new) - (void *) new->start)) {
155*cafe5635SKent Overstreet 		unsigned i, j;
156*cafe5635SKent Overstreet 
157*cafe5635SKent Overstreet 		console_lock();
158*cafe5635SKent Overstreet 
159*cafe5635SKent Overstreet 		printk(KERN_ERR "*** original memory node:\n");
160*cafe5635SKent Overstreet 		for (i = 0; i <= b->nsets; i++)
161*cafe5635SKent Overstreet 			dump_bset(b, b->sets[i].data);
162*cafe5635SKent Overstreet 
163*cafe5635SKent Overstreet 		printk(KERN_ERR "*** sorted memory node:\n");
164*cafe5635SKent Overstreet 		dump_bset(b, new);
165*cafe5635SKent Overstreet 
166*cafe5635SKent Overstreet 		printk(KERN_ERR "*** on disk node:\n");
167*cafe5635SKent Overstreet 		dump_bset(v, v->sets[0].data);
168*cafe5635SKent Overstreet 
169*cafe5635SKent Overstreet 		for (j = 0; j < new->keys; j++)
170*cafe5635SKent Overstreet 			if (new->d[j] != v->sets[0].data->d[j])
171*cafe5635SKent Overstreet 				break;
172*cafe5635SKent Overstreet 
173*cafe5635SKent Overstreet 		console_unlock();
174*cafe5635SKent Overstreet 		panic("verify failed at %u\n", j);
175*cafe5635SKent Overstreet 	}
176*cafe5635SKent Overstreet 
177*cafe5635SKent Overstreet 	mutex_unlock(&b->c->verify_lock);
178*cafe5635SKent Overstreet }
179*cafe5635SKent Overstreet 
180*cafe5635SKent Overstreet static void data_verify_endio(struct bio *bio, int error)
181*cafe5635SKent Overstreet {
182*cafe5635SKent Overstreet 	struct closure *cl = bio->bi_private;
183*cafe5635SKent Overstreet 	closure_put(cl);
184*cafe5635SKent Overstreet }
185*cafe5635SKent Overstreet 
186*cafe5635SKent Overstreet void bch_data_verify(struct search *s)
187*cafe5635SKent Overstreet {
188*cafe5635SKent Overstreet 	char name[BDEVNAME_SIZE];
189*cafe5635SKent Overstreet 	struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
190*cafe5635SKent Overstreet 	struct closure *cl = &s->cl;
191*cafe5635SKent Overstreet 	struct bio *check;
192*cafe5635SKent Overstreet 	struct bio_vec *bv;
193*cafe5635SKent Overstreet 	int i;
194*cafe5635SKent Overstreet 
195*cafe5635SKent Overstreet 	if (!s->unaligned_bvec)
196*cafe5635SKent Overstreet 		bio_for_each_segment(bv, s->orig_bio, i)
197*cafe5635SKent Overstreet 			bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
198*cafe5635SKent Overstreet 
199*cafe5635SKent Overstreet 	check = bio_clone(s->orig_bio, GFP_NOIO);
200*cafe5635SKent Overstreet 	if (!check)
201*cafe5635SKent Overstreet 		return;
202*cafe5635SKent Overstreet 
203*cafe5635SKent Overstreet 	if (bio_alloc_pages(check, GFP_NOIO))
204*cafe5635SKent Overstreet 		goto out_put;
205*cafe5635SKent Overstreet 
206*cafe5635SKent Overstreet 	check->bi_rw		= READ_SYNC;
207*cafe5635SKent Overstreet 	check->bi_private	= cl;
208*cafe5635SKent Overstreet 	check->bi_end_io	= data_verify_endio;
209*cafe5635SKent Overstreet 
210*cafe5635SKent Overstreet 	closure_bio_submit(check, cl, &dc->disk);
211*cafe5635SKent Overstreet 	closure_sync(cl);
212*cafe5635SKent Overstreet 
213*cafe5635SKent Overstreet 	bio_for_each_segment(bv, s->orig_bio, i) {
214*cafe5635SKent Overstreet 		void *p1 = kmap(bv->bv_page);
215*cafe5635SKent Overstreet 		void *p2 = kmap(check->bi_io_vec[i].bv_page);
216*cafe5635SKent Overstreet 
217*cafe5635SKent Overstreet 		if (memcmp(p1 + bv->bv_offset,
218*cafe5635SKent Overstreet 			   p2 + bv->bv_offset,
219*cafe5635SKent Overstreet 			   bv->bv_len))
220*cafe5635SKent Overstreet 			printk(KERN_ERR "bcache (%s): verify failed"
221*cafe5635SKent Overstreet 			       " at sector %llu\n",
222*cafe5635SKent Overstreet 			       bdevname(dc->bdev, name),
223*cafe5635SKent Overstreet 			       (uint64_t) s->orig_bio->bi_sector);
224*cafe5635SKent Overstreet 
225*cafe5635SKent Overstreet 		kunmap(bv->bv_page);
226*cafe5635SKent Overstreet 		kunmap(check->bi_io_vec[i].bv_page);
227*cafe5635SKent Overstreet 	}
228*cafe5635SKent Overstreet 
229*cafe5635SKent Overstreet 	__bio_for_each_segment(bv, check, i, 0)
230*cafe5635SKent Overstreet 		__free_page(bv->bv_page);
231*cafe5635SKent Overstreet out_put:
232*cafe5635SKent Overstreet 	bio_put(check);
233*cafe5635SKent Overstreet }
234*cafe5635SKent Overstreet 
235*cafe5635SKent Overstreet #endif
236*cafe5635SKent Overstreet 
237*cafe5635SKent Overstreet #ifdef CONFIG_BCACHE_EDEBUG
238*cafe5635SKent Overstreet 
239*cafe5635SKent Overstreet unsigned bch_count_data(struct btree *b)
240*cafe5635SKent Overstreet {
241*cafe5635SKent Overstreet 	unsigned ret = 0;
242*cafe5635SKent Overstreet 	struct btree_iter iter;
243*cafe5635SKent Overstreet 	struct bkey *k;
244*cafe5635SKent Overstreet 
245*cafe5635SKent Overstreet 	if (!b->level)
246*cafe5635SKent Overstreet 		for_each_key(b, k, &iter)
247*cafe5635SKent Overstreet 			ret += KEY_SIZE(k);
248*cafe5635SKent Overstreet 	return ret;
249*cafe5635SKent Overstreet }
250*cafe5635SKent Overstreet 
251*cafe5635SKent Overstreet static void vdump_bucket_and_panic(struct btree *b, const char *fmt,
252*cafe5635SKent Overstreet 				   va_list args)
253*cafe5635SKent Overstreet {
254*cafe5635SKent Overstreet 	unsigned i;
255*cafe5635SKent Overstreet 
256*cafe5635SKent Overstreet 	console_lock();
257*cafe5635SKent Overstreet 
258*cafe5635SKent Overstreet 	for (i = 0; i <= b->nsets; i++)
259*cafe5635SKent Overstreet 		dump_bset(b, b->sets[i].data);
260*cafe5635SKent Overstreet 
261*cafe5635SKent Overstreet 	vprintk(fmt, args);
262*cafe5635SKent Overstreet 
263*cafe5635SKent Overstreet 	console_unlock();
264*cafe5635SKent Overstreet 
265*cafe5635SKent Overstreet 	panic("at %s\n", pbtree(b));
266*cafe5635SKent Overstreet }
267*cafe5635SKent Overstreet 
268*cafe5635SKent Overstreet void bch_check_key_order_msg(struct btree *b, struct bset *i,
269*cafe5635SKent Overstreet 			     const char *fmt, ...)
270*cafe5635SKent Overstreet {
271*cafe5635SKent Overstreet 	struct bkey *k;
272*cafe5635SKent Overstreet 
273*cafe5635SKent Overstreet 	if (!i->keys)
274*cafe5635SKent Overstreet 		return;
275*cafe5635SKent Overstreet 
276*cafe5635SKent Overstreet 	for (k = i->start; bkey_next(k) < end(i); k = bkey_next(k))
277*cafe5635SKent Overstreet 		if (skipped_backwards(b, k)) {
278*cafe5635SKent Overstreet 			va_list args;
279*cafe5635SKent Overstreet 			va_start(args, fmt);
280*cafe5635SKent Overstreet 
281*cafe5635SKent Overstreet 			vdump_bucket_and_panic(b, fmt, args);
282*cafe5635SKent Overstreet 			va_end(args);
283*cafe5635SKent Overstreet 		}
284*cafe5635SKent Overstreet }
285*cafe5635SKent Overstreet 
286*cafe5635SKent Overstreet void bch_check_keys(struct btree *b, const char *fmt, ...)
287*cafe5635SKent Overstreet {
288*cafe5635SKent Overstreet 	va_list args;
289*cafe5635SKent Overstreet 	struct bkey *k, *p = NULL;
290*cafe5635SKent Overstreet 	struct btree_iter iter;
291*cafe5635SKent Overstreet 
292*cafe5635SKent Overstreet 	if (b->level)
293*cafe5635SKent Overstreet 		return;
294*cafe5635SKent Overstreet 
295*cafe5635SKent Overstreet 	for_each_key(b, k, &iter) {
296*cafe5635SKent Overstreet 		if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) {
297*cafe5635SKent Overstreet 			printk(KERN_ERR "Keys out of order:\n");
298*cafe5635SKent Overstreet 			goto bug;
299*cafe5635SKent Overstreet 		}
300*cafe5635SKent Overstreet 
301*cafe5635SKent Overstreet 		if (bch_ptr_invalid(b, k))
302*cafe5635SKent Overstreet 			continue;
303*cafe5635SKent Overstreet 
304*cafe5635SKent Overstreet 		if (p && bkey_cmp(p, &START_KEY(k)) > 0) {
305*cafe5635SKent Overstreet 			printk(KERN_ERR "Overlapping keys:\n");
306*cafe5635SKent Overstreet 			goto bug;
307*cafe5635SKent Overstreet 		}
308*cafe5635SKent Overstreet 		p = k;
309*cafe5635SKent Overstreet 	}
310*cafe5635SKent Overstreet 	return;
311*cafe5635SKent Overstreet bug:
312*cafe5635SKent Overstreet 	va_start(args, fmt);
313*cafe5635SKent Overstreet 	vdump_bucket_and_panic(b, fmt, args);
314*cafe5635SKent Overstreet 	va_end(args);
315*cafe5635SKent Overstreet }
316*cafe5635SKent Overstreet 
317*cafe5635SKent Overstreet #endif
318*cafe5635SKent Overstreet 
319*cafe5635SKent Overstreet #ifdef CONFIG_DEBUG_FS
320*cafe5635SKent Overstreet 
321*cafe5635SKent Overstreet /* XXX: cache set refcounting */
322*cafe5635SKent Overstreet 
323*cafe5635SKent Overstreet struct dump_iterator {
324*cafe5635SKent Overstreet 	char			buf[PAGE_SIZE];
325*cafe5635SKent Overstreet 	size_t			bytes;
326*cafe5635SKent Overstreet 	struct cache_set	*c;
327*cafe5635SKent Overstreet 	struct keybuf		keys;
328*cafe5635SKent Overstreet };
329*cafe5635SKent Overstreet 
330*cafe5635SKent Overstreet static bool dump_pred(struct keybuf *buf, struct bkey *k)
331*cafe5635SKent Overstreet {
332*cafe5635SKent Overstreet 	return true;
333*cafe5635SKent Overstreet }
334*cafe5635SKent Overstreet 
335*cafe5635SKent Overstreet static ssize_t bch_dump_read(struct file *file, char __user *buf,
336*cafe5635SKent Overstreet 			     size_t size, loff_t *ppos)
337*cafe5635SKent Overstreet {
338*cafe5635SKent Overstreet 	struct dump_iterator *i = file->private_data;
339*cafe5635SKent Overstreet 	ssize_t ret = 0;
340*cafe5635SKent Overstreet 
341*cafe5635SKent Overstreet 	while (size) {
342*cafe5635SKent Overstreet 		struct keybuf_key *w;
343*cafe5635SKent Overstreet 		unsigned bytes = min(i->bytes, size);
344*cafe5635SKent Overstreet 
345*cafe5635SKent Overstreet 		int err = copy_to_user(buf, i->buf, bytes);
346*cafe5635SKent Overstreet 		if (err)
347*cafe5635SKent Overstreet 			return err;
348*cafe5635SKent Overstreet 
349*cafe5635SKent Overstreet 		ret	 += bytes;
350*cafe5635SKent Overstreet 		buf	 += bytes;
351*cafe5635SKent Overstreet 		size	 -= bytes;
352*cafe5635SKent Overstreet 		i->bytes -= bytes;
353*cafe5635SKent Overstreet 		memmove(i->buf, i->buf + bytes, i->bytes);
354*cafe5635SKent Overstreet 
355*cafe5635SKent Overstreet 		if (i->bytes)
356*cafe5635SKent Overstreet 			break;
357*cafe5635SKent Overstreet 
358*cafe5635SKent Overstreet 		w = bch_keybuf_next_rescan(i->c, &i->keys, &MAX_KEY);
359*cafe5635SKent Overstreet 		if (!w)
360*cafe5635SKent Overstreet 			break;
361*cafe5635SKent Overstreet 
362*cafe5635SKent Overstreet 		i->bytes = snprintf(i->buf, PAGE_SIZE, "%s\n", pkey(&w->key));
363*cafe5635SKent Overstreet 		bch_keybuf_del(&i->keys, w);
364*cafe5635SKent Overstreet 	}
365*cafe5635SKent Overstreet 
366*cafe5635SKent Overstreet 	return ret;
367*cafe5635SKent Overstreet }
368*cafe5635SKent Overstreet 
369*cafe5635SKent Overstreet static int bch_dump_open(struct inode *inode, struct file *file)
370*cafe5635SKent Overstreet {
371*cafe5635SKent Overstreet 	struct cache_set *c = inode->i_private;
372*cafe5635SKent Overstreet 	struct dump_iterator *i;
373*cafe5635SKent Overstreet 
374*cafe5635SKent Overstreet 	i = kzalloc(sizeof(struct dump_iterator), GFP_KERNEL);
375*cafe5635SKent Overstreet 	if (!i)
376*cafe5635SKent Overstreet 		return -ENOMEM;
377*cafe5635SKent Overstreet 
378*cafe5635SKent Overstreet 	file->private_data = i;
379*cafe5635SKent Overstreet 	i->c = c;
380*cafe5635SKent Overstreet 	bch_keybuf_init(&i->keys, dump_pred);
381*cafe5635SKent Overstreet 	i->keys.last_scanned = KEY(0, 0, 0);
382*cafe5635SKent Overstreet 
383*cafe5635SKent Overstreet 	return 0;
384*cafe5635SKent Overstreet }
385*cafe5635SKent Overstreet 
386*cafe5635SKent Overstreet static int bch_dump_release(struct inode *inode, struct file *file)
387*cafe5635SKent Overstreet {
388*cafe5635SKent Overstreet 	kfree(file->private_data);
389*cafe5635SKent Overstreet 	return 0;
390*cafe5635SKent Overstreet }
391*cafe5635SKent Overstreet 
392*cafe5635SKent Overstreet static const struct file_operations cache_set_debug_ops = {
393*cafe5635SKent Overstreet 	.owner		= THIS_MODULE,
394*cafe5635SKent Overstreet 	.open		= bch_dump_open,
395*cafe5635SKent Overstreet 	.read		= bch_dump_read,
396*cafe5635SKent Overstreet 	.release	= bch_dump_release
397*cafe5635SKent Overstreet };
398*cafe5635SKent Overstreet 
399*cafe5635SKent Overstreet void bch_debug_init_cache_set(struct cache_set *c)
400*cafe5635SKent Overstreet {
401*cafe5635SKent Overstreet 	if (!IS_ERR_OR_NULL(debug)) {
402*cafe5635SKent Overstreet 		char name[50];
403*cafe5635SKent Overstreet 		snprintf(name, 50, "bcache-%pU", c->sb.set_uuid);
404*cafe5635SKent Overstreet 
405*cafe5635SKent Overstreet 		c->debug = debugfs_create_file(name, 0400, debug, c,
406*cafe5635SKent Overstreet 					       &cache_set_debug_ops);
407*cafe5635SKent Overstreet 	}
408*cafe5635SKent Overstreet }
409*cafe5635SKent Overstreet 
410*cafe5635SKent Overstreet #endif
411*cafe5635SKent Overstreet 
412*cafe5635SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
413*cafe5635SKent Overstreet static ssize_t btree_fuzz(struct kobject *k, struct kobj_attribute *a,
414*cafe5635SKent Overstreet 			  const char *buffer, size_t size)
415*cafe5635SKent Overstreet {
416*cafe5635SKent Overstreet 	void dump(struct btree *b)
417*cafe5635SKent Overstreet 	{
418*cafe5635SKent Overstreet 		struct bset *i;
419*cafe5635SKent Overstreet 
420*cafe5635SKent Overstreet 		for (i = b->sets[0].data;
421*cafe5635SKent Overstreet 		     index(i, b) < btree_blocks(b) &&
422*cafe5635SKent Overstreet 		     i->seq == b->sets[0].data->seq;
423*cafe5635SKent Overstreet 		     i = ((void *) i) + set_blocks(i, b->c) * block_bytes(b->c))
424*cafe5635SKent Overstreet 			dump_bset(b, i);
425*cafe5635SKent Overstreet 	}
426*cafe5635SKent Overstreet 
427*cafe5635SKent Overstreet 	struct cache_sb *sb;
428*cafe5635SKent Overstreet 	struct cache_set *c;
429*cafe5635SKent Overstreet 	struct btree *all[3], *b, *fill, *orig;
430*cafe5635SKent Overstreet 	int j;
431*cafe5635SKent Overstreet 
432*cafe5635SKent Overstreet 	struct btree_op op;
433*cafe5635SKent Overstreet 	bch_btree_op_init_stack(&op);
434*cafe5635SKent Overstreet 
435*cafe5635SKent Overstreet 	sb = kzalloc(sizeof(struct cache_sb), GFP_KERNEL);
436*cafe5635SKent Overstreet 	if (!sb)
437*cafe5635SKent Overstreet 		return -ENOMEM;
438*cafe5635SKent Overstreet 
439*cafe5635SKent Overstreet 	sb->bucket_size = 128;
440*cafe5635SKent Overstreet 	sb->block_size = 4;
441*cafe5635SKent Overstreet 
442*cafe5635SKent Overstreet 	c = bch_cache_set_alloc(sb);
443*cafe5635SKent Overstreet 	if (!c)
444*cafe5635SKent Overstreet 		return -ENOMEM;
445*cafe5635SKent Overstreet 
446*cafe5635SKent Overstreet 	for (j = 0; j < 3; j++) {
447*cafe5635SKent Overstreet 		BUG_ON(list_empty(&c->btree_cache));
448*cafe5635SKent Overstreet 		all[j] = list_first_entry(&c->btree_cache, struct btree, list);
449*cafe5635SKent Overstreet 		list_del_init(&all[j]->list);
450*cafe5635SKent Overstreet 
451*cafe5635SKent Overstreet 		all[j]->key = KEY(0, 0, c->sb.bucket_size);
452*cafe5635SKent Overstreet 		bkey_copy_key(&all[j]->key, &MAX_KEY);
453*cafe5635SKent Overstreet 	}
454*cafe5635SKent Overstreet 
455*cafe5635SKent Overstreet 	b = all[0];
456*cafe5635SKent Overstreet 	fill = all[1];
457*cafe5635SKent Overstreet 	orig = all[2];
458*cafe5635SKent Overstreet 
459*cafe5635SKent Overstreet 	while (1) {
460*cafe5635SKent Overstreet 		for (j = 0; j < 3; j++)
461*cafe5635SKent Overstreet 			all[j]->written = all[j]->nsets = 0;
462*cafe5635SKent Overstreet 
463*cafe5635SKent Overstreet 		bch_bset_init_next(b);
464*cafe5635SKent Overstreet 
465*cafe5635SKent Overstreet 		while (1) {
466*cafe5635SKent Overstreet 			struct bset *i = write_block(b);
467*cafe5635SKent Overstreet 			struct bkey *k = op.keys.top;
468*cafe5635SKent Overstreet 			unsigned rand;
469*cafe5635SKent Overstreet 
470*cafe5635SKent Overstreet 			bkey_init(k);
471*cafe5635SKent Overstreet 			rand = get_random_int();
472*cafe5635SKent Overstreet 
473*cafe5635SKent Overstreet 			op.type = rand & 1
474*cafe5635SKent Overstreet 				? BTREE_INSERT
475*cafe5635SKent Overstreet 				: BTREE_REPLACE;
476*cafe5635SKent Overstreet 			rand >>= 1;
477*cafe5635SKent Overstreet 
478*cafe5635SKent Overstreet 			SET_KEY_SIZE(k, bucket_remainder(c, rand));
479*cafe5635SKent Overstreet 			rand >>= c->bucket_bits;
480*cafe5635SKent Overstreet 			rand &= 1024 * 512 - 1;
481*cafe5635SKent Overstreet 			rand += c->sb.bucket_size;
482*cafe5635SKent Overstreet 			SET_KEY_OFFSET(k, rand);
483*cafe5635SKent Overstreet #if 0
484*cafe5635SKent Overstreet 			SET_KEY_PTRS(k, 1);
485*cafe5635SKent Overstreet #endif
486*cafe5635SKent Overstreet 			bch_keylist_push(&op.keys);
487*cafe5635SKent Overstreet 			bch_btree_insert_keys(b, &op);
488*cafe5635SKent Overstreet 
489*cafe5635SKent Overstreet 			if (should_split(b) ||
490*cafe5635SKent Overstreet 			    set_blocks(i, b->c) !=
491*cafe5635SKent Overstreet 			    __set_blocks(i, i->keys + 15, b->c)) {
492*cafe5635SKent Overstreet 				i->csum = csum_set(i);
493*cafe5635SKent Overstreet 
494*cafe5635SKent Overstreet 				memcpy(write_block(fill),
495*cafe5635SKent Overstreet 				       i, set_bytes(i));
496*cafe5635SKent Overstreet 
497*cafe5635SKent Overstreet 				b->written += set_blocks(i, b->c);
498*cafe5635SKent Overstreet 				fill->written = b->written;
499*cafe5635SKent Overstreet 				if (b->written == btree_blocks(b))
500*cafe5635SKent Overstreet 					break;
501*cafe5635SKent Overstreet 
502*cafe5635SKent Overstreet 				bch_btree_sort_lazy(b);
503*cafe5635SKent Overstreet 				bch_bset_init_next(b);
504*cafe5635SKent Overstreet 			}
505*cafe5635SKent Overstreet 		}
506*cafe5635SKent Overstreet 
507*cafe5635SKent Overstreet 		memcpy(orig->sets[0].data,
508*cafe5635SKent Overstreet 		       fill->sets[0].data,
509*cafe5635SKent Overstreet 		       btree_bytes(c));
510*cafe5635SKent Overstreet 
511*cafe5635SKent Overstreet 		bch_btree_sort(b);
512*cafe5635SKent Overstreet 		fill->written = 0;
513*cafe5635SKent Overstreet 		bch_btree_read_done(&fill->io.cl);
514*cafe5635SKent Overstreet 
515*cafe5635SKent Overstreet 		if (b->sets[0].data->keys != fill->sets[0].data->keys ||
516*cafe5635SKent Overstreet 		    memcmp(b->sets[0].data->start,
517*cafe5635SKent Overstreet 			   fill->sets[0].data->start,
518*cafe5635SKent Overstreet 			   b->sets[0].data->keys * sizeof(uint64_t))) {
519*cafe5635SKent Overstreet 			struct bset *i = b->sets[0].data;
520*cafe5635SKent Overstreet 			struct bkey *k, *l;
521*cafe5635SKent Overstreet 
522*cafe5635SKent Overstreet 			for (k = i->start,
523*cafe5635SKent Overstreet 			     l = fill->sets[0].data->start;
524*cafe5635SKent Overstreet 			     k < end(i);
525*cafe5635SKent Overstreet 			     k = bkey_next(k), l = bkey_next(l))
526*cafe5635SKent Overstreet 				if (bkey_cmp(k, l) ||
527*cafe5635SKent Overstreet 				    KEY_SIZE(k) != KEY_SIZE(l))
528*cafe5635SKent Overstreet 					pr_err("key %zi differs: %s "
529*cafe5635SKent Overstreet 					       "!= %s", (uint64_t *) k - i->d,
530*cafe5635SKent Overstreet 					       pkey(k), pkey(l));
531*cafe5635SKent Overstreet 
532*cafe5635SKent Overstreet 			for (j = 0; j < 3; j++) {
533*cafe5635SKent Overstreet 				pr_err("**** Set %i ****", j);
534*cafe5635SKent Overstreet 				dump(all[j]);
535*cafe5635SKent Overstreet 			}
536*cafe5635SKent Overstreet 			panic("\n");
537*cafe5635SKent Overstreet 		}
538*cafe5635SKent Overstreet 
539*cafe5635SKent Overstreet 		pr_info("fuzz complete: %i keys", b->sets[0].data->keys);
540*cafe5635SKent Overstreet 	}
541*cafe5635SKent Overstreet }
542*cafe5635SKent Overstreet 
543*cafe5635SKent Overstreet kobj_attribute_write(fuzz, btree_fuzz);
544*cafe5635SKent Overstreet #endif
545*cafe5635SKent Overstreet 
546*cafe5635SKent Overstreet void bch_debug_exit(void)
547*cafe5635SKent Overstreet {
548*cafe5635SKent Overstreet 	if (!IS_ERR_OR_NULL(debug))
549*cafe5635SKent Overstreet 		debugfs_remove_recursive(debug);
550*cafe5635SKent Overstreet }
551*cafe5635SKent Overstreet 
552*cafe5635SKent Overstreet int __init bch_debug_init(struct kobject *kobj)
553*cafe5635SKent Overstreet {
554*cafe5635SKent Overstreet 	int ret = 0;
555*cafe5635SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
556*cafe5635SKent Overstreet 	ret = sysfs_create_file(kobj, &ksysfs_fuzz.attr);
557*cafe5635SKent Overstreet 	if (ret)
558*cafe5635SKent Overstreet 		return ret;
559*cafe5635SKent Overstreet #endif
560*cafe5635SKent Overstreet 
561*cafe5635SKent Overstreet 	debug = debugfs_create_dir("bcache", NULL);
562*cafe5635SKent Overstreet 	return ret;
563*cafe5635SKent Overstreet }
564