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) 2011, 2012 Whamcloud, Inc.
24 * Use is subject to license terms.
28 * Author: Brian Behlendorf <behlendorf1@llnl.gov>
33 #include <libzfs/libzfs.h>
36 #include "mount_utils.h"
38 /* Persistent mount data is stored in these user attributes */
39 #define LDD_VERSION_PROP "lustre:version"
40 #define LDD_FLAGS_PROP "lustre:flags"
41 #define LDD_INDEX_PROP "lustre:index"
42 #define LDD_FSNAME_PROP "lustre:fsname"
43 #define LDD_SVNAME_PROP "lustre:svname"
44 #define LDD_UUID_PROP "lustre:uuid"
45 #define LDD_USERDATA_PROP "lustre:userdata"
46 #define LDD_MOUNTOPTS_PROP "lustre:mountopts"
47 #define LDD_MGSNODE_PROP "lustre:mgsnode"
48 #define LDD_FAILNODE_PROP "lustre:failnode"
49 #define LDD_FAILMODE_PROP "lustre:failmode"
50 #define LDD_IDENTITY_UPCALL_PROP "lustre:identity_upcall"
52 /* indicate if the ZFS OSD has been successfully setup */
53 static int osd_zfs_setup = 0;
55 static libzfs_handle_t *g_zfs;
57 /* dynamic linking handles for libzfs & libnvpair */
58 static void *handle_libzfs;
59 static void *handle_nvpair;
61 /* symbol table looked up with dlsym */
63 libzfs_handle_t *(*libzfs_init)(void);
64 void (*libzfs_fini)(libzfs_handle_t *);
65 int (*libzfs_load_module)(char *);
66 zfs_handle_t* (*zfs_open)(libzfs_handle_t *, const char *, int);
67 int (*zfs_destroy)(zfs_handle_t *, boolean_t);
68 void (*zfs_close)(zfs_handle_t *);
69 int (*zfs_prop_set)(zfs_handle_t*, const char*, const char*);
70 nvlist_t* (*zfs_get_user_props) (zfs_handle_t *);
71 int (*zfs_name_valid)(const char *, zfs_type_t);
72 zpool_handle_t* (*zpool_open)(libzfs_handle_t *, const char *);
73 void (*zpool_close)(zpool_handle_t *zhp);
74 int (*nvlist_lookup_string)(nvlist_t*, const char*, char**);
75 int (*nvlist_lookup_nvlist)(nvlist_t *, const char *, nvlist_t **);
78 static struct zfs_symbols sym;
81 #define DLSYM(handle, func) \
83 sym.func = (typeof(sym.func))dlsym(handle, #func); \
86 /* populate the symbol table after a successful call to dlopen() */
87 static int zfs_populate_symbols(void)
91 dlerror(); /* Clear any existing error */
93 DLSYM(handle_libzfs, libzfs_init);
94 #define libzfs_init (*sym.libzfs_init)
95 DLSYM(handle_libzfs, libzfs_fini);
96 #define libzfs_fini (*sym.libzfs_fini)
97 DLSYM(handle_libzfs, libzfs_load_module);
98 #define libzfs_load_module (*sym.libzfs_load_module)
99 DLSYM(handle_libzfs, zfs_open);
100 #define zfs_open (*sym.zfs_open)
101 DLSYM(handle_libzfs, zfs_destroy);
102 #define zfs_destroy (*sym.zfs_destroy)
103 DLSYM(handle_libzfs, zfs_close);
104 #define zfs_close (*sym.zfs_close)
105 DLSYM(handle_libzfs, zfs_prop_set);
106 #define zfs_prop_set (*sym.zfs_prop_set)
107 DLSYM(handle_libzfs, zfs_get_user_props);
108 #define zfs_get_user_props (*sym.zfs_get_user_props)
109 DLSYM(handle_libzfs, zfs_name_valid);
110 #define zfs_name_valid (*sym.zfs_name_valid)
111 DLSYM(handle_libzfs, zpool_open);
112 #define zpool_open (*sym.zpool_open)
113 DLSYM(handle_libzfs, zpool_close);
114 #define zpool_close (*sym.zpool_close)
115 DLSYM(handle_nvpair, nvlist_lookup_string);
116 #define nvlist_lookup_string (*sym.nvlist_lookup_string)
117 DLSYM(handle_nvpair, nvlist_lookup_nvlist);
118 #define nvlist_lookup_nvlist (*sym.nvlist_lookup_nvlist)
123 fprintf(stderr, "%s\n", error);
129 static int zfs_set_prop_int(zfs_handle_t *zhp, char *prop, __u32 val)
134 (void) snprintf(str, sizeof (str), "%lu", (unsigned long)val);
135 vprint(" %s=%s\n", prop, str);
136 ret = zfs_prop_set(zhp, prop, str);
142 * Write the zfs property string, note that properties with a NULL or
143 * zero-length value will not be written and 0 returned.
145 static int zfs_set_prop_str(zfs_handle_t *zhp, char *prop, char *val)
149 if (val && strlen(val) > 0) {
150 vprint(" %s=%s\n", prop, val);
151 ret = zfs_prop_set(zhp, prop, val);
157 static int zfs_set_prop_param(zfs_handle_t *zhp, struct lustre_disk_data *ldd,
158 char *param, char *prop)
163 if (get_param(ldd->ldd_params, param, &str) == 0) {
164 vprint(" %s=%s\n", prop, str);
165 ret = zfs_prop_set(zhp, prop, str);
172 static int osd_check_zfs_setup(void)
174 if (osd_zfs_setup == 0) {
177 fprintf(stderr, "Failed to initialize ZFS library. Are the ZFS "
178 "packages and modules correctly installed?\n");
180 return osd_zfs_setup == 1;
183 /* Write the server config as properties associated with the dataset */
184 int zfs_write_ldd(struct mkfs_opts *mop)
186 struct lustre_disk_data *ldd = &mop->mo_ldd;
187 char *ds = mop->mo_device;
191 if (osd_check_zfs_setup() == 0)
194 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
196 fprintf(stderr, "Failed to open zfs dataset %s\n", ds);
200 vprint("Writing %s properties\n", ds);
202 ret = zfs_set_prop_int(zhp, LDD_VERSION_PROP, ldd->ldd_config_ver);
206 ret = zfs_set_prop_int(zhp, LDD_FLAGS_PROP, ldd->ldd_flags);
210 ret = zfs_set_prop_int(zhp, LDD_INDEX_PROP, ldd->ldd_svindex);
214 ret = zfs_set_prop_str(zhp, LDD_FSNAME_PROP, ldd->ldd_fsname);
218 ret = zfs_set_prop_str(zhp, LDD_SVNAME_PROP, ldd->ldd_svname);
222 ret = zfs_set_prop_str(zhp, LDD_UUID_PROP, (char *)ldd->ldd_uuid);
226 ret = zfs_set_prop_str(zhp, LDD_USERDATA_PROP, ldd->ldd_userdata);
230 ret = zfs_set_prop_str(zhp, LDD_MOUNTOPTS_PROP, ldd->ldd_mount_opts);
234 ret = zfs_set_prop_param(zhp, ldd, PARAM_MGSNODE, LDD_MGSNODE_PROP);
238 ret = zfs_set_prop_param(zhp, ldd, PARAM_FAILNODE, LDD_FAILNODE_PROP);
242 ret = zfs_set_prop_param(zhp, ldd, PARAM_FAILMODE, LDD_FAILMODE_PROP);
246 ret = zfs_set_prop_param(zhp, ldd, PARAM_MDT PARAM_ID_UPCALL,
247 LDD_IDENTITY_UPCALL_PROP);
257 static int zfs_get_prop_int(zfs_handle_t *zhp, char *prop, __u32 *val)
263 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
267 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
272 *val = strtoul(propstr, NULL, 10);
279 static int zfs_get_prop_str(zfs_handle_t *zhp, char *prop, char *val)
285 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
289 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
293 (void) strcpy(val, propstr);
298 static int zfs_get_prop_param(zfs_handle_t *zhp, struct lustre_disk_data *ldd,
299 char *param, char *prop)
305 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
309 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
313 ret = add_param(ldd->ldd_params, param, propstr);
319 * Read the server config as properties associated with the dataset.
320 * Missing entries as not treated error and are simply skipped.
322 int zfs_read_ldd(char *ds, struct lustre_disk_data *ldd)
327 if (osd_check_zfs_setup() == 0)
330 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
334 ret = zfs_get_prop_int(zhp, LDD_VERSION_PROP, &ldd->ldd_config_ver);
335 if (ret && (ret != ENOENT))
338 ret = zfs_get_prop_int(zhp, LDD_FLAGS_PROP, &ldd->ldd_flags);
339 if (ret && (ret != ENOENT))
342 ret = zfs_get_prop_int(zhp, LDD_INDEX_PROP, &ldd->ldd_svindex);
343 if (ret && (ret != ENOENT))
346 ret = zfs_get_prop_str(zhp, LDD_FSNAME_PROP, ldd->ldd_fsname);
347 if (ret && (ret != ENOENT))
350 ret = zfs_get_prop_str(zhp, LDD_SVNAME_PROP, ldd->ldd_svname);
351 if (ret && (ret != ENOENT))
354 ret = zfs_get_prop_str(zhp, LDD_UUID_PROP, (char *)ldd->ldd_uuid);
355 if (ret && (ret != ENOENT))
358 ret = zfs_get_prop_str(zhp, LDD_USERDATA_PROP, ldd->ldd_userdata);
359 if (ret && (ret != ENOENT))
362 ret = zfs_get_prop_str(zhp, LDD_MOUNTOPTS_PROP, ldd->ldd_mount_opts);
363 if (ret && (ret != ENOENT))
366 ret = zfs_get_prop_param(zhp, ldd, PARAM_MGSNODE, LDD_MGSNODE_PROP);
367 if (ret && (ret != ENOENT))
370 ret = zfs_get_prop_param(zhp, ldd, PARAM_FAILNODE, LDD_FAILNODE_PROP);
371 if (ret && (ret != ENOENT))
374 ret = zfs_get_prop_param(zhp, ldd, PARAM_FAILMODE, LDD_FAILMODE_PROP);
375 if (ret && (ret != ENOENT))
378 ret = zfs_get_prop_param(zhp, ldd, PARAM_MDT PARAM_ID_UPCALL,
379 LDD_IDENTITY_UPCALL_PROP);
380 if (ret && (ret != ENOENT))
383 ldd->ldd_mount_type = LDD_MT_ZFS;
391 int zfs_is_lustre(char *ds, unsigned *mount_type)
393 struct lustre_disk_data tmp_ldd;
396 if (osd_zfs_setup == 0)
399 ret = zfs_read_ldd(ds, &tmp_ldd);
400 if ((ret == 0) && (tmp_ldd.ldd_config_ver > 0) &&
401 (strlen(tmp_ldd.ldd_fsname) > 0) &&
402 (strlen(tmp_ldd.ldd_svname) > 0)) {
403 *mount_type = tmp_ldd.ldd_mount_type;
410 static char *zfs_mkfs_opts(struct mkfs_opts *mop, char *str, int len)
414 if (strlen(mop->mo_mkfsopts) != 0)
415 snprintf(str, len, " -o %s", mop->mo_mkfsopts);
420 static int zfs_create_vdev(struct mkfs_opts *mop, char *vdev)
424 /* Silently ignore reserved vdev names */
425 if ((strncmp(vdev, "disk", 4) == 0) ||
426 (strncmp(vdev, "file", 4) == 0) ||
427 (strncmp(vdev, "mirror", 6) == 0) ||
428 (strncmp(vdev, "raidz", 5) == 0) ||
429 (strncmp(vdev, "spare", 5) == 0) ||
430 (strncmp(vdev, "log", 3) == 0) ||
431 (strncmp(vdev, "cache", 5) == 0))
435 * Verify a file exists at the provided absolute path. If it doesn't
436 * and mo_device_sz is set attempt to create a file vdev to be used.
437 * Relative paths will be passed directly to 'zpool create' which
438 * will check multiple multiple locations under /dev/.
440 if (vdev[0] == '/') {
441 ret = access(vdev, F_OK);
448 fprintf(stderr, "Unable to access required vdev "
449 "for pool %s (%d)\n", vdev, ret);
453 if (mop->mo_device_sz == 0) {
455 fprintf(stderr, "Unable to create vdev due to "
456 "missing --device-size=#N(KB) parameter\n");
460 ret = file_create(vdev, mop->mo_device_sz);
463 fprintf(stderr, "Unable to create vdev %s (%d)\n",
472 int zfs_make_lustre(struct mkfs_opts *mop)
477 char *mkfs_cmd = NULL;
478 char *mkfs_tmp = NULL;
479 char *ds = mop->mo_device;
480 int pool_exists = 0, ret;
482 if (osd_check_zfs_setup() == 0)
485 /* no automatic index with zfs backend */
486 if (mop->mo_ldd.ldd_flags & LDD_F_NEED_INDEX) {
488 fprintf(stderr, "The target index must be specified with "
497 mkfs_cmd = malloc(PATH_MAX);
498 if (mkfs_cmd == NULL) {
503 mkfs_tmp = malloc(PATH_MAX);
504 if (mkfs_tmp == NULL) {
509 /* Due to zfs_name_valid() check the '/' must exist */
510 strchr(pool, '/')[0] = '\0';
512 /* If --reformat was given attempt to destroy the previous dataset */
513 if ((mop->mo_flags & MO_FORCEFORMAT) &&
514 ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM)) != NULL)) {
516 ret = zfs_destroy(zhp, 0);
519 fprintf(stderr, "Failed destroy zfs dataset %s (%d)\n",
528 * Create the zpool if the vdevs have been specified and the pool
529 * does not already exists. The pool creation itself will be done
530 * with the zpool command rather than the zpool_create() library call
531 * so the existing zpool error handling can be leveraged.
533 php = zpool_open(g_zfs, pool);
539 if ((mop->mo_pool_vdevs != NULL) && (pool_exists == 0)) {
541 memset(mkfs_cmd, 0, PATH_MAX);
542 snprintf(mkfs_cmd, PATH_MAX,
543 "zpool create -f -O canmount=off %s", pool);
545 /* Append the vdev config and create file vdevs as required */
546 while (*mop->mo_pool_vdevs != NULL) {
547 strscat(mkfs_cmd, " ", PATH_MAX);
548 strscat(mkfs_cmd, *mop->mo_pool_vdevs, PATH_MAX);
550 ret = zfs_create_vdev(mop, *mop->mo_pool_vdevs);
554 mop->mo_pool_vdevs++;
557 vprint("mkfs_cmd = %s\n", mkfs_cmd);
558 ret = run_command(mkfs_cmd, PATH_MAX);
561 fprintf(stderr, "Unable to create pool %s (%d)\n",
568 * Create the ZFS filesystem with any required mkfs options:
569 * - canmount=off is set to prevent zfs automounting
570 * - version=4 is set because SA are not yet handled by the osd
572 memset(mkfs_cmd, 0, PATH_MAX);
573 snprintf(mkfs_cmd, PATH_MAX,
574 "zfs create -o canmount=off -o xattr=sa%s %s",
575 zfs_mkfs_opts(mop, mkfs_tmp, PATH_MAX),
578 vprint("mkfs_cmd = %s\n", mkfs_cmd);
579 ret = run_command(mkfs_cmd, PATH_MAX);
582 fprintf(stderr, "Unable to create filesystem %s (%d)\n",
591 if (mkfs_cmd != NULL)
594 if (mkfs_tmp != NULL)
600 int zfs_prepare_lustre(struct mkfs_opts *mop,
601 char *default_mountopts, int default_len,
602 char *always_mountopts, int always_len)
606 if (osd_check_zfs_setup() == 0)
609 ret = zfs_name_valid(mop->mo_device, ZFS_TYPE_FILESYSTEM);
612 fprintf(stderr, "Invalid filesystem name %s\n", mop->mo_device);
623 /* If the ZFS libs are not installed, don't print an error to avoid
624 * spamming ldiskfs users. An error message will still be printed if
625 * someone tries to do some real work involving a ZFS backend */
627 handle_libzfs = dlopen("libzfs.so", RTLD_LAZY);
628 if (handle_libzfs == NULL)
631 handle_nvpair = dlopen("libnvpair.so", RTLD_LAZY);
632 if (handle_nvpair == NULL) {
637 ret = zfs_populate_symbols();
641 if (libzfs_load_module("zfs") != 0) {
642 /* The ZFS modules are not installed */
647 g_zfs = libzfs_init();
649 fprintf(stderr, "Failed to initialize ZFS library\n");
666 dlclose(handle_nvpair);
667 handle_nvpair = NULL;
670 dlclose(handle_libzfs);
671 handle_libzfs = NULL;