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>
25 #include "btrfs_inode.h"
26 #include "transaction.h"
31 ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
32 void *buffer, size_t size)
34 struct btrfs_dir_item *di;
35 struct btrfs_root *root = BTRFS_I(inode)->root;
36 struct btrfs_path *path;
37 struct extent_buffer *leaf;
39 unsigned long data_ptr;
41 path = btrfs_alloc_path();
45 /* lookup the xattr by name */
46 di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
48 if (!di || IS_ERR(di)) {
53 leaf = path->nodes[0];
54 /* if size is 0, that means we want the size of the attr */
56 ret = btrfs_dir_data_len(leaf, di);
60 /* now get the data out of our dir_item */
61 if (btrfs_dir_data_len(leaf, di) > size) {
65 data_ptr = (unsigned long)((char *)(di + 1) +
66 btrfs_dir_name_len(leaf, di));
67 read_extent_buffer(leaf, buffer, data_ptr,
68 btrfs_dir_data_len(leaf, di));
69 ret = btrfs_dir_data_len(leaf, di);
72 btrfs_free_path(path);
76 int __btrfs_setxattr(struct inode *inode, const char *name,
77 const void *value, size_t size, int flags)
79 struct btrfs_dir_item *di;
80 struct btrfs_root *root = BTRFS_I(inode)->root;
81 struct btrfs_trans_handle *trans;
82 struct btrfs_path *path;
85 path = btrfs_alloc_path();
89 trans = btrfs_start_transaction(root, 1);
90 btrfs_set_trans_block_group(trans, inode);
92 /* first lets see if we already have this xattr */
93 di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
100 /* ok we already have this xattr, lets remove it */
102 /* if we want create only exit */
103 if (flags & XATTR_CREATE) {
108 ret = btrfs_delete_one_dir_name(trans, root, path, di);
111 btrfs_release_path(root, path);
113 /* if we don't have a value then we are removing the xattr */
119 btrfs_release_path(root, path);
121 if (flags & XATTR_REPLACE) {
122 /* we couldn't find the attr to replace */
128 /* ok we have to create a completely new xattr */
129 ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
130 value, size, inode->i_ino);
137 inode->i_ctime = CURRENT_TIME;
138 ret = btrfs_update_inode(trans, root, inode);
141 btrfs_end_transaction(trans, root);
142 btrfs_free_path(path);
146 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
148 struct btrfs_key key, found_key;
149 struct inode *inode = dentry->d_inode;
150 struct btrfs_root *root = BTRFS_I(inode)->root;
151 struct btrfs_path *path;
152 struct btrfs_item *item;
153 struct extent_buffer *leaf;
154 struct btrfs_dir_item *di;
155 int ret = 0, slot, advance;
156 size_t total_size = 0, size_left = size;
157 unsigned long name_ptr;
162 * ok we want all objects associated with this id.
163 * NOTE: we set key.offset = 0; because we want to start with the
164 * first xattr that we find and walk forward
166 key.objectid = inode->i_ino;
167 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
170 path = btrfs_alloc_path();
175 /* search for our xattrs */
176 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
182 leaf = path->nodes[0];
183 nritems = btrfs_header_nritems(leaf);
184 slot = path->slots[0];
186 /* this is where we start walking through the path */
187 if (advance || slot >= nritems) {
189 * if we've reached the last slot in this leaf we need
190 * to go to the next leaf and reset everything
192 if (slot >= nritems-1) {
193 ret = btrfs_next_leaf(root, path);
196 leaf = path->nodes[0];
197 nritems = btrfs_header_nritems(leaf);
198 slot = path->slots[0];
201 * just walking through the slots on this leaf
209 item = btrfs_item_nr(leaf, slot);
210 btrfs_item_key_to_cpu(leaf, &found_key, slot);
212 /* check to make sure this item is what we want */
213 if (found_key.objectid != key.objectid)
215 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
218 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
220 name_len = btrfs_dir_name_len(leaf, di);
221 total_size += name_len + 1;
223 /* we are just looking for how big our buffer needs to be */
227 if (!buffer || (name_len + 1) > size_left) {
232 name_ptr = (unsigned long)(di + 1);
233 read_extent_buffer(leaf, buffer, name_ptr, name_len);
234 buffer[name_len] = '\0';
236 size_left -= name_len + 1;
237 buffer += name_len + 1;
242 btrfs_free_path(path);
248 * List of handlers for synthetic system.* attributes. All real ondisk
249 * attributes are handled directly.
251 struct xattr_handler *btrfs_xattr_handlers[] = {
252 #ifdef CONFIG_FS_POSIX_ACL
253 &btrfs_xattr_acl_access_handler,
254 &btrfs_xattr_acl_default_handler,
260 * Check if the attribute is in a supported namespace.
262 * This applied after the check for the synthetic attributes in the system
265 static bool btrfs_is_valid_xattr(const char *name)
267 return !strncmp(name, XATTR_SECURITY_PREFIX,
268 XATTR_SECURITY_PREFIX_LEN) ||
269 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
270 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
271 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
274 ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
275 void *buffer, size_t size)
278 * If this is a request for a synthetic attribute in the system.*
279 * namespace use the generic infrastructure to resolve a handler
280 * for it via sb->s_xattr.
282 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
283 return generic_getxattr(dentry, name, buffer, size);
285 if (!btrfs_is_valid_xattr(name))
287 return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
290 int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
291 size_t size, int flags)
294 * If this is a request for a synthetic attribute in the system.*
295 * namespace use the generic infrastructure to resolve a handler
296 * for it via sb->s_xattr.
298 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
299 return generic_setxattr(dentry, name, value, size, flags);
301 if (!btrfs_is_valid_xattr(name))
305 value = ""; /* empty EA, do not remove */
306 return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
309 int btrfs_removexattr(struct dentry *dentry, const char *name)
312 * If this is a request for a synthetic attribute in the system.*
313 * namespace use the generic infrastructure to resolve a handler
314 * for it via sb->s_xattr.
316 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
317 return generic_removexattr(dentry, name);
319 if (!btrfs_is_valid_xattr(name))
321 return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);