Whamcloud - gitweb
058943188eb77e7c32a57b6ce10fc2dc9b3fed77
[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  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Robert Read <rread@clusterfs.com>
6  *   Author: Nathan Rutman <nathan@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25
26 #define _GNU_SOURCE
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/mount.h>
34 #include <mntent.h>
35 #include <getopt.h>
36 #include <sys/utsname.h>
37 #include "obdctl.h"
38 #include <lustre_ver.h>
39 #include <glob.h>
40 #include <ctype.h>
41
42 #define MAX_HW_SECTORS_KB_PATH  "queue/max_hw_sectors_kb"
43 #define MAX_SECTORS_KB_PATH     "queue/max_sectors_kb"
44
45 int          verbose = 0;
46 int          nomtab = 0;
47 int          fake = 0;
48 int          force = 0;
49 static char *progname = NULL;
50
51 void usage(FILE *out)
52 {
53         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
54         fprintf(out, "\nThis mount helper should only be invoked via the "
55                 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
56         fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
57                 progname);
58         fprintf(out,
59                 "\t<device>: the disk device, or for a client:\n"
60                 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
61                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
62                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
63                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
64                 "\t--force: force mount even if already in /etc/mtab\n"
65                 "\t-h|--help: print this usage message\n"
66                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
67                 "\t-v|--verbose: print verbose config settings\n"
68                 "\t<mntopt>: one or more comma separated of:\n"
69                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
70                 "\t\tnosvc: only start MGC/MGS obds\n"
71                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
72                 "inactive OSTs (e.g. lustre-OST0001)\n"
73                 );
74         exit((out != stdout) ? EINVAL : 0);
75 }
76
77 static int check_mtab_entry(char *spec, char *mtpt, char *type)
78 {
79         FILE *fp;
80         struct mntent *mnt;
81
82         fp = setmntent(MOUNTED, "r");
83         if (fp == NULL)
84                 return(0);
85
86         while ((mnt = getmntent(fp)) != NULL) {
87                 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
88                         strcmp(mnt->mnt_dir, mtpt) == 0 &&
89                         strcmp(mnt->mnt_type, type) == 0) {
90                         endmntent(fp);
91                         return(EEXIST);
92                 }
93         }
94         endmntent(fp);
95
96         return(0);
97 }
98
99 static int
100 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
101                   int flags, int freq, int pass)
102 {
103         FILE *fp;
104         struct mntent mnt;
105         int rc = 0;
106
107         mnt.mnt_fsname = spec;
108         mnt.mnt_dir = mtpt;
109         mnt.mnt_type = type;
110         mnt.mnt_opts = opts ? opts : "";
111         mnt.mnt_freq = freq;
112         mnt.mnt_passno = pass;
113
114         fp = setmntent(MOUNTED, "a+");
115         if (fp == NULL) {
116                 fprintf(stderr, "%s: setmntent(%s): %s:",
117                         progname, MOUNTED, strerror (errno));
118                 rc = 16;
119         } else {
120                 if ((addmntent(fp, &mnt)) == 1) {
121                         fprintf(stderr, "%s: addmntent: %s:",
122                                 progname, strerror (errno));
123                         rc = 16;
124                 }
125                 endmntent(fp);
126         }
127
128         return rc;
129 }
130
131 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
132 #define MAXNIDSTR 1024
133 static char *convert_hostnames(char *s1)
134 {
135         char *converted, *s2 = 0, *c;
136         char sep;
137         int left = MAXNIDSTR;
138         lnet_nid_t nid;
139
140         converted = malloc(left);
141         c = converted;
142         while ((left > 0) && (*s1 != '/')) {
143                 s2 = strpbrk(s1, ",:");
144                 if (!s2)
145                         goto out_free;
146                 sep = *s2;
147                 *s2 = '\0';
148                 nid = libcfs_str2nid(s1);
149                 *s2 = sep;                      /* back to original string */
150                 if (nid == LNET_NID_ANY)
151                         goto out_free;
152                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
153                 left = converted + MAXNIDSTR - c;
154                 s1 = s2 + 1;
155         }
156         snprintf(c, left, "%s", s1);
157         return converted;
158 out_free:
159         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
160         free(converted);
161         return NULL;
162 }
163
164 /*****************************************************************************
165  *
166  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
167  * license information, but many other files in the package are identified as
168  * GNU GPL, so it's a pretty safe bet that was their intent.
169  *
170  ****************************************************************************/
171 struct opt_map {
172         const char *opt;        /* option name */
173         int inv;                /* true if flag value should be inverted */
174         int mask;               /* flag mask value */
175 };
176
177 static const struct opt_map opt_map[] = {
178   /*"optname", inv,ms_mask */
179   /* These flags are parsed by mount, not lustre */
180   { "defaults", 0, 0         },      /* default options */
181   { "remount",  0, MS_REMOUNT},      /* remount with different options */
182   { "rw",       1, MS_RDONLY },      /* read-write */
183   { "ro",       0, MS_RDONLY },      /* read-only */
184   { "exec",     1, MS_NOEXEC },      /* permit execution of binaries */
185   { "noexec",   0, MS_NOEXEC },      /* don't execute binaries */
186   { "suid",     1, MS_NOSUID },      /* honor suid executables */
187   { "nosuid",   0, MS_NOSUID },      /* don't honor suid executables */
188   { "dev",      1, MS_NODEV  },      /* interpret device files  */
189   { "nodev",    0, MS_NODEV  },      /* don't interpret devices */
190   { "sync",     0, MS_SYNCHRONOUS},  /* synchronous I/O */
191   { "async",    1, MS_SYNCHRONOUS},  /* asynchronous I/O */
192   { "auto",     0, 0         },      /* Can be mounted using -a */
193   { "noauto",   0, 0         },      /* Can only be mounted explicitly */
194   { "nousers",  1, 0         },      /* Forbid ordinary user to mount */
195   { "nouser",   1, 0         },      /* Forbid ordinary user to mount */
196   { "noowner",  1, 0         },      /* Device owner has no special privs */
197   { "_netdev",  0, 0         },      /* Device accessible only via network */
198   { NULL,       0, 0         }
199 };
200 /****************************************************************************/
201
202 /* 1  = don't pass on to lustre
203    0  = pass on to lustre */
204 static int parse_one_option(const char *check, int *flagp)
205 {
206         const struct opt_map *opt;
207
208         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
209                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
210                         if (opt->mask) {
211                                 if (opt->inv)
212                                         *flagp &= ~(opt->mask);
213                                 else
214                                         *flagp |= opt->mask;
215                         }
216                         return 1;
217                 }
218         }
219         /* Assume any unknown options are valid and pass them on.  The mount
220            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
221         return 0;
222 }
223
224 /* Replace options with subset of Lustre-specific options, and
225    fill in mount flags */
226 int parse_options(char *orig_options, int *flagp)
227 {
228         char *options, *opt, *nextopt;
229
230         options = calloc(strlen(orig_options) + 1, 1);
231         *flagp = 0;
232         nextopt = orig_options;
233         while ((opt = strsep(&nextopt, ","))) {
234                 if (!*opt)
235                         /* empty option */
236                         continue;
237                 if (parse_one_option(opt, flagp) == 0) {
238                         /* pass this on as an option */
239                         if (*options)
240                                 strcat(options, ",");
241                         strcat(options, opt);
242                 }
243         }
244         strcpy(orig_options, options);
245         free(options);
246         return 0;
247 }
248
249
250 int read_file(char *path, char *buf, int size)
251 {
252         FILE *fd;
253
254         fd = fopen(path, "r");
255         if (fd == NULL)
256                 return errno;
257
258         fgets(buf, size, fd);
259         fclose(fd);
260         return 0;
261 }
262
263 int write_file(char *path, char *buf)
264 {
265         FILE *fd;
266
267         fd = fopen(path, "w");
268         if (fd == NULL)
269                 return errno;
270
271         fputs(buf, fd);
272         fclose(fd);
273         return 0;
274 }
275
276 /* This is to tune the kernel for good SCSI performance.
277  * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
278  * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
279 int set_tunables(char *source, int src_len)
280 {
281         glob_t glob_info;
282         struct stat stat_buf;
283         char *chk_major, *chk_minor;
284         char *savept, *dev, *s2 = 0;
285         char buf[PATH_MAX], path[PATH_MAX];
286         int i, rc = 0;
287         int major, minor;
288
289         if (!source)
290                 return -EINVAL;
291
292         if (strncmp(source, "/dev/loop", 9) == 0)
293                 return 0;
294
295         if ((*source != '/') && ((s2 = strpbrk(source, ",:")) != NULL))
296                 return 0;
297
298         dev = source + src_len - 1;
299         while (dev > source && (*dev != '/')) {
300                 if (isdigit(*dev))
301                         *dev = 0;
302                 dev--;
303         }
304         snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
305                  MAX_HW_SECTORS_KB_PATH);
306         rc = read_file(path, buf, sizeof(buf));
307         if (rc == 0 && (strlen(buf) - 1) > 0) {
308                 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
309                          MAX_SECTORS_KB_PATH);
310                 rc = write_file(path, buf);
311                 if (rc && verbose)
312                         fprintf(stderr, "warning: opening %s: %s\n",
313                                 path, strerror(errno));
314                 return rc;
315         }
316
317         if (rc != ENOENT)
318                 return rc;
319
320         /* The name of the device say 'X' specified in /dev/X may not
321          * match any entry under /sys/block/. In that case we need to
322          * match the major/minor number to find the entry under
323          * sys/block corresponding to /dev/X */
324         dev = source + src_len - 1;
325         while (dev > source) {
326                 if (isdigit(*dev))
327                         *dev = 0;
328                 dev--;
329         }
330
331         rc = stat(dev, &stat_buf);
332         if (rc) {
333                 if (verbose)
334                         fprintf(stderr, "warning: %s, device %s stat failed\n",
335                                 strerror(errno), dev);
336                 return rc;
337         }
338
339         major = major(stat_buf.st_rdev);
340         minor = minor(stat_buf.st_rdev);
341         rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
342         if (rc) {
343                 if (verbose)
344                         fprintf(stderr, "warning: failed to read entries under "
345                                 "/sys/block\n");
346                 return rc;
347         }
348
349         for (i = 0; i < glob_info.gl_pathc; i++){
350                 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
351
352                 rc = read_file(path, buf, sizeof(buf));
353                 if (rc)
354                         continue;
355
356                 if (buf[strlen(buf) - 1] == '\n')
357                         buf[strlen(buf) - 1] = '\0';
358
359                 chk_major = strtok_r(buf, ":", &savept);
360                 chk_minor = savept;
361                 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
362                         break;
363         }
364
365         if (i == glob_info.gl_pathc) {
366                 if (verbose)
367                         fprintf(stderr,"warning: device %s does not match any "
368                                 "entry under /sys/block\n", source);
369                 return -EINVAL;
370         }
371
372         snprintf(path, sizeof(path), "%s/%s", glob_info.gl_pathv[i],
373                  MAX_HW_SECTORS_KB_PATH);
374         rc = read_file(path, buf, sizeof(buf));
375         if (rc) {
376                 if (verbose)
377                         fprintf(stderr, "warning: opening %s: %s\n",
378                                 path, strerror(errno));
379                 return rc;
380         }
381
382         if (strlen(buf) - 1 > 0) {
383                 snprintf(path, sizeof(path), "%s/%s",
384                          glob_info.gl_pathv[i], MAX_SECTORS_KB_PATH);
385                 rc = write_file(path, buf);
386                 if (rc && verbose)
387                         fprintf(stderr, "warning: writing to %s: %s\n",
388                                 path, strerror(errno));
389         }
390         return rc;
391 }
392
393 int main(int argc, char *const argv[])
394 {
395         char default_options[] = "";
396         char *usource, *source, *target, *ptr;
397         char *options, *optcopy, *orig_options = default_options;
398         int i, nargs = 3, opt, rc, flags, optlen;
399         static struct option long_opt[] = {
400                 {"fake", 0, 0, 'f'},
401                 {"force", 0, 0, 1},
402                 {"help", 0, 0, 'h'},
403                 {"nomtab", 0, 0, 'n'},
404                 {"options", 1, 0, 'o'},
405                 {"verbose", 0, 0, 'v'},
406                 {0, 0, 0, 0}
407         };
408
409         progname = strrchr(argv[0], '/');
410         progname = progname ? progname + 1 : argv[0];
411
412         while ((opt = getopt_long(argc, argv, "fhno:v",
413                                   long_opt, NULL)) != EOF){
414                 switch (opt) {
415                 case 1:
416                         ++force;
417                         printf("force: %d\n", force);
418                         nargs++;
419                         break;
420                 case 'f':
421                         ++fake;
422                         printf("fake: %d\n", fake);
423                         nargs++;
424                         break;
425                 case 'h':
426                         usage(stdout);
427                         break;
428                 case 'n':
429                         ++nomtab;
430                         printf("nomtab: %d\n", nomtab);
431                         nargs++;
432                         break;
433                 case 'o':
434                         orig_options = optarg;
435                         nargs++;
436                         break;
437                 case 'v':
438                         ++verbose;
439                         nargs++;
440                         break;
441                 default:
442                         fprintf(stderr, "%s: unknown option '%c'\n",
443                                 progname, opt);
444                         usage(stderr);
445                         break;
446                 }
447         }
448
449         if (optind + 2 > argc) {
450                 fprintf(stderr, "%s: too few arguments\n", progname);
451                 usage(stderr);
452         }
453
454         usource = argv[optind];
455         source = convert_hostnames(usource);
456         target = argv[optind + 1];
457         ptr = target + strlen(target) - 1;
458         while ((ptr > target) && (*ptr == '/')) {
459                 *ptr = 0;
460                 ptr--;
461         }
462
463         if (!usource || !source) {
464                 usage(stderr);
465         }
466
467         if (verbose) {
468                 for (i = 0; i < argc; i++)
469                         printf("arg[%d] = %s\n", i, argv[i]);
470                 printf("source = %s (%s), target = %s\n", usource, source, target);
471                 printf("options = %s\n", orig_options);
472         }
473
474         options = malloc(strlen(orig_options) + 1);
475         strcpy(options, orig_options);
476         rc = parse_options(options, &flags);
477         if (rc) {
478                 fprintf(stderr, "%s: can't parse options: %s\n",
479                         progname, options);
480                 return(EINVAL);
481         }
482
483         if (!force) {
484                 rc = check_mtab_entry(usource, target, "lustre");
485                 if (rc && !(flags & MS_REMOUNT)) {
486                         fprintf(stderr, "%s: according to %s %s is "
487                                 "already mounted on %s\n",
488                                 progname, MOUNTED, usource, target);
489                         return(EEXIST);
490                 }
491                 if (!rc && (flags & MS_REMOUNT)) {
492                         fprintf(stderr, "%s: according to %s %s is "
493                                 "not already mounted on %s\n",
494                                 progname, MOUNTED, usource, target);
495                         return(ENOENT);
496                 }
497         }
498         if (flags & MS_REMOUNT)
499                 nomtab++;
500
501         rc = access(target, F_OK);
502         if (rc) {
503                 rc = errno;
504                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
505                         strerror(errno));
506                 return rc;
507         }
508
509         /* In Linux 2.4, the target device doesn't get passed to any of our
510            functions.  So we'll stick it on the end of the options. */
511         optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
512         optcopy = malloc(optlen);
513         strcpy(optcopy, options);
514         if (*optcopy)
515                 strcat(optcopy, ",");
516         strcat(optcopy, "device=");
517         strcat(optcopy, source);
518
519         if (verbose)
520                 printf("mounting device %s at %s, flags=%#x options=%s\n",
521                        source, target, flags, optcopy);
522
523         if (set_tunables(source, strlen(source)) && verbose)
524                 fprintf(stderr, "%s: unable to set tunables for %s"
525                                 " (may cause reduced IO performance)",
526                                 argv[0], source);
527
528         if (!fake)
529                 /* flags and target get to lustre_get_sb, but not
530                    lustre_fill_super.  Lustre ignores the flags, but mount
531                    does not. */
532                 rc = mount(source, target, "lustre", flags, (void *)optcopy);
533
534         if (rc) {
535                 char *cli;
536
537                 rc = errno;
538
539                 cli = strrchr(usource, ':');
540                 if (cli && (strlen(cli) > 2))
541                         cli += 2;
542                 else
543                         cli = NULL;
544
545                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
546                         usource, target, strerror(errno));
547                 if (errno == ENODEV)
548                         fprintf(stderr, "Are the lustre modules loaded?\n"
549                                 "Check /etc/modprobe.conf and /proc/filesystems"
550                                 "\nNote 'alias lustre llite' should be removed"
551                                 " from modprobe.conf\n");
552                 if (errno == ENOTBLK)
553                         fprintf(stderr, "Do you need -o loop?\n");
554                 if (errno == ENOMEDIUM)
555                         fprintf(stderr,
556                                 "This filesystem needs at least 1 OST\n");
557                 if (errno == ENOENT) {
558                         fprintf(stderr, "Is the MGS specification correct?\n");
559                         fprintf(stderr, "Is the filesystem name correct?\n");
560                         fprintf(stderr, "If upgrading, is the copied client log"
561                                 " valid? (see upgrade docs)\n");
562                 }
563                 if (errno == EALREADY)
564                         fprintf(stderr, "The target service is already running."
565                                 " (%s)\n", usource);
566                 if (errno == ENXIO)
567                         fprintf(stderr, "The target service failed to start "
568                                 "(bad config log?) (%s).  "
569                                 "See /var/log/messages.\n", usource);
570                 if (errno == EIO)
571                         fprintf(stderr, "Is the MGS running?\n");
572                 if (errno == EADDRINUSE)
573                         fprintf(stderr, "The target service's index is already "
574                                 "in use. (%s)\n", usource);
575                 if (errno == EINVAL) {
576                         fprintf(stderr, "This may have multiple causes.\n");
577                         if (cli)
578                                 fprintf(stderr, "Is '%s' the correct filesystem"
579                                         " name?\n", cli);
580                         fprintf(stderr, "Are the mount options correct?\n");
581                         fprintf(stderr, "Check the syslog for more info.\n");
582                 }
583
584                 /* May as well try to clean up loop devs */
585                 if (strncmp(usource, "/dev/loop", 9) == 0) {
586                         char cmd[256];
587                         sprintf(cmd, "/sbin/losetup -d %s", usource);
588                         system(cmd);
589                 }
590
591         } else if (!nomtab) {
592                 rc = update_mtab_entry(usource, target, "lustre", orig_options,
593                                        0,0,0);
594         }
595
596         free(optcopy);
597         free(source);
598         return rc;
599 }