blob: 5377c9c7a0c51980a5565f6a21be0c9639d8f12f [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;
Yan, Zheng76271512016-02-03 21:24:49 +080075 s64 pool = ci->i_layout.pool_id;
Sage Weil32ab0bd2013-01-19 16:46:32 -080076 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),
Yan, Zheng76271512016-02-03 21:24:49 +080085 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
86 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
87 ci->i_layout.object_size);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080088 if (!size) {
89 ret += len;
90 } else if (ret + len > size) {
91 ret = -ERANGE;
92 } else {
93 memcpy(val, buf, ret);
94 memcpy(val + ret, pool_name, len);
95 ret += len;
96 }
97 } else {
98 ret = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080099 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
100 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
101 ci->i_layout.object_size, (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 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200109 up_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -0800110 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{
Yan, Zheng76271512016-02-03 21:24:49 +0800116 return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
Sage Weil695b7112013-01-20 22:08:33 -0800117}
118
119static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
120 char *val, size_t size)
121{
Yan, Zheng76271512016-02-03 21:24:49 +0800122 return snprintf(val, size, "%u", ci->i_layout.stripe_count);
Sage Weil695b7112013-01-20 22:08:33 -0800123}
124
125static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
126 char *val, size_t size)
127{
Yan, Zheng76271512016-02-03 21:24:49 +0800128 return snprintf(val, size, "%u", ci->i_layout.object_size);
Sage Weil695b7112013-01-20 22:08:33 -0800129}
130
131static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
132 char *val, size_t size)
133{
134 int ret;
135 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
136 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng76271512016-02-03 21:24:49 +0800137 s64 pool = ci->i_layout.pool_id;
Sage Weil695b7112013-01-20 22:08:33 -0800138 const char *pool_name;
139
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200140 down_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800141 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
142 if (pool_name)
143 ret = snprintf(val, size, "%s", pool_name);
144 else
145 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200146 up_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800147 return ret;
148}
149
Sage Weil355da1e2009-10-06 11:31:08 -0700150/* directories */
151
Alex Elderaa4066e2012-01-23 15:49:28 -0600152static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700153 size_t size)
154{
155 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
156}
157
Alex Elderaa4066e2012-01-23 15:49:28 -0600158static size_t ceph_vxattrcb_dir_files(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);
162}
163
Alex Elderaa4066e2012-01-23 15:49:28 -0600164static size_t ceph_vxattrcb_dir_subdirs(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_subdirs);
168}
169
Alex Elderaa4066e2012-01-23 15:49:28 -0600170static size_t ceph_vxattrcb_dir_rentries(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_rfiles + ci->i_rsubdirs);
174}
175
Alex Elderaa4066e2012-01-23 15:49:28 -0600176static size_t ceph_vxattrcb_dir_rfiles(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);
180}
181
Alex Elderaa4066e2012-01-23 15:49:28 -0600182static size_t ceph_vxattrcb_dir_rsubdirs(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_rsubdirs);
186}
187
Alex Elderaa4066e2012-01-23 15:49:28 -0600188static size_t ceph_vxattrcb_dir_rbytes(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_rbytes);
192}
193
Alex Elderaa4066e2012-01-23 15:49:28 -0600194static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700195 size_t size)
196{
Alex Elder3489b422012-03-08 16:50:09 -0600197 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
Sage Weil355da1e2009-10-06 11:31:08 -0700198 (long)ci->i_rctime.tv_nsec);
199}
200
Sage Weil32ab0bd2013-01-19 16:46:32 -0800201
Alex Eldereb788082012-01-23 15:49:28 -0600202#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800203#define CEPH_XATTR_NAME2(_type, _name, _name2) \
204 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600205
Sage Weil88601472013-01-31 11:53:27 -0800206#define XATTR_NAME_CEPH(_type, _name) \
207 { \
208 .name = CEPH_XATTR_NAME(_type, _name), \
209 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
210 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
211 .readonly = true, \
212 .hidden = false, \
Sage Weilf36e4472013-01-20 21:59:29 -0800213 .exists_cb = NULL, \
Sage Weil88601472013-01-31 11:53:27 -0800214 }
Sage Weil695b7112013-01-20 22:08:33 -0800215#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
216 { \
217 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
218 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
219 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
220 .readonly = false, \
221 .hidden = true, \
222 .exists_cb = ceph_vxattrcb_layout_exists, \
223 }
Alex Eldereb788082012-01-23 15:49:28 -0600224
Alex Elder881a5fa2012-01-23 15:49:28 -0600225static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800226 {
227 .name = "ceph.dir.layout",
228 .name_size = sizeof("ceph.dir.layout"),
229 .getxattr_cb = ceph_vxattrcb_layout,
230 .readonly = false,
Yan, Zhengcc48c3e2014-03-24 19:15:05 +0800231 .hidden = true,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800232 .exists_cb = ceph_vxattrcb_layout_exists,
233 },
Sage Weil695b7112013-01-20 22:08:33 -0800234 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
235 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
236 XATTR_LAYOUT_FIELD(dir, layout, object_size),
237 XATTR_LAYOUT_FIELD(dir, layout, pool),
Alex Eldereb788082012-01-23 15:49:28 -0600238 XATTR_NAME_CEPH(dir, entries),
239 XATTR_NAME_CEPH(dir, files),
240 XATTR_NAME_CEPH(dir, subdirs),
241 XATTR_NAME_CEPH(dir, rentries),
242 XATTR_NAME_CEPH(dir, rfiles),
243 XATTR_NAME_CEPH(dir, rsubdirs),
244 XATTR_NAME_CEPH(dir, rbytes),
245 XATTR_NAME_CEPH(dir, rctime),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600246 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700247};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600248static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700249
250/* files */
251
Alex Elder881a5fa2012-01-23 15:49:28 -0600252static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800253 {
254 .name = "ceph.file.layout",
255 .name_size = sizeof("ceph.file.layout"),
256 .getxattr_cb = ceph_vxattrcb_layout,
257 .readonly = false,
Yan, Zhengcc48c3e2014-03-24 19:15:05 +0800258 .hidden = true,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800259 .exists_cb = ceph_vxattrcb_layout_exists,
260 },
Sage Weil695b7112013-01-20 22:08:33 -0800261 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
262 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
263 XATTR_LAYOUT_FIELD(file, layout, object_size),
264 XATTR_LAYOUT_FIELD(file, layout, pool),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600265 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700266};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600267static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700268
Alex Elder881a5fa2012-01-23 15:49:28 -0600269static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700270{
271 if (S_ISDIR(inode->i_mode))
272 return ceph_dir_vxattrs;
273 else if (S_ISREG(inode->i_mode))
274 return ceph_file_vxattrs;
275 return NULL;
276}
277
Alex Elder3ce6cd12012-01-23 15:49:28 -0600278static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
279{
280 if (vxattrs == ceph_dir_vxattrs)
281 return ceph_dir_vxattrs_name_size;
282 if (vxattrs == ceph_file_vxattrs)
283 return ceph_file_vxattrs_name_size;
Yan, Zheng0abb43d2014-09-18 16:11:12 +0800284 BUG_ON(vxattrs);
Alex Elder3ce6cd12012-01-23 15:49:28 -0600285 return 0;
286}
287
288/*
289 * Compute the aggregate size (including terminating '\0') of all
290 * virtual extended attribute names in the given vxattr table.
291 */
292static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
293{
294 struct ceph_vxattr *vxattr;
295 size_t size = 0;
296
297 for (vxattr = vxattrs; vxattr->name; vxattr++)
Sage Weil88601472013-01-31 11:53:27 -0800298 if (!vxattr->hidden)
299 size += vxattr->name_size;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600300
301 return size;
302}
303
304/* Routines called at initialization and exit time */
305
306void __init ceph_xattr_init(void)
307{
308 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
309 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
310}
311
312void ceph_xattr_exit(void)
313{
314 ceph_dir_vxattrs_name_size = 0;
315 ceph_file_vxattrs_name_size = 0;
316}
317
Alex Elder881a5fa2012-01-23 15:49:28 -0600318static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700319 const char *name)
320{
Alex Elder881a5fa2012-01-23 15:49:28 -0600321 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600322
323 if (vxattr) {
324 while (vxattr->name) {
325 if (!strcmp(vxattr->name, name))
326 return vxattr;
327 vxattr++;
328 }
329 }
330
Sage Weil355da1e2009-10-06 11:31:08 -0700331 return NULL;
332}
333
334static int __set_xattr(struct ceph_inode_info *ci,
335 const char *name, int name_len,
336 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800337 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700338 struct ceph_inode_xattr **newxattr)
339{
340 struct rb_node **p;
341 struct rb_node *parent = NULL;
342 struct ceph_inode_xattr *xattr = NULL;
343 int c;
344 int new = 0;
345
346 p = &ci->i_xattrs.index.rb_node;
347 while (*p) {
348 parent = *p;
349 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
350 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
351 if (c < 0)
352 p = &(*p)->rb_left;
353 else if (c > 0)
354 p = &(*p)->rb_right;
355 else {
356 if (name_len == xattr->name_len)
357 break;
358 else if (name_len < xattr->name_len)
359 p = &(*p)->rb_left;
360 else
361 p = &(*p)->rb_right;
362 }
363 xattr = NULL;
364 }
365
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800366 if (update_xattr) {
367 int err = 0;
368 if (xattr && (flags & XATTR_CREATE))
369 err = -EEXIST;
370 else if (!xattr && (flags & XATTR_REPLACE))
371 err = -ENODATA;
372 if (err) {
373 kfree(name);
374 kfree(val);
375 return err;
376 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800377 if (update_xattr < 0) {
378 if (xattr)
379 __remove_xattr(ci, xattr);
380 kfree(name);
381 return 0;
382 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800383 }
384
Sage Weil355da1e2009-10-06 11:31:08 -0700385 if (!xattr) {
386 new = 1;
387 xattr = *newxattr;
388 xattr->name = name;
389 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800390 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700391
392 ci->i_xattrs.count++;
393 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
394 } else {
395 kfree(*newxattr);
396 *newxattr = NULL;
397 if (xattr->should_free_val)
398 kfree((void *)xattr->val);
399
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800400 if (update_xattr) {
Sage Weil355da1e2009-10-06 11:31:08 -0700401 kfree((void *)name);
402 name = xattr->name;
403 }
404 ci->i_xattrs.names_size -= xattr->name_len;
405 ci->i_xattrs.vals_size -= xattr->val_len;
406 }
Sage Weil355da1e2009-10-06 11:31:08 -0700407 ci->i_xattrs.names_size += name_len;
408 ci->i_xattrs.vals_size += val_len;
409 if (val)
410 xattr->val = val;
411 else
412 xattr->val = "";
413
414 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800415 xattr->dirty = update_xattr;
416 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700417
418 if (new) {
419 rb_link_node(&xattr->node, parent, p);
420 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
421 dout("__set_xattr_val p=%p\n", p);
422 }
423
424 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
425 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
426
427 return 0;
428}
429
430static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
431 const char *name)
432{
433 struct rb_node **p;
434 struct rb_node *parent = NULL;
435 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800436 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700437 int c;
438
439 p = &ci->i_xattrs.index.rb_node;
440 while (*p) {
441 parent = *p;
442 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
443 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800444 if (c == 0 && name_len > xattr->name_len)
445 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700446 if (c < 0)
447 p = &(*p)->rb_left;
448 else if (c > 0)
449 p = &(*p)->rb_right;
450 else {
451 dout("__get_xattr %s: found %.*s\n", name,
452 xattr->val_len, xattr->val);
453 return xattr;
454 }
455 }
456
457 dout("__get_xattr %s: not found\n", name);
458
459 return NULL;
460}
461
462static void __free_xattr(struct ceph_inode_xattr *xattr)
463{
464 BUG_ON(!xattr);
465
466 if (xattr->should_free_name)
467 kfree((void *)xattr->name);
468 if (xattr->should_free_val)
469 kfree((void *)xattr->val);
470
471 kfree(xattr);
472}
473
474static int __remove_xattr(struct ceph_inode_info *ci,
475 struct ceph_inode_xattr *xattr)
476{
477 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800478 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700479
480 rb_erase(&xattr->node, &ci->i_xattrs.index);
481
482 if (xattr->should_free_name)
483 kfree((void *)xattr->name);
484 if (xattr->should_free_val)
485 kfree((void *)xattr->val);
486
487 ci->i_xattrs.names_size -= xattr->name_len;
488 ci->i_xattrs.vals_size -= xattr->val_len;
489 ci->i_xattrs.count--;
490 kfree(xattr);
491
492 return 0;
493}
494
Sage Weil355da1e2009-10-06 11:31:08 -0700495static char *__copy_xattr_names(struct ceph_inode_info *ci,
496 char *dest)
497{
498 struct rb_node *p;
499 struct ceph_inode_xattr *xattr = NULL;
500
501 p = rb_first(&ci->i_xattrs.index);
502 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
503
504 while (p) {
505 xattr = rb_entry(p, struct ceph_inode_xattr, node);
506 memcpy(dest, xattr->name, xattr->name_len);
507 dest[xattr->name_len] = '\0';
508
509 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
510 xattr->name_len, ci->i_xattrs.names_size);
511
512 dest += xattr->name_len + 1;
513 p = rb_next(p);
514 }
515
516 return dest;
517}
518
519void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
520{
521 struct rb_node *p, *tmp;
522 struct ceph_inode_xattr *xattr = NULL;
523
524 p = rb_first(&ci->i_xattrs.index);
525
526 dout("__ceph_destroy_xattrs p=%p\n", p);
527
528 while (p) {
529 xattr = rb_entry(p, struct ceph_inode_xattr, node);
530 tmp = p;
531 p = rb_next(tmp);
532 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
533 xattr->name_len, xattr->name);
534 rb_erase(tmp, &ci->i_xattrs.index);
535
536 __free_xattr(xattr);
537 }
538
539 ci->i_xattrs.names_size = 0;
540 ci->i_xattrs.vals_size = 0;
541 ci->i_xattrs.index_version = 0;
542 ci->i_xattrs.count = 0;
543 ci->i_xattrs.index = RB_ROOT;
544}
545
546static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800547 __releases(ci->i_ceph_lock)
548 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700549{
550 u32 namelen;
551 u32 numattr = 0;
552 void *p, *end;
553 u32 len;
554 const char *name, *val;
555 struct ceph_inode_info *ci = ceph_inode(inode);
556 int xattr_version;
557 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800558 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700559 int i;
560
561 dout("__build_xattrs() len=%d\n",
562 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
563
564 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
565 return 0; /* already built */
566
567 __ceph_destroy_xattrs(ci);
568
569start:
570 /* updated internal xattr rb tree */
571 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
572 p = ci->i_xattrs.blob->vec.iov_base;
573 end = p + ci->i_xattrs.blob->vec.iov_len;
574 ceph_decode_32_safe(&p, end, numattr, bad);
575 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800576 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700577
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400578 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700579 GFP_NOFS);
580 err = -ENOMEM;
581 if (!xattrs)
582 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400583
Sage Weil355da1e2009-10-06 11:31:08 -0700584 for (i = 0; i < numattr; i++) {
585 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
586 GFP_NOFS);
587 if (!xattrs[i])
588 goto bad_lock;
589 }
590
Sage Weilbe655592011-11-30 09:47:09 -0800591 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700592 if (ci->i_xattrs.version != xattr_version) {
593 /* lost a race, retry */
594 for (i = 0; i < numattr; i++)
595 kfree(xattrs[i]);
596 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500597 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700598 goto start;
599 }
600 err = -EIO;
601 while (numattr--) {
602 ceph_decode_32_safe(&p, end, len, bad);
603 namelen = len;
604 name = p;
605 p += len;
606 ceph_decode_32_safe(&p, end, len, bad);
607 val = p;
608 p += len;
609
610 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800611 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700612
613 if (err < 0)
614 goto bad;
615 }
616 kfree(xattrs);
617 }
618 ci->i_xattrs.index_version = ci->i_xattrs.version;
619 ci->i_xattrs.dirty = false;
620
621 return err;
622bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800623 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700624bad:
625 if (xattrs) {
626 for (i = 0; i < numattr; i++)
627 kfree(xattrs[i]);
628 kfree(xattrs);
629 }
630 ci->i_xattrs.names_size = 0;
631 return err;
632}
633
634static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
635 int val_size)
636{
637 /*
638 * 4 bytes for the length, and additional 4 bytes per each xattr name,
639 * 4 bytes per each value
640 */
641 int size = 4 + ci->i_xattrs.count*(4 + 4) +
642 ci->i_xattrs.names_size +
643 ci->i_xattrs.vals_size;
644 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
645 ci->i_xattrs.count, ci->i_xattrs.names_size,
646 ci->i_xattrs.vals_size);
647
648 if (name_size)
649 size += 4 + 4 + name_size + val_size;
650
651 return size;
652}
653
654/*
655 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
656 * and swap into place.
657 */
658void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
659{
660 struct rb_node *p;
661 struct ceph_inode_xattr *xattr = NULL;
662 void *dest;
663
664 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
665 if (ci->i_xattrs.dirty) {
666 int need = __get_required_blob_size(ci, 0, 0);
667
668 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
669
670 p = rb_first(&ci->i_xattrs.index);
671 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
672
673 ceph_encode_32(&dest, ci->i_xattrs.count);
674 while (p) {
675 xattr = rb_entry(p, struct ceph_inode_xattr, node);
676
677 ceph_encode_32(&dest, xattr->name_len);
678 memcpy(dest, xattr->name, xattr->name_len);
679 dest += xattr->name_len;
680 ceph_encode_32(&dest, xattr->val_len);
681 memcpy(dest, xattr->val, xattr->val_len);
682 dest += xattr->val_len;
683
684 p = rb_next(p);
685 }
686
687 /* adjust buffer len; it may be larger than we need */
688 ci->i_xattrs.prealloc_blob->vec.iov_len =
689 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
690
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800691 if (ci->i_xattrs.blob)
692 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700693 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
694 ci->i_xattrs.prealloc_blob = NULL;
695 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700696 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700697 }
698}
699
Yan, Zheng315f2402016-03-07 10:34:50 +0800700static inline int __get_request_mask(struct inode *in) {
701 struct ceph_mds_request *req = current->journal_info;
702 int mask = 0;
703 if (req && req->r_target_inode == in) {
704 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
705 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
706 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
707 req->r_op == CEPH_MDS_OP_GETATTR) {
708 mask = le32_to_cpu(req->r_args.getattr.mask);
709 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
710 req->r_op == CEPH_MDS_OP_CREATE) {
711 mask = le32_to_cpu(req->r_args.open.mask);
712 }
713 }
714 return mask;
715}
716
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800717ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700718 size_t size)
719{
Sage Weil355da1e2009-10-06 11:31:08 -0700720 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700721 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600722 struct ceph_vxattr *vxattr = NULL;
Yan, Zheng315f2402016-03-07 10:34:50 +0800723 int req_mask;
724 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700725
Sage Weil0bee82f2013-01-20 22:00:58 -0800726 /* let's see if a virtual xattr was requested */
727 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800728 if (vxattr) {
729 err = -ENODATA;
730 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
731 err = vxattr->getxattr_cb(ci, value, size);
majianpenga1dc1932013-06-19 14:58:10 +0800732 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800733 }
734
Yan, Zheng315f2402016-03-07 10:34:50 +0800735 req_mask = __get_request_mask(inode);
736
majianpenga1dc1932013-06-19 14:58:10 +0800737 spin_lock(&ci->i_ceph_lock);
738 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
739 ci->i_xattrs.version, ci->i_xattrs.index_version);
740
Yan, Zheng508b32d2014-09-16 21:46:17 +0800741 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800742 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
743 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800744 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800745
746 /* security module gets xattr while filling trace */
747 if (current->journal_info != NULL) {
748 pr_warn_ratelimited("sync getxattr %p "
749 "during filling trace\n", inode);
750 return -EBUSY;
751 }
752
Sage Weil355da1e2009-10-06 11:31:08 -0700753 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800754 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700755 if (err)
756 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800757 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700758 }
759
Sage Weil355da1e2009-10-06 11:31:08 -0700760 err = __build_xattrs(inode);
761 if (err < 0)
762 goto out;
763
Sage Weil355da1e2009-10-06 11:31:08 -0700764 err = -ENODATA; /* == ENOATTR */
765 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800766 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700767 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700768
769 err = -ERANGE;
770 if (size && size < xattr->val_len)
771 goto out;
772
773 err = xattr->val_len;
774 if (size == 0)
775 goto out;
776
777 memcpy(value, xattr->val, xattr->val_len);
778
Yan, Zheng315f2402016-03-07 10:34:50 +0800779 if (current->journal_info != NULL &&
780 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
781 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -0700782out:
Sage Weilbe655592011-11-30 09:47:09 -0800783 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700784 return err;
785}
786
787ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
788{
David Howells2b0143b2015-03-17 22:25:59 +0000789 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700790 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600791 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700792 u32 vir_namelen = 0;
793 u32 namelen;
794 int err;
795 u32 len;
796 int i;
797
Sage Weilbe655592011-11-30 09:47:09 -0800798 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700799 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
800 ci->i_xattrs.version, ci->i_xattrs.index_version);
801
Yan, Zheng508b32d2014-09-16 21:46:17 +0800802 if (ci->i_xattrs.version == 0 ||
803 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800804 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800805 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700806 if (err)
807 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800808 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700809 }
810
Sage Weil355da1e2009-10-06 11:31:08 -0700811 err = __build_xattrs(inode);
812 if (err < 0)
813 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600814 /*
815 * Start with virtual dir xattr names (if any) (including
816 * terminating '\0' characters for each).
817 */
818 vir_namelen = ceph_vxattrs_name_size(vxattrs);
819
Sage Weil355da1e2009-10-06 11:31:08 -0700820 /* adding 1 byte per each variable due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -0800821 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
Sage Weil355da1e2009-10-06 11:31:08 -0700822 err = -ERANGE;
Sage Weilb65917d2013-01-20 22:02:39 -0800823 if (size && vir_namelen + namelen > size)
Sage Weil355da1e2009-10-06 11:31:08 -0700824 goto out;
825
Sage Weilb65917d2013-01-20 22:02:39 -0800826 err = namelen + vir_namelen;
Sage Weil355da1e2009-10-06 11:31:08 -0700827 if (size == 0)
828 goto out;
829
830 names = __copy_xattr_names(ci, names);
831
832 /* virtual xattr names, too */
Sage Weilb65917d2013-01-20 22:02:39 -0800833 err = namelen;
834 if (vxattrs) {
Sage Weil355da1e2009-10-06 11:31:08 -0700835 for (i = 0; vxattrs[i].name; i++) {
Sage Weilb65917d2013-01-20 22:02:39 -0800836 if (!vxattrs[i].hidden &&
837 !(vxattrs[i].exists_cb &&
838 !vxattrs[i].exists_cb(ci))) {
839 len = sprintf(names, "%s", vxattrs[i].name);
840 names += len + 1;
841 err += len + 1;
842 }
Sage Weil355da1e2009-10-06 11:31:08 -0700843 }
Sage Weilb65917d2013-01-20 22:02:39 -0800844 }
Sage Weil355da1e2009-10-06 11:31:08 -0700845
846out:
Sage Weilbe655592011-11-30 09:47:09 -0800847 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700848 return err;
849}
850
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200851static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -0700852 const char *value, size_t size, int flags)
853{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200854 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700855 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700856 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700857 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800858 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +0800859 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -0700860 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700861
Yan, Zheng0aeff372014-12-17 21:26:47 +0800862 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800863 /* copy value into pagelist */
864 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
865 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -0700866 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800867
868 ceph_pagelist_init(pagelist);
869 err = ceph_pagelist_append(pagelist, value, size);
870 if (err)
871 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +0800872 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +0800873 if (flags & CEPH_XATTR_REPLACE)
874 op = CEPH_MDS_OP_RMXATTR;
875 else
876 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -0700877 }
878
879 dout("setxattr value=%.*s\n", (int)size, value);
880
881 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +0800882 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100883 if (IS_ERR(req)) {
884 err = PTR_ERR(req);
885 goto out;
886 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400887
Sage Weil355da1e2009-10-06 11:31:08 -0700888 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400889 if (!req->r_path2) {
890 ceph_mdsc_put_request(req);
891 err = -ENOMEM;
892 goto out;
893 }
Sage Weil355da1e2009-10-06 11:31:08 -0700894
Yan, Zheng04303d82016-04-21 12:11:54 +0800895 if (op == CEPH_MDS_OP_SETXATTR) {
896 req->r_args.setxattr.flags = cpu_to_le32(flags);
897 req->r_pagelist = pagelist;
898 pagelist = NULL;
899 }
Sage Weil355da1e2009-10-06 11:31:08 -0700900
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400901 req->r_inode = inode;
902 ihold(inode);
903 req->r_num_caps = 1;
904 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
905
Sage Weil355da1e2009-10-06 11:31:08 -0700906 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -0800907 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -0700908 ceph_mdsc_put_request(req);
909 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
910
911out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800912 if (pagelist)
913 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -0700914 return err;
915}
916
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200917int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800918 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -0700919{
Alex Elder881a5fa2012-01-23 15:49:28 -0600920 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700921 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200922 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +0800923 struct ceph_cap_flush *prealloc_cf = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -0600924 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700925 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800926 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700927 int name_len = strlen(name);
928 int val_len = size;
929 char *newname = NULL;
930 char *newval = NULL;
931 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700932 int required_blob_size;
Yan, Zheng604d1b02015-05-01 17:49:16 +0800933 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -0700934
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +0200935 if (ceph_snap(inode) != CEPH_NOSNAP)
936 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -0700937
Alex Elder06476a62012-01-23 15:49:27 -0600938 vxattr = ceph_match_vxattr(inode, name);
939 if (vxattr && vxattr->readonly)
940 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700941
Sage Weil3adf6542013-01-31 11:53:41 -0800942 /* pass any unhandled ceph.* xattrs through to the MDS */
943 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
944 goto do_sync_unlocked;
945
Sage Weil355da1e2009-10-06 11:31:08 -0700946 /* preallocate memory for xattr name, value, index node */
947 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200948 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700949 if (!newname)
950 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700951
952 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600953 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700954 if (!newval)
955 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700956 }
957
958 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
959 if (!xattr)
960 goto out;
961
Yan, Zhengf66fd9f2015-06-10 17:26:13 +0800962 prealloc_cf = ceph_alloc_cap_flush();
963 if (!prealloc_cf)
964 goto out;
965
Sage Weilbe655592011-11-30 09:47:09 -0800966 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700967retry:
968 issued = __ceph_caps_issued(ci, NULL);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800969 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -0700970 goto do_sync;
Yan, Zheng604d1b02015-05-01 17:49:16 +0800971
972 if (!lock_snap_rwsem && !ci->i_head_snapc) {
973 lock_snap_rwsem = true;
974 if (!down_read_trylock(&mdsc->snap_rwsem)) {
975 spin_unlock(&ci->i_ceph_lock);
976 down_read(&mdsc->snap_rwsem);
977 spin_lock(&ci->i_ceph_lock);
978 goto retry;
979 }
980 }
981
982 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -0700983 __build_xattrs(inode);
984
985 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
986
987 if (!ci->i_xattrs.prealloc_blob ||
988 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -0600989 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700990
Sage Weilbe655592011-11-30 09:47:09 -0800991 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700992 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800993 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700994 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +0800995 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -0800996 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800997 if (ci->i_xattrs.prealloc_blob)
998 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700999 ci->i_xattrs.prealloc_blob = blob;
1000 goto retry;
1001 }
1002
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001003 err = __set_xattr(ci, newname, name_len, newval, val_len,
1004 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001005
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001006 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001007 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1008 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001009 ci->i_xattrs.dirty = true;
Deepa Dinamani8bbd4712016-02-02 22:07:48 -08001010 inode->i_ctime = current_fs_time(inode->i_sb);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001011 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001012
Sage Weilbe655592011-11-30 09:47:09 -08001013 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001014 if (lock_snap_rwsem)
1015 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001016 if (dirty)
1017 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001018 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001019 return err;
1020
1021do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001022 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001023do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001024 if (lock_snap_rwsem)
1025 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001026
1027 /* security module set xattr while filling trace */
1028 if (current->journal_info != NULL) {
1029 pr_warn_ratelimited("sync setxattr %p "
1030 "during filling trace\n", inode);
1031 err = -EBUSY;
1032 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001033 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zheng315f2402016-03-07 10:34:50 +08001034 }
Sage Weil355da1e2009-10-06 11:31:08 -07001035out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001036 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001037 kfree(newname);
1038 kfree(newval);
1039 kfree(xattr);
1040 return err;
1041}
1042
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001043static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1044 struct dentry *dentry, struct inode *inode,
1045 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001046{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001047 if (!ceph_is_valid_xattr(name))
1048 return -EOPNOTSUPP;
1049 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001050}
Yan, Zheng315f2402016-03-07 10:34:50 +08001051
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001052static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04001053 struct dentry *unused, struct inode *inode,
1054 const char *name, const void *value,
1055 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001056{
1057 if (!ceph_is_valid_xattr(name))
1058 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001059 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001060}
1061
1062const struct xattr_handler ceph_other_xattr_handler = {
1063 .prefix = "", /* match any name => handlers called with full name */
1064 .get = ceph_get_xattr_handler,
1065 .set = ceph_set_xattr_handler,
1066};
1067
Yan, Zheng315f2402016-03-07 10:34:50 +08001068#ifdef CONFIG_SECURITY
1069bool ceph_security_xattr_wanted(struct inode *in)
1070{
1071 return in->i_security != NULL;
1072}
1073
1074bool ceph_security_xattr_deadlock(struct inode *in)
1075{
1076 struct ceph_inode_info *ci;
1077 bool ret;
1078 if (in->i_security == NULL)
1079 return false;
1080 ci = ceph_inode(in);
1081 spin_lock(&ci->i_ceph_lock);
1082 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1083 !(ci->i_xattrs.version > 0 &&
1084 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1085 spin_unlock(&ci->i_ceph_lock);
1086 return ret;
1087}
1088#endif