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