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