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