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 [sun.com URL with a
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_ASM_TYPES_H
63 #include <asm/types.h>
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
71 #include <liblustre.h>
72 #include <lnet/lnetctl.h>
74 #include <lustre_lib.h>
76 #include <lustre/liblustreapi.h>
78 static unsigned llapi_dir_filetype_table[] = {
87 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 static const int DT_MAX = DT_DOOR;
95 static const int DT_MAX = DT_SOCK;
98 static unsigned llapi_filetype_dir_table[] = {
107 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 #if defined(DT_DOOR) && defined(S_IFDOOR)
113 static const int S_IFMAX = DT_DOOR;
115 static const int S_IFMAX = DT_SOCK;
118 /* liblustreapi message level */
119 static int llapi_msg_level = LLAPI_MSG_MAX;
121 void llapi_msg_set_level(int level)
123 /* ensure level is in the good range */
124 if (level < LLAPI_MSG_OFF)
125 llapi_msg_level = LLAPI_MSG_OFF;
126 else if (level > LLAPI_MSG_MAX)
127 llapi_msg_level = LLAPI_MSG_MAX;
129 llapi_msg_level = level;
132 void llapi_err(int level, char *fmt, ...)
135 int tmp_errno = abs(errno);
137 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
141 vfprintf(stderr, fmt, args);
144 if (level & LLAPI_MSG_NO_ERRNO)
145 fprintf(stderr, "\n");
147 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
150 #define llapi_err_noerrno(level, fmt, a...) \
151 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
153 void llapi_printf(int level, char *fmt, ...)
157 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
161 vfprintf(stdout, fmt, args);
165 int parse_size(char *optarg, unsigned long long *size,
166 unsigned long long *size_units)
170 *size = strtoul(optarg, &end, 0);
173 if ((*end == 'b') && *(end+1) == '\0' &&
174 (*size & (~0ULL << (64 - 9))) == 0) {
176 *size_units = 1 << 9;
177 } else if ((*end == 'k' || *end == 'K') &&
178 *(end+1) == '\0' && (*size &
179 (~0ULL << (64 - 10))) == 0) {
181 *size_units = 1 << 10;
182 } else if ((*end == 'm' || *end == 'M') &&
183 *(end+1) == '\0' && (*size &
184 (~0ULL << (64 - 20))) == 0) {
186 *size_units = 1 << 20;
187 } else if ((*end == 'g' || *end == 'G') &&
188 *(end+1) == '\0' && (*size &
189 (~0ULL << (64 - 30))) == 0) {
191 *size_units = 1 << 30;
192 } else if ((*end == 't' || *end == 'T') &&
193 *(end+1) == '\0' && (*size &
194 (~0ULL << (64 - 40))) == 0) {
196 *size_units = 1ULL << 40;
197 } else if ((*end == 'p' || *end == 'P') &&
198 *(end+1) == '\0' && (*size &
199 (~0ULL << (64 - 50))) == 0) {
201 *size_units = 1ULL << 50;
202 } else if ((*end == 'e' || *end == 'E') &&
203 *(end+1) == '\0' && (*size &
204 (~0ULL << (64 - 60))) == 0) {
206 *size_units = 1ULL << 60;
215 int llapi_file_open(const char *name, int flags, int mode,
216 unsigned long stripe_size, int stripe_offset,
217 int stripe_count, int stripe_pattern)
219 struct lov_user_md lum = { 0 };
224 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
225 if (fd < 0 && errno == EISDIR) {
226 fd = open(name, O_DIRECTORY | O_RDONLY);
232 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
236 /* 64 KB is the largest common page size I'm aware of (on ia64), but
237 * check the local page size just in case. */
238 page_size = LOV_MIN_STRIPE_SIZE;
239 if (getpagesize() > page_size) {
240 page_size = getpagesize();
241 llapi_err_noerrno(LLAPI_MSG_WARN,
242 "warning: your page size (%u) is "
243 "larger than expected (%u)", page_size,
244 LOV_MIN_STRIPE_SIZE);
246 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
247 errno = rc = -EINVAL;
248 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
249 "must be an even multiple of %d bytes",
250 stripe_size, page_size);
253 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
254 errno = rc = -EINVAL;
255 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
259 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
260 errno = rc = -EINVAL;
261 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
265 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
266 errno = rc = -EINVAL;
267 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
268 "stripe_count %u exceeds 4GB", stripe_size,
273 /* Initialize IOCTL striping pattern structure */
274 lum.lmm_magic = LOV_USER_MAGIC;
275 lum.lmm_pattern = stripe_pattern;
276 lum.lmm_stripe_size = stripe_size;
277 lum.lmm_stripe_count = stripe_count;
278 lum.lmm_stripe_offset = stripe_offset;
280 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
281 char *errmsg = "stripe already set";
283 if (errno != EEXIST && errno != EALREADY)
284 errmsg = strerror(errno);
286 llapi_err_noerrno(LLAPI_MSG_ERROR,
287 "error on ioctl "LPX64" for '%s' (%d): %s",
288 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
299 int llapi_file_create(const char *name, unsigned long stripe_size,
300 int stripe_offset, int stripe_count, int stripe_pattern)
304 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
305 stripe_offset, stripe_count, stripe_pattern);
313 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
314 void *data, cfs_dirent_t *de);
316 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
317 #define OBD_NOT_FOUND (-1)
319 static int common_param_init(struct find_param *param)
321 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
322 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
323 llapi_err(LLAPI_MSG_ERROR,
324 "error: allocation of %d bytes for ioctl",
325 sizeof(lstat_t) + param->lumlen);
329 param->got_uuids = 0;
330 param->obdindexes = NULL;
331 param->obdindex = OBD_NOT_FOUND;
335 static void find_param_fini(struct find_param *param)
337 if (param->obdindexes)
338 free(param->obdindexes);
344 int llapi_file_get_lov_fuuid(int fd, struct obd_uuid *lov_name)
346 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
349 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
354 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
358 fd = open(path, O_RDONLY);
361 llapi_err(LLAPI_MSG_ERROR, "error opening %s\n", path);
365 rc = llapi_file_get_lov_fuuid(fd, lov_uuid);
373 * If uuidp is NULL, return the number of available obd uuids.
374 * If uuidp is non-NULL, then it will return the uuids of the obds. If
375 * there are more OSTs then allocated to uuidp, then an error is returned with
376 * the ost_count set to number of available obd uuids.
378 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
380 struct obd_uuid lov_name;
383 int rc = 0, index = 0;
385 /* Get the lov name */
386 rc = llapi_file_get_lov_fuuid(fd, &lov_name);
390 /* Now get the ost uuids from /proc */
391 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
393 fp = fopen(buf, "r");
396 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
400 while (fgets(buf, sizeof(buf), fp) != NULL) {
401 if (uuidp && (index < *ost_count)) {
402 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
410 if (uuidp && (index >= *ost_count))
417 /* Here, param->obduuid points to a single obduuid, the index of which is
418 * returned in param->obdindex */
419 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
421 struct obd_uuid lov_uuid;
422 char uuid[sizeof(struct obd_uuid)];
427 /* Get the lov name */
428 rc = llapi_file_get_lov_fuuid(dirfd(dir), &lov_uuid);
430 if (errno != ENOTTY) {
432 llapi_err(LLAPI_MSG_ERROR,
433 "error: can't get lov name: %s", dname);
440 param->got_uuids = 1;
442 /* Now get the ost uuids from /proc */
443 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
445 fp = fopen(buf, "r");
448 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
452 if (!param->obduuid && !param->quiet && !param->obds_printed)
453 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
455 while (fgets(buf, sizeof(buf), fp) != NULL) {
456 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
459 if (param->obduuid) {
460 if (strncmp(param->obduuid->uuid, uuid,
461 sizeof(uuid)) == 0) {
462 param->obdindex = index;
465 } else if (!param->quiet && !param->obds_printed) {
466 /* Print everything */
467 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
470 param->obds_printed = 1;
474 if (!param->quiet && param->obduuid &&
475 (param->obdindex == OBD_NOT_FOUND)) {
476 llapi_err_noerrno(LLAPI_MSG_ERROR,
477 "error: %s: unknown obduuid: %s",
478 __FUNCTION__, param->obduuid->uuid);
485 /* In this case, param->obduuid will be an array of obduuids and
486 * obd index for all these obduuids will be returned in
487 * param->obdindexes */
488 static int setup_obd_indexes(DIR *dir, struct find_param *param)
490 struct obd_uuid *uuids = NULL;
491 int obdcount = INIT_ALLOC_NUM_OSTS;
492 int ret, obd_valid = 0, obdnum, i;
494 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
495 sizeof(struct obd_uuid));
500 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
503 struct obd_uuid *uuids_temp;
505 if (ret == -EOVERFLOW) {
506 uuids_temp = realloc(uuids, obdcount *
507 sizeof(struct obd_uuid));
508 if (uuids_temp != NULL)
509 goto retry_get_uuids;
514 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
518 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
519 if (param->obdindexes == NULL)
522 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
523 for (i = 0; i <= obdcount; i++) {
524 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
525 (char *)&uuids[i]) == 0) {
526 param->obdindexes[obdnum] = i;
532 param->obdindexes[obdnum] = OBD_NOT_FOUND;
536 param->obdindex = OBD_NOT_FOUND;
538 param->obdindex = obd_valid;
540 param->got_uuids = 1;
545 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
546 int obdindex, int quiet, int header, int body)
548 int i, obdstripe = 0;
550 if (obdindex != OBD_NOT_FOUND) {
551 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
552 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
553 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
559 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
563 /* if it's a directory */
565 if (obdstripe == 1) {
566 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
567 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
568 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
570 llapi_printf(LLAPI_MSG_NORMAL,
571 "stripe_count: %d stripe_size: %u "
572 "stripe_offset: %d\n",
573 lum->lmm_stripe_count == (__u16)-1 ? -1 :
574 lum->lmm_stripe_count,
575 lum->lmm_stripe_size,
576 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
577 lum->lmm_stripe_offset);
582 if (header && (obdstripe == 1)) {
583 llapi_printf(LLAPI_MSG_NORMAL,
584 "lmm_magic: 0x%08X\n", lum->lmm_magic);
585 llapi_printf(LLAPI_MSG_NORMAL,
586 "lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
587 llapi_printf(LLAPI_MSG_NORMAL,
588 "lmm_object_id: "LPX64"\n", lum->lmm_object_id);
589 llapi_printf(LLAPI_MSG_NORMAL,
590 "lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
591 llapi_printf(LLAPI_MSG_NORMAL,
592 "lmm_stripe_size: %u\n", lum->lmm_stripe_size);
593 llapi_printf(LLAPI_MSG_NORMAL,
594 "lmm_stripe_pattern: %x\n", lum->lmm_pattern);
598 if ((!quiet) && (obdstripe == 1))
599 llapi_printf(LLAPI_MSG_NORMAL,
600 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
602 for (i = 0; i < lum->lmm_stripe_count; i++) {
603 int idx = lum->lmm_objects[i].l_ost_idx;
604 long long oid = lum->lmm_objects[i].l_object_id;
605 long long gr = lum->lmm_objects[i].l_object_gr;
606 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
607 llapi_printf(LLAPI_MSG_NORMAL,
608 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
610 obdindex == idx ? " *" : "");
612 llapi_printf(LLAPI_MSG_NORMAL, "\n");
616 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
617 int is_dir, int obdindex, int quiet,
618 int header, int body)
620 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
621 int i, obdstripe = 0;
623 if (obdindex != OBD_NOT_FOUND) {
624 for (i = 0; i < lumj->lmm_stripe_count; i++) {
625 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
626 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
632 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
636 if (header && obdstripe == 1) {
637 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
639 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
640 lumj->lmm_object_gr);
641 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
642 lumj->lmm_object_id);
643 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
644 (int)lumj->lmm_stripe_count);
645 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
646 lumj->lmm_stripe_size);
647 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
649 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
650 lumj->lmm_extent_count);
654 unsigned long long start = -1, end = 0;
655 if (!quiet && obdstripe == 1)
656 llapi_printf(LLAPI_MSG_NORMAL,
657 "joined\tobdidx\t\t objid\t\tobjid\t\t group"
658 "\t\tstart\t\tend\n");
659 for (i = 0; i < lumj->lmm_stripe_count; i++) {
660 int idx = lumj->lmm_objects[i].l_ost_idx;
661 long long oid = lumj->lmm_objects[i].l_object_id;
662 long long gr = lumj->lmm_objects[i].l_object_gr;
663 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
664 llapi_printf(LLAPI_MSG_NORMAL,
665 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
667 obdindex == idx ? " *" : "");
668 if (start != lumj->lmm_objects[i].l_extent_start ||
669 end != lumj->lmm_objects[i].l_extent_end) {
670 start = lumj->lmm_objects[i].l_extent_start;
671 llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start);
672 end = lumj->lmm_objects[i].l_extent_end;
673 if (end == (unsigned long long)-1)
674 llapi_printf(LLAPI_MSG_NORMAL,
677 llapi_printf(LLAPI_MSG_NORMAL,
680 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
683 llapi_printf(LLAPI_MSG_NORMAL, "\n");
687 void llapi_lov_dump_user_lmm(struct find_param *param,
688 char *path, int is_dir)
690 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
691 case LOV_USER_MAGIC_V1:
692 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
693 param->obdindex, param->quiet,
695 (param->verbose || !param->obduuid));
697 case LOV_USER_MAGIC_JOIN:
698 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
699 param->obdindex, param->quiet,
701 (param->verbose || !param->obduuid));
704 llapi_printf(LLAPI_MSG_NORMAL,
705 "unknown lmm_magic: %#x (expecting %#x)\n",
706 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
711 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
717 fname = strrchr(path, '/');
719 /* It should be a file (or other non-directory) */
721 dname = (char *)malloc(2);
725 fname = (char *)path;
727 dname = (char *)malloc(fname - path + 1);
730 strncpy(dname, path, fname - path);
731 dname[fname - path] = '\0';
735 if ((fd = open(dname, O_RDONLY)) == -1) {
741 strcpy((char *)lum, fname);
742 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
745 if (close(fd) == -1 && rc == 0)
753 int llapi_file_lookup(int dirfd, const char *name)
755 struct obd_ioctl_data data = { 0 };
760 if (dirfd < 0 || name == NULL)
763 data.ioc_version = OBD_IOCTL_VERSION;
764 data.ioc_len = sizeof(data);
765 data.ioc_inlbuf1 = (char *)name;
766 data.ioc_inllen1 = strlen(name) + 1;
768 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
770 llapi_err(LLAPI_MSG_ERROR,
771 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
776 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
779 int llapi_mds_getfileinfo(char *path, DIR *parent,
780 struct lov_user_mds_data *lmd)
782 lstat_t *st = &lmd->lmd_st;
783 char *fname = strrchr(path, '/');
789 fname = (fname == NULL ? path : fname + 1);
790 /* retrieve needed file info */
791 strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT));
792 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
795 if (errno == ENOTTY) {
796 /* ioctl is not supported, it is not a lustre fs.
797 * Do the regular lstat(2) instead. */
798 ret = lstat_f(path, st);
800 llapi_err(LLAPI_MSG_ERROR,
801 "error: %s: lstat failed for %s",
805 } else if (errno == ENOENT) {
806 llapi_err(LLAPI_MSG_WARN,
807 "warning: %s: %s does not exist",
811 llapi_err(LLAPI_MSG_ERROR,
812 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
821 static DIR *opendir_parent(char *path)
827 fname = strrchr(path, '/');
833 parent = opendir(path);
838 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
839 semantic_func_t sem_init,
840 semantic_func_t sem_fini, void *data,
851 if (!d && errno != ENOTDIR) {
852 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
855 } else if (!d && !parent) {
856 /* ENOTDIR. Open the parent dir. */
857 p = opendir_parent(path);
859 GOTO(out, ret = -EINVAL);
862 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
868 while ((dent = readdir64(d)) != NULL) {
869 ((struct find_param *)data)->have_fileinfo = 0;
871 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
875 if ((len + dent->d_reclen + 2) > size) {
876 llapi_err(LLAPI_MSG_ERROR,
877 "error: %s: string buffer is too small",
882 strcat(path, dent->d_name);
884 if (dent->d_type == DT_UNKNOWN) {
885 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
887 ret = llapi_mds_getfileinfo(path, d,
888 ((struct find_param *)data)->lmd);
890 ((struct find_param *)data)->have_fileinfo = 1;
891 dent->d_type = llapi_filetype_dir_table[st->st_mode &
898 switch (dent->d_type) {
900 llapi_err(LLAPI_MSG_ERROR,
901 "error: %s: '%s' is UNKNOWN type %d",
902 __FUNCTION__, dent->d_name, dent->d_type);
905 ret = llapi_semantic_traverse(path, size, d, sem_init,
906 sem_fini, data, dent);
913 ret = sem_init(path, d, NULL, data, dent);
917 if (sem_fini && ret == 0)
918 sem_fini(path, d, NULL, data, dent);
926 sem_fini(path, parent, d, data, de);
935 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
936 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
938 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
939 * The table bolow gives the answers for the specified parameters (value and
940 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
941 * --------------------------------------
942 * 1 | file > limit; sign > 0 | -1 / -1 |
943 * 2 | file = limit; sign > 0 | ? / 1 |
944 * 3 | file < limit; sign > 0 | ? / 1 |
945 * 4 | file > limit; sign = 0 | -1 / -1 |
946 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
947 * 6 | file < limit; sign = 0 | ? / -1 |
948 * 7 | file > limit; sign < 0 | 1 / 1 |
949 * 8 | file = limit; sign < 0 | ? / -1 |
950 * 9 | file < limit; sign < 0 | ? / -1 |
951 * --------------------------------------
952 * Note: 5th actually means that the value is within the interval
953 * (limit - margin, limit]. */
954 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
955 unsigned long long margin, int mds)
963 if (file <= limit && file + margin > limit)
965 if (file + margin <= limit)
979 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
980 * Return -1 or 1 if file timestamp does not or does match the given criteria
981 * correspondingly. Return 0 if the MDS time is being checked and there are
982 * attributes on OSTs and it is not yet clear if the timespamp matches.
984 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
985 * updated timestamps. */
986 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
991 /* Check if file is accepted. */
993 ret = find_value_cmp(st->st_atime, param->atime,
994 param->asign, 24 * 60 * 60, mds);
1001 ret = find_value_cmp(st->st_mtime, param->mtime,
1002 param->msign, 24 * 60 * 60, mds);
1006 /* If the previous check matches, but this one is not yet clear,
1007 * we should return 0 to do an RPC on OSTs. */
1013 ret = find_value_cmp(st->st_ctime, param->ctime,
1014 param->csign, 24 * 60 * 60, mds);
1018 /* If the previous check matches, but this one is not yet clear,
1019 * we should return 0 to do an RPC on OSTs. */
1027 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1028 void *data, cfs_dirent_t *de)
1030 struct find_param *param = (struct find_param *)data;
1031 int decision = 1; /* 1 is accepted; -1 is rejected. */
1032 lstat_t *st = ¶m->lmd->lmd_st;
1034 int checked_type = 0;
1037 LASSERT(parent != NULL || dir != NULL);
1039 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1041 /* If a regular expression is presented, make the initial decision */
1042 if (param->pattern != NULL) {
1043 char *fname = strrchr(path, '/');
1044 fname = (fname == NULL ? path : fname + 1);
1045 ret = fnmatch(param->pattern, fname, 0);
1046 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1047 (ret == 0 && param->exclude_pattern))
1051 /* See if we can check the file type from the dirent. */
1052 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1053 de->d_type < DT_MAX) {
1055 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1056 if (param->exclude_type)
1059 if (!param->exclude_type)
1065 /* If a time or OST should be checked, the decision is not taken yet. */
1066 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1071 /* Request MDS for the stat info. */
1072 if (param->have_fileinfo == 0) {
1074 /* retrieve needed file info */
1075 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1076 (void *)param->lmd);
1078 char *fname = strrchr(path, '/');
1079 fname = (fname == NULL ? path : fname + 1);
1081 /* retrieve needed file info */
1082 strncpy((char *)param->lmd, fname, param->lumlen);
1083 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1084 (void *)param->lmd);
1089 if (errno == ENOTTY) {
1090 /* ioctl is not supported, it is not a lustre fs.
1091 * Do the regular lstat(2) instead. */
1093 ret = lstat_f(path, st);
1095 llapi_err(LLAPI_MSG_ERROR,
1096 "error: %s: lstat failed for %s",
1097 __FUNCTION__, path);
1100 } else if (errno == ENOENT) {
1101 llapi_err(LLAPI_MSG_WARN,
1102 "warning: %s: %s does not exist",
1103 __FUNCTION__, path);
1106 llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
1107 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1108 "IOC_MDC_GETFILEINFO", path);
1113 if (param->type && !checked_type) {
1114 if ((st->st_mode & S_IFMT) == param->type) {
1115 if (param->exclude_type)
1118 if (!param->exclude_type)
1124 if (param->obduuid) {
1125 if (lustre_fs && param->got_uuids &&
1126 param->st_dev != st->st_dev) {
1127 /* A lustre/lustre mount point is crossed. */
1128 param->got_uuids = 0;
1129 param->obds_printed = 0;
1130 param->obdindex = OBD_NOT_FOUND;
1133 if (lustre_fs && !param->got_uuids) {
1134 ret = setup_obd_indexes(dir ? dir : parent, param);
1138 param->st_dev = st->st_dev;
1139 } else if (!lustre_fs && param->got_uuids) {
1140 /* A lustre/non-lustre mount point is crossed. */
1141 param->got_uuids = 0;
1142 param->obdindex = OBD_NOT_FOUND;
1146 /* If an OBD UUID is specified but no one matches, skip this file. */
1147 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1150 /* If a OST UUID is given, and some OST matches, check it here. */
1151 if (param->obdindex != OBD_NOT_FOUND) {
1152 if (!S_ISREG(st->st_mode))
1155 /* Only those files should be accepted, which have a
1156 * stripe on the specified OST. */
1157 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1162 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1163 for (j = 0; j < param->num_obds; j++) {
1164 if (param->obdindexes[j] ==
1165 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
1170 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1175 if (param->check_uid) {
1176 if (st->st_uid == param->uid) {
1177 if (param->exclude_uid)
1180 if (!param->exclude_uid)
1185 if (param->check_gid) {
1186 if (st->st_gid == param->gid) {
1187 if (param->exclude_gid)
1190 if (!param->exclude_gid)
1195 /* Check the time on mds. */
1199 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1200 param->lmd->lmd_lmm.lmm_stripe_count)
1202 decision = find_time_check(st, param, for_mds);
1206 /* If file still fits the request, ask osd for updated info.
1207 The regulat stat is almost of the same speed as some new
1208 'glimpse-size-ioctl'. */
1209 if (!decision && S_ISREG(st->st_mode) &&
1210 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1211 if (param->obdindex != OBD_NOT_FOUND) {
1212 /* Check whether the obd is active or not, if it is
1213 * not active, just print the object affected by this
1216 struct obd_statfs stat_buf;
1217 struct obd_uuid uuid_buf;
1219 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1220 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1221 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1222 param->obdindex, &stat_buf,
1225 if (ret == -ENODATA || ret == -ENODEV
1228 llapi_printf(LLAPI_MSG_NORMAL,
1229 "obd_uuid: %s failed %s ",
1230 param->obduuid->uuid,
1236 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1237 (void *)param->lmd);
1238 } else if (parent) {
1239 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1240 (void *)param->lmd);
1244 if (errno == ENOENT) {
1245 llapi_err(LLAPI_MSG_ERROR,
1246 "warning: %s: %s does not exist",
1247 __FUNCTION__, path);
1250 llapi_err(LLAPI_MSG_ERROR,
1251 "%s: IOC_LOV_GETINFO on %s failed",
1252 __FUNCTION__, path);
1257 /* Check the time on osc. */
1258 decision = find_time_check(st, param, 0);
1264 decision = find_value_cmp(st->st_size, param->size,
1265 param->size_sign, param->size_units,
1269 if (decision != -1) {
1270 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1272 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1274 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1278 /* Do not get down anymore? */
1279 if (param->depth == param->maxdepth)
1286 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1289 struct find_param *param = (struct find_param *)data;
1294 int llapi_find(char *path, struct find_param *param)
1297 int ret, len = strlen(path);
1299 if (len > PATH_MAX) {
1300 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1301 __FUNCTION__, path);
1305 buf = (char *)malloc(PATH_MAX + 1);
1309 ret = common_param_init(param);
1317 strncpy(buf, path, PATH_MAX + 1);
1318 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1319 cb_common_fini, param, NULL);
1321 find_param_fini(param);
1323 return ret < 0 ? ret : 0;
1326 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1329 struct find_param *param = (struct find_param *)data;
1332 LASSERT(parent != NULL || d != NULL);
1335 if (!param->got_uuids) {
1336 ret = setup_obd_uuid(d ? d : parent, path, param);
1342 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1343 (void *)¶m->lmd->lmd_lmm);
1344 } else if (parent) {
1345 char *fname = strrchr(path, '/');
1346 fname = (fname == NULL ? path : fname + 1);
1348 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1349 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1350 (void *)¶m->lmd->lmd_lmm);
1354 if (errno == ENODATA) {
1355 if (!param->obduuid && !param->quiet)
1356 llapi_printf(LLAPI_MSG_NORMAL,
1357 "%s has no stripe info\n", path);
1359 } else if (errno == ENOTTY) {
1360 llapi_err(LLAPI_MSG_ERROR,
1361 "%s: '%s' not on a Lustre fs?",
1362 __FUNCTION__, path);
1363 } else if (errno == ENOENT) {
1364 llapi_err(LLAPI_MSG_WARN,
1365 "warning: %s: %s does not exist",
1366 __FUNCTION__, path);
1369 llapi_err(LLAPI_MSG_ERROR,
1370 "error: %s: %s failed for %s",
1371 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1372 "IOC_MDC_GETFILESTRIPE", path);
1378 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1380 /* Do not get down anymore? */
1381 if (param->depth == param->maxdepth)
1388 int llapi_getstripe(char *path, struct find_param *param)
1391 int ret = 0, len = strlen(path);
1393 if (len > PATH_MAX) {
1394 llapi_err(LLAPI_MSG_ERROR,
1395 "%s: Path name '%s' is too long",
1396 __FUNCTION__, path);
1400 buf = (char *)malloc(PATH_MAX + 1);
1404 ret = common_param_init(param);
1412 strncpy(buf, path, PATH_MAX + 1);
1413 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1414 cb_common_fini, param, NULL);
1415 find_param_fini(param);
1417 return ret < 0 ? ret : 0;
1420 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1421 struct obd_statfs *stat_buf,
1422 struct obd_uuid *uuid_buf)
1425 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1427 struct obd_ioctl_data data = { 0 };
1430 data.ioc_inlbuf1 = (char *)&type;
1431 data.ioc_inllen1 = sizeof(__u32);
1432 data.ioc_inlbuf2 = (char *)&index;
1433 data.ioc_inllen2 = sizeof(__u32);
1434 data.ioc_pbuf1 = (char *)stat_buf;
1435 data.ioc_plen1 = sizeof(struct obd_statfs);
1436 data.ioc_pbuf2 = (char *)uuid_buf;
1437 data.ioc_plen2 = sizeof(struct obd_uuid);
1439 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1440 llapi_err(LLAPI_MSG_ERROR,
1441 "llapi_obd_statfs: error packing ioctl data");
1445 fd = open(path, O_RDONLY);
1446 if (errno == EISDIR)
1447 fd = open(path, O_DIRECTORY | O_RDONLY);
1450 rc = errno ? -errno : -EBADF;
1451 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1452 __FUNCTION__, path);
1455 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1457 rc = errno ? -errno : -EINVAL;
1463 #define MAX_STRING_SIZE 128
1464 #define DEVICES_LIST "/proc/fs/lustre/devices"
1466 int llapi_ping(char *obd_type, char *obd_name)
1468 char path[MAX_STRING_SIZE];
1472 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1473 obd_type, obd_name);
1475 fd = open(path, O_WRONLY);
1478 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1482 rc = write(fd, buf, 1);
1490 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1492 char buf[MAX_STRING_SIZE];
1493 FILE *fp = fopen(DEVICES_LIST, "r");
1498 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1502 while (fgets(buf, sizeof(buf), fp) != NULL) {
1503 char *obd_type_name = NULL;
1504 char *obd_name = NULL;
1505 char *obd_uuid = NULL;
1506 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1507 char *bufl = rawbuf;
1509 struct obd_ioctl_data datal = { 0, };
1510 struct obd_statfs osfs_buffer;
1512 while(bufp[0] == ' ')
1515 for(i = 0; i < 3; i++) {
1516 obd_type_name = strsep(&bufp, " ");
1518 obd_name = strsep(&bufp, " ");
1519 obd_uuid = strsep(&bufp, " ");
1521 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1523 memset(bufl, 0, sizeof(rawbuf));
1524 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1525 datal.ioc_plen1 = sizeof(osfs_buffer);
1527 for (i = 0; i < type_num; i++) {
1528 if (strcmp(obd_type_name, obd_type[i]) != 0)
1531 cb(obd_type_name, obd_name, obd_uuid, args);
1538 static void do_target_check(char *obd_type_name, char *obd_name,
1539 char *obd_uuid, void *args)
1543 rc = llapi_ping(obd_type_name, obd_name);
1544 if (rc == ENOTCONN) {
1545 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1547 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1549 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1553 int llapi_target_check(int type_num, char **obd_type, char *dir)
1555 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1558 #undef MAX_STRING_SIZE
1560 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1562 char raw[OBD_MAX_IOCTL_BUFFER];
1563 char out[LLOG_CHUNK_SIZE];
1565 struct obd_ioctl_data data = { 0 };
1570 sprintf(key, "%s", keyword);
1571 memset(raw, 0, sizeof(raw));
1572 memset(out, 0, sizeof(out));
1573 data.ioc_inlbuf1 = key;
1574 data.ioc_inllen1 = strlen(key) + 1;
1576 data.ioc_inlbuf2 = node_name;
1577 data.ioc_inllen2 = strlen(node_name) + 1;
1579 data.ioc_pbuf1 = out;
1580 data.ioc_plen1 = sizeof(out);
1581 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1585 root = opendir(dir);
1588 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1592 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1594 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1596 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1602 /* Is this a lustre fs? */
1603 int llapi_is_lustre_mnttype(const char *type)
1605 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1608 /* Is this a lustre client fs? */
1609 int llapi_is_lustre_mnt(struct mntent *mnt)
1611 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1612 strstr(mnt->mnt_fsname, ":/") != NULL);
1615 int llapi_quotacheck(char *mnt, int check_type)
1620 root = opendir(mnt);
1622 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1626 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1632 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1638 root = opendir(mnt);
1640 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1645 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1649 if (poll_intvl < 30)
1657 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1662 root = opendir(mnt);
1664 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1668 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1674 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1677 struct find_param *param = (struct find_param *)data;
1681 LASSERT(parent != NULL || d != NULL);
1684 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1685 (void *)param->lmd);
1686 } else if (parent) {
1687 char *fname = strrchr(path, '/');
1688 fname = (fname == NULL ? path : fname + 1);
1690 strncpy((char *)param->lmd, fname, param->lumlen);
1691 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1692 (void *)param->lmd);
1698 if (errno == ENODATA) {
1699 if (!param->obduuid && !param->quiet)
1700 llapi_err(LLAPI_MSG_ERROR,
1701 "%s has no stripe info", path);
1703 } else if (errno == ENOENT) {
1704 llapi_err(LLAPI_MSG_ERROR,
1705 "warning: %s: %s does not exist",
1706 __FUNCTION__, path);
1708 } else if (errno != EISDIR) {
1710 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
1711 d ? "LL_IOC_MDC_GETINFO" :
1712 "IOC_MDC_GETFILEINFO", path);
1717 st = ¶m->lmd->lmd_st;
1719 /* libc chown() will do extra check, and if the real owner is
1720 * the same as the ones to set, it won't fall into kernel, so
1721 * invoke syscall directly. */
1722 rc = syscall(SYS_chown, path, -1, -1);
1724 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
1726 rc = chmod(path, st->st_mode);
1728 llapi_err(LLAPI_MSG_ERROR,"error: chmod %s (%hu)", path, st->st_mode);
1733 int llapi_quotachown(char *path, int flag)
1735 struct find_param param;
1737 int ret = 0, len = strlen(path);
1739 if (len > PATH_MAX) {
1740 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1741 __FUNCTION__, path);
1745 buf = (char *)malloc(PATH_MAX + 1);
1749 memset(¶m, 0, sizeof(param));
1750 param.recursive = 1;
1754 ret = common_param_init(¶m);
1758 strncpy(buf, path, PATH_MAX + 1);
1759 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1760 NULL, ¶m, NULL);
1762 find_param_fini(¶m);
1770 #include <sys/wait.h>
1774 static int rmtacl_notify(int ops)
1778 int found = 0, fd, rc;
1780 fp = setmntent(MOUNTED, "r");
1782 perror("setmntent");
1787 mnt = getmntent(fp);
1791 if (!llapi_is_lustre_mnt(mnt))
1794 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
1800 rc = ioctl(fd, LL_IOC_RMTACL, ops);
1812 static char *next_token(char *p, int div)
1818 while (*p && *p != ':' && !isspace(*p))
1821 while (*p == ':' || isspace(*p))
1824 return *p ? p : NULL;
1827 static int rmtacl_name2id(char *name, int is_user)
1832 if ((pw = getpwnam(name)) == NULL)
1835 return (int)(pw->pw_uid);
1839 if ((gr = getgrnam(name)) == NULL)
1842 return (int)(gr->gr_gid);
1846 static int isodigit(int c)
1848 return (c >= '0' && c <= '7') ? 1 : 0;
1852 * Whether the name is just digits string (uid/gid) already or not.
1857 static int str_is_id(char *str)
1864 if (*str == 'x' || *str == 'X') { /* for Hex. */
1865 if (!isxdigit(*(++str)))
1868 while (isxdigit(*(++str)));
1869 } else if (isodigit(*str)) { /* for Oct. */
1870 while (isodigit(*(++str)));
1872 } else if (isdigit(*str)) { /* for Dec. */
1873 while (isdigit(*(++str)));
1876 return (*str == 0) ? 1 : 0;
1886 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
1888 static rmtacl_name_t rmtacl_namelist[] = {
1889 { RMTACL_OPTNAME("user:"), 1, 0 },
1890 { RMTACL_OPTNAME("group:"), 0, 0 },
1891 { RMTACL_OPTNAME("default:user:"), 1, 0 },
1892 { RMTACL_OPTNAME("default:group:"), 0, 0 },
1893 /* for --tabular option */
1894 { RMTACL_OPTNAME("user"), 1, 1 },
1895 { RMTACL_OPTNAME("group"), 0, 1 },
1899 static int rgetfacl_output(char *str)
1901 char *start = NULL, *end = NULL;
1902 int is_user = 0, n, id;
1909 for (rn = rmtacl_namelist; rn->name; rn++) {
1910 if(strncmp(str, rn->name, rn->length) == 0) {
1911 if (!rn->next_token)
1912 start = str + rn->length;
1914 start = next_token(str + rn->length, 0);
1915 is_user = rn->is_user;
1920 end = next_token(start, 1);
1921 if (end == NULL || start == end) {
1922 n = printf("%s", str);
1928 id = rmtacl_name2id(start, is_user);
1929 if (id == INVALID_ID) {
1930 if (str_is_id(start)) {
1932 n = printf("%s", str);
1935 } else if ((id == NOBODY_UID && is_user) ||
1936 (id == NOBODY_GID && !is_user)) {
1938 n = printf("%s", str);
1942 n = printf("%s%d%s", str, id, end);
1947 static int child_status(int status)
1949 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
1952 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
1965 if ((pid = fork()) < 0) {
1971 /* child process redirects its output. */
1974 if (dup2(fd[1], 1) < 0) {
1985 status = rmtacl_notify(ops);
1989 exit(execvp(argv[0], argv));
1992 /* the following is parent process */
1993 if ((fp = fdopen(fd[0], "r")) == NULL) {
2000 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2001 if (output_func(buf) < 0)
2002 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2008 if (waitpid(pid, &status, 0) < 0) {
2013 return child_status(status);
2016 int llapi_lsetfacl(int argc, char *argv[])
2018 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2021 int llapi_lgetfacl(int argc, char *argv[])
2023 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2026 int llapi_rsetfacl(int argc, char *argv[])
2028 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2031 int llapi_rgetfacl(int argc, char *argv[])
2033 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2036 int llapi_cp(int argc, char *argv[])
2040 rc = rmtacl_notify(RMT_RSETFACL);
2044 exit(execvp(argv[0], argv));
2047 int llapi_ls(int argc, char *argv[])
2051 rc = rmtacl_notify(RMT_LGETFACL);
2055 exit(execvp(argv[0], argv));