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