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