xref: /linux/fs/bcachefs/xattr.c (revision ef2233850edc4cc0d5fc6136fcdb004a1ddfa7db)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "acl.h"
5 #include "bkey_methods.h"
6 #include "btree_update.h"
7 #include "extents.h"
8 #include "fs.h"
9 #include "rebalance.h"
10 #include "str_hash.h"
11 #include "xattr.h"
12 
13 #include <linux/dcache.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/xattr.h>
16 
17 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
18 
bch2_xattr_hash(const struct bch_hash_info * info,const struct xattr_search_key * key)19 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
20 			  const struct xattr_search_key *key)
21 {
22 	struct bch_str_hash_ctx ctx;
23 
24 	bch2_str_hash_init(&ctx, info);
25 	bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
26 	bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
27 
28 	return bch2_str_hash_end(&ctx, info);
29 }
30 
xattr_hash_key(const struct bch_hash_info * info,const void * key)31 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
32 {
33 	return bch2_xattr_hash(info, key);
34 }
35 
xattr_hash_bkey(const struct bch_hash_info * info,struct bkey_s_c k)36 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
37 {
38 	struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
39 
40 	return bch2_xattr_hash(info,
41 		 &X_SEARCH(x.v->x_type, x.v->x_name_and_value, x.v->x_name_len));
42 }
43 
xattr_cmp_key(struct bkey_s_c _l,const void * _r)44 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
45 {
46 	struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
47 	const struct xattr_search_key *r = _r;
48 
49 	return l.v->x_type != r->type ||
50 		l.v->x_name_len != r->name.len ||
51 		memcmp(l.v->x_name_and_value, r->name.name, r->name.len);
52 }
53 
xattr_cmp_bkey(struct bkey_s_c _l,struct bkey_s_c _r)54 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
55 {
56 	struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
57 	struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
58 
59 	return l.v->x_type != r.v->x_type ||
60 		l.v->x_name_len != r.v->x_name_len ||
61 		memcmp(l.v->x_name_and_value, r.v->x_name_and_value, r.v->x_name_len);
62 }
63 
64 const struct bch_hash_desc bch2_xattr_hash_desc = {
65 	.btree_id	= BTREE_ID_xattrs,
66 	.key_type	= KEY_TYPE_xattr,
67 	.hash_key	= xattr_hash_key,
68 	.hash_bkey	= xattr_hash_bkey,
69 	.cmp_key	= xattr_cmp_key,
70 	.cmp_bkey	= xattr_cmp_bkey,
71 };
72 
bch2_xattr_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)73 int bch2_xattr_validate(struct bch_fs *c, struct bkey_s_c k,
74 			struct bkey_validate_context from)
75 {
76 	struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
77 	unsigned val_u64s = xattr_val_u64s(xattr.v->x_name_len,
78 					   le16_to_cpu(xattr.v->x_val_len));
79 	int ret = 0;
80 
81 	bkey_fsck_err_on(bkey_val_u64s(k.k) < val_u64s,
82 			 c, xattr_val_size_too_small,
83 			 "value too small (%zu < %u)",
84 			 bkey_val_u64s(k.k), val_u64s);
85 
86 	/* XXX why +4 ? */
87 	val_u64s = xattr_val_u64s(xattr.v->x_name_len,
88 				  le16_to_cpu(xattr.v->x_val_len) + 4);
89 
90 	bkey_fsck_err_on(bkey_val_u64s(k.k) > val_u64s,
91 			 c, xattr_val_size_too_big,
92 			 "value too big (%zu > %u)",
93 			 bkey_val_u64s(k.k), val_u64s);
94 
95 	bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr.v->x_type),
96 			 c, xattr_invalid_type,
97 			 "invalid type (%u)", xattr.v->x_type);
98 
99 	bkey_fsck_err_on(memchr(xattr.v->x_name_and_value, '\0', xattr.v->x_name_len),
100 			 c, xattr_name_invalid_chars,
101 			 "xattr name has invalid characters");
102 fsck_err:
103 	return ret;
104 }
105 
bch2_xattr_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)106 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
107 			struct bkey_s_c k)
108 {
109 	const struct xattr_handler *handler;
110 	struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
111 
112 	handler = bch2_xattr_type_to_handler(xattr.v->x_type);
113 	if (handler && handler->prefix)
114 		prt_printf(out, "%s", handler->prefix);
115 	else if (handler)
116 		prt_printf(out, "(type %u)", xattr.v->x_type);
117 	else
118 		prt_printf(out, "(unknown type %u)", xattr.v->x_type);
119 
120 	unsigned name_len = xattr.v->x_name_len;
121 	unsigned val_len  = le16_to_cpu(xattr.v->x_val_len);
122 	unsigned max_name_val_bytes = bkey_val_bytes(xattr.k) -
123 		offsetof(struct bch_xattr, x_name_and_value);
124 
125 	val_len  = min_t(int, val_len, max_name_val_bytes - name_len);
126 	name_len = min(name_len, max_name_val_bytes);
127 
128 	prt_printf(out, "%.*s:%.*s",
129 		   name_len, xattr.v->x_name_and_value,
130 		   val_len,  (char *) xattr_val(xattr.v));
131 
132 	if (xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS ||
133 	    xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT) {
134 		prt_char(out, ' ');
135 		bch2_acl_to_text(out, xattr_val(xattr.v),
136 				 le16_to_cpu(xattr.v->x_val_len));
137 	}
138 }
139 
bch2_xattr_get_trans(struct btree_trans * trans,struct bch_inode_info * inode,const char * name,void * buffer,size_t size,int type)140 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
141 				const char *name, void *buffer, size_t size, int type)
142 {
143 	struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
144 	struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
145 	struct btree_iter iter;
146 	struct bkey_s_c k = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
147 					     inode_inum(inode), &search, 0);
148 	int ret = bkey_err(k);
149 	if (ret)
150 		return ret;
151 
152 	struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
153 	ret = le16_to_cpu(xattr.v->x_val_len);
154 	if (buffer) {
155 		if (ret > size)
156 			ret = -ERANGE;
157 		else
158 			memcpy(buffer, xattr_val(xattr.v), ret);
159 	}
160 	bch2_trans_iter_exit(trans, &iter);
161 	return ret;
162 }
163 
bch2_xattr_set(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * inode_u,const struct bch_hash_info * hash_info,const char * name,const void * value,size_t size,int type,int flags)164 int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
165 		   struct bch_inode_unpacked *inode_u,
166 		   const struct bch_hash_info *hash_info,
167 		   const char *name, const void *value, size_t size,
168 		   int type, int flags)
169 {
170 	struct bch_fs *c = trans->c;
171 	struct btree_iter inode_iter = {};
172 	int ret;
173 
174 	ret   = bch2_subvol_is_ro_trans(trans, inum.subvol) ?:
175 		bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
176 	if (ret)
177 		return ret;
178 
179 	/*
180 	 * Besides the ctime update, extents, dirents and xattrs updates require
181 	 * that an inode update also happens - to ensure that if a key exists in
182 	 * one of those btrees with a given snapshot ID an inode is also present
183 	 */
184 	inode_u->bi_ctime = bch2_current_time(c);
185 
186 	ret = bch2_inode_write(trans, &inode_iter, inode_u);
187 	bch2_trans_iter_exit(trans, &inode_iter);
188 
189 	if (ret)
190 		return ret;
191 
192 	if (value) {
193 		struct bkey_i_xattr *xattr;
194 		unsigned namelen = strlen(name);
195 		unsigned u64s = BKEY_U64s +
196 			xattr_val_u64s(namelen, size);
197 
198 		if (u64s > U8_MAX)
199 			return -ERANGE;
200 
201 		xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
202 		if (IS_ERR(xattr))
203 			return PTR_ERR(xattr);
204 
205 		bkey_xattr_init(&xattr->k_i);
206 		xattr->k.u64s		= u64s;
207 		xattr->v.x_type		= type;
208 		xattr->v.x_name_len	= namelen;
209 		xattr->v.x_val_len	= cpu_to_le16(size);
210 		memcpy(xattr->v.x_name_and_value, name, namelen);
211 		memcpy(xattr_val(&xattr->v), value, size);
212 
213 		ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
214 			      inum, &xattr->k_i,
215 			      (flags & XATTR_CREATE ? STR_HASH_must_create : 0)|
216 			      (flags & XATTR_REPLACE ? STR_HASH_must_replace : 0));
217 	} else {
218 		struct xattr_search_key search =
219 			X_SEARCH(type, name, strlen(name));
220 
221 		ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
222 				       hash_info, inum, &search);
223 	}
224 
225 	if (bch2_err_matches(ret, ENOENT))
226 		ret = flags & XATTR_REPLACE ? -ENODATA : 0;
227 
228 	return ret;
229 }
230 
231 struct xattr_buf {
232 	char		*buf;
233 	size_t		len;
234 	size_t		used;
235 };
236 
__bch2_xattr_emit(const char * prefix,const char * name,size_t name_len,struct xattr_buf * buf)237 static int __bch2_xattr_emit(const char *prefix,
238 			     const char *name, size_t name_len,
239 			     struct xattr_buf *buf)
240 {
241 	const size_t prefix_len = strlen(prefix);
242 	const size_t total_len = prefix_len + name_len + 1;
243 
244 	if (buf->buf) {
245 		if (buf->used + total_len > buf->len)
246 			return -ERANGE;
247 
248 		memcpy(buf->buf + buf->used, prefix, prefix_len);
249 		memcpy(buf->buf + buf->used + prefix_len,
250 		       name, name_len);
251 		buf->buf[buf->used + prefix_len + name_len] = '\0';
252 	}
253 
254 	buf->used += total_len;
255 	return 0;
256 }
257 
bch2_xattr_prefix(unsigned type,struct dentry * dentry)258 static inline const char *bch2_xattr_prefix(unsigned type, struct dentry *dentry)
259 {
260 	const struct xattr_handler *handler = bch2_xattr_type_to_handler(type);
261 
262 	if (!xattr_handler_can_list(handler, dentry))
263 		return NULL;
264 
265 	return xattr_prefix(handler);
266 }
267 
bch2_xattr_emit(struct dentry * dentry,const struct bch_xattr * xattr,struct xattr_buf * buf)268 static int bch2_xattr_emit(struct dentry *dentry,
269 			    const struct bch_xattr *xattr,
270 			    struct xattr_buf *buf)
271 {
272 	const char *prefix;
273 
274 	prefix = bch2_xattr_prefix(xattr->x_type, dentry);
275 	if (!prefix)
276 		return 0;
277 
278 	return __bch2_xattr_emit(prefix, xattr->x_name_and_value, xattr->x_name_len, buf);
279 }
280 
bch2_xattr_list_bcachefs(struct bch_fs * c,struct bch_inode_unpacked * inode,struct xattr_buf * buf,bool all)281 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
282 				    struct bch_inode_unpacked *inode,
283 				    struct xattr_buf *buf,
284 				    bool all)
285 {
286 	const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
287 	unsigned id;
288 	int ret = 0;
289 	u64 v;
290 
291 	for (id = 0; id < Inode_opt_nr; id++) {
292 		v = bch2_inode_opt_get(inode, id);
293 		if (!v)
294 			continue;
295 
296 		if (!all &&
297 		    !(inode->bi_fields_set & (1 << id)))
298 			continue;
299 
300 		ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
301 					strlen(bch2_inode_opts[id]), buf);
302 		if (ret)
303 			break;
304 	}
305 
306 	return ret;
307 }
308 
bch2_xattr_list(struct dentry * dentry,char * buffer,size_t buffer_size)309 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
310 {
311 	struct bch_fs *c = dentry->d_sb->s_fs_info;
312 	struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
313 	struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
314 	u64 offset = 0, inum = inode->ei_inode.bi_inum;
315 
316 	int ret = bch2_trans_run(c,
317 		for_each_btree_key_in_subvolume_max(trans, iter, BTREE_ID_xattrs,
318 				   POS(inum, offset),
319 				   POS(inum, U64_MAX),
320 				   inode->ei_inum.subvol, 0, k, ({
321 			if (k.k->type != KEY_TYPE_xattr)
322 				continue;
323 
324 			bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
325 		}))) ?:
326 		bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false) ?:
327 		bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
328 
329 	return ret ? bch2_err_class(ret) : buf.used;
330 }
331 
bch2_xattr_get_handler(const struct xattr_handler * handler,struct dentry * dentry,struct inode * vinode,const char * name,void * buffer,size_t size)332 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
333 				  struct dentry *dentry, struct inode *vinode,
334 				  const char *name, void *buffer, size_t size)
335 {
336 	struct bch_inode_info *inode = to_bch_ei(vinode);
337 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
338 	int ret = bch2_trans_do(c,
339 		bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags));
340 
341 	if (ret < 0 && bch2_err_matches(ret, ENOENT))
342 		ret = -ENODATA;
343 
344 	return bch2_err_class(ret);
345 }
346 
bch2_xattr_set_handler(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * dentry,struct inode * vinode,const char * name,const void * value,size_t size,int flags)347 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
348 				  struct mnt_idmap *idmap,
349 				  struct dentry *dentry, struct inode *vinode,
350 				  const char *name, const void *value,
351 				  size_t size, int flags)
352 {
353 	struct bch_inode_info *inode = to_bch_ei(vinode);
354 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
355 	struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
356 	struct bch_inode_unpacked inode_u;
357 	int ret;
358 
359 	ret = bch2_trans_run(c,
360 		commit_do(trans, NULL, NULL, 0,
361 			bch2_xattr_set(trans, inode_inum(inode), &inode_u,
362 				       &hash, name, value, size,
363 				       handler->flags, flags)) ?:
364 		(bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0));
365 
366 	return bch2_err_class(ret);
367 }
368 
369 static const struct xattr_handler bch_xattr_user_handler = {
370 	.prefix	= XATTR_USER_PREFIX,
371 	.get	= bch2_xattr_get_handler,
372 	.set	= bch2_xattr_set_handler,
373 	.flags	= KEY_TYPE_XATTR_INDEX_USER,
374 };
375 
bch2_xattr_trusted_list(struct dentry * dentry)376 static bool bch2_xattr_trusted_list(struct dentry *dentry)
377 {
378 	return capable(CAP_SYS_ADMIN);
379 }
380 
381 static const struct xattr_handler bch_xattr_trusted_handler = {
382 	.prefix	= XATTR_TRUSTED_PREFIX,
383 	.list	= bch2_xattr_trusted_list,
384 	.get	= bch2_xattr_get_handler,
385 	.set	= bch2_xattr_set_handler,
386 	.flags	= KEY_TYPE_XATTR_INDEX_TRUSTED,
387 };
388 
389 static const struct xattr_handler bch_xattr_security_handler = {
390 	.prefix	= XATTR_SECURITY_PREFIX,
391 	.get	= bch2_xattr_get_handler,
392 	.set	= bch2_xattr_set_handler,
393 	.flags	= KEY_TYPE_XATTR_INDEX_SECURITY,
394 };
395 
396 #ifndef NO_BCACHEFS_FS
397 
opt_to_inode_opt(int id)398 static int opt_to_inode_opt(int id)
399 {
400 	switch (id) {
401 #define x(name, ...)				\
402 	case Opt_##name: return Inode_opt_##name;
403 	BCH_INODE_OPTS()
404 #undef  x
405 	default:
406 		return -1;
407 	}
408 }
409 
__bch2_xattr_bcachefs_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * vinode,const char * name,void * buffer,size_t size,bool all)410 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
411 				struct dentry *dentry, struct inode *vinode,
412 				const char *name, void *buffer, size_t size,
413 				bool all)
414 {
415 	struct bch_inode_info *inode = to_bch_ei(vinode);
416 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
417 	struct bch_opts opts =
418 		bch2_inode_opts_to_opts(&inode->ei_inode);
419 	const struct bch_option *opt;
420 	int id, inode_opt_id;
421 	struct printbuf out = PRINTBUF;
422 	int ret;
423 	u64 v;
424 
425 	id = bch2_opt_lookup(name);
426 	if (id < 0 || !bch2_opt_is_inode_opt(id))
427 		return -EINVAL;
428 
429 	inode_opt_id = opt_to_inode_opt(id);
430 	if (inode_opt_id < 0)
431 		return -EINVAL;
432 
433 	opt = bch2_opt_table + id;
434 
435 	if (!bch2_opt_defined_by_id(&opts, id))
436 		return -ENODATA;
437 
438 	if (!all &&
439 	    !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
440 		return -ENODATA;
441 
442 	v = bch2_opt_get_by_id(&opts, id);
443 	bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
444 
445 	ret = out.pos;
446 
447 	if (out.allocation_failure) {
448 		ret = -ENOMEM;
449 	} else if (buffer) {
450 		if (out.pos > size)
451 			ret = -ERANGE;
452 		else
453 			memcpy(buffer, out.buf, out.pos);
454 	}
455 
456 	printbuf_exit(&out);
457 	return ret;
458 }
459 
bch2_xattr_bcachefs_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * vinode,const char * name,void * buffer,size_t size)460 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
461 				   struct dentry *dentry, struct inode *vinode,
462 				   const char *name, void *buffer, size_t size)
463 {
464 	return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
465 					 name, buffer, size, false);
466 }
467 
468 struct inode_opt_set {
469 	int			id;
470 	u64			v;
471 	bool			defined;
472 };
473 
inode_opt_set_fn(struct btree_trans * trans,struct bch_inode_info * inode,struct bch_inode_unpacked * bi,void * p)474 static int inode_opt_set_fn(struct btree_trans *trans,
475 			    struct bch_inode_info *inode,
476 			    struct bch_inode_unpacked *bi,
477 			    void *p)
478 {
479 	struct inode_opt_set *s = p;
480 
481 	if (s->id == Inode_opt_casefold) {
482 		int ret = bch2_inode_set_casefold(trans, inode_inum(inode), bi, s->v);
483 		if (ret)
484 			return ret;
485 	}
486 
487 	if (s->defined)
488 		bi->bi_fields_set |= 1U << s->id;
489 	else
490 		bi->bi_fields_set &= ~(1U << s->id);
491 
492 	bch2_inode_opt_set(bi, s->id, s->v);
493 
494 	return 0;
495 }
496 
bch2_xattr_bcachefs_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * dentry,struct inode * vinode,const char * name,const void * value,size_t size,int flags)497 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
498 				   struct mnt_idmap *idmap,
499 				   struct dentry *dentry, struct inode *vinode,
500 				   const char *name, const void *value,
501 				   size_t size, int flags)
502 {
503 	struct bch_inode_info *inode = to_bch_ei(vinode);
504 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
505 	const struct bch_option *opt;
506 	char *buf;
507 	struct inode_opt_set s;
508 	int opt_id, inode_opt_id, ret;
509 
510 	opt_id = bch2_opt_lookup(name);
511 	if (opt_id < 0)
512 		return -EINVAL;
513 
514 	opt = bch2_opt_table + opt_id;
515 
516 	inode_opt_id = opt_to_inode_opt(opt_id);
517 	if (inode_opt_id < 0)
518 		return -EINVAL;
519 
520 	s.id = inode_opt_id;
521 
522 	if (value) {
523 		u64 v = 0;
524 
525 		buf = kmalloc(size + 1, GFP_KERNEL);
526 		if (!buf)
527 			return -ENOMEM;
528 		memcpy(buf, value, size);
529 		buf[size] = '\0';
530 
531 		ret = bch2_opt_parse(c, opt, buf, &v, NULL);
532 		kfree(buf);
533 
534 		if (ret < 0)
535 			goto err_class_exit;
536 
537 		ret = bch2_opt_hook_pre_set(c, NULL, opt_id, v);
538 		if (ret < 0)
539 			goto err_class_exit;
540 
541 		s.v = v + 1;
542 		s.defined = true;
543 	} else {
544 		/*
545 		 * Check if this option was set on the parent - if so, switched
546 		 * back to inheriting from the parent:
547 		 *
548 		 * rename() also has to deal with keeping inherited options up
549 		 * to date - see bch2_reinherit_attrs()
550 		 */
551 		spin_lock(&dentry->d_lock);
552 		if (!IS_ROOT(dentry)) {
553 			struct bch_inode_info *dir =
554 				to_bch_ei(d_inode(dentry->d_parent));
555 
556 			s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
557 		} else {
558 			s.v = 0;
559 		}
560 		spin_unlock(&dentry->d_lock);
561 
562 		s.defined = false;
563 	}
564 
565 	mutex_lock(&inode->ei_update_lock);
566 	if (inode_opt_id == Inode_opt_project) {
567 		/*
568 		 * inode fields accessible via the xattr interface are stored
569 		 * with a +1 bias, so that 0 means unset:
570 		 */
571 		ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
572 		if (ret)
573 			goto err;
574 	}
575 
576 	ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
577 err:
578 	mutex_unlock(&inode->ei_update_lock);
579 err_class_exit:
580 	return bch2_err_class(ret);
581 }
582 
583 static const struct xattr_handler bch_xattr_bcachefs_handler = {
584 	.prefix	= "bcachefs.",
585 	.get	= bch2_xattr_bcachefs_get,
586 	.set	= bch2_xattr_bcachefs_set,
587 };
588 
bch2_xattr_bcachefs_get_effective(const struct xattr_handler * handler,struct dentry * dentry,struct inode * vinode,const char * name,void * buffer,size_t size)589 static int bch2_xattr_bcachefs_get_effective(
590 				const struct xattr_handler *handler,
591 				struct dentry *dentry, struct inode *vinode,
592 				const char *name, void *buffer, size_t size)
593 {
594 	return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
595 					 name, buffer, size, true);
596 }
597 
598 /* Noop - xattrs in the bcachefs_effective namespace are inherited */
bch2_xattr_bcachefs_set_effective(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * dentry,struct inode * vinode,const char * name,const void * value,size_t size,int flags)599 static int bch2_xattr_bcachefs_set_effective(const struct xattr_handler *handler,
600 				   struct mnt_idmap *idmap,
601 				   struct dentry *dentry, struct inode *vinode,
602 				   const char *name, const void *value,
603 				   size_t size, int flags)
604 {
605 	return 0;
606 }
607 
608 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
609 	.prefix	= "bcachefs_effective.",
610 	.get	= bch2_xattr_bcachefs_get_effective,
611 	.set	= bch2_xattr_bcachefs_set_effective,
612 };
613 
614 #endif /* NO_BCACHEFS_FS */
615 
616 const struct xattr_handler * const bch2_xattr_handlers[] = {
617 	&bch_xattr_user_handler,
618 	&bch_xattr_trusted_handler,
619 	&bch_xattr_security_handler,
620 #ifndef NO_BCACHEFS_FS
621 	&bch_xattr_bcachefs_handler,
622 	&bch_xattr_bcachefs_effective_handler,
623 #endif
624 	NULL
625 };
626 
627 static const struct xattr_handler *bch_xattr_handler_map[] = {
628 	[KEY_TYPE_XATTR_INDEX_USER]			= &bch_xattr_user_handler,
629 	[KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS]	=
630 		&nop_posix_acl_access,
631 	[KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT]	=
632 		&nop_posix_acl_default,
633 	[KEY_TYPE_XATTR_INDEX_TRUSTED]		= &bch_xattr_trusted_handler,
634 	[KEY_TYPE_XATTR_INDEX_SECURITY]		= &bch_xattr_security_handler,
635 };
636 
bch2_xattr_type_to_handler(unsigned type)637 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
638 {
639 	return type < ARRAY_SIZE(bch_xattr_handler_map)
640 		? bch_xattr_handler_map[type]
641 		: NULL;
642 }
643