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