Whamcloud - gitweb
LU-8631 quota: better error message for 'lfs quota'
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index eebb848..820d28d 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -2103,13 +2099,13 @@ static int sattr_cache_get_defaults(const char *const fsname,
 }
 
 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
-                                     struct lov_user_ost_data_v1 *objects,
-                                     int is_dir, int verbose, int depth,
-                                     int raw, char *pool_name)
+                                    struct lov_user_ost_data_v1 *objects,
+                                    int is_dir, int verbose, int depth,
+                                    int raw, char *pool_name)
 {
-        char *prefix = is_dir ? "" : "lmm_";
+       char *prefix = is_dir ? "" : "lmm_";
        char *separator = "";
-        int rc;
+       int rc;
 
        if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
                lmm_oi_set_seq(&lum->lmm_oi, 0);
@@ -2117,8 +2113,8 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
        }
 
-        if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
-                llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
+       if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
+               llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
 
        if ((verbose & VERBOSE_DETAIL) && !is_dir) {
                llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic:          0x%08X\n",
@@ -2128,58 +2124,86 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id:      %#jx\n",
                             (uintmax_t)lmm_oi_id(&lum->lmm_oi));
        }
+       if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
+               if (verbose & ~VERBOSE_DFID)
+                       llapi_printf(LLAPI_MSG_NORMAL, "lmm_fid:            ");
+               /* This needs a bit of hand-holding since old 1.x lmm_oi
+                * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
+                * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
+                * a real FID.  Ideally the 2.x code would have stored this
+                * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
+                * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
+                * worked properly (especially since IGIF FIDs use mds_inum as
+                * the FID SEQ), but unfortunately that didn't happen.
+                *
+                * Print it to look like an IGIF FID, even though the fields
+                * are reversed on disk, so that it makes sense to userspace.
+                *
+                * Don't use ostid_id() and ostid_seq(), since they assume the
+                * oi_fid fields are in the right order.  This is why there are
+                * separate lmm_oi_seq() and lmm_oi_id() routines for this.
+                *
+                * For newer layout types hopefully this will be a real FID. */
+               llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
+                            lmm_oi_seq(&lum->lmm_oi) == 0 ?
+                               lmm_oi_id(&lum->lmm_oi) :
+                               lmm_oi_seq(&lum->lmm_oi),
+                            lmm_oi_seq(&lum->lmm_oi) == 0 ?
+                               0 : (__u32)lmm_oi_id(&lum->lmm_oi),
+                            (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
+       }
 
-        if (verbose & VERBOSE_COUNT) {
-                if (verbose & ~VERBOSE_COUNT)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count:   ",
-                                     prefix);
-                if (is_dir) {
-                        if (!raw && lum->lmm_stripe_count == 0) {
-                                unsigned int scount;
-                                rc = sattr_cache_get_defaults(NULL, path,
-                                                              &scount, NULL,
-                                                              NULL);
-                                if (rc == 0)
+       if (verbose & VERBOSE_COUNT) {
+               if (verbose & ~VERBOSE_COUNT)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count:   ",
+                                    prefix);
+               if (is_dir) {
+                       if (!raw && lum->lmm_stripe_count == 0) {
+                               unsigned int scount;
+                               rc = sattr_cache_get_defaults(NULL, path,
+                                                             &scount, NULL,
+                                                             NULL);
+                               if (rc == 0)
                                        llapi_printf(LLAPI_MSG_NORMAL, "%d",
                                                     scount);
-                                else
-                                        llapi_error(LLAPI_MSG_ERROR, rc,
-                                                    "Cannot determine default"
-                                                    " stripe count.");
-                        } else {
+                               else
+                                       llapi_error(LLAPI_MSG_ERROR, rc,
+                                                   "Cannot determine default"
+                                                   " stripe count.");
+                       } else {
                                llapi_printf(LLAPI_MSG_NORMAL, "%d",
-                                             lum->lmm_stripe_count ==
-                                             (typeof(lum->lmm_stripe_count))(-1)
+                                            lum->lmm_stripe_count ==
+                                            (typeof(lum->lmm_stripe_count))(-1)
                                             ? -1 : lum->lmm_stripe_count);
-                        }
-                } else {
+                       }
+               } else {
                        llapi_printf(LLAPI_MSG_NORMAL, "%hd",
                                     (__s16)lum->lmm_stripe_count);
-                }
+               }
                separator = is_dir ? " " : "\n";
-        }
+       }
 
