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