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