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