Whamcloud - gitweb
8f20c8eeb48dd5d001251b7ead466738caa52ed8
[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 extern int op_catinfo(char *dir, char *keyword, char *node_name);
47
48 /* all functions */
49 static int lfs_setstripe(int argc, char **argv);
50 static int lfs_find(int argc, char **argv);
51 static int lfs_getstripe(int argc, char **argv);
52 static int lfs_osts(int argc, char **argv);
53 static int lfs_check(int argc, char **argv);
54 static int lfs_catinfo(int argc, char **argv);
55
56 /* all avaialable commands */
57 command_t cmdlist[] = {
58         {"setstripe", lfs_setstripe, 0,
59          "blah...\n"
60          "usage: setstripe <filename> <stripe size> <stripe start> <stripe count>\n"
61          "\tstripe size:  Number of bytes in each stripe (0 default)\n"
62          "\tstripe start: OST index of first stripe (-1 default)\n"
63          "\tstripe count: Number of OSTs to stripe over (0 default)"},
64         {"find", lfs_find, 0,
65          "blah...\n"
66          "usage: find [--obd <uuid>] [--quiet | --verbose] [--recursive] <dir|file> ..."},
67         {"getstripe", lfs_getstripe, 0,
68          "blah...\n"
69          "usage:getstripe <filename>"},
70         {"check", lfs_check, 0, 
71          "blah...\n"
72          "usage: check <osts|mds|servers>"},
73         {"catinfo", lfs_catinfo, 0,
74          "Show information of specified type logs.\n"
75          "usage: catinfo <keyword> [node name]"
76          "keywords are one of followings: config, deletions.\n"
77          "client node name must be provided when use keyword config."},
78         {"osts", lfs_osts, 0, "osts"},
79         {"help", Parser_help, 0, "help"},
80         {"exit", Parser_quit, 0, "quit"},
81         {"quit", Parser_quit, 0, "quit"},
82         { 0, 0, 0, NULL }
83 };
84
85 /* functions */
86 static int lfs_setstripe(int argc, char **argv)
87 {
88         int result;
89         long st_size;
90         int  st_offset, st_count;
91         char *end;
92
93         if (argc != 5)
94                 return CMD_HELP;
95
96         // get the stripe size
97         st_size = strtoul(argv[2], &end, 0);
98         if (*end != '\0') {
99                 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
100                                 argv[0], argv[2]);
101                 return CMD_HELP;
102         }
103         // get the stripe offset
104         st_offset = strtoul(argv[3], &end, 0);
105         if (*end != '\0') {
106                 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
107                                 argv[0], argv[3]);
108                 return CMD_HELP;
109         }
110         // get the stripe count
111         st_count = strtoul(argv[4], &end, 0);
112         if (*end != '\0') {
113                 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
114                                 argv[0], argv[4]);
115                 return CMD_HELP;
116         }
117
118         result = op_create_file(argv[1], st_size, st_offset, st_count);
119         if (result)
120                 fprintf(stderr, "error: %s: create stripe file failed\n",
121                                 argv[0]);
122
123         return result;
124 }
125
126 static int lfs_find(int argc, char **argv)
127 {
128         struct option long_opts[] = {
129                 {"obd", 1, 0, 'o'},
130                 {"quiet", 0, 0, 'q'},
131                 {"recursive", 0, 0, 'r'},
132                 {"verbose", 0, 0, 'v'},
133                 {0, 0, 0, 0}
134         };
135         char short_opts[] = "ho:qrv";
136         int quiet, verbose, recursive, c, rc;
137         struct obd_uuid *obduuid = NULL;
138
139         optind = 0;
140         quiet = verbose = recursive = 0;
141         while ((c = getopt_long(argc, argv, short_opts,
142                                         long_opts, NULL)) != -1) {
143                 switch (c) {
144                 case 'o':
145                         if (obduuid) {
146                                 fprintf(stderr,
147                                         "error: %s: only one obduuid allowed",
148                                         argv[0]);
149                                 return CMD_HELP;
150                         }
151                         obduuid = (struct obd_uuid *)optarg;
152                         break;
153                 case 'q':
154                         quiet++;
155                         verbose = 0;
156                         break;
157                 case 'r':
158                         recursive = 1;
159                         break;
160                 case 'v':
161                         verbose++;
162                         quiet = 0;
163                         break;
164                 case '?':
165                         return CMD_HELP;
166                         break;
167                 default:
168                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
169                                 argv[0], argv[optind - 1]);
170                         return CMD_HELP;
171                         break;
172                 }
173         }
174
175         if (optind >= argc)
176                 return CMD_HELP;
177
178         do {
179                 rc = op_find(argv[optind], obduuid, recursive, verbose, quiet);
180         } while (++optind < argc && !rc);
181
182         if (rc)
183                 fprintf(stderr, "error: %s: find failed\n", argv[0]);
184         return rc;
185 }
186
187 static int lfs_getstripe(int argc, char **argv)
188 {
189         struct obd_uuid *obduuid = NULL;
190         int rc;
191
192         if (argc != 2)
193                 return CMD_HELP;
194
195         optind = 1;
196
197         do {
198                 rc = op_find(argv[optind], obduuid, 0, 0, 0);
199         } while (++optind < argc && !rc);
200
201         if (rc)
202                 fprintf(stderr, "error: %s: getstripe failed for %s\n",
203                         argv[0], argv[1]);
204
205         return rc;
206 }
207
208 static int lfs_osts(int argc, char **argv)
209 {
210         FILE *fp;
211         struct mntent *mnt = NULL;
212         struct obd_uuid *obduuid = NULL;
213         int rc=0;
214                                                                                                                              
215         if (argc != 1)
216                 return CMD_HELP;
217
218         fp = setmntent(MOUNTED, "r");
219
220         if (fp == NULL) {
221                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
222                         strerror (errno));
223         } else {
224                 mnt = getmntent(fp);
225                 while (feof(fp) == 0 && ferror(fp) ==0) {
226                         if (strcmp(mnt->mnt_type, "lustre_lite") == 0) {
227                                 rc = op_find(mnt->mnt_dir, obduuid, 0, 0, 0);
228                                 if (rc)
229                                         fprintf(stderr, "error: lfs osts failed for %s\n",
230                                                 mnt->mnt_dir);
231                         }
232                         mnt = getmntent(fp);
233                 }
234                 endmntent(fp);
235         }
236                                                                                                                              
237         return rc;
238 }
239
240 static int lfs_check(int argc, char **argv)
241 {
242         int rc;
243         FILE *fp;
244         struct mntent *mnt = NULL;
245         int type_num = 1;
246         char *obd_type_p[2];
247         char obd_type1[4];
248         char obd_type2[4];
249
250         if (argc != 2)
251                 return CMD_HELP;
252
253         obd_type_p[1]=obd_type1;
254         obd_type_p[2]=obd_type2;
255
256         if (strcmp(argv[1],"osts")==0) {
257                 strcpy(obd_type_p[0],"osc");
258         } else if (strcmp(argv[1],"mds")==0) {
259                 strcpy(obd_type_p[0],"mdc");
260         } else if (strcmp(argv[1],"servers")==0) {
261                 type_num=2;
262                 strcpy(obd_type_p[0],"osc");
263                 strcpy(obd_type_p[1],"mdc");
264         } else {
265                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
266                                 argv[0], argv[1]);
267                         return CMD_HELP;
268         }
269
270         fp = setmntent(MOUNTED, "r");
271         if (fp == NULL) {
272                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
273                         strerror (errno));
274         } else {
275                 mnt = getmntent(fp);
276                 while (feof(fp) == 0 && ferror(fp) ==0) {
277                         if (strcmp(mnt->mnt_type, "lustre_lite") == 0) 
278                                 break;
279                         mnt = getmntent(fp);
280                 }
281                 endmntent(fp);
282         }
283            
284         rc = op_check(type_num,obd_type_p,mnt->mnt_dir);
285
286         if (rc)
287                 fprintf(stderr, "error: %s: %s status failed\n",
288                                 argv[0],argv[1]);
289                                                                                                                              
290         return rc;
291
292 }
293
294 static int lfs_catinfo(int argc, char **argv)
295 {
296         FILE *fp;
297         struct mntent *mnt = NULL;
298         int rc;
299         
300         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
301                 return CMD_HELP;
302
303         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
304                 return CMD_HELP;
305
306         fp = setmntent(MOUNTED, "r");
307         if (fp == NULL) {
308                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED, 
309                          strerror(errno));
310         } else {
311                 mnt = getmntent(fp);
312                 while (feof(fp) == 0 && ferror(fp) == 0) {
313                         if (strcmp(mnt->mnt_type, "lustre_lite") == 0) 
314                                 break;
315                         mnt = getmntent(fp);
316                 }
317                 endmntent(fp);
318         }
319
320         if (mnt) {
321                 if (argc == 3)
322                         rc = op_catinfo(mnt->mnt_dir, argv[1], argv[2]);
323                 else
324                         rc = op_catinfo(mnt->mnt_dir, argv[1], NULL);
325         } else {
326                 fprintf(stderr, "no lustre_lite mounted.\n");
327                 rc = -1;
328         }
329
330         return rc;
331 }
332
333 int main(int argc, char **argv)
334 {
335         int rc;
336
337         setlinebuf(stdout);
338                                                                                                                              
339         ptl_initialize(argc, argv);
340         if (obd_initialize(argc, argv) < 0)
341                 exit(2);
342         if (dbg_initialize(argc, argv) < 0)
343                 exit(3);
344                                                                                                                              
345         Parser_init("lfs > ", cmdlist);
346
347         if (argc > 1) {
348                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
349         } else {
350                 rc = Parser_commands();
351         }
352
353         obd_finalize(argc, argv);
354         return rc;
355 }