lfs.c
来自「lustre 1.6.5 source code」· C语言 代码 · 共 1,844 行 · 第 1/5 页
C
1,844 行
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: * * Copyright (C) 2002 Cluster File Systems, Inc. * Author: Peter J. Braam <braam@clusterfs.com> * Author: Phil Schwan <phil@clusterfs.com> * Author: Robert Read <rread@clusterfs.com> * * This file is part of Lustre, http://www.lustre.org. * * Lustre is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * Lustre is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Lustre; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */#include <stdlib.h>#include <stdio.h>#include <getopt.h>#include <string.h>#include <mntent.h>#include <errno.h>#include <pwd.h>#include <grp.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <dirent.h>#include <time.h>#include <ctype.h>#include <lnet/api-support.h>#include <lnet/lnetctl.h>#include <liblustre.h>#include <lustre/lustre_idl.h>#include <lustre/liblustreapi.h>#include <lustre/lustre_user.h>#include "parser.h"#include "obdctl.h"unsigned int libcfs_subsystem_debug = 0;/* all functions */static int lfs_setstripe(int argc, char **argv);static int lfs_find(int argc, char **argv);static int lfs_getstripe(int argc, char **argv);static int lfs_osts(int argc, char **argv);static int lfs_df(int argc, char **argv);static int lfs_check(int argc, char **argv);static int lfs_catinfo(int argc, char **argv);#ifdef HAVE_QUOTA_SUPPORTstatic int lfs_quotachown(int argc, char **argv);static int lfs_quotacheck(int argc, char **argv);static int lfs_quotaon(int argc, char **argv);static int lfs_quotaoff(int argc, char **argv);static int lfs_setquota(int argc, char **argv);static int lfs_quota(int argc, char **argv);static int lfs_quotainv(int argc, char **argv);#endifstatic int lfs_join(int argc, char **argv);/* all avaialable commands */command_t cmdlist[] = { {"setstripe", lfs_setstripe, 0, "Create a new file with a specific striping pattern or\n" "set the default striping pattern on an existing directory or\n" "delete the default striping pattern from an existing directory\n" "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n" " or \n" " setstripe <filename|dirname> [--size|-s stripe_size]\n" " [--index|-i stripe_index]\n" " [--count|-c stripe_count]\n" " or \n" " setstripe -d <dirname> (to delete default striping)\n" "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n" "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n" "\tstripe_index: OST index of first stripe (-1 filesystem default)\n" "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"}, {"getstripe", lfs_getstripe, 0, "To list the striping info for a given filename or files in a\n" "directory or recursively for all files in a directory tree.\n" "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n" " [--recursive | -r] <dir|file> ..."}, {"find", lfs_find, 0, "To find files that match given parameters recursively in a directory tree.\n" "usage: find <dir/file> ... \n" " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n" " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n" " [--print|-p] [--obd|-O <uuid>] [[!] --type|-t <filetype>]\n" "\t !: used before an option indicates 'NOT' the requested attribute\n" "\t -: used before an value indicates 'AT MOST' the requested value\n" "\t +: used before an option indicates 'AT LEAST' the requested value\n"}, {"check", lfs_check, 0, "Display the status of MDS or OSTs (as specified in the command)\n" "or all the servers (MDS and OSTs).\n" "usage: check <osts|mds|servers>"}, {"catinfo", lfs_catinfo, 0, "Show information of specified type logs.\n" "usage: catinfo {keyword} [node name]\n" "\tkeywords are one of followings: config, deletions.\n" "\tnode name must be provided when use keyword config."}, {"join", lfs_join, 0, "join two lustre files into one - join A, B, will be like cat B >> A & del B\n" "usage: join <filename_A> <filename_B>\n"}, {"osts", lfs_osts, 0, "osts"}, {"df", lfs_df, 0, "report filesystem disk space usage or inodes usage" "of each MDS/OSD.\n" "Usage: df [-i] [-h] [path]"},#ifdef HAVE_QUOTA_SUPPORT {"quotachown",lfs_quotachown, 0, "Change files' owner or group on the specified filesystem.\n" "usage: quotachown [-i] <filesystem>\n" "\t-i: ignore error if file is not exist\n"}, {"quotacheck", lfs_quotacheck, 0, "Scan the specified filesystem for disk usage, and create,\n" "or update quota files.\n" "usage: quotacheck [ -ug ] <filesystem>"}, {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n" "usage: quotaon [ -ugf ] <filesystem>"}, {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n" "usage: quotaoff [ -ug ] <filesystem>"}, {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n" "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n" " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"}, {"quota", lfs_quota, 0, "Display disk usage and limits.\n" "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"}, {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n" "usage: quotainv [-u|-g] <filesystem>"},#endif {"help", Parser_help, 0, "help"}, {"exit", Parser_quit, 0, "quit"}, {"quit", Parser_quit, 0, "quit"}, { 0, 0, 0, NULL }};/* functions */static int lfs_setstripe(int argc, char **argv){ char *fname; int result; unsigned long long st_size; int st_offset, st_count; char *end; int c; int delete = 0; char *stripe_size_arg = NULL; char *stripe_off_arg = NULL; char *stripe_count_arg = NULL; unsigned long long size_units; struct option long_opts[] = { {"size", required_argument, 0, 's'}, {"count", required_argument, 0, 'c'}, {"index", required_argument, 0, 'i'}, {"delete", no_argument, 0, 'd'}, {0, 0, 0, 0} }; st_size = 0; st_offset = -1; st_count = 0; if (argc == 3 && strcmp(argv[1], "-d") == 0) { /* for compatibility with the existing positional parameter * usage */ fname = argv[2]; optind = 2; } else if (argc == 5 && (argv[2][0] != '-' || isdigit(argv[2][1])) && (argv[3][0] != '-' || isdigit(argv[3][1])) && (argv[4][0] != '-' || isdigit(argv[4][1])) ) { /* for compatibility with the existing positional parameter * usage */ fname = argv[1]; stripe_size_arg = argv[2]; stripe_off_arg = argv[3]; stripe_count_arg = argv[4]; optind = 4; } else { optind = 0; while ((c = getopt_long(argc, argv, "c:di:s:", long_opts, NULL)) >= 0) { switch (c) { case 0: /* Long options. */ break; case 'c': stripe_count_arg = optarg; break; case 'd': /* delete the default striping pattern */ delete = 1; break; case 'i': stripe_off_arg = optarg; break; case 's': stripe_size_arg = optarg; break; case '?': return CMD_HELP; default: fprintf(stderr, "error: %s: option '%s' " "unrecognized\n", argv[0], argv[optind - 1]); return CMD_HELP; } } if (optind < argc) fname = argv[optind]; else return CMD_HELP; if (delete && (stripe_size_arg != NULL || stripe_off_arg != NULL || stripe_count_arg != NULL)) { fprintf(stderr, "error: %s: cannot specify -d with " "-s, -c or -i options\n", argv[0]); return CMD_HELP; } } if (optind != argc - 1) { fprintf(stderr, "error: %s: only 1 filename|dirname can be " "specified: '%s'\n", argv[0], argv[argc-1]); return CMD_HELP; } /* get the stripe size */ if (stripe_size_arg != NULL) { result = parse_size(stripe_size_arg, &st_size, &size_units); if (result) { fprintf(stderr,"error: bad size '%s'\n", stripe_size_arg); return result; } } /* get the stripe offset */ if (stripe_off_arg != NULL) { st_offset = strtoul(stripe_off_arg, &end, 0); if (*end != '\0') { fprintf(stderr, "error: %s: bad stripe offset '%s'\n", argv[0], stripe_off_arg); return CMD_HELP; } } /* get the stripe count */ if (stripe_count_arg != NULL) { st_count = strtoul(stripe_count_arg, &end, 0); if (*end != '\0') { fprintf(stderr, "error: %s: bad stripe count '%s'\n", argv[0], stripe_count_arg); return CMD_HELP; } } result = llapi_file_create(fname, st_size, st_offset, st_count, 0); if (result) fprintf(stderr, "error: %s: create stripe file failed\n", argv[0]); return result;}static int set_time(time_t *time, time_t *set, char *str){ time_t t; int res = 0; if (str[0] == '+') res = 1; else if (str[0] == '-') res = -1; if (res) str++; t = strtol(str, NULL, 0); if (*time < t * 24 * 60 * 60) { if (res) str--; fprintf(stderr, "Wrong time '%s' is specified.\n", str); return INT_MAX; } *set = *time - t * 24 * 60 * 60; return res;}static int lfs_find(int argc, char **argv){ int new_fashion = 1; int c, ret; time_t t; struct find_param param = { .maxdepth = -1 }; char str[1024]; struct option long_opts[] = { /* New find options. */ {"atime", required_argument, 0, 'A'}, {"ctime", required_argument, 0, 'C'}, {"mtime", required_argument, 0, 'M'}, {"maxdepth", required_argument, 0, 'D'}, {"name", required_argument, 0, 'n'}, /* --obd is considered as a new option. */ {"obd", required_argument, 0, 'O'}, {"ost", required_argument, 0, 'O'}, {"print", no_argument, 0, 'P'}, {"print0", no_argument, 0, 'p'}, /* Old find options. */ {"quiet", no_argument, 0, 'q'}, {"recursive", no_argument, 0, 'r'}, {"size", required_argument, 0, 's'}, {"type", required_argument, 0, 't'}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; int pathstart = -1; int pathend = -1; int neg_opt = 0; time_t *xtime; int *xsign; int isoption; time(&t); optind = 0; while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrs:t:v", long_opts, NULL)) >= 0) { xtime = NULL; xsign = NULL; if (neg_opt) --neg_opt; /* '!' is part of option */ isoption = (c != 1) || (strcmp(optarg, "!") == 0); if (!isoption && pathend != -1) { fprintf(stderr, "err: %s: filename|dirname must either " "precede options or follow options\n", argv[0]); return CMD_HELP; } if (!isoption && pathstart == -1) pathstart = optind - 1; if (isoption && pathstart != -1 && pathend == -1) { pathend = optind - 2; if ((c == 1 && strcmp(optarg, "!") == 0) || c == 'P' || c == 'p' || c == 'O' || c == 'q' || c == 'r' || c == 'v') pathend = optind - 1; } switch (c) { case 0: /* Long options. */ break; case 1: if (strcmp(optarg, "!") == 0)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?