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