]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_fslog.c
2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <sys/errno.h>
31 #include <sys/types.h>
32 #include <sys/malloc.h>
35 #include <sys/kauth.h>
36 #include <sys/mount.h>
37 #include <sys/vnode.h>
38 #include <sys/syslog.h> /* for vaddlog() */
39 #include <sys/vnode_internal.h>
40 #include <dev/random/randomdev.h>
42 #include <sys/fslog.h>
43 #include <sys/mount_internal.h>
45 /* String to append as format modifier for each key-value pair */
46 #define FSLOG_KEYVAL_FMT "[%s %s] "
47 #define FSLOG_KEYVAL_FMT_LEN (sizeof(FSLOG_KEYVAL_FMT) - 1)
49 #define FSLOG_NEWLINE_CHAR "\n"
50 #define FSLOG_NEWLINE_CHAR_LEN (sizeof(FSLOG_NEWLINE_CHAR) - 1)
52 /* Length of entire ASL message in 10 characters. Kernel defaults to zero */
53 #define FSLOG_ASL_MSG_LEN " 0"
55 /* Length of default format string to be used by printf */
56 #define MAX_FMT_LEN 256
58 /* Internal function to print input values as key-value pairs in format
59 * identifiable by Apple system log (ASL) facility. All key-value pairs
60 * are assumed to be pointer to strings and are provided using two ways -
61 * (a) va_list argument which is a list of varying number of arguments
62 * created by the caller of this function.
63 * (b) variable number of arguments passed to this function.
66 * level - Priority level for this ASL message
67 * facility - Facility for this ASL message.
68 * num_pairs - Number of key-value pairs provided by vargs argument.
69 * vargs - List of key-value pairs.
70 * ... - Additional key-value pairs (apart from vargs) as variable
71 * argument list. A NULL value indicates the end of the
72 * variable argument list.
75 * zero - On success, when it prints all key-values pairs provided.
76 * E2BIG - When it cannot print all key-value pairs provided and had
77 * to truncate the output.
79 static int fslog_asl_msg(int level
, const char *facility
, int num_pairs
, va_list vargs
, ...)
82 char fmt
[MAX_FMT_LEN
]; /* Format string to use with vaddlog */
89 /* Mask extra bits, if any, from priority level */
90 level
= LOG_PRI(level
);
92 /* Create the first part of format string consisting of ASL
93 * message length, level, and facility.
96 snprintf(fmt
, MAX_FMT_LEN
, "%s [%s %d] [%s %d] [%s %s] ",
98 FSLOG_KEY_LEVEL
, level
,
99 FSLOG_KEY_READ_UID
, FSLOG_VAL_READ_UID
,
100 FSLOG_KEY_FACILITY
, facility
);
102 snprintf(fmt
, MAX_FMT_LEN
, "%s [%s %d] [%s %d] ",
104 FSLOG_KEY_LEVEL
, level
,
105 FSLOG_KEY_READ_UID
, FSLOG_VAL_READ_UID
);
108 /* Determine the number of key-value format string [%s %s] that
109 * should be added in format string for every key-value pair provided
110 * in va_list. Calculate maximum number of format string that can be
111 * accommodated in the remaining format buffer (after saving space
112 * for newline character). If the caller provided pairs in va_list
113 * is more than calculated pairs, truncate extra pairs.
115 len
= MAX_FMT_LEN
- strlen(fmt
) - FSLOG_NEWLINE_CHAR_LEN
- 1;
116 calc_pairs
= len
/ FSLOG_KEYVAL_FMT_LEN
;
117 if (num_pairs
<= calc_pairs
) {
118 calc_pairs
= num_pairs
;
123 /* Append format strings [%s %s] for the key-value pairs in vargs */
124 len
= MAX_FMT_LEN
- FSLOG_NEWLINE_CHAR_LEN
;
125 for (i
= 0; i
< calc_pairs
; i
++) {
126 (void) strlcat(fmt
, FSLOG_KEYVAL_FMT
, len
);
129 /* Count number of variable arguments provided to this function
130 * and determine total number of key-value pairs.
134 ptr
= va_arg(ap
, char *);
137 ptr
= va_arg(ap
, char *);
142 /* If user provided variable number of arguments, append them as
143 * as real key-value "[k v]" into the format string. If the format
144 * string is too small, ignore the key-value pair completely.
151 /* Calculate bytes available for key-value pairs after reserving
152 * bytes for newline character and NULL terminator
154 len
= MAX_FMT_LEN
- strlen(fmt
) - FSLOG_NEWLINE_CHAR_LEN
- 1;
155 offset
= strlen(fmt
);
158 for (i
= 0; i
< calc_pairs
; i
++) {
159 key
= va_arg(ap
, char *);
160 val
= va_arg(ap
, char *);
162 /* Calculate bytes required to store next key-value pair as
163 * "[key val] " including space for '[', ']', and two spaces.
165 pairlen
= strlen(key
) + strlen(val
) + 4;
171 /* len + 1 because one byte has been set aside for NULL
172 * terminator in calculation of 'len' above
174 snprintf((fmt
+ offset
), len
+ 1, FSLOG_KEYVAL_FMT
, key
, val
);
182 (void) strlcat(fmt
, FSLOG_NEWLINE_CHAR
, MAX_FMT_LEN
);
184 /* Print the key-value pairs in ASL format */
190 /* Log file system related error in key-value format identified by Apple
191 * system log (ASL) facility. The key-value pairs are string pointers
192 * (char *) and are provided as variable arguments list. A NULL value
193 * indicates end of the list.
195 * Keys can not contain '[', ']', space, and newline. Values can not
196 * contain '[', ']', and newline. If any key-value contains any of the
197 * reserved characters, the behavior is undefined. The caller of the
198 * function should escape any occurrences of '[' and ']' by prefixing
201 * The function takes a message ID which can be used to logically group
202 * different ASL messages. Messages in same logical group have same message
203 * ID and have information to describe order of the message --- first,
206 * The following message IDs have special meaning -
207 * FSLOG_MSG_FIRST - This message is the first message in its logical
208 * group. This generates a unique message ID, creates two key-value
209 * pairs with message ID and order of the message as "First".
210 * FSLOG_MSG_LAST - This is really a MASK which should be logically OR'ed
211 * with message ID to indicate the last message for a logical group.
212 * This also creates two key-value pairs with message ID and order of
214 * FSLOG_MSG_SINGLE - This signifies that the message is the only message
215 * in its logical group. Therefore no extra key-values are generated
217 * For all other values of message IDs, it regards them as intermediate
218 * message and generates two key-value pairs with message ID and order of
219 * message as "Middle".
222 * Message ID of the ASL message printed. The caller should use
223 * this value to print intermediate messages or end the logical message
225 * For FSLOG_MSG_SINGLE option, it returns FSLOG_MSG_SINGLE.
227 unsigned long fslog_err(unsigned long msg_id
, ... )
231 char msg_id_str
[21]; /* To convert 64-bit number to string with NULL char */
233 const char *msg_order_ptr
;
235 /* Count number of arguments and key-value pairs provided by user */
237 va_start(ap
, msg_id
);
238 arg
= va_arg(ap
, char *);
241 arg
= va_arg(ap
, char *);
246 va_start(ap
, msg_id
);
247 if (msg_id
== FSLOG_MSG_SINGLE
) {
248 /* Single message, do not print message ID and message order */
249 (void) fslog_asl_msg(FSLOG_VAL_LEVEL
, FSLOG_VAL_FACILITY
,
250 num_pairs
, ap
, NULL
);
252 if (msg_id
== FSLOG_MSG_FIRST
) {
253 /* First message, generate random message ID */
254 while ((msg_id
== FSLOG_MSG_FIRST
) ||
255 (msg_id
== FSLOG_MSG_LAST
) ||
256 (msg_id
== FSLOG_MSG_SINGLE
)) {
257 msg_id
= RandomULong();
258 /* MSB is reserved for indicating last message
259 * in sequence. Clear the MSB while generating
262 msg_id
= msg_id
>> 1;
264 msg_order_ptr
= FSLOG_VAL_ORDER_FIRST
;
265 } else if (msg_id
& FSLOG_MSG_LAST
) {
266 /* MSB set to indicate last message for this ID */
267 msg_order_ptr
= FSLOG_VAL_ORDER_LAST
;
268 /* MSB of message ID is set to indicate last message
269 * in sequence. Clear the bit to get real message ID.
271 msg_id
= msg_id
& ~FSLOG_MSG_LAST
;
273 /* Intermediate message for this ID */
274 msg_order_ptr
= FSLOG_VAL_ORDER_MIDDLE
;
277 snprintf(msg_id_str
, sizeof(msg_id_str
), "%lu", msg_id
);
278 (void) fslog_asl_msg(FSLOG_VAL_LEVEL
, FSLOG_VAL_FACILITY
,
280 FSLOG_KEY_MSG_ID
, msg_id_str
,
281 FSLOG_KEY_MSG_ORDER
, msg_order_ptr
, NULL
);
287 /* Search if given string contains '[' and ']'. If any, escape it by
288 * prefixing with a '\'. If the length of the string is not big enough,
289 * no changes are done and error is returned.
292 * str - string that can contain '[' or ']', should be NULL terminated
293 * len - length, in bytes, of valid data, including NULL character.
294 * buflen - size of buffer that contains the string
296 static int escape_str(char *str
, int len
, int buflen
)
301 /* Count number of characters to escape */
305 if ((*src
== '[') || (*src
== ']')) {
311 /* Check if the buffer has enough space to escape all characters */
312 if ((buflen
- len
) < count
) {
320 if ((*src
== '[') || (*src
== ']')) {
321 /* Last char copied needs to be escaped */
332 /* Log information about runtime file system corruption detected by
333 * the file system. It takes the VFS mount structure as
334 * parameter which is used to access the mount point of the
335 * corrupt volume. If no mount structure or mount point string
336 * string exists, nothing is logged to ASL database.
338 * Currently prints following information -
341 void fslog_fs_corrupt(struct mount
*mnt
)
344 if (mnt
->mnt_vfsstat
.f_mntonname
!= NULL
) {
345 fslog_err(FSLOG_MSG_SINGLE
,
346 FSLOG_KEY_ERR_TYPE
, FSLOG_VAL_ERR_TYPE_FS
,
347 FSLOG_KEY_MNTPT
, mnt
->mnt_vfsstat
.f_mntonname
,
355 /* Log information about IO error detected in buf_biodone()
356 * Currently prints following information -
357 * 1. Physical block number
358 * 2. Logical block number
361 * 5. Path for file, if any
363 * 7. Type of IO (read/write)
365 void fslog_io_error(const buf_t bp
)
368 unsigned long msg_id
;
370 char lblknum_str
[21];
373 unsigned char print_last
= 0;
376 if (buf_error(bp
) == 0) {
380 /* Convert error number to string */
381 snprintf (errno_str
, sizeof(errno_str
), "%d", buf_error(bp
));
383 /* Determine type of IO operation */
384 if (buf_flags(bp
) & B_READ
) {
385 iotype
= FSLOG_VAL_IOTYPE_READ
;
387 iotype
= FSLOG_VAL_IOTYPE_WRITE
;
390 /* Convert physical block number to string */
391 snprintf (blknum_str
, sizeof(blknum_str
), "%lld", buf_blkno(bp
));
393 /* Convert logical block number to string */
394 snprintf (lblknum_str
, sizeof(lblknum_str
), "%lld", buf_lblkno(bp
));
396 msg_id
= fslog_err(FSLOG_MSG_FIRST
,
397 FSLOG_KEY_ERR_TYPE
, FSLOG_VAL_ERR_TYPE_IO
,
398 FSLOG_KEY_ERRNO
, errno_str
,
399 FSLOG_KEY_IOTYPE
, iotype
,
400 FSLOG_KEY_PHYS_BLKNUM
, blknum_str
,
401 FSLOG_KEY_LOG_BLKNUM
, lblknum_str
,
404 /* Access the vnode for this buffer */
407 struct vfsstatfs
*sp
;
411 struct vfs_context context
;
413 mp
= vnode_mount(vp
);
414 /* mp should be NULL only for bdevvp during boot */
420 /* Access the file path */
421 MALLOC(path
, char *, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
424 context
.vc_thread
= current_thread();
425 context
.vc_ucred
= kauth_cred_get();
426 /* Find path without entering file system */
427 err
= build_path(vp
, path
, len
, &len
, BUILDPATH_NO_FS_ENTER
,
430 err
= escape_str(path
, len
, MAXPATHLEN
);
432 /* Print device node, mount point, path */
433 msg_id
= fslog_err(msg_id
| FSLOG_MSG_LAST
,
434 FSLOG_KEY_DEVNODE
, sp
->f_mntfromname
,
435 FSLOG_KEY_MNTPT
, sp
->f_mntonname
,
436 FSLOG_KEY_PATH
, path
,
444 if (print_last
== 0) {
445 /* Print device node and mount point */
446 msg_id
= fslog_err(msg_id
| FSLOG_MSG_LAST
,
447 FSLOG_KEY_DEVNODE
, sp
->f_mntfromname
,
448 FSLOG_KEY_MNTPT
, sp
->f_mntonname
,
455 if (print_last
== 0) {
456 msg_id
= fslog_err(msg_id
| FSLOG_MSG_LAST
, NULL
);