Whamcloud - gitweb
iam: add lvar format
[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) 2002 Cluster File Systems, Inc.
5  *   Author: Lin Song Tao <lincent@clusterfs.com>
6  *   Author: Nathan Rutman <nathan@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
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 #include <linux/types.h>
41 //#define HAVE_SYS_VFS_H 1
42 #include <linux/fs.h> // for BLKGETSIZE64
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
53 static char *progname;
54 static int verbose = 1;
55 static int print_only = 0;
56
57
58 void usage(FILE *out)
59 {
60         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
61         fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
62         fprintf(out,
63                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
64                 "\ttarget types:\n"
65                 "\t\t--ost: object storage, mutually exclusive with mdt\n"
66                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
67                 "\t\t--mgs: configuration management service - one per site\n"
68                 "\toptions (in order of popularity):\n"
69                 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
70                 "\t\t\trequired for all targets other than the mgs node\n"
71                 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
72                 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
73                 "\t\t--param <key>=<value> : set a permanent parameter\n"
74                 "\t\t--index=#N : target index\n"
75                 /* FIXME implement 1.6.x
76                 "\t\t--configdev=<altdevice|file>: store configuration info\n"
77                 "\t\t\tfor this device on an alternate device\n"
78                 */
79                 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
80                 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
81                 "\t\t--device-size=#N(KB) : device size for loop devices\n"
82 #ifndef TUNEFS
83                 "\t\t--mkfsoptions=<opts> : format options\n"
84                 "\t\t--reformat: overwrite an existing disk\n"
85                 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
86 #else
87                 "\t\t--erase-params : erase all old parameter settings\n"
88                 "\t\t--nomgs: turn off MGS service on this MDT\n"
89                 "\t\t--writeconf: erase all config logs for this fs.\n"
90 #endif
91                 "\t\t--print: just report what we would do; don't write to "
92                 "disk\n"
93                 "\t\t--verbose\n"
94                 "\t\t--quiet\n");
95         return;
96 }
97
98 #define vprint if (verbose > 0) printf
99
100 static void fatal(void)
101 {
102         verbose = 0;
103         fprintf(stderr, "\n%s FATAL: ", progname);
104 }
105
106 /*================ utility functions =====================*/
107
108 inline unsigned int
109 dev_major (unsigned long long int __dev)
110 {
111         return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
112 }
113
114 inline unsigned int
115 dev_minor (unsigned long long int __dev)
116 {
117         return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
118 }
119
120 int get_os_version()
121 {
122         static int version = 0;
123
124         if (!version) {
125                 int fd;
126                 char release[4] = "";
127
128                 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
129                 if (fd < 0)
130                         fprintf(stderr, "%s: Warning: Can't resolve kernel "
131                                 "version, assuming 2.6\n", progname);
132                 else {
133                         read(fd, release, 4);
134                         close(fd);
135                 }
136                 if (strncmp(release, "2.4.", 4) == 0)
137                         version = 24;
138                 else
139                         version = 26;
140         }
141         return version;
142 }
143
144 int run_command(char *cmd)
145 {
146         char log[] = "/tmp/mkfs_logXXXXXX";
147         int fd, rc;
148
149         if (verbose > 1)
150                 printf("cmd: %s\n", cmd);
151
152         if ((fd = mkstemp(log)) >= 0) {
153                 close(fd);
154                 strcat(cmd, " >");
155                 strcat(cmd, log);
156         }
157         strcat(cmd, " 2>&1");
158
159         /* Can't use popen because we need the rv of the command */
160         rc = system(cmd);
161         if (rc && fd >= 0) {
162                 char buf[128];
163                 FILE *fp;
164                 fp = fopen(log, "r");
165                 if (fp) {
166                         while (fgets(buf, sizeof(buf), fp) != NULL) {
167                                 if (rc || verbose > 2)
168                                         printf("   %s", buf);
169                         }
170                         fclose(fp);
171                 }
172         }
173         if (fd >= 0)
174                 remove(log);
175         return rc;
176 }
177
178 static int check_mtab_entry(char *spec, char *type)
179 {
180         FILE *fp;
181         struct mntent *mnt;
182
183         fp = setmntent(MOUNTED, "r");
184         if (fp == NULL)
185                 return(0);
186
187         while ((mnt = getmntent(fp)) != NULL) {
188                 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
189                         strcmp(mnt->mnt_type, type) == 0) {
190                         endmntent(fp);
191                         fprintf(stderr, "%s: according to %s %s is "
192                                 "already mounted on %s\n",
193                                 progname, MOUNTED, spec, mnt->mnt_dir);
194                         return(EEXIST);
195                 }
196         }
197         endmntent(fp);
198
199         return(0);
200 }
201
202 /*============ disk dev functions ===================*/
203
204 /* Setup a file in the first unused loop_device */
205 int loop_setup(struct mkfs_opts *mop)
206 {
207         char loop_base[20];
208         char l_device[64];
209         int i,ret = 0;
210
211         /* Figure out the loop device names */
212         if (!access("/dev/loop0", F_OK | R_OK))
213                 strcpy(loop_base, "/dev/loop\0");
214         else if (!access("/dev/loop/0", F_OK | R_OK))
215                 strcpy(loop_base, "/dev/loop/\0");
216         else {
217                 fprintf(stderr, "%s: can't access loop devices\n", progname);
218                 return 1;
219         }
220
221         /* Find unused loop device */
222         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
223                 char cmd[128];
224                 sprintf(l_device, "%s%d", loop_base, i);
225                 if (access(l_device, F_OK | R_OK))
226                         break;
227                 sprintf(cmd, "losetup %s > /dev/null 2>&1", l_device);
228                 ret = system(cmd);
229                 /* losetup gets 1 (ret=256) for non-set-up device */
230                 if (ret) {
231                         /* Set up a loopback device to our file */
232                         sprintf(cmd, "losetup %s %s", l_device, mop->mo_device);
233                         ret = run_command(cmd);
234                         if (ret) {
235                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
236                                         progname, ret, strerror(ret));
237                                 return ret;
238                         }
239                         strcpy(mop->mo_loopdev, l_device);
240                         return ret;
241                 }
242         }
243
244         fprintf(stderr, "%s: out of loop devices!\n", progname);
245         return EMFILE;
246 }
247
248 int loop_cleanup(struct mkfs_opts *mop)
249 {
250         char cmd[128];
251         int ret = 1;
252         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
253                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
254                 ret = run_command(cmd);
255         }
256         return ret;
257 }
258
259 /* Determine if a device is a block device (as opposed to a file) */
260 int is_block(char* devname)
261 {
262         struct stat st;
263         int ret = 0;
264
265         ret = access(devname, F_OK);
266         if (ret != 0)
267                 return 0;
268         ret = stat(devname, &st);
269         if (ret != 0) {
270                 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
271                 return -1;
272         }
273         return S_ISBLK(st.st_mode);
274 }
275
276 __u64 get_device_size(char* device)
277 {
278         int ret, fd;
279         __u64 size = 0;
280
281         fd = open(device, O_RDONLY);
282         if (fd < 0) {
283                 fprintf(stderr, "%s: cannot open %s: %s\n",
284                         progname, device, strerror(errno));
285                 return 0;
286         }
287
288         /* size in bytes. bz5831 */
289         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
290         close(fd);
291         if (ret < 0) {
292                 fprintf(stderr, "%s: size ioctl failed: %s\n",
293                         progname, strerror(errno));
294                 return 0;
295         }
296
297         vprint("device size = "LPU64"MB\n", size >> 20);
298         /* return value in KB */
299         return size >> 10;
300 }
301
302 int loop_format(struct mkfs_opts *mop)
303 {
304         int ret = 0;
305
306         if (mop->mo_device_sz == 0) {
307                 fatal();
308                 fprintf(stderr, "loop device requires a --device-size= "
309                         "param\n");
310                 return EINVAL;
311         }
312
313         ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
314         ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
315         if (ret != 0) {
316                 ret = errno;
317                 fprintf(stderr, "%s: Unable to create backing store: %d\n",
318                         progname, ret);
319         }
320
321         return ret;
322 }
323
324 /* Check whether the file exists in the device */
325 static int file_in_dev(char *file_name, char *dev_name)
326 {
327         FILE *fp;
328         char debugfs_cmd[256];
329         unsigned int inode_num;
330         int i;
331
332         /* Construct debugfs command line. */
333         memset(debugfs_cmd, 0, sizeof(debugfs_cmd));
334         sprintf(debugfs_cmd,
335                 "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
336                 file_name, dev_name);
337
338         fp = popen(debugfs_cmd, "r");
339         if (!fp) {
340                 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
341                 return 0;
342         }
343
344         if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
345                 pclose(fp);
346                 return 1;
347         }
348         i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
349         if (i) {
350                 /* Filesystem has unsupported feature */
351                 vprint("%.*s", i, debugfs_cmd);
352                 /* in all likelihood, the "unsupported feature" is
353                   'extents', which older debugfs does not understand.
354                   Use e2fsprogs-1.38-cfs1 or later, available from
355                   ftp://ftp.lustre.org/pub/lustre/other/e2fsprogs/ */
356                 return -1;
357         }
358         pclose(fp);
359         return 0;
360 }
361
362 /* Check whether the device has already been used with lustre */
363 static int is_lustre_target(struct mkfs_opts *mop)
364 {
365         int rc;
366         vprint("checking for existing Lustre data\n");
367
368         if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))
369             || (rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
370                 vprint("found Lustre data\n");
371                 /* in the -1 case, 'extents' means this really IS a lustre
372                    target */
373                 return rc;
374         }
375
376         return 0; /* The device is not a lustre target. */
377 }
378
379 /* Build fs according to type */
380 int make_lustre_backfs(struct mkfs_opts *mop)
381 {
382         char mkfs_cmd[512];
383         char buf[40];
384         char *dev;
385         int ret = 0;
386         int block_count = 0;
387
388         if (mop->mo_device_sz != 0) {
389                 if (mop->mo_device_sz < 8096){
390                         fprintf(stderr, "%s: size of filesystem must be larger "
391                                 "than 8MB, but is set to %lldKB\n",
392                                 progname, mop->mo_device_sz);
393                         return EINVAL;
394                 }
395                 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
396         }
397
398         if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
399             (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS)) {
400                 __u64 device_sz = mop->mo_device_sz;
401
402                 /* we really need the size */
403                 if (device_sz == 0) {
404                         device_sz = get_device_size(mop->mo_device);
405                         if (device_sz == 0)
406                                 return ENODEV;
407                 }
408
409                 /* Journal size in MB */
410                 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
411                         /* Choose our own default journal size */
412                         long journal_sz = 0, max_sz;
413                         if (device_sz > 1024 * 1024) /* 1GB */
414                                 journal_sz = (device_sz / 102400) * 4;
415                         /* man mkfs.ext3 */
416                         max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
417                         if (journal_sz > max_sz)
418                                 journal_sz = max_sz;
419                         if (journal_sz) {
420                                 sprintf(buf, " -J size=%ld", journal_sz);
421                                 strcat(mop->mo_mkfsopts, buf);
422                         }
423                 }
424
425                 /* Default bytes_per_inode is block size */
426                 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
427                         long bytes_per_inode = 0;
428
429                         if (IS_MDT(&mop->mo_ldd))
430                                 bytes_per_inode = 4096;
431
432                         /* Allocate fewer inodes on large OST devices.  Most
433                            filesystems can be much more aggressive than even
434                            this. */
435                         if ((IS_OST(&mop->mo_ldd) && (device_sz > 1000000)))
436                                 bytes_per_inode = 16384;
437
438                         if (bytes_per_inode > 0) {
439                                 sprintf(buf, " -i %ld", bytes_per_inode);
440                                 strcat(mop->mo_mkfsopts, buf);
441                         }
442                 }
443
444                 /* This is an undocumented mke2fs option. Default is 128. */
445                 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
446                         long inode_size = 0;
447                         if (IS_MDT(&mop->mo_ldd)) {
448                                 if (mop->mo_stripe_count > 77)
449                                         inode_size = 512; /* bz 7241 */
450                                 /* cray stripes across all osts (>60) */
451                                 else if (mop->mo_stripe_count > 34)
452                                         inode_size = 2048;
453                                 else if (mop->mo_stripe_count > 13)
454                                         inode_size = 1024;
455                                 else
456                                         inode_size = 512;
457                         } else if (IS_OST(&mop->mo_ldd)) {
458                                 /* now as we store fids in EA on OST we need
459                                    to make inode bigger */
460                                 inode_size = 256;
461                         }
462
463                         if (inode_size > 0) {
464                                 sprintf(buf, " -I %ld", inode_size);
465                                 strcat(mop->mo_mkfsopts, buf);
466                         }
467
468                 }
469
470                 if (verbose < 2) {
471                         strcat(mop->mo_mkfsopts, " -q");
472                 }
473
474                 /* Enable hashed b-tree directory lookup in large dirs bz6224 */
475                 if (strstr(mop->mo_mkfsopts, "-O") == NULL) {
476                         strcat(mop->mo_mkfsopts, " -O dir_index");
477                 }
478
479                 /* Allow reformat of full devices (as opposed to
480                    partitions.)  We already checked for mounted dev. */
481                 strcat(mop->mo_mkfsopts, " -F");
482
483                 sprintf(mkfs_cmd, "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
484                         mop->mo_ldd.ldd_svname);
485
486         } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
487                 long journal_sz = 0; /* FIXME default journal size */
488                 if (journal_sz > 0) {
489                         sprintf(buf, " --journal_size %ld", journal_sz);
490                         strcat(mop->mo_mkfsopts, buf);
491                 }
492                 sprintf(mkfs_cmd, "mkreiserfs -ff ");
493
494         } else {
495                 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
496                         progname, mop->mo_ldd.ldd_mount_type,
497                         MT_STR(&mop->mo_ldd));
498                 return EINVAL;
499         }
500
501         /* For loop device format the dev, not the filename */
502         dev = mop->mo_device;
503         if (mop->mo_flags & MO_IS_LOOP)
504                 dev = mop->mo_loopdev;
505
506         vprint("formatting backing filesystem %s on %s\n",
507                MT_STR(&mop->mo_ldd), dev);
508         vprint("\ttarget name  %s\n", mop->mo_ldd.ldd_svname);
509         vprint("\t4k blocks     %d\n", block_count);
510         vprint("\toptions       %s\n", mop->mo_mkfsopts);
511
512         /* mkfs_cmd's trailing space is important! */
513         strcat(mkfs_cmd, mop->mo_mkfsopts);
514         strcat(mkfs_cmd, " ");
515         strcat(mkfs_cmd, dev);
516         if (block_count != 0) {
517                 sprintf(buf, " %d", block_count);
518                 strcat(mkfs_cmd, buf);
519         }
520
521         vprint("mkfs_cmd = %s\n", mkfs_cmd);
522         ret = run_command(mkfs_cmd);
523         if (ret) {
524                 fatal();
525                 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
526                 goto out;
527         }
528
529 out:
530         return ret;
531 }
532
533 /* ==================== Lustre config functions =============*/
534
535 void print_ldd(char *str, struct lustre_disk_data *ldd)
536 {
537         printf("\n   %s:\n", str);
538         printf("Target:     %s\n", ldd->ldd_svname);
539         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
540                 printf("Index:      unassigned\n");
541         else
542                 printf("Index:      %d\n", ldd->ldd_svindex);
543         printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
544         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
545         printf("Mount type: %s\n", MT_STR(ldd));
546         printf("Flags:      %#x\n", ldd->ldd_flags);
547         printf("              (%s%s%s%s%s%s%s%s)\n",
548                IS_MDT(ldd) ? "MDT ":"",
549                IS_OST(ldd) ? "OST ":"",
550                IS_MGS(ldd) ? "MGS ":"",
551                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
552                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
553                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
554                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
555                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
556         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
557         printf("Parameters:%s\n", ldd->ldd_params);
558         printf("\n");
559 }
560
561 /* Write the server config files */
562 int write_local_files(struct mkfs_opts *mop)
563 {
564         char mntpt[] = "/tmp/mntXXXXXX";
565         char filepnm[128];
566         char *dev;
567         FILE *filep;
568         int ret = 0;
569
570         /* Mount this device temporarily in order to write these files */
571         if (!mkdtemp(mntpt)) {
572                 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
573                         progname, mntpt, strerror(errno));
574                 return errno;
575         }
576
577         dev = mop->mo_device;
578         if (mop->mo_flags & MO_IS_LOOP)
579                 dev = mop->mo_loopdev;
580
581         ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
582         if (ret) {
583                 fprintf(stderr, "%s: Unable to mount %s: %s\n",
584                         progname, dev, strerror(errno));
585                 if (errno == ENODEV) {
586                         fprintf(stderr, "Is the %s module available?\n",
587                                 MT_STR(&mop->mo_ldd));
588                 }
589                 goto out_rmdir;
590         }
591
592         /* Set up initial directories */
593         sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
594         ret = mkdir(filepnm, 0777);
595         if ((ret != 0) && (errno != EEXIST)) {
596                 fprintf(stderr, "%s: Can't make configs dir %s (%d)\n",
597                         progname, filepnm, ret);
598                 goto out_umnt;
599         } else if (errno == EEXIST) {
600                 ret = 0;
601         }
602
603         sprintf(filepnm, "%s/%s", mntpt, "ROOT");
604         ret = mkdir(filepnm, 0777);
605         if ((ret != 0) && (errno != EEXIST)) {
606                 fprintf(stderr, "%s: Can't make ROOT dir %s (%d)\n",
607                         progname, filepnm, ret);
608                 goto out_umnt;
609         } else if (errno == EEXIST) {
610                 ret = 0;
611         }
612
613         /* Save the persistent mount data into a file. Lustre must pre-read
614            this file to get the real mount options. */
615         vprint("Writing %s\n", MOUNT_DATA_FILE);
616         sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
617         filep = fopen(filepnm, "w");
618         if (!filep) {
619                 fprintf(stderr, "%s: Unable to create %s file\n",
620                         progname, filepnm);
621                 goto out_umnt;
622         }
623         fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
624         fclose(filep);
625         
626         /* COMPAT_146 */
627 #ifdef TUNEFS
628         /* Check for upgrade */
629         if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) 
630             == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
631                 char cmd[128];
632                 char *term;
633                 vprint("Copying old logs\n");
634 #if 0
635  /* Generate new client log as servers upgrade.  Starting a new client 
636     may end up with short lov's, so will be degraded until all servers
637     upgrade */
638                 /* Copy the old client log to fsname-client */
639                 sprintf(filepnm, "%s/%s/%s-client", 
640                         mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
641                 sprintf(cmd, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
642                         filepnm);
643                 if (verbose > 1) 
644                         printf("cmd: %s\n", cmd);
645                 ret = run_command(cmd);
646                 if (ret) {
647                         fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
648                                 "(%d)\n", progname, MDT_LOGS_DIR, ret);
649                         fprintf(stderr, "mount -t ext3 %s somewhere, "
650                                 "find the client log for fs %s and "
651                                 "copy it manually into %s/%s-client, "
652                                 "then umount.\n",
653                                 mop->mo_device, 
654                                 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
655                                 mop->mo_ldd.ldd_fsname);
656                         goto out_umnt;
657                 }
658  #endif
659                 /* We need to use the old mdt log because otherwise mdt won't
660                    have complete lov if old clients connect before all 
661                    servers upgrade. */
662                 /* Copy the old mdt log to fsname-MDT0000 (get old
663                    name from mdt_UUID) */
664                 ret = 1;
665                 strcpy(filepnm, mop->mo_ldd.ldd_uuid);
666                 term = strstr(filepnm, "_UUID");
667                 if (term) {
668                         *term = '\0';
669                         sprintf(cmd, "cp %s/%s/%s %s/%s/%s",
670                                 mntpt, MDT_LOGS_DIR, filepnm, 
671                                 mntpt, MOUNT_CONFIGS_DIR,
672                                 mop->mo_ldd.ldd_svname);
673                         if (verbose > 1) 
674                                 printf("cmd: %s\n", cmd);
675                         ret = run_command(cmd);
676                 }
677                 if (ret) {
678                         fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
679                                 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
680                         fprintf(stderr, "mount -t ext3 %s somewhere, "
681                                 "find the MDT log for fs %s and "
682                                 "copy it manually into %s/%s, "
683                                 "then umount.\n",
684                                 mop->mo_device, 
685                                 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
686                                 mop->mo_ldd.ldd_svname);
687                         goto out_umnt;
688                 }
689         }
690 #endif
691         /* end COMPAT_146 */
692
693
694 out_umnt:
695         umount(mntpt);    
696 out_rmdir:
697         rmdir(mntpt);
698         return ret;
699 }
700
701 int read_local_files(struct mkfs_opts *mop)
702 {
703         char mntpt[] = "/tmp/mntXXXXXX";
704         char filepnm[128];
705         char *dev;
706         FILE *filep;
707         int ret = 0;
708
709         /* Mount this device temporarily in order to read these files */
710         if (!mkdtemp(mntpt)) {
711                 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
712                         progname, mntpt, strerror(errno));
713                 return errno;
714         }
715
716         dev = mop->mo_device;
717         if (mop->mo_flags & MO_IS_LOOP) 
718                 dev = mop->mo_loopdev;
719         
720         ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
721         if (ret) {
722                 fprintf(stderr, "%s: Unable to mount %s: %s\n", 
723                         progname, dev, strerror(errno));
724                 goto out_rmdir;
725         }
726
727         sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
728         filep = fopen(filepnm, "r");
729         if (filep) {
730                 vprint("Reading %s\n", MOUNT_DATA_FILE);
731                 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
732         } else {
733                 /* COMPAT_146 */
734                 /* Try to read pre-1.6 config from last_rcvd */
735                 struct lr_server_data lsd;
736                 vprint("%s: Unable to read %s, trying last_rcvd\n",
737                        progname, MOUNT_DATA_FILE);
738                 sprintf(filepnm, "%s/%s", mntpt, LAST_RCVD);
739                 filep = fopen(filepnm, "r");
740                 if (!filep) {
741                         fprintf(stderr, "%s: Unable to read old data\n",
742                                 progname);
743                         ret = -errno;
744                         goto out_umnt;
745                 }
746                 vprint("Reading %s\n", LAST_RCVD);
747                 ret = fread(&lsd, 1, sizeof(lsd), filep);
748                 if (ret < sizeof(lsd)) {
749                         fprintf(stderr, "%s: Short read (%d of %d)\n",
750                                 progname, ret, sizeof(lsd));
751                         ret = -ferror(filep);
752                         if (ret) 
753                                 goto out_close;
754                 }
755                 ret = 0;
756                 if (lsd.lsd_feature_compat & OBD_COMPAT_OST) {
757                         mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
758                         mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
759                 } else if (lsd.lsd_feature_compat & OBD_COMPAT_MDT) {
760                         /* We must co-locate so mgs can see old logs.
761                            If user doesn't want this, they can copy the old
762                            logs manually and re-tunefs. */
763                         mop->mo_ldd.ldd_flags = 
764                                 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
765                         mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
766                 } else  {
767                         /* If neither is set, we're pre-1.4.6, make a guess. */
768                         sprintf(filepnm, "%s/%s", mntpt, MDT_LOGS_DIR);
769                         if (lsd.lsd_ost_index > 0) {
770                                 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
771                                 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
772                         } else {
773                                 /* If there's a LOGS dir, it's an MDT */
774                                 if ((ret = access(filepnm, F_OK)) == 0) {
775                                         mop->mo_ldd.ldd_flags =
776                                         LDD_F_SV_TYPE_MDT | 
777                                         LDD_F_SV_TYPE_MGS;
778                                         /* Old MDT's are always index 0 
779                                            (pre CMD) */
780                                         mop->mo_ldd.ldd_svindex = 0;
781                                 } else {
782                                         /* The index won't be correct */
783                                         mop->mo_ldd.ldd_flags =
784                                         LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
785                                 }
786                         }
787                 }
788
789                 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid, 
790                        sizeof(mop->mo_ldd.ldd_uuid));
791                 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
792         }
793         /* end COMPAT_146 */
794 out_close:        
795         fclose(filep);
796         
797 out_umnt:
798         umount(mntpt);    
799 out_rmdir:
800         rmdir(mntpt);
801         return ret;
802 }
803
804
805 void set_defaults(struct mkfs_opts *mop)
806 {
807         mop->mo_ldd.ldd_magic = LDD_MAGIC;
808         mop->mo_ldd.ldd_config_ver = 1;
809         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
810         mop->mo_mgs_failnodes = 0;
811         strcpy(mop->mo_ldd.ldd_fsname, "lustre");
812         if (get_os_version() == 24) 
813                 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
814         else 
815                 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
816         
817         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
818         mop->mo_stripe_count = 1;
819 }
820
821 static inline void badopt(const char *opt, char *type)
822 {
823         fprintf(stderr, "%s: '--%s' only valid for %s\n",
824                 progname, opt, type);
825         usage(stderr);
826 }
827
828 static int add_param(char *buf, char *key, char *val)
829 {
830         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
831         int start = strlen(buf);
832         int keylen = 0;
833
834         if (key) 
835                 keylen = strlen(key);
836         if (start + 1 + keylen + strlen(val) >= end) {
837                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
838                         progname, buf, key ? key : "", val);
839                 return 1;
840         }
841
842         sprintf(buf + start, " %s%s", key ? key : "", val);
843         return 0;
844 }
845
846 /* from mount_lustre */
847 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
848 #define MAXNIDSTR 1024
849 static char *convert_hostnames(char *s1)
850 {
851         char *converted, *s2 = 0, *c;
852         int left = MAXNIDSTR;
853         lnet_nid_t nid;
854         
855         converted = malloc(left);
856         c = converted;
857         while ((left > 0) && ((s2 = strsep(&s1, ",: \0")))) {
858                 nid = libcfs_str2nid(s2);
859                 if (nid == LNET_NID_ANY) {
860                         if (*s2 == '/') 
861                                 /* end of nids */
862                                 break;
863                         fprintf(stderr, "%s: Can't parse NID '%s'\n", 
864                                 progname, s2);
865                         free(converted);
866                         return NULL;
867                 }
868                 if (LNET_NETTYP(LNET_NIDNET(nid)) == SOCKLND) {
869                         __u32 addr = LNET_NIDADDR(nid);
870                         c += snprintf(c, left, "%u.%u.%u.%u@%s%u,",
871                                       (addr >> 24) & 0xff, (addr >> 16) & 0xff,
872                                       (addr >> 8) & 0xff, addr & 0xff,
873                                       libcfs_lnd2str(SOCKLND), 
874                                       LNET_NETNUM(LNET_NIDNET(nid)));
875                 } else {
876                         c += snprintf(c, left, "%s,", s2);
877                 }
878                 left = converted + MAXNIDSTR - c;
879         }
880         *(c - 1) = '\0';
881         return converted;
882 }
883
884 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
885                char **mountopts)
886 {
887         static struct option long_opt[] = {
888                 {"backfstype", 1, 0, 'b'},
889                 {"stripe-count-hint", 1, 0, 'c'},
890                 {"configdev", 1, 0, 'C'},
891                 {"device-size", 1, 0, 'd'},
892                 {"erase-params", 0, 0, 'e'},
893                 {"failnode", 1, 0, 'f'},
894                 {"failover", 1, 0, 'f'},
895                 {"mgs", 0, 0, 'G'},
896                 {"help", 0, 0, 'h'},
897                 {"index", 1, 0, 'i'},
898                 {"mkfsoptions", 1, 0, 'k'},
899                 {"mgsnode", 1, 0, 'm'},
900                 {"mgsnid", 1, 0, 'm'},
901                 {"mdt", 0, 0, 'M'},
902                 {"fsname",1, 0, 'n'},
903                 {"nomgs", 0, 0, 'N'},
904                 {"mountfsoptions", 1, 0, 'o'},
905                 {"ost", 0, 0, 'O'},
906                 {"param", 1, 0, 'p'},
907                 {"print", 0, 0, 'P'},
908                 {"quiet", 0, 0, 'q'},
909                 {"reformat", 0, 0, 'r'},
910                 {"verbose", 0, 0, 'v'},
911                 {"writeconf", 0, 0, 'w'},
912                 {0, 0, 0, 0}
913         };
914         char *optstring = "b:c:C:d:ef:Ghi:k:m:Mn:No:Op:Pqrvw";
915         char opt;
916         int rc, longidx;
917
918         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) != 
919                EOF) {
920                 switch (opt) {
921                 case 'b': {
922                         int i = 0;
923                         while (i < LDD_MT_LAST) {
924                                 if (strcmp(optarg, mt_str(i)) == 0) {
925                                         mop->mo_ldd.ldd_mount_type = i;
926                                         break;
927                                 }
928                                 i++;
929                         }
930                         break;
931                 }
932                 case 'c':
933                         if (IS_MDT(&mop->mo_ldd)) {
934                                 int stripe_count = atol(optarg);
935                                 if (stripe_count <= 0) {
936                                         fprintf(stderr, "%s: bad stripe count "
937                                                 "%d\n", progname, stripe_count);
938                                         return 1;
939                                 }
940                                 mop->mo_stripe_count = stripe_count;
941                         } else {
942                                 badopt(long_opt[longidx].name, "MDT");
943                                 return 1;
944                         }
945                         break;
946                 case 'C': /* Configdev */
947                         //FIXME
948                         printf("Configdev not implemented\n");
949                         return 1;
950                 case 'd':
951                         mop->mo_device_sz = atol(optarg); 
952                         break;
953                 case 'e':
954                         mop->mo_ldd.ldd_params[0] = '\0';
955                         break;
956                 case 'f': {
957                         char *nids = convert_hostnames(optarg);
958                         if (!nids) 
959                                 return 1;
960                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE, 
961                                        nids); 
962                         free(nids);
963                         if (rc) 
964                                 return rc;
965                         break;
966                 }
967                 case 'G':
968                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
969                         break;
970                 case 'h':
971                         usage(stdout);
972                         return 1;
973                 case 'i':
974                         if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
975                                 mop->mo_ldd.ldd_svindex = atol(optarg);
976                                 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
977                         } else {
978                                 badopt(long_opt[longidx].name, "MDT,OST");
979                                 return 1;
980                         }
981                         break;
982                 case 'k':
983                         strncpy(mop->mo_mkfsopts, optarg, 
984                                 sizeof(mop->mo_mkfsopts) - 1);
985                         break;
986                 case 'm': {
987                         char *nids = convert_hostnames(optarg);
988                         if (!nids) 
989                                 return 1;
990                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE, 
991                                        nids); 
992                         free(nids);
993                         if (rc) 
994                                 return rc;
995                         mop->mo_mgs_failnodes++;
996                         break;
997                 }
998                 case 'M':
999                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1000                         break;
1001                 case 'n':
1002                         if (!(IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd))) {
1003                                 badopt(long_opt[longidx].name, "MDT,OST");
1004                                 return 1;
1005                         }
1006                         if (strlen(optarg) > 8) {
1007                                 fprintf(stderr, "%s: filesystem name must be "
1008                                         "<= 8 chars\n", progname);
1009                                 return 1;
1010                         }
1011                         if (optarg[0] != 0) 
1012                                 strncpy(mop->mo_ldd.ldd_fsname, optarg, 
1013                                         sizeof(mop->mo_ldd.ldd_fsname) - 1);
1014                         break;
1015                 case 'N':
1016                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1017                         break;
1018                 case 'o':
1019                         *mountopts = optarg;
1020                         break;
1021                 case 'O':
1022                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1023                         break;
1024                 case 'p':
1025                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1026                         if (rc) 
1027                                 return rc;
1028                         break;
1029                 case 'P':
1030                         print_only++;
1031                         break;
1032                 case 'q':
1033                         verbose--;
1034                         break;
1035                 case 'r':
1036                         mop->mo_flags |= MO_FORCEFORMAT;
1037                         break;
1038                 case 'v':
1039                         verbose++;
1040                         break;
1041                 case 'w':
1042                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1043                         break;
1044                 default:
1045                         if (opt != '?') {
1046                                 fatal();
1047                                 fprintf(stderr, "Unknown option '%c'\n", opt);
1048                         }
1049                         usage(stderr);
1050                         return 1;
1051                 }
1052         }//while
1053         if (optind >= argc) {
1054                 fatal();
1055                 fprintf(stderr, "Bad arguments\n");
1056                 usage(stderr);
1057                 return 1;
1058         }
1059
1060         return 0;
1061 }
1062
1063 int main(int argc, char *const argv[])
1064 {
1065         struct mkfs_opts mop;
1066         struct lustre_disk_data *ldd;
1067         char *mountopts = NULL;
1068         char always_mountopts[512] = "";
1069         char default_mountopts[512] = "";
1070         int  ret = 0;
1071
1072         //printf("pad %d\n", offsetof(struct lustre_disk_data, ldd_padding));
1073         assert(offsetof(struct lustre_disk_data, ldd_padding) == 200);
1074         
1075         if ((progname = strrchr(argv[0], '/')) != NULL)
1076                 progname++;
1077         else
1078                 progname = argv[0];
1079
1080         if (argc < 2) {
1081                 usage(stderr);
1082                 ret = 1;
1083                 goto out;
1084         }
1085
1086         memset(&mop, 0, sizeof(mop));
1087         set_defaults(&mop);
1088
1089         /* device is last arg */
1090         strcpy(mop.mo_device, argv[argc - 1]);
1091
1092         if (check_mtab_entry(mop.mo_device, "lustre"))
1093                 return(EEXIST);
1094
1095         /* Are we using a loop device? */
1096         ret = is_block(mop.mo_device);
1097         if (ret < 0) 
1098                 goto out;
1099         if (ret == 0) 
1100                 mop.mo_flags |= MO_IS_LOOP;
1101
1102 #ifdef TUNEFS
1103         /* For tunefs, we must read in the old values before parsing any
1104            new ones. */
1105         /* Create the loopback file */
1106         if (mop.mo_flags & MO_IS_LOOP) {
1107                 ret = access(mop.mo_device, F_OK);
1108                 if (ret == 0)  
1109                         ret = loop_setup(&mop);
1110                 if (ret) {
1111                         fatal();
1112                         fprintf(stderr, "Loop device setup for %s failed: %s\n", 
1113                                 mop.mo_device, strerror(ret));
1114                         goto out;
1115                 }
1116         }
1117         
1118         /* Check whether the disk has already been formatted by mkfs.lustre */
1119         ret = is_lustre_target(&mop);
1120         if (ret == 0) {
1121                 fatal();
1122                 fprintf(stderr, "Device %s has not been formatted with "
1123                         "mkfs.lustre\n", mop.mo_device);
1124                 goto out;
1125         }
1126
1127         ret = read_local_files(&mop);
1128         if (ret) {
1129                 fatal();
1130                 fprintf(stderr, "Failed to read previous Lustre data from %s\n",
1131                         mop.mo_device);
1132                 goto out;
1133         }
1134
1135         if (verbose > 0) 
1136                 print_ldd("Read previous values", &(mop.mo_ldd));
1137 #endif
1138
1139         ret = parse_opts(argc, argv, &mop, &mountopts);
1140         if (ret) 
1141                 goto out;
1142
1143         ldd = &mop.mo_ldd;
1144         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1145                 fatal();
1146                 fprintf(stderr, "must set target type :{mdt,ost,mgs}\n");
1147                 usage(stderr);
1148                 ret = 1;
1149                 goto out;
1150         }
1151
1152         if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1153                 vprint("No management node specified, adding MGS to this "
1154                        "MDT\n");
1155                 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1156         }
1157
1158         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1159                 fatal();
1160                 fprintf(stderr, "Must specify either --mgs or --mgsnode\n");
1161                 usage(stderr);
1162                 goto out;
1163         }
1164
1165         /* These are the permanent mount options (always included) */ 
1166         switch (ldd->ldd_mount_type) {
1167         case LDD_MT_EXT3:
1168         case LDD_MT_LDISKFS: {
1169                 sprintf(always_mountopts, "errors=remount-ro");
1170                 if (IS_MDT(ldd) || IS_MGS(ldd))
1171                         strcat(always_mountopts,
1172                                ",iopen_nopriv,user_xattr");
1173                 if ((get_os_version() == 24) && IS_OST(ldd))
1174                         strcat(always_mountopts, ",asyncdel");
1175 #if 0
1176                 /* Files created while extents are enabled cannot be read if
1177                    mounted with a kernel that doesn't include the CFS patches.*/
1178                 if (IS_OST(ldd) && 
1179                     ldd->ldd_mount_type == LDD_MT_LDISKFS) {
1180                         strcat(default_mountopts, ",extents,mballoc");
1181                 }
1182 #endif 
1183                 break;
1184         }
1185         case LDD_MT_SMFS: {
1186                 mop.mo_flags |= MO_IS_LOOP;
1187                 sprintf(always_mountopts, "type=ext3,dev=%s",
1188                         mop.mo_device);
1189                 break;
1190         }
1191         default: {
1192                 fatal();
1193                 fprintf(stderr, "unknown fs type %d '%s'\n",
1194                         ldd->ldd_mount_type,
1195                         MT_STR(ldd));
1196                 ret = EINVAL;
1197                 goto out;
1198         }
1199         }               
1200
1201         if (mountopts) {
1202                 /* If user specifies mount opts, don't use defaults,
1203                    but always use always_mountopts */
1204                 sprintf(ldd->ldd_mount_opts, "%s,%s", 
1205                         always_mountopts, mountopts);
1206         } else {
1207 #ifdef TUNEFS
1208                 if (ldd->ldd_mount_opts[0] == 0) 
1209                         /* use the defaults unless old opts exist */
1210 #endif
1211                 {
1212                         if (default_mountopts[0]) 
1213                                 sprintf(ldd->ldd_mount_opts, "%s,%s", 
1214                                         always_mountopts, default_mountopts);
1215                         else
1216                                 strcpy(ldd->ldd_mount_opts,
1217                                        always_mountopts);
1218                 }
1219         }
1220
1221         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1222                          ldd->ldd_fsname, ldd->ldd_svname);
1223
1224         if (verbose > 0)
1225                 print_ldd("Permanent disk data", ldd);
1226
1227         if (print_only) {
1228                 printf("exiting before disk write.\n");
1229                 goto out;
1230         }
1231
1232 #ifndef TUNEFS /* mkfs.lustre */
1233         /* Create the loopback file of the correct size */
1234         if (mop.mo_flags & MO_IS_LOOP) {
1235                 ret = access(mop.mo_device, F_OK);
1236                 /* Don't destroy the loopback file if no FORCEFORMAT */
1237                 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1238                         ret = loop_format(&mop);
1239                 if (ret == 0)  
1240                         ret = loop_setup(&mop);
1241                 if (ret) {
1242                         fatal();
1243                         fprintf(stderr, "Loop device setup failed: %s\n", 
1244                                 strerror(ret));
1245                         goto out;
1246                 }
1247         }
1248
1249         /* Check whether the disk has already been formatted by mkfs.lustre */
1250         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1251                 ret = is_lustre_target(&mop);
1252                 if (ret) {
1253                         fatal();
1254                         fprintf(stderr, "Device %s was previously formatted " 
1255                                 "for lustre. Use --reformat to reformat it, "
1256                                 "or tunefs.lustre to modify.\n",
1257                                 mop.mo_device);
1258                         goto out;
1259                 }
1260         }
1261
1262         /* Format the backing filesystem */
1263         ret = make_lustre_backfs(&mop);
1264         if (ret != 0) {
1265                 fatal();
1266                 fprintf(stderr, "mkfs failed %d\n", ret);
1267                 goto out;
1268         }
1269 #endif
1270
1271         ret = write_local_files(&mop);
1272         if (ret != 0) {
1273                 fatal();
1274                 fprintf(stderr, "failed to write local files\n");
1275                 goto out;
1276         }
1277
1278 out:
1279         loop_cleanup(&mop);      
1280         return ret;
1281 }