blob: a73c1bdc7b0532e175bbd7d756b483983021cc05 [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 Jain262c96a2019-03-01 12:34:50 +080058static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
59 const char *name, const char *value, size_t value_len,
60 int flags)
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061{
62 const struct prop_handler *handler;
63 int ret;
64
65 if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
66 return -EINVAL;
67
68 handler = find_prop_handler(name, NULL);
69 if (!handler)
70 return -EINVAL;
71
72 if (value_len == 0) {
Anand Jain04e68632019-04-12 16:02:58 +080073 if (trans)
74 ret = btrfs_setxattr(trans, inode, handler->xattr_name,
75 NULL, 0, flags);
76 else
77 ret = btrfs_setxattr_trans(NULL, inode,
78 handler->xattr_name, NULL, 0,
79 flags);
Filipe David Borba Manana63541922014-01-07 11:47:46 +000080 if (ret)
81 return ret;
82
83 ret = handler->apply(inode, NULL, 0);
84 ASSERT(ret == 0);
85
86 return ret;
87 }
88
89 ret = handler->validate(value, value_len);
90 if (ret)
91 return ret;
Anand Jain04e68632019-04-12 16:02:58 +080092 if (trans)
93 ret = btrfs_setxattr(trans, inode, handler->xattr_name, value,
94 value_len, flags);
95 else
96 ret = btrfs_setxattr_trans(NULL, inode, handler->xattr_name,
97 value, value_len, flags);
98
Filipe David Borba Manana63541922014-01-07 11:47:46 +000099 if (ret)
100 return ret;
101 ret = handler->apply(inode, value, value_len);
102 if (ret) {
Anand Jain04e68632019-04-12 16:02:58 +0800103 if (trans)
104 btrfs_setxattr(trans, inode, handler->xattr_name, NULL,
105 0, flags);
106 else
107 btrfs_setxattr_trans(NULL, inode, handler->xattr_name,
108 NULL, 0, flags);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000109 return ret;
110 }
111
112 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
113
114 return 0;
115}
116
Anand Jain262c96a2019-03-01 12:34:50 +0800117int btrfs_set_prop_trans(struct inode *inode, const char *name,
118 const char *value, size_t value_len, int flags)
119{
120 return btrfs_set_prop(NULL, inode, name, value, value_len, flags);
121}
122
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000123static int iterate_object_props(struct btrfs_root *root,
124 struct btrfs_path *path,
125 u64 objectid,
126 void (*iterator)(void *,
127 const struct prop_handler *,
128 const char *,
129 size_t),
130 void *ctx)
131{
132 int ret;
133 char *name_buf = NULL;
134 char *value_buf = NULL;
135 int name_buf_len = 0;
136 int value_buf_len = 0;
137
138 while (1) {
139 struct btrfs_key key;
140 struct btrfs_dir_item *di;
141 struct extent_buffer *leaf;
142 u32 total_len, cur, this_len;
143 int slot;
144 const struct hlist_head *handlers;
145
146 slot = path->slots[0];
147 leaf = path->nodes[0];
148
149 if (slot >= btrfs_header_nritems(leaf)) {
150 ret = btrfs_next_leaf(root, path);
151 if (ret < 0)
152 goto out;
153 else if (ret > 0)
154 break;
155 continue;
156 }
157
158 btrfs_item_key_to_cpu(leaf, &key, slot);
159 if (key.objectid != objectid)
160 break;
161 if (key.type != BTRFS_XATTR_ITEM_KEY)
162 break;
163
164 handlers = find_prop_handlers_by_hash(key.offset);
165 if (!handlers)
166 goto next_slot;
167
168 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
169 cur = 0;
170 total_len = btrfs_item_size_nr(leaf, slot);
171
172 while (cur < total_len) {
173 u32 name_len = btrfs_dir_name_len(leaf, di);
174 u32 data_len = btrfs_dir_data_len(leaf, di);
175 unsigned long name_ptr, data_ptr;
176 const struct prop_handler *handler;
177
178 this_len = sizeof(*di) + name_len + data_len;
179 name_ptr = (unsigned long)(di + 1);
180 data_ptr = name_ptr + name_len;
181
182 if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
183 memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
184 name_ptr,
185 XATTR_BTRFS_PREFIX_LEN))
186 goto next_dir_item;
187
188 if (name_len >= name_buf_len) {
189 kfree(name_buf);
190 name_buf_len = name_len + 1;
191 name_buf = kmalloc(name_buf_len, GFP_NOFS);
192 if (!name_buf) {
193 ret = -ENOMEM;
194 goto out;
195 }
196 }
197 read_extent_buffer(leaf, name_buf, name_ptr, name_len);
198 name_buf[name_len] = '\0';
199
200 handler = find_prop_handler(name_buf, handlers);
201 if (!handler)
202 goto next_dir_item;
203
204 if (data_len > value_buf_len) {
205 kfree(value_buf);
206 value_buf_len = data_len;
207 value_buf = kmalloc(data_len, GFP_NOFS);
208 if (!value_buf) {
209 ret = -ENOMEM;
210 goto out;
211 }
212 }
213 read_extent_buffer(leaf, value_buf, data_ptr, data_len);
214
215 iterator(ctx, handler, value_buf, data_len);
216next_dir_item:
217 cur += this_len;
218 di = (struct btrfs_dir_item *)((char *) di + this_len);
219 }
220
221next_slot:
222 path->slots[0]++;
223 }
224
225 ret = 0;
226out:
227 btrfs_release_path(path);
228 kfree(name_buf);
229 kfree(value_buf);
230
231 return ret;
232}
233
234static void inode_prop_iterator(void *ctx,
235 const struct prop_handler *handler,
236 const char *value,
237 size_t len)
238{
239 struct inode *inode = ctx;
240 struct btrfs_root *root = BTRFS_I(inode)->root;
241 int ret;
242
243 ret = handler->apply(inode, value, len);
244 if (unlikely(ret))
245 btrfs_warn(root->fs_info,
246 "error applying prop %s to ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200247 handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000248 root->root_key.objectid, ret);
249 else
250 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
251}
252
253int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
254{
255 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200256 u64 ino = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000257 int ret;
258
259 ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
260
261 return ret;
262}
263
Anand Jain3dcf96c2019-03-01 12:34:48 +0800264static int prop_compression_validate(const char *value, size_t len)
265{
266 if (!value)
267 return 0;
268
269 if (!strncmp("lzo", value, 3))
270 return 0;
271 else if (!strncmp("zlib", value, 4))
272 return 0;
273 else if (!strncmp("zstd", value, 4))
274 return 0;
275
276 return -EINVAL;
277}
278
279static int prop_compression_apply(struct inode *inode, const char *value,
280 size_t len)
281{
282 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
283 int type;
284
285 if (len == 0) {
286 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
287 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
288 BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
289
290 return 0;
291 }
292
293 if (!strncmp("lzo", value, 3)) {
294 type = BTRFS_COMPRESS_LZO;
295 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
296 } else if (!strncmp("zlib", value, 4)) {
297 type = BTRFS_COMPRESS_ZLIB;
298 } else if (!strncmp("zstd", value, 4)) {
299 type = BTRFS_COMPRESS_ZSTD;
300 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
301 } else {
302 return -EINVAL;
303 }
304
305 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
306 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
307 BTRFS_I(inode)->prop_compress = type;
308
309 return 0;
310}
311
312static const char *prop_compression_extract(struct inode *inode)
313{
314 switch (BTRFS_I(inode)->prop_compress) {
315 case BTRFS_COMPRESS_ZLIB:
316 case BTRFS_COMPRESS_LZO:
317 case BTRFS_COMPRESS_ZSTD:
318 return btrfs_compress_type2str(BTRFS_I(inode)->prop_compress);
319 default:
320 break;
321 }
322
323 return NULL;
324}
325
326static struct prop_handler prop_handlers[] = {
327 {
328 .xattr_name = XATTR_BTRFS_PREFIX "compression",
329 .validate = prop_compression_validate,
330 .apply = prop_compression_apply,
331 .extract = prop_compression_extract,
332 .inheritable = 1
333 },
334};
335
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000336static int inherit_props(struct btrfs_trans_handle *trans,
337 struct inode *inode,
338 struct inode *parent)
339{
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000340 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400341 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000342 int ret;
Byongho Lee619ed392015-10-08 20:49:34 +0900343 int i;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000344
345 if (!test_bit(BTRFS_INODE_HAS_PROPS,
346 &BTRFS_I(parent)->runtime_flags))
347 return 0;
348
Byongho Lee619ed392015-10-08 20:49:34 +0900349 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
350 const struct prop_handler *h = &prop_handlers[i];
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000351 const char *value;
352 u64 num_bytes;
353
354 if (!h->inheritable)
355 continue;
356
357 value = h->extract(parent);
358 if (!value)
359 continue;
360
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800361 /*
362 * This is not strictly necessary as the property should be
363 * valid, but in case it isn't, don't propagate it futher.
364 */
365 ret = h->validate(value, strlen(value));
366 if (ret)
367 continue;
368
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400369 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000370 ret = btrfs_block_rsv_add(root, trans->block_rsv,
371 num_bytes, BTRFS_RESERVE_NO_FLUSH);
372 if (ret)
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800373 return ret;
374
Anand Jain04e68632019-04-12 16:02:58 +0800375 ret = btrfs_setxattr(trans, inode, h->xattr_name, value,
376 strlen(value), 0);
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800377 if (!ret) {
378 ret = h->apply(inode, value, strlen(value));
379 if (ret)
Anand Jain04e68632019-04-12 16:02:58 +0800380 btrfs_setxattr(trans, inode, h->xattr_name,
381 NULL, 0, 0);
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800382 else
383 set_bit(BTRFS_INODE_HAS_PROPS,
384 &BTRFS_I(inode)->runtime_flags);
385 }
386
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400387 btrfs_block_rsv_release(fs_info, trans->block_rsv, num_bytes);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000388 if (ret)
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800389 return ret;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000390 }
Anand Jain8b4d1ef2019-04-02 18:07:41 +0800391
392 return 0;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000393}
394
395int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
396 struct inode *inode,
397 struct inode *dir)
398{
399 if (!dir)
400 return 0;
401
402 return inherit_props(trans, inode, dir);
403}
404
405int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root,
407 struct btrfs_root *parent_root)
408{
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400409 struct super_block *sb = root->fs_info->sb;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000410 struct btrfs_key key;
411 struct inode *parent_inode, *child_inode;
412 int ret;
413
414 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
415 key.type = BTRFS_INODE_ITEM_KEY;
416 key.offset = 0;
417
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400418 parent_inode = btrfs_iget(sb, &key, parent_root, NULL);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000419 if (IS_ERR(parent_inode))
420 return PTR_ERR(parent_inode);
421
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400422 child_inode = btrfs_iget(sb, &key, root, NULL);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000423 if (IS_ERR(child_inode)) {
424 iput(parent_inode);
425 return PTR_ERR(child_inode);
426 }
427
428 ret = inherit_props(trans, child_inode, parent_inode);
429 iput(child_inode);
430 iput(parent_inode);
431
432 return ret;
433}
434
Anand Jain3dcf96c2019-03-01 12:34:48 +0800435void __init btrfs_props_init(void)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000436{
Anand Jain3dcf96c2019-03-01 12:34:48 +0800437 int i;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000438
Anand Jain3dcf96c2019-03-01 12:34:48 +0800439 hash_init(prop_handlers_ht);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000440
Anand Jain3dcf96c2019-03-01 12:34:48 +0800441 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
442 struct prop_handler *p = &prop_handlers[i];
443 u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000444
Anand Jain3dcf96c2019-03-01 12:34:48 +0800445 hash_add(prop_handlers_ht, &p->node, h);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000446 }
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000447}
448