aboutsummaryrefslogtreecommitdiff
path: root/cddl/contrib/opensolaris/lib/pyzfs/common/ioctl.c
blob: c0de5c474c0e0b948b3ce4e164a6550094f85742 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <Python.h>
#include <sys/zfs_ioctl.h>
#include <sys/fs/zfs.h>
#include <strings.h>
#include <unistd.h>
#include <libnvpair.h>
#include <idmap.h>
#include <zone.h>
#include <libintl.h>
#include <libzfs.h>
#include "zfs_prop.h"

static PyObject *ZFSError;
static int zfsdevfd;

#ifdef __lint
#define	dgettext(x, y) y
#endif

#define	_(s) dgettext(TEXT_DOMAIN, s)

#ifdef sun
extern int sid_to_id(char *sid, boolean_t user, uid_t *id);
#endif	/* sun */

/*PRINTFLIKE1*/
static void
seterr(char *fmt, ...)
{
	char errstr[1024];
	va_list v;

	va_start(v, fmt);
	(void) vsnprintf(errstr, sizeof (errstr), fmt, v);
	va_end(v);

	PyErr_SetObject(ZFSError, Py_BuildValue("is", errno, errstr));
}

static char cmdstr[HIS_MAX_RECORD_LEN];

static int
ioctl_with_cmdstr(unsigned long ioc, zfs_cmd_t *zc)
{
	int err;

	if (cmdstr[0])
		zc->zc_history = (uint64_t)(uintptr_t)cmdstr;
	err = ioctl(zfsdevfd, ioc, zc);
	cmdstr[0] = '\0';
	return (err);
}

static PyObject *
nvl2py(nvlist_t *nvl)
{
	PyObject *pyo;
	nvpair_t *nvp;

	pyo = PyDict_New();

	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp;
	    nvp = nvlist_next_nvpair(nvl, nvp)) {
		PyObject *pyval;
		char *sval;
		uint64_t ival;
		boolean_t bval;
		nvlist_t *nval;

		switch (nvpair_type(nvp)) {
		case DATA_TYPE_STRING:
			(void) nvpair_value_string(nvp, &sval);
			pyval = Py_BuildValue("s", sval);
			break;

		case DATA_TYPE_UINT64:
			(void) nvpair_value_uint64(nvp, &ival);
			pyval = Py_BuildValue("K", ival);
			break;

		case DATA_TYPE_NVLIST:
			(void) nvpair_value_nvlist(nvp, &nval);
			pyval = nvl2py(nval);
			break;

		case DATA_TYPE_BOOLEAN:
			Py_INCREF(Py_None);
			pyval = Py_None;
			break;

		case DATA_TYPE_BOOLEAN_VALUE:
			(void) nvpair_value_boolean_value(nvp, &bval);
			pyval = Py_BuildValue("i", bval);
			break;

		default:
			PyErr_SetNone(PyExc_ValueError);
			Py_DECREF(pyo);
			return (NULL);
		}

		PyDict_SetItemString(pyo, nvpair_name(nvp), pyval);
		Py_DECREF(pyval);
	}

	return (pyo);
}

static nvlist_t *
dict2nvl(PyObject *d)
{
	nvlist_t *nvl;
	int err;
	PyObject *key, *value;
//	int pos = 0;
	Py_ssize_t 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);
}

static PyObject *
fakepropval(uint64_t value)
{
	PyObject *d = PyDict_New();
	PyDict_SetItemString(d, "value", Py_BuildValue("K", value));
	return (d);
}

static void
add_ds_props(zfs_cmd_t *zc, PyObject *nvl)
{
	dmu_objset_stats_t *s = &zc->zc_objset_stats;
	PyDict_SetItemString(nvl, "numclones",
	    fakepropval(s->dds_num_clones));
	PyDict_SetItemString(nvl, "issnap",
	    fakepropval(s->dds_is_snapshot));
	PyDict_SetItemString(nvl, "inconsistent",
	    fakepropval(s->dds_inconsistent));
}

