blob: de7d072c78efaaf7f357dc57266cf840bd5c68c2 [file] [log] [blame]
Josef Bacik5103e942007-11-16 11:45:54 -05001/*
2 * Copyright (C) 2007 Red Hat. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/init.h>
20#include <linux/fs.h>
21#include <linux/slab.h>
22#include <linux/rwsem.h>
23#include <linux/xattr.h>
Jim Owens0279b4c2009-02-04 09:29:13 -050024#include <linux/security.h>
Christoph Hellwig996a7102013-12-20 05:16:43 -080025#include <linux/posix_acl_xattr.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050026#include <linux/iversion.h>
Josef Bacik5103e942007-11-16 11:45:54 -050027#include "ctree.h"
28#include "btrfs_inode.h"
29#include "transaction.h"
30#include "xattr.h"
31#include "disk-io.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000032#include "props.h"
Filipe Manana5f5bc6b2014-11-09 08:38:39 +000033#include "locking.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040034
Josef Bacik33268ea2008-07-24 12:16:36 -040035
Christoph Hellwig95819c02008-08-28 06:21:17 -040036ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
37 void *buffer, size_t size)
Josef Bacik5103e942007-11-16 11:45:54 -050038{
39 struct btrfs_dir_item *di;
40 struct btrfs_root *root = BTRFS_I(inode)->root;
41 struct btrfs_path *path;
42 struct extent_buffer *leaf;
Josef Bacik5103e942007-11-16 11:45:54 -050043 int ret = 0;
44 unsigned long data_ptr;
Josef Bacik5103e942007-11-16 11:45:54 -050045
46 path = btrfs_alloc_path();
Christoph Hellwig95819c02008-08-28 06:21:17 -040047 if (!path)
Josef Bacik5103e942007-11-16 11:45:54 -050048 return -ENOMEM;
Josef Bacik5103e942007-11-16 11:45:54 -050049
Josef Bacik5103e942007-11-16 11:45:54 -050050 /* lookup the xattr by name */
David Sterbaf85b7372017-01-20 14:54:07 +010051 di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)),
52 name, strlen(name), 0);
Josef Bacik07060402009-01-21 10:49:16 -050053 if (!di) {
Josef Bacik5103e942007-11-16 11:45:54 -050054 ret = -ENODATA;
55 goto out;
Josef Bacik07060402009-01-21 10:49:16 -050056 } else if (IS_ERR(di)) {
57 ret = PTR_ERR(di);
58 goto out;
Josef Bacik5103e942007-11-16 11:45:54 -050059 }
60
61 leaf = path->nodes[0];
62 /* if size is 0, that means we want the size of the attr */
63 if (!size) {
64 ret = btrfs_dir_data_len(leaf, di);
65 goto out;
66 }
67
68 /* now get the data out of our dir_item */
69 if (btrfs_dir_data_len(leaf, di) > size) {
70 ret = -ERANGE;
71 goto out;
72 }
Josef Bacik07060402009-01-21 10:49:16 -050073
74 /*
75 * The way things are packed into the leaf is like this
76 * |struct btrfs_dir_item|name|data|
77 * where name is the xattr name, so security.foo, and data is the
78 * content of the xattr. data_ptr points to the location in memory
79 * where the data starts in the in memory leaf
80 */
Josef Bacik5103e942007-11-16 11:45:54 -050081 data_ptr = (unsigned long)((char *)(di + 1) +
82 btrfs_dir_name_len(leaf, di));
83 read_extent_buffer(leaf, buffer, data_ptr,
Josef Bacik3acd7ee2007-11-19 10:18:19 -050084 btrfs_dir_data_len(leaf, di));
Josef Bacik5103e942007-11-16 11:45:54 -050085 ret = btrfs_dir_data_len(leaf, di);
86
87out:
Josef Bacik5103e942007-11-16 11:45:54 -050088 btrfs_free_path(path);
89 return ret;
90}
91
Yan, Zhengf34f57a2009-11-12 09:35:27 +000092static int do_setxattr(struct btrfs_trans_handle *trans,
93 struct inode *inode, const char *name,
94 const void *value, size_t size, int flags)
Josef Bacik5103e942007-11-16 11:45:54 -050095{
Filipe Manana5f5bc6b2014-11-09 08:38:39 +000096 struct btrfs_dir_item *di = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -050097 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040098 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik5103e942007-11-16 11:45:54 -050099 struct btrfs_path *path;
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000100 size_t name_len = strlen(name);
101 int ret = 0;
102
Jeff Mahoneyda170662016-06-15 09:22:56 -0400103 if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000104 return -ENOSPC;
Josef Bacik5103e942007-11-16 11:45:54 -0500105
106 path = btrfs_alloc_path();
Christoph Hellwig95819c02008-08-28 06:21:17 -0400107 if (!path)
Josef Bacik5103e942007-11-16 11:45:54 -0500108 return -ENOMEM;
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000109 path->skip_release_on_error = 1;
Josef Bacik5103e942007-11-16 11:45:54 -0500110
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000111 if (!value) {
David Sterbaf85b7372017-01-20 14:54:07 +0100112 di = btrfs_lookup_xattr(trans, root, path,
113 btrfs_ino(BTRFS_I(inode)), name, name_len, -1);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000114 if (!di && (flags & XATTR_REPLACE))
115 ret = -ENODATA;
Filipe Manana5cdf83e2015-02-23 19:50:49 +0000116 else if (IS_ERR(di))
117 ret = PTR_ERR(di);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000118 else if (di)
119 ret = btrfs_delete_one_dir_name(trans, root, path, di);
120 goto out;
121 }
122
123 /*
124 * For a replace we can't just do the insert blindly.
125 * Do a lookup first (read-only btrfs_search_slot), and return if xattr
126 * doesn't exist. If it exists, fall down below to the insert/replace
127 * path - we can't race with a concurrent xattr delete, because the VFS
128 * locks the inode's i_mutex before calling setxattr or removexattr.
129 */
Josef Bacikfa092002011-05-27 12:06:11 -0400130 if (flags & XATTR_REPLACE) {
Al Viro59551022016-01-22 15:40:57 -0500131 ASSERT(inode_is_locked(inode));
David Sterbaf85b7372017-01-20 14:54:07 +0100132 di = btrfs_lookup_xattr(NULL, root, path,
133 btrfs_ino(BTRFS_I(inode)), name, name_len, 0);
Filipe Manana5cdf83e2015-02-23 19:50:49 +0000134 if (!di)
Josef Bacik33268ea2008-07-24 12:16:36 -0400135 ret = -ENODATA;
Filipe Manana5cdf83e2015-02-23 19:50:49 +0000136 else if (IS_ERR(di))
137 ret = PTR_ERR(di);
138 if (ret)
Josef Bacik33268ea2008-07-24 12:16:36 -0400139 goto out;
Josef Bacikfa092002011-05-27 12:06:11 -0400140 btrfs_release_path(path);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000141 di = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500142 }
143
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200144 ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)),
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000145 name, name_len, value, size);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000146 if (ret == -EOVERFLOW) {
147 /*
148 * We have an existing item in a leaf, split_leaf couldn't
149 * expand it. That item might have or not a dir_item that
150 * matches our target xattr, so lets check.
151 */
152 ret = 0;
153 btrfs_assert_tree_locked(path->nodes[0]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400154 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000155 if (!di && !(flags & XATTR_REPLACE)) {
156 ret = -ENOSPC;
157 goto out;
158 }
159 } else if (ret == -EEXIST) {
160 ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400161 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000162 ASSERT(di); /* logic error */
163 } else if (ret) {
164 goto out;
165 }
166
167 if (di && (flags & XATTR_CREATE)) {
Josef Baciked3ee9f2011-10-13 13:09:22 -0400168 ret = -EEXIST;
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000169 goto out;
170 }
Josef Baciked3ee9f2011-10-13 13:09:22 -0400171
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000172 if (di) {
Josef Bacikfa092002011-05-27 12:06:11 -0400173 /*
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000174 * We're doing a replace, and it must be atomic, that is, at
175 * any point in time we have either the old or the new xattr
176 * value in the tree. We don't want readers (getxattr and
177 * listxattrs) to miss a value, this is specially important
178 * for ACLs.
Josef Bacikfa092002011-05-27 12:06:11 -0400179 */
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000180 const int slot = path->slots[0];
181 struct extent_buffer *leaf = path->nodes[0];
182 const u16 old_data_len = btrfs_dir_data_len(leaf, di);
183 const u32 item_size = btrfs_item_size_nr(leaf, slot);
184 const u32 data_size = sizeof(*di) + name_len + size;
185 struct btrfs_item *item;
186 unsigned long data_ptr;
187 char *ptr;
188
189 if (size > old_data_len) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400190 if (btrfs_leaf_free_space(fs_info, leaf) <
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000191 (size - old_data_len)) {
192 ret = -ENOSPC;
193 goto out;
194 }
Josef Bacikfa092002011-05-27 12:06:11 -0400195 }
196
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000197 if (old_data_len + name_len + sizeof(*di) == item_size) {
198 /* No other xattrs packed in the same leaf item. */
199 if (size > old_data_len)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400200 btrfs_extend_item(fs_info, path,
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000201 size - old_data_len);
202 else if (size < old_data_len)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400203 btrfs_truncate_item(fs_info, path,
204 data_size, 1);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000205 } else {
206 /* There are other xattrs packed in the same item. */
207 ret = btrfs_delete_one_dir_name(trans, root, path, di);
208 if (ret)
209 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400210 btrfs_extend_item(fs_info, path, data_size);
Josef Bacikfa092002011-05-27 12:06:11 -0400211 }
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000212
213 item = btrfs_item_nr(slot);
214 ptr = btrfs_item_ptr(leaf, slot, char);
215 ptr += btrfs_item_size(leaf, item) - data_size;
216 di = (struct btrfs_dir_item *)ptr;
217 btrfs_set_dir_data_len(leaf, di, size);
218 data_ptr = ((unsigned long)(di + 1)) + name_len;
219 write_extent_buffer(leaf, value, data_ptr, size);
220 btrfs_mark_buffer_dirty(leaf);
221 } else {
222 /*
223 * Insert, and we had space for the xattr, so path->slots[0] is
224 * where our xattr dir_item is and btrfs_insert_xattr_item()
225 * filled it.
226 */
Josef Bacikfa092002011-05-27 12:06:11 -0400227 }
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000228out:
229 btrfs_free_path(path);
230 return ret;
231}
232
David Sterba48150532011-09-11 10:52:25 -0400233/*
234 * @value: "" makes the attribute to empty, NULL removes it
235 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000236int __btrfs_setxattr(struct btrfs_trans_handle *trans,
237 struct inode *inode, const char *name,
238 const void *value, size_t size, int flags)
239{
240 struct btrfs_root *root = BTRFS_I(inode)->root;
241 int ret;
242
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +0200243 if (btrfs_root_readonly(root))
244 return -EROFS;
245
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000246 if (trans)
247 return do_setxattr(trans, inode, name, value, size, flags);
248
Yan, Zhenga22285a2010-05-16 10:48:46 -0400249 trans = btrfs_start_transaction(root, 2);
250 if (IS_ERR(trans))
251 return PTR_ERR(trans);
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000252
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000253 ret = do_setxattr(trans, inode, name, value, size, flags);
Josef Bacik5103e942007-11-16 11:45:54 -0500254 if (ret)
255 goto out;
Josef Bacik5103e942007-11-16 11:45:54 -0500256
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400257 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700258 inode->i_ctime = current_time(inode);
Josef Bacike9976152012-10-11 15:53:56 -0400259 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000260 ret = btrfs_update_inode(trans, root, inode);
261 BUG_ON(ret);
Josef Bacik5103e942007-11-16 11:45:54 -0500262out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400263 btrfs_end_transaction(trans);
Josef Bacik5103e942007-11-16 11:45:54 -0500264 return ret;
265}
266
267ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
268{
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000269 struct btrfs_key key;
David Howells2b0143b2015-03-17 22:25:59 +0000270 struct inode *inode = d_inode(dentry);
Josef Bacik5103e942007-11-16 11:45:54 -0500271 struct btrfs_root *root = BTRFS_I(inode)->root;
272 struct btrfs_path *path;
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000273 int ret = 0;
Christoph Hellwigeaa47d82008-08-28 06:21:16 -0400274 size_t total_size = 0, size_left = size;
Josef Bacik5103e942007-11-16 11:45:54 -0500275
276 /*
277 * ok we want all objects associated with this id.
278 * NOTE: we set key.offset = 0; because we want to start with the
279 * first xattr that we find and walk forward
280 */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200281 key.objectid = btrfs_ino(BTRFS_I(inode));
David Sterba962a2982014-06-04 18:41:45 +0200282 key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacik5103e942007-11-16 11:45:54 -0500283 key.offset = 0;
284
285 path = btrfs_alloc_path();
Josef Bacik5103e942007-11-16 11:45:54 -0500286 if (!path)
287 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +0100288 path->reada = READA_FORWARD;
Josef Bacik5103e942007-11-16 11:45:54 -0500289
Josef Bacik5103e942007-11-16 11:45:54 -0500290 /* search for our xattrs */
291 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
292 if (ret < 0)
293 goto err;
Li Zefan2e6a0032011-03-17 15:17:59 +0800294
Josef Bacik5103e942007-11-16 11:45:54 -0500295 while (1) {
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000296 struct extent_buffer *leaf;
297 int slot;
298 struct btrfs_dir_item *di;
299 struct btrfs_key found_key;
300 u32 item_size;
301 u32 cur;
302
Josef Bacik5103e942007-11-16 11:45:54 -0500303 leaf = path->nodes[0];
Josef Bacik5103e942007-11-16 11:45:54 -0500304 slot = path->slots[0];
305
306 /* this is where we start walking through the path */
Li Zefan2e6a0032011-03-17 15:17:59 +0800307 if (slot >= btrfs_header_nritems(leaf)) {
Josef Bacik5103e942007-11-16 11:45:54 -0500308 /*
309 * if we've reached the last slot in this leaf we need
310 * to go to the next leaf and reset everything
311 */
Li Zefan2e6a0032011-03-17 15:17:59 +0800312 ret = btrfs_next_leaf(root, path);
313 if (ret < 0)
314 goto err;
315 else if (ret > 0)
316 break;
317 continue;
Josef Bacik5103e942007-11-16 11:45:54 -0500318 }
Josef Bacik5103e942007-11-16 11:45:54 -0500319
Josef Bacik5103e942007-11-16 11:45:54 -0500320 btrfs_item_key_to_cpu(leaf, &found_key, slot);
321
322 /* check to make sure this item is what we want */
323 if (found_key.objectid != key.objectid)
324 break;
Filipe Mananaf1cd1f02015-11-09 18:06:38 +0000325 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
Josef Bacik5103e942007-11-16 11:45:54 -0500326 break;
Filipe Mananaf1cd1f02015-11-09 18:06:38 +0000327 if (found_key.type < BTRFS_XATTR_ITEM_KEY)
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000328 goto next_item;
Josef Bacik5103e942007-11-16 11:45:54 -0500329
330 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000331 item_size = btrfs_item_size_nr(leaf, slot);
332 cur = 0;
333 while (cur < item_size) {
334 u16 name_len = btrfs_dir_name_len(leaf, di);
335 u16 data_len = btrfs_dir_data_len(leaf, di);
336 u32 this_len = sizeof(*di) + name_len + data_len;
337 unsigned long name_ptr = (unsigned long)(di + 1);
Josef Bacik5103e942007-11-16 11:45:54 -0500338
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000339 total_size += name_len + 1;
340 /*
341 * We are just looking for how big our buffer needs to
342 * be.
343 */
344 if (!size)
345 goto next;
Josef Bacik5103e942007-11-16 11:45:54 -0500346
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000347 if (!buffer || (name_len + 1) > size_left) {
348 ret = -ERANGE;
349 goto err;
350 }
Josef Bacik5103e942007-11-16 11:45:54 -0500351
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000352 read_extent_buffer(leaf, buffer, name_ptr, name_len);
353 buffer[name_len] = '\0';
Christoph Hellwigeaa47d82008-08-28 06:21:16 -0400354
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000355 size_left -= name_len + 1;
356 buffer += name_len + 1;
Li Zefan2e6a0032011-03-17 15:17:59 +0800357next:
Filipe Mananadaac7ba2016-02-21 15:03:02 +0000358 cur += this_len;
359 di = (struct btrfs_dir_item *)((char *)di + this_len);
360 }
361next_item:
Li Zefan2e6a0032011-03-17 15:17:59 +0800362 path->slots[0]++;
Josef Bacik5103e942007-11-16 11:45:54 -0500363 }
364 ret = total_size;
365
366err:
Josef Bacik5103e942007-11-16 11:45:54 -0500367 btrfs_free_path(path);
368
369 return ret;
370}
371
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100372static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -0400373 struct dentry *unused, struct inode *inode,
374 const char *name, void *buffer, size_t size)
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100375{
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100376 name = xattr_full_name(handler, name);
377 return __btrfs_getxattr(inode, name, buffer, size);
378}
379
380static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -0400381 struct dentry *unused, struct inode *inode,
382 const char *name, const void *buffer,
383 size_t size, int flags)
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100384{
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100385 name = xattr_full_name(handler, name);
386 return __btrfs_setxattr(NULL, inode, name, buffer, size, flags);
387}
388
389static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -0400390 struct dentry *unused, struct inode *inode,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100391 const char *name, const void *value,
392 size_t size, int flags)
393{
394 name = xattr_full_name(handler, name);
Al Viro59301222016-05-27 10:19:30 -0400395 return btrfs_set_prop(inode, name, value, size, flags);
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100396}
397
398static const struct xattr_handler btrfs_security_xattr_handler = {
399 .prefix = XATTR_SECURITY_PREFIX,
400 .get = btrfs_xattr_handler_get,
401 .set = btrfs_xattr_handler_set,
402};
403
404static const struct xattr_handler btrfs_trusted_xattr_handler = {
405 .prefix = XATTR_TRUSTED_PREFIX,
406 .get = btrfs_xattr_handler_get,
407 .set = btrfs_xattr_handler_set,
408};
409
410static const struct xattr_handler btrfs_user_xattr_handler = {
411 .prefix = XATTR_USER_PREFIX,
412 .get = btrfs_xattr_handler_get,
413 .set = btrfs_xattr_handler_set,
414};
415
416static const struct xattr_handler btrfs_btrfs_xattr_handler = {
417 .prefix = XATTR_BTRFS_PREFIX,
418 .get = btrfs_xattr_handler_get,
419 .set = btrfs_xattr_handler_set_prop,
420};
421
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700422const struct xattr_handler *btrfs_xattr_handlers[] = {
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100423 &btrfs_security_xattr_handler,
Chris Mason0eda2942009-10-13 13:50:18 -0400424#ifdef CONFIG_BTRFS_FS_POSIX_ACL
Christoph Hellwig996a7102013-12-20 05:16:43 -0800425 &posix_acl_access_xattr_handler,
426 &posix_acl_default_xattr_handler,
Christoph Hellwig95819c02008-08-28 06:21:17 -0400427#endif
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +0100428 &btrfs_trusted_xattr_handler,
429 &btrfs_user_xattr_handler,
430 &btrfs_btrfs_xattr_handler,
Christoph Hellwig95819c02008-08-28 06:21:17 -0400431 NULL,
432};
433
Eric Sandeen48a3b632013-04-25 20:41:01 +0000434static int btrfs_initxattrs(struct inode *inode,
435 const struct xattr *xattr_array, void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400436{
437 const struct xattr *xattr;
438 struct btrfs_trans_handle *trans = fs_info;
439 char *name;
440 int err = 0;
441
442 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
443 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
David Sterba39a27ec2015-12-03 12:49:48 +0100444 strlen(xattr->name) + 1, GFP_KERNEL);
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400445 if (!name) {
446 err = -ENOMEM;
447 break;
448 }
449 strcpy(name, XATTR_SECURITY_PREFIX);
450 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
451 err = __btrfs_setxattr(trans, inode, name,
452 xattr->value, xattr->value_len, 0);
453 kfree(name);
454 if (err < 0)
455 break;
456 }
457 return err;
458}
459
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000460int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500461 struct inode *inode, struct inode *dir,
462 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500463{
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400464 return security_inode_init_security(inode, dir, qstr,
465 &btrfs_initxattrs, trans);
Jim Owens0279b4c2009-02-04 09:29:13 -0500466}