Whamcloud - gitweb
658a921157c603cddda818f0e38e27f7e5eddace
[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
91         if (argc != 5)
92                 return CMD_HELP;
93
94         // get the stripe size
95         st_size = strtoul(argv[2], &end, 0);
96         if (*end != '\0') {
97                 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
98                                 argv[0], argv[2]);
99                 return CMD_HELP;
100         }
101         // get the stripe offset
102         st_offset = strtoul(argv[3], &end, 0);
103         if (*end != '\0') {
104                 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
105                                 argv[0], argv[3]);
106                 return CMD_HELP;
107         }
108         // get the stripe count
109         st_count = strtoul(argv[4], &end, 0);
110         if (*end != '\0') {
111                 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
112                                 argv[0], argv[4]);
113                 return CMD_HELP;
114         }
115
116         result = op_create_file(argv[1], st_size, st_offset, st_count);
117         if (result)
118                 fprintf(stderr, "error: %s: create stripe file failed\n",
119                                 argv[0]);
120
121         return result;
122 }
123
124 static int lfs_find(int argc, char **argv)
125 {
126         struct option long_opts[] = {
127                 {"obd", 1, 0, 'o'},
128                 {"quiet", 0, 0, 'q'},
129                 {"recursive", 0, 0, 'r'},
130                 {"verbose", 0, 0, 'v'},
131                 {0, 0, 0, 0}
132         };
133         char short_opts[] = "ho:qrv";
134         int quiet, verbose, recursive, c, rc;
135         struct obd_uuid *obduuid = NULL;
136
137         optind = 0;
138         quiet = verbose = recursive = 0;
139         while ((c = getopt_long(argc, argv, short_opts,
140                                         long_opts, NULL)) != -1) {
141                 switch (c) {
142                 case 'o':
143                         if (obduuid) {
144                                 fprintf(stderr,
145                                         "error: %s: only one obduuid allowed",
146                                         argv[0]);
147                                 return CMD_HELP;
148                         }
149                         obduuid = (struct obd_uuid *)optarg;
150                         break;
151                 case 'q':
152                         quiet++;
153                         verbose = 0;
154                         break;
155                 case 'r':
156                         recursive = 1;
157                         break;
158                 case 'v':
159                         verbose++;
160                         quiet = 0;
161                         break;
162                 case '?':
163                         return CMD_HELP;
164                         break;
165                 default:
166                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
167                                 argv[0], argv[optind - 1]);
168                         return CMD_HELP;
169                         break;
170                 }
171         }
172
173         if (optind >= argc)
174                 return CMD_HELP;
175
176         do {
177                 rc = op_find(argv[optind], obduuid, recursive, verbose, quiet);
178         } while (++optind < argc && !rc);
179
180         if (rc)
181                 fprintf(stderr, "error: %s: find failed\n", argv[0]);
182         return rc;
183 }
184
185 static int lfs_getstripe(int argc, char **argv)
186 {
187         struct obd_uuid *obduuid = NULL;
188         int rc;
189
190         if (argc != 2)
191                 return CMD_HELP;
192
193         optind = 1;
194
195         do {
196                 rc = op_find(argv[optind], obduuid, 0, 0, 0);
197         } while (++optind < argc && !rc);
198
199         if (rc)
200                 fprintf(stderr, "error: %s: getstripe failed for %s\n",
201                         argv[0], argv[1]);
202
203         return rc;
204 }
205
206 static int lfs_osts(int argc, char **argv)
207 {
208         FILE *fp;
209         struct mntent *mnt = NULL;
210         struct obd_uuid *obduuid = NULL;
211         int rc=0;
212
213         if (argc != 1)
214                 return CMD_HELP;
215
216         fp = setmntent(MOUNTED, "r");
217
218         if (fp == NULL) {
219                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
220                         strerror (errno));
221         } else {
222                 mnt = getmntent(fp);
223                 while (feof(fp) == 0 && ferror(fp) ==0) {
224                         if (llapi_is_lustre_mnttype(mnt->mnt_type)) {
225                                 rc = op_find(mnt->mnt_dir, obduuid, 0, 0, 0);
226                                 if (rc)
227                                         fprintf(stderr, "error: lfs osts failed for %s\n",
228                                                 mnt->mnt_dir);
229                         }
230                         mnt = getmntent(fp);
231                 }
232                 endmntent(fp);
233         }
234
235         return rc;
236 }
237
238 static int lfs_check(int argc, char **argv)
239 {
240         int rc;
241         FILE *fp;
242         struct mntent *mnt = NULL;
243         int type_num = 1;
244         char *obd_type_p[2];
245         char obd_type1[4];
246         char obd_type2[4];
247
248         if (argc != 2)
249                 return CMD_HELP;
250
251         obd_type_p[1]=obd_type1;
252         obd_type_p[2]=obd_type2;
253
254         if (strcmp(argv[1],"osts")==0) {
255                 strcpy(obd_type_p[0],"osc");
256         } else if (strcmp(argv[1],"mds")==0) {
257                 strcpy(obd_type_p[0],"mdc");
258         } else if (strcmp(argv[1],"servers")==0) {
259                 type_num=2;
260                 strcpy(obd_type_p[0],"osc");
261                 strcpy(obd_type_p[1],"mdc");
262         } else {
263                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
264                                 argv[0], argv[1]);
265                         return CMD_HELP;
266         }
267
268         fp = setmntent(MOUNTED, "r");
269         if (fp == NULL) {
270                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
271                         strerror (errno));
272         } else {
273                 mnt = getmntent(fp);
274                 while (feof(fp) == 0 && ferror(fp) ==0) {
275                         if (llapi_is_lustre_mnttype(mnt->mnt_type))
276                                 break;
277                         mnt = getmntent(fp);
278                 }
279                 endmntent(fp);
280         }
281
282         rc = op_check(type_num,obd_type_p,mnt->mnt_dir);
283
284         if (rc)
285                 fprintf(stderr, "error: %s: %s status failed\n",
286                                 argv[0],argv[1]);
287
288         return rc;
289
290 }
291
292 static int lfs_catinfo(int argc, char **argv)
293 {
294         FILE *fp;
295         struct mntent *mnt = NULL;
296         int rc;
297
298         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
299                 return CMD_HELP;
300
301         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
302                 return CMD_HELP;
303
304         fp = setmntent(MOUNTED, "r");
305         if (fp == NULL) {
306                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
307                          strerror(errno));
308         } else {
309                 mnt = getmntent(fp);
310                 while (feof(fp) == 0 && ferror(fp) == 0) {
311                         if (llapi_is_lustre_mnttype(mnt->mnt_type))
312                                 break;
313                         mnt = getmntent(fp);
314                 }
315                 endmntent(fp);
316         }
317
318         if (mnt) {
319                 if (argc == 3)
320                         rc = op_catinfo(mnt->mnt_dir, argv[1], argv[2]);
321                 else
322                         rc = op_catinfo(mnt->mnt_dir, argv[1], NULL);
323         } else {
324                 fprintf(stderr, "no lustre_lite mounted.\n");
325                 rc = -1;
326         }
327
328         return rc;
329 }
330
331 int main(int argc, char **argv)
332 {
333         int rc;
334
335         setlinebuf(stdout);
336
337         ptl_initialize(argc, argv);
338         if (obd_initialize(argc, argv) < 0)
339                 exit(2);
340         if (dbg_initialize(argc, argv) < 0)
341                 exit(3);
342
343         Parser_init("lfs > ", cmdlist);
344
345         if (argc > 1) {
346                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
347         } else {
348                 rc = Parser_commands();
349         }
350
351         obd_finalize(argc, argv);
352         return rc;
353 }