Skip to content

Commit

Permalink
[vpsAdminOS] zfs share: add option -r to recursively share descendant…
Browse files Browse the repository at this point in the history
… filesystems

Signed-off-by: Jakub Skokan <[email protected]>
Signed-off-by: Pavel Snajdr <[email protected]>
  • Loading branch information
aither64 authored and snajpa committed Oct 8, 2024
1 parent 34a2fd2 commit c8a5162
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ get_usage(zfs_help_t idx)
return (gettext("\tset [-u] <property=value> ... "
"<filesystem|volume|snapshot> ...\n"));
case HELP_SHARE:
return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
return (gettext("\tshare [-l] <-a [nfs|smb] | [-r] filesystem>\n"));
case HELP_SNAPSHOT:
return (gettext("\tsnapshot [-r] [-o property=value] ... "
"<filesystem|volume>@<snap> ...\n"));
Expand Down Expand Up @@ -7426,6 +7426,7 @@ share_mount(int op, int argc, char **argv)
{
int do_all = 0;
int recursive = 0;
int share_recursive = 0;
boolean_t verbose = B_FALSE;
boolean_t json = B_FALSE;
int c, ret = 0;
Expand All @@ -7437,7 +7438,7 @@ share_mount(int op, int argc, char **argv)
jsobj = data = item = NULL;

/* check options */
while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ajRlvo:Of" : "al"))
while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ajRlvo:Of" : "alr"))
!= -1) {
switch (c) {
case 'a':
Expand Down Expand Up @@ -7476,6 +7477,9 @@ share_mount(int op, int argc, char **argv)
case 'f':
flags |= MS_FORCE;
break;
case 'r':
share_recursive = 1;
break;
case ':':
(void) fprintf(stderr, gettext("missing argument for "
"'%c' option\n"), optopt);
Expand Down Expand Up @@ -7636,10 +7640,61 @@ share_mount(int op, int argc, char **argv)
ZFS_TYPE_FILESYSTEM)) == NULL) {
ret = 1;
} else {
ret = share_mount_one(zhp, op, flags, SA_NO_PROTOCOL,
B_TRUE, options);
zfs_commit_shares(NULL);
zfs_close(zhp);
if (op == OP_SHARE && share_recursive) {
start_progress_timer();
get_all_cb_t cb = { 0 };
get_all_state_t state = {
.ga_verbose = verbose,
.ga_cbp = &cb
};

libzfs_add_handle(&cb, zhp);
assert(cb.cb_used <= cb.cb_alloc);

if (zfs_iter_filesystems_v2(zhp, 0, get_one_dataset, &state) != 0) {
zfs_close(zhp);
if (options != NULL)
free(options);
return (1);
}

if (cb.cb_used == 0) {
zfs_close(zhp);
if (options != NULL)
free(options);
return (0);
}

share_mount_state_t share_mount_state = { 0 };
share_mount_state.sm_op = op;
share_mount_state.sm_verbose = verbose;
share_mount_state.sm_flags = flags;
share_mount_state.sm_options = options;
share_mount_state.sm_total = cb.cb_used;
pthread_mutex_init(&share_mount_state.sm_lock, NULL);

/* For a 'zfs share -a' operation start with a clean slate. */
zfs_truncate_shares(NULL);

/*
* libshare isn't mt-safe, so don't do the operation in parallel
*/
zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
share_mount_one_cb, &share_mount_state,
op == OP_MOUNT && !(flags & MS_CRYPT));
zfs_commit_shares(NULL);

ret = share_mount_state.sm_status;

for (int i = 0; i < cb.cb_used; i++)
zfs_close(cb.cb_handles[i]);
free(cb.cb_handles);
} else {
ret = share_mount_one(zhp, op, flags, SA_NO_PROTOCOL,
B_TRUE, options);
zfs_commit_shares(NULL);
zfs_close(zhp);
}
}
}

Expand Down

0 comments on commit c8a5162

Please sign in to comment.