Ejemplo n.º 1
0
static int
ch_add_boolean(list_wrap_t **lw, boolean_t array, int argc, char **argv)
{
	nvlist_t *nvl = (*lw)->lw_nvl[(*lw)->lw_pos];

	if (array)
		abort();

	if (nvlist_add_boolean(nvl, argv[0]) != 0) {
		(void) fprintf(stderr, "fail at nvlist_add_boolean\n");
		return (-1);
	}
	return (0);
}
Ejemplo n.º 2
0
static nvlist_t *
dict2nvl(PyObject *d)
{
	nvlist_t *nvl;
	int err;
	PyObject *key, *value;
	int pos = 0;

	if (!PyDict_Check(d)) {
		PyErr_SetObject(PyExc_ValueError, d);
		return (NULL);
	}

	err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
	assert(err == 0);

	while (PyDict_Next(d, &pos, &key, &value)) {
		char *keystr = PyString_AsString(key);
		if (keystr == NULL) {
			PyErr_SetObject(PyExc_KeyError, key);
			nvlist_free(nvl);
			return (NULL);
		}

		if (PyDict_Check(value)) {
			nvlist_t *valnvl = dict2nvl(value);
			err = nvlist_add_nvlist(nvl, keystr, valnvl);
			nvlist_free(valnvl);
		} else if (value == Py_None) {
			err = nvlist_add_boolean(nvl, keystr);
		} else if (PyString_Check(value)) {
			char *valstr = PyString_AsString(value);
			err = nvlist_add_string(nvl, keystr, valstr);
		} else if (PyInt_Check(value)) {
			uint64_t valint = PyInt_AsUnsignedLongLongMask(value);
			err = nvlist_add_uint64(nvl, keystr, valint);
		} else if (PyBool_Check(value)) {
			boolean_t valbool = value == Py_True ? B_TRUE : B_FALSE;
			err = nvlist_add_boolean_value(nvl, keystr, valbool);
		} else {
			PyErr_SetObject(PyExc_ValueError, value);
			nvlist_free(nvl);
			return (NULL);
		}
		assert(err == 0);
	}

	return (nvl);
}
Ejemplo n.º 3
0
void
fnvlist_add_boolean(nvlist_t *nvl, const char *name)
{
	VERIFY0(nvlist_add_boolean(nvl, name));
}
Ejemplo n.º 4
0
/*
 * Find all 'allow' permissions from a given point and then continue
 * traversing up to the root.
 *
 * This function constructs an nvlist of nvlists.
 * each setpoint is an nvlist composed of an nvlist of an nvlist
 * of the individual * users/groups/everyone/create
 * permissions.
 *
 * The nvlist will look like this.
 *
 * { source fsname -> { whokeys { permissions,...}, ...}}
 *
 * The fsname nvpairs will be arranged in a bottom up order.  For example,
 * if we have the following structure a/b/c then the nvpairs for the fsnames
 * will be ordered a/b/c, a/b, a.
 */
