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