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