-        if (verbose & VERBOSE_SIZE) {
+       if (verbose & VERBOSE_SIZE) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_SIZE)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size:    ",
-                                     prefix);
-                if (is_dir && !raw && lum->lmm_stripe_size == 0) {
-                        unsigned int ssize;
-                        rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
-                                                      NULL);
-                        if (rc == 0)
+               if (verbose & ~VERBOSE_SIZE)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size:    ",
+                                    prefix);
+               if (is_dir && !raw && lum->lmm_stripe_size == 0) {
+                       unsigned int ssize;
+                       rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
+                                                     NULL);
+                       if (rc == 0)
                                llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
-                        else
-                                llapi_error(LLAPI_MSG_ERROR, rc,
-                                            "Cannot determine default"
-                                            " stripe size.");
-                } else {
+                       else
+                               llapi_error(LLAPI_MSG_ERROR, rc,
+                                           "Cannot determine default"
+                                           " stripe size.");
+               } else {
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     lum->lmm_stripe_size);
-                }
+               }
                separator = is_dir ? " " : "\n";
-        }
+       }
 
        if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
@@ -2190,42 +2214,42 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                separator = "\n";
        }
 
-        if ((verbose & VERBOSE_GENERATION) && !is_dir) {
+       if ((verbose & VERBOSE_GENERATION) && !is_dir) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_GENERATION)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen:     ",
-                                     prefix);
+               if (verbose & ~VERBOSE_GENERATION)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen:     ",
+                                    prefix);
                llapi_printf(LLAPI_MSG_NORMAL, "%u",
                             (int)lum->lmm_layout_gen);
                separator = "\n";
-        }
+       }
 
-        if (verbose & VERBOSE_OFFSET) {
+       if (verbose & VERBOSE_OFFSET) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_OFFSET)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset:  ",
-                                     prefix);
-                if (is_dir)
+               if (verbose & ~VERBOSE_OFFSET)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset:  ",
+                                    prefix);
+               if (is_dir)
                        llapi_printf(LLAPI_MSG_NORMAL, "%d",
-                                     lum->lmm_stripe_offset ==
-                                     (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
+                                    lum->lmm_stripe_offset ==
+                                    (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
                                     lum->lmm_stripe_offset);
-                else
+               else
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     objects[0].l_ost_idx);
                separator = is_dir ? " " : "\n";
-        }
+       }
 
-        if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
+       if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
-                if (verbose & ~VERBOSE_POOL)
-                        llapi_printf(LLAPI_MSG_NORMAL, "%spool:           ",
-                                     prefix);
+               if (verbose & ~VERBOSE_POOL)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%spool:           ",
+                                    prefix);
                llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
-        }
+       }
 
-        if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
-                llapi_printf(LLAPI_MSG_NORMAL, "\n");
+       if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
+               llapi_printf(LLAPI_MSG_NORMAL, "\n");
 }
 
 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
@@ -3501,8 +3525,16 @@ int llapi_is_lustre_mnt(struct mntent *mnt)
 
 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
 {
-        DIR *root;
-        int rc;
+       char fsname[PATH_MAX + 1];
+       DIR *root;
+       int rc;
+
+       rc = llapi_search_fsname(mnt, fsname);
+       if (rc) {
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "'%s' isn't on Lustre filesystem", mnt);
+               return rc;
+       }
 
         root = opendir(mnt);
         if (!root) {
@@ -3519,317 +3551,6 @@ int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
         return rc;
 }
 
