xref: /linux/fs/ceph/xattr.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
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 
ceph_is_valid_xattr(const char * name)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)
4581048c00SJeff Layton #define VXATTR_FLAG_DIRSTAT		(1<<3)
464e9906e7SYan, Zheng 
4732ab0bd7SSage Weil /* layouts */
4832ab0bd7SSage Weil 
ceph_vxattrcb_layout_exists(struct ceph_inode_info * ci)4932ab0bd7SSage Weil static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
5032ab0bd7SSage Weil {
51779fe0fbSYan, Zheng 	struct ceph_file_layout *fl = &ci->i_layout;
52779fe0fbSYan, Zheng 	return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
53779fe0fbSYan, Zheng 		fl->object_size > 0 || fl->pool_id >= 0 ||
54779fe0fbSYan, Zheng 		rcu_dereference_raw(fl->pool_ns) != NULL);
5532ab0bd7SSage Weil }
5632ab0bd7SSage Weil 
ceph_vxattrcb_layout(struct ceph_inode_info * ci,char * val,size_t size)57f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
5832ab0bd7SSage Weil 				    size_t size)
5932ab0bd7SSage Weil {
605995d90dSXiubo Li 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb);
61*38d46409SXiubo Li 	struct ceph_client *cl = fsc->client;
6232ab0bd7SSage Weil 	struct ceph_osd_client *osdc = &fsc->client->osdc;
63779fe0fbSYan, Zheng 	struct ceph_string *pool_ns;
647627151eSYan, Zheng 	s64 pool = ci->i_layout.pool_id;
6532ab0bd7SSage Weil 	const char *pool_name;
66779fe0fbSYan, Zheng 	const char *ns_field = " pool_namespace=";
671e5c6649SYan, Zheng 	char buf[128];
68779fe0fbSYan, Zheng 	size_t len, total_len = 0;
693b421018SJeff Layton 	ssize_t ret;
70779fe0fbSYan, Zheng 
71779fe0fbSYan, Zheng 	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
7232ab0bd7SSage Weil 
73*38d46409SXiubo Li 	doutc(cl, "%p\n", &ci->netfs.inode);
745aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
7532ab0bd7SSage Weil 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
761e5c6649SYan, Zheng 	if (pool_name) {
77779fe0fbSYan, Zheng 		len = snprintf(buf, sizeof(buf),
787627151eSYan, Zheng 		"stripe_unit=%u stripe_count=%u object_size=%u pool=",
797627151eSYan, Zheng 		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
807627151eSYan, Zheng 	        ci->i_layout.object_size);
81779fe0fbSYan, Zheng 		total_len = len + strlen(pool_name);
821e5c6649SYan, Zheng 	} else {
83779fe0fbSYan, Zheng 		len = snprintf(buf, sizeof(buf),
847627151eSYan, Zheng 		"stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
857627151eSYan, Zheng 		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
86f1d1b51dSJeff Layton 		ci->i_layout.object_size, pool);
87779fe0fbSYan, Zheng 		total_len = len;
88779fe0fbSYan, Zheng 	}
89779fe0fbSYan, Zheng 
90779fe0fbSYan, Zheng 	if (pool_ns)
91779fe0fbSYan, Zheng 		total_len += strlen(ns_field) + pool_ns->len;
92779fe0fbSYan, Zheng 
93779fe0fbSYan, Zheng 	ret = total_len;
943b421018SJeff Layton 	if (size >= total_len) {
95779fe0fbSYan, Zheng 		memcpy(val, buf, len);
96779fe0fbSYan, Zheng 		ret = len;
97779fe0fbSYan, Zheng 		if (pool_name) {
98779fe0fbSYan, Zheng 			len = strlen(pool_name);
99779fe0fbSYan, Zheng 			memcpy(val + ret, pool_name, len);
100779fe0fbSYan, Zheng 			ret += len;
101779fe0fbSYan, Zheng 		}
102779fe0fbSYan, Zheng 		if (pool_ns) {
103779fe0fbSYan, Zheng 			len = strlen(ns_field);
104779fe0fbSYan, Zheng 			memcpy(val + ret, ns_field, len);
105779fe0fbSYan, Zheng 			ret += len;
106779fe0fbSYan, Zheng 			memcpy(val + ret, pool_ns->str, pool_ns->len);
107779fe0fbSYan, Zheng 			ret += pool_ns->len;
1081e5c6649SYan, Zheng 		}
1091e5c6649SYan, Zheng 	}
1105aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
111779fe0fbSYan, Zheng 	ceph_put_string(pool_ns);
11232ab0bd7SSage Weil 	return ret;
11332ab0bd7SSage Weil }
11432ab0bd7SSage Weil 
11526350535SJeff Layton /*
11626350535SJeff Layton  * The convention with strings in xattrs is that they should not be NULL
11726350535SJeff Layton  * terminated, since we're returning the length with them. snprintf always
11826350535SJeff Layton  * NULL terminates however, so call it on a temporary buffer and then memcpy
11926350535SJeff Layton  * the result into place.
12026350535SJeff Layton  */
121f6fbdcd9SIlya Dryomov static __printf(3, 4)
ceph_fmt_xattr(char * val,size_t size,const char * fmt,...)122f6fbdcd9SIlya Dryomov int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
12326350535SJeff Layton {
12426350535SJeff Layton 	int ret;
12526350535SJeff Layton 	va_list args;
12626350535SJeff Layton 	char buf[96]; /* NB: reevaluate size if new vxattrs are added */
12726350535SJeff Layton 
12826350535SJeff Layton 	va_start(args, fmt);
12926350535SJeff Layton 	ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
13026350535SJeff Layton 	va_end(args);
13126350535SJeff Layton 
13226350535SJeff Layton 	/* Sanity check */
13326350535SJeff Layton 	if (size && ret + 1 > sizeof(buf)) {
13426350535SJeff Layton 		WARN_ONCE(true, "Returned length too big (%d)", ret);
13526350535SJeff Layton 		return -E2BIG;
13626350535SJeff Layton 	}
13726350535SJeff Layton 
13826350535SJeff Layton 	if (ret <= size)
13926350535SJeff Layton 		memcpy(val, buf, ret);
14026350535SJeff Layton 	return ret;
14126350535SJeff Layton }
14226350535SJeff Layton 
ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info * ci,char * val,size_t size)143f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
144695b7119SSage Weil 						char *val, size_t size)
145695b7119SSage Weil {
14626350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
147695b7119SSage Weil }
148695b7119SSage Weil 
ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info * ci,char * val,size_t size)149f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
150695b7119SSage Weil 						 char *val, size_t size)
151695b7119SSage Weil {
15226350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
153695b7119SSage Weil }
154695b7119SSage Weil 
ceph_vxattrcb_layout_object_size(struct ceph_inode_info * ci,char * val,size_t size)155f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
156695b7119SSage Weil 						char *val, size_t size)
157695b7119SSage Weil {
15826350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
159695b7119SSage Weil }
160695b7119SSage Weil 
ceph_vxattrcb_layout_pool(struct ceph_inode_info * ci,char * val,size_t size)161f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
162695b7119SSage Weil 					 char *val, size_t size)
163695b7119SSage Weil {
164f1d1b51dSJeff Layton 	ssize_t ret;
1655995d90dSXiubo Li 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb);
166695b7119SSage Weil 	struct ceph_osd_client *osdc = &fsc->client->osdc;
1677627151eSYan, Zheng 	s64 pool = ci->i_layout.pool_id;
168695b7119SSage Weil 	const char *pool_name;
169695b7119SSage Weil 
1705aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
171695b7119SSage Weil 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
17226350535SJeff Layton 	if (pool_name) {
17326350535SJeff Layton 		ret = strlen(pool_name);
17426350535SJeff Layton 		if (ret <= size)
17526350535SJeff Layton 			memcpy(val, pool_name, ret);
17626350535SJeff Layton 	} else {
17726350535SJeff Layton 		ret = ceph_fmt_xattr(val, size, "%lld", pool);
17826350535SJeff Layton 	}
1795aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
180695b7119SSage Weil 	return ret;
181695b7119SSage Weil }
182695b7119SSage Weil 
ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info * ci,char * val,size_t size)183f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
184779fe0fbSYan, Zheng 						   char *val, size_t size)
185779fe0fbSYan, Zheng {
18626350535SJeff Layton 	ssize_t ret = 0;
187779fe0fbSYan, Zheng 	struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
18826350535SJeff Layton 
189779fe0fbSYan, Zheng 	if (ns) {
19026350535SJeff Layton 		ret = ns->len;
19126350535SJeff Layton 		if (ret <= size)
19226350535SJeff Layton 			memcpy(val, ns->str, ret);
193779fe0fbSYan, Zheng 		ceph_put_string(ns);
194779fe0fbSYan, Zheng 	}
195779fe0fbSYan, Zheng 	return ret;
196779fe0fbSYan, Zheng }
197779fe0fbSYan, Zheng 
198355da1ebSSage Weil /* directories */
199355da1ebSSage Weil 
ceph_vxattrcb_dir_entries(struct ceph_inode_info * ci,char * val,size_t size)200f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
201355da1ebSSage Weil 					 size_t size)
202355da1ebSSage Weil {
20326350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
204355da1ebSSage Weil }
205355da1ebSSage Weil 
ceph_vxattrcb_dir_files(struct ceph_inode_info * ci,char * val,size_t size)206f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
207355da1ebSSage Weil 				       size_t size)
208355da1ebSSage Weil {
20926350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
210355da1ebSSage Weil }
211355da1ebSSage Weil 
ceph_vxattrcb_dir_subdirs(struct ceph_inode_info * ci,char * val,size_t size)212f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
213355da1ebSSage Weil 					 size_t size)
214355da1ebSSage Weil {
21526350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
216355da1ebSSage Weil }
217355da1ebSSage Weil 
ceph_vxattrcb_dir_rentries(struct ceph_inode_info * ci,char * val,size_t size)218f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
219355da1ebSSage Weil 					  size_t size)
220355da1ebSSage Weil {
22126350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld",
22226350535SJeff Layton 				ci->i_rfiles + ci->i_rsubdirs);
223355da1ebSSage Weil }
224355da1ebSSage Weil 
ceph_vxattrcb_dir_rfiles(struct ceph_inode_info * ci,char * val,size_t size)225f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
226355da1ebSSage Weil 					size_t size)
227355da1ebSSage Weil {
22826350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
229355da1ebSSage Weil }
230355da1ebSSage Weil 
ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info * ci,char * val,size_t size)231f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
232355da1ebSSage Weil 					  size_t size)
233355da1ebSSage Weil {
23426350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
235355da1ebSSage Weil }
236355da1ebSSage Weil 
ceph_vxattrcb_dir_rsnaps(struct ceph_inode_info * ci,char * val,size_t size)237e7f72952SYanhu Cao static ssize_t ceph_vxattrcb_dir_rsnaps(struct ceph_inode_info *ci, char *val,
238e7f72952SYanhu Cao 					  size_t size)
239e7f72952SYanhu Cao {
240e7f72952SYanhu Cao 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rsnaps);
241e7f72952SYanhu Cao }
242e7f72952SYanhu Cao 
ceph_vxattrcb_dir_rbytes(struct ceph_inode_info * ci,char * val,size_t size)243f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
244355da1ebSSage Weil 					size_t size)
245355da1ebSSage Weil {
24626350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
247355da1ebSSage Weil }
248355da1ebSSage Weil 
ceph_vxattrcb_dir_rctime(struct ceph_inode_info * ci,char * val,size_t size)249f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
250355da1ebSSage Weil 					size_t size)
251355da1ebSSage Weil {
25226350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
2539bbeab41SArnd Bergmann 				ci->i_rctime.tv_nsec);
254355da1ebSSage Weil }
255355da1ebSSage Weil 
25608796873SYan, Zheng /* dir pin */
ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info * ci)25708796873SYan, Zheng static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
25808796873SYan, Zheng {
25908796873SYan, Zheng 	return ci->i_dir_pin != -ENODATA;
26008796873SYan, Zheng }
261fb18a575SLuis Henriques 
ceph_vxattrcb_dir_pin(struct ceph_inode_info * ci,char * val,size_t size)262f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
26308796873SYan, Zheng 				     size_t size)
26408796873SYan, Zheng {
26526350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
26608796873SYan, Zheng }
26708796873SYan, Zheng 
26808796873SYan, Zheng /* quotas */
ceph_vxattrcb_quota_exists(struct ceph_inode_info * ci)269fb18a575SLuis Henriques static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
270fb18a575SLuis Henriques {
271f1919826SYan, Zheng 	bool ret = false;
272f1919826SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
273f1919826SYan, Zheng 	if ((ci->i_max_files || ci->i_max_bytes) &&
274f1919826SYan, Zheng 	    ci->i_vino.snap == CEPH_NOSNAP &&
275f1919826SYan, Zheng 	    ci->i_snap_realm &&
276f1919826SYan, Zheng 	    ci->i_snap_realm->ino == ci->i_vino.ino)
277f1919826SYan, Zheng 		ret = true;
278f1919826SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
279f1919826SYan, Zheng 	return ret;
280fb18a575SLuis Henriques }
281fb18a575SLuis Henriques 
ceph_vxattrcb_quota(struct ceph_inode_info * ci,char * val,size_t size)282f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
283fb18a575SLuis Henriques 				   size_t size)
284fb18a575SLuis Henriques {
28526350535SJeff Layton 	return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
286fb18a575SLuis Henriques 				ci->i_max_bytes, ci->i_max_files);
287fb18a575SLuis Henriques }
288fb18a575SLuis Henriques 
ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info * ci,char * val,size_t size)289f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
290fb18a575SLuis Henriques 					     char *val, size_t size)
291fb18a575SLuis Henriques {
29226350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
293fb18a575SLuis Henriques }
294fb18a575SLuis Henriques 
ceph_vxattrcb_quota_max_files(struct ceph_inode_info * ci,char * val,size_t size)295f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
296fb18a575SLuis Henriques 					     char *val, size_t size)
297fb18a575SLuis Henriques {
29826350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
299fb18a575SLuis Henriques }
30032ab0bd7SSage Weil 
301100cc610SDavid Disseldorp /* snapshots */
ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info * ci)302100cc610SDavid Disseldorp static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
303100cc610SDavid Disseldorp {
304100cc610SDavid Disseldorp 	return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
305100cc610SDavid Disseldorp }
306100cc610SDavid Disseldorp 
ceph_vxattrcb_snap_btime(struct ceph_inode_info * ci,char * val,size_t size)307f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
308100cc610SDavid Disseldorp 					size_t size)
309100cc610SDavid Disseldorp {
31026350535SJeff Layton 	return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
311100cc610SDavid Disseldorp 				ci->i_snap_btime.tv_nsec);
312100cc610SDavid Disseldorp }
313100cc610SDavid Disseldorp 
ceph_vxattrcb_cluster_fsid(struct ceph_inode_info * ci,char * val,size_t size)3145a9e2f5dSXiubo Li static ssize_t ceph_vxattrcb_cluster_fsid(struct ceph_inode_info *ci,
3155a9e2f5dSXiubo Li 					  char *val, size_t size)
3165a9e2f5dSXiubo Li {
3175995d90dSXiubo Li 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb);
3185a9e2f5dSXiubo Li 
3195a9e2f5dSXiubo Li 	return ceph_fmt_xattr(val, size, "%pU", &fsc->client->fsid);
3205a9e2f5dSXiubo Li }
3215a9e2f5dSXiubo Li 
ceph_vxattrcb_client_id(struct ceph_inode_info * ci,char * val,size_t size)3225a9e2f5dSXiubo Li static ssize_t ceph_vxattrcb_client_id(struct ceph_inode_info *ci,
3235a9e2f5dSXiubo Li 				       char *val, size_t size)
3245a9e2f5dSXiubo Li {
3255995d90dSXiubo Li 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb);
3265a9e2f5dSXiubo Li 
3275a9e2f5dSXiubo Li 	return ceph_fmt_xattr(val, size, "client%lld",
3285a9e2f5dSXiubo Li 			      ceph_client_gid(fsc->client));
3295a9e2f5dSXiubo Li }
3305a9e2f5dSXiubo Li 
ceph_vxattrcb_caps(struct ceph_inode_info * ci,char * val,size_t size)331dd980fc0SLuis Henriques static ssize_t ceph_vxattrcb_caps(struct ceph_inode_info *ci, char *val,
332dd980fc0SLuis Henriques 					size_t size)
333dd980fc0SLuis Henriques {
334dd980fc0SLuis Henriques 	int issued;
335dd980fc0SLuis Henriques 
336dd980fc0SLuis Henriques 	spin_lock(&ci->i_ceph_lock);
337dd980fc0SLuis Henriques 	issued = __ceph_caps_issued(ci, NULL);
338dd980fc0SLuis Henriques 	spin_unlock(&ci->i_ceph_lock);
339dd980fc0SLuis Henriques 
340dd980fc0SLuis Henriques 	return ceph_fmt_xattr(val, size, "%s/0x%x",
341dd980fc0SLuis Henriques 			      ceph_cap_string(issued), issued);
342dd980fc0SLuis Henriques }
343dd980fc0SLuis Henriques 
ceph_vxattrcb_auth_mds(struct ceph_inode_info * ci,char * val,size_t size)34440e309deSJeff Layton static ssize_t ceph_vxattrcb_auth_mds(struct ceph_inode_info *ci,
34540e309deSJeff Layton 				       char *val, size_t size)
34640e309deSJeff Layton {
34740e309deSJeff Layton 	int ret;
34840e309deSJeff Layton 
34940e309deSJeff Layton 	spin_lock(&ci->i_ceph_lock);
35040e309deSJeff Layton 	ret = ceph_fmt_xattr(val, size, "%d",
35140e309deSJeff Layton 			     ci->i_auth_cap ? ci->i_auth_cap->session->s_mds : -1);
35240e309deSJeff Layton 	spin_unlock(&ci->i_ceph_lock);
35340e309deSJeff Layton 	return ret;
35440e309deSJeff Layton }
35540e309deSJeff Layton 
356f061fedaSJeff Layton #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
ceph_vxattrcb_fscrypt_auth_exists(struct ceph_inode_info * ci)357f061fedaSJeff Layton static bool ceph_vxattrcb_fscrypt_auth_exists(struct ceph_inode_info *ci)
358f061fedaSJeff Layton {
359f061fedaSJeff Layton 	return ci->fscrypt_auth_len;
360f061fedaSJeff Layton }
361f061fedaSJeff Layton 
ceph_vxattrcb_fscrypt_auth(struct ceph_inode_info * ci,char * val,size_t size)362f061fedaSJeff Layton static ssize_t ceph_vxattrcb_fscrypt_auth(struct ceph_inode_info *ci,
363f061fedaSJeff Layton 					  char *val, size_t size)
364f061fedaSJeff Layton {
365f061fedaSJeff Layton 	if (size) {
366f061fedaSJeff Layton 		if (size < ci->fscrypt_auth_len)
367f061fedaSJeff Layton 			return -ERANGE;
368f061fedaSJeff Layton 		memcpy(val, ci->fscrypt_auth, ci->fscrypt_auth_len);
369f061fedaSJeff Layton 	}
370f061fedaSJeff Layton 	return ci->fscrypt_auth_len;
371f061fedaSJeff Layton }
372f061fedaSJeff Layton #endif /* CONFIG_FS_ENCRYPTION */
373f061fedaSJeff Layton 
374eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
375695b7119SSage Weil #define CEPH_XATTR_NAME2(_type, _name, _name2)	\
376695b7119SSage Weil 	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
377eb788084SAlex Elder 
37849a9f4f6SYan, Zheng #define XATTR_NAME_CEPH(_type, _name, _flags)				\
379eb788084SAlex Elder 	{								\
380eb788084SAlex Elder 		.name = CEPH_XATTR_NAME(_type, _name),			\
3813ce6cd12SAlex Elder 		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
382aa4066edSAlex Elder 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
383f36e4472SSage Weil 		.exists_cb = NULL,					\
38449a9f4f6SYan, Zheng 		.flags = (VXATTR_FLAG_READONLY | _flags),		\
385eb788084SAlex Elder 	}
38649a9f4f6SYan, Zheng #define XATTR_RSTAT_FIELD(_type, _name)			\
38749a9f4f6SYan, Zheng 	XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
388d7a2dc52SVenky Shankar #define XATTR_RSTAT_FIELD_UPDATABLE(_type, _name)			\
389d7a2dc52SVenky Shankar 	{								\
390d7a2dc52SVenky Shankar 		.name = CEPH_XATTR_NAME(_type, _name),			\
391d7a2dc52SVenky Shankar 		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)),	\
392d7a2dc52SVenky Shankar 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name,	\
393d7a2dc52SVenky Shankar 		.exists_cb = NULL,					\
394d7a2dc52SVenky Shankar 		.flags = VXATTR_FLAG_RSTAT,				\
395d7a2dc52SVenky Shankar 	}
396695b7119SSage Weil #define XATTR_LAYOUT_FIELD(_type, _name, _field)			\
397695b7119SSage Weil 	{								\
398695b7119SSage Weil 		.name = CEPH_XATTR_NAME2(_type, _name, _field),	\
399695b7119SSage Weil 		.name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
400695b7119SSage Weil 		.getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
401695b7119SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,	\
4024e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,			\
403695b7119SSage Weil 	}
404fb18a575SLuis Henriques #define XATTR_QUOTA_FIELD(_type, _name)					\
405fb18a575SLuis Henriques 	{								\
406fb18a575SLuis Henriques 		.name = CEPH_XATTR_NAME(_type, _name),			\
407fb18a575SLuis Henriques 		.name_size = sizeof(CEPH_XATTR_NAME(_type, _name)),	\
408fb18a575SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name,	\
409fb18a575SLuis Henriques 		.exists_cb = ceph_vxattrcb_quota_exists,		\
4104e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,				\
411fb18a575SLuis Henriques 	}
412eb788084SAlex Elder 
413881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = {
4141f08f2b0SSage Weil 	{
4151f08f2b0SSage Weil 		.name = "ceph.dir.layout",
4161f08f2b0SSage Weil 		.name_size = sizeof("ceph.dir.layout"),
4171f08f2b0SSage Weil 		.getxattr_cb = ceph_vxattrcb_layout,
4181f08f2b0SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,
4194e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
4201f08f2b0SSage Weil 	},
421695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
422695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
423695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, object_size),
424695b7119SSage Weil 	XATTR_LAYOUT_FIELD(dir, layout, pool),
425779fe0fbSYan, Zheng 	XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
42681048c00SJeff Layton 	XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT),
42781048c00SJeff Layton 	XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT),
42881048c00SJeff Layton 	XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT),
42949a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rentries),
43049a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rfiles),
43149a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rsubdirs),
432e7f72952SYanhu Cao 	XATTR_RSTAT_FIELD(dir, rsnaps),
43349a9f4f6SYan, Zheng 	XATTR_RSTAT_FIELD(dir, rbytes),
434d7a2dc52SVenky Shankar 	XATTR_RSTAT_FIELD_UPDATABLE(dir, rctime),
435fb18a575SLuis Henriques 	{
43608796873SYan, Zheng 		.name = "ceph.dir.pin",
437e1b81439SDavid Disseldorp 		.name_size = sizeof("ceph.dir.pin"),
43808796873SYan, Zheng 		.getxattr_cb = ceph_vxattrcb_dir_pin,
43908796873SYan, Zheng 		.exists_cb = ceph_vxattrcb_dir_pin_exists,
44008796873SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
44108796873SYan, Zheng 	},
44208796873SYan, Zheng 	{
443fb18a575SLuis Henriques 		.name = "ceph.quota",
444fb18a575SLuis Henriques 		.name_size = sizeof("ceph.quota"),
445fb18a575SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_quota,
446fb18a575SLuis Henriques 		.exists_cb = ceph_vxattrcb_quota_exists,
4474e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
448fb18a575SLuis Henriques 	},
449fb18a575SLuis Henriques 	XATTR_QUOTA_FIELD(quota, max_bytes),
450fb18a575SLuis Henriques 	XATTR_QUOTA_FIELD(quota, max_files),
451100cc610SDavid Disseldorp 	{
452100cc610SDavid Disseldorp 		.name = "ceph.snap.btime",
453100cc610SDavid Disseldorp 		.name_size = sizeof("ceph.snap.btime"),
454100cc610SDavid Disseldorp 		.getxattr_cb = ceph_vxattrcb_snap_btime,
455100cc610SDavid Disseldorp 		.exists_cb = ceph_vxattrcb_snap_btime_exists,
456100cc610SDavid Disseldorp 		.flags = VXATTR_FLAG_READONLY,
457100cc610SDavid Disseldorp 	},
458dd980fc0SLuis Henriques 	{
459dd980fc0SLuis Henriques 		.name = "ceph.caps",
460dd980fc0SLuis Henriques 		.name_size = sizeof("ceph.caps"),
461dd980fc0SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_caps,
462dd980fc0SLuis Henriques 		.exists_cb = NULL,
463dd980fc0SLuis Henriques 		.flags = VXATTR_FLAG_HIDDEN,
464dd980fc0SLuis Henriques 	},
4652c3dd4ffSAlex Elder 	{ .name = NULL, 0 }	/* Required table terminator */
466355da1ebSSage Weil };
467355da1ebSSage Weil 
468355da1ebSSage Weil /* files */
469355da1ebSSage Weil 
470881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = {
47132ab0bd7SSage Weil 	{
47232ab0bd7SSage Weil 		.name = "ceph.file.layout",
47332ab0bd7SSage Weil 		.name_size = sizeof("ceph.file.layout"),
47432ab0bd7SSage Weil 		.getxattr_cb = ceph_vxattrcb_layout,
47532ab0bd7SSage Weil 		.exists_cb = ceph_vxattrcb_layout_exists,
4764e9906e7SYan, Zheng 		.flags = VXATTR_FLAG_HIDDEN,
47732ab0bd7SSage Weil 	},
478695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
479695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, stripe_count),
480695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, object_size),
481695b7119SSage Weil 	XATTR_LAYOUT_FIELD(file, layout, pool),
482779fe0fbSYan, Zheng 	XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
483100cc610SDavid Disseldorp 	{
484100cc610SDavid Disseldorp 		.name = "ceph.snap.btime",
485100cc610SDavid Disseldorp 		.name_size = sizeof("ceph.snap.btime"),
486100cc610SDavid Disseldorp 		.getxattr_cb = ceph_vxattrcb_snap_btime,
487100cc610SDavid Disseldorp 		.exists_cb = ceph_vxattrcb_snap_btime_exists,
488100cc610SDavid Disseldorp 		.flags = VXATTR_FLAG_READONLY,
489100cc610SDavid Disseldorp 	},
490dd980fc0SLuis Henriques 	{
491dd980fc0SLuis Henriques 		.name = "ceph.caps",
492dd980fc0SLuis Henriques 		.name_size = sizeof("ceph.caps"),
493dd980fc0SLuis Henriques 		.getxattr_cb = ceph_vxattrcb_caps,
494dd980fc0SLuis Henriques 		.exists_cb = NULL,
495dd980fc0SLuis Henriques 		.flags = VXATTR_FLAG_HIDDEN,
496dd980fc0SLuis Henriques 	},
4972c3dd4ffSAlex Elder 	{ .name = NULL, 0 }	/* Required table terminator */
498355da1ebSSage Weil };
499355da1ebSSage Weil 
5005a9e2f5dSXiubo Li static struct ceph_vxattr ceph_common_vxattrs[] = {
5015a9e2f5dSXiubo Li 	{
5025a9e2f5dSXiubo Li 		.name = "ceph.cluster_fsid",
5035a9e2f5dSXiubo Li 		.name_size = sizeof("ceph.cluster_fsid"),
5045a9e2f5dSXiubo Li 		.getxattr_cb = ceph_vxattrcb_cluster_fsid,
5055a9e2f5dSXiubo Li 		.exists_cb = NULL,
5065a9e2f5dSXiubo Li 		.flags = VXATTR_FLAG_READONLY,
5075a9e2f5dSXiubo Li 	},
5085a9e2f5dSXiubo Li 	{
5095a9e2f5dSXiubo Li 		.name = "ceph.client_id",
5105a9e2f5dSXiubo Li 		.name_size = sizeof("ceph.client_id"),
5115a9e2f5dSXiubo Li 		.getxattr_cb = ceph_vxattrcb_client_id,
5125a9e2f5dSXiubo Li 		.exists_cb = NULL,
5135a9e2f5dSXiubo Li 		.flags = VXATTR_FLAG_READONLY,
5145a9e2f5dSXiubo Li 	},
51540e309deSJeff Layton 	{
51640e309deSJeff Layton 		.name = "ceph.auth_mds",
51740e309deSJeff Layton 		.name_size = sizeof("ceph.auth_mds"),
51840e309deSJeff Layton 		.getxattr_cb = ceph_vxattrcb_auth_mds,
51940e309deSJeff Layton 		.exists_cb = NULL,
52040e309deSJeff Layton 		.flags = VXATTR_FLAG_READONLY,
52140e309deSJeff Layton 	},
522f061fedaSJeff Layton #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
523f061fedaSJeff Layton 	{
524f061fedaSJeff Layton 		.name = "ceph.fscrypt.auth",
525f061fedaSJeff Layton 		.name_size = sizeof("ceph.fscrypt.auth"),
526f061fedaSJeff Layton 		.getxattr_cb = ceph_vxattrcb_fscrypt_auth,
527f061fedaSJeff Layton 		.exists_cb = ceph_vxattrcb_fscrypt_auth_exists,
528f061fedaSJeff Layton 		.flags = VXATTR_FLAG_READONLY,
529f061fedaSJeff Layton 	},
530f061fedaSJeff Layton #endif /* CONFIG_FS_ENCRYPTION */
5315a9e2f5dSXiubo Li 	{ .name = NULL, 0 }	/* Required table terminator */
5325a9e2f5dSXiubo Li };
5335a9e2f5dSXiubo Li 
ceph_inode_vxattrs(struct inode * inode)534881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
535355da1ebSSage Weil {
536355da1ebSSage Weil 	if (S_ISDIR(inode->i_mode))
537355da1ebSSage Weil 		return ceph_dir_vxattrs;
538355da1ebSSage Weil 	else if (S_ISREG(inode->i_mode))
539355da1ebSSage Weil 		return ceph_file_vxattrs;
540355da1ebSSage Weil 	return NULL;
541355da1ebSSage Weil }
542355da1ebSSage Weil 
ceph_match_vxattr(struct inode * inode,const char * name)543881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
544355da1ebSSage Weil 						const char *name)
545355da1ebSSage Weil {
546881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
54706476a69SAlex Elder 
54806476a69SAlex Elder 	if (vxattr) {
54906476a69SAlex Elder 		while (vxattr->name) {
55006476a69SAlex Elder 			if (!strcmp(vxattr->name, name))
551355da1ebSSage Weil 				return vxattr;
552355da1ebSSage Weil 			vxattr++;
55306476a69SAlex Elder 		}
55406476a69SAlex Elder 	}
55506476a69SAlex Elder 
5565a9e2f5dSXiubo Li 	vxattr = ceph_common_vxattrs;
5575a9e2f5dSXiubo Li 	while (vxattr->name) {
5585a9e2f5dSXiubo Li 		if (!strcmp(vxattr->name, name))
5595a9e2f5dSXiubo Li 			return vxattr;
5605a9e2f5dSXiubo Li 		vxattr++;
5615a9e2f5dSXiubo Li 	}
5625a9e2f5dSXiubo Li 
563355da1ebSSage Weil 	return NULL;
564355da1ebSSage Weil }
565355da1ebSSage Weil 
5667a6c3a03SXiubo Li #define MAX_XATTR_VAL_PRINT_LEN 256
5677a6c3a03SXiubo Li 
__set_xattr(struct ceph_inode_info * ci,const char * name,int name_len,const char * val,int val_len,int flags,int update_xattr,struct ceph_inode_xattr ** newxattr)568355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci,
569355da1ebSSage Weil 			   const char *name, int name_len,
570355da1ebSSage Weil 			   const char *val, int val_len,
571fbc0b970SYan, Zheng 			   int flags, int update_xattr,
572355da1ebSSage Weil 			   struct ceph_inode_xattr **newxattr)
573355da1ebSSage Weil {
574*38d46409SXiubo Li 	struct inode *inode = &ci->netfs.inode;
575*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
576355da1ebSSage Weil 	struct rb_node **p;
577355da1ebSSage Weil 	struct rb_node *parent = NULL;
578355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
579355da1ebSSage Weil 	int c;
580355da1ebSSage Weil 	int new = 0;
581355da1ebSSage Weil 
582355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
583355da1ebSSage Weil 	while (*p) {
584355da1ebSSage Weil 		parent = *p;
585355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
586355da1ebSSage Weil 		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
587355da1ebSSage Weil 		if (c < 0)
588355da1ebSSage Weil 			p = &(*p)->rb_left;
589355da1ebSSage Weil 		else if (c > 0)
590355da1ebSSage Weil 			p = &(*p)->rb_right;
591355da1ebSSage Weil 		else {
592355da1ebSSage Weil 			if (name_len == xattr->name_len)
593355da1ebSSage Weil 				break;
594355da1ebSSage Weil 			else if (name_len < xattr->name_len)
595355da1ebSSage Weil 				p = &(*p)->rb_left;
596355da1ebSSage Weil 			else
597355da1ebSSage Weil 				p = &(*p)->rb_right;
598355da1ebSSage Weil 		}
599355da1ebSSage Weil 		xattr = NULL;
600355da1ebSSage Weil 	}
601355da1ebSSage Weil 
602fbc0b970SYan, Zheng 	if (update_xattr) {
603fbc0b970SYan, Zheng 		int err = 0;
604eeca958dSLuis Henriques 
605fbc0b970SYan, Zheng 		if (xattr && (flags & XATTR_CREATE))
606fbc0b970SYan, Zheng 			err = -EEXIST;
607fbc0b970SYan, Zheng 		else if (!xattr && (flags & XATTR_REPLACE))
608fbc0b970SYan, Zheng 			err = -ENODATA;
609fbc0b970SYan, Zheng 		if (err) {
610fbc0b970SYan, Zheng 			kfree(name);
611fbc0b970SYan, Zheng 			kfree(val);
612eeca958dSLuis Henriques 			kfree(*newxattr);
613fbc0b970SYan, Zheng 			return err;
614fbc0b970SYan, Zheng 		}
615bcdfeb2eSYan, Zheng 		if (update_xattr < 0) {
616bcdfeb2eSYan, Zheng 			if (xattr)
617bcdfeb2eSYan, Zheng 				__remove_xattr(ci, xattr);
618bcdfeb2eSYan, Zheng 			kfree(name);
619eeca958dSLuis Henriques 			kfree(*newxattr);
620bcdfeb2eSYan, Zheng 			return 0;
621bcdfeb2eSYan, Zheng 		}
622fbc0b970SYan, Zheng 	}
623fbc0b970SYan, Zheng 
624355da1ebSSage Weil 	if (!xattr) {
625355da1ebSSage Weil 		new = 1;
626355da1ebSSage Weil 		xattr = *newxattr;
627355da1ebSSage Weil 		xattr->name = name;
628355da1ebSSage Weil 		xattr->name_len = name_len;
629fbc0b970SYan, Zheng 		xattr->should_free_name = update_xattr;
630355da1ebSSage Weil 
631355da1ebSSage Weil 		ci->i_xattrs.count++;
632*38d46409SXiubo Li 		doutc(cl, "count=%d\n", ci->i_xattrs.count);
633355da1ebSSage Weil 	} else {
634355da1ebSSage Weil 		kfree(*newxattr);
635355da1ebSSage Weil 		*newxattr = NULL;
636355da1ebSSage Weil 		if (xattr->should_free_val)
637c00e4522SXu Wang 			kfree(xattr->val);
638355da1ebSSage Weil 
639fbc0b970SYan, Zheng 		if (update_xattr) {
640c00e4522SXu Wang 			kfree(name);
641355da1ebSSage Weil 			name = xattr->name;
642355da1ebSSage Weil 		}
643355da1ebSSage Weil 		ci->i_xattrs.names_size -= xattr->name_len;
644355da1ebSSage Weil 		ci->i_xattrs.vals_size -= xattr->val_len;
645355da1ebSSage Weil 	}
646355da1ebSSage Weil 	ci->i_xattrs.names_size += name_len;
647355da1ebSSage Weil 	ci->i_xattrs.vals_size += val_len;
648355da1ebSSage Weil 	if (val)
649355da1ebSSage Weil 		xattr->val = val;
650355da1ebSSage Weil 	else
651355da1ebSSage Weil 		xattr->val = "";
652355da1ebSSage Weil 
653355da1ebSSage Weil 	xattr->val_len = val_len;
654fbc0b970SYan, Zheng 	xattr->dirty = update_xattr;
655fbc0b970SYan, Zheng 	xattr->should_free_val = (val && update_xattr);
656355da1ebSSage Weil 
657355da1ebSSage Weil 	if (new) {
658355da1ebSSage Weil 		rb_link_node(&xattr->node, parent, p);
659355da1ebSSage Weil 		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
660*38d46409SXiubo Li 		doutc(cl, "p=%p\n", p);
661355da1ebSSage Weil 	}
662355da1ebSSage Weil 
663*38d46409SXiubo Li 	doutc(cl, "added %p %llx.%llx xattr %p %.*s=%.*s%s\n", inode,
664*38d46409SXiubo Li 	      ceph_vinop(inode), xattr, name_len, name, min(val_len,
665*38d46409SXiubo Li 	      MAX_XATTR_VAL_PRINT_LEN), val,
6667a6c3a03SXiubo Li 	      val_len > MAX_XATTR_VAL_PRINT_LEN ? "..." : "");
667355da1ebSSage Weil 
668355da1ebSSage Weil 	return 0;
669355da1ebSSage Weil }
670355da1ebSSage Weil 
__get_xattr(struct ceph_inode_info * ci,const char * name)671355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
672355da1ebSSage Weil 			   const char *name)
673355da1ebSSage Weil {
674*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
675355da1ebSSage Weil 	struct rb_node **p;
676355da1ebSSage Weil 	struct rb_node *parent = NULL;
677355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
67817db143fSSage Weil 	int name_len = strlen(name);
679355da1ebSSage Weil 	int c;
680355da1ebSSage Weil 
681355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
682355da1ebSSage Weil 	while (*p) {
683355da1ebSSage Weil 		parent = *p;
684355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
685355da1ebSSage Weil 		c = strncmp(name, xattr->name, xattr->name_len);
68617db143fSSage Weil 		if (c == 0 && name_len > xattr->name_len)
68717db143fSSage Weil 			c = 1;
688355da1ebSSage Weil 		if (c < 0)
689355da1ebSSage Weil 			p = &(*p)->rb_left;
690355da1ebSSage Weil 		else if (c > 0)
691355da1ebSSage Weil 			p = &(*p)->rb_right;
692355da1ebSSage Weil 		else {
6937a6c3a03SXiubo Li 			int len = min(xattr->val_len, MAX_XATTR_VAL_PRINT_LEN);
6947a6c3a03SXiubo Li 
695*38d46409SXiubo Li 			doutc(cl, "%s found %.*s%s\n", name, len, xattr->val,
696*38d46409SXiubo Li 			      xattr->val_len > len ? "..." : "");
697355da1ebSSage Weil 			return xattr;
698355da1ebSSage Weil 		}
699355da1ebSSage Weil 	}
700355da1ebSSage Weil 
701*38d46409SXiubo Li 	doutc(cl, "%s not found\n", name);
702355da1ebSSage Weil 
703355da1ebSSage Weil 	return NULL;
704355da1ebSSage Weil }
705355da1ebSSage Weil 
__free_xattr(struct ceph_inode_xattr * xattr)706355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr)
707355da1ebSSage Weil {
708355da1ebSSage Weil 	BUG_ON(!xattr);
709355da1ebSSage Weil 
710355da1ebSSage Weil 	if (xattr->should_free_name)
711c00e4522SXu Wang 		kfree(xattr->name);
712355da1ebSSage Weil 	if (xattr->should_free_val)
713c00e4522SXu Wang 		kfree(xattr->val);
714355da1ebSSage Weil 
715355da1ebSSage Weil 	kfree(xattr);
716355da1ebSSage Weil }
717355da1ebSSage Weil 
__remove_xattr(struct ceph_inode_info * ci,struct ceph_inode_xattr * xattr)718355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci,
719355da1ebSSage Weil 			  struct ceph_inode_xattr *xattr)
720355da1ebSSage Weil {
721355da1ebSSage Weil 	if (!xattr)
722524186acSYan, Zheng 		return -ENODATA;
723355da1ebSSage Weil 
724355da1ebSSage Weil 	rb_erase(&xattr->node, &ci->i_xattrs.index);
725355da1ebSSage Weil 
726355da1ebSSage Weil 	if (xattr->should_free_name)
727c00e4522SXu Wang 		kfree(xattr->name);
728355da1ebSSage Weil 	if (xattr->should_free_val)
729c00e4522SXu Wang 		kfree(xattr->val);
730355da1ebSSage Weil 
731355da1ebSSage Weil 	ci->i_xattrs.names_size -= xattr->name_len;
732355da1ebSSage Weil 	ci->i_xattrs.vals_size -= xattr->val_len;
733355da1ebSSage Weil 	ci->i_xattrs.count--;
734355da1ebSSage Weil 	kfree(xattr);
735355da1ebSSage Weil 
736355da1ebSSage Weil 	return 0;
737355da1ebSSage Weil }
738355da1ebSSage Weil 
__copy_xattr_names(struct ceph_inode_info * ci,char * dest)739355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci,
740355da1ebSSage Weil 				char *dest)
741355da1ebSSage Weil {
742*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
743355da1ebSSage Weil 	struct rb_node *p;
744355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
745355da1ebSSage Weil 
746355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
747*38d46409SXiubo Li 	doutc(cl, "count=%d\n", ci->i_xattrs.count);
748355da1ebSSage Weil 
749355da1ebSSage Weil 	while (p) {
750355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
751355da1ebSSage Weil 		memcpy(dest, xattr->name, xattr->name_len);
752355da1ebSSage Weil 		dest[xattr->name_len] = '\0';
753355da1ebSSage Weil 
754*38d46409SXiubo Li 		doutc(cl, "dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
755355da1ebSSage Weil 		      xattr->name_len, ci->i_xattrs.names_size);
756355da1ebSSage Weil 
757355da1ebSSage Weil 		dest += xattr->name_len + 1;
758355da1ebSSage Weil 		p = rb_next(p);
759355da1ebSSage Weil 	}
760355da1ebSSage Weil 
761355da1ebSSage Weil 	return dest;
762355da1ebSSage Weil }
763355da1ebSSage Weil 
__ceph_destroy_xattrs(struct ceph_inode_info * ci)764355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
765355da1ebSSage Weil {
766*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
767355da1ebSSage Weil 	struct rb_node *p, *tmp;
768355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
769355da1ebSSage Weil 
770355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
771355da1ebSSage Weil 
772*38d46409SXiubo Li 	doutc(cl, "p=%p\n", p);
773355da1ebSSage Weil 
774355da1ebSSage Weil 	while (p) {
775355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
776355da1ebSSage Weil 		tmp = p;
777355da1ebSSage Weil 		p = rb_next(tmp);
778*38d46409SXiubo Li 		doutc(cl, "next p=%p (%.*s)\n", p, xattr->name_len, xattr->name);
779355da1ebSSage Weil 		rb_erase(tmp, &ci->i_xattrs.index);
780355da1ebSSage Weil 
781355da1ebSSage Weil 		__free_xattr(xattr);
782355da1ebSSage Weil 	}
783355da1ebSSage Weil 
784355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
785355da1ebSSage Weil 	ci->i_xattrs.vals_size = 0;
786355da1ebSSage Weil 	ci->i_xattrs.index_version = 0;
787355da1ebSSage Weil 	ci->i_xattrs.count = 0;
788355da1ebSSage Weil 	ci->i_xattrs.index = RB_ROOT;
789355da1ebSSage Weil }
790355da1ebSSage Weil 
__build_xattrs(struct inode * inode)791355da1ebSSage Weil static int __build_xattrs(struct inode *inode)
792be655596SSage Weil 	__releases(ci->i_ceph_lock)
793be655596SSage Weil 	__acquires(ci->i_ceph_lock)
794355da1ebSSage Weil {
795*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
796355da1ebSSage Weil 	u32 namelen;
797355da1ebSSage Weil 	u32 numattr = 0;
798355da1ebSSage Weil 	void *p, *end;
799355da1ebSSage Weil 	u32 len;
800355da1ebSSage Weil 	const char *name, *val;
801355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
8020eb30853SXiubo Li 	u64 xattr_version;
803355da1ebSSage Weil 	struct ceph_inode_xattr **xattrs = NULL;
80463ff78b2SSage Weil 	int err = 0;
805355da1ebSSage Weil 	int i;
806355da1ebSSage Weil 
807*38d46409SXiubo Li 	doutc(cl, "len=%d\n",
808355da1ebSSage Weil 	      ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
809355da1ebSSage Weil 
810355da1ebSSage Weil 	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
811355da1ebSSage Weil 		return 0; /* already built */
812355da1ebSSage Weil 
813355da1ebSSage Weil 	__ceph_destroy_xattrs(ci);
814355da1ebSSage Weil 
815355da1ebSSage Weil start:
816355da1ebSSage Weil 	/* updated internal xattr rb tree */
817355da1ebSSage Weil 	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
818355da1ebSSage Weil 		p = ci->i_xattrs.blob->vec.iov_base;
819355da1ebSSage Weil 		end = p + ci->i_xattrs.blob->vec.iov_len;
820355da1ebSSage Weil 		ceph_decode_32_safe(&p, end, numattr, bad);
821355da1ebSSage Weil 		xattr_version = ci->i_xattrs.version;
822be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
823355da1ebSSage Weil 
8247e8a2952SIlya Dryomov 		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
825355da1ebSSage Weil 				 GFP_NOFS);
826355da1ebSSage Weil 		err = -ENOMEM;
827355da1ebSSage Weil 		if (!xattrs)
828355da1ebSSage Weil 			goto bad_lock;
8291a295bd8SIlya Dryomov 
830355da1ebSSage Weil 		for (i = 0; i < numattr; i++) {
831355da1ebSSage Weil 			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
832355da1ebSSage Weil 					    GFP_NOFS);
833355da1ebSSage Weil 			if (!xattrs[i])
834355da1ebSSage Weil 				goto bad_lock;
835355da1ebSSage Weil 		}
836355da1ebSSage Weil 
837be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
838355da1ebSSage Weil 		if (ci->i_xattrs.version != xattr_version) {
839355da1ebSSage Weil 			/* lost a race, retry */
840355da1ebSSage Weil 			for (i = 0; i < numattr; i++)
841355da1ebSSage Weil 				kfree(xattrs[i]);
842355da1ebSSage Weil 			kfree(xattrs);
84321ec6ffaSAlan Cox 			xattrs = NULL;
844355da1ebSSage Weil 			goto start;
845355da1ebSSage Weil 		}
846355da1ebSSage Weil 		err = -EIO;
847355da1ebSSage Weil 		while (numattr--) {
848355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
849355da1ebSSage Weil 			namelen = len;
850355da1ebSSage Weil 			name = p;
851355da1ebSSage Weil 			p += len;
852355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
853355da1ebSSage Weil 			val = p;
854355da1ebSSage Weil 			p += len;
855355da1ebSSage Weil 
856355da1ebSSage Weil 			err = __set_xattr(ci, name, namelen, val, len,
857fbc0b970SYan, Zheng 					  0, 0, &xattrs[numattr]);
858355da1ebSSage Weil 
859355da1ebSSage Weil 			if (err < 0)
860355da1ebSSage Weil 				goto bad;
861355da1ebSSage Weil 		}
862355da1ebSSage Weil 		kfree(xattrs);
863355da1ebSSage Weil 	}
864355da1ebSSage Weil 	ci->i_xattrs.index_version = ci->i_xattrs.version;
865355da1ebSSage Weil 	ci->i_xattrs.dirty = false;
866355da1ebSSage Weil 
867355da1ebSSage Weil 	return err;
868355da1ebSSage Weil bad_lock:
869be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
870355da1ebSSage Weil bad:
871355da1ebSSage Weil 	if (xattrs) {
872355da1ebSSage Weil 		for (i = 0; i < numattr; i++)
873355da1ebSSage Weil 			kfree(xattrs[i]);
874355da1ebSSage Weil 		kfree(xattrs);
875355da1ebSSage Weil 	}
876355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
877355da1ebSSage Weil 	return err;
878355da1ebSSage Weil }
879355da1ebSSage Weil 
__get_required_blob_size(struct ceph_inode_info * ci,int name_size,int val_size)880355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
881355da1ebSSage Weil 				    int val_size)
882355da1ebSSage Weil {
883*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
884*38d46409SXiubo Li 
885355da1ebSSage Weil 	/*
886355da1ebSSage Weil 	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
887355da1ebSSage Weil 	 * 4 bytes per each value
888355da1ebSSage Weil 	 */
889355da1ebSSage Weil 	int size = 4 + ci->i_xattrs.count*(4 + 4) +
890355da1ebSSage Weil 			     ci->i_xattrs.names_size +
891355da1ebSSage Weil 			     ci->i_xattrs.vals_size;
892*38d46409SXiubo Li 	doutc(cl, "c=%d names.size=%d vals.size=%d\n", ci->i_xattrs.count,
893*38d46409SXiubo Li 	      ci->i_xattrs.names_size, ci->i_xattrs.vals_size);
894355da1ebSSage Weil 
895355da1ebSSage Weil 	if (name_size)
896355da1ebSSage Weil 		size += 4 + 4 + name_size + val_size;
897355da1ebSSage Weil 
898355da1ebSSage Weil 	return size;
899355da1ebSSage Weil }
900355da1ebSSage Weil 
901355da1ebSSage Weil /*
902355da1ebSSage Weil  * If there are dirty xattrs, re-encode xattrs into the prealloc_blob
90312fe3ddaSLuis Henriques  * and swap into place.  It returns the old i_xattrs.blob (or NULL) so
90412fe3ddaSLuis Henriques  * that it can be freed by the caller as the i_ceph_lock is likely to be
90512fe3ddaSLuis Henriques  * held.
906355da1ebSSage Weil  */
__ceph_build_xattrs_blob(struct ceph_inode_info * ci)90712fe3ddaSLuis Henriques struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
908355da1ebSSage Weil {
909*38d46409SXiubo Li 	struct inode *inode = &ci->netfs.inode;
910*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
911355da1ebSSage Weil 	struct rb_node *p;
912355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
91312fe3ddaSLuis Henriques 	struct ceph_buffer *old_blob = NULL;
914355da1ebSSage Weil 	void *dest;
915355da1ebSSage Weil 
916*38d46409SXiubo Li 	doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
917355da1ebSSage Weil 	if (ci->i_xattrs.dirty) {
918355da1ebSSage Weil 		int need = __get_required_blob_size(ci, 0, 0);
919355da1ebSSage Weil 
920355da1ebSSage Weil 		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
921355da1ebSSage Weil 
922355da1ebSSage Weil 		p = rb_first(&ci->i_xattrs.index);
923355da1ebSSage Weil 		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
924355da1ebSSage Weil 
925355da1ebSSage Weil 		ceph_encode_32(&dest, ci->i_xattrs.count);
926355da1ebSSage Weil 		while (p) {
927355da1ebSSage Weil 			xattr = rb_entry(p, struct ceph_inode_xattr, node);
928355da1ebSSage Weil 
929355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->name_len);
930355da1ebSSage Weil 			memcpy(dest, xattr->name, xattr->name_len);
931355da1ebSSage Weil 			dest += xattr->name_len;
932355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->val_len);
933355da1ebSSage Weil 			memcpy(dest, xattr->val, xattr->val_len);
934355da1ebSSage Weil 			dest += xattr->val_len;
935355da1ebSSage Weil 
936355da1ebSSage Weil 			p = rb_next(p);
937355da1ebSSage Weil 		}
938355da1ebSSage Weil 
939355da1ebSSage Weil 		/* adjust buffer len; it may be larger than we need */
940355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob->vec.iov_len =
941355da1ebSSage Weil 			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
942355da1ebSSage Weil 
943b6c1d5b8SSage Weil 		if (ci->i_xattrs.blob)
94412fe3ddaSLuis Henriques 			old_blob = ci->i_xattrs.blob;
945355da1ebSSage Weil 		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
946355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = NULL;
947355da1ebSSage Weil 		ci->i_xattrs.dirty = false;
9484a625be4SSage Weil 		ci->i_xattrs.version++;
949355da1ebSSage Weil 	}
95012fe3ddaSLuis Henriques 
95112fe3ddaSLuis Henriques 	return old_blob;
952355da1ebSSage Weil }
953355da1ebSSage Weil 
__get_request_mask(struct inode * in)954315f2408SYan, Zheng static inline int __get_request_mask(struct inode *in) {
955315f2408SYan, Zheng 	struct ceph_mds_request *req = current->journal_info;
956315f2408SYan, Zheng 	int mask = 0;
957315f2408SYan, Zheng 	if (req && req->r_target_inode == in) {
958315f2408SYan, Zheng 		if (req->r_op == CEPH_MDS_OP_LOOKUP ||
959315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_LOOKUPINO ||
960315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
961315f2408SYan, Zheng 		    req->r_op == CEPH_MDS_OP_GETATTR) {
962315f2408SYan, Zheng 			mask = le32_to_cpu(req->r_args.getattr.mask);
963315f2408SYan, Zheng 		} else if (req->r_op == CEPH_MDS_OP_OPEN ||
964315f2408SYan, Zheng 			   req->r_op == CEPH_MDS_OP_CREATE) {
965315f2408SYan, Zheng 			mask = le32_to_cpu(req->r_args.open.mask);
966315f2408SYan, Zheng 		}
967315f2408SYan, Zheng 	}
968315f2408SYan, Zheng 	return mask;
969315f2408SYan, Zheng }
970315f2408SYan, Zheng 
__ceph_getxattr(struct inode * inode,const char * name,void * value,size_t size)9717221fe4cSGuangliang Zhao ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
972355da1ebSSage Weil 		      size_t size)
973355da1ebSSage Weil {
974*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
975355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
976355da1ebSSage Weil 	struct ceph_inode_xattr *xattr;
9776ddf5f16SMilind Changire 	struct ceph_vxattr *vxattr;
978315f2408SYan, Zheng 	int req_mask;
979f1d1b51dSJeff Layton 	ssize_t err;
980355da1ebSSage Weil 
9816ddf5f16SMilind Changire 	if (strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
9826ddf5f16SMilind Changire 		goto handle_non_vxattrs;
9836ddf5f16SMilind Changire 
9840bee82fbSSage Weil 	/* let's see if a virtual xattr was requested */
9850bee82fbSSage Weil 	vxattr = ceph_match_vxattr(inode, name);
98629dccfa5SYan, Zheng 	if (vxattr) {
98749a9f4f6SYan, Zheng 		int mask = 0;
98849a9f4f6SYan, Zheng 		if (vxattr->flags & VXATTR_FLAG_RSTAT)
98949a9f4f6SYan, Zheng 			mask |= CEPH_STAT_RSTAT;
99081048c00SJeff Layton 		if (vxattr->flags & VXATTR_FLAG_DIRSTAT)
99181048c00SJeff Layton 			mask |= CEPH_CAP_FILE_SHARED;
99249a9f4f6SYan, Zheng 		err = ceph_do_getattr(inode, mask, true);
9931684dd03SYan, Zheng 		if (err)
9941684dd03SYan, Zheng 			return err;
99529dccfa5SYan, Zheng 		err = -ENODATA;
9963b421018SJeff Layton 		if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
9970bee82fbSSage Weil 			err = vxattr->getxattr_cb(ci, value, size);
9983b421018SJeff Layton 			if (size && size < err)
9993b421018SJeff Layton 				err = -ERANGE;
10003b421018SJeff Layton 		}
1001a1dc1937Smajianpeng 		return err;
10026ddf5f16SMilind Changire 	} else {
10036ddf5f16SMilind Changire 		err = ceph_do_getvxattr(inode, name, value, size);
10046ddf5f16SMilind Changire 		/* this would happen with a new client and old server combo */
10056ddf5f16SMilind Changire 		if (err == -EOPNOTSUPP)
10066ddf5f16SMilind Changire 			err = -ENODATA;
10076ddf5f16SMilind Changire 		return err;
10080bee82fbSSage Weil 	}
10096ddf5f16SMilind Changire handle_non_vxattrs:
1010315f2408SYan, Zheng 	req_mask = __get_request_mask(inode);
1011315f2408SYan, Zheng 
1012a1dc1937Smajianpeng 	spin_lock(&ci->i_ceph_lock);
1013*38d46409SXiubo Li 	doutc(cl, "%p %llx.%llx name '%s' ver=%lld index_ver=%lld\n", inode,
1014*38d46409SXiubo Li 	      ceph_vinop(inode), name, ci->i_xattrs.version,
1015*38d46409SXiubo Li 	      ci->i_xattrs.index_version);
1016a1dc1937Smajianpeng 
1017508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 ||
1018315f2408SYan, Zheng 	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
10191af16d54SXiubo Li 	      __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
1020be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1021315f2408SYan, Zheng 
1022315f2408SYan, Zheng 		/* security module gets xattr while filling trace */
1023d37b1d99SMarkus Elfring 		if (current->journal_info) {
1024*38d46409SXiubo Li 			pr_warn_ratelimited_client(cl,
1025*38d46409SXiubo Li 				"sync %p %llx.%llx during filling trace\n",
1026*38d46409SXiubo Li 				inode, ceph_vinop(inode));
1027315f2408SYan, Zheng 			return -EBUSY;
1028315f2408SYan, Zheng 		}
1029315f2408SYan, Zheng 
1030355da1ebSSage Weil 		/* get xattrs from mds (if we don't already have them) */
1031508b32d8SYan, Zheng 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
1032355da1ebSSage Weil 		if (err)
1033355da1ebSSage Weil 			return err;
1034be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1035508b32d8SYan, Zheng 	}
1036355da1ebSSage Weil 
1037355da1ebSSage Weil 	err = __build_xattrs(inode);
1038355da1ebSSage Weil 	if (err < 0)
1039355da1ebSSage Weil 		goto out;
1040355da1ebSSage Weil 
1041355da1ebSSage Weil 	err = -ENODATA;  /* == ENOATTR */
1042355da1ebSSage Weil 	xattr = __get_xattr(ci, name);
10430bee82fbSSage Weil 	if (!xattr)
1044355da1ebSSage Weil 		goto out;
1045355da1ebSSage Weil 
1046355da1ebSSage Weil 	err = -ERANGE;
1047355da1ebSSage Weil 	if (size && size < xattr->val_len)
1048355da1ebSSage Weil 		goto out;
1049355da1ebSSage Weil 
1050355da1ebSSage Weil 	err = xattr->val_len;
1051355da1ebSSage Weil 	if (size == 0)
1052355da1ebSSage Weil 		goto out;
1053355da1ebSSage Weil 
1054355da1ebSSage Weil 	memcpy(value, xattr->val, xattr->val_len);
1055355da1ebSSage Weil 
1056d37b1d99SMarkus Elfring 	if (current->journal_info &&
1057026105ebSJeff Layton 	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
1058026105ebSJeff Layton 	    security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
1059315f2408SYan, Zheng 		ci->i_ceph_flags |= CEPH_I_SEC_INITED;
1060355da1ebSSage Weil out:
1061be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1062355da1ebSSage Weil 	return err;
1063355da1ebSSage Weil }
1064355da1ebSSage Weil 
ceph_listxattr(struct dentry * dentry,char * names,size_t size)1065355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
1066355da1ebSSage Weil {
10672b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
1068*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
1069355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
10702b2abcacSDavid Disseldorp 	bool len_only = (size == 0);
1071355da1ebSSage Weil 	u32 namelen;
1072355da1ebSSage Weil 	int err;
1073355da1ebSSage Weil 
1074be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1075*38d46409SXiubo Li 	doutc(cl, "%p %llx.%llx ver=%lld index_ver=%lld\n", inode,
1076*38d46409SXiubo Li 	      ceph_vinop(inode), ci->i_xattrs.version,
1077*38d46409SXiubo Li 	      ci->i_xattrs.index_version);
1078355da1ebSSage Weil 
1079508b32d8SYan, Zheng 	if (ci->i_xattrs.version == 0 ||
10801af16d54SXiubo Li 	    !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
1081be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1082508b32d8SYan, Zheng 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
1083355da1ebSSage Weil 		if (err)
1084355da1ebSSage Weil 			return err;
1085be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1086508b32d8SYan, Zheng 	}
1087355da1ebSSage Weil 
1088355da1ebSSage Weil 	err = __build_xattrs(inode);
1089355da1ebSSage Weil 	if (err < 0)
1090355da1ebSSage Weil 		goto out;
10913ce6cd12SAlex Elder 
10922b2abcacSDavid Disseldorp 	/* add 1 byte for each xattr due to the null termination */
1093b65917ddSSage Weil 	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
10942b2abcacSDavid Disseldorp 	if (!len_only) {
10952b2abcacSDavid Disseldorp 		if (namelen > size) {
1096355da1ebSSage Weil 			err = -ERANGE;
1097355da1ebSSage Weil 			goto out;
10982b2abcacSDavid Disseldorp 		}
1099355da1ebSSage Weil 		names = __copy_xattr_names(ci, names);
11002b2abcacSDavid Disseldorp 		size -= namelen;
11012b2abcacSDavid Disseldorp 	}
11022b2abcacSDavid Disseldorp 	err = namelen;
1103355da1ebSSage Weil out:
1104be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1105355da1ebSSage Weil 	return err;
1106355da1ebSSage Weil }
1107355da1ebSSage Weil 
ceph_sync_setxattr(struct inode * inode,const char * name,const char * value,size_t size,int flags)1108a26feccaSAndreas Gruenbacher static int ceph_sync_setxattr(struct inode *inode, const char *name,
1109355da1ebSSage Weil 			      const char *value, size_t size, int flags)
1110355da1ebSSage Weil {
11115995d90dSXiubo Li 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
1112*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
1113355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1114355da1ebSSage Weil 	struct ceph_mds_request *req;
11153d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
1116968cd14eSXiubo Li 	struct ceph_osd_client *osdc = &fsc->client->osdc;
111725e6bae3SYan, Zheng 	struct ceph_pagelist *pagelist = NULL;
111804303d8aSYan, Zheng 	int op = CEPH_MDS_OP_SETXATTR;
1119355da1ebSSage Weil 	int err;
1120355da1ebSSage Weil 
11210aeff37aSYan, Zheng 	if (size > 0) {
112225e6bae3SYan, Zheng 		/* copy value into pagelist */
112333165d47SIlya Dryomov 		pagelist = ceph_pagelist_alloc(GFP_NOFS);
112425e6bae3SYan, Zheng 		if (!pagelist)
1125355da1ebSSage Weil 			return -ENOMEM;
112625e6bae3SYan, Zheng 
112725e6bae3SYan, Zheng 		err = ceph_pagelist_append(pagelist, value, size);
112825e6bae3SYan, Zheng 		if (err)
1129355da1ebSSage Weil 			goto out;
11300aeff37aSYan, Zheng 	} else if (!value) {
113104303d8aSYan, Zheng 		if (flags & CEPH_XATTR_REPLACE)
113204303d8aSYan, Zheng 			op = CEPH_MDS_OP_RMXATTR;
113304303d8aSYan, Zheng 		else
113425e6bae3SYan, Zheng 			flags |= CEPH_XATTR_REMOVE;
1135355da1ebSSage Weil 	}
1136355da1ebSSage Weil 
1137*38d46409SXiubo Li 	doutc(cl, "name %s value size %zu\n", name, size);
1138355da1ebSSage Weil 
1139355da1ebSSage Weil 	/* do request */
114004303d8aSYan, Zheng 	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
114160d87733SJulia Lawall 	if (IS_ERR(req)) {
114260d87733SJulia Lawall 		err = PTR_ERR(req);
114360d87733SJulia Lawall 		goto out;
114460d87733SJulia Lawall 	}
1145a149bb9aSSanidhya Kashyap 
1146355da1ebSSage Weil 	req->r_path2 = kstrdup(name, GFP_NOFS);
1147a149bb9aSSanidhya Kashyap 	if (!req->r_path2) {
1148a149bb9aSSanidhya Kashyap 		ceph_mdsc_put_request(req);
1149a149bb9aSSanidhya Kashyap 		err = -ENOMEM;
1150a149bb9aSSanidhya Kashyap 		goto out;
1151a149bb9aSSanidhya Kashyap 	}
1152355da1ebSSage Weil 
115304303d8aSYan, Zheng 	if (op == CEPH_MDS_OP_SETXATTR) {
115404303d8aSYan, Zheng 		req->r_args.setxattr.flags = cpu_to_le32(flags);
1155968cd14eSXiubo Li 		req->r_args.setxattr.osdmap_epoch =
1156968cd14eSXiubo Li 			cpu_to_le32(osdc->osdmap->epoch);
115725e6bae3SYan, Zheng 		req->r_pagelist = pagelist;
115825e6bae3SYan, Zheng 		pagelist = NULL;
115904303d8aSYan, Zheng 	}
1160355da1ebSSage Weil 
1161a149bb9aSSanidhya Kashyap 	req->r_inode = inode;
1162a149bb9aSSanidhya Kashyap 	ihold(inode);
1163a149bb9aSSanidhya Kashyap 	req->r_num_caps = 1;
1164a149bb9aSSanidhya Kashyap 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1165a149bb9aSSanidhya Kashyap 
1166*38d46409SXiubo Li 	doutc(cl, "xattr.ver (before): %lld\n", ci->i_xattrs.version);
1167752c8bdcSSage Weil 	err = ceph_mdsc_do_request(mdsc, NULL, req);
1168355da1ebSSage Weil 	ceph_mdsc_put_request(req);
1169*38d46409SXiubo Li 	doutc(cl, "xattr.ver (after): %lld\n", ci->i_xattrs.version);
1170355da1ebSSage Weil 
1171355da1ebSSage Weil out:
117225e6bae3SYan, Zheng 	if (pagelist)
117325e6bae3SYan, Zheng 		ceph_pagelist_release(pagelist);
1174355da1ebSSage Weil 	return err;
1175355da1ebSSage Weil }
1176355da1ebSSage Weil 
__ceph_setxattr(struct inode * inode,const char * name,const void * value,size_t size,int flags)1177a26feccaSAndreas Gruenbacher int __ceph_setxattr(struct inode *inode, const char *name,
1178355da1ebSSage Weil 			const void *value, size_t size, int flags)
1179355da1ebSSage Weil {
1180*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
1181881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr;
1182355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
11835995d90dSXiubo Li 	struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
1184f66fd9f0SYan, Zheng 	struct ceph_cap_flush *prealloc_cf = NULL;
118586968ef2SLuis Henriques 	struct ceph_buffer *old_blob = NULL;
118618fa8b3fSAlex Elder 	int issued;
1187355da1ebSSage Weil 	int err;
1188fbc0b970SYan, Zheng 	int dirty = 0;
1189355da1ebSSage Weil 	int name_len = strlen(name);
1190355da1ebSSage Weil 	int val_len = size;
1191355da1ebSSage Weil 	char *newname = NULL;
1192355da1ebSSage Weil 	char *newval = NULL;
1193355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
1194355da1ebSSage Weil 	int required_blob_size;
1195f1919826SYan, Zheng 	bool check_realm = false;
1196604d1b02SYan, Zheng 	bool lock_snap_rwsem = false;
1197355da1ebSSage Weil 
11982cdeb1e4SAndreas Gruenbacher 	if (ceph_snap(inode) != CEPH_NOSNAP)
11992cdeb1e4SAndreas Gruenbacher 		return -EROFS;
1200355da1ebSSage Weil 
120106476a69SAlex Elder 	vxattr = ceph_match_vxattr(inode, name);
1202f1919826SYan, Zheng 	if (vxattr) {
12034e9906e7SYan, Zheng 		if (vxattr->flags & VXATTR_FLAG_READONLY)
1204355da1ebSSage Weil 			return -EOPNOTSUPP;
1205f1919826SYan, Zheng 		if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1206f1919826SYan, Zheng 			check_realm = true;
1207f1919826SYan, Zheng 	}
1208355da1ebSSage Weil 
12093adf654dSSage Weil 	/* pass any unhandled ceph.* xattrs through to the MDS */
12103adf654dSSage Weil 	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
12113adf654dSSage Weil 		goto do_sync_unlocked;
12123adf654dSSage Weil 
1213355da1ebSSage Weil 	/* preallocate memory for xattr name, value, index node */
1214355da1ebSSage Weil 	err = -ENOMEM;
121561413c2fSJulia Lawall 	newname = kmemdup(name, name_len + 1, GFP_NOFS);
1216355da1ebSSage Weil 	if (!newname)
1217355da1ebSSage Weil 		goto out;
1218355da1ebSSage Weil 
1219355da1ebSSage Weil 	if (val_len) {
1220b829c195SAlex Elder 		newval = kmemdup(value, val_len, GFP_NOFS);
1221355da1ebSSage Weil 		if (!newval)
1222355da1ebSSage Weil 			goto out;
1223355da1ebSSage Weil 	}
1224355da1ebSSage Weil 
1225355da1ebSSage Weil 	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1226355da1ebSSage Weil 	if (!xattr)
1227355da1ebSSage Weil 		goto out;
1228355da1ebSSage Weil 
1229f66fd9f0SYan, Zheng 	prealloc_cf = ceph_alloc_cap_flush();
1230f66fd9f0SYan, Zheng 	if (!prealloc_cf)
1231f66fd9f0SYan, Zheng 		goto out;
1232f66fd9f0SYan, Zheng 
1233be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1234355da1ebSSage Weil retry:
1235355da1ebSSage Weil 	issued = __ceph_caps_issued(ci, NULL);
1236d93231a6SLuís Henriques 	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1237d93231a6SLuís Henriques 	if ((ci->i_xattrs.version == 0) || !(issued & CEPH_CAP_XATTR_EXCL) ||
1238d93231a6SLuís Henriques 	    (required_blob_size > mdsc->mdsmap->m_max_xattr_size)) {
1239*38d46409SXiubo Li 		doutc(cl, "sync version: %llu size: %d max: %llu\n",
1240*38d46409SXiubo Li 		      ci->i_xattrs.version, required_blob_size,
1241d93231a6SLuís Henriques 		      mdsc->mdsmap->m_max_xattr_size);
1242355da1ebSSage Weil 		goto do_sync;
1243d93231a6SLuís Henriques 	}
1244604d1b02SYan, Zheng 
1245604d1b02SYan, Zheng 	if (!lock_snap_rwsem && !ci->i_head_snapc) {
1246604d1b02SYan, Zheng 		lock_snap_rwsem = true;
1247604d1b02SYan, Zheng 		if (!down_read_trylock(&mdsc->snap_rwsem)) {
1248604d1b02SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
1249604d1b02SYan, Zheng 			down_read(&mdsc->snap_rwsem);
1250604d1b02SYan, Zheng 			spin_lock(&ci->i_ceph_lock);
1251604d1b02SYan, Zheng 			goto retry;
1252604d1b02SYan, Zheng 		}
1253604d1b02SYan, Zheng 	}
1254604d1b02SYan, Zheng 
1255*38d46409SXiubo Li 	doutc(cl, "%p %llx.%llx name '%s' issued %s\n", inode,
1256*38d46409SXiubo Li 	      ceph_vinop(inode), name, ceph_cap_string(issued));
1257355da1ebSSage Weil 	__build_xattrs(inode);
1258355da1ebSSage Weil 
1259355da1ebSSage Weil 	if (!ci->i_xattrs.prealloc_blob ||
1260355da1ebSSage Weil 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
126118fa8b3fSAlex Elder 		struct ceph_buffer *blob;
1262355da1ebSSage Weil 
1263be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
126486968ef2SLuis Henriques 		ceph_buffer_put(old_blob); /* Shouldn't be required */
1265*38d46409SXiubo Li 		doutc(cl, " pre-allocating new blob size=%d\n",
1266*38d46409SXiubo Li 		      required_blob_size);
1267b6c1d5b8SSage Weil 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1268355da1ebSSage Weil 		if (!blob)
1269604d1b02SYan, Zheng 			goto do_sync_unlocked;
1270be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
127186968ef2SLuis Henriques 		/* prealloc_blob can't be released while holding i_ceph_lock */
1272b6c1d5b8SSage Weil 		if (ci->i_xattrs.prealloc_blob)
127386968ef2SLuis Henriques 			old_blob = ci->i_xattrs.prealloc_blob;
1274355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = blob;
1275355da1ebSSage Weil 		goto retry;
1276355da1ebSSage Weil 	}
1277355da1ebSSage Weil 
1278bcdfeb2eSYan, Zheng 	err = __set_xattr(ci, newname, name_len, newval, val_len,
1279bcdfeb2eSYan, Zheng 			  flags, value ? 1 : -1, &xattr);
128018fa8b3fSAlex Elder 
1281fbc0b970SYan, Zheng 	if (!err) {
1282f66fd9f0SYan, Zheng 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1283f66fd9f0SYan, Zheng 					       &prealloc_cf);
1284355da1ebSSage Weil 		ci->i_xattrs.dirty = true;
12857795aef0SJeff Layton 		inode_set_ctime_current(inode);
1286fbc0b970SYan, Zheng 	}
128718fa8b3fSAlex Elder 
1288be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
128986968ef2SLuis Henriques 	ceph_buffer_put(old_blob);
1290604d1b02SYan, Zheng 	if (lock_snap_rwsem)
1291604d1b02SYan, Zheng 		up_read(&mdsc->snap_rwsem);
1292fca65b4aSSage Weil 	if (dirty)
1293fca65b4aSSage Weil 		__mark_inode_dirty(inode, dirty);
1294f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
1295355da1ebSSage Weil 	return err;
1296355da1ebSSage Weil 
1297355da1ebSSage Weil do_sync:
1298be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
12993adf654dSSage Weil do_sync_unlocked:
1300604d1b02SYan, Zheng 	if (lock_snap_rwsem)
1301604d1b02SYan, Zheng 		up_read(&mdsc->snap_rwsem);
1302315f2408SYan, Zheng 
1303315f2408SYan, Zheng 	/* security module set xattr while filling trace */
1304d37b1d99SMarkus Elfring 	if (current->journal_info) {
1305*38d46409SXiubo Li 		pr_warn_ratelimited_client(cl,
1306*38d46409SXiubo Li 				"sync %p %llx.%llx during filling trace\n",
1307*38d46409SXiubo Li 				inode, ceph_vinop(inode));
1308315f2408SYan, Zheng 		err = -EBUSY;
1309315f2408SYan, Zheng 	} else {
1310a26feccaSAndreas Gruenbacher 		err = ceph_sync_setxattr(inode, name, value, size, flags);
1311f1919826SYan, Zheng 		if (err >= 0 && check_realm) {
1312f1919826SYan, Zheng 			/* check if snaprealm was created for quota inode */
1313f1919826SYan, Zheng 			spin_lock(&ci->i_ceph_lock);
1314f1919826SYan, Zheng 			if ((ci->i_max_files || ci->i_max_bytes) &&
1315f1919826SYan, Zheng 			    !(ci->i_snap_realm &&
1316f1919826SYan, Zheng 			      ci->i_snap_realm->ino == ci->i_vino.ino))
1317f1919826SYan, Zheng 				err = -EOPNOTSUPP;
1318f1919826SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
1319f1919826SYan, Zheng 		}
1320315f2408SYan, Zheng 	}
1321355da1ebSSage Weil out:
1322f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
1323355da1ebSSage Weil 	kfree(newname);
1324355da1ebSSage Weil 	kfree(newval);
1325355da1ebSSage Weil 	kfree(xattr);
1326355da1ebSSage Weil 	return err;
1327355da1ebSSage Weil }
1328355da1ebSSage Weil 
ceph_get_xattr_handler(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)13292cdeb1e4SAndreas Gruenbacher static int ceph_get_xattr_handler(const struct xattr_handler *handler,
13302cdeb1e4SAndreas Gruenbacher 				  struct dentry *dentry, struct inode *inode,
13312cdeb1e4SAndreas Gruenbacher 				  const char *name, void *value, size_t size)
13327221fe4cSGuangliang Zhao {
13332cdeb1e4SAndreas Gruenbacher 	if (!ceph_is_valid_xattr(name))
13342cdeb1e4SAndreas Gruenbacher 		return -EOPNOTSUPP;
13352cdeb1e4SAndreas Gruenbacher 	return __ceph_getxattr(inode, name, value, size);
13367221fe4cSGuangliang Zhao }
1337315f2408SYan, Zheng 
ceph_set_xattr_handler(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)13382cdeb1e4SAndreas Gruenbacher static int ceph_set_xattr_handler(const struct xattr_handler *handler,
133939f60c1cSChristian Brauner 				  struct mnt_idmap *idmap,
134059301226SAl Viro 				  struct dentry *unused, struct inode *inode,
134159301226SAl Viro 				  const char *name, const void *value,
134259301226SAl Viro 				  size_t size, int flags)
13432cdeb1e4SAndreas Gruenbacher {
13442cdeb1e4SAndreas Gruenbacher 	if (!ceph_is_valid_xattr(name))
13452cdeb1e4SAndreas Gruenbacher 		return -EOPNOTSUPP;
134659301226SAl Viro 	return __ceph_setxattr(inode, name, value, size, flags);
13472cdeb1e4SAndreas Gruenbacher }
13482cdeb1e4SAndreas Gruenbacher 
13495130cceaSWei Yongjun static const struct xattr_handler ceph_other_xattr_handler = {
13502cdeb1e4SAndreas Gruenbacher 	.prefix = "",  /* match any name => handlers called with full name */
13512cdeb1e4SAndreas Gruenbacher 	.get = ceph_get_xattr_handler,
13522cdeb1e4SAndreas Gruenbacher 	.set = ceph_set_xattr_handler,
13532cdeb1e4SAndreas Gruenbacher };
13542cdeb1e4SAndreas Gruenbacher 
1355315f2408SYan, Zheng #ifdef CONFIG_SECURITY
ceph_security_xattr_wanted(struct inode * in)1356315f2408SYan, Zheng bool ceph_security_xattr_wanted(struct inode *in)
1357315f2408SYan, Zheng {
1358315f2408SYan, Zheng 	return in->i_security != NULL;
1359315f2408SYan, Zheng }
1360315f2408SYan, Zheng 
ceph_security_xattr_deadlock(struct inode * in)1361315f2408SYan, Zheng bool ceph_security_xattr_deadlock(struct inode *in)
1362315f2408SYan, Zheng {
1363315f2408SYan, Zheng 	struct ceph_inode_info *ci;
1364315f2408SYan, Zheng 	bool ret;
1365d37b1d99SMarkus Elfring 	if (!in->i_security)
1366315f2408SYan, Zheng 		return false;
1367315f2408SYan, Zheng 	ci = ceph_inode(in);
1368315f2408SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
1369315f2408SYan, Zheng 	ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1370315f2408SYan, Zheng 	      !(ci->i_xattrs.version > 0 &&
1371315f2408SYan, Zheng 		__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1372315f2408SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
1373315f2408SYan, Zheng 	return ret;
1374315f2408SYan, Zheng }
1375ac6713ccSYan, Zheng 
1376ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
ceph_security_init_secctx(struct dentry * dentry,umode_t mode,struct ceph_acl_sec_ctx * as_ctx)1377ac6713ccSYan, Zheng int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1378ac6713ccSYan, Zheng 			   struct ceph_acl_sec_ctx *as_ctx)
1379ac6713ccSYan, Zheng {
1380ac6713ccSYan, Zheng 	struct ceph_pagelist *pagelist = as_ctx->pagelist;
1381ac6713ccSYan, Zheng 	const char *name;
1382ac6713ccSYan, Zheng 	size_t name_len;
1383ac6713ccSYan, Zheng 	int err;
1384ac6713ccSYan, Zheng 
1385ac6713ccSYan, Zheng 	err = security_dentry_init_security(dentry, mode, &dentry->d_name,
138615bf3239SVivek Goyal 					    &name, &as_ctx->lsmctx);
1387ac6713ccSYan, Zheng 	if (err < 0) {
1388ac6713ccSYan, Zheng 		WARN_ON_ONCE(err != -EOPNOTSUPP);
1389ac6713ccSYan, Zheng 		err = 0; /* do nothing */
1390ac6713ccSYan, Zheng 		goto out;
1391ac6713ccSYan, Zheng 	}
1392ac6713ccSYan, Zheng 
1393ac6713ccSYan, Zheng 	err = -ENOMEM;
1394ac6713ccSYan, Zheng 	if (!pagelist) {
1395ac6713ccSYan, Zheng 		pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1396ac6713ccSYan, Zheng 		if (!pagelist)
1397ac6713ccSYan, Zheng 			goto out;
1398ac6713ccSYan, Zheng 		err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1399ac6713ccSYan, Zheng 		if (err)
1400ac6713ccSYan, Zheng 			goto out;
1401ac6713ccSYan, Zheng 		ceph_pagelist_encode_32(pagelist, 1);
1402ac6713ccSYan, Zheng 	}
1403ac6713ccSYan, Zheng 
1404ac6713ccSYan, Zheng 	/*
1405ac6713ccSYan, Zheng 	 * FIXME: Make security_dentry_init_security() generic. Currently
1406ac6713ccSYan, Zheng 	 * It only supports single security module and only selinux has
1407ac6713ccSYan, Zheng 	 * dentry_init_security hook.
1408ac6713ccSYan, Zheng 	 */
1409ac6713ccSYan, Zheng 	name_len = strlen(name);
1410ac6713ccSYan, Zheng 	err = ceph_pagelist_reserve(pagelist,
1411ac6713ccSYan, Zheng 				    4 * 2 + name_len + as_ctx->lsmctx.len);
1412ac6713ccSYan, Zheng 	if (err)
1413ac6713ccSYan, Zheng 		goto out;
1414ac6713ccSYan, Zheng 
1415ac6713ccSYan, Zheng 	if (as_ctx->pagelist) {
1416ac6713ccSYan, Zheng 		/* update count of KV pairs */
1417ac6713ccSYan, Zheng 		BUG_ON(pagelist->length <= sizeof(__le32));
1418ac6713ccSYan, Zheng 		if (list_is_singular(&pagelist->head)) {
1419ac6713ccSYan, Zheng 			le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1420ac6713ccSYan, Zheng 		} else {
1421ac6713ccSYan, Zheng 			struct page *page = list_first_entry(&pagelist->head,
1422ac6713ccSYan, Zheng 							     struct page, lru);
1423ac6713ccSYan, Zheng 			void *addr = kmap_atomic(page);
1424ac6713ccSYan, Zheng 			le32_add_cpu((__le32*)addr, 1);
1425ac6713ccSYan, Zheng 			kunmap_atomic(addr);
1426ac6713ccSYan, Zheng 		}
1427ac6713ccSYan, Zheng 	} else {
1428ac6713ccSYan, Zheng 		as_ctx->pagelist = pagelist;
1429ac6713ccSYan, Zheng 	}
1430ac6713ccSYan, Zheng 
1431ac6713ccSYan, Zheng 	ceph_pagelist_encode_32(pagelist, name_len);
1432ac6713ccSYan, Zheng 	ceph_pagelist_append(pagelist, name, name_len);
1433ac6713ccSYan, Zheng 
1434ac6713ccSYan, Zheng 	ceph_pagelist_encode_32(pagelist, as_ctx->lsmctx.len);
1435ac6713ccSYan, Zheng 	ceph_pagelist_append(pagelist, as_ctx->lsmctx.context,
1436ac6713ccSYan, Zheng 			     as_ctx->lsmctx.len);
1437ac6713ccSYan, Zheng 
1438ac6713ccSYan, Zheng 	err = 0;
1439ac6713ccSYan, Zheng out:
1440ac6713ccSYan, Zheng 	if (pagelist && !as_ctx->pagelist)
1441ac6713ccSYan, Zheng 		ceph_pagelist_release(pagelist);
1442ac6713ccSYan, Zheng 	return err;
1443ac6713ccSYan, Zheng }
1444668959a5SJeff Layton #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1445668959a5SJeff Layton #endif /* CONFIG_SECURITY */
14465c31e92dSYan, Zheng 
ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx * as_ctx)14475c31e92dSYan, Zheng void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
14485c31e92dSYan, Zheng {
14495c31e92dSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL
14505c31e92dSYan, Zheng 	posix_acl_release(as_ctx->acl);
14515c31e92dSYan, Zheng 	posix_acl_release(as_ctx->default_acl);
14525c31e92dSYan, Zheng #endif
1453ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1454ac6713ccSYan, Zheng 	security_release_secctx(&as_ctx->lsmctx);
1455ac6713ccSYan, Zheng #endif
14566b5717bdSJeff Layton #ifdef CONFIG_FS_ENCRYPTION
14576b5717bdSJeff Layton 	kfree(as_ctx->fscrypt_auth);
14586b5717bdSJeff Layton #endif
14595c31e92dSYan, Zheng 	if (as_ctx->pagelist)
14605c31e92dSYan, Zheng 		ceph_pagelist_release(as_ctx->pagelist);
14615c31e92dSYan, Zheng }
1462ac6713ccSYan, Zheng 
1463ac6713ccSYan, Zheng /*
1464ac6713ccSYan, Zheng  * List of handlers for synthetic system.* attributes. Other
1465ac6713ccSYan, Zheng  * attributes are handled directly.
1466ac6713ccSYan, Zheng  */
146710f9fbe9SWedson Almeida Filho const struct xattr_handler * const ceph_xattr_handlers[] = {
1468ac6713ccSYan, Zheng 	&ceph_other_xattr_handler,
1469ac6713ccSYan, Zheng 	NULL,
1470ac6713ccSYan, Zheng };
1471