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>
63 #ifdef HAVE_LINUX_UNISTD_H
64 #include <linux/unistd.h>
70 #include <liblustre.h>
71 #include <lnet/lnetctl.h>
73 #include <lustre_lib.h>
75 #include <lustre/liblustreapi.h>
77 static unsigned llapi_dir_filetype_table[] = {
86 #if defined(DT_DOOR) && defined(S_IFDOOR)
91 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 static const int DT_MAX = DT_DOOR;
94 static const int DT_MAX = DT_SOCK;
97 static unsigned llapi_filetype_dir_table[] = {
106 #if defined(DT_DOOR) && defined(S_IFDOOR)
111 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 static const int S_IFMAX = DT_DOOR;
114 static const int S_IFMAX = DT_SOCK;
117 /* liblustreapi message level */
118 static int llapi_msg_level = LLAPI_MSG_MAX;
120 void llapi_msg_set_level(int level)
122 /* ensure level is in the good range */
123 if (level < LLAPI_MSG_OFF)
124 llapi_msg_level = LLAPI_MSG_OFF;
125 else if (level > LLAPI_MSG_MAX)
126 llapi_msg_level = LLAPI_MSG_MAX;
128 llapi_msg_level = level;
131 void llapi_err(int level, char *fmt, ...)
134 int tmp_errno = abs(errno);
136 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
140 vfprintf(stderr, fmt, args);
143 if (level & LLAPI_MSG_NO_ERRNO)
144 fprintf(stderr, "\n");
146 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
149 #define llapi_err_noerrno(level, fmt, a...) \
150 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
152 void llapi_printf(int level, char *fmt, ...)
156 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
160 vfprintf(stdout, fmt, args);
165 * size_units is unchanged if no specifier used
167 int parse_size(char *optarg, unsigned long long *size,
168 unsigned long long *size_units, int bytes_spec)
172 *size = strtoull(optarg, &end, 0);
175 if ((*end == 'b') && *(end+1) == '\0' &&
176 (*size & (~0ULL << (64 - 9))) == 0 &&
179 *size_units = 1 << 9;
180 } else if ((*end == 'b') && *(end+1) == '\0' &&
183 } else if ((*end == 'k' || *end == 'K') &&
184 *(end+1) == '\0' && (*size &
185 (~0ULL << (64 - 10))) == 0) {
187 *size_units = 1 << 10;
188 } else if ((*end == 'm' || *end == 'M') &&
189 *(end+1) == '\0' && (*size &
190 (~0ULL << (64 - 20))) == 0) {
192 *size_units = 1 << 20;
193 } else if ((*end == 'g' || *end == 'G') &&
194 *(end+1) == '\0' && (*size &
195 (~0ULL << (64 - 30))) == 0) {
197 *size_units = 1 << 30;
198 } else if ((*end == 't' || *end == 'T') &&
199 *(end+1) == '\0' && (*size &
200 (~0ULL << (64 - 40))) == 0) {
202 *size_units = 1ULL << 40;
203 } else if ((*end == 'p' || *end == 'P') &&
204 *(end+1) == '\0' && (*size &
205 (~0ULL << (64 - 50))) == 0) {
207 *size_units = 1ULL << 50;
208 } else if ((*end == 'e' || *end == 'E') &&
209 *(end+1) == '\0' && (*size &
210 (~0ULL << (64 - 60))) == 0) {
212 *size_units = 1ULL << 60;
221 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
222 int stripe_count, int stripe_pattern)
226 /* 64 KB is the largest common page size I'm aware of (on ia64), but
227 * check the local page size just in case. */
228 page_size = LOV_MIN_STRIPE_SIZE;
229 if (getpagesize() > page_size) {
230 page_size = getpagesize();
231 llapi_err_noerrno(LLAPI_MSG_WARN,
232 "warning: your page size (%u) is "
233 "larger than expected (%u)", page_size,
234 LOV_MIN_STRIPE_SIZE);
236 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
237 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
238 "must be an even multiple of %d bytes",
239 stripe_size, page_size);
242 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
244 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
248 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
250 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
254 if (stripe_size >= (1ULL << 32)){
256 llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
257 " is not currently supported and would wrap");
263 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
265 int llapi_file_open_pool(const char *name, int flags, int mode,
266 unsigned long long stripe_size, int stripe_offset,
267 int stripe_count, int stripe_pattern, char *pool_name)
269 struct lov_user_md_v3 lum = { 0 };
272 char fsname[MAX_OBD_NAME + 1], *ptr;
274 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
275 if (fd < 0 && errno == EISDIR) {
276 fd = open(name, O_DIRECTORY | O_RDONLY);
282 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
286 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
287 stripe_count, stripe_pattern)) != 0){
292 /* Initialize IOCTL striping pattern structure */
293 lum.lmm_magic = LOV_USER_MAGIC_V3;
294 lum.lmm_pattern = stripe_pattern;
295 lum.lmm_stripe_size = stripe_size;
296 lum.lmm_stripe_count = stripe_count;
297 lum.lmm_stripe_offset = stripe_offset;
299 /* in case user give the full pool name <fsname>.<poolname>, skip
301 if (pool_name != NULL) {
302 ptr = strchr(pool_name, '.');
304 strncpy(fsname, pool_name, ptr - pool_name);
306 /* if fsname matches a filesystem skip it
307 * if not keep the poolname as is */
308 if (poolpath(fsname, NULL, NULL) == 0)
311 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
313 /* If no pool is specified at all, use V1 request */
314 lum.lmm_magic = LOV_USER_MAGIC_V1;
317 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
318 char *errmsg = "stripe already set";
320 if (errno != EEXIST && errno != EALREADY)
321 errmsg = strerror(errno);
323 llapi_err_noerrno(LLAPI_MSG_ERROR,
324 "error on ioctl "LPX64" for '%s' (%d): %s",
325 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
336 int llapi_file_open(const char *name, int flags, int mode,
337 unsigned long long stripe_size, int stripe_offset,
338 int stripe_count, int stripe_pattern)
340 return llapi_file_open_pool(name, flags, mode, stripe_size,
341 stripe_offset, stripe_count,
342 stripe_pattern, NULL);
345 int llapi_file_create(const char *name, unsigned long long stripe_size,
346 int stripe_offset, int stripe_count, int stripe_pattern)
350 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
351 stripe_offset, stripe_count, stripe_pattern,
360 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
361 int stripe_offset, int stripe_count,
362 int stripe_pattern, char *pool_name)
366 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
367 stripe_offset, stripe_count, stripe_pattern,
377 * Find the fsname, the full path, and/or an open fd.
378 * Either the fsname or path must not be NULL
380 #define WANT_PATH 0x1
381 #define WANT_FSNAME 0x2
383 #define WANT_INDEX 0x8
384 #define WANT_ERROR 0x10
385 static int get_root_path(int want, char *fsname, int *outfd, char *path,
389 char buf[PATH_MAX], mntdir[PATH_MAX];
390 char *name = NULL, *ptr;
392 int idx = 0, len = 0, mntlen, fd;
395 /* get the mount point */
396 fp = setmntent(MOUNTED, "r");
398 llapi_err(LLAPI_MSG_ERROR,
399 "setmntent(%s) failed: %s:", MOUNTED,
404 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
407 if (!llapi_is_lustre_mnt(&mnt))
410 mntlen = strlen(mnt.mnt_dir);
411 ptr = strrchr(mnt.mnt_fsname, '/');
418 if ((want & WANT_INDEX) && (idx++ != index))
421 /* Check the fsname for a match */
422 if (!(want & WANT_FSNAME) && fsname != NULL &&
423 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
426 /* If the path isn't set return the first one we find */
427 if (path == NULL || strlen(path) == 0) {
428 strcpy(mntdir, mnt.mnt_dir);
432 /* Otherwise find the longest matching path */
433 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
434 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
435 strcpy(mntdir, mnt.mnt_dir);
445 if ((want & WANT_FSNAME) && fsname != NULL)
446 strcpy(fsname, name);
447 if ((want & WANT_PATH) && path != NULL)
448 strcpy(path, mntdir);
449 if (want & WANT_FD) {
450 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
458 } else if (want & WANT_ERROR)
459 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
460 "can't find fs root for '%s': %d",
461 (want & WANT_PATH) ? fsname : path, rc);
466 * search lustre mounts
468 * Calling this function will return to the user the mount point, mntdir, and
469 * the file system name, fsname, if the user passed a buffer to this routine.
471 * The user inputs are pathname and index. If the pathname is supplied then
472 * the value of the index will be ignored. The pathname will return data if
473 * the pathname is located on a lustre mount. Index is used to pick which
474 * mount point you want in the case of multiple mounted lustre file systems.
475 * See function lfs_osts in lfs.c for a example of the index use.
477 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
480 int want = WANT_PATH, idx = -1;
486 strcpy(mntdir, pathname);
490 return get_root_path(want, fsname, NULL, mntdir, idx);
493 int llapi_search_fsname(const char *pathname, char *fsname)
495 return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
496 (char *)pathname, -1);
499 /* return the first file matching this pattern */
500 static int first_match(char *pattern, char *buffer)
504 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
507 if (glob_info.gl_pathc < 1) {
508 globfree(&glob_info);
512 strcpy(buffer, glob_info.gl_pathv[0]);
514 globfree(&glob_info);
519 * find the pool directory path under /proc
520 * (can be also used to test if a fsname is known)
522 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
525 char pattern[PATH_MAX + 1];
526 char buffer[PATH_MAX];
528 if (fsname == NULL) {
529 rc = llapi_search_fsname(pathname, buffer);
533 strcpy(pathname, fsname);
536 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
537 rc = first_match(pattern, buffer);
541 /* in fsname test mode, pool_pathname is NULL */
542 if (pool_pathname != NULL)
543 strcpy(pool_pathname, buffer);
549 * Get the list of pool members.
550 * \param poolname string of format <fsname>.<poolname>
551 * \param members caller-allocated array of char*
552 * \param list_size size of the members array
553 * \param buffer caller-allocated buffer for storing OST names
554 * \param buffer_size size of the buffer
556 * \return number of members retrieved for this pool
557 * \retval -error failure
559 int llapi_get_poolmembers(const char *poolname, char **members,
560 int list_size, char *buffer, int buffer_size)
562 char fsname[PATH_MAX + 1];
564 char pathname[PATH_MAX + 1];
565 char path[PATH_MAX + 1];
572 /* name is FSNAME.POOLNAME */
573 if (strlen(poolname) > PATH_MAX)
575 strcpy(fsname, poolname);
576 pool = strchr(fsname, '.');
583 rc = poolpath(fsname, NULL, pathname);
586 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
591 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
592 sprintf(path, "%s/%s", pathname, pool);
593 if ((fd = fopen(path, "r")) == NULL) {
594 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path);
599 while (fgets(buf, sizeof(buf), fd) != NULL) {
600 if (nb_entries >= list_size) {
605 if ((tmp = strchr(buf, '\n')) != NULL)
607 if (used + strlen(buf) + 1 > buffer_size) {
612 strcpy(buffer + used, buf);
613 members[nb_entries] = buffer + used;
614 used += strlen(buf) + 1;
624 * Get the list of pools in a filesystem.
625 * \param name filesystem name or path
626 * \param poollist caller-allocated array of char*
627 * \param list_size size of the poollist array
628 * \param buffer caller-allocated buffer for storing pool names
629 * \param buffer_size size of the buffer
631 * \return number of pools retrieved for this filesystem
632 * \retval -error failure
634 int llapi_get_poollist(const char *name, char **poollist, int list_size,
635 char *buffer, int buffer_size)
637 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
641 struct dirent *cookie = NULL;
643 unsigned int nb_entries = 0;
644 unsigned int used = 0;
647 /* initilize output array */
648 for (i = 0; i < list_size; i++)
651 /* is name a pathname ? */
652 ptr = strchr(name, '/');
654 /* only absolute pathname is supported */
657 if (!realpath(name, rname)) {
659 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
663 rc = poolpath(NULL, rname, pathname);
666 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
667 " a Lustre filesystem", name);
670 strcpy(fsname, rname);
673 strcpy(fsname, name);
674 rc = poolpath(fsname, NULL, pathname);
678 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
683 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
684 if ((dir = opendir(pathname)) == NULL) {
685 llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'",
691 rc = readdir_r(dir, &pool, &cookie);
694 llapi_err(LLAPI_MSG_ERROR,
695 "Error reading pool list for '%s'", name);
697 } else if ((rc == 0) && (cookie == NULL))
698 /* end of directory */
701 /* ignore . and .. */
702 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
705 /* check output bounds */
706 if (nb_entries >= list_size)
709 /* +2 for '.' and final '\0' */
710 if (used + strlen(pool.d_name) + strlen(fsname) + 2
714 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
715 poollist[nb_entries] = buffer + used;
716 used += strlen(pool.d_name) + strlen(fsname) + 2;
724 /* wrapper for lfs.c and obd.c */
725 int llapi_poollist(const char *name)
727 /* list of pool names (assume that pool count is smaller
729 char *list[FIND_MAX_OSTS];
731 /* fsname-OST0000_UUID < 32 char, 1 per OST */
732 int bufsize = FIND_MAX_OSTS * 32;
735 buffer = malloc(bufsize);
739 if ((name[0] == '/') || (strchr(name, '.') == NULL))
740 /* name is a path or fsname */
741 nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer,
744 /* name is a pool name (<fsname>.<poolname>) */
745 nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer,
748 for (i = 0; i < nb; i++)
749 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
752 return (nb < 0 ? nb : 0);
756 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
757 void *data, cfs_dirent_t *de);
759 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
760 #define OBD_NOT_FOUND (-1)
762 static int common_param_init(struct find_param *param)
764 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
765 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
766 llapi_err(LLAPI_MSG_ERROR,
767 "error: allocation of %d bytes for ioctl",
768 sizeof(lstat_t) + param->lumlen);
772 param->got_uuids = 0;
773 param->obdindexes = NULL;
774 param->obdindex = OBD_NOT_FOUND;
778 static void find_param_fini(struct find_param *param)
780 if (param->obdindexes)
781 free(param->obdindexes);
787 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
789 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
792 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
797 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
801 fd = open(path, O_RDONLY);
804 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
808 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
816 * If uuidp is NULL, return the number of available obd uuids.
817 * If uuidp is non-NULL, then it will return the uuids of the obds. If
818 * there are more OSTs then allocated to uuidp, then an error is returned with
819 * the ost_count set to number of available obd uuids.
821 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
823 struct obd_uuid lov_name;
826 int rc = 0, index = 0;
828 /* Get the lov name */
829 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
833 /* Now get the ost uuids from /proc */
834 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
836 fp = fopen(buf, "r");
839 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
843 while (fgets(buf, sizeof(buf), fp) != NULL) {
844 if (uuidp && (index < *ost_count)) {
845 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
853 if (uuidp && (index >= *ost_count))
860 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
867 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
872 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
878 /* Here, param->obduuid points to a single obduuid, the index of which is
879 * returned in param->obdindex */
880 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
882 struct obd_uuid lov_uuid;
883 char uuid[sizeof(struct obd_uuid)];
888 /* Get the lov name */
889 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
891 if (errno != ENOTTY) {
893 llapi_err(LLAPI_MSG_ERROR,
894 "error: can't get lov name: %s", dname);
901 param->got_uuids = 1;
903 /* Now get the ost uuids from /proc */
904 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
906 fp = fopen(buf, "r");
909 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
913 if (!param->obduuid && !param->quiet && !param->obds_printed)
914 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
916 while (fgets(buf, sizeof(buf), fp) != NULL) {
917 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
920 if (param->obduuid) {
921 if (strncmp(param->obduuid->uuid, uuid,
922 sizeof(uuid)) == 0) {
923 param->obdindex = index;
926 } else if (!param->quiet && !param->obds_printed) {
927 /* Print everything */
928 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
931 param->obds_printed = 1;
935 if (!param->quiet && param->obduuid &&
936 (param->obdindex == OBD_NOT_FOUND)) {
937 llapi_err_noerrno(LLAPI_MSG_ERROR,
938 "error: %s: unknown obduuid: %s",
939 __FUNCTION__, param->obduuid->uuid);
946 /* In this case, param->obduuid will be an array of obduuids and
947 * obd index for all these obduuids will be returned in
948 * param->obdindexes */
949 static int setup_obd_indexes(DIR *dir, struct find_param *param)
951 struct obd_uuid *uuids = NULL;
952 int obdcount = INIT_ALLOC_NUM_OSTS;
953 int ret, obd_valid = 0, obdnum, i;
955 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
956 sizeof(struct obd_uuid));
961 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
964 struct obd_uuid *uuids_temp;
966 if (ret == -EOVERFLOW) {
967 uuids_temp = realloc(uuids, obdcount *
968 sizeof(struct obd_uuid));
969 if (uuids_temp != NULL)
970 goto retry_get_uuids;
975 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
979 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
980 if (param->obdindexes == NULL)
983 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
984 for (i = 0; i <= obdcount; i++) {
985 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
986 (char *)&uuids[i]) == 0) {
987 param->obdindexes[obdnum] = i;
993 param->obdindexes[obdnum] = OBD_NOT_FOUND;
997 param->obdindex = OBD_NOT_FOUND;
999 param->obdindex = obd_valid;
1001 param->got_uuids = 1;
1006 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1007 int is_dir, int verbose, int quiet,
1010 char *prefix = is_dir ? "" : "lmm_";
1011 char nl = is_dir ? ' ' : '\n';
1013 if (verbose && path)
1014 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1016 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1017 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1019 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
1020 lum->lmm_object_gr);
1021 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1022 lum->lmm_object_id);
1025 if (verbose & VERBOSE_COUNT) {
1027 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1029 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1030 (int)lum->lmm_stripe_count, nl);
1033 if (verbose & VERBOSE_SIZE) {
1035 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1037 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
1041 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1042 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1043 lum->lmm_pattern, nl);
1046 if (verbose & VERBOSE_OFFSET) {
1048 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1050 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1051 lum->lmm_objects[0].l_ost_idx, nl);
1054 if ((verbose & VERBOSE_POOL) && (pool_name != NULL))
1055 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s%c", pool_name, nl);
1058 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1061 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1062 struct lov_user_ost_data_v1 *objects,
1063 char *path, int is_dir,
1064 int obdindex, int quiet, int header, int body)
1066 int i, obdstripe = 0;
1068 if (obdindex != OBD_NOT_FOUND) {
1069 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1070 if (obdindex == objects[i].l_ost_idx) {
1071 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1078 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1082 /* if it's a directory */
1084 if (obdstripe == 1) {
1085 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
1086 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1087 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
1089 /* maintain original behavior */
1091 header |= VERBOSE_ALL;
1092 lov_dump_user_lmm_header(lum, path, is_dir, header,
1098 if (header && (obdstripe == 1))
1099 lov_dump_user_lmm_header(lum, NULL, is_dir, header, quiet,
1103 if ((!quiet) && (obdstripe == 1))
1104 llapi_printf(LLAPI_MSG_NORMAL,
1105 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1107 for (i = 0; i < lum->lmm_stripe_count; i++) {
1108 int idx = objects[i].l_ost_idx;
1109 long long oid = objects[i].l_object_id;
1110 long long gr = objects[i].l_object_gr;
1111 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1112 llapi_printf(LLAPI_MSG_NORMAL,
1113 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1115 obdindex == idx ? " *" : "");
1117 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1121 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
1122 int is_dir, int obdindex, int quiet,
1123 int header, int body)
1125 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
1126 int i, obdstripe = 0;
1128 if (obdindex != OBD_NOT_FOUND) {
1129 for (i = 0; i < lumj->lmm_stripe_count; i++) {
1130 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
1131 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1138 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1142 if (header && obdstripe == 1) {
1143 lov_dump_user_lmm_header(lum, NULL, 0, header, quiet, NULL);
1145 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
1146 lumj->lmm_extent_count);
1150 unsigned long long start = -1, end = 0;
1151 if (!quiet && obdstripe == 1)
1152 llapi_printf(LLAPI_MSG_NORMAL,
1153 "joined\tobdidx\t\t objid\t\tobjid\t\t "
1154 "group\t\tstart\t\tend\n");
1155 for (i = 0; i < lumj->lmm_stripe_count; i++) {
1156 int idx = lumj->lmm_objects[i].l_ost_idx;
1157 long long oid = lumj->lmm_objects[i].l_object_id;
1158 long long gr = lumj->lmm_objects[i].l_object_gr;
1159 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
1160 llapi_printf(LLAPI_MSG_NORMAL,
1161 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
1163 obdindex == idx ? " *" : "");
1164 if (start != lumj->lmm_objects[i].l_extent_start ||
1165 end != lumj->lmm_objects[i].l_extent_end) {
1166 start = lumj->lmm_objects[i].l_extent_start;
1167 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
1168 end = lumj->lmm_objects[i].l_extent_end;
1169 if (end == (unsigned long long)-1)
1170 llapi_printf(LLAPI_MSG_NORMAL,
1173 llapi_printf(LLAPI_MSG_NORMAL,
1176 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
1179 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1183 void llapi_lov_dump_user_lmm(struct find_param *param,
1184 char *path, int is_dir)
1186 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1187 case LOV_USER_MAGIC_V1:
1188 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1189 param->lmd->lmd_lmm.lmm_objects,
1191 param->obdindex, param->quiet,
1193 (param->verbose || !param->obduuid));
1195 case LOV_USER_MAGIC_JOIN:
1196 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1197 param->obdindex, param->quiet,
1199 (param->verbose || !param->obduuid));
1201 case LOV_USER_MAGIC_V3: {
1202 char pool_name[LOV_MAXPOOLNAME + 1];
1203 struct lov_user_ost_data_v1 *objects;
1204 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1206 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1207 pool_name[LOV_MAXPOOLNAME] = '\0';
1208 objects = lmmv3->lmm_objects;
1209 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1210 objects, path, is_dir,
1211 param->obdindex, param->quiet,
1213 (param->verbose || !param->obduuid));
1217 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1218 "(expecting one of %#x %#x %#x)\n",
1219 *(__u32 *)¶m->lmd->lmd_lmm,
1220 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1226 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1232 fname = strrchr(path, '/');
1234 /* It should be a file (or other non-directory) */
1235 if (fname == NULL) {
1236 dname = (char *)malloc(2);
1240 fname = (char *)path;
1242 dname = (char *)malloc(fname - path + 1);
1245 strncpy(dname, path, fname - path);
1246 dname[fname - path] = '\0';
1250 if ((fd = open(dname, O_RDONLY)) == -1) {
1256 strcpy((char *)lum, fname);
1257 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1260 if (close(fd) == -1 && rc == 0)
1268 int llapi_file_lookup(int dirfd, const char *name)
1270 struct obd_ioctl_data data = { 0 };
1275 if (dirfd < 0 || name == NULL)
1278 data.ioc_version = OBD_IOCTL_VERSION;
1279 data.ioc_len = sizeof(data);
1280 data.ioc_inlbuf1 = (char *)name;
1281 data.ioc_inllen1 = strlen(name) + 1;
1283 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1285 llapi_err(LLAPI_MSG_ERROR,
1286 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1291 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1294 int llapi_mds_getfileinfo(char *path, DIR *parent,
1295 struct lov_user_mds_data *lmd)
1297 lstat_t *st = &lmd->lmd_st;
1298 char *fname = strrchr(path, '/');
1304 fname = (fname == NULL ? path : fname + 1);
1305 /* retrieve needed file info */
1306 strncpy((char *)lmd, fname,
1307 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1308 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1311 if (errno == ENOTTY) {
1312 /* ioctl is not supported, it is not a lustre fs.
1313 * Do the regular lstat(2) instead. */
1314 ret = lstat_f(path, st);
1316 llapi_err(LLAPI_MSG_ERROR,
1317 "error: %s: lstat failed for %s",
1318 __FUNCTION__, path);
1321 } else if (errno == ENOENT) {
1322 llapi_err(LLAPI_MSG_WARN,
1323 "warning: %s: %s does not exist",
1324 __FUNCTION__, path);
1327 llapi_err(LLAPI_MSG_ERROR,
1328 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1329 __FUNCTION__, path);
1337 static DIR *opendir_parent(char *path)
1343 fname = strrchr(path, '/');
1345 return opendir(".");
1349 parent = opendir(path);
1354 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1355 semantic_func_t sem_init,
1356 semantic_func_t sem_fini, void *data,
1367 if (!d && errno != ENOTDIR) {
1368 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1369 __FUNCTION__, path);
1371 } else if (!d && !parent) {
1372 /* ENOTDIR. Open the parent dir. */
1373 p = opendir_parent(path);
1375 GOTO(out, ret = -EINVAL);
1378 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1384 while ((dent = readdir64(d)) != NULL) {
1385 ((struct find_param *)data)->have_fileinfo = 0;
1387 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1390 /* Don't traverse .lustre directory */
1391 if (!(strcmp(dent->d_name, dot_lustre_name)))
1395 if ((len + dent->d_reclen + 2) > size) {
1396 llapi_err(LLAPI_MSG_ERROR,
1397 "error: %s: string buffer is too small",
1402 strcat(path, dent->d_name);
1404 if (dent->d_type == DT_UNKNOWN) {
1405 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1407 ret = llapi_mds_getfileinfo(path, d,
1408 ((struct find_param *)data)->lmd);
1410 ((struct find_param *)data)->have_fileinfo = 1;
1412 llapi_filetype_dir_table[st->st_mode &
1419 switch (dent->d_type) {
1421 llapi_err(LLAPI_MSG_ERROR,
1422 "error: %s: '%s' is UNKNOWN type %d",
1423 __FUNCTION__, dent->d_name, dent->d_type);
1426 ret = llapi_semantic_traverse(path, size, d, sem_init,
1427 sem_fini, data, dent);
1434 ret = sem_init(path, d, NULL, data, dent);
1438 if (sem_fini && ret == 0)
1439 sem_fini(path, d, NULL, data, dent);
1447 sem_fini(path, parent, d, data, de);
1456 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1457 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1459 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1460 * The table below gives the answers for the specified parameters (value and
1461 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1462 * --------------------------------------
1463 * 1 | file > limit; sign > 0 | -1 / -1 |
1464 * 2 | file = limit; sign > 0 | ? / 1 |
1465 * 3 | file < limit; sign > 0 | ? / 1 |
1466 * 4 | file > limit; sign = 0 | -1 / -1 |
1467 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1468 * 6 | file < limit; sign = 0 | ? / -1 |
1469 * 7 | file > limit; sign < 0 | 1 / 1 |
1470 * 8 | file = limit; sign < 0 | ? / -1 |
1471 * 9 | file < limit; sign < 0 | ? / -1 |
1472 * --------------------------------------
1473 * Note: 5th actually means that the value is within the interval
1474 * (limit - margin, limit]. */
1475 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1476 unsigned long long margin, int mds)
1484 if (file <= limit && file + margin > limit)
1486 if (file + margin <= limit)
1487 return mds ? 0 : -1;
1500 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1501 * Return -1 or 1 if file timestamp does not or does match the given criteria
1502 * correspondingly. Return 0 if the MDS time is being checked and there are
1503 * attributes on OSTs and it is not yet clear if the timespamp matches.
1505 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1506 * updated timestamps. */
1507 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1512 /* Check if file is accepted. */
1514 ret = find_value_cmp(st->st_atime, param->atime,
1515 param->asign, 24 * 60 * 60, mds);
1522 ret = find_value_cmp(st->st_mtime, param->mtime,
1523 param->msign, 24 * 60 * 60, mds);
1527 /* If the previous check matches, but this one is not yet clear,
1528 * we should return 0 to do an RPC on OSTs. */
1534 ret = find_value_cmp(st->st_ctime, param->ctime,
1535 param->csign, 24 * 60 * 60, mds);
1539 /* If the previous check matches, but this one is not yet clear,
1540 * we should return 0 to do an RPC on OSTs. */
1548 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1549 void *data, cfs_dirent_t *de)
1551 struct find_param *param = (struct find_param *)data;
1552 int decision = 1; /* 1 is accepted; -1 is rejected. */
1553 lstat_t *st = ¶m->lmd->lmd_st;
1555 int checked_type = 0;
1558 LASSERT(parent != NULL || dir != NULL);
1560 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1562 /* If a regular expression is presented, make the initial decision */
1563 if (param->pattern != NULL) {
1564 char *fname = strrchr(path, '/');
1565 fname = (fname == NULL ? path : fname + 1);
1566 ret = fnmatch(param->pattern, fname, 0);
1567 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1568 (ret == 0 && param->exclude_pattern))
1572 /* See if we can check the file type from the dirent. */
1573 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1574 de->d_type < DT_MAX) {
1576 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1577 if (param->exclude_type)
1580 if (!param->exclude_type)
1586 /* If a time or OST should be checked, the decision is not taken yet. */
1587 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1592 /* Request MDS for the stat info. */
1593 if (param->have_fileinfo == 0) {
1595 /* retrieve needed file info */
1596 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1597 (void *)param->lmd);
1599 char *fname = strrchr(path, '/');
1600 fname = (fname == NULL ? path : fname + 1);
1602 /* retrieve needed file info */
1603 strncpy((char *)param->lmd, fname, param->lumlen);
1604 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1605 (void *)param->lmd);
1610 if (errno == ENOTTY) {
1611 /* ioctl is not supported, it is not a lustre fs.
1612 * Do the regular lstat(2) instead. */
1614 ret = lstat_f(path, st);
1616 llapi_err(LLAPI_MSG_ERROR,
1617 "error: %s: lstat failed for %s",
1618 __FUNCTION__, path);
1621 } else if (errno == ENOENT) {
1622 llapi_err(LLAPI_MSG_WARN,
1623 "warning: %s: %s does not exist",
1624 __FUNCTION__, path);
1627 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1628 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1629 "IOC_MDC_GETFILEINFO", path);
1634 if (param->type && !checked_type) {
1635 if ((st->st_mode & S_IFMT) == param->type) {
1636 if (param->exclude_type)
1639 if (!param->exclude_type)
1645 if (param->obduuid) {
1646 if (lustre_fs && param->got_uuids &&
1647 param->st_dev != st->st_dev) {
1648 /* A lustre/lustre mount point is crossed. */
1649 param->got_uuids = 0;
1650 param->obds_printed = 0;
1651 param->obdindex = OBD_NOT_FOUND;
1654 if (lustre_fs && !param->got_uuids) {
1655 ret = setup_obd_indexes(dir ? dir : parent, param);
1659 param->st_dev = st->st_dev;
1660 } else if (!lustre_fs && param->got_uuids) {
1661 /* A lustre/non-lustre mount point is crossed. */
1662 param->got_uuids = 0;
1663 param->obdindex = OBD_NOT_FOUND;
1667 /* If an OBD UUID is specified but no one matches, skip this file. */
1668 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1671 /* If a OST UUID is given, and some OST matches, check it here. */
1672 if (param->obdindex != OBD_NOT_FOUND) {
1673 if (!S_ISREG(st->st_mode))
1676 /* Only those files should be accepted, which have a
1677 * stripe on the specified OST. */
1678 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1682 struct lov_user_ost_data_v1 *lmm_objects;
1684 if (param->lmd->lmd_lmm.lmm_magic ==
1685 LOV_USER_MAGIC_V3) {
1686 struct lov_user_md_v3 *lmmv3 =
1687 (void *)¶m->lmd->lmd_lmm;
1689 lmm_objects = lmmv3->lmm_objects;
1691 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1695 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1696 for (j = 0; j < param->num_obds; j++) {
1697 if (param->obdindexes[j] ==
1698 lmm_objects[i].l_ost_idx)
1703 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1708 if (param->check_uid) {
1709 if (st->st_uid == param->uid) {
1710 if (param->exclude_uid)
1713 if (!param->exclude_uid)
1718 if (param->check_gid) {
1719 if (st->st_gid == param->gid) {
1720 if (param->exclude_gid)
1723 if (!param->exclude_gid)
1728 if (param->check_pool) {
1729 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1731 /* empty requested pool is taken as no pool search => V1 */
1732 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1733 (param->poolname[0] == '\0')) ||
1734 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1735 (strncmp(lmmv3->lmm_pool_name,
1736 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1737 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1738 (strcmp(param->poolname, "*") == 0))) {
1739 if (param->exclude_pool)
1742 if (!param->exclude_pool)
1747 /* Check the time on mds. */
1751 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1752 param->lmd->lmd_lmm.lmm_stripe_count)
1754 decision = find_time_check(st, param, for_mds);
1758 /* If file still fits the request, ask ost for updated info.
1759 The regular stat is almost of the same speed as some new
1760 'glimpse-size-ioctl'. */
1761 if (!decision && S_ISREG(st->st_mode) &&
1762 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1763 if (param->obdindex != OBD_NOT_FOUND) {
1764 /* Check whether the obd is active or not, if it is
1765 * not active, just print the object affected by this
1768 struct obd_statfs stat_buf;
1769 struct obd_uuid uuid_buf;
1771 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1772 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1773 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1774 param->obdindex, &stat_buf,
1777 if (ret == -ENODATA || ret == -ENODEV
1780 llapi_printf(LLAPI_MSG_NORMAL,
1781 "obd_uuid: %s failed %s ",
1782 param->obduuid->uuid,
1788 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1789 (void *)param->lmd);
1790 } else if (parent) {
1791 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1792 (void *)param->lmd);
1796 if (errno == ENOENT) {
1797 llapi_err(LLAPI_MSG_ERROR,
1798 "warning: %s: %s does not exist",
1799 __FUNCTION__, path);
1802 llapi_err(LLAPI_MSG_ERROR,
1803 "%s: IOC_LOV_GETINFO on %s failed",
1804 __FUNCTION__, path);
1809 /* Check the time on osc. */
1810 decision = find_time_check(st, param, 0);
1816 decision = find_value_cmp(st->st_size, param->size,
1817 param->size_sign, param->size_units,
1821 if (decision != -1) {
1822 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1824 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1826 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1830 /* Do not get down anymore? */
1831 if (param->depth == param->maxdepth)
1838 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1841 struct find_param *param = (struct find_param *)data;
1846 int llapi_find(char *path, struct find_param *param)
1849 int ret, len = strlen(path);
1851 if (len > PATH_MAX) {
1852 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1853 __FUNCTION__, path);
1857 buf = (char *)malloc(PATH_MAX + 1);
1861 ret = common_param_init(param);
1869 strncpy(buf, path, PATH_MAX + 1);
1870 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1871 cb_common_fini, param, NULL);
1873 find_param_fini(param);
1875 return ret < 0 ? ret : 0;
1878 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1881 struct find_param *param = (struct find_param *)data;
1884 LASSERT(parent != NULL || d != NULL);
1887 if (!param->got_uuids) {
1888 ret = setup_obd_uuid(d ? d : parent, path, param);
1894 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1895 (void *)¶m->lmd->lmd_lmm);
1896 } else if (parent) {
1897 char *fname = strrchr(path, '/');
1898 fname = (fname == NULL ? path : fname + 1);
1900 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1901 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1902 (void *)¶m->lmd->lmd_lmm);
1906 if (errno == ENODATA) {
1907 if (!param->obduuid && !param->quiet)
1908 llapi_printf(LLAPI_MSG_NORMAL,
1909 "%s has no stripe info\n", path);
1911 } else if (errno == ENOTTY) {
1912 llapi_err(LLAPI_MSG_ERROR,
1913 "%s: '%s' not on a Lustre fs?",
1914 __FUNCTION__, path);
1915 } else if (errno == ENOENT) {
1916 llapi_err(LLAPI_MSG_WARN,
1917 "warning: %s: %s does not exist",
1918 __FUNCTION__, path);
1921 llapi_err(LLAPI_MSG_ERROR,
1922 "error: %s: %s failed for %s",
1923 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1924 "IOC_MDC_GETFILESTRIPE", path);
1930 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1932 /* Do not get down anymore? */
1933 if (param->depth == param->maxdepth)
1940 int llapi_getstripe(char *path, struct find_param *param)
1943 int ret = 0, len = strlen(path);
1945 if (len > PATH_MAX) {
1946 llapi_err(LLAPI_MSG_ERROR,
1947 "%s: Path name '%s' is too long",
1948 __FUNCTION__, path);
1952 buf = (char *)malloc(PATH_MAX + 1);
1956 ret = common_param_init(param);
1964 strncpy(buf, path, PATH_MAX + 1);
1965 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1966 cb_common_fini, param, NULL);
1967 find_param_fini(param);
1969 return ret < 0 ? ret : 0;
1972 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1973 struct obd_statfs *stat_buf,
1974 struct obd_uuid *uuid_buf)
1977 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1979 struct obd_ioctl_data data = { 0 };
1982 data.ioc_inlbuf1 = (char *)&type;
1983 data.ioc_inllen1 = sizeof(__u32);
1984 data.ioc_inlbuf2 = (char *)&index;
1985 data.ioc_inllen2 = sizeof(__u32);
1986 data.ioc_pbuf1 = (char *)stat_buf;
1987 data.ioc_plen1 = sizeof(struct obd_statfs);
1988 data.ioc_pbuf2 = (char *)uuid_buf;
1989 data.ioc_plen2 = sizeof(struct obd_uuid);
1991 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1992 llapi_err(LLAPI_MSG_ERROR,
1993 "llapi_obd_statfs: error packing ioctl data");
1997 fd = open(path, O_RDONLY);
1998 if (errno == EISDIR)
1999 fd = open(path, O_DIRECTORY | O_RDONLY);
2002 rc = errno ? -errno : -EBADF;
2003 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2004 __FUNCTION__, path);
2007 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2009 rc = errno ? -errno : -EINVAL;
2015 #define MAX_STRING_SIZE 128
2016 #define DEVICES_LIST "/proc/fs/lustre/devices"
2018 int llapi_ping(char *obd_type, char *obd_name)
2020 char path[MAX_STRING_SIZE];
2024 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2025 obd_type, obd_name);
2027 fd = open(path, O_WRONLY);
2030 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2034 rc = write(fd, buf, 1);
2042 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2044 char buf[MAX_STRING_SIZE];
2045 FILE *fp = fopen(DEVICES_LIST, "r");
2050 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2054 while (fgets(buf, sizeof(buf), fp) != NULL) {
2055 char *obd_type_name = NULL;
2056 char *obd_name = NULL;
2057 char *obd_uuid = NULL;
2059 struct obd_ioctl_data datal = { 0, };
2060 struct obd_statfs osfs_buffer;
2062 while(bufp[0] == ' ')
2065 for(i = 0; i < 3; i++) {
2066 obd_type_name = strsep(&bufp, " ");
2068 obd_name = strsep(&bufp, " ");
2069 obd_uuid = strsep(&bufp, " ");
2071 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2073 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2074 datal.ioc_plen1 = sizeof(osfs_buffer);
2076 for (i = 0; i < type_num; i++) {
2077 if (strcmp(obd_type_name, obd_type[i]) != 0)
2080 cb(obd_type_name, obd_name, obd_uuid, args);
2087 static void do_target_check(char *obd_type_name, char *obd_name,
2088 char *obd_uuid, void *args)
2092 rc = llapi_ping(obd_type_name, obd_name);
2093 if (rc == ENOTCONN) {
2094 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2096 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2098 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2102 int llapi_target_check(int type_num, char **obd_type, char *dir)
2104 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2107 #undef MAX_STRING_SIZE
2109 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2111 char raw[OBD_MAX_IOCTL_BUFFER];
2112 char out[LLOG_CHUNK_SIZE];
2114 struct obd_ioctl_data data = { 0 };
2119 sprintf(key, "%s", keyword);
2120 memset(raw, 0, sizeof(raw));
2121 memset(out, 0, sizeof(out));
2122 data.ioc_inlbuf1 = key;
2123 data.ioc_inllen1 = strlen(key) + 1;
2125 data.ioc_inlbuf2 = node_name;
2126 data.ioc_inllen2 = strlen(node_name) + 1;
2128 data.ioc_pbuf1 = out;
2129 data.ioc_plen1 = sizeof(out);
2130 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2134 root = opendir(dir);
2137 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2141 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2143 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2145 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2151 /* Is this a lustre fs? */
2152 int llapi_is_lustre_mnttype(const char *type)
2154 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2157 /* Is this a lustre client fs? */
2158 int llapi_is_lustre_mnt(struct mntent *mnt)
2160 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2161 strstr(mnt->mnt_fsname, ":/") != NULL);
2164 int llapi_quotacheck(char *mnt, int check_type)
2169 root = opendir(mnt);
2171 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2175 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2181 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2187 root = opendir(mnt);
2189 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2194 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2198 if (poll_intvl < 30)
2206 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2211 root = opendir(mnt);
2213 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2217 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2223 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2226 struct find_param *param = (struct find_param *)data;
2230 LASSERT(parent != NULL || d != NULL);
2233 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2234 (void *)param->lmd);
2235 } else if (parent) {
2236 char *fname = strrchr(path, '/');
2237 fname = (fname == NULL ? path : fname + 1);
2239 strncpy((char *)param->lmd, fname, param->lumlen);
2240 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2241 (void *)param->lmd);
2247 if (errno == ENODATA) {
2248 if (!param->obduuid && !param->quiet)
2249 llapi_err(LLAPI_MSG_ERROR,
2250 "%s has no stripe info", path);
2252 } else if (errno == ENOENT) {
2253 llapi_err(LLAPI_MSG_ERROR,
2254 "warning: %s: %s does not exist",
2255 __FUNCTION__, path);
2257 } else if (errno != EISDIR) {
2259 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2260 d ? "LL_IOC_MDC_GETINFO" :
2261 "IOC_MDC_GETFILEINFO", path);
2266 st = ¶m->lmd->lmd_st;
2268 /* libc chown() will do extra check, and if the real owner is
2269 * the same as the ones to set, it won't fall into kernel, so
2270 * invoke syscall directly. */
2271 rc = syscall(SYS_chown, path, -1, -1);
2273 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2275 rc = chmod(path, st->st_mode);
2277 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2283 int llapi_quotachown(char *path, int flag)
2285 struct find_param param;
2287 int ret = 0, len = strlen(path);
2289 if (len > PATH_MAX) {
2290 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2291 __FUNCTION__, path);
2295 buf = (char *)malloc(PATH_MAX + 1);
2299 memset(¶m, 0, sizeof(param));
2300 param.recursive = 1;
2304 ret = common_param_init(¶m);
2308 strncpy(buf, path, PATH_MAX + 1);
2309 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2310 NULL, ¶m, NULL);
2312 find_param_fini(¶m);
2320 #include <sys/wait.h>
2324 static int rmtacl_notify(int ops)
2328 int found = 0, fd, rc;
2330 fp = setmntent(MOUNTED, "r");
2332 perror("setmntent");
2337 mnt = getmntent(fp);
2341 if (!llapi_is_lustre_mnt(mnt))
2344 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2350 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2362 static char *next_token(char *p, int div)
2368 while (*p && *p != ':' && !isspace(*p))
2371 while (*p == ':' || isspace(*p))
2374 return *p ? p : NULL;
2377 static int rmtacl_name2id(char *name, int is_user)
2382 if ((pw = getpwnam(name)) == NULL)
2385 return (int)(pw->pw_uid);
2389 if ((gr = getgrnam(name)) == NULL)
2392 return (int)(gr->gr_gid);
2396 static int isodigit(int c)
2398 return (c >= '0' && c <= '7') ? 1 : 0;
2402 * Whether the name is just digits string (uid/gid) already or not.
2407 static int str_is_id(char *str)
2414 if (*str == 'x' || *str == 'X') { /* for Hex. */
2415 if (!isxdigit(*(++str)))
2418 while (isxdigit(*(++str)));
2419 } else if (isodigit(*str)) { /* for Oct. */
2420 while (isodigit(*(++str)));
2422 } else if (isdigit(*str)) { /* for Dec. */
2423 while (isdigit(*(++str)));
2426 return (*str == 0) ? 1 : 0;
2436 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2438 static rmtacl_name_t rmtacl_namelist[] = {
2439 { RMTACL_OPTNAME("user:"), 1, 0 },
2440 { RMTACL_OPTNAME("group:"), 0, 0 },
2441 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2442 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2443 /* for --tabular option */
2444 { RMTACL_OPTNAME("user"), 1, 1 },
2445 { RMTACL_OPTNAME("group"), 0, 1 },
2449 static int rgetfacl_output(char *str)
2451 char *start = NULL, *end = NULL;
2452 int is_user = 0, n, id;
2459 for (rn = rmtacl_namelist; rn->name; rn++) {
2460 if(strncmp(str, rn->name, rn->length) == 0) {
2461 if (!rn->next_token)
2462 start = str + rn->length;
2464 start = next_token(str + rn->length, 0);
2465 is_user = rn->is_user;
2470 end = next_token(start, 1);
2471 if (end == NULL || start == end) {
2472 n = printf("%s", str);
2478 id = rmtacl_name2id(start, is_user);
2479 if (id == INVALID_ID) {
2480 if (str_is_id(start)) {
2482 n = printf("%s", str);
2485 } else if ((id == NOBODY_UID && is_user) ||
2486 (id == NOBODY_GID && !is_user)) {
2488 n = printf("%s", str);
2492 n = printf("%s%d%s", str, id, end);
2497 static int child_status(int status)
2499 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2502 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2515 if ((pid = fork()) < 0) {
2521 /* child process redirects its output. */
2524 if (dup2(fd[1], 1) < 0) {
2535 status = rmtacl_notify(ops);
2539 exit(execvp(argv[0], argv));
2542 /* the following is parent process */
2543 if ((fp = fdopen(fd[0], "r")) == NULL) {
2550 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2551 if (output_func(buf) < 0)
2552 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2558 if (waitpid(pid, &status, 0) < 0) {
2563 return child_status(status);
2566 int llapi_lsetfacl(int argc, char *argv[])
2568 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2571 int llapi_lgetfacl(int argc, char *argv[])
2573 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2576 int llapi_rsetfacl(int argc, char *argv[])
2578 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2581 int llapi_rgetfacl(int argc, char *argv[])
2583 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2586 int llapi_cp(int argc, char *argv[])
2590 rc = rmtacl_notify(RMT_RSETFACL);
2594 exit(execvp(argv[0], argv));
2597 int llapi_ls(int argc, char *argv[])
2601 rc = rmtacl_notify(RMT_LGETFACL);
2605 exit(execvp(argv[0], argv));
2608 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2609 * format must have %s%s, buf must be > 16
2611 static int get_mdtname(char *name, char *format, char *buf)
2613 char suffix[]="-MDT0000";
2614 int len = strlen(name);
2616 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2617 name[len - 5] = '\0';
2622 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2625 /* Not enough room to add suffix */
2626 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2627 "MDT name too long |%s|", name);
2632 return sprintf(buf, format, name, suffix);
2635 /****** Changelog API ********/
2636 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2637 struct changelog_private {
2643 /** Start reading from a changelog
2644 * @param priv Opaque private control structure
2645 * @param flags Start flags (e.g. follow)
2646 * @param device Report changes recorded on this MDT
2647 * @param startrec Report changes beginning with this record number
2649 int llapi_changelog_start(void **priv, int flags, const char *device,
2652 struct changelog_private *cp;
2657 if (device[0] == '/')
2658 rc = llapi_search_fsname(device, mdtname);
2660 strncpy(mdtname, device, sizeof(mdtname));
2662 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2663 if (get_mdtname(mdtname,
2664 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2667 rc = first_match(path, path);
2671 if ((fd = open(path, O_RDONLY)) < 0) {
2672 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2676 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2678 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2682 cp = malloc(sizeof(*cp));
2688 cp->magic = CHANGELOG_PRIV_MAGIC;
2696 /** Finish reading from a changelog */
2697 int llapi_changelog_fini(void **priv)
2699 struct changelog_private *cp = (struct changelog_private *)*priv;
2701 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2710 static int pollwait(int fd) {
2711 struct pollfd pfds[1];
2715 pfds[0].events = POLLIN;
2716 rc = poll(pfds, 1, -1);
2717 return rc < 0 ? -errno : rc;
2720 /** Read the next changelog entry
2721 * @param priv Opaque private control structure
2722 * @param rech Changelog record handle; record will be allocated here
2723 * @return 0 valid message received; rec is set
2727 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2729 struct changelog_private *cp = (struct changelog_private *)priv;
2730 struct changelog_rec rec, *recp;
2733 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2739 /* Read in the rec to get the namelen */
2740 rc = read(cp->fd, &rec, sizeof(rec));
2744 if (cp->flags && CHANGELOG_FLAG_FOLLOW) {
2745 rc = pollwait(cp->fd);
2753 recp = malloc(sizeof(rec) + rec.cr_namelen);
2756 memcpy(recp, &rec, sizeof(rec));
2757 rc = read(cp->fd, recp->cr_name, rec.cr_namelen);
2760 llapi_err(LLAPI_MSG_ERROR, "Can't read entire filename");
2768 /** Release the changelog record when done with it. */
2769 int llapi_changelog_free(struct changelog_rec **rech)
2777 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2780 struct ioc_changelog_clear data;
2783 int id, fd, index, rc;
2786 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2787 "can't purge negative records\n");
2791 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2792 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2793 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2794 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2795 "expecting id of the form '"CHANGELOG_USER_PREFIX
2796 "<num>'; got '%s'\n", idstr);
2800 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2801 if (mdtname[0] == '/') {
2803 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2804 rc = fd < 0 ? -errno : 0;
2806 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2808 ptr = fsname + strlen(fsname) - 8;
2810 index = strtol(ptr + 4, NULL, 10);
2811 rc = get_root_path(WANT_FD | WANT_ERROR, fsname, &fd, NULL, -1);
2814 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2815 "Can't open %s: %d\n", mdtname, rc);
2819 data.icc_mdtindex = index;
2821 data.icc_recno = endrec;
2822 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2824 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2830 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2831 int buflen, long long *recno, int *linkno)
2833 char path[PATH_MAX];
2835 struct getinfo_fid2path *gf;
2838 while (*fidstr == '[')
2841 sscanf(fidstr, SFID, RFID(&fid));
2842 if (!fid_is_sane(&fid)) {
2843 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2844 "bad FID format [%s], should be "DFID"\n",
2845 fidstr, (__u64)1, 2, 0);
2849 /* Take path or fsname */
2850 if (device[0] == '/') {
2851 strcpy(path, device);
2853 rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
2858 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2859 fd = open(path, O_RDONLY | O_NONBLOCK);
2863 gf = malloc(sizeof(*gf) + buflen);
2865 gf->gf_recno = *recno;
2866 gf->gf_linkno = *linkno;
2867 gf->gf_pathlen = buflen;
2868 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2870 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2872 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2873 *recno = gf->gf_recno;
2874 *linkno = gf->gf_linkno;
2882 int llapi_path2fid(const char *path, lustre_fid *fid)
2886 fd = open(path, O_RDONLY);
2890 rc = ioctl(fd, LL_IOC_PATH2FID, fid);
2896 /****** HSM Copytool API ********/
2897 #define CT_PRIV_MAGIC 0xC0BE2001
2898 struct copytool_private {
2901 int archive_num_count;
2902 int archive_nums[0];
2905 #include <libcfs/libcfs.h>
2907 /** Register a copytool
2908 * @param priv Opaque private control structure
2909 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
2910 * @param archive_nums Which archive numbers this copytool is responsible for
2912 int llapi_copytool_start(void **priv, int flags, int archive_num_count,
2915 struct copytool_private *ct;
2918 if (archive_num_count > 0 && archive_nums == NULL) {
2919 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2920 "NULL archive numbers");
2924 ct = malloc(sizeof(*ct) +
2925 archive_num_count * sizeof(ct->archive_nums[0]));
2929 ct->magic = CT_PRIV_MAGIC;
2930 ct->archive_num_count = archive_num_count;
2931 if (ct->archive_num_count > 0)
2932 memcpy(ct->archive_nums, archive_nums, archive_num_count *
2933 sizeof(ct->archive_nums[0]));
2935 rc = libcfs_ulnl_start(&ct->lnl, LNL_GRP_HSM);
2947 /** Deregister a copytool */
2948 int llapi_copytool_fini(void **priv)
2950 struct copytool_private *ct = (struct copytool_private *)*priv;
2952 if (!ct || (ct->magic != CT_PRIV_MAGIC))
2955 libcfs_ulnl_stop(&ct->lnl);
2961 /** Wait for the next hsm_action_list
2962 * @param priv Opaque private control structure
2963 * @param halh Action list handle, will be allocated here
2964 * @param msgsize Number of bytes in the message, will be set here
2965 * @return 0 valid message received; halh and msgsize are set
2968 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
2970 struct copytool_private *ct = (struct copytool_private *)priv;
2971 struct lnl_hdr *lnlh;
2972 struct hsm_action_list *hal;
2975 if (!ct || (ct->magic != CT_PRIV_MAGIC))
2977 if (halh == NULL || msgsize == NULL)
2980 rc = libcfs_ulnl_msg_get(&ct->lnl, HAL_MAXSIZE,
2981 LNL_TRANSPORT_HSM, &lnlh);
2985 /* Handle generic messages */
2986 if (lnlh->lnl_transport == LNL_TRANSPORT_GENERIC &&
2987 lnlh->lnl_msgtype == LNL_MSG_SHUTDOWN) {
2992 if (lnlh->lnl_transport != LNL_TRANSPORT_HSM ||
2993 lnlh->lnl_msgtype != HMT_ACTION_LIST) {
2994 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2995 "Unknown HSM message type %d:%d\n",
2996 lnlh->lnl_transport, lnlh->lnl_msgtype);
3001 /* Our message is an hsm_action_list */
3003 hal = (struct hsm_action_list *)(lnlh + 1);
3005 /* Check that we have registered for this archive # */
3006 for (rc = 0; rc < ct->archive_num_count; rc++) {
3007 if (hal->hal_archive_num == ct->archive_nums[rc])
3010 if (rc >= ct->archive_num_count) {
3011 CDEBUG(D_INFO, "This copytool does not service archive #%d, "
3012 "ignoring this request.\n", hal->hal_archive_num);
3018 *msgsize = lnlh->lnl_msglen - sizeof(*lnlh);
3022 libcfs_ulnl_msg_free(&lnlh);
3028 /** Release the action list when done with it. */
3029 int llapi_copytool_free(struct hsm_action_list **hal)
3032 struct lnl_hdr *lnlh = (struct lnl_hdr *)*hal - 1;
3033 libcfs_ulnl_msg_free(&lnlh);