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