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