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