/* On error, returns NULL but does not set python exception. */
static PyObject *
ioctl_with_dstnv(unsigned long ioc, zfs_cmd_t *zc)
{
	int nvsz = 2048;
	void *nvbuf;
	PyObject *pynv = NULL;

again:
	nvbuf = malloc(nvsz);
	zc->zc_nvlist_dst_size = nvsz;
	zc->zc_nvlist_dst = (uintptr_t)nvbuf;

	if (ioctl(zfsdevfd, ioc, zc) == 0) {
		nvlist_t *nvl;

		errno = nvlist_unpack(nvbuf, zc->zc_nvlist_dst_size, &nvl, 0);
		if (errno == 0) {
			pynv = nvl2py(nvl);
			nvlist_free(nvl);
		}
	} else if (errno == ENOMEM) {
		free(nvbuf);
		nvsz = zc->zc_nvlist_dst_size;
		goto again;
	}
	free(nvbuf);
	return (pynv);
}

static PyObject *
py_next_dataset(PyObject *self, PyObject *args)
{
	unsigned long ioc;
	uint64_t cookie;
	zfs_cmd_t zc = { 0 };
	int snaps;
	char *name;
	PyObject *nvl;
	PyObject *ret = NULL;

	if (!PyArg_ParseTuple(args, "siK", &name, &snaps, &cookie))
		return (NULL);

	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
	zc.zc_cookie = cookie;

	if (snaps)
		ioc = ZFS_IOC_SNAPSHOT_LIST_NEXT;
	else
		ioc = ZFS_IOC_DATASET_LIST_NEXT;

	nvl = ioctl_with_dstnv(ioc, &zc);
	if (nvl) {
		add_ds_props(&zc, nvl);
		ret = Py_BuildValue("sKO", zc.zc_name, zc.zc_cookie, nvl);
		Py_DECREF(nvl);
	} else if (errno == ESRCH) {
		PyErr_SetNone(PyExc_StopIteration);
	} else {
		if (snaps)
			seterr(_("cannot get snapshots of %s"), name);
		else
			seterr(_("cannot get child datasets of %s"), name);
	}
	return (ret);
}

static PyObject *
py_dataset_props(PyObject *self, PyObject *args)
{
	zfs_cmd_t zc = { 0 };
	int snaps;
	char *name;
	PyObject *nvl;

	if (!PyArg_ParseTuple(args, "s", &name))
		return (NULL);

	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));

	nvl = ioctl_with_dstnv(ZFS_IOC_OBJSET_STATS, &zc);
	if (nvl) {
		add_ds_props(&zc, nvl);
	} else {
		seterr(_("cannot access dataset %s"), name);
	}
	return (nvl);
}

static PyObject *
py_get_fsacl(PyObject *self, PyObject *args)
{
	zfs_cmd_t zc = { 0 };
	char *name;
	PyObject *nvl;

	if (!PyArg_ParseTuple(args, "s", &name))
		return (NULL);

	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));

	nvl = ioctl_with_dstnv(ZFS_IOC_GET_FSACL, &zc);
	if (nvl == NULL)
		seterr(_("cannot get permissions on %s"), name);

	return (nvl);
}

static PyObject *
py_set_fsacl(PyObject *self, PyObject *args)
{
	int un;
	size_t nvsz;
	zfs_cmd_t zc = { 0 };
	char *name, *nvbuf;
	PyObject *dict, *file;
	nvlist_t *nvl;
	int err;

	if (!PyArg_ParseTuple(args, "siO!", &name, &un,
	    &PyDict_Type, &dict))
		return (NULL);

	nvl = dict2nvl(dict);
	if (nvl == NULL)
		return (NULL);

	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
	assert(err == 0);
	nvbuf = malloc(nvsz);
	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
	assert(err == 0);

	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
	zc.zc_nvlist_src_size = nvsz;
	zc.zc_nvlist_src = (uintptr_t)nvbuf;
	zc.zc_perm_action = un;

	err = ioctl_with_cmdstr(ZFS_IOC_SET_FSACL, &zc);
	free(nvbuf);
	if (err) {
		seterr(_("cannot set permissions on %s"), name);
		return (NULL);
	}

	Py_RETURN_NONE;
}

