translate.c revision 98d1cbfec254295273b6a761bc1861c0374bdf02
1689N/A/*
1689N/A * CDDL HEADER START
1689N/A *
1689N/A * The contents of this file are subject to the terms of the
1689N/A * Common Development and Distribution License (the "License").
1689N/A * You may not use this file except in compliance with the License.
1689N/A *
1689N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1689N/A * or http://www.opensolaris.org/os/licensing.
1689N/A * See the License for the specific language governing permissions
1689N/A * and limitations under the License.
1689N/A *
1689N/A * When distributing Covered Code, include this CDDL HEADER in each
1689N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1689N/A * If applicable, add the following below this CDDL HEADER, with the
1689N/A * fields enclosed by brackets "[]" replaced with your own identifying
1689N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1689N/A *
1689N/A * CDDL HEADER END
1689N/A */
1689N/A/*
1689N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
1689N/A */
1689N/A
5080N/A#include <libzfs.h>
1689N/A
1689N/A#include <sys/zfs_context.h>
2086N/A
1689N/A#include <errno.h>
1689N/A#include <fcntl.h>
1689N/A#include <stdarg.h>
1689N/A#include <stddef.h>
1689N/A#include <stdio.h>
1689N/A#include <stdlib.h>
1689N/A#include <strings.h>
1689N/A#include <sys/file.h>
1689N/A#include <sys/mntent.h>
1689N/A#include <sys/mnttab.h>
1689N/A#include <sys/param.h>
1689N/A#include <sys/stat.h>
2086N/A
1689N/A#include <sys/dmu.h>
1689N/A#include <sys/dmu_objset.h>
1689N/A#include <sys/dnode.h>
1689N/A#include <sys/vdev_impl.h>
1689N/A
1689N/A#include <sys/mkdev.h>
1689N/A
1689N/A#include "zinject.h"
1689N/A
3352N/Aextern void kernel_init(int);
3853N/Aextern void kernel_fini(void);
1689N/A
1689N/Astatic int debug;
1689N/A
3352N/Astatic void
1689N/Aziprintf(const char *fmt, ...)
1689N/A{
1689N/A va_list ap;
3853N/A
1689N/A if (!debug)
1689N/A return;
1689N/A
1689N/A va_start(ap, fmt);
1689N/A (void) vprintf(fmt, ap);
1689N/A va_end(ap);
1689N/A}
1689N/A
1689N/Astatic void
1689N/Acompress_slashes(const char *src, char *dest)
1689N/A{
1689N/A while (*src != '\0') {
1689N/A *dest = *src++;
1689N/A while (*dest == '/' && *src == '/')
1689N/A ++src;
1689N/A ++dest;
1689N/A }
1689N/A *dest = '\0';
1689N/A}
1689N/A
1689N/A/*
1689N/A * Given a full path to a file, translate into a dataset name and a relative
1689N/A * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
1689N/A * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
1689N/A * buffer, which we need later to get the object ID.
1689N/A */
1689N/Astatic int
1689N/Aparse_pathname(const char *inpath, char *dataset, char *relpath,
1689N/A struct stat64 *statbuf)
1689N/A{
1689N/A struct extmnttab mp;
1689N/A FILE *fp;
1689N/A int match;
1689N/A const char *rel;
1689N/A char fullpath[MAXPATHLEN];
1689N/A
1689N/A compress_slashes(inpath, fullpath);
1689N/A
1689N/A if (fullpath[0] != '/') {
1689N/A (void) fprintf(stderr, "invalid object '%s': must be full "
1689N/A "path\n", fullpath);
1689N/A usage();
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A if (strlen(fullpath) >= MAXPATHLEN) {
1689N/A (void) fprintf(stderr, "invalid object; pathname too long\n");
1689N/A return (-1);
3840N/A }
3840N/A
1689N/A if (stat64(fullpath, statbuf) != 0) {
1689N/A (void) fprintf(stderr, "cannot open '%s': %s\n",
1689N/A fullpath, strerror(errno));
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A if ((fp = fopen(MNTTAB, "r")) == NULL) {
1689N/A (void) fprintf(stderr, "cannot open /etc/mnttab\n");
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A match = 0;
1689N/A while (getextmntent(fp, &mp, sizeof (mp)) == 0) {
1689N/A if (makedev(mp.mnt_major, mp.mnt_minor) == statbuf->st_dev) {
1689N/A match = 1;
1689N/A break;
1689N/A }
1689N/A }
1689N/A
1689N/A if (!match) {
1689N/A (void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
1689N/A fullpath);
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) {
1689N/A (void) fprintf(stderr, "invalid path '%s': not a ZFS "
1689N/A "filesystem\n", fullpath);
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) {
1689N/A (void) fprintf(stderr, "invalid path '%s': mountpoint "
1689N/A "doesn't match path\n", fullpath);
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A (void) strcpy(dataset, mp.mnt_special);
1689N/A
1689N/A rel = fullpath + strlen(mp.mnt_mountp);
1689N/A if (rel[0] == '/')
1689N/A rel++;
1689N/A (void) strcpy(relpath, rel);
1689N/A
1689N/A return (0);
1689N/A}
1689N/A
1689N/A/*
1689N/A * Convert from a (dataset, path) pair into a (objset, object) pair. Note that
1689N/A * we grab the object number from the inode number, since looking this up via
1689N/A * libzpool is a real pain.
1689N/A */
1689N/A/* ARGSUSED */
1689N/Astatic int
1689N/Aobject_from_path(const char *dataset, const char *path, struct stat64 *statbuf,
1689N/A zinject_record_t *record)
1689N/A{
1689N/A objset_t *os;
1689N/A int err;
1689N/A
1689N/A /*
1689N/A * Before doing any libzpool operations, call sync() to ensure that the
1689N/A * on-disk state is consistent with the in-core state.
1689N/A */
1689N/A sync();
1689N/A
4134N/A err = dmu_objset_own(dataset, DMU_OST_ZFS, B_TRUE, FTAG, &os);
1689N/A if (err != 0) {
1689N/A (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
1689N/A dataset, strerror(err));
4134N/A return (-1);
1689N/A }
1689N/A
4134N/A record->zi_objset = dmu_objset_id(os);
1689N/A record->zi_object = statbuf->st_ino;
1689N/A
1689N/A dmu_objset_disown(os, FTAG);
1689N/A
1689N/A return (0);
1689N/A}
1689N/A
1689N/A/*
1689N/A * Calculate the real range based on the type, level, and range given.
1689N/A */
1689N/Astatic int
1689N/Acalculate_range(const char *dataset, err_type_t type, int level, char *range,
1689N/A zinject_record_t *record)
1689N/A{
1689N/A objset_t *os = NULL;
1689N/A dnode_t *dn = NULL;
1689N/A int err;
1689N/A int ret = -1;
1689N/A
1689N/A /*
1689N/A * Determine the numeric range from the string.
1689N/A */
1689N/A if (range == NULL) {
1689N/A /*
1689N/A * If range is unspecified, set the range to [0,-1], which
1689N/A * indicates that the whole object should be treated as an
1689N/A * error.
1689N/A */
1689N/A record->zi_start = 0;
1689N/A record->zi_end = -1ULL;
1689N/A } else {
1689N/A char *end;
1689N/A
1689N/A /* XXX add support for suffixes */
1689N/A record->zi_start = strtoull(range, &end, 10);
1689N/A
1689N/A
1689N/A if (*end == '\0')
1689N/A record->zi_end = record->zi_start + 1;
1689N/A else if (*end == ',')
1689N/A record->zi_end = strtoull(end + 1, &end, 10);
1689N/A
1689N/A if (*end != '\0') {
1689N/A (void) fprintf(stderr, "invalid range '%s': must be "
1689N/A "a numeric range of the form 'start[,end]'\n",
1689N/A range);
1689N/A goto out;
1689N/A }
1689N/A }
1689N/A
1689N/A switch (type) {
1689N/A case TYPE_DATA:
1689N/A break;
1689N/A
1689N/A case TYPE_DNODE:
1689N/A /*
1689N/A * If this is a request to inject faults into the dnode, then we
1689N/A * must translate the current (objset,object) pair into an
1689N/A * offset within the metadnode for the objset. Specifying any
1689N/A * kind of range with type 'dnode' is illegal.
1689N/A */
1689N/A if (range != NULL) {
1689N/A (void) fprintf(stderr, "range cannot be specified when "
1689N/A "type is 'dnode'\n");
1689N/A goto out;
1689N/A }
1689N/A
2086N/A record->zi_start = record->zi_object * sizeof (dnode_phys_t);
1689N/A record->zi_end = record->zi_start + sizeof (dnode_phys_t);
1689N/A record->zi_object = 0;
1689N/A break;
1689N/A }
1689N/A
1689N/A /*
1689N/A * Get the dnode associated with object, so we can calculate the block
1689N/A * size.
1689N/A */
1689N/A if ((err = dmu_objset_own(dataset, DMU_OST_ANY,
1689N/A B_TRUE, FTAG, &os)) != 0) {
1689N/A (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
1689N/A dataset, strerror(err));
1689N/A goto out;
1689N/A }
1689N/A
1689N/A if (record->zi_object == 0) {
1689N/A dn = os->os_meta_dnode;
1689N/A } else {
1689N/A err = dnode_hold(os, record->zi_object, FTAG, &dn);
1689N/A if (err != 0) {
1689N/A (void) fprintf(stderr, "failed to hold dnode "
1689N/A "for object %llu\n",
1689N/A (u_longlong_t)record->zi_object);
1689N/A goto out;
1689N/A }
1689N/A }
1689N/A
1689N/A
1689N/A ziprintf("data shift: %d\n", (int)dn->dn_datablkshift);
1689N/A ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift);
1689N/A
1689N/A /*
1689N/A * Translate range into block IDs.
1689N/A */
1689N/A if (record->zi_start != 0 || record->zi_end != -1ULL) {
1689N/A record->zi_start >>= dn->dn_datablkshift;
1689N/A record->zi_end >>= dn->dn_datablkshift;
1689N/A }
1689N/A
1689N/A /*
1689N/A * Check level, and then translate level 0 blkids into ranges
1689N/A * appropriate for level of indirection.
1689N/A */
1689N/A record->zi_level = level;
1689N/A if (level > 0) {
1689N/A ziprintf("level 0 blkid range: [%llu, %llu]\n",
1689N/A record->zi_start, record->zi_end);
1689N/A
1689N/A if (level >= dn->dn_nlevels) {
1689N/A (void) fprintf(stderr, "level %d exceeds max level "
1689N/A "of object (%d)\n", level, dn->dn_nlevels - 1);
1689N/A goto out;
1689N/A }
1689N/A
1689N/A if (record->zi_start != 0 || record->zi_end != 0) {
1689N/A int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1689N/A
1689N/A for (; level > 0; level--) {
1689N/A record->zi_start >>= shift;
1689N/A record->zi_end >>= shift;
1689N/A }
1689N/A }
1689N/A }
1689N/A
1689N/A ret = 0;
1689N/Aout:
1689N/A if (dn) {
1689N/A if (dn != os->os_meta_dnode)
1689N/A dnode_rele(dn, FTAG);
1689N/A }
1689N/A if (os)
1689N/A dmu_objset_disown(os, FTAG);
1689N/A
1689N/A return (ret);
1689N/A}
1689N/A
1689N/Aint
1689N/Atranslate_record(err_type_t type, const char *object, const char *range,
1689N/A int level, zinject_record_t *record, char *poolname, char *dataset)
1689N/A{
1689N/A char path[MAXPATHLEN];
1689N/A char *slash;
1689N/A struct stat64 statbuf;
1689N/A int ret = -1;
1689N/A
1689N/A kernel_init(FREAD);
1689N/A
1689N/A debug = (getenv("ZINJECT_DEBUG") != NULL);
1689N/A
1689N/A ziprintf("translating: %s\n", object);
1689N/A
1689N/A if (MOS_TYPE(type)) {
1689N/A /*
1689N/A * MOS objects are treated specially.
1689N/A */
1689N/A switch (type) {
1689N/A case TYPE_MOS:
1689N/A record->zi_type = 0;
1689N/A break;
1689N/A case TYPE_MOSDIR:
1689N/A record->zi_type = DMU_OT_OBJECT_DIRECTORY;
1689N/A break;
1689N/A case TYPE_METASLAB:
1689N/A record->zi_type = DMU_OT_OBJECT_ARRAY;
3840N/A break;
3840N/A case TYPE_CONFIG:
1689N/A record->zi_type = DMU_OT_PACKED_NVLIST;
1689N/A break;
1689N/A case TYPE_BPLIST:
1689N/A record->zi_type = DMU_OT_BPLIST;
1689N/A break;
1689N/A case TYPE_SPACEMAP:
1689N/A record->zi_type = DMU_OT_SPACE_MAP;
1689N/A break;
1689N/A case TYPE_ERRLOG:
1689N/A record->zi_type = DMU_OT_ERROR_LOG;
1689N/A break;
1689N/A }
1689N/A
1689N/A dataset[0] = '\0';
1689N/A (void) strcpy(poolname, object);
1689N/A return (0);
1689N/A }
1689N/A
1689N/A /*
3853N/A * Convert a full path into a (dataset, file) pair.
3853N/A */
2086N/A if (parse_pathname(object, dataset, path, &statbuf) != 0)
2086N/A goto err;
3853N/A
3853N/A ziprintf(" dataset: %s\n", dataset);
1689N/A ziprintf(" path: %s\n", path);
3853N/A
3853N/A /*
3853N/A * Convert (dataset, file) into (objset, object)
3853N/A */
3853N/A if (object_from_path(dataset, path, &statbuf, record) != 0)
3853N/A goto err;
3853N/A
3853N/A ziprintf("raw objset: %llu\n", record->zi_objset);
3853N/A ziprintf("raw object: %llu\n", record->zi_object);
3853N/A
3853N/A /*
3853N/A * For the given object, calculate the real (type, level, range)
3853N/A */
3853N/A if (calculate_range(dataset, type, level, (char *)range, record) != 0)
3853N/A goto err;
3853N/A
3853N/A ziprintf(" objset: %llu\n", record->zi_objset);
3853N/A ziprintf(" object: %llu\n", record->zi_object);
3853N/A if (record->zi_start == 0 &&
1689N/A record->zi_end == -1ULL)
1689N/A ziprintf(" range: all\n");
1689N/A else
1689N/A ziprintf(" range: [%llu, %llu]\n", record->zi_start,
1689N/A record->zi_end);
1689N/A
1689N/A /*
1689N/A * Copy the pool name
1689N/A */
1689N/A (void) strcpy(poolname, dataset);
1689N/A if ((slash = strchr(poolname, '/')) != NULL)
1689N/A *slash = '\0';
1689N/A
1689N/A ret = 0;
1689N/A
1689N/Aerr:
1689N/A kernel_fini();
1689N/A return (ret);
1689N/A}
1689N/A
1689N/Aint
1689N/Atranslate_raw(const char *str, zinject_record_t *record)
1689N/A{
1689N/A /*
1689N/A * A raw bookmark of the form objset:object:level:blkid, where each
1689N/A * number is a hexidecimal value.
1689N/A */
1689N/A if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
1689N/A (u_longlong_t *)&record->zi_object, &record->zi_level,
1689N/A (u_longlong_t *)&record->zi_start) != 4) {
1689N/A (void) fprintf(stderr, "bad raw spec '%s': must be of the form "
1689N/A "'objset:object:level:blkid'\n", str);
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A record->zi_end = record->zi_start;
1689N/A
1689N/A return (0);
1689N/A}
1689N/A
1689N/Aint
1689N/Atranslate_device(const char *pool, const char *device, err_type_t label_type,
1689N/A zinject_record_t *record)
1689N/A{
1689N/A char *end;
1689N/A zpool_handle_t *zhp;
1689N/A nvlist_t *tgt;
3853N/A boolean_t isspare, iscache;
3853N/A
1689N/A /*
1689N/A * Given a device name or GUID, create an appropriate injection record
3853N/A * with zi_guid set.
3853N/A */
3853N/A if ((zhp = zpool_open(g_zfs, pool)) == NULL)
1689N/A return (-1);
1689N/A
1689N/A record->zi_guid = strtoull(device, &end, 16);
3853N/A if (record->zi_guid == 0 || *end != '\0') {
1689N/A tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
1689N/A
1689N/A if (tgt == NULL) {
1689N/A (void) fprintf(stderr, "cannot find device '%s' in "
1689N/A "pool '%s'\n", device, pool);
1689N/A return (-1);
1689N/A }
1689N/A
1689N/A verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
1689N/A &record->zi_guid) == 0);
1689N/A }
2086N/A
1689N/A switch (label_type) {
1689N/A case TYPE_LABEL_UBERBLOCK:
1689N/A record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
1689N/A record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
3840N/A break;
5080N/A case TYPE_LABEL_NVLIST:
1689N/A record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
1689N/A record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;
1689N/A break;
1689N/A case TYPE_LABEL_PAD1:
1689N/A record->zi_start = offsetof(vdev_label_t, vl_pad1);
1689N/A record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
1689N/A break;
1689N/A case TYPE_LABEL_PAD2:
1689N/A record->zi_start = offsetof(vdev_label_t, vl_pad2);
1689N/A record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
1689N/A break;
1689N/A }
1689N/A return (0);
1689N/A}
1689N/A