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