Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Robert Read <rread@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 /* for O_DIRECTORY */
27 #define _GNU_SOURCE
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stddef.h>
33 #include <sys/ioctl.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <dirent.h>
38 #include <stdarg.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/syscall.h>
42 #include <fnmatch.h>
43 #ifdef HAVE_ASM_TYPES_H
44 #include <asm/types.h>
45 #endif
46 #ifdef HAVE_LINUX_UNISTD_H
47 #include <linux/unistd.h>
48 #else
49 #include <unistd.h>
50 #endif
51
52 #include <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, int size, 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                 if ((len + dent->d_reclen + 2) > size) {
563                         fprintf(stderr,
564                                 "error: %s: string buffer is too small\n",
565                                 __FUNCTION__);
566                         break;
567                 }
568                 strcat(path, "/");
569                 strcat(path, dent->d_name);
570
571                 if (dent->d_type == DT_UNKNOWN)
572                         dent->d_type = handle_dt_unknown(path);
573
574                 switch (dent->d_type) {
575                 case DT_UNKNOWN:
576                         fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
577                                 __FUNCTION__, dent->d_name, dent->d_type);
578                         /* If we cared we could stat the file to determine
579                          * type and continue on here, but we don't since we
580                          * know d_type should be valid for lustre and this
581                          * tool only makes sense for lustre filesystems. */
582                         break;
583                 case DT_DIR:
584                         ret = llapi_semantic_traverse(path, size, d, sem_init,
585                                                       sem_fini, data);
586                         if (ret < 0)
587                                 goto out;
588                         break;
589                 default:
590                         ret = 0;
591                         if (sem_init) {
592                                 ret = sem_init(path, d, NULL, data);
593                                 if (ret < 0)
594                                         goto out;
595                         }
596                         if (sem_fini && ret == 0)
597                                 sem_fini(path, d, NULL, data);
598                 }
599         }
600
601 out:
602         path[len] = 0;
603
604         if (sem_fini)
605                 sem_fini(path, parent, d, data);
606 err:
607         if (d)
608                 closedir(d);
609         if (p)
610                 closedir(p);
611         return ret;
612 }
613
614 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
615  * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
616  *
617  * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
618  * The table bolow gives the answers for the specified parameters (time and
619  * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
620  * --------------------------------------
621  * 1 | file > limit; sign > 0 | -1 / -1 |
622  * 2 | file = limit; sign > 0 |  ? /  1 |
623  * 3 | file < limit; sign > 0 |  ? /  1 |
624  * 4 | file > limit; sign = 0 | -1 / -1 |
625  * 5 | file = limit; sign = 0 |  ? /  1 |  <- (see the Note below)
626  * 6 | file < limit; sign = 0 |  ? / -1 |
627  * 7 | file > limit; sign < 0 |  1 /  1 |
628  * 8 | file = limit; sign < 0 |  ? / -1 |
629  * 9 | file < limit; sign < 0 |  ? / -1 |
630  * --------------------------------------
631  * Note: 5th actually means that the file time stamp is within the interval
632  * (limit - 24hours, limit]. */
633 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
634         if (sign > 0) {
635                 if (file <= limit)
636                         return mds ? 0 : 1;
637         }
638
639         if (sign == 0) {
640                 if (file <= limit && file + 24 * 60 * 60 > limit)
641                         return mds ? 0 : 1;
642                 if (file + 24 * 60 * 60 <= limit)
643                         return mds ? 0 : -1;
644         }
645
646         if (sign < 0) {
647                 if (file > limit)
648                         return 1;
649                 if (mds)
650                         return 0;
651         }
652
653         return -1;
654 }
655
656 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
657  * Return -1 or 1 if file timestamp does not or does match the given criteria
658  * correspondingly. Return 0 if the MDS time is being checked and there are
659  * attributes on OSTs and it is not yet clear if the timespamp matches.
660  *
661  * If 0 is returned, we need to do another RPC to the OSTs to obtain the
662  * updated timestamps. */
663 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
664 {
665         int ret;
666         int rc = 0;
667
668         /* Check if file is accepted. */
669         if (param->atime) {
670                 ret = find_time_cmp(st->st_atime, param->atime,
671                                     param->asign, mds);
672                 if (ret < 0)
673                         return ret;
674                 rc = ret;
675         }
676
677         if (param->mtime) {
678                 ret = find_time_cmp(st->st_mtime, param->mtime,
679                                     param->msign, mds);
680                 if (ret < 0)
681                         return ret;
682
683                 /* If the previous check matches, but this one is not yet clear,
684                  * we should return 0 to do an RPC on OSTs. */
685                 if (rc == 1)
686                         rc = ret;
687         }
688
689         if (param->ctime) {
690                 ret = find_time_cmp(st->st_ctime, param->ctime,
691                                     param->csign, mds);
692                 if (ret < 0)
693                         return ret;
694
695                 /* If the previous check matches, but this one is not yet clear,
696                  * we should return 0 to do an RPC on OSTs. */
697                 if (rc == 1)
698                         rc = ret;
699         }
700
701         return rc;
702 }
703
704 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
705 {
706         struct find_param *param = (struct find_param *)data;
707         int decision = 1; /* 1 is accepted; -1 is rejected. */
708         lstat_t *st = &param->lmd->lmd_st;
709         int lustre_fs = 1;
710         int ret = 0;
711
712         LASSERT(parent != NULL || dir != NULL);
713
714         param->lmd->lmd_lmm.lmm_stripe_count = 0;
715
716         /* If a time or OST should be checked, the decision is not taken yet. */
717         if (param->atime || param->ctime || param->mtime || param->obduuid)
718                 decision = 0;
719
720         /* Request MDS for the stat info. */
721         if (!decision && dir) {
722                 /* retrieve needed file info */
723                 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
724                             (void *)param->lmd);
725         } else if (!decision && parent) {
726                 char *fname = strrchr(path, '/');
727                 fname = (fname == NULL ? path : fname + 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 if (errno == ENOENT) {
747                         err_msg("warning: %s: %s does not exist",
748                                 __FUNCTION__, path);
749                         goto decided;
750                 } else {
751                         err_msg("error: %s: %s failed for %s", __FUNCTION__,
752                                 dir ? "LL_IOC_MDC_GETINFO" :
753                                 "IOC_MDC_GETFILEINFO", path);
754                         return ret;
755                 }
756         }
757
758         /* Prepare odb. */
759         if (param->obduuid) {
760                 if (lustre_fs && param->got_uuids &&
761                     param->st_dev != st->st_dev) {
762                         /* A lustre/lustre mount point is crossed. */
763                         param->got_uuids = 0;
764                         param->obds_printed = 0;
765                         param->obdindex = OBD_NOT_FOUND;
766                 }
767
768                 if (lustre_fs && !param->got_uuids) {
769                         ret = setup_obd_uuids(dir ? dir : parent, path, param);
770                         if (ret)
771                                 return ret;
772                         param->st_dev = st->st_dev;
773                 } else if (!lustre_fs && param->got_uuids) {
774                         /* A lustre/non-lustre mount point is crossed. */
775                         param->got_uuids = 0;
776                         param->obdindex = OBD_NOT_FOUND;
777                 }
778         }
779
780         /* If a regular expression is presented, make the initial decision */
781         if (param->pattern != NULL) {
782                 char *fname = strrchr(path, '/');
783                 fname = (fname == NULL ? path : fname + 1);
784                 ret = fnmatch(param->pattern, fname, 0);
785                 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
786                     (ret == 0 && param->exclude_pattern))
787                         decision = -1;
788         }
789
790         /* If an OBD UUID is specified but no one matches, skip this file. */
791         if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
792                 decision = -1;
793
794         /* If a OST UUID is given, and some OST matches, check it here. */
795         if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
796                 if (!S_ISREG(st->st_mode))
797                         goto decided;
798
799                 /* Only those files should be accepted, which have a
800                  * stripe on the specified OST. */
801                 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
802                         decision = -1;
803                 } else {
804                         int i;
805                         for (i = 0;
806                              i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
807                                if (param->obdindex ==
808                                    param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
809                                         break;
810                         }
811
812                         if (i == param->lmd->lmd_lmm.lmm_stripe_count)
813                                 decision = -1;
814                 }
815         }
816
817         /* Check the time on mds. */
818         if (!decision) {
819                 int for_mds;
820
821                 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
822                                        param->lmd->lmd_lmm.lmm_stripe_count)
823                                     : 0;
824                 decision = find_time_check(st, param, for_mds);
825         }
826
827         /* If file still fits the request, ask osd for updated info.
828            The regulat stat is almost of the same speed as some new
829            'glimpse-size-ioctl'. */
830         if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
831             S_ISREG(st->st_mode)) {
832                 if (param->obdindex != OBD_NOT_FOUND) {
833                         /* Check whether the obd is active or not, if it is
834                          * not active, just print the object affected by this
835                          * failed ost 
836                          * */
837                         struct obd_statfs stat_buf;
838                         struct obd_uuid uuid_buf;
839
840                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
841                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
842                         ret = llapi_obd_statfs(path, LL_STATFS_LOV,
843                                                param->obdindex, &stat_buf, 
844                                                &uuid_buf);
845                         if (ret) {
846                                 if (ret == -ENODATA || ret == -ENODEV 
847                                     || ret == -EIO)
848                                         errno = EIO;
849                                 printf("obd_uuid: %s failed %s ",
850                                         param->obduuid->uuid, strerror(errno));
851                                 goto print_path;
852                         }
853                 }
854                 if (dir) {
855                         ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
856                                     (void *)param->lmd);
857                 } else if (parent) {
858                         ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
859                                     (void *)param->lmd);
860                 }
861
862                 if (ret) {
863                         if (errno == ENOENT) {
864                                 err_msg("warning: %s: %s does not exist",
865                                         __FUNCTION__, path);
866                                 goto decided;
867                         } else {
868                                 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
869                                         "%s.\n", __FUNCTION__, path, strerror(errno));
870                                 return ret;
871                         }
872                 }
873
874                 /* Check the time on osc. */
875                 if (!decision)
876                         decision = find_time_check(st, param, 0);
877         }
878
879 print_path:
880         if (decision != -1) {
881                 printf("%s", path);
882                 if (param->zeroend)
883                         printf("%c", '\0');
884                 else
885                         printf("\n");
886         }
887
888 decided:
889         /* Do not get down anymore? */
890         if (param->depth == param->maxdepth)
891                 return 1;
892
893         param->depth++;
894         return 0;
895 }
896
897 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
898 {
899         struct find_param *param = (struct find_param *)data;
900         param->depth--;
901         return 0;
902 }
903
904 int llapi_find(char *path, struct find_param *param)
905 {
906         char *buf;
907         int ret, len = strlen(path);
908
909         if (len > PATH_MAX) {
910                 fprintf(stderr, "%s: Path name '%s' is too long.\n",
911                         __FUNCTION__, path);
912                 return -EINVAL;
913         }
914
915         buf = (char *)malloc(PATH_MAX + 1);
916         if (!buf)
917                 return -ENOMEM;
918
919         ret = common_param_init(param);
920         if (ret) {
921                 free(buf);
922                 return ret;
923         }
924
925         param->depth = 0;
926
927         strncpy(buf, path, PATH_MAX + 1);
928         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
929                                       cb_common_fini, param);
930
931         find_param_fini(param);
932         free(buf);
933         return ret < 0 ? ret : 0;
934 }
935
936 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
937 {
938         struct find_param *param = (struct find_param *)data;
939         int ret = 0;
940
941         LASSERT(parent != NULL || d != NULL);
942
943         /* Prepare odb. */
944         if (!param->got_uuids) {
945                 ret = setup_obd_uuids(d ? d : parent, path, param);
946                 if (ret)
947                         return ret;
948         }
949
950         if (d) {
951                 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
952                             (void *)&param->lmd->lmd_lmm);
953         } else if (parent) {
954                 char *fname = strrchr(path, '/');
955                 fname = (fname == NULL ? path : fname + 1);
956
957                 strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
958                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
959                             (void *)&param->lmd->lmd_lmm);
960         }
961
962         if (ret) {
963                 if (errno == ENODATA) {
964                         if (!param->obduuid && !param->quiet)
965                                 printf("%s has no stripe info\n", path);
966                         goto out;
967                 } else if (errno == ENOTTY) {
968                         fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
969                                 __FUNCTION__, path);
970                 } else if (errno == ENOENT) {
971                         err_msg("warning: %s: %s does not exist",
972                                 __FUNCTION__, path);
973                         goto out;
974                 } else {
975                         err_msg("error: %s: %s failed for %s", __FUNCTION__,
976                                 d ? "LL_IOC_LOV_GETSTRIPE" :
977                                 "IOC_MDC_GETFILESTRIPE", path);
978                 }
979
980                 return ret;
981         }
982
983         llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
984 out:
985         /* Do not get down anymore? */
986         if (param->depth == param->maxdepth)
987                 return 1;
988
989         param->depth++;
990         return 0;
991 }
992
993 int llapi_getstripe(char *path, struct find_param *param)
994 {
995         char *buf;
996         int ret = 0, len = strlen(path);
997
998         if (len > PATH_MAX) {
999                 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1000                         __FUNCTION__, path);
1001                 return -EINVAL;
1002         }
1003
1004         buf = (char *)malloc(PATH_MAX + 1);
1005         if (!buf)
1006                 return -ENOMEM;
1007
1008         ret = common_param_init(param);
1009         if (ret) {
1010                 free(buf);
1011                 return ret;
1012         }
1013
1014         param->depth = 0;
1015
1016         strncpy(buf, path, PATH_MAX + 1);
1017         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1018                                       cb_common_fini, param);
1019         find_param_fini(param);
1020         free(buf);
1021         return ret < 0 ? ret : 0;
1022 }
1023
1024 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1025                      struct obd_statfs *stat_buf,
1026                      struct obd_uuid *uuid_buf)
1027 {
1028         int fd;
1029         char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1030         char *rawbuf = raw;
1031         struct obd_ioctl_data data = { 0 };
1032         int rc = 0;
1033
1034         data.ioc_inlbuf1 = (char *)&type;
1035         data.ioc_inllen1 = sizeof(__u32);
1036         data.ioc_inlbuf2 = (char *)&index;
1037         data.ioc_inllen2 = sizeof(__u32);
1038         data.ioc_pbuf1 = (char *)stat_buf;
1039         data.ioc_plen1 = sizeof(struct obd_statfs);
1040         data.ioc_pbuf2 = (char *)uuid_buf;
1041         data.ioc_plen2 = sizeof(struct obd_uuid);
1042
1043         if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1044                 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
1045                 return rc;
1046         }
1047
1048         fd = open(path, O_RDONLY);
1049         if (errno == EISDIR)
1050                 fd = open(path, O_DIRECTORY | O_RDONLY);
1051
1052         if (fd < 0) {
1053                 rc = errno ? -errno : -EBADF;
1054                 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1055                 return rc;
1056         }
1057         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1058         if (rc)
1059                 rc = errno ? -errno : -EINVAL;
1060
1061         close(fd);
1062         return rc;
1063 }
1064
1065 #define MAX_STRING_SIZE 128
1066 #define DEVICES_LIST "/proc/fs/lustre/devices"
1067
1068 int llapi_ping(char *obd_type, char *obd_name)
1069 {
1070         char path[MAX_STRING_SIZE];
1071         char buf[1];
1072         int rc, fd;
1073
1074         snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1075                  obd_type, obd_name);
1076
1077         fd = open(path, O_WRONLY);
1078         if (fd < 0) {
1079                 rc = errno;
1080                 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1081                 return rc;
1082         }
1083
1084         rc = write(fd, buf, 1);
1085         close(fd);
1086
1087         if (rc == 1)
1088                 return 0;
1089         return rc;
1090 }
1091
1092 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1093 {
1094         char buf[MAX_STRING_SIZE];
1095         FILE *fp = fopen(DEVICES_LIST, "r");
1096         int i, rc = 0;
1097
1098         if (fp == NULL) {
1099                 rc = errno;
1100                 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1101                         strerror(errno));
1102                 return rc;
1103         }
1104
1105         while (fgets(buf, sizeof(buf), fp) != NULL) {
1106                 char *obd_type_name = NULL;
1107                 char *obd_name = NULL;
1108                 char *obd_uuid = NULL;
1109                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1110                 char *bufl = rawbuf;
1111                 char *bufp = buf;
1112                 struct obd_ioctl_data datal = { 0, };
1113                 struct obd_statfs osfs_buffer;
1114
1115                 while(bufp[0] == ' ')
1116                         ++bufp;
1117
1118                 for(i = 0; i < 3; i++) {
1119                         obd_type_name = strsep(&bufp, " ");
1120                 }
1121                 obd_name = strsep(&bufp, " ");
1122                 obd_uuid = strsep(&bufp, " ");
1123
1124                 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1125
1126                 memset(bufl, 0, sizeof(rawbuf));
1127                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1128                 datal.ioc_plen1 = sizeof(osfs_buffer);
1129
1130                 for (i = 0; i < type_num; i++) {
1131                         if (strcmp(obd_type_name, obd_type[i]) != 0)
1132                                 continue;
1133
1134                         cb(obd_type_name, obd_name, obd_uuid, args);
1135                 }
1136         }
1137         fclose(fp);
1138         return rc;
1139 }
1140
1141 static void do_target_check(char *obd_type_name, char *obd_name,
1142                             char *obd_uuid, void *args)
1143 {
1144         int rc;
1145
1146         rc = llapi_ping(obd_type_name, obd_name);
1147         if (rc) {
1148                 err_msg("error: check '%s'", obd_name);
1149         } else {
1150                 printf("%s active.\n", obd_name);
1151         }
1152 }
1153
1154 int llapi_target_check(int type_num, char **obd_type, char *dir)
1155 {
1156         return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1157 }
1158
1159 #undef MAX_STRING_SIZE
1160
1161 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1162 {
1163         char raw[OBD_MAX_IOCTL_BUFFER];
1164         char out[LLOG_CHUNK_SIZE];
1165         char *buf = raw;
1166         struct obd_ioctl_data data = { 0 };
1167         char key[30];
1168         DIR *root;
1169         int rc;
1170
1171         sprintf(key, "%s", keyword);
1172         memset(raw, 0, sizeof(raw));
1173         memset(out, 0, sizeof(out));
1174         data.ioc_inlbuf1 = key;
1175         data.ioc_inllen1 = strlen(key) + 1;
1176         if (node_name) {
1177                 data.ioc_inlbuf2 = node_name;
1178                 data.ioc_inllen2 = strlen(node_name) + 1;
1179         }
1180         data.ioc_pbuf1 = out;
1181         data.ioc_plen1 = sizeof(out);
1182         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1183         if (rc)
1184                 return rc;
1185
1186         root = opendir(dir);
1187         if (root == NULL) {
1188                 rc = errno;
1189                 err_msg("open %s failed", dir);
1190                 return rc;
1191         }
1192
1193         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1194         if (rc)
1195                 err_msg("ioctl OBD_IOC_CATINFO failed");
1196         else
1197                 fprintf(stdout, "%s", data.ioc_pbuf1);
1198
1199         closedir(root);
1200         return rc;
1201 }
1202
1203 /* Is this a lustre fs? */
1204 int llapi_is_lustre_mnttype(const char *type)
1205 {
1206         return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1207 }
1208
1209 /* Is this a lustre client fs? */
1210 int llapi_is_lustre_mnt(struct mntent *mnt)
1211 {
1212         return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1213                 strstr(mnt->mnt_fsname, ":/") != NULL);
1214 }
1215
1216 int llapi_quotacheck(char *mnt, int check_type)
1217 {
1218         DIR *root;
1219         int rc;
1220
1221         root = opendir(mnt);
1222         if (!root) {
1223                 err_msg("open %s failed", mnt);
1224                 return -1;
1225         }
1226
1227         rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1228
1229         closedir(root);
1230         return rc;
1231 }
1232
1233 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1234 {
1235         DIR *root;
1236         int poll_intvl = 2;
1237         int rc;
1238
1239         root = opendir(mnt);
1240         if (!root) {
1241                 err_msg("open %s failed", mnt);
1242                 return -1;
1243         }
1244
1245         while (1) {
1246                 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1247                 if (!rc)
1248                         break;
1249                 sleep(poll_intvl);
1250                 if (poll_intvl < 30)
1251                         poll_intvl *= 2;
1252         }
1253
1254         closedir(root);
1255         return rc;
1256 }
1257
1258 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1259 {
1260         DIR *root;
1261         int rc;
1262
1263         root = opendir(mnt);
1264         if (!root) {
1265                 err_msg("open %s failed", mnt);
1266                 return -1;
1267         }
1268
1269         rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1270
1271         closedir(root);
1272         return rc;
1273 }
1274
1275 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1276 {
1277         struct find_param *param = (struct find_param *)data;
1278         lstat_t *st;
1279         int rc;
1280
1281         LASSERT(parent != NULL || d != NULL);
1282
1283         if (d) {
1284                 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1285                            (void *)param->lmd);
1286         } else if (parent) {
1287                 char *fname = strrchr(path, '/');
1288                 fname = (fname == NULL ? path : fname + 1);
1289
1290                 strncpy((char *)param->lmd, fname, param->lumlen);
1291                 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1292                            (void *)param->lmd);
1293         } else {
1294                 return 0;
1295         }
1296
1297         if (rc) {
1298                 if (errno == ENODATA) {
1299                         if (!param->obduuid && !param->quiet)
1300                                 fprintf(stderr, "%s has no stripe info\n",
1301                                         path);
1302                         rc = 0;
1303                 } else if (errno == ENOENT) {
1304                         err_msg("warning: %s: %s does not exist",
1305                                 __FUNCTION__, path);
1306                         rc = 0;
1307                 } else if (errno != EISDIR) {
1308                         rc = errno;
1309                         err_msg("%s ioctl failed for %s.",
1310                                 d ? "LL_IOC_MDC_GETINFO" :
1311                                 "IOC_MDC_GETFILEINFO", path);
1312                 }
1313                 return rc;
1314         }
1315
1316         st = &param->lmd->lmd_st;
1317
1318         /* libc chown() will do extra check, and if the real owner is
1319          * the same as the ones to set, it won't fall into kernel, so
1320          * invoke syscall directly. */
1321         rc = syscall(SYS_chown, path, -1, -1);
1322         if (rc)
1323                 err_msg("error: chown %s (%u,%u)", path);
1324
1325         rc = chmod(path, st->st_mode);
1326         if (rc)
1327                 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1328
1329         return rc;
1330 }
1331
1332 int llapi_quotachown(char *path, int flag)
1333 {
1334         struct find_param param;
1335         char *buf;
1336         int ret = 0, len = strlen(path);
1337
1338         if (len > PATH_MAX) {
1339                 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1340                         __FUNCTION__, path);
1341                 return -EINVAL;
1342         }
1343
1344         buf = (char *)malloc(PATH_MAX + 1);
1345         if (!buf)
1346                 return -ENOMEM;
1347
1348         memset(&param, 0, sizeof(param));
1349         param.recursive = 1;
1350         param.verbose = 0;
1351         param.quiet = 1;
1352
1353         ret = common_param_init(&param);
1354         if (ret)
1355                 goto out;
1356
1357         strncpy(buf, path, PATH_MAX + 1);
1358         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1359                                       NULL, &param);
1360 out:
1361         find_param_fini(&param);
1362         free(buf);
1363         return ret;
1364 }
1365
1366 int llapi_getfacl(char *fname, char *cmd)
1367 {
1368         struct rmtacl_ioctl_data data;
1369         char out[RMTACL_SIZE_MAX] = "";
1370         int fd, rc;
1371
1372         data.cmd = cmd;
1373         data.cmd_len = strlen(cmd) + 1;
1374         data.res = out;
1375         data.res_len = sizeof(out);
1376
1377         fd = open(fname, 0);
1378         if (fd == -1) {
1379                 err_msg("open %s failed", fname);
1380                 return -1;
1381         }
1382
1383         rc = ioctl(fd, LL_IOC_GETFACL, &data);
1384         close(fd);
1385         if (errno == EBADE) {
1386                 fprintf(stderr, "Please use getfacl directly!\n");
1387                 rc = 1;
1388         } else if (rc) {
1389                 err_msg("getfacl %s failed", fname);
1390         } else {
1391                 printf("%s", out);
1392         }
1393
1394         return rc;
1395 }
1396
1397 int llapi_setfacl(char *fname, char *cmd)
1398 {
1399         struct rmtacl_ioctl_data data;
1400         char out[RMTACL_SIZE_MAX] = "";
1401         int fd, rc;
1402
1403         data.cmd = cmd;
1404         data.cmd_len = strlen(cmd) + 1;
1405         data.res = out;
1406         data.res_len = sizeof(out);
1407
1408         fd = open(fname, 0);
1409         if (fd == -1) {
1410                 err_msg("open %s failed", fname);
1411                 return -1;
1412         }
1413
1414         rc = ioctl(fd, LL_IOC_SETFACL, &data);
1415         close(fd);
1416         if (errno == EBADE) {
1417                 fprintf(stderr, "Please use setfacl directly!\n");
1418                 rc = 1;
1419         } else if (errno == EOPNOTSUPP) {
1420                 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1421                 rc = 1;
1422         } else if (rc) {
1423                 err_msg("setfacl %s failed", fname);
1424         } else {
1425                 printf("%s", out);
1426         }
1427
1428         return rc;
1429 }