blob: d2fb9f10720cf81382f50277f8d77cdf1d02f2aa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Yan, Zheng25e6bae2014-09-16 19:15:28 +08003#include <linux/ceph/pagelist.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004
Sage Weil355da1e2009-10-06 11:31:08 -07005#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07006#include "mds_client.h"
7
8#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -07009
10#include <linux/xattr.h>
Linus Torvalds4db658e2014-01-28 18:06:18 -080011#include <linux/posix_acl_xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Sage Weil355da1e2009-10-06 11:31:08 -070013
Alex Elder22891902012-01-23 15:49:28 -060014#define XATTR_CEPH_PREFIX "ceph."
15#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
16
Yan, Zhengbcdfeb22014-02-11 13:04:19 +080017static int __remove_xattr(struct ceph_inode_info *ci,
18 struct ceph_inode_xattr *xattr);
19
Wei Yongjun5130cce2016-10-17 15:11:29 +000020static const struct xattr_handler ceph_other_xattr_handler;
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +020021
Guangliang Zhao7221fe42013-11-11 15:18:03 +080022/*
23 * List of handlers for synthetic system.* attributes. Other
24 * attributes are handled directly.
25 */
26const struct xattr_handler *ceph_xattr_handlers[] = {
27#ifdef CONFIG_CEPH_FS_POSIX_ACL
Linus Torvalds4db658e2014-01-28 18:06:18 -080028 &posix_acl_access_xattr_handler,
29 &posix_acl_default_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080030#endif
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +020031 &ceph_other_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080032 NULL,
33};
34
Sage Weil355da1e2009-10-06 11:31:08 -070035static bool ceph_is_valid_xattr(const char *name)
36{
Alex Elder22891902012-01-23 15:49:28 -060037 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil1a756272010-05-11 11:40:25 -070038 !strncmp(name, XATTR_SECURITY_PREFIX,
Sage Weil355da1e2009-10-06 11:31:08 -070039 XATTR_SECURITY_PREFIX_LEN) ||
40 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
41 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
42}
43
44/*
45 * These define virtual xattrs exposing the recursive directory
46 * statistics and layout metadata.
47 */
Alex Elder881a5fa2012-01-23 15:49:28 -060048struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070049 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060050 size_t name_size; /* strlen(name) + 1 (for '\0') */
Sage Weil355da1e2009-10-06 11:31:08 -070051 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
52 size_t size);
Sage Weilf36e4472013-01-20 21:59:29 -080053 bool (*exists_cb)(struct ceph_inode_info *ci);
Yan, Zheng4e9906e2018-04-25 17:14:05 +080054 unsigned int flags;
Sage Weil355da1e2009-10-06 11:31:08 -070055};
56
Yan, Zheng4e9906e2018-04-25 17:14:05 +080057#define VXATTR_FLAG_READONLY (1<<0)
58#define VXATTR_FLAG_HIDDEN (1<<1)
Yan, Zheng49a9f4f2018-04-25 17:30:23 +080059#define VXATTR_FLAG_RSTAT (1<<2)
Yan, Zheng4e9906e2018-04-25 17:14:05 +080060
Sage Weil32ab0bd2013-01-19 16:46:32 -080061/* layouts */
62
63static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
64{
Yan, Zheng779fe0f2016-03-07 09:35:06 +080065 struct ceph_file_layout *fl = &ci->i_layout;
66 return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
67 fl->object_size > 0 || fl->pool_id >= 0 ||
68 rcu_dereference_raw(fl->pool_ns) != NULL);
Sage Weil32ab0bd2013-01-19 16:46:32 -080069}
70
71static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
Yan, Zheng1e5c6642014-03-24 13:00:54 +080072 size_t size)
Sage Weil32ab0bd2013-01-19 16:46:32 -080073{
Sage Weil32ab0bd2013-01-19 16:46:32 -080074 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
75 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080076 struct ceph_string *pool_ns;
Yan, Zheng76271512016-02-03 21:24:49 +080077 s64 pool = ci->i_layout.pool_id;
Sage Weil32ab0bd2013-01-19 16:46:32 -080078 const char *pool_name;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080079 const char *ns_field = " pool_namespace=";
Yan, Zheng1e5c6642014-03-24 13:00:54 +080080 char buf[128];
Yan, Zheng779fe0f2016-03-07 09:35:06 +080081 size_t len, total_len = 0;
82 int ret;
83
84 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -080085
86 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020087 down_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -080088 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080089 if (pool_name) {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080090 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080091 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
92 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
93 ci->i_layout.object_size);
Yan, Zheng779fe0f2016-03-07 09:35:06 +080094 total_len = len + strlen(pool_name);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080095 } else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080096 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080097 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
98 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
99 ci->i_layout.object_size, (unsigned long long)pool);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800100 total_len = len;
101 }
102
103 if (pool_ns)
104 total_len += strlen(ns_field) + pool_ns->len;
105
106 if (!size) {
107 ret = total_len;
108 } else if (total_len > size) {
109 ret = -ERANGE;
110 } else {
111 memcpy(val, buf, len);
112 ret = len;
113 if (pool_name) {
114 len = strlen(pool_name);
115 memcpy(val + ret, pool_name, len);
116 ret += len;
117 }
118 if (pool_ns) {
119 len = strlen(ns_field);
120 memcpy(val + ret, ns_field, len);
121 ret += len;
122 memcpy(val + ret, pool_ns->str, pool_ns->len);
123 ret += pool_ns->len;
Yan, Zheng1e5c6642014-03-24 13:00:54 +0800124 }
125 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200126 up_read(&osdc->lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800127 ceph_put_string(pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -0800128 return ret;
129}
130
Sage Weil695b7112013-01-20 22:08:33 -0800131static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
132 char *val, size_t size)
133{
Yan, Zheng76271512016-02-03 21:24:49 +0800134 return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
Sage Weil695b7112013-01-20 22:08:33 -0800135}
136
137static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
138 char *val, size_t size)
139{
Yan, Zheng76271512016-02-03 21:24:49 +0800140 return snprintf(val, size, "%u", ci->i_layout.stripe_count);
Sage Weil695b7112013-01-20 22:08:33 -0800141}
142
143static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
144 char *val, size_t size)
145{
Yan, Zheng76271512016-02-03 21:24:49 +0800146 return snprintf(val, size, "%u", ci->i_layout.object_size);
Sage Weil695b7112013-01-20 22:08:33 -0800147}
148
149static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
150 char *val, size_t size)
151{
152 int ret;
153 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
154 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng76271512016-02-03 21:24:49 +0800155 s64 pool = ci->i_layout.pool_id;
Sage Weil695b7112013-01-20 22:08:33 -0800156 const char *pool_name;
157
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200158 down_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800159 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
160 if (pool_name)
161 ret = snprintf(val, size, "%s", pool_name);
162 else
163 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200164 up_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800165 return ret;
166}
167
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800168static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
169 char *val, size_t size)
170{
171 int ret = 0;
172 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
173 if (ns) {
174 ret = snprintf(val, size, "%.*s", (int)ns->len, ns->str);
175 ceph_put_string(ns);
176 }
177 return ret;
178}
179
Sage Weil355da1e2009-10-06 11:31:08 -0700180/* directories */
181
Alex Elderaa4066e2012-01-23 15:49:28 -0600182static size_t ceph_vxattrcb_dir_entries(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_files + ci->i_subdirs);
186}
187
Alex Elderaa4066e2012-01-23 15:49:28 -0600188static size_t ceph_vxattrcb_dir_files(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_files);
192}
193
Alex Elderaa4066e2012-01-23 15:49:28 -0600194static size_t ceph_vxattrcb_dir_subdirs(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_subdirs);
198}
199
Alex Elderaa4066e2012-01-23 15:49:28 -0600200static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700201 size_t size)
202{
203 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
204}
205
Alex Elderaa4066e2012-01-23 15:49:28 -0600206static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700207 size_t size)
208{
209 return snprintf(val, size, "%lld", ci->i_rfiles);
210}
211
Alex Elderaa4066e2012-01-23 15:49:28 -0600212static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700213 size_t size)
214{
215 return snprintf(val, size, "%lld", ci->i_rsubdirs);
216}
217
Alex Elderaa4066e2012-01-23 15:49:28 -0600218static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700219 size_t size)
220{
221 return snprintf(val, size, "%lld", ci->i_rbytes);
222}
223
Alex Elderaa4066e2012-01-23 15:49:28 -0600224static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700225 size_t size)
226{
David Disseldorp71880722019-05-15 16:56:39 +0200227 return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
Arnd Bergmann9bbeab42018-07-13 22:18:36 +0200228 ci->i_rctime.tv_nsec);
Sage Weil355da1e2009-10-06 11:31:08 -0700229}
230
Yan, Zheng08796872019-01-09 11:07:02 +0800231/* dir pin */
232static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
233{
234 return ci->i_dir_pin != -ENODATA;
235}
Luis Henriquesfb18a572018-01-05 10:47:18 +0000236
Yan, Zheng08796872019-01-09 11:07:02 +0800237static size_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
238 size_t size)
239{
240 return snprintf(val, size, "%d", (int)ci->i_dir_pin);
241}
242
243/* quotas */
Luis Henriquesfb18a572018-01-05 10:47:18 +0000244static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
245{
Yan, Zhengf1919822018-04-08 09:54:39 +0800246 bool ret = false;
247 spin_lock(&ci->i_ceph_lock);
248 if ((ci->i_max_files || ci->i_max_bytes) &&
249 ci->i_vino.snap == CEPH_NOSNAP &&
250 ci->i_snap_realm &&
251 ci->i_snap_realm->ino == ci->i_vino.ino)
252 ret = true;
253 spin_unlock(&ci->i_ceph_lock);
254 return ret;
Luis Henriquesfb18a572018-01-05 10:47:18 +0000255}
256
257static size_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
258 size_t size)
259{
260 return snprintf(val, size, "max_bytes=%llu max_files=%llu",
261 ci->i_max_bytes, ci->i_max_files);
262}
263
264static size_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
265 char *val, size_t size)
266{
267 return snprintf(val, size, "%llu", ci->i_max_bytes);
268}
269
270static size_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
271 char *val, size_t size)
272{
273 return snprintf(val, size, "%llu", ci->i_max_files);
274}
Sage Weil32ab0bd2013-01-19 16:46:32 -0800275
David Disseldorp100cc612019-04-18 14:15:47 +0200276/* snapshots */
277static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
278{
279 return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
280}
281
282static size_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
283 size_t size)
284{
285 return snprintf(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
286 ci->i_snap_btime.tv_nsec);
287}
288
Alex Eldereb788082012-01-23 15:49:28 -0600289#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800290#define CEPH_XATTR_NAME2(_type, _name, _name2) \
291 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600292
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800293#define XATTR_NAME_CEPH(_type, _name, _flags) \
Sage Weil88601472013-01-31 11:53:27 -0800294 { \
295 .name = CEPH_XATTR_NAME(_type, _name), \
296 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
297 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800298 .exists_cb = NULL, \
299 .flags = (VXATTR_FLAG_READONLY | _flags), \
Sage Weil88601472013-01-31 11:53:27 -0800300 }
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800301#define XATTR_RSTAT_FIELD(_type, _name) \
302 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
Sage Weil695b7112013-01-20 22:08:33 -0800303#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
304 { \
305 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
306 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
307 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
Sage Weil695b7112013-01-20 22:08:33 -0800308 .exists_cb = ceph_vxattrcb_layout_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800309 .flags = VXATTR_FLAG_HIDDEN, \
Sage Weil695b7112013-01-20 22:08:33 -0800310 }
Luis Henriquesfb18a572018-01-05 10:47:18 +0000311#define XATTR_QUOTA_FIELD(_type, _name) \
312 { \
313 .name = CEPH_XATTR_NAME(_type, _name), \
314 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
315 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000316 .exists_cb = ceph_vxattrcb_quota_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800317 .flags = VXATTR_FLAG_HIDDEN, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000318 }
Alex Eldereb788082012-01-23 15:49:28 -0600319
Alex Elder881a5fa2012-01-23 15:49:28 -0600320static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800321 {
322 .name = "ceph.dir.layout",
323 .name_size = sizeof("ceph.dir.layout"),
324 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800325 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800326 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800327 },
Sage Weil695b7112013-01-20 22:08:33 -0800328 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
329 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
330 XATTR_LAYOUT_FIELD(dir, layout, object_size),
331 XATTR_LAYOUT_FIELD(dir, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800332 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800333 XATTR_NAME_CEPH(dir, entries, 0),
334 XATTR_NAME_CEPH(dir, files, 0),
335 XATTR_NAME_CEPH(dir, subdirs, 0),
336 XATTR_RSTAT_FIELD(dir, rentries),
337 XATTR_RSTAT_FIELD(dir, rfiles),
338 XATTR_RSTAT_FIELD(dir, rsubdirs),
339 XATTR_RSTAT_FIELD(dir, rbytes),
340 XATTR_RSTAT_FIELD(dir, rctime),
Luis Henriquesfb18a572018-01-05 10:47:18 +0000341 {
Yan, Zheng08796872019-01-09 11:07:02 +0800342 .name = "ceph.dir.pin",
David Disseldorpe1b81432019-04-18 14:15:45 +0200343 .name_size = sizeof("ceph.dir.pin"),
Yan, Zheng08796872019-01-09 11:07:02 +0800344 .getxattr_cb = ceph_vxattrcb_dir_pin,
345 .exists_cb = ceph_vxattrcb_dir_pin_exists,
346 .flags = VXATTR_FLAG_HIDDEN,
347 },
348 {
Luis Henriquesfb18a572018-01-05 10:47:18 +0000349 .name = "ceph.quota",
350 .name_size = sizeof("ceph.quota"),
351 .getxattr_cb = ceph_vxattrcb_quota,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000352 .exists_cb = ceph_vxattrcb_quota_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800353 .flags = VXATTR_FLAG_HIDDEN,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000354 },
355 XATTR_QUOTA_FIELD(quota, max_bytes),
356 XATTR_QUOTA_FIELD(quota, max_files),
David Disseldorp100cc612019-04-18 14:15:47 +0200357 {
358 .name = "ceph.snap.btime",
359 .name_size = sizeof("ceph.snap.btime"),
360 .getxattr_cb = ceph_vxattrcb_snap_btime,
361 .exists_cb = ceph_vxattrcb_snap_btime_exists,
362 .flags = VXATTR_FLAG_READONLY,
363 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600364 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700365};
366
367/* files */
368
Alex Elder881a5fa2012-01-23 15:49:28 -0600369static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800370 {
371 .name = "ceph.file.layout",
372 .name_size = sizeof("ceph.file.layout"),
373 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800374 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800375 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800376 },
Sage Weil695b7112013-01-20 22:08:33 -0800377 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
378 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
379 XATTR_LAYOUT_FIELD(file, layout, object_size),
380 XATTR_LAYOUT_FIELD(file, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800381 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
David Disseldorp100cc612019-04-18 14:15:47 +0200382 {
383 .name = "ceph.snap.btime",
384 .name_size = sizeof("ceph.snap.btime"),
385 .getxattr_cb = ceph_vxattrcb_snap_btime,
386 .exists_cb = ceph_vxattrcb_snap_btime_exists,
387 .flags = VXATTR_FLAG_READONLY,
388 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600389 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700390};
391
Alex Elder881a5fa2012-01-23 15:49:28 -0600392static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700393{
394 if (S_ISDIR(inode->i_mode))
395 return ceph_dir_vxattrs;
396 else if (S_ISREG(inode->i_mode))
397 return ceph_file_vxattrs;
398 return NULL;
399}
400
Alex Elder881a5fa2012-01-23 15:49:28 -0600401static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700402 const char *name)
403{
Alex Elder881a5fa2012-01-23 15:49:28 -0600404 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600405
406 if (vxattr) {
407 while (vxattr->name) {
408 if (!strcmp(vxattr->name, name))
409 return vxattr;
410 vxattr++;
411 }
412 }
413
Sage Weil355da1e2009-10-06 11:31:08 -0700414 return NULL;
415}
416
417static int __set_xattr(struct ceph_inode_info *ci,
418 const char *name, int name_len,
419 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800420 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700421 struct ceph_inode_xattr **newxattr)
422{
423 struct rb_node **p;
424 struct rb_node *parent = NULL;
425 struct ceph_inode_xattr *xattr = NULL;
426 int c;
427 int new = 0;
428
429 p = &ci->i_xattrs.index.rb_node;
430 while (*p) {
431 parent = *p;
432 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
433 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
434 if (c < 0)
435 p = &(*p)->rb_left;
436 else if (c > 0)
437 p = &(*p)->rb_right;
438 else {
439 if (name_len == xattr->name_len)
440 break;
441 else if (name_len < xattr->name_len)
442 p = &(*p)->rb_left;
443 else
444 p = &(*p)->rb_right;
445 }
446 xattr = NULL;
447 }
448
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800449 if (update_xattr) {
450 int err = 0;
Luis Henriqueseeca9582017-04-28 11:14:04 +0100451
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800452 if (xattr && (flags & XATTR_CREATE))
453 err = -EEXIST;
454 else if (!xattr && (flags & XATTR_REPLACE))
455 err = -ENODATA;
456 if (err) {
457 kfree(name);
458 kfree(val);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100459 kfree(*newxattr);
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800460 return err;
461 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800462 if (update_xattr < 0) {
463 if (xattr)
464 __remove_xattr(ci, xattr);
465 kfree(name);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100466 kfree(*newxattr);
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800467 return 0;
468 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800469 }
470
Sage Weil355da1e2009-10-06 11:31:08 -0700471 if (!xattr) {
472 new = 1;
473 xattr = *newxattr;
474 xattr->name = name;
475 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800476 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700477
478 ci->i_xattrs.count++;
479 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
480 } else {
481 kfree(*newxattr);
482 *newxattr = NULL;
483 if (xattr->should_free_val)
484 kfree((void *)xattr->val);
485
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800486 if (update_xattr) {
Sage Weil355da1e2009-10-06 11:31:08 -0700487 kfree((void *)name);
488 name = xattr->name;
489 }
490 ci->i_xattrs.names_size -= xattr->name_len;
491 ci->i_xattrs.vals_size -= xattr->val_len;
492 }
Sage Weil355da1e2009-10-06 11:31:08 -0700493 ci->i_xattrs.names_size += name_len;
494 ci->i_xattrs.vals_size += val_len;
495 if (val)
496 xattr->val = val;
497 else
498 xattr->val = "";
499
500 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800501 xattr->dirty = update_xattr;
502 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700503
504 if (new) {
505 rb_link_node(&xattr->node, parent, p);
506 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
507 dout("__set_xattr_val p=%p\n", p);
508 }
509
510 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
511 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
512
513 return 0;
514}
515
516static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
517 const char *name)
518{
519 struct rb_node **p;
520 struct rb_node *parent = NULL;
521 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800522 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700523 int c;
524
525 p = &ci->i_xattrs.index.rb_node;
526 while (*p) {
527 parent = *p;
528 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
529 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800530 if (c == 0 && name_len > xattr->name_len)
531 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700532 if (c < 0)
533 p = &(*p)->rb_left;
534 else if (c > 0)
535 p = &(*p)->rb_right;
536 else {
537 dout("__get_xattr %s: found %.*s\n", name,
538 xattr->val_len, xattr->val);
539 return xattr;
540 }
541 }
542
543 dout("__get_xattr %s: not found\n", name);
544
545 return NULL;
546}
547
548static void __free_xattr(struct ceph_inode_xattr *xattr)
549{
550 BUG_ON(!xattr);
551
552 if (xattr->should_free_name)
553 kfree((void *)xattr->name);
554 if (xattr->should_free_val)
555 kfree((void *)xattr->val);
556
557 kfree(xattr);
558}
559
560static int __remove_xattr(struct ceph_inode_info *ci,
561 struct ceph_inode_xattr *xattr)
562{
563 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800564 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700565
566 rb_erase(&xattr->node, &ci->i_xattrs.index);
567
568 if (xattr->should_free_name)
569 kfree((void *)xattr->name);
570 if (xattr->should_free_val)
571 kfree((void *)xattr->val);
572
573 ci->i_xattrs.names_size -= xattr->name_len;
574 ci->i_xattrs.vals_size -= xattr->val_len;
575 ci->i_xattrs.count--;
576 kfree(xattr);
577
578 return 0;
579}
580
Sage Weil355da1e2009-10-06 11:31:08 -0700581static char *__copy_xattr_names(struct ceph_inode_info *ci,
582 char *dest)
583{
584 struct rb_node *p;
585 struct ceph_inode_xattr *xattr = NULL;
586
587 p = rb_first(&ci->i_xattrs.index);
588 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
589
590 while (p) {
591 xattr = rb_entry(p, struct ceph_inode_xattr, node);
592 memcpy(dest, xattr->name, xattr->name_len);
593 dest[xattr->name_len] = '\0';
594
595 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
596 xattr->name_len, ci->i_xattrs.names_size);
597
598 dest += xattr->name_len + 1;
599 p = rb_next(p);
600 }
601
602 return dest;
603}
604
605void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
606{
607 struct rb_node *p, *tmp;
608 struct ceph_inode_xattr *xattr = NULL;
609
610 p = rb_first(&ci->i_xattrs.index);
611
612 dout("__ceph_destroy_xattrs p=%p\n", p);
613
614 while (p) {
615 xattr = rb_entry(p, struct ceph_inode_xattr, node);
616 tmp = p;
617 p = rb_next(tmp);
618 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
619 xattr->name_len, xattr->name);
620 rb_erase(tmp, &ci->i_xattrs.index);
621
622 __free_xattr(xattr);
623 }
624
625 ci->i_xattrs.names_size = 0;
626 ci->i_xattrs.vals_size = 0;
627 ci->i_xattrs.index_version = 0;
628 ci->i_xattrs.count = 0;
629 ci->i_xattrs.index = RB_ROOT;
630}
631
632static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800633 __releases(ci->i_ceph_lock)
634 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700635{
636 u32 namelen;
637 u32 numattr = 0;
638 void *p, *end;
639 u32 len;
640 const char *name, *val;
641 struct ceph_inode_info *ci = ceph_inode(inode);
642 int xattr_version;
643 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800644 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700645 int i;
646
647 dout("__build_xattrs() len=%d\n",
648 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
649
650 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
651 return 0; /* already built */
652
653 __ceph_destroy_xattrs(ci);
654
655start:
656 /* updated internal xattr rb tree */
657 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
658 p = ci->i_xattrs.blob->vec.iov_base;
659 end = p + ci->i_xattrs.blob->vec.iov_len;
660 ceph_decode_32_safe(&p, end, numattr, bad);
661 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800662 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700663
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400664 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700665 GFP_NOFS);
666 err = -ENOMEM;
667 if (!xattrs)
668 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400669
Sage Weil355da1e2009-10-06 11:31:08 -0700670 for (i = 0; i < numattr; i++) {
671 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
672 GFP_NOFS);
673 if (!xattrs[i])
674 goto bad_lock;
675 }
676
Sage Weilbe655592011-11-30 09:47:09 -0800677 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700678 if (ci->i_xattrs.version != xattr_version) {
679 /* lost a race, retry */
680 for (i = 0; i < numattr; i++)
681 kfree(xattrs[i]);
682 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500683 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700684 goto start;
685 }
686 err = -EIO;
687 while (numattr--) {
688 ceph_decode_32_safe(&p, end, len, bad);
689 namelen = len;
690 name = p;
691 p += len;
692 ceph_decode_32_safe(&p, end, len, bad);
693 val = p;
694 p += len;
695
696 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800697 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700698
699 if (err < 0)
700 goto bad;
701 }
702 kfree(xattrs);
703 }
704 ci->i_xattrs.index_version = ci->i_xattrs.version;
705 ci->i_xattrs.dirty = false;
706
707 return err;
708bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800709 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700710bad:
711 if (xattrs) {
712 for (i = 0; i < numattr; i++)
713 kfree(xattrs[i]);
714 kfree(xattrs);
715 }
716 ci->i_xattrs.names_size = 0;
717 return err;
718}
719
720static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
721 int val_size)
722{
723 /*
724 * 4 bytes for the length, and additional 4 bytes per each xattr name,
725 * 4 bytes per each value
726 */
727 int size = 4 + ci->i_xattrs.count*(4 + 4) +
728 ci->i_xattrs.names_size +
729 ci->i_xattrs.vals_size;
730 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
731 ci->i_xattrs.count, ci->i_xattrs.names_size,
732 ci->i_xattrs.vals_size);
733
734 if (name_size)
735 size += 4 + 4 + name_size + val_size;
736
737 return size;
738}
739
740/*
741 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
742 * and swap into place.
743 */
744void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
745{
746 struct rb_node *p;
747 struct ceph_inode_xattr *xattr = NULL;
748 void *dest;
749
750 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
751 if (ci->i_xattrs.dirty) {
752 int need = __get_required_blob_size(ci, 0, 0);
753
754 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
755
756 p = rb_first(&ci->i_xattrs.index);
757 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
758
759 ceph_encode_32(&dest, ci->i_xattrs.count);
760 while (p) {
761 xattr = rb_entry(p, struct ceph_inode_xattr, node);
762
763 ceph_encode_32(&dest, xattr->name_len);
764 memcpy(dest, xattr->name, xattr->name_len);
765 dest += xattr->name_len;
766 ceph_encode_32(&dest, xattr->val_len);
767 memcpy(dest, xattr->val, xattr->val_len);
768 dest += xattr->val_len;
769
770 p = rb_next(p);
771 }
772
773 /* adjust buffer len; it may be larger than we need */
774 ci->i_xattrs.prealloc_blob->vec.iov_len =
775 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
776
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800777 if (ci->i_xattrs.blob)
778 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700779 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
780 ci->i_xattrs.prealloc_blob = NULL;
781 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700782 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700783 }
784}
785
Yan, Zheng315f2402016-03-07 10:34:50 +0800786static inline int __get_request_mask(struct inode *in) {
787 struct ceph_mds_request *req = current->journal_info;
788 int mask = 0;
789 if (req && req->r_target_inode == in) {
790 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
791 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
792 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
793 req->r_op == CEPH_MDS_OP_GETATTR) {
794 mask = le32_to_cpu(req->r_args.getattr.mask);
795 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
796 req->r_op == CEPH_MDS_OP_CREATE) {
797 mask = le32_to_cpu(req->r_args.open.mask);
798 }
799 }
800 return mask;
801}
802
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800803ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700804 size_t size)
805{
Sage Weil355da1e2009-10-06 11:31:08 -0700806 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700807 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600808 struct ceph_vxattr *vxattr = NULL;
Yan, Zheng315f2402016-03-07 10:34:50 +0800809 int req_mask;
810 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700811
Sage Weil0bee82f2013-01-20 22:00:58 -0800812 /* let's see if a virtual xattr was requested */
813 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800814 if (vxattr) {
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800815 int mask = 0;
816 if (vxattr->flags & VXATTR_FLAG_RSTAT)
817 mask |= CEPH_STAT_RSTAT;
818 err = ceph_do_getattr(inode, mask, true);
Yan, Zheng1684dd02017-06-14 15:54:56 +0800819 if (err)
820 return err;
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800821 err = -ENODATA;
822 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
823 err = vxattr->getxattr_cb(ci, value, size);
majianpenga1dc1932013-06-19 14:58:10 +0800824 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800825 }
826
Yan, Zheng315f2402016-03-07 10:34:50 +0800827 req_mask = __get_request_mask(inode);
828
majianpenga1dc1932013-06-19 14:58:10 +0800829 spin_lock(&ci->i_ceph_lock);
830 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
831 ci->i_xattrs.version, ci->i_xattrs.index_version);
832
Yan, Zheng508b32d2014-09-16 21:46:17 +0800833 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800834 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
835 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800836 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800837
838 /* security module gets xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200839 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +0800840 pr_warn_ratelimited("sync getxattr %p "
841 "during filling trace\n", inode);
842 return -EBUSY;
843 }
844
Sage Weil355da1e2009-10-06 11:31:08 -0700845 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800846 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700847 if (err)
848 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800849 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700850 }
851
Sage Weil355da1e2009-10-06 11:31:08 -0700852 err = __build_xattrs(inode);
853 if (err < 0)
854 goto out;
855
Sage Weil355da1e2009-10-06 11:31:08 -0700856 err = -ENODATA; /* == ENOATTR */
857 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800858 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700859 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700860
861 err = -ERANGE;
862 if (size && size < xattr->val_len)
863 goto out;
864
865 err = xattr->val_len;
866 if (size == 0)
867 goto out;
868
869 memcpy(value, xattr->val, xattr->val_len);
870
Markus Elfringd37b1d92017-08-20 20:22:02 +0200871 if (current->journal_info &&
Yan, Zheng315f2402016-03-07 10:34:50 +0800872 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
873 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -0700874out:
Sage Weilbe655592011-11-30 09:47:09 -0800875 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700876 return err;
877}
878
879ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
880{
David Howells2b0143b2015-03-17 22:25:59 +0000881 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700882 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600883 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
David Disseldorp2b2abca2019-04-18 14:15:48 +0200884 bool len_only = (size == 0);
Sage Weil355da1e2009-10-06 11:31:08 -0700885 u32 namelen;
886 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700887 int i;
888
Sage Weilbe655592011-11-30 09:47:09 -0800889 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700890 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
891 ci->i_xattrs.version, ci->i_xattrs.index_version);
892
Yan, Zheng508b32d2014-09-16 21:46:17 +0800893 if (ci->i_xattrs.version == 0 ||
894 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800895 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800896 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700897 if (err)
898 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800899 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700900 }
901
Sage Weil355da1e2009-10-06 11:31:08 -0700902 err = __build_xattrs(inode);
903 if (err < 0)
904 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600905
David Disseldorp2b2abca2019-04-18 14:15:48 +0200906 /* add 1 byte for each xattr due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -0800907 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
David Disseldorp2b2abca2019-04-18 14:15:48 +0200908 if (!len_only) {
909 if (namelen > size) {
910 err = -ERANGE;
911 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700912 }
David Disseldorp2b2abca2019-04-18 14:15:48 +0200913 names = __copy_xattr_names(ci, names);
914 size -= namelen;
Sage Weilb65917d2013-01-20 22:02:39 -0800915 }
Sage Weil355da1e2009-10-06 11:31:08 -0700916
David Disseldorp2b2abca2019-04-18 14:15:48 +0200917
918 /* virtual xattr names, too */
919 if (vxattrs) {
920 for (i = 0; vxattrs[i].name; i++) {
921 size_t this_len;
922
923 if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN)
924 continue;
925 if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci))
926 continue;
927
928 this_len = strlen(vxattrs[i].name) + 1;
929 namelen += this_len;
930 if (len_only)
931 continue;
932
933 if (this_len > size) {
934 err = -ERANGE;
935 goto out;
936 }
937
938 memcpy(names, vxattrs[i].name, this_len);
939 names += this_len;
940 size -= this_len;
941 }
942 }
943 err = namelen;
Sage Weil355da1e2009-10-06 11:31:08 -0700944out:
Sage Weilbe655592011-11-30 09:47:09 -0800945 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700946 return err;
947}
948
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200949static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -0700950 const char *value, size_t size, int flags)
951{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200952 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700953 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700954 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700955 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800956 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +0800957 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -0700958 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700959
Yan, Zheng0aeff372014-12-17 21:26:47 +0800960 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800961 /* copy value into pagelist */
Ilya Dryomov33165d42018-09-28 15:38:34 +0200962 pagelist = ceph_pagelist_alloc(GFP_NOFS);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800963 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -0700964 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800965
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800966 err = ceph_pagelist_append(pagelist, value, size);
967 if (err)
968 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +0800969 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +0800970 if (flags & CEPH_XATTR_REPLACE)
971 op = CEPH_MDS_OP_RMXATTR;
972 else
973 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -0700974 }
975
976 dout("setxattr value=%.*s\n", (int)size, value);
977
978 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +0800979 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100980 if (IS_ERR(req)) {
981 err = PTR_ERR(req);
982 goto out;
983 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400984
Sage Weil355da1e2009-10-06 11:31:08 -0700985 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400986 if (!req->r_path2) {
987 ceph_mdsc_put_request(req);
988 err = -ENOMEM;
989 goto out;
990 }
Sage Weil355da1e2009-10-06 11:31:08 -0700991
Yan, Zheng04303d82016-04-21 12:11:54 +0800992 if (op == CEPH_MDS_OP_SETXATTR) {
993 req->r_args.setxattr.flags = cpu_to_le32(flags);
994 req->r_pagelist = pagelist;
995 pagelist = NULL;
996 }
Sage Weil355da1e2009-10-06 11:31:08 -0700997
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400998 req->r_inode = inode;
999 ihold(inode);
1000 req->r_num_caps = 1;
1001 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1002
Sage Weil355da1e2009-10-06 11:31:08 -07001003 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -08001004 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -07001005 ceph_mdsc_put_request(req);
1006 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1007
1008out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001009 if (pagelist)
1010 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -07001011 return err;
1012}
1013
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001014int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001015 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -07001016{
Alex Elder881a5fa2012-01-23 15:49:28 -06001017 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -07001018 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001019 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001020 struct ceph_cap_flush *prealloc_cf = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -06001021 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -07001022 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001023 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001024 int name_len = strlen(name);
1025 int val_len = size;
1026 char *newname = NULL;
1027 char *newval = NULL;
1028 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001029 int required_blob_size;
Yan, Zhengf1919822018-04-08 09:54:39 +08001030 bool check_realm = false;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001031 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -07001032
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001033 if (ceph_snap(inode) != CEPH_NOSNAP)
1034 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -07001035
Alex Elder06476a62012-01-23 15:49:27 -06001036 vxattr = ceph_match_vxattr(inode, name);
Yan, Zhengf1919822018-04-08 09:54:39 +08001037 if (vxattr) {
Yan, Zheng4e9906e2018-04-25 17:14:05 +08001038 if (vxattr->flags & VXATTR_FLAG_READONLY)
Yan, Zhengf1919822018-04-08 09:54:39 +08001039 return -EOPNOTSUPP;
1040 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1041 check_realm = true;
1042 }
Sage Weil355da1e2009-10-06 11:31:08 -07001043
Sage Weil3adf6542013-01-31 11:53:41 -08001044 /* pass any unhandled ceph.* xattrs through to the MDS */
1045 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1046 goto do_sync_unlocked;
1047
Sage Weil355da1e2009-10-06 11:31:08 -07001048 /* preallocate memory for xattr name, value, index node */
1049 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +02001050 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001051 if (!newname)
1052 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001053
1054 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -06001055 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001056 if (!newval)
1057 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001058 }
1059
1060 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1061 if (!xattr)
1062 goto out;
1063
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001064 prealloc_cf = ceph_alloc_cap_flush();
1065 if (!prealloc_cf)
1066 goto out;
1067
Sage Weilbe655592011-11-30 09:47:09 -08001068 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001069retry:
1070 issued = __ceph_caps_issued(ci, NULL);
Yan, Zheng508b32d2014-09-16 21:46:17 +08001071 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -07001072 goto do_sync;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001073
1074 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1075 lock_snap_rwsem = true;
1076 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1077 spin_unlock(&ci->i_ceph_lock);
1078 down_read(&mdsc->snap_rwsem);
1079 spin_lock(&ci->i_ceph_lock);
1080 goto retry;
1081 }
1082 }
1083
1084 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -07001085 __build_xattrs(inode);
1086
1087 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1088
1089 if (!ci->i_xattrs.prealloc_blob ||
1090 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -06001091 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001092
Sage Weilbe655592011-11-30 09:47:09 -08001093 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001094 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001095 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001096 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +08001097 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -08001098 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001099 if (ci->i_xattrs.prealloc_blob)
1100 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -07001101 ci->i_xattrs.prealloc_blob = blob;
1102 goto retry;
1103 }
1104
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001105 err = __set_xattr(ci, newname, name_len, newval, val_len,
1106 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001107
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001108 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001109 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1110 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001111 ci->i_xattrs.dirty = true;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001112 inode->i_ctime = current_time(inode);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001113 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001114
Sage Weilbe655592011-11-30 09:47:09 -08001115 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001116 if (lock_snap_rwsem)
1117 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001118 if (dirty)
1119 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001120 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001121 return err;
1122
1123do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001124 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001125do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001126 if (lock_snap_rwsem)
1127 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001128
1129 /* security module set xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001130 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +08001131 pr_warn_ratelimited("sync setxattr %p "
1132 "during filling trace\n", inode);
1133 err = -EBUSY;
1134 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001135 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zhengf1919822018-04-08 09:54:39 +08001136 if (err >= 0 && check_realm) {
1137 /* check if snaprealm was created for quota inode */
1138 spin_lock(&ci->i_ceph_lock);
1139 if ((ci->i_max_files || ci->i_max_bytes) &&
1140 !(ci->i_snap_realm &&
1141 ci->i_snap_realm->ino == ci->i_vino.ino))
1142 err = -EOPNOTSUPP;
1143 spin_unlock(&ci->i_ceph_lock);
1144 }
Yan, Zheng315f2402016-03-07 10:34:50 +08001145 }
Sage Weil355da1e2009-10-06 11:31:08 -07001146out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001147 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001148 kfree(newname);
1149 kfree(newval);
1150 kfree(xattr);
1151 return err;
1152}
1153
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001154static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1155 struct dentry *dentry, struct inode *inode,
1156 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001157{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001158 if (!ceph_is_valid_xattr(name))
1159 return -EOPNOTSUPP;
1160 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001161}
Yan, Zheng315f2402016-03-07 10:34:50 +08001162
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001163static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04001164 struct dentry *unused, struct inode *inode,
1165 const char *name, const void *value,
1166 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001167{
1168 if (!ceph_is_valid_xattr(name))
1169 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001170 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001171}
1172
Wei Yongjun5130cce2016-10-17 15:11:29 +00001173static const struct xattr_handler ceph_other_xattr_handler = {
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001174 .prefix = "", /* match any name => handlers called with full name */
1175 .get = ceph_get_xattr_handler,
1176 .set = ceph_set_xattr_handler,
1177};
1178
Yan, Zheng315f2402016-03-07 10:34:50 +08001179#ifdef CONFIG_SECURITY
1180bool ceph_security_xattr_wanted(struct inode *in)
1181{
1182 return in->i_security != NULL;
1183}
1184
1185bool ceph_security_xattr_deadlock(struct inode *in)
1186{
1187 struct ceph_inode_info *ci;
1188 bool ret;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001189 if (!in->i_security)
Yan, Zheng315f2402016-03-07 10:34:50 +08001190 return false;
1191 ci = ceph_inode(in);
1192 spin_lock(&ci->i_ceph_lock);
1193 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1194 !(ci->i_xattrs.version > 0 &&
1195 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1196 spin_unlock(&ci->i_ceph_lock);
1197 return ret;
1198}
1199#endif