/
open.c
625 lines (536 loc) · 15.6 KB
/
open.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/** @internal @file src/kdumpfile/open.c
* @brief Routines for opening dumps.
*/
/* Copyright (C) 2014 Petr Tesarik <ptesarik@suse.cz>
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
libkdumpfile is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include "kdumpfile-priv.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/** File cache size.
* This number should be big enough to cover page table lookups with a
* scattered page table hierarchy, including a possible Xen mtop lookup
* in a separate hierarchy. The worst case seems to be 4-level paging with
* a subsequent lookup (4-level paging again, plus the lookup page) and
* a data page. That is 4 + 1 + 4 + 1 = 10. Let's add some reserve and use
* a beautirul power of two.
*/
#define FCACHE_SIZE 16
/** File cache page order.
* This number should be high enough to leverage transparent huge pages in
* the kernel (if possible), but small enough not to exhaust the virtual
* address space (especially on 32-bit platforms).
* Choosing 10 here results in 4M blocks on architectures with 4K pages
* and 64M blocks on architectures with 64K pages. In the latter case,
* virtual address space may a bit tight on a 32-bit platform.
*/
#define FCACHE_ORDER 10
static kdump_status open_dump(kdump_ctx_t *ctx);
static kdump_status finish_open_dump(kdump_ctx_t *ctx);
static const struct format_ops *formats[] = {
&elfdump_ops,
&qemu_ops,
&libvirt_ops,
&xc_save_ops,
&xc_core_ops,
&diskdump_ops,
&lkcd_ops,
&mclxcd_ops,
&s390dump_ops,
&sadump_ops,
&devmem_ops
};
/** Open the dump if all file descriptors have been specified.
* @param ctx Dump file object.
*/
static inline kdump_status
maybe_open_dump(kdump_ctx_t *ctx)
{
return get_num_files(ctx) && !ctx->shared->pendfiles
? open_dump(ctx)
: KDUMP_OK;
}
/** Set a file descriptor.
* @param ctx Dump file object.
* @param attr File descriptor attribute data.
* @param val New attribute value.
* @returns Error status.
*
* Adjust number of pending dump files if appropriate.
*/
static kdump_status
fdset_pre_hook(kdump_ctx_t *ctx, struct attr_data *attr,
kdump_attr_value_t *val)
{
if (!attr_isset(attr))
--ctx->shared->pendfiles;
return KDUMP_OK;
}
/** Unset a file descriptor.
* @param ctx Dump file object.
* @param attr File descriptor attribute data.
*/
static void
fdset_clear_hook(kdump_ctx_t *ctx, struct attr_data *attr)
{
if (attr_isset(attr))
++ctx->shared->pendfiles;
if (attr->parent->template->fidx == 0)
clear_attr(ctx, gattr(ctx, GKI_file_fd));
}
/** Maybe open the dump after setting a file descriptor.
* @param ctx Dump file object.
* @param attr File descriptor attribute data.
* @returns Error status.
*
* If no more files are pending, open the dump.
*/
static kdump_status
fdset_post_hook(kdump_ctx_t *ctx, struct attr_data *attr)
{
return maybe_open_dump(ctx);
}
static const struct attr_ops fdset_ops = {
.pre_set = fdset_pre_hook,
.pre_clear = fdset_clear_hook,
.post_set = fdset_post_hook,
};
/** Set number of dump files.
* @param ctx Dump file object.
* @param attr Attribute data to be changed.
* @param val New attribute value.
* @returns Error status.
*
* Set up attributes for a set of dump files.
*/
static kdump_status
num_files_pre_hook(kdump_ctx_t *ctx, struct attr_data *attr,
kdump_attr_value_t *val)
{
static const struct attr_template tmpl = {
.key = "fd",
.type = KDUMP_NUMBER,
.ops = &fdset_ops,
};
static const struct attr_template name_tmpl = {
.key = "name",
.type = KDUMP_STRING,
};
struct attr_template dir_tmpl = {
.type = KDUMP_DIRECTORY,
};
struct attr_data *parent = attr->parent;
char fdkey[21];
size_t keylen;
size_t n, i;
kdump_status ret;
/* Check that the new value fits into a size_t. */
n = val->number;
if (n != val->number)
return set_error(ctx, KDUMP_ERR_INVALID, "Number too big");
/* Allocate new attributes */
ret = KDUMP_OK;
for (i = attr_value(attr)->number; i < n; ++i) {
struct attr_data *dir, *fdattr, *nameattr;
keylen = sprintf(fdkey, "%zd", i);
dir_tmpl.fidx = i;
dir = create_attr_path(ctx->dict, parent,
fdkey, keylen, &dir_tmpl);
fdattr = dir
? new_attr(ctx->dict, dir, &tmpl)
: NULL;
nameattr = fdattr
? new_attr(ctx->dict, dir, &name_tmpl)
: NULL;
if (!nameattr) {
ret = set_error(ctx, KDUMP_ERR_SYSTEM,
"Cannot allocate file.set attributes");
n = attr_value(attr)->number;
break;
}
if (i == 0) {
fdattr->flags.indirect = 1;
fdattr->pval = attr_mut_value(gattr(ctx, GKI_file_fd));
}
}
/* Delete superfluous attributes. */
if (i > n) {
struct attr_data **pprev = &parent->dir;
while (*pprev) {
struct attr_data *dir = *pprev;
if (dir->template->type == KDUMP_DIRECTORY &&
dir->template->fidx >= n) {
*pprev = dir->next;
dealloc_attr(dir);
} else
pprev = &dir->next;
}
}
return ret;
}
/** Set number of dump files pending open.
* @param ctx Dump file object.
* @param attr Number of files attribute.
* @returns Error status.
*
* Set up the number of pending files.
*/
static kdump_status
num_files_post_hook(kdump_ctx_t *ctx, struct attr_data *attr)
{
if (attr_value(attr)->number != 1)
clear_attr(ctx, gattr(ctx, GKI_file_fd));
size_t pendfiles = 0;
struct attr_data *dir;
for (dir = attr->parent->dir; dir; dir = dir->next) {
struct attr_data *child;
if (dir->template->type != KDUMP_DIRECTORY ||
!(child = lookup_dir_attr(ctx->dict, dir, "fd", 2)))
continue;
if (!attr_isset(child))
++pendfiles;
}
ctx->shared->pendfiles = pendfiles;
return maybe_open_dump(ctx);
}
/** Attribute operations for file.set.number. */
const struct attr_ops num_files_ops = {
.pre_set = num_files_pre_hook,
.post_set = num_files_post_hook,
};
/** Set dump file descriptor.
* @param ctx Dump file object.
* @returns Error status.
*/
static kdump_status
file_fd_post_hook(kdump_ctx_t *ctx, struct attr_data *attr)
{
int fd = attr_value(attr)->number;
return internal_open_fdset(ctx, 1, &fd);
}
const struct attr_ops file_fd_ops = {
.post_set = file_fd_post_hook,
};
/** Open the dump.
* @param ctx Dump file object.
* @returns Error status.
*
* Probe a dump file for known file formats.
* On success, initialize the dump file context for use.
*/
static kdump_status
open_dump(kdump_ctx_t *ctx)
{
/* Attributes that point into ctx->shared->fcache */
static const enum global_keyidx fcache_attrs[] = {
GKI_file_mmap_policy,
GKI_mmap_cache_hits,
GKI_mmap_cache_misses,
GKI_read_cache_hits,
GKI_read_cache_misses,
};
size_t nfiles = get_num_files(ctx);
struct attr_data *dir;
struct attr_data *mmap_attr;
kdump_status ret;
int fdset[nfiles];
int i;
if (ctx->shared->fcache) {
for (i = 0; i < ARRAY_SIZE(fcache_attrs); ++i)
attr_embed_value(gattr(ctx, fcache_attrs[i]));
fcache_decref(ctx->shared->fcache);
}
for (dir = gattr(ctx, GKI_dir_file_set)->dir; dir; dir = dir->next) {
struct attr_data *child;
if (dir->template->type != KDUMP_DIRECTORY ||
!(child = lookup_dir_attr(ctx->dict, dir, "fd", 2)))
continue;
fdset[dir->template->fidx] = attr_value(child)->number;
}
ctx->shared->fcache = fcache_new(nfiles, fdset,
FCACHE_SIZE, FCACHE_ORDER);
if (!ctx->shared->fcache)
return set_error(ctx, KDUMP_ERR_SYSTEM,
"Cannot allocate file cache");
mmap_attr = gattr(ctx, GKI_file_mmap_policy);
ctx->shared->fcache->mmap_policy = *attr_value(mmap_attr);
set_attr(ctx, mmap_attr, ATTR_PERSIST_INDIRECT,
&ctx->shared->fcache->mmap_policy);
cache_set_attrs(ctx->shared->fcache->cache, ctx,
gattr(ctx, GKI_mmap_cache_hits),
gattr(ctx, GKI_mmap_cache_misses));
cache_set_attrs(ctx->shared->fcache->fbcache, ctx,
gattr(ctx, GKI_read_cache_hits),
gattr(ctx, GKI_read_cache_misses));
ctx->xlat->dirty = true;
for (i = 0; i < ARRAY_SIZE(formats); ++i) {
ctx->shared->ops = formats[i];
ret = ctx->shared->ops->probe(ctx);
if (ret == KDUMP_OK)
return finish_open_dump(ctx);
if (ret != KDUMP_NOPROBE)
return ret;
ctx->shared->ops = NULL;
if (ctx->shared->cache) {
cache_free(ctx->shared->cache);
ctx->shared->cache = NULL;
}
clear_volatile_attrs(ctx);
clear_error(ctx);
}
return set_error(ctx, KDUMP_ERR_NOTIMPL, "Unknown file format");
}
/** Finish opening a dump file of a known file format.
* @param ctx Dump file object.
* @returns Error status.
*/
static kdump_status
finish_open_dump(kdump_ctx_t *ctx)
{
set_attr_static_string(ctx, gattr(ctx, GKI_file_format),
ATTR_DEFAULT, ctx->shared->ops->name);
return KDUMP_OK;
}
/** Clear file.set.x.fd attributes
* @param ctx Dump file object.
*
* Clear all file descriptors. This is done before installing a new file
* descriptor set to make sure that previously set file descriptor are not
* used unexpectedly.
*/
static void
clear_all_fds(kdump_ctx_t *ctx)
{
struct attr_data *dir = gattr(ctx, GKI_dir_file_set);
for (dir = dir->dir; dir; dir = dir->next) {
struct attr_data *child;
if (dir->template->type == KDUMP_DIRECTORY &&
(child = lookup_dir_attr(ctx->dict, dir, "fd", 2)))
clear_attr(ctx, child);
}
}
kdump_status
kdump_set_filenames(kdump_ctx_t *ctx, unsigned n, const char *const *names)
{
struct attr_data *dir;
kdump_status status;
clear_error(ctx);
if (get_num_files(ctx) < n &&
(status = set_attr_number(ctx, gattr(ctx, GKI_num_files),
ATTR_PERSIST, n)) != KDUMP_OK)
return set_error(ctx, status,
"Cannot initialize file set size");
for (dir = gattr(ctx, GKI_dir_file_set)->dir; dir; dir = dir->next) {
struct attr_data *child;
unsigned fidx;
if (dir->template->type != KDUMP_DIRECTORY)
continue;
fidx = dir->template->fidx;
if (fidx >= n)
continue;
child = lookup_dir_attr(ctx->dict, dir, "name", 4);
if (!child)
continue;
if (names[fidx]) {
status = set_attr_string(ctx, child, ATTR_PERSIST,
names[fidx]);
if (status != KDUMP_OK)
return set_error(ctx, status, "%s",
err_filename(ctx, fidx));
} else
clear_attr(ctx, child);
}
return KDUMP_OK;
}
DEFINE_ALIAS(open_fdset);
kdump_status
kdump_open_fdset(kdump_ctx_t *ctx, unsigned nfds, const int *fds)
{
struct attr_data *dir;
kdump_status status;
clear_error(ctx);
/* Make sure we do not use a stale file descriptor value. */
clear_all_fds(ctx);
status = set_attr_number(ctx, gattr(ctx, GKI_num_files),
ATTR_PERSIST, nfds);
if (status != KDUMP_OK)
return set_error(ctx, status,
"Cannot initialize file set size");
for (dir = gattr(ctx, GKI_dir_file_set)->dir; dir; dir = dir->next) {
struct attr_data *child;
if (dir->template->type != KDUMP_DIRECTORY ||
!(child = lookup_dir_attr(ctx->dict, dir, "fd", 2)))
continue;
status = set_attr_number(ctx, child, ATTR_PERSIST,
fds[dir->template->fidx]);
if (status != KDUMP_OK)
return set_error(ctx, status, "%s",
err_filename(ctx, dir->template->fidx));
}
return KDUMP_OK;
}
/* struct new_utsname is inside struct uts_namespace, preceded by a struct
* kref, but the offset is not stored in VMCOREINFO. So, search some sane
* amount of memory for UTS_SYSNAME, which can be used as kind of a magic
* signature.
*/
static kdump_status
uts_name_from_init_uts_ns(kdump_ctx_t *ctx, kdump_vaddr_t *uts_name)
{
kdump_vaddr_t init_uts_ns;
char buf[2 * NEW_UTS_LEN + sizeof(UTS_SYSNAME)];
char *p;
size_t rd;
kdump_status ret;
rwlock_unlock(&ctx->shared->lock);
ret = get_symbol_val(ctx, "init_uts_ns", &init_uts_ns);
rwlock_wrlock(&ctx->shared->lock);
if (ret != KDUMP_OK)
return ret;
rd = sizeof buf;
ret = read_locked(ctx, KDUMP_KVADDR, init_uts_ns, buf, &rd);
if (ret != KDUMP_OK)
return ret;
for (p = buf; p <= &buf[2 * NEW_UTS_LEN]; ++p)
if (!memcmp(p, UTS_SYSNAME, sizeof(UTS_SYSNAME)))
break;
if (p > &buf[2 * NEW_UTS_LEN])
return set_error(ctx, KDUMP_ERR_CORRUPT, "UTS_SYSNAME not found");
*uts_name = init_uts_ns + p - buf;
return KDUMP_OK;
}
static kdump_status
update_linux_utsname(kdump_ctx_t *ctx)
{
kdump_vaddr_t uts_name;
struct new_utsname uts;
size_t rd;
kdump_status ret;
if (attr_isset(gattr(ctx, GKI_linux_uts_sysname)))
return KDUMP_OK;
rwlock_unlock(&ctx->shared->lock);
ret = get_symbol_val(ctx, "system_utsname", &uts_name);
rwlock_wrlock(&ctx->shared->lock);
if (ret == KDUMP_ERR_NODATA) {
clear_error(ctx);
ret = uts_name_from_init_uts_ns(ctx, &uts_name);
}
if (ret == KDUMP_ERR_NODATA || ret == KDUMP_ERR_ADDRXLAT) {
clear_error(ctx);
return KDUMP_OK;
}
if (ret != KDUMP_OK)
return ret;
rd = sizeof uts;
ret = read_locked(ctx, KDUMP_KVADDR, uts_name,
(unsigned char*)&uts, &rd);
if (ret != KDUMP_OK)
return ret;
if (!uts_looks_sane(&uts))
return set_error(ctx, KDUMP_ERR_CORRUPT,
"Wrong utsname content");
set_uts(ctx, &uts);
return KDUMP_OK;
}
/** Read the Xen extra version string.
* @param ctx Dump file object.
* @returns Error status.
*/
static kdump_status
update_xen_extra_ver(kdump_ctx_t *ctx)
{
static const char desc[] = "Xen extra version";
struct attr_data *attr;
char *extra;
kdump_status status;
attr = gattr(ctx, GKI_xen_ver_extra_addr);
if (!attr_isset(attr))
return KDUMP_OK;
status = attr_revalidate(ctx, attr);
if (status != KDUMP_OK)
return set_error(ctx, status, "Cannot locate %s", desc);
status = read_string_locked(ctx, KDUMP_MACHPHYSADDR,
attr_value(attr)->address, &extra);
if (status == KDUMP_ERR_NODATA) {
/* Missing data is not fatal here. */
clear_error(ctx);
return KDUMP_OK;
}
if (status != KDUMP_OK)
return set_error(ctx, status, "Cannot read %s", desc);
status = set_attr_string(ctx, gattr(ctx, GKI_xen_ver_extra),
ATTR_DEFAULT, extra);
free(extra);
if (status != KDUMP_OK)
return set_error(ctx, status, "Cannot set %s", desc);
return KDUMP_OK;
}
static kdump_status
ostype_pre_hook(kdump_ctx_t *ctx, struct attr_data *attr,
kdump_attr_value_t *val)
{
if (!(strcmp(val->string, "linux"))) {
ctx->xlat->osdir = GKI_dir_linux;
} else if (!strcmp(val->string, "xen")) {
ctx->xlat->osdir = GKI_dir_xen;
} else
return set_error(ctx, KDUMP_ERR_NOTIMPL,
"Unsupported OS type");
return KDUMP_OK;
}
static kdump_status
ostype_post_hook(kdump_ctx_t *ctx, struct attr_data *attr)
{
kdump_status status;
ctx->xlat->dirty = true;
if (ctx->shared->arch_ops && ctx->shared->arch_ops->post_ostype) {
status = ctx->shared->arch_ops->post_ostype(ctx);
if (status != KDUMP_OK)
return status;
}
switch (ctx->xlat->osdir) {
case GKI_dir_linux:
status = update_linux_utsname(ctx);
if (status != KDUMP_OK)
return status;
/* fall through */
case GKI_dir_xen:
status = update_xen_extra_ver(ctx);
if (status != KDUMP_OK)
return status;
break;
default:
break;
}
return KDUMP_OK;
}
static void
ostype_clear_hook(kdump_ctx_t *ctx, struct attr_data *attr)
{
ctx->xlat->osdir = NR_GLOBAL_ATTRS;
ctx->xlat->dirty = true;
}
const struct attr_ops ostype_ops = {
.pre_set = ostype_pre_hook,
.post_set = ostype_post_hook,
.pre_clear = ostype_clear_hook,
};