Whamcloud - gitweb
LU-1581 utils: extract ldiskfs specifics from mkfs_lustre.c
[fs/lustre-release.git] / lustre / utils / mkfs_lustre.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.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, Whamcloud, Inc.
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 <stdlib.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <stdarg.h>
55 #include <mntent.h>
56
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/mount.h>
60 #include <sys/utsname.h>
61
62 #include <string.h>
63 #include <getopt.h>
64 #include <limits.h>
65 #include <ctype.h>
66
67 #ifdef __linux__
68 /* libcfs.h is not really needed here, but on SLES10/PPC, fs.h includes idr.h
69  * which requires BITS_PER_LONG to be defined */
70 #include <libcfs/libcfs.h>
71 #ifndef BLKGETSIZE64
72 #include <linux/fs.h> /* for BLKGETSIZE64 */
73 #endif
74 #include <linux/version.h>
75 #endif
76 #include <lustre_disk.h>
77 #include <lustre_param.h>
78 #include <lnet/lnetctl.h>
79 #include <lustre_ver.h>
80 #include "mount_utils.h"
81
82 #ifndef PATH_MAX
83 #define PATH_MAX 4096
84 #endif
85
86 char *progname;
87 int verbose = 1;
88 static int print_only = 0;
89 static int upgrade_to_18 = 0;
90
91 void usage(FILE *out)
92 {
93         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
94         fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
95         fprintf(out,
96                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
97                 "\ttarget types:\n"
98                 "\t\t--ost: object storage, mutually exclusive with mdt,mgs\n"
99                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
100                 "\t\t--mgs: configuration management service - one per site\n"
101                 "\toptions (in order of popularity):\n"
102                 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
103                 "\t\t\trequired for all targets other than the mgs node\n"
104                 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
105                 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
106                 "\t\t\tcannot be used with --servicenode\n"
107                 "\t\t--servicenode=<nid>[,<...>] : NID(s) of all service partners\n"
108                 "\t\t\ttreat all nodes as equal service node, cannot be used with --failnode\n"
109                 "\t\t--param <key>=<value> : set a permanent parameter\n"
110                 "\t\t\te.g. --param sys.timeout=40\n"
111                 "\t\t\t     --param lov.stripesize=2M\n"
112                 "\t\t--index=#N : target index (i.e. ost index within lov)\n"
113                 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
114                 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
115                 "\t\t--network=<net>[,<...>] : restrict OST/MDT to network(s)\n"
116 #ifndef TUNEFS
117                 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
118                 "\t\t--device-size=#N(KB) : device size for loop devices\n"
119                 "\t\t--mkfsoptions=<opts> : format options\n"
120                 "\t\t--reformat: overwrite an existing disk\n"
121                 "\t\t--stripe-count-hint=#N : for optimizing MDT inode size\n"
122                 "\t\t--iam-dir: use IAM directory format, not ext3 compatible\n"
123 #else
124                 "\t\t--erase-params : erase all old parameter settings\n"
125                 "\t\t--nomgs: turn off MGS service on this MDT\n"
126                 "\t\t--writeconf: erase all config logs for this fs.\n"
127 #endif
128                 "\t\t--dryrun: just report what we would do; "
129                 "don't write to disk\n"
130                 "\t\t--verbose : e.g. show mkfs progress\n"
131                 "\t\t--quiet\n",
132                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
133         return;
134 }
135
136 /*================ utility functions =====================*/
137
138 char *strscat(char *dst, char *src, int buflen) {
139         dst[buflen - 1] = 0;
140         if (strlen(dst) + strlen(src) >= buflen) {
141                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
142                         "\n", buflen, dst, src);
143                 exit(EOVERFLOW);
144         }
145         return strcat(dst, src);
146
147 }
148
149 char *strscpy(char *dst, char *src, int buflen) {
150         dst[0] = 0;
151         return strscat(dst, src, buflen);
152 }
153
154 inline unsigned int
155 dev_major (unsigned long long int __dev)
156 {
157         return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
158 }
159
160 inline unsigned int
161 dev_minor (unsigned long long int __dev)
162 {
163         return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
164 }
165
166 int get_os_version()
167 {
168         static int version = 0;
169
170         if (!version) {
171                 int fd;
172                 char release[4] = "";
173
174                 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
175                 if (fd < 0) {
176                         fprintf(stderr, "%s: Warning: Can't resolve kernel "
177                                 "version, assuming 2.6\n", progname);
178                 } else {
179                         if (read(fd, release, 4) < 0) {
180                                 fprintf(stderr, "reading from /proc/sys/kernel"
181                                         "/osrelease: %s\n", strerror(errno));
182                                 close(fd);
183                                 exit(-1);
184                         }
185                         close(fd);
186                 }
187                 if (strncmp(release, "2.4.", 4) == 0)
188                         version = 24;
189                 else
190                         version = 26;
191         }
192         return version;
193 }
194
195 static int check_mtab_entry(char *spec)
196 {
197         FILE *fp;
198         struct mntent *mnt;
199
200         fp = setmntent(MOUNTED, "r");
201         if (fp == NULL)
202                 return(0);
203
204         while ((mnt = getmntent(fp)) != NULL) {
205                 if (strcmp(mnt->mnt_fsname, spec) == 0) {
206                         endmntent(fp);
207                         fprintf(stderr, "%s: according to %s %s is "
208                                 "already mounted on %s\n",
209                                 progname, MOUNTED, spec, mnt->mnt_dir);
210                         return(EEXIST);
211                 }
212         }
213         endmntent(fp);
214
215         return(0);
216 }
217
218 /*============ disk dev functions ===================*/
219
220 /* Setup a file in the first unused loop_device */
221 int loop_setup(struct mkfs_opts *mop)
222 {
223         char loop_base[20];
224         char l_device[64];
225         int i, ret = 0;
226
227         /* Figure out the loop device names */
228         if (!access("/dev/loop0", F_OK | R_OK)) {
229                 strcpy(loop_base, "/dev/loop\0");
230         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
231                 strcpy(loop_base, "/dev/loop/\0");
232         } else {
233                 fprintf(stderr, "%s: can't access loop devices\n", progname);
234                 return EACCES;
235         }
236
237         /* Find unused loop device */
238         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
239                 char cmd[PATH_MAX];
240                 int cmdsz = sizeof(cmd);
241
242                 sprintf(l_device, "%s%d", loop_base, i);
243                 if (access(l_device, F_OK | R_OK))
244                         break;
245                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
246                 ret = system(cmd);
247
248                 /* losetup gets 1 (ret=256) for non-set-up device */
249                 if (ret) {
250                         /* Set up a loopback device to our file */
251                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
252                                  mop->mo_device);
253                         ret = run_command(cmd, cmdsz);
254                         if (ret == 256)
255                                 /* someone else picked up this loop device
256                                  * behind our back */
257                                 continue;
258                         if (ret) {
259                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
260                                         progname, ret, strerror(ret));
261                                 return ret;
262                         }
263                         strscpy(mop->mo_loopdev, l_device,
264                                 sizeof(mop->mo_loopdev));
265                         return ret;
266                 }
267         }
268
269         fprintf(stderr, "%s: out of loop devices!\n", progname);
270         return EMFILE;
271 }
272
273 int loop_cleanup(struct mkfs_opts *mop)
274 {
275         char cmd[150];
276         int ret = 1;
277         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
278                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
279                 ret = run_command(cmd, sizeof(cmd));
280         }
281         return ret;
282 }
283
284 __u64 get_device_size(char* device)
285 {
286         int ret, fd;
287         __u64 size = 0;
288
289         fd = open(device, O_RDONLY);
290         if (fd < 0) {
291                 fprintf(stderr, "%s: cannot open %s: %s\n",
292                         progname, device, strerror(errno));
293                 return 0;
294         }
295
296 #ifdef BLKGETSIZE64
297         /* size in bytes. bz5831 */
298         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
299 #else
300         {
301                 __u32 lsize = 0;
302                 /* size in blocks */
303                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
304                 size = (__u64)lsize * 512;
305         }
306 #endif
307         close(fd);
308         if (ret < 0) {
309                 fprintf(stderr, "%s: size ioctl failed: %s\n",
310                         progname, strerror(errno));
311                 return 0;
312         }
313
314         vprint("device size = "LPU64"MB\n", size >> 20);
315         /* return value in KB */
316         return size >> 10;
317 }
318
319 int loop_format(struct mkfs_opts *mop)
320 {
321         int fd;
322
323         if (mop->mo_device_sz == 0) {
324                 fatal();
325                 fprintf(stderr, "loop device requires a --device-size= "
326                         "param\n");
327                 return EINVAL;
328         }
329
330         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
331         if (fd < 0) {
332                 fatal();
333                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
334                         progname, strerror(errno));
335                 return errno;
336         }
337
338         if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
339                 close(fd);
340                 fatal();
341                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
342                         progname, strerror(errno));
343                 return errno;
344         }
345
346         close(fd);
347         return 0;
348 }
349
350 /* ==================== Lustre config functions =============*/
351
352 void print_ldd(char *str, struct lustre_disk_data *ldd)
353 {
354         printf("\n   %s:\n", str);
355         printf("Target:     %s\n", ldd->ldd_svname);
356         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
357                 printf("Index:      unassigned\n");
358         else
359                 printf("Index:      %d\n", ldd->ldd_svindex);
360         if (ldd->ldd_uuid[0])
361                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
362         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
363         printf("Mount type: %s\n", MT_STR(ldd));
364         printf("Flags:      %#x\n", ldd->ldd_flags);
365         printf("              (%s%s%s%s%s%s%s%s%s%s)\n",
366                IS_MDT(ldd) ? "MDT ":"",
367                IS_OST(ldd) ? "OST ":"",
368                IS_MGS(ldd) ? "MGS ":"",
369                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
370                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
371                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
372                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
373                ldd->ldd_flags & LDD_F_IAM_DIR  ? "IAM_dir_format ":"",
374                ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
375                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
376         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
377         printf("Parameters:%s\n", ldd->ldd_params);
378         if (ldd->ldd_userdata[0])
379                 printf("Comment: %s\n", ldd->ldd_userdata);
380         printf("\n");
381 }
382
383 void set_defaults(struct mkfs_opts *mop)
384 {
385         mop->mo_ldd.ldd_magic = LDD_MAGIC;
386         mop->mo_ldd.ldd_config_ver = 1;
387         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
388         mop->mo_mgs_failnodes = 0;
389         strcpy(mop->mo_ldd.ldd_fsname, "lustre");
390         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
391
392         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
393         mop->mo_stripe_count = 1;
394 }
395
396 static inline void badopt(const char *opt, char *type)
397 {
398         fprintf(stderr, "%s: '--%s' only valid for %s\n",
399                 progname, opt, type);
400         usage(stderr);
401 }
402
403 int add_param(char *buf, char *key, char *val)
404 {
405         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
406         int start = strlen(buf);
407         int keylen = 0;
408
409         if (key)
410                 keylen = strlen(key);
411         if (start + 1 + keylen + strlen(val) >= end) {
412                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
413                         progname, buf, key ? key : "", val);
414                 return 1;
415         }
416
417         sprintf(buf + start, " %s%s", key ? key : "", val);
418         return 0;
419 }
420
421 /* from mount_lustre */
422 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
423 #define MAXNIDSTR 1024
424 static char *convert_hostnames(char *s1)
425 {
426         char *converted, *s2 = 0, *c, *end, sep;
427         int left = MAXNIDSTR;
428         lnet_nid_t nid;
429
430         converted = malloc(left);
431         if (converted == NULL) {
432                 return NULL;
433         }
434
435         end = s1 + strlen(s1);
436         c = converted;
437         while ((left > 0) && (s1 < end)) {
438                 s2 = strpbrk(s1, ",:");
439                 if (!s2)
440                         s2 = end;
441                 sep = *s2;
442                 *s2 = '\0';
443                 nid = libcfs_str2nid(s1);
444
445                 if (nid == LNET_NID_ANY) {
446                         fprintf(stderr, "%s: Can't parse NID '%s'\n",
447                                 progname, s1);
448                         free(converted);
449                         return NULL;
450                 }
451                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
452                             strlen("127.0.0.1")) == 0) {
453                         fprintf(stderr, "%s: The NID '%s' resolves to the "
454                                 "loopback address '%s'.  Lustre requires a "
455                                 "non-loopback address.\n",
456                                 progname, s1, libcfs_nid2str(nid));
457                         free(converted);
458                         return NULL;
459                 }
460
461                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
462                 left = converted + MAXNIDSTR - c;
463                 s1 = s2 + 1;
464         }
465         return converted;
466 }
467
468 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
469                char **mountopts)
470 {
471         static struct option long_opt[] = {
472                 {"iam-dir", 0, 0, 'a'},
473                 {"backfstype", 1, 0, 'b'},
474                 {"stripe-count-hint", 1, 0, 'c'},
475                 {"comment", 1, 0, 'u'},
476                 {"configdev", 1, 0, 'C'},
477                 {"device-size", 1, 0, 'd'},
478                 {"dryrun", 0, 0, 'n'},
479                 {"erase-params", 0, 0, 'e'},
480                 {"failnode", 1, 0, 'f'},
481                 {"failover", 1, 0, 'f'},
482                 {"mgs", 0, 0, 'G'},
483                 {"help", 0, 0, 'h'},
484                 {"index", 1, 0, 'i'},
485                 {"mkfsoptions", 1, 0, 'k'},
486                 {"mgsnode", 1, 0, 'm'},
487                 {"mgsnid", 1, 0, 'm'},
488                 {"mdt", 0, 0, 'M'},
489                 {"fsname",1, 0, 'L'},
490                 {"noformat", 0, 0, 'n'},
491                 {"nomgs", 0, 0, 'N'},
492                 {"mountfsoptions", 1, 0, 'o'},
493                 {"ost", 0, 0, 'O'},
494                 {"param", 1, 0, 'p'},
495                 {"print", 0, 0, 'n'},
496                 {"quiet", 0, 0, 'q'},
497                 {"reformat", 0, 0, 'r'},
498                 {"servicenode", 1, 0, 's'},
499                 {"verbose", 0, 0, 'v'},
500                 {"writeconf", 0, 0, 'w'},
501                 {"upgrade_to_18", 0, 0, 'U'},
502                 {"network", 1, 0, 't'},
503                 {0, 0, 0, 0}
504         };
505         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
506         int opt;
507         int rc, longidx;
508         int failnode_set = 0, servicenode_set = 0;
509
510         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
511                EOF) {
512                 switch (opt) {
513                 case 'a': {
514                         if (IS_MDT(&mop->mo_ldd))
515                                 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
516                         break;
517                 }
518                 case 'b': {
519                         int i = 0;
520                         while (i < LDD_MT_LAST) {
521                                 if (strcmp(optarg, mt_str(i)) == 0) {
522                                         mop->mo_ldd.ldd_mount_type = i;
523                                         break;
524                                 }
525                                 i++;
526                         }
527                         break;
528                 }
529                 case 'c':
530                         if (IS_MDT(&mop->mo_ldd)) {
531                                 int stripe_count = atol(optarg);
532                                 if (stripe_count <= 0) {
533                                         fprintf(stderr, "%s: bad stripe count "
534                                                 "%d\n", progname, stripe_count);
535                                         return 1;
536                                 }
537                                 mop->mo_stripe_count = stripe_count;
538                         } else {
539                                 badopt(long_opt[longidx].name, "MDT");
540                                 return 1;
541                         }
542                         break;
543                 case 'C': /* Configdev */
544                         //FIXME
545                         printf("Configdev not implemented\n");
546                         return 1;
547                 case 'd':
548                         mop->mo_device_sz = atol(optarg);
549                         break;
550                 case 'e':
551                         mop->mo_ldd.ldd_params[0] = '\0';
552                         /* Must update the mgs logs */
553                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
554                         break;
555                 case 'f':
556                 case 's': {
557                         char *nids;
558
559                         if ((opt == 'f' && servicenode_set)
560                             || (opt == 's' && failnode_set)) {
561                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
562                                         progname, long_opt[longidx].name,
563                                         opt == 'f' ? "servicenode" : "failnode");
564                                 return 1;
565                         }
566
567                         nids = convert_hostnames(optarg);
568                         if (!nids)
569                                 return 1;
570                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
571                                        nids);
572                         free(nids);
573                         if (rc)
574                                 return rc;
575                         /* Must update the mgs logs */
576                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
577                         if (opt == 'f') {
578                                 failnode_set = 1;
579                         } else {
580                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
581                                 servicenode_set = 1;
582                         }
583                         mop->mo_flags |= MO_FAILOVER;
584                         break;
585                 }
586                 case 'G':
587                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
588                         break;
589                 case 'h':
590                         usage(stdout);
591                         return 1;
592                 case 'i':
593                         if (!(mop->mo_ldd.ldd_flags &
594                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
595                                LDD_F_WRITECONF))) {
596                                 fprintf(stderr, "%s: cannot change the index of"
597                                         " a registered target\n", progname);
598                                 return 1;
599                         }
600                         if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
601                                 mop->mo_ldd.ldd_svindex = atol(optarg);
602                                 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
603                         } else {
604                                 badopt(long_opt[longidx].name, "MDT,OST");
605                                 return 1;
606                         }
607                         break;
608                 case 'k':
609                         strscpy(mop->mo_mkfsopts, optarg,
610                                 sizeof(mop->mo_mkfsopts));
611                         break;
612                 case 'L': {
613                         char *tmp;
614                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
615                             (!(mop->mo_ldd.ldd_flags &
616                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
617                                 LDD_F_WRITECONF)))) {
618                                 fprintf(stderr, "%s: cannot change the name of"
619                                         " a registered target\n", progname);
620                                 return 1;
621                         }
622                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
623                                 fprintf(stderr, "%s: filesystem name must be "
624                                         "1-8 chars\n", progname);
625                                 return 1;
626                         }
627                         if ((tmp = strpbrk(optarg, "/:"))) {
628                                 fprintf(stderr, "%s: char '%c' not allowed in "
629                                         "filesystem name\n", progname, *tmp);
630                                 return 1;
631                         }
632                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
633                                 sizeof(mop->mo_ldd.ldd_fsname));
634                         break;
635                 }
636                 case 'm': {
637                         char *nids = convert_hostnames(optarg);
638                         if (!nids)
639                                 return 1;
640                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
641                                        nids);
642                         free(nids);
643                         if (rc)
644                                 return rc;
645                         mop->mo_mgs_failnodes++;
646                         break;
647                 }
648                 case 'M':
649                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
650                         break;
651                 case 'n':
652                         print_only++;
653                         break;
654                 case 'N':
655                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
656                         break;
657                 case 'o':
658                         *mountopts = optarg;
659                         break;
660                 case 'O':
661                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
662                         break;
663                 case 'p':
664                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
665                         if (rc)
666                                 return rc;
667                         /* Must update the mgs logs */
668                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
669                         break;
670                 case 'q':
671                         verbose--;
672                         break;
673                 case 'r':
674                         mop->mo_flags |= MO_FORCEFORMAT;
675                         break;
676                 case 't':
677                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
678                                 badopt(long_opt[longidx].name, "MDT,OST");
679                                 return 1;
680                         }
681
682                         if (!optarg)
683                                 return 1;
684
685                         rc = add_param(mop->mo_ldd.ldd_params,
686                                        PARAM_NETWORK, optarg);
687                         if (rc != 0)
688                                 return rc;
689                         /* Must update the mgs logs */
690                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
691                         break;
692                 case 'u':
693                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
694                                 sizeof(mop->mo_ldd.ldd_userdata));
695                         break;
696                 case 'v':
697                         verbose++;
698                         break;
699                 case 'w':
700                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
701                         break;
702                 case 'U':
703                         upgrade_to_18 = 1;
704                         break;
705                 default:
706                         if (opt != '?') {
707                                 fatal();
708                                 fprintf(stderr, "Unknown option '%c'\n", opt);
709                         }
710                         return EINVAL;
711                 }
712         }//while
713
714         /* Last arg is device */
715         if (optind != argc - 1) {
716                 fatal();
717                 fprintf(stderr, "Bad argument: %s\n", argv[optind]);
718                 return EINVAL;
719         }
720
721         /* single argument: <device> */
722         if (argc == 2)
723                 ++print_only;
724
725         return 0;
726 }
727
728 /* Search for opt in mntlist, returning true if found.
729  */
730 static int in_mntlist(char *opt, char *mntlist)
731 {
732         char *ml, *mlp, *item, *ctx = NULL;
733
734         if (!(ml = strdup(mntlist))) {
735                 fprintf(stderr, "%s: out of memory\n", progname);
736                 exit(1);
737         }
738         mlp = ml;
739         while ((item = strtok_r(mlp, ",", &ctx))) {
740                 if (!strcmp(opt, item))
741                         break;
742                 mlp = NULL;
743         }
744         free(ml);
745         return (item != NULL);
746 }
747
748 /* Issue a message on stderr for every item in wanted_mountopts that is not
749  * present in mountopts.  The justwarn boolean toggles between error and
750  * warning message.  Return an error count.
751  */
752 static int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
753                                 int justwarn)
754 {
755         char *ml, *mlp, *item, *ctx = NULL;
756         int errors = 0;
757
758         if (!(ml = strdup(wanted_mountopts))) {
759                 fprintf(stderr, "%s: out of memory\n", progname);
760                 exit(1);
761         }
762         mlp = ml;
763         while ((item = strtok_r(mlp, ",", &ctx))) {
764                 if (!in_mntlist(item, mountopts)) {
765                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
766                                 progname, justwarn ? "Warning: default"
767                                 : "Error: mandatory", item);
768                         errors++;
769                 }
770                 mlp = NULL;
771         }
772         free(ml);
773         return errors;
774 }
775
776 /* Trim embedded white space, leading and trailing commas from string s.
777  */
778 static void trim_mountfsoptions(char *s)
779 {
780         char *p;
781
782         for (p = s; *p; ) {
783                 if (isspace(*p)) {
784                         memmove(p, p + 1, strlen(p + 1) + 1);
785                         continue;
786                 }
787                 p++;
788         }
789
790         while (s[0] == ',')
791                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
792
793         p = s + strlen(s) - 1;
794         while (p >= s && *p == ',')
795                 *p-- = '\0';
796 }
797
798 int main(int argc, char *const argv[])
799 {
800         struct mkfs_opts mop;
801         struct lustre_disk_data *ldd;
802         char *mountopts = NULL;
803         char always_mountopts[512] = "";
804         char default_mountopts[512] = "";
805         int ret = 0;
806
807         if ((progname = strrchr(argv[0], '/')) != NULL)
808                 progname++;
809         else
810                 progname = argv[0];
811
812         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
813                 usage(stderr);
814                 return(EINVAL);
815         }
816
817         memset(&mop, 0, sizeof(mop));
818         set_defaults(&mop);
819
820         /* device is last arg */
821         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
822
823         /* Are we using a loop device? */
824         ret = is_block(mop.mo_device);
825         if (ret < 0) {
826                 ret = errno;
827                 goto out;
828         }
829         if (ret == 0)
830                 mop.mo_flags |= MO_IS_LOOP;
831
832 #ifdef TUNEFS
833         /* For tunefs, we must read in the old values before parsing any
834            new ones. */
835
836         /* Check whether the disk has already been formatted by mkfs.lustre */
837         ret = is_lustre_target(&mop);
838         if (ret == 0) {
839                 fatal();
840                 fprintf(stderr, "Device %s has not been formatted with "
841                         "mkfs.lustre\n", mop.mo_device);
842                 ret = ENODEV;
843                 goto out;
844         }
845
846         ret = read_local_files(&mop);
847         if (ret) {
848                 fatal();
849                 fprintf(stderr, "Failed to read previous Lustre data from %s "
850                         "(%d)\n", mop.mo_device, ret);
851                 goto out;
852         }
853         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
854             mop.mo_mgs_failnodes++;
855
856         if (verbose > 0)
857                 print_ldd("Read previous values", &(mop.mo_ldd));
858 #endif
859
860         ret = parse_opts(argc, argv, &mop, &mountopts);
861         if (ret)
862                 goto out;
863
864         ldd = &mop.mo_ldd;
865
866         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
867                 fatal();
868                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
869                 ret = EINVAL;
870                 goto out;
871         }
872
873         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
874                 fatal();
875                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
876                 ret = EINVAL;
877                 goto out;
878         }
879
880         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
881             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
882                 fatal();
883                 fprintf(stderr, "Can't find the target index, "
884                         "specify with --index\n");
885                 ret = EINVAL;
886                 goto out;
887         }
888 #if 0
889         /*
890          * Comment out these 2 checks temporarily, since for multi-MDSes
891          * in single node only 1 mds node could have mgs service
892          */
893         if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
894                 verrprint("No management node specified, adding MGS to this "
895                           "MDT\n");
896                 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
897         }
898         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
899                 fatal();
900                 if (IS_MDT(ldd))
901                         fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
902                 else
903                         fprintf(stderr, "Must specify --mgsnode=\n");
904                 ret = EINVAL;
905                 goto out;
906         }
907 #endif
908
909         /* These are the permanent mount options (always included) */
910         switch (ldd->ldd_mount_type) {
911         case LDD_MT_EXT3:
912         case LDD_MT_LDISKFS:
913         case LDD_MT_LDISKFS2:
914                 strscat(default_mountopts, ",errors=remount-ro",
915                         sizeof(default_mountopts));
916                 if (IS_MDT(ldd) || IS_MGS(ldd))
917                         strscat(always_mountopts, ",user_xattr",
918                                 sizeof(always_mountopts));
919                 /* NB: Files created while extents are enabled can only be read
920                  * if mounted using the ext4 or ldiskfs filesystem type. */
921                 if (IS_OST(ldd) &&
922                     (ldd->ldd_mount_type == LDD_MT_LDISKFS ||
923                      ldd->ldd_mount_type == LDD_MT_LDISKFS2)) {
924                         strscat(default_mountopts, ",extents,mballoc",
925                                 sizeof(default_mountopts));
926                 }
927                 break;
928         default:
929                 fatal();
930                 fprintf(stderr, "unknown fs type %d '%s'\n",
931                         ldd->ldd_mount_type,
932                         MT_STR(ldd));
933                 ret = EINVAL;
934                 goto out;
935         }
936
937         if (mountopts) {
938                 trim_mountfsoptions(mountopts);
939                 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
940                 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
941                         ret = EINVAL;
942                         goto out;
943                 }
944                 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
945         } else {
946 #ifdef TUNEFS
947                 if (ldd->ldd_mount_opts[0] == 0)
948                         /* use the defaults unless old opts exist */
949 #endif
950                 {
951                         sprintf(ldd->ldd_mount_opts, "%s%s",
952                                 always_mountopts, default_mountopts);
953                         trim_mountfsoptions(ldd->ldd_mount_opts);
954                 }
955         }
956
957         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
958                          ldd->ldd_fsname, ldd->ldd_svname);
959
960         if (verbose >= 0)
961                 print_ldd("Permanent disk data", ldd);
962
963         if (print_only) {
964                 printf("exiting before disk write.\n");
965                 goto out;
966         }
967
968         if (check_mtab_entry(mop.mo_device))
969                 return(EEXIST);
970
971         /* Create the loopback file */
972         if (mop.mo_flags & MO_IS_LOOP) {
973                 ret = access(mop.mo_device, F_OK);
974                 if (ret)
975                         ret = errno;
976 #ifndef TUNEFS /* mkfs.lustre */
977                 /* Reformat the loopback file */
978                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
979                         ret = loop_format(&mop);
980                         if (ret)
981                                 goto out;
982                 }
983 #endif
984                 if (ret == 0)
985                         ret = loop_setup(&mop);
986                 if (ret) {
987                         fatal();
988                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
989                                 mop.mo_device, strerror(ret));
990                         goto out;
991                 }
992         }
993
994 #ifndef TUNEFS /* mkfs.lustre */
995         /* Check whether the disk has already been formatted by mkfs.lustre */
996         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
997                 ret = is_lustre_target(&mop);
998                 if (ret) {
999                         fatal();
1000                         fprintf(stderr, "Device %s was previously formatted "
1001                                 "for lustre. Use --reformat to reformat it, "
1002                                 "or tunefs.lustre to modify.\n",
1003                                 mop.mo_device);
1004                         goto out;
1005                 }
1006         }
1007
1008         /* Format the backing filesystem */
1009         ret = make_lustre_backfs(&mop);
1010         if (ret != 0) {
1011                 fatal();
1012                 fprintf(stderr, "mkfs failed %d\n", ret);
1013                 goto out;
1014         }
1015 #endif
1016
1017         /* Write our config files */
1018         ret = write_local_files(&mop);
1019         if (ret != 0) {
1020                 fatal();
1021                 fprintf(stderr, "failed to write local files\n");
1022                 goto out;
1023         }
1024
1025 out:
1026         loop_cleanup(&mop);
1027
1028         /* Fix any crazy return values from system() */
1029         if (ret && ((ret & 255) == 0))
1030                 return (1);
1031         if (ret)
1032                 verrprint("%s: exiting with %d (%s)\n",
1033                           progname, ret, strerror(ret));
1034         return (ret);
1035 }