int
dsl_deleg_get(const char *ddname, nvlist_t **nvp)
{
	dsl_dir_t *dd, *startdd;
	dsl_pool_t *dp;
	int error;
	objset_t *mos;

	error = dsl_dir_open(ddname, FTAG, &startdd, NULL);
	if (error)
		return (error);

	dp = startdd->dd_pool;
	mos = dp->dp_meta_objset;

	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);

	rw_enter(&dp->dp_config_rwlock, RW_READER);
	for (dd = startdd; dd != NULL; dd = dd->dd_parent) {
		zap_cursor_t basezc;
		zap_attribute_t baseza;
		nvlist_t *sp_nvp;
		uint64_t n;
		char source[MAXNAMELEN];

		if (dd->dd_phys->dd_deleg_zapobj &&
		    (zap_count(mos, dd->dd_phys->dd_deleg_zapobj,
		    &n) == 0) && n) {
			VERIFY(nvlist_alloc(&sp_nvp,
			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
		} else {
			continue;
		}

		for (zap_cursor_init(&basezc, mos,
		    dd->dd_phys->dd_deleg_zapobj);
		    zap_cursor_retrieve(&basezc, &baseza) == 0;
		    zap_cursor_advance(&basezc)) {
			zap_cursor_t zc;
			zap_attribute_t za;
			nvlist_t *perms_nvp;

			ASSERT(baseza.za_integer_length == 8);
			ASSERT(baseza.za_num_integers == 1);

			VERIFY(nvlist_alloc(&perms_nvp,
			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
			for (zap_cursor_init(&zc, mos, baseza.za_first_integer);
			    zap_cursor_retrieve(&zc, &za) == 0;
			    zap_cursor_advance(&zc)) {
				VERIFY(nvlist_add_boolean(perms_nvp,
				    za.za_name) == 0);
			}
			zap_cursor_fini(&zc);
			VERIFY(nvlist_add_nvlist(sp_nvp, baseza.za_name,
			    perms_nvp) == 0);
			nvlist_free(perms_nvp);
		}

		zap_cursor_fini(&basezc);

		dsl_dir_name(dd, source);
		VERIFY(nvlist_add_nvlist(*nvp, source, sp_nvp) == 0);
		nvlist_free(sp_nvp);
	}
	rw_exit(&dp->dp_config_rwlock);

	dsl_dir_close(startdd, FTAG);
	return (0);
}
Ejemplo n.º 5
0
static int
get_attrs(descriptor_t *dp, int fd,  nvlist_t *attrs)
{
	struct dk_minfo	minfo;
	int		status;
	int		data_format = FMT_UNKNOWN;
	int		snum = -1;
	int		error;
	struct extvtoc	vtoc;
	struct dk_gpt	*efip;
	struct dk_cinfo	dkinfo;
	int		cooked_fd;
	struct stat	buf;

	if (fd < 0) {
	    return (ENODEV);
	}

	/* First make sure media is inserted and spun up. */
	if (!media_read_info(fd, &minfo)) {
	    return (ENODEV);
	}

	if ((status = read_extvtoc(fd, &vtoc)) >= 0) {
	    data_format = FMT_VTOC;
	} else if (status == VT_ENOTSUP && efi_alloc_and_read(fd, &efip) >= 0) {
	    data_format = FMT_EFI;
	    if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
		efi_free(efip);
		return (ENOMEM);
	    }
	}

	if (data_format == FMT_UNKNOWN) {
	    return (ENODEV);
	}

	if (ioctl(fd, DKIOCINFO, &dkinfo) >= 0) {
	    snum = dkinfo.dki_partition;
	}

	/* check the slice */
	if (data_format == FMT_VTOC) {
	    if (snum < 0 || snum >= vtoc.v_nparts ||
		vtoc.v_part[snum].p_size == 0) {
		return (ENODEV);
	    }
	} else { /* data_format == FMT_EFI */
	    if (snum < 0 || snum >= efip->efi_nparts ||
		efip->efi_parts[snum].p_size == 0) {
		efi_free(efip);
		return (ENODEV);
	    }
	}

	/* the slice exists */

	if (nvlist_add_uint32(attrs, DM_INDEX, snum) != 0) {
	    if (data_format == FMT_EFI) {
		efi_free(efip);
	    }
	    return (ENOMEM);
	}

	if (data_format == FMT_VTOC) {
	    if (nvlist_add_uint64(attrs, DM_START, vtoc.v_part[snum].p_start)
		!= 0) {
		return (ENOMEM);
	    }

	    if (nvlist_add_uint64(attrs, DM_SIZE, vtoc.v_part[snum].p_size)
		!= 0) {
		return (ENOMEM);
	    }

	    if (nvlist_add_uint32(attrs, DM_TAG, vtoc.v_part[snum].p_tag)
		!= 0) {
		return (ENOMEM);
	    }

	    if (nvlist_add_uint32(attrs, DM_FLAG, vtoc.v_part[snum].p_flag)
		!= 0) {
		return (ENOMEM);
	    }

	} else { /* data_format == FMT_EFI */
	    if (nvlist_add_uint64(attrs, DM_START,
		efip->efi_parts[snum].p_start) != 0) {
		efi_free(efip);
		return (ENOMEM);
	    }

	    if (nvlist_add_uint64(attrs, DM_SIZE, efip->efi_parts[snum].p_size)
		!= 0) {
		efi_free(efip);
		return (ENOMEM);
	    }

	    if (efip->efi_parts[snum].p_name[0] != 0) {
		char	label[EFI_PART_NAME_LEN + 1];

		(void) snprintf(label, sizeof (label), "%.*s",
		    EFI_PART_NAME_LEN, efip->efi_parts[snum].p_name);
		if (nvlist_add_string(attrs, DM_EFI_NAME, label) != 0) {
		    efi_free(efip);
		    return (ENOMEM);
		}
	    }
	}

	if (data_format == FMT_EFI) {
	    efi_free(efip);
	}

	if (inuse_mnt(dp->name, attrs, &error)) {
	    if (error != 0)
		return (error);
	}

	if (fstat(fd, &buf) != -1) {
	    if (nvlist_add_uint64(attrs, DM_DEVT, buf.st_rdev) != 0) {
		return (ENOMEM);
	    }
	}

	/*
	 * We need to open the cooked slice (not the raw one) to get the
	 * correct devid.
	 */
	cooked_fd = open(dp->name, O_RDONLY|O_NDELAY);

	if (cooked_fd >= 0) {
	    int		no_mem = 0;
	    ddi_devid_t	devid;

	    if (devid_get(cooked_fd, &devid) == 0) {
		char	*minor;

		if (devid_get_minor_name(cooked_fd, &minor) == 0) {
		    char	*devidstr;

		    if ((devidstr = devid_str_encode(devid, minor)) != 0) {

			if (nvlist_add_string(attrs, DM_DEVICEID, devidstr)
			    != 0) {
			    no_mem = 1;
			}

			devid_str_free(devidstr);
		    }
		    devid_str_free(minor);
		}
		devid_free(devid);
	    }
	    (void) close(cooked_fd);

	    if (no_mem) {
		return (ENOMEM);
	    }
	}

	return (0);
}
Ejemplo n.º 6
0
static int
get_attrs(disk_t *dp, int fd, nvlist_t *attrs)
{
	struct	dk_minfo minfo;
	struct	dk_geom	geometry;

	if (fd < 0) {
		return (ENODEV);
	}

	bzero(&minfo, sizeof (struct dk_minfo));

	/* The first thing to do is read the media */
	if (!media_read_info(fd, &minfo)) {
		return (ENODEV);
	}

	if (partition_has_fdisk(dp, fd)) {
		if (nvlist_add_boolean(attrs, DM_FDISK) != 0) {
			return (ENOMEM);
		}
	}

	if (dp->removable) {
		if (nvlist_add_boolean(attrs, DM_REMOVABLE) != 0) {
			return (ENOMEM);
		}

		if (nvlist_add_boolean(attrs, DM_LOADED) != 0) {
			return (ENOMEM);
		}
	}

	if (nvlist_add_uint64(attrs, DM_SIZE, minfo.dki_capacity) != 0) {
		return (ENOMEM);
	}

	if (nvlist_add_uint32(attrs, DM_BLOCKSIZE, minfo.dki_lbsize) != 0) {
		return (ENOMEM);
	}

	if (nvlist_add_uint32(attrs, DM_MTYPE,
	    get_media_type(minfo.dki_media_type)) != 0) {
		return (ENOMEM);
	}

	/* only for disks < 1TB  and x86 */
#if defined(i386) || defined(__amd64)
	if (ioctl(fd, DKIOCG_PHYGEOM, &geometry) >= 0) {
#else
	/* sparc call */
	if (ioctl(fd, DKIOCGGEOM, &geometry) >= 0) {
#endif
		struct extvtoc	vtoc;

		if (nvlist_add_uint64(attrs, DM_START, 0) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
		    geometry.dkg_ncyl * geometry.dkg_nhead * geometry.dkg_nsect)
		    != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NCYLINDERS, geometry.dkg_ncyl)
		    != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NPHYSCYLINDERS,
		    geometry.dkg_pcyl) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NALTCYLINDERS,
		    geometry.dkg_acyl) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NHEADS,
		    geometry.dkg_nhead) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NSECTORS, geometry.dkg_nsect)
		    != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NACTUALCYLINDERS,
		    geometry.dkg_ncyl) != 0) {
			return (ENOMEM);
		}

		if (read_extvtoc(fd, &vtoc) >= 0 && vtoc.v_volume[0] != 0) {
			char	label[LEN_DKL_VVOL + 1];

			(void) snprintf(label, sizeof (label), "%.*s",
			    LEN_DKL_VVOL, vtoc.v_volume);
			if (nvlist_add_string(attrs, DM_LABEL, label) != 0) {
				return (ENOMEM);
			}
		}

	} else {
		/* check for disks > 1TB for accessible size */
		struct dk_gpt	*efip;

		if (efi_alloc_and_read(fd, &efip) >= 0) {
			diskaddr_t	p8size = 0;

			if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
				return (ENOMEM);
			}
			if (nvlist_add_uint64(attrs, DM_START,
			    efip->efi_first_u_lba) != 0) {
				return (ENOMEM);
			}
			/* partition 8 is reserved on EFI labels */
			if (efip->efi_nparts >= 9) {
				p8size = efip->efi_parts[8].p_size;
			}
			if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
			    (efip->efi_last_u_lba - p8size) -
			    efip->efi_first_u_lba) != 0) {
				efi_free(efip);
				return (ENOMEM);
			}
			efi_free(efip);
		}
	}
	return (0);
}

