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