xref: /openbmc/linux/fs/ceph/xattr.c (revision 3b421018f48c482bdc9650f894aa1747cf90e51d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
325e6bae3SYan, Zheng #include <linux/ceph/pagelist.h>
43d14c5d2SYehuda Sadeh 
5355da1ebSSage Weil #include "super.h"
63d14c5d2SYehuda Sadeh #include "mds_client.h"
73d14c5d2SYehuda Sadeh 
83d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
9355da1ebSSage Weil 
10355da1ebSSage Weil #include <linux/xattr.h>
11ac6713ccSYan, Zheng #include <linux/security.h>
124db658eaSLinus Torvalds #include <linux/posix_acl_xattr.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14355da1ebSSage Weil 
1522891907SAlex Elder #define XATTR_CEPH_PREFIX "ceph."
1622891907SAlex Elder #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
1722891907SAlex Elder 
18bcdfeb2eSYan, Zheng static int __remove_xattr(struct ceph_inode_info *ci,
19bcdfeb2eSYan, Zheng 			  struct ceph_inode_xattr *xattr);
20bcdfeb2eSYan, Zheng 
21355da1ebSSage Weil static bool ceph_is_valid_xattr(const char *name)
22355da1ebSSage Weil {
2322891907SAlex Elder 	return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
24355da1ebSSage Weil 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
25355da1ebSSage Weil 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
26355da1ebSSage Weil }
27355da1ebSSage Weil 
28355da1ebSSage Weil /*
29355da1ebSSage Weil  * These define virtual xattrs exposing the recursive directory
30355da1ebSSage Weil  * statistics and layout metadata.
31355da1ebSSage Weil  */
32881a5fa2SAlex Elder struct ceph_vxattr {
33355da1ebSSage Weil 	char *name;
343ce6cd12SAlex Elder 	size_t name_size;	/* strlen(name) + 1 (for '\0') */
35f1d1b51dSJeff Layton 	ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
36355da1ebSSage Weil 			       size_t size);
37f36e4472SSage Weil 	bool (*exists_cb)(struct ceph_inode_info *ci);
384e9906e7SYan, Zheng 	unsigned int flags;
39355da1ebSSage Weil };
40355da1ebSSage Weil 
414e9906e7SYan, Zheng #define VXATTR_FLAG_READONLY		(1<<0)
424e9906e7SYan, Zheng #define VXATTR_FLAG_HIDDEN		(1<<1)
4349a9f4f6SYan, Zheng #define VXATTR_FLAG_RSTAT		(1<<2)
444e9906e7SYan, Zheng 
4532ab0bd7SSage Weil /* layouts */
4632ab0bd7SSage Weil 
4732ab0bd7SSage Weil static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
4832ab0bd7SSage Weil {
49779fe0fbSYan, Zheng 	struct ceph_file_layout *fl = &ci->i_layout;
50779fe0fbSYan, Zheng 	return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
51779fe0fbSYan, Zheng 		fl->object_size > 0 || fl->pool_id >= 0 ||
52779fe0fbSYan, Zheng 		rcu_dereference_raw(fl->pool_ns) != NULL);
5332ab0bd7SSage Weil }
5432ab0bd7SSage Weil 
55f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
5632ab0bd7SSage Weil 				    size_t size)
5732ab0bd7SSage Weil {
5832ab0bd7SSage Weil 	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
5932ab0bd7SSage Weil 	struct ceph_osd_client *osdc = &fsc->client->osdc;
60779fe0fbSYan, Zheng 	struct ceph_string *pool_ns;
617627151eSYan, Zheng 	s64 pool = ci->i_layout.pool_id;
6232ab0bd7SSage Weil 	const char *pool_name;
63779fe0fbSYan, Zheng 	const char *ns_field = " pool_namespace=";
641e5c6649SYan, Zheng 	char buf[128];
65779fe0fbSYan, Zheng 	size_t len, total_len = 0;
66*3b421018SJeff Layton 	ssize_t ret;
67779fe0fbSYan, Zheng 
68779fe0fbSYan, Zheng 	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
6932ab0bd7SSage Weil 
7032ab0bd7SSage Weil 	dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
715aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
7232ab0bd7SSage Weil 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
731e5c6649SYan, Zheng 	if (pool_name) {
74779fe0fbSYan, Zheng 		len = snprintf(buf, sizeof(buf),
757627151eSYan, Zheng 		"stripe_unit=%u stripe_count=%u object_size=%u pool=",
767627151eSYan, Zheng 		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
777627151eSYan, Zheng 	        ci->i_layout.object_size);
78779fe0fbSYan, Zheng 		total_len = len + strlen(pool_name);
791e5c6649SYan, Zheng 	} else {
80779fe0fbSYan, Zheng 		len = snprintf(buf, sizeof(buf),
817627151eSYan, Zheng 		"stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
827627151eSYan, Zheng 		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
83f1d1b51dSJeff Layton 		ci->i_layout.object_size, pool);
84779fe0fbSYan, Zheng 		total_len = len;
85779fe0fbSYan, Zheng 	}
86779fe0fbSYan, Zheng 
87779fe0fbSYan, Zheng 	if (pool_ns)
88779fe0fbSYan, Zheng 		total_len += strlen(ns_field) + pool_ns->len;
89779fe0fbSYan, Zheng 
90779fe0fbSYan, Zheng 	ret = total_len;
91*3b421018SJeff Layton 	if (size >= total_len) {
92779fe0fbSYan, Zheng 		memcpy(val, buf, len);
93779fe0fbSYan, Zheng 		ret = len;
94779fe0fbSYan, Zheng 		if (pool_name) {
95779fe0fbSYan, Zheng 			len = strlen(pool_name);
96779fe0fbSYan, Zheng 			memcpy(val + ret, pool_name, len);
97779fe0fbSYan, Zheng 			ret += len;
98779fe0fbSYan, Zheng 		}
99779fe0fbSYan, Zheng 		if (pool_ns) {
100779fe0fbSYan, Zheng 			len = strlen(ns_field);
101779fe0fbSYan, Zheng 			memcpy(val + ret, ns_field, len);
102779fe0fbSYan, Zheng 			ret += len;
103779fe0fbSYan, Zheng 			memcpy(val + ret, pool_ns->str, pool_ns->len);
104779fe0fbSYan, Zheng 			ret += pool_ns->len;
1051e5c6649SYan, Zheng 		}
1061e5c6649SYan, Zheng 	}
1075aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
108779fe0fbSYan, Zheng 	ceph_put_string(pool_ns);
10932ab0bd7SSage Weil 	return ret;
11032ab0bd7SSage Weil }
11132ab0bd7SSage Weil 
112f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
113695b7119SSage Weil 						char *val, size_t size)
114695b7119SSage Weil {
1157627151eSYan, Zheng 	return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
116695b7119SSage Weil }
117695b7119SSage Weil 
118f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
119695b7119SSage Weil 						 char *val, size_t size)
120695b7119SSage Weil {
1217627151eSYan, Zheng 	return snprintf(val, size, "%u", ci->i_layout.stripe_count);
122695b7119SSage Weil }
123695b7119SSage Weil 
124f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
125695b7119SSage Weil 						char *val, size_t size)
126695b7119SSage Weil {
1277627151eSYan, Zheng 	return snprintf(val, size, "%u", ci->i_layout.object_size);
128695b7119SSage Weil }
129695b7119SSage Weil 
130f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
131695b7119SSage Weil 					 char *val, size_t size)
132695b7119SSage Weil {
133f1d1b51dSJeff Layton 	ssize_t ret;
134695b7119SSage Weil 	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
135695b7119SSage Weil 	struct ceph_osd_client *osdc = &fsc->client->osdc;
1367627151eSYan, Zheng 	s64 pool = ci->i_layout.pool_id;
137695b7119SSage Weil 	const char *pool_name;
138695b7119SSage Weil 
1395aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
140695b7119SSage Weil 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
141695b7119SSage Weil 	if (pool_name)
142695b7119SSage Weil 		ret = snprintf(val, size, "%s", pool_name);
143695b7119SSage Weil 	else
144f1d1b51dSJeff Layton 		ret = snprintf(val, size, "%lld", pool);
1455aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
146695b7119SSage Weil 	return ret;
147695b7119SSage Weil }
148695b7119SSage Weil 
149f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
150779fe0fbSYan, Zheng 						   char *val, size_t size)
151779fe0fbSYan, Zheng {
152779fe0fbSYan, Zheng 	int ret = 0;
153779fe0fbSYan, Zheng 	struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
154779fe0fbSYan, Zheng 	if (ns) {
155f1d1b51dSJeff Layton 		ret = snprintf(val, size, "%.*s", ns->len, ns->str);
156779fe0fbSYan, Zheng 		ceph_put_string(ns);
157779fe0fbSYan, Zheng 	}
158779fe0fbSYan, Zheng 	return ret;
159779fe0fbSYan, Zheng }
160779fe0fbSYan, Zheng 
161355da1ebSSage Weil /* directories */
162355da1ebSSage Weil 
163f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
164355da1ebSSage Weil 					 size_t size)
165355da1ebSSage Weil {
166355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
167355da1ebSSage Weil }
168355da1ebSSage Weil 
169f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
170355da1ebSSage Weil 				       size_t size)
171355da1ebSSage Weil {
172355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_files);
173355da1ebSSage Weil }
174355da1ebSSage Weil 
175f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
176355da1ebSSage Weil 					 size_t size)
177355da1ebSSage Weil {
178355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_subdirs);
179355da1ebSSage Weil }
180355da1ebSSage Weil 
181f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
182355da1ebSSage Weil 					  size_t size)
183355da1ebSSage Weil {
184355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
185355da1ebSSage Weil }
186355da1ebSSage Weil 
187f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
188355da1ebSSage Weil 					size_t size)
189355da1ebSSage Weil {
190355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rfiles);
191355da1ebSSage Weil }
192355da1ebSSage Weil 
193f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
194355da1ebSSage Weil 					  size_t size)
195355da1ebSSage Weil {
196355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rsubdirs);
197355da1ebSSage Weil }
198355da1ebSSage Weil 
199f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
200355da1ebSSage Weil 					size_t size)
201355da1ebSSage Weil {
202355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rbytes);
203355da1ebSSage Weil }
204355da1ebSSage Weil 
205f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
206355da1ebSSage Weil 					size_t size)
207355da1ebSSage Weil {
20871880728SDavid Disseldorp 	return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
2099bbeab41SArnd Bergmann 			ci->i_rctime.tv_nsec);
210355da1ebSSage Weil }
211355da1ebSSage Weil 
21208796873SYan, Zheng /* dir pin */
21308796873SYan, Zheng static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
21408796873SYan, Zheng {
21508796873SYan, Zheng 	return ci->i_dir_pin != -ENODATA;
21608796873SYan, Zheng }
217fb18a575SLuis Henriques 
218f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
21908796873SYan, Zheng 				     size_t size)
22008796873SYan, Zheng {
22108796873SYan, Zheng 	return snprintf(val, size, "%d", (int)ci->i_dir_pin);
22208796873SYan, Zheng }
22308796873SYan, Zheng 
22408796873SYan, Zheng /* quotas */
225fb18a575SLuis Henriques static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
226fb18a575SLuis Henriques {
227f1919826SYan, Zheng 	bool ret = false;
228f1919826SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
229f1919826SYan, Zheng 	if ((ci->i_max_files || ci->i_max_bytes) &&
230f1919826SYan, Zheng 	    ci->i_vino.snap == CEPH_NOSNAP &&
231f1919826SYan, Zheng 	    ci->i_snap_realm &&
232f1919826SYan, Zheng 	    ci->i_snap_realm->ino == ci->i_vino.ino)
233f1919826SYan, Zheng 		ret = true;
234f1919826SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
235f1919826SYan, Zheng 	return ret;
236fb18a575SLuis Henriques }
237fb18a575SLuis Henriques 
238f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
239fb18a575SLuis Henriques 				   size_t size)
240fb18a575SLuis Henriques {
241fb18a575SLuis Henriques 	return snprintf(val, size, "max_bytes=%llu max_files=%llu",
242fb18a575SLuis Henriques 			ci->i_max_bytes, ci->i_max_files);
243fb18a575SLuis Henriques }
244fb18a575SLuis Henriques 
245f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
246fb18a575SLuis Henriques 					     char *val, size_t size)
247fb18a575SLuis Henriques {
248fb18a575SLuis Henriques 	return snprintf(val, size, "%llu", ci->i_max_bytes);
249fb18a575SLuis Henriques }
250fb18a575SLuis Henriques 
251f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
252fb18a575SLuis Henriques 					     char *val, size_t size)
253fb18a575SLuis Henriques {
254fb18a575SLuis Henriques 	return snprintf(val, size, "%llu", ci->i_max_files);
255fb18a575SLuis Henriques }
25632ab0bd7SSage Weil 
257100cc610SDavid Disseldorp /* snapshots */
258100cc610SDavid Disseldorp static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
259100cc610SDavid Disseldorp {
260100cc610SDavid Disseldorp 	return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
261100cc610SDavid Disseldorp }
262100cc610SDavid Disseldorp 
263f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
264100cc610SDavid Disseldorp 					size_t size)
265100cc610SDavid Disseldorp {
266100cc610SDavid Disseldorp 	return snprintf(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
267100cc610SDavid Disseldorp 			ci->i_snap_btime.tv_nsec);
268100cc610SDavid Disseldorp }
269100cc610SDavid Disseldorp 
270eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
271695b7119SSage Weil #define CEPH_XATTR_NAME2(_type, _name, _name2)	\
272695b7119SSage Weil 	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
273eb788084SAlex Elder 
27449a9f4f6SYan, Zheng #define XATTR_NAME_CEPH(_type, _name, _flags)				\
275eb788084SAlex Elder 	{								\
276eb788084SAlex Elder 		.name = CEPH_XATTR_NAME(_type, _name),			\
2773ce6cd12SAlex Elder 		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
278aa4066edSAlex Elder 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
279f36e4472SSage Weil 		.exists_cb = NULL,					\
28049a9f4f6SYan, Zheng 		.flags = (VXATTR_FLAG_READONLY | _flags),		\
281eb788084SAlex Elder 	}
28249a9f4f6SYan, Zheng #define XATTR_RSTAT_FIELD(_type, _name)			\
28349a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
284695b7119SSage Weil #define XATTR_LAYOUT_FIELD(_type, _name, _field)			\
285695b7119SSage Weil 	{								\
286695b7119SSage Weil 		.name = CEPH_XATTR_NAME2(_type, _name, _field),	\
287695b7119SSage Weil 		.name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
288695b7119SSage Weil 		.getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
289695b7119SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,	\
2904e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,			\
291695b7119SSage Weil 	}
292fb18a575SLuis Henriques #define XATTR_QUOTA_FIELD(_type, _name)					\
293fb18a575SLuis Henriques 	{								\
294fb18a575SLuis Henriques 		.name = CEPH_XATTR_NAME(_type, _name),			\
295fb18a575SLuis Henriques 		.name_size = sizeof(CEPH_XATTR_NAME(_type, _name)),	\
296fb18a575SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name,	\
297fb18a575SLuis Henriques 		.exists_cb = ceph_vxattrcb_quota_exists,		\
2984e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,				\
299fb18a575SLuis Henriques 	}
300eb788084SAlex Elder 
301881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = {
3021f08f2b0SSage Weil 	{
3031f08f2b0SSage Weil 		.name = "ceph.dir.layout",
3041f08f2b0SSage Weil 		.name_size = sizeof("ceph.dir.layout"),
3051f08f2b0SSage Weil 		.getxattr_cb = ceph_vxattrcb_layout,
3061f08f2b0SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,
3074e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
3081f08f2b0SSage Weil 	},
309695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
310695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
311695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, object_size),
312695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, pool),
313779fe0fbSYan, Zheng 	XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
31449a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(dir, entries, 0),
31549a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(dir, files, 0),
31649a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(dir, subdirs, 0),
31749a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rentries),
31849a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rfiles),
31949a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rsubdirs),
32049a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rbytes),
32149a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rctime),
322fb18a575SLuis Henriques 	{
32308796873SYan, Zheng 		.name = "ceph.dir.pin",
324e1b81439SDavid Disseldorp 		.name_size = sizeof("ceph.dir.pin"),
32508796873SYan, Zheng 		.getxattr_cb = ceph_vxattrcb_dir_pin,
32608796873SYan, Zheng 		.exists_cb = ceph_vxattrcb_dir_pin_exists,
32708796873SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
32808796873SYan, Zheng 	},
32908796873SYan, Zheng 	{
330fb18a575SLuis Henriques 		.name = "ceph.quota",
331fb18a575SLuis Henriques 		.name_size = sizeof("ceph.quota"),
332fb18a575SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_quota,
333fb18a575SLuis Henriques 		.exists_cb = ceph_vxattrcb_quota_exists,
3344e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
335fb18a575SLuis Henriques 	},
336fb18a575SLuis Henriques 	XATTR_QUOTA_FIELD(quota, max_bytes),
337fb18a575SLuis Henriques 	XATTR_QUOTA_FIELD(quota, max_files),
338100cc610SDavid Disseldorp 	{
339100cc610SDavid Disseldorp 		.name = "ceph.snap.btime",
340100cc610SDavid Disseldorp 		.name_size = sizeof("ceph.snap.btime"),
341100cc610SDavid Disseldorp 		.getxattr_cb = ceph_vxattrcb_snap_btime,
342100cc610SDavid Disseldorp 		.exists_cb = ceph_vxattrcb_snap_btime_exists,
343100cc610SDavid Disseldorp 		.flags = VXATTR_FLAG_READONLY,
344100cc610SDavid Disseldorp 	},
3452c3dd4ffSAlex Elder 	{ .name = NULL, 0 }	/* Required table terminator */
346355da1ebSSage Weil };
347355da1ebSSage Weil 
348355da1ebSSage Weil /* files */
349355da1ebSSage Weil 
350881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = {
35132ab0bd7SSage Weil 	{
35232ab0bd7SSage Weil 		.name = "ceph.file.layout",
35332ab0bd7SSage Weil 		.name_size = sizeof("ceph.file.layout"),
35432ab0bd7SSage Weil 		.getxattr_cb = ceph_vxattrcb_layout,
35532ab0bd7SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,
3564e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
35732ab0bd7SSage Weil 	},
358695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
359695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, stripe_count),
360695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, object_size),
361695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, pool),
362779fe0fbSYan, Zheng 	XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
363100cc610SDavid Disseldorp 	{
364100cc610SDavid Disseldorp 		.name = "ceph.snap.btime",
365100cc610SDavid Disseldorp 		.name_size = sizeof("ceph.snap.btime"),
366100cc610SDavid Disseldorp 		.getxattr_cb = ceph_vxattrcb_snap_btime,
367100cc610SDavid Disseldorp 		.exists_cb = ceph_vxattrcb_snap_btime_exists,
368100cc610SDavid Disseldorp 		.flags = VXATTR_FLAG_READONLY,
369100cc610SDavid Disseldorp 	},
3702c3dd4ffSAlex Elder 	{ .name = NULL, 0 }	/* Required table terminator */
371355da1ebSSage Weil };
372355da1ebSSage Weil 
373881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
374355da1ebSSage Weil {
375355da1ebSSage Weil 	if (S_ISDIR(inode->i_mode))
376355da1ebSSage Weil 		return ceph_dir_vxattrs;
377355da1ebSSage Weil 	else if (S_ISREG(inode->i_mode))
378355da1ebSSage Weil 		return ceph_file_vxattrs;
379355da1ebSSage Weil 	return NULL;
380355da1ebSSage Weil }
381355da1ebSSage Weil 
382881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
383355da1ebSSage Weil 						const char *name)
384355da1ebSSage Weil {
385881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
38606476a69SAlex Elder 
38706476a69SAlex Elder 	if (vxattr) {
38806476a69SAlex Elder 		while (vxattr->name) {
38906476a69SAlex Elder 			if (!strcmp(vxattr->name, name))
390355da1ebSSage Weil 				return vxattr;
391355da1ebSSage Weil 			vxattr++;
39206476a69SAlex Elder 		}
39306476a69SAlex Elder 	}
39406476a69SAlex Elder 
395355da1ebSSage Weil 	return NULL;
396355da1ebSSage Weil }
397355da1ebSSage Weil 
398355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci,
399355da1ebSSage Weil 			   const char *name, int name_len,
400355da1ebSSage Weil 			   const char *val, int val_len,
401fbc0b970SYan, Zheng 			   int flags, int update_xattr,
402355da1ebSSage Weil 			   struct ceph_inode_xattr **newxattr)
403355da1ebSSage Weil {
404355da1ebSSage Weil 	struct rb_node **p;
405355da1ebSSage Weil 	struct rb_node *parent = NULL;
406355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
407355da1ebSSage Weil 	int c;
408355da1ebSSage Weil 	int new = 0;
409355da1ebSSage Weil 
410355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
411355da1ebSSage Weil 	while (*p) {
412355da1ebSSage Weil 		parent = *p;
413355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
414355da1ebSSage Weil 		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
415355da1ebSSage Weil 		if (c < 0)
416355da1ebSSage Weil 			p = &(*p)->rb_left;
417355da1ebSSage Weil 		else if (c > 0)
418355da1ebSSage Weil 			p = &(*p)->rb_right;
419355da1ebSSage Weil 		else {
420355da1ebSSage Weil 			if (name_len == xattr->name_len)
421355da1ebSSage Weil 				break;
422355da1ebSSage Weil 			else if (name_len < xattr->name_len)
423355da1ebSSage Weil 				p = &(*p)->rb_left;
424355da1ebSSage Weil 			else
425355da1ebSSage Weil 				p = &(*p)->rb_right;
426355da1ebSSage Weil 		}
427355da1ebSSage Weil 		xattr = NULL;
428355da1ebSSage Weil 	}
429355da1ebSSage Weil 
430fbc0b970SYan, Zheng 	if (update_xattr) {
431fbc0b970SYan, Zheng 		int err = 0;
432eeca958dSLuis Henriques 
433fbc0b970SYan, Zheng 		if (xattr && (flags & XATTR_CREATE))
434fbc0b970SYan, Zheng 			err = -EEXIST;
435fbc0b970SYan, Zheng 		else if (!xattr && (flags & XATTR_REPLACE))
436fbc0b970SYan, Zheng 			err = -ENODATA;
437fbc0b970SYan, Zheng 		if (err) {
438fbc0b970SYan, Zheng 			kfree(name);
439fbc0b970SYan, Zheng 			kfree(val);
440eeca958dSLuis Henriques 			kfree(*newxattr);
441fbc0b970SYan, Zheng 			return err;
442fbc0b970SYan, Zheng 		}
443bcdfeb2eSYan, Zheng 		if (update_xattr < 0) {
444bcdfeb2eSYan, Zheng 			if (xattr)
445bcdfeb2eSYan, Zheng 				__remove_xattr(ci, xattr);
446bcdfeb2eSYan, Zheng 			kfree(name);
447eeca958dSLuis Henriques 			kfree(*newxattr);
448bcdfeb2eSYan, Zheng 			return 0;
449bcdfeb2eSYan, Zheng 		}
450fbc0b970SYan, Zheng 	}
451fbc0b970SYan, Zheng 
452355da1ebSSage Weil 	if (!xattr) {
453355da1ebSSage Weil 		new = 1;
454355da1ebSSage Weil 		xattr = *newxattr;
455355da1ebSSage Weil 		xattr->name = name;
456355da1ebSSage Weil 		xattr->name_len = name_len;
457fbc0b970SYan, Zheng 		xattr->should_free_name = update_xattr;
458355da1ebSSage Weil 
459355da1ebSSage Weil 		ci->i_xattrs.count++;
460355da1ebSSage Weil 		dout("__set_xattr count=%d\n", ci->i_xattrs.count);
461355da1ebSSage Weil 	} else {
462355da1ebSSage Weil 		kfree(*newxattr);
463355da1ebSSage Weil 		*newxattr = NULL;
464355da1ebSSage Weil 		if (xattr->should_free_val)
465355da1ebSSage Weil 			kfree((void *)xattr->val);
466355da1ebSSage Weil 
467fbc0b970SYan, Zheng 		if (update_xattr) {
468355da1ebSSage Weil 			kfree((void *)name);
469355da1ebSSage Weil 			name = xattr->name;
470355da1ebSSage Weil 		}
471355da1ebSSage Weil 		ci->i_xattrs.names_size -= xattr->name_len;
472355da1ebSSage Weil 		ci->i_xattrs.vals_size -= xattr->val_len;
473355da1ebSSage Weil 	}
474355da1ebSSage Weil 	ci->i_xattrs.names_size += name_len;
475355da1ebSSage Weil 	ci->i_xattrs.vals_size += val_len;
476355da1ebSSage Weil 	if (val)
477355da1ebSSage Weil 		xattr->val = val;
478355da1ebSSage Weil 	else
479355da1ebSSage Weil 		xattr->val = "";
480355da1ebSSage Weil 
481355da1ebSSage Weil 	xattr->val_len = val_len;
482fbc0b970SYan, Zheng 	xattr->dirty = update_xattr;
483fbc0b970SYan, Zheng 	xattr->should_free_val = (val && update_xattr);
484355da1ebSSage Weil 
485355da1ebSSage Weil 	if (new) {
486355da1ebSSage Weil 		rb_link_node(&xattr->node, parent, p);
487355da1ebSSage Weil 		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
488355da1ebSSage Weil 		dout("__set_xattr_val p=%p\n", p);
489355da1ebSSage Weil 	}
490355da1ebSSage Weil 
49105729781SYan, Zheng 	dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
49205729781SYan, Zheng 	     ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
493355da1ebSSage Weil 
494355da1ebSSage Weil 	return 0;
495355da1ebSSage Weil }
496355da1ebSSage Weil 
497355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
498355da1ebSSage Weil 			   const char *name)
499355da1ebSSage Weil {
500355da1ebSSage Weil 	struct rb_node **p;
501355da1ebSSage Weil 	struct rb_node *parent = NULL;
502355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
50317db143fSSage Weil 	int name_len = strlen(name);
504355da1ebSSage Weil 	int c;
505355da1ebSSage Weil 
506355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
507355da1ebSSage Weil 	while (*p) {
508355da1ebSSage Weil 		parent = *p;
509355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
510355da1ebSSage Weil 		c = strncmp(name, xattr->name, xattr->name_len);
51117db143fSSage Weil 		if (c == 0 && name_len > xattr->name_len)
51217db143fSSage Weil 			c = 1;
513355da1ebSSage Weil 		if (c < 0)
514355da1ebSSage Weil 			p = &(*p)->rb_left;
515355da1ebSSage Weil 		else if (c > 0)
516355da1ebSSage Weil 			p = &(*p)->rb_right;
517355da1ebSSage Weil 		else {
518355da1ebSSage Weil 			dout("__get_xattr %s: found %.*s\n", name,
519355da1ebSSage Weil 			     xattr->val_len, xattr->val);
520355da1ebSSage Weil 			return xattr;
521355da1ebSSage Weil 		}
522355da1ebSSage Weil 	}
523355da1ebSSage Weil 
524355da1ebSSage Weil 	dout("__get_xattr %s: not found\n", name);
525355da1ebSSage Weil 
526355da1ebSSage Weil 	return NULL;
527355da1ebSSage Weil }
528355da1ebSSage Weil 
529355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr)
530355da1ebSSage Weil {
531355da1ebSSage Weil 	BUG_ON(!xattr);
532355da1ebSSage Weil 
533355da1ebSSage Weil 	if (xattr->should_free_name)
534355da1ebSSage Weil 		kfree((void *)xattr->name);
535355da1ebSSage Weil 	if (xattr->should_free_val)
536355da1ebSSage Weil 		kfree((void *)xattr->val);
537355da1ebSSage Weil 
538355da1ebSSage Weil 	kfree(xattr);
539355da1ebSSage Weil }
540355da1ebSSage Weil 
541355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci,
542355da1ebSSage Weil 			  struct ceph_inode_xattr *xattr)
543355da1ebSSage Weil {
544355da1ebSSage Weil 	if (!xattr)
545524186acSYan, Zheng 		return -ENODATA;
546355da1ebSSage Weil 
547355da1ebSSage Weil 	rb_erase(&xattr->node, &ci->i_xattrs.index);
548355da1ebSSage Weil 
549355da1ebSSage Weil 	if (xattr->should_free_name)
550355da1ebSSage Weil 		kfree((void *)xattr->name);
551355da1ebSSage Weil 	if (xattr->should_free_val)
552355da1ebSSage Weil 		kfree((void *)xattr->val);
553355da1ebSSage Weil 
554355da1ebSSage Weil 	ci->i_xattrs.names_size -= xattr->name_len;
555355da1ebSSage Weil 	ci->i_xattrs.vals_size -= xattr->val_len;
556355da1ebSSage Weil 	ci->i_xattrs.count--;
557355da1ebSSage Weil 	kfree(xattr);
558355da1ebSSage Weil 
559355da1ebSSage Weil 	return 0;
560355da1ebSSage Weil }
561355da1ebSSage Weil 
562355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci,
563355da1ebSSage Weil 				char *dest)
564355da1ebSSage Weil {
565355da1ebSSage Weil 	struct rb_node *p;
566355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
567355da1ebSSage Weil 
568355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
569355da1ebSSage Weil 	dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
570355da1ebSSage Weil 
571355da1ebSSage Weil 	while (p) {
572355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
573355da1ebSSage Weil 		memcpy(dest, xattr->name, xattr->name_len);
574355da1ebSSage Weil 		dest[xattr->name_len] = '\0';
575355da1ebSSage Weil 
576355da1ebSSage Weil 		dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
577355da1ebSSage Weil 		     xattr->name_len, ci->i_xattrs.names_size);
578355da1ebSSage Weil 
579355da1ebSSage Weil 		dest += xattr->name_len + 1;
580355da1ebSSage Weil 		p = rb_next(p);
581355da1ebSSage Weil 	}
582355da1ebSSage Weil 
583355da1ebSSage Weil 	return dest;
584355da1ebSSage Weil }
585355da1ebSSage Weil 
586355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
587355da1ebSSage Weil {
588355da1ebSSage Weil 	struct rb_node *p, *tmp;
589355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
590355da1ebSSage Weil 
591355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
592355da1ebSSage Weil 
593355da1ebSSage Weil 	dout("__ceph_destroy_xattrs p=%p\n", p);
594355da1ebSSage Weil 
595355da1ebSSage Weil 	while (p) {
596355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
597355da1ebSSage Weil 		tmp = p;
598355da1ebSSage Weil 		p = rb_next(tmp);
599355da1ebSSage Weil 		dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
600355da1ebSSage Weil 		     xattr->name_len, xattr->name);
601355da1ebSSage Weil 		rb_erase(tmp, &ci->i_xattrs.index);
602355da1ebSSage Weil 
603355da1ebSSage Weil 		__free_xattr(xattr);
604355da1ebSSage Weil 	}
605355da1ebSSage Weil 
606355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
607355da1ebSSage Weil 	ci->i_xattrs.vals_size = 0;
608355da1ebSSage Weil 	ci->i_xattrs.index_version = 0;
609355da1ebSSage Weil 	ci->i_xattrs.count = 0;
610355da1ebSSage Weil 	ci->i_xattrs.index = RB_ROOT;
611355da1ebSSage Weil }
612355da1ebSSage Weil 
613355da1ebSSage Weil static int __build_xattrs(struct inode *inode)
614be655596SSage Weil 	__releases(ci->i_ceph_lock)
615be655596SSage Weil 	__acquires(ci->i_ceph_lock)
616355da1ebSSage Weil {
617355da1ebSSage Weil 	u32 namelen;
618355da1ebSSage Weil 	u32 numattr = 0;
619355da1ebSSage Weil 	void *p, *end;
620355da1ebSSage Weil 	u32 len;
621355da1ebSSage Weil 	const char *name, *val;
622355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
623355da1ebSSage Weil 	int xattr_version;
624355da1ebSSage Weil 	struct ceph_inode_xattr **xattrs = NULL;
62563ff78b2SSage Weil 	int err = 0;
626355da1ebSSage Weil 	int i;
627355da1ebSSage Weil 
628355da1ebSSage Weil 	dout("__build_xattrs() len=%d\n",
629355da1ebSSage Weil 	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
630355da1ebSSage Weil 
631355da1ebSSage Weil 	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
632355da1ebSSage Weil 		return 0; /* already built */
633355da1ebSSage Weil 
634355da1ebSSage Weil 	__ceph_destroy_xattrs(ci);
635355da1ebSSage Weil 
636355da1ebSSage Weil start:
637355da1ebSSage Weil 	/* updated internal xattr rb tree */
638355da1ebSSage Weil 	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
639355da1ebSSage Weil 		p = ci->i_xattrs.blob->vec.iov_base;
640355da1ebSSage Weil 		end = p + ci->i_xattrs.blob->vec.iov_len;
641355da1ebSSage Weil 		ceph_decode_32_safe(&p, end, numattr, bad);
642355da1ebSSage Weil 		xattr_version = ci->i_xattrs.version;
643be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
644355da1ebSSage Weil 
6457e8a2952SIlya Dryomov 		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
646355da1ebSSage Weil 				 GFP_NOFS);
647355da1ebSSage Weil 		err = -ENOMEM;
648355da1ebSSage Weil 		if (!xattrs)
649355da1ebSSage Weil 			goto bad_lock;
6501a295bd8SIlya Dryomov 
651355da1ebSSage Weil 		for (i = 0; i < numattr; i++) {
652355da1ebSSage Weil 			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
653355da1ebSSage Weil 					    GFP_NOFS);
654355da1ebSSage Weil 			if (!xattrs[i])
655355da1ebSSage Weil 				goto bad_lock;
656355da1ebSSage Weil 		}
657355da1ebSSage Weil 
658be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
659355da1ebSSage Weil 		if (ci->i_xattrs.version != xattr_version) {
660355da1ebSSage Weil 			/* lost a race, retry */
661355da1ebSSage Weil 			for (i = 0; i < numattr; i++)
662355da1ebSSage Weil 				kfree(xattrs[i]);
663355da1ebSSage Weil 			kfree(xattrs);
66421ec6ffaSAlan Cox 			xattrs = NULL;
665355da1ebSSage Weil 			goto start;
666355da1ebSSage Weil 		}
667355da1ebSSage Weil 		err = -EIO;
668355da1ebSSage Weil 		while (numattr--) {
669355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
670355da1ebSSage Weil 			namelen = len;
671355da1ebSSage Weil 			name = p;
672355da1ebSSage Weil 			p += len;
673355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
674355da1ebSSage Weil 			val = p;
675355da1ebSSage Weil 			p += len;
676355da1ebSSage Weil 
677355da1ebSSage Weil 			err = __set_xattr(ci, name, namelen, val, len,
678fbc0b970SYan, Zheng 					  0, 0, &xattrs[numattr]);
679355da1ebSSage Weil 
680355da1ebSSage Weil 			if (err < 0)
681355da1ebSSage Weil 				goto bad;
682355da1ebSSage Weil 		}
683355da1ebSSage Weil 		kfree(xattrs);
684355da1ebSSage Weil 	}
685355da1ebSSage Weil 	ci->i_xattrs.index_version = ci->i_xattrs.version;
686355da1ebSSage Weil 	ci->i_xattrs.dirty = false;
687355da1ebSSage Weil 
688355da1ebSSage Weil 	return err;
689355da1ebSSage Weil bad_lock:
690be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
691355da1ebSSage Weil bad:
692355da1ebSSage Weil 	if (xattrs) {
693355da1ebSSage Weil 		for (i = 0; i < numattr; i++)
694355da1ebSSage Weil 			kfree(xattrs[i]);
695355da1ebSSage Weil 		kfree(xattrs);
696355da1ebSSage Weil 	}
697355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
698355da1ebSSage Weil 	return err;
699355da1ebSSage Weil }
700355da1ebSSage Weil 
701355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
702355da1ebSSage Weil 				    int val_size)
703355da1ebSSage Weil {
704355da1ebSSage Weil 	/*
705355da1ebSSage Weil 	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
706355da1ebSSage Weil 	 * 4 bytes per each value
707355da1ebSSage Weil 	 */
708355da1ebSSage Weil 	int size = 4 + ci->i_xattrs.count*(4 + 4) +
709355da1ebSSage Weil 			     ci->i_xattrs.names_size +
710355da1ebSSage Weil 			     ci->i_xattrs.vals_size;
711355da1ebSSage Weil 	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
712355da1ebSSage Weil 	     ci->i_xattrs.count, ci->i_xattrs.names_size,
713355da1ebSSage Weil 	     ci->i_xattrs.vals_size);
714355da1ebSSage Weil 
715355da1ebSSage Weil 	if (name_size)
716355da1ebSSage Weil 		size += 4 + 4 + name_size + val_size;
717355da1ebSSage Weil 
718355da1ebSSage Weil 	return size;
719355da1ebSSage Weil }
720355da1ebSSage Weil 
721355da1ebSSage Weil /*
722355da1ebSSage Weil  * If there are dirty xattrs, reencode xattrs into the prealloc_blob
723355da1ebSSage Weil  * and swap into place.
724355da1ebSSage Weil  */
725355da1ebSSage Weil void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
726355da1ebSSage Weil {
727355da1ebSSage Weil 	struct rb_node *p;
728355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
729355da1ebSSage Weil 	void *dest;
730355da1ebSSage Weil 
731355da1ebSSage Weil 	dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
732355da1ebSSage Weil 	if (ci->i_xattrs.dirty) {
733355da1ebSSage Weil 		int need = __get_required_blob_size(ci, 0, 0);
734355da1ebSSage Weil 
735355da1ebSSage Weil 		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
736355da1ebSSage Weil 
737355da1ebSSage Weil 		p = rb_first(&ci->i_xattrs.index);
738355da1ebSSage Weil 		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
739355da1ebSSage Weil 
740355da1ebSSage Weil 		ceph_encode_32(&dest, ci->i_xattrs.count);
741355da1ebSSage Weil 		while (p) {
742355da1ebSSage Weil 			xattr = rb_entry(p, struct ceph_inode_xattr, node);
743355da1ebSSage Weil 
744355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->name_len);
745355da1ebSSage Weil 			memcpy(dest, xattr->name, xattr->name_len);
746355da1ebSSage Weil 			dest += xattr->name_len;
747355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->val_len);
748355da1ebSSage Weil 			memcpy(dest, xattr->val, xattr->val_len);
749355da1ebSSage Weil 			dest += xattr->val_len;
750355da1ebSSage Weil 
751355da1ebSSage Weil 			p = rb_next(p);
752355da1ebSSage Weil 		}
753355da1ebSSage Weil 
754355da1ebSSage Weil 		/* adjust buffer len; it may be larger than we need */
755355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob->vec.iov_len =
756355da1ebSSage Weil 			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
757355da1ebSSage Weil 
758b6c1d5b8SSage Weil 		if (ci->i_xattrs.blob)
759355da1ebSSage Weil 			ceph_buffer_put(ci->i_xattrs.blob);
760355da1ebSSage Weil 		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
761355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = NULL;
762355da1ebSSage Weil 		ci->i_xattrs.dirty = false;
7634a625be4SSage Weil 		ci->i_xattrs.version++;
764355da1ebSSage Weil 	}
765355da1ebSSage Weil }
766355da1ebSSage Weil 
767315f2408SYan, Zheng static inline int __get_request_mask(struct inode *in) {
768315f2408SYan, Zheng 	struct ceph_mds_request *req = current->journal_info;
769315f2408SYan, Zheng 	int mask = 0;
770315f2408SYan, Zheng 	if (req && req->r_target_inode == in) {
771315f2408SYan, Zheng 		if (req->r_op == CEPH_MDS_OP_LOOKUP ||
772315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_LOOKUPINO ||
773315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
774315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_GETATTR) {
775315f2408SYan, Zheng 			mask = le32_to_cpu(req->r_args.getattr.mask);
776315f2408SYan, Zheng 		} else if (req->r_op == CEPH_MDS_OP_OPEN ||
777315f2408SYan, Zheng 			   req->r_op == CEPH_MDS_OP_CREATE) {
778315f2408SYan, Zheng 			mask = le32_to_cpu(req->r_args.open.mask);
779315f2408SYan, Zheng 		}
780315f2408SYan, Zheng 	}
781315f2408SYan, Zheng 	return mask;
782315f2408SYan, Zheng }
783315f2408SYan, Zheng 
7847221fe4cSGuangliang Zhao ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
785355da1ebSSage Weil 		      size_t size)
786355da1ebSSage Weil {
787355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
788355da1ebSSage Weil 	struct ceph_inode_xattr *xattr;
789881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = NULL;
790315f2408SYan, Zheng 	int req_mask;
791f1d1b51dSJeff Layton 	ssize_t err;
792355da1ebSSage Weil 
7930bee82fbSSage Weil 	/* let's see if a virtual xattr was requested */
7940bee82fbSSage Weil 	vxattr = ceph_match_vxattr(inode, name);
79529dccfa5SYan, Zheng 	if (vxattr) {
79649a9f4f6SYan, Zheng 		int mask = 0;
79749a9f4f6SYan, Zheng 		if (vxattr->flags & VXATTR_FLAG_RSTAT)
79849a9f4f6SYan, Zheng 			mask |= CEPH_STAT_RSTAT;
79949a9f4f6SYan, Zheng 		err = ceph_do_getattr(inode, mask, true);
8001684dd03SYan, Zheng 		if (err)
8011684dd03SYan, Zheng 			return err;
80229dccfa5SYan, Zheng 		err = -ENODATA;
803*3b421018SJeff Layton 		if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
8040bee82fbSSage Weil 			err = vxattr->getxattr_cb(ci, value, size);
805*3b421018SJeff Layton 			if (size && size < err)
806*3b421018SJeff Layton 				err = -ERANGE;
807*3b421018SJeff Layton 		}
808a1dc1937Smajianpeng 		return err;
8090bee82fbSSage Weil 	}
8100bee82fbSSage Weil 
811315f2408SYan, Zheng 	req_mask = __get_request_mask(inode);
812315f2408SYan, Zheng 
813a1dc1937Smajianpeng 	spin_lock(&ci->i_ceph_lock);
814a1dc1937Smajianpeng 	dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
815a1dc1937Smajianpeng 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
816a1dc1937Smajianpeng 
817508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 ||
818315f2408SYan, Zheng 	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
819315f2408SYan, Zheng 	      __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
820be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
821315f2408SYan, Zheng 
822315f2408SYan, Zheng 		/* security module gets xattr while filling trace */
823d37b1d99SMarkus Elfring 		if (current->journal_info) {
824315f2408SYan, Zheng 			pr_warn_ratelimited("sync getxattr %p "
825315f2408SYan, Zheng 					    "during filling trace\n", inode);
826315f2408SYan, Zheng 			return -EBUSY;
827315f2408SYan, Zheng 		}
828315f2408SYan, Zheng 
829355da1ebSSage Weil 		/* get xattrs from mds (if we don't already have them) */
830508b32d8SYan, Zheng 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
831355da1ebSSage Weil 		if (err)
832355da1ebSSage Weil 			return err;
833be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
834508b32d8SYan, Zheng 	}
835355da1ebSSage Weil 
836355da1ebSSage Weil 	err = __build_xattrs(inode);
837355da1ebSSage Weil 	if (err < 0)
838355da1ebSSage Weil 		goto out;
839355da1ebSSage Weil 
840355da1ebSSage Weil 	err = -ENODATA;  /* == ENOATTR */
841355da1ebSSage Weil 	xattr = __get_xattr(ci, name);
8420bee82fbSSage Weil 	if (!xattr)
843355da1ebSSage Weil 		goto out;
844355da1ebSSage Weil 
845355da1ebSSage Weil 	err = -ERANGE;
846355da1ebSSage Weil 	if (size && size < xattr->val_len)
847355da1ebSSage Weil 		goto out;
848355da1ebSSage Weil 
849355da1ebSSage Weil 	err = xattr->val_len;
850355da1ebSSage Weil 	if (size == 0)
851355da1ebSSage Weil 		goto out;
852355da1ebSSage Weil 
853355da1ebSSage Weil 	memcpy(value, xattr->val, xattr->val_len);
854355da1ebSSage Weil 
855d37b1d99SMarkus Elfring 	if (current->journal_info &&
856315f2408SYan, Zheng 	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
857315f2408SYan, Zheng 		ci->i_ceph_flags |= CEPH_I_SEC_INITED;
858355da1ebSSage Weil out:
859be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
860355da1ebSSage Weil 	return err;
861355da1ebSSage Weil }
862355da1ebSSage Weil 
863355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
864355da1ebSSage Weil {
8652b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
866355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
867881a5fa2SAlex Elder 	struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
8682b2abcacSDavid Disseldorp 	bool len_only = (size == 0);
869355da1ebSSage Weil 	u32 namelen;
870355da1ebSSage Weil 	int err;
871355da1ebSSage Weil 	int i;
872355da1ebSSage Weil 
873be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
874355da1ebSSage Weil 	dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
875355da1ebSSage Weil 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
876355da1ebSSage Weil 
877508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 ||
878508b32d8SYan, Zheng 	    !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
879be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
880508b32d8SYan, Zheng 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
881355da1ebSSage Weil 		if (err)
882355da1ebSSage Weil 			return err;
883be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
884508b32d8SYan, Zheng 	}
885355da1ebSSage Weil 
886355da1ebSSage Weil 	err = __build_xattrs(inode);
887355da1ebSSage Weil 	if (err < 0)
888355da1ebSSage Weil 		goto out;
8893ce6cd12SAlex Elder 
8902b2abcacSDavid Disseldorp 	/* add 1 byte for each xattr due to the null termination */
891b65917ddSSage Weil 	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
8922b2abcacSDavid Disseldorp 	if (!len_only) {
8932b2abcacSDavid Disseldorp 		if (namelen > size) {
894355da1ebSSage Weil 			err = -ERANGE;
895355da1ebSSage Weil 			goto out;
8962b2abcacSDavid Disseldorp 		}
897355da1ebSSage Weil 		names = __copy_xattr_names(ci, names);
8982b2abcacSDavid Disseldorp 		size -= namelen;
8992b2abcacSDavid Disseldorp 	}
9002b2abcacSDavid Disseldorp 
901355da1ebSSage Weil 
902355da1ebSSage Weil 	/* virtual xattr names, too */
903b65917ddSSage Weil 	if (vxattrs) {
904355da1ebSSage Weil 		for (i = 0; vxattrs[i].name; i++) {
9052b2abcacSDavid Disseldorp 			size_t this_len;
9062b2abcacSDavid Disseldorp 
9072b2abcacSDavid Disseldorp 			if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN)
9082b2abcacSDavid Disseldorp 				continue;
9092b2abcacSDavid Disseldorp 			if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci))
9102b2abcacSDavid Disseldorp 				continue;
9112b2abcacSDavid Disseldorp 
9122b2abcacSDavid Disseldorp 			this_len = strlen(vxattrs[i].name) + 1;
9132b2abcacSDavid Disseldorp 			namelen += this_len;
9142b2abcacSDavid Disseldorp 			if (len_only)
9152b2abcacSDavid Disseldorp 				continue;
9162b2abcacSDavid Disseldorp 
9172b2abcacSDavid Disseldorp 			if (this_len > size) {
9182b2abcacSDavid Disseldorp 				err = -ERANGE;
9192b2abcacSDavid Disseldorp 				goto out;
920355da1ebSSage Weil 			}
921355da1ebSSage Weil 
9222b2abcacSDavid Disseldorp 			memcpy(names, vxattrs[i].name, this_len);
9232b2abcacSDavid Disseldorp 			names += this_len;
9242b2abcacSDavid Disseldorp 			size -= this_len;
9252b2abcacSDavid Disseldorp 		}
9262b2abcacSDavid Disseldorp 	}
9272b2abcacSDavid Disseldorp 	err = namelen;
928355da1ebSSage Weil out:
929be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
930355da1ebSSage Weil 	return err;
931355da1ebSSage Weil }
932355da1ebSSage Weil 
933a26feccaSAndreas Gruenbacher static int ceph_sync_setxattr(struct inode *inode, const char *name,
934355da1ebSSage Weil 			      const char *value, size_t size, int flags)
935355da1ebSSage Weil {
936a26feccaSAndreas Gruenbacher 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
937355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
938355da1ebSSage Weil 	struct ceph_mds_request *req;
9393d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
94025e6bae3SYan, Zheng 	struct ceph_pagelist *pagelist = NULL;
94104303d8aSYan, Zheng 	int op = CEPH_MDS_OP_SETXATTR;
942355da1ebSSage Weil 	int err;
943355da1ebSSage Weil 
9440aeff37aSYan, Zheng 	if (size > 0) {
94525e6bae3SYan, Zheng 		/* copy value into pagelist */
94633165d47SIlya Dryomov 		pagelist = ceph_pagelist_alloc(GFP_NOFS);
94725e6bae3SYan, Zheng 		if (!pagelist)
948355da1ebSSage Weil 			return -ENOMEM;
94925e6bae3SYan, Zheng 
95025e6bae3SYan, Zheng 		err = ceph_pagelist_append(pagelist, value, size);
95125e6bae3SYan, Zheng 		if (err)
952355da1ebSSage Weil 			goto out;
9530aeff37aSYan, Zheng 	} else if (!value) {
95404303d8aSYan, Zheng 		if (flags & CEPH_XATTR_REPLACE)
95504303d8aSYan, Zheng 			op = CEPH_MDS_OP_RMXATTR;
95604303d8aSYan, Zheng 		else
95725e6bae3SYan, Zheng 			flags |= CEPH_XATTR_REMOVE;
958355da1ebSSage Weil 	}
959355da1ebSSage Weil 
960355da1ebSSage Weil 	dout("setxattr value=%.*s\n", (int)size, value);
961355da1ebSSage Weil 
962355da1ebSSage Weil 	/* do request */
96304303d8aSYan, Zheng 	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
96460d87733SJulia Lawall 	if (IS_ERR(req)) {
96560d87733SJulia Lawall 		err = PTR_ERR(req);
96660d87733SJulia Lawall 		goto out;
96760d87733SJulia Lawall 	}
968a149bb9aSSanidhya Kashyap 
969355da1ebSSage Weil 	req->r_path2 = kstrdup(name, GFP_NOFS);
970a149bb9aSSanidhya Kashyap 	if (!req->r_path2) {
971a149bb9aSSanidhya Kashyap 		ceph_mdsc_put_request(req);
972a149bb9aSSanidhya Kashyap 		err = -ENOMEM;
973a149bb9aSSanidhya Kashyap 		goto out;
974a149bb9aSSanidhya Kashyap 	}
975355da1ebSSage Weil 
97604303d8aSYan, Zheng 	if (op == CEPH_MDS_OP_SETXATTR) {
97704303d8aSYan, Zheng 		req->r_args.setxattr.flags = cpu_to_le32(flags);
97825e6bae3SYan, Zheng 		req->r_pagelist = pagelist;
97925e6bae3SYan, Zheng 		pagelist = NULL;
98004303d8aSYan, Zheng 	}
981355da1ebSSage Weil 
982a149bb9aSSanidhya Kashyap 	req->r_inode = inode;
983a149bb9aSSanidhya Kashyap 	ihold(inode);
984a149bb9aSSanidhya Kashyap 	req->r_num_caps = 1;
985a149bb9aSSanidhya Kashyap 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
986a149bb9aSSanidhya Kashyap 
987355da1ebSSage Weil 	dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
988752c8bdcSSage Weil 	err = ceph_mdsc_do_request(mdsc, NULL, req);
989355da1ebSSage Weil 	ceph_mdsc_put_request(req);
990355da1ebSSage Weil 	dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
991355da1ebSSage Weil 
992355da1ebSSage Weil out:
99325e6bae3SYan, Zheng 	if (pagelist)
99425e6bae3SYan, Zheng 		ceph_pagelist_release(pagelist);
995355da1ebSSage Weil 	return err;
996355da1ebSSage Weil }
997355da1ebSSage Weil 
998a26feccaSAndreas Gruenbacher int __ceph_setxattr(struct inode *inode, const char *name,
999355da1ebSSage Weil 			const void *value, size_t size, int flags)
1000355da1ebSSage Weil {
1001881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr;
1002355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1003a26feccaSAndreas Gruenbacher 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1004f66fd9f0SYan, Zheng 	struct ceph_cap_flush *prealloc_cf = NULL;
100518fa8b3fSAlex Elder 	int issued;
1006355da1ebSSage Weil 	int err;
1007fbc0b970SYan, Zheng 	int dirty = 0;
1008355da1ebSSage Weil 	int name_len = strlen(name);
1009355da1ebSSage Weil 	int val_len = size;
1010355da1ebSSage Weil 	char *newname = NULL;
1011355da1ebSSage Weil 	char *newval = NULL;
1012355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
1013355da1ebSSage Weil 	int required_blob_size;
1014f1919826SYan, Zheng 	bool check_realm = false;
1015604d1b02SYan, Zheng 	bool lock_snap_rwsem = false;
1016355da1ebSSage Weil 
10172cdeb1e4SAndreas Gruenbacher 	if (ceph_snap(inode) != CEPH_NOSNAP)
10182cdeb1e4SAndreas Gruenbacher 		return -EROFS;
1019355da1ebSSage Weil 
102006476a69SAlex Elder 	vxattr = ceph_match_vxattr(inode, name);
1021f1919826SYan, Zheng 	if (vxattr) {
10224e9906e7SYan, Zheng 		if (vxattr->flags & VXATTR_FLAG_READONLY)
1023355da1ebSSage Weil 			return -EOPNOTSUPP;
1024f1919826SYan, Zheng 		if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1025f1919826SYan, Zheng 			check_realm = true;
1026f1919826SYan, Zheng 	}
1027355da1ebSSage Weil 
10283adf654dSSage Weil 	/* pass any unhandled ceph.* xattrs through to the MDS */
10293adf654dSSage Weil 	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
10303adf654dSSage Weil 		goto do_sync_unlocked;
10313adf654dSSage Weil 
1032355da1ebSSage Weil 	/* preallocate memory for xattr name, value, index node */
1033355da1ebSSage Weil 	err = -ENOMEM;
103461413c2fSJulia Lawall 	newname = kmemdup(name, name_len + 1, GFP_NOFS);
1035355da1ebSSage Weil 	if (!newname)
1036355da1ebSSage Weil 		goto out;
1037355da1ebSSage Weil 
1038355da1ebSSage Weil 	if (val_len) {
1039b829c195SAlex Elder 		newval = kmemdup(value, val_len, GFP_NOFS);
1040355da1ebSSage Weil 		if (!newval)
1041355da1ebSSage Weil 			goto out;
1042355da1ebSSage Weil 	}
1043355da1ebSSage Weil 
1044355da1ebSSage Weil 	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1045355da1ebSSage Weil 	if (!xattr)
1046355da1ebSSage Weil 		goto out;
1047355da1ebSSage Weil 
1048f66fd9f0SYan, Zheng 	prealloc_cf = ceph_alloc_cap_flush();
1049f66fd9f0SYan, Zheng 	if (!prealloc_cf)
1050f66fd9f0SYan, Zheng 		goto out;
1051f66fd9f0SYan, Zheng 
1052be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1053355da1ebSSage Weil retry:
1054355da1ebSSage Weil 	issued = __ceph_caps_issued(ci, NULL);
1055508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
1056355da1ebSSage Weil 		goto do_sync;
1057604d1b02SYan, Zheng 
1058604d1b02SYan, Zheng 	if (!lock_snap_rwsem && !ci->i_head_snapc) {
1059604d1b02SYan, Zheng 		lock_snap_rwsem = true;
1060604d1b02SYan, Zheng 		if (!down_read_trylock(&mdsc->snap_rwsem)) {
1061604d1b02SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
1062604d1b02SYan, Zheng 			down_read(&mdsc->snap_rwsem);
1063604d1b02SYan, Zheng 			spin_lock(&ci->i_ceph_lock);
1064604d1b02SYan, Zheng 			goto retry;
1065604d1b02SYan, Zheng 		}
1066604d1b02SYan, Zheng 	}
1067604d1b02SYan, Zheng 
1068604d1b02SYan, Zheng 	dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
1069355da1ebSSage Weil 	__build_xattrs(inode);
1070355da1ebSSage Weil 
1071355da1ebSSage Weil 	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1072355da1ebSSage Weil 
1073355da1ebSSage Weil 	if (!ci->i_xattrs.prealloc_blob ||
1074355da1ebSSage Weil 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
107518fa8b3fSAlex Elder 		struct ceph_buffer *blob;
1076355da1ebSSage Weil 
1077be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1078355da1ebSSage Weil 		dout(" preaallocating new blob size=%d\n", required_blob_size);
1079b6c1d5b8SSage Weil 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1080355da1ebSSage Weil 		if (!blob)
1081604d1b02SYan, Zheng 			goto do_sync_unlocked;
1082be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1083b6c1d5b8SSage Weil 		if (ci->i_xattrs.prealloc_blob)
1084355da1ebSSage Weil 			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1085355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = blob;
1086355da1ebSSage Weil 		goto retry;
1087355da1ebSSage Weil 	}
1088355da1ebSSage Weil 
1089bcdfeb2eSYan, Zheng 	err = __set_xattr(ci, newname, name_len, newval, val_len,
1090bcdfeb2eSYan, Zheng 			  flags, value ? 1 : -1, &xattr);
109118fa8b3fSAlex Elder 
1092fbc0b970SYan, Zheng 	if (!err) {
1093f66fd9f0SYan, Zheng 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1094f66fd9f0SYan, Zheng 					       &prealloc_cf);
1095355da1ebSSage Weil 		ci->i_xattrs.dirty = true;
1096c2050a45SDeepa Dinamani 		inode->i_ctime = current_time(inode);
1097fbc0b970SYan, Zheng 	}
109818fa8b3fSAlex Elder 
1099be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1100604d1b02SYan, Zheng 	if (lock_snap_rwsem)
1101604d1b02SYan, Zheng 		up_read(&mdsc->snap_rwsem);
1102fca65b4aSSage Weil 	if (dirty)
1103fca65b4aSSage Weil 		__mark_inode_dirty(inode, dirty);
1104f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
1105355da1ebSSage Weil 	return err;
1106355da1ebSSage Weil 
1107355da1ebSSage Weil do_sync:
1108be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
11093adf654dSSage Weil do_sync_unlocked:
1110604d1b02SYan, Zheng 	if (lock_snap_rwsem)
1111604d1b02SYan, Zheng 		up_read(&mdsc->snap_rwsem);
1112315f2408SYan, Zheng 
1113315f2408SYan, Zheng 	/* security module set xattr while filling trace */
1114d37b1d99SMarkus Elfring 	if (current->journal_info) {
1115315f2408SYan, Zheng 		pr_warn_ratelimited("sync setxattr %p "
1116315f2408SYan, Zheng 				    "during filling trace\n", inode);
1117315f2408SYan, Zheng 		err = -EBUSY;
1118315f2408SYan, Zheng 	} else {
1119a26feccaSAndreas Gruenbacher 		err = ceph_sync_setxattr(inode, name, value, size, flags);
1120f1919826SYan, Zheng 		if (err >= 0 && check_realm) {
1121f1919826SYan, Zheng 			/* check if snaprealm was created for quota inode */
1122f1919826SYan, Zheng 			spin_lock(&ci->i_ceph_lock);
1123f1919826SYan, Zheng 			if ((ci->i_max_files || ci->i_max_bytes) &&
1124f1919826SYan, Zheng 			    !(ci->i_snap_realm &&
1125f1919826SYan, Zheng 			      ci->i_snap_realm->ino == ci->i_vino.ino))
1126f1919826SYan, Zheng 				err = -EOPNOTSUPP;
1127f1919826SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
1128f1919826SYan, Zheng 		}
1129315f2408SYan, Zheng 	}
1130355da1ebSSage Weil out:
1131f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
1132355da1ebSSage Weil 	kfree(newname);
1133355da1ebSSage Weil 	kfree(newval);
1134355da1ebSSage Weil 	kfree(xattr);
1135355da1ebSSage Weil 	return err;
1136355da1ebSSage Weil }
1137355da1ebSSage Weil 
11382cdeb1e4SAndreas Gruenbacher static int ceph_get_xattr_handler(const struct xattr_handler *handler,
11392cdeb1e4SAndreas Gruenbacher 				  struct dentry *dentry, struct inode *inode,
11402cdeb1e4SAndreas Gruenbacher 				  const char *name, void *value, size_t size)
11417221fe4cSGuangliang Zhao {
11422cdeb1e4SAndreas Gruenbacher 	if (!ceph_is_valid_xattr(name))
11432cdeb1e4SAndreas Gruenbacher 		return -EOPNOTSUPP;
11442cdeb1e4SAndreas Gruenbacher 	return __ceph_getxattr(inode, name, value, size);
11457221fe4cSGuangliang Zhao }
1146315f2408SYan, Zheng 
11472cdeb1e4SAndreas Gruenbacher static int ceph_set_xattr_handler(const struct xattr_handler *handler,
114859301226SAl Viro 				  struct dentry *unused, struct inode *inode,
114959301226SAl Viro 				  const char *name, const void *value,
115059301226SAl Viro 				  size_t size, int flags)
11512cdeb1e4SAndreas Gruenbacher {
11522cdeb1e4SAndreas Gruenbacher 	if (!ceph_is_valid_xattr(name))
11532cdeb1e4SAndreas Gruenbacher 		return -EOPNOTSUPP;
115459301226SAl Viro 	return __ceph_setxattr(inode, name, value, size, flags);
11552cdeb1e4SAndreas Gruenbacher }
11562cdeb1e4SAndreas Gruenbacher 
11575130cceaSWei Yongjun static const struct xattr_handler ceph_other_xattr_handler = {
11582cdeb1e4SAndreas Gruenbacher 	.prefix = "",  /* match any name => handlers called with full name */
11592cdeb1e4SAndreas Gruenbacher 	.get = ceph_get_xattr_handler,
11602cdeb1e4SAndreas Gruenbacher 	.set = ceph_set_xattr_handler,
11612cdeb1e4SAndreas Gruenbacher };
11622cdeb1e4SAndreas Gruenbacher 
1163315f2408SYan, Zheng #ifdef CONFIG_SECURITY
1164315f2408SYan, Zheng bool ceph_security_xattr_wanted(struct inode *in)
1165315f2408SYan, Zheng {
1166315f2408SYan, Zheng 	return in->i_security != NULL;
1167315f2408SYan, Zheng }
1168315f2408SYan, Zheng 
1169315f2408SYan, Zheng bool ceph_security_xattr_deadlock(struct inode *in)
1170315f2408SYan, Zheng {
1171315f2408SYan, Zheng 	struct ceph_inode_info *ci;
1172315f2408SYan, Zheng 	bool ret;
1173d37b1d99SMarkus Elfring 	if (!in->i_security)
1174315f2408SYan, Zheng 		return false;
1175315f2408SYan, Zheng 	ci = ceph_inode(in);
1176315f2408SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
1177315f2408SYan, Zheng 	ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1178315f2408SYan, Zheng 	      !(ci->i_xattrs.version > 0 &&
1179315f2408SYan, Zheng 		__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1180315f2408SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
1181315f2408SYan, Zheng 	return ret;
1182315f2408SYan, Zheng }
1183ac6713ccSYan, Zheng 
1184ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1185ac6713ccSYan, Zheng int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1186ac6713ccSYan, Zheng 			   struct ceph_acl_sec_ctx *as_ctx)
1187ac6713ccSYan, Zheng {
1188ac6713ccSYan, Zheng 	struct ceph_pagelist *pagelist = as_ctx->pagelist;
1189ac6713ccSYan, Zheng 	const char *name;
1190ac6713ccSYan, Zheng 	size_t name_len;
1191ac6713ccSYan, Zheng 	int err;
1192ac6713ccSYan, Zheng 
1193ac6713ccSYan, Zheng 	err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1194ac6713ccSYan, Zheng 					    &as_ctx->sec_ctx,
1195ac6713ccSYan, Zheng 					    &as_ctx->sec_ctxlen);
1196ac6713ccSYan, Zheng 	if (err < 0) {
1197ac6713ccSYan, Zheng 		WARN_ON_ONCE(err != -EOPNOTSUPP);
1198ac6713ccSYan, Zheng 		err = 0; /* do nothing */
1199ac6713ccSYan, Zheng 		goto out;
1200ac6713ccSYan, Zheng 	}
1201ac6713ccSYan, Zheng 
1202ac6713ccSYan, Zheng 	err = -ENOMEM;
1203ac6713ccSYan, Zheng 	if (!pagelist) {
1204ac6713ccSYan, Zheng 		pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1205ac6713ccSYan, Zheng 		if (!pagelist)
1206ac6713ccSYan, Zheng 			goto out;
1207ac6713ccSYan, Zheng 		err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1208ac6713ccSYan, Zheng 		if (err)
1209ac6713ccSYan, Zheng 			goto out;
1210ac6713ccSYan, Zheng 		ceph_pagelist_encode_32(pagelist, 1);
1211ac6713ccSYan, Zheng 	}
1212ac6713ccSYan, Zheng 
1213ac6713ccSYan, Zheng 	/*
1214ac6713ccSYan, Zheng 	 * FIXME: Make security_dentry_init_security() generic. Currently
1215ac6713ccSYan, Zheng 	 * It only supports single security module and only selinux has
1216ac6713ccSYan, Zheng 	 * dentry_init_security hook.
1217ac6713ccSYan, Zheng 	 */
1218ac6713ccSYan, Zheng 	name = XATTR_NAME_SELINUX;
1219ac6713ccSYan, Zheng 	name_len = strlen(name);
1220ac6713ccSYan, Zheng 	err = ceph_pagelist_reserve(pagelist,
1221ac6713ccSYan, Zheng 				    4 * 2 + name_len + as_ctx->sec_ctxlen);
1222ac6713ccSYan, Zheng 	if (err)
1223ac6713ccSYan, Zheng 		goto out;
1224ac6713ccSYan, Zheng 
1225ac6713ccSYan, Zheng 	if (as_ctx->pagelist) {
1226ac6713ccSYan, Zheng 		/* update count of KV pairs */
1227ac6713ccSYan, Zheng 		BUG_ON(pagelist->length <= sizeof(__le32));
1228ac6713ccSYan, Zheng 		if (list_is_singular(&pagelist->head)) {
1229ac6713ccSYan, Zheng 			le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1230ac6713ccSYan, Zheng 		} else {
1231ac6713ccSYan, Zheng 			struct page *page = list_first_entry(&pagelist->head,
1232ac6713ccSYan, Zheng 							     struct page, lru);
1233ac6713ccSYan, Zheng 			void *addr = kmap_atomic(page);
1234ac6713ccSYan, Zheng 			le32_add_cpu((__le32*)addr, 1);
1235ac6713ccSYan, Zheng 			kunmap_atomic(addr);
1236ac6713ccSYan, Zheng 		}
1237ac6713ccSYan, Zheng 	} else {
1238ac6713ccSYan, Zheng 		as_ctx->pagelist = pagelist;
1239ac6713ccSYan, Zheng 	}
1240ac6713ccSYan, Zheng 
1241ac6713ccSYan, Zheng 	ceph_pagelist_encode_32(pagelist, name_len);
1242ac6713ccSYan, Zheng 	ceph_pagelist_append(pagelist, name, name_len);
1243ac6713ccSYan, Zheng 
1244ac6713ccSYan, Zheng 	ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1245ac6713ccSYan, Zheng 	ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1246ac6713ccSYan, Zheng 
1247ac6713ccSYan, Zheng 	err = 0;
1248ac6713ccSYan, Zheng out:
1249ac6713ccSYan, Zheng 	if (pagelist && !as_ctx->pagelist)
1250ac6713ccSYan, Zheng 		ceph_pagelist_release(pagelist);
1251ac6713ccSYan, Zheng 	return err;
1252ac6713ccSYan, Zheng }
1253ac6713ccSYan, Zheng 
1254ac6713ccSYan, Zheng void ceph_security_invalidate_secctx(struct inode *inode)
1255ac6713ccSYan, Zheng {
1256ac6713ccSYan, Zheng 	security_inode_invalidate_secctx(inode);
1257ac6713ccSYan, Zheng }
1258ac6713ccSYan, Zheng 
1259ac6713ccSYan, Zheng static int ceph_xattr_set_security_label(const struct xattr_handler *handler,
1260ac6713ccSYan, Zheng 				    struct dentry *unused, struct inode *inode,
1261ac6713ccSYan, Zheng 				    const char *key, const void *buf,
1262ac6713ccSYan, Zheng 				    size_t buflen, int flags)
1263ac6713ccSYan, Zheng {
1264ac6713ccSYan, Zheng 	if (security_ismaclabel(key)) {
1265ac6713ccSYan, Zheng 		const char *name = xattr_full_name(handler, key);
1266ac6713ccSYan, Zheng 		return __ceph_setxattr(inode, name, buf, buflen, flags);
1267ac6713ccSYan, Zheng 	}
1268ac6713ccSYan, Zheng 	return  -EOPNOTSUPP;
1269ac6713ccSYan, Zheng }
1270ac6713ccSYan, Zheng 
1271ac6713ccSYan, Zheng static int ceph_xattr_get_security_label(const struct xattr_handler *handler,
1272ac6713ccSYan, Zheng 				    struct dentry *unused, struct inode *inode,
1273ac6713ccSYan, Zheng 				    const char *key, void *buf, size_t buflen)
1274ac6713ccSYan, Zheng {
1275ac6713ccSYan, Zheng 	if (security_ismaclabel(key)) {
1276ac6713ccSYan, Zheng 		const char *name = xattr_full_name(handler, key);
1277ac6713ccSYan, Zheng 		return __ceph_getxattr(inode, name, buf, buflen);
1278ac6713ccSYan, Zheng 	}
1279ac6713ccSYan, Zheng 	return  -EOPNOTSUPP;
1280ac6713ccSYan, Zheng }
1281ac6713ccSYan, Zheng 
1282ac6713ccSYan, Zheng static const struct xattr_handler ceph_security_label_handler = {
1283ac6713ccSYan, Zheng 	.prefix = XATTR_SECURITY_PREFIX,
1284ac6713ccSYan, Zheng 	.get    = ceph_xattr_get_security_label,
1285ac6713ccSYan, Zheng 	.set    = ceph_xattr_set_security_label,
1286ac6713ccSYan, Zheng };
1287ac6713ccSYan, Zheng #endif
1288315f2408SYan, Zheng #endif
12895c31e92dSYan, Zheng 
12905c31e92dSYan, Zheng void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
12915c31e92dSYan, Zheng {
12925c31e92dSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL
12935c31e92dSYan, Zheng 	posix_acl_release(as_ctx->acl);
12945c31e92dSYan, Zheng 	posix_acl_release(as_ctx->default_acl);
12955c31e92dSYan, Zheng #endif
1296ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1297ac6713ccSYan, Zheng 	security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1298ac6713ccSYan, Zheng #endif
12995c31e92dSYan, Zheng 	if (as_ctx->pagelist)
13005c31e92dSYan, Zheng 		ceph_pagelist_release(as_ctx->pagelist);
13015c31e92dSYan, Zheng }
1302ac6713ccSYan, Zheng 
1303ac6713ccSYan, Zheng /*
1304ac6713ccSYan, Zheng  * List of handlers for synthetic system.* attributes. Other
1305ac6713ccSYan, Zheng  * attributes are handled directly.
1306ac6713ccSYan, Zheng  */
1307ac6713ccSYan, Zheng const struct xattr_handler *ceph_xattr_handlers[] = {
1308ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL
1309ac6713ccSYan, Zheng 	&posix_acl_access_xattr_handler,
1310ac6713ccSYan, Zheng 	&posix_acl_default_xattr_handler,
1311ac6713ccSYan, Zheng #endif
1312ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1313ac6713ccSYan, Zheng 	&ceph_security_label_handler,
1314ac6713ccSYan, Zheng #endif
1315ac6713ccSYan, Zheng 	&ceph_other_xattr_handler,
1316ac6713ccSYan, Zheng 	NULL,
1317ac6713ccSYan, Zheng };
1318