btrfs: use generic posix ACL infrastructure
[deliverable/linux.git] / fs / btrfs / xattr.c
CommitLineData
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>
996a710d 25#include <linux/posix_acl_xattr.h>
5103e947
JB
26#include "ctree.h"
27#include "btrfs_inode.h"
28#include "transaction.h"
29#include "xattr.h"
30#include "disk-io.h"
33268eaf 31
5103e947 32
95819c05
CH
33ssize_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
84out:
5103e947
JB
85 btrfs_free_path(path);
86 return ret;
87}
88
f34f57a3
YZ
89static 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
138again:
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
184out:
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
192int __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);
215out:
7ad85bb7 216 btrfs_end_transaction(trans, root);
5103e947
JB
217 return ret;
218}
219
220ssize_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
300next:
301 path->slots[0]++;
5103e947
JB
302 }
303 ret = total_size;
304
305err:
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 315const struct xattr_handler *btrfs_xattr_handlers[] = {
0eda294d 316#ifdef CONFIG_BTRFS_FS_POSIX_ACL
996a710d
CH
317 &posix_acl_access_xattr_handler,
318 &posix_acl_default_xattr_handler,
95819c05
CH
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
329static 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) ||
335 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
69a32ac5
CM
336}
337
95819c05
CH
338ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
339 void *buffer, size_t size)
340{
341 /*
342 * If this is a request for a synthetic attribute in the system.*
343 * namespace use the generic infrastructure to resolve a handler
344 * for it via sb->s_xattr.
345 */
346 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
347 return generic_getxattr(dentry, name, buffer, size);
5103e947 348
95819c05
CH
349 if (!btrfs_is_valid_xattr(name))
350 return -EOPNOTSUPP;
351 return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
352}
5103e947 353
95819c05
CH
354int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
355 size_t size, int flags)
356{
b83cc969
LZ
357 struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
358
359 /*
360 * The permission on security.* and system.* is not checked
361 * in permission().
362 */
363 if (btrfs_root_readonly(root))
364 return -EROFS;
365
95819c05
CH
366 /*
367 * If this is a request for a synthetic attribute in the system.*
368 * namespace use the generic infrastructure to resolve a handler
369 * for it via sb->s_xattr.
370 */
371 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
372 return generic_setxattr(dentry, name, value, size, flags);
5103e947 373
95819c05
CH
374 if (!btrfs_is_valid_xattr(name))
375 return -EOPNOTSUPP;
5103e947 376
95819c05
CH
377 if (size == 0)
378 value = ""; /* empty EA, do not remove */
f34f57a3
YZ
379
380 return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
381 flags);
95819c05
CH
382}
383
384int btrfs_removexattr(struct dentry *dentry, const char *name)
385{
b83cc969
LZ
386 struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
387
388 /*
389 * The permission on security.* and system.* is not checked
390 * in permission().
391 */
392 if (btrfs_root_readonly(root))
393 return -EROFS;
394
95819c05
CH
395 /*
396 * If this is a request for a synthetic attribute in the system.*
397 * namespace use the generic infrastructure to resolve a handler
398 * for it via sb->s_xattr.
399 */
400 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
401 return generic_removexattr(dentry, name);
402
403 if (!btrfs_is_valid_xattr(name))
404 return -EOPNOTSUPP;
f34f57a3
YZ
405
406 return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
407 XATTR_REPLACE);
95819c05 408}
0279b4cd 409
48a3b636
ES
410static int btrfs_initxattrs(struct inode *inode,
411 const struct xattr *xattr_array, void *fs_info)
0279b4cd 412{
9d8f13ba
MZ
413 const struct xattr *xattr;
414 struct btrfs_trans_handle *trans = fs_info;
0279b4cd 415 char *name;
9d8f13ba 416 int err = 0;
0279b4cd 417
9d8f13ba
MZ
418 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
419 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
420 strlen(xattr->name) + 1, GFP_NOFS);
421 if (!name) {
422 err = -ENOMEM;
423 break;
424 }
0279b4cd 425 strcpy(name, XATTR_SECURITY_PREFIX);
9d8f13ba
MZ
426 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
427 err = __btrfs_setxattr(trans, inode, name,
428 xattr->value, xattr->value_len, 0);
0279b4cd 429 kfree(name);
9d8f13ba
MZ
430 if (err < 0)
431 break;
0279b4cd 432 }
0279b4cd
JO
433 return err;
434}
9d8f13ba
MZ
435
436int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
437 struct inode *inode, struct inode *dir,
438 const struct qstr *qstr)
439{
440 return security_inode_init_security(inode, dir, qstr,
441 &btrfs_initxattrs, trans);
442}
This page took 0.351134 seconds and 5 git commands to generate.