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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/utils/liblustreapi.c
41 * Author: Peter J. Braam <braam@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
62 #include <sys/types.h>
63 #include <sys/syscall.h>
64 #include <sys/xattr.h>
67 #ifdef HAVE_LINUX_UNISTD_H
68 #include <linux/unistd.h>
74 #include <liblustre.h>
75 #include <lnet/lnetctl.h>
77 #include <lustre_lib.h>
79 #include <lustre/liblustreapi.h>
81 static unsigned llapi_dir_filetype_table[] = {
90 #if defined(DT_DOOR) && defined(S_IFDOOR)
95 #if defined(DT_DOOR) && defined(S_IFDOOR)
96 static const int DT_MAX = DT_DOOR;
98 static const int DT_MAX = DT_SOCK;
101 static unsigned llapi_filetype_dir_table[] = {
110 #if defined(DT_DOOR) && defined(S_IFDOOR)
115 #if defined(DT_DOOR) && defined(S_IFDOOR)
116 static const int S_IFMAX = DT_DOOR;
118 static const int S_IFMAX = DT_SOCK;
121 /* liblustreapi message level */
122 static int llapi_msg_level = LLAPI_MSG_MAX;
124 void llapi_msg_set_level(int level)
126 /* ensure level is in the good range */
127 if (level < LLAPI_MSG_OFF)
128 llapi_msg_level = LLAPI_MSG_OFF;
129 else if (level > LLAPI_MSG_MAX)
130 llapi_msg_level = LLAPI_MSG_MAX;
132 llapi_msg_level = level;
135 /* llapi_error will preserve errno */
136 void llapi_error(int level, int _rc, char *fmt, ...)
139 int tmp_errno = errno;
140 /* to protect using errno as _rc argument */
143 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
147 vfprintf(stderr, fmt, args);
150 if (level & LLAPI_MSG_NO_ERRNO)
151 fprintf(stderr, "\n");
153 fprintf(stderr, ": %s (%d)\n", strerror(rc), rc);
157 /* llapi_printf will preserve errno */
158 void llapi_printf(int level, char *fmt, ...)
161 int tmp_errno = errno;
163 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
167 vfprintf(stdout, fmt, args);
173 * size_units is to be initialized (or zeroed) by caller.
175 int parse_size(char *optarg, unsigned long long *size,
176 unsigned long long *size_units, int bytes_spec)
180 if (*size_units == 0)
183 *size = strtoull(optarg, &end, 0);
186 if ((*end == 'b') && *(end+1) == '\0' &&
187 (*size & (~0ULL << (64 - 9))) == 0 &&
189 *size_units = 1 << 9;
190 } else if ((*end == 'b') && *(end+1) == '\0' &&
193 } else if ((*end == 'k' || *end == 'K') &&
194 *(end+1) == '\0' && (*size &
195 (~0ULL << (64 - 10))) == 0) {
196 *size_units = 1 << 10;
197 } else if ((*end == 'm' || *end == 'M') &&
198 *(end+1) == '\0' && (*size &
199 (~0ULL << (64 - 20))) == 0) {
200 *size_units = 1 << 20;
201 } else if ((*end == 'g' || *end == 'G') &&
202 *(end+1) == '\0' && (*size &
203 (~0ULL << (64 - 30))) == 0) {
204 *size_units = 1 << 30;
205 } else if ((*end == 't' || *end == 'T') &&
206 *(end+1) == '\0' && (*size &
207 (~0ULL << (64 - 40))) == 0) {
208 *size_units = 1ULL << 40;
209 } else if ((*end == 'p' || *end == 'P') &&
210 *(end+1) == '\0' && (*size &
211 (~0ULL << (64 - 50))) == 0) {
212 *size_units = 1ULL << 50;
213 } else if ((*end == 'e' || *end == 'E') &&
214 *(end+1) == '\0' && (*size &
215 (~0ULL << (64 - 60))) == 0) {
216 *size_units = 1ULL << 60;
221 *size *= *size_units;
225 /* XXX: llapi_xxx() functions return negative values upon failure */
227 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
228 int stripe_count, int stripe_pattern)
232 /* 64 KB is the largest common page size I'm aware of (on ia64), but
233 * check the local page size just in case. */
234 page_size = LOV_MIN_STRIPE_SIZE;
235 if (getpagesize() > page_size) {
236 page_size = getpagesize();
237 llapi_err_noerrno(LLAPI_MSG_WARN,
238 "warning: your page size (%u) is "
239 "larger than expected (%u)", page_size,
240 LOV_MIN_STRIPE_SIZE);
242 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
244 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
245 "must be an even multiple of %d bytes",
246 stripe_size, page_size);
249 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
251 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
255 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
257 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
261 if (stripe_size >= (1ULL << 32)){
263 llapi_error(LLAPI_MSG_ERROR, rc,
264 "warning: stripe size larger than 4G "
265 "is not currently supported and would wrap");
271 static int find_target_obdpath(char *fsname, char *path)
274 char pattern[PATH_MAX + 1];
277 snprintf(pattern, PATH_MAX,
278 "/proc/fs/lustre/lov/%s-*/target_obd",
280 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
281 if (rc == GLOB_NOMATCH)
286 strcpy(path, glob_info.gl_pathv[0]);
287 globfree(&glob_info);
291 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
294 char pattern[PATH_MAX + 1];
297 snprintf(pattern, PATH_MAX,
298 "/proc/fs/lustre/lov/%s-*/pools/%s",
300 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
301 /* If no pools, make sure the lov is available */
302 if ((rc == GLOB_NOMATCH) &&
303 (find_target_obdpath(fsname, poolpath) == -ENODEV))
308 strcpy(poolpath, glob_info.gl_pathv[0]);
309 globfree(&glob_info);
314 * if pool is NULL, search ostname in target_obd
315 * if pool is not NULL:
316 * if pool not found returns errno < 0
317 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
318 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
320 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
323 char buffer[PATH_MAX + 1];
327 len = strlen(ostname);
329 if (poolname == NULL)
330 rc = find_target_obdpath(fsname, buffer);
332 rc = find_poolpath(fsname, poolname, buffer);
336 fd = fopen(buffer, "r");
340 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
341 if (poolname == NULL) {
343 /* Search for an ostname in the list of OSTs
344 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
345 ptr = strchr(buffer, ' ');
347 (strncmp(ptr + 1, ostname, len) == 0)) {
352 /* Search for an ostname in a pool,
353 (or an existing non-empty pool if no ostname) */
354 if ((ostname == NULL) ||
355 (strncmp(buffer, ostname, len) == 0)) {
365 int llapi_file_open_pool(const char *name, int flags, int mode,
366 unsigned long long stripe_size, int stripe_offset,
367 int stripe_count, int stripe_pattern, char *pool_name)
369 struct lov_user_md_v3 lum = { 0 };
373 /* Make sure we have a good pool */
374 if (pool_name != NULL) {
375 char fsname[MAX_OBD_NAME + 1], *ptr;
377 rc = llapi_search_fsname(name, fsname);
379 llapi_error(LLAPI_MSG_ERROR, rc,
380 "'%s' is not on a Lustre filesystem",
385 /* in case user gives the full pool name <fsname>.<poolname>,
386 * strip the fsname */
387 ptr = strchr(pool_name, '.');
390 if (strcmp(pool_name, fsname) != 0) {
392 llapi_err_noerrno(LLAPI_MSG_ERROR,
393 "Pool '%s' is not on filesystem '%s'",
400 /* Make sure the pool exists and is non-empty */
401 rc = llapi_search_ost(fsname, pool_name, NULL);
403 llapi_err_noerrno(LLAPI_MSG_ERROR,
404 "pool '%s.%s' %s", fsname, pool_name,
405 rc == 0 ? "has no OSTs" : "does not exist");
410 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
411 if (fd < 0 && errno == EISDIR) {
412 fd = open(name, O_DIRECTORY | O_RDONLY);
418 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
422 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
427 /* Initialize IOCTL striping pattern structure */
428 lum.lmm_magic = LOV_USER_MAGIC_V3;
429 lum.lmm_pattern = stripe_pattern;
430 lum.lmm_stripe_size = stripe_size;
431 lum.lmm_stripe_count = stripe_count;
432 lum.lmm_stripe_offset = stripe_offset;
433 if (pool_name != NULL) {
434 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
436 /* If no pool is specified at all, use V1 request */
437 lum.lmm_magic = LOV_USER_MAGIC_V1;
440 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
441 char *errmsg = "stripe already set";
443 if (errno != EEXIST && errno != EALREADY)
444 errmsg = strerror(errno);
446 llapi_err_noerrno(LLAPI_MSG_ERROR,
447 "error on ioctl "LPX64" for '%s' (%d): %s",
448 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
459 int llapi_file_open(const char *name, int flags, int mode,
460 unsigned long long stripe_size, int stripe_offset,
461 int stripe_count, int stripe_pattern)
463 return llapi_file_open_pool(name, flags, mode, stripe_size,
464 stripe_offset, stripe_count,
465 stripe_pattern, NULL);
468 int llapi_file_create(const char *name, unsigned long long stripe_size,
469 int stripe_offset, int stripe_count, int stripe_pattern)
473 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
474 stripe_offset, stripe_count, stripe_pattern,
483 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
484 int stripe_offset, int stripe_count,
485 int stripe_pattern, char *pool_name)
489 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
490 stripe_offset, stripe_count, stripe_pattern,
500 * Find the fsname, the full path, and/or an open fd.
501 * Either the fsname or path must not be NULL
503 #define WANT_PATH 0x1
504 #define WANT_FSNAME 0x2
506 #define WANT_INDEX 0x8
507 #define WANT_ERROR 0x10
508 static int get_root_path(int want, char *fsname, int *outfd, char *path,
512 char buf[PATH_MAX], mntdir[PATH_MAX];
515 int idx = 0, len = 0, mntlen, fd;
518 /* get the mount point */
519 fp = setmntent(MOUNTED, "r");
522 llapi_error(LLAPI_MSG_ERROR, rc,
523 "setmntent(%s) failed", MOUNTED);
527 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
530 if (!llapi_is_lustre_mnt(&mnt))
533 if ((want & WANT_INDEX) && (idx++ != index))
536 mntlen = strlen(mnt.mnt_dir);
537 ptr = strrchr(mnt.mnt_fsname, '/');
544 /* Check the fsname for a match, if given */
545 if (!(want & WANT_FSNAME) && fsname != NULL &&
546 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
549 /* If the path isn't set return the first one we find */
550 if (path == NULL || strlen(path) == 0) {
551 strcpy(mntdir, mnt.mnt_dir);
552 if ((want & WANT_FSNAME) && fsname != NULL)
556 /* Otherwise find the longest matching path */
557 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
558 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
559 strcpy(mntdir, mnt.mnt_dir);
561 if ((want & WANT_FSNAME) && fsname != NULL)
570 if ((want & WANT_PATH) && path != NULL)
571 strcpy(path, mntdir);
572 if (want & WANT_FD) {
573 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
576 llapi_error(LLAPI_MSG_ERROR, rc,
577 "error opening '%s'", mntdir);
583 } else if (want & WANT_ERROR)
584 llapi_err_noerrno(LLAPI_MSG_ERROR,
585 "can't find fs root for '%s': %d",
586 (want & WANT_PATH) ? fsname : path, rc);
591 * search lustre mounts
593 * Calling this function will return to the user the mount point, mntdir, and
594 * the file system name, fsname, if the user passed a buffer to this routine.
596 * The user inputs are pathname and index. If the pathname is supplied then
597 * the value of the index will be ignored. The pathname will return data if
598 * the pathname is located on a lustre mount. Index is used to pick which
599 * mount point you want in the case of multiple mounted lustre file systems.
600 * See function lfs_osts in lfs.c for a example of the index use.
602 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
605 int want = WANT_PATH, idx = -1;
607 if (!pathname || pathname[0] == '\0') {
611 strcpy(mntdir, pathname);
615 return get_root_path(want, fsname, NULL, mntdir, idx);
618 /* Given a path, find the corresponding Lustre fsname */
619 int llapi_search_fsname(const char *pathname, char *fsname)
624 path = realpath(pathname, NULL);
626 char buf[PATH_MAX + 1], *ptr;
629 if (pathname[0] != '/') {
630 /* Need an absolute path, but realpath() only works for
631 * pathnames that actually exist. We go through the
632 * extra hurdle of dirname(getcwd() + pathname) in
633 * case the relative pathname contains ".." in it. */
634 if (getcwd(buf, sizeof(buf) - 1) == NULL)
638 strncat(buf, pathname, sizeof(buf) - strlen(buf));
639 path = realpath(buf, NULL);
641 ptr = strrchr(buf, '/');
645 path = realpath(buf, NULL);
648 llapi_error(LLAPI_MSG_ERROR, rc,
649 "pathname '%s' cannot expand",
655 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
660 /* return the first file matching this pattern */
661 static int first_match(char *pattern, char *buffer)
665 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
668 if (glob_info.gl_pathc < 1) {
669 globfree(&glob_info);
673 strcpy(buffer, glob_info.gl_pathv[0]);
675 globfree(&glob_info);
680 * find the pool directory path under /proc
681 * (can be also used to test if a fsname is known)
683 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
686 char pattern[PATH_MAX + 1];
687 char buffer[PATH_MAX];
689 if (fsname == NULL) {
690 rc = llapi_search_fsname(pathname, buffer);
694 strcpy(pathname, fsname);
697 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
698 rc = first_match(pattern, buffer);
702 /* in fsname test mode, pool_pathname is NULL */
703 if (pool_pathname != NULL)
704 strcpy(pool_pathname, buffer);
710 * Get the list of pool members.
711 * \param poolname string of format \<fsname\>.\<poolname\>
712 * \param members caller-allocated array of char*
713 * \param list_size size of the members array
714 * \param buffer caller-allocated buffer for storing OST names
715 * \param buffer_size size of the buffer
717 * \return number of members retrieved for this pool
718 * \retval -error failure
720 int llapi_get_poolmembers(const char *poolname, char **members,
721 int list_size, char *buffer, int buffer_size)
723 char fsname[PATH_MAX + 1];
725 char pathname[PATH_MAX + 1];
726 char path[PATH_MAX + 1];
733 /* name is FSNAME.POOLNAME */
734 if (strlen(poolname) > PATH_MAX)
736 strcpy(fsname, poolname);
737 pool = strchr(fsname, '.');
744 rc = poolpath(fsname, NULL, pathname);
746 llapi_error(LLAPI_MSG_ERROR, rc,
747 "Lustre filesystem '%s' not found",
752 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
753 sprintf(path, "%s/%s", pathname, pool);
754 fd = fopen(path, "r");
757 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
762 while (fgets(buf, sizeof(buf), fd) != NULL) {
763 if (nb_entries >= list_size) {
768 tmp = strchr(buf, '\n');
771 if (used + strlen(buf) + 1 > buffer_size) {
776 strcpy(buffer + used, buf);
777 members[nb_entries] = buffer + used;
778 used += strlen(buf) + 1;
788 * Get the list of pools in a filesystem.
789 * \param name filesystem name or path
790 * \param poollist caller-allocated array of char*
791 * \param list_size size of the poollist array
792 * \param buffer caller-allocated buffer for storing pool names
793 * \param buffer_size size of the buffer
795 * \return number of pools retrieved for this filesystem
796 * \retval -error failure
798 int llapi_get_poollist(const char *name, char **poollist, int list_size,
799 char *buffer, int buffer_size)
801 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
805 struct dirent *cookie = NULL;
807 unsigned int nb_entries = 0;
808 unsigned int used = 0;
811 /* initilize output array */
812 for (i = 0; i < list_size; i++)
815 /* is name a pathname ? */
816 ptr = strchr(name, '/');
818 /* only absolute pathname is supported */
822 if (!realpath(name, rname)) {
824 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
829 rc = poolpath(NULL, rname, pathname);
831 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
832 " a Lustre filesystem", name);
835 strcpy(fsname, rname);
838 strcpy(fsname, name);
839 rc = poolpath(fsname, NULL, pathname);
842 llapi_error(LLAPI_MSG_ERROR, rc,
843 "Lustre filesystem '%s' not found", name);
847 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
848 dir = opendir(pathname);
851 llapi_error(LLAPI_MSG_ERROR, rc,
852 "Could not open pool list for '%s'",
858 rc = readdir_r(dir, &pool, &cookie);
862 llapi_error(LLAPI_MSG_ERROR, rc,
863 "Error reading pool list for '%s'", name);
865 } else if ((rc == 0) && (cookie == NULL)) {
866 /* end of directory */
870 /* ignore . and .. */
871 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
874 /* check output bounds */
875 if (nb_entries >= list_size)
878 /* +2 for '.' and final '\0' */
879 if (used + strlen(pool.d_name) + strlen(fsname) + 2
883 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
884 poollist[nb_entries] = buffer + used;
885 used += strlen(pool.d_name) + strlen(fsname) + 2;
893 /* wrapper for lfs.c and obd.c */
894 int llapi_poollist(const char *name)
896 /* list of pool names (assume that pool count is smaller
898 char *list[FIND_MAX_OSTS];
900 /* fsname-OST0000_UUID < 32 char, 1 per OST */
901 int bufsize = FIND_MAX_OSTS * 32;
904 buffer = malloc(bufsize);
908 if ((name[0] == '/') || (strchr(name, '.') == NULL))
909 /* name is a path or fsname */
910 nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer,
913 /* name is a pool name (<fsname>.<poolname>) */
914 nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer,
917 for (i = 0; i < nb; i++)
918 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
921 return (nb < 0 ? nb : 0);
925 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
926 void *data, cfs_dirent_t *de);
928 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
929 #define OBD_NOT_FOUND (-1)
931 static int common_param_init(struct find_param *param)
933 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
934 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
935 if (param->lmd == NULL) {
936 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
937 "error: allocation of %d bytes for ioctl",
938 sizeof(lstat_t) + param->lumlen);
942 param->got_uuids = 0;
943 param->obdindexes = NULL;
944 param->obdindex = OBD_NOT_FOUND;
948 static void find_param_fini(struct find_param *param)
950 if (param->obdindexes)
951 free(param->obdindexes);
957 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
960 struct find_param *param = (struct find_param *)data;
965 /* set errno upon failure */
966 static DIR *opendir_parent(char *path)
972 fname = strrchr(path, '/');
978 parent = opendir(path);
983 int llapi_mds_getfileinfo(char *path, DIR *parent,
984 struct lov_user_mds_data *lmd)
986 lstat_t *st = &lmd->lmd_st;
987 char *fname = strrchr(path, '/');
993 fname = (fname == NULL ? path : fname + 1);
994 /* retrieve needed file info */
995 strncpy((char *)lmd, fname,
996 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
997 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1000 if (errno == ENOTTY) {
1001 /* ioctl is not supported, it is not a lustre fs.
1002 * Do the regular lstat(2) instead. */
1003 ret = lstat_f(path, st);
1006 llapi_error(LLAPI_MSG_ERROR, ret,
1007 "error: %s: lstat failed for %s",
1011 } else if (errno == ENOENT) {
1013 llapi_error(LLAPI_MSG_WARN, ret,
1014 "warning: %s: %s does not exist",
1019 llapi_error(LLAPI_MSG_ERROR, ret,
1020 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1029 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1030 semantic_func_t sem_init,
1031 semantic_func_t sem_fini, void *data,
1042 if (!d && errno != ENOTDIR) {
1044 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1047 } else if (!d && !parent) {
1048 /* ENOTDIR. Open the parent dir. */
1049 p = opendir_parent(path);
1051 GOTO(out, ret = -errno);
1054 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1060 while ((dent = readdir64(d)) != NULL) {
1061 ((struct find_param *)data)->have_fileinfo = 0;
1063 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1066 /* Don't traverse .lustre directory */
1067 if (!(strcmp(dent->d_name, dot_lustre_name)))
1071 if ((len + dent->d_reclen + 2) > size) {
1072 llapi_err_noerrno(LLAPI_MSG_ERROR,
1073 "error: %s: string buffer is too small",
1078 strcat(path, dent->d_name);
1080 if (dent->d_type == DT_UNKNOWN) {
1081 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1083 ret = llapi_mds_getfileinfo(path, d,
1084 ((struct find_param *)data)->lmd);
1086 ((struct find_param *)data)->have_fileinfo = 1;
1088 llapi_filetype_dir_table[st->st_mode &
1095 switch (dent->d_type) {
1097 llapi_err_noerrno(LLAPI_MSG_ERROR,
1098 "error: %s: '%s' is UNKNOWN type %d",
1099 __func__, dent->d_name, dent->d_type);
1102 ret = llapi_semantic_traverse(path, size, d, sem_init,
1103 sem_fini, data, dent);
1110 ret = sem_init(path, d, NULL, data, dent);
1114 if (sem_fini && ret == 0)
1115 sem_fini(path, d, NULL, data, dent);
1123 sem_fini(path, parent, d, data, de);
1132 static int param_callback(char *path, semantic_func_t sem_init,
1133 semantic_func_t sem_fini, struct find_param *param)
1135 int ret, len = strlen(path);
1138 if (len > PATH_MAX) {
1140 llapi_error(LLAPI_MSG_ERROR, ret,
1141 "Path name '%s' is too long", path);
1145 buf = (char *)malloc(PATH_MAX + 1);
1149 ret = common_param_init(param);
1154 strncpy(buf, path, PATH_MAX + 1);
1155 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1156 sem_fini, param, NULL);
1158 find_param_fini(param);
1160 return ret < 0 ? ret : 0;
1163 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1165 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1168 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1173 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1177 fd = open(path, O_RDONLY);
1180 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1184 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1191 * If uuidp is NULL, return the number of available obd uuids.
1192 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1193 * there are more OSTs then allocated to uuidp, then an error is returned with
1194 * the ost_count set to number of available obd uuids.
1196 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1198 struct obd_uuid lov_name;
1201 int rc = 0, index = 0;
1203 /* Get the lov name */
1204 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
1208 /* Now get the ost uuids from /proc */
1209 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1211 fp = fopen(buf, "r");
1214 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1218 while (fgets(buf, sizeof(buf), fp) != NULL) {
1219 if (uuidp && (index < *ost_count)) {
1220 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1228 if (uuidp && (index >= *ost_count))
1235 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1240 root = opendir(mnt);
1243 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1248 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1256 /* Check if user specified value matches a real uuid. Ignore _UUID,
1257 * -osc-4ba41334, other trailing gunk in comparison.
1258 * @param real_uuid ends in "_UUID"
1259 * @param search_uuid may or may not end in "_UUID"
1261 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1263 int cmplen = strlen(real_uuid);
1264 int searchlen = strlen(search_uuid);
1266 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1268 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1271 /* The UUIDs may legitimately be different lengths, if
1272 * the system was upgraded from an older version. */
1273 if (cmplen != searchlen)
1276 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1279 /* Here, param->obduuid points to a single obduuid, the index of which is
1280 * returned in param->obdindex */
1281 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1283 struct obd_uuid lov_uuid;
1284 char uuid[sizeof(struct obd_uuid)];
1289 if (param->got_uuids)
1292 /* Get the lov name */
1293 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
1295 if (rc != -ENOTTY) {
1296 llapi_error(LLAPI_MSG_ERROR, rc,
1297 "error: can't get lov name: %s", dname);
1304 param->got_uuids = 1;
1306 /* Now get the ost uuids from /proc */
1307 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1309 fp = fopen(buf, "r");
1312 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1316 if (!param->obduuid && !param->quiet && !param->obds_printed)
1317 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
1319 while (fgets(buf, sizeof(buf), fp) != NULL) {
1320 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1323 if (param->obduuid) {
1324 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1325 param->obdindex = index;
1328 } else if (!param->quiet && !param->obds_printed) {
1329 /* Print everything */
1330 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1333 param->obds_printed = 1;
1337 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1338 llapi_err_noerrno(LLAPI_MSG_ERROR,
1339 "error: %s: unknown obduuid: %s",
1340 __func__, param->obduuid->uuid);
1347 /* In this case, param->obduuid will be an array of obduuids and
1348 * obd index for all these obduuids will be returned in
1349 * param->obdindexes */
1350 static int setup_obd_indexes(DIR *dir, struct find_param *param)
1352 struct obd_uuid *uuids = NULL;
1353 int obdcount = INIT_ALLOC_NUM_OSTS;
1354 int ret, obd_valid = 0, obdnum, i;
1356 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1357 sizeof(struct obd_uuid));
1362 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
1365 struct obd_uuid *uuids_temp;
1367 if (ret == -EOVERFLOW) {
1368 uuids_temp = realloc(uuids, obdcount *
1369 sizeof(struct obd_uuid));
1370 if (uuids_temp != NULL)
1371 goto retry_get_uuids;
1376 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1380 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
1381 if (param->obdindexes == NULL)
1384 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
1385 for (i = 0; i < obdcount; i++) {
1386 if (llapi_uuid_match(uuids[i].uuid,
1387 param->obduuid[obdnum].uuid)) {
1388 param->obdindexes[obdnum] = i;
1393 if (i >= obdcount) {
1394 param->obdindexes[obdnum] = OBD_NOT_FOUND;
1395 llapi_err_noerrno(LLAPI_MSG_ERROR,
1396 "error: %s: unknown obduuid: %s",
1398 param->obduuid[obdnum].uuid);
1404 param->obdindex = OBD_NOT_FOUND;
1406 param->obdindex = obd_valid;
1408 param->got_uuids = 1;
1413 int llapi_ostlist(char *path, struct find_param *param)
1418 dir = opendir(path);
1422 ret = setup_obd_uuid(dir, path, param);
1429 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
1430 * tries to determine the path to the filesystem's clilov directory under /proc
1432 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
1433 * The NUL terminator is compensated by the additional "%s" bytes. */
1434 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
1435 static int clilovpath(const char *fsname, const char *const pathname,
1439 char pattern[LOV_LEN];
1440 char buffer[PATH_MAX + 1];
1442 if (fsname == NULL) {
1443 rc = llapi_search_fsname(pathname, buffer);
1449 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
1452 rc = first_match(pattern, buffer);
1456 strncpy(clilovpath, buffer, sizeof(buffer));
1462 * Given the path to a stripe attribute proc file, tries to open and
1463 * read the attribute and return the value using the attr parameter
1465 static int sattr_read_attr(const char *const fpath,
1470 char line[PATH_MAX + 1];
1473 f = fopen(fpath, "r");
1476 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
1480 if (fgets(line, sizeof(line), f) != NULL) {
1483 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
1492 * Tries to determine the default stripe attributes for a given filesystem. The
1493 * filesystem to check should be specified by fsname, or will be determined
1496 static int sattr_get_defaults(const char *const fsname,
1497 const char *const pathname,
1498 unsigned int *scount,
1499 unsigned int *ssize,
1500 unsigned int *soffset)
1503 char dpath[PATH_MAX + 1];
1504 char fpath[PATH_MAX + 1];
1506 rc = clilovpath(fsname, pathname, dpath);
1511 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
1512 rc = sattr_read_attr(fpath, scount);
1518 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
1519 rc = sattr_read_attr(fpath, ssize);
1525 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
1526 rc = sattr_read_attr(fpath, soffset);
1535 * Tries to gather the default stripe attributes for a given filesystem. If
1536 * the attributes can be determined, they are cached for easy retreival the
1537 * next time they are needed. Only a single filesystem's attributes are
1540 static int sattr_cache_get_defaults(const char *const fsname,
1541 const char *const pathname,
1542 unsigned int *scount,
1543 unsigned int *ssize,
1544 unsigned int *soffset)
1547 char fsname[PATH_MAX + 1];
1548 unsigned int stripecount;
1549 unsigned int stripesize;
1550 unsigned int stripeoffset;
1556 char fsname_buf[PATH_MAX + 1];
1557 unsigned int tmp[3];
1559 if (fsname == NULL) {
1560 rc = llapi_search_fsname(pathname, fsname_buf);
1564 strncpy(fsname_buf, fsname, PATH_MAX);
1567 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
1569 * Ensure all 3 sattrs (count, size, and offset) are
1570 * successfully retrieved and stored in tmp before writing to
1573 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
1578 cache.stripecount = tmp[0];
1579 cache.stripesize = tmp[1];
1580 cache.stripeoffset = tmp[2];
1581 strncpy(cache.fsname, fsname_buf, PATH_MAX);
1585 *scount = cache.stripecount;
1587 *ssize = cache.stripesize;
1589 *soffset = cache.stripeoffset;
1594 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1595 struct lov_user_ost_data_v1 *objects,
1596 int is_dir, int verbose, int depth,
1597 int raw, char *pool_name)
1599 char *prefix = is_dir ? "" : "lmm_";
1600 char nl = is_dir ? ' ' : '\n';
1603 if (is_dir && lum->lmm_object_seq == FID_SEQ_LOV_DEFAULT) {
1604 lum->lmm_object_seq = FID_SEQ_OST_MDT0;
1605 if (verbose & VERBOSE_DETAIL)
1606 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1609 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1610 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1612 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1613 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1615 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
1616 lum->lmm_object_seq);
1617 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1618 lum->lmm_object_id);
1621 if (verbose & VERBOSE_COUNT) {
1622 if (verbose & ~VERBOSE_COUNT)
1623 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1626 if (!raw && lum->lmm_stripe_count == 0) {
1627 unsigned int scount;
1628 rc = sattr_cache_get_defaults(NULL, path,
1632 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1635 llapi_error(LLAPI_MSG_ERROR, rc,
1636 "Cannot determine default"
1639 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1640 lum->lmm_stripe_count ==
1641 (typeof(lum->lmm_stripe_count))(-1)
1642 ? -1 : lum->lmm_stripe_count, nl);
1645 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1646 (__s16)lum->lmm_stripe_count, nl);
1650 if (verbose & VERBOSE_SIZE) {
1651 if (verbose & ~VERBOSE_SIZE)
1652 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1654 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
1656 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
1659 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", ssize,
1662 llapi_error(LLAPI_MSG_ERROR, rc,
1663 "Cannot determine default"
1666 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1667 lum->lmm_stripe_size, nl);
1671 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1672 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1673 lum->lmm_pattern, nl);
1676 if (verbose & VERBOSE_OFFSET) {
1677 if (verbose & ~VERBOSE_OFFSET)
1678 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1681 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1682 lum->lmm_stripe_offset ==
1683 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
1684 lum->lmm_stripe_offset, nl);
1686 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1687 objects[0].l_ost_idx, nl);
1690 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1691 if (verbose & ~VERBOSE_POOL)
1692 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
1694 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
1697 if (is_dir && (verbose != VERBOSE_OBJID))
1698 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1701 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1702 struct lov_user_ost_data_v1 *objects,
1703 char *path, int is_dir,
1704 int obdindex, int depth, int header, int raw)
1706 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1709 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1710 if (obdindex == objects[i].l_ost_idx) {
1718 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
1719 depth, raw, pool_name);
1721 if (!is_dir && (header & VERBOSE_OBJID)) {
1723 llapi_printf(LLAPI_MSG_NORMAL,
1724 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1726 for (i = 0; i < lum->lmm_stripe_count; i++) {
1727 int idx = objects[i].l_ost_idx;
1728 long long oid = objects[i].l_object_id;
1729 long long gr = objects[i].l_object_seq;
1730 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1731 llapi_printf(LLAPI_MSG_NORMAL,
1732 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1734 obdindex == idx ? " *" : "");
1736 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1740 void llapi_lov_dump_user_lmm(struct find_param *param,
1741 char *path, int is_dir)
1743 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1744 case LOV_USER_MAGIC_V1:
1745 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1746 param->lmd->lmd_lmm.lmm_objects,
1748 param->obdindex, param->maxdepth,
1749 param->verbose, param->raw);
1751 case LOV_USER_MAGIC_V3: {
1752 char pool_name[LOV_MAXPOOLNAME + 1];
1753 struct lov_user_ost_data_v1 *objects;
1754 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1756 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1757 pool_name[LOV_MAXPOOLNAME] = '\0';
1758 objects = lmmv3->lmm_objects;
1759 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1760 objects, path, is_dir,
1761 param->obdindex, param->maxdepth,
1762 param->verbose, param->raw);
1766 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1767 "(expecting one of %#x %#x %#x)\n",
1768 *(__u32 *)¶m->lmd->lmd_lmm,
1769 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
1774 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1780 fname = strrchr(path, '/');
1782 /* It should be a file (or other non-directory) */
1783 if (fname == NULL) {
1784 dname = (char *)malloc(2);
1788 fname = (char *)path;
1790 dname = (char *)malloc(fname - path + 1);
1793 strncpy(dname, path, fname - path);
1794 dname[fname - path] = '\0';
1798 fd = open(dname, O_RDONLY);
1805 strcpy((char *)lum, fname);
1806 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1809 if (close(fd) == -1 && rc == 0)
1816 int llapi_file_lookup(int dirfd, const char *name)
1818 struct obd_ioctl_data data = { 0 };
1823 if (dirfd < 0 || name == NULL)
1826 data.ioc_version = OBD_IOCTL_VERSION;
1827 data.ioc_len = sizeof(data);
1828 data.ioc_inlbuf1 = (char *)name;
1829 data.ioc_inllen1 = strlen(name) + 1;
1831 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1833 llapi_error(LLAPI_MSG_ERROR, rc,
1834 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1839 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1845 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1846 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1848 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1849 * The table below gives the answers for the specified parameters (value and
1850 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1851 * --------------------------------------
1852 * 1 | file > limit; sign > 0 | -1 / -1 |
1853 * 2 | file = limit; sign > 0 | -1 / -1 |
1854 * 3 | file < limit; sign > 0 | ? / 1 |
1855 * 4 | file > limit; sign = 0 | -1 / -1 |
1856 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1857 * 6 | file < limit; sign = 0 | ? / -1 |
1858 * 7 | file > limit; sign < 0 | 1 / 1 |
1859 * 8 | file = limit; sign < 0 | ? / -1 |
1860 * 9 | file < limit; sign < 0 | ? / -1 |
1861 * --------------------------------------
1862 * Note: 5th actually means that the value is within the interval
1863 * (limit - margin, limit]. */
1864 static int find_value_cmp(unsigned long long file, unsigned long long limit,
1865 int sign, int negopt, unsigned long long margin,
1871 /* Drop the fraction of margin (of days). */
1872 if (file + margin <= limit)
1874 } else if (sign == 0) {
1875 if (file <= limit && file + margin > limit)
1877 else if (file + margin <= limit)
1879 } else if (sign < 0) {
1886 return negopt ? ~ret + 1 : ret;
1889 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1890 * Return -1 or 1 if file timestamp does not or does match the given criteria
1891 * correspondingly. Return 0 if the MDS time is being checked and there are
1892 * attributes on OSTs and it is not yet clear if the timespamp matches.
1894 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1895 * updated timestamps. */
1896 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1901 /* Check if file is accepted. */
1903 ret = find_value_cmp(st->st_atime, param->atime,
1904 param->asign, param->exclude_atime,
1912 ret = find_value_cmp(st->st_mtime, param->mtime,
1913 param->msign, param->exclude_mtime,
1918 /* If the previous check matches, but this one is not yet clear,
1919 * we should return 0 to do an RPC on OSTs. */
1925 ret = find_value_cmp(st->st_ctime, param->ctime,
1926 param->csign, param->exclude_ctime,
1931 /* If the previous check matches, but this one is not yet clear,
1932 * we should return 0 to do an RPC on OSTs. */
1940 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1941 void *data, cfs_dirent_t *de)
1943 struct find_param *param = (struct find_param *)data;
1944 int decision = 1; /* 1 is accepted; -1 is rejected. */
1945 lstat_t *st = ¶m->lmd->lmd_st;
1947 int checked_type = 0;
1950 LASSERT(parent != NULL || dir != NULL);
1952 if (param->have_fileinfo == 0)
1953 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1955 /* If a regular expression is presented, make the initial decision */
1956 if (param->pattern != NULL) {
1957 char *fname = strrchr(path, '/');
1958 fname = (fname == NULL ? path : fname + 1);
1959 ret = fnmatch(param->pattern, fname, 0);
1960 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1961 (ret == 0 && param->exclude_pattern))
1965 /* See if we can check the file type from the dirent. */
1966 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1967 de->d_type < DT_MAX) {
1969 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1970 if (param->exclude_type)
1973 if (!param->exclude_type)
1981 /* Request MDS for the stat info if some of these parameters need
1982 * to be compared. */
1983 if (param->obduuid || param->check_uid || param->check_gid ||
1984 param->check_pool || param->atime || param->ctime ||
1985 param->mtime || param->check_size)
1987 if (param->type && checked_type == 0)
1990 if (param->have_fileinfo == 0 && decision == 0) {
1992 /* retrieve needed file info */
1993 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1994 (void *)param->lmd);
1996 char *fname = strrchr(path, '/');
1997 fname = (fname == NULL ? path : fname + 1);
1999 /* retrieve needed file info */
2000 strncpy((char *)param->lmd, fname, param->lumlen);
2001 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2002 (void *)param->lmd);
2007 if (errno == ENOTTY) {
2008 /* ioctl is not supported, it is not a lustre fs.
2009 * Do the regular lstat(2) instead. */
2011 ret = lstat_f(path, st);
2014 llapi_error(LLAPI_MSG_ERROR, ret,
2015 "error: %s: lstat failed for %s",
2019 } else if (errno == ENOENT) {
2020 llapi_error(LLAPI_MSG_WARN, -ENOENT,
2021 "warning: %s: %s does not exist",
2026 llapi_error(LLAPI_MSG_ERROR, ret,
2027 "error: %s: %s failed for %s",
2028 __func__, dir ? "LL_IOC_MDC_GETINFO" :
2029 "IOC_MDC_GETFILEINFO", path);
2034 if (param->type && !checked_type) {
2035 if ((st->st_mode & S_IFMT) == param->type) {
2036 if (param->exclude_type)
2039 if (!param->exclude_type)
2045 if (param->obduuid) {
2046 if (lustre_fs && param->got_uuids &&
2047 param->st_dev != st->st_dev) {
2048 /* A lustre/lustre mount point is crossed. */
2049 param->got_uuids = 0;
2050 param->obds_printed = 0;
2051 param->obdindex = OBD_NOT_FOUND;
2054 if (lustre_fs && !param->got_uuids) {
2055 ret = setup_obd_indexes(dir ? dir : parent, param);
2059 param->st_dev = st->st_dev;
2060 } else if (!lustre_fs && param->got_uuids) {
2061 /* A lustre/non-lustre mount point is crossed. */
2062 param->got_uuids = 0;
2063 param->obdindex = OBD_NOT_FOUND;
2067 /* If an OBD UUID is specified but no one matches, skip this file. */
2068 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2071 /* If a OST UUID is given, and some OST matches, check it here. */
2072 if (param->obdindex != OBD_NOT_FOUND) {
2073 if (!S_ISREG(st->st_mode))
2076 /* Only those files should be accepted, which have a
2077 * stripe on the specified OST. */
2078 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
2082 struct lov_user_ost_data_v1 *lmm_objects;
2084 if (param->lmd->lmd_lmm.lmm_magic ==
2085 LOV_USER_MAGIC_V3) {
2086 struct lov_user_md_v3 *lmmv3 =
2087 (void *)¶m->lmd->lmd_lmm;
2089 lmm_objects = lmmv3->lmm_objects;
2091 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2095 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2096 for (j = 0; j < param->num_obds; j++) {
2097 if (param->obdindexes[j] ==
2098 lmm_objects[i].l_ost_idx) {
2099 if (param->exclude_obd)
2104 /* If an OBD matches, just break */
2105 if (j != param->num_obds)
2109 if (i == param->lmd->lmd_lmm.lmm_stripe_count) {
2110 if (!param->exclude_obd)
2116 if (param->check_uid) {
2117 if (st->st_uid == param->uid) {
2118 if (param->exclude_uid)
2121 if (!param->exclude_uid)
2126 if (param->check_gid) {
2127 if (st->st_gid == param->gid) {
2128 if (param->exclude_gid)
2131 if (!param->exclude_gid)
2136 if (param->check_pool) {
2137 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2139 /* empty requested pool is taken as no pool search => V1 */
2140 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2141 (param->poolname[0] == '\0')) ||
2142 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2143 (strncmp(lmmv3->lmm_pool_name,
2144 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
2145 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2146 (strcmp(param->poolname, "*") == 0))) {
2147 if (param->exclude_pool)
2150 if (!param->exclude_pool)
2155 /* Check the time on mds. */
2157 if (param->atime || param->ctime || param->mtime) {
2160 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2161 param->lmd->lmd_lmm.lmm_stripe_count)
2163 decision = find_time_check(st, param, for_mds);
2168 /* If file still fits the request, ask ost for updated info.
2169 The regular stat is almost of the same speed as some new
2170 'glimpse-size-ioctl'. */
2172 if (param->check_size && S_ISREG(st->st_mode) &&
2173 param->lmd->lmd_lmm.lmm_stripe_count)
2177 /* For regular files with the stripe the decision may have not
2178 * been taken yet if *time or size is to be checked. */
2179 LASSERT(S_ISREG(st->st_mode) &&
2180 param->lmd->lmd_lmm.lmm_stripe_count);
2182 if (param->obdindex != OBD_NOT_FOUND) {
2183 /* Check whether the obd is active or not, if it is
2184 * not active, just print the object affected by this
2187 struct obd_statfs stat_buf;
2188 struct obd_uuid uuid_buf;
2190 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2191 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2192 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
2193 param->obdindex, &stat_buf,
2196 llapi_printf(LLAPI_MSG_NORMAL,
2197 "obd_uuid: %s failed %s ",
2198 param->obduuid->uuid,
2204 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
2205 (void *)param->lmd);
2206 } else if (parent) {
2207 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
2208 (void *)param->lmd);
2212 if (errno == ENOENT) {
2213 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2214 "warning: %s: %s does not exist",
2219 llapi_error(LLAPI_MSG_ERROR, ret,
2220 "%s: IOC_LOV_GETINFO on %s failed",
2226 /* Check the time on osc. */
2227 decision = find_time_check(st, param, 0);
2234 if (param->check_size)
2235 decision = find_value_cmp(st->st_size, param->size,
2236 param->size_sign, param->exclude_size,
2237 param->size_units, 0);
2239 if (decision != -1) {
2240 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
2242 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
2244 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2248 /* Do not get down anymore? */
2249 if (param->depth == param->maxdepth)
2256 int llapi_find(char *path, struct find_param *param)
2258 return param_callback(path, cb_find_init, cb_common_fini, param);
2262 * Get MDT number that the file/directory inode referenced
2263 * by the open fd resides on.
2264 * Return 0 and mdtidx on success, or -ve errno.
2266 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
2268 if (ioctl(fd, LL_IOC_GET_MDTIDX, &mdtidx) < 0)
2273 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
2276 struct find_param *param = (struct find_param *)data;
2280 LASSERT(parent != NULL || d != NULL);
2283 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2284 } else if (parent) {
2287 fd = open(path, O_RDONLY);
2289 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2297 if (ret == -ENODATA) {
2298 if (!param->obduuid)
2299 llapi_printf(LLAPI_MSG_NORMAL,
2300 "%s has no stripe info\n", path);
2302 } else if (ret == -ENOENT) {
2303 llapi_error(LLAPI_MSG_WARN, ret,
2304 "warning: %s: %s does not exist",
2307 } else if (ret == -ENOTTY) {
2308 llapi_error(LLAPI_MSG_ERROR, ret,
2309 "%s: '%s' not on a Lustre fs?",
2312 llapi_error(LLAPI_MSG_ERROR, ret,
2313 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2320 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2322 llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
2326 /* Do not get down anymore? */
2327 if (param->depth == param->maxdepth)
2334 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2337 struct find_param *param = (struct find_param *)data;
2340 LASSERT(parent != NULL || d != NULL);
2342 if (param->obduuid) {
2344 ret = setup_obd_uuid(d ? d : parent, path, param);
2350 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2351 (void *)¶m->lmd->lmd_lmm);
2352 } else if (parent) {
2353 char *fname = strrchr(path, '/');
2354 fname = (fname == NULL ? path : fname + 1);
2356 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2358 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2359 (void *)¶m->lmd->lmd_lmm);
2363 if (errno == ENODATA && d != NULL) {
2364 /* We need to "fake" the "use the default" values
2365 * since the lmm struct is zeroed out at this point.
2366 * The magic needs to be set in order to satisfy
2367 * a check later on in the code path.
2368 * The object_seq needs to be set for the "(Default)"
2369 * prefix to be displayed. */
2370 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
2371 lmm->lmm_magic = LOV_MAGIC_V1;
2373 lmm->lmm_object_seq = FID_SEQ_LOV_DEFAULT;
2374 lmm->lmm_stripe_count = 0;
2375 lmm->lmm_stripe_size = 0;
2376 lmm->lmm_stripe_offset = -1;
2379 } else if (errno == ENODATA && parent != NULL) {
2380 if (!param->obduuid)
2381 llapi_printf(LLAPI_MSG_NORMAL,
2382 "%s has no stripe info\n", path);
2384 } else if (errno == ENOENT) {
2385 llapi_error(LLAPI_MSG_WARN, -ENOENT,
2386 "warning: %s: %s does not exist",
2389 } else if (errno == ENOTTY) {
2391 llapi_error(LLAPI_MSG_ERROR, ret,
2392 "%s: '%s' not on a Lustre fs?",
2396 llapi_error(LLAPI_MSG_ERROR, ret,
2397 "error: %s: %s failed for %s",
2398 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2399 "IOC_MDC_GETFILESTRIPE", path);
2406 if (!param->get_mdt_index)
2407 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2410 /* Do not get down anymore? */
2411 if (param->depth == param->maxdepth)
2418 int llapi_getstripe(char *path, struct find_param *param)
2420 return param_callback(path, param->get_mdt_index ?
2421 cb_get_mdt_index : cb_getstripe,
2422 cb_common_fini, param);
2425 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2426 struct obd_statfs *stat_buf,
2427 struct obd_uuid *uuid_buf)
2430 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2432 struct obd_ioctl_data data = { 0 };
2435 data.ioc_inlbuf1 = (char *)&type;
2436 data.ioc_inllen1 = sizeof(__u32);
2437 data.ioc_inlbuf2 = (char *)&index;
2438 data.ioc_inllen2 = sizeof(__u32);
2439 data.ioc_pbuf1 = (char *)stat_buf;
2440 data.ioc_plen1 = sizeof(struct obd_statfs);
2441 data.ioc_pbuf2 = (char *)uuid_buf;
2442 data.ioc_plen2 = sizeof(struct obd_uuid);
2444 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
2446 llapi_error(LLAPI_MSG_ERROR, rc,
2447 "llapi_obd_statfs: error packing ioctl data");
2451 fd = open(path, O_RDONLY);
2452 if (errno == EISDIR)
2453 fd = open(path, O_DIRECTORY | O_RDONLY);
2456 rc = errno ? -errno : -EBADF;
2457 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
2461 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2463 rc = errno ? -errno : -EINVAL;
2469 #define MAX_STRING_SIZE 128
2470 #define DEVICES_LIST "/proc/fs/lustre/devices"
2472 int llapi_ping(char *obd_type, char *obd_name)
2474 char path[MAX_STRING_SIZE];
2478 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2479 obd_type, obd_name);
2481 fd = open(path, O_WRONLY);
2484 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2488 rc = write(fd, buf, 1);
2498 int llapi_target_iterate(int type_num, char **obd_type,
2499 void *args, llapi_cb_t cb)
2501 char buf[MAX_STRING_SIZE];
2502 FILE *fp = fopen(DEVICES_LIST, "r");
2507 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
2511 while (fgets(buf, sizeof(buf), fp) != NULL) {
2512 char *obd_type_name = NULL;
2513 char *obd_name = NULL;
2514 char *obd_uuid = NULL;
2516 struct obd_ioctl_data datal = { 0, };
2517 struct obd_statfs osfs_buffer;
2519 while(bufp[0] == ' ')
2522 for(i = 0; i < 3; i++) {
2523 obd_type_name = strsep(&bufp, " ");
2525 obd_name = strsep(&bufp, " ");
2526 obd_uuid = strsep(&bufp, " ");
2528 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2530 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2531 datal.ioc_plen1 = sizeof(osfs_buffer);
2533 for (i = 0; i < type_num; i++) {
2534 if (strcmp(obd_type_name, obd_type[i]) != 0)
2537 cb(obd_type_name, obd_name, obd_uuid, args);
2544 static void do_target_check(char *obd_type_name, char *obd_name,
2545 char *obd_uuid, void *args)
2549 rc = llapi_ping(obd_type_name, obd_name);
2550 if (rc == ENOTCONN) {
2551 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2553 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
2555 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2559 int llapi_target_check(int type_num, char **obd_type, char *dir)
2561 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2564 #undef MAX_STRING_SIZE
2566 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2568 char raw[OBD_MAX_IOCTL_BUFFER];
2569 char out[LLOG_CHUNK_SIZE];
2571 struct obd_ioctl_data data = { 0 };
2576 sprintf(key, "%s", keyword);
2577 memset(raw, 0, sizeof(raw));
2578 memset(out, 0, sizeof(out));
2579 data.ioc_inlbuf1 = key;
2580 data.ioc_inllen1 = strlen(key) + 1;
2582 data.ioc_inlbuf2 = node_name;
2583 data.ioc_inllen2 = strlen(node_name) + 1;
2585 data.ioc_pbuf1 = out;
2586 data.ioc_plen1 = sizeof(out);
2587 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2591 root = opendir(dir);
2594 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", dir);
2598 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2601 llapi_error(LLAPI_MSG_ERROR, rc,
2602 "ioctl OBD_IOC_CATINFO failed");
2604 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2611 /* Is this a lustre fs? */
2612 int llapi_is_lustre_mnttype(const char *type)
2614 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2617 /* Is this a lustre client fs? */
2618 int llapi_is_lustre_mnt(struct mntent *mnt)
2620 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2621 strstr(mnt->mnt_fsname, ":/") != NULL);
2624 int llapi_quotacheck(char *mnt, int check_type)
2629 root = opendir(mnt);
2632 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2636 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2644 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2650 root = opendir(mnt);
2653 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2658 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2662 if (poll_intvl < 30)
2670 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2675 root = opendir(mnt);
2678 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2682 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2690 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2693 struct find_param *param = (struct find_param *)data;
2697 LASSERT(parent != NULL || d != NULL);
2700 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2701 (void *)param->lmd);
2702 } else if (parent) {
2703 char *fname = strrchr(path, '/');
2704 fname = (fname == NULL ? path : fname + 1);
2706 strncpy((char *)param->lmd, fname, param->lumlen);
2707 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2708 (void *)param->lmd);
2714 if (errno == ENODATA) {
2715 if (!param->obduuid && !param->quiet)
2716 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
2717 "%s has no stripe info", path);
2719 } else if (errno == ENOENT) {
2720 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2721 "warning: %s: %s does not exist",
2724 } else if (errno != EISDIR) {
2726 llapi_error(LLAPI_MSG_ERROR, rc, "%s ioctl failed for %s.",
2727 d ? "LL_IOC_MDC_GETINFO" :
2728 "IOC_MDC_GETFILEINFO", path);
2733 st = ¶m->lmd->lmd_st;
2735 /* libc chown() will do extra check, and if the real owner is
2736 * the same as the ones to set, it won't fall into kernel, so
2737 * invoke syscall directly. */
2738 rc = syscall(SYS_chown, path, -1, -1);
2740 llapi_error(LLAPI_MSG_ERROR, errno,
2741 "error: chown %s", path);
2743 rc = chmod(path, st->st_mode);
2746 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
2753 int llapi_quotachown(char *path, int flag)
2755 struct find_param param;
2757 memset(¶m, 0, sizeof(param));
2758 param.recursive = 1;
2762 return param_callback(path, cb_quotachown, NULL, ¶m);
2768 #include <sys/wait.h>
2772 static int rmtacl_notify(int ops)
2776 int found = 0, fd, rc;
2778 fp = setmntent(MOUNTED, "r");
2781 llapi_error(LLAPI_MSG_ERROR, rc,
2782 "error setmntent(%s)", MOUNTED);
2787 mnt = getmntent(fp);
2791 if (!llapi_is_lustre_mnt(mnt))
2794 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2797 llapi_error(LLAPI_MSG_ERROR, rc,
2798 "Can't open '%s'\n", mnt->mnt_dir);
2802 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2805 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd);
2815 static char *next_token(char *p, int div)
2821 while (*p && *p != ':' && !isspace(*p))
2824 while (*p == ':' || isspace(*p))
2827 return *p ? p : NULL;
2830 static int rmtacl_name2id(char *name, int is_user)
2835 pw = getpwnam(name);
2839 return (int)(pw->pw_uid);
2843 gr = getgrnam(name);
2847 return (int)(gr->gr_gid);
2851 static int isodigit(int c)
2853 return (c >= '0' && c <= '7') ? 1 : 0;
2857 * Whether the name is just digits string (uid/gid) already or not.
2862 static int str_is_id(char *str)
2869 if (*str == 'x' || *str == 'X') { /* for Hex. */
2870 if (!isxdigit(*(++str)))
2873 while (isxdigit(*(++str)));
2874 } else if (isodigit(*str)) { /* for Oct. */
2875 while (isodigit(*(++str)));
2877 } else if (isdigit(*str)) { /* for Dec. */
2878 while (isdigit(*(++str)));
2881 return (*str == 0) ? 1 : 0;
2891 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2893 static rmtacl_name_t rmtacl_namelist[] = {
2894 { RMTACL_OPTNAME("user:"), 1, 0 },
2895 { RMTACL_OPTNAME("group:"), 0, 0 },
2896 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2897 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2898 /* for --tabular option */
2899 { RMTACL_OPTNAME("user"), 1, 1 },
2900 { RMTACL_OPTNAME("group"), 0, 1 },
2904 static int rgetfacl_output(char *str)
2906 char *start = NULL, *end = NULL;
2907 int is_user = 0, n, id;
2914 for (rn = rmtacl_namelist; rn->name; rn++) {
2915 if(strncmp(str, rn->name, rn->length) == 0) {
2916 if (!rn->next_token)
2917 start = str + rn->length;
2919 start = next_token(str + rn->length, 0);
2920 is_user = rn->is_user;
2925 end = next_token(start, 1);
2926 if (end == NULL || start == end) {
2927 n = printf("%s", str);
2933 id = rmtacl_name2id(start, is_user);
2934 if (id == INVALID_ID) {
2935 if (str_is_id(start)) {
2937 n = printf("%s", str);
2940 } else if ((id == NOBODY_UID && is_user) ||
2941 (id == NOBODY_GID && !is_user)) {
2943 n = printf("%s", str);
2947 n = printf("%s%d%s", str, id, end);
2952 static int child_status(int status)
2954 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2957 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2960 int fd[2], status, rc;
2967 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
2974 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
2979 /* child process redirects its output. */
2982 if (dup2(fd[1], 1) < 0) {
2984 llapi_error(LLAPI_MSG_ERROR, rc,
2985 "Can't dup2 %d\n", fd[1]);
2995 status = rmtacl_notify(ops);
2999 exit(execvp(argv[0], argv));
3002 /* the following is parent process */
3003 fp = fdopen(fd[0], "r");
3006 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3012 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3013 if (output_func(buf) < 0)
3014 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3020 if (waitpid(pid, &status, 0) < 0) {
3022 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3026 return child_status(status);
3029 int llapi_lsetfacl(int argc, char *argv[])
3031 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3034 int llapi_lgetfacl(int argc, char *argv[])
3036 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3039 int llapi_rsetfacl(int argc, char *argv[])
3041 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3044 int llapi_rgetfacl(int argc, char *argv[])
3046 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3049 int llapi_cp(int argc, char *argv[])
3053 rc = rmtacl_notify(RMT_RSETFACL);
3057 exit(execvp(argv[0], argv));
3060 int llapi_ls(int argc, char *argv[])
3064 rc = rmtacl_notify(RMT_LGETFACL);
3068 exit(execvp(argv[0], argv));
3071 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3072 * format must have %s%s, buf must be > 16
3073 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3074 * then buf = "lustre-MDT0000"
3076 static int get_mdtname(char *name, char *format, char *buf)
3078 char suffix[]="-MDT0000";
3079 int len = strlen(name);
3081 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3082 name[len - 5] = '\0';
3087 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3090 /* Not enough room to add suffix */
3091 llapi_err_noerrno(LLAPI_MSG_ERROR,
3092 "MDT name too long |%s|", name);
3097 return sprintf(buf, format, name, suffix);
3100 /** ioctl on filsystem root, with mdtindex sent as data
3101 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3102 * \param mdtidxp pointer to integer within data to be filled in with the
3103 * mdt index (0 if no mdt is specified). NULL won't be filled.
3105 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3112 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3113 Open root and parse mdt index. */
3114 if (mdtname[0] == '/') {
3116 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3117 (char *)mdtname, -1);
3119 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3121 ptr = fsname + strlen(fsname) - 8;
3123 index = strtol(ptr + 4, NULL, 10);
3124 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3128 llapi_err_noerrno(LLAPI_MSG_ERROR,
3129 "Can't open %s: %d\n", mdtname, rc);
3136 rc = ioctl(fd, opc, data);
3141 if (rc && want_error)
3142 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
3148 /****** Changelog API ********/
3150 static int changelog_ioctl(const char *mdtname, int opc, int id,
3151 long long recno, int flags)
3153 struct ioc_changelog data;
3157 data.icc_recno = recno;
3158 data.icc_flags = flags;
3159 idx = (int *)(&data.icc_mdtindex);
3161 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3164 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3165 struct changelog_private {
3168 lustre_kernelcomm kuc;
3171 /** Start reading from a changelog
3172 * @param priv Opaque private control structure
3173 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3174 * @param device Report changes recorded on this MDT
3175 * @param startrec Report changes beginning with this record number
3176 * (just call llapi_changelog_fini when done; don't need an endrec)
3178 int llapi_changelog_start(void **priv, int flags, const char *device,
3181 struct changelog_private *cp;
3184 /* Set up the receiver control struct */
3185 cp = calloc(1, sizeof(*cp));
3189 cp->magic = CHANGELOG_PRIV_MAGIC;
3192 /* Set up the receiver */
3193 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
3199 /* Tell the kernel to start sending */
3200 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3202 /* Only the kernel reference keeps the write side open */
3203 close(cp->kuc.lk_wfd);
3206 /* frees and clears priv */
3207 llapi_changelog_fini(priv);
3218 /** Finish reading from a changelog */
3219 int llapi_changelog_fini(void **priv)
3221 struct changelog_private *cp = (struct changelog_private *)*priv;
3223 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3226 libcfs_ukuc_stop(&cp->kuc);
3232 /** Read the next changelog entry
3233 * @param priv Opaque private control structure
3234 * @param rech Changelog record handle; record will be allocated here
3235 * @return 0 valid message received; rec is set
3239 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
3241 struct changelog_private *cp = (struct changelog_private *)priv;
3242 struct kuc_hdr *kuch;
3245 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3249 kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
3254 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
3255 CR_MAXSIZE + sizeof(*kuch),
3256 KUC_TRANSPORT_CHANGELOG);
3260 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
3261 ((kuch->kuc_msgtype != CL_RECORD) &&
3262 (kuch->kuc_msgtype != CL_EOF))) {
3263 llapi_err_noerrno(LLAPI_MSG_ERROR,
3264 "Unknown changelog message type %d:%d\n",
3265 kuch->kuc_transport, kuch->kuc_msgtype);
3270 if (kuch->kuc_msgtype == CL_EOF) {
3271 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
3280 /* Our message is a changelog_rec. Use pointer math to skip
3281 * kuch_hdr and point directly to the message payload.
3283 *rech = (struct changelog_rec *)(kuch + 1);
3293 /** Release the changelog record when done with it. */
3294 int llapi_changelog_free(struct changelog_rec **rech)
3297 /* We allocated memory starting at the kuc_hdr, but passed
3298 * the consumer a pointer to the payload.
3299 * Use pointer math to get back to the header.
3301 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3308 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3314 llapi_err_noerrno(LLAPI_MSG_ERROR,
3315 "can't purge negative records\n");
3319 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3320 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
3321 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3322 llapi_err_noerrno(LLAPI_MSG_ERROR,
3323 "expecting id of the form '"
3324 CHANGELOG_USER_PREFIX
3325 "<num>'; got '%s'\n", idstr);
3329 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
3332 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
3333 int buflen, long long *recno, int *linkno)
3336 struct getinfo_fid2path *gf;
3339 while (*fidstr == '[')
3342 sscanf(fidstr, SFID, RFID(&fid));
3343 if (!fid_is_sane(&fid)) {
3344 llapi_err_noerrno(LLAPI_MSG_ERROR,
3345 "bad FID format [%s], should be "DFID"\n",
3346 fidstr, (__u64)1, 2, 0);
3350 gf = malloc(sizeof(*gf) + buflen);
3354 gf->gf_recno = *recno;
3355 gf->gf_linkno = *linkno;
3356 gf->gf_pathlen = buflen;
3358 /* Take path or fsname */
3359 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
3362 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
3364 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3365 *recno = gf->gf_recno;
3366 *linkno = gf->gf_linkno;
3373 static int path2fid_from_lma(const char *path, lustre_fid *fid)
3376 struct lustre_mdt_attrs *lma;
3379 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3382 lma = (struct lustre_mdt_attrs *)buf;
3383 fid_le_to_cpu(fid, &lma->lma_self_fid);
3387 int llapi_path2fid(const char *path, lustre_fid *fid)
3391 memset(fid, 0, sizeof(*fid));
3392 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3394 if (errno == ELOOP) /* symbolic link */
3395 return path2fid_from_lma(path, fid);
3399 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3400 if (rc == -EINVAL) /* char special device */
3401 rc = path2fid_from_lma(path, fid);
3407 /****** HSM Copytool API ********/
3408 #define CT_PRIV_MAGIC 0xC0BE2001
3409 struct copytool_private {
3412 lustre_kernelcomm kuc;
3416 #include <libcfs/libcfs.h>
3418 /** Register a copytool
3419 * @param[out] priv Opaque private control structure
3420 * @param fsname Lustre filesystem
3421 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3422 * @param archive_count
3423 * @param archives Which archive numbers this copytool is responsible for
3425 int llapi_copytool_start(void **priv, char *fsname, int flags,
3426 int archive_count, int *archives)
3428 struct copytool_private *ct;
3431 if (archive_count > 0 && archives == NULL) {
3432 llapi_err_noerrno(LLAPI_MSG_ERROR,
3433 "NULL archive numbers");
3437 ct = calloc(1, sizeof(*ct));
3441 ct->fsname = malloc(strlen(fsname) + 1);
3442 if (ct->fsname == NULL) {
3446 strcpy(ct->fsname, fsname);
3447 ct->magic = CT_PRIV_MAGIC;
3449 for (rc = 0; rc < archive_count; rc++) {
3450 if (archives[rc] > sizeof(ct->archives)) {
3451 llapi_err_noerrno(LLAPI_MSG_ERROR,
3452 "Maximum of %d archives supported",
3453 sizeof(ct->archives));
3456 ct->archives |= 1 << archives[rc];
3458 /* special case: if no archives specified, default to archive #0. */
3459 if (ct->archives == 0)
3462 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3466 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3467 ct->kuc.lk_data = ct->archives;
3468 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3470 /* Only the kernel reference keeps the write side open */
3471 close(ct->kuc.lk_wfd);
3486 /** Deregister a copytool */
3487 int llapi_copytool_fini(void **priv)
3489 struct copytool_private *ct = (struct copytool_private *)*priv;
3491 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3494 /* Tell the kernel to stop sending us messages */
3495 ct->kuc.lk_flags = LK_FLG_STOP;
3496 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3498 /* Shut down the kernelcomms */
3499 libcfs_ukuc_stop(&ct->kuc);
3507 /** Wait for the next hsm_action_list
3508 * @param priv Opaque private control structure
3509 * @param halh Action list handle, will be allocated here
3510 * @param msgsize Number of bytes in the message, will be set here
3511 * @return 0 valid message received; halh and msgsize are set
3514 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3516 struct copytool_private *ct = (struct copytool_private *)priv;
3517 struct kuc_hdr *kuch;
3518 struct hsm_action_list *hal;
3521 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3523 if (halh == NULL || msgsize == NULL)
3526 kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
3530 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
3531 HAL_MAXSIZE + sizeof(*kuch),
3536 /* Handle generic messages */
3537 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3538 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3543 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3544 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3545 llapi_err_noerrno(LLAPI_MSG_ERROR,
3546 "Unknown HSM message type %d:%d\n",
3547 kuch->kuc_transport, kuch->kuc_msgtype);
3552 /* Our message is a hsm_action_list. Use pointer math to skip
3553 * kuch_hdr and point directly to the message payload.
3555 hal = (struct hsm_action_list *)(kuch + 1);
3557 /* Check that we have registered for this archive # */
3558 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3559 llapi_err_noerrno(LLAPI_MSG_INFO,
3560 "Ignoring request for archive #%d (bitmask %#x)\n",
3561 hal->hal_archive_num, ct->archives);
3567 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3577 /** Release the action list when done with it. */
3578 int llapi_copytool_free(struct hsm_action_list **hal)
3580 /* Reuse the llapi_changelog_free function */
3581 return llapi_changelog_free((struct changelog_rec **)hal);
3584 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3589 root = opendir(mnt);
3592 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3596 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3599 llapi_error(LLAPI_MSG_ERROR, rc,
3600 "ioctl on %s for getting connect flags failed", mnt);
3606 int llapi_get_version(char *buffer, int buffer_size,
3611 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
3613 fd = open(OBD_DEV_PATH, O_RDONLY);
3617 memset(buffer, 0, buffer_size);
3618 data->ioc_version = OBD_IOCTL_VERSION;
3619 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
3620 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
3621 data->ioc_len = obd_ioctl_packlen(data);
3623 rc = ioctl(fd, OBD_GET_VERSION, buffer);
3630 *version = data->ioc_bulk;