static PyObject *
py_userspace_many(PyObject *self, PyObject *args)
{
	zfs_cmd_t zc = { 0 };
	zfs_userquota_prop_t type;
	char *name, *propname;
	int bufsz = 1<<20;
	void *buf;
	PyObject *dict, *file;
	int error;

	if (!PyArg_ParseTuple(args, "ss", &name, &propname))
		return (NULL);

	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++)
		if (strcmp(propname, zfs_userquota_prop_prefixes[type]) == 0)
			break;
	if (type == ZFS_NUM_USERQUOTA_PROPS) {
		PyErr_SetString(PyExc_KeyError, propname);
		return (NULL);
	}

	dict = PyDict_New();
	buf = malloc(bufsz);

	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
	zc.zc_objset_type = type;
	zc.zc_cookie = 0;

	while (1) {
		zfs_useracct_t *zua = buf;

		zc.zc_nvlist_dst = (uintptr_t)buf;
		zc.zc_nvlist_dst_size = bufsz;

		error = ioctl(zfsdevfd, ZFS_IOC_USERSPACE_MANY, &zc);
		if (error || zc.zc_nvlist_dst_size == 0)
			break;

		while (zc.zc_nvlist_dst_size > 0) {
			PyObject *pykey, *pyval;

			pykey = Py_BuildValue("sI",
			    zua->zu_domain, zua->zu_rid);
			pyval = Py_BuildValue("K", zua->zu_space);
			PyDict_SetItem(dict, pykey, pyval);
			Py_DECREF(pykey);
			Py_DECREF(pyval);

			zua++;
			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
		}
	}

	free(buf);

	if (error != 0) {
		Py_DECREF(dict);
		seterr(_("cannot get %s property on %s"), propname, name);
		return (NULL);
	}

	return (dict);
}

static PyObject *
py_userspace_upgrade(PyObject *self, PyObject *args)
{
	zfs_cmd_t zc = { 0 };
	char *name;
	int error;

	if (!PyArg_ParseTuple(args, "s", &name))
		return (NULL);

	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
	error = ioctl(zfsdevfd, ZFS_IOC_USERSPACE_UPGRADE, &zc);

	if (error != 0) {
		seterr(_("cannot initialize user accounting information on %s"),
		    name);
		return (NULL);
	}

	Py_RETURN_NONE;
}

static PyObject *
py_sid_to_id(PyObject *self, PyObject *args)
{
#ifdef sun
	char *sid;
	int err, isuser;
	uid_t id;

	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
		return (NULL);

	err = sid_to_id(sid, isuser, &id);
	if (err) {
		PyErr_SetString(PyExc_KeyError, sid);
		return (NULL);
	}

	return (Py_BuildValue("I", id));
#else	/* sun */
	return (NULL);
#endif	/* sun */
}

/*
 * Translate the sid string ("S-1-...") to the user@domain name, if
 * possible.  There should be a better way to do this, but for now we
 * just translate to the (possibly ephemeral) uid and then back again.
 */
static PyObject *
py_sid_to_name(PyObject *self, PyObject *args)
{
#ifdef sun
	char *sid;
	int err, isuser;
	uid_t id;
	char *name, *domain;
	char buf[256];

	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
		return (NULL);

	err = sid_to_id(sid, isuser, &id);
	if (err) {
		PyErr_SetString(PyExc_KeyError, sid);
		return (NULL);
	}

	if (isuser) {
		err = idmap_getwinnamebyuid(id,
		    IDMAP_REQ_FLG_USE_CACHE, &name, &domain);
	} else {
		err = idmap_getwinnamebygid(id,
		    IDMAP_REQ_FLG_USE_CACHE, &name, &domain);
	}
	if (err != IDMAP_SUCCESS) {
		PyErr_SetString(PyExc_KeyError, sid);
		return (NULL);
	}
	(void) snprintf(buf, sizeof (buf), "%s@%s", name, domain);
	free(name);
	free(domain);

	return (Py_BuildValue("s", buf));
#else	/* sun */
	return(NULL);
#endif	/* sun */
}

