debug.c 8.63 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
3
4
5
6
7
8
9
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2005-2019 Junjiro R. Okajima
 */

/*
 * debug print functions
 */

J. R. Okajima's avatar
J. R. Okajima committed
10
#include <linux/iversion.h>
J. R. Okajima's avatar
J. R. Okajima committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "aufs.h"

/* Returns 0, or -errno.  arg is in kp->arg. */
static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
{
	int err, n;

	err = kstrtoint(val, 0, &n);
	if (!err) {
		if (n > 0)
			au_debug_on();
		else
			au_debug_off();
	}
	return err;
}

/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
{
	atomic_t *a;

	a = kp->arg;
	return sprintf(buffer, "%d", atomic_read(a));
}

static struct kernel_param_ops param_ops_atomic_t = {
	.set = param_atomic_t_set,
	.get = param_atomic_t_get
	/* void (*free)(void *arg) */
};

atomic_t aufs_debug = ATOMIC_INIT(0);
MODULE_PARM_DESC(debug, "debug print");
module_param_named(debug, aufs_debug, atomic_t, 0664);
J. R. Okajima's avatar
J. R. Okajima committed
46
47
48
49
50
51
52
53
54
55
56
57

DEFINE_MUTEX(au_dbg_mtx);	/* just to serialize the dbg msgs */
char *au_plevel = KERN_DEBUG;
#define dpri(fmt, ...) do {					\
	if ((au_plevel						\
	     && strcmp(au_plevel, KERN_DEBUG))			\
	    || au_debug_test())					\
		printk("%s" fmt, au_plevel, ##__VA_ARGS__);	\
} while (0)

/* ---------------------------------------------------------------------- */

J. R. Okajima's avatar
J. R. Okajima committed
58
static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
J. R. Okajima's avatar
J. R. Okajima committed
59
			struct dentry *wh)
J. R. Okajima's avatar
J. R. Okajima committed
60
{
J. R. Okajima's avatar
J. R. Okajima committed
61
62
63
	char *n = NULL;
	int l = 0;

J. R. Okajima's avatar
J. R. Okajima committed
64
65
66
67
68
69
70
71
	if (!inode || IS_ERR(inode)) {
		dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
		return -1;
	}

	/* the type of i_blocks depends upon CONFIG_LBDAF */
	BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
		     && sizeof(inode->i_blocks) != sizeof(u64));
J. R. Okajima's avatar
J. R. Okajima committed
72
73
74
75
	if (wh) {
		n = (void *)wh->d_name.name;
		l = wh->d_name.len;
	}
J. R. Okajima's avatar
J. R. Okajima committed
76
77

	dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
J. R. Okajima's avatar
J. R. Okajima committed
78
	     " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
J. R. Okajima's avatar
J. R. Okajima committed
79
80
81
82
	     bindex, inode,
	     inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
	     atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
	     i_size_read(inode), (unsigned long long)inode->i_blocks,
J. R. Okajima's avatar
J. R. Okajima committed
83
	     hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
J. R. Okajima's avatar
J. R. Okajima committed
84
85
	     inode->i_mapping ? inode->i_mapping->nrpages : 0,
	     inode->i_state, inode->i_flags, inode_peek_iversion(inode),
J. R. Okajima's avatar
J. R. Okajima committed
86
87
	     inode->i_generation,
	     l ? ", wh " : "", l, n);
J. R. Okajima's avatar
J. R. Okajima committed
88
89
90
91
92
93
	return 0;
}

void au_dpri_inode(struct inode *inode)
{
	struct au_iinfo *iinfo;
J. R. Okajima's avatar
J. R. Okajima committed
94
	struct au_hinode *hi;
J. R. Okajima's avatar
J. R. Okajima committed
95
	aufs_bindex_t bindex;
J. R. Okajima's avatar
J. R. Okajima committed
96
	int err, hn;
J. R. Okajima's avatar
J. R. Okajima committed
97

J. R. Okajima's avatar
J. R. Okajima committed
98
	err = do_pri_inode(-1, inode, -1, NULL);
J. R. Okajima's avatar
J. R. Okajima committed
99
100
101
102
	if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
		return;

	iinfo = au_ii(inode);
J. R. Okajima's avatar
J. R. Okajima committed
103
	dpri("i-1: btop %d, bbot %d, gen %d\n",
J. R. Okajima's avatar
J. R. Okajima committed
104
	     iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
J. R. Okajima's avatar
J. R. Okajima committed
105
106
	if (iinfo->ii_btop < 0)
		return;
J. R. Okajima's avatar
J. R. Okajima committed
107
108
109
110
111
112
	hn = 0;
	for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
		hi = au_hinode(iinfo, bindex);
		hn = !!au_hn(hi);
		do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
	}
J. R. Okajima's avatar
J. R. Okajima committed
113
}
J. R. Okajima's avatar
J. R. Okajima committed
114
115
116
117
118
119
120
121
122
123
124
125
126

