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"
30 static struct xattr_handler *btrfs_xattr_handler_map[] = {
31 [BTRFS_XATTR_INDEX_USER] = &btrfs_xattr_user_handler,
32 [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler,
33 [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler,
34 [BTRFS_XATTR_INDEX_TRUSTED] = &btrfs_xattr_trusted_handler,
35 [BTRFS_XATTR_INDEX_SECURITY] = &btrfs_xattr_security_handler,
36 [BTRFS_XATTR_INDEX_SYSTEM] = &btrfs_xattr_system_handler,
39 struct xattr_handler *btrfs_xattr_handlers[] = {
40 &btrfs_xattr_user_handler,
41 &btrfs_xattr_acl_access_handler,
42 &btrfs_xattr_acl_default_handler,
43 &btrfs_xattr_trusted_handler,
44 &btrfs_xattr_security_handler,
45 &btrfs_xattr_system_handler,
50 * @param name - the xattr name
51 * @return - the xattr_handler for the xattr, NULL if its not found
53 * use this with listxattr where we don't already know the type of xattr we
56 static struct xattr_handler *find_btrfs_xattr_handler(struct extent_buffer *l,
57 unsigned long name_ptr,
60 struct xattr_handler *handler = NULL;
63 for (handler = btrfs_xattr_handlers[i]; handler != NULL; i++,
64 handler = btrfs_xattr_handlers[i]) {
65 u16 prefix_len = strlen(handler->prefix);
67 if (name_len < prefix_len)
70 if (memcmp_extent_buffer(l, handler->prefix, name_ptr,
79 * @param name_index - the index for the xattr handler
80 * @return the xattr_handler if we found it, NULL otherwise
82 * use this if we know the type of the xattr already
84 static struct xattr_handler *btrfs_xattr_handler(int name_index)
86 struct xattr_handler *handler = NULL;
88 if (name_index >= 0 &&
89 name_index < ARRAY_SIZE(btrfs_xattr_handler_map))
90 handler = btrfs_xattr_handler_map[name_index];
95 static inline char *get_name(const char *name, int name_index)
98 struct xattr_handler *handler = btrfs_xattr_handler(name_index);
104 prefix_len = strlen(handler->prefix);
106 ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL);
110 memcpy(ret, handler->prefix, prefix_len);
111 memcpy(ret+prefix_len, name, strlen(name));
112 ret[prefix_len + strlen(name)] = '\0';
117 size_t btrfs_xattr_generic_list(struct inode *inode, char *list,
118 size_t list_size, const char *name,
121 if (list && (name_len+1) <= list_size) {
122 memcpy(list, name, name_len);
123 list[name_len] = '\0';
130 ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
131 const char *attr_name, void *buffer, size_t size)
133 struct btrfs_dir_item *di;
134 struct btrfs_root *root = BTRFS_I(inode)->root;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct xattr_handler *handler = btrfs_xattr_handler(name_index);
139 unsigned long data_ptr;
144 name = get_name(attr_name, name_index);
148 path = btrfs_alloc_path();
154 mutex_lock(&root->fs_info->fs_mutex);
155 /* lookup the xattr by name */
156 di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
158 if (!di || IS_ERR(di)) {
163 leaf = path->nodes[0];
164 /* if size is 0, that means we want the size of the attr */
166 ret = btrfs_dir_data_len(leaf, di);
170 /* now get the data out of our dir_item */
171 if (btrfs_dir_data_len(leaf, di) > size) {
175 data_ptr = (unsigned long)((char *)(di + 1) +
176 btrfs_dir_name_len(leaf, di));
177 read_extent_buffer(leaf, buffer, data_ptr,
178 btrfs_dir_data_len(leaf, di));
179 ret = btrfs_dir_data_len(leaf, di);
182 mutex_unlock(&root->fs_info->fs_mutex);
184 btrfs_free_path(path);
188 int btrfs_xattr_set(struct inode *inode, int name_index,
189 const char *attr_name, const void *value, size_t size,
192 struct btrfs_dir_item *di;
193 struct btrfs_root *root = BTRFS_I(inode)->root;
194 struct btrfs_trans_handle *trans;
195 struct btrfs_path *path;
196 struct xattr_handler *handler = btrfs_xattr_handler(name_index);
198 int ret = 0, mod = 0;
201 name = get_name(attr_name, name_index);
205 path = btrfs_alloc_path();
211 mutex_lock(&root->fs_info->fs_mutex);
212 trans = btrfs_start_transaction(root, 1);
213 btrfs_set_trans_block_group(trans, inode);
215 /* first lets see if we already have this xattr */
216 di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
223 /* ok we already have this xattr, lets remove it */
225 /* if we want create only exit */
226 if (flags & XATTR_CREATE) {
231 ret = btrfs_delete_one_dir_name(trans, root, path, di);
234 btrfs_release_path(root, path);
236 /* if we don't have a value then we are removing the xattr */
241 } else if (flags & XATTR_REPLACE) {
242 /* we couldn't find the attr to replace, so error out */
247 /* ok we have to create a completely new xattr */
248 ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
249 value, size, inode->i_ino);
256 inode->i_ctime = CURRENT_TIME;
257 ret = btrfs_update_inode(trans, root, inode);
260 btrfs_end_transaction(trans, root);
261 mutex_unlock(&root->fs_info->fs_mutex);
263 btrfs_free_path(path);
268 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
270 struct btrfs_key key, found_key;
271 struct inode *inode = dentry->d_inode;
272 struct btrfs_root *root = BTRFS_I(inode)->root;
273 struct btrfs_path *path;
274 struct btrfs_item *item;
275 struct extent_buffer *leaf;
276 struct btrfs_dir_item *di;
277 struct xattr_handler *handler;
278 int ret = 0, slot, advance;
279 size_t total_size = 0, size_left = size, written;
280 unsigned long name_ptr;
285 * ok we want all objects associated with this id.
286 * NOTE: we set key.offset = 0; because we want to start with the
287 * first xattr that we find and walk forward
289 key.objectid = inode->i_ino;
290 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
293 path = btrfs_alloc_path();
298 mutex_lock(&root->fs_info->fs_mutex);
300 /* search for our xattrs */
301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
307 leaf = path->nodes[0];
308 nritems = btrfs_header_nritems(leaf);
309 slot = path->slots[0];
311 /* this is where we start walking through the path */
312 if (advance || slot >= nritems) {
314 * if we've reached the last slot in this leaf we need
315 * to go to the next leaf and reset everything
317 if (slot >= nritems-1) {
318 ret = btrfs_next_leaf(root, path);
321 leaf = path->nodes[0];
322 nritems = btrfs_header_nritems(leaf);
323 slot = path->slots[0];
326 * just walking through the slots on this leaf
334 item = btrfs_item_nr(leaf, slot);
335 btrfs_item_key_to_cpu(leaf, &found_key, slot);
337 /* check to make sure this item is what we want */
338 if (found_key.objectid != key.objectid)
340 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
343 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
345 total_size += btrfs_dir_name_len(leaf, di)+1;
347 /* we are just looking for how big our buffer needs to be */
351 /* find our handler for this xattr */
352 name_ptr = (unsigned long)(di + 1);
353 handler = find_btrfs_xattr_handler(leaf, name_ptr,
354 btrfs_dir_name_len(leaf, di));
356 printk(KERN_ERR "btrfs: unsupported xattr found\n");
360 name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL);
361 read_extent_buffer(leaf, name, name_ptr,
362 btrfs_dir_name_len(leaf, di));
364 /* call the list function associated with this xattr */
365 written = handler->list(inode, buffer, size_left, name,
366 btrfs_dir_name_len(leaf, di));
374 size_left -= written;
380 mutex_unlock(&root->fs_info->fs_mutex);
381 btrfs_free_path(path);
387 * delete all the xattrs associated with the inode. fs_mutex should be
388 * held when we come into here
390 int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
391 struct btrfs_root *root, struct inode *inode)
393 struct btrfs_path *path;
394 struct btrfs_key key, found_key;
395 struct btrfs_item *item;
396 struct extent_buffer *leaf;
399 path = btrfs_alloc_path();
403 key.objectid = inode->i_ino;
404 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
405 key.offset = (u64)-1;
408 /* look for our next xattr */
409 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
414 if (path->slots[0] == 0)
418 leaf = path->nodes[0];
419 item = btrfs_item_nr(leaf, path->slots[0]);
420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
422 if (found_key.objectid != key.objectid)
424 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
427 ret = btrfs_del_item(trans, root, path);
429 btrfs_release_path(root, path);
433 btrfs_free_path(path);
441 #define BTRFS_XATTR_SETGET_FUNCS(name, index) \
442 static int btrfs_xattr_##name##_get(struct inode *inode, \
443 const char *name, void *value, \
448 return btrfs_xattr_get(inode, index, name, value, size); \
450 static int btrfs_xattr_##name##_set(struct inode *inode, \
451 const char *name, const void *value,\
452 size_t size, int flags) \
456 return btrfs_xattr_set(inode, index, name, value, size, flags); \
458 BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
459 BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
460 BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
461 BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
463 struct xattr_handler btrfs_xattr_security_handler = {
464 .prefix = XATTR_SECURITY_PREFIX,
465 .list = btrfs_xattr_generic_list,
466 .get = btrfs_xattr_security_get,
467 .set = btrfs_xattr_security_set,
470 struct xattr_handler btrfs_xattr_system_handler = {
471 .prefix = XATTR_SYSTEM_PREFIX,
472 .list = btrfs_xattr_generic_list,
473 .get = btrfs_xattr_system_get,
474 .set = btrfs_xattr_system_set,
477 struct xattr_handler btrfs_xattr_user_handler = {
478 .prefix = XATTR_USER_PREFIX,
479 .list = btrfs_xattr_generic_list,
480 .get = btrfs_xattr_user_get,
481 .set = btrfs_xattr_user_set,
484 struct xattr_handler btrfs_xattr_trusted_handler = {
485 .prefix = XATTR_TRUSTED_PREFIX,
486 .list = btrfs_xattr_generic_list,
487 .get = btrfs_xattr_trusted_get,
488 .set = btrfs_xattr_trusted_set,