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 {
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 a regular expression is presented, make the initial decision */
777         if (param->pattern != NULL) {
778                 char *fname = strrchr(path, '/');
779                 fname = (fname == NULL ? path : fname + 1);
780                 ret = fnmatch(param->pattern, fname, 0);
781                 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
782                     (ret == 0 && param->exclude_pattern))
783                         decision = -1;
784         }
785
786         /* If an OBD UUID is specified but no one matches, skip this file. */
787         if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
788                 decision = -1;
789
790         /* If a OST UUID is given, and some OST matches, check it here. */
791         if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
792                 if (!S_ISREG(st->st_mode))
793                         goto decided;
794
795                 /* Only those files should be accepted, which have a
796                  * stripe on the specified OST. */
797                 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
798                         decision = -1;
799                 } else {
800                         int i;
801                         for (i = 0;
802                              i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
803                                if (param->obdindex ==
804                                    param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
805                                         break;
806                         }
807
808                         if (i == param->lmd->lmd_lmm.lmm_stripe_count)
809                                 decision = -1;
810                 }
811         }
812
813         /* Check the time on mds. */
814         if (!decision) {
815                 int for_mds;
816
817                 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
818                                        param->lmd->lmd_lmm.lmm_stripe_count)
819                                     : 0;
820                 decision = find_time_check(st, param, for_mds);
821         }
822
823         /* If file still fits the request, ask osd for updated info.
824            The regulat stat is almost of the same speed as some new
825            'glimpse-size-ioctl'. */
826         if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
827             S_ISREG(st->st_mode)) {
828                 if (param->obdindex != OBD_NOT_FOUND) {
829                         /* Check whether the obd is active or not, if it is
830                          * not active, just print the object affected by this
831                          * failed ost 
832                          * */
833                         struct obd_statfs stat_buf;
834                         struct obd_uuid uuid_buf;
835
836                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
837                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
838                         ret = llapi_obd_statfs(path, LL_STATFS_LOV,
839                                                param->obdindex, &stat_buf, 
840                                                &uuid_buf);
841                         if (ret) {
842                                 if (ret == -ENODATA || ret == -ENODEV 
843                                     || ret == -EIO)
844                                         errno = EIO;
845                                 printf("obd_uuid: %s failed %s ",
846                                         param->obduuid->uuid, strerror(errno));
847                                 goto print_path;
848                         }
849                 }
850                 if (dir) {
851                         ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
852                                     (void *)param->lmd);
853                 } else if (parent) {
854                         ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
855                                     (void *)param->lmd);
856                 }
857
858                 if (ret) {
859                         fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
860                                 "%s.\n", __FUNCTION__, path, strerror(errno));
861                         return -EINVAL;
862                 }
863
864                 /* Check the time on osc. */
865                 if (!decision)
866                         decision = find_time_check(st, param, 0);
867         }
868
869 print_path:
870         if (decision != -1) {
871                 printf("%s", path);
872                 if (param->zeroend)
873                         printf("%c", '\0');
874                 else
875                         printf("\n");
876         }
877
878 decided:
879         /* Do not get down anymore? */
880         if (param->depth == param->maxdepth)
881                 return 1;
882
883         param->depth++;
884         return 0;
885 }
886
887 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
888 {
889         struct find_param *param = (struct find_param *)data;
890         param->depth--;
891         return 0;
892 }
893
894 int llapi_find(char *path, struct find_param *param)
895 {
896         char *buf;
897         int ret, len = strlen(path);
898
899         if (len > PATH_MAX) {
900                 fprintf(stderr, "%s: Path name '%s' is too long.\n",
901                         __FUNCTION__, path);
902                 return -EINVAL;
903         }
904
905         buf = (char *)malloc(PATH_MAX + 1);
906         if (!buf)
907                 return -ENOMEM;
908
909         ret = common_param_init(param);
910         if (ret) {
911                 free(buf);
912                 return ret;
913         }
914
915         param->depth = 0;
916
917         strncpy(buf, path, PATH_MAX + 1);
918         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
919                                       cb_common_fini, param);
920
921         find_param_fini(param);
922         free(buf);
923         return ret < 0 ? ret : 0;
924 }
925
926 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
927 {
928         struct find_param *param = (struct find_param *)data;
929         int ret = 0;
930
931         LASSERT(parent != NULL || d != NULL);
932
933         /* Prepare odb. */
934         if (!param->got_uuids) {
935                 ret = setup_obd_uuids(d ? d : parent, path, param);
936                 if (ret)
937                         return ret;
938         }
939
940         if (d) {
941                 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
942                             (void *)&param->lmd->lmd_lmm);
943         } else if (parent) {
944                 char *fname = strrchr(path, '/');
945                 fname = (fname == NULL ? path : fname + 1);
946
947                 strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
948                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
949                             (void *)&param->lmd->lmd_lmm);
950         }
951
952         if (ret) {
953                 if (errno == ENODATA) {
954                         if (!param->obduuid && !param->quiet)
955                                 printf("%s has no stripe info\n", path);
956                         goto out;
957                 } else if (errno == ENOTTY) {
958                         fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
959                                 __FUNCTION__, path);
960                 } else {
961                         err_msg("error: %s: %s failed for %s", __FUNCTION__,
962                                 d ? "LL_IOC_LOV_GETSTRIPE" :
963                                 "IOC_MDC_GETFILESTRIPE", path);
964                 }
965
966                 return ret;
967         }
968
969         llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
970 out:
971         /* Do not get down anymore? */
972         if (param->depth == param->maxdepth)
973                 return 1;
974
975         param->depth++;
976         return 0;
977 }
978
979 int llapi_getstripe(char *path, struct find_param *param)
980 {
981         char *buf;
982         int ret = 0, len = strlen(path);
983
984         if (len > PATH_MAX) {
985                 fprintf(stderr, "%s: Path name '%s' is too long.\n",
986                         __FUNCTION__, path);
987                 return -EINVAL;
988         }
989
990         buf = (char *)malloc(PATH_MAX + 1);
991         if (!buf)
992                 return -ENOMEM;
993
994         ret = common_param_init(param);
995         if (ret) {
996                 free(buf);
997                 return ret;
998         }
999
1000         param->depth = 0;
1001
1002         strncpy(buf, path, PATH_MAX + 1);
1003         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1004                                       cb_common_fini, param);
1005         find_param_fini(param);
1006         free(buf);
1007         return ret < 0 ? ret : 0;
1008 }
1009
1010 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1011                      struct obd_statfs *stat_buf,
1012                      struct obd_uuid *uuid_buf)
1013 {
1014         int fd;
1015         char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1016         char *rawbuf = raw;
1017         struct obd_ioctl_data data = { 0 };
1018         int rc = 0;
1019
1020         data.ioc_inlbuf1 = (char *)&type;
1021         data.ioc_inllen1 = sizeof(__u32);
1022         data.ioc_inlbuf2 = (char *)&index;
1023         data.ioc_inllen2 = sizeof(__u32);
1024         data.ioc_pbuf1 = (char *)stat_buf;
1025         data.ioc_plen1 = sizeof(struct obd_statfs);
1026         data.ioc_pbuf2 = (char *)uuid_buf;
1027         data.ioc_plen2 = sizeof(struct obd_uuid);
1028
1029         if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
1030                 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
1031                 return -EINVAL;
1032         }
1033
1034         fd = open(path, O_RDONLY);
1035         if (errno == EISDIR)
1036                 fd = open(path, O_DIRECTORY | O_RDONLY);
1037
1038         if (fd < 0) {
1039                 rc = errno ? -errno : -EBADF;
1040                 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1041                 return rc;
1042         }
1043         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1044         if (rc)
1045                 rc = errno ? -errno : -EINVAL;
1046
1047         close(fd);
1048         return rc;
1049 }
1050
1051 #define MAX_STRING_SIZE 128
1052 #define DEVICES_LIST "/proc/fs/lustre/devices"
1053
1054 int llapi_ping(char *obd_type, char *obd_name)
1055 {
1056         char path[MAX_STRING_SIZE];
1057         char buf[1];
1058         int rc, fd;
1059
1060         snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1061                  obd_type, obd_name);
1062
1063         fd = open(path, O_WRONLY);
1064         if (fd < 0) {
1065                 rc = errno;
1066                 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1067                 return rc;
1068         }
1069
1070         rc = write(fd, buf, 1);
1071         close(fd);
1072
1073         if (rc == 1)
1074                 return 0;
1075         return rc;
1076 }
1077
1078 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1079 {
1080         char buf[MAX_STRING_SIZE];
1081         FILE *fp = fopen(DEVICES_LIST, "r");
1082         int i, rc = 0;
1083
1084         if (fp == NULL) {
1085                 rc = errno;
1086                 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1087                         strerror(errno));
1088                 return rc;
1089         }
1090
1091         while (fgets(buf, sizeof(buf), fp) != NULL) {
1092                 char *obd_type_name = NULL;
1093                 char *obd_name = NULL;
1094                 char *obd_uuid = NULL;
1095                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1096                 char *bufl = rawbuf;
1097                 char *bufp = buf;
1098                 struct obd_ioctl_data datal = { 0, };
1099                 struct obd_statfs osfs_buffer;
1100
1101                 while(bufp[0] == ' ')
1102                         ++bufp;
1103
1104                 for(i = 0; i < 3; i++) {
1105                         obd_type_name = strsep(&bufp, " ");
1106                 }
1107                 obd_name = strsep(&bufp, " ");
1108                 obd_uuid = strsep(&bufp, " ");
1109
1110                 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1111
1112                 memset(bufl, 0, sizeof(rawbuf));
1113                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1114                 datal.ioc_plen1 = sizeof(osfs_buffer);
1115
1116                 for (i = 0; i < type_num; i++) {
1117                         if (strcmp(obd_type_name, obd_type[i]) != 0)
1118                                 continue;
1119
1120                         cb(obd_type_name, obd_name, obd_uuid, args);
1121                 }
1122         }
1123         fclose(fp);
1124         return rc;
1125 }
1126
1127 static void do_target_check(char *obd_type_name, char *obd_name,
1128                             char *obd_uuid, void *args)
1129 {
1130         int rc;
1131
1132         rc = llapi_ping(obd_type_name, obd_name);
1133         if (rc) {
1134                 err_msg("error: check '%s'", obd_name);
1135         } else {
1136                 printf("%s active.\n", obd_name);
1137         }
1138 }
1139
1140 int llapi_target_check(int type_num, char **obd_type, char *dir)
1141 {
1142         return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1143 }
1144
1145 #undef MAX_STRING_SIZE
1146
1147 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1148 {
1149         char raw[OBD_MAX_IOCTL_BUFFER];
1150         char out[LLOG_CHUNK_SIZE];
1151         char *buf = raw;
1152         struct obd_ioctl_data data = { 0 };
1153         char key[30];
1154         DIR *root;
1155         int rc;
1156
1157         sprintf(key, "%s", keyword);
1158         memset(raw, 0, sizeof(raw));
1159         memset(out, 0, sizeof(out));
1160         data.ioc_inlbuf1 = key;
1161         data.ioc_inllen1 = strlen(key) + 1;
1162         if (node_name) {
1163                 data.ioc_inlbuf2 = node_name;
1164                 data.ioc_inllen2 = strlen(node_name) + 1;
1165         }
1166         data.ioc_pbuf1 = out;
1167         data.ioc_plen1 = sizeof(out);
1168         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1169         if (rc)
1170                 return rc;
1171
1172         root = opendir(dir);
1173         if (root == NULL) {
1174                 rc = errno;
1175                 err_msg("open %s failed", dir);
1176                 return rc;
1177         }
1178
1179         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1180         if (rc)
1181                 err_msg("ioctl OBD_IOC_CATINFO failed");
1182         else
1183                 fprintf(stdout, "%s", data.ioc_pbuf1);
1184
1185         closedir(root);
1186         return rc;
1187 }
1188
1189 /* Is this a lustre fs? */
1190 int llapi_is_lustre_mnttype(const char *type)
1191 {
1192         return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1193 }
1194
1195 /* Is this a lustre client fs? */
1196 int llapi_is_lustre_mnt(struct mntent *mnt)
1197 {
1198         return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1199                 strstr(mnt->mnt_fsname, ":/") != NULL);
1200 }
1201
1202 int llapi_quotacheck(char *mnt, int check_type)
1203 {
1204         DIR *root;
1205         int rc;
1206
1207         root = opendir(mnt);
1208         if (!root) {
1209                 err_msg("open %s failed", mnt);
1210                 return -1;
1211         }
1212
1213         rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1214
1215         closedir(root);
1216         return rc;
1217 }
1218
1219 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1220 {
1221         DIR *root;
1222         int poll_intvl = 2;
1223         int rc;
1224
1225         root = opendir(mnt);
1226         if (!root) {
1227                 err_msg("open %s failed", mnt);
1228                 return -1;
1229         }
1230
1231         while (1) {
1232                 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1233                 if (!rc)
1234                         break;
1235                 sleep(poll_intvl);
1236                 if (poll_intvl < 30)
1237                         poll_intvl *= 2;
1238         }
1239
1240         closedir(root);
1241         return rc;
1242 }
1243
1244 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1245 {
1246         DIR *root;
1247         int rc;
1248
1249         root = opendir(mnt);
1250         if (!root) {
1251                 err_msg("open %s failed", mnt);
1252                 return -1;
1253         }
1254
1255         rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1256
1257         closedir(root);
1258         return rc;
1259 }
1260
1261 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1262 {
1263         struct find_param *param = (struct find_param *)data;
1264         lstat_t *st;
1265         int rc;
1266
1267         LASSERT(parent != NULL || d != NULL);
1268
1269         if (d) {
1270                 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1271                            (void *)param->lmd);
1272         } else if (parent) {
1273                 char *fname = strrchr(path, '/');
1274                 fname = (fname == NULL ? path : fname + 1);
1275
1276                 strncpy((char *)param->lmd, fname, param->lumlen);
1277                 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1278                            (void *)param->lmd);
1279         } else {
1280                 return 0;
1281         }
1282
1283         if (rc) {
1284                 if (errno == ENODATA) {
1285                         if (!param->obduuid && !param->quiet)
1286                                 fprintf(stderr, "%s has no stripe info\n",
1287                                         path);
1288                         rc = 0;
1289                 } else if (errno != EISDIR) {
1290                         rc = errno;
1291                         err_msg("%s ioctl failed for %s.",
1292                                 d ? "LL_IOC_MDC_GETINFO" :
1293                                 "IOC_MDC_GETFILEINFO", path);
1294                 }
1295                 return rc;
1296         }
1297
1298         st = &param->lmd->lmd_st;
1299
1300         /* libc chown() will do extra check, and if the real owner is
1301          * the same as the ones to set, it won't fall into kernel, so
1302          * invoke syscall directly. */
1303         rc = syscall(SYS_chown, path, -1, -1);
1304         if (rc)
1305                 err_msg("error: chown %s (%u,%u)", path);
1306
1307         rc = chmod(path, st->st_mode);
1308         if (rc)
1309                 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1310
1311         return rc;
1312 }
1313
1314 int llapi_quotachown(char *path, int flag)
1315 {
1316         struct find_param param;
1317         char *buf;
1318         int ret = 0, len = strlen(path);
1319
1320         if (len > PATH_MAX) {
1321                 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1322                         __FUNCTION__, path);
1323                 return -EINVAL;
1324         }
1325
1326         buf = (char *)malloc(PATH_MAX + 1);
1327         if (!buf)
1328                 return -ENOMEM;
1329
1330         memset(&param, 0, sizeof(param));
1331         param.recursive = 1;
1332         param.verbose = 0;
1333         param.quiet = 1;
1334
1335         ret = common_param_init(&param);
1336         if (ret)
1337                 goto out;
1338
1339         strncpy(buf, path, PATH_MAX + 1);
1340         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1341                                       NULL, &param);
1342 out:
1343         find_param_fini(&param);
1344         free(buf);
1345         return ret;
1346 }
1347
1348 int llapi_getfacl(char *fname, char *cmd)
1349 {
1350         struct rmtacl_ioctl_data data;
1351         char out[RMTACL_SIZE_MAX] = "";
1352         int fd, rc;
1353
1354         data.cmd = cmd;
1355         data.cmd_len = strlen(cmd) + 1;
1356         data.res = out;
1357         data.res_len = sizeof(out);
1358
1359         fd = open(fname, 0);
1360         if (fd == -1) {
1361                 err_msg("open %s failed", fname);
1362                 return -1;
1363         }
1364
1365         rc = ioctl(fd, LL_IOC_GETFACL, &data);
1366         close(fd);
1367         if (errno == EBADE) {
1368                 fprintf(stderr, "Please use getfacl directly!\n");
1369                 rc = 1;
1370         } else if (rc) {
1371                 err_msg("getfacl %s failed", fname);
1372         } else {
1373                 printf("%s", out);
1374         }
1375
1376         return rc;
1377 }
1378
1379 int llapi_setfacl(char *fname, char *cmd)
1380 {
1381         struct rmtacl_ioctl_data data;
1382         char out[RMTACL_SIZE_MAX] = "";
1383         int fd, rc;
1384
1385         data.cmd = cmd;
1386         data.cmd_len = strlen(cmd) + 1;
1387         data.res = out;
1388         data.res_len = sizeof(out);
1389
1390         fd = open(fname, 0);
1391         if (fd == -1) {
1392                 err_msg("open %s failed", fname);
1393                 return -1;
1394         }
1395
1396         rc = ioctl(fd, LL_IOC_SETFACL, &data);
1397         close(fd);
1398         if (errno == EBADE) {
1399                 fprintf(stderr, "Please use setfacl directly!\n");
1400                 rc = 1;
1401         } else if (errno == EOPNOTSUPP) {
1402                 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1403                 rc = 1;
1404         } else if (rc) {
1405                 err_msg("setfacl %s failed", fname);
1406         } else {
1407                 printf("%s", out);
1408         }
1409
1410         return rc;
1411 }