Skip to content

Commit 607e4ed

Browse files
committed
FreeBSD: Add zfs jail property
A read-only property to report name of the jail that mounted the dataset. Sponsored-by: SkunkWerks, GmbH Signed-off-by: Igor Ostapenko <[email protected]>
1 parent 6ba51da commit 607e4ed

File tree

11 files changed

+141
-3
lines changed

11 files changed

+141
-3
lines changed

include/sys/dsl_dataset.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ typedef struct dsl_dataset {
268268

269269
/* Protected by ds_lock; keep at end of struct for better locality */
270270
char ds_snapname[ZFS_MAX_DATASET_NAME_LEN];
271+
272+
#ifdef __FreeBSD__
273+
char *ds_jailname;
274+
#endif
271275
} dsl_dataset_t;
272276

273277
static inline dsl_dataset_phys_t *

include/sys/fs/zfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ typedef enum {
203203
ZFS_PROP_DEFAULTUSEROBJQUOTA,
204204
ZFS_PROP_DEFAULTGROUPOBJQUOTA,
205205
ZFS_PROP_DEFAULTPROJECTOBJQUOTA,
206+
ZFS_PROP_ZONE,
206207
ZFS_NUM_PROPS
207208
} zfs_prop_t;
208209

lib/libzfs/libzfs.abi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,8 @@
22592259
<enumerator name='ZFS_PROP_DEFAULTUSEROBJQUOTA' value='103'/>
22602260
<enumerator name='ZFS_PROP_DEFAULTGROUPOBJQUOTA' value='104'/>
22612261
<enumerator name='ZFS_PROP_DEFAULTPROJECTOBJQUOTA' value='105'/>
2262-
<enumerator name='ZFS_NUM_PROPS' value='106'/>
2262+
<enumerator name='ZFS_PROP_ZONE' value='106'/>
2263+
<enumerator name='ZFS_NUM_PROPS' value='107'/>
22632264
</enum-decl>
22642265
<typedef-decl name='zfs_prop_t' type-id='4b000d60' id='58603c44'/>
22652266
<enum-decl name='zprop_source_t' naming-typedef-id='a2256d42' id='5903f80e'>

man/man7/zfsprops.7

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,16 @@ for more information.
20952095
Jails are a
20962096
.Fx
20972097
feature and this property is not available on other platforms.
2098+
.It Sy jail
2099+
This read-only property reports name of the jail that mounted the jailed
2100+
dataset.
2101+
The "0" name is used for datasets that are not mounted or not jailed.
2102+
This differs from the normal ZFS convention to print dash ('-') for unset
2103+
values, since it can be a valid jail name.
2104+
If the jail is renamed, the property will still report its old name from
2105+
the time the dataset was mounted.
2106+
The reported jail may no longer exist, while the dataset remains mounted.
2107+
The property is not revealed to jails themselves, the "0" is reported instead.
20982108
.It Sy zoned Ns = Ns Sy off Ns | Ns Sy on
20992109
Controls whether the dataset is managed from a non-global zone or namespace.
21002110
See

module/os/freebsd/zfs/zfs_vfsops.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,18 @@ zfs_domount(vfs_t *vfsp, char *osname)
13101310

