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