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 #define L_BLOCK_SIZE 4096
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 int is_e2fsprogs_feature_supp(const char *feature);
100 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs);
103 * Concatenate context of the temporary mount point if selinux is enabled
106 static void append_context_for_mount(char *mntpt, struct mkfs_opts *mop)
108 security_context_t fcontext;
110 if (getfilecon(mntpt, &fcontext) < 0) {
111 /* Continuing with default behaviour */
112 fprintf(stderr, "%s: Get file context failed : %s\n",
113 progname, strerror(errno));
117 if (fcontext != NULL) {
118 append_unique(mop->mo_ldd.ldd_mount_opts,
119 ",", "context", fcontext,
120 sizeof(mop->mo_ldd.ldd_mount_opts));
126 /* Determine if a device is a block device (as opposed to a file) */
127 static int is_block(char *devname)
133 ret = cfs_abs_path(devname, &devpath);
135 fprintf(stderr, "%s: failed to resolve path '%s': %s\n",
136 progname, devname, strerror(-ret));
140 ret = access(devname, F_OK);
142 if (strncmp(devpath, "/dev/", 5) == 0) {
143 /* nobody sane wants to create a loopback file under
144 * /dev. Let's just report the device doesn't exist */
145 fprintf(stderr, "%s: %s apparently does not exist\n",
153 ret = stat(devpath, &st);
155 fprintf(stderr, "%s: cannot stat %s\n", progname, devpath);
158 ret = S_ISBLK(st.st_mode);
164 static int is_feature_enabled(const char *feature, const char *devpath)
168 char enabled_features[4096] = "";
171 snprintf(cmd, sizeof(cmd), "%s -c -R features %s 2>&1",
174 /* Using popen() instead of run_command() since debugfs does
175 * not return proper error code if command is not supported */
176 fp = popen(cmd, "r");
178 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
182 ret = fread(enabled_features, 1, sizeof(enabled_features) - 1, fp);
183 enabled_features[ret] = '\0';
186 if (strstr(enabled_features, feature))
191 /* Write the server config files */
192 int ldiskfs_write_ldd(struct mkfs_opts *mop)
194 char mntpt[] = "/tmp/mntXXXXXX";
201 /* Mount this device temporarily in order to write these files */
202 if (!mkdtemp(mntpt)) {
203 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
204 progname, mntpt, strerror(errno));
209 * Append file context to mount options if SE Linux is enabled
212 if (is_selinux_enabled() > 0)
213 append_context_for_mount(mntpt, mop);
216 dev = mop->mo_device;
217 if (mop->mo_flags & MO_IS_LOOP)
218 dev = mop->mo_loopdev;
220 /* Multiple mount protection enabled if failover node specified */
221 if (mop->mo_flags & MO_FAILOVER &&
222 !is_feature_enabled("mmp", dev)) {
223 if (is_e2fsprogs_feature_supp("-O mmp") == 0) {
224 char *command = filepnm;
226 snprintf(command, sizeof(filepnm),
227 TUNE2FS" -O mmp '%s' >/dev/null 2>&1", dev);
228 ret = run_command(command, sizeof(filepnm));
231 "%s: Unable to set 'mmp' on %s: %d\n",
234 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.hpdd.intel.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 int 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));
482 ret = fread(supp_features, 1, sizeof(supp_features) - 1, fp);
483 supp_features[ret] = '\0';
486 if (ret > 0 && strstr(supp_features,
487 strncmp(feature, "-O ", 3) ? feature : feature+3))
490 if ((fd = mkstemp(imgname)) < 0)
495 snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
496 MKE2FS, feature, imgname);
497 /* run_command() displays the output of mke2fs when it fails for
498 * some feature, so use system() directly */
507 * append_unique: append @key or @key=@val pair to @buf only if @key does not
509 * @buf: buffer to hold @key or @key=@val
510 * @prefix: prefix string before @key
512 * @val: value string if it's a @key=@val pair
514 static void append_unique(char *buf, char *prefix, char *key, char *val,
523 anchor = end = strstr(buf, key);
524 /* try to find exact match string in @buf */
525 while (end && *end != '\0' && *end != ',' && *end != ' ' && *end != '=')
528 if (anchor == NULL || strlen(key) != len ||
529 strncmp(anchor, key, len) != 0) {
531 strscat(buf, prefix, maxbuflen);
533 strscat(buf, key, maxbuflen);
535 strscat(buf, "=\"", maxbuflen);
536 strscat(buf, val, maxbuflen);
537 strscat(buf, "\"", maxbuflen);
542 static int enable_default_ext4_features(struct mkfs_opts *mop, char *anchor,
543 size_t maxbuflen, int user_spec)
545 int enable_64bit = 0;
547 /* Enable large block addresses if the LUN is over 2^32 blocks. */
548 if ((mop->mo_device_kb / (L_BLOCK_SIZE >> 10) > UINT32_MAX) &&
549 is_e2fsprogs_feature_supp("-O 64bit") == 0)
552 if (IS_OST(&mop->mo_ldd)) {
553 append_unique(anchor, user_spec ? "," : " -O ",
554 "extents", NULL, maxbuflen);
555 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
556 } else if (IS_MDT(&mop->mo_ldd)) {
557 append_unique(anchor, user_spec ? "," : " -O ",
558 "dirdata", NULL, maxbuflen);
559 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
561 append_unique(anchor, ",", "extents", NULL, maxbuflen);
563 append_unique(anchor, ",", "^extents", NULL, maxbuflen);
565 append_unique(anchor, user_spec ? "," : " -O ",
566 "uninit_bg", NULL, maxbuflen);
569 /* Multiple mount protection enabled only if failover node specified */
570 if (mop->mo_flags & MO_FAILOVER) {
571 if (is_e2fsprogs_feature_supp("-O mmp") == 0)
572 append_unique(anchor, ",", "mmp", NULL, maxbuflen);
574 disp_old_e2fsprogs_msg("mmp", 1);
577 /* Allow more than 65000 subdirectories */
578 if (is_e2fsprogs_feature_supp("-O dir_nlink") == 0)
579 append_unique(anchor, ",", "dir_nlink", NULL, maxbuflen);
581 /* The following options are only valid for ext4-based ldiskfs.
582 * If --backfstype=ext3 is specified, do not enable them. */
583 if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
586 /* Enable quota by default */
587 if (is_e2fsprogs_feature_supp("-O quota") == 0) {
588 append_unique(anchor, ",", "quota", NULL, maxbuflen);
591 fprintf(stderr, "\"-O quota\" must be supported by "
592 "e2fsprogs, please upgrade your e2fsprogs.\n");
596 /* Allow files larger than 2TB. Also needs LU-16, but not harmful. */
597 if (is_e2fsprogs_feature_supp("-O huge_file") == 0)
598 append_unique(anchor, ",", "huge_file", NULL, maxbuflen);
601 append_unique(anchor, ",", "64bit", NULL, maxbuflen);
603 /* Cluster inode/block bitmaps and inode table for more efficient IO.
604 * Align the flex groups on a 1MB boundary for better performance. */
605 /* This -O feature needs to go last, since it adds the "-G" option. */
606 if (is_e2fsprogs_feature_supp("-O flex_bg") == 0) {
609 append_unique(anchor, ",", "flex_bg", NULL, maxbuflen);
611 if (IS_OST(&mop->mo_ldd) &&
612 strstr(mop->mo_mkfsopts, "-G") == NULL) {
613 snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
614 (1 << 20) / L_BLOCK_SIZE);
615 strscat(anchor, tmp_buf, maxbuflen);
618 /* Don't add any more "-O" options here, see last comment above */
623 * moveopts_to_end: find the option string, move remaining strings to
624 * where option string starts, and append the option
626 * @start: where the option string starts before the move
627 * RETURN: where the option string starts after the move
629 static char *moveopts_to_end(char *start)
635 /* skip whitespace before options */
640 /* find end of option characters */
641 while (*end != ' ' && *end != '\0')
645 if (len >= sizeof(save))
646 len = sizeof(save) - 1;
649 strncpy(save, start, len);
652 /* move remaining options up front */
654 memmove(start, end, strlen(end));
655 *(start + strlen(end)) = '\0';
657 /* append the specified options */
658 if (*(start + strlen(start) - 1) != ' ')
660 idx = start + strlen(start);
666 /* Build fs according to type */
667 int ldiskfs_make_lustre(struct mkfs_opts *mop)
669 __u64 device_kb = mop->mo_device_kb, block_count = 0;
670 char mkfs_cmd[PATH_MAX];
674 int ret = 0, ext_opts = 0;
677 if (!(mop->mo_flags & MO_IS_LOOP)) {
678 mop->mo_device_kb = get_device_size(mop->mo_device);
680 if (mop->mo_device_kb == 0)
683 /* Compare to real size */
684 if (device_kb == 0 || device_kb > mop->mo_device_kb)
685 device_kb = mop->mo_device_kb;
687 mop->mo_device_kb = device_kb;
690 if (mop->mo_device_kb != 0) {
691 if (mop->mo_device_kb < 32384) {
692 fprintf(stderr, "%s: size of filesystem must be larger "
693 "than 32MB, but is set to %lldKB\n",
694 progname, (long long)mop->mo_device_kb);
697 block_count = mop->mo_device_kb / (L_BLOCK_SIZE >> 10);
698 /* If the LUN size is just over 2^32 blocks, limit the
699 * filesystem size to 2^32-1 blocks to avoid problems with
700 * ldiskfs/mkfs not handling this size. Bug 22906 */
701 if (block_count > 0xffffffffULL && block_count < 0x100002000ULL)
702 block_count = 0xffffffffULL;
705 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
706 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
707 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
710 /* Journal size in MB */
711 if (strstr(mop->mo_mkfsopts, "-J") == NULL &&
712 device_kb > 1024 * 1024) {
713 /* Choose our own default journal size */
714 long journal_mb = 0, max_mb;
716 /* cap journal size at 4GB for MDT,
717 * leave it at 400MB for OSTs. */
718 if (IS_MDT(&mop->mo_ldd))
720 else if (IS_OST(&mop->mo_ldd))
722 else /* Use mke2fs default size for MGS */
725 /* Use at most 4% of device for journal */
726 journal_mb = device_kb * 4 / (1024 * 100);
727 if (journal_mb > max_mb)
731 sprintf(buf, " -J size=%ld", journal_mb);
732 strscat(mop->mo_mkfsopts, buf,
733 sizeof(mop->mo_mkfsopts));
738 * The inode size is constituted by following elements
739 * (assuming all files are in composite layout and has
742 * ldiskfs inode size: 156
743 * extended attributes size, including:
744 * ext4_xattr_header: 32
745 * LOV EA size: 32(lov_comp_md_v1) +
746 * 3 * 40(lov_comp_md_entry_v1) +
747 * 3 * 32(lov_mds_md) +
748 * stripes * 24(lov_ost_data) +
749 * 16(xattr_entry) + 3(lov)
750 * LMA EA size: 24(lustre_mdt_attrs) +
751 * 16(xattr_entry) + 3(lma)
752 * link EA size: 24(link_ea_header) + 18(link_ea_entry) +
753 * (filename) + 16(xattr_entry) + 4(link)
754 * and some margin for 4-byte alignment, ACLs and other EAs.
756 * If we say the average filename length is about 32 bytes,
757 * the calculation looks like:
758 * 156 + 32 + (32+3*(40 + 32)+24*N+19) + (24+19) +
759 * (24+18+~32+20) + other <= 512*2^m, {m=0,1,2,3}
761 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
762 if (IS_MDT(&mop->mo_ldd)) {
763 if (mop->mo_stripe_count > 59)
764 inode_size = 512; /* bz 7241 */
765 /* see also "-i" below for EA blocks */
766 else if (mop->mo_stripe_count > 16)
770 } else if (IS_OST(&mop->mo_ldd)) {
771 /* We store MDS FID and necessary composite
772 * layout information in the OST object EA. */
776 if (inode_size > 0) {
777 sprintf(buf, " -I %ld", inode_size);
778 strscat(mop->mo_mkfsopts, buf,
779 sizeof(mop->mo_mkfsopts));
783 /* Bytes_per_inode: disk size / num inodes */
784 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
785 strstr(mop->mo_mkfsopts, "-N") == NULL) {
786 long bytes_per_inode = 0;
788 /* Allocate more inodes on MDT devices. There is
789 * no data stored on the MDT, and very little extra
790 * metadata beyond the inode. It could go down as
791 * low as 1024 bytes, but this is conservative.
792 * Account for external EA blocks for wide striping. */
793 if (IS_MDT(&mop->mo_ldd)) {
794 bytes_per_inode = inode_size + 1536;
796 if (mop->mo_stripe_count > 59) {
797 int extra = mop->mo_stripe_count * 24;
798 extra = ((extra - 1) | 4095) + 1;
799 bytes_per_inode += extra;
803 /* Allocate fewer inodes on large OST devices. Most
804 * filesystems can be much more aggressive than even
805 * this, but it is impossible to know in advance. */
806 if (IS_OST(&mop->mo_ldd)) {
807 /* OST > 16TB assume average file size 1MB */
808 if (device_kb > (16ULL << 30))
809 bytes_per_inode = 1024 * 1024;
810 /* OST > 4TB assume average file size 512kB */
811 else if (device_kb > (4ULL << 30))
812 bytes_per_inode = 512 * 1024;
813 /* OST > 1TB assume average file size 256kB */
814 else if (device_kb > (1ULL << 30))
815 bytes_per_inode = 256 * 1024;
816 /* OST > 10GB assume average file size 64kB,
817 * plus a bit so that inodes will fit into a
818 * 256x flex_bg without overflowing */
819 else if (device_kb > (10ULL << 20))
820 bytes_per_inode = 69905;
823 if (bytes_per_inode > 0) {
824 sprintf(buf, " -i %ld", bytes_per_inode);
825 strscat(mop->mo_mkfsopts, buf,
826 sizeof(mop->mo_mkfsopts));
827 mop->mo_inode_size = bytes_per_inode;
832 strscat(mop->mo_mkfsopts, " -q",
833 sizeof(mop->mo_mkfsopts));
836 /* start handle -O mkfs options */
837 if ((start = strstr(mop->mo_mkfsopts, "-O")) != NULL) {
838 if (strstr(start + 2, "-O") != NULL) {
840 "%s: don't specify multiple -O options\n",
844 start = moveopts_to_end(start);
845 maxbuflen = sizeof(mop->mo_mkfsopts) -
846 (start - mop->mo_mkfsopts) - strlen(start);
847 ret = enable_default_ext4_features(mop, start, maxbuflen, 1);
849 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts),
850 maxbuflen = sizeof(mop->mo_mkfsopts) -
851 strlen(mop->mo_mkfsopts);
852 ret = enable_default_ext4_features(mop, start, maxbuflen, 0);
856 /* end handle -O mkfs options */
858 /* start handle -E mkfs options */
859 if ((start = strstr(mop->mo_mkfsopts, "-E")) != NULL) {
860 if (strstr(start + 2, "-E") != NULL) {
862 "%s: don't specify multiple -E options\n",
866 start = moveopts_to_end(start);
867 maxbuflen = sizeof(mop->mo_mkfsopts) -
868 (start - mop->mo_mkfsopts) - strlen(start);
871 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
872 maxbuflen = sizeof(mop->mo_mkfsopts) -
873 strlen(mop->mo_mkfsopts);
876 /* In order to align the filesystem metadata on 1MB boundaries,
877 * give a resize value that will reserve a power-of-two group
878 * descriptor blocks, but leave one block for the superblock.
879 * Only useful for filesystems with < 2^32 blocks due to resize
881 if (strstr(mop->mo_mkfsopts, "meta_bg") == NULL &&
882 IS_OST(&mop->mo_ldd) && mop->mo_device_kb > 100 * 1024 &&
883 mop->mo_device_kb * 1024 / L_BLOCK_SIZE <= 0xffffffffULL) {
884 unsigned group_blocks = L_BLOCK_SIZE * 8;
885 unsigned desc_per_block = L_BLOCK_SIZE / 32;
886 unsigned resize_blks;
888 resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
889 snprintf(buf, sizeof(buf), "%u", resize_blks);
890 append_unique(start, ext_opts ? "," : " -E ",
891 "resize", buf, maxbuflen);
895 /* Avoid zeroing out the full journal - speeds up mkfs */
896 if (is_e2fsprogs_feature_supp("-E lazy_journal_init") == 0)
897 append_unique(start, ext_opts ? "," : " -E ",
898 "lazy_journal_init", NULL, maxbuflen);
899 /* end handle -E mkfs options */
901 /* Allow reformat of full devices (as opposed to
902 partitions.) We already checked for mounted dev. */
903 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
905 snprintf(mkfs_cmd, sizeof(mkfs_cmd),
906 "%s -j -b %d -L %s ", MKE2FS, L_BLOCK_SIZE,
907 mop->mo_ldd.ldd_svname);
909 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
910 progname, mop->mo_ldd.ldd_mount_type,
911 MT_STR(&mop->mo_ldd));
915 /* For loop device format the dev, not the filename */
916 dev = mop->mo_device;
917 if (mop->mo_flags & MO_IS_LOOP)
918 dev = mop->mo_loopdev;
920 vprint("formatting backing filesystem %s on %s\n",
921 MT_STR(&mop->mo_ldd), dev);
922 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
923 vprint("\t4k blocks %ju\n", (uintmax_t)block_count);
924 vprint("\toptions %s\n", mop->mo_mkfsopts);
926 /* mkfs_cmd's trailing space is important! */
927 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
928 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
929 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
930 if (block_count != 0) {
931 snprintf(buf, sizeof(buf), " %ju",
932 (uintmax_t)block_count);
933 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
936 vprint("mkfs_cmd = %s\n", mkfs_cmd);
937 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
940 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
945 int ldiskfs_prepare_lustre(struct mkfs_opts *mop,
946 char *wanted_mountopts, size_t len)
948 struct lustre_disk_data *ldd = &mop->mo_ldd;
951 /* Set MO_IS_LOOP to indicate a loopback device is needed */
952 ret = is_block(mop->mo_device);
955 } else if (ret == 0) {
956 mop->mo_flags |= MO_IS_LOOP;
959 if (IS_MDT(ldd) || IS_MGS(ldd))
960 strscat(wanted_mountopts, ",user_xattr", len);
965 int ldiskfs_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
967 if (strstr(mountopts, "errors=") == NULL)
968 strscat(mountopts, ",errors=remount-ro", len);
973 static int read_file(const char *path, char *buf, int size)
977 fd = fopen(path, "r");
981 if (fgets(buf, size, fd) == NULL) {
982 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
988 /* strip trailing newline */
990 if (buf[size - 1] == '\n')
991 buf[size - 1] = '\0';
996 static int write_file(const char *path, const char *buf)
1000 fd = open(path, O_WRONLY);
1004 rc = write(fd, buf, strlen(buf));
1007 return rc < 0 ? errno : 0;
1010 static int tune_md_stripe_cache_size(const char *sys_path,
1011 struct mount_opts *mop)
1013 char path[PATH_MAX];
1014 unsigned long old_stripe_cache_size;
1015 unsigned long new_stripe_cache_size;
1016 char buf[3 * sizeof(old_stripe_cache_size) + 2];
1019 if (mop->mo_md_stripe_cache_size <= 0)
1022 new_stripe_cache_size = mop->mo_md_stripe_cache_size;
1024 snprintf(path, sizeof(path), "%s/%s", sys_path, STRIPE_CACHE_SIZE);
1025 rc = read_file(path, buf, sizeof(buf));
1028 fprintf(stderr, "warning: cannot read '%s': %s\n",
1029 path, strerror(errno));
1033 old_stripe_cache_size = strtoul(buf, NULL, 0);
1034 if (old_stripe_cache_size == 0 || old_stripe_cache_size == ULONG_MAX)
1037 if (new_stripe_cache_size <= old_stripe_cache_size)
1040 snprintf(buf, sizeof(buf), "%lu", new_stripe_cache_size);
1041 rc = write_file(path, buf);
1044 fprintf(stderr, "warning: cannot write '%s': %s\n",
1045 path, strerror(errno));
1052 static int tune_max_sectors_kb(const char *sys_path, struct mount_opts *mop)
1054 char path[PATH_MAX];
1055 unsigned long max_hw_sectors_kb;
1056 unsigned long old_max_sectors_kb;
1057 unsigned long new_max_sectors_kb;
1058 char buf[3 * sizeof(old_max_sectors_kb) + 2];
1061 if (mop->mo_max_sectors_kb >= 0) {
1062 new_max_sectors_kb = mop->mo_max_sectors_kb;
1063 goto have_new_max_sectors_kb;
1066 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_HW_SECTORS_KB_PATH);
1067 rc = read_file(path, buf, sizeof(buf));
1069 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
1070 * error for some devices. */
1074 max_hw_sectors_kb = strtoul(buf, NULL, 0);
1075 if (max_hw_sectors_kb == 0 || max_hw_sectors_kb == ULLONG_MAX) {
1076 /* No digits at all or something weird. */
1080 new_max_sectors_kb = max_hw_sectors_kb;
1082 /* Don't increase IO request size limit past 16MB. It is
1083 * about PTLRPC_MAX_BRW_SIZE, but that isn't in a public
1084 * header. Note that even though the block layer allows
1085 * larger values, setting max_sectors_kb = 32768 causes
1086 * crashes (LU-6974). */
1087 if (new_max_sectors_kb > 16 * 1024)
1088 new_max_sectors_kb = 16 * 1024;
1090 have_new_max_sectors_kb:
1091 snprintf(path, sizeof(path), "%s/%s", sys_path, MAX_SECTORS_KB_PATH);
1092 rc = read_file(path, buf, sizeof(buf));
1094 /* No MAX_SECTORS_KB_PATH isn't necessary an error for
1099 old_max_sectors_kb = strtoul(buf, NULL, 0);
1100 if (old_max_sectors_kb == 0 || old_max_sectors_kb == ULLONG_MAX) {
1101 /* No digits at all or something weird. */
1105 if (new_max_sectors_kb <= old_max_sectors_kb)
1108 snprintf(buf, sizeof(buf), "%lu", new_max_sectors_kb);
1109 rc = write_file(path, buf);
1112 fprintf(stderr, "warning: cannot write '%s': %s\n",
1113 path, strerror(errno));
1117 fprintf(stderr, "%s: increased '%s' from %lu to %lu\n",
1118 progname, path, old_max_sectors_kb, new_max_sectors_kb);
1123 static int tune_block_dev_scheduler(const char *sys_path, const char *new_sched)
1125 char path[PATH_MAX];
1131 /* Before setting the scheduler, we need to check to see if
1132 * it's already set to "noop". If it is then we don't want to
1133 * override that setting. If it's set to anything other than
1134 * "noop" then set the scheduler to what has been passed
1137 snprintf(path, sizeof(path), "%s/%s", sys_path, SCHEDULER_PATH);
1138 rc = read_file(path, buf, sizeof(buf));
1141 fprintf(stderr, "%s: cannot read '%s': %s\n",
1142 progname, path, strerror(errno));
1147 /* The expected format of buf: noop anticipatory deadline [cfq] */
1148 s = strchr(buf, '[');
1149 e = strchr(buf, ']');
1151 /* If the format is not what we expect then be safe and error out. */
1152 if (s == NULL || e == NULL || !(s < e)) {
1155 "%s: cannot parse scheduler options for '%s'\n",
1164 if (strcmp(old_sched, "noop") == 0 ||
1165 strcmp(old_sched, new_sched) == 0)
1168 rc = write_file(path, new_sched);
1172 "%s: cannot set scheduler on '%s': %s\n",
1173 progname, path, strerror(errno));
1177 fprintf(stderr, "%s: changed scheduler of '%s' from %s to %s\n",
1178 progname, path, old_sched, new_sched);
1183 static int tune_block_dev(const char *src, struct mount_opts *mop);
1185 static int tune_block_dev_slaves(const char *sys_path, struct mount_opts *mop)
1187 char slaves_path[PATH_MAX];
1192 snprintf(slaves_path, sizeof(slaves_path), "%s/slaves", sys_path);
1193 slaves_dir = opendir(slaves_path);
1194 if (slaves_dir == NULL) {
1195 if (errno == ENOENT)
1201 while ((d = readdir(slaves_dir)) != NULL) {
1202 char path[PATH_MAX];
1205 if (d->d_type != DT_LNK)
1208 snprintf(path, sizeof(path), "%s/%s", slaves_path, d->d_name);
1209 rc2 = tune_block_dev(path, mop);
1214 closedir(slaves_dir);
1219 /* This is to tune the kernel for good SCSI performance.
1220 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
1221 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
1222 static int tune_block_dev(const char *src, struct mount_opts *mop)
1225 char sys_path[PATH_MAX];
1226 char partition_path[PATH_MAX];
1227 char *real_sys_path = NULL;
1233 rc = stat(src, &st);
1236 fprintf(stderr, "warning: cannot stat '%s': %s\n",
1237 src, strerror(errno));
1241 if (!S_ISBLK(st.st_mode))
1244 if (major(st.st_rdev) == LOOP_MAJOR)
1247 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u",
1248 major(st.st_rdev), minor(st.st_rdev));
1250 snprintf(partition_path, sizeof(partition_path), "%s/partition",
1253 rc = access(partition_path, F_OK);
1255 if (errno == ENOENT)
1256 goto have_whole_dev;
1260 "warning: cannot access '%s': %s\n",
1261 partition_path, strerror(errno));
1266 snprintf(sys_path, sizeof(sys_path), "/sys/dev/block/%u:%u/..",
1267 major(st.st_rdev), minor(st.st_rdev));
1270 /* Since we recurse on slave devices we resolve the sys_path to
1271 * avoid path buffer overflows. */
1272 real_sys_path = realpath(sys_path, NULL);
1273 if (real_sys_path == NULL) {
1276 "warning: cannot resolve '%s': %s\n",
1277 sys_path, strerror(errno));
1282 if (major(st.st_rdev) == MD_MAJOR) {
1283 rc = tune_md_stripe_cache_size(real_sys_path, mop);
1285 /* Ignore errors from tune_max_sectors_kb() and
1286 * tune_scheduler(). The worst that will happen is a block
1287 * device with an "incorrect" scheduler. */
1288 tune_max_sectors_kb(real_sys_path, mop);
1289 tune_block_dev_scheduler(real_sys_path, DEFAULT_SCHEDULER);
1291 /* If device is multipath device then tune its slave
1293 rc = tune_block_dev_slaves(real_sys_path, mop);
1297 free(real_sys_path);
1302 int ldiskfs_tune_lustre(char *dev, struct mount_opts *mop)
1304 return tune_block_dev(dev, mop);
1307 int ldiskfs_label_lustre(struct mount_opts *mop)
1309 char label_cmd[PATH_MAX];
1312 snprintf(label_cmd, sizeof(label_cmd),
1313 TUNE2FS" -f -L '%s' '%s' >/dev/null 2>&1",
1314 mop->mo_ldd.ldd_svname, mop->mo_source);
1315 rc = run_command(label_cmd, sizeof(label_cmd));
1320 int ldiskfs_rename_fsname(struct mkfs_opts *mop, const char *oldname)
1322 struct mount_opts opts;
1323 struct lustre_disk_data *ldd = &mop->mo_ldd;
1324 char mntpt[] = "/tmp/mntXXXXXX";
1328 /* Change the filesystem label. */
1330 opts.mo_source = mop->mo_device;
1331 ret = ldiskfs_label_lustre(&opts);
1335 fprintf(stderr, "Can't change filesystem label: %s\n",
1340 /* Mount this device temporarily in order to write these files */
1341 if (mkdtemp(mntpt) == NULL) {
1346 fprintf(stderr, "Can't create temp mount point %s: %s\n",
1347 mntpt, strerror(ret));
1353 * Append file context to mount options if SE Linux is enabled
1355 if (is_selinux_enabled() > 0)
1356 append_context_for_mount(mntpt, mop);
1359 if (mop->mo_flags & MO_IS_LOOP)
1360 dev = mop->mo_loopdev;
1362 dev = mop->mo_device;
1363 ret = mount(dev, mntpt, MT_STR(ldd), 0, ldd->ldd_mount_opts);
1367 fprintf(stderr, "Unable to mount %s: %s\n",
1368 dev, strerror(ret));
1370 fprintf(stderr, "Is the %s module available?\n",
1375 ret = lustre_rename_fsname(mop, mntpt, oldname);
1383 /* Enable quota accounting */
1384 int ldiskfs_enable_quota(struct mkfs_opts *mop)
1388 int cmdsz = sizeof(cmd), ret;
1390 if (is_e2fsprogs_feature_supp("-O quota") != 0) {
1391 fprintf(stderr, "%s: \"-O quota\" is is not supported by "
1392 "current e2fsprogs\n", progname);
1396 dev = mop->mo_device;
1397 if (mop->mo_flags & MO_IS_LOOP)
1398 dev = mop->mo_loopdev;
1400 /* Quota feature is already enabled? */
1401 if (is_feature_enabled("quota", dev)) {
1402 vprint("Quota feature is already enabled.\n");
1406 /* Turn on quota feature by "tune2fs -O quota" */
1407 snprintf(cmd, cmdsz, "%s -O quota %s", TUNE2FS, dev);
1408 ret = run_command(cmd, cmdsz);
1410 fprintf(stderr, "command:%s (%d)", cmd, ret);
1415 int ldiskfs_init(void)
1417 /* Required because full path to DEBUGFS is not specified */
1418 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 0);
1423 void ldiskfs_fini(void)
1429 struct module_backfs_ops ldiskfs_ops = {
1430 .init = ldiskfs_init,
1431 .fini = ldiskfs_fini,
1432 .read_ldd = ldiskfs_read_ldd,
1433 .write_ldd = ldiskfs_write_ldd,
1434 .erase_ldd = ldiskfs_erase_ldd,
1435 .print_ldd_params = ldiskfs_print_ldd_params,
1436 .is_lustre = ldiskfs_is_lustre,
1437 .make_lustre = ldiskfs_make_lustre,
1438 .prepare_lustre = ldiskfs_prepare_lustre,
1439 .fix_mountopts = ldiskfs_fix_mountopts,
1440 .tune_lustre = ldiskfs_tune_lustre,
1441 .label_lustre = ldiskfs_label_lustre,
1442 .enable_quota = ldiskfs_enable_quota,
1443 .rename_fsname = ldiskfs_rename_fsname,
1445 #endif /* PLUGIN_DIR */