Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
25e6bae3 | 3 | #include <linux/ceph/pagelist.h> |
3d14c5d2 | 4 | |
355da1eb | 5 | #include "super.h" |
3d14c5d2 YS |
6 | #include "mds_client.h" |
7 | ||
8 | #include <linux/ceph/decode.h> | |
355da1eb SW |
9 | |
10 | #include <linux/xattr.h> | |
4db658ea | 11 | #include <linux/posix_acl_xattr.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
355da1eb | 13 | |
22891907 AE |
14 | #define XATTR_CEPH_PREFIX "ceph." |
15 | #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1) | |
16 | ||
bcdfeb2e YZ |
17 | static int __remove_xattr(struct ceph_inode_info *ci, |
18 | struct ceph_inode_xattr *xattr); | |
19 | ||
5130ccea | 20 | static const struct xattr_handler ceph_other_xattr_handler; |
2cdeb1e4 | 21 | |
7221fe4c GZ |
22 | /* |
23 | * List of handlers for synthetic system.* attributes. Other | |
24 | * attributes are handled directly. | |
25 | */ | |
26 | const struct xattr_handler *ceph_xattr_handlers[] = { | |
27 | #ifdef CONFIG_CEPH_FS_POSIX_ACL | |
4db658ea LT |
28 | &posix_acl_access_xattr_handler, |
29 | &posix_acl_default_xattr_handler, | |
7221fe4c | 30 | #endif |
2cdeb1e4 | 31 | &ceph_other_xattr_handler, |
7221fe4c GZ |
32 | NULL, |
33 | }; | |
34 | ||
355da1eb SW |
35 | static bool ceph_is_valid_xattr(const char *name) |
36 | { | |
22891907 | 37 | return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) || |
1a756278 | 38 | !strncmp(name, XATTR_SECURITY_PREFIX, |
355da1eb SW |
39 | XATTR_SECURITY_PREFIX_LEN) || |
40 | !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || | |
41 | !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); | |
42 | } | |
43 | ||
44 | /* | |
45 | * These define virtual xattrs exposing the recursive directory | |
46 | * statistics and layout metadata. | |
47 | */ | |
881a5fa2 | 48 | struct ceph_vxattr { |
355da1eb | 49 | char *name; |
3ce6cd12 | 50 | size_t name_size; /* strlen(name) + 1 (for '\0') */ |
355da1eb SW |
51 | size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, |
52 | size_t size); | |
f36e4472 | 53 | bool (*exists_cb)(struct ceph_inode_info *ci); |
4e9906e7 | 54 | unsigned int flags; |
355da1eb SW |
55 | }; |
56 | ||
4e9906e7 YZ |
57 | #define VXATTR_FLAG_READONLY (1<<0) |
58 | #define VXATTR_FLAG_HIDDEN (1<<1) | |
49a9f4f6 | 59 | #define VXATTR_FLAG_RSTAT (1<<2) |
4e9906e7 | 60 | |
32ab0bd7 SW |
61 | /* layouts */ |
62 | ||
63 | static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci) | |
64 | { | |
779fe0fb YZ |
65 | struct ceph_file_layout *fl = &ci->i_layout; |
66 | return (fl->stripe_unit > 0 || fl->stripe_count > 0 || | |
67 | fl->object_size > 0 || fl->pool_id >= 0 || | |
68 | rcu_dereference_raw(fl->pool_ns) != NULL); | |
32ab0bd7 SW |
69 | } |
70 | ||
71 | static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val, | |
1e5c6649 | 72 | size_t size) |
32ab0bd7 | 73 | { |
32ab0bd7 SW |
74 | struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); |
75 | struct ceph_osd_client *osdc = &fsc->client->osdc; | |
779fe0fb | 76 | struct ceph_string *pool_ns; |
7627151e | 77 | s64 pool = ci->i_layout.pool_id; |
32ab0bd7 | 78 | const char *pool_name; |
779fe0fb | 79 | const char *ns_field = " pool_namespace="; |
1e5c6649 | 80 | char buf[128]; |
779fe0fb YZ |
81 | size_t len, total_len = 0; |
82 | int ret; | |
83 | ||
84 | pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); | |
32ab0bd7 SW |
85 | |
86 | dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode); | |
5aea3dcd | 87 | down_read(&osdc->lock); |
32ab0bd7 | 88 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); |
1e5c6649 | 89 | if (pool_name) { |
779fe0fb | 90 | len = snprintf(buf, sizeof(buf), |
7627151e YZ |
91 | "stripe_unit=%u stripe_count=%u object_size=%u pool=", |
92 | ci->i_layout.stripe_unit, ci->i_layout.stripe_count, | |
93 | ci->i_layout.object_size); | |
779fe0fb | 94 | total_len = len + strlen(pool_name); |
1e5c6649 | 95 | } else { |
779fe0fb | 96 | len = snprintf(buf, sizeof(buf), |
7627151e YZ |
97 | "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld", |
98 | ci->i_layout.stripe_unit, ci->i_layout.stripe_count, | |
99 | ci->i_layout.object_size, (unsigned long long)pool); | |
779fe0fb YZ |
100 | total_len = len; |
101 | } | |
102 | ||
103 | if (pool_ns) | |
104 | total_len += strlen(ns_field) + pool_ns->len; | |
105 | ||
106 | if (!size) { | |
107 | ret = total_len; | |
108 | } else if (total_len > size) { | |
109 | ret = -ERANGE; | |
110 | } else { | |
111 | memcpy(val, buf, len); | |
112 | ret = len; | |
113 | if (pool_name) { | |
114 | len = strlen(pool_name); | |
115 | memcpy(val + ret, pool_name, len); | |
116 | ret += len; | |
117 | } | |
118 | if (pool_ns) { | |
119 | len = strlen(ns_field); | |
120 | memcpy(val + ret, ns_field, len); | |
121 | ret += len; | |
122 | memcpy(val + ret, pool_ns->str, pool_ns->len); | |
123 | ret += pool_ns->len; | |
1e5c6649 YZ |
124 | } |
125 | } | |
5aea3dcd | 126 | up_read(&osdc->lock); |
779fe0fb | 127 | ceph_put_string(pool_ns); |
32ab0bd7 SW |
128 | return ret; |
129 | } | |
130 | ||
695b7119 SW |
131 | static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci, |
132 | char *val, size_t size) | |
133 | { | |
7627151e | 134 | return snprintf(val, size, "%u", ci->i_layout.stripe_unit); |
695b7119 SW |
135 | } |
136 | ||
137 | static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci, | |
138 | char *val, size_t size) | |
139 | { | |
7627151e | 140 | return snprintf(val, size, "%u", ci->i_layout.stripe_count); |
695b7119 SW |
141 | } |
142 | ||
143 | static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci, | |
144 | char *val, size_t size) | |
145 | { | |
7627151e | 146 | return snprintf(val, size, "%u", ci->i_layout.object_size); |
695b7119 SW |
147 | } |
148 | ||
149 | static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci, | |
150 | char *val, size_t size) | |
151 | { | |
152 | int ret; | |
153 | struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); | |
154 | struct ceph_osd_client *osdc = &fsc->client->osdc; | |
7627151e | 155 | s64 pool = ci->i_layout.pool_id; |
695b7119 SW |
156 | const char *pool_name; |
157 | ||
5aea3dcd | 158 | down_read(&osdc->lock); |
695b7119 SW |
159 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); |
160 | if (pool_name) | |
161 | ret = snprintf(val, size, "%s", pool_name); | |
162 | else | |
163 | ret = snprintf(val, size, "%lld", (unsigned long long)pool); | |
5aea3dcd | 164 | up_read(&osdc->lock); |
695b7119 SW |
165 | return ret; |
166 | } | |
167 | ||
779fe0fb YZ |
168 | static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci, |
169 | char *val, size_t size) | |
170 | { | |
171 | int ret = 0; | |
172 | struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns); | |
173 | if (ns) { | |
174 | ret = snprintf(val, size, "%.*s", (int)ns->len, ns->str); | |
175 | ceph_put_string(ns); | |
176 | } | |
177 | return ret; | |
178 | } | |
179 | ||
355da1eb SW |
180 | /* directories */ |
181 | ||
aa4066ed | 182 | static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
183 | size_t size) |
184 | { | |
185 | return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs); | |
186 | } | |
187 | ||
aa4066ed | 188 | static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
189 | size_t size) |
190 | { | |
191 | return snprintf(val, size, "%lld", ci->i_files); | |
192 | } | |
193 | ||
aa4066ed | 194 | static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
195 | size_t size) |
196 | { | |
197 | return snprintf(val, size, "%lld", ci->i_subdirs); | |
198 | } | |
199 | ||
aa4066ed | 200 | static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
201 | size_t size) |
202 | { | |
203 | return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs); | |
204 | } | |
205 | ||
aa4066ed | 206 | static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
207 | size_t size) |
208 | { | |
209 | return snprintf(val, size, "%lld", ci->i_rfiles); | |
210 | } | |
211 | ||
aa4066ed | 212 | static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
213 | size_t size) |
214 | { | |
215 | return snprintf(val, size, "%lld", ci->i_rsubdirs); | |
216 | } | |
217 | ||
aa4066ed | 218 | static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
219 | size_t size) |
220 | { | |
221 | return snprintf(val, size, "%lld", ci->i_rbytes); | |
222 | } | |
223 | ||
aa4066ed | 224 | static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
225 | size_t size) |
226 | { | |
71880728 | 227 | return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec, |
9bbeab41 | 228 | ci->i_rctime.tv_nsec); |
355da1eb SW |
229 | } |
230 | ||
08796873 YZ |
231 | /* dir pin */ |
232 | static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci) | |
233 | { | |
234 | return ci->i_dir_pin != -ENODATA; | |
235 | } | |
236 | ||
237 | static size_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val, | |
238 | size_t size) | |
239 | { | |
240 | return snprintf(val, size, "%d", (int)ci->i_dir_pin); | |
241 | } | |
fb18a575 | 242 | |
08796873 | 243 | /* quotas */ |
fb18a575 LH |
244 | static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci) |
245 | { | |
f1919826 YZ |
246 | bool ret = false; |
247 | spin_lock(&ci->i_ceph_lock); | |
248 | if ((ci->i_max_files || ci->i_max_bytes) && | |
249 | ci->i_vino.snap == CEPH_NOSNAP && | |
250 | ci->i_snap_realm && | |
251 | ci->i_snap_realm->ino == ci->i_vino.ino) | |
252 | ret = true; | |
253 | spin_unlock(&ci->i_ceph_lock); | |
254 | return ret; | |
fb18a575 LH |
255 | } |
256 | ||
257 | static size_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val, | |
258 | size_t size) | |
259 | { | |
260 | return snprintf(val, size, "max_bytes=%llu max_files=%llu", | |
261 | ci->i_max_bytes, ci->i_max_files); | |
262 | } | |
263 | ||
264 | static size_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci, | |
265 | char *val, size_t size) | |
266 | { | |
267 | return snprintf(val, size, "%llu", ci->i_max_bytes); | |
268 | } | |
269 | ||
270 | static size_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci, | |
271 | char *val, size_t size) | |
272 | { | |
273 | return snprintf(val, size, "%llu", ci->i_max_files); | |
274 | } | |
32ab0bd7 | 275 | |
100cc610 DD |
276 | /* snapshots */ |
277 | static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci) | |
278 | { | |
279 | return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0); | |
280 | } | |
281 | ||
282 | static size_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val, | |
283 | size_t size) | |
284 | { | |
285 | return snprintf(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec, | |
286 | ci->i_snap_btime.tv_nsec); | |
287 | } | |
288 | ||
eb788084 | 289 | #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name |
695b7119 SW |
290 | #define CEPH_XATTR_NAME2(_type, _name, _name2) \ |
291 | XATTR_CEPH_PREFIX #_type "." #_name "." #_name2 | |
eb788084 | 292 | |
49a9f4f6 | 293 | #define XATTR_NAME_CEPH(_type, _name, _flags) \ |
8860147a SW |
294 | { \ |
295 | .name = CEPH_XATTR_NAME(_type, _name), \ | |
296 | .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ | |
297 | .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ | |
49a9f4f6 YZ |
298 | .exists_cb = NULL, \ |
299 | .flags = (VXATTR_FLAG_READONLY | _flags), \ | |
8860147a | 300 | } |
49a9f4f6 YZ |
301 | #define XATTR_RSTAT_FIELD(_type, _name) \ |
302 | XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT) | |
695b7119 SW |
303 | #define XATTR_LAYOUT_FIELD(_type, _name, _field) \ |
304 | { \ | |
305 | .name = CEPH_XATTR_NAME2(_type, _name, _field), \ | |
306 | .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \ | |
307 | .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \ | |
695b7119 | 308 | .exists_cb = ceph_vxattrcb_layout_exists, \ |
4e9906e7 | 309 | .flags = VXATTR_FLAG_HIDDEN, \ |
695b7119 | 310 | } |
fb18a575 LH |
311 | #define XATTR_QUOTA_FIELD(_type, _name) \ |
312 | { \ | |
313 | .name = CEPH_XATTR_NAME(_type, _name), \ | |
314 | .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \ | |
315 | .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ | |
fb18a575 | 316 | .exists_cb = ceph_vxattrcb_quota_exists, \ |
4e9906e7 | 317 | .flags = VXATTR_FLAG_HIDDEN, \ |
fb18a575 | 318 | } |
eb788084 | 319 | |
881a5fa2 | 320 | static struct ceph_vxattr ceph_dir_vxattrs[] = { |
1f08f2b0 SW |
321 | { |
322 | .name = "ceph.dir.layout", | |
323 | .name_size = sizeof("ceph.dir.layout"), | |
324 | .getxattr_cb = ceph_vxattrcb_layout, | |
1f08f2b0 | 325 | .exists_cb = ceph_vxattrcb_layout_exists, |
4e9906e7 | 326 | .flags = VXATTR_FLAG_HIDDEN, |
1f08f2b0 | 327 | }, |
695b7119 SW |
328 | XATTR_LAYOUT_FIELD(dir, layout, stripe_unit), |
329 | XATTR_LAYOUT_FIELD(dir, layout, stripe_count), | |
330 | XATTR_LAYOUT_FIELD(dir, layout, object_size), | |
331 | XATTR_LAYOUT_FIELD(dir, layout, pool), | |
779fe0fb | 332 | XATTR_LAYOUT_FIELD(dir, layout, pool_namespace), |
49a9f4f6 YZ |
333 | XATTR_NAME_CEPH(dir, entries, 0), |
334 | XATTR_NAME_CEPH(dir, files, 0), | |
335 | XATTR_NAME_CEPH(dir, subdirs, 0), | |
336 | XATTR_RSTAT_FIELD(dir, rentries), | |
337 | XATTR_RSTAT_FIELD(dir, rfiles), | |
338 | XATTR_RSTAT_FIELD(dir, rsubdirs), | |
339 | XATTR_RSTAT_FIELD(dir, rbytes), | |
340 | XATTR_RSTAT_FIELD(dir, rctime), | |
08796873 YZ |
341 | { |
342 | .name = "ceph.dir.pin", | |
e1b81439 | 343 | .name_size = sizeof("ceph.dir.pin"), |
08796873 YZ |
344 | .getxattr_cb = ceph_vxattrcb_dir_pin, |
345 | .exists_cb = ceph_vxattrcb_dir_pin_exists, | |
346 | .flags = VXATTR_FLAG_HIDDEN, | |
347 | }, | |
fb18a575 LH |
348 | { |
349 | .name = "ceph.quota", | |
350 | .name_size = sizeof("ceph.quota"), | |
351 | .getxattr_cb = ceph_vxattrcb_quota, | |
fb18a575 | 352 | .exists_cb = ceph_vxattrcb_quota_exists, |
4e9906e7 | 353 | .flags = VXATTR_FLAG_HIDDEN, |
fb18a575 LH |
354 | }, |
355 | XATTR_QUOTA_FIELD(quota, max_bytes), | |
356 | XATTR_QUOTA_FIELD(quota, max_files), | |
100cc610 DD |
357 | { |
358 | .name = "ceph.snap.btime", | |
359 | .name_size = sizeof("ceph.snap.btime"), | |
360 | .getxattr_cb = ceph_vxattrcb_snap_btime, | |
361 | .exists_cb = ceph_vxattrcb_snap_btime_exists, | |
362 | .flags = VXATTR_FLAG_READONLY, | |
363 | }, | |
2c3dd4ff | 364 | { .name = NULL, 0 } /* Required table terminator */ |
355da1eb SW |
365 | }; |
366 | ||
367 | /* files */ | |
368 | ||
881a5fa2 | 369 | static struct ceph_vxattr ceph_file_vxattrs[] = { |
32ab0bd7 SW |
370 | { |
371 | .name = "ceph.file.layout", | |
372 | .name_size = sizeof("ceph.file.layout"), | |
373 | .getxattr_cb = ceph_vxattrcb_layout, | |
32ab0bd7 | 374 | .exists_cb = ceph_vxattrcb_layout_exists, |
4e9906e7 | 375 | .flags = VXATTR_FLAG_HIDDEN, |
32ab0bd7 | 376 | }, |
695b7119 SW |
377 | XATTR_LAYOUT_FIELD(file, layout, stripe_unit), |
378 | XATTR_LAYOUT_FIELD(file, layout, stripe_count), | |
379 | XATTR_LAYOUT_FIELD(file, layout, object_size), | |
380 | XATTR_LAYOUT_FIELD(file, layout, pool), | |
779fe0fb | 381 | XATTR_LAYOUT_FIELD(file, layout, pool_namespace), |
100cc610 DD |
382 | { |
383 | .name = "ceph.snap.btime", | |
384 | .name_size = sizeof("ceph.snap.btime"), | |
385 | .getxattr_cb = ceph_vxattrcb_snap_btime, | |
386 | .exists_cb = ceph_vxattrcb_snap_btime_exists, | |
387 | .flags = VXATTR_FLAG_READONLY, | |
388 | }, | |
2c3dd4ff | 389 | { .name = NULL, 0 } /* Required table terminator */ |
355da1eb SW |
390 | }; |
391 | ||
881a5fa2 | 392 | static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) |
355da1eb SW |
393 | { |
394 | if (S_ISDIR(inode->i_mode)) | |
395 | return ceph_dir_vxattrs; | |
396 | else if (S_ISREG(inode->i_mode)) | |
397 | return ceph_file_vxattrs; | |
398 | return NULL; | |
399 | } | |
400 | ||
881a5fa2 | 401 | static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, |
355da1eb SW |
402 | const char *name) |
403 | { | |
881a5fa2 | 404 | struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); |
06476a69 AE |
405 | |
406 | if (vxattr) { | |
407 | while (vxattr->name) { | |
408 | if (!strcmp(vxattr->name, name)) | |
409 | return vxattr; | |
410 | vxattr++; | |
411 | } | |
412 | } | |
413 | ||
355da1eb SW |
414 | return NULL; |
415 | } | |
416 | ||
417 | static int __set_xattr(struct ceph_inode_info *ci, | |
418 | const char *name, int name_len, | |
419 | const char *val, int val_len, | |
fbc0b970 | 420 | int flags, int update_xattr, |
355da1eb SW |
421 | struct ceph_inode_xattr **newxattr) |
422 | { | |
423 | struct rb_node **p; | |
424 | struct rb_node *parent = NULL; | |
425 | struct ceph_inode_xattr *xattr = NULL; | |
426 | int c; | |
427 | int new = 0; | |
428 | ||
429 | p = &ci->i_xattrs.index.rb_node; | |
430 | while (*p) { | |
431 | parent = *p; | |
432 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); | |
433 | c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); | |
434 | if (c < 0) | |
435 | p = &(*p)->rb_left; | |
436 | else if (c > 0) | |
437 | p = &(*p)->rb_right; | |
438 | else { | |
439 | if (name_len == xattr->name_len) | |
440 | break; | |
441 | else if (name_len < xattr->name_len) | |
442 | p = &(*p)->rb_left; | |
443 | else | |
444 | p = &(*p)->rb_right; | |
445 | } | |
446 | xattr = NULL; | |
447 | } | |
448 | ||
fbc0b970 YZ |
449 | if (update_xattr) { |
450 | int err = 0; | |
eeca958d | 451 | |
fbc0b970 YZ |
452 | if (xattr && (flags & XATTR_CREATE)) |
453 | err = -EEXIST; | |
454 | else if (!xattr && (flags & XATTR_REPLACE)) | |
455 | err = -ENODATA; | |
456 | if (err) { | |
457 | kfree(name); | |
458 | kfree(val); | |
eeca958d | 459 | kfree(*newxattr); |
fbc0b970 YZ |
460 | return err; |
461 | } | |
bcdfeb2e YZ |
462 | if (update_xattr < 0) { |
463 | if (xattr) | |
464 | __remove_xattr(ci, xattr); | |
465 | kfree(name); | |
eeca958d | 466 | kfree(*newxattr); |
bcdfeb2e YZ |
467 | return 0; |
468 | } | |
fbc0b970 YZ |
469 | } |
470 | ||
355da1eb SW |
471 | if (!xattr) { |
472 | new = 1; | |
473 | xattr = *newxattr; | |
474 | xattr->name = name; | |
475 | xattr->name_len = name_len; | |
fbc0b970 | 476 | xattr->should_free_name = update_xattr; |
355da1eb SW |
477 | |
478 | ci->i_xattrs.count++; | |
479 | dout("__set_xattr count=%d\n", ci->i_xattrs.count); | |
480 | } else { | |
481 | kfree(*newxattr); | |
482 | *newxattr = NULL; | |
483 | if (xattr->should_free_val) | |
484 | kfree((void *)xattr->val); | |
485 | ||
fbc0b970 | 486 | if (update_xattr) { |
355da1eb SW |
487 | kfree((void *)name); |
488 | name = xattr->name; | |
489 | } | |
490 | ci->i_xattrs.names_size -= xattr->name_len; | |
491 | ci->i_xattrs.vals_size -= xattr->val_len; | |
492 | } | |
355da1eb SW |
493 | ci->i_xattrs.names_size += name_len; |
494 | ci->i_xattrs.vals_size += val_len; | |
495 | if (val) | |
496 | xattr->val = val; | |
497 | else | |
498 | xattr->val = ""; | |
499 | ||
500 | xattr->val_len = val_len; | |
fbc0b970 YZ |
501 | xattr->dirty = update_xattr; |
502 | xattr->should_free_val = (val && update_xattr); | |
355da1eb SW |
503 | |
504 | if (new) { | |
505 | rb_link_node(&xattr->node, parent, p); | |
506 | rb_insert_color(&xattr->node, &ci->i_xattrs.index); | |
507 | dout("__set_xattr_val p=%p\n", p); | |
508 | } | |
509 | ||
05729781 YZ |
510 | dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n", |
511 | ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val); | |
355da1eb SW |
512 | |
513 | return 0; | |
514 | } | |
515 | ||
516 | static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, | |
517 | const char *name) | |
518 | { | |
519 | struct rb_node **p; | |
520 | struct rb_node *parent = NULL; | |
521 | struct ceph_inode_xattr *xattr = NULL; | |
17db143f | 522 | int name_len = strlen(name); |
355da1eb SW |
523 | int c; |
524 | ||
525 | p = &ci->i_xattrs.index.rb_node; | |
526 | while (*p) { | |
527 | parent = *p; | |
528 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); | |
529 | c = strncmp(name, xattr->name, xattr->name_len); | |
17db143f SW |
530 | if (c == 0 && name_len > xattr->name_len) |
531 | c = 1; | |
355da1eb SW |
532 | if (c < 0) |
533 | p = &(*p)->rb_left; | |
534 | else if (c > 0) | |
535 | p = &(*p)->rb_right; | |
536 | else { | |
537 | dout("__get_xattr %s: found %.*s\n", name, | |
538 | xattr->val_len, xattr->val); | |
539 | return xattr; | |
540 | } | |
541 | } | |
542 | ||
543 | dout("__get_xattr %s: not found\n", name); | |
544 | ||
545 | return NULL; | |
546 | } | |
547 | ||
548 | static void __free_xattr(struct ceph_inode_xattr *xattr) | |
549 | { | |
550 | BUG_ON(!xattr); | |
551 | ||
552 | if (xattr->should_free_name) | |
553 | kfree((void *)xattr->name); | |
554 | if (xattr->should_free_val) | |
555 | kfree((void *)xattr->val); | |
556 | ||
557 | kfree(xattr); | |
558 | } | |
559 | ||
560 | static int __remove_xattr(struct ceph_inode_info *ci, | |
561 | struct ceph_inode_xattr *xattr) | |
562 | { | |
563 | if (!xattr) | |
524186ac | 564 | return -ENODATA; |
355da1eb SW |
565 | |
566 | rb_erase(&xattr->node, &ci->i_xattrs.index); | |
567 | ||
568 | if (xattr->should_free_name) | |
569 | kfree((void *)xattr->name); | |
570 | if (xattr->should_free_val) | |
571 | kfree((void *)xattr->val); | |
572 | ||
573 | ci->i_xattrs.names_size -= xattr->name_len; | |
574 | ci->i_xattrs.vals_size -= xattr->val_len; | |
575 | ci->i_xattrs.count--; | |
576 | kfree(xattr); | |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
355da1eb SW |
581 | static char *__copy_xattr_names(struct ceph_inode_info *ci, |
582 | char *dest) | |
583 | { | |
584 | struct rb_node *p; | |
585 | struct ceph_inode_xattr *xattr = NULL; | |
586 | ||
587 | p = rb_first(&ci->i_xattrs.index); | |
588 | dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); | |
589 | ||
590 | while (p) { | |
591 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | |
592 | memcpy(dest, xattr->name, xattr->name_len); | |
593 | dest[xattr->name_len] = '\0'; | |
594 | ||
595 | dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, | |
596 | xattr->name_len, ci->i_xattrs.names_size); | |
597 | ||
598 | dest += xattr->name_len + 1; | |
599 | p = rb_next(p); | |
600 | } | |
601 | ||
602 | return dest; | |
603 | } | |
604 | ||
605 | void __ceph_destroy_xattrs(struct ceph_inode_info *ci) | |
606 | { | |
607 | struct rb_node *p, *tmp; | |
608 | struct ceph_inode_xattr *xattr = NULL; | |
609 | ||
610 | p = rb_first(&ci->i_xattrs.index); | |
611 | ||
612 | dout("__ceph_destroy_xattrs p=%p\n", p); | |
613 | ||
614 | while (p) { | |
615 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | |
616 | tmp = p; | |
617 | p = rb_next(tmp); | |
618 | dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, | |
619 | xattr->name_len, xattr->name); | |
620 | rb_erase(tmp, &ci->i_xattrs.index); | |
621 | ||
622 | __free_xattr(xattr); | |
623 | } | |
624 | ||
625 | ci->i_xattrs.names_size = 0; | |
626 | ci->i_xattrs.vals_size = 0; | |
627 | ci->i_xattrs.index_version = 0; | |
628 | ci->i_xattrs.count = 0; | |
629 | ci->i_xattrs.index = RB_ROOT; | |
630 | } | |
631 | ||
632 | static int __build_xattrs(struct inode *inode) | |
be655596 SW |
633 | __releases(ci->i_ceph_lock) |
634 | __acquires(ci->i_ceph_lock) | |
355da1eb SW |
635 | { |
636 | u32 namelen; | |
637 | u32 numattr = 0; | |
638 | void *p, *end; | |
639 | u32 len; | |
640 | const char *name, *val; | |
641 | struct ceph_inode_info *ci = ceph_inode(inode); | |
642 | int xattr_version; | |
643 | struct ceph_inode_xattr **xattrs = NULL; | |
63ff78b2 | 644 | int err = 0; |
355da1eb SW |
645 | int i; |
646 | ||
647 | dout("__build_xattrs() len=%d\n", | |
648 | ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); | |
649 | ||
650 | if (ci->i_xattrs.index_version >= ci->i_xattrs.version) | |
651 | return 0; /* already built */ | |
652 | ||
653 | __ceph_destroy_xattrs(ci); | |
654 | ||
655 | start: | |
656 | /* updated internal xattr rb tree */ | |
657 | if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { | |
658 | p = ci->i_xattrs.blob->vec.iov_base; | |
659 | end = p + ci->i_xattrs.blob->vec.iov_len; | |
660 | ceph_decode_32_safe(&p, end, numattr, bad); | |
661 | xattr_version = ci->i_xattrs.version; | |
be655596 | 662 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 663 | |
7e8a2952 | 664 | xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *), |
355da1eb SW |
665 | GFP_NOFS); |
666 | err = -ENOMEM; | |
667 | if (!xattrs) | |
668 | goto bad_lock; | |
1a295bd8 | 669 | |
355da1eb SW |
670 | for (i = 0; i < numattr; i++) { |
671 | xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), | |
672 | GFP_NOFS); | |
673 | if (!xattrs[i]) | |
674 | goto bad_lock; | |
675 | } | |
676 | ||
be655596 | 677 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
678 | if (ci->i_xattrs.version != xattr_version) { |
679 | /* lost a race, retry */ | |
680 | for (i = 0; i < numattr; i++) | |
681 | kfree(xattrs[i]); | |
682 | kfree(xattrs); | |
21ec6ffa | 683 | xattrs = NULL; |
355da1eb SW |
684 | goto start; |
685 | } | |
686 | err = -EIO; | |
687 | while (numattr--) { | |
688 | ceph_decode_32_safe(&p, end, len, bad); | |
689 | namelen = len; | |
690 | name = p; | |
691 | p += len; | |
692 | ceph_decode_32_safe(&p, end, len, bad); | |
693 | val = p; | |
694 | p += len; | |
695 | ||
696 | err = __set_xattr(ci, name, namelen, val, len, | |
fbc0b970 | 697 | 0, 0, &xattrs[numattr]); |
355da1eb SW |
698 | |
699 | if (err < 0) | |
700 | goto bad; | |
701 | } | |
702 | kfree(xattrs); | |
703 | } | |
704 | ci->i_xattrs.index_version = ci->i_xattrs.version; | |
705 | ci->i_xattrs.dirty = false; | |
706 | ||
707 | return err; | |
708 | bad_lock: | |
be655596 | 709 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
710 | bad: |
711 | if (xattrs) { | |
712 | for (i = 0; i < numattr; i++) | |
713 | kfree(xattrs[i]); | |
714 | kfree(xattrs); | |
715 | } | |
716 | ci->i_xattrs.names_size = 0; | |
717 | return err; | |
718 | } | |
719 | ||
720 | static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, | |
721 | int val_size) | |
722 | { | |
723 | /* | |
724 | * 4 bytes for the length, and additional 4 bytes per each xattr name, | |
725 | * 4 bytes per each value | |
726 | */ | |
727 | int size = 4 + ci->i_xattrs.count*(4 + 4) + | |
728 | ci->i_xattrs.names_size + | |
729 | ci->i_xattrs.vals_size; | |
730 | dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", | |
731 | ci->i_xattrs.count, ci->i_xattrs.names_size, | |
732 | ci->i_xattrs.vals_size); | |
733 | ||
734 | if (name_size) | |
735 | size += 4 + 4 + name_size + val_size; | |
736 | ||
737 | return size; | |
738 | } | |
739 | ||
740 | /* | |
741 | * If there are dirty xattrs, reencode xattrs into the prealloc_blob | |
742 | * and swap into place. | |
743 | */ | |
744 | void __ceph_build_xattrs_blob(struct ceph_inode_info *ci) | |
745 | { | |
746 | struct rb_node *p; | |
747 | struct ceph_inode_xattr *xattr = NULL; | |
748 | void *dest; | |
749 | ||
750 | dout("__build_xattrs_blob %p\n", &ci->vfs_inode); | |
751 | if (ci->i_xattrs.dirty) { | |
752 | int need = __get_required_blob_size(ci, 0, 0); | |
753 | ||
754 | BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); | |
755 | ||
756 | p = rb_first(&ci->i_xattrs.index); | |
757 | dest = ci->i_xattrs.prealloc_blob->vec.iov_base; | |
758 | ||
759 | ceph_encode_32(&dest, ci->i_xattrs.count); | |
760 | while (p) { | |
761 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | |
762 | ||
763 | ceph_encode_32(&dest, xattr->name_len); | |
764 | memcpy(dest, xattr->name, xattr->name_len); | |
765 | dest += xattr->name_len; | |
766 | ceph_encode_32(&dest, xattr->val_len); | |
767 | memcpy(dest, xattr->val, xattr->val_len); | |
768 | dest += xattr->val_len; | |
769 | ||
770 | p = rb_next(p); | |
771 | } | |
772 | ||
773 | /* adjust buffer len; it may be larger than we need */ | |
774 | ci->i_xattrs.prealloc_blob->vec.iov_len = | |
775 | dest - ci->i_xattrs.prealloc_blob->vec.iov_base; | |
776 | ||
b6c1d5b8 SW |
777 | if (ci->i_xattrs.blob) |
778 | ceph_buffer_put(ci->i_xattrs.blob); | |
355da1eb SW |
779 | ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; |
780 | ci->i_xattrs.prealloc_blob = NULL; | |
781 | ci->i_xattrs.dirty = false; | |
4a625be4 | 782 | ci->i_xattrs.version++; |
355da1eb SW |
783 | } |
784 | } | |
785 | ||
315f2408 YZ |
786 | static inline int __get_request_mask(struct inode *in) { |
787 | struct ceph_mds_request *req = current->journal_info; | |
788 | int mask = 0; | |
789 | if (req && req->r_target_inode == in) { | |
790 | if (req->r_op == CEPH_MDS_OP_LOOKUP || | |
791 | req->r_op == CEPH_MDS_OP_LOOKUPINO || | |
792 | req->r_op == CEPH_MDS_OP_LOOKUPPARENT || | |
793 | req->r_op == CEPH_MDS_OP_GETATTR) { | |
794 | mask = le32_to_cpu(req->r_args.getattr.mask); | |
795 | } else if (req->r_op == CEPH_MDS_OP_OPEN || | |
796 | req->r_op == CEPH_MDS_OP_CREATE) { | |
797 | mask = le32_to_cpu(req->r_args.open.mask); | |
798 | } | |
799 | } | |
800 | return mask; | |
801 | } | |
802 | ||
7221fe4c | 803 | ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value, |
355da1eb SW |
804 | size_t size) |
805 | { | |
355da1eb | 806 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb | 807 | struct ceph_inode_xattr *xattr; |
881a5fa2 | 808 | struct ceph_vxattr *vxattr = NULL; |
315f2408 YZ |
809 | int req_mask; |
810 | int err; | |
355da1eb | 811 | |
0bee82fb SW |
812 | /* let's see if a virtual xattr was requested */ |
813 | vxattr = ceph_match_vxattr(inode, name); | |
29dccfa5 | 814 | if (vxattr) { |
49a9f4f6 YZ |
815 | int mask = 0; |
816 | if (vxattr->flags & VXATTR_FLAG_RSTAT) | |
817 | mask |= CEPH_STAT_RSTAT; | |
818 | err = ceph_do_getattr(inode, mask, true); | |
1684dd03 YZ |
819 | if (err) |
820 | return err; | |
29dccfa5 YZ |
821 | err = -ENODATA; |
822 | if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) | |
823 | err = vxattr->getxattr_cb(ci, value, size); | |
a1dc1937 | 824 | return err; |
0bee82fb SW |
825 | } |
826 | ||
315f2408 YZ |
827 | req_mask = __get_request_mask(inode); |
828 | ||
a1dc1937 | 829 | spin_lock(&ci->i_ceph_lock); |
830 | dout("getxattr %p ver=%lld index_ver=%lld\n", inode, | |
831 | ci->i_xattrs.version, ci->i_xattrs.index_version); | |
832 | ||
508b32d8 | 833 | if (ci->i_xattrs.version == 0 || |
315f2408 YZ |
834 | !((req_mask & CEPH_CAP_XATTR_SHARED) || |
835 | __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) { | |
be655596 | 836 | spin_unlock(&ci->i_ceph_lock); |
315f2408 YZ |
837 | |
838 | /* security module gets xattr while filling trace */ | |
d37b1d99 | 839 | if (current->journal_info) { |
315f2408 YZ |
840 | pr_warn_ratelimited("sync getxattr %p " |
841 | "during filling trace\n", inode); | |
842 | return -EBUSY; | |
843 | } | |
844 | ||
355da1eb | 845 | /* get xattrs from mds (if we don't already have them) */ |
508b32d8 | 846 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); |
355da1eb SW |
847 | if (err) |
848 | return err; | |
508b32d8 | 849 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
850 | } |
851 | ||
355da1eb SW |
852 | err = __build_xattrs(inode); |
853 | if (err < 0) | |
854 | goto out; | |
855 | ||
355da1eb SW |
856 | err = -ENODATA; /* == ENOATTR */ |
857 | xattr = __get_xattr(ci, name); | |
0bee82fb | 858 | if (!xattr) |
355da1eb | 859 | goto out; |
355da1eb SW |
860 | |
861 | err = -ERANGE; | |
862 | if (size && size < xattr->val_len) | |
863 | goto out; | |
864 | ||
865 | err = xattr->val_len; | |
866 | if (size == 0) | |
867 | goto out; | |
868 | ||
869 | memcpy(value, xattr->val, xattr->val_len); | |
870 | ||
d37b1d99 | 871 | if (current->journal_info && |
315f2408 YZ |
872 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
873 | ci->i_ceph_flags |= CEPH_I_SEC_INITED; | |
355da1eb | 874 | out: |
be655596 | 875 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
876 | return err; |
877 | } | |
878 | ||
879 | ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) | |
880 | { | |
2b0143b5 | 881 | struct inode *inode = d_inode(dentry); |
355da1eb | 882 | struct ceph_inode_info *ci = ceph_inode(inode); |
881a5fa2 | 883 | struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode); |
2b2abcac | 884 | bool len_only = (size == 0); |
355da1eb SW |
885 | u32 namelen; |
886 | int err; | |
355da1eb SW |
887 | int i; |
888 | ||
be655596 | 889 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
890 | dout("listxattr %p ver=%lld index_ver=%lld\n", inode, |
891 | ci->i_xattrs.version, ci->i_xattrs.index_version); | |
892 | ||
508b32d8 YZ |
893 | if (ci->i_xattrs.version == 0 || |
894 | !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) { | |
be655596 | 895 | spin_unlock(&ci->i_ceph_lock); |
508b32d8 | 896 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); |
355da1eb SW |
897 | if (err) |
898 | return err; | |
508b32d8 | 899 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
900 | } |
901 | ||
355da1eb SW |
902 | err = __build_xattrs(inode); |
903 | if (err < 0) | |
904 | goto out; | |
3ce6cd12 | 905 | |
2b2abcac | 906 | /* add 1 byte for each xattr due to the null termination */ |
b65917dd | 907 | namelen = ci->i_xattrs.names_size + ci->i_xattrs.count; |
2b2abcac DD |
908 | if (!len_only) { |
909 | if (namelen > size) { | |
910 | err = -ERANGE; | |
911 | goto out; | |
912 | } | |
913 | names = __copy_xattr_names(ci, names); | |
914 | size -= namelen; | |
915 | } | |
355da1eb | 916 | |
355da1eb SW |
917 | |
918 | /* virtual xattr names, too */ | |
b65917dd | 919 | if (vxattrs) { |
355da1eb | 920 | for (i = 0; vxattrs[i].name; i++) { |
2b2abcac DD |
921 | size_t this_len; |
922 | ||
923 | if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN) | |
924 | continue; | |
925 | if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci)) | |
926 | continue; | |
927 | ||
928 | this_len = strlen(vxattrs[i].name) + 1; | |
929 | namelen += this_len; | |
930 | if (len_only) | |
931 | continue; | |
932 | ||
933 | if (this_len > size) { | |
934 | err = -ERANGE; | |
935 | goto out; | |
b65917dd | 936 | } |
2b2abcac DD |
937 | |
938 | memcpy(names, vxattrs[i].name, this_len); | |
939 | names += this_len; | |
940 | size -= this_len; | |
355da1eb | 941 | } |
b65917dd | 942 | } |
2b2abcac | 943 | err = namelen; |
355da1eb | 944 | out: |
be655596 | 945 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
946 | return err; |
947 | } | |
948 | ||
a26fecca | 949 | static int ceph_sync_setxattr(struct inode *inode, const char *name, |
355da1eb SW |
950 | const char *value, size_t size, int flags) |
951 | { | |
a26fecca | 952 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); |
355da1eb | 953 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb | 954 | struct ceph_mds_request *req; |
3d14c5d2 | 955 | struct ceph_mds_client *mdsc = fsc->mdsc; |
25e6bae3 | 956 | struct ceph_pagelist *pagelist = NULL; |
04303d8a | 957 | int op = CEPH_MDS_OP_SETXATTR; |
355da1eb | 958 | int err; |
25e6bae3 | 959 | |
0aeff37a | 960 | if (size > 0) { |
25e6bae3 | 961 | /* copy value into pagelist */ |
33165d47 | 962 | pagelist = ceph_pagelist_alloc(GFP_NOFS); |
25e6bae3 | 963 | if (!pagelist) |
355da1eb | 964 | return -ENOMEM; |
25e6bae3 | 965 | |
25e6bae3 YZ |
966 | err = ceph_pagelist_append(pagelist, value, size); |
967 | if (err) | |
968 | goto out; | |
0aeff37a | 969 | } else if (!value) { |
04303d8a YZ |
970 | if (flags & CEPH_XATTR_REPLACE) |
971 | op = CEPH_MDS_OP_RMXATTR; | |
972 | else | |
973 | flags |= CEPH_XATTR_REMOVE; | |
355da1eb SW |
974 | } |
975 | ||
976 | dout("setxattr value=%.*s\n", (int)size, value); | |
977 | ||
978 | /* do request */ | |
04303d8a | 979 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
60d87733 JL |
980 | if (IS_ERR(req)) { |
981 | err = PTR_ERR(req); | |
982 | goto out; | |
983 | } | |
a149bb9a | 984 | |
355da1eb | 985 | req->r_path2 = kstrdup(name, GFP_NOFS); |
a149bb9a SK |
986 | if (!req->r_path2) { |
987 | ceph_mdsc_put_request(req); | |
988 | err = -ENOMEM; | |
989 | goto out; | |
990 | } | |
355da1eb | 991 | |
04303d8a YZ |
992 | if (op == CEPH_MDS_OP_SETXATTR) { |
993 | req->r_args.setxattr.flags = cpu_to_le32(flags); | |
994 | req->r_pagelist = pagelist; | |
995 | pagelist = NULL; | |
996 | } | |
355da1eb | 997 | |
a149bb9a SK |
998 | req->r_inode = inode; |
999 | ihold(inode); | |
1000 | req->r_num_caps = 1; | |
1001 | req->r_inode_drop = CEPH_CAP_XATTR_SHARED; | |
1002 | ||
355da1eb | 1003 | dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); |
752c8bdc | 1004 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
355da1eb SW |
1005 | ceph_mdsc_put_request(req); |
1006 | dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); | |
1007 | ||
1008 | out: | |
25e6bae3 YZ |
1009 | if (pagelist) |
1010 | ceph_pagelist_release(pagelist); | |
355da1eb SW |
1011 | return err; |
1012 | } | |
1013 | ||
a26fecca | 1014 | int __ceph_setxattr(struct inode *inode, const char *name, |
7221fe4c | 1015 | const void *value, size_t size, int flags) |
355da1eb | 1016 | { |
881a5fa2 | 1017 | struct ceph_vxattr *vxattr; |
355da1eb | 1018 | struct ceph_inode_info *ci = ceph_inode(inode); |
a26fecca | 1019 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
f66fd9f0 | 1020 | struct ceph_cap_flush *prealloc_cf = NULL; |
18fa8b3f | 1021 | int issued; |
355da1eb | 1022 | int err; |
fbc0b970 | 1023 | int dirty = 0; |
355da1eb SW |
1024 | int name_len = strlen(name); |
1025 | int val_len = size; | |
1026 | char *newname = NULL; | |
1027 | char *newval = NULL; | |
1028 | struct ceph_inode_xattr *xattr = NULL; | |
355da1eb | 1029 | int required_blob_size; |
f1919826 | 1030 | bool check_realm = false; |
604d1b02 | 1031 | bool lock_snap_rwsem = false; |
355da1eb | 1032 | |
2cdeb1e4 AG |
1033 | if (ceph_snap(inode) != CEPH_NOSNAP) |
1034 | return -EROFS; | |
355da1eb | 1035 | |
06476a69 | 1036 | vxattr = ceph_match_vxattr(inode, name); |
f1919826 | 1037 | if (vxattr) { |
4e9906e7 | 1038 | if (vxattr->flags & VXATTR_FLAG_READONLY) |
f1919826 YZ |
1039 | return -EOPNOTSUPP; |
1040 | if (value && !strncmp(vxattr->name, "ceph.quota", 10)) | |
1041 | check_realm = true; | |
1042 | } | |
355da1eb | 1043 | |
3adf654d SW |
1044 | /* pass any unhandled ceph.* xattrs through to the MDS */ |
1045 | if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) | |
1046 | goto do_sync_unlocked; | |
1047 | ||
355da1eb SW |
1048 | /* preallocate memory for xattr name, value, index node */ |
1049 | err = -ENOMEM; | |
61413c2f | 1050 | newname = kmemdup(name, name_len + 1, GFP_NOFS); |
355da1eb SW |
1051 | if (!newname) |
1052 | goto out; | |
355da1eb SW |
1053 | |
1054 | if (val_len) { | |
b829c195 | 1055 | newval = kmemdup(value, val_len, GFP_NOFS); |
355da1eb SW |
1056 | if (!newval) |
1057 | goto out; | |
355da1eb SW |
1058 | } |
1059 | ||
1060 | xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); | |
1061 | if (!xattr) | |
1062 | goto out; | |
1063 | ||
f66fd9f0 YZ |
1064 | prealloc_cf = ceph_alloc_cap_flush(); |
1065 | if (!prealloc_cf) | |
1066 | goto out; | |
1067 | ||
be655596 | 1068 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
1069 | retry: |
1070 | issued = __ceph_caps_issued(ci, NULL); | |
508b32d8 | 1071 | if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) |
355da1eb | 1072 | goto do_sync; |
604d1b02 YZ |
1073 | |
1074 | if (!lock_snap_rwsem && !ci->i_head_snapc) { | |
1075 | lock_snap_rwsem = true; | |
1076 | if (!down_read_trylock(&mdsc->snap_rwsem)) { | |
1077 | spin_unlock(&ci->i_ceph_lock); | |
1078 | down_read(&mdsc->snap_rwsem); | |
1079 | spin_lock(&ci->i_ceph_lock); | |
1080 | goto retry; | |
1081 | } | |
1082 | } | |
1083 | ||
1084 | dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued)); | |
355da1eb SW |
1085 | __build_xattrs(inode); |
1086 | ||
1087 | required_blob_size = __get_required_blob_size(ci, name_len, val_len); | |
1088 | ||
1089 | if (!ci->i_xattrs.prealloc_blob || | |
1090 | required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { | |
18fa8b3f | 1091 | struct ceph_buffer *blob; |
355da1eb | 1092 | |
be655596 | 1093 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 1094 | dout(" preaallocating new blob size=%d\n", required_blob_size); |
b6c1d5b8 | 1095 | blob = ceph_buffer_new(required_blob_size, GFP_NOFS); |
355da1eb | 1096 | if (!blob) |
604d1b02 | 1097 | goto do_sync_unlocked; |
be655596 | 1098 | spin_lock(&ci->i_ceph_lock); |
b6c1d5b8 SW |
1099 | if (ci->i_xattrs.prealloc_blob) |
1100 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | |
355da1eb SW |
1101 | ci->i_xattrs.prealloc_blob = blob; |
1102 | goto retry; | |
1103 | } | |
1104 | ||
bcdfeb2e YZ |
1105 | err = __set_xattr(ci, newname, name_len, newval, val_len, |
1106 | flags, value ? 1 : -1, &xattr); | |
18fa8b3f | 1107 | |
fbc0b970 | 1108 | if (!err) { |
f66fd9f0 YZ |
1109 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL, |
1110 | &prealloc_cf); | |
fbc0b970 | 1111 | ci->i_xattrs.dirty = true; |
c2050a45 | 1112 | inode->i_ctime = current_time(inode); |
fbc0b970 | 1113 | } |
18fa8b3f | 1114 | |
be655596 | 1115 | spin_unlock(&ci->i_ceph_lock); |
604d1b02 YZ |
1116 | if (lock_snap_rwsem) |
1117 | up_read(&mdsc->snap_rwsem); | |
fca65b4a SW |
1118 | if (dirty) |
1119 | __mark_inode_dirty(inode, dirty); | |
f66fd9f0 | 1120 | ceph_free_cap_flush(prealloc_cf); |
355da1eb SW |
1121 | return err; |
1122 | ||
1123 | do_sync: | |
be655596 | 1124 | spin_unlock(&ci->i_ceph_lock); |
3adf654d | 1125 | do_sync_unlocked: |
604d1b02 YZ |
1126 | if (lock_snap_rwsem) |
1127 | up_read(&mdsc->snap_rwsem); | |
315f2408 YZ |
1128 | |
1129 | /* security module set xattr while filling trace */ | |
d37b1d99 | 1130 | if (current->journal_info) { |
315f2408 YZ |
1131 | pr_warn_ratelimited("sync setxattr %p " |
1132 | "during filling trace\n", inode); | |
1133 | err = -EBUSY; | |
1134 | } else { | |
a26fecca | 1135 | err = ceph_sync_setxattr(inode, name, value, size, flags); |
f1919826 YZ |
1136 | if (err >= 0 && check_realm) { |
1137 | /* check if snaprealm was created for quota inode */ | |
1138 | spin_lock(&ci->i_ceph_lock); | |
1139 | if ((ci->i_max_files || ci->i_max_bytes) && | |
1140 | !(ci->i_snap_realm && | |
1141 | ci->i_snap_realm->ino == ci->i_vino.ino)) | |
1142 | err = -EOPNOTSUPP; | |
1143 | spin_unlock(&ci->i_ceph_lock); | |
1144 | } | |
315f2408 | 1145 | } |
355da1eb | 1146 | out: |
f66fd9f0 | 1147 | ceph_free_cap_flush(prealloc_cf); |
355da1eb SW |
1148 | kfree(newname); |
1149 | kfree(newval); | |
1150 | kfree(xattr); | |
1151 | return err; | |
1152 | } | |
1153 | ||
2cdeb1e4 AG |
1154 | static int ceph_get_xattr_handler(const struct xattr_handler *handler, |
1155 | struct dentry *dentry, struct inode *inode, | |
1156 | const char *name, void *value, size_t size) | |
7221fe4c | 1157 | { |
2cdeb1e4 AG |
1158 | if (!ceph_is_valid_xattr(name)) |
1159 | return -EOPNOTSUPP; | |
1160 | return __ceph_getxattr(inode, name, value, size); | |
1161 | } | |
7221fe4c | 1162 | |
2cdeb1e4 | 1163 | static int ceph_set_xattr_handler(const struct xattr_handler *handler, |
59301226 AV |
1164 | struct dentry *unused, struct inode *inode, |
1165 | const char *name, const void *value, | |
1166 | size_t size, int flags) | |
2cdeb1e4 AG |
1167 | { |
1168 | if (!ceph_is_valid_xattr(name)) | |
1169 | return -EOPNOTSUPP; | |
59301226 | 1170 | return __ceph_setxattr(inode, name, value, size, flags); |
7221fe4c | 1171 | } |
315f2408 | 1172 | |
5130ccea | 1173 | static const struct xattr_handler ceph_other_xattr_handler = { |
2cdeb1e4 AG |
1174 | .prefix = "", /* match any name => handlers called with full name */ |
1175 | .get = ceph_get_xattr_handler, | |
1176 | .set = ceph_set_xattr_handler, | |
1177 | }; | |
1178 | ||
315f2408 YZ |
1179 | #ifdef CONFIG_SECURITY |
1180 | bool ceph_security_xattr_wanted(struct inode *in) | |
1181 | { | |
1182 | return in->i_security != NULL; | |
1183 | } | |
1184 | ||
1185 | bool ceph_security_xattr_deadlock(struct inode *in) | |
1186 | { | |
1187 | struct ceph_inode_info *ci; | |
1188 | bool ret; | |
d37b1d99 | 1189 | if (!in->i_security) |
315f2408 YZ |
1190 | return false; |
1191 | ci = ceph_inode(in); | |
1192 | spin_lock(&ci->i_ceph_lock); | |
1193 | ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) && | |
1194 | !(ci->i_xattrs.version > 0 && | |
1195 | __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)); | |
1196 | spin_unlock(&ci->i_ceph_lock); | |
1197 | return ret; | |
1198 | } | |
1199 | #endif | |
5c31e92d YZ |
1200 | |
1201 | void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx) | |
1202 | { | |
1203 | #ifdef CONFIG_CEPH_FS_POSIX_ACL | |
1204 | posix_acl_release(as_ctx->acl); | |
1205 | posix_acl_release(as_ctx->default_acl); | |
1206 | #endif | |
1207 | if (as_ctx->pagelist) | |
1208 | ceph_pagelist_release(as_ctx->pagelist); | |
1209 | } |