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