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