Whamcloud - gitweb
LU-9430 utils: fix logic errors and putchar in sk_name2hmac()
[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, 2016, 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 <lustre_ver.h>
53 #include <ctype.h>
54 #include <limits.h>
55 #include <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   /*"optname", inv,ms_mask */
176   /* These flags are parsed by mount, not lustre */
177   { "defaults", 0, 0         },      /* default options */
178   { "remount",  0, MS_REMOUNT},      /* remount with different options */
179   { "rw",       1, MS_RDONLY },      /* read-write */
180   { "ro",       0, MS_RDONLY },      /* read-only */
181   { "exec",     1, MS_NOEXEC },      /* permit execution of binaries */
182   { "noexec",   0, MS_NOEXEC },      /* don't execute binaries */
183   { "suid",     1, MS_NOSUID },      /* honor suid executables */
184   { "nosuid",   0, MS_NOSUID },      /* don't honor suid executables */
185   { "dev",      1, MS_NODEV  },      /* interpret device files  */
186   { "nodev",    0, MS_NODEV  },      /* don't interpret devices */
187   { "sync",     0, MS_SYNCHRONOUS},  /* synchronous I/O */
188   { "async",    1, MS_SYNCHRONOUS},  /* asynchronous I/O */
189   { "atime",    1, MS_NOATIME  },    /* set file access time on read */
190   { "noatime",  0, MS_NOATIME  },    /* do not set file access time on read */
191 #ifdef MS_NODIRATIME
192   { "diratime", 1, MS_NODIRATIME },  /* set file access time on read */
193   { "nodiratime",0,MS_NODIRATIME },  /* do not set file access time on read */
194 #endif
195 #ifdef MS_RELATIME
196   { "relatime", 0, MS_RELATIME },  /* set file access time on read */
197   { "norelatime",1,MS_RELATIME },  /* do not set file access time on read */
198 #endif
199 #ifdef MS_STRICTATIME
200   { "strictatime",0,MS_STRICTATIME },  /* update access time strictly */
201 #endif
202   { "auto",     0, 0         },      /* Can be mounted using -a */
203   { "noauto",   0, 0         },      /* Can only be mounted explicitly */
204   { "nousers",  1, 0         },      /* Forbid ordinary user to mount */
205   { "nouser",   1, 0         },      /* Forbid ordinary user to mount */
206   { "noowner",  1, 0         },      /* Device owner has no special privs */
207   { "_netdev",  0, 0         },      /* Device accessible only via network */
208   { "loop",     0, 0         },
209   { NULL,       0, 0         }
210 };
211 /****************************************************************************/
212
213 /* 1  = don't pass on to lustre
214    0  = pass on to lustre */
215 static int parse_one_option(const char *check, int *flagp)
216 {
217         const struct opt_map *opt;
218
219         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
220                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
221                         if (opt->mask) {
222                                 if (opt->inv)
223                                         *flagp &= ~(opt->mask);
224                                 else
225                                         *flagp |= opt->mask;
226                         }
227                         return 1;
228                 }
229         }
230         /* Assume any unknown options are valid and pass them on.  The mount
231            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
232         return 0;
233 }
234
235 static int append_option(char *options, size_t options_len,
236                          const char *param, const char *value)
237 {
238         int rc;
239         if (options[0] != '\0') {
240                 rc = strlcat(options, ",", options_len);
241                 if (rc >= options_len)
242                         goto out_err;
243         }
244         rc = strlcat(options, param, options_len);
245         if (rc >= options_len)
246                 goto out_err;
247         if (value != NULL) {
248                 rc = strlcat(options, value, options_len);
249                 if (rc >= options_len)
250                         goto out_err;
251         }
252         return 0;
253 out_err:
254         fprintf(stderr, "error: mount options %s%s too long\n", param, value);
255         return E2BIG;
256 }
257
258 /* Replace options with subset of Lustre-specific options, and
259    fill in mount flags */
260 int parse_options(struct mount_opts *mop, char *orig_options,
261                   int *flagp, size_t options_len)
262 {
263         char *options, *opt, *nextopt, *arg, *val;
264         int rc = 0;
265
266         options = calloc(strlen(orig_options) + 1, 1);
267         *flagp = 0;
268         nextopt = orig_options;
269         while ((opt = strsep(&nextopt, ","))) {
270                 if (!*opt)
271                         /* empty option */
272                         continue;
273
274                 /* Handle retries in a slightly different
275                  * manner */
276                 arg = opt;
277                 val = strchr(opt, '=');
278                 /* please note that some ldiskfs mount options are also in
279                  * the form of param=value. We should pay attention not to
280                  * remove those mount options, see bug 22097. */
281                 if (val && strncmp(arg, "max_sectors_kb", 14) == 0) {
282                         mop->mo_max_sectors_kb = atoi(val + 1);
283                 } else if (val &&
284                            strncmp(arg, "md_stripe_cache_size", 20) == 0) {
285                         mop->mo_md_stripe_cache_size = atoi(val + 1);
286                 } else if (val && strncmp(arg, "retry", 5) == 0) {
287                         mop->mo_retry = atoi(val + 1);
288                         if (mop->mo_retry > MAX_RETRIES)
289                                 mop->mo_retry = MAX_RETRIES;
290                         else if (mop->mo_retry < 0)
291                                 mop->mo_retry = 0;
292                 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
293                         rc = append_option(options, options_len, opt, NULL);
294                         if (rc != 0)
295                                 goto out_options;
296                 } else if (strncmp(arg, "nosvc", 5) == 0) {
297                         mop->mo_nosvc = 1;
298                         rc = append_option(options, options_len, opt, NULL);
299                         if (rc != 0)
300                                 goto out_options;
301                 } else if (strcmp(opt, "force") == 0) {
302                         /* XXX special check for 'force' option */
303                         ++mop->mo_force;
304                         printf("force: %d\n", mop->mo_force);
305 #ifdef HAVE_GSS
306                 } else if (val && strncmp(opt, "skpath=", 7) == 0) {
307                         if (strlen(val) + 1 >= sizeof(mop->mo_skpath)) {
308                                 fprintf(stderr,
309                                         "%s: shared key path too long\n",
310                                         progname);
311                                 free(options);
312                                 return -1;
313                         }
314                         strncpy(mop->mo_skpath, val + 1, strlen(val + 1));
315 #endif
316                 } else if (parse_one_option(opt, flagp) == 0) {
317                         /* pass this on as an option */
318                         rc = append_option(options, options_len, opt, NULL);
319                         if (rc != 0)
320                                 goto out_options;
321                 }
322         }
323 #ifdef MS_STRICTATIME
324 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 2, 53, 0)
325         /*
326          * LU-1783
327          * In the future when upstream fixes land in all supported kernels
328          * we should stop forcing MS_STRICTATIME in lustre mounts.
329          * We override the kernel level default of MS_RELATIME for now
330          * due to a kernel vfs level bug in atime updates that fails
331          * to reset timestamps from the future.
332          */
333 #warn "remove MS_STRICTATIME override if kernel updates atime from the future"
334 #endif
335         /* set strictatime to default if NOATIME or RELATIME
336            not given explicit */
337         if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
338                 *flagp |= MS_STRICTATIME;
339 #endif
340         strcpy(orig_options, options);
341
342 out_options:
343         free(options);
344         return rc;
345 }
346
347 /* Add mgsnids from ldd params */
348 static int add_mgsnids(struct mount_opts *mop, char *options,
349                        const char *params, size_t options_len)
350 {
351         char *ptr = (char *)params;
352         char tmp, *sep;
353         int rc = 0;
354
355         while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
356                 sep = strchr(ptr, ' ');
357                 if (sep != NULL) {
358                         tmp = *sep;
359                         *sep = '\0';
360                 }
361                 rc = append_option(options, options_len, ptr, NULL);
362                 if (rc != 0)
363                         goto out;
364                 mop->mo_have_mgsnid++;
365                 if (sep) {
366                         *sep = tmp;
367                         ptr = sep;
368                 } else {
369                         break;
370                 }
371         }
372
373 out:
374         return rc;
375 }
376
377 static int clear_update_ondisk(char *source, struct lustre_disk_data *ldd)
378 {
379         char wanted_mountopts[512] = "";
380         struct mkfs_opts mkop;
381         int ret;
382         int ret2;
383
384         memset(&mkop, 0, sizeof(mkop));
385         mkop.mo_ldd = *ldd;
386         mkop.mo_ldd.ldd_flags &= ~LDD_F_UPDATE;
387         mkop.mo_flags = MO_NOHOSTID_CHECK; /* Ignore missing hostid */
388         if (strlen(source) > sizeof(mkop.mo_device)-1) {
389                 fatal();
390                 fprintf(stderr, "Device name too long: %s\n", source);
391                 return -E2BIG;
392         }
393         strncpy(mkop.mo_device, source, sizeof(mkop.mo_device));
394
395         ret = osd_prepare_lustre(&mkop,
396                                  wanted_mountopts, sizeof(wanted_mountopts));
397         if (ret) {
398                 fatal();
399                 fprintf(stderr, "Can't prepare device %s: %s\n",
400                         source, strerror(ret));
401                 return ret;
402         }
403
404         /* Create the loopback file */
405         if (mkop.mo_flags & MO_IS_LOOP) {
406                 ret = access(mkop.mo_device, F_OK);
407                 if (ret) {
408                         ret = errno;
409                         fatal();
410                         fprintf(stderr, "Can't access device %s: %s\n",
411                                         source, strerror(ret));
412                         return ret;
413                 }
414
415                 ret = loop_setup(&mkop);
416                 if (ret) {
417                         fatal();
418                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
419                                         mkop.mo_device, strerror(ret));
420                         return ret;
421                 }
422         }
423         ret = osd_write_ldd(&mkop);
424         if (ret != 0) {
425                 fatal();
426                 fprintf(stderr, "failed to write local files: %s\n",
427                         strerror(ret));
428         }
429
430         ret2 = loop_cleanup(&mkop);
431         if (ret == 0)
432                 ret = ret2;
433
434         return ret;
435 }
436
437 static int parse_ldd(char *source, struct mount_opts *mop,
438                      char *options, size_t options_len)
439 {
440         struct lustre_disk_data *ldd = &mop->mo_ldd;
441         char *cur, *start;
442         int rc = 0;
443
444         rc = osd_is_lustre(source, &ldd->ldd_mount_type);
445         if (rc == 0) {
446                 fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
447                         " or the backend filesystem type is not supported by "
448                         "this tool\n", progname, source);
449                 return ENODEV;
450         }
451
452         rc = osd_read_ldd(source, ldd);
453         if (rc) {
454                 fprintf(stderr, "%s: %s failed to read permanent mount"
455                         " data: %s\n", progname, source,
456                         rc >= 0 ? strerror(rc) : "");
457                 return rc;
458         }
459
460         if ((IS_MDT(ldd) || IS_OST(ldd)) &&
461             (ldd->ldd_flags & LDD_F_NEED_INDEX)) {
462                 fprintf(stderr, "%s: %s has no index assigned "
463                         "(probably formatted with old mkfs)\n",
464                         progname, source);
465                 return EINVAL;
466         }
467
468         if (ldd->ldd_flags & LDD_F_UPGRADE14) {
469                 fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
470                         "Lustre version\n", progname, source);
471                 return EINVAL;
472         }
473
474         if (ldd->ldd_flags & LDD_F_UPDATE)
475                 clear_update_ondisk(source, ldd);
476
477         /* Since we never rewrite ldd, ignore temp flags */
478         ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_WRITECONF);
479
480         /* svname of the form lustre:OST1234 means never registered */
481         rc = strlen(ldd->ldd_svname);
482         if (strcmp(ldd->ldd_svname, "MGS") != 0) {
483                 if (rc < 8) {
484                         fprintf(stderr, "%s: invalid name '%s'\n",
485                                 progname, ldd->ldd_svname);
486                         return EINVAL;
487                 } else if (ldd->ldd_svname[rc - 8] == ':') {
488                         ldd->ldd_svname[rc - 8] = '-';
489                         ldd->ldd_flags |= LDD_F_VIRGIN;
490                 } else if (ldd->ldd_svname[rc - 8] == '=') {
491                         ldd->ldd_svname[rc - 8] = '-';
492                         ldd->ldd_flags |= LDD_F_WRITECONF;
493                 }
494         }
495         /* backend osd type */
496         rc = append_option(options, options_len, "osd=",
497                            mt_type(ldd->ldd_mount_type));
498         if (rc != 0)
499                 return rc;
500
501         rc = append_option(options, options_len, ldd->ldd_mount_opts, NULL);
502         if (rc != 0)
503                 return rc;
504
505         if (!mop->mo_have_mgsnid) {
506                 /* Only use disk data if mount -o mgsnode=nid wasn't
507                  * specified */
508                 if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
509                         rc = append_option(options, options_len, "mgs", NULL);
510                         if (rc != 0)
511                                 return rc;
512                         mop->mo_have_mgsnid++;
513                 } else {
514                         if (add_mgsnids(mop, options, ldd->ldd_params,
515                                         options_len))
516                                 return E2BIG;
517                 }
518         }
519         /* Better have an mgsnid by now */
520         if (!mop->mo_have_mgsnid) {
521                 fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
522                         progname);
523                 return EINVAL;
524         }
525
526         if (ldd->ldd_flags & LDD_F_VIRGIN) {
527                 rc = append_option(options, options_len, "virgin", NULL);
528                 if (rc != 0)
529                         return rc;
530         }
531         if (ldd->ldd_flags & LDD_F_UPDATE) {
532                 rc = append_option(options, options_len, "update", NULL);
533                 if (rc != 0)
534                         return rc;
535         }
536         if (ldd->ldd_flags & LDD_F_WRITECONF) {
537                 rc = append_option(options, options_len, "writeconf", NULL);
538                 if (rc != 0)
539                         return rc;
540         }
541         if (ldd->ldd_flags & LDD_F_NO_PRIMNODE) {
542                 rc = append_option(options, options_len, "noprimnode", NULL);
543                 if (rc != 0)
544                         return rc;
545         }
546
547         /* prefix every lustre parameter with param= so that in-kernel
548          * mount can recognize them properly and send to MGS at registration */
549         start = ldd->ldd_params;
550         while (start && *start != '\0') {
551                 while (*start == ' ') start++;
552                 if (*start == '\0')
553                         break;
554                 cur = start;
555                 start = strchr(cur, ' ');
556                 if (start) {
557                         *start = '\0';
558                         start++;
559                 }
560                 rc = append_option(options, options_len, "param=", cur);
561                 if (rc != 0)
562                         return rc;
563         }
564
565         /* svname must be last option */
566         rc = append_option(options, options_len, "svname=", ldd->ldd_svname);
567
568         return rc;
569 }
570
571 static void set_defaults(struct mount_opts *mop)
572 {
573         memset(mop, 0, sizeof(*mop));
574         mop->mo_usource = NULL;
575         mop->mo_source = NULL;
576         mop->mo_nomtab = 0;
577         mop->mo_fake = 0;
578         mop->mo_force = 0;
579         mop->mo_retry = 0;
580         mop->mo_have_mgsnid = 0;
581         mop->mo_md_stripe_cache_size = 16384;
582         mop->mo_orig_options = "";
583         mop->mo_nosvc = 0;
584         mop->mo_max_sectors_kb = -1;
585 }
586
587 static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
588 {
589         static struct option long_opt[] = {
590                 {"fake", 0, 0, 'f'},
591                 {"force", 0, 0, 1},
592                 {"help", 0, 0, 'h'},
593                 {"nomtab", 0, 0, 'n'},
594                 {"options", 1, 0, 'o'},
595                 {"verbose", 0, 0, 'v'},
596                 {"version", 0, 0, 'V'},
597                 {0, 0, 0, 0}
598         };
599         char real_path[PATH_MAX] = {'\0'};
600         FILE *f;
601         char path[256], name[256];
602         size_t sz;
603         char *ptr;
604         int opt, rc;
605
606         while ((opt = getopt_long(argc, argv, "fhno:vV",
607                                   long_opt, NULL)) != EOF){
608                 switch (opt) {
609                 case 1:
610                         ++mop->mo_force;
611                         printf("force: %d\n", mop->mo_force);
612                         break;
613                 case 'f':
614                         ++mop->mo_fake;
615                         printf("fake: %d\n", mop->mo_fake);
616                         break;
617                 case 'h':
618                         usage(stdout);
619                         break;
620                 case 'n':
621                         ++mop->mo_nomtab;
622                         printf("nomtab: %d\n", mop->mo_nomtab);
623                         break;
624                 case 'o':
625                         mop->mo_orig_options = optarg;
626                         break;
627                 case 'v':
628                         ++verbose;
629                         break;
630                 case 'V':
631                         ++version;
632                         fprintf(stdout, "%s %s %s\n", progname,
633                                 LUSTRE_VERSION_STRING, WITH_LIBMOUNT);
634                         return 0;
635                 default:
636                         fprintf(stderr, "%s: unknown option '%c'\n",
637                                         progname, opt);
638                         usage(stderr);
639                         break;
640                 }
641         }
642
643         if (optind + 2 > argc) {
644                 fprintf(stderr, "%s: too few arguments\n", progname);
645                 usage(stderr);
646         }
647
648         mop->mo_usource = argv[optind];
649         if (!mop->mo_usource) {
650                 usage(stderr);
651         }
652
653         /**
654          * Try to get the real path to the device, in case it is a
655          * symbolic link for instance
656          */
657         if (realpath(mop->mo_usource, real_path) != NULL) {
658                 ptr = strrchr(real_path, '/');
659                 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
660                         snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
661                         if ((f = fopen(path, "r"))) {
662                                 /* read "<name>\n" from sysfs */
663                                 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
664                                         name[sz - 1] = '\0';
665                                         snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
666                                 }
667                                 fclose(f);
668                         }
669                 }
670                 mop->mo_usource = strdup(real_path);
671         }
672
673         ptr = strstr(mop->mo_usource, ":/");
674         if (ptr != NULL) {
675                 mop->mo_source = convert_hostnames(mop->mo_usource);
676                 if (!mop->mo_source)
677                         usage(stderr);
678         } else {
679                 mop->mo_source = strdup(mop->mo_usource);
680         }
681
682         if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
683                 rc = errno;
684                 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
685                                 argv[optind + 1], strerror(errno));
686                 return rc;
687         }
688
689         return 0;
690 }
691
692 /* change label from <fsname>:<index> to
693  * <fsname>-<index> to indicate the device has
694  * been registered. only if the label is
695  * supposed to be changed and target service
696  * is supposed to start */
697 static void label_lustre(struct mount_opts *mop)
698 {
699         if (mop->mo_nosvc)
700                 return;
701
702         if (mop->mo_ldd.ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF)) {
703                 (void)osd_label_lustre(mop);
704         } else {
705                 struct lustre_disk_data ldd;
706                 int rc;
707
708                 /* device label could be changed after journal recovery,
709                  * it should also be relabeled for mount has succeeded. */
710                 memset(&ldd, 0, sizeof(ldd));
711                 rc = osd_read_ldd(mop->mo_source, &ldd);
712                 if (rc == 0) {
713                         rc = strlen(ldd.ldd_svname);
714                         if (rc >= 8 && ldd.ldd_svname[rc - 8] != '-')
715                                 (void)osd_label_lustre(mop);
716                 }
717         }
718 }
719
720 int main(int argc, char *const argv[])
721 {
722         struct mount_opts mop;
723         char *options;
724         int i, flags;
725         int rc;
726         bool client;
727         size_t maxopt_len;
728         size_t g_pagesize;
729
730         progname = strrchr(argv[0], '/');
731         progname = progname ? progname + 1 : argv[0];
732
733         set_defaults(&mop);
734
735         g_pagesize = sysconf(_SC_PAGESIZE);
736         if (g_pagesize == -1) {
737                 rc = errno;
738                 printf("error: %d failed to get page size.\n", rc);
739                 return rc;
740         }
741         maxopt_len = MIN(g_pagesize, 64 * 1024);
742
743         rc = parse_opts(argc, argv, &mop);
744         if (rc || version)
745                 return rc;
746
747         if (verbose) {
748                 for (i = 0; i < argc; i++)
749                         printf("arg[%d] = %s\n", i, argv[i]);
750                 printf("source = %s (%s), target = %s\n", mop.mo_usource,
751                        mop.mo_source, mop.mo_target);
752                 printf("options = %s\n", mop.mo_orig_options);
753         }
754
755         options = malloc(maxopt_len);
756         if (options == NULL) {
757                 fprintf(stderr, "can't allocate memory for options\n");
758                 rc = ENOMEM;
759                 goto out_mo_source;
760         }
761
762         if (strlen(mop.mo_orig_options) >= maxopt_len) {
763                 fprintf(stderr, "error: mount options too long\n");
764                 rc = E2BIG;
765                 goto out_options;
766         }
767
768         strcpy(options, mop.mo_orig_options);
769         rc = parse_options(&mop, options, &flags, maxopt_len);
770         if (rc) {
771                 fprintf(stderr, "%s: can't parse options: %s\n",
772                         progname, options);
773                 rc = EINVAL;
774                 goto out_options;
775         }
776
777         if (!mop.mo_force) {
778                 rc = check_mtab_entry(mop.mo_usource, mop.mo_source,
779                                       mop.mo_target, "lustre");
780                 if (rc && !(flags & MS_REMOUNT)) {
781                         fprintf(stderr, "%s: according to %s %s is "
782                                 "already mounted on %s\n", progname, MOUNTED,
783                                 mop.mo_usource, mop.mo_target);
784                         rc = EEXIST;
785                         goto out_options;
786                 }
787                 if (!rc && (flags & MS_REMOUNT)) {
788                         fprintf(stderr, "%s: according to %s %s is "
789                                 "not already mounted on %s\n", progname, MOUNTED,
790                                 mop.mo_usource, mop.mo_target);
791                         rc = ENOENT;
792                         goto out_options;
793                 }
794         }
795         if (flags & MS_REMOUNT)
796                 mop.mo_nomtab++;
797
798         rc = access(mop.mo_target, F_OK);
799         if (rc) {
800                 rc = errno;
801                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname,
802                         mop.mo_target, strerror(errno));
803                 goto out_options;
804         }
805
806         client = (strstr(mop.mo_usource, ":/") != NULL);
807         if (!client) {
808                 rc = osd_init();
809                 if (rc)
810                         goto out_options;
811
812                 rc = parse_ldd(mop.mo_source, &mop, options, maxopt_len);
813                 if (rc)
814                         goto out_osd;
815         }
816
817         /* In Linux 2.4, the target device doesn't get passed to any of our
818            functions.  So we'll stick it on the end of the options. */
819         rc = append_option(options, maxopt_len, "device=", mop.mo_source);
820         if (rc != 0)
821                 goto out_osd;
822
823         if (verbose)
824                 printf("mounting device %s at %s, flags=%#x options=%s\n",
825                        mop.mo_source, mop.mo_target, flags, options);
826
827         if (!client && osd_tune_lustre(mop.mo_source, &mop)) {
828                 if (verbose)
829                         fprintf(stderr, "%s: unable to set tunables for %s"
830                                         " (may cause reduced IO performance)\n",
831                                         argv[0], mop.mo_source);
832         }
833
834 #ifdef HAVE_GSS
835         if (mop.mo_skpath[0] != '\0') {
836                 /* Treat shared key failures as fatal */
837                 rc = load_shared_keys(&mop);
838                 if (rc) {
839                         fprintf(stderr,
840                                 "%s: Error loading shared keys: %s\n",
841                                 progname, strerror(rc));
842                         goto out_osd;
843                 }
844         }
845 #endif /* HAVE_GSS */
846
847         if (!mop.mo_fake) {
848                 /* flags and target get to lustre_get_sb(), but not
849                  * lustre_fill_super().  Lustre ignores the flags, but mount
850                  * does not. */
851                 for (i = 0, rc = -EAGAIN; i <= mop.mo_retry && rc != 0; i++) {
852                         rc = mount(mop.mo_source, mop.mo_target, "lustre",
853                                    flags, (void *)options);
854                         if (rc == 0) {
855                                 if (!client)
856                                         label_lustre(&mop);
857                         } else {
858                                 if (verbose) {
859                                         fprintf(stderr, "%s: mount %s at %s "
860                                                 "failed: %s retries left: "
861                                                 "%d\n", basename(progname),
862                                                 mop.mo_usource, mop.mo_target,
863                                                 strerror(errno),
864                                                 mop.mo_retry - i);
865                                 }
866
867                                 if (mop.mo_retry) {
868                                         int limit = i/2 > 5 ? i/2 : 5;
869
870                                         sleep(1 << limit);
871                                 } else {
872                                         rc = errno;
873                                 }
874                         }
875                 }
876         }
877
878         if (rc) {
879                 char *cli;
880
881                 rc = errno;
882
883                 cli = strrchr(mop.mo_usource, ':');
884                 if (cli && (strlen(cli) > 2))
885                         cli += 2;
886                 else
887                         cli = NULL;
888
889                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
890                         mop.mo_usource, mop.mo_target, strerror(errno));
891                 if (errno == EBUSY)
892                         fprintf(stderr, "Is the backend filesystem mounted?\n"
893                                         "Check /etc/mtab and /proc/mounts\n");
894                 if (errno == ENODEV)
895                         fprintf(stderr, "Are the lustre modules loaded?\n"
896                                 "Check /etc/modprobe.conf and "
897                                 "/proc/filesystems\n");
898                 if (errno == ENOTBLK)
899                         fprintf(stderr, "Do you need -o loop?\n");
900                 if (errno == ENOMEDIUM)
901                         fprintf(stderr,
902                                 "This filesystem needs at least 1 OST\n");
903                 if (errno == ENOENT) {
904                         fprintf(stderr, "Is the MGS specification correct?\n");
905                         fprintf(stderr, "Is the filesystem name correct?\n");
906                         fprintf(stderr, "If upgrading, is the copied client log"
907                                 " valid? (see upgrade docs)\n");
908                 }
909                 if (errno == EALREADY)
910                         fprintf(stderr, "The target service is already running."
911                                 " (%s)\n", mop.mo_usource);
912                 if (errno == ENXIO)
913                         fprintf(stderr, "The target service failed to start "
914                                 "(bad config log?) (%s).  "
915                                 "See /var/log/messages.\n", mop.mo_usource);
916                 if (errno == EIO)
917                         fprintf(stderr, "Is the MGS running?\n");
918                 if (errno == EADDRINUSE)
919                         fprintf(stderr, "The target service's index is already "
920                                 "in use. (%s)\n", mop.mo_usource);
921                 if (errno == EINVAL) {
922                         fprintf(stderr, "This may have multiple causes.\n");
923                         if (cli)
924                                 fprintf(stderr, "Is '%s' the correct filesystem"
925                                         " name?\n", cli);
926                         fprintf(stderr, "Are the mount options correct?\n");
927                         fprintf(stderr, "Check the syslog for more info.\n");
928                 }
929
930                 /* May as well try to clean up loop devs */
931                 if (strncmp(mop.mo_usource, "/dev/loop", 9) == 0) {
932                         char cmd[256];
933                         int ret;
934                         sprintf(cmd, "/sbin/losetup -d %s", mop.mo_usource);
935                         if ((ret = system(cmd)) < 0)
936                                 rc = errno;
937                         else if (ret > 0)
938                                 rc = WEXITSTATUS(ret);
939                 }
940
941         } else {
942                 /* Deal with utab just for client. Note that we ignore
943                  * the return value here since it is not worth to fail
944                  * mount by prevent some rare cases */
945                 if (strstr(mop.mo_usource, ":/") != NULL)
946                         update_utab_entry(&mop);
947                 if (!mop.mo_nomtab) {
948                         rc = update_mtab_entry(mop.mo_usource, mop.mo_target,
949                                                "lustre", mop.mo_orig_options,
950                                                0, 0, 0);
951                 }
952         }
953
954 out_osd:
955         if (!client)
956                 osd_fini();
957
958 out_options:
959         free(options);
960
961 out_mo_source:
962         /* mo_usource should be freed, but we can rely on the kernel */
963         free(mop.mo_source);
964         return rc;
965 }