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