1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012 by Delphix. All rights reserved. 24 */ 25 26#include <libzfs.h> 27 28#include <sys/zfs_context.h> 29 30#include <errno.h> 31#include <fcntl.h> 32#include <stdarg.h> 33#include <stddef.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <strings.h> 37#include <sys/file.h> 38#include <sys/mntent.h> 39#include <sys/mnttab.h> 40#include <sys/param.h> 41#include <sys/stat.h> 42 43#include <sys/dmu.h> 44#include <sys/dmu_objset.h> 45#include <sys/dnode.h> 46#include <sys/vdev_impl.h> 47 48#include "zinject.h" 49 50extern void kernel_init(int); 51extern void kernel_fini(void); 52 53static int debug; 54 55static void 56ziprintf(const char *fmt, ...) 57{ 58 va_list ap; 59 60 if (!debug) 61 return; 62 63 va_start(ap, fmt); 64 (void) vprintf(fmt, ap); 65 va_end(ap); 66} 67 68static void 69compress_slashes(const char *src, char *dest) 70{ 71 while (*src != '\0') { 72 *dest = *src++; 73 while (*dest == '/' && *src == '/') 74 ++src; 75 ++dest; 76 } 77 *dest = '\0'; 78} 79 80/* 81 * Given a full path to a file, translate into a dataset name and a relative 82 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters, 83 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64 84 * buffer, which we need later to get the object ID. 85 */ 86static int 87parse_pathname(const char *inpath, char *dataset, char *relpath, 88 struct stat64 *statbuf) 89{ 90 struct statfs sfs; 91 const char *rel; 92 char fullpath[MAXPATHLEN]; 93 94 compress_slashes(inpath, fullpath); 95 96 if (fullpath[0] != '/') { 97 (void) fprintf(stderr, "invalid object '%s': must be full " 98 "path\n", fullpath); 99 usage(); 100 return (-1); 101 } 102 103 if (strlen(fullpath) >= MAXPATHLEN) { 104 (void) fprintf(stderr, "invalid object; pathname too long\n"); 105 return (-1); 106 } 107 108 if (stat64(fullpath, statbuf) != 0) { 109 (void) fprintf(stderr, "cannot open '%s': %s\n", 110 fullpath, strerror(errno)); 111 return (-1); 112 } 113 114 if (statfs(fullpath, &sfs) == -1) { 115 (void) fprintf(stderr, "cannot find mountpoint for '%s': %s\n", 116 fullpath, strerror(errno)); 117 return (-1); 118 } 119 120 if (strcmp(sfs.f_fstypename, MNTTYPE_ZFS) != 0) { 121 (void) fprintf(stderr, "invalid path '%s': not a ZFS " 122 "filesystem\n", fullpath); 123 return (-1); 124 } 125 126 if (strncmp(fullpath, sfs.f_mntonname, strlen(sfs.f_mntonname)) != 0) { 127 (void) fprintf(stderr, "invalid path '%s': mountpoint " 128 "doesn't match path\n", fullpath); 129 return (-1); 130 } 131 132 (void) strcpy(dataset, sfs.f_mntfromname); 133 134 rel = fullpath + strlen(sfs.f_mntonname); 135 if (rel[0] == '/') 136 rel++; 137 (void) strcpy(relpath, rel); 138 139 return (0); 140} 141 142/* 143 * Convert from a (dataset, path) pair into a (objset, object) pair. Note that 144 * we grab the object number from the inode number, since looking this up via 145 * libzpool is a real pain. 146 */ 147/* ARGSUSED */ 148static int 149object_from_path(const char *dataset, const char *path, struct stat64 *statbuf, 150 zinject_record_t *record) 151{ 152 objset_t *os; 153 int err; 154 155 /* 156 * Before doing any libzpool operations, call sync() to ensure that the 157 * on-disk state is consistent with the in-core state. 158 */ 159 sync(); 160 161 err = dmu_objset_own(dataset, DMU_OST_ZFS, B_TRUE, FTAG, &os); 162 if (err != 0) { 163 (void) fprintf(stderr, "cannot open dataset '%s': %s\n", 164 dataset, strerror(err)); 165 return (-1); 166 } 167 168 record->zi_objset = dmu_objset_id(os); 169 record->zi_object = statbuf->st_ino; 170 171 dmu_objset_disown(os, FTAG); 172 173 return (0); 174} 175 176/* 177 * Calculate the real range based on the type, level, and range given. 178 */ 179static int 180calculate_range(const char *dataset, err_type_t type, int level, char *range, 181 zinject_record_t *record) 182{ 183 objset_t *os = NULL; 184 dnode_t *dn = NULL; 185 int err; 186 int ret = -1; 187 188 /* 189 * Determine the numeric range from the string. 190 */ 191 if (range == NULL) { 192 /* 193 * If range is unspecified, set the range to [0,-1], which 194 * indicates that the whole object should be treated as an 195 * error. 196 */ 197 record->zi_start = 0; 198 record->zi_end = -1ULL; 199 } else { 200 char *end; 201 202 /* XXX add support for suffixes */ 203 record->zi_start = strtoull(range, &end, 10); 204 205 206 if (*end == '\0') 207 record->zi_end = record->zi_start + 1; 208 else if (*end == ',') 209 record->zi_end = strtoull(end + 1, &end, 10); 210 211 if (*end != '\0') { 212 (void) fprintf(stderr, "invalid range '%s': must be " 213 "a numeric range of the form 'start[,end]'\n", 214 range); 215 goto out; 216 } 217 } 218 219 switch (type) { 220 case TYPE_DATA: 221 break; 222 223 case TYPE_DNODE: 224 /* 225 * If this is a request to inject faults into the dnode, then we 226 * must translate the current (objset,object) pair into an 227 * offset within the metadnode for the objset. Specifying any 228 * kind of range with type 'dnode' is illegal. 229 */ 230 if (range != NULL) { 231 (void) fprintf(stderr, "range cannot be specified when " 232 "type is 'dnode'\n"); 233 goto out; 234 } 235 236 record->zi_start = record->zi_object * sizeof (dnode_phys_t); 237 record->zi_end = record->zi_start + sizeof (dnode_phys_t); 238 record->zi_object = 0; 239 break; 240 } 241 242 /* 243 * Get the dnode associated with object, so we can calculate the block 244 * size. 245 */ 246 if ((err = dmu_objset_own(dataset, DMU_OST_ANY, 247 B_TRUE, FTAG, &os)) != 0) { 248 (void) fprintf(stderr, "cannot open dataset '%s': %s\n", 249 dataset, strerror(err)); 250 goto out; 251 } 252 253 if (record->zi_object == 0) { 254 dn = DMU_META_DNODE(os); 255 } else { 256 err = dnode_hold(os, record->zi_object, FTAG, &dn); 257 if (err != 0) { 258 (void) fprintf(stderr, "failed to hold dnode " 259 "for object %llu\n", 260 (u_longlong_t)record->zi_object); 261 goto out; 262 } 263 } 264 265 266 ziprintf("data shift: %d\n", (int)dn->dn_datablkshift); 267 ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift); 268 269 /* 270 * Translate range into block IDs. 271 */ 272 if (record->zi_start != 0 || record->zi_end != -1ULL) { 273 record->zi_start >>= dn->dn_datablkshift; 274 record->zi_end >>= dn->dn_datablkshift; 275 } 276 277 /* 278 * Check level, and then translate level 0 blkids into ranges 279 * appropriate for level of indirection. 280 */ 281 record->zi_level = level; 282 if (level > 0) { 283 ziprintf("level 0 blkid range: [%llu, %llu]\n", 284 record->zi_start, record->zi_end); 285 286 if (level >= dn->dn_nlevels) { 287 (void) fprintf(stderr, "level %d exceeds max level " 288 "of object (%d)\n", level, dn->dn_nlevels - 1); 289 goto out; 290 } 291 292 if (record->zi_start != 0 || record->zi_end != 0) { 293 int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 294 295 for (; level > 0; level--) { 296 record->zi_start >>= shift; 297 record->zi_end >>= shift; 298 } 299 } 300 } 301 302 ret = 0; 303out: 304 if (dn) { 305 if (dn != DMU_META_DNODE(os)) 306 dnode_rele(dn, FTAG); 307 } 308 if (os) 309 dmu_objset_disown(os, FTAG); 310 311 return (ret); 312} 313 314int 315translate_record(err_type_t type, const char *object, const char *range, 316 int level, zinject_record_t *record, char *poolname, char *dataset) 317{ 318 char path[MAXPATHLEN]; 319 char *slash; 320 struct stat64 statbuf; 321 int ret = -1; 322 323 kernel_init(FREAD); 324 325 debug = (getenv("ZINJECT_DEBUG") != NULL); 326 327 ziprintf("translating: %s\n", object); 328 329 if (MOS_TYPE(type)) { 330 /* 331 * MOS objects are treated specially. 332 */ 333 switch (type) { 334 case TYPE_MOS: 335 record->zi_type = 0; 336 break; 337 case TYPE_MOSDIR: 338 record->zi_type = DMU_OT_OBJECT_DIRECTORY; 339 break; 340 case TYPE_METASLAB: 341 record->zi_type = DMU_OT_OBJECT_ARRAY; 342 break; 343 case TYPE_CONFIG: 344 record->zi_type = DMU_OT_PACKED_NVLIST; 345 break; 346 case TYPE_BPOBJ: 347 record->zi_type = DMU_OT_BPOBJ; 348 break; 349 case TYPE_SPACEMAP: 350 record->zi_type = DMU_OT_SPACE_MAP; 351 break; 352 case TYPE_ERRLOG: 353 record->zi_type = DMU_OT_ERROR_LOG; 354 break; 355 } 356 357 dataset[0] = '\0'; 358 (void) strcpy(poolname, object); 359 return (0); 360 } 361 362 /* 363 * Convert a full path into a (dataset, file) pair. 364 */ 365 if (parse_pathname(object, dataset, path, &statbuf) != 0) 366 goto err; 367 368 ziprintf(" dataset: %s\n", dataset); 369 ziprintf(" path: %s\n", path); 370 371 /* 372 * Convert (dataset, file) into (objset, object) 373 */ 374 if (object_from_path(dataset, path, &statbuf, record) != 0) 375 goto err; 376 377 ziprintf("raw objset: %llu\n", record->zi_objset); 378 ziprintf("raw object: %llu\n", record->zi_object); 379 380 /* 381 * For the given object, calculate the real (type, level, range) 382 */ 383 if (calculate_range(dataset, type, level, (char *)range, record) != 0) 384 goto err; 385 386 ziprintf(" objset: %llu\n", record->zi_objset); 387 ziprintf(" object: %llu\n", record->zi_object); 388 if (record->zi_start == 0 && 389 record->zi_end == -1ULL) 390 ziprintf(" range: all\n"); 391 else 392 ziprintf(" range: [%llu, %llu]\n", record->zi_start, 393 record->zi_end); 394 395 /* 396 * Copy the pool name 397 */ 398 (void) strcpy(poolname, dataset); 399 if ((slash = strchr(poolname, '/')) != NULL) 400 *slash = '\0'; 401 402 ret = 0; 403 404err: 405 kernel_fini(); 406 return (ret); 407} 408 409int 410translate_raw(const char *str, zinject_record_t *record) 411{ 412 /* 413 * A raw bookmark of the form objset:object:level:blkid, where each 414 * number is a hexidecimal value. 415 */ 416 if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset, 417 (u_longlong_t *)&record->zi_object, &record->zi_level, 418 (u_longlong_t *)&record->zi_start) != 4) { 419 (void) fprintf(stderr, "bad raw spec '%s': must be of the form " 420 "'objset:object:level:blkid'\n", str); 421 return (-1); 422 } 423 424 record->zi_end = record->zi_start; 425 426 return (0); 427} 428 429int 430translate_device(const char *pool, const char *device, err_type_t label_type, 431 zinject_record_t *record) 432{ 433 char *end; 434 zpool_handle_t *zhp; 435 nvlist_t *tgt; 436 boolean_t isspare, iscache; 437 438 /* 439 * Given a device name or GUID, create an appropriate injection record 440 * with zi_guid set. 441 */ 442 if ((zhp = zpool_open(g_zfs, pool)) == NULL) 443 return (-1); 444 445 record->zi_guid = strtoull(device, &end, 16); 446 if (record->zi_guid == 0 || *end != '\0') { 447 tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL); 448 449 if (tgt == NULL) { 450 (void) fprintf(stderr, "cannot find device '%s' in " 451 "pool '%s'\n", device, pool); 452 return (-1); 453 } 454 455 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 456 &record->zi_guid) == 0); 457 } 458 459 /* 460 * Device faults can take on three different forms: 461 * 1). delayed or hanging I/O 462 * 2). zfs label faults 463 * 3). generic disk faults 464 */ 465 if (record->zi_timer != 0) { 466 record->zi_cmd = ZINJECT_DELAY_IO; 467 } else if (label_type != TYPE_INVAL) { 468 record->zi_cmd = ZINJECT_LABEL_FAULT; 469 } else { 470 record->zi_cmd = ZINJECT_DEVICE_FAULT; 471 } 472 473 switch (label_type) { 474 case TYPE_LABEL_UBERBLOCK: 475 record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]); 476 record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1; 477 break; 478 case TYPE_LABEL_NVLIST: 479 record->zi_start = offsetof(vdev_label_t, vl_vdev_phys); 480 record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1; 481 break; 482 case TYPE_LABEL_PAD1: 483 record->zi_start = offsetof(vdev_label_t, vl_pad1); 484 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1; 485 break; 486 case TYPE_LABEL_PAD2: 487 record->zi_start = offsetof(vdev_label_t, vl_pad2); 488 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1; 489 break; 490 } 491 return (0); 492} 493