blob: c52cd78fea3138a5cf04e6e176249d35a2b318f6 [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 Jain7715da82019-03-01 12:34:47 +080058int 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) {
David Sterba78527812018-02-27 15:48:52 +010073 ret = btrfs_setxattr(trans, inode, handler->xattr_name,
Filipe David Borba Manana63541922014-01-07 11:47:46 +000074 NULL, 0, flags);
75 if (ret)
76 return ret;
77
78 ret = handler->apply(inode, NULL, 0);
79 ASSERT(ret == 0);
80
81 return ret;
82 }
83
84 ret = handler->validate(value, value_len);
85 if (ret)
86 return ret;
David Sterba78527812018-02-27 15:48:52 +010087 ret = btrfs_setxattr(trans, inode, handler->xattr_name,
Filipe David Borba Manana63541922014-01-07 11:47:46 +000088 value, value_len, flags);
89 if (ret)
90 return ret;
91 ret = handler->apply(inode, value, value_len);
92 if (ret) {
David Sterba78527812018-02-27 15:48:52 +010093 btrfs_setxattr(trans, inode, handler->xattr_name,
Filipe David Borba Manana63541922014-01-07 11:47:46 +000094 NULL, 0, flags);
95 return ret;
96 }
97
98 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
99
100 return 0;
101}
102
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000103static int iterate_object_props(struct btrfs_root *root,
104 struct btrfs_path *path,
105 u64 objectid,
106 void (*iterator)(void *,
107 const struct prop_handler *,
108 const char *,
109 size_t),
110 void *ctx)
111{
112 int ret;
113 char *name_buf = NULL;
114 char *value_buf = NULL;
115 int name_buf_len = 0;
116 int value_buf_len = 0;
117
118 while (1) {
119 struct btrfs_key key;
120 struct btrfs_dir_item *di;
121 struct extent_buffer *leaf;
122 u32 total_len, cur, this_len;
123 int slot;
124 const struct hlist_head *handlers;
125
126 slot = path->slots[0];
127 leaf = path->nodes[0];
128
129 if (slot >= btrfs_header_nritems(leaf)) {
130 ret = btrfs_next_leaf(root, path);
131 if (ret < 0)
132 goto out;
133 else if (ret > 0)
134 break;
135 continue;
136 }
137
138 btrfs_item_key_to_cpu(leaf, &key, slot);
139 if (key.objectid != objectid)
140 break;
141 if (key.type != BTRFS_XATTR_ITEM_KEY)
142 break;
143
144 handlers = find_prop_handlers_by_hash(key.offset);
145 if (!handlers)
146 goto next_slot;
147
148 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
149 cur = 0;
150 total_len = btrfs_item_size_nr(leaf, slot);
151
152 while (cur < total_len) {
153 u32 name_len = btrfs_dir_name_len(leaf, di);
154 u32 data_len = btrfs_dir_data_len(leaf, di);
155 unsigned long name_ptr, data_ptr;
156 const struct prop_handler *handler;
157
158 this_len = sizeof(*di) + name_len + data_len;
159 name_ptr = (unsigned long)(di + 1);
160 data_ptr = name_ptr + name_len;
161
162 if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
163 memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
164 name_ptr,
165 XATTR_BTRFS_PREFIX_LEN))
166 goto next_dir_item;
167
168 if (name_len >= name_buf_len) {
169 kfree(name_buf);
170 name_buf_len = name_len + 1;
171 name_buf = kmalloc(name_buf_len, GFP_NOFS);
172 if (!name_buf) {
173 ret = -ENOMEM;
174 goto out;
175 }
176 }
177 read_extent_buffer(leaf, name_buf, name_ptr, name_len);
178 name_buf[name_len] = '\0';
179
180 handler = find_prop_handler(name_buf, handlers);
181 if (!handler)
182 goto next_dir_item;
183
184 if (data_len > value_buf_len) {
185 kfree(value_buf);
186 value_buf_len = data_len;
187 value_buf = kmalloc(data_len, GFP_NOFS);
188 if (!value_buf) {
189 ret = -ENOMEM;
190 goto out;
191 }
192 }
193 read_extent_buffer(leaf, value_buf, data_ptr, data_len);
194
195 iterator(ctx, handler, value_buf, data_len);
196next_dir_item:
197 cur += this_len;
198 di = (struct btrfs_dir_item *)((char *) di + this_len);
199 }
200
201next_slot:
202 path->slots[0]++;
203 }
204
205 ret = 0;
206out:
207 btrfs_release_path(path);
208 kfree(name_buf);
209 kfree(value_buf);
210
211 return ret;
212}
213
214static void inode_prop_iterator(void *ctx,
215 const struct prop_handler *handler,
216 const char *value,
217 size_t len)
218{
219 struct inode *inode = ctx;
220 struct btrfs_root *root = BTRFS_I(inode)->root;
221 int ret;
222
223 ret = handler->apply(inode, value, len);
224 if (unlikely(ret))
225 btrfs_warn(root->fs_info,
226 "error applying prop %s to ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200227 handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000228 root->root_key.objectid, ret);
229 else
230 set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
231}
232
233int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
234{
235 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200236 u64 ino = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000237 int ret;
238
239 ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
240
241 return ret;
242}
243
Anand Jain3dcf96c2019-03-01 12:34:48 +0800244static int prop_compression_validate(const char *value, size_t len)
245{
246 if (!value)
247 return 0;
248
249 if (!strncmp("lzo", value, 3))
250 return 0;
251 else if (!strncmp("zlib", value, 4))
252 return 0;
253 else if (!strncmp("zstd", value, 4))
254 return 0;
255
256 return -EINVAL;
257}
258
259static int prop_compression_apply(struct inode *inode, const char *value,
260 size_t len)
261{
262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
263 int type;
264
265 if (len == 0) {
266 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
267 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
268 BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
269
270 return 0;
271 }
272
273 if (!strncmp("lzo", value, 3)) {
274 type = BTRFS_COMPRESS_LZO;
275 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
276 } else if (!strncmp("zlib", value, 4)) {
277 type = BTRFS_COMPRESS_ZLIB;
278 } else if (!strncmp("zstd", value, 4)) {
279 type = BTRFS_COMPRESS_ZSTD;
280 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
281 } else {
282 return -EINVAL;
283 }
284
285 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
286 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
287 BTRFS_I(inode)->prop_compress = type;
288
289 return 0;
290}
291
292static const char *prop_compression_extract(struct inode *inode)
293{
294 switch (BTRFS_I(inode)->prop_compress) {
295 case BTRFS_COMPRESS_ZLIB:
296 case BTRFS_COMPRESS_LZO:
297 case BTRFS_COMPRESS_ZSTD:
298 return btrfs_compress_type2str(BTRFS_I(inode)->prop_compress);
299 default:
300 break;
301 }
302
303 return NULL;
304}
305
306static struct prop_handler prop_handlers[] = {
307 {
308 .xattr_name = XATTR_BTRFS_PREFIX "compression",
309 .validate = prop_compression_validate,
310 .apply = prop_compression_apply,
311 .extract = prop_compression_extract,
312 .inheritable = 1
313 },
314};
315
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000316static int inherit_props(struct btrfs_trans_handle *trans,
317 struct inode *inode,
318 struct inode *parent)
319{
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000320 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400321 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000322 int ret;
Byongho Lee619ed392015-10-08 20:49:34 +0900323 int i;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000324
325 if (!test_bit(BTRFS_INODE_HAS_PROPS,
326 &BTRFS_I(parent)->runtime_flags))
327 return 0;
328
Byongho Lee619ed392015-10-08 20:49:34 +0900329 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
330 const struct prop_handler *h = &prop_handlers[i];
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000331 const char *value;
332 u64 num_bytes;
333
334 if (!h->inheritable)
335 continue;
336
337 value = h->extract(parent);
338 if (!value)
339 continue;
340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400341 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000342 ret = btrfs_block_rsv_add(root, trans->block_rsv,
343 num_bytes, BTRFS_RESERVE_NO_FLUSH);
344 if (ret)
345 goto out;
Anand Jain7715da82019-03-01 12:34:47 +0800346 ret = btrfs_set_prop(trans, inode, h->xattr_name, value,
347 strlen(value), 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400348 btrfs_block_rsv_release(fs_info, trans->block_rsv, num_bytes);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000349 if (ret)
350 goto out;
351 }
352 ret = 0;
353out:
354 return ret;
355}
356
357int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
358 struct inode *inode,
359 struct inode *dir)
360{
361 if (!dir)
362 return 0;
363
364 return inherit_props(trans, inode, dir);
365}
366
367int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
368 struct btrfs_root *root,
369 struct btrfs_root *parent_root)
370{
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400371 struct super_block *sb = root->fs_info->sb;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000372 struct btrfs_key key;
373 struct inode *parent_inode, *child_inode;
374 int ret;
375
376 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
377 key.type = BTRFS_INODE_ITEM_KEY;
378 key.offset = 0;
379
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400380 parent_inode = btrfs_iget(sb, &key, parent_root, NULL);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000381 if (IS_ERR(parent_inode))
382 return PTR_ERR(parent_inode);
383
Jeff Mahoneybd6c57d2016-06-10 17:03:59 -0400384 child_inode = btrfs_iget(sb, &key, root, NULL);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000385 if (IS_ERR(child_inode)) {
386 iput(parent_inode);
387 return PTR_ERR(child_inode);
388 }
389
390 ret = inherit_props(trans, child_inode, parent_inode);
391 iput(child_inode);
392 iput(parent_inode);
393
394 return ret;
395}
396
Anand Jain3dcf96c2019-03-01 12:34:48 +0800397void __init btrfs_props_init(void)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000398{
Anand Jain3dcf96c2019-03-01 12:34:48 +0800399 int i;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000400
Anand Jain3dcf96c2019-03-01 12:34:48 +0800401 hash_init(prop_handlers_ht);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000402
Anand Jain3dcf96c2019-03-01 12:34:48 +0800403 for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
404 struct prop_handler *p = &prop_handlers[i];
405 u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000406
Anand Jain3dcf96c2019-03-01 12:34:48 +0800407 hash_add(prop_handlers_ht, &p->node, h);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000408 }
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000409}
410