xref: /openbmc/linux/fs/ceph/xattr.c (revision aa4066ed7ba60421423c35f66b789bb3dd21d89e)
13d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
23d14c5d2SYehuda Sadeh 
3355da1ebSSage Weil #include "super.h"
43d14c5d2SYehuda Sadeh #include "mds_client.h"
53d14c5d2SYehuda Sadeh 
63d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
7355da1ebSSage Weil 
8355da1ebSSage Weil #include <linux/xattr.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
10355da1ebSSage Weil 
1122891907SAlex Elder #define XATTR_CEPH_PREFIX "ceph."
1222891907SAlex Elder #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
1322891907SAlex Elder 
14355da1ebSSage Weil static bool ceph_is_valid_xattr(const char *name)
15355da1ebSSage Weil {
1622891907SAlex Elder 	return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
171a756278SSage Weil 	       !strncmp(name, XATTR_SECURITY_PREFIX,
18355da1ebSSage Weil 			XATTR_SECURITY_PREFIX_LEN) ||
19355da1ebSSage Weil 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
20355da1ebSSage Weil 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
21355da1ebSSage Weil }
22355da1ebSSage Weil 
23355da1ebSSage Weil /*
24355da1ebSSage Weil  * These define virtual xattrs exposing the recursive directory
25355da1ebSSage Weil  * statistics and layout metadata.
26355da1ebSSage Weil  */
27881a5fa2SAlex Elder struct ceph_vxattr {
28355da1ebSSage Weil 	char *name;
29355da1ebSSage Weil 	size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
30355da1ebSSage Weil 			      size_t size);
31eb788084SAlex Elder 	bool readonly;
32355da1ebSSage Weil };
33355da1ebSSage Weil 
34355da1ebSSage Weil /* directories */
35355da1ebSSage Weil 
36*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
37355da1ebSSage Weil 					size_t size)
38355da1ebSSage Weil {
39355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
40355da1ebSSage Weil }
41355da1ebSSage Weil 
42*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
43355da1ebSSage Weil 				      size_t size)
44355da1ebSSage Weil {
45355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_files);
46355da1ebSSage Weil }
47355da1ebSSage Weil 
48*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
49355da1ebSSage Weil 					size_t size)
50355da1ebSSage Weil {
51355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_subdirs);
52355da1ebSSage Weil }
53355da1ebSSage Weil 
54*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
55355da1ebSSage Weil 					 size_t size)
56355da1ebSSage Weil {
57355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
58355da1ebSSage Weil }
59355da1ebSSage Weil 
60*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
61355da1ebSSage Weil 				       size_t size)
62355da1ebSSage Weil {
63355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rfiles);
64355da1ebSSage Weil }
65355da1ebSSage Weil 
66*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
67355da1ebSSage Weil 					 size_t size)
68355da1ebSSage Weil {
69355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rsubdirs);
70355da1ebSSage Weil }
71355da1ebSSage Weil 
72*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
73355da1ebSSage Weil 				       size_t size)
74355da1ebSSage Weil {
75355da1ebSSage Weil 	return snprintf(val, size, "%lld", ci->i_rbytes);
76355da1ebSSage Weil }
77355da1ebSSage Weil 
78*aa4066edSAlex Elder static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
79355da1ebSSage Weil 				       size_t size)
80355da1ebSSage Weil {
81355da1ebSSage Weil 	return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
82355da1ebSSage Weil 			(long)ci->i_rctime.tv_nsec);
83355da1ebSSage Weil }
84355da1ebSSage Weil 
85eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
86eb788084SAlex Elder 
87eb788084SAlex Elder #define XATTR_NAME_CEPH(_type, _name) \
88eb788084SAlex Elder 		{ \
89eb788084SAlex Elder 			.name = CEPH_XATTR_NAME(_type, _name), \
90*aa4066edSAlex Elder 			.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
91eb788084SAlex Elder 			.readonly = true, \
92eb788084SAlex Elder 		}
93eb788084SAlex Elder 
94881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = {
95eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, entries),
96eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, files),
97eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, subdirs),
98eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, rentries),
99eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, rfiles),
100eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, rsubdirs),
101eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, rbytes),
102eb788084SAlex Elder 	XATTR_NAME_CEPH(dir, rctime),
103eb788084SAlex Elder 	{ 0 }	/* Required table terminator */
104355da1ebSSage Weil };
105355da1ebSSage Weil 
106355da1ebSSage Weil /* files */
107355da1ebSSage Weil 
108*aa4066edSAlex Elder static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
109355da1ebSSage Weil 				   size_t size)
110355da1ebSSage Weil {
111b195befdSSage Weil 	int ret;
112b195befdSSage Weil 
113b195befdSSage Weil 	ret = snprintf(val, size,
114355da1ebSSage Weil 		"chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
115355da1ebSSage Weil 		(unsigned long long)ceph_file_layout_su(ci->i_layout),
116355da1ebSSage Weil 		(unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
117355da1ebSSage Weil 		(unsigned long long)ceph_file_layout_object_size(ci->i_layout));
118b195befdSSage Weil 	if (ceph_file_layout_pg_preferred(ci->i_layout))
119b195befdSSage Weil 		ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
120b195befdSSage Weil 			    (unsigned long long)ceph_file_layout_pg_preferred(
121b195befdSSage Weil 				    ci->i_layout));
122b195befdSSage Weil 	return ret;
123355da1ebSSage Weil }
124355da1ebSSage Weil 
125881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = {
126eb788084SAlex Elder 	XATTR_NAME_CEPH(file, layout),
127114fc474SAlex Elder 	/* The following extended attribute name is deprecated */
128eb788084SAlex Elder 	{
129eb788084SAlex Elder 		.name = XATTR_CEPH_PREFIX "layout",
130*aa4066edSAlex Elder 		.getxattr_cb = ceph_vxattrcb_file_layout,
131eb788084SAlex Elder 		.readonly = true,
132eb788084SAlex Elder 	},
133eb788084SAlex Elder 	{ 0 }	/* Required table terminator */
134355da1ebSSage Weil };
135355da1ebSSage Weil 
136881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
137355da1ebSSage Weil {
138355da1ebSSage Weil 	if (S_ISDIR(inode->i_mode))
139355da1ebSSage Weil 		return ceph_dir_vxattrs;
140355da1ebSSage Weil 	else if (S_ISREG(inode->i_mode))
141355da1ebSSage Weil 		return ceph_file_vxattrs;
142355da1ebSSage Weil 	return NULL;
143355da1ebSSage Weil }
144355da1ebSSage Weil 
145881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
146355da1ebSSage Weil 						const char *name)
147355da1ebSSage Weil {
148881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
14906476a69SAlex Elder 
15006476a69SAlex Elder 	if (vxattr) {
15106476a69SAlex Elder 		while (vxattr->name) {
15206476a69SAlex Elder 			if (!strcmp(vxattr->name, name))
153355da1ebSSage Weil 				return vxattr;
154355da1ebSSage Weil 			vxattr++;
15506476a69SAlex Elder 		}
15606476a69SAlex Elder 	}
15706476a69SAlex Elder 
158355da1ebSSage Weil 	return NULL;
159355da1ebSSage Weil }
160355da1ebSSage Weil 
161355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci,
162355da1ebSSage Weil 			   const char *name, int name_len,
163355da1ebSSage Weil 			   const char *val, int val_len,
164355da1ebSSage Weil 			   int dirty,
165355da1ebSSage Weil 			   int should_free_name, int should_free_val,
166355da1ebSSage Weil 			   struct ceph_inode_xattr **newxattr)
167355da1ebSSage Weil {
168355da1ebSSage Weil 	struct rb_node **p;
169355da1ebSSage Weil 	struct rb_node *parent = NULL;
170355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
171355da1ebSSage Weil 	int c;
172355da1ebSSage Weil 	int new = 0;
173355da1ebSSage Weil 
174355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
175355da1ebSSage Weil 	while (*p) {
176355da1ebSSage Weil 		parent = *p;
177355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
178355da1ebSSage Weil 		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
179355da1ebSSage Weil 		if (c < 0)
180355da1ebSSage Weil 			p = &(*p)->rb_left;
181355da1ebSSage Weil 		else if (c > 0)
182355da1ebSSage Weil 			p = &(*p)->rb_right;
183355da1ebSSage Weil 		else {
184355da1ebSSage Weil 			if (name_len == xattr->name_len)
185355da1ebSSage Weil 				break;
186355da1ebSSage Weil 			else if (name_len < xattr->name_len)
187355da1ebSSage Weil 				p = &(*p)->rb_left;
188355da1ebSSage Weil 			else
189355da1ebSSage Weil 				p = &(*p)->rb_right;
190355da1ebSSage Weil 		}
191355da1ebSSage Weil 		xattr = NULL;
192355da1ebSSage Weil 	}
193355da1ebSSage Weil 
194355da1ebSSage Weil 	if (!xattr) {
195355da1ebSSage Weil 		new = 1;
196355da1ebSSage Weil 		xattr = *newxattr;
197355da1ebSSage Weil 		xattr->name = name;
198355da1ebSSage Weil 		xattr->name_len = name_len;
199355da1ebSSage Weil 		xattr->should_free_name = should_free_name;
200355da1ebSSage Weil 
201355da1ebSSage Weil 		ci->i_xattrs.count++;
202355da1ebSSage Weil 		dout("__set_xattr count=%d\n", ci->i_xattrs.count);
203355da1ebSSage Weil 	} else {
204355da1ebSSage Weil 		kfree(*newxattr);
205355da1ebSSage Weil 		*newxattr = NULL;
206355da1ebSSage Weil 		if (xattr->should_free_val)
207355da1ebSSage Weil 			kfree((void *)xattr->val);
208355da1ebSSage Weil 
209355da1ebSSage Weil 		if (should_free_name) {
210355da1ebSSage Weil 			kfree((void *)name);
211355da1ebSSage Weil 			name = xattr->name;
212355da1ebSSage Weil 		}
213355da1ebSSage Weil 		ci->i_xattrs.names_size -= xattr->name_len;
214355da1ebSSage Weil 		ci->i_xattrs.vals_size -= xattr->val_len;
215355da1ebSSage Weil 	}
216355da1ebSSage Weil 	ci->i_xattrs.names_size += name_len;
217355da1ebSSage Weil 	ci->i_xattrs.vals_size += val_len;
218355da1ebSSage Weil 	if (val)
219355da1ebSSage Weil 		xattr->val = val;
220355da1ebSSage Weil 	else
221355da1ebSSage Weil 		xattr->val = "";
222355da1ebSSage Weil 
223355da1ebSSage Weil 	xattr->val_len = val_len;
224355da1ebSSage Weil 	xattr->dirty = dirty;
225355da1ebSSage Weil 	xattr->should_free_val = (val && should_free_val);
226355da1ebSSage Weil 
227355da1ebSSage Weil 	if (new) {
228355da1ebSSage Weil 		rb_link_node(&xattr->node, parent, p);
229355da1ebSSage Weil 		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
230355da1ebSSage Weil 		dout("__set_xattr_val p=%p\n", p);
231355da1ebSSage Weil 	}
232355da1ebSSage Weil 
233355da1ebSSage Weil 	dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
234355da1ebSSage Weil 	     ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
235355da1ebSSage Weil 
236355da1ebSSage Weil 	return 0;
237355da1ebSSage Weil }
238355da1ebSSage Weil 
239355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
240355da1ebSSage Weil 			   const char *name)
241355da1ebSSage Weil {
242355da1ebSSage Weil 	struct rb_node **p;
243355da1ebSSage Weil 	struct rb_node *parent = NULL;
244355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
24517db143fSSage Weil 	int name_len = strlen(name);
246355da1ebSSage Weil 	int c;
247355da1ebSSage Weil 
248355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
249355da1ebSSage Weil 	while (*p) {
250355da1ebSSage Weil 		parent = *p;
251355da1ebSSage Weil 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
252355da1ebSSage Weil 		c = strncmp(name, xattr->name, xattr->name_len);
25317db143fSSage Weil 		if (c == 0 && name_len > xattr->name_len)
25417db143fSSage Weil 			c = 1;
255355da1ebSSage Weil 		if (c < 0)
256355da1ebSSage Weil 			p = &(*p)->rb_left;
257355da1ebSSage Weil 		else if (c > 0)
258355da1ebSSage Weil 			p = &(*p)->rb_right;
259355da1ebSSage Weil 		else {
260355da1ebSSage Weil 			dout("__get_xattr %s: found %.*s\n", name,
261355da1ebSSage Weil 			     xattr->val_len, xattr->val);
262355da1ebSSage Weil 			return xattr;
263355da1ebSSage Weil 		}
264355da1ebSSage Weil 	}
265355da1ebSSage Weil 
266355da1ebSSage Weil 	dout("__get_xattr %s: not found\n", name);
267355da1ebSSage Weil 
268355da1ebSSage Weil 	return NULL;
269355da1ebSSage Weil }
270355da1ebSSage Weil 
271355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr)
272355da1ebSSage Weil {
273355da1ebSSage Weil 	BUG_ON(!xattr);
274355da1ebSSage Weil 
275355da1ebSSage Weil 	if (xattr->should_free_name)
276355da1ebSSage Weil 		kfree((void *)xattr->name);
277355da1ebSSage Weil 	if (xattr->should_free_val)
278355da1ebSSage Weil 		kfree((void *)xattr->val);
279355da1ebSSage Weil 
280355da1ebSSage Weil 	kfree(xattr);
281355da1ebSSage Weil }
282355da1ebSSage Weil 
283355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci,
284355da1ebSSage Weil 			  struct ceph_inode_xattr *xattr)
285355da1ebSSage Weil {
286355da1ebSSage Weil 	if (!xattr)
287355da1ebSSage Weil 		return -EOPNOTSUPP;
288355da1ebSSage Weil 
289355da1ebSSage Weil 	rb_erase(&xattr->node, &ci->i_xattrs.index);
290355da1ebSSage Weil 
291355da1ebSSage Weil 	if (xattr->should_free_name)
292355da1ebSSage Weil 		kfree((void *)xattr->name);
293355da1ebSSage Weil 	if (xattr->should_free_val)
294355da1ebSSage Weil 		kfree((void *)xattr->val);
295355da1ebSSage Weil 
296355da1ebSSage Weil 	ci->i_xattrs.names_size -= xattr->name_len;
297355da1ebSSage Weil 	ci->i_xattrs.vals_size -= xattr->val_len;
298355da1ebSSage Weil 	ci->i_xattrs.count--;
299355da1ebSSage Weil 	kfree(xattr);
300355da1ebSSage Weil 
301355da1ebSSage Weil 	return 0;
302355da1ebSSage Weil }
303355da1ebSSage Weil 
304355da1ebSSage Weil static int __remove_xattr_by_name(struct ceph_inode_info *ci,
305355da1ebSSage Weil 			   const char *name)
306355da1ebSSage Weil {
307355da1ebSSage Weil 	struct rb_node **p;
308355da1ebSSage Weil 	struct ceph_inode_xattr *xattr;
309355da1ebSSage Weil 	int err;
310355da1ebSSage Weil 
311355da1ebSSage Weil 	p = &ci->i_xattrs.index.rb_node;
312355da1ebSSage Weil 	xattr = __get_xattr(ci, name);
313355da1ebSSage Weil 	err = __remove_xattr(ci, xattr);
314355da1ebSSage Weil 	return err;
315355da1ebSSage Weil }
316355da1ebSSage Weil 
317355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci,
318355da1ebSSage Weil 				char *dest)
319355da1ebSSage Weil {
320355da1ebSSage Weil 	struct rb_node *p;
321355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
322355da1ebSSage Weil 
323355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
324355da1ebSSage Weil 	dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
325355da1ebSSage Weil 
326355da1ebSSage Weil 	while (p) {
327355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
328355da1ebSSage Weil 		memcpy(dest, xattr->name, xattr->name_len);
329355da1ebSSage Weil 		dest[xattr->name_len] = '\0';
330355da1ebSSage Weil 
331355da1ebSSage Weil 		dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
332355da1ebSSage Weil 		     xattr->name_len, ci->i_xattrs.names_size);
333355da1ebSSage Weil 
334355da1ebSSage Weil 		dest += xattr->name_len + 1;
335355da1ebSSage Weil 		p = rb_next(p);
336355da1ebSSage Weil 	}
337355da1ebSSage Weil 
338355da1ebSSage Weil 	return dest;
339355da1ebSSage Weil }
340355da1ebSSage Weil 
341355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
342355da1ebSSage Weil {
343355da1ebSSage Weil 	struct rb_node *p, *tmp;
344355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
345355da1ebSSage Weil 
346355da1ebSSage Weil 	p = rb_first(&ci->i_xattrs.index);
347355da1ebSSage Weil 
348355da1ebSSage Weil 	dout("__ceph_destroy_xattrs p=%p\n", p);
349355da1ebSSage Weil 
350355da1ebSSage Weil 	while (p) {
351355da1ebSSage Weil 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
352355da1ebSSage Weil 		tmp = p;
353355da1ebSSage Weil 		p = rb_next(tmp);
354355da1ebSSage Weil 		dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
355355da1ebSSage Weil 		     xattr->name_len, xattr->name);
356355da1ebSSage Weil 		rb_erase(tmp, &ci->i_xattrs.index);
357355da1ebSSage Weil 
358355da1ebSSage Weil 		__free_xattr(xattr);
359355da1ebSSage Weil 	}
360355da1ebSSage Weil 
361355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
362355da1ebSSage Weil 	ci->i_xattrs.vals_size = 0;
363355da1ebSSage Weil 	ci->i_xattrs.index_version = 0;
364355da1ebSSage Weil 	ci->i_xattrs.count = 0;
365355da1ebSSage Weil 	ci->i_xattrs.index = RB_ROOT;
366355da1ebSSage Weil }
367355da1ebSSage Weil 
368355da1ebSSage Weil static int __build_xattrs(struct inode *inode)
369be655596SSage Weil 	__releases(ci->i_ceph_lock)
370be655596SSage Weil 	__acquires(ci->i_ceph_lock)
371355da1ebSSage Weil {
372355da1ebSSage Weil 	u32 namelen;
373355da1ebSSage Weil 	u32 numattr = 0;
374355da1ebSSage Weil 	void *p, *end;
375355da1ebSSage Weil 	u32 len;
376355da1ebSSage Weil 	const char *name, *val;
377355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
378355da1ebSSage Weil 	int xattr_version;
379355da1ebSSage Weil 	struct ceph_inode_xattr **xattrs = NULL;
38063ff78b2SSage Weil 	int err = 0;
381355da1ebSSage Weil 	int i;
382355da1ebSSage Weil 
383355da1ebSSage Weil 	dout("__build_xattrs() len=%d\n",
384355da1ebSSage Weil 	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
385355da1ebSSage Weil 
386355da1ebSSage Weil 	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
387355da1ebSSage Weil 		return 0; /* already built */
388355da1ebSSage Weil 
389355da1ebSSage Weil 	__ceph_destroy_xattrs(ci);
390355da1ebSSage Weil 
391355da1ebSSage Weil start:
392355da1ebSSage Weil 	/* updated internal xattr rb tree */
393355da1ebSSage Weil 	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
394355da1ebSSage Weil 		p = ci->i_xattrs.blob->vec.iov_base;
395355da1ebSSage Weil 		end = p + ci->i_xattrs.blob->vec.iov_len;
396355da1ebSSage Weil 		ceph_decode_32_safe(&p, end, numattr, bad);
397355da1ebSSage Weil 		xattr_version = ci->i_xattrs.version;
398be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
399355da1ebSSage Weil 
400355da1ebSSage Weil 		xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
401355da1ebSSage Weil 				 GFP_NOFS);
402355da1ebSSage Weil 		err = -ENOMEM;
403355da1ebSSage Weil 		if (!xattrs)
404355da1ebSSage Weil 			goto bad_lock;
405355da1ebSSage Weil 		memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
406355da1ebSSage Weil 		for (i = 0; i < numattr; i++) {
407355da1ebSSage Weil 			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
408355da1ebSSage Weil 					    GFP_NOFS);
409355da1ebSSage Weil 			if (!xattrs[i])
410355da1ebSSage Weil 				goto bad_lock;
411355da1ebSSage Weil 		}
412355da1ebSSage Weil 
413be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
414355da1ebSSage Weil 		if (ci->i_xattrs.version != xattr_version) {
415355da1ebSSage Weil 			/* lost a race, retry */
416355da1ebSSage Weil 			for (i = 0; i < numattr; i++)
417355da1ebSSage Weil 				kfree(xattrs[i]);
418355da1ebSSage Weil 			kfree(xattrs);
419355da1ebSSage Weil 			goto start;
420355da1ebSSage Weil 		}
421355da1ebSSage Weil 		err = -EIO;
422355da1ebSSage Weil 		while (numattr--) {
423355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
424355da1ebSSage Weil 			namelen = len;
425355da1ebSSage Weil 			name = p;
426355da1ebSSage Weil 			p += len;
427355da1ebSSage Weil 			ceph_decode_32_safe(&p, end, len, bad);
428355da1ebSSage Weil 			val = p;
429355da1ebSSage Weil 			p += len;
430355da1ebSSage Weil 
431355da1ebSSage Weil 			err = __set_xattr(ci, name, namelen, val, len,
432355da1ebSSage Weil 					  0, 0, 0, &xattrs[numattr]);
433355da1ebSSage Weil 
434355da1ebSSage Weil 			if (err < 0)
435355da1ebSSage Weil 				goto bad;
436355da1ebSSage Weil 		}
437355da1ebSSage Weil 		kfree(xattrs);
438355da1ebSSage Weil 	}
439355da1ebSSage Weil 	ci->i_xattrs.index_version = ci->i_xattrs.version;
440355da1ebSSage Weil 	ci->i_xattrs.dirty = false;
441355da1ebSSage Weil 
442355da1ebSSage Weil 	return err;
443355da1ebSSage Weil bad_lock:
444be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
445355da1ebSSage Weil bad:
446355da1ebSSage Weil 	if (xattrs) {
447355da1ebSSage Weil 		for (i = 0; i < numattr; i++)
448355da1ebSSage Weil 			kfree(xattrs[i]);
449355da1ebSSage Weil 		kfree(xattrs);
450355da1ebSSage Weil 	}
451355da1ebSSage Weil 	ci->i_xattrs.names_size = 0;
452355da1ebSSage Weil 	return err;
453355da1ebSSage Weil }
454355da1ebSSage Weil 
455355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
456355da1ebSSage Weil 				    int val_size)
457355da1ebSSage Weil {
458355da1ebSSage Weil 	/*
459355da1ebSSage Weil 	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
460355da1ebSSage Weil 	 * 4 bytes per each value
461355da1ebSSage Weil 	 */
462355da1ebSSage Weil 	int size = 4 + ci->i_xattrs.count*(4 + 4) +
463355da1ebSSage Weil 			     ci->i_xattrs.names_size +
464355da1ebSSage Weil 			     ci->i_xattrs.vals_size;
465355da1ebSSage Weil 	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
466355da1ebSSage Weil 	     ci->i_xattrs.count, ci->i_xattrs.names_size,
467355da1ebSSage Weil 	     ci->i_xattrs.vals_size);
468355da1ebSSage Weil 
469355da1ebSSage Weil 	if (name_size)
470355da1ebSSage Weil 		size += 4 + 4 + name_size + val_size;
471355da1ebSSage Weil 
472355da1ebSSage Weil 	return size;
473355da1ebSSage Weil }
474355da1ebSSage Weil 
475355da1ebSSage Weil /*
476355da1ebSSage Weil  * If there are dirty xattrs, reencode xattrs into the prealloc_blob
477355da1ebSSage Weil  * and swap into place.
478355da1ebSSage Weil  */
479355da1ebSSage Weil void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
480355da1ebSSage Weil {
481355da1ebSSage Weil 	struct rb_node *p;
482355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
483355da1ebSSage Weil 	void *dest;
484355da1ebSSage Weil 
485355da1ebSSage Weil 	dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
486355da1ebSSage Weil 	if (ci->i_xattrs.dirty) {
487355da1ebSSage Weil 		int need = __get_required_blob_size(ci, 0, 0);
488355da1ebSSage Weil 
489355da1ebSSage Weil 		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
490355da1ebSSage Weil 
491355da1ebSSage Weil 		p = rb_first(&ci->i_xattrs.index);
492355da1ebSSage Weil 		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
493355da1ebSSage Weil 
494355da1ebSSage Weil 		ceph_encode_32(&dest, ci->i_xattrs.count);
495355da1ebSSage Weil 		while (p) {
496355da1ebSSage Weil 			xattr = rb_entry(p, struct ceph_inode_xattr, node);
497355da1ebSSage Weil 
498355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->name_len);
499355da1ebSSage Weil 			memcpy(dest, xattr->name, xattr->name_len);
500355da1ebSSage Weil 			dest += xattr->name_len;
501355da1ebSSage Weil 			ceph_encode_32(&dest, xattr->val_len);
502355da1ebSSage Weil 			memcpy(dest, xattr->val, xattr->val_len);
503355da1ebSSage Weil 			dest += xattr->val_len;
504355da1ebSSage Weil 
505355da1ebSSage Weil 			p = rb_next(p);
506355da1ebSSage Weil 		}
507355da1ebSSage Weil 
508355da1ebSSage Weil 		/* adjust buffer len; it may be larger than we need */
509355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob->vec.iov_len =
510355da1ebSSage Weil 			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
511355da1ebSSage Weil 
512b6c1d5b8SSage Weil 		if (ci->i_xattrs.blob)
513355da1ebSSage Weil 			ceph_buffer_put(ci->i_xattrs.blob);
514355da1ebSSage Weil 		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
515355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = NULL;
516355da1ebSSage Weil 		ci->i_xattrs.dirty = false;
5174a625be4SSage Weil 		ci->i_xattrs.version++;
518355da1ebSSage Weil 	}
519355da1ebSSage Weil }
520355da1ebSSage Weil 
521355da1ebSSage Weil ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
522355da1ebSSage Weil 		      size_t size)
523355da1ebSSage Weil {
524355da1ebSSage Weil 	struct inode *inode = dentry->d_inode;
525355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
526355da1ebSSage Weil 	int err;
527355da1ebSSage Weil 	struct ceph_inode_xattr *xattr;
528881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr = NULL;
529355da1ebSSage Weil 
530355da1ebSSage Weil 	if (!ceph_is_valid_xattr(name))
531355da1ebSSage Weil 		return -ENODATA;
532355da1ebSSage Weil 
533355da1ebSSage Weil 	/* let's see if a virtual xattr was requested */
53406476a69SAlex Elder 	vxattr = ceph_match_vxattr(inode, name);
535355da1ebSSage Weil 
536be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
537355da1ebSSage Weil 	dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
538355da1ebSSage Weil 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
539355da1ebSSage Weil 
540355da1ebSSage Weil 	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
541355da1ebSSage Weil 	    (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
542355da1ebSSage Weil 		goto get_xattr;
543355da1ebSSage Weil 	} else {
544be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
545355da1ebSSage Weil 		/* get xattrs from mds (if we don't already have them) */
546355da1ebSSage Weil 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
547355da1ebSSage Weil 		if (err)
548355da1ebSSage Weil 			return err;
549355da1ebSSage Weil 	}
550355da1ebSSage Weil 
551be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
552355da1ebSSage Weil 
553355da1ebSSage Weil 	if (vxattr && vxattr->readonly) {
554355da1ebSSage Weil 		err = vxattr->getxattr_cb(ci, value, size);
555355da1ebSSage Weil 		goto out;
556355da1ebSSage Weil 	}
557355da1ebSSage Weil 
558355da1ebSSage Weil 	err = __build_xattrs(inode);
559355da1ebSSage Weil 	if (err < 0)
560355da1ebSSage Weil 		goto out;
561355da1ebSSage Weil 
562355da1ebSSage Weil get_xattr:
563355da1ebSSage Weil 	err = -ENODATA;  /* == ENOATTR */
564355da1ebSSage Weil 	xattr = __get_xattr(ci, name);
565355da1ebSSage Weil 	if (!xattr) {
566355da1ebSSage Weil 		if (vxattr)
567355da1ebSSage Weil 			err = vxattr->getxattr_cb(ci, value, size);
568355da1ebSSage Weil 		goto out;
569355da1ebSSage Weil 	}
570355da1ebSSage Weil 
571355da1ebSSage Weil 	err = -ERANGE;
572355da1ebSSage Weil 	if (size && size < xattr->val_len)
573355da1ebSSage Weil 		goto out;
574355da1ebSSage Weil 
575355da1ebSSage Weil 	err = xattr->val_len;
576355da1ebSSage Weil 	if (size == 0)
577355da1ebSSage Weil 		goto out;
578355da1ebSSage Weil 
579355da1ebSSage Weil 	memcpy(value, xattr->val, xattr->val_len);
580355da1ebSSage Weil 
581355da1ebSSage Weil out:
582be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
583355da1ebSSage Weil 	return err;
584355da1ebSSage Weil }
585355da1ebSSage Weil 
586355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
587355da1ebSSage Weil {
588355da1ebSSage Weil 	struct inode *inode = dentry->d_inode;
589355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
590881a5fa2SAlex Elder 	struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
591355da1ebSSage Weil 	u32 vir_namelen = 0;
592355da1ebSSage Weil 	u32 namelen;
593355da1ebSSage Weil 	int err;
594355da1ebSSage Weil 	u32 len;
595355da1ebSSage Weil 	int i;
596355da1ebSSage Weil 
597be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
598355da1ebSSage Weil 	dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
599355da1ebSSage Weil 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
600355da1ebSSage Weil 
601355da1ebSSage Weil 	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
602bddfa3ccSHenry C Chang 	    (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
603355da1ebSSage Weil 		goto list_xattr;
604355da1ebSSage Weil 	} else {
605be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
606355da1ebSSage Weil 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
607355da1ebSSage Weil 		if (err)
608355da1ebSSage Weil 			return err;
609355da1ebSSage Weil 	}
610355da1ebSSage Weil 
611be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
612355da1ebSSage Weil 
613355da1ebSSage Weil 	err = __build_xattrs(inode);
614355da1ebSSage Weil 	if (err < 0)
615355da1ebSSage Weil 		goto out;
616355da1ebSSage Weil 
617355da1ebSSage Weil list_xattr:
618355da1ebSSage Weil 	vir_namelen = 0;
619355da1ebSSage Weil 	/* include virtual dir xattrs */
620355da1ebSSage Weil 	if (vxattrs)
621355da1ebSSage Weil 		for (i = 0; vxattrs[i].name; i++)
622355da1ebSSage Weil 			vir_namelen += strlen(vxattrs[i].name) + 1;
623355da1ebSSage Weil 	/* adding 1 byte per each variable due to the null termination */
624355da1ebSSage Weil 	namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
625355da1ebSSage Weil 	err = -ERANGE;
626355da1ebSSage Weil 	if (size && namelen > size)
627355da1ebSSage Weil 		goto out;
628355da1ebSSage Weil 
629355da1ebSSage Weil 	err = namelen;
630355da1ebSSage Weil 	if (size == 0)
631355da1ebSSage Weil 		goto out;
632355da1ebSSage Weil 
633355da1ebSSage Weil 	names = __copy_xattr_names(ci, names);
634355da1ebSSage Weil 
635355da1ebSSage Weil 	/* virtual xattr names, too */
636355da1ebSSage Weil 	if (vxattrs)
637355da1ebSSage Weil 		for (i = 0; vxattrs[i].name; i++) {
638355da1ebSSage Weil 			len = sprintf(names, "%s", vxattrs[i].name);
639355da1ebSSage Weil 			names += len + 1;
640355da1ebSSage Weil 		}
641355da1ebSSage Weil 
642355da1ebSSage Weil out:
643be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
644355da1ebSSage Weil 	return err;
645355da1ebSSage Weil }
646355da1ebSSage Weil 
647355da1ebSSage Weil static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
648355da1ebSSage Weil 			      const char *value, size_t size, int flags)
649355da1ebSSage Weil {
6503d14c5d2SYehuda Sadeh 	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
651355da1ebSSage Weil 	struct inode *inode = dentry->d_inode;
652355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
6535f21c96dSSage Weil 	struct inode *parent_inode;
654355da1ebSSage Weil 	struct ceph_mds_request *req;
6553d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
656355da1ebSSage Weil 	int err;
657355da1ebSSage Weil 	int i, nr_pages;
658355da1ebSSage Weil 	struct page **pages = NULL;
659355da1ebSSage Weil 	void *kaddr;
660355da1ebSSage Weil 
661355da1ebSSage Weil 	/* copy value into some pages */
662355da1ebSSage Weil 	nr_pages = calc_pages_for(0, size);
663355da1ebSSage Weil 	if (nr_pages) {
664355da1ebSSage Weil 		pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
665355da1ebSSage Weil 		if (!pages)
666355da1ebSSage Weil 			return -ENOMEM;
667355da1ebSSage Weil 		err = -ENOMEM;
668355da1ebSSage Weil 		for (i = 0; i < nr_pages; i++) {
66931459fe4SYehuda Sadeh 			pages[i] = __page_cache_alloc(GFP_NOFS);
670355da1ebSSage Weil 			if (!pages[i]) {
671355da1ebSSage Weil 				nr_pages = i;
672355da1ebSSage Weil 				goto out;
673355da1ebSSage Weil 			}
674355da1ebSSage Weil 			kaddr = kmap(pages[i]);
675355da1ebSSage Weil 			memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
676355da1ebSSage Weil 			       min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
677355da1ebSSage Weil 		}
678355da1ebSSage Weil 	}
679355da1ebSSage Weil 
680355da1ebSSage Weil 	dout("setxattr value=%.*s\n", (int)size, value);
681355da1ebSSage Weil 
682355da1ebSSage Weil 	/* do request */
683355da1ebSSage Weil 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
684355da1ebSSage Weil 				       USE_AUTH_MDS);
68560d87733SJulia Lawall 	if (IS_ERR(req)) {
68660d87733SJulia Lawall 		err = PTR_ERR(req);
68760d87733SJulia Lawall 		goto out;
68860d87733SJulia Lawall 	}
68970b666c3SSage Weil 	req->r_inode = inode;
69070b666c3SSage Weil 	ihold(inode);
691355da1ebSSage Weil 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
692355da1ebSSage Weil 	req->r_num_caps = 1;
693355da1ebSSage Weil 	req->r_args.setxattr.flags = cpu_to_le32(flags);
694355da1ebSSage Weil 	req->r_path2 = kstrdup(name, GFP_NOFS);
695355da1ebSSage Weil 
696355da1ebSSage Weil 	req->r_pages = pages;
697355da1ebSSage Weil 	req->r_num_pages = nr_pages;
698355da1ebSSage Weil 	req->r_data_len = size;
699355da1ebSSage Weil 
700355da1ebSSage Weil 	dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
7015f21c96dSSage Weil 	parent_inode = ceph_get_dentry_parent_inode(dentry);
702355da1ebSSage Weil 	err = ceph_mdsc_do_request(mdsc, parent_inode, req);
7035f21c96dSSage Weil 	iput(parent_inode);
704355da1ebSSage Weil 	ceph_mdsc_put_request(req);
705355da1ebSSage Weil 	dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
706355da1ebSSage Weil 
707355da1ebSSage Weil out:
708355da1ebSSage Weil 	if (pages) {
709355da1ebSSage Weil 		for (i = 0; i < nr_pages; i++)
710355da1ebSSage Weil 			__free_page(pages[i]);
711355da1ebSSage Weil 		kfree(pages);
712355da1ebSSage Weil 	}
713355da1ebSSage Weil 	return err;
714355da1ebSSage Weil }
715355da1ebSSage Weil 
716355da1ebSSage Weil int ceph_setxattr(struct dentry *dentry, const char *name,
717355da1ebSSage Weil 		  const void *value, size_t size, int flags)
718355da1ebSSage Weil {
719355da1ebSSage Weil 	struct inode *inode = dentry->d_inode;
720881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr;
721355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
722355da1ebSSage Weil 	int err;
723355da1ebSSage Weil 	int name_len = strlen(name);
724355da1ebSSage Weil 	int val_len = size;
725355da1ebSSage Weil 	char *newname = NULL;
726355da1ebSSage Weil 	char *newval = NULL;
727355da1ebSSage Weil 	struct ceph_inode_xattr *xattr = NULL;
728355da1ebSSage Weil 	int issued;
729355da1ebSSage Weil 	int required_blob_size;
730fca65b4aSSage Weil 	int dirty;
731355da1ebSSage Weil 
732355da1ebSSage Weil 	if (ceph_snap(inode) != CEPH_NOSNAP)
733355da1ebSSage Weil 		return -EROFS;
734355da1ebSSage Weil 
735355da1ebSSage Weil 	if (!ceph_is_valid_xattr(name))
736355da1ebSSage Weil 		return -EOPNOTSUPP;
737355da1ebSSage Weil 
73806476a69SAlex Elder 	vxattr = ceph_match_vxattr(inode, name);
739355da1ebSSage Weil 	if (vxattr && vxattr->readonly)
740355da1ebSSage Weil 		return -EOPNOTSUPP;
741355da1ebSSage Weil 
742355da1ebSSage Weil 	/* preallocate memory for xattr name, value, index node */
743355da1ebSSage Weil 	err = -ENOMEM;
74461413c2fSJulia Lawall 	newname = kmemdup(name, name_len + 1, GFP_NOFS);
745355da1ebSSage Weil 	if (!newname)
746355da1ebSSage Weil 		goto out;
747355da1ebSSage Weil 
748355da1ebSSage Weil 	if (val_len) {
749b829c195SAlex Elder 		newval = kmemdup(value, val_len, GFP_NOFS);
750355da1ebSSage Weil 		if (!newval)
751355da1ebSSage Weil 			goto out;
752355da1ebSSage Weil 	}
753355da1ebSSage Weil 
754355da1ebSSage Weil 	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
755355da1ebSSage Weil 	if (!xattr)
756355da1ebSSage Weil 		goto out;
757355da1ebSSage Weil 
758be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
759355da1ebSSage Weil retry:
760355da1ebSSage Weil 	issued = __ceph_caps_issued(ci, NULL);
761355da1ebSSage Weil 	if (!(issued & CEPH_CAP_XATTR_EXCL))
762355da1ebSSage Weil 		goto do_sync;
763355da1ebSSage Weil 	__build_xattrs(inode);
764355da1ebSSage Weil 
765355da1ebSSage Weil 	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
766355da1ebSSage Weil 
767355da1ebSSage Weil 	if (!ci->i_xattrs.prealloc_blob ||
768355da1ebSSage Weil 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
769355da1ebSSage Weil 		struct ceph_buffer *blob = NULL;
770355da1ebSSage Weil 
771be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
772355da1ebSSage Weil 		dout(" preaallocating new blob size=%d\n", required_blob_size);
773b6c1d5b8SSage Weil 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
774355da1ebSSage Weil 		if (!blob)
775355da1ebSSage Weil 			goto out;
776be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
777b6c1d5b8SSage Weil 		if (ci->i_xattrs.prealloc_blob)
778355da1ebSSage Weil 			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
779355da1ebSSage Weil 		ci->i_xattrs.prealloc_blob = blob;
780355da1ebSSage Weil 		goto retry;
781355da1ebSSage Weil 	}
782355da1ebSSage Weil 
783355da1ebSSage Weil 	dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
784355da1ebSSage Weil 	err = __set_xattr(ci, newname, name_len, newval,
785355da1ebSSage Weil 			  val_len, 1, 1, 1, &xattr);
786fca65b4aSSage Weil 	dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
787355da1ebSSage Weil 	ci->i_xattrs.dirty = true;
788355da1ebSSage Weil 	inode->i_ctime = CURRENT_TIME;
789be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
790fca65b4aSSage Weil 	if (dirty)
791fca65b4aSSage Weil 		__mark_inode_dirty(inode, dirty);
792355da1ebSSage Weil 	return err;
793355da1ebSSage Weil 
794355da1ebSSage Weil do_sync:
795be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
796355da1ebSSage Weil 	err = ceph_sync_setxattr(dentry, name, value, size, flags);
797355da1ebSSage Weil out:
798355da1ebSSage Weil 	kfree(newname);
799355da1ebSSage Weil 	kfree(newval);
800355da1ebSSage Weil 	kfree(xattr);
801355da1ebSSage Weil 	return err;
802355da1ebSSage Weil }
803355da1ebSSage Weil 
804355da1ebSSage Weil static int ceph_send_removexattr(struct dentry *dentry, const char *name)
805355da1ebSSage Weil {
8063d14c5d2SYehuda Sadeh 	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
8073d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
808355da1ebSSage Weil 	struct inode *inode = dentry->d_inode;
8095f21c96dSSage Weil 	struct inode *parent_inode;
810355da1ebSSage Weil 	struct ceph_mds_request *req;
811355da1ebSSage Weil 	int err;
812355da1ebSSage Weil 
813355da1ebSSage Weil 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
814355da1ebSSage Weil 				       USE_AUTH_MDS);
815355da1ebSSage Weil 	if (IS_ERR(req))
816355da1ebSSage Weil 		return PTR_ERR(req);
81770b666c3SSage Weil 	req->r_inode = inode;
81870b666c3SSage Weil 	ihold(inode);
819355da1ebSSage Weil 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
820355da1ebSSage Weil 	req->r_num_caps = 1;
821355da1ebSSage Weil 	req->r_path2 = kstrdup(name, GFP_NOFS);
822355da1ebSSage Weil 
8235f21c96dSSage Weil 	parent_inode = ceph_get_dentry_parent_inode(dentry);
824355da1ebSSage Weil 	err = ceph_mdsc_do_request(mdsc, parent_inode, req);
8255f21c96dSSage Weil 	iput(parent_inode);
826355da1ebSSage Weil 	ceph_mdsc_put_request(req);
827355da1ebSSage Weil 	return err;
828355da1ebSSage Weil }
829355da1ebSSage Weil 
830355da1ebSSage Weil int ceph_removexattr(struct dentry *dentry, const char *name)
831355da1ebSSage Weil {
832355da1ebSSage Weil 	struct inode *inode = dentry->d_inode;
833881a5fa2SAlex Elder 	struct ceph_vxattr *vxattr;
834355da1ebSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
835355da1ebSSage Weil 	int issued;
836355da1ebSSage Weil 	int err;
83783eb26afSAlex Elder 	int required_blob_size;
838fca65b4aSSage Weil 	int dirty;
839355da1ebSSage Weil 
840355da1ebSSage Weil 	if (ceph_snap(inode) != CEPH_NOSNAP)
841355da1ebSSage Weil 		return -EROFS;
842355da1ebSSage Weil 
843355da1ebSSage Weil 	if (!ceph_is_valid_xattr(name))
844355da1ebSSage Weil 		return -EOPNOTSUPP;
845355da1ebSSage Weil 
84606476a69SAlex Elder 	vxattr = ceph_match_vxattr(inode, name);
847355da1ebSSage Weil 	if (vxattr && vxattr->readonly)
848355da1ebSSage Weil 		return -EOPNOTSUPP;
849355da1ebSSage Weil 
85083eb26afSAlex Elder 	err = -ENOMEM;
851be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
852355da1ebSSage Weil 	__build_xattrs(inode);
85383eb26afSAlex Elder retry:
854355da1ebSSage Weil 	issued = __ceph_caps_issued(ci, NULL);
855355da1ebSSage Weil 	dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
856355da1ebSSage Weil 
857355da1ebSSage Weil 	if (!(issued & CEPH_CAP_XATTR_EXCL))
858355da1ebSSage Weil 		goto do_sync;
859355da1ebSSage Weil 
86083eb26afSAlex Elder 	required_blob_size = __get_required_blob_size(ci, 0, 0);
86183eb26afSAlex Elder 
86283eb26afSAlex Elder 	if (!ci->i_xattrs.prealloc_blob ||
86383eb26afSAlex Elder 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
86483eb26afSAlex Elder 		struct ceph_buffer *blob;
86583eb26afSAlex Elder 
86683eb26afSAlex Elder 		spin_unlock(&ci->i_ceph_lock);
86783eb26afSAlex Elder 		dout(" preaallocating new blob size=%d\n", required_blob_size);
86883eb26afSAlex Elder 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
86983eb26afSAlex Elder 		if (!blob)
87083eb26afSAlex Elder 			goto out;
87183eb26afSAlex Elder 		spin_lock(&ci->i_ceph_lock);
87283eb26afSAlex Elder 		if (ci->i_xattrs.prealloc_blob)
87383eb26afSAlex Elder 			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
87483eb26afSAlex Elder 		ci->i_xattrs.prealloc_blob = blob;
87583eb26afSAlex Elder 		goto retry;
87683eb26afSAlex Elder 	}
87783eb26afSAlex Elder 
878355da1ebSSage Weil 	err = __remove_xattr_by_name(ceph_inode(inode), name);
879fca65b4aSSage Weil 	dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
880355da1ebSSage Weil 	ci->i_xattrs.dirty = true;
881355da1ebSSage Weil 	inode->i_ctime = CURRENT_TIME;
882355da1ebSSage Weil 
883be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
884fca65b4aSSage Weil 	if (dirty)
885fca65b4aSSage Weil 		__mark_inode_dirty(inode, dirty);
886355da1ebSSage Weil 	return err;
887355da1ebSSage Weil do_sync:
888be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
889355da1ebSSage Weil 	err = ceph_send_removexattr(dentry, name);
89083eb26afSAlex Elder out:
891355da1ebSSage Weil 	return err;
892355da1ebSSage Weil }
893355da1ebSSage Weil 
894