1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
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>
9 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
34 #include <portals/ptlctl.h>
36 #include <liblustre.h>
37 #include <linux/lustre_idl.h>
38 #include <lustre/liblustreapi.h>
39 #include <lustre/lustre_user.h>
45 static int lfs_setstripe(int argc, char **argv);
46 static int lfs_dirstripe(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_showfid(int argc, char **argv);
50 static int lfs_osts(int argc, char **argv);
51 static int lfs_check(int argc, char **argv);
52 static int lfs_catinfo(int argc, char **argv);
53 static int lfs_getfacl(int argc, char **argv);
54 static int lfs_setfacl(int argc, char **argv);
56 /* all avaialable commands */
57 command_t cmdlist[] = {
58 {"setstripe", lfs_setstripe, 0,
59 "Create a new file with a specific striping pattern or\n"
60 "set the default striping pattern on an existing directory or\n"
61 "delete the default striping pattern from an existing directory\n"
62 "usage: setstripe <filename|dirname> <stripe size> <stripe start> <stripe count>\n"
64 " setstripe -d <dirname>\n"
65 "\tstripe size: Number of bytes in each stripe (0 default)\n"
66 "\tstripe start: OST index of first stripe (-1 default)\n"
67 "\tstripe count: Number of OSTs to stripe over (0 default)"},
68 {"dirstripe", lfs_dirstripe, 0,
69 "To create a new dir with a specific striping pattern.\n"
70 "usage: dirstripe <dirname> <stripe count> [<mds idx list>]\n"
71 "\tstripe count: Number of MDSes to stripe over (0 default)\n"
72 "\tmds idx list: List of MDS servers to contain the dir (not implemented)"},
74 "To list the extended attributes for a given filename or files in a\n"
75 "directory or recursively for all files in a directory tree.\n"
76 "usage: find [--obd <uuid>] [--quiet | --verbose] [--recursive] <dir|file> ..."},
77 {"getstripe", lfs_getstripe, 0,
78 "To list the striping pattern for given filename.\n"
79 "usage: getstripe <filename>"},
80 {"showfid", lfs_showfid, 0,
81 "To list the fid and store cookie for given filename.\n"
82 "usage: showfid [--quiet | --verbose] [--recursive] <dir|file> ..."},
83 {"check", lfs_check, 0,
84 "Display the status of MDS or OSTs (as specified in the command)\n"
85 "or all the servers (MDS and OSTs).\n"
86 "usage: check <osts|mds|servers>"},
87 {"catinfo", lfs_catinfo, 0,
88 "Show information of specified type logs.\n"
89 "usage: catinfo {keyword} [node name]\n"
90 "\tkeywords are one of followings: config, deletions.\n"
91 "\tnode name must be provided when use keyword config."},
92 {"getfacl", lfs_getfacl, 0,
93 "Get file access control lists.\n"
94 "usage: getfacl [-dRLPvh] file"},
95 {"setfacl", lfs_setfacl, 0,
96 "Set file access control lists.\n"
97 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
98 {"osts", lfs_osts, 0, "osts"},
99 {"help", Parser_help, 0, "help"},
100 {"exit", Parser_quit, 0, "quit"},
101 {"quit", Parser_quit, 0, "quit"},
106 static int lfs_setstripe(int argc, char **argv)
111 int st_offset = -1, st_count = 0;
114 if (argc != 5 && argc != 3)
118 if (strcmp(argv[1], "-d") != 0)
126 // get the stripe size
127 st_size = strtoul(argv[2], &end, 0);
129 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
133 // get the stripe offset
134 st_offset = strtoul(argv[3], &end, 0);
136 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
140 // get the stripe count
141 st_count = strtoul(argv[4], &end, 0);
143 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
149 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
151 fprintf(stderr, "error: %s: create stripe file failed\n",
157 static int lfs_dirstripe(int argc, char **argv)
166 // get the stripe size
167 st_count = strtoul(argv[2], &end, 0);
169 fprintf(stderr, "error: %s: bad count '%s'\n",
174 result = op_create_dir(argv[1], st_count);
176 fprintf(stderr, "error: %s: create stripe dir failed\n",
182 static int lfs_find(int argc, char **argv)
184 struct option long_opts[] = {
186 {"quiet", 0, 0, 'q'},
187 {"recursive", 0, 0, 'r'},
188 {"verbose", 0, 0, 'v'},
191 char short_opts[] = "ho:qrv";
192 int quiet, verbose, recursive, c, rc;
193 struct obd_uuid *obduuid = NULL;
196 quiet = verbose = recursive = 0;
197 while ((c = getopt_long(argc, argv, short_opts,
198 long_opts, NULL)) != -1) {
203 "error: %s: only one obduuid allowed",
207 obduuid = (struct obd_uuid *)optarg;
224 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
225 argv[0], argv[optind - 1]);
235 rc = llapi_find(argv[optind], obduuid, recursive, verbose, quiet, 0);
236 } while (++optind < argc && !rc);
239 fprintf(stderr, "error: %s: find failed\n", argv[0]);
243 static int lfs_getstripe(int argc, char **argv)
245 struct option long_opts[] = {
246 {"quiet", 0, 0, 'q'},
247 {"verbose", 0, 0, 'v'},
250 char short_opts[] = "qv";
251 int quiet, verbose, recursive, c, rc;
252 struct obd_uuid *obduuid = NULL;
255 quiet = verbose = recursive = 0;
256 while ((c = getopt_long(argc, argv, short_opts,
257 long_opts, NULL)) != -1) {
262 "error: %s: only one obduuid allowed",
266 obduuid = (struct obd_uuid *)optarg;
280 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
281 argv[0], argv[optind - 1]);
291 rc = llapi_find(argv[optind], obduuid, recursive, verbose, quiet, 0);
292 } while (++optind < argc && !rc);
295 fprintf(stderr, "error: %s: getstripe failed for %s\n",
301 static int lfs_showfid(int argc, char **argv)
303 struct option long_opts[] = {
304 {"quiet", 0, 0, 'q'},
305 {"recursive", 0, 0, 'r'},
306 {"verbose", 0, 0, 'v'},
309 char short_opts[] = "hqrv";
310 int quiet, verbose, recursive, c, rc;
313 quiet = verbose = recursive = 0;
314 while ((c = getopt_long(argc, argv, short_opts,
315 long_opts, NULL)) != -1) {
332 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
333 argv[0], argv[optind - 1]);
342 rc = llapi_find(argv[optind], NULL, recursive, verbose, quiet, 1);
343 } while (++optind < argc && !rc);
346 fprintf(stderr, "error: %s: find failed\n", argv[0]);
351 static int lfs_osts(int argc, char **argv)
354 struct mntent *mnt = NULL;
355 struct obd_uuid *obduuid = NULL;
361 fp = setmntent(MOUNTED, "r");
364 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
368 while (feof(fp) == 0 && ferror(fp) ==0) {
369 if (llapi_is_lustre_mnttype(mnt->mnt_type)) {
370 rc = llapi_find(mnt->mnt_dir, obduuid, 0, 0, 0, 0);
373 "error: lfs osts failed on %s\n",
384 static int lfs_check(int argc, char **argv)
388 struct mntent *mnt = NULL;
397 obd_types[0] = obd_type1;
398 obd_types[1] = obd_type2;
400 if (strcmp(argv[1], "osts") == 0) {
401 strcpy(obd_types[0], "osc");
402 } else if (strcmp(argv[1], "mds") == 0) {
403 strcpy(obd_types[0], "mdc");
404 } else if (strcmp(argv[1], "servers") == 0) {
406 strcpy(obd_types[0], "osc");
407 strcpy(obd_types[1], "mdc");
409 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
414 fp = setmntent(MOUNTED, "r");
416 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
420 while (feof(fp) == 0 && ferror(fp) ==0) {
421 if (llapi_is_lustre_mnttype(mnt->mnt_type))
428 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
431 fprintf(stderr, "error: %s: %s status failed\n",
438 static int lfs_catinfo(int argc, char **argv)
441 struct mntent *mnt = NULL;
444 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
447 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
450 fp = setmntent(MOUNTED, "r");
452 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
456 while (feof(fp) == 0 && ferror(fp) == 0) {
457 if (llapi_is_lustre_mnttype(mnt->mnt_type))
466 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
468 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
470 fprintf(stderr, "no lustre_lite mounted.\n");
478 * We assume one and only one filename is supplied as the
481 static int acl_cmd_parse(int argc, char **argv,
482 char *dirbuf, char *cmdbuf)
485 char path2[PATH_MAX];
487 struct mntent *mnt = NULL;
494 /* get path prefix */
495 strncpy(path, argv[argc - 1], PATH_MAX);
498 /* try to resolve the pathname into relative to the
499 * root of the mounted lustre filesystem.
500 * FIXME we simply suppose there's no sub-mounted filesystems
501 * under this mounted lustre tree.
503 if (getcwd(path2, PATH_MAX) == NULL) {
504 fprintf(stderr, "getcwd old: %s\n", strerror(errno));
508 if (chdir(dir) == -1) {
509 fprintf(stderr, "chdir to %s: %s\n",
510 dir, strerror(errno));
514 if (getcwd(path, PATH_MAX) == NULL) {
515 fprintf(stderr, "getcwd new: %s\n", strerror(errno));
519 if (chdir(path2) == -1) {
520 fprintf(stderr, "chdir back: %s\n", strerror(errno));
524 fp = setmntent(MOUNTED, "r");
526 fprintf(stderr, "setmntent(%s): %s\n", MOUNTED,
536 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
539 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
542 /* save the mountpoint dir part */
543 strncpy(dirbuf, mnt->mnt_dir, PATH_MAX);
545 /* get rest of path under the mountpoint,
546 * don't start with slash.
548 p = path + strlen(mnt->mnt_dir);
551 snprintf(path2, PATH_MAX, "%s", p);
553 /* remove trailing slash */
554 if (path2[strlen(path2)] == '/')
555 path2[strlen(path2)] = '\0';
557 /* continue try to match more proper fs */
563 fprintf(stderr, "no mounted lustre fs\n");
567 /* get base name of target */
568 strncpy(path, argv[argc - 1], PATH_MAX);
569 tgt = basename(path);
572 for (i = 1; i < argc - 1; i++) {
573 strncat(cmdbuf, argv[i], PATH_MAX);
574 strncat(cmdbuf, " ", PATH_MAX);
576 strncat(cmdbuf, path2, PATH_MAX);
577 if (path2[0] != '\0')
578 strncat(cmdbuf, "/", PATH_MAX);
579 strncat(cmdbuf, tgt, PATH_MAX);
584 static int lfs_getfacl(int argc, char **argv)
589 if (acl_cmd_parse(argc, argv, dir, cmd))
592 return llapi_getfacl(dir, cmd);
595 static int lfs_setfacl(int argc, char **argv)
600 if (acl_cmd_parse(argc, argv, dir, cmd))
603 return llapi_setfacl(dir, cmd);
606 int main(int argc, char **argv)
612 ptl_initialize(argc, argv);
613 if (obd_initialize(argc, argv) < 0)
615 if (dbg_initialize(argc, argv) < 0)
618 Parser_init("lfs > ", cmdlist);
621 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
623 rc = Parser_commands();
626 obd_finalize(argc, argv);