-#include <pwd.h>
-#include <grp.h>
-#include <mntent.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <ctype.h>
-
-static int rmtacl_notify(int ops)
-{
-        FILE *fp;
-        struct mntent *mnt;
-       int found = 0, fd = 0, rc = 0;
-
-        fp = setmntent(MOUNTED, "r");
-        if (fp == NULL) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc,
-                            "error setmntent(%s)", MOUNTED);
-                return rc;
-        }
-
-        while (1) {
-                mnt = getmntent(fp);
-                if (!mnt)
-                        break;
-
-               if (!llapi_is_lustre_mnt(mnt))
-                        continue;
-
-                fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
-                if (fd < 0) {
-                        rc = -errno;
-                        llapi_error(LLAPI_MSG_ERROR, rc,
-                                   "Can't open '%s'", mnt->mnt_dir);
-                       goto out;
-                }
-
-                rc = ioctl(fd, LL_IOC_RMTACL, ops);
-               close(fd);
-                if (rc < 0) {
-                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc,
-                                   "ioctl RMTACL on '%s' err %d",
-                                   mnt->mnt_dir, rc);
-                       goto out;
-                }
-
-                found++;
-        }
-
-out:
-        endmntent(fp);
-       return ((rc != 0) ? rc : found);
-}
-
-static char *next_token(char *p, int div)
-{
-        if (p == NULL)
-                return NULL;
-
-        if (div)
-                while (*p && *p != ':' && !isspace(*p))
-                        p++;
-        else
-                while (*p == ':' || isspace(*p))
-                        p++;
-
-        return *p ? p : NULL;
-}
-
-static int rmtacl_name2id(char *name, int is_user)
-{
-        if (is_user) {
-                struct passwd *pw;
-
-                pw = getpwnam(name);
-                if (pw == NULL)
-                        return INVALID_ID;
-                else
-                        return (int)(pw->pw_uid);
-        } else {
-                struct group *gr;
-
-                gr = getgrnam(name);
-                if (gr == NULL)
-                        return INVALID_ID;
-                else
-                        return (int)(gr->gr_gid);
-        }
-}
-
-static int isodigit(int c)
-{
-        return (c >= '0' && c <= '7') ? 1 : 0;
-}
-
-/*
- * Whether the name is just digits string (uid/gid) already or not.
- * Return value:
- * 1: str is id
- * 0: str is not id
- */
-static int str_is_id(char *str)
-{
-        if (str == NULL)
-                return 0;
-
-        if (*str == '0') {
-                str++;
-                if (*str == 'x' || *str == 'X') { /* for Hex. */
-                        if (!isxdigit(*(++str)))
-                                return 0;
-
-                        while (isxdigit(*(++str)));
-                } else if (isodigit(*str)) { /* for Oct. */
-                        while (isodigit(*(++str)));
-                }
-        } else if (isdigit(*str)) { /* for Dec. */
-                while (isdigit(*(++str)));
-        }
-
-        return (*str == 0) ? 1 : 0;
-}
-
-typedef struct {
-        char *name;
-        int   length;
-        int   is_user;
-        int   next_token;
-} rmtacl_name_t;
-
-#define RMTACL_OPTNAME(name) name, sizeof(name) - 1
-
-static rmtacl_name_t rmtacl_namelist[] = {
-        { RMTACL_OPTNAME("user:"),            1,      0 },
-        { RMTACL_OPTNAME("group:"),           0,      0 },
-        { RMTACL_OPTNAME("default:user:"),    1,      0 },
-        { RMTACL_OPTNAME("default:group:"),   0,      0 },
-        /* for --tabular option */
-        { RMTACL_OPTNAME("user"),             1,      1 },
-        { RMTACL_OPTNAME("group"),            0,      1 },
-        { 0 }
-};
-
-static int rgetfacl_output(char *str)
-{
-        char *start = NULL, *end = NULL;
-        int is_user = 0, n, id;
-        char c;
-        rmtacl_name_t *rn;
-
-        if (str == NULL)
-                return -1;
-
-        for (rn = rmtacl_namelist; rn->name; rn++) {
-                if(strncmp(str, rn->name, rn->length) == 0) {
-                        if (!rn->next_token)
-                                start = str + rn->length;
-                        else
-                                start = next_token(str + rn->length, 0);
-                        is_user = rn->is_user;
-                        break;
-                }
-        }
-
-        end = next_token(start, 1);
-        if (end == NULL || start == end) {
-                n = printf("%s", str);
-                return n;
-        }
-
-        c = *end;
-        *end = 0;
-        id = rmtacl_name2id(start, is_user);
-        if (id == INVALID_ID) {
-                if (str_is_id(start)) {
-                        *end = c;
-                        n = printf("%s", str);
-                } else
-                        return -1;
-        } else if ((id == NOBODY_UID && is_user) ||
-                   (id == NOBODY_GID && !is_user)) {
-                *end = c;
-                n = printf("%s", str);
-        } else {
-                *end = c;
-                *start = 0;
-                n = printf("%s%d%s", str, id, end);
-        }
-        return n;
-}
-
-static int child_status(int status)
-{
-        return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
-}
-
-static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
-{
-        pid_t pid = 0;
-        int fd[2], status, rc;
-        FILE *fp;
-        char buf[PIPE_BUF];
-
-        if (output_func) {
-                if (pipe(fd) < 0) {
-                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe");
-                        return rc;
-                }
-
-                pid = fork();
-                if (pid < 0) {
-                        rc = -errno;
-                       llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork");
-                        close(fd[0]);
-                        close(fd[1]);
-                        return rc;
-                } else if (!pid) {
-                        /* child process redirects its output. */
-                        close(fd[0]);
-                        close(1);
-                        if (dup2(fd[1], 1) < 0) {
-                                rc = -errno;
-                                llapi_error(LLAPI_MSG_ERROR, rc,
-                                           "Can't dup2 %d", fd[1]);
-                                close(fd[1]);
-                                return rc;
-                        }
-                } else {
-                        close(fd[1]);
-                }
-        }
-
-        if (!pid) {
-                status = rmtacl_notify(ops);
-                if (status < 0)
-                        return -errno;
-
-                exit(execvp(argv[0], argv));
-        }
-
-        /* the following is parent process */
-        fp = fdopen(fd[0], "r");
-        if (fp == NULL) {
-                rc = -errno;
-               llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed", fd[0]);
-                kill(pid, SIGKILL);
-                close(fd[0]);
-                return rc;
-        }
-
-        while (fgets(buf, PIPE_BUF, fp) != NULL) {
-                if (output_func(buf) < 0)
-                        fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
-                                buf);
-        }
-        fclose(fp);
-        close(fd[0]);
-
-        if (waitpid(pid, &status, 0) < 0) {
-                rc = -errno;
-               llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed", pid);
-                return rc;
-        }
-
-        return child_status(status);
-}
-
-int llapi_lsetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
-}
-
-int llapi_lgetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
-}
-
-int llapi_rsetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
-}
-
-int llapi_rgetfacl(int argc, char *argv[])
-{
-        return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
-}
-
-int llapi_cp(int argc, char *argv[])
-{
-        int rc;
-
-        rc = rmtacl_notify(RMT_RSETFACL);
-        if (rc < 0)
-                return rc;
-
-        exit(execvp(argv[0], argv));
-}
-
-int llapi_ls(int argc, char *argv[])
-{
-        int rc;
-
-        rc = rmtacl_notify(RMT_LGETFACL);
-        if (rc < 0)
-                return rc;
-
-        exit(execvp(argv[0], argv));
-}
-
 /* Print mdtname 'name' into 'buf' using 'format'.  Add -MDT0000 if needed.
  * format must have %s%s, buf must be > 16
  * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
@@ -4115,6 +3836,7 @@ int llapi_changelog_clear(const char *mdtname, const char *idstr,
 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
                   int buflen, long long *recno, int *linkno)
 {
+       const char *fidstr_orig = fidstr;
        struct lu_fid fid;
        struct getinfo_fid2path *gf;
        int rc;
@@ -4125,8 +3847,8 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
        sscanf(fidstr, SFID, RFID(&fid));
        if (!fid_is_sane(&fid)) {
                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                 "bad FID format [%s], should be [seq:oid:ver]"
-                                 " (e.g. "DFID")\n", fidstr,
+                                 "bad FID format '%s', should be [seq:oid:ver]"
+                                 " (e.g. "DFID")\n", fidstr_orig,
                                  (unsigned long long)FID_SEQ_NORMAL, 2, 0);
                return -EINVAL;
        }
@@ -4134,6 +3856,7 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
        gf = malloc(sizeof(*gf) + buflen);
        if (gf == NULL)
                return -ENOMEM;
+
        gf->gf_fid = fid;
        gf->gf_recno = *recno;
        gf->gf_linkno = *linkno;
@@ -4141,21 +3864,20 @@ int llapi_fid2path(const char *device, const char *fidstr, char *buf,
 
        /* Take path or fsname */
        rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
