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, Intel Corporation.
24 * Use is subject to license terms.
28 * Author: Brian Behlendorf <behlendorf1@llnl.gov>
30 #include "mount_utils.h"
36 /* Persistent mount data is stored in these user attributes */
37 #define LDD_VERSION_PROP "lustre:version"
38 #define LDD_FLAGS_PROP "lustre:flags"
39 #define LDD_INDEX_PROP "lustre:index"
40 #define LDD_FSNAME_PROP "lustre:fsname"
41 #define LDD_SVNAME_PROP "lustre:svname"
42 #define LDD_UUID_PROP "lustre:uuid"
43 #define LDD_USERDATA_PROP "lustre:userdata"
44 #define LDD_MOUNTOPTS_PROP "lustre:mountopts"
45 #define LDD_PARAMS_PROP "lustre:params"
47 /* indicate if the ZFS OSD has been successfully setup */
48 static int osd_zfs_setup = 0;
50 static libzfs_handle_t *g_zfs;
52 /* dynamic linking handles for libzfs & libnvpair */
53 static void *handle_libzfs;
54 static void *handle_nvpair;
56 /* symbol table looked up with dlsym */
58 libzfs_handle_t *(*libzfs_init)(void);
59 void (*libzfs_fini)(libzfs_handle_t *);
60 int (*libzfs_load_module)(char *);
61 zfs_handle_t* (*zfs_open)(libzfs_handle_t *, const char *, int);
62 int (*zfs_destroy)(zfs_handle_t *, boolean_t);
63 void (*zfs_close)(zfs_handle_t *);
64 int (*zfs_prop_set)(zfs_handle_t*, const char*, const char*);
65 nvlist_t* (*zfs_get_user_props) (zfs_handle_t *);
66 int (*zfs_name_valid)(const char *, zfs_type_t);
67 zpool_handle_t* (*zpool_open)(libzfs_handle_t *, const char *);
68 void (*zpool_close)(zpool_handle_t *zhp);
69 int (*nvlist_lookup_string)(nvlist_t*, const char*, char**);
70 int (*nvlist_lookup_nvlist)(nvlist_t *, const char *, nvlist_t **);
73 static struct zfs_symbols sym;
76 #define DLSYM(handle, func) \
78 sym.func = (typeof(sym.func))dlsym(handle, #func); \
81 /* populate the symbol table after a successful call to dlopen() */
82 static int zfs_populate_symbols(void)
86 dlerror(); /* Clear any existing error */
88 DLSYM(handle_libzfs, libzfs_init);
89 #define libzfs_init (*sym.libzfs_init)
90 DLSYM(handle_libzfs, libzfs_fini);
91 #define libzfs_fini (*sym.libzfs_fini)
92 DLSYM(handle_libzfs, libzfs_load_module);
93 #define libzfs_load_module (*sym.libzfs_load_module)
94 DLSYM(handle_libzfs, zfs_open);
95 #define zfs_open (*sym.zfs_open)
96 DLSYM(handle_libzfs, zfs_destroy);
97 #define zfs_destroy (*sym.zfs_destroy)
98 DLSYM(handle_libzfs, zfs_close);
99 #define zfs_close (*sym.zfs_close)
100 DLSYM(handle_libzfs, zfs_prop_set);
101 #define zfs_prop_set (*sym.zfs_prop_set)
102 DLSYM(handle_libzfs, zfs_get_user_props);
103 #define zfs_get_user_props (*sym.zfs_get_user_props)
104 DLSYM(handle_libzfs, zfs_name_valid);
105 #define zfs_name_valid (*sym.zfs_name_valid)
106 DLSYM(handle_libzfs, zpool_open);
107 #define zpool_open (*sym.zpool_open)
108 DLSYM(handle_libzfs, zpool_close);
109 #define zpool_close (*sym.zpool_close)
110 DLSYM(handle_nvpair, nvlist_lookup_string);
111 #define nvlist_lookup_string (*sym.nvlist_lookup_string)
112 DLSYM(handle_nvpair, nvlist_lookup_nvlist);
113 #define nvlist_lookup_nvlist (*sym.nvlist_lookup_nvlist)
118 fprintf(stderr, "%s\n", error);
124 static int zfs_set_prop_int(zfs_handle_t *zhp, char *prop, __u32 val)
129 (void) snprintf(str, sizeof (str), "%lu", (unsigned long)val);
130 vprint(" %s=%s\n", prop, str);
131 ret = zfs_prop_set(zhp, prop, str);
137 * Write the zfs property string, note that properties with a NULL or
138 * zero-length value will not be written and 0 returned.
140 static int zfs_set_prop_str(zfs_handle_t *zhp, char *prop, char *val)
144 if (val && strlen(val) > 0) {
145 vprint(" %s=%s\n", prop, val);
146 ret = zfs_prop_set(zhp, prop, val);
152 static int osd_check_zfs_setup(void)
154 if (osd_zfs_setup == 0) {
157 fprintf(stderr, "Failed to initialize ZFS library. Are the ZFS "
158 "packages and modules correctly installed?\n");
160 return osd_zfs_setup == 1;
163 /* Write the server config as properties associated with the dataset */
164 int zfs_write_ldd(struct mkfs_opts *mop)
166 struct lustre_disk_data *ldd = &mop->mo_ldd;
167 char *ds = mop->mo_device;
171 if (osd_check_zfs_setup() == 0)
174 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
176 fprintf(stderr, "Failed to open zfs dataset %s\n", ds);
180 vprint("Writing %s properties\n", ds);
182 ret = zfs_set_prop_int(zhp, LDD_VERSION_PROP, ldd->ldd_config_ver);
186 ret = zfs_set_prop_int(zhp, LDD_FLAGS_PROP, ldd->ldd_flags);
190 ret = zfs_set_prop_int(zhp, LDD_INDEX_PROP, ldd->ldd_svindex);
194 ret = zfs_set_prop_str(zhp, LDD_FSNAME_PROP, ldd->ldd_fsname);
198 ret = zfs_set_prop_str(zhp, LDD_SVNAME_PROP, ldd->ldd_svname);
202 ret = zfs_set_prop_str(zhp, LDD_UUID_PROP, (char *)ldd->ldd_uuid);
206 ret = zfs_set_prop_str(zhp, LDD_USERDATA_PROP, ldd->ldd_userdata);
210 ret = zfs_set_prop_str(zhp, LDD_MOUNTOPTS_PROP, ldd->ldd_mount_opts);
214 if (strlen(ldd->ldd_params) > ZAP_MAXVALUELEN) {
218 ret = zfs_set_prop_str(zhp, LDD_PARAMS_PROP, ldd->ldd_params);
226 static int zfs_get_prop_int(zfs_handle_t *zhp, char *prop, __u32 *val)
232 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
236 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
241 *val = strtoul(propstr, NULL, 10);
248 static int zfs_get_prop_str(zfs_handle_t *zhp, char *prop, char *val)
254 ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
258 ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
262 (void) strcpy(val, propstr);
268 * Read the server config as properties associated with the dataset.
269 * Missing entries as not treated error and are simply skipped.
271 int zfs_read_ldd(char *ds, struct lustre_disk_data *ldd)
276 if (osd_check_zfs_setup() == 0)
279 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
283 ret = zfs_get_prop_int(zhp, LDD_VERSION_PROP, &ldd->ldd_config_ver);
284 if (ret && (ret != ENOENT))
287 ret = zfs_get_prop_int(zhp, LDD_FLAGS_PROP, &ldd->ldd_flags);
288 if (ret && (ret != ENOENT))
291 ret = zfs_get_prop_int(zhp, LDD_INDEX_PROP, &ldd->ldd_svindex);
292 if (ret && (ret != ENOENT))
295 ret = zfs_get_prop_str(zhp, LDD_FSNAME_PROP, ldd->ldd_fsname);
296 if (ret && (ret != ENOENT))
299 ret = zfs_get_prop_str(zhp, LDD_SVNAME_PROP, ldd->ldd_svname);
300 if (ret && (ret != ENOENT))
303 ret = zfs_get_prop_str(zhp, LDD_UUID_PROP, (char *)ldd->ldd_uuid);
304 if (ret && (ret != ENOENT))
307 ret = zfs_get_prop_str(zhp, LDD_USERDATA_PROP, ldd->ldd_userdata);
308 if (ret && (ret != ENOENT))
311 ret = zfs_get_prop_str(zhp, LDD_MOUNTOPTS_PROP, ldd->ldd_mount_opts);
312 if (ret && (ret != ENOENT))
315 ret = zfs_get_prop_str(zhp, LDD_PARAMS_PROP, ldd->ldd_params);
316 if (ret && (ret != ENOENT))
319 ldd->ldd_mount_type = LDD_MT_ZFS;
327 int zfs_is_lustre(char *ds, unsigned *mount_type)
329 struct lustre_disk_data tmp_ldd;
332 if (osd_zfs_setup == 0)
335 ret = zfs_read_ldd(ds, &tmp_ldd);
336 if ((ret == 0) && (tmp_ldd.ldd_config_ver > 0) &&
337 (strlen(tmp_ldd.ldd_svname) > 0)) {
338 *mount_type = tmp_ldd.ldd_mount_type;
345 static char *zfs_mkfs_opts(struct mkfs_opts *mop, char *str, int len)
349 if (strlen(mop->mo_mkfsopts) != 0)
350 snprintf(str, len, " -o %s", mop->mo_mkfsopts);
355 static int zfs_create_vdev(struct mkfs_opts *mop, char *vdev)
359 /* Silently ignore reserved vdev names */
360 if ((strncmp(vdev, "disk", 4) == 0) ||
361 (strncmp(vdev, "file", 4) == 0) ||
362 (strncmp(vdev, "mirror", 6) == 0) ||
363 (strncmp(vdev, "raidz", 5) == 0) ||
364 (strncmp(vdev, "spare", 5) == 0) ||
365 (strncmp(vdev, "log", 3) == 0) ||
366 (strncmp(vdev, "cache", 5) == 0))
370 * Verify a file exists at the provided absolute path. If it doesn't
371 * and mo_device_sz is set attempt to create a file vdev to be used.
372 * Relative paths will be passed directly to 'zpool create' which
373 * will check multiple multiple locations under /dev/.
375 if (vdev[0] == '/') {
376 ret = access(vdev, F_OK);
383 fprintf(stderr, "Unable to access required vdev "
384 "for pool %s (%d)\n", vdev, ret);
388 if (mop->mo_device_sz == 0) {
390 fprintf(stderr, "Unable to create vdev due to "
391 "missing --device-size=#N(KB) parameter\n");
395 ret = file_create(vdev, mop->mo_device_sz);
398 fprintf(stderr, "Unable to create vdev %s (%d)\n",
407 int zfs_make_lustre(struct mkfs_opts *mop)
412 char *mkfs_cmd = NULL;
413 char *mkfs_tmp = NULL;
414 char *ds = mop->mo_device;
415 int pool_exists = 0, ret;
417 if (osd_check_zfs_setup() == 0)
420 /* no automatic index with zfs backend */
421 if (mop->mo_ldd.ldd_flags & LDD_F_NEED_INDEX) {
423 fprintf(stderr, "The target index must be specified with "
432 mkfs_cmd = malloc(PATH_MAX);
433 if (mkfs_cmd == NULL) {
438 mkfs_tmp = malloc(PATH_MAX);
439 if (mkfs_tmp == NULL) {
444 /* Due to zfs_prepare_lustre() check the '/' must exist */
445 strchr(pool, '/')[0] = '\0';
447 /* If --reformat was given attempt to destroy the previous dataset */
448 if ((mop->mo_flags & MO_FORCEFORMAT) &&
449 ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM)) != NULL)) {
451 ret = zfs_destroy(zhp, 0);
454 fprintf(stderr, "Failed destroy zfs dataset %s (%d)\n",
463 * Create the zpool if the vdevs have been specified and the pool
464 * does not already exists. The pool creation itself will be done
465 * with the zpool command rather than the zpool_create() library call
466 * so the existing zpool error handling can be leveraged.
468 php = zpool_open(g_zfs, pool);
474 if ((mop->mo_pool_vdevs != NULL) && (pool_exists == 0)) {
476 memset(mkfs_cmd, 0, PATH_MAX);
477 snprintf(mkfs_cmd, PATH_MAX,
478 "zpool create -f -O canmount=off %s", pool);
480 /* Append the vdev config and create file vdevs as required */
481 while (*mop->mo_pool_vdevs != NULL) {
482 strscat(mkfs_cmd, " ", PATH_MAX);
483 strscat(mkfs_cmd, *mop->mo_pool_vdevs, PATH_MAX);
485 ret = zfs_create_vdev(mop, *mop->mo_pool_vdevs);
489 mop->mo_pool_vdevs++;
492 vprint("mkfs_cmd = %s\n", mkfs_cmd);
493 ret = run_command(mkfs_cmd, PATH_MAX);
496 fprintf(stderr, "Unable to create pool %s (%d)\n",
503 * Create the ZFS filesystem with any required mkfs options:
504 * - canmount=off is set to prevent zfs automounting
505 * - version=4 is set because SA are not yet handled by the osd
507 memset(mkfs_cmd, 0, PATH_MAX);
508 snprintf(mkfs_cmd, PATH_MAX,
509 "zfs create -o canmount=off -o xattr=sa%s %s",
510 zfs_mkfs_opts(mop, mkfs_tmp, PATH_MAX),
513 vprint("mkfs_cmd = %s\n", mkfs_cmd);
514 ret = run_command(mkfs_cmd, PATH_MAX);
517 fprintf(stderr, "Unable to create filesystem %s (%d)\n",
526 if (mkfs_cmd != NULL)
529 if (mkfs_tmp != NULL)
535 int zfs_prepare_lustre(struct mkfs_opts *mop,
536 char *default_mountopts, int default_len,
537 char *always_mountopts, int always_len)
539 if (osd_check_zfs_setup() == 0)
542 if (zfs_name_valid(mop->mo_device, ZFS_TYPE_FILESYSTEM) == 0) {
544 fprintf(stderr, "Invalid filesystem name %s\n", mop->mo_device);
548 if (strchr(mop->mo_device, '/') == NULL) {
550 fprintf(stderr, "Missing pool in filesystem name %s\n",
558 int zfs_tune_lustre(char *dev, struct mount_opts *mop)
560 if (osd_check_zfs_setup() == 0)
566 int zfs_label_lustre(struct mount_opts *mop)
571 if (osd_check_zfs_setup() == 0)
574 zhp = zfs_open(g_zfs, mop->mo_source, ZFS_TYPE_FILESYSTEM);
578 ret = zfs_set_prop_str(zhp, LDD_SVNAME_PROP, mop->mo_ldd.ldd_svname);
588 /* If the ZFS libs are not installed, don't print an error to avoid
589 * spamming ldiskfs users. An error message will still be printed if
590 * someone tries to do some real work involving a ZFS backend */
592 handle_libzfs = dlopen("libzfs.so", RTLD_LAZY);
593 if (handle_libzfs == NULL)
596 handle_nvpair = dlopen("libnvpair.so", RTLD_LAZY);
597 if (handle_nvpair == NULL) {
602 ret = zfs_populate_symbols();
606 if (libzfs_load_module("zfs") != 0) {
607 /* The ZFS modules are not installed */
612 g_zfs = libzfs_init();
614 fprintf(stderr, "Failed to initialize ZFS library\n");
631 dlclose(handle_nvpair);
632 handle_nvpair = NULL;
635 dlclose(handle_libzfs);
636 handle_libzfs = NULL;