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