Whamcloud - gitweb
94d35ce4000b6e5117478e8a8cbaea518e697e7c
[fs/lustre-release.git] / lustre / utils / mkfs_lustre.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/mkfs_lustre.c
32  *
33  * Author: Nathan Rutman <nathan@clusterfs.com>
34 */
35
36 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
37
38 #if HAVE_CONFIG_H
39 #  include "config.h"
40 #endif /* HAVE_CONFIG_H */
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <mntent.h>
51 #include <stdbool.h>
52
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/mount.h>
56 #include <sys/utsname.h>
57
58 #include <string.h>
59 #include <getopt.h>
60 #include <limits.h>
61 #include <ctype.h>
62 #include <linux/lnet/nidstr.h>
63 #include <linux/lnet/lnetctl.h>
64 #include <linux/lustre/lustre_user.h>
65 #include <linux/lustre/lustre_ver.h>
66
67 #include "mount_utils.h"
68
69 char *progname;
70 int verbose = 1;
71 int version;
72 static int print_only;
73
74 #ifdef HAVE_LDISKFS_OSD
75 #define FSLIST_LDISKFS "ldiskfs"
76 #define HAVE_FSLIST
77 #else
78  #define FSLIST_LDISKFS ""
79 #endif /* HAVE_LDISKFS_OSD */
80 #ifdef HAVE_ZFS_OSD
81  #ifdef HAVE_FSLIST
82    #define FSLIST_ZFS "|zfs"
83  #else
84   #define FSLIST_ZFS "zfs"
85   #define HAVE_FSLIST
86  #endif
87 #else
88  #define FSLIST_ZFS ""
89 #endif /* HAVE_ZFS_OSD */
90
91 #ifndef HAVE_FSLIST
92  #error "no backing OSD types (ldiskfs or ZFS) are configured"
93 #endif
94
95 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
96
97 void usage(FILE *out)
98 {
99         fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
100                 "--fsname=<filesystem name>\n"
101                 "\t--index=<target index> [options] <device>\n", progname);
102 #ifdef HAVE_ZFS_OSD
103         fprintf(out, "usage: %s <target type> --backfstype=zfs "
104                 "--fsname=<filesystem name> [options]\n"
105                 "\t<pool name>/<dataset name>\n"
106                 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
107                 progname);
108 #endif
109         fprintf(out,
110                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
111 #ifdef HAVE_ZFS_OSD
112                 "\t<pool name>: name of ZFS pool where target is created "
113                         "(e.g. tank)\n"
114                 "\t<dataset name>: name of new dataset, must be unique within "
115                         "pool (e.g. ost1)\n"
116                 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
117                         "cache, log)\n"
118 #endif
119                 "\n"
120                 "\ttarget types:\n"
121                 "\t\t--mgs: configuration management service\n"
122                 "\t\t--nomgs: turn off MGS service on this MDT\n"
123 #ifndef TUNEFS
124                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
125                 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
126 #endif
127                 "\toptions (in order of popularity):\n"
128                 "\t\t--index=#N: numerical target index (0..N)\n"
129                 "\t\t\trequired for all targets other than the MGS,\n"
130                 "\t\t\ttarget index may either be a decimal number or\n"
131                 "\t\t\thexadecimal number starting with '0x'\n"
132                 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
133                 "\t\t\trequired for all targets other than MGS\n"
134                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
135                 "\t\t\trequired for all targets other than MGS\n"
136                 "\t\t--mountfsoptions=<opts>: permanent Lustre mount options\n"
137                 "\t\t--backfs-mount-opts=<opts>: backing fs mount options\n"
138                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
139                 "\t\t\tmutually exclusive with --servicenode\n"
140                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
141                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
142                         "with --failnode\n"
143                 "\t\t--param <key>=<value>: set a permanent parameter\n"
144                 "\t\t\te.g. --param sys.timeout=40\n"
145                 "\t\t\t     --param lov.stripesize=2M\n"
146                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
147 #ifndef TUNEFS
148                 "\t\t--backfstype=<fstype>: backing fs type (ldiskfs, zfs)\n"
149                 "\t\t--device-size=#N(KB): device size for loop devices\n"
150                 "\t\t--mkfsoptions=<opts>: format options\n"
151                 "\t\t--reformat: overwrite an existing disk\n"
152                 "\t\t--replace: replace an old target with the same index\n"
153                 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
154 #else
155                 "\t\t--erase-param <key>: erase all instances of a parameter\n"
156                 "\t\t--erase-params: erase all old parameter settings\n"
157                 "\t\t--writeconf: erase all config logs for this fs.\n"
158                 "\t\t--nolocallogs: use logs from MGS, not local ones.\n"
159                 "\t\t--quota: enable space accounting on old 2.x device.\n"
160                 "\t\t--rename: rename the filesystem name\n"
161 #endif
162                 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
163                 "\t\t--dryrun: report what we would do; don't write to disk\n"
164                 "\t\t--verbose: e.g. show mkfs progress\n"
165                 "\t\t--force-nohostid: Ignore hostid requirement for ZFS "
166                         "import\n"
167                 "\t\t-V|--version: output build version of the utility and\n"
168                 "\t\t\texit\n"
169                 "\t\t--quiet\n",
170                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
171 }
172
173 /* ==================== Lustre config functions =============*/
174
175 void print_ldd(char *str, struct mkfs_opts *mop)
176 {
177         struct lustre_disk_data *ldd = &mop->mo_ldd;
178
179         printf("\n   %s:\n", str);
180         printf("Target:     %s\n", ldd->ldd_svname);
181         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
182                 printf("Index:      unassigned\n");
183         else
184                 printf("Index:      %d\n", ldd->ldd_svindex);
185         if (ldd->ldd_uuid[0])
186                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
187         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
188         printf("Mount type: %s\n", MT_STR(ldd));
189         printf("Flags:      %#x\n", ldd->ldd_flags);
190         printf("              (%s%s%s%s%s%s%s%s%s)\n",
191                IS_MDT(ldd) ? "MDT " : "",
192                IS_OST(ldd) ? "OST " : "",
193                IS_MGS(ldd) ? "MGS " : "",
194                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index " : "",
195                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time " : "",
196                ldd->ldd_flags & LDD_F_UPDATE     ? "update " : "",
197                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf " : "",
198                ldd->ldd_flags & LDD_F_NO_PRIMNODE ? "no_primnode " : "",
199                ldd->ldd_flags & LDD_F_NO_LOCAL_LOGS ? "nolocallogs " : "");
200         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
201         osd_print_ldd_params(mop);
202         if (ldd->ldd_userdata[0])
203                 printf("Comment: %s\n", ldd->ldd_userdata);
204         printf("\n");
205 }
206
207 void set_defaults(struct mkfs_opts *mop)
208 {
209         mop->mo_ldd.ldd_magic = LDD_MAGIC;
210         mop->mo_ldd.ldd_config_ver = 1;
211         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
212 #ifdef HAVE_LDISKFS_OSD
213         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
214 #else
215         mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
216 #endif
217         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
218         mop->mo_mgs_failnodes = 0;
219         mop->mo_stripe_count = 1;
220         mop->mo_pool_vdevs = NULL;
221 }
222
223 /* Make the mdt/ost server obd name based on the filesystem name */
224 static bool server_make_name(__u32 flags, __u16 index, const char *fs,
225                              char *name_buf, size_t name_buf_size)
226 {
227         bool invalid_flag = false;
228
229         if (flags & (LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_OST)) {
230                 char reg_flag = '-';
231
232                 if (flags & LDD_F_WRITECONF)
233                         reg_flag = '=';
234                 else if (flags & LDD_F_VIRGIN)
235                         reg_flag = ':';
236                 else if (flags & LDD_F_NO_LOCAL_LOGS)
237                         reg_flag = '+';
238
239
240                 if (!(flags & LDD_F_SV_ALL))
241                         snprintf(name_buf, name_buf_size, "%.8s%c%s%04x", fs,
242                                  reg_flag,
243                                  (flags & LDD_F_SV_TYPE_MDT) ? "MDT" : "OST",
244                                 index);
245         } else if (flags & LDD_F_SV_TYPE_MGS) {
246                 snprintf(name_buf, name_buf_size, "MGS");
247         } else {
248                 fprintf(stderr, "unknown server type %#x\n", flags);
249                 invalid_flag = true;
250         }
251         return invalid_flag;
252 }
253
254 static inline void badopt(const char *opt, char *type)
255 {
256         fprintf(stderr, "%s: '--%s' only valid for %s\n",
257                 progname, opt, type);
258         usage(stderr);
259 }
260
261 #ifdef TUNEFS
262 /**
263  * Removes all existing instances of the parameter passed in \a param,
264  * which are in the form of "key=<value>", from the buffer at \a buf.
265  *
266  * The parameter can be either in the form of "key" when passed by option
267  * "--erase-param", or in the form of "key=<value>" when passed by option
268  * "--param".
269  *
270  * \param buf     the buffer holding on-disk server parameters.
271  * \param param   the parameter whose instances are to be removed from \a buf.
272  * \param withval true means the parameter is in the form of "key=<value>"
273  *                false means the parameter is in the form of "key"
274  *
275  * \retval 0      success, parameter was erased,
276  * \retval 1      success, parameter was not found, don't need to do erase_ldd,
277  * \retval EINVAL failure, invalid input parameter.
278  */
279 static int erase_param(const char *const buf, const char *const param,
280                        bool withval)
281 {
282         char    search[PARAM_MAX + 8] = "";
283         char    *buffer = (char *)buf;
284         bool    found = false;
285
286         if (strlen(param) > PARAM_MAX) {
287                 fprintf(stderr, "%s: param to erase is too long-\n%s\n",
288                         progname, param);
289                 return EINVAL;
290         }
291
292         /* add_param() writes a space as the first character in ldd_params */
293         search[0] = ' ';
294
295         /* "key" or "key=<value>" */
296         if (withval) {
297                 char *keyend;
298
299                 keyend = strchr(param, '=');
300                 if (!keyend)
301                         return EINVAL;
302                 strncpy(&search[1], param, keyend - param + 1);
303         } else {
304                 snprintf(search + 1, sizeof(search) - 1, "%s=", param);
305         }
306
307         while (1) {
308                 char    *space;
309
310                 buffer = strstr(buffer, search);
311                 if (!buffer)
312                         return found == true ? 0 : 1;
313                 found = true;
314                 space = strchr(buffer + 1, ' ');
315                 if (space) {
316                         memmove(buffer, space, strlen(space) + 1);
317                 } else {
318                         *buffer = '\0';
319                         return 0;
320                 }
321         }
322 }
323 #endif
324
325 /* from mount_lustre */
326 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
327 #define MAXNIDSTR 1024
328 static char *convert_hostnames(char *s1)
329 {
330         char *converted, *s2 = 0, *c, *end, sep;
331         int left = MAXNIDSTR;
332         lnet_nid_t nid;
333
334         converted = malloc(left);
335         if (!converted)
336                 return NULL;
337
338         end = s1 + strlen(s1);
339         c = converted;
340         while ((left > 0) && (s1 < end)) {
341                 s2 = strpbrk(s1, ",:");
342                 if (!s2)
343                         s2 = end;
344                 sep = *s2;
345                 *s2 = '\0';
346                 nid = libcfs_str2nid(s1);
347                 *s2 = sep;
348
349                 if (nid == LNET_NID_ANY) {
350                         fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
351                                 progname, s1);
352                         free(converted);
353                         return NULL;
354                 }
355                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
356                             strlen("127.0.0.1")) == 0) {
357                         fprintf(stderr,
358                                 "%s: The NID '%s' resolves to the loopback address '%s'.  Lustre requires a non-loopback address.\n",
359                                 progname, s1, libcfs_nid2str(nid));
360                         free(converted);
361                         return NULL;
362                 }
363
364                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
365                 left = converted + MAXNIDSTR - c;
366                 s1 = s2 + 1;
367         }
368         return converted;
369 }
370
371 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
372                char **mountopts, char *old_fsname)
373 {
374         static struct option long_opts[] = {
375         { .val = 'B',   .name =  "backfs-mount-opts",
376                                                 .has_arg = required_argument},
377         { .val = 'f',   .name =  "failnode",    .has_arg = required_argument},
378         { .val = 'f',   .name =  "failover",    .has_arg = required_argument},
379         { .val = 'G',   .name =  "mgs",         .has_arg = no_argument},
380         { .val = 'h',   .name =  "help",        .has_arg = no_argument},
381         { .val = 'i',   .name =  "index",       .has_arg = required_argument},
382         { .val = 'L',   .name =  "fsname",      .has_arg = required_argument},
383         { .val = 'm',   .name =  "mgsnode",     .has_arg = required_argument},
384         { .val = 'm',   .name =  "mgsnid",      .has_arg = required_argument},
385         { .val = 'n',   .name =  "dryrun",      .has_arg = no_argument},
386         { .val = 'N',   .name =  "nomgs",       .has_arg = no_argument},
387         { .val = 'o',   .name =  "mountfsoptions",
388                                                 .has_arg = required_argument},
389         { .val = 'p',   .name =  "param",       .has_arg = required_argument},
390         { .val = 'q',   .name =  "quiet",       .has_arg = no_argument},
391         { .val = 's',   .name =  "servicenode", .has_arg = required_argument},
392         { .val = 't',   .name =  "network",     .has_arg = required_argument},
393         { .val = 'u',   .name =  "comment",     .has_arg = required_argument},
394         { .val = 'U',   .name =  "force-nohostid",
395                                                 .has_arg = no_argument},
396         { .val = 'v',   .name =  "verbose",     .has_arg = no_argument},
397         { .val = 'V',   .name =  "version",     .has_arg = no_argument},
398 #ifndef TUNEFS
399         { .val = 'b',   .name =  "backfstype",  .has_arg = required_argument},
400         { .val = 'c',   .name =  "stripe-count-hint",
401                                                 .has_arg = required_argument},
402         { .val = 'd',   .name =  "device-size", .has_arg = required_argument},
403         { .val = 'k',   .name =  "mkfsoptions", .has_arg = required_argument},
404         { .val = 'M',   .name =  "mdt",         .has_arg = no_argument},
405         { .val = 'O',   .name =  "ost",         .has_arg = no_argument},
406         { .val = 'r',   .name =  "reformat",    .has_arg = no_argument},
407         { .val = 'R',   .name =  "replace",     .has_arg = no_argument},
408 #else
409         { .val = 'E',   .name =  "erase-param", .has_arg = required_argument},
410         { .val = 'e',   .name =  "erase-params",
411                                                 .has_arg = no_argument},
412         { .val = 'l',   .name =  "nolocallogs", .has_arg = no_argument},
413         { .val = 'Q',   .name =  "quota",       .has_arg = no_argument},
414         { .val = 'R',   .name =  "rename",      .has_arg = optional_argument},
415         { .val = 'w',   .name =  "writeconf",   .has_arg = no_argument},
416 #endif
417         { .name = NULL } };
418         char *short_opts = "B:f:Ghi:L:m:nNo:p:qs:t:u:vV"
419 #ifndef TUNEFS
420                           "b:c:d:k:MOrR";
421 #else
422                           "E:elQR::w";
423 #endif
424         struct lustre_disk_data *ldd = &mop->mo_ldd;
425         char new_fsname[16] = { 0 };
426         int opt;
427         int rc, longidx;
428         int failnode_set = 0, servicenode_set = 0;
429         int replace = 0;
430         bool index_option = false;
431
432 #ifdef TUNEFS
433         /*
434          * For the right semantics, if '-e'/'--erase-params' is specified,
435          * it must be picked out and all old parameters should be erased
436          * before any other changes are done.
437          */
438         while ((opt = getopt_long(argc, argv, short_opts, long_opts,
439                                   &longidx)) != EOF) {
440                 switch (opt) {
441                 case 'e':
442                         ldd->ldd_params[0] = '\0';
443                         mop->mo_flags |= MO_ERASE_ALL;
444                         ldd->ldd_flags |= LDD_F_UPDATE;
445                         break;
446                 default:
447                         break;
448                 }
449                 if (mop->mo_flags & MO_ERASE_ALL)
450                         break;
451         }
452         optind = 0;
453 #endif
454         while ((opt = getopt_long(argc, argv, short_opts, long_opts,
455                                   &longidx)) != EOF) {
456                 switch (opt) {
457                 case 'B':
458                         mop->mo_mountopts = optarg;
459                         break;
460                 case 'f':
461                 case 's': {
462                         char *nids;
463
464                         if ((opt == 'f' && servicenode_set) ||
465                             (opt == 's' && failnode_set)) {
466                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
467                                         progname, long_opts[longidx].name,
468                                         opt == 'f' ? "servicenode" :
469                                         "failnode");
470                                 return 1;
471                         }
472
473                         nids = convert_hostnames(optarg);
474                         if (!nids)
475                                 return 1;
476
477                         rc = append_param(ldd->ldd_params, PARAM_FAILNODE,
478                                           nids, ':');
479                         free(nids);
480                         if (rc != 0)
481                                 return rc;
482
483                         /* Must update the mgs logs */
484                         ldd->ldd_flags |= LDD_F_UPDATE;
485                         if (opt == 'f') {
486                                 ldd->ldd_flags &= ~LDD_F_NO_PRIMNODE;
487                                 failnode_set = 1;
488                         } else {
489                                 ldd->ldd_flags |= LDD_F_NO_PRIMNODE;
490                                 servicenode_set = 1;
491                         }
492                         mop->mo_flags |= MO_FAILOVER;
493                         break;
494                 }
495                 case 'G':
496                         ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
497                         break;
498                 case 'h':
499                         usage(stdout);
500                         return 1;
501                 case 'i': {
502                         char *endptr = NULL;
503                         int base;
504
505                         index_option = true;
506                         /* LU-2374: check whether it is OST/MDT later */
507                         base = (strlen(optarg) > 1 &&
508                                 !strncmp(optarg, "0x", 2)) ? 16 : 10;
509                         /* Allowed input are base 16 and base 10 numbers only */
510                         mop->mo_ldd.ldd_svindex = strtoul(optarg,
511                                                           &endptr, base);
512                         if (*endptr != '\0') {
513                                 fprintf(stderr,
514                                         "%s: wrong index %s. Target index must be decimal or hexadecimal.\n",
515                                         progname, optarg);
516                                 return 1;
517                         }
518                         if (ldd->ldd_svindex >= INDEX_UNASSIGNED) {
519                                 fprintf(stderr,
520                                         "%s: wrong index %u. Target index must be less than %u.\n",
521                                         progname, ldd->ldd_svindex,
522                                         INDEX_UNASSIGNED);
523                                 return 1;
524                         }
525
526                         ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
527                         break;
528                 }
529                 case 'L': {
530                         const char *tmp;
531                         size_t len;
532
533                         len = strlen(optarg);
534                         if (len < 1 || len > LUSTRE_MAXFSNAME) {
535                                 fprintf(stderr,
536                                         "%s: filesystem name must be 1-%d chars\n",
537                                         progname, LUSTRE_MAXFSNAME);
538                                 return 1;
539                         }
540
541                         for (tmp = optarg; *tmp != '\0'; ++tmp) {
542                                 if (isalnum(*tmp) || *tmp == '_' || *tmp == '-')
543                                         continue;
544                                 else
545                                         break;
546                         }
547                         if (*tmp != '\0') {
548                                 fprintf(stderr,
549                                         "%s: char '%c' not allowed in filesystem name\n",
550                                         progname, *tmp);
551                                 return 1;
552                         }
553                         strscpy(new_fsname, optarg, sizeof(new_fsname));
554                         break;
555                 }
556                 case 'm': {
557                         char *nids = convert_hostnames(optarg);
558
559                         if (!nids)
560                                 return 1;
561
562                         rc = append_param(ldd->ldd_params, PARAM_MGSNODE,
563                                           nids, ':');
564                         free(nids);
565                         if (rc != 0)
566                                 return rc;
567
568                         mop->mo_mgs_failnodes++;
569                         break;
570                 }
571                 case 'n':
572                         print_only++;
573                         break;
574                 case 'N':
575                         ldd->ldd_flags &= ~LDD_F_SV_TYPE_MGS;
576                         break;
577                 case 'o':
578                         *mountopts = optarg;
579                         break;
580                 case 'p':
581 #ifdef TUNEFS
582                         /*
583                          * Removes all existing instances of the parameter
584                          * before adding new values.
585                          */
586                         rc = erase_param(ldd->ldd_params, optarg, true);
587                         if (rc > 1)
588                                 return rc;
589 #endif
590                         rc = add_param(ldd->ldd_params, NULL, optarg);
591                         if (rc != 0)
592                                 return rc;
593                         /* Must update the mgs logs */
594                         ldd->ldd_flags |= LDD_F_UPDATE;
595                         break;
596                 case 'q':
597                         verbose--;
598                         break;
599                 case 't':
600                         if (!IS_MDT(ldd) && !IS_OST(ldd)) {
601                                 badopt(long_opts[longidx].name, "MDT,OST");
602                                 return 1;
603                         }
604
605                         if (!optarg)
606                                 return 1;
607
608                         rc = add_param(ldd->ldd_params, PARAM_NETWORK, optarg);
609                         if (rc != 0)
610                                 return rc;
611
612                         /* Must update the mgs logs */
613                         ldd->ldd_flags |= LDD_F_UPDATE;
614                         break;
615                 case 'u':
616                         strscpy(ldd->ldd_userdata, optarg,
617                                 sizeof(ldd->ldd_userdata));
618                         break;
619                 case 'U':
620                         mop->mo_flags |= MO_NOHOSTID_CHECK;
621                         break;
622                 case 'v':
623                         verbose++;
624                         break;
625                 case 'V':
626                         ++version;
627                         fprintf(stdout, "%s %s\n", progname,
628                                 LUSTRE_VERSION_STRING);
629                         return 0;
630 #ifndef TUNEFS
631                 case 'b': {
632                         int i = 0;
633
634                         do {
635                                 if (strcmp(optarg, mt_str(i)) == 0) {
636                                         ldd->ldd_mount_type = i;
637                                         break;
638                                 }
639                         } while (++i < LDD_MT_LAST);
640
641                         if (i == LDD_MT_LAST) {
642                                 fprintf(stderr,
643                                         "%s: invalid backend filesystem type %s\n",
644                                         progname, optarg);
645                                 return 1;
646                         }
647                         break;
648                 }
649                 case 'c':
650                         if (IS_MDT(ldd)) {
651                                 int stripe_count = atol(optarg);
652
653                                 if (stripe_count <= 0) {
654                                         fprintf(stderr,
655                                                 "%s: bad stripe count %s\n",
656                                                 progname, optarg);
657                                         return 1;
658                                 }
659                                 mop->mo_stripe_count = stripe_count;
660                         } else {
661                                 badopt(long_opts[longidx].name, "MDT");
662                                 return 1;
663                         }
664                         break;
665                 case 'd':
666                         mop->mo_device_kb = atol(optarg);
667                         break;
668                 case 'k':
669                         strscpy(mop->mo_mkfsopts, optarg,
670                                 sizeof(mop->mo_mkfsopts));
671                         break;
672                 case 'M':
673                         ldd->ldd_flags |= LDD_F_SV_TYPE_MDT;
674                         break;
675                 case 'O':
676                         ldd->ldd_flags |= LDD_F_SV_TYPE_OST;
677                         break;
678                 case 'r':
679                         mop->mo_flags |= MO_FORCEFORMAT;
680                         break;
681                 case 'R':
682                         replace = 1;
683                         break;
684 #else /* TUNEFS */
685                 case 'E':
686                         rc = erase_param(ldd->ldd_params, optarg, false);
687                         /*
688                          * (rc == 1) means not found, so don't need to
689                          * call osd_erase_ldd().
690                          */
691                         if (rc > 1)
692                                 return rc;
693                         if (!rc) {
694                                 rc = osd_erase_ldd(mop, optarg);
695                                 if (rc)
696                                         return rc;
697                         }
698                         /* Must update the mgs logs */
699                         ldd->ldd_flags |= LDD_F_UPDATE;
700                         break;
701                 case 'e':
702                         /* Already done in the beginning */
703                         break;
704                 case 'Q':
705                         mop->mo_flags |= MO_QUOTA;
706                         break;
707                 case 'R': {
708                         char *tmp;
709
710                         mop->mo_flags |= MO_RENAME;
711                         if (!optarg) {
712                                 if (IS_SEPARATED_MGS(ldd)) {
713                                         fprintf(stderr,
714                                                 "%s: must specify the old fsname to be renamed for separated MGS\n",
715                                                 progname);
716                                         return 1;
717                                 }
718                                 break;
719                         }
720
721                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
722                                 fprintf(stderr,
723                                         "%s: filesystem name must be 1-8 chars\n",
724                                         progname);
725                                 return 1;
726                         }
727
728                         tmp = strpbrk(optarg, "/:");
729                         if (tmp) {
730                                 fprintf(stderr,
731                                         "%s: char '%c' not allowed in filesystem name\n",
732                                         progname, *tmp);
733                                 return 1;
734                         }
735
736                         if (IS_SEPARATED_MGS(ldd)) {
737                                 strscpy(old_fsname, optarg,
738                                         sizeof(ldd->ldd_fsname));
739                         } else if (strlen(old_fsname) != strlen(optarg) ||
740                                    strcmp(old_fsname, optarg) != 0) {
741                                 fprintf(stderr,
742                                         "%s: the given fsname '%s' to be renamed does not exist\n",
743                                         progname, optarg);
744                                 return 1;
745                         }
746                         break;
747                 }
748                 case 'w':
749                         ldd->ldd_flags |= LDD_F_WRITECONF;
750                         break;
751                 case 'l':
752                         if (ldd->ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF)) {
753                                 fprintf(stderr, "Can not apply nolocallogs to the target that was writeconfed or never been registered\n");
754                                 return EINVAL;
755                         }
756                         ldd->ldd_flags |= LDD_F_NO_LOCAL_LOGS;
757                         break;
758 #endif /* !TUNEFS */
759                 default:
760                         if (opt != '?') {
761                                 fatal();
762                                 fprintf(stderr, "Unknown option '%c'\n", opt);
763                         }
764                         return EINVAL;
765                 }
766         }
767
768         if (ldd->ldd_mount_type == LDD_MT_ZFS &&
769             (ldd->ldd_flags & LDD_F_SV_TYPE_OST)) {
770                 rc = add_param(ldd->ldd_params, PARAM_AUTODEGRADE, "on");
771                 if (rc)
772                         return rc;
773         }
774
775         if (strlen(new_fsname) > 0) {
776                 if (!(mop->mo_flags & (MO_FORCEFORMAT | MO_RENAME)) &&
777                     (!(ldd->ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF)))) {
778                         fprintf(stderr,
779                                 "%s: cannot change the name of a registered target\n",
780                                 progname);
781                         return 1;
782                 }
783
784                 strscpy(ldd->ldd_fsname, new_fsname, sizeof(ldd->ldd_fsname));
785         }
786
787         if (index_option && !(mop->mo_ldd.ldd_flags &
788                               (LDD_F_VIRGIN | LDD_F_WRITECONF))) {
789                 fprintf(stderr,
790                         "%s: cannot change the index of a registered target\n",
791                         progname);
792                 return 1;
793         }
794
795 #ifdef TUNEFS
796         if (mop->mo_flags & MO_RENAME) {
797                 if (new_fsname[0] == '\0') {
798                         fprintf(stderr,
799                                 "%s: need to specify new fsname for renaming case\n",
800                                 progname);
801                         return 1;
802                 }
803
804                 if (strcmp(old_fsname, new_fsname) == 0) {
805                         fprintf(stderr,
806                                 "%s: cannot rename fsname '%s' to the same name\n",
807                                 progname, old_fsname);
808                         return 1;
809                 }
810         }
811 #endif
812
813         /* Need to clear this flag after parsing 'L' and 'i' options. */
814         if (replace)
815                 ldd->ldd_flags &= ~LDD_F_VIRGIN;
816
817         if (optind == argc) {
818                 /* The user didn't specify device name */
819                 fatal();
820                 fprintf(stderr,
821                         "Not enough arguments - device name or pool/dataset name not specified.\n");
822                 return EINVAL;
823         }
824
825         /*  The device or pool/filesystem name */
826         strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
827
828         /* Followed by optional vdevs */
829         if (optind < argc - 1)
830                 mop->mo_pool_vdevs = (char **)&argv[optind + 1];
831
832         return 0;
833 }
834
835 /*
836  * This function checks args for sanity.
837  *
838  * Returns 0 if all is found good.
839  * Returns 1 if invalid args is detected
840  */
841 int chk_args(int argc, char *const argv[])
842 {
843         /* If no argument is given to mkfs.lustre, bail out */
844         if (argc < 2)
845                 return 1;
846
847         /* If argc is exactly 2, make sure the user wants "--version"
848          * or "-V" and handle correctly if that is what the user wants
849          * without bailing out with error.
850          */
851         if (argc == 2) {
852                 if (strncmp(argv[1], "--version", strlen(argv[1])) == 0 &&
853                     (strlen(argv[1]) == 9))
854                         return 0;
855
856                 if (strncmp(argv[1], "-V", strlen(argv[1])) == 0 &&
857                     (strlen(argv[1]) == 2))
858                         return 0;
859         }
860
861         /* This check validates if the last argument is really a device name
862          * and does not start with char '-'.
863          */
864         if ((argv[argc - 1][0] == '-'))
865                 return 1;
866         else
867                 return 0;
868 }
869
870 int main(int argc, char *const argv[])
871 {
872         struct mkfs_opts mop;
873         struct lustre_disk_data *ldd = &mop.mo_ldd;
874         char *mountopts = NULL;
875         char wanted_mountopts[512] = "";
876         char old_fsname[16] = "";
877         unsigned int mount_type;
878         int ret = 0;
879         int ret2 = 0;
880
881         progname = strrchr(argv[0], '/');
882         if (progname)
883                 progname++;
884         else
885                 progname = argv[0];
886
887         if (chk_args(argc, argv)) {
888                 usage(stderr);
889                 return EINVAL;
890         }
891
892         memset(&mop, 0, sizeof(mop));
893         set_defaults(&mop);
894
895         /* device is last arg */
896         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
897
898         ret = osd_init();
899         if (ret != 0) {
900                 fprintf(stderr, "%s: osd_init() failed: %d (%s)\n",
901                         progname, ret, strerror(ret));
902                 return ret;
903         }
904
905 #ifdef TUNEFS
906         /*
907          * For tunefs, we must read in the old values before parsing any
908          * new ones.
909          */
910
911         /* Check whether the disk has already been formatted by mkfs.lustre */
912         ret = osd_is_lustre(mop.mo_device, &mount_type);
913         if (ret == 0) {
914                 fatal();
915                 fprintf(stderr,
916                         "Device %s has not been formatted with mkfs.lustre\n",
917                         mop.mo_device);
918                 ret = ENODEV;
919                 goto out;
920         }
921         ldd->ldd_mount_type = mount_type;
922
923         ret = osd_read_ldd(mop.mo_device, ldd);
924         if (ret != 0) {
925                 fatal();
926                 fprintf(stderr,
927                         "Failed to read previous Lustre data from %s (%d)\n",
928                         mop.mo_device, ret);
929                 goto out;
930         }
931
932         strscpy(old_fsname, ldd->ldd_fsname, sizeof(ldd->ldd_fsname));
933         ldd->ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN |
934                             LDD_F_NO_LOCAL_LOGS);
935
936         /* svname of the form lustre:OST1234 means never registered */
937         ret = strlen(ldd->ldd_svname);
938         if (ldd->ldd_svname[ret - 8] == ':') {
939                 ldd->ldd_svname[ret - 8] = '-';
940                 ldd->ldd_flags |= LDD_F_VIRGIN;
941         } else if (ldd->ldd_svname[ret - 8] == '=') {
942                 ldd->ldd_svname[ret - 8] = '-';
943                 ldd->ldd_flags |= LDD_F_WRITECONF;
944         } else if (ldd->ldd_svname[ret - 8] == '+') {
945                 ldd->ldd_svname[ret - 8] = '-';
946                 ldd->ldd_flags |= LDD_F_NO_LOCAL_LOGS;
947         }
948
949         if (strstr(ldd->ldd_params, PARAM_MGSNODE))
950                 mop.mo_mgs_failnodes++;
951
952         if (verbose > 0)
953                 print_ldd("Read previous values", &mop);
954 #endif /* TUNEFS */
955
956         ret = parse_opts(argc, argv, &mop, &mountopts, old_fsname);
957         if (ret != 0 || version)
958                 goto out;
959
960         if (!IS_MDT(ldd) && !IS_OST(ldd) && !IS_MGS(ldd)) {
961                 fatal();
962                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
963                 ret = EINVAL;
964                 goto out;
965         }
966
967         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
968                 fatal();
969                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
970                 ret = EINVAL;
971                 goto out;
972         }
973
974         /* Stand alone MGS doesn't need an index */
975         if (!IS_MDT(ldd) && IS_MGS(ldd)) {
976 #ifndef TUNEFS
977                 /* But if --index was specified flag an error */
978                 if (!(ldd->ldd_flags & LDD_F_NEED_INDEX)) {
979                         badopt("index", "MDT,OST");
980                         goto out;
981                 }
982 #endif
983                 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
984         }
985
986         if (ldd->ldd_flags & LDD_F_NEED_INDEX)
987                 fprintf(stderr,
988                         "warning: %s: for Lustre 2.4 and later, the target index must be specified with --index\n",
989                         mop.mo_device);
990
991         /* If no index is supplied for MDT by default set index to zero */
992         if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
993                 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
994                 ldd->ldd_svindex = 0;
995         }
996 #ifndef TUNEFS
997         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
998 #else
999         /*
1000          * Don't check --mgs or --mgsnode if print_only is set or
1001          * --erase-params is set.
1002          */
1003         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0) && !print_only &&
1004             !(mop.mo_flags & MO_ERASE_ALL)) {
1005 #endif
1006                 fatal();
1007                 if (IS_MDT(ldd))
1008                         fprintf(stderr, "Must specify --mgs or --mgsnode\n");
1009                 else
1010                         fprintf(stderr, "Must specify --mgsnode\n");
1011                 ret = EINVAL;
1012                 goto out;
1013         }
1014         if ((IS_MDT(ldd) || IS_OST(ldd)) && ldd->ldd_fsname[0] == '\0') {
1015                 fatal();
1016                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
1017                 ret = EINVAL;
1018                 goto out;
1019         }
1020
1021         /* These are the permanent mount options (always included) */
1022         ret = osd_prepare_lustre(&mop,
1023                                  wanted_mountopts, sizeof(wanted_mountopts));
1024         if (ret != 0) {
1025                 fatal();
1026                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
1027                 goto out;
1028         }
1029
1030         if (mountopts) {
1031                 trim_mountfsoptions(mountopts);
1032                 if (check_mountfsoptions(mountopts, wanted_mountopts)) {
1033                         ret = EINVAL;
1034                         goto out;
1035                 }
1036                 snprintf(ldd->ldd_mount_opts, sizeof(ldd->ldd_mount_opts),
1037                          "%s", mountopts);
1038         } else {
1039 #ifdef TUNEFS
1040                 if (ldd->ldd_mount_opts[0] == 0)
1041                 /* use the defaults unless old opts exist */
1042 #endif
1043                 {
1044                         snprintf(ldd->ldd_mount_opts,
1045                                  sizeof(ldd->ldd_mount_opts),
1046                                  "%s", wanted_mountopts);
1047                         trim_mountfsoptions(ldd->ldd_mount_opts);
1048                 }
1049         }
1050
1051         ret = osd_fix_mountopts(&mop, ldd->ldd_mount_opts,
1052                                 sizeof(ldd->ldd_mount_opts));
1053         if (ret != 0) {
1054                 fatal();
1055                 fprintf(stderr, "unable to fix mountfsoptions (%d)\n", ret);
1056                 goto out;
1057         }
1058
1059         if (server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1060                              ldd->ldd_fsname, ldd->ldd_svname,
1061                              sizeof(ldd->ldd_svname))) {
1062                 printf("unknown server type %#x\n", ldd->ldd_flags);
1063                 goto out;
1064         }
1065
1066         if (verbose >= 0)
1067                 print_ldd("Permanent disk data", &mop);
1068
1069         if (print_only) {
1070                 printf("exiting before disk write.\n");
1071                 goto out;
1072         }
1073
1074         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL)) {
1075                 fprintf(stderr, "%s: is currently mounted, exiting without any change\n",
1076                         mop.mo_device);
1077                 return EEXIST;
1078         }
1079
1080         /* Create the loopback file */
1081         if (mop.mo_flags & MO_IS_LOOP) {
1082                 ret = access(mop.mo_device, F_OK);
1083                 if (ret != 0)
1084                         ret = errno;
1085
1086 #ifndef TUNEFS
1087                 /* Reformat the loopback file */
1088                 if (ret != 0 || (mop.mo_flags & MO_FORCEFORMAT)) {
1089                         ret = loop_format(&mop);
1090                         if (ret != 0)
1091                                 goto out;
1092                 }
1093 #endif
1094                 if (ret == 0)
1095                         ret = loop_setup(&mop);
1096                 if (ret != 0) {
1097                         fatal();
1098                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
1099                                 mop.mo_device, strerror(ret));
1100                         goto out;
1101                 }
1102         }
1103
1104 #ifndef TUNEFS
1105         /* Check whether the disk has already been formatted by mkfs.lustre */
1106         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1107                 ret = osd_is_lustre(mop.mo_device, &mount_type);
1108                 if (ret != 0) {
1109                         fatal();
1110                         fprintf(stderr,
1111                                 "Device %s was previously formatted for lustre. Use --reformat to reformat it, or tunefs.lustre to modify.\n",
1112                                 mop.mo_device);
1113                         goto out;
1114                 }
1115         }
1116
1117         /* Format the backing filesystem */
1118         ret = osd_make_lustre(&mop);
1119         if (ret != 0) {
1120                 fatal();
1121                 fprintf(stderr, "mkfs failed %d\n", ret);
1122                 goto out;
1123         }
1124 #else /* TUNEFS */
1125         /* update svname with '=' to refresh config */
1126         if (ldd->ldd_flags & LDD_F_WRITECONF) {
1127                 struct mount_opts opts;
1128
1129                 opts.mo_ldd = *ldd;
1130                 opts.mo_source = mop.mo_device;
1131                 (void)osd_label_lustre(&opts);
1132         }
1133
1134         /* update svname with '+' to force remote logs */
1135         if (ldd->ldd_flags & LDD_F_NO_LOCAL_LOGS) {
1136                 struct mount_opts opts;
1137
1138                 opts.mo_ldd = *ldd;
1139                 opts.mo_source = mop.mo_device;
1140                 (void) osd_label_lustre(&opts);
1141         }
1142
1143         /* Rename filesystem fsname */
1144         if (mop.mo_flags & MO_RENAME) {
1145                 ret = osd_rename_fsname(&mop, old_fsname);
1146                 if (ret)
1147                         goto out;
1148         }
1149
1150         /* Enable quota accounting */
1151         if (mop.mo_flags & MO_QUOTA) {
1152                 ret = osd_enable_quota(&mop);
1153                 goto out;
1154         }
1155 #endif /* !TUNEFS */
1156
1157         /* Write our config files */
1158         ret = osd_write_ldd(&mop);
1159         if (ret != 0) {
1160                 fatal();
1161                 fprintf(stderr, "failed to write local files\n");
1162                 goto out;
1163         }
1164 out:
1165         osd_fini();
1166         ret2 = loop_cleanup(&mop);
1167         if (ret == 0)
1168                 ret = ret2;
1169
1170         /* Fix any crazy return values from system() */
1171         if (ret != 0 && ((ret & 255) == 0))
1172                 return 1;
1173
1174         if (ret != 0)
1175                 verrprint("%s: exiting with %d (%s)\n",
1176                           progname, ret, strerror(ret));
1177         return ret;
1178 }