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 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;
1366 LASSERT(parent != NULL || d != NULL);
1369 ret = setup_obd_uuid(d ? d : parent, path, param);
1371 /* We don't want to actually traverse the directory tree,
1372 * so return a positive value from sem_init to terminate
1373 * the traversal before it starts.
1375 return ret == 0 ? 1 : ret;
1378 int llapi_ostlist(char *path, struct find_param *param)
1380 return param_callback(path, cb_ostlist, cb_common_fini, param);
1383 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1384 struct lov_user_ost_data_v1 *objects,
1385 int is_dir, int verbose, int depth,
1388 char *prefix = is_dir ? "" : "lmm_";
1389 char nl = is_dir ? ' ' : '\n';
1391 if (is_dir && lum->lmm_object_seq == LOV_OBJECT_GROUP_DEFAULT) {
1392 lum->lmm_object_seq = LOV_OBJECT_GROUP_CLEAR;
1393 if (verbose & VERBOSE_DETAIL)
1394 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1397 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1398 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1400 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1401 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1403 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
1404 lum->lmm_object_seq);
1405 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1406 lum->lmm_object_id);
1409 if (verbose & VERBOSE_COUNT) {
1410 if (verbose & ~VERBOSE_COUNT)
1411 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1413 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1414 (__s16)lum->lmm_stripe_count, nl);
1417 if (verbose & VERBOSE_SIZE) {
1418 if (verbose & ~VERBOSE_SIZE)
1419 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1421 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
1425 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1426 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1427 lum->lmm_pattern, nl);
1430 if (verbose & VERBOSE_OFFSET) {
1431 if (verbose & ~VERBOSE_OFFSET)
1432 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1434 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1435 objects[0].l_ost_idx, nl);
1438 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1439 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s", pool_name);
1443 if (is_dir && (verbose != VERBOSE_OBJID))
1444 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1447 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1448 struct lov_user_ost_data_v1 *objects,
1449 char *path, int is_dir,
1450 int obdindex, int depth, int header)
1452 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1455 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1456 if (obdindex == objects[i].l_ost_idx) {
1464 lov_dump_user_lmm_header(lum, path, objects, is_dir, header, depth,
1467 if (!is_dir && (header & VERBOSE_OBJID)) {
1469 llapi_printf(LLAPI_MSG_NORMAL,
1470 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1472 for (i = 0; i < lum->lmm_stripe_count; i++) {
1473 int idx = objects[i].l_ost_idx;
1474 long long oid = objects[i].l_object_id;
1475 long long gr = objects[i].l_object_seq;
1476 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
1477 llapi_printf(LLAPI_MSG_NORMAL,
1478 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
1480 obdindex == idx ? " *" : "");
1482 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1486 void llapi_lov_dump_user_lmm(struct find_param *param,
1487 char *path, int is_dir)
1489 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1490 case LOV_USER_MAGIC_V1:
1491 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1492 param->lmd->lmd_lmm.lmm_objects,
1494 param->obdindex, param->maxdepth,
1497 case LOV_USER_MAGIC_V3: {
1498 char pool_name[LOV_MAXPOOLNAME + 1];
1499 struct lov_user_ost_data_v1 *objects;
1500 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1502 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1503 pool_name[LOV_MAXPOOLNAME] = '\0';
1504 objects = lmmv3->lmm_objects;
1505 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1506 objects, path, is_dir,
1507 param->obdindex, param->maxdepth,
1512 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1513 "(expecting one of %#x %#x %#x)\n",
1514 *(__u32 *)¶m->lmd->lmd_lmm,
1515 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
1520 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1526 fname = strrchr(path, '/');
1528 /* It should be a file (or other non-directory) */
1529 if (fname == NULL) {
1530 dname = (char *)malloc(2);
1534 fname = (char *)path;
1536 dname = (char *)malloc(fname - path + 1);
1539 strncpy(dname, path, fname - path);
1540 dname[fname - path] = '\0';
1544 if ((fd = open(dname, O_RDONLY)) == -1) {
1550 strcpy((char *)lum, fname);
1551 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1554 if (close(fd) == -1 && rc == 0)
1562 int llapi_file_lookup(int dirfd, const char *name)
1564 struct obd_ioctl_data data = { 0 };
1569 if (dirfd < 0 || name == NULL)
1572 data.ioc_version = OBD_IOCTL_VERSION;
1573 data.ioc_len = sizeof(data);
1574 data.ioc_inlbuf1 = (char *)name;
1575 data.ioc_inllen1 = strlen(name) + 1;
1577 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1579 llapi_err(LLAPI_MSG_ERROR,
1580 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1585 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1588 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1589 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1591 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1592 * The table below gives the answers for the specified parameters (value and
1593 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1594 * --------------------------------------
1595 * 1 | file > limit; sign > 0 | -1 / -1 |
1596 * 2 | file = limit; sign > 0 | ? / 1 |
1597 * 3 | file < limit; sign > 0 | ? / 1 |
1598 * 4 | file > limit; sign = 0 | -1 / -1 |
1599 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1600 * 6 | file < limit; sign = 0 | ? / -1 |
1601 * 7 | file > limit; sign < 0 | 1 / 1 |
1602 * 8 | file = limit; sign < 0 | ? / -1 |
1603 * 9 | file < limit; sign < 0 | ? / -1 |
1604 * --------------------------------------
1605 * Note: 5th actually means that the value is within the interval
1606 * (limit - margin, limit]. */
1607 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1608 unsigned long long margin, int mds)
1616 if (file <= limit && file + margin > limit)
1618 if (file + margin <= limit)
1619 return mds ? 0 : -1;
1632 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1633 * Return -1 or 1 if file timestamp does not or does match the given criteria
1634 * correspondingly. Return 0 if the MDS time is being checked and there are
1635 * attributes on OSTs and it is not yet clear if the timespamp matches.
1637 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1638 * updated timestamps. */
1639 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1644 /* Check if file is accepted. */
1646 ret = find_value_cmp(st->st_atime, param->atime,
1647 param->asign, 24 * 60 * 60, mds);
1654 ret = find_value_cmp(st->st_mtime, param->mtime,
1655 param->msign, 24 * 60 * 60, mds);
1659 /* If the previous check matches, but this one is not yet clear,
1660 * we should return 0 to do an RPC on OSTs. */
1666 ret = find_value_cmp(st->st_ctime, param->ctime,
1667 param->csign, 24 * 60 * 60, mds);
1671 /* If the previous check matches, but this one is not yet clear,
1672 * we should return 0 to do an RPC on OSTs. */
1680 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1681 void *data, cfs_dirent_t *de)
1683 struct find_param *param = (struct find_param *)data;
1684 int decision = 1; /* 1 is accepted; -1 is rejected. */
1685 lstat_t *st = ¶m->lmd->lmd_st;
1687 int checked_type = 0;
1690 LASSERT(parent != NULL || dir != NULL);
1692 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1694 /* If a regular expression is presented, make the initial decision */
1695 if (param->pattern != NULL) {
1696 char *fname = strrchr(path, '/');
1697 fname = (fname == NULL ? path : fname + 1);
1698 ret = fnmatch(param->pattern, fname, 0);
1699 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1700 (ret == 0 && param->exclude_pattern))
1704 /* See if we can check the file type from the dirent. */
1705 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1706 de->d_type < DT_MAX) {
1708 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1709 if (param->exclude_type)
1712 if (!param->exclude_type)
1718 /* If a time or OST should be checked, the decision is not taken yet. */
1719 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1724 /* Request MDS for the stat info. */
1725 if (param->have_fileinfo == 0) {
1727 /* retrieve needed file info */
1728 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1729 (void *)param->lmd);
1731 char *fname = strrchr(path, '/');
1732 fname = (fname == NULL ? path : fname + 1);
1734 /* retrieve needed file info */
1735 strncpy((char *)param->lmd, fname, param->lumlen);
1736 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1737 (void *)param->lmd);
1742 if (errno == ENOTTY) {
1743 /* ioctl is not supported, it is not a lustre fs.
1744 * Do the regular lstat(2) instead. */
1746 ret = lstat_f(path, st);
1748 llapi_err(LLAPI_MSG_ERROR,
1749 "error: %s: lstat failed for %s",
1753 } else if (errno == ENOENT) {
1754 llapi_err(LLAPI_MSG_WARN,
1755 "warning: %s: %s does not exist",
1759 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1760 __func__, dir ? "LL_IOC_MDC_GETINFO" :
1761 "IOC_MDC_GETFILEINFO", path);
1766 if (param->type && !checked_type) {
1767 if ((st->st_mode & S_IFMT) == param->type) {
1768 if (param->exclude_type)
1771 if (!param->exclude_type)
1777 if (param->obduuid) {
1778 if (lustre_fs && param->got_uuids &&
1779 param->st_dev != st->st_dev) {
1780 /* A lustre/lustre mount point is crossed. */
1781 param->got_uuids = 0;
1782 param->obds_printed = 0;
1783 param->obdindex = OBD_NOT_FOUND;
1786 if (lustre_fs && !param->got_uuids) {
1787 ret = setup_obd_indexes(dir ? dir : parent, param);
1791 param->st_dev = st->st_dev;
1792 } else if (!lustre_fs && param->got_uuids) {
1793 /* A lustre/non-lustre mount point is crossed. */
1794 param->got_uuids = 0;
1795 param->obdindex = OBD_NOT_FOUND;
1799 /* If an OBD UUID is specified but no one matches, skip this file. */
1800 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1803 /* If a OST UUID is given, and some OST matches, check it here. */
1804 if (param->obdindex != OBD_NOT_FOUND) {
1805 if (!S_ISREG(st->st_mode))
1808 /* Only those files should be accepted, which have a
1809 * stripe on the specified OST. */
1810 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1814 struct lov_user_ost_data_v1 *lmm_objects;
1816 if (param->lmd->lmd_lmm.lmm_magic ==
1817 LOV_USER_MAGIC_V3) {
1818 struct lov_user_md_v3 *lmmv3 =
1819 (void *)¶m->lmd->lmd_lmm;
1821 lmm_objects = lmmv3->lmm_objects;
1823 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1827 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1828 for (j = 0; j < param->num_obds; j++) {
1829 if (param->obdindexes[j] ==
1830 lmm_objects[i].l_ost_idx) {
1831 if (param->exclude_obd)
1838 if (i == param->lmd->lmd_lmm.lmm_stripe_count) {
1839 if (param->exclude_obd)
1846 if (param->check_uid) {
1847 if (st->st_uid == param->uid) {
1848 if (param->exclude_uid)
1851 if (!param->exclude_uid)
1856 if (param->check_gid) {
1857 if (st->st_gid == param->gid) {
1858 if (param->exclude_gid)
1861 if (!param->exclude_gid)
1866 if (param->check_pool) {
1867 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1869 /* empty requested pool is taken as no pool search => V1 */
1870 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1871 (param->poolname[0] == '\0')) ||
1872 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1873 (strncmp(lmmv3->lmm_pool_name,
1874 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1875 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1876 (strcmp(param->poolname, "*") == 0))) {
1877 if (param->exclude_pool)
1880 if (!param->exclude_pool)
1885 /* Check the time on mds. */
1889 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1890 param->lmd->lmd_lmm.lmm_stripe_count)
1892 decision = find_time_check(st, param, for_mds);
1896 /* If file still fits the request, ask ost for updated info.
1897 The regular stat is almost of the same speed as some new
1898 'glimpse-size-ioctl'. */
1899 if (!decision && S_ISREG(st->st_mode) &&
1900 param->lmd->lmd_lmm.lmm_stripe_count &&
1901 (param->size ||param->atime || param->mtime || param->ctime)) {
1902 if (param->obdindex != OBD_NOT_FOUND) {
1903 /* Check whether the obd is active or not, if it is
1904 * not active, just print the object affected by this
1907 struct obd_statfs stat_buf;
1908 struct obd_uuid uuid_buf;
1910 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1911 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1912 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1913 param->obdindex, &stat_buf,
1916 if (ret == -ENODATA || ret == -ENODEV
1919 llapi_printf(LLAPI_MSG_NORMAL,
1920 "obd_uuid: %s failed %s ",
1921 param->obduuid->uuid,
1927 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1928 (void *)param->lmd);
1929 } else if (parent) {
1930 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1931 (void *)param->lmd);
1935 if (errno == ENOENT) {
1936 llapi_err(LLAPI_MSG_ERROR,
1937 "warning: %s: %s does not exist",
1941 llapi_err(LLAPI_MSG_ERROR,
1942 "%s: IOC_LOV_GETINFO on %s failed",
1948 /* Check the time on osc. */
1949 decision = find_time_check(st, param, 0);
1955 decision = find_value_cmp(st->st_size, param->size,
1956 param->size_sign, param->size_units,
1960 if (decision != -1) {
1961 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1963 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1965 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1969 /* Do not get down anymore? */
1970 if (param->depth == param->maxdepth)
1977 int llapi_find(char *path, struct find_param *param)
1979 return param_callback(path, cb_find_init, cb_common_fini, param);
1983 * Get MDT number that the file/directory inode referenced
1984 * by the open fd resides on.
1985 * Return 0 and mdtidx on success, or -ve errno.
1987 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
1989 if (ioctl(fd, LL_IOC_GET_MDTIDX, &mdtidx) < 0)
1994 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
1997 struct find_param *param = (struct find_param *)data;
2001 LASSERT(parent != NULL || d != NULL);
2004 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2005 } else if (parent) {
2008 fd = open(path, O_RDONLY);
2010 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2018 if (errno == ENODATA) {
2019 if (!param->obduuid)
2020 llapi_printf(LLAPI_MSG_NORMAL,
2021 "%s has no stripe info\n", path);
2023 } else if (errno == ENOTTY) {
2024 llapi_err(LLAPI_MSG_ERROR,
2025 "%s: '%s' not on a Lustre fs?",
2027 } else if (errno == ENOENT) {
2028 llapi_err(LLAPI_MSG_WARN,
2029 "warning: %s: %s does not exist",
2033 llapi_err(LLAPI_MSG_ERROR,
2034 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2041 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2043 llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
2047 /* Do not get down anymore? */
2048 if (param->depth == param->maxdepth)
2055 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2058 struct find_param *param = (struct find_param *)data;
2061 LASSERT(parent != NULL || d != NULL);
2063 if (param->obduuid) {
2065 ret = setup_obd_uuid(d ? d : parent, path, param);
2071 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2072 (void *)¶m->lmd->lmd_lmm);
2073 } else if (parent) {
2074 char *fname = strrchr(path, '/');
2075 fname = (fname == NULL ? path : fname + 1);
2077 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2079 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2080 (void *)¶m->lmd->lmd_lmm);
2084 if (errno == ENODATA) {
2085 if (!param->obduuid)
2086 llapi_printf(LLAPI_MSG_NORMAL,
2087 "%s has no stripe info\n", path);
2089 } else if (errno == ENOTTY) {
2090 llapi_err(LLAPI_MSG_ERROR,
2091 "%s: '%s' not on a Lustre fs?",
2093 } else if (errno == ENOENT) {
2094 llapi_err(LLAPI_MSG_WARN,
2095 "warning: %s: %s does not exist",
2099 llapi_err(LLAPI_MSG_ERROR,
2100 "error: %s: %s failed for %s",
2101 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2102 "IOC_MDC_GETFILESTRIPE", path);
2108 if (!param->get_mdt_index)
2109 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2112 /* Do not get down anymore? */
2113 if (param->depth == param->maxdepth)
2120 int llapi_getstripe(char *path, struct find_param *param)
2122 return param_callback(path, param->get_mdt_index ?
2123 cb_get_mdt_index : cb_getstripe,
2124 cb_common_fini, param);
2127 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2128 struct obd_statfs *stat_buf,
2129 struct obd_uuid *uuid_buf)
2132 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2134 struct obd_ioctl_data data = { 0 };
2137 data.ioc_inlbuf1 = (char *)&type;
2138 data.ioc_inllen1 = sizeof(__u32);
2139 data.ioc_inlbuf2 = (char *)&index;
2140 data.ioc_inllen2 = sizeof(__u32);
2141 data.ioc_pbuf1 = (char *)stat_buf;
2142 data.ioc_plen1 = sizeof(struct obd_statfs);
2143 data.ioc_pbuf2 = (char *)uuid_buf;
2144 data.ioc_plen2 = sizeof(struct obd_uuid);
2146 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
2147 llapi_err(LLAPI_MSG_ERROR,
2148 "llapi_obd_statfs: error packing ioctl data");
2152 fd = open(path, O_RDONLY);
2153 if (errno == EISDIR)
2154 fd = open(path, O_DIRECTORY | O_RDONLY);
2157 rc = errno ? -errno : -EBADF;
2158 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
2162 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2164 rc = errno ? -errno : -EINVAL;
2170 #define MAX_STRING_SIZE 128
2171 #define DEVICES_LIST "/proc/fs/lustre/devices"
2173 int llapi_ping(char *obd_type, char *obd_name)
2175 char path[MAX_STRING_SIZE];
2179 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2180 obd_type, obd_name);
2182 fd = open(path, O_WRONLY);
2185 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
2189 rc = write(fd, buf, 1);
2197 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
2199 char buf[MAX_STRING_SIZE];
2200 FILE *fp = fopen(DEVICES_LIST, "r");
2205 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
2209 while (fgets(buf, sizeof(buf), fp) != NULL) {
2210 char *obd_type_name = NULL;
2211 char *obd_name = NULL;
2212 char *obd_uuid = NULL;
2214 struct obd_ioctl_data datal = { 0, };
2215 struct obd_statfs osfs_buffer;
2217 while(bufp[0] == ' ')
2220 for(i = 0; i < 3; i++) {
2221 obd_type_name = strsep(&bufp, " ");
2223 obd_name = strsep(&bufp, " ");
2224 obd_uuid = strsep(&bufp, " ");
2226 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2228 datal.ioc_pbuf1 = (char *)&osfs_buffer;
2229 datal.ioc_plen1 = sizeof(osfs_buffer);
2231 for (i = 0; i < type_num; i++) {
2232 if (strcmp(obd_type_name, obd_type[i]) != 0)
2235 cb(obd_type_name, obd_name, obd_uuid, args);
2242 static void do_target_check(char *obd_type_name, char *obd_name,
2243 char *obd_uuid, void *args)
2247 rc = llapi_ping(obd_type_name, obd_name);
2248 if (rc == ENOTCONN) {
2249 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2251 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
2253 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2257 int llapi_target_check(int type_num, char **obd_type, char *dir)
2259 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2262 #undef MAX_STRING_SIZE
2264 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2266 char raw[OBD_MAX_IOCTL_BUFFER];
2267 char out[LLOG_CHUNK_SIZE];
2269 struct obd_ioctl_data data = { 0 };
2274 sprintf(key, "%s", keyword);
2275 memset(raw, 0, sizeof(raw));
2276 memset(out, 0, sizeof(out));
2277 data.ioc_inlbuf1 = key;
2278 data.ioc_inllen1 = strlen(key) + 1;
2280 data.ioc_inlbuf2 = node_name;
2281 data.ioc_inllen2 = strlen(node_name) + 1;
2283 data.ioc_pbuf1 = out;
2284 data.ioc_plen1 = sizeof(out);
2285 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2289 root = opendir(dir);
2292 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
2296 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2298 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
2300 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2306 /* Is this a lustre fs? */
2307 int llapi_is_lustre_mnttype(const char *type)
2309 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2312 /* Is this a lustre client fs? */
2313 int llapi_is_lustre_mnt(struct mntent *mnt)
2315 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2316 strstr(mnt->mnt_fsname, ":/") != NULL);
2319 int llapi_quotacheck(char *mnt, int check_type)
2324 root = opendir(mnt);
2326 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2330 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2336 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2342 root = opendir(mnt);
2344 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2349 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2353 if (poll_intvl < 30)
2361 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2366 root = opendir(mnt);
2368 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2372 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2378 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2381 struct find_param *param = (struct find_param *)data;
2385 LASSERT(parent != NULL || d != NULL);
2388 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2389 (void *)param->lmd);
2390 } else if (parent) {
2391 char *fname = strrchr(path, '/');
2392 fname = (fname == NULL ? path : fname + 1);
2394 strncpy((char *)param->lmd, fname, param->lumlen);
2395 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2396 (void *)param->lmd);
2402 if (errno == ENODATA) {
2403 if (!param->obduuid && !param->quiet)
2404 llapi_err(LLAPI_MSG_ERROR,
2405 "%s has no stripe info", path);
2407 } else if (errno == ENOENT) {
2408 llapi_err(LLAPI_MSG_ERROR,
2409 "warning: %s: %s does not exist",
2412 } else if (errno != EISDIR) {
2414 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2415 d ? "LL_IOC_MDC_GETINFO" :
2416 "IOC_MDC_GETFILEINFO", path);
2421 st = ¶m->lmd->lmd_st;
2423 /* libc chown() will do extra check, and if the real owner is
2424 * the same as the ones to set, it won't fall into kernel, so
2425 * invoke syscall directly. */
2426 rc = syscall(SYS_chown, path, -1, -1);
2428 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2430 rc = chmod(path, st->st_mode);
2432 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2438 int llapi_quotachown(char *path, int flag)
2440 struct find_param param;
2442 memset(¶m, 0, sizeof(param));
2443 param.recursive = 1;
2447 return param_callback(path, cb_quotachown, NULL, ¶m);
2453 #include <sys/wait.h>
2457 static int rmtacl_notify(int ops)
2461 int found = 0, fd, rc;
2463 fp = setmntent(MOUNTED, "r");
2465 perror("setmntent");
2470 mnt = getmntent(fp);
2474 if (!llapi_is_lustre_mnt(mnt))
2477 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2483 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2495 static char *next_token(char *p, int div)
2501 while (*p && *p != ':' && !isspace(*p))
2504 while (*p == ':' || isspace(*p))
2507 return *p ? p : NULL;
2510 static int rmtacl_name2id(char *name, int is_user)
2515 if ((pw = getpwnam(name)) == NULL)
2518 return (int)(pw->pw_uid);
2522 if ((gr = getgrnam(name)) == NULL)
2525 return (int)(gr->gr_gid);
2529 static int isodigit(int c)
2531 return (c >= '0' && c <= '7') ? 1 : 0;
2535 * Whether the name is just digits string (uid/gid) already or not.
2540 static int str_is_id(char *str)
2547 if (*str == 'x' || *str == 'X') { /* for Hex. */
2548 if (!isxdigit(*(++str)))
2551 while (isxdigit(*(++str)));
2552 } else if (isodigit(*str)) { /* for Oct. */
2553 while (isodigit(*(++str)));
2555 } else if (isdigit(*str)) { /* for Dec. */
2556 while (isdigit(*(++str)));
2559 return (*str == 0) ? 1 : 0;
2569 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2571 static rmtacl_name_t rmtacl_namelist[] = {
2572 { RMTACL_OPTNAME("user:"), 1, 0 },
2573 { RMTACL_OPTNAME("group:"), 0, 0 },
2574 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2575 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2576 /* for --tabular option */
2577 { RMTACL_OPTNAME("user"), 1, 1 },
2578 { RMTACL_OPTNAME("group"), 0, 1 },
2582 static int rgetfacl_output(char *str)
2584 char *start = NULL, *end = NULL;
2585 int is_user = 0, n, id;
2592 for (rn = rmtacl_namelist; rn->name; rn++) {
2593 if(strncmp(str, rn->name, rn->length) == 0) {
2594 if (!rn->next_token)
2595 start = str + rn->length;
2597 start = next_token(str + rn->length, 0);
2598 is_user = rn->is_user;
2603 end = next_token(start, 1);
2604 if (end == NULL || start == end) {
2605 n = printf("%s", str);
2611 id = rmtacl_name2id(start, is_user);
2612 if (id == INVALID_ID) {
2613 if (str_is_id(start)) {
2615 n = printf("%s", str);
2618 } else if ((id == NOBODY_UID && is_user) ||
2619 (id == NOBODY_GID && !is_user)) {
2621 n = printf("%s", str);
2625 n = printf("%s%d%s", str, id, end);
2630 static int child_status(int status)
2632 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2635 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2648 if ((pid = fork()) < 0) {
2654 /* child process redirects its output. */
2657 if (dup2(fd[1], 1) < 0) {
2668 status = rmtacl_notify(ops);
2672 exit(execvp(argv[0], argv));
2675 /* the following is parent process */
2676 if ((fp = fdopen(fd[0], "r")) == NULL) {
2683 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2684 if (output_func(buf) < 0)
2685 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2691 if (waitpid(pid, &status, 0) < 0) {
2696 return child_status(status);
2699 int llapi_lsetfacl(int argc, char *argv[])
2701 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2704 int llapi_lgetfacl(int argc, char *argv[])
2706 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2709 int llapi_rsetfacl(int argc, char *argv[])
2711 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2714 int llapi_rgetfacl(int argc, char *argv[])
2716 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2719 int llapi_cp(int argc, char *argv[])
2723 rc = rmtacl_notify(RMT_RSETFACL);
2727 exit(execvp(argv[0], argv));
2730 int llapi_ls(int argc, char *argv[])
2734 rc = rmtacl_notify(RMT_LGETFACL);
2738 exit(execvp(argv[0], argv));
2741 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2742 * format must have %s%s, buf must be > 16
2743 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
2744 * then buf = "lustre-MDT0000"
2746 static int get_mdtname(char *name, char *format, char *buf)
2748 char suffix[]="-MDT0000";
2749 int len = strlen(name);
2751 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2752 name[len - 5] = '\0';
2757 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2760 /* Not enough room to add suffix */
2761 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2762 "MDT name too long |%s|", name);
2767 return sprintf(buf, format, name, suffix);
2770 /** ioctl on filsystem root, with mdtindex sent as data
2771 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
2772 * \param mdtidxp pointer to integer within data to be filled in with the
2773 * mdt index (0 if no mdt is specified). NULL won't be filled.
2775 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
2782 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
2783 Open root and parse mdt index. */
2784 if (mdtname[0] == '/') {
2786 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
2787 (char *)mdtname, -1);
2789 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2791 ptr = fsname + strlen(fsname) - 8;
2793 index = strtol(ptr + 4, NULL, 10);
2794 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
2798 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2799 "Can't open %s: %d\n", mdtname, rc);
2806 rc = ioctl(fd, opc, data);
2807 if (rc && want_error)
2808 llapi_err(LLAPI_MSG_ERROR, "ioctl %d err %d", opc, rc);
2814 /****** Changelog API ********/
2816 static int changelog_ioctl(const char *mdtname, int opc, int id,
2817 long long recno, int flags)
2819 struct ioc_changelog data;
2823 data.icc_recno = recno;
2824 data.icc_flags = flags;
2825 idx = (int *)(&data.icc_mdtindex);
2827 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
2830 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2831 struct changelog_private {
2834 lustre_kernelcomm kuc;
2838 /** Start reading from a changelog
2839 * @param priv Opaque private control structure
2840 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
2841 * @param device Report changes recorded on this MDT
2842 * @param startrec Report changes beginning with this record number
2843 * (just call llapi_changelog_fini when done; don't need an endrec)
2845 int llapi_changelog_start(void **priv, int flags, const char *device,
2848 struct changelog_private *cp;
2851 /* Set up the receiver control struct */
2852 cp = malloc(sizeof(*cp));
2856 cp->buf = malloc(CR_MAXSIZE);
2857 if (cp->buf == NULL) {
2862 cp->magic = CHANGELOG_PRIV_MAGIC;
2865 /* Set up the receiver */
2866 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
2872 /* Tell the kernel to start sending */
2873 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
2875 /* Only the kernel reference keeps the write side open */
2876 close(cp->kuc.lk_wfd);
2879 /* frees and clears priv */
2880 llapi_changelog_fini(priv);
2893 /** Finish reading from a changelog */
2894 int llapi_changelog_fini(void **priv)
2896 struct changelog_private *cp = (struct changelog_private *)*priv;
2898 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2901 libcfs_ukuc_stop(&cp->kuc);
2908 /** Read the next changelog entry
2909 * @param priv Opaque private control structure
2910 * @param rech Changelog record handle; record will be allocated here
2911 * @return 0 valid message received; rec is set
2915 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2917 struct changelog_private *cp = (struct changelog_private *)priv;
2918 struct kuc_hdr *kuch;
2921 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2927 rc = libcfs_ukuc_msg_get(&cp->kuc, cp->buf, CR_MAXSIZE,
2928 KUC_TRANSPORT_CHANGELOG);
2932 kuch = (struct kuc_hdr *)cp->buf;
2933 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
2934 ((kuch->kuc_msgtype != CL_RECORD) &&
2935 (kuch->kuc_msgtype != CL_EOF))) {
2936 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2937 "Unknown changelog message type %d:%d\n",
2938 kuch->kuc_transport, kuch->kuc_msgtype);
2943 if (kuch->kuc_msgtype == CL_EOF) {
2944 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
2953 /* Our message is a changelog_rec */
2954 *rech = (struct changelog_rec *)(kuch + 1);
2963 /** Release the changelog record when done with it. */
2964 int llapi_changelog_free(struct changelog_rec **rech)
2970 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2976 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2977 "can't purge negative records\n");
2981 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2982 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2983 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2984 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2985 "expecting id of the form '"CHANGELOG_USER_PREFIX
2986 "<num>'; got '%s'\n", idstr);
2990 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
2993 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2994 int buflen, long long *recno, int *linkno)
2996 char path[PATH_MAX];
2998 struct getinfo_fid2path *gf;
3001 while (*fidstr == '[')
3004 sscanf(fidstr, SFID, RFID(&fid));
3005 if (!fid_is_sane(&fid)) {
3006 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3007 "bad FID format [%s], should be "DFID"\n",
3008 fidstr, (__u64)1, 2, 0);
3012 /* Take path or fsname */
3013 if (device[0] == '/') {
3014 strcpy(path, device);
3016 rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device,
3021 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
3022 fd = open(path, O_RDONLY | O_NONBLOCK);
3026 gf = malloc(sizeof(*gf) + buflen);
3028 gf->gf_recno = *recno;
3029 gf->gf_linkno = *linkno;
3030 gf->gf_pathlen = buflen;
3031 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
3033 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
3035 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3036 *recno = gf->gf_recno;
3037 *linkno = gf->gf_linkno;
3045 static int path2fid_from_lma(const char *path, lustre_fid *fid)
3048 struct lustre_mdt_attrs *lma;
3051 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3054 lma = (struct lustre_mdt_attrs *)buf;
3055 fid_le_to_cpu(fid, &lma->lma_self_fid);
3059 int llapi_path2fid(const char *path, lustre_fid *fid)
3063 memset(fid, 0, sizeof(*fid));
3064 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3066 if (errno == ELOOP) /* symbolic link */
3067 return path2fid_from_lma(path, fid);
3071 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3072 if (rc == -EINVAL) /* char special device */
3073 rc = path2fid_from_lma(path, fid);
3079 /****** HSM Copytool API ********/
3080 #define CT_PRIV_MAGIC 0xC0BE2001
3081 struct copytool_private {
3085 lustre_kernelcomm kuc;
3089 #include <libcfs/libcfs.h>
3091 /** Register a copytool
3092 * @param[out] priv Opaque private control structure
3093 * @param fsname Lustre filesystem
3094 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3095 * @param archive_count
3096 * @param archives Which archive numbers this copytool is responsible for
3098 int llapi_copytool_start(void **priv, char *fsname, int flags,
3099 int archive_count, int *archives)
3101 struct copytool_private *ct;
3104 if (archive_count > 0 && archives == NULL) {
3105 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3106 "NULL archive numbers");
3110 ct = malloc(sizeof(*ct));
3114 ct->buf = malloc(HAL_MAXSIZE);
3115 ct->fsname = malloc(strlen(fsname) + 1);
3116 if (ct->buf == NULL || ct->fsname == NULL) {
3120 strcpy(ct->fsname, fsname);
3121 ct->magic = CT_PRIV_MAGIC;
3123 for (rc = 0; rc < archive_count; rc++) {
3124 if (archives[rc] > sizeof(ct->archives)) {
3125 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3126 "Maximum of %d archives supported",
3127 sizeof(ct->archives));
3130 ct->archives |= 1 << archives[rc];
3132 /* special case: if no archives specified, default to archive #0. */
3133 if (ct->archives == 0)
3136 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3140 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3141 ct->kuc.lk_data = ct->archives;
3142 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3144 /* Only the kernel reference keeps the write side open */
3145 close(ct->kuc.lk_wfd);
3162 /** Deregister a copytool */
3163 int llapi_copytool_fini(void **priv)
3165 struct copytool_private *ct = (struct copytool_private *)*priv;
3167 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3170 /* Tell the kernel to stop sending us messages */
3171 ct->kuc.lk_flags = LK_FLG_STOP;
3172 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3174 /* Shut down the kernelcomms */
3175 libcfs_ukuc_stop(&ct->kuc);
3184 /** Wait for the next hsm_action_list
3185 * @param priv Opaque private control structure
3186 * @param halh Action list handle, will be allocated here
3187 * @param msgsize Number of bytes in the message, will be set here
3188 * @return 0 valid message received; halh and msgsize are set
3191 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3193 struct copytool_private *ct = (struct copytool_private *)priv;
3194 struct kuc_hdr *kuch;
3195 struct hsm_action_list *hal;
3198 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3200 if (halh == NULL || msgsize == NULL)
3203 rc = libcfs_ukuc_msg_get(&ct->kuc, ct->buf, HAL_MAXSIZE,
3208 /* Handle generic messages */
3209 kuch = (struct kuc_hdr *)ct->buf;
3210 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3211 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3216 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3217 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3218 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
3219 "Unknown HSM message type %d:%d\n",
3220 kuch->kuc_transport, kuch->kuc_msgtype);
3225 /* Our message is an hsm_action_list */
3227 hal = (struct hsm_action_list *)(kuch + 1);
3229 /* Check that we have registered for this archive # */
3230 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3231 llapi_err(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO,
3232 "Ignoring request for archive #%d (bitmask %#x)\n",
3233 hal->hal_archive_num, ct->archives);
3239 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3248 /** Release the action list when done with it. */
3249 int llapi_copytool_free(struct hsm_action_list **hal)
3255 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3260 root = opendir(mnt);
3262 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
3266 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3269 llapi_err(LLAPI_MSG_ERROR,
3270 "ioctl on %s for getting connect flags failed", mnt);