4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/utils/mount_utils_ldiskfs.c
33 * Author: Nathan Rutman <nathan@clusterfs.com>
36 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
40 #endif /* HAVE_CONFIG_H */
55 #include <sys/types.h>
57 #include <sys/mount.h>
58 #include <sys/utsname.h>
59 #include <sys/sysmacros.h>
66 #include <ext2fs/ext2fs.h>
69 #include <linux/fs.h> /* for BLKGETSIZE64 */
71 #include <linux/major.h>
72 #include <linux/types.h>
73 #include <linux/version.h>
74 #include <linux/lnet/lnetctl.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <libcfs/util/string.h>
78 #include "mount_utils.h"
80 #define MAX_HW_SECTORS_KB_PATH "queue/max_hw_sectors_kb"
81 #define MAX_SECTORS_KB_PATH "queue/max_sectors_kb"
82 #define SCHEDULER_PATH "queue/scheduler"
83 #define STRIPE_CACHE_SIZE "md/stripe_cache_size"
85 #define DEFAULT_SCHEDULER "deadline"
87 extern char *progname;
89 static ext2_filsys backfs;
90 static int open_flags = EXT2_FLAG_64BITS | EXT2_FLAG_SKIP_MMP |
91 EXT2_FLAG_IGNORE_SB_ERRORS | EXT2_FLAG_SUPER_ONLY;
93 /* keep it less than LL_FID_NAMELEN */
94 #define DUMMY_FILE_NAME_LEN 25
95 #define EXT3_DIRENT_SIZE DUMMY_FILE_NAME_LEN
97 static void append_unique(char *buf, char *prefix, char *key, char *val,
99 static bool is_e2fsprogs_feature_supp(const char *feature);
100 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs);
102 /* Determine if a device is a block device (as opposed to a file) */
103 static int is_block(char *devname)
109 ret = cfs_abs_path(devname, &devpath);
111 fprintf(stderr, "%s: failed to resolve path '%s': %s\n",
112 progname, devname, strerror(-ret));
116 ret = access(devname, F_OK);
118 if (strncmp(devpath, "/dev/", 5) == 0) {
119 /* nobody sane wants to create a loopback file under
120 * /dev. Let's just report the device doesn't exist */
121 fprintf(stderr, "%s: %s apparently does not exist\n",
129 ret = stat(devpath, &st);
131 fprintf(stderr, "%s: cannot stat %s\n", progname, devpath);
134 ret = S_ISBLK(st.st_mode);
140 /* Write the server config files */
141 int ldiskfs_write_ldd(struct mkfs_opts *mop)
143 char mntpt[] = "/tmp/mntXXXXXX";
150 /* Mount this device temporarily in order to write these files */
151 if (!mkdtemp(mntpt)) {
152 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
153 progname, mntpt, strerror(errno));
157 dev = mop->mo_device;
158 if (mop->mo_flags & MO_IS_LOOP)
159 dev = mop->mo_loopdev;
161 /* Multiple mount protection enabled if failover node specified */
162 if (mop->mo_flags & MO_FAILOVER) {
164 ext2fs_open(dev, open_flags, 0, 0,
165 unix_io_manager, &backfs);
166 if (!backfs || !ext2fs_has_feature_mmp(backfs->super)) {
167 if (is_e2fsprogs_feature_supp("-O mmp")) {
168 char *command = filepnm;
170 snprintf(command, sizeof(filepnm),
171 TUNE2FS" -O mmp '%s' >/dev/null 2>&1",
173 ret = run_command(command, sizeof(filepnm));
176 "%s: Unable to set 'mmp' "
180 disp_old_e2fsprogs_msg("mmp", 1);
182 /* avoid stale cache after following operations */
184 ext2fs_close(backfs);
190 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0,
191 (mop->mo_mountopts == NULL) ?
192 "errors=remount-ro" : mop->mo_mountopts);
194 fprintf(stderr, "%s: Unable to mount %s: %s\n",
195 progname, dev, strerror(errno));
197 if (errno == ENODEV) {
198 fprintf(stderr, "Is the %s module available?\n",
199 MT_STR(&mop->mo_ldd));
204 /* Set up initial directories */
205 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
206 ret = mkdir(filepnm, 0777);
207 if ((ret != 0) && (errno != EEXIST)) {
208 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
209 progname, filepnm, strerror(errno));
211 } else if (errno == EEXIST) {
215 /* Save the persistent mount data into a file. Lustre must pre-read
216 this file to get the real mount options. */
217 vprint("Writing %s\n", MOUNT_DATA_FILE);
218 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
219 filep = fopen(filepnm, "w");
221 fprintf(stderr, "%s: Unable to create %s file: %s\n",
222 progname, filepnm, strerror(errno));
225 num = fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
226 if (num < 1 && ferror(filep)) {
227 fprintf(stderr, "%s: Unable to write to file (%s): %s\n",
228 progname, filepnm, strerror(errno));
232 fsync(filep->_fileno);
242 static int readcmd(char *cmd, char *buf, int len)
247 fp = popen(cmd, "r");
251 red = fread(buf, 1, len, fp);
254 /* strip trailing newline */
255 if (buf[red - 1] == '\n')
258 return (red == 0) ? -ENOENT : 0;
261 int ldiskfs_read_ldd(char *dev, struct lustre_disk_data *mo_ldd)
271 retval = ext2fs_open(dev, open_flags, 0, 0,
272 unix_io_manager, &backfs);
274 fprintf(stderr, "Unable to open fs on %s\n", dev);
278 retval = ext2fs_namei(backfs, EXT2_ROOT_INO, EXT2_ROOT_INO,
279 MOUNT_DATA_FILE, &ino);
281 fprintf(stderr, "Error while looking up %s\n", MOUNT_DATA_FILE);
284 retval = ext2fs_file_open(backfs, ino, 0, &file);
286 fprintf(stderr, "Error while opening file %s\n",
290 retval = ext2fs_file_read(file, mo_ldd, sizeof(*mo_ldd), &got);
291 if (retval || got == 0)
292 fprintf(stderr, "Failed to read file %s\n", MOUNT_DATA_FILE);
294 /* As long as we at least have the label, we're good to go */
295 snprintf(cmd, sizeof(cmd), E2LABEL" %s", dev);
296 ret = readcmd(cmd, mo_ldd->ldd_svname, sizeof(mo_ldd->ldd_svname) - 1);
301 int ldiskfs_erase_ldd(struct mkfs_opts *mop, char *param)
306 void ldiskfs_print_ldd_params(struct mkfs_opts *mop)
308 printf("Parameters:%s\n", mop->mo_ldd.ldd_params);
311 /* Display the need for the latest e2fsprogs to be installed. make_backfs
312 * indicates if the caller is make_lustre_backfs() or not. */
313 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
315 static int msg_displayed;
318 fprintf(stderr, "WARNING: %s does not support %s "
319 "feature.\n\n", E2FSPROGS, feature);
325 fprintf(stderr, "WARNING: The %s package currently installed on "
326 "your system does not support \"%s\" feature.\n",
328 #if !(HAVE_LDISKFSPROGS)
329 fprintf(stderr, "Please install the latest version of e2fsprogs from\n"
330 "https://downloads.whamcloud.com/public/e2fsprogs/latest/\n"
331 "to enable this feature.\n");
335 "Feature will not be enabled until %s is updated and '%s -O %s %%{device}' is run.\n\n",
336 E2FSPROGS, TUNE2FS, feature);
339 /* Check whether the file exists in the device */
340 static int file_in_dev(char *file_name, char *dev_name)
346 retval = ext2fs_open(dev_name, open_flags, 0, 0,
347 unix_io_manager, &backfs);
351 retval = ext2fs_namei(backfs, EXT2_ROOT_INO, EXT2_ROOT_INO,
359 /* Check whether the device has already been used with lustre */
360 int ldiskfs_is_lustre(char *dev, unsigned *mount_type)
364 ret = file_in_dev(MOUNT_DATA_FILE, dev);
366 /* in the -1 case, 'extents' means IS a lustre target */
367 *mount_type = LDD_MT_LDISKFS;
371 ret = file_in_dev(LAST_RCVD, dev);
373 *mount_type = LDD_MT_LDISKFS;
380 /* Check if a certain feature is supported by e2fsprogs.
381 * Firstly we try to use "debugfs supported_features" command to check if
382 * the feature is supported. If this fails we try to set this feature with
383 * mke2fs to check for its support. */
384 static bool is_e2fsprogs_feature_supp(const char *feature)
386 static char supp_features[4096] = "";
389 char imgname[] = "/tmp/test-img-XXXXXX";
393 if (supp_features[0] == '\0') {
394 snprintf(cmd, sizeof(cmd), "%s -c -R supported_features 2>&1",
397 /* Using popen() instead of run_command() since debugfs does
398 * not return proper error code if command is not supported */
399 fp = popen(cmd, "r");
401 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
403 ret = fread(supp_features, 1,
404 sizeof(supp_features) - 1, fp);
405 supp_features[ret] = '\0';
410 if (strstr(supp_features,
411 strncmp(feature, "-O ", 3) ? feature : feature + 3))
414 if ((fd = mkstemp(imgname)) < 0)
419 snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
420 MKE2FS, feature, imgname);
421 /* run_command() displays the output of mke2fs when it fails for
422 * some feature, so use system() directly */
430 * append_unique: append @key or @key=@val pair to @buf only if @key does not
432 * @buf: buffer to hold @key or @key=@val
433 * @prefix: prefix string before @key
435 * @val: value string if it's a @key=@val pair
437 static void append_unique(char *buf, char *prefix, char *key, char *val,
446 anchor = end = strstr(buf, key);
447 /* try to find exact match string in @buf */
448 while (end && *end != '\0' && *end != ',' && *end != ' ' && *end != '=')
451 if (anchor == NULL || strlen(key) != len ||
452 strncmp(anchor, key, len) != 0) {
454 strscat(buf, prefix, maxbuflen);
456 strscat(buf, key, maxbuflen);
458 strscat(buf, "=\"", maxbuflen);
459 strscat(buf, val, maxbuflen);
460 strscat(buf, "\"", maxbuflen);
465 static int enable_default_ext4_features(struct mkfs_opts *mop, char *anchor,
466 size_t maxbuflen, bool user_spec)
468 unsigned long long blocks = mop->mo_device_kb / mop->mo_blocksize_kb;
469 bool enable_64bit = false;
471 /* Enable large block addresses if the LUN is over 2^32 blocks. */
472 if (blocks > 0xffffffffULL && is_e2fsprogs_feature_supp("-O 64bit"))
475 if (IS_OST(&mop->mo_ldd)) {
476 append_unique(anchor, user_spec ? "," : " -O ",
477 "extents", NULL, maxbuflen);
478 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
479 } else if (IS_MDT(&mop->mo_ldd)) {
480 append_unique(anchor, user_spec ? "," : " -O ",
481 "dirdata", NULL, maxbuflen);
482 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
484 append_unique(anchor, ",", "extents", NULL, maxbuflen);
486 append_unique(anchor, ",", "^extents", NULL, maxbuflen);
488 append_unique(anchor, user_spec ? "," : " -O ",
489 "uninit_bg", NULL, maxbuflen);
492 /* Multiple mount protection enabled only if failover node specified */
493 if (mop->mo_flags & MO_FAILOVER) {
494 if (is_e2fsprogs_feature_supp("-O mmp"))
495 append_unique(anchor, ",", "mmp", NULL, maxbuflen);
497 disp_old_e2fsprogs_msg("mmp", 1);
500 /* Allow more than 65000 subdirectories */
501 if (is_e2fsprogs_feature_supp("-O dir_nlink"))
502 append_unique(anchor, ",", "dir_nlink", NULL, maxbuflen);
504 /* The following options are only valid for ext4-based ldiskfs.
505 * If --backfstype=ext3 is specified, do not enable them. */
506 if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
509 /* Enable quota by default */
510 if (is_e2fsprogs_feature_supp("-O quota")) {
511 append_unique(anchor, ",", "quota", NULL, maxbuflen);
512 /* Enable project quota by default */
513 if (is_e2fsprogs_feature_supp("-O project"))
514 append_unique(anchor, ",", "project", NULL, maxbuflen);
517 fprintf(stderr, "\"-O quota\" must be supported by "
518 "e2fsprogs, please upgrade your e2fsprogs.\n");
522 /* Allow files larger than 2TB. Also needs LU-16, but not harmful. */
523 if (is_e2fsprogs_feature_supp("-O huge_file"))
524 append_unique(anchor, ",", "huge_file", NULL, maxbuflen);
527 append_unique(anchor, ",", "64bit", NULL, maxbuflen);
529 if (blocks >= 0x1000000000 && is_e2fsprogs_feature_supp("-O meta_bg"))
530 append_unique(anchor, ",", "meta_bg", NULL, maxbuflen);
532 if (enable_64bit || strstr(mop->mo_mkfsopts, "meta_bg"))
533 append_unique(anchor, ",", "^resize_inode", NULL, maxbuflen);
535 /* Allow xattrs larger than one block, stored in a separate inode */
536 if (IS_MDT(&mop->mo_ldd) && is_e2fsprogs_feature_supp("-O ea_inode"))
537 append_unique(anchor, ",", "ea_inode", NULL, maxbuflen);
539 /* Allow more than 10M directory entries */
540 if (IS_MDT(&mop->mo_ldd) && is_e2fsprogs_feature_supp("-O large_dir"))
541 append_unique(anchor, ",", "large_dir", NULL, maxbuflen);
543 /* Disable fast_commit since it breaks ldiskfs transactions ordering */
544 if (is_e2fsprogs_feature_supp("fast_commit"))
545 append_unique(anchor, ",", "^fast_commit", NULL, maxbuflen);
547 /* Cluster inode/block bitmaps and inode table for more efficient IO.
548 * Align the flex groups on a 1MB boundary for better performance. */
549 /* This -O feature needs to go last, since it adds the "-G" option. */
550 if (is_e2fsprogs_feature_supp("-O flex_bg")) {
553 append_unique(anchor, ",", "flex_bg", NULL, maxbuflen);
555 if (IS_OST(&mop->mo_ldd) &&
556 strstr(mop->mo_mkfsopts, "-G") == NULL) {
557 snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
558 1024 / mop->mo_blocksize_kb);
559 strscat(anchor, tmp_buf, maxbuflen);
562 /* Don't add any more "-O" options here, see last comment above */
567 * moveopts_to_end: find the option string, move remaining strings to
568 * where option string starts, and append the option
570 * @start: where the option string starts before the move
571 * RETURN: where the option string starts after the move
573 static char *moveopts_to_end(char *start)
579 /* skip whitespace before options */
584 /* find end of option characters */
585 while (*end != ' ' && *end != '\0')
589 if (len >= sizeof(save))
590 len = sizeof(save) - 1;
593 strncpy(save, start, len);
596 /* move remaining options up front */
598 memmove(start, end, strlen(end));
599 *(start + strlen(end)) = '\0';
601 /* append the specified options */
602 if (*(start + strlen(start) - 1) != ' ')
604 idx = start + strlen(start);
610 /* Build fs according to type */
611 int ldiskfs_make_lustre(struct mkfs_opts *mop)
613 char mkfs_cmd[PATH_MAX];
617 int ret = 0, ext_opts = 0;
618 bool enable_64bit = false;
622 mop->mo_blocksize_kb = 4;
624 start = strstr(mop->mo_mkfsopts, "-b");
629 blocksize = strtol(start + 2, &end, 0);
630 if (end && (*end == 'k' || *end == 'K'))
632 /* EXT4_MIN_BLOCK_SIZE || EXT4_MAX_BLOCK_SIZE */
633 if (blocksize < 1024 || blocksize > 65536) {
635 "%s: blocksize %lu not in 1024-65536 bytes, normally 4096 bytes\n",
636 progname, blocksize);
640 if ((blocksize & (blocksize - 1)) != 0) {
642 "%s: blocksize %lu not a power-of-two value\n",
643 progname, blocksize);
646 mop->mo_blocksize_kb = blocksize >> 10;
649 if (!(mop->mo_flags & MO_IS_LOOP)) {
650 __u64 device_kb = get_device_size(mop->mo_device);
655 /* Compare to real size */
656 if (mop->mo_device_kb == 0 || device_kb < mop->mo_device_kb)
657 mop->mo_device_kb = device_kb;
660 if (mop->mo_device_kb != 0) {
663 if (mop->mo_device_kb < 32384) {
664 fprintf(stderr, "%s: size of filesystem must be larger "
665 "than 32MB, but is set to %lldKB\n",
666 progname, (long long)mop->mo_device_kb);
669 block_count = mop->mo_device_kb / mop->mo_blocksize_kb;
670 if (block_count > 0xffffffffULL) {
671 /* If the LUN size is just over 2^32 blocks, limit the
672 * filesystem size to 2^32-1 blocks to avoid problems
673 * with ldiskfs/mkfs not handling this well. b=22906
675 if (block_count < 0x100002000ULL)
677 0xffffffffULL * mop->mo_blocksize_kb;
683 if ((mop->mo_ldd.ldd_mount_type != LDD_MT_EXT3) &&
684 (mop->mo_ldd.ldd_mount_type != LDD_MT_LDISKFS) &&
685 (mop->mo_ldd.ldd_mount_type != LDD_MT_LDISKFS2)) {
686 fprintf(stderr, "%s: unsupported fs type: %d (%s)\n",
687 progname, mop->mo_ldd.ldd_mount_type,
688 MT_STR(&mop->mo_ldd));
693 /* Journal size in MB */
694 if (strstr(mop->mo_mkfsopts, "-J") == NULL &&
695 mop->mo_device_kb > 1024 * 1024) {
696 /* Choose our own default journal size */
697 long journal_mb = 0, max_mb;
699 /* cap journal size at 4GB for MDT, leave at 1GB for OSTs */
700 if (IS_MDT(&mop->mo_ldd))
702 else if (IS_OST(&mop->mo_ldd))
704 else /* Use mke2fs default size for MGS */
707 /* Use at most 4% of device for journal */
708 journal_mb = mop->mo_device_kb * 4 / (1024 * 100);
709 if (journal_mb > max_mb)
713 snprintf(buf, sizeof(buf), " -J size=%ld", journal_mb);
714 strscat(mop->mo_mkfsopts, buf,
715 sizeof(mop->mo_mkfsopts));
720 * The inode size is constituted by following elements
721 * (assuming all files are in composite layout and has
724 * ldiskfs inode size: 160
725 * MDT extended attributes size, including:
726 * ext4_xattr_header: 32
727 * LOV EA size: 32(lov_comp_md_v1) +
728 * 3 * 40(lov_comp_md_entry_v1) +
729 * 3 * 32(lov_mds_md) +
730 * stripes * 24(lov_ost_data) +
731 * 16(xattr_entry) + 4("lov")
732 * LMA EA size: 24(lustre_mdt_attrs) +
733 * 16(xattr_entry) + 4("lma")
734 * SOM EA size: 24(lustre_som_attrs) +
735 * 16(xattr_entry) + 4("som")
736 * link EA size: 24(link_ea_header) + 18(link_ea_entry) +
737 * 16(filename) + 16(xattr_entry) + 4("link")
738 * and some margin for 4-byte alignment, ACLs and other EAs.
740 * If we say the average filename length is about 32 bytes,
741 * the calculation looks like:
742 * 160 + 32 + (32+3*(40+32)+24*stripes+20) + (24+20) + (24+20) +
743 * (24+20) + (~42+16+20) + other <= 512*2^m, {m=0,1,2,3}
745 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
746 if (IS_MDT(&mop->mo_ldd)) {
747 if (mop->mo_stripe_count > 59)
748 inode_size = 512; /* bz 7241 */
749 /* see also "-i" below for EA blocks */
750 else if (mop->mo_stripe_count > 16)
754 } else if (IS_OST(&mop->mo_ldd)) {
755 /* We store MDS FID and necessary composite
756 * layout information in the OST object EA:
757 * ldiskfs inode size: 160
758 * OST extended attributes size, including:
759 * ext4_xattr_header: 32
760 * LMA EA size: 24(lustre_mdt_attrs) +
761 * 16(xattr_entry) + 4("lma")
762 * FID EA size: 52(filter_fid) +
763 * 16(xattr_entry) + 4("fid")
764 * 160 + 32 + (24+20) + (52+20) = 308
769 if (inode_size > 0) {
770 snprintf(buf, sizeof(buf), " -I %ld", inode_size);
771 strscat(mop->mo_mkfsopts, buf,
772 sizeof(mop->mo_mkfsopts));
776 /* Bytes_per_inode: disk size / num inodes */
777 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
778 strstr(mop->mo_mkfsopts, "-N") == NULL) {
779 long bytes_per_inode = 0;
781 /* Allocate more inodes on MDT devices. There is
782 * no data stored on the MDT, and very little extra
783 * metadata beyond the inode. It could go down as
784 * low as 1024 bytes, but this is conservative.
785 * Account for external EA blocks for wide striping.
787 if (IS_MDT(&mop->mo_ldd)) {
788 bytes_per_inode = inode_size + 1536;
790 if (mop->mo_stripe_count > 59) {
791 int extra = mop->mo_stripe_count * 24;
793 extra = ((extra - 1) | 4095) + 1;
794 bytes_per_inode += extra;
798 /* Allocate fewer inodes on large OST devices. Most
799 * filesystems can be much more aggressive than even
800 * this, but it is impossible to know in advance.
802 if (IS_OST(&mop->mo_ldd)) {
803 /* OST > 16TB assume average file size 1MB */
804 if (mop->mo_device_kb > (16ULL << 30))
805 bytes_per_inode = 1024 * 1024;
806 /* OST > 4TB assume average file size 512kB */
807 else if (mop->mo_device_kb > (4ULL << 30))
808 bytes_per_inode = 512 * 1024;
809 /* OST > 1TB assume average file size 256kB */
810 else if (mop->mo_device_kb > (1ULL << 30))
811 bytes_per_inode = 256 * 1024;
812 /* OST > 10GB assume average file size 64kB,
813 * plus a bit so that inodes will fit into a
814 * 256x flex_bg without overflowing.
816 else if (mop->mo_device_kb > (10ULL << 20))
817 bytes_per_inode = 69905;
820 if (bytes_per_inode > 0) {
821 snprintf(buf, sizeof(buf), " -i %ld", bytes_per_inode);
822 strscat(mop->mo_mkfsopts, buf,
823 sizeof(mop->mo_mkfsopts));
824 mop->mo_inode_size = bytes_per_inode;
829 strscat(mop->mo_mkfsopts, " -q", sizeof(mop->mo_mkfsopts));
831 /* start handle -O mkfs options */
832 start = strstr(mop->mo_mkfsopts, "-O");
834 if (strstr(start + 2, "-O") != NULL) {
836 "%s: don't specify multiple -O options\n",
840 start = moveopts_to_end(start);
841 maxbuflen = sizeof(mop->mo_mkfsopts) -
842 (start - mop->mo_mkfsopts) - strlen(start);
843 ret = enable_default_ext4_features(mop, start, maxbuflen, 1);
845 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
846 maxbuflen = sizeof(mop->mo_mkfsopts) - strlen(mop->mo_mkfsopts);
847 ret = enable_default_ext4_features(mop, start, maxbuflen, 0);
851 /* end handle -O mkfs options */
853 /* start handle -E mkfs options */
854 start = strstr(mop->mo_mkfsopts, "-E");
856 if (strstr(start + 2, "-E") != NULL) {
858 "%s: don't specify multiple -E options\n",
862 start = moveopts_to_end(start);
863 maxbuflen = sizeof(mop->mo_mkfsopts) -
864 (start - mop->mo_mkfsopts) - strlen(start);
867 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
868 maxbuflen = sizeof(mop->mo_mkfsopts) - strlen(mop->mo_mkfsopts);
871 /* In order to align the filesystem metadata on 1MB boundaries,
872 * give a resize value that will reserve a power-of-two group
873 * descriptor blocks, but leave one block for the superblock.
874 * Only useful for filesystems with < 2^32 blocks due to resize
877 if (!enable_64bit && strstr(mop->mo_mkfsopts, "meta_bg") == NULL &&
878 IS_OST(&mop->mo_ldd) && mop->mo_device_kb > 100 * 1024) {
879 unsigned int group_blocks = mop->mo_blocksize_kb * 8192;
880 unsigned int desc_per_block = mop->mo_blocksize_kb * 1024 / 32;
881 unsigned int resize_blks;
883 resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
884 snprintf(buf, sizeof(buf), "%u", resize_blks);
885 append_unique(start, ext_opts ? "," : " -E ",
886 "resize", buf, maxbuflen);
890 /* Avoid zeroing out the full journal - speeds up mkfs */
891 if (is_e2fsprogs_feature_supp("-E lazy_journal_init=0")) {
892 append_unique(start, ext_opts ? "," : " -E ",
893 "lazy_journal_init", "0", maxbuflen);
896 if (is_e2fsprogs_feature_supp("-E lazy_itable_init=0")) {
897 append_unique(start, ext_opts ? "," : "-E",
898 "lazy_itable_init", "0", maxbuflen);
902 /* end handle -E mkfs options */
904 /* Allow reformat of full devices (as opposed to partitions).
905 * We already checked for mounted dev.
907 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
909 snprintf(mkfs_cmd, sizeof(mkfs_cmd), "%s -j -b %d -L %s ", MKE2FS,
910 mop->mo_blocksize_kb * 1024, mop->mo_ldd.ldd_svname);
912 /* For loop device format the dev, not the filename */
913 dev = mop->mo_device;
914 if (mop->mo_flags & MO_IS_LOOP)
915 dev = mop->mo_loopdev;
917 vprint("formatting backing filesystem %s on %s\n",
918 MT_STR(&mop->mo_ldd), dev);
919 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
920 vprint("\tkilobytes %llu\n", mop->mo_device_kb);
921 vprint("\toptions %s\n", mop->mo_mkfsopts);
923 /* mkfs_cmd's trailing space is important! */
924 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
925 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
926 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
927 if (mop->mo_device_kb != 0) {
928 snprintf(buf, sizeof(buf), " %lluk",
929 (unsigned long long)mop->mo_device_kb);
930 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
933 vprint("mkfs_cmd = %s\n", mkfs_cmd);
934 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
937 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
942 int ldiskfs_prepare_lustre(struct mkfs_opts *mop,
943 char *wanted_mountopts, size_t len)
945 struct lustre_disk_data *ldd = &mop->mo_ldd;
948 /* Set MO_IS_LOOP to indicate a loopback device is needed */
949 ret = is_block(mop->mo_device);
952 } else if (ret == 0) {
953 mop->mo_flags |= MO_IS_LOOP;
956 if (IS_MDT(ldd) || IS_MGS(ldd))
957 strscat(wanted_mountopts, ",user_xattr", len);
962 int ldiskfs_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
964 if (strstr(mountopts, "errors=") == NULL)
965 strscat(mountopts, ",errors=remount-ro", len);
970 static int read_file(const char *path, char *buf, int size)
974 fd = fopen(path, "r");
978 if (fgets(buf, size, fd) == NULL) {
979 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
985 /* strip trailing newline */
987 if (buf[size - 1] == '\n')
988 buf[size - 1] = '\0';
993 static int write_file(const char *path, const char *buf)
997 fd = open(path, O_WRONLY);
1001 rc = write(fd, buf, strlen(buf));
1004 return rc < 0 ? errno : 0;
1007 static int tune_md_stripe_cache_size(const char *sys_path,
1008 struct mount_opts *mop)
1010 char path[PATH_MAX];
1011 unsigned long old_stripe_cache_size;
1012 unsigned long new_stripe_cache_size;
1013 char buf[3 * sizeof(old_stripe_cache_size) + 2];
1016 if (mop->mo_md_stripe_cache_size <= 0)
1019 new_stripe_cache_size = mop->mo_md_stripe_cache_size;
1021 snprintf(path, sizeof(path), "%s/%s", sys_path, STRIPE_CACHE_SIZE);
1022 rc = read_file(path, buf, sizeof(buf));
1025 fprintf(stderr, "warning: cannot read '%s': %s\n",
1026 path, strerror(errno));
1030 old_stripe_cache_size = strtoul(buf, NULL, 0);
1031 if (old_stripe_cache_size == 0 || old_stripe_cache_size == ULONG_MAX)
1034 if (new_stripe_cache_size <= old_stripe_cache_size)
1037 snprintf(buf, sizeof(buf), "%lu", new_stripe_cache_size);
1038 rc = write_file(path, buf);
1041 fprintf(stderr, "warning: cannot write '%s': %s\n",
1042 path, strerror(errno));
1049 static int tune_max_sectors_kb(const char *sys_path, struct mount_opts *mop)
1051 char path[PATH_MAX];
1052 unsigned long max_hw_sectors_kb;
1053 unsigned long old_max_sectors_kb;
1054 unsigned long new_max_sectors_kb;
1055 char buf[3 * sizeof(old_max_sectors_kb) + 2];
1058 if (mop->mo_max_sectors_kb >= 0) {
1059 new_max_sectors_kb = mop->mo_max_sectors_kb;
1060 goto have_new_max_sectors_kb;
1063 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_HW_SECTORS_KB_PATH);
1064 rc = read_file(path, buf, sizeof(buf));
1066 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
1067 * error for some devices. */
1071 max_hw_sectors_kb = strtoul(buf, NULL, 0);
1072 if (max_hw_sectors_kb == 0 || max_hw_sectors_kb == ULLONG_MAX) {
1073 /* No digits at all or something weird. */
1077 new_max_sectors_kb = max_hw_sectors_kb;
1079 /* Don't increase IO request size limit past 16MB. It is
1080 * about PTLRPC_MAX_BRW_SIZE, but that isn't in a public
1081 * header. Note that even though the block layer allows
1082 * larger values, setting max_sectors_kb = 32768 causes
1083 * crashes (LU-6974). */
1084 if (new_max_sectors_kb > 16 * 1024)
1085 new_max_sectors_kb = 16 * 1024;
1087 have_new_max_sectors_kb:
1088 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_SECTORS_KB_PATH);
1089 rc = read_file(path, buf, sizeof(buf));
1091 /* No MAX_SECTORS_KB_PATH isn't necessary an error for
1096 old_max_sectors_kb = strtoul(buf, NULL, 0);
1097 if (old_max_sectors_kb == 0 || old_max_sectors_kb == ULLONG_MAX) {
1098 /* No digits at all or something weird. */
1102 if (new_max_sectors_kb <= old_max_sectors_kb)
1105 snprintf(buf, sizeof(buf), "%lu", new_max_sectors_kb);
1106 rc = write_file(path, buf);
1109 fprintf(stderr, "warning: cannot write '%s': %s\n",
1110 path, strerror(errno));
1114 fprintf(stderr, "%s: increased '%s' from %lu to %lu\n",
1115 progname, path, old_max_sectors_kb, new_max_sectors_kb);
1120 static int tune_block_dev_scheduler(const char *sys_path, const char *new_sched)
1122 char path[PATH_MAX];
1128 /* Before setting the scheduler, we need to check to see if
1129 * it's already set to "noop". If it is then we don't want to
1130 * override that setting. If it's set to anything other than
1131 * "noop" then set the scheduler to what has been passed
1134 snprintf(path, sizeof(path), "%s/%s", sys_path, SCHEDULER_PATH);
1135 rc = read_file(path, buf, sizeof(buf));
1138 fprintf(stderr, "%s: cannot read '%s': %s\n",
1139 progname, path, strerror(errno));
1144 /* The expected format of buf: noop anticipatory deadline [cfq] */
1145 s = strchr(buf, '[');
1146 e = strchr(buf, ']');
1148 /* If the format is not what we expect then be safe and error out. */
1149 if (s == NULL || e == NULL || !(s < e)) {
1152 "%s: cannot parse scheduler options for '%s'\n",
1161 if (strcmp(old_sched, "noop") == 0 ||
1162 strcmp(old_sched, "deadline") == 0 ||
1163 strcmp(old_sched, "mq-deadline") == 0 ||
1164 strstr(old_sched, new_sched) == 0)
1167 rc = write_file(path, new_sched);
1171 "%s: cannot set scheduler on '%s': %s\n",
1172 progname, path, strerror(errno));
1176 fprintf(stderr, "%s: changed scheduler of '%s' from %s to %s\n",
1177 progname, path, old_sched, new_sched);
1182 static int tune_block_dev(const char *src, struct mount_opts *mop);
1184 static int tune_block_dev_slaves(const char *sys_path, struct mount_opts *mop)
1186 char slaves_path[PATH_MAX];
1191 snprintf(slaves_path, sizeof(slaves_path), "%s/slaves", sys_path);
1192 slaves_dir = opendir(slaves_path);
1193 if (slaves_dir == NULL) {
1194 if (errno == ENOENT)
1200 while ((d = readdir(slaves_dir)) != NULL) {
1201 char path[PATH_MAX * 2];
1204 if (d->d_type != DT_LNK)
1207 snprintf(path, sizeof(path), "/dev/%s", d->d_name);
1208 rc2 = tune_block_dev(path, mop);
1213 closedir(slaves_dir);
1218 /* This is to tune the kernel for good SCSI performance.
1219 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
1220 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
1221 static int tune_block_dev(const char *src, struct mount_opts *mop)
1224 char sys_path[PATH_MAX];
1225 char partition_path[PATH_MAX + sizeof("partition")];
1226 char *real_sys_path = NULL;
1230 * Don't apply block device tuning for MDT or MGT devices,
1231 * since we don't need huge IO sizes to get good performance
1233 if (!IS_OST(&mop->mo_ldd))
1239 rc = stat(src, &st);
1242 fprintf(stderr, "warning: cannot stat '%s': %s\n",
1243 src, strerror(errno));
1247 if (!S_ISBLK(st.st_mode))
1250 if (major(st.st_rdev) == LOOP_MAJOR)
1253 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u",
1254 major(st.st_rdev), minor(st.st_rdev));
1256 snprintf(partition_path, sizeof(partition_path), "%s/partition",
1259 rc = access(partition_path, F_OK);
1261 if (errno == ENOENT)
1262 goto have_whole_dev;
1265 fprintf(stderr, "warning: cannot access '%s': %s\n",
1266 partition_path, strerror(errno));
1271 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u/..",
1272 major(st.st_rdev), minor(st.st_rdev));
1275 /* Since we recurse on slave devices we resolve the sys_path to
1276 * avoid path buffer overflows. */
1277 real_sys_path = realpath(sys_path, NULL);
1278 if (real_sys_path == NULL) {
1281 "warning: cannot resolve '%s': %s\n",
1282 sys_path, strerror(errno));
1287 if (major(st.st_rdev) == MD_MAJOR) {
1288 rc = tune_md_stripe_cache_size(real_sys_path, mop);
1290 /* Ignore errors from tune_max_sectors_kb() and
1291 * tune_scheduler(). The worst that will happen is a block
1292 * device with an "incorrect" scheduler. */
1293 tune_max_sectors_kb(real_sys_path, mop);
1294 tune_block_dev_scheduler(real_sys_path, DEFAULT_SCHEDULER);
1296 /* If device is multipath device then tune its slave
1298 rc = tune_block_dev_slaves(real_sys_path, mop);
1302 free(real_sys_path);
1307 int ldiskfs_tune_lustre(char *dev, struct mount_opts *mop)
1309 return tune_block_dev(dev, mop);
1312 int ldiskfs_label_lustre(struct mount_opts *mop)
1314 char label_cmd[PATH_MAX];
1317 snprintf(label_cmd, sizeof(label_cmd),
1318 TUNE2FS" -f -L '%s' '%s' >/dev/null 2>&1",
1319 mop->mo_ldd.ldd_svname, mop->mo_source);
1320 rc = run_command(label_cmd, sizeof(label_cmd));
1325 int ldiskfs_rename_fsname(struct mkfs_opts *mop, const char *oldname)
1327 struct mount_opts opts;
1328 struct lustre_disk_data *ldd = &mop->mo_ldd;
1329 char mntpt[] = "/tmp/mntXXXXXX";
1333 /* Change the filesystem label. */
1335 opts.mo_source = mop->mo_device;
1336 ret = ldiskfs_label_lustre(&opts);
1340 fprintf(stderr, "Can't change filesystem label: %s\n",
1345 /* Mount this device temporarily in order to write these files */
1346 if (mkdtemp(mntpt) == NULL) {
1351 fprintf(stderr, "Can't create temp mount point %s: %s\n",
1352 mntpt, strerror(ret));
1356 if (mop->mo_flags & MO_IS_LOOP)
1357 dev = mop->mo_loopdev;
1359 dev = mop->mo_device;
1360 ret = mount(dev, mntpt, MT_STR(ldd), 0, ldd->ldd_mount_opts);
1364 fprintf(stderr, "Unable to mount %s: %s\n",
1365 dev, strerror(ret));
1367 fprintf(stderr, "Is the %s module available?\n",
1372 ret = lustre_rename_fsname(mop, mntpt, oldname);
1380 /* Enable quota accounting */
1381 int ldiskfs_enable_quota(struct mkfs_opts *mop)
1385 int cmdsz = sizeof(cmd), ret;
1387 if (!is_e2fsprogs_feature_supp("-O quota")) {
1388 fprintf(stderr, "%s: \"-O quota\" is is not supported by "
1389 "current e2fsprogs\n", progname);
1393 dev = mop->mo_device;
1394 if (mop->mo_flags & MO_IS_LOOP)
1395 dev = mop->mo_loopdev;
1397 /* Quota feature is already enabled? */
1399 ext2fs_open(dev, open_flags, 0, 0, unix_io_manager, &backfs);
1400 if (backfs && ext2fs_has_feature_quota(backfs->super)) {
1401 vprint("Quota feature is already enabled.\n");
1405 /* Turn on quota feature by "tune2fs -O quota" */
1406 snprintf(cmd, cmdsz, "%s -O quota %s", TUNE2FS, dev);
1407 ret = run_command(cmd, cmdsz);
1409 fprintf(stderr, "command:%s (%d)", cmd, ret);
1414 int ldiskfs_init(void)
1416 /* Required because full path to DEBUGFS is not specified */
1417 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 0);
1422 void ldiskfs_fini(void)
1425 ext2fs_close(backfs);
1431 struct module_backfs_ops ldiskfs_ops = {
1432 .init = ldiskfs_init,
1433 .fini = ldiskfs_fini,
1434 .read_ldd = ldiskfs_read_ldd,
1435 .write_ldd = ldiskfs_write_ldd,
1436 .erase_ldd = ldiskfs_erase_ldd,
1437 .print_ldd_params = ldiskfs_print_ldd_params,
1438 .is_lustre = ldiskfs_is_lustre,
1439 .make_lustre = ldiskfs_make_lustre,
1440 .prepare_lustre = ldiskfs_prepare_lustre,
1441 .fix_mountopts = ldiskfs_fix_mountopts,
1442 .tune_lustre = ldiskfs_tune_lustre,
1443 .label_lustre = ldiskfs_label_lustre,
1444 .enable_quota = ldiskfs_enable_quota,
1445 .rename_fsname = ldiskfs_rename_fsname,
1447 #endif /* PLUGIN_DIR */