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