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