Blame view

fs/ceph/xattr.c 32.7 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
2
  #include <linux/ceph/ceph_debug.h>
25e6bae35   Yan, Zheng   ceph: use pagelis...
3
  #include <linux/ceph/pagelist.h>
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
4

355da1eb7   Sage Weil   ceph: inode opera...
5
  #include "super.h"
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
6
7
8
  #include "mds_client.h"
  
  #include <linux/ceph/decode.h>
355da1eb7   Sage Weil   ceph: inode opera...
9
10
  
  #include <linux/xattr.h>
ac6713ccb   Yan, Zheng   ceph: add selinux...
11
  #include <linux/security.h>
4db658ea0   Linus Torvalds   ceph: Fix up afte...
12
  #include <linux/posix_acl_xattr.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
13
  #include <linux/slab.h>
355da1eb7   Sage Weil   ceph: inode opera...
14

228919071   Alex Elder   ceph: use a symbo...
15
16
  #define XATTR_CEPH_PREFIX "ceph."
  #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
bcdfeb2eb   Yan, Zheng   ceph: remove xatt...
17
18
  static int __remove_xattr(struct ceph_inode_info *ci,
  			  struct ceph_inode_xattr *xattr);
355da1eb7   Sage Weil   ceph: inode opera...
19
20
  static bool ceph_is_valid_xattr(const char *name)
  {
b8fe918b0   Jeff Layton   ceph: allow arbit...
21
22
  	return !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  	       !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
355da1eb7   Sage Weil   ceph: inode opera...
23
24
25
26
27
28
29
30
  	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  }
  
  /*
   * These define virtual xattrs exposing the recursive directory
   * statistics and layout metadata.
   */
881a5fa20   Alex Elder   ceph: drop "_cb" ...
31
  struct ceph_vxattr {
355da1eb7   Sage Weil   ceph: inode opera...
32
  	char *name;
3ce6cd123   Alex Elder   ceph: avoid repea...
33
  	size_t name_size;	/* strlen(name) + 1 (for '\0') */
f1d1b51de   Jeff Layton   ceph: make getxat...
34
35
  	ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
  			       size_t size);
f36e44729   Sage Weil   ceph: add exists_...
36
  	bool (*exists_cb)(struct ceph_inode_info *ci);
4e9906e79   Yan, Zheng   ceph: use bit fla...
37
  	unsigned int flags;
355da1eb7   Sage Weil   ceph: inode opera...
38
  };
4e9906e79   Yan, Zheng   ceph: use bit fla...
39
40
  #define VXATTR_FLAG_READONLY		(1<<0)
  #define VXATTR_FLAG_HIDDEN		(1<<1)
49a9f4f67   Yan, Zheng   ceph: always get ...
41
  #define VXATTR_FLAG_RSTAT		(1<<2)
4e9906e79   Yan, Zheng   ceph: use bit fla...
42

32ab0bd78   Sage Weil   ceph: change ceph...
43
44
45
46
  /* layouts */
  
  static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
  {
779fe0fb8   Yan, Zheng   ceph: rados pool ...
47
48
49
50
  	struct ceph_file_layout *fl = &ci->i_layout;
  	return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
  		fl->object_size > 0 || fl->pool_id >= 0 ||
  		rcu_dereference_raw(fl->pool_ns) != NULL);
32ab0bd78   Sage Weil   ceph: change ceph...
51
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
52
53
  static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
  				    size_t size)
32ab0bd78   Sage Weil   ceph: change ceph...
54
  {
32ab0bd78   Sage Weil   ceph: change ceph...
55
56
  	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
  	struct ceph_osd_client *osdc = &fsc->client->osdc;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
57
  	struct ceph_string *pool_ns;
7627151ea   Yan, Zheng   libceph: define n...
58
  	s64 pool = ci->i_layout.pool_id;
32ab0bd78   Sage Weil   ceph: change ceph...
59
  	const char *pool_name;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
60
  	const char *ns_field = " pool_namespace=";
1e5c6649f   Yan, Zheng   ceph: check buffe...
61
  	char buf[128];
779fe0fb8   Yan, Zheng   ceph: rados pool ...
62
  	size_t len, total_len = 0;
3b421018f   Jeff Layton   ceph: return -ERA...
63
  	ssize_t ret;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
64
65
  
  	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
32ab0bd78   Sage Weil   ceph: change ceph...
66
67
68
  
  	dout("ceph_vxattrcb_layout %p
  ", &ci->vfs_inode);
5aea3dcd5   Ilya Dryomov   libceph: a major ...
69
  	down_read(&osdc->lock);
32ab0bd78   Sage Weil   ceph: change ceph...
70
  	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
1e5c6649f   Yan, Zheng   ceph: check buffe...
71
  	if (pool_name) {
779fe0fb8   Yan, Zheng   ceph: rados pool ...
72
  		len = snprintf(buf, sizeof(buf),
7627151ea   Yan, Zheng   libceph: define n...
73
74
75
  		"stripe_unit=%u stripe_count=%u object_size=%u pool=",
  		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
  	        ci->i_layout.object_size);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
76
  		total_len = len + strlen(pool_name);
1e5c6649f   Yan, Zheng   ceph: check buffe...
77
  	} else {
779fe0fb8   Yan, Zheng   ceph: rados pool ...
78
  		len = snprintf(buf, sizeof(buf),
7627151ea   Yan, Zheng   libceph: define n...
79
80
  		"stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
  		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
f1d1b51de   Jeff Layton   ceph: make getxat...
81
  		ci->i_layout.object_size, pool);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
82
83
84
85
86
  		total_len = len;
  	}
  
  	if (pool_ns)
  		total_len += strlen(ns_field) + pool_ns->len;
3b421018f   Jeff Layton   ceph: return -ERA...
87
88
  	ret = total_len;
  	if (size >= total_len) {
779fe0fb8   Yan, Zheng   ceph: rados pool ...
89
90
91
92
93
94
95
96
97
98
99
100
101
  		memcpy(val, buf, len);
  		ret = len;
  		if (pool_name) {
  			len = strlen(pool_name);
  			memcpy(val + ret, pool_name, len);
  			ret += len;
  		}
  		if (pool_ns) {
  			len = strlen(ns_field);
  			memcpy(val + ret, ns_field, len);
  			ret += len;
  			memcpy(val + ret, pool_ns->str, pool_ns->len);
  			ret += pool_ns->len;
1e5c6649f   Yan, Zheng   ceph: check buffe...
102
103
  		}
  	}
5aea3dcd5   Ilya Dryomov   libceph: a major ...
104
  	up_read(&osdc->lock);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
105
  	ceph_put_string(pool_ns);
32ab0bd78   Sage Weil   ceph: change ceph...
106
107
  	return ret;
  }
26350535c   Jeff Layton   ceph: don't NULL ...
108
109
110
111
112
113
  /*
   * The convention with strings in xattrs is that they should not be NULL
   * terminated, since we're returning the length with them. snprintf always
   * NULL terminates however, so call it on a temporary buffer and then memcpy
   * the result into place.
   */
f6fbdcd99   Ilya Dryomov   ceph: mark ceph_f...
114
115
  static __printf(3, 4)
  int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
26350535c   Jeff Layton   ceph: don't NULL ...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  {
  	int ret;
  	va_list args;
  	char buf[96]; /* NB: reevaluate size if new vxattrs are added */
  
  	va_start(args, fmt);
  	ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
  	va_end(args);
  
  	/* Sanity check */
  	if (size && ret + 1 > sizeof(buf)) {
  		WARN_ONCE(true, "Returned length too big (%d)", ret);
  		return -E2BIG;
  	}
  
  	if (ret <= size)
  		memcpy(val, buf, ret);
  	return ret;
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
135
136
  static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
  						char *val, size_t size)
695b71193   Sage Weil   ceph: implement h...
137
  {
26350535c   Jeff Layton   ceph: don't NULL ...
138
  	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
695b71193   Sage Weil   ceph: implement h...
139
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
140
141
  static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
  						 char *val, size_t size)
695b71193   Sage Weil   ceph: implement h...
142
  {
26350535c   Jeff Layton   ceph: don't NULL ...
143
  	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
695b71193   Sage Weil   ceph: implement h...
144
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
145
146
  static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
  						char *val, size_t size)
695b71193   Sage Weil   ceph: implement h...
147
  {
26350535c   Jeff Layton   ceph: don't NULL ...
148
  	return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
695b71193   Sage Weil   ceph: implement h...
149
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
150
151
  static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
  					 char *val, size_t size)
