Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 0ea4b93..c5e410e 100644 (file)
@@ -55,7 +55,6 @@
 #include <liblustre.h>
 #include <obd.h>
 #include <lustre_lib.h>
-#include <lustre/liblustreapi.h>
 #include <obd_lov.h>
 #include <lustre/liblustreapi.h>
 
@@ -70,23 +69,24 @@ static void err_msg(char *fmt, ...)
         fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
 }
 
-int llapi_file_create(const char *name, unsigned long stripe_size,
-                      int stripe_offset, int stripe_count, int stripe_pattern)
+int llapi_file_open(const char *name, int flags, int mode,
+                    unsigned long stripe_size, int stripe_offset,
+                    int stripe_count, int stripe_pattern)
 {
         struct lov_user_md lum = { 0 };
         int fd, rc = 0;
         int isdir = 0;
         int page_size;
 
-        fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
-        if (errno == EISDIR) {
+        fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
+        if (fd < 0 && errno == EISDIR) {
                 fd = open(name, O_DIRECTORY | O_RDONLY);
                 isdir++;
         }
 
         if (fd < 0) {
                 rc = -errno;
-                err_msg("unable to open '%s'",name);
+                err_msg("unable to open '%s'", name);
                 return rc;
         }
 
@@ -139,12 +139,26 @@ int llapi_file_create(const char *name, unsigned long stripe_size,
                         (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
         }
 out:
-        if (close(fd) < 0) {
-                if (rc == 0)
-                        rc = -errno;
-                err_msg("error on close for '%s' (%d)", name, fd);
+        if (rc) {
+                close(fd);
+                fd = rc;
         }
-        return rc;
+
+        return fd;
+}
+
+int llapi_file_create(const char *name, unsigned long stripe_size,
+                      int stripe_offset, int stripe_count, int stripe_pattern)
+{
+        int fd;
+
+        fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
+                             stripe_offset, stripe_count, stripe_pattern);
+        if (fd < 0)
+                return fd;
+
+        close(fd);
+        return 0;
 }
 
 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
@@ -182,12 +196,8 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
         /* Get the lov name */
         rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
         if (rc) {
-                if (errno != ENOTTY) {
-                        rc = errno;
-                        err_msg("error: can't get lov name.");
-                } else {
-                        rc = 0;
-                }
+                rc = errno;
+                err_msg("error: can't get lov name.");
                 return rc;
         }
 
@@ -202,7 +212,7 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
         }
 
         while ((fgets(buf, sizeof(buf), fp) != NULL) && index < *ost_count) {
-                if (sscanf(buf, "%d: %s", &index, &uuidp[index]) < 2)
+                if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) < 2)
                         break;
                 index++;
         }
@@ -775,7 +785,8 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
                 decision = -1;
 
         /* If a OST UUID is given, and some OST matches, check it here. */
-        if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
+        if (decision != -1 && param->obdindex != OBD_NOT_FOUND &&
+            S_ISREG(st->st_mode)) {
                 /* Only those files should be accepted, which have a strip on
                  * the specified OST. */
                 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
@@ -798,14 +809,39 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
         if (!decision) {
                 int for_mds;
 
-                for_mds = lustre_fs ? param->lmd->lmd_lmm.lmm_stripe_count : 0;
+                for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
+                                       param->lmd->lmd_lmm.lmm_stripe_count)
+                                    : 0;
                 decision = find_time_check(st, param, for_mds);
         }
 
         /* If file still fits the request, ask osd for updated info.
            The regulat stat is almost of the same speed as some new
            'glimpse-size-ioctl'. */
-        if (!decision && param->lmd->lmd_lmm.lmm_stripe_count) {
+        if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
+            S_ISREG(st->st_mode)) {
+                if (param->obdindex != OBD_NOT_FOUND) {
+                        /* Check whether the obd is active or not, if it is
+                         * not active, just print the object affected by this
+                         * failed ost 
+                         * */
+                        struct obd_statfs stat_buf;
+                        struct obd_uuid uuid_buf;
+
+                        memset(&stat_buf, 0, sizeof(struct obd_statfs));
+                        memset(&uuid_buf, 0, sizeof(struct obd_uuid));
+                        ret = llapi_obd_statfs(path, LL_STATFS_LOV,
+                                               param->obdindex, &stat_buf, 
+                                               &uuid_buf);
+                        if (ret) {
+                                if (ret == -ENODATA || ret == -ENODEV 
+                                    || ret == -EIO)
+                                        errno = EIO;
+                                printf("obd_uuid: %s failed %s ",
+                                        param->obduuid->uuid, strerror(errno));
+                                goto print_path;
+                        }
+                }
                 if (dir) {
                         ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
                                     (void *)param->lmd);
@@ -825,6 +861,7 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
                         decision = find_time_check(st, param, 0);
         }
 
+print_path:
         if (decision != -1) {
                 printf("%s", path);
                 if (param->zeroend)
@@ -1260,3 +1297,68 @@ out:
         find_param_fini(&param);
         return ret;
 }
+
+int llapi_getfacl(char *fname, char *cmd)
+{
+        struct rmtacl_ioctl_data data;
+        char out[RMTACL_SIZE_MAX] = "";
+        int fd, rc;
+
+        data.cmd = cmd;
+        data.cmd_len = strlen(cmd) + 1;
+        data.res = out;
+        data.res_len = sizeof(out);
+
+        fd = open(fname, 0);
+        if (fd == -1) {
+                err_msg("open %s failed", fname);
+                return -1;
+        }
+
+        rc = ioctl(fd, LL_IOC_GETFACL, &data);
+        close(fd);
+        if (errno == EBADE) {
+                fprintf(stderr, "Please use getfacl directly!\n");
+                rc = 1;
+        } else if (rc) {
+                err_msg("getfacl %s failed", fname);
+        } else {
+                printf("%s", out);
+        }
+
+        return rc;
+}
+
+int llapi_setfacl(char *fname, char *cmd)
+{
+        struct rmtacl_ioctl_data data;
+        char out[RMTACL_SIZE_MAX] = "";
+        int fd, rc;
+
+        data.cmd = cmd;
+        data.cmd_len = strlen(cmd) + 1;
+        data.res = out;
+        data.res_len = sizeof(out);
+
+        fd = open(fname, 0);
+        if (fd == -1) {
+                err_msg("open %s failed", fname);
+                return -1;
+        }
+
+        rc = ioctl(fd, LL_IOC_SETFACL, &data);
+        close(fd);
+        if (errno == EBADE) {
+                fprintf(stderr, "Please use setfacl directly!\n");
+                rc = 1;
+        } else if (errno == EOPNOTSUPP) {
+                fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
+                rc = 1;
+        } else if (rc) {
+                err_msg("setfacl %s failed", fname);
+        } else {
+                printf("%s", out);
+        }
+
+        return rc;
+}