2 * Copyright (C) 2007 Red Hat. All rights reserved.
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.
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.
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.
19 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/rwsem.h>
23 #include <linux/xattr.h>
24 #include <linux/security.h>
25 #include <linux/posix_acl_xattr.h>
27 #include "btrfs_inode.h"
28 #include "transaction.h"
33 ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
34 void *buffer, size_t size)
36 struct btrfs_dir_item *di;
37 struct btrfs_root *root = BTRFS_I(inode)->root;
38 struct btrfs_path *path;
39 struct extent_buffer *leaf;
41 unsigned long data_ptr;
43 path = btrfs_alloc_path();
47 /* lookup the xattr by name */
48 di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name,
53 } else if (IS_ERR(di)) {
58 leaf = path->nodes[0];
59 /* if size is 0, that means we want the size of the attr */
61 ret = btrfs_dir_data_len(leaf, di);
65 /* now get the data out of our dir_item */
66 if (btrfs_dir_data_len(leaf, di) > size) {
72 * The way things are packed into the leaf is like this
73 * |struct btrfs_dir_item|name|data|
74 * where name is the xattr name, so security.foo, and data is the
75 * content of the xattr. data_ptr points to the location in memory
76 * where the data starts in the in memory leaf
78 data_ptr = (unsigned long)((char *)(di + 1) +
79 btrfs_dir_name_len(leaf, di));
80 read_extent_buffer(leaf, buffer, data_ptr,
81 btrfs_dir_data_len(leaf, di));
82 ret = btrfs_dir_data_len(leaf, di);
85 btrfs_free_path(path);
89 static int do_setxattr(struct btrfs_trans_handle *trans,
90 struct inode *inode, const char *name,
91 const void *value, size_t size, int flags)
93 struct btrfs_dir_item *di;
94 struct btrfs_root *root = BTRFS_I(inode)->root;
95 struct btrfs_path *path;
96 size_t name_len = strlen(name);
99 if (name_len + size > BTRFS_MAX_XATTR_SIZE(root))
102 path = btrfs_alloc_path();
106 if (flags & XATTR_REPLACE) {
107 di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
116 ret = btrfs_delete_one_dir_name(trans, root, path, di);
119 btrfs_release_path(path);
122 * remove the attribute
127 di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode),
135 btrfs_release_path(path);
139 ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
140 name, name_len, value, size);
142 * If we're setting an xattr to a new value but the new value is say
143 * exactly BTRFS_MAX_XATTR_SIZE, we could end up with EOVERFLOW getting
144 * back from split_leaf. This is because it thinks we'll be extending
145 * the existing item size, but we're asking for enough space to add the
146 * item itself. So if we get EOVERFLOW just set ret to EEXIST and let
147 * the rest of the function figure it out.
149 if (ret == -EOVERFLOW)
152 if (ret == -EEXIST) {
153 if (flags & XATTR_CREATE)
156 * We can't use the path we already have since we won't have the
157 * proper locking for a delete, so release the path and
158 * re-lookup to delete the thing.
160 btrfs_release_path(path);
161 di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
167 /* Shouldn't happen but just in case... */
168 btrfs_release_path(path);
172 ret = btrfs_delete_one_dir_name(trans, root, path, di);
177 * We have a value to set, so go back and try to insert it now.
180 btrfs_release_path(path);
185 btrfs_free_path(path);
190 * @value: "" makes the attribute to empty, NULL removes it
192 int __btrfs_setxattr(struct btrfs_trans_handle *trans,
193 struct inode *inode, const char *name,
194 const void *value, size_t size, int flags)
196 struct btrfs_root *root = BTRFS_I(inode)->root;
200 return do_setxattr(trans, inode, name, value, size, flags);
202 trans = btrfs_start_transaction(root, 2);
204 return PTR_ERR(trans);
206 ret = do_setxattr(trans, inode, name, value, size, flags);
210 inode_inc_iversion(inode);
211 inode->i_ctime = CURRENT_TIME;
212 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
213 ret = btrfs_update_inode(trans, root, inode);
216 btrfs_end_transaction(trans, root);
220 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
222 struct btrfs_key key, found_key;
223 struct inode *inode = dentry->d_inode;
224 struct btrfs_root *root = BTRFS_I(inode)->root;
225 struct btrfs_path *path;
226 struct extent_buffer *leaf;
227 struct btrfs_dir_item *di;
229 size_t total_size = 0, size_left = size;
230 unsigned long name_ptr;
234 * ok we want all objects associated with this id.
235 * NOTE: we set key.offset = 0; because we want to start with the
236 * first xattr that we find and walk forward
238 key.objectid = btrfs_ino(inode);
239 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
242 path = btrfs_alloc_path();
247 /* search for our xattrs */
248 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
253 leaf = path->nodes[0];
254 slot = path->slots[0];
256 /* this is where we start walking through the path */
257 if (slot >= btrfs_header_nritems(leaf)) {
259 * if we've reached the last slot in this leaf we need
260 * to go to the next leaf and reset everything
262 ret = btrfs_next_leaf(root, path);
270 btrfs_item_key_to_cpu(leaf, &found_key, slot);
272 /* check to make sure this item is what we want */
273 if (found_key.objectid != key.objectid)
275 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
278 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
279 if (verify_dir_item(root, leaf, di))
282 name_len = btrfs_dir_name_len(leaf, di);
283 total_size += name_len + 1;
285 /* we are just looking for how big our buffer needs to be */
289 if (!buffer || (name_len + 1) > size_left) {
294 name_ptr = (unsigned long)(di + 1);
295 read_extent_buffer(leaf, buffer, name_ptr, name_len);
296 buffer[name_len] = '\0';
298 size_left -= name_len + 1;
299 buffer += name_len + 1;
306 btrfs_free_path(path);
312 * List of handlers for synthetic system.* attributes. All real ondisk
313 * attributes are handled directly.
315 const struct xattr_handler *btrfs_xattr_handlers[] = {
316 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
317 &posix_acl_access_xattr_handler,
318 &posix_acl_default_xattr_handler,
324 * Check if the attribute is in a supported namespace.
326 * This applied after the check for the synthetic attributes in the system
329 static bool btrfs_is_valid_xattr(const char *name)
331 return !strncmp(name, XATTR_SECURITY_PREFIX,
332 XATTR_SECURITY_PREFIX_LEN) ||
333 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
334 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
335 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
338 ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
339 void *buffer, size_t size)
342 * If this is a request for a synthetic attribute in the system.*
343 * namespace use the generic infrastructure to resolve a handler
344 * for it via sb->s_xattr.
346 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
347 return generic_getxattr(dentry, name, buffer, size);
349 if (!btrfs_is_valid_xattr(name))
351 return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
354 int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
355 size_t size, int flags)
357 struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
360 * The permission on security.* and system.* is not checked
363 if (btrfs_root_readonly(root))
367 * If this is a request for a synthetic attribute in the system.*
368 * namespace use the generic infrastructure to resolve a handler
369 * for it via sb->s_xattr.
371 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
372 return generic_setxattr(dentry, name, value, size, flags);
374 if (!btrfs_is_valid_xattr(name))
378 value = ""; /* empty EA, do not remove */
380 return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
384 int btrfs_removexattr(struct dentry *dentry, const char *name)
386 struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
389 * The permission on security.* and system.* is not checked
392 if (btrfs_root_readonly(root))
396 * If this is a request for a synthetic attribute in the system.*
397 * namespace use the generic infrastructure to resolve a handler
398 * for it via sb->s_xattr.
400 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
401 return generic_removexattr(dentry, name);
403 if (!btrfs_is_valid_xattr(name))
406 return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
410 static int btrfs_initxattrs(struct inode *inode,
411 const struct xattr *xattr_array, void *fs_info)
413 const struct xattr *xattr;
414 struct btrfs_trans_handle *trans = fs_info;
418 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
419 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
420 strlen(xattr->name) + 1, GFP_NOFS);
425 strcpy(name, XATTR_SECURITY_PREFIX);
426 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
427 err = __btrfs_setxattr(trans, inode, name,
428 xattr->value, xattr->value_len, 0);
436 int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
437 struct inode *inode, struct inode *dir,
438 const struct qstr *qstr)
440 return security_inode_init_security(inode, dir, qstr,
441 &btrfs_initxattrs, trans);