void au_dpri_dalias(struct inode *inode)
{
	struct dentry *d;

	spin_lock(&inode->i_lock);
	hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
		au_dpri_dentry(d);
	spin_unlock(&inode->i_lock);
}

static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
{
J. R. Okajima's avatar
J. R. Okajima committed
127
	struct dentry *wh = NULL;
J. R. Okajima's avatar
J. R. Okajima committed
128
	int hn;
J. R. Okajima's avatar
J. R. Okajima committed
129
130
131
132
	struct inode *inode;
	struct au_iinfo *iinfo;
	struct au_hinode *hi;

J. R. Okajima's avatar
J. R. Okajima committed
133
134
135
136
137
138
139
140
141
142
143
	if (!dentry || IS_ERR(dentry)) {
		dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
		return -1;
	}
	/* do not call dget_parent() here */
	/* note: access d_xxx without d_lock */
	dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
	     bindex, dentry, dentry,
	     dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
	     au_dcount(dentry), dentry->d_flags,
	     d_unhashed(dentry) ? "un" : "");
J. R. Okajima's avatar
J. R. Okajima committed
144
	hn = -1;
J. R. Okajima's avatar
J. R. Okajima committed
145
146
147
148
149
150
151
152
153
	inode = NULL;
	if (d_is_positive(dentry))
		inode = d_inode(dentry);
	if (inode
	    && au_test_aufs(dentry->d_sb)
	    && bindex >= 0
	    && !au_is_bad_inode(inode)) {
		iinfo = au_ii(inode);
		hi = au_hinode(iinfo, bindex);
J. R. Okajima's avatar
J. R. Okajima committed
154
		hn = !!au_hn(hi);
J. R. Okajima's avatar
J. R. Okajima committed
155
156
		wh = hi->hi_whdentry;
	}
J. R. Okajima's avatar
J. R. Okajima committed
157
	do_pri_inode(bindex, inode, hn, wh);
J. R. Okajima's avatar
J. R. Okajima committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	return 0;
}

void au_dpri_dentry(struct dentry *dentry)
{
	struct au_dinfo *dinfo;
	aufs_bindex_t bindex;
	int err;

	err = do_pri_dentry(-1, dentry);
	if (err || !au_test_aufs(dentry->d_sb))
		return;

	dinfo = au_di(dentry);
	if (!dinfo)
		return;
J. R. Okajima's avatar
J. R. Okajima committed
174
	dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d\n",
J. R. Okajima's avatar
J. R. Okajima committed
175
	     dinfo->di_btop, dinfo->di_bbot,
J. R. Okajima's avatar
J. R. Okajima committed
176
	     dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry));
J. R. Okajima's avatar
J. R. Okajima committed
177
178
179
180
181
182
	if (dinfo->di_btop < 0)
		return;
	for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
		do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
}

J. R. Okajima's avatar
J. R. Okajima committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
static int do_pri_file(aufs_bindex_t bindex, struct file *file)
{
	char a[32];

	if (!file || IS_ERR(file)) {
		dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
		return -1;
	}
	a[0] = 0;
	if (bindex < 0
	    && !IS_ERR_OR_NULL(file->f_path.dentry)
	    && au_test_aufs(file->f_path.dentry->d_sb)
	    && au_fi(file))
		snprintf(a, sizeof(a), ", gen %d",
			 au_figen(file));
	dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
	     bindex, file->f_mode, file->f_flags, (long)file_count(file),
	     file->f_version, file->f_pos, a);
	if (!IS_ERR_OR_NULL(file->f_path.dentry))
		do_pri_dentry(bindex, file->f_path.dentry);
	return 0;
}

