btrfs: merge _btrfs_set_prop helpers
[linux-block.git] / fs / btrfs / props.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
63541927
FDBM
2/*
3 * Copyright (C) 2014 Filipe David Borba Manana <fdmanana@gmail.com>
63541927
FDBM
4 */
5
6#include <linux/hashtable.h>
7#include "props.h"
8#include "btrfs_inode.h"
63541927 9#include "transaction.h"
9678c543 10#include "ctree.h"
63541927 11#include "xattr.h"
ebb8765b 12#include "compression.h"
63541927
FDBM
13
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
26static int prop_compression_validate(const char *value, size_t len);
27static int prop_compression_apply(struct inode *inode,
28 const char *value,
29 size_t len);
30static const char *prop_compression_extract(struct inode *inode);
31
32static struct prop_handler prop_handlers[] = {
33 {
34 .xattr_name = XATTR_BTRFS_PREFIX "compression",
35 .validate = prop_compression_validate,
36 .apply = prop_compression_apply,
37 .extract = prop_compression_extract,
38 .inheritable = 1
39 },
63541927
FDBM
40};
41
42void __init btrfs_props_init(void)
43{
619ed392 44 int i;
63541927
FDBM
45
46 hash_init(prop_handlers_ht);
47
619ed392
BL
48 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
49 struct prop_handler *p = &prop_handlers[i];
63541927
FDBM
50 u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
51
52 hash_add(prop_handlers_ht, &p->node, h);
53 }
54}
55
56static const struct hlist_head *find_prop_handlers_by_hash(const u64 hash)
57{
58 struct hlist_head *h;
59
60 h = &prop_handlers_ht[hash_min(hash, BTRFS_PROP_HANDLERS_HT_BITS)];
61 if (hlist_empty(h))
62 return NULL;
63
64 return h;
65}
66
67static const struct prop_handler *
68find_prop_handler(const char *name,
69 const struct hlist_head *handlers)
70{
71 struct prop_handler *h;
72
73 if (!handlers) {
74 u64 hash = btrfs_name_hash(name, strlen(name));
75
76 handlers = find_prop_handlers_by_hash(hash);
77 if (!handlers)
78 return NULL;
79 }
80
81 hlist_for_each_entry(h, handlers, node)
82 if (!strcmp(h->xattr_name, name))
83 return h;
84
85 return NULL;
86}
87
7715da84
AJ
88int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
89 const char *name, const char *value, size_t value_len,
90 int flags)
63541927
FDBM
91{
92 const struct prop_handler *handler;
93 int ret;
94
95 if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
96 return -EINVAL;
97
98 handler = find_prop_handler(name, NULL);
99 if (!handler)
100 return -EINVAL;
101
102 if (value_len == 0) {
7852781d 103 ret = btrfs_setxattr(trans, inode, handler->xattr_name,
63541927
FDBM
104 NULL, 0, flags);
105 if (ret)
106 return ret;
107
108 ret = handler->apply(inode, NULL, 0);
109 ASSERT(ret == 0);
110
111 return ret;
112 }
113
114 ret = handler->validate(value, value_len);
115 if (ret)
116 return ret;
7852781d 117 ret = btrfs_setxattr(trans, inode, handler->xattr_name,
63541927
FDBM
118 value, value_len, flags);
119 if (ret)
120 return ret;
121 ret = handler->apply(inode, value, value_len);
122 if (ret) {
7852781d 123 btrfs_setxattr(trans, inode, handler->xattr_name,
63541927
FDBM
124 NULL, 0, flags);
125 return ret;
126 }
127
128 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
129
130 return 0;
131}
132
63541927
FDBM
133static 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",
4a0cc7ca 257 handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
63541927
FDBM
258 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;
4a0cc7ca 266 u64 ino = btrfs_ino(BTRFS_I(inode));
63541927
FDBM
267 int ret;
268
269 ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
270
271 return ret;
272}
273
274static int inherit_props(struct btrfs_trans_handle *trans,
275 struct inode *inode,
276 struct inode *parent)
277{
63541927 278 struct btrfs_root *root = BTRFS_I(inode)->root;
2ff7e61e 279 struct btrfs_fs_info *fs_info = root->fs_info;
63541927 280 int ret;
619ed392 281 int i;
63541927
FDBM
282
283 if (!test_bit(BTRFS_INODE_HAS_PROPS,
284 &BTRFS_I(parent)->runtime_flags))
285 return 0;
286
619ed392
BL
287 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
288 const struct prop_handler *h = &prop_handlers[i];
63541927
FDBM
289 const char *value;
290 u64 num_bytes;
291
292 if (!h->inheritable)
293 continue;
294
295 value = h->extract(parent);
296 if (!value)
297 continue;
298
2ff7e61e 299 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
63541927
FDBM
300 ret = btrfs_block_rsv_add(root, trans->block_rsv,
301 num_bytes, BTRFS_RESERVE_NO_FLUSH);
302 if (ret)
303 goto out;
7715da84
AJ
304 ret = btrfs_set_prop(trans, inode, h->xattr_name, value,
305 strlen(value), 0);
2ff7e61e 306 btrfs_block_rsv_release(fs_info, trans->block_rsv, num_bytes);
63541927
FDBM
307 if (ret)
308 goto out;
309 }
310 ret = 0;
311out:
312 return ret;
313}
314
315int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
316 struct inode *inode,
317 struct inode *dir)
318{
319 if (!dir)
320 return 0;
321
322 return inherit_props(trans, inode, dir);
323}
324
325int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
327 struct btrfs_root *parent_root)
328{
bd6c57dd 329 struct super_block *sb = root->fs_info->sb;
63541927
FDBM
330 struct btrfs_key key;
331 struct inode *parent_inode, *child_inode;
332 int ret;
333
334 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
335 key.type = BTRFS_INODE_ITEM_KEY;
336 key.offset = 0;
337
bd6c57dd 338 parent_inode = btrfs_iget(sb, &key, parent_root, NULL);
63541927
FDBM
339 if (IS_ERR(parent_inode))
340 return PTR_ERR(parent_inode);
341
bd6c57dd 342 child_inode = btrfs_iget(sb, &key, root, NULL);
63541927
FDBM
343 if (IS_ERR(child_inode)) {
344 iput(parent_inode);
345 return PTR_ERR(child_inode);
346 }
347
348 ret = inherit_props(trans, child_inode, parent_inode);
349 iput(child_inode);
350 iput(parent_inode);
351
352 return ret;
353}
354
355static int prop_compression_validate(const char *value, size_t len)
356{
272e5326 357 if (!strncmp("lzo", value, 3))
63541927 358 return 0;
272e5326 359 else if (!strncmp("zlib", value, 4))
63541927 360 return 0;
272e5326 361 else if (!strncmp("zstd", value, 4))
5c1aab1d 362 return 0;
63541927
FDBM
363
364 return -EINVAL;
365}
366
367static int prop_compression_apply(struct inode *inode,
368 const char *value,
369 size_t len)
370{
1a63c198 371 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
63541927
FDBM
372 int type;
373
374 if (len == 0) {
375 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
376 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
b52aa8c9 377 BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
63541927
FDBM
378
379 return 0;
380 }
381
1a63c198 382 if (!strncmp("lzo", value, 3)) {
63541927 383 type = BTRFS_COMPRESS_LZO;
1a63c198
MT
384 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
385 } else if (!strncmp("zlib", value, 4)) {
63541927 386 type = BTRFS_COMPRESS_ZLIB;
50398fde 387 } else if (!strncmp("zstd", value, 4)) {
5c1aab1d 388 type = BTRFS_COMPRESS_ZSTD;
1a63c198
MT
389 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
390 } else {
63541927 391 return -EINVAL;
1a63c198 392 }
63541927
FDBM
393
394 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
395 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
b52aa8c9 396 BTRFS_I(inode)->prop_compress = type;
63541927
FDBM
397
398 return 0;
399}
400
401static const char *prop_compression_extract(struct inode *inode)
402{
b52aa8c9 403 switch (BTRFS_I(inode)->prop_compress) {
63541927 404 case BTRFS_COMPRESS_ZLIB:
63541927 405 case BTRFS_COMPRESS_LZO:
5c1aab1d 406 case BTRFS_COMPRESS_ZSTD:
802a5c69
DS
407 return btrfs_compress_type2str(BTRFS_I(inode)->prop_compress);
408 default:
409 break;
63541927
FDBM
410 }
411
412 return NULL;
413}
e8c9f186
DS
414
415