Whamcloud - gitweb
LU-7134 utils: Ensure hostid set for ZFS during mkfs
[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                 "\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 void append_option(char *options, const char *one)
236 {
237         if (*options)
238                 strcat(options, ",");
239         strcat(options, one);
240 }
241
242 /* Replace options with subset of Lustre-specific options, and
243    fill in mount flags */
244 int parse_options(struct mount_opts *mop, char *orig_options, int *flagp)
245 {
246         char *options, *opt, *nextopt, *arg, *val;
247
248         options = calloc(strlen(orig_options) + 1, 1);
249         *flagp = 0;
250         nextopt = orig_options;
251         while ((opt = strsep(&nextopt, ","))) {
252                 if (!*opt)
253                         /* empty option */
254                         continue;
255
256                 /* Handle retries in a slightly different
257                  * manner */
258                 arg = opt;
259                 val = strchr(opt, '=');
260                 /* please note that some ldiskfs mount options are also in the form
261                  * of param=value. We should pay attention not to remove those
262                  * mount options, see bug 22097. */
263                 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
264                         mop->mo_md_stripe_cache_size = atoi(val + 1);
265                 } else if (val && strncmp(arg, "retry", 5) == 0) {
266                         mop->mo_retry = atoi(val + 1);
267                         if (mop->mo_retry > MAX_RETRIES)
268                                 mop->mo_retry = MAX_RETRIES;
269                         else if (mop->mo_retry < 0)
270                                 mop->mo_retry = 0;
271                 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
272                         append_option(options, opt);
273                 } else if (strncmp(arg, "nosvc", 5) == 0) {
274                         mop->mo_nosvc = 1;
275                         append_option(options, opt);
276                 } else if (strcmp(opt, "force") == 0) {
277                         /* XXX special check for 'force' option */
278                         ++mop->mo_force;
279                         printf("force: %d\n", mop->mo_force);
280                 } else if (parse_one_option(opt, flagp) == 0) {
281                         /* pass this on as an option */
282                         append_option(options, opt);
283                 }
284         }
285 #ifdef MS_STRICTATIME
286 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 2, 53, 0)
287         /*
288          * LU-1783
289          * In the future when upstream fixes land in all supported kernels
290          * we should stop forcing MS_STRICTATIME in lustre mounts.
291          * We override the kernel level default of MS_RELATIME for now
292          * due to a kernel vfs level bug in atime updates that fails
293          * to reset timestamps from the future.
294          */
295 #warn "remove MS_STRICTATIME override if kernel updates atime from the future"
296 #endif
297         /* set strictatime to default if NOATIME or RELATIME
298            not given explicit */
299         if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
300                 *flagp |= MS_STRICTATIME;
301 #endif
302         strcpy(orig_options, options);
303         free(options);
304
305         return 0;
306 }
307
308 /* Add mgsnids from ldd params */
309 static int add_mgsnids(struct mount_opts *mop, char *options,
310                        const char *params)
311 {
312         char *ptr = (char *)params;
313         char tmp, *sep;
314
315         while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
316                 sep = strchr(ptr, ' ');
317                 if (sep != NULL) {
318                         tmp = *sep;
319                         *sep = '\0';
320                 }
321                 append_option(options, ptr);
322                 mop->mo_have_mgsnid++;
323                 if (sep) {
324                         *sep = tmp;
325                         ptr = sep;
326                 } else {
327                         break;
328                 }
329         }
330
331         return 0;
332 }
333
334 static int clear_update_ondisk(char *source, struct lustre_disk_data *ldd)
335 {
336         char wanted_mountopts[512] = "";
337         struct mkfs_opts mkop;
338         int ret;
339         int ret2;
340
341         memset(&mkop, 0, sizeof(mkop));
342         mkop.mo_ldd = *ldd;
343         mkop.mo_ldd.ldd_flags &= ~LDD_F_UPDATE;
344         mkop.mo_flags = MO_NOHOSTID_CHECK; /* Ignore missing hostid */
345         if (strlen(source) > sizeof(mkop.mo_device)-1) {
346                 fatal();
347                 fprintf(stderr, "Device name too long: %s\n", source);
348                 return -E2BIG;
349         }
350         strncpy(mkop.mo_device, source, sizeof(mkop.mo_device));
351
352         ret = osd_prepare_lustre(&mkop,
353                                  wanted_mountopts, sizeof(wanted_mountopts));
354         if (ret) {
355                 fatal();
356                 fprintf(stderr, "Can't prepare device %s: %s\n",
357                         source, strerror(ret));
358                 return ret;
359         }
360
361         /* Create the loopback file */
362         if (mkop.mo_flags & MO_IS_LOOP) {
363                 ret = access(mkop.mo_device, F_OK);
364                 if (ret) {
365                         ret = errno;
366                         fatal();
367                         fprintf(stderr, "Can't access device %s: %s\n",
368                                         source, strerror(ret));
369                         return ret;
370                 }
371
372                 ret = loop_setup(&mkop);
373                 if (ret) {
374                         fatal();
375                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
376                                         mkop.mo_device, strerror(ret));
377                         return ret;
378                 }
379         }
380         ret = osd_write_ldd(&mkop);
381         if (ret != 0) {
382                 fatal();
383                 fprintf(stderr, "failed to write local files: %s\n",
384                         strerror(ret));
385         }
386
387         ret2 = loop_cleanup(&mkop);
388         if (ret == 0)
389                 ret = ret2;
390
391         return ret;
392 }
393
394 static int parse_ldd(char *source, struct mount_opts *mop, char *options)
395 {
396         struct lustre_disk_data *ldd = &mop->mo_ldd;
397         char *cur, *start;
398         int rc;
399
400         rc = osd_is_lustre(source, &ldd->ldd_mount_type);
401         if (rc == 0) {
402                 fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
403                         " or the backend filesystem type is not supported by "
404                         "this tool\n", progname, source);
405                 return ENODEV;
406         }
407
408         rc = osd_read_ldd(source, ldd);
409         if (rc) {
410                 fprintf(stderr, "%s: %s failed to read permanent mount"
411                         " data: %s\n", progname, source,
412                         rc >= 0 ? strerror(rc) : "");
413                 return rc;
414         }
415
416         if ((IS_MDT(ldd) || IS_OST(ldd)) &&
417             (ldd->ldd_flags & LDD_F_NEED_INDEX)) {
418                 fprintf(stderr, "%s: %s has no index assigned "
419                         "(probably formatted with old mkfs)\n",
420                         progname, source);
421                 return EINVAL;
422         }
423
424         if (ldd->ldd_flags & LDD_F_UPGRADE14) {
425                 fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
426                         "Lustre version\n", progname, source);
427                 return EINVAL;
428         }
429
430         if (ldd->ldd_flags & LDD_F_UPDATE)
431                 clear_update_ondisk(source, ldd);
432
433         /* Since we never rewrite ldd, ignore temp flags */
434         ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_WRITECONF);
435
436         /* svname of the form lustre:OST1234 means never registered */
437         rc = strlen(ldd->ldd_svname);
438         if (strcmp(ldd->ldd_svname, "MGS") != 0) {
439                 if (rc < 8) {
440                         fprintf(stderr, "%s: invalid name '%s'\n",
441                                 progname, ldd->ldd_svname);
442                         return EINVAL;
443                 } else if (ldd->ldd_svname[rc - 8] == ':') {
444                         ldd->ldd_svname[rc - 8] = '-';
445                         ldd->ldd_flags |= LDD_F_VIRGIN;
446                 } else if (ldd->ldd_svname[rc - 8] == '=') {
447                         ldd->ldd_svname[rc - 8] = '-';
448                         ldd->ldd_flags |= LDD_F_WRITECONF;
449                 }
450         }
451         /* backend osd type */
452         append_option(options, "osd=");
453         strcat(options, mt_type(ldd->ldd_mount_type));
454
455         append_option(options, ldd->ldd_mount_opts);
456
457         if (!mop->mo_have_mgsnid) {
458                 /* Only use disk data if mount -o mgsnode=nid wasn't
459                  * specified */
460                 if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
461                         append_option(options, "mgs");
462                         mop->mo_have_mgsnid++;
463                 } else {
464                         add_mgsnids(mop, options, ldd->ldd_params);
465                 }
466         }
467         /* Better have an mgsnid by now */
468         if (!mop->mo_have_mgsnid) {
469                 fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
470                         progname);
471                 return EINVAL;
472         }
473
474         if (ldd->ldd_flags & LDD_F_VIRGIN)
475                 append_option(options, "virgin");
476         if (ldd->ldd_flags & LDD_F_UPDATE)
477                 append_option(options, "update");
478         if (ldd->ldd_flags & LDD_F_WRITECONF)
479                 append_option(options, "writeconf");
480         if (ldd->ldd_flags & LDD_F_NO_PRIMNODE)
481                 append_option(options, "noprimnode");
482
483         /* prefix every lustre parameter with param= so that in-kernel
484          * mount can recognize them properly and send to MGS at registration */
485         start = ldd->ldd_params;
486         while (start && *start != '\0') {
487                 while (*start == ' ') start++;
488                 if (*start == '\0')
489                         break;
490                 cur = start;
491                 start = strchr(cur, ' ');
492                 if (start) {
493                         *start = '\0';
494                         start++;
495                 }
496                 append_option(options, "param=");
497                 strcat(options, cur);
498         }
499
500         /* svname must be last option */
501         append_option(options, "svname=");
502         strcat(options, ldd->ldd_svname);
503
504         return 0;
505 }
506
507 static void set_defaults(struct mount_opts *mop)
508 {
509         memset(mop, 0, sizeof(*mop));
510         mop->mo_usource = NULL;
511         mop->mo_source = NULL;
512         mop->mo_nomtab = 0;
513         mop->mo_fake = 0;
514         mop->mo_force = 0;
515         mop->mo_retry = 0;
516         mop->mo_have_mgsnid = 0;
517         mop->mo_md_stripe_cache_size = 16384;
518         mop->mo_orig_options = "";
519         mop->mo_nosvc = 0;
520 }
521
522 static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
523 {
524         static struct option long_opt[] = {
525                 {"fake", 0, 0, 'f'},
526                 {"force", 0, 0, 1},
527                 {"help", 0, 0, 'h'},
528                 {"nomtab", 0, 0, 'n'},
529                 {"options", 1, 0, 'o'},
530                 {"verbose", 0, 0, 'v'},
531                 {"version", 0, 0, 'V'},
532                 {0, 0, 0, 0}
533         };
534         char real_path[PATH_MAX] = {'\0'};
535         FILE *f;
536         char path[256], name[256];
537         size_t sz;
538         char *ptr;
539         int opt, rc;
540
541         while ((opt = getopt_long(argc, argv, "fhno:vV",
542                                   long_opt, NULL)) != EOF){
543                 switch (opt) {
544                 case 1:
545                         ++mop->mo_force;
546                         printf("force: %d\n", mop->mo_force);
547                         break;
548                 case 'f':
549                         ++mop->mo_fake;
550                         printf("fake: %d\n", mop->mo_fake);
551                         break;
552                 case 'h':
553                         usage(stdout);
554                         break;
555                 case 'n':
556                         ++mop->mo_nomtab;
557                         printf("nomtab: %d\n", mop->mo_nomtab);
558                         break;
559                 case 'o':
560                         mop->mo_orig_options = optarg;
561                         break;
562                 case 'v':
563                         ++verbose;
564                         break;
565                 case 'V':
566                         ++version;
567                         fprintf(stdout, "%s %s %s\n", progname,
568                                 LUSTRE_VERSION_STRING, WITH_LIBMOUNT);
569                         return 0;
570                 default:
571                         fprintf(stderr, "%s: unknown option '%c'\n",
572                                         progname, opt);
573                         usage(stderr);
574                         break;
575                 }
576         }
577
578         if (optind + 2 > argc) {
579                 fprintf(stderr, "%s: too few arguments\n", progname);
580                 usage(stderr);
581         }
582
583         mop->mo_usource = argv[optind];
584         if (!mop->mo_usource) {
585                 usage(stderr);
586         }
587
588         /**
589          * Try to get the real path to the device, in case it is a
590          * symbolic link for instance
591          */
592         if (realpath(mop->mo_usource, real_path) != NULL) {
593                 ptr = strrchr(real_path, '/');
594                 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
595                         snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
596                         if ((f = fopen(path, "r"))) {
597                                 /* read "<name>\n" from sysfs */
598                                 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
599                                         name[sz - 1] = '\0';
600                                         snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
601                                 }
602                                 fclose(f);
603                         }
604                 }
605                 mop->mo_usource = strdup(real_path);
606         }
607
608         ptr = strstr(mop->mo_usource, ":/");
609         if (ptr != NULL) {
610                 mop->mo_source = convert_hostnames(mop->mo_usource);
611                 if (!mop->mo_source)
612                         usage(stderr);
613         } else {
614                 mop->mo_source = strdup(mop->mo_usource);
615         }
616
617         if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
618                 rc = errno;
619                 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
620                                 argv[optind + 1], strerror(errno));
621                 return rc;
622         }
623
624         return 0;
625 }
626
627 int main(int argc, char *const argv[])
628 {
629         struct mount_opts mop;
630         char *options;
631         int i, rc, flags;
632
633         progname = strrchr(argv[0], '/');
634         progname = progname ? progname + 1 : argv[0];
635
636         set_defaults(&mop);
637
638         rc = parse_opts(argc, argv, &mop);
639         if (rc || version)
640                 return rc;
641
642         if (verbose) {
643                 for (i = 0; i < argc; i++)
644                         printf("arg[%d] = %s\n", i, argv[i]);
645                 printf("source = %s (%s), target = %s\n", mop.mo_usource,
646                        mop.mo_source, mop.mo_target);
647                 printf("options = %s\n", mop.mo_orig_options);
648         }
649
650         options = malloc(MAXOPT);
651         if (options == NULL) {
652                 fprintf(stderr, "can't allocate memory for options\n");
653                 return -1;
654         }
655         strcpy(options, mop.mo_orig_options);
656         rc = parse_options(&mop, options, &flags);
657         if (rc) {
658                 fprintf(stderr, "%s: can't parse options: %s\n",
659                         progname, options);
660                 return(EINVAL);
661         }
662
663         if (!mop.mo_force) {
664                 rc = check_mtab_entry(mop.mo_usource, mop.mo_source,
665                                       mop.mo_target, "lustre");
666                 if (rc && !(flags & MS_REMOUNT)) {
667                         fprintf(stderr, "%s: according to %s %s is "
668                                 "already mounted on %s\n", progname, MOUNTED,
669                                 mop.mo_usource, mop.mo_target);
670                         return(EEXIST);
671                 }
672                 if (!rc && (flags & MS_REMOUNT)) {
673                         fprintf(stderr, "%s: according to %s %s is "
674                                 "not already mounted on %s\n", progname, MOUNTED,
675                                 mop.mo_usource, mop.mo_target);
676                         return(ENOENT);
677                 }
678         }
679         if (flags & MS_REMOUNT)
680                 mop.mo_nomtab++;
681
682         rc = access(mop.mo_target, F_OK);
683         if (rc) {
684                 rc = errno;
685                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname,
686                         mop.mo_target, strerror(errno));
687                 return rc;
688         }
689
690         if (strstr(mop.mo_usource, ":/") == NULL) {
691                 rc = osd_init();
692                 if (rc)
693                         return rc;
694
695                 rc = parse_ldd(mop.mo_source, &mop, options);
696                 if (rc)
697                         return rc;
698         }
699
700         /* In Linux 2.4, the target device doesn't get passed to any of our
701            functions.  So we'll stick it on the end of the options. */
702         append_option(options, "device=");
703         strcat(options, mop.mo_source);
704
705         if (verbose)
706                 printf("mounting device %s at %s, flags=%#x options=%s\n",
707                        mop.mo_source, mop.mo_target, flags, options);
708
709         if (strstr(mop.mo_usource, ":/") == NULL &&
710             osd_tune_lustre(mop.mo_source, &mop)) {
711                 if (verbose)
712                         fprintf(stderr, "%s: unable to set tunables for %s"
713                                         " (may cause reduced IO performance)\n",
714                                         argv[0], mop.mo_source);
715         }
716
717         if (!mop.mo_fake) {
718                 /* flags and target get to lustre_get_sb(), but not
719                  * lustre_fill_super().  Lustre ignores the flags, but mount
720                  * does not. */
721                 for (i = 0, rc = -EAGAIN; i <= mop.mo_retry && rc != 0; i++) {
722                         rc = mount(mop.mo_source, mop.mo_target, "lustre",
723                                    flags, (void *)options);
724                         if (rc == 0) {
725                                 /* change label from <fsname>:<index> to
726                                  * <fsname>-<index> to indicate the device has
727                                  *  been registered. only if the label is
728                                  *  supposed to be changed and target service
729                                  *  is supposed to start */
730                                 if (mop.mo_ldd.ldd_flags &
731                                    (LDD_F_VIRGIN | LDD_F_WRITECONF)) {
732                                         if (mop.mo_nosvc == 0)
733                                                 (void)osd_label_lustre(&mop);
734                                 }
735                         } else {
736                                 if (verbose) {
737                                         fprintf(stderr, "%s: mount %s at %s "
738                                                 "failed: %s retries left: "
739                                                 "%d\n", basename(progname),
740                                                 mop.mo_usource, mop.mo_target,
741                                                 strerror(errno),
742                                                 mop.mo_retry - i);
743                                 }
744
745                                 if (mop.mo_retry) {
746                                         int limit = i/2 > 5 ? i/2 : 5;
747
748                                         sleep(1 << limit);
749                                 } else {
750                                         rc = errno;
751                                 }
752                         }
753                 }
754         }
755
756         if (rc) {
757                 char *cli;
758
759                 rc = errno;
760
761                 cli = strrchr(mop.mo_usource, ':');
762                 if (cli && (strlen(cli) > 2))
763                         cli += 2;
764                 else
765                         cli = NULL;
766
767                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
768                         mop.mo_usource, mop.mo_target, strerror(errno));
769                 if (errno == EBUSY)
770                         fprintf(stderr, "Is the backend filesystem mounted?\n"
771                                         "Check /etc/mtab and /proc/mounts\n");
772                 if (errno == ENODEV)
773                         fprintf(stderr, "Are the lustre modules loaded?\n"
774                                 "Check /etc/modprobe.conf and "
775                                 "/proc/filesystems\n");
776                 if (errno == ENOTBLK)
777                         fprintf(stderr, "Do you need -o loop?\n");
778                 if (errno == ENOMEDIUM)
779                         fprintf(stderr,
780                                 "This filesystem needs at least 1 OST\n");
781                 if (errno == ENOENT) {
782                         fprintf(stderr, "Is the MGS specification correct?\n");
783                         fprintf(stderr, "Is the filesystem name correct?\n");
784                         fprintf(stderr, "If upgrading, is the copied client log"
785                                 " valid? (see upgrade docs)\n");
786                 }
787                 if (errno == EALREADY)
788                         fprintf(stderr, "The target service is already running."
789                                 " (%s)\n", mop.mo_usource);
790                 if (errno == ENXIO)
791                         fprintf(stderr, "The target service failed to start "
792                                 "(bad config log?) (%s).  "
793                                 "See /var/log/messages.\n", mop.mo_usource);
794                 if (errno == EIO)
795                         fprintf(stderr, "Is the MGS running?\n");
796                 if (errno == EADDRINUSE)
797                         fprintf(stderr, "The target service's index is already "
798                                 "in use. (%s)\n", mop.mo_usource);
799                 if (errno == EINVAL) {
800                         fprintf(stderr, "This may have multiple causes.\n");
801                         if (cli)
802                                 fprintf(stderr, "Is '%s' the correct filesystem"
803                                         " name?\n", cli);
804                         fprintf(stderr, "Are the mount options correct?\n");
805                         fprintf(stderr, "Check the syslog for more info.\n");
806                 }
807
808                 /* May as well try to clean up loop devs */
809                 if (strncmp(mop.mo_usource, "/dev/loop", 9) == 0) {
810                         char cmd[256];
811                         int ret;
812                         sprintf(cmd, "/sbin/losetup -d %s", mop.mo_usource);
813                         if ((ret = system(cmd)) < 0)
814                                 rc = errno;
815                         else if (ret > 0)
816                                 rc = WEXITSTATUS(ret);
817                 }
818
819         } else {
820                 /* Deal with utab just for client. Note that we ignore
821                  * the return value here since it is not worth to fail
822                  * mount by prevent some rare cases */
823                 if (strstr(mop.mo_usource, ":/") != NULL)
824                         update_utab_entry(&mop);
825                 if (!mop.mo_nomtab) {
826                         rc = update_mtab_entry(mop.mo_usource, mop.mo_target,
827                                                "lustre", mop.mo_orig_options,
828                                                0, 0, 0);
829                 }
830         }
831
832         free(options);
833         /* mo_usource should be freed, but we can rely on the kernel */
834         free(mop.mo_source);
835
836         if (strstr(mop.mo_usource, ":/") == NULL)
837                 osd_fini();
838
839         return rc;
840 }