155997Simp/* 255997Simp * CDDL HEADER START 355997Simp * 455997Simp * The contents of this file are subject to the terms of the 555997Simp * Common Development and Distribution License (the "License"). 655997Simp * You may not use this file except in compliance with the License. 755997Simp * 855997Simp * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 955997Simp * or http://www.opensolaris.org/os/licensing. 1055997Simp * See the License for the specific language governing permissions 1155997Simp * and limitations under the License. 1255997Simp * 1355997Simp * When distributing Covered Code, include this CDDL HEADER in each 1455997Simp * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1555997Simp * If applicable, add the following below this CDDL HEADER, with the 1655997Simp * fields enclosed by brackets "[]" replaced with your own identifying 1755997Simp * information: Portions Copyright [yyyy] [name of copyright owner] 1855997Simp * 1955997Simp * CDDL HEADER END 2055997Simp */ 2155997Simp/* 2255997Simp * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 2355997Simp * Copyright (c) 2012 by Delphix. All rights reserved. 2455997Simp */ 2555997Simp 2655997Simp#include <libzfs.h> 27119418Sobrien 28119418Sobrien#include <sys/zfs_context.h> 29119418Sobrien 3055997Simp#include <errno.h> 3155997Simp#include <fcntl.h> 3255997Simp#include <stdarg.h> 3355997Simp#include <stddef.h> 3455997Simp#include <stdio.h> 3555997Simp#include <stdlib.h> 3655997Simp#include <strings.h> 3755997Simp#include <sys/file.h> 3855997Simp#include <sys/mntent.h> 3955997Simp#include <sys/mnttab.h> 4055997Simp#include <sys/param.h> 4170782Simp#include <sys/stat.h> 4255997Simp 4370782Simp#include <sys/dmu.h> 44129764Simp#include <sys/dmu_objset.h> 4570782Simp#include <sys/dnode.h> 4655997Simp#include <sys/vdev_impl.h> 4755997Simp 4855997Simp#include "zinject.h" 4955997Simp 5055997Simpextern void kernel_init(int); 5155997Simpextern void kernel_fini(void); 5255997Simp 5370782Simpstatic int debug; 5470782Simp 5570782Simpstatic void 5670782Simpziprintf(const char *fmt, ...) 5770782Simp{ 5855997Simp va_list ap; 5970782Simp 6086394Simp if (!debug) 6186394Simp return; 6286394Simp 63106893Simp va_start(ap, fmt); 64106893Simp (void) vprintf(fmt, ap); 6570782Simp va_end(ap); 6670782Simp} 6770782Simp 6855997Simpstatic void 6955997Simpcompress_slashes(const char *src, char *dest) 7055997Simp{ 7155997Simp while (*src != '\0') { 7255997Simp *dest = *src++; 7355997Simp while (*dest == '/' && *src == '/') 7455997Simp ++src; 7555997Simp ++dest; 7655997Simp } 7755997Simp *dest = '\0'; 7855997Simp} 7955997Simp 8055997Simp/* 8155997Simp * Given a full path to a file, translate into a dataset name and a relative 8255997Simp * path within the dataset. 'dataset' must be at least MAXNAMELEN characters, 8355997Simp * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64 8455997Simp * buffer, which we need later to get the object ID. 85127135Snjl */ 8655997Simpstatic int 8755997Simpparse_pathname(const char *inpath, char *dataset, char *relpath, 8855997Simp struct stat64 *statbuf) 8955997Simp{ 9055997Simp struct statfs sfs; 9155997Simp const char *rel; 9255997Simp char fullpath[MAXPATHLEN]; 9355997Simp 9455997Simp compress_slashes(inpath, fullpath); 9555997Simp 9655997Simp if (fullpath[0] != '/') { 9755997Simp (void) fprintf(stderr, "invalid object '%s': must be full " 9855997Simp "path\n", fullpath); 9955997Simp usage(); 10055997Simp return (-1); 10155997Simp } 10255997Simp 10355997Simp if (strlen(fullpath) >= MAXPATHLEN) { 10455997Simp (void) fprintf(stderr, "invalid object; pathname too long\n"); 10555997Simp return (-1); 10655997Simp } 10755997Simp 10855997Simp if (stat64(fullpath, statbuf) != 0) { 10955997Simp (void) fprintf(stderr, "cannot open '%s': %s\n", 11070782Simp fullpath, strerror(errno)); 11170782Simp return (-1); 11270782Simp } 11370782Simp 11470782Simp if (statfs(fullpath, &sfs) == -1) { 11570782Simp (void) fprintf(stderr, "cannot find mountpoint for '%s': %s\n", 116113315Simp fullpath, strerror(errno)); 117113315Simp return (-1); 11870782Simp } 11970782Simp 12070782Simp if (strcmp(sfs.f_fstypename, MNTTYPE_ZFS) != 0) { 12170782Simp (void) fprintf(stderr, "invalid path '%s': not a ZFS " 12270782Simp "filesystem\n", fullpath); 12370782Simp return (-1); 12455997Simp } 12555997Simp 12655997Simp if (strncmp(fullpath, sfs.f_mntonname, strlen(sfs.f_mntonname)) != 0) { 12755997Simp (void) fprintf(stderr, "invalid path '%s': mountpoint " 12855997Simp "doesn't match path\n", fullpath); 12955997Simp return (-1); 13055997Simp } 13155997Simp 13255997Simp (void) strcpy(dataset, sfs.f_mntfromname); 13355997Simp 13455997Simp rel = fullpath + strlen(sfs.f_mntonname); 13555997Simp if (rel[0] == '/') 13655997Simp rel++; 13755997Simp (void) strcpy(relpath, rel); 13855997Simp 13955997Simp return (0); 14055997Simp} 14155997Simp 14255997Simp/* 14355997Simp * Convert from a (dataset, path) pair into a (objset, object) pair. Note that 14455997Simp * we grab the object number from the inode number, since looking this up via 14555997Simp * libzpool is a real pain. 14655997Simp */ 14755997Simp/* ARGSUSED */ 14855997Simpstatic int 14955997Simpobject_from_path(const char *dataset, const char *path, struct stat64 *statbuf, 15055997Simp zinject_record_t *record) 15155997Simp{ 15255997Simp objset_t *os; 15355997Simp int err; 15455997Simp 15555997Simp /* 15655997Simp * Before doing any libzpool operations, call sync() to ensure that the 15755997Simp * on-disk state is consistent with the in-core state. 15855997Simp */ 15955997Simp sync(); 16055997Simp 16173280Smarkm err = dmu_objset_own(dataset, DMU_OST_ZFS, B_TRUE, FTAG, &os); 16273280Smarkm if (err != 0) { 16355997Simp (void) fprintf(stderr, "cannot open dataset '%s': %s\n", 16455997Simp dataset, strerror(err)); 16555997Simp return (-1); 16655997Simp } 16755997Simp 16855997Simp record->zi_objset = dmu_objset_id(os); 16955997Simp record->zi_object = statbuf->st_ino; 17055997Simp 17155997Simp dmu_objset_disown(os, FTAG); 17255997Simp 17355997Simp return (0); 17455997Simp} 17555997Simp 17655997Simp/* 17755997Simp * Calculate the real range based on the type, level, and range given. 17855997Simp */ 17955997Simpstatic int 18055997Simpcalculate_range(const char *dataset, err_type_t type, int level, char *range, 18155997Simp zinject_record_t *record) 18255997Simp{ 18355997Simp objset_t *os = NULL; 18455997Simp dnode_t *dn = NULL; 18555997Simp int err; 18655997Simp int ret = -1; 18755997Simp 18855997Simp /* 18955997Simp * Determine the numeric range from the string. 19055997Simp */ 19155997Simp if (range == NULL) { 19255997Simp /* 19355997Simp * If range is unspecified, set the range to [0,-1], which 19455997Simp * indicates that the whole object should be treated as an 19570782Simp * error. 19670782Simp */ 19755997Simp record->zi_start = 0; 19870782Simp record->zi_end = -1ULL; 19970782Simp } else { 20070782Simp char *end; 20170782Simp 20270782Simp /* XXX add support for suffixes */ 20370782Simp record->zi_start = strtoull(range, &end, 10); 20455997Simp 20555997Simp 20655997Simp if (*end == '\0') 20755997Simp record->zi_end = record->zi_start + 1; 20855997Simp else if (*end == ',') 20955997Simp record->zi_end = strtoull(end + 1, &end, 10); 21055997Simp 21155997Simp if (*end != '\0') { 21255997Simp (void) fprintf(stderr, "invalid range '%s': must be " 21355997Simp "a numeric range of the form 'start[,end]'\n", 21469960Simp range); 21555997Simp 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