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