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