Whamcloud - gitweb
LU-365 Update copyright for files modified by Whamcloud
[fs/lustre-release.git] / lustre / utils / mount_lustre.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/utils/mount_lustre.c
40  *
41  * Author: Robert Read <rread@clusterfs.com>
42  * Author: Nathan Rutman <nathan@clusterfs.com>
43  */
44
45 #ifndef _GNU_SOURCE
46 #define _GNU_SOURCE
47 #endif
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 <linux/fs.h>
56 #include <mntent.h>
57 #include <getopt.h>
58 #include "obdctl.h"
59 #include <lustre_ver.h>
60 #include <glob.h>
61 #include <ctype.h>
62 #include <limits.h>
63 #include "mount_utils.h"
64
65 #define MAX_HW_SECTORS_KB_PATH  "queue/max_hw_sectors_kb"
66 #define MAX_SECTORS_KB_PATH     "queue/max_sectors_kb"
67 #define STRIPE_CACHE_SIZE       "md/stripe_cache_size"
68 #define MAX_RETRIES 99
69
70 int          verbose = 0;
71 int          nomtab = 0;
72 int          fake = 0;
73 int          force = 0;
74 int          retry = 0;
75 int          md_stripe_cache_size = 16384;
76 char         *progname = NULL;
77
78 void usage(FILE *out)
79 {
80         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
81         fprintf(out, "\nThis mount helper should only be invoked via the "
82                 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
83         fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
84                 progname);
85         fprintf(out,
86                 "\t<device>: the disk device, or for a client:\n"
87                 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
88                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
89                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
90                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
91                 "\t-o force|--force: force mount even if already in /etc/mtab\n"
92                 "\t-h|--help: print this usage message\n"
93                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
94                 "\t-v|--verbose: print verbose config settings\n"
95                 "\t<mntopt>: one or more comma separated of:\n"
96                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
97                 "\t\tabort_recov: abort server recovery handling\n"
98                 "\t\tnosvc: only start MGC/MGS obds\n"
99                 "\t\tnomgs: only start target obds, using existing MGS\n"
100                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
101                 "inactive OSTs (e.g. lustre-OST0001)\n"
102                 "\t\tretry=<num>: number of times mount is retried by client\n"
103                 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
104                 "size for the underlying raid if present\n"
105                 );
106         exit((out != stdout) ? EINVAL : 0);
107 }
108
109 static int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
110 {
111         FILE *fp;
112         struct mntent *mnt;
113
114         fp = setmntent(MOUNTED, "r");
115         if (fp == NULL)
116                 return(0);
117
118         while ((mnt = getmntent(fp)) != NULL) {
119                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
120                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
121                         strcmp(mnt->mnt_dir, mtpt) == 0 &&
122                         strcmp(mnt->mnt_type, type) == 0) {
123                         endmntent(fp);
124                         return(EEXIST);
125                 }
126         }
127         endmntent(fp);
128
129         return(0);
130 }
131
132 static int
133 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
134                   int flags, int freq, int pass)
135 {
136         FILE *fp;
137         struct mntent mnt;
138         int rc = 0;
139
140         mnt.mnt_fsname = spec;
141         mnt.mnt_dir = mtpt;
142         mnt.mnt_type = type;
143         mnt.mnt_opts = opts ? opts : "";
144         mnt.mnt_freq = freq;
145         mnt.mnt_passno = pass;
146
147         fp = setmntent(MOUNTED, "a+");
148         if (fp == NULL) {
149                 fprintf(stderr, "%s: setmntent(%s): %s:",
150                         progname, MOUNTED, strerror (errno));
151                 rc = 16;
152         } else {
153                 if ((addmntent(fp, &mnt)) == 1) {
154                         fprintf(stderr, "%s: addmntent: %s:",
155                                 progname, strerror (errno));
156                         rc = 16;
157                 }
158                 endmntent(fp);
159         }
160
161         return rc;
162 }
163
164 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
165 #define MAXNIDSTR 1024
166 static char *convert_hostnames(char *s1)
167 {
168         char *converted, *s2 = 0, *c;
169         char sep;
170         int left = MAXNIDSTR;
171         lnet_nid_t nid;
172
173         converted = malloc(left);
174         if (converted == NULL) {
175                 fprintf(stderr, "out of memory: needed %d bytes\n",
176                         MAXNIDSTR);
177                 return NULL;
178         }
179         c = converted;
180         while ((left > 0) && (*s1 != '/')) {
181                 s2 = strpbrk(s1, ",:");
182                 if (!s2)
183                         goto out_free;
184                 sep = *s2;
185                 *s2 = '\0';
186                 nid = libcfs_str2nid(s1);
187                 *s2 = sep;                      /* back to original string */
188                 if (nid == LNET_NID_ANY)
189                         goto out_free;
190                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
191                 left = converted + MAXNIDSTR - c;
192                 s1 = s2 + 1;
193         }
194         snprintf(c, left, "%s", s1);
195         return converted;
196 out_free:
197         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
198         free(converted);
199         return NULL;
200 }
201
202 /*****************************************************************************
203  *
204  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
205  * license information, but many other files in the package are identified as
206  * GNU GPL, so it's a pretty safe bet that was their intent.
207  *
208  ****************************************************************************/
209 struct opt_map {
210         const char *opt;        /* option name */
211         int inv;                /* true if flag value should be inverted */
212         int mask;               /* flag mask value */
213 };
214
215 static const struct opt_map opt_map[] = {
216   /*"optname", inv,ms_mask */
217   /* These flags are parsed by mount, not lustre */
218   { "defaults", 0, 0         },      /* default options */
219   { "remount",  0, MS_REMOUNT},      /* remount with different options */
220   { "rw",       1, MS_RDONLY },      /* read-write */
221   { "ro",       0, MS_RDONLY },      /* read-only */
222   { "exec",     1, MS_NOEXEC },      /* permit execution of binaries */
223   { "noexec",   0, MS_NOEXEC },      /* don't execute binaries */
224   { "suid",     1, MS_NOSUID },      /* honor suid executables */
225   { "nosuid",   0, MS_NOSUID },      /* don't honor suid executables */
226   { "dev",      1, MS_NODEV  },      /* interpret device files  */
227   { "nodev",    0, MS_NODEV  },      /* don't interpret devices */
228   { "sync",     0, MS_SYNCHRONOUS},  /* synchronous I/O */
229   { "async",    1, MS_SYNCHRONOUS},  /* asynchronous I/O */
230   { "atime",    1, MS_NOATIME  },    /* set file access time on read */
231   { "noatime",  0, MS_NOATIME  },    /* do not set file access time on read */
232 #ifdef MS_NODIRATIME
233   { "diratime", 1, MS_NODIRATIME },  /* set file access time on read */
234   { "nodiratime",0,MS_NODIRATIME },  /* do not set file access time on read */
235 #endif
236 #ifdef MS_RELATIME
237   { "relatime", 0, MS_RELATIME },  /* set file access time on read */
238   { "norelatime",1,MS_RELATIME },  /* do not set file access time on read */
239 #endif
240 #ifdef MS_STRICTATIME
241   { "strictatime",0,MS_STRICTATIME },  /* update access time strictly */
242 #endif
243   { "auto",     0, 0         },      /* Can be mounted using -a */
244   { "noauto",   0, 0         },      /* Can only be mounted explicitly */
245   { "nousers",  1, 0         },      /* Forbid ordinary user to mount */
246   { "nouser",   1, 0         },      /* Forbid ordinary user to mount */
247   { "noowner",  1, 0         },      /* Device owner has no special privs */
248   { "_netdev",  0, 0         },      /* Device accessible only via network */
249   { "loop",     0, 0         },
250   { NULL,       0, 0         }
251 };
252 /****************************************************************************/
253
254 /* 1  = don't pass on to lustre
255    0  = pass on to lustre */
256 static int parse_one_option(const char *check, int *flagp)
257 {
258         const struct opt_map *opt;
259
260         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
261                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
262                         if (opt->mask) {
263                                 if (opt->inv)
264                                         *flagp &= ~(opt->mask);
265                                 else
266                                         *flagp |= opt->mask;
267                         }
268                         return 1;
269                 }
270         }
271         /* Assume any unknown options are valid and pass them on.  The mount
272            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
273         return 0;
274 }
275
276 static void append_option(char *options, const char *one)
277 {
278         if (*options)
279                 strcat(options, ",");
280         strcat(options, one);
281 }
282
283 /* Replace options with subset of Lustre-specific options, and
284    fill in mount flags */
285 int parse_options(char *orig_options, int *flagp)
286 {
287         char *options, *opt, *nextopt, *arg, *val;
288
289         options = calloc(strlen(orig_options) + 1, 1);
290         *flagp = 0;
291         nextopt = orig_options;
292         while ((opt = strsep(&nextopt, ","))) {
293                 if (!*opt)
294                         /* empty option */
295                         continue;
296
297                 /* Handle retries in a slightly different
298                  * manner */
299                 arg = opt;
300                 val = strchr(opt, '=');
301                 /* please note that some ldiskfs mount options are also in the form
302                  * of param=value. We should pay attention not to remove those
303                  * mount options, see bug 22097. */
304                 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
305                         md_stripe_cache_size = atoi(val + 1);
306                 } else if (val && strncmp(arg, "retry", 5) == 0) {
307                         retry = atoi(val + 1);
308                         if (retry > MAX_RETRIES)
309                                 retry = MAX_RETRIES;
310                         else if (retry < 0)
311                                 retry = 0;
312                 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
313                         append_option(options, opt);
314                 } else if (strcmp(opt, "force") == 0) {
315                         //XXX special check for 'force' option
316                         ++force;
317                         printf("force: %d\n", force);
318                 } else if (parse_one_option(opt, flagp) == 0) {
319                         /* pass this on as an option */
320                         append_option(options, opt);
321                 }
322         }
323 #ifdef MS_STRICTATIME
324                 /* set strictatime to default if NOATIME or RELATIME
325                    not given explicit */
326         if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
327                 *flagp |= MS_STRICTATIME;
328 #endif
329         strcpy(orig_options, options);
330         free(options);
331         return 0;
332 }
333
334
335 int read_file(char *path, char *buf, int size)
336 {
337         FILE *fd;
338
339         fd = fopen(path, "r");
340         if (fd == NULL)
341                 return errno;
342
343         /* should not ignore fgets(3)'s return value */
344         if (!fgets(buf, size, fd)) {
345                 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
346                 fclose(fd);
347                 return 1;
348         }
349         fclose(fd);
350         return 0;
351 }
352
353 int write_file(char *path, char *buf)
354 {
355         FILE *fd;
356
357         fd = fopen(path, "w");
358         if (fd == NULL)
359                 return errno;
360
361         fputs(buf, fd);
362         fclose(fd);
363         return 0;
364 }
365
366 /* This is to tune the kernel for good SCSI performance.
367  * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
368  * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
369 int set_blockdev_tunables(char *source, int fan_out)
370 {
371         glob_t glob_info = { 0 };
372         struct stat stat_buf;
373         char *chk_major, *chk_minor;
374         char *savept, *dev;
375         char *ret_path;
376         char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
377         char real_path[PATH_MAX] = {'\0'};
378         int i, rc = 0;
379         int major, minor;
380
381         if (!source)
382                 return -EINVAL;
383
384         ret_path = realpath(source, real_path);
385         if (ret_path == NULL) {
386                 if (verbose)
387                         fprintf(stderr, "warning: %s: cannot resolve: %s\n",
388                                 source, strerror(errno));
389                 return -EINVAL;
390         }
391
392         if (strncmp(real_path, "/dev/loop", 9) == 0)
393                 return 0;
394
395         if ((real_path[0] != '/') && (strpbrk(real_path, ",:") != NULL))
396                 return 0;
397
398         snprintf(path, sizeof(path), "/sys/block%s", real_path + 4);
399         if (access(path, X_OK) == 0)
400                 goto set_params;
401
402         /* The name of the device say 'X' specified in /dev/X may not
403          * match any entry under /sys/block/. In that case we need to
404          * match the major/minor number to find the entry under
405          * sys/block corresponding to /dev/X */
406
407         /* Don't chop tail digit on /dev/mapper/xxx, LU-478 */
408         if (strncmp(real_path, "/dev/mapper", 11) != 0) {
409                 dev = real_path + strlen(real_path);
410                 while (--dev > real_path && isdigit(*dev))
411                         *dev = 0;
412
413                 if (strncmp(real_path, "/dev/md_", 8) == 0)
414                         *dev = 0;
415         }
416
417         rc = stat(real_path, &stat_buf);
418         if (rc) {
419                 if (verbose)
420                         fprintf(stderr, "warning: %s, device %s stat failed\n",
421                                 strerror(errno), real_path);
422                 return rc;
423         }
424
425         major = major(stat_buf.st_rdev);
426         minor = minor(stat_buf.st_rdev);
427         rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
428         if (rc) {
429                 if (verbose)
430                         fprintf(stderr, "warning: failed to read entries under "
431                                 "/sys/block\n");
432                 globfree(&glob_info);
433                 return rc;
434         }
435
436         for (i = 0; i < glob_info.gl_pathc; i++){
437                 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
438
439                 rc = read_file(path, buf, sizeof(buf));
440                 if (rc)
441                         continue;
442
443                 if (buf[strlen(buf) - 1] == '\n')
444                         buf[strlen(buf) - 1] = '\0';
445
446                 chk_major = strtok_r(buf, ":", &savept);
447                 chk_minor = savept;
448                 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
449                         break;
450         }
451
452         if (i == glob_info.gl_pathc) {
453                 if (verbose)
454                         fprintf(stderr,"warning: device %s does not match any "
455                                 "entry under /sys/block\n", real_path);
456                 globfree(&glob_info);
457                 return -EINVAL;
458         }
459
460         /* Chop off "/dev" from path we found */
461         path[strlen(glob_info.gl_pathv[i])] = '\0';
462         globfree(&glob_info);
463
464 set_params:
465         if (strncmp(real_path, "/dev/md", 7) == 0) {
466                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
467                          STRIPE_CACHE_SIZE);
468
469                 rc = read_file(real_path, buf, sizeof(buf));
470                 if (rc) {
471                         if (verbose)
472                                 fprintf(stderr, "warning: opening %s: %s\n",
473                                         real_path, strerror(errno));
474                         return 0;
475                 }
476
477                 if (atoi(buf) >= md_stripe_cache_size)
478                         return 0;
479
480                 if (strlen(buf) - 1 > 0) {
481                         snprintf(buf, sizeof(buf), "%d", md_stripe_cache_size);
482                         rc = write_file(real_path, buf);
483                         if (rc && verbose)
484                                 fprintf(stderr, "warning: opening %s: %s\n",
485                                         real_path, strerror(errno));
486                 }
487                 /* Return since raid and disk tunables are different */
488                 return rc;
489         }
490
491         snprintf(real_path, sizeof(real_path), "%s/%s", path,
492                  MAX_HW_SECTORS_KB_PATH);
493         rc = read_file(real_path, buf, sizeof(buf));
494         if (rc) {
495                 if (verbose)
496                         fprintf(stderr, "warning: opening %s: %s\n",
497                                 real_path, strerror(errno));
498                 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
499                  * error for some device. */
500                 rc = 0;
501         }
502
503         if (strlen(buf) - 1 > 0) {
504                 snprintf(real_path, sizeof(real_path), "%s/%s", path,
505                          MAX_SECTORS_KB_PATH);
506                 rc = write_file(real_path, buf);
507                 if (rc) {
508                         if (verbose)
509                                 fprintf(stderr, "warning: writing to %s: %s\n",
510                                         real_path, strerror(errno));
511                         /* No MAX_SECTORS_KB_PATH isn't necessary an
512                          * error for some device. */
513                         rc = 0;
514                 }
515         }
516
517         if (fan_out) {
518                 char *slave = NULL;
519                 glob_info.gl_pathc = 0;
520                 glob_info.gl_offs = 0;
521                 /* if device is multipath device, tune its slave devices */
522                 snprintf(real_path, sizeof(real_path), "%s/slaves/*", path);
523                 rc = glob(real_path, GLOB_NOSORT, NULL, &glob_info);
524
525                 for (i = 0; rc == 0 && i < glob_info.gl_pathc; i++){
526                         slave = basename(glob_info.gl_pathv[i]);
527                         snprintf(real_path, sizeof(real_path), "/dev/%s", slave);
528                         rc = set_blockdev_tunables(real_path, 0);
529                 }
530
531                 if (rc == GLOB_NOMATCH) {
532                         /* no slave device is not an error */
533                         rc = 0;
534                 } else if (rc && verbose) {
535                         if (slave == NULL) {
536                                 fprintf(stderr, "warning: %s, failed to read"
537                                         " entries under %s/slaves\n",
538                                         strerror(errno), path);
539                         } else {
540                                 fprintf(stderr, "unable to set tunables for"
541                                         " slave device %s (slave would be"
542                                         " unable to handle IO request from"
543                                         " master %s)\n",
544                                         real_path, source);
545                         }
546                 }
547                 globfree(&glob_info);
548         }
549
550         return rc;
551 }
552
553 int main(int argc, char *const argv[])
554 {
555         char default_options[] = "";
556         char *usource, *source, *ptr;
557         char target[PATH_MAX] = {'\0'};
558         char real_path[PATH_MAX] = {'\0'};
559         char path[256], name[256];
560         FILE *f;
561         size_t sz;
562         char *options, *optcopy, *orig_options = default_options;
563         int i, nargs = 3, opt, rc, flags, optlen;
564         static struct option long_opt[] = {
565                 {"fake", 0, 0, 'f'},
566                 {"force", 0, 0, 1},
567                 {"help", 0, 0, 'h'},
568                 {"nomtab", 0, 0, 'n'},
569                 {"options", 1, 0, 'o'},
570                 {"verbose", 0, 0, 'v'},
571                 {0, 0, 0, 0}
572         };
573
574         progname = strrchr(argv[0], '/');
575         progname = progname ? progname + 1 : argv[0];
576
577         while ((opt = getopt_long(argc, argv, "fhno:v",
578                                   long_opt, NULL)) != EOF){
579                 switch (opt) {
580                 case 1:
581                         ++force;
582                         printf("force: %d\n", force);
583                         nargs++;
584                         break;
585                 case 'f':
586                         ++fake;
587                         printf("fake: %d\n", fake);
588                         nargs++;
589                         break;
590                 case 'h':
591                         usage(stdout);
592                         break;
593                 case 'n':
594                         ++nomtab;
595                         printf("nomtab: %d\n", nomtab);
596                         nargs++;
597                         break;
598                 case 'o':
599                         orig_options = optarg;
600                         nargs++;
601                         break;
602                 case 'v':
603                         ++verbose;
604                         nargs++;
605                         break;
606                 default:
607                         fprintf(stderr, "%s: unknown option '%c'\n",
608                                 progname, opt);
609                         usage(stderr);
610                         break;
611                 }
612         }
613
614         if (optind + 2 > argc) {
615                 fprintf(stderr, "%s: too few arguments\n", progname);
616                 usage(stderr);
617         }
618
619         usource = argv[optind];
620         if (!usource) {
621                 usage(stderr);
622         }
623
624         /**
625          * Try to get the real path to the device, in case it is a
626          * symbolic link for instance
627          */
628         if (realpath(usource, real_path) != NULL) {
629                 usource = real_path;
630
631                 ptr = strrchr(real_path, '/');
632                 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
633                         snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
634                         if ((f = fopen(path, "r"))) {
635                                 /* read "<name>\n" from sysfs */
636                                 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
637                                         name[sz - 1] = '\0';
638                                         snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
639                                 }
640                                 fclose(f);
641                         }
642                 }
643         }
644
645         source = convert_hostnames(usource);
646         if (!source) {
647                 usage(stderr);
648         }
649
650         if (realpath(argv[optind + 1], target) == NULL) {
651                 rc = errno;
652                 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
653                         argv[optind + 1], strerror(errno));
654                 return rc;
655         }
656
657         if (verbose) {
658                 for (i = 0; i < argc; i++)
659                         printf("arg[%d] = %s\n", i, argv[i]);
660                 printf("source = %s (%s), target = %s\n", usource, source,
661                        target);
662                 printf("options = %s\n", orig_options);
663         }
664
665         options = malloc(strlen(orig_options) + 1);
666         if (options == NULL) {
667                 fprintf(stderr, "can't allocate memory for options\n");
668                 return -1;
669         }
670         strcpy(options, orig_options);
671         rc = parse_options(options, &flags);
672         if (rc) {
673                 fprintf(stderr, "%s: can't parse options: %s\n",
674                         progname, options);
675                 return(EINVAL);
676         }
677
678         if (!force) {
679                 rc = check_mtab_entry(usource, source, target, "lustre");
680                 if (rc && !(flags & MS_REMOUNT)) {
681                         fprintf(stderr, "%s: according to %s %s is "
682                                 "already mounted on %s\n",
683                                 progname, MOUNTED, usource, target);
684                         return(EEXIST);
685                 }
686                 if (!rc && (flags & MS_REMOUNT)) {
687                         fprintf(stderr, "%s: according to %s %s is "
688                                 "not already mounted on %s\n",
689                                 progname, MOUNTED, usource, target);
690                         return(ENOENT);
691                 }
692         }
693         if (flags & MS_REMOUNT)
694                 nomtab++;
695
696         rc = access(target, F_OK);
697         if (rc) {
698                 rc = errno;
699                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
700                         strerror(errno));
701                 return rc;
702         }
703
704         /* In Linux 2.4, the target device doesn't get passed to any of our
705            functions.  So we'll stick it on the end of the options. */
706         optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
707         optcopy = malloc(optlen);
708         if (optcopy == NULL) {
709                 fprintf(stderr, "can't allocate memory to optcopy\n");
710                 return -1;
711         }
712         strcpy(optcopy, options);
713         if (*optcopy)
714                 strcat(optcopy, ",");
715         strcat(optcopy, "device=");
716         strcat(optcopy, source);
717
718         if (verbose)
719                 printf("mounting device %s at %s, flags=%#x options=%s\n",
720                        source, target, flags, optcopy);
721
722         if (!strstr(usource, ":/") && set_blockdev_tunables(source, 1)) {
723                 if (verbose)
724                         fprintf(stderr, "%s: unable to set tunables for %s"
725                                 " (may cause reduced IO performance)\n",
726                                 argv[0], source);
727         }
728
729         register_service_tags(usource, source, target);
730
731         if (!fake) {
732                 /* flags and target get to lustre_get_sb, but not
733                    lustre_fill_super.  Lustre ignores the flags, but mount
734                    does not. */
735                 for (i = 0, rc = -EAGAIN; i <= retry && rc != 0; i++) {
736                         rc = mount(source, target, "lustre", flags,
737                                    (void *)optcopy);
738                         if (rc) {
739                                 if (verbose) {
740                                         fprintf(stderr, "%s: mount %s at %s "
741                                                 "failed: %s retries left: "
742                                                 "%d\n", basename(progname),
743                                                 usource, target,
744                                                 strerror(errno), retry-i);
745                                 }
746
747                                 if (retry) {
748                                         sleep(1 << max((i/2), 5));
749                                 }
750                                 else {
751                                         rc = errno;
752                                 }
753                         }
754                 }
755         }
756
757         if (rc) {
758                 char *cli;
759
760                 rc = errno;
761
762                 cli = strrchr(usource, ':');
763                 if (cli && (strlen(cli) > 2))
764                         cli += 2;
765                 else
766                         cli = NULL;
767
768                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
769                         usource, target, strerror(errno));
770                 if (errno == ENODEV)
771                         fprintf(stderr, "Are the lustre modules loaded?\n"
772                                 "Check /etc/modprobe.conf and "
773                                 "/proc/filesystems\n");
774                 if (errno == ENOTBLK)
775                         fprintf(stderr, "Do you need -o loop?\n");
776                 if (errno == ENOMEDIUM)
777                         fprintf(stderr,
778                                 "This filesystem needs at least 1 OST\n");
779                 if (errno == ENOENT) {
780                         fprintf(stderr, "Is the MGS specification correct?\n");
781                         fprintf(stderr, "Is the filesystem name correct?\n");
782                         fprintf(stderr, "If upgrading, is the copied client log"
783                                 " valid? (see upgrade docs)\n");
784                 }
785                 if (errno == EALREADY)
786                         fprintf(stderr, "The target service is already running."
787                                 " (%s)\n", usource);
788                 if (errno == ENXIO)
789                         fprintf(stderr, "The target service failed to start "
790                                 "(bad config log?) (%s).  "
791                                 "See /var/log/messages.\n", usource);
792                 if (errno == EIO)
793                         fprintf(stderr, "Is the MGS running?\n");
794                 if (errno == EADDRINUSE)
795                         fprintf(stderr, "The target service's index is already "
796                                 "in use. (%s)\n", usource);
797                 if (errno == EINVAL) {
798                         fprintf(stderr, "This may have multiple causes.\n");
799                         if (cli)
800                                 fprintf(stderr, "Is '%s' the correct filesystem"
801                                         " name?\n", cli);
802                         fprintf(stderr, "Are the mount options correct?\n");
803                         fprintf(stderr, "Check the syslog for more info.\n");
804                 }
805
806                 /* May as well try to clean up loop devs */
807                 if (strncmp(usource, "/dev/loop", 9) == 0) {
808                         char cmd[256];
809                         int ret;
810                         sprintf(cmd, "/sbin/losetup -d %s", usource);
811                         if ((ret = system(cmd)) < 0)
812                                 rc = errno;
813                         else if (ret > 0)
814                                 rc = WEXITSTATUS(ret);
815                 }
816
817         } else if (!nomtab) {
818                 rc = update_mtab_entry(usource, target, "lustre", orig_options,
819                                        0,0,0);
820         }
821
822         free(optcopy);
823         free(source);
824         return rc;
825 }