Whamcloud - gitweb
- landed b_hd_cray_merge3
[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_showfid(int argc, char **argv);
48 static int lfs_osts(int argc, char **argv);
49 static int lfs_check(int argc, char **argv);
50 static int lfs_catinfo(int argc, char **argv);
51
52 /* all avaialable commands */
53 command_t cmdlist[] = {
54         {"setstripe", lfs_setstripe, 0,
55          "Create a new file with a specific striping pattern or\n"
56          "set the default striping pattern on an existing directory or\n"
57          "delete the default striping pattern from an existing directory\n"
58          "usage: setstripe <filename|dirname> <stripe size> <stripe start> <stripe count>\n"
59          "       or \n"
60          "       setstripe -d <dirname>\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         {"find", lfs_find, 0,
70          "To list the extended attributes for a given filename or files in a\n"
71          "directory or recursively for all files in a directory tree.\n"
72          "usage: find [--obd <uuid>] [--quiet | --verbose] [--recursive] <dir|file> ..."},
73         {"getstripe", lfs_getstripe, 0,
74          "To list the striping pattern for given filename.\n"
75          "usage: getstripe <filename>"},
76         {"showfid", lfs_showfid, 0,
77          "To list the fid and store cookie for given filename.\n"
78          "usage: showfid [--quiet | --verbose] [--recursive] <dir|file> ..."},
79         {"check", lfs_check, 0,
80          "Display the status of MDS or OSTs (as specified in the command)\n"
81          "or all the servers (MDS and OSTs).\n"
82          "usage: check <osts|mds|servers>"},
83         {"catinfo", lfs_catinfo, 0,
84          "Show information of specified type logs.\n"
85          "usage: catinfo {keyword} [node name]\n"
86          "\tkeywords are one of followings: config, deletions.\n"
87          "\tnode name must be provided when use keyword config."},
88         {"osts", lfs_osts, 0, "osts"},
89         {"help", Parser_help, 0, "help"},
90         {"exit", Parser_quit, 0, "quit"},
91         {"quit", Parser_quit, 0, "quit"},
92         { 0, 0, 0, NULL }
93 };
94
95 /* functions */
96 static int lfs_setstripe(int argc, char **argv)
97 {
98         char *fname;
99         int result;
100         long st_size = 0;
101         int  st_offset = -1, st_count = 0;
102         char *end;
103
104         if (argc != 5 && argc != 3)
105                 return CMD_HELP;
106
107         if (argc == 3) {
108                 if (strcmp(argv[1], "-d") != 0)
109                         return CMD_HELP;
110
111                 fname = argv[2];
112                 st_size = -1;
113         } else {
114                 fname = argv[1];
115
116                 // get the stripe size
117                 st_size = strtoul(argv[2], &end, 0);
118                 if (*end != '\0') {
119                         fprintf(stderr, "error: %s: bad stripe size '%s'\n",
120                                         argv[0], argv[2]);
121                         return CMD_HELP;
122                 }
123                 // get the stripe offset
124                 st_offset = strtoul(argv[3], &end, 0);
125                 if (*end != '\0') {
126                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
127                                         argv[0], argv[3]);
128                         return CMD_HELP;
129                 }
130                 // get the stripe count
131                 st_count = strtoul(argv[4], &end, 0);
132                 if (*end != '\0') {
133                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
134                                         argv[0], argv[4]);
135                         return CMD_HELP;
136                 }
137         }
138
139         result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
140         if (result)
141                 fprintf(stderr, "error: %s: create stripe file failed\n",
142                                 argv[0]);
143
144         return result;
145 }
146
147 static int lfs_dirstripe(int argc, char **argv)
148 {
149         int result;
150         int st_count;
151         char *end;
152
153         if (argc != 3)
154                 return CMD_HELP;
155
156         // get the stripe size
157         st_count = strtoul(argv[2], &end, 0);
158         if (*end != '\0') {
159                 fprintf(stderr, "error: %s: bad count '%s'\n",
160                                 argv[0], argv[2]);
161                 return CMD_HELP;
162         }
163
164         result = op_create_dir(argv[1], st_count);
165         if (result)
166                 fprintf(stderr, "error: %s: create stripe dir failed\n",
167                                 argv[0]);
168
169         return result;
170 }
171
172 static int lfs_find(int argc, char **argv)
173 {
174         struct option long_opts[] = {
175                 {"obd", 1, 0, 'o'},
176                 {"quiet", 0, 0, 'q'},
177                 {"recursive", 0, 0, 'r'},
178                 {"verbose", 0, 0, 'v'},
179                 {0, 0, 0, 0}
180         };
181         char short_opts[] = "ho:qrv";
182         int quiet, verbose, recursive, c, rc;
183         struct obd_uuid *obduuid = NULL;
184
185         optind = 0;
186         quiet = verbose = recursive = 0;
187         while ((c = getopt_long(argc, argv, short_opts,
188                                         long_opts, NULL)) != -1) {
189                 switch (c) {
190                 case 'o':
191                         if (obduuid) {
192                                 fprintf(stderr,
193                                         "error: %s: only one obduuid allowed",
194                                         argv[0]);
195                                 return CMD_HELP;
196                         }
197                         obduuid = (struct obd_uuid *)optarg;
198                         break;
199                 case 'q':
200                         quiet++;
201                         verbose = 0;
202                         break;
203                 case 'r':
204                         recursive = 1;
205                         break;
206                 case 'v':
207                         verbose++;
208                         quiet = 0;
209                         break;
210                 case '?':
211                         return CMD_HELP;
212                         break;
213                 default:
214                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
215                                 argv[0], argv[optind - 1]);
216                         return CMD_HELP;
217                         break;
218                 }
219         }
220
221         if (optind >= argc)
222                 return CMD_HELP;
223
224         do {
225                 rc = llapi_find(argv[optind], obduuid, recursive, verbose, quiet, 0);
226         } while (++optind < argc && !rc);
227
228         if (rc)
229                 fprintf(stderr, "error: %s: find failed\n", argv[0]);
230         return rc;
231 }
232
233 static int lfs_getstripe(int argc, char **argv)
234 {
235         struct option long_opts[] = {
236                 {"quiet", 0, 0, 'q'},
237                 {"verbose", 0, 0, 'v'},
238                 {0, 0, 0, 0}
239         };
240         char short_opts[] = "qv";
241         int quiet, verbose, recursive, c, rc;
242         struct obd_uuid *obduuid = NULL;
243
244         optind = 0;
245         quiet = verbose = recursive = 0;
246         while ((c = getopt_long(argc, argv, short_opts,
247                                         long_opts, NULL)) != -1) {
248                 switch (c) {
249                 case 'o':
250                         if (obduuid) {
251                                 fprintf(stderr,
252                                         "error: %s: only one obduuid allowed",
253                                         argv[0]);
254                                 return CMD_HELP;
255                         }
256                         obduuid = (struct obd_uuid *)optarg;
257                         break;
258                 case 'q':
259                         quiet++;
260                         verbose = 0;
261                         break;
262                 case 'v':
263                         verbose++;
264                         quiet = 0;
265                         break;
266                 case '?':
267                         return CMD_HELP;
268                         break;
269                 default:
270                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
271                                 argv[0], argv[optind - 1]);
272                         return CMD_HELP;
273                         break;
274                 }
275         }
276
277         if (optind >= argc)
278                 return CMD_HELP;
279
280         do {
281                 rc = llapi_find(argv[optind], obduuid, recursive, verbose, quiet, 0);
282         } while (++optind < argc && !rc);
283
284         if (rc)
285                 fprintf(stderr, "error: %s: getstripe failed for %s\n",
286                         argv[0], argv[1]);
287
288         return rc;
289 }
290
291 static int lfs_showfid(int argc, char **argv)
292 {
293         struct option long_opts[] = {
294                 {"quiet", 0, 0, 'q'},
295                 {"recursive", 0, 0, 'r'},
296                 {"verbose", 0, 0, 'v'},
297                 {0, 0, 0, 0}
298         };
299         char short_opts[] = "hqrv";
300         int quiet, verbose, recursive, c, rc;
301
302         optind = 0;
303         quiet = verbose = recursive = 0;
304         while ((c = getopt_long(argc, argv, short_opts,
305                                 long_opts, NULL)) != -1) {
306                 switch (c) {
307                 case 'q':
308                         quiet++;
309                         verbose = 0;
310                         break;
311                 case 'r':
312                         recursive = 1;
313                         break;
314                 case 'v':
315                         verbose++;
316                         quiet = 0;
317                         break;
318                 case '?':
319                         return CMD_HELP;
320                         break;
321                 default:
322                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
323                                 argv[0], argv[optind - 1]);
324                         return CMD_HELP;
325                 }
326         }
327
328         if (optind >= argc)
329                 return CMD_HELP;
330
331         do {
332                 rc = llapi_find(argv[optind], NULL, recursive, verbose, quiet, 1);
333         } while (++optind < argc && !rc);
334
335         if (rc)
336                 fprintf(stderr, "error: %s: find failed\n", argv[0]);
337         
338         return rc;
339 }
340
341 static int lfs_osts(int argc, char **argv)
342 {
343         FILE *fp;
344         struct mntent *mnt = NULL;
345         struct obd_uuid *obduuid = NULL;
346         int rc=0;
347
348         if (argc != 1)
349                 return CMD_HELP;
350
351         fp = setmntent(MOUNTED, "r");
352
353         if (fp == NULL) {
354                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
355                         strerror (errno));
356         } else {
357                 mnt = getmntent(fp);
358                 while (feof(fp) == 0 && ferror(fp) ==0) {
359                         if (llapi_is_lustre_mnttype(mnt->mnt_type)) {
360                                 rc = llapi_find(mnt->mnt_dir, obduuid, 0, 0, 0, 0);
361                                 if (rc)
362                                         fprintf(stderr,
363                                                "error: lfs osts failed on %s\n",
364                                                mnt->mnt_dir);
365                         }
366                         mnt = getmntent(fp);
367                 }
368                 endmntent(fp);
369         }
370
371         return rc;
372 }
373
374 static int lfs_check(int argc, char **argv)
375 {
376         int rc;
377         FILE *fp;
378         struct mntent *mnt = NULL;
379         int num_types = 1;
380         char *obd_types[2];
381         char obd_type1[4];
382         char obd_type2[4];
383
384         if (argc != 2)
385                 return CMD_HELP;
386
387         obd_types[0] = obd_type1;
388         obd_types[1] = obd_type2;
389
390         if (strcmp(argv[1], "osts") == 0) {
391                 strcpy(obd_types[0], "osc");
392         } else if (strcmp(argv[1], "mds") == 0) {
393                 strcpy(obd_types[0], "mdc");
394         } else if (strcmp(argv[1], "servers") == 0) {
395                 num_types = 2;
396                 strcpy(obd_types[0], "osc");
397                 strcpy(obd_types[1], "mdc");
398         } else {
399                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
400                         argv[0], argv[1]);
401                 return CMD_HELP;
402         }
403
404         fp = setmntent(MOUNTED, "r");
405         if (fp == NULL) {
406                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
407                         strerror (errno));
408         } else {
409                 mnt = getmntent(fp);
410                 while (feof(fp) == 0 && ferror(fp) ==0) {
411                         if (llapi_is_lustre_mnttype(mnt->mnt_type))
412                                 break;
413                         mnt = getmntent(fp);
414                 }
415                 endmntent(fp);
416         }
417
418         rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
419
420         if (rc)
421                 fprintf(stderr, "error: %s: %s status failed\n",
422                                 argv[0],argv[1]);
423
424         return rc;
425
426 }
427
428 static int lfs_catinfo(int argc, char **argv)
429 {
430         FILE *fp;
431         struct mntent *mnt = NULL;
432         int rc;
433
434         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
435                 return CMD_HELP;
436
437         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
438                 return CMD_HELP;
439
440         fp = setmntent(MOUNTED, "r");
441         if (fp == NULL) {
442                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
443                          strerror(errno));
444         } else {
445                 mnt = getmntent(fp);
446                 while (feof(fp) == 0 && ferror(fp) == 0) {
447                         if (llapi_is_lustre_mnttype(mnt->mnt_type))
448                                 break;
449                         mnt = getmntent(fp);
450                 }
451                 endmntent(fp);
452         }
453
454         if (mnt) {
455                 if (argc == 3)
456                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
457                 else
458                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
459         } else {
460                 fprintf(stderr, "no lustre_lite mounted.\n");
461                 rc = -1;
462         }
463
464         return rc;
465 }
466
467 int main(int argc, char **argv)
468 {
469         int rc;
470
471         setlinebuf(stdout);
472
473         ptl_initialize(argc, argv);
474         if (obd_initialize(argc, argv) < 0)
475                 exit(2);
476         if (dbg_initialize(argc, argv) < 0)
477                 exit(3);
478
479         Parser_init("lfs > ", cmdlist);
480
481         if (argc > 1) {
482                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
483         } else {
484                 rc = Parser_commands();
485         }
486
487         obd_finalize(argc, argv);
488         return rc;
489 }