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