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