3 #include "bkey_methods.h"
4 #include "btree_update.h"
12 #include <linux/dcache.h>
13 #include <linux/posix_acl_xattr.h>
14 #include <linux/xattr.h>
16 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
18 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
19 const struct xattr_search_key *key)
21 struct bch_str_hash_ctx ctx;
23 bch2_str_hash_init(&ctx, info);
24 bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
25 bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
27 return bch2_str_hash_end(&ctx, info);
30 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
32 return bch2_xattr_hash(info, key);
35 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
37 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
39 return bch2_xattr_hash(info,
40 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
43 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
45 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
46 const struct xattr_search_key *r = _r;
48 return l.v->x_type != r->type ||
49 l.v->x_name_len != r->name.len ||
50 memcmp(l.v->x_name, r->name.name, r->name.len);
53 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
55 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
56 struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
58 return l.v->x_type != r.v->x_type ||
59 l.v->x_name_len != r.v->x_name_len ||
60 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
63 const struct bch_hash_desc bch2_xattr_hash_desc = {
64 .btree_id = BTREE_ID_XATTRS,
65 .key_type = BCH_XATTR,
66 .whiteout_type = BCH_XATTR_WHITEOUT,
67 .hash_key = xattr_hash_key,
68 .hash_bkey = xattr_hash_bkey,
69 .cmp_key = xattr_cmp_key,
70 .cmp_bkey = xattr_cmp_bkey,
73 const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
75 const struct xattr_handler *handler;
76 struct bkey_s_c_xattr xattr;
80 if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr))
81 return "value too small";
83 xattr = bkey_s_c_to_xattr(k);
85 if (bkey_val_u64s(k.k) <
86 xattr_val_u64s(xattr.v->x_name_len,
87 le16_to_cpu(xattr.v->x_val_len)))
88 return "value too small";
90 if (bkey_val_u64s(k.k) >
91 xattr_val_u64s(xattr.v->x_name_len,
92 le16_to_cpu(xattr.v->x_val_len) + 4))
93 return "value too big";
95 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
97 return "invalid type";
99 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len))
100 return "xattr name has invalid characters";
103 case BCH_XATTR_WHITEOUT:
104 return bkey_val_bytes(k.k) != 0
105 ? "value size should be zero"
109 return "invalid type";
113 void bch2_xattr_to_text(struct bch_fs *c, char *buf,
114 size_t size, struct bkey_s_c k)
116 const struct xattr_handler *handler;
117 struct bkey_s_c_xattr xattr;
122 xattr = bkey_s_c_to_xattr(k);
124 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
125 if (handler && handler->prefix)
126 n += scnprintf(buf + n, size - n, "%s", handler->prefix);
128 n += scnprintf(buf + n, size - n, "(type %u)",
131 n += scnprintf(buf + n, size - n, "(unknown type %u)",
134 n += bch_scnmemcpy(buf + n, size - n, xattr.v->x_name,
135 xattr.v->x_name_len);
136 n += scnprintf(buf + n, size - n, ":");
137 n += bch_scnmemcpy(buf + n, size - n, xattr_val(xattr.v),
138 le16_to_cpu(xattr.v->x_val_len));
140 case BCH_XATTR_WHITEOUT:
141 scnprintf(buf, size, "whiteout");
146 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
147 const char *name, void *buffer, size_t size, int type)
149 struct btree_trans trans;
150 struct btree_iter *iter;
151 struct bkey_s_c_xattr xattr;
154 bch2_trans_init(&trans, c);
156 iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
157 &inode->ei_str_hash, inode->v.i_ino,
158 &X_SEARCH(type, name, strlen(name)),
161 bch2_trans_exit(&trans);
162 BUG_ON(PTR_ERR(iter) == -EINTR);
164 return PTR_ERR(iter) == -ENOENT ? -ENODATA : PTR_ERR(iter);
167 xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
168 ret = le16_to_cpu(xattr.v->x_val_len);
173 memcpy(buffer, xattr_val(xattr.v), ret);
176 bch2_trans_exit(&trans);
180 int bch2_xattr_set(struct btree_trans *trans, u64 inum,
181 const struct bch_hash_info *hash_info,
182 const char *name, const void *value, size_t size,
188 struct bkey_i_xattr *xattr;
189 unsigned namelen = strlen(name);
190 unsigned u64s = BKEY_U64s +
191 xattr_val_u64s(namelen, size);
196 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
198 return PTR_ERR(xattr);
200 bkey_xattr_init(&xattr->k_i);
201 xattr->k.u64s = u64s;
202 xattr->v.x_type = type;
203 xattr->v.x_name_len = namelen;
204 xattr->v.x_val_len = cpu_to_le16(size);
205 memcpy(xattr->v.x_name, name, namelen);
206 memcpy(xattr_val(&xattr->v), value, size);
208 ret = __bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
210 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
211 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
213 struct xattr_search_key search =
214 X_SEARCH(type, name, strlen(name));
216 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
217 hash_info, inum, &search);
221 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
226 static size_t bch2_xattr_emit(struct dentry *dentry,
227 const struct bch_xattr *xattr,
228 char *buffer, size_t buffer_size)
230 const struct xattr_handler *handler =
231 bch2_xattr_type_to_handler(xattr->x_type);
233 if (handler && (!handler->list || handler->list(dentry))) {
234 const char *prefix = handler->prefix ?: handler->name;
235 const size_t prefix_len = strlen(prefix);
236 const size_t total_len = prefix_len + xattr->x_name_len + 1;
238 if (buffer && total_len <= buffer_size) {
239 memcpy(buffer, prefix, prefix_len);
240 memcpy(buffer + prefix_len,
241 xattr->x_name, xattr->x_name_len);
242 buffer[prefix_len + xattr->x_name_len] = '\0';
251 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
253 struct bch_fs *c = dentry->d_sb->s_fs_info;
254 struct btree_iter iter;
256 const struct bch_xattr *xattr;
257 u64 inum = dentry->d_inode->i_ino;
261 for_each_btree_key(&iter, c, BTREE_ID_XATTRS, POS(inum, 0), 0, k) {
262 BUG_ON(k.k->p.inode < inum);
264 if (k.k->p.inode > inum)
267 if (k.k->type != BCH_XATTR)
270 xattr = bkey_s_c_to_xattr(k).v;
272 len = bch2_xattr_emit(dentry, xattr, buffer, buffer_size);
274 if (len > buffer_size) {
275 bch2_btree_iter_unlock(&iter);
286 bch2_btree_iter_unlock(&iter);
291 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
292 struct dentry *dentry, struct inode *vinode,
293 const char *name, void *buffer, size_t size)
295 struct bch_inode_info *inode = to_bch_ei(vinode);
296 struct bch_fs *c = inode->v.i_sb->s_fs_info;
298 return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
301 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
302 struct dentry *dentry, struct inode *vinode,
303 const char *name, const void *value,
304 size_t size, int flags)
306 struct bch_inode_info *inode = to_bch_ei(vinode);
307 struct bch_fs *c = inode->v.i_sb->s_fs_info;
309 return bch2_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC,
310 bch2_xattr_set(&trans, inode->v.i_ino,
313 handler->flags, flags));
316 static const struct xattr_handler bch_xattr_user_handler = {
317 .prefix = XATTR_USER_PREFIX,
318 .get = bch2_xattr_get_handler,
319 .set = bch2_xattr_set_handler,
320 .flags = BCH_XATTR_INDEX_USER,
323 static bool bch2_xattr_trusted_list(struct dentry *dentry)
325 return capable(CAP_SYS_ADMIN);
328 static const struct xattr_handler bch_xattr_trusted_handler = {
329 .prefix = XATTR_TRUSTED_PREFIX,
330 .list = bch2_xattr_trusted_list,
331 .get = bch2_xattr_get_handler,
332 .set = bch2_xattr_set_handler,
333 .flags = BCH_XATTR_INDEX_TRUSTED,
336 static const struct xattr_handler bch_xattr_security_handler = {
337 .prefix = XATTR_SECURITY_PREFIX,
338 .get = bch2_xattr_get_handler,
339 .set = bch2_xattr_set_handler,
340 .flags = BCH_XATTR_INDEX_SECURITY,
343 #ifndef NO_BCACHEFS_FS
345 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
346 struct dentry *dentry, struct inode *vinode,
347 const char *name, void *buffer, size_t size)
349 struct bch_inode_info *inode = to_bch_ei(vinode);
350 struct bch_fs *c = inode->v.i_sb->s_fs_info;
351 struct bch_opts opts =
352 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
353 const struct bch_option *opt;
357 id = bch2_opt_lookup(name);
358 if (id < 0 || !bch2_opt_is_inode_opt(id))
361 opt = bch2_opt_table + id;
363 if (!bch2_opt_defined_by_id(&opts, id))
366 v = bch2_opt_get_by_id(&opts, id);
368 ret = bch2_opt_to_text(c, buffer, size, opt, v, 0);
370 return ret < size || !buffer ? ret : -ERANGE;
373 struct inode_opt_set {
379 static int inode_opt_set_fn(struct bch_inode_info *inode,
380 struct bch_inode_unpacked *bi,
383 struct inode_opt_set *s = p;
386 bch2_inode_opt_set(bi, s->id, s->v);
388 bch2_inode_opt_clear(bi, s->id);
392 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
393 struct dentry *dentry, struct inode *vinode,
394 const char *name, const void *value,
395 size_t size, int flags)
397 struct bch_inode_info *inode = to_bch_ei(vinode);
398 struct bch_fs *c = inode->v.i_sb->s_fs_info;
399 const struct bch_option *opt;
401 struct inode_opt_set s;
404 s.id = bch2_opt_lookup(name);
405 if (s.id < 0 || !bch2_opt_is_inode_opt(s.id))
408 opt = bch2_opt_table + s.id;
411 buf = kmalloc(size + 1, GFP_KERNEL);
414 memcpy(buf, value, size);
417 ret = bch2_opt_parse(c, opt, buf, &s.v);
423 if (s.id == Opt_compression ||
424 s.id == Opt_background_compression) {
425 ret = bch2_check_set_has_compressed_data(c, s.v);
435 mutex_lock(&inode->ei_update_lock);
436 ret = __bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
437 mutex_unlock(&inode->ei_update_lock);
440 (s.id == Opt_background_compression ||
441 s.id == Opt_background_target))
442 bch2_rebalance_add_work(c, inode->v.i_blocks);
447 static const struct xattr_handler bch_xattr_bcachefs_handler = {
448 .prefix = "bcachefs.",
449 .get = bch2_xattr_bcachefs_get,
450 .set = bch2_xattr_bcachefs_set,
453 #endif /* NO_BCACHEFS_FS */
455 const struct xattr_handler *bch2_xattr_handlers[] = {
456 &bch_xattr_user_handler,
457 &posix_acl_access_xattr_handler,
458 &posix_acl_default_xattr_handler,
459 &bch_xattr_trusted_handler,
460 &bch_xattr_security_handler,
461 #ifndef NO_BCACHEFS_FS
462 &bch_xattr_bcachefs_handler,
467 static const struct xattr_handler *bch_xattr_handler_map[] = {
468 [BCH_XATTR_INDEX_USER] = &bch_xattr_user_handler,
469 [BCH_XATTR_INDEX_POSIX_ACL_ACCESS] =
470 &posix_acl_access_xattr_handler,
471 [BCH_XATTR_INDEX_POSIX_ACL_DEFAULT] =
472 &posix_acl_default_xattr_handler,
473 [BCH_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
474 [BCH_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
477 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
479 return type < ARRAY_SIZE(bch_xattr_handler_map)
480 ? bch_xattr_handler_map[type]