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