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