1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011 Red Hat, Inc.  All Rights Reserved.
4  */
5 
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_error.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 
14 /*
15  * XFS logging functions
16  */
17 static void
__xfs_printk(const char * level,const struct xfs_mount * mp,struct va_format * vaf)18 __xfs_printk(
19 	const char		*level,
20 	const struct xfs_mount	*mp,
21 	struct va_format	*vaf)
22 {
23 	if (mp && mp->m_super) {
24 		printk("%sXFS (%s): %pV\n", level, mp->m_super->s_id, vaf);
25 		return;
26 	}
27 	printk("%sXFS: %pV\n", level, vaf);
28 }
29 
30 #define define_xfs_printk_level(func, kern_level)		\
31 void func(const struct xfs_mount *mp, const char *fmt, ...)	\
32 {								\
33 	struct va_format	vaf;				\
34 	va_list			args;				\
35 	int			level;				\
36 								\
37 	va_start(args, fmt);					\
38 								\
39 	vaf.fmt = fmt;						\
40 	vaf.va = &args;						\
41 								\
42 	__xfs_printk(kern_level, mp, &vaf);			\
43 	va_end(args);						\
44 								\
45 	if (!kstrtoint(kern_level, 0, &level) &&		\
46 	    level <= LOGLEVEL_ERR &&				\
47 	    xfs_error_level >= XFS_ERRLEVEL_HIGH)		\
48 		xfs_stack_trace();				\
49 }								\
50 
51 define_xfs_printk_level(xfs_emerg, KERN_EMERG);
52 define_xfs_printk_level(xfs_alert, KERN_ALERT);
53 define_xfs_printk_level(xfs_crit, KERN_CRIT);
54 define_xfs_printk_level(xfs_err, KERN_ERR);
55 define_xfs_printk_level(xfs_warn, KERN_WARNING);
56 define_xfs_printk_level(xfs_notice, KERN_NOTICE);
57 define_xfs_printk_level(xfs_info, KERN_INFO);
58 #ifdef DEBUG
59 define_xfs_printk_level(xfs_debug, KERN_DEBUG);
60 #endif
61 
62 void
xfs_alert_tag(const struct xfs_mount * mp,int panic_tag,const char * fmt,...)63 xfs_alert_tag(
64 	const struct xfs_mount	*mp,
65 	int			panic_tag,
66 	const char		*fmt, ...)
67 {
68 	struct va_format	vaf;
69 	va_list			args;
70 	int			do_panic = 0;
71 
72 	if (xfs_panic_mask && (xfs_panic_mask & panic_tag)) {
73 		xfs_alert(mp, "Transforming an alert into a BUG.");
74 		do_panic = 1;
75 	}
76 
77 	va_start(args, fmt);
78 
79 	vaf.fmt = fmt;
80 	vaf.va = &args;
81 
82 	__xfs_printk(KERN_ALERT, mp, &vaf);
83 	va_end(args);
84 
85 	BUG_ON(do_panic);
86 }
87 
88 void
asswarn(struct xfs_mount * mp,char * expr,char * file,int line)89 asswarn(
90 	struct xfs_mount	*mp,
91 	char			*expr,
92 	char			*file,
93 	int			line)
94 {
95 	xfs_warn(mp, "Assertion failed: %s, file: %s, line: %d",
96 		expr, file, line);
97 	WARN_ON(1);
98 }
99 
100 void
assfail(struct xfs_mount * mp,char * expr,char * file,int line)101 assfail(
102 	struct xfs_mount	*mp,
103 	char			*expr,
104 	char			*file,
105 	int			line)
106 {
107 	xfs_emerg(mp, "Assertion failed: %s, file: %s, line: %d",
108 		expr, file, line);
109 	if (xfs_globals.bug_on_assert)
110 		BUG();
111 	else
112 		WARN_ON(1);
113 }
114 
115 void
xfs_hex_dump(const void * p,int length)116 xfs_hex_dump(const void *p, int length)
117 {
118 	print_hex_dump(KERN_ALERT, "", DUMP_PREFIX_OFFSET, 16, 1, p, length, 1);
119 }
120 
121 void
xfs_buf_alert_ratelimited(struct xfs_buf * bp,const char * rlmsg,const char * fmt,...)122 xfs_buf_alert_ratelimited(
123 	struct xfs_buf		*bp,
124 	const char		*rlmsg,
125 	const char		*fmt,
126 	...)
127 {
128 	struct xfs_mount	*mp = bp->b_mount;
129 	struct va_format	vaf;
130 	va_list			args;
131 
132 	/* use the more aggressive per-target rate limit for buffers */
133 	if (!___ratelimit(&bp->b_target->bt_ioerror_rl, rlmsg))
134 		return;
135 
136 	va_start(args, fmt);
137 	vaf.fmt = fmt;
138 	vaf.va = &args;
139 	__xfs_printk(KERN_ALERT, mp, &vaf);
140 	va_end(args);
141 }
142