Whamcloud - gitweb
8efe2db20dc1caa714ff6a53b4363251ba17b54d
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Robert Read <rread@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 /* for O_DIRECTORY */
27 #define _GNU_SOURCE
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stddef.h>
33 #include <sys/ioctl.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <dirent.h>
38 #include <stdarg.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/syscall.h>
42 #include <fnmatch.h>
43 #ifdef HAVE_ASM_TYPES_H
44 #include <asm/types.h>
45 #endif
46 #ifdef HAVE_LINUX_UNISTD_H
47 #include <linux/unistd.h>
48 #else
49 #include <unistd.h>
50 #endif
51
52 #include <liblustre.h>
53 #include <lnet/lnetctl.h>
54 #include <obd.h>
55 #include <lustre_lib.h>
56 #include <obd_lov.h>
57 #include <lustre/liblustreapi.h>
58
59 static unsigned llapi_dir_filetype_table[] = {
60         [DT_UNKNOWN]= 0,
61         [DT_FIFO]= S_IFIFO,
62         [DT_CHR] = S_IFCHR,
63         [DT_DIR] = S_IFDIR,
64         [DT_BLK] = S_IFBLK,
65         [DT_REG] = S_IFREG,
66         [DT_LNK] = S_IFLNK,
67         [DT_SOCK]= S_IFSOCK,
68 #if defined(DT_DOOR) && defined(S_IFDOOR)
69         [DT_DOOR]= S_IFDOOR,
70 #endif
71 };
72
73 #if defined(DT_DOOR) && defined(S_IFDOOR)
74 static const int DT_MAX = DT_DOOR;
75 #else
76 static const int DT_MAX = DT_SOCK;
77 #endif
78
79 static unsigned llapi_filetype_dir_table[] = {
80         [0]= DT_UNKNOWN,
81         [S_IFIFO]= DT_FIFO,
82         [S_IFCHR] = DT_CHR,
83         [S_IFDIR] = DT_DIR,
84         [S_IFBLK] = DT_BLK,
85         [S_IFREG] = DT_REG,
86         [S_IFLNK] = DT_LNK,
87         [S_IFSOCK]= DT_SOCK,
88 #if defined(DT_DOOR) && defined(S_IFDOOR)
89         [S_IFDOOR]= DT_DOOR,
90 #endif
91 };
92
93 #if defined(DT_DOOR) && defined(S_IFDOOR)
94 static const int S_IFMAX = DT_DOOR;
95 #else
96 static const int S_IFMAX = DT_SOCK;
97 #endif
98
99 /* liblustreapi message level */
100 static int llapi_msg_level = LLAPI_MSG_MAX;
101
102 void llapi_msg_set_level(int level)
103 {
104         /* ensure level is in the good range */
105         if (level < LLAPI_MSG_OFF)
106                 llapi_msg_level = LLAPI_MSG_OFF;
107         else if (level > LLAPI_MSG_MAX)
108                 llapi_msg_level = LLAPI_MSG_MAX;
109         else
110                 llapi_msg_level = level;
111 }
112
113 void llapi_err(int level, char *fmt, ...)
114 {
115         va_list args;
116         int tmp_errno = abs(errno);
117
118         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
119                 return;
120
121         va_start(args, fmt);
122         vfprintf(stderr, fmt, args);
123         va_end(args);
124
125         if (level & LLAPI_MSG_NO_ERRNO)
126                 fprintf(stderr, "\n");
127         else
128                 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
129 }
130
131 #define llapi_err_noerrno(level, fmt, a...)                             \
132         llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
133
134 void llapi_printf(int level, char *fmt, ...)
135 {
136         va_list args;
137
138         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
139                 return;
140
141         va_start(args, fmt);
142         vfprintf(stdout, fmt, args);
143         va_end(args);
144 }
145
146 int parse_size(char *optarg, unsigned long long *size,
147                unsigned long long *size_units)
148 {
149         char *end;
150
151         *size = strtoul(optarg, &end, 0);
152
153         if (*end != '\0') {
154                 if ((*end == 'b') && *(end+1) == '\0' &&
155                     (*size & (~0ULL << (64 - 9))) == 0) {
156                         *size <<= 9;
157                         *size_units = 1 << 9;
158                 } else if ((*end == 'k' || *end == 'K') &&
159                            *(end+1) == '\0' && (*size &
160                            (~0ULL << (64 - 10))) == 0) {
161                         *size <<= 10;
162                         *size_units = 1 << 10;
163                 } else if ((*end == 'm' || *end == 'M') &&
164                            *(end+1) == '\0' && (*size &
165                            (~0ULL << (64 - 20))) == 0) {
166                         *size <<= 20;
167                         *size_units = 1 << 20;
168                 } else if ((*end == 'g' || *end == 'G') &&
169                            *(end+1) == '\0' && (*size &
170                            (~0ULL << (64 - 30))) == 0) {
171                         *size <<= 30;
172                         *size_units = 1 << 30;
173                 } else if ((*end == 't' || *end == 'T') &&
174                            *(end+1) == '\0' && (*size &
175                            (~0ULL << (64 - 40))) == 0) {
176                         *size <<= 40;
177                         *size_units = 1ULL << 40;
178                 } else if ((*end == 'p' || *end == 'P') &&
179                            *(end+1) == '\0' && (*size &
180                            (~0ULL << (64 - 50))) == 0) {
181                         *size <<= 50;
182                         *size_units = 1ULL << 50;
183                 } else if ((*end == 'e' || *end == 'E') &&
184                            *(end+1) == '\0' && (*size &
185                            (~0ULL << (64 - 60))) == 0) {
186                         *size <<= 60;
187                         *size_units = 1ULL << 60;
188                 } else {
189                         return -1;
190                 }
191         }
192
193         return 0;
194 }
195
196 int llapi_file_open(const char *name, int flags, int mode,
197                     unsigned long stripe_size, int stripe_offset,
198                     int stripe_count, int stripe_pattern)
199 {
200         struct lov_user_md lum = { 0 };
201         int fd, rc = 0;
202         int isdir = 0;
203         int page_size;
204
205         fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
206         if (fd < 0 && errno == EISDIR) {
207                 fd = open(name, O_DIRECTORY | O_RDONLY);
208                 isdir++;
209         }
210
211         if (fd < 0) {
212                 rc = -errno;
213                 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
214                 return rc;
215         }
216
217         /* 64 KB is the largest common page size I'm aware of (on ia64), but
218          * check the local page size just in case. */
219         page_size = LOV_MIN_STRIPE_SIZE;
220         if (getpagesize() > page_size) {
221                 page_size = getpagesize();
222                 llapi_err_noerrno(LLAPI_MSG_WARN, 
223                                   "warning: your page size (%u) is "
224                                   "larger than expected (%u)", page_size, 
225                                   LOV_MIN_STRIPE_SIZE);
226         }
227         if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
228                 errno = rc = -EINVAL;
229                 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
230                           "must be an even multiple of %d bytes", 
231                           stripe_size, page_size);
232                 goto out;
233         }
234         if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
235                 errno = rc = -EINVAL;
236                 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d", 
237                           stripe_offset);
238                 goto out;
239         }
240         if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
241                 errno = rc = -EINVAL;
242                 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d", 
243                           stripe_count);
244                 goto out;
245         }
246         if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
247                 errno = rc = -EINVAL;
248                 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
249                           "stripe_count %u exceeds 4GB", stripe_size, 
250                           stripe_count);
251                 goto out;
252         }
253
254         /*  Initialize IOCTL striping pattern structure */
255         lum.lmm_magic = LOV_USER_MAGIC;
256         lum.lmm_pattern = stripe_pattern;
257         lum.lmm_stripe_size = stripe_size;
258         lum.lmm_stripe_count = stripe_count;
259         lum.lmm_stripe_offset = stripe_offset;
260
261         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
262                 char *errmsg = "stripe already set";
263                 rc = -errno;
264                 if (errno != EEXIST && errno != EALREADY)
265                         errmsg = strerror(errno);
266
267                 llapi_err_noerrno(LLAPI_MSG_ERROR,
268                                   "error on ioctl "LPX64" for '%s' (%d): %s",
269                                   (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
270         }
271 out:
272         if (rc) {
273                 close(fd);
274                 fd = rc;
275         }
276
277         return fd;
278 }
279
280 int llapi_file_create(const char *name, unsigned long stripe_size,
281                       int stripe_offset, int stripe_count, int stripe_pattern)
282 {
283         int fd;
284
285         fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
286                              stripe_offset, stripe_count, stripe_pattern);
287         if (fd < 0)
288                 return fd;
289
290         close(fd);
291         return 0;
292 }
293
294 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
295                               void *data, struct dirent64 *de);
296
297 #define MAX_LOV_UUID_COUNT      max(LOV_MAX_STRIPE_COUNT, 1000)
298 #define OBD_NOT_FOUND           (-1)
299
300 static int common_param_init(struct find_param *param)
301 {
302         param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
303         if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
304                 llapi_err(LLAPI_MSG_ERROR, 
305                           "error: allocation of %d bytes for ioctl",
306                           sizeof(lstat_t) + param->lumlen);
307                 return -ENOMEM;
308         }
309
310         param->got_uuids = 0;
311         param->obdindexes = NULL;
312         param->obdindex = OBD_NOT_FOUND;
313         return 0;
314 }
315
316 static void find_param_fini(struct find_param *param)
317 {
318         if (param->obdindexes)
319                 free(param->obdindexes);
320
321         if (param->lmd)
322                 free(param->lmd);
323 }
324
325 /*
326  * If uuidp is NULL, return the number of available obd uuids.
327  * If uuidp is non-NULL, then it will return the uuids of the obds. If
328  * there are more OSTs then allocated to uuidp, then an error is returned with
329  * the ost_count set to number of available obd uuids.
330  */
331 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
332 {
333         char lov_name[sizeof(struct obd_uuid)];
334         char buf[1024];
335         FILE *fp;
336         int rc = 0, index = 0;
337
338         /* Get the lov name */
339         rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
340         if (rc) {
341                 rc = errno;
342                 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name");
343                 return rc;
344         }
345
346         /* Now get the ost uuids from /proc */
347         snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
348                  lov_name);
349         fp = fopen(buf, "r");
350         if (fp == NULL) {
351                 rc = errno;
352                 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
353                 return rc;
354         }
355
356         while (fgets(buf, sizeof(buf), fp) != NULL) {
357                 if (uuidp && (index < *ost_count)) {
358                         if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
359                                 break;
360                 }
361                 index++;
362         }
363
364         fclose(fp);
365
366         if (uuidp && (index >= *ost_count))
367                 return -EOVERFLOW;
368
369         *ost_count = index;
370         return rc;
371 }
372
373 /* Here, param->obduuid points to a single obduuid, the index of which is
374  * returned in param->obdindex */
375 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
376 {
377         char uuid[sizeof(struct obd_uuid)];
378         char buf[1024];
379         FILE *fp;
380         int rc = 0, index;
381
382         /* Get the lov name */
383         rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
384         if (rc) {
385                 if (errno != ENOTTY) {
386                         rc = errno;
387                         llapi_err(LLAPI_MSG_ERROR, 
388                                   "error: can't get lov name: %s", dname);
389                 } else {
390                         rc = 0;
391                 }
392                 return rc;
393         }
394
395         param->got_uuids = 1;
396
397         /* Now get the ost uuids from /proc */
398         snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
399                  uuid);
400         fp = fopen(buf, "r");
401         if (fp == NULL) {
402                 rc = errno;
403                 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
404                 return rc;
405         }
406
407         if (!param->obduuid && !param->quiet && !param->obds_printed)
408                 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
409
410         while (fgets(buf, sizeof(buf), fp) != NULL) {
411                 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
412                         break;
413
414                 if (param->obduuid) {
415                         if (strncmp(param->obduuid->uuid, uuid,
416                                     sizeof(uuid)) == 0) {
417                                 param->obdindex = index;
418                                 break;
419                         }
420                 } else if (!param->quiet && !param->obds_printed) {
421                         /* Print everything */
422                         llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
423                 }
424         }
425         param->obds_printed = 1;
426
427         fclose(fp);
428
429         if (!param->quiet && param->obduuid &&
430             (param->obdindex == OBD_NOT_FOUND)) {
431                 llapi_err_noerrno(LLAPI_MSG_ERROR, 
432                                   "error: %s: unknown obduuid: %s",
433                                   __FUNCTION__, param->obduuid->uuid);
434                 //rc = EINVAL;
435         }
436
437         return (rc);
438 }
439
440 /* In this case, param->obduuid will be an array of obduuids and
441  * obd index for all these obduuids will be returned in
442  * param->obdindexes */
443 static int setup_obd_indexes(DIR *dir, struct find_param *param)
444 {
445         struct obd_uuid *uuids = NULL;
446         int obdcount = INIT_ALLOC_NUM_OSTS;
447         int ret, obd_valid = 0, obdnum, i;
448
449         uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
450                                           sizeof(struct obd_uuid));
451         if (uuids == NULL)
452                 return -ENOMEM;
453
454 retry_get_uuids:
455         ret = llapi_lov_get_uuids(dirfd(dir), uuids,
456                                   &obdcount);
457         if (ret) {
458                 struct obd_uuid *uuids_temp;
459
460                 if (ret == -EOVERFLOW) {
461                         uuids_temp = realloc(uuids, obdcount *
462                                              sizeof(struct obd_uuid));
463                         if (uuids_temp != NULL)
464                                 goto retry_get_uuids;
465                         else
466                                 ret = -ENOMEM;
467                 }
468
469                 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
470                 return ret;
471         }
472
473         param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
474         if (param->obdindexes == NULL)
475                 return -ENOMEM;
476
477         for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
478                 for (i = 0; i <= obdcount; i++) {
479                         if (strcmp((char *)&param->obduuid[obdnum].uuid,
480                                    (char *)&uuids[i]) == 0) {
481                                 param->obdindexes[obdnum] = i;
482                                 obd_valid++;
483                                 break;
484                         }
485                 }
486                 if (i == obdcount)
487                         param->obdindexes[obdnum] = OBD_NOT_FOUND;
488         }
489
490         if (obd_valid == 0)
491                 param->obdindex = OBD_NOT_FOUND;
492         else
493                 param->obdindex = obd_valid;
494
495         param->got_uuids = 1;
496
497         return 0;
498 }
499
500 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
501                           int obdindex, int quiet, int header, int body)
502 {
503         int i, obdstripe = 0;
504
505         if (obdindex != OBD_NOT_FOUND) {
506                 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
507                         if (obdindex == lum->lmm_objects[i].l_ost_idx) {
508                                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
509                                 obdstripe = 1;
510                                 break;
511                         }
512                 }
513         } else if (!quiet) {
514                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
515                 obdstripe = 1;
516         }
517
518         /* if it's a directory */
519         if (is_dir) {
520                 if (obdstripe == 1) {
521                         llapi_printf(LLAPI_MSG_NORMAL, 
522                                      "default stripe_count: %d stripe_size: %u "
523                                      "stripe_offset: %d\n",
524                                      lum->lmm_stripe_count == (__u16)-1 ? -1 :
525                                      lum->lmm_stripe_count,
526                                      lum->lmm_stripe_size,
527                                      lum->lmm_stripe_offset == (__u16)-1 ? -1 :
528                                      lum->lmm_stripe_offset);
529                 }
530                 return;
531         }
532
533         if (header && (obdstripe == 1)) {
534                 llapi_printf(LLAPI_MSG_NORMAL, 
535                              "lmm_magic:          0x%08X\n",  lum->lmm_magic);
536                 llapi_printf(LLAPI_MSG_NORMAL, 
537                              "lmm_object_gr:      "LPX64"\n", lum->lmm_object_gr);
538                 llapi_printf(LLAPI_MSG_NORMAL, 
539                              "lmm_object_id:      "LPX64"\n", lum->lmm_object_id);
540                 llapi_printf(LLAPI_MSG_NORMAL, 
541                              "lmm_stripe_count:   %u\n", (int)lum->lmm_stripe_count);
542                 llapi_printf(LLAPI_MSG_NORMAL, 
543                              "lmm_stripe_size:    %u\n",      lum->lmm_stripe_size);
544                 llapi_printf(LLAPI_MSG_NORMAL, 
545                              "lmm_stripe_pattern: %x\n",      lum->lmm_pattern);
546         }
547
548         if (body) {
549                 if ((!quiet) && (obdstripe == 1))
550                         llapi_printf(LLAPI_MSG_NORMAL, 
551                                      "\tobdidx\t\t objid\t\tobjid\t\t group\n");
552
553                 for (i = 0; i < lum->lmm_stripe_count; i++) {
554                         int idx = lum->lmm_objects[i].l_ost_idx;
555                         long long oid = lum->lmm_objects[i].l_object_id;
556                         long long gr = lum->lmm_objects[i].l_object_gr;
557                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
558                                 llapi_printf(LLAPI_MSG_NORMAL, 
559                                              "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
560                                              idx, oid, oid, gr,
561                                              obdindex == idx ? " *" : "");
562                 }
563                 llapi_printf(LLAPI_MSG_NORMAL, "\n");
564         }
565 }
566
567 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
568                             int is_dir, int obdindex, int quiet,
569                             int header, int body)
570 {
571         struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
572         int i, obdstripe = 0;
573
574         if (obdindex != OBD_NOT_FOUND) {
575                 for (i = 0; i < lumj->lmm_stripe_count; i++) {
576                         if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
577                                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
578                                 obdstripe = 1;
579                                 break;
580                         }
581                 }
582         } else if (!quiet) {
583                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
584                 obdstripe = 1;
585         }
586
587         if (header && obdstripe == 1) {
588                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic:          0x%08X\n",  
589                              lumj->lmm_magic);
590                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr:      "LPX64"\n", 
591                              lumj->lmm_object_gr);
592                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id:      "LPX64"\n", 
593                              lumj->lmm_object_id);
594                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count:   %u\n", 
595                              (int)lumj->lmm_stripe_count);
596                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size:    %u\n",
597                              lumj->lmm_stripe_size);
598                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
599                              lumj->lmm_pattern);
600                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count:   %x\n",
601                              lumj->lmm_extent_count);
602         }
603
604         if (body) {
605                 unsigned long long start = -1, end = 0;
606                 if (!quiet && obdstripe == 1)
607                         llapi_printf(LLAPI_MSG_NORMAL, 
608                                      "joined\tobdidx\t\t objid\t\tobjid\t\t group"
609                                      "\t\tstart\t\tend\n");
610                 for (i = 0; i < lumj->lmm_stripe_count; i++) {
611                         int idx = lumj->lmm_objects[i].l_ost_idx;
612                         long long oid = lumj->lmm_objects[i].l_object_id;
613                         long long gr = lumj->lmm_objects[i].l_object_gr;
614                         if (obdindex == OBD_NOT_FOUND || obdindex == idx)
615                                 llapi_printf(LLAPI_MSG_NORMAL, 
616                                              "\t%6u\t%14llu\t%#13llx\t%14llu%s",
617                                              idx, oid, oid, gr,
618                                              obdindex == idx ? " *" : "");
619                         if (start != lumj->lmm_objects[i].l_extent_start ||
620                             end != lumj->lmm_objects[i].l_extent_end) {
621                                 start = lumj->lmm_objects[i].l_extent_start;
622                                 llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start);
623                                 end = lumj->lmm_objects[i].l_extent_end;
624                                 if (end == (unsigned long long)-1)
625                                         llapi_printf(LLAPI_MSG_NORMAL, 
626                                                      "\t\tEOF\n");
627                                 else
628                                         llapi_printf(LLAPI_MSG_NORMAL, 
629                                                      "\t\t%llu\n", end);
630                         } else {
631                                 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
632                         }
633                 }
634                 llapi_printf(LLAPI_MSG_NORMAL, "\n");
635         }
636 }
637
638 void llapi_lov_dump_user_lmm(struct find_param *param,
639                              char *path, int is_dir)
640 {
641         switch(*(__u32 *)&param->lmd->lmd_lmm) { /* lum->lmm_magic */
642         case LOV_USER_MAGIC_V1:
643                 lov_dump_user_lmm_v1(&param->lmd->lmd_lmm, path, is_dir,
644                                       param->obdindex, param->quiet,
645                                       param->verbose,
646                                       (param->verbose || !param->obduuid));
647                 break;
648         case LOV_USER_MAGIC_JOIN:
649                 lov_dump_user_lmm_join(&param->lmd->lmd_lmm, path, is_dir,
650                                        param->obdindex, param->quiet,
651                                        param->verbose,
652                                        (param->verbose || !param->obduuid));
653                 break;
654         default:
655                 llapi_printf(LLAPI_MSG_NORMAL, 
656                              "unknown lmm_magic:  %#x (expecting %#x)\n",
657                        *(__u32 *)&param->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
658                 return;
659         }
660 }
661
662 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
663 {
664         const char *fname;
665         char *dname;
666         int fd, rc = 0;
667
668         fname = strrchr(path, '/');
669
670         /* It should be a file (or other non-directory) */
671         if (fname == NULL) {
672                 dname = (char *)malloc(2);
673                 if (dname == NULL)
674                         return ENOMEM;
675                 strcpy(dname, ".");
676                 fname = (char *)path;
677         } else {
678                 dname = (char *)malloc(fname - path + 1);
679                 if (dname == NULL)
680                         return ENOMEM;
681                 strncpy(dname, path, fname - path);
682                 dname[fname - path] = '\0';
683                 fname++;
684         }
685
686         if ((fd = open(dname, O_RDONLY)) == -1) {
687                 rc = errno;
688                 free(dname);
689                 return rc;
690         }
691
692         strcpy((char *)lum, fname);
693         if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
694                 rc = errno;
695
696         if (close(fd) == -1 && rc == 0)
697                 rc = errno;
698
699         free(dname);
700
701         return rc;
702 }
703
704 int llapi_file_lookup(int dirfd, const char *name)
705 {
706         struct obd_ioctl_data data = { 0 };
707         char rawbuf[8192];
708         char *buf = rawbuf;
709         int rc;
710
711         if (dirfd < 0 || name == NULL)
712                 return -EINVAL;
713
714         data.ioc_version = OBD_IOCTL_VERSION;
715         data.ioc_len = sizeof(data);
716         data.ioc_inlbuf1 = (char *)name;
717         data.ioc_inllen1 = strlen(name) + 1;
718
719         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
720         if (rc) {
721                 llapi_err(LLAPI_MSG_ERROR,
722                           "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
723                           name, rc);
724                 return rc;
725         }
726
727         return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
728 }
729
730 int llapi_mds_getfileinfo(char *path, DIR *parent,
731                           struct lov_user_mds_data *lmd)
732 {
733         lstat_t *st = &lmd->lmd_st;
734         char *fname = strrchr(path, '/');
735         int ret = 0;
736
737         if (parent == NULL)
738                 return -EINVAL;
739
740         fname = (fname == NULL ? path : fname + 1);
741         /* retrieve needed file info */
742         strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT));
743         ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
744
745         if (ret) {
746                 if (errno == ENOTTY) {
747                         /* ioctl is not supported, it is not a lustre fs.
748                          * Do the regular lstat(2) instead. */
749                         ret = lstat_f(path, st);
750                         if (ret) {
751                                 llapi_err(LLAPI_MSG_ERROR, 
752                                           "error: %s: lstat failed for %s",
753                                           __FUNCTION__, path);
754                                 return ret;
755                         }
756                 } else if (errno == ENOENT) {
757                         llapi_err(LLAPI_MSG_WARN, 
758                                   "warning: %s: %s does not exist", 
759                                   __FUNCTION__, path);
760                         return -ENOENT;
761                 } else {
762                         llapi_err(LLAPI_MSG_ERROR, 
763                                   "error: %s: IOC_MDC_GETFILEINFO failed for %s",
764                                   __FUNCTION__, path);
765                         return ret;
766                 }
767         }
768
769         return 0;
770 }
771
772 static DIR *opendir_parent(char *path)
773 {
774         DIR *parent;
775         char *fname;
776         char c;
777
778         fname = strrchr(path, '/');
779         if (fname == NULL)
780                 return opendir(".");
781
782         c = fname[1];
783         fname[1] = '\0';
784         parent = opendir(path);
785         fname[1] = c;
786         return parent;
787 }
788
789 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
790                                    semantic_func_t sem_init,
791                                    semantic_func_t sem_fini, void *data,
792                                    struct dirent64 *de)
793 {
794         struct dirent64 *dent;
795         int len, ret;
796         DIR *d, *p = NULL;
797
798         ret = 0;
799         len = strlen(path);
800
801         d = opendir(path);
802         if (!d && errno != ENOTDIR) {
803                 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
804                           __FUNCTION__, path);
805                 return -EINVAL;
806         } else if (!d && !parent) {
807                 /* ENOTDIR. Open the parent dir. */
808                 p = opendir_parent(path);
809                 if (!p)
810                         GOTO(out, ret = -EINVAL);
811         }
812
813         if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
814                 goto err;
815
816         if (!d)
817                 GOTO(out, ret = 0);
818
819         while ((dent = readdir64(d)) != NULL) {
820                 ((struct find_param *)data)->have_fileinfo = 0;
821
822                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
823                         continue;
824
825                 path[len] = 0;
826                 if ((len + dent->d_reclen + 2) > size) {
827                         llapi_err(LLAPI_MSG_ERROR,
828                                   "error: %s: string buffer is too small",
829                                   __FUNCTION__);
830                         break;
831                 }
832                 strcat(path, "/");
833                 strcat(path, dent->d_name);
834
835                 if (dent->d_type == DT_UNKNOWN) {
836                         lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
837
838                         ret = llapi_mds_getfileinfo(path, d,
839                                              ((struct find_param *)data)->lmd);
840                         if (ret == 0) {
841                                 ((struct find_param *)data)->have_fileinfo = 1;
842                                 dent->d_type = llapi_filetype_dir_table[st->st_mode &
843                                                                         S_IFMT];
844                         }
845                         if (ret == -ENOENT)
846                                 continue;
847                 }
848
849                 switch (dent->d_type) {
850                 case DT_UNKNOWN:
851                         llapi_err(LLAPI_MSG_ERROR, 
852                                   "error: %s: '%s' is UNKNOWN type %d",
853                                   __FUNCTION__, dent->d_name, dent->d_type);
854                         break;
855                 case DT_DIR:
856                         ret = llapi_semantic_traverse(path, size, d, sem_init,
857                                                       sem_fini, data, dent);
858                         if (ret < 0)
859                                 goto out;
860                         break;
861                 default:
862                         ret = 0;
863                         if (sem_init) {
864                                 ret = sem_init(path, d, NULL, data, dent);
865                                 if (ret < 0)
866                                         goto out;
867                         }
868                         if (sem_fini && ret == 0)
869                                 sem_fini(path, d, NULL, data, dent);
870                 }
871         }
872
873 out:
874         path[len] = 0;
875
876         if (sem_fini)
877                 sem_fini(path, parent, d, data, de);
878 err:
879         if (d)
880                 closedir(d);
881         if (p)
882                 closedir(p);
883         return ret;
884 }
885
886 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
887  * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
888  *
889  * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
890  * The table bolow gives the answers for the specified parameters (value and
891  * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
892  * --------------------------------------
893  * 1 | file > limit; sign > 0 | -1 / -1 |
894  * 2 | file = limit; sign > 0 |  ? /  1 |
895  * 3 | file < limit; sign > 0 |  ? /  1 |
896  * 4 | file > limit; sign = 0 | -1 / -1 |
897  * 5 | file = limit; sign = 0 |  ? /  1 |  <- (see the Note below)
898  * 6 | file < limit; sign = 0 |  ? / -1 |
899  * 7 | file > limit; sign < 0 |  1 /  1 |
900  * 8 | file = limit; sign < 0 |  ? / -1 |
901  * 9 | file < limit; sign < 0 |  ? / -1 |
902  * --------------------------------------
903  * Note: 5th actually means that the value is within the interval
904  * (limit - margin, limit]. */
905 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
906                           unsigned long long margin, int mds)
907 {
908         if (sign > 0) {
909                 if (file < limit)
910                         return mds ? 0 : 1;
911         }
912
913         if (sign == 0) {
914                 if (file <= limit && file + margin > limit)
915                         return mds ? 0 : 1;
916                 if (file + margin <= limit)
917                         return mds ? 0 : -1;
918         }
919
920         if (sign < 0) {
921                 if (file > limit)
922                         return 1;
923                 if (mds)
924                         return 0;
925         }
926
927         return -1;
928 }
929
930 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
931  * Return -1 or 1 if file timestamp does not or does match the given criteria
932  * correspondingly. Return 0 if the MDS time is being checked and there are
933  * attributes on OSTs and it is not yet clear if the timespamp matches.
934  *
935  * If 0 is returned, we need to do another RPC to the OSTs to obtain the
936  * updated timestamps. */
937 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
938 {
939         int ret;
940         int rc = 0;
941
942         /* Check if file is accepted. */
943         if (param->atime) {
944                 ret = find_value_cmp(st->st_atime, param->atime,
945                                      param->asign, 24 * 60 * 60, mds);
946                 if (ret < 0)
947                         return ret;
948                 rc = ret;
949         }
950
951         if (param->mtime) {
952                 ret = find_value_cmp(st->st_mtime, param->mtime,
953                                      param->msign, 24 * 60 * 60, mds);
954                 if (ret < 0)
955                         return ret;
956
957                 /* If the previous check matches, but this one is not yet clear,
958                  * we should return 0 to do an RPC on OSTs. */
959                 if (rc == 1)
960                         rc = ret;
961         }
962
963         if (param->ctime) {
964                 ret = find_value_cmp(st->st_ctime, param->ctime,
965                                      param->csign, 24 * 60 * 60, mds);
966                 if (ret < 0)
967                         return ret;
968
969                 /* If the previous check matches, but this one is not yet clear,
970                  * we should return 0 to do an RPC on OSTs. */
971                 if (rc == 1)
972                         rc = ret;
973         }
974
975         return rc;
976 }
977
978 static int cb_find_init(char *path, DIR *parent, DIR *dir,
979                         void *data, struct dirent64 *de)
980 {
981         struct find_param *param = (struct find_param *)data;
982         int decision = 1; /* 1 is accepted; -1 is rejected. */
983         lstat_t *st = &param->lmd->lmd_st;
984         int lustre_fs = 1;
985         int checked_type = 0;
986         int ret = 0;
987
988         LASSERT(parent != NULL || dir != NULL);
989
990         param->lmd->lmd_lmm.lmm_stripe_count = 0;
991
992         /* If a regular expression is presented, make the initial decision */
993         if (param->pattern != NULL) {
994                 char *fname = strrchr(path, '/');
995                 fname = (fname == NULL ? path : fname + 1);
996                 ret = fnmatch(param->pattern, fname, 0);
997                 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
998                     (ret == 0 && param->exclude_pattern))
999                         goto decided;
1000         }
1001
1002         /* See if we can check the file type from the dirent. */
1003         if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1004             de->d_type < DT_MAX) {
1005                 checked_type = 1;
1006                 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1007                         if (param->exclude_type)
1008                                 goto decided;
1009                 } else {
1010                         if (!param->exclude_type)
1011                                 goto decided;
1012                 }
1013         }
1014
1015
1016         /* If a time or OST should be checked, the decision is not taken yet. */
1017         if (param->atime || param->ctime || param->mtime || param->obduuid ||
1018             param->size)
1019                 decision = 0;
1020
1021         ret = 0;
1022         /* Request MDS for the stat info. */
1023         if (param->have_fileinfo == 0) {
1024                 if (dir) {
1025                         /* retrieve needed file info */
1026                         ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1027                                     (void *)param->lmd);
1028                 } else {
1029                         char *fname = strrchr(path, '/');
1030                         fname = (fname == NULL ? path : fname + 1);
1031
1032                         /* retrieve needed file info */
1033                         strncpy((char *)param->lmd, fname, param->lumlen);
1034                         ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1035                                    (void *)param->lmd);
1036                 }
1037         }
1038
1039         if (ret) {
1040                 if (errno == ENOTTY) {
1041                         /* ioctl is not supported, it is not a lustre fs.
1042                          * Do the regular lstat(2) instead. */
1043                         lustre_fs = 0;
1044                         ret = lstat_f(path, st);
1045                         if (ret) {
1046                                 llapi_err(LLAPI_MSG_ERROR, 
1047                                           "error: %s: lstat failed for %s",
1048                                           __FUNCTION__, path);
1049                                 return ret;
1050                         }
1051                 } else if (errno == ENOENT) {
1052                         llapi_err(LLAPI_MSG_WARN, 
1053                                   "warning: %s: %s does not exist",
1054                                   __FUNCTION__, path);
1055                         goto decided;
1056                 } else {
1057                         llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
1058                                   __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1059                                   "IOC_MDC_GETFILEINFO", path);
1060                         return ret;
1061                 }
1062         }
1063
1064         if (param->type && !checked_type) {
1065                 if ((st->st_mode & S_IFMT) == param->type) {
1066                         if (param->exclude_type)
1067                                 goto decided;
1068                 } else {
1069                         if (!param->exclude_type)
1070                                 goto decided;
1071                 }
1072         }
1073
1074         /* Prepare odb. */
1075         if (param->obduuid) {
1076                 if (lustre_fs && param->got_uuids &&
1077                     param->st_dev != st->st_dev) {
1078                         /* A lustre/lustre mount point is crossed. */
1079                         param->got_uuids = 0;
1080                         param->obds_printed = 0;
1081                         param->obdindex = OBD_NOT_FOUND;
1082                 }
1083
1084                 if (lustre_fs && !param->got_uuids) {
1085                         ret = setup_obd_indexes(dir ? dir : parent, param);
1086                         if (ret)
1087                                 return ret;
1088
1089                         param->st_dev = st->st_dev;
1090                 } else if (!lustre_fs && param->got_uuids) {
1091                         /* A lustre/non-lustre mount point is crossed. */
1092                         param->got_uuids = 0;
1093                         param->obdindex = OBD_NOT_FOUND;
1094                 }
1095         }
1096
1097         /* If an OBD UUID is specified but no one matches, skip this file. */
1098         if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1099                 goto decided;
1100
1101         /* If a OST UUID is given, and some OST matches, check it here. */
1102         if (param->obdindex != OBD_NOT_FOUND) {
1103                 if (!S_ISREG(st->st_mode))
1104                         goto decided;
1105
1106                 /* Only those files should be accepted, which have a
1107                  * stripe on the specified OST. */
1108                 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1109                         goto decided;
1110                 } else {
1111                         int i, j;
1112                         for (i = 0;
1113                              i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1114                                 for (j = 0; j < param->num_obds; j++) {
1115                                         if (param->obdindexes[j] ==
1116                                             param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
1117                                                 goto obd_matches;
1118                                 }
1119                         }
1120
1121                         if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1122                                 goto decided;
1123                 }
1124         }
1125
1126         /* Check the time on mds. */
1127         if (!decision) {
1128                 int for_mds;
1129
1130                 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1131                                        param->lmd->lmd_lmm.lmm_stripe_count)
1132                                     : 0;
1133                 decision = find_time_check(st, param, for_mds);
1134         }
1135
1136 obd_matches:
1137         /* If file still fits the request, ask osd for updated info.
1138            The regulat stat is almost of the same speed as some new
1139            'glimpse-size-ioctl'. */
1140         if (!decision && S_ISREG(st->st_mode) &&
1141             (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1142                 if (param->obdindex != OBD_NOT_FOUND) {
1143                         /* Check whether the obd is active or not, if it is
1144                          * not active, just print the object affected by this
1145                          * failed ost 
1146                          * */
1147                         struct obd_statfs stat_buf;
1148                         struct obd_uuid uuid_buf;
1149
1150                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
1151                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1152                         ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1153                                                param->obdindex, &stat_buf, 
1154                                                &uuid_buf);
1155                         if (ret) {
1156                                 if (ret == -ENODATA || ret == -ENODEV 
1157                                     || ret == -EIO)
1158                                         errno = EIO;
1159                                 llapi_printf(LLAPI_MSG_NORMAL, 
1160                                              "obd_uuid: %s failed %s ",
1161                                              param->obduuid->uuid, 
1162                                              strerror(errno));
1163                                 goto print_path;
1164                         }
1165                 }
1166                 if (dir) {
1167                         ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1168                                     (void *)param->lmd);
1169                 } else if (parent) {
1170                         ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1171                                     (void *)param->lmd);
1172                 }
1173
1174                 if (ret) {
1175                         if (errno == ENOENT) {
1176                                 llapi_err(LLAPI_MSG_ERROR, 
1177                                           "warning: %s: %s does not exist",
1178                                           __FUNCTION__, path);
1179                                 goto decided;
1180                         } else {
1181                                 llapi_err(LLAPI_MSG_ERROR, 
1182                                           "%s: IOC_LOV_GETINFO on %s failed",
1183                                           __FUNCTION__, path);
1184                                 return ret;
1185                         }
1186                 }
1187
1188                 /* Check the time on osc. */
1189                 decision = find_time_check(st, param, 0);
1190                 if (decision == -1)
1191                         goto decided;
1192         }
1193
1194         if (param->size)
1195                 decision = find_value_cmp(st->st_size, param->size,
1196                                           param->size_sign, param->size_units,
1197                                           0);
1198
1199 print_path:
1200         if (decision != -1) {
1201                 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1202                 if (param->zeroend)
1203                         llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1204                 else
1205                         llapi_printf(LLAPI_MSG_NORMAL, "\n");
1206         }
1207
1208 decided:
1209         /* Do not get down anymore? */
1210         if (param->depth == param->maxdepth)
1211                 return 1;
1212
1213         param->depth++;
1214         return 0;
1215 }
1216
1217 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1218                           struct dirent64 *de)
1219 {
1220         struct find_param *param = (struct find_param *)data;
1221         param->depth--;
1222         return 0;
1223 }
1224
1225 int llapi_find(char *path, struct find_param *param)
1226 {
1227         char *buf;
1228         int ret, len = strlen(path);
1229
1230         if (len > PATH_MAX) {
1231                 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1232                           __FUNCTION__, path);
1233                 return -EINVAL;
1234         }
1235
1236         buf = (char *)malloc(PATH_MAX + 1);
1237         if (!buf)
1238                 return -ENOMEM;
1239
1240         ret = common_param_init(param);
1241         if (ret) {
1242                 free(buf);
1243                 return ret;
1244         }
1245
1246         param->depth = 0;
1247
1248         strncpy(buf, path, PATH_MAX + 1);
1249         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1250                                       cb_common_fini, param, NULL);
1251
1252         find_param_fini(param);
1253         free(buf);
1254         return ret < 0 ? ret : 0;
1255 }
1256
1257 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1258                         struct dirent64 *de)
1259 {
1260         struct find_param *param = (struct find_param *)data;
1261         int ret = 0;
1262
1263         LASSERT(parent != NULL || d != NULL);
1264
1265         /* Prepare odb. */
1266         if (!param->got_uuids) {
1267                 ret = setup_obd_uuid(d ? d : parent, path, param);
1268                 if (ret)
1269                         return ret;
1270         }
1271
1272         if (d) {
1273                 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1274                             (void *)&param->lmd->lmd_lmm);
1275         } else if (parent) {
1276                 char *fname = strrchr(path, '/');
1277                 fname = (fname == NULL ? path : fname + 1);
1278
1279                 strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
1280                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1281                             (void *)&param->lmd->lmd_lmm);
1282         }
1283
1284         if (ret) {
1285                 if (errno == ENODATA) {
1286                         if (!param->obduuid && !param->quiet)
1287                                 llapi_printf(LLAPI_MSG_NORMAL, 
1288                                              "%s has no stripe info\n", path);
1289                         goto out;
1290                 } else if (errno == ENOTTY) {
1291                         llapi_err(LLAPI_MSG_ERROR, 
1292                                   "%s: '%s' not on a Lustre fs?",
1293                                   __FUNCTION__, path);
1294                 } else if (errno == ENOENT) {
1295                         llapi_err(LLAPI_MSG_WARN, 
1296                                   "warning: %s: %s does not exist",
1297                                   __FUNCTION__, path);
1298                         goto out;
1299                 } else {
1300                         llapi_err(LLAPI_MSG_ERROR, 
1301                                   "error: %s: %s failed for %s",
1302                                    __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1303                                   "IOC_MDC_GETFILESTRIPE", path);
1304                 }
1305
1306                 return ret;
1307         }
1308
1309         llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1310 out:
1311         /* Do not get down anymore? */
1312         if (param->depth == param->maxdepth)
1313                 return 1;
1314
1315         param->depth++;
1316         return 0;
1317 }
1318
1319 int llapi_getstripe(char *path, struct find_param *param)
1320 {
1321         char *buf;
1322         int ret = 0, len = strlen(path);
1323
1324         if (len > PATH_MAX) {
1325                 llapi_err(LLAPI_MSG_ERROR, 
1326                           "%s: Path name '%s' is too long",
1327                           __FUNCTION__, path);
1328                 return -EINVAL;
1329         }
1330
1331         buf = (char *)malloc(PATH_MAX + 1);
1332         if (!buf)
1333                 return -ENOMEM;
1334
1335         ret = common_param_init(param);
1336         if (ret) {
1337                 free(buf);
1338                 return ret;
1339         }
1340
1341         param->depth = 0;
1342
1343         strncpy(buf, path, PATH_MAX + 1);
1344         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1345                                       cb_common_fini, param, NULL);
1346         find_param_fini(param);
1347         free(buf);
1348         return ret < 0 ? ret : 0;
1349 }
1350
1351 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1352                      struct obd_statfs *stat_buf,
1353                      struct obd_uuid *uuid_buf)
1354 {
1355         int fd;
1356         char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1357         char *rawbuf = raw;
1358         struct obd_ioctl_data data = { 0 };
1359         int rc = 0;
1360
1361         data.ioc_inlbuf1 = (char *)&type;
1362         data.ioc_inllen1 = sizeof(__u32);
1363         data.ioc_inlbuf2 = (char *)&index;
1364         data.ioc_inllen2 = sizeof(__u32);
1365         data.ioc_pbuf1 = (char *)stat_buf;
1366         data.ioc_plen1 = sizeof(struct obd_statfs);
1367         data.ioc_pbuf2 = (char *)uuid_buf;
1368         data.ioc_plen2 = sizeof(struct obd_uuid);
1369
1370         if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1371                 llapi_err(LLAPI_MSG_ERROR, 
1372                           "llapi_obd_statfs: error packing ioctl data");
1373                 return rc;
1374         }
1375
1376         fd = open(path, O_RDONLY);
1377         if (errno == EISDIR)
1378                 fd = open(path, O_DIRECTORY | O_RDONLY);
1379
1380         if (fd < 0) {
1381                 rc = errno ? -errno : -EBADF;
1382                 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'", 
1383                           __FUNCTION__, path);
1384                 return rc;
1385         }
1386         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1387         if (rc)
1388                 rc = errno ? -errno : -EINVAL;
1389
1390         close(fd);
1391         return rc;
1392 }
1393
1394 #define MAX_STRING_SIZE 128
1395 #define DEVICES_LIST "/proc/fs/lustre/devices"
1396
1397 int llapi_ping(char *obd_type, char *obd_name)
1398 {
1399         char path[MAX_STRING_SIZE];
1400         char buf[1];
1401         int rc, fd;
1402
1403         snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1404                  obd_type, obd_name);
1405
1406         fd = open(path, O_WRONLY);
1407         if (fd < 0) {
1408                 rc = errno;
1409                 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1410                 return rc;
1411         }
1412
1413         rc = write(fd, buf, 1);
1414         close(fd);
1415
1416         if (rc == 1)
1417                 return 0;
1418         return rc;
1419 }
1420
1421 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1422 {
1423         char buf[MAX_STRING_SIZE];
1424         FILE *fp = fopen(DEVICES_LIST, "r");
1425         int i, rc = 0;
1426
1427         if (fp == NULL) {
1428                 rc = errno;
1429                 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1430                 return rc;
1431         }
1432
1433         while (fgets(buf, sizeof(buf), fp) != NULL) {
1434                 char *obd_type_name = NULL;
1435                 char *obd_name = NULL;
1436                 char *obd_uuid = NULL;
1437                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1438                 char *bufl = rawbuf;
1439                 char *bufp = buf;
1440                 struct obd_ioctl_data datal = { 0, };
1441                 struct obd_statfs osfs_buffer;
1442
1443                 while(bufp[0] == ' ')
1444                         ++bufp;
1445
1446                 for(i = 0; i < 3; i++) {
1447                         obd_type_name = strsep(&bufp, " ");
1448                 }
1449                 obd_name = strsep(&bufp, " ");
1450                 obd_uuid = strsep(&bufp, " ");
1451
1452                 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1453
1454                 memset(bufl, 0, sizeof(rawbuf));
1455                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1456                 datal.ioc_plen1 = sizeof(osfs_buffer);
1457
1458                 for (i = 0; i < type_num; i++) {
1459                         if (strcmp(obd_type_name, obd_type[i]) != 0)
1460                                 continue;
1461
1462                         cb(obd_type_name, obd_name, obd_uuid, args);
1463                 }
1464         }
1465         fclose(fp);
1466         return rc;
1467 }
1468
1469 static void do_target_check(char *obd_type_name, char *obd_name,
1470                             char *obd_uuid, void *args)
1471 {
1472         int rc;
1473
1474         rc = llapi_ping(obd_type_name, obd_name);
1475         if (rc) {
1476                 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1477         } else {
1478                 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1479         }
1480 }
1481
1482 int llapi_target_check(int type_num, char **obd_type, char *dir)
1483 {
1484         return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1485 }
1486
1487 #undef MAX_STRING_SIZE
1488
1489 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1490 {
1491         char raw[OBD_MAX_IOCTL_BUFFER];
1492         char out[LLOG_CHUNK_SIZE];
1493         char *buf = raw;
1494         struct obd_ioctl_data data = { 0 };
1495         char key[30];
1496         DIR *root;
1497         int rc;
1498
1499         sprintf(key, "%s", keyword);
1500         memset(raw, 0, sizeof(raw));
1501         memset(out, 0, sizeof(out));
1502         data.ioc_inlbuf1 = key;
1503         data.ioc_inllen1 = strlen(key) + 1;
1504         if (node_name) {
1505                 data.ioc_inlbuf2 = node_name;
1506                 data.ioc_inllen2 = strlen(node_name) + 1;
1507         }
1508         data.ioc_pbuf1 = out;
1509         data.ioc_plen1 = sizeof(out);
1510         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1511         if (rc)
1512                 return rc;
1513
1514         root = opendir(dir);
1515         if (root == NULL) {
1516                 rc = errno;
1517                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1518                 return rc;
1519         }
1520
1521         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1522         if (rc)
1523                 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1524         else
1525                 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1526
1527         closedir(root);
1528         return rc;
1529 }
1530
1531 /* Is this a lustre fs? */
1532 int llapi_is_lustre_mnttype(const char *type)
1533 {
1534         return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1535 }
1536
1537 /* Is this a lustre client fs? */
1538 int llapi_is_lustre_mnt(struct mntent *mnt)
1539 {
1540         return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1541                 strstr(mnt->mnt_fsname, ":/") != NULL);
1542 }
1543
1544 int llapi_quotacheck(char *mnt, int check_type)
1545 {
1546         DIR *root;
1547         int rc;
1548
1549         root = opendir(mnt);
1550         if (!root) {
1551                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1552                 return -1;
1553         }
1554
1555         rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1556
1557         closedir(root);
1558         return rc;
1559 }
1560
1561 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1562 {
1563         DIR *root;
1564         int poll_intvl = 2;
1565         int rc;
1566
1567         root = opendir(mnt);
1568         if (!root) {
1569                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1570                 return -1;
1571         }
1572
1573         while (1) {
1574                 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1575                 if (!rc)
1576                         break;
1577                 sleep(poll_intvl);
1578                 if (poll_intvl < 30)
1579                         poll_intvl *= 2;
1580         }
1581
1582         closedir(root);
1583         return rc;
1584 }
1585
1586 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1587 {
1588         DIR *root;
1589         int rc;
1590
1591         root = opendir(mnt);
1592         if (!root) {
1593                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1594                 return -1;
1595         }
1596
1597         rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1598
1599         closedir(root);
1600         return rc;
1601 }
1602
1603 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1604                          struct dirent64 *de)
1605 {
1606         struct find_param *param = (struct find_param *)data;
1607         lstat_t *st;
1608         int rc;
1609
1610         LASSERT(parent != NULL || d != NULL);
1611
1612         if (d) {
1613                 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1614                            (void *)param->lmd);
1615         } else if (parent) {
1616                 char *fname = strrchr(path, '/');
1617                 fname = (fname == NULL ? path : fname + 1);
1618
1619                 strncpy((char *)param->lmd, fname, param->lumlen);
1620                 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1621                            (void *)param->lmd);
1622         } else {
1623                 return 0;
1624         }
1625
1626         if (rc) {
1627                 if (errno == ENODATA) {
1628                         if (!param->obduuid && !param->quiet)
1629                                 llapi_err(LLAPI_MSG_ERROR, 
1630                                           "%s has no stripe info", path);
1631                         rc = 0;
1632                 } else if (errno == ENOENT) {
1633                         llapi_err(LLAPI_MSG_ERROR, 
1634                                   "warning: %s: %s does not exist",
1635                                   __FUNCTION__, path);
1636                         rc = 0;
1637                 } else if (errno != EISDIR) {
1638                         rc = errno;
1639                         llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
1640                                   d ? "LL_IOC_MDC_GETINFO" :
1641                                   "IOC_MDC_GETFILEINFO", path);
1642                 }
1643                 return rc;
1644         }
1645
1646         st = &param->lmd->lmd_st;
1647
1648         /* libc chown() will do extra check, and if the real owner is
1649          * the same as the ones to set, it won't fall into kernel, so
1650          * invoke syscall directly. */
1651         rc = syscall(SYS_chown, path, -1, -1);
1652         if (rc)
1653                 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
1654
1655         rc = chmod(path, st->st_mode);
1656         if (rc)
1657                 llapi_err(LLAPI_MSG_ERROR,"error: chmod %s (%hu)", path, st->st_mode);
1658
1659         return rc;
1660 }
1661
1662 int llapi_quotachown(char *path, int flag)
1663 {
1664         struct find_param param;
1665         char *buf;
1666         int ret = 0, len = strlen(path);
1667
1668         if (len > PATH_MAX) {
1669                 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1670                           __FUNCTION__, path);
1671                 return -EINVAL;
1672         }
1673
1674         buf = (char *)malloc(PATH_MAX + 1);
1675         if (!buf)
1676                 return -ENOMEM;
1677
1678         memset(&param, 0, sizeof(param));
1679         param.recursive = 1;
1680         param.verbose = 0;
1681         param.quiet = 1;
1682
1683         ret = common_param_init(&param);
1684         if (ret)
1685                 goto out;
1686
1687         strncpy(buf, path, PATH_MAX + 1);
1688         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1689                                       NULL, &param, NULL);
1690 out:
1691         find_param_fini(&param);
1692         free(buf);
1693         return ret;
1694 }
1695
1696 int llapi_getfacl(char *fname, char *cmd)
1697 {
1698         struct rmtacl_ioctl_data data;
1699         char out[RMTACL_SIZE_MAX] = "";
1700         int fd, rc;
1701
1702         data.cmd = cmd;
1703         data.cmd_len = strlen(cmd) + 1;
1704         data.res = out;
1705         data.res_len = sizeof(out);
1706
1707         fd = open(fname, 0);
1708         if (fd == -1) {
1709                 llapi_err(LLAPI_MSG_ERROR,"open %s failed", fname);
1710                 return -1;
1711         }
1712
1713         rc = ioctl(fd, LL_IOC_GETFACL, &data);
1714         close(fd);
1715         if (errno == EBADE) {
1716                 llapi_err(LLAPI_MSG_ERROR, "Please use getfacl directly!");
1717                 rc = 1;
1718         } else if (rc) {
1719                 llapi_err(LLAPI_MSG_ERROR,"getfacl %s failed", fname);
1720         } else {
1721                 llapi_printf(LLAPI_MSG_NORMAL, "%s", out);
1722         }
1723
1724         return rc;
1725 }
1726
1727 int llapi_setfacl(char *fname, char *cmd)
1728 {
1729         struct rmtacl_ioctl_data data;
1730         char out[RMTACL_SIZE_MAX] = "";
1731         int fd, rc;
1732
1733         data.cmd = cmd;
1734         data.cmd_len = strlen(cmd) + 1;
1735         data.res = out;
1736         data.res_len = sizeof(out);
1737
1738         fd = open(fname, 0);
1739         if (fd == -1) {
1740                 llapi_err(LLAPI_MSG_ERROR,"open %s failed", fname);
1741                 return -1;
1742         }
1743
1744         rc = ioctl(fd, LL_IOC_SETFACL, &data);
1745         close(fd);
1746         if (errno == EBADE) {
1747                 llapi_err(LLAPI_MSG_ERROR, "Please use setfacl directly!");
1748                 rc = 1;
1749         } else if (errno == EOPNOTSUPP) {
1750                 llapi_err(LLAPI_MSG_ERROR, "setfacl: %s: %s", fname);
1751                 rc = 1;
1752         } else if (rc) {
1753                 llapi_err(LLAPI_MSG_ERROR,"setfacl %s failed", fname);
1754         } else {
1755                 llapi_printf(LLAPI_MSG_NORMAL, "%s", out);
1756         }
1757
1758         return rc;
1759 }