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> | |
0279b4cd | 24 | #include <linux/security.h> |
5103e947 JB |
25 | #include "ctree.h" |
26 | #include "btrfs_inode.h" | |
27 | #include "transaction.h" | |
28 | #include "xattr.h" | |
29 | #include "disk-io.h" | |
63541927 | 30 | #include "props.h" |
33268eaf | 31 | |
5103e947 | 32 | |
95819c05 CH |
33 | ssize_t __btrfs_getxattr(struct inode *inode, const char *name, |
34 | void *buffer, size_t size) | |
5103e947 JB |
35 | { |
36 | struct btrfs_dir_item *di; | |
37 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
38 | struct btrfs_path *path; | |
39 | struct extent_buffer *leaf; | |
5103e947 JB |
40 | int ret = 0; |
41 | unsigned long data_ptr; | |
5103e947 JB |
42 | |
43 | path = btrfs_alloc_path(); | |
95819c05 | 44 | if (!path) |
5103e947 | 45 | return -ENOMEM; |
5103e947 | 46 | |
5103e947 | 47 | /* lookup the xattr by name */ |
33345d01 | 48 | di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name, |
5103e947 | 49 | strlen(name), 0); |
07060404 | 50 | if (!di) { |
5103e947 JB |
51 | ret = -ENODATA; |
52 | goto out; | |
07060404 JB |
53 | } else if (IS_ERR(di)) { |
54 | ret = PTR_ERR(di); | |
55 | goto out; | |
5103e947 JB |
56 | } |
57 | ||
58 | leaf = path->nodes[0]; | |
59 | /* if size is 0, that means we want the size of the attr */ | |
60 | if (!size) { | |
61 | ret = btrfs_dir_data_len(leaf, di); | |
62 | goto out; | |
63 | } | |
64 | ||
65 | /* now get the data out of our dir_item */ | |
66 | if (btrfs_dir_data_len(leaf, di) > size) { | |
67 | ret = -ERANGE; | |
68 | goto out; | |
69 | } | |
07060404 JB |
70 | |
71 | /* | |
72 | * The way things are packed into the leaf is like this | |
73 | * |struct btrfs_dir_item|name|data| | |
74 | * where name is the xattr name, so security.foo, and data is the | |
75 | * content of the xattr. data_ptr points to the location in memory | |
76 | * where the data starts in the in memory leaf | |
77 | */ | |
5103e947 JB |
78 | data_ptr = (unsigned long)((char *)(di + 1) + |
79 | btrfs_dir_name_len(leaf, di)); | |
80 | read_extent_buffer(leaf, buffer, data_ptr, | |
3acd7ee8 | 81 | btrfs_dir_data_len(leaf, di)); |
5103e947 JB |
82 | ret = btrfs_dir_data_len(leaf, di); |
83 | ||
84 | out: | |
5103e947 JB |
85 | btrfs_free_path(path); |
86 | return ret; | |
87 | } | |
88 | ||
f34f57a3 YZ |
89 | static int do_setxattr(struct btrfs_trans_handle *trans, |
90 | struct inode *inode, const char *name, | |
91 | const void *value, size_t size, int flags) | |
5103e947 JB |
92 | { |
93 | struct btrfs_dir_item *di; | |
94 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5103e947 | 95 | struct btrfs_path *path; |
f34f57a3 YZ |
96 | size_t name_len = strlen(name); |
97 | int ret = 0; | |
98 | ||
99 | if (name_len + size > BTRFS_MAX_XATTR_SIZE(root)) | |
100 | return -ENOSPC; | |
5103e947 JB |
101 | |
102 | path = btrfs_alloc_path(); | |
95819c05 | 103 | if (!path) |
5103e947 | 104 | return -ENOMEM; |
5103e947 | 105 | |
fa09200b JB |
106 | if (flags & XATTR_REPLACE) { |
107 | di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name, | |
108 | name_len, -1); | |
109 | if (IS_ERR(di)) { | |
110 | ret = PTR_ERR(di); | |
111 | goto out; | |
112 | } else if (!di) { | |
113 | ret = -ENODATA; | |
5103e947 JB |
114 | goto out; |
115 | } | |
5103e947 | 116 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
fa09200b JB |
117 | if (ret) |
118 | goto out; | |
b3b4aa74 | 119 | btrfs_release_path(path); |
4815053a DS |
120 | |
121 | /* | |
122 | * remove the attribute | |
123 | */ | |
124 | if (!value) | |
125 | goto out; | |
01e6deb2 LB |
126 | } else { |
127 | di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), | |
128 | name, name_len, 0); | |
129 | if (IS_ERR(di)) { | |
130 | ret = PTR_ERR(di); | |
131 | goto out; | |
132 | } | |
133 | if (!di && !value) | |
134 | goto out; | |
135 | btrfs_release_path(path); | |
fa09200b | 136 | } |
5103e947 | 137 | |
fa09200b JB |
138 | again: |
139 | ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode), | |
140 | name, name_len, value, size); | |
ed3ee9f4 JB |
141 | /* |
142 | * If we're setting an xattr to a new value but the new value is say | |
143 | * exactly BTRFS_MAX_XATTR_SIZE, we could end up with EOVERFLOW getting | |
144 | * back from split_leaf. This is because it thinks we'll be extending | |
145 | * the existing item size, but we're asking for enough space to add the | |
146 | * item itself. So if we get EOVERFLOW just set ret to EEXIST and let | |
147 | * the rest of the function figure it out. | |
148 | */ | |
149 | if (ret == -EOVERFLOW) | |
150 | ret = -EEXIST; | |
151 | ||
fa09200b JB |
152 | if (ret == -EEXIST) { |
153 | if (flags & XATTR_CREATE) | |
5103e947 | 154 | goto out; |
fa09200b JB |
155 | /* |
156 | * We can't use the path we already have since we won't have the | |
157 | * proper locking for a delete, so release the path and | |
158 | * re-lookup to delete the thing. | |
159 | */ | |
b3b4aa74 | 160 | btrfs_release_path(path); |
fa09200b JB |
161 | di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), |
162 | name, name_len, -1); | |
163 | if (IS_ERR(di)) { | |
164 | ret = PTR_ERR(di); | |
165 | goto out; | |
166 | } else if (!di) { | |
167 | /* Shouldn't happen but just in case... */ | |
168 | btrfs_release_path(path); | |
169 | goto again; | |
170 | } | |
33268eaf | 171 | |
fa09200b JB |
172 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
173 | if (ret) | |
33268eaf | 174 | goto out; |
fa09200b JB |
175 | |
176 | /* | |
177 | * We have a value to set, so go back and try to insert it now. | |
178 | */ | |
179 | if (value) { | |
180 | btrfs_release_path(path); | |
181 | goto again; | |
33268eaf | 182 | } |
5103e947 | 183 | } |
f34f57a3 YZ |
184 | out: |
185 | btrfs_free_path(path); | |
186 | return ret; | |
187 | } | |
188 | ||
4815053a DS |
189 | /* |
190 | * @value: "" makes the attribute to empty, NULL removes it | |
191 | */ | |
f34f57a3 YZ |
192 | int __btrfs_setxattr(struct btrfs_trans_handle *trans, |
193 | struct inode *inode, const char *name, | |
194 | const void *value, size_t size, int flags) | |
195 | { | |
196 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
197 | int ret; | |
198 | ||
199 | if (trans) | |
200 | return do_setxattr(trans, inode, name, value, size, flags); | |
201 | ||
a22285a6 YZ |
202 | trans = btrfs_start_transaction(root, 2); |
203 | if (IS_ERR(trans)) | |
204 | return PTR_ERR(trans); | |
5103e947 | 205 | |
f34f57a3 YZ |
206 | ret = do_setxattr(trans, inode, name, value, size, flags); |
207 | if (ret) | |
208 | goto out; | |
209 | ||
0c4d2d95 | 210 | inode_inc_iversion(inode); |
f34f57a3 | 211 | inode->i_ctime = CURRENT_TIME; |
e9976151 | 212 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
f34f57a3 YZ |
213 | ret = btrfs_update_inode(trans, root, inode); |
214 | BUG_ON(ret); | |
215 | out: | |
7ad85bb7 | 216 | btrfs_end_transaction(trans, root); |
5103e947 JB |
217 | return ret; |
218 | } | |
219 | ||
220 | ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
221 | { | |
222 | struct btrfs_key key, found_key; | |
223 | struct inode *inode = dentry->d_inode; | |
224 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
225 | struct btrfs_path *path; | |
5103e947 JB |
226 | struct extent_buffer *leaf; |
227 | struct btrfs_dir_item *di; | |
2e6a0035 | 228 | int ret = 0, slot; |
eaa47d86 | 229 | size_t total_size = 0, size_left = size; |
5103e947 | 230 | unsigned long name_ptr; |
eaa47d86 | 231 | size_t name_len; |
5103e947 JB |
232 | |
233 | /* | |
234 | * ok we want all objects associated with this id. | |
235 | * NOTE: we set key.offset = 0; because we want to start with the | |
236 | * first xattr that we find and walk forward | |
237 | */ | |
33345d01 | 238 | key.objectid = btrfs_ino(inode); |
5103e947 JB |
239 | btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); |
240 | key.offset = 0; | |
241 | ||
242 | path = btrfs_alloc_path(); | |
5103e947 JB |
243 | if (!path) |
244 | return -ENOMEM; | |
1caf9342 | 245 | path->reada = 2; |
5103e947 | 246 | |
5103e947 JB |
247 | /* search for our xattrs */ |
248 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
249 | if (ret < 0) | |
250 | goto err; | |
2e6a0035 | 251 | |
5103e947 JB |
252 | while (1) { |
253 | leaf = path->nodes[0]; | |
5103e947 JB |
254 | slot = path->slots[0]; |
255 | ||
256 | /* this is where we start walking through the path */ | |
2e6a0035 | 257 | if (slot >= btrfs_header_nritems(leaf)) { |
5103e947 JB |
258 | /* |
259 | * if we've reached the last slot in this leaf we need | |
260 | * to go to the next leaf and reset everything | |
261 | */ | |
2e6a0035 LZ |
262 | ret = btrfs_next_leaf(root, path); |
263 | if (ret < 0) | |
264 | goto err; | |
265 | else if (ret > 0) | |
266 | break; | |
267 | continue; | |
5103e947 | 268 | } |
5103e947 | 269 | |
5103e947 JB |
270 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
271 | ||
272 | /* check to make sure this item is what we want */ | |
273 | if (found_key.objectid != key.objectid) | |
274 | break; | |
275 | if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) | |
276 | break; | |
277 | ||
278 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
22a94d44 | 279 | if (verify_dir_item(root, leaf, di)) |
db2254bc | 280 | goto next; |
5103e947 | 281 | |
eaa47d86 CH |
282 | name_len = btrfs_dir_name_len(leaf, di); |
283 | total_size += name_len + 1; | |
5103e947 JB |
284 | |
285 | /* we are just looking for how big our buffer needs to be */ | |
286 | if (!size) | |
2e6a0035 | 287 | goto next; |
5103e947 | 288 | |
eaa47d86 | 289 | if (!buffer || (name_len + 1) > size_left) { |
5103e947 | 290 | ret = -ERANGE; |
b16281c3 | 291 | goto err; |
5103e947 JB |
292 | } |
293 | ||
eaa47d86 CH |
294 | name_ptr = (unsigned long)(di + 1); |
295 | read_extent_buffer(leaf, buffer, name_ptr, name_len); | |
296 | buffer[name_len] = '\0'; | |
297 | ||
298 | size_left -= name_len + 1; | |
299 | buffer += name_len + 1; | |
2e6a0035 LZ |
300 | next: |
301 | path->slots[0]++; | |
5103e947 JB |
302 | } |
303 | ret = total_size; | |
304 | ||
305 | err: | |
5103e947 JB |
306 | btrfs_free_path(path); |
307 | ||
308 | return ret; | |
309 | } | |
310 | ||
5103e947 | 311 | /* |
95819c05 CH |
312 | * List of handlers for synthetic system.* attributes. All real ondisk |
313 | * attributes are handled directly. | |
314 | */ | |
f01cbd3f | 315 | const struct xattr_handler *btrfs_xattr_handlers[] = { |
0eda294d | 316 | #ifdef CONFIG_BTRFS_FS_POSIX_ACL |
95819c05 CH |
317 | &btrfs_xattr_acl_access_handler, |
318 | &btrfs_xattr_acl_default_handler, | |
319 | #endif | |
320 | NULL, | |
321 | }; | |
322 | ||
323 | /* | |
324 | * Check if the attribute is in a supported namespace. | |
325 | * | |
326 | * This applied after the check for the synthetic attributes in the system | |
327 | * namespace. | |
5103e947 | 328 | */ |
95819c05 CH |
329 | static bool btrfs_is_valid_xattr(const char *name) |
330 | { | |
d397712b CM |
331 | return !strncmp(name, XATTR_SECURITY_PREFIX, |
332 | XATTR_SECURITY_PREFIX_LEN) || | |
95819c05 CH |
333 | !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) || |
334 | !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || | |
63541927 FDBM |
335 | !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) || |
336 | !strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN); | |
69a32ac5 CM |
337 | } |
338 | ||
95819c05 CH |
339 | ssize_t btrfs_getxattr(struct dentry *dentry, const char *name, |
340 | void *buffer, size_t size) | |
341 | { | |
342 | /* | |
343 | * If this is a request for a synthetic attribute in the system.* | |
344 | * namespace use the generic infrastructure to resolve a handler | |
345 | * for it via sb->s_xattr. | |
346 | */ | |
347 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
348 | return generic_getxattr(dentry, name, buffer, size); | |
5103e947 | 349 | |
95819c05 CH |
350 | if (!btrfs_is_valid_xattr(name)) |
351 | return -EOPNOTSUPP; | |
352 | return __btrfs_getxattr(dentry->d_inode, name, buffer, size); | |
353 | } | |
5103e947 | 354 | |
95819c05 CH |
355 | int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
356 | size_t size, int flags) | |
357 | { | |
b83cc969 LZ |
358 | struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root; |
359 | ||
360 | /* | |
361 | * The permission on security.* and system.* is not checked | |
362 | * in permission(). | |
363 | */ | |
364 | if (btrfs_root_readonly(root)) | |
365 | return -EROFS; | |
366 | ||
95819c05 CH |
367 | /* |
368 | * If this is a request for a synthetic attribute in the system.* | |
369 | * namespace use the generic infrastructure to resolve a handler | |
370 | * for it via sb->s_xattr. | |
371 | */ | |
372 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
373 | return generic_setxattr(dentry, name, value, size, flags); | |
5103e947 | 374 | |
95819c05 CH |
375 | if (!btrfs_is_valid_xattr(name)) |
376 | return -EOPNOTSUPP; | |
5103e947 | 377 | |
63541927 FDBM |
378 | if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) |
379 | return btrfs_set_prop(dentry->d_inode, name, | |
380 | value, size, flags); | |
381 | ||
95819c05 CH |
382 | if (size == 0) |
383 | value = ""; /* empty EA, do not remove */ | |
f34f57a3 YZ |
384 | |
385 | return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size, | |
386 | flags); | |
95819c05 CH |
387 | } |
388 | ||
389 | int btrfs_removexattr(struct dentry *dentry, const char *name) | |
390 | { | |
b83cc969 LZ |
391 | struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root; |
392 | ||
393 | /* | |
394 | * The permission on security.* and system.* is not checked | |
395 | * in permission(). | |
396 | */ | |
397 | if (btrfs_root_readonly(root)) | |
398 | return -EROFS; | |
399 | ||
95819c05 CH |
400 | /* |
401 | * If this is a request for a synthetic attribute in the system.* | |
402 | * namespace use the generic infrastructure to resolve a handler | |
403 | * for it via sb->s_xattr. | |
404 | */ | |
405 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
406 | return generic_removexattr(dentry, name); | |
407 | ||
408 | if (!btrfs_is_valid_xattr(name)) | |
409 | return -EOPNOTSUPP; | |
f34f57a3 | 410 | |
63541927 FDBM |
411 | if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) |
412 | return btrfs_set_prop(dentry->d_inode, name, | |
413 | NULL, 0, XATTR_REPLACE); | |
414 | ||
f34f57a3 YZ |
415 | return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0, |
416 | XATTR_REPLACE); | |
95819c05 | 417 | } |
0279b4cd | 418 | |
48a3b636 ES |
419 | static int btrfs_initxattrs(struct inode *inode, |
420 | const struct xattr *xattr_array, void *fs_info) | |
0279b4cd | 421 | { |
9d8f13ba MZ |
422 | const struct xattr *xattr; |
423 | struct btrfs_trans_handle *trans = fs_info; | |
0279b4cd | 424 | char *name; |
9d8f13ba | 425 | int err = 0; |
0279b4cd | 426 | |
9d8f13ba MZ |
427 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
428 | name = kmalloc(XATTR_SECURITY_PREFIX_LEN + | |
429 | strlen(xattr->name) + 1, GFP_NOFS); | |
430 | if (!name) { | |
431 | err = -ENOMEM; | |
432 | break; | |
433 | } | |
0279b4cd | 434 | strcpy(name, XATTR_SECURITY_PREFIX); |
9d8f13ba MZ |
435 | strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); |
436 | err = __btrfs_setxattr(trans, inode, name, | |
437 | xattr->value, xattr->value_len, 0); | |
0279b4cd | 438 | kfree(name); |
9d8f13ba MZ |
439 | if (err < 0) |
440 | break; | |
0279b4cd | 441 | } |
0279b4cd JO |
442 | return err; |
443 | } | |
9d8f13ba MZ |
444 | |
445 | int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, | |
446 | struct inode *inode, struct inode *dir, | |
447 | const struct qstr *qstr) | |
448 | { | |
449 | return security_inode_init_security(inode, dir, qstr, | |
450 | &btrfs_initxattrs, trans); | |
451 | } |