-#define vprint if (verbose > 0) printf
-#define verrprint if (verbose >= 0) printf
-
-static void fatal(void)
-{
- verbose = 0;
- fprintf(stderr, "\n%s FATAL: ", progname);
-}
-
-/*================ utility functions =====================*/
-
-char *strscat(char *dst, char *src, int buflen) {
- dst[buflen - 1] = 0;
- if (strlen(dst) + strlen(src) >= buflen) {
- fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
- "\n", buflen, dst, src);
- exit(EOVERFLOW);
- }
- return strcat(dst, src);
-
-}
-
-char *strscpy(char *dst, char *src, int buflen) {
- dst[0] = 0;
- return strscat(dst, src, buflen);
-}
-
-inline unsigned int
-dev_major (unsigned long long int __dev)
-{
- return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
-}
-
-inline unsigned int
-dev_minor (unsigned long long int __dev)
-{
- return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
-}
-
-int get_os_version()
-{
- static int version = 0;
-
- if (!version) {
- int fd;
- char release[4] = "";
-
- fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
- if (fd < 0)
- fprintf(stderr, "%s: Warning: Can't resolve kernel "
- "version, assuming 2.6\n", progname);
- else {
- read(fd, release, 4);
- close(fd);
- }
- if (strncmp(release, "2.4.", 4) == 0)
- version = 24;
- else
- version = 26;
- }
- return version;
-}
-
-int run_command(char *cmd, int cmdsz)
-{
- char log[] = "/tmp/mkfs_logXXXXXX";
- int fd = -1, rc;
-
- if ((cmdsz - strlen(cmd)) < 6) {
- fatal();
- fprintf(stderr, "Command buffer overflow: %.*s...\n",
- cmdsz, cmd);
- return ENOMEM;
- }
-
- if (verbose > 1) {
- printf("cmd: %s\n", cmd);
- } else {
- if ((fd = mkstemp(log)) >= 0) {
- close(fd);
- strcat(cmd, " >");
- strcat(cmd, log);
- }
- }
- strcat(cmd, " 2>&1");
-
- /* Can't use popen because we need the rv of the command */
- rc = system(cmd);
- if (rc && (fd >= 0)) {
- char buf[128];
- FILE *fp;
- fp = fopen(log, "r");
- if (fp) {
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- printf(" %s", buf);
- }
- fclose(fp);
- }
- }
- if (fd >= 0)
- remove(log);
- return rc;
-}
-
-static int check_mtab_entry(char *spec)
-{
- FILE *fp;
- struct mntent *mnt;
-
- fp = setmntent(MOUNTED, "r");
- if (fp == NULL)
- return(0);
-
- while ((mnt = getmntent(fp)) != NULL) {
- if (strcmp(mnt->mnt_fsname, spec) == 0) {
- endmntent(fp);
- fprintf(stderr, "%s: according to %s %s is "
- "already mounted on %s\n",
- progname, MOUNTED, spec, mnt->mnt_dir);
- return(EEXIST);
- }
- }
- endmntent(fp);
-
- return(0);
-}
-
-/*============ disk dev functions ===================*/
-
-/* Setup a file in the first unused loop_device */
-int loop_setup(struct mkfs_opts *mop)
-{
- char loop_base[20];
- char l_device[64];
- int i, ret = 0;
-
- /* Figure out the loop device names */
- if (!access("/dev/loop0", F_OK | R_OK)) {
- strcpy(loop_base, "/dev/loop\0");
- } else if (!access("/dev/loop/0", F_OK | R_OK)) {
- strcpy(loop_base, "/dev/loop/\0");
- } else {
- fprintf(stderr, "%s: can't access loop devices\n", progname);
- return EACCES;
- }
-
- /* Find unused loop device */
- for (i = 0; i < MAX_LOOP_DEVICES; i++) {
- char cmd[PATH_MAX];
- int cmdsz = sizeof(cmd);
-
- sprintf(l_device, "%s%d", loop_base, i);
- if (access(l_device, F_OK | R_OK))
- break;
- snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
- ret = system(cmd);
-
- /* losetup gets 1 (ret=256) for non-set-up device */
- if (ret) {
- /* Set up a loopback device to our file */
- snprintf(cmd, cmdsz, "losetup %s %s", l_device,
- mop->mo_device);
- ret = run_command(cmd, cmdsz);
- if (ret) {
- fprintf(stderr, "%s: error %d on losetup: %s\n",
- progname, ret, strerror(ret));
- return ret;
- }
- strscpy(mop->mo_loopdev, l_device,
- sizeof(mop->mo_loopdev));
- return ret;
- }
- }
-
- fprintf(stderr, "%s: out of loop devices!\n", progname);
- return EMFILE;
-}
-
-int loop_cleanup(struct mkfs_opts *mop)
-{
- char cmd[150];
- int ret = 1;
- if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
- sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
- ret = run_command(cmd, sizeof(cmd));
- }
- return ret;
-}
-
-/* Determine if a device is a block device (as opposed to a file) */
-int is_block(char* devname)
-{
- struct stat st;
- int ret = 0;
-
- ret = access(devname, F_OK);
- if (ret != 0)
- return 0;
- ret = stat(devname, &st);
- if (ret != 0) {
- fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
- return -1;
- }
- return S_ISBLK(st.st_mode);
-}
-
-__u64 get_device_size(char* device)
-{
- int ret, fd;
- __u64 size = 0;
-
- fd = open(device, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "%s: cannot open %s: %s\n",
- progname, device, strerror(errno));
- return 0;
- }
-
-#ifdef BLKGETSIZE64
- /* size in bytes. bz5831 */
- ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
-#else
- {
- __u32 lsize = 0;
- /* size in blocks */
- ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
- size = (__u64)lsize * 512;
- }
-#endif
- close(fd);
- if (ret < 0) {
- fprintf(stderr, "%s: size ioctl failed: %s\n",
- progname, strerror(errno));
- return 0;
- }
-
- vprint("device size = "LPU64"MB\n", size >> 20);
- /* return value in KB */
- return size >> 10;
-}
-
-int loop_format(struct mkfs_opts *mop)
-{
- int ret = 0;
-
- if (mop->mo_device_sz == 0) {
- fatal();
- fprintf(stderr, "loop device requires a --device-size= "
- "param\n");
- return EINVAL;
- }
-
- ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
- if (ret < 0) {
- ret = errno;
- fprintf(stderr, "%s: Unable to create backing store: %d\n",
- progname, ret);
- } else {
- close(ret);
- }
-
- ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
- if (ret != 0) {
- ret = errno;
- fprintf(stderr, "%s: Unable to truncate backing store: %d\n",
- progname, ret);
- }
-
- return ret;
-}
-
-/* Display the need for the latest e2fsprogs to be installed. make_backfs
- * indicates if the caller is make_lustre_backfs() or not. */
-static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
-{
- static int msg_displayed;
-
- if (msg_displayed) {
- fprintf(stderr, "WARNING: e2fsprogs does not support %s "
- "feature.\n\n", feature);
- return;
- }
-
- msg_displayed++;
-
- fprintf(stderr, "WARNING: The e2fsprogs package currently installed on "
- "your system does not support \"%s\" feature.\nPlease install "
- "the latest version of e2fsprogs from http://www.clusterfs.com/"
- "downloads/public/Lustre/Tools/e2fsprogs/\nto enable this "
- "feature.\n", feature);
-
- if (make_backfs)
- fprintf(stderr, "Feature will not be enabled until e2fsprogs "
- "is updated and 'tune2fs -O %s %%{device}' "
- "is run.\n\n", feature);
-}
-
-/* Check whether the file exists in the device */
-static int file_in_dev(char *file_name, char *dev_name)
-{
- FILE *fp;
- char debugfs_cmd[256];
- unsigned int inode_num;
- int i;
-
- /* Construct debugfs command line. */
- snprintf(debugfs_cmd, sizeof(debugfs_cmd),
- "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
- file_name, dev_name);
-
- fp = popen(debugfs_cmd, "r");
- if (!fp) {
- fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- return 0;
- }
-
- if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
- pclose(fp);
- return 1;
- }
- i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
- if (i) {
- debugfs_cmd[i] = 0;
- fprintf(stderr, "%s", debugfs_cmd);
- if (strstr(debugfs_cmd, "unsupported feature")) {
- disp_old_e2fsprogs_msg("an unknown", 0);
- }
- return -1;
- }
- pclose(fp);
- return 0;
-}
-
-/* Check whether the device has already been used with lustre */
-static int is_lustre_target(struct mkfs_opts *mop)
-{
- int rc;
-
- vprint("checking for existing Lustre data: ");
-
- if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))) {
- vprint("found %s\n",
- (rc == 1) ? MOUNT_DATA_FILE : "extents");
- /* in the -1 case, 'extents' means this really IS a lustre
- target */
- return rc;
- }
-
- if ((rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
- vprint("found %s\n", LAST_RCVD);
- return rc;
- }
-
- vprint("not found\n");
- return 0; /* The device is not a lustre target. */
-}
-
-/* Check if a certain feature is supported by e2fsprogs.
- * Firstly we try to use "debugfs supported_features" command to check if
- * the feature is supported. If this fails we try to set this feature with
- * mke2fs to check for its support. */
-static int is_e2fsprogs_feature_supp(const char *feature)
-{
- FILE *fp;
- char cmd[PATH_MAX];
- char imgname[] = "/tmp/test-img-XXXXXX";
- int fd = -1;
- int ret = 0;
-
- snprintf(cmd, sizeof(cmd),
- "debugfs -c -R \"supported_features %s\" 2>&1", feature);
-
- /* Using popen() instead of run_command() since debugfs does not return
- * proper error code if command is not supported */
- fp = popen(cmd, "r");
- if (!fp) {
- fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- return 0;
- }
- ret = fread(cmd, 1, sizeof(cmd), fp);
- if (ret > 0) {
- if (strstr(cmd, feature) && !(strstr(cmd, "Unknown")))
- return 0;
- }
-
- if ((fd = mkstemp(imgname)) < 0)
- return -1;
-
- snprintf(cmd, sizeof(cmd), "mke2fs -F -O %s %s 100 >/dev/null 2>&1",
- feature, imgname);
- /* run_command() displays the output of mke2fs when it fails for
- * some feature, so use system() directly */
- ret = system(cmd);
- if (fd >= 0)
- remove(imgname);
-
- return ret;
-}
-
-static void disp_old_kernel_msg(char *feature)
-{
- fprintf(stderr, "WARNING: ldiskfs filesystem does not support \"%s\" "
- "feature.\n\n", feature);
-}
-
-static void enable_default_backfs_features(struct mkfs_opts *mop)
-{
- struct utsname uts;
- int maj_high, maj_low, min;
- int ret;
-
- strscat(mop->mo_mkfsopts, " -O dir_index", sizeof(mop->mo_mkfsopts));
-
- /* Upstream e2fsprogs called our uninit_groups feature uninit_bg,
- * check for both of them when testing e2fsprogs features. */
- if (is_e2fsprogs_feature_supp("uninit_groups") == 0)
- strscat(mop->mo_mkfsopts, ",uninit_groups",
- sizeof(mop->mo_mkfsopts));
- else if (is_e2fsprogs_feature_supp("uninit_bg") == 0)
- strscat(mop->mo_mkfsopts, ",uninit_bg",
- sizeof(mop->mo_mkfsopts));
- else
- disp_old_e2fsprogs_msg("uninit_bg", 1);
-
- ret = uname(&uts);
- if (ret)
- return;
-
- sscanf(uts.release, "%d.%d.%d", &maj_high, &maj_low, &min);
- printf("%d %d %d\n", maj_high, maj_low, min);
-
- /* Multiple mount protection is enabled only if failover node is
- * specified and if kernel version is higher than 2.6.9 */
- if (failover) {
- if (KERNEL_VERSION(maj_high, maj_low, min) >=
- KERNEL_VERSION(2,6,9)) {
- if (is_e2fsprogs_feature_supp("mmp") == 0)
- strscat(mop->mo_mkfsopts, ",mmp",
- sizeof(mop->mo_mkfsopts));
- else
- disp_old_e2fsprogs_msg("mmp", 1);
- } else {
- disp_old_kernel_msg("mmp");
- }
- }
-}
-/* Build fs according to type */
-int make_lustre_backfs(struct mkfs_opts *mop)
-{
- char mkfs_cmd[PATH_MAX];
- char buf[64];
- char *dev;
- int ret = 0;
- int block_count = 0;
-
- if (mop->mo_device_sz != 0) {
- if (mop->mo_device_sz < 8096){
- fprintf(stderr, "%s: size of filesystem must be larger "
- "than 8MB, but is set to %lldKB\n",
- progname, (long long)mop->mo_device_sz);
- return EINVAL;
- }
- block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
- }
-
- if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
- (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
- (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
- __u64 device_sz = mop->mo_device_sz;
-
- /* we really need the size */
- if (device_sz == 0) {
- device_sz = get_device_size(mop->mo_device);
- if (device_sz == 0)
- return ENODEV;
- }
-
- /* Journal size in MB */
- if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
- /* Choose our own default journal size */
- long journal_sz = 0, max_sz;
- if (device_sz > 1024 * 1024) /* 1GB */
- journal_sz = (device_sz / 102400) * 4;
- /* cap journal size at 1GB */
- if (journal_sz > 1024L)
- journal_sz = 1024L;
- /* man mkfs.ext3 */
- max_sz = (256000 * L_BLOCK_SIZE) >> 20; /* 1GB */
- if (journal_sz > max_sz)
- journal_sz = max_sz;
- if (journal_sz) {
- sprintf(buf, " -J size=%ld", journal_sz);
- strscat(mop->mo_mkfsopts, buf,
- sizeof(mop->mo_mkfsopts));
- }
- }
-
- /* Bytes_per_inode: disk size / num inodes */
- if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
- long bytes_per_inode = 0;
-
- if (IS_MDT(&mop->mo_ldd))
- bytes_per_inode = 4096;
-
- /* Allocate fewer inodes on large OST devices. Most
- filesystems can be much more aggressive than even
- this. */
- if ((IS_OST(&mop->mo_ldd) && (device_sz > 100000000)))
- bytes_per_inode = 16384; /* > 100 Gb device */
-
-
- if (bytes_per_inode > 0) {
- sprintf(buf, " -i %ld", bytes_per_inode);
- strscat(mop->mo_mkfsopts, buf,
- sizeof(mop->mo_mkfsopts));
- }
- }
-
- /* Inode size (for extended attributes). The LOV EA size is
- * 32 (EA hdr) + 32 (lov_mds_md) + stripes * 24 (lov_ost_data),
- * and we want some margin above that for ACLs, other EAs... */
- if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
- long inode_size = 0;
- if (IS_MDT(&mop->mo_ldd)) {
- if (mop->mo_stripe_count > 72)
- inode_size = 512; /* bz 7241 */
- /* cray stripes across all osts (>60) */
- else if (mop->mo_stripe_count > 32)
- inode_size = 2048;
- else if (mop->mo_stripe_count > 10)
- inode_size = 1024;
- else
- inode_size = 512;
- } else if (IS_OST(&mop->mo_ldd)) {
- /* now as we store fids in EA on OST we need
- to make inode bigger */
- inode_size = 256;
- }
-
- if (inode_size > 0) {
- sprintf(buf, " -I %ld", inode_size);
- strscat(mop->mo_mkfsopts, buf,
- sizeof(mop->mo_mkfsopts));
- }
- }
-
- if (verbose < 2) {
- strscat(mop->mo_mkfsopts, " -q",
- sizeof(mop->mo_mkfsopts));
- }
-
- if (strstr(mop->mo_mkfsopts, "-O") == NULL)
- enable_default_backfs_features(mop);
-
- /* Allow reformat of full devices (as opposed to
- partitions.) We already checked for mounted dev. */
- strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
-
- snprintf(mkfs_cmd, sizeof(mkfs_cmd),
- "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
- mop->mo_ldd.ldd_svname);
- } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
- long journal_sz = 0; /* FIXME default journal size */
- if (journal_sz > 0) {
- sprintf(buf, " --journal_size %ld", journal_sz);
- strscat(mop->mo_mkfsopts, buf,
- sizeof(mop->mo_mkfsopts));
- }
- snprintf(mkfs_cmd, sizeof(mkfs_cmd), "mkreiserfs -ff ");
- } else {
- fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
- progname, mop->mo_ldd.ldd_mount_type,
- MT_STR(&mop->mo_ldd));
- return EINVAL;
- }
-
- /* For loop device format the dev, not the filename */
- dev = mop->mo_device;
- if (mop->mo_flags & MO_IS_LOOP)
- dev = mop->mo_loopdev;
-
- vprint("formatting backing filesystem %s on %s\n",
- MT_STR(&mop->mo_ldd), dev);
- vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
- vprint("\t4k blocks %d\n", block_count);
- vprint("\toptions %s\n", mop->mo_mkfsopts);
-
- /* mkfs_cmd's trailing space is important! */
- strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
- strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
- strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
- if (block_count != 0) {
- sprintf(buf, " %d", block_count);
- strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
- }
-
- vprint("mkfs_cmd = %s\n", mkfs_cmd);
- ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
- if (ret) {
- fatal();
- fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
- }
- return ret;
-}
-