Skip to content

Commit f1155a3

Browse files
committed
zfs_main.c: cleanup initial implementation
- corrected `HELP_MOUNT` to more accurately reflect the options - changed my usage of `dataset` to `filesystem` in order to align with documentation - improved the recursive checks in `get_one_dataset()` - Skip any dataset with the canmount=off property - Skip any dataset whose name doesn't match the target filesystem or - Skip any dataset whose name doesn't contain the target filesystem + '/' - corrected the `usage()` errors in `share_mount()` to accurately reflect the options - limited the recursive feature to `zfs mount` only - implemented a validity check of the specified target filesystem using `zfs_open()` Signed-off-by: QORTEC <[email protected]>
1 parent 30a50e7 commit f1155a3

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

cmd/zfs/zfs_main.c

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ get_usage(zfs_help_t idx)
307307
"[filesystem|volume|snapshot] ...\n"));
308308
case HELP_MOUNT:
309309
return (gettext("\tmount\n"
310-
"\tmount [-flvO] [-o opts] -r <-a | filesystem>\n"));
310+
"\tmount [-flvO] [-o opts] [-r filesystem] "
311+
"<-a [filesystem] | filesystem>\n"));
311312
case HELP_PROMOTE:
312313
return (gettext("\tpromote <clone-filesystem>\n"));
313314
case HELP_RECEIVE:
@@ -6731,7 +6732,7 @@ zfs_do_holds(int argc, char **argv)
67316732
typedef struct get_all_state {
67326733
boolean_t ga_verbose;
67336734
get_all_cb_t *ga_cbp;
6734-
const char *ga_dataset;
6735+
const char *ga_filesystem;
67356736
} get_all_state_t;
67366737

67376738
static int
@@ -6772,12 +6773,31 @@ get_one_dataset(zfs_handle_t *zhp, void *data)
67726773
}
67736774

67746775
/*
6775-
* Skip any dataset whos name doesn't have a prefix matching ga_dataset.
6776+
* Check if ga_filesystem is set; used for recursive mounting
67766777
*/
6777-
if (state->ga_dataset != NULL && strncmp(zfs_get_name(zhp),
6778-
state->ga_dataset, strlen(state->ga_dataset)) != 0) {
6779-
zfs_close(zhp);
6780-
return (0);
6778+
if (state->ga_filesystem != NULL) {
6779+
/*
6780+
* Skip any dataset with the canmount=off property.
6781+
*/
6782+
if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) {
6783+
zfs_close(zhp);
6784+
return (0);
6785+
}
6786+
6787+
/* Add a trailing / to ga_filesystem */
6788+
char filesystem[strlen(state->ga_filesystem) + 2];
6789+
strcpy(filesystem, state->ga_filesystem);
6790+
strcat(filesystem, "/");
6791+
6792+
/*
6793+
* Skip any dataset whose name doesn't match ga_filesystem exactly
6794+
* or have a prefix matching filesystem.
6795+
*/
6796+
if (!strcmp(zfs_get_name(zhp), state->ga_filesystem) == 0 ||
6797+
!strncmp(zfs_get_name(zhp), filesystem, strlen(filesystem) == 0)) {
6798+
zfs_close(zhp);
6799+
return (0);
6800+
}
67816801
}
67826802

67836803
libzfs_add_handle(state->ga_cbp, zhp);
@@ -6787,12 +6807,12 @@ get_one_dataset(zfs_handle_t *zhp, void *data)
67876807
}
67886808

67896809
static void
6790-
get_all_datasets(get_all_cb_t *cbp, boolean_t verbose, const char *dataset)
6810+
get_all_datasets(get_all_cb_t *cbp, boolean_t verbose, const char *filesystem)
67916811
{
67926812
get_all_state_t state = {
67936813
.ga_verbose = verbose,
67946814
.ga_cbp = cbp,
6795-
.ga_dataset = dataset
6815+
.ga_filesystem = filesystem
67966816
};
67976817

67986818
if (verbose)
@@ -6820,7 +6840,8 @@ typedef struct share_mount_state {
68206840
uint_t sm_total; /* number of filesystems to process */
68216841
uint_t sm_done; /* number of filesystems processed */
68226842
int sm_status; /* -1 if any of the share/mount operations failed */
6823-
boolean_t sm_explicit;
6843+
boolean_t sm_explicit; /* true if filesystems were explictly requested,
6844+
false if they are implied by `-a` */
68246845
} share_mount_state_t;
68256846

68266847
/*
@@ -7212,23 +7233,34 @@ share_mount(int op, int argc, char **argv)
72127233
argv++;
72137234
}
72147235

7215-
if (argc > 1) {
7236+
if (!recursive && argc > 1) {
72167237
(void) fprintf(stderr, gettext("usage: "
7217-
"zfs mount -a <dataset>\n"));
7238+
"zfs mount -a [filesystem]\n"));
72187239
usage(B_FALSE);
72197240
}
72207241

72217242
if (recursive && argc != 1) {
72227243
(void) fprintf(stderr, gettext("usage: "
7223-
"zfs mount -r <dataset>\n"));
7244+
"zfs mount -r <filesystem>\n"));
72247245
usage(B_FALSE);
72257246
}
72267247

7227-
const char *dataset = (argv[0] != NULL) ? argv[0] : NULL;
7248+
/*
7249+
* Enable recursive -a for mounting only
7250+
* Validate filesystem is actually a valid zfs filesystem
7251+
*/
7252+
const char *filesystem = NULL;
7253+
if (op == OP_MOUNT)
7254+
filesystem = argv[0];
7255+
if (filesystem != NULL &&
7256+
zfs_open(g_zfs, filesystem, ZFS_TYPE_FILESYSTEM) == NULL) {
7257+
free(options);
7258+
return (1);
7259+
}
72287260

72297261
start_progress_timer();
72307262
get_all_cb_t cb = { 0 };
7231-
get_all_datasets(&cb, verbose, dataset);
7263+
get_all_datasets(&cb, verbose, filesystem);
72327264

72337265
if (cb.cb_used == 0) {
72347266
free(options);

0 commit comments

Comments
 (0)