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