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 * 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>
60 #include <sys/types.h>
61 #include <sys/syscall.h>
62 #include <sys/param.h>
65 #ifdef HAVE_ASM_TYPES_H
66 #include <asm/types.h>
68 #ifdef HAVE_LINUX_UNISTD_H
69 #include <linux/unistd.h>
74 #include <liblustre.h>
75 #include <lnet/lnetctl.h>
77 #include <lustre_lib.h>
78 #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 void llapi_err(int level, char *fmt, ...)
138 int tmp_errno = abs(errno);
140 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
144 vfprintf(stderr, fmt, args);
147 if (level & LLAPI_MSG_NO_ERRNO)
148 fprintf(stderr, "\n");
150 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
153 #define llapi_err_noerrno(level, fmt, a...) \
154 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
156 void llapi_printf(int level, char *fmt, ...)
160 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
164 vfprintf(stdout, fmt, args);
168 /* size_units is unchanged if no specifier used */
169 int parse_size(char *optarg, unsigned long long *size,
170 unsigned long long *size_units, int bytes_spec)
174 *size = strtoull(optarg, &end, 0);
177 if ((*end == 'b') && *(end+1) == '\0' &&
178 (*size & (~0ULL << (64 - 9))) == 0 &&
181 *size_units = 1 << 9;
182 } else if ((*end == 'b') && *(end+1) == '\0' &&
185 } else if ((*end == 'k' || *end == 'K') &&
186 *(end+1) == '\0' && (*size &
187 (~0ULL << (64 - 10))) == 0) {
189 *size_units = 1 << 10;
190 } else if ((*end == 'm' || *end == 'M') &&
191 *(end+1) == '\0' && (*size &
192 (~0ULL << (64 - 20))) == 0) {
194 *size_units = 1 << 20;
195 } else if ((*end == 'g' || *end == 'G') &&
196 *(end+1) == '\0' && (*size &
197 (~0ULL << (64 - 30))) == 0) {
199 *size_units = 1 << 30;
200 } else if ((*end == 't' || *end == 'T') &&
201 *(end+1) == '\0' && (*size &
202 (~0ULL << (64 - 40))) == 0) {
204 *size_units = 1ULL << 40;
205 } else if ((*end == 'p' || *end == 'P') &&
206 *(end+1) == '\0' && (*size &
207 (~0ULL << (64 - 50))) == 0) {
209 *size_units = 1ULL << 50;
210 } else if ((*end == 'e' || *end == 'E') &&
211 *(end+1) == '\0' && (*size &
212 (~0ULL << (64 - 60))) == 0) {
214 *size_units = 1ULL << 60;
223 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
224 int stripe_count, int stripe_pattern)
228 /* 64 KB is the largest common page size I'm aware of (on ia64), but
229 * check the local page size just in case. */
230 page_size = LOV_MIN_STRIPE_SIZE;
231 if (getpagesize() > page_size) {
232 page_size = getpagesize();
233 llapi_err_noerrno(LLAPI_MSG_WARN,
234 "warning: your page size (%u) is "
235 "larger than expected (%u)", page_size,
236 LOV_MIN_STRIPE_SIZE);
238 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
240 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
241 "must be an even multiple of %d bytes",
242 stripe_size, page_size);
245 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
247 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
251 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
253 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
257 if (stripe_size >= (1ULL << 32)) {
259 llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
260 " is not currently supported and would wrap");
266 static int find_target_obdpath(char *fsname, char *path)
269 char pattern[PATH_MAX + 1];
272 snprintf(pattern, PATH_MAX,
273 "/proc/fs/lustre/lov/%s-*/target_obd",
275 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
276 if (rc == GLOB_NOMATCH)
281 strcpy(path, glob_info.gl_pathv[0]);
282 globfree(&glob_info);
286 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
289 char pattern[PATH_MAX + 1];
292 snprintf(pattern, PATH_MAX,
293 "/proc/fs/lustre/lov/%s-*/pools/%s",
295 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
296 /* If no pools, make sure the lov is available */
297 if ((rc == GLOB_NOMATCH) &&
298 (find_target_obdpath(fsname, poolpath) == -ENODEV))
303 strcpy(poolpath, glob_info.gl_pathv[0]);
304 globfree(&glob_info);
309 * if pool is NULL, search ostname in target_obd
310 * if pool is not NULL:
311 * if pool not found returns errno < 0
312 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
313 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
315 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
318 char buffer[PATH_MAX + 1];
322 len = strlen(ostname);
324 if (poolname == NULL)
325 rc = find_target_obdpath(fsname, buffer);
327 rc = find_poolpath(fsname, poolname, buffer);
331 if ((fd = fopen(buffer, "r")) == NULL)
334 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
335 if (poolname == NULL) {
337 /* Search for an ostname in the list of OSTs
338 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
339 ptr = strchr(buffer, ' ');
341 (strncmp(ptr + 1, ostname, len) == 0)) {
346 /* Search for an ostname in a pool,
347 (or an existing non-empty pool if no ostname) */
348 if ((ostname == NULL) ||
349 (strncmp(buffer, ostname, len) == 0)) {
359 int llapi_file_open_pool(const char *name, int flags, int mode,
360 unsigned long long stripe_size, int stripe_offset,
361 int stripe_count, int stripe_pattern, char *pool_name)
363 struct lov_user_md_v3 lum = { 0 };
367 /* Make sure we have a good pool */
368 if (pool_name != NULL) {
369 char fsname[MAX_OBD_NAME + 1], *ptr;
371 if (llapi_search_fsname(name, fsname)) {
372 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
373 "'%s' is not on a Lustre filesystem", name);
377 /* in case user gives the full pool name <fsname>.<poolname>,
378 * strip the fsname */
379 ptr = strchr(pool_name, '.');
382 if (strcmp(pool_name, fsname) != 0) {
384 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
385 "Pool '%s' is not on filesystem '%s'",
392 /* Make sure the pool exists and is non-empty */
393 if ((rc = llapi_search_ost(fsname, pool_name, NULL)) < 1) {
394 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
395 "pool '%s.%s' %s", fsname, pool_name,
396 rc == 0 ? "has no OSTs" : "does not exist");
401 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
402 if (fd < 0 && errno == EISDIR) {
403 fd = open(name, O_DIRECTORY | O_RDONLY);
409 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
413 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
414 stripe_count, stripe_pattern)) != 0){
419 /* Initialize IOCTL striping pattern structure */
420 lum.lmm_magic = LOV_USER_MAGIC_V3;
421 lum.lmm_pattern = stripe_pattern;
422 lum.lmm_stripe_size = stripe_size;
423 lum.lmm_stripe_count = stripe_count;
424 lum.lmm_stripe_offset = stripe_offset;
425 if (pool_name != NULL) {
426 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
428 /* If no pool is specified at all, use V1 request */
429 lum.lmm_magic = LOV_USER_MAGIC_V1;
432 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
433 char *errmsg = "stripe already set";
435 if (errno != EEXIST && errno != EALREADY)
436 errmsg = strerror(errno);
438 llapi_err_noerrno(LLAPI_MSG_ERROR,
439 "error on ioctl "LPX64" for '%s' (%d): %s",
440 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
451 int llapi_file_open(const char *name, int flags, int mode,
452 unsigned long long stripe_size, int stripe_offset,
453 int stripe_count, int stripe_pattern)
455 return llapi_file_open_pool(name, flags, mode, stripe_size,
456 stripe_offset, stripe_count,
457 stripe_pattern, NULL);
460 int llapi_file_create(const char *name, unsigned long long stripe_size,
461 int stripe_offset, int stripe_count, int stripe_pattern)
465 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
466 stripe_offset, stripe_count, stripe_pattern,
475 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
476 int stripe_offset, int stripe_count,
477 int stripe_pattern, char *pool_name)
481 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
482 stripe_offset, stripe_count, stripe_pattern,
492 * Find the fsname, the full path, and/or an open fd.
493 * Either the fsname or path must not be NULL
495 #define WANT_PATH 0x1
496 #define WANT_FSNAME 0x2
498 #define WANT_INDEX 0x8
499 #define WANT_ERROR 0x10
500 static int get_root_path(int want, char *fsname, int *outfd, char *path,
504 char buf[PATH_MAX], mntdir[PATH_MAX];
507 int idx = 0, len = 0, mntlen, fd;
510 /* get the mount point */
511 fp = setmntent(MOUNTED, "r");
513 llapi_err(LLAPI_MSG_ERROR,
514 "setmntent(%s) failed: %s:", MOUNTED,
519 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
522 if (!llapi_is_lustre_mnt(&mnt))
525 if ((want & WANT_INDEX) && (idx++ != index))
528 mntlen = strlen(mnt.mnt_dir);
529 ptr = strrchr(mnt.mnt_fsname, '/');
536 /* Check the fsname for a match, if given */
537 if (!(want & WANT_FSNAME) && fsname != NULL &&
538 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
541 /* If the path isn't set return the first one we find */
542 if (path == NULL || strlen(path) == 0) {
543 strcpy(mntdir, mnt.mnt_dir);
544 if ((want & WANT_FSNAME) && fsname != NULL)
548 /* Otherwise find the longest matching path */
549 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
550 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
551 strcpy(mntdir, mnt.mnt_dir);
553 if ((want & WANT_FSNAME) && fsname != NULL)
562 if ((want & WANT_PATH) && path != NULL)
563 strcpy(path, mntdir);
564 if (want & WANT_FD) {
565 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
573 } else if (want & WANT_ERROR)
574 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
575 "can't find fs root for '%s': %d",
576 (want & WANT_PATH) ? fsname : path, rc);
581 * search lustre mounts
583 * Calling this function will return to the user the mount point, mntdir, and
584 * the file system name, fsname, if the user passed a buffer to this routine.
586 * The user inputs are pathname and index. If the pathname is supplied then
587 * the value of the index will be ignored. The pathname will return data if
588 * the pathname is located on a lustre mount. Index is used to pick which
589 * mount point you want in the case of multiple mounted lustre file systems.
590 * See function lfs_osts in lfs.c for a example of the index use.
592 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
595 int want = WANT_PATH, idx = -1;
597 if (!pathname || pathname[0] == '\0') {
601 strcpy(mntdir, pathname);
605 return get_root_path(want, fsname, NULL, mntdir, idx);
608 int llapi_search_fsname(const char *pathname, char *fsname)
610 char *path = (char*)pathname, buf[PATH_MAX + 1];
612 if (pathname[0] != '/') { /* Need a absolute path */
613 memset(buf, '\0', sizeof(buf));
614 if (realpath(pathname, buf) == NULL) {
615 llapi_err(LLAPI_MSG_ERROR, "pathname '%s' cannot expand",
621 return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
625 /* return the first file matching this pattern */
626 static int first_match(char *pattern, char *buffer)
630 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
633 if (glob_info.gl_pathc < 1) {
634 globfree(&glob_info);
638 strcpy(buffer, glob_info.gl_pathv[0]);
640 globfree(&glob_info);
645 * find the pool directory path under /proc
646 * (can be also used to test if a fsname is known)
648 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
651 char pattern[PATH_MAX + 1];
652 char buffer[PATH_MAX];
654 if (fsname == NULL) {
655 rc = llapi_search_fsname(pathname, buffer);
659 strcpy(pathname, fsname);
662 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
663 rc = first_match(pattern, buffer);
667 /* in fsname test mode, pool_pathname is NULL */
668 if (pool_pathname != NULL)
669 strcpy(pool_pathname, buffer);
675 * Get the list of pool members.
676 * \param poolname string of format \<fsname\>.\<poolname\>
677 * \param members caller-allocated array of char*
678 * \param list_size size of the members array
679 * \param buffer caller-allocated buffer for storing OST names
680 * \param buffer_size size of the buffer
682 * \return number of members retrieved for this pool
683 * \retval -error failure
685 int llapi_get_poolmembers(const char *poolname, char **members,
686 int list_size, char *buffer, int buffer_size)
688 char fsname[PATH_MAX + 1];
690 char pathname[PATH_MAX + 1];
691 char path[PATH_MAX + 1];
698 /* name is FSNAME.POOLNAME */
699 if (strlen(poolname) > PATH_MAX)
701 strcpy(fsname, poolname);
702 pool = strchr(fsname, '.');
709 rc = poolpath(fsname, NULL, pathname);
712 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
717 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
718 sprintf(path, "%s/%s", pathname, pool);
719 if ((fd = fopen(path, "r")) == NULL) {
720 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path);
725 while (fgets(buf, sizeof(buf), fd) != NULL) {
726 if (nb_entries >= list_size) {
731 if ((tmp = strchr(buf, '\n')) != NULL)
733 if (used + strlen(buf) + 1 > buffer_size) {
738 strcpy(buffer + used, buf);
739 members[nb_entries] = buffer + used;
740 used += strlen(buf) + 1;
750 * Get the list of pools in a filesystem.
751 * \param name filesystem name or path
752 * \param poollist caller-allocated array of char*
753 * \param list_size size of the poollist array
754 * \param buffer caller-allocated buffer for storing pool names
755 * \param buffer_size size of the buffer
757 * \return number of pools retrieved for this filesystem
758 * \retval -error failure
760 int llapi_get_poollist(const char *name, char **poollist, int list_size,
761 char *buffer, int buffer_size)
763 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
767 struct dirent *cookie = NULL;
769 unsigned int nb_entries = 0;
770 unsigned int used = 0;
773 /* initilize output array */
774 for (i = 0; i < list_size; i++)
777 /* is name a pathname ? */
778 ptr = strchr(name, '/');
780 /* only absolute pathname is supported */
783 if (!realpath(name, rname)) {
785 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
789 rc = poolpath(NULL, rname, pathname);
792 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
793 " a Lustre filesystem", name);
796 strcpy(fsname, rname);
799 strcpy(fsname, name);
800 rc = poolpath(fsname, NULL, pathname);
804 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
809 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
810 if ((dir = opendir(pathname)) == NULL) {
811 llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'",
817 rc = readdir_r(dir, &pool, &cookie);
820 llapi_err(LLAPI_MSG_ERROR,
821 "Error reading pool list for '%s'", name);
823 } else if ((rc == 0) && (cookie == NULL))
824 /* end of directory */
827 /* ignore . and .. */
828 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
831 /* check output bounds */
832 if (nb_entries >= list_size)
835 /* +2 for '.' and final '\0' */
836 if (used + strlen(pool.d_name) + strlen(fsname) + 2
840 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
841 poollist[nb_entries] = buffer + used;
842 used += strlen(pool.d_name) + strlen(fsname) + 2;
850 /* wrapper for lfs.c and obd.c */
851 int llapi_poollist(const char *name)
853 /* list of pool names (assume that pool count is smaller
855 char *list[FIND_MAX_OSTS];
857 /* fsname-OST0000_UUID < 32 char, 1 per OST */
858 int bufsize = FIND_MAX_OSTS * 32;
861 buffer = malloc(bufsize);
865 if ((name[0] == '/') || (strchr(name, '.') == NULL))
866 /* name is a path or fsname */
867 nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer,
870 /* name is a pool name (<fsname>.<poolname>) */
871 nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer,
874 for (i = 0; i < nb; i++)
875 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
878 return (nb < 0 ? nb : 0);
881 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
882 void *data, struct dirent64 *de);
884 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
885 #define OBD_NOT_FOUND (-1)
887 static int common_param_init(struct find_param *param)
889 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
890 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
891 llapi_err(LLAPI_MSG_ERROR,
892 "error: allocation of %d bytes for ioctl",
893 sizeof(lstat_t) + param->lumlen);
897 param->got_uuids = 0;
898 param->obdindexes = NULL;
899 param->obdindex = OBD_NOT_FOUND;
903 static void find_param_fini(struct find_param *param)
905 if (param->obdindexes)
906 free(param->obdindexes);
912 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
915 struct find_param *param = (struct find_param *)data;
920 static DIR *opendir_parent(char *path)
926 fname = strrchr(path, '/');
932 parent = opendir(path);
937 int llapi_mds_getfileinfo(char *path, DIR *parent,
938 struct lov_user_mds_data *lmd)
940 lstat_t *st = &lmd->lmd_st;
941 char *fname = strrchr(path, '/');
947 fname = (fname == NULL ? path : fname + 1);
948 /* retrieve needed file info */
949 strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT,
951 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
954 if (errno == ENOTTY) {
955 /* ioctl is not supported, it is not a lustre fs.
956 * Do the regular lstat(2) instead. */
957 ret = lstat_f(path, st);
959 llapi_err(LLAPI_MSG_ERROR,
960 "error: %s: lstat failed for %s",
964 } else if (errno == ENOENT) {
965 llapi_err(LLAPI_MSG_WARN,
966 "warning: %s: %s does not exist",
970 llapi_err(LLAPI_MSG_ERROR,
971 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
980 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
981 semantic_func_t sem_init,
982 semantic_func_t sem_fini, void *data,
985 struct dirent64 *dent;
993 if (!d && errno != ENOTDIR) {
994 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
997 } else if (!d && !parent) {
998 /* ENOTDIR. Open the parent dir. */
999 p = opendir_parent(path);
1001 GOTO(out, ret = -EINVAL);
1004 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1010 while ((dent = readdir64(d)) != NULL) {
1011 ((struct find_param *)data)->have_fileinfo = 0;
1013 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1017 if ((len + dent->d_reclen + 2) > size) {
1018 llapi_err(LLAPI_MSG_ERROR,
1019 "error: %s: string buffer is too small",
1024 if (path[len-1] != '/')
1026 strcat(path, dent->d_name);
1028 if (dent->d_type == DT_UNKNOWN) {
1029 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1031 ret = llapi_mds_getfileinfo(path, d,
1032 ((struct find_param *)data)->lmd);
1034 ((struct find_param *)data)->have_fileinfo = 1;
1036 llapi_filetype_dir_table[st->st_mode &
1043 switch (dent->d_type) {
1045 llapi_err(LLAPI_MSG_ERROR,
1046 "error: %s: '%s' is UNKNOWN type %d",
1047 __func__, dent->d_name, dent->d_type);
1050 ret = llapi_semantic_traverse(path, size, d, sem_init,
1051 sem_fini, data, dent);
1058 ret = sem_init(path, d, NULL, data, dent);
1062 if (sem_fini && ret == 0)
1063 sem_fini(path, d, NULL, data, dent);
1071 sem_fini(path, parent, d, data, de);
1080 static int param_callback(char *path, semantic_func_t sem_init,
1081 semantic_func_t sem_fini, struct find_param *param)
1083 int ret, len = strlen(path);
1086 if (len > PATH_MAX) {
1087 llapi_err(LLAPI_MSG_ERROR, "Path name '%s' is too long", path);
1091 buf = (char *)malloc(PATH_MAX + 1);
1095 ret = common_param_init(param);
1100 strncpy(buf, path, PATH_MAX + 1);
1101 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1102 sem_fini, param, NULL);
1104 find_param_fini(param);
1106 return ret < 0 ? ret : 0;
1109 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1111 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1114 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
1119 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1123 fd = open(path, O_RDONLY);
1126 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1130 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1138 * If uuidp is NULL, return the number of available obd uuids.
1139 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1140 * there are more OSTs then allocated to uuidp, then an error is returned with
1141 * the ost_count set to number of available obd uuids.
1143 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1145 struct obd_uuid lov_name;
1148 int rc = 0, index = 0;
1150 /* Get the lov name */
1151 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
1155 /* Now get the ost uuids from /proc */
1156 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1158 fp = fopen(buf, "r");
1161 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
1165 while (fgets(buf, sizeof(buf), fp) != NULL) {
1166 if (uuidp && (index < *ost_count)) {
1167 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1175 if (uuidp && (index >= *ost_count))
1182 /* Check if user specified value matches a real uuid. Ignore _UUID,
1183 * -osc-4ba41334, other trailing gunk in comparison.
1184 * @param real_uuid ends in "_UUID"
1185 * @param search_uuid may or may not end in "_UUID"
1187 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1189 int cmplen = strlen(real_uuid);
1190 int searchlen = strlen(search_uuid);
1192 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1194 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1197 /* The UUIDs may legitimately be different lengths, if
1198 * the system was upgraded from an older version. */
1199 if (cmplen != searchlen)
1202 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1205 /* Here, param->obduuid points to a single obduuid, the index of which is
1206 * returned in param->obdindex */
1207 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1209 struct obd_uuid lov_uuid;
1210 char uuid[sizeof(struct obd_uuid)];
1215 if (param->got_uuids)
1218 /* Get the lov name */
1219 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
1221 if (errno != ENOTTY) {
1223 llapi_err(LLAPI_MSG_ERROR,
1224 "error: can't get lov name: %s", dname);
1231 param->got_uuids = 1;
1233 /* Now get the ost uuids from /proc */
1234 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1236 fp = fopen(buf, "r");
1239 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
1243 if (!param->obduuid && !param->quiet && !param->obds_printed)
1244 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
1246 while (fgets(buf, sizeof(buf), fp) != NULL) {
1247 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1250 if (param->obduuid) {
1251 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1252 param->obdindex = index;
1255 } else if (!param->quiet && !param->obds_printed) {
1256 /* Print everything */
1257 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1260 param->obds_printed = 1;
1264 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1265 llapi_err_noerrno(LLAPI_MSG_ERROR,
1266 "error: %s: unknown obduuid: %s",
1267 __FUNCTION__, param->obduuid->uuid);
1274 /* In this case, param->obduuid will be an array of obduuids and
1275 * obd index for all these obduuids will be returned in
1276 * param->obdindexes */
1277 static int setup_obd_indexes(DIR *dir, struct find_param *param)
1279 struct obd_uuid *uuids = NULL;
1280 int obdcount = INIT_ALLOC_NUM_OSTS;
1281 int ret, obd_valid = 0, obdnum, i;
1283 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1284 sizeof(struct obd_uuid));
1289 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
1292 struct obd_uuid *uuids_temp;
1294 if (ret == -EOVERFLOW) {
1295 uuids_temp = realloc(uuids, obdcount *
1296 sizeof(struct obd_uuid));
1297 if (uuids_temp != NULL)
1298 goto retry_get_uuids;
1303 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
1307 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
1308 if (param->obdindexes == NULL)
1311 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
1312 for (i = 0; i < obdcount; i++) {
1313 if (llapi_uuid_match(uuids[i].uuid,
1314 param->obduuid[obdnum].uuid)) {
1315 param->obdindexes[obdnum] = i;
1320 if (i >= obdcount) {
1321 param->obdindexes[obdnum] = OBD_NOT_FOUND;
1322 llapi_err_noerrno(LLAPI_MSG_ERROR,
1323 "error: %s: unknown obduuid: %s",
1325 param->obduuid[obdnum].uuid);
1331 param->obdindex = OBD_NOT_FOUND;
1333 param->obdindex = obd_valid;
1335 param->got_uuids = 1;
1341 int llapi_ostlist(char *path, struct find_param *param)
1346 dir = opendir(path);
1350 ret = setup_obd_uuid(dir, path, param);
1356 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1357 int is_dir, int verbose, int depth,
1360 char *prefix = is_dir ? "" : "lmm_";
1361 char nl = is_dir ? ' ' : '\n';
1363 if (is_dir && lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
1364 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
1365 if (verbose & VERBOSE_DETAIL)
1366 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1369 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1370 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1372 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1373 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1375 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
1376 lum->lmm_object_gr);
1377 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1378 lum->lmm_object_id);
1381 if (verbose & VERBOSE_COUNT) {
1382 if (verbose & ~VERBOSE_COUNT)
1383 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1385 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1386 (int)lum->lmm_stripe_count, nl);
1389 if (verbose & VERBOSE_SIZE) {
1390 if (verbose & ~VERBOSE_SIZE)
1391 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1393 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
1397 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1398 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1399 lum->lmm_pattern, nl);
1402 if (verbose & VERBOSE_OFFSET) {
1403 if (verbose & ~VERBOSE_OFFSET)
1404 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1407 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1408 lum->lmm_stripe_offset ==
1409 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
1410 lum->lmm_stripe_offset, nl);
1412 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1413 lum->lmm_objects[0].l_ost_idx, nl);
1416 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1417 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s", pool_name);
1421 if (is_dir && (verbose != VERBOSE_OBJID))
1422 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1425 static void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, int is_dir,
1426 int obdstripe, int obdindex, int header)
1428 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
1431 if (header && (obdstripe == 1))
1432 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
1433 lumj->lmm_extent_count);
1435 if (!is_dir && (header & VERBOSE_OBJID)) {
1436 unsigned long long start = -1, end = 0;
1439 llapi_printf(LLAPI_MSG_NORMAL,
1440 "joined\tobdidx\t\t objid\t\tobjid\t\t "
1441 "group\t\tstart\t\tend\n");
1442 for (i = 0; i < lumj->lmm_stripe_count; i++) {
1443 int idx = lumj->lmm_objects[i].l_ost_idx;
1444 long long oid = lumj->lmm_objects[i].l_object_id;
1445 long long gr = lumj->lmm_objects[i].l_object_gr;
1446 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
1447 llapi_printf(LLAPI_MSG_NORMAL,
1448 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
1450 obdindex == idx ? " *" : "");
1451 if (start != lumj->lmm_objects[i].l_extent_start ||
1452 end != lumj->lmm_objects[i].l_extent_end) {
1453 start = lumj->lmm_objects[i].l_extent_start;
1454 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
1455 end = lumj->lmm_objects[i].l_extent_end;
1456 if (end == (unsigned long long)-1)
1457 llapi_printf(LLAPI_MSG_NORMAL,
1460 llapi_printf(LLAPI_MSG_NORMAL,
1463 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
1466 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1470 static void lov_dump_user_lmm_v1v3(struct lov_user_md *lum,
1471 struct lov_user_ost_data_v1 *objects,
1472 int is_dir, int obdstripe,
1473 int obdindex,int header)
1477 if (!is_dir && (header & VERBOSE_OBJID)) {
1479 llapi_printf(LLAPI_MSG_NORMAL,
1480 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1482 for (i = 0; i < lum->lmm_stripe_count; i++) {
1483 int idx = objects[i].l_ost_idx;
1484 long long oid = objects[i].l_object_id;
1485 long long gr = objects[i].l_object_gr;
1486 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1487 llapi_printf(LLAPI_MSG_NORMAL,
1488 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1490 obdindex == idx ? " *" : "");
1492 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1496 void llapi_lov_dump_user_lmm(struct find_param *param,
1497 char *path, int is_dir)
1499 int i, obdstripe = (param->obdindex != OBD_NOT_FOUND) ? 0 : 1;
1500 struct lov_user_md *lum = ¶m->lmd->lmd_lmm;
1503 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1504 if (param->obdindex == lum->lmm_objects[i].l_ost_idx) {
1511 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1512 case LOV_USER_MAGIC_V1:
1513 if (param->verbose && (obdstripe == 1))
1514 lov_dump_user_lmm_header(lum, path, is_dir,
1518 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm,
1519 param->lmd->lmd_lmm.lmm_objects,
1524 case LOV_USER_MAGIC_JOIN:
1525 if (param->verbose && (obdstripe == 1))
1526 lov_dump_user_lmm_header(lum, path, is_dir,
1530 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, is_dir,
1531 obdstripe, param->obdindex,
1534 case LOV_USER_MAGIC_V3: {
1535 char pool_name[LOV_MAXPOOLNAME + 1];
1536 struct lov_user_ost_data_v1 *objects;
1537 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1539 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1540 pool_name[LOV_MAXPOOLNAME] = '\0';
1541 objects = lmmv3->lmm_objects;
1542 if (param->verbose && (obdstripe == 1))
1543 lov_dump_user_lmm_header(lum, path, is_dir,
1547 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, objects, is_dir,
1548 obdstripe, param->obdindex,
1553 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1554 "(expecting one of %#x %#x %#x)\n",
1555 param->lmd->lmd_lmm.lmm_magic,
1556 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1562 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1568 fname = strrchr(path, '/');
1570 /* It should be a file (or other non-directory) */
1571 if (fname == NULL) {
1572 dname = (char *)malloc(2);
1576 fname = (char *)path;
1578 dname = (char *)malloc(fname - path + 1);
1581 strncpy(dname, path, fname - path);
1582 dname[fname - path] = '\0';
1586 if ((fd = open(dname, O_RDONLY)) == -1) {
1592 strcpy((char *)lum, fname);
1593 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1596 if (close(fd) == -1 && rc == 0)
1604 int llapi_file_lookup(int dirfd, const char *name)
1606 struct obd_ioctl_data data = { 0 };
1611 if (dirfd < 0 || name == NULL)
1614 data.ioc_version = OBD_IOCTL_VERSION;
1615 data.ioc_len = sizeof(data);
1616 data.ioc_inlbuf1 = (char *)name;
1617 data.ioc_inllen1 = strlen(name) + 1;
1619 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1621 llapi_err(LLAPI_MSG_ERROR,
1622 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1627 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1630 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1631 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1633 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1634 * The table below gives the answers for the specified parameters (value and
1635 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1636 * --------------------------------------
1637 * 1 | file > limit; sign > 0 | -1 / -1 |
1638 * 2 | file = limit; sign > 0 | ? / 1 |
1639 * 3 | file < limit; sign > 0 | ? / 1 |
1640 * 4 | file > limit; sign = 0 | -1 / -1 |
1641 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1642 * 6 | file < limit; sign = 0 | ? / -1 |
1643 * 7 | file > limit; sign < 0 | 1 / 1 |
1644 * 8 | file = limit; sign < 0 | ? / -1 |
1645 * 9 | file < limit; sign < 0 | ? / -1 |
1646 * --------------------------------------
1647 * Note: 5th actually means that the value is within the interval
1648 * (limit - margin, limit]. */
1649 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1650 int negopt, unsigned long long margin, int mds)
1657 } else if (sign == 0) {
1658 if (file <= limit && file + margin >= limit)
1660 else if (file + margin <= limit)
1662 } else if (sign < 0) {
1669 return negopt ? ~ret + 1 : ret;
1672 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1673 * Return -1 or 1 if file timestamp does not or does match the given criteria
1674 * correspondingly. Return 0 if the MDS time is being checked and there are
1675 * attributes on OSTs and it is not yet clear if the timespamp matches.
1677 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1678 * updated timestamps. */
1679 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1684 /* Check if file is accepted. */
1686 ret = find_value_cmp(st->st_atime, param->atime,
1687 param->asign, param->exclude_atime,
1695 ret = find_value_cmp(st->st_mtime, param->mtime,
1696 param->msign, param->exclude_mtime,
1701 /* If the previous check matches, but this one is not yet clear,
1702 * we should return 0 to do an RPC on OSTs. */
1708 ret = find_value_cmp(st->st_ctime, param->ctime,
1709 param->csign, param->exclude_ctime,
1714 /* If the previous check matches, but this one is not yet clear,
1715 * we should return 0 to do an RPC on OSTs. */
1723 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1724 void *data, struct dirent64 *de)
1726 struct find_param *param = (struct find_param *)data;
1727 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1728 int decision = 1; /* 1 is accepted; -1 is rejected. */
1729 lstat_t *st = ¶m->lmd->lmd_st;
1731 int checked_type = 0;
1734 LASSERT(parent != NULL || dir != NULL);
1736 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1738 /* If a regular expression is presented, make the initial decision */
1739 if (param->pattern != NULL) {
1740 char *fname = strrchr(path, '/');
1741 fname = (fname == NULL ? path : fname + 1);
1742 ret = fnmatch(param->pattern, fname, 0);
1743 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1744 (ret == 0 && param->exclude_pattern))
1748 /* See if we can check the file type from the dirent. */
1749 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1750 de->d_type <= DT_MAX) {
1752 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1753 if (param->exclude_type)
1756 if (!param->exclude_type)
1762 /* If a time or OST should be checked, the decision is not taken yet. */
1763 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1768 /* Request MDS for the stat info. */
1769 if (param->have_fileinfo == 0) {
1771 /* retrieve needed file info */
1772 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1773 (void *)param->lmd);
1775 char *fname = strrchr(path, '/');
1776 fname = (fname == NULL ? path : fname + 1);
1778 /* retrieve needed file info */
1779 strncpy((char *)param->lmd, fname, param->lumlen);
1780 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1781 (void *)param->lmd);
1786 if (errno == ENOTTY) {
1787 /* ioctl is not supported, it is not a lustre fs.
1788 * Do the regular lstat(2) instead. */
1790 ret = lstat_f(path, st);
1792 llapi_err(LLAPI_MSG_ERROR,
1793 "error: %s: lstat failed for %s",
1797 } else if (errno == ENOENT) {
1798 llapi_err(LLAPI_MSG_WARN,
1799 "warning: %s: %s does not exist",
1803 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1804 __func__, dir ? "LL_IOC_MDC_GETINFO" :
1805 "IOC_MDC_GETFILEINFO", path);
1810 if (param->check_uid) {
1811 if (st->st_uid == param->uid) {
1812 if (param->exclude_uid)
1815 if (!param->exclude_uid)
1820 if (param->check_gid) {
1821 if (st->st_gid == param->gid) {
1822 if (param->exclude_gid)
1825 if (!param->exclude_gid)
1830 if (param->check_pool) {
1831 /* empty requested pool is taken as no pool search => V1 */
1832 if ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1 &&
1833 param->poolname[0] == '\0') ||
1834 (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3 &&
1835 (strncmp(lmmv3->lmm_pool_name, param->poolname,
1836 LOV_MAXPOOLNAME) == 0 ||
1837 strcmp(param->poolname, "*") == 0))) {
1838 if (param->exclude_pool)
1841 if (!param->exclude_pool)
1846 /* Check the time on mds. */
1850 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1851 param->lmd->lmd_lmm.lmm_stripe_count)
1853 decision = find_time_check(st, param, for_mds);
1858 if (param->type && !checked_type) {
1859 if ((st->st_mode & S_IFMT) == param->type) {
1860 if (param->exclude_type)
1863 if (!param->exclude_type)
1869 if (param->obduuid) {
1870 if (lustre_fs && param->got_uuids &&
1871 param->st_dev != st->st_dev) {
1872 /* A lustre/lustre mount point is crossed. */
1873 param->got_uuids = 0;
1874 param->obds_printed = 0;
1875 param->obdindex = OBD_NOT_FOUND;
1878 if (lustre_fs && !param->got_uuids) {
1879 ret = setup_obd_indexes(dir ? dir : parent, param);
1883 param->st_dev = st->st_dev;
1884 } else if (!lustre_fs && param->got_uuids) {
1885 /* A lustre/non-lustre mount point is crossed. */
1886 param->got_uuids = 0;
1887 param->obdindex = OBD_NOT_FOUND;
1891 /* If an OBD UUID is specified but no one matches, skip this file. */
1892 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1895 /* If a OST UUID is given, and some OST matches, check it here. */
1896 if (param->obdindex != OBD_NOT_FOUND) {
1897 if (!S_ISREG(st->st_mode))
1900 /* Only those files should be accepted, which have a
1901 * stripe on the specified OST. */
1902 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1906 struct lov_user_ost_data_v1 *lmm_objects;
1908 if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3)
1909 lmm_objects = lmmv3->lmm_objects;
1911 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1914 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1915 for (j = 0; j < param->num_obds; j++) {
1916 if (param->obdindexes[j] ==
1917 lmm_objects[i].l_ost_idx) {
1918 if (param->exclude_obd)
1925 if (i == param->lmd->lmd_lmm.lmm_stripe_count) {
1926 if (param->exclude_obd)
1935 /* If file still fits the request, ask osd for updated info.
1936 The regulat stat is almost of the same speed as some new
1937 'glimpse-size-ioctl'. */
1938 if (!decision && S_ISREG(st->st_mode) &&
1939 param->lmd->lmd_lmm.lmm_stripe_count &&
1940 (param->size_check ||param->atime ||param->mtime ||param->ctime)) {
1942 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1943 (void *)param->lmd);
1944 } else if (parent) {
1945 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1946 (void *)param->lmd);
1950 if (errno == ENOENT) {
1951 llapi_err(LLAPI_MSG_ERROR,
1952 "warning: %s: %s does not exist",
1956 llapi_err(LLAPI_MSG_ERROR,
1957 "%s: IOC_LOV_GETINFO on %s failed",
1963 /* Check the time on osc. */
1964 decision = find_time_check(st, param, 0);
1969 if (param->size_check) {
1970 decision = find_value_cmp(st->st_size, param->size,
1971 param->size_sign, param->exclude_size,
1972 param->size_units, 0);
1975 if (decision != -1) {
1976 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1978 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1980 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1984 /* Do not get down anymore? */
1985 if (param->depth == param->maxdepth)
1992 int llapi_find(char *path, struct find_param *param)
1994 return param_callback(path, cb_find_init, cb_common_fini, param);
1997 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1998 struct dirent64 *de)
2000 struct find_param *param = (struct find_param *)data;
2003 LASSERT(parent != NULL || d != NULL);
2005 if (param->obduuid) {
2007 ret = setup_obd_uuid(d ? d : parent, path, param);
2013 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2014 (void *)¶m->lmd->lmd_lmm);
2015 } else if (parent) {
2016 char *fname = strrchr(path, '/');
2017 fname = (fname == NULL ? path : fname + 1);
2019 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2020 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2021 (void *)¶m->lmd->lmd_lmm);
2025 if (errno == ENODATA) {
2026 if (!param->obduuid)
2027 llapi_printf(LLAPI_MSG_NORMAL,
2028 "%s has no stripe info\n", path);
2030 } else if (errno == ENOTTY) {
2031 llapi_err(LLAPI_MSG_ERROR,
2032 "%s: '%s' not on a Lustre fs?",
2034 } else if (errno == ENOENT) {
2035 llapi_err(LLAPI_MSG_WARN,
2036 "warning: %s: %s does not exist",
2040 llapi_err(LLAPI_MSG_ERROR,
2041 "error: %s: %s failed for %s",
2042 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2043 "IOC_MDC_GETFILESTRIPE", path);
2049 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2051 /* Do not get down anymore? */
2052 if (param->depth == param->maxdepth)
2059 int llapi_getstripe(char *path, struct find_param *param)
2061 return param_callback(path, cb_getstripe, cb_common_fini, param);
2064 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2065 struct obd_statfs *stat_buf,
2066 struct obd_uuid *uuid_buf)
2069 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2071 struct obd_ioctl_data data = { 0 };
2074 data.ioc_inlbuf1 = (char *)&type;
2075 data.ioc_inllen1 = sizeof(__u32);
2076 data.ioc_inlbuf2 = (char *)&index;
2077 data.ioc_inllen2 = sizeof(__u32);
2078 data.ioc_pbuf1 = (char *)stat_buf;
2079 data.ioc_plen1 = sizeof(struct obd_statfs);
2080 data.ioc_pbuf2 = (char *)uuid_buf;
2081 data.ioc_plen2 = sizeof(struct obd_uuid);
2083 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
2084 llapi_err(LLAPI_MSG_ERROR,
2085 "llapi_obd_statfs: error packing ioctl data");
2089 fd = open(path, O_RDONLY);
2090 if (errno == EISDIR)
2091 fd = open(path, O_DIRECTORY | O_RDONLY);
2094 rc = errno ? -errno : -EBADF;
2095 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2099 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2101 rc = errno ? -errno : -EINVAL;
2107 #define MAX_STRING_SIZE 128
2108 #define DEVICES_LIST "/proc/fs/lustre/devices"
2110 int llapi_ping(char *obd_type, char *obd_name)
2112 char path[MAX_STRING_SIZE];
2116 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2117 obd_type, obd_name);
2119 fd = open(path, O_WRONLY);
2122 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2126 rc = write(fd, buf, 1);
2134 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2136 char buf[MAX_STRING_SIZE];
2137 FILE *fp = fopen(DEVICES_LIST, "r");
2142 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2146 while (fgets(buf, sizeof(buf), fp) != NULL) {
2147 char *obd_type_name = NULL;
2148 char *obd_name = NULL;
2149 char *obd_uuid = NULL;
2150 char rawbuf[OBD_MAX_IOCTL_BUFFER];
2151 char *bufl = rawbuf;
2153 struct obd_ioctl_data datal = { 0, };
2154 struct obd_statfs osfs_buffer;
2156 while(bufp[0] == ' ')
2159 for(i = 0; i < 3; i++) {
2160 obd_type_name = strsep(&bufp, " ");
2162 obd_name = strsep(&bufp, " ");
2163 obd_uuid = strsep(&bufp, " ");
2165 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2167 memset(bufl, 0, sizeof(rawbuf));
2168 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2169 datal.ioc_plen1 = sizeof(osfs_buffer);
2171 for (i = 0; i < type_num; i++) {
2172 if (strcmp(obd_type_name, obd_type[i]) != 0)
2175 cb(obd_type_name, obd_name, obd_uuid, args);
2182 static void do_target_check(char *obd_type_name, char *obd_name,
2183 char *obd_uuid, void *args)
2187 rc = llapi_ping(obd_type_name, obd_name);
2188 if (rc == ENOTCONN) {
2189 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2191 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2193 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2197 int llapi_target_check(int type_num, char **obd_type, char *dir)
2199 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2202 #undef MAX_STRING_SIZE
2204 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2206 char raw[OBD_MAX_IOCTL_BUFFER];
2207 char out[LLOG_CHUNK_SIZE];
2209 struct obd_ioctl_data data = { 0 };
2214 sprintf(key, "%s", keyword);
2215 memset(raw, 0, sizeof(raw));
2216 memset(out, 0, sizeof(out));
2217 data.ioc_inlbuf1 = key;
2218 data.ioc_inllen1 = strlen(key) + 1;
2220 data.ioc_inlbuf2 = node_name;
2221 data.ioc_inllen2 = strlen(node_name) + 1;
2223 data.ioc_pbuf1 = out;
2224 data.ioc_plen1 = sizeof(out);
2225 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2229 root = opendir(dir);
2232 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2236 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2238 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2240 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2246 /* Is this a lustre fs? */
2247 int llapi_is_lustre_mnttype(const char *type)
2249 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2252 /* Is this a lustre client fs? */
2253 int llapi_is_lustre_mnt(struct mntent *mnt)
2255 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2256 strstr(mnt->mnt_fsname, ":/") != NULL);
2259 int llapi_quotacheck(char *mnt, int check_type)
2264 root = opendir(mnt);
2266 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2270 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2276 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2282 root = opendir(mnt);
2284 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2289 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2293 if (poll_intvl < 30)
2301 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2306 root = opendir(mnt);
2308 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2312 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2318 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2319 struct dirent64 *de)
2321 struct find_param *param = (struct find_param *)data;
2325 LASSERT(parent != NULL || d != NULL);
2328 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2329 (void *)param->lmd);
2330 } else if (parent) {
2331 char *fname = strrchr(path, '/');
2332 fname = (fname == NULL ? path : fname + 1);
2334 strncpy((char *)param->lmd, fname, param->lumlen);
2335 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2336 (void *)param->lmd);
2342 if (errno == ENODATA) {
2343 if (!param->obduuid && !param->quiet)
2344 llapi_err(LLAPI_MSG_ERROR,
2345 "%s has no stripe info", path);
2347 } else if (errno == ENOENT) {
2348 llapi_err(LLAPI_MSG_ERROR,
2349 "warning: %s: %s does not exist",
2352 } else if (errno != EISDIR) {
2354 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2355 d ? "LL_IOC_MDC_GETINFO" :
2356 "IOC_MDC_GETFILEINFO", path);
2361 st = ¶m->lmd->lmd_st;
2363 /* libc chown() will do extra check, and if the real owner is
2364 * the same as the ones to set, it won't fall into kernel, so
2365 * invoke syscall directly. */
2366 rc = syscall(SYS_chown, path, -1, -1);
2368 llapi_err(LLAPI_MSG_ERROR, "error: chown %s", path);
2370 rc = chmod(path, st->st_mode);
2372 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2378 int llapi_quotachown(char *path, int flag)
2380 struct find_param param;
2382 memset(¶m, 0, sizeof(param));
2383 param.recursive = 1;
2387 return param_callback(path, cb_quotachown, NULL, ¶m);
2390 int llapi_path2fid(const char *path, lustre_fid *fid)
2394 fd = open(path, O_RDONLY);
2398 rc = ioctl(fd, LL_IOC_PATH2FID, fid);
2404 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
2409 root = opendir(mnt);
2411 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2415 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
2418 llapi_err(LLAPI_MSG_ERROR,
2419 "ioctl on %s for getting connect flags failed", mnt);