- memset(&osfs_buffer, 0, sizeof (osfs_buffer));
-
- for (i = 0; i < type_num; i++) {
- if (strcmp(obd_type_name, obd_type[i]) != 0)
- continue;
-
- cb(obd_type_name, obd_name, obd_uuid, args);
- }
- }
- fclose(fp);
- return 0;
-}
-
-static void do_target_check(char *obd_type_name, char *obd_name,
- char *obd_uuid, void *args)
-{
- int rc;
-
- rc = llapi_ping(obd_type_name, obd_name);
- if (rc == ENOTCONN) {
- llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
- } else if (rc) {
- llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
- } else {
- llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
- }
-}
-
-int llapi_target_check(int type_num, char **obd_type, char *dir)
-{
- return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
-}
-
-#undef MAX_STRING_SIZE
-
-/* Is this a lustre fs? */
-int llapi_is_lustre_mnttype(const char *type)
-{
- return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
-}
-
-/* Is this a lustre client fs? */
-int llapi_is_lustre_mnt(struct mntent *mnt)
-{
- return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
- strstr(mnt->mnt_fsname, ":/") != NULL);
-}
-
-int llapi_quotacheck(char *mnt, int check_type)
-{
- DIR *root;
- int rc;
-
- root = opendir(mnt);
- if (!root) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
- return rc;
- }
-
- rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
- if (rc < 0)
- rc = -errno;
-
- closedir(root);
- return rc;
-}
-
-int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
-{
- DIR *root;
- int poll_intvl = 2;
- int rc;
-
- root = opendir(mnt);
- if (!root) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
- return rc;
- }
-
- while (1) {
- rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
- if (!rc)
- break;
- sleep(poll_intvl);
- if (poll_intvl < 30)
- poll_intvl *= 2;
- }
-
- closedir(root);
- return 0;
-}
-
-int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
-{
- DIR *root;
- int rc;
-
- root = opendir(mnt);
- if (!root) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
- return rc;
- }
-
- rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
- if (rc < 0)
- rc = -errno;
-
- closedir(root);
- return rc;
-}
-
-static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
- struct dirent64 *de)
-{
- struct find_param *param = (struct find_param *)data;
- lstat_t *st;
- int rc;
-
- LASSERT(parent != NULL || d != NULL);
-
- rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
- if (rc) {
- if (rc == -ENODATA) {
- if (!param->obduuid && !param->quiet)
- llapi_error(LLAPI_MSG_ERROR, -ENODATA,
- "%s has no stripe info", path);
- rc = 0;
- } else if (rc == -ENOENT) {
- rc = 0;
- }
- return rc;
- }
-
- st = ¶m->lmd->lmd_st;
-
- /* libc chown() will do extra check, and if the real owner is
- * the same as the ones to set, it won't fall into kernel, so
- * invoke syscall directly. */
- rc = syscall(SYS_chown, path, -1, -1);
- if (rc)
- llapi_error(LLAPI_MSG_ERROR, errno,
- "error: chown %s", path);
-
- rc = chmod(path, st->st_mode);
- if (rc) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
- path, st->st_mode);
- }
-
- return rc;
-}
-
-int llapi_quotachown(char *path, int flag)
-{
- struct find_param param;
-
- memset(¶m, 0, sizeof(param));
- param.recursive = 1;
- param.verbose = 0;
- param.quiet = 1;
-
- return param_callback(path, cb_quotachown, NULL, ¶m);
-}
-
-#include <pwd.h>
-#include <grp.h>
-#include <mntent.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <ctype.h>
-
-static int rmtacl_notify(int ops)
-{
- FILE *fp;
- struct mntent *mnt;
- int found = 0, fd = 0, rc = 0;
-
- fp = setmntent(MOUNTED, "r");
- if (fp == NULL) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc,
- "error setmntent(%s)", MOUNTED);
- return rc;
- }
-
- while (1) {
- mnt = getmntent(fp);
- if (!mnt)
- break;
-
- if (!llapi_is_lustre_mnt(mnt))
- continue;
-
- fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
- if (fd < 0) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc,
- "Can't open '%s'\n", mnt->mnt_dir);
- goto out;
- }
-
- rc = ioctl(fd, LL_IOC_RMTACL, ops);
- close(fd);
- if (rc < 0) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc,
- "ioctl RMTACL on '%s' err %d\n",
- mnt->mnt_dir, rc);
- goto out;
- }
-
- found++;
- }
-
-out:
- endmntent(fp);
- return ((rc != 0) ? rc : found);
-}
-
-static char *next_token(char *p, int div)
-{
- if (p == NULL)
- return NULL;
-
- if (div)
- while (*p && *p != ':' && !isspace(*p))
- p++;
- else
- while (*p == ':' || isspace(*p))
- p++;
-
- return *p ? p : NULL;
-}
-
-static int rmtacl_name2id(char *name, int is_user)
-{
- if (is_user) {
- struct passwd *pw;
-
- pw = getpwnam(name);
- if (pw == NULL)
- return INVALID_ID;
- else
- return (int)(pw->pw_uid);
- } else {
- struct group *gr;
-
- gr = getgrnam(name);
- if (gr == NULL)
- return INVALID_ID;
- else
- return (int)(gr->gr_gid);
- }
-}
-
-static int isodigit(int c)
-{
- return (c >= '0' && c <= '7') ? 1 : 0;
-}
-
-/*
- * Whether the name is just digits string (uid/gid) already or not.
- * Return value:
- * 1: str is id
- * 0: str is not id
- */
-static int str_is_id(char *str)
-{
- if (str == NULL)
- return 0;
-
- if (*str == '0') {
- str++;
- if (*str == 'x' || *str == 'X') { /* for Hex. */
- if (!isxdigit(*(++str)))
- return 0;
-
- while (isxdigit(*(++str)));
- } else if (isodigit(*str)) { /* for Oct. */
- while (isodigit(*(++str)));
- }
- } else if (isdigit(*str)) { /* for Dec. */
- while (isdigit(*(++str)));
- }
-
- return (*str == 0) ? 1 : 0;
-}
-
-typedef struct {
- char *name;
- int length;
- int is_user;
- int next_token;
-} rmtacl_name_t;
-
-#define RMTACL_OPTNAME(name) name, sizeof(name) - 1
-
-static rmtacl_name_t rmtacl_namelist[] = {
- { RMTACL_OPTNAME("user:"), 1, 0 },
- { RMTACL_OPTNAME("group:"), 0, 0 },
- { RMTACL_OPTNAME("default:user:"), 1, 0 },
- { RMTACL_OPTNAME("default:group:"), 0, 0 },
- /* for --tabular option */
- { RMTACL_OPTNAME("user"), 1, 1 },
- { RMTACL_OPTNAME("group"), 0, 1 },
- { 0 }
-};
-
-static int rgetfacl_output(char *str)
-{
- char *start = NULL, *end = NULL;
- int is_user = 0, n, id;
- char c;
- rmtacl_name_t *rn;
-
- if (str == NULL)
- return -1;
-
- for (rn = rmtacl_namelist; rn->name; rn++) {
- if(strncmp(str, rn->name, rn->length) == 0) {
- if (!rn->next_token)
- start = str + rn->length;
- else
- start = next_token(str + rn->length, 0);
- is_user = rn->is_user;
- break;
- }
- }
-
- end = next_token(start, 1);
- if (end == NULL || start == end) {
- n = printf("%s", str);
- return n;
- }
-
- c = *end;
- *end = 0;
- id = rmtacl_name2id(start, is_user);
- if (id == INVALID_ID) {
- if (str_is_id(start)) {
- *end = c;
- n = printf("%s", str);
- } else
- return -1;
- } else if ((id == NOBODY_UID && is_user) ||
- (id == NOBODY_GID && !is_user)) {
- *end = c;
- n = printf("%s", str);
- } else {
- *end = c;
- *start = 0;
- n = printf("%s%d%s", str, id, end);
- }
- return n;
-}
-
-static int child_status(int status)
-{
- return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
-}
-
-static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
-{
- pid_t pid = 0;
- int fd[2], status, rc;
- FILE *fp;
- char buf[PIPE_BUF];
-
- if (output_func) {
- if (pipe(fd) < 0) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
- return rc;
- }
-
- pid = fork();
- if (pid < 0) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
- close(fd[0]);
- close(fd[1]);
- return rc;
- } else if (!pid) {
- /* child process redirects its output. */
- close(fd[0]);
- close(1);
- if (dup2(fd[1], 1) < 0) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc,
- "Can't dup2 %d\n", fd[1]);
- close(fd[1]);
- return rc;
- }
- } else {
- close(fd[1]);
- }
- }
-
- if (!pid) {
- status = rmtacl_notify(ops);
- if (status < 0)
- return -errno;
-
- exit(execvp(argv[0], argv));
- }
-
- /* the following is parent process */
- fp = fdopen(fd[0], "r");
- if (fp == NULL) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
- kill(pid, SIGKILL);
- close(fd[0]);
- return rc;
- }
-
- while (fgets(buf, PIPE_BUF, fp) != NULL) {
- if (output_func(buf) < 0)
- fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
- buf);
- }
- fclose(fp);
- close(fd[0]);
-
- if (waitpid(pid, &status, 0) < 0) {
- rc = -errno;
- llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
- return rc;
- }