1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
52 #include <sys/ioctl.h>
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 int llapi_ostlist(char *path, struct find_param *param)
1365 dir = opendir(path);
1369 ret = setup_obd_uuid(dir, path, param);
1375 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1376 struct lov_user_ost_data_v1 *objects,
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_seq == LOV_OBJECT_GROUP_DEFAULT) {
1384 lum->lmm_object_seq = 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_seq: "LPX64"\n",
1396 lum->lmm_object_seq);
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: ",
1427 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1428 lum->lmm_stripe_offset ==
1429 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
1430 lum->lmm_stripe_offset, nl);
1432 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1433 objects[0].l_ost_idx, nl);
1436 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1437 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s", pool_name);
1441 if (is_dir && (verbose != VERBOSE_OBJID))
1442 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1445 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1446 struct lov_user_ost_data_v1 *objects,
1447 char *path, int is_dir,
1448 int obdindex, int depth, int header)
1450 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1453 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1454 if (obdindex == objects[i].l_ost_idx) {
1462 lov_dump_user_lmm_header(lum, path, objects, is_dir, header, depth,
1465 if (!is_dir && (header & VERBOSE_OBJID)) {
1467 llapi_printf(LLAPI_MSG_NORMAL,
1468 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1470 for (i = 0; i < lum->lmm_stripe_count; i++) {
1471 int idx = objects[i].l_ost_idx;
1472 long long oid = objects[i].l_object_id;
1473 long long gr = objects[i].l_object_seq;
1474 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1475 llapi_printf(LLAPI_MSG_NORMAL,
1476 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1478 obdindex == idx ? " *" : "");
1480 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1484 void llapi_lov_dump_user_lmm(struct find_param *param,
1485 char *path, int is_dir)
1487 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1488 case LOV_USER_MAGIC_V1:
1489 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1490 param->lmd->lmd_lmm.lmm_objects,
1492 param->obdindex, param->maxdepth,
1495 case LOV_USER_MAGIC_V3: {
1496 char pool_name[LOV_MAXPOOLNAME + 1];
1497 struct lov_user_ost_data_v1 *objects;
1498 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1500 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1501 pool_name[LOV_MAXPOOLNAME] = '\0';
1502 objects = lmmv3->lmm_objects;
1503 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1504 objects, path, is_dir,
1505 param->obdindex, param->maxdepth,
1510 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1511 "(expecting one of %#x %#x %#x)\n",
1512 *(__u32 *)¶m->lmd->lmd_lmm,
1513 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
1518 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1524 fname = strrchr(path, '/');
1526 /* It should be a file (or other non-directory) */
1527 if (fname == NULL) {
1528 dname = (char *)malloc(2);
1532 fname = (char *)path;
1534 dname = (char *)malloc(fname - path + 1);
1537 strncpy(dname, path, fname - path);
1538 dname[fname - path] = '\0';
1542 if ((fd = open(dname, O_RDONLY)) == -1) {
1548 strcpy((char *)lum, fname);
1549 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1552 if (close(fd) == -1 && rc == 0)
1560 int llapi_file_lookup(int dirfd, const char *name)
1562 struct obd_ioctl_data data = { 0 };
1567 if (dirfd < 0 || name == NULL)
1570 data.ioc_version = OBD_IOCTL_VERSION;
1571 data.ioc_len = sizeof(data);
1572 data.ioc_inlbuf1 = (char *)name;
1573 data.ioc_inllen1 = strlen(name) + 1;
1575 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1577 llapi_err(LLAPI_MSG_ERROR,
1578 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1583 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1586 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1587 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1589 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1590 * The table below gives the answers for the specified parameters (value and
1591 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1592 * --------------------------------------
1593 * 1 | file > limit; sign > 0 | -1 / -1 |
1594 * 2 | file = limit; sign > 0 | ? / 1 |
1595 * 3 | file < limit; sign > 0 | ? / 1 |
1596 * 4 | file > limit; sign = 0 | -1 / -1 |
1597 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1598 * 6 | file < limit; sign = 0 | ? / -1 |
1599 * 7 | file > limit; sign < 0 | 1 / 1 |
1600 * 8 | file = limit; sign < 0 | ? / -1 |
1601 * 9 | file < limit; sign < 0 | ? / -1 |
1602 * --------------------------------------
1603 * Note: 5th actually means that the value is within the interval
1604 * (limit - margin, limit]. */
1605 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1606 int negopt, unsigned long long margin, int mds)
1613 } else if (sign == 0) {
1614 if (file <= limit && file + margin >= limit)
1616 else if (file + margin <= limit)
1618 } else if (sign < 0) {
1625 return negopt ? ~ret + 1 : ret;
1628 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1629 * Return -1 or 1 if file timestamp does not or does match the given criteria
1630 * correspondingly. Return 0 if the MDS time is being checked and there are
1631 * attributes on OSTs and it is not yet clear if the timespamp matches.
1633 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1634 * updated timestamps. */
1635 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1640 /* Check if file is accepted. */
1642 ret = find_value_cmp(st->st_atime, param->atime,
1643 param->asign, param->exclude_atime,
1651 ret = find_value_cmp(st->st_mtime, param->mtime,
1652 param->msign, param->exclude_mtime,
1657 /* If the previous check matches, but this one is not yet clear,
1658 * we should return 0 to do an RPC on OSTs. */
1664 ret = find_value_cmp(st->st_ctime, param->ctime,
1665 param->csign, param->exclude_ctime,
1670 /* If the previous check matches, but this one is not yet clear,
1671 * we should return 0 to do an RPC on OSTs. */
1679 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1680 void *data, cfs_dirent_t *de)
1682 struct find_param *param = (struct find_param *)data;
1683 int decision = 1; /* 1 is accepted; -1 is rejected. */
1684 lstat_t *st = ¶m->lmd->lmd_st;
1686 int checked_type = 0;
1689 LASSERT(parent != NULL || dir != NULL);
1691 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1693 /* If a regular expression is presented, make the initial decision */
1694 if (param->pattern != NULL) {
1695 char *fname = strrchr(path, '/');
1696 fname = (fname == NULL ? path : fname + 1);
1697 ret = fnmatch(param->pattern, fname, 0);
1698 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1699 (ret == 0 && param->exclude_pattern))
1703 /* See if we can check the file type from the dirent. */
1704 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1705 de->d_type < DT_MAX) {
1707 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1708 if (param->exclude_type)
1711 if (!param->exclude_type)
1717 /* If a time or OST should be checked, the decision is not taken yet. */
1718 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1723 /* Request MDS for the stat info. */
1724 if (param->have_fileinfo == 0) {
1726 /* retrieve needed file info */
1727 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1728 (void *)param->lmd);
1730 char *fname = strrchr(path, '/');
1731 fname = (fname == NULL ? path : fname + 1);
1733 /* retrieve needed file info */
1734 strncpy((char *)param->lmd, fname, param->lumlen);
1735 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1736 (void *)param->lmd);
1741 if (errno == ENOTTY) {
1742 /* ioctl is not supported, it is not a lustre fs.
1743 * Do the regular lstat(2) instead. */
1745 ret = lstat_f(path, st);
1747 llapi_err(LLAPI_MSG_ERROR,
1748 "error: %s: lstat failed for %s",
1752 } else if (errno == ENOENT) {
1753 llapi_err(LLAPI_MSG_WARN,
1754 "warning: %s: %s does not exist",
1758 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1759 __func__, dir ? "LL_IOC_MDC_GETINFO" :
1760 "IOC_MDC_GETFILEINFO", path);
1765 if (param->type && !checked_type) {
1766 if ((st->st_mode & S_IFMT) == param->type) {
1767 if (param->exclude_type)
1770 if (!param->exclude_type)
1776 if (param->obduuid) {
1777 if (lustre_fs && param->got_uuids &&
1778 param->st_dev != st->st_dev) {
1779 /* A lustre/lustre mount point is crossed. */
1780 param->got_uuids = 0;
1781 param->obds_printed = 0;
1782 param->obdindex = OBD_NOT_FOUND;
1785 if (lustre_fs && !param->got_uuids) {
1786 ret = setup_obd_indexes(dir ? dir : parent, param);
1790 param->st_dev = st->st_dev;
1791 } else if (!lustre_fs && param->got_uuids) {
1792 /* A lustre/non-lustre mount point is crossed. */
1793 param->got_uuids = 0;
1794 param->obdindex = OBD_NOT_FOUND;
1798 /* If an OBD UUID is specified but no one matches, skip this file. */
1799 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1802 /* If a OST UUID is given, and some OST matches, check it here. */
1803 if (param->obdindex != OBD_NOT_FOUND) {
1804 if (!S_ISREG(st->st_mode))
1807 /* Only those files should be accepted, which have a
1808 * stripe on the specified OST. */
1809 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1813 struct lov_user_ost_data_v1 *lmm_objects;
1815 if (param->lmd->lmd_lmm.lmm_magic ==
1816 LOV_USER_MAGIC_V3) {
1817 struct lov_user_md_v3 *lmmv3 =
1818 (void *)¶m->lmd->lmd_lmm;
1820 lmm_objects = lmmv3->lmm_objects;
1822 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1826 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1827 for (j = 0; j < param->num_obds; j++) {
1828 if (param->obdindexes[j] ==
1829 lmm_objects[i].l_ost_idx) {
1830 if (param->exclude_obd)
1837 if (i == param->lmd->lmd_lmm.lmm_stripe_count) {
1838 if (param->exclude_obd)
1845 if (param->check_uid) {
1846 if (st->st_uid == param->uid) {
1847 if (param->exclude_uid)
1850 if (!param->exclude_uid)
1855 if (param->check_gid) {
1856 if (st->st_gid == param->gid) {
1857 if (param->exclude_gid)
1860 if (!param->exclude_gid)
1865 if (param->check_pool) {
1866 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1868 /* empty requested pool is taken as no pool search => V1 */
1869 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1870 (param->poolname[0] == '\0')) ||
1871 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1872 (strncmp(lmmv3->lmm_pool_name,
1873 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1874 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1875 (strcmp(param->poolname, "*") == 0))) {
1876 if (param->exclude_pool)
1879 if (!param->exclude_pool)
1884 /* Check the time on mds. */
1888 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1889 param->lmd->lmd_lmm.lmm_stripe_count)
1891 decision = find_time_check(st, param, for_mds);
1895 /* If file still fits the request, ask ost for updated info.
1896 The regular stat is almost of the same speed as some new
1897 'glimpse-size-ioctl'. */
1898 if (!decision && S_ISREG(st->st_mode) &&
1899 param->lmd->lmd_lmm.lmm_stripe_count &&
1900 (param->check_size ||param->atime || param->mtime || param->ctime)) {
1901 if (param->obdindex != OBD_NOT_FOUND) {
1902 /* Check whether the obd is active or not, if it is
1903 * not active, just print the object affected by this
1906 struct obd_statfs stat_buf;
1907 struct obd_uuid uuid_buf;
1909 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1910 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1911 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1912 param->obdindex, &stat_buf,
1915 if (ret == -ENODATA || ret == -ENODEV
1918 llapi_printf(LLAPI_MSG_NORMAL,
1919 "obd_uuid: %s failed %s ",
1920 param->obduuid->uuid,
1926 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1927 (void *)param->lmd);
1928 } else if (parent) {
1929 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1930 (void *)param->lmd);
1934 if (errno == ENOENT) {
1935 llapi_err(LLAPI_MSG_ERROR,
1936 "warning: %s: %s does not exist",
1940 llapi_err(LLAPI_MSG_ERROR,
1941 "%s: IOC_LOV_GETINFO on %s failed",
1947 /* Check the time on osc. */
1948 decision = find_time_check(st, param, 0);
1953 if (param->check_size)
1954 decision = find_value_cmp(st->st_size, param->size,
1955 param->size_sign, param->exclude_size,
1956 param->size_units, 0);
1959 if (decision != -1) {
1960 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1962 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1964 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1968 /* Do not get down anymore? */
1969 if (param->depth == param->maxdepth)
1976 int llapi_find(char *path, struct find_param *param)
1978 return param_callback(path, cb_find_init, cb_common_fini, param);
1982 * Get MDT number that the file/directory inode referenced
1983 * by the open fd resides on.
1984 * Return 0 and mdtidx on success, or -ve errno.
1986 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
1988 if (ioctl(fd, LL_IOC_GET_MDTIDX, &mdtidx) < 0)
1993 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
1996 struct find_param *param = (struct find_param *)data;
2000 LASSERT(parent != NULL || d != NULL);
2003 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2004 } else if (parent) {
2007 fd = open(path, O_RDONLY);
2009 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2017 if (errno == ENODATA) {
2018 if (!param->obduuid)
2019 llapi_printf(LLAPI_MSG_NORMAL,
2020 "%s has no stripe info\n", path);
2022 } else if (errno == ENOTTY) {
2023 llapi_err(LLAPI_MSG_ERROR,
2024 "%s: '%s' not on a Lustre fs?",
2026 } else if (errno == ENOENT) {
2027 llapi_err(LLAPI_MSG_WARN,
2028 "warning: %s: %s does not exist",
2032 llapi_err(LLAPI_MSG_ERROR,
2033 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2040 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2042 llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
2046 /* Do not get down anymore? */
2047 if (param->depth == param->maxdepth)
2054 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2057 struct find_param *param = (struct find_param *)data;
2060 LASSERT(parent != NULL || d != NULL);
2062 if (param->obduuid) {
2064 ret = setup_obd_uuid(d ? d : parent, path, param);
2070 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2071 (void *)¶m->lmd->lmd_lmm);
2072 } else if (parent) {
2073 char *fname = strrchr(path, '/');
2074 fname = (fname == NULL ? path : fname + 1);
2076 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2078 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2079 (void *)¶m->lmd->lmd_lmm);
2083 if (errno == ENODATA) {
2084 if (!param->obduuid)
2085 llapi_printf(LLAPI_MSG_NORMAL,
2086 "%s has no stripe info\n", path);
2088 } else if (errno == ENOTTY) {
2089 llapi_err(LLAPI_MSG_ERROR,
2090 "%s: '%s' not on a Lustre fs?",
2092 } else if (errno == ENOENT) {
2093 llapi_err(LLAPI_MSG_WARN,
2094 "warning: %s: %s does not exist",
2098 llapi_err(LLAPI_MSG_ERROR,
2099 "error: %s: %s failed for %s",
2100 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2101 "IOC_MDC_GETFILESTRIPE", path);
2107 if (!param->get_mdt_index)
2108 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2111 /* Do not get down anymore? */
2112 if (param->depth == param->maxdepth)
2119 int llapi_getstripe(char *path, struct find_param *param)
2121 return param_callback(path, param->get_mdt_index ?
2122 cb_get_mdt_index : cb_getstripe,
2123 cb_common_fini, param);
2126 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2127 struct obd_statfs *stat_buf,
2128 struct obd_uuid *uuid_buf)
2131 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2133 struct obd_ioctl_data data = { 0 };
2136 data.ioc_inlbuf1 = (char *)&type;
2137 data.ioc_inllen1 = sizeof(__u32);
2138 data.ioc_inlbuf2 = (char *)&index;
2139 data.ioc_inllen2 = sizeof(__u32);
2140 data.ioc_pbuf1 = (char *)stat_buf;
2141 data.ioc_plen1 = sizeof(struct obd_statfs);
2142 data.ioc_pbuf2 = (char *)uuid_buf;
2143 data.ioc_plen2 = sizeof(struct obd_uuid);
2145 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
2146 llapi_err(LLAPI_MSG_ERROR,
2147 "llapi_obd_statfs: error packing ioctl data");
2151 fd = open(path, O_RDONLY);
2152 if (errno == EISDIR)
2153 fd = open(path, O_DIRECTORY | O_RDONLY);
2156 rc = errno ? -errno : -EBADF;
2157 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2161 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2163 rc = errno ? -errno : -EINVAL;
2169 #define MAX_STRING_SIZE 128
2170 #define DEVICES_LIST "/proc/fs/lustre/devices"
2172 int llapi_ping(char *obd_type, char *obd_name)
2174 char path[MAX_STRING_SIZE];
2178 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2179 obd_type, obd_name);
2181 fd = open(path, O_WRONLY);
2184 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2188 rc = write(fd, buf, 1);
2196 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2198 char buf[MAX_STRING_SIZE];
2199 FILE *fp = fopen(DEVICES_LIST, "r");
2204 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2208 while (fgets(buf, sizeof(buf), fp) != NULL) {
2209 char *obd_type_name = NULL;
2210 char *obd_name = NULL;
2211 char *obd_uuid = NULL;
2213 struct obd_ioctl_data datal = { 0, };
2214 struct obd_statfs osfs_buffer;
2216 while(bufp[0] == ' ')
2219 for(i = 0; i < 3; i++) {
2220 obd_type_name = strsep(&bufp, " ");
2222 obd_name = strsep(&bufp, " ");
2223 obd_uuid = strsep(&bufp, " ");
2225 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2227 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2228 datal.ioc_plen1 = sizeof(osfs_buffer);
2230 for (i = 0; i < type_num; i++) {
2231 if (strcmp(obd_type_name, obd_type[i]) != 0)
2234 cb(obd_type_name, obd_name, obd_uuid, args);
2241 static void do_target_check(char *obd_type_name, char *obd_name,
2242 char *obd_uuid, void *args)
2246 rc = llapi_ping(obd_type_name, obd_name);
2247 if (rc == ENOTCONN) {
2248 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2250 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2252 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2256 int llapi_target_check(int type_num, char **obd_type, char *dir)
2258 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2261 #undef MAX_STRING_SIZE
2263 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2265 char raw[OBD_MAX_IOCTL_BUFFER];
2266 char out[LLOG_CHUNK_SIZE];
2268 struct obd_ioctl_data data = { 0 };
2273 sprintf(key, "%s", keyword);
2274 memset(raw, 0, sizeof(raw));
2275 memset(out, 0, sizeof(out));
2276 data.ioc_inlbuf1 = key;
2277 data.ioc_inllen1 = strlen(key) + 1;
2279 data.ioc_inlbuf2 = node_name;
2280 data.ioc_inllen2 = strlen(node_name) + 1;
2282 data.ioc_pbuf1 = out;
2283 data.ioc_plen1 = sizeof(out);
2284 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2288 root = opendir(dir);
2291 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2295 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2297 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2299 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2305 /* Is this a lustre fs? */
2306 int llapi_is_lustre_mnttype(const char *type)
2308 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2311 /* Is this a lustre client fs? */
2312 int llapi_is_lustre_mnt(struct mntent *mnt)
2314 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2315 strstr(mnt->mnt_fsname, ":/") != NULL);
2318 int llapi_quotacheck(char *mnt, int check_type)
2323 root = opendir(mnt);
2325 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2329 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2335 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2341 root = opendir(mnt);
2343 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2348 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2352 if (poll_intvl < 30)
2360 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2365 root = opendir(mnt);
2367 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2371 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2377 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2380 struct find_param *param = (struct find_param *)data;
2384 LASSERT(parent != NULL || d != NULL);
2387 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2388 (void *)param->lmd);
2389 } else if (parent) {
2390 char *fname = strrchr(path, '/');
2391 fname = (fname == NULL ? path : fname + 1);
2393 strncpy((char *)param->lmd, fname, param->lumlen);
2394 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2395 (void *)param->lmd);
2401 if (errno == ENODATA) {
2402 if (!param->obduuid && !param->quiet)
2403 llapi_err(LLAPI_MSG_ERROR,
2404 "%s has no stripe info", path);
2406 } else if (errno == ENOENT) {
2407 llapi_err(LLAPI_MSG_ERROR,
2408 "warning: %s: %s does not exist",
2411 } else if (errno != EISDIR) {
2413 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2414 d ? "LL_IOC_MDC_GETINFO" :
2415 "IOC_MDC_GETFILEINFO", path);
2420 st = ¶m->lmd->lmd_st;
2422 /* libc chown() will do extra check, and if the real owner is
2423 * the same as the ones to set, it won't fall into kernel, so
2424 * invoke syscall directly. */
2425 rc = syscall(SYS_chown, path, -1, -1);
2427 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2429 rc = chmod(path, st->st_mode);
2431 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2437 int llapi_quotachown(char *path, int flag)
2439 struct find_param param;
2441 memset(¶m, 0, sizeof(param));
2442 param.recursive = 1;
2446 return param_callback(path, cb_quotachown, NULL, ¶m);
2452 #include <sys/wait.h>
2456 static int rmtacl_notify(int ops)
2460 int found = 0, fd, rc;
2462 fp = setmntent(MOUNTED, "r");
2464 perror("setmntent");
2469 mnt = getmntent(fp);
2473 if (!llapi_is_lustre_mnt(mnt))
2476 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2482 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2494 static char *next_token(char *p, int div)
2500 while (*p && *p != ':' && !isspace(*p))
2503 while (*p == ':' || isspace(*p))
2506 return *p ? p : NULL;
2509 static int rmtacl_name2id(char *name, int is_user)
2514 if ((pw = getpwnam(name)) == NULL)
2517 return (int)(pw->pw_uid);
2521 if ((gr = getgrnam(name)) == NULL)
2524 return (int)(gr->gr_gid);
2528 static int isodigit(int c)
2530 return (c >= '0' && c <= '7') ? 1 : 0;
2534 * Whether the name is just digits string (uid/gid) already or not.
2539 static int str_is_id(char *str)
2546 if (*str == 'x' || *str == 'X') { /* for Hex. */
2547 if (!isxdigit(*(++str)))
2550 while (isxdigit(*(++str)));
2551 } else if (isodigit(*str)) { /* for Oct. */
2552 while (isodigit(*(++str)));
2554 } else if (isdigit(*str)) { /* for Dec. */
2555 while (isdigit(*(++str)));
2558 return (*str == 0) ? 1 : 0;
2568 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2570 static rmtacl_name_t rmtacl_namelist[] = {
2571 { RMTACL_OPTNAME("user:"), 1, 0 },
2572 { RMTACL_OPTNAME("group:"), 0, 0 },
2573 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2574 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2575 /* for --tabular option */
2576 { RMTACL_OPTNAME("user"), 1, 1 },
2577 { RMTACL_OPTNAME("group"), 0, 1 },
2581 static int rgetfacl_output(char *str)
2583 char *start = NULL, *end = NULL;
2584 int is_user = 0, n, id;
2591 for (rn = rmtacl_namelist; rn->name; rn++) {
2592 if(strncmp(str, rn->name, rn->length) == 0) {
2593 if (!rn->next_token)
2594 start = str + rn->length;
2596 start = next_token(str + rn->length, 0);
2597 is_user = rn->is_user;
2602 end = next_token(start, 1);
2603 if (end == NULL || start == end) {
2604 n = printf("%s", str);
2610 id = rmtacl_name2id(start, is_user);
2611 if (id == INVALID_ID) {
2612 if (str_is_id(start)) {
2614 n = printf("%s", str);
2617 } else if ((id == NOBODY_UID && is_user) ||
2618 (id == NOBODY_GID && !is_user)) {
2620 n = printf("%s", str);
2624 n = printf("%s%d%s", str, id, end);
2629 static int child_status(int status)
2631 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2634 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2647 if ((pid = fork()) < 0) {
2653 /* child process redirects its output. */
2656 if (dup2(fd[1], 1) < 0) {
2667 status = rmtacl_notify(ops);
2671 exit(execvp(argv[0], argv));
2674 /* the following is parent process */
2675 if ((fp = fdopen(fd[0], "r")) == NULL) {
2682 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2683 if (output_func(buf) < 0)
2684 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2690 if (waitpid(pid, &status, 0) < 0) {
2695 return child_status(status);
2698 int llapi_lsetfacl(int argc, char *argv[])
2700 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2703 int llapi_lgetfacl(int argc, char *argv[])
2705 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2708 int llapi_rsetfacl(int argc, char *argv[])
2710 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2713 int llapi_rgetfacl(int argc, char *argv[])
2715 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2718 int llapi_cp(int argc, char *argv[])
2722 rc = rmtacl_notify(RMT_RSETFACL);
2726 exit(execvp(argv[0], argv));
2729 int llapi_ls(int argc, char *argv[])
2733 rc = rmtacl_notify(RMT_LGETFACL);
2737 exit(execvp(argv[0], argv));
2740 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2741 * format must have %s%s, buf must be > 16
2742 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
2743 * then buf = "lustre-MDT0000"
2745 static int get_mdtname(char *name, char *format, char *buf)
2747 char suffix[]="-MDT0000";
2748 int len = strlen(name);
2750 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2751 name[len - 5] = '\0';
2756 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2759 /* Not enough room to add suffix */
2760 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2761 "MDT name too long |%s|", name);
2766 return sprintf(buf, format, name, suffix);
2769 /** ioctl on filsystem root, with mdtindex sent as data
2770 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
2771 * \param mdtidxp pointer to integer within data to be filled in with the
2772 * mdt index (0 if no mdt is specified). NULL won't be filled.
2774 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
2781 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
2782 Open root and parse mdt index. */
2783 if (mdtname[0] == '/') {
2785 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
2786 (char *)mdtname, -1);
2788 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2790 ptr = fsname + strlen(fsname) - 8;
2792 index = strtol(ptr + 4, NULL, 10);
2793 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
2797 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2798 "Can't open %s: %d\n", mdtname, rc);
2805 rc = ioctl(fd, opc, data);
2806 if (rc && want_error)
2807 llapi_err(LLAPI_MSG_ERROR, "ioctl %d err %d", opc, rc);
2813 /****** Changelog API ********/
2815 static int changelog_ioctl(const char *mdtname, int opc, int id,
2816 long long recno, int flags)
2818 struct ioc_changelog data;
2822 data.icc_recno = recno;
2823 data.icc_flags = flags;
2824 idx = (int *)(&data.icc_mdtindex);
2826 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
2829 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2830 struct changelog_private {
2833 lustre_kernelcomm kuc;
2836 /** Start reading from a changelog
2837 * @param priv Opaque private control structure
2838 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
2839 * @param device Report changes recorded on this MDT
2840 * @param startrec Report changes beginning with this record number
2841 * (just call llapi_changelog_fini when done; don't need an endrec)
2843 int llapi_changelog_start(void **priv, int flags, const char *device,
2846 struct changelog_private *cp;
2849 /* Set up the receiver control struct */
2850 cp = calloc(1, sizeof(*cp));
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);
2883 /** Finish reading from a changelog */
2884 int llapi_changelog_fini(void **priv)
2886 struct changelog_private *cp = (struct changelog_private *)*priv;
2888 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2891 libcfs_ukuc_stop(&cp->kuc);
2897 /** Read the next changelog entry
2898 * @param priv Opaque private control structure
2899 * @param rech Changelog record handle; record will be allocated here
2900 * @return 0 valid message received; rec is set
2904 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2906 struct changelog_private *cp = (struct changelog_private *)priv;
2907 struct kuc_hdr *kuch;
2910 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2914 kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
2919 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
2920 CR_MAXSIZE + sizeof(*kuch),
2921 KUC_TRANSPORT_CHANGELOG);
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. Use pointer math to skip
2946 * kuch_hdr and point directly to the message payload.
2948 *rech = (struct changelog_rec *)(kuch + 1);
2958 /** Release the changelog record when done with it. */
2959 int llapi_changelog_free(struct changelog_rec **rech)
2962 /* We allocated memory starting at the kuc_hdr, but passed
2963 * the consumer a pointer to the payload.
2964 * Use pointer math to get back to the header.
2966 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
2973 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2979 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2980 "can't purge negative records\n");
2984 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2985 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2986 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2987 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2988 "expecting id of the form '"CHANGELOG_USER_PREFIX
2989 "<num>'; got '%s'\n", idstr);
2993 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
2996 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2997 int buflen, long long *recno, int *linkno)
2999 char path[PATH_MAX];
3001 struct getinfo_fid2path *gf;
3004 while (*fidstr == '[')
3007 sscanf(fidstr, SFID, RFID(&fid));
3008 if (!fid_is_sane(&fid)) {
3009 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3010 "bad FID format [%s], should be "DFID"\n",
3011 fidstr, (__u64)1, 2, 0);
3015 /* Take path or fsname */
3016 if (device[0] == '/') {
3017 strcpy(path, device);
3019 rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
3024 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
3025 fd = open(path, O_RDONLY | O_NONBLOCK);
3029 gf = malloc(sizeof(*gf) + buflen);
3031 gf->gf_recno = *recno;
3032 gf->gf_linkno = *linkno;
3033 gf->gf_pathlen = buflen;
3034 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
3036 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
3038 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3039 *recno = gf->gf_recno;
3040 *linkno = gf->gf_linkno;
3048 static int path2fid_from_lma(const char *path, lustre_fid *fid)
3051 struct lustre_mdt_attrs *lma;
3054 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3057 lma = (struct lustre_mdt_attrs *)buf;
3058 fid_le_to_cpu(fid, &lma->lma_self_fid);
3062 int llapi_path2fid(const char *path, lustre_fid *fid)
3066 memset(fid, 0, sizeof(*fid));
3067 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3069 if (errno == ELOOP) /* symbolic link */
3070 return path2fid_from_lma(path, fid);
3074 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3075 if (rc == -EINVAL) /* char special device */
3076 rc = path2fid_from_lma(path, fid);
3082 /****** HSM Copytool API ********/
3083 #define CT_PRIV_MAGIC 0xC0BE2001
3084 struct copytool_private {
3087 lustre_kernelcomm kuc;
3091 #include <libcfs/libcfs.h>
3093 /** Register a copytool
3094 * @param[out] priv Opaque private control structure
3095 * @param fsname Lustre filesystem
3096 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3097 * @param archive_count
3098 * @param archives Which archive numbers this copytool is responsible for
3100 int llapi_copytool_start(void **priv, char *fsname, int flags,
3101 int archive_count, int *archives)
3103 struct copytool_private *ct;
3106 if (archive_count > 0 && archives == NULL) {
3107 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3108 "NULL archive numbers");
3112 ct = calloc(1, sizeof(*ct));
3116 ct->fsname = malloc(strlen(fsname) + 1);
3117 if (ct->fsname == NULL) {
3121 strcpy(ct->fsname, fsname);
3122 ct->magic = CT_PRIV_MAGIC;
3124 for (rc = 0; rc < archive_count; rc++) {
3125 if (archives[rc] > sizeof(ct->archives)) {
3126 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3127 "Maximum of %d archives supported",
3128 sizeof(ct->archives));
3131 ct->archives |= 1 << archives[rc];
3133 /* special case: if no archives specified, default to archive #0. */
3134 if (ct->archives == 0)
3137 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3141 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3142 ct->kuc.lk_data = ct->archives;
3143 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3145 /* Only the kernel reference keeps the write side open */
3146 close(ct->kuc.lk_wfd);
3161 /** Deregister a copytool */
3162 int llapi_copytool_fini(void **priv)
3164 struct copytool_private *ct = (struct copytool_private *)*priv;
3166 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3169 /* Tell the kernel to stop sending us messages */
3170 ct->kuc.lk_flags = LK_FLG_STOP;
3171 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3173 /* Shut down the kernelcomms */
3174 libcfs_ukuc_stop(&ct->kuc);
3182 /** Wait for the next hsm_action_list
3183 * @param priv Opaque private control structure
3184 * @param halh Action list handle, will be allocated here
3185 * @param msgsize Number of bytes in the message, will be set here
3186 * @return 0 valid message received; halh and msgsize are set
3189 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3191 struct copytool_private *ct = (struct copytool_private *)priv;
3192 struct kuc_hdr *kuch;
3193 struct hsm_action_list *hal;
3196 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3198 if (halh == NULL || msgsize == NULL)
3201 kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
3205 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
3206 HAL_MAXSIZE + sizeof(*kuch),
3211 /* Handle generic messages */
3212 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3213 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3218 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3219 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3220 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3221 "Unknown HSM message type %d:%d\n",
3222 kuch->kuc_transport, kuch->kuc_msgtype);
3227 /* Our message is a hsm_action_list. Use pointer math to skip
3228 * kuch_hdr and point directly to the message payload.
3230 hal = (struct hsm_action_list *)(kuch + 1);
3232 /* Check that we have registered for this archive # */
3233 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3234 llapi_err(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO,
3235 "Ignoring request for archive #%d (bitmask %#x)\n",
3236 hal->hal_archive_num, ct->archives);
3242 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3252 /** Release the action list when done with it. */
3253 int llapi_copytool_free(struct hsm_action_list **hal)
3255 /* Reuse the llapi_changelog_free function */
3256 return llapi_changelog_free((struct changelog_rec **)hal);
3259 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3264 root = opendir(mnt);
3266 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
3270 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3273 llapi_err(LLAPI_MSG_ERROR,
3274 "ioctl on %s for getting connect flags failed", mnt);
3278 int llapi_get_version(char *buffer, int buffer_size,
3283 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
3285 fd = open(OBD_DEV_PATH, O_RDONLY);
3289 memset(buffer, 0, buffer_size);
3290 data->ioc_version = OBD_IOCTL_VERSION;
3291 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
3292 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
3293 data->ioc_len = obd_ioctl_packlen(data);
3295 rc = ioctl(fd, OBD_GET_VERSION, buffer);
3302 *version = data->ioc_bulk;