]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * Copyright (c) 2006 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | #include <stdarg.h> | |
30 | #include <sys/errno.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/malloc.h> | |
33 | #include <sys/buf.h> | |
34 | #include <sys/time.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> | |
41 | ||
42 | #include <sys/fslog.h> | |
43 | #include <sys/mount_internal.h> | |
44 | ||
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) | |
48 | ||
49 | #define FSLOG_NEWLINE_CHAR "\n" | |
50 | #define FSLOG_NEWLINE_CHAR_LEN (sizeof(FSLOG_NEWLINE_CHAR) - 1) | |
51 | ||
52 | /* Length of entire ASL message in 10 characters. Kernel defaults to zero */ | |
53 | #define FSLOG_ASL_MSG_LEN " 0" | |
54 | ||
55 | /* Length of default format string to be used by printf */ | |
56 | #define MAX_FMT_LEN 256 | |
57 | ||
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. | |
64 | * | |
65 | * Parameters - | |
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. | |
73 | * | |
74 | * Returns - | |
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. | |
78 | */ | |
79 | static int fslog_asl_msg(int level, const char *facility, int num_pairs, va_list vargs, ...) | |
80 | { | |
81 | int err = 0; | |
82 | char fmt[MAX_FMT_LEN]; /* Format string to use with vaddlog */ | |
83 | int calc_pairs = 0; | |
84 | size_t len; | |
85 | int i; | |
86 | va_list ap; | |
87 | char *ptr; | |
88 | ||
89 | /* Mask extra bits, if any, from priority level */ | |
90 | level = LOG_PRI(level); | |
91 | ||
92 | /* Create the first part of format string consisting of ASL | |
93 | * message length, level, and facility. | |
94 | */ | |
95 | if (facility) { | |
96 | snprintf(fmt, MAX_FMT_LEN, "%s [%s %d] [%s %d] [%s %s] ", | |
97 | FSLOG_ASL_MSG_LEN, | |
98 | FSLOG_KEY_LEVEL, level, | |
99 | FSLOG_KEY_READ_UID, FSLOG_VAL_READ_UID, | |
100 | FSLOG_KEY_FACILITY, facility); | |
101 | } else { | |
102 | snprintf(fmt, MAX_FMT_LEN, "%s [%s %d] [%s %d] ", | |
103 | FSLOG_ASL_MSG_LEN, | |
104 | FSLOG_KEY_LEVEL, level, | |
105 | FSLOG_KEY_READ_UID, FSLOG_VAL_READ_UID); | |
106 | } | |
107 | ||
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. | |
114 | */ | |
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; | |
119 | } else { | |
120 | err = E2BIG; | |
121 | } | |
122 | ||
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); | |
127 | } | |
128 | ||
129 | /* Count number of variable arguments provided to this function | |
130 | * and determine total number of key-value pairs. | |
131 | */ | |
132 | calc_pairs = 0; | |
133 | va_start(ap, vargs); | |
134 | ptr = va_arg(ap, char *); | |
135 | while (ptr) { | |
136 | calc_pairs++; | |
137 | ptr = va_arg(ap, char *); | |
138 | } | |
139 | calc_pairs /= 2; | |
140 | va_end(ap); | |
141 | ||
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. | |
145 | */ | |
146 | if (calc_pairs) { | |
147 | char *key, *val; | |
148 | size_t pairlen; | |
149 | int offset; | |
150 | ||
151 | /* Calculate bytes available for key-value pairs after reserving | |
152 | * bytes for newline character and NULL terminator | |
153 | */ | |
154 | len = MAX_FMT_LEN - strlen(fmt) - FSLOG_NEWLINE_CHAR_LEN - 1; | |
155 | offset = strlen(fmt); | |
156 | ||
157 | va_start(ap, vargs); | |
158 | for (i = 0; i < calc_pairs; i++) { | |
159 | key = va_arg(ap, char *); | |
160 | val = va_arg(ap, char *); | |
161 | ||
162 | /* Calculate bytes required to store next key-value pair as | |
163 | * "[key val] " including space for '[', ']', and two spaces. | |
164 | */ | |
165 | pairlen = strlen(key) + strlen(val) + 4; | |
166 | if (pairlen > len) { | |
167 | err = E2BIG; | |
168 | break; | |
169 | } | |
170 | ||
171 | /* len + 1 because one byte has been set aside for NULL | |
172 | * terminator in calculation of 'len' above | |
173 | */ | |
174 | snprintf((fmt + offset), len + 1, FSLOG_KEYVAL_FMT, key, val); | |
175 | offset += pairlen; | |
176 | len -= pairlen; | |
177 | } | |
178 | va_end(ap); | |
179 | } | |
180 | ||
181 | /* Append newline */ | |
182 | (void) strlcat(fmt, FSLOG_NEWLINE_CHAR, MAX_FMT_LEN); | |
183 | ||
184 | /* Print the key-value pairs in ASL format */ | |
185 | vaddlog(fmt, vargs); | |
186 | ||
187 | return err; | |
188 | } | |
189 | ||
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. | |
194 | * | |
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 | |
199 | * it with '\'. | |
200 | * | |
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, | |
204 | * middle, or last. | |
205 | * | |
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 | |
213 | * message as "Last". | |
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 | |
216 | * for this option. | |
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". | |
220 | * | |
221 | * Returns - | |
222 | * Message ID of the ASL message printed. The caller should use | |
223 | * this value to print intermediate messages or end the logical message | |
224 | * group. | |
225 | * For FSLOG_MSG_SINGLE option, it returns FSLOG_MSG_SINGLE. | |
226 | */ | |
227 | unsigned long fslog_err(unsigned long msg_id, ... ) | |
228 | { | |
229 | va_list ap; | |
230 | int num_pairs; | |
231 | char msg_id_str[21]; /* To convert 64-bit number to string with NULL char */ | |
232 | char *arg; | |
233 | const char *msg_order_ptr; | |
234 | ||
235 | /* Count number of arguments and key-value pairs provided by user */ | |
236 | num_pairs = 0; | |
237 | va_start(ap, msg_id); | |
238 | arg = va_arg(ap, char *); | |
239 | while (arg) { | |
240 | num_pairs++; | |
241 | arg = va_arg(ap, char *); | |
242 | } | |
243 | num_pairs /= 2; | |
244 | va_end(ap); | |
245 | ||
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); | |
251 | } else { | |
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 | |
260 | * new message ID. | |
261 | */ | |
262 | msg_id = msg_id >> 1; | |
263 | } | |
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. | |
270 | */ | |
271 | msg_id = msg_id & ~FSLOG_MSG_LAST; | |
272 | } else { | |
273 | /* Intermediate message for this ID */ | |
274 | msg_order_ptr = FSLOG_VAL_ORDER_MIDDLE; | |
275 | } | |
276 | ||
277 | snprintf(msg_id_str, sizeof(msg_id_str), "%lu", msg_id); | |
278 | (void) fslog_asl_msg(FSLOG_VAL_LEVEL, FSLOG_VAL_FACILITY, | |
279 | num_pairs, ap, | |
280 | FSLOG_KEY_MSG_ID, msg_id_str, | |
281 | FSLOG_KEY_MSG_ORDER, msg_order_ptr, NULL); | |
282 | } | |
283 | va_end(ap); | |
284 | return msg_id; | |
285 | } | |
286 | ||
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. | |
290 | * | |
291 | * Parameters - | |
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 | |
295 | */ | |
296 | static int escape_str(char *str, int len, int buflen) | |
297 | { | |
298 | int count; | |
299 | char *src, *dst; | |
300 | ||
301 | /* Count number of characters to escape */ | |
302 | src = str; | |
303 | count = 0; | |
304 | do { | |
305 | if ((*src == '[') || (*src == ']')) { | |
306 | count++; | |
307 | } | |
308 | } while (*src++); | |
309 | ||
310 | if (count) { | |
311 | /* Check if the buffer has enough space to escape all characters */ | |
312 | if ((buflen - len) < count) { | |
313 | return ENOSPC; | |
314 | } | |
315 | ||
316 | src = str + len; | |
317 | dst = src + count; | |
318 | while (count) { | |
319 | *dst-- = *src; | |
320 | if ((*src == '[') || (*src == ']')) { | |
321 | /* Last char copied needs to be escaped */ | |
322 | *dst-- = '\\'; | |
323 | count--; | |
324 | } | |
cf7d32b8 | 325 | src--; |
2d21ac55 A |
326 | } |
327 | } | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
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. | |
337 | * | |
338 | * Currently prints following information - | |
339 | * 1. Mount Point | |
340 | */ | |
341 | void fslog_fs_corrupt(struct mount *mnt) | |
342 | { | |
343 | if (mnt != NULL) { | |
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, | |
348 | NULL); | |
349 | } | |
350 | } | |
351 | ||
352 | return; | |
353 | } | |
354 | ||
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 | |
359 | * 3. Device node | |
360 | * 4. Mount point | |
361 | * 5. Path for file, if any | |
362 | * 6. Error number | |
363 | * 7. Type of IO (read/write) | |
364 | */ | |
365 | void fslog_io_error(const buf_t bp) | |
366 | { | |
367 | int err; | |
368 | unsigned long msg_id; | |
369 | char blknum_str[21]; | |
370 | char lblknum_str[21]; | |
371 | char errno_str[6]; | |
372 | const char *iotype; | |
373 | unsigned char print_last = 0; | |
374 | vnode_t vp; | |
375 | ||
376 | if (buf_error(bp) == 0) { | |
377 | return; | |
378 | } | |
379 | ||
380 | /* Convert error number to string */ | |
381 | snprintf (errno_str, sizeof(errno_str), "%d", buf_error(bp)); | |
382 | ||
383 | /* Determine type of IO operation */ | |
384 | if (buf_flags(bp) & B_READ) { | |
385 | iotype = FSLOG_VAL_IOTYPE_READ; | |
386 | } else { | |
387 | iotype = FSLOG_VAL_IOTYPE_WRITE; | |
388 | } | |
389 | ||
390 | /* Convert physical block number to string */ | |
391 | snprintf (blknum_str, sizeof(blknum_str), "%lld", buf_blkno(bp)); | |
392 | ||
393 | /* Convert logical block number to string */ | |
394 | snprintf (lblknum_str, sizeof(lblknum_str), "%lld", buf_lblkno(bp)); | |
395 | ||
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, | |
402 | NULL); | |
403 | ||
404 | /* Access the vnode for this buffer */ | |
405 | vp = buf_vnode(bp); | |
406 | if (vp) { | |
407 | struct vfsstatfs *sp; | |
408 | mount_t mp; | |
409 | char *path; | |
410 | int len; | |
411 | struct vfs_context context; | |
412 | ||
413 | mp = vnode_mount(vp); | |
414 | /* mp should be NULL only for bdevvp during boot */ | |
415 | if (mp == NULL) { | |
416 | goto out; | |
417 | } | |
418 | sp = vfs_statfs(mp); | |
419 | ||
420 | /* Access the file path */ | |
421 | MALLOC(path, char *, MAXPATHLEN, M_TEMP, M_WAITOK); | |
422 | if (path) { | |
423 | len = MAXPATHLEN; | |
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, | |
428 | &context); | |
429 | if (!err) { | |
430 | err = escape_str(path, len, MAXPATHLEN); | |
431 | if (!err) { | |
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, | |
437 | NULL); | |
438 | print_last = 1; | |
439 | } | |
440 | } | |
441 | FREE(path, M_TEMP); | |
442 | } | |
443 | ||
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, | |
449 | NULL); | |
450 | print_last = 1; | |
451 | } | |
452 | } | |
453 | ||
454 | out: | |
455 | if (print_last == 0) { | |
456 | msg_id = fslog_err(msg_id | FSLOG_MSG_LAST, NULL); | |
457 | } | |
458 | ||
459 | return; | |
460 | } |