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