695b71193   Sage Weil   ceph: implement h...
152
  {
f1d1b51de   Jeff Layton   ceph: make getxat...
153
  	ssize_t ret;
695b71193   Sage Weil   ceph: implement h...
154
155
  	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
  	struct ceph_osd_client *osdc = &fsc->client->osdc;
7627151ea   Yan, Zheng   libceph: define n...
156
  	s64 pool = ci->i_layout.pool_id;
695b71193   Sage Weil   ceph: implement h...
157
  	const char *pool_name;
5aea3dcd5   Ilya Dryomov   libceph: a major ...
158
  	down_read(&osdc->lock);
695b71193   Sage Weil   ceph: implement h...
159
  	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
26350535c   Jeff Layton   ceph: don't NULL ...
160
161
162
163
164
165
166
  	if (pool_name) {
  		ret = strlen(pool_name);
  		if (ret <= size)
  			memcpy(val, pool_name, ret);
  	} else {
  		ret = ceph_fmt_xattr(val, size, "%lld", pool);
  	}
5aea3dcd5   Ilya Dryomov   libceph: a major ...
167
  	up_read(&osdc->lock);
695b71193   Sage Weil   ceph: implement h...
168
169
  	return ret;
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
170
171
  static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
  						   char *val, size_t size)
779fe0fb8   Yan, Zheng   ceph: rados pool ...
172
  {
26350535c   Jeff Layton   ceph: don't NULL ...
173
  	ssize_t ret = 0;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
174
  	struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
26350535c   Jeff Layton   ceph: don't NULL ...
175

779fe0fb8   Yan, Zheng   ceph: rados pool ...
176
  	if (ns) {
26350535c   Jeff Layton   ceph: don't NULL ...
177
178
179
  		ret = ns->len;
  		if (ret <= size)
  			memcpy(val, ns->str, ret);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
180
181
182
183
  		ceph_put_string(ns);
  	}
  	return ret;
  }
355da1eb7   Sage Weil   ceph: inode opera...
184
  /* directories */
f1d1b51de   Jeff Layton   ceph: make getxat...
185
186
  static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
  					 size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
