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