Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / utils / lfs.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: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Robert Read <rread@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <mntent.h>
32 #include <portals/api-support.h>
33 #include <portals/ptlctl.h>
34
35 #include <liblustre.h>
36 #include <linux/lustre_idl.h>
37
38 #include "parser.h"
39 #include "obdctl.h"
40
41 extern int op_create_file(char *name, long stripe_size, int stripe_offset,
42                 int stripe_count);
43 extern int op_find(char *path, struct obd_uuid *obduuid, int recursive,
44                 int verbose, int quiet);
45 extern int op_check(int type_num, char **obd_type_p, char *dir);
46
47 /* all functions */
48 static int lfs_setstripe(int argc, char **argv);
49 static int lfs_find(int argc, char **argv);
50 static int lfs_getstripe(int argc, char **argv);
51 static int lfs_osts(int argc, char **argv);
52 static int lfs_check(int argc, char **argv);
53
54 /* all avaialable commands */
55 command_t cmdlist[] = {
56         {"setstripe", lfs_setstripe, 0,
57          "blah...\n"
58          "usage: setstripe <filename> <stripe size> <stripe start> <stripe count>\n"
59          "\tstripe size:  Number of bytes in each stripe (0 default)\n"
60          "\tstripe start: OST index of first stripe (-1 default)\n"
61          "\tstripe count: Number of OSTs to stripe over (0 default)"},
62         {"find", lfs_find, 0,
63          "blah...\n"
64          "usage: find [--obd <uuid>] [--quiet | --verbose] [--recursive] <dir|file> ..."},
65         {"getstripe", lfs_getstripe, 0,
66          "blah...\n"
67          "usage:getstripe <filename>"},
68         {"check", lfs_check, 0, 
69          "blah...\n"
70          "usage: check <osts|mds|servers>"},
71         {"osts", lfs_osts, 0, "osts"},
72         {"help", Parser_help, 0, "help"},
73         {"exit", Parser_quit, 0, "quit"},
74         {"quit", Parser_quit, 0, "quit"},
75         { 0, 0, 0, NULL }
76 };
77
78 /* functions */
79 static int lfs_setstripe(int argc, char **argv)
80 {
81         int result;
82         long st_size;
83         int  st_offset, st_count;
84         char *end;
85
86         if (argc != 5)
87                 return CMD_HELP;
88
89         // get the stripe size
90         st_size = strtoul(argv[2], &end, 0);
91         if (*end != '\0') {
92                 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
93                                 argv[0], argv[2]);
94                 return CMD_HELP;
95         }
96         // get the stripe offset
97         st_offset = strtoul(argv[3], &end, 0);
98         if (*end != '\0') {
99                 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
100                                 argv[0], argv[3]);
101                 return CMD_HELP;
102         }
103         // get the stripe count
104         st_count = strtoul(argv[4], &end, 0);
105         if (*end != '\0') {
106                 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
107                                 argv[0], argv[4]);
108                 return CMD_HELP;
109         }
110
111         result = op_create_file(argv[1], st_size, st_offset, st_count);
112         if (result)
113                 fprintf(stderr, "error: %s: create stripe file failed\n",
114                                 argv[0]);
115
116         return result;
117 }
118
119 static int lfs_find(int argc, char **argv)
120 {
121         struct option long_opts[] = {
122                 {"obd", 1, 0, 'o'},
123                 {"quiet", 0, 0, 'q'},
124                 {"recursive", 0, 0, 'r'},
125                 {"verbose", 0, 0, 'v'},
126                 {0, 0, 0, 0}
127         };
128         char short_opts[] = "ho:qrv";
129         int quiet, verbose, recursive, c, rc;
130         struct obd_uuid *obduuid = NULL;
131
132         optind = 0;
133         quiet = verbose = recursive = 0;
134         while ((c = getopt_long(argc, argv, short_opts,
135                                         long_opts, NULL)) != -1) {
136                 switch (c) {
137                 case 'o':
138                         if (obduuid) {
139                                 fprintf(stderr,
140                                         "error: %s: only one obduuid allowed",
141                                         argv[0]);
142                                 return CMD_HELP;
143                         }
144                         obduuid = (struct obd_uuid *)optarg;
145                         break;
146                 case 'q':
147                         quiet++;
148                         verbose = 0;
149                         break;
150                 case 'r':
151                         recursive = 1;
152                         break;
153                 case 'v':
154                         verbose++;
155                         quiet = 0;
156                         break;
157                 case '?':
158                         return CMD_HELP;
159                         break;
160                 default:
161                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
162                                 argv[0], argv[optind - 1]);
163                         return CMD_HELP;
164                         break;
165                 }
166         }
167
168         if (optind >= argc)
169                 return CMD_HELP;
170
171         do {
172                 rc = op_find(argv[optind], obduuid, recursive, verbose, quiet);
173         } while (++optind < argc && !rc);
174
175         if (rc)
176                 fprintf(stderr, "error: %s: find failed\n", argv[0]);
177         return rc;
178 }
179
180 static int lfs_getstripe(int argc, char **argv)
181 {
182         struct obd_uuid *obduuid = NULL;
183         int rc;
184
185         if (argc != 2)
186                 return CMD_HELP;
187
188         optind = 1;
189
190         do {
191                 rc = op_find(argv[optind], obduuid, 0, 0, 0);
192         } while (++optind < argc && !rc);
193
194         if (rc)
195                 fprintf(stderr, "error: %s: getstripe failed for %s\n",
196                         argv[0], argv[1]);
197
198         return rc;
199 }
200
201 static int lfs_osts(int argc, char **argv)
202 {
203         FILE *fp;
204         struct mntent *mnt = NULL;
205         struct obd_uuid *obduuid = NULL;
206         int rc=0;
207                                                                                                                              
208         if (argc != 1)
209                 return CMD_HELP;
210
211         fp = setmntent(MOUNTED, "r");
212
213         if (fp == NULL) {
214                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
215                         strerror (errno));
216         } else {
217                 mnt = getmntent(fp);
218                 while (feof(fp) == 0 && ferror(fp) ==0) {
219                         if (strcmp(mnt->mnt_type, "lustre_lite") == 0) {
220                                 rc = op_find(mnt->mnt_dir, obduuid, 0, 0, 0);
221                                 if (rc)
222                                         fprintf(stderr, "error: lfs osts failed for %s\n",
223                                                 mnt->mnt_dir);
224                         }
225                         mnt = getmntent(fp);
226                 }
227                 endmntent(fp);
228         }
229                                                                                                                              
230         return rc;
231 }
232
233 static int lfs_check(int argc, char **argv)
234 {
235         int rc;
236         FILE *fp;
237         struct mntent *mnt = NULL;
238         int type_num = 1;
239         char *obd_type_p[2];
240         char obd_type1[4];
241         char obd_type2[4];
242
243         if (argc != 2)
244                 return CMD_HELP;
245
246         obd_type_p[1]=obd_type1;
247         obd_type_p[2]=obd_type2;
248
249         if (strcmp(argv[1],"osts")==0) {
250                 strcpy(obd_type_p[0],"osc");
251         } else if (strcmp(argv[1],"mds")==0) {
252                 strcpy(obd_type_p[0],"mdc");
253         } else if (strcmp(argv[1],"servers")==0) {
254                 type_num=2;
255                 strcpy(obd_type_p[0],"osc");
256                 strcpy(obd_type_p[1],"mdc");
257         } else {
258                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
259                                 argv[0], argv[1]);
260                         return CMD_HELP;
261         }
262
263         fp = setmntent(MOUNTED, "r");
264         if (fp == NULL) {
265                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
266                         strerror (errno));
267         } else {
268                 mnt = getmntent(fp);
269                 while (feof(fp) == 0 && ferror(fp) ==0) {
270                         if (strcmp(mnt->mnt_type, "lustre_lite") == 0) 
271                                 break;
272                         mnt = getmntent(fp);
273                 }
274                 endmntent(fp);
275         }
276            
277         rc = op_check(type_num,obd_type_p,mnt->mnt_dir);
278
279         if (rc)
280                 fprintf(stderr, "error: %s: %s status failed\n",
281                                 argv[0],argv[1]);
282                                                                                                                              
283         return rc;
284
285 }
286
287 int main(int argc, char **argv)
288 {
289         int rc;
290
291         setlinebuf(stdout);
292                                                                                                                              
293         ptl_initialize(argc, argv);
294         if (obd_initialize(argc, argv) < 0)
295                 exit(2);
296         if (dbg_initialize(argc, argv) < 0)
297                 exit(3);
298                                                                                                                              
299         Parser_init("lfs > ", cmdlist);
300
301         if (argc > 1) {
302                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
303         } else {
304                 rc = Parser_commands();
305         }
306
307         obd_finalize(argc, argv);
308         return rc;
309 }