btrfs: move accessor helpers into accessors.h
[linux-block.git] / fs / btrfs / xattr.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
5103e947
JB
2/*
3 * Copyright (C) 2007 Red Hat. All rights reserved.
5103e947
JB
4 */
5
6#include <linux/init.h>
7#include <linux/fs.h>
8#include <linux/slab.h>
9#include <linux/rwsem.h>
10#include <linux/xattr.h>
0279b4cd 11#include <linux/security.h>
996a710d 12#include <linux/posix_acl_xattr.h>
ae5e165d 13#include <linux/iversion.h>
827aa18e 14#include <linux/sched/mm.h>
5103e947 15#include "ctree.h"
ec8eb376
JB
16#include "fs.h"
17#include "messages.h"
5103e947
JB
18#include "btrfs_inode.h"
19#include "transaction.h"
20#include "xattr.h"
21#include "disk-io.h"
63541927 22#include "props.h"
5f5bc6b1 23#include "locking.h"
07e81dc9 24#include "accessors.h"
33268eaf 25
bcadd705 26int btrfs_getxattr(struct inode *inode, const char *name,
95819c05 27 void *buffer, size_t size)
5103e947
JB
28{
29 struct btrfs_dir_item *di;
30 struct btrfs_root *root = BTRFS_I(inode)->root;
31 struct btrfs_path *path;
32 struct extent_buffer *leaf;
5103e947
JB
33 int ret = 0;
34 unsigned long data_ptr;
5103e947
JB
35
36 path = btrfs_alloc_path();
95819c05 37 if (!path)
5103e947 38 return -ENOMEM;
5103e947 39
5103e947 40 /* lookup the xattr by name */
f85b7379
DS
41 di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)),
42 name, strlen(name), 0);
07060404 43 if (!di) {
5103e947
JB
44 ret = -ENODATA;
45 goto out;
07060404
JB
46 } else if (IS_ERR(di)) {
47 ret = PTR_ERR(di);
48 goto out;
5103e947
JB
49 }
50
51 leaf = path->nodes[0];
52 /* if size is 0, that means we want the size of the attr */
53 if (!size) {
54 ret = btrfs_dir_data_len(leaf, di);
55 goto out;
56 }
57
58 /* now get the data out of our dir_item */
59 if (btrfs_dir_data_len(leaf, di) > size) {
60 ret = -ERANGE;
61 goto out;
62 }
07060404
JB
63
64 /*
65 * The way things are packed into the leaf is like this
66 * |struct btrfs_dir_item|name|data|
67 * where name is the xattr name, so security.foo, and data is the
68 * content of the xattr. data_ptr points to the location in memory
69 * where the data starts in the in memory leaf
70 */
5103e947
JB
71 data_ptr = (unsigned long)((char *)(di + 1) +
72 btrfs_dir_name_len(leaf, di));
73 read_extent_buffer(leaf, buffer, data_ptr,
3acd7ee8 74 btrfs_dir_data_len(leaf, di));
5103e947
JB
75 ret = btrfs_dir_data_len(leaf, di);
76
77out:
5103e947
JB
78 btrfs_free_path(path);
79 return ret;
80}
81
3e125a74
AJ
82int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
83 const char *name, const void *value, size_t size, int flags)
5103e947 84{
5f5bc6b1 85 struct btrfs_dir_item *di = NULL;
5103e947 86 struct btrfs_root *root = BTRFS_I(inode)->root;
2ff7e61e 87 struct btrfs_fs_info *fs_info = root->fs_info;
5103e947 88 struct btrfs_path *path;
f34f57a3
YZ
89 size_t name_len = strlen(name);
90 int ret = 0;
91
04e6863b
AJ
92 ASSERT(trans);
93
da17066c 94 if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
f34f57a3 95 return -ENOSPC;
5103e947
JB
96
97 path = btrfs_alloc_path();
95819c05 98 if (!path)
5103e947 99 return -ENOMEM;
5f5bc6b1
FM
100 path->skip_release_on_error = 1;
101
102 if (!value) {
f85b7379
DS
103 di = btrfs_lookup_xattr(trans, root, path,
104 btrfs_ino(BTRFS_I(inode)), name, name_len, -1);
5f5bc6b1
FM
105 if (!di && (flags & XATTR_REPLACE))
106 ret = -ENODATA;
5cdf83ed
FM
107 else if (IS_ERR(di))
108 ret = PTR_ERR(di);
5f5bc6b1
FM
109 else if (di)
110 ret = btrfs_delete_one_dir_name(trans, root, path, di);
111 goto out;
112 }
5103e947 113
5f5bc6b1
FM
114 /*
115 * For a replace we can't just do the insert blindly.
116 * Do a lookup first (read-only btrfs_search_slot), and return if xattr
117 * doesn't exist. If it exists, fall down below to the insert/replace
118 * path - we can't race with a concurrent xattr delete, because the VFS
119 * locks the inode's i_mutex before calling setxattr or removexattr.
120 */
fa09200b 121 if (flags & XATTR_REPLACE) {
5955102c 122 ASSERT(inode_is_locked(inode));
f85b7379
DS
123 di = btrfs_lookup_xattr(NULL, root, path,
124 btrfs_ino(BTRFS_I(inode)), name, name_len, 0);
5cdf83ed 125 if (!di)
fa09200b 126 ret = -ENODATA;
5cdf83ed
FM
127 else if (IS_ERR(di))
128 ret = PTR_ERR(di);
129 if (ret)
5103e947 130 goto out;
b3b4aa74 131 btrfs_release_path(path);
5f5bc6b1
FM
132 di = NULL;
133 }
4815053a 134
4a0cc7ca 135 ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)),
5f5bc6b1
FM
136 name, name_len, value, size);
137 if (ret == -EOVERFLOW) {
4815053a 138 /*
5f5bc6b1
FM
139 * We have an existing item in a leaf, split_leaf couldn't
140 * expand it. That item might have or not a dir_item that
141 * matches our target xattr, so lets check.
4815053a 142 */
5f5bc6b1 143 ret = 0;
49d0c642 144 btrfs_assert_tree_write_locked(path->nodes[0]);
2ff7e61e 145 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
5f5bc6b1
FM
146 if (!di && !(flags & XATTR_REPLACE)) {
147 ret = -ENOSPC;
01e6deb2
LB
148 goto out;
149 }
5f5bc6b1
FM
150 } else if (ret == -EEXIST) {
151 ret = 0;
2ff7e61e 152 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
5f5bc6b1
FM
153 ASSERT(di); /* logic error */
154 } else if (ret) {
155 goto out;
fa09200b 156 }
5103e947 157
5f5bc6b1 158 if (di && (flags & XATTR_CREATE)) {
ed3ee9f4 159 ret = -EEXIST;
5f5bc6b1
FM
160 goto out;
161 }
ed3ee9f4 162
5f5bc6b1 163 if (di) {
fa09200b 164 /*
5f5bc6b1
FM
165 * We're doing a replace, and it must be atomic, that is, at
166 * any point in time we have either the old or the new xattr
167 * value in the tree. We don't want readers (getxattr and
168 * listxattrs) to miss a value, this is specially important
169 * for ACLs.
fa09200b 170 */
5f5bc6b1
FM
171 const int slot = path->slots[0];
172 struct extent_buffer *leaf = path->nodes[0];
173 const u16 old_data_len = btrfs_dir_data_len(leaf, di);
3212fa14 174 const u32 item_size = btrfs_item_size(leaf, slot);
5f5bc6b1 175 const u32 data_size = sizeof(*di) + name_len + size;
5f5bc6b1
FM
176 unsigned long data_ptr;
177 char *ptr;
178
179 if (size > old_data_len) {
e902baac 180 if (btrfs_leaf_free_space(leaf) <
5f5bc6b1
FM
181 (size - old_data_len)) {
182 ret = -ENOSPC;
183 goto out;
184 }
fa09200b 185 }
33268eaf 186
5f5bc6b1
FM
187 if (old_data_len + name_len + sizeof(*di) == item_size) {
188 /* No other xattrs packed in the same leaf item. */
189 if (size > old_data_len)
c71dd880 190 btrfs_extend_item(path, size - old_data_len);
5f5bc6b1 191 else if (size < old_data_len)
78ac4f9e 192 btrfs_truncate_item(path, data_size, 1);
5f5bc6b1
FM
193 } else {
194 /* There are other xattrs packed in the same item. */
195 ret = btrfs_delete_one_dir_name(trans, root, path, di);
196 if (ret)
197 goto out;
c71dd880 198 btrfs_extend_item(path, data_size);
5f5bc6b1 199 }
fa09200b 200
5f5bc6b1 201 ptr = btrfs_item_ptr(leaf, slot, char);
3212fa14 202 ptr += btrfs_item_size(leaf, slot) - data_size;
5f5bc6b1
FM
203 di = (struct btrfs_dir_item *)ptr;
204 btrfs_set_dir_data_len(leaf, di, size);
205 data_ptr = ((unsigned long)(di + 1)) + name_len;
206 write_extent_buffer(leaf, value, data_ptr, size);
207 btrfs_mark_buffer_dirty(leaf);
208 } else {
fa09200b 209 /*
5f5bc6b1
FM
210 * Insert, and we had space for the xattr, so path->slots[0] is
211 * where our xattr dir_item is and btrfs_insert_xattr_item()
212 * filled it.
fa09200b 213 */
5103e947 214 }
f34f57a3
YZ
215out:
216 btrfs_free_path(path);
f2f121ab 217 if (!ret) {
3763771c
FM
218 set_bit(BTRFS_INODE_COPY_EVERYTHING,
219 &BTRFS_I(inode)->runtime_flags);
f2f121ab
FM
220 clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags);
221 }
f34f57a3
YZ
222 return ret;
223}
224
4815053a
DS
225/*
226 * @value: "" makes the attribute to empty, NULL removes it
227 */
e3de9b15 228int btrfs_setxattr_trans(struct inode *inode, const char *name,
cac237ae 229 const void *value, size_t size, int flags)
f34f57a3
YZ
230{
231 struct btrfs_root *root = BTRFS_I(inode)->root;
e3de9b15 232 struct btrfs_trans_handle *trans;
fd57a98d 233 const bool start_trans = (current->journal_info == NULL);
f34f57a3
YZ
234 int ret;
235
fd57a98d
FM
236 if (start_trans) {
237 /*
238 * 1 unit for inserting/updating/deleting the xattr
239 * 1 unit for the inode item update
240 */
241 trans = btrfs_start_transaction(root, 2);
242 if (IS_ERR(trans))
243 return PTR_ERR(trans);
244 } else {
245 /*
246 * This can happen when smack is enabled and a directory is being
247 * created. It happens through d_instantiate_new(), which calls
248 * smack_d_instantiate(), which in turn calls __vfs_setxattr() to
249 * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the
250 * inode. We have already reserved space for the xattr and inode
251 * update at btrfs_mkdir(), so just use the transaction handle.
252 * We don't join or start a transaction, as that will reset the
253 * block_rsv of the handle and trigger a warning for the start
254 * case.
255 */
256 ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
257 XATTR_SECURITY_PREFIX_LEN) == 0);
258 trans = current->journal_info;
259 }
5103e947 260
2d74fa3e 261 ret = btrfs_setxattr(trans, inode, name, value, size, flags);
f34f57a3
YZ
262 if (ret)
263 goto out;
264
0c4d2d95 265 inode_inc_iversion(inode);
c2050a45 266 inode->i_ctime = current_time(inode);
9a56fcd1 267 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
193b4e83
FM
268 if (ret)
269 btrfs_abort_transaction(trans, ret);
f34f57a3 270out:
fd57a98d
FM
271 if (start_trans)
272 btrfs_end_transaction(trans);
5103e947
JB
273 return ret;
274}
275
276ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
277{
184b3d19 278 struct btrfs_key found_key;
daac7ba6 279 struct btrfs_key key;
2b0143b5 280 struct inode *inode = d_inode(dentry);
5103e947
JB
281 struct btrfs_root *root = BTRFS_I(inode)->root;
282 struct btrfs_path *path;
184b3d19 283 int iter_ret = 0;
daac7ba6 284 int ret = 0;
eaa47d86 285 size_t total_size = 0, size_left = size;
5103e947
JB
286
287 /*
288 * ok we want all objects associated with this id.
289 * NOTE: we set key.offset = 0; because we want to start with the
290 * first xattr that we find and walk forward
291 */
4a0cc7ca 292 key.objectid = btrfs_ino(BTRFS_I(inode));
962a298f 293 key.type = BTRFS_XATTR_ITEM_KEY;
5103e947
JB
294 key.offset = 0;
295
296 path = btrfs_alloc_path();
5103e947
JB
297 if (!path)
298 return -ENOMEM;
e4058b54 299 path->reada = READA_FORWARD;
5103e947 300
5103e947 301 /* search for our xattrs */
184b3d19 302 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
daac7ba6
FM
303 struct extent_buffer *leaf;
304 int slot;
305 struct btrfs_dir_item *di;
daac7ba6
FM
306 u32 item_size;
307 u32 cur;
308
5103e947 309 leaf = path->nodes[0];
5103e947
JB
310 slot = path->slots[0];
311
5103e947
JB
312 /* check to make sure this item is what we want */
313 if (found_key.objectid != key.objectid)
314 break;
f1cd1f0b 315 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
5103e947 316 break;
f1cd1f0b 317 if (found_key.type < BTRFS_XATTR_ITEM_KEY)
184b3d19 318 continue;
5103e947
JB
319
320 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3212fa14 321 item_size = btrfs_item_size(leaf, slot);
daac7ba6
FM
322 cur = 0;
323 while (cur < item_size) {
324 u16 name_len = btrfs_dir_name_len(leaf, di);
325 u16 data_len = btrfs_dir_data_len(leaf, di);
326 u32 this_len = sizeof(*di) + name_len + data_len;
327 unsigned long name_ptr = (unsigned long)(di + 1);
328
daac7ba6
FM
329 total_size += name_len + 1;
330 /*
331 * We are just looking for how big our buffer needs to
332 * be.
333 */
334 if (!size)
335 goto next;
5103e947 336
daac7ba6 337 if (!buffer || (name_len + 1) > size_left) {
184b3d19
GN
338 iter_ret = -ERANGE;
339 break;
daac7ba6 340 }
5103e947 341
daac7ba6
FM
342 read_extent_buffer(leaf, buffer, name_ptr, name_len);
343 buffer[name_len] = '\0';
eaa47d86 344
daac7ba6
FM
345 size_left -= name_len + 1;
346 buffer += name_len + 1;
2e6a0035 347next:
daac7ba6
FM
348 cur += this_len;
349 di = (struct btrfs_dir_item *)((char *)di + this_len);
350 }
5103e947 351 }
5103e947 352
184b3d19
GN
353 if (iter_ret < 0)
354 ret = iter_ret;
355 else
356 ret = total_size;
357
5103e947
JB
358 btrfs_free_path(path);
359
360 return ret;
361}
362
9172abbc 363static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
b296821a
AV
364 struct dentry *unused, struct inode *inode,
365 const char *name, void *buffer, size_t size)
95819c05 366{
9172abbc 367 name = xattr_full_name(handler, name);
7852781d 368 return btrfs_getxattr(inode, name, buffer, size);
69a32ac5
CM
369}
370
9172abbc 371static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
e65ce2a5 372 struct user_namespace *mnt_userns,
59301226
AV
373 struct dentry *unused, struct inode *inode,
374 const char *name, const void *buffer,
375 size_t size, int flags)
95819c05 376{
b5111127
GR
377 if (btrfs_root_readonly(BTRFS_I(inode)->root))
378 return -EROFS;
379
9172abbc 380 name = xattr_full_name(handler, name);
e3de9b15 381 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
9172abbc 382}
5103e947 383
9172abbc 384static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
e65ce2a5 385 struct user_namespace *mnt_userns,
59301226 386 struct dentry *unused, struct inode *inode,
9172abbc
AG
387 const char *name, const void *value,
388 size_t size, int flags)
389{
f22125e5 390 int ret;
b3f6a4be
AJ
391 struct btrfs_trans_handle *trans;
392 struct btrfs_root *root = BTRFS_I(inode)->root;
f22125e5 393
9172abbc 394 name = xattr_full_name(handler, name);
0e852ab8 395 ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size);
f22125e5
AJ
396 if (ret)
397 return ret;
b3f6a4be 398
4b73c55f
FM
399 if (btrfs_ignore_prop(BTRFS_I(inode), name))
400 return 0;
401
b3f6a4be
AJ
402 trans = btrfs_start_transaction(root, 2);
403 if (IS_ERR(trans))
404 return PTR_ERR(trans);
405
406 ret = btrfs_set_prop(trans, inode, name, value, size, flags);
407 if (!ret) {
408 inode_inc_iversion(inode);
409 inode->i_ctime = current_time(inode);
9a56fcd1 410 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
193b4e83
FM
411 if (ret)
412 btrfs_abort_transaction(trans, ret);
b3f6a4be
AJ
413 }
414
415 btrfs_end_transaction(trans);
416
417 return ret;
95819c05 418}
5103e947 419
9172abbc
AG
420static const struct xattr_handler btrfs_security_xattr_handler = {
421 .prefix = XATTR_SECURITY_PREFIX,
422 .get = btrfs_xattr_handler_get,
423 .set = btrfs_xattr_handler_set,
424};
425
426static const struct xattr_handler btrfs_trusted_xattr_handler = {
427 .prefix = XATTR_TRUSTED_PREFIX,
428 .get = btrfs_xattr_handler_get,
429 .set = btrfs_xattr_handler_set,
430};
431
432static const struct xattr_handler btrfs_user_xattr_handler = {
433 .prefix = XATTR_USER_PREFIX,
434 .get = btrfs_xattr_handler_get,
435 .set = btrfs_xattr_handler_set,
436};
437
438static const struct xattr_handler btrfs_btrfs_xattr_handler = {
439 .prefix = XATTR_BTRFS_PREFIX,
440 .get = btrfs_xattr_handler_get,
441 .set = btrfs_xattr_handler_set_prop,
442};
443
444const struct xattr_handler *btrfs_xattr_handlers[] = {
445 &btrfs_security_xattr_handler,
446#ifdef CONFIG_BTRFS_FS_POSIX_ACL
447 &posix_acl_access_xattr_handler,
448 &posix_acl_default_xattr_handler,
449#endif
450 &btrfs_trusted_xattr_handler,
451 &btrfs_user_xattr_handler,
452 &btrfs_btrfs_xattr_handler,
453 NULL,
454};
455
48a3b636 456static int btrfs_initxattrs(struct inode *inode,
419a6f30 457 const struct xattr *xattr_array, void *fs_private)
0279b4cd 458{
419a6f30 459 struct btrfs_trans_handle *trans = fs_private;
9d8f13ba 460 const struct xattr *xattr;
827aa18e 461 unsigned int nofs_flag;
0279b4cd 462 char *name;
9d8f13ba 463 int err = 0;
0279b4cd 464
827aa18e
FM
465 /*
466 * We're holding a transaction handle, so use a NOFS memory allocation
467 * context to avoid deadlock if reclaim happens.
468 */
469 nofs_flag = memalloc_nofs_save();
9d8f13ba
MZ
470 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
471 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
39a27ec1 472 strlen(xattr->name) + 1, GFP_KERNEL);
9d8f13ba
MZ
473 if (!name) {
474 err = -ENOMEM;
475 break;
476 }
0279b4cd 477 strcpy(name, XATTR_SECURITY_PREFIX);
9d8f13ba 478 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
04e6863b
AJ
479 err = btrfs_setxattr(trans, inode, name, xattr->value,
480 xattr->value_len, 0);
0279b4cd 481 kfree(name);
9d8f13ba
MZ
482 if (err < 0)
483 break;
0279b4cd 484 }
827aa18e 485 memalloc_nofs_restore(nofs_flag);
0279b4cd
JO
486 return err;
487}
9d8f13ba
MZ
488
489int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
490 struct inode *inode, struct inode *dir,
491 const struct qstr *qstr)
492{
493 return security_inode_init_security(inode, dir, qstr,
494 &btrfs_initxattrs, trans);
495}