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