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