4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
29 * lustre/utils/lshowmount.h
31 * Author: Herb Wartens <wartens2@llnl.gov>
32 * Author: Jim Garlick <garlick@llnl.gov>
47 #include <arpa/inet.h>
52 #include <linux/lustre/lustre_user.h>
54 #include <lustre/lustreapi.h>
55 #include <libcfs/util/param.h>
57 #define PROC_UUID_TMPL "%s/%s/uuid"
59 static void print_nids(NIDList nidlist, int lookup, int enumerate, int indent);
60 static int lshowmount(int lookup, int enumerate, int verbose);
61 static void read_exports(char *exports, NIDList nidlist);
65 #define OPTIONS "ehlv"
66 static struct option long_opts[] = {
67 { .val = 'e', .name = "enumerate", .has_arg = no_argument },
68 { .val = 'h', .name = "help", .has_arg = no_argument },
69 { .val = 'l', .name = "lookup", .has_arg = no_argument },
70 { .val = 'v', .name = "verbose", .has_arg = no_argument },
73 static void usage(void)
75 fprintf(stderr, "usage: %s [-e] [-h] [-l] [-v]\n", prog);
79 int main(int argc, char **argv)
86 prog = basename(argv[0]);
88 while ((opt = getopt_long(argc, argv, OPTIONS, long_opts,
91 case 'e': /* --enumerate */
94 case 'l': /* --lookup */
97 case 'v': /* --verbose */
100 case 'h': /* --help */
106 if (lshowmount(lopt, eopt, vopt) == 0) {
107 fprintf(stderr, "%s: lustre server modules not loaded\n", prog);
114 static void print_expname(const char *path)
116 char *hp, buf[PATH_MAX + 1];
118 strncpy(buf, path, PATH_MAX);
119 buf[PATH_MAX] = '\0';
120 hp = strstr(buf, "exports");
121 if (hp && hp > buf) {
123 for (; *hp == '/' && hp > buf; hp--)
125 for (; *hp != '/' && hp > buf; hp--)
127 printf("%s:\n", hp + 1);
131 static void print_nids(NIDList nidlist, int lookup, int enumerate, int indent)
133 char *s, *sep = "\n", *pfx = "";
136 nl_lookup_ip(nidlist);
139 if (nl_count(nidlist) > 0) {
145 s = nl_string(nidlist, sep);
147 s = nl_xstring(nidlist, sep);
148 printf("%s%s\n", pfx, s);
153 static int lshowmount(int lookup, int enumerate, int verbose)
155 NIDList nidlist = NULL;
159 i = cfs_get_param_paths(&exp_list, "{mgs,mdt,obdfilter}/*/exports");
163 nidlist = nl_create();
164 for (i = 0; i < exp_list.gl_pathc; i++) {
166 nidlist = nl_create();
167 read_exports(exp_list.gl_pathv[i], nidlist);
168 print_expname(exp_list.gl_pathv[i]);
169 print_nids(nidlist, lookup, enumerate, 1);
172 read_exports(exp_list.gl_pathv[i], nidlist);
175 print_nids(nidlist, lookup, enumerate, 0);
178 cfs_free_param_data(&exp_list);
182 static int empty_proc_file(char *path)
188 fd = open(path, O_RDONLY);
189 if (fd < 0 || read(fd, buf, sizeof(buf)) <= 0)
196 static void read_exports(char *exports, NIDList nidlist)
200 char path[PATH_MAX + 1];
202 dirp = opendir(exports);
204 while ((dp = readdir(dirp))) {
205 if (dp->d_type != DT_DIR)
207 if (!strcmp(dp->d_name, "."))
209 if (!strcmp(dp->d_name, ".."))
211 if (strchr(dp->d_name, '@') == NULL)
213 snprintf(path, sizeof(path), PROC_UUID_TMPL, exports,
215 if (empty_proc_file(path))
218 nl_add(nidlist, dp->d_name);