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