blob: 4870b29df22460dcf1617e32fe04a7283de96a07 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002#include <linux/ceph/pagelist.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003
Sage Weil355da1e2009-10-06 11:31:08 -07004#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005#include "mds_client.h"
6
7#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -07008
9#include <linux/xattr.h>
Linus Torvalds4db658e2014-01-28 18:06:18 -080010#include <linux/posix_acl_xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Sage Weil355da1e2009-10-06 11:31:08 -070012
Alex Elder22891902012-01-23 15:49:28 -060013#define XATTR_CEPH_PREFIX "ceph."
14#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
15
Yan, Zhengbcdfeb22014-02-11 13:04:19 +080016static int __remove_xattr(struct ceph_inode_info *ci,
17 struct ceph_inode_xattr *xattr);
18
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +020019const struct xattr_handler ceph_other_xattr_handler;
20
Guangliang Zhao7221fe42013-11-11 15:18:03 +080021/*
22 * List of handlers for synthetic system.* attributes. Other
23 * attributes are handled directly.
24 */
25const struct xattr_handler *ceph_xattr_handlers[] = {
26#ifdef CONFIG_CEPH_FS_POSIX_ACL
Linus Torvalds4db658e2014-01-28 18:06:18 -080027 &posix_acl_access_xattr_handler,
28 &posix_acl_default_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080029#endif
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +020030 &ceph_other_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080031 NULL,
32};
33
Sage Weil355da1e2009-10-06 11:31:08 -070034static bool ceph_is_valid_xattr(const char *name)
35{
Alex Elder22891902012-01-23 15:49:28 -060036 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil1a756272010-05-11 11:40:25 -070037 !strncmp(name, XATTR_SECURITY_PREFIX,
Sage Weil355da1e2009-10-06 11:31:08 -070038 XATTR_SECURITY_PREFIX_LEN) ||
39 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
40 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
41}
42
43/*
44 * These define virtual xattrs exposing the recursive directory
45 * statistics and layout metadata.
46 */
Alex Elder881a5fa2012-01-23 15:49:28 -060047struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070048 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060049 size_t name_size; /* strlen(name) + 1 (for '\0') */
Sage Weil355da1e2009-10-06 11:31:08 -070050 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
51 size_t size);
Sage Weil88601472013-01-31 11:53:27 -080052 bool readonly, hidden;
Sage Weilf36e4472013-01-20 21:59:29 -080053 bool (*exists_cb)(struct ceph_inode_info *ci);
Sage Weil355da1e2009-10-06 11:31:08 -070054};
55
Sage Weil32ab0bd2013-01-19 16:46:32 -080056/* layouts */
57
58static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
59{
60 size_t s;
61 char *p = (char *)&ci->i_layout;
62
63 for (s = 0; s < sizeof(ci->i_layout); s++, p++)
64 if (*p)
65 return true;
66 return false;
67}
68
69static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
Yan, Zheng1e5c6642014-03-24 13:00:54 +080070 size_t size)
Sage Weil32ab0bd2013-01-19 16:46:32 -080071{
72 int ret;
73 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
74 struct ceph_osd_client *osdc = &fsc->client->osdc;
75 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
76 const char *pool_name;
Yan, Zheng1e5c6642014-03-24 13:00:54 +080077 char buf[128];
Sage Weil32ab0bd2013-01-19 16:46:32 -080078
79 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020080 down_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -080081 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080082 if (pool_name) {
83 size_t len = strlen(pool_name);
84 ret = snprintf(buf, sizeof(buf),
85 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=",
Sage Weil32ab0bd2013-01-19 16:46:32 -080086 (unsigned long long)ceph_file_layout_su(ci->i_layout),
87 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
Yan, Zheng1e5c6642014-03-24 13:00:54 +080088 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
89 if (!size) {
90 ret += len;
91 } else if (ret + len > size) {
92 ret = -ERANGE;
93 } else {
94 memcpy(val, buf, ret);
95 memcpy(val + ret, pool_name, len);
96 ret += len;
97 }
98 } else {
99 ret = snprintf(buf, sizeof(buf),
Sage Weil32ab0bd2013-01-19 16:46:32 -0800100 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%lld",
101 (unsigned long long)ceph_file_layout_su(ci->i_layout),
102 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
103 (unsigned long long)ceph_file_layout_object_size(ci->i_layout),
104 (unsigned long long)pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +0800105 if (size) {
106 if (ret <= size)
107 memcpy(val, buf, ret);
108 else
109 ret = -ERANGE;
110 }
111 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200112 up_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -0800113 return ret;
114}
115
Sage Weil695b7112013-01-20 22:08:33 -0800116static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
117 char *val, size_t size)
118{
119 return snprintf(val, size, "%lld",
120 (unsigned long long)ceph_file_layout_su(ci->i_layout));
121}
122
123static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
124 char *val, size_t size)
125{
126 return snprintf(val, size, "%lld",
127 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout));
128}
129
130static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
131 char *val, size_t size)
132{
133 return snprintf(val, size, "%lld",
134 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
135}
136
137static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
138 char *val, size_t size)
139{
140 int ret;
141 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
142 struct ceph_osd_client *osdc = &fsc->client->osdc;
143 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
144 const char *pool_name;
145
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200146 down_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800147 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
148 if (pool_name)
149 ret = snprintf(val, size, "%s", pool_name);
150 else
151 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200152 up_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800153 return ret;
154}
155
Sage Weil355da1e2009-10-06 11:31:08 -0700156/* directories */
157
Alex Elderaa4066e2012-01-23 15:49:28 -0600158static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700159 size_t size)
160{
161 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
162}
163
Alex Elderaa4066e2012-01-23 15:49:28 -0600164static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700165 size_t size)
166{
167 return snprintf(val, size, "%lld", ci->i_files);
168}
169
Alex Elderaa4066e2012-01-23 15:49:28 -0600170static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700171 size_t size)
172{
173 return snprintf(val, size, "%lld", ci->i_subdirs);
174}
175
Alex Elderaa4066e2012-01-23 15:49:28 -0600176static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700177 size_t size)
178{
179 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
180}
181
Alex Elderaa4066e2012-01-23 15:49:28 -0600182static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700183 size_t size)
184{
185 return snprintf(val, size, "%lld", ci->i_rfiles);
186}
187
Alex Elderaa4066e2012-01-23 15:49:28 -0600188static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700189 size_t size)
190{
191 return snprintf(val, size, "%lld", ci->i_rsubdirs);
192}
193
Alex Elderaa4066e2012-01-23 15:49:28 -0600194static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700195 size_t size)
196{
197 return snprintf(val, size, "%lld", ci->i_rbytes);
198}
199
Alex Elderaa4066e2012-01-23 15:49:28 -0600200static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700201 size_t size)
202{
Alex Elder3489b422012-03-08 16:50:09 -0600203 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
Sage Weil355da1e2009-10-06 11:31:08 -0700204 (long)ci->i_rctime.tv_nsec);
205}
206
Sage Weil32ab0bd2013-01-19 16:46:32 -0800207
Alex Eldereb788082012-01-23 15:49:28 -0600208#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800209#define CEPH_XATTR_NAME2(_type, _name, _name2) \
210 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600211
Sage Weil88601472013-01-31 11:53:27 -0800212#define XATTR_NAME_CEPH(_type, _name) \
213 { \
214 .name = CEPH_XATTR_NAME(_type, _name), \
215 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
216 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
217 .readonly = true, \
218 .hidden = false, \
Sage Weilf36e4472013-01-20 21:59:29 -0800219 .exists_cb = NULL, \
Sage Weil88601472013-01-31 11:53:27 -0800220 }
Sage Weil695b7112013-01-20 22:08:33 -0800221#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
222 { \
223 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
224 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
225 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
226 .readonly = false, \
227 .hidden = true, \
228 .exists_cb = ceph_vxattrcb_layout_exists, \
229 }
Alex Eldereb788082012-01-23 15:49:28 -0600230
Alex Elder881a5fa2012-01-23 15:49:28 -0600231static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800232 {
233 .name = "ceph.dir.layout",
234 .name_size = sizeof("ceph.dir.layout"),
235 .getxattr_cb = ceph_vxattrcb_layout,
236 .readonly = false,
Yan, Zhengcc48c3e2014-03-24 19:15:05 +0800237 .hidden = true,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800238 .exists_cb = ceph_vxattrcb_layout_exists,
239 },
Sage Weil695b7112013-01-20 22:08:33 -0800240 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
241 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
242 XATTR_LAYOUT_FIELD(dir, layout, object_size),
243 XATTR_LAYOUT_FIELD(dir, layout, pool),
Alex Eldereb788082012-01-23 15:49:28 -0600244 XATTR_NAME_CEPH(dir, entries),
245 XATTR_NAME_CEPH(dir, files),
246 XATTR_NAME_CEPH(dir, subdirs),
247 XATTR_NAME_CEPH(dir, rentries),
248 XATTR_NAME_CEPH(dir, rfiles),
249 XATTR_NAME_CEPH(dir, rsubdirs),
250 XATTR_NAME_CEPH(dir, rbytes),
251 XATTR_NAME_CEPH(dir, rctime),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600252 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700253};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600254static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700255
256/* files */
257
Alex Elder881a5fa2012-01-23 15:49:28 -0600258static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800259 {
260 .name = "ceph.file.layout",
261 .name_size = sizeof("ceph.file.layout"),
262 .getxattr_cb = ceph_vxattrcb_layout,
263 .readonly = false,
Yan, Zhengcc48c3e2014-03-24 19:15:05 +0800264 .hidden = true,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800265 .exists_cb = ceph_vxattrcb_layout_exists,
266 },
Sage Weil695b7112013-01-20 22:08:33 -0800267 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
268 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
269 XATTR_LAYOUT_FIELD(file, layout, object_size),
270 XATTR_LAYOUT_FIELD(file, layout, pool),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600271 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700272};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600273static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700274
Alex Elder881a5fa2012-01-23 15:49:28 -0600275static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700276{
277 if (S_ISDIR(inode->i_mode))
278 return ceph_dir_vxattrs;
279 else if (S_ISREG(inode->i_mode))
280 return ceph_file_vxattrs;
281 return NULL;
282}
283
Alex Elder3ce6cd12012-01-23 15:49:28 -0600284static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
285{
286 if (vxattrs == ceph_dir_vxattrs)
287 return ceph_dir_vxattrs_name_size;
288 if (vxattrs == ceph_file_vxattrs)
289 return ceph_file_vxattrs_name_size;
Yan, Zheng0abb43d2014-09-18 16:11:12 +0800290 BUG_ON(vxattrs);
Alex Elder3ce6cd12012-01-23 15:49:28 -0600291 return 0;
292}
293
294/*
295 * Compute the aggregate size (including terminating '\0') of all
296 * virtual extended attribute names in the given vxattr table.
297 */
298static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
299{
300 struct ceph_vxattr *vxattr;
301 size_t size = 0;
302
303 for (vxattr = vxattrs; vxattr->name; vxattr++)
Sage Weil88601472013-01-31 11:53:27 -0800304 if (!vxattr->hidden)
305 size += vxattr->name_size;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600306
307 return size;
308}
309
310/* Routines called at initialization and exit time */
311
312void __init ceph_xattr_init(void)
313{
314 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
315 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
316}
317
318void ceph_xattr_exit(void)
319{
320 ceph_dir_vxattrs_name_size = 0;
321 ceph_file_vxattrs_name_size = 0;
322}
323
Alex Elder881a5fa2012-01-23 15:49:28 -0600324static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700325 const char *name)
326{
Alex Elder881a5fa2012-01-23 15:49:28 -0600327 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600328
329 if (vxattr) {
330 while (vxattr->name) {
331 if (!strcmp(vxattr->name, name))
332 return vxattr;
333 vxattr++;
334 }
335 }
336
Sage Weil355da1e2009-10-06 11:31:08 -0700337 return NULL;
338}
339
340static int __set_xattr(struct ceph_inode_info *ci,
341 const char *name, int name_len,
342 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800343 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700344 struct ceph_inode_xattr **newxattr)
345{
346 struct rb_node **p;
347 struct rb_node *parent = NULL;
348 struct ceph_inode_xattr *xattr = NULL;
349 int c;
350 int new = 0;
351
352 p = &ci->i_xattrs.index.rb_node;
353 while (*p) {
354 parent = *p;
355 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
356 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
357 if (c < 0)
358 p = &(*p)->rb_left;
359 else if (c > 0)
360 p = &(*p)->rb_right;
361 else {
362 if (name_len == xattr->name_len)
363 break;
364 else if (name_len < xattr->name_len)
365 p = &(*p)->rb_left;
366 else
367 p = &(*p)->rb_right;
368 }
369 xattr = NULL;
370 }
371
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800372 if (update_xattr) {
373 int err = 0;
374 if (xattr && (flags & XATTR_CREATE))
375 err = -EEXIST;
376 else if (!xattr && (flags & XATTR_REPLACE))
377 err = -ENODATA;
378 if (err) {
379 kfree(name);
380 kfree(val);
381 return err;
382 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800383 if (update_xattr < 0) {
384 if (xattr)
385 __remove_xattr(ci, xattr);
386 kfree(name);
387 return 0;
388 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800389 }
390
Sage Weil355da1e2009-10-06 11:31:08 -0700391 if (!xattr) {
392 new = 1;
393 xattr = *newxattr;
394 xattr->name = name;
395 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800396 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700397
398 ci->i_xattrs.count++;
399 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
400 } else {
401 kfree(*newxattr);
402 *newxattr = NULL;
403 if (xattr->should_free_val)
404 kfree((void *)xattr->val);
405
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800406 if (update_xattr) {
Sage Weil355da1e2009-10-06 11:31:08 -0700407 kfree((void *)name);
408 name = xattr->name;
409 }
410 ci->i_xattrs.names_size -= xattr->name_len;
411 ci->i_xattrs.vals_size -= xattr->val_len;
412 }
Sage Weil355da1e2009-10-06 11:31:08 -0700413 ci->i_xattrs.names_size += name_len;
414 ci->i_xattrs.vals_size += val_len;
415 if (val)
416 xattr->val = val;
417 else
418 xattr->val = "";
419
420 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800421 xattr->dirty = update_xattr;
422 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700423
424 if (new) {
425 rb_link_node(&xattr->node, parent, p);
426 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
427 dout("__set_xattr_val p=%p\n", p);
428 }
429
430 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
431 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
432
433 return 0;
434}
435
436static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
437 const char *name)
438{
439 struct rb_node **p;
440 struct rb_node *parent = NULL;
441 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800442 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700443 int c;
444
445 p = &ci->i_xattrs.index.rb_node;
446 while (*p) {
447 parent = *p;
448 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
449 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800450 if (c == 0 && name_len > xattr->name_len)
451 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700452 if (c < 0)
453 p = &(*p)->rb_left;
454 else if (c > 0)
455 p = &(*p)->rb_right;
456 else {
457 dout("__get_xattr %s: found %.*s\n", name,
458 xattr->val_len, xattr->val);
459 return xattr;
460 }
461 }
462
463 dout("__get_xattr %s: not found\n", name);
464
465 return NULL;
466}
467
468static void __free_xattr(struct ceph_inode_xattr *xattr)
469{
470 BUG_ON(!xattr);
471
472 if (xattr->should_free_name)
473 kfree((void *)xattr->name);
474 if (xattr->should_free_val)
475 kfree((void *)xattr->val);
476
477 kfree(xattr);
478}
479
480static int __remove_xattr(struct ceph_inode_info *ci,
481 struct ceph_inode_xattr *xattr)
482{
483 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800484 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700485
486 rb_erase(&xattr->node, &ci->i_xattrs.index);
487
488 if (xattr->should_free_name)
489 kfree((void *)xattr->name);
490 if (xattr->should_free_val)
491 kfree((void *)xattr->val);
492
493 ci->i_xattrs.names_size -= xattr->name_len;
494 ci->i_xattrs.vals_size -= xattr->val_len;
495 ci->i_xattrs.count--;
496 kfree(xattr);
497
498 return 0;
499}
500
Sage Weil355da1e2009-10-06 11:31:08 -0700501static char *__copy_xattr_names(struct ceph_inode_info *ci,
502 char *dest)
503{
504 struct rb_node *p;
505 struct ceph_inode_xattr *xattr = NULL;
506
507 p = rb_first(&ci->i_xattrs.index);
508 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
509
510 while (p) {
511 xattr = rb_entry(p, struct ceph_inode_xattr, node);
512 memcpy(dest, xattr->name, xattr->name_len);
513 dest[xattr->name_len] = '\0';
514
515 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
516 xattr->name_len, ci->i_xattrs.names_size);
517
518 dest += xattr->name_len + 1;
519 p = rb_next(p);
520 }
521
522 return dest;
523}
524
525void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
526{
527 struct rb_node *p, *tmp;
528 struct ceph_inode_xattr *xattr = NULL;
529
530 p = rb_first(&ci->i_xattrs.index);
531
532 dout("__ceph_destroy_xattrs p=%p\n", p);
533
534 while (p) {
535 xattr = rb_entry(p, struct ceph_inode_xattr, node);
536 tmp = p;
537 p = rb_next(tmp);
538 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
539 xattr->name_len, xattr->name);
540 rb_erase(tmp, &ci->i_xattrs.index);
541
542 __free_xattr(xattr);
543 }
544
545 ci->i_xattrs.names_size = 0;
546 ci->i_xattrs.vals_size = 0;
547 ci->i_xattrs.index_version = 0;
548 ci->i_xattrs.count = 0;
549 ci->i_xattrs.index = RB_ROOT;
550}
551
552static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800553 __releases(ci->i_ceph_lock)
554 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700555{
556 u32 namelen;
557 u32 numattr = 0;
558 void *p, *end;
559 u32 len;
560 const char *name, *val;
561 struct ceph_inode_info *ci = ceph_inode(inode);
562 int xattr_version;
563 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800564 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700565 int i;
566
567 dout("__build_xattrs() len=%d\n",
568 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
569
570 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
571 return 0; /* already built */
572
573 __ceph_destroy_xattrs(ci);
574
575start:
576 /* updated internal xattr rb tree */
577 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
578 p = ci->i_xattrs.blob->vec.iov_base;
579 end = p + ci->i_xattrs.blob->vec.iov_len;
580 ceph_decode_32_safe(&p, end, numattr, bad);
581 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800582 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700583
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400584 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700585 GFP_NOFS);
586 err = -ENOMEM;
587 if (!xattrs)
588 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400589
Sage Weil355da1e2009-10-06 11:31:08 -0700590 for (i = 0; i < numattr; i++) {
591 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
592 GFP_NOFS);
593 if (!xattrs[i])
594 goto bad_lock;
595 }
596
Sage Weilbe655592011-11-30 09:47:09 -0800597 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700598 if (ci->i_xattrs.version != xattr_version) {
599 /* lost a race, retry */
600 for (i = 0; i < numattr; i++)
601 kfree(xattrs[i]);
602 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500603 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700604 goto start;
605 }
606 err = -EIO;
607 while (numattr--) {
608 ceph_decode_32_safe(&p, end, len, bad);
609 namelen = len;
610 name = p;
611 p += len;
612 ceph_decode_32_safe(&p, end, len, bad);
613 val = p;
614 p += len;
615
616 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800617 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700618
619 if (err < 0)
620 goto bad;
621 }
622 kfree(xattrs);
623 }
624 ci->i_xattrs.index_version = ci->i_xattrs.version;
625 ci->i_xattrs.dirty = false;
626
627 return err;
628bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800629 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700630bad:
631 if (xattrs) {
632 for (i = 0; i < numattr; i++)
633 kfree(xattrs[i]);
634 kfree(xattrs);
635 }
636 ci->i_xattrs.names_size = 0;
637 return err;
638}
639
640static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
641 int val_size)
642{
643 /*
644 * 4 bytes for the length, and additional 4 bytes per each xattr name,
645 * 4 bytes per each value
646 */
647 int size = 4 + ci->i_xattrs.count*(4 + 4) +
648 ci->i_xattrs.names_size +
649 ci->i_xattrs.vals_size;
650 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
651 ci->i_xattrs.count, ci->i_xattrs.names_size,
652 ci->i_xattrs.vals_size);
653
654 if (name_size)
655 size += 4 + 4 + name_size + val_size;
656
657 return size;
658}
659
660/*
661 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
662 * and swap into place.
663 */
664void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
665{
666 struct rb_node *p;
667 struct ceph_inode_xattr *xattr = NULL;
668 void *dest;
669
670 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
671 if (ci->i_xattrs.dirty) {
672 int need = __get_required_blob_size(ci, 0, 0);
673
674 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
675
676 p = rb_first(&ci->i_xattrs.index);
677 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
678
679 ceph_encode_32(&dest, ci->i_xattrs.count);
680 while (p) {
681 xattr = rb_entry(p, struct ceph_inode_xattr, node);
682
683 ceph_encode_32(&dest, xattr->name_len);
684 memcpy(dest, xattr->name, xattr->name_len);
685 dest += xattr->name_len;
686 ceph_encode_32(&dest, xattr->val_len);
687 memcpy(dest, xattr->val, xattr->val_len);
688 dest += xattr->val_len;
689
690 p = rb_next(p);
691 }
692
693 /* adjust buffer len; it may be larger than we need */
694 ci->i_xattrs.prealloc_blob->vec.iov_len =
695 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
696
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800697 if (ci->i_xattrs.blob)
698 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700699 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
700 ci->i_xattrs.prealloc_blob = NULL;
701 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700702 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700703 }
704}
705
Yan, Zheng315f2402016-03-07 10:34:50 +0800706static inline int __get_request_mask(struct inode *in) {
707 struct ceph_mds_request *req = current->journal_info;
708 int mask = 0;
709 if (req && req->r_target_inode == in) {
710 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
711 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
712 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
713 req->r_op == CEPH_MDS_OP_GETATTR) {
714 mask = le32_to_cpu(req->r_args.getattr.mask);
715 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
716 req->r_op == CEPH_MDS_OP_CREATE) {
717 mask = le32_to_cpu(req->r_args.open.mask);
718 }
719 }
720 return mask;
721}
722
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800723ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700724 size_t size)
725{
Sage Weil355da1e2009-10-06 11:31:08 -0700726 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700727 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600728 struct ceph_vxattr *vxattr = NULL;
Yan, Zheng315f2402016-03-07 10:34:50 +0800729 int req_mask;
730 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700731
Sage Weil0bee82f2013-01-20 22:00:58 -0800732 /* let's see if a virtual xattr was requested */
733 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800734 if (vxattr) {
735 err = -ENODATA;
736 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
737 err = vxattr->getxattr_cb(ci, value, size);
majianpenga1dc1932013-06-19 14:58:10 +0800738 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800739 }
740
Yan, Zheng315f2402016-03-07 10:34:50 +0800741 req_mask = __get_request_mask(inode);
742
majianpenga1dc1932013-06-19 14:58:10 +0800743 spin_lock(&ci->i_ceph_lock);
744 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
745 ci->i_xattrs.version, ci->i_xattrs.index_version);
746
Yan, Zheng508b32d2014-09-16 21:46:17 +0800747 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800748 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
749 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800750 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800751
752 /* security module gets xattr while filling trace */
753 if (current->journal_info != NULL) {
754 pr_warn_ratelimited("sync getxattr %p "
755 "during filling trace\n", inode);
756 return -EBUSY;
757 }
758
Sage Weil355da1e2009-10-06 11:31:08 -0700759 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800760 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700761 if (err)
762 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800763 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700764 }
765
Sage Weil355da1e2009-10-06 11:31:08 -0700766 err = __build_xattrs(inode);
767 if (err < 0)
768 goto out;
769
Sage Weil355da1e2009-10-06 11:31:08 -0700770 err = -ENODATA; /* == ENOATTR */
771 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800772 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700773 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700774
775 err = -ERANGE;
776 if (size && size < xattr->val_len)
777 goto out;
778
779 err = xattr->val_len;
780 if (size == 0)
781 goto out;
782
783 memcpy(value, xattr->val, xattr->val_len);
784
Yan, Zheng315f2402016-03-07 10:34:50 +0800785 if (current->journal_info != NULL &&
786 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
787 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -0700788out:
Sage Weilbe655592011-11-30 09:47:09 -0800789 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700790 return err;
791}
792
793ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
794{
David Howells2b0143b2015-03-17 22:25:59 +0000795 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700796 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600797 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700798 u32 vir_namelen = 0;
799 u32 namelen;
800 int err;
801 u32 len;
802 int i;
803
Sage Weilbe655592011-11-30 09:47:09 -0800804 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700805 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
806 ci->i_xattrs.version, ci->i_xattrs.index_version);
807
Yan, Zheng508b32d2014-09-16 21:46:17 +0800808 if (ci->i_xattrs.version == 0 ||
809 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800810 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800811 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700812 if (err)
813 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800814 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700815 }
816
Sage Weil355da1e2009-10-06 11:31:08 -0700817 err = __build_xattrs(inode);
818 if (err < 0)
819 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600820 /*
821 * Start with virtual dir xattr names (if any) (including
822 * terminating '\0' characters for each).
823 */
824 vir_namelen = ceph_vxattrs_name_size(vxattrs);
825
Sage Weil355da1e2009-10-06 11:31:08 -0700826 /* adding 1 byte per each variable due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -0800827 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
Sage Weil355da1e2009-10-06 11:31:08 -0700828 err = -ERANGE;
Sage Weilb65917d2013-01-20 22:02:39 -0800829 if (size && vir_namelen + namelen > size)
Sage Weil355da1e2009-10-06 11:31:08 -0700830 goto out;
831
Sage Weilb65917d2013-01-20 22:02:39 -0800832 err = namelen + vir_namelen;
Sage Weil355da1e2009-10-06 11:31:08 -0700833 if (size == 0)
834 goto out;
835
836 names = __copy_xattr_names(ci, names);
837
838 /* virtual xattr names, too */
Sage Weilb65917d2013-01-20 22:02:39 -0800839 err = namelen;
840 if (vxattrs) {
Sage Weil355da1e2009-10-06 11:31:08 -0700841 for (i = 0; vxattrs[i].name; i++) {
Sage Weilb65917d2013-01-20 22:02:39 -0800842 if (!vxattrs[i].hidden &&
843 !(vxattrs[i].exists_cb &&
844 !vxattrs[i].exists_cb(ci))) {
845 len = sprintf(names, "%s", vxattrs[i].name);
846 names += len + 1;
847 err += len + 1;
848 }
Sage Weil355da1e2009-10-06 11:31:08 -0700849 }
Sage Weilb65917d2013-01-20 22:02:39 -0800850 }
Sage Weil355da1e2009-10-06 11:31:08 -0700851
852out:
Sage Weilbe655592011-11-30 09:47:09 -0800853 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700854 return err;
855}
856
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200857static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -0700858 const char *value, size_t size, int flags)
859{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200860 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700861 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700862 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700863 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800864 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +0800865 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -0700866 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700867
Yan, Zheng0aeff372014-12-17 21:26:47 +0800868 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800869 /* copy value into pagelist */
870 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
871 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -0700872 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800873
874 ceph_pagelist_init(pagelist);
875 err = ceph_pagelist_append(pagelist, value, size);
876 if (err)
877 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +0800878 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +0800879 if (flags & CEPH_XATTR_REPLACE)
880 op = CEPH_MDS_OP_RMXATTR;
881 else
882 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -0700883 }
884
885 dout("setxattr value=%.*s\n", (int)size, value);
886
887 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +0800888 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100889 if (IS_ERR(req)) {
890 err = PTR_ERR(req);
891 goto out;
892 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400893
Sage Weil355da1e2009-10-06 11:31:08 -0700894 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400895 if (!req->r_path2) {
896 ceph_mdsc_put_request(req);
897 err = -ENOMEM;
898 goto out;
899 }
Sage Weil355da1e2009-10-06 11:31:08 -0700900
Yan, Zheng04303d82016-04-21 12:11:54 +0800901 if (op == CEPH_MDS_OP_SETXATTR) {
902 req->r_args.setxattr.flags = cpu_to_le32(flags);
903 req->r_pagelist = pagelist;
904 pagelist = NULL;
905 }
Sage Weil355da1e2009-10-06 11:31:08 -0700906
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400907 req->r_inode = inode;
908 ihold(inode);
909 req->r_num_caps = 1;
910 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
911
Sage Weil355da1e2009-10-06 11:31:08 -0700912 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -0800913 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -0700914 ceph_mdsc_put_request(req);
915 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
916
917out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800918 if (pagelist)
919 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -0700920 return err;
921}
922
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200923int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800924 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -0700925{
Alex Elder881a5fa2012-01-23 15:49:28 -0600926 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700927 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200928 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +0800929 struct ceph_cap_flush *prealloc_cf = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -0600930 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700931 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800932 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700933 int name_len = strlen(name);
934 int val_len = size;
935 char *newname = NULL;
936 char *newval = NULL;
937 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700938 int required_blob_size;
Yan, Zheng604d1b02015-05-01 17:49:16 +0800939 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -0700940
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +0200941 if (ceph_snap(inode) != CEPH_NOSNAP)
942 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -0700943
Alex Elder06476a62012-01-23 15:49:27 -0600944 vxattr = ceph_match_vxattr(inode, name);
945 if (vxattr && vxattr->readonly)
946 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700947
Sage Weil3adf6542013-01-31 11:53:41 -0800948 /* pass any unhandled ceph.* xattrs through to the MDS */
949 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
950 goto do_sync_unlocked;
951
Sage Weil355da1e2009-10-06 11:31:08 -0700952 /* preallocate memory for xattr name, value, index node */
953 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200954 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700955 if (!newname)
956 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700957
958 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600959 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700960 if (!newval)
961 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700962 }
963
964 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
965 if (!xattr)
966 goto out;
967
Yan, Zhengf66fd9f2015-06-10 17:26:13 +0800968 prealloc_cf = ceph_alloc_cap_flush();
969 if (!prealloc_cf)
970 goto out;
971
Sage Weilbe655592011-11-30 09:47:09 -0800972 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700973retry:
974 issued = __ceph_caps_issued(ci, NULL);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800975 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -0700976 goto do_sync;
Yan, Zheng604d1b02015-05-01 17:49:16 +0800977
978 if (!lock_snap_rwsem && !ci->i_head_snapc) {
979 lock_snap_rwsem = true;
980 if (!down_read_trylock(&mdsc->snap_rwsem)) {
981 spin_unlock(&ci->i_ceph_lock);
982 down_read(&mdsc->snap_rwsem);
983 spin_lock(&ci->i_ceph_lock);
984 goto retry;
985 }
986 }
987
988 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -0700989 __build_xattrs(inode);
990
991 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
992
993 if (!ci->i_xattrs.prealloc_blob ||
994 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -0600995 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700996
Sage Weilbe655592011-11-30 09:47:09 -0800997 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700998 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800999 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001000 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +08001001 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -08001002 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001003 if (ci->i_xattrs.prealloc_blob)
1004 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -07001005 ci->i_xattrs.prealloc_blob = blob;
1006 goto retry;
1007 }
1008
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001009 err = __set_xattr(ci, newname, name_len, newval, val_len,
1010 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001011
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001012 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001013 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1014 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001015 ci->i_xattrs.dirty = true;
Deepa Dinamani8bbd4712016-02-02 22:07:48 -08001016 inode->i_ctime = current_fs_time(inode->i_sb);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001017 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001018
Sage Weilbe655592011-11-30 09:47:09 -08001019 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001020 if (lock_snap_rwsem)
1021 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001022 if (dirty)
1023 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001024 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001025 return err;
1026
1027do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001028 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001029do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001030 if (lock_snap_rwsem)
1031 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001032
1033 /* security module set xattr while filling trace */
1034 if (current->journal_info != NULL) {
1035 pr_warn_ratelimited("sync setxattr %p "
1036 "during filling trace\n", inode);
1037 err = -EBUSY;
1038 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001039 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zheng315f2402016-03-07 10:34:50 +08001040 }
Sage Weil355da1e2009-10-06 11:31:08 -07001041out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001042 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001043 kfree(newname);
1044 kfree(newval);
1045 kfree(xattr);
1046 return err;
1047}
1048
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001049static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1050 struct dentry *dentry, struct inode *inode,
1051 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001052{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001053 if (!ceph_is_valid_xattr(name))
1054 return -EOPNOTSUPP;
1055 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001056}
Yan, Zheng315f2402016-03-07 10:34:50 +08001057
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001058static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04001059 struct dentry *unused, struct inode *inode,
1060 const char *name, const void *value,
1061 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001062{
1063 if (!ceph_is_valid_xattr(name))
1064 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001065 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001066}
1067
1068const struct xattr_handler ceph_other_xattr_handler = {
1069 .prefix = "", /* match any name => handlers called with full name */
1070 .get = ceph_get_xattr_handler,
1071 .set = ceph_set_xattr_handler,
1072};
1073
Yan, Zheng315f2402016-03-07 10:34:50 +08001074#ifdef CONFIG_SECURITY
1075bool ceph_security_xattr_wanted(struct inode *in)
1076{
1077 return in->i_security != NULL;
1078}
1079
1080bool ceph_security_xattr_deadlock(struct inode *in)
1081{
1082 struct ceph_inode_info *ci;
1083 bool ret;
1084 if (in->i_security == NULL)
1085 return false;
1086 ci = ceph_inode(in);
1087 spin_lock(&ci->i_ceph_lock);
1088 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1089 !(ci->i_xattrs.version > 0 &&
1090 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1091 spin_unlock(&ci->i_ceph_lock);
1092 return ret;
1093}
1094#endif