]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_gzio.c
mtx: pre-read the lock value in thread_lock_flags_
[FreeBSD/FreeBSD.git] / sys / kern / kern_gzio.c
1 /*-
2  * Copyright (c) 2014 Mark Johnston <markj@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in
11  *    the documentation and/or other materials provided with the
12  *    distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31
32 #include <sys/gzio.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/zutil.h>
36
37 #define KERN_GZ_HDRLEN          10      /* gzip header length */
38 #define KERN_GZ_TRAILERLEN      8       /* gzip trailer length */
39 #define KERN_GZ_MAGIC1          0x1f    /* first magic byte */
40 #define KERN_GZ_MAGIC2          0x8b    /* second magic byte */
41
42 MALLOC_DEFINE(M_GZIO, "gzio", "zlib state");
43
44 struct gzio_stream {
45         uint8_t *       gz_buffer;      /* output buffer */
46         size_t          gz_bufsz;       /* total buffer size */
47         off_t           gz_off;         /* offset into the output stream */
48         enum gzio_mode  gz_mode;        /* stream mode */
49         uint32_t        gz_crc;         /* stream CRC32 */
50         gzio_cb         gz_cb;          /* output callback */
51         void *          gz_arg;         /* private callback arg */
52         z_stream        gz_stream;      /* zlib state */
53 };
54
55 static void *   gz_alloc(void *, u_int, u_int);
56 static void     gz_free(void *, void *);
57 static int      gz_write(struct gzio_stream *, void *, u_int, int);
58
59 struct gzio_stream *
60 gzio_init(gzio_cb cb, enum gzio_mode mode, size_t bufsz, int level, void *arg)
61 {
62         struct gzio_stream *s;
63         int error;
64
65         if (bufsz < KERN_GZ_HDRLEN)
66                 return (NULL);
67         if (mode != GZIO_DEFLATE)
68                 return (NULL);
69
70         s = gz_alloc(NULL, 1, sizeof(*s));
71         s->gz_bufsz = bufsz;
72         s->gz_buffer = gz_alloc(NULL, 1, s->gz_bufsz);
73         s->gz_mode = mode;
74         s->gz_cb = cb;
75         s->gz_arg = arg;
76
77         s->gz_stream.zalloc = gz_alloc;
78         s->gz_stream.zfree = gz_free;
79         s->gz_stream.opaque = NULL;
80         s->gz_stream.next_in = Z_NULL;
81         s->gz_stream.avail_in = 0;
82
83         error = deflateInit2(&s->gz_stream, level, Z_DEFLATED, -MAX_WBITS,
84             DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
85         if (error != 0)
86                 goto fail;
87
88         gzio_reset(s);
89
90         return (s);
91
92 fail:
93         gz_free(NULL, s->gz_buffer);
94         gz_free(NULL, s);
95         return (NULL);
96 }
97
98 void
99 gzio_reset(struct gzio_stream *s)
100 {
101         uint8_t *hdr;
102
103         (void)deflateReset(&s->gz_stream);
104
105         s->gz_off = 0;
106         s->gz_crc = ~0U;
107
108         s->gz_stream.avail_out = s->gz_bufsz;
109         s->gz_stream.next_out = s->gz_buffer;
110
111         /* Write the gzip header to the output buffer. */
112         hdr = s->gz_buffer;
113         memset(hdr, 0, KERN_GZ_HDRLEN);
114         hdr[0] = KERN_GZ_MAGIC1;
115         hdr[1] = KERN_GZ_MAGIC2;
116         hdr[2] = Z_DEFLATED;
117         hdr[9] = OS_CODE;
118         s->gz_stream.next_out += KERN_GZ_HDRLEN;
119         s->gz_stream.avail_out -= KERN_GZ_HDRLEN;
120 }
121
122 int
123 gzio_write(struct gzio_stream *s, void *data, u_int len)
124 {
125
126         return (gz_write(s, data, len, Z_NO_FLUSH));
127 }
128
129 int
130 gzio_flush(struct gzio_stream *s)
131 {
132
133         return (gz_write(s, NULL, 0, Z_FINISH));
134 }
135
136 void
137 gzio_fini(struct gzio_stream *s)
138 {
139
140         (void)deflateEnd(&s->gz_stream);
141         gz_free(NULL, s->gz_buffer);
142         gz_free(NULL, s);
143 }
144
145 static void *
146 gz_alloc(void *arg __unused, u_int n, u_int sz)
147 {
148
149         /*
150          * Memory for zlib state is allocated using M_NODUMP since it may be
151          * used to compress a kernel dump, and we don't want zlib to attempt to
152          * compress its own state.
153          */
154         return (malloc(n * sz, M_GZIO, M_WAITOK | M_ZERO | M_NODUMP));
155 }
156
157 static void
158 gz_free(void *arg __unused, void *ptr)
159 {
160
161         free(ptr, M_GZIO);
162 }
163
164 static int
165 gz_write(struct gzio_stream *s, void *buf, u_int len, int zflag)
166 {
167         uint8_t trailer[KERN_GZ_TRAILERLEN];
168         size_t room;
169         int error, zerror;
170
171         KASSERT(zflag == Z_FINISH || zflag == Z_NO_FLUSH,
172             ("unexpected flag %d", zflag));
173         KASSERT(s->gz_mode == GZIO_DEFLATE,
174             ("invalid stream mode %d", s->gz_mode));
175
176         if (len > 0) {
177                 s->gz_stream.avail_in = len;
178                 s->gz_stream.next_in = buf;
179                 s->gz_crc = crc32_raw(buf, len, s->gz_crc);
180         } else
181                 s->gz_crc ^= ~0U;
182
183         error = 0;
184         do {
185                 zerror = deflate(&s->gz_stream, zflag);
186                 if (zerror != Z_OK && zerror != Z_STREAM_END) {
187                         error = EIO;
188                         break;
189                 }
190
191                 if (s->gz_stream.avail_out == 0 || zerror == Z_STREAM_END) {
192                         /*
193                          * Our output buffer is full or there's nothing left
194                          * to produce, so we're flushing the buffer.
195                          */
196                         len = s->gz_bufsz - s->gz_stream.avail_out;
197                         if (zerror == Z_STREAM_END) {
198                                 /*
199                                  * Try to pack as much of the trailer into the
200                                  * output buffer as we can.
201                                  */
202                                 ((uint32_t *)trailer)[0] = s->gz_crc;
203                                 ((uint32_t *)trailer)[1] =
204                                     s->gz_stream.total_in;
205                                 room = MIN(KERN_GZ_TRAILERLEN,
206                                     s->gz_bufsz - len);
207                                 memcpy(s->gz_buffer + len, trailer, room);
208                                 len += room;
209                         }
210
211                         error = s->gz_cb(s->gz_buffer, len, s->gz_off,
212                             s->gz_arg);
213                         if (error != 0)
214                                 break;
215
216                         s->gz_off += len;
217                         s->gz_stream.next_out = s->gz_buffer;
218                         s->gz_stream.avail_out = s->gz_bufsz;
219
220                         /*
221                          * If we couldn't pack the trailer into the output
222                          * buffer, write it out now.
223                          */
224                         if (zerror == Z_STREAM_END && room < KERN_GZ_TRAILERLEN)
225                                 error = s->gz_cb(trailer + room,
226                                     KERN_GZ_TRAILERLEN - room, s->gz_off,
227                                     s->gz_arg);
228                 }
229         } while (zerror != Z_STREAM_END &&
230             (zflag == Z_FINISH || s->gz_stream.avail_in > 0));
231
232         return (error);
233 }