Whamcloud - gitweb
LU-1581 utils: unknown fstype
[fs/lustre-release.git] / lustre / utils / mount_lustre.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) 2011, 2012, Whamcloud, Inc.
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_lustre.c
37  *
38  * Author: Robert Read <rread@clusterfs.com>
39  * Author: Nathan Rutman <nathan@clusterfs.com>
40  */
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <string.h>
51 #include "obdctl.h"
52 #include <lustre_ver.h>
53 #include <glob.h>
54 #include <ctype.h>
55 #include <limits.h>
56 #include "mount_utils.h"
57
58 #define MAX_HW_SECTORS_KB_PATH  "queue/max_hw_sectors_kb"
59 #define MAX_SECTORS_KB_PATH     "queue/max_sectors_kb"
60 #define STRIPE_CACHE_SIZE       "md/stripe_cache_size"
61 #define MAX_RETRIES 99
62
63 int          verbose = 0;
64 int          nomtab = 0;
65 int          fake = 0;
66 int          force = 0;
67 int          retry = 0;
68 int          md_stripe_cache_size = 16384;
69 char         *progname = NULL;
70
71 void usage(FILE *out)
72 {
73         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
74         fprintf(out, "\nThis mount helper should only be invoked via the "
75                 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
76         fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
77                 progname);
78         fprintf(out,
79                 "\t<device>: the disk device, or for a client:\n"
80                 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
81                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
82                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
83                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
84                 "\t-o force|--force: force mount even if already in /etc/mtab\n"
85                 "\t-h|--help: print this usage message\n"
86                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
87                 "\t-v|--verbose: print verbose config settings\n"
88                 "\t<mntopt>: one or more comma separated of:\n"
89                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
90                 "\t\tabort_recov: abort server recovery handling\n"
91                 "\t\tnosvc: only start MGC/MGS obds\n"
92                 "\t\tnomgs: only start target obds, using existing MGS\n"
93                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
94                 "inactive OSTs (e.g. lustre-OST0001)\n"
95                 "\t\tretry=<num>: number of times mount is retried by client\n"
96                 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
97                 "size for the underlying raid if present\n"
98                 );
99         exit((out != stdout) ? EINVAL : 0);
100 }
101
102 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
103 #define MAXNIDSTR 1024
104 static char *convert_hostnames(char *s1)
105 {
106         char *converted, *s2 = 0, *c;
107         char sep;
108         int left = MAXNIDSTR;
109         lnet_nid_t nid;
110
111         converted = malloc(left);
112         if (converted == NULL) {
113                 fprintf(stderr, "out of memory: needed %d bytes\n",
114                         MAXNIDSTR);
115                 return NULL;
116         }
117         c = converted;
118         while ((left > 0) && (*s1 != '/')) {
119                 s2 = strpbrk(s1, ",:");
120                 if (!s2)
121                         goto out_free;
122                 sep = *s2;
123                 *s2 = '\0';
124                 nid = libcfs_str2nid(s1);
125                 *s2 = sep;                      /* back to original string */
126                 if (nid == LNET_NID_ANY)
127                         goto out_free;
128                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
129                 left = converted + MAXNIDSTR - c;
130                 s1 = s2 + 1;
131         }
132         snprintf(c, left, "%s", s1);
133         return converted;
134 out_free:
135         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
136         free(converted);
137         return NULL;
138 }
139
140 /*****************************************************************************
141  *
142  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
143  * license information, but many other files in the package are identified as
144  * GNU GPL, so it's a pretty safe bet that was their intent.
145  *
146  ****************************************************************************/
147 struct opt_map {
148         const char *opt;        /* option name */
149         int inv;                /* true if flag value should be inverted */
150         int mask;               /* flag mask value */
151 };
152
153 static const struct opt_map opt_map[] = {
154   /*"optname", inv,ms_mask */
155   /* These flags are parsed by mount, not lustre */
156   { "defaults", 0, 0         },      /* default options */
157   { "remount",  0, MS_REMOUNT},      /* remount with different options */
158   { "rw",       1, MS_RDONLY },      /* read-write */
159   { "ro",       0, MS_RDONLY },      /* read-only */
160   { "exec",     1, MS_NOEXEC },      /* permit execution of binaries */
161   { "noexec",   0, MS_NOEXEC },      /* don't execute binaries */
162   { "suid",     1, MS_NOSUID },      /* honor suid executables */
163   { "nosuid",   0, MS_NOSUID },      /* don't honor suid executables */
164   { "dev",      1, MS_NODEV  },      /* interpret device files  */
165   { "nodev",    0, MS_NODEV  },      /* don't interpret devices */
166   { "sync",     0, MS_SYNCHRONOUS},  /* synchronous I/O */
167   { "async",    1, MS_SYNCHRONOUS},  /* asynchronous I/O */
168   { "atime",    1, MS_NOATIME  },    /* set file access time on read */
169   { "noatime",  0, MS_NOATIME  },    /* do not set file access time on read */
170 #ifdef MS_NODIRATIME
171   { "diratime", 1, MS_NODIRATIME },  /* set file access time on read */
172   { "nodiratime",0,MS_NODIRATIME },  /* do not set file access time on read */
173 #endif
174 #ifdef MS_RELATIME
175   { "relatime", 0, MS_RELATIME },  /* set file access time on read */
176   { "norelatime",1,MS_RELATIME },  /* do not set file access time on read */
177 #endif
178 #ifdef MS_STRICTATIME
179   { "strictatime",0,MS_STRICTATIME },  /* update access time strictly */
180 #endif
181   { "auto",     0, 0         },      /* Can be mounted using -a */
182   { "noauto",   0, 0         },      /* Can only be mounted explicitly */
183   { "nousers",  1, 0         },      /* Forbid ordinary user to mount */
184   { "nouser",   1, 0         },      /* Forbid ordinary user to mount */
185   { "noowner",  1, 0         },      /* Device owner has no special privs */
186   { "_netdev",  0, 0         },      /* Device accessible only via network */
187   { "loop",     0, 0         },
188   { NULL,       0, 0         }
189 };
190 /****************************************************************************/
191
192 /* 1  = don't pass on to lustre
193    0  = pass on to lustre */
194 static int parse_one_option(const char *check, int *flagp)
195 {
196         const struct opt_map *opt;
197
198         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
199                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
200                         if (opt->mask) {
201                                 if (opt->inv)
202                                         *flagp &= ~(opt->mask);
203                                 else
204                                         *flagp |= opt->mask;
205                         }
206                         return 1;
207                 }
208         }
209         /* Assume any unknown options are valid and pass them on.  The mount
210            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
211         return 0;
212 }
213
214 static void append_option(char *options, const char *one)
215 {
216         if (*options)
217                 strcat(options, ",");
218         strcat(options, one);
219 }
220
221 /* Replace options with subset of Lustre-specific options, and
222    fill in mount flags */
223 int parse_options(char *orig_options, int *flagp)
224 {
225         char *options, *opt, *nextopt, *arg, *val;
226
227         options = calloc(strlen(orig_options) + 1, 1);
228         *flagp = 0;
229         nextopt = orig_options;
230         while ((opt = strsep(&nextopt, ","))) {
231                 if (!*opt)
232                         /* empty option */
233                         continue;
234
235                 /* Handle retries in a slightly different
236                  * manner */
237                 arg = opt;
238                 val = strchr(opt, '=');
239                 /* please note that some ldiskfs mount options are also in the form
240                  * of param=value. We should pay attention not to remove those
241                  * mount options, see bug 22097. */
242                 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
243                         md_stripe_cache_size = atoi(val + 1);
244                 } else if (val && strncmp(arg, "retry", 5) == 0) {
245                         retry = atoi(val + 1);
246                         if (retry > MAX_RETRIES)
247                                 retry = MAX_RETRIES;
248                         else if (retry < 0)
249                                 retry = 0;
250                 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
251                         append_option(options, opt);
252                 } else if (strcmp(opt, "force") == 0) {
253                         //XXX special check for 'force' option
254                         ++force;
255                         printf("force: %d\n", force);
256                 } else if (parse_one_option(opt, flagp) == 0) {
257                         /* pass this on as an option */
258                         append_option(options, opt);
259                 }
260         }
261 #ifdef MS_STRICTATIME
262                 /* set strictatime to default if NOATIME or RELATIME
263                    not given explicit */
264         if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
265                 *flagp |= MS_STRICTATIME;
266 #endif
267         strcpy(orig_options, options);
268         free(options);
269         return 0;
270 }
271
272
273 int read_file(char *path, char *buf, int size)
274 {
275         FILE *fd;
276
277         fd = fopen(path, "r");
278         if (fd == NULL)
279                 return errno;
280
281         /* should not ignore fgets(3)'s return value */
282         if (!fgets(buf, size, fd)) {
283                 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
284                 fclose(fd);
285                 return 1;
286         }
287         fclose(fd);
288         return 0;
289 }
290
291 int write_file(char *path, char *buf)
292 {
293         FILE *fd;
294
295         fd = fopen(path, "w");
296         if (fd == NULL)
297                 return errno;
298
299         fputs(buf, fd);
300         fclose(fd);
301         return 0;
302 }
303
304 /* This is to tune the kernel for good SCSI performance.
305  * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
306  * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
307 int set_blockdev_tunables(char *source, int fan_out)
308 {
309         glob_t glob_info = { 0 };
310         struct stat stat_buf;
311         char *chk_major, *chk_minor;
312         char *savept = NULL, *dev;
313         char *ret_path;
314         char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
315         char real_path[PATH_MAX] = {'\0'};
316         int i, rc = 0;
317         int major, minor;
318
319         if (!source)
320                 return -EINVAL;
321
322         ret_path = realpath(source, real_path);
323         if (ret_path == NULL) {
324                 if (verbose)
325                         fprintf(stderr, "warning: %s: cannot resolve: %s\n",
326                                 source, strerror(errno));
327                 return -EINVAL;
328         }
329
330         if (strncmp(real_path, "/dev/loop", 9) == 0)
331                 return 0;
332
333         if ((real_path[0] != '/') && (strpbrk(real_path, ",:") != NULL))
334                 return 0;
335
336         snprintf(path, sizeof(path), "/sys/block%s", real_path + 4);
337         if (access(path, X_OK) == 0)
338                 goto set_params;
339
340         /* The name of the device say 'X' specified in /dev/X may not
341          * match any entry under /sys/block/. In that case we need to
342          * match the major/minor number to find the entry under
343          * sys/block corresponding to /dev/X */
344
345         /* Don't chop tail digit on /dev/mapper/xxx, LU-478 */
346         if (strncmp(real_path, "/dev/mapper", 11) != 0) {
347                 dev = real_path + strlen(real_path);
348                 while (--dev > real_path && isdigit(*dev))
349                         *dev = 0;
350
351                 if (strncmp(real_path, "/dev/md_", 8) == 0)
352                         *dev = 0;
353         }
354
355         rc = stat(real_path, &stat_buf);
356         if (rc) {
357                 if (verbose)
358                         fprintf(stderr, "warning: %s, device %s stat failed\n",
359                                 strerror(errno), real_path);
360                 return rc;
361         }
362
363         major = major(stat_buf.st_rdev);
364         minor = minor(stat_buf.st_rdev);
365         rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
366         if (rc) {
367                 if (verbose)
368                         fprintf(stderr, "warning: failed to read entries under "
369                                 "/sys/block\n");
370                 globfree(&glob_info);
371                 return rc;
372         }
373
374         for (i = 0; i < glob_info.gl_pathc; i++){
375                 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
376
377                 rc = read_file(path, buf, sizeof(buf));
378                 if (rc)
379                         continue;
380
381                 if (buf[strlen(buf) - 1] == '\n')
382                         buf[strlen(buf) - 1] = '\0';
383
384                 chk_major = strtok_r(buf, ":", &savept);
385                 chk_minor = savept;
386                 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
387                         break;
388         }
389
390         if (i == glob_info.gl_pathc) {
391                 if (verbose)
392                         fprintf(stderr,"warning: device %s does not match any "
393                                 "entry under /sys/block\n", real_path);
394                 globfree(&glob_info);
395                 return -EINVAL;
396         }
397
398         /* Chop off "/dev" from path we found */
399         path[strlen(glob_info.gl_pathv[i])] = '\0';
400         globfree(&glob_info);
401
402 set_params:
403         if (strncmp(real_path, "/dev/md", 7) == 0) {
404                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
405                          STRIPE_CACHE_SIZE);
406
407                 rc = read_file(real_path, buf, sizeof(buf));
408                 if (rc) {
409                         if (verbose)
410                                 fprintf(stderr, "warning: opening %s: %s\n",
411                                         real_path, strerror(errno));
412                         return 0;
413                 }
414
415                 if (atoi(buf) >= md_stripe_cache_size)
416                         return 0;
417
418                 if (strlen(buf) - 1 > 0) {
419                         snprintf(buf, sizeof(buf), "%d", md_stripe_cache_size);
420                         rc = write_file(real_path, buf);
421                         if (rc && verbose)
422                                 fprintf(stderr, "warning: opening %s: %s\n",
423                                         real_path, strerror(errno));
424                 }
425                 /* Return since raid and disk tunables are different */
426                 return rc;
427         }
428
429         snprintf(real_path, sizeof(real_path), "%s/%s", path,
430                  MAX_HW_SECTORS_KB_PATH);
431         rc = read_file(real_path, buf, sizeof(buf));
432         if (rc) {
433                 if (verbose)
434                         fprintf(stderr, "warning: opening %s: %s\n",
435                                 real_path, strerror(errno));
436                 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
437                  * error for some device. */
438                 rc = 0;
439         }
440
441         if (strlen(buf) - 1 > 0) {
442                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
443                          MAX_SECTORS_KB_PATH);
444                 rc = write_file(real_path, buf);
445                 if (rc) {
446                         if (verbose)
447                                 fprintf(stderr, "warning: writing to %s: %s\n",
448                                         real_path, strerror(errno));
449                         /* No MAX_SECTORS_KB_PATH isn't necessary an
450                          * error for some device. */
451                         rc = 0;
452                 }
453         }
454
455         if (fan_out) {
456                 char *slave = NULL;
457                 glob_info.gl_pathc = 0;
458                 glob_info.gl_offs = 0;
459                 /* if device is multipath device, tune its slave devices */
460                 snprintf(real_path, sizeof(real_path), "%s/slaves/*", path);
461                 rc = glob(real_path, GLOB_NOSORT, NULL, &glob_info);
462
463                 for (i = 0; rc == 0 && i < glob_info.gl_pathc; i++){
464                         slave = basename(glob_info.gl_pathv[i]);
465                         snprintf(real_path, sizeof(real_path), "/dev/%s", slave);
466                         rc = set_blockdev_tunables(real_path, 0);
467                 }
468
469                 if (rc == GLOB_NOMATCH) {
470                         /* no slave device is not an error */
471                         rc = 0;
472                 } else if (rc && verbose) {
473                         if (slave == NULL) {
474                                 fprintf(stderr, "warning: %s, failed to read"
475                                         " entries under %s/slaves\n",
476                                         strerror(errno), path);
477                         } else {
478                                 fprintf(stderr, "unable to set tunables for"
479                                         " slave device %s (slave would be"
480                                         " unable to handle IO request from"
481                                         " master %s)\n",
482                                         real_path, source);
483                         }
484                 }
485                 globfree(&glob_info);
486         }
487
488         return rc;
489 }
490
491 int main(int argc, char *const argv[])
492 {
493         char default_options[] = "";
494         char *usource, *source, *ptr;
495         char target[PATH_MAX] = {'\0'};
496         char real_path[PATH_MAX] = {'\0'};
497         char path[256], name[256];
498         FILE *f;
499         size_t sz;
500         char *options, *optcopy, *orig_options = default_options;
501         int i, nargs = 3, opt, rc, flags, optlen;
502         static struct option long_opt[] = {
503                 {"fake", 0, 0, 'f'},
504                 {"force", 0, 0, 1},
505                 {"help", 0, 0, 'h'},
506                 {"nomtab", 0, 0, 'n'},
507                 {"options", 1, 0, 'o'},
508                 {"verbose", 0, 0, 'v'},
509                 {0, 0, 0, 0}
510         };
511
512         progname = strrchr(argv[0], '/');
513         progname = progname ? progname + 1 : argv[0];
514
515         while ((opt = getopt_long(argc, argv, "fhno:v",
516                                   long_opt, NULL)) != EOF){
517                 switch (opt) {
518                 case 1:
519                         ++force;
520                         printf("force: %d\n", force);
521                         nargs++;
522                         break;
523                 case 'f':
524                         ++fake;
525                         printf("fake: %d\n", fake);
526                         nargs++;
527                         break;
528                 case 'h':
529                         usage(stdout);
530                         break;
531                 case 'n':
532                         ++nomtab;
533                         printf("nomtab: %d\n", nomtab);
534                         nargs++;
535                         break;
536                 case 'o':
537                         orig_options = optarg;
538                         nargs++;
539                         break;
540                 case 'v':
541                         ++verbose;
542                         nargs++;
543                         break;
544                 default:
545                         fprintf(stderr, "%s: unknown option '%c'\n",
546                                 progname, opt);
547                         usage(stderr);
548                         break;
549                 }
550         }
551
552         if (optind + 2 > argc) {
553                 fprintf(stderr, "%s: too few arguments\n", progname);
554                 usage(stderr);
555         }
556
557         usource = argv[optind];
558         if (!usource) {
559                 usage(stderr);
560         }
561
562         /**
563          * Try to get the real path to the device, in case it is a
564          * symbolic link for instance
565          */
566         if (realpath(usource, real_path) != NULL) {
567                 usource = real_path;
568
569                 ptr = strrchr(real_path, '/');
570                 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
571                         snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
572                         if ((f = fopen(path, "r"))) {
573                                 /* read "<name>\n" from sysfs */
574                                 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
575                                         name[sz - 1] = '\0';
576                                         snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
577                                 }
578                                 fclose(f);
579                         }
580                 }
581         }
582
583         source = convert_hostnames(usource);
584         if (!source) {
585                 usage(stderr);
586         }
587
588         if (realpath(argv[optind + 1], target) == NULL) {
589                 rc = errno;
590                 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
591                         argv[optind + 1], strerror(errno));
592                 return rc;
593         }
594
595         if (verbose) {
596                 for (i = 0; i < argc; i++)
597                         printf("arg[%d] = %s\n", i, argv[i]);
598                 printf("source = %s (%s), target = %s\n", usource, source,
599                        target);
600                 printf("options = %s\n", orig_options);
601         }
602
603         options = malloc(strlen(orig_options) + 1);
604         if (options == NULL) {
605                 fprintf(stderr, "can't allocate memory for options\n");
606                 return -1;
607         }
608         strcpy(options, orig_options);
609         rc = parse_options(options, &flags);
610         if (rc) {
611                 fprintf(stderr, "%s: can't parse options: %s\n",
612                         progname, options);
613                 return(EINVAL);
614         }
615
616         if (!force) {
617                 rc = check_mtab_entry(usource, source, target, "lustre");
618                 if (rc && !(flags & MS_REMOUNT)) {
619                         fprintf(stderr, "%s: according to %s %s is "
620                                 "already mounted on %s\n",
621                                 progname, MOUNTED, usource, target);
622                         return(EEXIST);
623                 }
624                 if (!rc && (flags & MS_REMOUNT)) {
625                         fprintf(stderr, "%s: according to %s %s is "
626                                 "not already mounted on %s\n",
627                                 progname, MOUNTED, usource, target);
628                         return(ENOENT);
629                 }
630         }
631         if (flags & MS_REMOUNT)
632                 nomtab++;
633
634         rc = access(target, F_OK);
635         if (rc) {
636                 rc = errno;
637                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
638                         strerror(errno));
639                 return rc;
640         }
641
642         /* In Linux 2.4, the target device doesn't get passed to any of our
643            functions.  So we'll stick it on the end of the options. */
644         optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
645         optcopy = malloc(optlen);
646         if (optcopy == NULL) {
647                 fprintf(stderr, "can't allocate memory to optcopy\n");
648                 return -1;
649         }
650         strcpy(optcopy, options);
651         if (*optcopy)
652                 strcat(optcopy, ",");
653         strcat(optcopy, "device=");
654         strcat(optcopy, source);
655
656         if (verbose)
657                 printf("mounting device %s at %s, flags=%#x options=%s\n",
658                        source, target, flags, optcopy);
659
660         if (!strstr(usource, ":/") && set_blockdev_tunables(source, 1)) {
661                 if (verbose)
662                         fprintf(stderr, "%s: unable to set tunables for %s"
663                                 " (may cause reduced IO performance)\n",
664                                 argv[0], source);
665         }
666
667         if (!fake) {
668                 /* flags and target get to lustre_get_sb, but not
669                    lustre_fill_super.  Lustre ignores the flags, but mount
670                    does not. */
671                 for (i = 0, rc = -EAGAIN; i <= retry && rc != 0; i++) {
672                         rc = mount(source, target, "lustre", flags,
673                                    (void *)optcopy);
674                         if (rc) {
675                                 if (verbose) {
676                                         fprintf(stderr, "%s: mount %s at %s "
677                                                 "failed: %s retries left: "
678                                                 "%d\n", basename(progname),
679                                                 usource, target,
680                                                 strerror(errno), retry-i);
681                                 }
682
683                                 if (retry) {
684                                         sleep(1 << max((i/2), 5));
685                                 }
686                                 else {
687                                         rc = errno;
688                                 }
689                         }
690                 }
691         }
692
693         if (rc) {
694                 char *cli;
695
696                 rc = errno;
697
698                 cli = strrchr(usource, ':');
699                 if (cli && (strlen(cli) > 2))
700                         cli += 2;
701                 else
702                         cli = NULL;
703
704                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
705                         usource, target, strerror(errno));
706                 if (errno == ENODEV)
707                         fprintf(stderr, "Are the lustre modules loaded?\n"
708                                 "Check /etc/modprobe.conf and "
709                                 "/proc/filesystems\n");
710                 if (errno == ENOTBLK)
711                         fprintf(stderr, "Do you need -o loop?\n");
712                 if (errno == ENOMEDIUM)
713                         fprintf(stderr,
714                                 "This filesystem needs at least 1 OST\n");
715                 if (errno == ENOENT) {
716                         fprintf(stderr, "Is the MGS specification correct?\n");
717                         fprintf(stderr, "Is the filesystem name correct?\n");
718                         fprintf(stderr, "If upgrading, is the copied client log"
719                                 " valid? (see upgrade docs)\n");
720                 }
721                 if (errno == EALREADY)
722                         fprintf(stderr, "The target service is already running."
723                                 " (%s)\n", usource);
724                 if (errno == ENXIO)
725                         fprintf(stderr, "The target service failed to start "
726                                 "(bad config log?) (%s).  "
727                                 "See /var/log/messages.\n", usource);
728                 if (errno == EIO)
729                         fprintf(stderr, "Is the MGS running?\n");
730                 if (errno == EADDRINUSE)
731                         fprintf(stderr, "The target service's index is already "
732                                 "in use. (%s)\n", usource);
733                 if (errno == EINVAL) {
734                         fprintf(stderr, "This may have multiple causes.\n");
735                         if (cli)
736                                 fprintf(stderr, "Is '%s' the correct filesystem"
737                                         " name?\n", cli);
738                         fprintf(stderr, "Are the mount options correct?\n");
739                         fprintf(stderr, "Check the syslog for more info.\n");
740                 }
741
742                 /* May as well try to clean up loop devs */
743                 if (strncmp(usource, "/dev/loop", 9) == 0) {
744                         char cmd[256];
745                         int ret;
746                         sprintf(cmd, "/sbin/losetup -d %s", usource);
747                         if ((ret = system(cmd)) < 0)
748                                 rc = errno;
749                         else if (ret > 0)
750                                 rc = WEXITSTATUS(ret);
751                 }
752
753         } else if (!nomtab) {
754                 rc = update_mtab_entry(usource, target, "lustre", orig_options,
755                                        0,0,0);
756         }
757
758         free(optcopy);
759         free(source);
760         return rc;
761 }