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