void au_dpri_file(struct file *file)
{
	struct au_finfo *finfo;
	int err;

	err = do_pri_file(-1, file);
	if (err
	    || IS_ERR_OR_NULL(file->f_path.dentry)
	    || !au_test_aufs(file->f_path.dentry->d_sb))
		return;

	finfo = au_fi(file);
	if (!finfo)
		return;
	if (finfo->fi_btop < 0)
		return;
	do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
}

225
226
227
228
229
230
231
232
233
234
235
236
237
238
static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
{
	struct vfsmount *mnt;
	struct super_block *sb;

	if (!br || IS_ERR(br))
		goto out;
	mnt = au_br_mnt(br);
	if (!mnt || IS_ERR(mnt))
		goto out;
	sb = mnt->mnt_sb;
	if (!sb || IS_ERR(sb))
		goto out;

239
	dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
J. R. Okajima's avatar
J. R. Okajima committed
240
241
	     "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
	     "xino %d\n",
242
	     bindex, br->br_perm, br->br_id, br->br_wbr,
243
244
	     au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
	     sb->s_flags, sb->s_count,
J. R. Okajima's avatar
J. R. Okajima committed
245
246
	     atomic_read(&sb->s_active),
	     !!au_xino_file(br->br_xino, /*idx*/-1));
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	return 0;

out:
	dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
	return -1;
}

void au_dpri_sb(struct super_block *sb)
{
	struct au_sbinfo *sbinfo;
	aufs_bindex_t bindex;
	int err;
	/* to reduce stack size */
	struct {
		struct vfsmount mnt;
		struct au_branch fake;
	} *a;

	/* this function can be called from magic sysrq */
	a = kzalloc(sizeof(*a), GFP_ATOMIC);
	if (unlikely(!a)) {
		dpri("no memory\n");
		return;
	}

	a->mnt.mnt_sb = sb;
	a->fake.br_path.mnt = &a->mnt;
	err = do_pri_br(-1, &a->fake);
	au_kfree_rcu(a);
	dpri("dev 0x%x\n", sb->s_dev);
	if (err || !au_test_aufs(sb))
		return;

	sbinfo = au_sbi(sb);
	if (!sbinfo)
		return;
J. R. Okajima's avatar
J. R. Okajima committed
283
284
	dpri("nw %d, gen %u, kobj %d\n",
	     atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
285
286
287
288
289
	     kref_read(&sbinfo->si_kobj.kref));
	for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
		do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
}

J. R. Okajima's avatar
J. R. Okajima committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* ---------------------------------------------------------------------- */

void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
{
	struct inode *h_inode, *inode = d_inode(dentry);
	struct dentry *h_dentry;
	aufs_bindex_t bindex, bbot, bi;

	if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
		return;

	bbot = au_dbbot(dentry);
	bi = au_ibbot(inode);
	if (bi < bbot)
		bbot = bi;
	bindex = au_dbtop(dentry);
	bi = au_ibtop(inode);
	if (bi > bindex)
		bindex = bi;

	for (; bindex <= bbot; bindex++) {
		h_dentry = au_h_dptr(dentry, bindex);
		if (!h_dentry)
			continue;
		h_inode = au_h_iptr(inode, bindex);
		if (unlikely(h_inode != d_inode(h_dentry))) {
			au_debug_on();
			AuDbg("b%d, %s:%d\n", bindex, func, line);
			AuDbgDentry(dentry);
			AuDbgInode(inode);
			au_debug_off();
			BUG();
		}
	}
}
J. R. Okajima's avatar
J. R. Okajima committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
{
	int err, i, j;
	struct au_dcsub_pages dpages;
	struct au_dpage *dpage;
	struct dentry **dentries;

	err = au_dpages_init(&dpages, GFP_NOFS);
	AuDebugOn(err);
	err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
	AuDebugOn(err);
	for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
		dpage = dpages.dpages + i;
		dentries = dpage->dentries;
		for (j = dpage->ndentry - 1; !err && j >= 0; j--)
			AuDebugOn(au_digen_test(dentries[j], sigen));
	}
	au_dpages_free(&dpages);
}
J. R. Okajima's avatar
J. R. Okajima committed
345
346
347
348
349
350
351
352
353
354
355
356

void au_dbg_verify_kthread(void)
{
	if (au_wkq_test()) {
		/* au_dbg_blocked(); re-commit later */
		/*
		 * It may be recursive, but udba=notify between two aufs mounts,
		 * where a single ro branch is shared, is not a problem.
		 */
		/* WARN_ON(1); */
	}
}