]> Pileus Git - ~andy/linux/blob - fs/btrfs/xattr.c
Merge branch 'for-3.14' of git://linux-nfs.org/~bfields/linux
[~andy/linux] / fs / btrfs / xattr.c
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 <linux/security.h>
25 #include <linux/posix_acl_xattr.h>
26 #include "ctree.h"
27 #include "btrfs_inode.h"
28 #include "transaction.h"
29 #include "xattr.h"
30 #include "disk-io.h"
31
32
33 ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
34                                 void *buffer, size_t size)
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;
40         int ret = 0;
41         unsigned long data_ptr;
42
43         path = btrfs_alloc_path();
44         if (!path)
45                 return -ENOMEM;
46
47         /* lookup the xattr by name */
48         di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name,
49                                 strlen(name), 0);
50         if (!di) {
51                 ret = -ENODATA;
52                 goto out;
53         } else if (IS_ERR(di)) {
54                 ret = PTR_ERR(di);
55                 goto out;
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         }
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          */
78         data_ptr = (unsigned long)((char *)(di + 1) +
79                                    btrfs_dir_name_len(leaf, di));
80         read_extent_buffer(leaf, buffer, data_ptr,
81                            btrfs_dir_data_len(leaf, di));
82         ret = btrfs_dir_data_len(leaf, di);
83
84 out:
85         btrfs_free_path(path);
86         return ret;
87 }
88
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)
92 {
93         struct btrfs_dir_item *di;
94         struct btrfs_root *root = BTRFS_I(inode)->root;
95         struct btrfs_path *path;
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;
101
102         path = btrfs_alloc_path();
103         if (!path)
104                 return -ENOMEM;
105
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;
114                         goto out;
115                 }
116                 ret = btrfs_delete_one_dir_name(trans, root, path, di);
117                 if (ret)
118                         goto out;
119                 btrfs_release_path(path);
120
121                 /*
122                  * remove the attribute
123                  */
124                 if (!value)
125                         goto out;
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);
136         }
137
138 again:
139         ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
140                                       name, name_len, value, size);
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
152         if (ret == -EEXIST) {
153                 if (flags & XATTR_CREATE)
154                         goto out;
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                  */
160                 btrfs_release_path(path);
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                 }
171
172                 ret = btrfs_delete_one_dir_name(trans, root, path, di);
173                 if (ret)
174                         goto out;
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;
182                 }
183         }
184 out:
185         btrfs_free_path(path);
186         return ret;
187 }
188
189 /*
190  * @value: "" makes the attribute to empty, NULL removes it
191  */
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
202         trans = btrfs_start_transaction(root, 2);
203         if (IS_ERR(trans))
204                 return PTR_ERR(trans);
205
206         ret = do_setxattr(trans, inode, name, value, size, flags);
207         if (ret)
208                 goto out;
209
210         inode_inc_iversion(inode);
211         inode->i_ctime = CURRENT_TIME;
212         set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
213         ret = btrfs_update_inode(trans, root, inode);
214         BUG_ON(ret);
215 out:
216         btrfs_end_transaction(trans, root);
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;
226         struct extent_buffer *leaf;
227         struct btrfs_dir_item *di;
228         int ret = 0, slot;
229         size_t total_size = 0, size_left = size;
230         unsigned long name_ptr;
231         size_t name_len;
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          */
238         key.objectid = btrfs_ino(inode);
239         btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
240         key.offset = 0;
241
242         path = btrfs_alloc_path();
243         if (!path)
244                 return -ENOMEM;
245         path->reada = 2;
246
247         /* search for our xattrs */
248         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
249         if (ret < 0)
250                 goto err;
251
252         while (1) {
253                 leaf = path->nodes[0];
254                 slot = path->slots[0];
255
256                 /* this is where we start walking through the path */
257                 if (slot >= btrfs_header_nritems(leaf)) {
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                          */
262                         ret = btrfs_next_leaf(root, path);
263                         if (ret < 0)
264                                 goto err;
265                         else if (ret > 0)
266                                 break;
267                         continue;
268                 }
269
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);
279                 if (verify_dir_item(root, leaf, di))
280                         goto next;
281
282                 name_len = btrfs_dir_name_len(leaf, di);
283                 total_size += name_len + 1;
284
285                 /* we are just looking for how big our buffer needs to be */
286                 if (!size)
287                         goto next;
288
289                 if (!buffer || (name_len + 1) > size_left) {
290                         ret = -ERANGE;
291                         goto err;
292                 }
293
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;
300 next:
301                 path->slots[0]++;
302         }
303         ret = total_size;
304
305 err:
306         btrfs_free_path(path);
307
308         return ret;
309 }
310
311 /*
312  * List of handlers for synthetic system.* attributes.  All real ondisk
313  * attributes are handled directly.
314  */
315 const struct xattr_handler *btrfs_xattr_handlers[] = {
316 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
317         &posix_acl_access_xattr_handler,
318         &posix_acl_default_xattr_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.
328  */
329 static bool btrfs_is_valid_xattr(const char *name)
330 {
331         return !strncmp(name, XATTR_SECURITY_PREFIX,
332                         XATTR_SECURITY_PREFIX_LEN) ||
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);
336 }
337
338 ssize_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);
348
349         if (!btrfs_is_valid_xattr(name))
350                 return -EOPNOTSUPP;
351         return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
352 }
353
354 int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
355                    size_t size, int flags)
356 {
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
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);
373
374         if (!btrfs_is_valid_xattr(name))
375                 return -EOPNOTSUPP;
376
377         if (size == 0)
378                 value = "";  /* empty EA, do not remove */
379
380         return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
381                                 flags);
382 }
383
384 int btrfs_removexattr(struct dentry *dentry, const char *name)
385 {
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
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;
405
406         return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
407                                 XATTR_REPLACE);
408 }
409
410 static int btrfs_initxattrs(struct inode *inode,
411                             const struct xattr *xattr_array, void *fs_info)
412 {
413         const struct xattr *xattr;
414         struct btrfs_trans_handle *trans = fs_info;
415         char *name;
416         int err = 0;
417
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                 }
425                 strcpy(name, XATTR_SECURITY_PREFIX);
426                 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
427                 err = __btrfs_setxattr(trans, inode, name,
428                                        xattr->value, xattr->value_len, 0);
429                 kfree(name);
430                 if (err < 0)
431                         break;
432         }
433         return err;
434 }
435
436 int 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 }