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