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
,
51 } else if (IS_ERR(di
)) {
56 leaf
= path
->nodes
[0];
57 /* if size is 0, that means we want the size of the attr */
59 ret
= btrfs_dir_data_len(leaf
, di
);
63 /* now get the data out of our dir_item */
64 if (btrfs_dir_data_len(leaf
, di
) > size
) {
70 * The way things are packed into the leaf is like this
71 * |struct btrfs_dir_item|name|data|
72 * where name is the xattr name, so security.foo, and data is the
73 * content of the xattr. data_ptr points to the location in memory
74 * where the data starts in the in memory leaf
76 data_ptr
= (unsigned long)((char *)(di
+ 1) +
77 btrfs_dir_name_len(leaf
, di
));
78 read_extent_buffer(leaf
, buffer
, data_ptr
,
79 btrfs_dir_data_len(leaf
, di
));
80 ret
= btrfs_dir_data_len(leaf
, di
);
83 btrfs_free_path(path
);
87 int __btrfs_setxattr(struct inode
*inode
, const char *name
,
88 const void *value
, size_t size
, int flags
)
90 struct btrfs_dir_item
*di
;
91 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
92 struct btrfs_trans_handle
*trans
;
93 struct btrfs_path
*path
;
96 path
= btrfs_alloc_path();
100 trans
= btrfs_start_transaction(root
, 1);
101 btrfs_set_trans_block_group(trans
, inode
);
103 /* first lets see if we already have this xattr */
104 di
= btrfs_lookup_xattr(trans
, root
, path
, inode
->i_ino
, name
,
111 /* ok we already have this xattr, lets remove it */
113 /* if we want create only exit */
114 if (flags
& XATTR_CREATE
) {
119 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
122 btrfs_release_path(root
, path
);
124 /* if we don't have a value then we are removing the xattr */
130 btrfs_release_path(root
, path
);
132 if (flags
& XATTR_REPLACE
) {
133 /* we couldn't find the attr to replace */
139 /* ok we have to create a completely new xattr */
140 ret
= btrfs_insert_xattr_item(trans
, root
, name
, strlen(name
),
141 value
, size
, inode
->i_ino
);
148 inode
->i_ctime
= CURRENT_TIME
;
149 ret
= btrfs_update_inode(trans
, root
, inode
);
152 btrfs_end_transaction(trans
, root
);
153 btrfs_free_path(path
);
157 ssize_t
btrfs_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
159 struct btrfs_key key
, found_key
;
160 struct inode
*inode
= dentry
->d_inode
;
161 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
162 struct btrfs_path
*path
;
163 struct btrfs_item
*item
;
164 struct extent_buffer
*leaf
;
165 struct btrfs_dir_item
*di
;
166 int ret
= 0, slot
, advance
;
167 size_t total_size
= 0, size_left
= size
;
168 unsigned long name_ptr
;
173 * ok we want all objects associated with this id.
174 * NOTE: we set key.offset = 0; because we want to start with the
175 * first xattr that we find and walk forward
177 key
.objectid
= inode
->i_ino
;
178 btrfs_set_key_type(&key
, BTRFS_XATTR_ITEM_KEY
);
181 path
= btrfs_alloc_path();
186 /* search for our xattrs */
187 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
192 leaf
= path
->nodes
[0];
193 nritems
= btrfs_header_nritems(leaf
);
194 slot
= path
->slots
[0];
196 /* this is where we start walking through the path */
197 if (advance
|| slot
>= nritems
) {
199 * if we've reached the last slot in this leaf we need
200 * to go to the next leaf and reset everything
202 if (slot
>= nritems
-1) {
203 ret
= btrfs_next_leaf(root
, path
);
206 leaf
= path
->nodes
[0];
207 nritems
= btrfs_header_nritems(leaf
);
208 slot
= path
->slots
[0];
211 * just walking through the slots on this leaf
219 item
= btrfs_item_nr(leaf
, slot
);
220 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
222 /* check to make sure this item is what we want */
223 if (found_key
.objectid
!= key
.objectid
)
225 if (btrfs_key_type(&found_key
) != BTRFS_XATTR_ITEM_KEY
)
228 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
230 name_len
= btrfs_dir_name_len(leaf
, di
);
231 total_size
+= name_len
+ 1;
233 /* we are just looking for how big our buffer needs to be */
237 if (!buffer
|| (name_len
+ 1) > size_left
) {
242 name_ptr
= (unsigned long)(di
+ 1);
243 read_extent_buffer(leaf
, buffer
, name_ptr
, name_len
);
244 buffer
[name_len
] = '\0';
246 size_left
-= name_len
+ 1;
247 buffer
+= name_len
+ 1;
252 btrfs_free_path(path
);
258 * List of handlers for synthetic system.* attributes. All real ondisk
259 * attributes are handled directly.
261 struct xattr_handler
*btrfs_xattr_handlers
[] = {
262 #ifdef CONFIG_FS_POSIX_ACL
263 &btrfs_xattr_acl_access_handler
,
264 &btrfs_xattr_acl_default_handler
,
270 * Check if the attribute is in a supported namespace.
272 * This applied after the check for the synthetic attributes in the system
275 static bool btrfs_is_valid_xattr(const char *name
)
277 return !strncmp(name
, XATTR_SECURITY_PREFIX
,
278 XATTR_SECURITY_PREFIX_LEN
) ||
279 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) ||
280 !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
281 !strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
);
284 ssize_t
btrfs_getxattr(struct dentry
*dentry
, const char *name
,
285 void *buffer
, size_t size
)
288 * If this is a request for a synthetic attribute in the system.*
289 * namespace use the generic infrastructure to resolve a handler
290 * for it via sb->s_xattr.
292 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
293 return generic_getxattr(dentry
, name
, buffer
, size
);
295 if (!btrfs_is_valid_xattr(name
))
297 return __btrfs_getxattr(dentry
->d_inode
, name
, buffer
, size
);
300 int btrfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
301 size_t size
, int flags
)
304 * If this is a request for a synthetic attribute in the system.*
305 * namespace use the generic infrastructure to resolve a handler
306 * for it via sb->s_xattr.
308 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
309 return generic_setxattr(dentry
, name
, value
, size
, flags
);
311 if (!btrfs_is_valid_xattr(name
))
315 value
= ""; /* empty EA, do not remove */
316 return __btrfs_setxattr(dentry
->d_inode
, name
, value
, size
, flags
);
319 int btrfs_removexattr(struct dentry
*dentry
, const char *name
)
322 * If this is a request for a synthetic attribute in the system.*
323 * namespace use the generic infrastructure to resolve a handler
324 * for it via sb->s_xattr.
326 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
327 return generic_removexattr(dentry
, name
);
329 if (!btrfs_is_valid_xattr(name
))
331 return __btrfs_setxattr(dentry
->d_inode
, name
, NULL
, 0, XATTR_REPLACE
);