Whamcloud - gitweb
Branch HEAD
[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 <lnet/lnetctl.h>
53
54 #include <liblustre.h>
55 #include <obd.h>
56 #include <lustre_lib.h>
57 #include <obd_lov.h>
58 #include <lustre/liblustreapi.h>
59
60 static void err_msg(char *fmt, ...)
61 {
62         va_list args;
63         int tmp_errno = abs(errno);
64
65         va_start(args, fmt);
66         vfprintf(stderr, fmt, args);
67         va_end(args);
68         fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
69 }
70
71 int llapi_file_open(const char *name, int flags, int mode,
72                     unsigned long stripe_size, int stripe_offset,
73                     int stripe_count, int stripe_pattern)
74 {
75         struct lov_user_md lum = { 0 };
76         int fd, rc = 0;
77         int isdir = 0;
78         int page_size;
79
80         fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
81         if (fd < 0 && errno == EISDIR) {
82                 fd = open(name, O_DIRECTORY | O_RDONLY);
83                 isdir++;
84         }
85
86         if (fd < 0) {
87                 rc = -errno;
88                 err_msg("unable to open '%s'", name);
89                 return rc;
90         }
91
92         /* 64 KB is the largest common page size I'm aware of (on ia64), but
93          * check the local page size just in case. */
94         page_size = LOV_MIN_STRIPE_SIZE;
95         if (getpagesize() > page_size) {
96                 page_size = getpagesize();
97                 fprintf(stderr, "warning: your page size (%u) is larger than "
98                         "expected (%u).\n", page_size, LOV_MIN_STRIPE_SIZE);
99         }
100         if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
101                 errno = rc = -EINVAL;
102                 err_msg("error: bad stripe_size %lu, must be an even "
103                         "multiple of %d bytes", stripe_size, page_size);
104                 goto out;
105         }
106         if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
107                 errno = rc = -EINVAL;
108                 err_msg("error: bad stripe offset %d", stripe_offset);
109                 goto out;
110         }
111         if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
112                 errno = rc = -EINVAL;
113                 err_msg("error: bad stripe count %d", stripe_count);
114                 goto out;
115         }
116         if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
117                 errno = rc = -EINVAL;
118                 err_msg("error: stripe_size %lu * stripe_count %u "
119                         "exceeds 4GB", stripe_size, stripe_count);
120                 goto out;
121         }
122
123         /*  Initialize IOCTL striping pattern structure */
124         lum.lmm_magic = LOV_USER_MAGIC;
125         lum.lmm_pattern = stripe_pattern;
126         lum.lmm_stripe_size = stripe_size;
127         lum.lmm_stripe_count = stripe_count;
128         lum.lmm_stripe_offset = stripe_offset;
129
130         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
131                 char *errmsg = "stripe already set";
132                 rc = -errno;
133                 if (errno != EEXIST && errno != EALREADY)
134                         errmsg = strerror(errno);
135
136                 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
137                         (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
138         }
139 out:
140         if (rc) {
141                 close(fd);
142                 fd = rc;
143         }
144
145         return fd;
146 }
147
148 int llapi_file_create(const char *name, unsigned long stripe_size,
149                       int stripe_offset, int stripe_count, int stripe_pattern)
150 {
151         int fd;
152
153         fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
154                              stripe_offset, stripe_count, stripe_pattern);
155         if (fd < 0)
156                 return fd;
157
158         close(fd);
159         return 0;
160 }
161
162 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
163
164 #define MAX_LOV_UUID_COUNT      max(LOV_MAX_STRIPE_COUNT, 1000)
165 #define OBD_NOT_FOUND           (-1)
166
167 static int common_param_init(struct find_param *param)
168 {
169         param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
170         if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
171                 err_msg("error: allocation of %d bytes for ioctl",
172                         sizeof(lstat_t) + param->lumlen);
173                 return -ENOMEM;
174         }
175
176         param->got_uuids = 0;
177         param->obdindex = OBD_NOT_FOUND;
178         return 0;
179 }
180
181 static void find_param_fini(struct find_param *param)
182 {
183         if (param->lmd)
184                 free(param->lmd);
185 }
186
187 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
188 {
189         char lov_name[sizeof(struct obd_uuid)];
190         char buf[1024];
191         FILE *fp;
192         int rc = 0, index = 0;
193
194         /* Get the lov name */
195         rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
196         if (rc) {
197                 rc = errno;
198                 err_msg("error: can't get lov name.");
199                 return rc;
200         }
201
202         /* Now get the ost uuids from /proc */
203         snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
204                  lov_name);
205         fp = fopen(buf, "r");
206         if (fp == NULL) {
207                 rc = errno;
208                 err_msg("error: opening '%s'", buf);
209                 return rc;
210         }
211
212         while ((fgets(buf, sizeof(buf), fp) != NULL) && index < *ost_count) {
213                 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) < 2)
214                         break;
215                 index++;
216         }
217
218         fclose(fp);
219         *ost_count = index;
220
221         return rc;
222 }
223
224 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
225 {
226         char uuid[sizeof(struct obd_uuid)];
227         char buf[1024];
228         FILE *fp;
229         int rc = 0, index;
230
231         /* Get the lov name */
232         rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
233         if (rc) {
234                 if (errno != ENOTTY) {
235                         rc = errno;
236                         err_msg("error: can't get lov name: %s", dname);
237                 } else {
238                         rc = 0;
239                 }
240                 return rc;
241         }
242
243         param->got_uuids = 1;
244
245         /* Now get the ost uuids from /proc */
246         snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
247                  uuid);
248         fp = fopen(buf, "r");
249         if (fp == NULL) {
250                 rc = errno;
251                 err_msg("error: opening '%s'", buf);
252                 return rc;
253         }
254
255         if (!param->obduuid && !param->quiet && !param->obds_printed)
256                 printf("OBDS:\n");
257
258         while (fgets(buf, sizeof(buf), fp) != NULL) {
259                 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
260                         break;
261
262                 if (param->obduuid) {
263                         if (strncmp(param->obduuid->uuid, uuid,
264                                     sizeof(uuid)) == 0) {
265                                 param->obdindex = index;
266                                 break;
267                         }
268                 } else if (!param->quiet && !param->obds_printed) {
269                         /* Print everything */
270                         printf("%s", buf);
271                 }
272         }
273         param->obds_printed = 1;
274
275         fclose(fp);
276
277         if (!param->quiet && param->obduuid &&
278             (param->obdindex == OBD_NOT_FOUND)) {
279                 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
280                         __FUNCTION__, param->obduuid->uuid);
281                 //rc = EINVAL;
282         }
283
284         return (rc);
285 }
286
287 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
288                           int obdindex, int quiet, int header, int body)
289 {
290         int i, obdstripe = 0;
291
292         if (obdindex != OBD_NOT_FOUND) {
293                 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
294                         if (obdindex == lum->lmm_objects[i].l_ost_idx) {
295                                 printf("%s\n", path);
296                                 obdstripe = 1;
297                                 break;
298                         }
299                 }
300         } else if (!quiet) {
301                 printf("%s\n", path);
302                 obdstripe = 1;
303         }
304
305         /* if it's a directory */
306         if (is_dir) {
307                 if (obdstripe == 1) {
308                         printf("default stripe_count: %d stripe_size: %u "
309                                "stripe_offset: %d\n",
310                                lum->lmm_stripe_count == (__u16)-1 ? -1 :
311                                         lum->lmm_stripe_count,
312                                lum->lmm_stripe_size,
313                                lum->lmm_stripe_offset == (__u16)-1 ? -1 :
314                                         lum->lmm_stripe_offset);
315                 }
316                 return;
317         }
318
319         if (header && (obdstripe == 1)) {
320                 printf("lmm_magic:          0x%08X\n",  lum->lmm_magic);
321                 printf("lmm_object_gr:      "LPX64"\n", lum->lmm_object_gr);
322                 printf("lmm_object_id:      "LPX64"\n", lum->lmm_object_id);
323                 printf("lmm_stripe_count:   %u\n", (int)lum->lmm_stripe_count);
324                 printf("lmm_stripe_size:    %u\n",      lum->lmm_stripe_size);
325                 printf("lmm_stripe_pattern: %x\n",      lum->lmm_pattern);
326         }
327
328         if (body) {
329                 if ((!quiet) && (obdstripe == 1))
330                         printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
331
332                 for (i = 0; i < lum->lmm_stripe_count; i++) {
333                         int idx = lum->lmm_objects[i].l_ost_idx;
334                         long long oid = lum->lmm_objects[i].l_object_id;
335                         long long gr = lum->lmm_objects[i].l_object_gr;
336                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
337                                 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
338                                        idx, oid, oid, gr,
339                                        obdindex == idx ? " *" : "");
340                 }
341                 printf("\n");
342         }
343 }
344
345 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
346                             int is_dir, int obdindex, int quiet,
347                             int header, int body)
348 {
349         struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
350         int i, obdstripe = 0;
351
352         if (obdindex != OBD_NOT_FOUND) {
353                 for (i = 0; i < lumj->lmm_stripe_count; i++) {
354                         if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
355                                 printf("%s\n", path);
356                                 obdstripe = 1;
357                                 break;
358                         }
359                 }
360         } else if (!quiet) {
361                 printf("%s\n", path);
362                 obdstripe = 1;
363         }
364
365         if (header && obdstripe == 1) {
366                 printf("lmm_magic:          0x%08X\n",  lumj->lmm_magic);
367                 printf("lmm_object_gr:      "LPX64"\n", lumj->lmm_object_gr);
368                 printf("lmm_object_id:      "LPX64"\n", lumj->lmm_object_id);
369                 printf("lmm_stripe_count:   %u\n", (int)lumj->lmm_stripe_count);
370                 printf("lmm_stripe_size:    %u\n",      lumj->lmm_stripe_size);
371                 printf("lmm_stripe_pattern: %x\n",      lumj->lmm_pattern);
372                 printf("lmm_extent_count:   %x\n",      lumj->lmm_extent_count);
373         }
374
375         if (body) {
376                 unsigned long long start = -1, end = 0;
377                 if (!quiet && obdstripe == 1)
378                         printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
379                                "\t\tstart\t\tend\n");
380                 for (i = 0; i < lumj->lmm_stripe_count; i++) {
381                         int idx = lumj->lmm_objects[i].l_ost_idx;
382                         long long oid = lumj->lmm_objects[i].l_object_id;
383                         long long gr = lumj->lmm_objects[i].l_object_gr;
384                         if (obdindex == OBD_NOT_FOUND || obdindex == idx)
385                                 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
386                                        idx, oid, oid, gr,
387                                        obdindex == idx ? " *" : "");
388                         if (start != lumj->lmm_objects[i].l_extent_start ||
389                             end != lumj->lmm_objects[i].l_extent_end) {
390                                 start = lumj->lmm_objects[i].l_extent_start;
391                                 printf("\t%14llu", start);
392                                 end = lumj->lmm_objects[i].l_extent_end;
393                                 if (end == (unsigned long long)-1)
394                                         printf("\t\tEOF\n");
395                                 else
396                                         printf("\t\t%llu\n", end);
397                         } else {
398                                 printf("\t\t\t\t\n");
399                         }
400                 }
401                 printf("\n");
402         }
403 }
404
405 void llapi_lov_dump_user_lmm(struct find_param *param,
406                              char *path, int is_dir)
407 {
408         switch(*(__u32 *)&param->lmd->lmd_lmm) { /* lum->lmm_magic */
409         case LOV_USER_MAGIC_V1:
410                 lov_dump_user_lmm_v1(&param->lmd->lmd_lmm, path, is_dir,
411                                       param->obdindex, param->quiet,
412                                       param->verbose,
413                                       (param->verbose || !param->obduuid));
414                 break;
415         case LOV_USER_MAGIC_JOIN:
416                 lov_dump_user_lmm_join(&param->lmd->lmd_lmm, path, is_dir,
417                                        param->obdindex, param->quiet,
418                                        param->verbose,
419                                        (param->verbose || !param->obduuid));
420                 break;
421         default:
422                 printf("unknown lmm_magic:  %#x (expecting %#x)\n",
423                        *(__u32 *)&param->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
424                 return;
425         }
426 }
427
428 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
429 {
430         const char *fname;
431         char *dname;
432         int fd, rc = 0;
433
434         fname = strrchr(path, '/');
435
436         /* It should be a file (or other non-directory) */
437         if (fname == NULL) {
438                 dname = (char *)malloc(2);
439                 if (dname == NULL)
440                         return ENOMEM;
441                 strcpy(dname, ".");
442                 fname = (char *)path;
443         } else {
444                 dname = (char *)malloc(fname - path + 1);
445                 if (dname == NULL)
446                         return ENOMEM;
447                 strncpy(dname, path, fname - path);
448                 dname[fname - path] = '\0';
449                 fname++;
450         }
451
452         if ((fd = open(dname, O_RDONLY)) == -1) {
453                 rc = errno;
454                 free(dname);
455                 return rc;
456         }
457
458         strcpy((char *)lum, fname);
459         if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
460                 rc = errno;
461
462         if (close(fd) == -1 && rc == 0)
463                 rc = errno;
464
465         free(dname);
466
467         return rc;
468 }
469
470 int llapi_file_lookup(int dirfd, const char *name)
471 {
472         struct obd_ioctl_data data = { 0 };
473         char rawbuf[8192];
474         char *buf = rawbuf;
475         int rc;
476
477         if (dirfd < 0 || name == NULL)
478                 return -EINVAL;
479
480         data.ioc_version = OBD_IOCTL_VERSION;
481         data.ioc_len = sizeof(data);
482         data.ioc_inlbuf1 = (char *)name;
483         data.ioc_inllen1 = strlen(name) + 1;
484
485         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
486         if (rc) {
487                 fprintf(stderr,
488                         "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
489                         name, rc);
490                 return rc;
491         }
492
493         return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
494 }
495
496 /* some 64bit libcs implement readdir64() by calling sys_getdents().  the
497  * kernel's sys_getdents() doesn't return d_type.  */
498 unsigned char handle_dt_unknown(char *path)
499 {
500         int fd;
501
502         fd = open(path, O_DIRECTORY|O_RDONLY);
503         if (fd < 0) {
504                 if (errno == ENOTDIR)
505                         return DT_REG; /* kind of a lie */
506                 return DT_UNKNOWN;
507         }
508         close(fd);
509         return DT_DIR;
510 }
511
512 static DIR *opendir_parent(char *path)
513 {
514         DIR *parent;
515         char *fname;
516         char c;
517
518         fname = strrchr(path, '/');
519         if (fname == NULL)
520                 return opendir(".");
521
522         c = fname[1];
523         fname[1] = '\0';
524         parent = opendir(path);
525         fname[1] = c;
526         return parent;
527 }
528
529 static int llapi_semantic_traverse(char *path, DIR *parent,
530                                    semantic_func_t sem_init,
531                                    semantic_func_t sem_fini, void *data)
532 {
533         struct dirent64 *dent;
534         int len, ret;
535         DIR *d, *p = NULL;
536
537         ret = 0;
538         len = strlen(path);
539
540         d = opendir(path);
541         if (!d && errno != ENOTDIR) {
542                 fprintf(stderr, "%s: Failed to open '%s': %s.",
543                         __FUNCTION__, path, strerror(errno));
544                 return -EINVAL;
545         } else if (!d && !parent) {
546                 /* ENOTDIR. Open the parent dir. */
547                 p = opendir_parent(path);
548                 if (!p)
549                         GOTO(out, ret = -EINVAL);
550         }
551
552         if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
553                 goto err;
554
555         if (!d)
556                 GOTO(out, ret = 0);
557
558         while ((dent = readdir64(d)) != NULL) {
559                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
560                         continue;
561
562                 path[len] = 0;
563                 strcat(path, "/");
564                 strcat(path, dent->d_name);
565
566                 if (dent->d_type == DT_UNKNOWN)
567                         dent->d_type = handle_dt_unknown(path);
568
569                 switch (dent->d_type) {
570                 case DT_UNKNOWN:
571                         fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
572                                 __FUNCTION__, dent->d_name, dent->d_type);
573                         /* If we cared we could stat the file to determine
574                          * type and continue on here, but we don't since we
575                          * know d_type should be valid for lustre and this
576                          * tool only makes sense for lustre filesystems. */
577                         break;
578                 case DT_DIR:
579                         ret = llapi_semantic_traverse(path, d, sem_init,
580                                                       sem_fini, data);
581                         if (ret < 0)
582                                 goto out;
583                         break;
584                 default:
585                         ret = 0;
586                         if (sem_init) {
587                                 ret = sem_init(path, d, NULL, data);
588                                 if (ret < 0)
589                                         goto out;
590                         }
591                         if (sem_fini && ret == 0)
592                                 sem_fini(path, d, NULL, data);
593                 }
594         }
595
596 out:
597         path[len] = 0;
598
599         if (sem_fini)
600                 sem_fini(path, parent, d, data);
601 err:
602         if (d)
603                 closedir(d);
604         if (p)
605                 closedir(p);
606         return ret;
607 }
608
609 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
610  * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
611  *
612  * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
613  * The table bolow gives the answers for the specified parameters (time and
614  * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
615  * --------------------------------------
616  * 1 | file > limit; sign > 0 | -1 / -1 |
617  * 2 | file = limit; sign > 0 |  ? /  1 |
618  * 3 | file < limit; sign > 0 |  ? /  1 |
619  * 4 | file > limit; sign = 0 | -1 / -1 |
620  * 5 | file = limit; sign = 0 |  ? /  1 |  <- (see the Note below)
621  * 6 | file < limit; sign = 0 |  ? / -1 |
622  * 7 | file > limit; sign < 0 |  1 /  1 |
623  * 8 | file = limit; sign < 0 |  ? / -1 |
624  * 9 | file < limit; sign < 0 |  ? / -1 |
625  * --------------------------------------
626  * Note: 5th actually means that the file time stamp is within the interval
627  * (limit - 24hours, limit]. */
628 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
629         if (sign > 0) {
630                 if (file <= limit)
631                         return mds ? 0 : 1;
632         }
633
634         if (sign == 0) {
635                 if (file <= limit && file + 24 * 60 * 60 > limit)
636                         return mds ? 0 : 1;
637                 if (file + 24 * 60 * 60 <= limit)
638                         return mds ? 0 : -1;
639         }
640
641         if (sign < 0) {
642                 if (file > limit)
643                         return 1;
644                 if (mds)
645                         return 0;
646         }
647
648         return -1;
649 }
650
651 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
652  * Return -1 or 1 if file timestamp does not or does match the given criteria
653  * correspondingly. Return 0 if the MDS time is being checked and there are
654  * attributes on OSTs and it is not yet clear if the timespamp matches.
655  *
656  * If 0 is returned, we need to do another RPC to the OSTs to obtain the
657  * updated timestamps. */
658 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
659 {
660         int ret;
661         int rc = 0;
662
663         /* Check if file is accepted. */
664         if (param->atime) {
665                 ret = find_time_cmp(st->st_atime, param->atime,
666                                     param->asign, mds);
667                 if (ret < 0)
668                         return ret;
669                 rc = ret;
670         }
671
672         if (param->mtime) {
673                 ret = find_time_cmp(st->st_mtime, param->mtime,
674                                     param->msign, mds);
675                 if (ret < 0)
676                         return ret;
677
678                 /* If the previous check matches, but this one is not yet clear,
679                  * we should return 0 to do an RPC on OSTs. */
680                 if (rc == 1)
681                         rc = ret;
682         }
683
684         if (param->ctime) {
685                 ret = find_time_cmp(st->st_ctime, param->ctime,
686                                     param->csign, mds);
687                 if (ret < 0)
688                         return ret;
689
690                 /* If the previous check matches, but this one is not yet clear,
691                  * we should return 0 to do an RPC on OSTs. */
692                 if (rc == 1)
693                         rc = ret;
694         }
695
696         return rc;
697 }
698
699 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
700 {
701         struct find_param *param = (struct find_param *)data;
702         int decision = 1; /* 1 is accepted; -1 is rejected. */
703         lstat_t *st = &param->lmd->lmd_st;
704         int lustre_fs = 1;
705         int ret = 0;
706
707         LASSERT(parent != NULL || dir != NULL);
708
709         param->lmd->lmd_lmm.lmm_stripe_count = 0;
710
711         /* If a time or OST should be checked, the decision is not taken yet. */
712         if (param->atime || param->ctime || param->mtime || param->obduuid)
713                 decision = 0;
714
715         /* Request MDS for the stat info. */
716         if (!decision && dir) {
717                 /* retrieve needed file info */
718                 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
719                             (void *)param->lmd);
720         } else if (!decision && parent) {
721                 char *fname = strrchr(path, '/');
722                 fname = (fname == NULL ? path : fname + 1);
723
724                 /* retrieve needed file info */
725                 strncpy((char *)param->lmd, fname, param->lumlen);
726                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
727                            (void *)param->lmd);
728         }
729
730         if (ret) {
731                 if (errno == ENOTTY) {
732                         /* ioctl is not supported, it is not a lustre fs.
733                          * Do the regular lstat(2) instead. */
734                         lustre_fs = 0;
735                         ret = lstat_f(path, st);
736                         if (ret) {
737                                 err_msg("error: %s: lstat failed for %s",
738                                         __FUNCTION__, path);
739                                 return ret;
740                         }
741                 } else {
742                         err_msg("error: %s: %s failed for %s", __FUNCTION__,
743                                 dir ? "LL_IOC_MDC_GETINFO" :
744                                 "IOC_MDC_GETFILEINFO", path);
745                         return ret;
746                 }
747         }
748
749         /* Prepare odb. */
750         if (param->obduuid) {
751                 if (lustre_fs && param->got_uuids &&
752                     param->st_dev != st->st_dev) {
753                         /* A lustre/lustre mount point is crossed. */
754                         param->got_uuids = 0;
755                         param->obds_printed = 0;
756                         param->obdindex = OBD_NOT_FOUND;
757                 }
758
759                 if (lustre_fs && !param->got_uuids) {
760                         ret = setup_obd_uuids(dir ? dir : parent, path, param);
761                         if (ret)
762                                 return ret;
763                         param->st_dev = st->st_dev;
764                 } else if (!lustre_fs && param->got_uuids) {
765                         /* A lustre/non-lustre mount point is crossed. */
766                         param->got_uuids = 0;
767                         param->obdindex = OBD_NOT_FOUND;
768                 }
769         }
770
771         /* If a regular expression is presented, make the initial decision */
772         if (param->pattern != NULL) {
773                 char *fname = strrchr(path, '/');
774                 fname = (fname == NULL ? path : fname + 1);
775                 ret = fnmatch(param->pattern, fname, 0);
776                 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
777                     (ret == 0 && param->exclude_pattern))
778                         decision = -1;
779         }
780
781         /* If an OBD UUID is specified but no one matches, skip this file. */
782         if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
783                 decision = -1;
784
785         /* If a OST UUID is given, and some OST matches, check it here. */
786         if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
787                 if (!S_ISREG(st->st_mode))
788                         goto decided;
789
790                 /* Only those files should be accepted, which have a
791                  * stripe on the specified OST. */
792                 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
793                         decision = -1;
794                 } else {
795                         int i;
796                         for (i = 0;
797                              i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
798                                if (param->obdindex ==
799                                    param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
800                                         break;
801                         }
802
803                         if (i == param->lmd->lmd_lmm.lmm_stripe_count)
804                                 decision = -1;
805                 }
806         }
807
808         /* Check the time on mds. */
809         if (!decision) {
810                 int for_mds;
811
812                 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
813                                        param->lmd->lmd_lmm.lmm_stripe_count)
814                                     : 0;
815                 decision = find_time_check(st, param, for_mds);
816         }
817
818         /* If file still fits the request, ask osd for updated info.
819            The regulat stat is almost of the same speed as some new
820            'glimpse-size-ioctl'. */
821         if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
822             S_ISREG(st->st_mode)) {
823                 if (param->obdindex != OBD_NOT_FOUND) {
824                         /* Check whether the obd is active or not, if it is
825                          * not active, just print the object affected by this
826                          * failed ost 
827                          * */
828                         struct obd_statfs stat_buf;
829                         struct obd_uuid uuid_buf;
830
831                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
832                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
833                         ret = llapi_obd_statfs(path, LL_STATFS_LOV,
834                                                param->obdindex, &stat_buf, 
835                                                &uuid_buf);
836                         if (ret) {
837                                 if (ret == -ENODATA || ret == -ENODEV 
838                                     || ret == -EIO)
839                                         errno = EIO;
840                                 printf("obd_uuid: %s failed %s ",
841                                         param->obduuid->uuid, strerror(errno));
842                                 goto print_path;
843                         }
844                 }
845                 if (dir) {
846                         ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
847                                     (void *)param->lmd);
848                 } else if (parent) {
849                         ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
850                                     (void *)param->lmd);
851                 }
852
853                 if (ret) {
854                         fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
855                                 "%s.\n", __FUNCTION__, path, strerror(errno));
856                         return -EINVAL;
857                 }
858
859                 /* Check the time on osc. */
860                 if (!decision)
861                         decision = find_time_check(st, param, 0);
862         }
863
864 print_path:
865         if (decision != -1) {
866                 printf("%s", path);
867                 if (param->zeroend)
868                         printf("%c", '\0');
869                 else
870                         printf("\n");
871         }
872
873 decided:
874         /* Do not get down anymore? */
875         if (param->depth == param->maxdepth)
876                 return 1;
877
878         param->depth++;
879         return 0;
880 }
881
882 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
883 {
884         struct find_param *param = (struct find_param *)data;
885         param->depth--;
886         return 0;
887 }
888
889 int llapi_find(char *path, struct find_param *param)
890 {
891         char buf[PATH_MAX + 1];
892         int ret;
893
894         ret = common_param_init(param);
895         if (ret)
896                 return ret;
897
898         param->depth = 0;
899         strncpy(buf, path, strlen(path));
900         buf[strlen(path)] = '\0';
901
902         ret = llapi_semantic_traverse(buf, NULL, cb_find_init,
903                                       cb_common_fini, param);
904
905         find_param_fini(param);
906         return ret < 0 ? ret : 0;
907 }
908
909 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
910 {
911         struct find_param *param = (struct find_param *)data;
912         int ret = 0;
913
914         LASSERT(parent != NULL || d != NULL);
915
916         /* Prepare odb. */
917         if (!param->got_uuids) {
918                 ret = setup_obd_uuids(d ? d : parent, path, param);
919                 if (ret)
920                         return ret;
921         }
922
923         if (d) {
924                 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
925                             (void *)&param->lmd->lmd_lmm);
926         } else if (parent) {
927                 char *fname = strrchr(path, '/');
928                 fname = (fname == NULL ? path : fname + 1);
929
930                 strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
931                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
932                             (void *)&param->lmd->lmd_lmm);
933         }
934
935         if (ret) {
936                 if (errno == ENODATA) {
937                         if (!param->obduuid && !param->quiet)
938                                 printf("%s has no stripe info\n", path);
939                         goto out;
940                 } else if (errno == ENOTTY) {
941                         fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
942                                 __FUNCTION__, path);
943                 } else {
944                         err_msg("error: %s: %s failed for %s", __FUNCTION__,
945                                 d ? "LL_IOC_LOV_GETSTRIPE" :
946                                 "IOC_MDC_GETFILESTRIPE", path);
947                 }
948
949                 return ret;
950         }
951
952         llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
953 out:
954         /* Do not get down anymore? */
955         if (param->depth == param->maxdepth)
956                 return 1;
957
958         param->depth++;
959         return 0;
960 }
961
962 int llapi_getstripe(char *path, struct find_param *param)
963 {
964         int ret = 0;
965
966         ret = common_param_init(param);
967         if (ret)
968                 return ret;
969
970         param->depth = 0;
971         ret = llapi_semantic_traverse(path, NULL, cb_getstripe,
972                                       cb_common_fini, param);
973         find_param_fini(param);
974         return ret < 0 ? ret : 0;
975 }
976
977 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
978                      struct obd_statfs *stat_buf,
979                      struct obd_uuid *uuid_buf)
980 {
981         int fd;
982         char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
983         char *rawbuf = raw;
984         struct obd_ioctl_data data = { 0 };
985         int rc = 0;
986
987         data.ioc_inlbuf1 = (char *)&type;
988         data.ioc_inllen1 = sizeof(__u32);
989         data.ioc_inlbuf2 = (char *)&index;
990         data.ioc_inllen2 = sizeof(__u32);
991         data.ioc_pbuf1 = (char *)stat_buf;
992         data.ioc_plen1 = sizeof(struct obd_statfs);
993         data.ioc_pbuf2 = (char *)uuid_buf;
994         data.ioc_plen2 = sizeof(struct obd_uuid);
995
996         if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
997                 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
998                 return -EINVAL;
999         }
1000
1001         fd = open(path, O_RDONLY);
1002         if (errno == EISDIR)
1003                 fd = open(path, O_DIRECTORY | O_RDONLY);
1004
1005         if (fd < 0) {
1006                 rc = errno ? -errno : -EBADF;
1007                 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1008                 return rc;
1009         }
1010         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1011         if (rc)
1012                 rc = errno ? -errno : -EINVAL;
1013
1014         close(fd);
1015         return rc;
1016 }
1017
1018 #define MAX_STRING_SIZE 128
1019 #define DEVICES_LIST "/proc/fs/lustre/devices"
1020
1021 int llapi_ping(char *obd_type, char *obd_name)
1022 {
1023         char path[MAX_STRING_SIZE];
1024         char buf[1];
1025         int rc, fd;
1026
1027         snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1028                  obd_type, obd_name);
1029
1030         fd = open(path, O_WRONLY);
1031         if (fd < 0) {
1032                 rc = errno;
1033                 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1034                 return rc;
1035         }
1036
1037         rc = write(fd, buf, 1);
1038         close(fd);
1039
1040         if (rc == 1)
1041                 return 0;
1042         return rc;
1043 }
1044
1045 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1046 {
1047         char buf[MAX_STRING_SIZE];
1048         FILE *fp = fopen(DEVICES_LIST, "r");
1049         int i, rc = 0;
1050
1051         if (fp == NULL) {
1052                 rc = errno;
1053                 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1054                         strerror(errno));
1055                 return rc;
1056         }
1057
1058         while (fgets(buf, sizeof(buf), fp) != NULL) {
1059                 char *obd_type_name = NULL;
1060                 char *obd_name = NULL;
1061                 char *obd_uuid = NULL;
1062                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1063                 char *bufl = rawbuf;
1064                 char *bufp = buf;
1065                 struct obd_ioctl_data datal = { 0, };
1066                 struct obd_statfs osfs_buffer;
1067
1068                 while(bufp[0] == ' ')
1069                         ++bufp;
1070
1071                 for(i = 0; i < 3; i++) {
1072                         obd_type_name = strsep(&bufp, " ");
1073                 }
1074                 obd_name = strsep(&bufp, " ");
1075                 obd_uuid = strsep(&bufp, " ");
1076
1077                 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1078
1079                 memset(bufl, 0, sizeof(rawbuf));
1080                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1081                 datal.ioc_plen1 = sizeof(osfs_buffer);
1082
1083                 for (i = 0; i < type_num; i++) {
1084                         if (strcmp(obd_type_name, obd_type[i]) != 0)
1085                                 continue;
1086
1087                         cb(obd_type_name, obd_name, obd_uuid, args);
1088                 }
1089         }
1090         fclose(fp);
1091         return rc;
1092 }
1093
1094 static void do_target_check(char *obd_type_name, char *obd_name,
1095                             char *obd_uuid, void *args)
1096 {
1097         int rc;
1098
1099         rc = llapi_ping(obd_type_name, obd_name);
1100         if (rc) {
1101                 err_msg("error: check '%s'", obd_name);
1102         } else {
1103                 printf("%s active.\n", obd_name);
1104         }
1105 }
1106
1107 int llapi_target_check(int type_num, char **obd_type, char *dir)
1108 {
1109         return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1110 }
1111
1112 #undef MAX_STRING_SIZE
1113
1114 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1115 {
1116         char raw[OBD_MAX_IOCTL_BUFFER];
1117         char out[LLOG_CHUNK_SIZE];
1118         char *buf = raw;
1119         struct obd_ioctl_data data = { 0 };
1120         char key[30];
1121         DIR *root;
1122         int rc;
1123
1124         sprintf(key, "%s", keyword);
1125         memset(raw, 0, sizeof(raw));
1126         memset(out, 0, sizeof(out));
1127         data.ioc_inlbuf1 = key;
1128         data.ioc_inllen1 = strlen(key) + 1;
1129         if (node_name) {
1130                 data.ioc_inlbuf2 = node_name;
1131                 data.ioc_inllen2 = strlen(node_name) + 1;
1132         }
1133         data.ioc_pbuf1 = out;
1134         data.ioc_plen1 = sizeof(out);
1135         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1136         if (rc)
1137                 return rc;
1138
1139         root = opendir(dir);
1140         if (root == NULL) {
1141                 rc = errno;
1142                 err_msg("open %s failed", dir);
1143                 return rc;
1144         }
1145
1146         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1147         if (rc)
1148                 err_msg("ioctl OBD_IOC_CATINFO failed");
1149         else
1150                 fprintf(stdout, "%s", data.ioc_pbuf1);
1151
1152         closedir(root);
1153         return rc;
1154 }
1155
1156 /* Is this a lustre fs? */
1157 int llapi_is_lustre_mnttype(const char *type)
1158 {
1159         return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1160 }
1161
1162 /* Is this a lustre client fs? */
1163 int llapi_is_lustre_mnt(struct mntent *mnt)
1164 {
1165         return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1166                 strstr(mnt->mnt_fsname, ":/") != NULL);
1167 }
1168
1169 int llapi_quotacheck(char *mnt, int check_type)
1170 {
1171         DIR *root;
1172         int rc;
1173
1174         root = opendir(mnt);
1175         if (!root) {
1176                 err_msg("open %s failed", mnt);
1177                 return -1;
1178         }
1179
1180         rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1181
1182         closedir(root);
1183         return rc;
1184 }
1185
1186 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1187 {
1188         DIR *root;
1189         int poll_intvl = 2;
1190         int rc;
1191
1192         root = opendir(mnt);
1193         if (!root) {
1194                 err_msg("open %s failed", mnt);
1195                 return -1;
1196         }
1197
1198         while (1) {
1199                 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1200                 if (!rc)
1201                         break;
1202                 sleep(poll_intvl);
1203                 if (poll_intvl < 30)
1204                         poll_intvl *= 2;
1205         }
1206
1207         closedir(root);
1208         return rc;
1209 }
1210
1211 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1212 {
1213         DIR *root;
1214         int rc;
1215
1216         root = opendir(mnt);
1217         if (!root) {
1218                 err_msg("open %s failed", mnt);
1219                 return -1;
1220         }
1221
1222         rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1223
1224         closedir(root);
1225         return rc;
1226 }
1227
1228 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1229 {
1230         struct find_param *param = (struct find_param *)data;
1231         lstat_t *st;
1232         int rc;
1233
1234         LASSERT(parent != NULL || d != NULL);
1235
1236         if (d) {
1237                 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1238                            (void *)param->lmd);
1239         } else if (parent) {
1240                 char *fname = strrchr(path, '/');
1241                 fname = (fname == NULL ? path : fname + 1);
1242
1243                 strncpy((char *)param->lmd, fname, param->lumlen);
1244                 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1245                            (void *)param->lmd);
1246         } else {
1247                 return 0;
1248         }
1249
1250         if (rc) {
1251                 if (errno == ENODATA) {
1252                         if (!param->obduuid && !param->quiet)
1253                                 fprintf(stderr, "%s has no stripe info\n",
1254                                         path);
1255                         rc = 0;
1256                 } else if (errno != EISDIR) {
1257                         rc = errno;
1258                         err_msg("%s ioctl failed for %s.",
1259                                 d ? "LL_IOC_MDC_GETINFO" :
1260                                 "IOC_MDC_GETFILEINFO", path);
1261                 }
1262                 return rc;
1263         }
1264
1265         st = &param->lmd->lmd_st;
1266
1267         /* libc chown() will do extra check, and if the real owner is
1268          * the same as the ones to set, it won't fall into kernel, so
1269          * invoke syscall directly. */
1270         rc = syscall(SYS_chown, path, -1, -1);
1271         if (rc)
1272                 err_msg("error: chown %s (%u,%u)", path);
1273
1274         rc = chmod(path, st->st_mode);
1275         if (rc)
1276                 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1277
1278         return rc;
1279 }
1280
1281 int llapi_quotachown(char *path, int flag)
1282 {
1283         struct find_param param;
1284         int ret = 0;
1285
1286         memset(&param, 0, sizeof(param));
1287         param.recursive = 1;
1288         param.verbose = 0;
1289         param.quiet = 1;
1290
1291         ret = common_param_init(&param);
1292         if (ret)
1293                 goto out;
1294
1295         ret = llapi_semantic_traverse(path, NULL, cb_quotachown,
1296                                       NULL, &param);
1297 out:
1298         find_param_fini(&param);
1299         return ret;
1300 }
1301
1302 int llapi_getfacl(char *fname, char *cmd)
1303 {
1304         struct rmtacl_ioctl_data data;
1305         char out[RMTACL_SIZE_MAX] = "";
1306         int fd, rc;
1307
1308         data.cmd = cmd;
1309         data.cmd_len = strlen(cmd) + 1;
1310         data.res = out;
1311         data.res_len = sizeof(out);
1312
1313         fd = open(fname, 0);
1314         if (fd == -1) {
1315                 err_msg("open %s failed", fname);
1316                 return -1;
1317         }
1318
1319         rc = ioctl(fd, LL_IOC_GETFACL, &data);
1320         close(fd);
1321         if (errno == EBADE) {
1322                 fprintf(stderr, "Please use getfacl directly!\n");
1323                 rc = 1;
1324         } else if (rc) {
1325                 err_msg("getfacl %s failed", fname);
1326         } else {
1327                 printf("%s", out);
1328         }
1329
1330         return rc;
1331 }
1332
1333 int llapi_setfacl(char *fname, char *cmd)
1334 {
1335         struct rmtacl_ioctl_data data;
1336         char out[RMTACL_SIZE_MAX] = "";
1337         int fd, rc;
1338
1339         data.cmd = cmd;
1340         data.cmd_len = strlen(cmd) + 1;
1341         data.res = out;
1342         data.res_len = sizeof(out);
1343
1344         fd = open(fname, 0);
1345         if (fd == -1) {
1346                 err_msg("open %s failed", fname);
1347                 return -1;
1348         }
1349
1350         rc = ioctl(fd, LL_IOC_SETFACL, &data);
1351         close(fd);
1352         if (errno == EBADE) {
1353                 fprintf(stderr, "Please use setfacl directly!\n");
1354                 rc = 1;
1355         } else if (errno == EOPNOTSUPP) {
1356                 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1357                 rc = 1;
1358         } else if (rc) {
1359                 err_msg("setfacl %s failed", fname);
1360         } else {
1361                 printf("%s", out);
1362         }
1363
1364         return rc;
1365 }