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