Whamcloud - gitweb
LU-5162 mdc: Add exception entry check for radix_tree
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/mkfs_lustre.c
37  *
38  * Author: Nathan Rutman <nathan@clusterfs.com>
39 */
40
41 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
42
43 #if HAVE_CONFIG_H
44 #  include "config.h"
45 #endif /* HAVE_CONFIG_H */
46
47 #ifndef _GNU_SOURCE
48 #define _GNU_SOURCE
49 #endif
50 #include "mount_utils.h"
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <stdarg.h>
56 #include <mntent.h>
57
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/mount.h>
61 #include <sys/utsname.h>
62
63 #include <string.h>
64 #include <getopt.h>
65 #include <limits.h>
66 #include <ctype.h>
67
68 #include <lustre_disk.h>
69 #include <lustre_param.h>
70 #include <lnet/lnetctl.h>
71 #include <lustre_ver.h>
72
73 #ifndef PATH_MAX
74 #define PATH_MAX 4096
75 #endif
76
77 char *progname;
78 int verbose = 1;
79 static int print_only = 0;
80
81 #ifdef HAVE_LDISKFS_OSD
82 #define FSLIST_LDISKFS "ldiskfs"
83 #define HAVE_FSLIST
84 #else
85  #define FSLIST_LDISKFS ""
86 #endif /* HAVE_LDISKFS_OSD */
87 #ifdef HAVE_ZFS_OSD
88  #ifdef HAVE_FSLIST
89    #define FSLIST_ZFS "|zfs"
90  #else
91   #define FSLIST_ZFS "zfs"
92   #define HAVE_FSLIST
93  #endif
94 #else
95  #define FSLIST_ZFS ""
96 #endif /* HAVE_ZFS_OSD */
97
98 #ifndef HAVE_FSLIST
99  #error "no backing OSD types (ldiskfs or ZFS) are configured"
100 #endif
101
102 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
103
104 void usage(FILE *out)
105 {
106         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
107         fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
108                 "--fsname=<filesystem name>\n"
109                 "\t--index=<target index> [options] <device>\n", progname);
110 #ifdef HAVE_ZFS_OSD
111         fprintf(out, "usage: %s <target type> --backfstype=zfs "
112                 "--fsname=<filesystem name> [options]\n"
113                 "\t<pool name>/<dataset name>\n"
114                 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
115                 progname);
116 #endif
117         fprintf(out,
118                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
119 #ifdef HAVE_ZFS_OSD
120                 "\t<pool name>: name of ZFS pool where target is created "
121                         "(e.g. tank)\n"
122                 "\t<dataset name>: name of new dataset, must be unique within "
123                         "pool (e.g. ost1)\n"
124                 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
125                         "cache, log)\n"
126 #endif
127                 "\n"
128                 "\ttarget types:\n"
129                 "\t\t--mgs: configuration management service\n"
130                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
131                 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
132                 "\toptions (in order of popularity):\n"
133                 "\t\t--index=#N: numerical target index (0..N)\n"
134                 "\t\t\trequired for all targets other than the MGS\n"
135                 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
136                 "\t\t\trequired for all targets other than MGS\n"
137                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
138                 "\t\t\trequired for all targets other than MGS\n"
139                 "\t\t--mountfsoptions=<opts>: permanent mount options\n"
140                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
141                 "\t\t\tmutually exclusive with --servicenode\n"
142                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
143                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
144                         "with --failnode\n"
145                 "\t\t--param <key>=<value>: set a permanent parameter\n"
146                 "\t\t\te.g. --param sys.timeout=40\n"
147                 "\t\t\t     --param lov.stripesize=2M\n"
148                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
149 #ifndef TUNEFS
150                 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
151                 "\t\t--device-size=#N(KB): device size for loop devices\n"
152                 "\t\t--mkfsoptions=<opts>: format options\n"
153                 "\t\t--reformat: overwrite an existing disk\n"
154                 "\t\t--replace: replace an old target with the same index\n"
155                 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
156 #else
157                 "\t\t--erase-params: erase all old parameter settings\n"
158                 "\t\t--nomgs: turn off MGS service on this MDT\n"
159                 "\t\t--writeconf: erase all config logs for this fs.\n"
160                 "\t\t--quota: enable space accounting on old 2.x device.\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--quiet\n",
166                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
167         return;
168 }
169
170 /* ==================== Lustre config functions =============*/
171
172 void print_ldd(char *str, struct lustre_disk_data *ldd)
173 {
174         printf("\n   %s:\n", str);
175         printf("Target:     %s\n", ldd->ldd_svname);
176         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
177                 printf("Index:      unassigned\n");
178         else
179                 printf("Index:      %d\n", ldd->ldd_svindex);
180         if (ldd->ldd_uuid[0])
181                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
182         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
183         printf("Mount type: %s\n", MT_STR(ldd));
184         printf("Flags:      %#x\n", ldd->ldd_flags);
185         printf("              (%s%s%s%s%s%s%s%s%s)\n",
186                IS_MDT(ldd) ? "MDT ":"",
187                IS_OST(ldd) ? "OST ":"",
188                IS_MGS(ldd) ? "MGS ":"",
189                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
190                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
191                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
192                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
193                ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
194                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
195         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
196         printf("Parameters:%s\n", ldd->ldd_params);
197         if (ldd->ldd_userdata[0])
198                 printf("Comment: %s\n", ldd->ldd_userdata);
199         printf("\n");
200 }
201
202 void set_defaults(struct mkfs_opts *mop)
203 {
204         mop->mo_ldd.ldd_magic = LDD_MAGIC;
205         mop->mo_ldd.ldd_config_ver = 1;
206         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
207 #ifdef HAVE_LDISKFS_OSD
208         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
209 #else
210         mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
211 #endif
212         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
213         mop->mo_mgs_failnodes = 0;
214         mop->mo_stripe_count = 1;
215         mop->mo_pool_vdevs = NULL;
216 }
217
218 static inline void badopt(const char *opt, char *type)
219 {
220         fprintf(stderr, "%s: '--%s' only valid for %s\n",
221                 progname, opt, type);
222         usage(stderr);
223 }
224
225 /* from mount_lustre */
226 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
227 #define MAXNIDSTR 1024
228 static char *convert_hostnames(char *s1)
229 {
230         char *converted, *s2 = 0, *c, *end, sep;
231         int left = MAXNIDSTR;
232         lnet_nid_t nid;
233
234         converted = malloc(left);
235         if (converted == NULL) {
236                 return NULL;
237         }
238
239         end = s1 + strlen(s1);
240         c = converted;
241         while ((left > 0) && (s1 < end)) {
242                 s2 = strpbrk(s1, ",:");
243                 if (!s2)
244                         s2 = end;
245                 sep = *s2;
246                 *s2 = '\0';
247                 nid = libcfs_str2nid(s1);
248                 *s2 = sep;
249
250                 if (nid == LNET_NID_ANY) {
251                         fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
252                                 progname, s1);
253                         free(converted);
254                         return NULL;
255                 }
256                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
257                             strlen("127.0.0.1")) == 0) {
258                         fprintf(stderr, "%s: The NID '%s' resolves to the "
259                                 "loopback address '%s'.  Lustre requires a "
260                                 "non-loopback address.\n",
261                                 progname, s1, libcfs_nid2str(nid));
262                         free(converted);
263                         return NULL;
264                 }
265
266                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
267                 left = converted + MAXNIDSTR - c;
268                 s1 = s2 + 1;
269         }
270         return converted;
271 }
272
273 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
274                char **mountopts)
275 {
276         static struct option long_opt[] = {
277                 { "backfstype",         required_argument,      NULL, 'b' },
278                 { "stripe-count-hint",  required_argument,      NULL, 'c' },
279                 { "comment",            required_argument,      NULL, 'u' },
280                 { "configdev",          required_argument,      NULL, 'C' },
281                 { "device-size",        required_argument,      NULL, 'd' },
282                 { "dryrun",             no_argument,            NULL, 'n' },
283                 { "erase-params",       no_argument,            NULL, 'e' },
284                 { "failnode",           required_argument,      NULL, 'f' },
285                 { "failover",           required_argument,      NULL, 'f' },
286                 { "mgs",                no_argument,            NULL, 'G' },
287                 { "help",               no_argument,            NULL, 'h' },
288                 { "index",              required_argument,      NULL, 'i' },
289                 { "mkfsoptions",        required_argument,      NULL, 'k' },
290                 { "mgsnode",            required_argument,      NULL, 'm' },
291                 { "mgsnid",             required_argument,      NULL, 'm' },
292                 { "mdt",                no_argument,            NULL, 'M' },
293                 { "fsname",             required_argument,      NULL, 'L' },
294                 { "noformat",           no_argument,            NULL, 'n' },
295                 { "nomgs",              no_argument,            NULL, 'N' },
296                 { "mountfsoptions",     required_argument,      NULL, 'o' },
297                 { "ost",                no_argument,            NULL, 'O' },
298                 { "param",              required_argument,      NULL, 'p' },
299                 { "print",              no_argument,            NULL, 'n' },
300                 { "quiet",              no_argument,            NULL, 'q' },
301                 { "quota",              no_argument,            NULL, 'Q' },
302                 { "reformat",           no_argument,            NULL, 'r' },
303                 { "replace",            no_argument,            NULL, 'R' },
304                 { "servicenode",        required_argument,      NULL, 's' },
305                 { "network",            required_argument,      NULL, 't' },
306                 { "verbose",            no_argument,            NULL, 'v' },
307                 { "writeconf",          no_argument,            NULL, 'w' },
308                 { 0,                    0,                      NULL,  0  }
309         };
310         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:PqrRs:t:Uu:vw";
311         int opt;
312         int rc, longidx;
313         int failnode_set = 0, servicenode_set = 0;
314         int replace = 0;
315
316         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
317                EOF) {
318                 switch (opt) {
319                 case 'b': {
320                         int i = 0;
321                         while (i < LDD_MT_LAST) {
322                                 if (strcmp(optarg, mt_str(i)) == 0) {
323                                         mop->mo_ldd.ldd_mount_type = i;
324                                         break;
325                                 }
326                                 i++;
327                         }
328                         if (i == LDD_MT_LAST) {
329                                 fprintf(stderr, "%s: invalid backend filesystem"
330                                         " type %s\n", progname, optarg);
331                                 return 1;
332                         }
333                         break;
334                 }
335                 case 'c':
336                         if (IS_MDT(&mop->mo_ldd)) {
337                                 int stripe_count = atol(optarg);
338                                 if (stripe_count <= 0) {
339                                         fprintf(stderr, "%s: bad stripe count "
340                                                 "%d\n", progname, stripe_count);
341                                         return 1;
342                                 }
343                                 mop->mo_stripe_count = stripe_count;
344                         } else {
345                                 badopt(long_opt[longidx].name, "MDT");
346                                 return 1;
347                         }
348                         break;
349                 case 'C': /* Configdev */
350                         //FIXME
351                         printf("Configdev not implemented\n");
352                         return 1;
353                 case 'd':
354                         mop->mo_device_kb = atol(optarg);
355                         break;
356                 case 'e':
357                         mop->mo_ldd.ldd_params[0] = '\0';
358                         /* Must update the mgs logs */
359                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
360                         break;
361                 case 'f':
362                 case 's': {
363                         char *nids;
364
365                         if ((opt == 'f' && servicenode_set)
366                             || (opt == 's' && failnode_set)) {
367                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
368                                         progname, long_opt[longidx].name,
369                                         opt == 'f' ? "servicenode" : "failnode");
370                                 return 1;
371                         }
372
373                         nids = convert_hostnames(optarg);
374                         if (!nids)
375                                 return 1;
376                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
377                                        nids);
378                         free(nids);
379                         if (rc)
380                                 return rc;
381                         /* Must update the mgs logs */
382                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
383                         if (opt == 'f') {
384                                 failnode_set = 1;
385                         } else {
386                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
387                                 servicenode_set = 1;
388                         }
389                         mop->mo_flags |= MO_FAILOVER;
390                         break;
391                 }
392                 case 'G':
393                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
394                         break;
395                 case 'h':
396                         usage(stdout);
397                         return 1;
398                 case 'i':
399                         if (!(mop->mo_ldd.ldd_flags &
400                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
401                                LDD_F_WRITECONF))) {
402                                 fprintf(stderr, "%s: cannot change the index of"
403                                         " a registered target\n", progname);
404                                 return 1;
405                         }
406                         /* LU-2374: check whether it is OST/MDT later */
407                         mop->mo_ldd.ldd_svindex = atol(optarg);
408                         mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
409                         break;
410                 case 'k':
411                         strscpy(mop->mo_mkfsopts, optarg,
412                                 sizeof(mop->mo_mkfsopts));
413                         break;
414                 case 'L': {
415                         char *tmp;
416                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
417                             (!(mop->mo_ldd.ldd_flags &
418                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
419                                 LDD_F_WRITECONF)))) {
420                                 fprintf(stderr, "%s: cannot change the name of"
421                                         " a registered target\n", progname);
422                                 return 1;
423                         }
424                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
425                                 fprintf(stderr, "%s: filesystem name must be "
426                                         "1-8 chars\n", progname);
427                                 return 1;
428                         }
429                         if ((tmp = strpbrk(optarg, "/:"))) {
430                                 fprintf(stderr, "%s: char '%c' not allowed in "
431                                         "filesystem name\n", progname, *tmp);
432                                 return 1;
433                         }
434                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
435                                 sizeof(mop->mo_ldd.ldd_fsname));
436                         break;
437                 }
438                 case 'm': {
439                         char *nids = convert_hostnames(optarg);
440                         if (!nids)
441                                 return 1;
442                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
443                                        nids);
444                         free(nids);
445                         if (rc)
446                                 return rc;
447                         mop->mo_mgs_failnodes++;
448                         break;
449                 }
450                 case 'M':
451                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
452                         break;
453                 case 'n':
454                         print_only++;
455                         break;
456                 case 'N':
457                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
458                         break;
459                 case 'o':
460                         *mountopts = optarg;
461                         break;
462                 case 'O':
463                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
464                         break;
465                 case 'p':
466                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
467                         if (rc)
468                                 return rc;
469                         /* Must update the mgs logs */
470                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
471                         break;
472                 case 'q':
473                         verbose--;
474                         break;
475                 case 'r':
476                         mop->mo_flags |= MO_FORCEFORMAT;
477                         break;
478                 case 'R':
479                         replace = 1;
480                         break;
481                 case 't':
482                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
483                                 badopt(long_opt[longidx].name, "MDT,OST");
484                                 return 1;
485                         }
486
487                         if (!optarg)
488                                 return 1;
489
490                         rc = add_param(mop->mo_ldd.ldd_params,
491                                        PARAM_NETWORK, optarg);
492                         if (rc != 0)
493                                 return rc;
494                         /* Must update the mgs logs */
495                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
496                         break;
497                 case 'u':
498                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
499                                 sizeof(mop->mo_ldd.ldd_userdata));
500                         break;
501                 case 'v':
502                         verbose++;
503                         break;
504                 case 'w':
505                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
506                         break;
507                 case 'Q':
508                         mop->mo_flags |= MO_QUOTA;
509                         break;
510                 default:
511                         if (opt != '?') {
512                                 fatal();
513                                 fprintf(stderr, "Unknown option '%c'\n", opt);
514                         }
515                         return EINVAL;
516                 }
517         }//while
518
519         /* Need to clear this flag after parsing 'L' and 'i' options. */
520         if (replace)
521                 mop->mo_ldd.ldd_flags &= ~LDD_F_VIRGIN;
522
523         if (optind == argc) {
524                 /* The user didn't specify device name */
525                 fatal();
526                 fprintf(stderr, "Not enough arguments - device name or "
527                         "pool/dataset name not specified.\n");
528                 return EINVAL;
529         } else {
530                 /*  The device or pool/filesystem name */
531                 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
532
533                 /* Followed by optional vdevs */
534                 if (optind < argc - 1)
535                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
536         }
537
538         return 0;
539 }
540
541 int main(int argc, char *const argv[])
542 {
543         struct mkfs_opts mop;
544         struct lustre_disk_data *ldd;
545         char *mountopts = NULL;
546         char always_mountopts[512] = "";
547         char default_mountopts[512] = "";
548         unsigned mount_type;
549         int ret = 0;
550         int ret2 = 0;
551
552         if ((progname = strrchr(argv[0], '/')) != NULL)
553                 progname++;
554         else
555                 progname = argv[0];
556
557         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
558                 usage(stderr);
559                 return(EINVAL);
560         }
561
562         memset(&mop, 0, sizeof(mop));
563         set_defaults(&mop);
564
565         /* device is last arg */
566         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
567
568         ret = osd_init();
569         if (ret)
570                 return ret;
571
572 #ifdef TUNEFS
573         /* For tunefs, we must read in the old values before parsing any
574            new ones. */
575
576         /* Check whether the disk has already been formatted by mkfs.lustre */
577         ret = osd_is_lustre(mop.mo_device, &mount_type);
578         if (ret == 0) {
579                 fatal();
580                 fprintf(stderr, "Device %s has not been formatted with "
581                         "mkfs.lustre\n", mop.mo_device);
582                 ret = ENODEV;
583                 goto out;
584         }
585         mop.mo_ldd.ldd_mount_type = mount_type;
586
587         ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
588         if (ret) {
589                 fatal();
590                 fprintf(stderr, "Failed to read previous Lustre data from %s "
591                         "(%d)\n", mop.mo_device, ret);
592                 goto out;
593         }
594         mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
595
596         /* svname of the form lustre:OST1234 means never registered */
597         ret = strlen(mop.mo_ldd.ldd_svname);
598         if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
599                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
600                 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
601         } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
602                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
603                 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
604         }
605
606         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
607             mop.mo_mgs_failnodes++;
608
609         if (verbose > 0)
610                 print_ldd("Read previous values", &(mop.mo_ldd));
611 #endif
612
613         ret = parse_opts(argc, argv, &mop, &mountopts);
614         if (ret)
615                 goto out;
616
617         ldd = &mop.mo_ldd;
618
619         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
620                 fatal();
621                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
622                 ret = EINVAL;
623                 goto out;
624         }
625
626         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
627                 fatal();
628                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
629                 ret = EINVAL;
630                 goto out;
631         }
632
633         /* Stand alone MGS doesn't need a index */
634         if (!IS_MDT(ldd) && IS_MGS(ldd)) {
635 #ifndef TUNEFS /* mkfs.lustre */
636                 /* But if --index was specified flag an error */
637                 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
638                         badopt("index", "MDT,OST");
639                         goto out;
640                 }
641 #endif
642                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
643         }
644
645         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
646             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
647                 fatal();
648                 fprintf(stderr, "Can't find the target index, "
649                         "specify with --index\n");
650                 ret = EINVAL;
651                 goto out;
652         }
653
654         if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
655                 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
656                         "target index must be specified with --index\n",
657                         mop.mo_device);
658
659         /* If no index is supplied for MDT by default set index to zero */
660         if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
661                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
662                 mop.mo_ldd.ldd_svindex = 0;
663         }
664         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
665                 fatal();
666                 if (IS_MDT(ldd))
667                         fprintf(stderr, "Must specify --mgs or --mgsnode\n");
668                 else
669                         fprintf(stderr, "Must specify --mgsnode\n");
670                 ret = EINVAL;
671                 goto out;
672         }
673         if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
674                 fatal();
675                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
676                 ret = EINVAL;
677                 goto out;
678         }
679
680         /* These are the permanent mount options (always included) */
681         ret = osd_prepare_lustre(&mop,
682                                  default_mountopts, sizeof(default_mountopts),
683                                  always_mountopts, sizeof(always_mountopts));
684         if (ret) {
685                 fatal();
686                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
687                 goto out;
688         }
689
690         if (mountopts) {
691                 trim_mountfsoptions(mountopts);
692                 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
693                 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
694                         ret = EINVAL;
695                         goto out;
696                 }
697                 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
698         } else {
699 #ifdef TUNEFS
700                 if (ldd->ldd_mount_opts[0] == 0)
701                         /* use the defaults unless old opts exist */
702 #endif
703                 {
704                         sprintf(ldd->ldd_mount_opts, "%s%s",
705                                 always_mountopts, default_mountopts);
706                         trim_mountfsoptions(ldd->ldd_mount_opts);
707                 }
708         }
709
710         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
711                          ldd->ldd_fsname, ldd->ldd_svname);
712
713         if (verbose >= 0)
714                 print_ldd("Permanent disk data", ldd);
715
716         if (print_only) {
717                 printf("exiting before disk write.\n");
718                 goto out;
719         }
720
721         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
722                 return(EEXIST);
723
724         /* Create the loopback file */
725         if (mop.mo_flags & MO_IS_LOOP) {
726                 ret = access(mop.mo_device, F_OK);
727                 if (ret)
728                         ret = errno;
729 #ifndef TUNEFS /* mkfs.lustre */
730                 /* Reformat the loopback file */
731                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
732                         ret = loop_format(&mop);
733                         if (ret)
734                                 goto out;
735                 }
736 #endif
737                 if (ret == 0)
738                         ret = loop_setup(&mop);
739                 if (ret) {
740                         fatal();
741                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
742                                 mop.mo_device, strerror(ret));
743                         goto out;
744                 }
745         }
746
747 #ifndef TUNEFS /* mkfs.lustre */
748         /* Check whether the disk has already been formatted by mkfs.lustre */
749         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
750                 ret = osd_is_lustre(mop.mo_device, &mount_type);
751                 if (ret) {
752                         fatal();
753                         fprintf(stderr, "Device %s was previously formatted "
754                                 "for lustre. Use --reformat to reformat it, "
755                                 "or tunefs.lustre to modify.\n",
756                                 mop.mo_device);
757                         goto out;
758                 }
759         }
760
761         /* Format the backing filesystem */
762         ret = osd_make_lustre(&mop);
763         if (ret != 0) {
764                 fatal();
765                 fprintf(stderr, "mkfs failed %d\n", ret);
766                 goto out;
767         }
768 #else
769         /* update svname with '=' to refresh config */
770         if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
771                 struct mount_opts opts;
772                 opts.mo_ldd = mop.mo_ldd;
773                 opts.mo_source = mop.mo_device;
774                 (void) osd_label_lustre(&opts);
775         }
776
777         /* Enable quota accounting */
778         if (mop.mo_flags & MO_QUOTA) {
779                 ret = osd_enable_quota(&mop);
780                 goto out;
781         }
782
783 #endif
784
785         /* Write our config files */
786         ret = osd_write_ldd(&mop);
787         if (ret != 0) {
788                 fatal();
789                 fprintf(stderr, "failed to write local files\n");
790                 goto out;
791         }
792
793 out:
794         osd_fini();
795         ret2 = loop_cleanup(&mop);
796         if (ret == 0)
797                 ret = ret2;
798
799         /* Fix any crazy return values from system() */
800         if (ret && ((ret & 255) == 0))
801                 return (1);
802         if (ret)
803                 verrprint("%s: exiting with %d (%s)\n",
804                           progname, ret, strerror(ret));
805         return (ret);
806 }