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