blob: aedf5a7d69c9129457f7a3202e1d1000cd1624c4 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Filipe David Borba Manana63541922014-01-07 11:47:46 +00002/*
3 * Copyright (C) 2014 Filipe David Borba Manana <fdmanana@gmail.com>
Filipe David Borba Manana63541922014-01-07 11:47:46 +00004 */
5
6#include <linux/hashtable.h>
7#include "props.h"
8#include "btrfs_inode.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009#include "transaction.h"
Nikolay Borisov9678c542018-01-08 11:45:05 +020010#include "ctree.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000011#include "xattr.h"
Anand Jainebb87652016-03-10 17:26:59 +080012#include "compression.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000013
14#define BTRFS_PROP_HANDLERS_HT_BITS 8
15static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
16
17struct prop_handler {
18 struct hlist_node node;
19 const char *xattr_name;
20 int (*validate)(const char *value, size_t len);
21 int (*apply)(struct inode *inode, const char *value, size_t len);
22 const char *(*extract)(struct inode *inode);
23 int inheritable;
24};
25
Filipe David Borba Manana63541922014-01-07 11:47:46 +000026static const struct hlist_head *find_prop_handlers_by_hash(const u64 hash)
27{
28 struct hlist_head *h;
29
30 h = &prop_handlers_ht[hash_min(hash, BTRFS_PROP_HANDLERS_HT_BITS)];
31 if (hlist_empty(h))
32 return NULL;
33
34 return h;
35}
36
37static const struct prop_handler *
38find_prop_handler(const char *name,
39 const struct hlist_head *handlers)
40{
41 struct prop_handler *h;
42
43 if (!handlers) {
44 u64 hash = btrfs_name_hash(name, strlen(name));
45
46 handlers = find_prop_handlers_by_hash(hash);
47 if (!handlers)
48 return NULL;
49 }
50
51 hlist_for_each_entry(h, handlers, node)
52 if (!strcmp(h->xattr_name, name))
53 return h;
54
55 return NULL;
56}
57
Anand Jainf22125e2019-04-20 19:48:51 +080058int btrfs_validate_prop(const char *name, const char *value, size_t value_len)
59{
60 const struct prop_handler *handler;
61
62 if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
63 return -EINVAL;
64
65 handler = find_prop_handler(name, NULL);
66 if (!handler)
67 return -EINVAL;
68
69 if (value_len == 0)
70 return 0;
71
72 return handler->validate(value, value_len);
73}
74
Anand Jaincd31af12019-04-20 19:48:52 +080075int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
76 const char *name, const char *value, size_t value_len,
77 int flags)
Filipe David Borba Manana63541922014-01-07 11:47:46 +000078{
79 const struct prop_handler *handler;
80 int ret;
81
Filipe David Borba Manana63541922014-01-07 11:47:46 +000082 handler = find_prop_handler(name, NULL);
83 if (!handler)
84 return -EINVAL;
85
86 if (value_len == 0) {
Anand Jain04e68632019-04-12 16:02:58 +080087 if (trans)
88 ret = btrfs_setxattr(trans, inode, handler->xattr_name,
89 NULL, 0, flags);
90 else
Anand Jaine3de9b12019-04-12 16:02:59 +080091 ret = btrfs_setxattr_trans(inode, handler->xattr_name,
92 NULL, 0, flags);
Filipe David Borba Manana63541922014-01-07 11:47:46 +000093 if (ret)
94 return ret;
95
96 ret = handler->apply(inode, NULL, 0);
97 ASSERT(ret == 0);
98
99 return ret;
100 }
101
Anand Jain04e68632019-04-12 16:02:58 +0800102 if (trans)
103 ret = btrfs_setxattr(trans, inode, handler->xattr_name, value,
104 value_len, flags);
105 else
Anand Jaine3de9b12019-04-12 16:02:59 +0800106 ret = btrfs_setxattr_trans(inode, handler->xattr_name, value,
107 value_len, flags);
Anand Jain04e68632019-04-12 16:02:58 +0800108
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000109 if (ret)
110 return ret;
111 ret = handler->apply(inode, value, value_len);
112 if (ret) {
Anand Jain04e68632019-04-12 16:02:58 +0800113 if (trans)
114 btrfs_setxattr(trans, inode, handler->xattr_name, NULL,
115 0, flags);
116 else
Anand Jaine3de9b12019-04-12 16:02:59 +0800117 btrfs_setxattr_trans(inode, handler->xattr_name, NULL,
118 0, flags);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000119 return ret;
120 }
121
122 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
123
124 return 0;
125}
126
Anand Jain262c96a2019-03-01 12:34:50 +0800127int btrfs_set_prop_trans(struct inode *inode, const char *name,
128 const char *value, size_t value_len, int flags)
129{
130 return btrfs_set_prop(NULL, inode, name, value, value_len, flags);
131}
132
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000133static int iterate_object_props(struct btrfs_root *root,
134 struct btrfs_path *path,
135 u64 objectid,
136 void (*iterator)(void *,
137 const struct prop_handler *,
138 const char *,
139 size_t),
140 void *ctx)
141{
142 int ret;
143 char *name_buf = NULL;
144 char *value_buf = NULL;
145 int name_buf_len = 0;
146 int value_buf_len = 0;
147
148 while (1) {
149 struct btrfs_key key;
150 struct btrfs_dir_item *di;
151 struct extent_buffer *leaf;
152 u32 total_len, cur, this_len;
153 int slot;
154 const struct hlist_head *handlers;
155
156 slot = path->slots[0];
157 leaf = path->nodes[0];
158
159 if (slot >= btrfs_header_nritems(leaf)) {
160 ret = btrfs_next_leaf(root, path);
161 if (ret < 0)
162 goto out;
163 else if (ret > 0)
164 break;
165 continue;
166 }
167
168 btrfs_item_key_to_cpu(leaf, &key, slot);
169 if (key.objectid != objectid)
170 break;
171 if (key.type != BTRFS_XATTR_ITEM_KEY)
172 break;
173
174 handlers = find_prop_handlers_by_hash(key.offset);
175 if (!handlers)
176 goto next_slot;
177
178 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
179 cur = 0;
180 total_len = btrfs_item_size_nr(leaf, slot);
181
182 while (cur < total_len) {
183 u32 name_len = btrfs_dir_name_len(leaf, di);
184 u32 data_len = btrfs_dir_data_len(leaf, di);
185 unsigned long name_ptr, data_ptr;
186 const struct prop_handler *handler;
187
188 this_len = sizeof(*di) + name_len + data_len;
189 name_ptr = (unsigned long)(di + 1);
190 data_ptr = name_ptr + name_len;
191
192 if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
193 memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
194 name_ptr,
195 XATTR_BTRFS_PREFIX_LEN))
196 goto next_dir_item;
197
198 if (name_len >= name_buf_len) {
199 kfree(name_buf);
200 name_buf_len = name_len + 1;
201 name_buf = kmalloc(name_buf_len, GFP_NOFS);
202 if (!name_buf) {
203 ret = -ENOMEM;
204 goto out;
205 }
206 }
207 read_extent_buffer(leaf, name_buf, name_ptr, name_len);
208 name_buf[name_len] = '\0';
209
210 handler = find_prop_handler(name_buf, handlers);
211 if (!handler)
212 goto next_dir_item;
213
214 if (data_len > value_buf_len) {
215 kfree(value_buf);
216 value_buf_len = data_len;
217 value_buf = kmalloc(data_len, GFP_NOFS);
218 if (!value_buf) {
219 ret = -ENOMEM;
220 goto out;
221 }
222 }
223 read_extent_buffer(leaf, value_buf, data_ptr, data_len);
224
225 iterator(ctx, handler, value_buf, data_len);
226next_dir_item:
227 cur += this_len;
228 di = (struct btrfs_dir_item *)((char *) di + this_len);
229 }
230
231next_slot:
232 path->slots[0]++;
233 }
234
235 ret = 0;
236out:
237 btrfs_release_path(path);
238 kfree(name_buf);
239 kfree(value_buf);
240
241 return ret;
242}
243
244static void inode_prop_iterator(void *ctx,
245 const struct prop_handler *handler,
246 const char *value,
247 size_t len)
248{
249 struct inode *inode = ctx;
250 struct btrfs_root *root = BTRFS_I(inode)->root;
251 int ret;
252
253 ret = handler->apply(inode, value, len);
254 if (unlikely(ret))
255 btrfs_warn(root->fs_info,
256 "error applying prop %s to ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200257 handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000258 root->root_key.objectid, ret);
259 else
260 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
261}
262
263int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
264{
265 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200266 u64 ino = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000267 int ret;
268
269 ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
270
271 return ret;
272}
273
Anand Jain3dcf96c2019-03-01 12:34:48 +0800274static int prop_compression_validate(const char *value, size_t len)
275{
276 if (!value)
277 return 0;
278
279 if (!strncmp("lzo", value, 3))
280 return 0;
281 else if (!strncmp("zlib", value, 4))
282 return 0;
283 else if (!strncmp("zstd", value, 4))
284 return 0;
285
286 return -EINVAL;
287}
288
289static int prop_compression_apply(struct inode *inode, const char *value,
290 size_t len)
291{
292 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
293 int type;
294
295 if (len == 0) {
296 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
297 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
298 BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
299
300 return 0;
301 }
302
303 if (!strncmp("lzo", value, 3)) {
304 type = BTRFS_COMPRESS_LZO;
305 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
306 } else if (!strncmp("zlib", value, 4)) {
307 type = BTRFS_COMPRESS_ZLIB;
308 } else if (!strncmp("zstd", value, 4)) {
309 type = BTRFS_COMPRESS_ZSTD;
310 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
311 } else {
312 return -EINVAL;
313 }
314
315 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
316 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
317 BTRFS_I(inode)->prop_compress = type;
318
319 return 0;
320}
321
322static const char *prop_compression_extract(struct inode *inode)
323{
324 switch (BTRFS_I(inode)->prop_compress) {
325 case BTRFS_COMPRESS_ZLIB:
326 case BTRFS_COMPRESS_LZO:
327 case BTRFS_COMPRESS_ZSTD:
328 return btrfs_compress_type2str(BTRFS_I(inode)->prop_compress);
329 default:
330 break;
331 }
332
333 return NULL;
334}
335
336static struct prop_handler prop_handlers[] = {
337 {
338 .xattr_name = XATTR_BTRFS_PREFIX "compression",
339 .validate = prop_compression_validate,
340 .apply = prop_compression_apply,
341 .extract = prop_compression_extract,
342 .inheritable = 1
343 },
344};
345
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000346static int inherit_props(struct btrfs_trans_handle *trans,
347 struct inode *inode,
348 struct inode *parent)
349{
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000350 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400351 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000352 int ret;
Byongho Lee619ed392015-10-08 20:49:34 +0900353 int i;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000354
355 if (!test_bit(BTRFS_INODE_HAS_PROPS,
356 &BTRFS_I(parent)->runtime_flags))
357 return 0;
358
Byongho Lee619ed392015-10-08 20:49:34 +0900359 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
360 const struct prop_handler *h = &prop_handlers[i];
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000361 const char *value;
362 u64 num_bytes;
363
364 if (!h->inheritable)
365 continue;
366
367 value = h->extract(parent);
368 if (!value)
369 continue;
370
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800371 /*
372 * This is not strictly necessary as the property should be
373 * valid, but in case it isn't, don't propagate it futher.
374 */
375 ret = h->validate(value, strlen(value));
376 if (ret)
377 continue;
378
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400379 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000380 ret = btrfs_block_rsv_add(root, trans->block_rsv,
381 num_bytes, BTRFS_RESERVE_NO_FLUSH);
382 if (ret)
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800383 return ret;
384
Anand Jain04e68632019-04-12 16:02:58 +0800385 ret = btrfs_setxattr(trans, inode, h->xattr_name, value,
386 strlen(value), 0);
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800387 if (!ret) {
388 ret = h->apply(inode, value, strlen(value));
389 if (ret)
Anand Jain04e68632019-04-12 16:02:58 +0800390 btrfs_setxattr(trans, inode, h->xattr_name,
391 NULL, 0, 0);
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800392 else
393 set_bit(BTRFS_INODE_HAS_PROPS,
394 &BTRFS_I(inode)->runtime_flags);
395 }
396
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400397 btrfs_block_rsv_release(fs_info, trans->block_rsv, num_bytes);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000398 if (ret)
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800399 return ret;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000400 }
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800401
402 return 0;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000403}
404
405int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
406 struct inode *inode,
407 struct inode *dir)
408{
409 if (!dir)
410 return 0;
411
412 return inherit_props(trans, inode, dir);
413}
414
415int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
416 struct btrfs_root *root,
417 struct btrfs_root *parent_root)
418{
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400419 struct super_block *sb = root->fs_info->sb;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000420 struct btrfs_key key;
421 struct inode *parent_inode, *child_inode;
422 int ret;
423
424 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
425 key.type = BTRFS_INODE_ITEM_KEY;
426 key.offset = 0;
427
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400428 parent_inode = btrfs_iget(sb, &key, parent_root, NULL);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000429 if (IS_ERR(parent_inode))
430 return PTR_ERR(parent_inode);
431
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400432 child_inode = btrfs_iget(sb, &key, root, NULL);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000433 if (IS_ERR(child_inode)) {
434 iput(parent_inode);
435 return PTR_ERR(child_inode);
436 }
437
438 ret = inherit_props(trans, child_inode, parent_inode);
439 iput(child_inode);
440 iput(parent_inode);
441
442 return ret;
443}
444
Anand Jain3dcf96c2019-03-01 12:34:48 +0800445void __init btrfs_props_init(void)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000446{
Anand Jain3dcf96c2019-03-01 12:34:48 +0800447 int i;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000448
Anand Jain3dcf96c2019-03-01 12:34:48 +0800449 hash_init(prop_handlers_ht);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000450
Anand Jain3dcf96c2019-03-01 12:34:48 +0800451 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
452 struct prop_handler *p = &prop_handlers[i];
453 u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000454
Anand Jain3dcf96c2019-03-01 12:34:48 +0800455 hash_add(prop_handlers_ht, &p->node, h);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000456 }
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000457}
458