Whamcloud - gitweb
LU-9960 osd-zfs: don't auto-upgrade quota
[fs/lustre-release.git] / lustre / utils / libmount_utils_zfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 2016, Intel Corporation.
24  * Use is subject to license terms.
25  */
26 /*
27  * Author: Brian Behlendorf <behlendorf1@llnl.gov>
28  */
29 #include "mount_utils.h"
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <libzfs.h>
34
35 #define HOSTID_PATH "/etc/hostid"
36
37 /* Persistent mount data is stored in these user attributes */
38 #define LDD_PREFIX              "lustre:"
39 #define LDD_VERSION_PROP        LDD_PREFIX "version"
40 #define LDD_FLAGS_PROP          LDD_PREFIX "flags"
41 #define LDD_INDEX_PROP          LDD_PREFIX "index"
42 #define LDD_FSNAME_PROP         LDD_PREFIX "fsname"
43 #define LDD_SVNAME_PROP         LDD_PREFIX "svname"
44 #define LDD_UUID_PROP           LDD_PREFIX "uuid"
45 #define LDD_USERDATA_PROP       LDD_PREFIX "userdata"
46 #define LDD_MOUNTOPTS_PROP      LDD_PREFIX "mountopts"
47
48 /* This structure is used to help bridge the gap between the ZFS
49  * properties Lustre uses and their corresponding internal LDD fields.
50  * It is meant to be used internally by the mount utility only. */
51 struct zfs_ldd_prop_bridge {
52         /* Contains the publicly visible name for the property
53          * (i.e. what is shown when running "zfs get") */
54         char *zlpb_prop_name;
55         /* Contains the offset into the lustre_disk_data structure where
56          * the value of this property is or will be stored. (i.e. the
57          * property is read from and written to this offset within ldd) */
58         int   zlpb_ldd_offset;
59         /* Function pointer responsible for reading in the @prop
60          * property from @zhp and storing it in @ldd_field */
61         int (*zlpb_get_prop_fn)(zfs_handle_t *zhp, char *prop, void *ldd_field);
62         /* Function pointer responsible for writing the value of @ldd_field
63          * into the @prop dataset property in @zhp */
64         int (*zlpb_set_prop_fn)(zfs_handle_t *zhp, char *prop, void *ldd_field);
65 };
66
67 /* Forward declarations needed to initialize the ldd prop bridge list */
68 static int zfs_get_prop_int(zfs_handle_t *, char *, void *);
69 static int zfs_set_prop_int(zfs_handle_t *, char *, void *);
70 static int zfs_get_prop_str(zfs_handle_t *, char *, void *);
71 static int zfs_set_prop_str(zfs_handle_t *, char *, void *);
72
73 /* Helper for initializing the entries in the special_ldd_prop_params list.
74  *    - @name: stored directly in the zlpb_prop_name field
75  *             (e.g. lustre:fsname, lustre:version, etc.)
76  *    - @field: the field in the lustre_disk_data which directly maps to
77  *              the @name property. (e.g. ldd_fsname, ldd_config_ver, etc.)
78  *    - @type: The type of @field. Only "int" and "str" are supported.
79  */
80 #define ZLB_INIT(name, field, type)                                     \
81 {                                                                       \
82         .zlpb_prop_name   = name,                                       \
83         .zlpb_ldd_offset  = offsetof(struct lustre_disk_data, field),   \
84         .zlpb_get_prop_fn = zfs_get_prop_ ## type,                      \
85         .zlpb_set_prop_fn = zfs_set_prop_ ## type                       \
86 }
87
88 /* These ldd properties are special because they all have their own
89  * individual fields in the lustre_disk_data structure, as opposed to
90  * being globbed into the ldd_params field. As such, these need special
91  * handling when reading/writing the ldd structure to/from persistent
92  * storage. */
93 struct zfs_ldd_prop_bridge special_ldd_prop_params[] = {
94         ZLB_INIT(LDD_VERSION_PROP,   ldd_config_ver, int),
95         ZLB_INIT(LDD_FLAGS_PROP,     ldd_flags,      int),
96         ZLB_INIT(LDD_INDEX_PROP,     ldd_svindex,    int),
97         ZLB_INIT(LDD_FSNAME_PROP,    ldd_fsname,     str),
98         ZLB_INIT(LDD_SVNAME_PROP,    ldd_svname,     str),
99         ZLB_INIT(LDD_UUID_PROP,      ldd_uuid,       str),
100         ZLB_INIT(LDD_USERDATA_PROP,  ldd_userdata,   str),
101         ZLB_INIT(LDD_MOUNTOPTS_PROP, ldd_mount_opts, str),
102         { NULL }
103 };
104
105 /* indicate if the ZFS OSD has been successfully setup */
106 static int osd_zfs_setup = 0;
107
108 static libzfs_handle_t *g_zfs;
109
110 void zfs_fini(void);
111
112 static int zfs_set_prop_int(zfs_handle_t *zhp, char *prop, void *val)
113 {
114         char str[64];
115         int ret;
116
117         (void) snprintf(str, sizeof (str), "%i", *(int *)val);
118         vprint("  %s=%s\n", prop, str);
119         ret = zfs_prop_set(zhp, prop, str);
120
121         return ret;
122 }
123
124 /*
125  * Write the zfs property string, note that properties with a NULL or
126  * zero-length value will not be written and 0 returned.
127  */
128 static int zfs_set_prop_str(zfs_handle_t *zhp, char *prop, void *val)
129 {
130         int ret = 0;
131
132         if (val && strlen(val) > 0) {
133                 vprint("  %s=%s\n", prop, (char *)val);
134                 ret = zfs_prop_set(zhp, prop, (char *)val);
135         }
136
137         return ret;
138 }
139
140 /*
141  * Remove a property from zfs property dataset
142  */
143 static int zfs_remove_prop(zfs_handle_t *zhp, nvlist_t *nvl, char *propname)
144 {
145         nvlist_remove_all(nvl, propname);
146         /* XXX: please replace zfs_prop_inherit() if there is a better function
147          * to call zfs_ioctl() to update data on-disk.
148          */
149         return zfs_prop_inherit(zhp, propname, false);
150 }
151
152 static int zfs_erase_prop(zfs_handle_t *zhp, char *param)
153 {
154         nvlist_t *nvl;
155         char propname[ZFS_MAXPROPLEN];
156         int len = strlen(param) + strlen(LDD_PREFIX);
157
158         if (len > ZFS_MAXPROPLEN) {
159                 fprintf(stderr, "%s: zfs prop to erase is too long-\n%s\n",
160                         progname, param);
161                 return EINVAL;
162         }
163
164         nvl = zfs_get_user_props(zhp);
165         if (!nvl)
166                 return ENOENT;
167
168         snprintf(propname, len + 1, "%s%s", LDD_PREFIX, param);
169         return zfs_remove_prop(zhp, nvl, propname);
170 }
171
172 static int zfs_erase_allprops(zfs_handle_t *zhp)
173 {
174         nvlist_t *nvl;
175         nvpair_t *curr = NULL;
176
177         nvl = zfs_get_user_props(zhp);
178         if (!nvl)
179                 return ENOENT;
180
181         curr = nvlist_next_nvpair(nvl, curr);
182         while (curr) {
183                 nvpair_t *next = nvlist_next_nvpair(nvl, curr);
184
185                 zfs_remove_prop(zhp, nvl, nvpair_name(curr));
186                 curr = next;
187         }
188
189         return 0;
190 }
191
192 /*
193  * Map '<key>=<value> ...' pairs in the passed string to dataset properties
194  * of the form 'lustre:<key>=<value>'. "<key>=" means to remove this key
195  * from the dataset.
196  */
197 static int zfs_set_prop_params(zfs_handle_t *zhp, char *params)
198 {
199         char *params_dup, *token, *key, *value;
200         char *save_token = NULL;
201         char propname[ZFS_MAXPROPLEN];
202         int ret = 0;
203
204         params_dup = strdup(params);
205         if (params_dup == NULL)
206                 return ENOMEM;
207
208         token = strtok_r(params_dup, " ", &save_token);
209         while (token) {
210                 key = strtok(token, "=");
211                 if (key == NULL)
212                         continue;
213
214                 value = strtok(NULL, "=");
215                 if (!value) {
216                         /* remove this prop when its value is null */
217                         ret = zfs_erase_prop(zhp, key);
218                         if (ret)
219                                 break;
220                 } else {
221                         snprintf(propname, strlen(LDD_PREFIX) + strlen(key) + 1,
222                                  "%s%s", LDD_PREFIX, key);
223                         vprint("  %s=%s\n", propname, value);
224
225                         ret = zfs_prop_set(zhp, propname, value);
226                         if (ret)
227                                 break;
228                 }
229
230                 token = strtok_r(NULL, " ", &save_token);
231         }
232
233         free(params_dup);
234
235         return ret;
236 }
237
238 static int zfs_check_hostid(struct mkfs_opts *mop)
239 {
240         FILE *f;
241         unsigned long hostid;
242         int rc;
243
244         if (strstr(mop->mo_ldd.ldd_params, PARAM_FAILNODE) == NULL)
245                 return 0;
246
247         f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
248         if (f == NULL) {
249                 fatal();
250                 fprintf(stderr, "Failed to open spl_hostid: %s\n",
251                         strerror(errno));
252                 return errno;
253         }
254         rc = fscanf(f, "%li", &hostid);
255         fclose(f);
256         if (rc != 1) {
257                 fatal();
258                 fprintf(stderr, "Failed to read spl_hostid: %d\n", rc);
259                 return rc;
260         }
261
262         if (hostid != 0)
263                 return 0;
264
265         f = fopen(HOSTID_PATH, "r");
266         if (f == NULL)
267                 goto out;
268
269         rc = fread(&hostid, sizeof(uint32_t), 1, f);
270         fclose(f);
271
272         if (rc != 1) {
273                 fprintf(stderr, "Failed to read "HOSTID_PATH": %d\n",
274                        rc);
275                 hostid = 0;
276         }
277
278 out:
279         if (hostid == 0) {
280                 if (mop->mo_flags & MO_NOHOSTID_CHECK) {
281                         fprintf(stderr, "WARNING: spl_hostid not set. ZFS has "
282                                 "no zpool import protection\n");
283                 } else {
284                         fatal();
285                         fprintf(stderr, "spl_hostid not set. See %s(8)",
286                                 progname);
287                         return EINVAL;
288                 }
289         }
290
291         return 0;
292 }
293
294 static int osd_check_zfs_setup(void)
295 {
296         if (osd_zfs_setup == 0) {
297                 /* setup failed */
298                 fatal();
299                 fprintf(stderr, "Failed to initialize ZFS library. Are the ZFS "
300                         "packages and modules correctly installed?\n");
301         }
302         return osd_zfs_setup == 1;
303 }
304
305 /* Write the server config as properties associated with the dataset */
306 int zfs_write_ldd(struct mkfs_opts *mop)
307 {
308         struct lustre_disk_data *ldd = &mop->mo_ldd;
309         char *ds = mop->mo_device;
310         zfs_handle_t *zhp;
311         struct zfs_ldd_prop_bridge *bridge;
312         int i, ret = EINVAL;
313
314         if (osd_check_zfs_setup() == 0)
315                 goto out;
316
317         zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
318         if (zhp == NULL) {
319                 fprintf(stderr, "Failed to open zfs dataset %s\n", ds);
320                 goto out;
321         }
322
323         ret = zfs_check_hostid(mop);
324         if (ret != 0)
325                 goto out_close;
326
327         vprint("Writing %s properties\n", ds);
328
329         if (mop->mo_flags & MO_ERASE_ALL)
330                 ret = zfs_erase_allprops(zhp);
331         ret = zfs_set_prop_params(zhp, ldd->ldd_params);
332
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_set_prop_fn(zhp, bridge->zlpb_prop_name,
336                                         (void *)ldd + bridge->zlpb_ldd_offset);
337                 if (ret)
338                         goto out_close;
339         }
340
341 out_close:
342         zfs_close(zhp);
343 out:
344         return ret;
345 }
346
347 /* Mark a property to be removed by the form of "key=" */
348 int zfs_erase_ldd(struct mkfs_opts *mop, char *param)
349 {
350         char key[ZFS_MAXPROPLEN] = "";
351
352         if (strlen(LDD_PREFIX) + strlen(param) > ZFS_MAXPROPLEN) {
353                 fprintf(stderr, "%s: zfs prop to erase is too long-\n%s\n",
354                         progname, param);
355                 return EINVAL;
356         }
357         snprintf(key, strlen(param) + 2, "%s=", param);
358         return add_param(mop->mo_ldd.ldd_params, key, "");
359 }
360
361 static int zfs_get_prop_int(zfs_handle_t *zhp, char *prop, void *val)
362 {
363         nvlist_t *propval;
364         char *propstr;
365         int ret;
366
367         ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
368         if (ret)
369                 return ret;
370
371         ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
372         if (ret)
373                 return ret;
374
375         errno = 0;
376         *(__u32 *)val = strtoul(propstr, NULL, 10);
377         if (errno)
378                 return errno;
379
380         return ret;
381 }
382
383 static int zfs_get_prop_str(zfs_handle_t *zhp, char *prop, void *val)
384 {
385         nvlist_t *propval;
386         char *propstr;
387         int ret;
388
389         ret = nvlist_lookup_nvlist(zfs_get_user_props(zhp), prop, &propval);
390         if (ret)
391                 return ret;
392
393         ret = nvlist_lookup_string(propval, ZPROP_VALUE, &propstr);
394         if (ret)
395                 return ret;
396
397         (void) strcpy(val, propstr);
398
399         return ret;
400 }
401
402 static int zfs_is_special_ldd_prop_param(char *name)
403 {
404         int i;
405
406         for (i = 0; special_ldd_prop_params[i].zlpb_prop_name != NULL; i++)
407                 if (!strcmp(name, special_ldd_prop_params[i].zlpb_prop_name))
408                         return 1;
409
410         return 0;
411 }
412
413 static int zfs_get_prop_params(zfs_handle_t *zhp, char *param)
414 {
415         nvlist_t *props;
416         nvpair_t *nvp;
417         char key[ZFS_MAXPROPLEN] = "";
418         char value[PARAM_MAX] = "";
419         int ret = 0;
420
421         props = zfs_get_user_props(zhp);
422         if (props == NULL)
423                 return ENOENT;
424
425         nvp = NULL;
426         while (nvp = nvlist_next_nvpair(props, nvp), nvp) {
427                 ret = zfs_get_prop_str(zhp, nvpair_name(nvp), value);
428                 if (ret)
429                         break;
430
431                 if (strncmp(nvpair_name(nvp), LDD_PREFIX, strlen(LDD_PREFIX)))
432                         continue;
433
434                 if (zfs_is_special_ldd_prop_param(nvpair_name(nvp)))
435                         continue;
436
437                 sprintf(key, "%s=",  nvpair_name(nvp) + strlen(LDD_PREFIX));
438                 ret = add_param(param, key, value);
439                 if (ret)
440                         break;
441         }
442
443         return ret;
444 }
445
446 /*
447  * Read the server config as properties associated with the dataset.
448  * Missing entries as not treated error and are simply skipped.
449  */
450 int zfs_read_ldd(char *ds,  struct lustre_disk_data *ldd)
451 {
452         zfs_handle_t *zhp;
453         struct zfs_ldd_prop_bridge *bridge;
454         int i, ret = EINVAL;
455
456         if (osd_check_zfs_setup() == 0)
457                 return EINVAL;
458
459         zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
460         if (!zhp) {
461                 zhp = zfs_open(g_zfs, ds, ZFS_TYPE_SNAPSHOT);
462                 if (!zhp)
463                         goto out;
464         }
465
466         for (i = 0; special_ldd_prop_params[i].zlpb_prop_name != NULL; i++) {
467                 bridge = &special_ldd_prop_params[i];
468                 ret = bridge->zlpb_get_prop_fn(zhp, bridge->zlpb_prop_name,
469                                         (void *)ldd + bridge->zlpb_ldd_offset);
470                 if (ret && (ret != ENOENT))
471                         goto out_close;
472         }
473
474         ret = zfs_get_prop_params(zhp, ldd->ldd_params);
475         if (ret && (ret != ENOENT))
476                 goto out_close;
477
478         ldd->ldd_mount_type = LDD_MT_ZFS;
479         ret = 0;
480 out_close:
481         zfs_close(zhp);
482 out:
483         return ret;
484 }
485
486 /* Print ldd params */
487 void zfs_print_ldd_params(struct mkfs_opts *mop)
488 {
489         char *from = mop->mo_ldd.ldd_params;
490         char *to;
491         int len;
492
493         vprint("Parameters:");
494         while (from) {
495                 /* skip those keys to be removed in the form of "key=" */
496                 to = strstr(from, "= ");
497                 if (!to)
498                         /* "key=" may be in the end */
499                         if (*(from + strlen(from) - 1) == '=')
500                                 to = from + strlen(from) - 1;
501
502                 /* find " " inward */
503                 len = strlen(from);
504                 if (to) {
505                         len = strlen(from) - strlen(to);
506                         while ((*(from + len) != ' ') && len)
507                                 len--;
508                 }
509                 if (len)
510                         /* no space in the end */
511                         vprint("%*.*s", len, len, from);
512
513                 /* If there is no "key=" or "key=" is in the end, stop. */
514                 if (!to || strlen(to) == 1)
515                         break;
516
517                 /* skip "=" */
518                 from = to + 1;
519         }
520 }
521
522 int zfs_is_lustre(char *ds, unsigned *mount_type)
523 {
524         struct lustre_disk_data tmp_ldd;
525         int ret;
526
527         if (osd_zfs_setup == 0)
528                 return 0;
529
530         ret = zfs_read_ldd(ds, &tmp_ldd);
531         if ((ret == 0) && (tmp_ldd.ldd_config_ver > 0) &&
532             (strlen(tmp_ldd.ldd_svname) > 0)) {
533                 *mount_type = tmp_ldd.ldd_mount_type;
534                 return 1;
535         }
536
537         return 0;
538 }
539
540 static char *zfs_mkfs_opts(struct mkfs_opts *mop, char *str, int len)
541 {
542         memset(str, 0, len);
543
544         if (strlen(mop->mo_mkfsopts) != 0)
545                 snprintf(str, len, " -o %s", mop->mo_mkfsopts);
546
547         return str;
548 }
549
550 static int zfs_create_vdev(struct mkfs_opts *mop, char *vdev)
551 {
552         int ret = 0;
553
554         /* Silently ignore reserved vdev names */
555         if ((strncmp(vdev, "disk", 4) == 0) ||
556             (strncmp(vdev, "file", 4) == 0) ||
557             (strncmp(vdev, "mirror", 6) == 0) ||
558             (strncmp(vdev, "raidz", 5) == 0) ||
559             (strncmp(vdev, "spare", 5) == 0) ||
560             (strncmp(vdev, "log", 3) == 0) ||
561             (strncmp(vdev, "cache", 5) == 0))
562                 return ret;
563
564         /*
565          * Verify a file exists at the provided absolute path.  If it doesn't
566          * and mo_device_kb is set attempt to create a file vdev to be used.
567          * Relative paths will be passed directly to 'zpool create' which
568          * will check multiple multiple locations under /dev/.
569          */
570         if (vdev[0] == '/') {
571                 ret = access(vdev, F_OK);
572                 if (ret == 0)
573                         return ret;
574
575                 ret = errno;
576                 if (ret != ENOENT) {
577                         fatal();
578                         fprintf(stderr, "Unable to access required vdev "
579                                 "for pool %s (%d)\n", vdev, ret);
580                         return ret;
581                 }
582
583                 if (mop->mo_device_kb == 0) {
584                         fatal();
585                         fprintf(stderr, "Unable to create vdev due to "
586                                 "missing --device-size=#N(KB) parameter\n");
587                         return EINVAL;
588                 }
589
590                 ret = file_create(vdev, mop->mo_device_kb);
591                 if (ret) {
592                         fatal();
593                         fprintf(stderr, "Unable to create vdev %s (%d)\n",
594                                 vdev, ret);
595                         return ret;
596                 }
597         }
598
599         return ret;
600 }
601
602 int zfs_make_lustre(struct mkfs_opts *mop)
603 {
604         zfs_handle_t *zhp;
605         zpool_handle_t *php;
606         char *pool = NULL;
607         char *mkfs_cmd = NULL;
608         char *mkfs_tmp = NULL;
609         char *ds = mop->mo_device;
610         int pool_exists = 0, ret;
611
612         if (osd_check_zfs_setup() == 0)
613                 return EINVAL;
614
615         /* no automatic index with zfs backend */
616         if (mop->mo_ldd.ldd_flags & LDD_F_NEED_INDEX) {
617                 fatal();
618                 fprintf(stderr, "The target index must be specified with "
619                                 "--index\n");
620                 return EINVAL;
621         }
622
623         ret = zfs_check_hostid(mop);
624         if (ret != 0)
625                 goto out;
626
627         pool = strdup(ds);
628         if (pool == NULL)
629                 return ENOMEM;
630
631         mkfs_cmd = malloc(PATH_MAX);
632         if (mkfs_cmd == NULL) {
633                 ret = ENOMEM;
634                 goto out;
635         }
636
637         mkfs_tmp = malloc(PATH_MAX);
638         if (mkfs_tmp == NULL) {
639                 ret = ENOMEM;
640                 goto out;
641         }
642
643         /* Due to zfs_prepare_lustre() check the '/' must exist */
644         strchr(pool, '/')[0] = '\0';
645
646         /* If --reformat was given attempt to destroy the previous dataset */
647         if ((mop->mo_flags & MO_FORCEFORMAT) &&
648             ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM)) != NULL)) {
649
650                 ret = zfs_destroy(zhp, 0);
651                 if (ret) {
652                         zfs_close(zhp);
653                         fprintf(stderr, "Failed destroy zfs dataset %s (%d)\n",
654                                 ds, ret);
655                         goto out;
656                 }
657
658                 zfs_close(zhp);
659         }
660
661         /*
662          * Create the zpool if the vdevs have been specified and the pool
663          * does not already exists.  The pool creation itself will be done
664          * with the zpool command rather than the zpool_create() library call
665          * so the existing zpool error handling can be leveraged.
666          */
667         php = zpool_open(g_zfs, pool);
668         if (php) {
669                 pool_exists = 1;
670                 zpool_set_prop(php, "canmount", "off");
671                 zpool_close(php);
672         }
673
674         if ((mop->mo_pool_vdevs != NULL) && (pool_exists == 0)) {
675
676                 memset(mkfs_cmd, 0, PATH_MAX);
677                 snprintf(mkfs_cmd, PATH_MAX,
678                         "zpool create -f -O canmount=off %s", pool);
679
680                 /* Append the vdev config and create file vdevs as required */
681                 while (*mop->mo_pool_vdevs != NULL) {
682                         strscat(mkfs_cmd, " ", PATH_MAX);
683                         strscat(mkfs_cmd, *mop->mo_pool_vdevs, PATH_MAX);
684
685                         ret = zfs_create_vdev(mop, *mop->mo_pool_vdevs);
686                         if (ret)
687                                 goto out;
688
689                         mop->mo_pool_vdevs++;
690                 }
691
692                 vprint("mkfs_cmd = %s\n", mkfs_cmd);
693                 ret = run_command(mkfs_cmd, PATH_MAX);
694                 if (ret) {
695                         fatal();
696                         fprintf(stderr, "Unable to create pool %s (%d)\n",
697                                 pool, ret);
698                         goto out;
699                 }
700         }
701
702         /*
703          * Set Options on ZPOOL
704          *
705          * ALL   - canmount=off (set above)
706          * 0.7.0 - multihost=on
707          * 0.7.0 - feature@userobj_accounting=enabled
708          */
709         php = zpool_open(g_zfs, pool);
710         if (php) {
711                 zpool_set_prop(php, "multihost", "on");
712                 zpool_set_prop(php, "feature@userobj_accounting", "enabled");
713
714                 zpool_close(php);
715         }
716
717         /*
718          * Create the ZFS filesystem with any required mkfs options:
719          * - canmount=off is set to prevent zfs automounting
720          */
721         memset(mkfs_cmd, 0, PATH_MAX);
722         snprintf(mkfs_cmd, PATH_MAX,
723                  "zfs create -o canmount=off %s %s",
724                  zfs_mkfs_opts(mop, mkfs_tmp, PATH_MAX), ds);
725
726         vprint("mkfs_cmd = %s\n", mkfs_cmd);
727         ret = run_command(mkfs_cmd, PATH_MAX);
728         if (ret) {
729                 fatal();
730                 fprintf(stderr, "Unable to create filesystem %s (%d)\n",
731                         ds, ret);
732                 goto out;
733         }
734
735         /*
736          * Attempt to set dataset properties to reasonable defaults
737          * to optimize performance, unless the values were specified
738          * at the mkfs command line. Some ZFS pools or ZFS versions
739          * do not support these properties. We can safely ignore the
740          * errors and continue in those cases.
741          *
742          * zfs 0.6.1 - system attribute based xattrs
743          * zfs 0.6.5 - large block support
744          * zfs 0.7.0 - large dnode support
745          *
746          * Check if zhp is NULL as a defensive measure. Any dataset
747          * validation errors that would cause zfs_open() to fail
748          * should have been caught earlier.
749          */
750         zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
751         if (zhp) {
752                 /* zfs 0.6.1 - system attribute based xattrs */
753                 if (!strstr(mop->mo_mkfsopts, "xattr="))
754                         zfs_set_prop_str(zhp, "xattr", "sa");
755
756                 /* zfs 0.7.0 - large dnode support */
757                 if (!strstr(mop->mo_mkfsopts, "dnodesize=") &&
758                     !strstr(mop->mo_mkfsopts, "dnsize="))
759                         zfs_set_prop_str(zhp, "dnodesize", "auto");
760
761                 if (IS_OST(&mop->mo_ldd)) {
762                         /* zfs 0.6.5 - large block support */
763                         if (!strstr(mop->mo_mkfsopts, "recordsize=") &&
764                             !strstr(mop->mo_mkfsopts, "recsize="))
765                                 zfs_set_prop_str(zhp, "recordsize", "1M");
766                 }
767
768                 zfs_close(zhp);
769         }
770
771 out:
772         if (pool != NULL)
773                 free(pool);
774
775         if (mkfs_cmd != NULL)
776                 free(mkfs_cmd);
777
778         if (mkfs_tmp != NULL)
779                 free(mkfs_tmp);
780
781         return ret;
782 }
783
784 int zfs_enable_quota(struct mkfs_opts *mop)
785 {
786         fprintf(stderr, "this option is not only valid for zfs\n");
787         return ENOSYS;
788 }
789
790 int zfs_prepare_lustre(struct mkfs_opts *mop,
791                        char *wanted_mountopts, size_t len)
792 {
793         if (osd_check_zfs_setup() == 0)
794                 return EINVAL;
795
796         if (zfs_name_valid(mop->mo_device, ZFS_TYPE_FILESYSTEM) == 0) {
797                 fatal();
798                 fprintf(stderr, "Invalid filesystem name %s\n", mop->mo_device);
799                 return EINVAL;
800         }
801
802         if (strchr(mop->mo_device, '/') == NULL) {
803                 fatal();
804                 fprintf(stderr, "Missing pool in filesystem name %s\n",
805                         mop->mo_device);
806                 return EINVAL;
807         }
808
809         return 0;
810 }
811
812 int zfs_tune_lustre(char *dev, struct mount_opts *mop)
813 {
814         if (osd_check_zfs_setup() == 0)
815                 return EINVAL;
816
817         return 0;
818 }
819
820 int zfs_label_lustre(struct mount_opts *mop)
821 {
822         zfs_handle_t *zhp;
823         int ret;
824
825         if (osd_check_zfs_setup() == 0)
826                 return EINVAL;
827
828         zhp = zfs_open(g_zfs, mop->mo_source, ZFS_TYPE_FILESYSTEM);
829         if (zhp == NULL)
830                 return EINVAL;
831
832         ret = zfs_set_prop_str(zhp, LDD_SVNAME_PROP, mop->mo_ldd.ldd_svname);
833         zfs_close(zhp);
834
835         return ret;
836 }
837
838 int zfs_rename_fsname(struct mkfs_opts *mop, const char *oldname)
839 {
840         struct mount_opts opts;
841         char mntpt[] = "/tmp/mntXXXXXX";
842         char *cmd_buf;
843         int ret;
844
845         /* Change the filesystem label. */
846         opts.mo_ldd = mop->mo_ldd;
847         opts.mo_source = mop->mo_device;
848         ret = zfs_label_lustre(&opts);
849         if (ret) {
850                 if (errno != 0)
851                         ret = errno;
852                 fprintf(stderr, "Can't change filesystem label: %s\n",
853                         strerror(ret));
854                 return ret;
855         }
856
857         /* Mount this device temporarily in order to write these files */
858         if (mkdtemp(mntpt) == NULL) {
859                 if (errno != 0)
860                         ret = errno;
861                 fprintf(stderr, "Can't create temp mount point %s: %s\n",
862                         mntpt, strerror(ret));
863                 return ret;
864         }
865
866         cmd_buf = malloc(PATH_MAX);
867         if (!cmd_buf) {
868                 ret = ENOMEM;
869                 goto out_rmdir;
870         }
871
872         memset(cmd_buf, 0, PATH_MAX);
873         snprintf(cmd_buf, PATH_MAX - 1, "zfs set mountpoint=%s %s && "
874                  "zfs set canmount=on %s && zfs mount %s",
875                  mntpt, mop->mo_device, mop->mo_device, mop->mo_device);
876         ret = run_command(cmd_buf, PATH_MAX);
877         if (ret) {
878                 if (errno != 0)
879                         ret = errno;
880                 fprintf(stderr, "Unable to mount %s (%s)\n",
881                         mop->mo_device, strerror(ret));
882                 if (ret == ENODEV)
883                         fprintf(stderr, "Is the %s module available?\n",
884                                 MT_STR(&mop->mo_ldd));
885                 goto out_free;
886         }
887
888         ret = lustre_rename_fsname(mop, mntpt, oldname);
889         memset(cmd_buf, 0, PATH_MAX);
890         snprintf(cmd_buf, PATH_MAX - 1, "zfs umount %s && "
891                  "zfs set canmount=off %s && zfs set mountpoint=none %s",
892                  mop->mo_device, mop->mo_device, mop->mo_device);
893         run_command(cmd_buf, PATH_MAX);
894
895 out_free:
896         free(cmd_buf);
897 out_rmdir:
898         rmdir(mntpt);
899         return ret;
900 }
901
902 int zfs_init(void)
903 {
904         int ret = 0;
905
906         g_zfs = libzfs_init();
907
908         if (g_zfs == NULL) {
909                 /* Try to load zfs.ko and retry libzfs_init() */
910
911                 ret = system("/sbin/modprobe -q zfs");
912
913                 if (ret == 0) {
914                         g_zfs = libzfs_init();
915                         if (g_zfs == NULL)
916                                 ret = EINVAL;
917                 }
918         }
919
920         if (ret == 0)
921                 osd_zfs_setup = 1;
922         else
923                 fprintf(stderr, "Failed to initialize ZFS library: %d\n", ret);
924
925         return ret;
926 }
927
928 void zfs_fini(void)
929 {
930         if (g_zfs) {
931                 libzfs_fini(g_zfs);
932                 g_zfs = NULL;
933         }
934         osd_zfs_setup = 0;
935 }
936
937 #ifndef PLUGIN_DIR
938 struct module_backfs_ops zfs_ops = {
939         .init                   = zfs_init,
940         .fini                   = zfs_fini,
941         .read_ldd               = zfs_read_ldd,
942         .write_ldd              = zfs_write_ldd,
943         .erase_ldd              = zfs_erase_ldd,
944         .print_ldd_params       = zfs_print_ldd_params,
945         .is_lustre              = zfs_is_lustre,
946         .make_lustre            = zfs_make_lustre,
947         .prepare_lustre         = zfs_prepare_lustre,
948         .tune_lustre            = zfs_tune_lustre,
949         .label_lustre           = zfs_label_lustre,
950         .enable_quota           = zfs_enable_quota,
951         .rename_fsname          = zfs_rename_fsname,
952 };
953 #endif /* PLUGIN_DIR */