1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Robert Read <rread@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <sys/mount.h>
36 #include <portals/ptlctl.h>
42 static char *progname = NULL;
50 #define MAX_ROUTES 1024
52 ptl_nid_t lmd_cluster_id = 0;
53 llmount_route_t routes[MAX_ROUTES];
57 fprintf(out, "usage: %s <mdsnode>:/<mdsname>/<cfgname> <mountpt> "
58 "[-fhnv] [-o mntopt]\n", progname);
59 fprintf(out, "\t<mdsnode>: hostname or nid of MDS (config) node\n"
60 "\t<mdsname>: name of MDS service (e.g. mds1)\n"
61 "\t<cfgname>: name of client config (e.g. client)\n"
62 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
63 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
64 "\t--force: force mount even if already in /etc/mtab\n"
65 "\t-h|--help: print this usage message\n"
66 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
67 "\t-v|--verbose: print verbose config settings\n"
68 "\t-o: filesystem mount options:\n"
69 "\t\tnettype={tcp,elan,iibnal,lonal}: network type\n"
70 "\t\tcluster_id=0xNNNN: cluster this node is part of\n"
71 "\t\tlocal_nid=0xNNNN: client ID (default ipaddr or nodenum)\n"
72 "\t\tserver_nid=0xNNNN: server node ID (default mdsnode)\n"
73 "\t\tport=NNN: server port (default 988 for tcp)\n"
74 "\t\troute=<gw>[-<gw>]:<low>[-<high>]: portal route to MDS\n");
78 static int check_mtab_entry(char *spec, char *mtpt, char *type)
84 fp = setmntent(MOUNTED, "r");
88 while ((mnt = getmntent(fp)) != NULL) {
89 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
90 strcmp(mnt->mnt_dir, mtpt) == 0 &&
91 strcmp(mnt->mnt_type, type) == 0) {
92 fprintf(stderr, "%s: according to %s %s is "
93 "already mounted on %s\n",
94 progname, MOUNTED, spec, mtpt);
95 return(1); /* or should we return an error? */
104 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
105 int flags, int freq, int pass)
111 mnt.mnt_fsname = spec;
114 mnt.mnt_opts = opts ? opts : "";
116 mnt.mnt_passno = pass;
118 fp = setmntent(MOUNTED, "a+");
120 fprintf(stderr, "%s: setmntent(%s): %s:",
121 progname, MOUNTED, strerror (errno));
124 if ((addmntent(fp, &mnt)) == 1) {
125 fprintf(stderr, "%s: addmntent: %s:",
126 progname, strerror (errno));
136 init_options(struct lustre_mount_data *lmd)
138 memset(lmd, 0, sizeof(*lmd));
139 lmd->lmd_magic = LMD_MAGIC;
140 lmd->lmd_server_nid = PTL_NID_ANY;
141 lmd->lmd_local_nid = PTL_NID_ANY;
142 lmd->lmd_port = 988; /* XXX define LUSTRE_DEFAULT_PORT */
143 lmd->lmd_nal = SOCKNAL;
148 print_options(struct lustre_mount_data *lmd)
152 printf("mds: %s\n", lmd->lmd_mds);
153 printf("profile: %s\n", lmd->lmd_profile);
154 printf("server_nid: "LPX64"\n", lmd->lmd_server_nid);
155 printf("local_nid: "LPX64"\n", lmd->lmd_local_nid);
156 printf("nal: %x\n", lmd->lmd_nal);
157 printf("server_ipaddr: 0x%x\n", lmd->lmd_server_ipaddr);
158 printf("port: %d\n", lmd->lmd_port);
160 for (i = 0; i < route_index; i++)
161 printf("route: "LPX64" : "LPX64" - "LPX64"\n",
162 routes[i].gw, routes[i].lo, routes[i].hi);
167 static int parse_route(char *opteq, char *opttgts)
169 char *gw_lo_ptr, *gw_hi_ptr, *tgt_lo_ptr, *tgt_hi_ptr;
170 ptl_nid_t gw_lo, gw_hi, tgt_lo, tgt_hi;
173 gw_lo_ptr = opteq + 1;
174 if (!(gw_hi_ptr = strchr(gw_lo_ptr, '-'))) {
175 gw_hi_ptr = gw_lo_ptr;
181 if (ptl_parse_nid(&gw_lo, gw_lo_ptr) != 0) {
182 fprintf(stderr, "%s: can't parse NID %s\n", progname,gw_lo_ptr);
186 if (ptl_parse_nid(&gw_hi, gw_hi_ptr) != 0) {
187 fprintf(stderr, "%s: can't parse NID %s\n", progname,gw_hi_ptr);
191 tgt_lo_ptr = opttgts + 1;
192 if (!(tgt_hi_ptr = strchr(tgt_lo_ptr, '-'))) {
193 tgt_hi_ptr = tgt_lo_ptr;
195 tgt_hi_ptr[0] = '\0';
199 if (ptl_parse_nid(&tgt_lo, tgt_lo_ptr) != 0) {
200 fprintf(stderr, "%s: can't parse NID %s\n",progname,tgt_lo_ptr);
204 if (ptl_parse_nid(&tgt_hi, tgt_hi_ptr) != 0) {
205 fprintf(stderr, "%s: can't parse NID %s\n",progname,tgt_hi_ptr);
209 while (gw_lo <= gw_hi) {
210 if (route_index >= MAX_ROUTES) {
211 fprintf(stderr, "%s: to many routes %d\n",
212 progname, MAX_ROUTES);
216 routes[route_index].gw = gw_lo;
217 routes[route_index].lo = tgt_lo;
218 routes[route_index].hi = tgt_hi;
226 static int ignored_option(const char *check)
228 char *ignore[] = { "noatime", "async", "rw", "suid", "dev",
229 "exec", "nouser", "auto", "noauto", "_netdev",
231 char **which = ignore;
233 while (*which != NULL) {
234 if (strcmp(check, *which) == 0)
241 int parse_options(char *options, struct lustre_mount_data *lmd)
243 ptl_nid_t nid = 0, cluster_id = 0;
245 char *opt, *opteq, *opttgts;
247 /* parsing ideas here taken from util-linux/mount/nfsmount.c */
248 for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
249 if ((opteq = strchr(opt, '='))) {
250 val = atoi(opteq + 1);
252 if (!strcmp(opt, "nettype")) {
253 lmd->lmd_nal = ptl_name2nal(opteq + 1);
254 } else if(!strcmp(opt, "cluster_id")) {
255 if (ptl_parse_nid(&cluster_id, opteq+1) != 0) {
256 fprintf(stderr, "%s: can't parse NID "
257 "%s\n", progname, opteq+1);
260 lmd_cluster_id = cluster_id;
261 } else if(!strcmp(opt, "route")) {
262 if (!(opttgts = strchr(opteq + 1, ':'))) {
263 fprintf(stderr, "%s: Route must be "
264 "of the form: route="
265 "<gw>[-<gw>]:<low>[-<high>]\n",
269 parse_route(opteq, opttgts);
270 } else if (!strcmp(opt, "local_nid")) {
271 if (ptl_parse_nid(&nid, opteq + 1) != 0) {
272 fprintf(stderr, "%s: "
273 "can't parse NID %s\n",
278 lmd->lmd_local_nid = nid;
279 } else if (!strcmp(opt, "server_nid")) {
280 if (ptl_parse_nid(&nid, opteq + 1) != 0) {
281 fprintf(stderr, "%s: "
282 "can't parse NID %s\n",
283 progname, opteq + 1);
286 lmd->lmd_server_nid = nid;
287 } else if (!strcmp(opt, "port")) {
290 fprintf(stderr, "%s: unknown option '%s'\n",
295 if (ignored_option(opt))
299 if (!strncmp(opt, "no", 2)) {
303 if (!strcmp(opt, "debug")) { /* deprecated */
306 fprintf(stderr, "%s: unknown option '%s'\n",
316 get_local_elan_id(char *fname, char *buf)
318 FILE *fp = fopen(fname, "r");
324 rc = fscanf(fp, "NodeId %255s", buf);
328 return (rc == 1) ? 0 : -1;
332 set_local(struct lustre_mount_data *lmd)
335 * XXX PtlGetId() will be safer if portals is loaded and
336 * initialised correctly at this time... */
337 char buf[256], *ptr = buf;
341 if (lmd->lmd_local_nid != PTL_NID_ANY)
344 memset(buf, 0, sizeof(buf));
346 switch (lmd->lmd_nal) {
348 fprintf(stderr, "%s: Unknown network type: %d\n",
349 progname, lmd->lmd_nal);
358 rc = gethostname(buf, sizeof(buf) - 1);
360 fprintf (stderr, "%s: can't get local buf: %d\n",
366 char *pfiles[] = {"/proc/qsnet/elan3/device0/position",
367 "/proc/qsnet/elan4/device0/position",
368 "/proc/elan/device0/position",
373 rc = get_local_elan_id(pfiles[i], buf);
374 } while (rc != 0 && pfiles[++i] != NULL);
377 rc = gethostname(buf, sizeof(buf) - 1);
380 while ((*tmp >= 'a' && *tmp <= 'z') ||
381 (*tmp >= 'A' && *tmp <= 'Z'))
383 ptr = strsep(&tmp, ".");
386 "%s: can't read Elan ID from /proc\n",
395 if (ptl_parse_nid (&nid, ptr) != 0) {
396 fprintf (stderr, "%s: can't parse NID %s\n", progname, buf);
400 lmd->lmd_local_nid = nid + lmd_cluster_id;
405 set_peer(char *hostname, struct lustre_mount_data *lmd)
410 switch (lmd->lmd_nal) {
412 fprintf(stderr, "%s: Unknown network type: %d\n",
413 progname, lmd->lmd_nal);
418 if (lmd->lmd_server_nid != PTL_NID_ANY)
420 if (ptl_parse_nid (&nid, hostname) != 0) {
421 fprintf (stderr, "%s: can't parse NID %s\n",
425 lmd->lmd_server_nid = nid;
432 if (lmd->lmd_server_nid == PTL_NID_ANY) {
433 if (ptl_parse_nid (&nid, hostname) != 0) {
434 fprintf (stderr, "%s: can't parse NID %s\n",
438 lmd->lmd_server_nid = nid;
441 if (ptl_parse_ipaddr(&lmd->lmd_server_ipaddr, hostname) != 0) {
442 fprintf (stderr, "%s: can't parse host %s\n",
450 if (lmd->lmd_server_nid != PTL_NID_ANY)
453 rc = sscanf(hostname, "%*[^0-9]%63[0-9]", buf);
455 fprintf (stderr, "%s: can't get elan id from host %s\n",
459 if (ptl_parse_nid (&nid, buf) != 0) {
460 fprintf (stderr, "%s: can't parse NID %s\n",
464 lmd->lmd_server_nid = nid;
474 build_data(char *source, char *options, struct lustre_mount_data *lmd)
477 char *hostname = NULL, *mds = NULL, *profile = NULL, *s;
480 if (lmd_bad_magic(lmd))
483 if (strlen(source) > sizeof(buf) + 1) {
484 fprintf(stderr, "%s: host:/mds/profile argument too long\n",
489 if ((s = strchr(buf, ':'))) {
496 if ((s = strchr(mds, '/'))) {
500 fprintf(stderr, "%s: directory to mount not in "
501 "host:/mds/profile format\n",
506 fprintf(stderr, "%s: "
507 "directory to mount not in host:/mds/profile format\n",
512 rc = parse_options(options, lmd);
520 rc = set_peer(hostname, lmd);
523 if (strlen(mds) > sizeof(lmd->lmd_mds) + 1) {
524 fprintf(stderr, "%s: mds name too long\n", progname);
527 strcpy(lmd->lmd_mds, mds);
529 if (strlen(profile) > sizeof(lmd->lmd_profile) + 1) {
530 fprintf(stderr, "%s: profile name too long\n", progname);
533 strcpy(lmd->lmd_profile, profile);
540 static int set_routes(struct lustre_mount_data *lmd) {
541 struct portals_cfg pcfg;
542 struct portal_ioctl_data data;
543 int i, j, route_exists, rc, err = 0;
545 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
547 for (i = 0; i < route_index; i++) {
549 /* Check for existing routes so as not to add duplicates */
551 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
552 pcfg.pcfg_nal = ROUTER;
555 PORTAL_IOC_INIT(data);
556 data.ioc_pbuf1 = (char*)&pcfg;
557 data.ioc_plen1 = sizeof(pcfg);
558 data.ioc_nid = pcfg.pcfg_nid;
560 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
566 if ((pcfg.pcfg_gw_nal == lmd->lmd_nal) &&
567 (pcfg.pcfg_nid == routes[i].gw) &&
568 (pcfg.pcfg_nid2 == routes[i].lo) &&
569 (pcfg.pcfg_nid3 == routes[i].hi)) {
578 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
579 pcfg.pcfg_nid = routes[i].gw;
580 pcfg.pcfg_nal = ROUTER;
581 pcfg.pcfg_gw_nal = lmd->lmd_nal;
582 pcfg.pcfg_nid2 = MIN(routes[i].lo, routes[i].hi);
583 pcfg.pcfg_nid3 = MAX(routes[i].lo, routes[i].hi);
585 PORTAL_IOC_INIT(data);
586 data.ioc_pbuf1 = (char*)&pcfg;
587 data.ioc_plen1 = sizeof(pcfg);
588 data.ioc_nid = pcfg.pcfg_nid;
590 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
592 fprintf(stderr, "%s: Unable to add route "
593 LPX64" : "LPX64" - "LPX64"\n[%d] %s\n",
594 progname, routes[i].gw, routes[i].lo,
595 routes[i].hi, errno, strerror(errno));
601 unregister_ioc_dev(PORTALS_DEV_ID);
605 int main(int argc, char *const argv[])
607 char *source, *target, *options = "";
608 int i, nargs = 3, opt, rc;
609 struct lustre_mount_data lmd;
610 static struct option long_opt[] = {
614 {"nomtab", 0, 0, 'n'},
615 {"options", 1, 0, 'o'},
616 {"verbose", 0, 0, 'v'},
620 progname = strrchr(argv[0], '/');
621 progname = progname ? progname + 1 : argv[0];
623 while ((opt = getopt_long(argc, argv, "fhno:v", long_opt,NULL)) != EOF){
627 printf("force: %d\n", force);
632 printf("fake: %d\n", fake);
640 printf("nomtab: %d\n", nomtab);
649 printf("verbose: %d\n", verbose);
653 fprintf(stderr, "%s: unknown option '%c'\n",
660 if (optind + 2 > argc) {
661 fprintf(stderr, "%s: too few arguments\n", progname);
665 source = argv[optind];
666 target = argv[optind + 1];
669 for (i = 0; i < argc; i++)
670 printf("arg[%d] = %s\n", i, argv[i]);
671 printf("source = %s, target = %s\n", source, target);
674 if (!force && check_mtab_entry(source, target, "lustre"))
678 rc = build_data(source, options, &lmd);
684 rc = set_routes(&lmd);
689 rc = access(target, F_OK);
692 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
698 rc = mount(source, target, "lustre", 0, (void *)&lmd);
700 fprintf(stderr, "%s: mount(%s, %s) failed: %s\n", source,
701 target, progname, strerror(errno));
703 fprintf(stderr, "Are the lustre modules loaded?\n"
704 "Check /etc/modules.conf and /proc/filesystems\n");
706 } else if (!nomtab) {
707 rc = update_mtab_entry(source, target, "lustre", options,0,0,0);