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