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 <lnet/lnetctl.h>
76 #include <lustre/lustreapi.h>
77 #include <lustre_ioctl.h>
78 #include "lustreapi_internal.h"
80 static int llapi_msg_level = LLAPI_MSG_MAX;
82 void llapi_msg_set_level(int level)
84 /* ensure level is in the good range */
85 if (level < LLAPI_MSG_OFF)
86 llapi_msg_level = LLAPI_MSG_OFF;
87 else if (level > LLAPI_MSG_MAX)
88 llapi_msg_level = LLAPI_MSG_MAX;
90 llapi_msg_level = level;
93 int llapi_msg_get_level(void)
95 return llapi_msg_level;
98 static void error_callback_default(enum llapi_message_level level, int err,
99 const char *fmt, va_list ap)
101 vfprintf(stderr, fmt, ap);
102 if (level & LLAPI_MSG_NO_ERRNO)
103 fprintf(stderr, "\n");
105 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
108 static void info_callback_default(enum llapi_message_level level, int err,
109 const char *fmt, va_list ap)
111 vfprintf(stdout, fmt, ap);
114 static llapi_log_callback_t llapi_error_callback = error_callback_default;
115 static llapi_log_callback_t llapi_info_callback = info_callback_default;
118 /* llapi_error will preserve errno */
119 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
122 int tmp_errno = errno;
124 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
128 llapi_error_callback(level, abs(err), fmt, args);
133 /* llapi_printf will preserve errno */
134 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
137 int tmp_errno = errno;
139 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
143 llapi_info_callback(level, 0, fmt, args);
149 * Set a custom error logging function. Passing in NULL will reset the logging
150 * callback to its default value.
152 * This function returns the value of the old callback.
154 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
156 llapi_log_callback_t old = llapi_error_callback;
159 llapi_error_callback = cb;
161 llapi_error_callback = error_callback_default;
167 * Set a custom info logging function. Passing in NULL will reset the logging
168 * callback to its default value.
170 * This function returns the value of the old callback.
172 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
174 llapi_log_callback_t old = llapi_info_callback;
177 llapi_info_callback = cb;
179 llapi_info_callback = info_callback_default;
185 * size_units is to be initialized (or zeroed) by caller.
187 int llapi_parse_size(const char *optarg, unsigned long long *size,
188 unsigned long long *size_units, int bytes_spec)
192 if (strncmp(optarg, "-", 1) == 0)
195 if (*size_units == 0)
198 *size = strtoull(optarg, &end, 0);
201 if ((*end == 'b') && *(end + 1) == '\0' &&
202 (*size & (~0ULL << (64 - 9))) == 0 &&
204 *size_units = 1 << 9;
205 } else if ((*end == 'b') &&
206 *(end + 1) == '\0' &&
209 } else if ((*end == 'k' || *end == 'K') &&
210 *(end + 1) == '\0' &&
211 (*size & (~0ULL << (64 - 10))) == 0) {
212 *size_units = 1 << 10;
213 } else if ((*end == 'm' || *end == 'M') &&
214 *(end + 1) == '\0' &&
215 (*size & (~0ULL << (64 - 20))) == 0) {
216 *size_units = 1 << 20;
217 } else if ((*end == 'g' || *end == 'G') &&
218 *(end + 1) == '\0' &&
219 (*size & (~0ULL << (64 - 30))) == 0) {
220 *size_units = 1 << 30;
221 } else if ((*end == 't' || *end == 'T') &&
222 *(end + 1) == '\0' &&
223 (*size & (~0ULL << (64 - 40))) == 0) {
224 *size_units = 1ULL << 40;
225 } else if ((*end == 'p' || *end == 'P') &&
226 *(end + 1) == '\0' &&
227 (*size & (~0ULL << (64 - 50))) == 0) {
228 *size_units = 1ULL << 50;
229 } else if ((*end == 'e' || *end == 'E') &&
230 *(end + 1) == '\0' &&
231 (*size & (~0ULL << (64 - 60))) == 0) {
232 *size_units = 1ULL << 60;
237 *size *= *size_units;
241 /* XXX: llapi_xxx() functions return negative values upon failure */
243 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
244 int stripe_count, int stripe_pattern)
248 /* 64 KB is the largest common page size I'm aware of (on ia64), but
249 * check the local page size just in case. */
250 page_size = LOV_MIN_STRIPE_SIZE;
251 if (getpagesize() > page_size) {
252 page_size = getpagesize();
253 llapi_err_noerrno(LLAPI_MSG_WARN,
254 "warning: your page size (%u) is "
255 "larger than expected (%u)", page_size,
256 LOV_MIN_STRIPE_SIZE);
258 if (!llapi_stripe_size_is_aligned(stripe_size)) {
260 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
261 "must be an even multiple of %d bytes",
262 stripe_size, page_size);
265 if (!llapi_stripe_offset_is_valid(stripe_offset)) {
267 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
271 if (!llapi_stripe_count_is_valid(stripe_count)) {
273 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
277 if (llapi_stripe_size_is_too_big(stripe_size)) {
279 llapi_error(LLAPI_MSG_ERROR, rc,
280 "warning: stripe size 4G or larger "
281 "is not currently supported and would wrap");
287 /* return the first file matching this pattern */
288 static int first_match(char *pattern, char *buffer)
292 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
295 if (glob_info.gl_pathc < 1) {
296 globfree(&glob_info);
300 strcpy(buffer, glob_info.gl_pathv[0]);
302 globfree(&glob_info);
306 static int find_target_obdpath(char *fsname, char *path)
309 char pattern[PATH_MAX + 1];
312 snprintf(pattern, PATH_MAX,
313 "/proc/fs/lustre/lov/%s-*/target_obd",
315 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
316 if (rc == GLOB_NOMATCH)
321 strcpy(path, glob_info.gl_pathv[0]);
322 globfree(&glob_info);
326 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
329 char pattern[PATH_MAX + 1];
332 snprintf(pattern, PATH_MAX,
333 "/proc/fs/lustre/lov/%s-*/pools/%s",
335 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
336 /* If no pools, make sure the lov is available */
337 if ((rc == GLOB_NOMATCH) &&
338 (find_target_obdpath(fsname, poolpath) == -ENODEV))
343 strcpy(poolpath, glob_info.gl_pathv[0]);
344 globfree(&glob_info);
349 * Trim a trailing newline from a string, if it exists.
351 int llapi_chomp_string(char *buf)
367 * return a parameter string for a specific device type or mountpoint
369 * \param param_path the path to the file containing parameter data
370 * \param result buffer for parameter value string
371 * \param result_size size of buffer for return value
373 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
374 * complete the absolute path to the file containing the parameter data
375 * the user is requesting. If that file exist then the data is read from
376 * the file and placed into the \param result buffer that is passed by
377 * the user. Data is only copied up to the \param result_size to prevent
378 * overflow of the array.
380 * Return 0 for success, with a NUL-terminated string in \param result.
381 * Return -ve value for error.
383 int get_param(const char *param_path, char *result,
384 unsigned int result_size)
386 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
390 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
392 rc = first_match(pattern, file);
393 if (rc != 0 || result == NULL)
396 fp = fopen(file, "r");
398 while (fgets(buf, result_size, fp) != NULL)
407 #define DEVICES_LIST "/proc/fs/lustre/devices"
410 * return a parameter string for a specific device type or mountpoint
412 * \param fsname Lustre filesystem name (optional)
413 * \param file_path path to file in filesystem (optional, if fsname unset)
414 * \param obd_type Lustre OBD device type
415 * \param param_name parameter name to fetch
416 * \param value return buffer for parameter value string
417 * \param val_len size of buffer for return value
419 * If fsname is specified then the parameter will be from that filesystem
420 * (if it exists). If file_path is given and it is in a mounted Lustre
421 * filesystem, then the parameter will be otherwise the value may be
422 * from any mounted filesystem (if there is more than one).
424 * If "obd_type" matches a Lustre device then the first matching device
425 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
426 * will be used to provide the return value, otherwise the first such
427 * device found will be used.
429 * Return 0 for success, with a NUL-terminated string in \param buffer.
430 * Return -ve value for error.
432 static int get_param_obdvar(const char *fsname, const char *file_path,
433 const char *obd_type, const char *param_name,
434 char *value, unsigned int val_len)
436 char devices[PATH_MAX];
437 char dev[PATH_MAX] = "*";
442 fp = fopen(DEVICES_LIST, "r");
445 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
449 if (fsname == NULL && file_path != NULL) {
450 rc = llapi_search_fsname(file_path, fs);
452 llapi_error(LLAPI_MSG_ERROR, rc,
453 "'%s' is not on a Lustre filesystem",
457 } else if (fsname != NULL) {
458 rc = strlcpy(fs, fsname, sizeof(fs));
459 if (rc >= sizeof(fs)) {
465 while (fgets(devices, sizeof(devices) - 1, fp) != NULL) {
466 char *bufp = devices, *tmp;
468 devices[sizeof(devices) - 1] = '\0';
469 while (bufp[0] == ' ')
472 tmp = strstr(bufp, obd_type);
474 tmp += strlen(obd_type) + 1;
477 rc = strlcpy(dev, tmp, sizeof(dev));
478 if (rc >= sizeof(dev)) {
483 tmp = strchr(dev, ' ');
490 if (dev[0] == '*' && strlen(fs)) {
491 rc = snprintf(dev, sizeof(dev), "%s-*", fs);
492 if (rc >= sizeof(dev)) {
497 rc = snprintf(devices, sizeof(devices), "%s/%s/%s", obd_type, dev,
499 if (rc >= sizeof(devices)) {
505 return get_param(devices, value, val_len);
513 * TYPE one of llite, lmv, lov.
514 * /proc/fs/lustre/TYPE/INST the directory of interest.
516 static int get_param_cli(const char *type, const char *inst,
517 const char *param, char *buf, size_t buf_size)
519 char param_path[PATH_MAX + 1];
520 FILE *param_file = NULL;
523 snprintf(param_path, sizeof(param_path),
524 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
526 param_file = fopen(param_path, "r");
527 if (param_file == NULL) {
532 if (fgets(buf, buf_size, param_file) == NULL) {
539 if (param_file != NULL)
545 static int get_param_llite(const char *path,
546 const char *param, char *buf, size_t buf_size)
551 rc = llapi_getname(path, inst, sizeof(inst));
555 return get_param_cli("llite", inst, param, buf, buf_size);
558 static int get_param_lov(const char *path,
559 const char *param, char *buf, size_t buf_size)
561 struct obd_uuid uuid;
564 rc = llapi_file_get_lov_uuid(path, &uuid);
568 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
571 static int get_param_lmv(const char *path,
572 const char *param, char *buf, size_t buf_size)
574 struct obd_uuid uuid;
577 rc = llapi_file_get_lmv_uuid(path, &uuid);
581 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
584 static int get_mds_md_size(const char *path)
586 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
590 /* Get the max ea size from llite proc. */
591 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
603 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
605 return get_param_lmv(path, "uuid", buf, bufsize);
609 * if pool is NULL, search ostname in target_obd
610 * if pool is not NULL:
611 * if pool not found returns errno < 0
612 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
613 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
615 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
618 char buffer[PATH_MAX + 1];
622 len = strlen(ostname);
624 if (poolname == NULL) {
628 rc = find_target_obdpath(fsname, buffer);
630 rc = find_poolpath(fsname, poolname, buffer);
635 fd = fopen(buffer, "r");
639 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
640 if (poolname == NULL) {
642 /* Search for an ostname in the list of OSTs
643 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
644 ptr = strchr(buffer, ' ');
646 (strncmp(ptr + 1, ostname, len) == 0)) {
651 /* Search for an ostname in a pool,
652 (or an existing non-empty pool if no ostname) */
653 if ((ostname == NULL) ||
654 (strncmp(buffer, ostname, len) == 0)) {
665 * Open a Lustre file.
667 * \param name the name of the file to be opened
668 * \param flags access mode, see flags in open(2)
669 * \param mode permission of the file if it is created, see mode in open(2)
670 * \param param stripe pattern of the newly created file
672 * \retval file descriptor of opened file
673 * \retval negative errno on failure
675 int llapi_file_open_param(const char *name, int flags, mode_t mode,
676 const struct llapi_stripe_param *param)
678 char fsname[MAX_OBD_NAME + 1] = { 0 };
679 char *pool_name = param->lsp_pool;
680 struct lov_user_md *lum = NULL;
681 size_t lum_size = sizeof(*lum);
684 /* Make sure we are on a Lustre file system */
685 rc = llapi_search_fsname(name, fsname);
687 llapi_error(LLAPI_MSG_ERROR, rc,
688 "'%s' is not on a Lustre filesystem",
693 /* Check if the stripe pattern is sane. */
694 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
695 param->lsp_stripe_offset,
696 param->lsp_stripe_count,
697 param->lsp_stripe_pattern);
701 /* Make sure we have a good pool */
702 if (pool_name != NULL) {
703 /* in case user gives the full pool name <fsname>.<poolname>,
704 * strip the fsname */
705 char *ptr = strchr(pool_name, '.');
708 if (strcmp(pool_name, fsname) != 0) {
710 llapi_err_noerrno(LLAPI_MSG_ERROR,
711 "Pool '%s' is not on filesystem '%s'",
718 /* Make sure the pool exists and is non-empty */
719 rc = llapi_search_ost(fsname, pool_name, NULL);
721 char *err = rc == 0 ? "has no OSTs" : "does not exist";
723 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
724 fsname, pool_name, err);
728 lum_size = sizeof(struct lov_user_md_v3);
731 /* sanity check of target list */
732 if (param->lsp_is_specific) {
733 char ostname[MAX_OBD_NAME + 1];
737 for (i = 0; i < param->lsp_stripe_count; i++) {
738 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
739 fsname, param->lsp_osts[i]);
740 rc = llapi_search_ost(fsname, pool_name, ostname);
745 llapi_error(LLAPI_MSG_ERROR, rc,
746 "%s: cannot find OST %s in %s",
753 /* Make sure stripe offset is in OST list. */
754 if (param->lsp_osts[i] == param->lsp_stripe_offset)
758 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
759 "%s: stripe offset '%d' is not in the "
761 __func__, param->lsp_stripe_offset);
765 lum_size = lov_user_md_size(param->lsp_stripe_count,
766 LOV_USER_MAGIC_SPECIFIC);
769 lum = calloc(1, lum_size);
774 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
776 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
777 flags = O_DIRECTORY | O_RDONLY;
784 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
789 /* Initialize IOCTL striping pattern structure */
790 lum->lmm_magic = LOV_USER_MAGIC_V1;
791 lum->lmm_pattern = param->lsp_stripe_pattern;
792 lum->lmm_stripe_size = param->lsp_stripe_size;
793 lum->lmm_stripe_count = param->lsp_stripe_count;
794 lum->lmm_stripe_offset = param->lsp_stripe_offset;
795 if (pool_name != NULL) {
796 struct lov_user_md_v3 *lumv3 = (void *)lum;
798 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
799 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
801 if (param->lsp_is_specific) {
802 struct lov_user_md_v3 *lumv3 = (void *)lum;
805 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
806 if (pool_name == NULL) {
807 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
808 * OST list, therefore if pool is not specified we have
809 * to pack a null pool name for placeholder. */
810 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
813 for (i = 0; i < param->lsp_stripe_count; i++)
814 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
817 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
818 char *errmsg = "stripe already set";
821 if (errno != EEXIST && errno != EALREADY)
822 errmsg = strerror(errno);
824 llapi_err_noerrno(LLAPI_MSG_ERROR,
825 "error on ioctl "LPX64" for '%s' (%d): %s",
826 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,
838 int llapi_file_open_pool(const char *name, int flags, int mode,
839 unsigned long long stripe_size, int stripe_offset,
840 int stripe_count, int stripe_pattern, char *pool_name)
842 const struct llapi_stripe_param param = {
843 .lsp_stripe_size = stripe_size,
844 .lsp_stripe_count = stripe_count,
845 .lsp_stripe_pattern = stripe_pattern,
846 .lsp_stripe_offset = stripe_offset,
847 .lsp_pool = pool_name
849 return llapi_file_open_param(name, flags, mode, ¶m);
852 int llapi_file_open(const char *name, int flags, int mode,
853 unsigned long long stripe_size, int stripe_offset,
854 int stripe_count, int stripe_pattern)
856 return llapi_file_open_pool(name, flags, mode, stripe_size,
857 stripe_offset, stripe_count,
858 stripe_pattern, NULL);
861 int llapi_file_create(const char *name, unsigned long long stripe_size,
862 int stripe_offset, int stripe_count, int stripe_pattern)
866 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
867 stripe_offset, stripe_count, stripe_pattern,
876 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
877 int stripe_offset, int stripe_count,
878 int stripe_pattern, char *pool_name)
882 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
883 stripe_offset, stripe_count, stripe_pattern,
892 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
893 int stripe_count, int stripe_pattern,
894 const char *pool_name)
896 struct lmv_user_md lum = { 0 };
900 lum.lum_magic = LMV_USER_MAGIC;
901 lum.lum_stripe_offset = stripe_offset;
902 lum.lum_stripe_count = stripe_count;
903 lum.lum_hash_type = stripe_pattern;
904 if (pool_name != NULL) {
905 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
906 llapi_err_noerrno(LLAPI_MSG_ERROR,
907 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
908 ": too large pool name: %s", name, pool_name);
911 strncpy(lum.lum_pool_name, pool_name,
912 sizeof(lum.lum_pool_name));
915 fd = open(name, O_DIRECTORY | O_RDONLY);
918 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
922 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
924 char *errmsg = "stripe already set";
926 if (errno != EEXIST && errno != EALREADY)
927 errmsg = strerror(errno);
929 llapi_err_noerrno(LLAPI_MSG_ERROR,
930 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
937 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
938 int stripe_count, int stripe_pattern,
939 const char *pool_name)
941 struct lmv_user_md lmu = { 0 };
942 struct obd_ioctl_data data = { 0 };
945 char *dirpath = NULL;
946 char *namepath = NULL;
952 dirpath = strdup(name);
953 namepath = strdup(name);
954 if (!dirpath || !namepath)
957 lmu.lum_magic = LMV_USER_MAGIC;
958 lmu.lum_stripe_offset = stripe_offset;
959 lmu.lum_stripe_count = stripe_count;
960 lmu.lum_hash_type = stripe_pattern;
961 if (pool_name != NULL) {
962 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
963 llapi_err_noerrno(LLAPI_MSG_ERROR,
964 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
965 "pool name: %s", name, pool_name);
969 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
972 filename = basename(namepath);
973 dir = dirname(dirpath);
975 data.ioc_inlbuf1 = (char *)filename;
976 data.ioc_inllen1 = strlen(filename) + 1;
977 data.ioc_inlbuf2 = (char *)&lmu;
978 data.ioc_inllen2 = sizeof(struct lmv_user_md);
979 data.ioc_type = mode;
980 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
982 llapi_error(LLAPI_MSG_ERROR, rc,
983 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
988 fd = open(dir, O_DIRECTORY | O_RDONLY);
991 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
995 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
996 char *errmsg = "stripe already set";
998 if (errno != EEXIST && errno != EALREADY)
999 errmsg = strerror(errno);
1001 llapi_err_noerrno(LLAPI_MSG_ERROR,
1002 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
1012 int llapi_direntry_remove(char *dname)
1014 char *dirpath = NULL;
1015 char *namepath = NULL;
1021 dirpath = strdup(dname);
1022 namepath = strdup(dname);
1023 if (!dirpath || !namepath)
1026 filename = basename(namepath);
1028 dir = dirname(dirpath);
1030 fd = open(dir, O_DIRECTORY | O_RDONLY);
1033 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1038 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1039 char *errmsg = strerror(errno);
1040 llapi_err_noerrno(LLAPI_MSG_ERROR,
1041 "error on ioctl "LPX64" for '%s' (%d): %s",
1042 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
1054 * Find the fsname, the full path, and/or an open fd.
1055 * Either the fsname or path must not be NULL
1057 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1060 char buf[PATH_MAX], mntdir[PATH_MAX];
1063 int idx = 0, len = 0, mntlen, fd;
1066 /* get the mount point */
1067 fp = setmntent(MOUNTED, "r");
1070 llapi_error(LLAPI_MSG_ERROR, rc,
1071 "setmntent(%s) failed", MOUNTED);
1075 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1078 if (!llapi_is_lustre_mnt(&mnt))
1081 if ((want & WANT_INDEX) && (idx++ != index))
1084 mntlen = strlen(mnt.mnt_dir);
1085 ptr = strrchr(mnt.mnt_fsname, '/');
1086 /* thanks to the call to llapi_is_lustre_mnt() above,
1087 * we are sure that mnt.mnt_fsname contains ":/",
1088 * so ptr should never be NULL */
1093 /* Check the fsname for a match, if given */
1094 if (!(want & WANT_FSNAME) && fsname != NULL &&
1095 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
1098 /* If the path isn't set return the first one we find */
1099 if (path == NULL || strlen(path) == 0) {
1100 strcpy(mntdir, mnt.mnt_dir);
1101 if ((want & WANT_FSNAME) && fsname != NULL)
1102 strcpy(fsname, ptr);
1105 /* Otherwise find the longest matching path */
1106 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1107 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1108 strcpy(mntdir, mnt.mnt_dir);
1110 if ((want & WANT_FSNAME) && fsname != NULL)
1111 strcpy(fsname, ptr);
1119 if ((want & WANT_PATH) && path != NULL)
1120 strcpy(path, mntdir);
1121 if (want & WANT_FD) {
1122 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1125 llapi_error(LLAPI_MSG_ERROR, rc,
1126 "error opening '%s'", mntdir);
1132 } else if (want & WANT_ERROR)
1133 llapi_err_noerrno(LLAPI_MSG_ERROR,
1134 "can't find fs root for '%s': %d",
1135 (want & WANT_PATH) ? fsname : path, rc);
1140 * search lustre mounts
1142 * Calling this function will return to the user the mount point, mntdir, and
1143 * the file system name, fsname, if the user passed a buffer to this routine.
1145 * The user inputs are pathname and index. If the pathname is supplied then
1146 * the value of the index will be ignored. The pathname will return data if
1147 * the pathname is located on a lustre mount. Index is used to pick which
1148 * mount point you want in the case of multiple mounted lustre file systems.
1149 * See function lfs_osts in lfs.c for an example of the index use.
1151 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1154 int want = WANT_PATH, idx = -1;
1156 if (!pathname || pathname[0] == '\0') {
1160 strcpy(mntdir, pathname);
1163 want |= WANT_FSNAME;
1164 return get_root_path(want, fsname, NULL, mntdir, idx);
1167 /* Given a path, find the corresponding Lustre fsname */
1168 int llapi_search_fsname(const char *pathname, char *fsname)
1173 path = realpath(pathname, NULL);
1175 char buf[PATH_MAX], *ptr;
1178 if (pathname[0] != '/') {
1179 /* Need an absolute path, but realpath() only works for
1180 * pathnames that actually exist. We go through the
1181 * extra hurdle of dirname(getcwd() + pathname) in
1182 * case the relative pathname contains ".." in it. */
1183 if (getcwd(buf, sizeof(buf) - 2) == NULL)
1185 rc = strlcat(buf, "/", sizeof(buf));
1186 if (rc >= sizeof(buf))
1189 rc = strlcat(buf, pathname, sizeof(buf));
1190 if (rc >= sizeof(buf))
1192 path = realpath(buf, NULL);
1194 ptr = strrchr(buf, '/');
1198 path = realpath(buf, NULL);
1201 llapi_error(LLAPI_MSG_ERROR, rc,
1202 "pathname '%s' cannot expand",
1208 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1213 int llapi_search_rootpath(char *pathname, const char *fsname)
1215 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1218 int llapi_getname(const char *path, char *buf, size_t size)
1220 struct obd_uuid uuid_buf;
1221 char *uuid = uuid_buf.uuid;
1224 memset(&uuid_buf, 0, sizeof(uuid_buf));
1225 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1229 /* We want to turn lustre-clilov-ffff88002738bc00 into
1230 * lustre-ffff88002738bc00. */
1232 nr = snprintf(buf, size, "%.*s-%s",
1233 (int) (strlen(uuid) - 24), uuid,
1234 uuid + strlen(uuid) - 16);
1244 * find the pool directory path under /proc
1245 * (can be also used to test if a fsname is known)
1247 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1250 char pattern[PATH_MAX + 1];
1251 char buffer[PATH_MAX];
1253 if (fsname == NULL) {
1254 rc = llapi_search_fsname(pathname, buffer);
1258 strcpy(pathname, fsname);
1261 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1262 rc = first_match(pattern, buffer);
1266 /* in fsname test mode, pool_pathname is NULL */
1267 if (pool_pathname != NULL)
1268 strcpy(pool_pathname, buffer);
1274 * Get the list of pool members.
1275 * \param poolname string of format \<fsname\>.\<poolname\>
1276 * \param members caller-allocated array of char*
1277 * \param list_size size of the members array
1278 * \param buffer caller-allocated buffer for storing OST names
1279 * \param buffer_size size of the buffer
1281 * \return number of members retrieved for this pool
1282 * \retval -error failure
1284 int llapi_get_poolmembers(const char *poolname, char **members,
1285 int list_size, char *buffer, int buffer_size)
1287 char fsname[PATH_MAX];
1289 char pathname[PATH_MAX];
1290 char path[PATH_MAX];
1297 /* name is FSNAME.POOLNAME */
1298 if (strlen(poolname) >= sizeof(fsname))
1300 strlcpy(fsname, poolname, sizeof(fsname));
1301 pool = strchr(fsname, '.');
1308 rc = poolpath(fsname, NULL, pathname);
1310 llapi_error(LLAPI_MSG_ERROR, rc,
1311 "Lustre filesystem '%s' not found",
1316 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1317 rc = snprintf(path, sizeof(path), "%s/%s", pathname, pool);
1318 if (rc >= sizeof(path))
1320 fd = fopen(path, "r");
1323 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1328 while (fgets(buf, sizeof(buf), fd) != NULL) {
1329 if (nb_entries >= list_size) {
1333 buf[sizeof(buf) - 1] = '\0';
1335 tmp = strchr(buf, '\n');
1338 if (used + strlen(buf) + 1 > buffer_size) {
1343 strcpy(buffer + used, buf);
1344 members[nb_entries] = buffer + used;
1345 used += strlen(buf) + 1;
1355 * Get the list of pools in a filesystem.
1356 * \param name filesystem name or path
1357 * \param poollist caller-allocated array of char*
1358 * \param list_size size of the poollist array
1359 * \param buffer caller-allocated buffer for storing pool names
1360 * \param buffer_size size of the buffer
1362 * \return number of pools retrieved for this filesystem
1363 * \retval -error failure
1365 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1366 char *buffer, int buffer_size)
1368 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1372 struct dirent *cookie = NULL;
1374 unsigned int nb_entries = 0;
1375 unsigned int used = 0;
1378 /* initialize output array */
1379 for (i = 0; i < list_size; i++)
1382 /* is name a pathname ? */
1383 ptr = strchr(name, '/');
1385 /* only absolute pathname is supported */
1389 if (!realpath(name, rname)) {
1391 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1396 rc = poolpath(NULL, rname, pathname);
1398 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1399 " a Lustre filesystem", name);
1402 if (strlen(rname) > sizeof(fsname)-1)
1404 strncpy(fsname, rname, sizeof(fsname));
1406 /* name is FSNAME */
1407 if (strlen(name) > sizeof(fsname)-1)
1409 strncpy(fsname, name, sizeof(fsname));
1410 rc = poolpath(fsname, NULL, pathname);
1413 llapi_error(LLAPI_MSG_ERROR, rc,
1414 "Lustre filesystem '%s' not found", name);
1418 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1419 dir = opendir(pathname);
1422 llapi_error(LLAPI_MSG_ERROR, rc,
1423 "Could not open pool list for '%s'",
1429 rc = readdir_r(dir, &pool, &cookie);
1433 llapi_error(LLAPI_MSG_ERROR, rc,
1434 "Error reading pool list for '%s'", name);
1436 } else if ((rc == 0) && (cookie == NULL)) {
1437 /* end of directory */
1441 /* ignore . and .. */
1442 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1445 /* check output bounds */
1446 if (nb_entries >= list_size) {
1451 /* +2 for '.' and final '\0' */
1452 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1458 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1459 poollist[nb_entries] = buffer + used;
1460 used += strlen(pool.d_name) + strlen(fsname) + 2;
1466 return ((rc != 0) ? rc : nb_entries);
1469 /* wrapper for lfs.c and obd.c */
1470 int llapi_poollist(const char *name)
1472 /* list of pool names (assume that pool count is smaller
1474 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1475 int obdcount, bufsize, rc, nb, i;
1476 char *poolname = NULL, *tmp = NULL, data[16];
1481 if (name[0] != '/') {
1482 fsname = strdup(name);
1486 poolname = strchr(fsname, '.');
1490 path = (char *) name;
1493 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1494 data, sizeof(data));
1497 obdcount = atoi(data);
1499 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1500 * and also an array to store the pointers for all that
1501 * allocated space. */
1503 bufsize = sizeof(struct obd_uuid) * obdcount;
1504 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1505 if (buffer == NULL) {
1509 list = (char **) (buffer + bufsize);
1512 /* name is a path or fsname */
1513 nb = llapi_get_poollist(name, list, obdcount,
1516 /* name is a pool name (<fsname>.<poolname>) */
1517 nb = llapi_get_poolmembers(name, list, obdcount,
1521 if (nb == -EOVERFLOW) {
1524 goto retry_get_pools;
1527 for (i = 0; i < nb; i++)
1528 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1529 rc = (nb < 0 ? nb : 0);
1538 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1539 void *data, struct dirent64 *de);
1541 #define OBD_NOT_FOUND (-1)
1543 static int common_param_init(struct find_param *param, char *path)
1545 int lum_size = get_mds_md_size(path);
1547 if (lum_size < PATH_MAX + 1)
1548 lum_size = PATH_MAX + 1;
1550 param->fp_lum_size = lum_size;
1551 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1552 if (param->fp_lmd == NULL) {
1553 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1554 "error: allocation of %zu bytes for ioctl",
1555 sizeof(lstat_t) + param->fp_lum_size);
1559 param->fp_lmv_stripe_count = 256;
1560 param->fp_lmv_md = calloc(1,
1561 lmv_user_md_size(param->fp_lmv_stripe_count,
1563 if (param->fp_lmv_md == NULL) {
1564 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1565 "error: allocation of %d bytes for ioctl",
1566 lmv_user_md_size(param->fp_lmv_stripe_count,
1571 param->fp_got_uuids = 0;
1572 param->fp_obd_indexes = NULL;
1573 param->fp_obd_index = OBD_NOT_FOUND;
1574 if (!param->fp_migrate)
1575 param->fp_mdt_index = OBD_NOT_FOUND;
1579 static void find_param_fini(struct find_param *param)
1581 if (param->fp_obd_indexes)
1582 free(param->fp_obd_indexes);
1585 free(param->fp_lmd);
1587 if (param->fp_lmv_md)
1588 free(param->fp_lmv_md);
1591 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1592 struct dirent64 *de)
1594 struct find_param *param = data;
1600 /* set errno upon failure */
1601 static DIR *opendir_parent(char *path)
1607 fname = strrchr(path, '/');
1609 return opendir(".");
1613 parent = opendir(path);
1618 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1623 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1624 if (param->fp_get_default_lmv)
1625 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1627 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1629 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1630 if (errno == E2BIG && ret != 0) {
1634 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1635 if (stripe_count <= param->fp_lmv_stripe_count)
1638 free(param->fp_lmv_md);
1639 param->fp_lmv_stripe_count = stripe_count;
1640 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1641 param->fp_lmv_md = malloc(lmv_size);
1642 if (param->fp_lmv_md == NULL) {
1643 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1644 "error: allocation of %d bytes for ioctl",
1645 lmv_user_md_size(param->fp_lmv_stripe_count,
1654 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1655 struct lov_user_mds_data *lmd, int lumlen)
1657 lstat_t *st = &lmd->lmd_st;
1660 if (parent == NULL && dir == NULL)
1664 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1665 } else if (parent) {
1666 char *fname = strrchr(path, '/');
1668 /* To avoid opening, locking, and closing each file on the
1669 * client if that is not needed. The GETFILEINFO ioctl can
1670 * be done on the patent dir with a single open for all
1671 * files in that directory, and it also doesn't pollute the
1672 * client dcache with millions of dentries when traversing
1673 * a large filesystem. */
1674 fname = (fname == NULL ? path : fname + 1);
1675 /* retrieve needed file info */
1676 strlcpy((char *)lmd, fname, lumlen);
1677 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1681 if (errno == ENOTTY) {
1682 /* ioctl is not supported, it is not a lustre fs.
1683 * Do the regular lstat(2) instead. */
1684 ret = lstat_f(path, st);
1687 llapi_error(LLAPI_MSG_ERROR, ret,
1688 "error: %s: lstat failed for %s",
1691 } else if (errno == ENOENT) {
1693 llapi_error(LLAPI_MSG_WARN, ret,
1694 "warning: %s: %s does not exist",
1696 } else if (errno != EISDIR) {
1698 llapi_error(LLAPI_MSG_ERROR, ret,
1699 "%s ioctl failed for %s.",
1700 dir ? "LL_IOC_MDC_GETINFO" :
1701 "IOC_MDC_GETFILEINFO", path);
1704 llapi_error(LLAPI_MSG_ERROR, ret,
1705 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1712 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1713 semantic_func_t sem_init,
1714 semantic_func_t sem_fini, void *data,
1715 struct dirent64 *de)
1717 struct find_param *param = (struct find_param *)data;
1718 struct dirent64 *dent;
1726 if (!d && errno != ENOTDIR) {
1728 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1731 } else if (!d && !parent) {
1732 /* ENOTDIR. Open the parent dir. */
1733 p = opendir_parent(path);
1740 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1746 while ((dent = readdir64(d)) != NULL) {
1749 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1752 /* Don't traverse .lustre directory */
1753 if (!(strcmp(dent->d_name, dot_lustre_name)))
1757 if ((len + dent->d_reclen + 2) > size) {
1758 llapi_err_noerrno(LLAPI_MSG_ERROR,
1759 "error: %s: string buffer is too small",
1764 strcat(path, dent->d_name);
1766 if (dent->d_type == DT_UNKNOWN) {
1767 lstat_t *st = ¶m->fp_lmd->lmd_st;
1769 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1770 param->fp_lum_size);
1772 dent->d_type = IFTODT(st->st_mode);
1779 switch (dent->d_type) {
1781 llapi_err_noerrno(LLAPI_MSG_ERROR,
1782 "error: %s: '%s' is UNKNOWN type %d",
1783 __func__, dent->d_name, dent->d_type);
1786 rc = llapi_semantic_traverse(path, size, d, sem_init,
1787 sem_fini, data, dent);
1788 if (rc != 0 && ret == 0)
1794 rc = sem_init(path, d, NULL, data, dent);
1795 if (rc < 0 && ret == 0)
1798 if (sem_fini && rc == 0)
1799 sem_fini(path, d, NULL, data, dent);
1807 sem_fini(path, parent, &d, data, de);
1816 static int param_callback(char *path, semantic_func_t sem_init,
1817 semantic_func_t sem_fini, struct find_param *param)
1819 int ret, len = strlen(path);
1822 if (len > PATH_MAX) {
1824 llapi_error(LLAPI_MSG_ERROR, ret,
1825 "Path name '%s' is too long", path);
1829 buf = (char *)malloc(PATH_MAX + 1);
1833 strlcpy(buf, path, PATH_MAX + 1);
1834 ret = common_param_init(param, buf);
1838 param->fp_depth = 0;
1840 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1841 sem_fini, param, NULL);
1843 find_param_fini(param);
1845 return ret < 0 ? ret : 0;
1848 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1850 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1853 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1858 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1860 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1863 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1868 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1872 fd = open(path, O_RDONLY);
1875 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1879 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1885 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1889 fd = open(path, O_RDONLY);
1892 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1896 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1907 * If uuidp is NULL, return the number of available obd uuids.
1908 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1909 * there are more OSTs than allocated to uuidp, then an error is returned with
1910 * the ost_count set to number of available obd uuids.
1912 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1913 int *ost_count, enum tgt_type type)
1915 struct obd_uuid name;
1919 int rc = 0, index = 0;
1921 /* Get the lov name */
1922 if (type == LOV_TYPE) {
1923 rc = llapi_file_fget_lov_uuid(fd, &name);
1927 rc = llapi_file_fget_lmv_uuid(fd, &name);
1932 /* Now get the ost uuids from /proc */
1933 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1934 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1935 fp = fopen(buf, "r");
1938 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1942 snprintf(format, sizeof(format),
1943 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1944 while (fgets(buf, sizeof(buf), fp) != NULL) {
1945 if (uuidp && (index < *ost_count)) {
1946 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1954 if (uuidp && (index > *ost_count))
1961 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1963 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1966 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1971 root = opendir(mnt);
1974 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1979 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1987 /* Check if user specified value matches a real uuid. Ignore _UUID,
1988 * -osc-4ba41334, other trailing gunk in comparison.
1989 * @param real_uuid ends in "_UUID"
1990 * @param search_uuid may or may not end in "_UUID"
1992 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1994 int cmplen = strlen(real_uuid);
1995 int searchlen = strlen(search_uuid);
1997 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1999 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2002 /* The UUIDs may legitimately be different lengths, if
2003 * the system was upgraded from an older version. */
2004 if (cmplen != searchlen)
2007 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2010 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2011 * returned in param->fp_obd_index */
2012 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
2014 struct obd_uuid obd_uuid;
2020 if (param->fp_got_uuids)
2023 /* Get the lov/lmv name */
2024 if (param->fp_get_lmv)
2025 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
2027 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
2029 if (rc != -ENOTTY) {
2030 llapi_error(LLAPI_MSG_ERROR, rc,
2031 "error: can't get lov name: %s", dname);
2038 param->fp_got_uuids = 1;
2040 /* Now get the ost uuids from /proc */
2041 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
2042 param->fp_get_lmv ? "lmv" : "lov", obd_uuid.uuid);
2043 fp = fopen(buf, "r");
2046 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
2050 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2051 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2052 param->fp_get_lmv ? "MDTS" : "OBDS:");
2054 snprintf(format, sizeof(format),
2055 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2056 while (fgets(buf, sizeof(buf), fp) != NULL) {
2059 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2062 if (param->fp_obd_uuid) {
2063 if (llapi_uuid_match(obd_uuid.uuid,
2064 param->fp_obd_uuid->uuid)) {
2065 param->fp_obd_index = index;
2068 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2069 /* Print everything */
2070 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2073 param->fp_obds_printed = 1;
2077 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2078 llapi_err_noerrno(LLAPI_MSG_ERROR,
2079 "error: %s: unknown obduuid: %s",
2080 __func__, param->fp_obd_uuid->uuid);
2087 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2088 * obd index for all these obduuids will be returned in
2089 * param->fp_obd_indexes */
2090 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2091 int num_obds, int **obdindexes, int *obdindex,
2094 int ret, obdcount, obd_valid = 0, obdnum;
2096 struct obd_uuid *uuids = NULL;
2100 if (type == LOV_TYPE)
2101 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2103 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2107 obdcount = atoi(buf);
2108 uuids = (struct obd_uuid *)malloc(obdcount *
2109 sizeof(struct obd_uuid));
2114 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2116 struct obd_uuid *uuids_temp;
2118 if (ret == -EOVERFLOW) {
2119 uuids_temp = realloc(uuids, obdcount *
2120 sizeof(struct obd_uuid));
2121 if (uuids_temp != NULL) {
2123 goto retry_get_uuids;
2129 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
2133 indexes = malloc(num_obds * sizeof(*obdindex));
2134 if (indexes == NULL) {
2139 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2142 /* The user may have specified a simple index */
2143 i = strtol(obduuids[obdnum].uuid, &end, 0);
2144 if (end && *end == '\0' && i < obdcount) {
2145 indexes[obdnum] = i;
2148 for (i = 0; i < obdcount; i++) {
2149 if (llapi_uuid_match(uuids[i].uuid,
2150 obduuids[obdnum].uuid)) {
2151 indexes[obdnum] = i;
2157 if (i >= obdcount) {
2158 indexes[obdnum] = OBD_NOT_FOUND;
2159 llapi_err_noerrno(LLAPI_MSG_ERROR,
2160 "error: %s: unknown obduuid: %s",
2161 __func__, obduuids[obdnum].uuid);
2167 *obdindex = OBD_NOT_FOUND;
2169 *obdindex = obd_valid;
2171 *obdindexes = indexes;
2179 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2183 if (param->fp_mdt_uuid) {
2184 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2186 ¶m->fp_mdt_indexes,
2187 ¶m->fp_mdt_index, LMV_TYPE);
2192 if (param->fp_obd_uuid) {
2193 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2195 ¶m->fp_obd_indexes,
2196 ¶m->fp_obd_index, LOV_TYPE);
2201 param->fp_got_uuids = 1;
2206 int llapi_ostlist(char *path, struct find_param *param)
2211 dir = opendir(path);
2215 ret = setup_obd_uuid(dir, path, param);
2222 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2223 * tries to determine the path to the filesystem's clilov directory under /proc
2225 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2226 * The NUL terminator is compensated by the additional "%s" bytes. */
2227 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2228 static int clilovpath(const char *fsname, const char *const pathname,
2232 char pattern[LOV_LEN];
2233 char buffer[PATH_MAX + 1];
2235 if (fsname == NULL) {
2236 rc = llapi_search_fsname(pathname, buffer);
2242 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2245 rc = first_match(pattern, buffer);
2249 strlcpy(clilovpath, buffer, sizeof(buffer));
2255 * Given the path to a stripe attribute proc file, tries to open and
2256 * read the attribute and return the value using the attr parameter
2258 static int sattr_read_attr(const char *const fpath,
2263 char line[PATH_MAX + 1];
2266 f = fopen(fpath, "r");
2269 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2273 if (fgets(line, sizeof(line), f) != NULL) {
2276 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2285 * Tries to determine the default stripe attributes for a given filesystem. The
2286 * filesystem to check should be specified by fsname, or will be determined
2289 static int sattr_get_defaults(const char *const fsname,
2290 const char *const pathname,
2291 unsigned int *scount,
2292 unsigned int *ssize,
2293 unsigned int *soffset)
2296 char dpath[PATH_MAX + 1];
2297 char fpath[PATH_MAX + 1];
2299 rc = clilovpath(fsname, pathname, dpath);
2304 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2305 rc = sattr_read_attr(fpath, scount);
2311 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2312 rc = sattr_read_attr(fpath, ssize);
2318 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2319 rc = sattr_read_attr(fpath, soffset);
2328 * Tries to gather the default stripe attributes for a given filesystem. If
2329 * the attributes can be determined, they are cached for easy retreival the
2330 * next time they are needed. Only a single filesystem's attributes are
2333 static int sattr_cache_get_defaults(const char *const fsname,
2334 const char *const pathname,
2335 unsigned int *scount,
2336 unsigned int *ssize,
2337 unsigned int *soffset)
2340 char fsname[PATH_MAX + 1];
2341 unsigned int stripecount;
2342 unsigned int stripesize;
2343 unsigned int stripeoffset;
2349 char fsname_buf[PATH_MAX + 1];
2350 unsigned int tmp[3];
2352 if (fsname == NULL) {
2353 rc = llapi_search_fsname(pathname, fsname_buf);
2357 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2360 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2362 * Ensure all 3 sattrs (count, size, and offset) are
2363 * successfully retrieved and stored in tmp before writing to
2366 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2371 cache.stripecount = tmp[0];
2372 cache.stripesize = tmp[1];
2373 cache.stripeoffset = tmp[2];
2374 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2378 *scount = cache.stripecount;
2380 *ssize = cache.stripesize;
2382 *soffset = cache.stripeoffset;
2387 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2388 struct lov_user_ost_data_v1 *objects,
2389 int is_dir, int verbose, int depth,
2390 int raw, char *pool_name)
2392 char *prefix = is_dir ? "" : "lmm_";
2393 char *separator = "";
2396 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2397 lmm_oi_set_seq(&lum->lmm_oi, 0);
2398 if (verbose & VERBOSE_DETAIL)
2399 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2402 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2403 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2405 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2406 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2408 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2409 lmm_oi_seq(&lum->lmm_oi));
2410 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2411 lmm_oi_id(&lum->lmm_oi));
2414 if (verbose & VERBOSE_COUNT) {
2415 if (verbose & ~VERBOSE_COUNT)
2416 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2419 if (!raw && lum->lmm_stripe_count == 0) {
2420 unsigned int scount;
2421 rc = sattr_cache_get_defaults(NULL, path,
2425 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2428 llapi_error(LLAPI_MSG_ERROR, rc,
2429 "Cannot determine default"
2432 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2433 lum->lmm_stripe_count ==
2434 (typeof(lum->lmm_stripe_count))(-1)
2435 ? -1 : lum->lmm_stripe_count);
2438 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2439 (__s16)lum->lmm_stripe_count);
2441 separator = is_dir ? " " : "\n";
2444 if (verbose & VERBOSE_SIZE) {
2445 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2446 if (verbose & ~VERBOSE_SIZE)
2447 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2449 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2451 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2454 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2456 llapi_error(LLAPI_MSG_ERROR, rc,
2457 "Cannot determine default"
2460 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2461 lum->lmm_stripe_size);
2463 separator = is_dir ? " " : "\n";
2466 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2467 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2468 if (verbose & ~VERBOSE_LAYOUT)
2469 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2471 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2475 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2476 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2477 if (verbose & ~VERBOSE_GENERATION)
2478 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2480 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2481 (int)lum->lmm_layout_gen);
2485 if (verbose & VERBOSE_OFFSET) {
2486 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2487 if (verbose & ~VERBOSE_OFFSET)
2488 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2491 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2492 lum->lmm_stripe_offset ==
2493 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2494 lum->lmm_stripe_offset);
2496 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2497 objects[0].l_ost_idx);
2498 separator = is_dir ? " " : "\n";
2501 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2502 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2503 if (verbose & ~VERBOSE_POOL)
2504 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2506 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2509 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2510 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2513 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2514 struct lov_user_ost_data_v1 *objects,
2515 char *path, int is_dir, int obdindex,
2516 int depth, int header, int raw)
2518 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2521 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2522 if (obdindex == objects[i].l_ost_idx) {
2530 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2531 depth, raw, pool_name);
2533 if (!is_dir && (header & VERBOSE_OBJID) &&
2534 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2536 llapi_printf(LLAPI_MSG_NORMAL,
2537 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2539 for (i = 0; i < lum->lmm_stripe_count; i++) {
2540 int idx = objects[i].l_ost_idx;
2541 long long oid = ostid_id(&objects[i].l_ost_oi);
2542 long long gr = ostid_seq(&objects[i].l_ost_oi);
2543 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2545 sprintf(fmt, "%s%s%s\n",
2546 "\t%6u\t%14llu\t%#13llx\t",
2547 (fid_seq_is_rsvd(gr) ||
2548 fid_seq_is_mdt0(gr)) ?
2549 "%14llu" : "%#14llx", "%s");
2550 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2552 obdindex == idx ? " *" : "");
2556 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2560 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2561 char *path, int obdindex, int depth, int verbose)
2563 struct lmv_user_mds_data *objects = lum->lum_objects;
2564 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2565 int i, obdstripe = 0;
2566 char *separator = "";
2568 if (obdindex != OBD_NOT_FOUND) {
2569 if (lum->lum_stripe_count == 0) {
2570 if (obdindex == lum->lum_stripe_offset)
2573 for (i = 0; i < lum->lum_stripe_count; i++) {
2574 if (obdindex == objects[i].lum_mds) {
2575 llapi_printf(LLAPI_MSG_NORMAL,
2590 /* show all information default */
2592 if (lum->lum_magic == LMV_USER_MAGIC)
2593 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2595 verbose = VERBOSE_OBJID;
2598 if (depth && path && ((verbose != VERBOSE_OBJID)))
2599 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2601 if (verbose & VERBOSE_COUNT) {
2602 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2603 if (verbose & ~VERBOSE_COUNT)
2604 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2605 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2606 (int)lum->lum_stripe_count);
2607 if (verbose & VERBOSE_OFFSET)
2613 if (verbose & VERBOSE_OFFSET) {
2614 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2615 if (verbose & ~VERBOSE_OFFSET)
2616 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2617 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2618 (int)lum->lum_stripe_offset);
2622 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2623 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2624 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2625 llapi_printf(LLAPI_MSG_NORMAL,
2626 "mdtidx\t\t FID[seq:oid:ver]\n");
2627 for (i = 0; i < lum->lum_stripe_count; i++) {
2628 int idx = objects[i].lum_mds;
2629 struct lu_fid *fid = &objects[i].lum_fid;
2630 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2631 llapi_printf(LLAPI_MSG_NORMAL,
2632 "%6u\t\t "DFID"\t\t%s\n",
2634 obdindex == idx ? " *" : "");
2639 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2640 pool_name[0] != '\0') {
2641 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2642 if (verbose & ~VERBOSE_POOL)
2643 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2645 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2649 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2650 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2653 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2657 if (param->fp_get_lmv || param->fp_get_default_lmv)
2658 magic = (__u32)param->fp_lmv_md->lum_magic;
2660 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2663 case LOV_USER_MAGIC_V1:
2664 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2665 param->fp_lmd->lmd_lmm.lmm_objects,
2667 param->fp_obd_index, param->fp_max_depth,
2668 param->fp_verbose, param->fp_raw);
2670 case LOV_USER_MAGIC_V3: {
2671 char pool_name[LOV_MAXPOOLNAME + 1];
2672 struct lov_user_ost_data_v1 *objects;
2673 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2675 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2676 objects = lmmv3->lmm_objects;
2677 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm,
2678 pool_name[0] == '\0' ? NULL : pool_name,
2679 objects, path, is_dir,
2680 param->fp_obd_index, param->fp_max_depth,
2681 param->fp_verbose, param->fp_raw);
2685 case LMV_USER_MAGIC: {
2686 char pool_name[LOV_MAXPOOLNAME + 1];
2687 struct lmv_user_md *lum;
2689 lum = (struct lmv_user_md *)param->fp_lmv_md;
2690 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2691 lmv_dump_user_lmm(lum,
2692 pool_name[0] == '\0' ? NULL : pool_name,
2693 path, param->fp_obd_index,
2694 param->fp_max_depth, param->fp_verbose);
2698 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2699 "(expecting one of %#x %#x %#x %#x)\n",
2700 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2701 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2702 LMV_USER_MAGIC, LMV_MAGIC_V1);
2707 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2713 fname = strrchr(path, '/');
2715 /* It should be a file (or other non-directory) */
2716 if (fname == NULL) {
2717 dname = (char *)malloc(2);
2721 fname = (char *)path;
2723 dname = (char *)malloc(fname - path + 1);
2726 strncpy(dname, path, fname - path);
2727 dname[fname - path] = '\0';
2731 fd = open(dname, O_RDONLY);
2738 strcpy((char *)lum, fname);
2739 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2742 if (close(fd) == -1 && rc == 0)
2749 int llapi_file_lookup(int dirfd, const char *name)
2751 struct obd_ioctl_data data = { 0 };
2756 if (dirfd < 0 || name == NULL)
2759 data.ioc_version = OBD_IOCTL_VERSION;
2760 data.ioc_len = sizeof(data);
2761 data.ioc_inlbuf1 = (char *)name;
2762 data.ioc_inllen1 = strlen(name) + 1;
2764 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2766 llapi_error(LLAPI_MSG_ERROR, rc,
2767 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2772 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2778 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2779 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2781 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2782 * The table below gives the answers for the specified parameters (value and
2783 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2784 * --------------------------------------
2785 * 1 | file > limit; sign > 0 | -1 / -1 |
2786 * 2 | file = limit; sign > 0 | -1 / -1 |
2787 * 3 | file < limit; sign > 0 | ? / 1 |
2788 * 4 | file > limit; sign = 0 | -1 / -1 |
2789 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2790 * 6 | file < limit; sign = 0 | ? / -1 |
2791 * 7 | file > limit; sign < 0 | 1 / 1 |
2792 * 8 | file = limit; sign < 0 | ? / -1 |
2793 * 9 | file < limit; sign < 0 | ? / -1 |
2794 * --------------------------------------
2795 * Note: 5th actually means that the value is within the interval
2796 * (limit - margin, limit]. */
2797 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2798 int sign, int negopt, unsigned long long margin,
2804 /* Drop the fraction of margin (of days). */
2805 if (file + margin <= limit)
2807 } else if (sign == 0) {
2808 if (file <= limit && file + margin > limit)
2810 else if (file + margin <= limit)
2812 } else if (sign < 0) {
2819 return negopt ? ~ret + 1 : ret;
2822 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2823 * Return -1 or 1 if file timestamp does not or does match the given criteria
2824 * correspondingly. Return 0 if the MDS time is being checked and there are
2825 * attributes on OSTs and it is not yet clear if the timespamp matches.
2827 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2828 * updated timestamps. */
2829 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2834 /* Check if file is accepted. */
2835 if (param->fp_atime) {
2836 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2837 param->fp_asign, param->fp_exclude_atime,
2844 if (param->fp_mtime) {
2845 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2846 param->fp_msign, param->fp_exclude_mtime,
2851 /* If the previous check matches, but this one is not yet clear,
2852 * we should return 0 to do an RPC on OSTs. */
2857 if (param->fp_ctime) {
2858 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2859 param->fp_csign, param->fp_exclude_ctime,
2864 /* If the previous check matches, but this one is not yet clear,
2865 * we should return 0 to do an RPC on OSTs. */
2874 * Check whether the stripes matches the indexes user provided
2878 static int check_obd_match(struct find_param *param)
2880 lstat_t *st = ¶m->fp_lmd->lmd_st;
2881 struct lov_user_ost_data_v1 *lmm_objects;
2884 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
2887 if (!S_ISREG(st->st_mode))
2890 /* Only those files should be accepted, which have a
2891 * stripe on the specified OST. */
2892 if (!param->fp_lmd->lmd_lmm.lmm_stripe_count)
2895 if (param->fp_lmd->lmd_lmm.lmm_magic ==
2896 LOV_USER_MAGIC_V3) {
2897 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2899 lmm_objects = lmmv3->lmm_objects;
2900 } else if (param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2901 lmm_objects = param->fp_lmd->lmd_lmm.lmm_objects;
2903 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2904 __func__, param->fp_lmd->lmd_lmm.lmm_magic);
2908 for (i = 0; i < param->fp_lmd->lmd_lmm.lmm_stripe_count; i++) {
2909 for (j = 0; j < param->fp_num_obds; j++) {
2910 if (param->fp_obd_indexes[j] ==
2911 lmm_objects[i].l_ost_idx) {
2912 if (param->fp_exclude_obd)
2919 if (param->fp_exclude_obd)
2925 static int check_mdt_match(struct find_param *param)
2929 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
2932 /* FIXME: For striped dir, we should get stripe information and check */
2933 for (i = 0; i < param->fp_num_mdts; i++) {
2934 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
2935 return !param->fp_exclude_mdt;
2938 if (param->fp_exclude_mdt)
2945 * Check whether the obd is active or not, if it is
2946 * not active, just print the object affected by this
2949 static int print_failed_tgt(struct find_param *param, char *path, int type)
2951 struct obd_statfs stat_buf;
2952 struct obd_uuid uuid_buf;
2955 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2957 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2958 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2959 ret = llapi_obd_statfs(path, type,
2960 param->fp_obd_index, &stat_buf,
2963 llapi_printf(LLAPI_MSG_NORMAL,
2964 "obd_uuid: %s failed %s ",
2965 param->fp_obd_uuid->uuid,
2972 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2973 void *data, struct dirent64 *de)
2975 struct find_param *param = (struct find_param *)data;
2976 DIR *dir = dirp == NULL ? NULL : *dirp;
2977 int decision = 1; /* 1 is accepted; -1 is rejected. */
2978 lstat_t *st = ¶m->fp_lmd->lmd_st;
2980 int checked_type = 0;
2983 LASSERT(parent != NULL || dir != NULL);
2985 param->fp_lmd->lmd_lmm.lmm_stripe_count = 0;
2987 /* If a regular expression is presented, make the initial decision */
2988 if (param->fp_pattern != NULL) {
2989 char *fname = strrchr(path, '/');
2990 fname = (fname == NULL ? path : fname + 1);
2991 ret = fnmatch(param->fp_pattern, fname, 0);
2992 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
2993 (ret == 0 && param->fp_exclude_pattern))
2997 /* See if we can check the file type from the dirent. */
2998 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
3001 if (DTTOIF(de->d_type) == param->fp_type) {
3002 if (param->fp_exclude_type)
3005 if (!param->fp_exclude_type)
3012 /* Request MDS for the stat info if some of these parameters need
3013 * to be compared. */
3014 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
3015 param->fp_check_uid || param->fp_check_gid ||
3016 param->fp_atime || param->fp_mtime || param->fp_ctime ||
3017 param->fp_check_pool || param->fp_check_size ||
3018 param->fp_check_stripe_count || param->fp_check_stripe_size ||
3019 param->fp_check_layout)
3022 if (param->fp_type != 0 && checked_type == 0)
3025 if (decision == 0) {
3026 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
3027 param->fp_lum_size);
3028 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
3029 (param->fp_check_pool || param->fp_check_stripe_count ||
3030 param->fp_check_stripe_size || param->fp_check_layout)) {
3031 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
3033 /* We need to "fake" the "use the default" values
3034 * since the lmm struct is zeroed out at this point. */
3035 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3036 lmm->lmm_pattern = 0xFFFFFFFF;
3038 ostid_set_seq(&lmm->lmm_oi,
3039 FID_SEQ_LOV_DEFAULT);
3040 lmm->lmm_stripe_size = 0;
3041 lmm->lmm_stripe_count = 0;
3042 lmm->lmm_stripe_offset = -1;
3044 if (ret == 0 && param->fp_mdt_uuid != NULL) {
3046 ret = llapi_file_fget_mdtidx(dirfd(dir),
3047 ¶m->fp_file_mdt_index);
3048 } else if (S_ISREG(st->st_mode)) {
3051 /* FIXME: we could get the MDT index from the
3052 * file's FID in lmd->lmd_lmm.lmm_oi without
3053 * opening the file, once we are sure that
3054 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
3055 * That would still be an ioctl() to map the
3056 * FID to the MDT, but not an open RPC. */
3057 fd = open(path, O_RDONLY);
3059 ret = llapi_file_fget_mdtidx(fd,
3060 ¶m->fp_file_mdt_index);
3066 /* For a special file, we assume it resides on
3067 * the same MDT as the parent directory. */
3068 ret = llapi_file_fget_mdtidx(dirfd(parent),
3069 ¶m->fp_file_mdt_index);
3082 if (param->fp_type && !checked_type) {
3083 if ((st->st_mode & S_IFMT) == param->fp_type) {
3084 if (param->fp_exclude_type)
3087 if (!param->fp_exclude_type)
3093 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
3094 if (lustre_fs && param->fp_got_uuids &&
3095 param->fp_dev != st->st_dev) {
3096 /* A lustre/lustre mount point is crossed. */
3097 param->fp_got_uuids = 0;
3098 param->fp_obds_printed = 0;
3099 param->fp_mdt_index = OBD_NOT_FOUND;
3100 param->fp_obd_index = OBD_NOT_FOUND;
3103 if (lustre_fs && !param->fp_got_uuids) {
3104 ret = setup_target_indexes(dir ? dir : parent, path,
3109 param->fp_dev = st->st_dev;
3110 } else if (!lustre_fs && param->fp_got_uuids) {
3111 /* A lustre/non-lustre mount point is crossed. */
3112 param->fp_got_uuids = 0;
3113 param->fp_mdt_index = OBD_NOT_FOUND;
3114 param->fp_obd_index = OBD_NOT_FOUND;
3118 if (param->fp_check_stripe_size) {
3119 decision = find_value_cmp(
3120 param->fp_lmd->lmd_lmm.lmm_stripe_size,
3121 param->fp_stripe_size,
3122 param->fp_stripe_size_sign,
3123 param->fp_exclude_stripe_size,
3124 param->fp_stripe_size_units, 0);
3129 if (param->fp_check_stripe_count) {
3130 decision = find_value_cmp(
3131 param->fp_lmd->lmd_lmm.lmm_stripe_count,
3132 param->fp_stripe_count,
3133 param->fp_stripe_count_sign,
3134 param->fp_exclude_stripe_count, 1, 0);
3139 if (param->fp_check_layout) {
3142 found = (param->fp_lmd->lmd_lmm.lmm_pattern & param->fp_layout);
3143 if ((param->fp_lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
3144 (found && param->fp_exclude_layout) ||
3145 (!found && !param->fp_exclude_layout)) {
3151 /* If an OBD UUID is specified but none matches, skip this file. */
3152 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
3153 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
3156 /* If an OST or MDT UUID is given, and some OST matches,
3158 if (param->fp_obd_index != OBD_NOT_FOUND ||
3159 param->fp_mdt_index != OBD_NOT_FOUND) {
3160 if (param->fp_obd_uuid) {
3161 if (check_obd_match(param)) {
3162 /* If no mdtuuid is given, we are done.
3163 * Otherwise, fall through to the mdtuuid
3165 if (!param->fp_mdt_uuid)
3172 if (param->fp_mdt_uuid) {
3173 if (check_mdt_match(param))
3180 if (param->fp_check_uid) {
3181 if (st->st_uid == param->fp_uid) {
3182 if (param->fp_exclude_uid)
3185 if (!param->fp_exclude_uid)
3190 if (param->fp_check_gid) {
3191 if (st->st_gid == param->fp_gid) {
3192 if (param->fp_exclude_gid)
3195 if (!param->fp_exclude_gid)
3200 if (param->fp_check_pool) {
3201 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3203 /* empty requested pool is taken as no pool search => V1 */
3204 if (((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
3205 (param->fp_poolname[0] == '\0')) ||
3206 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3207 (strncmp(lmmv3->lmm_pool_name,
3208 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
3209 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3210 (strcmp(param->fp_poolname, "*") == 0))) {
3211 if (param->fp_exclude_pool)
3214 if (!param->fp_exclude_pool)
3219 /* Check the time on mds. */
3221 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3224 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3225 param->fp_lmd->lmd_lmm.lmm_stripe_count)
3227 decision = find_time_check(st, param, for_mds);
3232 /* If file still fits the request, ask ost for updated info.
3233 The regular stat is almost of the same speed as some new
3234 'glimpse-size-ioctl'. */
3236 if (param->fp_check_size && S_ISREG(st->st_mode) &&
3237 param->fp_lmd->lmd_lmm.lmm_stripe_count)
3240 if (param->fp_check_size && S_ISDIR(st->st_mode))
3244 /* For regular files with the stripe the decision may have not
3245 * been taken yet if *time or size is to be checked. */
3246 if (param->fp_obd_index != OBD_NOT_FOUND)
3247 print_failed_tgt(param, path, LL_STATFS_LOV);
3249 if (param->fp_mdt_index != OBD_NOT_FOUND)
3250 print_failed_tgt(param, path, LL_STATFS_LMV);
3253 ret = fstat_f(dirfd(dir), st);
3254 else if (de != NULL)
3255 ret = fstatat_f(dirfd(parent), de->d_name, st,
3256 AT_SYMLINK_NOFOLLOW);
3258 ret = lstat_f(path, st);
3261 if (errno == ENOENT) {
3262 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3263 "warning: %s: %s does not exist",
3268 llapi_error(LLAPI_MSG_ERROR, ret,
3269 "%s: IOC_LOV_GETINFO on %s failed",
3275 /* Check the time on osc. */
3276 decision = find_time_check(st, param, 0);
3281 if (param->fp_check_size)
3282 decision = find_value_cmp(st->st_size, param->fp_size,
3283 param->fp_size_sign,
3284 param->fp_exclude_size,
3285 param->fp_size_units, 0);
3287 if (decision != -1) {
3288 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3289 if (param->fp_zero_end)
3290 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3292 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3296 /* Do not get down anymore? */
3297 if (param->fp_depth == param->fp_max_depth)
3305 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3306 void *param_data, struct dirent64 *de)
3308 struct find_param *param = (struct find_param *)param_data;
3310 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3312 struct obd_ioctl_data data = { 0 };
3317 LASSERT(parent != NULL || dirp != NULL);
3321 if (parent == NULL) {
3322 dir = opendir_parent(path);
3326 llapi_error(LLAPI_MSG_ERROR, ret,
3327 "can not open %s", path);
3334 filename = basename(path);
3335 data.ioc_inlbuf1 = (char *)filename;
3336 data.ioc_inllen1 = strlen(filename) + 1;
3337 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3338 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3339 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3341 llapi_error(LLAPI_MSG_ERROR, ret,
3342 "llapi_obd_statfs: error packing ioctl data");
3346 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3349 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3350 path, strerror(-ret), 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_migrate_mdt(char *path, struct find_param *param)
3381 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3384 int llapi_mv(char *path, struct find_param *param)
3386 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 53, 0)
3387 static bool printed;
3390 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3391 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3395 return llapi_migrate_mdt(path, param);
3398 int llapi_find(char *path, struct find_param *param)
3400 return param_callback(path, cb_find_init, cb_common_fini, param);
3404 * Get MDT number that the file/directory inode referenced
3405 * by the open fd resides on.
3406 * Return 0 and mdtidx on success, or -ve errno.
3408 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3410 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3415 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3416 struct dirent64 *de)
3418 struct find_param *param = (struct find_param *)data;
3419 DIR *d = dirp == NULL ? NULL : *dirp;
3423 LASSERT(parent != NULL || d != NULL);
3426 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3427 } else /* if (parent) */ {
3430 fd = open(path, O_RDONLY | O_NOCTTY);
3432 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3440 if (ret == -ENODATA) {
3441 if (!param->fp_obd_uuid)
3442 llapi_printf(LLAPI_MSG_NORMAL,
3443 "'%s' has no stripe info\n", path);
3445 } else if (ret == -ENOENT) {
3446 llapi_error(LLAPI_MSG_WARN, ret,
3447 "warning: %s: '%s' does not exist",
3450 } else if (ret == -ENOTTY) {
3451 llapi_error(LLAPI_MSG_ERROR, ret,
3452 "%s: '%s' not on a Lustre fs",
3455 llapi_error(LLAPI_MSG_ERROR, ret,
3456 "error: %s: '%s' failed get_mdtidx",
3462 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3463 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3465 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3469 /* Do not go down anymore? */
3470 if (param->fp_depth == param->fp_max_depth)
3478 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3479 struct dirent64 *de)
3481 struct find_param *param = (struct find_param *)data;
3482 DIR *d = dirp == NULL ? NULL : *dirp;
3485 LASSERT(parent != NULL || d != NULL);
3487 if (param->fp_obd_uuid) {
3488 param->fp_quiet = 1;
3489 ret = setup_obd_uuid(d ? d : parent, path, param);
3495 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3496 ret = cb_get_dirstripe(path, d, param);
3498 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3499 (void *)¶m->fp_lmd->lmd_lmm);
3502 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3503 char *fname = strrchr(path, '/');
3504 fname = (fname == NULL ? path : fname + 1);
3506 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3507 param->fp_lum_size);
3509 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3510 (void *)¶m->fp_lmd->lmd_lmm);
3516 if (errno == ENODATA && d != NULL) {
3517 /* We need to "fake" the "use the default" values
3518 * since the lmm struct is zeroed out at this point.
3519 * The magic needs to be set in order to satisfy
3520 * a check later on in the code path.
3521 * The object_seq needs to be set for the "(Default)"
3522 * prefix to be displayed. */
3523 if (param->fp_get_default_lmv) {
3524 struct lmv_user_md *lum = param->fp_lmv_md;
3526 lum->lum_magic = LMV_USER_MAGIC;
3527 lum->lum_stripe_count = 0;
3528 lum->lum_stripe_offset = -1;
3530 } else if (param->fp_get_lmv) {
3531 struct lmv_user_md *lum = param->fp_lmv_md;
3534 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3537 lum->lum_magic = LMV_MAGIC_V1;
3538 lum->lum_stripe_count = 0;
3539 lum->lum_stripe_offset = mdtidx;
3542 struct lov_user_md *lmm =
3543 ¶m->fp_lmd->lmd_lmm;
3545 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3547 ostid_set_seq(&lmm->lmm_oi,
3548 FID_SEQ_LOV_DEFAULT);
3549 lmm->lmm_stripe_count = 0;
3550 lmm->lmm_stripe_size = 0;
3551 lmm->lmm_stripe_offset = -1;
3554 } else if (errno == ENODATA && parent != NULL) {
3555 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
3556 llapi_printf(LLAPI_MSG_NORMAL,
3557 "%s has no stripe info\n", path);
3559 } else if (errno == ENOENT) {
3560 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3561 "warning: %s: %s does not exist",
3564 } else if (errno == ENOTTY) {
3566 llapi_error(LLAPI_MSG_ERROR, ret,
3567 "%s: '%s' not on a Lustre fs?",
3572 llapi_error(LLAPI_MSG_ERROR, ret,
3573 "error: %s: %s failed for %s",
3574 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3575 "IOC_MDC_GETFILESTRIPE", path);
3582 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
3583 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3586 /* Do not get down anymore? */
3587 if (param->fp_depth == param->fp_max_depth)
3595 int llapi_getstripe(char *path, struct find_param *param)
3597 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
3598 cb_get_mdt_index : cb_getstripe,
3599 cb_common_fini, param);
3602 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3603 struct obd_statfs *stat_buf,
3604 struct obd_uuid *uuid_buf)
3607 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3609 struct obd_ioctl_data data = { 0 };
3612 data.ioc_inlbuf1 = (char *)&type;
3613 data.ioc_inllen1 = sizeof(__u32);
3614 data.ioc_inlbuf2 = (char *)&index;
3615 data.ioc_inllen2 = sizeof(__u32);
3616 data.ioc_pbuf1 = (char *)stat_buf;
3617 data.ioc_plen1 = sizeof(struct obd_statfs);
3618 data.ioc_pbuf2 = (char *)uuid_buf;
3619 data.ioc_plen2 = sizeof(struct obd_uuid);
3621 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3623 llapi_error(LLAPI_MSG_ERROR, rc,
3624 "llapi_obd_statfs: error packing ioctl data");
3628 fd = open(path, O_RDONLY);
3629 if (errno == EISDIR)
3630 fd = open(path, O_DIRECTORY | O_RDONLY);
3633 rc = errno ? -errno : -EBADF;
3634 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3636 /* If we can't even open a file on the filesystem (e.g. with
3637 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3640 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3642 rc = errno ? -errno : -EINVAL;
3648 #define MAX_STRING_SIZE 128
3650 int llapi_ping(char *obd_type, char *obd_name)
3652 char path[MAX_STRING_SIZE];
3656 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3657 obd_type, obd_name);
3659 fd = open(path, O_WRONLY);
3662 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3666 /* The purpose is to send a byte as a ping, whatever this byte is. */
3667 /* coverity[uninit_use_in_call] */
3668 rc = write(fd, buf, 1);
3678 int llapi_target_iterate(int type_num, char **obd_type,
3679 void *args, llapi_cb_t cb)
3681 char buf[MAX_STRING_SIZE];
3682 FILE *fp = fopen(DEVICES_LIST, "r");
3687 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3691 while (fgets(buf, sizeof(buf), fp) != NULL) {
3692 char *obd_type_name = NULL;
3693 char *obd_name = NULL;
3694 char *obd_uuid = NULL;
3696 struct obd_statfs osfs_buffer;
3698 while(bufp[0] == ' ')
3701 for(i = 0; i < 3; i++) {
3702 obd_type_name = strsep(&bufp, " ");
3704 obd_name = strsep(&bufp, " ");
3705 obd_uuid = strsep(&bufp, " ");
3707 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3709 for (i = 0; i < type_num; i++) {
3710 if (strcmp(obd_type_name, obd_type[i]) != 0)
3713 cb(obd_type_name, obd_name, obd_uuid, args);
3720 static void do_target_check(char *obd_type_name, char *obd_name,
3721 char *obd_uuid, void *args)
3725 rc = llapi_ping(obd_type_name, obd_name);
3726 if (rc == ENOTCONN) {
3727 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3729 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3731 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3735 int llapi_target_check(int type_num, char **obd_type, char *dir)
3737 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3740 #undef MAX_STRING_SIZE
3742 /* Is this a lustre fs? */
3743 int llapi_is_lustre_mnttype(const char *type)
3745 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3748 /* Is this a lustre client fs? */
3749 int llapi_is_lustre_mnt(struct mntent *mnt)
3751 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3752 strstr(mnt->mnt_fsname, ":/") != NULL);
3755 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3760 root = opendir(mnt);
3763 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3767 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3778 #include <sys/wait.h>
3782 static int rmtacl_notify(int ops)
3786 int found = 0, fd = 0, rc = 0;
3788 fp = setmntent(MOUNTED, "r");
3791 llapi_error(LLAPI_MSG_ERROR, rc,
3792 "error setmntent(%s)", MOUNTED);
3797 mnt = getmntent(fp);
3801 if (!llapi_is_lustre_mnt(mnt))
3804 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3807 llapi_error(LLAPI_MSG_ERROR, rc,
3808 "Can't open '%s'", mnt->mnt_dir);
3812 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3816 llapi_error(LLAPI_MSG_ERROR, rc,
3817 "ioctl RMTACL on '%s' err %d",
3827 return ((rc != 0) ? rc : found);
3830 static char *next_token(char *p, int div)
3836 while (*p && *p != ':' && !isspace(*p))
3839 while (*p == ':' || isspace(*p))
3842 return *p ? p : NULL;
3845 static int rmtacl_name2id(char *name, int is_user)
3850 pw = getpwnam(name);
3854 return (int)(pw->pw_uid);
3858 gr = getgrnam(name);
3862 return (int)(gr->gr_gid);
3866 static int isodigit(int c)
3868 return (c >= '0' && c <= '7') ? 1 : 0;
3872 * Whether the name is just digits string (uid/gid) already or not.
3877 static int str_is_id(char *str)
3884 if (*str == 'x' || *str == 'X') { /* for Hex. */
3885 if (!isxdigit(*(++str)))
3888 while (isxdigit(*(++str)));
3889 } else if (isodigit(*str)) { /* for Oct. */
3890 while (isodigit(*(++str)));
3892 } else if (isdigit(*str)) { /* for Dec. */
3893 while (isdigit(*(++str)));
3896 return (*str == 0) ? 1 : 0;
3906 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3908 static rmtacl_name_t rmtacl_namelist[] = {
3909 { RMTACL_OPTNAME("user:"), 1, 0 },
3910 { RMTACL_OPTNAME("group:"), 0, 0 },
3911 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3912 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3913 /* for --tabular option */
3914 { RMTACL_OPTNAME("user"), 1, 1 },
3915 { RMTACL_OPTNAME("group"), 0, 1 },
3919 static int rgetfacl_output(char *str)
3921 char *start = NULL, *end = NULL;
3922 int is_user = 0, n, id;
3929 for (rn = rmtacl_namelist; rn->name; rn++) {
3930 if(strncmp(str, rn->name, rn->length) == 0) {
3931 if (!rn->next_token)
3932 start = str + rn->length;
3934 start = next_token(str + rn->length, 0);
3935 is_user = rn->is_user;
3940 end = next_token(start, 1);
3941 if (end == NULL || start == end) {
3942 n = printf("%s", str);
3948 id = rmtacl_name2id(start, is_user);
3949 if (id == INVALID_ID) {
3950 if (str_is_id(start)) {
3952 n = printf("%s", str);
3955 } else if ((id == NOBODY_UID && is_user) ||
3956 (id == NOBODY_GID && !is_user)) {
3958 n = printf("%s", str);
3962 n = printf("%s%d%s", str, id, end);
3967 static int child_status(int status)
3969 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3972 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3975 int fd[2], status, rc;
3982 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe");
3989 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork");
3994 /* child process redirects its output. */
3997 if (dup2(fd[1], 1) < 0) {
3999 llapi_error(LLAPI_MSG_ERROR, rc,
4000 "Can't dup2 %d", fd[1]);
4010 status = rmtacl_notify(ops);
4014 exit(execvp(argv[0], argv));
4017 /* the following is parent process */
4018 fp = fdopen(fd[0], "r");
4021 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed", fd[0]);
4027 while (fgets(buf, PIPE_BUF, fp) != NULL) {
4028 if (output_func(buf) < 0)
4029 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
4035 if (waitpid(pid, &status, 0) < 0) {
4037 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed", pid);
4041 return child_status(status);
4044 int llapi_lsetfacl(int argc, char *argv[])
4046 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
4049 int llapi_lgetfacl(int argc, char *argv[])
4051 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
4054 int llapi_rsetfacl(int argc, char *argv[])
4056 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
4059 int llapi_rgetfacl(int argc, char *argv[])
4061 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
4064 int llapi_cp(int argc, char *argv[])
4068 rc = rmtacl_notify(RMT_RSETFACL);
4072 exit(execvp(argv[0], argv));
4075 int llapi_ls(int argc, char *argv[])
4079 rc = rmtacl_notify(RMT_LGETFACL);
4083 exit(execvp(argv[0], argv));
4086 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4087 * format must have %s%s, buf must be > 16
4088 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4089 * then buf = "lustre-MDT0000"
4091 static int get_mdtname(char *name, char *format, char *buf)
4093 char suffix[]="-MDT0000";
4094 int len = strlen(name);
4096 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4097 name[len - 5] = '\0';
4102 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4105 /* Not enough room to add suffix */
4106 llapi_err_noerrno(LLAPI_MSG_ERROR,
4107 "MDT name too long |%s|", name);
4112 return sprintf(buf, format, name, suffix);
4115 /** ioctl on filsystem root, with mdtindex sent as data
4116 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4117 * \param mdtidxp pointer to integer within data to be filled in with the
4118 * mdt index (0 if no mdt is specified). NULL won't be filled.
4120 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4128 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4129 Open root and parse mdt index. */
4130 if (mdtname[0] == '/') {
4132 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4133 (char *)mdtname, -1);
4135 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4137 ptr = fsname + strlen(fsname) - 8;
4139 index = strtol(ptr + 4, NULL, 10);
4140 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4144 llapi_err_noerrno(LLAPI_MSG_ERROR,
4145 "Can't open %s: %d\n", mdtname, rc);
4152 rc = ioctl(fd, opc, data);
4157 if (rc && want_error)
4158 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
4164 /****** Changelog API ********/
4166 static int changelog_ioctl(const char *mdtname, int opc, int id,
4167 long long recno, int flags)
4169 struct ioc_changelog data;
4173 data.icc_recno = recno;
4174 data.icc_flags = flags;
4175 idx = (int *)(&data.icc_mdtindex);
4177 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
4180 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
4181 struct changelog_private {
4183 enum changelog_send_flag flags;
4184 struct lustre_kernelcomm kuc;
4187 /** Start reading from a changelog
4188 * @param priv Opaque private control structure
4189 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
4190 * @param device Report changes recorded on this MDT
4191 * @param startrec Report changes beginning with this record number
4192 * (just call llapi_changelog_fini when done; don't need an endrec)
4194 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
4195 const char *device, long long startrec)
4197 struct changelog_private *cp;
4201 /* Set up the receiver control struct */
4202 cp = calloc(1, sizeof(*cp));
4206 cp->magic = CHANGELOG_PRIV_MAGIC;
4209 /* Set up the receiver */
4210 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */, 0);
4216 /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
4217 * warning if it's missing. */
4218 if (!(flags & CHANGELOG_FLAG_JOBID) && !warned) {
4219 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
4220 "w/o CHANGELOG_FLAG_JOBID", __func__);
4224 /* Tell the kernel to start sending */
4225 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
4227 /* Only the kernel reference keeps the write side open */
4228 close(cp->kuc.lk_wfd);
4229 cp->kuc.lk_wfd = LK_NOFD;
4231 /* frees and clears priv */
4232 llapi_changelog_fini(priv);
4243 /** Finish reading from a changelog */
4244 int llapi_changelog_fini(void **priv)
4246 struct changelog_private *cp = (struct changelog_private *)*priv;
4248 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4251 libcfs_ukuc_stop(&cp->kuc);
4258 * Convert all records to a same format according to the caller's wishes.
4259 * Default is CLF_VERSION | CLF_RENAME.
4260 * Add CLF_JOBID if explicitely requested.
4262 * \param rec The record to remap. It is expected to be big enough to
4263 * properly handle the final format.
4264 * \return 1 if anything changed. 0 otherwise.
4266 /** Read the next changelog entry
4267 * @param priv Opaque private control structure
4268 * @param rech Changelog record handle; record will be allocated here
4269 * @return 0 valid message received; rec is set
4273 #define DEFAULT_RECORD_FMT (CLF_VERSION | CLF_RENAME)
4274 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
4276 struct changelog_private *cp = (struct changelog_private *)priv;
4277 struct kuc_hdr *kuch;
4278 enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
4281 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4285 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4289 if (cp->flags & CHANGELOG_FLAG_JOBID)
4290 rec_fmt |= CLF_JOBID;
4293 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4294 KUC_CHANGELOG_MSG_MAXSIZE,
4295 KUC_TRANSPORT_CHANGELOG);
4299 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4300 ((kuch->kuc_msgtype != CL_RECORD) &&
4301 (kuch->kuc_msgtype != CL_EOF))) {
4302 llapi_err_noerrno(LLAPI_MSG_ERROR,
4303 "Unknown changelog message type %d:%d\n",
4304 kuch->kuc_transport, kuch->kuc_msgtype);
4309 if (kuch->kuc_msgtype == CL_EOF) {
4310 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4319 /* Our message is a changelog_rec. Use pointer math to skip
4320 * kuch_hdr and point directly to the message payload. */
4321 *rech = (struct changelog_rec *)(kuch + 1);
4322 changelog_remap_rec(*rech, rec_fmt);
4332 /** Release the changelog record when done with it. */
4333 int llapi_changelog_free(struct changelog_rec **rech)
4336 /* We allocated memory starting at the kuc_hdr, but passed
4337 * the consumer a pointer to the payload.
4338 * Use pointer math to get back to the header.
4340 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4347 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4353 llapi_err_noerrno(LLAPI_MSG_ERROR,
4354 "can't purge negative records\n");
4358 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4359 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4360 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4361 llapi_err_noerrno(LLAPI_MSG_ERROR,
4362 "expecting id of the form '"
4363 CHANGELOG_USER_PREFIX
4364 "<num>'; got '%s'\n", idstr);
4368 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4371 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4372 int buflen, long long *recno, int *linkno)
4375 struct getinfo_fid2path *gf;
4378 while (*fidstr == '[')
4381 sscanf(fidstr, SFID, RFID(&fid));
4382 if (!fid_is_sane(&fid)) {
4383 llapi_err_noerrno(LLAPI_MSG_ERROR,
4384 "bad FID format [%s], should be "DFID"\n",
4385 fidstr, (__u64)1, 2, 0);
4389 gf = malloc(sizeof(*gf) + buflen);
4393 gf->gf_recno = *recno;
4394 gf->gf_linkno = *linkno;
4395 gf->gf_pathlen = buflen;
4397 /* Take path or fsname */
4398 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4401 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4403 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4404 if (buf[0] == '\0') { /* ROOT path */
4408 *recno = gf->gf_recno;
4409 *linkno = gf->gf_linkno;
4416 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4419 struct lustre_mdt_attrs *lma;
4423 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4425 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4428 lma = (struct lustre_mdt_attrs *)buf;
4429 fid_le_to_cpu(fid, &lma->lma_self_fid);
4433 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4438 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4447 int llapi_fd2fid(const int fd, lustre_fid *fid)
4451 memset(fid, 0, sizeof(*fid));
4453 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4454 if (rc == -EINVAL || rc == -ENOTTY)
4455 rc = fid_from_lma(NULL, fd, fid);
4460 int llapi_path2fid(const char *path, lustre_fid *fid)
4464 memset(fid, 0, sizeof(*fid));
4465 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4467 if (errno == ELOOP || errno == ENXIO)
4468 return fid_from_lma(path, -1, fid);
4472 rc = llapi_fd2fid(fd, fid);
4473 if (rc == -EINVAL || rc == -ENOTTY)
4474 rc = fid_from_lma(path, -1, fid);
4480 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
4481 char *name, size_t name_size)
4483 struct getparent *gp;
4486 gp = malloc(sizeof(*gp) + name_size);
4490 gp->gp_linkno = linkno;
4491 gp->gp_name_size = name_size;
4493 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
4499 *parent_fid = gp->gp_fid;
4501 strncpy(name, gp->gp_name, name_size);
4502 name[name_size - 1] = '\0';
4509 int llapi_path2parent(const char *path, unsigned int linkno,
4510 lustre_fid *parent_fid, char *name, size_t name_size)
4515 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4519 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4524 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4529 root = opendir(mnt);
4532 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4536 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4539 llapi_error(LLAPI_MSG_ERROR, rc,
4540 "ioctl on %s for getting connect flags failed", mnt);
4546 int llapi_get_version(char *buffer, int buffer_size,
4551 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4553 fd = open(OBD_DEV_PATH, O_RDONLY);
4557 memset(buffer, 0, buffer_size);
4558 data->ioc_version = OBD_IOCTL_VERSION;
4559 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4560 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4561 data->ioc_len = obd_ioctl_packlen(data);
4563 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4570 *version = data->ioc_bulk;
4575 * Get a 64-bit value representing the version of file data pointed by fd.
4577 * Each write or truncate, flushed on OST, will change this value. You can use
4578 * this value to verify if file data was modified. This only checks the file
4579 * data, not metadata.
4581 * \param flags 0: no flush pages, usually used it the process has already
4583 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4585 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4586 * pages from clients.
4588 * \retval 0 on success.
4589 * \retval -errno on error.
4591 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4594 struct ioc_data_version idv;
4596 idv.idv_flags = flags;
4598 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4602 *data_version = idv.idv_version;
4608 * Create a file without any name open it for read/write
4610 * - file is created as if it were a standard file in the given \a directory
4611 * - file does not appear in \a directory and mtime does not change because
4612 * the filename is handled specially by the Lustre MDS.
4613 * - file is removed at final close
4614 * - file modes are rw------- since it doesn't make sense to have a read-only
4615 * or write-only file that cannot be opened again.
4616 * - if user wants another mode it must use fchmod() on the open file, no
4617 * security problems arise because it cannot be opened by another process.
4619 * \param[in] directory directory from which to inherit layout/MDT idx
4620 * \param[in] idx MDT index on which the file is created,
4621 * \a idx == -1 means no specific MDT is requested
4622 * \param[in] open_flags standard open(2) flags
4624 * \retval 0 on success.
4625 * \retval -errno on error.
4627 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4629 char file_path[PATH_MAX];
4630 char filename[PATH_MAX];
4637 snprintf(filename, sizeof(filename),
4638 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4640 snprintf(filename, sizeof(filename),
4641 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4643 rc = snprintf(file_path, sizeof(file_path),
4644 "%s/%s", directory, filename);
4645 if (rc >= sizeof(file_path))
4648 fd = open(file_path, O_RDWR | O_CREAT | open_flags, S_IRUSR | S_IWUSR);
4650 llapi_error(LLAPI_MSG_ERROR, errno,
4651 "Cannot create volatile file '%s' in '%s'",
4652 filename + LUSTRE_VOLATILE_HDR_LEN,
4656 /* unlink file in case this wasn't a Lustre filesystem, and the
4657 * magic volatile filename wasn't handled as intended. The effect
4665 * Swap the layouts between 2 file descriptors
4666 * the 2 files must be open for writing
4667 * first fd received the ioctl, second fd is passed as arg
4668 * this is assymetric but avoid use of root path for ioctl
4670 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4671 int gid, __u64 flags)
4673 struct lustre_swap_layouts lsl;
4678 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4679 rc = fstat(fd1, &st1);
4683 rc = fstat(fd2, &st2);
4688 lsl.sl_flags = flags;
4692 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4696 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4697 struct timeval tv1[2];
4698 struct timeval tv2[2];
4700 memset(tv1, 0, sizeof(tv1));
4701 memset(tv2, 0, sizeof(tv2));
4703 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4704 tv1[0].tv_sec = st1.st_atime;
4705 tv2[0].tv_sec = st2.st_atime;
4707 tv1[0].tv_sec = st2.st_atime;
4708 tv2[0].tv_sec = st1.st_atime;
4711 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4712 tv1[1].tv_sec = st1.st_mtime;
4713 tv2[1].tv_sec = st2.st_mtime;
4715 tv1[1].tv_sec = st2.st_mtime;
4716 tv2[1].tv_sec = st1.st_mtime;
4719 rc = futimes(fd1, tv1);
4723 rc = futimes(fd2, tv2);
4731 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4738 while (grp_id == 0);
4740 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4748 * Swap the layouts between 2 files
4749 * the 2 files are open in write
4751 int llapi_swap_layouts(const char *path1, const char *path2,
4752 __u64 dv1, __u64 dv2, __u64 flags)
4756 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4759 llapi_error(LLAPI_MSG_ERROR, rc,
4760 "error: cannot open '%s' for write", path1);
4764 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4767 llapi_error(LLAPI_MSG_ERROR, rc,
4768 "error: cannot open '%s' for write", path2);
4772 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4774 llapi_error(LLAPI_MSG_ERROR, rc,
4775 "error: cannot swap layout between '%s' and '%s'",
4786 * Attempt to open a file with Lustre file identifier \a fid
4787 * and return an open file descriptor.
4789 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4790 * \param[in] fid Lustre file identifier of file to open
4791 * \param[in] flags open() flags
4793 * \retval non-negative file descriptor on successful open
4794 * \retval -1 if an error occurred
4796 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4798 char mntdir[PATH_MAX];
4799 char path[PATH_MAX];
4802 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4806 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4807 return open(path, flags);
4813 * \param fd File to lock.
4814 * \param gid Group Identifier.
4816 * \retval 0 on success.
4817 * \retval -errno on failure.
4819 int llapi_group_lock(int fd, int gid)
4823 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4826 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4834 * \param fd File to unlock.
4835 * \param gid Group Identifier.
4837 * \retval 0 on success.
4838 * \retval -errno on failure.
4840 int llapi_group_unlock(int fd, int gid)
4844 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4847 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");