]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_vnodedumper.c
Add new vnode dumper to support live minidumps
[FreeBSD/FreeBSD.git] / sys / kern / kern_vnodedumper.c
1 /*-
2  * Copyright (c) 2021-2022 Juniper Networks
3  *
4  * This software was developed by Mitchell Horne <mhorne@FreeBSD.org>
5  * under sponsorship from Juniper Networks and Klara Systems.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/caprights.h>
33 #include <sys/disk.h>
34 #include <sys/fcntl.h>
35 #include <sys/file.h>
36 #include <sys/kerneldump.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/namei.h>
40 #include <sys/priv.h>
41 #include <sys/proc.h>
42 #include <sys/stat.h>
43 #include <sys/sysctl.h>
44 #include <sys/vnode.h>
45
46 #include <machine/vmparam.h>
47
48 static dumper_start_t vnode_dumper_start;
49 static dumper_t vnode_dump;
50 static dumper_hdr_t vnode_write_headers;
51
52 static struct sx livedump_sx;
53 SX_SYSINIT(livedump, &livedump_sx, "Livedump sx");
54
55 /*
56  * Invoke a live minidump on the system.
57  */
58 int
59 livedump_start(int fd, int flags, uint8_t compression)
60 {
61 #if MINIDUMP_PAGE_TRACKING == 1
62         struct dumperinfo di, *livedi;
63         struct diocskerneldump_arg kda;
64         struct vnode *vp;
65         struct file *fp;
66         void *rl_cookie;
67         int error;
68
69         error = priv_check(curthread, PRIV_KMEM_READ);
70         if (error != 0)
71                 return (error);
72
73         if (flags != 0)
74                 return (EINVAL);
75
76         error = getvnode(curthread, fd, &cap_write_rights, &fp);
77         if (error != 0)
78                 return (error);
79         vp = fp->f_vnode;
80
81         if ((fp->f_flag & FWRITE) == 0) {
82                 error = EBADF;
83                 goto drop;
84         }
85
86         /* Set up a new dumper. */
87         bzero(&di, sizeof(di));
88         di.dumper_start = vnode_dumper_start;
89         di.dumper = vnode_dump;
90         di.dumper_hdr = vnode_write_headers;
91         di.blocksize = PAGE_SIZE; /* Arbitrary. */
92         di.maxiosize = MAXDUMPPGS * PAGE_SIZE;
93
94         bzero(&kda, sizeof(kda));
95         kda.kda_compression = compression;
96         error = dumper_create(&di, "livedump", &kda, &livedi);
97         if (error != 0)
98                 goto drop;
99
100         /* Only allow one livedump to proceed at a time. */
101         if (sx_try_xlock(&livedump_sx) == 0) {
102                 dumper_destroy(livedi);
103                 error = EBUSY;
104                 goto drop;
105         }
106
107         /* To be used by the callback functions. */
108         livedi->priv = vp;
109
110         /* Lock the entire file range and vnode. */
111         rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
112         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
113
114         dump_savectx();
115         error = minidumpsys(livedi, true);
116
117         VOP_UNLOCK(vp);
118         vn_rangelock_unlock(vp, rl_cookie);
119         sx_xunlock(&livedump_sx);
120         dumper_destroy(livedi);
121 drop:
122         fdrop(fp, curthread);
123         return (error);
124 #else
125         return (EOPNOTSUPP);
126 #endif /* MINIDUMP_PAGE_TRACKING == 1 */
127 }
128
129 int
130 vnode_dumper_start(struct dumperinfo *di, void *key, uint32_t keysize)
131 {
132
133         /* Always begin with an offset of zero. */
134         di->dumpoff = 0;
135
136         KASSERT(keysize == 0, ("encryption not supported for livedumps"));
137         return (0);
138 }
139
140 /*
141  * Callback from dumpsys() to dump a chunk of memory.
142  *
143  * Parameters:
144  *      arg      Opaque private pointer to vnode
145  *      virtual  Virtual address (where to read the data from)
146  *      physical Physical memory address (unused)
147  *      offset   Offset from start of core file
148  *      length   Data length
149  *
150  * Return value:
151  *      0 on success
152  *      errno on error
153  */
154 int
155 vnode_dump(void *arg, void *virtual, vm_offset_t physical __unused,
156     off_t offset, size_t length)
157 {
158         struct vnode *vp;
159         int error = 0;
160
161         vp = arg;
162         MPASS(vp != NULL);
163         ASSERT_VOP_LOCKED(vp, __func__);
164
165         /* Done? */
166         if (virtual == NULL)
167                 return (0);
168
169         error = vn_rdwr(UIO_WRITE, vp, virtual, length, offset, UIO_SYSSPACE,
170             IO_NODELOCKED, curthread->td_ucred, NOCRED, NULL, curthread);
171         if (error != 0)
172                 uprintf("%s: error writing livedump block at offset %jx: %d\n",
173                     __func__, (uintmax_t)offset, error);
174         return (error);
175 }
176
177 /*
178  * Callback from dumpsys() to write out the dump header, placed at the end.
179  */
180 int
181 vnode_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh)
182 {
183         struct vnode *vp;
184         int error;
185         off_t offset;
186
187         vp = di->priv;
188         MPASS(vp != NULL);
189         ASSERT_VOP_LOCKED(vp, __func__);
190
191         /* Compensate for compression/encryption adjustment of dumpoff. */
192         offset = roundup2(di->dumpoff, di->blocksize);
193
194         /* Write the kernel dump header to the end of the file. */
195         error = vn_rdwr(UIO_WRITE, vp, kdh, sizeof(*kdh), offset,
196             UIO_SYSSPACE, IO_NODELOCKED, curthread->td_ucred, NOCRED, NULL,
197             curthread);
198         if (error != 0)
199                 uprintf("%s: error writing livedump header: %d\n", __func__,
200                     error);
201         return (error);
202 }