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