-       if (rc) {
-               if (rc != -ENOENT)
-                       llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
-       } else {
-               memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
-               if (buf[0] == '\0') { /* ROOT path */
-                       buf[0] = '/';
-                       buf[1] = '\0';
-               }
-               *recno = gf->gf_recno;
-               *linkno = gf->gf_linkno;
-        }
+       if (rc)
+               goto out_free;
 
-        free(gf);
-        return rc;
+       memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
+       if (buf[0] == '\0') { /* ROOT path */
+               buf[0] = '/';
+               buf[1] = '\0';
+       }
+       *recno = gf->gf_recno;
+       *linkno = gf->gf_linkno;
+
+out_free:
+       free(gf);
+       return rc;
 }
 
 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
@@ -4288,34 +4010,6 @@ int llapi_get_connect_flags(const char *mnt, __u64 *flags)
         return rc;
 }
 
-int llapi_get_version(char *buffer, int buffer_size,
-                      char **version)
-{
-        int rc;
-        int fd;
-        struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
-
-        fd = open(OBD_DEV_PATH, O_RDONLY);
-        if (fd == -1)
-                return -errno;
-
-        memset(buffer, 0, buffer_size);
-        data->ioc_version = OBD_IOCTL_VERSION;
-        data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
-        data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
-        data->ioc_len = obd_ioctl_packlen(data);
-
-        rc = ioctl(fd, OBD_GET_VERSION, buffer);
-        if (rc == -1) {
-                rc = -errno;
-                close(fd);
-                return rc;
-        }
-        close(fd);
-        *version = data->ioc_bulk;
-        return 0;
-}
-
 /**
  * Get a 64-bit value representing the version of file data pointed by fd.
  *