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