187
  {
26350535c   Jeff Layton   ceph: don't NULL ...
188
  	return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
355da1eb7   Sage Weil   ceph: inode opera...
189
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
190
191
  static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
  				       size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
192
  {
26350535c   Jeff Layton   ceph: don't NULL ...
193
  	return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
355da1eb7   Sage Weil   ceph: inode opera...
194
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
195
196
  static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
  					 size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
197
  {
26350535c   Jeff Layton   ceph: don't NULL ...
198
  	return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
355da1eb7   Sage Weil   ceph: inode opera...
199
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
200
201
  static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
  					  size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
202
  {
26350535c   Jeff Layton   ceph: don't NULL ...
203
204
  	return ceph_fmt_xattr(val, size, "%lld",
  				ci->i_rfiles + ci->i_rsubdirs);
355da1eb7   Sage Weil   ceph: inode opera...
205
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
206
207
  static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
  					size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
208
  {
26350535c   Jeff Layton   ceph: don't NULL ...
209
  	return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
355da1eb7   Sage Weil   ceph: inode opera...
210
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
211
212
  static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
  					  size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
213
  {
26350535c   Jeff Layton   ceph: don't NULL ...
214
  	return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
355da1eb7   Sage Weil   ceph: inode opera...
215
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
216
217
  static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
  					size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
218
  {
26350535c   Jeff Layton   ceph: don't NULL ...
219
  	return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
355da1eb7   Sage Weil   ceph: inode opera...
220
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
221
222
  static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
  					size_t size)
355da1eb7   Sage Weil   ceph: inode opera...
223
  {
26350535c   Jeff Layton   ceph: don't NULL ...
224
225
  	return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
  				ci->i_rctime.tv_nsec);
355da1eb7   Sage Weil   ceph: inode opera...
226
  }
08796873a   Yan, Zheng   ceph: support get...
227
228
229
230
231
  /* dir pin */
  static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
  {
  	return ci->i_dir_pin != -ENODATA;
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
232
233
  static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
  				     size_t size)
08796873a   Yan, Zheng   ceph: support get...
234
  {
26350535c   Jeff Layton   ceph: don't NULL ...
235
  	return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
08796873a   Yan, Zheng   ceph: support get...
236
  }
fb18a5756   Luis Henriques   ceph: quota: add ...
237

08796873a   Yan, Zheng   ceph: support get...
238
  /* quotas */
fb18a5756   Luis Henriques   ceph: quota: add ...
239
240
  static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
  {
f19198268   Yan, Zheng   ceph: check if md...
241
242
243
244
245
246
247
248
249
  	bool ret = false;
  	spin_lock(&ci->i_ceph_lock);
  	if ((ci->i_max_files || ci->i_max_bytes) &&
  	    ci->i_vino.snap == CEPH_NOSNAP &&
  	    ci->i_snap_realm &&
  	    ci->i_snap_realm->ino == ci->i_vino.ino)
  		ret = true;
  	spin_unlock(&ci->i_ceph_lock);
  	return ret;
fb18a5756   Luis Henriques   ceph: quota: add ...
250
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
251
252
  static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
  				   size_t size)
fb18a5756   Luis Henriques   ceph: quota: add ...
253
  {
26350535c   Jeff Layton   ceph: don't NULL ...
254
255
  	return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
  				ci->i_max_bytes, ci->i_max_files);
fb18a5756   Luis Henriques   ceph: quota: add ...
256
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
257
258
  static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
  					     char *val, size_t size)
fb18a5756   Luis Henriques   ceph: quota: add ...
259
  {
26350535c   Jeff Layton   ceph: don't NULL ...
260
  	return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
fb18a5756   Luis Henriques   ceph: quota: add ...
261
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
262
263
  static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
  					     char *val, size_t size)
fb18a5756   Luis Henriques   ceph: quota: add ...
264
  {
26350535c   Jeff Layton   ceph: don't NULL ...
265
  	return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
fb18a5756   Luis Henriques   ceph: quota: add ...
266
  }
32ab0bd78   Sage Weil   ceph: change ceph...
267

100cc610a   David Disseldorp   ceph: add ceph.sn...
268
269
270
271
272
  /* snapshots */
  static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
  {
  	return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
  }
f1d1b51de   Jeff Layton   ceph: make getxat...
273
274
  static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
  					size_t size)
100cc610a   David Disseldorp   ceph: add ceph.sn...
275
  {
26350535c   Jeff Layton   ceph: don't NULL ...
276
277
  	return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
  				ci->i_snap_btime.tv_nsec);
100cc610a   David Disseldorp   ceph: add ceph.sn...
278
  }
eb7880844   Alex Elder   ceph: use macros ...
279
  #define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
695b71193   Sage Weil   ceph: implement h...
280
281
  #define CEPH_XATTR_NAME2(_type, _name, _name2)	\
  	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
eb7880844   Alex Elder   ceph: use macros ...
282

49a9f4f67   Yan, Zheng   ceph: always get ...
283
  #define XATTR_NAME_CEPH(_type, _name, _flags)				\
8860147a0   Sage Weil   ceph: support hid...
284
285
286
287
  	{								\
  		.name = CEPH_XATTR_NAME(_type, _name),			\
  		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
  		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
49a9f4f67   Yan, Zheng   ceph: always get ...
288
289
  		.exists_cb = NULL,					\
  		.flags = (VXATTR_FLAG_READONLY | _flags),		\
8860147a0   Sage Weil   ceph: support hid...
290
  	}
49a9f4f67   Yan, Zheng   ceph: always get ...
291
292
  #define XATTR_RSTAT_FIELD(_type, _name)			\
  	XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
695b71193   Sage Weil   ceph: implement h...
293
294
295
296
297
  #define XATTR_LAYOUT_FIELD(_type, _name, _field)			\
  	{								\
  		.name = CEPH_XATTR_NAME2(_type, _name, _field),	\
  		.name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
  		.getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
695b71193   Sage Weil   ceph: implement h...
298
  		.exists_cb = ceph_vxattrcb_layout_exists,	\
4e9906e79   Yan, Zheng   ceph: use bit fla...
299
  		.flags = VXATTR_FLAG_HIDDEN,			\
695b71193   Sage Weil   ceph: implement h...
300
  	}
fb18a5756   Luis Henriques   ceph: quota: add ...
301
302
303
304
305
  #define XATTR_QUOTA_FIELD(_type, _name)					\
  	{								\
  		.name = CEPH_XATTR_NAME(_type, _name),			\
  		.name_size = sizeof(CEPH_XATTR_NAME(_type, _name)),	\
  		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name,	\
fb18a5756   Luis Henriques   ceph: quota: add ...
306
  		.exists_cb = ceph_vxattrcb_quota_exists,		\
4e9906e79   Yan, Zheng   ceph: use bit fla...
307
  		.flags = VXATTR_FLAG_HIDDEN,				\
fb18a5756   Luis Henriques   ceph: quota: add ...
308
  	}
eb7880844   Alex Elder   ceph: use macros ...
309

881a5fa20   Alex Elder   ceph: drop "_cb" ...
310
  static struct ceph_vxattr ceph_dir_vxattrs[] = {
1f08f2b05   Sage Weil   ceph: add ceph.di...
311
312
313
314
  	{
  		.name = "ceph.dir.layout",
  		.name_size = sizeof("ceph.dir.layout"),
  		.getxattr_cb = ceph_vxattrcb_layout,
1f08f2b05   Sage Weil   ceph: add ceph.di...
315
  		.exists_cb = ceph_vxattrcb_layout_exists,
4e9906e79   Yan, Zheng   ceph: use bit fla...
316
  		.flags = VXATTR_FLAG_HIDDEN,
1f08f2b05   Sage Weil   ceph: add ceph.di...
317
  	},
695b71193   Sage Weil   ceph: implement h...
318
319
320
321
  	XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
  	XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
  	XATTR_LAYOUT_FIELD(dir, layout, object_size),
  	XATTR_LAYOUT_FIELD(dir, layout, pool),
779fe0fb8   Yan, Zheng   ceph: rados pool ...
322
  	XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
49a9f4f67   Yan, Zheng   ceph: always get ...
323
324
325
326
327
328
329
330
  	XATTR_NAME_CEPH(dir, entries, 0),
  	XATTR_NAME_CEPH(dir, files, 0),
  	XATTR_NAME_CEPH(dir, subdirs, 0),
  	XATTR_RSTAT_FIELD(dir, rentries),
  	XATTR_RSTAT_FIELD(dir, rfiles),
  	XATTR_RSTAT_FIELD(dir, rsubdirs),
  	XATTR_RSTAT_FIELD(dir, rbytes),
  	XATTR_RSTAT_FIELD(dir, rctime),
fb18a5756   Luis Henriques   ceph: quota: add ...
331
  	{
08796873a   Yan, Zheng   ceph: support get...
332
  		.name = "ceph.dir.pin",
e1b814391   David Disseldorp   ceph: clean up ce...
333
  		.name_size = sizeof("ceph.dir.pin"),
08796873a   Yan, Zheng   ceph: support get...
334
335
336
337
338
  		.getxattr_cb = ceph_vxattrcb_dir_pin,
  		.exists_cb = ceph_vxattrcb_dir_pin_exists,
  		.flags = VXATTR_FLAG_HIDDEN,
  	},
  	{
fb18a5756   Luis Henriques   ceph: quota: add ...
339
340
341
  		.name = "ceph.quota",
  		.name_size = sizeof("ceph.quota"),
  		.getxattr_cb = ceph_vxattrcb_quota,
fb18a5756   Luis Henriques   ceph: quota: add ...
342
  		.exists_cb = ceph_vxattrcb_quota_exists,
4e9906e79   Yan, Zheng   ceph: use bit fla...
343
  		.flags = VXATTR_FLAG_HIDDEN,
fb18a5756   Luis Henriques   ceph: quota: add ...
344
345
346
  	},
  	XATTR_QUOTA_FIELD(quota, max_bytes),
  	XATTR_QUOTA_FIELD(quota, max_files),
100cc610a   David Disseldorp   ceph: add ceph.sn...
347
348
349
350
351
352
353
  	{
  		.name = "ceph.snap.btime",
  		.name_size = sizeof("ceph.snap.btime"),
  		.getxattr_cb = ceph_vxattrcb_snap_btime,
  		.exists_cb = ceph_vxattrcb_snap_btime_exists,
  		.flags = VXATTR_FLAG_READONLY,
  	},
2c3dd4ff5   Alex Elder   ceph: eliminate s...
354
  	{ .name = NULL, 0 }	/* Required table terminator */
355da1eb7   Sage Weil   ceph: inode opera...
355
356
357
  };
  
  /* files */
881a5fa20   Alex Elder   ceph: drop "_cb" ...
358
  static struct ceph_vxattr ceph_file_vxattrs[] = {
32ab0bd78   Sage Weil   ceph: change ceph...
359
360
361
362
  	{
  		.name = "ceph.file.layout",
  		.name_size = sizeof("ceph.file.layout"),
  		.getxattr_cb = ceph_vxattrcb_layout,
32ab0bd78   Sage Weil   ceph: change ceph...
363
  		.exists_cb = ceph_vxattrcb_layout_exists,
4e9906e79   Yan, Zheng   ceph: use bit fla...
364
  		.flags = VXATTR_FLAG_HIDDEN,
32ab0bd78   Sage Weil   ceph: change ceph...
365
  	},
695b71193   Sage Weil   ceph: implement h...
366
367
368
369
  	XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
  	XATTR_LAYOUT_FIELD(file, layout, stripe_count),
  	XATTR_LAYOUT_FIELD(file, layout, object_size),
  	XATTR_LAYOUT_FIELD(file, layout, pool),
779fe0fb8   Yan, Zheng   ceph: rados pool ...
370
  	XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
100cc610a   David Disseldorp   ceph: add ceph.sn...
371
372
373
374
375
376
377
  	{
  		.name = "ceph.snap.btime",
  		.name_size = sizeof("ceph.snap.btime"),
  		.getxattr_cb = ceph_vxattrcb_snap_btime,
  		.exists_cb = ceph_vxattrcb_snap_btime_exists,
  		.flags = VXATTR_FLAG_READONLY,
  	},
2c3dd4ff5   Alex Elder   ceph: eliminate s...
378
  	{ .name = NULL, 0 }	/* Required table terminator */
355da1eb7   Sage Weil   ceph: inode opera...
379
  };
881a5fa20   Alex Elder   ceph: drop "_cb" ...
380
  static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
355da1eb7   Sage Weil   ceph: inode opera...
381
382
383
384
385
386
387
  {
  	if (S_ISDIR(inode->i_mode))
  		return ceph_dir_vxattrs;
  	else if (S_ISREG(inode->i_mode))
  		return ceph_file_vxattrs;
  	return NULL;
  }
881a5fa20   Alex Elder   ceph: drop "_cb" ...
388
  static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
355da1eb7   Sage Weil   ceph: inode opera...
389
390
  						const char *name)
  {
881a5fa20   Alex Elder   ceph: drop "_cb" ...
391
  	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
06476a69d   Alex Elder   ceph: pass inode ...
392
393
394
395
396
397
398
399
  
  	if (vxattr) {
  		while (vxattr->name) {
  			if (!strcmp(vxattr->name, name))
  				return vxattr;
  			vxattr++;
  		}
  	}
355da1eb7   Sage Weil   ceph: inode opera...
400
401
402
403
404
405
  	return NULL;
  }
  
  static int __set_xattr(struct ceph_inode_info *ci,
  			   const char *name, int name_len,
  			   const char *val, int val_len,
fbc0b970d   Yan, Zheng   ceph: properly ha...
406
  			   int flags, int update_xattr,
355da1eb7   Sage Weil   ceph: inode opera...
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
  			   struct ceph_inode_xattr **newxattr)
  {
  	struct rb_node **p;
  	struct rb_node *parent = NULL;
  	struct ceph_inode_xattr *xattr = NULL;
  	int c;
  	int new = 0;
  
  	p = &ci->i_xattrs.index.rb_node;
  	while (*p) {
  		parent = *p;
  		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
  		if (c < 0)
  			p = &(*p)->rb_left;
  		else if (c > 0)
  			p = &(*p)->rb_right;
  		else {
  			if (name_len == xattr->name_len)
  				break;
  			else if (name_len < xattr->name_len)
  				p = &(*p)->rb_left;
  			else
  				p = &(*p)->rb_right;
  		}
  		xattr = NULL;
  	}
fbc0b970d   Yan, Zheng   ceph: properly ha...
434
435
  	if (update_xattr) {
  		int err = 0;
eeca958dc   Luis Henriques   ceph: fix memory ...
436

fbc0b970d   Yan, Zheng   ceph: properly ha...
437
438
439
440
441
442
443
  		if (xattr && (flags & XATTR_CREATE))
  			err = -EEXIST;
  		else if (!xattr && (flags & XATTR_REPLACE))
  			err = -ENODATA;
  		if (err) {
  			kfree(name);
  			kfree(val);
eeca958dc   Luis Henriques   ceph: fix memory ...
444
  			kfree(*newxattr);
fbc0b970d   Yan, Zheng   ceph: properly ha...
445
446
  			return err;
  		}
bcdfeb2eb   Yan, Zheng   ceph: remove xatt...
447
448
449
450
  		if (update_xattr < 0) {
  			if (xattr)
  				__remove_xattr(ci, xattr);
  			kfree(name);
eeca958dc   Luis Henriques   ceph: fix memory ...
451
  			kfree(*newxattr);
bcdfeb2eb   Yan, Zheng   ceph: remove xatt...
452
453
  			return 0;
  		}
fbc0b970d   Yan, Zheng   ceph: properly ha...
454
  	}
355da1eb7   Sage Weil   ceph: inode opera...
455
456
457
458
459
  	if (!xattr) {
  		new = 1;
  		xattr = *newxattr;
  		xattr->name = name;
  		xattr->name_len = name_len;
fbc0b970d   Yan, Zheng   ceph: properly ha...
460
  		xattr->should_free_name = update_xattr;
355da1eb7   Sage Weil   ceph: inode opera...
461
462
463
464
465
466
467
468
  
  		ci->i_xattrs.count++;
  		dout("__set_xattr count=%d
  ", ci->i_xattrs.count);
  	} else {
  		kfree(*newxattr);
  		*newxattr = NULL;
  		if (xattr->should_free_val)
c00e4522a   Xu Wang   ceph: remove unne...
469
  			kfree(xattr->val);
355da1eb7   Sage Weil   ceph: inode opera...
470

fbc0b970d   Yan, Zheng   ceph: properly ha...
471
  		if (update_xattr) {
c00e4522a   Xu Wang   ceph: remove unne...
472
  			kfree(name);
355da1eb7   Sage Weil   ceph: inode opera...
473
474
475
476
477
  			name = xattr->name;
  		}
  		ci->i_xattrs.names_size -= xattr->name_len;
  		ci->i_xattrs.vals_size -= xattr->val_len;
  	}
355da1eb7   Sage Weil   ceph: inode opera...
478
479
480
481
482
483
484
485
  	ci->i_xattrs.names_size += name_len;
  	ci->i_xattrs.vals_size += val_len;
  	if (val)
  		xattr->val = val;
  	else
  		xattr->val = "";
  
  	xattr->val_len = val_len;
fbc0b970d   Yan, Zheng   ceph: properly ha...
486
487
  	xattr->dirty = update_xattr;
  	xattr->should_free_val = (val && update_xattr);
355da1eb7   Sage Weil   ceph: inode opera...
488
489
490
491
492
493
494
  
  	if (new) {
  		rb_link_node(&xattr->node, parent, p);
  		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
  		dout("__set_xattr_val p=%p
  ", p);
  	}
057297812   Yan, Zheng   ceph: fix debug p...
495
496
497
  	dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s
  ",
  	     ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
355da1eb7   Sage Weil   ceph: inode opera...
498
499
500
501
502
503
504
505
506
507
  
  	return 0;
  }
  
  static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
  			   const char *name)
  {
  	struct rb_node **p;
  	struct rb_node *parent = NULL;
  	struct ceph_inode_xattr *xattr = NULL;
17db143fc   Sage Weil   ceph: fix xattr r...
508
  	int name_len = strlen(name);
355da1eb7   Sage Weil   ceph: inode opera...
509
510
511
512
513
514
515
  	int c;
  
  	p = &ci->i_xattrs.index.rb_node;
  	while (*p) {
  		parent = *p;
  		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  		c = strncmp(name, xattr->name, xattr->name_len);
17db143fc   Sage Weil   ceph: fix xattr r...
516
517
  		if (c == 0 && name_len > xattr->name_len)
  			c = 1;
355da1eb7   Sage Weil   ceph: inode opera...
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  		if (c < 0)
  			p = &(*p)->rb_left;
  		else if (c > 0)
  			p = &(*p)->rb_right;
  		else {
  			dout("__get_xattr %s: found %.*s
  ", name,
  			     xattr->val_len, xattr->val);
  			return xattr;
  		}
  	}
  
  	dout("__get_xattr %s: not found
  ", name);
  
  	return NULL;
  }
  
  static void __free_xattr(struct ceph_inode_xattr *xattr)
  {
  	BUG_ON(!xattr);
  
  	if (xattr->should_free_name)
c00e4522a   Xu Wang   ceph: remove unne...
541
  		kfree(xattr->name);
355da1eb7   Sage Weil   ceph: inode opera...
542
  	if (xattr->should_free_val)
c00e4522a   Xu Wang   ceph: remove unne...
543
  		kfree(xattr->val);
355da1eb7   Sage Weil   ceph: inode opera...
544
545
546
547
548
549
550
551
  
  	kfree(xattr);
  }
  
  static int __remove_xattr(struct ceph_inode_info *ci,
  			  struct ceph_inode_xattr *xattr)
  {
  	if (!xattr)
524186ace   Yan, Zheng   ceph: fix ceph_re...
552
  		return -ENODATA;
355da1eb7   Sage Weil   ceph: inode opera...
553
554
555
556
  
  	rb_erase(&xattr->node, &ci->i_xattrs.index);
  
  	if (xattr->should_free_name)
c00e4522a   Xu Wang   ceph: remove unne...
557
  		kfree(xattr->name);
355da1eb7   Sage Weil   ceph: inode opera...
558
  	if (xattr->should_free_val)
c00e4522a   Xu Wang   ceph: remove unne...
559
  		kfree(xattr->val);
355da1eb7   Sage Weil   ceph: inode opera...
560
561
562
563
564
565
566
567
  
  	ci->i_xattrs.names_size -= xattr->name_len;
  	ci->i_xattrs.vals_size -= xattr->val_len;
  	ci->i_xattrs.count--;
  	kfree(xattr);
  
  	return 0;
  }
355da1eb7   Sage Weil   ceph: inode opera...
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  static char *__copy_xattr_names(struct ceph_inode_info *ci,
  				char *dest)
  {
  	struct rb_node *p;
  	struct ceph_inode_xattr *xattr = NULL;
  
  	p = rb_first(&ci->i_xattrs.index);
  	dout("__copy_xattr_names count=%d
  ", ci->i_xattrs.count);
  
  	while (p) {
  		xattr = rb_entry(p, struct ceph_inode_xattr, node);
  		memcpy(dest, xattr->name, xattr->name_len);
  		dest[xattr->name_len] = '\0';
  
  		dout("dest=%s %p (%s) (%d/%d)
  ", dest, xattr, xattr->name,
  		     xattr->name_len, ci->i_xattrs.names_size);
  
  		dest += xattr->name_len + 1;
  		p = rb_next(p);
  	}
  
  	return dest;
  }
  
  void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  {
  	struct rb_node *p, *tmp;
  	struct ceph_inode_xattr *xattr = NULL;
  
  	p = rb_first(&ci->i_xattrs.index);
  
  	dout("__ceph_destroy_xattrs p=%p
  ", p);
  
  	while (p) {
  		xattr = rb_entry(p, struct ceph_inode_xattr, node);
  		tmp = p;
  		p = rb_next(tmp);
  		dout("__ceph_destroy_xattrs next p=%p (%.*s)
  ", p,
  		     xattr->name_len, xattr->name);
  		rb_erase(tmp, &ci->i_xattrs.index);
  
  		__free_xattr(xattr);
  	}
  
  	ci->i_xattrs.names_size = 0;
  	ci->i_xattrs.vals_size = 0;
  	ci->i_xattrs.index_version = 0;
  	ci->i_xattrs.count = 0;
  	ci->i_xattrs.index = RB_ROOT;
  }
  
  static int __build_xattrs(struct inode *inode)
be655596b   Sage Weil   ceph: use i_ceph_...
624
625
  	__releases(ci->i_ceph_lock)
  	__acquires(ci->i_ceph_lock)
355da1eb7   Sage Weil   ceph: inode opera...
626
627
628
629
630
631
632
  {
  	u32 namelen;
  	u32 numattr = 0;
  	void *p, *end;
  	u32 len;
  	const char *name, *val;
  	struct ceph_inode_info *ci = ceph_inode(inode);
0eb308531   Xiubo Li   ceph: print dentr...
633
  	u64 xattr_version;
355da1eb7   Sage Weil   ceph: inode opera...
634
  	struct ceph_inode_xattr **xattrs = NULL;
63ff78b25   Sage Weil   ceph: fix uniniti...
635
  	int err = 0;
355da1eb7   Sage Weil   ceph: inode opera...
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
  	int i;
  
  	dout("__build_xattrs() len=%d
  ",
  	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  
  	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  		return 0; /* already built */
  
  	__ceph_destroy_xattrs(ci);
  
  start:
  	/* updated internal xattr rb tree */
  	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  		p = ci->i_xattrs.blob->vec.iov_base;
  		end = p + ci->i_xattrs.blob->vec.iov_len;
  		ceph_decode_32_safe(&p, end, numattr, bad);
  		xattr_version = ci->i_xattrs.version;
be655596b   Sage Weil   ceph: use i_ceph_...
654
  		spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
655

7e8a29529   Ilya Dryomov   ceph: fix sizeof(...
656
  		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
355da1eb7   Sage Weil   ceph: inode opera...
657
658
659
660
  				 GFP_NOFS);
  		err = -ENOMEM;
  		if (!xattrs)
  			goto bad_lock;
1a295bd8c   Ilya Dryomov   ceph: remove redu...
661

355da1eb7   Sage Weil   ceph: inode opera...
662
663
664
665
666
667
  		for (i = 0; i < numattr; i++) {
  			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  					    GFP_NOFS);
  			if (!xattrs[i])
  				goto bad_lock;
  		}
be655596b   Sage Weil   ceph: use i_ceph_...
668
  		spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
669
670
671
672
673
  		if (ci->i_xattrs.version != xattr_version) {
  			/* lost a race, retry */
  			for (i = 0; i < numattr; i++)
  				kfree(xattrs[i]);
  			kfree(xattrs);
21ec6ffa4   Alan Cox   ceph: fix potenti...
674
  			xattrs = NULL;
355da1eb7   Sage Weil   ceph: inode opera...
675
676
677
678
679
680
681
682
683
684
685
686
687
  			goto start;
  		}
  		err = -EIO;
  		while (numattr--) {
  			ceph_decode_32_safe(&p, end, len, bad);
  			namelen = len;
  			name = p;
  			p += len;
  			ceph_decode_32_safe(&p, end, len, bad);
  			val = p;
  			p += len;
  
  			err = __set_xattr(ci, name, namelen, val, len,
fbc0b970d   Yan, Zheng   ceph: properly ha...
688
  					  0, 0, &xattrs[numattr]);
355da1eb7   Sage Weil   ceph: inode opera...
689
690
691
692
693
694
695
696
697
698
699
  
  			if (err < 0)
  				goto bad;
  		}
  		kfree(xattrs);
  	}
  	ci->i_xattrs.index_version = ci->i_xattrs.version;
  	ci->i_xattrs.dirty = false;
  
  	return err;
  bad_lock:
be655596b   Sage Weil   ceph: use i_ceph_...
700
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  bad:
  	if (xattrs) {
  		for (i = 0; i < numattr; i++)
  			kfree(xattrs[i]);
  		kfree(xattrs);
  	}
  	ci->i_xattrs.names_size = 0;
  	return err;
  }
  
  static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  				    int val_size)
  {
  	/*
  	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
  	 * 4 bytes per each value
  	 */
  	int size = 4 + ci->i_xattrs.count*(4 + 4) +
  			     ci->i_xattrs.names_size +
  			     ci->i_xattrs.vals_size;
  	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d
  ",
  	     ci->i_xattrs.count, ci->i_xattrs.names_size,
  	     ci->i_xattrs.vals_size);
  
  	if (name_size)
  		size += 4 + 4 + name_size + val_size;
  
  	return size;
  }
  
  /*
   * If there are dirty xattrs, reencode xattrs into the prealloc_blob
12fe3dda7   Luis Henriques   ceph: fix buffer ...
734
735
736
   * and swap into place.  It returns the old i_xattrs.blob (or NULL) so
   * that it can be freed by the caller as the i_ceph_lock is likely to be
   * held.
355da1eb7   Sage Weil   ceph: inode opera...
737
   */
12fe3dda7   Luis Henriques   ceph: fix buffer ...
738
  struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
355da1eb7   Sage Weil   ceph: inode opera...
739
740
741
  {
  	struct rb_node *p;
  	struct ceph_inode_xattr *xattr = NULL;
12fe3dda7   Luis Henriques   ceph: fix buffer ...
742
  	struct ceph_buffer *old_blob = NULL;
355da1eb7   Sage Weil   ceph: inode opera...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
  	void *dest;
  
  	dout("__build_xattrs_blob %p
  ", &ci->vfs_inode);
  	if (ci->i_xattrs.dirty) {
  		int need = __get_required_blob_size(ci, 0, 0);
  
  		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  
  		p = rb_first(&ci->i_xattrs.index);
  		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  
  		ceph_encode_32(&dest, ci->i_xattrs.count);
  		while (p) {
  			xattr = rb_entry(p, struct ceph_inode_xattr, node);
  
  			ceph_encode_32(&dest, xattr->name_len);
  			memcpy(dest, xattr->name, xattr->name_len);
  			dest += xattr->name_len;
  			ceph_encode_32(&dest, xattr->val_len);
  			memcpy(dest, xattr->val, xattr->val_len);
  			dest += xattr->val_len;
  
  			p = rb_next(p);
  		}
  
  		/* adjust buffer len; it may be larger than we need */
  		ci->i_xattrs.prealloc_blob->vec.iov_len =
  			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
b6c1d5b81   Sage Weil   ceph: simplify ce...
772
  		if (ci->i_xattrs.blob)
12fe3dda7   Luis Henriques   ceph: fix buffer ...
773
  			old_blob = ci->i_xattrs.blob;
355da1eb7   Sage Weil   ceph: inode opera...
774
775
776
  		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  		ci->i_xattrs.prealloc_blob = NULL;
  		ci->i_xattrs.dirty = false;
4a625be47   Sage Weil   ceph: include dir...
777
  		ci->i_xattrs.version++;
355da1eb7   Sage Weil   ceph: inode opera...
778
  	}
12fe3dda7   Luis Henriques   ceph: fix buffer ...
779
780
  
  	return old_blob;
355da1eb7   Sage Weil   ceph: inode opera...
781
  }
315f24088   Yan, Zheng   ceph: fix securit...
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
  static inline int __get_request_mask(struct inode *in) {
  	struct ceph_mds_request *req = current->journal_info;
  	int mask = 0;
  	if (req && req->r_target_inode == in) {
  		if (req->r_op == CEPH_MDS_OP_LOOKUP ||
  		    req->r_op == CEPH_MDS_OP_LOOKUPINO ||
  		    req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
  		    req->r_op == CEPH_MDS_OP_GETATTR) {
  			mask = le32_to_cpu(req->r_args.getattr.mask);
  		} else if (req->r_op == CEPH_MDS_OP_OPEN ||
  			   req->r_op == CEPH_MDS_OP_CREATE) {
  			mask = le32_to_cpu(req->r_args.open.mask);
  		}
  	}
  	return mask;
  }
7221fe4c2   Guangliang Zhao   ceph: add acl for...
798
  ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
355da1eb7   Sage Weil   ceph: inode opera...
799
800
  		      size_t size)
  {
355da1eb7   Sage Weil   ceph: inode opera...
801
  	struct ceph_inode_info *ci = ceph_inode(inode);
355da1eb7   Sage Weil   ceph: inode opera...
802
  	struct ceph_inode_xattr *xattr;
881a5fa20   Alex Elder   ceph: drop "_cb" ...
803
  	struct ceph_vxattr *vxattr = NULL;
315f24088   Yan, Zheng   ceph: fix securit...
804
  	int req_mask;
f1d1b51de   Jeff Layton   ceph: make getxat...
805
  	ssize_t err;
355da1eb7   Sage Weil   ceph: inode opera...
806

0bee82fb4   Sage Weil   ceph: fix getxatt...
807
808
  	/* let's see if a virtual xattr was requested */
  	vxattr = ceph_match_vxattr(inode, name);
29dccfa5a   Yan, Zheng   ceph: don't reque...
809
  	if (vxattr) {
49a9f4f67   Yan, Zheng   ceph: always get ...
810
811
812
813
  		int mask = 0;
  		if (vxattr->flags & VXATTR_FLAG_RSTAT)
  			mask |= CEPH_STAT_RSTAT;
  		err = ceph_do_getattr(inode, mask, true);
1684dd03e   Yan, Zheng   ceph: getattr bef...
814
815
  		if (err)
  			return err;
29dccfa5a   Yan, Zheng   ceph: don't reque...
816
  		err = -ENODATA;
3b421018f   Jeff Layton   ceph: return -ERA...
817
  		if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
29dccfa5a   Yan, Zheng   ceph: don't reque...
818
  			err = vxattr->getxattr_cb(ci, value, size);
3b421018f   Jeff Layton   ceph: return -ERA...
819
820
821
  			if (size && size < err)
  				err = -ERANGE;
  		}
a1dc19373   majianpeng   ceph: fix sleepin...
822
  		return err;
0bee82fb4   Sage Weil   ceph: fix getxatt...
823
  	}
315f24088   Yan, Zheng   ceph: fix securit...
824
  	req_mask = __get_request_mask(inode);
a1dc19373   majianpeng   ceph: fix sleepin...
825
  	spin_lock(&ci->i_ceph_lock);
d36e0b620   Jeff Layton   ceph: print name ...
826
827
  	dout("getxattr %p name '%s' ver=%lld index_ver=%lld
  ", inode, name,
a1dc19373   majianpeng   ceph: fix sleepin...
828
  	     ci->i_xattrs.version, ci->i_xattrs.index_version);
508b32d86   Yan, Zheng   ceph: request xat...
829
  	if (ci->i_xattrs.version == 0 ||
315f24088   Yan, Zheng   ceph: fix securit...
830
  	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
1af16d547   Xiubo Li   ceph: add caps pe...
831
  	      __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
be655596b   Sage Weil   ceph: use i_ceph_...
832
  		spin_unlock(&ci->i_ceph_lock);
315f24088   Yan, Zheng   ceph: fix securit...
833
834
  
  		/* security module gets xattr while filling trace */
d37b1d994   Markus Elfring   ceph: adjust 36 c...
835
  		if (current->journal_info) {
315f24088   Yan, Zheng   ceph: fix securit...
836
837
838
839
840
  			pr_warn_ratelimited("sync getxattr %p "
  					    "during filling trace
  ", inode);
  			return -EBUSY;
  		}
355da1eb7   Sage Weil   ceph: inode opera...
841
  		/* get xattrs from mds (if we don't already have them) */
508b32d86   Yan, Zheng   ceph: request xat...
842
  		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
355da1eb7   Sage Weil   ceph: inode opera...
843
844
  		if (err)
  			return err;
508b32d86   Yan, Zheng   ceph: request xat...
845
  		spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
846
  	}
355da1eb7   Sage Weil   ceph: inode opera...
847
848
849
  	err = __build_xattrs(inode);
  	if (err < 0)
  		goto out;
355da1eb7   Sage Weil   ceph: inode opera...
850
851
  	err = -ENODATA;  /* == ENOATTR */
  	xattr = __get_xattr(ci, name);
0bee82fb4   Sage Weil   ceph: fix getxatt...
852
  	if (!xattr)
355da1eb7   Sage Weil   ceph: inode opera...
853
  		goto out;
355da1eb7   Sage Weil   ceph: inode opera...
854
855
856
857
858
859
860
861
862
863
  
  	err = -ERANGE;
  	if (size && size < xattr->val_len)
  		goto out;
  
  	err = xattr->val_len;
  	if (size == 0)
  		goto out;
  
  	memcpy(value, xattr->val, xattr->val_len);
d37b1d994   Markus Elfring   ceph: adjust 36 c...
864
  	if (current->journal_info &&
026105ebb   Jeff Layton   ceph: only set CE...
865
866
  	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
  	    security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
315f24088   Yan, Zheng   ceph: fix securit...
867
  		ci->i_ceph_flags |= CEPH_I_SEC_INITED;
355da1eb7   Sage Weil   ceph: inode opera...
868
  out:
be655596b   Sage Weil   ceph: use i_ceph_...
869
  	spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
870
871
872
873
874
  	return err;
  }
  
  ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  {
2b0143b5c   David Howells   VFS: normal files...
875
  	struct inode *inode = d_inode(dentry);
355da1eb7   Sage Weil   ceph: inode opera...
876
  	struct ceph_inode_info *ci = ceph_inode(inode);
2b2abcac8   David Disseldorp   ceph: fix listxat...
877
  	bool len_only = (size == 0);
355da1eb7   Sage Weil   ceph: inode opera...
878
879
  	u32 namelen;
  	int err;
355da1eb7   Sage Weil   ceph: inode opera...
880

be655596b   Sage Weil   ceph: use i_ceph_...
881
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
882
883
884
  	dout("listxattr %p ver=%lld index_ver=%lld
  ", inode,
  	     ci->i_xattrs.version, ci->i_xattrs.index_version);
508b32d86   Yan, Zheng   ceph: request xat...
885
  	if (ci->i_xattrs.version == 0 ||
1af16d547   Xiubo Li   ceph: add caps pe...
886
  	    !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
be655596b   Sage Weil   ceph: use i_ceph_...
887
  		spin_unlock(&ci->i_ceph_lock);
508b32d86   Yan, Zheng   ceph: request xat...
888
  		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
355da1eb7   Sage Weil   ceph: inode opera...
889
890
  		if (err)
  			return err;
508b32d86   Yan, Zheng   ceph: request xat...
891
  		spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
892
  	}
355da1eb7   Sage Weil   ceph: inode opera...
893
894
895
  	err = __build_xattrs(inode);
  	if (err < 0)
  		goto out;
3ce6cd123   Alex Elder   ceph: avoid repea...
896

2b2abcac8   David Disseldorp   ceph: fix listxat...
897
  	/* add 1 byte for each xattr due to the null termination */
b65917dd2   Sage Weil   ceph: fix listxat...
898
  	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
2b2abcac8   David Disseldorp   ceph: fix listxat...
899
900
901
902
903
904
905
906
  	if (!len_only) {
  		if (namelen > size) {
  			err = -ERANGE;
  			goto out;
  		}
  		names = __copy_xattr_names(ci, names);
  		size -= namelen;
  	}
2b2abcac8   David Disseldorp   ceph: fix listxat...
907
  	err = namelen;
355da1eb7   Sage Weil   ceph: inode opera...
908
  out:
be655596b   Sage Weil   ceph: use i_ceph_...
909
  	spin_unlock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
910
911
  	return err;
  }
a26feccab   Andreas Gruenbacher   ceph: Get rid of ...
912
  static int ceph_sync_setxattr(struct inode *inode, const char *name,
355da1eb7   Sage Weil   ceph: inode opera...
913
914
  			      const char *value, size_t size, int flags)
  {
a26feccab   Andreas Gruenbacher   ceph: Get rid of ...
915
  	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
355da1eb7   Sage Weil   ceph: inode opera...
916
  	struct ceph_inode_info *ci = ceph_inode(inode);
355da1eb7   Sage Weil   ceph: inode opera...
917
  	struct ceph_mds_request *req;
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
918
  	struct ceph_mds_client *mdsc = fsc->mdsc;
25e6bae35   Yan, Zheng   ceph: use pagelis...
919
  	struct ceph_pagelist *pagelist = NULL;
04303d8ad   Yan, Zheng   ceph: use CEPH_MD...
920
  	int op = CEPH_MDS_OP_SETXATTR;
355da1eb7   Sage Weil   ceph: inode opera...
921
  	int err;
25e6bae35   Yan, Zheng   ceph: use pagelis...
922

0aeff37ab   Yan, Zheng   ceph: fix setting...
923
  	if (size > 0) {
25e6bae35   Yan, Zheng   ceph: use pagelis...
924
  		/* copy value into pagelist */
33165d472   Ilya Dryomov   libceph: introduc...
925
  		pagelist = ceph_pagelist_alloc(GFP_NOFS);
25e6bae35   Yan, Zheng   ceph: use pagelis...
926
  		if (!pagelist)
355da1eb7   Sage Weil   ceph: inode opera...
927
  			return -ENOMEM;
25e6bae35   Yan, Zheng   ceph: use pagelis...
928

25e6bae35   Yan, Zheng   ceph: use pagelis...
929
930
931
  		err = ceph_pagelist_append(pagelist, value, size);
  		if (err)
  			goto out;
0aeff37ab   Yan, Zheng   ceph: fix setting...
932
  	} else if (!value) {
04303d8ad   Yan, Zheng   ceph: use CEPH_MD...
933
934
935
936
  		if (flags & CEPH_XATTR_REPLACE)
  			op = CEPH_MDS_OP_RMXATTR;
  		else
  			flags |= CEPH_XATTR_REMOVE;
355da1eb7   Sage Weil   ceph: inode opera...
937
938
939
940
941
942
  	}
  
  	dout("setxattr value=%.*s
  ", (int)size, value);
  
  	/* do request */
04303d8ad   Yan, Zheng   ceph: use CEPH_MD...
943
  	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
60d877334   Julia Lawall   fs/ceph: introduc...
944
945
946
947
  	if (IS_ERR(req)) {
  		err = PTR_ERR(req);
  		goto out;
  	}
a149bb9a2   Sanidhya Kashyap   ceph: kstrdup() m...
948

355da1eb7   Sage Weil   ceph: inode opera...
949
  	req->r_path2 = kstrdup(name, GFP_NOFS);
a149bb9a2   Sanidhya Kashyap   ceph: kstrdup() m...
950
951
952
953
954
  	if (!req->r_path2) {
  		ceph_mdsc_put_request(req);
  		err = -ENOMEM;
  		goto out;
  	}
355da1eb7   Sage Weil   ceph: inode opera...
955

04303d8ad   Yan, Zheng   ceph: use CEPH_MD...
956
957
958
959
960
  	if (op == CEPH_MDS_OP_SETXATTR) {
  		req->r_args.setxattr.flags = cpu_to_le32(flags);
  		req->r_pagelist = pagelist;
  		pagelist = NULL;
  	}
355da1eb7   Sage Weil   ceph: inode opera...
961

a149bb9a2   Sanidhya Kashyap   ceph: kstrdup() m...
962
963
964
965
  	req->r_inode = inode;
  	ihold(inode);
  	req->r_num_caps = 1;
  	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
355da1eb7   Sage Weil   ceph: inode opera...
966
967
  	dout("xattr.ver (before): %lld
  ", ci->i_xattrs.version);
752c8bdcf   Sage Weil   ceph: do not chai...
968
  	err = ceph_mdsc_do_request(mdsc, NULL, req);
355da1eb7   Sage Weil   ceph: inode opera...
969
970
971
972
973
  	ceph_mdsc_put_request(req);
  	dout("xattr.ver (after): %lld
  ", ci->i_xattrs.version);
  
  out:
25e6bae35   Yan, Zheng   ceph: use pagelis...
974
975
  	if (pagelist)
  		ceph_pagelist_release(pagelist);
355da1eb7   Sage Weil   ceph: inode opera...
976
977
  	return err;
  }
a26feccab   Andreas Gruenbacher   ceph: Get rid of ...
978
  int __ceph_setxattr(struct inode *inode, const char *name,
7221fe4c2   Guangliang Zhao   ceph: add acl for...
979
  			const void *value, size_t size, int flags)
355da1eb7   Sage Weil   ceph: inode opera...
980
  {
881a5fa20   Alex Elder   ceph: drop "_cb" ...
981
  	struct ceph_vxattr *vxattr;
355da1eb7   Sage Weil   ceph: inode opera...
982
  	struct ceph_inode_info *ci = ceph_inode(inode);
a26feccab   Andreas Gruenbacher   ceph: Get rid of ...
983
  	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
984
  	struct ceph_cap_flush *prealloc_cf = NULL;
86968ef21   Luis Henriques   ceph: fix buffer ...
985
  	struct ceph_buffer *old_blob = NULL;
18fa8b3fe   Alex Elder   ceph: make ceph_s...
986
  	int issued;
355da1eb7   Sage Weil   ceph: inode opera...
987
  	int err;
fbc0b970d   Yan, Zheng   ceph: properly ha...
988
  	int dirty = 0;
355da1eb7   Sage Weil   ceph: inode opera...
989
990
991
992
993
  	int name_len = strlen(name);
  	int val_len = size;
  	char *newname = NULL;
  	char *newval = NULL;
  	struct ceph_inode_xattr *xattr = NULL;
355da1eb7   Sage Weil   ceph: inode opera...
994
  	int required_blob_size;
f19198268   Yan, Zheng   ceph: check if md...
995
  	bool check_realm = false;
604d1b024   Yan, Zheng   ceph: take snap_r...
996
  	bool lock_snap_rwsem = false;
355da1eb7   Sage Weil   ceph: inode opera...
997

2cdeb1e47   Andreas Gruenbacher   ceph: Switch to g...
998
999
  	if (ceph_snap(inode) != CEPH_NOSNAP)
  		return -EROFS;
355da1eb7   Sage Weil   ceph: inode opera...
1000

06476a69d   Alex Elder   ceph: pass inode ...
1001
  	vxattr = ceph_match_vxattr(inode, name);
f19198268   Yan, Zheng   ceph: check if md...
1002
  	if (vxattr) {
4e9906e79   Yan, Zheng   ceph: use bit fla...
1003
  		if (vxattr->flags & VXATTR_FLAG_READONLY)
f19198268   Yan, Zheng   ceph: check if md...
1004
1005
1006
1007
  			return -EOPNOTSUPP;
  		if (value && !strncmp(vxattr->name, "ceph.quota", 10))
  			check_realm = true;
  	}
355da1eb7   Sage Weil   ceph: inode opera...
1008

3adf654dd   Sage Weil   ceph: pass unhand...
1009
1010
1011
  	/* pass any unhandled ceph.* xattrs through to the MDS */
  	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
  		goto do_sync_unlocked;
355da1eb7   Sage Weil   ceph: inode opera...
1012
1013
  	/* preallocate memory for xattr name, value, index node */
  	err = -ENOMEM;
61413c2f5   Julia Lawall   fs/ceph/xattr.c: ...
1014
  	newname = kmemdup(name, name_len + 1, GFP_NOFS);
355da1eb7   Sage Weil   ceph: inode opera...
1015
1016
  	if (!newname)
  		goto out;
355da1eb7   Sage Weil   ceph: inode opera...
1017
1018
  
  	if (val_len) {
b829c1954   Alex Elder   ceph: don't null-...
1019
  		newval = kmemdup(value, val_len, GFP_NOFS);
355da1eb7   Sage Weil   ceph: inode opera...
1020
1021
  		if (!newval)
  			goto out;
355da1eb7   Sage Weil   ceph: inode opera...
1022
1023
1024
1025
1026
  	}
  
  	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  	if (!xattr)
  		goto out;
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1027
1028
1029
  	prealloc_cf = ceph_alloc_cap_flush();
  	if (!prealloc_cf)
  		goto out;
be655596b   Sage Weil   ceph: use i_ceph_...
1030
  	spin_lock(&ci->i_ceph_lock);
355da1eb7   Sage Weil   ceph: inode opera...
1031
1032
  retry:
  	issued = __ceph_caps_issued(ci, NULL);
508b32d86   Yan, Zheng   ceph: request xat...
1033
  	if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
355da1eb7   Sage Weil   ceph: inode opera...
1034
  		goto do_sync;
604d1b024   Yan, Zheng   ceph: take snap_r...
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
  
  	if (!lock_snap_rwsem && !ci->i_head_snapc) {
  		lock_snap_rwsem = true;
  		if (!down_read_trylock(&mdsc->snap_rwsem)) {
  			spin_unlock(&ci->i_ceph_lock);
  			down_read(&mdsc->snap_rwsem);
  			spin_lock(&ci->i_ceph_lock);
  			goto retry;
  		}
  	}
d36e0b620   Jeff Layton   ceph: print name ...
1045
1046
1047
  	dout("setxattr %p name '%s' issued %s
  ", inode, name,
  	     ceph_cap_string(issued));
355da1eb7   Sage Weil   ceph: inode opera...
1048
1049
1050
1051
1052
1053
  	__build_xattrs(inode);
  
  	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  
  	if (!ci->i_xattrs.prealloc_blob ||
  	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
18fa8b3fe   Alex Elder   ceph: make ceph_s...
1054
  		struct ceph_buffer *blob;
355da1eb7   Sage Weil   ceph: inode opera...
1055

be655596b   Sage Weil   ceph: use i_ceph_...
1056
  		spin_unlock(&ci->i_ceph_lock);
86968ef21   Luis Henriques   ceph: fix buffer ...
1057
1058
1059
  		ceph_buffer_put(old_blob); /* Shouldn't be required */
  		dout(" pre-allocating new blob size=%d
  ", required_blob_size);
b6c1d5b81   Sage Weil   ceph: simplify ce...
1060
  		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
355da1eb7   Sage Weil   ceph: inode opera...
1061
  		if (!blob)
604d1b024   Yan, Zheng   ceph: take snap_r...
1062
  			goto do_sync_unlocked;
be655596b   Sage Weil   ceph: use i_ceph_...
1063
  		spin_lock(&ci->i_ceph_lock);
86968ef21   Luis Henriques   ceph: fix buffer ...
1064
  		/* prealloc_blob can't be released while holding i_ceph_lock */
b6c1d5b81   Sage Weil   ceph: simplify ce...
1065
  		if (ci->i_xattrs.prealloc_blob)
86968ef21   Luis Henriques   ceph: fix buffer ...
1066
  			old_blob = ci->i_xattrs.prealloc_blob;
355da1eb7   Sage Weil   ceph: inode opera...
1067
1068
1069
  		ci->i_xattrs.prealloc_blob = blob;
  		goto retry;
  	}
bcdfeb2eb   Yan, Zheng   ceph: remove xatt...
1070
1071
  	err = __set_xattr(ci, newname, name_len, newval, val_len,
  			  flags, value ? 1 : -1, &xattr);
18fa8b3fe   Alex Elder   ceph: make ceph_s...
1072

fbc0b970d   Yan, Zheng   ceph: properly ha...
1073
  	if (!err) {
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1074
1075
  		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
  					       &prealloc_cf);
fbc0b970d   Yan, Zheng   ceph: properly ha...
1076
  		ci->i_xattrs.dirty = true;
c2050a454   Deepa Dinamani   fs: Replace curre...
1077
  		inode->i_ctime = current_time(inode);
fbc0b970d   Yan, Zheng   ceph: properly ha...
1078
  	}
18fa8b3fe   Alex Elder   ceph: make ceph_s...
1079

be655596b   Sage Weil   ceph: use i_ceph_...
1080
  	spin_unlock(&ci->i_ceph_lock);
86968ef21   Luis Henriques   ceph: fix buffer ...
1081
  	ceph_buffer_put(old_blob);
604d1b024   Yan, Zheng   ceph: take snap_r...
1082
1083
  	if (lock_snap_rwsem)
  		up_read(&mdsc->snap_rwsem);
fca65b4ad   Sage Weil   ceph: do not call...
1084
1085
  	if (dirty)
  		__mark_inode_dirty(inode, dirty);
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1086
  	ceph_free_cap_flush(prealloc_cf);
355da1eb7   Sage Weil   ceph: inode opera...
1087
1088
1089
  	return err;
  
  do_sync:
be655596b   Sage Weil   ceph: use i_ceph_...
1090
  	spin_unlock(&ci->i_ceph_lock);
3adf654dd   Sage Weil   ceph: pass unhand...
1091
  do_sync_unlocked:
604d1b024   Yan, Zheng   ceph: take snap_r...
1092
1093
  	if (lock_snap_rwsem)
  		up_read(&mdsc->snap_rwsem);
315f24088   Yan, Zheng   ceph: fix securit...
1094
1095
  
  	/* security module set xattr while filling trace */
d37b1d994   Markus Elfring   ceph: adjust 36 c...
1096
  	if (current->journal_info) {
315f24088   Yan, Zheng   ceph: fix securit...
1097
1098
1099
1100
1101
  		pr_warn_ratelimited("sync setxattr %p "
  				    "during filling trace
  ", inode);
  		err = -EBUSY;
  	} else {
a26feccab   Andreas Gruenbacher   ceph: Get rid of ...
1102
  		err = ceph_sync_setxattr(inode, name, value, size, flags);
f19198268   Yan, Zheng   ceph: check if md...
1103
1104
1105
1106
1107
1108
1109
1110
1111
  		if (err >= 0 && check_realm) {
  			/* check if snaprealm was created for quota inode */
  			spin_lock(&ci->i_ceph_lock);
  			if ((ci->i_max_files || ci->i_max_bytes) &&
  			    !(ci->i_snap_realm &&
  			      ci->i_snap_realm->ino == ci->i_vino.ino))
  				err = -EOPNOTSUPP;
  			spin_unlock(&ci->i_ceph_lock);
  		}
315f24088   Yan, Zheng   ceph: fix securit...
1112
  	}
355da1eb7   Sage Weil   ceph: inode opera...
1113
  out:
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1114
  	ceph_free_cap_flush(prealloc_cf);
355da1eb7   Sage Weil   ceph: inode opera...
1115
1116
1117
1118
1119
  	kfree(newname);
  	kfree(newval);
  	kfree(xattr);
  	return err;
  }
2cdeb1e47   Andreas Gruenbacher   ceph: Switch to g...
1120
1121
  static int ceph_get_xattr_handler(const struct xattr_handler *handler,
  				  struct dentry *dentry, struct inode *inode,
3484eba91   Mark Salyzyn   FROMLIST: Add fla...
1122
1123
  				  const char *name, void *value, size_t size,
  				  int flags)
7221fe4c2   Guangliang Zhao   ceph: add acl for...
1124
  {
2cdeb1e47   Andreas Gruenbacher   ceph: Switch to g...
1125
1126
1127
1128
  	if (!ceph_is_valid_xattr(name))
  		return -EOPNOTSUPP;
  	return __ceph_getxattr(inode, name, value, size);
  }
7221fe4c2   Guangliang Zhao   ceph: add acl for...
1129

2cdeb1e47   Andreas Gruenbacher   ceph: Switch to g...
1130
  static int ceph_set_xattr_handler(const struct xattr_handler *handler,
593012268   Al Viro   switch xattr_hand...
1131
1132
1133
  				  struct dentry *unused, struct inode *inode,
  				  const char *name, const void *value,
  				  size_t size, int flags)
2cdeb1e47   Andreas Gruenbacher   ceph: Switch to g...
1134
1135
1136
  {
  	if (!ceph_is_valid_xattr(name))
  		return -EOPNOTSUPP;
593012268   Al Viro   switch xattr_hand...
1137
  	return __ceph_setxattr(inode, name, value, size, flags);
7221fe4c2   Guangliang Zhao   ceph: add acl for...
1138
  }
315f24088   Yan, Zheng   ceph: fix securit...
1139

5130ccea7   Wei Yongjun   ceph: fix non sta...
1140
  static const struct xattr_handler ceph_other_xattr_handler = {
2cdeb1e47   Andreas Gruenbacher   ceph: Switch to g...
1141
1142
1143
1144
  	.prefix = "",  /* match any name => handlers called with full name */
  	.get = ceph_get_xattr_handler,
  	.set = ceph_set_xattr_handler,
  };
315f24088   Yan, Zheng   ceph: fix securit...
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
  #ifdef CONFIG_SECURITY
  bool ceph_security_xattr_wanted(struct inode *in)
  {
  	return in->i_security != NULL;
  }
  
  bool ceph_security_xattr_deadlock(struct inode *in)
  {
  	struct ceph_inode_info *ci;
  	bool ret;
d37b1d994   Markus Elfring   ceph: adjust 36 c...
1155
  	if (!in->i_security)
315f24088   Yan, Zheng   ceph: fix securit...
1156
1157
1158
1159
1160
1161
1162
1163
1164
  		return false;
  	ci = ceph_inode(in);
  	spin_lock(&ci->i_ceph_lock);
  	ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
  	      !(ci->i_xattrs.version > 0 &&
  		__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
  	spin_unlock(&ci->i_ceph_lock);
  	return ret;
  }
ac6713ccb   Yan, Zheng   ceph: add selinux...
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
  
  #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
  int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
  			   struct ceph_acl_sec_ctx *as_ctx)
  {
  	struct ceph_pagelist *pagelist = as_ctx->pagelist;
  	const char *name;
  	size_t name_len;
  	int err;
  
  	err = security_dentry_init_security(dentry, mode, &dentry->d_name,
  					    &as_ctx->sec_ctx,
  					    &as_ctx->sec_ctxlen);
  	if (err < 0) {
  		WARN_ON_ONCE(err != -EOPNOTSUPP);
  		err = 0; /* do nothing */
  		goto out;
  	}
  
  	err = -ENOMEM;
  	if (!pagelist) {
  		pagelist = ceph_pagelist_alloc(GFP_KERNEL);
  		if (!pagelist)
  			goto out;
  		err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  		if (err)
  			goto out;
  		ceph_pagelist_encode_32(pagelist, 1);
  	}
  
  	/*
  	 * FIXME: Make security_dentry_init_security() generic. Currently
  	 * It only supports single security module and only selinux has
  	 * dentry_init_security hook.
  	 */
  	name = XATTR_NAME_SELINUX;
  	name_len = strlen(name);
  	err = ceph_pagelist_reserve(pagelist,
  				    4 * 2 + name_len + as_ctx->sec_ctxlen);
  	if (err)
  		goto out;
  
  	if (as_ctx->pagelist) {
  		/* update count of KV pairs */
  		BUG_ON(pagelist->length <= sizeof(__le32));
  		if (list_is_singular(&pagelist->head)) {
  			le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
  		} else {
  			struct page *page = list_first_entry(&pagelist->head,
  							     struct page, lru);
  			void *addr = kmap_atomic(page);
  			le32_add_cpu((__le32*)addr, 1);
  			kunmap_atomic(addr);
  		}
  	} else {
  		as_ctx->pagelist = pagelist;
  	}
  
  	ceph_pagelist_encode_32(pagelist, name_len);
  	ceph_pagelist_append(pagelist, name, name_len);
  
  	ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
  	ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
  
  	err = 0;
  out:
  	if (pagelist && !as_ctx->pagelist)
  		ceph_pagelist_release(pagelist);
  	return err;
  }
668959a53   Jeff Layton   ceph: turn ceph_s...
1235
1236
  #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
  #endif /* CONFIG_SECURITY */
5c31e92df   Yan, Zheng   ceph: rename stru...
1237
1238
1239
1240
1241
1242
1243
  
  void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
  {
  #ifdef CONFIG_CEPH_FS_POSIX_ACL
  	posix_acl_release(as_ctx->acl);
  	posix_acl_release(as_ctx->default_acl);
  #endif
ac6713ccb   Yan, Zheng   ceph: add selinux...
1244
1245
1246
  #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
  	security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
  #endif
5c31e92df   Yan, Zheng   ceph: rename stru...
1247
1248
1249
  	if (as_ctx->pagelist)
  		ceph_pagelist_release(as_ctx->pagelist);
  }
ac6713ccb   Yan, Zheng   ceph: add selinux...
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
  
  /*
   * List of handlers for synthetic system.* attributes. Other
   * attributes are handled directly.
   */
  const struct xattr_handler *ceph_xattr_handlers[] = {
  #ifdef CONFIG_CEPH_FS_POSIX_ACL
  	&posix_acl_access_xattr_handler,
  	&posix_acl_default_xattr_handler,
  #endif
ac6713ccb   Yan, Zheng   ceph: add selinux...
1260
1261
1262
  	&ceph_other_xattr_handler,
  	NULL,
  };