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