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