Whamcloud - gitweb
3b6be2c35174f0858ed57ed111b4c75e7d120bd3
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/liblustreapi.c
32  *
33  * Author: Peter J. Braam <braam@clusterfs.com>
34  * Author: Phil Schwan <phil@clusterfs.com>
35  * Author: Robert Read <rread@clusterfs.com>
36  */
37
38 /* for O_DIRECTORY */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 #include <ctype.h>
44 #include <mntent.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <stddef.h>
49 #include <sys/ioctl.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <dirent.h>
54 #include <stdarg.h>
55 #include <sys/stat.h>
56 #include <sys/statfs.h>
57 #include <sys/syscall.h>
58 #include <sys/time.h>
59 #include <sys/types.h>
60 #include <sys/xattr.h>
61 #include <sys/sysmacros.h>
62 #include <time.h>
63 #include <fnmatch.h>
64 #include <libgen.h> /* for dirname() */
65 #include <linux/limits.h>
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
68 #else
69 #include <unistd.h>
70 #endif
71 #include <poll.h>
72 #include <time.h>
73 #include <inttypes.h>
74
75 #include <libcfs/util/ioctl.h>
76 #include <libcfs/util/param.h>
77 #include <libcfs/util/string.h>
78 #include <linux/lnet/lnetctl.h>
79 #include <lustre/lustreapi.h>
80 #include <linux/lustre/lustre_ostid.h>
81 #include <linux/lustre/lustre_ioctl.h>
82 #include "lustreapi_internal.h"
83 #include "lstddef.h"
84
85 #define FORMATTED_BUF_LEN       1024
86
87 static int llapi_msg_level = LLAPI_MSG_MAX;
88 const char *liblustreapi_cmd;
89
90 struct lustre_foreign_type lu_foreign_types[] = {
91         {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
92         {.lft_type = LU_FOREIGN_TYPE_SYMLINK, .lft_name = "symlink"},
93         /* must be the last element */
94         {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
95         /* array max dimension must be <= UINT32_MAX */
96 };
97
98 void llapi_msg_set_level(int level)
99 {
100         /* ensure level is in the good range */
101         if (level < LLAPI_MSG_OFF)
102                 llapi_msg_level = LLAPI_MSG_OFF;
103         else if (level > LLAPI_MSG_MAX)
104                 llapi_msg_level = LLAPI_MSG_MAX;
105         else
106                 llapi_msg_level = level;
107 }
108
109 int llapi_msg_get_level(void)
110 {
111         return llapi_msg_level;
112 }
113
114 void llapi_set_command_name(const char *cmd)
115 {
116         liblustreapi_cmd = cmd;
117 }
118
119 void llapi_clear_command_name(void)
120 {
121         liblustreapi_cmd = NULL;
122 }
123
124 static void error_callback_default(enum llapi_message_level level, int err,
125                                    const char *fmt, va_list ap)
126 {
127         bool has_nl = strchr(fmt, '\n') != NULL;
128
129         if (liblustreapi_cmd != NULL)
130                 fprintf(stderr, "%s %s: ", program_invocation_short_name,
131                         liblustreapi_cmd);
132         else
133                 fprintf(stderr, "%s: ", program_invocation_short_name);
134
135
136         if (level & LLAPI_MSG_NO_ERRNO) {
137                 vfprintf(stderr, fmt, ap);
138                 if (!has_nl)
139                         fprintf(stderr, "\n");
140         } else {
141                 char *newfmt;
142
143                 /*
144                  * Remove trailing linefeed so error string can be appended.
145                  * @fmt is a const string, so we can't modify it directly.
146                  */
147                 if (has_nl && (newfmt = strdup(fmt)))
148                         *strrchr(newfmt, '\n') = '\0';
149                 else
150                         newfmt = (char *)fmt;
151
152                 vfprintf(stderr, newfmt, ap);
153                 if (newfmt != fmt)
154                         free(newfmt);
155                 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
156         }
157 }
158
159 static void info_callback_default(enum llapi_message_level level, int err,
160                                   const char *fmt, va_list ap)
161 {
162         if (err != 0) {
163                 if (liblustreapi_cmd != NULL) {
164                         fprintf(stdout, "%s %s: ",
165                                 program_invocation_short_name,
166                                 liblustreapi_cmd);
167                 } else {
168                         fprintf(stdout, "%s: ", program_invocation_short_name);
169                 }
170         }
171         vfprintf(stdout, fmt, ap);
172 }
173
174 static llapi_log_callback_t llapi_error_callback = error_callback_default;
175 static llapi_log_callback_t llapi_info_callback = info_callback_default;
176
177
178 /* llapi_error will preserve errno */
179 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
180 {
181         va_list  args;
182         int      tmp_errno = errno;
183
184         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
185                 return;
186
187         va_start(args, fmt);
188         llapi_error_callback(level, abs(err), fmt, args);
189         va_end(args);
190         errno = tmp_errno;
191 }
192
193 /* llapi_printf will preserve errno */
194 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
195 {
196         va_list  args;
197         int      tmp_errno = errno;
198
199         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
200                 return;
201
202         va_start(args, fmt);
203         llapi_info_callback(level, 0, fmt, args);
204         va_end(args);
205         errno = tmp_errno;
206 }
207
208 /**
209  * Set a custom error logging function. Passing in NULL will reset the logging
210  * callback to its default value.
211  *
212  * This function returns the value of the old callback.
213  */
214 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
215 {
216         llapi_log_callback_t    old = llapi_error_callback;
217
218         if (cb != NULL)
219                 llapi_error_callback = cb;
220         else
221                 llapi_error_callback = error_callback_default;
222
223         return old;
224 }
225
226 /**
227  * Set a custom info logging function. Passing in NULL will reset the logging
228  * callback to its default value.
229  *
230  * This function returns the value of the old callback.
231  */
232 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
233 {
234         llapi_log_callback_t    old = llapi_info_callback;
235
236         if (cb != NULL)
237                 llapi_info_callback = cb;
238         else
239                 llapi_info_callback = info_callback_default;
240
241         return old;
242 }
243
244 /**
245  * size_units is to be initialized (or zeroed) by caller.
246  */
247 int llapi_parse_size(const char *optarg, unsigned long long *size,
248                      unsigned long long *size_units, int bytes_spec)
249 {
250         char *end;
251         char *argbuf = (char *)optarg;
252         unsigned long long frac = 0, frac_d = 1;
253
254         if (strncmp(optarg, "-", 1) == 0)
255                 return -1;
256
257         if (*size_units == 0)
258                 *size_units = 1;
259
260         *size = strtoull(argbuf, &end, 0);
261         if (end != NULL && *end == '.') {
262                 int i;
263
264                 argbuf = end + 1;
265                 frac = strtoull(argbuf, &end, 10);
266                 /* count decimal places */
267                 for (i = 0; i < (end - argbuf); i++)
268                         frac_d *= 10;
269         }
270
271         if (*end != '\0') {
272                 if ((*end == 'b') && *(end + 1) == '\0' &&
273                     (*size & (~0ULL << (64 - 9))) == 0 &&
274                     !bytes_spec) {
275                         *size_units = 1 << 9;
276                 } else if ((*end == 'b') &&
277                            *(end + 1) == '\0' &&
278                            bytes_spec) {
279                         *size_units = 1;
280                 } else if ((*end == 'k' || *end == 'K') &&
281                            *(end + 1) == '\0' &&
282                            (*size & (~0ULL << (64 - 10))) == 0) {
283                         *size_units = 1 << 10;
284                 } else if ((*end == 'm' || *end == 'M') &&
285                            *(end + 1) == '\0' &&
286                            (*size & (~0ULL << (64 - 20))) == 0) {
287                         *size_units = 1 << 20;
288                 } else if ((*end == 'g' || *end == 'G') &&
289                            *(end + 1) == '\0' &&
290                            (*size & (~0ULL << (64 - 30))) == 0) {
291                         *size_units = 1 << 30;
292                 } else if ((*end == 't' || *end == 'T') &&
293                            *(end + 1) == '\0' &&
294                            (*size & (~0ULL << (64 - 40))) == 0) {
295                         *size_units = 1ULL << 40;
296                 } else if ((*end == 'p' || *end == 'P') &&
297                            *(end + 1) == '\0' &&
298                            (*size & (~0ULL << (64 - 50))) == 0) {
299                         *size_units = 1ULL << 50;
300                 } else if ((*end == 'e' || *end == 'E') &&
301                            *(end + 1) == '\0' &&
302                            (*size & (~0ULL << (64 - 60))) == 0) {
303                         *size_units = 1ULL << 60;
304                 } else {
305                         return -1;
306                 }
307         }
308         *size = *size * *size_units + frac * *size_units / frac_d;
309
310         return 0;
311 }
312
313 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
314 {
315         struct obd_ioctl_data *overlay;
316         char *ptr;
317
318         data->ioc_len = obd_ioctl_packlen(data);
319         data->ioc_version = OBD_IOCTL_VERSION;
320
321         if (*pbuf != NULL && data->ioc_len > max_len) {
322                 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
323                             "pbuf = %p, ioc_len = %u, max_len = %d",
324                             *pbuf, data->ioc_len, max_len);
325                 return -EINVAL;
326         }
327
328         if (*pbuf == NULL)
329                 *pbuf = malloc(data->ioc_len);
330
331         if (*pbuf == NULL)
332                 return -ENOMEM;
333
334         overlay = (struct obd_ioctl_data *)*pbuf;
335         memcpy(*pbuf, data, sizeof(*data));
336
337         ptr = overlay->ioc_bulk;
338         if (data->ioc_inlbuf1) {
339                 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
340                 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
341         }
342
343         if (data->ioc_inlbuf2) {
344                 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
345                 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
346         }
347
348         if (data->ioc_inlbuf3) {
349                 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
350                 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
351         }
352
353         if (data->ioc_inlbuf4) {
354                 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
355                 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
356         }
357
358         return 0;
359 }
360
361 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
362 {
363         struct obd_ioctl_data *overlay;
364         char *ptr;
365
366         if (pbuf == NULL)
367                 return 1;
368
369         overlay = (struct obd_ioctl_data *)pbuf;
370
371         /* Preserve the caller's buffer pointers */
372         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
373         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
374         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
375         overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
376
377         memcpy(data, pbuf, sizeof(*data));
378
379         ptr = overlay->ioc_bulk;
380         if (data->ioc_inlbuf1) {
381                 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
382                 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
383         }
384
385         if (data->ioc_inlbuf2) {
386                 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
387                 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
388         }
389
390         if (data->ioc_inlbuf3) {
391                 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
392                 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
393         }
394
395         if (data->ioc_inlbuf4) {
396                 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
397                 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
398         }
399
400         return 0;
401 }
402
403 /* XXX: llapi_xxx() functions return negative values upon failure */
404
405 int llapi_layout_search_ost(__u32 ost, char *pname, char *fsname)
406 {
407         char ostname[MAX_OBD_NAME + 64];
408         char *pool_name = pname;
409         int rc = 0;
410
411         /**
412          * The current policy is that the pool does not have to exist at the
413          * setstripe time, see sanity-pfl/-flr tests.
414          * If this logic will change, re-enable it.
415          *
416          * if (pname && strlen(pname) == 0)
417          */
418                 pool_name = NULL;
419
420         snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
421                  fsname, ost);
422         rc = llapi_search_ost(fsname, pool_name, ostname);
423         if (rc <= 0) {
424                 if (rc == 0)
425                         rc = -ENODEV;
426
427                 llapi_error(LLAPI_MSG_ERROR, rc,
428                             "%s: cannot find OST %s in %s", __func__, ostname,
429                             pool_name != NULL ? "pool" : "system");
430                 return rc;
431         }
432
433         return 0;
434 }
435
436 /**
437  * Verify the setstripe parameters before using.
438  * This is a pair method for comp_args_to_layout()/llapi_layout_sanity_cb()
439  * when just 1 component or a non-PFL layout is given.
440  *
441  * \param[in] param             stripe parameters
442  * \param[in] pool_name         pool name
443  * \param[in] fsname            lustre FS name
444  *
445  * \retval                      0, success
446  *                              < 0, error code on failre
447  */
448 static int llapi_stripe_param_verify(const struct llapi_stripe_param *param,
449                                      char **pool_name,
450                                      char *fsname)
451 {
452         int count;
453         static int page_size;
454         int rc = 0;
455
456         if (page_size == 0) {
457                 /*
458                  * 64 KB is the largest common page size (on ia64/PPC/ARM),
459                  * but check the local page size just in case. The page_size
460                  * will not change for the lifetime of this process at least.
461                  */
462                 page_size = LOV_MIN_STRIPE_SIZE;
463                 if (getpagesize() > page_size) {
464                         page_size = getpagesize();
465                         llapi_err_noerrno(LLAPI_MSG_WARN,
466                                           "warning: page size (%u) larger than expected (%u)",
467                                           page_size, LOV_MIN_STRIPE_SIZE);
468                 }
469         }
470         if (!llapi_stripe_size_is_aligned(param->lsp_stripe_size)) {
471                 rc = -EINVAL;
472                 llapi_error(LLAPI_MSG_ERROR, rc,
473                             "error: bad stripe_size %llu, must be an even multiple of %d bytes",
474                             param->lsp_stripe_size, page_size);
475                 goto out;
476         }
477         if (!llapi_stripe_index_is_valid(param->lsp_stripe_offset)) {
478                 rc = -EINVAL;
479                 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
480                             param->lsp_stripe_offset);
481                 goto out;
482         }
483         if (llapi_stripe_size_is_too_big(param->lsp_stripe_size)) {
484                 rc = -EINVAL;
485                 llapi_error(LLAPI_MSG_ERROR, rc,
486                             "error: stripe size '%llu' over 4GB limit",
487                             param->lsp_stripe_size);
488                 goto out;
489         }
490
491         count = param->lsp_stripe_count;
492         if (param->lsp_stripe_pattern == LOV_PATTERN_MDT) {
493                 rc = -EINVAL;
494                 llapi_error(LLAPI_MSG_ERROR, rc,
495                             "Invalid pattern: %d, must be specified with -E\n",
496                             param->lsp_stripe_pattern);
497                 goto out;
498         } else {
499                 if (!llapi_stripe_count_is_valid(count)) {
500                         rc = -EINVAL;
501                         llapi_error(LLAPI_MSG_ERROR, rc,
502                                     "Invalid stripe count %d\n", count);
503                         goto out;
504                 }
505         }
506
507         /* Make sure we have a good pool */
508         if (*pool_name != NULL) {
509                 if (!llapi_pool_name_is_valid(pool_name, fsname)) {
510                         rc = -EINVAL;
511                         llapi_error(LLAPI_MSG_ERROR, rc,
512                                     "Pool '%s' is not on filesystem '%s'",
513                                     *pool_name, fsname);
514                         goto out;
515                 }
516
517                 /* Make sure the pool exists and is non-empty */
518                 rc = llapi_search_ost(fsname, *pool_name, NULL);
519                 if (rc < 1) {
520                         char *err = rc == 0 ? "has no OSTs" : "does not exist";
521
522                         rc = -EINVAL;
523                         llapi_error(LLAPI_MSG_ERROR, rc, "pool '%s.%s' %s",
524                                     fsname, *pool_name, err);
525                         goto out;
526                 }
527                 rc = 0;
528         }
529
530         /* sanity check of target list */
531         if (param->lsp_is_specific) {
532                 bool found = false;
533                 int i;
534
535                 for (i = 0; i < count; i++) {
536                         rc = llapi_layout_search_ost(param->lsp_osts[i],
537                                                      *pool_name, fsname);
538                         if (rc)
539                                 goto out;
540
541                         /* Make sure stripe offset is in OST list. */
542                         if (param->lsp_osts[i] == param->lsp_stripe_offset)
543                                 found = true;
544                 }
545                 if (!found) {
546                         rc = -EINVAL;
547                         llapi_error(LLAPI_MSG_ERROR, rc,
548                                     "%s: stripe offset '%d' is not in the target list",
549                                     __func__, param->lsp_stripe_offset);
550                         goto out;
551                 }
552         } else if (param->lsp_stripe_offset != -1) {
553                 rc = llapi_layout_search_ost(param->lsp_stripe_offset,
554                                              *pool_name, fsname);
555                 if (rc)
556                         goto out;
557         }
558 out:
559         errno = -rc;
560         return rc;
561 }
562
563 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
564                                  int hash_type)
565 {
566         int rc;
567
568         if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
569                 rc = -EINVAL;
570                 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
571                                 stripe_offset);
572                 return rc;
573         }
574         if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
575                 rc = -EINVAL;
576                 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
577                                 stripe_count);
578                 return rc;
579         }
580
581         if (!llapi_dir_hash_type_is_valid(hash_type)) {
582                 rc = -EINVAL;
583                 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
584                                 hash_type);
585                 return rc;
586         }
587         return 0;
588 }
589
590 /*
591  * Trim a trailing newline from a string, if it exists.
592  */
593 int llapi_chomp_string(char *buf)
594 {
595         if (!buf || !*buf)
596                 return 0;
597
598         while (buf[1])
599                 buf++;
600
601         if (*buf != '\n')
602                 return 0;
603
604         *buf = '\0';
605         return '\n';
606 }
607
608 /*
609  * Wrapper to grab parameter settings for lov.*-clilov-*.* values
610  */
611 static int get_param_lov(const char *path, const char *param,
612                          char *buf, size_t buf_size)
613 {
614         struct obd_uuid uuid;
615         int rc;
616
617         rc = llapi_file_get_lov_uuid(path, &uuid);
618         if (rc != 0)
619                 return rc;
620
621         return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
622                                       buf, buf_size);
623 }
624
625 /*
626  * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
627  */
628 static int get_param_lmv(const char *path, const char *param,
629                          char *buf, size_t buf_size)
630 {
631         struct obd_uuid uuid;
632         int rc;
633
634         rc = llapi_file_get_lmv_uuid(path, &uuid);
635         if (rc != 0)
636                 return rc;
637
638         return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
639                                buf, buf_size);
640 }
641
642 static int get_mds_md_size(const char *path)
643 {
644         int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
645
646         /*
647          * Rather than open the file and do the ioctl to get the
648          * instance name and close the file and search for the param
649          * file and open the param file and read the param file and
650          * parse the value and close the param file, let's just return
651          * a large enough value. It's 2020, RAM is cheap and this is
652          * much faster.
653          */
654
655         if (md_size < XATTR_SIZE_MAX)
656                 md_size = XATTR_SIZE_MAX;
657
658         return md_size;
659 }
660
661 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
662 {
663         return get_param_lmv(path, "uuid", buf, bufsize);
664 }
665
666 /**
667  * Open a Lustre file.
668  *
669  * \param name     the name of the file to be opened
670  * \param flags    access mode, see flags in open(2)
671  * \param mode     permission of the file if it is created, see mode in open(2)
672  * \param param    stripe pattern of the newly created file
673  *
674  * \retval         file descriptor of opened file
675  * \retval         negative errno on failure
676  */
677 int llapi_file_open_param(const char *name, int flags, mode_t mode,
678                           const struct llapi_stripe_param *param)
679 {
680         char fsname[MAX_OBD_NAME + 1] = { 0 };
681         struct lov_user_md *lum = NULL;
682         char *pool_name = param->lsp_pool;
683         size_t lum_size;
684         int fd, rc;
685
686         /* Make sure we are on a Lustre file system */
687         rc = llapi_search_fsname(name, fsname);
688         if (rc) {
689                 llapi_error(LLAPI_MSG_ERROR, rc,
690                             "'%s' is not on a Lustre filesystem",
691                             name);
692                 return rc;
693         }
694
695         /* Check if the stripe pattern is sane. */
696         rc = llapi_stripe_param_verify(param, &pool_name, fsname);
697         if (rc != 0)
698                 return rc;
699
700         if (param->lsp_is_specific)
701                 lum_size = lov_user_md_size(param->lsp_stripe_count,
702                                             LOV_USER_MAGIC_SPECIFIC);
703         else if (pool_name)
704                 lum_size = sizeof(struct lov_user_md_v3);
705         else
706                 lum_size = sizeof(*lum);
707
708         lum = calloc(1, lum_size);
709         if (lum == NULL)
710                 return -ENOMEM;
711
712 retry_open:
713         fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
714         if (fd < 0) {
715                 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
716                         flags = O_DIRECTORY | O_RDONLY;
717                         goto retry_open;
718                 }
719         }
720
721         if (fd < 0) {
722                 rc = -errno;
723                 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
724                 free(lum);
725                 return rc;
726         }
727
728         /*  Initialize IOCTL striping pattern structure */
729         lum->lmm_magic = LOV_USER_MAGIC_V1;
730         lum->lmm_pattern = param->lsp_stripe_pattern;
731         lum->lmm_stripe_size = param->lsp_stripe_size;
732         lum->lmm_stripe_count = param->lsp_stripe_count;
733         lum->lmm_stripe_offset = param->lsp_stripe_offset;
734         if (pool_name != NULL) {
735                 struct lov_user_md_v3 *lumv3 = (void *)lum;
736
737                 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
738                 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
739         }
740         if (param->lsp_is_specific) {
741                 struct lov_user_md_v3 *lumv3 = (void *)lum;
742                 int i;
743
744                 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
745                 if (pool_name == NULL) {
746                         /*
747                          * LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
748                          * OST list, therefore if pool is not specified we have
749                          * to pack a null pool name for placeholder.
750                          */
751                         memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
752                 }
753
754                 for (i = 0; i < param->lsp_stripe_count; i++)
755                         lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
756         }
757
758         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
759                 char errmsg[512] = "stripe already set";
760
761                 rc = -errno;
762                 if (errno != EEXIST && errno != EALREADY)
763                         strncpy(errmsg, strerror(errno), sizeof(errmsg) - 1);
764                 if (rc == -EREMOTEIO)
765                         snprintf(errmsg, sizeof(errmsg),
766                                  "inactive OST among your specified %d OST(s)",
767                                  param->lsp_stripe_count);
768
769                 llapi_err_noerrno(LLAPI_MSG_ERROR,
770                                   "setstripe error for '%s': %s", name, errmsg);
771
772                 close(fd);
773                 fd = rc;
774         }
775
776         free(lum);
777
778         return fd;
779 }
780
781 int llapi_file_is_encrypted(int fd)
782 {
783         unsigned long flags;
784         int rc;
785
786         rc = ioctl(fd, FS_IOC_GETFLAGS, &flags);
787         if (rc == -1)
788                 return -errno;
789
790         return !!(flags & LUSTRE_ENCRYPT_FL);
791 }
792
793 int llapi_file_open_pool(const char *name, int flags, int mode,
794                          unsigned long long stripe_size, int stripe_offset,
795                          int stripe_count, int stripe_pattern, char *pool_name)
796 {
797         const struct llapi_stripe_param param = {
798                 .lsp_stripe_size = stripe_size,
799                 .lsp_stripe_count = stripe_count,
800                 .lsp_stripe_pattern = stripe_pattern,
801                 .lsp_stripe_offset = stripe_offset,
802                 .lsp_pool = pool_name
803         };
804         return llapi_file_open_param(name, flags, mode, &param);
805 }
806
807 int llapi_file_open(const char *name, int flags, int mode,
808                     unsigned long long stripe_size, int stripe_offset,
809                     int stripe_count, int stripe_pattern)
810 {
811         return llapi_file_open_pool(name, flags, mode, stripe_size,
812                                     stripe_offset, stripe_count,
813                                     stripe_pattern, NULL);
814 }
815
816 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
817                               __u32 flags, char *foreign_lov)
818 {
819         size_t len;
820         struct lov_foreign_md *lfm;
821         int fd, rc;
822
823         if (foreign_lov == NULL) {
824                 rc = -EINVAL;
825                 llapi_error(LLAPI_MSG_ERROR, rc,
826                             "foreign LOV EA content must be provided");
827                 goto out_err;
828         }
829
830         len = strlen(foreign_lov);
831         if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
832             len <= 0) {
833                 rc = -EINVAL;
834                 llapi_error(LLAPI_MSG_ERROR, rc,
835                             "foreign LOV EA size %zu (must be 0 < len < %zu)",
836                             len, XATTR_SIZE_MAX -
837                             offsetof(struct lov_foreign_md, lfm_value));
838                 goto out_err;
839         }
840
841         lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
842         if (lfm == NULL) {
843                 rc = -ENOMEM;
844                 llapi_error(LLAPI_MSG_ERROR, rc,
845                             "failed to allocate lov_foreign_md");
846                 goto out_err;
847         }
848
849         fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
850         if (fd == -1) {
851                 fd = -errno;
852                 llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
853                 goto out_free;
854         }
855
856         lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
857         lfm->lfm_length = len;
858         lfm->lfm_type = type;
859         lfm->lfm_flags = flags;
860         memcpy(lfm->lfm_value, foreign_lov, len);
861
862         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
863                 char *errmsg = "stripe already set";
864
865                 rc = -errno;
866                 if (errno == ENOTTY)
867                         errmsg = "not on a Lustre filesystem";
868                 else if (errno == EEXIST || errno == EALREADY)
869                         errmsg = "stripe already set";
870                 else
871                         errmsg = strerror(errno);
872
873                 llapi_err_noerrno(LLAPI_MSG_ERROR,
874                                   "setstripe error for '%s': %s", name, errmsg);
875
876                 close(fd);
877                 fd = rc;
878         }
879
880 out_free:
881         free(lfm);
882
883         return fd;
884
885 out_err:
886         errno = -rc;
887         return rc;
888 }
889
890 int llapi_file_create(const char *name, unsigned long long stripe_size,
891                       int stripe_offset, int stripe_count, int stripe_pattern)
892 {
893         int fd;
894
895         fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
896                                   stripe_offset, stripe_count, stripe_pattern,
897                                   NULL);
898         if (fd < 0)
899                 return fd;
900
901         close(fd);
902         return 0;
903 }
904
905 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
906                            int stripe_offset, int stripe_count,
907                            int stripe_pattern, char *pool_name)
908 {
909         int fd;
910
911         fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
912                                   stripe_offset, stripe_count, stripe_pattern,
913                                   pool_name);
914         if (fd < 0)
915                 return fd;
916
917         close(fd);
918         return 0;
919 }
920
921 static int verify_dir_param(const char *name,
922                             const struct llapi_stripe_param *param)
923 {
924         char fsname[MAX_OBD_NAME + 1] = { 0 };
925         char *pool_name = param->lsp_pool;
926         int rc;
927
928         /* Make sure we are on a Lustre file system */
929         rc = llapi_search_fsname(name, fsname);
930         if (rc) {
931                 llapi_error(LLAPI_MSG_ERROR, rc,
932                             "'%s' is not on a Lustre filesystem",
933                             name);
934                 return rc;
935         }
936
937         /* Check if the stripe pattern is sane. */
938         rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
939                                           param->lsp_stripe_count,
940                                           param->lsp_stripe_pattern);
941         if (rc != 0)
942                 return rc;
943
944         /* Make sure we have a good pool */
945         if (pool_name != NULL) {
946                 /*
947                  * in case user gives the full pool name <fsname>.<poolname>,
948                  * strip the fsname
949                  */
950                 char *ptr = strchr(pool_name, '.');
951
952                 if (ptr != NULL) {
953                         *ptr = '\0';
954                         if (strcmp(pool_name, fsname) != 0) {
955                                 *ptr = '.';
956                                 llapi_err_noerrno(LLAPI_MSG_ERROR,
957                                         "Pool '%s' is not on filesystem '%s'",
958                                         pool_name, fsname);
959                                 return -EINVAL;
960                         }
961                         pool_name = ptr + 1;
962                 }
963
964                 /* Make sure the pool exists and is non-empty */
965                 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
966                 if (rc < 1) {
967                         char *err = rc == 0 ? "has no OSTs" : "does not exist";
968
969                         llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
970                                           fsname, pool_name, err);
971                         return -EINVAL;
972                 }
973         }
974
975         /* sanity check of target list */
976         if (param->lsp_is_specific) {
977                 char mdtname[MAX_OBD_NAME + 64];
978                 bool found = false;
979                 int i;
980
981                 for (i = 0; i < param->lsp_stripe_count; i++) {
982                         snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
983                                  fsname, param->lsp_tgts[i]);
984                         rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
985                         if (rc <= 0) {
986                                 if (rc == 0)
987                                         rc = -ENODEV;
988
989                                 llapi_error(LLAPI_MSG_ERROR, rc,
990                                             "%s: cannot find MDT %s in %s",
991                                             __func__, mdtname,
992                                             pool_name != NULL ?
993                                             "pool" : "system");
994                                 return rc;
995                         }
996
997                         /* Make sure stripe offset is in MDT list. */
998                         if (param->lsp_tgts[i] == param->lsp_stripe_offset)
999                                 found = true;
1000                 }
1001                 if (!found) {
1002                         llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1003                                     "%s: stripe offset '%d' is not in the target list",
1004                                     __func__, param->lsp_stripe_offset);
1005                         return -EINVAL;
1006                 }
1007         }
1008
1009         return 0;
1010 }
1011
1012 static inline void param2lmu(struct lmv_user_md *lmu,
1013                              const struct llapi_stripe_param *param)
1014 {
1015         lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1016                                                   LMV_USER_MAGIC;
1017         lmu->lum_stripe_count = param->lsp_stripe_count;
1018         lmu->lum_stripe_offset = param->lsp_stripe_offset;
1019         lmu->lum_hash_type = param->lsp_stripe_pattern;
1020         lmu->lum_max_inherit = param->lsp_max_inherit;
1021         lmu->lum_max_inherit_rr = param->lsp_max_inherit_rr;
1022         if (param->lsp_pool != NULL)
1023                 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1024         if (param->lsp_is_specific) {
1025                 int i;
1026
1027                 for (i = 0; i < param->lsp_stripe_count; i++)
1028                         lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1029         }
1030 }
1031
1032 int llapi_dir_set_default_lmv(const char *name,
1033                               const struct llapi_stripe_param *param)
1034 {
1035         struct lmv_user_md lmu = { 0 };
1036         int fd;
1037         int rc = 0;
1038
1039         rc = verify_dir_param(name, param);
1040         if (rc)
1041                 return rc;
1042
1043         /* TODO: default lmv doesn't support specific targets yet */
1044         if (param->lsp_is_specific)
1045                 return -EINVAL;
1046
1047         param2lmu(&lmu, param);
1048
1049         fd = open(name, O_DIRECTORY | O_RDONLY);
1050         if (fd < 0) {
1051                 rc = -errno;
1052                 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1053                 return rc;
1054         }
1055
1056         rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1057         if (rc < 0) {
1058                 char *errmsg = "stripe already set";
1059
1060                 rc = -errno;
1061                 if (errno != EEXIST && errno != EALREADY)
1062                         errmsg = strerror(errno);
1063
1064                 llapi_err_noerrno(LLAPI_MSG_ERROR,
1065                                   "default dirstripe error on '%s': %s",
1066                                   name, errmsg);
1067         }
1068         close(fd);
1069         return rc;
1070 }
1071
1072 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1073                                      int stripe_count, int stripe_pattern,
1074                                      const char *pool_name)
1075 {
1076         const struct llapi_stripe_param param = {
1077                 .lsp_stripe_count = stripe_count,
1078                 .lsp_stripe_offset = stripe_offset,
1079                 .lsp_stripe_pattern = stripe_pattern,
1080                 .lsp_pool = (char *)pool_name
1081         };
1082
1083         return llapi_dir_set_default_lmv(name, &param);
1084 }
1085
1086 /**
1087  * Create a Lustre directory.
1088  *
1089  * \param name     the name of the directory to be created
1090  * \param mode     permission of the file if it is created, see mode in open(2)
1091  * \param param    stripe pattern of the newly created directory
1092  *
1093  * \retval         0 on success
1094  * \retval         negative errno on failure
1095  */
1096 int llapi_dir_create(const char *name, mode_t mode,
1097                      const struct llapi_stripe_param *param)
1098 {
1099         struct lmv_user_md *lmu = NULL;
1100         size_t lmu_size;
1101         struct obd_ioctl_data data = { 0 };
1102         char rawbuf[8192];
1103         char *buf = rawbuf;
1104         char *dirpath = NULL;
1105         char *namepath = NULL;
1106         char *dir;
1107         char *filename;
1108         int fd, rc;
1109
1110         rc = verify_dir_param(name, param);
1111         if (rc)
1112                 return rc;
1113
1114         lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1115                                     param->lsp_is_specific ?
1116                                          LMV_USER_MAGIC_SPECIFIC :
1117                                          LMV_USER_MAGIC);
1118
1119         lmu = calloc(1, lmu_size);
1120         if (lmu == NULL)
1121                 return -ENOMEM;
1122
1123         dirpath = strdup(name);
1124         if (!dirpath) {
1125                 free(lmu);
1126                 return -ENOMEM;
1127         }
1128
1129         namepath = strdup(name);
1130         if (!namepath) {
1131                 free(dirpath);
1132                 free(lmu);
1133                 return -ENOMEM;
1134         }
1135
1136         param2lmu(lmu, param);
1137
1138         filename = basename(namepath);
1139         dir = dirname(dirpath);
1140
1141         data.ioc_inlbuf1 = (char *)filename;
1142         data.ioc_inllen1 = strlen(filename) + 1;
1143         data.ioc_inlbuf2 = (char *)lmu;
1144         data.ioc_inllen2 = lmu_size;
1145         data.ioc_type = mode;
1146         if (param->lsp_is_create)
1147                 /* borrow obdo1.o_flags to store this flag */
1148                 data.ioc_obdo1.o_flags = OBD_FL_OBDMDEXISTS;
1149         rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1150         if (rc) {
1151                 llapi_error(LLAPI_MSG_ERROR, rc,
1152                             "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1153                             name);
1154                 goto out;
1155         }
1156
1157         fd = open(dir, O_DIRECTORY | O_RDONLY);
1158         if (fd < 0) {
1159                 rc = -errno;
1160                 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1161                 goto out;
1162         }
1163
1164         if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1165                 char *errmsg = "stripe already set";
1166
1167                 rc = -errno;
1168                 if (errno != EEXIST && errno != EALREADY)
1169                         errmsg = strerror(errno);
1170
1171                 llapi_err_noerrno(LLAPI_MSG_ERROR,
1172                                   "dirstripe error on '%s': %s", name, errmsg);
1173         }
1174         close(fd);
1175 out:
1176         free(namepath);
1177         free(dirpath);
1178         free(lmu);
1179         return rc;
1180 }
1181
1182 /**
1183  * Create a foreign directory.
1184  *
1185  * \param name     the name of the directory to be created
1186  * \param mode     permission of the file if it is created, see mode in open(2)
1187  * \param type     foreign type to be set in LMV EA
1188  * \param flags    foreign flags to be set in LMV EA
1189  * \param value    foreign pattern to be set in LMV EA
1190  *
1191  * \retval         0 on success
1192  * \retval         negative errno on failure
1193  */
1194 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1195                              __u32 flags, const char *value)
1196 {
1197         struct lmv_foreign_md *lfm = NULL;
1198         size_t lfm_size, len;
1199         struct obd_ioctl_data data = { 0 };
1200         char rawbuf[8192];
1201         char *buf = rawbuf;
1202         char *dirpath = NULL;
1203         char *namepath = NULL;
1204         char *dir;
1205         char *filename;
1206         int fd, rc;
1207
1208         len = strlen(value);
1209         if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1210             len <= 0) {
1211                 rc = -EINVAL;
1212                 llapi_error(LLAPI_MSG_ERROR, rc,
1213                             "invalid LOV EA length %zu (must be 0 < len < %zu)",
1214                             len, XATTR_SIZE_MAX -
1215                             offsetof(struct lmv_foreign_md, lfm_value));
1216                 return rc;
1217         }
1218         lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1219         lfm = calloc(1, lfm_size);
1220         if (lfm == NULL)
1221                 return -ENOMEM;
1222
1223         dirpath = strdup(name);
1224         if (!dirpath) {
1225                 free(lfm);
1226                 return -ENOMEM;
1227         }
1228
1229         namepath = strdup(name);
1230         if (!namepath) {
1231                 free(dirpath);
1232                 free(lfm);
1233                 return -ENOMEM;
1234         }
1235
1236         lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1237         lfm->lfm_length = len;
1238         lfm->lfm_type = type;
1239         lfm->lfm_flags = flags;
1240         memcpy(lfm->lfm_value, value, len);
1241
1242         filename = basename(namepath);
1243         dir = dirname(dirpath);
1244
1245         data.ioc_inlbuf1 = (char *)filename;
1246         data.ioc_inllen1 = strlen(filename) + 1;
1247         data.ioc_inlbuf2 = (char *)lfm;
1248         data.ioc_inllen2 = lfm_size;
1249         data.ioc_type = mode;
1250         rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1251         if (rc) {
1252                 llapi_error(LLAPI_MSG_ERROR, rc,
1253                             "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1254                             name);
1255                 goto out;
1256         }
1257
1258         fd = open(dir, O_DIRECTORY | O_RDONLY);
1259         if (fd < 0) {
1260                 rc = -errno;
1261                 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1262                 goto out;
1263         }
1264
1265         if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1266                 char *errmsg = "stripe already set";
1267
1268                 rc = -errno;
1269                 if (errno != EEXIST && errno != EALREADY)
1270                         errmsg = strerror(errno);
1271
1272                 llapi_err_noerrno(LLAPI_MSG_ERROR,
1273                                   "dirstripe error on '%s': %s", name, errmsg);
1274         }
1275         close(fd);
1276 out:
1277         free(namepath);
1278         free(dirpath);
1279         free(lfm);
1280         return rc;
1281 }
1282
1283 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1284                           int stripe_count, int stripe_pattern,
1285                           const char *pool_name)
1286 {
1287         const struct llapi_stripe_param param = {
1288                 .lsp_stripe_count = stripe_count,
1289                 .lsp_stripe_offset = stripe_offset,
1290                 .lsp_stripe_pattern = stripe_pattern,
1291                 .lsp_pool = (char *)pool_name
1292         };
1293
1294         return llapi_dir_create(name, mode, &param);
1295 }
1296
1297 /*
1298  * Find the fsname, the full path, and/or an open fd.
1299  * Either the fsname or path must not be NULL
1300  */
1301 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1302 {
1303         struct mntent mnt;
1304         char buf[PATH_MAX], mntdir[PATH_MAX];
1305         char *ptr, *ptr_end;
1306         FILE *fp;
1307         int idx = 0, mntlen = 0, fd;
1308         int rc = -ENODEV;
1309         int fsnamelen, mountlen;
1310
1311         /* get the mount point */
1312         fp = setmntent(PROC_MOUNTS, "r");
1313         if (fp == NULL) {
1314                 rc = -EIO;
1315                 llapi_error(LLAPI_MSG_ERROR, rc,
1316                             "cannot retrieve filesystem mount point");
1317                 return rc;
1318         }
1319         while (1) {
1320                 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1321                         break;
1322
1323                 if (!llapi_is_lustre_mnt(&mnt))
1324                         continue;
1325
1326                 if ((want & WANT_INDEX) && (idx++ != index))
1327                         continue;
1328
1329                 mntlen = strlen(mnt.mnt_dir);
1330                 ptr = strchr(mnt.mnt_fsname, '/');
1331                 while (ptr && *ptr == '/')
1332                         ptr++;
1333                 /*
1334                  * thanks to the call to llapi_is_lustre_mnt() above,
1335                  * we are sure that mnt.mnt_fsname contains ":/",
1336                  * so ptr should never be NULL
1337                  */
1338                 if (ptr == NULL)
1339                         continue;
1340                 ptr_end = ptr;
1341                 while (*ptr_end != '/' && *ptr_end != '\0')
1342                         ptr_end++;
1343
1344                 /* Check the fsname for a match, if given */
1345                 mountlen = ptr_end - ptr;
1346                 if (!(want & WANT_FSNAME) && fsname != NULL &&
1347                     (fsnamelen = strlen(fsname)) > 0 &&
1348                     (fsnamelen != mountlen ||
1349                     (strncmp(ptr, fsname, mountlen) != 0)))
1350                         continue;
1351
1352                 /* If the path isn't set return the first one we find */
1353                 if (path == NULL || strlen(path) == 0) {
1354                         strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1355                         mntdir[sizeof(mntdir) - 1] = '\0';
1356                         if ((want & WANT_FSNAME) && fsname != NULL) {
1357                                 strncpy(fsname, ptr, mountlen);
1358                                 fsname[mountlen] = '\0';
1359                         }
1360                         rc = 0;
1361                         break;
1362                 /* Otherwise find the longest matching path */
1363                 } else if ((strlen(path) >= mntlen) &&
1364                            (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1365                         /* check the path format */
1366                         if (strlen(path) > mntlen && path[mntlen] != '/')
1367                                 continue;
1368                         strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1369                         mntdir[sizeof(mntdir) - 1] = '\0';
1370                         if ((want & WANT_FSNAME) && fsname != NULL) {
1371                                 strncpy(fsname, ptr, mountlen);
1372                                 fsname[mountlen] = '\0';
1373                         }
1374                         rc = 0;
1375                         break;
1376                 }
1377         }
1378         endmntent(fp);
1379
1380         /* Found it */
1381         if (rc == 0) {
1382                 if ((want & WANT_PATH) && path != NULL) {
1383                         strncpy(path, mntdir, mntlen);
1384                         path[mntlen] = '\0';
1385                 }
1386                 if (want & WANT_FD) {
1387                         fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1388                         if (fd < 0) {
1389                                 rc = -errno;
1390                                 llapi_error(LLAPI_MSG_ERROR, rc,
1391                                             "cannot open '%s'", mntdir);
1392
1393                         } else {
1394                                 *outfd = fd;
1395                         }
1396                 }
1397         } else if (want & WANT_ERROR)
1398                 llapi_err_noerrno(LLAPI_MSG_ERROR,
1399                                   "'%s' not on a mounted Lustre filesystem",
1400                                   (want & WANT_PATH) ? fsname : path);
1401         return rc;
1402 }
1403
1404 /*
1405  * search lustre mounts
1406  *
1407  * Calling this function will return to the user the mount point, mntdir, and
1408  * the file system name, fsname, if the user passed a buffer to this routine.
1409  *
1410  * The user inputs are pathname and index. If the pathname is supplied then
1411  * the value of the index will be ignored. The pathname will return data if
1412  * the pathname is located on a lustre mount. Index is used to pick which
1413  * mount point you want in the case of multiple mounted lustre file systems.
1414  * See function lfs_osts in lfs.c for an example of the index use.
1415  */
1416 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1417                         char *fsname)
1418 {
1419         int want = WANT_PATH, idx = -1;
1420
1421         if (!pathname || pathname[0] == '\0') {
1422                 want |= WANT_INDEX;
1423                 idx = index;
1424         } else {
1425                 strcpy(mntdir, pathname);
1426         }
1427
1428         if (fsname)
1429                 want |= WANT_FSNAME;
1430         return get_root_path(want, fsname, NULL, mntdir, idx);
1431 }
1432
1433 /* Given a path, find the corresponding Lustre fsname */
1434 int llapi_search_fsname(const char *pathname, char *fsname)
1435 {
1436         char *path;
1437         int rc;
1438
1439         path = realpath(pathname, NULL);
1440         if (path == NULL) {
1441                 char tmp[PATH_MAX - 1];
1442                 char buf[PATH_MAX];
1443                 char *ptr;
1444
1445                 tmp[0] = '\0';
1446                 buf[0] = '\0';
1447                 if (pathname[0] != '/') {
1448                         /*
1449                          * Need an absolute path, but realpath() only works for
1450                          * pathnames that actually exist.  We go through the
1451                          * extra hurdle of dirname(getcwd() + pathname) in
1452                          * case the relative pathname contains ".." in it.
1453                          */
1454                         char realpath[PATH_MAX - 1];
1455
1456                         if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1457                                 rc = -errno;
1458                                 llapi_error(LLAPI_MSG_ERROR, rc,
1459                                             "cannot get current working directory");
1460                                 return rc;
1461                         }
1462
1463                         rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1464                         if (rc >= sizeof(tmp)) {
1465                                 rc = -E2BIG;
1466                                 llapi_error(LLAPI_MSG_ERROR, rc,
1467                                             "invalid parent path '%s'",
1468                                             tmp);
1469                                 return rc;
1470                         }
1471                 }
1472
1473                 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1474                 if (rc >= sizeof(buf)) {
1475                         rc = -E2BIG;
1476                         llapi_error(LLAPI_MSG_ERROR, rc,
1477                                     "invalid path '%s'", pathname);
1478                         return rc;
1479                 }
1480                 path = realpath(buf, NULL);
1481                 if (path == NULL) {
1482                         ptr = strrchr(buf, '/');
1483                         if (ptr == NULL) {
1484                                 llapi_error(LLAPI_MSG_ERROR |
1485                                             LLAPI_MSG_NO_ERRNO, 0,
1486                                             "cannot resolve path '%s'",
1487                                             buf);
1488                                 return -ENOENT;
1489                         }
1490                         *ptr = '\0';
1491                         path = realpath(buf, NULL);
1492                         if (path == NULL) {
1493                                 rc = -errno;
1494                                 llapi_error(LLAPI_MSG_ERROR, rc,
1495                                             "cannot resolve path '%s'",
1496                                              pathname);
1497                                 return rc;
1498                         }
1499                 }
1500         }
1501         rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1502         free(path);
1503         return rc;
1504 }
1505
1506 int llapi_search_rootpath(char *pathname, const char *fsname)
1507 {
1508         /*
1509          * pathname can be used as an argument by get_root_path(),
1510          * clear it for safety
1511          */
1512         pathname[0] = 0;
1513         return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1514 }
1515
1516 /**
1517  * Get the list of pool members.
1518  * \param poolname    string of format \<fsname\>.\<poolname\>
1519  * \param members     caller-allocated array of char*
1520  * \param list_size   size of the members array
1521  * \param buffer      caller-allocated buffer for storing OST names
1522  * \param buffer_size size of the buffer
1523  *
1524  * \return number of members retrieved for this pool
1525  * \retval -error failure
1526  */
1527 int llapi_get_poolmembers(const char *poolname, char **members,
1528                           int list_size, char *buffer, int buffer_size)
1529 {
1530         char fsname[PATH_MAX];
1531         char *pool, *tmp;
1532         glob_t pathname;
1533         char buf[PATH_MAX];
1534         FILE *fd;
1535         int rc = 0;
1536         int nb_entries = 0;
1537         int used = 0;
1538
1539         /* name is FSNAME.POOLNAME */
1540         if (strlen(poolname) >= sizeof(fsname))
1541                 return -EOVERFLOW;
1542
1543         snprintf(fsname, sizeof(fsname), "%s", poolname);
1544         pool = strchr(fsname, '.');
1545         if (pool == NULL)
1546                 return -EINVAL;
1547
1548         *pool = '\0';
1549         pool++;
1550
1551         rc = poolpath(&pathname, fsname, NULL);
1552         if (rc != 0) {
1553                 llapi_error(LLAPI_MSG_ERROR, rc,
1554                             "Lustre filesystem '%s' not found",
1555                             fsname);
1556                 return rc;
1557         }
1558
1559         llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1560         rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1561         cfs_free_param_data(&pathname);
1562         if (rc >= sizeof(buf))
1563                 return -EOVERFLOW;
1564         fd = fopen(buf, "r");
1565         if (fd == NULL) {
1566                 rc = -errno;
1567                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1568                 return rc;
1569         }
1570
1571         rc = 0;
1572         while (fgets(buf, sizeof(buf), fd) != NULL) {
1573                 if (nb_entries >= list_size) {
1574                         rc = -EOVERFLOW;
1575                         break;
1576                 }
1577                 buf[sizeof(buf) - 1] = '\0';
1578                 /* remove '\n' */
1579                 tmp = strchr(buf, '\n');
1580                 if (tmp != NULL)
1581                         *tmp = '\0';
1582                 if (used + strlen(buf) + 1 > buffer_size) {
1583                         rc = -EOVERFLOW;
1584                         break;
1585                 }
1586
1587                 strcpy(buffer + used, buf);
1588                 members[nb_entries] = buffer + used;
1589                 used += strlen(buf) + 1;
1590                 nb_entries++;
1591                 rc = nb_entries;
1592         }
1593
1594         fclose(fd);
1595         return rc;
1596 }
1597
1598 /**
1599  * Get the list of pools in a filesystem.
1600  * \param name        filesystem name or path
1601  * \param poollist    caller-allocated array of char*
1602  * \param list_size   size of the poollist array
1603  * \param buffer      caller-allocated buffer for storing pool names
1604  * \param buffer_size size of the buffer
1605  *
1606  * \return number of pools retrieved for this filesystem
1607  * \retval -error failure
1608  */
1609 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1610                        char *buffer, int buffer_size)
1611 {
1612         glob_t pathname;
1613         char *fsname;
1614         char *ptr;
1615         DIR *dir;
1616         struct dirent *pool;
1617         int rc = 0;
1618         unsigned int nb_entries = 0;
1619         unsigned int used = 0;
1620         unsigned int i;
1621
1622         /* initialize output array */
1623         for (i = 0; i < list_size; i++)
1624                 poollist[i] = NULL;
1625
1626         /* is name a pathname ? */
1627         ptr = strchr(name, '/');
1628         if (ptr != NULL) {
1629                 char fsname_buf[MAXNAMLEN];
1630
1631                 /* We will need fsname for printing later */
1632                 rc = llapi_getname(name, fsname_buf, sizeof(fsname_buf));
1633                 if (rc)
1634                         return rc;
1635
1636                 ptr = strrchr(fsname_buf, '-');
1637                 if (ptr)
1638                         *ptr = '\0';
1639
1640                 fsname = strdup(fsname_buf);
1641                 if (!fsname)
1642                         return -ENOMEM;
1643         } else {
1644                 /* name is FSNAME */
1645                 fsname = strdup(name);
1646                 if (!fsname)
1647                         return -ENOMEM;
1648         }
1649
1650         rc = poolpath(&pathname, fsname, NULL);
1651         if (rc != 0) {
1652                 llapi_error(LLAPI_MSG_ERROR, rc,
1653                             "Lustre filesystem '%s' not found", name);
1654                 goto free_path;
1655         }
1656
1657         dir = opendir(pathname.gl_pathv[0]);
1658         if (dir == NULL) {
1659                 rc = -errno;
1660                 llapi_error(LLAPI_MSG_ERROR, rc,
1661                             "Could not open pool list for '%s'",
1662                             name);
1663                 goto free_path;
1664         }
1665
1666         do {
1667                 errno = 0;
1668                 pool = readdir(dir);
1669                 if (pool == NULL) {
1670                         rc = -errno;
1671                         goto free_dir;
1672                 }
1673
1674                 /* ignore . and .. */
1675                 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1676                         continue;
1677
1678                 /* check output bounds */
1679                 if (nb_entries >= list_size) {
1680                         rc = -EOVERFLOW;
1681                         goto free_dir_no_msg;
1682                 }
1683
1684                 /* +2 for '.' and final '\0' */
1685                 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1686                     > buffer_size) {
1687                         rc = -EOVERFLOW;
1688                         goto free_dir_no_msg;
1689                 }
1690
1691                 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1692                 poollist[nb_entries] = buffer + used;
1693                 used += strlen(pool->d_name) + strlen(fsname) + 2;
1694                 nb_entries++;
1695         } while (1);
1696
1697 free_dir:
1698         if (rc)
1699                 llapi_error(LLAPI_MSG_ERROR, rc,
1700                             "Error reading pool list for '%s'", name);
1701         else
1702                 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1703
1704 free_dir_no_msg:
1705         closedir(dir);
1706 free_path:
1707         cfs_free_param_data(&pathname);
1708         if (fsname)
1709                 free(fsname);
1710         return rc != 0 ? rc : nb_entries;
1711 }
1712
1713 /* wrapper for lfs.c and obd.c */
1714 int llapi_poollist(const char *name)
1715 {
1716         int poolcount, rc, i;
1717         char *buf, **pools;
1718
1719         rc = llapi_get_poolbuf(name, &buf, &pools, &poolcount);
1720         if (rc)
1721                 return rc;
1722
1723         for (i = 0; i < poolcount; i++)
1724                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", pools[i]);
1725         free(buf);
1726
1727         return 0;
1728 }
1729
1730 /**
1731  * Get buffer that holds uuids and the list of pools in a filesystem.
1732  *
1733  * \param name          filesystem name or path
1734  * \param buf           bufffer that has to be freed if function returns 0
1735  * \param pools         pointer to the list of pools in buffer
1736  * \param poolcount     number of pools
1737  *
1738  * \return 0 when found at least 1 pool, i.e. poolcount  > 0
1739  * \retval -error failure
1740  */
1741 int llapi_get_poolbuf(const char *name, char **buf,
1742                       char ***pools, int *poolcount)
1743 {
1744         /*
1745          * list of pool names (assume that pool count is smaller
1746          * than OST count)
1747          */
1748         char **list, *buffer = NULL, *fsname = (char *)name;
1749         char *poolname = NULL, *tmp = NULL, data[16];
1750         enum param_filter type = FILTER_BY_PATH;
1751         int obdcount, bufsize, rc, nb;
1752
1753         if (name == NULL)
1754                 return -EINVAL;
1755
1756         if (name[0] != '/') {
1757                 fsname = strdup(name);
1758                 if (fsname == NULL)
1759                         return -ENOMEM;
1760
1761                 poolname = strchr(fsname, '.');
1762                 if (poolname)
1763                         *poolname = '\0';
1764                 type = FILTER_BY_FS_NAME;
1765         }
1766
1767         rc = get_lustre_param_value("lov", fsname, type, "numobd",
1768                                     data, sizeof(data));
1769         if (rc < 0)
1770                 goto err;
1771         obdcount = atoi(data);
1772
1773         /*
1774          * Allocate space for each fsname-OST0000_UUID, 1 per OST,
1775          * and also an array to store the pointers for all that
1776          * allocated space.
1777          */
1778 retry_get_pools:
1779         bufsize = sizeof(struct obd_uuid) * obdcount;
1780         buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1781         if (buffer == NULL) {
1782                 rc = -ENOMEM;
1783                 goto err;
1784         }
1785         list = (char **) (buffer + bufsize);
1786
1787         if (!poolname) {
1788                 /* name is a path or fsname */
1789                 nb = llapi_get_poollist(name, list, obdcount,
1790                                         buffer, bufsize);
1791         } else {
1792                 /* name is a pool name (<fsname>.<poolname>) */
1793                 nb = llapi_get_poolmembers(name, list, obdcount,
1794                                            buffer, bufsize);
1795         }
1796
1797         if (nb == -EOVERFLOW) {
1798                 obdcount *= 2;
1799                 tmp = buffer;
1800                 goto retry_get_pools;
1801         }
1802
1803         rc = (nb < 0 ? nb : 0);
1804         if (!rc) {
1805                 *buf = buffer;
1806                 *pools = list;
1807                 *poolcount = nb;
1808         }
1809 err:
1810         /* Don't free buffer, it will be used later */
1811         if (rc && buffer)
1812                 free(buffer);
1813         if (fsname != NULL && type == FILTER_BY_FS_NAME)
1814                 free(fsname);
1815         return rc;
1816 }
1817
1818 typedef int (semantic_func_t)(char *path, int p, int *d,
1819                               void *data, struct dirent64 *de);
1820
1821 #define OBD_NOT_FOUND           (-1)
1822
1823 static bool lmv_is_foreign(__u32 magic)
1824 {
1825         return magic == LMV_MAGIC_FOREIGN;
1826 }
1827
1828 static void find_param_fini(struct find_param *param)
1829 {
1830         if (param->fp_migrate)
1831                 return;
1832
1833         if (param->fp_obd_indexes) {
1834                 free(param->fp_obd_indexes);
1835                 param->fp_obd_indexes = NULL;
1836         }
1837
1838         if (param->fp_lmd) {
1839                 free(param->fp_lmd);
1840                 param->fp_lmd = NULL;
1841         }
1842
1843         if (param->fp_lmv_md) {
1844                 free(param->fp_lmv_md);
1845                 param->fp_lmv_md = NULL;
1846         }
1847 }
1848
1849 static int common_param_init(struct find_param *param, char *path)
1850 {
1851         int lum_size = get_mds_md_size(path);
1852
1853         if (lum_size < 0)
1854                 return lum_size;
1855
1856         /* migrate has fp_lmv_md initialized outside */
1857         if (param->fp_migrate)
1858                 return 0;
1859
1860         if (lum_size < PATH_MAX + 1)
1861                 lum_size = PATH_MAX + 1;
1862
1863         param->fp_lum_size = lum_size;
1864         param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1865                                lum_size);
1866         if (param->fp_lmd == NULL) {
1867                 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1868                             "error: allocate %zu bytes for layout failed",
1869                             sizeof(lstat_t) + param->fp_lum_size);
1870                 return -ENOMEM;
1871         }
1872
1873         param->fp_lmv_stripe_count = 256;
1874         param->fp_lmv_md = calloc(1,
1875                                   lmv_user_md_size(param->fp_lmv_stripe_count,
1876                                                    LMV_USER_MAGIC_SPECIFIC));
1877         if (param->fp_lmv_md == NULL) {
1878                 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1879                             "error: allocation of %d bytes for ioctl",
1880                             lmv_user_md_size(param->fp_lmv_stripe_count,
1881                                              LMV_USER_MAGIC_SPECIFIC));
1882                 find_param_fini(param);
1883                 return -ENOMEM;
1884         }
1885
1886         param->fp_got_uuids = 0;
1887         param->fp_obd_indexes = NULL;
1888         param->fp_obd_index = OBD_NOT_FOUND;
1889         param->fp_mdt_index = OBD_NOT_FOUND;
1890         return 0;
1891 }
1892
1893 static int cb_common_fini(char *path, int p, int *dp, void *data,
1894                           struct dirent64 *de)
1895 {
1896         struct find_param *param = data;
1897
1898         param->fp_depth--;
1899         return 0;
1900 }
1901
1902 /* set errno upon failure */
1903 static int open_parent(const char *path)
1904 {
1905         char *path_copy;
1906         char *parent_path;
1907         int parent;
1908
1909         path_copy = strdup(path);
1910         if (path_copy == NULL)
1911                 return -1;
1912
1913         parent_path = dirname(path_copy);
1914         parent = open(parent_path, O_RDONLY|O_NDELAY|O_DIRECTORY);
1915         free(path_copy);
1916
1917         return parent;
1918 }
1919
1920 static int cb_get_dirstripe(char *path, int *d, struct find_param *param)
1921 {
1922         int ret;
1923         bool did_nofollow = false;
1924
1925         if (!d || *d < 0)
1926                 return -ENOTDIR;
1927 again:
1928         param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1929         if (param->fp_get_default_lmv)
1930                 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1931         else
1932                 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1933
1934         ret = ioctl(*d, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1935
1936         /* if ENOTTY likely to be a fake symlink, so try again after
1937          * new open() with O_NOFOLLOW, but only once to prevent any
1938          * loop like for the path of a file/dir not on Lustre !!
1939          */
1940         if (ret < 0 && errno == ENOTTY && !did_nofollow) {
1941                 int fd, ret2;
1942
1943                 did_nofollow = true;
1944                 fd = open(path, O_RDONLY | O_NOFOLLOW);
1945                 if (fd < 0) {
1946                         /* restore original errno */
1947                         errno = ENOTTY;
1948                         return ret;
1949                 }
1950
1951                 /* close original fd and set new */
1952                 close(*d);
1953                 *d = fd;
1954                 ret2 = ioctl(fd, LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1955                 if (ret2 < 0 && errno != E2BIG) {
1956                         /* restore original errno */
1957                         errno = ENOTTY;
1958                         return ret;
1959                 }
1960                 /* LMV is ok or need to handle E2BIG case now */
1961                 ret = ret2;
1962         }
1963
1964         if (errno == E2BIG && ret != 0) {
1965                 int stripe_count;
1966                 int lmv_size;
1967
1968                 /* if foreign LMV case, fake stripes number */
1969                 if (lmv_is_foreign(param->fp_lmv_md->lum_magic)) {
1970                         struct lmv_foreign_md *lfm;
1971
1972                         lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1973                         if (lfm->lfm_length < XATTR_SIZE_MAX -
1974                             offsetof(typeof(*lfm), lfm_value)) {
1975                                 uint32_t size = lfm->lfm_length +
1976                                              offsetof(typeof(*lfm), lfm_value);
1977
1978                                 stripe_count = lmv_foreign_to_md_stripes(size);
1979                         } else {
1980                                 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1981                                             "error: invalid %d foreign size returned from ioctl",
1982                                             lfm->lfm_length);
1983                                 return -EINVAL;
1984                         }
1985                 } else {
1986                         stripe_count = param->fp_lmv_md->lum_stripe_count;
1987                 }
1988                 if (stripe_count <= param->fp_lmv_stripe_count)
1989                         return ret;
1990
1991                 free(param->fp_lmv_md);
1992                 param->fp_lmv_stripe_count = stripe_count;
1993                 lmv_size = lmv_user_md_size(stripe_count,
1994                                             LMV_USER_MAGIC_SPECIFIC);
1995                 param->fp_lmv_md = malloc(lmv_size);
1996                 if (param->fp_lmv_md == NULL) {
1997                         llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1998                                     "error: allocation of %d bytes for ioctl",
1999                                     lmv_user_md_size(param->fp_lmv_stripe_count,
2000                                                      LMV_USER_MAGIC_SPECIFIC));
2001                         return -ENOMEM;
2002                 }
2003                 goto again;
2004         }
2005
2006         return ret;
2007 }
2008
2009 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
2010                               bool strict)
2011 {
2012         memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
2013         lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
2014         lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
2015         lmd_v2->lmd_stx.stx_uid = st->st_uid;
2016         lmd_v2->lmd_stx.stx_gid = st->st_gid;
2017         lmd_v2->lmd_stx.stx_mode = st->st_mode;
2018         lmd_v2->lmd_stx.stx_ino = st->st_ino;
2019         lmd_v2->lmd_stx.stx_size = st->st_size;
2020         lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
2021         lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
2022         lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
2023         lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
2024         lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
2025         lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
2026         lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
2027         lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
2028         lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
2029
2030         lmd_v2->lmd_flags = 0;
2031         if (strict) {
2032                 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
2033         } else {
2034                 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
2035                 if (lmd_v2->lmd_stx.stx_size)
2036                         lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
2037                 if (lmd_v2->lmd_stx.stx_blocks)
2038                         lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
2039         }
2040         lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
2041                              OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
2042                              OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
2043                              OBD_MD_FLRDEV;
2044
2045 }
2046
2047 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
2048 {
2049         struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
2050         struct lov_user_mds_data *lmd_v2 = lmdbuf;
2051         lstat_t st;
2052         int size;
2053
2054         size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
2055         if (size < 0)
2056                 return size;
2057
2058         if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
2059                 return -EOVERFLOW;
2060
2061         st = lmd_v1->lmd_st;
2062         memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
2063                 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
2064         convert_lmd_statx(lmd_v2, &st, false);
2065         lmd_v2->lmd_lmmsize = 0;
2066         lmd_v2->lmd_padding = 0;
2067
2068         return 0;
2069 }
2070
2071 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
2072                     void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
2073 {
2074         struct lov_user_mds_data *lmd = lmdbuf;
2075         static bool use_old_ioctl;
2076         unsigned long cmd;
2077         int ret = 0;
2078
2079         if (parent_fd < 0 && dir_fd < 0)
2080                 return -EINVAL;
2081         if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
2082                 return -EINVAL;
2083
2084         if (dir_fd >= 0) {
2085                 /*
2086                  * LL_IOC_MDC_GETINFO operates on the current directory inode
2087                  * and returns struct lov_user_mds_data, while
2088                  * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
2089                  */
2090                 if (type == GET_LMD_INFO)
2091                         cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_V1 :
2092                                               LL_IOC_MDC_GETINFO_V2;
2093                 else
2094                         cmd = LL_IOC_LOV_GETSTRIPE;
2095
2096 retry_getinfo:
2097                 ret = ioctl(dir_fd, cmd, lmdbuf);
2098                 if (ret < 0 && errno == ENOTTY &&
2099                     cmd == LL_IOC_MDC_GETINFO_V2) {
2100                         cmd = LL_IOC_MDC_GETINFO_V1;
2101                         use_old_ioctl = true;
2102                         goto retry_getinfo;
2103                 }
2104
2105                 if (cmd == LL_IOC_MDC_GETINFO_V1 && !ret)
2106                         ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2107
2108                 if (ret < 0 && errno == ENOTTY && type == GET_LMD_STRIPE) {
2109                         int dir_fd2;
2110
2111                         /* retry ioctl() after new open() with O_NOFOLLOW
2112                          * just in case it could be a fake symlink
2113                          * need using a new open() as dir_fd is being closed
2114                          * by caller
2115                          */
2116
2117                         dir_fd2 = open(path, O_RDONLY | O_NDELAY | O_NOFOLLOW);
2118                         if (dir_fd2 < 0) {
2119                                 /* return original error */
2120                                 errno = ENOTTY;
2121                         } else {
2122                                 ret = ioctl(dir_fd2, cmd, lmdbuf);
2123                                 /* pass new errno or success back to caller */
2124
2125                                 close(dir_fd2);
2126                         }
2127                 }
2128
2129         } else if (parent_fd >= 0) {
2130                 const char *fname = strrchr(path, '/');
2131
2132                 /*
2133                  * IOC_MDC_GETFILEINFO takes as input the filename (relative to
2134                  * the parent directory) and returns struct lov_user_mds_data,
2135                  * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2136                  *
2137                  * This avoids opening, locking, and closing each file on the
2138                  * client if that is not needed. Multiple of these ioctl() can
2139                  * be done on the parent dir with a single open for all
2140                  * files in that directory, and it also doesn't pollute the
2141                  * client dcache with millions of dentries when traversing
2142                  * a large filesystem.
2143                  */
2144                 fname = (fname == NULL ? path : fname + 1);
2145
2146                 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2147                 if (ret < 0)
2148                         errno = -ret;
2149                 else if (ret >= lmdlen || ret++ == 0)
2150                         errno = EINVAL;
2151                 else {
2152                         if (type == GET_LMD_INFO)
2153                                 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_V1 :
2154                                                       IOC_MDC_GETFILEINFO_V2;
2155                         else
2156                                 cmd = IOC_MDC_GETFILESTRIPE;
2157
2158 retry_getfileinfo:
2159                         ret = ioctl(parent_fd, cmd, lmdbuf);
2160                         if (ret < 0 && errno == ENOTTY &&
2161                             cmd == IOC_MDC_GETFILEINFO_V2) {
2162                                 cmd = IOC_MDC_GETFILEINFO_V1;
2163                                 use_old_ioctl = true;
2164                                 goto retry_getfileinfo;
2165                         }
2166
2167                         if (cmd == IOC_MDC_GETFILEINFO_V1 && !ret)
2168                                 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
2169                 }
2170         }
2171
2172         if (ret && type == GET_LMD_INFO) {
2173                 if (errno == ENOTTY) {
2174                         lstat_t st;
2175
2176                         /*
2177                          * ioctl is not supported, it is not a lustre fs.
2178                          * Do the regular lstat(2) instead.
2179                          */
2180                         ret = lstat_f(path, &st);
2181                         if (ret) {
2182                                 ret = -errno;
2183                                 llapi_error(LLAPI_MSG_ERROR, ret,
2184                                             "error: %s: lstat failed for %s",
2185                                             __func__, path);
2186                         }
2187
2188                         convert_lmd_statx(lmd, &st, true);
2189                         /*
2190                          * It may be wrong to set use_old_ioctl with true as
2191                          * the file is not a lustre fs. So reset it with false
2192                          * directly here.
2193                          */
2194                         use_old_ioctl = false;
2195                 } else if (errno == ENOENT) {
2196                         ret = -errno;
2197                         llapi_error(LLAPI_MSG_WARN, ret,
2198                                     "warning: %s does not exist", path);
2199                 } else if (errno != EISDIR && errno != ENODATA) {
2200                         ret = -errno;
2201                         llapi_error(LLAPI_MSG_ERROR, ret,
2202                                     "%s ioctl failed for %s.",
2203                                     dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2204                                     "IOC_MDC_GETFILEINFO", path);
2205                 }
2206         }
2207
2208         return ret;
2209 }
2210
2211 static int llapi_semantic_traverse(char *path, int size, int parent,
2212                                    semantic_func_t sem_init,
2213                                    semantic_func_t sem_fini, void *data,
2214                                    struct dirent64 *de)
2215 {
2216         struct find_param *param = (struct find_param *)data;
2217         struct dirent64 *dent;
2218         int len, ret, d, p = -1;
2219         DIR *dir = NULL;
2220
2221         ret = 0;
2222         len = strlen(path);
2223
2224         d = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
2225         /* if an invalid fake dir symlink, opendir() will return EINVAL
2226          * instead of ENOTDIR. If a valid but dangling faked or real file/dir
2227          * symlink ENOENT will be returned. For a valid/resolved fake or real
2228          * file symlink ENOTDIR will be returned as for a regular file.
2229          * opendir() will be successful for a  valid and resolved fake or real
2230          * dir simlink or a regular dir.
2231          */
2232         if (d == -1 && errno != ENOTDIR && errno != EINVAL && errno != ENOENT) {
2233                 ret = -errno;
2234                 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2235                             __func__, path);
2236                 return ret;
2237         } else if (d == -1) {
2238                 if (errno == ENOENT || errno == EINVAL) {
2239                         int old_errno = errno;
2240
2241                         /* try to open with O_NOFOLLOW this will help
2242                          * differentiate fake vs real symlinks
2243                          * it is ok to not use O_DIRECTORY with O_RDONLY
2244                          * and it will prevent the need to deal with ENOTDIR
2245                          * error, instead of ELOOP, being returned by recent
2246                          * kernels for real symlinks
2247                          */
2248                         d = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2249                         /* if a dangling real symlink should return ELOOP, or
2250                          * again ENOENT if really non-existing path, or E...??
2251                          * So return original error. If success or ENOTDIR, path
2252                          * is likely to be a fake dir/file symlink, so continue
2253                          */
2254                         if (d == -1) {
2255                                 ret =  -old_errno;
2256                                 goto out;
2257                         }
2258
2259                 }
2260
2261                 /* ENOTDIR */
2262                 if (parent == -1 && d == -1) {
2263                         /* Open the parent dir. */
2264                         p = open_parent(path);
2265                         if (p == -1) {
2266                                 ret = -errno;
2267                                 goto out;
2268                         }
2269                 }
2270         } else { /* d != -1 */
2271                 int d2;
2272
2273                 /* try to reopen dir with O_NOFOLLOW just in case of a foreign
2274                  * symlink dir
2275                  */
2276                 d2 = open(path, O_RDONLY|O_NDELAY|O_NOFOLLOW);
2277                 if (d2 != -1) {
2278                         close(d);
2279                         d = d2;
2280                 } else {
2281                         /* continue with d */
2282                         errno = 0;
2283                 }
2284         }
2285
2286         if (sem_init) {
2287                 ret = sem_init(path, (parent != -1) ? parent : p, &d, data, de);
2288                 if (ret)
2289                         goto err;
2290         }
2291
2292         if (d == -1)
2293                 goto out;
2294
2295         dir = fdopendir(d);
2296         if (dir == NULL) {
2297                 /* ENOTDIR if fake symlink, do not consider it as an error */
2298                 if (errno != ENOTDIR)
2299                         llapi_error(LLAPI_MSG_ERROR, errno,
2300                                     "fdopendir() failed");
2301                 else
2302                         errno = 0;
2303
2304                 goto out;
2305         }
2306
2307         while ((dent = readdir64(dir)) != NULL) {
2308                 int rc;
2309
2310                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2311                         continue;
2312
2313                 path[len] = 0;
2314                 if ((len + dent->d_reclen + 2) > size) {
2315                         llapi_err_noerrno(LLAPI_MSG_ERROR,
2316                                           "error: %s: string buffer too small for %s",
2317                                           __func__, path);
2318                         break;
2319                 }
2320                 strcat(path, "/");
2321                 strcat(path, dent->d_name);
2322
2323                 if (dent->d_type == DT_UNKNOWN) {
2324                         struct lov_user_mds_data *lmd = param->fp_lmd;
2325
2326                         rc = get_lmd_info_fd(path, d, -1, param->fp_lmd,
2327                                              param->fp_lum_size, GET_LMD_INFO);
2328                         if (rc == 0)
2329                                 dent->d_type = IFTODT(lmd->lmd_stx.stx_mode);
2330                         else if (ret == 0)
2331                                 ret = rc;
2332
2333                         if (rc == -ENOENT)
2334                                 continue;
2335                 }
2336                 switch (dent->d_type) {
2337                 case DT_UNKNOWN:
2338                         llapi_err_noerrno(LLAPI_MSG_ERROR,
2339                                           "error: %s: '%s' is UNKNOWN type %d",
2340                                           __func__, dent->d_name, dent->d_type);
2341                         break;
2342                 case DT_DIR:
2343                         rc = llapi_semantic_traverse(path, size, d, sem_init,
2344                                                       sem_fini, data, dent);
2345                         if (rc != 0 && ret == 0)
2346                                 ret = rc;
2347                         break;
2348                 default:
2349                         rc = 0;
2350                         if (sem_init) {
2351                                 rc = sem_init(path, d, NULL, data, dent);
2352                                 if (rc < 0 && ret == 0) {
2353                                         ret = rc;
2354                                         break;
2355                                 }
2356                         }
2357                         if (sem_fini && rc == 0)
2358                                 sem_fini(path, d, NULL, data, dent);
2359                 }
2360         }
2361
2362 out:
2363         path[len] = 0;
2364
2365         if (sem_fini)
2366                 sem_fini(path, parent, &d, data, de);
2367 err:
2368         if (d != -1) {
2369                 if (dir)
2370                         closedir(dir);
2371                 else
2372                         close(d);
2373         }
2374         if (p != -1)
2375                 close(p);
2376         return ret;
2377 }
2378
2379 static int param_callback(char *path, semantic_func_t sem_init,
2380                           semantic_func_t sem_fini, struct find_param *param)
2381 {
2382         int ret, len = strlen(path);
2383         char *buf;
2384
2385         if (len > PATH_MAX) {
2386                 ret = -EINVAL;
2387                 llapi_error(LLAPI_MSG_ERROR, ret,
2388                             "Path name '%s' is too long", path);
2389                 return ret;
2390         }
2391
2392         buf = (char *)malloc(2 * PATH_MAX);
2393         if (!buf)
2394                 return -ENOMEM;
2395
2396         snprintf(buf, PATH_MAX + 1, "%s", path);
2397         ret = common_param_init(param, buf);
2398         if (ret)
2399                 goto out;
2400
2401         param->fp_depth = 0;
2402
2403         ret = llapi_semantic_traverse(buf, 2 * PATH_MAX, -1, sem_init,
2404                                       sem_fini, param, NULL);
2405 out:
2406         find_param_fini(param);
2407         free(buf);
2408         return ret < 0 ? ret : 0;
2409 }
2410
2411 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2412 {
2413         int rc;
2414
2415         rc = ioctl(fd, OBD_IOC_GETDTNAME, lov_name);
2416         if (rc && errno == ENOTTY)
2417                 rc = ioctl(fd, OBD_IOC_GETNAME_OLD, lov_name);
2418         if (rc) {
2419                 rc = -errno;
2420                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2421         }
2422
2423         return rc;
2424 }
2425
2426 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2427 {
2428         int rc;
2429
2430         rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2431         if (rc) {
2432                 rc = -errno;
2433                 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2434         }
2435
2436         return rc;
2437 }
2438
2439 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2440 {
2441         int fd, rc;
2442
2443         /* do not follow faked symlinks */
2444         fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
2445         if (fd < 0) {
2446                 /* real symlink should have failed with ELOOP so retry without
2447                  * O_NOFOLLOW just in case
2448                  */
2449                 fd = open(path, O_RDONLY | O_NONBLOCK);
2450                 if (fd < 0) {
2451                         rc = -errno;
2452                         llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'",
2453                                     path);
2454                         return rc;
2455                 }
2456         }
2457
2458         rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2459
2460         close(fd);
2461         return rc;
2462 }
2463
2464 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2465 {
2466         int fd, rc;
2467
2468         fd = open(path, O_RDONLY | O_NONBLOCK);
2469         if (fd < 0) {
2470                 rc = -errno;
2471                 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2472                 return rc;
2473         }
2474
2475         rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2476
2477         close(fd);
2478         return rc;
2479 }
2480
2481 enum tgt_type {
2482         LOV_TYPE = 1,
2483         LMV_TYPE
2484 };
2485
2486 /*
2487  * If uuidp is NULL, return the number of available obd uuids.
2488  * If uuidp is non-NULL, then it will return the uuids of the obds. If
2489  * there are more OSTs than allocated to uuidp, then an error is returned with
2490  * the ost_count set to number of available obd uuids.
2491  */
2492 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2493                                   int *ost_count, enum tgt_type type)
2494 {
2495         char buf[PATH_MAX], format[32];
2496         int rc = 0, index = 0;
2497         struct obd_uuid name;
2498         glob_t param;
2499         FILE *fp;
2500
2501         /* Get the lov name */
2502         if (type == LOV_TYPE)
2503                 rc = llapi_file_fget_lov_uuid(fd, &name);
2504         else
2505                 rc = llapi_file_fget_lmv_uuid(fd, &name);
2506         if (rc != 0)
2507                 return rc;
2508
2509         /* Now get the ost uuids */
2510         rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2511                                    FILTER_BY_EXACT, "target_obd", &param);
2512         if (rc != 0)
2513                 return -ENOENT;
2514
2515         fp = fopen(param.gl_pathv[0], "r");
2516         if (fp == NULL) {
2517                 rc = -errno;
2518                 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2519                             param.gl_pathv[0]);
2520                 goto free_param;
2521         }
2522
2523         snprintf(format, sizeof(format),
2524                  "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2525         while (fgets(buf, sizeof(buf), fp) != NULL) {
2526                 if (uuidp && (index < *ost_count)) {
2527                         if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2528                                 break;
2529                 }
2530                 index++;
2531         }
2532
2533         fclose(fp);
2534
2535         if (uuidp && (index > *ost_count))
2536                 rc = -EOVERFLOW;
2537
2538         *ost_count = index;
2539 free_param:
2540         cfs_free_param_data(&param);
2541         return rc;
2542 }
2543
2544 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2545 {
2546         return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2547 }
2548
2549 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2550 {
2551         int root;
2552         int rc;
2553
2554         root = open(mnt, O_RDONLY | O_DIRECTORY);
2555         if (root < 0) {
2556                 rc = -errno;
2557                 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2558                 return rc;
2559         }
2560
2561         *count = is_mdt;
2562         rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2563         if (rc < 0)
2564                 rc = -errno;
2565
2566         close(root);
2567         return rc;
2568 }
2569
2570 /*
2571  * Check if user specified value matches a real uuid.  Ignore _UUID,
2572  * -osc-4ba41334, other trailing gunk in comparison.
2573  * @param real_uuid ends in "_UUID"
2574  * @param search_uuid may or may not end in "_UUID"
2575  */
2576 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2577 {
2578         int cmplen = strlen(real_uuid);
2579         int searchlen = strlen(search_uuid);
2580
2581         if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2582                 cmplen -= 5;
2583         if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2584                 searchlen -= 5;
2585
2586         /*
2587          * The UUIDs may legitimately be different lengths, if
2588          * the system was upgraded from an older version.
2589          */
2590         if (cmplen != searchlen)
2591                 return 0;
2592
2593         return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2594 }
2595
2596 /*
2597  * Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2598  * returned in param->fp_obd_index
2599  */
2600 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2601 {
2602         struct obd_uuid obd_uuid;
2603         char buf[PATH_MAX];
2604         glob_t param_data;
2605         char format[32];
2606         int rc = 0;
2607         FILE *fp;
2608
2609         if (param->fp_got_uuids)
2610                 return rc;
2611
2612         /* Get the lov/lmv name */
2613         if (param->fp_get_lmv)
2614                 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2615         else
2616                 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2617         if (rc) {
2618                 if (rc != -ENOTTY) {
2619                         llapi_error(LLAPI_MSG_ERROR, rc,
2620                                     "error: can't get %s name: %s",
2621                                     param->fp_get_lmv ? "lmv" : "lov",
2622                                     dname);
2623                 } else {
2624                         rc = 0;
2625                 }
2626                 return rc;
2627         }
2628
2629         param->fp_got_uuids = 1;
2630
2631         /* Now get the ost uuids */
2632         rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2633                                    obd_uuid.uuid, FILTER_BY_EXACT,
2634                                    "target_obd", &param_data);
2635         if (rc != 0)
2636                 return -ENOENT;
2637
2638         fp = fopen(param_data.gl_pathv[0], "r");
2639         if (fp == NULL) {
2640                 rc = -errno;
2641                 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2642                             param_data.gl_pathv[0]);
2643                 goto free_param;
2644         }
2645
2646         if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2647                 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2648                              param->fp_get_lmv ? "MDTS" : "OBDS");
2649
2650         snprintf(format, sizeof(format),
2651                  "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2652         while (fgets(buf, sizeof(buf), fp) != NULL) {
2653                 int index;
2654
2655                 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2656                         break;
2657
2658                 if (param->fp_obd_uuid) {
2659                         if (llapi_uuid_match(obd_uuid.uuid,
2660                                              param->fp_obd_uuid->uuid)) {
2661                                 param->fp_obd_index = index;
2662                                 break;
2663                         }
2664                 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2665                         /* Print everything */
2666                         llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2667                 }
2668         }
2669         param->fp_obds_printed = 1;
2670
2671         fclose(fp);
2672
2673         if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2674                 llapi_err_noerrno(LLAPI_MSG_ERROR,
2675                                   "error: %s: unknown obduuid: %s",
2676                                   __func__, param->fp_obd_uuid->uuid);
2677                 rc = -EINVAL;
2678         }
2679 free_param:
2680         cfs_free_param_data(&param_data);
2681         return rc;
2682 }
2683
2684 /*
2685  * In this case, param->fp_obd_uuid will be an array of obduuids and
2686  * obd index for all these obduuids will be returned in
2687  * param->fp_obd_indexes
2688  */
2689 static int setup_indexes(int d, char *path, struct obd_uuid *obduuids,
2690                          int num_obds, int **obdindexes, int *obdindex,
2691                          enum tgt_type type)
2692 {
2693         int ret, obdcount, obd_valid = 0, obdnum;
2694         long i;
2695         struct obd_uuid *uuids = NULL;
2696         char buf[16];
2697         int *indexes;
2698
2699         if (type == LOV_TYPE)
2700                 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2701         else
2702                 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2703         if (ret != 0)
2704                 return ret;
2705
2706         obdcount = atoi(buf);
2707         uuids = malloc(obdcount * sizeof(struct obd_uuid));
2708         if (uuids == NULL)
2709                 return -ENOMEM;
2710
2711 retry_get_uuids:
2712         ret = llapi_get_target_uuids(d, uuids, &obdcount, type);
2713         if (ret) {
2714                 if (ret == -EOVERFLOW) {
2715                         struct obd_uuid *uuids_temp;
2716
2717                         uuids_temp = realloc(uuids, obdcount *
2718                                              sizeof(struct obd_uuid));
2719                         if (uuids_temp != NULL) {
2720                                 uuids = uuids_temp;
2721                                 goto retry_get_uuids;
2722                         }
2723                         ret = -ENOMEM;
2724                 }
2725
2726                 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2727                 goto out_free;
2728         }
2729
2730         indexes = malloc(num_obds * sizeof(*obdindex));
2731         if (indexes == NULL) {
2732                 ret = -ENOMEM;
2733                 goto out_free;
2734         }
2735
2736         for (obdnum = 0; obdnum < num_obds; obdnum++) {
2737                 char *end = NULL;
2738
2739                 /* The user may have specified a simple index */
2740                 i = strtol(obduuids[obdnum].uuid, &end, 0);
2741                 if (end && *end == '\0' && i < obdcount) {
2742                         indexes[obdnum] = i;
2743                         obd_valid++;
2744                 } else {
2745                         for (i = 0; i < obdcount; i++) {
2746                                 if (llapi_uuid_match(uuids[i].uuid,
2747                                                      obduuids[obdnum].uuid)) {
2748                                         indexes[obdnum] = i;
2749                                         obd_valid++;
2750                                         break;
2751                                 }
2752                         }
2753                 }
2754                 if (i >= obdcount) {
2755                         indexes[obdnum] = OBD_NOT_FOUND;
2756                         llapi_err_noerrno(LLAPI_MSG_ERROR,
2757                                           "invalid obduuid '%s'",
2758                                           obduuids[obdnum].uuid);
2759                         ret = -EINVAL;
2760                 }
2761         }
2762
2763         if (obd_valid == 0)
2764                 *obdindex = OBD_NOT_FOUND;
2765         else
2766                 *obdindex = obd_valid;
2767
2768         *obdindexes = indexes;
2769 out_free:
2770         if (uuids)
2771                 free(uuids);
2772
2773         return ret;
2774 }
2775
2776 static int setup_target_indexes(int d, char *path, struct find_param *param)
2777 {
2778         int ret = 0;
2779
2780         if (param->fp_mdt_uuid) {
2781                 ret = setup_indexes(d, path, param->fp_mdt_uuid,
2782                                     param->fp_num_mdts,
2783                                     &param->fp_mdt_indexes,
2784                                     &param->fp_mdt_index, LMV_TYPE);
2785                 if (ret)
2786                         return ret;
2787         }
2788
2789         if (param->fp_obd_uuid) {
2790                 ret = setup_indexes(d, path, param->fp_obd_uuid,
2791                                     param->fp_num_obds,
2792                                     &param->fp_obd_indexes,
2793                                     &param->fp_obd_index, LOV_TYPE);
2794                 if (ret)
2795                         return ret;
2796         }
2797
2798         param->fp_got_uuids = 1;
2799
2800         return ret;
2801 }
2802
2803 int llapi_ostlist(char *path, struct find_param *param)
2804 {
2805         int fd;
2806         int ret;
2807
2808         fd = open(path, O_RDONLY | O_DIRECTORY);
2809         if (fd < 0)
2810                 return -errno;
2811
2812         ret = setup_obd_uuid(fd, path, param);
2813         close(fd);
2814
2815         return ret;
2816 }
2817
2818 /*
2819  * Tries to determine the default stripe attributes for a given filesystem. The
2820  * filesystem to check should be specified by fsname, or will be determined
2821  * using pathname.
2822  */
2823 static int sattr_get_defaults(const char *const fsname,
2824                               unsigned int *scount,
2825                               unsigned int *ssize,
2826                               unsigned int *soffset)
2827 {
2828         char val[PATH_MAX];
2829         int rc;
2830
2831         if (scount) {
2832                 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2833                                             "stripecount", val, sizeof(val));
2834                 if (rc != 0)
2835                         return rc;
2836                 *scount = atoi(val);
2837         }
2838
2839         if (ssize) {
2840                 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2841                                             "stripesize", val, sizeof(val));
2842                 if (rc != 0)
2843                         return rc;
2844                 *ssize = atoi(val);
2845         }
2846
2847         if (soffset) {
2848                 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2849                                             "stripeoffset", val, sizeof(val));
2850                 if (rc != 0)
2851                         return rc;
2852                 *soffset = atoi(val);
2853         }
2854
2855         return 0;
2856 }
2857
2858 /*
2859  * Tries to gather the default stripe attributes for a given filesystem. If
2860  * the attributes can be determined, they are cached for easy retreival the
2861  * next time they are needed. Only a single filesystem's attributes are
2862  * cached at a time.
2863  */
2864 int sattr_cache_get_defaults(const char *const fsname,
2865                              const char *const pathname, unsigned int *scount,
2866                              unsigned int *ssize, unsigned int *soffset)
2867 {
2868         static struct {
2869                 char fsname[PATH_MAX + 1];
2870                 unsigned int stripecount;
2871                 unsigned int stripesize;
2872                 unsigned int stripeoffset;
2873         } cache = {
2874                 .fsname = {'\0'}
2875         };
2876
2877         int rc;
2878         char fsname_buf[PATH_MAX + 1];
2879         unsigned int tmp[3];
2880
2881         if (fsname == NULL) {
2882                 rc = llapi_search_fsname(pathname, fsname_buf);
2883                 if (rc)
2884                         return rc;
2885         } else {
2886                 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2887         }
2888
2889         if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2890                 /*
2891                  * Ensure all 3 sattrs (count, size, and offset) are
2892                  * successfully retrieved and stored in tmp before writing to
2893                  * cache.
2894                  */
2895                 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2896                 if (rc != 0)
2897                         return rc;
2898
2899                 cache.stripecount = tmp[0];
2900                 cache.stripesize = tmp[1];
2901                 cache.stripeoffset = tmp[2];
2902                 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2903         }
2904
2905         if (scount)
2906                 *scount = cache.stripecount;
2907         if (ssize)
2908                 *ssize = cache.stripesize;
2909         if (soffset)
2910                 *soffset = cache.stripeoffset;
2911
2912         return 0;
2913 }
2914
2915 static char *layout2name(__u32 layout_pattern)
2916 {
2917         if (layout_pattern & LOV_PATTERN_F_RELEASED)
2918                 return "released";
2919         else if (layout_pattern == LOV_PATTERN_MDT)
2920                 return "mdt";
2921         else if (layout_pattern == LOV_PATTERN_RAID0)
2922                 return "raid0";
2923         else if (layout_pattern ==
2924                         (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING))
2925                 return "raid0,overstriped";
2926         else
2927                 return "unknown";
2928 }
2929
2930 enum lov_dump_flags {
2931         LDF_IS_DIR      = 0x0001,
2932         LDF_IS_RAW      = 0x0002,
2933         LDF_INDENT      = 0x0004,
2934         LDF_SKIP_OBJS   = 0x0008,
2935         LDF_YAML        = 0x0010,
2936         LDF_EXTENSION   = 0x0020,
2937 };
2938
2939 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2940                                      struct lov_user_ost_data_v1 *objects,
2941                                      enum llapi_layout_verbose verbose,
2942                                      int depth, char *pool_name,
2943                                      enum lov_dump_flags flags)
2944 {
2945         bool is_dir = flags & LDF_IS_DIR;
2946         bool is_raw = flags & LDF_IS_RAW;
2947         bool indent = flags & LDF_INDENT;
2948         bool yaml = flags & LDF_YAML;
2949         bool skip_objs = flags & LDF_SKIP_OBJS;
2950         bool extension = flags & LDF_EXTENSION;
2951         char *prefix = is_dir ? "" : "lmm_";
2952         char *separator = "";
2953         char *space = indent ? "      " : "";
2954         int rc;
2955
2956         if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2957                 lmm_oi_set_seq(&lum->lmm_oi, 0);
2958                 if (!indent && (verbose & VERBOSE_DETAIL))
2959                         llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2960         }
2961
2962         if (!yaml && !indent && depth && path &&
2963             ((verbose != VERBOSE_OBJID) || !is_dir))
2964                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2965
2966         if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2967                 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic:         0x%08X\n",
2968                              space, prefix, lum->lmm_magic);
2969                 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq:           %#jx\n",
2970                              space, prefix,
2971                              (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2972                 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id:     %#jx\n",
2973                              space, prefix,
2974                              (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2975         }
2976
2977         if (verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) {
2978                 __u64 seq;
2979                 __u32 oid;
2980                 __u32 ver;
2981
2982                 if (verbose & ~VERBOSE_DFID)
2983                         llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid:           ",
2984                                      space);
2985
2986                 if (is_dir) {
2987                         struct lu_fid dir_fid;
2988
2989                         rc = llapi_path2fid(path, &dir_fid);
2990                         if (rc)
2991                                 llapi_error(LLAPI_MSG_ERROR, rc,
2992                                             "Cannot determine directory fid.");
2993
2994                         seq = dir_fid.f_seq;
2995                         oid = dir_fid.f_oid;
2996                         ver = dir_fid.f_ver;
2997                 } else {
2998                         /*
2999                          * This needs a bit of hand-holding since old 1.x
3000                          * lmm_oi have { oi.oi_id = mds_inum, oi.oi_seq = 0 }
3001                          * and 2.x lmm_oi have { oi.oi_id = mds_oid,
3002                          * oi.oi_seq = mds_seq } instead of a real FID.
3003                          * Ideally the 2.x code would have stored this like a
3004                          * FID with { oi_id = mds_seq, oi_seq = mds_oid } so
3005                          * the ostid union lu_fid { f_seq = mds_seq,
3006                          * f_oid = mds_oid } worked properly (especially since
3007                          * IGIF FIDs use mds_inum as the FID SEQ), but
3008                          * unfortunately that didn't happen.
3009                          *
3010                          * Print it to look like an IGIF FID, even though the
3011                          * fields are reversed on disk, so that it makes sense
3012                          * to userspace.
3013                          *
3014                          * Don't use ostid_id() and ostid_seq(), since they
3015                          * assume the oi_fid fields are in the right order.
3016                          * This is why there are separate lmm_oi_seq() and
3017                          * lmm_oi_id() routines for this.
3018                          *
3019                          * For newer layout types hopefully this will be a
3020                          * real FID.
3021                          */
3022                         seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3023                                 lmm_oi_id(&lum->lmm_oi) :
3024                                 lmm_oi_seq(&lum->lmm_oi);
3025                         oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
3026                             0 : (__u32)lmm_oi_id(&lum->lmm_oi);
3027                         ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
3028                 }
3029
3030                 if (yaml)
3031                         llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
3032                                      (unsigned long long)seq, oid, ver);
3033                 else
3034                         llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
3035                                      (unsigned long long)seq, oid, ver);
3036         }
3037
3038         if (verbose & VERBOSE_STRIPE_COUNT) {
3039                 if (verbose & ~VERBOSE_STRIPE_COUNT)
3040                         llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count:  ",
3041                                      space, prefix);
3042                 if (is_dir) {
3043                         if (!is_raw && lum->lmm_stripe_count == 0 &&
3044                             lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
3045                                 unsigned int scount;
3046
3047                                 rc = sattr_cache_get_defaults(NULL, path,
3048                                                               &scount, NULL,
3049                                                               NULL);
3050                                 if (rc == 0)
3051                                         llapi_printf(LLAPI_MSG_NORMAL, "%d",
3052                                                      scount);
3053                                 else
3054                                         llapi_error(LLAPI_MSG_ERROR, rc,
3055                                                     "Cannot determine default stripe count.");
3056                         } else {
3057                                 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3058                                              extension ? 0 :
3059                                              (__s16)lum->lmm_stripe_count);
3060                         }
3061                 } else {
3062                         llapi_printf(LLAPI_MSG_NORMAL, "%hd",
3063                                      extension ? 0 :
3064                                      (__s16)lum->lmm_stripe_count);
3065                 }
3066                 if (!yaml && is_dir)
3067                         separator = " ";
3068                 else
3069                         separator = "\n";
3070         }
3071
3072         if (((verbose & VERBOSE_STRIPE_SIZE) && !extension) ||
3073             ((verbose & VERBOSE_EXT_SIZE) && extension)) {
3074                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3075                 if (verbose & ~VERBOSE_EXT_SIZE && extension)
3076                         llapi_printf(LLAPI_MSG_NORMAL, "%s%sextension_size: ",
3077                                      space, prefix);
3078                 if (verbose & ~VERBOSE_STRIPE_SIZE && !extension)
3079                         llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size:   ",
3080                                      space, prefix);
3081                 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
3082                         unsigned int ssize;
3083
3084                         rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
3085                                                       NULL);
3086                         if (rc == 0)
3087                                 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
3088                         else
3089                                 llapi_error(LLAPI_MSG_ERROR, rc,
3090                                             "Cannot determine default stripe size.");
3091                 } else {
3092                         /* Extension size is in KiB */
3093                         llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3094                                      extension ?
3095                                      (unsigned long long)(lum->lmm_stripe_size * SEL_UNIT_SIZE) :
3096                                      (unsigned long long)lum->lmm_stripe_size);
3097                 }
3098                 if (!yaml && is_dir)
3099                         separator = " ";
3100                 else
3101                         separator = "\n";
3102         }
3103
3104         if ((verbose & VERBOSE_PATTERN)) {
3105                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3106                 if (verbose & ~VERBOSE_PATTERN)
3107                         llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern:       ",
3108                                      space, prefix);
3109                 if (lov_pattern_supported(lum->lmm_pattern))
3110                         llapi_printf(LLAPI_MSG_NORMAL, "%s",
3111                                      layout2name(lum->lmm_pattern));
3112                 else
3113                         llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
3114                 separator = (!yaml && is_dir) ? " " : "\n";
3115         }
3116
3117         if ((verbose & VERBOSE_GENERATION) && !is_dir) {
3118                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3119                 if (verbose & ~VERBOSE_GENERATION)
3120                         llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen:    ",
3121                                      space, prefix);
3122                 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3123                              skip_objs ? 0 : (int)lum->lmm_layout_gen);
3124                 separator = "\n";
3125         }
3126
3127         if (verbose & VERBOSE_STRIPE_OFFSET) {
3128                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3129                 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3130                         llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
3131                                      space, prefix);
3132                 if (is_dir || skip_objs)
3133                         llapi_printf(LLAPI_MSG_NORMAL, "%d",
3134                                      lum->lmm_stripe_offset ==
3135                                      (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
3136                                      lum->lmm_stripe_offset);
3137                 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
3138                         llapi_printf(LLAPI_MSG_NORMAL, "0");
3139                 else
3140                         llapi_printf(LLAPI_MSG_NORMAL, "%u",
3141                                      objects[0].l_ost_idx);
3142                 if (!yaml && is_dir)
3143                         separator = " ";
3144                 else
3145                         separator = "\n";
3146         }
3147
3148         if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
3149                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3150                 if (verbose & ~VERBOSE_POOL)
3151                         llapi_printf(LLAPI_MSG_NORMAL, "%s%spool:          ",
3152                                      space, prefix);
3153                 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
3154                 if (!yaml && is_dir)
3155                         separator = " ";
3156                 else
3157                         separator = "\n";
3158         }
3159
3160         if (strlen(separator) != 0)
3161                 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3162 }
3163
3164 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
3165                             struct lov_user_ost_data_v1 *objects,
3166                             char *path, int obdindex, int depth,
3167                             enum llapi_layout_verbose verbose,
3168                             enum lov_dump_flags flags)
3169 {
3170         bool is_dir = flags & LDF_IS_DIR;
3171         bool indent = flags & LDF_INDENT;
3172         bool skip_objs = flags & LDF_SKIP_OBJS;
3173         bool yaml = flags & LDF_YAML;
3174         bool obdstripe = obdindex == OBD_NOT_FOUND;
3175         int i;
3176
3177         if (!obdstripe && !skip_objs) {
3178                 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
3179                         if (obdindex == objects[i].l_ost_idx) {
3180                                 obdstripe = true;
3181                                 break;
3182                         }
3183                 }
3184         }
3185
3186         if (!obdstripe)
3187                 return;
3188
3189         lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
3190                                  flags);
3191
3192         if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
3193             !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
3194               lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
3195                 char *space = "      - ";
3196
3197                 if (indent)
3198                         llapi_printf(LLAPI_MSG_NORMAL,
3199                                      "%6slmm_objects:\n", " ");
3200                 else if (yaml)
3201                         llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
3202                 else
3203                         llapi_printf(LLAPI_MSG_NORMAL,
3204                                 "\tobdidx\t\t objid\t\t objid\t\t group\n");
3205
3206                 for (i = 0; i < lum->lmm_stripe_count; i++) {
3207                         int idx = objects[i].l_ost_idx;
3208                         long long oid = ostid_id(&objects[i].l_ost_oi);
3209                         long long gr = ostid_seq(&objects[i].l_ost_oi);
3210
3211                         if (obdindex != OBD_NOT_FOUND && obdindex != idx)
3212                                 continue;
3213
3214                         if (yaml) {
3215                                 struct lu_fid fid = { 0 };
3216
3217                                 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3218                                 llapi_printf(LLAPI_MSG_NORMAL,
3219                                     "%sl_ost_idx: %d\n", space, idx);
3220                                 llapi_printf(LLAPI_MSG_NORMAL,
3221                                     "%8sl_fid:     "DFID_NOBRACE"\n",
3222                                     " ", PFID(&fid));
3223                         } else if (indent) {
3224                                 struct lu_fid fid = { 0 };
3225
3226                                 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
3227                                 llapi_printf(LLAPI_MSG_NORMAL,
3228                                     "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
3229                                     space, i, idx, PFID(&fid));
3230                         } else {
3231                                 char fmt[48];
3232
3233                                 sprintf(fmt, "%s%s%s\n",
3234                                         "\t%6u\t%14llu\t%#13llx\t",
3235                                         (fid_seq_is_rsvd(gr) ||
3236                                          fid_seq_is_mdt0(gr)) ?
3237                                          "%14llu" : "%#14llx", "%s");
3238                                 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
3239                                              oid, gr,
3240                                              obdindex == idx ? " *" : "");
3241                         }
3242                 }
3243         }
3244         llapi_printf(LLAPI_MSG_NORMAL, "\n");
3245 }
3246
3247 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
3248                        char *path, int obdindex, int depth,
3249                        enum llapi_layout_verbose verbose,
3250                        enum lov_dump_flags flags)
3251 {
3252         struct lmv_user_mds_data *objects = lum->lum_objects;
3253         char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
3254         char *separator = "";
3255         bool yaml = flags & LDF_YAML;
3256         bool obdstripe = false;
3257         int i;
3258
3259         if (obdindex != OBD_NOT_FOUND) {
3260                 if (lum->lum_stripe_count == 0) {
3261                         if (obdindex == lum->lum_stripe_offset)
3262                                 obdstripe = true;
3263                 } else {
3264                         for (i = 0; i < lum->lum_stripe_count; i++) {
3265                                 if (obdindex == objects[i].lum_mds) {
3266                                         llapi_printf(LLAPI_MSG_NORMAL,
3267                                                      "%s%s\n", prefix,
3268                                                      path);
3269                                         obdstripe = true;
3270                                         break;
3271                                 }
3272                         }
3273                 }
3274         } else {
3275                 obdstripe = true;
3276         }
3277
3278         if (!obdstripe)
3279                 return;
3280
3281         /* show all information default */
3282         if (!verbose) {
3283                 if (lum->lum_magic == LMV_USER_MAGIC)
3284                         verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3285                                   VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3286                 else
3287                         verbose = VERBOSE_OBJID;
3288         }
3289
3290         if (depth && path && ((verbose != VERBOSE_OBJID)))
3291                 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3292
3293         if (verbose & VERBOSE_STRIPE_COUNT) {
3294                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3295                 if (verbose & ~VERBOSE_STRIPE_COUNT)
3296                         llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3297                 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3298                              (int)lum->lum_stripe_count);
3299                 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3300                         separator = " ";
3301                 else
3302                         separator = "\n";
3303         }
3304
3305         if (verbose & VERBOSE_STRIPE_OFFSET) {
3306                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3307                 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3308                         llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3309                 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3310                              (int)lum->lum_stripe_offset);
3311                 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3312                         separator = " ";
3313                 else
3314                         separator = "\n";
3315         }
3316
3317         if (verbose & VERBOSE_HASH_TYPE) {
3318                 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3319                 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3320
3321                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3322                 if (verbose & ~VERBOSE_HASH_TYPE)
3323                         llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3324                 if (type < LMV_HASH_TYPE_MAX)
3325                         llapi_printf(LLAPI_MSG_NORMAL, "%s",
3326                                      mdt_hash_name[type]);
3327                 else
3328                         llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3329
3330                 if (flags & LMV_HASH_FLAG_MIGRATION)
3331                         llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3332                 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3333                         llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3334                 if (flags & LMV_HASH_FLAG_LOST_LMV)
3335                         llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3336                 if (flags & LMV_HASH_FLAG_FIXED)
3337                         llapi_printf(LLAPI_MSG_NORMAL, ",fixed");
3338                 if (flags & ~LMV_HASH_FLAG_KNOWN)
3339                         llapi_printf(LLAPI_MSG_NORMAL, ",unknown_%04x",
3340                                      flags & ~LMV_HASH_FLAG_KNOWN);
3341
3342                 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3343                         separator = " ";
3344                 else
3345                         separator = "\n";
3346         }
3347
3348         if ((verbose & VERBOSE_INHERIT) && lum->lum_magic == LMV_USER_MAGIC) {
3349                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3350                 if (verbose & ~VERBOSE_INHERIT)
3351                         llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit: ");
3352                 if (lum->lum_max_inherit == LMV_INHERIT_UNLIMITED)
3353                         llapi_printf(LLAPI_MSG_NORMAL, "-1");
3354                 else if (lum->lum_max_inherit == LMV_INHERIT_NONE)
3355                         llapi_printf(LLAPI_MSG_NORMAL, "0");
3356                 else
3357                         llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3358                                      lum->lum_max_inherit);
3359                 if (verbose & VERBOSE_INHERIT && !yaml)
3360                         separator = " ";
3361                 else
3362                         separator = "\n";
3363         }
3364
3365         if ((verbose & VERBOSE_INHERIT_RR) &&
3366             lum->lum_magic == LMV_USER_MAGIC &&
3367             lum->lum_stripe_offset == LMV_OFFSET_DEFAULT) {
3368                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3369                 if (verbose & ~VERBOSE_INHERIT_RR)
3370                         llapi_printf(LLAPI_MSG_NORMAL, "lmv_max_inherit_rr: ");
3371                 if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED)
3372                         llapi_printf(LLAPI_MSG_NORMAL, "-1");
3373                 else if (lum->lum_max_inherit_rr == LMV_INHERIT_RR_NONE)
3374                         llapi_printf(LLAPI_MSG_NORMAL, "0");
3375                 else
3376                         llapi_printf(LLAPI_MSG_NORMAL, "%hhu",
3377                                      lum->lum_max_inherit_rr);
3378                 if (verbose & VERBOSE_INHERIT_RR && !yaml)
3379                         separator = " ";
3380                 else
3381                         separator = "\n";
3382         }
3383
3384         separator = "\n";
3385
3386         if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3387                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3388                 if (lum->lum_stripe_count > 0)
3389                         llapi_printf(LLAPI_MSG_NORMAL,
3390                                      "mdtidx\t\t FID[seq:oid:ver]\n");
3391                 for (i = 0; i < lum->lum_stripe_count; i++) {
3392                         int idx = objects[i].lum_mds;
3393                         struct lu_fid *fid = &objects[i].lum_fid;
3394
3395                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3396                                 llapi_printf(LLAPI_MSG_NORMAL,
3397                                              "%6u\t\t "DFID"\t\t%s\n",
3398                                             idx, PFID(fid),
3399                                             obdindex == idx ? " *" : "");
3400                 }
3401         }
3402
3403         if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3404              pool_name[0] != '\0') {
3405                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3406                 if (verbose & ~VERBOSE_POOL)
3407                         llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool:           ",
3408                                      prefix);
3409                 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3410                 separator = "\n";
3411         }
3412
3413         if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3414                 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3415 }
3416
3417 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3418                                     enum lov_dump_flags flags)
3419 {
3420         struct lov_comp_md_v1 *comp_v1 = (void *)&param->fp_lmd->lmd_lmm;
3421         int depth = param->fp_max_depth;
3422         enum llapi_layout_verbose verbose = param->fp_verbose;
3423         bool yaml = flags & LDF_YAML;
3424
3425         if (depth && path && ((verbose != VERBOSE_OBJID) ||
3426                               !(flags & LDF_IS_DIR)) && !yaml)
3427                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3428
3429         if (verbose & VERBOSE_DETAIL) {
3430                 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3431                 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic:         0x%08X\n",
3432                              " ", comp_v1->lcm_magic);
3433                 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size:          %u\n",
3434                              " ", comp_v1->lcm_size);
3435                 if (flags & LDF_IS_DIR)
3436                         llapi_printf(LLAPI_MSG_NORMAL,
3437                                      "%2slcm_flags:         %s\n", " ",
3438                                      comp_v1->lcm_mirror_count > 0 ?
3439                                                         "mirrored" : "");
3440                 else
3441                         llapi_printf(LLAPI_MSG_NORMAL,
3442                                      "%2slcm_flags:         %s\n", " ",
3443                                 llapi_layout_flags_string(comp_v1->lcm_flags));
3444         }
3445
3446         if (verbose & VERBOSE_GENERATION) {
3447                 if (verbose & ~VERBOSE_GENERATION)
3448                         llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen:    ",
3449                                      " ");
3450                 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3451         }
3452
3453         if (verbose & VERBOSE_MIRROR_COUNT) {
3454                 if (verbose & ~VERBOSE_MIRROR_COUNT)
3455                         llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count:  ",
3456                                      " ");
3457                 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3458                              comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3459                              comp_v1->lcm_mirror_count + 1 : 1);
3460         }
3461
3462         if (verbose & VERBOSE_COMP_COUNT) {
3463                 if (verbose & ~VERBOSE_COMP_COUNT)
3464                         llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count:   ",
3465                                      " ");
3466                 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3467                              comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3468                              comp_v1->lcm_entry_count : 0);
3469         }
3470
3471         if (verbose & VERBOSE_DETAIL && !yaml)
3472                 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3473 }
3474
3475 static void lcme_flags2str(__u32 comp_flags)
3476 {
3477         bool found = false;
3478         int i = 0;
3479
3480         if (!comp_flags) {
3481                 llapi_printf(LLAPI_MSG_NORMAL, "0");
3482                 return;
3483         }
3484         for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3485                 if (comp_flags & comp_flags_table[i].cfn_flag) {
3486                         if (found)
3487                                 llapi_printf(LLAPI_MSG_NORMAL, ",");
3488                         llapi_printf(LLAPI_MSG_NORMAL, "%s",
3489                                      comp_flags_table[i].cfn_name);
3490                         comp_flags &= ~comp_flags_table[i].cfn_flag;
3491                         found = true;
3492                 }
3493         }
3494         if (comp_flags) {
3495                 if (found)
3496                         llapi_printf(LLAPI_MSG_NORMAL, ",");
3497                 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3498         }
3499 }
3500
3501 static void lov_dump_comp_v1_entry(struct find_param *param,
3502                                    enum lov_dump_flags flags, int index)
3503 {
3504         struct lov_comp_md_v1 *comp_v1 = (void *)&param->fp_lmd->lmd_lmm;
3505         struct lov_comp_md_entry_v1 *entry;
3506         char *separator = "";
3507         enum llapi_layout_verbose verbose = param->fp_verbose;
3508         bool yaml = flags & LDF_YAML;
3509
3510         entry = &comp_v1->lcm_entries[index];
3511
3512         if (yaml)
3513                 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3514
3515         if (verbose & VERBOSE_COMP_ID) {
3516                 if (verbose & VERBOSE_DETAIL && !yaml)
3517                         llapi_printf(LLAPI_MSG_NORMAL,
3518                                      "%slcme_id:             ", "  - ");
3519                 else if (verbose & ~VERBOSE_COMP_ID)
3520                         llapi_printf(LLAPI_MSG_NORMAL,
3521                                      "%4slcme_id:             ", " ");
3522                 if (entry->lcme_id != LCME_ID_INVAL)
3523                         llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3524                 else
3525                         llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3526                 separator = "\n";
3527         }
3528
3529         if (verbose & VERBOSE_MIRROR_ID) {
3530                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3531                 if (verbose & ~VERBOSE_MIRROR_ID)
3532                         llapi_printf(LLAPI_MSG_NORMAL,
3533                                      "%4slcme_mirror_id:      ", " ");
3534                 if (entry->lcme_id != LCME_ID_INVAL)
3535                         llapi_printf(LLAPI_MSG_NORMAL, "%u",
3536                                      mirror_id_of(entry->lcme_id));
3537                 else
3538                         llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3539                 separator = "\n";
3540         }
3541
3542         if (verbose & VERBOSE_COMP_FLAGS) {
3543                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3544                 if (verbose & ~VERBOSE_COMP_FLAGS)
3545                         llapi_printf(LLAPI_MSG_NORMAL,
3546                                      "%4slcme_flags:          ", " ");
3547                 lcme_flags2str(entry->lcme_flags);
3548                 separator = "\n";
3549         }
3550         /* print snapshot timestamp if its a nosync comp */
3551         if ((verbose & VERBOSE_COMP_FLAGS) &&
3552             (entry->lcme_flags & LCME_FL_NOSYNC)) {
3553                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3554                 if (verbose & ~VERBOSE_COMP_FLAGS)
3555                         llapi_printf(LLAPI_MSG_NORMAL,
3556                                      "%4slcme_timestamp:      ", " ");
3557                 if (yaml) {
3558                         llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3559                                      (unsigned long long)entry->lcme_timestamp);
3560                 } else {
3561                         time_t stamp = entry->lcme_timestamp;
3562                         char *date_str = asctime(localtime(&stamp));
3563
3564                         date_str[strlen(date_str) - 1] = '\0';
3565                         llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3566                 }
3567
3568                 separator = "\n";
3569         }
3570
3571         if (verbose & VERBOSE_COMP_START) {
3572                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3573                 if (verbose & ~VERBOSE_COMP_START)
3574                         llapi_printf(LLAPI_MSG_NORMAL,
3575                                      "%4slcme_extent.e_start: ", " ");
3576                 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3577                              (unsigned long long)entry->lcme_extent.e_start);
3578                 separator = "\n";
3579         }
3580
3581         if (verbose & VERBOSE_COMP_END) {
3582                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3583                 if (verbose & ~VERBOSE_COMP_END)
3584                         llapi_printf(LLAPI_MSG_NORMAL,
3585                                      "%4slcme_extent.e_end:   ", " ");
3586                 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3587                         llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3588                 else
3589                         llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3590                                      (unsigned long long)entry->lcme_extent.e_end);
3591                 separator = "\n";
3592         }
3593
3594         if (yaml) {
3595                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3596                 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3597         } else if (verbose & VERBOSE_DETAIL) {
3598                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3599                 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset:         %u\n",
3600                              " ", entry->lcme_offset);
3601                 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size:           %u\n",
3602                              " ", entry->lcme_size);
3603                 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3604         } else {
3605                 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3606         }
3607 }
3608
3609 /*
3610  * Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3611  * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3612  *
3613  * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3614  * The table below gives the answers for the specified parameters (value and
3615  * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3616  * --------------------------------------
3617  * 1 | file > limit; sign > 0 | -1 / -1 |
3618  * 2 | file = limit; sign > 0 | -1 / -1 |
3619  * 3 | file < limit; sign > 0 |  ? /  1 |
3620  * 4 | file > limit; sign = 0 | -1 / -1 |
3621  * 5 | file = limit; sign = 0 |  ? /  1 |  <- (see the Note below)
3622  * 6 | file < limit; sign = 0 |  ? / -1 |
3623  * 7 | file > limit; sign < 0 |  1 /  1 |
3624  * 8 | file = limit; sign < 0 |  ? / -1 |
3625  * 9 | file < limit; sign < 0 |  ? / -1 |
3626  * --------------------------------------
3627  * Note: 5th actually means that the value is within the interval
3628  * (limit - margin, limit].
3629  */
3630 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3631                           int sign, int negopt, unsigned long long margin,
3632                           bool mds)
3633 {
3634         int ret = -1;
3635
3636         if (sign > 0) {
3637                 /* Drop the fraction of margin (of days or size). */
3638                 if (file + margin <= limit)
3639                         ret = mds ? 0 : 1;
3640         } else if (sign == 0) {
3641                 if (file <= limit && file + margin > limit)
3642                         ret = mds ? 0 : 1;
3643                 else if (file + margin <= limit)
3644                         ret = mds ? 0 : -1;
3645         } else if (sign < 0) {
3646                 if (file > limit)
3647                         ret = 1;
3648                 else if (mds)
3649                         ret = 0;
3650         }
3651
3652         return negopt ? ~ret + 1 : ret;
3653 }
3654
3655 static inline struct lov_user_md *
3656 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3657 {
3658         return (struct lov_user_md *)((char *)comp_v1 +
3659                         comp_v1->lcm_entries[ent_idx].lcme_offset);
3660 }
3661
3662 static inline struct lov_user_ost_data_v1 *
3663 lov_v1v3_objects(struct lov_user_md *v1)
3664 {
3665         if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3666                 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3667         else
3668                 return v1->lmm_objects;
3669 }
3670
3671 static inline void
3672 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3673 {
3674         if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3675                 snprintf(pool_name, LOV_MAXPOOLNAME + 1, "%s",
3676                          ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3677         else
3678                 pool_name[0] = '\0';
3679 }
3680
3681 static inline bool
3682 print_last_init_comp(struct find_param *param)
3683 {
3684         /* print all component info */
3685         if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3686                 return false;
3687
3688         /* print specific component info */
3689         if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3690             param->fp_check_comp_start || param->fp_check_comp_end ||
3691             param->fp_check_mirror_id || param->fp_check_mirror_index)
3692                 return false;
3693
3694         return true;
3695 }
3696
3697 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3698 {
3699         int match;
3700
3701         if (param->fp_comp_end == LUSTRE_EOF) {
3702                 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3703                         match = end == LUSTRE_EOF ? 1 : -1;
3704                 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3705                         match = end == LUSTRE_EOF ? -1 : 1;
3706                 else /* at least EOF */
3707                         match = -1;
3708                 if (param->fp_exclude_comp_end)
3709                         match = ~match + 1;
3710         } else {
3711                 unsigned long long margin;
3712
3713                 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3714                 match = find_value_cmp(end, param->fp_comp_end,
3715                                        param->fp_comp_end_sign,
3716                                        param->fp_exclude_comp_end, margin, 0);
3717         }
3718
3719         return match;
3720 }
3721
3722 /**
3723  * An example of "getstripe -v" for a two components PFL file:
3724  *
3725  * composite_header:
3726  * lcm_magic:       0x0BD60BD0
3727  * lcm_size:        264
3728  * lcm_flags:       0
3729  * lcm_layout_gen:  2
3730  * lcm_entry_count: 2
3731  * components:
3732  * - lcme_id:             1
3733  *   lcme_flags:          0x10
3734  *   lcme_extent.e_start: 0
3735  *   lcme_extent.e_end:   1048576
3736  *   lcme_offset:         128
3737  *   lcme_size:           56
3738  *   sub_layout:
3739  *     lmm_magic:         0x0BD10BD0
3740  *     lmm_seq:           0x200000401
3741  *     lmm_object_id:     0x1
3742  *     lmm_fid:           [0x200000401:0x1:0x0]
3743  *     lmm_stripe_count:  1
3744  *     lmm_stripe_size:   1048576
3745  *     lmm_pattern:       raid0
3746  *     lmm_layout_gen:    0
3747  *     lmm_stripe_offset: 0
3748  *     lmm_objects:
3749  *     - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3750  *
3751  * - lcme_id:             2
3752  *   lcme_flags:          0x10
3753  *   lcme_extent.e_start: 1048576
3754  *   lcme_extent.e_end:   EOF
3755  *   lcme_offset:         184
3756  *   lcme_size:           80
3757  *     sub_layout:
3758  *     lmm_magic:         0x0BD10BD0
3759  *     lmm_seq:           0x200000401
3760  *     lmm_object_id:     0x1
3761  *     lmm_fid:           [0x200000401:0x1:0x0]
3762  *     lmm_stripe_count:  2
3763  *     lmm_stripe_size:   1048576
3764  *     lmm_pattern:       raid0
3765  *     lmm_layout_gen:    0
3766  *     lmm_stripe_offset: 1
3767  *     lmm_objects:
3768  *     - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3769  *     - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3770  */
3771 static void lov_dump_comp_v1(struct find_param *param, char *path,
3772                              enum lov_dump_flags flags)
3773 {
3774         struct lov_comp_md_entry_v1 *entry;
3775         struct lov_user_ost_data_v1 *objects;
3776         struct lov_comp_md_v1 *comp_v1 = (void *)&param->fp_lmd->lmd_lmm;
3777         struct lov_user_md_v1 *v1;
3778         char pool_name[LOV_MAXPOOLNAME + 1];
3779         int obdindex = param->fp_obd_index;
3780         int i, j, match, ext;
3781         bool obdstripe = false;
3782         __u16 mirror_index = 0;
3783         __u16 mirror_id = 0;
3784
3785         if (obdindex != OBD_NOT_FOUND) {
3786                 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3787                             i < comp_v1->lcm_entry_count; i++) {
3788                         if (!(comp_v1->lcm_entries[i].lcme_flags &
3789                               LCME_FL_INIT))
3790                                 continue;
3791
3792                         v1 = lov_comp_entry(comp_v1, i);
3793                         objects = lov_v1v3_objects(v1);
3794
3795                         for (j = 0; j < v1->lmm_stripe_count; j++) {
3796                                 if (obdindex == objects[j].l_ost_idx) {
3797                                         obdstripe = true;
3798                                         break;
3799                                 }
3800                         }
3801                 }
3802         } else {
3803                 obdstripe = true;
3804         }
3805
3806         if (!obdstripe)
3807                 return;
3808
3809         lov_dump_comp_v1_header(param, path, flags);
3810
3811         flags |= LDF_INDENT;
3812
3813         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3814                 entry = &comp_v1->lcm_entries[i];
3815
3816                 if (param->fp_check_comp_flags) {
3817                         if (((param->fp_comp_flags & entry->lcme_flags) !=
3818                              param->fp_comp_flags) ||
3819                             (param->fp_comp_neg_flags & entry->lcme_flags))
3820                                 continue;
3821                 }
3822
3823                 if (param->fp_check_comp_id &&
3824                     param->fp_comp_id != entry->lcme_id)
3825                         continue;
3826
3827                 if (param->fp_check_comp_start) {
3828                         match = find_value_cmp(entry->lcme_extent.e_start,
3829                                                param->fp_comp_start,
3830                                                param->fp_comp_start_sign,
3831                                                0,
3832                                                param->fp_comp_start_units, 0);
3833                         if (match == -1)
3834                                 continue;
3835                 }
3836
3837                 if (param->fp_check_comp_end) {
3838                         match = find_comp_end_cmp(entry->lcme_extent.e_end,
3839                                                   param);
3840                         if (match == -1)
3841                                 continue;
3842                 }
3843
3844                 if (param->fp_check_mirror_index) {
3845                         if (mirror_id != mirror_id_of(entry->lcme_id)) {
3846                                 mirror_index++;
3847                                 mirror_id = mirror_id_of(entry->lcme_id);
3848                         }
3849
3850                         match = find_value_cmp(mirror_index,
3851                                                param->fp_mirror_index,
3852                                                param->fp_mirror_index_sign,
3853                                                param->fp_exclude_mirror_index,
3854                                                1, 0);
3855                         if (match == -1)
3856                                 continue;
3857                 } else if (param->fp_check_mirror_id) {
3858                         if (mirror_id != mirror_id_of(entry->lcme_id))
3859                                 mirror_id = mirror_id_of(entry->lcme_id);
3860
3861                         match = find_value_cmp(mirror_id,
3862                                                param->fp_mirror_id,
3863                                                param->fp_mirror_id_sign,
3864                                                param->fp_exclude_mirror_id,
3865                                                1, 0);
3866                         if (match == -1)
3867                                 continue;
3868                 }
3869
3870                 if (print_last_init_comp(param)) {
3871                         /**
3872                          * if part of stripe info is needed, we'd print only
3873                          * the last instantiated component info.
3874                          */
3875                         if (entry->lcme_flags & LCME_FL_INIT)
3876                                 continue;
3877
3878                         if (param->fp_verbose & VERBOSE_EXT_SIZE) {
3879                                 if (entry->lcme_flags & LCME_FL_EXTENSION)
3880                                         /* moved back below */
3881                                         i++;
3882                                 else
3883                                         continue;
3884                         }
3885                         break;
3886                 }
3887
3888                 if (entry->lcme_flags & LCME_FL_INIT) {
3889                         if (obdindex != OBD_NOT_FOUND) {
3890                                 flags |= LDF_SKIP_OBJS;
3891                                 v1 = lov_comp_entry(comp_v1, i);
3892                                 objects = lov_v1v3_objects(v1);
3893
3894                                 for (j = 0; j < v1->lmm_stripe_count; j++) {
3895                                         if (obdindex == objects[j].l_ost_idx) {
3896                                                 flags &= ~LDF_SKIP_OBJS;
3897                                                 break;
3898                                         }
3899                                 }
3900                         } else {
3901                                 flags &= ~LDF_SKIP_OBJS;
3902                         }
3903                 } else {
3904                         flags |= LDF_SKIP_OBJS;
3905                 }
3906
3907                 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3908                         continue;
3909                 lov_dump_comp_v1_entry(param, flags, i);
3910
3911                 v1 = lov_comp_entry(comp_v1, i);
3912                 objects = lov_v1v3_objects(v1);
3913                 lov_v1v3_pool_name(v1, pool_name);
3914
3915                 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3916                 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3917                                        param->fp_max_depth, param->fp_verbose,
3918                                        flags | ext);
3919         }
3920         if (print_last_init_comp(param)) {
3921                 /**
3922                  * directory layout contains only layout template, print the
3923                  * last component.
3924                  */
3925                 if (i == 0)
3926                         i = comp_v1->lcm_entry_count - 1;
3927                 else
3928                         i--;
3929                 flags &= ~LDF_SKIP_OBJS;
3930
3931                 lov_dump_comp_v1_entry(param, flags, i);
3932
3933                 v1 = lov_comp_entry(comp_v1, i);
3934                 objects = lov_v1v3_objects(v1);
3935                 lov_v1v3_pool_name(v1, pool_name);
3936
3937                 entry = &comp_v1->lcm_entries[i];
3938                 ext = entry->lcme_flags & LCME_FL_EXTENSION ? LDF_EXTENSION : 0;
3939                 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3940                                        param->fp_max_depth, param->fp_verbose,
3941                                        flags | ext);
3942         }
3943 }
3944
3945 #define VERBOSE_COMP_OPTS       (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3946                                  VERBOSE_COMP_START | VERBOSE_COMP_END | \
3947                                  VERBOSE_COMP_FLAGS)
3948
3949 static inline bool has_any_comp_options(struct find_param *param)
3950 {
3951         enum llapi_layout_verbose verbose = param->fp_verbose;
3952
3953         if (param->fp_check_comp_id || param->fp_check_comp_count ||
3954             param->fp_check_comp_start || param->fp_check_comp_end ||
3955             param->fp_check_comp_flags)
3956                 return true;
3957
3958         /* show full layout information, not component specific */
3959         if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3960                 return false;
3961
3962         return verbose & VERBOSE_COMP_OPTS;
3963 }
3964
3965 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3966                                             bool is_dir)
3967 {
3968         struct lov_user_md *lum = &orig->lmd_lmm;
3969         struct lov_user_mds_data *new;
3970         struct lov_comp_md_v1 *comp_v1;
3971         struct lov_comp_md_entry_v1 *ent;
3972         int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3973         int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3974                                         lum->lmm_magic);
3975
3976         new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3977         if (new == NULL) {
3978                 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3979                 return new;
3980         }
3981
3982         memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3983                + sizeof(new->lmd_lmmsize));
3984
3985         comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3986         comp_v1->lcm_magic = lum->lmm_magic;
3987         comp_v1->lcm_size = lum_off + lum_size;
3988         comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3989         comp_v1->lcm_flags = 0;
3990         comp_v1->lcm_entry_count = 1;
3991
3992         ent = &comp_v1->lcm_entries[0];
3993         ent->lcme_id = 0;
3994         ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3995         ent->lcme_extent.e_start = 0;
3996         ent->lcme_extent.e_end = LUSTRE_EOF;
3997         ent->lcme_offset = lum_off;
3998         ent->lcme_size = lum_size;
3999
4000         memcpy((char *)comp_v1 + lum_off, lum, lum_size);
4001
4002         return new;
4003 }
4004
4005 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
4006                                     enum lov_dump_flags flags)
4007 {
4008         __u32 magic = *(__u32 *)&param->fp_lmd->lmd_lmm;
4009
4010         if (has_any_comp_options(param)) {
4011                 struct lov_user_mds_data *new_lmd, *orig_lmd;
4012
4013                 orig_lmd = param->fp_lmd;
4014                 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
4015                 if (new_lmd != NULL) {
4016                         param->fp_lmd = new_lmd;
4017                         lov_dump_comp_v1(param, path, flags);
4018                         param->fp_lmd = orig_lmd;
4019                         free(new_lmd);
4020                 }
4021                 return;
4022         }
4023
4024         if (magic == LOV_USER_MAGIC_V1) {
4025                 lov_dump_user_lmm_v1v3(&param->fp_lmd->lmd_lmm, NULL,
4026                                        param->fp_lmd->lmd_lmm.lmm_objects,
4027                                        path, param->fp_obd_index,
4028                                        param->fp_max_depth, param->fp_verbose,
4029                                        flags);
4030         } else {
4031                 char pool_name[LOV_MAXPOOLNAME + 1];
4032                 struct lov_user_ost_data_v1 *objects;
4033                 struct lov_user_md_v3 *lmmv3 = (void *)&param->fp_lmd->lmd_lmm;
4034
4035                 snprintf(pool_name, sizeof(pool_name), "%s",
4036                          lmmv3->lmm_pool_name);
4037                 objects = lmmv3->lmm_objects;
4038                 lov_dump_user_lmm_v1v3(&param->fp_lmd->lmd_lmm, pool_name,
4039                                        objects, path, param->fp_obd_index,
4040                                        param->fp_max_depth, param->fp_verbose,
4041                                        flags);
4042         }
4043 }
4044
4045 static uint32_t check_foreign_type(uint32_t foreign_type)
4046 {
4047         uint32_t i;
4048
4049         for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
4050                 if (lu_foreign_types[i].lft_name == NULL)
4051                         break;
4052                 if (foreign_type == lu_foreign_types[i].lft_type)
4053                         return i;
4054         }
4055
4056         return LU_FOREIGN_TYPE_UNKNOWN;
4057 }
4058
4059 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
4060                                  enum lov_dump_flags flags)
4061 {
4062         struct lov_foreign_md *lfm = (void *)&param->fp_lmd->lmd_lmm;
4063         bool yaml = flags & LDF_YAML;
4064
4065         if (!yaml && param->fp_depth && path)
4066                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4067
4068         if (param->fp_verbose & VERBOSE_DETAIL) {
4069                 uint32_t type = check_foreign_type(lfm->lfm_type);
4070
4071                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic:         0x%08X\n",
4072                              lfm->lfm_magic);
4073                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length:          %u\n",
4074                              lfm->lfm_length);
4075                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type:          0x%08X",
4076                              lfm->lfm_type);
4077                 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4078                         llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4079                                      lu_foreign_types[type].lft_name);
4080                 else
4081                         llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4082
4083                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags:          0x%08X\n",
4084                              lfm->lfm_flags);
4085         }
4086         llapi_printf(LLAPI_MSG_NORMAL, "lfm_value:     '%.*s'\n",
4087                      lfm->lfm_length, lfm->lfm_value);
4088         llapi_printf(LLAPI_MSG_NORMAL, "\n");
4089 }
4090
4091 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
4092                                     enum lov_dump_flags flags)
4093 {
4094         struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
4095         bool yaml = flags & LDF_YAML;
4096
4097         if (!yaml && param->fp_depth && path)
4098                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
4099
4100         if (param->fp_verbose & VERBOSE_DETAIL) {
4101                 uint32_t type = check_foreign_type(lfm->lfm_type);
4102
4103                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic:         0x%08X\n",
4104                              lfm->lfm_magic);
4105                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length:          %u\n",
4106                              lfm->lfm_length);
4107                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type:          0x%08X",
4108                              lfm->lfm_type);
4109                 if (type < LU_FOREIGN_TYPE_UNKNOWN)
4110                         llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
4111                                      lu_foreign_types[type].lft_name);
4112                 else
4113                         llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
4114
4115                 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags:          0x%08X\n",
4116                              lfm->lfm_flags);
4117         }
4118         llapi_printf(LLAPI_MSG_NORMAL, "lfm_value:     '%.*s'\n",
4119                      lfm->lfm_length, lfm->lfm_value);
4120         llapi_printf(LLAPI_MSG_NORMAL, "\n");
4121 }
4122
4123 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
4124                                     enum lov_dump_flags flags)
4125 {
4126         __u32 magic;
4127
4128         if (param->fp_get_lmv || param->fp_get_default_lmv)
4129                 magic = (__u32)param->fp_lmv_md->lum_magic;
4130         else
4131                 magic = *(__u32 *)&param->fp_lmd->lmd_lmm; /* lum->lmm_magic */
4132
4133         if (param->fp_raw)
4134                 flags |= LDF_IS_RAW;
4135         if (param->fp_yaml)
4136                 flags |= LDF_YAML;
4137
4138         switch (magic) {
4139         case LOV_USER_MAGIC_V1:
4140         case LOV_USER_MAGIC_V3:
4141         case LOV_USER_MAGIC_SPECIFIC:
4142                 lov_dump_plain_user_lmm(param, path, flags);
4143                 break;
4144         case LOV_USER_MAGIC_FOREIGN:
4145                 lov_dump_foreign_lmm(param, path, flags);
4146                 break;
4147         case LMV_MAGIC_V1:
4148         case LMV_USER_MAGIC: {
4149                 char pool_name[LOV_MAXPOOLNAME + 1];
4150                 struct lmv_user_md *lum;
4151
4152                 lum = (struct lmv_user_md *)param->fp_lmv_md;
4153                 snprintf(pool_name, sizeof(pool_name), "%s",
4154                          lum->lum_pool_name);
4155                 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
4156                                   param->fp_max_depth, param->fp_verbose,
4157                                   flags);
4158                 break;
4159         }
4160         case LOV_USER_MAGIC_COMP_V1:
4161                 lov_dump_comp_v1(param, path, flags);
4162                 break;
4163         case LMV_MAGIC_FOREIGN:
4164                 lmv_dump_foreign_lmm(param, path, flags);
4165                 break;
4166         default:
4167                 llapi_printf(LLAPI_MSG_NORMAL,
4168                              "unknown lmm_magic:  %#x (expecting one of %#x %#x %#x %#x)\n",
4169                              *(__u32 *)&param->fp_lmd->lmd_lmm,
4170                              LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
4171                              LMV_USER_MAGIC, LMV_MAGIC_V1);
4172                 return;
4173         }
4174 }
4175
4176 static int llapi_file_get_stripe1(const char *path, struct lov_user_md *lum)
4177 {
4178         const char *fname;
4179         char *dname;
4180         int fd, rc = 0;
4181
4182         fname = strrchr(path, '/');
4183
4184         /* It should be a file (or other non-directory) */
4185         if (fname == NULL) {
4186                 dname = (char *)malloc(2);
4187                 if (dname == NULL)
4188                         return -ENOMEM;
4189                 strcpy(dname, ".");
4190                 fname = (char *)path;
4191         } else {
4192                 dname = (char *)malloc(fname - path + 1);
4193                 if (dname == NULL)
4194                         return -ENOMEM;
4195                 strncpy(dname, path, fname - path);
4196                 dname[fname - path] = '\0';
4197                 fname++;
4198         }
4199
4200         fd = open(dname, O_RDONLY | O_NONBLOCK);
4201         if (fd == -1) {
4202                 rc = -errno;
4203                 goto out_free;
4204         }
4205
4206         strcpy((char *)lum, fname);
4207         if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
4208                 rc = -errno;
4209
4210         if (close(fd) == -1 && rc == 0)
4211                 rc = -errno;
4212
4213 out_free:
4214         free(dname);
4215         return rc;
4216 }
4217
4218 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
4219 {
4220         char *canon_path = NULL;
4221         int rc, rc2;
4222
4223         rc = llapi_file_get_stripe1(path, lum);
4224         if (!(rc == -ENOTTY || rc == -ENODATA))
4225                 goto out;
4226
4227         /* Handle failure due to symlinks by dereferencing path manually. */
4228         canon_path = canonicalize_file_name(path);
4229         if (canon_path == NULL)
4230                 goto out; /* Keep original rc. */
4231
4232         rc2 = llapi_file_get_stripe1(canon_path, lum);
4233         if (rc2 < 0)
4234                 goto out; /* Keep original rc. */
4235
4236         rc = 0;
4237 out:
4238         free(canon_path);
4239
4240         return rc;
4241 }
4242
4243 int llapi_file_lookup(int dirfd, const char *name)
4244 {
4245         struct obd_ioctl_data data = { 0 };
4246         char rawbuf[8192];
4247         char *buf = rawbuf;
4248         int rc;
4249
4250         if (dirfd < 0 || name == NULL)
4251                 return -EINVAL;
4252
4253         data.ioc_version = OBD_IOCTL_VERSION;
4254         data.ioc_len = sizeof(data);
4255         data.ioc_inlbuf1 = (char *)name;
4256         data.ioc_inllen1 = strlen(name) + 1;
4257
4258         rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4259         if (rc) {
4260                 llapi_error(LLAPI_MSG_ERROR, rc,
4261                             "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
4262                             name, rc);
4263                 return rc;
4264         }
4265
4266         rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
4267         if (rc < 0)
4268                 rc = -errno;
4269         return rc;
4270 }
4271
4272 /*
4273  * Check if the file time matches all the given criteria (e.g. --atime +/-N).
4274  * Return -1 or 1 if file timestamp does not or does match the given criteria
4275  * correspondingly. Return 0 if the MDS time is being checked and there are
4276  * attributes on OSTs and it is not yet clear if the timespamp matches.
4277  *
4278  * If 0 is returned, we need to do another RPC to the OSTs to obtain the
4279  * updated timestamps.
4280  */
4281 static int find_time_check(struct find_param *param, int mds)
4282 {
4283         struct lov_user_mds_data *lmd = param->fp_lmd;
4284         int rc = 1;
4285         int rc2;
4286
4287         /* Check if file is accepted. */
4288         if (param->fp_atime) {
4289                 rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4290                                      param->fp_atime, param->fp_asign,
4291                                      param->fp_exclude_atime,
4292                                      param->fp_time_margin, mds);
4293                 if (rc2 < 0)
4294                         return rc2;
4295                 rc = rc2;
4296         }
4297
4298         if (param->fp_mtime) {
4299                 rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4300                                      param->fp_mtime, param->fp_msign,
4301                                      param->fp_exclude_mtime,
4302                                      param->fp_time_margin, mds);
4303                 if (rc2 < 0)
4304                         return rc2;
4305
4306                 /*
4307                  * If the previous check matches, but this one is not yet clear,
4308                  * we should return 0 to do an RPC on OSTs.
4309                  */
4310                 if (rc == 1)
4311                         rc = rc2;
4312         }
4313
4314         if (param->fp_ctime) {
4315                 rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4316                                      param->fp_ctime, param->fp_csign,
4317                                      param->fp_exclude_ctime,
4318                                      param->fp_time_margin, mds);
4319                 if (rc2 < 0)
4320                         return rc2;
4321
4322                 /*
4323                  * If the previous check matches, but this one is not yet clear,
4324                  * we should return 0 to do an RPC on OSTs.
4325                  */
4326                 if (rc == 1)
4327                         rc = rc2;
4328         }
4329
4330         return rc;
4331 }
4332
4333 static int find_newerxy_check(struct find_param *param, int mds, bool from_mdt)
4334 {
4335         struct lov_user_mds_data *lmd = param->fp_lmd;
4336         int i;
4337         int rc = 1;
4338         int rc2;
4339
4340         for (i = 0; i < 2; i++) {
4341                 /* Check if file is accepted. */
4342                 if (param->fp_newery[NEWERXY_ATIME][i]) {
4343                         rc2 = find_value_cmp(lmd->lmd_stx.stx_atime.tv_sec,
4344                                              param->fp_newery[NEWERXY_ATIME][i],
4345                                              -1, i, 0, mds);
4346                         if (rc2 < 0)
4347                                 return rc2;
4348                         rc = rc2;
4349                 }
4350
4351                 if (param->fp_newery[NEWERXY_MTIME][i]) {
4352                         rc2 = find_value_cmp(lmd->lmd_stx.stx_mtime.tv_sec,
4353                                              param->fp_newery[NEWERXY_MTIME][i],
4354                                              -1, i, 0, mds);
4355                         if (rc2 < 0)
4356                                 return rc2;
4357
4358                         /*
4359                          * If the previous check matches, but this one is not
4360                          * yet clear, we should return 0 to do an RPC on OSTs.
4361                          */
4362                         if (rc == 1)
4363                                 rc = rc2;
4364                 }
4365
4366                 if (param->fp_newery[NEWERXY_CTIME][i]) {
4367                         rc2 = find_value_cmp(lmd->lmd_stx.stx_ctime.tv_sec,
4368                                              param->fp_newery[NEWERXY_CTIME][i],
4369                                              -1, i, 0, mds);
4370                         if (rc2 < 0)
4371                                 return rc2;
4372
4373                         /*
4374                          * If the previous check matches, but this one is not
4375                          * yet clear, we should return 0 to do an RPC on OSTs.
4376                          */
4377                         if (rc == 1)
4378                                 rc = rc2;
4379                 }
4380
4381                 /*
4382                  * File birth time (btime) can get from MDT directly.
4383                  * if @from_mdt is true, it means the input file attributs are
4384                  * obtained directly from MDT.
4385                  * Thus, if @from_mdt is false, we should skip the following
4386                  * btime check.
4387                  */
4388                 if (!from_mdt)
4389                         continue;
4390
4391                 if (param->fp_newery[NEWERXY_BTIME][i]) {
4392                         if (!(lmd->lmd_stx.stx_mask & STATX_BTIME))
4393                                 return -EOPNOTSUPP;
4394
4395                         rc2 = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
4396                                              param->fp_newery[NEWERXY_BTIME][i],
4397                                              -1, i, 0, 0);
4398                         if (rc2 < 0)
4399                                 return rc2;
4400                 }
4401         }
4402
4403         return rc;
4404 }
4405
4406 /**
4407  * Check whether the stripes matches the indexes user provided
4408  *       1   : matched
4409  *       0   : Unmatched
4410  */
4411 static int check_obd_match(struct find_param *param)
4412 {
4413         struct lov_user_ost_data_v1 *objects;
4414         struct lov_comp_md_v1 *comp_v1 = NULL;
4415         struct lov_user_mds_data *lmd = param->fp_lmd;
4416         struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4417         int i, j, k, count = 1;
4418
4419         if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
4420                 return 0;
4421
4422         if (!S_ISREG(lmd->lmd_stx.stx_mode))
4423                 return 0;
4424
4425         /* exclude foreign */
4426         if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4427                 return param->fp_exclude_obd;
4428
4429         /*
4430          * Only those files should be accepted, which have a
4431          * stripe on the specified OST.
4432          */
4433         if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4434                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4435                 count = comp_v1->lcm_entry_count;
4436         }
4437
4438         for (i = 0; i < count; i++) {
4439                 if (comp_v1)
4440                         v1 = lov_comp_entry(comp_v1, i);
4441
4442                 objects = lov_v1v3_objects(v1);
4443
4444                 for (j = 0; j < v1->lmm_stripe_count; j++) {
4445                         if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4446                                          LCME_FL_INIT))
4447                                 continue;
4448                         for (k = 0; k < param->fp_num_obds; k++) {
4449                                 if (param->fp_obd_indexes[k] ==
4450                                     objects[j].l_ost_idx)
4451                                         return !param->fp_exclude_obd;
4452                         }
4453                 }
4454         }
4455
4456         return param->fp_exclude_obd;
4457 }
4458
4459 static int check_mdt_match(struct find_param *param)
4460 {
4461         int i;
4462
4463         if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4464                 return 0;
4465
4466         /* FIXME: For striped dir, we should get stripe information and check */
4467         for (i = 0; i < param->fp_num_mdts; i++) {
4468                 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4469                         return !param->fp_exclude_mdt;
4470         }
4471
4472         if (param->fp_exclude_mdt)
4473                 return 1;
4474
4475         return 0;
4476 }
4477
4478 /**
4479  * Check whether the obd is active or not, if it is
4480  * not active, just print the object affected by this
4481  * failed target
4482  **/
4483 static void print_failed_tgt(struct find_param *param, char *path, int type)
4484 {
4485         struct obd_statfs stat_buf;
4486         struct obd_uuid uuid_buf;
4487         int tgt_nr, i, *indexes;
4488         int ret = 0;
4489
4490         if (type != LL_STATFS_LOV && type != LL_STATFS_LMV) {
4491                 llapi_error(LLAPI_MSG_NORMAL, ret, "%s: wrong statfs type(%d)",
4492                             __func__, type);
4493                 return;
4494         }
4495
4496         tgt_nr = (type == LL_STATFS_LOV) ? param->fp_obd_index :
4497                  param->fp_mdt_index;
4498         indexes = (type == LL_STATFS_LOV) ? param->fp_obd_indexes :
4499                   param->fp_mdt_indexes;
4500
4501         for (i = 0; i < tgt_nr; i++) {
4502                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4503                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4504
4505                 ret = llapi_obd_statfs(path, type, indexes[i], &stat_buf,
4506                                        &uuid_buf);
4507                 if (ret)
4508                         llapi_error(LLAPI_MSG_NORMAL, ret,
4509                                     "%s: obd_uuid: %s failed",
4510                                     __func__, param->fp_obd_uuid->uuid);
4511         }
4512 }
4513
4514 static int find_check_stripe_size(struct find_param *param)
4515 {
4516         struct lov_comp_md_v1 *comp_v1 = NULL;
4517         struct lov_user_md_v1 *v1 = &param->fp_lmd->lmd_lmm;
4518         __u32 stripe_size = 0;
4519         int ret, i, count = 1;
4520
4521         if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4522                 return param->fp_exclude_stripe_size ? 1 : -1;
4523
4524         ret = param->fp_exclude_stripe_size ? 1 : -1;
4525         if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4526                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4527                 count = comp_v1->lcm_entry_count;
4528         }
4529
4530         for (i = 0; i < count; i++) {
4531                 struct lov_comp_md_entry_v1 *ent;
4532
4533                 if (comp_v1) {
4534                         v1 = lov_comp_entry(comp_v1, i);
4535
4536                         ent = &comp_v1->lcm_entries[i];
4537                         if (ent->lcme_flags & LCME_FL_EXTENSION)
4538                                 continue;
4539                         if (!(ent->lcme_flags & LCME_FL_INIT))
4540                                 continue;
4541                 }
4542                 stripe_size = v1->lmm_stripe_size;
4543         }
4544
4545         ret = find_value_cmp(stripe_size, param->fp_stripe_size,
4546                              param->fp_stripe_size_sign,
4547                              param->fp_exclude_stripe_size,
4548                              param->fp_stripe_size_units, 0);
4549
4550         return ret;
4551 }
4552
4553 static int find_check_ext_size(struct find_param *param)
4554 {
4555         struct lov_comp_md_v1 *comp_v1;
4556         struct lov_user_md_v1 *v1;
4557         int ret, i;
4558
4559         ret = param->fp_exclude_ext_size ? 1 : -1;
4560         comp_v1 = (struct lov_comp_md_v1 *)&param->fp_lmd->lmd_lmm;
4561         if (comp_v1->lcm_magic != LOV_USER_MAGIC_COMP_V1)
4562                 return ret;
4563
4564         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4565                 struct lov_comp_md_entry_v1 *ent;
4566
4567                 v1 = lov_comp_entry(comp_v1, i);
4568
4569                 ent = &comp_v1->lcm_entries[i];
4570                 if (!(ent->lcme_flags & LCME_FL_EXTENSION))
4571                         continue;
4572
4573                 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_ext_size,
4574                                      param->fp_ext_size_sign,
4575                                      param->fp_exclude_ext_size,
4576                                      param->fp_ext_size_units, 0);
4577                 /* If any ext_size matches */
4578                 if (ret != -1)
4579                         break;
4580         }
4581
4582         return ret;
4583 }
4584
4585 static __u32 find_get_stripe_count(struct find_param *param)
4586 {
4587         struct lov_comp_md_v1 *comp_v1 = NULL;
4588         struct lov_user_md_v1 *v1 = &param->fp_lmd->lmd_lmm;
4589         int i, count = 1;
4590         __u32 stripe_count = 0;
4591
4592         if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4593                 return 0;
4594
4595         if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4596                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4597                 count = comp_v1->lcm_entry_count;
4598         }
4599
4600         for (i = 0; i < count; i++) {
4601                 if (comp_v1) {
4602                         struct lov_comp_md_entry_v1 *ent;
4603
4604                         v1 = lov_comp_entry(comp_v1, i);
4605
4606                         ent = &comp_v1->lcm_entries[i];
4607                         if (!(ent->lcme_flags & LCME_FL_INIT))
4608                                 continue;
4609
4610                         if (ent->lcme_flags & LCME_FL_EXTENSION)
4611                                 continue;
4612                 }
4613                 stripe_count = v1->lmm_stripe_count;
4614         }
4615
4616         return stripe_count;
4617 }
4618
4619 #define LOV_PATTERN_INVALID     0xFFFFFFFF
4620
4621 static int find_check_layout(struct find_param *param)
4622 {
4623         struct lov_comp_md_v1 *comp_v1 = NULL;
4624         struct lov_user_md_v1 *v1 = &param->fp_lmd->lmd_lmm;
4625         int i, count = 1;
4626         bool found = false, valid = false;
4627
4628         if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4629                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4630                 count = comp_v1->lcm_entry_count;
4631         }
4632
4633         for (i = 0; i < count; i++) {
4634                 if (comp_v1)
4635                         v1 = lov_comp_entry(comp_v1, i);
4636
4637                 /* foreign file have a special magic but no pattern field */
4638                 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4639                         continue;
4640
4641                 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4642                         continue;
4643
4644                 valid = true;
4645                 if (v1->lmm_pattern & param->fp_layout) {
4646                         found = true;
4647                         break;
4648                 }
4649         }
4650
4651         if (!valid)
4652                 return -1;
4653
4654         if ((found && !param->fp_exclude_layout) ||
4655             (!found && param->fp_exclude_layout))
4656                 return 1;
4657
4658         return -1;
4659 }
4660
4661 /*
4662  * if no type specified, check/exclude all foreign
4663  * if type specified, check all foreign&type and exclude !foreign + foreign&type
4664  */
4665 static int find_check_foreign(struct find_param *param)
4666 {
4667         if (S_ISREG(param->fp_lmd->lmd_stx.stx_mode)) {
4668                 struct lov_foreign_md *lfm;
4669
4670                 lfm = (void *)&param->fp_lmd->lmd_lmm;
4671                 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4672                         if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4673                                 return param->fp_exclude_foreign ? 1 : -1;
4674                         return -1;
4675                 }
4676
4677                 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4678                     lfm->lfm_type == param->fp_foreign_type)
4679                         return param->fp_exclude_foreign ? -1 : 1;
4680                 return param->fp_exclude_foreign ? 1 : -1;
4681         }
4682
4683         if (S_ISDIR(param->fp_lmd->lmd_stx.stx_mode)) {
4684                 struct lmv_foreign_md *lfm;
4685
4686                 lfm = (void *)param->fp_lmv_md;
4687                 if (lmv_is_foreign(lfm->lfm_magic)) {
4688                         if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4689                                 return param->fp_exclude_foreign ? 1 : -1;
4690                         return -1;
4691                 }
4692
4693                 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4694                     lfm->lfm_type == param->fp_foreign_type)
4695                         return param->fp_exclude_foreign ? -1 : 1;
4696                 return param->fp_exclude_foreign ? 1 : -1;
4697         }
4698         return -1;
4699 }
4700
4701 static int find_check_pool(struct find_param *param)
4702 {
4703         struct lov_comp_md_v1 *comp_v1 = NULL;
4704         struct lov_user_md_v3 *v3 = (void *)&param->fp_lmd->lmd_lmm;
4705         int i, count = 1;
4706         bool found = false;
4707
4708         if (v3->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4709                 comp_v1 = (struct lov_comp_md_v1 *)v3;
4710                 count = comp_v1->lcm_entry_count;
4711                 /* empty requested pool is taken as no pool search */
4712                 if (count == 0 && param->fp_poolname[0] == '\0') {
4713                         found = true;
4714                         goto found;
4715                 }
4716         }
4717
4718         for (i = 0; i < count; i++) {
4719                 if (comp_v1 != NULL) {
4720                         if (!(comp_v1->lcm_entries[i].lcme_flags &
4721                               LCME_FL_INIT))
4722                                 continue;
4723
4724                         v3 = (void *)lov_comp_entry(comp_v1, i);
4725                 }
4726
4727                 if (v3->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4728                         continue;
4729
4730                 if (((v3->lmm_magic == LOV_USER_MAGIC_V1) &&
4731                      (param->fp_poolname[0] == '\0')) ||
4732                     ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4733                      (strncmp(v3->lmm_pool_name,
4734                               param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4735                     ((v3->lmm_magic == LOV_USER_MAGIC_V3) &&
4736                      (strcmp(param->fp_poolname, "*") == 0))) {
4737                         found = true;
4738                         break;
4739                 }
4740         }
4741
4742 found:
4743         if ((found && !param->fp_exclude_pool) ||
4744             (!found && param->fp_exclude_pool))
4745                 return 1;
4746
4747         return -1;
4748 }
4749
4750 static int find_check_comp_options(struct find_param *param)
4751 {
4752         struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4753         struct lov_user_mds_data *lmd = param->fp_lmd;
4754         struct lov_user_md_v1 *v1 = &lmd->lmd_lmm;
4755         struct lov_comp_md_entry_v1 *entry;
4756         int i, ret = 0;
4757
4758         if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4759                 return -1;
4760
4761         if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4762                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4763         } else {
4764                 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4765                 if (forged_v1 == NULL)
4766                         return -1;
4767                 comp_v1 = forged_v1;
4768                 comp_v1->lcm_entry_count = 1;
4769                 entry = &comp_v1->lcm_entries[0];
4770                 entry->lcme_flags = S_ISDIR(lmd->lmd_stx.stx_mode) ?
4771                                     0 : LCME_FL_INIT;
4772                 entry->lcme_extent.e_start = 0;
4773                 entry->lcme_extent.e_end = LUSTRE_EOF;
4774         }
4775
4776         /* invalid case, don't match for any kind of search. */
4777         if (comp_v1->lcm_entry_count == 0) {
4778                 ret = -1;
4779                 goto out;
4780         }
4781
4782         if (param->fp_check_comp_count) {
4783                 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4784                                      param->fp_comp_count,
4785                                      param->fp_comp_count_sign,
4786                                      param->fp_exclude_comp_count, 1, 0);
4787                 if (ret == -1)
4788                         goto out;
4789         }
4790
4791         ret = 1;
4792         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4793                 entry = &comp_v1->lcm_entries[i];
4794
4795                 if (param->fp_check_comp_flags) {
4796                         ret = 1;
4797                         if (((param->fp_comp_flags & entry->lcme_flags) !=
4798                              param->fp_comp_flags) ||
4799                             (param->fp_comp_neg_flags & entry->lcme_flags)) {
4800                                 ret = -1;
4801                                 continue;
4802                         }
4803                 }
4804
4805                 if (param->fp_check_comp_start) {
4806                         ret = find_value_cmp(entry->lcme_extent.e_start,
4807                                              param->fp_comp_start,
4808                                              param->fp_comp_start_sign,
4809                                              param->fp_exclude_comp_start,
4810                                              param->fp_comp_start_units, 0);
4811                         if (ret == -1)
4812                                 continue;
4813                 }
4814
4815                 if (param->fp_check_comp_end) {
4816                         ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4817                                                 param);
4818                         if (ret == -1)
4819                                 continue;
4820                 }
4821
4822                 /* the component matches all criteria */
4823                 break;
4824         }
4825 out:
4826         if (forged_v1)
4827                 free(forged_v1);
4828         return ret;
4829 }
4830
4831 static int find_check_mirror_options(struct find_param *param)
4832 {
4833         struct lov_comp_md_v1 *comp_v1;
4834         struct lov_user_md_v1 *v1 = &param->fp_lmd->lmd_lmm;
4835         int ret = 0;
4836
4837         if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4838                 return -1;
4839
4840         comp_v1 = (struct lov_comp_md_v1 *)v1;
4841
4842         if (param->fp_check_mirror_count) {
4843                 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4844                                      param->fp_mirror_count,
4845                                      param->fp_mirror_count_sign,
4846                                      param->fp_exclude_mirror_count, 1, 0);
4847                 if (ret == -1)
4848                         return ret;
4849         }
4850
4851         if (param->fp_check_mirror_state) {
4852                 ret = 1;
4853                 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4854
4855                 if ((param->fp_mirror_state != 0 &&
4856                     file_state != param->fp_mirror_state) ||
4857                     file_state == param->fp_mirror_neg_state)
4858                         return -1;
4859         }
4860
4861         return ret;
4862 }
4863
4864 static bool find_check_lmm_info(struct find_param *param)
4865 {
4866         return param->fp_check_pool || param->fp_check_stripe_count ||
4867                param->fp_check_stripe_size || param->fp_check_layout ||
4868                param->fp_check_comp_count || param->fp_check_comp_end ||
4869                param->fp_check_comp_start || param->fp_check_comp_flags ||
4870                param->fp_check_mirror_count || param->fp_check_foreign ||
4871                param->fp_check_mirror_state || param->fp_check_ext_size ||
4872                param->fp_check_projid;
4873 }
4874
4875 /*
4876  * Interpret backslash escape sequences and write output into buffer.
4877  * Anything written to the buffer will be null terminated.
4878  *
4879  * @param[in]   seq     String being parsed for escape sequence. The leading
4880  *                      '\' character is not included in this string (only the
4881  *                      characters after it)
4882  * @param[out]  buffer  Location where interpreted escape sequence is written
4883  * @param[in]   size    Size of the available buffer. (Needs to be large enough
4884  *                      to handle escape sequence output plus null terminator.)
4885  * @param[out]  wrote   Number of bytes written to the buffer.
4886  * @return              Number of characters from input string processed
4887  *                      as part of the escape sequence (0 for an unrecognized
4888  *                      escape sequence)
4889  */
4890 int printf_format_escape(char *seq, char *buffer, size_t size, int *wrote)
4891 {
4892         *wrote = 0;
4893         /* For now, only handle single char escape sequences: \n, \t, \\ */
4894         if (size < 2)
4895                 return 0;
4896
4897         switch (*seq) {
4898         case 'n':
4899                 *buffer = '\n';
4900                 break;
4901         case 't':
4902                 *buffer = '\t';
4903                 break;
4904         case '\\':
4905                 *buffer = '\\';
4906                 break;
4907         default:
4908                 return 0;
4909         }
4910
4911         *wrote = 1;
4912         return 1;
4913 }
4914
4915 /*
4916  * Interpret formats for timestamps (%a, %A@, etc)
4917  *
4918  * @param[in]   seq     String being parsed for timestamp format.  The leading
4919  *                      '%' character is not included in this string
4920  * @param[out]  buffer  Location where timestamp info is written
4921  * @param[in]   size    Size of the available buffer.
4922  * @param[out]  wrote   Number of bytes written to the buffer.
4923  * @return              Number of characters from input string processed
4924  *                      as part of the format (0 for an unknown format)
4925  */
4926
4927 int printf_format_timestamp(char *seq, char *buffer, size_t size, int *wrote,
4928                             struct find_param *param)
4929 {
4930         struct statx_timestamp ts = { 0, 0 };
4931         struct tm *tm;
4932         time_t t;
4933         int rc = 0;
4934         char *fmt = "%c";  /* Print in ctime format by default */
4935         *wrote = 0;
4936
4937         switch (*seq) {
4938         case 'a':
4939                 ts = param->fp_lmd->lmd_stx.stx_atime;
4940                 rc = 1;
4941                 break;
4942         case 'A':
4943                 if (*(seq + 1) == '@') {
4944                         ts = param->fp_lmd->lmd_stx.stx_atime;
4945                         fmt = "%s";
4946                         rc = 2;
4947                 }
4948                 break;
4949         case 'c':
4950                 ts = param->fp_lmd->lmd_stx.stx_ctime;
4951                 rc = 1;
4952                 break;
4953         case 'C':
4954                 if (*(seq + 1) == '@') {
4955                         ts = param->fp_lmd->lmd_stx.stx_ctime;
4956                         fmt = "%s";
4957                         rc = 2;
4958                 }
4959                 break;
4960         case 't':
4961                 ts = param->fp_lmd->lmd_stx.stx_mtime;
4962                 rc = 1;
4963                 break;
4964         case 'T':
4965                 if (*(seq + 1) == '@') {
4966                         ts = param->fp_lmd->lmd_stx.stx_mtime;
4967                         fmt = "%s";
4968                         rc = 2;
4969                 }
4970                 break;
4971         case 'w':
4972                 ts = param->fp_lmd->lmd_stx.stx_btime;
4973                 rc = 1;
4974                 break;
4975         case 'W':
4976                 if (*(seq + 1) == '@') {
4977                         ts = param->fp_lmd->lmd_stx.stx_btime;
4978                         fmt = "%s";
4979                         rc = 2;
4980                 }
4981                 break;
4982         default:
4983                 rc = 0;
4984         }
4985
4986         if (rc) {
4987                 /* Found valid format, print to buffer */
4988                 t = ts.tv_sec;
4989                 tm = localtime(&t);
4990                 *wrote = strftime(buffer, size, fmt, tm);
4991         }
4992
4993         return rc;
4994 }
4995
4996 /*
4997  * Print all ost indices associated with a file layout using a commma separated
4998  * list.  For a file with mutliple components, the list of indices for each
4999  * component will be enclosed in brackets.
5000  *
5001  * @param[out]  buffer  Location where OST indices are written
5002  * @param[in]   size    Size of the available buffer.
5003  * @pararm[in]  layout  Pointer to layout structure for the file
5004  * @return              Number of bytes written to output buffer
5005  */
5006 static int printf_format_ost_indices(char *buffer, size_t size,
5007                                 struct llapi_layout *layout)
5008 {
5009         uint64_t count, idx, i;
5010         int err, bytes, wrote = 0;
5011
5012         /* Make sure to start at the first component */
5013         err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
5014         if (err) {
5015                 llapi_error(LLAPI_MSG_ERROR, err,
5016                             "error: layout component iteration failed\n");
5017                 goto format_done;
5018         }
5019         while (1) {
5020                 err = llapi_layout_stripe_count_get(layout, &count);
5021                 if (err) {
5022                         llapi_error(LLAPI_MSG_ERROR, err,
5023                                     "error: cannot get stripe_count\n");
5024                         goto format_done;
5025                 }
5026
5027                 bytes = snprintf(buffer, (size - wrote), "%s", "[");
5028                 wrote += bytes;
5029                 if (wrote >= size)
5030                         goto format_done;
5031                 buffer += bytes;
5032                 for (i = 0; i < count; i++) {
5033                         err = llapi_layout_ost_index_get(layout, i, &idx);
5034                         if (err) {
5035                                 llapi_error(LLAPI_MSG_ERROR, err,
5036                                             "error: cannot get OST index\n");
5037                                 bytes = snprintf(buffer, (size - wrote),
5038                                                  "%c,", '?');
5039                         } else {
5040                                 bytes = snprintf(buffer, (size - wrote),
5041                                                  "%"PRIu64",", idx);
5042                         }
5043                         wrote += bytes;
5044                         if (wrote >= size)
5045                                 goto format_done;
5046                         buffer += bytes;
5047                 }
5048                 /* Overwrite last comma with closing bracket */
5049                 *(buffer - 1) = ']';
5050
5051                 err = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
5052                 if (err == 0)           /* next component is found */
5053                         continue;
5054                 if (err < 0)
5055                         llapi_error(LLAPI_MSG_ERROR, err,
5056                                     "error: layout component iteration failed\n");
5057                 /* At this point, either got error or reached last component */
5058                 break;
5059         }
5060
5061 format_done:
5062         if (wrote >= size)
5063                 wrote = (size - 1);
5064         return wrote;
5065 }
5066
5067 /*
5068  * Parse Lustre-specific format sequences of the form %L{x}.
5069  *
5070  * @param[in]   seq     String being parsed for format sequence.  The leading
5071  *                      '%' character is not included in this string
5072  * @param[out]  buffer  Location where interpreted format info is written
5073  * @param[in]   size    Size of the available buffer.
5074  * @param[out]  wrote   Number of bytes written to the buffer.
5075  * @param[in]   param   The find_param structure associated with the file/dir
5076  * @param[in]   path    Pathname of the current file/dir being handled
5077  * @param[in]   projid  Project ID associated with the current file/dir
5078  * @param[in]   d       File descriptor for the directory (or -1 for a
5079  *                      non-directory file)
5080  * @return              Number of characters from input string processed
5081  *                      as part of the format (0 for an unknown format)
5082  */
5083 int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote,
5084                          struct find_param *param, char *path, int projid,
5085                          int d)
5086 {
5087         struct lmv_user_md *lum;
5088         struct lmv_user_mds_data *objects;
5089         struct llapi_layout *layout = NULL;
5090         struct lu_fid fid;
5091         unsigned int hash_type;
5092         uint64_t str_cnt, str_size, idx;
5093         char pool_name[LOV_MAXPOOLNAME + 1] = { '\0' };
5094         int err, bytes, i;
5095         int rc = 2;     /* all current valid sequences are 2 chars */
5096         *wrote = 0;
5097
5098         /* Sanity check.  Formats always look like %L{X} */
5099         if (*seq++ != 'L') {
5100                 rc = 0;
5101                 goto format_done;
5102         }
5103
5104         /*
5105          * Some formats like %LF or %LP are handled the same for both files
5106          * and dirs, so handle all of those here.
5107          */
5108         switch (*seq) {
5109         case 'F':
5110                 err = llapi_path2fid(path, &fid);
5111                 if (err) {
5112                         llapi_error(LLAPI_MSG_ERROR, err,
5113                                     "error: cannot get fid\n");
5114                         goto format_done;
5115                 }
5116                 *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid));
5117                 goto format_done;
5118         case 'P':
5119                 *wrote = snprintf(buffer, size, "%d", projid);
5120                 goto format_done;
5121         }
5122
5123         /* Other formats for files/dirs need to be handled differently */
5124         if (d == -1) {          /* file */
5125                 //layout = llapi_layout_get_by_xattr(&param->fp_lmd->lmd_lmm,
5126                 //                                 param->fp_lum_size, 0);
5127                 layout = llapi_layout_get_by_path(path, 0);
5128                 if (layout == NULL) {
5129                         llapi_error(LLAPI_MSG_ERROR, errno,
5130                                     "error: cannot get file layout\n");
5131                         goto format_done;
5132                 }
5133
5134                 /*
5135                  * Set the layout pointer to the last init component
5136                  * since that is the component used for most of these
5137                  * formats. (This also works for non-composite files)
5138                  */
5139                 err = llapi_layout_get_last_init_comp(layout);
5140                 if (err) {
5141                         llapi_error(LLAPI_MSG_ERROR, err,
5142                                     "error: cannot get last initialized compomnent\n");
5143                         goto format_done;
5144                 }
5145
5146                 switch (*seq) {
5147                 case 'c':       /* stripe count */
5148                         err = llapi_layout_stripe_count_get(layout, &str_cnt);
5149                         if (err) {
5150                                 llapi_error(LLAPI_MSG_ERROR, err,
5151                                             "error: cannot get stripe_count\n");
5152                                 goto format_done;
5153                         }
5154                         *wrote = snprintf(buffer, size, "%"PRIu64, str_cnt);
5155                         break;
5156                 case 'h':       /* hash info */
5157                         /* Not applicable to files.  Skip it. */
5158                         break;
5159                 case 'i':       /* starting index */
5160                         err = llapi_layout_ost_index_get(layout, 0, &idx);
5161                         if (err) {
5162                                 llapi_error(LLAPI_MSG_ERROR, err,
5163                                             "error: cannot get OST index of last initialized component\n");
5164                                 goto format_done;
5165                         }
5166                         *wrote = snprintf(buffer, size, "%"PRIu64, idx);
5167                         break;
5168                 case 'o':       /* list of object indices */
5169                         *wrote = printf_format_ost_indices(buffer, size, layout);
5170                         break;
5171                 case 'p':       /* pool name */
5172                         err = llapi_layout_pool_name_get(layout, pool_name,
5173                                                          sizeof(pool_name));
5174                         if (err) {
5175                                 llapi_error(LLAPI_MSG_ERROR, rc,
5176                                             "error: cannot get pool name\n");
5177                                 goto format_done;
5178                         }
5179                         *wrote = snprintf(buffer, size, "%s", pool_name);
5180                         break;
5181                 case 'S':       /* stripe size */
5182                         err = llapi_layout_stripe_size_get(layout, &str_size);
5183                         if (err) {
5184                                 llapi_error(LLAPI_MSG_ERROR, rc,
5185                                             "error: cannot get stripe_size\n");
5186                                 goto format_done;
5187                         }
5188                         *wrote = snprintf(buffer, size, "%"PRIu64, str_size);
5189                         break;
5190                 default:
5191                         rc = 0;
5192                         break;
5193                 }
5194         } else {                /* directory */
5195                 lum = (struct lmv_user_md *)param->fp_lmv_md;
5196                 objects = lum->lum_objects;
5197
5198                 switch (*seq) {
5199                 case 'c':       /* stripe count */
5200                         *wrote = snprintf(buffer, size, "%d",
5201                                           (int)lum->lum_stripe_count);
5202                         break;
5203                 case 'h':       /* hash info */
5204                         hash_type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
5205                         if (hash_type < LMV_HASH_TYPE_MAX)
5206                                 *wrote = snprintf(buffer, size, "%s",
5207                                                   mdt_hash_name[hash_type]);
5208                         else
5209                                 *wrote = snprintf(buffer, size, "%#x",
5210                                                   hash_type);
5211                         break;
5212                 case 'i':       /* starting index */
5213                         *wrote = snprintf(buffer, size, "%d",
5214                                           lum->lum_stripe_offset);
5215                         break;
5216                 case 'o':       /* list of object indices */
5217                         str_cnt = (int) lum->lum_stripe_count;
5218                         *wrote = snprintf(buffer, size, "%s", "[");
5219                         if (*wrote >= size)
5220                                 goto format_done;
5221                         buffer += *wrote;
5222                         for (i = 0; i < str_cnt; i++) {
5223                                 bytes = snprintf(buffer, (size - *wrote),
5224                                                  "%d,", objects[i].lum_mds);
5225                                 *wrote += bytes;
5226                                 if (*wrote >= size)
5227                                         goto format_done;
5228                                 buffer += bytes;
5229                         }
5230                         if (str_cnt == 0) {
5231                                 /* Use lum_offset as the only list entry */
5232                                 bytes = snprintf(buffer, (size - *wrote),
5233                                                 "%d]", lum->lum_stripe_offset);
5234                                 *wrote += bytes;
5235                         } else {
5236                                 /* Overwrite last comma with closing bracket */
5237                                 *(buffer - 1) = ']';
5238                         }
5239                         break;
5240                 case 'p':       /* pool name */
5241                         *wrote = snprintf(buffer, size, "%s",
5242                                           lum->lum_pool_name);
5243                         break;
5244                 case 'S':       /* stripe size */
5245                         /* This has no meaning for directories.  Skip it. */
5246                         break;
5247                 default:
5248                         rc = 0;
5249                         break;
5250                 }
5251         }
5252
5253 format_done:
5254         if (layout != NULL)
5255                 llapi_layout_free(layout);
5256
5257         if (*wrote >= size)
5258                 /* output of snprintf was truncated */
5259                 *wrote = size - 1;
5260
5261         return rc;
5262 }
5263
5264 /*
5265  * Interpret format specifiers beginning with '%'.
5266  *
5267  * @param[in]   seq     String being parsed for format specifier.  The leading
5268  *                      '%' character is not included in this string
5269  * @param[out]  buffer  Location where formatted info is written
5270  * @param[in]   size    Size of the available buffer.
5271  * @param[out]  wrote   Number of bytes written to the buffer.
5272  * @param[in]   param   The find_param structure associated with the file/dir
5273  * @param[in]   path    Pathname of the current file/dir being handled
5274  * @param[in]   projid  Project ID associated with the current file/dir
5275  * @param[in]   d       File descriptor for the directory (or -1 for a
5276  *                      non-directory file)
5277  * @return              Number of characters from input string processed
5278  *                      as part of the format (0 for an unknown format)
5279  */
5280 int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote,
5281                          struct find_param *param, char *path, int projid,
5282                          int d)
5283 {
5284         __u16 mode = param->fp_lmd->lmd_stx.stx_mode;
5285         uint64_t blocks = param->fp_lmd->lmd_stx.stx_blocks;
5286         int rc = 1;  /* most specifiers are single character */
5287
5288         *wrote = 0;
5289
5290         switch (*seq) {
5291         case 'a': case 'A':
5292         case 'c': case 'C':
5293         case 't': case 'T':
5294         case 'w': case 'W':     /* timestamps */
5295                 rc = printf_format_timestamp(seq, buffer, size, wrote, param);
5296                 break;
5297         case 'b':       /* file size (in 512B blocks) */
5298                 *wrote = snprintf(buffer, size, "%"PRIu64, blocks);
5299                 break;
5300         case 'G':       /* GID of owner */
5301                 *wrote = snprintf(buffer, size, "%u",
5302                                    param->fp_lmd->lmd_stx.stx_gid);
5303                 break;
5304         case 'k':       /* file size (in 1K blocks) */
5305                 *wrote = snprintf(buffer, size, "%"PRIu64, (blocks + 1)/2);
5306                 break;
5307         case 'L':       /* Lustre-specific formats */
5308                 rc = printf_format_lustre(seq, buffer, size, wrote, param,
5309                                           path, projid, d);
5310                 break;
5311         case 'm':       /* file mode in octal */
5312                 *wrote = snprintf(buffer, size, "%#o", (mode & (~S_IFMT)));
5313                 break;
5314         case 'p':       /* Path name of file */
5315                 *wrote = snprintf(buffer, size, "%s", path);
5316                 break;
5317         case 's':       /* file size (in bytes) */
5318                 *wrote = snprintf(buffer, size, "%"PRIu64,
5319                                    (uint64_t) param->fp_lmd->lmd_stx.stx_size);
5320                 break;
5321         case 'U':       /* UID of owner */
5322                 *wrote = snprintf(buffer, size, "%u",
5323                                    param->fp_lmd->lmd_stx.stx_uid);
5324                 break;
5325         case 'y':       /* file type */
5326                 if (S_ISREG(mode))
5327                         *buffer = 'f';
5328                 else if (S_ISDIR(mode))
5329                         *buffer = 'd';
5330                 else if (S_ISLNK(mode))
5331                         *buffer = 'l';
5332                 else if (S_ISBLK(mode))
5333                         *buffer = 'b';
5334                 else if (S_ISCHR(mode))
5335                         *buffer = 'c';
5336                 else if (S_ISFIFO(mode))
5337                         *buffer = 'p';
5338                 else if (S_ISSOCK(mode))
5339                         *buffer = 's';
5340                 else
5341                         *buffer = '?';
5342                 *wrote = 1;
5343                 break;
5344         case '%':
5345                 *buffer = '%';
5346                 *wrote = 1;
5347                 break;
5348         default:        /* invalid format specifier */
5349                 rc = 0;
5350                 break;
5351         }
5352
5353         if (*wrote >= size)
5354                 /* output of snprintf was truncated */
5355                 *wrote = size - 1;
5356
5357         return rc;
5358 }
5359
5360 /*
5361  * Parse user-supplied string for the -printf option and interpret any
5362  * '%' format specifiers or '\' escape sequences.
5363  *
5364  * @param[in]   param   The find_param struct containing the -printf string
5365  *                      as well as info about the current file/dir that mathced
5366  *                      the lfs find search criteria
5367  * @param[in]   path    Path name for current file/dir
5368  * @param[in]   projid  Project ID associated with current file/dir
5369  * @param[in]   d       File descriptor for current directory (or -1 for a
5370  *                      non-directory file)
5371  */
5372 void printf_format_string(struct find_param *param, char *path,
5373                            int projid, int d)
5374 {
5375         char output[FORMATTED_BUF_LEN];
5376         char *fmt_char = param->fp_format_printf_str;
5377         char *buff = output;
5378         size_t buff_size;
5379         int rc, written;
5380
5381         buff = output;
5382         *buff = '\0';
5383         buff_size = FORMATTED_BUF_LEN;
5384
5385         /* Always leave one free byte in buffer for trailing NUL */
5386         while (*fmt_char && (buff_size > 1)) {
5387                 rc = 0;
5388                 written = 0;
5389                 if (*fmt_char == '%') {
5390                         rc = printf_format_directive(fmt_char + 1, buff,
5391                                                   buff_size, &written, param,
5392                                                   path, projid, d);
5393                 } else if (*fmt_char == '\\')
5394                         rc = printf_format_escape(fmt_char + 1, buff,
5395                                                   buff_size, &written);
5396
5397                 if (rc > 0) {
5398                         /* Either a '\' escape or '%' format was processed.
5399                          * Increment pointers accordingly.
5400                          */
5401                         fmt_char += (rc + 1);
5402                         buff += written;
5403                         buff_size -= written;
5404                 } else {
5405                         /* Regular char or invalid escape/format.
5406                          * Either way, copy current character.
5407                          */
5408                         *buff++ = *fmt_char++;
5409                         buff_size--;
5410                 }
5411         }
5412
5413         /* Terminate output buffer and print */
5414         *buff = '\0';
5415         llapi_printf(LLAPI_MSG_NORMAL, "%s", output);
5416 }
5417
5418 /*
5419  * Get file/directory project id.
5420  * by the open fd resides on.
5421  * Return 0 and project id on success, or -ve errno.
5422  */
5423 static int fget_projid(int fd, int *projid)
5424 {
5425         struct fsxattr fsx;
5426         int rc;
5427
5428         rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
5429         if (rc)
5430                 return -errno;
5431
5432         *projid = fsx.fsx_projid;
5433         return 0;
5434 }
5435
5436 /*
5437  * Check that the file's permissions in *st matches the one in find_param
5438  */
5439 static int check_file_permissions(const struct find_param *param,
5440                         mode_t mode)
5441 {
5442         int decision = 0;
5443
5444         mode &= 07777;
5445
5446         switch (param->fp_perm_sign) {
5447         case LFS_FIND_PERM_EXACT:
5448                 decision = (mode == param->fp_perm);
5449                 break;
5450         case LFS_FIND_PERM_ALL:
5451                 decision = ((mode & param->fp_perm) == param->fp_perm);
5452                 break;
5453         case LFS_FIND_PERM_ANY:
5454                 decision = ((mode & param->fp_perm) != 0);
5455                 break;
5456         }
5457
5458         if ((param->fp_exclude_perm && decision)
5459                 || (!param->fp_exclude_perm && !decision))
5460                 return -1;
5461         else
5462                 return 1;
5463 }
5464
5465 static int cb_find_init(char *path, int p, int *dp,
5466                         void *data, struct dirent64 *de)
5467 {
5468         struct find_param *param = (struct find_param *)data;
5469         struct lov_user_mds_data *lmd = param->fp_lmd;
5470         int d = dp == NULL ? -1 : *dp;
5471         int decision = 1; /* 1 is accepted; -1 is rejected. */
5472         int lustre_fs = 1;
5473         int checked_type = 0;
5474         int ret = 0;
5475         __u32 stripe_count = 0;
5476         __u64 flags;
5477         int fd = -2;
5478         int projid = 0;
5479         bool gather_all = false;
5480
5481         if (p == -1 && d == -1)
5482                 return -EINVAL;
5483
5484         /* Reset this value between invocations */
5485         param->fp_get_lmv = 0;
5486
5487         /* Gather all file/dir info, not just what's needed for search params */
5488         if (param->fp_format_printf_str)
5489                 gather_all = true;
5490
5491         /* If a regular expression is presented, make the initial decision */
5492         if (param->fp_pattern != NULL) {
5493                 char *fname = strrchr(path, '/');
5494
5495                 fname = (fname == NULL ? path : fname + 1);
5496                 ret = fnmatch(param->fp_pattern, fname, 0);
5497                 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
5498                     (ret == 0 && param->fp_exclude_pattern))
5499                         goto decided;
5500         }
5501
5502         /* See if we can check the file type from the dirent. */
5503         if (de != NULL && de->d_type != DT_UNKNOWN) {
5504                 if (param->fp_type != 0) {
5505                         checked_type = 1;
5506
5507                         if (DTTOIF(de->d_type) == param->fp_type) {
5508                                 if (param->fp_exclude_type)
5509                                         goto decided;
5510                         } else {
5511                                 if (!param->fp_exclude_type)
5512                                         goto decided;
5513                         }
5514                 }
5515                 if ((param->fp_check_mdt_count || param->fp_hash_type ||
5516                      param->fp_check_hash_flag) && de->d_type != DT_DIR)
5517                         goto decided;
5518         }
5519
5520         ret = 0;
5521
5522         /*
5523          * Request MDS for the stat info if some of these parameters need
5524          * to be compared.
5525          */
5526         if (param->fp_obd_uuid || param->fp_mdt_uuid ||
5527             param->fp_check_uid || param->fp_check_gid ||
5528             param->fp_newerxy || param->fp_btime ||
5529             param->fp_atime || param->fp_mtime || param->fp_ctime ||
5530             param->fp_check_size || param->fp_check_blocks ||
5531             find_check_lmm_info(param) ||
5532             param->fp_check_mdt_count || param->fp_hash_type ||
5533             param->fp_check_hash_flag || param->fp_perm_sign ||
5534             gather_all)
5535                 decision = 0;
5536
5537         if (param->fp_type != 0 && checked_type == 0)
5538                 decision = 0;
5539
5540         if (decision == 0) {
5541                 if (d != -1 &&
5542                     (param->fp_check_mdt_count || param->fp_hash_type ||
5543                      param->fp_check_hash_flag || param->fp_check_foreign ||
5544                      gather_all)) {
5545                         param->fp_get_lmv = 1;
5546                         ret = cb_get_dirstripe(path, &d, param);
5547                         if (ret != 0) {
5548                                 if (errno == ENODATA) {
5549                                         /* Fill in struct for unstriped dir */
5550                                         ret = 0;
5551                                         param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
5552                                         /* Use 0 until we find actual offset */
5553                                         param->fp_lmv_md->lum_stripe_offset = 0;
5554                                         param->fp_lmv_md->lum_stripe_count = 0;
5555                                         param->fp_lmv_md->lum_hash_type = 0;
5556
5557                                         if (param->fp_check_foreign) {
5558                                                 if (param->fp_exclude_foreign)
5559                                                         goto print;
5560                                                 goto decided;
5561                                         }
5562                                 } else {
5563                                         return ret;
5564                                 }
5565                         }
5566
5567                         if (param->fp_check_mdt_count) {
5568                                 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5569                                         goto decided;
5570
5571                                 decision = find_value_cmp(param->fp_lmv_md->lum_stripe_count,
5572                                                           param->fp_mdt_count,
5573                                                           param->fp_mdt_count_sign,
5574                                                           param->fp_exclude_mdt_count, 1, 0);
5575                                 if (decision == -1)
5576                                         goto decided;
5577                         }
5578
5579                         if (param->fp_hash_type) {
5580                                 __u32 found;
5581                                 __u32 type = param->fp_lmv_md->lum_hash_type &
5582                                         LMV_HASH_TYPE_MASK;
5583
5584                                 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5585                                         goto decided;
5586
5587                                 found = (1 << type) & param->fp_hash_type;
5588                                 if ((found && param->fp_exclude_hash_type) ||
5589                                     (!found && !param->fp_exclude_hash_type))
5590                                         goto decided;
5591                         }
5592
5593                         if (param->fp_check_hash_flag) {
5594                                 __u32 flags = param->fp_lmv_md->lum_hash_type &
5595                                         ~LMV_HASH_TYPE_MASK;
5596
5597                                 if (lmv_is_foreign(param->fp_lmv_md->lum_magic))
5598                                         goto decided;
5599
5600                                 if (!(flags & param->fp_hash_inflags) ||
5601                                     (flags & param->fp_hash_exflags))
5602                                         goto decided;
5603                         }
5604                 }
5605
5606                 param->fp_lmd->lmd_lmm.lmm_magic = 0;
5607                 ret = get_lmd_info_fd(path, p, d, param->fp_lmd,
5608                                       param->fp_lum_size, GET_LMD_INFO);
5609                 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
5610                     find_check_lmm_info(param)) {
5611                         struct lov_user_md *lmm = &param->fp_lmd->lmd_lmm;
5612
5613                         /*
5614                          * We need to "fake" the "use the default" values
5615                          * since the lmm struct is zeroed out at this point.
5616                          */
5617                         lmm->lmm_magic = LOV_USER_MAGIC_V1;
5618                         lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
5619                         if (!param->fp_raw)
5620                                 ostid_set_seq(&lmm->lmm_oi,
5621                                               FID_SEQ_LOV_DEFAULT);
5622                         lmm->lmm_stripe_size = 0;
5623                         lmm->lmm_stripe_count = 0;
5624                         lmm->lmm_stripe_offset = -1;
5625                 }
5626                 if (ret == 0 && (param->fp_mdt_uuid != NULL || gather_all)) {
5627                         if (d != -1) {
5628                                 ret = llapi_file_fget_mdtidx(d,
5629                                                      &param->fp_file_mdt_index);
5630                                 /*
5631                                  *  Make sure lum_stripe_offset matches
5632                                  *  mdt_index even for unstriped directories.
5633                                  */
5634                                 if (ret == 0 && param->fp_get_lmv)
5635                                         param->fp_lmv_md->lum_stripe_offset =
5636                                                 param->fp_file_mdt_index;
5637                         } else if (S_ISREG(lmd->lmd_stx.stx_mode)) {
5638                                 /*
5639                                  * FIXME: we could get the MDT index from the
5640                                  * file's FID in lmd->lmd_lmm.lmm_oi without
5641                                  * opening the file, once we are sure that
5642                                  * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
5643                                  * That would still be an ioctl() to map the
5644                                  * FID to the MDT, but not an open RPC.
5645                                  */
5646                                 fd = open(path, O_RDONLY);
5647                                 if (fd > 0) {
5648                                         ret = llapi_file_fget_mdtidx(fd,
5649                                                      &param->fp_file_mdt_index);
5650                                 } else {
5651                                         ret = -errno;
5652                                 }
5653                         } else {
5654                                 /*
5655                                  * For a special file, we assume it resides on
5656                                  * the same MDT as the parent directory.
5657                                  */
5658                                 ret = llapi_file_fget_mdtidx(p,
5659                                                      &param->fp_file_mdt_index);
5660                         }
5661                 }
5662                 if (ret != 0) {
5663                         if (ret == -ENOTTY)
5664                                 lustre_fs = 0;
5665                         if (ret == -ENOENT)
5666                                 goto decided;
5667
5668                         goto out;
5669                 } else {
5670                         stripe_count = find_get_stripe_count(param);
5671                 }
5672         }
5673
5674         /* Check the file permissions from the stat info */
5675         if (param->fp_perm_sign) {
5676                 decision = check_file_permissions(param, lmd->lmd_stx.stx_mode);
5677                 if (decision == -1)
5678                         goto decided;
5679         }
5680
5681         if (param->fp_type && !checked_type) {
5682                 if ((param->fp_check_mdt_count || param->fp_check_hash_flag ||
5683                      param->fp_hash_type) && !S_ISDIR(lmd->lmd_stx.stx_mode))
5684                         goto decided;
5685
5686                 if ((lmd->lmd_stx.stx_mode & S_IFMT) == param->fp_type) {
5687                         if (param->fp_exclude_type)
5688                                 goto decided;
5689                 } else {
5690                         if (!param->fp_exclude_type)
5691                                 goto decided;
5692                 }
5693         }
5694
5695         /* Prepare odb. */
5696         if (param->fp_obd_uuid || param->fp_mdt_uuid) {
5697                 if (lustre_fs && param->fp_got_uuids &&
5698                     param->fp_dev != makedev(lmd->lmd_stx.stx_dev_major,
5699                                              lmd->lmd_stx.stx_dev_minor)) {
5700                         /* A lustre/lustre mount point is crossed. */
5701                         param->fp_got_uuids = 0;
5702                         param->fp_obds_printed = 0;
5703                         param->fp_mdt_index = OBD_NOT_FOUND;
5704                         param->fp_obd_index = OBD_NOT_FOUND;
5705                 }
5706
5707                 if (lustre_fs && !param->fp_got_uuids) {
5708                         ret = setup_target_indexes((d != -1) ? d : p, path,
5709                                                    param);
5710                         if (ret)
5711                                 goto out;
5712
5713                         param->fp_dev = makedev(lmd->lmd_stx.stx_dev_major,
5714                                                 lmd->lmd_stx.stx_dev_minor);
5715                 } else if (!lustre_fs && param->fp_got_uuids) {
5716                         /* A lustre/non-lustre mount point is crossed. */
5717                         param->fp_got_uuids = 0;
5718                         param->fp_mdt_index = OBD_NOT_FOUND;
5719                         param->fp_obd_index = OBD_NOT_FOUND;
5720                 }
5721         }
5722
5723         if (param->fp_check_foreign) {
5724                 decision = find_check_foreign(param);
5725                 if (decision == -1)
5726                         goto decided;
5727         }
5728
5729         if (param->fp_check_stripe_size) {
5730                 decision = find_check_stripe_size(param);
5731                 if (decision == -1)
5732                         goto decided;
5733         }
5734
5735         if (param->fp_check_ext_size) {
5736                 decision = find_check_ext_size(param);
5737                 if (decision == -1)
5738                         goto decided;
5739         }
5740
5741         if (param->fp_check_stripe_count) {
5742                 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
5743                                           param->fp_stripe_count_sign,
5744                                           param->fp_exclude_stripe_count, 1, 0);
5745                 if (decision == -1)
5746                         goto decided;
5747         }
5748
5749         if (param->fp_check_layout) {
5750                 decision = find_check_layout(param);
5751                 if (decision == -1)
5752                         goto decided;
5753         }
5754
5755         /* If an OBD UUID is specified but none matches, skip this file. */
5756         if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
5757             (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
5758                 goto decided;
5759
5760         /*
5761          * If an OST or MDT UUID is given, and some OST matches,
5762          * check it here.
5763          */
5764         if (param->fp_obd_index != OBD_NOT_FOUND ||
5765             param->fp_mdt_index != OBD_NOT_FOUND) {
5766                 if (param->fp_obd_uuid) {
5767                         if (check_obd_match(param)) {
5768                                 /*
5769                                  * If no mdtuuid is given, we are done.
5770                                  * Otherwise, fall through to the mdtuuid
5771                                  * check below.
5772                                  */
5773                                 if (!param->fp_mdt_uuid)
5774                                         goto obd_matches;
5775                         } else {
5776                                 goto decided;
5777                         }
5778                 }
5779
5780                 if (param->fp_mdt_uuid) {
5781                         if (check_mdt_match(param))
5782                                 goto obd_matches;
5783                         goto decided;
5784                 }
5785         }
5786
5787 obd_matches:
5788         if (param->fp_check_uid) {
5789                 if (lmd->lmd_stx.stx_uid == param->fp_uid) {
5790                         if (param->fp_exclude_uid)
5791                                 goto decided;
5792                 } else {
5793                         if (!param->fp_exclude_uid)
5794                                 goto decided;
5795                 }
5796         }
5797
5798         if (param->fp_check_gid) {
5799                 if (lmd->lmd_stx.stx_gid == param->fp_gid) {
5800                         if (param->fp_exclude_gid)
5801                                 goto decided;
5802                 } else {
5803                         if (!param->fp_exclude_gid)
5804                                 goto decided;
5805                 }
5806         }
5807
5808         if (param->fp_check_projid || gather_all) {
5809                 int projid = 0;
5810
5811                 if (fd == -2)
5812                         fd = open(path, O_RDONLY);
5813
5814                 if (fd > 0)
5815                         ret = fget_projid(fd, &projid);
5816                 else
5817                         ret = -errno;
5818                 if (ret)
5819                         goto out;
5820                 if (projid == param->fp_projid) {
5821                         if (param->fp_exclude_projid)
5822                                 goto decided;
5823                 } else {
5824                         if (!param->fp_exclude_projid)
5825                                 goto decided;
5826                 }
5827         }
5828
5829         if (param->fp_check_pool) {
5830                 decision = find_check_pool(param);
5831                 if (decision == -1)
5832                         goto decided;
5833         }
5834
5835         if (param->fp_check_comp_count || param->fp_check_comp_flags ||
5836             param->fp_check_comp_start || param->fp_check_comp_end) {
5837                 decision = find_check_comp_options(param);
5838                 if (decision == -1)
5839                         goto decided;
5840         }
5841
5842         if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
5843                 decision = find_check_mirror_options(param);
5844                 if (decision == -1)
5845                         goto decided;
5846         }
5847
5848         /* Check the time on mds. */
5849         decision = 1;
5850         if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
5851                 int for_mds;
5852
5853                 for_mds = lustre_fs ?
5854                           (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5855                 decision = find_time_check(param, for_mds);
5856                 if (decision == -1)
5857                         goto decided;
5858         }
5859
5860         if (param->fp_btime) {
5861                 if (!(lmd->lmd_stx.stx_mask & STATX_BTIME)) {
5862                         ret = -EOPNOTSUPP;
5863                         goto out;
5864                 }
5865
5866                 decision = find_value_cmp(lmd->lmd_stx.stx_btime.tv_sec,
5867                                           param->fp_btime, param->fp_bsign,
5868                                           param->fp_exclude_btime,
5869                                           param->fp_time_margin, 0);
5870                 if (decision == -1)
5871                         goto decided;
5872         }
5873
5874         if (param->fp_newerxy) {
5875                 int for_mds;
5876
5877                 for_mds = lustre_fs ?
5878                           (S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) : 0;
5879                 decision = find_newerxy_check(param, for_mds, true);
5880                 if (decision == -1)
5881                         goto decided;
5882                 if (decision < 0) {
5883                         ret = decision;
5884                         goto out;
5885                 }
5886         }
5887
5888         flags = param->fp_lmd->lmd_flags;
5889         if (param->fp_check_size &&
5890             ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5891               S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5892             !(flags & OBD_MD_FLSIZE ||
5893               (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
5894                 decision = 0;
5895
5896         if (param->fp_check_blocks &&
5897             ((S_ISREG(lmd->lmd_stx.stx_mode) && stripe_count) ||
5898               S_ISDIR(lmd->lmd_stx.stx_mode)) &&
5899             !(flags & OBD_MD_FLBLOCKS ||
5900               (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
5901                 decision = 0;
5902
5903         /*
5904          * If file still fits the request, ask ost for updated info.
5905          * The regular stat is almost of the same speed as some new
5906          * 'glimpse-size-ioctl'.
5907          */
5908         if (!decision || gather_all) {
5909                 lstat_t st;
5910
5911                 /*
5912                  * For regular files with the stripe the decision may have not
5913                  * been taken yet if *time or size is to be checked.
5914                  */
5915                 if (param->fp_obd_index != OBD_NOT_FOUND)
5916                         print_failed_tgt(param, path, LL_STATFS_LOV);
5917
5918                 if (param->fp_mdt_index != OBD_NOT_FOUND)
5919                         print_failed_tgt(param, path, LL_STATFS_LMV);
5920
5921                 if (d != -1)
5922                         ret = fstat_f(d, &st);
5923                 else if (de != NULL)
5924                         ret = fstatat_f(p, de->d_name, &st,
5925                                         AT_SYMLINK_NOFOLLOW);
5926                 else
5927                         ret = lstat_f(path, &st);
5928
5929                 if (ret) {
5930                         if (errno == ENOENT) {
5931                                 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
5932                                             "warning: %s: %s does not exist",
5933                                             __func__, path);
5934                                 goto decided;
5935                         } else {
5936                                 ret = -errno;
5937                                 llapi_error(LLAPI_MSG_ERROR, ret,
5938                                             "%s: stat on %s failed",
5939                                             __func__, path);
5940                                 goto out;
5941                         }
5942                 }
5943
5944                 convert_lmd_statx(param->fp_lmd, &st, true);
5945                 /* Check the time on osc. */
5946                 decision = find_time_check(param, 0);
5947                 if (decision == -1)
5948                         goto decided;
5949
5950                 if (param->fp_newerxy) {
5951                         decision = find_newerxy_check(param, 0, false);
5952                         if (decision == -1)
5953                                 goto decided;
5954                         if (decision < 0) {
5955                                 ret = decision;
5956                                 goto out;
5957                         }
5958                 }
5959         }
5960
5961         if (param->fp_check_size) {
5962                 decision = find_value_cmp(lmd->lmd_stx.stx_size,
5963                                           param->fp_size,
5964                                           param->fp_size_sign,
5965                                           param->fp_exclude_size,
5966                                           param->fp_size_units, 0);
5967                 if (decision == -1)
5968                         goto decided;
5969         }
5970
5971         if (param->fp_check_blocks) { /* convert st_blocks to bytes */
5972                 decision = find_value_cmp(lmd->lmd_stx.stx_blocks * 512,
5973                                           param->fp_blocks,
5974                                           param->fp_blocks_sign,
5975                                           param->fp_exclude_blocks,
5976                                           param->fp_blocks_units, 0);
5977                 if (decision == -1)
5978                         goto decided;
5979         }
5980
5981 print:
5982         if (param->fp_format_printf_str)
5983                 printf_format_string(param, path, projid, d);
5984         else
5985                 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", path,
5986                              param->fp_zero_end ? '\0' : '\n');
5987
5988 decided:
5989         ret = 0;
5990         /* Do not get down anymore? */
5991         if (param->fp_depth == param->fp_max_depth) {
5992                 ret = 1;
5993                 goto out;
5994         }
5995         param->fp_depth++;
5996 out:
5997         if (fd > 0)
5998                 close(fd);
5999         return ret;
6000 }
6001
6002 static int cb_migrate_mdt_init(char *path, int p, int *dp,
6003                                void *param_data, struct dirent64 *de)
6004 {
6005         struct find_param *param = (struct find_param *)param_data;
6006         struct lmv_user_md *lmu = param->fp_lmv_md;
6007         int tmp_p = p;
6008         char raw[MAX_IOC_BUFLEN] = {'\0'};
6009         char *rawbuf = raw;
6010         struct obd_ioctl_data data = { 0 };
6011         int ret;
6012         char *path_copy;
6013         char *filename;
6014         bool retry = false;
6015
6016         if (p == -1 && dp == NULL)
6017                 return -EINVAL;
6018
6019         if (!lmu)
6020                 return -EINVAL;
6021
6022         if (dp != NULL && *dp != -1)
6023                 close(*dp);
6024
6025         if (p == -1) {
6026                 tmp_p = open_parent(path);
6027                 if (tmp_p == -1) {
6028                         *dp = -1;
6029                         ret = -errno;
6030                         llapi_error(LLAPI_MSG_ERROR, ret,
6031                                     "can not open %s", path);
6032                         return ret;
6033                 }
6034         }
6035
6036         path_copy = strdup(path);
6037         filename = basename(path_copy);
6038
6039         data.ioc_inlbuf1 = (char *)filename;
6040         data.ioc_inllen1 = strlen(filename) + 1;
6041         data.ioc_inlbuf2 = (char *)lmu;
6042         data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
6043                                             lmu->lum_magic);
6044         /* reach bottom? */
6045         if (param->fp_depth == param->fp_max_depth)
6046                 data.ioc_type = MDS_MIGRATE_NSONLY;
6047         ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6048         if (ret != 0) {
6049                 llapi_error(LLAPI_MSG_ERROR, ret,
6050                             "%s: error packing ioctl data", __func__);
6051                 goto out;
6052         }
6053
6054 migrate:
6055         ret = ioctl(tmp_p, LL_IOC_MIGRATE, rawbuf);
6056         if (ret != 0) {
6057                 if (errno == EBUSY && !retry) {
6058                         /*
6059                          * because migrate may not be able to lock all involved
6060                          * objects in order, for some of them it try lock, while
6061                          * there may be conflicting COS locks and cause migrate
6062                          * fail with EBUSY, hope a sync() could cause
6063                          * transaction commit and release these COS locks.
6064                          */
6065                         sync();
6066                         retry = true;
6067                         goto migrate;
6068                 } else if (errno == EALREADY) {
6069                         if (param->fp_verbose & VERBOSE_DETAIL)
6070                                 llapi_printf(LLAPI_MSG_NORMAL,
6071                                              "%s migrated to MDT%d already\n",
6072                                              path, lmu->lum_stripe_offset);
6073                         ret = 0;
6074                 } else {
6075                         ret = -errno;
6076                         llapi_error(LLAPI_MSG_ERROR, ret, "%s migrate failed",
6077                                     path);
6078                         goto out;
6079                 }
6080         } else if (param->fp_verbose & VERBOSE_DETAIL) {
6081                 llapi_printf(LLAPI_MSG_NORMAL,
6082                              "migrate %s to MDT%d stripe count %d\n",
6083                              path, lmu->lum_stripe_offset,
6084                              lmu->lum_stripe_count);
6085         }
6086
6087 out:
6088         /* Do not get down anymore? */
6089         if (param->fp_depth == param->fp_max_depth)
6090                 ret = 1;
6091         param->fp_depth++;
6092
6093         if (dp != NULL) {
6094                 /*
6095                  * If the directory is being migration, we need
6096                  * close the directory after migration,
6097                  * so the old directory cache will be cleanup
6098                  * on the client side, and re-open to get the
6099                  * new directory handle
6100                  */
6101                 *dp = open(path, O_RDONLY|O_NDELAY|O_DIRECTORY);
6102                 if (*dp == -1) {
6103                         ret = -errno;
6104                         llapi_error(LLAPI_MSG_ERROR, ret,
6105                                     "%s: Failed to open '%s'", __func__, path);
6106                 }
6107         }
6108
6109         if (p == -1)
6110                 close(tmp_p);
6111
6112         free(path_copy);
6113
6114         return ret;
6115 }
6116
6117 /* dir migration finished, shrink its stripes */
6118 static int cb_migrate_mdt_fini(char *path, int p, int *dp, void *data,
6119                                struct dirent64 *de)
6120 {
6121         struct find_param *param = data;
6122         struct lmv_user_md *lmu = param->fp_lmv_md;
6123         int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
6124         int ret = 0;
6125
6126         if (de && de->d_type != DT_DIR)
6127                 goto out;
6128
6129         if (*dp != -1) {
6130                 /*
6131                  * close it before setxattr because the latter may destroy the
6132                  * original object, and cause close fail.
6133                  */
6134                 ret = close(*dp);
6135                 *dp = -1;
6136                 if (ret)
6137                         goto out;
6138         }
6139
6140         ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
6141         if (ret == -EALREADY)
6142                 ret = 0;
6143 out:
6144         cb_common_fini(path, p, dp, data, de);
6145         return ret;
6146 }
6147
6148 int llapi_migrate_mdt(char *path, struct find_param *param)
6149 {
6150         return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
6151                               param);
6152 }
6153
6154 int llapi_mv(char *path, struct find_param *param)
6155 {
6156 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
6157         static bool printed;
6158
6159         if (!printed) {
6160                 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
6161                           "%s() is deprecated, use llapi_migrate_mdt() instead",
6162                           __func__);
6163                 printed = true;
6164         }
6165 #endif
6166         return llapi_migrate_mdt(path, param);
6167 }
6168
6169 /*
6170  * Check string for escape sequences and print a message to stdout
6171  * if any invalid escapes are found.
6172  *
6173  * @param[in]   c       Pointer to character immediately following the
6174  *                      '\' character indicating the start of an escape
6175  *                      sequence.
6176  * @return              Number of characters examined in the escape sequence
6177  *                      (regardless of whether the sequence is valid or not).
6178  */
6179 int validate_printf_esc(char *c)
6180 {
6181         char *valid_esc = "nt\\";
6182
6183         if (*c == '\0') {
6184                  /* backslash at end of string */
6185                 llapi_err_noerrno(LLAPI_MSG_WARN,
6186                         "warning: '\\' at end of -printf format string\n");
6187                 return 0;
6188         }
6189
6190         if (!strchr(valid_esc, *c))
6191                 /* Invalid escape character */
6192                 llapi_err_noerrno(LLAPI_MSG_WARN,
6193                         "warning: unrecognized escape: '\\%c'\n", *c);
6194
6195         return 1;
6196 }
6197
6198 /*
6199  * Check string for format directives and print a message to stdout
6200  * if any invalid directives are found.
6201  *
6202  * @param[in]   c       Pointer to character immediately following the
6203  *                      '%' character indicating the start of a format
6204  *                      directive.
6205  * @return              Number of characters examined in the format directive
6206  *                      (regardless of whether the directive is valid or not).
6207  */
6208 int validate_printf_fmt(char *c)
6209 {
6210         char *valid_fmt_single = "abcGkmpstUwy%";
6211         char *valid_fmt_double = "ACTW";
6212         char *valid_fmt_lustre = "cFhioPpS";
6213         char curr = *c, next;
6214
6215         if (curr == '\0') {
6216                 llapi_err_noerrno(LLAPI_MSG_WARN,
6217                         "warning: '%%' at end of -printf format string\n");
6218                 return 0;
6219         }
6220
6221         next = *(c + 1);
6222         if ((next == '\0') || (next == '%') || (next == '\\'))
6223                 /* Treat as single char format directive */
6224                 goto check_single;
6225
6226         /* Check format directives with multiple characters */
6227         if (strchr(valid_fmt_double, curr)) {
6228                 /* For now, only valid formats are followed by '@' char */
6229                 if (next != '@')
6230                         llapi_err_noerrno(LLAPI_MSG_WARN,
6231                                 "warning: unrecognized format directive: '%%%c%c'\n",
6232                                 curr, next);
6233                 return 2;
6234         }
6235
6236         /* Lustre formats always start with 'L' */
6237         if (curr == 'L') {
6238                 if (!strchr(valid_fmt_lustre, next))
6239                         llapi_err_noerrno(LLAPI_MSG_WARN,
6240                                 "warning: unrecognized format directive: '%%%c%c'\n",
6241                                 curr, next);
6242                 return 2;
6243         }
6244
6245 check_single:
6246
6247         if (!strchr(valid_fmt_single, curr))
6248                 llapi_err_noerrno(LLAPI_MSG_WARN,
6249                         "warning: unrecognized format directive: '%%%c'\n", curr);
6250         return 1;
6251 }
6252
6253 /*
6254  * Validate the user-supplied string for the -printf option and report
6255  * any invalid backslash escape sequences or format directives.
6256  *
6257  * @param[in]   param   Structure containing info about invocation of lfs find
6258  * @return              None
6259  */
6260 void validate_printf_str(struct find_param *param)
6261 {
6262         char *c = param->fp_format_printf_str;
6263         int ret = 0;
6264
6265         while (*c) {
6266                 switch (*c) {
6267                 case '%':
6268                         ret = validate_printf_fmt(++c);
6269                         c += ret;
6270                         break;
6271                 case '\\':
6272                         ret = validate_printf_esc(++c);
6273                         c += ret;
6274                         break;
6275                 default:
6276                         c++;
6277                         break;
6278                 }
6279         }
6280 }
6281
6282 int llapi_find(char *path, struct find_param *param)
6283 {
6284         if (param->fp_format_printf_str)
6285                 validate_printf_str(param);
6286         return param_callback(path, cb_find_init, cb_common_fini, param);
6287 }
6288
6289 /*
6290  * Get MDT number that the file/directory inode referenced
6291  * by the open fd resides on.
6292  * Return 0 and mdtidx on success, or -ve errno.
6293  */
6294 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
6295 {
6296         if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
6297                 return -errno;
6298         return 0;
6299 }
6300
6301 static int cb_get_mdt_index(char *path, int p, int *dp, void *data,
6302                             struct dirent64 *de)
6303 {
6304         struct find_param *param = (struct find_param *)data;
6305         int d = dp == NULL ? -1 : *dp;
6306         int ret;
6307         int mdtidx;
6308
6309         if (p == -1 && d == -1)
6310                 return -EINVAL;
6311
6312         if (d != -1) {
6313                 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6314         } else /* if (p != -1) */ {
6315                 int fd;
6316
6317                 fd = open(path, O_RDONLY | O_NOCTTY);
6318                 if (fd > 0) {
6319                         ret = llapi_file_fget_mdtidx(fd, &mdtidx);
6320                         close(fd);
6321                 } else {
6322                         ret = -errno;
6323                 }
6324         }
6325
6326         if (ret != 0) {
6327                 if (ret == -ENODATA) {
6328                         if (!param->fp_obd_uuid)
6329                                 llapi_printf(LLAPI_MSG_NORMAL,
6330                                              "'%s' has no stripe info\n", path);
6331                         goto out;
6332                 } else if (ret == -ENOENT) {
6333                         llapi_error(LLAPI_MSG_WARN, ret,
6334                                     "warning: %s: '%s' does not exist",
6335                                     __func__, path);
6336                         goto out;
6337                 } else if (ret == -ENOTTY) {
6338                         llapi_error(LLAPI_MSG_ERROR, ret,
6339                                     "%s: '%s' not on a Lustre fs",
6340                                     __func__, path);
6341                 } else {
6342                         llapi_error(LLAPI_MSG_ERROR, ret,
6343                                     "error: %s: '%s' failed get_mdtidx",
6344                                     __func__, path);
6345                 }
6346                 return ret;
6347         }
6348
6349         if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
6350                 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
6351         else
6352                 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
6353                              path, mdtidx);
6354
6355 out:
6356         /* Do not go down anymore? */
6357         if (param->fp_depth == param->fp_max_depth)
6358                 return 1;
6359
6360         param->fp_depth++;
6361
6362         return 0;
6363 }
6364
6365 static int cb_getstripe(char *path, int p, int *dp, void *data,
6366                         struct dirent64 *de)
6367 {
6368         struct find_param *param = (struct find_param *)data;
6369         int d = dp == NULL ? -1 : *dp;
6370         int ret = 0;
6371
6372         if (p == -1 && d == -1)
6373                 return -EINVAL;
6374
6375         if (param->fp_obd_uuid) {
6376                 param->fp_quiet = 1;
6377                 ret = setup_obd_uuid(d != -1 ? d : p, path, param);
6378                 if (ret)
6379                         return ret;
6380         }
6381
6382         if (d != -1 && (param->fp_get_lmv || param->fp_get_default_lmv))
6383                 ret = cb_get_dirstripe(path, &d, param);
6384         else if (d != -1 ||
6385                  (p != -1 && !param->fp_get_lmv && !param->fp_get_default_lmv))
6386                 ret = get_lmd_info_fd(path, p, d, &param->fp_lmd->lmd_lmm,
6387                                       param->fp_lum_size, GET_LMD_STRIPE);
6388         else if (d == -1 && (param->fp_get_lmv || param->fp_get_default_lmv)) {
6389                 /* in case of a dangling or valid faked symlink dir, opendir()
6390                  * should have return either EINVAL or ENOENT, so let's try
6391                  * to get LMV just in case, and by opening it as a file but
6392                  * with O_NOFOLLOW ...
6393                  */
6394                 int fd = open(path, O_RDONLY | O_NOFOLLOW);
6395
6396                 if (fd == -1)
6397                         return 0;
6398                 ret = cb_get_dirstripe(path, &fd, param);
6399                 if (ret == 0)
6400                         llapi_lov_dump_user_lmm(param, path, LDF_IS_DIR);
6401                 close(fd);
6402                 return 0;
6403         } else
6404                 return 0;
6405
6406         if (ret) {
6407                 if (errno == ENODATA && d != -1) {
6408                         /*
6409                          * We need to "fake" the "use the default" values
6410                          * since the lmm struct is zeroed out at this point.
6411                          * The magic needs to be set in order to satisfy
6412                          * a check later on in the code path.
6413                          * The object_seq needs to be set for the "(Default)"
6414                          * prefix to be displayed.
6415                          */
6416                         if (param->fp_get_default_lmv) {
6417                                 struct lmv_user_md *lum = param->fp_lmv_md;
6418
6419                                 lum->lum_magic = LMV_USER_MAGIC;
6420                                 lum->lum_stripe_count = 0;
6421                                 lum->lum_stripe_offset = LMV_OFFSET_DEFAULT;
6422                                 goto dump;
6423                         } else if (param->fp_get_lmv) {
6424                                 struct lmv_user_md *lum = param->fp_lmv_md;
6425                                 int mdtidx;
6426
6427                                 ret = llapi_file_fget_mdtidx(d, &mdtidx);
6428                                 if (ret != 0)
6429                                         goto err_out;
6430                                 lum->lum_magic = LMV_MAGIC_V1;
6431                                 lum->lum_stripe_count = 0;
6432                                 lum->lum_stripe_offset = mdtidx;
6433                                 goto dump;
6434                         } else {
6435                                 struct lov_user_md *lmm =
6436                                         &param->fp_lmd->lmd_lmm;
6437
6438                                 lmm->lmm_magic = LOV_USER_MAGIC_V1;
6439                                 if (!param->fp_raw)
6440                                         ostid_set_seq(&lmm->lmm_oi,
6441                                                       FID_SEQ_LOV_DEFAULT);
6442                                 lmm->lmm_stripe_count = 0;
6443                                 lmm->lmm_stripe_size = 0;
6444                                 lmm->lmm_stripe_offset = -1;
6445                                 goto dump;
6446                         }
6447                 } else if (errno == ENODATA && p != -1) {
6448                         if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
6449                                 llapi_printf(LLAPI_MSG_NORMAL,
6450                                              "%s has no stripe info\n", path);
6451                         goto out;
6452                 } else if (errno == ENOENT) {
6453                         llapi_error(LLAPI_MSG_WARN, -ENOENT,
6454                                     "warning: %s: %s does not exist",
6455                                     __func__, path);
6456                         goto out;
6457                 } else if (errno == ENOTTY) {
6458                         ret = -errno;
6459                         llapi_error(LLAPI_MSG_ERROR, ret,
6460                                     "%s: '%s' not on a Lustre fs?",
6461                                     __func__, path);
6462                 } else {
6463                         ret = -errno;
6464 err_out:
6465                         llapi_error(LLAPI_MSG_ERROR, ret,
6466                                     "error: %s: %s failed for %s",
6467                                      __func__, d != -1 ?
6468                                                "LL_IOC_LOV_GETSTRIPE" :
6469                                                "IOC_MDC_GETFILESTRIPE", path);
6470                 }
6471
6472                 return ret;
6473         }
6474
6475 dump:
6476         if (!(param->fp_verbose & VERBOSE_MDTINDEX))
6477                 llapi_lov_dump_user_lmm(param, path, d != -1 ? LDF_IS_DIR : 0);
6478
6479 out:
6480         /* Do not get down anymore? */
6481         if (param->fp_depth == param->fp_max_depth)
6482                 return 1;
6483
6484         param->fp_depth++;
6485
6486         return 0;
6487 }
6488
6489 int llapi_getstripe(char *path, struct find_param *param)
6490 {
6491         return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
6492                               cb_get_mdt_index : cb_getstripe,
6493                               cb_common_fini, param);
6494 }
6495
6496 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
6497                       struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6498 {
6499         char raw[MAX_IOC_BUFLEN] = {'\0'};
6500         char *rawbuf = raw;
6501         struct obd_ioctl_data data = { 0 };
6502         int rc = 0;
6503
6504         data.ioc_inlbuf1 = (char *)&type;
6505         data.ioc_inllen1 = sizeof(__u32);
6506         data.ioc_inlbuf2 = (char *)&index;
6507         data.ioc_inllen2 = sizeof(__u32);
6508         data.ioc_pbuf1 = (char *)stat_buf;
6509         data.ioc_plen1 = sizeof(struct obd_statfs);
6510         data.ioc_pbuf2 = (char *)uuid_buf;
6511         data.ioc_plen2 = sizeof(struct obd_uuid);
6512
6513         rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
6514         if (rc != 0) {
6515                 llapi_error(LLAPI_MSG_ERROR, rc,
6516                             "%s: error packing ioctl data", __func__);
6517                 return rc;
6518         }
6519
6520         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
6521
6522         return rc < 0 ? -errno : 0;
6523 }
6524
6525 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
6526                      struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
6527 {
6528         int fd;
6529         int rc;
6530
6531         fd = open(path, O_RDONLY);
6532         if (fd < 0) {
6533                 rc = -errno;
6534                 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
6535                             __func__, path);
6536                 /*
6537                  * If we can't even open a file on the filesystem (e.g. with
6538                  * -ESHUTDOWN), force caller to exit or it will loop forever.
6539                  */
6540                 return -ENODEV;
6541         }
6542
6543         rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
6544
6545         close(fd);
6546
6547         return rc;
6548 }
6549
6550 #define MAX_STRING_SIZE 128
6551
6552 int llapi_ping(char *obd_type, char *obd_name)
6553 {
6554         int flags = O_RDONLY;
6555         char buf[1] = { 0 };
6556         glob_t path;
6557         int rc, fd;
6558
6559         rc = cfs_get_param_paths(&path, "%s/%s/ping",
6560                                 obd_type, obd_name);
6561         if (rc != 0)
6562                 return -errno;
6563 retry_open:
6564         fd = open(path.gl_pathv[0], flags);
6565         if (fd < 0) {
6566                 if (errno == EACCES && flags == O_RDONLY) {
6567                         flags = O_WRONLY;
6568                         goto retry_open;
6569                 }
6570                 rc = -errno;
6571                 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
6572                             path.gl_pathv[0]);
6573                 goto failed;
6574         }
6575
6576         if (flags == O_RDONLY)
6577                 rc = read(fd, buf, sizeof(buf));
6578         else
6579                 rc = write(fd, buf, sizeof(buf));
6580         if (rc < 0)
6581                 rc = -errno;
6582         close(fd);
6583
6584         if (rc == 1)
6585                 rc = 0;
6586 failed:
6587         cfs_free_param_data(&path);
6588         return rc;
6589 }
6590
6591 int llapi_target_iterate(int type_num, char **obd_type,
6592                          void *args, llapi_cb_t cb)
6593 {
6594         int i, rc = 0;
6595         glob_t param;
6596         FILE *fp;
6597
6598         for (i = 0; i < type_num; i++) {
6599                 int j;
6600
6601                 rc = cfs_get_param_paths(&param, "%s/*/uuid", obd_type[i]);
6602                 if (rc != 0)
6603                         continue;
6604
6605                 for (j = 0; j < param.gl_pathc; j++) {
6606                         char obd_uuid[UUID_MAX + 1];
6607                         char *obd_name;
6608                         char *ptr;
6609
6610                         fp = fopen(param.gl_pathv[j], "r");
6611                         if (fp == NULL) {
6612                                 rc = -errno;
6613                                 llapi_error(LLAPI_MSG_ERROR, rc,
6614                                             "error: opening '%s'",
6615                                             param.gl_pathv[j]);
6616                                 goto free_path;
6617                         }
6618
6619                         if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
6620                                 rc = -errno;
6621                                 llapi_error(LLAPI_MSG_ERROR, rc,
6622                                             "error: reading '%s'",
6623                                             param.gl_pathv[j]);
6624                                 goto free_path;
6625                         }
6626
6627                         /* Extract the obd_name from the sysfs path.
6628                          * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
6629                          */
6630                         obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
6631                         if (!obd_name) {
6632                                 rc = -EINVAL;
6633                                 goto free_path;
6634                         }
6635
6636                         /* skip /fs/lustre/'obd_type'/ */
6637                         obd_name += strlen(obd_type[i]) + 12;
6638                         /* chop off after obd_name */
6639                         ptr = strrchr(obd_name, '/');
6640                         if (ptr)
6641                                 *ptr = '\0';
6642
6643                         cb(obd_type[i], obd_name, obd_uuid, args);
6644
6645                         fclose(fp);
6646                         fp = NULL;
6647                 }
6648         }
6649 free_path:
6650         if (fp)
6651                 fclose(fp);
6652         cfs_free_param_data(&param);
6653         return rc;
6654 }
6655
6656 static void do_target_check(char *obd_type_name, char *obd_name,
6657                             char *obd_uuid, void *args)
6658 {
6659         int rc;
6660
6661         rc = llapi_ping(obd_type_name, obd_name);
6662         if (rc == ENOTCONN)
6663                 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
6664         else if (rc)
6665                 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
6666         else
6667                 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
6668 }
6669
6670 int llapi_target_check(int type_num, char **obd_type, char *dir)
6671 {
6672         return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
6673 }
6674
6675 #undef MAX_STRING_SIZE
6676
6677 /* Is this a lustre fs? */
6678 int llapi_is_lustre_mnttype(const char *type)
6679 {
6680         return strcmp(type, "lustre") == 0 || strcmp(type, "lustre_tgt") == 0;
6681 }
6682
6683 /* Is this a lustre client fs? */
6684 int llapi_is_lustre_mnt(struct mntent *mnt)
6685 {
6686         return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
6687                 strstr(mnt->mnt_fsname, ":/") != NULL);
6688 }
6689
6690 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
6691 {
6692         char fsname[PATH_MAX + 1];
6693         int root;
6694         int rc;
6695
6696         rc = llapi_search_fsname(mnt, fsname);
6697         if (rc)
6698                 return rc;
6699
6700         root = open(mnt, O_RDONLY | O_DIRECTORY);
6701         if (root < 0) {
6702                 rc = -errno;
6703                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
6704                 return rc;
6705         }
6706
6707         rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
6708         if (rc < 0)
6709                 rc = -errno;
6710         if (rc == -ENOENT && LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd))
6711                 llapi_error(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, rc,
6712                             "Cannot find pool '%s'", qctl->qc_poolname);
6713
6714         close(root);
6715         return rc;
6716 }
6717
6718 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
6719 {
6720         int root;
6721         int rc;
6722
6723         root = open(mnt, O_RDONLY | O_DIRECTORY);
6724         if (root < 0) {
6725                 rc = -errno;
6726                 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
6727                 return rc;
6728         }
6729
6730         rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
6731         if (rc < 0) {
6732                 rc = -errno;
6733                 llapi_error(LLAPI_MSG_ERROR, rc,
6734                         "ioctl on %s for getting connect flags failed", mnt);
6735         }
6736         close(root);
6737         return rc;
6738 }
6739
6740 /**
6741  * Flush cached pages from all clients.
6742  *
6743  * \param fd    File descriptor
6744  * \retval 0    success
6745  * \retval < 0  error
6746  */
6747 int llapi_file_flush(int fd)
6748 {
6749         __u64 dv;
6750
6751         return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
6752 }
6753