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