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 if ((want & WANT_INDEX) && (idx++ != index))
526 mntlen = strlen(mnt.mnt_dir);
527 ptr = strrchr(mnt.mnt_fsname, '/');
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 /* Given a path, find the corresponding Lustre fsname */
607 int llapi_search_fsname(const char *pathname, char *fsname)
609 char *path = (char*)pathname, buf[PATH_MAX + 1];
611 if (pathname[0] != '/') { /* Need a absolute path */
612 memset(buf, '\0', sizeof(buf));
613 if (realpath(pathname, buf) == NULL) {
614 llapi_err(LLAPI_MSG_ERROR, "pathname '%s' cannot expand",
620 return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
624 /* return the first file matching this pattern */
625 static int first_match(char *pattern, char *buffer)
629 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
632 if (glob_info.gl_pathc < 1) {
633 globfree(&glob_info);
637 strcpy(buffer, glob_info.gl_pathv[0]);
639 globfree(&glob_info);
644 * find the pool directory path under /proc
645 * (can be also used to test if a fsname is known)
647 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
650 char pattern[PATH_MAX + 1];
651 char buffer[PATH_MAX];
653 if (fsname == NULL) {
654 rc = llapi_search_fsname(pathname, buffer);
658 strcpy(pathname, fsname);
661 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
662 rc = first_match(pattern, buffer);
666 /* in fsname test mode, pool_pathname is NULL */
667 if (pool_pathname != NULL)
668 strcpy(pool_pathname, buffer);
674 * Get the list of pool members.
675 * \param poolname string of format \<fsname\>.\<poolname\>
676 * \param members caller-allocated array of char*
677 * \param list_size size of the members array
678 * \param buffer caller-allocated buffer for storing OST names
679 * \param buffer_size size of the buffer
681 * \return number of members retrieved for this pool
682 * \retval -error failure
684 int llapi_get_poolmembers(const char *poolname, char **members,
685 int list_size, char *buffer, int buffer_size)
687 char fsname[PATH_MAX + 1];
689 char pathname[PATH_MAX + 1];
690 char path[PATH_MAX + 1];
697 /* name is FSNAME.POOLNAME */
698 if (strlen(poolname) > PATH_MAX)
700 strcpy(fsname, poolname);
701 pool = strchr(fsname, '.');
708 rc = poolpath(fsname, NULL, pathname);
711 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
716 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
717 sprintf(path, "%s/%s", pathname, pool);
718 if ((fd = fopen(path, "r")) == NULL) {
719 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path);
724 while (fgets(buf, sizeof(buf), fd) != NULL) {
725 if (nb_entries >= list_size) {
730 if ((tmp = strchr(buf, '\n')) != NULL)
732 if (used + strlen(buf) + 1 > buffer_size) {
737 strcpy(buffer + used, buf);
738 members[nb_entries] = buffer + used;
739 used += strlen(buf) + 1;
749 * Get the list of pools in a filesystem.
750 * \param name filesystem name or path
751 * \param poollist caller-allocated array of char*
752 * \param list_size size of the poollist array
753 * \param buffer caller-allocated buffer for storing pool names
754 * \param buffer_size size of the buffer
756 * \return number of pools retrieved for this filesystem
757 * \retval -error failure
759 int llapi_get_poollist(const char *name, char **poollist, int list_size,
760 char *buffer, int buffer_size)
762 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
766 struct dirent *cookie = NULL;
768 unsigned int nb_entries = 0;
769 unsigned int used = 0;
772 /* initilize output array */
773 for (i = 0; i < list_size; i++)
776 /* is name a pathname ? */
777 ptr = strchr(name, '/');
779 /* only absolute pathname is supported */
782 if (!realpath(name, rname)) {
784 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
788 rc = poolpath(NULL, rname, pathname);
791 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
792 " a Lustre filesystem", name);
795 strcpy(fsname, rname);
798 strcpy(fsname, name);
799 rc = poolpath(fsname, NULL, pathname);
803 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
808 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
809 if ((dir = opendir(pathname)) == NULL) {
810 llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'",
816 rc = readdir_r(dir, &pool, &cookie);
819 llapi_err(LLAPI_MSG_ERROR,
820 "Error reading pool list for '%s'", name);
822 } else if ((rc == 0) && (cookie == NULL))
823 /* end of directory */
826 /* ignore . and .. */
827 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
830 /* check output bounds */
831 if (nb_entries >= list_size)
834 /* +2 for '.' and final '\0' */
835 if (used + strlen(pool.d_name) + strlen(fsname) + 2
839 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
840 poollist[nb_entries] = buffer + used;
841 used += strlen(pool.d_name) + strlen(fsname) + 2;
849 /* wrapper for lfs.c and obd.c */
850 int llapi_poollist(const char *name)
852 /* list of pool names (assume that pool count is smaller
854 char *list[FIND_MAX_OSTS];
856 /* fsname-OST0000_UUID < 32 char, 1 per OST */
857 int bufsize = FIND_MAX_OSTS * 32;
860 buffer = malloc(bufsize);
864 if ((name[0] == '/') || (strchr(name, '.') == NULL))
865 /* name is a path or fsname */
866 nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer,
869 /* name is a pool name (<fsname>.<poolname>) */
870 nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer,
873 for (i = 0; i < nb; i++)
874 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
877 return (nb < 0 ? nb : 0);
881 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
882 void *data, cfs_dirent_t *de);
884 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
885 #define OBD_NOT_FOUND (-1)
887 static int common_param_init(struct find_param *param)
889 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
890 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
891 llapi_err(LLAPI_MSG_ERROR,
892 "error: allocation of %d bytes for ioctl",
893 sizeof(lstat_t) + param->lumlen);
897 param->got_uuids = 0;
898 param->obdindexes = NULL;
899 param->obdindex = OBD_NOT_FOUND;
903 static void find_param_fini(struct find_param *param)
905 if (param->obdindexes)
906 free(param->obdindexes);
912 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
915 struct find_param *param = (struct find_param *)data;
920 static DIR *opendir_parent(char *path)
926 fname = strrchr(path, '/');
932 parent = opendir(path);
937 int llapi_mds_getfileinfo(char *path, DIR *parent,
938 struct lov_user_mds_data *lmd)
940 lstat_t *st = &lmd->lmd_st;
941 char *fname = strrchr(path, '/');
947 fname = (fname == NULL ? path : fname + 1);
948 /* retrieve needed file info */
949 strncpy((char *)lmd, fname,
950 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
951 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
954 if (errno == ENOTTY) {
955 /* ioctl is not supported, it is not a lustre fs.
956 * Do the regular lstat(2) instead. */
957 ret = lstat_f(path, st);
959 llapi_err(LLAPI_MSG_ERROR,
960 "error: %s: lstat failed for %s",
964 } else if (errno == ENOENT) {
965 llapi_err(LLAPI_MSG_WARN,
966 "warning: %s: %s does not exist",
970 llapi_err(LLAPI_MSG_ERROR,
971 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
980 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
981 semantic_func_t sem_init,
982 semantic_func_t sem_fini, void *data,
993 if (!d && errno != ENOTDIR) {
994 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
997 } else if (!d && !parent) {
998 /* ENOTDIR. Open the parent dir. */
999 p = opendir_parent(path);
1001 GOTO(out, ret = -EINVAL);
1004 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1010 while ((dent = readdir64(d)) != NULL) {
1011 ((struct find_param *)data)->have_fileinfo = 0;
1013 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1016 /* Don't traverse .lustre directory */
1017 if (!(strcmp(dent->d_name, dot_lustre_name)))
1021 if ((len + dent->d_reclen + 2) > size) {
1022 llapi_err(LLAPI_MSG_ERROR,
1023 "error: %s: string buffer is too small",
1028 strcat(path, dent->d_name);
1030 if (dent->d_type == DT_UNKNOWN) {
1031 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1033 ret = llapi_mds_getfileinfo(path, d,
1034 ((struct find_param *)data)->lmd);
1036 ((struct find_param *)data)->have_fileinfo = 1;
1038 llapi_filetype_dir_table[st->st_mode &
1045 switch (dent->d_type) {
1047 llapi_err(LLAPI_MSG_ERROR,
1048 "error: %s: '%s' is UNKNOWN type %d",
1049 __func__, dent->d_name, dent->d_type);
1052 ret = llapi_semantic_traverse(path, size, d, sem_init,
1053 sem_fini, data, dent);
1060 ret = sem_init(path, d, NULL, data, dent);
1064 if (sem_fini && ret == 0)
1065 sem_fini(path, d, NULL, data, dent);
1073 sem_fini(path, parent, d, data, de);
1082 static int param_callback(char *path, semantic_func_t sem_init,
1083 semantic_func_t sem_fini, struct find_param *param)
1085 int ret, len = strlen(path);
1088 if (len > PATH_MAX) {
1089 llapi_err(LLAPI_MSG_ERROR, "Path name '%s' is too long", path);
1093 buf = (char *)malloc(PATH_MAX + 1);
1097 ret = common_param_init(param);
1102 strncpy(buf, path, PATH_MAX + 1);
1103 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1104 sem_fini, param, NULL);
1106 find_param_fini(param);
1108 return ret < 0 ? ret : 0;
1111 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1113 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1116 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
1121 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1125 fd = open(path, O_RDONLY);
1128 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1132 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1140 * If uuidp is NULL, return the number of available obd uuids.
1141 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1142 * there are more OSTs then allocated to uuidp, then an error is returned with
1143 * the ost_count set to number of available obd uuids.
1145 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1147 struct obd_uuid lov_name;
1150 int rc = 0, index = 0;
1152 /* Get the lov name */
1153 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
1157 /* Now get the ost uuids from /proc */
1158 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1160 fp = fopen(buf, "r");
1163 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
1167 while (fgets(buf, sizeof(buf), fp) != NULL) {
1168 if (uuidp && (index < *ost_count)) {
1169 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1177 if (uuidp && (index >= *ost_count))
1184 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1189 root = opendir(mnt);
1191 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1196 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1202 /* Check if user specified value matches a real uuid. Ignore _UUID,
1203 * -osc-4ba41334, other trailing gunk in comparison.
1204 * @param real_uuid ends in "_UUID"
1205 * @param search_uuid may or may not end in "_UUID"
1207 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1209 int cmplen = strlen(real_uuid);
1210 int searchlen = strlen(search_uuid);
1212 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1214 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1217 /* The UUIDs may legitimately be different lengths, if
1218 * the system was upgraded from an older version. */
1219 if (cmplen != searchlen)
1222 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1225 /* Here, param->obduuid points to a single obduuid, the index of which is
1226 * returned in param->obdindex */
1227 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1229 struct obd_uuid lov_uuid;
1230 char uuid[sizeof(struct obd_uuid)];
1235 if (param->got_uuids)
1238 /* Get the lov name */
1239 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
1241 if (errno != ENOTTY) {
1243 llapi_err(LLAPI_MSG_ERROR,
1244 "error: can't get lov name: %s", dname);
1251 param->got_uuids = 1;
1253 /* Now get the ost uuids from /proc */
1254 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
1256 fp = fopen(buf, "r");
1259 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
1263 if (!param->obduuid && !param->quiet && !param->obds_printed)
1264 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
1266 while (fgets(buf, sizeof(buf), fp) != NULL) {
1267 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1270 if (param->obduuid) {
1271 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1272 param->obdindex = index;
1275 } else if (!param->quiet && !param->obds_printed) {
1276 /* Print everything */
1277 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1280 param->obds_printed = 1;
1284 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1285 llapi_err_noerrno(LLAPI_MSG_ERROR,
1286 "error: %s: unknown obduuid: %s",
1287 __func__, param->obduuid->uuid);
1294 /* In this case, param->obduuid will be an array of obduuids and
1295 * obd index for all these obduuids will be returned in
1296 * param->obdindexes */
1297 static int setup_obd_indexes(DIR *dir, struct find_param *param)
1299 struct obd_uuid *uuids = NULL;
1300 int obdcount = INIT_ALLOC_NUM_OSTS;
1301 int ret, obd_valid = 0, obdnum, i;
1303 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1304 sizeof(struct obd_uuid));
1309 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
1312 struct obd_uuid *uuids_temp;
1314 if (ret == -EOVERFLOW) {
1315 uuids_temp = realloc(uuids, obdcount *
1316 sizeof(struct obd_uuid));
1317 if (uuids_temp != NULL)
1318 goto retry_get_uuids;
1323 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
1327 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
1328 if (param->obdindexes == NULL)
1331 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
1332 for (i = 0; i < obdcount; i++) {
1333 if (llapi_uuid_match(uuids[i].uuid,
1334 param->obduuid[obdnum].uuid)) {
1335 param->obdindexes[obdnum] = i;
1340 if (i >= obdcount) {
1341 param->obdindexes[obdnum] = OBD_NOT_FOUND;
1342 llapi_err_noerrno(LLAPI_MSG_ERROR,
1343 "error: %s: unknown obduuid: %s",
1345 param->obduuid[obdnum].uuid);
1351 param->obdindex = OBD_NOT_FOUND;
1353 param->obdindex = obd_valid;
1355 param->got_uuids = 1;
1360 static int cb_ostlist(char *path, DIR *parent, DIR *d, void *data,
1361 struct dirent64 *de)
1363 struct find_param *param = (struct find_param *)data;
1365 LASSERT(parent != NULL || d != NULL);
1368 return setup_obd_uuid(d ? d : parent, path, param);
1371 int llapi_ostlist(char *path, struct find_param *param)
1373 return param_callback(path, cb_ostlist, cb_common_fini, param);
1376 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1377 int is_dir, int verbose, int depth,
1380 char *prefix = is_dir ? "" : "lmm_";
1381 char nl = is_dir ? ' ' : '\n';
1383 if (is_dir && lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
1384 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
1385 if (verbose & VERBOSE_DETAIL)
1386 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1389 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1390 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1392 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1393 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1395 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
1396 lum->lmm_object_gr);
1397 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1398 lum->lmm_object_id);
1401 if (verbose & VERBOSE_COUNT) {
1402 if (verbose & ~VERBOSE_COUNT)
1403 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1405 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1406 (__s16)lum->lmm_stripe_count, nl);
1409 if (verbose & VERBOSE_SIZE) {
1410 if (verbose & ~VERBOSE_SIZE)
1411 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1413 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
1417 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1418 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1419 lum->lmm_pattern, nl);
1422 if (verbose & VERBOSE_OFFSET) {
1423 if (verbose & ~VERBOSE_OFFSET)
1424 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1426 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1427 lum->lmm_objects[0].l_ost_idx, nl);
1430 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1431 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s", pool_name);
1435 if (is_dir && (verbose != VERBOSE_OBJID))
1436 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1439 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1440 struct lov_user_ost_data_v1 *objects,
1441 char *path, int is_dir,
1442 int obdindex, int depth, int header)
1444 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1447 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1448 if (obdindex == objects[i].l_ost_idx) {
1456 lov_dump_user_lmm_header(lum, path, is_dir, header, depth,
1459 if (!is_dir && (header & VERBOSE_OBJID)) {
1461 llapi_printf(LLAPI_MSG_NORMAL,
1462 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1464 for (i = 0; i < lum->lmm_stripe_count; i++) {
1465 int idx = objects[i].l_ost_idx;
1466 long long oid = objects[i].l_object_id;
1467 long long gr = objects[i].l_object_gr;
1468 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1469 llapi_printf(LLAPI_MSG_NORMAL,
1470 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1472 obdindex == idx ? " *" : "");
1474 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1478 void llapi_lov_dump_user_lmm(struct find_param *param,
1479 char *path, int is_dir)
1481 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1482 case LOV_USER_MAGIC_V1:
1483 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1484 param->lmd->lmd_lmm.lmm_objects,
1486 param->obdindex, param->maxdepth,
1489 case LOV_USER_MAGIC_V3: {
1490 char pool_name[LOV_MAXPOOLNAME + 1];
1491 struct lov_user_ost_data_v1 *objects;
1492 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1494 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1495 pool_name[LOV_MAXPOOLNAME] = '\0';
1496 objects = lmmv3->lmm_objects;
1497 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1498 objects, path, is_dir,
1499 param->obdindex, param->maxdepth,
1504 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1505 "(expecting one of %#x %#x %#x)\n",
1506 *(__u32 *)¶m->lmd->lmd_lmm,
1507 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
1512 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1518 fname = strrchr(path, '/');
1520 /* It should be a file (or other non-directory) */
1521 if (fname == NULL) {
1522 dname = (char *)malloc(2);
1526 fname = (char *)path;
1528 dname = (char *)malloc(fname - path + 1);
1531 strncpy(dname, path, fname - path);
1532 dname[fname - path] = '\0';
1536 if ((fd = open(dname, O_RDONLY)) == -1) {
1542 strcpy((char *)lum, fname);
1543 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1546 if (close(fd) == -1 && rc == 0)
1554 int llapi_file_lookup(int dirfd, const char *name)
1556 struct obd_ioctl_data data = { 0 };
1561 if (dirfd < 0 || name == NULL)
1564 data.ioc_version = OBD_IOCTL_VERSION;
1565 data.ioc_len = sizeof(data);
1566 data.ioc_inlbuf1 = (char *)name;
1567 data.ioc_inllen1 = strlen(name) + 1;
1569 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1571 llapi_err(LLAPI_MSG_ERROR,
1572 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1577 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1580 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1581 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1583 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1584 * The table below gives the answers for the specified parameters (value and
1585 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1586 * --------------------------------------
1587 * 1 | file > limit; sign > 0 | -1 / -1 |
1588 * 2 | file = limit; sign > 0 | ? / 1 |
1589 * 3 | file < limit; sign > 0 | ? / 1 |
1590 * 4 | file > limit; sign = 0 | -1 / -1 |
1591 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1592 * 6 | file < limit; sign = 0 | ? / -1 |
1593 * 7 | file > limit; sign < 0 | 1 / 1 |
1594 * 8 | file = limit; sign < 0 | ? / -1 |
1595 * 9 | file < limit; sign < 0 | ? / -1 |
1596 * --------------------------------------
1597 * Note: 5th actually means that the value is within the interval
1598 * (limit - margin, limit]. */
1599 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1600 unsigned long long margin, int mds)
1608 if (file <= limit && file + margin > limit)
1610 if (file + margin <= limit)
1611 return mds ? 0 : -1;
1624 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1625 * Return -1 or 1 if file timestamp does not or does match the given criteria
1626 * correspondingly. Return 0 if the MDS time is being checked and there are
1627 * attributes on OSTs and it is not yet clear if the timespamp matches.
1629 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1630 * updated timestamps. */
1631 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1636 /* Check if file is accepted. */
1638 ret = find_value_cmp(st->st_atime, param->atime,
1639 param->asign, 24 * 60 * 60, mds);
1646 ret = find_value_cmp(st->st_mtime, param->mtime,
1647 param->msign, 24 * 60 * 60, mds);
1651 /* If the previous check matches, but this one is not yet clear,
1652 * we should return 0 to do an RPC on OSTs. */
1658 ret = find_value_cmp(st->st_ctime, param->ctime,
1659 param->csign, 24 * 60 * 60, mds);
1663 /* If the previous check matches, but this one is not yet clear,
1664 * we should return 0 to do an RPC on OSTs. */
1672 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1673 void *data, cfs_dirent_t *de)
1675 struct find_param *param = (struct find_param *)data;
1676 int decision = 1; /* 1 is accepted; -1 is rejected. */
1677 lstat_t *st = ¶m->lmd->lmd_st;
1679 int checked_type = 0;
1682 LASSERT(parent != NULL || dir != NULL);
1684 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1686 /* If a regular expression is presented, make the initial decision */
1687 if (param->pattern != NULL) {
1688 char *fname = strrchr(path, '/');
1689 fname = (fname == NULL ? path : fname + 1);
1690 ret = fnmatch(param->pattern, fname, 0);
1691 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1692 (ret == 0 && param->exclude_pattern))
1696 /* See if we can check the file type from the dirent. */
1697 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1698 de->d_type < DT_MAX) {
1700 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1701 if (param->exclude_type)
1704 if (!param->exclude_type)
1710 /* If a time or OST should be checked, the decision is not taken yet. */
1711 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1716 /* Request MDS for the stat info. */
1717 if (param->have_fileinfo == 0) {
1719 /* retrieve needed file info */
1720 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1721 (void *)param->lmd);
1723 char *fname = strrchr(path, '/');
1724 fname = (fname == NULL ? path : fname + 1);
1726 /* retrieve needed file info */
1727 strncpy((char *)param->lmd, fname, param->lumlen);
1728 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1729 (void *)param->lmd);
1734 if (errno == ENOTTY) {
1735 /* ioctl is not supported, it is not a lustre fs.
1736 * Do the regular lstat(2) instead. */
1738 ret = lstat_f(path, st);
1740 llapi_err(LLAPI_MSG_ERROR,
1741 "error: %s: lstat failed for %s",
1745 } else if (errno == ENOENT) {
1746 llapi_err(LLAPI_MSG_WARN,
1747 "warning: %s: %s does not exist",
1751 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1752 __func__, dir ? "LL_IOC_MDC_GETINFO" :
1753 "IOC_MDC_GETFILEINFO", path);
1758 if (param->type && !checked_type) {
1759 if ((st->st_mode & S_IFMT) == param->type) {
1760 if (param->exclude_type)
1763 if (!param->exclude_type)
1769 if (param->obduuid) {
1770 if (lustre_fs && param->got_uuids &&
1771 param->st_dev != st->st_dev) {
1772 /* A lustre/lustre mount point is crossed. */
1773 param->got_uuids = 0;
1774 param->obds_printed = 0;
1775 param->obdindex = OBD_NOT_FOUND;
1778 if (lustre_fs && !param->got_uuids) {
1779 ret = setup_obd_indexes(dir ? dir : parent, param);
1783 param->st_dev = st->st_dev;
1784 } else if (!lustre_fs && param->got_uuids) {
1785 /* A lustre/non-lustre mount point is crossed. */
1786 param->got_uuids = 0;
1787 param->obdindex = OBD_NOT_FOUND;
1791 /* If an OBD UUID is specified but no one matches, skip this file. */
1792 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1795 /* If a OST UUID is given, and some OST matches, check it here. */
1796 if (param->obdindex != OBD_NOT_FOUND) {
1797 if (!S_ISREG(st->st_mode))
1800 /* Only those files should be accepted, which have a
1801 * stripe on the specified OST. */
1802 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1806 struct lov_user_ost_data_v1 *lmm_objects;
1808 if (param->lmd->lmd_lmm.lmm_magic ==
1809 LOV_USER_MAGIC_V3) {
1810 struct lov_user_md_v3 *lmmv3 =
1811 (void *)¶m->lmd->lmd_lmm;
1813 lmm_objects = lmmv3->lmm_objects;
1815 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1819 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1820 for (j = 0; j < param->num_obds; j++) {
1821 if (param->obdindexes[j] ==
1822 lmm_objects[i].l_ost_idx) {
1823 if (param->exclude_obd)
1830 if (i == param->lmd->lmd_lmm.lmm_stripe_count) {
1831 if (param->exclude_obd)
1838 if (param->check_uid) {
1839 if (st->st_uid == param->uid) {
1840 if (param->exclude_uid)
1843 if (!param->exclude_uid)
1848 if (param->check_gid) {
1849 if (st->st_gid == param->gid) {
1850 if (param->exclude_gid)
1853 if (!param->exclude_gid)
1858 if (param->check_pool) {
1859 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1861 /* empty requested pool is taken as no pool search => V1 */
1862 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1863 (param->poolname[0] == '\0')) ||
1864 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1865 (strncmp(lmmv3->lmm_pool_name,
1866 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1867 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1868 (strcmp(param->poolname, "*") == 0))) {
1869 if (param->exclude_pool)
1872 if (!param->exclude_pool)
1877 /* Check the time on mds. */
1881 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1882 param->lmd->lmd_lmm.lmm_stripe_count)
1884 decision = find_time_check(st, param, for_mds);
1888 /* If file still fits the request, ask ost for updated info.
1889 The regular stat is almost of the same speed as some new
1890 'glimpse-size-ioctl'. */
1891 if (!decision && S_ISREG(st->st_mode) &&
1892 param->lmd->lmd_lmm.lmm_stripe_count &&
1893 (param->size ||param->atime || param->mtime || param->ctime)) {
1894 if (param->obdindex != OBD_NOT_FOUND) {
1895 /* Check whether the obd is active or not, if it is
1896 * not active, just print the object affected by this
1899 struct obd_statfs stat_buf;
1900 struct obd_uuid uuid_buf;
1902 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1903 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1904 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1905 param->obdindex, &stat_buf,
1908 if (ret == -ENODATA || ret == -ENODEV
1911 llapi_printf(LLAPI_MSG_NORMAL,
1912 "obd_uuid: %s failed %s ",
1913 param->obduuid->uuid,
1919 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1920 (void *)param->lmd);
1921 } else if (parent) {
1922 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1923 (void *)param->lmd);
1927 if (errno == ENOENT) {
1928 llapi_err(LLAPI_MSG_ERROR,
1929 "warning: %s: %s does not exist",
1933 llapi_err(LLAPI_MSG_ERROR,
1934 "%s: IOC_LOV_GETINFO on %s failed",
1940 /* Check the time on osc. */
1941 decision = find_time_check(st, param, 0);
1947 decision = find_value_cmp(st->st_size, param->size,
1948 param->size_sign, param->size_units,
1952 if (decision != -1) {
1953 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1955 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1957 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1961 /* Do not get down anymore? */
1962 if (param->depth == param->maxdepth)
1969 int llapi_find(char *path, struct find_param *param)
1971 return param_callback(path, cb_find_init, cb_common_fini, param);
1975 * Get MDT number that the file/directory inode referenced
1976 * by the open fd resides on.
1977 * Return 0 and mdtidx on success, or -ve errno.
1979 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
1981 if (ioctl(fd, LL_IOC_GET_MDTIDX, &mdtidx) < 0)
1986 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
1989 struct find_param *param = (struct find_param *)data;
1993 LASSERT(parent != NULL || d != NULL);
1996 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
1997 } else if (parent) {
2000 fd = open(path, O_RDONLY);
2002 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2010 if (errno == ENODATA) {
2011 if (!param->obduuid)
2012 llapi_printf(LLAPI_MSG_NORMAL,
2013 "%s has no stripe info\n", path);
2015 } else if (errno == ENOTTY) {
2016 llapi_err(LLAPI_MSG_ERROR,
2017 "%s: '%s' not on a Lustre fs?",
2019 } else if (errno == ENOENT) {
2020 llapi_err(LLAPI_MSG_WARN,
2021 "warning: %s: %s does not exist",
2025 llapi_err(LLAPI_MSG_ERROR,
2026 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2033 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2035 llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
2039 /* Do not get down anymore? */
2040 if (param->depth == param->maxdepth)
2047 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2050 struct find_param *param = (struct find_param *)data;
2053 LASSERT(parent != NULL || d != NULL);
2055 if (param->obduuid) {
2057 ret = setup_obd_uuid(d ? d : parent, path, param);
2063 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2064 (void *)¶m->lmd->lmd_lmm);
2065 } else if (parent) {
2066 char *fname = strrchr(path, '/');
2067 fname = (fname == NULL ? path : fname + 1);
2069 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2071 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2072 (void *)¶m->lmd->lmd_lmm);
2076 if (errno == ENODATA) {
2077 if (!param->obduuid)
2078 llapi_printf(LLAPI_MSG_NORMAL,
2079 "%s has no stripe info\n", path);
2081 } else if (errno == ENOTTY) {
2082 llapi_err(LLAPI_MSG_ERROR,
2083 "%s: '%s' not on a Lustre fs?",
2085 } else if (errno == ENOENT) {
2086 llapi_err(LLAPI_MSG_WARN,
2087 "warning: %s: %s does not exist",
2091 llapi_err(LLAPI_MSG_ERROR,
2092 "error: %s: %s failed for %s",
2093 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2094 "IOC_MDC_GETFILESTRIPE", path);
2100 if (!param->get_mdt_index)
2101 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2104 /* Do not get down anymore? */
2105 if (param->depth == param->maxdepth)
2112 int llapi_getstripe(char *path, struct find_param *param)
2114 return param_callback(path, param->get_mdt_index ?
2115 cb_get_mdt_index : cb_getstripe,
2116 cb_common_fini, param);
2119 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2120 struct obd_statfs *stat_buf,
2121 struct obd_uuid *uuid_buf)
2124 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2126 struct obd_ioctl_data data = { 0 };
2129 data.ioc_inlbuf1 = (char *)&type;
2130 data.ioc_inllen1 = sizeof(__u32);
2131 data.ioc_inlbuf2 = (char *)&index;
2132 data.ioc_inllen2 = sizeof(__u32);
2133 data.ioc_pbuf1 = (char *)stat_buf;
2134 data.ioc_plen1 = sizeof(struct obd_statfs);
2135 data.ioc_pbuf2 = (char *)uuid_buf;
2136 data.ioc_plen2 = sizeof(struct obd_uuid);
2138 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
2139 llapi_err(LLAPI_MSG_ERROR,
2140 "llapi_obd_statfs: error packing ioctl data");
2144 fd = open(path, O_RDONLY);
2145 if (errno == EISDIR)
2146 fd = open(path, O_DIRECTORY | O_RDONLY);
2149 rc = errno ? -errno : -EBADF;
2150 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2154 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2156 rc = errno ? -errno : -EINVAL;
2162 #define MAX_STRING_SIZE 128
2163 #define DEVICES_LIST "/proc/fs/lustre/devices"
2165 int llapi_ping(char *obd_type, char *obd_name)
2167 char path[MAX_STRING_SIZE];
2171 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2172 obd_type, obd_name);
2174 fd = open(path, O_WRONLY);
2177 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2181 rc = write(fd, buf, 1);
2189 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2191 char buf[MAX_STRING_SIZE];
2192 FILE *fp = fopen(DEVICES_LIST, "r");
2197 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2201 while (fgets(buf, sizeof(buf), fp) != NULL) {
2202 char *obd_type_name = NULL;
2203 char *obd_name = NULL;
2204 char *obd_uuid = NULL;
2206 struct obd_ioctl_data datal = { 0, };
2207 struct obd_statfs osfs_buffer;
2209 while(bufp[0] == ' ')
2212 for(i = 0; i < 3; i++) {
2213 obd_type_name = strsep(&bufp, " ");
2215 obd_name = strsep(&bufp, " ");
2216 obd_uuid = strsep(&bufp, " ");
2218 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2220 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2221 datal.ioc_plen1 = sizeof(osfs_buffer);
2223 for (i = 0; i < type_num; i++) {
2224 if (strcmp(obd_type_name, obd_type[i]) != 0)
2227 cb(obd_type_name, obd_name, obd_uuid, args);
2234 static void do_target_check(char *obd_type_name, char *obd_name,
2235 char *obd_uuid, void *args)
2239 rc = llapi_ping(obd_type_name, obd_name);
2240 if (rc == ENOTCONN) {
2241 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2243 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2245 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2249 int llapi_target_check(int type_num, char **obd_type, char *dir)
2251 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2254 #undef MAX_STRING_SIZE
2256 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2258 char raw[OBD_MAX_IOCTL_BUFFER];
2259 char out[LLOG_CHUNK_SIZE];
2261 struct obd_ioctl_data data = { 0 };
2266 sprintf(key, "%s", keyword);
2267 memset(raw, 0, sizeof(raw));
2268 memset(out, 0, sizeof(out));
2269 data.ioc_inlbuf1 = key;
2270 data.ioc_inllen1 = strlen(key) + 1;
2272 data.ioc_inlbuf2 = node_name;
2273 data.ioc_inllen2 = strlen(node_name) + 1;
2275 data.ioc_pbuf1 = out;
2276 data.ioc_plen1 = sizeof(out);
2277 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2281 root = opendir(dir);
2284 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2288 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2290 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2292 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2298 /* Is this a lustre fs? */
2299 int llapi_is_lustre_mnttype(const char *type)
2301 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2304 /* Is this a lustre client fs? */
2305 int llapi_is_lustre_mnt(struct mntent *mnt)
2307 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2308 strstr(mnt->mnt_fsname, ":/") != NULL);
2311 int llapi_quotacheck(char *mnt, int check_type)
2316 root = opendir(mnt);
2318 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2322 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2328 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2334 root = opendir(mnt);
2336 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2341 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2345 if (poll_intvl < 30)
2353 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2358 root = opendir(mnt);
2360 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2364 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2370 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2373 struct find_param *param = (struct find_param *)data;
2377 LASSERT(parent != NULL || d != NULL);
2380 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2381 (void *)param->lmd);
2382 } else if (parent) {
2383 char *fname = strrchr(path, '/');
2384 fname = (fname == NULL ? path : fname + 1);
2386 strncpy((char *)param->lmd, fname, param->lumlen);
2387 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2388 (void *)param->lmd);
2394 if (errno == ENODATA) {
2395 if (!param->obduuid && !param->quiet)
2396 llapi_err(LLAPI_MSG_ERROR,
2397 "%s has no stripe info", path);
2399 } else if (errno == ENOENT) {
2400 llapi_err(LLAPI_MSG_ERROR,
2401 "warning: %s: %s does not exist",
2404 } else if (errno != EISDIR) {
2406 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2407 d ? "LL_IOC_MDC_GETINFO" :
2408 "IOC_MDC_GETFILEINFO", path);
2413 st = ¶m->lmd->lmd_st;
2415 /* libc chown() will do extra check, and if the real owner is
2416 * the same as the ones to set, it won't fall into kernel, so
2417 * invoke syscall directly. */
2418 rc = syscall(SYS_chown, path, -1, -1);
2420 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2422 rc = chmod(path, st->st_mode);
2424 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2430 int llapi_quotachown(char *path, int flag)
2432 struct find_param param;
2434 memset(¶m, 0, sizeof(param));
2435 param.recursive = 1;
2439 return param_callback(path, cb_quotachown, NULL, ¶m);
2445 #include <sys/wait.h>
2449 static int rmtacl_notify(int ops)
2453 int found = 0, fd, rc;
2455 fp = setmntent(MOUNTED, "r");
2457 perror("setmntent");
2462 mnt = getmntent(fp);
2466 if (!llapi_is_lustre_mnt(mnt))
2469 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2475 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2487 static char *next_token(char *p, int div)
2493 while (*p && *p != ':' && !isspace(*p))
2496 while (*p == ':' || isspace(*p))
2499 return *p ? p : NULL;
2502 static int rmtacl_name2id(char *name, int is_user)
2507 if ((pw = getpwnam(name)) == NULL)
2510 return (int)(pw->pw_uid);
2514 if ((gr = getgrnam(name)) == NULL)
2517 return (int)(gr->gr_gid);
2521 static int isodigit(int c)
2523 return (c >= '0' && c <= '7') ? 1 : 0;
2527 * Whether the name is just digits string (uid/gid) already or not.
2532 static int str_is_id(char *str)
2539 if (*str == 'x' || *str == 'X') { /* for Hex. */
2540 if (!isxdigit(*(++str)))
2543 while (isxdigit(*(++str)));
2544 } else if (isodigit(*str)) { /* for Oct. */
2545 while (isodigit(*(++str)));
2547 } else if (isdigit(*str)) { /* for Dec. */
2548 while (isdigit(*(++str)));
2551 return (*str == 0) ? 1 : 0;
2561 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2563 static rmtacl_name_t rmtacl_namelist[] = {
2564 { RMTACL_OPTNAME("user:"), 1, 0 },
2565 { RMTACL_OPTNAME("group:"), 0, 0 },
2566 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2567 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2568 /* for --tabular option */
2569 { RMTACL_OPTNAME("user"), 1, 1 },
2570 { RMTACL_OPTNAME("group"), 0, 1 },
2574 static int rgetfacl_output(char *str)
2576 char *start = NULL, *end = NULL;
2577 int is_user = 0, n, id;
2584 for (rn = rmtacl_namelist; rn->name; rn++) {
2585 if(strncmp(str, rn->name, rn->length) == 0) {
2586 if (!rn->next_token)
2587 start = str + rn->length;
2589 start = next_token(str + rn->length, 0);
2590 is_user = rn->is_user;
2595 end = next_token(start, 1);
2596 if (end == NULL || start == end) {
2597 n = printf("%s", str);
2603 id = rmtacl_name2id(start, is_user);
2604 if (id == INVALID_ID) {
2605 if (str_is_id(start)) {
2607 n = printf("%s", str);
2610 } else if ((id == NOBODY_UID && is_user) ||
2611 (id == NOBODY_GID && !is_user)) {
2613 n = printf("%s", str);
2617 n = printf("%s%d%s", str, id, end);
2622 static int child_status(int status)
2624 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2627 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2640 if ((pid = fork()) < 0) {
2646 /* child process redirects its output. */
2649 if (dup2(fd[1], 1) < 0) {
2660 status = rmtacl_notify(ops);
2664 exit(execvp(argv[0], argv));
2667 /* the following is parent process */
2668 if ((fp = fdopen(fd[0], "r")) == NULL) {
2675 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2676 if (output_func(buf) < 0)
2677 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2683 if (waitpid(pid, &status, 0) < 0) {
2688 return child_status(status);
2691 int llapi_lsetfacl(int argc, char *argv[])
2693 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2696 int llapi_lgetfacl(int argc, char *argv[])
2698 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2701 int llapi_rsetfacl(int argc, char *argv[])
2703 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2706 int llapi_rgetfacl(int argc, char *argv[])
2708 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2711 int llapi_cp(int argc, char *argv[])
2715 rc = rmtacl_notify(RMT_RSETFACL);
2719 exit(execvp(argv[0], argv));
2722 int llapi_ls(int argc, char *argv[])
2726 rc = rmtacl_notify(RMT_LGETFACL);
2730 exit(execvp(argv[0], argv));
2733 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2734 * format must have %s%s, buf must be > 16
2735 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
2736 * then buf = "lustre-MDT0000"
2738 static int get_mdtname(char *name, char *format, char *buf)
2740 char suffix[]="-MDT0000";
2741 int len = strlen(name);
2743 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2744 name[len - 5] = '\0';
2749 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2752 /* Not enough room to add suffix */
2753 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2754 "MDT name too long |%s|", name);
2759 return sprintf(buf, format, name, suffix);
2762 /** ioctl on filsystem root, with mdtindex sent as data
2763 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
2764 * \param mdtidxp pointer to integer within data to be filled in with the
2765 * mdt index (0 if no mdt is specified). NULL won't be filled.
2767 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
2774 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
2775 Open root and parse mdt index. */
2776 if (mdtname[0] == '/') {
2778 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
2779 (char *)mdtname, -1);
2781 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2783 ptr = fsname + strlen(fsname) - 8;
2785 index = strtol(ptr + 4, NULL, 10);
2786 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
2790 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2791 "Can't open %s: %d\n", mdtname, rc);
2798 rc = ioctl(fd, opc, data);
2799 if (rc && want_error)
2800 llapi_err(LLAPI_MSG_ERROR, "ioctl %d err %d", opc, rc);
2806 /****** Changelog API ********/
2808 static int changelog_ioctl(const char *mdtname, int opc, int id,
2809 long long recno, int flags)
2811 struct ioc_changelog data;
2815 data.icc_recno = recno;
2816 data.icc_flags = flags;
2817 idx = (int *)(&data.icc_mdtindex);
2819 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
2822 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2823 struct changelog_private {
2826 lustre_kernelcomm kuc;
2830 /** Start reading from a changelog
2831 * @param priv Opaque private control structure
2832 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
2833 * @param device Report changes recorded on this MDT
2834 * @param startrec Report changes beginning with this record number
2835 * (just call llapi_changelog_fini when done; don't need an endrec)
2837 int llapi_changelog_start(void **priv, int flags, const char *device,
2840 struct changelog_private *cp;
2843 /* Set up the receiver control struct */
2844 cp = malloc(sizeof(*cp));
2848 cp->buf = malloc(CR_MAXSIZE);
2849 if (cp->buf == NULL) {
2854 cp->magic = CHANGELOG_PRIV_MAGIC;
2857 /* Set up the receiver */
2858 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
2864 /* Tell the kernel to start sending */
2865 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
2867 /* Only the kernel reference keeps the write side open */
2868 close(cp->kuc.lk_wfd);
2871 /* frees and clears priv */
2872 llapi_changelog_fini(priv);
2885 /** Finish reading from a changelog */
2886 int llapi_changelog_fini(void **priv)
2888 struct changelog_private *cp = (struct changelog_private *)*priv;
2890 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2893 libcfs_ukuc_stop(&cp->kuc);
2900 /** Read the next changelog entry
2901 * @param priv Opaque private control structure
2902 * @param rech Changelog record handle; record will be allocated here
2903 * @return 0 valid message received; rec is set
2907 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2909 struct changelog_private *cp = (struct changelog_private *)priv;
2910 struct kuc_hdr *kuch;
2913 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2919 rc = libcfs_ukuc_msg_get(&cp->kuc, cp->buf, CR_MAXSIZE,
2920 KUC_TRANSPORT_CHANGELOG);
2924 kuch = (struct kuc_hdr *)cp->buf;
2925 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
2926 ((kuch->kuc_msgtype != CL_RECORD) &&
2927 (kuch->kuc_msgtype != CL_EOF))) {
2928 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2929 "Unknown changelog message type %d:%d\n",
2930 kuch->kuc_transport, kuch->kuc_msgtype);
2935 if (kuch->kuc_msgtype == CL_EOF) {
2936 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
2945 /* Our message is a changelog_rec */
2946 *rech = (struct changelog_rec *)(kuch + 1);
2955 /** Release the changelog record when done with it. */
2956 int llapi_changelog_free(struct changelog_rec **rech)
2962 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2968 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2969 "can't purge negative records\n");
2973 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2974 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2975 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2976 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2977 "expecting id of the form '"CHANGELOG_USER_PREFIX
2978 "<num>'; got '%s'\n", idstr);
2982 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
2985 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2986 int buflen, long long *recno, int *linkno)
2988 char path[PATH_MAX];
2990 struct getinfo_fid2path *gf;
2993 while (*fidstr == '[')
2996 sscanf(fidstr, SFID, RFID(&fid));
2997 if (!fid_is_sane(&fid)) {
2998 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2999 "bad FID format [%s], should be "DFID"\n",
3000 fidstr, (__u64)1, 2, 0);
3004 /* Take path or fsname */
3005 if (device[0] == '/') {
3006 strcpy(path, device);
3008 rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
3013 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
3014 fd = open(path, O_RDONLY | O_NONBLOCK);
3018 gf = malloc(sizeof(*gf) + buflen);
3020 gf->gf_recno = *recno;
3021 gf->gf_linkno = *linkno;
3022 gf->gf_pathlen = buflen;
3023 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
3025 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
3027 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3028 *recno = gf->gf_recno;
3029 *linkno = gf->gf_linkno;
3037 static int path2fid_from_lma(const char *path, lustre_fid *fid)
3040 struct lustre_mdt_attrs *lma;
3043 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3046 lma = (struct lustre_mdt_attrs *)buf;
3047 fid_le_to_cpu(fid, &lma->lma_self_fid);
3051 int llapi_path2fid(const char *path, lustre_fid *fid)
3055 memset(fid, 0, sizeof(*fid));
3056 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3058 if (errno == ELOOP) /* symbolic link */
3059 return path2fid_from_lma(path, fid);
3063 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3064 if (rc == -EINVAL) /* char special device */
3065 rc = path2fid_from_lma(path, fid);
3071 /****** HSM Copytool API ********/
3072 #define CT_PRIV_MAGIC 0xC0BE2001
3073 struct copytool_private {
3077 lustre_kernelcomm kuc;
3081 #include <libcfs/libcfs.h>
3083 /** Register a copytool
3084 * @param[out] priv Opaque private control structure
3085 * @param fsname Lustre filesystem
3086 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3087 * @param archive_count
3088 * @param archives Which archive numbers this copytool is responsible for
3090 int llapi_copytool_start(void **priv, char *fsname, int flags,
3091 int archive_count, int *archives)
3093 struct copytool_private *ct;
3096 if (archive_count > 0 && archives == NULL) {
3097 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3098 "NULL archive numbers");
3102 ct = malloc(sizeof(*ct));
3106 ct->buf = malloc(HAL_MAXSIZE);
3107 ct->fsname = malloc(strlen(fsname) + 1);
3108 if (ct->buf == NULL || ct->fsname == NULL) {
3112 strcpy(ct->fsname, fsname);
3113 ct->magic = CT_PRIV_MAGIC;
3115 for (rc = 0; rc < archive_count; rc++) {
3116 if (archives[rc] > sizeof(ct->archives)) {
3117 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3118 "Maximum of %d archives supported",
3119 sizeof(ct->archives));
3122 ct->archives |= 1 << archives[rc];
3124 /* special case: if no archives specified, default to archive #0. */
3125 if (ct->archives == 0)
3128 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3132 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3133 ct->kuc.lk_data = ct->archives;
3134 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3136 /* Only the kernel reference keeps the write side open */
3137 close(ct->kuc.lk_wfd);
3154 /** Deregister a copytool */
3155 int llapi_copytool_fini(void **priv)
3157 struct copytool_private *ct = (struct copytool_private *)*priv;
3159 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3162 /* Tell the kernel to stop sending us messages */
3163 ct->kuc.lk_flags = LK_FLG_STOP;
3164 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3166 /* Shut down the kernelcomms */
3167 libcfs_ukuc_stop(&ct->kuc);
3176 /** Wait for the next hsm_action_list
3177 * @param priv Opaque private control structure
3178 * @param halh Action list handle, will be allocated here
3179 * @param msgsize Number of bytes in the message, will be set here
3180 * @return 0 valid message received; halh and msgsize are set
3183 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3185 struct copytool_private *ct = (struct copytool_private *)priv;
3186 struct kuc_hdr *kuch;
3187 struct hsm_action_list *hal;
3190 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3192 if (halh == NULL || msgsize == NULL)
3195 rc = libcfs_ukuc_msg_get(&ct->kuc, ct->buf, HAL_MAXSIZE,
3200 /* Handle generic messages */
3201 kuch = (struct kuc_hdr *)ct->buf;
3202 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3203 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3208 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3209 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3210 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3211 "Unknown HSM message type %d:%d\n",
3212 kuch->kuc_transport, kuch->kuc_msgtype);
3217 /* Our message is an hsm_action_list */
3219 hal = (struct hsm_action_list *)(kuch + 1);
3221 /* Check that we have registered for this archive # */
3222 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3223 llapi_err(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO,
3224 "Ignoring request for archive #%d (bitmask %#x)\n",
3225 hal->hal_archive_num, ct->archives);
3231 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3240 /** Release the action list when done with it. */
3241 int llapi_copytool_free(struct hsm_action_list **hal)
3247 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3252 root = opendir(mnt);
3254 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
3258 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3261 llapi_err(LLAPI_MSG_ERROR,
3262 "ioctl on %s for getting connect flags failed", mnt);