]>
Commit | Line | Data |
---|---|---|
0d331203 SF |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2005-2020 Junjiro R. Okajima | |
4 | * | |
5 | * This program, aufs is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | /* | |
20 | * debug print functions | |
21 | */ | |
22 | ||
23 | #include <linux/iversion.h> | |
24 | #include "aufs.h" | |
25 | ||
26 | /* Returns 0, or -errno. arg is in kp->arg. */ | |
27 | static int param_atomic_t_set(const char *val, const struct kernel_param *kp) | |
28 | { | |
29 | int err, n; | |
30 | ||
31 | err = kstrtoint(val, 0, &n); | |
32 | if (!err) { | |
33 | if (n > 0) | |
34 | au_debug_on(); | |
35 | else | |
36 | au_debug_off(); | |
37 | } | |
38 | return err; | |
39 | } | |
40 | ||
41 | /* Returns length written or -errno. Buffer is 4k (ie. be short!) */ | |
42 | static int param_atomic_t_get(char *buffer, const struct kernel_param *kp) | |
43 | { | |
44 | atomic_t *a; | |
45 | ||
46 | a = kp->arg; | |
47 | return sprintf(buffer, "%d", atomic_read(a)); | |
48 | } | |
49 | ||
50 | static struct kernel_param_ops param_ops_atomic_t = { | |
51 | .set = param_atomic_t_set, | |
52 | .get = param_atomic_t_get | |
53 | /* void (*free)(void *arg) */ | |
54 | }; | |
55 | ||
56 | atomic_t aufs_debug = ATOMIC_INIT(0); | |
57 | MODULE_PARM_DESC(debug, "debug print"); | |
58 | module_param_named(debug, aufs_debug, atomic_t, 0664); | |
59 | ||
60 | DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */ | |
61 | char *au_plevel = KERN_DEBUG; | |
62 | #define dpri(fmt, ...) do { \ | |
63 | if ((au_plevel \ | |
64 | && strcmp(au_plevel, KERN_DEBUG)) \ | |
65 | || au_debug_test()) \ | |
66 | printk("%s" fmt, au_plevel, ##__VA_ARGS__); \ | |
67 | } while (0) | |
68 | ||
69 | /* ---------------------------------------------------------------------- */ | |
70 | ||
71 | void au_dpri_whlist(struct au_nhash *whlist) | |
72 | { | |
73 | unsigned long ul, n; | |
74 | struct hlist_head *head; | |
75 | struct au_vdir_wh *pos; | |
76 | ||
77 | n = whlist->nh_num; | |
78 | head = whlist->nh_head; | |
79 | for (ul = 0; ul < n; ul++) { | |
80 | hlist_for_each_entry(pos, head, wh_hash) | |
81 | dpri("b%d, %.*s, %d\n", | |
82 | pos->wh_bindex, | |
83 | pos->wh_str.len, pos->wh_str.name, | |
84 | pos->wh_str.len); | |
85 | head++; | |
86 | } | |
87 | } | |
88 | ||
89 | void au_dpri_vdir(struct au_vdir *vdir) | |
90 | { | |
91 | unsigned long ul; | |
92 | union au_vdir_deblk_p p; | |
93 | unsigned char *o; | |
94 | ||
95 | if (!vdir || IS_ERR(vdir)) { | |
96 | dpri("err %ld\n", PTR_ERR(vdir)); | |
97 | return; | |
98 | } | |
99 | ||
100 | dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n", | |
101 | vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk, | |
102 | vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version); | |
103 | for (ul = 0; ul < vdir->vd_nblk; ul++) { | |
104 | p.deblk = vdir->vd_deblk[ul]; | |
105 | o = p.deblk; | |
106 | dpri("[%lu]: %p\n", ul, o); | |
107 | } | |
108 | } | |
109 | ||
110 | static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn, | |
111 | struct dentry *wh) | |
112 | { | |
113 | char *n = NULL; | |
114 | int l = 0; | |
115 | ||
116 | if (!inode || IS_ERR(inode)) { | |
117 | dpri("i%d: err %ld\n", bindex, PTR_ERR(inode)); | |
118 | return -1; | |
119 | } | |
120 | ||
121 | /* the type of i_blocks depends upon CONFIG_LBDAF */ | |
122 | BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long) | |
123 | && sizeof(inode->i_blocks) != sizeof(u64)); | |
124 | if (wh) { | |
125 | n = (void *)wh->d_name.name; | |
126 | l = wh->d_name.len; | |
127 | } | |
128 | ||
129 | dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu," | |
130 | " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n", | |
131 | bindex, inode, | |
132 | inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??", | |
133 | atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode, | |
134 | i_size_read(inode), (unsigned long long)inode->i_blocks, | |
135 | hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff, | |
136 | inode->i_mapping ? inode->i_mapping->nrpages : 0, | |
137 | inode->i_state, inode->i_flags, inode_peek_iversion(inode), | |
138 | inode->i_generation, | |
139 | l ? ", wh " : "", l, n); | |
140 | return 0; | |
141 | } | |
142 | ||
143 | void au_dpri_inode(struct inode *inode) | |
144 | { | |
145 | struct au_iinfo *iinfo; | |
146 | struct au_hinode *hi; | |
147 | aufs_bindex_t bindex; | |
148 | int err, hn; | |
149 | ||
150 | err = do_pri_inode(-1, inode, -1, NULL); | |
151 | if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode)) | |
152 | return; | |
153 | ||
154 | iinfo = au_ii(inode); | |
155 | dpri("i-1: btop %d, bbot %d, gen %d\n", | |
156 | iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL)); | |
157 | if (iinfo->ii_btop < 0) | |
158 | return; | |
159 | hn = 0; | |
160 | for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) { | |
161 | hi = au_hinode(iinfo, bindex); | |
162 | hn = !!au_hn(hi); | |
163 | do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry); | |
164 | } | |
165 | } | |
166 | ||
167 | void au_dpri_dalias(struct inode *inode) | |
168 | { | |
169 | struct dentry *d; | |
170 | ||
171 | spin_lock(&inode->i_lock); | |
172 | hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) | |
173 | au_dpri_dentry(d); | |
174 | spin_unlock(&inode->i_lock); | |
175 | } | |
176 | ||
177 | static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry) | |
178 | { | |
179 | struct dentry *wh = NULL; | |
180 | int hn; | |
181 | struct inode *inode; | |
182 | struct au_iinfo *iinfo; | |
183 | struct au_hinode *hi; | |
184 | ||
185 | if (!dentry || IS_ERR(dentry)) { | |
186 | dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry)); | |
187 | return -1; | |
188 | } | |
189 | /* do not call dget_parent() here */ | |
190 | /* note: access d_xxx without d_lock */ | |
191 | dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n", | |
192 | bindex, dentry, dentry, | |
193 | dentry->d_sb ? au_sbtype(dentry->d_sb) : "??", | |
194 | au_dcount(dentry), dentry->d_flags, | |
195 | d_unhashed(dentry) ? "un" : ""); | |
196 | hn = -1; | |
197 | inode = NULL; | |
198 | if (d_is_positive(dentry)) | |
199 | inode = d_inode(dentry); | |
200 | if (inode | |
201 | && au_test_aufs(dentry->d_sb) | |
202 | && bindex >= 0 | |
203 | && !au_is_bad_inode(inode)) { | |
204 | iinfo = au_ii(inode); | |
205 | hi = au_hinode(iinfo, bindex); | |
206 | hn = !!au_hn(hi); | |
207 | wh = hi->hi_whdentry; | |
208 | } | |
209 | do_pri_inode(bindex, inode, hn, wh); | |
210 | return 0; | |
211 | } | |
212 | ||
213 | void au_dpri_dentry(struct dentry *dentry) | |
214 | { | |
215 | struct au_dinfo *dinfo; | |
216 | aufs_bindex_t bindex; | |
217 | int err; | |
218 | ||
219 | err = do_pri_dentry(-1, dentry); | |
220 | if (err || !au_test_aufs(dentry->d_sb)) | |
221 | return; | |
222 | ||
223 | dinfo = au_di(dentry); | |
224 | if (!dinfo) | |
225 | return; | |
226 | dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n", | |
227 | dinfo->di_btop, dinfo->di_bbot, | |
228 | dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry), | |
229 | dinfo->di_tmpfile); | |
230 | if (dinfo->di_btop < 0) | |
231 | return; | |
232 | for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++) | |
233 | do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry); | |
234 | } | |
235 | ||
236 | static int do_pri_file(aufs_bindex_t bindex, struct file *file) | |
237 | { | |
238 | char a[32]; | |
239 | ||
240 | if (!file || IS_ERR(file)) { | |
241 | dpri("f%d: err %ld\n", bindex, PTR_ERR(file)); | |
242 | return -1; | |
243 | } | |
244 | a[0] = 0; | |
245 | if (bindex < 0 | |
246 | && !IS_ERR_OR_NULL(file->f_path.dentry) | |
247 | && au_test_aufs(file->f_path.dentry->d_sb) | |
248 | && au_fi(file)) | |
249 | snprintf(a, sizeof(a), ", gen %d, mmapped %d", | |
250 | au_figen(file), atomic_read(&au_fi(file)->fi_mmapped)); | |
251 | dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n", | |
252 | bindex, file->f_mode, file->f_flags, (long)file_count(file), | |
253 | file->f_version, file->f_pos, a); | |
254 | if (!IS_ERR_OR_NULL(file->f_path.dentry)) | |
255 | do_pri_dentry(bindex, file->f_path.dentry); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | void au_dpri_file(struct file *file) | |
260 | { | |
261 | struct au_finfo *finfo; | |
262 | struct au_fidir *fidir; | |
263 | struct au_hfile *hfile; | |
264 | aufs_bindex_t bindex; | |
265 | int err; | |
266 | ||
267 | err = do_pri_file(-1, file); | |
268 | if (err | |
269 | || IS_ERR_OR_NULL(file->f_path.dentry) | |
270 | || !au_test_aufs(file->f_path.dentry->d_sb)) | |
271 | return; | |
272 | ||
273 | finfo = au_fi(file); | |
274 | if (!finfo) | |
275 | return; | |
276 | if (finfo->fi_btop < 0) | |
277 | return; | |
278 | fidir = finfo->fi_hdir; | |
279 | if (!fidir) | |
280 | do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file); | |
281 | else | |
282 | for (bindex = finfo->fi_btop; | |
283 | bindex >= 0 && bindex <= fidir->fd_bbot; | |
284 | bindex++) { | |
285 | hfile = fidir->fd_hfile + bindex; | |
286 | do_pri_file(bindex, hfile ? hfile->hf_file : NULL); | |
287 | } | |
288 | } | |
289 | ||
290 | static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br) | |
291 | { | |
292 | struct vfsmount *mnt; | |
293 | struct super_block *sb; | |
294 | ||
295 | if (!br || IS_ERR(br)) | |
296 | goto out; | |
297 | mnt = au_br_mnt(br); | |
298 | if (!mnt || IS_ERR(mnt)) | |
299 | goto out; | |
300 | sb = mnt->mnt_sb; | |
301 | if (!sb || IS_ERR(sb)) | |
302 | goto out; | |
303 | ||
304 | dpri("s%d: {perm 0x%x, id %d, wbr %p}, " | |
305 | "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, " | |
306 | "xino %d\n", | |
307 | bindex, br->br_perm, br->br_id, br->br_wbr, | |
308 | au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev), | |
309 | sb->s_flags, sb->s_count, | |
310 | atomic_read(&sb->s_active), | |
311 | !!au_xino_file(br->br_xino, /*idx*/-1)); | |
312 | return 0; | |
313 | ||
314 | out: | |
315 | dpri("s%d: err %ld\n", bindex, PTR_ERR(br)); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | void au_dpri_sb(struct super_block *sb) | |
320 | { | |
321 | struct au_sbinfo *sbinfo; | |
322 | aufs_bindex_t bindex; | |
323 | int err; | |
324 | /* to reduce stack size */ | |
325 | struct { | |
326 | struct vfsmount mnt; | |
327 | struct au_branch fake; | |
328 | } *a; | |
329 | ||
330 | /* this function can be called from magic sysrq */ | |
331 | a = kzalloc(sizeof(*a), GFP_ATOMIC); | |
332 | if (unlikely(!a)) { | |
333 | dpri("no memory\n"); | |
334 | return; | |
335 | } | |
336 | ||
337 | a->mnt.mnt_sb = sb; | |
338 | a->fake.br_path.mnt = &a->mnt; | |
339 | err = do_pri_br(-1, &a->fake); | |
340 | au_kfree_rcu(a); | |
341 | dpri("dev 0x%x\n", sb->s_dev); | |
342 | if (err || !au_test_aufs(sb)) | |
343 | return; | |
344 | ||
345 | sbinfo = au_sbi(sb); | |
346 | if (!sbinfo) | |
347 | return; | |
348 | dpri("nw %d, gen %u, kobj %d\n", | |
349 | atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation, | |
350 | kref_read(&sbinfo->si_kobj.kref)); | |
351 | for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++) | |
352 | do_pri_br(bindex, sbinfo->si_branch[0 + bindex]); | |
353 | } | |
354 | ||
355 | /* ---------------------------------------------------------------------- */ | |
356 | ||
357 | void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line) | |
358 | { | |
359 | struct inode *h_inode, *inode = d_inode(dentry); | |
360 | struct dentry *h_dentry; | |
361 | aufs_bindex_t bindex, bbot, bi; | |
362 | ||
363 | if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */) | |
364 | return; | |
365 | ||
366 | bbot = au_dbbot(dentry); | |
367 | bi = au_ibbot(inode); | |
368 | if (bi < bbot) | |
369 | bbot = bi; | |
370 | bindex = au_dbtop(dentry); | |
371 | bi = au_ibtop(inode); | |
372 | if (bi > bindex) | |
373 | bindex = bi; | |
374 | ||
375 | for (; bindex <= bbot; bindex++) { | |
376 | h_dentry = au_h_dptr(dentry, bindex); | |
377 | if (!h_dentry) | |
378 | continue; | |
379 | h_inode = au_h_iptr(inode, bindex); | |
380 | if (unlikely(h_inode != d_inode(h_dentry))) { | |
381 | au_debug_on(); | |
382 | AuDbg("b%d, %s:%d\n", bindex, func, line); | |
383 | AuDbgDentry(dentry); | |
384 | AuDbgInode(inode); | |
385 | au_debug_off(); | |
386 | BUG(); | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen) | |
392 | { | |
393 | int err, i, j; | |
394 | struct au_dcsub_pages dpages; | |
395 | struct au_dpage *dpage; | |
396 | struct dentry **dentries; | |
397 | ||
398 | err = au_dpages_init(&dpages, GFP_NOFS); | |
399 | AuDebugOn(err); | |
400 | err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1); | |
401 | AuDebugOn(err); | |
402 | for (i = dpages.ndpage - 1; !err && i >= 0; i--) { | |
403 | dpage = dpages.dpages + i; | |
404 | dentries = dpage->dentries; | |
405 | for (j = dpage->ndentry - 1; !err && j >= 0; j--) | |
406 | AuDebugOn(au_digen_test(dentries[j], sigen)); | |
407 | } | |
408 | au_dpages_free(&dpages); | |
409 | } | |
410 | ||
411 | void au_dbg_verify_kthread(void) | |
412 | { | |
413 | if (au_wkq_test()) { | |
414 | au_dbg_blocked(); | |
415 | /* | |
416 | * It may be recursive, but udba=notify between two aufs mounts, | |
417 | * where a single ro branch is shared, is not a problem. | |
418 | */ | |
419 | /* WARN_ON(1); */ | |
420 | } | |
421 | } | |
422 | ||
423 | /* ---------------------------------------------------------------------- */ | |
424 | ||
425 | int __init au_debug_init(void) | |
426 | { | |
427 | aufs_bindex_t bindex; | |
428 | struct au_vdir_destr destr; | |
429 | ||
430 | bindex = -1; | |
431 | AuDebugOn(bindex >= 0); | |
432 | ||
433 | destr.len = -1; | |
434 | AuDebugOn(destr.len < NAME_MAX); | |
435 | ||
436 | #ifdef CONFIG_4KSTACKS | |
437 | pr_warn("CONFIG_4KSTACKS is defined.\n"); | |
438 | #endif | |
439 | ||
440 | return 0; | |
441 | } |