Whamcloud - gitweb
LU-9934 build: address issues raised by gcc7
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/utils/mount_lustre.c
33  *
34  * Author: Robert Read <rread@clusterfs.com>
35  * Author: Nathan Rutman <nathan@clusterfs.com>
36  */
37
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41 #include <getopt.h>
42 #include <mntent.h>
43 #include <stdbool.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <sys/mount.h>
51 #include <sys/param.h>
52 #include <linux/lustre/lustre_ver.h>
53 #include <ctype.h>
54 #include <limits.h>
55 #include <linux/lnet/nidstr.h>
56 #include <libcfs/util/string.h>
57
58 #include "obdctl.h"
59 #include "mount_utils.h"
60
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 #ifdef HAVE_LIBMOUNT
76 # define WITH_LIBMOUNT  "(libmount)"
77 #else
78 # define WITH_LIBMOUNT  ""
79 #endif
80
81 #define MAX_RETRIES 99
82
83 int     verbose;
84 int     version;
85 char    *progname;
86
87 void usage(FILE *out)
88 {
89         fprintf(out, "\nThis mount helper should only be invoked via the "
90                 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
91         fprintf(out, "usage: %s [-fhnvV] [-o <mntopt>] <device> <mountpt>\n",
92                 progname);
93         fprintf(out,
94                 "\t<device>: the disk device, or for a client:\n"
95                 "\t\t<mgsnid>[:<altmgsnid>...]:/<filesystem>[/<subdir>]\n"
96                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
97                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
98                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
99                 "\t-o force|--force: force mount even if already in /etc/mtab\n"
100                 "\t-h|--help: print this usage message\n"
101                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
102                 "\t-v|--verbose: print verbose config settings\n"
103                 "\t-V|--version: output build version of the utility and exit\n"
104                 "\t<mntopt>: one or more comma separated of:\n"
105                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
106                 "\t\tabort_recov: abort server recovery handling\n"
107                 "\t\tnosvc: only start MGC/MGS obds\n"
108                 "\t\tnomgs: only start target obds, using existing MGS\n"
109                 "\t\tnoscrub: NOT auto start OI scrub unless start explicitly\n"
110                 "\t\tskip_lfsck: NOT auto resume the paused/crashed LFSCK\n"
111                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
112                 "inactive OSTs (e.g. lustre-OST0001)\n"
113 #ifdef HAVE_GSS
114                 "\t\tskpath=<file|directory>: Path to a file or directory of"
115                 "key configuration files to load into the kernel keyring\n"
116 #endif
117                 "\t\tretry=<num>: number of times mount is retried by client\n"
118                 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
119                 "size for the underlying raid if present\n");
120         exit((out != stdout) ? EINVAL : 0);
121 }
122
123 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
124 #define MAXNIDSTR 1024
125 static char *convert_hostnames(char *s1)
126 {
127         char *converted, *s2 = 0, *c;
128         char sep;
129         int left = MAXNIDSTR;
130         lnet_nid_t nid;
131
132         converted = malloc(left);
133         if (converted == NULL) {
134                 fprintf(stderr, "out of memory: needed %d bytes\n",
135                         MAXNIDSTR);
136                 return NULL;
137         }
138         c = converted;
139         while ((left > 0) && (*s1 != '/')) {
140                 s2 = strpbrk(s1, ",:");
141                 if (!s2)
142                         goto out_free;
143                 sep = *s2;
144                 *s2 = '\0';
145                 nid = libcfs_str2nid(s1);
146                 *s2 = sep;                      /* back to original string */
147                 if (nid == LNET_NID_ANY)
148                         goto out_free;
149                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
150                 left = converted + MAXNIDSTR - c;
151                 s1 = s2 + 1;
152         }
153         snprintf(c, left, "%s", s1);
154         return converted;
155 out_free:
156         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
157         free(converted);
158         return NULL;
159 }
160
161 /*****************************************************************************
162  *
163  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
164  * license information, but many other files in the package are identified as
165  * GNU GPL, so it's a pretty safe bet that was their intent.
166  *
167  ****************************************************************************/
168 struct opt_map {
169         const char *opt;        /* option name */
170         int inv;                /* true if flag value should be inverted */
171         int mask;               /* flag mask value */
172 };
173
174 static const struct opt_map opt_map[] = {
175 /* these flags are parsed by mount, not Lustre */
176 { .opt = "async",   .mask = MS_SYNCHRONOUS, .inv = 1 }, /* asynchronous I/O */
177 { .opt = "atime",   .mask = MS_NOATIME,     .inv = 1 }, /* set access time */
178 { .opt = "auto" },                                      /* allow auto mount */
179 { .opt = "defaults" },                                  /* default options */
180 { .opt = "dev",     .mask = MS_NODEV,       .inv = 1 }, /* interpret devs */
181 { .opt = "exec",    .mask = MS_NOEXEC,      .inv = 1 }, /* allow execution */
182 { .opt = "loop" },
183 { .opt = "noatime", .mask = MS_NOATIME },               /* do not set atime */
184 { .opt = "noauto" },                                    /* mount explicitly */
185 { .opt = "nodev",   .mask = MS_NODEV },                 /* no interpret devs */
186 { .opt = "noowner",                         .inv = 1 }, /* no special privs */
187 { .opt = "nosuid",  .mask = MS_NOSUID },                /* do not honor suid */
188 { .opt = "nouser",                          .inv = 1 }, /* users cannot mount */
189 { .opt = "nousers",                         .inv = 1 }, /* users cannot mount */
190 { .opt = "_netdev" },                                   /* network only */
191 { .opt = "noexec",  .mask = MS_NOEXEC },                /* no execute */
192 { .opt = "remount", .mask = MS_REMOUNT },               /* remount */
193 { .opt = "ro",      .mask = MS_RDONLY },                /* read-only */
194 { .opt = "rw",      .mask = MS_RDONLY,      .inv = 1 }, /* read-write */
195 { .opt = "suid",    .mask = MS_NOSUID,      .inv = 1 }, /* honor suid */
196 { .opt = "sync",    .mask = MS_SYNCHRONOUS },           /* synchronous I/O */
197 #ifdef MS_NODIRATIME
198 { .opt = "diratime",                                    /* set access time */
199                     .mask = MS_NODIRATIME,  .inv = 1 }, /* on read */
200 { .opt = "nodiratime",                                  /* do not set access */
201                     .mask = MS_NODIRATIME },            /* time on read */
202 #endif
203 #ifdef MS_RELATIME
204 { .opt = "norelatime",                                  /* do not set rel */
205                     .mask = MS_RELATIME,    .inv = 1 }, /* access time */
206 { .opt = "relatime",                                    /* set relative */
207                     .mask = MS_RELATIME },              /* access time */
208 #endif
209 #ifdef MS_STRICTATIME
210 { .opt = "strictatime",
211                     .mask = MS_STRICTATIME },           /* strict access time */
212 #endif
213 { .opt = NULL } };
214 /****************************************************************************/
215
216 /* 1  = don't pass on to lustre
217    0  = pass on to lustre */
218 static int parse_one_option(const char *check, int *flagp)
219 {
220         const struct opt_map *opt;
221
222         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
223                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
224                         if (opt->mask) {
225                                 if (opt->inv)
226                                         *flagp &= ~(opt->mask);
227                                 else
228                                         *flagp |= opt->mask;
229                         }
230                         return 1;
231                 }
232         }
233         /* Assume any unknown options are valid and pass them on.  The mount
234            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
235         return 0;
236 }
237
238 static int append_option(char *options, size_t options_len,
239                          const char *param, const char *value)
240 {
241         int rc;
242         if (options[0] != '\0') {
243                 rc = strlcat(options, ",", options_len);
244                 if (rc >= options_len)
245                         goto out_err;
246         }
247         rc = strlcat(options, param, options_len);
248         if (rc >= options_len)
249                 goto out_err;
250         if (value != NULL) {
251                 rc = strlcat(options, value, options_len);
252                 if (rc >= options_len)
253                         goto out_err;
254         }
255         return 0;
256 out_err:
257         fprintf(stderr, "error: mount options %s%s too long\n", param, value);
258         return E2BIG;
259 }
260
261 /* Replace options with subset of Lustre-specific options, and
262    fill in mount flags */
263 int parse_options(struct mount_opts *mop, char *orig_options,
264                   int *flagp, size_t options_len)
265 {
266         char *options, *opt, *nextopt, *arg, *val;
267         int rc = 0;
268
269         options = calloc(strlen(orig_options) + 1, 1);
270         *flagp = 0;
271         nextopt = orig_options;
272         while ((opt = strsep(&nextopt, ","))) {
273                 if (!*opt)
274                         /* empty option */
275                         continue;
276
277                 /* Handle retries in a slightly different
278                  * manner */
279                 arg = opt;
280                 val = strchr(opt, '=');
281                 /* please note that some ldiskfs mount options are also in
282                  * the form of param=value. We should pay attention not to
283                  * remove those mount options, see bug 22097. */
284                 if (val && strncmp(arg, "max_sectors_kb", 14) == 0) {
285                         mop->mo_max_sectors_kb = atoi(val + 1);
286                 } else if (val &&
287                            strncmp(arg, "md_stripe_cache_size", 20) == 0) {
288                         mop->mo_md_stripe_cache_size = atoi(val + 1);
289                 } else if (val && strncmp(arg, "retry", 5) == 0) {
290                         mop->mo_retry = atoi(val + 1);
291                         if (mop->mo_retry > MAX_RETRIES)
292                                 mop->mo_retry = MAX_RETRIES;
293                         else if (mop->mo_retry < 0)
294                                 mop->mo_retry = 0;
295                 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
296                         rc = append_option(options, options_len, opt, NULL);
297                         if (rc != 0)
298                                 goto out_options;
299                 } else if (strncmp(arg, "nosvc", 5) == 0) {
300                         mop->mo_nosvc = 1;
301                         rc = append_option(options, options_len, opt, NULL);
302                         if (rc != 0)
303                                 goto out_options;
304                 } else if (strcmp(opt, "force") == 0) {
305                         /* XXX special check for 'force' option */
306                         ++mop->mo_force;
307                         printf("force: %d\n", mop->mo_force);
308 #ifdef HAVE_GSS
309                 } else if (val && strncmp(opt, "skpath=", 7) == 0) {
310                         if (strlen(val) + 1 >= sizeof(mop->mo_skpath)) {
311                                 fprintf(stderr,
312                                         "%s: shared key path too long\n",
313                                         progname);
314                                 free(options);
315                                 return -1;
316                         }
317                         strncpy(mop->mo_skpath, val + 1, strlen(val + 1));
318 #endif
319                 } else if (parse_one_option(opt, flagp) == 0) {
320                         /* pass this on as an option */
321                         rc = append_option(options, options_len, opt, NULL);
322                         if (rc != 0)
323                                 goto out_options;
324                 }
325         }
326 #ifdef MS_STRICTATIME
327 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 2, 53, 0)
328         /*
329          * LU-1783
330          * In the future when upstream fixes land in all supported kernels
331          * we should stop forcing MS_STRICTATIME in lustre mounts.
332          * We override the kernel level default of MS_RELATIME for now
333          * due to a kernel vfs level bug in atime updates that fails
334          * to reset timestamps from the future.
335          */
336 #warn "remove MS_STRICTATIME override if kernel updates atime from the future"
337 #endif
338         /* set strictatime to default if NOATIME or RELATIME
339            not given explicit */
340         if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
341                 *flagp |= MS_STRICTATIME;
342 #endif
343         strcpy(orig_options, options);
344
345 out_options:
346         free(options);
347         return rc;
348 }
349
350 #ifdef HAVE_SERVER_SUPPORT
351 /* Add mgsnids from ldd params */
352 static int add_mgsnids(struct mount_opts *mop, char *options,
353                        const char *params, size_t options_len)
354 {
355         char *ptr = (char *)params;
356         char tmp, *sep;
357         int rc = 0;
358
359         while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
360                 sep = strchr(ptr, ' ');
361                 if (sep != NULL) {
362                         tmp = *sep;
363                         *sep = '\0';
364                 }
365                 rc = append_option(options, options_len, ptr, NULL);
366                 if (rc != 0)
367                         goto out;
368                 mop->mo_have_mgsnid++;
369                 if (sep) {
370                         *sep = tmp;
371                         ptr = sep;
372                 } else {
373                         break;
374                 }
375         }
376
377 out:
378         return rc;
379 }
380
381 static int clear_update_ondisk(char *source, struct lustre_disk_data *ldd)
382 {
383         char wanted_mountopts[512] = "";
384         struct mkfs_opts mkop;
385         int ret;
386         int ret2;
387
388         memset(&mkop, 0, sizeof(mkop));
389         mkop.mo_ldd = *ldd;
390         mkop.mo_ldd.ldd_flags &= ~LDD_F_UPDATE;
391         mkop.mo_flags = MO_NOHOSTID_CHECK; /* Ignore missing hostid */
392         if (strlen(source) > sizeof(mkop.mo_device)-1) {
393                 fatal();
394                 fprintf(stderr, "Device name too long: %s\n", source);
395                 return -E2BIG;
396         }
397         strncpy(mkop.mo_device, source, sizeof(mkop.mo_device));
398
399         ret = osd_prepare_lustre(&mkop,
400                                  wanted_mountopts, sizeof(wanted_mountopts));
401         if (ret) {
402                 fatal();
403                 fprintf(stderr, "Can't prepare device %s: %s\n",
404                         source, strerror(ret));
405                 return ret;
406         }
407
408         /* Create the loopback file */
409         if (mkop.mo_flags & MO_IS_LOOP) {
410                 ret = access(mkop.mo_device, F_OK);
411                 if (ret) {
412                         ret = errno;
413                         fatal();
414                         fprintf(stderr, "Can't access device %s: %s\n",
415                                         source, strerror(ret));
416                         return ret;
417                 }
418
419                 ret = loop_setup(&mkop);
420                 if (ret) {
421                         fatal();
422                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
423                                         mkop.mo_device, strerror(ret));
424                         return ret;
425                 }
426         }
427         ret = osd_write_ldd(&mkop);
428         if (ret != 0) {
429                 fatal();
430                 fprintf(stderr, "failed to write local files: %s\n",
431                         strerror(ret));
432         }
433
434         ret2 = loop_cleanup(&mkop);
435         if (ret == 0)
436                 ret = ret2;
437
438         return ret;
439 }
440
441 static int parse_ldd(char *source, struct mount_opts *mop,
442                      char *options, size_t options_len)
443 {
444         struct lustre_disk_data *ldd = &mop->mo_ldd;
445         char *cur, *start;
446         char *temp_options;
447         int rc = 0;
448
449         rc = osd_is_lustre(source, &ldd->ldd_mount_type);
450         if (rc == 0) {
451                 fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
452                         " or the backend filesystem type is not supported by "
453                         "this tool\n", progname, source);
454                 return ENODEV;
455         }
456
457         rc = osd_read_ldd(source, ldd);
458         if (rc) {
459                 fprintf(stderr, "%s: %s failed to read permanent mount"
460                         " data: %s\n", progname, source,
461                         rc >= 0 ? strerror(rc) : "");
462                 return rc;
463         }
464
465         if ((IS_MDT(ldd) || IS_OST(ldd)) &&
466             (ldd->ldd_flags & LDD_F_NEED_INDEX)) {
467                 fprintf(stderr, "%s: %s has no index assigned "
468                         "(probably formatted with old mkfs)\n",
469                         progname, source);
470                 return EINVAL;
471         }
472
473         if (ldd->ldd_flags & LDD_F_UPGRADE14) {
474                 fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
475                         "Lustre version\n", progname, source);
476                 return EINVAL;
477         }
478
479         if (ldd->ldd_flags & LDD_F_UPDATE)
480                 clear_update_ondisk(source, ldd);
481
482         /* Since we never rewrite ldd, ignore temp flags */
483         ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_WRITECONF);
484
485         /* This is to make sure default options go first */
486         temp_options = strdup(options);
487         if (temp_options == NULL) {
488                 fprintf(stderr, "%s: can't allocate memory for temp_options\n",
489                         progname);
490                 return ENOMEM;
491         }
492         strncpy(options, ldd->ldd_mount_opts, options_len);
493         rc = append_option(options, options_len, temp_options, NULL);
494         free(temp_options);
495         if (rc != 0)
496                 return rc;
497
498         /* svname of the form lustre:OST1234 means never registered */
499         rc = strlen(ldd->ldd_svname);
500         if (strcmp(ldd->ldd_svname, "MGS") != 0) {
501                 if (rc < 8) {
502                         fprintf(stderr, "%s: invalid name '%s'\n",
503                                 progname, ldd->ldd_svname);
504                         return EINVAL;
505                 } else if (ldd->ldd_svname[rc - 8] == ':') {
506                         ldd->ldd_svname[rc - 8] = '-';
507                         ldd->ldd_flags |= LDD_F_VIRGIN;
508                 } else if (ldd->ldd_svname[rc - 8] == '=') {
509                         ldd->ldd_svname[rc - 8] = '-';
510                         ldd->ldd_flags |= LDD_F_WRITECONF;
511                 }
512         }
513         /* backend osd type */
514         rc = append_option(options, options_len, "osd=",
515                            mt_type(ldd->ldd_mount_type));
516         if (rc != 0)
517                 return rc;
518
519         if (!mop->mo_have_mgsnid) {
520                 /* Only use disk data if mount -o mgsnode=nid wasn't
521                  * specified */
522                 if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
523                         rc = append_option(options, options_len, "mgs", NULL);
524                         if (rc != 0)
525                                 return rc;
526                         mop->mo_have_mgsnid++;
527                 } else {
528                         if (add_mgsnids(mop, options, ldd->ldd_params,
529                                         options_len))
530                                 return E2BIG;
531                 }
532         }
533         /* Better have an mgsnid by now */
534         if (!mop->mo_have_mgsnid) {
535                 fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
536                         progname);
537                 return EINVAL;
538         }
539
540         if (ldd->ldd_flags & LDD_F_VIRGIN) {
541                 rc = append_option(options, options_len, "virgin", NULL);
542                 if (rc != 0)
543                         return rc;
544         }
545         if (ldd->ldd_flags & LDD_F_UPDATE) {
546                 rc = append_option(options, options_len, "update", NULL);
547                 if (rc != 0)
548                         return rc;
549         }
550         if (ldd->ldd_flags & LDD_F_WRITECONF) {
551                 rc = append_option(options, options_len, "writeconf", NULL);
552                 if (rc != 0)
553                         return rc;
554         }
555         if (ldd->ldd_flags & LDD_F_NO_PRIMNODE) {
556                 rc = append_option(options, options_len, "noprimnode", NULL);
557                 if (rc != 0)
558                         return rc;
559         }
560
561         /* prefix every lustre parameter with param= so that in-kernel
562          * mount can recognize them properly and send to MGS at registration */
563         start = ldd->ldd_params;
564         while (start && *start != '\0') {
565                 while (*start == ' ') start++;
566                 if (*start == '\0')
567                         break;
568                 cur = start;
569                 start = strchr(cur, ' ');
570                 if (start) {
571                         *start = '\0';
572                         start++;
573                 }
574                 rc = append_option(options, options_len, "param=", cur);
575                 if (rc != 0)
576                         return rc;
577         }
578
579         /* svname must be last option */
580         rc = append_option(options, options_len, "svname=", ldd->ldd_svname);
581
582         return rc;
583 }
584 #endif /* HAVE_SERVER_SUPPORT */
585
586 static void set_defaults(struct mount_opts *mop)
587 {
588         memset(mop, 0, sizeof(*mop));
589         mop->mo_usource = NULL;
590         mop->mo_source = NULL;
591         mop->mo_nomtab = 0;
592         mop->mo_fake = 0;
593         mop->mo_force = 0;
594         mop->mo_retry = 0;
595         mop->mo_have_mgsnid = 0;
596         mop->mo_md_stripe_cache_size = 16384;
597         mop->mo_orig_options = "";
598         mop->mo_nosvc = 0;
599         mop->mo_max_sectors_kb = -1;
600 }
601
602 static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
603 {
604         static struct option long_opts[] = {
605         { .val = 1,     .name = "force",        .has_arg = no_argument },
606         { .val = 'f',   .name = "fake",         .has_arg = no_argument },
607         { .val = 'h',   .name = "help",         .has_arg = no_argument },
608         { .val = 'n',   .name = "nomtab",       .has_arg = no_argument },
609         { .val = 'o',   .name = "options",      .has_arg = required_argument },
610         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
611         { .val = 'V',   .name = "version",      .has_arg = no_argument },
612         { .name = NULL } };
613         char real_path[PATH_MAX] = {'\0'};
614         FILE *f;
615         char path[256], name[256];
616         size_t sz;
617         char *ptr;
618         int opt, rc;
619
620         while ((opt = getopt_long(argc, argv, "fhno:vV",
621                                   long_opts, NULL)) != EOF){
622                 switch (opt) {
623                 case 1:
624                         ++mop->mo_force;
625                         printf("force: %d\n", mop->mo_force);
626                         break;
627                 case 'f':
628                         ++mop->mo_fake;
629                         printf("fake: %d\n", mop->mo_fake);
630                         break;
631                 case 'h':
632                         usage(stdout);
633                         break;
634                 case 'n':
635                         ++mop->mo_nomtab;
636                         printf("nomtab: %d\n", mop->mo_nomtab);
637                         break;
638                 case 'o':
639                         mop->mo_orig_options = optarg;
640                         break;
641                 case 'v':
642                         ++verbose;
643                         break;
644                 case 'V':
645                         ++version;
646                         fprintf(stdout, "%s %s %s\n", progname,
647                                 LUSTRE_VERSION_STRING, WITH_LIBMOUNT);
648                         return 0;
649                 default:
650                         fprintf(stderr, "%s: unknown option '%c'\n",
651                                         progname, opt);
652                         usage(stderr);
653                         break;
654                 }
655         }
656
657         if (optind + 2 > argc) {
658                 fprintf(stderr, "%s: too few arguments\n", progname);
659                 usage(stderr);
660         }
661
662         mop->mo_usource = argv[optind];
663         if (!mop->mo_usource) {
664                 usage(stderr);
665         }
666
667         /**
668          * Try to get the real path to the device, in case it is a
669          * symbolic link for instance
670          */
671         if (realpath(mop->mo_usource, real_path) != NULL) {
672                 ptr = strrchr(real_path, '/');
673                 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
674                         snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
675                         if ((f = fopen(path, "r"))) {
676                                 /* read "<name>\n" from sysfs */
677                                 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
678                                         name[sz - 1] = '\0';
679                                         snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
680                                 }
681                                 fclose(f);
682                         }
683                 }
684                 mop->mo_usource = strdup(real_path);
685         }
686
687         ptr = strstr(mop->mo_usource, ":/");
688         if (ptr != NULL) {
689                 mop->mo_source = convert_hostnames(mop->mo_usource);
690                 if (!mop->mo_source)
691                         usage(stderr);
692         } else {
693                 mop->mo_source = strdup(mop->mo_usource);
694         }
695
696         if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
697                 rc = errno;
698                 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
699                                 argv[optind + 1], strerror(errno));
700                 return rc;
701         }
702
703         return 0;
704 }
705
706 #ifdef HAVE_SERVER_SUPPORT
707 /* change label from <fsname>:<index> to
708  * <fsname>-<index> to indicate the device has
709  * been registered. only if the label is
710  * supposed to be changed and target service
711  * is supposed to start */
712 static void label_lustre(struct mount_opts *mop)
713 {
714         if (mop->mo_nosvc)
715                 return;
716
717         if (mop->mo_ldd.ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF)) {
718                 (void)osd_label_lustre(mop);
719         } else {
720                 struct lustre_disk_data ldd;
721                 int rc;
722
723                 /* device label could be changed after journal recovery,
724                  * it should also be relabeled for mount has succeeded. */
725                 memset(&ldd, 0, sizeof(ldd));
726                 ldd.ldd_mount_type = mop->mo_ldd.ldd_mount_type;
727                 rc = osd_read_ldd(mop->mo_source, &ldd);
728                 if (rc == 0) {
729                         rc = strlen(ldd.ldd_svname);
730                         if (rc >= 8 && ldd.ldd_svname[rc - 8] != '-')
731                                 (void)osd_label_lustre(mop);
732                 }
733         }
734 }
735 #endif /* HAVE_SERVER_SUPPORT */
736
737 int main(int argc, char *const argv[])
738 {
739         struct mount_opts mop;
740         char *options;
741         int i, flags;
742         int rc;
743         bool client;
744         size_t maxopt_len;
745         size_t g_pagesize;
746
747         progname = strrchr(argv[0], '/');
748         progname = progname ? progname + 1 : argv[0];
749
750         set_defaults(&mop);
751
752         g_pagesize = sysconf(_SC_PAGESIZE);
753         if (g_pagesize == -1) {
754                 rc = errno;
755                 printf("error: %d failed to get page size.\n", rc);
756                 return rc;
757         }
758         maxopt_len = MIN(g_pagesize, 64 * 1024);
759
760         rc = parse_opts(argc, argv, &mop);
761         if (rc || version)
762                 return rc;
763
764         if (verbose) {
765                 for (i = 0; i < argc; i++)
766                         printf("arg[%d] = %s\n", i, argv[i]);
767                 printf("source = %s (%s), target = %s\n", mop.mo_usource,
768                        mop.mo_source, mop.mo_target);
769                 printf("options = %s\n", mop.mo_orig_options);
770         }
771
772         options = malloc(maxopt_len);
773         if (options == NULL) {
774                 fprintf(stderr, "can't allocate memory for options\n");
775                 rc = ENOMEM;
776                 goto out_mo_source;
777         }
778
779         if (strlen(mop.mo_orig_options) >= maxopt_len) {
780                 fprintf(stderr, "error: mount options too long\n");
781                 rc = E2BIG;
782                 goto out_options;
783         }
784
785         strcpy(options, mop.mo_orig_options);
786         rc = parse_options(&mop, options, &flags, maxopt_len);
787         if (rc) {
788                 fprintf(stderr, "%s: can't parse options: %s\n",
789                         progname, options);
790                 rc = EINVAL;
791                 goto out_options;
792         }
793
794         if (!mop.mo_force) {
795                 rc = check_mtab_entry(mop.mo_usource, mop.mo_source,
796                                       mop.mo_target, "lustre");
797                 if (rc && !(flags & MS_REMOUNT)) {
798                         fprintf(stderr, "%s: according to %s %s is "
799                                 "already mounted on %s\n", progname, MOUNTED,
800                                 mop.mo_usource, mop.mo_target);
801                         rc = EEXIST;
802                         goto out_options;
803                 }
804                 if (!rc && (flags & MS_REMOUNT)) {
805                         fprintf(stderr, "%s: according to %s %s is "
806                                 "not already mounted on %s\n", progname, MOUNTED,
807                                 mop.mo_usource, mop.mo_target);
808                         rc = ENOENT;
809                         goto out_options;
810                 }
811         }
812         if (flags & MS_REMOUNT)
813                 mop.mo_nomtab++;
814
815         rc = access(mop.mo_target, F_OK);
816         if (rc) {
817                 rc = errno;
818                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname,
819                         mop.mo_target, strerror(errno));
820                 goto out_options;
821         }
822
823         client = (strstr(mop.mo_usource, ":/") != NULL);
824         if (!client) {
825 #ifdef HAVE_SERVER_SUPPORT
826                 rc = osd_init();
827                 if (rc)
828                         goto out_options;
829
830                 rc = parse_ldd(mop.mo_source, &mop, options, maxopt_len);
831                 if (rc)
832                         goto out_osd;
833 #else
834                 rc = -EINVAL;
835                 goto out_options;
836 #endif
837         }
838
839         /* In Linux 2.4, the target device doesn't get passed to any of our
840            functions.  So we'll stick it on the end of the options. */
841         rc = append_option(options, maxopt_len, "device=", mop.mo_source);
842         if (rc != 0)
843                 goto out_osd;
844
845         if (verbose)
846                 printf("mounting device %s at %s, flags=%#x options=%s\n",
847                        mop.mo_source, mop.mo_target, flags, options);
848
849 #ifdef HAVE_SERVER_SUPPORT
850         if (!client && osd_tune_lustre(mop.mo_source, &mop)) {
851                 if (verbose)
852                         fprintf(stderr, "%s: unable to set tunables for %s"
853                                         " (may cause reduced IO performance)\n",
854                                         argv[0], mop.mo_source);
855         }
856 #endif
857 #ifdef HAVE_GSS
858         if (mop.mo_skpath[0] != '\0') {
859                 /* Treat shared key failures as fatal */
860                 rc = load_shared_keys(&mop);
861                 if (rc) {
862                         fprintf(stderr,
863                                 "%s: Error loading shared keys: %s\n",
864                                 progname, strerror(rc));
865                         goto out_osd;
866                 }
867         }
868 #endif /* HAVE_GSS */
869
870         if (!mop.mo_fake) {
871                 /* flags and target get to lustre_get_sb(), but not
872                  * lustre_fill_super().  Lustre ignores the flags, but mount
873                  * does not. */
874                 for (i = 0, rc = -EAGAIN; i <= mop.mo_retry && rc != 0; i++) {
875                         rc = mount(mop.mo_source, mop.mo_target, "lustre",
876                                    flags, (void *)options);
877                         if (rc != 0) {
878                                 if (verbose) {
879                                         fprintf(stderr, "%s: mount %s at %s "
880                                                 "failed: %s retries left: "
881                                                 "%d\n", basename(progname),
882                                                 mop.mo_usource, mop.mo_target,
883                                                 strerror(errno),
884                                                 mop.mo_retry - i);
885                                 }
886
887                                 if (mop.mo_retry) {
888                                         int limit = i/2 > 5 ? i/2 : 5;
889
890                                         sleep(1 << limit);
891                                 } else {
892                                         rc = errno;
893                                 }
894 #ifdef HAVE_SERVER_SUPPORT
895                         } else {
896                                 if (!client)
897                                         label_lustre(&mop);
898 #endif
899                         }
900                 }
901         }
902
903         if (rc) {
904                 char *cli;
905
906                 rc = errno;
907
908                 cli = strrchr(mop.mo_usource, ':');
909                 if (cli && (strlen(cli) > 2))
910                         cli += 2;
911                 else
912                         cli = NULL;
913
914                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
915                         mop.mo_usource, mop.mo_target, strerror(errno));
916                 if (errno == EBUSY)
917                         fprintf(stderr, "Is the backend filesystem mounted?\n"
918                                         "Check /etc/mtab and /proc/mounts\n");
919                 if (errno == ENODEV)
920                         fprintf(stderr, "Are the lustre modules loaded?\n"
921                                 "Check /etc/modprobe.conf and "
922                                 "/proc/filesystems\n");
923                 if (errno == ENOTBLK)
924                         fprintf(stderr, "Do you need -o loop?\n");
925                 if (errno == ENOMEDIUM)
926                         fprintf(stderr,
927                                 "This filesystem needs at least 1 OST\n");
928                 if (errno == ENOENT) {
929                         fprintf(stderr, "Is the MGS specification correct?\n");
930                         fprintf(stderr, "Is the filesystem name correct?\n");
931                         fprintf(stderr, "If upgrading, is the copied client log"
932                                 " valid? (see upgrade docs)\n");
933                 }
934                 if (errno == EALREADY)
935                         fprintf(stderr, "The target service is already running."
936                                 " (%s)\n", mop.mo_usource);
937                 if (errno == ENXIO)
938                         fprintf(stderr, "The target service failed to start "
939                                 "(bad config log?) (%s).  "
940                                 "See /var/log/messages.\n", mop.mo_usource);
941                 if (errno == EIO)
942                         fprintf(stderr, "Is the MGS running?\n");
943                 if (errno == EADDRINUSE)
944                         fprintf(stderr, "The target service's index is already "
945                                 "in use. (%s)\n", mop.mo_usource);
946                 if (errno == EINVAL) {
947                         fprintf(stderr, "This may have multiple causes.\n");
948                         if (cli)
949                                 fprintf(stderr, "Is '%s' the correct filesystem"
950                                         " name?\n", cli);
951                         fprintf(stderr, "Are the mount options correct?\n");
952                         fprintf(stderr, "Check the syslog for more info.\n");
953                 }
954
955                 /* May as well try to clean up loop devs */
956                 if (strncmp(mop.mo_usource, "/dev/loop", 9) == 0) {
957                         char cmd[256];
958                         int ret;
959                         sprintf(cmd, "/sbin/losetup -d %s", mop.mo_usource);
960                         if ((ret = system(cmd)) < 0)
961                                 rc = errno;
962                         else if (ret > 0)
963                                 rc = WEXITSTATUS(ret);
964                 }
965
966         } else {
967                 /* Deal with utab just for client. Note that we ignore
968                  * the return value here since it is not worth to fail
969                  * mount by prevent some rare cases */
970                 if (strstr(mop.mo_usource, ":/") != NULL)
971                         update_utab_entry(&mop);
972                 if (!mop.mo_nomtab) {
973                         rc = update_mtab_entry(mop.mo_usource, mop.mo_target,
974                                                "lustre", mop.mo_orig_options,
975                                                0, 0, 0);
976                 }
977         }
978
979 out_osd:
980 #ifdef HAVE_SERVER_SUPPORT
981         if (!client)
982                 osd_fini();
983 #endif
984 out_options:
985         free(options);
986
987 out_mo_source:
988         /* mo_usource should be freed, but we can rely on the kernel */
989         free(mop.mo_source);
990         return rc;
991 }