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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/mount_utils_ldiskfs.c
34 * Author: Nathan Rutman <nathan@clusterfs.com>
37 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
41 #endif /* HAVE_CONFIG_H */
56 #include <sys/types.h>
58 #include <sys/mount.h>
59 #include <sys/utsname.h>
60 #include <sys/sysmacros.h>
67 #include <ext2fs/ext2fs.h>
70 #include <linux/fs.h> /* for BLKGETSIZE64 */
72 #include <linux/major.h>
73 #include <linux/types.h>
74 #include <linux/version.h>
75 #include <linux/lnet/lnetctl.h>
76 #include <linux/lustre/lustre_ver.h>
77 #include <libcfs/util/string.h>
79 #include "mount_utils.h"
81 #define MAX_HW_SECTORS_KB_PATH "queue/max_hw_sectors_kb"
82 #define MAX_SECTORS_KB_PATH "queue/max_sectors_kb"
83 #define SCHEDULER_PATH "queue/scheduler"
84 #define STRIPE_CACHE_SIZE "md/stripe_cache_size"
86 #define DEFAULT_SCHEDULER "deadline"
88 extern char *progname;
90 static ext2_filsys backfs;
91 static int open_flags = EXT2_FLAG_64BITS | EXT2_FLAG_SKIP_MMP |
92 EXT2_FLAG_IGNORE_SB_ERRORS | EXT2_FLAG_SUPER_ONLY;
94 /* keep it less than LL_FID_NAMELEN */
95 #define DUMMY_FILE_NAME_LEN 25
96 #define EXT3_DIRENT_SIZE DUMMY_FILE_NAME_LEN
98 static void append_unique(char *buf, char *prefix, char *key, char *val,
100 static bool is_e2fsprogs_feature_supp(const char *feature);
101 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs);
103 /* Determine if a device is a block device (as opposed to a file) */
104 static int is_block(char *devname)
110 ret = cfs_abs_path(devname, &devpath);
112 fprintf(stderr, "%s: failed to resolve path '%s': %s\n",
113 progname, devname, strerror(-ret));
117 ret = access(devname, F_OK);
119 if (strncmp(devpath, "/dev/", 5) == 0) {
120 /* nobody sane wants to create a loopback file under
121 * /dev. Let's just report the device doesn't exist */
122 fprintf(stderr, "%s: %s apparently does not exist\n",
130 ret = stat(devpath, &st);
132 fprintf(stderr, "%s: cannot stat %s\n", progname, devpath);
135 ret = S_ISBLK(st.st_mode);
141 /* Write the server config files */
142 int ldiskfs_write_ldd(struct mkfs_opts *mop)
144 char mntpt[] = "/tmp/mntXXXXXX";
151 /* Mount this device temporarily in order to write these files */
152 if (!mkdtemp(mntpt)) {
153 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
154 progname, mntpt, strerror(errno));
158 dev = mop->mo_device;
159 if (mop->mo_flags & MO_IS_LOOP)
160 dev = mop->mo_loopdev;
162 /* Multiple mount protection enabled if failover node specified */
163 if (mop->mo_flags & MO_FAILOVER) {
165 ext2fs_open(dev, open_flags, 0, 0,
166 unix_io_manager, &backfs);
167 if (!backfs || !ext2fs_has_feature_mmp(backfs->super)) {
168 if (is_e2fsprogs_feature_supp("-O mmp")) {
169 char *command = filepnm;
171 snprintf(command, sizeof(filepnm),
172 TUNE2FS" -O mmp '%s' >/dev/null 2>&1",
174 ret = run_command(command, sizeof(filepnm));
177 "%s: Unable to set 'mmp' "
181 disp_old_e2fsprogs_msg("mmp", 1);
183 /* avoid stale cache after following operations */
185 ext2fs_close(backfs);
191 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0,
192 (mop->mo_mountopts == NULL) ?
193 "errors=remount-ro" : mop->mo_mountopts);
195 fprintf(stderr, "%s: Unable to mount %s: %s\n",
196 progname, dev, strerror(errno));
198 if (errno == ENODEV) {
199 fprintf(stderr, "Is the %s module available?\n",
200 MT_STR(&mop->mo_ldd));
205 /* Set up initial directories */
206 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
207 ret = mkdir(filepnm, 0777);
208 if ((ret != 0) && (errno != EEXIST)) {
209 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
210 progname, filepnm, strerror(errno));
212 } else if (errno == EEXIST) {
216 /* Save the persistent mount data into a file. Lustre must pre-read
217 this file to get the real mount options. */
218 vprint("Writing %s\n", MOUNT_DATA_FILE);
219 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
220 filep = fopen(filepnm, "w");
222 fprintf(stderr, "%s: Unable to create %s file: %s\n",
223 progname, filepnm, strerror(errno));
226 num = fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
227 if (num < 1 && ferror(filep)) {
228 fprintf(stderr, "%s: Unable to write to file (%s): %s\n",
229 progname, filepnm, strerror(errno));
233 fsync(filep->_fileno);
243 static int readcmd(char *cmd, char *buf, int len)
248 fp = popen(cmd, "r");
252 red = fread(buf, 1, len, fp);
255 /* strip trailing newline */
256 if (buf[red - 1] == '\n')
259 return (red == 0) ? -ENOENT : 0;
262 int ldiskfs_read_ldd(char *dev, struct lustre_disk_data *mo_ldd)
272 retval = ext2fs_open(dev, open_flags, 0, 0,
273 unix_io_manager, &backfs);
275 fprintf(stderr, "Unable to open fs on %s\n", dev);
279 retval = ext2fs_namei(backfs, EXT2_ROOT_INO, EXT2_ROOT_INO,
280 MOUNT_DATA_FILE, &ino);
282 fprintf(stderr, "Error while looking up %s\n", MOUNT_DATA_FILE);
285 retval = ext2fs_file_open(backfs, ino, 0, &file);
287 fprintf(stderr, "Error while opening file %s\n",
291 retval = ext2fs_file_read(file, mo_ldd, sizeof(*mo_ldd), &got);
292 if (retval || got == 0)
293 fprintf(stderr, "Failed to read file %s\n", MOUNT_DATA_FILE);
295 /* As long as we at least have the label, we're good to go */
296 snprintf(cmd, sizeof(cmd), E2LABEL" %s", dev);
297 ret = readcmd(cmd, mo_ldd->ldd_svname, sizeof(mo_ldd->ldd_svname) - 1);
302 int ldiskfs_erase_ldd(struct mkfs_opts *mop, char *param)
307 void ldiskfs_print_ldd_params(struct mkfs_opts *mop)
309 printf("Parameters:%s\n", mop->mo_ldd.ldd_params);
312 /* Display the need for the latest e2fsprogs to be installed. make_backfs
313 * indicates if the caller is make_lustre_backfs() or not. */
314 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
316 static int msg_displayed;
319 fprintf(stderr, "WARNING: %s does not support %s "
320 "feature.\n\n", E2FSPROGS, feature);
326 fprintf(stderr, "WARNING: The %s package currently installed on "
327 "your system does not support \"%s\" feature.\n",
329 #if !(HAVE_LDISKFSPROGS)
330 fprintf(stderr, "Please install the latest version of e2fsprogs from\n"
331 "https://downloads.whamcloud.com/public/e2fsprogs/latest/\n"
332 "to enable this feature.\n");
336 "Feature will not be enabled until %s is updated and '%s -O %s %%{device}' is run.\n\n",
337 E2FSPROGS, TUNE2FS, feature);
340 /* Check whether the file exists in the device */
341 static int file_in_dev(char *file_name, char *dev_name)
347 retval = ext2fs_open(dev_name, open_flags, 0, 0,
348 unix_io_manager, &backfs);
352 retval = ext2fs_namei(backfs, EXT2_ROOT_INO, EXT2_ROOT_INO,
360 /* Check whether the device has already been used with lustre */
361 int ldiskfs_is_lustre(char *dev, unsigned *mount_type)
365 ret = file_in_dev(MOUNT_DATA_FILE, dev);
367 /* in the -1 case, 'extents' means IS a lustre target */
368 *mount_type = LDD_MT_LDISKFS;
372 ret = file_in_dev(LAST_RCVD, dev);
374 *mount_type = LDD_MT_LDISKFS;
381 /* Check if a certain feature is supported by e2fsprogs.
382 * Firstly we try to use "debugfs supported_features" command to check if
383 * the feature is supported. If this fails we try to set this feature with
384 * mke2fs to check for its support. */
385 static bool is_e2fsprogs_feature_supp(const char *feature)
387 static char supp_features[4096] = "";
390 char imgname[] = "/tmp/test-img-XXXXXX";
394 if (supp_features[0] == '\0') {
395 snprintf(cmd, sizeof(cmd), "%s -c -R supported_features 2>&1",
398 /* Using popen() instead of run_command() since debugfs does
399 * not return proper error code if command is not supported */
400 fp = popen(cmd, "r");
402 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
404 ret = fread(supp_features, 1,
405 sizeof(supp_features) - 1, fp);
406 supp_features[ret] = '\0';
411 if (strstr(supp_features,
412 strncmp(feature, "-O ", 3) ? feature : feature + 3))
415 if ((fd = mkstemp(imgname)) < 0)
420 snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
421 MKE2FS, feature, imgname);
422 /* run_command() displays the output of mke2fs when it fails for
423 * some feature, so use system() directly */
431 * append_unique: append @key or @key=@val pair to @buf only if @key does not
433 * @buf: buffer to hold @key or @key=@val
434 * @prefix: prefix string before @key
436 * @val: value string if it's a @key=@val pair
438 static void append_unique(char *buf, char *prefix, char *key, char *val,
447 anchor = end = strstr(buf, key);
448 /* try to find exact match string in @buf */
449 while (end && *end != '\0' && *end != ',' && *end != ' ' && *end != '=')
452 if (anchor == NULL || strlen(key) != len ||
453 strncmp(anchor, key, len) != 0) {
455 strscat(buf, prefix, maxbuflen);
457 strscat(buf, key, maxbuflen);
459 strscat(buf, "=\"", maxbuflen);
460 strscat(buf, val, maxbuflen);
461 strscat(buf, "\"", maxbuflen);
466 static int enable_default_ext4_features(struct mkfs_opts *mop, char *anchor,
467 size_t maxbuflen, bool user_spec)
469 unsigned long long blocks = mop->mo_device_kb / mop->mo_blocksize_kb;
470 bool enable_64bit = false;
472 /* Enable large block addresses if the LUN is over 2^32 blocks. */
473 if (blocks > 0xffffffffULL && is_e2fsprogs_feature_supp("-O 64bit"))
476 if (IS_OST(&mop->mo_ldd)) {
477 append_unique(anchor, user_spec ? "," : " -O ",
478 "extents", NULL, maxbuflen);
479 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
480 } else if (IS_MDT(&mop->mo_ldd)) {
481 append_unique(anchor, user_spec ? "," : " -O ",
482 "dirdata", NULL, maxbuflen);
483 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
485 append_unique(anchor, ",", "extents", NULL, maxbuflen);
487 append_unique(anchor, ",", "^extents", NULL, maxbuflen);
489 append_unique(anchor, user_spec ? "," : " -O ",
490 "uninit_bg", NULL, maxbuflen);
493 /* Multiple mount protection enabled only if failover node specified */
494 if (mop->mo_flags & MO_FAILOVER) {
495 if (is_e2fsprogs_feature_supp("-O mmp"))
496 append_unique(anchor, ",", "mmp", NULL, maxbuflen);
498 disp_old_e2fsprogs_msg("mmp", 1);
501 /* Allow more than 65000 subdirectories */
502 if (is_e2fsprogs_feature_supp("-O dir_nlink"))
503 append_unique(anchor, ",", "dir_nlink", NULL, maxbuflen);
505 /* The following options are only valid for ext4-based ldiskfs.
506 * If --backfstype=ext3 is specified, do not enable them. */
507 if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
510 /* Enable quota by default */
511 if (is_e2fsprogs_feature_supp("-O quota")) {
512 append_unique(anchor, ",", "quota", NULL, maxbuflen);
513 /* Enable project quota by default */
514 if (is_e2fsprogs_feature_supp("-O project"))
515 append_unique(anchor, ",", "project", NULL, maxbuflen);
518 fprintf(stderr, "\"-O quota\" must be supported by "
519 "e2fsprogs, please upgrade your e2fsprogs.\n");
523 /* Allow files larger than 2TB. Also needs LU-16, but not harmful. */
524 if (is_e2fsprogs_feature_supp("-O huge_file"))
525 append_unique(anchor, ",", "huge_file", NULL, maxbuflen);
528 append_unique(anchor, ",", "64bit", NULL, maxbuflen);
530 if (blocks >= 0x1000000000 && is_e2fsprogs_feature_supp("-O meta_bg"))
531 append_unique(anchor, ",", "meta_bg", NULL, maxbuflen);
533 if (enable_64bit || strstr(mop->mo_mkfsopts, "meta_bg"))
534 append_unique(anchor, ",", "^resize_inode", NULL, maxbuflen);
536 /* Allow xattrs larger than one block, stored in a separate inode */
537 if (IS_MDT(&mop->mo_ldd) && is_e2fsprogs_feature_supp("-O ea_inode"))
538 append_unique(anchor, ",", "ea_inode", NULL, maxbuflen);
540 /* Allow more than 10M directory entries */
541 if (IS_MDT(&mop->mo_ldd) && is_e2fsprogs_feature_supp("-O large_dir"))
542 append_unique(anchor, ",", "large_dir", NULL, maxbuflen);
544 /* Cluster inode/block bitmaps and inode table for more efficient IO.
545 * Align the flex groups on a 1MB boundary for better performance. */
546 /* This -O feature needs to go last, since it adds the "-G" option. */
547 if (is_e2fsprogs_feature_supp("-O flex_bg")) {
550 append_unique(anchor, ",", "flex_bg", NULL, maxbuflen);
552 if (IS_OST(&mop->mo_ldd) &&
553 strstr(mop->mo_mkfsopts, "-G") == NULL) {
554 snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
555 1024 / mop->mo_blocksize_kb);
556 strscat(anchor, tmp_buf, maxbuflen);
559 /* Don't add any more "-O" options here, see last comment above */
564 * moveopts_to_end: find the option string, move remaining strings to
565 * where option string starts, and append the option
567 * @start: where the option string starts before the move
568 * RETURN: where the option string starts after the move
570 static char *moveopts_to_end(char *start)
576 /* skip whitespace before options */
581 /* find end of option characters */
582 while (*end != ' ' && *end != '\0')
586 if (len >= sizeof(save))
587 len = sizeof(save) - 1;
590 strncpy(save, start, len);
593 /* move remaining options up front */
595 memmove(start, end, strlen(end));
596 *(start + strlen(end)) = '\0';
598 /* append the specified options */
599 if (*(start + strlen(start) - 1) != ' ')
601 idx = start + strlen(start);
607 /* Build fs according to type */
608 int ldiskfs_make_lustre(struct mkfs_opts *mop)
610 char mkfs_cmd[PATH_MAX];
614 int ret = 0, ext_opts = 0;
615 bool enable_64bit = false;
619 mop->mo_blocksize_kb = 4;
621 start = strstr(mop->mo_mkfsopts, "-b");
626 blocksize = strtol(start + 2, &end, 0);
627 if (end && (*end == 'k' || *end == 'K'))
629 /* EXT4_MIN_BLOCK_SIZE || EXT4_MAX_BLOCK_SIZE */
630 if (blocksize < 1024 || blocksize > 65536) {
632 "%s: blocksize %lu not in 1024-65536 bytes, normally 4096 bytes\n",
633 progname, blocksize);
637 if ((blocksize & (blocksize - 1)) != 0) {
639 "%s: blocksize %lu not a power-of-two value\n",
640 progname, blocksize);
643 mop->mo_blocksize_kb = blocksize >> 10;
646 if (!(mop->mo_flags & MO_IS_LOOP)) {
647 __u64 device_kb = get_device_size(mop->mo_device);
652 /* Compare to real size */
653 if (mop->mo_device_kb == 0 || device_kb < mop->mo_device_kb)
654 mop->mo_device_kb = device_kb;
657 if (mop->mo_device_kb != 0) {
660 if (mop->mo_device_kb < 32384) {
661 fprintf(stderr, "%s: size of filesystem must be larger "
662 "than 32MB, but is set to %lldKB\n",
663 progname, (long long)mop->mo_device_kb);
666 block_count = mop->mo_device_kb / mop->mo_blocksize_kb;
667 if (block_count > 0xffffffffULL) {
668 /* If the LUN size is just over 2^32 blocks, limit the
669 * filesystem size to 2^32-1 blocks to avoid problems
670 * with ldiskfs/mkfs not handling this well. b=22906
672 if (block_count < 0x100002000ULL)
674 0xffffffffULL * mop->mo_blocksize_kb;
680 if ((mop->mo_ldd.ldd_mount_type != LDD_MT_EXT3) &&
681 (mop->mo_ldd.ldd_mount_type != LDD_MT_LDISKFS) &&
682 (mop->mo_ldd.ldd_mount_type != LDD_MT_LDISKFS2)) {
683 fprintf(stderr, "%s: unsupported fs type: %d (%s)\n",
684 progname, mop->mo_ldd.ldd_mount_type,
685 MT_STR(&mop->mo_ldd));
690 /* Journal size in MB */
691 if (strstr(mop->mo_mkfsopts, "-J") == NULL &&
692 mop->mo_device_kb > 1024 * 1024) {
693 /* Choose our own default journal size */
694 long journal_mb = 0, max_mb;
696 /* cap journal size at 4GB for MDT, leave at 1GB for OSTs */
697 if (IS_MDT(&mop->mo_ldd))
699 else if (IS_OST(&mop->mo_ldd))
701 else /* Use mke2fs default size for MGS */
704 /* Use at most 4% of device for journal */
705 journal_mb = mop->mo_device_kb * 4 / (1024 * 100);
706 if (journal_mb > max_mb)
710 snprintf(buf, sizeof(buf), " -J size=%ld", journal_mb);
711 strscat(mop->mo_mkfsopts, buf,
712 sizeof(mop->mo_mkfsopts));
717 * The inode size is constituted by following elements
718 * (assuming all files are in composite layout and has
721 * ldiskfs inode size: 160
722 * MDT extended attributes size, including:
723 * ext4_xattr_header: 32
724 * LOV EA size: 32(lov_comp_md_v1) +
725 * 3 * 40(lov_comp_md_entry_v1) +
726 * 3 * 32(lov_mds_md) +
727 * stripes * 24(lov_ost_data) +
728 * 16(xattr_entry) + 4("lov")
729 * LMA EA size: 24(lustre_mdt_attrs) +
730 * 16(xattr_entry) + 4("lma")
731 * SOM EA size: 24(lustre_som_attrs) +
732 * 16(xattr_entry) + 4("som")
733 * link EA size: 24(link_ea_header) + 18(link_ea_entry) +
734 * 16(filename) + 16(xattr_entry) + 4("link")
735 * and some margin for 4-byte alignment, ACLs and other EAs.
737 * If we say the average filename length is about 32 bytes,
738 * the calculation looks like:
739 * 160 + 32 + (32+3*(40+32)+24*stripes+20) + (24+20) + (24+20) +
740 * (24+20) + (~42+16+20) + other <= 512*2^m, {m=0,1,2,3}
742 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
743 if (IS_MDT(&mop->mo_ldd)) {
744 if (mop->mo_stripe_count > 59)
745 inode_size = 512; /* bz 7241 */
746 /* see also "-i" below for EA blocks */
747 else if (mop->mo_stripe_count > 16)
751 } else if (IS_OST(&mop->mo_ldd)) {
752 /* We store MDS FID and necessary composite
753 * layout information in the OST object EA:
754 * ldiskfs inode size: 160
755 * OST extended attributes size, including:
756 * ext4_xattr_header: 32
757 * LMA EA size: 24(lustre_mdt_attrs) +
758 * 16(xattr_entry) + 4("lma")
759 * FID EA size: 52(filter_fid) +
760 * 16(xattr_entry) + 4("fid")
761 * 160 + 32 + (24+20) + (52+20) = 308
766 if (inode_size > 0) {
767 snprintf(buf, sizeof(buf), " -I %ld", inode_size);
768 strscat(mop->mo_mkfsopts, buf,
769 sizeof(mop->mo_mkfsopts));
773 /* Bytes_per_inode: disk size / num inodes */
774 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
775 strstr(mop->mo_mkfsopts, "-N") == NULL) {
776 long bytes_per_inode = 0;
778 /* Allocate more inodes on MDT devices. There is
779 * no data stored on the MDT, and very little extra
780 * metadata beyond the inode. It could go down as
781 * low as 1024 bytes, but this is conservative.
782 * Account for external EA blocks for wide striping.
784 if (IS_MDT(&mop->mo_ldd)) {
785 bytes_per_inode = inode_size + 1536;
787 if (mop->mo_stripe_count > 59) {
788 int extra = mop->mo_stripe_count * 24;
790 extra = ((extra - 1) | 4095) + 1;
791 bytes_per_inode += extra;
795 /* Allocate fewer inodes on large OST devices. Most
796 * filesystems can be much more aggressive than even
797 * this, but it is impossible to know in advance.
799 if (IS_OST(&mop->mo_ldd)) {
800 /* OST > 16TB assume average file size 1MB */
801 if (mop->mo_device_kb > (16ULL << 30))
802 bytes_per_inode = 1024 * 1024;
803 /* OST > 4TB assume average file size 512kB */
804 else if (mop->mo_device_kb > (4ULL << 30))
805 bytes_per_inode = 512 * 1024;
806 /* OST > 1TB assume average file size 256kB */
807 else if (mop->mo_device_kb > (1ULL << 30))
808 bytes_per_inode = 256 * 1024;
809 /* OST > 10GB assume average file size 64kB,
810 * plus a bit so that inodes will fit into a
811 * 256x flex_bg without overflowing.
813 else if (mop->mo_device_kb > (10ULL << 20))
814 bytes_per_inode = 69905;
817 if (bytes_per_inode > 0) {
818 snprintf(buf, sizeof(buf), " -i %ld", bytes_per_inode);
819 strscat(mop->mo_mkfsopts, buf,
820 sizeof(mop->mo_mkfsopts));
821 mop->mo_inode_size = bytes_per_inode;
826 strscat(mop->mo_mkfsopts, " -q", sizeof(mop->mo_mkfsopts));
828 /* start handle -O mkfs options */
829 start = strstr(mop->mo_mkfsopts, "-O");
831 if (strstr(start + 2, "-O") != NULL) {
833 "%s: don't specify multiple -O options\n",
837 start = moveopts_to_end(start);
838 maxbuflen = sizeof(mop->mo_mkfsopts) -
839 (start - mop->mo_mkfsopts) - strlen(start);
840 ret = enable_default_ext4_features(mop, start, maxbuflen, 1);
842 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
843 maxbuflen = sizeof(mop->mo_mkfsopts) - strlen(mop->mo_mkfsopts);
844 ret = enable_default_ext4_features(mop, start, maxbuflen, 0);
848 /* end handle -O mkfs options */
850 /* start handle -E mkfs options */
851 start = strstr(mop->mo_mkfsopts, "-E");
853 if (strstr(start + 2, "-E") != NULL) {
855 "%s: don't specify multiple -E options\n",
859 start = moveopts_to_end(start);
860 maxbuflen = sizeof(mop->mo_mkfsopts) -
861 (start - mop->mo_mkfsopts) - strlen(start);
864 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
865 maxbuflen = sizeof(mop->mo_mkfsopts) - strlen(mop->mo_mkfsopts);
868 /* In order to align the filesystem metadata on 1MB boundaries,
869 * give a resize value that will reserve a power-of-two group
870 * descriptor blocks, but leave one block for the superblock.
871 * Only useful for filesystems with < 2^32 blocks due to resize
874 if (!enable_64bit && strstr(mop->mo_mkfsopts, "meta_bg") == NULL &&
875 IS_OST(&mop->mo_ldd) && mop->mo_device_kb > 100 * 1024) {
876 unsigned int group_blocks = mop->mo_blocksize_kb * 8192;
877 unsigned int desc_per_block = mop->mo_blocksize_kb * 1024 / 32;
878 unsigned int resize_blks;
880 resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
881 snprintf(buf, sizeof(buf), "%u", resize_blks);
882 append_unique(start, ext_opts ? "," : " -E ",
883 "resize", buf, maxbuflen);
887 /* Avoid zeroing out the full journal - speeds up mkfs */
888 if (is_e2fsprogs_feature_supp("-E lazy_journal_init=0")) {
889 append_unique(start, ext_opts ? "," : " -E ",
890 "lazy_journal_init=0", NULL, maxbuflen);
893 if (is_e2fsprogs_feature_supp("-E lazy_itable_init=0")) {
894 append_unique(start, ext_opts ? "," : "-E",
895 "lazy_itable_init=0", NULL, maxbuflen);
899 /* end handle -E mkfs options */
901 /* Allow reformat of full devices (as opposed to partitions).
902 * We already checked for mounted dev.
904 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
906 snprintf(mkfs_cmd, sizeof(mkfs_cmd), "%s -j -b %d -L %s ", MKE2FS,
907 mop->mo_blocksize_kb * 1024, mop->mo_ldd.ldd_svname);
909 /* For loop device format the dev, not the filename */
910 dev = mop->mo_device;
911 if (mop->mo_flags & MO_IS_LOOP)
912 dev = mop->mo_loopdev;
914 vprint("formatting backing filesystem %s on %s\n",
915 MT_STR(&mop->mo_ldd), dev);
916 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
917 vprint("\tkilobytes %llu\n", mop->mo_device_kb);
918 vprint("\toptions %s\n", mop->mo_mkfsopts);
920 /* mkfs_cmd's trailing space is important! */
921 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
922 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
923 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
924 if (mop->mo_device_kb != 0) {
925 snprintf(buf, sizeof(buf), " %lluk",
926 (unsigned long long)mop->mo_device_kb);
927 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
930 vprint("mkfs_cmd = %s\n", mkfs_cmd);
931 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
934 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
939 int ldiskfs_prepare_lustre(struct mkfs_opts *mop,
940 char *wanted_mountopts, size_t len)
942 struct lustre_disk_data *ldd = &mop->mo_ldd;
945 /* Set MO_IS_LOOP to indicate a loopback device is needed */
946 ret = is_block(mop->mo_device);
949 } else if (ret == 0) {
950 mop->mo_flags |= MO_IS_LOOP;
953 if (IS_MDT(ldd) || IS_MGS(ldd))
954 strscat(wanted_mountopts, ",user_xattr", len);
959 int ldiskfs_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
961 if (strstr(mountopts, "errors=") == NULL)
962 strscat(mountopts, ",errors=remount-ro", len);
967 static int read_file(const char *path, char *buf, int size)
971 fd = fopen(path, "r");
975 if (fgets(buf, size, fd) == NULL) {
976 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
982 /* strip trailing newline */
984 if (buf[size - 1] == '\n')
985 buf[size - 1] = '\0';
990 static int write_file(const char *path, const char *buf)
994 fd = open(path, O_WRONLY);
998 rc = write(fd, buf, strlen(buf));
1001 return rc < 0 ? errno : 0;
1004 static int tune_md_stripe_cache_size(const char *sys_path,
1005 struct mount_opts *mop)
1007 char path[PATH_MAX];
1008 unsigned long old_stripe_cache_size;
1009 unsigned long new_stripe_cache_size;
1010 char buf[3 * sizeof(old_stripe_cache_size) + 2];
1013 if (mop->mo_md_stripe_cache_size <= 0)
1016 new_stripe_cache_size = mop->mo_md_stripe_cache_size;
1018 snprintf(path, sizeof(path), "%s/%s", sys_path, STRIPE_CACHE_SIZE);
1019 rc = read_file(path, buf, sizeof(buf));
1022 fprintf(stderr, "warning: cannot read '%s': %s\n",
1023 path, strerror(errno));
1027 old_stripe_cache_size = strtoul(buf, NULL, 0);
1028 if (old_stripe_cache_size == 0 || old_stripe_cache_size == ULONG_MAX)
1031 if (new_stripe_cache_size <= old_stripe_cache_size)
1034 snprintf(buf, sizeof(buf), "%lu", new_stripe_cache_size);
1035 rc = write_file(path, buf);
1038 fprintf(stderr, "warning: cannot write '%s': %s\n",
1039 path, strerror(errno));
1046 static int tune_max_sectors_kb(const char *sys_path, struct mount_opts *mop)
1048 char path[PATH_MAX];
1049 unsigned long max_hw_sectors_kb;
1050 unsigned long old_max_sectors_kb;
1051 unsigned long new_max_sectors_kb;
1052 char buf[3 * sizeof(old_max_sectors_kb) + 2];
1055 if (mop->mo_max_sectors_kb >= 0) {
1056 new_max_sectors_kb = mop->mo_max_sectors_kb;
1057 goto have_new_max_sectors_kb;
1060 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_HW_SECTORS_KB_PATH);
1061 rc = read_file(path, buf, sizeof(buf));
1063 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
1064 * error for some devices. */
1068 max_hw_sectors_kb = strtoul(buf, NULL, 0);
1069 if (max_hw_sectors_kb == 0 || max_hw_sectors_kb == ULLONG_MAX) {
1070 /* No digits at all or something weird. */
1074 new_max_sectors_kb = max_hw_sectors_kb;
1076 /* Don't increase IO request size limit past 16MB. It is
1077 * about PTLRPC_MAX_BRW_SIZE, but that isn't in a public
1078 * header. Note that even though the block layer allows
1079 * larger values, setting max_sectors_kb = 32768 causes
1080 * crashes (LU-6974). */
1081 if (new_max_sectors_kb > 16 * 1024)
1082 new_max_sectors_kb = 16 * 1024;
1084 have_new_max_sectors_kb:
1085 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_SECTORS_KB_PATH);
1086 rc = read_file(path, buf, sizeof(buf));
1088 /* No MAX_SECTORS_KB_PATH isn't necessary an error for
1093 old_max_sectors_kb = strtoul(buf, NULL, 0);
1094 if (old_max_sectors_kb == 0 || old_max_sectors_kb == ULLONG_MAX) {
1095 /* No digits at all or something weird. */
1099 if (new_max_sectors_kb <= old_max_sectors_kb)
1102 snprintf(buf, sizeof(buf), "%lu", new_max_sectors_kb);
1103 rc = write_file(path, buf);
1106 fprintf(stderr, "warning: cannot write '%s': %s\n",
1107 path, strerror(errno));
1111 fprintf(stderr, "%s: increased '%s' from %lu to %lu\n",
1112 progname, path, old_max_sectors_kb, new_max_sectors_kb);
1117 static int tune_block_dev_scheduler(const char *sys_path, const char *new_sched)
1119 char path[PATH_MAX];
1125 /* Before setting the scheduler, we need to check to see if
1126 * it's already set to "noop". If it is then we don't want to
1127 * override that setting. If it's set to anything other than
1128 * "noop" then set the scheduler to what has been passed
1131 snprintf(path, sizeof(path), "%s/%s", sys_path, SCHEDULER_PATH);
1132 rc = read_file(path, buf, sizeof(buf));
1135 fprintf(stderr, "%s: cannot read '%s': %s\n",
1136 progname, path, strerror(errno));
1141 /* The expected format of buf: noop anticipatory deadline [cfq] */
1142 s = strchr(buf, '[');
1143 e = strchr(buf, ']');
1145 /* If the format is not what we expect then be safe and error out. */
1146 if (s == NULL || e == NULL || !(s < e)) {
1149 "%s: cannot parse scheduler options for '%s'\n",
1158 if (strcmp(old_sched, "noop") == 0 ||
1159 strcmp(old_sched, "deadline") == 0 ||
1160 strcmp(old_sched, "mq-deadline") == 0 ||
1161 strstr(old_sched, new_sched) == 0)
1164 rc = write_file(path, new_sched);
1168 "%s: cannot set scheduler on '%s': %s\n",
1169 progname, path, strerror(errno));
1173 fprintf(stderr, "%s: changed scheduler of '%s' from %s to %s\n",
1174 progname, path, old_sched, new_sched);
1179 static int tune_block_dev(const char *src, struct mount_opts *mop);
1181 static int tune_block_dev_slaves(const char *sys_path, struct mount_opts *mop)
1183 char slaves_path[PATH_MAX];
1188 snprintf(slaves_path, sizeof(slaves_path), "%s/slaves", sys_path);
1189 slaves_dir = opendir(slaves_path);
1190 if (slaves_dir == NULL) {
1191 if (errno == ENOENT)
1197 while ((d = readdir(slaves_dir)) != NULL) {
1198 char path[PATH_MAX * 2];
1201 if (d->d_type != DT_LNK)
1204 snprintf(path, sizeof(path), "/dev/%s", d->d_name);
1205 rc2 = tune_block_dev(path, mop);
1210 closedir(slaves_dir);
1215 /* This is to tune the kernel for good SCSI performance.
1216 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
1217 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
1218 static int tune_block_dev(const char *src, struct mount_opts *mop)
1221 char sys_path[PATH_MAX];
1222 char partition_path[PATH_MAX + sizeof("partition")];
1223 char *real_sys_path = NULL;
1227 * Don't apply block device tuning for MDT or MGT devices,
1228 * since we don't need huge IO sizes to get good performance
1230 if (!IS_OST(&mop->mo_ldd))
1236 rc = stat(src, &st);
1239 fprintf(stderr, "warning: cannot stat '%s': %s\n",
1240 src, strerror(errno));
1244 if (!S_ISBLK(st.st_mode))
1247 if (major(st.st_rdev) == LOOP_MAJOR)
1250 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u",
1251 major(st.st_rdev), minor(st.st_rdev));
1253 snprintf(partition_path, sizeof(partition_path), "%s/partition",
1256 rc = access(partition_path, F_OK);
1258 if (errno == ENOENT)
1259 goto have_whole_dev;
1262 fprintf(stderr, "warning: cannot access '%s': %s\n",
1263 partition_path, strerror(errno));
1268 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u/..",
1269 major(st.st_rdev), minor(st.st_rdev));
1272 /* Since we recurse on slave devices we resolve the sys_path to
1273 * avoid path buffer overflows. */
1274 real_sys_path = realpath(sys_path, NULL);
1275 if (real_sys_path == NULL) {
1278 "warning: cannot resolve '%s': %s\n",
1279 sys_path, strerror(errno));
1284 if (major(st.st_rdev) == MD_MAJOR) {
1285 rc = tune_md_stripe_cache_size(real_sys_path, mop);
1287 /* Ignore errors from tune_max_sectors_kb() and
1288 * tune_scheduler(). The worst that will happen is a block
1289 * device with an "incorrect" scheduler. */
1290 tune_max_sectors_kb(real_sys_path, mop);
1291 tune_block_dev_scheduler(real_sys_path, DEFAULT_SCHEDULER);
1293 /* If device is multipath device then tune its slave
1295 rc = tune_block_dev_slaves(real_sys_path, mop);
1299 free(real_sys_path);
1304 int ldiskfs_tune_lustre(char *dev, struct mount_opts *mop)
1306 return tune_block_dev(dev, mop);
1309 int ldiskfs_label_lustre(struct mount_opts *mop)
1311 char label_cmd[PATH_MAX];
1314 snprintf(label_cmd, sizeof(label_cmd),
1315 TUNE2FS" -f -L '%s' '%s' >/dev/null 2>&1",
1316 mop->mo_ldd.ldd_svname, mop->mo_source);
1317 rc = run_command(label_cmd, sizeof(label_cmd));
1322 int ldiskfs_rename_fsname(struct mkfs_opts *mop, const char *oldname)
1324 struct mount_opts opts;
1325 struct lustre_disk_data *ldd = &mop->mo_ldd;
1326 char mntpt[] = "/tmp/mntXXXXXX";
1330 /* Change the filesystem label. */
1332 opts.mo_source = mop->mo_device;
1333 ret = ldiskfs_label_lustre(&opts);
1337 fprintf(stderr, "Can't change filesystem label: %s\n",
1342 /* Mount this device temporarily in order to write these files */
1343 if (mkdtemp(mntpt) == NULL) {
1348 fprintf(stderr, "Can't create temp mount point %s: %s\n",
1349 mntpt, strerror(ret));
1353 if (mop->mo_flags & MO_IS_LOOP)
1354 dev = mop->mo_loopdev;
1356 dev = mop->mo_device;
1357 ret = mount(dev, mntpt, MT_STR(ldd), 0, ldd->ldd_mount_opts);
1361 fprintf(stderr, "Unable to mount %s: %s\n",
1362 dev, strerror(ret));
1364 fprintf(stderr, "Is the %s module available?\n",
1369 ret = lustre_rename_fsname(mop, mntpt, oldname);
1377 /* Enable quota accounting */
1378 int ldiskfs_enable_quota(struct mkfs_opts *mop)
1382 int cmdsz = sizeof(cmd), ret;
1384 if (!is_e2fsprogs_feature_supp("-O quota")) {
1385 fprintf(stderr, "%s: \"-O quota\" is is not supported by "
1386 "current e2fsprogs\n", progname);
1390 dev = mop->mo_device;
1391 if (mop->mo_flags & MO_IS_LOOP)
1392 dev = mop->mo_loopdev;
1394 /* Quota feature is already enabled? */
1396 ext2fs_open(dev, open_flags, 0, 0, unix_io_manager, &backfs);
1397 if (backfs && ext2fs_has_feature_quota(backfs->super)) {
1398 vprint("Quota feature is already enabled.\n");
1402 /* Turn on quota feature by "tune2fs -O quota" */
1403 snprintf(cmd, cmdsz, "%s -O quota %s", TUNE2FS, dev);
1404 ret = run_command(cmd, cmdsz);
1406 fprintf(stderr, "command:%s (%d)", cmd, ret);
1411 int ldiskfs_init(void)
1413 /* Required because full path to DEBUGFS is not specified */
1414 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 0);
1419 void ldiskfs_fini(void)
1422 ext2fs_close(backfs);
1428 struct module_backfs_ops ldiskfs_ops = {
1429 .init = ldiskfs_init,
1430 .fini = ldiskfs_fini,
1431 .read_ldd = ldiskfs_read_ldd,
1432 .write_ldd = ldiskfs_write_ldd,
1433 .erase_ldd = ldiskfs_erase_ldd,
1434 .print_ldd_params = ldiskfs_print_ldd_params,
1435 .is_lustre = ldiskfs_is_lustre,
1436 .make_lustre = ldiskfs_make_lustre,
1437 .prepare_lustre = ldiskfs_prepare_lustre,
1438 .fix_mountopts = ldiskfs_fix_mountopts,
1439 .tune_lustre = ldiskfs_tune_lustre,
1440 .label_lustre = ldiskfs_label_lustre,
1441 .enable_quota = ldiskfs_enable_quota,
1442 .rename_fsname = ldiskfs_rename_fsname,
1444 #endif /* PLUGIN_DIR */