Whamcloud - gitweb
LU-8998 utils: enlarge default inode size
[fs/lustre-release.git] / lustre / utils / mount_utils_ldiskfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/utils/mount_utils_ldiskfs.c
33  *
34  * Author: Nathan Rutman <nathan@clusterfs.com>
35 */
36
37 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
38
39 #if HAVE_CONFIG_H
40 #  include "config.h"
41 #endif /* HAVE_CONFIG_H */
42
43 #ifndef _GNU_SOURCE
44 #define _GNU_SOURCE
45 #endif
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <inttypes.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <mntent.h>
54 #include <glob.h>
55
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/mount.h>
59 #include <sys/utsname.h>
60
61 #include <string.h>
62 #include <getopt.h>
63 #include <limits.h>
64 #include <ctype.h>
65
66 #ifndef BLKGETSIZE64
67 #include <linux/fs.h> /* for BLKGETSIZE64 */
68 #endif
69 #include <linux/types.h>
70 #include <linux/version.h>
71 #include <lustre_param.h>
72 #include <lnet/lnetctl.h>
73 #include <lustre_ver.h>
74
75 #ifdef HAVE_SELINUX
76 #include <selinux/selinux.h>
77 #endif
78
79 #include "mount_utils.h"
80
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"
85
86 #define DEFAULT_SCHEDULER       "deadline"
87
88 extern char *progname;
89
90 #define L_BLOCK_SIZE 4096
91 /* keep it less than LL_FID_NAMELEN */
92 #define DUMMY_FILE_NAME_LEN             25
93 #define EXT3_DIRENT_SIZE                DUMMY_FILE_NAME_LEN
94
95 static void append_unique(char *buf, char *prefix, char *key, char *val,
96                           size_t maxbuflen);
97
98 /*
99  * Concatenate context of the temporary mount point if selinux is enabled
100  */
101 #ifdef HAVE_SELINUX
102 static void append_context_for_mount(char *mntpt, struct mkfs_opts *mop)
103 {
104         security_context_t fcontext;
105
106         if (getfilecon(mntpt, &fcontext) < 0) {
107                 /* Continuing with default behaviour */
108                 fprintf(stderr, "%s: Get file context failed : %s\n",
109                         progname, strerror(errno));
110                 return;
111         }
112
113         if (fcontext != NULL) {
114                 append_unique(mop->mo_ldd.ldd_mount_opts,
115                               ",", "context", fcontext,
116                               sizeof(mop->mo_ldd.ldd_mount_opts));
117                 freecon(fcontext);
118         }
119 }
120 #endif
121
122 /* return canonicalized absolute pathname, even if the target file does not
123  * exist, unlike realpath */
124 static char *absolute_path(char *devname)
125 {
126         char  buf[PATH_MAX + 1] = "";
127         char *path;
128         char *ptr;
129         int len;
130
131         path = malloc(sizeof(buf));
132         if (path == NULL)
133                 return NULL;
134
135         if (devname[0] != '/') {
136                 if (getcwd(buf, sizeof(buf) - 1) == NULL) {
137                         free(path);
138                         return NULL;
139                 }
140                 len = snprintf(path, sizeof(buf), "%s/%s", buf, devname);
141                 if (len >= sizeof(buf)) {
142                         free(path);
143                         return NULL;
144                 }
145         } else {
146                 len = snprintf(path, sizeof(buf), "%s", devname);
147                 if (len >= sizeof(buf)) {
148                         free(path);
149                         return NULL;
150                 }
151         }
152
153         /* truncate filename before calling realpath */
154         ptr = strrchr(path, '/');
155         if (ptr == NULL) {
156                 free(path);
157                 return NULL;
158         }
159         *ptr = '\0';
160         if (buf != realpath(path, buf)) {
161                 free(path);
162                 return NULL;
163         }
164         /* add the filename back */
165         len = snprintf(path, PATH_MAX, "%s/%s", buf, ptr+1);
166         if (len >= PATH_MAX) {
167                 free(path);
168                 return NULL;
169         }
170         return path;
171 }
172
173 /* Determine if a device is a block device (as opposed to a file) */
174 static int is_block(char *devname)
175 {
176         struct stat st;
177         int     ret = 0;
178         char    *devpath;
179
180         devpath = absolute_path(devname);
181         if (devpath == NULL) {
182                 fprintf(stderr, "%s: failed to resolve path to %s\n",
183                         progname, devname);
184                 return -1;
185         }
186
187         ret = access(devname, F_OK);
188         if (ret != 0) {
189                 if (strncmp(devpath, "/dev/", 5) == 0) {
190                         /* nobody sane wants to create a loopback file under
191                          * /dev. Let's just report the device doesn't exist */
192                         fprintf(stderr, "%s: %s apparently does not exist\n",
193                                 progname, devpath);
194                         ret = -1;
195                         goto out;
196                 }
197                 ret = 0;
198                 goto out;
199         }
200         ret = stat(devpath, &st);
201         if (ret != 0) {
202                 fprintf(stderr, "%s: cannot stat %s\n", progname, devpath);
203                 goto out;
204         }
205         ret = S_ISBLK(st.st_mode);
206 out:
207         free(devpath);
208         return ret;
209 }
210
211 static int is_feature_enabled(const char *feature, const char *devpath)
212 {
213         char cmd[PATH_MAX];
214         FILE *fp;
215         char enabled_features[4096] = "";
216         int ret = 1;
217
218         snprintf(cmd, sizeof(cmd), "%s -c -R features %s 2>&1",
219                  DEBUGFS, devpath);
220
221         /* Using popen() instead of run_command() since debugfs does
222          * not return proper error code if command is not supported */
223         fp = popen(cmd, "r");
224         if (!fp) {
225                 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
226                 return 0;
227         }
228
229         ret = fread(enabled_features, 1, sizeof(enabled_features) - 1, fp);
230         enabled_features[ret] = '\0';
231         pclose(fp);
232
233         if (strstr(enabled_features, feature))
234                 return 1;
235         return 0;
236 }
237
238 /* Write the server config files */
239 int ldiskfs_write_ldd(struct mkfs_opts *mop)
240 {
241         char mntpt[] = "/tmp/mntXXXXXX";
242         char filepnm[128];
243         char *dev;
244         FILE *filep;
245         int ret = 0;
246         size_t num;
247
248         /* Mount this device temporarily in order to write these files */
249         if (!mkdtemp(mntpt)) {
250                 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
251                         progname, mntpt, strerror(errno));
252                 return errno;
253         }
254
255         /*
256          * Append file context to mount options if SE Linux is enabled
257          */
258         #ifdef HAVE_SELINUX
259         if (is_selinux_enabled() > 0)
260                 append_context_for_mount(mntpt, mop);
261         #endif
262
263         dev = mop->mo_device;
264         if (mop->mo_flags & MO_IS_LOOP)
265                 dev = mop->mo_loopdev;
266
267         ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0,
268                 (mop->mo_mountopts == NULL) ?
269                 "errors=remount-ro" : mop->mo_mountopts);
270         if (ret) {
271                 fprintf(stderr, "%s: Unable to mount %s: %s\n",
272                         progname, dev, strerror(errno));
273                 ret = errno;
274                 if (errno == ENODEV) {
275                         fprintf(stderr, "Is the %s module available?\n",
276                                 MT_STR(&mop->mo_ldd));
277                 }
278                 goto out_rmdir;
279         }
280
281         /* Set up initial directories */
282         sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
283         ret = mkdir(filepnm, 0777);
284         if ((ret != 0) && (errno != EEXIST)) {
285                 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
286                         progname, filepnm, strerror(errno));
287                 goto out_umnt;
288         } else if (errno == EEXIST) {
289                 ret = 0;
290         }
291
292         /* Save the persistent mount data into a file. Lustre must pre-read
293            this file to get the real mount options. */
294         vprint("Writing %s\n", MOUNT_DATA_FILE);
295         sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
296         filep = fopen(filepnm, "w");
297         if (!filep) {
298                 fprintf(stderr, "%s: Unable to create %s file: %s\n",
299                         progname, filepnm, strerror(errno));
300                 goto out_umnt;
301         }
302         num = fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
303         if (num < 1 && ferror(filep)) {
304                 fprintf(stderr, "%s: Unable to write to file (%s): %s\n",
305                         progname, filepnm, strerror(errno));
306                 fclose(filep);
307                 goto out_umnt;
308         }
309         fsync(filep->_fileno);
310         fclose(filep);
311
312 out_umnt:
313         umount(mntpt);
314 out_rmdir:
315         rmdir(mntpt);
316         return ret;
317 }
318
319 static int readcmd(char *cmd, char *buf, int len)
320 {
321         FILE *fp;
322         int red;
323
324         fp = popen(cmd, "r");
325         if (!fp)
326                 return errno;
327
328         red = fread(buf, 1, len, fp);
329         pclose(fp);
330
331         /* strip trailing newline */
332         if (buf[red - 1] == '\n')
333                 buf[red - 1] = '\0';
334
335         return (red == 0) ? -ENOENT : 0;
336 }
337
338 int ldiskfs_read_ldd(char *dev, struct lustre_disk_data *mo_ldd)
339 {
340         char tmpdir[] = "/tmp/dirXXXXXX";
341         char cmd[PATH_MAX];
342         char filepnm[128];
343         FILE *filep;
344         int ret = 0;
345         int cmdsz = sizeof(cmd);
346
347         /* Make a temporary directory to hold Lustre data files. */
348         if (!mkdtemp(tmpdir)) {
349                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
350                         progname, tmpdir, strerror(errno));
351                 return errno;
352         }
353
354         /* TODO: it's worth observing the get_mountdata() function that is
355            in mount_utils.c for getting the mountdata out of the
356            filesystem */
357
358         /* Construct debugfs command line. */
359         snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' '%s'",
360                  DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
361
362         ret = run_command(cmd, cmdsz);
363         if (ret)
364                 verrprint("%s: Unable to dump %s dir (%d)\n",
365                           progname, MOUNT_CONFIGS_DIR, ret);
366
367         sprintf(filepnm, "%s/mountdata", tmpdir);
368         filep = fopen(filepnm, "r");
369         if (filep) {
370                 size_t num_read;
371                 vprint("Reading %s\n", MOUNT_DATA_FILE);
372                 num_read = fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
373                 if (num_read < 1 && ferror(filep)) {
374                         fprintf(stderr, "%s: Unable to read from file %s: %s\n",
375                                 progname, filepnm, strerror(errno));
376                 }
377                 fclose(filep);
378         }
379
380         snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
381         run_command(cmd, cmdsz);
382         if (ret)
383                 verrprint("Failed to read old data (%d)\n", ret);
384
385         /* As long as we at least have the label, we're good to go */
386         snprintf(cmd, sizeof(cmd), E2LABEL" %s", dev);
387         ret = readcmd(cmd, mo_ldd->ldd_svname, sizeof(mo_ldd->ldd_svname) - 1);
388
389         return ret;
390 }
391
392 int ldiskfs_erase_ldd(struct mkfs_opts *mop, char *param)
393 {
394         return 0;
395 }
396
397 void ldiskfs_print_ldd_params(struct mkfs_opts *mop)
398 {
399         printf("Parameters:%s\n", mop->mo_ldd.ldd_params);
400 }
401
402 /* Display the need for the latest e2fsprogs to be installed. make_backfs
403  * indicates if the caller is make_lustre_backfs() or not. */
404 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
405 {
406         static int msg_displayed;
407
408         if (msg_displayed) {
409                 fprintf(stderr, "WARNING: %s does not support %s "
410                         "feature.\n\n", E2FSPROGS, feature);
411                 return;
412         }
413
414         msg_displayed++;
415
416         fprintf(stderr, "WARNING: The %s package currently installed on "
417                 "your system does not support \"%s\" feature.\n",
418                 E2FSPROGS, feature);
419 #if !(HAVE_LDISKFSPROGS)
420         fprintf(stderr, "Please install the latest version of e2fsprogs from\n"
421                 "https://downloads.hpdd.intel.com/public/e2fsprogs/latest/\n"
422                 "to enable this feature.\n");
423 #endif
424         if (make_backfs)
425                 fprintf(stderr, "Feature will not be enabled until %s"
426                         "is updated and '%s -O %s %%{device}' "
427                         "is run.\n\n", E2FSPROGS, TUNE2FS, feature);
428 }
429
430 /* Check whether the file exists in the device */
431 static int file_in_dev(char *file_name, char *dev_name)
432 {
433         FILE *fp;
434         char debugfs_cmd[256];
435         unsigned int inode_num;
436         int i;
437
438         /* Construct debugfs command line. */
439         snprintf(debugfs_cmd, sizeof(debugfs_cmd),
440                  "%s -c -R 'stat %s' '%s' 2>&1 | egrep '(Inode|unsupported)'",
441                  DEBUGFS, file_name, dev_name);
442
443         fp = popen(debugfs_cmd, "r");
444         if (!fp) {
445                 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
446                 return 0;
447         }
448
449         if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
450                 pclose(fp);
451                 return 1;
452         }
453         i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd) - 1, fp);
454         if (i) {
455                 debugfs_cmd[i] = 0;
456                 fprintf(stderr, "%s", debugfs_cmd);
457                 if (strstr(debugfs_cmd, "unsupported feature")) {
458                         disp_old_e2fsprogs_msg("an unknown", 0);
459                 }
460                 pclose(fp);
461                 return -1;
462         }
463         pclose(fp);
464         return 0;
465 }
466
467 /* Check whether the device has already been used with lustre */
468 int ldiskfs_is_lustre(char *dev, unsigned *mount_type)
469 {
470         int ret;
471
472         ret = file_in_dev(MOUNT_DATA_FILE, dev);
473         if (ret) {
474                 /* in the -1 case, 'extents' means IS a lustre target */
475                 *mount_type = LDD_MT_LDISKFS;
476                 return 1;
477         }
478
479         ret = file_in_dev(LAST_RCVD, dev);
480         if (ret) {
481                 *mount_type = LDD_MT_LDISKFS;
482                 return 1;
483         }
484
485         return 0;
486 }
487
488 /* Check if a certain feature is supported by e2fsprogs.
489  * Firstly we try to use "debugfs supported_features" command to check if
490  * the feature is supported. If this fails we try to set this feature with
491  * mke2fs to check for its support. */
492 static int is_e2fsprogs_feature_supp(const char *feature)
493 {
494         static char supp_features[4096] = "";
495         FILE *fp;
496         char cmd[PATH_MAX];
497         char imgname[] = "/tmp/test-img-XXXXXX";
498         int fd = -1;
499         int ret = 1;
500
501         if (supp_features[0] == '\0') {
502                 snprintf(cmd, sizeof(cmd), "%s -c -R supported_features 2>&1",
503                          DEBUGFS);
504
505                 /* Using popen() instead of run_command() since debugfs does
506                  * not return proper error code if command is not supported */
507                 fp = popen(cmd, "r");
508                 if (!fp) {
509                         fprintf(stderr, "%s: %s\n", progname, strerror(errno));
510                         return 0;
511                 }
512                 ret = fread(supp_features, 1, sizeof(supp_features) - 1, fp);
513                 supp_features[ret] = '\0';
514                 pclose(fp);
515         }
516         if (ret > 0 && strstr(supp_features,
517                               strncmp(feature, "-O ", 3) ? feature : feature+3))
518                 return 0;
519
520         if ((fd = mkstemp(imgname)) < 0)
521                 return -1;
522         else
523                 close(fd);
524
525         snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
526                  MKE2FS, feature, imgname);
527         /* run_command() displays the output of mke2fs when it fails for
528          * some feature, so use system() directly */
529         ret = system(cmd);
530         unlink(imgname);
531
532         return ret;
533 }
534
535
536 /**
537  * append_unique: append @key or @key=@val pair to @buf only if @key does not
538  *                exists
539  *      @buf: buffer to hold @key or @key=@val
540  *      @prefix: prefix string before @key
541  *      @key: key string
542  *      @val: value string if it's a @key=@val pair
543  */
544 static void append_unique(char *buf, char *prefix, char *key, char *val,
545                           size_t maxbuflen)
546 {
547         char *anchor, *end;
548         int  len;
549
550         if (key == NULL)
551                 return;
552
553         anchor = end = strstr(buf, key);
554         /* try to find exact match string in @buf */
555         while (end && *end != '\0' && *end != ',' && *end != ' ' && *end != '=')
556                 ++end;
557         len = end - anchor;
558         if (anchor == NULL || strlen(key) != len ||
559                         strncmp(anchor, key, len) != 0) {
560                 if (prefix != NULL)
561                         strscat(buf, prefix, maxbuflen);
562
563                 strscat(buf, key, maxbuflen);
564                 if (val != NULL) {
565                         strscat(buf, "=\"", maxbuflen);
566                         strscat(buf, val, maxbuflen);
567                         strscat(buf, "\"", maxbuflen);
568                 }
569         }
570 }
571
572 static int enable_default_ext4_features(struct mkfs_opts *mop, char *anchor,
573                                         size_t maxbuflen, int user_spec)
574 {
575         if (IS_OST(&mop->mo_ldd)) {
576                 append_unique(anchor, user_spec ? "," : " -O ",
577                               "extents", NULL, maxbuflen);
578                 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
579         } else if (IS_MDT(&mop->mo_ldd)) {
580                 append_unique(anchor, user_spec ? "," : " -O ",
581                               "dirdata", NULL, maxbuflen);
582                 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
583                 append_unique(anchor, ",", "^extents", NULL, maxbuflen);
584         } else {
585                 append_unique(anchor, user_spec ? "," : " -O ",
586                               "uninit_bg", NULL, maxbuflen);
587         }
588
589         /* Multiple mount protection enabled only if failover node specified */
590         if (mop->mo_flags & MO_FAILOVER) {
591                 if (is_e2fsprogs_feature_supp("-O mmp") == 0)
592                         append_unique(anchor, ",", "mmp", NULL, maxbuflen);
593                 else
594                         disp_old_e2fsprogs_msg("mmp", 1);
595         }
596
597         /* Allow more than 65000 subdirectories */
598         if (is_e2fsprogs_feature_supp("-O dir_nlink") == 0)
599                 append_unique(anchor, ",", "dir_nlink", NULL, maxbuflen);
600
601         /* The following options are only valid for ext4-based ldiskfs.
602          * If --backfstype=ext3 is specified, do not enable them. */
603         if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
604                 return 0;
605
606         /* Enable quota by default */
607         if (is_e2fsprogs_feature_supp("-O quota") == 0) {
608                 append_unique(anchor, ",", "quota", NULL, maxbuflen);
609         } else {
610                 fatal();
611                 fprintf(stderr, "\"-O quota\" must be supported by "
612                         "e2fsprogs, please upgrade your e2fsprogs.\n");
613                 return EINVAL;
614         }
615
616         /* Allow files larger than 2TB.  Also needs LU-16, but not harmful. */
617         if (is_e2fsprogs_feature_supp("-O huge_file") == 0)
618                 append_unique(anchor, ",", "huge_file", NULL, maxbuflen);
619
620         /* Enable large block addresses if the LUN is over 2^32 blocks. */
621         if (mop->mo_device_kb / (L_BLOCK_SIZE >> 10) >= 0x100002000ULL &&
622             is_e2fsprogs_feature_supp("-O 64bit") == 0)
623                 append_unique(anchor, ",", "64bit", NULL, maxbuflen);
624
625         /* Cluster inode/block bitmaps and inode table for more efficient IO.
626          * Align the flex groups on a 1MB boundary for better performance. */
627         /* This -O feature needs to go last, since it adds the "-G" option. */
628         if (is_e2fsprogs_feature_supp("-O flex_bg") == 0) {
629                 char tmp_buf[64];
630
631                 append_unique(anchor, ",", "flex_bg", NULL, maxbuflen);
632
633                 if (IS_OST(&mop->mo_ldd) &&
634                     strstr(mop->mo_mkfsopts, "-G") == NULL) {
635                         snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
636                                  (1 << 20) / L_BLOCK_SIZE);
637                         strscat(anchor, tmp_buf, maxbuflen);
638                 }
639         }
640         /* Don't add any more "-O" options here, see last comment above */
641         return 0;
642 }
643
644 /**
645  * moveopts_to_end: find the option string, move remaining strings to
646  *                  where option string starts, and append the option
647  *                  string at the end
648  *      @start: where the option string starts before the move
649  *      RETURN: where the option string starts after the move
650  */
651 static char *moveopts_to_end(char *start)
652 {
653         size_t len;
654         char save[512];
655         char *end, *idx;
656
657         /* skip whitespace before options */
658         end = start + 2;
659         while (*end == ' ')
660                 ++end;
661
662         /* find end of option characters */
663         while (*end != ' ' && *end != '\0')
664                 ++end;
665
666         len = end - start;
667         if (len >= sizeof(save))
668                 len = sizeof(save) - 1;
669
670         /* save options */
671         strncpy(save, start, len);
672         save[len] = '\0';
673
674         /* move remaining options up front */
675         if (*end)
676                 memmove(start, end, strlen(end));
677         *(start + strlen(end)) = '\0';
678
679         /* append the specified options */
680         if (*(start + strlen(start) - 1) != ' ')
681                 strcat(start, " ");
682         idx = start + strlen(start);
683         strcat(start, save);
684
685         return idx;
686 }
687
688 /* Build fs according to type */
689 int ldiskfs_make_lustre(struct mkfs_opts *mop)
690 {
691         __u64 device_kb = mop->mo_device_kb, block_count = 0;
692         char mkfs_cmd[PATH_MAX];
693         char buf[64];
694         char *start;
695         char *dev;
696         int ret = 0, ext_opts = 0;
697         size_t maxbuflen;
698
699         if (!(mop->mo_flags & MO_IS_LOOP)) {
700                 mop->mo_device_kb = get_device_size(mop->mo_device);
701
702                 if (mop->mo_device_kb == 0)
703                         return ENODEV;
704
705                 /* Compare to real size */
706                 if (device_kb == 0 || device_kb > mop->mo_device_kb)
707                         device_kb = mop->mo_device_kb;
708                 else
709                         mop->mo_device_kb = device_kb;
710         }
711
712         if (mop->mo_device_kb != 0) {
713                 if (mop->mo_device_kb < 32384) {
714                         fprintf(stderr, "%s: size of filesystem must be larger "
715                                 "than 32MB, but is set to %lldKB\n",
716                                 progname, (long long)mop->mo_device_kb);
717                         return EINVAL;
718                 }
719                 block_count = mop->mo_device_kb / (L_BLOCK_SIZE >> 10);
720                 /* If the LUN size is just over 2^32 blocks, limit the
721                  * filesystem size to 2^32-1 blocks to avoid problems with
722                  * ldiskfs/mkfs not handling this size.  Bug 22906 */
723                 if (block_count > 0xffffffffULL && block_count < 0x100002000ULL)
724                         block_count = 0xffffffffULL;
725         }
726
727         if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
728             (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
729             (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
730                 long inode_size = 0;
731
732                 /* Journal size in MB */
733                 if (strstr(mop->mo_mkfsopts, "-J") == NULL &&
734                     device_kb > 1024 * 1024) {
735                         /* Choose our own default journal size */
736                         long journal_mb = 0, max_mb;
737
738                         /* cap journal size at 4GB for MDT,
739                          * leave it at 400MB for OSTs. */
740                         if (IS_MDT(&mop->mo_ldd))
741                                 max_mb = 4096;
742                         else if (IS_OST(&mop->mo_ldd))
743                                 max_mb = 400;
744                         else /* Use mke2fs default size for MGS */
745                                 max_mb = 0;
746
747                         /* Use at most 4% of device for journal */
748                         journal_mb = device_kb * 4 / (1024 * 100);
749                         if (journal_mb > max_mb)
750                                 journal_mb = max_mb;
751
752                         if (journal_mb) {
753                                 sprintf(buf, " -J size=%ld", journal_mb);
754                                 strscat(mop->mo_mkfsopts, buf,
755                                         sizeof(mop->mo_mkfsopts));
756                         }
757                 }
758
759                 /*
760                  * The inode size is constituted by following elements
761                  * (assuming all files are in composite layout and has
762                  * 3 components):
763                  *
764                  *   ldiskfs inode size: 156
765                  *   extended attributes size, including:
766                  *      ext4_xattr_header: 32
767                  *      LOV EA size: 32(lov_comp_md_v1) +
768                  *                   3 * 40(lov_comp_md_entry_v1) +
769                  *                   3 * 32(lov_mds_md) +
770                  *                   stripes * 24(lov_ost_data) +
771                  *                   16(xattr_entry) + 3(lov)
772                  *      LMA EA size: 24(lustre_mdt_attrs) +
773                  *                   16(xattr_entry) + 3(lma)
774                  *      link EA size: 24(link_ea_header) + 18(link_ea_entry) +
775                  *                    (filename) + 16(xattr_entry) + 4(link)
776                  *   and some margin for 4-byte alignment, ACLs and other EAs.
777                  *
778                  * If we say the average filename length is about 32 bytes,
779                  * the calculation looks like:
780                  * 156 + 32 + (32+3*(40 + 32)+24*N+19) + (24+19) +
781                  * (24+18+~32+20) + other <= 512*2^m, {m=0,1,2,3}
782                  */
783                 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
784                         if (IS_MDT(&mop->mo_ldd)) {
785                                 if (mop->mo_stripe_count > 59)
786                                         inode_size = 512; /* bz 7241 */
787                                 /* see also "-i" below for EA blocks */
788                                 else if (mop->mo_stripe_count > 16)
789                                         inode_size = 2048;
790                                 else
791                                         inode_size = 1024;
792                         } else if (IS_OST(&mop->mo_ldd)) {
793                                 /* We store MDS FID and necessary composite
794                                  * layout information in the OST object EA. */
795                                 inode_size = 512;
796                         }
797
798                         if (inode_size > 0) {
799                                 sprintf(buf, " -I %ld", inode_size);
800                                 strscat(mop->mo_mkfsopts, buf,
801                                         sizeof(mop->mo_mkfsopts));
802                         }
803                 }
804
805                 /* Bytes_per_inode: disk size / num inodes */
806                 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
807                     strstr(mop->mo_mkfsopts, "-N") == NULL) {
808                         long bytes_per_inode = 0;
809
810                         /* Allocate more inodes on MDT devices.  There is
811                          * no data stored on the MDT, and very little extra
812                          * metadata beyond the inode.  It could go down as
813                          * low as 1024 bytes, but this is conservative.
814                          * Account for external EA blocks for wide striping. */
815                         if (IS_MDT(&mop->mo_ldd)) {
816                                 bytes_per_inode = inode_size + 1536;
817
818                                 if (mop->mo_stripe_count > 59) {
819                                         int extra = mop->mo_stripe_count * 24;
820                                         extra = ((extra - 1) | 4095) + 1;
821                                         bytes_per_inode += extra;
822                                 }
823                         }
824
825                         /* Allocate fewer inodes on large OST devices.  Most
826                          * filesystems can be much more aggressive than even
827                          * this, but it is impossible to know in advance. */
828                         if (IS_OST(&mop->mo_ldd)) {
829                                 /* OST > 16TB assume average file size 1MB */
830                                 if (device_kb > (16ULL << 30))
831                                         bytes_per_inode = 1024 * 1024;
832                                 /* OST > 4TB assume average file size 512kB */
833                                 else if (device_kb > (4ULL << 30))
834                                         bytes_per_inode = 512 * 1024;
835                                 /* OST > 1TB assume average file size 256kB */
836                                 else if (device_kb > (1ULL << 30))
837                                         bytes_per_inode = 256 * 1024;
838                                 /* OST > 10GB assume average file size 64kB,
839                                  * plus a bit so that inodes will fit into a
840                                  * 256x flex_bg without overflowing */
841                                 else if (device_kb > (10ULL << 20))
842                                         bytes_per_inode = 69905;
843                         }
844
845                         if (bytes_per_inode > 0) {
846                                 sprintf(buf, " -i %ld", bytes_per_inode);
847                                 strscat(mop->mo_mkfsopts, buf,
848                                         sizeof(mop->mo_mkfsopts));
849                         }
850                 }
851
852                 if (verbose < 2) {
853                         strscat(mop->mo_mkfsopts, " -q",
854                                 sizeof(mop->mo_mkfsopts));
855                 }
856
857                 /* start handle -O mkfs options */
858                 if ((start = strstr(mop->mo_mkfsopts, "-O")) != NULL) {
859                         if (strstr(start + 2, "-O") != NULL) {
860                                 fprintf(stderr,
861                                         "%s: don't specify multiple -O options\n",
862                                         progname);
863                                 return EINVAL;
864                         }
865                         start = moveopts_to_end(start);
866                         maxbuflen = sizeof(mop->mo_mkfsopts) -
867                                 (start - mop->mo_mkfsopts) - strlen(start);
868                         ret = enable_default_ext4_features(mop, start, maxbuflen, 1);
869                 } else {
870                         start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts),
871                               maxbuflen = sizeof(mop->mo_mkfsopts) -
872                                       strlen(mop->mo_mkfsopts);
873                         ret = enable_default_ext4_features(mop, start, maxbuflen, 0);
874                 }
875                 if (ret)
876                         return ret;
877                 /* end handle -O mkfs options */
878
879                 /* start handle -E mkfs options */
880                 if ((start = strstr(mop->mo_mkfsopts, "-E")) != NULL) {
881                         if (strstr(start + 2, "-E") != NULL) {
882                                 fprintf(stderr,
883                                         "%s: don't specify multiple -E options\n",
884                                         progname);
885                                 return EINVAL;
886                         }
887                         start = moveopts_to_end(start);
888                         maxbuflen = sizeof(mop->mo_mkfsopts) -
889                                 (start - mop->mo_mkfsopts) - strlen(start);
890                         ext_opts = 1;
891                 } else {
892                         start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
893                         maxbuflen = sizeof(mop->mo_mkfsopts) -
894                                 strlen(mop->mo_mkfsopts);
895                 }
896
897                 /* In order to align the filesystem metadata on 1MB boundaries,
898                  * give a resize value that will reserve a power-of-two group
899                  * descriptor blocks, but leave one block for the superblock.
900                  * Only useful for filesystems with < 2^32 blocks due to resize
901                  * limitations. */
902                 if (strstr(mop->mo_mkfsopts, "meta_bg") == NULL &&
903                     IS_OST(&mop->mo_ldd) && mop->mo_device_kb > 100 * 1024 &&
904                     mop->mo_device_kb * 1024 / L_BLOCK_SIZE <= 0xffffffffULL) {
905                         unsigned group_blocks = L_BLOCK_SIZE * 8;
906                         unsigned desc_per_block = L_BLOCK_SIZE / 32;
907                         unsigned resize_blks;
908
909                         resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
910                         snprintf(buf, sizeof(buf), "%u", resize_blks);
911                         append_unique(start, ext_opts ? "," : " -E ",
912                                       "resize", buf, maxbuflen);
913                         ext_opts = 1;
914                 }
915
916                 /* Avoid zeroing out the full journal - speeds up mkfs */
917                 if (is_e2fsprogs_feature_supp("-E lazy_journal_init") == 0)
918                         append_unique(start, ext_opts ? "," : " -E ",
919                                       "lazy_journal_init", NULL, maxbuflen);
920                 /* end handle -E mkfs options */
921
922                 /* Allow reformat of full devices (as opposed to
923                    partitions.)  We already checked for mounted dev. */
924                 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
925
926                 snprintf(mkfs_cmd, sizeof(mkfs_cmd),
927                          "%s -j -b %d -L %s ", MKE2FS, L_BLOCK_SIZE,
928                          mop->mo_ldd.ldd_svname);
929         } else {
930                 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
931                         progname, mop->mo_ldd.ldd_mount_type,
932                         MT_STR(&mop->mo_ldd));
933                 return EINVAL;
934         }
935
936         /* For loop device format the dev, not the filename */
937         dev = mop->mo_device;
938         if (mop->mo_flags & MO_IS_LOOP)
939                 dev = mop->mo_loopdev;
940
941         vprint("formatting backing filesystem %s on %s\n",
942                MT_STR(&mop->mo_ldd), dev);
943         vprint("\ttarget name   %s\n", mop->mo_ldd.ldd_svname);
944         vprint("\t4k blocks     %ju\n", (uintmax_t)block_count);
945         vprint("\toptions       %s\n", mop->mo_mkfsopts);
946
947         /* mkfs_cmd's trailing space is important! */
948         strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
949         strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
950         strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
951         if (block_count != 0) {
952                 snprintf(buf, sizeof(buf), " %ju",
953                          (uintmax_t)block_count);
954                 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
955         }
956
957         vprint("mkfs_cmd = %s\n", mkfs_cmd);
958         ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
959         if (ret) {
960                 fatal();
961                 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
962         }
963         return ret;
964 }
965
966 int ldiskfs_prepare_lustre(struct mkfs_opts *mop,
967                            char *wanted_mountopts, size_t len)
968 {
969         struct lustre_disk_data *ldd = &mop->mo_ldd;
970         int ret;
971
972         /* Set MO_IS_LOOP to indicate a loopback device is needed */
973         ret = is_block(mop->mo_device);
974         if (ret < 0) {
975                 return errno;
976         } else if (ret == 0) {
977                 mop->mo_flags |= MO_IS_LOOP;
978         }
979
980         if (IS_MDT(ldd) || IS_MGS(ldd))
981                 strscat(wanted_mountopts, ",user_xattr", len);
982
983         return 0;
984 }
985
986 int ldiskfs_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
987 {
988         if (strstr(mountopts, "errors=") == NULL)
989                 strscat(mountopts, ",errors=remount-ro", len);
990
991         return 0;
992 }
993
994 static int read_file(const char *path, char *buf, int size)
995 {
996         FILE *fd;
997
998         fd = fopen(path, "r");
999         if (fd == NULL)
1000                 return errno;
1001
1002         if (fgets(buf, size, fd) == NULL) {
1003                 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
1004                 fclose(fd);
1005                 return 1;
1006         }
1007         fclose(fd);
1008
1009         /* strip trailing newline */
1010         size = strlen(buf);
1011         if (buf[size - 1] == '\n')
1012                 buf[size - 1] = '\0';
1013
1014         return 0;
1015 }
1016
1017 static int write_file(const char *path, const char *buf)
1018 {
1019         int fd, rc;
1020
1021         fd = open(path, O_WRONLY);
1022         if (fd < 0)
1023                 return errno;
1024
1025         rc = write(fd, buf, strlen(buf));
1026         close(fd);
1027
1028         return rc < 0 ? errno : 0;
1029 }
1030
1031 static int set_blockdev_scheduler(const char *path, const char *scheduler)
1032 {
1033         char buf[PATH_MAX], *s, *e, orig_sched[50];
1034         int rc;
1035
1036         /* Before setting the scheduler, we need to check to see if it's
1037          * already set to "noop". If it is, we don't want to override
1038          * that setting. If it's set to anything other than "noop", set
1039          * the scheduler to what has been passed in. */
1040
1041         rc = read_file(path, buf, sizeof(buf));
1042         if (rc) {
1043                 if (verbose)
1044                         fprintf(stderr, "%s: cannot open '%s': %s\n",
1045                                 progname, path, strerror(errno));
1046                 return rc;
1047         }
1048
1049         /* The expected format of buf: noop anticipatory deadline [cfq] */
1050         s = strchr(buf, '[');
1051         e = strchr(buf, ']');
1052
1053         /* If the format is not what we expect. Play it safe and error out. */
1054         if (s == NULL || e == NULL) {
1055                 if (verbose)
1056                         fprintf(stderr, "%s: cannot parse scheduler "
1057                                         "options for '%s'\n", progname, path);
1058                 return -EINVAL;
1059         }
1060
1061         snprintf(orig_sched, e - s, "%s", s + 1);
1062
1063         if (strcmp(orig_sched, "noop") == 0 ||
1064             strcmp(orig_sched, scheduler) == 0)
1065                 return 0;
1066
1067         rc = write_file(path, scheduler);
1068         if (rc) {
1069                 if (verbose)
1070                         fprintf(stderr, "%s: cannot set scheduler on "
1071                                         "'%s': %s\n", progname, path,
1072                                         strerror(errno));
1073                 return rc;
1074         } else {
1075                 fprintf(stderr, "%s: change scheduler of %s from %s to %s\n",
1076                         progname, path, orig_sched, scheduler);
1077         }
1078
1079         return rc;
1080 }
1081
1082 /* This is to tune the kernel for good SCSI performance.
1083  * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
1084  * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
1085 static int set_blockdev_tunables(char *source, struct mount_opts *mop)
1086 {
1087         glob_t glob_info = { 0 };
1088         struct stat stat_buf;
1089         char *chk_major, *chk_minor;
1090         char *savept = NULL, *dev;
1091         char *ret_path;
1092         char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1093         char real_path[PATH_MAX] = {'\0'};
1094         int i, rc = 0;
1095         int major, minor;
1096         char *slave = NULL;
1097
1098         if (!source)
1099                 return -EINVAL;
1100
1101         ret_path = realpath(source, real_path);
1102         if (ret_path == NULL) {
1103                 if (verbose)
1104                         fprintf(stderr, "warning: %s: cannot resolve: %s\n",
1105                                 source, strerror(errno));
1106                 return -EINVAL;
1107         }
1108
1109         if (strncmp(real_path, "/dev/loop", 9) == 0)
1110                 return 0;
1111
1112         if ((real_path[0] != '/') && (strpbrk(real_path, ",:") != NULL))
1113                 return 0;
1114
1115         snprintf(path, sizeof(path), "/sys/block%s", real_path + 4);
1116         if (access(path, X_OK) == 0)
1117                 goto set_params;
1118
1119         /* The name of the device say 'X' specified in /dev/X may not
1120          * match any entry under /sys/block/. In that case we need to
1121          * match the major/minor number to find the entry under
1122          * sys/block corresponding to /dev/X */
1123
1124         /* Don't chop tail digit on /dev/mapper/xxx, LU-478 */
1125         if (strncmp(real_path, "/dev/mapper", 11) != 0) {
1126                 dev = real_path + strlen(real_path);
1127                 while (--dev > real_path && isdigit(*dev))
1128                         *dev = 0;
1129
1130                 if (strncmp(real_path, "/dev/md_", 8) == 0)
1131                         *dev = 0;
1132         }
1133
1134         rc = stat(real_path, &stat_buf);
1135         if (rc) {
1136                 if (verbose)
1137                         fprintf(stderr, "warning: %s, device %s stat failed\n",
1138                                 strerror(errno), real_path);
1139                 return rc;
1140         }
1141
1142         major = major(stat_buf.st_rdev);
1143         minor = minor(stat_buf.st_rdev);
1144         rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
1145         if (rc) {
1146                 if (verbose)
1147                         fprintf(stderr, "warning: failed to read entries under "
1148                                 "/sys/block\n");
1149                 globfree(&glob_info);
1150                 return rc;
1151         }
1152
1153         for (i = 0; i < glob_info.gl_pathc; i++){
1154                 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
1155
1156                 rc = read_file(path, buf, sizeof(buf));
1157                 if (rc)
1158                         continue;
1159
1160                 if (buf[strlen(buf) - 1] == '\n')
1161                         buf[strlen(buf) - 1] = '\0';
1162
1163                 chk_major = strtok_r(buf, ":", &savept);
1164                 chk_minor = savept;
1165                 if (chk_major != NULL && major == atoi(chk_major) &&
1166                     chk_minor != NULL && minor == atoi(chk_minor))
1167                         break;
1168         }
1169
1170         if (i == glob_info.gl_pathc) {
1171                 if (verbose)
1172                         fprintf(stderr,"warning: device %s does not match any "
1173                                 "entry under /sys/block\n", real_path);
1174                 globfree(&glob_info);
1175                 return -EINVAL;
1176         }
1177
1178         /* Chop off "/dev" from path we found */
1179         path[strlen(glob_info.gl_pathv[i])] = '\0';
1180         globfree(&glob_info);
1181
1182 set_params:
1183         if (strncmp(real_path, "/dev/md", 7) == 0) {
1184                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
1185                          STRIPE_CACHE_SIZE);
1186
1187                 rc = read_file(real_path, buf, sizeof(buf));
1188                 if (rc) {
1189                         if (verbose)
1190                                 fprintf(stderr, "warning: opening %s: %s\n",
1191                                         real_path, strerror(errno));
1192                         return 0;
1193                 }
1194
1195                 if (atoi(buf) >= mop->mo_md_stripe_cache_size)
1196                         return 0;
1197
1198                 if (strlen(buf) - 1 > 0) {
1199                         snprintf(buf, sizeof(buf), "%d",
1200                                  mop->mo_md_stripe_cache_size);
1201                         rc = write_file(real_path, buf);
1202                         if (rc != 0 && verbose)
1203                                 fprintf(stderr, "warning: opening %s: %s\n",
1204                                         real_path, strerror(errno));
1205                 }
1206                 /* Return since raid and disk tunables are different */
1207                 return rc;
1208         }
1209
1210         if (mop->mo_max_sectors_kb >= 0) {
1211                 snprintf(buf, sizeof(buf), "%d", mop->mo_max_sectors_kb);
1212         } else {
1213                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
1214                          MAX_HW_SECTORS_KB_PATH);
1215                 rc = read_file(real_path, buf, sizeof(buf));
1216                 if (rc) {
1217                         if (verbose)
1218                                 fprintf(stderr, "warning: opening %s: %s\n",
1219                                         real_path, strerror(errno));
1220                         /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
1221                          * error for some device. */
1222                         goto subdevs;
1223                 }
1224         }
1225
1226         if (strlen(buf) - 1 > 0) {
1227                 char oldbuf[32] = "", *end = NULL;
1228                 unsigned long long oldval, newval;
1229
1230                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
1231                          MAX_SECTORS_KB_PATH);
1232                 rc = read_file(real_path, oldbuf, sizeof(oldbuf));
1233                 /* Only set new parameter if different from the old one. */
1234                 if (rc != 0 || strcmp(oldbuf, buf) == 0) {
1235                         /* No MAX_SECTORS_KB_PATH isn't necessary an
1236                          * error for some device. */
1237                         goto subdevs;
1238                 }
1239
1240                 newval = strtoull(buf, &end, 0);
1241                 if (newval == 0 || newval == ULLONG_MAX || end == buf)
1242                         goto subdevs;
1243
1244                 /* Don't increase IO request size limit past 16MB.  It is about
1245                  * PTLRPC_MAX_BRW_SIZE, but that isn't in a public header.
1246                  * Note that even though the block layer allows larger values,
1247                  * setting max_sectors_kb = 32768 causes crashes (LU-6974). */
1248                 if (mop->mo_max_sectors_kb < 0 && newval > 16 * 1024) {
1249                         newval = 16 * 1024;
1250                         snprintf(buf, sizeof(buf), "%llu", newval);
1251                 }
1252
1253                 oldval = strtoull(oldbuf, &end, 0);
1254                 /* Don't shrink the current limit. */
1255                 if (mop->mo_max_sectors_kb < 0 && oldval != ULLONG_MAX &&
1256                     newval <= oldval)
1257                         goto subdevs;
1258
1259                 rc = write_file(real_path, buf);
1260                 if (rc != 0) {
1261                         if (verbose)
1262                                 fprintf(stderr, "warning: writing to %s: %s\n",
1263                                         real_path, strerror(errno));
1264                         /* No MAX_SECTORS_KB_PATH isn't necessary an
1265                          * error for some device. */
1266                         goto subdevs;
1267                 }
1268                 fprintf(stderr, "%s: increased %s from %s to %s\n",
1269                         progname, real_path, oldbuf, buf);
1270         }
1271
1272 subdevs:
1273         /* Purposely ignore errors reported from set_blockdev_scheduler.
1274          * The worst that will happen is a block device with an "incorrect"
1275          * scheduler. */
1276         snprintf(real_path, sizeof(real_path), "%s/%s", path, SCHEDULER_PATH);
1277         set_blockdev_scheduler(real_path, DEFAULT_SCHEDULER);
1278
1279         /* if device is multipath device, tune its slave devices */
1280         glob_info.gl_pathc = 0;
1281         glob_info.gl_offs = 0;
1282         snprintf(real_path, sizeof(real_path), "%s/slaves/*", path);
1283         rc = glob(real_path, GLOB_NOSORT, NULL, &glob_info);
1284
1285         for (i = 0; rc == 0 && i < glob_info.gl_pathc; i++) {
1286                 slave = basename(glob_info.gl_pathv[i]);
1287                 snprintf(real_path, sizeof(real_path), "/dev/%s", slave);
1288                 rc = set_blockdev_tunables(real_path, mop);
1289         }
1290
1291         if (rc == GLOB_NOMATCH) {
1292                 /* no slave device is not an error */
1293                 rc = 0;
1294         } else if (rc && verbose) {
1295                 if (slave == NULL) {
1296                         fprintf(stderr, "warning: %s, failed to read"
1297                                 " entries under %s/slaves\n",
1298                                 strerror(errno), path);
1299                 } else {
1300                         fprintf(stderr, "unable to set tunables for"
1301                                 " slave device %s (slave would be"
1302                                 " unable to handle IO request from"
1303                                 " master %s)\n",
1304                                 real_path, source);
1305                 }
1306         }
1307         globfree(&glob_info);
1308
1309         return rc;
1310 }
1311
1312 int ldiskfs_tune_lustre(char *dev, struct mount_opts *mop)
1313 {
1314         return set_blockdev_tunables(dev, mop);
1315 }
1316
1317 int ldiskfs_label_lustre(struct mount_opts *mop)
1318 {
1319         char label_cmd[PATH_MAX];
1320         int rc;
1321
1322         snprintf(label_cmd, sizeof(label_cmd),
1323                  TUNE2FS" -f -L '%s' '%s' >/dev/null 2>&1",
1324                  mop->mo_ldd.ldd_svname, mop->mo_source);
1325         rc = run_command(label_cmd, sizeof(label_cmd));
1326
1327         return rc;
1328 }
1329
1330 int ldiskfs_rename_fsname(struct mkfs_opts *mop, const char *oldname)
1331 {
1332         struct mount_opts opts;
1333         struct lustre_disk_data *ldd = &mop->mo_ldd;
1334         char mntpt[] = "/tmp/mntXXXXXX";
1335         char *dev;
1336         int ret;
1337
1338         /* Change the filesystem label. */
1339         opts.mo_ldd = *ldd;
1340         opts.mo_source = mop->mo_device;
1341         ret = ldiskfs_label_lustre(&opts);
1342         if (ret) {
1343                 if (errno != 0)
1344                         ret = errno;
1345                 fprintf(stderr, "Can't change filesystem label: %s\n",
1346                         strerror(ret));
1347                 return ret;
1348         }
1349
1350         /* Mount this device temporarily in order to write these files */
1351         if (mkdtemp(mntpt) == NULL) {
1352                 if (errno != 0)
1353                         ret = errno;
1354                 else
1355                         ret = EINVAL;
1356                 fprintf(stderr, "Can't create temp mount point %s: %s\n",
1357                         mntpt, strerror(ret));
1358                 return ret;
1359         }
1360
1361 #ifdef HAVE_SELINUX
1362         /*
1363          * Append file context to mount options if SE Linux is enabled
1364          */
1365         if (is_selinux_enabled() > 0)
1366                 append_context_for_mount(mntpt, mop);
1367 #endif
1368
1369         if (mop->mo_flags & MO_IS_LOOP)
1370                 dev = mop->mo_loopdev;
1371         else
1372                 dev = mop->mo_device;
1373         ret = mount(dev, mntpt, MT_STR(ldd), 0, ldd->ldd_mount_opts);
1374         if (ret) {
1375                 if (errno != 0)
1376                         ret = errno;
1377                 fprintf(stderr, "Unable to mount %s: %s\n",
1378                         dev, strerror(ret));
1379                 if (ret == ENODEV)
1380                         fprintf(stderr, "Is the %s module available?\n",
1381                                 MT_STR(ldd));
1382                 goto out_rmdir;
1383         }
1384
1385         ret = lustre_rename_fsname(mop, mntpt, oldname);
1386         umount(mntpt);
1387
1388 out_rmdir:
1389         rmdir(mntpt);
1390         return ret;
1391 }
1392
1393 /* Enable quota accounting */
1394 int ldiskfs_enable_quota(struct mkfs_opts *mop)
1395 {
1396         char *dev;
1397         char cmd[512];
1398         int cmdsz = sizeof(cmd), ret;
1399
1400         if (is_e2fsprogs_feature_supp("-O quota") != 0) {
1401                 fprintf(stderr, "%s: \"-O quota\" is is not supported by "
1402                         "current e2fsprogs\n", progname);
1403                 return EINVAL;
1404         }
1405
1406         dev = mop->mo_device;
1407         if (mop->mo_flags & MO_IS_LOOP)
1408                 dev = mop->mo_loopdev;
1409
1410         /* Quota feature is already enabled? */
1411         if (is_feature_enabled("quota", dev)) {
1412                 vprint("Quota feature is already enabled.\n");
1413                 return 0;
1414         }
1415
1416         /* Turn on quota feature by "tune2fs -O quota" */
1417         snprintf(cmd, cmdsz, "%s -O quota %s", TUNE2FS, dev);
1418         ret = run_command(cmd, cmdsz);
1419         if (ret)
1420                 fprintf(stderr, "command:%s (%d)", cmd, ret);
1421
1422         return ret;
1423 }
1424
1425 int ldiskfs_init(void)
1426 {
1427         /* Required because full path to DEBUGFS is not specified */
1428         setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 0);
1429
1430         return 0;
1431 }
1432
1433 void ldiskfs_fini(void)
1434 {
1435         return;
1436 }
1437