xref: /openbmc/linux/fs/ceph/xattr.c (revision f6fbdcd997f5d5d0658204ca42abdeced56fd4e5)
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 {
23b8fe918bSJeff Layton 	return !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
24b8fe918bSJeff Layton 	       !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
25355da1ebSSage Weil 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
26355da1ebSSage Weil 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
27355da1ebSSage Weil }
28355da1ebSSage Weil 
29355da1ebSSage Weil /*
30355da1ebSSage Weil  * These define virtual xattrs exposing the recursive directory
31355da1ebSSage Weil  * statistics and layout metadata.
32355da1ebSSage Weil  */
33881a5fa2SAlex Elder struct ceph_vxattr {
34355da1ebSSage Weil 	char *name;
353ce6cd12SAlex Elder 	size_t name_size;	/* strlen(name) + 1 (for '\0') */
36f1d1b51dSJeff Layton 	ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
37355da1ebSSage Weil 			       size_t size);
38f36e4472SSage Weil 	bool (*exists_cb)(struct ceph_inode_info *ci);
394e9906e7SYan, Zheng 	unsigned int flags;
40355da1ebSSage Weil };
41355da1ebSSage Weil 
424e9906e7SYan, Zheng #define VXATTR_FLAG_READONLY		(1<<0)
434e9906e7SYan, Zheng #define VXATTR_FLAG_HIDDEN		(1<<1)
4449a9f4f6SYan, Zheng #define VXATTR_FLAG_RSTAT		(1<<2)
454e9906e7SYan, Zheng 
4632ab0bd7SSage Weil /* layouts */
4732ab0bd7SSage Weil 
4832ab0bd7SSage Weil static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
4932ab0bd7SSage Weil {
50779fe0fbSYan, Zheng 	struct ceph_file_layout *fl = &ci->i_layout;
51779fe0fbSYan, Zheng 	return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
52779fe0fbSYan, Zheng 		fl->object_size > 0 || fl->pool_id >= 0 ||
53779fe0fbSYan, Zheng 		rcu_dereference_raw(fl->pool_ns) != NULL);
5432ab0bd7SSage Weil }
5532ab0bd7SSage Weil 
56f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
5732ab0bd7SSage Weil 				    size_t size)
5832ab0bd7SSage Weil {
5932ab0bd7SSage Weil 	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
6032ab0bd7SSage Weil 	struct ceph_osd_client *osdc = &fsc->client->osdc;
61779fe0fbSYan, Zheng 	struct ceph_string *pool_ns;
627627151eSYan, Zheng 	s64 pool = ci->i_layout.pool_id;
6332ab0bd7SSage Weil 	const char *pool_name;
64779fe0fbSYan, Zheng 	const char *ns_field = " pool_namespace=";
651e5c6649SYan, Zheng 	char buf[128];
66779fe0fbSYan, Zheng 	size_t len, total_len = 0;
673b421018SJeff Layton 	ssize_t ret;
68779fe0fbSYan, Zheng 
69779fe0fbSYan, Zheng 	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
7032ab0bd7SSage Weil 
7132ab0bd7SSage Weil 	dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
725aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
7332ab0bd7SSage Weil 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
741e5c6649SYan, Zheng 	if (pool_name) {
75779fe0fbSYan, Zheng 		len = snprintf(buf, sizeof(buf),
767627151eSYan, Zheng 		"stripe_unit=%u stripe_count=%u object_size=%u pool=",
777627151eSYan, Zheng 		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
787627151eSYan, Zheng 	        ci->i_layout.object_size);
79779fe0fbSYan, Zheng 		total_len = len + strlen(pool_name);
801e5c6649SYan, Zheng 	} else {
81779fe0fbSYan, Zheng 		len = snprintf(buf, sizeof(buf),
827627151eSYan, Zheng 		"stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
837627151eSYan, Zheng 		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
84f1d1b51dSJeff Layton 		ci->i_layout.object_size, pool);
85779fe0fbSYan, Zheng 		total_len = len;
86779fe0fbSYan, Zheng 	}
87779fe0fbSYan, Zheng 
88779fe0fbSYan, Zheng 	if (pool_ns)
89779fe0fbSYan, Zheng 		total_len += strlen(ns_field) + pool_ns->len;
90779fe0fbSYan, Zheng 
91779fe0fbSYan, Zheng 	ret = total_len;
923b421018SJeff Layton 	if (size >= total_len) {
93779fe0fbSYan, Zheng 		memcpy(val, buf, len);
94779fe0fbSYan, Zheng 		ret = len;
95779fe0fbSYan, Zheng 		if (pool_name) {
96779fe0fbSYan, Zheng 			len = strlen(pool_name);
97779fe0fbSYan, Zheng 			memcpy(val + ret, pool_name, len);
98779fe0fbSYan, Zheng 			ret += len;
99779fe0fbSYan, Zheng 		}
100779fe0fbSYan, Zheng 		if (pool_ns) {
101779fe0fbSYan, Zheng 			len = strlen(ns_field);
102779fe0fbSYan, Zheng 			memcpy(val + ret, ns_field, len);
103779fe0fbSYan, Zheng 			ret += len;
104779fe0fbSYan, Zheng 			memcpy(val + ret, pool_ns->str, pool_ns->len);
105779fe0fbSYan, Zheng 			ret += pool_ns->len;
1061e5c6649SYan, Zheng 		}
1071e5c6649SYan, Zheng 	}
1085aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
109779fe0fbSYan, Zheng 	ceph_put_string(pool_ns);
11032ab0bd7SSage Weil 	return ret;
11132ab0bd7SSage Weil }
11232ab0bd7SSage Weil 
11326350535SJeff Layton /*
11426350535SJeff Layton  * The convention with strings in xattrs is that they should not be NULL
11526350535SJeff Layton  * terminated, since we're returning the length with them. snprintf always
11626350535SJeff Layton  * NULL terminates however, so call it on a temporary buffer and then memcpy
11726350535SJeff Layton  * the result into place.
11826350535SJeff Layton  */
119*f6fbdcd9SIlya Dryomov static __printf(3, 4)
120*f6fbdcd9SIlya Dryomov int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
12126350535SJeff Layton {
12226350535SJeff Layton 	int ret;
12326350535SJeff Layton 	va_list args;
12426350535SJeff Layton 	char buf[96]; /* NB: reevaluate size if new vxattrs are added */
12526350535SJeff Layton 
12626350535SJeff Layton 	va_start(args, fmt);
12726350535SJeff Layton 	ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
12826350535SJeff Layton 	va_end(args);
12926350535SJeff Layton 
13026350535SJeff Layton 	/* Sanity check */
13126350535SJeff Layton 	if (size && ret + 1 > sizeof(buf)) {
13226350535SJeff Layton 		WARN_ONCE(true, "Returned length too big (%d)", ret);
13326350535SJeff Layton 		return -E2BIG;
13426350535SJeff Layton 	}
13526350535SJeff Layton 
13626350535SJeff Layton 	if (ret <= size)
13726350535SJeff Layton 		memcpy(val, buf, ret);
13826350535SJeff Layton 	return ret;
13926350535SJeff Layton }
14026350535SJeff Layton 
141f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
142695b7119SSage Weil 						char *val, size_t size)
143695b7119SSage Weil {
14426350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
145695b7119SSage Weil }
146695b7119SSage Weil 
147f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
148695b7119SSage Weil 						 char *val, size_t size)
149695b7119SSage Weil {
15026350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
151695b7119SSage Weil }
152695b7119SSage Weil 
153f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
154695b7119SSage Weil 						char *val, size_t size)
155695b7119SSage Weil {
15626350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
157695b7119SSage Weil }
158695b7119SSage Weil 
159f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
160695b7119SSage Weil 					 char *val, size_t size)
161695b7119SSage Weil {
162f1d1b51dSJeff Layton 	ssize_t ret;
163695b7119SSage Weil 	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
164695b7119SSage Weil 	struct ceph_osd_client *osdc = &fsc->client->osdc;
1657627151eSYan, Zheng 	s64 pool = ci->i_layout.pool_id;
166695b7119SSage Weil 	const char *pool_name;
167695b7119SSage Weil 
1685aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
169695b7119SSage Weil 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
17026350535SJeff Layton 	if (pool_name) {
17126350535SJeff Layton 		ret = strlen(pool_name);
17226350535SJeff Layton 		if (ret <= size)
17326350535SJeff Layton 			memcpy(val, pool_name, ret);
17426350535SJeff Layton 	} else {
17526350535SJeff Layton 		ret = ceph_fmt_xattr(val, size, "%lld", pool);
17626350535SJeff Layton 	}
1775aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
178695b7119SSage Weil 	return ret;
179695b7119SSage Weil }
180695b7119SSage Weil 
181f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
182779fe0fbSYan, Zheng 						   char *val, size_t size)
183779fe0fbSYan, Zheng {
18426350535SJeff Layton 	ssize_t ret = 0;
185779fe0fbSYan, Zheng 	struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
18626350535SJeff Layton 
187779fe0fbSYan, Zheng 	if (ns) {
18826350535SJeff Layton 		ret = ns->len;
18926350535SJeff Layton 		if (ret <= size)
19026350535SJeff Layton 			memcpy(val, ns->str, ret);
191779fe0fbSYan, Zheng 		ceph_put_string(ns);
192779fe0fbSYan, Zheng 	}
193779fe0fbSYan, Zheng 	return ret;
194779fe0fbSYan, Zheng }
195779fe0fbSYan, Zheng 
196355da1ebSSage Weil /* directories */
197355da1ebSSage Weil 
198f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
199355da1ebSSage Weil 					 size_t size)
200355da1ebSSage Weil {
20126350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
202355da1ebSSage Weil }
203355da1ebSSage Weil 
204f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
205355da1ebSSage Weil 				       size_t size)
206355da1ebSSage Weil {
20726350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
208355da1ebSSage Weil }
209355da1ebSSage Weil 
210f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
211355da1ebSSage Weil 					 size_t size)
212355da1ebSSage Weil {
21326350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
214355da1ebSSage Weil }
215355da1ebSSage Weil 
216f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
217355da1ebSSage Weil 					  size_t size)
218355da1ebSSage Weil {
21926350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld",
22026350535SJeff Layton 				ci->i_rfiles + ci->i_rsubdirs);
221355da1ebSSage Weil }
222355da1ebSSage Weil 
223f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
224355da1ebSSage Weil 					size_t size)
225355da1ebSSage Weil {
22626350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
227355da1ebSSage Weil }
228355da1ebSSage Weil 
229f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
230355da1ebSSage Weil 					  size_t size)
231355da1ebSSage Weil {
23226350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
233355da1ebSSage Weil }
234355da1ebSSage Weil 
235f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
236355da1ebSSage Weil 					size_t size)
237355da1ebSSage Weil {
23826350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
239355da1ebSSage Weil }
240355da1ebSSage Weil 
241f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
242355da1ebSSage Weil 					size_t size)
243355da1ebSSage Weil {
24426350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
2459bbeab41SArnd Bergmann 				ci->i_rctime.tv_nsec);
246355da1ebSSage Weil }
247355da1ebSSage Weil 
24808796873SYan, Zheng /* dir pin */
24908796873SYan, Zheng static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
25008796873SYan, Zheng {
25108796873SYan, Zheng 	return ci->i_dir_pin != -ENODATA;
25208796873SYan, Zheng }
253fb18a575SLuis Henriques 
254f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
25508796873SYan, Zheng 				     size_t size)
25608796873SYan, Zheng {
25726350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
25808796873SYan, Zheng }
25908796873SYan, Zheng 
26008796873SYan, Zheng /* quotas */
261fb18a575SLuis Henriques static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
262fb18a575SLuis Henriques {
263f1919826SYan, Zheng 	bool ret = false;
264f1919826SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
265f1919826SYan, Zheng 	if ((ci->i_max_files || ci->i_max_bytes) &&
266f1919826SYan, Zheng 	    ci->i_vino.snap == CEPH_NOSNAP &&
267f1919826SYan, Zheng 	    ci->i_snap_realm &&
268f1919826SYan, Zheng 	    ci->i_snap_realm->ino == ci->i_vino.ino)
269f1919826SYan, Zheng 		ret = true;
270f1919826SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
271f1919826SYan, Zheng 	return ret;
272fb18a575SLuis Henriques }
273fb18a575SLuis Henriques 
274f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
275fb18a575SLuis Henriques 				   size_t size)
276fb18a575SLuis Henriques {
27726350535SJeff Layton 	return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
278fb18a575SLuis Henriques 				ci->i_max_bytes, ci->i_max_files);
279fb18a575SLuis Henriques }
280fb18a575SLuis Henriques 
281f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
282fb18a575SLuis Henriques 					     char *val, size_t size)
283fb18a575SLuis Henriques {
28426350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
285fb18a575SLuis Henriques }
286fb18a575SLuis Henriques 
287f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
288fb18a575SLuis Henriques 					     char *val, size_t size)
289fb18a575SLuis Henriques {
29026350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
291fb18a575SLuis Henriques }
29232ab0bd7SSage Weil 
293100cc610SDavid Disseldorp /* snapshots */
294100cc610SDavid Disseldorp static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
295100cc610SDavid Disseldorp {
296100cc610SDavid Disseldorp 	return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
297100cc610SDavid Disseldorp }
298100cc610SDavid Disseldorp 
299f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
300100cc610SDavid Disseldorp 					size_t size)
301100cc610SDavid Disseldorp {
30226350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
303100cc610SDavid Disseldorp 				ci->i_snap_btime.tv_nsec);
304100cc610SDavid Disseldorp }
305100cc610SDavid Disseldorp 
306eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
307695b7119SSage Weil #define CEPH_XATTR_NAME2(_type, _name, _name2)	\
308695b7119SSage Weil 	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
309eb788084SAlex Elder 
31049a9f4f6SYan, Zheng #define XATTR_NAME_CEPH(_type, _name, _flags)				\
311eb788084SAlex Elder 	{								\
312eb788084SAlex Elder 		.name = CEPH_XATTR_NAME(_type, _name),			\
3133ce6cd12SAlex Elder 		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
314aa4066edSAlex Elder 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
315f36e4472SSage Weil 		.exists_cb = NULL,					\
31649a9f4f6SYan, Zheng 		.flags = (VXATTR_FLAG_READONLY | _flags),		\
317eb788084SAlex Elder 	}
31849a9f4f6SYan, Zheng #define XATTR_RSTAT_FIELD(_type, _name)			\
31949a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
320695b7119SSage Weil #define XATTR_LAYOUT_FIELD(_type, _name, _field)			\
321695b7119SSage Weil 	{								\
322695b7119SSage Weil 		.name = CEPH_XATTR_NAME2(_type, _name, _field),	\
323695b7119SSage Weil 		.name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
324695b7119SSage Weil 		.getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
325695b7119SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,	\
3264e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,			\
327695b7119SSage Weil 	}
328fb18a575SLuis Henriques #define XATTR_QUOTA_FIELD(_type, _name)					\
329fb18a575SLuis Henriques 	{								\
330fb18a575SLuis Henriques 		.name = CEPH_XATTR_NAME(_type, _name),			\
331fb18a575SLuis Henriques 		.name_size = sizeof(CEPH_XATTR_NAME(_type, _name)),	\
332fb18a575SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name,	\
333fb18a575SLuis Henriques 		.exists_cb = ceph_vxattrcb_quota_exists,		\
3344e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,				\
335fb18a575SLuis Henriques 	}
336eb788084SAlex Elder 
337881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = {
3381f08f2b0SSage Weil 	{
3391f08f2b0SSage Weil 		.name = "ceph.dir.layout",
3401f08f2b0SSage Weil 		.name_size = sizeof("ceph.dir.layout"),
3411f08f2b0SSage Weil 		.getxattr_cb = ceph_vxattrcb_layout,
3421f08f2b0SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,
3434e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
3441f08f2b0SSage Weil 	},
345695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
346695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
347695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, object_size),
348695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, pool),
349779fe0fbSYan, Zheng 	XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
35049a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(dir, entries, 0),
35149a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(dir, files, 0),
35249a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(dir, subdirs, 0),
35349a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rentries),
35449a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rfiles),
35549a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rsubdirs),
35649a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rbytes),
35749a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rctime),
358fb18a575SLuis Henriques 	{
35908796873SYan, Zheng 		.name = "ceph.dir.pin",
360e1b81439SDavid Disseldorp 		.name_size = sizeof("ceph.dir.pin"),
36108796873SYan, Zheng 		.getxattr_cb = ceph_vxattrcb_dir_pin,
36208796873SYan, Zheng 		.exists_cb = ceph_vxattrcb_dir_pin_exists,
36308796873SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
36408796873SYan, Zheng 	},
36508796873SYan, Zheng 	{
366fb18a575SLuis Henriques 		.name = "ceph.quota",
367fb18a575SLuis Henriques 		.name_size = sizeof("ceph.quota"),
368fb18a575SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_quota,
369fb18a575SLuis Henriques 		.exists_cb = ceph_vxattrcb_quota_exists,
3704e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
371fb18a575SLuis Henriques 	},
372fb18a575SLuis Henriques 	XATTR_QUOTA_FIELD(quota, max_bytes),
373fb18a575SLuis Henriques 	XATTR_QUOTA_FIELD(quota, max_files),
374100cc610SDavid Disseldorp 	{
375100cc610SDavid Disseldorp 		.name = "ceph.snap.btime",
376100cc610SDavid Disseldorp 		.name_size = sizeof("ceph.snap.btime"),
377100cc610SDavid Disseldorp 		.getxattr_cb = ceph_vxattrcb_snap_btime,
378100cc610SDavid Disseldorp 		.exists_cb = ceph_vxattrcb_snap_btime_exists,
379100cc610SDavid Disseldorp 		.flags = VXATTR_FLAG_READONLY,
380100cc610SDavid Disseldorp 	},
3812c3dd4ffSAlex Elder 	{ .name = NULL, 0 }	/* Required table terminator */
382355da1ebSSage Weil };
383355da1ebSSage Weil 
384355da1ebSSage Weil /* files */
385355da1ebSSage Weil 
386881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = {
38732ab0bd7SSage Weil 	{
38832ab0bd7SSage Weil 		.name = "ceph.file.layout",
38932ab0bd7SSage Weil 		.name_size = sizeof("ceph.file.layout"),
39032ab0bd7SSage Weil 		.getxattr_cb = ceph_vxattrcb_layout,
39132ab0bd7SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,
3924e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
39332ab0bd7SSage Weil 	},
394695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
395695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, stripe_count),
396695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, object_size),
397695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, pool),
398779fe0fbSYan, Zheng 	XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
399100cc610SDavid Disseldorp 	{
400100cc610SDavid Disseldorp 		.name = "ceph.snap.btime",
401100cc610SDavid Disseldorp 		.name_size = sizeof("ceph.snap.btime"),
402100cc610SDavid Disseldorp 		.getxattr_cb = ceph_vxattrcb_snap_btime,
403100cc610SDavid Disseldorp 		.exists_cb = ceph_vxattrcb_snap_btime_exists,
404100cc610SDavid Disseldorp 		.flags = VXATTR_FLAG_READONLY,
405100cc610SDavid Disseldorp 	},
4062c3dd4ffSAlex Elder 	{ .name = NULL, 0 }	/* Required table terminator */
407355da1ebSSage Weil };
408355da1ebSSage Weil 
409881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
410355da1ebSSage Weil {
411355da1ebSSage Weil 	if (S_ISDIR(inode->i_mode))
412355da1ebSSage Weil 		return ceph_dir_vxattrs;
413355da1ebSSage Weil 	else if (S_ISREG(inode->i_mode))
414355da1ebSSage Weil 		return ceph_file_vxattrs;
415355da1ebSSage Weil 	return NULL;
416355da1ebSSage Weil }
417355da1ebSSage Weil 
418881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
419355da1ebSSage Weil 						const char *name)
420355da1ebSSage Weil {
421881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
42206476a69SAlex Elder 
42306476a69SAlex Elder 	if (vxattr) {
42406476a69SAlex Elder 		while (vxattr->name) {
42506476a69SAlex Elder 			if (!strcmp(vxattr->name, name))
426355da1ebSSage Weil 				return vxattr;
427355da1ebSSage Weil 			vxattr++;
42806476a69SAlex Elder 		}
42906476a69SAlex Elder 	}
43006476a69SAlex Elder 
431355da1ebSSage Weil 	return NULL;
432355da1ebSSage Weil }
433355da1ebSSage Weil 
434355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci,
435355da1ebSSage Weil 			   const char *name, int name_len,
436355da1ebSSage Weil 			   const char *val, int val_len,
437fbc0b970SYan, Zheng 			   int flags, int update_xattr,
438355da1ebSSage Weil 			   struct ceph_inode_xattr **newxattr)
439355da1ebSSage Weil {
440355da1ebSSage Weil 	struct rb_node **p;
441355da1ebSSage Weil 	struct rb_node *parent = NULL;
442355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
443355da1ebSSage Weil 	int c;
444355da1ebSSage Weil 	int new = 0;
445355da1ebSSage Weil 
446355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
447355da1ebSSage Weil 	while (*p) {
448355da1ebSSage Weil 		parent = *p;
449355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
450355da1ebSSage Weil 		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
451355da1ebSSage Weil 		if (c < 0)
452355da1ebSSage Weil 			p = &(*p)->rb_left;
453355da1ebSSage Weil 		else if (c > 0)
454355da1ebSSage Weil 			p = &(*p)->rb_right;
455355da1ebSSage Weil 		else {
456355da1ebSSage Weil 			if (name_len == xattr->name_len)
457355da1ebSSage Weil 				break;
458355da1ebSSage Weil 			else if (name_len < xattr->name_len)
459355da1ebSSage Weil 				p = &(*p)->rb_left;
460355da1ebSSage Weil 			else
461355da1ebSSage Weil 				p = &(*p)->rb_right;
462355da1ebSSage Weil 		}
463355da1ebSSage Weil 		xattr = NULL;
464355da1ebSSage Weil 	}
465355da1ebSSage Weil 
466fbc0b970SYan, Zheng 	if (update_xattr) {
467fbc0b970SYan, Zheng 		int err = 0;
468eeca958dSLuis Henriques 
469fbc0b970SYan, Zheng 		if (xattr && (flags & XATTR_CREATE))
470fbc0b970SYan, Zheng 			err = -EEXIST;
471fbc0b970SYan, Zheng 		else if (!xattr && (flags & XATTR_REPLACE))
472fbc0b970SYan, Zheng 			err = -ENODATA;
473fbc0b970SYan, Zheng 		if (err) {
474fbc0b970SYan, Zheng 			kfree(name);
475fbc0b970SYan, Zheng 			kfree(val);
476eeca958dSLuis Henriques 			kfree(*newxattr);
477fbc0b970SYan, Zheng 			return err;
478fbc0b970SYan, Zheng 		}
479bcdfeb2eSYan, Zheng 		if (update_xattr < 0) {
480bcdfeb2eSYan, Zheng 			if (xattr)
481bcdfeb2eSYan, Zheng 				__remove_xattr(ci, xattr);
482bcdfeb2eSYan, Zheng 			kfree(name);
483eeca958dSLuis Henriques 			kfree(*newxattr);
484bcdfeb2eSYan, Zheng 			return 0;
485bcdfeb2eSYan, Zheng 		}
486fbc0b970SYan, Zheng 	}
487fbc0b970SYan, Zheng 
488355da1ebSSage Weil 	if (!xattr) {
489355da1ebSSage Weil 		new = 1;
490355da1ebSSage Weil 		xattr = *newxattr;
491355da1ebSSage Weil 		xattr->name = name;
492355da1ebSSage Weil 		xattr->name_len = name_len;
493fbc0b970SYan, Zheng 		xattr->should_free_name = update_xattr;
494355da1ebSSage Weil 
495355da1ebSSage Weil 		ci->i_xattrs.count++;
496355da1ebSSage Weil 		dout("__set_xattr count=%d\n", ci->i_xattrs.count);
497355da1ebSSage Weil 	} else {
498355da1ebSSage Weil 		kfree(*newxattr);
499355da1ebSSage Weil 		*newxattr = NULL;
500355da1ebSSage Weil 		if (xattr->should_free_val)
501c00e4522SXu Wang 			kfree(xattr->val);
502355da1ebSSage Weil 
503fbc0b970SYan, Zheng 		if (update_xattr) {
504c00e4522SXu Wang 			kfree(name);
505355da1ebSSage Weil 			name = xattr->name;
506355da1ebSSage Weil 		}
507355da1ebSSage Weil 		ci->i_xattrs.names_size -= xattr->name_len;
508355da1ebSSage Weil 		ci->i_xattrs.vals_size -= xattr->val_len;
509355da1ebSSage Weil 	}
510355da1ebSSage Weil 	ci->i_xattrs.names_size += name_len;
511355da1ebSSage Weil 	ci->i_xattrs.vals_size += val_len;
512355da1ebSSage Weil 	if (val)
513355da1ebSSage Weil 		xattr->val = val;
514355da1ebSSage Weil 	else
515355da1ebSSage Weil 		xattr->val = "";
516355da1ebSSage Weil 
517355da1ebSSage Weil 	xattr->val_len = val_len;
518fbc0b970SYan, Zheng 	xattr->dirty = update_xattr;
519fbc0b970SYan, Zheng 	xattr->should_free_val = (val && update_xattr);
520355da1ebSSage Weil 
521355da1ebSSage Weil 	if (new) {
522355da1ebSSage Weil 		rb_link_node(&xattr->node, parent, p);
523355da1ebSSage Weil 		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
524355da1ebSSage Weil 		dout("__set_xattr_val p=%p\n", p);
525355da1ebSSage Weil 	}
526355da1ebSSage Weil 
52705729781SYan, Zheng 	dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
52805729781SYan, Zheng 	     ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
529355da1ebSSage Weil 
530355da1ebSSage Weil 	return 0;
531355da1ebSSage Weil }
532355da1ebSSage Weil 
533355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
534355da1ebSSage Weil 			   const char *name)
535355da1ebSSage Weil {
536355da1ebSSage Weil 	struct rb_node **p;
537355da1ebSSage Weil 	struct rb_node *parent = NULL;
538355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
53917db143fSSage Weil 	int name_len = strlen(name);
540355da1ebSSage Weil 	int c;
541355da1ebSSage Weil 
542355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
543355da1ebSSage Weil 	while (*p) {
544355da1ebSSage Weil 		parent = *p;
545355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
546355da1ebSSage Weil 		c = strncmp(name, xattr->name, xattr->name_len);
54717db143fSSage Weil 		if (c == 0 && name_len > xattr->name_len)
54817db143fSSage Weil 			c = 1;
549355da1ebSSage Weil 		if (c < 0)
550355da1ebSSage Weil 			p = &(*p)->rb_left;
551355da1ebSSage Weil 		else if (c > 0)
552355da1ebSSage Weil 			p = &(*p)->rb_right;
553355da1ebSSage Weil 		else {
554355da1ebSSage Weil 			dout("__get_xattr %s: found %.*s\n", name,
555355da1ebSSage Weil 			     xattr->val_len, xattr->val);
556355da1ebSSage Weil 			return xattr;
557355da1ebSSage Weil 		}
558355da1ebSSage Weil 	}
559355da1ebSSage Weil 
560355da1ebSSage Weil 	dout("__get_xattr %s: not found\n", name);
561355da1ebSSage Weil 
562355da1ebSSage Weil 	return NULL;
563355da1ebSSage Weil }
564355da1ebSSage Weil 
565355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr)
566355da1ebSSage Weil {
567355da1ebSSage Weil 	BUG_ON(!xattr);
568355da1ebSSage Weil 
569355da1ebSSage Weil 	if (xattr->should_free_name)
570c00e4522SXu Wang 		kfree(xattr->name);
571355da1ebSSage Weil 	if (xattr->should_free_val)
572c00e4522SXu Wang 		kfree(xattr->val);
573355da1ebSSage Weil 
574355da1ebSSage Weil 	kfree(xattr);
575355da1ebSSage Weil }
576355da1ebSSage Weil 
577355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci,
578355da1ebSSage Weil 			  struct ceph_inode_xattr *xattr)
579355da1ebSSage Weil {
580355da1ebSSage Weil 	if (!xattr)
581524186acSYan, Zheng 		return -ENODATA;
582355da1ebSSage Weil 
583355da1ebSSage Weil 	rb_erase(&xattr->node, &ci->i_xattrs.index);
584355da1ebSSage Weil 
585355da1ebSSage Weil 	if (xattr->should_free_name)
586c00e4522SXu Wang 		kfree(xattr->name);
587355da1ebSSage Weil 	if (xattr->should_free_val)
588c00e4522SXu Wang 		kfree(xattr->val);
589355da1ebSSage Weil 
590355da1ebSSage Weil 	ci->i_xattrs.names_size -= xattr->name_len;
591355da1ebSSage Weil 	ci->i_xattrs.vals_size -= xattr->val_len;
592355da1ebSSage Weil 	ci->i_xattrs.count--;
593355da1ebSSage Weil 	kfree(xattr);
594355da1ebSSage Weil 
595355da1ebSSage Weil 	return 0;
596355da1ebSSage Weil }
597355da1ebSSage Weil 
598355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci,
599355da1ebSSage Weil 				char *dest)
600355da1ebSSage Weil {
601355da1ebSSage Weil 	struct rb_node *p;
602355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
603355da1ebSSage Weil 
604355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
605355da1ebSSage Weil 	dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
606355da1ebSSage Weil 
607355da1ebSSage Weil 	while (p) {
608355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
609355da1ebSSage Weil 		memcpy(dest, xattr->name, xattr->name_len);
610355da1ebSSage Weil 		dest[xattr->name_len] = '\0';
611355da1ebSSage Weil 
612355da1ebSSage Weil 		dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
613355da1ebSSage Weil 		     xattr->name_len, ci->i_xattrs.names_size);
614355da1ebSSage Weil 
615355da1ebSSage Weil 		dest += xattr->name_len + 1;
616355da1ebSSage Weil 		p = rb_next(p);
617355da1ebSSage Weil 	}
618355da1ebSSage Weil 
619355da1ebSSage Weil 	return dest;
620355da1ebSSage Weil }
621355da1ebSSage Weil 
622355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
623355da1ebSSage Weil {
624355da1ebSSage Weil 	struct rb_node *p, *tmp;
625355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
626355da1ebSSage Weil 
627355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
628355da1ebSSage Weil 
629355da1ebSSage Weil 	dout("__ceph_destroy_xattrs p=%p\n", p);
630355da1ebSSage Weil 
631355da1ebSSage Weil 	while (p) {
632355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
633355da1ebSSage Weil 		tmp = p;
634355da1ebSSage Weil 		p = rb_next(tmp);
635355da1ebSSage Weil 		dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
636355da1ebSSage Weil 		     xattr->name_len, xattr->name);
637355da1ebSSage Weil 		rb_erase(tmp, &ci->i_xattrs.index);
638355da1ebSSage Weil 
639355da1ebSSage Weil 		__free_xattr(xattr);
640355da1ebSSage Weil 	}
641355da1ebSSage Weil 
642355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
643355da1ebSSage Weil 	ci->i_xattrs.vals_size = 0;
644355da1ebSSage Weil 	ci->i_xattrs.index_version = 0;
645355da1ebSSage Weil 	ci->i_xattrs.count = 0;
646355da1ebSSage Weil 	ci->i_xattrs.index = RB_ROOT;
647355da1ebSSage Weil }
648355da1ebSSage Weil 
649355da1ebSSage Weil static int __build_xattrs(struct inode *inode)
650be655596SSage Weil 	__releases(ci->i_ceph_lock)
651be655596SSage Weil 	__acquires(ci->i_ceph_lock)
652355da1ebSSage Weil {
653355da1ebSSage Weil 	u32 namelen;
654355da1ebSSage Weil 	u32 numattr = 0;
655355da1ebSSage Weil 	void *p, *end;
656355da1ebSSage Weil 	u32 len;
657355da1ebSSage Weil 	const char *name, *val;
658355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
6590eb30853SXiubo Li 	u64 xattr_version;
660355da1ebSSage Weil 	struct ceph_inode_xattr **xattrs = NULL;
66163ff78b2SSage Weil 	int err = 0;
662355da1ebSSage Weil 	int i;
663355da1ebSSage Weil 
664355da1ebSSage Weil 	dout("__build_xattrs() len=%d\n",
665355da1ebSSage Weil 	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
666355da1ebSSage Weil 
667355da1ebSSage Weil 	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
668355da1ebSSage Weil 		return 0; /* already built */
669355da1ebSSage Weil 
670355da1ebSSage Weil 	__ceph_destroy_xattrs(ci);
671355da1ebSSage Weil 
672355da1ebSSage Weil start:
673355da1ebSSage Weil 	/* updated internal xattr rb tree */
674355da1ebSSage Weil 	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
675355da1ebSSage Weil 		p = ci->i_xattrs.blob->vec.iov_base;
676355da1ebSSage Weil 		end = p + ci->i_xattrs.blob->vec.iov_len;
677355da1ebSSage Weil 		ceph_decode_32_safe(&p, end, numattr, bad);
678355da1ebSSage Weil 		xattr_version = ci->i_xattrs.version;
679be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
680355da1ebSSage Weil 
6817e8a2952SIlya Dryomov 		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
682355da1ebSSage Weil 				 GFP_NOFS);
683355da1ebSSage Weil 		err = -ENOMEM;
684355da1ebSSage Weil 		if (!xattrs)
685355da1ebSSage Weil 			goto bad_lock;
6861a295bd8SIlya Dryomov 
687355da1ebSSage Weil 		for (i = 0; i < numattr; i++) {
688355da1ebSSage Weil 			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
689355da1ebSSage Weil 					    GFP_NOFS);
690355da1ebSSage Weil 			if (!xattrs[i])
691355da1ebSSage Weil 				goto bad_lock;
692355da1ebSSage Weil 		}
693355da1ebSSage Weil 
694be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
695355da1ebSSage Weil 		if (ci->i_xattrs.version != xattr_version) {
696355da1ebSSage Weil 			/* lost a race, retry */
697355da1ebSSage Weil 			for (i = 0; i < numattr; i++)
698355da1ebSSage Weil 				kfree(xattrs[i]);
699355da1ebSSage Weil 			kfree(xattrs);
70021ec6ffaSAlan Cox 			xattrs = NULL;
701355da1ebSSage Weil 			goto start;
702355da1ebSSage Weil 		}
703355da1ebSSage Weil 		err = -EIO;
704355da1ebSSage Weil 		while (numattr--) {
705355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
706355da1ebSSage Weil 			namelen = len;
707355da1ebSSage Weil 			name = p;
708355da1ebSSage Weil 			p += len;
709355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
710355da1ebSSage Weil 			val = p;
711355da1ebSSage Weil 			p += len;
712355da1ebSSage Weil 
713355da1ebSSage Weil 			err = __set_xattr(ci, name, namelen, val, len,
714fbc0b970SYan, Zheng 					  0, 0, &xattrs[numattr]);
715355da1ebSSage Weil 
716355da1ebSSage Weil 			if (err < 0)
717355da1ebSSage Weil 				goto bad;
718355da1ebSSage Weil 		}
719355da1ebSSage Weil 		kfree(xattrs);
720355da1ebSSage Weil 	}
721355da1ebSSage Weil 	ci->i_xattrs.index_version = ci->i_xattrs.version;
722355da1ebSSage Weil 	ci->i_xattrs.dirty = false;
723355da1ebSSage Weil 
724355da1ebSSage Weil 	return err;
725355da1ebSSage Weil bad_lock:
726be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
727355da1ebSSage Weil bad:
728355da1ebSSage Weil 	if (xattrs) {
729355da1ebSSage Weil 		for (i = 0; i < numattr; i++)
730355da1ebSSage Weil 			kfree(xattrs[i]);
731355da1ebSSage Weil 		kfree(xattrs);
732355da1ebSSage Weil 	}
733355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
734355da1ebSSage Weil 	return err;
735355da1ebSSage Weil }
736355da1ebSSage Weil 
737355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
738355da1ebSSage Weil 				    int val_size)
739355da1ebSSage Weil {
740355da1ebSSage Weil 	/*
741355da1ebSSage Weil 	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
742355da1ebSSage Weil 	 * 4 bytes per each value
743355da1ebSSage Weil 	 */
744355da1ebSSage Weil 	int size = 4 + ci->i_xattrs.count*(4 + 4) +
745355da1ebSSage Weil 			     ci->i_xattrs.names_size +
746355da1ebSSage Weil 			     ci->i_xattrs.vals_size;
747355da1ebSSage Weil 	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
748355da1ebSSage Weil 	     ci->i_xattrs.count, ci->i_xattrs.names_size,
749355da1ebSSage Weil 	     ci->i_xattrs.vals_size);
750355da1ebSSage Weil 
751355da1ebSSage Weil 	if (name_size)
752355da1ebSSage Weil 		size += 4 + 4 + name_size + val_size;
753355da1ebSSage Weil 
754355da1ebSSage Weil 	return size;
755355da1ebSSage Weil }
756355da1ebSSage Weil 
757355da1ebSSage Weil /*
758355da1ebSSage Weil  * If there are dirty xattrs, reencode xattrs into the prealloc_blob
75912fe3ddaSLuis Henriques  * and swap into place.  It returns the old i_xattrs.blob (or NULL) so
76012fe3ddaSLuis Henriques  * that it can be freed by the caller as the i_ceph_lock is likely to be
76112fe3ddaSLuis Henriques  * held.
762355da1ebSSage Weil  */
76312fe3ddaSLuis Henriques struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
764355da1ebSSage Weil {
765355da1ebSSage Weil 	struct rb_node *p;
766355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
76712fe3ddaSLuis Henriques 	struct ceph_buffer *old_blob = NULL;
768355da1ebSSage Weil 	void *dest;
769355da1ebSSage Weil 
770355da1ebSSage Weil 	dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
771355da1ebSSage Weil 	if (ci->i_xattrs.dirty) {
772355da1ebSSage Weil 		int need = __get_required_blob_size(ci, 0, 0);
773355da1ebSSage Weil 
774355da1ebSSage Weil 		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
775355da1ebSSage Weil 
776355da1ebSSage Weil 		p = rb_first(&ci->i_xattrs.index);
777355da1ebSSage Weil 		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
778355da1ebSSage Weil 
779355da1ebSSage Weil 		ceph_encode_32(&dest, ci->i_xattrs.count);
780355da1ebSSage Weil 		while (p) {
781355da1ebSSage Weil 			xattr = rb_entry(p, struct ceph_inode_xattr, node);
782355da1ebSSage Weil 
783355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->name_len);
784355da1ebSSage Weil 			memcpy(dest, xattr->name, xattr->name_len);
785355da1ebSSage Weil 			dest += xattr->name_len;
786355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->val_len);
787355da1ebSSage Weil 			memcpy(dest, xattr->val, xattr->val_len);
788355da1ebSSage Weil 			dest += xattr->val_len;
789355da1ebSSage Weil 
790355da1ebSSage Weil 			p = rb_next(p);
791355da1ebSSage Weil 		}
792355da1ebSSage Weil 
793355da1ebSSage Weil 		/* adjust buffer len; it may be larger than we need */
794355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob->vec.iov_len =
795355da1ebSSage Weil 			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
796355da1ebSSage Weil 
797b6c1d5b8SSage Weil 		if (ci->i_xattrs.blob)
79812fe3ddaSLuis Henriques 			old_blob = ci->i_xattrs.blob;
799355da1ebSSage Weil 		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
800355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = NULL;
801355da1ebSSage Weil 		ci->i_xattrs.dirty = false;
8024a625be4SSage Weil 		ci->i_xattrs.version++;
803355da1ebSSage Weil 	}
80412fe3ddaSLuis Henriques 
80512fe3ddaSLuis Henriques 	return old_blob;
806355da1ebSSage Weil }
807355da1ebSSage Weil 
808315f2408SYan, Zheng static inline int __get_request_mask(struct inode *in) {
809315f2408SYan, Zheng 	struct ceph_mds_request *req = current->journal_info;
810315f2408SYan, Zheng 	int mask = 0;
811315f2408SYan, Zheng 	if (req && req->r_target_inode == in) {
812315f2408SYan, Zheng 		if (req->r_op == CEPH_MDS_OP_LOOKUP ||
813315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_LOOKUPINO ||
814315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
815315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_GETATTR) {
816315f2408SYan, Zheng 			mask = le32_to_cpu(req->r_args.getattr.mask);
817315f2408SYan, Zheng 		} else if (req->r_op == CEPH_MDS_OP_OPEN ||
818315f2408SYan, Zheng 			   req->r_op == CEPH_MDS_OP_CREATE) {
819315f2408SYan, Zheng 			mask = le32_to_cpu(req->r_args.open.mask);
820315f2408SYan, Zheng 		}
821315f2408SYan, Zheng 	}
822315f2408SYan, Zheng 	return mask;
823315f2408SYan, Zheng }
824315f2408SYan, Zheng 
8257221fe4cSGuangliang Zhao ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
826355da1ebSSage Weil 		      size_t size)
827355da1ebSSage Weil {
828355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
829355da1ebSSage Weil 	struct ceph_inode_xattr *xattr;
830881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = NULL;
831315f2408SYan, Zheng 	int req_mask;
832f1d1b51dSJeff Layton 	ssize_t err;
833355da1ebSSage Weil 
8340bee82fbSSage Weil 	/* let's see if a virtual xattr was requested */
8350bee82fbSSage Weil 	vxattr = ceph_match_vxattr(inode, name);
83629dccfa5SYan, Zheng 	if (vxattr) {
83749a9f4f6SYan, Zheng 		int mask = 0;
83849a9f4f6SYan, Zheng 		if (vxattr->flags & VXATTR_FLAG_RSTAT)
83949a9f4f6SYan, Zheng 			mask |= CEPH_STAT_RSTAT;
84049a9f4f6SYan, Zheng 		err = ceph_do_getattr(inode, mask, true);
8411684dd03SYan, Zheng 		if (err)
8421684dd03SYan, Zheng 			return err;
84329dccfa5SYan, Zheng 		err = -ENODATA;
8443b421018SJeff Layton 		if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
8450bee82fbSSage Weil 			err = vxattr->getxattr_cb(ci, value, size);
8463b421018SJeff Layton 			if (size && size < err)
8473b421018SJeff Layton 				err = -ERANGE;
8483b421018SJeff Layton 		}
849a1dc1937Smajianpeng 		return err;
8500bee82fbSSage Weil 	}
8510bee82fbSSage Weil 
852315f2408SYan, Zheng 	req_mask = __get_request_mask(inode);
853315f2408SYan, Zheng 
854a1dc1937Smajianpeng 	spin_lock(&ci->i_ceph_lock);
855d36e0b62SJeff Layton 	dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name,
856a1dc1937Smajianpeng 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
857a1dc1937Smajianpeng 
858508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 ||
859315f2408SYan, Zheng 	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
8601af16d54SXiubo Li 	      __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
861be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
862315f2408SYan, Zheng 
863315f2408SYan, Zheng 		/* security module gets xattr while filling trace */
864d37b1d99SMarkus Elfring 		if (current->journal_info) {
865315f2408SYan, Zheng 			pr_warn_ratelimited("sync getxattr %p "
866315f2408SYan, Zheng 					    "during filling trace\n", inode);
867315f2408SYan, Zheng 			return -EBUSY;
868315f2408SYan, Zheng 		}
869315f2408SYan, Zheng 
870355da1ebSSage Weil 		/* get xattrs from mds (if we don't already have them) */
871508b32d8SYan, Zheng 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
872355da1ebSSage Weil 		if (err)
873355da1ebSSage Weil 			return err;
874be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
875508b32d8SYan, Zheng 	}
876355da1ebSSage Weil 
877355da1ebSSage Weil 	err = __build_xattrs(inode);
878355da1ebSSage Weil 	if (err < 0)
879355da1ebSSage Weil 		goto out;
880355da1ebSSage Weil 
881355da1ebSSage Weil 	err = -ENODATA;  /* == ENOATTR */
882355da1ebSSage Weil 	xattr = __get_xattr(ci, name);
8830bee82fbSSage Weil 	if (!xattr)
884355da1ebSSage Weil 		goto out;
885355da1ebSSage Weil 
886355da1ebSSage Weil 	err = -ERANGE;
887355da1ebSSage Weil 	if (size && size < xattr->val_len)
888355da1ebSSage Weil 		goto out;
889355da1ebSSage Weil 
890355da1ebSSage Weil 	err = xattr->val_len;
891355da1ebSSage Weil 	if (size == 0)
892355da1ebSSage Weil 		goto out;
893355da1ebSSage Weil 
894355da1ebSSage Weil 	memcpy(value, xattr->val, xattr->val_len);
895355da1ebSSage Weil 
896d37b1d99SMarkus Elfring 	if (current->journal_info &&
897026105ebSJeff Layton 	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
898026105ebSJeff Layton 	    security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
899315f2408SYan, Zheng 		ci->i_ceph_flags |= CEPH_I_SEC_INITED;
900355da1ebSSage Weil out:
901be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
902355da1ebSSage Weil 	return err;
903355da1ebSSage Weil }
904355da1ebSSage Weil 
905355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
906355da1ebSSage Weil {
9072b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
908355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
9092b2abcacSDavid Disseldorp 	bool len_only = (size == 0);
910355da1ebSSage Weil 	u32 namelen;
911355da1ebSSage Weil 	int err;
912355da1ebSSage Weil 
913be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
914355da1ebSSage Weil 	dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
915355da1ebSSage Weil 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
916355da1ebSSage Weil 
917508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 ||
9181af16d54SXiubo Li 	    !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
919be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
920508b32d8SYan, Zheng 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
921355da1ebSSage Weil 		if (err)
922355da1ebSSage Weil 			return err;
923be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
924508b32d8SYan, Zheng 	}
925355da1ebSSage Weil 
926355da1ebSSage Weil 	err = __build_xattrs(inode);
927355da1ebSSage Weil 	if (err < 0)
928355da1ebSSage Weil 		goto out;
9293ce6cd12SAlex Elder 
9302b2abcacSDavid Disseldorp 	/* add 1 byte for each xattr due to the null termination */
931b65917ddSSage Weil 	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
9322b2abcacSDavid Disseldorp 	if (!len_only) {
9332b2abcacSDavid Disseldorp 		if (namelen > size) {
934355da1ebSSage Weil 			err = -ERANGE;
935355da1ebSSage Weil 			goto out;
9362b2abcacSDavid Disseldorp 		}
937355da1ebSSage Weil 		names = __copy_xattr_names(ci, names);
9382b2abcacSDavid Disseldorp 		size -= namelen;
9392b2abcacSDavid Disseldorp 	}
9402b2abcacSDavid Disseldorp 	err = namelen;
941355da1ebSSage Weil out:
942be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
943355da1ebSSage Weil 	return err;
944355da1ebSSage Weil }
945355da1ebSSage Weil 
946a26feccaSAndreas Gruenbacher static int ceph_sync_setxattr(struct inode *inode, const char *name,
947355da1ebSSage Weil 			      const char *value, size_t size, int flags)
948355da1ebSSage Weil {
949a26feccaSAndreas Gruenbacher 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
950355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
951355da1ebSSage Weil 	struct ceph_mds_request *req;
9523d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
95325e6bae3SYan, Zheng 	struct ceph_pagelist *pagelist = NULL;
95404303d8aSYan, Zheng 	int op = CEPH_MDS_OP_SETXATTR;
955355da1ebSSage Weil 	int err;
956355da1ebSSage Weil 
9570aeff37aSYan, Zheng 	if (size > 0) {
95825e6bae3SYan, Zheng 		/* copy value into pagelist */
95933165d47SIlya Dryomov 		pagelist = ceph_pagelist_alloc(GFP_NOFS);
96025e6bae3SYan, Zheng 		if (!pagelist)
961355da1ebSSage Weil 			return -ENOMEM;
96225e6bae3SYan, Zheng 
96325e6bae3SYan, Zheng 		err = ceph_pagelist_append(pagelist, value, size);
96425e6bae3SYan, Zheng 		if (err)
965355da1ebSSage Weil 			goto out;
9660aeff37aSYan, Zheng 	} else if (!value) {
96704303d8aSYan, Zheng 		if (flags & CEPH_XATTR_REPLACE)
96804303d8aSYan, Zheng 			op = CEPH_MDS_OP_RMXATTR;
96904303d8aSYan, Zheng 		else
97025e6bae3SYan, Zheng 			flags |= CEPH_XATTR_REMOVE;
971355da1ebSSage Weil 	}
972355da1ebSSage Weil 
973355da1ebSSage Weil 	dout("setxattr value=%.*s\n", (int)size, value);
974355da1ebSSage Weil 
975355da1ebSSage Weil 	/* do request */
97604303d8aSYan, Zheng 	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
97760d87733SJulia Lawall 	if (IS_ERR(req)) {
97860d87733SJulia Lawall 		err = PTR_ERR(req);
97960d87733SJulia Lawall 		goto out;
98060d87733SJulia Lawall 	}
981a149bb9aSSanidhya Kashyap 
982355da1ebSSage Weil 	req->r_path2 = kstrdup(name, GFP_NOFS);
983a149bb9aSSanidhya Kashyap 	if (!req->r_path2) {
984a149bb9aSSanidhya Kashyap 		ceph_mdsc_put_request(req);
985a149bb9aSSanidhya Kashyap 		err = -ENOMEM;
986a149bb9aSSanidhya Kashyap 		goto out;
987a149bb9aSSanidhya Kashyap 	}
988355da1ebSSage Weil 
98904303d8aSYan, Zheng 	if (op == CEPH_MDS_OP_SETXATTR) {
99004303d8aSYan, Zheng 		req->r_args.setxattr.flags = cpu_to_le32(flags);
99125e6bae3SYan, Zheng 		req->r_pagelist = pagelist;
99225e6bae3SYan, Zheng 		pagelist = NULL;
99304303d8aSYan, Zheng 	}
994355da1ebSSage Weil 
995a149bb9aSSanidhya Kashyap 	req->r_inode = inode;
996a149bb9aSSanidhya Kashyap 	ihold(inode);
997a149bb9aSSanidhya Kashyap 	req->r_num_caps = 1;
998a149bb9aSSanidhya Kashyap 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
999a149bb9aSSanidhya Kashyap 
1000355da1ebSSage Weil 	dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
1001752c8bdcSSage Weil 	err = ceph_mdsc_do_request(mdsc, NULL, req);
1002355da1ebSSage Weil 	ceph_mdsc_put_request(req);
1003355da1ebSSage Weil 	dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1004355da1ebSSage Weil 
1005355da1ebSSage Weil out:
100625e6bae3SYan, Zheng 	if (pagelist)
100725e6bae3SYan, Zheng 		ceph_pagelist_release(pagelist);
1008355da1ebSSage Weil 	return err;
1009355da1ebSSage Weil }
1010355da1ebSSage Weil 
1011a26feccaSAndreas Gruenbacher int __ceph_setxattr(struct inode *inode, const char *name,
1012355da1ebSSage Weil 			const void *value, size_t size, int flags)
1013355da1ebSSage Weil {
1014881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr;
1015355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1016a26feccaSAndreas Gruenbacher 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1017f66fd9f0SYan, Zheng 	struct ceph_cap_flush *prealloc_cf = NULL;
101886968ef2SLuis Henriques 	struct ceph_buffer *old_blob = NULL;
101918fa8b3fSAlex Elder 	int issued;
1020355da1ebSSage Weil 	int err;
1021fbc0b970SYan, Zheng 	int dirty = 0;
1022355da1ebSSage Weil 	int name_len = strlen(name);
1023355da1ebSSage Weil 	int val_len = size;
1024355da1ebSSage Weil 	char *newname = NULL;
1025355da1ebSSage Weil 	char *newval = NULL;
1026355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
1027355da1ebSSage Weil 	int required_blob_size;
1028f1919826SYan, Zheng 	bool check_realm = false;
1029604d1b02SYan, Zheng 	bool lock_snap_rwsem = false;
1030355da1ebSSage Weil 
10312cdeb1e4SAndreas Gruenbacher 	if (ceph_snap(inode) != CEPH_NOSNAP)
10322cdeb1e4SAndreas Gruenbacher 		return -EROFS;
1033355da1ebSSage Weil 
103406476a69SAlex Elder 	vxattr = ceph_match_vxattr(inode, name);
1035f1919826SYan, Zheng 	if (vxattr) {
10364e9906e7SYan, Zheng 		if (vxattr->flags & VXATTR_FLAG_READONLY)
1037355da1ebSSage Weil 			return -EOPNOTSUPP;
1038f1919826SYan, Zheng 		if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1039f1919826SYan, Zheng 			check_realm = true;
1040f1919826SYan, Zheng 	}
1041355da1ebSSage Weil 
10423adf654dSSage Weil 	/* pass any unhandled ceph.* xattrs through to the MDS */
10433adf654dSSage Weil 	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
10443adf654dSSage Weil 		goto do_sync_unlocked;
10453adf654dSSage Weil 
1046355da1ebSSage Weil 	/* preallocate memory for xattr name, value, index node */
1047355da1ebSSage Weil 	err = -ENOMEM;
104861413c2fSJulia Lawall 	newname = kmemdup(name, name_len + 1, GFP_NOFS);
1049355da1ebSSage Weil 	if (!newname)
1050355da1ebSSage Weil 		goto out;
1051355da1ebSSage Weil 
1052355da1ebSSage Weil 	if (val_len) {
1053b829c195SAlex Elder 		newval = kmemdup(value, val_len, GFP_NOFS);
1054355da1ebSSage Weil 		if (!newval)
1055355da1ebSSage Weil 			goto out;
1056355da1ebSSage Weil 	}
1057355da1ebSSage Weil 
1058355da1ebSSage Weil 	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1059355da1ebSSage Weil 	if (!xattr)
1060355da1ebSSage Weil 		goto out;
1061355da1ebSSage Weil 
1062f66fd9f0SYan, Zheng 	prealloc_cf = ceph_alloc_cap_flush();
1063f66fd9f0SYan, Zheng 	if (!prealloc_cf)
1064f66fd9f0SYan, Zheng 		goto out;
1065f66fd9f0SYan, Zheng 
1066be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1067355da1ebSSage Weil retry:
1068355da1ebSSage Weil 	issued = __ceph_caps_issued(ci, NULL);
1069508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
1070355da1ebSSage Weil 		goto do_sync;
1071604d1b02SYan, Zheng 
1072604d1b02SYan, Zheng 	if (!lock_snap_rwsem && !ci->i_head_snapc) {
1073604d1b02SYan, Zheng 		lock_snap_rwsem = true;
1074604d1b02SYan, Zheng 		if (!down_read_trylock(&mdsc->snap_rwsem)) {
1075604d1b02SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
1076604d1b02SYan, Zheng 			down_read(&mdsc->snap_rwsem);
1077604d1b02SYan, Zheng 			spin_lock(&ci->i_ceph_lock);
1078604d1b02SYan, Zheng 			goto retry;
1079604d1b02SYan, Zheng 		}
1080604d1b02SYan, Zheng 	}
1081604d1b02SYan, Zheng 
1082d36e0b62SJeff Layton 	dout("setxattr %p name '%s' issued %s\n", inode, name,
1083d36e0b62SJeff Layton 	     ceph_cap_string(issued));
1084355da1ebSSage Weil 	__build_xattrs(inode);
1085355da1ebSSage Weil 
1086355da1ebSSage Weil 	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1087355da1ebSSage Weil 
1088355da1ebSSage Weil 	if (!ci->i_xattrs.prealloc_blob ||
1089355da1ebSSage Weil 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
109018fa8b3fSAlex Elder 		struct ceph_buffer *blob;
1091355da1ebSSage Weil 
1092be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
109386968ef2SLuis Henriques 		ceph_buffer_put(old_blob); /* Shouldn't be required */
109486968ef2SLuis Henriques 		dout(" pre-allocating new blob size=%d\n", required_blob_size);
1095b6c1d5b8SSage Weil 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1096355da1ebSSage Weil 		if (!blob)
1097604d1b02SYan, Zheng 			goto do_sync_unlocked;
1098be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
109986968ef2SLuis Henriques 		/* prealloc_blob can't be released while holding i_ceph_lock */
1100b6c1d5b8SSage Weil 		if (ci->i_xattrs.prealloc_blob)
110186968ef2SLuis Henriques 			old_blob = ci->i_xattrs.prealloc_blob;
1102355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = blob;
1103355da1ebSSage Weil 		goto retry;
1104355da1ebSSage Weil 	}
1105355da1ebSSage Weil 
1106bcdfeb2eSYan, Zheng 	err = __set_xattr(ci, newname, name_len, newval, val_len,
1107bcdfeb2eSYan, Zheng 			  flags, value ? 1 : -1, &xattr);
110818fa8b3fSAlex Elder 
1109fbc0b970SYan, Zheng 	if (!err) {
1110f66fd9f0SYan, Zheng 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1111f66fd9f0SYan, Zheng 					       &prealloc_cf);
1112355da1ebSSage Weil 		ci->i_xattrs.dirty = true;
1113c2050a45SDeepa Dinamani 		inode->i_ctime = current_time(inode);
1114fbc0b970SYan, Zheng 	}
111518fa8b3fSAlex Elder 
1116be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
111786968ef2SLuis Henriques 	ceph_buffer_put(old_blob);
1118604d1b02SYan, Zheng 	if (lock_snap_rwsem)
1119604d1b02SYan, Zheng 		up_read(&mdsc->snap_rwsem);
1120fca65b4aSSage Weil 	if (dirty)
1121fca65b4aSSage Weil 		__mark_inode_dirty(inode, dirty);
1122f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
1123355da1ebSSage Weil 	return err;
1124355da1ebSSage Weil 
1125355da1ebSSage Weil do_sync:
1126be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
11273adf654dSSage Weil do_sync_unlocked:
1128604d1b02SYan, Zheng 	if (lock_snap_rwsem)
1129604d1b02SYan, Zheng 		up_read(&mdsc->snap_rwsem);
1130315f2408SYan, Zheng 
1131315f2408SYan, Zheng 	/* security module set xattr while filling trace */
1132d37b1d99SMarkus Elfring 	if (current->journal_info) {
1133315f2408SYan, Zheng 		pr_warn_ratelimited("sync setxattr %p "
1134315f2408SYan, Zheng 				    "during filling trace\n", inode);
1135315f2408SYan, Zheng 		err = -EBUSY;
1136315f2408SYan, Zheng 	} else {
1137a26feccaSAndreas Gruenbacher 		err = ceph_sync_setxattr(inode, name, value, size, flags);
1138f1919826SYan, Zheng 		if (err >= 0 && check_realm) {
1139f1919826SYan, Zheng 			/* check if snaprealm was created for quota inode */
1140f1919826SYan, Zheng 			spin_lock(&ci->i_ceph_lock);
1141f1919826SYan, Zheng 			if ((ci->i_max_files || ci->i_max_bytes) &&
1142f1919826SYan, Zheng 			    !(ci->i_snap_realm &&
1143f1919826SYan, Zheng 			      ci->i_snap_realm->ino == ci->i_vino.ino))
1144f1919826SYan, Zheng 				err = -EOPNOTSUPP;
1145f1919826SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
1146f1919826SYan, Zheng 		}
1147315f2408SYan, Zheng 	}
1148355da1ebSSage Weil out:
1149f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
1150355da1ebSSage Weil 	kfree(newname);
1151355da1ebSSage Weil 	kfree(newval);
1152355da1ebSSage Weil 	kfree(xattr);
1153355da1ebSSage Weil 	return err;
1154355da1ebSSage Weil }
1155355da1ebSSage Weil 
11562cdeb1e4SAndreas Gruenbacher static int ceph_get_xattr_handler(const struct xattr_handler *handler,
11572cdeb1e4SAndreas Gruenbacher 				  struct dentry *dentry, struct inode *inode,
11582cdeb1e4SAndreas Gruenbacher 				  const char *name, void *value, size_t size)
11597221fe4cSGuangliang Zhao {
11602cdeb1e4SAndreas Gruenbacher 	if (!ceph_is_valid_xattr(name))
11612cdeb1e4SAndreas Gruenbacher 		return -EOPNOTSUPP;
11622cdeb1e4SAndreas Gruenbacher 	return __ceph_getxattr(inode, name, value, size);
11637221fe4cSGuangliang Zhao }
1164315f2408SYan, Zheng 
11652cdeb1e4SAndreas Gruenbacher static int ceph_set_xattr_handler(const struct xattr_handler *handler,
116659301226SAl Viro 				  struct dentry *unused, struct inode *inode,
116759301226SAl Viro 				  const char *name, const void *value,
116859301226SAl Viro 				  size_t size, int flags)
11692cdeb1e4SAndreas Gruenbacher {
11702cdeb1e4SAndreas Gruenbacher 	if (!ceph_is_valid_xattr(name))
11712cdeb1e4SAndreas Gruenbacher 		return -EOPNOTSUPP;
117259301226SAl Viro 	return __ceph_setxattr(inode, name, value, size, flags);
11732cdeb1e4SAndreas Gruenbacher }
11742cdeb1e4SAndreas Gruenbacher 
11755130cceaSWei Yongjun static const struct xattr_handler ceph_other_xattr_handler = {
11762cdeb1e4SAndreas Gruenbacher 	.prefix = "",  /* match any name => handlers called with full name */
11772cdeb1e4SAndreas Gruenbacher 	.get = ceph_get_xattr_handler,
11782cdeb1e4SAndreas Gruenbacher 	.set = ceph_set_xattr_handler,
11792cdeb1e4SAndreas Gruenbacher };
11802cdeb1e4SAndreas Gruenbacher 
1181315f2408SYan, Zheng #ifdef CONFIG_SECURITY
1182315f2408SYan, Zheng bool ceph_security_xattr_wanted(struct inode *in)
1183315f2408SYan, Zheng {
1184315f2408SYan, Zheng 	return in->i_security != NULL;
1185315f2408SYan, Zheng }
1186315f2408SYan, Zheng 
1187315f2408SYan, Zheng bool ceph_security_xattr_deadlock(struct inode *in)
1188315f2408SYan, Zheng {
1189315f2408SYan, Zheng 	struct ceph_inode_info *ci;
1190315f2408SYan, Zheng 	bool ret;
1191d37b1d99SMarkus Elfring 	if (!in->i_security)
1192315f2408SYan, Zheng 		return false;
1193315f2408SYan, Zheng 	ci = ceph_inode(in);
1194315f2408SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
1195315f2408SYan, Zheng 	ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1196315f2408SYan, Zheng 	      !(ci->i_xattrs.version > 0 &&
1197315f2408SYan, Zheng 		__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1198315f2408SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
1199315f2408SYan, Zheng 	return ret;
1200315f2408SYan, Zheng }
1201ac6713ccSYan, Zheng 
1202ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1203ac6713ccSYan, Zheng int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1204ac6713ccSYan, Zheng 			   struct ceph_acl_sec_ctx *as_ctx)
1205ac6713ccSYan, Zheng {
1206ac6713ccSYan, Zheng 	struct ceph_pagelist *pagelist = as_ctx->pagelist;
1207ac6713ccSYan, Zheng 	const char *name;
1208ac6713ccSYan, Zheng 	size_t name_len;
1209ac6713ccSYan, Zheng 	int err;
1210ac6713ccSYan, Zheng 
1211ac6713ccSYan, Zheng 	err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1212ac6713ccSYan, Zheng 					    &as_ctx->sec_ctx,
1213ac6713ccSYan, Zheng 					    &as_ctx->sec_ctxlen);
1214ac6713ccSYan, Zheng 	if (err < 0) {
1215ac6713ccSYan, Zheng 		WARN_ON_ONCE(err != -EOPNOTSUPP);
1216ac6713ccSYan, Zheng 		err = 0; /* do nothing */
1217ac6713ccSYan, Zheng 		goto out;
1218ac6713ccSYan, Zheng 	}
1219ac6713ccSYan, Zheng 
1220ac6713ccSYan, Zheng 	err = -ENOMEM;
1221ac6713ccSYan, Zheng 	if (!pagelist) {
1222ac6713ccSYan, Zheng 		pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1223ac6713ccSYan, Zheng 		if (!pagelist)
1224ac6713ccSYan, Zheng 			goto out;
1225ac6713ccSYan, Zheng 		err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1226ac6713ccSYan, Zheng 		if (err)
1227ac6713ccSYan, Zheng 			goto out;
1228ac6713ccSYan, Zheng 		ceph_pagelist_encode_32(pagelist, 1);
1229ac6713ccSYan, Zheng 	}
1230ac6713ccSYan, Zheng 
1231ac6713ccSYan, Zheng 	/*
1232ac6713ccSYan, Zheng 	 * FIXME: Make security_dentry_init_security() generic. Currently
1233ac6713ccSYan, Zheng 	 * It only supports single security module and only selinux has
1234ac6713ccSYan, Zheng 	 * dentry_init_security hook.
1235ac6713ccSYan, Zheng 	 */
1236ac6713ccSYan, Zheng 	name = XATTR_NAME_SELINUX;
1237ac6713ccSYan, Zheng 	name_len = strlen(name);
1238ac6713ccSYan, Zheng 	err = ceph_pagelist_reserve(pagelist,
1239ac6713ccSYan, Zheng 				    4 * 2 + name_len + as_ctx->sec_ctxlen);
1240ac6713ccSYan, Zheng 	if (err)
1241ac6713ccSYan, Zheng 		goto out;
1242ac6713ccSYan, Zheng 
1243ac6713ccSYan, Zheng 	if (as_ctx->pagelist) {
1244ac6713ccSYan, Zheng 		/* update count of KV pairs */
1245ac6713ccSYan, Zheng 		BUG_ON(pagelist->length <= sizeof(__le32));
1246ac6713ccSYan, Zheng 		if (list_is_singular(&pagelist->head)) {
1247ac6713ccSYan, Zheng 			le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1248ac6713ccSYan, Zheng 		} else {
1249ac6713ccSYan, Zheng 			struct page *page = list_first_entry(&pagelist->head,
1250ac6713ccSYan, Zheng 							     struct page, lru);
1251ac6713ccSYan, Zheng 			void *addr = kmap_atomic(page);
1252ac6713ccSYan, Zheng 			le32_add_cpu((__le32*)addr, 1);
1253ac6713ccSYan, Zheng 			kunmap_atomic(addr);
1254ac6713ccSYan, Zheng 		}
1255ac6713ccSYan, Zheng 	} else {
1256ac6713ccSYan, Zheng 		as_ctx->pagelist = pagelist;
1257ac6713ccSYan, Zheng 	}
1258ac6713ccSYan, Zheng 
1259ac6713ccSYan, Zheng 	ceph_pagelist_encode_32(pagelist, name_len);
1260ac6713ccSYan, Zheng 	ceph_pagelist_append(pagelist, name, name_len);
1261ac6713ccSYan, Zheng 
1262ac6713ccSYan, Zheng 	ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1263ac6713ccSYan, Zheng 	ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1264ac6713ccSYan, Zheng 
1265ac6713ccSYan, Zheng 	err = 0;
1266ac6713ccSYan, Zheng out:
1267ac6713ccSYan, Zheng 	if (pagelist && !as_ctx->pagelist)
1268ac6713ccSYan, Zheng 		ceph_pagelist_release(pagelist);
1269ac6713ccSYan, Zheng 	return err;
1270ac6713ccSYan, Zheng }
1271668959a5SJeff Layton #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1272668959a5SJeff Layton #endif /* CONFIG_SECURITY */
12735c31e92dSYan, Zheng 
12745c31e92dSYan, Zheng void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
12755c31e92dSYan, Zheng {
12765c31e92dSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL
12775c31e92dSYan, Zheng 	posix_acl_release(as_ctx->acl);
12785c31e92dSYan, Zheng 	posix_acl_release(as_ctx->default_acl);
12795c31e92dSYan, Zheng #endif
1280ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1281ac6713ccSYan, Zheng 	security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1282ac6713ccSYan, Zheng #endif
12835c31e92dSYan, Zheng 	if (as_ctx->pagelist)
12845c31e92dSYan, Zheng 		ceph_pagelist_release(as_ctx->pagelist);
12855c31e92dSYan, Zheng }
1286ac6713ccSYan, Zheng 
1287ac6713ccSYan, Zheng /*
1288ac6713ccSYan, Zheng  * List of handlers for synthetic system.* attributes. Other
1289ac6713ccSYan, Zheng  * attributes are handled directly.
1290ac6713ccSYan, Zheng  */
1291ac6713ccSYan, Zheng const struct xattr_handler *ceph_xattr_handlers[] = {
1292ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL
1293ac6713ccSYan, Zheng 	&posix_acl_access_xattr_handler,
1294ac6713ccSYan, Zheng 	&posix_acl_default_xattr_handler,
1295ac6713ccSYan, Zheng #endif
1296ac6713ccSYan, Zheng 	&ceph_other_xattr_handler,
1297ac6713ccSYan, Zheng 	NULL,
1298ac6713ccSYan, Zheng };
1299