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