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