static int
get_media_type(uint_t media_type)
{
	switch (media_type) {
	case DK_UNKNOWN:
		return (DM_MT_UNKNOWN);
	case DK_MO_ERASABLE:
		return (DM_MT_MO_ERASABLE);
	case DK_MO_WRITEONCE:
		return (DM_MT_MO_WRITEONCE);
	case DK_AS_MO:
		return (DM_MT_AS_MO);
	case DK_CDROM:
		return (DM_MT_CDROM);
	case DK_CDR:
		return (DM_MT_CDR);
	case DK_CDRW:
		return (DM_MT_CDRW);
	case DK_DVDROM:
		return (DM_MT_DVDROM);
	case DK_DVDR:
		return (DM_MT_DVDR);
	case DK_DVDRAM:
		return (DM_MT_DVDRAM);
	case DK_FIXED_DISK:
		return (DM_MT_FIXED);
	case DK_FLOPPY:
		return (DM_MT_FLOPPY);
	case DK_ZIP:
		return (DM_MT_ZIP);
	case DK_JAZ:
		return (DM_MT_JAZ);
	default:
		return (DM_MT_UNKNOWN);
	}
}

/*
 * This function handles removable media.
 */
static int
get_rmm_name(disk_t *dp, char *mname, int size)
{
	int		loaded;
	int		fd;

	loaded = 0;

	if ((fd = drive_open_disk(dp, NULL, 0)) >= 0) {
		struct dk_minfo minfo;

		if ((loaded = media_read_info(fd, &minfo))) {
			struct extvtoc vtoc;

			if (read_extvtoc(fd, &vtoc) >= 0) {
				if (vtoc.v_volume[0] != NULL) {
					if (LEN_DKL_VVOL < size) {
						(void) strlcpy(mname,
						    vtoc.v_volume,
						    LEN_DKL_VVOL);
					} else {
						(void) strlcpy(mname,
						    vtoc.v_volume, size);
					}
				}
			}
		}

		(void) close(fd);
	}

	return (loaded);
}
Ejemplo n.º 7
0
static int
get_attrs(disk_t *diskp, int fd, char *opath, nvlist_t *attrs)
{
	if (diskp->removable) {
	    struct dk_minfo	minfo;

	    if (nvlist_add_boolean(attrs, DM_REMOVABLE) != 0) {
		return (ENOMEM);
	    }

	    /* Make sure media is inserted and spun up. */
	    if (fd >= 0 && media_read_info(fd, &minfo)) {
		if (nvlist_add_boolean(attrs, DM_LOADED) != 0) {
		    return (ENOMEM);
		}
	    }

	    /* can't tell diff between dead & no media on removable drives */
	    if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_UP) != 0) {
		return (ENOMEM);
	    }

	    get_drive_type(diskp, fd);

	} else {
	    struct dk_minfo	minfo;

	    /* check if the fixed drive is up or not */
	    if (fd >= 0 && media_read_info(fd, &minfo)) {
		if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_UP) != 0) {
		    return (ENOMEM);
		}
	    } else {
		if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_DOWN) != 0) {
		    return (ENOMEM);
		}
	    }

	    get_drive_type(diskp, fd);
	}

	if (nvlist_add_uint32(attrs, DM_DRVTYPE, diskp->drv_type) != 0) {
	    return (ENOMEM);
	}

	if (diskp->product_id != NULL) {
	    if (nvlist_add_string(attrs, DM_PRODUCT_ID, diskp->product_id)
		!= 0) {
		return (ENOMEM);
	    }
	}
	if (diskp->vendor_id != NULL) {
	    if (nvlist_add_string(attrs, DM_VENDOR_ID, diskp->vendor_id) != 0) {
		return (ENOMEM);
	    }
	}

	if (diskp->sync_speed != -1) {
	    if (nvlist_add_uint32(attrs, DM_SYNC_SPEED, diskp->sync_speed)
		!= 0) {
		return (ENOMEM);
	    }
	}

	if (diskp->wide == 1) {
	    if (nvlist_add_boolean(attrs, DM_WIDE) != 0) {
		return (ENOMEM);
	    }
	}

	if (diskp->rpm == 0) {
	    diskp->rpm = get_rpm(diskp, fd);
	}

	if (diskp->rpm > 0) {
	    if (nvlist_add_uint32(attrs, DM_RPM, diskp->rpm) != 0) {
		return (ENOMEM);
	    }
	}

	if (diskp->aliases != NULL && diskp->aliases->cluster) {
	    if (nvlist_add_boolean(attrs, DM_CLUSTERED) != 0) {
		return (ENOMEM);
	    }
	}

	if (strlen(opath) > 0) {
	    if (nvlist_add_string(attrs, DM_OPATH, opath) != 0) {
		return (ENOMEM);
	    }
	}

	if (diskp->solid_state < 0) {
		diskp->solid_state = get_solidstate(diskp, fd);
	}

	if (diskp->solid_state > 0) {
		if (nvlist_add_boolean(attrs, DM_SOLIDSTATE) != 0) {
			return (ENOMEM);
		}
	}

	return (0);
}