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 find_target_obdpath(char *fsname, char *path)
267 char pattern[PATH_MAX + 1];
270 snprintf(pattern, PATH_MAX,
271 "/proc/fs/lustre/lov/%s-*/target_obd",
273 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
274 if (rc == GLOB_NOMATCH)
279 strcpy(path, glob_info.gl_pathv[0]);
280 globfree(&glob_info);
284 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
287 char pattern[PATH_MAX + 1];
290 snprintf(pattern, PATH_MAX,
291 "/proc/fs/lustre/lov/%s-*/pools/%s",
293 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
294 /* If no pools, make sure the lov is available */
295 if ((rc == GLOB_NOMATCH) &&
296 (find_target_obdpath(fsname, poolpath) == -ENODEV))
301 strcpy(poolpath, glob_info.gl_pathv[0]);
302 globfree(&glob_info);
307 * if pool is NULL, search ostname in target_obd
308 * if pool is not NULL:
309 * if pool not found returns errno < 0
310 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
311 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
313 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
316 char buffer[PATH_MAX + 1];
320 len = strlen(ostname);
322 if (poolname == NULL)
323 rc = find_target_obdpath(fsname, buffer);
325 rc = find_poolpath(fsname, poolname, buffer);
329 if ((fd = fopen(buffer, "r")) == NULL)
332 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
333 if (poolname == NULL) {
335 /* Search for an ostname in the list of OSTs
336 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
337 ptr = strchr(buffer, ' ');
339 (strncmp(ptr + 1, ostname, len) == 0)) {
344 /* Search for an ostname in a pool,
345 (or an existing non-empty pool if no ostname) */
346 if ((ostname == NULL) ||
347 (strncmp(buffer, ostname, len) == 0)) {
357 int llapi_file_open_pool(const char *name, int flags, int mode,
358 unsigned long long stripe_size, int stripe_offset,
359 int stripe_count, int stripe_pattern, char *pool_name)
361 struct lov_user_md_v3 lum = { 0 };
365 /* Make sure we have a good pool */
366 if (pool_name != NULL) {
367 char fsname[MAX_OBD_NAME + 1], *ptr;
369 if (llapi_search_fsname(name, fsname)) {
370 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
371 "'%s' is not on a Lustre filesystem", name);
375 /* in case user gives the full pool name <fsname>.<poolname>,
376 * strip the fsname */
377 ptr = strchr(pool_name, '.');
380 if (strcmp(pool_name, fsname) != 0) {
382 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
383 "Pool '%s' is not on filesystem '%s'",
390 /* Make sure the pool exists and is non-empty */
391 if ((rc = llapi_search_ost(fsname, pool_name, NULL)) < 1) {
392 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
393 "pool '%s.%s' %s", fsname, pool_name,
394 rc == 0 ? "has no OSTs" : "does not exist");
399 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
400 if (fd < 0 && errno == EISDIR) {
401 fd = open(name, O_DIRECTORY | O_RDONLY);
407 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
411 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
412 stripe_count, stripe_pattern)) != 0){
417 /* Initialize IOCTL striping pattern structure */
418 lum.lmm_magic = LOV_USER_MAGIC_V3;
419 lum.lmm_pattern = stripe_pattern;
420 lum.lmm_stripe_size = stripe_size;
421 lum.lmm_stripe_count = stripe_count;
422 lum.lmm_stripe_offset = stripe_offset;
423 if (pool_name != NULL) {
424 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
426 /* If no pool is specified at all, use V1 request */
427 lum.lmm_magic = LOV_USER_MAGIC_V1;
430 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
431 char *errmsg = "stripe already set";
433 if (errno != EEXIST && errno != EALREADY)
434 errmsg = strerror(errno);
436 llapi_err_noerrno(LLAPI_MSG_ERROR,
437 "error on ioctl "LPX64" for '%s' (%d): %s",
438 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
449 int llapi_file_open(const char *name, int flags, int mode,
450 unsigned long long stripe_size, int stripe_offset,
451 int stripe_count, int stripe_pattern)
453 return llapi_file_open_pool(name, flags, mode, stripe_size,
454 stripe_offset, stripe_count,
455 stripe_pattern, NULL);
458 int llapi_file_create(const char *name, unsigned long long stripe_size,
459 int stripe_offset, int stripe_count, int stripe_pattern)
463 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
464 stripe_offset, stripe_count, stripe_pattern,
473 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
474 int stripe_offset, int stripe_count,
475 int stripe_pattern, char *pool_name)
479 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
480 stripe_offset, stripe_count, stripe_pattern,
490 * Find the fsname, the full path, and/or an open fd.
491 * Either the fsname or path must not be NULL
493 #define WANT_PATH 0x1
494 #define WANT_FSNAME 0x2
496 #define WANT_INDEX 0x8
497 #define WANT_ERROR 0x10
498 static int get_root_path(int want, char *fsname, int *outfd, char *path,
502 char buf[PATH_MAX], mntdir[PATH_MAX];
505 int idx = 0, len = 0, mntlen, fd;
508 /* get the mount point */
509 fp = setmntent(MOUNTED, "r");
511 llapi_err(LLAPI_MSG_ERROR,
512 "setmntent(%s) failed: %s:", MOUNTED,
517 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
520 if (!llapi_is_lustre_mnt(&mnt))
523 mntlen = strlen(mnt.mnt_dir);
524 ptr = strrchr(mnt.mnt_fsname, '/');
531 if ((want & WANT_INDEX) && (idx++ != index))
534 /* Check the fsname for a match, if given */
535 if (!(want & WANT_FSNAME) && fsname != NULL &&
536 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
539 /* If the path isn't set return the first one we find */
540 if (path == NULL || strlen(path) == 0) {
541 strcpy(mntdir, mnt.mnt_dir);
542 if ((want & WANT_FSNAME) && fsname != NULL)
546 /* Otherwise find the longest matching path */
547 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
548 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
549 strcpy(mntdir, mnt.mnt_dir);
551 if ((want & WANT_FSNAME) && fsname != NULL)
560 if ((want & WANT_PATH) && path != NULL)
561 strcpy(path, mntdir);
562 if (want & WANT_FD) {
563 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
571 } else if (want & WANT_ERROR)
572 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
573 "can't find fs root for '%s': %d",
574 (want & WANT_PATH) ? fsname : path, rc);
579 * search lustre mounts
581 * Calling this function will return to the user the mount point, mntdir, and
582 * the file system name, fsname, if the user passed a buffer to this routine.
584 * The user inputs are pathname and index. If the pathname is supplied then
585 * the value of the index will be ignored. The pathname will return data if
586 * the pathname is located on a lustre mount. Index is used to pick which
587 * mount point you want in the case of multiple mounted lustre file systems.
588 * See function lfs_osts in lfs.c for a example of the index use.
590 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
593 int want = WANT_PATH, idx = -1;
599 strcpy(mntdir, pathname);
603 return get_root_path(want, fsname, NULL, mntdir, idx);
606 int llapi_search_fsname(const char *pathname, char *fsname)
608 return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
609 (char *)pathname, -1);
612 /* return the first file matching this pattern */
613 static int first_match(char *pattern, char *buffer)
617 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
620 if (glob_info.gl_pathc < 1) {
621 globfree(&glob_info);
625 strcpy(buffer, glob_info.gl_pathv[0]);
627 globfree(&glob_info);
632 * find the pool directory path under /proc
633 * (can be also used to test if a fsname is known)
635 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
638 char pattern[PATH_MAX + 1];
639 char buffer[PATH_MAX];
641 if (fsname == NULL) {
642 rc = llapi_search_fsname(pathname, buffer);
646 strcpy(pathname, fsname);
649 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
650 rc = first_match(pattern, buffer);
654 /* in fsname test mode, pool_pathname is NULL */
655 if (pool_pathname != NULL)
656 strcpy(pool_pathname, buffer);
662 * Get the list of pool members.
663 * \param poolname string of format \<fsname\>.\<poolname\>
664 * \param members caller-allocated array of char*
665 * \param list_size size of the members array
666 * \param buffer caller-allocated buffer for storing OST names
667 * \param buffer_size size of the buffer
669 * \return number of members retrieved for this pool
670 * \retval -error failure
672 int llapi_get_poolmembers(const char *poolname, char **members,
673 int list_size, char *buffer, int buffer_size)
675 char fsname[PATH_MAX + 1];
677 char pathname[PATH_MAX + 1];
678 char path[PATH_MAX + 1];
685 /* name is FSNAME.POOLNAME */
686 if (strlen(poolname) > PATH_MAX)
688 strcpy(fsname, poolname);
689 pool = strchr(fsname, '.');
696 rc = poolpath(fsname, NULL, pathname);
699 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
704 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
705 sprintf(path, "%s/%s", pathname, pool);
706 if ((fd = fopen(path, "r")) == NULL) {
707 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path);
712 while (fgets(buf, sizeof(buf), fd) != NULL) {
713 if (nb_entries >= list_size) {
718 if ((tmp = strchr(buf, '\n')) != NULL)
720 if (used + strlen(buf) + 1 > buffer_size) {
725 strcpy(buffer + used, buf);
726 members[nb_entries] = buffer + used;
727 used += strlen(buf) + 1;
737 * Get the list of pools in a filesystem.
738 * \param name filesystem name or path
739 * \param poollist caller-allocated array of char*
740 * \param list_size size of the poollist array
741 * \param buffer caller-allocated buffer for storing pool names
742 * \param buffer_size size of the buffer
744 * \return number of pools retrieved for this filesystem
745 * \retval -error failure
747 int llapi_get_poollist(const char *name, char **poollist, int list_size,
748 char *buffer, int buffer_size)
750 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
754 struct dirent *cookie = NULL;
756 unsigned int nb_entries = 0;
757 unsigned int used = 0;
760 /* initilize output array */
761 for (i = 0; i < list_size; i++)
764 /* is name a pathname ? */
765 ptr = strchr(name, '/');
767 /* only absolute pathname is supported */
770 if (!realpath(name, rname)) {
772 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
776 rc = poolpath(NULL, rname, pathname);
779 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
780 " a Lustre filesystem", name);
783 strcpy(fsname, rname);
786 strcpy(fsname, name);
787 rc = poolpath(fsname, NULL, pathname);
791 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
796 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
797 if ((dir = opendir(pathname)) == NULL) {
798 llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'",
804 rc = readdir_r(dir, &pool, &cookie);
807 llapi_err(LLAPI_MSG_ERROR,
808 "Error reading pool list for '%s'", name);
810 } else if ((rc == 0) && (cookie == NULL))
811 /* end of directory */
814 /* ignore . and .. */
815 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
818 /* check output bounds */
819 if (nb_entries >= list_size)
822 /* +2 for '.' and final '\0' */
823 if (used + strlen(pool.d_name) + strlen(fsname) + 2
827 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
828 poollist[nb_entries] = buffer + used;
829 used += strlen(pool.d_name) + strlen(fsname) + 2;
837 /* wrapper for lfs.c and obd.c */
838 int llapi_poollist(const char *name)
840 /* list of pool names (assume that pool count is smaller
842 char *list[FIND_MAX_OSTS];
844 /* fsname-OST0000_UUID < 32 char, 1 per OST */
845 int bufsize = FIND_MAX_OSTS * 32;
848 buffer = malloc(bufsize);
852 if ((name[0] == '/') || (strchr(name, '.') == NULL))
853 /* name is a path or fsname */
854 nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer,
857 /* name is a pool name (<fsname>.<poolname>) */
858 nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer,
861 for (i = 0; i < nb; i++)
862 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
865 return (nb < 0 ? nb : 0);
869 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
870 void *data, cfs_dirent_t *de);
872 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
873 #define OBD_NOT_FOUND (-1)
875 static int common_param_init(struct find_param *param)
877 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
878 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
879 llapi_err(LLAPI_MSG_ERROR,
880 "error: allocation of %d bytes for ioctl",
881 sizeof(lstat_t) + param->lumlen);
885 param->got_uuids = 0;
886 param->obdindexes = NULL;
887 param->obdindex = OBD_NOT_FOUND;
891 static void find_param_fini(struct find_param *param)
893 if (param->obdindexes)
894 free(param->obdindexes);
900 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
903 struct find_param *param = (struct find_param *)data;
908 static DIR *opendir_parent(char *path)
914 fname = strrchr(path, '/');
920 parent = opendir(path);
925 int llapi_mds_getfileinfo(char *path, DIR *parent,
926 struct lov_user_mds_data *lmd)
928 lstat_t *st = &lmd->lmd_st;
929 char *fname = strrchr(path, '/');
935 fname = (fname == NULL ? path : fname + 1);
936 /* retrieve needed file info */
937 strncpy((char *)lmd, fname,
938 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
939 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
942 if (errno == ENOTTY) {
943 /* ioctl is not supported, it is not a lustre fs.
944 * Do the regular lstat(2) instead. */
945 ret = lstat_f(path, st);
947 llapi_err(LLAPI_MSG_ERROR,
948 "error: %s: lstat failed for %s",
952 } else if (errno == ENOENT) {
953 llapi_err(LLAPI_MSG_WARN,
954 "warning: %s: %s does not exist",
958 llapi_err(LLAPI_MSG_ERROR,
959 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
968 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
969 semantic_func_t sem_init,
970 semantic_func_t sem_fini, void *data,
981 if (!d && errno != ENOTDIR) {
982 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
985 } else if (!d && !parent) {
986 /* ENOTDIR. Open the parent dir. */
987 p = opendir_parent(path);
989 GOTO(out, ret = -EINVAL);
992 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
998 while ((dent = readdir64(d)) != NULL) {
999 ((struct find_param *)data)->have_fileinfo = 0;
1001 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1004 /* Don't traverse .lustre directory */
1005 if (!(strcmp(dent->d_name, dot_lustre_name)))
1009 if ((len + dent->d_reclen + 2) > size) {
1010 llapi_err(LLAPI_MSG_ERROR,
1011 "error: %s: string buffer is too small",
1016 strcat(path, dent->d_name);
1018 if (dent->d_type == DT_UNKNOWN) {
1019 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1021 ret = llapi_mds_getfileinfo(path, d,
1022 ((struct find_param *)data)->lmd);
1024 ((struct find_param *)data)->have_fileinfo = 1;
1026 llapi_filetype_dir_table[st->st_mode &
1033 switch (dent->d_type) {
1035 llapi_err(LLAPI_MSG_ERROR,
1036 "error: %s: '%s' is UNKNOWN type %d",
1037 __FUNCTION__, dent->d_name, dent->d_type);
1040 ret = llapi_semantic_traverse(path, size, d, sem_init,
1041 sem_fini, data, dent);
1048 ret = sem_init(path, d, NULL, data, dent);
1052 if (sem_fini && ret == 0)
1053 sem_fini(path, d, NULL, data, dent);
1061 sem_fini(path, parent, d, data, de);
1070 static int param_callback(char *path, semantic_func_t sem_init,
1071 semantic_func_t sem_fini, struct find_param *param)
1073 int ret, len = strlen(path);
1076 if (len > PATH_MAX) {
1077 llapi_err(LLAPI_MSG_ERROR, "Path name '%s' is too long", path);
1081 buf = (char *)malloc(PATH_MAX + 1);
1085 ret = common_param_init(param);
1090 strncpy(buf, path, PATH_MAX + 1);
1091 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1092 sem_fini, param, NULL);
1094 find_param_fini(param);
1096 return ret < 0 ? ret : 0;
1099 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1101 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1104 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
1109 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1113 fd = open(path, O_RDONLY);
1116 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1120 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1128 * If uuidp is NULL, return the number of available obd uuids.
1129 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1130 * there are more OSTs then allocated to uuidp, then an error is returned with
1131 * the ost_count set to number of available obd uuids.
1133 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1135 struct obd_uuid lov_name;
1138 int rc = 0, index = 0;
1140 /* Get the lov name */
1141 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
1145 /* Now get the ost uuids from /proc */
1146 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1148 fp = fopen(buf, "r");
1151 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
1155 while (fgets(buf, sizeof(buf), fp) != NULL) {
1156 if (uuidp && (index < *ost_count)) {
1157 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1165 if (uuidp && (index >= *ost_count))
1172 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1177 root = opendir(mnt);
1179 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1184 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1190 /* Check if user specified value matches a real uuid. Ignore _UUID,
1191 * -osc-4ba41334, other trailing gunk in comparison.
1192 * @param real_uuid ends in "_UUID"
1193 * @param search_uuid may or may not end in "_UUID"
1195 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1197 int cmplen = strlen(real_uuid) - 5;
1199 if ((strlen(search_uuid) > cmplen) && isxdigit(search_uuid[cmplen])) {
1200 /* OST00000003 doesn't match OST0000 */
1201 llapi_err(LLAPI_MSG_ERROR, "Bad UUID format '%s'", search_uuid);
1205 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1208 /* Here, param->obduuid points to a single obduuid, the index of which is
1209 * returned in param->obdindex */
1210 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1212 struct obd_uuid lov_uuid;
1213 char uuid[sizeof(struct obd_uuid)];
1218 if (param->got_uuids)
1221 /* Get the lov name */
1222 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
1224 if (errno != ENOTTY) {
1226 llapi_err(LLAPI_MSG_ERROR,
1227 "error: can't get lov name: %s", dname);
1234 param->got_uuids = 1;
1236 /* Now get the ost uuids from /proc */
1237 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1239 fp = fopen(buf, "r");
1242 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
1246 if (!param->obduuid && !param->quiet && !param->obds_printed)
1247 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
1249 while (fgets(buf, sizeof(buf), fp) != NULL) {
1250 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1253 if (param->obduuid) {
1254 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1255 param->obdindex = index;
1258 } else if (!param->quiet && !param->obds_printed) {
1259 /* Print everything */
1260 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1263 param->obds_printed = 1;
1267 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1268 llapi_err_noerrno(LLAPI_MSG_ERROR,
1269 "error: %s: unknown obduuid: %s",
1270 __FUNCTION__, param->obduuid->uuid);
1277 /* In this case, param->obduuid will be an array of obduuids and
1278 * obd index for all these obduuids will be returned in
1279 * param->obdindexes */
1280 static int setup_obd_indexes(DIR *dir, struct find_param *param)
1282 struct obd_uuid *uuids = NULL;
1283 int obdcount = INIT_ALLOC_NUM_OSTS;
1284 int ret, obd_valid = 0, obdnum, i;
1286 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1287 sizeof(struct obd_uuid));
1292 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
1295 struct obd_uuid *uuids_temp;
1297 if (ret == -EOVERFLOW) {
1298 uuids_temp = realloc(uuids, obdcount *
1299 sizeof(struct obd_uuid));
1300 if (uuids_temp != NULL)
1301 goto retry_get_uuids;
1306 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
1310 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
1311 if (param->obdindexes == NULL)
1314 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
1315 for (i = 0; i < obdcount; i++) {
1316 if (llapi_uuid_match(uuids[i].uuid,
1317 param->obduuid[obdnum].uuid)) {
1318 param->obdindexes[obdnum] = i;
1323 if (i >= obdcount) {
1324 param->obdindexes[obdnum] = OBD_NOT_FOUND;
1325 llapi_err_noerrno(LLAPI_MSG_ERROR,
1326 "error: %s: unknown obduuid: %s",
1328 param->obduuid[obdnum].uuid);
1334 param->obdindex = OBD_NOT_FOUND;
1336 param->obdindex = obd_valid;
1338 param->got_uuids = 1;
1343 static int cb_ostlist(char *path, DIR *parent, DIR *d, void *data,
1344 struct dirent64 *de)
1346 struct find_param *param = (struct find_param *)data;
1348 LASSERT(parent != NULL || d != NULL);
1351 return setup_obd_uuid(d ? d : parent, path, param);
1354 int llapi_ostlist(char *path, struct find_param *param)
1356 return param_callback(path, cb_ostlist, cb_common_fini, param);
1359 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1360 int is_dir, int verbose, int depth,
1363 char *prefix = is_dir ? "" : "lmm_";
1364 char nl = is_dir ? ' ' : '\n';
1366 if (is_dir && lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
1367 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
1368 if (verbose & VERBOSE_DETAIL)
1369 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1372 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1373 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1375 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1376 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1378 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
1379 lum->lmm_object_gr);
1380 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1381 lum->lmm_object_id);
1384 if (verbose & VERBOSE_COUNT) {
1385 if (verbose & ~VERBOSE_COUNT)
1386 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1388 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1389 (__s16)lum->lmm_stripe_count, nl);
1392 if (verbose & VERBOSE_SIZE) {
1393 if (verbose & ~VERBOSE_SIZE)
1394 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1396 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
1400 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1401 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1402 lum->lmm_pattern, nl);
1405 if (verbose & VERBOSE_OFFSET) {
1406 if (verbose & ~VERBOSE_OFFSET)
1407 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1409 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1410 lum->lmm_objects[0].l_ost_idx, nl);
1413 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1414 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s", pool_name);
1418 if (is_dir && (verbose != VERBOSE_OBJID))
1419 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1422 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1423 struct lov_user_ost_data_v1 *objects,
1424 char *path, int is_dir,
1425 int obdindex, int depth, int header)
1427 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1430 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1431 if (obdindex == objects[i].l_ost_idx) {
1439 lov_dump_user_lmm_header(lum, path, is_dir, header, depth,
1442 if (!is_dir && (header & VERBOSE_OBJID)) {
1444 llapi_printf(LLAPI_MSG_NORMAL,
1445 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1447 for (i = 0; i < lum->lmm_stripe_count; i++) {
1448 int idx = objects[i].l_ost_idx;
1449 long long oid = objects[i].l_object_id;
1450 long long gr = objects[i].l_object_gr;
1451 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1452 llapi_printf(LLAPI_MSG_NORMAL,
1453 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1455 obdindex == idx ? " *" : "");
1457 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1461 void llapi_lov_dump_user_lmm(struct find_param *param,
1462 char *path, int is_dir)
1464 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1465 case LOV_USER_MAGIC_V1:
1466 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1467 param->lmd->lmd_lmm.lmm_objects,
1469 param->obdindex, param->maxdepth,
1472 case LOV_USER_MAGIC_V3: {
1473 char pool_name[LOV_MAXPOOLNAME + 1];
1474 struct lov_user_ost_data_v1 *objects;
1475 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1477 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1478 pool_name[LOV_MAXPOOLNAME] = '\0';
1479 objects = lmmv3->lmm_objects;
1480 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1481 objects, path, is_dir,
1482 param->obdindex, param->maxdepth,
1487 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1488 "(expecting one of %#x %#x %#x)\n",
1489 *(__u32 *)¶m->lmd->lmd_lmm,
1490 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
1495 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1501 fname = strrchr(path, '/');
1503 /* It should be a file (or other non-directory) */
1504 if (fname == NULL) {
1505 dname = (char *)malloc(2);
1509 fname = (char *)path;
1511 dname = (char *)malloc(fname - path + 1);
1514 strncpy(dname, path, fname - path);
1515 dname[fname - path] = '\0';
1519 if ((fd = open(dname, O_RDONLY)) == -1) {
1525 strcpy((char *)lum, fname);
1526 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1529 if (close(fd) == -1 && rc == 0)
1537 int llapi_file_lookup(int dirfd, const char *name)
1539 struct obd_ioctl_data data = { 0 };
1544 if (dirfd < 0 || name == NULL)
1547 data.ioc_version = OBD_IOCTL_VERSION;
1548 data.ioc_len = sizeof(data);
1549 data.ioc_inlbuf1 = (char *)name;
1550 data.ioc_inllen1 = strlen(name) + 1;
1552 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1554 llapi_err(LLAPI_MSG_ERROR,
1555 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1560 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1563 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1564 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1566 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1567 * The table below gives the answers for the specified parameters (value and
1568 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1569 * --------------------------------------
1570 * 1 | file > limit; sign > 0 | -1 / -1 |
1571 * 2 | file = limit; sign > 0 | ? / 1 |
1572 * 3 | file < limit; sign > 0 | ? / 1 |
1573 * 4 | file > limit; sign = 0 | -1 / -1 |
1574 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1575 * 6 | file < limit; sign = 0 | ? / -1 |
1576 * 7 | file > limit; sign < 0 | 1 / 1 |
1577 * 8 | file = limit; sign < 0 | ? / -1 |
1578 * 9 | file < limit; sign < 0 | ? / -1 |
1579 * --------------------------------------
1580 * Note: 5th actually means that the value is within the interval
1581 * (limit - margin, limit]. */
1582 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1583 unsigned long long margin, int mds)
1591 if (file <= limit && file + margin > limit)
1593 if (file + margin <= limit)
1594 return mds ? 0 : -1;
1607 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1608 * Return -1 or 1 if file timestamp does not or does match the given criteria
1609 * correspondingly. Return 0 if the MDS time is being checked and there are
1610 * attributes on OSTs and it is not yet clear if the timespamp matches.
1612 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1613 * updated timestamps. */
1614 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1619 /* Check if file is accepted. */
1621 ret = find_value_cmp(st->st_atime, param->atime,
1622 param->asign, 24 * 60 * 60, mds);
1629 ret = find_value_cmp(st->st_mtime, param->mtime,
1630 param->msign, 24 * 60 * 60, mds);
1634 /* If the previous check matches, but this one is not yet clear,
1635 * we should return 0 to do an RPC on OSTs. */
1641 ret = find_value_cmp(st->st_ctime, param->ctime,
1642 param->csign, 24 * 60 * 60, mds);
1646 /* If the previous check matches, but this one is not yet clear,
1647 * we should return 0 to do an RPC on OSTs. */
1655 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1656 void *data, cfs_dirent_t *de)
1658 struct find_param *param = (struct find_param *)data;
1659 int decision = 1; /* 1 is accepted; -1 is rejected. */
1660 lstat_t *st = ¶m->lmd->lmd_st;
1662 int checked_type = 0;
1665 LASSERT(parent != NULL || dir != NULL);
1667 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1669 /* If a regular expression is presented, make the initial decision */
1670 if (param->pattern != NULL) {
1671 char *fname = strrchr(path, '/');
1672 fname = (fname == NULL ? path : fname + 1);
1673 ret = fnmatch(param->pattern, fname, 0);
1674 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1675 (ret == 0 && param->exclude_pattern))
1679 /* See if we can check the file type from the dirent. */
1680 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1681 de->d_type < DT_MAX) {
1683 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1684 if (param->exclude_type)
1687 if (!param->exclude_type)
1693 /* If a time or OST should be checked, the decision is not taken yet. */
1694 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1699 /* Request MDS for the stat info. */
1700 if (param->have_fileinfo == 0) {
1702 /* retrieve needed file info */
1703 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1704 (void *)param->lmd);
1706 char *fname = strrchr(path, '/');
1707 fname = (fname == NULL ? path : fname + 1);
1709 /* retrieve needed file info */
1710 strncpy((char *)param->lmd, fname, param->lumlen);
1711 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1712 (void *)param->lmd);
1717 if (errno == ENOTTY) {
1718 /* ioctl is not supported, it is not a lustre fs.
1719 * Do the regular lstat(2) instead. */
1721 ret = lstat_f(path, st);
1723 llapi_err(LLAPI_MSG_ERROR,
1724 "error: %s: lstat failed for %s",
1725 __FUNCTION__, path);
1728 } else if (errno == ENOENT) {
1729 llapi_err(LLAPI_MSG_WARN,
1730 "warning: %s: %s does not exist",
1731 __FUNCTION__, path);
1734 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1735 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1736 "IOC_MDC_GETFILEINFO", path);
1741 if (param->type && !checked_type) {
1742 if ((st->st_mode & S_IFMT) == param->type) {
1743 if (param->exclude_type)
1746 if (!param->exclude_type)
1752 if (param->obduuid) {
1753 if (lustre_fs && param->got_uuids &&
1754 param->st_dev != st->st_dev) {
1755 /* A lustre/lustre mount point is crossed. */
1756 param->got_uuids = 0;
1757 param->obds_printed = 0;
1758 param->obdindex = OBD_NOT_FOUND;
1761 if (lustre_fs && !param->got_uuids) {
1762 ret = setup_obd_indexes(dir ? dir : parent, param);
1766 param->st_dev = st->st_dev;
1767 } else if (!lustre_fs && param->got_uuids) {
1768 /* A lustre/non-lustre mount point is crossed. */
1769 param->got_uuids = 0;
1770 param->obdindex = OBD_NOT_FOUND;
1774 /* If an OBD UUID is specified but no one matches, skip this file. */
1775 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1778 /* If a OST UUID is given, and some OST matches, check it here. */
1779 if (param->obdindex != OBD_NOT_FOUND) {
1780 if (!S_ISREG(st->st_mode))
1783 /* Only those files should be accepted, which have a
1784 * stripe on the specified OST. */
1785 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1789 struct lov_user_ost_data_v1 *lmm_objects;
1791 if (param->lmd->lmd_lmm.lmm_magic ==
1792 LOV_USER_MAGIC_V3) {
1793 struct lov_user_md_v3 *lmmv3 =
1794 (void *)¶m->lmd->lmd_lmm;
1796 lmm_objects = lmmv3->lmm_objects;
1798 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1802 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1803 for (j = 0; j < param->num_obds; j++) {
1804 if (param->obdindexes[j] ==
1805 lmm_objects[i].l_ost_idx)
1810 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1815 if (param->check_uid) {
1816 if (st->st_uid == param->uid) {
1817 if (param->exclude_uid)
1820 if (!param->exclude_uid)
1825 if (param->check_gid) {
1826 if (st->st_gid == param->gid) {
1827 if (param->exclude_gid)
1830 if (!param->exclude_gid)
1835 if (param->check_pool) {
1836 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1838 /* empty requested pool is taken as no pool search => V1 */
1839 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1840 (param->poolname[0] == '\0')) ||
1841 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1842 (strncmp(lmmv3->lmm_pool_name,
1843 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1844 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1845 (strcmp(param->poolname, "*") == 0))) {
1846 if (param->exclude_pool)
1849 if (!param->exclude_pool)
1854 /* Check the time on mds. */
1858 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1859 param->lmd->lmd_lmm.lmm_stripe_count)
1861 decision = find_time_check(st, param, for_mds);
1865 /* If file still fits the request, ask ost for updated info.
1866 The regular stat is almost of the same speed as some new
1867 'glimpse-size-ioctl'. */
1868 if (!decision && S_ISREG(st->st_mode) &&
1869 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1870 if (param->obdindex != OBD_NOT_FOUND) {
1871 /* Check whether the obd is active or not, if it is
1872 * not active, just print the object affected by this
1875 struct obd_statfs stat_buf;
1876 struct obd_uuid uuid_buf;
1878 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1879 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1880 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1881 param->obdindex, &stat_buf,
1884 if (ret == -ENODATA || ret == -ENODEV
1887 llapi_printf(LLAPI_MSG_NORMAL,
1888 "obd_uuid: %s failed %s ",
1889 param->obduuid->uuid,
1895 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1896 (void *)param->lmd);
1897 } else if (parent) {
1898 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1899 (void *)param->lmd);
1903 if (errno == ENOENT) {
1904 llapi_err(LLAPI_MSG_ERROR,
1905 "warning: %s: %s does not exist",
1906 __FUNCTION__, path);
1909 llapi_err(LLAPI_MSG_ERROR,
1910 "%s: IOC_LOV_GETINFO on %s failed",
1911 __FUNCTION__, path);
1916 /* Check the time on osc. */
1917 decision = find_time_check(st, param, 0);
1923 decision = find_value_cmp(st->st_size, param->size,
1924 param->size_sign, param->size_units,
1928 if (decision != -1) {
1929 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1931 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1933 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1937 /* Do not get down anymore? */
1938 if (param->depth == param->maxdepth)
1945 int llapi_find(char *path, struct find_param *param)
1947 return param_callback(path, cb_find_init, cb_common_fini, param);
1950 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1953 struct find_param *param = (struct find_param *)data;
1956 LASSERT(parent != NULL || d != NULL);
1958 if (param->obduuid) {
1960 ret = setup_obd_uuid(d ? d : parent, path, param);
1966 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1967 (void *)¶m->lmd->lmd_lmm);
1968 } else if (parent) {
1969 char *fname = strrchr(path, '/');
1970 fname = (fname == NULL ? path : fname + 1);
1972 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1973 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1974 (void *)¶m->lmd->lmd_lmm);
1978 if (errno == ENODATA) {
1979 if (!param->obduuid)
1980 llapi_printf(LLAPI_MSG_NORMAL,
1981 "%s has no stripe info\n", path);
1983 } else if (errno == ENOTTY) {
1984 llapi_err(LLAPI_MSG_ERROR,
1985 "%s: '%s' not on a Lustre fs?",
1986 __FUNCTION__, path);
1987 } else if (errno == ENOENT) {
1988 llapi_err(LLAPI_MSG_WARN,
1989 "warning: %s: %s does not exist",
1990 __FUNCTION__, path);
1993 llapi_err(LLAPI_MSG_ERROR,
1994 "error: %s: %s failed for %s",
1995 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1996 "IOC_MDC_GETFILESTRIPE", path);
2002 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2004 /* Do not get down anymore? */
2005 if (param->depth == param->maxdepth)
2012 int llapi_getstripe(char *path, struct find_param *param)
2014 return param_callback(path, cb_getstripe, cb_common_fini, param);
2017 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2018 struct obd_statfs *stat_buf,
2019 struct obd_uuid *uuid_buf)
2022 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2024 struct obd_ioctl_data data = { 0 };
2027 data.ioc_inlbuf1 = (char *)&type;
2028 data.ioc_inllen1 = sizeof(__u32);
2029 data.ioc_inlbuf2 = (char *)&index;
2030 data.ioc_inllen2 = sizeof(__u32);
2031 data.ioc_pbuf1 = (char *)stat_buf;
2032 data.ioc_plen1 = sizeof(struct obd_statfs);
2033 data.ioc_pbuf2 = (char *)uuid_buf;
2034 data.ioc_plen2 = sizeof(struct obd_uuid);
2036 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
2037 llapi_err(LLAPI_MSG_ERROR,
2038 "llapi_obd_statfs: error packing ioctl data");
2042 fd = open(path, O_RDONLY);
2043 if (errno == EISDIR)
2044 fd = open(path, O_DIRECTORY | O_RDONLY);
2047 rc = errno ? -errno : -EBADF;
2048 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2049 __FUNCTION__, path);
2052 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2054 rc = errno ? -errno : -EINVAL;
2060 #define MAX_STRING_SIZE 128
2061 #define DEVICES_LIST "/proc/fs/lustre/devices"
2063 int llapi_ping(char *obd_type, char *obd_name)
2065 char path[MAX_STRING_SIZE];
2069 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2070 obd_type, obd_name);
2072 fd = open(path, O_WRONLY);
2075 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2079 rc = write(fd, buf, 1);
2087 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2089 char buf[MAX_STRING_SIZE];
2090 FILE *fp = fopen(DEVICES_LIST, "r");
2095 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2099 while (fgets(buf, sizeof(buf), fp) != NULL) {
2100 char *obd_type_name = NULL;
2101 char *obd_name = NULL;
2102 char *obd_uuid = NULL;
2104 struct obd_ioctl_data datal = { 0, };
2105 struct obd_statfs osfs_buffer;
2107 while(bufp[0] == ' ')
2110 for(i = 0; i < 3; i++) {
2111 obd_type_name = strsep(&bufp, " ");
2113 obd_name = strsep(&bufp, " ");
2114 obd_uuid = strsep(&bufp, " ");
2116 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2118 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2119 datal.ioc_plen1 = sizeof(osfs_buffer);
2121 for (i = 0; i < type_num; i++) {
2122 if (strcmp(obd_type_name, obd_type[i]) != 0)
2125 cb(obd_type_name, obd_name, obd_uuid, args);
2132 static void do_target_check(char *obd_type_name, char *obd_name,
2133 char *obd_uuid, void *args)
2137 rc = llapi_ping(obd_type_name, obd_name);
2138 if (rc == ENOTCONN) {
2139 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2141 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2143 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2147 int llapi_target_check(int type_num, char **obd_type, char *dir)
2149 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2152 #undef MAX_STRING_SIZE
2154 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2156 char raw[OBD_MAX_IOCTL_BUFFER];
2157 char out[LLOG_CHUNK_SIZE];
2159 struct obd_ioctl_data data = { 0 };
2164 sprintf(key, "%s", keyword);
2165 memset(raw, 0, sizeof(raw));
2166 memset(out, 0, sizeof(out));
2167 data.ioc_inlbuf1 = key;
2168 data.ioc_inllen1 = strlen(key) + 1;
2170 data.ioc_inlbuf2 = node_name;
2171 data.ioc_inllen2 = strlen(node_name) + 1;
2173 data.ioc_pbuf1 = out;
2174 data.ioc_plen1 = sizeof(out);
2175 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2179 root = opendir(dir);
2182 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2186 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2188 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2190 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2196 /* Is this a lustre fs? */
2197 int llapi_is_lustre_mnttype(const char *type)
2199 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2202 /* Is this a lustre client fs? */
2203 int llapi_is_lustre_mnt(struct mntent *mnt)
2205 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2206 strstr(mnt->mnt_fsname, ":/") != NULL);
2209 int llapi_quotacheck(char *mnt, int check_type)
2214 root = opendir(mnt);
2216 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2220 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2226 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2232 root = opendir(mnt);
2234 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2239 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2243 if (poll_intvl < 30)
2251 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2256 root = opendir(mnt);
2258 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2262 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2268 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2271 struct find_param *param = (struct find_param *)data;
2275 LASSERT(parent != NULL || d != NULL);
2278 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2279 (void *)param->lmd);
2280 } else if (parent) {
2281 char *fname = strrchr(path, '/');
2282 fname = (fname == NULL ? path : fname + 1);
2284 strncpy((char *)param->lmd, fname, param->lumlen);
2285 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2286 (void *)param->lmd);
2292 if (errno == ENODATA) {
2293 if (!param->obduuid && !param->quiet)
2294 llapi_err(LLAPI_MSG_ERROR,
2295 "%s has no stripe info", path);
2297 } else if (errno == ENOENT) {
2298 llapi_err(LLAPI_MSG_ERROR,
2299 "warning: %s: %s does not exist",
2300 __FUNCTION__, path);
2302 } else if (errno != EISDIR) {
2304 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2305 d ? "LL_IOC_MDC_GETINFO" :
2306 "IOC_MDC_GETFILEINFO", path);
2311 st = ¶m->lmd->lmd_st;
2313 /* libc chown() will do extra check, and if the real owner is
2314 * the same as the ones to set, it won't fall into kernel, so
2315 * invoke syscall directly. */
2316 rc = syscall(SYS_chown, path, -1, -1);
2318 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2320 rc = chmod(path, st->st_mode);
2322 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2328 int llapi_quotachown(char *path, int flag)
2330 struct find_param param;
2332 memset(¶m, 0, sizeof(param));
2333 param.recursive = 1;
2337 return param_callback(path, cb_quotachown, NULL, ¶m);
2343 #include <sys/wait.h>
2347 static int rmtacl_notify(int ops)
2351 int found = 0, fd, rc;
2353 fp = setmntent(MOUNTED, "r");
2355 perror("setmntent");
2360 mnt = getmntent(fp);
2364 if (!llapi_is_lustre_mnt(mnt))
2367 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2373 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2385 static char *next_token(char *p, int div)
2391 while (*p && *p != ':' && !isspace(*p))
2394 while (*p == ':' || isspace(*p))
2397 return *p ? p : NULL;
2400 static int rmtacl_name2id(char *name, int is_user)
2405 if ((pw = getpwnam(name)) == NULL)
2408 return (int)(pw->pw_uid);
2412 if ((gr = getgrnam(name)) == NULL)
2415 return (int)(gr->gr_gid);
2419 static int isodigit(int c)
2421 return (c >= '0' && c <= '7') ? 1 : 0;
2425 * Whether the name is just digits string (uid/gid) already or not.
2430 static int str_is_id(char *str)
2437 if (*str == 'x' || *str == 'X') { /* for Hex. */
2438 if (!isxdigit(*(++str)))
2441 while (isxdigit(*(++str)));
2442 } else if (isodigit(*str)) { /* for Oct. */
2443 while (isodigit(*(++str)));
2445 } else if (isdigit(*str)) { /* for Dec. */
2446 while (isdigit(*(++str)));
2449 return (*str == 0) ? 1 : 0;
2459 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2461 static rmtacl_name_t rmtacl_namelist[] = {
2462 { RMTACL_OPTNAME("user:"), 1, 0 },
2463 { RMTACL_OPTNAME("group:"), 0, 0 },
2464 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2465 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2466 /* for --tabular option */
2467 { RMTACL_OPTNAME("user"), 1, 1 },
2468 { RMTACL_OPTNAME("group"), 0, 1 },
2472 static int rgetfacl_output(char *str)
2474 char *start = NULL, *end = NULL;
2475 int is_user = 0, n, id;
2482 for (rn = rmtacl_namelist; rn->name; rn++) {
2483 if(strncmp(str, rn->name, rn->length) == 0) {
2484 if (!rn->next_token)
2485 start = str + rn->length;
2487 start = next_token(str + rn->length, 0);
2488 is_user = rn->is_user;
2493 end = next_token(start, 1);
2494 if (end == NULL || start == end) {
2495 n = printf("%s", str);
2501 id = rmtacl_name2id(start, is_user);
2502 if (id == INVALID_ID) {
2503 if (str_is_id(start)) {
2505 n = printf("%s", str);
2508 } else if ((id == NOBODY_UID && is_user) ||
2509 (id == NOBODY_GID && !is_user)) {
2511 n = printf("%s", str);
2515 n = printf("%s%d%s", str, id, end);
2520 static int child_status(int status)
2522 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2525 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2538 if ((pid = fork()) < 0) {
2544 /* child process redirects its output. */
2547 if (dup2(fd[1], 1) < 0) {
2558 status = rmtacl_notify(ops);
2562 exit(execvp(argv[0], argv));
2565 /* the following is parent process */
2566 if ((fp = fdopen(fd[0], "r")) == NULL) {
2573 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2574 if (output_func(buf) < 0)
2575 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2581 if (waitpid(pid, &status, 0) < 0) {
2586 return child_status(status);
2589 int llapi_lsetfacl(int argc, char *argv[])
2591 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2594 int llapi_lgetfacl(int argc, char *argv[])
2596 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2599 int llapi_rsetfacl(int argc, char *argv[])
2601 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2604 int llapi_rgetfacl(int argc, char *argv[])
2606 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2609 int llapi_cp(int argc, char *argv[])
2613 rc = rmtacl_notify(RMT_RSETFACL);
2617 exit(execvp(argv[0], argv));
2620 int llapi_ls(int argc, char *argv[])
2624 rc = rmtacl_notify(RMT_LGETFACL);
2628 exit(execvp(argv[0], argv));
2631 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2632 * format must have %s%s, buf must be > 16
2634 static int get_mdtname(char *name, char *format, char *buf)
2636 char suffix[]="-MDT0000";
2637 int len = strlen(name);
2639 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2640 name[len - 5] = '\0';
2645 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2648 /* Not enough room to add suffix */
2649 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2650 "MDT name too long |%s|", name);
2655 return sprintf(buf, format, name, suffix);
2658 /****** Changelog API ********/
2659 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2660 struct changelog_private {
2666 /** Start reading from a changelog
2667 * @param priv Opaque private control structure
2668 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
2669 * @param device Report changes recorded on this MDT
2670 * @param startrec Report changes beginning with this record number
2671 * (just call llapi_changelog_fini when done; don't need an endrec)
2673 int llapi_changelog_start(void **priv, int flags, const char *device,
2676 struct changelog_private *cp;
2677 struct changelog_show cs = {};
2679 char pattern[PATH_MAX];
2680 char trigger[PATH_MAX];
2683 /* Find mdtname from path, fsname, mdtname, or mdtname_UUID */
2684 if (device[0] == '/') {
2685 if ((rc = llapi_search_fsname(device, mdtname)))
2687 if ((rc = get_mdtname(mdtname, "%s%s", mdtname)) < 0)
2690 if ((rc = get_mdtname((char *)device, "%s%s", mdtname)) < 0)
2694 /* Find corresponding mdc trigger */
2695 snprintf(pattern, PATH_MAX,
2696 "/proc/fs/lustre/mdc/%s-*/changelog_trigger", mdtname);
2697 rc = first_match(pattern, trigger);
2701 /* Make sure we can write the trigger */
2702 fd = open(trigger, O_WRONLY);
2706 /* Set up the receiver control struct */
2707 cp = malloc(sizeof(*cp));
2713 cp->magic = CHANGELOG_PRIV_MAGIC;
2715 /* Start the receiver */
2716 rc = libcfs_ulnl_start(&cp->lnl, 0 /* unicast */);
2720 /* We need to trigger Lustre to start sending messages now.
2721 We could send a lnl message to a kernel listener,
2722 or write into proc. Proc has the advantage of running in this
2723 context, avoiding the need for a kernel thread. */
2724 cs.cs_pid = getpid();
2725 cs.cs_startrec = startrec;
2726 cs.cs_flags = flags & CHANGELOG_FLAG_BLOCK ? LNL_FL_BLOCK : 0;
2727 if ((pid = fork()) < 0) {
2730 /* Write triggers Lustre to start sending, but it
2731 won't return until it is complete, meaning everything
2732 got shipped through lnl (or error). So we trigger it
2733 from a child process here, allowing the llapi call to
2734 return and wait for the lnl messages. */
2735 rc = write(fd, &cs, sizeof(cs));
2749 /** Finish reading from a changelog */
2750 int llapi_changelog_fini(void **priv)
2752 struct changelog_private *cp = (struct changelog_private *)*priv;
2754 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2757 libcfs_ulnl_stop(&cp->lnl);
2763 /** Read the next changelog entry
2764 * @param priv Opaque private control structure
2765 * @param rech Changelog record handle; record will be allocated here
2766 * @return 0 valid message received; rec is set
2770 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2772 struct changelog_private *cp = (struct changelog_private *)priv;
2773 struct lnl_hdr *lnlh;
2776 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2782 rc = libcfs_ulnl_msg_get(&cp->lnl, CR_MAXSIZE, LNL_TRANSPORT_CHANGELOG,
2787 if ((lnlh->lnl_transport != LNL_TRANSPORT_CHANGELOG) ||
2788 ((lnlh->lnl_msgtype != CL_RECORD) &&
2789 (lnlh->lnl_msgtype != CL_EOF))) {
2790 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2791 "Unknown changelog message type %d:%d\n",
2792 lnlh->lnl_transport, lnlh->lnl_msgtype);
2797 if (lnlh->lnl_msgtype == CL_EOF) {
2798 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
2807 /* Our message is a changelog_rec */
2808 *rech = (struct changelog_rec *)(lnlh + 1);
2813 libcfs_ulnl_msg_free(&lnlh);
2818 /** Release the changelog record when done with it. */
2819 int llapi_changelog_free(struct changelog_rec **rech)
2822 struct lnl_hdr *lnlh = (struct lnl_hdr *)*rech - 1;
2823 libcfs_ulnl_msg_free(&lnlh);
2829 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2832 struct ioc_changelog_clear data;
2835 int id, fd, index, rc;
2838 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2839 "can't purge negative records\n");
2843 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2844 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2845 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2846 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2847 "expecting id of the form '"CHANGELOG_USER_PREFIX
2848 "<num>'; got '%s'\n", idstr);
2852 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2853 if (mdtname[0] == '/') {
2855 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2856 rc = fd < 0 ? -errno : 0;
2858 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2860 ptr = fsname + strlen(fsname) - 8;
2862 index = strtol(ptr + 4, NULL, 10);
2863 rc = get_root_path(WANT_FD | WANT_ERROR, fsname, &fd, NULL, -1);
2866 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2867 "Can't open %s: %d\n", mdtname, rc);
2871 data.icc_mdtindex = index;
2873 data.icc_recno = endrec;
2874 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2876 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2882 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2883 int buflen, long long *recno, int *linkno)
2885 char path[PATH_MAX];
2887 struct getinfo_fid2path *gf;
2890 while (*fidstr == '[')
2893 sscanf(fidstr, SFID, RFID(&fid));
2894 if (!fid_is_sane(&fid)) {
2895 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2896 "bad FID format [%s], should be "DFID"\n",
2897 fidstr, (__u64)1, 2, 0);
2901 /* Take path or fsname */
2902 if (device[0] == '/') {
2903 strcpy(path, device);
2905 rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
2910 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2911 fd = open(path, O_RDONLY | O_NONBLOCK);
2915 gf = malloc(sizeof(*gf) + buflen);
2917 gf->gf_recno = *recno;
2918 gf->gf_linkno = *linkno;
2919 gf->gf_pathlen = buflen;
2920 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2922 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2924 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2925 *recno = gf->gf_recno;
2926 *linkno = gf->gf_linkno;
2934 static int path2fid_from_lma(const char *path, lustre_fid *fid)
2937 struct lustre_mdt_attrs *lma;
2940 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
2943 lma = (struct lustre_mdt_attrs *)buf;
2944 fid_le_to_cpu(fid, &lma->lma_self_fid);
2948 int llapi_path2fid(const char *path, lustre_fid *fid)
2952 memset(fid, 0, sizeof(*fid));
2953 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2955 if (errno == ELOOP) /* symbolic link */
2956 return path2fid_from_lma(path, fid);
2960 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
2961 if (rc == -EINVAL) /* char special device */
2962 rc = path2fid_from_lma(path, fid);
2968 /****** HSM Copytool API ********/
2969 #define CT_PRIV_MAGIC 0xC0BE2001
2970 struct copytool_private {
2973 int archive_num_count;
2974 int archive_nums[0];
2977 #include <libcfs/libcfs.h>
2979 /** Register a copytool
2980 * @param priv Opaque private control structure
2981 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
2982 * @param archive_num_count
2983 * @param archive_nums Which archive numbers this copytool is responsible for
2985 int llapi_copytool_start(void **priv, int flags, int archive_num_count,
2988 struct copytool_private *ct;
2991 if (archive_num_count > 0 && archive_nums == NULL) {
2992 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2993 "NULL archive numbers");
2997 ct = malloc(sizeof(*ct) +
2998 archive_num_count * sizeof(ct->archive_nums[0]));
3002 ct->magic = CT_PRIV_MAGIC;
3003 ct->archive_num_count = archive_num_count;
3004 if (ct->archive_num_count > 0)
3005 memcpy(ct->archive_nums, archive_nums, archive_num_count *
3006 sizeof(ct->archive_nums[0]));
3008 rc = libcfs_ulnl_start(&ct->lnl, LNL_GRP_HSM);
3020 /** Deregister a copytool */
3021 int llapi_copytool_fini(void **priv)
3023 struct copytool_private *ct = (struct copytool_private *)*priv;
3025 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3028 libcfs_ulnl_stop(&ct->lnl);
3034 /** Wait for the next hsm_action_list
3035 * @param priv Opaque private control structure
3036 * @param halh Action list handle, will be allocated here
3037 * @param msgsize Number of bytes in the message, will be set here
3038 * @return 0 valid message received; halh and msgsize are set
3041 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3043 struct copytool_private *ct = (struct copytool_private *)priv;
3044 struct lnl_hdr *lnlh;
3045 struct hsm_action_list *hal;
3048 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3050 if (halh == NULL || msgsize == NULL)
3053 rc = libcfs_ulnl_msg_get(&ct->lnl, HAL_MAXSIZE,
3054 LNL_TRANSPORT_HSM, &lnlh);
3058 /* Handle generic messages */
3059 if (lnlh->lnl_transport == LNL_TRANSPORT_GENERIC &&
3060 lnlh->lnl_msgtype == LNL_MSG_SHUTDOWN) {
3065 if (lnlh->lnl_transport != LNL_TRANSPORT_HSM ||
3066 lnlh->lnl_msgtype != HMT_ACTION_LIST) {
3067 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3068 "Unknown HSM message type %d:%d\n",
3069 lnlh->lnl_transport, lnlh->lnl_msgtype);
3074 /* Our message is an hsm_action_list */
3076 hal = (struct hsm_action_list *)(lnlh + 1);
3078 /* Check that we have registered for this archive # */
3079 for (rc = 0; rc < ct->archive_num_count; rc++) {
3080 if (hal->hal_archive_num == ct->archive_nums[rc])
3083 if (rc >= ct->archive_num_count) {
3084 CDEBUG(D_INFO, "This copytool does not service archive #%d, "
3085 "ignoring this request.\n", hal->hal_archive_num);
3091 *msgsize = lnlh->lnl_msglen - sizeof(*lnlh);
3095 libcfs_ulnl_msg_free(&lnlh);
3101 /** Release the action list when done with it. */
3102 int llapi_copytool_free(struct hsm_action_list **hal)
3105 struct lnl_hdr *lnlh = (struct lnl_hdr *)*hal - 1;
3106 libcfs_ulnl_msg_free(&lnlh);