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" | |
5103e947 JB |
29 | static struct xattr_handler *btrfs_xattr_handler_map[] = { |
30 | [BTRFS_XATTR_INDEX_USER] = &btrfs_xattr_user_handler, | |
caaca38b | 31 | #ifdef CONFIG_FS_POSIX_ACL |
eba12c7b Y |
32 | // [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler, |
33 | // [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler, | |
caaca38b | 34 | #endif |
5103e947 JB |
35 | [BTRFS_XATTR_INDEX_TRUSTED] = &btrfs_xattr_trusted_handler, |
36 | [BTRFS_XATTR_INDEX_SECURITY] = &btrfs_xattr_security_handler, | |
eba12c7b | 37 | // [BTRFS_XATTR_INDEX_SYSTEM] = &btrfs_xattr_system_handler, |
5103e947 | 38 | }; |
5103e947 JB |
39 | struct xattr_handler *btrfs_xattr_handlers[] = { |
40 | &btrfs_xattr_user_handler, | |
caaca38b | 41 | #ifdef CONFIG_FS_POSIX_ACL |
eba12c7b Y |
42 | // &btrfs_xattr_acl_access_handler, |
43 | // &btrfs_xattr_acl_default_handler, | |
caaca38b | 44 | #endif |
5103e947 JB |
45 | &btrfs_xattr_trusted_handler, |
46 | &btrfs_xattr_security_handler, | |
eba12c7b | 47 | // &btrfs_xattr_system_handler, |
5103e947 JB |
48 | NULL, |
49 | }; | |
50 | ||
51 | /* | |
52 | * @param name - the xattr name | |
53 | * @return - the xattr_handler for the xattr, NULL if its not found | |
54 | * | |
55 | * use this with listxattr where we don't already know the type of xattr we | |
56 | * have | |
57 | */ | |
58 | static struct xattr_handler *find_btrfs_xattr_handler(struct extent_buffer *l, | |
59 | unsigned long name_ptr, | |
60 | u16 name_len) | |
61 | { | |
62 | struct xattr_handler *handler = NULL; | |
63 | int i = 0; | |
64 | ||
65 | for (handler = btrfs_xattr_handlers[i]; handler != NULL; i++, | |
66 | handler = btrfs_xattr_handlers[i]) { | |
67 | u16 prefix_len = strlen(handler->prefix); | |
68 | ||
69 | if (name_len < prefix_len) | |
70 | continue; | |
71 | ||
72 | if (memcmp_extent_buffer(l, handler->prefix, name_ptr, | |
73 | prefix_len) == 0) | |
74 | break; | |
75 | } | |
76 | ||
77 | return handler; | |
78 | } | |
79 | ||
80 | /* | |
81 | * @param name_index - the index for the xattr handler | |
82 | * @return the xattr_handler if we found it, NULL otherwise | |
83 | * | |
84 | * use this if we know the type of the xattr already | |
85 | */ | |
86 | static struct xattr_handler *btrfs_xattr_handler(int name_index) | |
87 | { | |
88 | struct xattr_handler *handler = NULL; | |
89 | ||
90 | if (name_index >= 0 && | |
91 | name_index < ARRAY_SIZE(btrfs_xattr_handler_map)) | |
92 | handler = btrfs_xattr_handler_map[name_index]; | |
93 | ||
94 | return handler; | |
95 | } | |
96 | ||
97 | static inline char *get_name(const char *name, int name_index) | |
98 | { | |
99 | char *ret = NULL; | |
100 | struct xattr_handler *handler = btrfs_xattr_handler(name_index); | |
101 | int prefix_len; | |
102 | ||
103 | if (!handler) | |
104 | return ret; | |
105 | ||
106 | prefix_len = strlen(handler->prefix); | |
107 | ||
108 | ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL); | |
109 | if (!ret) | |
110 | return ret; | |
111 | ||
112 | memcpy(ret, handler->prefix, prefix_len); | |
113 | memcpy(ret+prefix_len, name, strlen(name)); | |
114 | ret[prefix_len + strlen(name)] = '\0'; | |
115 | ||
116 | return ret; | |
117 | } | |
118 | ||
119 | size_t btrfs_xattr_generic_list(struct inode *inode, char *list, | |
120 | size_t list_size, const char *name, | |
121 | size_t name_len) | |
122 | { | |
123 | if (list && (name_len+1) <= list_size) { | |
124 | memcpy(list, name, name_len); | |
125 | list[name_len] = '\0'; | |
126 | } else | |
127 | return -ERANGE; | |
128 | ||
129 | return name_len+1; | |
130 | } | |
131 | ||
132 | ssize_t btrfs_xattr_get(struct inode *inode, int name_index, | |
133 | const char *attr_name, void *buffer, size_t size) | |
134 | { | |
135 | struct btrfs_dir_item *di; | |
136 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
137 | struct btrfs_path *path; | |
138 | struct extent_buffer *leaf; | |
139 | struct xattr_handler *handler = btrfs_xattr_handler(name_index); | |
140 | int ret = 0; | |
141 | unsigned long data_ptr; | |
142 | char *name; | |
143 | ||
144 | if (!handler) | |
145 | return -EOPNOTSUPP; | |
5103e947 JB |
146 | name = get_name(attr_name, name_index); |
147 | if (!name) | |
148 | return -ENOMEM; | |
149 | ||
150 | path = btrfs_alloc_path(); | |
151 | if (!path) { | |
152 | kfree(name); | |
153 | return -ENOMEM; | |
154 | } | |
155 | ||
5103e947 JB |
156 | /* lookup the xattr by name */ |
157 | di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name, | |
158 | strlen(name), 0); | |
159 | if (!di || IS_ERR(di)) { | |
160 | ret = -ENODATA; | |
161 | goto out; | |
162 | } | |
163 | ||
164 | leaf = path->nodes[0]; | |
165 | /* if size is 0, that means we want the size of the attr */ | |
166 | if (!size) { | |
167 | ret = btrfs_dir_data_len(leaf, di); | |
168 | goto out; | |
169 | } | |
170 | ||
171 | /* now get the data out of our dir_item */ | |
172 | if (btrfs_dir_data_len(leaf, di) > size) { | |
173 | ret = -ERANGE; | |
174 | goto out; | |
175 | } | |
176 | data_ptr = (unsigned long)((char *)(di + 1) + | |
177 | btrfs_dir_name_len(leaf, di)); | |
178 | read_extent_buffer(leaf, buffer, data_ptr, | |
3acd7ee8 | 179 | btrfs_dir_data_len(leaf, di)); |
5103e947 JB |
180 | ret = btrfs_dir_data_len(leaf, di); |
181 | ||
182 | out: | |
5103e947 JB |
183 | kfree(name); |
184 | btrfs_free_path(path); | |
185 | return ret; | |
186 | } | |
187 | ||
188 | int btrfs_xattr_set(struct inode *inode, int name_index, | |
189 | const char *attr_name, const void *value, size_t size, | |
190 | int flags) | |
191 | { | |
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); | |
197 | char *name; | |
198 | int ret = 0, mod = 0; | |
5103e947 JB |
199 | if (!handler) |
200 | return -EOPNOTSUPP; | |
5103e947 JB |
201 | name = get_name(attr_name, name_index); |
202 | if (!name) | |
203 | return -ENOMEM; | |
204 | ||
205 | path = btrfs_alloc_path(); | |
206 | if (!path) { | |
207 | kfree(name); | |
208 | return -ENOMEM; | |
209 | } | |
210 | ||
5103e947 JB |
211 | trans = btrfs_start_transaction(root, 1); |
212 | btrfs_set_trans_block_group(trans, inode); | |
213 | ||
214 | /* first lets see if we already have this xattr */ | |
215 | di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name, | |
216 | strlen(name), -1); | |
217 | if (IS_ERR(di)) { | |
218 | ret = PTR_ERR(di); | |
219 | goto out; | |
220 | } | |
221 | ||
222 | /* ok we already have this xattr, lets remove it */ | |
223 | if (di) { | |
224 | /* if we want create only exit */ | |
225 | if (flags & XATTR_CREATE) { | |
226 | ret = -EEXIST; | |
227 | goto out; | |
228 | } | |
229 | ||
230 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
231 | if (ret) | |
232 | goto out; | |
233 | btrfs_release_path(root, path); | |
234 | ||
235 | /* if we don't have a value then we are removing the xattr */ | |
236 | if (!value) { | |
237 | mod = 1; | |
238 | goto out; | |
239 | } | |
240 | } else if (flags & XATTR_REPLACE) { | |
241 | /* we couldn't find the attr to replace, so error out */ | |
242 | ret = -ENODATA; | |
243 | goto out; | |
244 | } | |
245 | ||
246 | /* ok we have to create a completely new xattr */ | |
247 | ret = btrfs_insert_xattr_item(trans, root, name, strlen(name), | |
248 | value, size, inode->i_ino); | |
249 | if (ret) | |
250 | goto out; | |
251 | mod = 1; | |
252 | ||
253 | out: | |
254 | if (mod) { | |
255 | inode->i_ctime = CURRENT_TIME; | |
256 | ret = btrfs_update_inode(trans, root, inode); | |
257 | } | |
258 | ||
259 | btrfs_end_transaction(trans, root); | |
5103e947 JB |
260 | kfree(name); |
261 | btrfs_free_path(path); | |
262 | ||
263 | return ret; | |
264 | } | |
265 | ||
266 | ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
267 | { | |
268 | struct btrfs_key key, found_key; | |
269 | struct inode *inode = dentry->d_inode; | |
270 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
271 | struct btrfs_path *path; | |
272 | struct btrfs_item *item; | |
273 | struct extent_buffer *leaf; | |
274 | struct btrfs_dir_item *di; | |
275 | struct xattr_handler *handler; | |
276 | int ret = 0, slot, advance; | |
277 | size_t total_size = 0, size_left = size, written; | |
278 | unsigned long name_ptr; | |
279 | char *name; | |
280 | u32 nritems; | |
281 | ||
282 | /* | |
283 | * ok we want all objects associated with this id. | |
284 | * NOTE: we set key.offset = 0; because we want to start with the | |
285 | * first xattr that we find and walk forward | |
286 | */ | |
287 | key.objectid = inode->i_ino; | |
288 | btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); | |
289 | key.offset = 0; | |
290 | ||
291 | path = btrfs_alloc_path(); | |
5103e947 JB |
292 | if (!path) |
293 | return -ENOMEM; | |
1caf9342 | 294 | path->reada = 2; |
5103e947 | 295 | |
5103e947 JB |
296 | /* search for our xattrs */ |
297 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
298 | if (ret < 0) | |
299 | goto err; | |
300 | ret = 0; | |
301 | advance = 0; | |
302 | while (1) { | |
303 | leaf = path->nodes[0]; | |
304 | nritems = btrfs_header_nritems(leaf); | |
305 | slot = path->slots[0]; | |
306 | ||
307 | /* this is where we start walking through the path */ | |
308 | if (advance || slot >= nritems) { | |
309 | /* | |
310 | * if we've reached the last slot in this leaf we need | |
311 | * to go to the next leaf and reset everything | |
312 | */ | |
313 | if (slot >= nritems-1) { | |
314 | ret = btrfs_next_leaf(root, path); | |
315 | if (ret) | |
316 | break; | |
317 | leaf = path->nodes[0]; | |
318 | nritems = btrfs_header_nritems(leaf); | |
319 | slot = path->slots[0]; | |
320 | } else { | |
321 | /* | |
322 | * just walking through the slots on this leaf | |
323 | */ | |
324 | slot++; | |
325 | path->slots[0]++; | |
326 | } | |
327 | } | |
328 | advance = 1; | |
329 | ||
330 | item = btrfs_item_nr(leaf, slot); | |
331 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
332 | ||
333 | /* check to make sure this item is what we want */ | |
334 | if (found_key.objectid != key.objectid) | |
335 | break; | |
336 | if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) | |
337 | break; | |
338 | ||
339 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
340 | ||
341 | total_size += btrfs_dir_name_len(leaf, di)+1; | |
342 | ||
343 | /* we are just looking for how big our buffer needs to be */ | |
344 | if (!size) | |
345 | continue; | |
346 | ||
347 | /* find our handler for this xattr */ | |
348 | name_ptr = (unsigned long)(di + 1); | |
349 | handler = find_btrfs_xattr_handler(leaf, name_ptr, | |
350 | btrfs_dir_name_len(leaf, di)); | |
351 | if (!handler) { | |
352 | printk(KERN_ERR "btrfs: unsupported xattr found\n"); | |
353 | continue; | |
354 | } | |
355 | ||
356 | name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL); | |
357 | read_extent_buffer(leaf, name, name_ptr, | |
358 | btrfs_dir_name_len(leaf, di)); | |
359 | ||
360 | /* call the list function associated with this xattr */ | |
361 | written = handler->list(inode, buffer, size_left, name, | |
362 | btrfs_dir_name_len(leaf, di)); | |
363 | kfree(name); | |
364 | ||
365 | if (written < 0) { | |
366 | ret = -ERANGE; | |
367 | break; | |
368 | } | |
369 | ||
370 | size_left -= written; | |
371 | buffer += written; | |
372 | } | |
373 | ret = total_size; | |
374 | ||
375 | err: | |
5103e947 JB |
376 | btrfs_free_path(path); |
377 | ||
378 | return ret; | |
379 | } | |
380 | ||
381 | /* | |
a2135011 | 382 | * delete all the xattrs associated with the inode. |
5103e947 JB |
383 | */ |
384 | int btrfs_delete_xattrs(struct btrfs_trans_handle *trans, | |
385 | struct btrfs_root *root, struct inode *inode) | |
386 | { | |
387 | struct btrfs_path *path; | |
388 | struct btrfs_key key, found_key; | |
389 | struct btrfs_item *item; | |
390 | struct extent_buffer *leaf; | |
391 | int ret; | |
392 | ||
393 | path = btrfs_alloc_path(); | |
394 | if (!path) | |
395 | return -ENOMEM; | |
1caf9342 | 396 | path->reada = -1; |
5103e947 JB |
397 | key.objectid = inode->i_ino; |
398 | btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); | |
399 | key.offset = (u64)-1; | |
400 | ||
401 | while(1) { | |
402 | /* look for our next xattr */ | |
403 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
404 | if (ret < 0) | |
405 | goto out; | |
406 | BUG_ON(ret == 0); | |
407 | ||
408 | if (path->slots[0] == 0) | |
409 | break; | |
410 | ||
411 | path->slots[0]--; | |
412 | leaf = path->nodes[0]; | |
413 | item = btrfs_item_nr(leaf, path->slots[0]); | |
414 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
415 | ||
416 | if (found_key.objectid != key.objectid) | |
417 | break; | |
418 | if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) | |
419 | break; | |
420 | ||
421 | ret = btrfs_del_item(trans, root, path); | |
422 | BUG_ON(ret); | |
423 | btrfs_release_path(root, path); | |
424 | } | |
425 | ret = 0; | |
426 | out: | |
427 | btrfs_free_path(path); | |
428 | ||
429 | return ret; | |
430 | } | |
431 | ||
432 | /* | |
433 | * Handler functions | |
434 | */ | |
435 | #define BTRFS_XATTR_SETGET_FUNCS(name, index) \ | |
436 | static int btrfs_xattr_##name##_get(struct inode *inode, \ | |
437 | const char *name, void *value, \ | |
438 | size_t size) \ | |
439 | { \ | |
744f52f9 Y |
440 | if (*name == '\0') \ |
441 | return -EINVAL; \ | |
5103e947 JB |
442 | return btrfs_xattr_get(inode, index, name, value, size); \ |
443 | } \ | |
444 | static int btrfs_xattr_##name##_set(struct inode *inode, \ | |
445 | const char *name, const void *value,\ | |
446 | size_t size, int flags) \ | |
447 | { \ | |
744f52f9 Y |
448 | if (*name == '\0') \ |
449 | return -EINVAL; \ | |
5103e947 | 450 | return btrfs_xattr_set(inode, index, name, value, size, flags); \ |
69a32ac5 CM |
451 | } |
452 | ||
5103e947 JB |
453 | BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY); |
454 | BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM); | |
455 | BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER); | |
456 | BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED); | |
457 | ||
458 | struct xattr_handler btrfs_xattr_security_handler = { | |
459 | .prefix = XATTR_SECURITY_PREFIX, | |
460 | .list = btrfs_xattr_generic_list, | |
461 | .get = btrfs_xattr_security_get, | |
462 | .set = btrfs_xattr_security_set, | |
463 | }; | |
464 | ||
465 | struct xattr_handler btrfs_xattr_system_handler = { | |
466 | .prefix = XATTR_SYSTEM_PREFIX, | |
467 | .list = btrfs_xattr_generic_list, | |
468 | .get = btrfs_xattr_system_get, | |
469 | .set = btrfs_xattr_system_set, | |
470 | }; | |
471 | ||
472 | struct xattr_handler btrfs_xattr_user_handler = { | |
473 | .prefix = XATTR_USER_PREFIX, | |
474 | .list = btrfs_xattr_generic_list, | |
475 | .get = btrfs_xattr_user_get, | |
476 | .set = btrfs_xattr_user_set, | |
477 | }; | |
478 | ||
479 | struct xattr_handler btrfs_xattr_trusted_handler = { | |
3acd7ee8 | 480 | .prefix = XATTR_TRUSTED_PREFIX, |
5103e947 JB |
481 | .list = btrfs_xattr_generic_list, |
482 | .get = btrfs_xattr_trusted_get, | |
483 | .set = btrfs_xattr_trusted_set, | |
484 | }; |