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