4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
24 * Use is subject to license terms.
28 * Author: Brian Behlendorf <behlendorf1@llnl.gov>
30 #include "mount_utils.h"
35 /* Persistent mount data is stored in these user attributes */
36 #define LDD_PREFIX "lustre:"
37 #define LDD_VERSION_PROP LDD_PREFIX "version"
38 #define LDD_FLAGS_PROP LDD_PREFIX "flags"
39 #define LDD_INDEX_PROP LDD_PREFIX "index"
40 #define LDD_FSNAME_PROP LDD_PREFIX "fsname"
41 #define LDD_SVNAME_PROP LDD_PREFIX "svname"
42 #define LDD_UUID_PROP LDD_PREFIX "uuid"
43 #define LDD_USERDATA_PROP LDD_PREFIX "userdata"
44 #define LDD_MOUNTOPTS_PROP LDD_PREFIX "mountopts"
46 /* This structure is used to help bridge the gap between the ZFS
47 * properties Lustre uses and their corresponding internal LDD fields.
48 * It is meant to be used internally by the mount utility only. */
49 struct zfs_ldd_prop_bridge {
50 /* Contains the publicly visible name for the property
51 * (i.e. what is shown when running "zfs get") */
53 /* Contains the offset into the lustre_disk_data structure where
54 * the value of this property is or will be stored. (i.e. the
55 * property is read from and written to this offset within ldd) */
57 /* Function pointer responsible for reading in the @prop
58 * property from @zhp and storing it in @ldd_field */
59 int (*zlpb_get_prop_fn)(zfs_handle_t *zhp, char *prop, void *ldd_field);
60 /* Function pointer responsible for writing the value of @ldd_field
61 * into the @prop dataset property in @zhp */
62 int (*zlpb_set_prop_fn)(zfs_handle_t *zhp, char *prop, void *ldd_field);
65 /* Forward declarations needed to initialize the ldd prop bridge list */
66 static int zfs_get_prop_int(zfs_handle_t *, char *, void *);
67 static int zfs_set_prop_int(zfs_handle_t *, char *, void *);
68 static int zfs_get_prop_str(zfs_handle_t *, char *, void *);
69 static int zfs_set_prop_str(zfs_handle_t *, char *, void *);
71 /* Helper for initializing the entries in the special_ldd_prop_params list.
72 * - @name: stored directly in the zlpb_prop_name field
73 * (e.g. lustre:fsname, lustre:version, etc.)
74 * - @field: the field in the lustre_disk_data which directly maps to
75 * the @name property. (e.g. ldd_fsname, ldd_config_ver, etc.)
76 * - @type: The type of @field. Only "int" and "str" are supported.
78 #define ZLB_INIT(name, field, type) \
80 name, offsetof(struct lustre_disk_data, field), \
81 zfs_get_prop_ ## type, zfs_set_prop_ ## type \
84 /* These ldd properties are special because they all have their own
85 * individual fields in the lustre_disk_data structure, as opposed to
86 * being globbed into the ldd_params field. As such, these need special
87 * handling when reading/writing the ldd structure to/from persistent
89 struct zfs_ldd_prop_bridge special_ldd_prop_params[] = {
90 ZLB_INIT(LDD_VERSION_PROP, ldd_config_ver, int),
91 ZLB_INIT(LDD_FLAGS_PROP, ldd_flags, int),
92 ZLB_INIT(LDD_INDEX_PROP, ldd_svindex, int),
93 ZLB_INIT(LDD_FSNAME_PROP, ldd_fsname, str),
94 ZLB_INIT(LDD_SVNAME_PROP, ldd_svname, str),
95 ZLB_INIT(LDD_UUID_PROP, ldd_uuid, str),
96 ZLB_INIT(LDD_USERDATA_PROP, ldd_userdata, str),
97 ZLB_INIT(LDD_MOUNTOPTS_PROP, ldd_mount_opts, str),
101 /* indicate if the ZFS OSD has been successfully setup */
102 static int osd_zfs_setup = 0;
104 static libzfs_handle_t *g_zfs;
108 static int zfs_set_prop_int(zfs_handle_t *zhp, char *prop, void *val)
113 (void) snprintf(str, sizeof (str), "%i", *(int *)val);
114 vprint(" %s=%s\n", prop, str);
115 ret = zfs_prop_set(zhp, prop, str);
121 * Write the zfs property string, note that properties with a NULL or
122 * zero-length value will not be written and 0 returned.
124 static int zfs_set_prop_str(zfs_handle_t *zhp, char *prop, void *val)
128 if (val && strlen(val) > 0) {
129 vprint(" %s=%s\n", prop, (char *)val);
130 ret = zfs_prop_set(zhp, prop, (char *)val);
137 * Map '<key>=<value> ...' pairs in the passed string to dataset properties
138 * of the form 'lustre:<key>=<value>'. Malformed <key>=<value> pairs will
141 static int zfs_set_prop_params(zfs_handle_t *zhp, char *params)
143 char *params_dup, *token, *key, *value;
144 char *save_token = NULL;
145 char prop_name[ZFS_MAXNAMELEN];
148 params_dup = strdup(params);
149 if (params_dup == NULL)
152 token = strtok_r(params_dup, " ", &save_token);
154 key = strtok(token, "=");
158 value = strtok(NULL, "=");
162 sprintf(prop_name, "%s%s", LDD_PREFIX, key);
163 vprint(" %s=%s\n", prop_name, value);
165 ret = zfs_prop_set(zhp, prop_name, value);
169 token = strtok_r(NULL, " ", &save_token);
177 static int osd_check_zfs_setup(void)
179 if (osd_zfs_setup == 0) {
182 fprintf(stderr, "Failed to initialize ZFS library. Are the ZFS "
183 "packages and modules correctly installed?\n");
185 return osd_zfs_setup == 1;
188 /* Write the server config as properties associated with the dataset */
189 int zfs_write_ldd(struct mkfs_opts *mop)
191 struct lustre_disk_data *ldd = &mop->mo_ldd;
192 char *ds = mop->mo_device;
194 struct zfs_ldd_prop_bridge *bridge;
197 if (osd_check_zfs_setup() == 0)
200 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
202 fprintf(stderr, "Failed to open zfs dataset %s\n", ds);
206 vprint("Writing %s properties\n", ds);
208 for (i = 0; special_ldd_prop_params[i].zlpb_prop_name != NULL; i++) {
209 bridge = &special_ldd_prop_params[i];
210 ret = bridge->zlpb_set_prop_fn(zhp, bridge->zlpb_prop_name,
211 (void *)ldd + bridge->zlpb_ldd_offset);
216 ret = zfs_set_prop_params(zhp, ldd->ldd_params);
224 static int zfs_get_prop_int(zfs_handle_t *zhp, char *prop, void *val)
230 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
234 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
239 *(__u32 *)val = strtoul(propstr, NULL, 10);
246 static int zfs_get_prop_str(zfs_handle_t *zhp, char *prop, void *val)
252 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
256 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
260 (void) strcpy(val, propstr);
265 static int zfs_is_special_ldd_prop_param(char *name)
269 for (i = 0; special_ldd_prop_params[i].zlpb_prop_name != NULL; i++)
270 if (!strcmp(name, special_ldd_prop_params[i].zlpb_prop_name))
276 static int zfs_get_prop_params(zfs_handle_t *zhp, char *param, int len)
280 char key[ZFS_MAXNAMELEN];
284 props = zfs_get_user_props(zhp);
293 while (nvp = nvlist_next_nvpair(props, nvp), nvp) {
294 ret = zfs_get_prop_str(zhp, nvpair_name(nvp), value);
298 if (strncmp(nvpair_name(nvp), LDD_PREFIX, strlen(LDD_PREFIX)))
301 if (zfs_is_special_ldd_prop_param(nvpair_name(nvp)))
304 sprintf(key, "%s=", nvpair_name(nvp) + strlen(LDD_PREFIX));
306 ret = add_param(param, key, value);
317 * Read the server config as properties associated with the dataset.
318 * Missing entries as not treated error and are simply skipped.
320 int zfs_read_ldd(char *ds, struct lustre_disk_data *ldd)
323 struct zfs_ldd_prop_bridge *bridge;
326 if (osd_check_zfs_setup() == 0)
329 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
333 for (i = 0; special_ldd_prop_params[i].zlpb_prop_name != NULL; i++) {
334 bridge = &special_ldd_prop_params[i];
335 ret = bridge->zlpb_get_prop_fn(zhp, bridge->zlpb_prop_name,
336 (void *)ldd + bridge->zlpb_ldd_offset);
337 if (ret && (ret != ENOENT))
341 ret = zfs_get_prop_params(zhp, ldd->ldd_params, 4096);
342 if (ret && (ret != ENOENT))
345 ldd->ldd_mount_type = LDD_MT_ZFS;
353 int zfs_is_lustre(char *ds, unsigned *mount_type)
355 struct lustre_disk_data tmp_ldd;
358 if (osd_zfs_setup == 0)
361 ret = zfs_read_ldd(ds, &tmp_ldd);
362 if ((ret == 0) && (tmp_ldd.ldd_config_ver > 0) &&
363 (strlen(tmp_ldd.ldd_svname) > 0)) {
364 *mount_type = tmp_ldd.ldd_mount_type;
371 static char *zfs_mkfs_opts(struct mkfs_opts *mop, char *str, int len)
375 if (strlen(mop->mo_mkfsopts) != 0)
376 snprintf(str, len, " -o %s", mop->mo_mkfsopts);
381 static int zfs_create_vdev(struct mkfs_opts *mop, char *vdev)
385 /* Silently ignore reserved vdev names */
386 if ((strncmp(vdev, "disk", 4) == 0) ||
387 (strncmp(vdev, "file", 4) == 0) ||
388 (strncmp(vdev, "mirror", 6) == 0) ||
389 (strncmp(vdev, "raidz", 5) == 0) ||
390 (strncmp(vdev, "spare", 5) == 0) ||
391 (strncmp(vdev, "log", 3) == 0) ||
392 (strncmp(vdev, "cache", 5) == 0))
396 * Verify a file exists at the provided absolute path. If it doesn't
397 * and mo_device_kb is set attempt to create a file vdev to be used.
398 * Relative paths will be passed directly to 'zpool create' which
399 * will check multiple multiple locations under /dev/.
401 if (vdev[0] == '/') {
402 ret = access(vdev, F_OK);
409 fprintf(stderr, "Unable to access required vdev "
410 "for pool %s (%d)\n", vdev, ret);
414 if (mop->mo_device_kb == 0) {
416 fprintf(stderr, "Unable to create vdev due to "
417 "missing --device-size=#N(KB) parameter\n");
421 ret = file_create(vdev, mop->mo_device_kb);
424 fprintf(stderr, "Unable to create vdev %s (%d)\n",
433 int zfs_make_lustre(struct mkfs_opts *mop)
438 char *mkfs_cmd = NULL;
439 char *mkfs_tmp = NULL;
440 char *ds = mop->mo_device;
441 int pool_exists = 0, ret;
443 if (osd_check_zfs_setup() == 0)
446 /* no automatic index with zfs backend */
447 if (mop->mo_ldd.ldd_flags & LDD_F_NEED_INDEX) {
449 fprintf(stderr, "The target index must be specified with "
458 mkfs_cmd = malloc(PATH_MAX);
459 if (mkfs_cmd == NULL) {
464 mkfs_tmp = malloc(PATH_MAX);
465 if (mkfs_tmp == NULL) {
470 /* Due to zfs_prepare_lustre() check the '/' must exist */
471 strchr(pool, '/')[0] = '\0';
473 /* If --reformat was given attempt to destroy the previous dataset */
474 if ((mop->mo_flags & MO_FORCEFORMAT) &&
475 ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM)) != NULL)) {
477 ret = zfs_destroy(zhp, 0);
480 fprintf(stderr, "Failed destroy zfs dataset %s (%d)\n",
489 * Create the zpool if the vdevs have been specified and the pool
490 * does not already exists. The pool creation itself will be done
491 * with the zpool command rather than the zpool_create() library call
492 * so the existing zpool error handling can be leveraged.
494 php = zpool_open(g_zfs, pool);
500 if ((mop->mo_pool_vdevs != NULL) && (pool_exists == 0)) {
502 memset(mkfs_cmd, 0, PATH_MAX);
503 snprintf(mkfs_cmd, PATH_MAX,
504 "zpool create -f -O canmount=off %s", pool);
506 /* Append the vdev config and create file vdevs as required */
507 while (*mop->mo_pool_vdevs != NULL) {
508 strscat(mkfs_cmd, " ", PATH_MAX);
509 strscat(mkfs_cmd, *mop->mo_pool_vdevs, PATH_MAX);
511 ret = zfs_create_vdev(mop, *mop->mo_pool_vdevs);
515 mop->mo_pool_vdevs++;
518 vprint("mkfs_cmd = %s\n", mkfs_cmd);
519 ret = run_command(mkfs_cmd, PATH_MAX);
522 fprintf(stderr, "Unable to create pool %s (%d)\n",
529 * Create the ZFS filesystem with any required mkfs options:
530 * - canmount=off is set to prevent zfs automounting
531 * - xattr=sa is set to use system attribute based xattrs
533 memset(mkfs_cmd, 0, PATH_MAX);
534 snprintf(mkfs_cmd, PATH_MAX,
535 "zfs create -o canmount=off -o xattr=sa%s %s",
536 zfs_mkfs_opts(mop, mkfs_tmp, PATH_MAX), ds);
538 vprint("mkfs_cmd = %s\n", mkfs_cmd);
539 ret = run_command(mkfs_cmd, PATH_MAX);
542 fprintf(stderr, "Unable to create filesystem %s (%d)\n",
551 if (mkfs_cmd != NULL)
554 if (mkfs_tmp != NULL)
560 int zfs_enable_quota(struct mkfs_opts *mop)
562 fprintf(stderr, "this option is not only valid for zfs\n");
566 int zfs_prepare_lustre(struct mkfs_opts *mop,
567 char *default_mountopts, int default_len,
568 char *always_mountopts, int always_len)
570 if (osd_check_zfs_setup() == 0)
573 if (zfs_name_valid(mop->mo_device, ZFS_TYPE_FILESYSTEM) == 0) {
575 fprintf(stderr, "Invalid filesystem name %s\n", mop->mo_device);
579 if (strchr(mop->mo_device, '/') == NULL) {
581 fprintf(stderr, "Missing pool in filesystem name %s\n",
589 int zfs_tune_lustre(char *dev, struct mount_opts *mop)
591 if (osd_check_zfs_setup() == 0)
597 int zfs_label_lustre(struct mount_opts *mop)
602 if (osd_check_zfs_setup() == 0)
605 zhp = zfs_open(g_zfs, mop->mo_source, ZFS_TYPE_FILESYSTEM);
609 ret = zfs_set_prop_str(zhp, LDD_SVNAME_PROP, mop->mo_ldd.ldd_svname);
619 /* If the ZFS libs are not installed, don't print an error to avoid
620 * spamming ldiskfs users. An error message will still be printed if
621 * someone tries to do some real work involving a ZFS backend */
623 if (libzfs_load_module("zfs") != 0) {
624 /* The ZFS modules are not installed */
629 g_zfs = libzfs_init();
631 fprintf(stderr, "Failed to initialize ZFS library\n");