Merge tag 'for-linus-3.4' of git://git.infradead.org/mtd-2.6
[firefly-linux-kernel-4.4.55.git] / fs / ceph / xattr.c
index a76f697303d9e5db700598fc817008494908bb99..35b86331d8a5ce84c311e9eb2730757f80149179 100644 (file)
@@ -8,9 +8,12 @@
 #include <linux/xattr.h>
 #include <linux/slab.h>
 
+#define XATTR_CEPH_PREFIX "ceph."
+#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
+
 static bool ceph_is_valid_xattr(const char *name)
 {
-       return !strncmp(name, "ceph.", 5) ||
+       return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
               !strncmp(name, XATTR_SECURITY_PREFIX,
                        XATTR_SECURITY_PREFIX_LEN) ||
               !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
@@ -21,79 +24,91 @@ static bool ceph_is_valid_xattr(const char *name)
  * These define virtual xattrs exposing the recursive directory
  * statistics and layout metadata.
  */
-struct ceph_vxattr_cb {
-       bool readonly;
+struct ceph_vxattr {
        char *name;
+       size_t name_size;       /* strlen(name) + 1 (for '\0') */
        size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
                              size_t size);
+       bool readonly;
 };
 
 /* directories */
 
-static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
                                        size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
 }
 
-static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
                                      size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_files);
 }
 
-static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
                                        size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_subdirs);
 }
 
-static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
                                         size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
 }
 
-static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
                                       size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_rfiles);
 }
 
-static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
                                         size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_rsubdirs);
 }
 
-static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
                                       size_t size)
 {
        return snprintf(val, size, "%lld", ci->i_rbytes);
 }
 
-static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
                                       size_t size)
 {
-       return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
+       return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
                        (long)ci->i_rctime.tv_nsec);
 }
 
