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