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