-static struct ceph_vxattr_cb ceph_dir_vxattrs[] = {
-       { true, "ceph.dir.entries", ceph_vxattrcb_entries},
-       { true, "ceph.dir.files", ceph_vxattrcb_files},
-       { true, "ceph.dir.subdirs", ceph_vxattrcb_subdirs},
-       { true, "ceph.dir.rentries", ceph_vxattrcb_rentries},
-       { true, "ceph.dir.rfiles", ceph_vxattrcb_rfiles},
-       { true, "ceph.dir.rsubdirs", ceph_vxattrcb_rsubdirs},
-       { true, "ceph.dir.rbytes", ceph_vxattrcb_rbytes},
-       { true, "ceph.dir.rctime", ceph_vxattrcb_rctime},
-       { true, NULL, NULL }
+#define CEPH_XATTR_NAME(_type, _name)  XATTR_CEPH_PREFIX #_type "." #_name
+
+#define XATTR_NAME_CEPH(_type, _name) \
+               { \
+                       .name = CEPH_XATTR_NAME(_type, _name), \
+                       .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
+                       .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
+                       .readonly = true, \
+               }
+
+static struct ceph_vxattr ceph_dir_vxattrs[] = {
+       XATTR_NAME_CEPH(dir, entries),
+       XATTR_NAME_CEPH(dir, files),
+       XATTR_NAME_CEPH(dir, subdirs),
+       XATTR_NAME_CEPH(dir, rentries),
+       XATTR_NAME_CEPH(dir, rfiles),
+       XATTR_NAME_CEPH(dir, rsubdirs),
+       XATTR_NAME_CEPH(dir, rbytes),
+       XATTR_NAME_CEPH(dir, rctime),
+       { 0 }   /* Required table terminator */
 };
+static size_t ceph_dir_vxattrs_name_size;      /* total size of all names */
 
 /* files */
 
-static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
+static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
                                   size_t size)
 {
        int ret;
@@ -103,21 +118,32 @@ static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
                (unsigned long long)ceph_file_layout_su(ci->i_layout),
                (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
                (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
-       if (ceph_file_layout_pg_preferred(ci->i_layout))
-               ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
+
+       if (ceph_file_layout_pg_preferred(ci->i_layout) >= 0) {
+               val += ret;
+               size -= ret;
+               ret += snprintf(val, size, "preferred_osd=%lld\n",
                            (unsigned long long)ceph_file_layout_pg_preferred(
                                    ci->i_layout));
+       }
+
        return ret;
 }
 
-static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
-       { true, "ceph.file.layout", ceph_vxattrcb_layout},
+static struct ceph_vxattr ceph_file_vxattrs[] = {
+       XATTR_NAME_CEPH(file, layout),
        /* The following extended attribute name is deprecated */
-       { true, "ceph.layout", ceph_vxattrcb_layout},
-       { true, NULL, NULL }
+       {
+               .name = XATTR_CEPH_PREFIX "layout",
+               .name_size = sizeof (XATTR_CEPH_PREFIX "layout"),
+               .getxattr_cb = ceph_vxattrcb_file_layout,
+               .readonly = true,
+       },
+       { 0 }   /* Required table terminator */
 };
+static size_t ceph_file_vxattrs_name_size;     /* total size of all names */
 
-static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
+static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
 {
        if (S_ISDIR(inode->i_mode))
                return ceph_dir_vxattrs;
@@ -126,14 +152,59 @@ static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
        return NULL;
 }
 
-static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr,
+static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
+{
+       if (vxattrs == ceph_dir_vxattrs)
+               return ceph_dir_vxattrs_name_size;
+       if (vxattrs == ceph_file_vxattrs)
+               return ceph_file_vxattrs_name_size;
+       BUG();
+
+       return 0;
+}
+
+/*
+ * Compute the aggregate size (including terminating '\0') of all
+ * virtual extended attribute names in the given vxattr table.
+ */
+static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
+{
+       struct ceph_vxattr *vxattr;
+       size_t size = 0;
+
+       for (vxattr = vxattrs; vxattr->name; vxattr++)
+               size += vxattr->name_size;
+
+       return size;
+}
+
+/* Routines called at initialization and exit time */
+
+void __init ceph_xattr_init(void)
+{
+       ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
+       ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
+}
+
+void ceph_xattr_exit(void)
+{
+       ceph_dir_vxattrs_name_size = 0;
+       ceph_file_vxattrs_name_size = 0;
+}
+
+static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
                                                const char *name)
 {
-       do {
-               if (strcmp(vxattr->name, name) == 0)
-                       return vxattr;
-               vxattr++;
-       } while (vxattr->name);
+       struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
+
+       if (vxattr) {
+               while (vxattr->name) {
+                       if (!strcmp(vxattr->name, name))
+                               return vxattr;
+                       vxattr++;
+               }
+       }
+
        return NULL;
 }
 
@@ -502,17 +573,15 @@ ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
 {
        struct inode *inode = dentry->d_inode;
        struct ceph_inode_info *ci = ceph_inode(inode);
-       struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
        int err;
        struct ceph_inode_xattr *xattr;
-       struct ceph_vxattr_cb *vxattr = NULL;
+       struct ceph_vxattr *vxattr = NULL;
 
        if (!ceph_is_valid_xattr(name))
                return -ENODATA;
 
        /* let's see if a virtual xattr was requested */
-       if (vxattrs)
-               vxattr = ceph_match_vxattr(vxattrs, name);
+       vxattr = ceph_match_vxattr(inode, name);
 
        spin_lock(&ci->i_ceph_lock);
        dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
@@ -568,7 +637,7 @@ ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
 {
        struct inode *inode = dentry->d_inode;
        struct ceph_inode_info *ci = ceph_inode(inode);
-       struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
+       struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
        u32 vir_namelen = 0;
        u32 namelen;
        int err;
@@ -596,11 +665,12 @@ ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
                goto out;
 
 list_xattr:
-       vir_namelen = 0;
-       /* include virtual dir xattrs */
-       if (vxattrs)
-               for (i = 0; vxattrs[i].name; i++)
-                       vir_namelen += strlen(vxattrs[i].name) + 1;
+       /*
+        * Start with virtual dir xattr names (if any) (including
+        * terminating '\0' characters for each).
+        */
+       vir_namelen = ceph_vxattrs_name_size(vxattrs);
+
        /* adding 1 byte per each variable due to the null termination */
        namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
        err = -ERANGE;
@@ -698,17 +768,17 @@ int ceph_setxattr(struct dentry *dentry, const char *name,
                  const void *value, size_t size, int flags)
 {
        struct inode *inode = dentry->d_inode;
+       struct ceph_vxattr *vxattr;
        struct ceph_inode_info *ci = ceph_inode(inode);
-       struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
+       int issued;
        int err;
+       int dirty;
        int name_len = strlen(name);
        int val_len = size;
        char *newname = NULL;
        char *newval = NULL;
        struct ceph_inode_xattr *xattr = NULL;
-       int issued;
        int required_blob_size;
-       int dirty;
 
        if (ceph_snap(inode) != CEPH_NOSNAP)
                return -EROFS;
@@ -716,12 +786,9 @@ int ceph_setxattr(struct dentry *dentry, const char *name,
        if (!ceph_is_valid_xattr(name))
                return -EOPNOTSUPP;
 
-       if (vxattrs) {
-               struct ceph_vxattr_cb *vxattr =
-                       ceph_match_vxattr(vxattrs, name);
-               if (vxattr && vxattr->readonly)
-                       return -EOPNOTSUPP;
-       }
+       vxattr = ceph_match_vxattr(inode, name);
+       if (vxattr && vxattr->readonly)
+               return -EOPNOTSUPP;
 
        /* preallocate memory for xattr name, value, index node */
        err = -ENOMEM;
@@ -730,11 +797,9 @@ int ceph_setxattr(struct dentry *dentry, const char *name,
                goto out;
 
        if (val_len) {
-               newval = kmalloc(val_len + 1, GFP_NOFS);
+               newval = kmemdup(value, val_len, GFP_NOFS);
                if (!newval)
                        goto out;
-               memcpy(newval, value, val_len);
-               newval[val_len] = '\0';
        }
 
        xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
@@ -744,6 +809,7 @@ int ceph_setxattr(struct dentry *dentry, const char *name,
        spin_lock(&ci->i_ceph_lock);
 retry:
        issued = __ceph_caps_issued(ci, NULL);
+       dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
        if (!(issued & CEPH_CAP_XATTR_EXCL))
                goto do_sync;
        __build_xattrs(inode);
@@ -752,7 +818,7 @@ retry:
 
        if (!ci->i_xattrs.prealloc_blob ||
            required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
-               struct ceph_buffer *blob = NULL;
+               struct ceph_buffer *blob;
 
                spin_unlock(&ci->i_ceph_lock);
                dout(" preaallocating new blob size=%d\n", required_blob_size);
@@ -766,12 +832,13 @@ retry:
                goto retry;
        }
 
-       dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
        err = __set_xattr(ci, newname, name_len, newval,
                          val_len, 1, 1, 1, &xattr);
+
        dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
        ci->i_xattrs.dirty = true;
        inode->i_ctime = CURRENT_TIME;
+
        spin_unlock(&ci->i_ceph_lock);
        if (dirty)
                __mark_inode_dirty(inode, dirty);
@@ -816,8 +883,8 @@ static int ceph_send_removexattr(struct dentry *dentry, const char *name)
 int ceph_removexattr(struct dentry *dentry, const char *name)
 {
        struct inode *inode = dentry->d_inode;
+       struct ceph_vxattr *vxattr;
        struct ceph_inode_info *ci = ceph_inode(inode);
-       struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
        int issued;
        int err;
        int required_blob_size;
@@ -829,22 +896,19 @@ int ceph_removexattr(struct dentry *dentry, const char *name)
        if (!ceph_is_valid_xattr(name))
                return -EOPNOTSUPP;
 
-       if (vxattrs) {
-               struct ceph_vxattr_cb *vxattr =
-                       ceph_match_vxattr(vxattrs, name);
-               if (vxattr && vxattr->readonly)
-                       return -EOPNOTSUPP;
-       }
+       vxattr = ceph_match_vxattr(inode, name);
+       if (vxattr && vxattr->readonly)
+               return -EOPNOTSUPP;
 
        err = -ENOMEM;
        spin_lock(&ci->i_ceph_lock);
-       __build_xattrs(inode);
 retry:
        issued = __ceph_caps_issued(ci, NULL);
        dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
 
        if (!(issued & CEPH_CAP_XATTR_EXCL))
                goto do_sync;
+       __build_xattrs(inode);
 
        required_blob_size = __get_required_blob_size(ci, 0, 0);
 
@@ -865,10 +929,10 @@ retry:
        }
 
        err = __remove_xattr_by_name(ceph_inode(inode), name);
+
        dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
        ci->i_xattrs.dirty = true;
        inode->i_ctime = CURRENT_TIME;
-
        spin_unlock(&ci->i_ceph_lock);
        if (dirty)
                __mark_inode_dirty(inode, dirty);