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