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