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