4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
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>
53 #include <sys/ioctl.h>
60 #include <sys/statfs.h>
61 #include <sys/types.h>
62 #include <sys/syscall.h>
63 #include <sys/xattr.h>
66 #include <libgen.h> /* for dirname() */
67 #ifdef HAVE_LINUX_UNISTD_H
68 #include <linux/unistd.h>
74 #include <libcfs/util/string.h>
75 #include <libcfs/libcfs.h>
76 #include <lnet/lnetctl.h>
77 #include <lustre/lustreapi.h>
78 #include <lustre_ioctl.h>
79 #include "lustreapi_internal.h"
81 static int llapi_msg_level = LLAPI_MSG_MAX;
83 void llapi_msg_set_level(int level)
85 /* ensure level is in the good range */
86 if (level < LLAPI_MSG_OFF)
87 llapi_msg_level = LLAPI_MSG_OFF;
88 else if (level > LLAPI_MSG_MAX)
89 llapi_msg_level = LLAPI_MSG_MAX;
91 llapi_msg_level = level;
94 int llapi_msg_get_level(void)
96 return llapi_msg_level;
99 static void error_callback_default(enum llapi_message_level level, int err,
100 const char *fmt, va_list ap)
102 vfprintf(stderr, fmt, ap);
103 if (level & LLAPI_MSG_NO_ERRNO)
104 fprintf(stderr, "\n");
106 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
109 static void info_callback_default(enum llapi_message_level level, int err,
110 const char *fmt, va_list ap)
112 vfprintf(stdout, fmt, ap);
115 static llapi_log_callback_t llapi_error_callback = error_callback_default;
116 static llapi_log_callback_t llapi_info_callback = info_callback_default;
119 /* llapi_error will preserve errno */
120 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
123 int tmp_errno = errno;
125 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
129 llapi_error_callback(level, abs(err), fmt, args);
134 /* llapi_printf will preserve errno */
135 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
138 int tmp_errno = errno;
140 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
144 llapi_info_callback(level, 0, fmt, args);
150 * Set a custom error logging function. Passing in NULL will reset the logging
151 * callback to its default value.
153 * This function returns the value of the old callback.
155 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
157 llapi_log_callback_t old = llapi_error_callback;
160 llapi_error_callback = cb;
162 llapi_error_callback = error_callback_default;
168 * Set a custom info logging function. Passing in NULL will reset the logging
169 * callback to its default value.
171 * This function returns the value of the old callback.
173 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
175 llapi_log_callback_t old = llapi_info_callback;
178 llapi_info_callback = cb;
180 llapi_info_callback = info_callback_default;
186 * size_units is to be initialized (or zeroed) by caller.
188 int llapi_parse_size(const char *optarg, unsigned long long *size,
189 unsigned long long *size_units, int bytes_spec)
193 if (strncmp(optarg, "-", 1) == 0)
196 if (*size_units == 0)
199 *size = strtoull(optarg, &end, 0);
202 if ((*end == 'b') && *(end + 1) == '\0' &&
203 (*size & (~0ULL << (64 - 9))) == 0 &&
205 *size_units = 1 << 9;
206 } else if ((*end == 'b') &&
207 *(end + 1) == '\0' &&
210 } else if ((*end == 'k' || *end == 'K') &&
211 *(end + 1) == '\0' &&
212 (*size & (~0ULL << (64 - 10))) == 0) {
213 *size_units = 1 << 10;
214 } else if ((*end == 'm' || *end == 'M') &&
215 *(end + 1) == '\0' &&
216 (*size & (~0ULL << (64 - 20))) == 0) {
217 *size_units = 1 << 20;
218 } else if ((*end == 'g' || *end == 'G') &&
219 *(end + 1) == '\0' &&
220 (*size & (~0ULL << (64 - 30))) == 0) {
221 *size_units = 1 << 30;
222 } else if ((*end == 't' || *end == 'T') &&
223 *(end + 1) == '\0' &&
224 (*size & (~0ULL << (64 - 40))) == 0) {
225 *size_units = 1ULL << 40;
226 } else if ((*end == 'p' || *end == 'P') &&
227 *(end + 1) == '\0' &&
228 (*size & (~0ULL << (64 - 50))) == 0) {
229 *size_units = 1ULL << 50;
230 } else if ((*end == 'e' || *end == 'E') &&
231 *(end + 1) == '\0' &&
232 (*size & (~0ULL << (64 - 60))) == 0) {
233 *size_units = 1ULL << 60;
238 *size *= *size_units;
242 /* XXX: llapi_xxx() functions return negative values upon failure */
244 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
245 int stripe_count, int stripe_pattern)
249 /* 64 KB is the largest common page size I'm aware of (on ia64), but
250 * check the local page size just in case. */
251 page_size = LOV_MIN_STRIPE_SIZE;
252 if (getpagesize() > page_size) {
253 page_size = getpagesize();
254 llapi_err_noerrno(LLAPI_MSG_WARN,
255 "warning: your page size (%u) is "
256 "larger than expected (%u)", page_size,
257 LOV_MIN_STRIPE_SIZE);
259 if (!llapi_stripe_size_is_aligned(stripe_size)) {
261 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
262 "must be an even multiple of %d bytes",
263 stripe_size, page_size);
266 if (!llapi_stripe_offset_is_valid(stripe_offset)) {
268 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
272 if (!llapi_stripe_count_is_valid(stripe_count)) {
274 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
278 if (llapi_stripe_size_is_too_big(stripe_size)) {
280 llapi_error(LLAPI_MSG_ERROR, rc,
281 "warning: stripe size 4G or larger "
282 "is not currently supported and would wrap");
288 /* return the first file matching this pattern */
289 static int first_match(char *pattern, char *buffer)
293 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
296 if (glob_info.gl_pathc < 1) {
297 globfree(&glob_info);
301 strcpy(buffer, glob_info.gl_pathv[0]);
303 globfree(&glob_info);
307 static int find_target_obdpath(char *fsname, char *path)
310 char pattern[PATH_MAX + 1];
313 snprintf(pattern, PATH_MAX,
314 "/proc/fs/lustre/lov/%s-*/target_obd",
316 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
317 if (rc == GLOB_NOMATCH)
322 strcpy(path, glob_info.gl_pathv[0]);
323 globfree(&glob_info);
327 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
330 char pattern[PATH_MAX + 1];
333 snprintf(pattern, PATH_MAX,
334 "/proc/fs/lustre/lov/%s-*/pools/%s",
336 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
337 /* If no pools, make sure the lov is available */
338 if ((rc == GLOB_NOMATCH) &&
339 (find_target_obdpath(fsname, poolpath) == -ENODEV))
344 strcpy(poolpath, glob_info.gl_pathv[0]);
345 globfree(&glob_info);
350 * Trim a trailing newline from a string, if it exists.
352 int llapi_chomp_string(char *buf)
368 * return a parameter string for a specific device type or mountpoint
370 * \param param_path the path to the file containing parameter data
371 * \param result buffer for parameter value string
372 * \param result_size size of buffer for return value
374 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
375 * complete the absolute path to the file containing the parameter data
376 * the user is requesting. If that file exist then the data is read from
377 * the file and placed into the \param result buffer that is passed by
378 * the user. Data is only copied up to the \param result_size to prevent
379 * overflow of the array.
381 * Return 0 for success, with a NUL-terminated string in \param result.
382 * Return -ve value for error.
384 int get_param(const char *param_path, char *result,
385 unsigned int result_size)
387 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
391 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
393 rc = first_match(pattern, file);
394 if (rc != 0 || result == NULL)
397 fp = fopen(file, "r");
399 while (fgets(buf, result_size, fp) != NULL)
408 #define DEVICES_LIST "/proc/fs/lustre/devices"
411 * return a parameter string for a specific device type or mountpoint
413 * \param fsname Lustre filesystem name (optional)
414 * \param file_path path to file in filesystem (optional, if fsname unset)
415 * \param obd_type Lustre OBD device type
416 * \param param_name parameter name to fetch
417 * \param value return buffer for parameter value string
418 * \param val_len size of buffer for return value
420 * If fsname is specified then the parameter will be from that filesystem
421 * (if it exists). If file_path is given and it is in a mounted Lustre
422 * filesystem, then the parameter will be otherwise the value may be
423 * from any mounted filesystem (if there is more than one).
425 * If "obd_type" matches a Lustre device then the first matching device
426 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
427 * will be used to provide the return value, otherwise the first such
428 * device found will be used.
430 * Return 0 for success, with a NUL-terminated string in \param buffer.
431 * Return -ve value for error.
433 static int get_param_obdvar(const char *fsname, const char *file_path,
434 const char *obd_type, const char *param_name,
435 char *value, unsigned int val_len)
437 char devices[PATH_MAX];
438 char dev[PATH_MAX] = "*";
443 fp = fopen(DEVICES_LIST, "r");
446 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
450 if (fsname == NULL && file_path != NULL) {
451 rc = llapi_search_fsname(file_path, fs);
453 llapi_error(LLAPI_MSG_ERROR, rc,
454 "'%s' is not on a Lustre filesystem",
458 } else if (fsname != NULL) {
459 rc = strlcpy(fs, fsname, sizeof(fs));
460 if (rc >= sizeof(fs)) {
466 while (fgets(devices, sizeof(devices) - 1, fp) != NULL) {
467 char *bufp = devices, *tmp;
469 devices[sizeof(devices) - 1] = '\0';
470 while (bufp[0] == ' ')
473 tmp = strstr(bufp, obd_type);
475 tmp += strlen(obd_type) + 1;
478 rc = strlcpy(dev, tmp, sizeof(dev));
479 if (rc >= sizeof(dev)) {
484 tmp = strchr(dev, ' ');
491 if (dev[0] == '*' && strlen(fs)) {
492 rc = snprintf(dev, sizeof(dev), "%s-*", fs);
493 if (rc >= sizeof(dev)) {
498 rc = snprintf(devices, sizeof(devices), "%s/%s/%s", obd_type, dev,
500 if (rc >= sizeof(devices)) {
506 return get_param(devices, value, val_len);
514 * TYPE one of llite, lmv, lov.
515 * /proc/fs/lustre/TYPE/INST the directory of interest.
517 static int get_param_cli(const char *type, const char *inst,
518 const char *param, char *buf, size_t buf_size)
520 char param_path[PATH_MAX + 1];
521 FILE *param_file = NULL;
524 snprintf(param_path, sizeof(param_path),
525 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
527 param_file = fopen(param_path, "r");
528 if (param_file == NULL) {
533 if (fgets(buf, buf_size, param_file) == NULL) {
540 if (param_file != NULL)
546 static int get_param_llite(const char *path,
547 const char *param, char *buf, size_t buf_size)
552 rc = llapi_getname(path, inst, sizeof(inst));
556 return get_param_cli("llite", inst, param, buf, buf_size);
559 static int get_param_lov(const char *path,
560 const char *param, char *buf, size_t buf_size)
562 struct obd_uuid uuid;
565 rc = llapi_file_get_lov_uuid(path, &uuid);
569 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
572 static int get_param_lmv(const char *path,
573 const char *param, char *buf, size_t buf_size)
575 struct obd_uuid uuid;
578 rc = llapi_file_get_lmv_uuid(path, &uuid);
582 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
585 static int get_mds_md_size(const char *path)
587 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
591 /* Get the max ea size from llite proc. */
592 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
604 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
606 return get_param_lmv(path, "uuid", buf, bufsize);
610 * if pool is NULL, search ostname in target_obd
611 * if pool is not NULL:
612 * if pool not found returns errno < 0
613 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
614 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
616 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
619 char buffer[PATH_MAX + 1];
623 len = strlen(ostname);
625 if (poolname == NULL) {
629 rc = find_target_obdpath(fsname, buffer);
631 rc = find_poolpath(fsname, poolname, buffer);
636 fd = fopen(buffer, "r");
640 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
641 if (poolname == NULL) {
643 /* Search for an ostname in the list of OSTs
644 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
645 ptr = strchr(buffer, ' ');
647 (strncmp(ptr + 1, ostname, len) == 0)) {
652 /* Search for an ostname in a pool,
653 (or an existing non-empty pool if no ostname) */
654 if ((ostname == NULL) ||
655 (strncmp(buffer, ostname, len) == 0)) {
666 * Open a Lustre file.
668 * \param name the name of the file to be opened
669 * \param flags access mode, see flags in open(2)
670 * \param mode permission of the file if it is created, see mode in open(2)
671 * \param param stripe pattern of the newly created file
673 * \retval file descriptor of opened file
674 * \retval negative errno on failure
676 int llapi_file_open_param(const char *name, int flags, mode_t mode,
677 const struct llapi_stripe_param *param)
679 char fsname[MAX_OBD_NAME + 1] = { 0 };
680 char *pool_name = param->lsp_pool;
681 struct lov_user_md *lum = NULL;
682 size_t lum_size = sizeof(*lum);
685 /* Make sure we are on a Lustre file system */
686 rc = llapi_search_fsname(name, fsname);
688 llapi_error(LLAPI_MSG_ERROR, rc,
689 "'%s' is not on a Lustre filesystem",
694 /* Check if the stripe pattern is sane. */
695 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
696 param->lsp_stripe_offset,
697 param->lsp_stripe_count,
698 param->lsp_stripe_pattern);
702 /* Make sure we have a good pool */
703 if (pool_name != NULL) {
704 /* in case user gives the full pool name <fsname>.<poolname>,
705 * strip the fsname */
706 char *ptr = strchr(pool_name, '.');
709 if (strcmp(pool_name, fsname) != 0) {
711 llapi_err_noerrno(LLAPI_MSG_ERROR,
712 "Pool '%s' is not on filesystem '%s'",
719 /* Make sure the pool exists and is non-empty */
720 rc = llapi_search_ost(fsname, pool_name, NULL);
722 char *err = rc == 0 ? "has no OSTs" : "does not exist";
724 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
725 fsname, pool_name, err);
729 lum_size = sizeof(struct lov_user_md_v3);
732 /* sanity check of target list */
733 if (param->lsp_is_specific) {
734 char ostname[MAX_OBD_NAME + 1];
738 for (i = 0; i < param->lsp_stripe_count; i++) {
739 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
740 fsname, param->lsp_osts[i]);
741 rc = llapi_search_ost(fsname, pool_name, ostname);
746 llapi_error(LLAPI_MSG_ERROR, rc,
747 "%s: cannot find OST %s in %s",
754 /* Make sure stripe offset is in OST list. */
755 if (param->lsp_osts[i] == param->lsp_stripe_offset)
759 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
760 "%s: stripe offset '%d' is not in the "
762 __func__, param->lsp_stripe_offset);
766 lum_size = lov_user_md_size(param->lsp_stripe_count,
767 LOV_USER_MAGIC_SPECIFIC);
770 lum = calloc(1, lum_size);
775 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
777 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
778 flags = O_DIRECTORY | O_RDONLY;
785 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
790 /* Initialize IOCTL striping pattern structure */
791 lum->lmm_magic = LOV_USER_MAGIC_V1;
792 lum->lmm_pattern = param->lsp_stripe_pattern;
793 lum->lmm_stripe_size = param->lsp_stripe_size;
794 lum->lmm_stripe_count = param->lsp_stripe_count;
795 lum->lmm_stripe_offset = param->lsp_stripe_offset;
796 if (pool_name != NULL) {
797 struct lov_user_md_v3 *lumv3 = (void *)lum;
799 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
800 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
802 if (param->lsp_is_specific) {
803 struct lov_user_md_v3 *lumv3 = (void *)lum;
806 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
807 if (pool_name == NULL) {
808 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
809 * OST list, therefore if pool is not specified we have
810 * to pack a null pool name for placeholder. */
811 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
814 for (i = 0; i < param->lsp_stripe_count; i++)
815 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
818 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
819 char *errmsg = "stripe already set";
822 if (errno != EEXIST && errno != EALREADY)
823 errmsg = strerror(errno);
825 llapi_err_noerrno(LLAPI_MSG_ERROR,
826 "error on ioctl "LPX64" for '%s' (%d): %s",
827 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,
839 int llapi_file_open_pool(const char *name, int flags, int mode,
840 unsigned long long stripe_size, int stripe_offset,
841 int stripe_count, int stripe_pattern, char *pool_name)
843 const struct llapi_stripe_param param = {
844 .lsp_stripe_size = stripe_size,
845 .lsp_stripe_count = stripe_count,
846 .lsp_stripe_pattern = stripe_pattern,
847 .lsp_stripe_offset = stripe_offset,
848 .lsp_pool = pool_name
850 return llapi_file_open_param(name, flags, mode, ¶m);
853 int llapi_file_open(const char *name, int flags, int mode,
854 unsigned long long stripe_size, int stripe_offset,
855 int stripe_count, int stripe_pattern)
857 return llapi_file_open_pool(name, flags, mode, stripe_size,
858 stripe_offset, stripe_count,
859 stripe_pattern, NULL);
862 int llapi_file_create(const char *name, unsigned long long stripe_size,
863 int stripe_offset, int stripe_count, int stripe_pattern)
867 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
868 stripe_offset, stripe_count, stripe_pattern,
877 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
878 int stripe_offset, int stripe_count,
879 int stripe_pattern, char *pool_name)
883 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
884 stripe_offset, stripe_count, stripe_pattern,
893 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
894 int stripe_count, int stripe_pattern,
895 const char *pool_name)
897 struct lmv_user_md lum = { 0 };
901 lum.lum_magic = LMV_USER_MAGIC;
902 lum.lum_stripe_offset = stripe_offset;
903 lum.lum_stripe_count = stripe_count;
904 lum.lum_hash_type = stripe_pattern;
905 if (pool_name != NULL) {
906 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
907 llapi_err_noerrno(LLAPI_MSG_ERROR,
908 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
909 ": too large pool name: %s", name, pool_name);
912 strncpy(lum.lum_pool_name, pool_name,
913 sizeof(lum.lum_pool_name));
916 fd = open(name, O_DIRECTORY | O_RDONLY);
919 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
923 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
925 char *errmsg = "stripe already set";
927 if (errno != EEXIST && errno != EALREADY)
928 errmsg = strerror(errno);
930 llapi_err_noerrno(LLAPI_MSG_ERROR,
931 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
938 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
939 int stripe_count, int stripe_pattern,
940 const char *pool_name)
942 struct lmv_user_md lmu = { 0 };
943 struct obd_ioctl_data data = { 0 };
946 char *dirpath = NULL;
947 char *namepath = NULL;
953 dirpath = strdup(name);
954 namepath = strdup(name);
955 if (!dirpath || !namepath)
958 lmu.lum_magic = LMV_USER_MAGIC;
959 lmu.lum_stripe_offset = stripe_offset;
960 lmu.lum_stripe_count = stripe_count;
961 lmu.lum_hash_type = stripe_pattern;
962 if (pool_name != NULL) {
963 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
964 llapi_err_noerrno(LLAPI_MSG_ERROR,
965 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
966 "pool name: %s", name, pool_name);
970 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
973 filename = basename(namepath);
974 dir = dirname(dirpath);
976 data.ioc_inlbuf1 = (char *)filename;
977 data.ioc_inllen1 = strlen(filename) + 1;
978 data.ioc_inlbuf2 = (char *)&lmu;
979 data.ioc_inllen2 = sizeof(struct lmv_user_md);
980 data.ioc_type = mode;
981 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
983 llapi_error(LLAPI_MSG_ERROR, rc,
984 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
989 fd = open(dir, O_DIRECTORY | O_RDONLY);
992 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
996 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
997 char *errmsg = "stripe already set";
999 if (errno != EEXIST && errno != EALREADY)
1000 errmsg = strerror(errno);
1002 llapi_err_noerrno(LLAPI_MSG_ERROR,
1003 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
1013 int llapi_direntry_remove(char *dname)
1015 char *dirpath = NULL;
1016 char *namepath = NULL;
1022 dirpath = strdup(dname);
1023 namepath = strdup(dname);
1024 if (!dirpath || !namepath)
1027 filename = basename(namepath);
1029 dir = dirname(dirpath);
1031 fd = open(dir, O_DIRECTORY | O_RDONLY);
1034 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1039 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1040 char *errmsg = strerror(errno);
1041 llapi_err_noerrno(LLAPI_MSG_ERROR,
1042 "error on ioctl "LPX64" for '%s' (%d): %s",
1043 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
1055 * Find the fsname, the full path, and/or an open fd.
1056 * Either the fsname or path must not be NULL
1058 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1061 char buf[PATH_MAX], mntdir[PATH_MAX];
1064 int idx = 0, len = 0, mntlen, fd;
1067 /* get the mount point */
1068 fp = setmntent(MOUNTED, "r");
1071 llapi_error(LLAPI_MSG_ERROR, rc,
1072 "setmntent(%s) failed", MOUNTED);
1076 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1079 if (!llapi_is_lustre_mnt(&mnt))
1082 if ((want & WANT_INDEX) && (idx++ != index))
1085 mntlen = strlen(mnt.mnt_dir);
1086 ptr = strrchr(mnt.mnt_fsname, '/');
1087 /* thanks to the call to llapi_is_lustre_mnt() above,
1088 * we are sure that mnt.mnt_fsname contains ":/",
1089 * so ptr should never be NULL */
1094 /* Check the fsname for a match, if given */
1095 if (!(want & WANT_FSNAME) && fsname != NULL &&
1096 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
1099 /* If the path isn't set return the first one we find */
1100 if (path == NULL || strlen(path) == 0) {
1101 strcpy(mntdir, mnt.mnt_dir);
1102 if ((want & WANT_FSNAME) && fsname != NULL)
1103 strcpy(fsname, ptr);
1106 /* Otherwise find the longest matching path */
1107 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1108 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1109 strcpy(mntdir, mnt.mnt_dir);
1111 if ((want & WANT_FSNAME) && fsname != NULL)
1112 strcpy(fsname, ptr);
1120 if ((want & WANT_PATH) && path != NULL)
1121 strcpy(path, mntdir);
1122 if (want & WANT_FD) {
1123 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1126 llapi_error(LLAPI_MSG_ERROR, rc,
1127 "error opening '%s'", mntdir);
1133 } else if (want & WANT_ERROR)
1134 llapi_err_noerrno(LLAPI_MSG_ERROR,
1135 "can't find fs root for '%s': %d",
1136 (want & WANT_PATH) ? fsname : path, rc);
1141 * search lustre mounts
1143 * Calling this function will return to the user the mount point, mntdir, and
1144 * the file system name, fsname, if the user passed a buffer to this routine.
1146 * The user inputs are pathname and index. If the pathname is supplied then
1147 * the value of the index will be ignored. The pathname will return data if
1148 * the pathname is located on a lustre mount. Index is used to pick which
1149 * mount point you want in the case of multiple mounted lustre file systems.
1150 * See function lfs_osts in lfs.c for an example of the index use.
1152 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1155 int want = WANT_PATH, idx = -1;
1157 if (!pathname || pathname[0] == '\0') {
1161 strcpy(mntdir, pathname);
1164 want |= WANT_FSNAME;
1165 return get_root_path(want, fsname, NULL, mntdir, idx);
1168 /* Given a path, find the corresponding Lustre fsname */
1169 int llapi_search_fsname(const char *pathname, char *fsname)
1174 path = realpath(pathname, NULL);
1176 char buf[PATH_MAX], *ptr;
1179 if (pathname[0] != '/') {
1180 /* Need an absolute path, but realpath() only works for
1181 * pathnames that actually exist. We go through the
1182 * extra hurdle of dirname(getcwd() + pathname) in
1183 * case the relative pathname contains ".." in it. */
1184 if (getcwd(buf, sizeof(buf) - 2) == NULL)
1186 rc = strlcat(buf, "/", sizeof(buf));
1187 if (rc >= sizeof(buf))
1190 rc = strlcat(buf, pathname, sizeof(buf));
1191 if (rc >= sizeof(buf))
1193 path = realpath(buf, NULL);
1195 ptr = strrchr(buf, '/');
1199 path = realpath(buf, NULL);
1202 llapi_error(LLAPI_MSG_ERROR, rc,
1203 "pathname '%s' cannot expand",
1209 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1214 int llapi_search_rootpath(char *pathname, const char *fsname)
1216 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1219 int llapi_getname(const char *path, char *buf, size_t size)
1221 struct obd_uuid uuid_buf;
1222 char *uuid = uuid_buf.uuid;
1225 memset(&uuid_buf, 0, sizeof(uuid_buf));
1226 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1230 /* We want to turn lustre-clilov-ffff88002738bc00 into
1231 * lustre-ffff88002738bc00. */
1233 nr = snprintf(buf, size, "%.*s-%s",
1234 (int) (strlen(uuid) - 24), uuid,
1235 uuid + strlen(uuid) - 16);
1245 * find the pool directory path under /proc
1246 * (can be also used to test if a fsname is known)
1248 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1251 char pattern[PATH_MAX + 1];
1252 char buffer[PATH_MAX];
1254 if (fsname == NULL) {
1255 rc = llapi_search_fsname(pathname, buffer);
1259 strcpy(pathname, fsname);
1262 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1263 rc = first_match(pattern, buffer);
1267 /* in fsname test mode, pool_pathname is NULL */
1268 if (pool_pathname != NULL)
1269 strcpy(pool_pathname, buffer);
1275 * Get the list of pool members.
1276 * \param poolname string of format \<fsname\>.\<poolname\>
1277 * \param members caller-allocated array of char*
1278 * \param list_size size of the members array
1279 * \param buffer caller-allocated buffer for storing OST names
1280 * \param buffer_size size of the buffer
1282 * \return number of members retrieved for this pool
1283 * \retval -error failure
1285 int llapi_get_poolmembers(const char *poolname, char **members,
1286 int list_size, char *buffer, int buffer_size)
1288 char fsname[PATH_MAX];
1290 char pathname[PATH_MAX];
1291 char path[PATH_MAX];
1298 /* name is FSNAME.POOLNAME */
1299 if (strlen(poolname) >= sizeof(fsname))
1301 strlcpy(fsname, poolname, sizeof(fsname));
1302 pool = strchr(fsname, '.');
1309 rc = poolpath(fsname, NULL, pathname);
1311 llapi_error(LLAPI_MSG_ERROR, rc,
1312 "Lustre filesystem '%s' not found",
1317 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1318 rc = snprintf(path, sizeof(path), "%s/%s", pathname, pool);
1319 if (rc >= sizeof(path))
1321 fd = fopen(path, "r");
1324 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1329 while (fgets(buf, sizeof(buf), fd) != NULL) {
1330 if (nb_entries >= list_size) {
1334 buf[sizeof(buf) - 1] = '\0';
1336 tmp = strchr(buf, '\n');
1339 if (used + strlen(buf) + 1 > buffer_size) {
1344 strcpy(buffer + used, buf);
1345 members[nb_entries] = buffer + used;
1346 used += strlen(buf) + 1;
1356 * Get the list of pools in a filesystem.
1357 * \param name filesystem name or path
1358 * \param poollist caller-allocated array of char*
1359 * \param list_size size of the poollist array
1360 * \param buffer caller-allocated buffer for storing pool names
1361 * \param buffer_size size of the buffer
1363 * \return number of pools retrieved for this filesystem
1364 * \retval -error failure
1366 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1367 char *buffer, int buffer_size)
1369 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1373 struct dirent *cookie = NULL;
1375 unsigned int nb_entries = 0;
1376 unsigned int used = 0;
1379 /* initialize output array */
1380 for (i = 0; i < list_size; i++)
1383 /* is name a pathname ? */
1384 ptr = strchr(name, '/');
1386 /* only absolute pathname is supported */
1390 if (!realpath(name, rname)) {
1392 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1397 rc = poolpath(NULL, rname, pathname);
1399 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1400 " a Lustre filesystem", name);
1403 if (strlen(rname) > sizeof(fsname)-1)
1405 strncpy(fsname, rname, sizeof(fsname));
1407 /* name is FSNAME */
1408 if (strlen(name) > sizeof(fsname)-1)
1410 strncpy(fsname, name, sizeof(fsname));
1411 rc = poolpath(fsname, NULL, pathname);
1414 llapi_error(LLAPI_MSG_ERROR, rc,
1415 "Lustre filesystem '%s' not found", name);
1419 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1420 dir = opendir(pathname);
1423 llapi_error(LLAPI_MSG_ERROR, rc,
1424 "Could not open pool list for '%s'",
1430 rc = readdir_r(dir, &pool, &cookie);
1434 llapi_error(LLAPI_MSG_ERROR, rc,
1435 "Error reading pool list for '%s'", name);
1437 } else if ((rc == 0) && (cookie == NULL)) {
1438 /* end of directory */
1442 /* ignore . and .. */
1443 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1446 /* check output bounds */
1447 if (nb_entries >= list_size) {
1452 /* +2 for '.' and final '\0' */
1453 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1459 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1460 poollist[nb_entries] = buffer + used;
1461 used += strlen(pool.d_name) + strlen(fsname) + 2;
1467 return ((rc != 0) ? rc : nb_entries);
1470 /* wrapper for lfs.c and obd.c */
1471 int llapi_poollist(const char *name)
1473 /* list of pool names (assume that pool count is smaller
1475 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1476 int obdcount, bufsize, rc, nb, i;
1477 char *poolname = NULL, *tmp = NULL, data[16];
1482 if (name[0] != '/') {
1483 fsname = strdup(name);
1487 poolname = strchr(fsname, '.');
1491 path = (char *) name;
1494 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1495 data, sizeof(data));
1498 obdcount = atoi(data);
1500 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1501 * and also an array to store the pointers for all that
1502 * allocated space. */
1504 bufsize = sizeof(struct obd_uuid) * obdcount;
1505 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1506 if (buffer == NULL) {
1510 list = (char **) (buffer + bufsize);
1513 /* name is a path or fsname */
1514 nb = llapi_get_poollist(name, list, obdcount,
1517 /* name is a pool name (<fsname>.<poolname>) */
1518 nb = llapi_get_poolmembers(name, list, obdcount,
1522 if (nb == -EOVERFLOW) {
1525 goto retry_get_pools;
1528 for (i = 0; i < nb; i++)
1529 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1530 rc = (nb < 0 ? nb : 0);
1539 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1540 void *data, struct dirent64 *de);
1542 #define OBD_NOT_FOUND (-1)
1544 static int common_param_init(struct find_param *param, char *path)
1546 int lum_size = get_mds_md_size(path);
1548 if (lum_size < PATH_MAX + 1)
1549 lum_size = PATH_MAX + 1;
1551 param->fp_lum_size = lum_size;
1552 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1553 if (param->fp_lmd == NULL) {
1554 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1555 "error: allocation of %zu bytes for ioctl",
1556 sizeof(lstat_t) + param->fp_lum_size);
1560 param->fp_lmv_stripe_count = 256;
1561 param->fp_lmv_md = calloc(1,
1562 lmv_user_md_size(param->fp_lmv_stripe_count,
1564 if (param->fp_lmv_md == NULL) {
1565 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1566 "error: allocation of %d bytes for ioctl",
1567 lmv_user_md_size(param->fp_lmv_stripe_count,
1572 param->fp_got_uuids = 0;
1573 param->fp_obd_indexes = NULL;
1574 param->fp_obd_index = OBD_NOT_FOUND;
1575 if (!param->fp_migrate)
1576 param->fp_mdt_index = OBD_NOT_FOUND;
1580 static void find_param_fini(struct find_param *param)
1582 if (param->fp_obd_indexes)
1583 free(param->fp_obd_indexes);
1586 free(param->fp_lmd);
1588 if (param->fp_lmv_md)
1589 free(param->fp_lmv_md);
1592 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1593 struct dirent64 *de)
1595 struct find_param *param = data;
1601 /* set errno upon failure */
1602 static DIR *opendir_parent(char *path)
1608 fname = strrchr(path, '/');
1610 return opendir(".");
1614 parent = opendir(path);
1619 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1624 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1625 if (param->fp_get_default_lmv)
1626 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1628 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1630 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1631 if (errno == E2BIG && ret != 0) {
1635 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1636 if (stripe_count <= param->fp_lmv_stripe_count)
1639 free(param->fp_lmv_md);
1640 param->fp_lmv_stripe_count = stripe_count;
1641 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1642 param->fp_lmv_md = malloc(lmv_size);
1643 if (param->fp_lmv_md == NULL) {
1644 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1645 "error: allocation of %d bytes for ioctl",
1646 lmv_user_md_size(param->fp_lmv_stripe_count,
1655 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1656 struct lov_user_mds_data *lmd, int lumlen)
1658 lstat_t *st = &lmd->lmd_st;
1661 if (parent == NULL && dir == NULL)
1665 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1666 } else if (parent) {
1667 char *fname = strrchr(path, '/');
1669 /* To avoid opening, locking, and closing each file on the
1670 * client if that is not needed. The GETFILEINFO ioctl can
1671 * be done on the patent dir with a single open for all
1672 * files in that directory, and it also doesn't pollute the
1673 * client dcache with millions of dentries when traversing
1674 * a large filesystem. */
1675 fname = (fname == NULL ? path : fname + 1);
1676 /* retrieve needed file info */
1677 strlcpy((char *)lmd, fname, lumlen);
1678 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1682 if (errno == ENOTTY) {
1683 /* ioctl is not supported, it is not a lustre fs.
1684 * Do the regular lstat(2) instead. */
1685 ret = lstat_f(path, st);
1688 llapi_error(LLAPI_MSG_ERROR, ret,
1689 "error: %s: lstat failed for %s",
1692 } else if (errno == ENOENT) {
1694 llapi_error(LLAPI_MSG_WARN, ret,
1695 "warning: %s: %s does not exist",
1697 } else if (errno != EISDIR) {
1699 llapi_error(LLAPI_MSG_ERROR, ret,
1700 "%s ioctl failed for %s.",
1701 dir ? "LL_IOC_MDC_GETINFO" :
1702 "IOC_MDC_GETFILEINFO", path);
1705 llapi_error(LLAPI_MSG_ERROR, ret,
1706 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1713 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1714 semantic_func_t sem_init,
1715 semantic_func_t sem_fini, void *data,
1716 struct dirent64 *de)
1718 struct find_param *param = (struct find_param *)data;
1719 struct dirent64 *dent;
1727 if (!d && errno != ENOTDIR) {
1729 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1732 } else if (!d && !parent) {
1733 /* ENOTDIR. Open the parent dir. */
1734 p = opendir_parent(path);
1741 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1747 while ((dent = readdir64(d)) != NULL) {
1750 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1753 /* Don't traverse .lustre directory */
1754 if (!(strcmp(dent->d_name, dot_lustre_name)))
1758 if ((len + dent->d_reclen + 2) > size) {
1759 llapi_err_noerrno(LLAPI_MSG_ERROR,
1760 "error: %s: string buffer is too small",
1765 strcat(path, dent->d_name);
1767 if (dent->d_type == DT_UNKNOWN) {
1768 lstat_t *st = ¶m->fp_lmd->lmd_st;
1770 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1771 param->fp_lum_size);
1773 dent->d_type = IFTODT(st->st_mode);
1780 switch (dent->d_type) {
1782 llapi_err_noerrno(LLAPI_MSG_ERROR,
1783 "error: %s: '%s' is UNKNOWN type %d",
1784 __func__, dent->d_name, dent->d_type);
1787 rc = llapi_semantic_traverse(path, size, d, sem_init,
1788 sem_fini, data, dent);
1789 if (rc != 0 && ret == 0)
1795 rc = sem_init(path, d, NULL, data, dent);
1796 if (rc < 0 && ret == 0)
1799 if (sem_fini && rc == 0)
1800 sem_fini(path, d, NULL, data, dent);
1808 sem_fini(path, parent, &d, data, de);
1817 static int param_callback(char *path, semantic_func_t sem_init,
1818 semantic_func_t sem_fini, struct find_param *param)
1820 int ret, len = strlen(path);
1823 if (len > PATH_MAX) {
1825 llapi_error(LLAPI_MSG_ERROR, ret,
1826 "Path name '%s' is too long", path);
1830 buf = (char *)malloc(PATH_MAX + 1);
1834 strlcpy(buf, path, PATH_MAX + 1);
1835 ret = common_param_init(param, buf);
1839 param->fp_depth = 0;
1841 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1842 sem_fini, param, NULL);
1844 find_param_fini(param);
1846 return ret < 0 ? ret : 0;
1849 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1851 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1854 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1859 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1861 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1864 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1869 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1873 fd = open(path, O_RDONLY);
1876 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1880 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1886 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1890 fd = open(path, O_RDONLY);
1893 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1897 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1908 * If uuidp is NULL, return the number of available obd uuids.
1909 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1910 * there are more OSTs than allocated to uuidp, then an error is returned with
1911 * the ost_count set to number of available obd uuids.
1913 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1914 int *ost_count, enum tgt_type type)
1916 struct obd_uuid name;
1920 int rc = 0, index = 0;
1922 /* Get the lov name */
1923 if (type == LOV_TYPE) {
1924 rc = llapi_file_fget_lov_uuid(fd, &name);
1928 rc = llapi_file_fget_lmv_uuid(fd, &name);
1933 /* Now get the ost uuids from /proc */
1934 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1935 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1936 fp = fopen(buf, "r");
1939 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1943 snprintf(format, sizeof(format),
1944 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1945 while (fgets(buf, sizeof(buf), fp) != NULL) {
1946 if (uuidp && (index < *ost_count)) {
1947 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1955 if (uuidp && (index > *ost_count))
1962 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1964 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1967 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1972 root = opendir(mnt);
1975 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1980 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1988 /* Check if user specified value matches a real uuid. Ignore _UUID,
1989 * -osc-4ba41334, other trailing gunk in comparison.
1990 * @param real_uuid ends in "_UUID"
1991 * @param search_uuid may or may not end in "_UUID"
1993 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1995 int cmplen = strlen(real_uuid);
1996 int searchlen = strlen(search_uuid);
1998 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2000 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2003 /* The UUIDs may legitimately be different lengths, if
2004 * the system was upgraded from an older version. */
2005 if (cmplen != searchlen)
2008 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2011 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2012 * returned in param->fp_obd_index */
2013 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
2015 struct obd_uuid obd_uuid;
2021 if (param->fp_got_uuids)
2024 /* Get the lov/lmv name */
2025 if (param->fp_get_lmv)
2026 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
2028 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
2030 if (rc != -ENOTTY) {
2031 llapi_error(LLAPI_MSG_ERROR, rc,
2032 "error: can't get lov name: %s", dname);
2039 param->fp_got_uuids = 1;
2041 /* Now get the ost uuids from /proc */
2042 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
2043 param->fp_get_lmv ? "lmv" : "lov", obd_uuid.uuid);
2044 fp = fopen(buf, "r");
2047 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
2051 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2052 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2053 param->fp_get_lmv ? "MDTS" : "OBDS:");
2055 snprintf(format, sizeof(format),
2056 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2057 while (fgets(buf, sizeof(buf), fp) != NULL) {
2060 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2063 if (param->fp_obd_uuid) {
2064 if (llapi_uuid_match(obd_uuid.uuid,
2065 param->fp_obd_uuid->uuid)) {
2066 param->fp_obd_index = index;
2069 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2070 /* Print everything */
2071 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2074 param->fp_obds_printed = 1;
2078 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2079 llapi_err_noerrno(LLAPI_MSG_ERROR,
2080 "error: %s: unknown obduuid: %s",
2081 __func__, param->fp_obd_uuid->uuid);
2088 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2089 * obd index for all these obduuids will be returned in
2090 * param->fp_obd_indexes */
2091 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2092 int num_obds, int **obdindexes, int *obdindex,
2095 int ret, obdcount, obd_valid = 0, obdnum;
2097 struct obd_uuid *uuids = NULL;
2101 if (type == LOV_TYPE)
2102 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2104 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2108 obdcount = atoi(buf);
2109 uuids = (struct obd_uuid *)malloc(obdcount *
2110 sizeof(struct obd_uuid));
2115 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2117 struct obd_uuid *uuids_temp;
2119 if (ret == -EOVERFLOW) {
2120 uuids_temp = realloc(uuids, obdcount *
2121 sizeof(struct obd_uuid));
2122 if (uuids_temp != NULL) {
2124 goto retry_get_uuids;
2130 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
2134 indexes = malloc(num_obds * sizeof(*obdindex));
2135 if (indexes == NULL) {
2140 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2143 /* The user may have specified a simple index */
2144 i = strtol(obduuids[obdnum].uuid, &end, 0);
2145 if (end && *end == '\0' && i < obdcount) {
2146 indexes[obdnum] = i;
2149 for (i = 0; i < obdcount; i++) {
2150 if (llapi_uuid_match(uuids[i].uuid,
2151 obduuids[obdnum].uuid)) {
2152 indexes[obdnum] = i;
2158 if (i >= obdcount) {
2159 indexes[obdnum] = OBD_NOT_FOUND;
2160 llapi_err_noerrno(LLAPI_MSG_ERROR,
2161 "error: %s: unknown obduuid: %s",
2162 __func__, obduuids[obdnum].uuid);
2168 *obdindex = OBD_NOT_FOUND;
2170 *obdindex = obd_valid;
2172 *obdindexes = indexes;
2180 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2184 if (param->fp_mdt_uuid) {
2185 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2187 ¶m->fp_mdt_indexes,
2188 ¶m->fp_mdt_index, LMV_TYPE);
2193 if (param->fp_obd_uuid) {
2194 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2196 ¶m->fp_obd_indexes,
2197 ¶m->fp_obd_index, LOV_TYPE);
2202 param->fp_got_uuids = 1;
2207 int llapi_ostlist(char *path, struct find_param *param)
2212 dir = opendir(path);
2216 ret = setup_obd_uuid(dir, path, param);
2223 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2224 * tries to determine the path to the filesystem's clilov directory under /proc
2226 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2227 * The NUL terminator is compensated by the additional "%s" bytes. */
2228 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2229 static int clilovpath(const char *fsname, const char *const pathname,
2233 char pattern[LOV_LEN];
2234 char buffer[PATH_MAX + 1];
2236 if (fsname == NULL) {
2237 rc = llapi_search_fsname(pathname, buffer);
2243 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2246 rc = first_match(pattern, buffer);
2250 strlcpy(clilovpath, buffer, sizeof(buffer));
2256 * Given the path to a stripe attribute proc file, tries to open and
2257 * read the attribute and return the value using the attr parameter
2259 static int sattr_read_attr(const char *const fpath,
2264 char line[PATH_MAX + 1];
2267 f = fopen(fpath, "r");
2270 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2274 if (fgets(line, sizeof(line), f) != NULL) {
2277 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2286 * Tries to determine the default stripe attributes for a given filesystem. The
2287 * filesystem to check should be specified by fsname, or will be determined
2290 static int sattr_get_defaults(const char *const fsname,
2291 const char *const pathname,
2292 unsigned int *scount,
2293 unsigned int *ssize,
2294 unsigned int *soffset)
2297 char dpath[PATH_MAX + 1];
2298 char fpath[PATH_MAX + 1];
2300 rc = clilovpath(fsname, pathname, dpath);
2305 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2306 rc = sattr_read_attr(fpath, scount);
2312 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2313 rc = sattr_read_attr(fpath, ssize);
2319 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2320 rc = sattr_read_attr(fpath, soffset);
2329 * Tries to gather the default stripe attributes for a given filesystem. If
2330 * the attributes can be determined, they are cached for easy retreival the
2331 * next time they are needed. Only a single filesystem's attributes are
2334 static int sattr_cache_get_defaults(const char *const fsname,
2335 const char *const pathname,
2336 unsigned int *scount,
2337 unsigned int *ssize,
2338 unsigned int *soffset)
2341 char fsname[PATH_MAX + 1];
2342 unsigned int stripecount;
2343 unsigned int stripesize;
2344 unsigned int stripeoffset;
2350 char fsname_buf[PATH_MAX + 1];
2351 unsigned int tmp[3];
2353 if (fsname == NULL) {
2354 rc = llapi_search_fsname(pathname, fsname_buf);
2358 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2361 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2363 * Ensure all 3 sattrs (count, size, and offset) are
2364 * successfully retrieved and stored in tmp before writing to
2367 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2372 cache.stripecount = tmp[0];
2373 cache.stripesize = tmp[1];
2374 cache.stripeoffset = tmp[2];
2375 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2379 *scount = cache.stripecount;
2381 *ssize = cache.stripesize;
2383 *soffset = cache.stripeoffset;
2388 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2389 struct lov_user_ost_data_v1 *objects,
2390 int is_dir, int verbose, int depth,
2391 int raw, char *pool_name)
2393 char *prefix = is_dir ? "" : "lmm_";
2394 char *separator = "";
2397 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2398 lmm_oi_set_seq(&lum->lmm_oi, 0);
2399 if (verbose & VERBOSE_DETAIL)
2400 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2403 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2404 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2406 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2407 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2409 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2410 lmm_oi_seq(&lum->lmm_oi));
2411 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2412 lmm_oi_id(&lum->lmm_oi));
2415 if (verbose & VERBOSE_COUNT) {
2416 if (verbose & ~VERBOSE_COUNT)
2417 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2420 if (!raw && lum->lmm_stripe_count == 0) {
2421 unsigned int scount;
2422 rc = sattr_cache_get_defaults(NULL, path,
2426 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2429 llapi_error(LLAPI_MSG_ERROR, rc,
2430 "Cannot determine default"
2433 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2434 lum->lmm_stripe_count ==
2435 (typeof(lum->lmm_stripe_count))(-1)
2436 ? -1 : lum->lmm_stripe_count);
2439 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2440 (__s16)lum->lmm_stripe_count);
2442 separator = is_dir ? " " : "\n";
2445 if (verbose & VERBOSE_SIZE) {
2446 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2447 if (verbose & ~VERBOSE_SIZE)
2448 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2450 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2452 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2455 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2457 llapi_error(LLAPI_MSG_ERROR, rc,
2458 "Cannot determine default"
2461 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2462 lum->lmm_stripe_size);
2464 separator = is_dir ? " " : "\n";
2467 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2468 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2469 if (verbose & ~VERBOSE_LAYOUT)
2470 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2472 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2476 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2477 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2478 if (verbose & ~VERBOSE_GENERATION)
2479 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2481 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2482 (int)lum->lmm_layout_gen);
2486 if (verbose & VERBOSE_OFFSET) {
2487 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2488 if (verbose & ~VERBOSE_OFFSET)
2489 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2492 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2493 lum->lmm_stripe_offset ==
2494 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2495 lum->lmm_stripe_offset);
2497 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2498 objects[0].l_ost_idx);
2499 separator = is_dir ? " " : "\n";
2502 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2503 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2504 if (verbose & ~VERBOSE_POOL)
2505 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2507 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2510 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2511 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2514 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2515 struct lov_user_ost_data_v1 *objects,
2516 char *path, int is_dir, int obdindex,
2517 int depth, int header, int raw)
2519 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2522 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2523 if (obdindex == objects[i].l_ost_idx) {
2531 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2532 depth, raw, pool_name);
2534 if (!is_dir && (header & VERBOSE_OBJID) &&
2535 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2537 llapi_printf(LLAPI_MSG_NORMAL,
2538 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2540 for (i = 0; i < lum->lmm_stripe_count; i++) {
2541 int idx = objects[i].l_ost_idx;
2542 long long oid = ostid_id(&objects[i].l_ost_oi);
2543 long long gr = ostid_seq(&objects[i].l_ost_oi);
2544 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2546 sprintf(fmt, "%s%s%s\n",
2547 "\t%6u\t%14llu\t%#13llx\t",
2548 (fid_seq_is_rsvd(gr) ||
2549 fid_seq_is_mdt0(gr)) ?
2550 "%14llu" : "%#14llx", "%s");
2551 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2553 obdindex == idx ? " *" : "");
2557 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2561 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2562 char *path, int obdindex, int depth, int verbose)
2564 struct lmv_user_mds_data *objects = lum->lum_objects;
2565 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2566 int i, obdstripe = 0;
2567 char *separator = "";
2569 if (obdindex != OBD_NOT_FOUND) {
2570 if (lum->lum_stripe_count == 0) {
2571 if (obdindex == lum->lum_stripe_offset)
2574 for (i = 0; i < lum->lum_stripe_count; i++) {
2575 if (obdindex == objects[i].lum_mds) {
2576 llapi_printf(LLAPI_MSG_NORMAL,
2591 /* show all information default */
2593 if (lum->lum_magic == LMV_USER_MAGIC)
2594 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2596 verbose = VERBOSE_OBJID;
2599 if (depth && path && ((verbose != VERBOSE_OBJID)))
2600 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2602 if (verbose & VERBOSE_COUNT) {
2603 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2604 if (verbose & ~VERBOSE_COUNT)
2605 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2606 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2607 (int)lum->lum_stripe_count);
2608 if (verbose & VERBOSE_OFFSET)
2614 if (verbose & VERBOSE_OFFSET) {
2615 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2616 if (verbose & ~VERBOSE_OFFSET)
2617 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2618 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2619 (int)lum->lum_stripe_offset);
2623 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2624 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2625 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2626 llapi_printf(LLAPI_MSG_NORMAL,
2627 "mdtidx\t\t FID[seq:oid:ver]\n");
2628 for (i = 0; i < lum->lum_stripe_count; i++) {
2629 int idx = objects[i].lum_mds;
2630 struct lu_fid *fid = &objects[i].lum_fid;
2631 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2632 llapi_printf(LLAPI_MSG_NORMAL,
2633 "%6u\t\t "DFID"\t\t%s\n",
2635 obdindex == idx ? " *" : "");
2640 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2641 pool_name[0] != '\0') {
2642 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2643 if (verbose & ~VERBOSE_POOL)
2644 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2646 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2650 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2651 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2654 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2658 if (param->fp_get_lmv || param->fp_get_default_lmv)
2659 magic = (__u32)param->fp_lmv_md->lum_magic;
2661 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2664 case LOV_USER_MAGIC_V1:
2665 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2666 param->fp_lmd->lmd_lmm.lmm_objects,
2668 param->fp_obd_index, param->fp_max_depth,
2669 param->fp_verbose, param->fp_raw);
2671 case LOV_USER_MAGIC_V3: {
2672 char pool_name[LOV_MAXPOOLNAME + 1];
2673 struct lov_user_ost_data_v1 *objects;
2674 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2676 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2677 objects = lmmv3->lmm_objects;
2678 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm,
2679 pool_name[0] == '\0' ? NULL : pool_name,
2680 objects, path, is_dir,
2681 param->fp_obd_index, param->fp_max_depth,
2682 param->fp_verbose, param->fp_raw);
2686 case LMV_USER_MAGIC: {
2687 char pool_name[LOV_MAXPOOLNAME + 1];
2688 struct lmv_user_md *lum;
2690 lum = (struct lmv_user_md *)param->fp_lmv_md;
2691 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2692 lmv_dump_user_lmm(lum,
2693 pool_name[0] == '\0' ? NULL : pool_name,
2694 path, param->fp_obd_index,
2695 param->fp_max_depth, param->fp_verbose);
2699 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2700 "(expecting one of %#x %#x %#x %#x)\n",
2701 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2702 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2703 LMV_USER_MAGIC, LMV_MAGIC_V1);
2708 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2714 fname = strrchr(path, '/');
2716 /* It should be a file (or other non-directory) */
2717 if (fname == NULL) {
2718 dname = (char *)malloc(2);
2722 fname = (char *)path;
2724 dname = (char *)malloc(fname - path + 1);
2727 strncpy(dname, path, fname - path);
2728 dname[fname - path] = '\0';
2732 fd = open(dname, O_RDONLY);
2739 strcpy((char *)lum, fname);
2740 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2743 if (close(fd) == -1 && rc == 0)
2750 int llapi_file_lookup(int dirfd, const char *name)
2752 struct obd_ioctl_data data = { 0 };
2757 if (dirfd < 0 || name == NULL)
2760 data.ioc_version = OBD_IOCTL_VERSION;
2761 data.ioc_len = sizeof(data);
2762 data.ioc_inlbuf1 = (char *)name;
2763 data.ioc_inllen1 = strlen(name) + 1;
2765 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2767 llapi_error(LLAPI_MSG_ERROR, rc,
2768 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2773 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2779 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2780 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2782 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2783 * The table below gives the answers for the specified parameters (value and
2784 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2785 * --------------------------------------
2786 * 1 | file > limit; sign > 0 | -1 / -1 |
2787 * 2 | file = limit; sign > 0 | -1 / -1 |
2788 * 3 | file < limit; sign > 0 | ? / 1 |
2789 * 4 | file > limit; sign = 0 | -1 / -1 |
2790 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2791 * 6 | file < limit; sign = 0 | ? / -1 |
2792 * 7 | file > limit; sign < 0 | 1 / 1 |
2793 * 8 | file = limit; sign < 0 | ? / -1 |
2794 * 9 | file < limit; sign < 0 | ? / -1 |
2795 * --------------------------------------
2796 * Note: 5th actually means that the value is within the interval
2797 * (limit - margin, limit]. */
2798 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2799 int sign, int negopt, unsigned long long margin,
2805 /* Drop the fraction of margin (of days). */
2806 if (file + margin <= limit)
2808 } else if (sign == 0) {
2809 if (file <= limit && file + margin > limit)
2811 else if (file + margin <= limit)
2813 } else if (sign < 0) {
2820 return negopt ? ~ret + 1 : ret;
2823 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2824 * Return -1 or 1 if file timestamp does not or does match the given criteria
2825 * correspondingly. Return 0 if the MDS time is being checked and there are
2826 * attributes on OSTs and it is not yet clear if the timespamp matches.
2828 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2829 * updated timestamps. */
2830 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2835 /* Check if file is accepted. */
2836 if (param->fp_atime) {
2837 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2838 param->fp_asign, param->fp_exclude_atime,
2845 if (param->fp_mtime) {
2846 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2847 param->fp_msign, param->fp_exclude_mtime,
2852 /* If the previous check matches, but this one is not yet clear,
2853 * we should return 0 to do an RPC on OSTs. */
2858 if (param->fp_ctime) {
2859 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2860 param->fp_csign, param->fp_exclude_ctime,
2865 /* If the previous check matches, but this one is not yet clear,
2866 * we should return 0 to do an RPC on OSTs. */
2875 * Check whether the stripes matches the indexes user provided
2879 static int check_obd_match(struct find_param *param)
2881 lstat_t *st = ¶m->fp_lmd->lmd_st;
2882 struct lov_user_ost_data_v1 *lmm_objects;
2885 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
2888 if (!S_ISREG(st->st_mode))
2891 /* Only those files should be accepted, which have a
2892 * stripe on the specified OST. */
2893 if (!param->fp_lmd->lmd_lmm.lmm_stripe_count)
2896 if (param->fp_lmd->lmd_lmm.lmm_magic ==
2897 LOV_USER_MAGIC_V3) {
2898 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2900 lmm_objects = lmmv3->lmm_objects;
2901 } else if (param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2902 lmm_objects = param->fp_lmd->lmd_lmm.lmm_objects;
2904 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2905 __func__, param->fp_lmd->lmd_lmm.lmm_magic);
2909 for (i = 0; i < param->fp_lmd->lmd_lmm.lmm_stripe_count; i++) {
2910 for (j = 0; j < param->fp_num_obds; j++) {
2911 if (param->fp_obd_indexes[j] ==
2912 lmm_objects[i].l_ost_idx) {
2913 if (param->fp_exclude_obd)
2920 if (param->fp_exclude_obd)
2926 static int check_mdt_match(struct find_param *param)
2930 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
2933 /* FIXME: For striped dir, we should get stripe information and check */
2934 for (i = 0; i < param->fp_num_mdts; i++) {
2935 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
2936 return !param->fp_exclude_mdt;
2939 if (param->fp_exclude_mdt)
2946 * Check whether the obd is active or not, if it is
2947 * not active, just print the object affected by this
2950 static int print_failed_tgt(struct find_param *param, char *path, int type)
2952 struct obd_statfs stat_buf;
2953 struct obd_uuid uuid_buf;
2956 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2958 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2959 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2960 ret = llapi_obd_statfs(path, type,
2961 param->fp_obd_index, &stat_buf,
2964 llapi_printf(LLAPI_MSG_NORMAL,
2965 "obd_uuid: %s failed %s ",
2966 param->fp_obd_uuid->uuid,
2973 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2974 void *data, struct dirent64 *de)
2976 struct find_param *param = (struct find_param *)data;
2977 DIR *dir = dirp == NULL ? NULL : *dirp;
2978 int decision = 1; /* 1 is accepted; -1 is rejected. */
2979 lstat_t *st = ¶m->fp_lmd->lmd_st;
2981 int checked_type = 0;
2984 LASSERT(parent != NULL || dir != NULL);
2986 param->fp_lmd->lmd_lmm.lmm_stripe_count = 0;
2988 /* If a regular expression is presented, make the initial decision */
2989 if (param->fp_pattern != NULL) {
2990 char *fname = strrchr(path, '/');
2991 fname = (fname == NULL ? path : fname + 1);
2992 ret = fnmatch(param->fp_pattern, fname, 0);
2993 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
2994 (ret == 0 && param->fp_exclude_pattern))
2998 /* See if we can check the file type from the dirent. */
2999 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
3002 if (DTTOIF(de->d_type) == param->fp_type) {
3003 if (param->fp_exclude_type)
3006 if (!param->fp_exclude_type)
3013 /* Request MDS for the stat info if some of these parameters need
3014 * to be compared. */
3015 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
3016 param->fp_check_uid || param->fp_check_gid ||
3017 param->fp_atime || param->fp_mtime || param->fp_ctime ||
3018 param->fp_check_pool || param->fp_check_size ||
3019 param->fp_check_stripe_count || param->fp_check_stripe_size ||
3020 param->fp_check_layout)
3023 if (param->fp_type != 0 && checked_type == 0)
3026 if (decision == 0) {
3027 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
3028 param->fp_lum_size);
3029 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
3030 (param->fp_check_pool || param->fp_check_stripe_count ||
3031 param->fp_check_stripe_size || param->fp_check_layout)) {
3032 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
3034 /* We need to "fake" the "use the default" values
3035 * since the lmm struct is zeroed out at this point. */
3036 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3037 lmm->lmm_pattern = 0xFFFFFFFF;
3039 ostid_set_seq(&lmm->lmm_oi,
3040 FID_SEQ_LOV_DEFAULT);
3041 lmm->lmm_stripe_size = 0;
3042 lmm->lmm_stripe_count = 0;
3043 lmm->lmm_stripe_offset = -1;
3045 if (ret == 0 && param->fp_mdt_uuid != NULL) {
3047 ret = llapi_file_fget_mdtidx(dirfd(dir),
3048 ¶m->fp_file_mdt_index);
3049 } else if (S_ISREG(st->st_mode)) {
3052 /* FIXME: we could get the MDT index from the
3053 * file's FID in lmd->lmd_lmm.lmm_oi without
3054 * opening the file, once we are sure that
3055 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
3056 * That would still be an ioctl() to map the
3057 * FID to the MDT, but not an open RPC. */
3058 fd = open(path, O_RDONLY);
3060 ret = llapi_file_fget_mdtidx(fd,
3061 ¶m->fp_file_mdt_index);
3067 /* For a special file, we assume it resides on
3068 * the same MDT as the parent directory. */
3069 ret = llapi_file_fget_mdtidx(dirfd(parent),
3070 ¶m->fp_file_mdt_index);
3083 if (param->fp_type && !checked_type) {
3084 if ((st->st_mode & S_IFMT) == param->fp_type) {
3085 if (param->fp_exclude_type)
3088 if (!param->fp_exclude_type)
3094 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
3095 if (lustre_fs && param->fp_got_uuids &&
3096 param->fp_dev != st->st_dev) {
3097 /* A lustre/lustre mount point is crossed. */
3098 param->fp_got_uuids = 0;
3099 param->fp_obds_printed = 0;
3100 param->fp_mdt_index = OBD_NOT_FOUND;
3101 param->fp_obd_index = OBD_NOT_FOUND;
3104 if (lustre_fs && !param->fp_got_uuids) {
3105 ret = setup_target_indexes(dir ? dir : parent, path,
3110 param->fp_dev = st->st_dev;
3111 } else if (!lustre_fs && param->fp_got_uuids) {
3112 /* A lustre/non-lustre mount point is crossed. */
3113 param->fp_got_uuids = 0;
3114 param->fp_mdt_index = OBD_NOT_FOUND;
3115 param->fp_obd_index = OBD_NOT_FOUND;
3119 if (param->fp_check_stripe_size) {
3120 decision = find_value_cmp(
3121 param->fp_lmd->lmd_lmm.lmm_stripe_size,
3122 param->fp_stripe_size,
3123 param->fp_stripe_size_sign,
3124 param->fp_exclude_stripe_size,
3125 param->fp_stripe_size_units, 0);
3130 if (param->fp_check_stripe_count) {
3131 decision = find_value_cmp(
3132 param->fp_lmd->lmd_lmm.lmm_stripe_count,
3133 param->fp_stripe_count,
3134 param->fp_stripe_count_sign,
3135 param->fp_exclude_stripe_count, 1, 0);
3140 if (param->fp_check_layout) {
3143 found = (param->fp_lmd->lmd_lmm.lmm_pattern & param->fp_layout);
3144 if ((param->fp_lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
3145 (found && param->fp_exclude_layout) ||
3146 (!found && !param->fp_exclude_layout)) {
3152 /* If an OBD UUID is specified but none matches, skip this file. */
3153 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
3154 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
3157 /* If an OST or MDT UUID is given, and some OST matches,
3159 if (param->fp_obd_index != OBD_NOT_FOUND ||
3160 param->fp_mdt_index != OBD_NOT_FOUND) {
3161 if (param->fp_obd_uuid) {
3162 if (check_obd_match(param)) {
3163 /* If no mdtuuid is given, we are done.
3164 * Otherwise, fall through to the mdtuuid
3166 if (!param->fp_mdt_uuid)
3173 if (param->fp_mdt_uuid) {
3174 if (check_mdt_match(param))
3181 if (param->fp_check_uid) {
3182 if (st->st_uid == param->fp_uid) {
3183 if (param->fp_exclude_uid)
3186 if (!param->fp_exclude_uid)
3191 if (param->fp_check_gid) {
3192 if (st->st_gid == param->fp_gid) {
3193 if (param->fp_exclude_gid)
3196 if (!param->fp_exclude_gid)
3201 if (param->fp_check_pool) {
3202 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3204 /* empty requested pool is taken as no pool search => V1 */
3205 if (((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
3206 (param->fp_poolname[0] == '\0')) ||
3207 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3208 (strncmp(lmmv3->lmm_pool_name,
3209 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
3210 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3211 (strcmp(param->fp_poolname, "*") == 0))) {
3212 if (param->fp_exclude_pool)
3215 if (!param->fp_exclude_pool)
3220 /* Check the time on mds. */
3222 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3225 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3226 param->fp_lmd->lmd_lmm.lmm_stripe_count)
3228 decision = find_time_check(st, param, for_mds);
3233 /* If file still fits the request, ask ost for updated info.
3234 The regular stat is almost of the same speed as some new
3235 'glimpse-size-ioctl'. */
3237 if (param->fp_check_size && S_ISREG(st->st_mode) &&
3238 param->fp_lmd->lmd_lmm.lmm_stripe_count)
3241 if (param->fp_check_size && S_ISDIR(st->st_mode))
3245 /* For regular files with the stripe the decision may have not
3246 * been taken yet if *time or size is to be checked. */
3247 if (param->fp_obd_index != OBD_NOT_FOUND)
3248 print_failed_tgt(param, path, LL_STATFS_LOV);
3250 if (param->fp_mdt_index != OBD_NOT_FOUND)
3251 print_failed_tgt(param, path, LL_STATFS_LMV);
3254 ret = fstat_f(dirfd(dir), st);
3255 else if (de != NULL)
3256 ret = fstatat_f(dirfd(parent), de->d_name, st,
3257 AT_SYMLINK_NOFOLLOW);
3259 ret = lstat_f(path, st);
3262 if (errno == ENOENT) {
3263 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3264 "warning: %s: %s does not exist",
3269 llapi_error(LLAPI_MSG_ERROR, ret,
3270 "%s: IOC_LOV_GETINFO on %s failed",
3276 /* Check the time on osc. */
3277 decision = find_time_check(st, param, 0);
3282 if (param->fp_check_size)
3283 decision = find_value_cmp(st->st_size, param->fp_size,
3284 param->fp_size_sign,
3285 param->fp_exclude_size,
3286 param->fp_size_units, 0);
3288 if (decision != -1) {
3289 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3290 if (param->fp_zero_end)
3291 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3293 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3297 /* Do not get down anymore? */
3298 if (param->fp_depth == param->fp_max_depth)
3306 static int cb_mv_init(char *path, DIR *parent, DIR **dirp,
3307 void *param_data, struct dirent64 *de)
3309 struct find_param *param = (struct find_param *)param_data;
3311 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3313 struct obd_ioctl_data data = { 0 };
3318 LASSERT(parent != NULL || dirp != NULL);
3322 if (parent == NULL) {
3323 dir = opendir_parent(path);
3327 llapi_error(LLAPI_MSG_ERROR, ret,
3328 "can not open %s", path);
3335 filename = basename(path);
3336 data.ioc_inlbuf1 = (char *)filename;
3337 data.ioc_inllen1 = strlen(filename) + 1;
3338 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3339 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3340 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3342 llapi_error(LLAPI_MSG_ERROR, ret,
3343 "llapi_obd_statfs: error packing ioctl data");
3347 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3350 fprintf(stderr, "%s migrate failed %d\n", path, ret);
3352 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3353 fprintf(stdout, "migrate %s to MDT%d\n",
3354 path, param->fp_mdt_index);
3359 /* If the directory is being migration, we need
3360 * close the directory after migration,
3361 * so the old directory cache will be cleanup
3362 * on the client side, and re-open to get the
3363 * new directory handle */
3364 *dirp = opendir(path);
3367 llapi_error(LLAPI_MSG_ERROR, ret,
3368 "%s: Failed to open '%s'", __func__, path);
3379 int llapi_mv(char *path, struct find_param *param)
3381 return param_callback(path, cb_mv_init, cb_common_fini, param);
3384 int llapi_find(char *path, struct find_param *param)
3386 return param_callback(path, cb_find_init, cb_common_fini, param);
3390 * Get MDT number that the file/directory inode referenced
3391 * by the open fd resides on.
3392 * Return 0 and mdtidx on success, or -ve errno.
3394 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3396 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3401 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3402 struct dirent64 *de)
3404 struct find_param *param = (struct find_param *)data;
3405 DIR *d = dirp == NULL ? NULL : *dirp;
3409 LASSERT(parent != NULL || d != NULL);
3412 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3413 } else /* if (parent) */ {
3416 fd = open(path, O_RDONLY | O_NOCTTY);
3418 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3426 if (ret == -ENODATA) {
3427 if (!param->fp_obd_uuid)
3428 llapi_printf(LLAPI_MSG_NORMAL,
3429 "'%s' has no stripe info\n", path);
3431 } else if (ret == -ENOENT) {
3432 llapi_error(LLAPI_MSG_WARN, ret,
3433 "warning: %s: '%s' does not exist",
3436 } else if (ret == -ENOTTY) {
3437 llapi_error(LLAPI_MSG_ERROR, ret,
3438 "%s: '%s' not on a Lustre fs",
3441 llapi_error(LLAPI_MSG_ERROR, ret,
3442 "error: %s: '%s' failed get_mdtidx",
3448 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3449 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3451 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3455 /* Do not go down anymore? */
3456 if (param->fp_depth == param->fp_max_depth)
3464 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3465 struct dirent64 *de)
3467 struct find_param *param = (struct find_param *)data;
3468 DIR *d = dirp == NULL ? NULL : *dirp;
3471 LASSERT(parent != NULL || d != NULL);
3473 if (param->fp_obd_uuid) {
3474 param->fp_quiet = 1;
3475 ret = setup_obd_uuid(d ? d : parent, path, param);
3481 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3482 ret = cb_get_dirstripe(path, d, param);
3484 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3485 (void *)¶m->fp_lmd->lmd_lmm);
3488 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3489 char *fname = strrchr(path, '/');
3490 fname = (fname == NULL ? path : fname + 1);
3492 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3493 param->fp_lum_size);
3495 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3496 (void *)¶m->fp_lmd->lmd_lmm);
3502 if (errno == ENODATA && d != NULL) {
3503 /* We need to "fake" the "use the default" values
3504 * since the lmm struct is zeroed out at this point.
3505 * The magic needs to be set in order to satisfy
3506 * a check later on in the code path.
3507 * The object_seq needs to be set for the "(Default)"
3508 * prefix to be displayed. */
3509 if (param->fp_get_default_lmv) {
3510 struct lmv_user_md *lum = param->fp_lmv_md;
3512 lum->lum_magic = LMV_USER_MAGIC;
3513 lum->lum_stripe_count = 0;
3514 lum->lum_stripe_offset = -1;
3516 } else if (param->fp_get_lmv) {
3517 struct lmv_user_md *lum = param->fp_lmv_md;
3520 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3523 lum->lum_magic = LMV_MAGIC_V1;
3524 lum->lum_stripe_count = 0;
3525 lum->lum_stripe_offset = mdtidx;
3528 struct lov_user_md *lmm =
3529 ¶m->fp_lmd->lmd_lmm;
3531 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3533 ostid_set_seq(&lmm->lmm_oi,
3534 FID_SEQ_LOV_DEFAULT);
3535 lmm->lmm_stripe_count = 0;
3536 lmm->lmm_stripe_size = 0;
3537 lmm->lmm_stripe_offset = -1;
3540 } else if (errno == ENODATA && parent != NULL) {
3541 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
3542 llapi_printf(LLAPI_MSG_NORMAL,
3543 "%s has no stripe info\n", path);
3545 } else if (errno == ENOENT) {
3546 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3547 "warning: %s: %s does not exist",
3550 } else if (errno == ENOTTY) {
3552 llapi_error(LLAPI_MSG_ERROR, ret,
3553 "%s: '%s' not on a Lustre fs?",
3558 llapi_error(LLAPI_MSG_ERROR, ret,
3559 "error: %s: %s failed for %s",
3560 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3561 "IOC_MDC_GETFILESTRIPE", path);
3568 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
3569 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3572 /* Do not get down anymore? */
3573 if (param->fp_depth == param->fp_max_depth)
3581 int llapi_getstripe(char *path, struct find_param *param)
3583 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
3584 cb_get_mdt_index : cb_getstripe,
3585 cb_common_fini, param);
3588 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3589 struct obd_statfs *stat_buf,
3590 struct obd_uuid *uuid_buf)
3593 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3595 struct obd_ioctl_data data = { 0 };
3598 data.ioc_inlbuf1 = (char *)&type;
3599 data.ioc_inllen1 = sizeof(__u32);
3600 data.ioc_inlbuf2 = (char *)&index;
3601 data.ioc_inllen2 = sizeof(__u32);
3602 data.ioc_pbuf1 = (char *)stat_buf;
3603 data.ioc_plen1 = sizeof(struct obd_statfs);
3604 data.ioc_pbuf2 = (char *)uuid_buf;
3605 data.ioc_plen2 = sizeof(struct obd_uuid);
3607 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3609 llapi_error(LLAPI_MSG_ERROR, rc,
3610 "llapi_obd_statfs: error packing ioctl data");
3614 fd = open(path, O_RDONLY);
3615 if (errno == EISDIR)
3616 fd = open(path, O_DIRECTORY | O_RDONLY);
3619 rc = errno ? -errno : -EBADF;
3620 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3622 /* If we can't even open a file on the filesystem (e.g. with
3623 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3626 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3628 rc = errno ? -errno : -EINVAL;
3634 #define MAX_STRING_SIZE 128
3636 int llapi_ping(char *obd_type, char *obd_name)
3638 char path[MAX_STRING_SIZE];
3642 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3643 obd_type, obd_name);
3645 fd = open(path, O_WRONLY);
3648 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3652 /* The purpose is to send a byte as a ping, whatever this byte is. */
3653 /* coverity[uninit_use_in_call] */
3654 rc = write(fd, buf, 1);
3664 int llapi_target_iterate(int type_num, char **obd_type,
3665 void *args, llapi_cb_t cb)
3667 char buf[MAX_STRING_SIZE];
3668 FILE *fp = fopen(DEVICES_LIST, "r");
3673 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3677 while (fgets(buf, sizeof(buf), fp) != NULL) {
3678 char *obd_type_name = NULL;
3679 char *obd_name = NULL;
3680 char *obd_uuid = NULL;
3682 struct obd_statfs osfs_buffer;
3684 while(bufp[0] == ' ')
3687 for(i = 0; i < 3; i++) {
3688 obd_type_name = strsep(&bufp, " ");
3690 obd_name = strsep(&bufp, " ");
3691 obd_uuid = strsep(&bufp, " ");
3693 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3695 for (i = 0; i < type_num; i++) {
3696 if (strcmp(obd_type_name, obd_type[i]) != 0)
3699 cb(obd_type_name, obd_name, obd_uuid, args);
3706 static void do_target_check(char *obd_type_name, char *obd_name,
3707 char *obd_uuid, void *args)
3711 rc = llapi_ping(obd_type_name, obd_name);
3712 if (rc == ENOTCONN) {
3713 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3715 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3717 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3721 int llapi_target_check(int type_num, char **obd_type, char *dir)
3723 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3726 #undef MAX_STRING_SIZE
3728 /* Is this a lustre fs? */
3729 int llapi_is_lustre_mnttype(const char *type)
3731 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3734 /* Is this a lustre client fs? */
3735 int llapi_is_lustre_mnt(struct mntent *mnt)
3737 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3738 strstr(mnt->mnt_fsname, ":/") != NULL);
3741 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3746 root = opendir(mnt);
3749 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3753 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3761 static int cb_quotachown(char *path, DIR *parent, DIR **dirp, void *data,
3762 struct dirent64 *de)
3764 struct find_param *param = (struct find_param *)data;
3765 DIR *d = dirp == NULL ? NULL : *dirp;
3769 LASSERT(parent != NULL || d != NULL);
3771 rc = get_lmd_info(path, parent, d, param->fp_lmd, param->fp_lum_size);
3773 if (rc == -ENODATA) {
3774 if (!param->fp_obd_uuid && !param->fp_quiet)
3775 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3776 "%s has no stripe info", path);
3778 } else if (rc == -ENOENT) {
3784 st = ¶m->fp_lmd->lmd_st;
3786 /* libc chown() will do extra check, and if the real owner is
3787 * the same as the ones to set, it won't fall into kernel, so
3788 * invoke syscall directly. */
3789 rc = syscall(SYS_chown, path, -1, -1);
3791 llapi_error(LLAPI_MSG_ERROR, errno,
3792 "error: chown %s", path);
3794 rc = chmod(path, st->st_mode);
3797 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3804 int llapi_quotachown(char *path, int flag)
3806 struct find_param param;
3808 memset(¶m, 0, sizeof(param));
3809 param.fp_recursive = 1;
3810 param.fp_verbose = 0;
3813 return param_callback(path, cb_quotachown, NULL, ¶m);
3819 #include <sys/wait.h>
3823 static int rmtacl_notify(int ops)
3827 int found = 0, fd = 0, rc = 0;
3829 fp = setmntent(MOUNTED, "r");
3832 llapi_error(LLAPI_MSG_ERROR, rc,
3833 "error setmntent(%s)", MOUNTED);
3838 mnt = getmntent(fp);
3842 if (!llapi_is_lustre_mnt(mnt))
3845 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3848 llapi_error(LLAPI_MSG_ERROR, rc,
3849 "Can't open '%s'", mnt->mnt_dir);
3853 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3857 llapi_error(LLAPI_MSG_ERROR, rc,
3858 "ioctl RMTACL on '%s' err %d",
3868 return ((rc != 0) ? rc : found);
3871 static char *next_token(char *p, int div)
3877 while (*p && *p != ':' && !isspace(*p))
3880 while (*p == ':' || isspace(*p))
3883 return *p ? p : NULL;
3886 static int rmtacl_name2id(char *name, int is_user)
3891 pw = getpwnam(name);
3895 return (int)(pw->pw_uid);
3899 gr = getgrnam(name);
3903 return (int)(gr->gr_gid);
3907 static int isodigit(int c)
3909 return (c >= '0' && c <= '7') ? 1 : 0;
3913 * Whether the name is just digits string (uid/gid) already or not.
3918 static int str_is_id(char *str)
3925 if (*str == 'x' || *str == 'X') { /* for Hex. */
3926 if (!isxdigit(*(++str)))
3929 while (isxdigit(*(++str)));
3930 } else if (isodigit(*str)) { /* for Oct. */
3931 while (isodigit(*(++str)));
3933 } else if (isdigit(*str)) { /* for Dec. */
3934 while (isdigit(*(++str)));
3937 return (*str == 0) ? 1 : 0;
3947 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3949 static rmtacl_name_t rmtacl_namelist[] = {
3950 { RMTACL_OPTNAME("user:"), 1, 0 },
3951 { RMTACL_OPTNAME("group:"), 0, 0 },
3952 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3953 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3954 /* for --tabular option */
3955 { RMTACL_OPTNAME("user"), 1, 1 },
3956 { RMTACL_OPTNAME("group"), 0, 1 },
3960 static int rgetfacl_output(char *str)
3962 char *start = NULL, *end = NULL;
3963 int is_user = 0, n, id;
3970 for (rn = rmtacl_namelist; rn->name; rn++) {
3971 if(strncmp(str, rn->name, rn->length) == 0) {
3972 if (!rn->next_token)
3973 start = str + rn->length;
3975 start = next_token(str + rn->length, 0);
3976 is_user = rn->is_user;
3981 end = next_token(start, 1);
3982 if (end == NULL || start == end) {
3983 n = printf("%s", str);
3989 id = rmtacl_name2id(start, is_user);
3990 if (id == INVALID_ID) {
3991 if (str_is_id(start)) {
3993 n = printf("%s", str);
3996 } else if ((id == NOBODY_UID && is_user) ||
3997 (id == NOBODY_GID && !is_user)) {
3999 n = printf("%s", str);
4003 n = printf("%s%d%s", str, id, end);
4008 static int child_status(int status)
4010 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
4013 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
4016 int fd[2], status, rc;
4023 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe");
4030 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork");
4035 /* child process redirects its output. */
4038 if (dup2(fd[1], 1) < 0) {
4040 llapi_error(LLAPI_MSG_ERROR, rc,
4041 "Can't dup2 %d", fd[1]);
4051 status = rmtacl_notify(ops);
4055 exit(execvp(argv[0], argv));
4058 /* the following is parent process */
4059 fp = fdopen(fd[0], "r");
4062 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed", fd[0]);
4068 while (fgets(buf, PIPE_BUF, fp) != NULL) {
4069 if (output_func(buf) < 0)
4070 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
4076 if (waitpid(pid, &status, 0) < 0) {
4078 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed", pid);
4082 return child_status(status);
4085 int llapi_lsetfacl(int argc, char *argv[])
4087 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
4090 int llapi_lgetfacl(int argc, char *argv[])
4092 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
4095 int llapi_rsetfacl(int argc, char *argv[])
4097 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
4100 int llapi_rgetfacl(int argc, char *argv[])
4102 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
4105 int llapi_cp(int argc, char *argv[])
4109 rc = rmtacl_notify(RMT_RSETFACL);
4113 exit(execvp(argv[0], argv));
4116 int llapi_ls(int argc, char *argv[])
4120 rc = rmtacl_notify(RMT_LGETFACL);
4124 exit(execvp(argv[0], argv));
4127 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4128 * format must have %s%s, buf must be > 16
4129 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4130 * then buf = "lustre-MDT0000"
4132 static int get_mdtname(char *name, char *format, char *buf)
4134 char suffix[]="-MDT0000";
4135 int len = strlen(name);
4137 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4138 name[len - 5] = '\0';
4143 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4146 /* Not enough room to add suffix */
4147 llapi_err_noerrno(LLAPI_MSG_ERROR,
4148 "MDT name too long |%s|", name);
4153 return sprintf(buf, format, name, suffix);
4156 /** ioctl on filsystem root, with mdtindex sent as data
4157 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4158 * \param mdtidxp pointer to integer within data to be filled in with the
4159 * mdt index (0 if no mdt is specified). NULL won't be filled.
4161 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4169 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4170 Open root and parse mdt index. */
4171 if (mdtname[0] == '/') {
4173 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4174 (char *)mdtname, -1);
4176 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4178 ptr = fsname + strlen(fsname) - 8;
4180 index = strtol(ptr + 4, NULL, 10);
4181 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4185 llapi_err_noerrno(LLAPI_MSG_ERROR,
4186 "Can't open %s: %d\n", mdtname, rc);
4193 rc = ioctl(fd, opc, data);
4198 if (rc && want_error)
4199 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
4205 /****** Changelog API ********/
4207 static int changelog_ioctl(const char *mdtname, int opc, int id,
4208 long long recno, int flags)
4210 struct ioc_changelog data;
4214 data.icc_recno = recno;
4215 data.icc_flags = flags;
4216 idx = (int *)(&data.icc_mdtindex);
4218 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
4221 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
4222 struct changelog_private {
4224 enum changelog_send_flag flags;
4225 struct lustre_kernelcomm kuc;
4228 /** Start reading from a changelog
4229 * @param priv Opaque private control structure
4230 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
4231 * @param device Report changes recorded on this MDT
4232 * @param startrec Report changes beginning with this record number
4233 * (just call llapi_changelog_fini when done; don't need an endrec)
4235 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
4236 const char *device, long long startrec)
4238 struct changelog_private *cp;
4242 /* Set up the receiver control struct */
4243 cp = calloc(1, sizeof(*cp));
4247 cp->magic = CHANGELOG_PRIV_MAGIC;
4250 /* Set up the receiver */
4251 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */, 0);
4257 /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
4258 * warning if it's missing. */
4259 if (!(flags & CHANGELOG_FLAG_JOBID) && !warned) {
4260 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
4261 "w/o CHANGELOG_FLAG_JOBID", __func__);
4265 /* Tell the kernel to start sending */
4266 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
4268 /* Only the kernel reference keeps the write side open */
4269 close(cp->kuc.lk_wfd);
4270 cp->kuc.lk_wfd = LK_NOFD;
4272 /* frees and clears priv */
4273 llapi_changelog_fini(priv);
4284 /** Finish reading from a changelog */
4285 int llapi_changelog_fini(void **priv)
4287 struct changelog_private *cp = (struct changelog_private *)*priv;
4289 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4292 libcfs_ukuc_stop(&cp->kuc);
4299 * Convert all records to a same format according to the caller's wishes.
4300 * Default is CLF_VERSION | CLF_RENAME.
4301 * Add CLF_JOBID if explicitely requested.
4303 * \param rec The record to remap. It is expected to be big enough to
4304 * properly handle the final format.
4305 * \return 1 if anything changed. 0 otherwise.
4307 /** Read the next changelog entry
4308 * @param priv Opaque private control structure
4309 * @param rech Changelog record handle; record will be allocated here
4310 * @return 0 valid message received; rec is set
4314 #define DEFAULT_RECORD_FMT (CLF_VERSION | CLF_RENAME)
4315 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
4317 struct changelog_private *cp = (struct changelog_private *)priv;
4318 struct kuc_hdr *kuch;
4319 enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
4322 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4326 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4330 if (cp->flags & CHANGELOG_FLAG_JOBID)
4331 rec_fmt |= CLF_JOBID;
4334 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4335 KUC_CHANGELOG_MSG_MAXSIZE,
4336 KUC_TRANSPORT_CHANGELOG);
4340 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4341 ((kuch->kuc_msgtype != CL_RECORD) &&
4342 (kuch->kuc_msgtype != CL_EOF))) {
4343 llapi_err_noerrno(LLAPI_MSG_ERROR,
4344 "Unknown changelog message type %d:%d\n",
4345 kuch->kuc_transport, kuch->kuc_msgtype);
4350 if (kuch->kuc_msgtype == CL_EOF) {
4351 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4360 /* Our message is a changelog_rec. Use pointer math to skip
4361 * kuch_hdr and point directly to the message payload. */
4362 *rech = (struct changelog_rec *)(kuch + 1);
4363 changelog_remap_rec(*rech, rec_fmt);
4373 /** Release the changelog record when done with it. */
4374 int llapi_changelog_free(struct changelog_rec **rech)
4377 /* We allocated memory starting at the kuc_hdr, but passed
4378 * the consumer a pointer to the payload.
4379 * Use pointer math to get back to the header.
4381 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4388 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4394 llapi_err_noerrno(LLAPI_MSG_ERROR,
4395 "can't purge negative records\n");
4399 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4400 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4401 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4402 llapi_err_noerrno(LLAPI_MSG_ERROR,
4403 "expecting id of the form '"
4404 CHANGELOG_USER_PREFIX
4405 "<num>'; got '%s'\n", idstr);
4409 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4412 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4413 int buflen, long long *recno, int *linkno)
4416 struct getinfo_fid2path *gf;
4419 while (*fidstr == '[')
4422 sscanf(fidstr, SFID, RFID(&fid));
4423 if (!fid_is_sane(&fid)) {
4424 llapi_err_noerrno(LLAPI_MSG_ERROR,
4425 "bad FID format [%s], should be "DFID"\n",
4426 fidstr, (__u64)1, 2, 0);
4430 gf = malloc(sizeof(*gf) + buflen);
4434 gf->gf_recno = *recno;
4435 gf->gf_linkno = *linkno;
4436 gf->gf_pathlen = buflen;
4438 /* Take path or fsname */
4439 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4442 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4444 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4445 if (buf[0] == '\0') { /* ROOT path */
4449 *recno = gf->gf_recno;
4450 *linkno = gf->gf_linkno;
4457 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4460 struct lustre_mdt_attrs *lma;
4464 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4466 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4469 lma = (struct lustre_mdt_attrs *)buf;
4470 fid_le_to_cpu(fid, &lma->lma_self_fid);
4474 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4479 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4488 int llapi_fd2fid(const int fd, lustre_fid *fid)
4492 memset(fid, 0, sizeof(*fid));
4494 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4495 if (rc == -EINVAL || rc == -ENOTTY)
4496 rc = fid_from_lma(NULL, fd, fid);
4501 int llapi_path2fid(const char *path, lustre_fid *fid)
4505 memset(fid, 0, sizeof(*fid));
4506 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4508 if (errno == ELOOP || errno == ENXIO)
4509 return fid_from_lma(path, -1, fid);
4513 rc = llapi_fd2fid(fd, fid);
4514 if (rc == -EINVAL || rc == -ENOTTY)
4515 rc = fid_from_lma(path, -1, fid);
4521 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
4522 char *name, size_t name_size)
4524 struct getparent *gp;
4527 gp = malloc(sizeof(*gp) + name_size);
4531 gp->gp_linkno = linkno;
4532 gp->gp_name_size = name_size;
4534 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
4540 *parent_fid = gp->gp_fid;
4542 strncpy(name, gp->gp_name, name_size);
4543 name[name_size - 1] = '\0';
4550 int llapi_path2parent(const char *path, unsigned int linkno,
4551 lustre_fid *parent_fid, char *name, size_t name_size)
4556 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4560 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4565 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4570 root = opendir(mnt);
4573 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4577 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4580 llapi_error(LLAPI_MSG_ERROR, rc,
4581 "ioctl on %s for getting connect flags failed", mnt);
4587 int llapi_get_version(char *buffer, int buffer_size,
4592 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4594 fd = open(OBD_DEV_PATH, O_RDONLY);
4598 memset(buffer, 0, buffer_size);
4599 data->ioc_version = OBD_IOCTL_VERSION;
4600 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4601 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4602 data->ioc_len = obd_ioctl_packlen(data);
4604 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4611 *version = data->ioc_bulk;
4616 * Get a 64-bit value representing the version of file data pointed by fd.
4618 * Each write or truncate, flushed on OST, will change this value. You can use
4619 * this value to verify if file data was modified. This only checks the file
4620 * data, not metadata.
4622 * \param flags 0: no flush pages, usually used it the process has already
4624 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4626 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4627 * pages from clients.
4629 * \retval 0 on success.
4630 * \retval -errno on error.
4632 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4635 struct ioc_data_version idv;
4637 idv.idv_flags = flags;
4639 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4643 *data_version = idv.idv_version;
4649 * Create a file without any name open it for read/write
4651 * - file is created as if it were a standard file in the given \a directory
4652 * - file does not appear in \a directory and mtime does not change because
4653 * the filename is handled specially by the Lustre MDS.
4654 * - file is removed at final close
4655 * - file modes are rw------- since it doesn't make sense to have a read-only
4656 * or write-only file that cannot be opened again.
4657 * - if user wants another mode it must use fchmod() on the open file, no
4658 * security problems arise because it cannot be opened by another process.
4660 * \param[in] directory directory from which to inherit layout/MDT idx
4661 * \param[in] idx MDT index on which the file is created,
4662 * \a idx == -1 means no specific MDT is requested
4663 * \param[in] open_flags standard open(2) flags
4665 * \retval 0 on success.
4666 * \retval -errno on error.
4668 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4670 char file_path[PATH_MAX];
4671 char filename[PATH_MAX];
4678 snprintf(filename, sizeof(filename),
4679 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4681 snprintf(filename, sizeof(filename),
4682 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4684 rc = snprintf(file_path, sizeof(file_path),
4685 "%s/%s", directory, filename);
4686 if (rc >= sizeof(file_path))
4689 fd = open(file_path, O_RDWR | O_CREAT | open_flags, S_IRUSR | S_IWUSR);
4691 llapi_error(LLAPI_MSG_ERROR, errno,
4692 "Cannot create volatile file '%s' in '%s'",
4693 filename + LUSTRE_VOLATILE_HDR_LEN,
4697 /* unlink file in case this wasn't a Lustre filesystem, and the
4698 * magic volatile filename wasn't handled as intended. The effect
4706 * Swap the layouts between 2 file descriptors
4707 * the 2 files must be open for writing
4708 * first fd received the ioctl, second fd is passed as arg
4709 * this is assymetric but avoid use of root path for ioctl
4711 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4712 int gid, __u64 flags)
4714 struct lustre_swap_layouts lsl;
4719 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4720 rc = fstat(fd1, &st1);
4724 rc = fstat(fd2, &st2);
4729 lsl.sl_flags = flags;
4733 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4737 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4738 struct timeval tv1[2];
4739 struct timeval tv2[2];
4741 memset(tv1, 0, sizeof(tv1));
4742 memset(tv2, 0, sizeof(tv2));
4744 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4745 tv1[0].tv_sec = st1.st_atime;
4746 tv2[0].tv_sec = st2.st_atime;
4748 tv1[0].tv_sec = st2.st_atime;
4749 tv2[0].tv_sec = st1.st_atime;
4752 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4753 tv1[1].tv_sec = st1.st_mtime;
4754 tv2[1].tv_sec = st2.st_mtime;
4756 tv1[1].tv_sec = st2.st_mtime;
4757 tv2[1].tv_sec = st1.st_mtime;
4760 rc = futimes(fd1, tv1);
4764 rc = futimes(fd2, tv2);
4772 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4779 while (grp_id == 0);
4781 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4789 * Swap the layouts between 2 files
4790 * the 2 files are open in write
4792 int llapi_swap_layouts(const char *path1, const char *path2,
4793 __u64 dv1, __u64 dv2, __u64 flags)
4797 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4800 llapi_error(LLAPI_MSG_ERROR, rc,
4801 "error: cannot open '%s' for write", path1);
4805 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4808 llapi_error(LLAPI_MSG_ERROR, rc,
4809 "error: cannot open '%s' for write", path2);
4813 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4815 llapi_error(LLAPI_MSG_ERROR, rc,
4816 "error: cannot swap layout between '%s' and '%s'",
4827 * Attempt to open a file with Lustre file identifier \a fid
4828 * and return an open file descriptor.
4830 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4831 * \param[in] fid Lustre file identifier of file to open
4832 * \param[in] flags open() flags
4834 * \retval non-negative file descriptor on successful open
4835 * \retval -1 if an error occurred
4837 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4839 char mntdir[PATH_MAX];
4840 char path[PATH_MAX];
4843 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4847 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4848 return open(path, flags);
4854 * \param fd File to lock.
4855 * \param gid Group Identifier.
4857 * \retval 0 on success.
4858 * \retval -errno on failure.
4860 int llapi_group_lock(int fd, int gid)
4864 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4867 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4875 * \param fd File to unlock.
4876 * \param gid Group Identifier.
4878 * \retval 0 on success.
4879 * \retval -errno on failure.
4881 int llapi_group_unlock(int fd, int gid)
4885 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4888 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");