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