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