3 #include "bkey_methods.h"
4 #include "btree_update.h"
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/xattr.h>
13 struct xattr_search_key {
18 #define X_SEARCH(_type, _name, _len) ((struct xattr_search_key) \
19 { .type = _type, .name = QSTR_INIT(_name, _len) })
21 static u64 bch_xattr_hash(const struct bch_hash_info *info,
22 const struct xattr_search_key *key)
24 struct bch_str_hash_ctx ctx;
26 bch_str_hash_init(&ctx, info);
27 bch_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
28 bch_str_hash_update(&ctx, info, key->name.name, key->name.len);
30 return bch_str_hash_end(&ctx, info);
33 #define xattr_val(_xattr) ((_xattr)->x_name + (_xattr)->x_name_len)
35 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
37 return bch_xattr_hash(info, key);
40 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
42 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
44 return bch_xattr_hash(info,
45 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
48 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
50 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
51 const struct xattr_search_key *r = _r;
53 return l.v->x_type != r->type ||
54 l.v->x_name_len != r->name.len ||
55 memcmp(l.v->x_name, r->name.name, r->name.len);
58 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
60 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
61 struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
63 return l.v->x_type != r.v->x_type ||
64 l.v->x_name_len != r.v->x_name_len ||
65 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
68 static const struct bch_hash_desc xattr_hash_desc = {
69 .btree_id = BTREE_ID_XATTRS,
70 .key_type = BCH_XATTR,
71 .whiteout_type = BCH_XATTR_WHITEOUT,
72 .hash_key = xattr_hash_key,
73 .hash_bkey = xattr_hash_bkey,
74 .cmp_key = xattr_cmp_key,
75 .cmp_bkey = xattr_cmp_bkey,
78 static const char *bch_xattr_invalid(const struct cache_set *c,
83 return bkey_val_bytes(k.k) < sizeof(struct bch_xattr)
87 case BCH_XATTR_WHITEOUT:
88 return bkey_val_bytes(k.k) != 0
89 ? "value size should be zero"
93 return "invalid type";
97 static void bch_xattr_to_text(struct cache_set *c, char *buf,
98 size_t size, struct bkey_s_c k)
100 struct bkey_s_c_xattr xattr;
105 xattr = bkey_s_c_to_xattr(k);
108 n = min_t(unsigned, size, xattr.v->x_name_len);
109 memcpy(buf, xattr.v->x_name, n);
110 buf[size - 1] = '\0';
115 n = scnprintf(buf, size, " -> ");
120 n = min_t(unsigned, size,
121 le16_to_cpu(xattr.v->x_val_len));
122 memcpy(buf, xattr_val(xattr.v), n);
123 buf[size - 1] = '\0';
129 case BCH_XATTR_WHITEOUT:
130 scnprintf(buf, size, "whiteout");
135 const struct bkey_ops bch_bkey_xattr_ops = {
136 .key_invalid = bch_xattr_invalid,
137 .val_to_text = bch_xattr_to_text,
140 int bch_xattr_get(struct cache_set *c, struct inode *inode,
141 const char *name, void *buffer, size_t size, int type)
143 struct bch_inode_info *ei = to_bch_ei(inode);
144 struct btree_iter iter;
146 struct bkey_s_c_xattr xattr;
149 k = bch_hash_lookup(xattr_hash_desc, &ei->str_hash, c,
150 ei->vfs_inode.i_ino, &iter,
151 &X_SEARCH(type, name, strlen(name)));
153 return bch_btree_iter_unlock(&iter) ?: -ENODATA;
155 xattr = bkey_s_c_to_xattr(k);
156 ret = le16_to_cpu(xattr.v->x_val_len);
161 memcpy(buffer, xattr_val(xattr.v), ret);
164 bch_btree_iter_unlock(&iter);
168 int __bch_xattr_set(struct cache_set *c, u64 inum,
169 const struct bch_hash_info *hash_info,
170 const char *name, const void *value, size_t size,
171 int flags, int type, u64 *journal_seq)
173 struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
177 ret = bch_hash_delete(xattr_hash_desc, hash_info,
179 journal_seq, &search);
181 struct bkey_i_xattr *xattr;
182 unsigned u64s = BKEY_U64s +
183 DIV_ROUND_UP(sizeof(struct bch_xattr) +
184 search.name.len + size,
190 xattr = kmalloc(u64s * sizeof(u64), GFP_NOFS);
194 bkey_xattr_init(&xattr->k_i);
195 xattr->k.u64s = u64s;
196 xattr->v.x_type = type;
197 xattr->v.x_name_len = search.name.len;
198 xattr->v.x_val_len = cpu_to_le16(size);
199 memcpy(xattr->v.x_name, search.name.name, search.name.len);
200 memcpy(xattr_val(&xattr->v), value, size);
202 ret = bch_hash_set(xattr_hash_desc, hash_info, c,
205 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
206 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
211 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
216 int bch_xattr_set(struct cache_set *c, struct inode *inode,
217 const char *name, const void *value, size_t size,
220 struct bch_inode_info *ei = to_bch_ei(inode);
222 return __bch_xattr_set(c, inode->i_ino, &ei->str_hash,
223 name, value, size, flags, type,
227 static const struct xattr_handler *bch_xattr_type_to_handler(unsigned);
229 static size_t bch_xattr_emit(struct dentry *dentry,
230 const struct bch_xattr *xattr,
231 char *buffer, size_t buffer_size)
233 const struct xattr_handler *handler =
234 bch_xattr_type_to_handler(xattr->x_type);
236 if (handler && (!handler->list || handler->list(dentry))) {
237 const char *prefix = handler->prefix ?: handler->name;
238 const size_t prefix_len = strlen(prefix);
239 const size_t total_len = prefix_len + xattr->x_name_len + 1;
241 if (buffer && total_len <= buffer_size) {
242 memcpy(buffer, prefix, prefix_len);
243 memcpy(buffer + prefix_len,
244 xattr->x_name, xattr->x_name_len);
245 buffer[prefix_len + xattr->x_name_len] = '\0';
254 ssize_t bch_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
256 struct cache_set *c = dentry->d_sb->s_fs_info;
257 struct btree_iter iter;
259 const struct bch_xattr *xattr;
260 u64 inum = dentry->d_inode->i_ino;
264 for_each_btree_key(&iter, c, BTREE_ID_XATTRS, POS(inum, 0), k) {
265 BUG_ON(k.k->p.inode < inum);
267 if (k.k->p.inode > inum)
270 if (k.k->type != BCH_XATTR)
273 xattr = bkey_s_c_to_xattr(k).v;
275 len = bch_xattr_emit(dentry, xattr, buffer, buffer_size);
277 if (len > buffer_size) {
278 bch_btree_iter_unlock(&iter);
289 bch_btree_iter_unlock(&iter);
294 static int bch_xattr_get_handler(const struct xattr_handler *handler,
295 struct dentry *dentry, struct inode *inode,
296 const char *name, void *buffer, size_t size)
298 struct cache_set *c = inode->i_sb->s_fs_info;
300 return bch_xattr_get(c, inode, name, buffer, size, handler->flags);
303 static int bch_xattr_set_handler(const struct xattr_handler *handler,
304 struct dentry *dentry, struct inode *inode,
305 const char *name, const void *value,
306 size_t size, int flags)
308 struct cache_set *c = inode->i_sb->s_fs_info;
310 return bch_xattr_set(c, inode, name, value, size, flags,
314 static const struct xattr_handler bch_xattr_user_handler = {
315 .prefix = XATTR_USER_PREFIX,
316 .get = bch_xattr_get_handler,
317 .set = bch_xattr_set_handler,
318 .flags = BCH_XATTR_INDEX_USER,
321 static bool bch_xattr_trusted_list(struct dentry *dentry)
323 return capable(CAP_SYS_ADMIN);
326 static const struct xattr_handler bch_xattr_trusted_handler = {
327 .prefix = XATTR_TRUSTED_PREFIX,
328 .list = bch_xattr_trusted_list,
329 .get = bch_xattr_get_handler,
330 .set = bch_xattr_set_handler,
331 .flags = BCH_XATTR_INDEX_TRUSTED,
334 static const struct xattr_handler bch_xattr_security_handler = {
335 .prefix = XATTR_SECURITY_PREFIX,
336 .get = bch_xattr_get_handler,
337 .set = bch_xattr_set_handler,
338 .flags = BCH_XATTR_INDEX_SECURITY,
341 static const struct xattr_handler *bch_xattr_handler_map[] = {
342 [BCH_XATTR_INDEX_USER] = &bch_xattr_user_handler,
343 [BCH_XATTR_INDEX_POSIX_ACL_ACCESS] =
344 &posix_acl_access_xattr_handler,
345 [BCH_XATTR_INDEX_POSIX_ACL_DEFAULT] =
346 &posix_acl_default_xattr_handler,
347 [BCH_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
348 [BCH_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
351 const struct xattr_handler *bch_xattr_handlers[] = {
352 &bch_xattr_user_handler,
353 &posix_acl_access_xattr_handler,
354 &posix_acl_default_xattr_handler,
355 &bch_xattr_trusted_handler,
356 &bch_xattr_security_handler,
360 static const struct xattr_handler *bch_xattr_type_to_handler(unsigned type)
362 return type < ARRAY_SIZE(bch_xattr_handler_map)
363 ? bch_xattr_handler_map[type]