1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
52 #include <sys/ioctl.h>
59 #include <sys/types.h>
60 #include <sys/syscall.h>
62 #ifdef HAVE_LINUX_UNISTD_H
63 #include <linux/unistd.h>
68 #include <liblustre.h>
69 #include <lnet/lnetctl.h>
71 #include <lustre_lib.h>
73 #include <lustre/liblustreapi.h>
75 static unsigned llapi_dir_filetype_table[] = {
84 #if defined(DT_DOOR) && defined(S_IFDOOR)
89 #if defined(DT_DOOR) && defined(S_IFDOOR)
90 static const int DT_MAX = DT_DOOR;
92 static const int DT_MAX = DT_SOCK;
95 static unsigned llapi_filetype_dir_table[] = {
104 #if defined(DT_DOOR) && defined(S_IFDOOR)
109 #if defined(DT_DOOR) && defined(S_IFDOOR)
110 static const int S_IFMAX = DT_DOOR;
112 static const int S_IFMAX = DT_SOCK;
115 /* liblustreapi message level */
116 static int llapi_msg_level = LLAPI_MSG_MAX;
118 void llapi_msg_set_level(int level)
120 /* ensure level is in the good range */
121 if (level < LLAPI_MSG_OFF)
122 llapi_msg_level = LLAPI_MSG_OFF;
123 else if (level > LLAPI_MSG_MAX)
124 llapi_msg_level = LLAPI_MSG_MAX;
126 llapi_msg_level = level;
129 void llapi_err(int level, char *fmt, ...)
132 int tmp_errno = abs(errno);
134 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
138 vfprintf(stderr, fmt, args);
141 if (level & LLAPI_MSG_NO_ERRNO)
142 fprintf(stderr, "\n");
144 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
147 #define llapi_err_noerrno(level, fmt, a...) \
148 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
150 void llapi_printf(int level, char *fmt, ...)
154 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
158 vfprintf(stdout, fmt, args);
162 int parse_size(char *optarg, unsigned long long *size,
163 unsigned long long *size_units)
167 *size = strtoul(optarg, &end, 0);
170 if ((*end == 'b') && *(end+1) == '\0' &&
171 (*size & (~0ULL << (64 - 9))) == 0) {
173 *size_units = 1 << 9;
174 } else if ((*end == 'k' || *end == 'K') &&
175 *(end+1) == '\0' && (*size &
176 (~0ULL << (64 - 10))) == 0) {
178 *size_units = 1 << 10;
179 } else if ((*end == 'm' || *end == 'M') &&
180 *(end+1) == '\0' && (*size &
181 (~0ULL << (64 - 20))) == 0) {
183 *size_units = 1 << 20;
184 } else if ((*end == 'g' || *end == 'G') &&
185 *(end+1) == '\0' && (*size &
186 (~0ULL << (64 - 30))) == 0) {
188 *size_units = 1 << 30;
189 } else if ((*end == 't' || *end == 'T') &&
190 *(end+1) == '\0' && (*size &
191 (~0ULL << (64 - 40))) == 0) {
193 *size_units = 1ULL << 40;
194 } else if ((*end == 'p' || *end == 'P') &&
195 *(end+1) == '\0' && (*size &
196 (~0ULL << (64 - 50))) == 0) {
198 *size_units = 1ULL << 50;
199 } else if ((*end == 'e' || *end == 'E') &&
200 *(end+1) == '\0' && (*size &
201 (~0ULL << (64 - 60))) == 0) {
203 *size_units = 1ULL << 60;
212 int llapi_file_open(const char *name, int flags, int mode,
213 unsigned long stripe_size, int stripe_offset,
214 int stripe_count, int stripe_pattern)
216 struct lov_user_md lum = { 0 };
221 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
222 if (fd < 0 && errno == EISDIR) {
223 fd = open(name, O_DIRECTORY | O_RDONLY);
229 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
233 /* 64 KB is the largest common page size I'm aware of (on ia64), but
234 * check the local page size just in case. */
235 page_size = LOV_MIN_STRIPE_SIZE;
236 if (getpagesize() > page_size) {
237 page_size = getpagesize();
238 llapi_err_noerrno(LLAPI_MSG_WARN,
239 "warning: your page size (%u) is "
240 "larger than expected (%u)", page_size,
241 LOV_MIN_STRIPE_SIZE);
243 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
244 errno = rc = -EINVAL;
245 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
246 "must be an even multiple of %d bytes",
247 stripe_size, page_size);
250 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
251 errno = rc = -EINVAL;
252 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
256 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
257 errno = rc = -EINVAL;
258 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
262 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
263 errno = rc = -EINVAL;
264 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
265 "stripe_count %u exceeds 4GB", stripe_size,
270 /* Initialize IOCTL striping pattern structure */
271 lum.lmm_magic = LOV_USER_MAGIC;
272 lum.lmm_pattern = stripe_pattern;
273 lum.lmm_stripe_size = stripe_size;
274 lum.lmm_stripe_count = stripe_count;
275 lum.lmm_stripe_offset = stripe_offset;
277 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
278 char *errmsg = "stripe already set";
280 if (errno != EEXIST && errno != EALREADY)
281 errmsg = strerror(errno);
283 llapi_err_noerrno(LLAPI_MSG_ERROR,
284 "error on ioctl "LPX64" for '%s' (%d): %s",
285 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
296 int llapi_file_create(const char *name, unsigned long stripe_size,
297 int stripe_offset, int stripe_count, int stripe_pattern)
301 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
302 stripe_offset, stripe_count, stripe_pattern);
310 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
311 void *data, cfs_dirent_t *de);
313 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
314 #define OBD_NOT_FOUND (-1)
316 static int common_param_init(struct find_param *param)
318 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
319 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
320 llapi_err(LLAPI_MSG_ERROR,
321 "error: allocation of %d bytes for ioctl",
322 sizeof(lstat_t) + param->lumlen);
326 param->got_uuids = 0;
327 param->obdindexes = NULL;
328 param->obdindex = OBD_NOT_FOUND;
332 static void find_param_fini(struct find_param *param)
334 if (param->obdindexes)
335 free(param->obdindexes);
341 int llapi_file_get_lov_fuuid(int fd, struct obd_uuid *lov_name)
343 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
346 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
351 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
355 fd = open(path, O_RDONLY);
358 llapi_err(LLAPI_MSG_ERROR, "error opening %s\n", path);
362 rc = llapi_file_get_lov_fuuid(fd, lov_uuid);
370 * If uuidp is NULL, return the number of available obd uuids.
371 * If uuidp is non-NULL, then it will return the uuids of the obds. If
372 * there are more OSTs then allocated to uuidp, then an error is returned with
373 * the ost_count set to number of available obd uuids.
375 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
377 struct obd_uuid lov_name;
380 int rc = 0, index = 0;
382 /* Get the lov name */
383 rc = llapi_file_get_lov_fuuid(fd, &lov_name);
387 /* Now get the ost uuids from /proc */
388 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
390 fp = fopen(buf, "r");
393 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
397 while (fgets(buf, sizeof(buf), fp) != NULL) {
398 if (uuidp && (index < *ost_count)) {
399 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
407 if (uuidp && (index >= *ost_count))
414 /* Here, param->obduuid points to a single obduuid, the index of which is
415 * returned in param->obdindex */
416 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
418 struct obd_uuid lov_uuid;
419 char uuid[sizeof(struct obd_uuid)];
424 /* Get the lov name */
425 rc = llapi_file_get_lov_fuuid(dirfd(dir), &lov_uuid);
427 if (errno != ENOTTY) {
429 llapi_err(LLAPI_MSG_ERROR,
430 "error: can't get lov name: %s", dname);
437 param->got_uuids = 1;
439 /* Now get the ost uuids from /proc */
440 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
442 fp = fopen(buf, "r");
445 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
449 if (!param->obduuid && !param->quiet && !param->obds_printed)
450 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
452 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
456 if (param->obduuid) {
457 if (strncmp(param->obduuid->uuid, uuid,
458 sizeof(uuid)) == 0) {
459 param->obdindex = index;
462 } else if (!param->quiet && !param->obds_printed) {
463 /* Print everything */
464 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
467 param->obds_printed = 1;
471 if (!param->quiet && param->obduuid &&
472 (param->obdindex == OBD_NOT_FOUND)) {
473 llapi_err_noerrno(LLAPI_MSG_ERROR,
474 "error: %s: unknown obduuid: %s",
475 __FUNCTION__, param->obduuid->uuid);
482 /* In this case, param->obduuid will be an array of obduuids and
483 * obd index for all these obduuids will be returned in
484 * param->obdindexes */
485 static int setup_obd_indexes(DIR *dir, struct find_param *param)
487 struct obd_uuid *uuids = NULL;
488 int obdcount = INIT_ALLOC_NUM_OSTS;
489 int ret, obd_valid = 0, obdnum, i;
491 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
492 sizeof(struct obd_uuid));
497 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
500 struct obd_uuid *uuids_temp;
502 if (ret == -EOVERFLOW) {
503 uuids_temp = realloc(uuids, obdcount *
504 sizeof(struct obd_uuid));
505 if (uuids_temp != NULL)
506 goto retry_get_uuids;
511 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
515 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
516 if (param->obdindexes == NULL)
519 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
520 for (i = 0; i <= obdcount; i++) {
521 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
522 (char *)&uuids[i]) == 0) {
523 param->obdindexes[obdnum] = i;
529 param->obdindexes[obdnum] = OBD_NOT_FOUND;
533 param->obdindex = OBD_NOT_FOUND;
535 param->obdindex = obd_valid;
537 param->got_uuids = 1;
542 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
543 int obdindex, int quiet, int header, int body)
545 int i, obdstripe = 0;
547 if (obdindex != OBD_NOT_FOUND) {
548 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
549 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
550 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
556 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
560 /* if it's a directory */
562 if (obdstripe == 1) {
563 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
564 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
565 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
567 llapi_printf(LLAPI_MSG_NORMAL,
568 "stripe_count: %d stripe_size: %u "
569 "stripe_offset: %d\n",
570 lum->lmm_stripe_count == (__u16)-1 ? -1 :
571 lum->lmm_stripe_count,
572 lum->lmm_stripe_size,
573 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
574 lum->lmm_stripe_offset);
579 if (header && (obdstripe == 1)) {
580 llapi_printf(LLAPI_MSG_NORMAL,
581 "lmm_magic: 0x%08X\n", lum->lmm_magic);
582 llapi_printf(LLAPI_MSG_NORMAL,
583 "lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
584 llapi_printf(LLAPI_MSG_NORMAL,
585 "lmm_object_id: "LPX64"\n", lum->lmm_object_id);
586 llapi_printf(LLAPI_MSG_NORMAL,
587 "lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
588 llapi_printf(LLAPI_MSG_NORMAL,
589 "lmm_stripe_size: %u\n", lum->lmm_stripe_size);
590 llapi_printf(LLAPI_MSG_NORMAL,
591 "lmm_stripe_pattern: %x\n", lum->lmm_pattern);
595 if ((!quiet) && (obdstripe == 1))
596 llapi_printf(LLAPI_MSG_NORMAL,
597 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
599 for (i = 0; i < lum->lmm_stripe_count; i++) {
600 int idx = lum->lmm_objects[i].l_ost_idx;
601 long long oid = lum->lmm_objects[i].l_object_id;
602 long long gr = lum->lmm_objects[i].l_object_gr;
603 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
604 llapi_printf(LLAPI_MSG_NORMAL,
605 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
607 obdindex == idx ? " *" : "");
609 llapi_printf(LLAPI_MSG_NORMAL, "\n");
613 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
614 int is_dir, int obdindex, int quiet,
615 int header, int body)
617 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
618 int i, obdstripe = 0;
620 if (obdindex != OBD_NOT_FOUND) {
621 for (i = 0; i < lumj->lmm_stripe_count; i++) {
622 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
623 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
629 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
633 if (header && obdstripe == 1) {
634 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
636 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
637 lumj->lmm_object_gr);
638 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
639 lumj->lmm_object_id);
640 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
641 (int)lumj->lmm_stripe_count);
642 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
643 lumj->lmm_stripe_size);
644 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
646 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
647 lumj->lmm_extent_count);
651 unsigned long long start = -1, end = 0;
652 if (!quiet && obdstripe == 1)
653 llapi_printf(LLAPI_MSG_NORMAL,
654 "joined\tobdidx\t\t objid\t\tobjid\t\t group"
655 "\t\tstart\t\tend\n");
656 for (i = 0; i < lumj->lmm_stripe_count; i++) {
657 int idx = lumj->lmm_objects[i].l_ost_idx;
658 long long oid = lumj->lmm_objects[i].l_object_id;
659 long long gr = lumj->lmm_objects[i].l_object_gr;
660 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
661 llapi_printf(LLAPI_MSG_NORMAL,
662 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
664 obdindex == idx ? " *" : "");
665 if (start != lumj->lmm_objects[i].l_extent_start ||
666 end != lumj->lmm_objects[i].l_extent_end) {
667 start = lumj->lmm_objects[i].l_extent_start;
668 llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start);
669 end = lumj->lmm_objects[i].l_extent_end;
670 if (end == (unsigned long long)-1)
671 llapi_printf(LLAPI_MSG_NORMAL,
674 llapi_printf(LLAPI_MSG_NORMAL,
677 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
680 llapi_printf(LLAPI_MSG_NORMAL, "\n");
684 void llapi_lov_dump_user_lmm(struct find_param *param,
685 char *path, int is_dir)
687 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
688 case LOV_USER_MAGIC_V1:
689 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
690 param->obdindex, param->quiet,
692 (param->verbose || !param->obduuid));
694 case LOV_USER_MAGIC_JOIN:
695 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
696 param->obdindex, param->quiet,
698 (param->verbose || !param->obduuid));
701 llapi_printf(LLAPI_MSG_NORMAL,
702 "unknown lmm_magic: %#x (expecting %#x)\n",
703 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
708 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
714 fname = strrchr(path, '/');
716 /* It should be a file (or other non-directory) */
718 dname = (char *)malloc(2);
722 fname = (char *)path;
724 dname = (char *)malloc(fname - path + 1);
727 strncpy(dname, path, fname - path);
728 dname[fname - path] = '\0';
732 if ((fd = open(dname, O_RDONLY)) == -1) {
738 strcpy((char *)lum, fname);
739 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
742 if (close(fd) == -1 && rc == 0)
750 int llapi_file_lookup(int dirfd, const char *name)
752 struct obd_ioctl_data data = { 0 };
757 if (dirfd < 0 || name == NULL)
760 data.ioc_version = OBD_IOCTL_VERSION;
761 data.ioc_len = sizeof(data);
762 data.ioc_inlbuf1 = (char *)name;
763 data.ioc_inllen1 = strlen(name) + 1;
765 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
767 llapi_err(LLAPI_MSG_ERROR,
768 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
773 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
776 int llapi_mds_getfileinfo(char *path, DIR *parent,
777 struct lov_user_mds_data *lmd)
779 lstat_t *st = &lmd->lmd_st;
780 char *fname = strrchr(path, '/');
786 fname = (fname == NULL ? path : fname + 1);
787 /* retrieve needed file info */
788 strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT));
789 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
792 if (errno == ENOTTY) {
793 /* ioctl is not supported, it is not a lustre fs.
794 * Do the regular lstat(2) instead. */
795 ret = lstat_f(path, st);
797 llapi_err(LLAPI_MSG_ERROR,
798 "error: %s: lstat failed for %s",
802 } else if (errno == ENOENT) {
803 llapi_err(LLAPI_MSG_WARN,
804 "warning: %s: %s does not exist",
808 llapi_err(LLAPI_MSG_ERROR,
809 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
818 static DIR *opendir_parent(char *path)
824 fname = strrchr(path, '/');
830 parent = opendir(path);
835 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
836 semantic_func_t sem_init,
837 semantic_func_t sem_fini, void *data,
848 if (!d && errno != ENOTDIR) {
849 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
852 } else if (!d && !parent) {
853 /* ENOTDIR. Open the parent dir. */
854 p = opendir_parent(path);
856 GOTO(out, ret = -EINVAL);
859 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
865 while ((dent = readdir64(d)) != NULL) {
866 ((struct find_param *)data)->have_fileinfo = 0;
868 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
872 if ((len + dent->d_reclen + 2) > size) {
873 llapi_err(LLAPI_MSG_ERROR,
874 "error: %s: string buffer is too small",
879 strcat(path, dent->d_name);
881 if (dent->d_type == DT_UNKNOWN) {
882 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
884 ret = llapi_mds_getfileinfo(path, d,
885 ((struct find_param *)data)->lmd);
887 ((struct find_param *)data)->have_fileinfo = 1;
888 dent->d_type = llapi_filetype_dir_table[st->st_mode &
895 switch (dent->d_type) {
897 llapi_err(LLAPI_MSG_ERROR,
898 "error: %s: '%s' is UNKNOWN type %d",
899 __FUNCTION__, dent->d_name, dent->d_type);
902 ret = llapi_semantic_traverse(path, size, d, sem_init,
903 sem_fini, data, dent);
910 ret = sem_init(path, d, NULL, data, dent);
914 if (sem_fini && ret == 0)
915 sem_fini(path, d, NULL, data, dent);
923 sem_fini(path, parent, d, data, de);
932 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
933 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
935 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
936 * The table bolow gives the answers for the specified parameters (value and
937 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
938 * --------------------------------------
939 * 1 | file > limit; sign > 0 | -1 / -1 |
940 * 2 | file = limit; sign > 0 | ? / 1 |
941 * 3 | file < limit; sign > 0 | ? / 1 |
942 * 4 | file > limit; sign = 0 | -1 / -1 |
943 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
944 * 6 | file < limit; sign = 0 | ? / -1 |
945 * 7 | file > limit; sign < 0 | 1 / 1 |
946 * 8 | file = limit; sign < 0 | ? / -1 |
947 * 9 | file < limit; sign < 0 | ? / -1 |
948 * --------------------------------------
949 * Note: 5th actually means that the value is within the interval
950 * (limit - margin, limit]. */
951 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
952 unsigned long long margin, int mds)
960 if (file <= limit && file + margin > limit)
962 if (file + margin <= limit)
976 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
977 * Return -1 or 1 if file timestamp does not or does match the given criteria
978 * correspondingly. Return 0 if the MDS time is being checked and there are
979 * attributes on OSTs and it is not yet clear if the timespamp matches.
981 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
982 * updated timestamps. */
983 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
988 /* Check if file is accepted. */
990 ret = find_value_cmp(st->st_atime, param->atime,
991 param->asign, 24 * 60 * 60, mds);
998 ret = find_value_cmp(st->st_mtime, param->mtime,
999 param->msign, 24 * 60 * 60, mds);
1003 /* If the previous check matches, but this one is not yet clear,
1004 * we should return 0 to do an RPC on OSTs. */
1010 ret = find_value_cmp(st->st_ctime, param->ctime,
1011 param->csign, 24 * 60 * 60, mds);
1015 /* If the previous check matches, but this one is not yet clear,
1016 * we should return 0 to do an RPC on OSTs. */
1024 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1025 void *data, cfs_dirent_t *de)
1027 struct find_param *param = (struct find_param *)data;
1028 int decision = 1; /* 1 is accepted; -1 is rejected. */
1029 lstat_t *st = ¶m->lmd->lmd_st;
1031 int checked_type = 0;
1034 LASSERT(parent != NULL || dir != NULL);
1036 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1038 /* If a regular expression is presented, make the initial decision */
1039 if (param->pattern != NULL) {
1040 char *fname = strrchr(path, '/');
1041 fname = (fname == NULL ? path : fname + 1);
1042 ret = fnmatch(param->pattern, fname, 0);
1043 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1044 (ret == 0 && param->exclude_pattern))
1048 /* See if we can check the file type from the dirent. */
1049 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1050 de->d_type < DT_MAX) {
1052 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1053 if (param->exclude_type)
1056 if (!param->exclude_type)
1062 /* If a time or OST should be checked, the decision is not taken yet. */
1063 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1068 /* Request MDS for the stat info. */
1069 if (param->have_fileinfo == 0) {
1071 /* retrieve needed file info */
1072 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1073 (void *)param->lmd);
1075 char *fname = strrchr(path, '/');
1076 fname = (fname == NULL ? path : fname + 1);
1078 /* retrieve needed file info */
1079 strncpy((char *)param->lmd, fname, param->lumlen);
1080 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1081 (void *)param->lmd);
1086 if (errno == ENOTTY) {
1087 /* ioctl is not supported, it is not a lustre fs.
1088 * Do the regular lstat(2) instead. */
1090 ret = lstat_f(path, st);
1092 llapi_err(LLAPI_MSG_ERROR,
1093 "error: %s: lstat failed for %s",
1094 __FUNCTION__, path);
1097 } else if (errno == ENOENT) {
1098 llapi_err(LLAPI_MSG_WARN,
1099 "warning: %s: %s does not exist",
1100 __FUNCTION__, path);
1103 llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
1104 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1105 "IOC_MDC_GETFILEINFO", path);
1110 if (param->type && !checked_type) {
1111 if ((st->st_mode & S_IFMT) == param->type) {
1112 if (param->exclude_type)
1115 if (!param->exclude_type)
1121 if (param->obduuid) {
1122 if (lustre_fs && param->got_uuids &&
1123 param->st_dev != st->st_dev) {
1124 /* A lustre/lustre mount point is crossed. */
1125 param->got_uuids = 0;
1126 param->obds_printed = 0;
1127 param->obdindex = OBD_NOT_FOUND;
1130 if (lustre_fs && !param->got_uuids) {
1131 ret = setup_obd_indexes(dir ? dir : parent, param);
1135 param->st_dev = st->st_dev;
1136 } else if (!lustre_fs && param->got_uuids) {
1137 /* A lustre/non-lustre mount point is crossed. */
1138 param->got_uuids = 0;
1139 param->obdindex = OBD_NOT_FOUND;
1143 /* If an OBD UUID is specified but no one matches, skip this file. */
1144 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1147 /* If a OST UUID is given, and some OST matches, check it here. */
1148 if (param->obdindex != OBD_NOT_FOUND) {
1149 if (!S_ISREG(st->st_mode))
1152 /* Only those files should be accepted, which have a
1153 * stripe on the specified OST. */
1154 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1159 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1160 for (j = 0; j < param->num_obds; j++) {
1161 if (param->obdindexes[j] ==
1162 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
1167 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1172 if (param->check_uid) {
1173 if (st->st_uid == param->uid) {
1174 if (param->exclude_uid)
1177 if (!param->exclude_uid)
1182 if (param->check_gid) {
1183 if (st->st_gid == param->gid) {
1184 if (param->exclude_gid)
1187 if (!param->exclude_gid)
1192 /* Check the time on mds. */
1196 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1197 param->lmd->lmd_lmm.lmm_stripe_count)
1199 decision = find_time_check(st, param, for_mds);
1203 /* If file still fits the request, ask osd for updated info.
1204 The regulat stat is almost of the same speed as some new
1205 'glimpse-size-ioctl'. */
1206 if (!decision && S_ISREG(st->st_mode) &&
1207 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1208 if (param->obdindex != OBD_NOT_FOUND) {
1209 /* Check whether the obd is active or not, if it is
1210 * not active, just print the object affected by this
1213 struct obd_statfs stat_buf;
1214 struct obd_uuid uuid_buf;
1216 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1217 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1218 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1219 param->obdindex, &stat_buf,
1222 if (ret == -ENODATA || ret == -ENODEV
1225 llapi_printf(LLAPI_MSG_NORMAL,
1226 "obd_uuid: %s failed %s ",
1227 param->obduuid->uuid,
1233 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1234 (void *)param->lmd);
1235 } else if (parent) {
1236 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1237 (void *)param->lmd);
1241 if (errno == ENOENT) {
1242 llapi_err(LLAPI_MSG_ERROR,
1243 "warning: %s: %s does not exist",
1244 __FUNCTION__, path);
1247 llapi_err(LLAPI_MSG_ERROR,
1248 "%s: IOC_LOV_GETINFO on %s failed",
1249 __FUNCTION__, path);
1254 /* Check the time on osc. */
1255 decision = find_time_check(st, param, 0);
1261 decision = find_value_cmp(st->st_size, param->size,
1262 param->size_sign, param->size_units,
1266 if (decision != -1) {
1267 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1269 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1271 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1275 /* Do not get down anymore? */
1276 if (param->depth == param->maxdepth)
1283 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1286 struct find_param *param = (struct find_param *)data;
1291 int llapi_find(char *path, struct find_param *param)
1294 int ret, len = strlen(path);
1296 if (len > PATH_MAX) {
1297 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1298 __FUNCTION__, path);
1302 buf = (char *)malloc(PATH_MAX + 1);
1306 ret = common_param_init(param);
1314 strncpy(buf, path, PATH_MAX + 1);
1315 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1316 cb_common_fini, param, NULL);
1318 find_param_fini(param);
1320 return ret < 0 ? ret : 0;
1323 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1326 struct find_param *param = (struct find_param *)data;
1329 LASSERT(parent != NULL || d != NULL);
1332 if (!param->got_uuids) {
1333 ret = setup_obd_uuid(d ? d : parent, path, param);
1339 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1340 (void *)¶m->lmd->lmd_lmm);
1341 } else if (parent) {
1342 char *fname = strrchr(path, '/');
1343 fname = (fname == NULL ? path : fname + 1);
1345 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1346 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1347 (void *)¶m->lmd->lmd_lmm);
1351 if (errno == ENODATA) {
1352 if (!param->obduuid && !param->quiet)
1353 llapi_printf(LLAPI_MSG_NORMAL,
1354 "%s has no stripe info\n", path);
1356 } else if (errno == ENOTTY) {
1357 llapi_err(LLAPI_MSG_ERROR,
1358 "%s: '%s' not on a Lustre fs?",
1359 __FUNCTION__, path);
1360 } else if (errno == ENOENT) {
1361 llapi_err(LLAPI_MSG_WARN,
1362 "warning: %s: %s does not exist",
1363 __FUNCTION__, path);
1366 llapi_err(LLAPI_MSG_ERROR,
1367 "error: %s: %s failed for %s",
1368 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1369 "IOC_MDC_GETFILESTRIPE", path);
1375 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1377 /* Do not get down anymore? */
1378 if (param->depth == param->maxdepth)
1385 int llapi_getstripe(char *path, struct find_param *param)
1388 int ret = 0, len = strlen(path);
1390 if (len > PATH_MAX) {
1391 llapi_err(LLAPI_MSG_ERROR,
1392 "%s: Path name '%s' is too long",
1393 __FUNCTION__, path);
1397 buf = (char *)malloc(PATH_MAX + 1);
1401 ret = common_param_init(param);
1409 strncpy(buf, path, PATH_MAX + 1);
1410 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1411 cb_common_fini, param, NULL);
1412 find_param_fini(param);
1414 return ret < 0 ? ret : 0;
1417 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1418 struct obd_statfs *stat_buf,
1419 struct obd_uuid *uuid_buf)
1422 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1424 struct obd_ioctl_data data = { 0 };
1427 data.ioc_inlbuf1 = (char *)&type;
1428 data.ioc_inllen1 = sizeof(__u32);
1429 data.ioc_inlbuf2 = (char *)&index;
1430 data.ioc_inllen2 = sizeof(__u32);
1431 data.ioc_pbuf1 = (char *)stat_buf;
1432 data.ioc_plen1 = sizeof(struct obd_statfs);
1433 data.ioc_pbuf2 = (char *)uuid_buf;
1434 data.ioc_plen2 = sizeof(struct obd_uuid);
1436 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1437 llapi_err(LLAPI_MSG_ERROR,
1438 "llapi_obd_statfs: error packing ioctl data");
1442 fd = open(path, O_RDONLY);
1443 if (errno == EISDIR)
1444 fd = open(path, O_DIRECTORY | O_RDONLY);
1447 rc = errno ? -errno : -EBADF;
1448 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1449 __FUNCTION__, path);
1452 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1454 rc = errno ? -errno : -EINVAL;
1460 #define MAX_STRING_SIZE 128
1461 #define DEVICES_LIST "/proc/fs/lustre/devices"
1463 int llapi_ping(char *obd_type, char *obd_name)
1465 char path[MAX_STRING_SIZE];
1469 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1470 obd_type, obd_name);
1472 fd = open(path, O_WRONLY);
1475 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1479 rc = write(fd, buf, 1);
1487 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1489 char buf[MAX_STRING_SIZE];
1490 FILE *fp = fopen(DEVICES_LIST, "r");
1495 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1499 while (fgets(buf, sizeof(buf), fp) != NULL) {
1500 char *obd_type_name = NULL;
1501 char *obd_name = NULL;
1502 char *obd_uuid = NULL;
1503 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1504 char *bufl = rawbuf;
1506 struct obd_ioctl_data datal = { 0, };
1507 struct obd_statfs osfs_buffer;
1509 while(bufp[0] == ' ')
1512 for(i = 0; i < 3; i++) {
1513 obd_type_name = strsep(&bufp, " ");
1515 obd_name = strsep(&bufp, " ");
1516 obd_uuid = strsep(&bufp, " ");
1518 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1520 memset(bufl, 0, sizeof(rawbuf));
1521 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1522 datal.ioc_plen1 = sizeof(osfs_buffer);
1524 for (i = 0; i < type_num; i++) {
1525 if (strcmp(obd_type_name, obd_type[i]) != 0)
1528 cb(obd_type_name, obd_name, obd_uuid, args);
1535 static void do_target_check(char *obd_type_name, char *obd_name,
1536 char *obd_uuid, void *args)
1540 rc = llapi_ping(obd_type_name, obd_name);
1541 if (rc == ENOTCONN) {
1542 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1544 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1546 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1550 int llapi_target_check(int type_num, char **obd_type, char *dir)
1552 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1555 #undef MAX_STRING_SIZE
1557 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1559 char raw[OBD_MAX_IOCTL_BUFFER];
1560 char out[LLOG_CHUNK_SIZE];
1562 struct obd_ioctl_data data = { 0 };
1567 sprintf(key, "%s", keyword);
1568 memset(raw, 0, sizeof(raw));
1569 memset(out, 0, sizeof(out));
1570 data.ioc_inlbuf1 = key;
1571 data.ioc_inllen1 = strlen(key) + 1;
1573 data.ioc_inlbuf2 = node_name;
1574 data.ioc_inllen2 = strlen(node_name) + 1;
1576 data.ioc_pbuf1 = out;
1577 data.ioc_plen1 = sizeof(out);
1578 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1582 root = opendir(dir);
1585 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1589 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1591 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1593 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1599 /* Is this a lustre fs? */
1600 int llapi_is_lustre_mnttype(const char *type)
1602 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1605 /* Is this a lustre client fs? */
1606 int llapi_is_lustre_mnt(struct mntent *mnt)
1608 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1609 strstr(mnt->mnt_fsname, ":/") != NULL);
1612 int llapi_quotacheck(char *mnt, int check_type)
1617 root = opendir(mnt);
1619 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1623 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1629 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1635 root = opendir(mnt);
1637 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1642 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1646 if (poll_intvl < 30)
1654 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1659 root = opendir(mnt);
1661 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1665 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1671 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1674 struct find_param *param = (struct find_param *)data;
1678 LASSERT(parent != NULL || d != NULL);
1681 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1682 (void *)param->lmd);
1683 } else if (parent) {
1684 char *fname = strrchr(path, '/');
1685 fname = (fname == NULL ? path : fname + 1);
1687 strncpy((char *)param->lmd, fname, param->lumlen);
1688 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1689 (void *)param->lmd);
1695 if (errno == ENODATA) {
1696 if (!param->obduuid && !param->quiet)
1697 llapi_err(LLAPI_MSG_ERROR,
1698 "%s has no stripe info", path);
1700 } else if (errno == ENOENT) {
1701 llapi_err(LLAPI_MSG_ERROR,
1702 "warning: %s: %s does not exist",
1703 __FUNCTION__, path);
1705 } else if (errno != EISDIR) {
1707 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
1708 d ? "LL_IOC_MDC_GETINFO" :
1709 "IOC_MDC_GETFILEINFO", path);
1714 st = ¶m->lmd->lmd_st;
1716 /* libc chown() will do extra check, and if the real owner is
1717 * the same as the ones to set, it won't fall into kernel, so
1718 * invoke syscall directly. */
1719 rc = syscall(SYS_chown, path, -1, -1);
1721 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
1723 rc = chmod(path, st->st_mode);
1725 llapi_err(LLAPI_MSG_ERROR,"error: chmod %s (%hu)", path, st->st_mode);
1730 int llapi_quotachown(char *path, int flag)
1732 struct find_param param;
1734 int ret = 0, len = strlen(path);
1736 if (len > PATH_MAX) {
1737 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1738 __FUNCTION__, path);
1742 buf = (char *)malloc(PATH_MAX + 1);
1746 memset(¶m, 0, sizeof(param));
1747 param.recursive = 1;
1751 ret = common_param_init(¶m);
1755 strncpy(buf, path, PATH_MAX + 1);
1756 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1757 NULL, ¶m, NULL);
1759 find_param_fini(¶m);
1767 #include <sys/wait.h>
1771 static int rmtacl_notify(int ops)
1775 int found = 0, fd, rc;
1777 fp = setmntent(MOUNTED, "r");
1779 perror("setmntent");
1784 mnt = getmntent(fp);
1788 if (!llapi_is_lustre_mnt(mnt))
1791 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
1797 rc = ioctl(fd, LL_IOC_RMTACL, ops);
1809 static char *next_token(char *p, int div)
1815 while (*p && *p != ':' && !isspace(*p))
1818 while (*p == ':' || isspace(*p))
1821 return *p ? p : NULL;
1824 static int rmtacl_name2id(char *name, int is_user)
1829 if ((pw = getpwnam(name)) == NULL)
1832 return (int)(pw->pw_uid);
1836 if ((gr = getgrnam(name)) == NULL)
1839 return (int)(gr->gr_gid);
1843 static int isodigit(int c)
1845 return (c >= '0' && c <= '7') ? 1 : 0;
1849 * Whether the name is just digits string (uid/gid) already or not.
1854 static int str_is_id(char *str)
1861 if (*str == 'x' || *str == 'X') { /* for Hex. */
1862 if (!isxdigit(*(++str)))
1865 while (isxdigit(*(++str)));
1866 } else if (isodigit(*str)) { /* for Oct. */
1867 while (isodigit(*(++str)));
1869 } else if (isdigit(*str)) { /* for Dec. */
1870 while (isdigit(*(++str)));
1873 return (*str == 0) ? 1 : 0;
1883 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
1885 static rmtacl_name_t rmtacl_namelist[] = {
1886 { RMTACL_OPTNAME("user:"), 1, 0 },
1887 { RMTACL_OPTNAME("group:"), 0, 0 },
1888 { RMTACL_OPTNAME("default:user:"), 1, 0 },
1889 { RMTACL_OPTNAME("default:group:"), 0, 0 },
1890 /* for --tabular option */
1891 { RMTACL_OPTNAME("user"), 1, 1 },
1892 { RMTACL_OPTNAME("group"), 0, 1 },
1896 static int rgetfacl_output(char *str)
1898 char *start = NULL, *end = NULL;
1899 int is_user = 0, n, id;
1906 for (rn = rmtacl_namelist; rn->name; rn++) {
1907 if(strncmp(str, rn->name, rn->length) == 0) {
1908 if (!rn->next_token)
1909 start = str + rn->length;
1911 start = next_token(str + rn->length, 0);
1912 is_user = rn->is_user;
1917 end = next_token(start, 1);
1918 if (end == NULL || start == end) {
1919 n = printf("%s", str);
1925 id = rmtacl_name2id(start, is_user);
1926 if (id == INVALID_ID) {
1927 if (str_is_id(start)) {
1929 n = printf("%s", str);
1932 } else if ((id == NOBODY_UID && is_user) ||
1933 (id == NOBODY_GID && !is_user)) {
1935 n = printf("%s", str);
1939 n = printf("%s%d%s", str, id, end);
1944 static int child_status(int status)
1946 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
1949 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
1962 if ((pid = fork()) < 0) {
1968 /* child process redirects its output. */
1971 if (dup2(fd[1], 1) < 0) {
1982 status = rmtacl_notify(ops);
1986 exit(execvp(argv[0], argv));
1989 /* the following is parent process */
1990 if ((fp = fdopen(fd[0], "r")) == NULL) {
1997 while (fgets(buf, PIPE_BUF, fp) != NULL) {
1998 if (output_func(buf) < 0)
1999 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2005 if (waitpid(pid, &status, 0) < 0) {
2010 return child_status(status);
2013 int llapi_lsetfacl(int argc, char *argv[])
2015 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2018 int llapi_lgetfacl(int argc, char *argv[])
2020 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2023 int llapi_rsetfacl(int argc, char *argv[])
2025 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2028 int llapi_rgetfacl(int argc, char *argv[])
2030 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2033 int llapi_cp(int argc, char *argv[])
2037 rc = rmtacl_notify(RMT_RSETFACL);
2041 exit(execvp(argv[0], argv));
2044 int llapi_ls(int argc, char *argv[])
2048 rc = rmtacl_notify(RMT_LGETFACL);
2052 exit(execvp(argv[0], argv));