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.
31 #include <sys/mount.h>
35 #include <sys/utsname.h>
40 #include <portals/ptlctl.h>
46 static char *progname = NULL;
54 #define MAX_ROUTES 1024
56 ptl_nid_t lmd_cluster_id = 0;
57 llmount_route_t routes[MAX_ROUTES];
59 static int check_mtab_entry(char *spec, char *mtpt, char *type)
65 fp = setmntent(MOUNTED, "r");
69 while ((mnt = getmntent(fp)) != NULL) {
70 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
71 strcmp(mnt->mnt_dir, mtpt) == 0 &&
72 strcmp(mnt->mnt_type, type) == 0) {
73 fprintf(stderr, "%s: according to %s %s is "
74 "already mounted on %s\n",
75 progname, MOUNTED, spec, mtpt);
76 return(1); /* or should we return an error? */
85 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
86 int flags, int freq, int pass)
91 mnt.mnt_fsname = spec;
94 mnt.mnt_opts = opts ? opts : "";
96 mnt.mnt_passno = pass;
99 fp = setmntent(MOUNTED, "a+");
101 fprintf(stderr, "%s: setmntent(%s): %s:",
102 progname, MOUNTED, strerror (errno));
104 if ((addmntent (fp, &mnt)) == 1) {
105 fprintf(stderr, "%s: addmntent: %s:",
106 progname, strerror (errno));
114 init_options(struct lustre_mount_data *lmd)
116 memset(lmd, 0, sizeof(*lmd));
117 lmd->lmd_magic = LMD_MAGIC;
118 lmd->lmd_server_nid = PTL_NID_ANY;
119 lmd->lmd_local_nid = PTL_NID_ANY;
120 lmd->lmd_port = 988; /* XXX define LUSTRE_DEFAULT_PORT */
121 lmd->lmd_nal = SOCKNAL;
125 strncpy(lmd->lmd_security, "null", sizeof(lmd->lmd_security));
130 print_options(struct lustre_mount_data *lmd)
134 printf("mds: %s\n", lmd->lmd_mds);
135 printf("profile: %s\n", lmd->lmd_profile);
136 printf("sec_flavor: %s\n", lmd->lmd_security);
137 printf("server_nid: "LPX64"\n", lmd->lmd_server_nid);
139 if (lmd->lmd_nal != CRAY_KB_SSNAL) {
141 printf("local_nid: "LPX64"\n", lmd->lmd_local_nid);
145 printf("nal: %x\n", lmd->lmd_nal);
147 if (lmd->lmd_nal != CRAY_KB_SSNAL) {
149 printf("server_ipaddr: 0x%x\n", lmd->lmd_server_ipaddr);
150 printf("port: %d\n", lmd->lmd_port);
155 for (i = 0; i < route_index; i++)
156 printf("route: "LPX64" : "LPX64" - "LPX64"\n",
157 routes[i].gw, routes[i].lo, routes[i].hi);
162 static int parse_route(char *opteq, char *opttgts)
164 char *gw_lo_ptr, *gw_hi_ptr, *tgt_lo_ptr, *tgt_hi_ptr;
165 ptl_nid_t gw_lo, gw_hi, tgt_lo, tgt_hi;
168 gw_lo_ptr = opteq + 1;
169 if (!(gw_hi_ptr = strchr(gw_lo_ptr, '-'))) {
170 gw_hi_ptr = gw_lo_ptr;
176 if (ptl_parse_nid(&gw_lo, gw_lo_ptr) != 0) {
177 fprintf(stderr, "%s: can't parse NID %s\n", progname,gw_lo_ptr);
181 if (ptl_parse_nid(&gw_hi, gw_hi_ptr) != 0) {
182 fprintf(stderr, "%s: can't parse NID %s\n", progname,gw_hi_ptr);
186 tgt_lo_ptr = opttgts + 1;
187 if (!(tgt_hi_ptr = strchr(tgt_lo_ptr, '-'))) {
188 tgt_hi_ptr = tgt_lo_ptr;
190 tgt_hi_ptr[0] = '\0';
194 if (ptl_parse_nid(&tgt_lo, tgt_lo_ptr) != 0) {
195 fprintf(stderr, "%s: can't parse NID %s\n",progname,tgt_lo_ptr);
199 if (ptl_parse_nid(&tgt_hi, tgt_hi_ptr) != 0) {
200 fprintf(stderr, "%s: can't parse NID %s\n",progname,tgt_hi_ptr);
204 while (gw_lo <= gw_hi) {
205 if (route_index >= MAX_ROUTES) {
206 fprintf(stderr, "%s: to many routes %d\n",
207 progname, MAX_ROUTES);
211 routes[route_index].gw = gw_lo;
212 routes[route_index].lo = tgt_lo;
213 routes[route_index].hi = tgt_hi;
222 * here all what we do is gurantee the result is exactly
223 * what user intend to get, no ambiguous. maybe there have
224 * simpler library call could do the same job for us?
226 static int parse_u32(char *str, uint32_t *res)
231 id = strtol(str, &endptr, 0);
232 if (endptr && *endptr != 0)
235 if (id == LONG_MAX || id == LONG_MIN)
238 if ((uint32_t)id != id)
241 *res = (uint32_t) id;
245 static int parse_nllu(struct lustre_mount_data *lmd, char *str_nllu)
249 if (parse_u32(str_nllu, &lmd->lmd_nllu) == 0)
252 pass = getpwnam(str_nllu);
256 lmd->lmd_nllu = pass->pw_uid;
260 static int parse_nllg(struct lustre_mount_data *lmd, char *str_nllg)
264 if (parse_u32(str_nllg, &lmd->lmd_nllg) == 0)
267 grp = getgrnam(str_nllg);
271 lmd->lmd_nllg = grp->gr_gid;
275 int parse_options(char * options, struct lustre_mount_data *lmd)
277 ptl_nid_t nid = 0, cluster_id = 0;
279 char *opt, *opteq, *opttgts;
281 /* parsing ideas here taken from util-linux/mount/nfsmount.c */
282 for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
283 if ((opteq = strchr(opt, '='))) {
284 val = atoi(opteq + 1);
286 if (!strcmp(opt, "nettype")) {
287 lmd->lmd_nal = ptl_name2nal(opteq + 1);
288 } else if(!strcmp(opt, "cluster_id")) {
289 if (ptl_parse_nid(&cluster_id, opteq+1) != 0) {
290 fprintf (stderr, "%s: can't parse NID "
291 "%s\n", progname, opteq+1);
294 lmd_cluster_id = cluster_id;
295 } else if(!strcmp(opt, "route")) {
296 if (!(opttgts = strchr(opteq + 1, ':'))) {
297 fprintf(stderr, "%s: Route must be "
298 "of the form: route="
299 "<gw>[-<gw>]:<low>[-<high>]\n",
303 parse_route(opteq, opttgts);
304 } else if (!strcmp(opt, "local_nid")) {
305 if (ptl_parse_nid(&nid, opteq + 1) != 0) {
306 fprintf (stderr, "%s: "
307 "can't parse NID %s\n",
312 lmd->lmd_local_nid = nid;
313 } else if (!strcmp(opt, "server_nid")) {
314 if (ptl_parse_nid(&nid, opteq + 1) != 0) {
315 fprintf (stderr, "%s: "
316 "can't parse NID %s\n",
317 progname, opteq + 1);
320 lmd->lmd_server_nid = nid;
321 } else if (!strcmp(opt, "port")) {
323 } else if (!strcmp(opt, "sec")) {
324 strncpy(lmd->lmd_security, opteq + 1,
325 sizeof(lmd->lmd_security));
326 } else if (!strcmp(opt, "nllu")) {
327 if (parse_nllu(lmd, opteq + 1)) {
328 fprintf(stderr, "%s: "
329 "can't parse user: %s\n",
330 progname, opteq + 1);
333 } else if (!strcmp(opt, "nllg")) {
334 if (parse_nllg(lmd, opteq + 1)) {
335 fprintf(stderr, "%s: "
336 "can't parse group: %s\n",
337 progname, opteq + 1);
343 if (!strncmp(opt, "no", 2)) {
347 if (!strcmp(opt, "debug")) {
349 } else if (!strcmp(opt, "async")) {
358 get_local_elan_id(char *fname, char *buf)
360 FILE *fp = fopen(fname, "r");
366 rc = fscanf(fp, "NodeId %255s", buf);
370 return (rc == 1) ? 0 : -1;
374 set_local(struct lustre_mount_data *lmd)
377 * XXX PtlGetId() will be safer if portals is loaded and
378 * initialised correctly at this time... */
383 if (lmd->lmd_local_nid != PTL_NID_ANY)
386 memset(buf, 0, sizeof(buf));
389 if (lmd->lmd_nal == CRAY_KB_ERNAL) {
391 if (lmd->lmd_nal == SOCKNAL || lmd->lmd_nal == TCPNAL ||
392 lmd->lmd_nal == OPENIBNAL || lmd->lmd_nal == IIBNAL) {
396 rc = gethostname(buf, sizeof(buf) - 1);
398 fprintf(stderr, "%s: can't get hostname: %s\n",
399 progname, strerror(rc));
404 /* for 2.6 kernels, reserve at least 8MB free, or we will
405 * go OOM during heavy read load */
406 if (rc == 0 && strncmp(uts.release, "2.6", 3) == 0) {
407 int f, minfree = 32768;
408 char name[40], val[40];
411 meminfo = fopen("/proc/meminfo", "r");
412 if (meminfo != NULL) {
413 while (fscanf(meminfo, "%s %s %*s\n", name, val) != EOF) {
414 if (strcmp(name, "MemTotal:") == 0) {
415 f = strtol(val, NULL, 0);
416 if (f > 0 && f < 8 * minfree)
423 f = open("/proc/sys/vm/min_free_kbytes", O_WRONLY);
425 sprintf(val, "%d", minfree);
426 write(f, val, strlen(val));
431 } else if (lmd->lmd_nal == QSWNAL) {
432 char *pfiles[] = {"/proc/qsnet/elan3/device0/position",
433 "/proc/qsnet/elan4/device0/position",
434 "/proc/elan/device0/position",
439 rc = get_local_elan_id(pfiles[i], buf);
440 } while (rc != 0 && pfiles[++i] != NULL);
443 fprintf(stderr, "%s: can't read Elan ID from /proc\n",
449 } else if (lmd->lmd_nal == CRAY_KB_SSNAL) {
454 if (ptl_parse_nid (&nid, buf) != 0) {
455 fprintf (stderr, "%s: can't parse NID %s\n", progname, buf);
459 lmd->lmd_local_nid = nid + lmd_cluster_id;
464 set_peer(char *hostname, struct lustre_mount_data *lmd)
470 if (lmd->lmd_nal == CRAY_KB_ERNAL) {
472 if (lmd->lmd_nal == SOCKNAL || lmd->lmd_nal == TCPNAL ||
473 lmd->lmd_nal == OPENIBNAL || lmd->lmd_nal == IIBNAL) {
475 if (lmd->lmd_server_nid == PTL_NID_ANY) {
476 if (ptl_parse_nid (&nid, hostname) != 0) {
477 fprintf (stderr, "%s: can't parse NID %s\n",
481 lmd->lmd_server_nid = nid;
484 if (ptl_parse_ipaddr(&lmd->lmd_server_ipaddr, hostname) != 0) {
485 fprintf (stderr, "%s: can't parse host %s\n",
490 } else if (lmd->lmd_nal == QSWNAL &&lmd->lmd_server_nid == PTL_NID_ANY){
492 rc = sscanf(hostname, "%*[^0-9]%63[0-9]", buf);
494 fprintf (stderr, "%s: can't get elan id from host %s\n",
498 if (ptl_parse_nid (&nid, buf) != 0) {
499 fprintf (stderr, "%s: can't parse NID %s\n",
503 lmd->lmd_server_nid = nid;
505 } else if (lmd->lmd_nal == CRAY_KB_SSNAL) {
506 lmd->lmd_server_nid = strtoll(hostname,0,0);
515 build_data(char *source, char *options, struct lustre_mount_data *lmd)
518 char *hostname = NULL, *mds = NULL, *profile = NULL, *s;
521 if (lmd_bad_magic(lmd))
524 if (strlen(source) > sizeof(buf) + 1) {
525 fprintf(stderr, "%s: host:/mds/profile argument too long\n",
530 if ((s = strchr(buf, ':'))) {
537 if ((s = strchr(mds, '/'))) {
541 fprintf(stderr, "%s: directory to mount not in "
542 "host:/mds/profile format\n",
547 fprintf(stderr, "%s: "
548 "directory to mount not in host:/mds/profile format\n",
553 rc = parse_options(options, lmd);
561 rc = set_peer(hostname, lmd);
564 if (strlen(mds) > sizeof(lmd->lmd_mds) + 1) {
565 fprintf(stderr, "%s: mds name too long\n", progname);
568 strcpy(lmd->lmd_mds, mds);
570 if (strlen(profile) > sizeof(lmd->lmd_profile) + 1) {
571 fprintf(stderr, "%s: profile name too long\n", progname);
574 strcpy(lmd->lmd_profile, profile);
581 static int set_routes(struct lustre_mount_data *lmd) {
582 struct portals_cfg pcfg;
583 struct portal_ioctl_data data;
584 int i, j, route_exists, rc, err = 0;
586 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
588 for (i = 0; i < route_index; i++) {
590 /* Check for existing routes so as not to add duplicates */
592 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
593 pcfg.pcfg_nal = ROUTER;
596 PORTAL_IOC_INIT(data);
597 data.ioc_pbuf1 = (char*)&pcfg;
598 data.ioc_plen1 = sizeof(pcfg);
599 data.ioc_nid = pcfg.pcfg_nid;
601 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
607 if ((pcfg.pcfg_gw_nal == lmd->lmd_nal) &&
608 (pcfg.pcfg_nid == routes[i].gw) &&
609 (pcfg.pcfg_nid2 == routes[i].lo) &&
610 (pcfg.pcfg_nid3 == routes[i].hi)) {
619 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
620 pcfg.pcfg_nid = routes[i].gw;
621 pcfg.pcfg_nal = ROUTER;
622 pcfg.pcfg_gw_nal = lmd->lmd_nal;
623 pcfg.pcfg_nid2 = MIN(routes[i].lo, routes[i].hi);
624 pcfg.pcfg_nid3 = MAX(routes[i].lo, routes[i].hi);
626 PORTAL_IOC_INIT(data);
627 data.ioc_pbuf1 = (char*)&pcfg;
628 data.ioc_plen1 = sizeof(pcfg);
629 data.ioc_nid = pcfg.pcfg_nid;
631 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
633 fprintf(stderr, "%s: Unable to add route "
634 LPX64" : "LPX64" - "LPX64"\n[%d] %s\n",
635 progname, routes[i].gw, routes[i].lo,
636 routes[i].hi, errno, strerror(errno));
642 unregister_ioc_dev(PORTALS_DEV_ID);
646 void usage(FILE *out)
648 fprintf(out, "usage: %s <source> <target> [-f] [-v] [-n] [-o mntopt]\n",
653 int main(int argc, char *const argv[])
655 char *source, *target, *options = "";
656 int i, nargs = 3, opt, rc;
657 struct lustre_mount_data lmd;
658 static struct option long_opt[] = {
659 {"force", 0, 0, 'f'},
661 {"nomtab", 0, 0, 'n'},
662 {"options", 1, 0, 'o'},
663 {"verbose", 0, 0, 'v'},
667 progname = strrchr(argv[0], '/');
668 progname = progname ? progname + 1 : argv[0];
670 while ((opt = getopt_long(argc, argv, "fno:v", long_opt, NULL)) != EOF){
674 printf("force: %d\n", force);
682 printf("nomtab: %d\n", nomtab);
691 printf("verbose: %d\n", verbose);
695 fprintf(stderr, "%s: unknown option '%c'\n",
702 if (optind + 2 > argc) {
703 fprintf(stderr, "%s: too few arguments\n", progname);
707 source = argv[optind];
708 target = argv[optind + 1];
711 for (i = 0; i < argc; i++)
712 printf("arg[%d] = %s\n", i, argv[i]);
713 printf("source = %s, target = %s\n", source, target);
716 if (check_mtab_entry(source, target, "lustre"))
720 rc = build_data(source, options, &lmd);
725 rc = set_routes(&lmd);
731 printf("%s: debug mode, not mounting\n", progname);
735 rc = access(target, F_OK);
738 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
743 rc = mount(source, target, "lustre", 0, (void *)&lmd);
747 fprintf(stderr, "%s: mount(%s, %s) failed: %s\n", source,
748 target, progname, strerror(errno));
750 fprintf(stderr, "Are the lustre modules loaded?\n"
751 "Check /etc/modules.conf and /proc/filesystems\n");
754 update_mtab_entry(source, target, "lustre", options, 0, 0, 0);