Btrfs: make btrfs acls selectable
[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>
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
31ssize_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);
07060404 48 if (!di) {
5103e947
JB
49 ret = -ENODATA;
50 goto out;
07060404
JB
51 } else if (IS_ERR(di)) {
52 ret = PTR_ERR(di);
53 goto out;
5103e947
JB
54 }
55
56 leaf = path->nodes[0];
57 /* if size is 0, that means we want the size of the attr */
58 if (!size) {
59 ret = btrfs_dir_data_len(leaf, di);
60 goto out;
61 }
62
63 /* now get the data out of our dir_item */
64 if (btrfs_dir_data_len(leaf, di) > size) {
65 ret = -ERANGE;
66 goto out;
67 }
07060404
JB
68
69 /*
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
75 */
5103e947
JB
76 data_ptr = (unsigned long)((char *)(di + 1) +
77 btrfs_dir_name_len(leaf, di));
78 read_extent_buffer(leaf, buffer, data_ptr,
3acd7ee8 79 btrfs_dir_data_len(leaf, di));
5103e947
JB
80 ret = btrfs_dir_data_len(leaf, di);
81
82out:
5103e947
JB
83 btrfs_free_path(path);
84 return ret;
85}
86
95819c05
CH
87int __btrfs_setxattr(struct inode *inode, const char *name,
88 const void *value, size_t size, int flags)
5103e947
JB
89{
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;
5103e947 94 int ret = 0, mod = 0;
5103e947
JB
95
96 path = btrfs_alloc_path();
95819c05 97 if (!path)
5103e947 98 return -ENOMEM;
5103e947 99
5103e947
JB
100 trans = btrfs_start_transaction(root, 1);
101 btrfs_set_trans_block_group(trans, inode);
102
103 /* first lets see if we already have this xattr */
104 di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
105 strlen(name), -1);
106 if (IS_ERR(di)) {
107 ret = PTR_ERR(di);
108 goto out;
109 }
110
111 /* ok we already have this xattr, lets remove it */
112 if (di) {
113 /* if we want create only exit */
114 if (flags & XATTR_CREATE) {
115 ret = -EEXIST;
116 goto out;
117 }
118
119 ret = btrfs_delete_one_dir_name(trans, root, path, di);
120 if (ret)
121 goto out;
122 btrfs_release_path(root, path);
123
124 /* if we don't have a value then we are removing the xattr */
125 if (!value) {
126 mod = 1;
127 goto out;
128 }
33268eaf
JB
129 } else {
130 btrfs_release_path(root, path);
131
132 if (flags & XATTR_REPLACE) {
133 /* we couldn't find the attr to replace */
134 ret = -ENODATA;
135 goto out;
136 }
5103e947
JB
137 }
138
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);
142 if (ret)
143 goto out;
144 mod = 1;
145
146out:
147 if (mod) {
148 inode->i_ctime = CURRENT_TIME;
149 ret = btrfs_update_inode(trans, root, inode);
150 }
151
152 btrfs_end_transaction(trans, root);
5103e947 153 btrfs_free_path(path);
5103e947
JB
154 return ret;
155}
156
157ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
158{
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;
5103e947 166 int ret = 0, slot, advance;
eaa47d86 167 size_t total_size = 0, size_left = size;
5103e947 168 unsigned long name_ptr;
eaa47d86 169 size_t name_len;
5103e947
JB
170 u32 nritems;
171
172 /*
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
176 */
177 key.objectid = inode->i_ino;
178 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
179 key.offset = 0;
180
181 path = btrfs_alloc_path();
5103e947
JB
182 if (!path)
183 return -ENOMEM;
1caf9342 184 path->reada = 2;
5103e947 185
5103e947
JB
186 /* search for our xattrs */
187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
188 if (ret < 0)
189 goto err;
5103e947
JB
190 advance = 0;
191 while (1) {
192 leaf = path->nodes[0];
193 nritems = btrfs_header_nritems(leaf);
194 slot = path->slots[0];
195
196 /* this is where we start walking through the path */
197 if (advance || slot >= nritems) {
198 /*
199 * if we've reached the last slot in this leaf we need
200 * to go to the next leaf and reset everything
201 */
202 if (slot >= nritems-1) {
203 ret = btrfs_next_leaf(root, path);
204 if (ret)
205 break;
206 leaf = path->nodes[0];
207 nritems = btrfs_header_nritems(leaf);
208 slot = path->slots[0];
209 } else {
210 /*
211 * just walking through the slots on this leaf
212 */
213 slot++;
214 path->slots[0]++;
215 }
216 }
217 advance = 1;
218
219 item = btrfs_item_nr(leaf, slot);
220 btrfs_item_key_to_cpu(leaf, &found_key, slot);
221
222 /* check to make sure this item is what we want */
223 if (found_key.objectid != key.objectid)
224 break;
225 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
226 break;
227
228 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
229
eaa47d86
CH
230 name_len = btrfs_dir_name_len(leaf, di);
231 total_size += name_len + 1;
5103e947
JB
232
233 /* we are just looking for how big our buffer needs to be */
234 if (!size)
235 continue;
236
eaa47d86 237 if (!buffer || (name_len + 1) > size_left) {
5103e947 238 ret = -ERANGE;
b16281c3 239 goto err;
5103e947
JB
240 }
241
eaa47d86
CH
242 name_ptr = (unsigned long)(di + 1);
243 read_extent_buffer(leaf, buffer, name_ptr, name_len);
244 buffer[name_len] = '\0';
245
246 size_left -= name_len + 1;
247 buffer += name_len + 1;
5103e947
JB
248 }
249 ret = total_size;
250
251err:
5103e947
JB
252 btrfs_free_path(path);
253
254 return ret;
255}
256
5103e947 257/*
95819c05
CH
258 * List of handlers for synthetic system.* attributes. All real ondisk
259 * attributes are handled directly.
260 */
261struct xattr_handler *btrfs_xattr_handlers[] = {
262#ifdef CONFIG_FS_POSIX_ACL
263 &btrfs_xattr_acl_access_handler,
264 &btrfs_xattr_acl_default_handler,
265#endif
266 NULL,
267};
268
269/*
270 * Check if the attribute is in a supported namespace.
271 *
272 * This applied after the check for the synthetic attributes in the system
273 * namespace.
5103e947 274 */
95819c05
CH
275static bool btrfs_is_valid_xattr(const char *name)
276{
d397712b
CM
277 return !strncmp(name, XATTR_SECURITY_PREFIX,
278 XATTR_SECURITY_PREFIX_LEN) ||
95819c05
CH
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);
69a32ac5
CM
282}
283
95819c05
CH
284ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
285 void *buffer, size_t size)
286{
287 /*
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.
291 */
292 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
293 return generic_getxattr(dentry, name, buffer, size);
5103e947 294
95819c05
CH
295 if (!btrfs_is_valid_xattr(name))
296 return -EOPNOTSUPP;
297 return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
298}
5103e947 299
95819c05
CH
300int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
301 size_t size, int flags)
302{
303 /*
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.
307 */
308 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
309 return generic_setxattr(dentry, name, value, size, flags);
5103e947 310
95819c05
CH
311 if (!btrfs_is_valid_xattr(name))
312 return -EOPNOTSUPP;
5103e947 313
95819c05
CH
314 if (size == 0)
315 value = ""; /* empty EA, do not remove */
316 return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
317}
318
319int btrfs_removexattr(struct dentry *dentry, const char *name)
320{
321 /*
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.
325 */
326 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
327 return generic_removexattr(dentry, name);
328
329 if (!btrfs_is_valid_xattr(name))
330 return -EOPNOTSUPP;
331 return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
332}
This page took 0.055183 seconds and 5 git commands to generate.