Whamcloud - gitweb
Land b_head_interop_disk on HEAD (20081119_1314)
[fs/lustre-release.git] / lustre / utils / mkfs_lustre.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #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
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/mount.h>
56 #include <sys/utsname.h>
57
58 #include <string.h>
59 #include <getopt.h>
60 #include <limits.h>
61
62 #ifdef __linux__
63 /* libcfs.h is not really needed here, but on SLES10/PPC, fs.h includes idr.h which
64  * requires BITS_PER_LONG to be defined */
65 #include <libcfs/libcfs.h>
66 #include <linux/fs.h> /* for BLKGETSIZE64 */
67 #include <linux/version.h>
68 #endif
69 #include <lustre_disk.h>
70 #include <lustre_param.h>
71 #include <lnet/lnetctl.h>
72 #include <lustre_ver.h>
73 #include "mount_utils.h"
74
75 #ifndef PATH_MAX
76 #define PATH_MAX 4096
77 #endif
78
79 #define MAX_LOOP_DEVICES 16
80 #define L_BLOCK_SIZE 4096
81 #define INDEX_UNASSIGNED 0xFFFF
82 #define MO_IS_LOOP     0x01
83 #define MO_FORCEFORMAT 0x02
84
85 /* used to describe the options to format the lustre disk, not persistent */
86 struct mkfs_opts {
87         struct lustre_disk_data mo_ldd; /* to be written in MOUNT_DATA_FILE */
88         char  mo_device[128];           /* disk device name */
89         char  mo_mkfsopts[128];         /* options to the backing-store mkfs */
90         char  mo_loopdev[128];          /* in case a loop dev is needed */
91         __u64 mo_device_sz;             /* in KB */
92         int   mo_stripe_count;
93         int   mo_flags;
94         int   mo_mgs_failnodes;
95 };
96
97 char *progname;
98 int verbose = 1;
99 static int print_only = 0;
100 static int failover = 0;
101 static int upgrade_to_18 = 0;
102
103 void usage(FILE *out)
104 {
105         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
106         fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
107         fprintf(out,
108                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
109                 "\ttarget types:\n"
110                 "\t\t--ost: object storage, mutually exclusive with mdt,mgs\n"
111                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
112                 "\t\t--mgs: configuration management service - one per site\n"
113                 "\toptions (in order of popularity):\n"
114                 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
115                 "\t\t\trequired for all targets other than the mgs node\n"
116                 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
117                 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
118                 "\t\t--param <key>=<value> : set a permanent parameter\n"
119                 "\t\t\te.g. --param sys.timeout=40\n"
120                 "\t\t\t     --param lov.stripesize=2M\n"
121                 "\t\t--index=#N : target index (i.e. ost index within the lov)\n"
122                 /* FIXME implement 1.6.x
123                 "\t\t--configdev=<altdevice|file>: store configuration info\n"
124                 "\t\t\tfor this device on an alternate device\n"
125                 */
126                 "\t\t--comment=<user comment>: arbitrary user string (%d bytes)\n"
127                 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
128 #ifndef TUNEFS
129                 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
130                 "\t\t--device-size=#N(KB) : device size for loop devices\n"
131                 "\t\t--mkfsoptions=<opts> : format options\n"
132                 "\t\t--reformat: overwrite an existing disk\n"
133                 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
134                 "\t\t--iam-dir: make use of IAM directory format on backfs, incompatible with ext3.\n"
135 #else
136                 "\t\t--erase-params : erase all old parameter settings\n"
137                 "\t\t--nomgs: turn off MGS service on this MDT\n"
138                 "\t\t--writeconf: erase all config logs for this fs.\n"
139 #endif
140                 "\t\t--dryrun: just report what we would do; "
141                 "don't write to disk\n"
142                 "\t\t--verbose : e.g. show mkfs progress\n"
143                 "\t\t--quiet\n",
144                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
145         return;
146 }
147
148 #define vprint if (verbose > 0) printf
149 #define verrprint if (verbose >= 0) printf
150
151 /*================ utility functions =====================*/
152
153 char *strscat(char *dst, char *src, int buflen) {
154         dst[buflen - 1] = 0;
155         if (strlen(dst) + strlen(src) >= buflen) {
156                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
157                         "\n", buflen, dst, src);
158                 exit(EOVERFLOW);
159         }
160         return strcat(dst, src);
161
162 }
163
164 char *strscpy(char *dst, char *src, int buflen) {
165         dst[0] = 0;
166         return strscat(dst, src, buflen);
167 }
168
169 inline unsigned int
170 dev_major (unsigned long long int __dev)
171 {
172         return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
173 }
174
175 inline unsigned int
176 dev_minor (unsigned long long int __dev)
177 {
178         return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
179 }
180
181 int get_os_version()
182 {
183         static int version = 0;
184
185         if (!version) {
186                 int fd;
187                 char release[4] = "";
188
189                 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
190                 if (fd < 0)
191                         fprintf(stderr, "%s: Warning: Can't resolve kernel "
192                                 "version, assuming 2.6\n", progname);
193                 else {
194                         read(fd, release, 4);
195                         close(fd);
196                 }
197                 if (strncmp(release, "2.4.", 4) == 0)
198                         version = 24;
199                 else
200                         version = 26;
201         }
202         return version;
203 }
204
205 static int check_mtab_entry(char *spec)
206 {
207         FILE *fp;
208         struct mntent *mnt;
209
210         fp = setmntent(MOUNTED, "r");
211         if (fp == NULL)
212                 return(0);
213
214         while ((mnt = getmntent(fp)) != NULL) {
215                 if (strcmp(mnt->mnt_fsname, spec) == 0) {
216                         endmntent(fp);
217                         fprintf(stderr, "%s: according to %s %s is "
218                                 "already mounted on %s\n",
219                                 progname, MOUNTED, spec, mnt->mnt_dir);
220                         return(EEXIST);
221                 }
222         }
223         endmntent(fp);
224
225         return(0);
226 }
227
228 /*============ disk dev functions ===================*/
229
230 /* Setup a file in the first unused loop_device */
231 int loop_setup(struct mkfs_opts *mop)
232 {
233         char loop_base[20];
234         char l_device[64];
235         int i, ret = 0;
236
237         /* Figure out the loop device names */
238         if (!access("/dev/loop0", F_OK | R_OK)) {
239                 strcpy(loop_base, "/dev/loop\0");
240         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
241                 strcpy(loop_base, "/dev/loop/\0");
242         } else {
243                 fprintf(stderr, "%s: can't access loop devices\n", progname);
244                 return EACCES;
245         }
246
247         /* Find unused loop device */
248         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
249                 char cmd[PATH_MAX];
250                 int cmdsz = sizeof(cmd);
251
252                 sprintf(l_device, "%s%d", loop_base, i);
253                 if (access(l_device, F_OK | R_OK))
254                         break;
255                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
256                 ret = system(cmd);
257
258                 /* losetup gets 1 (ret=256) for non-set-up device */
259                 if (ret) {
260                         /* Set up a loopback device to our file */
261                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
262                                  mop->mo_device);
263                         ret = run_command(cmd, cmdsz);
264                         if (ret) {
265                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
266                                         progname, ret, strerror(ret));
267                                 return ret;
268                         }
269                         strscpy(mop->mo_loopdev, l_device,
270                                 sizeof(mop->mo_loopdev));
271                         return ret;
272                 }
273         }
274
275         fprintf(stderr, "%s: out of loop devices!\n", progname);
276         return EMFILE;
277 }
278
279 int loop_cleanup(struct mkfs_opts *mop)
280 {
281         char cmd[150];
282         int ret = 1;
283         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
284                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
285                 ret = run_command(cmd, sizeof(cmd));
286         }
287         return ret;
288 }
289
290 /* Determine if a device is a block device (as opposed to a file) */
291 int is_block(char* devname)
292 {
293         struct stat st;
294         int ret = 0;
295
296         ret = access(devname, F_OK);
297         if (ret != 0)
298                 return 0;
299         ret = stat(devname, &st);
300         if (ret != 0) {
301                 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
302                 return -1;
303         }
304         return S_ISBLK(st.st_mode);
305 }
306
307 __u64 get_device_size(char* device)
308 {
309         int ret, fd;
310         __u64 size = 0;
311
312         fd = open(device, O_RDONLY);
313         if (fd < 0) {
314                 fprintf(stderr, "%s: cannot open %s: %s\n",
315                         progname, device, strerror(errno));
316                 return 0;
317         }
318
319 #ifdef BLKGETSIZE64
320         /* size in bytes. bz5831 */
321         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
322 #else
323         {
324                 __u32 lsize = 0;
325                 /* size in blocks */
326                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
327                 size = (__u64)lsize * 512;
328         }
329 #endif
330         close(fd);
331         if (ret < 0) {
332                 fprintf(stderr, "%s: size ioctl failed: %s\n",
333                         progname, strerror(errno));
334                 return 0;
335         }
336
337         vprint("device size = "LPU64"MB\n", size >> 20);
338         /* return value in KB */
339         return size >> 10;
340 }
341
342 int loop_format(struct mkfs_opts *mop)
343 {
344         int ret = 0;
345
346         if (mop->mo_device_sz == 0) {
347                 fatal();
348                 fprintf(stderr, "loop device requires a --device-size= "
349                         "param\n");
350                 return EINVAL;
351         }
352
353         ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
354         if (ret < 0) {
355                 ret = errno;
356                 fprintf(stderr, "%s: Unable to create backing store: %d\n",
357                         progname, ret);
358         } else {
359                 close(ret);
360         }
361
362         ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
363         if (ret != 0) {
364                 ret = errno;
365                 fprintf(stderr, "%s: Unable to truncate backing store: %d\n",
366                         progname, ret);
367         }
368
369         return ret;
370 }
371
372 /* Display the need for the latest e2fsprogs to be installed. make_backfs
373  * indicates if the caller is make_lustre_backfs() or not. */
374 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
375 {
376         static int msg_displayed;
377
378         if (msg_displayed) {
379                 fprintf(stderr, "WARNING: e2fsprogs does not support %s "
380                         "feature.\n\n", feature);
381                 return;
382         }
383
384         msg_displayed++;
385
386         fprintf(stderr, "WARNING: The e2fsprogs package currently installed on "
387                 "your system does not support \"%s\" feature.\nPlease install "
388                 "the latest version of e2fsprogs from http://downloads.lustre.org"
389                 "/public/tools/e2fsprogs/\nto enable this feature.\n", feature);
390
391         if (make_backfs)
392                 fprintf(stderr, "Feature will not be enabled until e2fsprogs "
393                         "is updated and 'tune2fs -O %s %%{device}' "
394                         "is run.\n\n", feature);
395 }
396
397 /* Check whether the file exists in the device */
398 static int file_in_dev(char *file_name, char *dev_name)
399 {
400         FILE *fp;
401         char debugfs_cmd[256];
402         unsigned int inode_num;
403         int i;
404
405         /* Construct debugfs command line. */
406         snprintf(debugfs_cmd, sizeof(debugfs_cmd),
407                 "debugfs -c -R 'stat %s' '%s' 2>&1 | egrep '(Inode|unsupported)'",
408                 file_name, dev_name);
409
410         fp = popen(debugfs_cmd, "r");
411         if (!fp) {
412                 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
413                 return 0;
414         }
415
416         if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
417                 pclose(fp);
418                 return 1;
419         }
420         i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
421         if (i) {
422                 debugfs_cmd[i] = 0;
423                 fprintf(stderr, "%s", debugfs_cmd);
424                 if (strstr(debugfs_cmd, "unsupported feature")) {
425                           disp_old_e2fsprogs_msg("an unknown", 0);
426                 }
427                 pclose(fp);
428                 return -1;
429         }
430         pclose(fp);
431         return 0;
432 }
433
434 /* Check whether the device has already been used with lustre */
435 static int is_lustre_target(struct mkfs_opts *mop)
436 {
437         int rc;
438
439         vprint("checking for existing Lustre data: ");
440
441         if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))) {
442                 vprint("found %s\n",
443                        (rc == 1) ? MOUNT_DATA_FILE : "extents");
444                  /* in the -1 case, 'extents' means this really IS a lustre
445                     target */
446                 return rc;
447         }
448
449         if ((rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
450                 vprint("found %s\n", LAST_RCVD);
451                 return rc;
452         }
453
454         vprint("not found\n");
455         return 0; /* The device is not a lustre target. */
456 }
457
458 /* Check if a certain feature is supported by e2fsprogs.
459  * Firstly we try to use "debugfs supported_features" command to check if
460  * the feature is supported. If this fails we try to set this feature with
461  * mke2fs to check for its support. */
462 static int is_e2fsprogs_feature_supp(const char *feature)
463 {
464         FILE *fp;
465         char cmd[PATH_MAX];
466         char imgname[] = "/tmp/test-img-XXXXXX";
467         int fd = -1;
468         int ret = 0;
469
470         snprintf(cmd, sizeof(cmd),
471                  "debugfs -c -R \"supported_features %s\" 2>&1", feature);
472
473         /* Using popen() instead of run_command() since debugfs does not return
474          * proper error code if command is not supported */
475         fp = popen(cmd, "r");
476         if (!fp) {
477                 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
478                 return 0;
479         }
480         ret = fread(cmd, 1, sizeof(cmd), fp);
481         if (ret > 0) {
482                 if (strstr(cmd, feature) && !(strstr(cmd, "Unknown")))
483                         return 0;
484         }
485
486         if ((fd = mkstemp(imgname)) < 0)
487                 return -1;
488
489         snprintf(cmd, sizeof(cmd), "mke2fs -F -O %s %s 100 >/dev/null 2>&1",
490                  feature, imgname);
491         /* run_command() displays the output of mke2fs when it fails for
492          * some feature, so use system() directly */
493         ret = system(cmd);
494         if (fd >= 0)
495                 remove(imgname);
496
497         return ret;
498 }
499
500 static void disp_old_kernel_msg(char *feature)
501 {
502        fprintf(stderr, "WARNING: ldiskfs filesystem does not support \"%s\" "
503                "feature.\n\n", feature);
504 }
505
506 static void enable_default_backfs_features(struct mkfs_opts *mop)
507 {
508         struct utsname uts;
509         int maj_high, maj_low, min;
510         int ret;
511
512         strscat(mop->mo_mkfsopts, " -O dir_index", sizeof(mop->mo_mkfsopts));
513
514         /* Upstream e2fsprogs called our uninit_groups feature uninit_bg,
515          * check for both of them when testing e2fsprogs features. */
516         if (is_e2fsprogs_feature_supp("uninit_groups") == 0)
517                 strscat(mop->mo_mkfsopts, ",uninit_groups",
518                         sizeof(mop->mo_mkfsopts));
519         else if (is_e2fsprogs_feature_supp("uninit_bg") == 0)
520                 strscat(mop->mo_mkfsopts, ",uninit_bg",
521                         sizeof(mop->mo_mkfsopts));
522         else
523                 disp_old_e2fsprogs_msg("uninit_bg", 1);
524
525         ret = uname(&uts);
526         if (ret)
527                 return;
528
529         sscanf(uts.release, "%d.%d.%d", &maj_high, &maj_low, &min);
530         printf("%d %d %d\n", maj_high, maj_low, min);
531
532         /* Multiple mount protection is enabled only if failover node is
533          * specified and if kernel version is higher than 2.6.9 */
534         if (failover) {
535                 if (KERNEL_VERSION(maj_high, maj_low, min) >=
536                     KERNEL_VERSION(2,6,9)) {
537                         if (is_e2fsprogs_feature_supp("mmp") == 0)
538                                 strscat(mop->mo_mkfsopts, ",mmp",
539                                         sizeof(mop->mo_mkfsopts));
540                         else
541                                 disp_old_e2fsprogs_msg("mmp", 1);
542                 } else {
543                         disp_old_kernel_msg("mmp");
544                 }
545         }
546 }
547 /* Build fs according to type */
548 int make_lustre_backfs(struct mkfs_opts *mop)
549 {
550         char mkfs_cmd[PATH_MAX];
551         char buf[64];
552         char *dev;
553         int ret = 0;
554         int block_count = 0;
555
556         if (mop->mo_device_sz != 0) {
557                 if (mop->mo_device_sz < 8096){
558                         fprintf(stderr, "%s: size of filesystem must be larger "
559                                 "than 8MB, but is set to %lldKB\n",
560                                 progname, (long long)mop->mo_device_sz);
561                         return EINVAL;
562                 }
563                 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
564         }
565
566         if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
567             (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
568             (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
569                 __u64 device_sz = mop->mo_device_sz;
570
571                 /* we really need the size */
572                 if (device_sz == 0) {
573                         device_sz = get_device_size(mop->mo_device);
574                         if (device_sz == 0)
575                                 return ENODEV;
576                 }
577
578                 /* Journal size in MB */
579                 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
580                         /* Choose our own default journal size */
581                         long journal_sz = 0, max_sz;
582                         if (device_sz > 1024 * 1024) /* 1GB */
583                                 journal_sz = (device_sz / 102400) * 4;
584                         /* cap journal size at 1GB */
585                         if (journal_sz > 1024L)
586                                 journal_sz = 1024L;
587                         /* man mkfs.ext3 */
588                         max_sz = (256000 * L_BLOCK_SIZE) >> 20; /* 1GB */
589                         if (journal_sz > max_sz)
590                                 journal_sz = max_sz;
591                         if (journal_sz) {
592                                 sprintf(buf, " -J size=%ld", journal_sz);
593                                 strscat(mop->mo_mkfsopts, buf,
594                                         sizeof(mop->mo_mkfsopts));
595                         }
596                 }
597
598                 /* Bytes_per_inode: disk size / num inodes */
599                 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
600                         long bytes_per_inode = 0;
601
602                         if (IS_MDT(&mop->mo_ldd))
603                                 bytes_per_inode = 4096;
604
605                         /* Allocate fewer inodes on large OST devices.  Most
606                            filesystems can be much more aggressive than even
607                            this. */
608                         if ((IS_OST(&mop->mo_ldd) && (device_sz > 100000000)))
609                                 bytes_per_inode = 16384;  /* > 100 Gb device */
610
611
612                         if (bytes_per_inode > 0) {
613                                 sprintf(buf, " -i %ld", bytes_per_inode);
614                                 strscat(mop->mo_mkfsopts, buf,
615                                         sizeof(mop->mo_mkfsopts));
616                         }
617                 }
618
619                 /* Inode size (for extended attributes).  The LOV EA size is
620                  * 32 (EA hdr) + 32 (lov_mds_md) + stripes * 24 (lov_ost_data),
621                  * and we want some margin above that for ACLs, other EAs... */
622                 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
623                         long inode_size = 0;
624                         if (IS_MDT(&mop->mo_ldd)) {
625                                 if (mop->mo_stripe_count > 72)
626                                         inode_size = 512; /* bz 7241 */
627                                 /* cray stripes across all osts (>60) */
628                                 else if (mop->mo_stripe_count > 32)
629                                         inode_size = 2048;
630                                 else if (mop->mo_stripe_count > 10)
631                                         inode_size = 1024;
632                                 else
633                                         inode_size = 512;
634                         } else if (IS_OST(&mop->mo_ldd)) {
635                                 /* now as we store fids in EA on OST we need
636                                    to make inode bigger */
637                                 inode_size = 256;
638                         }
639
640                         if (inode_size > 0) {
641                                 sprintf(buf, " -I %ld", inode_size);
642                                 strscat(mop->mo_mkfsopts, buf,
643                                         sizeof(mop->mo_mkfsopts));
644                         }
645                 }
646
647                 if (verbose < 2) {
648                         strscat(mop->mo_mkfsopts, " -q",
649                                 sizeof(mop->mo_mkfsopts));
650                 }
651
652                 if (strstr(mop->mo_mkfsopts, "-O") == NULL)
653                         enable_default_backfs_features(mop);
654
655                 /* Allow reformat of full devices (as opposed to
656                    partitions.)  We already checked for mounted dev. */
657                 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
658
659                 snprintf(mkfs_cmd, sizeof(mkfs_cmd),
660                          "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
661                          mop->mo_ldd.ldd_svname);
662         } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
663                 long journal_sz = 0; /* FIXME default journal size */
664                 if (journal_sz > 0) {
665                         sprintf(buf, " --journal_size %ld", journal_sz);
666                         strscat(mop->mo_mkfsopts, buf,
667                                 sizeof(mop->mo_mkfsopts));
668                 }
669                 snprintf(mkfs_cmd, sizeof(mkfs_cmd), "mkreiserfs -ff ");
670         } else {
671                 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
672                         progname, mop->mo_ldd.ldd_mount_type,
673                         MT_STR(&mop->mo_ldd));
674                 return EINVAL;
675         }
676
677         /* For loop device format the dev, not the filename */
678         dev = mop->mo_device;
679         if (mop->mo_flags & MO_IS_LOOP)
680                 dev = mop->mo_loopdev;
681
682         vprint("formatting backing filesystem %s on %s\n",
683                MT_STR(&mop->mo_ldd), dev);
684         vprint("\ttarget name  %s\n", mop->mo_ldd.ldd_svname);
685         vprint("\t4k blocks     %d\n", block_count);
686         vprint("\toptions       %s\n", mop->mo_mkfsopts);
687
688         /* mkfs_cmd's trailing space is important! */
689         strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
690         strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
691         strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
692         if (block_count != 0) {
693                 sprintf(buf, " %d", block_count);
694                 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
695         }
696
697         vprint("mkfs_cmd = %s\n", mkfs_cmd);
698         ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
699         if (ret) {
700                 fatal();
701                 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
702         }
703         return ret;
704 }
705
706 /* ==================== Lustre config functions =============*/
707
708 void print_ldd(char *str, struct lustre_disk_data *ldd)
709 {
710         printf("\n   %s:\n", str);
711         printf("Target:     %s\n", ldd->ldd_svname);
712         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
713                 printf("Index:      unassigned\n");
714         else
715                 printf("Index:      %d\n", ldd->ldd_svindex);
716         if (ldd->ldd_uuid[0])
717                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
718         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
719         printf("Mount type: %s\n", MT_STR(ldd));
720         printf("Flags:      %#x\n", ldd->ldd_flags);
721         printf("              (%s%s%s%s%s%s%s%s%s)\n",
722                IS_MDT(ldd) ? "MDT ":"",
723                IS_OST(ldd) ? "OST ":"",
724                IS_MGS(ldd) ? "MGS ":"",
725                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
726                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
727                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
728                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
729                ldd->ldd_flags & LDD_F_IAM_DIR  ? "IAM_dir_format ":"",
730                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
731         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
732         printf("Parameters:%s\n", ldd->ldd_params);
733         if (ldd->ldd_userdata[0])
734                 printf("Comment: %s\n", ldd->ldd_userdata);
735         printf("\n");
736 }
737
738 static int touch_file(char *filename)
739 {
740         int fd;
741
742         if (filename == NULL) {
743                 return 1;
744         }
745
746         fd = open(filename, O_CREAT | O_TRUNC, 0600);
747         if (fd < 0) {
748                 return 1;
749         } else {
750                 close(fd);
751                 return 0;
752         }
753 }
754
755 /* keep it less than LL_FID_NAMELEN */
756 #define DUMMY_FILE_NAME_LEN             25
757 #define EXT3_DIRENT_SIZE                DUMMY_FILE_NAME_LEN
758
759 /* Need to add these many entries to this directory to make HTREE dir. */
760 #define MIN_ENTRIES_REQ_FOR_HTREE       ((L_BLOCK_SIZE / EXT3_DIRENT_SIZE))
761
762 static int add_dummy_files(char *dir)
763 {
764         char fpname[PATH_MAX];
765         int i;
766         int rc;
767
768         for (i = 0; i < MIN_ENTRIES_REQ_FOR_HTREE; i++) {
769                 snprintf(fpname, PATH_MAX, "%s/%0*d", dir,
770                          DUMMY_FILE_NAME_LEN, i);
771
772                 rc = touch_file(fpname);
773                 if (rc && rc != -EEXIST) {
774                         fprintf(stderr,
775                                 "%s: Can't create dummy file %s: %s\n",
776                                 progname, fpname , strerror(errno));
777                         return rc;
778                 }
779         }
780         return 0;
781 }
782
783 static int __l_mkdir(char * filepnm, int mode , struct mkfs_opts *mop)
784 {
785         int ret;
786
787         ret = mkdir(filepnm, mode);
788         if (ret && ret != -EEXIST)
789                 return ret;
790
791         /* IAM mode supports ext3 directories of HTREE type only. So add dummy
792          * entries to new directory to create htree type of container for
793          * this directory. */
794         if (mop->mo_ldd.ldd_flags & LDD_F_IAM_DIR)
795                 return add_dummy_files(filepnm);
796         return 0;
797 }
798
799 /* Write the server config files */
800 int write_local_files(struct mkfs_opts *mop)
801 {
802         char mntpt[] = "/tmp/mntXXXXXX";
803         char filepnm[128];
804         char *dev;
805         FILE *filep;
806         int ret = 0;
807
808         /* Mount this device temporarily in order to write these files */
809         if (!mkdtemp(mntpt)) {
810                 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
811                         progname, mntpt, strerror(errno));
812                 return errno;
813         }
814
815         dev = mop->mo_device;
816         if (mop->mo_flags & MO_IS_LOOP)
817                 dev = mop->mo_loopdev;
818
819         ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
820         if (ret) {
821                 fprintf(stderr, "%s: Unable to mount %s: %s\n",
822                         progname, dev, strerror(errno));
823                 ret = errno;
824                 if (errno == ENODEV) {
825                         fprintf(stderr, "Is the %s module available?\n",
826                                 MT_STR(&mop->mo_ldd));
827                 }
828                 goto out_rmdir;
829         }
830
831         /* Set up initial directories */
832         sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
833         ret = __l_mkdir(filepnm, 0777, mop);
834         if ((ret != 0) && (errno != EEXIST)) {
835                 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
836                         progname, filepnm, strerror(errno));
837                 goto out_umnt;
838         } else if (errno == EEXIST) {
839                 ret = 0;
840         }
841
842         /* Save the persistent mount data into a file. Lustre must pre-read
843            this file to get the real mount options. */
844         vprint("Writing %s\n", MOUNT_DATA_FILE);
845         sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
846         filep = fopen(filepnm, "w");
847         if (!filep) {
848                 fprintf(stderr, "%s: Unable to create %s file: %s\n",
849                         progname, filepnm, strerror(errno));
850                 goto out_umnt;
851         }
852         fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
853         fclose(filep);
854         /* COMPAT_146 */
855 #ifdef TUNEFS
856         /* Check for upgrade */
857         if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
858             == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
859                 char cmd[128];
860                 char *term;
861                 int cmdsz = sizeof(cmd);
862                 vprint("Copying old logs\n");
863
864                 /* Copy the old client log to fsname-client */
865                 sprintf(filepnm, "%s/%s/%s-client",
866                         mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
867                 snprintf(cmd, cmdsz, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
868                          filepnm);
869                 ret = run_command(cmd, cmdsz);
870                 if (ret) {
871                         fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
872                                 "(%d)\n", progname, MDT_LOGS_DIR, ret);
873                         fprintf(stderr, "mount -t ldiskfs %s somewhere, "
874                                 "find the client log for fs %s and "
875                                 "copy it manually into %s/%s-client, "
876                                 "then umount.\n",
877                                 mop->mo_device,
878                                 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
879                                 mop->mo_ldd.ldd_fsname);
880                         goto out_umnt;
881                 }
882
883                 /* We need to use the old mdt log because otherwise mdt won't
884                    have complete lov if old clients connect before all
885                    servers upgrade. */
886                 /* Copy the old mdt log to fsname-MDT0000 (get old
887                    name from mdt_UUID) */
888                 ret = 1;
889                 strscpy(filepnm, (char *)mop->mo_ldd.ldd_uuid, sizeof(filepnm));
890                 term = strstr(filepnm, "_UUID");
891                 if (term) {
892                         *term = '\0';
893                         snprintf(cmd, cmdsz, "cp %s/%s/%s %s/%s/%s",
894                                  mntpt, MDT_LOGS_DIR, filepnm,
895                                  mntpt, MOUNT_CONFIGS_DIR,
896                                  mop->mo_ldd.ldd_svname);
897                         ret = run_command(cmd, cmdsz);
898                 }
899                 if (ret) {
900                         fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
901                                 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
902                         fprintf(stderr, "mount -t ext3 %s somewhere, "
903                                 "find the MDT log for fs %s and "
904                                 "copy it manually into %s/%s, "
905                                 "then umount.\n",
906                                 mop->mo_device,
907                                 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
908                                 mop->mo_ldd.ldd_svname);
909                         goto out_umnt;
910                 }
911         }
912 #endif
913         /* end COMPAT_146 */
914
915 out_umnt:
916         umount(mntpt);
917 out_rmdir:
918         rmdir(mntpt);
919         return ret;
920 }
921
922 int read_local_files(struct mkfs_opts *mop)
923 {
924         char tmpdir[] = "/tmp/dirXXXXXX";
925         char cmd[PATH_MAX];
926         char filepnm[128];
927         char *dev;
928         FILE *filep;
929         int ret = 0;
930         int cmdsz = sizeof(cmd);
931
932         /* Make a temporary directory to hold Lustre data files. */
933         if (!mkdtemp(tmpdir)) {
934                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
935                         progname, tmpdir, strerror(errno));
936                 return errno;
937         }
938
939         dev = mop->mo_device;
940
941         /* TODO: it's worth observing the get_mountdata() function that is
942                  in mount_utils.c for getting the mountdata out of the
943                  filesystem */
944
945         /* Construct debugfs command line. */
946         snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s/mountdata' '%s'",
947                  MOUNT_DATA_FILE, tmpdir, dev);
948
949         ret = run_command(cmd, cmdsz);
950         if (ret)
951                 verrprint("%s: Unable to dump %s dir (%d)\n",
952                           progname, MOUNT_CONFIGS_DIR, ret);
953
954         sprintf(filepnm, "%s/mountdata", tmpdir);
955         filep = fopen(filepnm, "r");
956         if (filep) {
957                 vprint("Reading %s\n", MOUNT_DATA_FILE);
958                 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
959         } else {
960                 /* COMPAT_146 */
961                 /* Try to read pre-1.6 config from last_rcvd */
962                 struct lr_server_data lsd;
963                 verrprint("%s: Unable to read %d.%d config %s.\n",
964                           progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
965
966                 verrprint("Trying 1.4 config from last_rcvd\n");
967                 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
968
969                 /* Construct debugfs command line. */
970                 snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s' %s",
971                          LAST_RCVD, filepnm, dev);
972
973                 ret = run_command(cmd, cmdsz);
974                 if (ret) {
975                         fprintf(stderr, "%s: Unable to dump %s file (%d)\n",
976                                 progname, LAST_RCVD, ret);
977                         goto out_rmdir;
978                 }
979
980                 filep = fopen(filepnm, "r");
981                 if (!filep) {
982                         fprintf(stderr, "%s: Unable to open %s: %s\n",
983                                 progname, filepnm, strerror(errno));
984                         ret = errno;
985                         verrprint("Contents of %s:\n", tmpdir);
986                         verbose+=2;
987                         snprintf(cmd, cmdsz, "ls -l %s/", tmpdir);
988                         run_command(cmd, cmdsz);
989                         verrprint("Contents of disk:\n");
990                         snprintf(cmd, cmdsz, "debugfs -c -R 'ls -l /' %s", dev);
991                         run_command(cmd, cmdsz);
992
993                         goto out_rmdir;
994                 }
995                 vprint("Reading %s\n", LAST_RCVD);
996                 ret = fread(&lsd, 1, sizeof(lsd), filep);
997                 if (ret < sizeof(lsd)) {
998                         fprintf(stderr, "%s: Short read (%d of %d)\n",
999                                 progname, ret, (int)sizeof(lsd));
1000                         ret = ferror(filep);
1001                         if (ret)
1002                                 goto out_close;
1003                 }
1004                 vprint("Feature compat=%x, incompat=%x\n",
1005                        lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
1006
1007                 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
1008                     (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
1009                         mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
1010                         mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
1011                 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
1012                            (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
1013                         /* We must co-locate so mgs can see old logs.
1014                            If user doesn't want this, they can copy the old
1015                            logs manually and re-tunefs. */
1016                         mop->mo_ldd.ldd_flags =
1017                                 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
1018                         mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
1019                 } else  {
1020                         /* If neither is set, we're pre-1.4.6, make a guess. */
1021                         /* Construct debugfs command line. */
1022                         snprintf(cmd, cmdsz, "debugfs -c -R 'rdump /%s %s' %s",
1023                                  MDT_LOGS_DIR, tmpdir, dev);
1024                         run_command(cmd, cmdsz);
1025
1026                         sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
1027                         if (lsd.lsd_ost_index > 0) {
1028                                 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
1029                                 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
1030                         } else {
1031                                 /* If there's a LOGS dir, it's an MDT */
1032                                 if ((ret = access(filepnm, F_OK)) == 0) {
1033                                         mop->mo_ldd.ldd_flags =
1034                                         LDD_F_SV_TYPE_MDT |
1035                                         LDD_F_SV_TYPE_MGS;
1036                                         /* Old MDT's are always index 0
1037                                            (pre CMD) */
1038                                         mop->mo_ldd.ldd_svindex = 0;
1039                                 } else {
1040                                         /* The index may not be correct */
1041                                         mop->mo_ldd.ldd_flags =
1042                                         LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
1043                                         verrprint("OST with unknown index\n");
1044                                 }
1045                         }
1046                 }
1047
1048                 ret = 0;
1049                 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
1050                        sizeof(mop->mo_ldd.ldd_uuid));
1051                 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
1052         }
1053         /* end COMPAT_146 */
1054 out_close:
1055         fclose(filep);
1056
1057 out_rmdir:
1058         snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
1059         run_command(cmd, cmdsz);
1060         if (ret)
1061                 verrprint("Failed to read old data (%d)\n", ret);
1062         return ret;
1063 }
1064
1065
1066 void set_defaults(struct mkfs_opts *mop)
1067 {
1068         mop->mo_ldd.ldd_magic = LDD_MAGIC;
1069         mop->mo_ldd.ldd_config_ver = 1;
1070         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
1071         mop->mo_mgs_failnodes = 0;
1072         strcpy(mop->mo_ldd.ldd_fsname, "lustre");
1073         if (get_os_version() == 24)
1074                 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
1075         else
1076                 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
1077
1078         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
1079         mop->mo_stripe_count = 1;
1080 }
1081
1082 static inline void badopt(const char *opt, char *type)
1083 {
1084         fprintf(stderr, "%s: '--%s' only valid for %s\n",
1085                 progname, opt, type);
1086         usage(stderr);
1087 }
1088
1089 static int add_param(char *buf, char *key, char *val)
1090 {
1091         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
1092         int start = strlen(buf);
1093         int keylen = 0;
1094
1095         if (key)
1096                 keylen = strlen(key);
1097         if (start + 1 + keylen + strlen(val) >= end) {
1098                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
1099                         progname, buf, key ? key : "", val);
1100                 return 1;
1101         }
1102
1103         sprintf(buf + start, " %s%s", key ? key : "", val);
1104         return 0;
1105 }
1106
1107 /* from mount_lustre */
1108 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
1109 #define MAXNIDSTR 1024
1110 static char *convert_hostnames(char *s1)
1111 {
1112         char *converted, *s2 = 0, *c, *end, sep;
1113         int left = MAXNIDSTR;
1114         lnet_nid_t nid;
1115
1116         converted = malloc(left);
1117         if (converted == NULL) {
1118                 return NULL;
1119         }
1120
1121         end = s1 + strlen(s1);
1122         c = converted;
1123         while ((left > 0) && (s1 < end)) {
1124                 s2 = strpbrk(s1, ",:");
1125                 if (!s2)
1126                         s2 = end;
1127                 sep = *s2;
1128                 *s2 = '\0';
1129                 nid = libcfs_str2nid(s1);
1130                 
1131                 if (nid == LNET_NID_ANY) {
1132                         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
1133                         free(converted);
1134                         return NULL;
1135                 }
1136                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
1137                             strlen("127.0.0.1")) == 0) {
1138                         fprintf(stderr, "%s: The NID '%s' resolves to the "
1139                                 "loopback address '%s'.  Lustre requires a "
1140                                 "non-loopback address.\n",
1141                                 progname, s1, libcfs_nid2str(nid));
1142                         free(converted);
1143                         return NULL;
1144                 }
1145                                         
1146                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
1147                 left = converted + MAXNIDSTR - c;
1148                 s1 = s2 + 1;
1149         }
1150         return converted;
1151 }
1152
1153 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
1154                char **mountopts)
1155 {
1156         static struct option long_opt[] = {
1157                 {"iam-dir", 0, 0, 'a'},
1158                 {"backfstype", 1, 0, 'b'},
1159                 {"stripe-count-hint", 1, 0, 'c'},
1160                 {"comment", 1, 0, 'u'},
1161                 {"configdev", 1, 0, 'C'},
1162                 {"device-size", 1, 0, 'd'},
1163                 {"dryrun", 0, 0, 'n'},
1164                 {"erase-params", 0, 0, 'e'},
1165                 {"failnode", 1, 0, 'f'},
1166                 {"failover", 1, 0, 'f'},
1167                 {"mgs", 0, 0, 'G'},
1168                 {"help", 0, 0, 'h'},
1169                 {"index", 1, 0, 'i'},
1170                 {"mkfsoptions", 1, 0, 'k'},
1171                 {"mgsnode", 1, 0, 'm'},
1172                 {"mgsnid", 1, 0, 'm'},
1173                 {"mdt", 0, 0, 'M'},
1174                 {"fsname",1, 0, 'L'},
1175                 {"noformat", 0, 0, 'n'},
1176                 {"nomgs", 0, 0, 'N'},
1177                 {"mountfsoptions", 1, 0, 'o'},
1178                 {"ost", 0, 0, 'O'},
1179                 {"param", 1, 0, 'p'},
1180                 {"print", 0, 0, 'n'},
1181                 {"quiet", 0, 0, 'q'},
1182                 {"reformat", 0, 0, 'r'},
1183                 {"verbose", 0, 0, 'v'},
1184                 {"writeconf", 0, 0, 'w'},
1185                 {"upgrade_to_18", 0, 0, 'U'},
1186                 {0, 0, 0, 0}
1187         };
1188         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqru:vw";
1189         int opt;
1190         int rc, longidx;
1191
1192         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
1193                EOF) {
1194                 switch (opt) {
1195                 case 'a': {
1196                         if (IS_MDT(&mop->mo_ldd))
1197                                 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
1198                         break;
1199                 }
1200                 case 'b': {
1201                         int i = 0;
1202                         while (i < LDD_MT_LAST) {
1203                                 if (strcmp(optarg, mt_str(i)) == 0) {
1204                                         mop->mo_ldd.ldd_mount_type = i;
1205                                         break;
1206                                 }
1207                                 i++;
1208                         }
1209                         break;
1210                 }
1211                 case 'c':
1212                         if (IS_MDT(&mop->mo_ldd)) {
1213                                 int stripe_count = atol(optarg);
1214                                 if (stripe_count <= 0) {
1215                                         fprintf(stderr, "%s: bad stripe count "
1216                                                 "%d\n", progname, stripe_count);
1217                                         return 1;
1218                                 }
1219                                 mop->mo_stripe_count = stripe_count;
1220                         } else {
1221                                 badopt(long_opt[longidx].name, "MDT");
1222                                 return 1;
1223                         }
1224                         break;
1225                 case 'C': /* Configdev */
1226                         //FIXME
1227                         printf("Configdev not implemented\n");
1228                         return 1;
1229                 case 'd':
1230                         mop->mo_device_sz = atol(optarg);
1231                         break;
1232                 case 'e':
1233                         mop->mo_ldd.ldd_params[0] = '\0';
1234                         /* Must update the mgs logs */
1235                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1236                         break;
1237                 case 'f': {
1238                         char *nids = convert_hostnames(optarg);
1239                         if (!nids)
1240                                 return 1;
1241                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
1242                                        nids);
1243                         free(nids);
1244                         if (rc)
1245                                 return rc;
1246                         /* Must update the mgs logs */
1247                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1248                         failover = 1;
1249                         break;
1250                 }
1251                 case 'G':
1252                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1253                         break;
1254                 case 'h':
1255                         usage(stdout);
1256                         return 1;
1257                 case 'i':
1258                         if (!(mop->mo_ldd.ldd_flags &
1259                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1260                                LDD_F_WRITECONF))) {
1261                                 fprintf(stderr, "%s: cannot change the index of"
1262                                         " a registered target\n", progname);
1263                                 return 1;
1264                         }
1265                         if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1266                                 mop->mo_ldd.ldd_svindex = atol(optarg);
1267                                 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1268                         } else {
1269                                 badopt(long_opt[longidx].name, "MDT,OST");
1270                                 return 1;
1271                         }
1272                         break;
1273                 case 'k':
1274                         strscpy(mop->mo_mkfsopts, optarg,
1275                                 sizeof(mop->mo_mkfsopts));
1276                         break;
1277                 case 'L': {
1278                         char *tmp;
1279                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
1280                             (!(mop->mo_ldd.ldd_flags &
1281                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1282                                 LDD_F_WRITECONF)))) {
1283                                 fprintf(stderr, "%s: cannot change the name of"
1284                                         " a registered target\n", progname);
1285                                 return 1;
1286                         }
1287                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
1288                                 fprintf(stderr, "%s: filesystem name must be "
1289                                         "1-8 chars\n", progname);
1290                                 return 1;
1291                         }
1292                         if ((tmp = strpbrk(optarg, "/:"))) {
1293                                 fprintf(stderr, "%s: char '%c' not allowed in "
1294                                         "filesystem name\n", progname, *tmp);
1295                                 return 1;
1296                         }
1297                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
1298                                 sizeof(mop->mo_ldd.ldd_fsname));
1299                         break;
1300                 }
1301                 case 'm': {
1302                         char *nids = convert_hostnames(optarg);
1303                         if (!nids)
1304                                 return 1;
1305                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
1306                                        nids);
1307                         free(nids);
1308                         if (rc)
1309                                 return rc;
1310                         mop->mo_mgs_failnodes++;
1311                         break;
1312                 }
1313                 case 'M':
1314                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1315                         break;
1316                 case 'n':
1317                         print_only++;
1318                         break;
1319                 case 'N':
1320                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1321                         break;
1322                 case 'o':
1323                         *mountopts = optarg;
1324                         break;
1325                 case 'O':
1326                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1327                         break;
1328                 case 'p':
1329                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1330                         if (rc)
1331                                 return rc;
1332                         /* Must update the mgs logs */
1333                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1334                         break;
1335                 case 'q':
1336                         verbose--;
1337                         break;
1338                 case 'r':
1339                         mop->mo_flags |= MO_FORCEFORMAT;
1340                         break;
1341                 case 'u':
1342                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
1343                                 sizeof(mop->mo_ldd.ldd_userdata));
1344                         break;
1345                 case 'v':
1346                         verbose++;
1347                         break;
1348                 case 'w':
1349                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1350                         break;
1351                 case 'U':
1352                         upgrade_to_18 = 1;
1353                         break;
1354                 default:
1355                         if (opt != '?') {
1356                                 fatal();
1357                                 fprintf(stderr, "Unknown option '%c'\n", opt);
1358                         }
1359                         return EINVAL;
1360                 }
1361         }//while
1362
1363         /* Last arg is device */
1364         if (optind != argc - 1) {
1365                 fatal();
1366                 fprintf(stderr, "Bad argument: %s\n", argv[optind]);
1367                 return EINVAL;
1368         }
1369
1370         return 0;
1371 }
1372
1373 int main(int argc, char *const argv[])
1374 {
1375         struct mkfs_opts mop;
1376         struct lustre_disk_data *ldd;
1377         char *mountopts = NULL;
1378         char always_mountopts[512] = "";
1379         char default_mountopts[512] = "";
1380         int ret = 0;
1381
1382         if ((progname = strrchr(argv[0], '/')) != NULL)
1383                 progname++;
1384         else
1385                 progname = argv[0];
1386
1387         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1388                 usage(stderr);
1389                 return(EINVAL);
1390         }
1391
1392         memset(&mop, 0, sizeof(mop));
1393         set_defaults(&mop);
1394
1395         /* device is last arg */
1396         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
1397
1398         /* Are we using a loop device? */
1399         ret = is_block(mop.mo_device);
1400         if (ret < 0)
1401                 goto out;
1402         if (ret == 0)
1403                 mop.mo_flags |= MO_IS_LOOP;
1404
1405 #ifdef TUNEFS
1406         /* For tunefs, we must read in the old values before parsing any
1407            new ones. */
1408
1409         /* Check whether the disk has already been formatted by mkfs.lustre */
1410         ret = is_lustre_target(&mop);
1411         if (ret == 0) {
1412                 fatal();
1413                 fprintf(stderr, "Device %s has not been formatted with "
1414                         "mkfs.lustre\n", mop.mo_device);
1415                 ret = ENODEV;
1416                 goto out;
1417         }
1418
1419         ret = read_local_files(&mop);
1420         if (ret) {
1421                 fatal();
1422                 fprintf(stderr, "Failed to read previous Lustre data from %s "
1423                         "(%d)\n", mop.mo_device, ret);
1424                 goto out;
1425         }
1426         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1427             mop.mo_mgs_failnodes++;
1428
1429         if (verbose > 0)
1430                 print_ldd("Read previous values", &(mop.mo_ldd));
1431 #endif
1432
1433         ret = parse_opts(argc, argv, &mop, &mountopts);
1434         if (ret)
1435                 goto out;
1436
1437         ldd = &mop.mo_ldd;
1438
1439         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1440                 fatal();
1441                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1442                 ret = EINVAL;
1443                 goto out;
1444         }
1445
1446         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1447                 fatal();
1448                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1449                 ret = EINVAL;
1450                 goto out;
1451         }
1452
1453         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1454             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1455                 fatal();
1456                 fprintf(stderr, "Can't find the target index, "
1457                         "specify with --index\n");
1458                 ret = EINVAL;
1459                 goto out;
1460         }
1461 #if 0
1462         /*
1463          * Comment out these 2 checks temporarily, since for multi-MDSes
1464          * in single node only 1 mds node could have mgs service
1465          */
1466         if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1467                 verrprint("No management node specified, adding MGS to this "
1468                           "MDT\n");
1469                 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1470         }
1471         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1472                 fatal();
1473                 if (IS_MDT(ldd))
1474                         fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
1475                 else
1476                         fprintf(stderr, "Must specify --mgsnode=\n");
1477                 ret = EINVAL;
1478                 goto out;
1479         }
1480 #endif
1481
1482         /* These are the permanent mount options (always included) */
1483         switch (ldd->ldd_mount_type) {
1484         case LDD_MT_EXT3:
1485         case LDD_MT_LDISKFS:
1486         case LDD_MT_LDISKFS2: {
1487                 sprintf(always_mountopts, "errors=remount-ro");
1488                 if (IS_MDT(ldd) || IS_MGS(ldd))
1489                         strscat(always_mountopts, ",iopen_nopriv,user_xattr",
1490                                 sizeof(always_mountopts));
1491                 if ((get_os_version() == 24) && IS_OST(ldd))
1492                         strscat(always_mountopts, ",asyncdel",
1493                                 sizeof(always_mountopts));
1494                 /* NB: Files created while extents are enabled cannot be read
1495                    if mounted with a kernel that doesn't include the Lustre ldiskfs
1496                    patches! */
1497                 if (IS_OST(ldd) &&
1498                     (ldd->ldd_mount_type == LDD_MT_LDISKFS ||
1499                      ldd->ldd_mount_type == LDD_MT_LDISKFS2)) {
1500                         strscat(default_mountopts, ",extents,mballoc",
1501                                 sizeof(default_mountopts));
1502                 }
1503                 break;
1504         }
1505         case LDD_MT_SMFS: {
1506                 mop.mo_flags |= MO_IS_LOOP;
1507                 sprintf(always_mountopts, "type=ext3,dev=%s",
1508                         mop.mo_device);
1509                 break;
1510         }
1511         default: {
1512                 fatal();
1513                 fprintf(stderr, "unknown fs type %d '%s'\n",
1514                         ldd->ldd_mount_type,
1515                         MT_STR(ldd));
1516                 ret = EINVAL;
1517                 goto out;
1518         }
1519         }
1520
1521         if (mountopts) {
1522                 /* If user specifies mount opts, don't use defaults,
1523                    but always use always_mountopts */
1524                 sprintf(ldd->ldd_mount_opts, "%s,%s",
1525                         always_mountopts, mountopts);
1526         } else {
1527 #ifdef TUNEFS
1528                 if (ldd->ldd_mount_opts[0] == 0)
1529                         /* use the defaults unless old opts exist */
1530 #endif
1531                 {
1532                         sprintf(ldd->ldd_mount_opts, "%s%s",
1533                                 always_mountopts, default_mountopts);
1534                 }
1535         }
1536
1537         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1538                          ldd->ldd_fsname, ldd->ldd_svname);
1539
1540         if (verbose >= 0)
1541                 print_ldd("Permanent disk data", ldd);
1542
1543         if (print_only) {
1544                 printf("exiting before disk write.\n");
1545                 goto out;
1546         }
1547
1548         if (check_mtab_entry(mop.mo_device))
1549                 return(EEXIST);
1550
1551         /* Create the loopback file */
1552         if (mop.mo_flags & MO_IS_LOOP) {
1553                 ret = access(mop.mo_device, F_OK);
1554                 if (ret)
1555                         ret = errno;
1556 #ifndef TUNEFS /* mkfs.lustre */
1557                 /* Reformat the loopback file */
1558                 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1559                         ret = loop_format(&mop);
1560 #endif
1561                 if (ret == 0)
1562                         ret = loop_setup(&mop);
1563                 if (ret) {
1564                         fatal();
1565                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
1566                                 mop.mo_device, strerror(ret));
1567                         goto out;
1568                 }
1569         }
1570
1571 #ifndef TUNEFS /* mkfs.lustre */
1572         /* Check whether the disk has already been formatted by mkfs.lustre */
1573         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1574                 ret = is_lustre_target(&mop);
1575                 if (ret) {
1576                         fatal();
1577                         fprintf(stderr, "Device %s was previously formatted "
1578                                 "for lustre. Use --reformat to reformat it, "
1579                                 "or tunefs.lustre to modify.\n",
1580                                 mop.mo_device);
1581                         goto out;
1582                 }
1583         }
1584
1585         /* Format the backing filesystem */
1586         ret = make_lustre_backfs(&mop);
1587         if (ret != 0) {
1588                 fatal();
1589                 fprintf(stderr, "mkfs failed %d\n", ret);
1590                 goto out;
1591         }
1592 #endif
1593
1594         /* Write our config files */
1595         ret = write_local_files(&mop);
1596         if (ret != 0) {
1597                 fatal();
1598                 fprintf(stderr, "failed to write local files\n");
1599                 goto out;
1600         }
1601
1602 out:
1603         loop_cleanup(&mop);
1604
1605         /* Fix any crazy return values from system() */
1606         if (ret && ((ret & 255) == 0))
1607                 return (1);
1608         if (ret)
1609                 verrprint("%s: exiting with %d (%s)\n",
1610                           progname, ret, strerror(ret));
1611         return (ret);
1612 }