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