1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _G_JOURNAL_H_ 32 #define _G_JOURNAL_H_ 33 34 #include <sys/endian.h> 35 #include <sys/md5.h> 36 #ifdef _KERNEL 37 #include <sys/bio.h> 38 #endif 39 40 #define G_JOURNAL_CLASS_NAME "JOURNAL" 41 42 #define G_JOURNAL_MAGIC "GEOM::JOURNAL" 43 /* 44 * Version history: 45 * 0 - Initial version number. 46 */ 47 #define G_JOURNAL_VERSION 0 48 49 #ifdef _KERNEL 50 extern int g_journal_debug; 51 52 #define GJ_DEBUG(lvl, ...) \ 53 _GEOM_DEBUG("GEOM_JOURNAL", g_journal_debug, (lvl), NULL, __VA_ARGS__) 54 #define GJ_LOGREQ(lvl, bp, ...) \ 55 _GEOM_DEBUG("GEOM_JOURNAL", g_journal_debug, (lvl), (bp), __VA_ARGS__) 56 57 #define JEMPTY(sc) ((sc)->sc_journal_offset - \ 58 (sc)->sc_jprovider->sectorsize == \ 59 (sc)->sc_active.jj_offset && \ 60 (sc)->sc_current_count == 0) 61 62 #define GJ_BIO_REGULAR 0x00 63 #define GJ_BIO_READ 0x01 64 #define GJ_BIO_JOURNAL 0x02 65 #define GJ_BIO_COPY 0x03 66 #define GJ_BIO_MASK 0x0f 67 68 #if 0 69 #define GJF_BIO_DONT_FREE 0x10 70 #define GJF_BIO_MASK 0xf0 71 #endif 72 73 #define GJF_DEVICE_HARDCODED 0x0001 74 #define GJF_DEVICE_DESTROY 0x0010 75 #define GJF_DEVICE_SWITCH 0x0020 76 #define GJF_DEVICE_BEFORE_SWITCH 0x0040 77 #define GJF_DEVICE_CLEAN 0x0080 78 #define GJF_DEVICE_CHECKSUM 0x0100 79 80 #define GJ_HARD_LIMIT 64 81 82 /* 83 * We keep pointers to journaled data in bio structure and because we 84 * need to store two off_t values (offset in data provider and offset in 85 * journal), we have to borrow bio_completed field for this. 86 */ 87 #define bio_joffset bio_completed 88 /* 89 * Use bio_caller1 field as a pointer in queue. 90 */ 91 #define bio_next bio_caller1 92 93 /* 94 * There are two such structures maintained inside each journaled device. 95 * One describes active part of the journal, were recent requests are stored. 96 * The second describes the last consistent part of the journal with requests 97 * that are copied to the destination provider. 98 */ 99 struct g_journal_journal { 100 struct bio *jj_queue; /* Cached journal entries. */ 101 off_t jj_offset; /* Journal's start offset. */ 102 }; 103 104 struct g_journal_softc { 105 uint32_t sc_id; 106 uint8_t sc_type; 107 uint8_t sc_orig_type; 108 struct g_geom *sc_geom; 109 u_int sc_flags; 110 struct mtx sc_mtx; 111 off_t sc_mediasize; 112 u_int sc_sectorsize; 113 #define GJ_FLUSH_DATA 0x01 114 #define GJ_FLUSH_JOURNAL 0x02 115 u_int sc_bio_flush; 116 117 uint32_t sc_journal_id; 118 uint32_t sc_journal_next_id; 119 int sc_journal_copying; 120 off_t sc_journal_offset; 121 off_t sc_journal_previous_id; 122 123 struct bio_queue_head sc_back_queue; 124 struct bio_queue_head sc_regular_queue; 125 126 struct bio_queue_head sc_delayed_queue; 127 int sc_delayed_count; 128 129 struct bio *sc_current_queue; 130 int sc_current_count; 131 132 struct bio *sc_flush_queue; 133 int sc_flush_count; 134 int sc_flush_in_progress; 135 136 struct bio *sc_copy_queue; 137 int sc_copy_in_progress; 138 139 struct g_consumer *sc_dconsumer; 140 struct g_consumer *sc_jconsumer; 141 142 struct g_journal_journal sc_inactive; 143 struct g_journal_journal sc_active; 144 145 off_t sc_jstart; /* Journal space start offset. */ 146 off_t sc_jend; /* Journal space end offset. */ 147 148 struct callout sc_callout; 149 struct proc *sc_worker; 150 151 struct root_hold_token *sc_rootmount; 152 }; 153 #define sc_dprovider sc_dconsumer->provider 154 #define sc_jprovider sc_jconsumer->provider 155 #define sc_name sc_dprovider->name 156 157 #define GJQ_INSERT_HEAD(head, bp) do { \ 158 (bp)->bio_next = (head); \ 159 (head) = (bp); \ 160 } while (0) 161 #define GJQ_INSERT_AFTER(head, bp, pbp) do { \ 162 if ((pbp) == NULL) \ 163 GJQ_INSERT_HEAD(head, bp); \ 164 else { \ 165 (bp)->bio_next = (pbp)->bio_next; \ 166 (pbp)->bio_next = (bp); \ 167 } \ 168 } while (0) 169 #define GJQ_LAST(head, bp) do { \ 170 struct bio *_bp; \ 171 \ 172 if ((head) == NULL) { \ 173 (bp) = (head); \ 174 break; \ 175 } \ 176 for (_bp = (head); _bp->bio_next != NULL; _bp = _bp->bio_next) \ 177 continue; \ 178 (bp) = (_bp); \ 179 } while (0) 180 #define GJQ_FIRST(head) (head) 181 #define GJQ_REMOVE(head, bp) do { \ 182 struct bio *_bp; \ 183 \ 184 if ((head) == (bp)) { \ 185 (head) = (bp)->bio_next; \ 186 (bp)->bio_next = NULL; \ 187 break; \ 188 } \ 189 for (_bp = (head); _bp->bio_next != NULL; _bp = _bp->bio_next) {\ 190 if (_bp->bio_next == (bp)) \ 191 break; \ 192 } \ 193 KASSERT(_bp->bio_next != NULL, ("NULL bio_next")); \ 194 KASSERT(_bp->bio_next == (bp), ("bio_next != bp")); \ 195 _bp->bio_next = (bp)->bio_next; \ 196 (bp)->bio_next = NULL; \ 197 } while (0) 198 #define GJQ_FOREACH(head, bp) \ 199 for ((bp) = (head); (bp) != NULL; (bp) = (bp)->bio_next) 200 201 #define GJ_HEADER_MAGIC "GJHDR" 202 203 struct g_journal_header { 204 char jh_magic[sizeof(GJ_HEADER_MAGIC)]; 205 uint32_t jh_journal_id; 206 uint32_t jh_journal_next_id; 207 } __packed; 208 209 struct g_journal_entry { 210 uint64_t je_joffset; 211 uint64_t je_offset; 212 uint64_t je_length; 213 } __packed; 214 215 #define GJ_RECORD_HEADER_MAGIC "GJRHDR" 216 #define GJ_RECORD_HEADER_NENTRIES (20) 217 #define GJ_RECORD_MAX_SIZE(sc) \ 218 ((sc)->sc_jprovider->sectorsize + GJ_RECORD_HEADER_NENTRIES * MAXPHYS) 219 #define GJ_VALIDATE_OFFSET(offset, sc) do { \ 220 if ((offset) + GJ_RECORD_MAX_SIZE(sc) >= (sc)->sc_jend) { \ 221 (offset) = (sc)->sc_jstart; \ 222 GJ_DEBUG(2, "Starting from the beginning (%s).", \ 223 (sc)->sc_name); \ 224 } \ 225 } while (0) 226 227 struct g_journal_record_header { 228 char jrh_magic[sizeof(GJ_RECORD_HEADER_MAGIC)]; 229 uint32_t jrh_journal_id; 230 uint16_t jrh_nentries; 231 u_char jrh_sum[8]; 232 struct g_journal_entry jrh_entries[GJ_RECORD_HEADER_NENTRIES]; 233 } __packed; 234 235 typedef int (g_journal_clean_t)(struct mount *mp); 236 typedef void (g_journal_dirty_t)(struct g_consumer *cp); 237 238 struct g_journal_desc { 239 const char *jd_fstype; 240 g_journal_clean_t *jd_clean; 241 g_journal_dirty_t *jd_dirty; 242 }; 243 244 /* Supported file systems. */ 245 extern const struct g_journal_desc g_journal_ufs; 246 247 #define GJ_TIMER_START(lvl, bt) do { \ 248 if (g_journal_debug >= (lvl)) \ 249 binuptime(bt); \ 250 } while (0) 251 #define GJ_TIMER_STOP(lvl, bt, ...) do { \ 252 if (g_journal_debug >= (lvl)) { \ 253 struct bintime _bt2; \ 254 struct timeval _tv; \ 255 \ 256 binuptime(&_bt2); \ 257 bintime_sub(&_bt2, bt); \ 258 bintime2timeval(&_bt2, &_tv); \ 259 printf("GEOM_JOURNAL"); \ 260 if (g_journal_debug > 0) \ 261 printf("[%u]", lvl); \ 262 printf(": "); \ 263 printf(__VA_ARGS__); \ 264 printf(": %jd.%06jds\n", (intmax_t)_tv.tv_sec, \ 265 (intmax_t)_tv.tv_usec); \ 266 } \ 267 } while (0) 268 #endif /* _KERNEL */ 269 270 #define GJ_TYPE_DATA 0x01 271 #define GJ_TYPE_JOURNAL 0x02 272 #define GJ_TYPE_COMPLETE (GJ_TYPE_DATA|GJ_TYPE_JOURNAL) 273 274 #define GJ_FLAG_CLEAN 0x01 275 #define GJ_FLAG_CHECKSUM 0x02 276 277 struct g_journal_metadata { 278 char md_magic[16]; /* Magic value. */ 279 uint32_t md_version; /* Version number. */ 280 uint32_t md_id; /* Journal unique ID. */ 281 uint8_t md_type; /* Provider type. */ 282 uint64_t md_jstart; /* Journal space start offset. */ 283 uint64_t md_jend; /* Journal space end offset. */ 284 uint64_t md_joffset; /* Last known consistent journal offset. */ 285 uint32_t md_jid; /* Last known consistent journal ID. */ 286 uint64_t md_flags; /* Journal flags. */ 287 char md_provider[16]; /* Hardcoded provider. */ 288 uint64_t md_provsize; /* Provider's size. */ 289 u_char md_hash[16]; /* MD5 hash. */ 290 }; 291 static __inline void 292 journal_metadata_encode(struct g_journal_metadata *md, u_char *data) 293 { 294 MD5_CTX ctx; 295 296 bcopy(md->md_magic, data, 16); 297 le32enc(data + 16, md->md_version); 298 le32enc(data + 20, md->md_id); 299 *(data + 24) = md->md_type; 300 le64enc(data + 25, md->md_jstart); 301 le64enc(data + 33, md->md_jend); 302 le64enc(data + 41, md->md_joffset); 303 le32enc(data + 49, md->md_jid); 304 le64enc(data + 53, md->md_flags); 305 bcopy(md->md_provider, data + 61, 16); 306 le64enc(data + 77, md->md_provsize); 307 MD5Init(&ctx); 308 MD5Update(&ctx, data, 85); 309 MD5Final(md->md_hash, &ctx); 310 bcopy(md->md_hash, data + 85, 16); 311 } 312 static __inline int 313 journal_metadata_decode_v0(const u_char *data, struct g_journal_metadata *md) 314 { 315 MD5_CTX ctx; 316 317 md->md_id = le32dec(data + 20); 318 md->md_type = *(data + 24); 319 md->md_jstart = le64dec(data + 25); 320 md->md_jend = le64dec(data + 33); 321 md->md_joffset = le64dec(data + 41); 322 md->md_jid = le32dec(data + 49); 323 md->md_flags = le64dec(data + 53); 324 bcopy(data + 61, md->md_provider, 16); 325 md->md_provsize = le64dec(data + 77); 326 MD5Init(&ctx); 327 MD5Update(&ctx, data, 85); 328 MD5Final(md->md_hash, &ctx); 329 if (bcmp(md->md_hash, data + 85, 16) != 0) 330 return (EINVAL); 331 return (0); 332 } 333 static __inline int 334 journal_metadata_decode(const u_char *data, struct g_journal_metadata *md) 335 { 336 int error; 337 338 bcopy(data, md->md_magic, 16); 339 md->md_version = le32dec(data + 16); 340 switch (md->md_version) { 341 case 0: 342 error = journal_metadata_decode_v0(data, md); 343 break; 344 default: 345 error = EINVAL; 346 break; 347 } 348 return (error); 349 } 350 351 static __inline void 352 journal_metadata_dump(const struct g_journal_metadata *md) 353 { 354 static const char hex[] = "0123456789abcdef"; 355 char hash[16 * 2 + 1]; 356 u_int i; 357 358 printf(" magic: %s\n", md->md_magic); 359 printf(" version: %u\n", (u_int)md->md_version); 360 printf(" id: %u\n", (u_int)md->md_id); 361 printf(" type: %u\n", (u_int)md->md_type); 362 printf(" start: %ju\n", (uintmax_t)md->md_jstart); 363 printf(" end: %ju\n", (uintmax_t)md->md_jend); 364 printf(" joffset: %ju\n", (uintmax_t)md->md_joffset); 365 printf(" jid: %u\n", (u_int)md->md_jid); 366 printf(" flags: %u\n", (u_int)md->md_flags); 367 printf("hcprovider: %s\n", md->md_provider); 368 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize); 369 bzero(hash, sizeof(hash)); 370 for (i = 0; i < 16; i++) { 371 hash[i * 2] = hex[md->md_hash[i] >> 4]; 372 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 373 } 374 printf(" MD5 hash: %s\n", hash); 375 } 376 #endif /* !_G_JOURNAL_H_ */ 377