1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * debug.c - NTFS kernel debug support. Part of the Linux-NTFS project.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2001-2004 Anton Altaparmakov
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8*4882a593Smuzhiyun #include "debug.h"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /**
11*4882a593Smuzhiyun * __ntfs_warning - output a warning to the syslog
12*4882a593Smuzhiyun * @function: name of function outputting the warning
13*4882a593Smuzhiyun * @sb: super block of mounted ntfs filesystem
14*4882a593Smuzhiyun * @fmt: warning string containing format specifications
15*4882a593Smuzhiyun * @...: a variable number of arguments specified in @fmt
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Outputs a warning to the syslog for the mounted ntfs filesystem described
18*4882a593Smuzhiyun * by @sb.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * @fmt and the corresponding @... is printf style format string containing
21*4882a593Smuzhiyun * the warning string and the corresponding format arguments, respectively.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * @function is the name of the function from which __ntfs_warning is being
24*4882a593Smuzhiyun * called.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Note, you should be using debug.h::ntfs_warning(@sb, @fmt, @...) instead
27*4882a593Smuzhiyun * as this provides the @function parameter automatically.
28*4882a593Smuzhiyun */
__ntfs_warning(const char * function,const struct super_block * sb,const char * fmt,...)29*4882a593Smuzhiyun void __ntfs_warning(const char *function, const struct super_block *sb,
30*4882a593Smuzhiyun const char *fmt, ...)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun struct va_format vaf;
33*4882a593Smuzhiyun va_list args;
34*4882a593Smuzhiyun int flen = 0;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #ifndef DEBUG
37*4882a593Smuzhiyun if (!printk_ratelimit())
38*4882a593Smuzhiyun return;
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun if (function)
41*4882a593Smuzhiyun flen = strlen(function);
42*4882a593Smuzhiyun va_start(args, fmt);
43*4882a593Smuzhiyun vaf.fmt = fmt;
44*4882a593Smuzhiyun vaf.va = &args;
45*4882a593Smuzhiyun if (sb)
46*4882a593Smuzhiyun pr_warn("(device %s): %s(): %pV\n",
47*4882a593Smuzhiyun sb->s_id, flen ? function : "", &vaf);
48*4882a593Smuzhiyun else
49*4882a593Smuzhiyun pr_warn("%s(): %pV\n", flen ? function : "", &vaf);
50*4882a593Smuzhiyun va_end(args);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * __ntfs_error - output an error to the syslog
55*4882a593Smuzhiyun * @function: name of function outputting the error
56*4882a593Smuzhiyun * @sb: super block of mounted ntfs filesystem
57*4882a593Smuzhiyun * @fmt: error string containing format specifications
58*4882a593Smuzhiyun * @...: a variable number of arguments specified in @fmt
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Outputs an error to the syslog for the mounted ntfs filesystem described
61*4882a593Smuzhiyun * by @sb.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * @fmt and the corresponding @... is printf style format string containing
64*4882a593Smuzhiyun * the error string and the corresponding format arguments, respectively.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * @function is the name of the function from which __ntfs_error is being
67*4882a593Smuzhiyun * called.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * Note, you should be using debug.h::ntfs_error(@sb, @fmt, @...) instead
70*4882a593Smuzhiyun * as this provides the @function parameter automatically.
71*4882a593Smuzhiyun */
__ntfs_error(const char * function,const struct super_block * sb,const char * fmt,...)72*4882a593Smuzhiyun void __ntfs_error(const char *function, const struct super_block *sb,
73*4882a593Smuzhiyun const char *fmt, ...)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun struct va_format vaf;
76*4882a593Smuzhiyun va_list args;
77*4882a593Smuzhiyun int flen = 0;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun #ifndef DEBUG
80*4882a593Smuzhiyun if (!printk_ratelimit())
81*4882a593Smuzhiyun return;
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun if (function)
84*4882a593Smuzhiyun flen = strlen(function);
85*4882a593Smuzhiyun va_start(args, fmt);
86*4882a593Smuzhiyun vaf.fmt = fmt;
87*4882a593Smuzhiyun vaf.va = &args;
88*4882a593Smuzhiyun if (sb)
89*4882a593Smuzhiyun pr_err("(device %s): %s(): %pV\n",
90*4882a593Smuzhiyun sb->s_id, flen ? function : "", &vaf);
91*4882a593Smuzhiyun else
92*4882a593Smuzhiyun pr_err("%s(): %pV\n", flen ? function : "", &vaf);
93*4882a593Smuzhiyun va_end(args);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun #ifdef DEBUG
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* If 1, output debug messages, and if 0, don't. */
99*4882a593Smuzhiyun int debug_msgs = 0;
100*4882a593Smuzhiyun
__ntfs_debug(const char * file,int line,const char * function,const char * fmt,...)101*4882a593Smuzhiyun void __ntfs_debug(const char *file, int line, const char *function,
102*4882a593Smuzhiyun const char *fmt, ...)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct va_format vaf;
105*4882a593Smuzhiyun va_list args;
106*4882a593Smuzhiyun int flen = 0;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun if (!debug_msgs)
109*4882a593Smuzhiyun return;
110*4882a593Smuzhiyun if (function)
111*4882a593Smuzhiyun flen = strlen(function);
112*4882a593Smuzhiyun va_start(args, fmt);
113*4882a593Smuzhiyun vaf.fmt = fmt;
114*4882a593Smuzhiyun vaf.va = &args;
115*4882a593Smuzhiyun pr_debug("(%s, %d): %s(): %pV", file, line, flen ? function : "", &vaf);
116*4882a593Smuzhiyun va_end(args);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* Dump a runlist. Caller has to provide synchronisation for @rl. */
ntfs_debug_dump_runlist(const runlist_element * rl)120*4882a593Smuzhiyun void ntfs_debug_dump_runlist(const runlist_element *rl)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun int i;
123*4882a593Smuzhiyun const char *lcn_str[5] = { "LCN_HOLE ", "LCN_RL_NOT_MAPPED",
124*4882a593Smuzhiyun "LCN_ENOENT ", "LCN_unknown " };
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if (!debug_msgs)
127*4882a593Smuzhiyun return;
128*4882a593Smuzhiyun pr_debug("Dumping runlist (values in hex):\n");
129*4882a593Smuzhiyun if (!rl) {
130*4882a593Smuzhiyun pr_debug("Run list not present.\n");
131*4882a593Smuzhiyun return;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun pr_debug("VCN LCN Run length\n");
134*4882a593Smuzhiyun for (i = 0; ; i++) {
135*4882a593Smuzhiyun LCN lcn = (rl + i)->lcn;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun if (lcn < (LCN)0) {
138*4882a593Smuzhiyun int index = -lcn - 1;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (index > -LCN_ENOENT - 1)
141*4882a593Smuzhiyun index = 3;
142*4882a593Smuzhiyun pr_debug("%-16Lx %s %-16Lx%s\n",
143*4882a593Smuzhiyun (long long)(rl + i)->vcn, lcn_str[index],
144*4882a593Smuzhiyun (long long)(rl + i)->length,
145*4882a593Smuzhiyun (rl + i)->length ? "" :
146*4882a593Smuzhiyun " (runlist end)");
147*4882a593Smuzhiyun } else
148*4882a593Smuzhiyun pr_debug("%-16Lx %-16Lx %-16Lx%s\n",
149*4882a593Smuzhiyun (long long)(rl + i)->vcn,
150*4882a593Smuzhiyun (long long)(rl + i)->lcn,
151*4882a593Smuzhiyun (long long)(rl + i)->length,
152*4882a593Smuzhiyun (rl + i)->length ? "" :
153*4882a593Smuzhiyun " (runlist end)");
154*4882a593Smuzhiyun if (!(rl + i)->length)
155*4882a593Smuzhiyun break;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun #endif
160