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>
68 #include <linux/fs.h> /* for BLKGETSIZE64 */
70 #include <linux/major.h>
71 #include <linux/types.h>
72 #include <linux/version.h>
73 #include <linux/lnet/lnetctl.h>
74 #include <linux/lustre/lustre_ver.h>
75 #include <libcfs/util/string.h>
78 #include <selinux/selinux.h>
81 #include "mount_utils.h"
83 #define MAX_HW_SECTORS_KB_PATH "queue/max_hw_sectors_kb"
84 #define MAX_SECTORS_KB_PATH "queue/max_sectors_kb"
85 #define SCHEDULER_PATH "queue/scheduler"
86 #define STRIPE_CACHE_SIZE "md/stripe_cache_size"
88 #define DEFAULT_SCHEDULER "deadline"
90 extern char *progname;
92 /* keep it less than LL_FID_NAMELEN */
93 #define DUMMY_FILE_NAME_LEN 25
94 #define EXT3_DIRENT_SIZE DUMMY_FILE_NAME_LEN
96 static void append_unique(char *buf, char *prefix, char *key, char *val,
98 static bool is_e2fsprogs_feature_supp(const char *feature);
99 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs);
102 * Concatenate context of the temporary mount point if selinux is enabled
105 static void append_context_for_mount(char *mntpt, struct mkfs_opts *mop)
107 security_context_t fcontext;
109 if (getfilecon(mntpt, &fcontext) < 0) {
110 /* Continuing with default behaviour */
111 fprintf(stderr, "%s: Get file context failed : %s\n",
112 progname, strerror(errno));
116 if (fcontext != NULL) {
117 append_unique(mop->mo_ldd.ldd_mount_opts,
118 ",", "context", fcontext,
119 sizeof(mop->mo_ldd.ldd_mount_opts));
125 /* Determine if a device is a block device (as opposed to a file) */
126 static int is_block(char *devname)
132 ret = cfs_abs_path(devname, &devpath);
134 fprintf(stderr, "%s: failed to resolve path '%s': %s\n",
135 progname, devname, strerror(-ret));
139 ret = access(devname, F_OK);
141 if (strncmp(devpath, "/dev/", 5) == 0) {
142 /* nobody sane wants to create a loopback file under
143 * /dev. Let's just report the device doesn't exist */
144 fprintf(stderr, "%s: %s apparently does not exist\n",
152 ret = stat(devpath, &st);
154 fprintf(stderr, "%s: cannot stat %s\n", progname, devpath);
157 ret = S_ISBLK(st.st_mode);
163 static int is_feature_enabled(const char *feature, const char *devpath)
167 char enabled_features[4096] = "";
170 snprintf(cmd, sizeof(cmd), "%s -c -R features %s 2>&1",
173 /* Using popen() instead of run_command() since debugfs does
174 * not return proper error code if command is not supported */
175 fp = popen(cmd, "r");
177 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
181 ret = fread(enabled_features, 1, sizeof(enabled_features) - 1, fp);
182 enabled_features[ret] = '\0';
185 if (strstr(enabled_features, feature))
190 /* Write the server config files */
191 int ldiskfs_write_ldd(struct mkfs_opts *mop)
193 char mntpt[] = "/tmp/mntXXXXXX";
200 /* Mount this device temporarily in order to write these files */
201 if (!mkdtemp(mntpt)) {
202 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
203 progname, mntpt, strerror(errno));
208 * Append file context to mount options if SE Linux is enabled
211 if (is_selinux_enabled() > 0)
212 append_context_for_mount(mntpt, mop);
215 dev = mop->mo_device;
216 if (mop->mo_flags & MO_IS_LOOP)
217 dev = mop->mo_loopdev;
219 /* Multiple mount protection enabled if failover node specified */
220 if (mop->mo_flags & MO_FAILOVER &&
221 !is_feature_enabled("mmp", dev)) {
222 if (is_e2fsprogs_feature_supp("-O mmp")) {
223 char *command = filepnm;
225 snprintf(command, sizeof(filepnm),
226 TUNE2FS" -O mmp '%s' >/dev/null 2>&1", dev);
227 ret = run_command(command, sizeof(filepnm));
230 "%s: Unable to set 'mmp' on %s: %d\n",
233 disp_old_e2fsprogs_msg("mmp", 1);
237 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0,
238 (mop->mo_mountopts == NULL) ?
239 "errors=remount-ro" : mop->mo_mountopts);
241 fprintf(stderr, "%s: Unable to mount %s: %s\n",
242 progname, dev, strerror(errno));
244 if (errno == ENODEV) {
245 fprintf(stderr, "Is the %s module available?\n",
246 MT_STR(&mop->mo_ldd));
251 /* Set up initial directories */
252 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
253 ret = mkdir(filepnm, 0777);
254 if ((ret != 0) && (errno != EEXIST)) {
255 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
256 progname, filepnm, strerror(errno));
258 } else if (errno == EEXIST) {
262 /* Save the persistent mount data into a file. Lustre must pre-read
263 this file to get the real mount options. */
264 vprint("Writing %s\n", MOUNT_DATA_FILE);
265 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
266 filep = fopen(filepnm, "w");
268 fprintf(stderr, "%s: Unable to create %s file: %s\n",
269 progname, filepnm, strerror(errno));
272 num = fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
273 if (num < 1 && ferror(filep)) {
274 fprintf(stderr, "%s: Unable to write to file (%s): %s\n",
275 progname, filepnm, strerror(errno));
279 fsync(filep->_fileno);
289 static int readcmd(char *cmd, char *buf, int len)
294 fp = popen(cmd, "r");
298 red = fread(buf, 1, len, fp);
301 /* strip trailing newline */
302 if (buf[red - 1] == '\n')
305 return (red == 0) ? -ENOENT : 0;
308 int ldiskfs_read_ldd(char *dev, struct lustre_disk_data *mo_ldd)
310 char tmpdir[] = "/tmp/dirXXXXXX";
315 int cmdsz = sizeof(cmd);
317 /* Make a temporary directory to hold Lustre data files. */
318 if (!mkdtemp(tmpdir)) {
319 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
320 progname, tmpdir, strerror(errno));
324 /* TODO: it's worth observing the get_mountdata() function that is
325 in mount_utils.c for getting the mountdata out of the
328 /* Construct debugfs command line. */
329 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' '%s'",
330 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
332 ret = run_command(cmd, cmdsz);
334 verrprint("%s: Unable to dump %s dir (%d)\n",
335 progname, MOUNT_CONFIGS_DIR, ret);
337 sprintf(filepnm, "%s/mountdata", tmpdir);
338 filep = fopen(filepnm, "r");
341 vprint("Reading %s\n", MOUNT_DATA_FILE);
342 num_read = fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
343 if (num_read < 1 && ferror(filep)) {
344 fprintf(stderr, "%s: Unable to read from file %s: %s\n",
345 progname, filepnm, strerror(errno));
350 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
351 run_command(cmd, cmdsz);
353 verrprint("Failed to read old data (%d)\n", ret);
355 /* As long as we at least have the label, we're good to go */
356 snprintf(cmd, sizeof(cmd), E2LABEL" %s", dev);
357 ret = readcmd(cmd, mo_ldd->ldd_svname, sizeof(mo_ldd->ldd_svname) - 1);
362 int ldiskfs_erase_ldd(struct mkfs_opts *mop, char *param)
367 void ldiskfs_print_ldd_params(struct mkfs_opts *mop)
369 printf("Parameters:%s\n", mop->mo_ldd.ldd_params);
372 /* Display the need for the latest e2fsprogs to be installed. make_backfs
373 * indicates if the caller is make_lustre_backfs() or not. */
374 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
376 static int msg_displayed;
379 fprintf(stderr, "WARNING: %s does not support %s "
380 "feature.\n\n", E2FSPROGS, feature);
386 fprintf(stderr, "WARNING: The %s package currently installed on "
387 "your system does not support \"%s\" feature.\n",
389 #if !(HAVE_LDISKFSPROGS)
390 fprintf(stderr, "Please install the latest version of e2fsprogs from\n"
391 "https://downloads.whamcloud.com/public/e2fsprogs/latest/\n"
392 "to enable this feature.\n");
395 fprintf(stderr, "Feature will not be enabled until %s"
396 "is updated and '%s -O %s %%{device}' "
397 "is run.\n\n", E2FSPROGS, TUNE2FS, feature);
400 /* Check whether the file exists in the device */
401 static int file_in_dev(char *file_name, char *dev_name)
404 char debugfs_cmd[256];
405 unsigned int inode_num;
408 /* Construct debugfs command line. */
409 snprintf(debugfs_cmd, sizeof(debugfs_cmd),
410 "%s -c -R 'stat %s' '%s' 2>&1 | egrep '(Inode|unsupported)'",
411 DEBUGFS, file_name, dev_name);
413 fp = popen(debugfs_cmd, "r");
415 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
419 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
423 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd) - 1, fp);
426 fprintf(stderr, "%s", debugfs_cmd);
427 if (strstr(debugfs_cmd, "unsupported feature")) {
428 disp_old_e2fsprogs_msg("an unknown", 0);
437 /* Check whether the device has already been used with lustre */
438 int ldiskfs_is_lustre(char *dev, unsigned *mount_type)
442 ret = file_in_dev(MOUNT_DATA_FILE, dev);
444 /* in the -1 case, 'extents' means IS a lustre target */
445 *mount_type = LDD_MT_LDISKFS;
449 ret = file_in_dev(LAST_RCVD, dev);
451 *mount_type = LDD_MT_LDISKFS;
458 /* Check if a certain feature is supported by e2fsprogs.
459 * Firstly we try to use "debugfs supported_features" command to check if
460 * the feature is supported. If this fails we try to set this feature with
461 * mke2fs to check for its support. */
462 static bool is_e2fsprogs_feature_supp(const char *feature)
464 static char supp_features[4096] = "";
467 char imgname[] = "/tmp/test-img-XXXXXX";
471 if (supp_features[0] == '\0') {
472 snprintf(cmd, sizeof(cmd), "%s -c -R supported_features 2>&1",
475 /* Using popen() instead of run_command() since debugfs does
476 * not return proper error code if command is not supported */
477 fp = popen(cmd, "r");
479 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
481 ret = fread(supp_features, 1,
482 sizeof(supp_features) - 1, fp);
483 supp_features[ret] = '\0';
488 if (strstr(supp_features,
489 strncmp(feature, "-O ", 3) ? feature : feature + 3))
492 if ((fd = mkstemp(imgname)) < 0)
497 snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
498 MKE2FS, feature, imgname);
499 /* run_command() displays the output of mke2fs when it fails for
500 * some feature, so use system() directly */
508 * append_unique: append @key or @key=@val pair to @buf only if @key does not
510 * @buf: buffer to hold @key or @key=@val
511 * @prefix: prefix string before @key
513 * @val: value string if it's a @key=@val pair
515 static void append_unique(char *buf, char *prefix, char *key, char *val,
524 anchor = end = strstr(buf, key);
525 /* try to find exact match string in @buf */
526 while (end && *end != '\0' && *end != ',' && *end != ' ' && *end != '=')
529 if (anchor == NULL || strlen(key) != len ||
530 strncmp(anchor, key, len) != 0) {
532 strscat(buf, prefix, maxbuflen);
534 strscat(buf, key, maxbuflen);
536 strscat(buf, "=\"", maxbuflen);
537 strscat(buf, val, maxbuflen);
538 strscat(buf, "\"", maxbuflen);
543 static int enable_default_ext4_features(struct mkfs_opts *mop, char *anchor,
544 size_t maxbuflen, bool user_spec)
546 unsigned long long blocks = mop->mo_device_kb / mop->mo_blocksize_kb;
547 bool enable_64bit = false;
549 /* Enable large block addresses if the LUN is over 2^32 blocks. */
550 if (blocks > 0xffffffffULL && is_e2fsprogs_feature_supp("-O 64bit"))
553 if (IS_OST(&mop->mo_ldd)) {
554 append_unique(anchor, user_spec ? "," : " -O ",
555 "extents", NULL, maxbuflen);
556 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
557 } else if (IS_MDT(&mop->mo_ldd)) {
558 append_unique(anchor, user_spec ? "," : " -O ",
559 "dirdata", NULL, maxbuflen);
560 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
562 append_unique(anchor, ",", "extents", NULL, maxbuflen);
564 append_unique(anchor, ",", "^extents", NULL, maxbuflen);
566 append_unique(anchor, user_spec ? "," : " -O ",
567 "uninit_bg", NULL, maxbuflen);
570 /* Multiple mount protection enabled only if failover node specified */
571 if (mop->mo_flags & MO_FAILOVER) {
572 if (is_e2fsprogs_feature_supp("-O mmp"))
573 append_unique(anchor, ",", "mmp", NULL, maxbuflen);
575 disp_old_e2fsprogs_msg("mmp", 1);
578 /* Allow more than 65000 subdirectories */
579 if (is_e2fsprogs_feature_supp("-O dir_nlink"))
580 append_unique(anchor, ",", "dir_nlink", NULL, maxbuflen);
582 /* The following options are only valid for ext4-based ldiskfs.
583 * If --backfstype=ext3 is specified, do not enable them. */
584 if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
587 /* Enable quota by default */
588 if (is_e2fsprogs_feature_supp("-O quota")) {
589 append_unique(anchor, ",", "quota", NULL, maxbuflen);
592 fprintf(stderr, "\"-O quota\" must be supported by "
593 "e2fsprogs, please upgrade your e2fsprogs.\n");
597 /* Allow files larger than 2TB. Also needs LU-16, but not harmful. */
598 if (is_e2fsprogs_feature_supp("-O huge_file"))
599 append_unique(anchor, ",", "huge_file", NULL, maxbuflen);
602 append_unique(anchor, ",", "64bit", NULL, maxbuflen);
604 if (blocks >= 0x1000000000 && is_e2fsprogs_feature_supp("-O meta_bg"))
605 append_unique(anchor, ",", "meta_bg", NULL, maxbuflen);
607 if (enable_64bit || strstr(mop->mo_mkfsopts, "meta_bg"))
608 append_unique(anchor, ",", "^resize_inode", NULL, maxbuflen);
610 /* Cluster inode/block bitmaps and inode table for more efficient IO.
611 * Align the flex groups on a 1MB boundary for better performance. */
612 /* This -O feature needs to go last, since it adds the "-G" option. */
613 if (is_e2fsprogs_feature_supp("-O flex_bg")) {
616 append_unique(anchor, ",", "flex_bg", NULL, maxbuflen);
618 if (IS_OST(&mop->mo_ldd) &&
619 strstr(mop->mo_mkfsopts, "-G") == NULL) {
620 snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
621 1024 / mop->mo_blocksize_kb);
622 strscat(anchor, tmp_buf, maxbuflen);
625 /* Don't add any more "-O" options here, see last comment above */
630 * moveopts_to_end: find the option string, move remaining strings to
631 * where option string starts, and append the option
633 * @start: where the option string starts before the move
634 * RETURN: where the option string starts after the move
636 static char *moveopts_to_end(char *start)
642 /* skip whitespace before options */
647 /* find end of option characters */
648 while (*end != ' ' && *end != '\0')
652 if (len >= sizeof(save))
653 len = sizeof(save) - 1;
656 strncpy(save, start, len);
659 /* move remaining options up front */
661 memmove(start, end, strlen(end));
662 *(start + strlen(end)) = '\0';
664 /* append the specified options */
665 if (*(start + strlen(start) - 1) != ' ')
667 idx = start + strlen(start);
673 /* Build fs according to type */
674 int ldiskfs_make_lustre(struct mkfs_opts *mop)
676 char mkfs_cmd[PATH_MAX];
680 int ret = 0, ext_opts = 0;
681 bool enable_64bit = false;
685 mop->mo_blocksize_kb = 4;
687 start = strstr(mop->mo_mkfsopts, "-b");
692 blocksize = strtol(start + 2, &end, 0);
693 if (end && (*end == 'k' || *end == 'K'))
695 /* EXT4_MIN_BLOCK_SIZE || EXT4_MAX_BLOCK_SIZE */
696 if (blocksize < 1024 || blocksize > 65536) {
698 "%s: blocksize %lu not in 1024-65536 bytes, normally 4096 bytes\n",
699 progname, blocksize);
703 if ((blocksize & (blocksize - 1)) != 0) {
705 "%s: blocksize %lu not a power-of-two value\n",
706 progname, blocksize);
709 mop->mo_blocksize_kb = blocksize >> 10;
712 if (!(mop->mo_flags & MO_IS_LOOP)) {
713 __u64 device_kb = get_device_size(mop->mo_device);
718 /* Compare to real size */
719 if (mop->mo_device_kb == 0 || device_kb < mop->mo_device_kb)
720 mop->mo_device_kb = device_kb;
723 if (mop->mo_device_kb != 0) {
726 if (mop->mo_device_kb < 32384) {
727 fprintf(stderr, "%s: size of filesystem must be larger "
728 "than 32MB, but is set to %lldKB\n",
729 progname, (long long)mop->mo_device_kb);
732 block_count = mop->mo_device_kb / mop->mo_blocksize_kb;
733 if (block_count > 0xffffffffULL) {
734 /* If the LUN size is just over 2^32 blocks, limit the
735 * filesystem size to 2^32-1 blocks to avoid problems
736 * with ldiskfs/mkfs not handling this well. b=22906
738 if (block_count < 0x100002000ULL)
740 0xffffffffULL * mop->mo_blocksize_kb;
746 if ((mop->mo_ldd.ldd_mount_type != LDD_MT_EXT3) &&
747 (mop->mo_ldd.ldd_mount_type != LDD_MT_LDISKFS) &&
748 (mop->mo_ldd.ldd_mount_type != LDD_MT_LDISKFS2)) {
749 fprintf(stderr, "%s: unsupported fs type: %d (%s)\n",
750 progname, mop->mo_ldd.ldd_mount_type,
751 MT_STR(&mop->mo_ldd));
756 /* Journal size in MB */
757 if (strstr(mop->mo_mkfsopts, "-J") == NULL &&
758 mop->mo_device_kb > 1024 * 1024) {
759 /* Choose our own default journal size */
760 long journal_mb = 0, max_mb;
762 /* cap journal size at 4GB for MDT, leave at 1GB for OSTs */
763 if (IS_MDT(&mop->mo_ldd))
765 else if (IS_OST(&mop->mo_ldd))
767 else /* Use mke2fs default size for MGS */
770 /* Use at most 4% of device for journal */
771 journal_mb = mop->mo_device_kb * 4 / (1024 * 100);
772 if (journal_mb > max_mb)
776 snprintf(buf, sizeof(buf), " -J size=%ld", journal_mb);
777 strscat(mop->mo_mkfsopts, buf,
778 sizeof(mop->mo_mkfsopts));
783 * The inode size is constituted by following elements
784 * (assuming all files are in composite layout and has
787 * ldiskfs inode size: 160
788 * MDT extended attributes size, including:
789 * ext4_xattr_header: 32
790 * LOV EA size: 32(lov_comp_md_v1) +
791 * 3 * 40(lov_comp_md_entry_v1) +
792 * 3 * 32(lov_mds_md) +
793 * stripes * 24(lov_ost_data) +
794 * 16(xattr_entry) + 4("lov")
795 * LMA EA size: 24(lustre_mdt_attrs) +
796 * 16(xattr_entry) + 4("lma")
797 * SOM EA size: 24(lustre_som_attrs) +
798 * 16(xattr_entry) + 4("som")
799 * link EA size: 24(link_ea_header) + 18(link_ea_entry) +
800 * 16(filename) + 16(xattr_entry) + 4("link")
801 * and some margin for 4-byte alignment, ACLs and other EAs.
803 * If we say the average filename length is about 32 bytes,
804 * the calculation looks like:
805 * 160 + 32 + (32+3*(40+32)+24*stripes+20) + (24+20) + (24+20) +
806 * (24+20) + (~42+16+20) + other <= 512*2^m, {m=0,1,2,3}
808 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
809 if (IS_MDT(&mop->mo_ldd)) {
810 if (mop->mo_stripe_count > 59)
811 inode_size = 512; /* bz 7241 */
812 /* see also "-i" below for EA blocks */
813 else if (mop->mo_stripe_count > 16)
817 } else if (IS_OST(&mop->mo_ldd)) {
818 /* We store MDS FID and necessary composite
819 * layout information in the OST object EA:
820 * ldiskfs inode size: 160
821 * OST extended attributes size, including:
822 * ext4_xattr_header: 32
823 * LMA EA size: 24(lustre_mdt_attrs) +
824 * 16(xattr_entry) + 4("lma")
825 * FID EA size: 52(filter_fid) +
826 * 16(xattr_entry) + 4("fid")
827 * 160 + 32 + (24+20) + (52+20) = 308
832 if (inode_size > 0) {
833 snprintf(buf, sizeof(buf), " -I %ld", inode_size);
834 strscat(mop->mo_mkfsopts, buf,
835 sizeof(mop->mo_mkfsopts));
839 /* Bytes_per_inode: disk size / num inodes */
840 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
841 strstr(mop->mo_mkfsopts, "-N") == NULL) {
842 long bytes_per_inode = 0;
844 /* Allocate more inodes on MDT devices. There is
845 * no data stored on the MDT, and very little extra
846 * metadata beyond the inode. It could go down as
847 * low as 1024 bytes, but this is conservative.
848 * Account for external EA blocks for wide striping.
850 if (IS_MDT(&mop->mo_ldd)) {
851 bytes_per_inode = inode_size + 1536;
853 if (mop->mo_stripe_count > 59) {
854 int extra = mop->mo_stripe_count * 24;
856 extra = ((extra - 1) | 4095) + 1;
857 bytes_per_inode += extra;
861 /* Allocate fewer inodes on large OST devices. Most
862 * filesystems can be much more aggressive than even
863 * this, but it is impossible to know in advance.
865 if (IS_OST(&mop->mo_ldd)) {
866 /* OST > 16TB assume average file size 1MB */
867 if (mop->mo_device_kb > (16ULL << 30))
868 bytes_per_inode = 1024 * 1024;
869 /* OST > 4TB assume average file size 512kB */
870 else if (mop->mo_device_kb > (4ULL << 30))
871 bytes_per_inode = 512 * 1024;
872 /* OST > 1TB assume average file size 256kB */
873 else if (mop->mo_device_kb > (1ULL << 30))
874 bytes_per_inode = 256 * 1024;
875 /* OST > 10GB assume average file size 64kB,
876 * plus a bit so that inodes will fit into a
877 * 256x flex_bg without overflowing.
879 else if (mop->mo_device_kb > (10ULL << 20))
880 bytes_per_inode = 69905;
883 if (bytes_per_inode > 0) {
884 snprintf(buf, sizeof(buf), " -i %ld", bytes_per_inode);
885 strscat(mop->mo_mkfsopts, buf,
886 sizeof(mop->mo_mkfsopts));
887 mop->mo_inode_size = bytes_per_inode;
892 strscat(mop->mo_mkfsopts, " -q", sizeof(mop->mo_mkfsopts));
894 /* start handle -O mkfs options */
895 start = strstr(mop->mo_mkfsopts, "-O");
897 if (strstr(start + 2, "-O") != NULL) {
899 "%s: don't specify multiple -O options\n",
903 start = moveopts_to_end(start);
904 maxbuflen = sizeof(mop->mo_mkfsopts) -
905 (start - mop->mo_mkfsopts) - strlen(start);
906 ret = enable_default_ext4_features(mop, start, maxbuflen, 1);
908 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
909 maxbuflen = sizeof(mop->mo_mkfsopts) - strlen(mop->mo_mkfsopts);
910 ret = enable_default_ext4_features(mop, start, maxbuflen, 0);
914 /* end handle -O mkfs options */
916 /* start handle -E mkfs options */
917 start = strstr(mop->mo_mkfsopts, "-E");
919 if (strstr(start + 2, "-E") != NULL) {
921 "%s: don't specify multiple -E options\n",
925 start = moveopts_to_end(start);
926 maxbuflen = sizeof(mop->mo_mkfsopts) -
927 (start - mop->mo_mkfsopts) - strlen(start);
930 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
931 maxbuflen = sizeof(mop->mo_mkfsopts) - strlen(mop->mo_mkfsopts);
934 /* In order to align the filesystem metadata on 1MB boundaries,
935 * give a resize value that will reserve a power-of-two group
936 * descriptor blocks, but leave one block for the superblock.
937 * Only useful for filesystems with < 2^32 blocks due to resize
940 if (!enable_64bit && strstr(mop->mo_mkfsopts, "meta_bg") == NULL &&
941 IS_OST(&mop->mo_ldd) && mop->mo_device_kb > 100 * 1024) {
942 unsigned int group_blocks = mop->mo_blocksize_kb * 8192;
943 unsigned int desc_per_block = mop->mo_blocksize_kb * 1024 / 32;
944 unsigned int resize_blks;
946 resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
947 snprintf(buf, sizeof(buf), "%u", resize_blks);
948 append_unique(start, ext_opts ? "," : " -E ",
949 "resize", buf, maxbuflen);
953 /* Avoid zeroing out the full journal - speeds up mkfs */
954 if (is_e2fsprogs_feature_supp("-E lazy_journal_init"))
955 append_unique(start, ext_opts ? "," : " -E ",
956 "lazy_journal_init", NULL, maxbuflen);
957 /* end handle -E mkfs options */
959 /* Allow reformat of full devices (as opposed to partitions).
960 * We already checked for mounted dev.
962 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
964 snprintf(mkfs_cmd, sizeof(mkfs_cmd), "%s -j -b %d -L %s ", MKE2FS,
965 mop->mo_blocksize_kb * 1024, mop->mo_ldd.ldd_svname);
967 /* For loop device format the dev, not the filename */
968 dev = mop->mo_device;
969 if (mop->mo_flags & MO_IS_LOOP)
970 dev = mop->mo_loopdev;
972 vprint("formatting backing filesystem %s on %s\n",
973 MT_STR(&mop->mo_ldd), dev);
974 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
975 vprint("\tkilobytes %llu\n", mop->mo_device_kb);
976 vprint("\toptions %s\n", mop->mo_mkfsopts);
978 /* mkfs_cmd's trailing space is important! */
979 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
980 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
981 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
982 if (mop->mo_device_kb != 0) {
983 snprintf(buf, sizeof(buf), " %lluk",
984 (unsigned long long)mop->mo_device_kb);
985 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
988 vprint("mkfs_cmd = %s\n", mkfs_cmd);
989 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
992 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
997 int ldiskfs_prepare_lustre(struct mkfs_opts *mop,
998 char *wanted_mountopts, size_t len)
1000 struct lustre_disk_data *ldd = &mop->mo_ldd;
1003 /* Set MO_IS_LOOP to indicate a loopback device is needed */
1004 ret = is_block(mop->mo_device);
1007 } else if (ret == 0) {
1008 mop->mo_flags |= MO_IS_LOOP;
1011 if (IS_MDT(ldd) || IS_MGS(ldd))
1012 strscat(wanted_mountopts, ",user_xattr", len);
1017 int ldiskfs_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
1019 if (strstr(mountopts, "errors=") == NULL)
1020 strscat(mountopts, ",errors=remount-ro", len);
1025 static int read_file(const char *path, char *buf, int size)
1029 fd = fopen(path, "r");
1033 if (fgets(buf, size, fd) == NULL) {
1034 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
1040 /* strip trailing newline */
1042 if (buf[size - 1] == '\n')
1043 buf[size - 1] = '\0';
1048 static int write_file(const char *path, const char *buf)
1052 fd = open(path, O_WRONLY);
1056 rc = write(fd, buf, strlen(buf));
1059 return rc < 0 ? errno : 0;
1062 static int tune_md_stripe_cache_size(const char *sys_path,
1063 struct mount_opts *mop)
1065 char path[PATH_MAX];
1066 unsigned long old_stripe_cache_size;
1067 unsigned long new_stripe_cache_size;
1068 char buf[3 * sizeof(old_stripe_cache_size) + 2];
1071 if (mop->mo_md_stripe_cache_size <= 0)
1074 new_stripe_cache_size = mop->mo_md_stripe_cache_size;
1076 snprintf(path, sizeof(path), "%s/%s", sys_path, STRIPE_CACHE_SIZE);
1077 rc = read_file(path, buf, sizeof(buf));
1080 fprintf(stderr, "warning: cannot read '%s': %s\n",
1081 path, strerror(errno));
1085 old_stripe_cache_size = strtoul(buf, NULL, 0);
1086 if (old_stripe_cache_size == 0 || old_stripe_cache_size == ULONG_MAX)
1089 if (new_stripe_cache_size <= old_stripe_cache_size)
1092 snprintf(buf, sizeof(buf), "%lu", new_stripe_cache_size);
1093 rc = write_file(path, buf);
1096 fprintf(stderr, "warning: cannot write '%s': %s\n",
1097 path, strerror(errno));
1104 static int tune_max_sectors_kb(const char *sys_path, struct mount_opts *mop)
1106 char path[PATH_MAX];
1107 unsigned long max_hw_sectors_kb;
1108 unsigned long old_max_sectors_kb;
1109 unsigned long new_max_sectors_kb;
1110 char buf[3 * sizeof(old_max_sectors_kb) + 2];
1113 if (mop->mo_max_sectors_kb >= 0) {
1114 new_max_sectors_kb = mop->mo_max_sectors_kb;
1115 goto have_new_max_sectors_kb;
1118 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_HW_SECTORS_KB_PATH);
1119 rc = read_file(path, buf, sizeof(buf));
1121 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
1122 * error for some devices. */
1126 max_hw_sectors_kb = strtoul(buf, NULL, 0);
1127 if (max_hw_sectors_kb == 0 || max_hw_sectors_kb == ULLONG_MAX) {
1128 /* No digits at all or something weird. */
1132 new_max_sectors_kb = max_hw_sectors_kb;
1134 /* Don't increase IO request size limit past 16MB. It is
1135 * about PTLRPC_MAX_BRW_SIZE, but that isn't in a public
1136 * header. Note that even though the block layer allows
1137 * larger values, setting max_sectors_kb = 32768 causes
1138 * crashes (LU-6974). */
1139 if (new_max_sectors_kb > 16 * 1024)
1140 new_max_sectors_kb = 16 * 1024;
1142 have_new_max_sectors_kb:
1143 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_SECTORS_KB_PATH);
1144 rc = read_file(path, buf, sizeof(buf));
1146 /* No MAX_SECTORS_KB_PATH isn't necessary an error for
1151 old_max_sectors_kb = strtoul(buf, NULL, 0);
1152 if (old_max_sectors_kb == 0 || old_max_sectors_kb == ULLONG_MAX) {
1153 /* No digits at all or something weird. */
1157 if (new_max_sectors_kb <= old_max_sectors_kb)
1160 snprintf(buf, sizeof(buf), "%lu", new_max_sectors_kb);
1161 rc = write_file(path, buf);
1164 fprintf(stderr, "warning: cannot write '%s': %s\n",
1165 path, strerror(errno));
1169 fprintf(stderr, "%s: increased '%s' from %lu to %lu\n",
1170 progname, path, old_max_sectors_kb, new_max_sectors_kb);
1175 static int tune_block_dev_scheduler(const char *sys_path, const char *new_sched)
1177 char path[PATH_MAX];
1183 /* Before setting the scheduler, we need to check to see if
1184 * it's already set to "noop". If it is then we don't want to
1185 * override that setting. If it's set to anything other than
1186 * "noop" then set the scheduler to what has been passed
1189 snprintf(path, sizeof(path), "%s/%s", sys_path, SCHEDULER_PATH);
1190 rc = read_file(path, buf, sizeof(buf));
1193 fprintf(stderr, "%s: cannot read '%s': %s\n",
1194 progname, path, strerror(errno));
1199 /* The expected format of buf: noop anticipatory deadline [cfq] */
1200 s = strchr(buf, '[');
1201 e = strchr(buf, ']');
1203 /* If the format is not what we expect then be safe and error out. */
1204 if (s == NULL || e == NULL || !(s < e)) {
1207 "%s: cannot parse scheduler options for '%s'\n",
1216 if (strcmp(old_sched, "noop") == 0 ||
1217 strcmp(old_sched, "deadline") == 0 ||
1218 strcmp(old_sched, "mq-deadline") == 0 ||
1219 strstr(old_sched, new_sched) == 0)
1222 rc = write_file(path, new_sched);
1226 "%s: cannot set scheduler on '%s': %s\n",
1227 progname, path, strerror(errno));
1231 fprintf(stderr, "%s: changed scheduler of '%s' from %s to %s\n",
1232 progname, path, old_sched, new_sched);
1237 static int tune_block_dev(const char *src, struct mount_opts *mop);
1239 static int tune_block_dev_slaves(const char *sys_path, struct mount_opts *mop)
1241 char slaves_path[PATH_MAX];
1246 snprintf(slaves_path, sizeof(slaves_path), "%s/slaves", sys_path);
1247 slaves_dir = opendir(slaves_path);
1248 if (slaves_dir == NULL) {
1249 if (errno == ENOENT)
1255 while ((d = readdir(slaves_dir)) != NULL) {
1256 char path[PATH_MAX];
1259 if (d->d_type != DT_LNK)
1262 snprintf(path, sizeof(path), "%s/%s", slaves_path, d->d_name);
1263 rc2 = tune_block_dev(path, mop);
1268 closedir(slaves_dir);
1273 /* This is to tune the kernel for good SCSI performance.
1274 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
1275 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
1276 static int tune_block_dev(const char *src, struct mount_opts *mop)
1279 char sys_path[PATH_MAX];
1280 char partition_path[PATH_MAX];
1281 char *real_sys_path = NULL;
1285 * Don't apply block device tuning for MDT or MGT devices,
1286 * since we don't need huge IO sizes to get good performance
1288 if (!IS_OST(&mop->mo_ldd))
1294 rc = stat(src, &st);
1297 fprintf(stderr, "warning: cannot stat '%s': %s\n",
1298 src, strerror(errno));
1302 if (!S_ISBLK(st.st_mode))
1305 if (major(st.st_rdev) == LOOP_MAJOR)
1308 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u",
1309 major(st.st_rdev), minor(st.st_rdev));
1311 snprintf(partition_path, sizeof(partition_path), "%s/partition",
1314 rc = access(partition_path, F_OK);
1316 if (errno == ENOENT)
1317 goto have_whole_dev;
1320 fprintf(stderr, "warning: cannot access '%s': %s\n",
1321 partition_path, strerror(errno));
1326 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u/..",
1327 major(st.st_rdev), minor(st.st_rdev));
1330 /* Since we recurse on slave devices we resolve the sys_path to
1331 * avoid path buffer overflows. */
1332 real_sys_path = realpath(sys_path, NULL);
1333 if (real_sys_path == NULL) {
1336 "warning: cannot resolve '%s': %s\n",
1337 sys_path, strerror(errno));
1342 if (major(st.st_rdev) == MD_MAJOR) {
1343 rc = tune_md_stripe_cache_size(real_sys_path, mop);
1345 /* Ignore errors from tune_max_sectors_kb() and
1346 * tune_scheduler(). The worst that will happen is a block
1347 * device with an "incorrect" scheduler. */
1348 tune_max_sectors_kb(real_sys_path, mop);
1349 tune_block_dev_scheduler(real_sys_path, DEFAULT_SCHEDULER);
1351 /* If device is multipath device then tune its slave
1353 rc = tune_block_dev_slaves(real_sys_path, mop);
1357 free(real_sys_path);
1362 int ldiskfs_tune_lustre(char *dev, struct mount_opts *mop)
1364 return tune_block_dev(dev, mop);
1367 int ldiskfs_label_lustre(struct mount_opts *mop)
1369 char label_cmd[PATH_MAX];
1372 snprintf(label_cmd, sizeof(label_cmd),
1373 TUNE2FS" -f -L '%s' '%s' >/dev/null 2>&1",
1374 mop->mo_ldd.ldd_svname, mop->mo_source);
1375 rc = run_command(label_cmd, sizeof(label_cmd));
1380 int ldiskfs_rename_fsname(struct mkfs_opts *mop, const char *oldname)
1382 struct mount_opts opts;
1383 struct lustre_disk_data *ldd = &mop->mo_ldd;
1384 char mntpt[] = "/tmp/mntXXXXXX";
1388 /* Change the filesystem label. */
1390 opts.mo_source = mop->mo_device;
1391 ret = ldiskfs_label_lustre(&opts);
1395 fprintf(stderr, "Can't change filesystem label: %s\n",
1400 /* Mount this device temporarily in order to write these files */
1401 if (mkdtemp(mntpt) == NULL) {
1406 fprintf(stderr, "Can't create temp mount point %s: %s\n",
1407 mntpt, strerror(ret));
1413 * Append file context to mount options if SE Linux is enabled
1415 if (is_selinux_enabled() > 0)
1416 append_context_for_mount(mntpt, mop);
1419 if (mop->mo_flags & MO_IS_LOOP)
1420 dev = mop->mo_loopdev;
1422 dev = mop->mo_device;
1423 ret = mount(dev, mntpt, MT_STR(ldd), 0, ldd->ldd_mount_opts);
1427 fprintf(stderr, "Unable to mount %s: %s\n",
1428 dev, strerror(ret));
1430 fprintf(stderr, "Is the %s module available?\n",
1435 ret = lustre_rename_fsname(mop, mntpt, oldname);
1443 /* Enable quota accounting */
1444 int ldiskfs_enable_quota(struct mkfs_opts *mop)
1448 int cmdsz = sizeof(cmd), ret;
1450 if (!is_e2fsprogs_feature_supp("-O quota")) {
1451 fprintf(stderr, "%s: \"-O quota\" is is not supported by "
1452 "current e2fsprogs\n", progname);
1456 dev = mop->mo_device;
1457 if (mop->mo_flags & MO_IS_LOOP)
1458 dev = mop->mo_loopdev;
1460 /* Quota feature is already enabled? */
1461 if (is_feature_enabled("quota", dev)) {
1462 vprint("Quota feature is already enabled.\n");
1466 /* Turn on quota feature by "tune2fs -O quota" */
1467 snprintf(cmd, cmdsz, "%s -O quota %s", TUNE2FS, dev);
1468 ret = run_command(cmd, cmdsz);
1470 fprintf(stderr, "command:%s (%d)", cmd, ret);
1475 int ldiskfs_init(void)
1477 /* Required because full path to DEBUGFS is not specified */
1478 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 0);
1483 void ldiskfs_fini(void)
1489 struct module_backfs_ops ldiskfs_ops = {
1490 .init = ldiskfs_init,
1491 .fini = ldiskfs_fini,
1492 .read_ldd = ldiskfs_read_ldd,
1493 .write_ldd = ldiskfs_write_ldd,
1494 .erase_ldd = ldiskfs_erase_ldd,
1495 .print_ldd_params = ldiskfs_print_ldd_params,
1496 .is_lustre = ldiskfs_is_lustre,
1497 .make_lustre = ldiskfs_make_lustre,
1498 .prepare_lustre = ldiskfs_prepare_lustre,
1499 .fix_mountopts = ldiskfs_fix_mountopts,
1500 .tune_lustre = ldiskfs_tune_lustre,
1501 .label_lustre = ldiskfs_label_lustre,
1502 .enable_quota = ldiskfs_enable_quota,
1503 .rename_fsname = ldiskfs_rename_fsname,
1505 #endif /* PLUGIN_DIR */