1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
52 #include <sys/ioctl.h>
59 #include <sys/types.h>
60 #include <sys/syscall.h>
61 #include <sys/xattr.h>
64 #ifdef HAVE_LINUX_UNISTD_H
65 #include <linux/unistd.h>
71 #include <liblustre.h>
72 #include <lnet/lnetctl.h>
74 #include <lustre_lib.h>
76 #include <lustre/liblustreapi.h>
78 static unsigned llapi_dir_filetype_table[] = {
87 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 static const int DT_MAX = DT_DOOR;
95 static const int DT_MAX = DT_SOCK;
98 static unsigned llapi_filetype_dir_table[] = {
107 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 #if defined(DT_DOOR) && defined(S_IFDOOR)
113 static const int S_IFMAX = DT_DOOR;
115 static const int S_IFMAX = DT_SOCK;
118 /* liblustreapi message level */
119 static int llapi_msg_level = LLAPI_MSG_MAX;
121 void llapi_msg_set_level(int level)
123 /* ensure level is in the good range */
124 if (level < LLAPI_MSG_OFF)
125 llapi_msg_level = LLAPI_MSG_OFF;
126 else if (level > LLAPI_MSG_MAX)
127 llapi_msg_level = LLAPI_MSG_MAX;
129 llapi_msg_level = level;
132 void llapi_err(int level, char *fmt, ...)
135 int tmp_errno = abs(errno);
137 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
141 vfprintf(stderr, fmt, args);
144 if (level & LLAPI_MSG_NO_ERRNO)
145 fprintf(stderr, "\n");
147 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
150 #define llapi_err_noerrno(level, fmt, a...) \
151 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
153 void llapi_printf(int level, char *fmt, ...)
157 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
161 vfprintf(stdout, fmt, args);
166 * size_units is unchanged if no specifier used
168 int parse_size(char *optarg, unsigned long long *size,
169 unsigned long long *size_units, int bytes_spec)
173 *size = strtoull(optarg, &end, 0);
176 if ((*end == 'b') && *(end+1) == '\0' &&
177 (*size & (~0ULL << (64 - 9))) == 0 &&
180 *size_units = 1 << 9;
181 } else if ((*end == 'b') && *(end+1) == '\0' &&
184 } else if ((*end == 'k' || *end == 'K') &&
185 *(end+1) == '\0' && (*size &
186 (~0ULL << (64 - 10))) == 0) {
188 *size_units = 1 << 10;
189 } else if ((*end == 'm' || *end == 'M') &&
190 *(end+1) == '\0' && (*size &
191 (~0ULL << (64 - 20))) == 0) {
193 *size_units = 1 << 20;
194 } else if ((*end == 'g' || *end == 'G') &&
195 *(end+1) == '\0' && (*size &
196 (~0ULL << (64 - 30))) == 0) {
198 *size_units = 1 << 30;
199 } else if ((*end == 't' || *end == 'T') &&
200 *(end+1) == '\0' && (*size &
201 (~0ULL << (64 - 40))) == 0) {
203 *size_units = 1ULL << 40;
204 } else if ((*end == 'p' || *end == 'P') &&
205 *(end+1) == '\0' && (*size &
206 (~0ULL << (64 - 50))) == 0) {
208 *size_units = 1ULL << 50;
209 } else if ((*end == 'e' || *end == 'E') &&
210 *(end+1) == '\0' && (*size &
211 (~0ULL << (64 - 60))) == 0) {
213 *size_units = 1ULL << 60;
222 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
223 int stripe_count, int stripe_pattern)
227 /* 64 KB is the largest common page size I'm aware of (on ia64), but
228 * check the local page size just in case. */
229 page_size = LOV_MIN_STRIPE_SIZE;
230 if (getpagesize() > page_size) {
231 page_size = getpagesize();
232 llapi_err_noerrno(LLAPI_MSG_WARN,
233 "warning: your page size (%u) is "
234 "larger than expected (%u)", page_size,
235 LOV_MIN_STRIPE_SIZE);
237 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
238 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
239 "must be an even multiple of %d bytes",
240 stripe_size, page_size);
243 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
245 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
249 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
251 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
255 if (stripe_size >= (1ULL << 32)){
257 llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
258 " is not currently supported and would wrap");
264 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
266 int llapi_file_open_pool(const char *name, int flags, int mode,
267 unsigned long long stripe_size, int stripe_offset,
268 int stripe_count, int stripe_pattern, char *pool_name)
270 struct lov_user_md_v3 lum = { 0 };
273 char fsname[MAX_OBD_NAME + 1], *ptr;
275 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
276 if (fd < 0 && errno == EISDIR) {
277 fd = open(name, O_DIRECTORY | O_RDONLY);
283 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
287 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
288 stripe_count, stripe_pattern)) != 0){
293 /* Initialize IOCTL striping pattern structure */
294 lum.lmm_magic = LOV_USER_MAGIC_V3;
295 lum.lmm_pattern = stripe_pattern;
296 lum.lmm_stripe_size = stripe_size;
297 lum.lmm_stripe_count = stripe_count;
298 lum.lmm_stripe_offset = stripe_offset;
300 /* in case user give the full pool name <fsname>.<poolname>, skip
302 if (pool_name != NULL) {
303 ptr = strchr(pool_name, '.');
305 strncpy(fsname, pool_name, ptr - pool_name);
307 /* if fsname matches a filesystem skip it
308 * if not keep the poolname as is */
309 if (poolpath(fsname, NULL, NULL) == 0)
312 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
314 /* If no pool is specified at all, use V1 request */
315 lum.lmm_magic = LOV_USER_MAGIC_V1;
318 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
319 char *errmsg = "stripe already set";
321 if (errno != EEXIST && errno != EALREADY)
322 errmsg = strerror(errno);
324 llapi_err_noerrno(LLAPI_MSG_ERROR,
325 "error on ioctl "LPX64" for '%s' (%d): %s",
326 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
337 int llapi_file_open(const char *name, int flags, int mode,
338 unsigned long long stripe_size, int stripe_offset,
339 int stripe_count, int stripe_pattern)
341 return llapi_file_open_pool(name, flags, mode, stripe_size,
342 stripe_offset, stripe_count,
343 stripe_pattern, NULL);
346 int llapi_file_create(const char *name, unsigned long long stripe_size,
347 int stripe_offset, int stripe_count, int stripe_pattern)
351 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
352 stripe_offset, stripe_count, stripe_pattern,
361 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
362 int stripe_offset, int stripe_count,
363 int stripe_pattern, char *pool_name)
367 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
368 stripe_offset, stripe_count, stripe_pattern,
378 * Find the fsname, the full path, and/or an open fd.
379 * Either the fsname or path must not be NULL
381 #define WANT_PATH 0x1
382 #define WANT_FSNAME 0x2
384 #define WANT_INDEX 0x8
385 #define WANT_ERROR 0x10
386 static int get_root_path(int want, char *fsname, int *outfd, char *path,
390 char buf[PATH_MAX], mntdir[PATH_MAX];
391 char *name = NULL, *ptr;
393 int idx = 0, len = 0, mntlen, fd;
396 /* get the mount point */
397 fp = setmntent(MOUNTED, "r");
399 llapi_err(LLAPI_MSG_ERROR,
400 "setmntent(%s) failed: %s:", MOUNTED,
405 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
408 if (!llapi_is_lustre_mnt(&mnt))
411 mntlen = strlen(mnt.mnt_dir);
412 ptr = strrchr(mnt.mnt_fsname, '/');
419 if ((want & WANT_INDEX) && (idx++ != index))
422 /* Check the fsname for a match */
423 if (!(want & WANT_FSNAME) && fsname != NULL &&
424 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
427 /* If the path isn't set return the first one we find */
428 if (path == NULL || strlen(path) == 0) {
429 strcpy(mntdir, mnt.mnt_dir);
433 /* Otherwise find the longest matching path */
434 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
435 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
436 strcpy(mntdir, mnt.mnt_dir);
446 if ((want & WANT_FSNAME) && fsname != NULL)
447 strcpy(fsname, name);
448 if ((want & WANT_PATH) && path != NULL)
449 strcpy(path, mntdir);
450 if (want & WANT_FD) {
451 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
459 } else if (want & WANT_ERROR)
460 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
461 "can't find fs root for '%s': %d",
462 (want & WANT_PATH) ? fsname : path, rc);
467 * search lustre mounts
469 * Calling this function will return to the user the mount point, mntdir, and
470 * the file system name, fsname, if the user passed a buffer to this routine.
472 * The user inputs are pathname and index. If the pathname is supplied then
473 * the value of the index will be ignored. The pathname will return data if
474 * the pathname is located on a lustre mount. Index is used to pick which
475 * mount point you want in the case of multiple mounted lustre file systems.
476 * See function lfs_osts in lfs.c for a example of the index use.
478 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
481 int want = WANT_PATH, idx = -1;
487 strcpy(mntdir, pathname);
491 return get_root_path(want, fsname, NULL, mntdir, idx);
494 int llapi_search_fsname(const char *pathname, char *fsname)
496 return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
497 (char *)pathname, -1);
500 /* return the first file matching this pattern */
501 static int first_match(char *pattern, char *buffer)
505 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
508 if (glob_info.gl_pathc < 1) {
509 globfree(&glob_info);
513 strcpy(buffer, glob_info.gl_pathv[0]);
515 globfree(&glob_info);
520 * find the pool directory path under /proc
521 * (can be also used to test if a fsname is known)
523 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
526 char pattern[PATH_MAX + 1];
527 char buffer[PATH_MAX];
529 if (fsname == NULL) {
530 rc = llapi_search_fsname(pathname, buffer);
534 strcpy(pathname, fsname);
537 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
538 rc = first_match(pattern, buffer);
542 /* in fsname test mode, pool_pathname is NULL */
543 if (pool_pathname != NULL)
544 strcpy(pool_pathname, buffer);
550 * Get the list of pool members.
551 * \param poolname string of format <fsname>.<poolname>
552 * \param members caller-allocated array of char*
553 * \param list_size size of the members array
554 * \param buffer caller-allocated buffer for storing OST names
555 * \param buffer_size size of the buffer
557 * \return number of members retrieved for this pool
558 * \retval -error failure
560 int llapi_get_poolmembers(const char *poolname, char **members,
561 int list_size, char *buffer, int buffer_size)
563 char fsname[PATH_MAX + 1];
565 char pathname[PATH_MAX + 1];
566 char path[PATH_MAX + 1];
573 /* name is FSNAME.POOLNAME */
574 if (strlen(poolname) > PATH_MAX)
576 strcpy(fsname, poolname);
577 pool = strchr(fsname, '.');
584 rc = poolpath(fsname, NULL, pathname);
587 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
592 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
593 sprintf(path, "%s/%s", pathname, pool);
594 if ((fd = fopen(path, "r")) == NULL) {
595 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path);
600 while (fgets(buf, sizeof(buf), fd) != NULL) {
601 if (nb_entries >= list_size) {
606 if ((tmp = strchr(buf, '\n')) != NULL)
608 if (used + strlen(buf) + 1 > buffer_size) {
613 strcpy(buffer + used, buf);
614 members[nb_entries] = buffer + used;
615 used += strlen(buf) + 1;
625 * Get the list of pools in a filesystem.
626 * \param name filesystem name or path
627 * \param poollist caller-allocated array of char*
628 * \param list_size size of the poollist array
629 * \param buffer caller-allocated buffer for storing pool names
630 * \param buffer_size size of the buffer
632 * \return number of pools retrieved for this filesystem
633 * \retval -error failure
635 int llapi_get_poollist(const char *name, char **poollist, int list_size,
636 char *buffer, int buffer_size)
638 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
642 struct dirent *cookie = NULL;
644 unsigned int nb_entries = 0;
645 unsigned int used = 0;
648 /* initilize output array */
649 for (i = 0; i < list_size; i++)
652 /* is name a pathname ? */
653 ptr = strchr(name, '/');
655 /* only absolute pathname is supported */
658 if (!realpath(name, rname)) {
660 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
664 rc = poolpath(NULL, rname, pathname);
667 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
668 " a Lustre filesystem", name);
671 strcpy(fsname, rname);
674 strcpy(fsname, name);
675 rc = poolpath(fsname, NULL, pathname);
679 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
684 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
685 if ((dir = opendir(pathname)) == NULL) {
686 llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'",
692 rc = readdir_r(dir, &pool, &cookie);
695 llapi_err(LLAPI_MSG_ERROR,
696 "Error reading pool list for '%s'", name);
698 } else if ((rc == 0) && (cookie == NULL))
699 /* end of directory */
702 /* ignore . and .. */
703 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
706 /* check output bounds */
707 if (nb_entries >= list_size)
710 /* +2 for '.' and final '\0' */
711 if (used + strlen(pool.d_name) + strlen(fsname) + 2
715 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
716 poollist[nb_entries] = buffer + used;
717 used += strlen(pool.d_name) + strlen(fsname) + 2;
725 /* wrapper for lfs.c and obd.c */
726 int llapi_poollist(const char *name)
728 /* list of pool names (assume that pool count is smaller
730 char *list[FIND_MAX_OSTS];
732 /* fsname-OST0000_UUID < 32 char, 1 per OST */
733 int bufsize = FIND_MAX_OSTS * 32;
736 buffer = malloc(bufsize);
740 if ((name[0] == '/') || (strchr(name, '.') == NULL))
741 /* name is a path or fsname */
742 nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer,
745 /* name is a pool name (<fsname>.<poolname>) */
746 nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer,
749 for (i = 0; i < nb; i++)
750 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
753 return (nb < 0 ? nb : 0);
757 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
758 void *data, cfs_dirent_t *de);
760 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
761 #define OBD_NOT_FOUND (-1)
763 static int common_param_init(struct find_param *param)
765 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
766 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
767 llapi_err(LLAPI_MSG_ERROR,
768 "error: allocation of %d bytes for ioctl",
769 sizeof(lstat_t) + param->lumlen);
773 param->got_uuids = 0;
774 param->obdindexes = NULL;
775 param->obdindex = OBD_NOT_FOUND;
779 static void find_param_fini(struct find_param *param)
781 if (param->obdindexes)
782 free(param->obdindexes);
788 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
790 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
793 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
798 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
802 fd = open(path, O_RDONLY);
805 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
809 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
817 * If uuidp is NULL, return the number of available obd uuids.
818 * If uuidp is non-NULL, then it will return the uuids of the obds. If
819 * there are more OSTs then allocated to uuidp, then an error is returned with
820 * the ost_count set to number of available obd uuids.
822 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
824 struct obd_uuid lov_name;
827 int rc = 0, index = 0;
829 /* Get the lov name */
830 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
834 /* Now get the ost uuids from /proc */
835 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
837 fp = fopen(buf, "r");
840 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
844 while (fgets(buf, sizeof(buf), fp) != NULL) {
845 if (uuidp && (index < *ost_count)) {
846 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
854 if (uuidp && (index >= *ost_count))
861 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
868 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
873 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
879 /* Here, param->obduuid points to a single obduuid, the index of which is
880 * returned in param->obdindex */
881 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
883 struct obd_uuid lov_uuid;
884 char uuid[sizeof(struct obd_uuid)];
889 /* Get the lov name */
890 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
892 if (errno != ENOTTY) {
894 llapi_err(LLAPI_MSG_ERROR,
895 "error: can't get lov name: %s", dname);
902 param->got_uuids = 1;
904 /* Now get the ost uuids from /proc */
905 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
907 fp = fopen(buf, "r");
910 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
914 if (!param->obduuid && !param->quiet && !param->obds_printed)
915 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
917 while (fgets(buf, sizeof(buf), fp) != NULL) {
918 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
921 if (param->obduuid) {
922 if (strncmp(param->obduuid->uuid, uuid,
923 sizeof(uuid)) == 0) {
924 param->obdindex = index;
927 } else if (!param->quiet && !param->obds_printed) {
928 /* Print everything */
929 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
932 param->obds_printed = 1;
936 if (!param->quiet && param->obduuid &&
937 (param->obdindex == OBD_NOT_FOUND)) {
938 llapi_err_noerrno(LLAPI_MSG_ERROR,
939 "error: %s: unknown obduuid: %s",
940 __FUNCTION__, param->obduuid->uuid);
947 /* In this case, param->obduuid will be an array of obduuids and
948 * obd index for all these obduuids will be returned in
949 * param->obdindexes */
950 static int setup_obd_indexes(DIR *dir, struct find_param *param)
952 struct obd_uuid *uuids = NULL;
953 int obdcount = INIT_ALLOC_NUM_OSTS;
954 int ret, obd_valid = 0, obdnum, i;
956 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
957 sizeof(struct obd_uuid));
962 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
965 struct obd_uuid *uuids_temp;
967 if (ret == -EOVERFLOW) {
968 uuids_temp = realloc(uuids, obdcount *
969 sizeof(struct obd_uuid));
970 if (uuids_temp != NULL)
971 goto retry_get_uuids;
976 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
980 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
981 if (param->obdindexes == NULL)
984 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
985 for (i = 0; i <= obdcount; i++) {
986 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
987 (char *)&uuids[i]) == 0) {
988 param->obdindexes[obdnum] = i;
994 param->obdindexes[obdnum] = OBD_NOT_FOUND;
998 param->obdindex = OBD_NOT_FOUND;
1000 param->obdindex = obd_valid;
1002 param->got_uuids = 1;
1007 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1008 int is_dir, int verbose, int quiet,
1011 char *prefix = is_dir ? "" : "lmm_";
1012 char nl = is_dir ? ' ' : '\n';
1014 if (verbose && path)
1015 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1017 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1018 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1020 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
1021 lum->lmm_object_gr);
1022 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1023 lum->lmm_object_id);
1026 if (verbose & VERBOSE_COUNT) {
1028 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1030 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1031 (int)lum->lmm_stripe_count, nl);
1034 if (verbose & VERBOSE_SIZE) {
1036 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1038 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
1042 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1043 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1044 lum->lmm_pattern, nl);
1047 if (verbose & VERBOSE_OFFSET) {
1049 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1051 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1052 lum->lmm_objects[0].l_ost_idx, nl);
1055 if ((verbose & VERBOSE_POOL) && (pool_name != NULL))
1056 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s%c", pool_name, nl);
1059 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1062 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1063 struct lov_user_ost_data_v1 *objects,
1064 char *path, int is_dir,
1065 int obdindex, int quiet, int header, int body)
1067 int i, obdstripe = 0;
1069 if (obdindex != OBD_NOT_FOUND) {
1070 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1071 if (obdindex == objects[i].l_ost_idx) {
1072 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1079 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1083 /* if it's a directory */
1085 if (obdstripe == 1) {
1086 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
1087 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1088 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
1090 /* maintain original behavior */
1092 header |= VERBOSE_ALL;
1093 lov_dump_user_lmm_header(lum, path, is_dir, header,
1099 if (header && (obdstripe == 1))
1100 lov_dump_user_lmm_header(lum, NULL, is_dir, header, quiet,
1104 if ((!quiet) && (obdstripe == 1))
1105 llapi_printf(LLAPI_MSG_NORMAL,
1106 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1108 for (i = 0; i < lum->lmm_stripe_count; i++) {
1109 int idx = objects[i].l_ost_idx;
1110 long long oid = objects[i].l_object_id;
1111 long long gr = objects[i].l_object_gr;
1112 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1113 llapi_printf(LLAPI_MSG_NORMAL,
1114 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1116 obdindex == idx ? " *" : "");
1118 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1122 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
1123 int is_dir, int obdindex, int quiet,
1124 int header, int body)
1126 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
1127 int i, obdstripe = 0;
1129 if (obdindex != OBD_NOT_FOUND) {
1130 for (i = 0; i < lumj->lmm_stripe_count; i++) {
1131 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
1132 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1139 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1143 if (header && obdstripe == 1) {
1144 lov_dump_user_lmm_header(lum, NULL, 0, header, quiet, NULL);
1146 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
1147 lumj->lmm_extent_count);
1151 unsigned long long start = -1, end = 0;
1152 if (!quiet && obdstripe == 1)
1153 llapi_printf(LLAPI_MSG_NORMAL,
1154 "joined\tobdidx\t\t objid\t\tobjid\t\t "
1155 "group\t\tstart\t\tend\n");
1156 for (i = 0; i < lumj->lmm_stripe_count; i++) {
1157 int idx = lumj->lmm_objects[i].l_ost_idx;
1158 long long oid = lumj->lmm_objects[i].l_object_id;
1159 long long gr = lumj->lmm_objects[i].l_object_gr;
1160 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
1161 llapi_printf(LLAPI_MSG_NORMAL,
1162 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
1164 obdindex == idx ? " *" : "");
1165 if (start != lumj->lmm_objects[i].l_extent_start ||
1166 end != lumj->lmm_objects[i].l_extent_end) {
1167 start = lumj->lmm_objects[i].l_extent_start;
1168 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
1169 end = lumj->lmm_objects[i].l_extent_end;
1170 if (end == (unsigned long long)-1)
1171 llapi_printf(LLAPI_MSG_NORMAL,
1174 llapi_printf(LLAPI_MSG_NORMAL,
1177 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
1180 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1184 void llapi_lov_dump_user_lmm(struct find_param *param,
1185 char *path, int is_dir)
1187 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1188 case LOV_USER_MAGIC_V1:
1189 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1190 param->lmd->lmd_lmm.lmm_objects,
1192 param->obdindex, param->quiet,
1194 (param->verbose || !param->obduuid));
1196 case LOV_USER_MAGIC_JOIN:
1197 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1198 param->obdindex, param->quiet,
1200 (param->verbose || !param->obduuid));
1202 case LOV_USER_MAGIC_V3: {
1203 char pool_name[LOV_MAXPOOLNAME + 1];
1204 struct lov_user_ost_data_v1 *objects;
1205 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1207 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1208 pool_name[LOV_MAXPOOLNAME] = '\0';
1209 objects = lmmv3->lmm_objects;
1210 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1211 objects, path, is_dir,
1212 param->obdindex, param->quiet,
1214 (param->verbose || !param->obduuid));
1218 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1219 "(expecting one of %#x %#x %#x)\n",
1220 *(__u32 *)¶m->lmd->lmd_lmm,
1221 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1227 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1233 fname = strrchr(path, '/');
1235 /* It should be a file (or other non-directory) */
1236 if (fname == NULL) {
1237 dname = (char *)malloc(2);
1241 fname = (char *)path;
1243 dname = (char *)malloc(fname - path + 1);
1246 strncpy(dname, path, fname - path);
1247 dname[fname - path] = '\0';
1251 if ((fd = open(dname, O_RDONLY)) == -1) {
1257 strcpy((char *)lum, fname);
1258 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1261 if (close(fd) == -1 && rc == 0)
1269 int llapi_file_lookup(int dirfd, const char *name)
1271 struct obd_ioctl_data data = { 0 };
1276 if (dirfd < 0 || name == NULL)
1279 data.ioc_version = OBD_IOCTL_VERSION;
1280 data.ioc_len = sizeof(data);
1281 data.ioc_inlbuf1 = (char *)name;
1282 data.ioc_inllen1 = strlen(name) + 1;
1284 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1286 llapi_err(LLAPI_MSG_ERROR,
1287 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1292 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1295 int llapi_mds_getfileinfo(char *path, DIR *parent,
1296 struct lov_user_mds_data *lmd)
1298 lstat_t *st = &lmd->lmd_st;
1299 char *fname = strrchr(path, '/');
1305 fname = (fname == NULL ? path : fname + 1);
1306 /* retrieve needed file info */
1307 strncpy((char *)lmd, fname,
1308 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1309 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1312 if (errno == ENOTTY) {
1313 /* ioctl is not supported, it is not a lustre fs.
1314 * Do the regular lstat(2) instead. */
1315 ret = lstat_f(path, st);
1317 llapi_err(LLAPI_MSG_ERROR,
1318 "error: %s: lstat failed for %s",
1319 __FUNCTION__, path);
1322 } else if (errno == ENOENT) {
1323 llapi_err(LLAPI_MSG_WARN,
1324 "warning: %s: %s does not exist",
1325 __FUNCTION__, path);
1328 llapi_err(LLAPI_MSG_ERROR,
1329 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1330 __FUNCTION__, path);
1338 static DIR *opendir_parent(char *path)
1344 fname = strrchr(path, '/');
1346 return opendir(".");
1350 parent = opendir(path);
1355 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1356 semantic_func_t sem_init,
1357 semantic_func_t sem_fini, void *data,
1368 if (!d && errno != ENOTDIR) {
1369 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1370 __FUNCTION__, path);
1372 } else if (!d && !parent) {
1373 /* ENOTDIR. Open the parent dir. */
1374 p = opendir_parent(path);
1376 GOTO(out, ret = -EINVAL);
1379 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1385 while ((dent = readdir64(d)) != NULL) {
1386 ((struct find_param *)data)->have_fileinfo = 0;
1388 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1391 /* Don't traverse .lustre directory */
1392 if (!(strcmp(dent->d_name, dot_lustre_name)))
1396 if ((len + dent->d_reclen + 2) > size) {
1397 llapi_err(LLAPI_MSG_ERROR,
1398 "error: %s: string buffer is too small",
1403 strcat(path, dent->d_name);
1405 if (dent->d_type == DT_UNKNOWN) {
1406 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1408 ret = llapi_mds_getfileinfo(path, d,
1409 ((struct find_param *)data)->lmd);
1411 ((struct find_param *)data)->have_fileinfo = 1;
1413 llapi_filetype_dir_table[st->st_mode &
1420 switch (dent->d_type) {
1422 llapi_err(LLAPI_MSG_ERROR,
1423 "error: %s: '%s' is UNKNOWN type %d",
1424 __FUNCTION__, dent->d_name, dent->d_type);
1427 ret = llapi_semantic_traverse(path, size, d, sem_init,
1428 sem_fini, data, dent);
1435 ret = sem_init(path, d, NULL, data, dent);
1439 if (sem_fini && ret == 0)
1440 sem_fini(path, d, NULL, data, dent);
1448 sem_fini(path, parent, d, data, de);
1457 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1458 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1460 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1461 * The table below gives the answers for the specified parameters (value and
1462 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1463 * --------------------------------------
1464 * 1 | file > limit; sign > 0 | -1 / -1 |
1465 * 2 | file = limit; sign > 0 | ? / 1 |
1466 * 3 | file < limit; sign > 0 | ? / 1 |
1467 * 4 | file > limit; sign = 0 | -1 / -1 |
1468 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1469 * 6 | file < limit; sign = 0 | ? / -1 |
1470 * 7 | file > limit; sign < 0 | 1 / 1 |
1471 * 8 | file = limit; sign < 0 | ? / -1 |
1472 * 9 | file < limit; sign < 0 | ? / -1 |
1473 * --------------------------------------
1474 * Note: 5th actually means that the value is within the interval
1475 * (limit - margin, limit]. */
1476 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1477 unsigned long long margin, int mds)
1485 if (file <= limit && file + margin > limit)
1487 if (file + margin <= limit)
1488 return mds ? 0 : -1;
1501 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1502 * Return -1 or 1 if file timestamp does not or does match the given criteria
1503 * correspondingly. Return 0 if the MDS time is being checked and there are
1504 * attributes on OSTs and it is not yet clear if the timespamp matches.
1506 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1507 * updated timestamps. */
1508 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1513 /* Check if file is accepted. */
1515 ret = find_value_cmp(st->st_atime, param->atime,
1516 param->asign, 24 * 60 * 60, mds);
1523 ret = find_value_cmp(st->st_mtime, param->mtime,
1524 param->msign, 24 * 60 * 60, mds);
1528 /* If the previous check matches, but this one is not yet clear,
1529 * we should return 0 to do an RPC on OSTs. */
1535 ret = find_value_cmp(st->st_ctime, param->ctime,
1536 param->csign, 24 * 60 * 60, mds);
1540 /* If the previous check matches, but this one is not yet clear,
1541 * we should return 0 to do an RPC on OSTs. */
1549 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1550 void *data, cfs_dirent_t *de)
1552 struct find_param *param = (struct find_param *)data;
1553 int decision = 1; /* 1 is accepted; -1 is rejected. */
1554 lstat_t *st = ¶m->lmd->lmd_st;
1556 int checked_type = 0;
1559 LASSERT(parent != NULL || dir != NULL);
1561 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1563 /* If a regular expression is presented, make the initial decision */
1564 if (param->pattern != NULL) {
1565 char *fname = strrchr(path, '/');
1566 fname = (fname == NULL ? path : fname + 1);
1567 ret = fnmatch(param->pattern, fname, 0);
1568 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1569 (ret == 0 && param->exclude_pattern))
1573 /* See if we can check the file type from the dirent. */
1574 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1575 de->d_type < DT_MAX) {
1577 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1578 if (param->exclude_type)
1581 if (!param->exclude_type)
1587 /* If a time or OST should be checked, the decision is not taken yet. */
1588 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1593 /* Request MDS for the stat info. */
1594 if (param->have_fileinfo == 0) {
1596 /* retrieve needed file info */
1597 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1598 (void *)param->lmd);
1600 char *fname = strrchr(path, '/');
1601 fname = (fname == NULL ? path : fname + 1);
1603 /* retrieve needed file info */
1604 strncpy((char *)param->lmd, fname, param->lumlen);
1605 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1606 (void *)param->lmd);
1611 if (errno == ENOTTY) {
1612 /* ioctl is not supported, it is not a lustre fs.
1613 * Do the regular lstat(2) instead. */
1615 ret = lstat_f(path, st);
1617 llapi_err(LLAPI_MSG_ERROR,
1618 "error: %s: lstat failed for %s",
1619 __FUNCTION__, path);
1622 } else if (errno == ENOENT) {
1623 llapi_err(LLAPI_MSG_WARN,
1624 "warning: %s: %s does not exist",
1625 __FUNCTION__, path);
1628 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1629 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1630 "IOC_MDC_GETFILEINFO", path);
1635 if (param->type && !checked_type) {
1636 if ((st->st_mode & S_IFMT) == param->type) {
1637 if (param->exclude_type)
1640 if (!param->exclude_type)
1646 if (param->obduuid) {
1647 if (lustre_fs && param->got_uuids &&
1648 param->st_dev != st->st_dev) {
1649 /* A lustre/lustre mount point is crossed. */
1650 param->got_uuids = 0;
1651 param->obds_printed = 0;
1652 param->obdindex = OBD_NOT_FOUND;
1655 if (lustre_fs && !param->got_uuids) {
1656 ret = setup_obd_indexes(dir ? dir : parent, param);
1660 param->st_dev = st->st_dev;
1661 } else if (!lustre_fs && param->got_uuids) {
1662 /* A lustre/non-lustre mount point is crossed. */
1663 param->got_uuids = 0;
1664 param->obdindex = OBD_NOT_FOUND;
1668 /* If an OBD UUID is specified but no one matches, skip this file. */
1669 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1672 /* If a OST UUID is given, and some OST matches, check it here. */
1673 if (param->obdindex != OBD_NOT_FOUND) {
1674 if (!S_ISREG(st->st_mode))
1677 /* Only those files should be accepted, which have a
1678 * stripe on the specified OST. */
1679 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1683 struct lov_user_ost_data_v1 *lmm_objects;
1685 if (param->lmd->lmd_lmm.lmm_magic ==
1686 LOV_USER_MAGIC_V3) {
1687 struct lov_user_md_v3 *lmmv3 =
1688 (void *)¶m->lmd->lmd_lmm;
1690 lmm_objects = lmmv3->lmm_objects;
1692 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1696 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1697 for (j = 0; j < param->num_obds; j++) {
1698 if (param->obdindexes[j] ==
1699 lmm_objects[i].l_ost_idx)
1704 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1709 if (param->check_uid) {
1710 if (st->st_uid == param->uid) {
1711 if (param->exclude_uid)
1714 if (!param->exclude_uid)
1719 if (param->check_gid) {
1720 if (st->st_gid == param->gid) {
1721 if (param->exclude_gid)
1724 if (!param->exclude_gid)
1729 if (param->check_pool) {
1730 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1732 /* empty requested pool is taken as no pool search => V1 */
1733 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1734 (param->poolname[0] == '\0')) ||
1735 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1736 (strncmp(lmmv3->lmm_pool_name,
1737 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1738 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1739 (strcmp(param->poolname, "*") == 0))) {
1740 if (param->exclude_pool)
1743 if (!param->exclude_pool)
1748 /* Check the time on mds. */
1752 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1753 param->lmd->lmd_lmm.lmm_stripe_count)
1755 decision = find_time_check(st, param, for_mds);
1759 /* If file still fits the request, ask ost for updated info.
1760 The regular stat is almost of the same speed as some new
1761 'glimpse-size-ioctl'. */
1762 if (!decision && S_ISREG(st->st_mode) &&
1763 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1764 if (param->obdindex != OBD_NOT_FOUND) {
1765 /* Check whether the obd is active or not, if it is
1766 * not active, just print the object affected by this
1769 struct obd_statfs stat_buf;
1770 struct obd_uuid uuid_buf;
1772 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1773 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1774 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1775 param->obdindex, &stat_buf,
1778 if (ret == -ENODATA || ret == -ENODEV
1781 llapi_printf(LLAPI_MSG_NORMAL,
1782 "obd_uuid: %s failed %s ",
1783 param->obduuid->uuid,
1789 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1790 (void *)param->lmd);
1791 } else if (parent) {
1792 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1793 (void *)param->lmd);
1797 if (errno == ENOENT) {
1798 llapi_err(LLAPI_MSG_ERROR,
1799 "warning: %s: %s does not exist",
1800 __FUNCTION__, path);
1803 llapi_err(LLAPI_MSG_ERROR,
1804 "%s: IOC_LOV_GETINFO on %s failed",
1805 __FUNCTION__, path);
1810 /* Check the time on osc. */
1811 decision = find_time_check(st, param, 0);
1817 decision = find_value_cmp(st->st_size, param->size,
1818 param->size_sign, param->size_units,
1822 if (decision != -1) {
1823 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1825 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1827 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1831 /* Do not get down anymore? */
1832 if (param->depth == param->maxdepth)
1839 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1842 struct find_param *param = (struct find_param *)data;
1847 int llapi_find(char *path, struct find_param *param)
1850 int ret, len = strlen(path);
1852 if (len > PATH_MAX) {
1853 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1854 __FUNCTION__, path);
1858 buf = (char *)malloc(PATH_MAX + 1);
1862 ret = common_param_init(param);
1870 strncpy(buf, path, PATH_MAX + 1);
1871 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1872 cb_common_fini, param, NULL);
1874 find_param_fini(param);
1876 return ret < 0 ? ret : 0;
1879 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1882 struct find_param *param = (struct find_param *)data;
1885 LASSERT(parent != NULL || d != NULL);
1888 if (!param->got_uuids) {
1889 ret = setup_obd_uuid(d ? d : parent, path, param);
1895 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1896 (void *)¶m->lmd->lmd_lmm);
1897 } else if (parent) {
1898 char *fname = strrchr(path, '/');
1899 fname = (fname == NULL ? path : fname + 1);
1901 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1902 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1903 (void *)¶m->lmd->lmd_lmm);
1907 if (errno == ENODATA) {
1908 if (!param->obduuid && !param->quiet)
1909 llapi_printf(LLAPI_MSG_NORMAL,
1910 "%s has no stripe info\n", path);
1912 } else if (errno == ENOTTY) {
1913 llapi_err(LLAPI_MSG_ERROR,
1914 "%s: '%s' not on a Lustre fs?",
1915 __FUNCTION__, path);
1916 } else if (errno == ENOENT) {
1917 llapi_err(LLAPI_MSG_WARN,
1918 "warning: %s: %s does not exist",
1919 __FUNCTION__, path);
1922 llapi_err(LLAPI_MSG_ERROR,
1923 "error: %s: %s failed for %s",
1924 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1925 "IOC_MDC_GETFILESTRIPE", path);
1931 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1933 /* Do not get down anymore? */
1934 if (param->depth == param->maxdepth)
1941 int llapi_getstripe(char *path, struct find_param *param)
1944 int ret = 0, len = strlen(path);
1946 if (len > PATH_MAX) {
1947 llapi_err(LLAPI_MSG_ERROR,
1948 "%s: Path name '%s' is too long",
1949 __FUNCTION__, path);
1953 buf = (char *)malloc(PATH_MAX + 1);
1957 ret = common_param_init(param);
1965 strncpy(buf, path, PATH_MAX + 1);
1966 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1967 cb_common_fini, param, NULL);
1968 find_param_fini(param);
1970 return ret < 0 ? ret : 0;
1973 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1974 struct obd_statfs *stat_buf,
1975 struct obd_uuid *uuid_buf)
1978 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1980 struct obd_ioctl_data data = { 0 };
1983 data.ioc_inlbuf1 = (char *)&type;
1984 data.ioc_inllen1 = sizeof(__u32);
1985 data.ioc_inlbuf2 = (char *)&index;
1986 data.ioc_inllen2 = sizeof(__u32);
1987 data.ioc_pbuf1 = (char *)stat_buf;
1988 data.ioc_plen1 = sizeof(struct obd_statfs);
1989 data.ioc_pbuf2 = (char *)uuid_buf;
1990 data.ioc_plen2 = sizeof(struct obd_uuid);
1992 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1993 llapi_err(LLAPI_MSG_ERROR,
1994 "llapi_obd_statfs: error packing ioctl data");
1998 fd = open(path, O_RDONLY);
1999 if (errno == EISDIR)
2000 fd = open(path, O_DIRECTORY | O_RDONLY);
2003 rc = errno ? -errno : -EBADF;
2004 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2005 __FUNCTION__, path);
2008 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2010 rc = errno ? -errno : -EINVAL;
2016 #define MAX_STRING_SIZE 128
2017 #define DEVICES_LIST "/proc/fs/lustre/devices"
2019 int llapi_ping(char *obd_type, char *obd_name)
2021 char path[MAX_STRING_SIZE];
2025 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2026 obd_type, obd_name);
2028 fd = open(path, O_WRONLY);
2031 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2035 rc = write(fd, buf, 1);
2043 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2045 char buf[MAX_STRING_SIZE];
2046 FILE *fp = fopen(DEVICES_LIST, "r");
2051 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2055 while (fgets(buf, sizeof(buf), fp) != NULL) {
2056 char *obd_type_name = NULL;
2057 char *obd_name = NULL;
2058 char *obd_uuid = NULL;
2060 struct obd_ioctl_data datal = { 0, };
2061 struct obd_statfs osfs_buffer;
2063 while(bufp[0] == ' ')
2066 for(i = 0; i < 3; i++) {
2067 obd_type_name = strsep(&bufp, " ");
2069 obd_name = strsep(&bufp, " ");
2070 obd_uuid = strsep(&bufp, " ");
2072 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2074 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2075 datal.ioc_plen1 = sizeof(osfs_buffer);
2077 for (i = 0; i < type_num; i++) {
2078 if (strcmp(obd_type_name, obd_type[i]) != 0)
2081 cb(obd_type_name, obd_name, obd_uuid, args);
2088 static void do_target_check(char *obd_type_name, char *obd_name,
2089 char *obd_uuid, void *args)
2093 rc = llapi_ping(obd_type_name, obd_name);
2094 if (rc == ENOTCONN) {
2095 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2097 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2099 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2103 int llapi_target_check(int type_num, char **obd_type, char *dir)
2105 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2108 #undef MAX_STRING_SIZE
2110 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2112 char raw[OBD_MAX_IOCTL_BUFFER];
2113 char out[LLOG_CHUNK_SIZE];
2115 struct obd_ioctl_data data = { 0 };
2120 sprintf(key, "%s", keyword);
2121 memset(raw, 0, sizeof(raw));
2122 memset(out, 0, sizeof(out));
2123 data.ioc_inlbuf1 = key;
2124 data.ioc_inllen1 = strlen(key) + 1;
2126 data.ioc_inlbuf2 = node_name;
2127 data.ioc_inllen2 = strlen(node_name) + 1;
2129 data.ioc_pbuf1 = out;
2130 data.ioc_plen1 = sizeof(out);
2131 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2135 root = opendir(dir);
2138 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2142 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2144 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2146 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2152 /* Is this a lustre fs? */
2153 int llapi_is_lustre_mnttype(const char *type)
2155 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2158 /* Is this a lustre client fs? */
2159 int llapi_is_lustre_mnt(struct mntent *mnt)
2161 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2162 strstr(mnt->mnt_fsname, ":/") != NULL);
2165 int llapi_quotacheck(char *mnt, int check_type)
2170 root = opendir(mnt);
2172 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2176 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2182 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2188 root = opendir(mnt);
2190 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2195 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2199 if (poll_intvl < 30)
2207 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2212 root = opendir(mnt);
2214 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2218 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2224 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2227 struct find_param *param = (struct find_param *)data;
2231 LASSERT(parent != NULL || d != NULL);
2234 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2235 (void *)param->lmd);
2236 } else if (parent) {
2237 char *fname = strrchr(path, '/');
2238 fname = (fname == NULL ? path : fname + 1);
2240 strncpy((char *)param->lmd, fname, param->lumlen);
2241 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2242 (void *)param->lmd);
2248 if (errno == ENODATA) {
2249 if (!param->obduuid && !param->quiet)
2250 llapi_err(LLAPI_MSG_ERROR,
2251 "%s has no stripe info", path);
2253 } else if (errno == ENOENT) {
2254 llapi_err(LLAPI_MSG_ERROR,
2255 "warning: %s: %s does not exist",
2256 __FUNCTION__, path);
2258 } else if (errno != EISDIR) {
2260 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2261 d ? "LL_IOC_MDC_GETINFO" :
2262 "IOC_MDC_GETFILEINFO", path);
2267 st = ¶m->lmd->lmd_st;
2269 /* libc chown() will do extra check, and if the real owner is
2270 * the same as the ones to set, it won't fall into kernel, so
2271 * invoke syscall directly. */
2272 rc = syscall(SYS_chown, path, -1, -1);
2274 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2276 rc = chmod(path, st->st_mode);
2278 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2284 int llapi_quotachown(char *path, int flag)
2286 struct find_param param;
2288 int ret = 0, len = strlen(path);
2290 if (len > PATH_MAX) {
2291 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2292 __FUNCTION__, path);
2296 buf = (char *)malloc(PATH_MAX + 1);
2300 memset(¶m, 0, sizeof(param));
2301 param.recursive = 1;
2305 ret = common_param_init(¶m);
2309 strncpy(buf, path, PATH_MAX + 1);
2310 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2311 NULL, ¶m, NULL);
2313 find_param_fini(¶m);
2321 #include <sys/wait.h>
2325 static int rmtacl_notify(int ops)
2329 int found = 0, fd, rc;
2331 fp = setmntent(MOUNTED, "r");
2333 perror("setmntent");
2338 mnt = getmntent(fp);
2342 if (!llapi_is_lustre_mnt(mnt))
2345 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2351 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2363 static char *next_token(char *p, int div)
2369 while (*p && *p != ':' && !isspace(*p))
2372 while (*p == ':' || isspace(*p))
2375 return *p ? p : NULL;
2378 static int rmtacl_name2id(char *name, int is_user)
2383 if ((pw = getpwnam(name)) == NULL)
2386 return (int)(pw->pw_uid);
2390 if ((gr = getgrnam(name)) == NULL)
2393 return (int)(gr->gr_gid);
2397 static int isodigit(int c)
2399 return (c >= '0' && c <= '7') ? 1 : 0;
2403 * Whether the name is just digits string (uid/gid) already or not.
2408 static int str_is_id(char *str)
2415 if (*str == 'x' || *str == 'X') { /* for Hex. */
2416 if (!isxdigit(*(++str)))
2419 while (isxdigit(*(++str)));
2420 } else if (isodigit(*str)) { /* for Oct. */
2421 while (isodigit(*(++str)));
2423 } else if (isdigit(*str)) { /* for Dec. */
2424 while (isdigit(*(++str)));
2427 return (*str == 0) ? 1 : 0;
2437 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2439 static rmtacl_name_t rmtacl_namelist[] = {
2440 { RMTACL_OPTNAME("user:"), 1, 0 },
2441 { RMTACL_OPTNAME("group:"), 0, 0 },
2442 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2443 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2444 /* for --tabular option */
2445 { RMTACL_OPTNAME("user"), 1, 1 },
2446 { RMTACL_OPTNAME("group"), 0, 1 },
2450 static int rgetfacl_output(char *str)
2452 char *start = NULL, *end = NULL;
2453 int is_user = 0, n, id;
2460 for (rn = rmtacl_namelist; rn->name; rn++) {
2461 if(strncmp(str, rn->name, rn->length) == 0) {
2462 if (!rn->next_token)
2463 start = str + rn->length;
2465 start = next_token(str + rn->length, 0);
2466 is_user = rn->is_user;
2471 end = next_token(start, 1);
2472 if (end == NULL || start == end) {
2473 n = printf("%s", str);
2479 id = rmtacl_name2id(start, is_user);
2480 if (id == INVALID_ID) {
2481 if (str_is_id(start)) {
2483 n = printf("%s", str);
2486 } else if ((id == NOBODY_UID && is_user) ||
2487 (id == NOBODY_GID && !is_user)) {
2489 n = printf("%s", str);
2493 n = printf("%s%d%s", str, id, end);
2498 static int child_status(int status)
2500 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2503 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2516 if ((pid = fork()) < 0) {
2522 /* child process redirects its output. */
2525 if (dup2(fd[1], 1) < 0) {
2536 status = rmtacl_notify(ops);
2540 exit(execvp(argv[0], argv));
2543 /* the following is parent process */
2544 if ((fp = fdopen(fd[0], "r")) == NULL) {
2551 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2552 if (output_func(buf) < 0)
2553 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2559 if (waitpid(pid, &status, 0) < 0) {
2564 return child_status(status);
2567 int llapi_lsetfacl(int argc, char *argv[])
2569 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2572 int llapi_lgetfacl(int argc, char *argv[])
2574 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2577 int llapi_rsetfacl(int argc, char *argv[])
2579 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2582 int llapi_rgetfacl(int argc, char *argv[])
2584 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2587 int llapi_cp(int argc, char *argv[])
2591 rc = rmtacl_notify(RMT_RSETFACL);
2595 exit(execvp(argv[0], argv));
2598 int llapi_ls(int argc, char *argv[])
2602 rc = rmtacl_notify(RMT_LGETFACL);
2606 exit(execvp(argv[0], argv));
2609 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2610 * format must have %s%s, buf must be > 16
2612 static int get_mdtname(char *name, char *format, char *buf)
2614 char suffix[]="-MDT0000";
2615 int len = strlen(name);
2617 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2618 name[len - 5] = '\0';
2623 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2626 /* Not enough room to add suffix */
2627 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2628 "MDT name too long |%s|", name);
2633 return sprintf(buf, format, name, suffix);
2636 /****** Changelog API ********/
2637 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2638 struct changelog_private {
2644 /** Start reading from a changelog
2645 * @param priv Opaque private control structure
2646 * @param flags Start flags (e.g. follow)
2647 * @param device Report changes recorded on this MDT
2648 * @param startrec Report changes beginning with this record number
2650 int llapi_changelog_start(void **priv, int flags, const char *device,
2653 struct changelog_private *cp;
2658 if (device[0] == '/')
2659 rc = llapi_search_fsname(device, mdtname);
2661 strncpy(mdtname, device, sizeof(mdtname));
2663 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2664 if (get_mdtname(mdtname,
2665 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2668 rc = first_match(path, path);
2672 if ((fd = open(path, O_RDONLY)) < 0) {
2673 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2677 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2679 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2683 cp = malloc(sizeof(*cp));
2689 cp->magic = CHANGELOG_PRIV_MAGIC;
2697 /** Finish reading from a changelog */
2698 int llapi_changelog_fini(void **priv)
2700 struct changelog_private *cp = (struct changelog_private *)*priv;
2702 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2711 static int pollwait(int fd) {
2712 struct pollfd pfds[1];
2716 pfds[0].events = POLLIN;
2717 rc = poll(pfds, 1, -1);
2718 return rc < 0 ? -errno : rc;
2721 /** Read the next changelog entry
2722 * @param priv Opaque private control structure
2723 * @param rech Changelog record handle; record will be allocated here
2724 * @return 0 valid message received; rec is set
2728 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2730 struct changelog_private *cp = (struct changelog_private *)priv;
2731 struct changelog_rec rec, *recp;
2734 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2740 /* Read in the rec to get the namelen */
2741 rc = read(cp->fd, &rec, sizeof(rec));
2745 if (cp->flags && CHANGELOG_FLAG_FOLLOW) {
2746 rc = pollwait(cp->fd);
2754 recp = malloc(sizeof(rec) + rec.cr_namelen);
2757 memcpy(recp, &rec, sizeof(rec));
2758 rc = read(cp->fd, recp->cr_name, rec.cr_namelen);
2761 llapi_err(LLAPI_MSG_ERROR, "Can't read entire filename");
2769 /** Release the changelog record when done with it. */
2770 int llapi_changelog_free(struct changelog_rec **rech)
2778 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2781 struct ioc_changelog_clear data;
2784 int id, fd, index, rc;
2787 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2788 "can't purge negative records\n");
2792 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2793 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2794 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2795 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2796 "expecting id of the form '"CHANGELOG_USER_PREFIX
2797 "<num>'; got '%s'\n", idstr);
2801 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2802 if (mdtname[0] == '/') {
2804 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2805 rc = fd < 0 ? -errno : 0;
2807 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2809 ptr = fsname + strlen(fsname) - 8;
2811 index = strtol(ptr + 4, NULL, 10);
2812 rc = get_root_path(WANT_FD | WANT_ERROR, fsname, &fd, NULL, -1);
2815 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2816 "Can't open %s: %d\n", mdtname, rc);
2820 data.icc_mdtindex = index;
2822 data.icc_recno = endrec;
2823 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2825 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2831 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2832 int buflen, long long *recno, int *linkno)
2834 char path[PATH_MAX];
2836 struct getinfo_fid2path *gf;
2839 while (*fidstr == '[')
2842 sscanf(fidstr, SFID, RFID(&fid));
2843 if (!fid_is_sane(&fid)) {
2844 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2845 "bad FID format [%s], should be "DFID"\n",
2846 fidstr, (__u64)1, 2, 0);
2850 /* Take path or fsname */
2851 if (device[0] == '/') {
2852 strcpy(path, device);
2854 rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
2859 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2860 fd = open(path, O_RDONLY | O_NONBLOCK);
2864 gf = malloc(sizeof(*gf) + buflen);
2866 gf->gf_recno = *recno;
2867 gf->gf_linkno = *linkno;
2868 gf->gf_pathlen = buflen;
2869 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2871 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2873 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2874 *recno = gf->gf_recno;
2875 *linkno = gf->gf_linkno;
2883 static int path2fid_from_lma(const char *path, lustre_fid *fid)
2886 struct lustre_mdt_attrs *lma;
2889 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
2892 lma = (struct lustre_mdt_attrs *)buf;
2893 fid_be_to_cpu(fid, &lma->lma_self_fid);
2897 int llapi_path2fid(const char *path, lustre_fid *fid)
2901 memset(fid, 0, sizeof(*fid));
2902 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2904 if (errno == ELOOP) /* symbolic link */
2905 return path2fid_from_lma(path, fid);
2909 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
2910 if (rc == -EINVAL) /* char special device */
2911 rc = path2fid_from_lma(path, fid);
2917 /****** HSM Copytool API ********/
2918 #define CT_PRIV_MAGIC 0xC0BE2001
2919 struct copytool_private {
2922 int archive_num_count;
2923 int archive_nums[0];
2926 #include <libcfs/libcfs.h>
2928 /** Register a copytool
2929 * @param priv Opaque private control structure
2930 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
2931 * @param archive_nums Which archive numbers this copytool is responsible for
2933 int llapi_copytool_start(void **priv, int flags, int archive_num_count,
2936 struct copytool_private *ct;
2939 if (archive_num_count > 0 && archive_nums == NULL) {
2940 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2941 "NULL archive numbers");
2945 ct = malloc(sizeof(*ct) +
2946 archive_num_count * sizeof(ct->archive_nums[0]));
2950 ct->magic = CT_PRIV_MAGIC;
2951 ct->archive_num_count = archive_num_count;
2952 if (ct->archive_num_count > 0)
2953 memcpy(ct->archive_nums, archive_nums, archive_num_count *
2954 sizeof(ct->archive_nums[0]));
2956 rc = libcfs_ulnl_start(&ct->lnl, LNL_GRP_HSM);
2968 /** Deregister a copytool */
2969 int llapi_copytool_fini(void **priv)
2971 struct copytool_private *ct = (struct copytool_private *)*priv;
2973 if (!ct || (ct->magic != CT_PRIV_MAGIC))
2976 libcfs_ulnl_stop(&ct->lnl);
2982 /** Wait for the next hsm_action_list
2983 * @param priv Opaque private control structure
2984 * @param halh Action list handle, will be allocated here
2985 * @param msgsize Number of bytes in the message, will be set here
2986 * @return 0 valid message received; halh and msgsize are set
2989 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
2991 struct copytool_private *ct = (struct copytool_private *)priv;
2992 struct lnl_hdr *lnlh;
2993 struct hsm_action_list *hal;
2996 if (!ct || (ct->magic != CT_PRIV_MAGIC))
2998 if (halh == NULL || msgsize == NULL)
3001 rc = libcfs_ulnl_msg_get(&ct->lnl, HAL_MAXSIZE,
3002 LNL_TRANSPORT_HSM, &lnlh);
3006 /* Handle generic messages */
3007 if (lnlh->lnl_transport == LNL_TRANSPORT_GENERIC &&
3008 lnlh->lnl_msgtype == LNL_MSG_SHUTDOWN) {
3013 if (lnlh->lnl_transport != LNL_TRANSPORT_HSM ||
3014 lnlh->lnl_msgtype != HMT_ACTION_LIST) {
3015 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3016 "Unknown HSM message type %d:%d\n",
3017 lnlh->lnl_transport, lnlh->lnl_msgtype);
3022 /* Our message is an hsm_action_list */
3024 hal = (struct hsm_action_list *)(lnlh + 1);
3026 /* Check that we have registered for this archive # */
3027 for (rc = 0; rc < ct->archive_num_count; rc++) {
3028 if (hal->hal_archive_num == ct->archive_nums[rc])
3031 if (rc >= ct->archive_num_count) {
3032 CDEBUG(D_INFO, "This copytool does not service archive #%d, "
3033 "ignoring this request.\n", hal->hal_archive_num);
3039 *msgsize = lnlh->lnl_msglen - sizeof(*lnlh);
3043 libcfs_ulnl_msg_free(&lnlh);
3049 /** Release the action list when done with it. */
3050 int llapi_copytool_free(struct hsm_action_list **hal)
3053 struct lnl_hdr *lnlh = (struct lnl_hdr *)*hal - 1;
3054 libcfs_ulnl_msg_free(&lnlh);