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