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