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.
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.
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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/zfs_ioctl.h>
28 #include <sys/fs/zfs.h>
31 #include <libnvpair.h>
34 #include <libzfs_impl.h>
37 static PyObject *ZFSError;
41 #define dgettext(x, y) y
44 #define _(s) dgettext(TEXT_DOMAIN, s)
48 seterr(char *fmt, ...)
54 (void) vsnprintf(errstr, sizeof (errstr), fmt, v);
57 PyErr_SetObject(ZFSError, Py_BuildValue("is", errno, errstr));
60 static char cmdstr[HIS_MAX_RECORD_LEN];
63 ioctl_with_cmdstr(int ioc, zfs_cmd_t *zc)
68 zc->zc_history = (uint64_t)(uintptr_t)cmdstr;
69 err = ioctl(zfsdevfd, ioc, zc);
82 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp;
83 nvp = nvlist_next_nvpair(nvl, nvp)) {
90 switch (nvpair_type(nvp)) {
91 case DATA_TYPE_STRING:
92 (void) nvpair_value_string(nvp, &sval);
93 pyval = Py_BuildValue("s", sval);
96 case DATA_TYPE_UINT64:
97 (void) nvpair_value_uint64(nvp, &ival);
98 pyval = Py_BuildValue("K", ival);
101 case DATA_TYPE_NVLIST:
102 (void) nvpair_value_nvlist(nvp, &nval);
103 pyval = nvl2py(nval);
106 case DATA_TYPE_BOOLEAN:
111 case DATA_TYPE_BOOLEAN_VALUE:
112 (void) nvpair_value_boolean_value(nvp, &bval);
113 pyval = Py_BuildValue("i", bval);
117 PyErr_SetNone(PyExc_ValueError);
122 PyDict_SetItemString(pyo, nvpair_name(nvp), pyval);
130 dict2nvl(PyObject *d)
134 PyObject *key, *value;
137 if (!PyDict_Check(d)) {
138 PyErr_SetObject(PyExc_ValueError, d);
142 err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
145 while (PyDict_Next(d, &pos, &key, &value)) {
146 char *keystr = PyString_AsString(key);
147 if (keystr == NULL) {
148 PyErr_SetObject(PyExc_KeyError, key);
153 if (PyDict_Check(value)) {
154 nvlist_t *valnvl = dict2nvl(value);
155 err = nvlist_add_nvlist(nvl, keystr, valnvl);
157 } else if (value == Py_None) {
158 err = nvlist_add_boolean(nvl, keystr);
159 } else if (PyString_Check(value)) {
160 char *valstr = PyString_AsString(value);
161 err = nvlist_add_string(nvl, keystr, valstr);
162 } else if (PyInt_Check(value)) {
163 uint64_t valint = PyInt_AsUnsignedLongLongMask(value);
164 err = nvlist_add_uint64(nvl, keystr, valint);
165 } else if (PyBool_Check(value)) {
166 boolean_t valbool = value == Py_True ? B_TRUE : B_FALSE;
167 err = nvlist_add_boolean_value(nvl, keystr, valbool);
169 PyErr_SetObject(PyExc_ValueError, value);
180 fakepropval(uint64_t value)
182 PyObject *d = PyDict_New();
183 PyDict_SetItemString(d, "value", Py_BuildValue("K", value));
188 add_ds_props(zfs_cmd_t *zc, PyObject *nvl)
190 dmu_objset_stats_t *s = &zc->zc_objset_stats;
191 PyDict_SetItemString(nvl, "numclones",
192 fakepropval(s->dds_num_clones));
193 PyDict_SetItemString(nvl, "issnap",
194 fakepropval(s->dds_is_snapshot));
195 PyDict_SetItemString(nvl, "inconsistent",
196 fakepropval(s->dds_inconsistent));
199 /* On error, returns NULL but does not set python exception. */
201 ioctl_with_dstnv(int ioc, zfs_cmd_t *zc)
205 PyObject *pynv = NULL;
208 nvbuf = malloc(nvsz);
209 zc->zc_nvlist_dst_size = nvsz;
210 zc->zc_nvlist_dst = (uintptr_t)nvbuf;
212 if (ioctl(zfsdevfd, ioc, zc) == 0) {
215 errno = nvlist_unpack(nvbuf, zc->zc_nvlist_dst_size, &nvl, 0);
220 } else if (errno == ENOMEM) {
222 nvsz = zc->zc_nvlist_dst_size;
230 py_next_dataset(PyObject *self, PyObject *args)
234 zfs_cmd_t zc = { 0 };
238 PyObject *ret = NULL;
240 if (!PyArg_ParseTuple(args, "siK", &name, &snaps, &cookie))
243 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
244 zc.zc_cookie = cookie;
247 ioc = ZFS_IOC_SNAPSHOT_LIST_NEXT;
249 ioc = ZFS_IOC_DATASET_LIST_NEXT;
251 nvl = ioctl_with_dstnv(ioc, &zc);
253 add_ds_props(&zc, nvl);
254 ret = Py_BuildValue("sKO", zc.zc_name, zc.zc_cookie, nvl);
256 } else if (errno == ESRCH) {
257 PyErr_SetNone(PyExc_StopIteration);
260 seterr(_("cannot get snapshots of %s"), name);
262 seterr(_("cannot get child datasets of %s"), name);
268 py_dataset_props(PyObject *self, PyObject *args)
270 zfs_cmd_t zc = { 0 };
275 if (!PyArg_ParseTuple(args, "s", &name))
278 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
280 nvl = ioctl_with_dstnv(ZFS_IOC_OBJSET_STATS, &zc);
282 add_ds_props(&zc, nvl);
284 seterr(_("cannot access dataset %s"), name);
290 py_get_fsacl(PyObject *self, PyObject *args)
292 zfs_cmd_t zc = { 0 };
296 if (!PyArg_ParseTuple(args, "s", &name))
299 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
301 nvl = ioctl_with_dstnv(ZFS_IOC_GET_FSACL, &zc);
303 seterr(_("cannot get permissions on %s"), name);
309 py_set_fsacl(PyObject *self, PyObject *args)
313 zfs_cmd_t zc = { 0 };
315 PyObject *dict, *file;
319 if (!PyArg_ParseTuple(args, "siO!", &name, &un,
320 &PyDict_Type, &dict))
323 nvl = dict2nvl(dict);
327 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
329 nvbuf = malloc(nvsz);
330 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
333 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
334 zc.zc_nvlist_src_size = nvsz;
335 zc.zc_nvlist_src = (uintptr_t)nvbuf;
336 zc.zc_perm_action = un;
338 err = ioctl_with_cmdstr(ZFS_IOC_SET_FSACL, &zc);
341 seterr(_("cannot set permissions on %s"), name);
349 py_get_holds(PyObject *self, PyObject *args)
351 zfs_cmd_t zc = { 0 };
355 if (!PyArg_ParseTuple(args, "s", &name))
358 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
360 nvl = ioctl_with_dstnv(ZFS_IOC_GET_HOLDS, &zc);
362 seterr(_("cannot get holds for %s"), name);
368 py_userspace_many(PyObject *self, PyObject *args)
370 zfs_cmd_t zc = { 0 };
371 zfs_userquota_prop_t type;
372 char *name, *propname;
375 PyObject *dict, *file;
378 if (!PyArg_ParseTuple(args, "ss", &name, &propname))
381 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++)
382 if (strcmp(propname, zfs_userquota_prop_prefixes[type]) == 0)
384 if (type == ZFS_NUM_USERQUOTA_PROPS) {
385 PyErr_SetString(PyExc_KeyError, propname);
392 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
393 zc.zc_objset_type = type;
397 zfs_useracct_t *zua = buf;
399 zc.zc_nvlist_dst = (uintptr_t)buf;
400 zc.zc_nvlist_dst_size = bufsz;
402 error = ioctl(zfsdevfd, ZFS_IOC_USERSPACE_MANY, &zc);
403 if (error || zc.zc_nvlist_dst_size == 0)
406 while (zc.zc_nvlist_dst_size > 0) {
407 PyObject *pykey, *pyval;
409 pykey = Py_BuildValue("sI",
410 zua->zu_domain, zua->zu_rid);
411 pyval = Py_BuildValue("K", zua->zu_space);
412 PyDict_SetItem(dict, pykey, pyval);
417 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
425 seterr(_("cannot get %s property on %s"), propname, name);
433 py_userspace_upgrade(PyObject *self, PyObject *args)
435 zfs_cmd_t zc = { 0 };
439 if (!PyArg_ParseTuple(args, "s", &name))
442 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
443 error = ioctl(zfsdevfd, ZFS_IOC_USERSPACE_UPGRADE, &zc);
446 seterr(_("cannot initialize user accounting information on %s"),
455 py_set_cmdstr(PyObject *self, PyObject *args)
459 if (!PyArg_ParseTuple(args, "s", &str))
462 (void) strlcpy(cmdstr, str, sizeof (cmdstr));
468 py_get_proptable(PyObject *self, PyObject *args)
470 zprop_desc_t *t = zfs_prop_get_table();
471 PyObject *d = PyDict_New();
474 for (i = 0; i < ZFS_NUM_PROPS; i++) {
475 zprop_desc_t *p = &t[i];
477 static const char *typetable[] =
478 {"number", "string", "index"};
479 static const char *attrtable[] =
480 {"default", "readonly", "inherit", "onetime"};
481 PyObject *indextable;
483 if (p->pd_proptype == PROP_TYPE_INDEX) {
484 const zprop_index_t *it = p->pd_table;
485 indextable = PyDict_New();
487 for (j = 0; it[j].pi_name; j++) {
488 PyDict_SetItemString(indextable,
490 Py_BuildValue("K", it[j].pi_value));
494 indextable = Py_None;
497 tuple = Py_BuildValue("sissKsissiiO",
498 p->pd_name, p->pd_propnum, typetable[p->pd_proptype],
499 p->pd_strdefault, p->pd_numdefault,
500 attrtable[p->pd_attr], p->pd_types,
501 p->pd_values, p->pd_colname,
502 p->pd_rightalign, p->pd_visible, indextable);
503 PyDict_SetItemString(d, p->pd_name, tuple);
510 static PyMethodDef zfsmethods[] = {
511 {"next_dataset", py_next_dataset, METH_VARARGS,
512 "Get next child dataset or snapshot."},
513 {"get_fsacl", py_get_fsacl, METH_VARARGS, "Get allowed permissions."},
514 {"set_fsacl", py_set_fsacl, METH_VARARGS, "Set allowed permissions."},
515 {"userspace_many", py_userspace_many, METH_VARARGS,
516 "Get user space accounting."},
517 {"userspace_upgrade", py_userspace_upgrade, METH_VARARGS,
518 "Upgrade fs to enable user space accounting."},
519 {"set_cmdstr", py_set_cmdstr, METH_VARARGS,
520 "Set command string for history logging."},
521 {"dataset_props", py_dataset_props, METH_VARARGS,
522 "Get dataset properties."},
523 {"get_proptable", py_get_proptable, METH_NOARGS,
524 "Get property table."},
525 {"get_holds", py_get_holds, METH_VARARGS, "Get user holds."},
526 {NULL, NULL, 0, NULL}
532 PyObject *zfs_ioctl = Py_InitModule("zfs.ioctl", zfsmethods);
533 PyObject *zfs_util = PyImport_ImportModule("zfs.util");
536 if (zfs_util == NULL)
539 ZFSError = PyObject_GetAttrString(zfs_util, "ZFSError");
540 devfile = PyObject_GetAttrString(zfs_util, "dev");
541 zfsdevfd = PyObject_AsFileDescriptor(devfile);