Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5103e947 JB |
2 | /* |
3 | * Copyright (C) 2007 Red Hat. All rights reserved. | |
5103e947 JB |
4 | */ |
5 | ||
6 | #include <linux/init.h> | |
7 | #include <linux/fs.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/rwsem.h> | |
10 | #include <linux/xattr.h> | |
0279b4cd | 11 | #include <linux/security.h> |
996a710d | 12 | #include <linux/posix_acl_xattr.h> |
ae5e165d | 13 | #include <linux/iversion.h> |
827aa18e | 14 | #include <linux/sched/mm.h> |
5103e947 | 15 | #include "ctree.h" |
ec8eb376 JB |
16 | #include "fs.h" |
17 | #include "messages.h" | |
5103e947 JB |
18 | #include "btrfs_inode.h" |
19 | #include "transaction.h" | |
20 | #include "xattr.h" | |
21 | #include "disk-io.h" | |
63541927 | 22 | #include "props.h" |
5f5bc6b1 | 23 | #include "locking.h" |
07e81dc9 | 24 | #include "accessors.h" |
f2b39277 | 25 | #include "dir-item.h" |
33268eaf | 26 | |
2917f741 | 27 | int btrfs_getxattr(const struct inode *inode, const char *name, |
95819c05 | 28 | void *buffer, size_t size) |
5103e947 JB |
29 | { |
30 | struct btrfs_dir_item *di; | |
31 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
32 | struct btrfs_path *path; | |
33 | struct extent_buffer *leaf; | |
5103e947 JB |
34 | int ret = 0; |
35 | unsigned long data_ptr; | |
5103e947 JB |
36 | |
37 | path = btrfs_alloc_path(); | |
95819c05 | 38 | if (!path) |
5103e947 | 39 | return -ENOMEM; |
5103e947 | 40 | |
5103e947 | 41 | /* lookup the xattr by name */ |
f85b7379 DS |
42 | di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)), |
43 | name, strlen(name), 0); | |
07060404 | 44 | if (!di) { |
5103e947 JB |
45 | ret = -ENODATA; |
46 | goto out; | |
07060404 JB |
47 | } else if (IS_ERR(di)) { |
48 | ret = PTR_ERR(di); | |
49 | goto out; | |
5103e947 JB |
50 | } |
51 | ||
52 | leaf = path->nodes[0]; | |
53 | /* if size is 0, that means we want the size of the attr */ | |
54 | if (!size) { | |
55 | ret = btrfs_dir_data_len(leaf, di); | |
56 | goto out; | |
57 | } | |
58 | ||
59 | /* now get the data out of our dir_item */ | |
60 | if (btrfs_dir_data_len(leaf, di) > size) { | |
61 | ret = -ERANGE; | |
62 | goto out; | |
63 | } | |
07060404 JB |
64 | |
65 | /* | |
66 | * The way things are packed into the leaf is like this | |
67 | * |struct btrfs_dir_item|name|data| | |
68 | * where name is the xattr name, so security.foo, and data is the | |
69 | * content of the xattr. data_ptr points to the location in memory | |
70 | * where the data starts in the in memory leaf | |
71 | */ | |
5103e947 JB |
72 | data_ptr = (unsigned long)((char *)(di + 1) + |
73 | btrfs_dir_name_len(leaf, di)); | |
74 | read_extent_buffer(leaf, buffer, data_ptr, | |
3acd7ee8 | 75 | btrfs_dir_data_len(leaf, di)); |
5103e947 JB |
76 | ret = btrfs_dir_data_len(leaf, di); |
77 | ||
78 | out: | |
5103e947 JB |
79 | btrfs_free_path(path); |
80 | return ret; | |
81 | } | |
82 | ||
3e125a74 AJ |
83 | int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, |
84 | const char *name, const void *value, size_t size, int flags) | |
5103e947 | 85 | { |
5f5bc6b1 | 86 | struct btrfs_dir_item *di = NULL; |
5103e947 | 87 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5103e947 | 88 | struct btrfs_path *path; |
f34f57a3 YZ |
89 | size_t name_len = strlen(name); |
90 | int ret = 0; | |
91 | ||
04e6863b AJ |
92 | ASSERT(trans); |
93 | ||
da17066c | 94 | if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info)) |
f34f57a3 | 95 | return -ENOSPC; |
5103e947 JB |
96 | |
97 | path = btrfs_alloc_path(); | |
95819c05 | 98 | if (!path) |
5103e947 | 99 | return -ENOMEM; |
5f5bc6b1 FM |
100 | path->skip_release_on_error = 1; |
101 | ||
102 | if (!value) { | |
f85b7379 DS |
103 | di = btrfs_lookup_xattr(trans, root, path, |
104 | btrfs_ino(BTRFS_I(inode)), name, name_len, -1); | |
5f5bc6b1 FM |
105 | if (!di && (flags & XATTR_REPLACE)) |
106 | ret = -ENODATA; | |
5cdf83ed FM |
107 | else if (IS_ERR(di)) |
108 | ret = PTR_ERR(di); | |
5f5bc6b1 FM |
109 | else if (di) |
110 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
111 | goto out; | |
112 | } | |
5103e947 | 113 | |
5f5bc6b1 FM |
114 | /* |
115 | * For a replace we can't just do the insert blindly. | |
116 | * Do a lookup first (read-only btrfs_search_slot), and return if xattr | |
117 | * doesn't exist. If it exists, fall down below to the insert/replace | |
118 | * path - we can't race with a concurrent xattr delete, because the VFS | |
119 | * locks the inode's i_mutex before calling setxattr or removexattr. | |
120 | */ | |
fa09200b | 121 | if (flags & XATTR_REPLACE) { |
1b6e068a | 122 | btrfs_assert_inode_locked(BTRFS_I(inode)); |
f85b7379 DS |
123 | di = btrfs_lookup_xattr(NULL, root, path, |
124 | btrfs_ino(BTRFS_I(inode)), name, name_len, 0); | |
5cdf83ed | 125 | if (!di) |
fa09200b | 126 | ret = -ENODATA; |
5cdf83ed FM |
127 | else if (IS_ERR(di)) |
128 | ret = PTR_ERR(di); | |
129 | if (ret) | |
5103e947 | 130 | goto out; |
b3b4aa74 | 131 | btrfs_release_path(path); |
5f5bc6b1 FM |
132 | di = NULL; |
133 | } | |
4815053a | 134 | |
4a0cc7ca | 135 | ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)), |
5f5bc6b1 FM |
136 | name, name_len, value, size); |
137 | if (ret == -EOVERFLOW) { | |
4815053a | 138 | /* |
5f5bc6b1 FM |
139 | * We have an existing item in a leaf, split_leaf couldn't |
140 | * expand it. That item might have or not a dir_item that | |
141 | * matches our target xattr, so lets check. | |
4815053a | 142 | */ |
5f5bc6b1 | 143 | ret = 0; |
49d0c642 | 144 | btrfs_assert_tree_write_locked(path->nodes[0]); |
8c7cd2b6 | 145 | di = btrfs_match_dir_item_name(path, name, name_len); |
5f5bc6b1 FM |
146 | if (!di && !(flags & XATTR_REPLACE)) { |
147 | ret = -ENOSPC; | |
01e6deb2 LB |
148 | goto out; |
149 | } | |
5f5bc6b1 FM |
150 | } else if (ret == -EEXIST) { |
151 | ret = 0; | |
8c7cd2b6 | 152 | di = btrfs_match_dir_item_name(path, name, name_len); |
5f5bc6b1 FM |
153 | ASSERT(di); /* logic error */ |
154 | } else if (ret) { | |
155 | goto out; | |
fa09200b | 156 | } |
5103e947 | 157 | |
5f5bc6b1 | 158 | if (di && (flags & XATTR_CREATE)) { |
ed3ee9f4 | 159 | ret = -EEXIST; |
5f5bc6b1 FM |
160 | goto out; |
161 | } | |
ed3ee9f4 | 162 | |
5f5bc6b1 | 163 | if (di) { |
fa09200b | 164 | /* |
5f5bc6b1 FM |
165 | * We're doing a replace, and it must be atomic, that is, at |
166 | * any point in time we have either the old or the new xattr | |
167 | * value in the tree. We don't want readers (getxattr and | |
168 | * listxattrs) to miss a value, this is specially important | |
169 | * for ACLs. | |
fa09200b | 170 | */ |
5f5bc6b1 FM |
171 | const int slot = path->slots[0]; |
172 | struct extent_buffer *leaf = path->nodes[0]; | |
173 | const u16 old_data_len = btrfs_dir_data_len(leaf, di); | |
3212fa14 | 174 | const u32 item_size = btrfs_item_size(leaf, slot); |
5f5bc6b1 | 175 | const u32 data_size = sizeof(*di) + name_len + size; |
5f5bc6b1 FM |
176 | unsigned long data_ptr; |
177 | char *ptr; | |
178 | ||
179 | if (size > old_data_len) { | |
e902baac | 180 | if (btrfs_leaf_free_space(leaf) < |
5f5bc6b1 FM |
181 | (size - old_data_len)) { |
182 | ret = -ENOSPC; | |
183 | goto out; | |
184 | } | |
fa09200b | 185 | } |
33268eaf | 186 | |
5f5bc6b1 FM |
187 | if (old_data_len + name_len + sizeof(*di) == item_size) { |
188 | /* No other xattrs packed in the same leaf item. */ | |
189 | if (size > old_data_len) | |
50564b65 | 190 | btrfs_extend_item(trans, path, size - old_data_len); |
5f5bc6b1 | 191 | else if (size < old_data_len) |
50564b65 | 192 | btrfs_truncate_item(trans, path, data_size, 1); |
5f5bc6b1 FM |
193 | } else { |
194 | /* There are other xattrs packed in the same item. */ | |
195 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
196 | if (ret) | |
197 | goto out; | |
50564b65 | 198 | btrfs_extend_item(trans, path, data_size); |
5f5bc6b1 | 199 | } |
fa09200b | 200 | |
5f5bc6b1 | 201 | ptr = btrfs_item_ptr(leaf, slot, char); |
3212fa14 | 202 | ptr += btrfs_item_size(leaf, slot) - data_size; |
5f5bc6b1 FM |
203 | di = (struct btrfs_dir_item *)ptr; |
204 | btrfs_set_dir_data_len(leaf, di, size); | |
205 | data_ptr = ((unsigned long)(di + 1)) + name_len; | |
206 | write_extent_buffer(leaf, value, data_ptr, size); | |
5f5bc6b1 | 207 | } else { |
fa09200b | 208 | /* |
5f5bc6b1 FM |
209 | * Insert, and we had space for the xattr, so path->slots[0] is |
210 | * where our xattr dir_item is and btrfs_insert_xattr_item() | |
211 | * filled it. | |
fa09200b | 212 | */ |
5103e947 | 213 | } |
f34f57a3 YZ |
214 | out: |
215 | btrfs_free_path(path); | |
f2f121ab | 216 | if (!ret) { |
3763771c FM |
217 | set_bit(BTRFS_INODE_COPY_EVERYTHING, |
218 | &BTRFS_I(inode)->runtime_flags); | |
f2f121ab FM |
219 | clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags); |
220 | } | |
f34f57a3 YZ |
221 | return ret; |
222 | } | |
223 | ||
4815053a DS |
224 | /* |
225 | * @value: "" makes the attribute to empty, NULL removes it | |
226 | */ | |
e3de9b15 | 227 | int btrfs_setxattr_trans(struct inode *inode, const char *name, |
cac237ae | 228 | const void *value, size_t size, int flags) |
f34f57a3 YZ |
229 | { |
230 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e3de9b15 | 231 | struct btrfs_trans_handle *trans; |
fd57a98d | 232 | const bool start_trans = (current->journal_info == NULL); |
f34f57a3 YZ |
233 | int ret; |
234 | ||
fd57a98d FM |
235 | if (start_trans) { |
236 | /* | |
237 | * 1 unit for inserting/updating/deleting the xattr | |
238 | * 1 unit for the inode item update | |
239 | */ | |
240 | trans = btrfs_start_transaction(root, 2); | |
241 | if (IS_ERR(trans)) | |
242 | return PTR_ERR(trans); | |
243 | } else { | |
244 | /* | |
245 | * This can happen when smack is enabled and a directory is being | |
246 | * created. It happens through d_instantiate_new(), which calls | |
247 | * smack_d_instantiate(), which in turn calls __vfs_setxattr() to | |
248 | * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the | |
249 | * inode. We have already reserved space for the xattr and inode | |
250 | * update at btrfs_mkdir(), so just use the transaction handle. | |
251 | * We don't join or start a transaction, as that will reset the | |
252 | * block_rsv of the handle and trigger a warning for the start | |
253 | * case. | |
254 | */ | |
255 | ASSERT(strncmp(name, XATTR_SECURITY_PREFIX, | |
256 | XATTR_SECURITY_PREFIX_LEN) == 0); | |
257 | trans = current->journal_info; | |
258 | } | |
5103e947 | 259 | |
2d74fa3e | 260 | ret = btrfs_setxattr(trans, inode, name, value, size, flags); |
f34f57a3 YZ |
261 | if (ret) |
262 | goto out; | |
263 | ||
0c4d2d95 | 264 | inode_inc_iversion(inode); |
2a9462de | 265 | inode_set_ctime_current(inode); |
8b9d0322 | 266 | ret = btrfs_update_inode(trans, BTRFS_I(inode)); |
193b4e83 FM |
267 | if (ret) |
268 | btrfs_abort_transaction(trans, ret); | |
f34f57a3 | 269 | out: |
fd57a98d FM |
270 | if (start_trans) |
271 | btrfs_end_transaction(trans); | |
5103e947 JB |
272 | return ret; |
273 | } | |
274 | ||
275 | ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
276 | { | |
184b3d19 | 277 | struct btrfs_key found_key; |
daac7ba6 | 278 | struct btrfs_key key; |
2b0143b5 | 279 | struct inode *inode = d_inode(dentry); |
5103e947 JB |
280 | struct btrfs_root *root = BTRFS_I(inode)->root; |
281 | struct btrfs_path *path; | |
184b3d19 | 282 | int iter_ret = 0; |
daac7ba6 | 283 | int ret = 0; |
eaa47d86 | 284 | size_t total_size = 0, size_left = size; |
5103e947 JB |
285 | |
286 | /* | |
287 | * ok we want all objects associated with this id. | |
288 | * NOTE: we set key.offset = 0; because we want to start with the | |
289 | * first xattr that we find and walk forward | |
290 | */ | |
4a0cc7ca | 291 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
962a298f | 292 | key.type = BTRFS_XATTR_ITEM_KEY; |
5103e947 JB |
293 | key.offset = 0; |
294 | ||
295 | path = btrfs_alloc_path(); | |
5103e947 JB |
296 | if (!path) |
297 | return -ENOMEM; | |
e4058b54 | 298 | path->reada = READA_FORWARD; |
5103e947 | 299 | |
5103e947 | 300 | /* search for our xattrs */ |
184b3d19 | 301 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { |
daac7ba6 FM |
302 | struct extent_buffer *leaf; |
303 | int slot; | |
304 | struct btrfs_dir_item *di; | |
daac7ba6 FM |
305 | u32 item_size; |
306 | u32 cur; | |
307 | ||
5103e947 | 308 | leaf = path->nodes[0]; |
5103e947 JB |
309 | slot = path->slots[0]; |
310 | ||
5103e947 JB |
311 | /* check to make sure this item is what we want */ |
312 | if (found_key.objectid != key.objectid) | |
313 | break; | |
f1cd1f0b | 314 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
5103e947 | 315 | break; |
f1cd1f0b | 316 | if (found_key.type < BTRFS_XATTR_ITEM_KEY) |
184b3d19 | 317 | continue; |
5103e947 JB |
318 | |
319 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
3212fa14 | 320 | item_size = btrfs_item_size(leaf, slot); |
daac7ba6 FM |
321 | cur = 0; |
322 | while (cur < item_size) { | |
323 | u16 name_len = btrfs_dir_name_len(leaf, di); | |
324 | u16 data_len = btrfs_dir_data_len(leaf, di); | |
325 | u32 this_len = sizeof(*di) + name_len + data_len; | |
326 | unsigned long name_ptr = (unsigned long)(di + 1); | |
327 | ||
daac7ba6 FM |
328 | total_size += name_len + 1; |
329 | /* | |
330 | * We are just looking for how big our buffer needs to | |
331 | * be. | |
332 | */ | |
333 | if (!size) | |
334 | goto next; | |
5103e947 | 335 | |
daac7ba6 | 336 | if (!buffer || (name_len + 1) > size_left) { |
184b3d19 GN |
337 | iter_ret = -ERANGE; |
338 | break; | |
daac7ba6 | 339 | } |
5103e947 | 340 | |
daac7ba6 FM |
341 | read_extent_buffer(leaf, buffer, name_ptr, name_len); |
342 | buffer[name_len] = '\0'; | |
eaa47d86 | 343 | |
daac7ba6 FM |
344 | size_left -= name_len + 1; |
345 | buffer += name_len + 1; | |
2e6a0035 | 346 | next: |
daac7ba6 FM |
347 | cur += this_len; |
348 | di = (struct btrfs_dir_item *)((char *)di + this_len); | |
349 | } | |
5103e947 | 350 | } |
5103e947 | 351 | |
184b3d19 GN |
352 | if (iter_ret < 0) |
353 | ret = iter_ret; | |
354 | else | |
355 | ret = total_size; | |
356 | ||
5103e947 JB |
357 | btrfs_free_path(path); |
358 | ||
359 | return ret; | |
360 | } | |
361 | ||
9172abbc | 362 | static int btrfs_xattr_handler_get(const struct xattr_handler *handler, |
b296821a AV |
363 | struct dentry *unused, struct inode *inode, |
364 | const char *name, void *buffer, size_t size) | |
95819c05 | 365 | { |
9172abbc | 366 | name = xattr_full_name(handler, name); |
7852781d | 367 | return btrfs_getxattr(inode, name, buffer, size); |
69a32ac5 CM |
368 | } |
369 | ||
9172abbc | 370 | static int btrfs_xattr_handler_set(const struct xattr_handler *handler, |
39f60c1c | 371 | struct mnt_idmap *idmap, |
59301226 AV |
372 | struct dentry *unused, struct inode *inode, |
373 | const char *name, const void *buffer, | |
374 | size_t size, int flags) | |
95819c05 | 375 | { |
b5111127 GR |
376 | if (btrfs_root_readonly(BTRFS_I(inode)->root)) |
377 | return -EROFS; | |
378 | ||
9172abbc | 379 | name = xattr_full_name(handler, name); |
e3de9b15 | 380 | return btrfs_setxattr_trans(inode, name, buffer, size, flags); |
9172abbc | 381 | } |
5103e947 | 382 | |
ed9b50a1 JB |
383 | static int btrfs_xattr_handler_get_security(const struct xattr_handler *handler, |
384 | struct dentry *unused, | |
385 | struct inode *inode, | |
386 | const char *name, void *buffer, | |
387 | size_t size) | |
388 | { | |
389 | int ret; | |
390 | bool is_cap = false; | |
391 | ||
392 | name = xattr_full_name(handler, name); | |
393 | ||
394 | /* | |
395 | * security.capability doesn't cache the results, so calls into us | |
396 | * constantly to see if there's a capability xattr. Cache the result | |
397 | * here in order to avoid wasting time doing lookups for xattrs we know | |
398 | * don't exist. | |
399 | */ | |
400 | if (strcmp(name, XATTR_NAME_CAPS) == 0) { | |
401 | is_cap = true; | |
402 | if (test_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags)) | |
403 | return -ENODATA; | |
404 | } | |
405 | ||
406 | ret = btrfs_getxattr(inode, name, buffer, size); | |
407 | if (ret == -ENODATA && is_cap) | |
408 | set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags); | |
409 | return ret; | |
410 | } | |
411 | ||
412 | static int btrfs_xattr_handler_set_security(const struct xattr_handler *handler, | |
413 | struct mnt_idmap *idmap, | |
414 | struct dentry *unused, | |
415 | struct inode *inode, | |
416 | const char *name, | |
417 | const void *buffer, | |
418 | size_t size, int flags) | |
419 | { | |
420 | if (btrfs_root_readonly(BTRFS_I(inode)->root)) | |
421 | return -EROFS; | |
422 | ||
423 | name = xattr_full_name(handler, name); | |
424 | if (strcmp(name, XATTR_NAME_CAPS) == 0) | |
425 | clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags); | |
426 | ||
427 | return btrfs_setxattr_trans(inode, name, buffer, size, flags); | |
428 | } | |
429 | ||
9172abbc | 430 | static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, |
39f60c1c | 431 | struct mnt_idmap *idmap, |
59301226 | 432 | struct dentry *unused, struct inode *inode, |
9172abbc AG |
433 | const char *name, const void *value, |
434 | size_t size, int flags) | |
435 | { | |
f22125e5 | 436 | int ret; |
b3f6a4be AJ |
437 | struct btrfs_trans_handle *trans; |
438 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
f22125e5 | 439 | |
9172abbc | 440 | name = xattr_full_name(handler, name); |
0e852ab8 | 441 | ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size); |
f22125e5 AJ |
442 | if (ret) |
443 | return ret; | |
b3f6a4be | 444 | |
4b73c55f FM |
445 | if (btrfs_ignore_prop(BTRFS_I(inode), name)) |
446 | return 0; | |
447 | ||
b3f6a4be AJ |
448 | trans = btrfs_start_transaction(root, 2); |
449 | if (IS_ERR(trans)) | |
450 | return PTR_ERR(trans); | |
451 | ||
0d9b7e16 | 452 | ret = btrfs_set_prop(trans, BTRFS_I(inode), name, value, size, flags); |
b3f6a4be AJ |
453 | if (!ret) { |
454 | inode_inc_iversion(inode); | |
2a9462de | 455 | inode_set_ctime_current(inode); |
8b9d0322 | 456 | ret = btrfs_update_inode(trans, BTRFS_I(inode)); |
193b4e83 FM |
457 | if (ret) |
458 | btrfs_abort_transaction(trans, ret); | |
b3f6a4be AJ |
459 | } |
460 | ||
461 | btrfs_end_transaction(trans); | |
462 | ||
463 | return ret; | |
95819c05 | 464 | } |
5103e947 | 465 | |
9172abbc AG |
466 | static const struct xattr_handler btrfs_security_xattr_handler = { |
467 | .prefix = XATTR_SECURITY_PREFIX, | |
ed9b50a1 JB |
468 | .get = btrfs_xattr_handler_get_security, |
469 | .set = btrfs_xattr_handler_set_security, | |
9172abbc AG |
470 | }; |
471 | ||
472 | static const struct xattr_handler btrfs_trusted_xattr_handler = { | |
473 | .prefix = XATTR_TRUSTED_PREFIX, | |
474 | .get = btrfs_xattr_handler_get, | |
475 | .set = btrfs_xattr_handler_set, | |
476 | }; | |
477 | ||
478 | static const struct xattr_handler btrfs_user_xattr_handler = { | |
479 | .prefix = XATTR_USER_PREFIX, | |
480 | .get = btrfs_xattr_handler_get, | |
481 | .set = btrfs_xattr_handler_set, | |
482 | }; | |
483 | ||
484 | static const struct xattr_handler btrfs_btrfs_xattr_handler = { | |
485 | .prefix = XATTR_BTRFS_PREFIX, | |
486 | .get = btrfs_xattr_handler_get, | |
487 | .set = btrfs_xattr_handler_set_prop, | |
488 | }; | |
489 | ||
8a25b418 | 490 | const struct xattr_handler * const btrfs_xattr_handlers[] = { |
9172abbc | 491 | &btrfs_security_xattr_handler, |
9172abbc AG |
492 | &btrfs_trusted_xattr_handler, |
493 | &btrfs_user_xattr_handler, | |
494 | &btrfs_btrfs_xattr_handler, | |
495 | NULL, | |
496 | }; | |
497 | ||
48a3b636 | 498 | static int btrfs_initxattrs(struct inode *inode, |
419a6f30 | 499 | const struct xattr *xattr_array, void *fs_private) |
0279b4cd | 500 | { |
419a6f30 | 501 | struct btrfs_trans_handle *trans = fs_private; |
9d8f13ba | 502 | const struct xattr *xattr; |
827aa18e | 503 | unsigned int nofs_flag; |
0279b4cd | 504 | char *name; |
c87b979d | 505 | int ret = 0; |
0279b4cd | 506 | |
827aa18e FM |
507 | /* |
508 | * We're holding a transaction handle, so use a NOFS memory allocation | |
509 | * context to avoid deadlock if reclaim happens. | |
510 | */ | |
511 | nofs_flag = memalloc_nofs_save(); | |
9d8f13ba MZ |
512 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
513 | name = kmalloc(XATTR_SECURITY_PREFIX_LEN + | |
39a27ec1 | 514 | strlen(xattr->name) + 1, GFP_KERNEL); |
9d8f13ba | 515 | if (!name) { |
c87b979d | 516 | ret = -ENOMEM; |
9d8f13ba MZ |
517 | break; |
518 | } | |
0279b4cd | 519 | strcpy(name, XATTR_SECURITY_PREFIX); |
9d8f13ba | 520 | strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); |
ed9b50a1 JB |
521 | |
522 | if (strcmp(name, XATTR_NAME_CAPS) == 0) | |
523 | clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags); | |
524 | ||
c87b979d | 525 | ret = btrfs_setxattr(trans, inode, name, xattr->value, |
04e6863b | 526 | xattr->value_len, 0); |
0279b4cd | 527 | kfree(name); |
c87b979d | 528 | if (ret < 0) |
9d8f13ba | 529 | break; |
0279b4cd | 530 | } |
827aa18e | 531 | memalloc_nofs_restore(nofs_flag); |
c87b979d | 532 | return ret; |
0279b4cd | 533 | } |
9d8f13ba MZ |
534 | |
535 | int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, | |
536 | struct inode *inode, struct inode *dir, | |
537 | const struct qstr *qstr) | |
538 | { | |
539 | return security_inode_init_security(inode, dir, qstr, | |
540 | &btrfs_initxattrs, trans); | |
541 | } |