13111311
if (!zfsvfs->z_issnap)
13121312
zfsctl_create(zfsvfs);
1313+
1314+
#ifdef __FreeBSD__
1315+
if (error == 0) {
1316+
/* zone dataset visiblity was checked before in zfs_mount() */
1317+
struct prison *pr = curthread->td_ucred->cr_prison;
1318+
if (pr != &prison0) {
1319+
zfsvfs->z_os->os_dsl_dataset->ds_jailname =
1320+
kmem_strdup(pr->pr_name);
1321+
}
1322+
}
1323+
#endif
1324+
13131325
out:
13141326
if (error) {
13151327
dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
@@ -1783,6 +1795,12 @@ zfs_umount(vfs_t *vfsp, int fflag)
17831795
dmu_objset_set_user(os, NULL);
17841796
mutex_exit(&os->os_user_ptr_lock);
17851797

1798+
#ifdef __FreeBSD__
1799+
if (os->os_dsl_dataset->ds_jailname)
1800+
kmem_strfree(os->os_dsl_dataset->ds_jailname);
1801+
os->os_dsl_dataset->ds_jailname = NULL;
1802+
#endif
1803+
17861804
/*
17871805
* Finally release the objset
17881806
*/

module/zcommon/zfs_prop.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,13 @@ zfs_prop_init(void)
516516
zprop_register_index(ZFS_PROP_ZONED, "jailed", 0, PROP_INHERIT,
517517
ZFS_TYPE_FILESYSTEM, "on | off", "JAILED", boolean_table,
518518
sfeatures);
519+
zprop_register_string(ZFS_PROP_ZONE, "jail", NULL, PROP_READONLY,
520+
ZFS_TYPE_FILESYSTEM, "<jailname> | 0", "JAIL", sfeatures);
519521
#else
520522
zprop_register_index(ZFS_PROP_ZONED, "zoned", 0, PROP_INHERIT,
521523
ZFS_TYPE_FILESYSTEM, "on | off", "ZONED", boolean_table, sfeatures);
524+
zprop_register_string(ZFS_PROP_ZONE, "zone", NULL, PROP_READONLY,
525+
ZFS_TYPE_FILESYSTEM, "<zonename>", "ZONE", sfeatures);
522526
#endif
523527
zprop_register_index(ZFS_PROP_VSCAN, "vscan", 0, PROP_INHERIT,
524528
ZFS_TYPE_FILESYSTEM, "on | off", "VSCAN", boolean_table, sfeatures);

module/zfs/dsl_prop.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,15 @@ dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
12301230
goto out;
12311231
}
12321232

1233+
#ifdef __FreeBSD__
1234+
nvlist_t *propval = fnvlist_alloc();
1235+
fnvlist_add_string(propval, ZPROP_VALUE,
1236+
(ds->ds_jailname && INGLOBALZONE(curproc)) ?
1237+
ds->ds_jailname : "0");
1238+
fnvlist_add_nvlist(*nvp, "jail", propval);
1239+
nvlist_free(propval);
1240+
#endif
1241+
12331242
for (; dd != NULL; dd = dd->dd_parent) {
12341243
if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
12351244
if (flags & (DSL_PROP_GET_LOCAL |

tests/runfiles/freebsd.run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ failsafe = callbacks/zfs_failsafe
2323
tags = ['functional']
2424

2525
[tests/functional/cli_root/zfs_jail:FreeBSD]
26-
tests = ['zfs_jail_001_pos']
26+
tests = ['zfs_jail_001_pos', 'zfs_jail_property']
2727
tags = ['functional', 'cli_root', 'zfs_jail']
2828

2929
[tests/functional/pam:FreeBSD]

tests/zfs-tests/tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \
761761
functional/cli_root/zfs_jail/cleanup.ksh \
762762
functional/cli_root/zfs_jail/setup.ksh \
763763
functional/cli_root/zfs_jail/zfs_jail_001_pos.ksh \
764+
functional/cli_root/zfs_jail/zfs_jail_property.ksh \
764765
functional/cli_root/zfs_load-key/cleanup.ksh \
765766
functional/cli_root/zfs_load-key/setup.ksh \
766767
functional/cli_root/zfs_load-key/zfs_load-key_all.ksh \

tests/zfs-tests/tests/functional/cli_root/zfs_jail/zfs_jail_001_pos.ksh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# 1. Create a jail.
3636
# 2. Perform some basic ZFS operations on a dataset both in the host and
3737
# in the jail to confirm the dataset is functional in the host
38-
# and hidden in in the jail.
38+
# and hidden in the jail.
3939
# 3. Run `zfs jail` to expose the dataset in the jail.
4040
# 4. Perform some basic ZFS operations on the dataset both in the host and
4141
# in the jail to confirm the dataset is functional in the jail and host.

0 commit comments

Comments
 (0)