Commit | Line | Data |
---|---|---|
5103e947 JB |
1 | /* |
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> | |
24 | #include "ctree.h" | |
25 | #include "btrfs_inode.h" | |
26 | #include "transaction.h" | |
27 | #include "xattr.h" | |
28 | #include "disk-io.h" | |
33268eaf | 29 | |
5103e947 | 30 | |
95819c05 CH |
31 | ssize_t __btrfs_getxattr(struct inode *inode, const char *name, |
32 | void *buffer, size_t size) | |
5103e947 JB |
33 | { |
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; | |
5103e947 JB |
38 | int ret = 0; |
39 | unsigned long data_ptr; | |
5103e947 JB |
40 | |
41 | path = btrfs_alloc_path(); | |
95819c05 | 42 | if (!path) |
5103e947 | 43 | return -ENOMEM; |
5103e947 | 44 | |
5103e947 JB |
45 | /* lookup the xattr by name */ |
46 | di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name, | |
47 | strlen(name), 0); | |
48 | if (!di || IS_ERR(di)) { | |
49 | ret = -ENODATA; | |
50 | goto out; | |
51 | } | |
52 | ||
53 | leaf = path->nodes[0]; | |
54 | /* if size is 0, that means we want the size of the attr */ | |
55 | if (!size) { | |
56 | ret = btrfs_dir_data_len(leaf, di); | |
57 | goto out; | |
58 | } | |
59 | ||
60 | /* now get the data out of our dir_item */ | |
61 | if (btrfs_dir_data_len(leaf, di) > size) { | |
62 | ret = -ERANGE; | |
63 | goto out; | |
64 | } | |
65 | data_ptr = (unsigned long)((char *)(di + 1) + | |
66 | btrfs_dir_name_len(leaf, di)); | |
67 | read_extent_buffer(leaf, buffer, data_ptr, | |
3acd7ee8 | 68 | btrfs_dir_data_len(leaf, di)); |
5103e947 JB |
69 | ret = btrfs_dir_data_len(leaf, di); |
70 | ||
71 | out: | |
5103e947 JB |
72 | btrfs_free_path(path); |
73 | return ret; | |
74 | } | |
75 | ||
95819c05 CH |
76 | int __btrfs_setxattr(struct inode *inode, const char *name, |
77 | const void *value, size_t size, int flags) | |
5103e947 JB |
78 | { |
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; | |
5103e947 | 83 | int ret = 0, mod = 0; |
5103e947 JB |
84 | |
85 | path = btrfs_alloc_path(); | |
95819c05 | 86 | if (!path) |
5103e947 | 87 | return -ENOMEM; |
5103e947 | 88 | |
5103e947 JB |
89 | trans = btrfs_start_transaction(root, 1); |
90 | btrfs_set_trans_block_group(trans, inode); | |
91 | ||
92 | /* first lets see if we already have this xattr */ | |
93 | di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name, | |
94 | strlen(name), -1); | |
95 | if (IS_ERR(di)) { | |
96 | ret = PTR_ERR(di); | |
97 | goto out; | |
98 | } | |
99 | ||
100 | /* ok we already have this xattr, lets remove it */ | |
101 | if (di) { | |
102 | /* if we want create only exit */ | |
103 | if (flags & XATTR_CREATE) { | |
104 | ret = -EEXIST; | |
105 | goto out; | |
106 | } | |
107 | ||
108 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
109 | if (ret) | |
110 | goto out; | |
111 | btrfs_release_path(root, path); | |
112 | ||
113 | /* if we don't have a value then we are removing the xattr */ | |
114 | if (!value) { | |
115 | mod = 1; | |
116 | goto out; | |
117 | } | |
33268eaf JB |
118 | } else { |
119 | btrfs_release_path(root, path); | |
120 | ||
121 | if (flags & XATTR_REPLACE) { | |
122 | /* we couldn't find the attr to replace */ | |
123 | ret = -ENODATA; | |
124 | goto out; | |
125 | } | |
5103e947 JB |
126 | } |
127 | ||
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); | |
131 | if (ret) | |
132 | goto out; | |
133 | mod = 1; | |
134 | ||
135 | out: | |
136 | if (mod) { | |
137 | inode->i_ctime = CURRENT_TIME; | |
138 | ret = btrfs_update_inode(trans, root, inode); | |
139 | } | |
140 | ||
141 | btrfs_end_transaction(trans, root); | |
5103e947 | 142 | btrfs_free_path(path); |
5103e947 JB |
143 | return ret; |
144 | } | |
145 | ||
146 | ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
147 | { | |
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; | |
5103e947 | 155 | int ret = 0, slot, advance; |
eaa47d86 | 156 | size_t total_size = 0, size_left = size; |
5103e947 | 157 | unsigned long name_ptr; |
eaa47d86 | 158 | size_t name_len; |
5103e947 JB |
159 | u32 nritems; |
160 | ||
161 | /* | |
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 | |
165 | */ | |
166 | key.objectid = inode->i_ino; | |
167 | btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); | |
168 | key.offset = 0; | |
169 | ||
170 | path = btrfs_alloc_path(); | |
5103e947 JB |
171 | if (!path) |
172 | return -ENOMEM; | |
1caf9342 | 173 | path->reada = 2; |
5103e947 | 174 | |
5103e947 JB |
175 | /* search for our xattrs */ |
176 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
177 | if (ret < 0) | |
178 | goto err; | |
179 | ret = 0; | |
180 | advance = 0; | |
181 | while (1) { | |
182 | leaf = path->nodes[0]; | |
183 | nritems = btrfs_header_nritems(leaf); | |
184 | slot = path->slots[0]; | |
185 | ||
186 | /* this is where we start walking through the path */ | |
187 | if (advance || slot >= nritems) { | |
188 | /* | |
189 | * if we've reached the last slot in this leaf we need | |
190 | * to go to the next leaf and reset everything | |
191 | */ | |
192 | if (slot >= nritems-1) { | |
193 | ret = btrfs_next_leaf(root, path); | |
194 | if (ret) | |
195 | break; | |
196 | leaf = path->nodes[0]; | |
197 | nritems = btrfs_header_nritems(leaf); | |
198 | slot = path->slots[0]; | |
199 | } else { | |
200 | /* | |
201 | * just walking through the slots on this leaf | |
202 | */ | |
203 | slot++; | |
204 | path->slots[0]++; | |
205 | } | |
206 | } | |
207 | advance = 1; | |
208 | ||
209 | item = btrfs_item_nr(leaf, slot); | |
210 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
211 | ||
212 | /* check to make sure this item is what we want */ | |
213 | if (found_key.objectid != key.objectid) | |
214 | break; | |
215 | if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) | |
216 | break; | |
217 | ||
218 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
219 | ||
eaa47d86 CH |
220 | name_len = btrfs_dir_name_len(leaf, di); |
221 | total_size += name_len + 1; | |
5103e947 JB |
222 | |
223 | /* we are just looking for how big our buffer needs to be */ | |
224 | if (!size) | |
225 | continue; | |
226 | ||
eaa47d86 | 227 | if (!buffer || (name_len + 1) > size_left) { |
5103e947 JB |
228 | ret = -ERANGE; |
229 | break; | |
230 | } | |
231 | ||
eaa47d86 CH |
232 | name_ptr = (unsigned long)(di + 1); |
233 | read_extent_buffer(leaf, buffer, name_ptr, name_len); | |
234 | buffer[name_len] = '\0'; | |
235 | ||
236 | size_left -= name_len + 1; | |
237 | buffer += name_len + 1; | |
5103e947 JB |
238 | } |
239 | ret = total_size; | |
240 | ||
241 | err: | |
5103e947 JB |
242 | btrfs_free_path(path); |
243 | ||
244 | return ret; | |
245 | } | |
246 | ||
5103e947 | 247 | /* |
95819c05 CH |
248 | * List of handlers for synthetic system.* attributes. All real ondisk |
249 | * attributes are handled directly. | |
250 | */ | |
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, | |
255 | #endif | |
256 | NULL, | |
257 | }; | |
258 | ||
259 | /* | |
260 | * Check if the attribute is in a supported namespace. | |
261 | * | |
262 | * This applied after the check for the synthetic attributes in the system | |
263 | * namespace. | |
5103e947 | 264 | */ |
95819c05 CH |
265 | static bool btrfs_is_valid_xattr(const char *name) |
266 | { | |
267 | return !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) || | |
268 | !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) || | |
269 | !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || | |
270 | !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); | |
69a32ac5 CM |
271 | } |
272 | ||
95819c05 CH |
273 | ssize_t btrfs_getxattr(struct dentry *dentry, const char *name, |
274 | void *buffer, size_t size) | |
275 | { | |
276 | /* | |
277 | * If this is a request for a synthetic attribute in the system.* | |
278 | * namespace use the generic infrastructure to resolve a handler | |
279 | * for it via sb->s_xattr. | |
280 | */ | |
281 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
282 | return generic_getxattr(dentry, name, buffer, size); | |
5103e947 | 283 | |
95819c05 CH |
284 | if (!btrfs_is_valid_xattr(name)) |
285 | return -EOPNOTSUPP; | |
286 | return __btrfs_getxattr(dentry->d_inode, name, buffer, size); | |
287 | } | |
5103e947 | 288 | |
95819c05 CH |
289 | int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
290 | size_t size, int flags) | |
291 | { | |
292 | /* | |
293 | * If this is a request for a synthetic attribute in the system.* | |
294 | * namespace use the generic infrastructure to resolve a handler | |
295 | * for it via sb->s_xattr. | |
296 | */ | |
297 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
298 | return generic_setxattr(dentry, name, value, size, flags); | |
5103e947 | 299 | |
95819c05 CH |
300 | if (!btrfs_is_valid_xattr(name)) |
301 | return -EOPNOTSUPP; | |
5103e947 | 302 | |
95819c05 CH |
303 | if (size == 0) |
304 | value = ""; /* empty EA, do not remove */ | |
305 | return __btrfs_setxattr(dentry->d_inode, name, value, size, flags); | |
306 | } | |
307 | ||
308 | int btrfs_removexattr(struct dentry *dentry, const char *name) | |
309 | { | |
310 | /* | |
311 | * If this is a request for a synthetic attribute in the system.* | |
312 | * namespace use the generic infrastructure to resolve a handler | |
313 | * for it via sb->s_xattr. | |
314 | */ | |
315 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
316 | return generic_removexattr(dentry, name); | |
317 | ||
318 | if (!btrfs_is_valid_xattr(name)) | |
319 | return -EOPNOTSUPP; | |
320 | return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE); | |
321 | } |