Whamcloud - gitweb
b=16098
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/liblustreapi.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Robert Read <rread@clusterfs.com>
41  */
42
43 /* for O_DIRECTORY */
44 #ifndef _GNU_SOURCE
45 #define _GNU_SOURCE
46 #endif
47
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <stddef.h>
52 #include <sys/ioctl.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <errno.h>
56 #include <dirent.h>
57 #include <stdarg.h>
58 #include <sys/stat.h>
59 #include <sys/types.h>
60 #include <sys/syscall.h>
61 #include <fnmatch.h>
62 #ifdef HAVE_ASM_TYPES_H
63 #include <asm/types.h>
64 #endif
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
67 #else
68 #include <unistd.h>
69 #endif
70
71 #include <liblustre.h>
72 #include <lnet/lnetctl.h>
73 #include <obd.h>
74 #include <lustre_lib.h>
75 #include <obd_lov.h>
76 #include <lustre/liblustreapi.h>
77
78 static unsigned llapi_dir_filetype_table[] = {
79         [DT_UNKNOWN]= 0,
80         [DT_FIFO]= S_IFIFO,
81         [DT_CHR] = S_IFCHR,
82         [DT_DIR] = S_IFDIR,
83         [DT_BLK] = S_IFBLK,
84         [DT_REG] = S_IFREG,
85         [DT_LNK] = S_IFLNK,
86         [DT_SOCK]= S_IFSOCK,
87 #if defined(DT_DOOR) && defined(S_IFDOOR)
88         [DT_DOOR]= S_IFDOOR,
89 #endif
90 };
91
92 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 static const int DT_MAX = DT_DOOR;
94 #else
95 static const int DT_MAX = DT_SOCK;
96 #endif
97
98 static unsigned llapi_filetype_dir_table[] = {
99         [0]= DT_UNKNOWN,
100         [S_IFIFO]= DT_FIFO,
101         [S_IFCHR] = DT_CHR,
102         [S_IFDIR] = DT_DIR,
103         [S_IFBLK] = DT_BLK,
104         [S_IFREG] = DT_REG,
105         [S_IFLNK] = DT_LNK,
106         [S_IFSOCK]= DT_SOCK,
107 #if defined(DT_DOOR) && defined(S_IFDOOR)
108         [S_IFDOOR]= DT_DOOR,
109 #endif
110 };
111
112 #if defined(DT_DOOR) && defined(S_IFDOOR)
113 static const int S_IFMAX = DT_DOOR;
114 #else
115 static const int S_IFMAX = DT_SOCK;
116 #endif
117
118 /* liblustreapi message level */
119 static int llapi_msg_level = LLAPI_MSG_MAX;
120
121 void llapi_msg_set_level(int level)
122 {
123         /* ensure level is in the good range */
124         if (level < LLAPI_MSG_OFF)
125                 llapi_msg_level = LLAPI_MSG_OFF;
126         else if (level > LLAPI_MSG_MAX)
127                 llapi_msg_level = LLAPI_MSG_MAX;
128         else
129                 llapi_msg_level = level;
130 }
131
132 void llapi_err(int level, char *fmt, ...)
133 {
134         va_list args;
135         int tmp_errno = abs(errno);
136
137         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
138                 return;
139
140         va_start(args, fmt);
141         vfprintf(stderr, fmt, args);
142         va_end(args);
143
144         if (level & LLAPI_MSG_NO_ERRNO)
145                 fprintf(stderr, "\n");
146         else
147                 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
148 }
149
150 #define llapi_err_noerrno(level, fmt, a...)                             \
151         llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
152
153 void llapi_printf(int level, char *fmt, ...)
154 {
155         va_list args;
156
157         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
158                 return;
159
160         va_start(args, fmt);
161         vfprintf(stdout, fmt, args);
162         va_end(args);
163 }
164
165 int parse_size(char *optarg, unsigned long long *size,
166                unsigned long long *size_units)
167 {
168         char *end;
169
170         *size = strtoul(optarg, &end, 0);
171
172         if (*end != '\0') {
173                 if ((*end == 'b') && *(end+1) == '\0' &&
174                     (*size & (~0ULL << (64 - 9))) == 0) {
175                         *size <<= 9;
176                         *size_units = 1 << 9;
177                 } else if ((*end == 'k' || *end == 'K') &&
178                            *(end+1) == '\0' && (*size &
179                            (~0ULL << (64 - 10))) == 0) {
180                         *size <<= 10;
181                         *size_units = 1 << 10;
182                 } else if ((*end == 'm' || *end == 'M') &&
183                            *(end+1) == '\0' && (*size &
184                            (~0ULL << (64 - 20))) == 0) {
185                         *size <<= 20;
186                         *size_units = 1 << 20;
187                 } else if ((*end == 'g' || *end == 'G') &&
188                            *(end+1) == '\0' && (*size &
189                            (~0ULL << (64 - 30))) == 0) {
190                         *size <<= 30;
191                         *size_units = 1 << 30;
192                 } else if ((*end == 't' || *end == 'T') &&
193                            *(end+1) == '\0' && (*size &
194                            (~0ULL << (64 - 40))) == 0) {
195                         *size <<= 40;
196                         *size_units = 1ULL << 40;
197                 } else if ((*end == 'p' || *end == 'P') &&
198                            *(end+1) == '\0' && (*size &
199                            (~0ULL << (64 - 50))) == 0) {
200                         *size <<= 50;
201                         *size_units = 1ULL << 50;
202                 } else if ((*end == 'e' || *end == 'E') &&
203                            *(end+1) == '\0' && (*size &
204                            (~0ULL << (64 - 60))) == 0) {
205                         *size <<= 60;
206                         *size_units = 1ULL << 60;
207                 } else {
208                         return -1;
209                 }
210         }
211
212         return 0;
213 }
214
215 int llapi_file_open(const char *name, int flags, int mode,
216                     unsigned long stripe_size, int stripe_offset,
217                     int stripe_count, int stripe_pattern)
218 {
219         struct lov_user_md lum = { 0 };
220         int fd, rc = 0;
221         int isdir = 0;
222         int page_size;
223
224         fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
225         if (fd < 0 && errno == EISDIR) {
226                 fd = open(name, O_DIRECTORY | O_RDONLY);
227                 isdir++;
228         }
229
230         if (fd < 0) {
231                 rc = -errno;
232                 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
233                 return rc;
234         }
235
236         /* 64 KB is the largest common page size I'm aware of (on ia64), but
237          * check the local page size just in case. */
238         page_size = LOV_MIN_STRIPE_SIZE;
239         if (getpagesize() > page_size) {
240                 page_size = getpagesize();
241                 llapi_err_noerrno(LLAPI_MSG_WARN, 
242                                   "warning: your page size (%u) is "
243                                   "larger than expected (%u)", page_size, 
244                                   LOV_MIN_STRIPE_SIZE);
245         }
246         if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
247                 errno = rc = -EINVAL;
248                 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
249                           "must be an even multiple of %d bytes", 
250                           stripe_size, page_size);
251                 goto out;
252         }
253         if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
254                 errno = rc = -EINVAL;
255                 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d", 
256                           stripe_offset);
257                 goto out;
258         }
259         if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
260                 errno = rc = -EINVAL;
261                 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d", 
262                           stripe_count);
263                 goto out;
264         }
265         if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
266                 errno = rc = -EINVAL;
267                 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
268                           "stripe_count %u exceeds 4GB", stripe_size, 
269                           stripe_count);
270                 goto out;
271         }
272
273         /*  Initialize IOCTL striping pattern structure */
274         lum.lmm_magic = LOV_USER_MAGIC;
275         lum.lmm_pattern = stripe_pattern;
276         lum.lmm_stripe_size = stripe_size;
277         lum.lmm_stripe_count = stripe_count;
278         lum.lmm_stripe_offset = stripe_offset;
279
280         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
281                 char *errmsg = "stripe already set";
282                 rc = -errno;
283                 if (errno != EEXIST && errno != EALREADY)
284                         errmsg = strerror(errno);
285
286                 llapi_err_noerrno(LLAPI_MSG_ERROR,
287                                   "error on ioctl "LPX64" for '%s' (%d): %s",
288                                   (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
289         }
290 out:
291         if (rc) {
292                 close(fd);
293                 fd = rc;
294         }
295
296         return fd;
297 }
298
299 int llapi_file_create(const char *name, unsigned long stripe_size,
300                       int stripe_offset, int stripe_count, int stripe_pattern)
301 {
302         int fd;
303
304         fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
305                              stripe_offset, stripe_count, stripe_pattern);
306         if (fd < 0)
307                 return fd;
308
309         close(fd);
310         return 0;
311 }
312
313 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
314                               void *data, cfs_dirent_t *de);
315
316 #define MAX_LOV_UUID_COUNT      max(LOV_MAX_STRIPE_COUNT, 1000)
317 #define OBD_NOT_FOUND           (-1)
318
319 static int common_param_init(struct find_param *param)
320 {
321         param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
322         if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
323                 llapi_err(LLAPI_MSG_ERROR, 
324                           "error: allocation of %d bytes for ioctl",
325                           sizeof(lstat_t) + param->lumlen);
326                 return -ENOMEM;
327         }
328
329         param->got_uuids = 0;
330         param->obdindexes = NULL;
331         param->obdindex = OBD_NOT_FOUND;
332         return 0;
333 }
334
335 static void find_param_fini(struct find_param *param)
336 {
337         if (param->obdindexes)
338                 free(param->obdindexes);
339
340         if (param->lmd)
341                 free(param->lmd);
342 }
343
344 int llapi_file_get_lov_fuuid(int fd, struct obd_uuid *lov_name)
345 {
346         int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
347         if (rc) {
348                 rc = errno;
349                 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
350         }
351         return rc;
352 }
353
354 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
355 {
356         int fd, rc;
357
358         fd = open(path, O_RDONLY);
359         if (fd < 0) {
360                 rc = errno;
361                 llapi_err(LLAPI_MSG_ERROR, "error opening %s\n", path);
362                 return rc;
363         }
364
365         rc = llapi_file_get_lov_fuuid(fd, lov_uuid);
366
367         close(fd);
368
369         return rc;
370 }
371
372 /*
373  * If uuidp is NULL, return the number of available obd uuids.
374  * If uuidp is non-NULL, then it will return the uuids of the obds. If
375  * there are more OSTs then allocated to uuidp, then an error is returned with
376  * the ost_count set to number of available obd uuids.
377  */
378 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
379 {
380         struct obd_uuid lov_name;
381         char buf[1024];
382         FILE *fp;
383         int rc = 0, index = 0;
384
385         /* Get the lov name */
386         rc = llapi_file_get_lov_fuuid(fd, &lov_name);
387         if (rc)
388                 return rc;
389
390         /* Now get the ost uuids from /proc */
391         snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
392                  lov_name.uuid);
393         fp = fopen(buf, "r");
394         if (fp == NULL) {
395                 rc = errno;
396                 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
397                 return rc;
398         }
399
400         while (fgets(buf, sizeof(buf), fp) != NULL) {
401                 if (uuidp && (index < *ost_count)) {
402                         if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
403                                 break;
404                 }
405                 index++;
406         }
407
408         fclose(fp);
409
410         if (uuidp && (index >= *ost_count))
411                 return -EOVERFLOW;
412
413         *ost_count = index;
414         return rc;
415 }
416
417 /* Here, param->obduuid points to a single obduuid, the index of which is
418  * returned in param->obdindex */
419 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
420 {
421         struct obd_uuid lov_uuid;
422         char uuid[sizeof(struct obd_uuid)];
423         char buf[1024];
424         FILE *fp;
425         int rc = 0, index;
426
427         /* Get the lov name */
428         rc = llapi_file_get_lov_fuuid(dirfd(dir), &lov_uuid);
429         if (rc) {
430                 if (errno != ENOTTY) {
431                         rc = errno;
432                         llapi_err(LLAPI_MSG_ERROR, 
433                                   "error: can't get lov name: %s", dname);
434                 } else {
435                         rc = 0;
436                 }
437                 return rc;
438         }
439
440         param->got_uuids = 1;
441
442         /* Now get the ost uuids from /proc */
443         snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
444                  lov_uuid.uuid);
445         fp = fopen(buf, "r");
446         if (fp == NULL) {
447                 rc = errno;
448                 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
449                 return rc;
450         }
451
452         if (!param->obduuid && !param->quiet && !param->obds_printed)
453                 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
454
455         while (fgets(buf, sizeof(buf), fp) != NULL) {
456                 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
457                         break;
458
459                 if (param->obduuid) {
460                         if (strncmp(param->obduuid->uuid, uuid,
461                                     sizeof(uuid)) == 0) {
462                                 param->obdindex = index;
463                                 break;
464                         }
465                 } else if (!param->quiet && !param->obds_printed) {
466                         /* Print everything */
467                         llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
468                 }
469         }
470         param->obds_printed = 1;
471
472         fclose(fp);
473
474         if (!param->quiet && param->obduuid &&
475             (param->obdindex == OBD_NOT_FOUND)) {
476                 llapi_err_noerrno(LLAPI_MSG_ERROR, 
477                                   "error: %s: unknown obduuid: %s",
478                                   __FUNCTION__, param->obduuid->uuid);
479                 //rc = EINVAL;
480         }
481
482         return (rc);
483 }
484
485 /* In this case, param->obduuid will be an array of obduuids and
486  * obd index for all these obduuids will be returned in
487  * param->obdindexes */
488 static int setup_obd_indexes(DIR *dir, struct find_param *param)
489 {
490         struct obd_uuid *uuids = NULL;
491         int obdcount = INIT_ALLOC_NUM_OSTS;
492         int ret, obd_valid = 0, obdnum, i;
493
494         uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
495                                           sizeof(struct obd_uuid));
496         if (uuids == NULL)
497                 return -ENOMEM;
498
499 retry_get_uuids:
500         ret = llapi_lov_get_uuids(dirfd(dir), uuids,
501                                   &obdcount);
502         if (ret) {
503                 struct obd_uuid *uuids_temp;
504
505                 if (ret == -EOVERFLOW) {
506                         uuids_temp = realloc(uuids, obdcount *
507                                              sizeof(struct obd_uuid));
508                         if (uuids_temp != NULL)
509                                 goto retry_get_uuids;
510                         else
511                                 ret = -ENOMEM;
512                 }
513
514                 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
515                 return ret;
516         }
517
518         param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
519         if (param->obdindexes == NULL)
520                 return -ENOMEM;
521
522         for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
523                 for (i = 0; i <= obdcount; i++) {
524                         if (strcmp((char *)&param->obduuid[obdnum].uuid,
525                                    (char *)&uuids[i]) == 0) {
526                                 param->obdindexes[obdnum] = i;
527                                 obd_valid++;
528                                 break;
529                         }
530                 }
531                 if (i == obdcount)
532                         param->obdindexes[obdnum] = OBD_NOT_FOUND;
533         }
534
535         if (obd_valid == 0)
536                 param->obdindex = OBD_NOT_FOUND;
537         else
538                 param->obdindex = obd_valid;
539
540         param->got_uuids = 1;
541
542         return 0;
543 }
544
545 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
546                           int obdindex, int quiet, int header, int body)
547 {
548         int i, obdstripe = 0;
549
550         if (obdindex != OBD_NOT_FOUND) {
551                 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
552                         if (obdindex == lum->lmm_objects[i].l_ost_idx) {
553                                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
554                                 obdstripe = 1;
555                                 break;
556                         }
557                 }
558         } else if (!quiet) {
559                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
560                 obdstripe = 1;
561         }
562
563         /* if it's a directory */
564         if (is_dir) {
565                 if (obdstripe == 1) {
566                         if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
567                                 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
568                                 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
569                         }
570                         llapi_printf(LLAPI_MSG_NORMAL, 
571                                      "stripe_count: %d stripe_size: %u "
572                                      "stripe_offset: %d\n",
573                                      lum->lmm_stripe_count == (__u16)-1 ? -1 :
574                                      lum->lmm_stripe_count,
575                                      lum->lmm_stripe_size,
576                                      lum->lmm_stripe_offset == (__u16)-1 ? -1 :
577                                      lum->lmm_stripe_offset);
578                 }
579                 return;
580         }
581
582         if (header && (obdstripe == 1)) {
583                 llapi_printf(LLAPI_MSG_NORMAL, 
584                              "lmm_magic:          0x%08X\n",  lum->lmm_magic);
585                 llapi_printf(LLAPI_MSG_NORMAL, 
586                              "lmm_object_gr:      "LPX64"\n", lum->lmm_object_gr);
587                 llapi_printf(LLAPI_MSG_NORMAL, 
588                              "lmm_object_id:      "LPX64"\n", lum->lmm_object_id);
589                 llapi_printf(LLAPI_MSG_NORMAL, 
590                              "lmm_stripe_count:   %u\n", (int)lum->lmm_stripe_count);
591                 llapi_printf(LLAPI_MSG_NORMAL, 
592                              "lmm_stripe_size:    %u\n",      lum->lmm_stripe_size);
593                 llapi_printf(LLAPI_MSG_NORMAL, 
594                              "lmm_stripe_pattern: %x\n",      lum->lmm_pattern);
595         }
596
597         if (body) {
598                 if ((!quiet) && (obdstripe == 1))
599                         llapi_printf(LLAPI_MSG_NORMAL, 
600                                      "\tobdidx\t\t objid\t\tobjid\t\t group\n");
601
602                 for (i = 0; i < lum->lmm_stripe_count; i++) {
603                         int idx = lum->lmm_objects[i].l_ost_idx;
604                         long long oid = lum->lmm_objects[i].l_object_id;
605                         long long gr = lum->lmm_objects[i].l_object_gr;
606                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
607                                 llapi_printf(LLAPI_MSG_NORMAL, 
608                                              "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
609                                              idx, oid, oid, gr,
610                                              obdindex == idx ? " *" : "");
611                 }
612                 llapi_printf(LLAPI_MSG_NORMAL, "\n");
613         }
614 }
615
616 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
617                             int is_dir, int obdindex, int quiet,
618                             int header, int body)
619 {
620         struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
621         int i, obdstripe = 0;
622
623         if (obdindex != OBD_NOT_FOUND) {
624                 for (i = 0; i < lumj->lmm_stripe_count; i++) {
625                         if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
626                                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
627                                 obdstripe = 1;
628                                 break;
629                         }
630                 }
631         } else if (!quiet) {
632                 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
633                 obdstripe = 1;
634         }
635
636         if (header && obdstripe == 1) {
637                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic:          0x%08X\n",  
638                              lumj->lmm_magic);
639                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr:      "LPX64"\n", 
640                              lumj->lmm_object_gr);
641                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id:      "LPX64"\n", 
642                              lumj->lmm_object_id);
643                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count:   %u\n", 
644                              (int)lumj->lmm_stripe_count);
645                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size:    %u\n",
646                              lumj->lmm_stripe_size);
647                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
648                              lumj->lmm_pattern);
649                 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count:   %x\n",
650                              lumj->lmm_extent_count);
651         }
652
653         if (body) {
654                 unsigned long long start = -1, end = 0;
655                 if (!quiet && obdstripe == 1)
656                         llapi_printf(LLAPI_MSG_NORMAL, 
657                                      "joined\tobdidx\t\t objid\t\tobjid\t\t group"
658                                      "\t\tstart\t\tend\n");
659                 for (i = 0; i < lumj->lmm_stripe_count; i++) {
660                         int idx = lumj->lmm_objects[i].l_ost_idx;
661                         long long oid = lumj->lmm_objects[i].l_object_id;
662                         long long gr = lumj->lmm_objects[i].l_object_gr;
663                         if (obdindex == OBD_NOT_FOUND || obdindex == idx)
664                                 llapi_printf(LLAPI_MSG_NORMAL, 
665                                              "\t%6u\t%14llu\t%#13llx\t%14llu%s",
666                                              idx, oid, oid, gr,
667                                              obdindex == idx ? " *" : "");
668                         if (start != lumj->lmm_objects[i].l_extent_start ||
669                             end != lumj->lmm_objects[i].l_extent_end) {
670                                 start = lumj->lmm_objects[i].l_extent_start;
671                                 llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start);
672                                 end = lumj->lmm_objects[i].l_extent_end;
673                                 if (end == (unsigned long long)-1)
674                                         llapi_printf(LLAPI_MSG_NORMAL, 
675                                                      "\t\tEOF\n");
676                                 else
677                                         llapi_printf(LLAPI_MSG_NORMAL, 
678                                                      "\t\t%llu\n", end);
679                         } else {
680                                 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
681                         }
682                 }
683                 llapi_printf(LLAPI_MSG_NORMAL, "\n");
684         }
685 }
686
687 void llapi_lov_dump_user_lmm(struct find_param *param,
688                              char *path, int is_dir)
689 {
690         switch(*(__u32 *)&param->lmd->lmd_lmm) { /* lum->lmm_magic */
691         case LOV_USER_MAGIC_V1:
692                 lov_dump_user_lmm_v1(&param->lmd->lmd_lmm, path, is_dir,
693                                       param->obdindex, param->quiet,
694                                       param->verbose,
695                                       (param->verbose || !param->obduuid));
696                 break;
697         case LOV_USER_MAGIC_JOIN:
698                 lov_dump_user_lmm_join(&param->lmd->lmd_lmm, path, is_dir,
699                                        param->obdindex, param->quiet,
700                                        param->verbose,
701                                        (param->verbose || !param->obduuid));
702                 break;
703         default:
704                 llapi_printf(LLAPI_MSG_NORMAL, 
705                              "unknown lmm_magic:  %#x (expecting %#x)\n",
706                        *(__u32 *)&param->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
707                 return;
708         }
709 }
710
711 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
712 {
713         const char *fname;
714         char *dname;
715         int fd, rc = 0;
716
717         fname = strrchr(path, '/');
718
719         /* It should be a file (or other non-directory) */
720         if (fname == NULL) {
721                 dname = (char *)malloc(2);
722                 if (dname == NULL)
723                         return ENOMEM;
724                 strcpy(dname, ".");
725                 fname = (char *)path;
726         } else {
727                 dname = (char *)malloc(fname - path + 1);
728                 if (dname == NULL)
729                         return ENOMEM;
730                 strncpy(dname, path, fname - path);
731                 dname[fname - path] = '\0';
732                 fname++;
733         }
734
735         if ((fd = open(dname, O_RDONLY)) == -1) {
736                 rc = errno;
737                 free(dname);
738                 return rc;
739         }
740
741         strcpy((char *)lum, fname);
742         if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
743                 rc = errno;
744
745         if (close(fd) == -1 && rc == 0)
746                 rc = errno;
747
748         free(dname);
749
750         return rc;
751 }
752
753 int llapi_file_lookup(int dirfd, const char *name)
754 {
755         struct obd_ioctl_data data = { 0 };
756         char rawbuf[8192];
757         char *buf = rawbuf;
758         int rc;
759
760         if (dirfd < 0 || name == NULL)
761                 return -EINVAL;
762
763         data.ioc_version = OBD_IOCTL_VERSION;
764         data.ioc_len = sizeof(data);
765         data.ioc_inlbuf1 = (char *)name;
766         data.ioc_inllen1 = strlen(name) + 1;
767
768         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
769         if (rc) {
770                 llapi_err(LLAPI_MSG_ERROR,
771                           "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
772                           name, rc);
773                 return rc;
774         }
775
776         return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
777 }
778
779 int llapi_mds_getfileinfo(char *path, DIR *parent,
780                           struct lov_user_mds_data *lmd)
781 {
782         lstat_t *st = &lmd->lmd_st;
783         char *fname = strrchr(path, '/');
784         int ret = 0;
785
786         if (parent == NULL)
787                 return -EINVAL;
788
789         fname = (fname == NULL ? path : fname + 1);
790         /* retrieve needed file info */
791         strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT));
792         ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
793
794         if (ret) {
795                 if (errno == ENOTTY) {
796                         /* ioctl is not supported, it is not a lustre fs.
797                          * Do the regular lstat(2) instead. */
798                         ret = lstat_f(path, st);
799                         if (ret) {
800                                 llapi_err(LLAPI_MSG_ERROR, 
801                                           "error: %s: lstat failed for %s",
802                                           __FUNCTION__, path);
803                                 return ret;
804                         }
805                 } else if (errno == ENOENT) {
806                         llapi_err(LLAPI_MSG_WARN, 
807                                   "warning: %s: %s does not exist", 
808                                   __FUNCTION__, path);
809                         return -ENOENT;
810                 } else {
811                         llapi_err(LLAPI_MSG_ERROR, 
812                                   "error: %s: IOC_MDC_GETFILEINFO failed for %s",
813                                   __FUNCTION__, path);
814                         return ret;
815                 }
816         }
817
818         return 0;
819 }
820
821 static DIR *opendir_parent(char *path)
822 {
823         DIR *parent;
824         char *fname;
825         char c;
826
827         fname = strrchr(path, '/');
828         if (fname == NULL)
829                 return opendir(".");
830
831         c = fname[1];
832         fname[1] = '\0';
833         parent = opendir(path);
834         fname[1] = c;
835         return parent;
836 }
837
838 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
839                                    semantic_func_t sem_init,
840                                    semantic_func_t sem_fini, void *data,
841                                    cfs_dirent_t *de)
842 {
843         cfs_dirent_t *dent;
844         int len, ret;
845         DIR *d, *p = NULL;
846
847         ret = 0;
848         len = strlen(path);
849
850         d = opendir(path);
851         if (!d && errno != ENOTDIR) {
852                 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
853                           __FUNCTION__, path);
854                 return -EINVAL;
855         } else if (!d && !parent) {
856                 /* ENOTDIR. Open the parent dir. */
857                 p = opendir_parent(path);
858                 if (!p)
859                         GOTO(out, ret = -EINVAL);
860         }
861
862         if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
863                 goto err;
864
865         if (!d)
866                 GOTO(out, ret = 0);
867
868         while ((dent = readdir64(d)) != NULL) {
869                 ((struct find_param *)data)->have_fileinfo = 0;
870
871                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
872                         continue;
873
874                 path[len] = 0;
875                 if ((len + dent->d_reclen + 2) > size) {
876                         llapi_err(LLAPI_MSG_ERROR,
877                                   "error: %s: string buffer is too small",
878                                   __FUNCTION__);
879                         break;
880                 }
881                 strcat(path, "/");
882                 strcat(path, dent->d_name);
883
884                 if (dent->d_type == DT_UNKNOWN) {
885                         lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
886
887                         ret = llapi_mds_getfileinfo(path, d,
888                                              ((struct find_param *)data)->lmd);
889                         if (ret == 0) {
890                                 ((struct find_param *)data)->have_fileinfo = 1;
891                                 dent->d_type = llapi_filetype_dir_table[st->st_mode &
892                                                                         S_IFMT];
893                         }
894                         if (ret == -ENOENT)
895                                 continue;
896                 }
897
898                 switch (dent->d_type) {
899                 case DT_UNKNOWN:
900                         llapi_err(LLAPI_MSG_ERROR, 
901                                   "error: %s: '%s' is UNKNOWN type %d",
902                                   __FUNCTION__, dent->d_name, dent->d_type);
903                         break;
904                 case DT_DIR:
905                         ret = llapi_semantic_traverse(path, size, d, sem_init,
906                                                       sem_fini, data, dent);
907                         if (ret < 0)
908                                 goto out;
909                         break;
910                 default:
911                         ret = 0;
912                         if (sem_init) {
913                                 ret = sem_init(path, d, NULL, data, dent);
914                                 if (ret < 0)
915                                         goto out;
916                         }
917                         if (sem_fini && ret == 0)
918                                 sem_fini(path, d, NULL, data, dent);
919                 }
920         }
921
922 out:
923         path[len] = 0;
924
925         if (sem_fini)
926                 sem_fini(path, parent, d, data, de);
927 err:
928         if (d)
929                 closedir(d);
930         if (p)
931                 closedir(p);
932         return ret;
933 }
934
935 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
936  * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
937  *
938  * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
939  * The table bolow gives the answers for the specified parameters (value and
940  * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
941  * --------------------------------------
942  * 1 | file > limit; sign > 0 | -1 / -1 |
943  * 2 | file = limit; sign > 0 |  ? /  1 |
944  * 3 | file < limit; sign > 0 |  ? /  1 |
945  * 4 | file > limit; sign = 0 | -1 / -1 |
946  * 5 | file = limit; sign = 0 |  ? /  1 |  <- (see the Note below)
947  * 6 | file < limit; sign = 0 |  ? / -1 |
948  * 7 | file > limit; sign < 0 |  1 /  1 |
949  * 8 | file = limit; sign < 0 |  ? / -1 |
950  * 9 | file < limit; sign < 0 |  ? / -1 |
951  * --------------------------------------
952  * Note: 5th actually means that the value is within the interval
953  * (limit - margin, limit]. */
954 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
955                           unsigned long long margin, int mds)
956 {
957         if (sign > 0) {
958                 if (file < limit)
959                         return mds ? 0 : 1;
960         }
961
962         if (sign == 0) {
963                 if (file <= limit && file + margin > limit)
964                         return mds ? 0 : 1;
965                 if (file + margin <= limit)
966                         return mds ? 0 : -1;
967         }
968
969         if (sign < 0) {
970                 if (file > limit)
971                         return 1;
972                 if (mds)
973                         return 0;
974         }
975
976         return -1;
977 }
978
979 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
980  * Return -1 or 1 if file timestamp does not or does match the given criteria
981  * correspondingly. Return 0 if the MDS time is being checked and there are
982  * attributes on OSTs and it is not yet clear if the timespamp matches.
983  *
984  * If 0 is returned, we need to do another RPC to the OSTs to obtain the
985  * updated timestamps. */
986 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
987 {
988         int ret;
989         int rc = 0;
990
991         /* Check if file is accepted. */
992         if (param->atime) {
993                 ret = find_value_cmp(st->st_atime, param->atime,
994                                      param->asign, 24 * 60 * 60, mds);
995                 if (ret < 0)
996                         return ret;
997                 rc = ret;
998         }
999
1000         if (param->mtime) {
1001                 ret = find_value_cmp(st->st_mtime, param->mtime,
1002                                      param->msign, 24 * 60 * 60, mds);
1003                 if (ret < 0)
1004                         return ret;
1005
1006                 /* If the previous check matches, but this one is not yet clear,
1007                  * we should return 0 to do an RPC on OSTs. */
1008                 if (rc == 1)
1009                         rc = ret;
1010         }
1011
1012         if (param->ctime) {
1013                 ret = find_value_cmp(st->st_ctime, param->ctime,
1014                                      param->csign, 24 * 60 * 60, mds);
1015                 if (ret < 0)
1016                         return ret;
1017
1018                 /* If the previous check matches, but this one is not yet clear,
1019                  * we should return 0 to do an RPC on OSTs. */
1020                 if (rc == 1)
1021                         rc = ret;
1022         }
1023
1024         return rc;
1025 }
1026
1027 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1028                         void *data, cfs_dirent_t *de)
1029 {
1030         struct find_param *param = (struct find_param *)data;
1031         int decision = 1; /* 1 is accepted; -1 is rejected. */
1032         lstat_t *st = &param->lmd->lmd_st;
1033         int lustre_fs = 1;
1034         int checked_type = 0;
1035         int ret = 0;
1036
1037         LASSERT(parent != NULL || dir != NULL);
1038
1039         param->lmd->lmd_lmm.lmm_stripe_count = 0;
1040
1041         /* If a regular expression is presented, make the initial decision */
1042         if (param->pattern != NULL) {
1043                 char *fname = strrchr(path, '/');
1044                 fname = (fname == NULL ? path : fname + 1);
1045                 ret = fnmatch(param->pattern, fname, 0);
1046                 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1047                     (ret == 0 && param->exclude_pattern))
1048                         goto decided;
1049         }
1050
1051         /* See if we can check the file type from the dirent. */
1052         if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1053             de->d_type < DT_MAX) {
1054                 checked_type = 1;
1055                 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1056                         if (param->exclude_type)
1057                                 goto decided;
1058                 } else {
1059                         if (!param->exclude_type)
1060                                 goto decided;
1061                 }
1062         }
1063
1064
1065         /* If a time or OST should be checked, the decision is not taken yet. */
1066         if (param->atime || param->ctime || param->mtime || param->obduuid ||
1067             param->size)
1068                 decision = 0;
1069
1070         ret = 0;
1071         /* Request MDS for the stat info. */
1072         if (param->have_fileinfo == 0) {
1073                 if (dir) {
1074                         /* retrieve needed file info */
1075                         ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1076                                     (void *)param->lmd);
1077                 } else {
1078                         char *fname = strrchr(path, '/');
1079                         fname = (fname == NULL ? path : fname + 1);
1080
1081                         /* retrieve needed file info */
1082                         strncpy((char *)param->lmd, fname, param->lumlen);
1083                         ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1084                                    (void *)param->lmd);
1085                 }
1086         }
1087
1088         if (ret) {
1089                 if (errno == ENOTTY) {
1090                         /* ioctl is not supported, it is not a lustre fs.
1091                          * Do the regular lstat(2) instead. */
1092                         lustre_fs = 0;
1093                         ret = lstat_f(path, st);
1094                         if (ret) {
1095                                 llapi_err(LLAPI_MSG_ERROR, 
1096                                           "error: %s: lstat failed for %s",
1097                                           __FUNCTION__, path);
1098                                 return ret;
1099                         }
1100                 } else if (errno == ENOENT) {
1101                         llapi_err(LLAPI_MSG_WARN, 
1102                                   "warning: %s: %s does not exist",
1103                                   __FUNCTION__, path);
1104                         goto decided;
1105                 } else {
1106                         llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
1107                                   __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1108                                   "IOC_MDC_GETFILEINFO", path);
1109                         return ret;
1110                 }
1111         }
1112
1113         if (param->type && !checked_type) {
1114                 if ((st->st_mode & S_IFMT) == param->type) {
1115                         if (param->exclude_type)
1116                                 goto decided;
1117                 } else {
1118                         if (!param->exclude_type)
1119                                 goto decided;
1120                 }
1121         }
1122
1123         /* Prepare odb. */
1124         if (param->obduuid) {
1125                 if (lustre_fs && param->got_uuids &&
1126                     param->st_dev != st->st_dev) {
1127                         /* A lustre/lustre mount point is crossed. */
1128                         param->got_uuids = 0;
1129                         param->obds_printed = 0;
1130                         param->obdindex = OBD_NOT_FOUND;
1131                 }
1132
1133                 if (lustre_fs && !param->got_uuids) {
1134                         ret = setup_obd_indexes(dir ? dir : parent, param);
1135                         if (ret)
1136                                 return ret;
1137
1138                         param->st_dev = st->st_dev;
1139                 } else if (!lustre_fs && param->got_uuids) {
1140                         /* A lustre/non-lustre mount point is crossed. */
1141                         param->got_uuids = 0;
1142                         param->obdindex = OBD_NOT_FOUND;
1143                 }
1144         }
1145
1146         /* If an OBD UUID is specified but no one matches, skip this file. */
1147         if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1148                 goto decided;
1149
1150         /* If a OST UUID is given, and some OST matches, check it here. */
1151         if (param->obdindex != OBD_NOT_FOUND) {
1152                 if (!S_ISREG(st->st_mode))
1153                         goto decided;
1154
1155                 /* Only those files should be accepted, which have a
1156                  * stripe on the specified OST. */
1157                 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1158                         goto decided;
1159                 } else {
1160                         int i, j;
1161                         for (i = 0;
1162                              i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1163                                 for (j = 0; j < param->num_obds; j++) {
1164                                         if (param->obdindexes[j] ==
1165                                             param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
1166                                                 goto obd_matches;
1167                                 }
1168                         }
1169
1170                         if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1171                                 goto decided;
1172                 }
1173         }
1174
1175         if (param->check_uid) {
1176                 if (st->st_uid == param->uid) {
1177                         if (param->exclude_uid)
1178                                 goto decided;
1179                 } else {
1180                         if (!param->exclude_uid)
1181                                 goto decided;
1182                 }
1183         }
1184
1185         if (param->check_gid) {
1186                 if (st->st_gid == param->gid) {
1187                         if (param->exclude_gid)
1188                                 goto decided;
1189                 } else {
1190                         if (!param->exclude_gid)
1191                                 goto decided;
1192                 }
1193         }
1194
1195         /* Check the time on mds. */
1196         if (!decision) {
1197                 int for_mds;
1198
1199                 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1200                                        param->lmd->lmd_lmm.lmm_stripe_count)
1201                                     : 0;
1202                 decision = find_time_check(st, param, for_mds);
1203         }
1204
1205 obd_matches:
1206         /* If file still fits the request, ask osd for updated info.
1207            The regulat stat is almost of the same speed as some new
1208            'glimpse-size-ioctl'. */
1209         if (!decision && S_ISREG(st->st_mode) &&
1210             (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1211                 if (param->obdindex != OBD_NOT_FOUND) {
1212                         /* Check whether the obd is active or not, if it is
1213                          * not active, just print the object affected by this
1214                          * failed ost 
1215                          * */
1216                         struct obd_statfs stat_buf;
1217                         struct obd_uuid uuid_buf;
1218
1219                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
1220                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1221                         ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1222                                                param->obdindex, &stat_buf, 
1223                                                &uuid_buf);
1224                         if (ret) {
1225                                 if (ret == -ENODATA || ret == -ENODEV 
1226                                     || ret == -EIO)
1227                                         errno = EIO;
1228                                 llapi_printf(LLAPI_MSG_NORMAL, 
1229                                              "obd_uuid: %s failed %s ",
1230                                              param->obduuid->uuid, 
1231                                              strerror(errno));
1232                                 goto print_path;
1233                         }
1234                 }
1235                 if (dir) {
1236                         ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1237                                     (void *)param->lmd);
1238                 } else if (parent) {
1239                         ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1240                                     (void *)param->lmd);
1241                 }
1242
1243                 if (ret) {
1244                         if (errno == ENOENT) {
1245                                 llapi_err(LLAPI_MSG_ERROR, 
1246                                           "warning: %s: %s does not exist",
1247                                           __FUNCTION__, path);
1248                                 goto decided;
1249                         } else {
1250                                 llapi_err(LLAPI_MSG_ERROR, 
1251                                           "%s: IOC_LOV_GETINFO on %s failed",
1252                                           __FUNCTION__, path);
1253                                 return ret;
1254                         }
1255                 }
1256
1257                 /* Check the time on osc. */
1258                 decision = find_time_check(st, param, 0);
1259                 if (decision == -1)
1260                         goto decided;
1261         }
1262
1263         if (param->size)
1264                 decision = find_value_cmp(st->st_size, param->size,
1265                                           param->size_sign, param->size_units,
1266                                           0);
1267
1268 print_path:
1269         if (decision != -1) {
1270                 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1271                 if (param->zeroend)
1272                         llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1273                 else
1274                         llapi_printf(LLAPI_MSG_NORMAL, "\n");
1275         }
1276
1277 decided:
1278         /* Do not get down anymore? */
1279         if (param->depth == param->maxdepth)
1280                 return 1;
1281
1282         param->depth++;
1283         return 0;
1284 }
1285
1286 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1287                           cfs_dirent_t *de)
1288 {
1289         struct find_param *param = (struct find_param *)data;
1290         param->depth--;
1291         return 0;
1292 }
1293
1294 int llapi_find(char *path, struct find_param *param)
1295 {
1296         char *buf;
1297         int ret, len = strlen(path);
1298
1299         if (len > PATH_MAX) {
1300                 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1301                           __FUNCTION__, path);
1302                 return -EINVAL;
1303         }
1304
1305         buf = (char *)malloc(PATH_MAX + 1);
1306         if (!buf)
1307                 return -ENOMEM;
1308
1309         ret = common_param_init(param);
1310         if (ret) {
1311                 free(buf);
1312                 return ret;
1313         }
1314
1315         param->depth = 0;
1316
1317         strncpy(buf, path, PATH_MAX + 1);
1318         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1319                                       cb_common_fini, param, NULL);
1320
1321         find_param_fini(param);
1322         free(buf);
1323         return ret < 0 ? ret : 0;
1324 }
1325
1326 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1327                         cfs_dirent_t *de)
1328 {
1329         struct find_param *param = (struct find_param *)data;
1330         int ret = 0;
1331
1332         LASSERT(parent != NULL || d != NULL);
1333
1334         /* Prepare odb. */
1335         if (!param->got_uuids) {
1336                 ret = setup_obd_uuid(d ? d : parent, path, param);
1337                 if (ret)
1338                         return ret;
1339         }
1340
1341         if (d) {
1342                 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1343                             (void *)&param->lmd->lmd_lmm);
1344         } else if (parent) {
1345                 char *fname = strrchr(path, '/');
1346                 fname = (fname == NULL ? path : fname + 1);
1347
1348                 strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
1349                 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1350                             (void *)&param->lmd->lmd_lmm);
1351         }
1352
1353         if (ret) {
1354                 if (errno == ENODATA) {
1355                         if (!param->obduuid && !param->quiet)
1356                                 llapi_printf(LLAPI_MSG_NORMAL, 
1357                                              "%s has no stripe info\n", path);
1358                         goto out;
1359                 } else if (errno == ENOTTY) {
1360                         llapi_err(LLAPI_MSG_ERROR, 
1361                                   "%s: '%s' not on a Lustre fs?",
1362                                   __FUNCTION__, path);
1363                 } else if (errno == ENOENT) {
1364                         llapi_err(LLAPI_MSG_WARN, 
1365                                   "warning: %s: %s does not exist",
1366                                   __FUNCTION__, path);
1367                         goto out;
1368                 } else {
1369                         llapi_err(LLAPI_MSG_ERROR, 
1370                                   "error: %s: %s failed for %s",
1371                                    __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1372                                   "IOC_MDC_GETFILESTRIPE", path);
1373                 }
1374
1375                 return ret;
1376         }
1377
1378         llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1379 out:
1380         /* Do not get down anymore? */
1381         if (param->depth == param->maxdepth)
1382                 return 1;
1383
1384         param->depth++;
1385         return 0;
1386 }
1387
1388 int llapi_getstripe(char *path, struct find_param *param)
1389 {
1390         char *buf;
1391         int ret = 0, len = strlen(path);
1392
1393         if (len > PATH_MAX) {
1394                 llapi_err(LLAPI_MSG_ERROR, 
1395                           "%s: Path name '%s' is too long",
1396                           __FUNCTION__, path);
1397                 return -EINVAL;
1398         }
1399
1400         buf = (char *)malloc(PATH_MAX + 1);
1401         if (!buf)
1402                 return -ENOMEM;
1403
1404         ret = common_param_init(param);
1405         if (ret) {
1406                 free(buf);
1407                 return ret;
1408         }
1409
1410         param->depth = 0;
1411
1412         strncpy(buf, path, PATH_MAX + 1);
1413         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1414                                       cb_common_fini, param, NULL);
1415         find_param_fini(param);
1416         free(buf);
1417         return ret < 0 ? ret : 0;
1418 }
1419
1420 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1421                      struct obd_statfs *stat_buf,
1422                      struct obd_uuid *uuid_buf)
1423 {
1424         int fd;
1425         char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1426         char *rawbuf = raw;
1427         struct obd_ioctl_data data = { 0 };
1428         int rc = 0;
1429
1430         data.ioc_inlbuf1 = (char *)&type;
1431         data.ioc_inllen1 = sizeof(__u32);
1432         data.ioc_inlbuf2 = (char *)&index;
1433         data.ioc_inllen2 = sizeof(__u32);
1434         data.ioc_pbuf1 = (char *)stat_buf;
1435         data.ioc_plen1 = sizeof(struct obd_statfs);
1436         data.ioc_pbuf2 = (char *)uuid_buf;
1437         data.ioc_plen2 = sizeof(struct obd_uuid);
1438
1439         if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1440                 llapi_err(LLAPI_MSG_ERROR, 
1441                           "llapi_obd_statfs: error packing ioctl data");
1442                 return rc;
1443         }
1444
1445         fd = open(path, O_RDONLY);
1446         if (errno == EISDIR)
1447                 fd = open(path, O_DIRECTORY | O_RDONLY);
1448
1449         if (fd < 0) {
1450                 rc = errno ? -errno : -EBADF;
1451                 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'", 
1452                           __FUNCTION__, path);
1453                 return rc;
1454         }
1455         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1456         if (rc)
1457                 rc = errno ? -errno : -EINVAL;
1458
1459         close(fd);
1460         return rc;
1461 }
1462
1463 #define MAX_STRING_SIZE 128
1464 #define DEVICES_LIST "/proc/fs/lustre/devices"
1465
1466 int llapi_ping(char *obd_type, char *obd_name)
1467 {
1468         char path[MAX_STRING_SIZE];
1469         char buf[1];
1470         int rc, fd;
1471
1472         snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1473                  obd_type, obd_name);
1474
1475         fd = open(path, O_WRONLY);
1476         if (fd < 0) {
1477                 rc = errno;
1478                 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1479                 return rc;
1480         }
1481
1482         rc = write(fd, buf, 1);
1483         close(fd);
1484
1485         if (rc == 1)
1486                 return 0;
1487         return rc;
1488 }
1489
1490 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1491 {
1492         char buf[MAX_STRING_SIZE];
1493         FILE *fp = fopen(DEVICES_LIST, "r");
1494         int i, rc = 0;
1495
1496         if (fp == NULL) {
1497                 rc = errno;
1498                 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1499                 return rc;
1500         }
1501
1502         while (fgets(buf, sizeof(buf), fp) != NULL) {
1503                 char *obd_type_name = NULL;
1504                 char *obd_name = NULL;
1505                 char *obd_uuid = NULL;
1506                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1507                 char *bufl = rawbuf;
1508                 char *bufp = buf;
1509                 struct obd_ioctl_data datal = { 0, };
1510                 struct obd_statfs osfs_buffer;
1511
1512                 while(bufp[0] == ' ')
1513                         ++bufp;
1514
1515                 for(i = 0; i < 3; i++) {
1516                         obd_type_name = strsep(&bufp, " ");
1517                 }
1518                 obd_name = strsep(&bufp, " ");
1519                 obd_uuid = strsep(&bufp, " ");
1520
1521                 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1522
1523                 memset(bufl, 0, sizeof(rawbuf));
1524                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1525                 datal.ioc_plen1 = sizeof(osfs_buffer);
1526
1527                 for (i = 0; i < type_num; i++) {
1528                         if (strcmp(obd_type_name, obd_type[i]) != 0)
1529                                 continue;
1530
1531                         cb(obd_type_name, obd_name, obd_uuid, args);
1532                 }
1533         }
1534         fclose(fp);
1535         return rc;
1536 }
1537
1538 static void do_target_check(char *obd_type_name, char *obd_name,
1539                             char *obd_uuid, void *args)
1540 {
1541         int rc;
1542
1543         rc = llapi_ping(obd_type_name, obd_name);
1544         if (rc == ENOTCONN) {
1545                 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1546         } else if (rc) {
1547                 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1548         } else {
1549                 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1550         }
1551 }
1552
1553 int llapi_target_check(int type_num, char **obd_type, char *dir)
1554 {
1555         return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1556 }
1557
1558 #undef MAX_STRING_SIZE
1559
1560 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1561 {
1562         char raw[OBD_MAX_IOCTL_BUFFER];
1563         char out[LLOG_CHUNK_SIZE];
1564         char *buf = raw;
1565         struct obd_ioctl_data data = { 0 };
1566         char key[30];
1567         DIR *root;
1568         int rc;
1569
1570         sprintf(key, "%s", keyword);
1571         memset(raw, 0, sizeof(raw));
1572         memset(out, 0, sizeof(out));
1573         data.ioc_inlbuf1 = key;
1574         data.ioc_inllen1 = strlen(key) + 1;
1575         if (node_name) {
1576                 data.ioc_inlbuf2 = node_name;
1577                 data.ioc_inllen2 = strlen(node_name) + 1;
1578         }
1579         data.ioc_pbuf1 = out;
1580         data.ioc_plen1 = sizeof(out);
1581         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1582         if (rc)
1583                 return rc;
1584
1585         root = opendir(dir);
1586         if (root == NULL) {
1587                 rc = errno;
1588                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1589                 return rc;
1590         }
1591
1592         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1593         if (rc)
1594                 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1595         else
1596                 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1597
1598         closedir(root);
1599         return rc;
1600 }
1601
1602 /* Is this a lustre fs? */
1603 int llapi_is_lustre_mnttype(const char *type)
1604 {
1605         return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1606 }
1607
1608 /* Is this a lustre client fs? */
1609 int llapi_is_lustre_mnt(struct mntent *mnt)
1610 {
1611         return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1612                 strstr(mnt->mnt_fsname, ":/") != NULL);
1613 }
1614
1615 int llapi_quotacheck(char *mnt, int check_type)
1616 {
1617         DIR *root;
1618         int rc;
1619
1620         root = opendir(mnt);
1621         if (!root) {
1622                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1623                 return -1;
1624         }
1625
1626         rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1627
1628         closedir(root);
1629         return rc;
1630 }
1631
1632 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1633 {
1634         DIR *root;
1635         int poll_intvl = 2;
1636         int rc;
1637
1638         root = opendir(mnt);
1639         if (!root) {
1640                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1641                 return -1;
1642         }
1643
1644         while (1) {
1645                 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1646                 if (!rc)
1647                         break;
1648                 sleep(poll_intvl);
1649                 if (poll_intvl < 30)
1650                         poll_intvl *= 2;
1651         }
1652
1653         closedir(root);
1654         return rc;
1655 }
1656
1657 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1658 {
1659         DIR *root;
1660         int rc;
1661
1662         root = opendir(mnt);
1663         if (!root) {
1664                 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1665                 return -1;
1666         }
1667
1668         rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1669
1670         closedir(root);
1671         return rc;
1672 }
1673
1674 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1675                          cfs_dirent_t *de)
1676 {
1677         struct find_param *param = (struct find_param *)data;
1678         lstat_t *st;
1679         int rc;
1680
1681         LASSERT(parent != NULL || d != NULL);
1682
1683         if (d) {
1684                 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1685                            (void *)param->lmd);
1686         } else if (parent) {
1687                 char *fname = strrchr(path, '/');
1688                 fname = (fname == NULL ? path : fname + 1);
1689
1690                 strncpy((char *)param->lmd, fname, param->lumlen);
1691                 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1692                            (void *)param->lmd);
1693         } else {
1694                 return 0;
1695         }
1696
1697         if (rc) {
1698                 if (errno == ENODATA) {
1699                         if (!param->obduuid && !param->quiet)
1700                                 llapi_err(LLAPI_MSG_ERROR, 
1701                                           "%s has no stripe info", path);
1702                         rc = 0;
1703                 } else if (errno == ENOENT) {
1704                         llapi_err(LLAPI_MSG_ERROR, 
1705                                   "warning: %s: %s does not exist",
1706                                   __FUNCTION__, path);
1707                         rc = 0;
1708                 } else if (errno != EISDIR) {
1709                         rc = errno;
1710                         llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
1711                                   d ? "LL_IOC_MDC_GETINFO" :
1712                                   "IOC_MDC_GETFILEINFO", path);
1713                 }
1714                 return rc;
1715         }
1716
1717         st = &param->lmd->lmd_st;
1718
1719         /* libc chown() will do extra check, and if the real owner is
1720          * the same as the ones to set, it won't fall into kernel, so
1721          * invoke syscall directly. */
1722         rc = syscall(SYS_chown, path, -1, -1);
1723         if (rc)
1724                 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
1725
1726         rc = chmod(path, st->st_mode);
1727         if (rc)
1728                 llapi_err(LLAPI_MSG_ERROR,"error: chmod %s (%hu)", path, st->st_mode);
1729
1730         return rc;
1731 }
1732
1733 int llapi_quotachown(char *path, int flag)
1734 {
1735         struct find_param param;
1736         char *buf;
1737         int ret = 0, len = strlen(path);
1738
1739         if (len > PATH_MAX) {
1740                 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1741                           __FUNCTION__, path);
1742                 return -EINVAL;
1743         }
1744
1745         buf = (char *)malloc(PATH_MAX + 1);
1746         if (!buf)
1747                 return -ENOMEM;
1748
1749         memset(&param, 0, sizeof(param));
1750         param.recursive = 1;
1751         param.verbose = 0;
1752         param.quiet = 1;
1753
1754         ret = common_param_init(&param);
1755         if (ret)
1756                 goto out;
1757
1758         strncpy(buf, path, PATH_MAX + 1);
1759         ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1760                                       NULL, &param, NULL);
1761 out:
1762         find_param_fini(&param);
1763         free(buf);
1764         return ret;
1765 }
1766
1767 #include <pwd.h>
1768 #include <grp.h>
1769 #include <mntent.h>
1770 #include <sys/wait.h>
1771 #include <errno.h>
1772 #include <ctype.h>
1773
1774 static int rmtacl_notify(int ops)
1775 {
1776         FILE *fp;
1777         struct mntent *mnt;
1778         int found = 0, fd, rc;
1779
1780         fp = setmntent(MOUNTED, "r");
1781         if (fp == NULL) {
1782                 perror("setmntent");
1783                 return -1;
1784         }
1785
1786         while (1) {
1787                 mnt = getmntent(fp);
1788                 if (!mnt)
1789                         break;
1790
1791                 if (!llapi_is_lustre_mnt(mnt))
1792                         continue;
1793
1794                 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
1795                 if (fd < 0) {
1796                         perror("open");
1797                         return -1;
1798                 }
1799
1800                 rc = ioctl(fd, LL_IOC_RMTACL, ops);
1801                 if (rc < 0) {
1802                         perror("ioctl");
1803                 return -1;
1804         }
1805
1806                 found++;
1807         }
1808         endmntent(fp);
1809         return found;
1810 }
1811
1812 static char *next_token(char *p, int div)
1813 {
1814         if (p == NULL)
1815                 return NULL;
1816
1817         if (div)
1818                 while (*p && *p != ':' && !isspace(*p))
1819                         p++;
1820         else
1821                 while (*p == ':' || isspace(*p))
1822                         p++;
1823
1824         return *p ? p : NULL;
1825 }
1826
1827 static int rmtacl_name2id(char *name, int is_user)
1828 {
1829         if (is_user) {
1830                 struct passwd *pw;
1831
1832                 if ((pw = getpwnam(name)) == NULL)
1833                         return INVALID_ID;
1834                 else
1835                         return (int)(pw->pw_uid);
1836         } else {
1837                 struct group *gr;
1838
1839                 if ((gr = getgrnam(name)) == NULL)
1840                         return INVALID_ID;
1841                 else
1842                         return (int)(gr->gr_gid);
1843         }
1844 }
1845
1846 static int isodigit(int c)
1847 {
1848         return (c >= '0' && c <= '7') ? 1 : 0;
1849 }
1850
1851 /*
1852  * Whether the name is just digits string (uid/gid) already or not.
1853  * Return value:
1854  * 1: str is id
1855  * 0: str is not id
1856  */
1857 static int str_is_id(char *str)
1858 {
1859         if (str == NULL)
1860                 return 0;
1861
1862         if (*str == '0') {
1863                 str++;
1864                 if (*str == 'x' || *str == 'X') { /* for Hex. */
1865                         if (!isxdigit(*(++str)))
1866                                 return 0;
1867
1868                         while (isxdigit(*(++str)));
1869                 } else if (isodigit(*str)) { /* for Oct. */
1870                         while (isodigit(*(++str)));
1871                 }
1872         } else if (isdigit(*str)) { /* for Dec. */
1873                 while (isdigit(*(++str)));
1874         }
1875
1876         return (*str == 0) ? 1 : 0;
1877 }
1878
1879 typedef struct {
1880         char *name;
1881         int   length;
1882         int   is_user;
1883         int   next_token;
1884 } rmtacl_name_t;
1885
1886 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
1887
1888 static rmtacl_name_t rmtacl_namelist[] = {
1889         { RMTACL_OPTNAME("user:"),            1,      0 },
1890         { RMTACL_OPTNAME("group:"),           0,      0 },
1891         { RMTACL_OPTNAME("default:user:"),    1,      0 },
1892         { RMTACL_OPTNAME("default:group:"),   0,      0 },
1893         /* for --tabular option */
1894         { RMTACL_OPTNAME("user"),             1,      1 },
1895         { RMTACL_OPTNAME("group"),            0,      1 },
1896         { 0 }
1897 };
1898
1899 static int rgetfacl_output(char *str)
1900 {
1901         char *start = NULL, *end = NULL;
1902         int is_user = 0, n, id;
1903         char c;
1904         rmtacl_name_t *rn;
1905
1906         if (str == NULL)
1907                 return -1;
1908
1909         for (rn = rmtacl_namelist; rn->name; rn++) {
1910                 if(strncmp(str, rn->name, rn->length) == 0) {
1911                         if (!rn->next_token)
1912                                 start = str + rn->length;
1913                         else
1914                                 start = next_token(str + rn->length, 0);
1915                         is_user = rn->is_user;
1916                         break;
1917                 }
1918         }
1919
1920         end = next_token(start, 1);
1921         if (end == NULL || start == end) {
1922                 n = printf("%s", str);
1923                 return n;
1924         }
1925
1926         c = *end;
1927         *end = 0;
1928         id = rmtacl_name2id(start, is_user);
1929         if (id == INVALID_ID) {
1930                 if (str_is_id(start)) {
1931                         *end = c;
1932                         n = printf("%s", str);
1933                 } else
1934                         return -1;
1935         } else if ((id == NOBODY_UID && is_user) ||
1936                    (id == NOBODY_GID && !is_user)) {
1937                 *end = c;
1938                 n = printf("%s", str);
1939         } else {
1940                 *end = c;
1941                 *start = 0;
1942                 n = printf("%s%d%s", str, id, end);
1943         }
1944         return n;
1945 }
1946
1947 static int child_status(int status)
1948 {
1949         return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
1950 }
1951
1952 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
1953 {
1954         pid_t pid = 0;
1955         int fd[2], status;
1956         FILE *fp;
1957         char buf[PIPE_BUF];
1958
1959         if (output_func) {
1960                 if (pipe(fd) < 0) {
1961                         perror("pipe");
1962                         return -1;
1963                 }
1964
1965                 if ((pid = fork()) < 0) {
1966                         perror("fork");
1967                         close(fd[0]);
1968                         close(fd[1]);
1969                         return -1;
1970                 } else if (!pid) {
1971                         /* child process redirects its output. */
1972                         close(fd[0]);
1973                         close(1);
1974                         if (dup2(fd[1], 1) < 0) {
1975                                 perror("dup2");
1976                                 close(fd[1]);
1977                                 return -1;
1978                         }
1979                 } else {
1980                         close(fd[1]);
1981                 }
1982         }
1983
1984         if (!pid) {
1985                 status = rmtacl_notify(ops);
1986                 if (status < 0)
1987                         return -1;
1988
1989                 exit(execvp(argv[0], argv));
1990         }
1991
1992         /* the following is parent process */
1993         if ((fp = fdopen(fd[0], "r")) == NULL) {
1994                 perror("fdopen");
1995                 kill(pid, SIGKILL);
1996                 close(fd[0]);
1997                 return -1;
1998         }
1999
2000         while (fgets(buf, PIPE_BUF, fp) != NULL) {
2001                 if (output_func(buf) < 0)
2002                         fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2003                                 buf);
2004         }
2005         fclose(fp);
2006         close(fd[0]);
2007
2008         if (waitpid(pid, &status, 0) < 0) {
2009                 perror("waitpid");
2010                 return -1;
2011         }
2012
2013         return child_status(status);
2014 }
2015
2016 int llapi_lsetfacl(int argc, char *argv[])
2017 {
2018         return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2019 }
2020
2021 int llapi_lgetfacl(int argc, char *argv[])
2022 {
2023         return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2024 }
2025
2026 int llapi_rsetfacl(int argc, char *argv[])
2027 {
2028         return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2029 }
2030
2031 int llapi_rgetfacl(int argc, char *argv[])
2032 {
2033         return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2034 }
2035
2036 int llapi_cp(int argc, char *argv[])
2037 {
2038         int rc;
2039
2040         rc = rmtacl_notify(RMT_RSETFACL);
2041         if (rc < 0)
2042                 return -1;
2043
2044         exit(execvp(argv[0], argv));
2045 }
2046
2047 int llapi_ls(int argc, char *argv[])
2048 {
2049         int rc;
2050
2051         rc = rmtacl_notify(RMT_LGETFACL);
2052         if (rc < 0)
2053                 return -1;
2054
2055         exit(execvp(argv[0], argv));
2056 }