static PyObject *
py_isglobalzone(PyObject *self, PyObject *args)
{
	return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
}

static PyObject *
py_set_cmdstr(PyObject *self, PyObject *args)
{
	char *str;

	if (!PyArg_ParseTuple(args, "s", &str))
		return (NULL);

	(void) strlcpy(cmdstr, str, sizeof (cmdstr));

	Py_RETURN_NONE;
}

static PyObject *
py_get_proptable(PyObject *self, PyObject *args)
{
	zprop_desc_t *t = zfs_prop_get_table();
	PyObject *d = PyDict_New();
	zfs_prop_t i;

	for (i = 0; i < ZFS_NUM_PROPS; i++) {
		zprop_desc_t *p = &t[i];
		PyObject *tuple;
		static const char *typetable[] =
		    {"number", "string", "index"};
		static const char *attrtable[] =
		    {"default", "readonly", "inherit", "onetime"};
		PyObject *indextable;

		if (p->pd_proptype == PROP_TYPE_INDEX) {
			const zprop_index_t *it = p->pd_table;
			indextable = PyDict_New();
			int j;
			for (j = 0; it[j].pi_name; j++) {
				PyDict_SetItemString(indextable,
				    it[j].pi_name,
				    Py_BuildValue("K", it[j].pi_value));
			}
		} else {
			Py_INCREF(Py_None);
			indextable = Py_None;
		}

		tuple = Py_BuildValue("sissKsissiiO",
		    p->pd_name, p->pd_propnum, typetable[p->pd_proptype],
		    p->pd_strdefault, p->pd_numdefault,
		    attrtable[p->pd_attr], p->pd_types,
		    p->pd_values, p->pd_colname,
		    p->pd_rightalign, p->pd_visible, indextable);
		PyDict_SetItemString(d, p->pd_name, tuple);
		Py_DECREF(tuple);
	}

	return (d);
}

static PyMethodDef zfsmethods[] = {
	{"next_dataset", py_next_dataset, METH_VARARGS,
	    "Get next child dataset or snapshot."},
	{"get_fsacl", py_get_fsacl, METH_VARARGS, "Get allowed permissions."},
	{"set_fsacl", py_set_fsacl, METH_VARARGS, "Set allowed permissions."},
	{"userspace_many", py_userspace_many, METH_VARARGS,
	    "Get user space accounting."},
	{"userspace_upgrade", py_userspace_upgrade, METH_VARARGS,
	    "Upgrade fs to enable user space accounting."},
	{"set_cmdstr", py_set_cmdstr, METH_VARARGS,
	    "Set command string for history logging."},
	{"dataset_props", py_dataset_props, METH_VARARGS,
	    "Get dataset properties."},
	{"get_proptable", py_get_proptable, METH_NOARGS,
	    "Get property table."},
	/* Below are not really zfs-specific: */
	{"sid_to_id", py_sid_to_id, METH_VARARGS, "Map SID to UID/GID."},
	{"sid_to_name", py_sid_to_name, METH_VARARGS,
	    "Map SID to name@domain."},
	{"isglobalzone", py_isglobalzone, METH_NOARGS,
	    "Determine if this is the global zone."},
	{NULL, NULL, 0, NULL}
};

void
initioctl(void)
{
	PyObject *zfs_ioctl = Py_InitModule("zfs.ioctl", zfsmethods);
	PyObject *zfs_util = PyImport_ImportModule("zfs.util");
	PyObject *devfile;

	if (zfs_util == NULL)
		return;

	ZFSError = PyObject_GetAttrString(zfs_util, "ZFSError");
	devfile = PyObject_GetAttrString(zfs_util, "dev");
	zfsdevfd = PyObject_AsFileDescriptor(devfile);

	zfs_prop_init();
}