blob: 19da5026c38f565fa138b5c43d68e9577ff23acc [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
2
Sage Weil355da1e2009-10-06 11:31:08 -07003#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include "mds_client.h"
5
6#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -07007
8#include <linux/xattr.h>
Linus Torvalds4db658e2014-01-28 18:06:18 -08009#include <linux/posix_acl_xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Sage Weil355da1e2009-10-06 11:31:08 -070011
Alex Elder22891902012-01-23 15:49:28 -060012#define XATTR_CEPH_PREFIX "ceph."
13#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
14
Yan, Zhengbcdfeb22014-02-11 13:04:19 +080015static int __remove_xattr(struct ceph_inode_info *ci,
16 struct ceph_inode_xattr *xattr);
17
Guangliang Zhao7221fe42013-11-11 15:18:03 +080018/*
19 * List of handlers for synthetic system.* attributes. Other
20 * attributes are handled directly.
21 */
22const struct xattr_handler *ceph_xattr_handlers[] = {
23#ifdef CONFIG_CEPH_FS_POSIX_ACL
Linus Torvalds4db658e2014-01-28 18:06:18 -080024 &posix_acl_access_xattr_handler,
25 &posix_acl_default_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080026#endif
27 NULL,
28};
29
Sage Weil355da1e2009-10-06 11:31:08 -070030static bool ceph_is_valid_xattr(const char *name)
31{
Alex Elder22891902012-01-23 15:49:28 -060032 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil1a756272010-05-11 11:40:25 -070033 !strncmp(name, XATTR_SECURITY_PREFIX,
Sage Weil355da1e2009-10-06 11:31:08 -070034 XATTR_SECURITY_PREFIX_LEN) ||
Guangliang Zhao7221fe42013-11-11 15:18:03 +080035 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
Sage Weil355da1e2009-10-06 11:31:08 -070036 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
37 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
38}
39
40/*
41 * These define virtual xattrs exposing the recursive directory
42 * statistics and layout metadata.
43 */
Alex Elder881a5fa2012-01-23 15:49:28 -060044struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070045 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060046 size_t name_size; /* strlen(name) + 1 (for '\0') */
Sage Weil355da1e2009-10-06 11:31:08 -070047 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
48 size_t size);
Sage Weil88601472013-01-31 11:53:27 -080049 bool readonly, hidden;
Sage Weilf36e4472013-01-20 21:59:29 -080050 bool (*exists_cb)(struct ceph_inode_info *ci);
Sage Weil355da1e2009-10-06 11:31:08 -070051};
52
Sage Weil32ab0bd2013-01-19 16:46:32 -080053/* layouts */
54
55static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
56{
57 size_t s;
58 char *p = (char *)&ci->i_layout;
59
60 for (s = 0; s < sizeof(ci->i_layout); s++, p++)
61 if (*p)
62 return true;
63 return false;
64}
65
66static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
Yan, Zheng1e5c6642014-03-24 13:00:54 +080067 size_t size)
Sage Weil32ab0bd2013-01-19 16:46:32 -080068{
69 int ret;
70 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
71 struct ceph_osd_client *osdc = &fsc->client->osdc;
72 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
73 const char *pool_name;
Yan, Zheng1e5c6642014-03-24 13:00:54 +080074 char buf[128];
Sage Weil32ab0bd2013-01-19 16:46:32 -080075
76 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
77 down_read(&osdc->map_sem);
78 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080079 if (pool_name) {
80 size_t len = strlen(pool_name);
81 ret = snprintf(buf, sizeof(buf),
82 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=",
Sage Weil32ab0bd2013-01-19 16:46:32 -080083 (unsigned long long)ceph_file_layout_su(ci->i_layout),
84 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
Yan, Zheng1e5c6642014-03-24 13:00:54 +080085 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
86 if (!size) {
87 ret += len;
88 } else if (ret + len > size) {
89 ret = -ERANGE;
90 } else {
91 memcpy(val, buf, ret);
92 memcpy(val + ret, pool_name, len);
93 ret += len;
94 }
95 } else {
96 ret = snprintf(buf, sizeof(buf),
Sage Weil32ab0bd2013-01-19 16:46:32 -080097 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%lld",
98 (unsigned long long)ceph_file_layout_su(ci->i_layout),
99 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
100 (unsigned long long)ceph_file_layout_object_size(ci->i_layout),
101 (unsigned long long)pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +0800102 if (size) {
103 if (ret <= size)
104 memcpy(val, buf, ret);
105 else
106 ret = -ERANGE;
107 }
108 }
Sage Weil32ab0bd2013-01-19 16:46:32 -0800109 up_read(&osdc->map_sem);
110 return ret;
111}
112
Sage Weil695b7112013-01-20 22:08:33 -0800113static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
114 char *val, size_t size)
115{
116 return snprintf(val, size, "%lld",
117 (unsigned long long)ceph_file_layout_su(ci->i_layout));
118}
119
120static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
121 char *val, size_t size)
122{
123 return snprintf(val, size, "%lld",
124 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout));
125}
126
127static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
128 char *val, size_t size)
129{
130 return snprintf(val, size, "%lld",
131 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
132}
133
134static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
135 char *val, size_t size)
136{
137 int ret;
138 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
139 struct ceph_osd_client *osdc = &fsc->client->osdc;
140 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
141 const char *pool_name;
142
143 down_read(&osdc->map_sem);
144 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
145 if (pool_name)
146 ret = snprintf(val, size, "%s", pool_name);
147 else
148 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
149 up_read(&osdc->map_sem);
150 return ret;
151}
152
Sage Weil355da1e2009-10-06 11:31:08 -0700153/* directories */
154
Alex Elderaa4066e2012-01-23 15:49:28 -0600155static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700156 size_t size)
157{
158 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
159}
160
Alex Elderaa4066e2012-01-23 15:49:28 -0600161static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700162 size_t size)
163{
164 return snprintf(val, size, "%lld", ci->i_files);
165}
166
Alex Elderaa4066e2012-01-23 15:49:28 -0600167static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700168 size_t size)
169{
170 return snprintf(val, size, "%lld", ci->i_subdirs);
171}
172
Alex Elderaa4066e2012-01-23 15:49:28 -0600173static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700174 size_t size)
175{
176 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
177}
178
Alex Elderaa4066e2012-01-23 15:49:28 -0600179static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700180 size_t size)
181{
182 return snprintf(val, size, "%lld", ci->i_rfiles);
183}
184
Alex Elderaa4066e2012-01-23 15:49:28 -0600185static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700186 size_t size)
187{
188 return snprintf(val, size, "%lld", ci->i_rsubdirs);
189}
190
Alex Elderaa4066e2012-01-23 15:49:28 -0600191static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700192 size_t size)
193{
194 return snprintf(val, size, "%lld", ci->i_rbytes);
195}
196
Alex Elderaa4066e2012-01-23 15:49:28 -0600197static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700198 size_t size)
199{
Alex Elder3489b422012-03-08 16:50:09 -0600200 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
Sage Weil355da1e2009-10-06 11:31:08 -0700201 (long)ci->i_rctime.tv_nsec);
202}
203
Sage Weil32ab0bd2013-01-19 16:46:32 -0800204
Alex Eldereb788082012-01-23 15:49:28 -0600205#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800206#define CEPH_XATTR_NAME2(_type, _name, _name2) \
207 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600208
Sage Weil88601472013-01-31 11:53:27 -0800209#define XATTR_NAME_CEPH(_type, _name) \
210 { \
211 .name = CEPH_XATTR_NAME(_type, _name), \
212 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
213 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
214 .readonly = true, \
215 .hidden = false, \
Sage Weilf36e4472013-01-20 21:59:29 -0800216 .exists_cb = NULL, \
Sage Weil88601472013-01-31 11:53:27 -0800217 }
Sage Weil695b7112013-01-20 22:08:33 -0800218#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
219 { \
220 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
221 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
222 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
223 .readonly = false, \
224 .hidden = true, \
225 .exists_cb = ceph_vxattrcb_layout_exists, \
226 }
Alex Eldereb788082012-01-23 15:49:28 -0600227
Alex Elder881a5fa2012-01-23 15:49:28 -0600228static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800229 {
230 .name = "ceph.dir.layout",
231 .name_size = sizeof("ceph.dir.layout"),
232 .getxattr_cb = ceph_vxattrcb_layout,
233 .readonly = false,
Yan, Zhengcc48c3e2014-03-24 19:15:05 +0800234 .hidden = true,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800235 .exists_cb = ceph_vxattrcb_layout_exists,
236 },
Sage Weil695b7112013-01-20 22:08:33 -0800237 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
238 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
239 XATTR_LAYOUT_FIELD(dir, layout, object_size),
240 XATTR_LAYOUT_FIELD(dir, layout, pool),
Alex Eldereb788082012-01-23 15:49:28 -0600241 XATTR_NAME_CEPH(dir, entries),
242 XATTR_NAME_CEPH(dir, files),
243 XATTR_NAME_CEPH(dir, subdirs),
244 XATTR_NAME_CEPH(dir, rentries),
245 XATTR_NAME_CEPH(dir, rfiles),
246 XATTR_NAME_CEPH(dir, rsubdirs),
247 XATTR_NAME_CEPH(dir, rbytes),
248 XATTR_NAME_CEPH(dir, rctime),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600249 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700250};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600251static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700252
253/* files */
254
Alex Elder881a5fa2012-01-23 15:49:28 -0600255static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800256 {
257 .name = "ceph.file.layout",
258 .name_size = sizeof("ceph.file.layout"),
259 .getxattr_cb = ceph_vxattrcb_layout,
260 .readonly = false,
Yan, Zhengcc48c3e2014-03-24 19:15:05 +0800261 .hidden = true,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800262 .exists_cb = ceph_vxattrcb_layout_exists,
263 },
Sage Weil695b7112013-01-20 22:08:33 -0800264 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
265 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
266 XATTR_LAYOUT_FIELD(file, layout, object_size),
267 XATTR_LAYOUT_FIELD(file, layout, pool),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600268 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700269};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600270static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700271
Alex Elder881a5fa2012-01-23 15:49:28 -0600272static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700273{
274 if (S_ISDIR(inode->i_mode))
275 return ceph_dir_vxattrs;
276 else if (S_ISREG(inode->i_mode))
277 return ceph_file_vxattrs;
278 return NULL;
279}
280
Alex Elder3ce6cd12012-01-23 15:49:28 -0600281static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
282{
283 if (vxattrs == ceph_dir_vxattrs)
284 return ceph_dir_vxattrs_name_size;
285 if (vxattrs == ceph_file_vxattrs)
286 return ceph_file_vxattrs_name_size;
Yan, Zheng0abb43d2014-09-18 16:11:12 +0800287 BUG_ON(vxattrs);
Alex Elder3ce6cd12012-01-23 15:49:28 -0600288 return 0;
289}
290
291/*
292 * Compute the aggregate size (including terminating '\0') of all
293 * virtual extended attribute names in the given vxattr table.
294 */
295static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
296{
297 struct ceph_vxattr *vxattr;
298 size_t size = 0;
299
300 for (vxattr = vxattrs; vxattr->name; vxattr++)
Sage Weil88601472013-01-31 11:53:27 -0800301 if (!vxattr->hidden)
302 size += vxattr->name_size;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600303
304 return size;
305}
306
307/* Routines called at initialization and exit time */
308
309void __init ceph_xattr_init(void)
310{
311 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
312 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
313}
314
315void ceph_xattr_exit(void)
316{
317 ceph_dir_vxattrs_name_size = 0;
318 ceph_file_vxattrs_name_size = 0;
319}
320
Alex Elder881a5fa2012-01-23 15:49:28 -0600321static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700322 const char *name)
323{
Alex Elder881a5fa2012-01-23 15:49:28 -0600324 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600325
326 if (vxattr) {
327 while (vxattr->name) {
328 if (!strcmp(vxattr->name, name))
329 return vxattr;
330 vxattr++;
331 }
332 }
333
Sage Weil355da1e2009-10-06 11:31:08 -0700334 return NULL;
335}
336
337static int __set_xattr(struct ceph_inode_info *ci,
338 const char *name, int name_len,
339 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800340 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700341 struct ceph_inode_xattr **newxattr)
342{
343 struct rb_node **p;
344 struct rb_node *parent = NULL;
345 struct ceph_inode_xattr *xattr = NULL;
346 int c;
347 int new = 0;
348
349 p = &ci->i_xattrs.index.rb_node;
350 while (*p) {
351 parent = *p;
352 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
353 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
354 if (c < 0)
355 p = &(*p)->rb_left;
356 else if (c > 0)
357 p = &(*p)->rb_right;
358 else {
359 if (name_len == xattr->name_len)
360 break;
361 else if (name_len < xattr->name_len)
362 p = &(*p)->rb_left;
363 else
364 p = &(*p)->rb_right;
365 }
366 xattr = NULL;
367 }
368
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800369 if (update_xattr) {
370 int err = 0;
371 if (xattr && (flags & XATTR_CREATE))
372 err = -EEXIST;
373 else if (!xattr && (flags & XATTR_REPLACE))
374 err = -ENODATA;
375 if (err) {
376 kfree(name);
377 kfree(val);
378 return err;
379 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800380 if (update_xattr < 0) {
381 if (xattr)
382 __remove_xattr(ci, xattr);
383 kfree(name);
384 return 0;
385 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800386 }
387
Sage Weil355da1e2009-10-06 11:31:08 -0700388 if (!xattr) {
389 new = 1;
390 xattr = *newxattr;
391 xattr->name = name;
392 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800393 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700394
395 ci->i_xattrs.count++;
396 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
397 } else {
398 kfree(*newxattr);
399 *newxattr = NULL;
400 if (xattr->should_free_val)
401 kfree((void *)xattr->val);
402
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800403 if (update_xattr) {
Sage Weil355da1e2009-10-06 11:31:08 -0700404 kfree((void *)name);
405 name = xattr->name;
406 }
407 ci->i_xattrs.names_size -= xattr->name_len;
408 ci->i_xattrs.vals_size -= xattr->val_len;
409 }
Sage Weil355da1e2009-10-06 11:31:08 -0700410 ci->i_xattrs.names_size += name_len;
411 ci->i_xattrs.vals_size += val_len;
412 if (val)
413 xattr->val = val;
414 else
415 xattr->val = "";
416
417 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800418 xattr->dirty = update_xattr;
419 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700420
421 if (new) {
422 rb_link_node(&xattr->node, parent, p);
423 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
424 dout("__set_xattr_val p=%p\n", p);
425 }
426
427 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
428 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
429
430 return 0;
431}
432
433static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
434 const char *name)
435{
436 struct rb_node **p;
437 struct rb_node *parent = NULL;
438 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800439 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700440 int c;
441
442 p = &ci->i_xattrs.index.rb_node;
443 while (*p) {
444 parent = *p;
445 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
446 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800447 if (c == 0 && name_len > xattr->name_len)
448 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700449 if (c < 0)
450 p = &(*p)->rb_left;
451 else if (c > 0)
452 p = &(*p)->rb_right;
453 else {
454 dout("__get_xattr %s: found %.*s\n", name,
455 xattr->val_len, xattr->val);
456 return xattr;
457 }
458 }
459
460 dout("__get_xattr %s: not found\n", name);
461
462 return NULL;
463}
464
465static void __free_xattr(struct ceph_inode_xattr *xattr)
466{
467 BUG_ON(!xattr);
468
469 if (xattr->should_free_name)
470 kfree((void *)xattr->name);
471 if (xattr->should_free_val)
472 kfree((void *)xattr->val);
473
474 kfree(xattr);
475}
476
477static int __remove_xattr(struct ceph_inode_info *ci,
478 struct ceph_inode_xattr *xattr)
479{
480 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800481 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700482
483 rb_erase(&xattr->node, &ci->i_xattrs.index);
484
485 if (xattr->should_free_name)
486 kfree((void *)xattr->name);
487 if (xattr->should_free_val)
488 kfree((void *)xattr->val);
489
490 ci->i_xattrs.names_size -= xattr->name_len;
491 ci->i_xattrs.vals_size -= xattr->val_len;
492 ci->i_xattrs.count--;
493 kfree(xattr);
494
495 return 0;
496}
497
498static int __remove_xattr_by_name(struct ceph_inode_info *ci,
499 const char *name)
500{
501 struct rb_node **p;
502 struct ceph_inode_xattr *xattr;
503 int err;
504
505 p = &ci->i_xattrs.index.rb_node;
506 xattr = __get_xattr(ci, name);
507 err = __remove_xattr(ci, xattr);
508 return err;
509}
510
511static char *__copy_xattr_names(struct ceph_inode_info *ci,
512 char *dest)
513{
514 struct rb_node *p;
515 struct ceph_inode_xattr *xattr = NULL;
516
517 p = rb_first(&ci->i_xattrs.index);
518 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
519
520 while (p) {
521 xattr = rb_entry(p, struct ceph_inode_xattr, node);
522 memcpy(dest, xattr->name, xattr->name_len);
523 dest[xattr->name_len] = '\0';
524
525 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
526 xattr->name_len, ci->i_xattrs.names_size);
527
528 dest += xattr->name_len + 1;
529 p = rb_next(p);
530 }
531
532 return dest;
533}
534
535void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
536{
537 struct rb_node *p, *tmp;
538 struct ceph_inode_xattr *xattr = NULL;
539
540 p = rb_first(&ci->i_xattrs.index);
541
542 dout("__ceph_destroy_xattrs p=%p\n", p);
543
544 while (p) {
545 xattr = rb_entry(p, struct ceph_inode_xattr, node);
546 tmp = p;
547 p = rb_next(tmp);
548 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
549 xattr->name_len, xattr->name);
550 rb_erase(tmp, &ci->i_xattrs.index);
551
552 __free_xattr(xattr);
553 }
554
555 ci->i_xattrs.names_size = 0;
556 ci->i_xattrs.vals_size = 0;
557 ci->i_xattrs.index_version = 0;
558 ci->i_xattrs.count = 0;
559 ci->i_xattrs.index = RB_ROOT;
560}
561
562static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800563 __releases(ci->i_ceph_lock)
564 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700565{
566 u32 namelen;
567 u32 numattr = 0;
568 void *p, *end;
569 u32 len;
570 const char *name, *val;
571 struct ceph_inode_info *ci = ceph_inode(inode);
572 int xattr_version;
573 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800574 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700575 int i;
576
577 dout("__build_xattrs() len=%d\n",
578 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
579
580 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
581 return 0; /* already built */
582
583 __ceph_destroy_xattrs(ci);
584
585start:
586 /* updated internal xattr rb tree */
587 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
588 p = ci->i_xattrs.blob->vec.iov_base;
589 end = p + ci->i_xattrs.blob->vec.iov_len;
590 ceph_decode_32_safe(&p, end, numattr, bad);
591 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800592 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700593
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400594 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700595 GFP_NOFS);
596 err = -ENOMEM;
597 if (!xattrs)
598 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400599
Sage Weil355da1e2009-10-06 11:31:08 -0700600 for (i = 0; i < numattr; i++) {
601 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
602 GFP_NOFS);
603 if (!xattrs[i])
604 goto bad_lock;
605 }
606
Sage Weilbe655592011-11-30 09:47:09 -0800607 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700608 if (ci->i_xattrs.version != xattr_version) {
609 /* lost a race, retry */
610 for (i = 0; i < numattr; i++)
611 kfree(xattrs[i]);
612 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500613 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700614 goto start;
615 }
616 err = -EIO;
617 while (numattr--) {
618 ceph_decode_32_safe(&p, end, len, bad);
619 namelen = len;
620 name = p;
621 p += len;
622 ceph_decode_32_safe(&p, end, len, bad);
623 val = p;
624 p += len;
625
626 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800627 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700628
629 if (err < 0)
630 goto bad;
631 }
632 kfree(xattrs);
633 }
634 ci->i_xattrs.index_version = ci->i_xattrs.version;
635 ci->i_xattrs.dirty = false;
636
637 return err;
638bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800639 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700640bad:
641 if (xattrs) {
642 for (i = 0; i < numattr; i++)
643 kfree(xattrs[i]);
644 kfree(xattrs);
645 }
646 ci->i_xattrs.names_size = 0;
647 return err;
648}
649
650static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
651 int val_size)
652{
653 /*
654 * 4 bytes for the length, and additional 4 bytes per each xattr name,
655 * 4 bytes per each value
656 */
657 int size = 4 + ci->i_xattrs.count*(4 + 4) +
658 ci->i_xattrs.names_size +
659 ci->i_xattrs.vals_size;
660 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
661 ci->i_xattrs.count, ci->i_xattrs.names_size,
662 ci->i_xattrs.vals_size);
663
664 if (name_size)
665 size += 4 + 4 + name_size + val_size;
666
667 return size;
668}
669
670/*
671 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
672 * and swap into place.
673 */
674void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
675{
676 struct rb_node *p;
677 struct ceph_inode_xattr *xattr = NULL;
678 void *dest;
679
680 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
681 if (ci->i_xattrs.dirty) {
682 int need = __get_required_blob_size(ci, 0, 0);
683
684 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
685
686 p = rb_first(&ci->i_xattrs.index);
687 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
688
689 ceph_encode_32(&dest, ci->i_xattrs.count);
690 while (p) {
691 xattr = rb_entry(p, struct ceph_inode_xattr, node);
692
693 ceph_encode_32(&dest, xattr->name_len);
694 memcpy(dest, xattr->name, xattr->name_len);
695 dest += xattr->name_len;
696 ceph_encode_32(&dest, xattr->val_len);
697 memcpy(dest, xattr->val, xattr->val_len);
698 dest += xattr->val_len;
699
700 p = rb_next(p);
701 }
702
703 /* adjust buffer len; it may be larger than we need */
704 ci->i_xattrs.prealloc_blob->vec.iov_len =
705 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
706
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800707 if (ci->i_xattrs.blob)
708 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700709 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
710 ci->i_xattrs.prealloc_blob = NULL;
711 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700712 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700713 }
714}
715
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800716ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700717 size_t size)
718{
Sage Weil355da1e2009-10-06 11:31:08 -0700719 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700720 int err;
721 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600722 struct ceph_vxattr *vxattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700723
724 if (!ceph_is_valid_xattr(name))
725 return -ENODATA;
726
Sage Weil0bee82f2013-01-20 22:00:58 -0800727 /* let's see if a virtual xattr was requested */
728 vxattr = ceph_match_vxattr(inode, name);
729 if (vxattr && !(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
730 err = vxattr->getxattr_cb(ci, value, size);
majianpenga1dc1932013-06-19 14:58:10 +0800731 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800732 }
733
majianpenga1dc1932013-06-19 14:58:10 +0800734 spin_lock(&ci->i_ceph_lock);
735 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
736 ci->i_xattrs.version, ci->i_xattrs.index_version);
737
Yan, Zheng508b32d2014-09-16 21:46:17 +0800738 if (ci->i_xattrs.version == 0 ||
739 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800740 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700741 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800742 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700743 if (err)
744 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800745 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700746 }
747
Sage Weil355da1e2009-10-06 11:31:08 -0700748 err = __build_xattrs(inode);
749 if (err < 0)
750 goto out;
751
Sage Weil355da1e2009-10-06 11:31:08 -0700752 err = -ENODATA; /* == ENOATTR */
753 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800754 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700755 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700756
757 err = -ERANGE;
758 if (size && size < xattr->val_len)
759 goto out;
760
761 err = xattr->val_len;
762 if (size == 0)
763 goto out;
764
765 memcpy(value, xattr->val, xattr->val_len);
766
767out:
Sage Weilbe655592011-11-30 09:47:09 -0800768 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700769 return err;
770}
771
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800772ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
773 size_t size)
774{
775 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
776 return generic_getxattr(dentry, name, value, size);
777
778 return __ceph_getxattr(dentry->d_inode, name, value, size);
779}
780
Sage Weil355da1e2009-10-06 11:31:08 -0700781ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
782{
783 struct inode *inode = dentry->d_inode;
784 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600785 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700786 u32 vir_namelen = 0;
787 u32 namelen;
788 int err;
789 u32 len;
790 int i;
791
Sage Weilbe655592011-11-30 09:47:09 -0800792 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700793 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
794 ci->i_xattrs.version, ci->i_xattrs.index_version);
795
Yan, Zheng508b32d2014-09-16 21:46:17 +0800796 if (ci->i_xattrs.version == 0 ||
797 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800798 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800799 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700800 if (err)
801 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800802 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700803 }
804
Sage Weil355da1e2009-10-06 11:31:08 -0700805 err = __build_xattrs(inode);
806 if (err < 0)
807 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600808 /*
809 * Start with virtual dir xattr names (if any) (including
810 * terminating '\0' characters for each).
811 */
812 vir_namelen = ceph_vxattrs_name_size(vxattrs);
813
Sage Weil355da1e2009-10-06 11:31:08 -0700814 /* adding 1 byte per each variable due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -0800815 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
Sage Weil355da1e2009-10-06 11:31:08 -0700816 err = -ERANGE;
Sage Weilb65917d2013-01-20 22:02:39 -0800817 if (size && vir_namelen + namelen > size)
Sage Weil355da1e2009-10-06 11:31:08 -0700818 goto out;
819
Sage Weilb65917d2013-01-20 22:02:39 -0800820 err = namelen + vir_namelen;
Sage Weil355da1e2009-10-06 11:31:08 -0700821 if (size == 0)
822 goto out;
823
824 names = __copy_xattr_names(ci, names);
825
826 /* virtual xattr names, too */
Sage Weilb65917d2013-01-20 22:02:39 -0800827 err = namelen;
828 if (vxattrs) {
Sage Weil355da1e2009-10-06 11:31:08 -0700829 for (i = 0; vxattrs[i].name; i++) {
Sage Weilb65917d2013-01-20 22:02:39 -0800830 if (!vxattrs[i].hidden &&
831 !(vxattrs[i].exists_cb &&
832 !vxattrs[i].exists_cb(ci))) {
833 len = sprintf(names, "%s", vxattrs[i].name);
834 names += len + 1;
835 err += len + 1;
836 }
Sage Weil355da1e2009-10-06 11:31:08 -0700837 }
Sage Weilb65917d2013-01-20 22:02:39 -0800838 }
Sage Weil355da1e2009-10-06 11:31:08 -0700839
840out:
Sage Weilbe655592011-11-30 09:47:09 -0800841 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700842 return err;
843}
844
845static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
846 const char *value, size_t size, int flags)
847{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700848 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700849 struct inode *inode = dentry->d_inode;
850 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700851 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700852 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700853 int err;
854 int i, nr_pages;
855 struct page **pages = NULL;
856 void *kaddr;
857
858 /* copy value into some pages */
859 nr_pages = calc_pages_for(0, size);
860 if (nr_pages) {
861 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
862 if (!pages)
863 return -ENOMEM;
864 err = -ENOMEM;
865 for (i = 0; i < nr_pages; i++) {
Yehuda Sadeh31459fe2010-03-17 13:54:02 -0700866 pages[i] = __page_cache_alloc(GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700867 if (!pages[i]) {
868 nr_pages = i;
869 goto out;
870 }
871 kaddr = kmap(pages[i]);
872 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
873 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
874 }
875 }
876
877 dout("setxattr value=%.*s\n", (int)size, value);
878
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800879 if (!value)
880 flags |= CEPH_XATTR_REMOVE;
881
Sage Weil355da1e2009-10-06 11:31:08 -0700882 /* do request */
883 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
884 USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100885 if (IS_ERR(req)) {
886 err = PTR_ERR(req);
887 goto out;
888 }
Sage Weil70b666c2011-05-27 09:24:26 -0700889 req->r_inode = inode;
890 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700891 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
892 req->r_num_caps = 1;
893 req->r_args.setxattr.flags = cpu_to_le32(flags);
894 req->r_path2 = kstrdup(name, GFP_NOFS);
895
896 req->r_pages = pages;
897 req->r_num_pages = nr_pages;
898 req->r_data_len = size;
899
900 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -0800901 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -0700902 ceph_mdsc_put_request(req);
903 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
904
905out:
906 if (pages) {
907 for (i = 0; i < nr_pages; i++)
908 __free_page(pages[i]);
909 kfree(pages);
910 }
911 return err;
912}
913
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800914int __ceph_setxattr(struct dentry *dentry, const char *name,
915 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -0700916{
917 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600918 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700919 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder18fa8b32012-01-23 15:49:28 -0600920 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700921 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800922 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700923 int name_len = strlen(name);
924 int val_len = size;
925 char *newname = NULL;
926 char *newval = NULL;
927 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700928 int required_blob_size;
929
Sage Weil355da1e2009-10-06 11:31:08 -0700930 if (!ceph_is_valid_xattr(name))
931 return -EOPNOTSUPP;
932
Alex Elder06476a62012-01-23 15:49:27 -0600933 vxattr = ceph_match_vxattr(inode, name);
934 if (vxattr && vxattr->readonly)
935 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700936
Sage Weil3adf6542013-01-31 11:53:41 -0800937 /* pass any unhandled ceph.* xattrs through to the MDS */
938 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
939 goto do_sync_unlocked;
940
Sage Weil355da1e2009-10-06 11:31:08 -0700941 /* preallocate memory for xattr name, value, index node */
942 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200943 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700944 if (!newname)
945 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700946
947 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600948 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700949 if (!newval)
950 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700951 }
952
953 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
954 if (!xattr)
955 goto out;
956
Sage Weilbe655592011-11-30 09:47:09 -0800957 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700958retry:
959 issued = __ceph_caps_issued(ci, NULL);
Alex Elder18fa8b32012-01-23 15:49:28 -0600960 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Yan, Zheng508b32d2014-09-16 21:46:17 +0800961 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -0700962 goto do_sync;
963 __build_xattrs(inode);
964
965 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
966
967 if (!ci->i_xattrs.prealloc_blob ||
968 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -0600969 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700970
Sage Weilbe655592011-11-30 09:47:09 -0800971 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700972 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800973 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700974 if (!blob)
975 goto out;
Sage Weilbe655592011-11-30 09:47:09 -0800976 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800977 if (ci->i_xattrs.prealloc_blob)
978 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700979 ci->i_xattrs.prealloc_blob = blob;
980 goto retry;
981 }
982
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800983 err = __set_xattr(ci, newname, name_len, newval, val_len,
984 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -0600985
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800986 if (!err) {
987 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
988 ci->i_xattrs.dirty = true;
989 inode->i_ctime = CURRENT_TIME;
990 }
Alex Elder18fa8b32012-01-23 15:49:28 -0600991
Sage Weilbe655592011-11-30 09:47:09 -0800992 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700993 if (dirty)
994 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700995 return err;
996
997do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800998 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -0800999do_sync_unlocked:
Sage Weil355da1e2009-10-06 11:31:08 -07001000 err = ceph_sync_setxattr(dentry, name, value, size, flags);
1001out:
1002 kfree(newname);
1003 kfree(newval);
1004 kfree(xattr);
1005 return err;
1006}
1007
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001008int ceph_setxattr(struct dentry *dentry, const char *name,
1009 const void *value, size_t size, int flags)
1010{
1011 if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1012 return -EROFS;
1013
1014 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1015 return generic_setxattr(dentry, name, value, size, flags);
1016
1017 return __ceph_setxattr(dentry, name, value, size, flags);
1018}
1019
Sage Weil355da1e2009-10-06 11:31:08 -07001020static int ceph_send_removexattr(struct dentry *dentry, const char *name)
1021{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001022 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
1023 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001024 struct inode *inode = dentry->d_inode;
Sage Weil355da1e2009-10-06 11:31:08 -07001025 struct ceph_mds_request *req;
1026 int err;
1027
1028 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
1029 USE_AUTH_MDS);
1030 if (IS_ERR(req))
1031 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -07001032 req->r_inode = inode;
1033 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001034 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1035 req->r_num_caps = 1;
1036 req->r_path2 = kstrdup(name, GFP_NOFS);
1037
Sage Weil752c8bd2013-02-05 13:52:29 -08001038 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -07001039 ceph_mdsc_put_request(req);
1040 return err;
1041}
1042
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001043int __ceph_removexattr(struct dentry *dentry, const char *name)
Sage Weil355da1e2009-10-06 11:31:08 -07001044{
1045 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -06001046 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -07001047 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001048 int issued;
1049 int err;
Alex Elder83eb26a2012-01-11 17:41:01 -08001050 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -07001051 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -07001052
Sage Weil355da1e2009-10-06 11:31:08 -07001053 if (!ceph_is_valid_xattr(name))
1054 return -EOPNOTSUPP;
1055
Alex Elder06476a62012-01-23 15:49:27 -06001056 vxattr = ceph_match_vxattr(inode, name);
1057 if (vxattr && vxattr->readonly)
1058 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -07001059
Sage Weild421acb2013-01-20 21:55:30 -08001060 /* pass any unhandled ceph.* xattrs through to the MDS */
1061 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1062 goto do_sync_unlocked;
1063
Alex Elder83eb26a2012-01-11 17:41:01 -08001064 err = -ENOMEM;
Sage Weilbe655592011-11-30 09:47:09 -08001065 spin_lock(&ci->i_ceph_lock);
Alex Elder83eb26a2012-01-11 17:41:01 -08001066retry:
Sage Weil355da1e2009-10-06 11:31:08 -07001067 issued = __ceph_caps_issued(ci, NULL);
1068 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
1069
Yan, Zheng508b32d2014-09-16 21:46:17 +08001070 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -07001071 goto do_sync;
Alex Elder18fa8b32012-01-23 15:49:28 -06001072 __build_xattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001073
Alex Elder83eb26a2012-01-11 17:41:01 -08001074 required_blob_size = __get_required_blob_size(ci, 0, 0);
1075
1076 if (!ci->i_xattrs.prealloc_blob ||
1077 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1078 struct ceph_buffer *blob;
1079
1080 spin_unlock(&ci->i_ceph_lock);
1081 dout(" preaallocating new blob size=%d\n", required_blob_size);
1082 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1083 if (!blob)
1084 goto out;
1085 spin_lock(&ci->i_ceph_lock);
1086 if (ci->i_xattrs.prealloc_blob)
1087 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1088 ci->i_xattrs.prealloc_blob = blob;
1089 goto retry;
1090 }
1091
Sage Weil355da1e2009-10-06 11:31:08 -07001092 err = __remove_xattr_by_name(ceph_inode(inode), name);
Alex Elder18fa8b32012-01-23 15:49:28 -06001093
Sage Weilfca65b42011-05-04 11:33:47 -07001094 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -07001095 ci->i_xattrs.dirty = true;
1096 inode->i_ctime = CURRENT_TIME;
Sage Weilbe655592011-11-30 09:47:09 -08001097 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -07001098 if (dirty)
1099 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -07001100 return err;
1101do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001102 spin_unlock(&ci->i_ceph_lock);
Sage Weild421acb2013-01-20 21:55:30 -08001103do_sync_unlocked:
Sage Weil355da1e2009-10-06 11:31:08 -07001104 err = ceph_send_removexattr(dentry, name);
Alex Elder83eb26a2012-01-11 17:41:01 -08001105out:
Sage Weil355da1e2009-10-06 11:31:08 -07001106 return err;
1107}
1108
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001109int ceph_removexattr(struct dentry *dentry, const char *name)
1110{
1111 if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1112 return -EROFS;
1113
1114 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1115 return generic_removexattr(dentry, name);
1116
1117 return __ceph_removexattr(dentry, name);
1118}