Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 3 | #include <linux/ceph/pagelist.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 5 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 6 | #include "mds_client.h" |
| 7 | |
| 8 | #include <linux/ceph/decode.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 9 | |
| 10 | #include <linux/xattr.h> |
Yan, Zheng | ac6713c | 2019-05-26 16:27:56 +0800 | [diff] [blame] | 11 | #include <linux/security.h> |
Linus Torvalds | 4db658e | 2014-01-28 18:06:18 -0800 | [diff] [blame] | 12 | #include <linux/posix_acl_xattr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 14 | |
Alex Elder | 2289190 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 15 | #define XATTR_CEPH_PREFIX "ceph." |
| 16 | #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1) |
| 17 | |
Yan, Zheng | bcdfeb2 | 2014-02-11 13:04:19 +0800 | [diff] [blame] | 18 | static int __remove_xattr(struct ceph_inode_info *ci, |
| 19 | struct ceph_inode_xattr *xattr); |
| 20 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 21 | static bool ceph_is_valid_xattr(const char *name) |
| 22 | { |
Alex Elder | 2289190 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 23 | return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) || |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 24 | !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 25 | !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | * These define virtual xattrs exposing the recursive directory |
| 30 | * statistics and layout metadata. |
| 31 | */ |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 32 | struct ceph_vxattr { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 33 | char *name; |
Alex Elder | 3ce6cd1 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 34 | size_t name_size; /* strlen(name) + 1 (for '\0') */ |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 35 | ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, |
| 36 | size_t size); |
Sage Weil | f36e447 | 2013-01-20 21:59:29 -0800 | [diff] [blame] | 37 | bool (*exists_cb)(struct ceph_inode_info *ci); |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 38 | unsigned int flags; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 41 | #define VXATTR_FLAG_READONLY (1<<0) |
| 42 | #define VXATTR_FLAG_HIDDEN (1<<1) |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 43 | #define VXATTR_FLAG_RSTAT (1<<2) |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 44 | |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 45 | /* layouts */ |
| 46 | |
| 47 | static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci) |
| 48 | { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 49 | struct ceph_file_layout *fl = &ci->i_layout; |
| 50 | return (fl->stripe_unit > 0 || fl->stripe_count > 0 || |
| 51 | fl->object_size > 0 || fl->pool_id >= 0 || |
| 52 | rcu_dereference_raw(fl->pool_ns) != NULL); |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 55 | static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val, |
| 56 | size_t size) |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 57 | { |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 58 | struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); |
| 59 | struct ceph_osd_client *osdc = &fsc->client->osdc; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 60 | struct ceph_string *pool_ns; |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 61 | s64 pool = ci->i_layout.pool_id; |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 62 | const char *pool_name; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 63 | const char *ns_field = " pool_namespace="; |
Yan, Zheng | 1e5c664 | 2014-03-24 13:00:54 +0800 | [diff] [blame] | 64 | char buf[128]; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 65 | size_t len, total_len = 0; |
Jeff Layton | 3b42101 | 2019-06-13 15:17:00 -0400 | [diff] [blame] | 66 | ssize_t ret; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 67 | |
| 68 | pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 69 | |
| 70 | dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode); |
Ilya Dryomov | 5aea3dc | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 71 | down_read(&osdc->lock); |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 72 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); |
Yan, Zheng | 1e5c664 | 2014-03-24 13:00:54 +0800 | [diff] [blame] | 73 | if (pool_name) { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 74 | len = snprintf(buf, sizeof(buf), |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 75 | "stripe_unit=%u stripe_count=%u object_size=%u pool=", |
| 76 | ci->i_layout.stripe_unit, ci->i_layout.stripe_count, |
| 77 | ci->i_layout.object_size); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 78 | total_len = len + strlen(pool_name); |
Yan, Zheng | 1e5c664 | 2014-03-24 13:00:54 +0800 | [diff] [blame] | 79 | } else { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 80 | len = snprintf(buf, sizeof(buf), |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 81 | "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld", |
| 82 | ci->i_layout.stripe_unit, ci->i_layout.stripe_count, |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 83 | ci->i_layout.object_size, pool); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 84 | total_len = len; |
| 85 | } |
| 86 | |
| 87 | if (pool_ns) |
| 88 | total_len += strlen(ns_field) + pool_ns->len; |
| 89 | |
Jeff Layton | 3b42101 | 2019-06-13 15:17:00 -0400 | [diff] [blame] | 90 | ret = total_len; |
| 91 | if (size >= total_len) { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 92 | memcpy(val, buf, len); |
| 93 | ret = len; |
| 94 | if (pool_name) { |
| 95 | len = strlen(pool_name); |
| 96 | memcpy(val + ret, pool_name, len); |
| 97 | ret += len; |
| 98 | } |
| 99 | if (pool_ns) { |
| 100 | len = strlen(ns_field); |
| 101 | memcpy(val + ret, ns_field, len); |
| 102 | ret += len; |
| 103 | memcpy(val + ret, pool_ns->str, pool_ns->len); |
| 104 | ret += pool_ns->len; |
Yan, Zheng | 1e5c664 | 2014-03-24 13:00:54 +0800 | [diff] [blame] | 105 | } |
| 106 | } |
Ilya Dryomov | 5aea3dc | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 107 | up_read(&osdc->lock); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 108 | ceph_put_string(pool_ns); |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 112 | /* |
| 113 | * The convention with strings in xattrs is that they should not be NULL |
| 114 | * terminated, since we're returning the length with them. snprintf always |
| 115 | * NULL terminates however, so call it on a temporary buffer and then memcpy |
| 116 | * the result into place. |
| 117 | */ |
| 118 | static int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...) |
| 119 | { |
| 120 | int ret; |
| 121 | va_list args; |
| 122 | char buf[96]; /* NB: reevaluate size if new vxattrs are added */ |
| 123 | |
| 124 | va_start(args, fmt); |
| 125 | ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args); |
| 126 | va_end(args); |
| 127 | |
| 128 | /* Sanity check */ |
| 129 | if (size && ret + 1 > sizeof(buf)) { |
| 130 | WARN_ONCE(true, "Returned length too big (%d)", ret); |
| 131 | return -E2BIG; |
| 132 | } |
| 133 | |
| 134 | if (ret <= size) |
| 135 | memcpy(val, buf, ret); |
| 136 | return ret; |
| 137 | } |
| 138 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 139 | static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci, |
| 140 | char *val, size_t size) |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 141 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 142 | return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit); |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 145 | static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci, |
| 146 | char *val, size_t size) |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 147 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 148 | return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count); |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 151 | static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci, |
| 152 | char *val, size_t size) |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 153 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 154 | return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size); |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 157 | static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci, |
| 158 | char *val, size_t size) |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 159 | { |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 160 | ssize_t ret; |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 161 | struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); |
| 162 | struct ceph_osd_client *osdc = &fsc->client->osdc; |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 163 | s64 pool = ci->i_layout.pool_id; |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 164 | const char *pool_name; |
| 165 | |
Ilya Dryomov | 5aea3dc | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 166 | down_read(&osdc->lock); |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 167 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 168 | if (pool_name) { |
| 169 | ret = strlen(pool_name); |
| 170 | if (ret <= size) |
| 171 | memcpy(val, pool_name, ret); |
| 172 | } else { |
| 173 | ret = ceph_fmt_xattr(val, size, "%lld", pool); |
| 174 | } |
Ilya Dryomov | 5aea3dc | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 175 | up_read(&osdc->lock); |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 176 | return ret; |
| 177 | } |
| 178 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 179 | static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci, |
| 180 | char *val, size_t size) |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 181 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 182 | ssize_t ret = 0; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 183 | struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns); |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 184 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 185 | if (ns) { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 186 | ret = ns->len; |
| 187 | if (ret <= size) |
| 188 | memcpy(val, ns->str, ret); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 189 | ceph_put_string(ns); |
| 190 | } |
| 191 | return ret; |
| 192 | } |
| 193 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 194 | /* directories */ |
| 195 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 196 | static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val, |
| 197 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 198 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 199 | return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 202 | static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val, |
| 203 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 204 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 205 | return ceph_fmt_xattr(val, size, "%lld", ci->i_files); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 208 | static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val, |
| 209 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 210 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 211 | return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 214 | static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val, |
| 215 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 216 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 217 | return ceph_fmt_xattr(val, size, "%lld", |
| 218 | ci->i_rfiles + ci->i_rsubdirs); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 221 | static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val, |
| 222 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 223 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 224 | return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 227 | static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val, |
| 228 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 229 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 230 | return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 233 | static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, |
| 234 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 235 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 236 | return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 239 | static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, |
| 240 | size_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 241 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 242 | return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec, |
| 243 | ci->i_rctime.tv_nsec); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 246 | /* dir pin */ |
| 247 | static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci) |
| 248 | { |
| 249 | return ci->i_dir_pin != -ENODATA; |
| 250 | } |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 251 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 252 | static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val, |
| 253 | size_t size) |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 254 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 255 | return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin); |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* quotas */ |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 259 | static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci) |
| 260 | { |
Yan, Zheng | f191982 | 2018-04-08 09:54:39 +0800 | [diff] [blame] | 261 | bool ret = false; |
| 262 | spin_lock(&ci->i_ceph_lock); |
| 263 | if ((ci->i_max_files || ci->i_max_bytes) && |
| 264 | ci->i_vino.snap == CEPH_NOSNAP && |
| 265 | ci->i_snap_realm && |
| 266 | ci->i_snap_realm->ino == ci->i_vino.ino) |
| 267 | ret = true; |
| 268 | spin_unlock(&ci->i_ceph_lock); |
| 269 | return ret; |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 272 | static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val, |
| 273 | size_t size) |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 274 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 275 | return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu", |
| 276 | ci->i_max_bytes, ci->i_max_files); |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 279 | static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci, |
| 280 | char *val, size_t size) |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 281 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 282 | return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes); |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 285 | static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci, |
| 286 | char *val, size_t size) |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 287 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 288 | return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files); |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 289 | } |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 290 | |
David Disseldorp | 100cc61 | 2019-04-18 14:15:47 +0200 | [diff] [blame] | 291 | /* snapshots */ |
| 292 | static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci) |
| 293 | { |
| 294 | return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0); |
| 295 | } |
| 296 | |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 297 | static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val, |
| 298 | size_t size) |
David Disseldorp | 100cc61 | 2019-04-18 14:15:47 +0200 | [diff] [blame] | 299 | { |
Jeff Layton | 2635053 | 2019-06-24 07:32:21 -0400 | [diff] [blame] | 300 | return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec, |
| 301 | ci->i_snap_btime.tv_nsec); |
David Disseldorp | 100cc61 | 2019-04-18 14:15:47 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Alex Elder | eb78808 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 304 | #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 305 | #define CEPH_XATTR_NAME2(_type, _name, _name2) \ |
| 306 | XATTR_CEPH_PREFIX #_type "." #_name "." #_name2 |
Alex Elder | eb78808 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 307 | |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 308 | #define XATTR_NAME_CEPH(_type, _name, _flags) \ |
Sage Weil | 8860147 | 2013-01-31 11:53:27 -0800 | [diff] [blame] | 309 | { \ |
| 310 | .name = CEPH_XATTR_NAME(_type, _name), \ |
| 311 | .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ |
| 312 | .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 313 | .exists_cb = NULL, \ |
| 314 | .flags = (VXATTR_FLAG_READONLY | _flags), \ |
Sage Weil | 8860147 | 2013-01-31 11:53:27 -0800 | [diff] [blame] | 315 | } |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 316 | #define XATTR_RSTAT_FIELD(_type, _name) \ |
| 317 | XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT) |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 318 | #define XATTR_LAYOUT_FIELD(_type, _name, _field) \ |
| 319 | { \ |
| 320 | .name = CEPH_XATTR_NAME2(_type, _name, _field), \ |
| 321 | .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \ |
| 322 | .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \ |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 323 | .exists_cb = ceph_vxattrcb_layout_exists, \ |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 324 | .flags = VXATTR_FLAG_HIDDEN, \ |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 325 | } |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 326 | #define XATTR_QUOTA_FIELD(_type, _name) \ |
| 327 | { \ |
| 328 | .name = CEPH_XATTR_NAME(_type, _name), \ |
| 329 | .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \ |
| 330 | .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 331 | .exists_cb = ceph_vxattrcb_quota_exists, \ |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 332 | .flags = VXATTR_FLAG_HIDDEN, \ |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 333 | } |
Alex Elder | eb78808 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 334 | |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 335 | static struct ceph_vxattr ceph_dir_vxattrs[] = { |
Sage Weil | 1f08f2b | 2013-01-20 22:07:12 -0800 | [diff] [blame] | 336 | { |
| 337 | .name = "ceph.dir.layout", |
| 338 | .name_size = sizeof("ceph.dir.layout"), |
| 339 | .getxattr_cb = ceph_vxattrcb_layout, |
Sage Weil | 1f08f2b | 2013-01-20 22:07:12 -0800 | [diff] [blame] | 340 | .exists_cb = ceph_vxattrcb_layout_exists, |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 341 | .flags = VXATTR_FLAG_HIDDEN, |
Sage Weil | 1f08f2b | 2013-01-20 22:07:12 -0800 | [diff] [blame] | 342 | }, |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 343 | XATTR_LAYOUT_FIELD(dir, layout, stripe_unit), |
| 344 | XATTR_LAYOUT_FIELD(dir, layout, stripe_count), |
| 345 | XATTR_LAYOUT_FIELD(dir, layout, object_size), |
| 346 | XATTR_LAYOUT_FIELD(dir, layout, pool), |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 347 | XATTR_LAYOUT_FIELD(dir, layout, pool_namespace), |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 348 | XATTR_NAME_CEPH(dir, entries, 0), |
| 349 | XATTR_NAME_CEPH(dir, files, 0), |
| 350 | XATTR_NAME_CEPH(dir, subdirs, 0), |
| 351 | XATTR_RSTAT_FIELD(dir, rentries), |
| 352 | XATTR_RSTAT_FIELD(dir, rfiles), |
| 353 | XATTR_RSTAT_FIELD(dir, rsubdirs), |
| 354 | XATTR_RSTAT_FIELD(dir, rbytes), |
| 355 | XATTR_RSTAT_FIELD(dir, rctime), |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 356 | { |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 357 | .name = "ceph.dir.pin", |
David Disseldorp | e1b8143 | 2019-04-18 14:15:45 +0200 | [diff] [blame] | 358 | .name_size = sizeof("ceph.dir.pin"), |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 359 | .getxattr_cb = ceph_vxattrcb_dir_pin, |
| 360 | .exists_cb = ceph_vxattrcb_dir_pin_exists, |
| 361 | .flags = VXATTR_FLAG_HIDDEN, |
| 362 | }, |
| 363 | { |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 364 | .name = "ceph.quota", |
| 365 | .name_size = sizeof("ceph.quota"), |
| 366 | .getxattr_cb = ceph_vxattrcb_quota, |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 367 | .exists_cb = ceph_vxattrcb_quota_exists, |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 368 | .flags = VXATTR_FLAG_HIDDEN, |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 369 | }, |
| 370 | XATTR_QUOTA_FIELD(quota, max_bytes), |
| 371 | XATTR_QUOTA_FIELD(quota, max_files), |
David Disseldorp | 100cc61 | 2019-04-18 14:15:47 +0200 | [diff] [blame] | 372 | { |
| 373 | .name = "ceph.snap.btime", |
| 374 | .name_size = sizeof("ceph.snap.btime"), |
| 375 | .getxattr_cb = ceph_vxattrcb_snap_btime, |
| 376 | .exists_cb = ceph_vxattrcb_snap_btime_exists, |
| 377 | .flags = VXATTR_FLAG_READONLY, |
| 378 | }, |
Alex Elder | 2c3dd4f | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 379 | { .name = NULL, 0 } /* Required table terminator */ |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | /* files */ |
| 383 | |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 384 | static struct ceph_vxattr ceph_file_vxattrs[] = { |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 385 | { |
| 386 | .name = "ceph.file.layout", |
| 387 | .name_size = sizeof("ceph.file.layout"), |
| 388 | .getxattr_cb = ceph_vxattrcb_layout, |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 389 | .exists_cb = ceph_vxattrcb_layout_exists, |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 390 | .flags = VXATTR_FLAG_HIDDEN, |
Sage Weil | 32ab0bd | 2013-01-19 16:46:32 -0800 | [diff] [blame] | 391 | }, |
Sage Weil | 695b711 | 2013-01-20 22:08:33 -0800 | [diff] [blame] | 392 | XATTR_LAYOUT_FIELD(file, layout, stripe_unit), |
| 393 | XATTR_LAYOUT_FIELD(file, layout, stripe_count), |
| 394 | XATTR_LAYOUT_FIELD(file, layout, object_size), |
| 395 | XATTR_LAYOUT_FIELD(file, layout, pool), |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 396 | XATTR_LAYOUT_FIELD(file, layout, pool_namespace), |
David Disseldorp | 100cc61 | 2019-04-18 14:15:47 +0200 | [diff] [blame] | 397 | { |
| 398 | .name = "ceph.snap.btime", |
| 399 | .name_size = sizeof("ceph.snap.btime"), |
| 400 | .getxattr_cb = ceph_vxattrcb_snap_btime, |
| 401 | .exists_cb = ceph_vxattrcb_snap_btime_exists, |
| 402 | .flags = VXATTR_FLAG_READONLY, |
| 403 | }, |
Alex Elder | 2c3dd4f | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 404 | { .name = NULL, 0 } /* Required table terminator */ |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 407 | static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 408 | { |
| 409 | if (S_ISDIR(inode->i_mode)) |
| 410 | return ceph_dir_vxattrs; |
| 411 | else if (S_ISREG(inode->i_mode)) |
| 412 | return ceph_file_vxattrs; |
| 413 | return NULL; |
| 414 | } |
| 415 | |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 416 | static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 417 | const char *name) |
| 418 | { |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 419 | struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); |
Alex Elder | 06476a6 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 420 | |
| 421 | if (vxattr) { |
| 422 | while (vxattr->name) { |
| 423 | if (!strcmp(vxattr->name, name)) |
| 424 | return vxattr; |
| 425 | vxattr++; |
| 426 | } |
| 427 | } |
| 428 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | static int __set_xattr(struct ceph_inode_info *ci, |
| 433 | const char *name, int name_len, |
| 434 | const char *val, int val_len, |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 435 | int flags, int update_xattr, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 436 | struct ceph_inode_xattr **newxattr) |
| 437 | { |
| 438 | struct rb_node **p; |
| 439 | struct rb_node *parent = NULL; |
| 440 | struct ceph_inode_xattr *xattr = NULL; |
| 441 | int c; |
| 442 | int new = 0; |
| 443 | |
| 444 | p = &ci->i_xattrs.index.rb_node; |
| 445 | while (*p) { |
| 446 | parent = *p; |
| 447 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); |
| 448 | c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); |
| 449 | if (c < 0) |
| 450 | p = &(*p)->rb_left; |
| 451 | else if (c > 0) |
| 452 | p = &(*p)->rb_right; |
| 453 | else { |
| 454 | if (name_len == xattr->name_len) |
| 455 | break; |
| 456 | else if (name_len < xattr->name_len) |
| 457 | p = &(*p)->rb_left; |
| 458 | else |
| 459 | p = &(*p)->rb_right; |
| 460 | } |
| 461 | xattr = NULL; |
| 462 | } |
| 463 | |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 464 | if (update_xattr) { |
| 465 | int err = 0; |
Luis Henriques | eeca958 | 2017-04-28 11:14:04 +0100 | [diff] [blame] | 466 | |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 467 | if (xattr && (flags & XATTR_CREATE)) |
| 468 | err = -EEXIST; |
| 469 | else if (!xattr && (flags & XATTR_REPLACE)) |
| 470 | err = -ENODATA; |
| 471 | if (err) { |
| 472 | kfree(name); |
| 473 | kfree(val); |
Luis Henriques | eeca958 | 2017-04-28 11:14:04 +0100 | [diff] [blame] | 474 | kfree(*newxattr); |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 475 | return err; |
| 476 | } |
Yan, Zheng | bcdfeb2 | 2014-02-11 13:04:19 +0800 | [diff] [blame] | 477 | if (update_xattr < 0) { |
| 478 | if (xattr) |
| 479 | __remove_xattr(ci, xattr); |
| 480 | kfree(name); |
Luis Henriques | eeca958 | 2017-04-28 11:14:04 +0100 | [diff] [blame] | 481 | kfree(*newxattr); |
Yan, Zheng | bcdfeb2 | 2014-02-11 13:04:19 +0800 | [diff] [blame] | 482 | return 0; |
| 483 | } |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 484 | } |
| 485 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 486 | if (!xattr) { |
| 487 | new = 1; |
| 488 | xattr = *newxattr; |
| 489 | xattr->name = name; |
| 490 | xattr->name_len = name_len; |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 491 | xattr->should_free_name = update_xattr; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 492 | |
| 493 | ci->i_xattrs.count++; |
| 494 | dout("__set_xattr count=%d\n", ci->i_xattrs.count); |
| 495 | } else { |
| 496 | kfree(*newxattr); |
| 497 | *newxattr = NULL; |
| 498 | if (xattr->should_free_val) |
| 499 | kfree((void *)xattr->val); |
| 500 | |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 501 | if (update_xattr) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 502 | kfree((void *)name); |
| 503 | name = xattr->name; |
| 504 | } |
| 505 | ci->i_xattrs.names_size -= xattr->name_len; |
| 506 | ci->i_xattrs.vals_size -= xattr->val_len; |
| 507 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 508 | ci->i_xattrs.names_size += name_len; |
| 509 | ci->i_xattrs.vals_size += val_len; |
| 510 | if (val) |
| 511 | xattr->val = val; |
| 512 | else |
| 513 | xattr->val = ""; |
| 514 | |
| 515 | xattr->val_len = val_len; |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 516 | xattr->dirty = update_xattr; |
| 517 | xattr->should_free_val = (val && update_xattr); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 518 | |
| 519 | if (new) { |
| 520 | rb_link_node(&xattr->node, parent, p); |
| 521 | rb_insert_color(&xattr->node, &ci->i_xattrs.index); |
| 522 | dout("__set_xattr_val p=%p\n", p); |
| 523 | } |
| 524 | |
Yan, Zheng | 0572978 | 2019-05-27 16:15:41 +0800 | [diff] [blame] | 525 | dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n", |
| 526 | ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, |
| 532 | const char *name) |
| 533 | { |
| 534 | struct rb_node **p; |
| 535 | struct rb_node *parent = NULL; |
| 536 | struct ceph_inode_xattr *xattr = NULL; |
Sage Weil | 17db143 | 2011-01-13 15:27:29 -0800 | [diff] [blame] | 537 | int name_len = strlen(name); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 538 | int c; |
| 539 | |
| 540 | p = &ci->i_xattrs.index.rb_node; |
| 541 | while (*p) { |
| 542 | parent = *p; |
| 543 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); |
| 544 | c = strncmp(name, xattr->name, xattr->name_len); |
Sage Weil | 17db143 | 2011-01-13 15:27:29 -0800 | [diff] [blame] | 545 | if (c == 0 && name_len > xattr->name_len) |
| 546 | c = 1; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 547 | if (c < 0) |
| 548 | p = &(*p)->rb_left; |
| 549 | else if (c > 0) |
| 550 | p = &(*p)->rb_right; |
| 551 | else { |
| 552 | dout("__get_xattr %s: found %.*s\n", name, |
| 553 | xattr->val_len, xattr->val); |
| 554 | return xattr; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | dout("__get_xattr %s: not found\n", name); |
| 559 | |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | static void __free_xattr(struct ceph_inode_xattr *xattr) |
| 564 | { |
| 565 | BUG_ON(!xattr); |
| 566 | |
| 567 | if (xattr->should_free_name) |
| 568 | kfree((void *)xattr->name); |
| 569 | if (xattr->should_free_val) |
| 570 | kfree((void *)xattr->val); |
| 571 | |
| 572 | kfree(xattr); |
| 573 | } |
| 574 | |
| 575 | static int __remove_xattr(struct ceph_inode_info *ci, |
| 576 | struct ceph_inode_xattr *xattr) |
| 577 | { |
| 578 | if (!xattr) |
Yan, Zheng | 524186a | 2014-02-11 13:23:09 +0800 | [diff] [blame] | 579 | return -ENODATA; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 580 | |
| 581 | rb_erase(&xattr->node, &ci->i_xattrs.index); |
| 582 | |
| 583 | if (xattr->should_free_name) |
| 584 | kfree((void *)xattr->name); |
| 585 | if (xattr->should_free_val) |
| 586 | kfree((void *)xattr->val); |
| 587 | |
| 588 | ci->i_xattrs.names_size -= xattr->name_len; |
| 589 | ci->i_xattrs.vals_size -= xattr->val_len; |
| 590 | ci->i_xattrs.count--; |
| 591 | kfree(xattr); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 596 | static char *__copy_xattr_names(struct ceph_inode_info *ci, |
| 597 | char *dest) |
| 598 | { |
| 599 | struct rb_node *p; |
| 600 | struct ceph_inode_xattr *xattr = NULL; |
| 601 | |
| 602 | p = rb_first(&ci->i_xattrs.index); |
| 603 | dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); |
| 604 | |
| 605 | while (p) { |
| 606 | xattr = rb_entry(p, struct ceph_inode_xattr, node); |
| 607 | memcpy(dest, xattr->name, xattr->name_len); |
| 608 | dest[xattr->name_len] = '\0'; |
| 609 | |
| 610 | dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, |
| 611 | xattr->name_len, ci->i_xattrs.names_size); |
| 612 | |
| 613 | dest += xattr->name_len + 1; |
| 614 | p = rb_next(p); |
| 615 | } |
| 616 | |
| 617 | return dest; |
| 618 | } |
| 619 | |
| 620 | void __ceph_destroy_xattrs(struct ceph_inode_info *ci) |
| 621 | { |
| 622 | struct rb_node *p, *tmp; |
| 623 | struct ceph_inode_xattr *xattr = NULL; |
| 624 | |
| 625 | p = rb_first(&ci->i_xattrs.index); |
| 626 | |
| 627 | dout("__ceph_destroy_xattrs p=%p\n", p); |
| 628 | |
| 629 | while (p) { |
| 630 | xattr = rb_entry(p, struct ceph_inode_xattr, node); |
| 631 | tmp = p; |
| 632 | p = rb_next(tmp); |
| 633 | dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, |
| 634 | xattr->name_len, xattr->name); |
| 635 | rb_erase(tmp, &ci->i_xattrs.index); |
| 636 | |
| 637 | __free_xattr(xattr); |
| 638 | } |
| 639 | |
| 640 | ci->i_xattrs.names_size = 0; |
| 641 | ci->i_xattrs.vals_size = 0; |
| 642 | ci->i_xattrs.index_version = 0; |
| 643 | ci->i_xattrs.count = 0; |
| 644 | ci->i_xattrs.index = RB_ROOT; |
| 645 | } |
| 646 | |
| 647 | static int __build_xattrs(struct inode *inode) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 648 | __releases(ci->i_ceph_lock) |
| 649 | __acquires(ci->i_ceph_lock) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 650 | { |
| 651 | u32 namelen; |
| 652 | u32 numattr = 0; |
| 653 | void *p, *end; |
| 654 | u32 len; |
| 655 | const char *name, *val; |
| 656 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 657 | int xattr_version; |
| 658 | struct ceph_inode_xattr **xattrs = NULL; |
Sage Weil | 63ff78b | 2009-11-01 17:51:15 -0800 | [diff] [blame] | 659 | int err = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 660 | int i; |
| 661 | |
| 662 | dout("__build_xattrs() len=%d\n", |
| 663 | ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); |
| 664 | |
| 665 | if (ci->i_xattrs.index_version >= ci->i_xattrs.version) |
| 666 | return 0; /* already built */ |
| 667 | |
| 668 | __ceph_destroy_xattrs(ci); |
| 669 | |
| 670 | start: |
| 671 | /* updated internal xattr rb tree */ |
| 672 | if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { |
| 673 | p = ci->i_xattrs.blob->vec.iov_base; |
| 674 | end = p + ci->i_xattrs.blob->vec.iov_len; |
| 675 | ceph_decode_32_safe(&p, end, numattr, bad); |
| 676 | xattr_version = ci->i_xattrs.version; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 677 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 678 | |
Ilya Dryomov | 7e8a295 | 2014-07-25 11:47:21 +0400 | [diff] [blame] | 679 | xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *), |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 680 | GFP_NOFS); |
| 681 | err = -ENOMEM; |
| 682 | if (!xattrs) |
| 683 | goto bad_lock; |
Ilya Dryomov | 1a295bd | 2014-07-25 12:44:58 +0400 | [diff] [blame] | 684 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 685 | for (i = 0; i < numattr; i++) { |
| 686 | xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), |
| 687 | GFP_NOFS); |
| 688 | if (!xattrs[i]) |
| 689 | goto bad_lock; |
| 690 | } |
| 691 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 692 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 693 | if (ci->i_xattrs.version != xattr_version) { |
| 694 | /* lost a race, retry */ |
| 695 | for (i = 0; i < numattr; i++) |
| 696 | kfree(xattrs[i]); |
| 697 | kfree(xattrs); |
Alan Cox | 21ec6ff | 2012-07-20 08:18:36 -0500 | [diff] [blame] | 698 | xattrs = NULL; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 699 | goto start; |
| 700 | } |
| 701 | err = -EIO; |
| 702 | while (numattr--) { |
| 703 | ceph_decode_32_safe(&p, end, len, bad); |
| 704 | namelen = len; |
| 705 | name = p; |
| 706 | p += len; |
| 707 | ceph_decode_32_safe(&p, end, len, bad); |
| 708 | val = p; |
| 709 | p += len; |
| 710 | |
| 711 | err = __set_xattr(ci, name, namelen, val, len, |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 712 | 0, 0, &xattrs[numattr]); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 713 | |
| 714 | if (err < 0) |
| 715 | goto bad; |
| 716 | } |
| 717 | kfree(xattrs); |
| 718 | } |
| 719 | ci->i_xattrs.index_version = ci->i_xattrs.version; |
| 720 | ci->i_xattrs.dirty = false; |
| 721 | |
| 722 | return err; |
| 723 | bad_lock: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 724 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 725 | bad: |
| 726 | if (xattrs) { |
| 727 | for (i = 0; i < numattr; i++) |
| 728 | kfree(xattrs[i]); |
| 729 | kfree(xattrs); |
| 730 | } |
| 731 | ci->i_xattrs.names_size = 0; |
| 732 | return err; |
| 733 | } |
| 734 | |
| 735 | static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, |
| 736 | int val_size) |
| 737 | { |
| 738 | /* |
| 739 | * 4 bytes for the length, and additional 4 bytes per each xattr name, |
| 740 | * 4 bytes per each value |
| 741 | */ |
| 742 | int size = 4 + ci->i_xattrs.count*(4 + 4) + |
| 743 | ci->i_xattrs.names_size + |
| 744 | ci->i_xattrs.vals_size; |
| 745 | dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", |
| 746 | ci->i_xattrs.count, ci->i_xattrs.names_size, |
| 747 | ci->i_xattrs.vals_size); |
| 748 | |
| 749 | if (name_size) |
| 750 | size += 4 + 4 + name_size + val_size; |
| 751 | |
| 752 | return size; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * If there are dirty xattrs, reencode xattrs into the prealloc_blob |
| 757 | * and swap into place. |
| 758 | */ |
| 759 | void __ceph_build_xattrs_blob(struct ceph_inode_info *ci) |
| 760 | { |
| 761 | struct rb_node *p; |
| 762 | struct ceph_inode_xattr *xattr = NULL; |
| 763 | void *dest; |
| 764 | |
| 765 | dout("__build_xattrs_blob %p\n", &ci->vfs_inode); |
| 766 | if (ci->i_xattrs.dirty) { |
| 767 | int need = __get_required_blob_size(ci, 0, 0); |
| 768 | |
| 769 | BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); |
| 770 | |
| 771 | p = rb_first(&ci->i_xattrs.index); |
| 772 | dest = ci->i_xattrs.prealloc_blob->vec.iov_base; |
| 773 | |
| 774 | ceph_encode_32(&dest, ci->i_xattrs.count); |
| 775 | while (p) { |
| 776 | xattr = rb_entry(p, struct ceph_inode_xattr, node); |
| 777 | |
| 778 | ceph_encode_32(&dest, xattr->name_len); |
| 779 | memcpy(dest, xattr->name, xattr->name_len); |
| 780 | dest += xattr->name_len; |
| 781 | ceph_encode_32(&dest, xattr->val_len); |
| 782 | memcpy(dest, xattr->val, xattr->val_len); |
| 783 | dest += xattr->val_len; |
| 784 | |
| 785 | p = rb_next(p); |
| 786 | } |
| 787 | |
| 788 | /* adjust buffer len; it may be larger than we need */ |
| 789 | ci->i_xattrs.prealloc_blob->vec.iov_len = |
| 790 | dest - ci->i_xattrs.prealloc_blob->vec.iov_base; |
| 791 | |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 792 | if (ci->i_xattrs.blob) |
| 793 | ceph_buffer_put(ci->i_xattrs.blob); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 794 | ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; |
| 795 | ci->i_xattrs.prealloc_blob = NULL; |
| 796 | ci->i_xattrs.dirty = false; |
Sage Weil | 4a625be | 2010-08-22 15:03:56 -0700 | [diff] [blame] | 797 | ci->i_xattrs.version++; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 801 | static inline int __get_request_mask(struct inode *in) { |
| 802 | struct ceph_mds_request *req = current->journal_info; |
| 803 | int mask = 0; |
| 804 | if (req && req->r_target_inode == in) { |
| 805 | if (req->r_op == CEPH_MDS_OP_LOOKUP || |
| 806 | req->r_op == CEPH_MDS_OP_LOOKUPINO || |
| 807 | req->r_op == CEPH_MDS_OP_LOOKUPPARENT || |
| 808 | req->r_op == CEPH_MDS_OP_GETATTR) { |
| 809 | mask = le32_to_cpu(req->r_args.getattr.mask); |
| 810 | } else if (req->r_op == CEPH_MDS_OP_OPEN || |
| 811 | req->r_op == CEPH_MDS_OP_CREATE) { |
| 812 | mask = le32_to_cpu(req->r_args.open.mask); |
| 813 | } |
| 814 | } |
| 815 | return mask; |
| 816 | } |
| 817 | |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 818 | ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 819 | size_t size) |
| 820 | { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 821 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 822 | struct ceph_inode_xattr *xattr; |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 823 | struct ceph_vxattr *vxattr = NULL; |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 824 | int req_mask; |
Jeff Layton | f1d1b51 | 2019-06-24 07:39:18 -0400 | [diff] [blame] | 825 | ssize_t err; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 826 | |
Sage Weil | 0bee82f | 2013-01-20 22:00:58 -0800 | [diff] [blame] | 827 | /* let's see if a virtual xattr was requested */ |
| 828 | vxattr = ceph_match_vxattr(inode, name); |
Yan, Zheng | 29dccfa | 2016-03-12 13:32:16 +0800 | [diff] [blame] | 829 | if (vxattr) { |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 830 | int mask = 0; |
| 831 | if (vxattr->flags & VXATTR_FLAG_RSTAT) |
| 832 | mask |= CEPH_STAT_RSTAT; |
| 833 | err = ceph_do_getattr(inode, mask, true); |
Yan, Zheng | 1684dd0 | 2017-06-14 15:54:56 +0800 | [diff] [blame] | 834 | if (err) |
| 835 | return err; |
Yan, Zheng | 29dccfa | 2016-03-12 13:32:16 +0800 | [diff] [blame] | 836 | err = -ENODATA; |
Jeff Layton | 3b42101 | 2019-06-13 15:17:00 -0400 | [diff] [blame] | 837 | if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) { |
Yan, Zheng | 29dccfa | 2016-03-12 13:32:16 +0800 | [diff] [blame] | 838 | err = vxattr->getxattr_cb(ci, value, size); |
Jeff Layton | 3b42101 | 2019-06-13 15:17:00 -0400 | [diff] [blame] | 839 | if (size && size < err) |
| 840 | err = -ERANGE; |
| 841 | } |
majianpeng | a1dc193 | 2013-06-19 14:58:10 +0800 | [diff] [blame] | 842 | return err; |
Sage Weil | 0bee82f | 2013-01-20 22:00:58 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 845 | req_mask = __get_request_mask(inode); |
| 846 | |
majianpeng | a1dc193 | 2013-06-19 14:58:10 +0800 | [diff] [blame] | 847 | spin_lock(&ci->i_ceph_lock); |
| 848 | dout("getxattr %p ver=%lld index_ver=%lld\n", inode, |
| 849 | ci->i_xattrs.version, ci->i_xattrs.index_version); |
| 850 | |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 851 | if (ci->i_xattrs.version == 0 || |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 852 | !((req_mask & CEPH_CAP_XATTR_SHARED) || |
| 853 | __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 854 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 855 | |
| 856 | /* security module gets xattr while filling trace */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 857 | if (current->journal_info) { |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 858 | pr_warn_ratelimited("sync getxattr %p " |
| 859 | "during filling trace\n", inode); |
| 860 | return -EBUSY; |
| 861 | } |
| 862 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 863 | /* get xattrs from mds (if we don't already have them) */ |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 864 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 865 | if (err) |
| 866 | return err; |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 867 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 870 | err = __build_xattrs(inode); |
| 871 | if (err < 0) |
| 872 | goto out; |
| 873 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 874 | err = -ENODATA; /* == ENOATTR */ |
| 875 | xattr = __get_xattr(ci, name); |
Sage Weil | 0bee82f | 2013-01-20 22:00:58 -0800 | [diff] [blame] | 876 | if (!xattr) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 877 | goto out; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 878 | |
| 879 | err = -ERANGE; |
| 880 | if (size && size < xattr->val_len) |
| 881 | goto out; |
| 882 | |
| 883 | err = xattr->val_len; |
| 884 | if (size == 0) |
| 885 | goto out; |
| 886 | |
| 887 | memcpy(value, xattr->val, xattr->val_len); |
| 888 | |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 889 | if (current->journal_info && |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 890 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 891 | ci->i_ceph_flags |= CEPH_I_SEC_INITED; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 892 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 893 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 894 | return err; |
| 895 | } |
| 896 | |
| 897 | ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) |
| 898 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 899 | struct inode *inode = d_inode(dentry); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 900 | struct ceph_inode_info *ci = ceph_inode(inode); |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 901 | struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode); |
David Disseldorp | 2b2abca | 2019-04-18 14:15:48 +0200 | [diff] [blame] | 902 | bool len_only = (size == 0); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 903 | u32 namelen; |
| 904 | int err; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 905 | int i; |
| 906 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 907 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 908 | dout("listxattr %p ver=%lld index_ver=%lld\n", inode, |
| 909 | ci->i_xattrs.version, ci->i_xattrs.index_version); |
| 910 | |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 911 | if (ci->i_xattrs.version == 0 || |
| 912 | !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 913 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 914 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 915 | if (err) |
| 916 | return err; |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 917 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 920 | err = __build_xattrs(inode); |
| 921 | if (err < 0) |
| 922 | goto out; |
Alex Elder | 3ce6cd1 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 923 | |
David Disseldorp | 2b2abca | 2019-04-18 14:15:48 +0200 | [diff] [blame] | 924 | /* add 1 byte for each xattr due to the null termination */ |
Sage Weil | b65917d | 2013-01-20 22:02:39 -0800 | [diff] [blame] | 925 | namelen = ci->i_xattrs.names_size + ci->i_xattrs.count; |
David Disseldorp | 2b2abca | 2019-04-18 14:15:48 +0200 | [diff] [blame] | 926 | if (!len_only) { |
| 927 | if (namelen > size) { |
| 928 | err = -ERANGE; |
| 929 | goto out; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 930 | } |
David Disseldorp | 2b2abca | 2019-04-18 14:15:48 +0200 | [diff] [blame] | 931 | names = __copy_xattr_names(ci, names); |
| 932 | size -= namelen; |
Sage Weil | b65917d | 2013-01-20 22:02:39 -0800 | [diff] [blame] | 933 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 934 | |
David Disseldorp | 2b2abca | 2019-04-18 14:15:48 +0200 | [diff] [blame] | 935 | |
| 936 | /* virtual xattr names, too */ |
| 937 | if (vxattrs) { |
| 938 | for (i = 0; vxattrs[i].name; i++) { |
| 939 | size_t this_len; |
| 940 | |
| 941 | if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN) |
| 942 | continue; |
| 943 | if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci)) |
| 944 | continue; |
| 945 | |
| 946 | this_len = strlen(vxattrs[i].name) + 1; |
| 947 | namelen += this_len; |
| 948 | if (len_only) |
| 949 | continue; |
| 950 | |
| 951 | if (this_len > size) { |
| 952 | err = -ERANGE; |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | memcpy(names, vxattrs[i].name, this_len); |
| 957 | names += this_len; |
| 958 | size -= this_len; |
| 959 | } |
| 960 | } |
| 961 | err = namelen; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 962 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 963 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 964 | return err; |
| 965 | } |
| 966 | |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 967 | static int ceph_sync_setxattr(struct inode *inode, const char *name, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 968 | const char *value, size_t size, int flags) |
| 969 | { |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 970 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 971 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 972 | struct ceph_mds_request *req; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 973 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 974 | struct ceph_pagelist *pagelist = NULL; |
Yan, Zheng | 04303d8 | 2016-04-21 12:11:54 +0800 | [diff] [blame] | 975 | int op = CEPH_MDS_OP_SETXATTR; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 976 | int err; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 977 | |
Yan, Zheng | 0aeff37 | 2014-12-17 21:26:47 +0800 | [diff] [blame] | 978 | if (size > 0) { |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 979 | /* copy value into pagelist */ |
Ilya Dryomov | 33165d4 | 2018-09-28 15:38:34 +0200 | [diff] [blame] | 980 | pagelist = ceph_pagelist_alloc(GFP_NOFS); |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 981 | if (!pagelist) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 982 | return -ENOMEM; |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 983 | |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 984 | err = ceph_pagelist_append(pagelist, value, size); |
| 985 | if (err) |
| 986 | goto out; |
Yan, Zheng | 0aeff37 | 2014-12-17 21:26:47 +0800 | [diff] [blame] | 987 | } else if (!value) { |
Yan, Zheng | 04303d8 | 2016-04-21 12:11:54 +0800 | [diff] [blame] | 988 | if (flags & CEPH_XATTR_REPLACE) |
| 989 | op = CEPH_MDS_OP_RMXATTR; |
| 990 | else |
| 991 | flags |= CEPH_XATTR_REMOVE; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | dout("setxattr value=%.*s\n", (int)size, value); |
| 995 | |
| 996 | /* do request */ |
Yan, Zheng | 04303d8 | 2016-04-21 12:11:54 +0800 | [diff] [blame] | 997 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
Julia Lawall | 60d8773 | 2009-11-21 12:53:08 +0100 | [diff] [blame] | 998 | if (IS_ERR(req)) { |
| 999 | err = PTR_ERR(req); |
| 1000 | goto out; |
| 1001 | } |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 1002 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1003 | req->r_path2 = kstrdup(name, GFP_NOFS); |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 1004 | if (!req->r_path2) { |
| 1005 | ceph_mdsc_put_request(req); |
| 1006 | err = -ENOMEM; |
| 1007 | goto out; |
| 1008 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1009 | |
Yan, Zheng | 04303d8 | 2016-04-21 12:11:54 +0800 | [diff] [blame] | 1010 | if (op == CEPH_MDS_OP_SETXATTR) { |
| 1011 | req->r_args.setxattr.flags = cpu_to_le32(flags); |
| 1012 | req->r_pagelist = pagelist; |
| 1013 | pagelist = NULL; |
| 1014 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1015 | |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 1016 | req->r_inode = inode; |
| 1017 | ihold(inode); |
| 1018 | req->r_num_caps = 1; |
| 1019 | req->r_inode_drop = CEPH_CAP_XATTR_SHARED; |
| 1020 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1021 | dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); |
Sage Weil | 752c8bd | 2013-02-05 13:52:29 -0800 | [diff] [blame] | 1022 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1023 | ceph_mdsc_put_request(req); |
| 1024 | dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); |
| 1025 | |
| 1026 | out: |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 1027 | if (pagelist) |
| 1028 | ceph_pagelist_release(pagelist); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1029 | return err; |
| 1030 | } |
| 1031 | |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 1032 | int __ceph_setxattr(struct inode *inode, const char *name, |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 1033 | const void *value, size_t size, int flags) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1034 | { |
Alex Elder | 881a5fa | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 1035 | struct ceph_vxattr *vxattr; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1036 | struct ceph_inode_info *ci = ceph_inode(inode); |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 1037 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1038 | struct ceph_cap_flush *prealloc_cf = NULL; |
Alex Elder | 18fa8b3 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 1039 | int issued; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1040 | int err; |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 1041 | int dirty = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1042 | int name_len = strlen(name); |
| 1043 | int val_len = size; |
| 1044 | char *newname = NULL; |
| 1045 | char *newval = NULL; |
| 1046 | struct ceph_inode_xattr *xattr = NULL; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1047 | int required_blob_size; |
Yan, Zheng | f191982 | 2018-04-08 09:54:39 +0800 | [diff] [blame] | 1048 | bool check_realm = false; |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1049 | bool lock_snap_rwsem = false; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1050 | |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1051 | if (ceph_snap(inode) != CEPH_NOSNAP) |
| 1052 | return -EROFS; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1053 | |
Alex Elder | 06476a6 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 1054 | vxattr = ceph_match_vxattr(inode, name); |
Yan, Zheng | f191982 | 2018-04-08 09:54:39 +0800 | [diff] [blame] | 1055 | if (vxattr) { |
Yan, Zheng | 4e9906e | 2018-04-25 17:14:05 +0800 | [diff] [blame] | 1056 | if (vxattr->flags & VXATTR_FLAG_READONLY) |
Yan, Zheng | f191982 | 2018-04-08 09:54:39 +0800 | [diff] [blame] | 1057 | return -EOPNOTSUPP; |
| 1058 | if (value && !strncmp(vxattr->name, "ceph.quota", 10)) |
| 1059 | check_realm = true; |
| 1060 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1061 | |
Sage Weil | 3adf654 | 2013-01-31 11:53:41 -0800 | [diff] [blame] | 1062 | /* pass any unhandled ceph.* xattrs through to the MDS */ |
| 1063 | if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) |
| 1064 | goto do_sync_unlocked; |
| 1065 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1066 | /* preallocate memory for xattr name, value, index node */ |
| 1067 | err = -ENOMEM; |
Julia Lawall | 61413c2 | 2010-10-17 21:55:21 +0200 | [diff] [blame] | 1068 | newname = kmemdup(name, name_len + 1, GFP_NOFS); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1069 | if (!newname) |
| 1070 | goto out; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1071 | |
| 1072 | if (val_len) { |
Alex Elder | b829c19 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 1073 | newval = kmemdup(value, val_len, GFP_NOFS); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1074 | if (!newval) |
| 1075 | goto out; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); |
| 1079 | if (!xattr) |
| 1080 | goto out; |
| 1081 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1082 | prealloc_cf = ceph_alloc_cap_flush(); |
| 1083 | if (!prealloc_cf) |
| 1084 | goto out; |
| 1085 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1086 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1087 | retry: |
| 1088 | issued = __ceph_caps_issued(ci, NULL); |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 1089 | if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1090 | goto do_sync; |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1091 | |
| 1092 | if (!lock_snap_rwsem && !ci->i_head_snapc) { |
| 1093 | lock_snap_rwsem = true; |
| 1094 | if (!down_read_trylock(&mdsc->snap_rwsem)) { |
| 1095 | spin_unlock(&ci->i_ceph_lock); |
| 1096 | down_read(&mdsc->snap_rwsem); |
| 1097 | spin_lock(&ci->i_ceph_lock); |
| 1098 | goto retry; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1103 | __build_xattrs(inode); |
| 1104 | |
| 1105 | required_blob_size = __get_required_blob_size(ci, name_len, val_len); |
| 1106 | |
| 1107 | if (!ci->i_xattrs.prealloc_blob || |
| 1108 | required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { |
Alex Elder | 18fa8b3 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 1109 | struct ceph_buffer *blob; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1110 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1111 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1112 | dout(" preaallocating new blob size=%d\n", required_blob_size); |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 1113 | blob = ceph_buffer_new(required_blob_size, GFP_NOFS); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1114 | if (!blob) |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1115 | goto do_sync_unlocked; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1116 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 1117 | if (ci->i_xattrs.prealloc_blob) |
| 1118 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1119 | ci->i_xattrs.prealloc_blob = blob; |
| 1120 | goto retry; |
| 1121 | } |
| 1122 | |
Yan, Zheng | bcdfeb2 | 2014-02-11 13:04:19 +0800 | [diff] [blame] | 1123 | err = __set_xattr(ci, newname, name_len, newval, val_len, |
| 1124 | flags, value ? 1 : -1, &xattr); |
Alex Elder | 18fa8b3 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 1125 | |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 1126 | if (!err) { |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1127 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL, |
| 1128 | &prealloc_cf); |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 1129 | ci->i_xattrs.dirty = true; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 1130 | inode->i_ctime = current_time(inode); |
Yan, Zheng | fbc0b97 | 2014-02-11 13:01:19 +0800 | [diff] [blame] | 1131 | } |
Alex Elder | 18fa8b3 | 2012-01-23 15:49:28 -0600 | [diff] [blame] | 1132 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1133 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1134 | if (lock_snap_rwsem) |
| 1135 | up_read(&mdsc->snap_rwsem); |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1136 | if (dirty) |
| 1137 | __mark_inode_dirty(inode, dirty); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1138 | ceph_free_cap_flush(prealloc_cf); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1139 | return err; |
| 1140 | |
| 1141 | do_sync: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1142 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 3adf654 | 2013-01-31 11:53:41 -0800 | [diff] [blame] | 1143 | do_sync_unlocked: |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1144 | if (lock_snap_rwsem) |
| 1145 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1146 | |
| 1147 | /* security module set xattr while filling trace */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1148 | if (current->journal_info) { |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1149 | pr_warn_ratelimited("sync setxattr %p " |
| 1150 | "during filling trace\n", inode); |
| 1151 | err = -EBUSY; |
| 1152 | } else { |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 1153 | err = ceph_sync_setxattr(inode, name, value, size, flags); |
Yan, Zheng | f191982 | 2018-04-08 09:54:39 +0800 | [diff] [blame] | 1154 | if (err >= 0 && check_realm) { |
| 1155 | /* check if snaprealm was created for quota inode */ |
| 1156 | spin_lock(&ci->i_ceph_lock); |
| 1157 | if ((ci->i_max_files || ci->i_max_bytes) && |
| 1158 | !(ci->i_snap_realm && |
| 1159 | ci->i_snap_realm->ino == ci->i_vino.ino)) |
| 1160 | err = -EOPNOTSUPP; |
| 1161 | spin_unlock(&ci->i_ceph_lock); |
| 1162 | } |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1163 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1164 | out: |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1165 | ceph_free_cap_flush(prealloc_cf); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1166 | kfree(newname); |
| 1167 | kfree(newval); |
| 1168 | kfree(xattr); |
| 1169 | return err; |
| 1170 | } |
| 1171 | |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1172 | static int ceph_get_xattr_handler(const struct xattr_handler *handler, |
| 1173 | struct dentry *dentry, struct inode *inode, |
| 1174 | const char *name, void *value, size_t size) |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 1175 | { |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1176 | if (!ceph_is_valid_xattr(name)) |
| 1177 | return -EOPNOTSUPP; |
| 1178 | return __ceph_getxattr(inode, name, value, size); |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 1179 | } |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1180 | |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1181 | static int ceph_set_xattr_handler(const struct xattr_handler *handler, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 1182 | struct dentry *unused, struct inode *inode, |
| 1183 | const char *name, const void *value, |
| 1184 | size_t size, int flags) |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1185 | { |
| 1186 | if (!ceph_is_valid_xattr(name)) |
| 1187 | return -EOPNOTSUPP; |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 1188 | return __ceph_setxattr(inode, name, value, size, flags); |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Wei Yongjun | 5130cce | 2016-10-17 15:11:29 +0000 | [diff] [blame] | 1191 | static const struct xattr_handler ceph_other_xattr_handler = { |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 1192 | .prefix = "", /* match any name => handlers called with full name */ |
| 1193 | .get = ceph_get_xattr_handler, |
| 1194 | .set = ceph_set_xattr_handler, |
| 1195 | }; |
| 1196 | |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1197 | #ifdef CONFIG_SECURITY |
| 1198 | bool ceph_security_xattr_wanted(struct inode *in) |
| 1199 | { |
| 1200 | return in->i_security != NULL; |
| 1201 | } |
| 1202 | |
| 1203 | bool ceph_security_xattr_deadlock(struct inode *in) |
| 1204 | { |
| 1205 | struct ceph_inode_info *ci; |
| 1206 | bool ret; |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1207 | if (!in->i_security) |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1208 | return false; |
| 1209 | ci = ceph_inode(in); |
| 1210 | spin_lock(&ci->i_ceph_lock); |
| 1211 | ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) && |
| 1212 | !(ci->i_xattrs.version > 0 && |
| 1213 | __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)); |
| 1214 | spin_unlock(&ci->i_ceph_lock); |
| 1215 | return ret; |
| 1216 | } |
Yan, Zheng | ac6713c | 2019-05-26 16:27:56 +0800 | [diff] [blame] | 1217 | |
| 1218 | #ifdef CONFIG_CEPH_FS_SECURITY_LABEL |
| 1219 | int ceph_security_init_secctx(struct dentry *dentry, umode_t mode, |
| 1220 | struct ceph_acl_sec_ctx *as_ctx) |
| 1221 | { |
| 1222 | struct ceph_pagelist *pagelist = as_ctx->pagelist; |
| 1223 | const char *name; |
| 1224 | size_t name_len; |
| 1225 | int err; |
| 1226 | |
| 1227 | err = security_dentry_init_security(dentry, mode, &dentry->d_name, |
| 1228 | &as_ctx->sec_ctx, |
| 1229 | &as_ctx->sec_ctxlen); |
| 1230 | if (err < 0) { |
| 1231 | WARN_ON_ONCE(err != -EOPNOTSUPP); |
| 1232 | err = 0; /* do nothing */ |
| 1233 | goto out; |
| 1234 | } |
| 1235 | |
| 1236 | err = -ENOMEM; |
| 1237 | if (!pagelist) { |
| 1238 | pagelist = ceph_pagelist_alloc(GFP_KERNEL); |
| 1239 | if (!pagelist) |
| 1240 | goto out; |
| 1241 | err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); |
| 1242 | if (err) |
| 1243 | goto out; |
| 1244 | ceph_pagelist_encode_32(pagelist, 1); |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * FIXME: Make security_dentry_init_security() generic. Currently |
| 1249 | * It only supports single security module and only selinux has |
| 1250 | * dentry_init_security hook. |
| 1251 | */ |
| 1252 | name = XATTR_NAME_SELINUX; |
| 1253 | name_len = strlen(name); |
| 1254 | err = ceph_pagelist_reserve(pagelist, |
| 1255 | 4 * 2 + name_len + as_ctx->sec_ctxlen); |
| 1256 | if (err) |
| 1257 | goto out; |
| 1258 | |
| 1259 | if (as_ctx->pagelist) { |
| 1260 | /* update count of KV pairs */ |
| 1261 | BUG_ON(pagelist->length <= sizeof(__le32)); |
| 1262 | if (list_is_singular(&pagelist->head)) { |
| 1263 | le32_add_cpu((__le32*)pagelist->mapped_tail, 1); |
| 1264 | } else { |
| 1265 | struct page *page = list_first_entry(&pagelist->head, |
| 1266 | struct page, lru); |
| 1267 | void *addr = kmap_atomic(page); |
| 1268 | le32_add_cpu((__le32*)addr, 1); |
| 1269 | kunmap_atomic(addr); |
| 1270 | } |
| 1271 | } else { |
| 1272 | as_ctx->pagelist = pagelist; |
| 1273 | } |
| 1274 | |
| 1275 | ceph_pagelist_encode_32(pagelist, name_len); |
| 1276 | ceph_pagelist_append(pagelist, name, name_len); |
| 1277 | |
| 1278 | ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen); |
| 1279 | ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen); |
| 1280 | |
| 1281 | err = 0; |
| 1282 | out: |
| 1283 | if (pagelist && !as_ctx->pagelist) |
| 1284 | ceph_pagelist_release(pagelist); |
| 1285 | return err; |
| 1286 | } |
| 1287 | |
| 1288 | void ceph_security_invalidate_secctx(struct inode *inode) |
| 1289 | { |
| 1290 | security_inode_invalidate_secctx(inode); |
| 1291 | } |
| 1292 | |
| 1293 | static int ceph_xattr_set_security_label(const struct xattr_handler *handler, |
| 1294 | struct dentry *unused, struct inode *inode, |
| 1295 | const char *key, const void *buf, |
| 1296 | size_t buflen, int flags) |
| 1297 | { |
| 1298 | if (security_ismaclabel(key)) { |
| 1299 | const char *name = xattr_full_name(handler, key); |
| 1300 | return __ceph_setxattr(inode, name, buf, buflen, flags); |
| 1301 | } |
| 1302 | return -EOPNOTSUPP; |
| 1303 | } |
| 1304 | |
| 1305 | static int ceph_xattr_get_security_label(const struct xattr_handler *handler, |
| 1306 | struct dentry *unused, struct inode *inode, |
| 1307 | const char *key, void *buf, size_t buflen) |
| 1308 | { |
| 1309 | if (security_ismaclabel(key)) { |
| 1310 | const char *name = xattr_full_name(handler, key); |
| 1311 | return __ceph_getxattr(inode, name, buf, buflen); |
| 1312 | } |
| 1313 | return -EOPNOTSUPP; |
| 1314 | } |
| 1315 | |
| 1316 | static const struct xattr_handler ceph_security_label_handler = { |
| 1317 | .prefix = XATTR_SECURITY_PREFIX, |
| 1318 | .get = ceph_xattr_get_security_label, |
| 1319 | .set = ceph_xattr_set_security_label, |
| 1320 | }; |
| 1321 | #endif |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1322 | #endif |
Yan, Zheng | 5c31e92 | 2019-05-26 15:35:39 +0800 | [diff] [blame] | 1323 | |
| 1324 | void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx) |
| 1325 | { |
| 1326 | #ifdef CONFIG_CEPH_FS_POSIX_ACL |
| 1327 | posix_acl_release(as_ctx->acl); |
| 1328 | posix_acl_release(as_ctx->default_acl); |
| 1329 | #endif |
Yan, Zheng | ac6713c | 2019-05-26 16:27:56 +0800 | [diff] [blame] | 1330 | #ifdef CONFIG_CEPH_FS_SECURITY_LABEL |
| 1331 | security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen); |
| 1332 | #endif |
Yan, Zheng | 5c31e92 | 2019-05-26 15:35:39 +0800 | [diff] [blame] | 1333 | if (as_ctx->pagelist) |
| 1334 | ceph_pagelist_release(as_ctx->pagelist); |
| 1335 | } |
Yan, Zheng | ac6713c | 2019-05-26 16:27:56 +0800 | [diff] [blame] | 1336 | |
| 1337 | /* |
| 1338 | * List of handlers for synthetic system.* attributes. Other |
| 1339 | * attributes are handled directly. |
| 1340 | */ |
| 1341 | const struct xattr_handler *ceph_xattr_handlers[] = { |
| 1342 | #ifdef CONFIG_CEPH_FS_POSIX_ACL |
| 1343 | &posix_acl_access_xattr_handler, |
| 1344 | &posix_acl_default_xattr_handler, |
| 1345 | #endif |
| 1346 | #ifdef CONFIG_CEPH_FS_SECURITY_LABEL |
| 1347 | &ceph_security_label_handler, |
| 1348 | #endif |
| 1349 | &ceph_other_xattr_handler, |
| 1350 | NULL, |
| 1351 | }; |