1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2014 Filipe David Borba Manana <fdmanana@gmail.com>
4 */
5
6 #include <linux/hashtable.h>
7 #include <linux/xattr.h>
8 #include "messages.h"
9 #include "props.h"
10 #include "btrfs_inode.h"
11 #include "transaction.h"
12 #include "ctree.h"
13 #include "xattr.h"
14 #include "compression.h"
15 #include "space-info.h"
16 #include "fs.h"
17 #include "accessors.h"
18 #include "super.h"
19 #include "dir-item.h"
20
21 #define BTRFS_PROP_HANDLERS_HT_BITS 8
22 static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
23
24 struct prop_handler {
25 struct hlist_node node;
26 const char *xattr_name;
27 int (*validate)(const struct btrfs_inode *inode, const char *value,
28 size_t len);
29 int (*apply)(struct btrfs_inode *inode, const char *value, size_t len);
30 const char *(*extract)(const struct btrfs_inode *inode);
31 bool (*ignore)(const struct btrfs_inode *inode);
32 int inheritable;
33 };
34
find_prop_handlers_by_hash(const u64 hash)35 static const struct hlist_head *find_prop_handlers_by_hash(const u64 hash)
36 {
37 struct hlist_head *h;
38
39 h = &prop_handlers_ht[hash_min(hash, BTRFS_PROP_HANDLERS_HT_BITS)];
40 if (hlist_empty(h))
41 return NULL;
42
43 return h;
44 }
45
46 static const struct prop_handler *
find_prop_handler(const char * name,const struct hlist_head * handlers)47 find_prop_handler(const char *name,
48 const struct hlist_head *handlers)
49 {
50 struct prop_handler *h;
51
52 if (!handlers) {
53 u64 hash = btrfs_name_hash(name, strlen(name));
54
55 handlers = find_prop_handlers_by_hash(hash);
56 if (!handlers)
57 return NULL;
58 }
59
60 hlist_for_each_entry(h, handlers, node)
61 if (!strcmp(h->xattr_name, name))
62 return h;
63
64 return NULL;
65 }
66
btrfs_validate_prop(const struct btrfs_inode * inode,const char * name,const char * value,size_t value_len)67 int btrfs_validate_prop(const struct btrfs_inode *inode, const char *name,
68 const char *value, size_t value_len)
69 {
70 const struct prop_handler *handler;
71
72 if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
73 return -EINVAL;
74
75 handler = find_prop_handler(name, NULL);
76 if (!handler)
77 return -EINVAL;
78
79 if (value_len == 0)
80 return 0;
81
82 return handler->validate(inode, value, value_len);
83 }
84
85 /*
86 * Check if a property should be ignored (not set) for an inode.
87 *
88 * @inode: The target inode.
89 * @name: The property's name.
90 *
91 * The caller must be sure the given property name is valid, for example by
92 * having previously called btrfs_validate_prop().
93 *
94 * Returns: true if the property should be ignored for the given inode
95 * false if the property must not be ignored for the given inode
96 */
btrfs_ignore_prop(const struct btrfs_inode * inode,const char * name)97 bool btrfs_ignore_prop(const struct btrfs_inode *inode, const char *name)
98 {
99 const struct prop_handler *handler;
100
101 handler = find_prop_handler(name, NULL);
102 ASSERT(handler != NULL);
103
104 return handler->ignore(inode);
105 }
106
btrfs_set_prop(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,const char * name,const char * value,size_t value_len,int flags)107 int btrfs_set_prop(struct btrfs_trans_handle *trans, struct btrfs_inode *inode,
108 const char *name, const char *value, size_t value_len,
109 int flags)
110 {
111 const struct prop_handler *handler;
112 int ret;
113
114 handler = find_prop_handler(name, NULL);
115 if (!handler)
116 return -EINVAL;
117
118 if (value_len == 0) {
119 ret = btrfs_setxattr(trans, &inode->vfs_inode, handler->xattr_name,
120 NULL, 0, flags);
121 if (ret)
122 return ret;
123
124 ret = handler->apply(inode, NULL, 0);
125 ASSERT(ret == 0);
126
127 return ret;
128 }
129
130 ret = handler->validate(inode, value, value_len);
131 if (ret)
132 return ret;
133 ret = btrfs_setxattr(trans, &inode->vfs_inode, handler->xattr_name, value,
134 value_len, flags);
135 if (ret)
136 return ret;
137 ret = handler->apply(inode, value, value_len);
138 /* We validated before, so it should not fail here. */
139 ASSERT(ret == 0);
140 if (unlikely(ret)) {
141 int ret2;
142
143 /* Try to delete xattr, if not possible abort transaction. */
144 ret2 = btrfs_setxattr(trans, &inode->vfs_inode, handler->xattr_name,
145 NULL, 0, flags);
146 if (unlikely(ret2))
147 btrfs_abort_transaction(trans, ret2);
148 return ret;
149 }
150
151 set_bit(BTRFS_INODE_HAS_PROPS, &inode->runtime_flags);
152
153 return 0;
154 }
155
iterate_object_props(struct btrfs_root * root,struct btrfs_path * path,u64 objectid,void (* iterator)(void *,const struct prop_handler *,const char *,size_t),void * ctx)156 static int iterate_object_props(struct btrfs_root *root,
157 struct btrfs_path *path,
158 u64 objectid,
159 void (*iterator)(void *,
160 const struct prop_handler *,
161 const char *,
162 size_t),
163 void *ctx)
164 {
165 int ret;
166 char *name_buf = NULL;
167 char *value_buf = NULL;
168 int name_buf_len = 0;
169 int value_buf_len = 0;
170
171 while (1) {
172 struct btrfs_key key;
173 struct btrfs_dir_item *di;
174 struct extent_buffer *leaf;
175 u32 total_len, cur, this_len;
176 int slot;
177 const struct hlist_head *handlers;
178
179 slot = path->slots[0];
180 leaf = path->nodes[0];
181
182 if (slot >= btrfs_header_nritems(leaf)) {
183 ret = btrfs_next_leaf(root, path);
184 if (ret < 0)
185 goto out;
186 else if (ret > 0)
187 break;
188 continue;
189 }
190
191 btrfs_item_key_to_cpu(leaf, &key, slot);
192 if (key.objectid != objectid)
193 break;
194 if (key.type != BTRFS_XATTR_ITEM_KEY)
195 break;
196
197 handlers = find_prop_handlers_by_hash(key.offset);
198 if (!handlers)
199 goto next_slot;
200
201 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
202 cur = 0;
203 total_len = btrfs_item_size(leaf, slot);
204
205 while (cur < total_len) {
206 u32 name_len = btrfs_dir_name_len(leaf, di);
207 u32 data_len = btrfs_dir_data_len(leaf, di);
208 unsigned long name_ptr, data_ptr;
209 const struct prop_handler *handler;
210
211 this_len = sizeof(*di) + name_len + data_len;
212 name_ptr = (unsigned long)(di + 1);
213 data_ptr = name_ptr + name_len;
214
215 if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
216 memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
217 name_ptr,
218 XATTR_BTRFS_PREFIX_LEN))
219 goto next_dir_item;
220
221 if (name_len >= name_buf_len) {
222 kfree(name_buf);
223 name_buf_len = name_len + 1;
224 name_buf = kmalloc(name_buf_len, GFP_NOFS);
225 if (!name_buf) {
226 ret = -ENOMEM;
227 goto out;
228 }
229 }
230 read_extent_buffer(leaf, name_buf, name_ptr, name_len);
231 name_buf[name_len] = '\0';
232
233 handler = find_prop_handler(name_buf, handlers);
234 if (!handler)
235 goto next_dir_item;
236
237 if (data_len > value_buf_len) {
238 kfree(value_buf);
239 value_buf_len = data_len;
240 value_buf = kmalloc(data_len, GFP_NOFS);
241 if (!value_buf) {
242 ret = -ENOMEM;
243 goto out;
244 }
245 }
246 read_extent_buffer(leaf, value_buf, data_ptr, data_len);
247
248 iterator(ctx, handler, value_buf, data_len);
249 next_dir_item:
250 cur += this_len;
251 di = (struct btrfs_dir_item *)((char *) di + this_len);
252 }
253
254 next_slot:
255 path->slots[0]++;
256 }
257
258 ret = 0;
259 out:
260 btrfs_release_path(path);
261 kfree(name_buf);
262 kfree(value_buf);
263
264 return ret;
265 }
266
inode_prop_iterator(void * ctx,const struct prop_handler * handler,const char * value,size_t len)267 static void inode_prop_iterator(void *ctx,
268 const struct prop_handler *handler,
269 const char *value,
270 size_t len)
271 {
272 struct inode *inode = ctx;
273 struct btrfs_root *root = BTRFS_I(inode)->root;
274 int ret;
275
276 ret = handler->apply(BTRFS_I(inode), value, len);
277 if (unlikely(ret))
278 btrfs_warn(root->fs_info,
279 "error applying prop %s to ino %llu (root %llu): %d",
280 handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
281 btrfs_root_id(root), ret);
282 else
283 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
284 }
285
btrfs_load_inode_props(struct btrfs_inode * inode,struct btrfs_path * path)286 int btrfs_load_inode_props(struct btrfs_inode *inode, struct btrfs_path *path)
287 {
288 struct btrfs_root *root = inode->root;
289 u64 ino = btrfs_ino(inode);
290
291 return iterate_object_props(root, path, ino, inode_prop_iterator,
292 &inode->vfs_inode);
293 }
294
prop_compression_validate(const struct btrfs_inode * inode,const char * value,size_t len)295 static int prop_compression_validate(const struct btrfs_inode *inode,
296 const char *value, size_t len)
297 {
298 if (!btrfs_inode_can_compress(inode))
299 return -EINVAL;
300
301 if (!value)
302 return 0;
303
304 if (btrfs_compress_is_valid_type(value, len))
305 return 0;
306
307 if ((len == 2 && strncmp("no", value, 2) == 0) ||
308 (len == 4 && strncmp("none", value, 4) == 0))
309 return 0;
310
311 return -EINVAL;
312 }
313
prop_compression_apply(struct btrfs_inode * inode,const char * value,size_t len)314 static int prop_compression_apply(struct btrfs_inode *inode, const char *value,
315 size_t len)
316 {
317 struct btrfs_fs_info *fs_info = inode->root->fs_info;
318 int type;
319
320 /* Reset to defaults */
321 if (len == 0) {
322 inode->flags &= ~BTRFS_INODE_COMPRESS;
323 inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
324 inode->prop_compress = BTRFS_COMPRESS_NONE;
325 return 0;
326 }
327
328 /* Set NOCOMPRESS flag */
329 if ((len == 2 && strncmp("no", value, 2) == 0) ||
330 (len == 4 && strncmp("none", value, 4) == 0)) {
331 inode->flags |= BTRFS_INODE_NOCOMPRESS;
332 inode->flags &= ~BTRFS_INODE_COMPRESS;
333 inode->prop_compress = BTRFS_COMPRESS_NONE;
334
335 return 0;
336 }
337
338 if (!strncmp("lzo", value, 3)) {
339 type = BTRFS_COMPRESS_LZO;
340 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
341 } else if (!strncmp("zlib", value, 4)) {
342 type = BTRFS_COMPRESS_ZLIB;
343 } else if (!strncmp("zstd", value, 4)) {
344 type = BTRFS_COMPRESS_ZSTD;
345 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
346 } else {
347 return -EINVAL;
348 }
349
350 inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
351 inode->flags |= BTRFS_INODE_COMPRESS;
352 inode->prop_compress = type;
353
354 return 0;
355 }
356
prop_compression_ignore(const struct btrfs_inode * inode)357 static bool prop_compression_ignore(const struct btrfs_inode *inode)
358 {
359 /*
360 * Compression only has effect for regular files, and for directories
361 * we set it just to propagate it to new files created inside them.
362 * Everything else (symlinks, devices, sockets, fifos) is pointless as
363 * it will do nothing, so don't waste metadata space on a compression
364 * xattr for anything that is neither a file nor a directory.
365 */
366 if (!S_ISREG(inode->vfs_inode.i_mode) &&
367 !S_ISDIR(inode->vfs_inode.i_mode))
368 return true;
369
370 return false;
371 }
372
prop_compression_extract(const struct btrfs_inode * inode)373 static const char *prop_compression_extract(const struct btrfs_inode *inode)
374 {
375 switch (inode->prop_compress) {
376 case BTRFS_COMPRESS_ZLIB:
377 case BTRFS_COMPRESS_LZO:
378 case BTRFS_COMPRESS_ZSTD:
379 return btrfs_compress_type2str(inode->prop_compress);
380 default:
381 break;
382 }
383
384 return NULL;
385 }
386
387 static struct prop_handler prop_handlers[] = {
388 {
389 .xattr_name = XATTR_BTRFS_PREFIX "compression",
390 .validate = prop_compression_validate,
391 .apply = prop_compression_apply,
392 .extract = prop_compression_extract,
393 .ignore = prop_compression_ignore,
394 .inheritable = 1
395 },
396 };
397
btrfs_inode_inherit_props(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,const struct btrfs_inode * parent)398 int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
399 struct btrfs_inode *inode,
400 const struct btrfs_inode *parent)
401 {
402 struct btrfs_root *root = inode->root;
403 struct btrfs_fs_info *fs_info = root->fs_info;
404 int ret;
405 int i;
406 bool need_reserve = false;
407
408 if (!test_bit(BTRFS_INODE_HAS_PROPS, &parent->runtime_flags))
409 return 0;
410
411 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
412 const struct prop_handler *h = &prop_handlers[i];
413 const char *value;
414 u64 num_bytes = 0;
415
416 if (!h->inheritable)
417 continue;
418
419 if (h->ignore(inode))
420 continue;
421
422 value = h->extract(parent);
423 if (!value)
424 continue;
425
426 /*
427 * This is not strictly necessary as the property should be
428 * valid, but in case it isn't, don't propagate it further.
429 */
430 ret = h->validate(inode, value, strlen(value));
431 if (ret)
432 continue;
433
434 /*
435 * Currently callers should be reserving 1 item for properties,
436 * since we only have 1 property that we currently support. If
437 * we add more in the future we need to try and reserve more
438 * space for them. But we should also revisit how we do space
439 * reservations if we do add more properties in the future.
440 */
441 if (need_reserve) {
442 num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
443 ret = btrfs_block_rsv_add(fs_info, trans->block_rsv,
444 num_bytes,
445 BTRFS_RESERVE_NO_FLUSH);
446 if (ret)
447 return ret;
448 }
449
450 ret = btrfs_setxattr(trans, &inode->vfs_inode, h->xattr_name, value,
451 strlen(value), 0);
452 if (!ret) {
453 ret = h->apply(inode, value, strlen(value));
454 if (ret)
455 btrfs_setxattr(trans, &inode->vfs_inode, h->xattr_name,
456 NULL, 0, 0);
457 else
458 set_bit(BTRFS_INODE_HAS_PROPS, &inode->runtime_flags);
459 }
460
461 if (need_reserve) {
462 btrfs_block_rsv_release(fs_info, trans->block_rsv,
463 num_bytes, NULL);
464 if (ret)
465 return ret;
466 }
467 need_reserve = true;
468 }
469
470 return 0;
471 }
472
btrfs_props_init(void)473 int __init btrfs_props_init(void)
474 {
475 int i;
476
477 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
478 struct prop_handler *p = &prop_handlers[i];
479 u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
480
481 hash_add(prop_handlers_ht, &p->node, h);
482 }
483 return 0;
484 }
485
486