1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hfsplus/xattr.c
5 * Vyacheslav Dubeyko <slava@dubeyko.com>
7 * Logic of processing extended attributes
10 #include "hfsplus_fs.h"
11 #include <linux/nls.h>
14 static int hfsplus_removexattr(struct inode
*inode
, const char *name
);
16 const struct xattr_handler
* const hfsplus_xattr_handlers
[] = {
17 &hfsplus_xattr_osx_handler
,
18 &hfsplus_xattr_user_handler
,
19 &hfsplus_xattr_trusted_handler
,
20 &hfsplus_xattr_security_handler
,
24 static int strcmp_xattr_finder_info(const char *name
)
27 return strncmp(name
, HFSPLUS_XATTR_FINDER_INFO_NAME
,
28 sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME
));
33 static int strcmp_xattr_acl(const char *name
)
36 return strncmp(name
, HFSPLUS_XATTR_ACL_NAME
,
37 sizeof(HFSPLUS_XATTR_ACL_NAME
));
42 static bool is_known_namespace(const char *name
)
44 if (strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) &&
45 strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) &&
46 strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) &&
47 strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
))
53 static void hfsplus_init_header_node(struct inode
*attr_file
,
55 char *buf
, u16 node_size
)
57 struct hfs_bnode_desc
*desc
;
58 struct hfs_btree_header_rec
*head
;
61 u32 hdr_node_map_rec_bits
;
67 hfs_dbg(ATTR_MOD
, "init_hdr_attr_file: clump %u, node_size %u\n",
68 clump_size
, node_size
);
70 /* The end of the node contains list of record offsets */
71 rec_offsets
= (__be16
*)(buf
+ node_size
);
73 desc
= (struct hfs_bnode_desc
*)buf
;
74 desc
->type
= HFS_NODE_HEADER
;
75 desc
->num_recs
= cpu_to_be16(HFSPLUS_BTREE_HDR_NODE_RECS_COUNT
);
76 offset
= sizeof(struct hfs_bnode_desc
);
77 *--rec_offsets
= cpu_to_be16(offset
);
79 head
= (struct hfs_btree_header_rec
*)(buf
+ offset
);
80 head
->node_size
= cpu_to_be16(node_size
);
81 tmp
= i_size_read(attr_file
);
82 do_div(tmp
, node_size
);
83 head
->node_count
= cpu_to_be32(tmp
);
84 head
->free_nodes
= cpu_to_be32(be32_to_cpu(head
->node_count
) - 1);
85 head
->clump_size
= cpu_to_be32(clump_size
);
86 head
->attributes
|= cpu_to_be32(HFS_TREE_BIGKEYS
| HFS_TREE_VARIDXKEYS
);
87 head
->max_key_len
= cpu_to_be16(HFSPLUS_ATTR_KEYLEN
- sizeof(u16
));
88 offset
+= sizeof(struct hfs_btree_header_rec
);
89 *--rec_offsets
= cpu_to_be16(offset
);
90 offset
+= HFSPLUS_BTREE_HDR_USER_BYTES
;
91 *--rec_offsets
= cpu_to_be16(offset
);
93 hdr_node_map_rec_bits
= 8 * (node_size
- offset
- (4 * sizeof(u16
)));
94 if (be32_to_cpu(head
->node_count
) > hdr_node_map_rec_bits
) {
98 desc
->next
= cpu_to_be32(be32_to_cpu(head
->leaf_tail
) + 1);
99 map_node_bits
= 8 * (node_size
- sizeof(struct hfs_bnode_desc
) -
100 (2 * sizeof(u16
)) - 2);
101 map_nodes
= (be32_to_cpu(head
->node_count
) -
102 hdr_node_map_rec_bits
+
103 (map_node_bits
- 1)) / map_node_bits
;
104 be32_add_cpu(&head
->free_nodes
, 0 - map_nodes
);
109 be32_to_cpu(head
->node_count
) - be32_to_cpu(head
->free_nodes
);
110 used_bmp_bytes
= used_nodes
/ 8;
111 if (used_bmp_bytes
) {
112 memset(bmp
, 0xFF, used_bmp_bytes
);
113 bmp
+= used_bmp_bytes
;
116 *bmp
= ~(0xFF >> used_nodes
);
117 offset
+= hdr_node_map_rec_bits
/ 8;
118 *--rec_offsets
= cpu_to_be16(offset
);
121 static int hfsplus_create_attributes_file(struct super_block
*sb
)
124 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(sb
);
125 struct inode
*attr_file
;
126 struct hfsplus_inode_info
*hip
;
128 u16 node_size
= HFSPLUS_ATTR_TREE_NODE_SIZE
;
131 struct address_space
*mapping
;
133 int old_state
= HFSPLUS_EMPTY_ATTR_TREE
;
135 hfs_dbg(ATTR_MOD
, "create_attr_file: ino %d\n", HFSPLUS_ATTR_CNID
);
137 check_attr_tree_state_again
:
138 switch (atomic_read(&sbi
->attr_tree_state
)) {
139 case HFSPLUS_EMPTY_ATTR_TREE
:
140 if (old_state
!= atomic_cmpxchg(&sbi
->attr_tree_state
,
142 HFSPLUS_CREATING_ATTR_TREE
))
143 goto check_attr_tree_state_again
;
145 case HFSPLUS_CREATING_ATTR_TREE
:
147 * This state means that another thread is in process
148 * of AttributesFile creation. Theoretically, it is
149 * possible to be here. But really __setxattr() method
150 * first of all calls hfs_find_init() for lookup in
151 * B-tree of CatalogFile. This method locks mutex of
152 * CatalogFile's B-tree. As a result, if some thread
153 * is inside AttributedFile creation operation then
154 * another threads will be waiting unlocking of
155 * CatalogFile's B-tree's mutex. However, if code will
156 * change then we will return error code (-EAGAIN) from
157 * here. Really, it means that first try to set of xattr
158 * fails with error but second attempt will have success.
161 case HFSPLUS_VALID_ATTR_TREE
:
163 case HFSPLUS_FAILED_ATTR_TREE
:
169 attr_file
= hfsplus_iget(sb
, HFSPLUS_ATTR_CNID
);
170 if (IS_ERR(attr_file
)) {
171 pr_err("failed to load attributes file\n");
172 return PTR_ERR(attr_file
);
175 BUG_ON(i_size_read(attr_file
) != 0);
177 hip
= HFSPLUS_I(attr_file
);
179 clump_size
= hfsplus_calc_btree_clump_size(sb
->s_blocksize
,
184 mutex_lock(&hip
->extents_lock
);
185 hip
->clump_blocks
= clump_size
>> sbi
->alloc_blksz_shift
;
186 mutex_unlock(&hip
->extents_lock
);
188 if (sbi
->free_blocks
<= (hip
->clump_blocks
<< 1)) {
190 goto end_attr_file_creation
;
193 while (hip
->alloc_blocks
< hip
->clump_blocks
) {
194 err
= hfsplus_file_extend(attr_file
, false);
196 pr_err("failed to extend attributes file\n");
197 goto end_attr_file_creation
;
199 hip
->phys_size
= attr_file
->i_size
=
200 (loff_t
)hip
->alloc_blocks
<< sbi
->alloc_blksz_shift
;
201 hip
->fs_blocks
= hip
->alloc_blocks
<< sbi
->fs_shift
;
202 inode_set_bytes(attr_file
, attr_file
->i_size
);
205 buf
= kzalloc(node_size
, GFP_NOFS
);
208 goto end_attr_file_creation
;
211 hfsplus_init_header_node(attr_file
, clump_size
, buf
, node_size
);
213 mapping
= attr_file
->i_mapping
;
217 for (; written
< node_size
; index
++, written
+= PAGE_SIZE
) {
220 page
= read_mapping_page(mapping
, index
, NULL
);
223 goto failed_header_node_init
;
226 kaddr
= kmap_atomic(page
);
227 memcpy(kaddr
, buf
+ written
,
228 min_t(size_t, PAGE_SIZE
, node_size
- written
));
229 kunmap_atomic(kaddr
);
231 set_page_dirty(page
);
235 hfsplus_mark_inode_dirty(attr_file
, HFSPLUS_I_ATTR_DIRTY
);
237 sbi
->attr_tree
= hfs_btree_open(sb
, HFSPLUS_ATTR_CNID
);
239 pr_err("failed to load attributes file\n");
241 failed_header_node_init
:
244 end_attr_file_creation
:
248 atomic_set(&sbi
->attr_tree_state
, HFSPLUS_VALID_ATTR_TREE
);
249 else if (err
== -ENOSPC
)
250 atomic_set(&sbi
->attr_tree_state
, HFSPLUS_EMPTY_ATTR_TREE
);
252 atomic_set(&sbi
->attr_tree_state
, HFSPLUS_FAILED_ATTR_TREE
);
257 int __hfsplus_setxattr(struct inode
*inode
, const char *name
,
258 const void *value
, size_t size
, int flags
)
261 struct hfs_find_data cat_fd
;
262 hfsplus_cat_entry entry
;
263 u16 cat_entry_flags
, cat_entry_type
;
264 u16 folder_finderinfo_len
= sizeof(struct DInfo
) +
265 sizeof(struct DXInfo
);
266 u16 file_finderinfo_len
= sizeof(struct FInfo
) +
267 sizeof(struct FXInfo
);
269 if ((!S_ISREG(inode
->i_mode
) &&
270 !S_ISDIR(inode
->i_mode
)) ||
271 HFSPLUS_IS_RSRC(inode
))
275 return hfsplus_removexattr(inode
, name
);
277 err
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &cat_fd
);
279 pr_err("can't init xattr find struct\n");
283 err
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &cat_fd
);
285 pr_err("catalog searching failed\n");
289 if (!strcmp_xattr_finder_info(name
)) {
290 if (flags
& XATTR_CREATE
) {
291 pr_err("xattr exists yet\n");
295 hfs_bnode_read(cat_fd
.bnode
, &entry
, cat_fd
.entryoffset
,
296 sizeof(hfsplus_cat_entry
));
297 if (be16_to_cpu(entry
.type
) == HFSPLUS_FOLDER
) {
298 if (size
== folder_finderinfo_len
) {
299 memcpy(&entry
.folder
.info
, value
,
300 folder_finderinfo_len
);
301 hfs_bnode_write(cat_fd
.bnode
, &entry
,
303 sizeof(struct hfsplus_cat_folder
));
304 hfsplus_mark_inode_dirty(inode
,
305 HFSPLUS_I_CAT_DIRTY
);
310 } else if (be16_to_cpu(entry
.type
) == HFSPLUS_FILE
) {
311 if (size
== file_finderinfo_len
) {
312 memcpy(&entry
.file
.info
, value
,
313 file_finderinfo_len
);
314 hfs_bnode_write(cat_fd
.bnode
, &entry
,
316 sizeof(struct hfsplus_cat_file
));
317 hfsplus_mark_inode_dirty(inode
,
318 HFSPLUS_I_CAT_DIRTY
);
330 if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
) {
331 err
= hfsplus_create_attributes_file(inode
->i_sb
);
336 if (hfsplus_attr_exists(inode
, name
)) {
337 if (flags
& XATTR_CREATE
) {
338 pr_err("xattr exists yet\n");
342 err
= hfsplus_delete_attr(inode
, name
);
345 err
= hfsplus_create_attr(inode
, name
, value
, size
);
349 if (flags
& XATTR_REPLACE
) {
350 pr_err("cannot replace xattr\n");
354 err
= hfsplus_create_attr(inode
, name
, value
, size
);
359 cat_entry_type
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
);
360 if (cat_entry_type
== HFSPLUS_FOLDER
) {
361 cat_entry_flags
= hfs_bnode_read_u16(cat_fd
.bnode
,
363 offsetof(struct hfsplus_cat_folder
, flags
));
364 cat_entry_flags
|= HFSPLUS_XATTR_EXISTS
;
365 if (!strcmp_xattr_acl(name
))
366 cat_entry_flags
|= HFSPLUS_ACL_EXISTS
;
367 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
368 offsetof(struct hfsplus_cat_folder
, flags
),
370 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
371 } else if (cat_entry_type
== HFSPLUS_FILE
) {
372 cat_entry_flags
= hfs_bnode_read_u16(cat_fd
.bnode
,
374 offsetof(struct hfsplus_cat_file
, flags
));
375 cat_entry_flags
|= HFSPLUS_XATTR_EXISTS
;
376 if (!strcmp_xattr_acl(name
))
377 cat_entry_flags
|= HFSPLUS_ACL_EXISTS
;
378 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
379 offsetof(struct hfsplus_cat_file
, flags
),
381 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
383 pr_err("invalid catalog entry type\n");
389 hfs_find_exit(&cat_fd
);
393 static int name_len(const char *xattr_name
, int xattr_name_len
)
395 int len
= xattr_name_len
+ 1;
397 if (!is_known_namespace(xattr_name
))
398 len
+= XATTR_MAC_OSX_PREFIX_LEN
;
403 static ssize_t
copy_name(char *buffer
, const char *xattr_name
, int name_len
)
407 if (!is_known_namespace(xattr_name
))
408 len
= scnprintf(buffer
, name_len
+ XATTR_MAC_OSX_PREFIX_LEN
,
409 "%s%s", XATTR_MAC_OSX_PREFIX
, xattr_name
);
411 len
= strscpy(buffer
, xattr_name
, name_len
+ 1);
413 /* include NUL-byte in length for non-empty name */
419 int hfsplus_setxattr(struct inode
*inode
, const char *name
,
420 const void *value
, size_t size
, int flags
,
421 const char *prefix
, size_t prefixlen
)
426 xattr_name
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
+ 1,
430 strcpy(xattr_name
, prefix
);
431 strcpy(xattr_name
+ prefixlen
, name
);
432 res
= __hfsplus_setxattr(inode
, xattr_name
, value
, size
, flags
);
437 static ssize_t
hfsplus_getxattr_finder_info(struct inode
*inode
,
438 void *value
, size_t size
)
441 struct hfs_find_data fd
;
443 u16 folder_rec_len
= sizeof(struct DInfo
) + sizeof(struct DXInfo
);
444 u16 file_rec_len
= sizeof(struct FInfo
) + sizeof(struct FXInfo
);
445 u16 record_len
= max(folder_rec_len
, file_rec_len
);
446 u8 folder_finder_info
[sizeof(struct DInfo
) + sizeof(struct DXInfo
)];
447 u8 file_finder_info
[sizeof(struct FInfo
) + sizeof(struct FXInfo
)];
449 if (size
>= record_len
) {
450 res
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &fd
);
452 pr_err("can't init xattr find struct\n");
455 res
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &fd
);
457 goto end_getxattr_finder_info
;
458 entry_type
= hfs_bnode_read_u16(fd
.bnode
, fd
.entryoffset
);
460 if (entry_type
== HFSPLUS_FOLDER
) {
461 hfs_bnode_read(fd
.bnode
, folder_finder_info
,
463 offsetof(struct hfsplus_cat_folder
, user_info
),
465 memcpy(value
, folder_finder_info
, folder_rec_len
);
466 res
= folder_rec_len
;
467 } else if (entry_type
== HFSPLUS_FILE
) {
468 hfs_bnode_read(fd
.bnode
, file_finder_info
,
470 offsetof(struct hfsplus_cat_file
, user_info
),
472 memcpy(value
, file_finder_info
, file_rec_len
);
476 goto end_getxattr_finder_info
;
479 res
= size
? -ERANGE
: record_len
;
481 end_getxattr_finder_info
:
482 if (size
>= record_len
)
487 ssize_t
__hfsplus_getxattr(struct inode
*inode
, const char *name
,
488 void *value
, size_t size
)
490 struct hfs_find_data fd
;
491 hfsplus_attr_entry
*entry
;
492 __be32 xattr_record_type
;
494 u16 record_length
= 0;
497 if ((!S_ISREG(inode
->i_mode
) &&
498 !S_ISDIR(inode
->i_mode
)) ||
499 HFSPLUS_IS_RSRC(inode
))
502 if (!strcmp_xattr_finder_info(name
))
503 return hfsplus_getxattr_finder_info(inode
, value
, size
);
505 if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
)
508 entry
= hfsplus_alloc_attr_entry();
510 pr_err("can't allocate xattr entry\n");
514 res
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->attr_tree
, &fd
);
516 pr_err("can't init xattr find struct\n");
517 goto failed_getxattr_init
;
520 res
= hfsplus_find_attr(inode
->i_sb
, inode
->i_ino
, name
, &fd
);
525 pr_err("xattr searching failed\n");
529 hfs_bnode_read(fd
.bnode
, &xattr_record_type
,
530 fd
.entryoffset
, sizeof(xattr_record_type
));
531 record_type
= be32_to_cpu(xattr_record_type
);
532 if (record_type
== HFSPLUS_ATTR_INLINE_DATA
) {
533 record_length
= hfs_bnode_read_u16(fd
.bnode
,
535 offsetof(struct hfsplus_attr_inline_data
,
537 if (record_length
> HFSPLUS_MAX_INLINE_DATA_SIZE
) {
538 pr_err("invalid xattr record size\n");
542 } else if (record_type
== HFSPLUS_ATTR_FORK_DATA
||
543 record_type
== HFSPLUS_ATTR_EXTENTS
) {
544 pr_err("only inline data xattr are supported\n");
548 pr_err("invalid xattr record\n");
554 hfs_bnode_read(fd
.bnode
, entry
, fd
.entryoffset
,
555 offsetof(struct hfsplus_attr_inline_data
,
556 raw_bytes
) + record_length
);
559 if (size
>= record_length
) {
560 memcpy(value
, entry
->inline_data
.raw_bytes
, record_length
);
563 res
= size
? -ERANGE
: record_length
;
568 failed_getxattr_init
:
569 hfsplus_destroy_attr_entry(entry
);
573 ssize_t
hfsplus_getxattr(struct inode
*inode
, const char *name
,
574 void *value
, size_t size
,
575 const char *prefix
, size_t prefixlen
)
580 xattr_name
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
+ 1,
585 strcpy(xattr_name
, prefix
);
586 strcpy(xattr_name
+ prefixlen
, name
);
588 res
= __hfsplus_getxattr(inode
, xattr_name
, value
, size
);
594 static inline int can_list(const char *xattr_name
)
599 return strncmp(xattr_name
, XATTR_TRUSTED_PREFIX
,
600 XATTR_TRUSTED_PREFIX_LEN
) ||
601 capable(CAP_SYS_ADMIN
);
604 static ssize_t
hfsplus_listxattr_finder_info(struct dentry
*dentry
,
605 char *buffer
, size_t size
)
608 struct inode
*inode
= d_inode(dentry
);
609 struct hfs_find_data fd
;
611 u8 folder_finder_info
[sizeof(struct DInfo
) + sizeof(struct DXInfo
)];
612 u8 file_finder_info
[sizeof(struct FInfo
) + sizeof(struct FXInfo
)];
613 unsigned long len
, found_bit
;
614 int xattr_name_len
, symbols_count
;
616 res
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &fd
);
618 pr_err("can't init xattr find struct\n");
622 res
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &fd
);
624 goto end_listxattr_finder_info
;
626 entry_type
= hfs_bnode_read_u16(fd
.bnode
, fd
.entryoffset
);
627 if (entry_type
== HFSPLUS_FOLDER
) {
628 len
= sizeof(struct DInfo
) + sizeof(struct DXInfo
);
629 hfs_bnode_read(fd
.bnode
, folder_finder_info
,
631 offsetof(struct hfsplus_cat_folder
, user_info
),
633 found_bit
= find_first_bit((void *)folder_finder_info
, len
*8);
634 } else if (entry_type
== HFSPLUS_FILE
) {
635 len
= sizeof(struct FInfo
) + sizeof(struct FXInfo
);
636 hfs_bnode_read(fd
.bnode
, file_finder_info
,
638 offsetof(struct hfsplus_cat_file
, user_info
),
640 found_bit
= find_first_bit((void *)file_finder_info
, len
*8);
643 goto end_listxattr_finder_info
;
646 if (found_bit
>= (len
*8))
649 symbols_count
= sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME
) - 1;
651 name_len(HFSPLUS_XATTR_FINDER_INFO_NAME
, symbols_count
);
652 if (!buffer
|| !size
) {
653 if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME
))
654 res
= xattr_name_len
;
655 } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME
)) {
656 if (size
< xattr_name_len
)
659 res
= copy_name(buffer
,
660 HFSPLUS_XATTR_FINDER_INFO_NAME
,
666 end_listxattr_finder_info
:
672 ssize_t
hfsplus_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
676 struct inode
*inode
= d_inode(dentry
);
677 struct hfs_find_data fd
;
678 struct hfsplus_attr_key attr_key
;
682 if ((!S_ISREG(inode
->i_mode
) &&
683 !S_ISDIR(inode
->i_mode
)) ||
684 HFSPLUS_IS_RSRC(inode
))
687 res
= hfsplus_listxattr_finder_info(dentry
, buffer
, size
);
690 else if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
)
691 return (res
== 0) ? -EOPNOTSUPP
: res
;
693 err
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->attr_tree
, &fd
);
695 pr_err("can't init xattr find struct\n");
699 strbuf
= kzalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
+
700 XATTR_MAC_OSX_PREFIX_LEN
+ 1, GFP_KERNEL
);
706 err
= hfsplus_find_attr(inode
->i_sb
, inode
->i_ino
, NULL
, &fd
);
708 if (err
== -ENOENT
) {
719 u16 key_len
= hfs_bnode_read_u16(fd
.bnode
, fd
.keyoffset
);
721 if (key_len
== 0 || key_len
> fd
.tree
->max_key_len
) {
722 pr_err("invalid xattr key length: %d\n", key_len
);
727 hfs_bnode_read(fd
.bnode
, &attr_key
,
728 fd
.keyoffset
, key_len
+ sizeof(key_len
));
730 if (be32_to_cpu(attr_key
.cnid
) != inode
->i_ino
)
733 xattr_name_len
= NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
;
734 if (hfsplus_uni2asc(inode
->i_sb
,
735 (const struct hfsplus_unistr
*)&fd
.key
->attr
.key_name
,
736 strbuf
, &xattr_name_len
)) {
737 pr_err("unicode conversion failed\n");
742 if (!buffer
|| !size
) {
743 if (can_list(strbuf
))
744 res
+= name_len(strbuf
, xattr_name_len
);
745 } else if (can_list(strbuf
)) {
746 if (size
< (res
+ name_len(strbuf
, xattr_name_len
))) {
750 res
+= copy_name(buffer
+ res
,
751 strbuf
, xattr_name_len
);
754 if (hfs_brec_goto(&fd
, 1))
765 static int hfsplus_removexattr(struct inode
*inode
, const char *name
)
768 struct hfs_find_data cat_fd
;
771 int is_xattr_acl_deleted
;
772 int is_all_xattrs_deleted
;
774 if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
)
777 if (!strcmp_xattr_finder_info(name
))
780 err
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &cat_fd
);
782 pr_err("can't init xattr find struct\n");
786 err
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &cat_fd
);
788 pr_err("catalog searching failed\n");
789 goto end_removexattr
;
792 err
= hfsplus_delete_attr(inode
, name
);
794 goto end_removexattr
;
796 is_xattr_acl_deleted
= !strcmp_xattr_acl(name
);
797 is_all_xattrs_deleted
= !hfsplus_attr_exists(inode
, NULL
);
799 if (!is_xattr_acl_deleted
&& !is_all_xattrs_deleted
)
800 goto end_removexattr
;
802 cat_entry_type
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
);
804 if (cat_entry_type
== HFSPLUS_FOLDER
) {
805 flags
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
806 offsetof(struct hfsplus_cat_folder
, flags
));
807 if (is_xattr_acl_deleted
)
808 flags
&= ~HFSPLUS_ACL_EXISTS
;
809 if (is_all_xattrs_deleted
)
810 flags
&= ~HFSPLUS_XATTR_EXISTS
;
811 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
812 offsetof(struct hfsplus_cat_folder
, flags
),
814 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
815 } else if (cat_entry_type
== HFSPLUS_FILE
) {
816 flags
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
817 offsetof(struct hfsplus_cat_file
, flags
));
818 if (is_xattr_acl_deleted
)
819 flags
&= ~HFSPLUS_ACL_EXISTS
;
820 if (is_all_xattrs_deleted
)
821 flags
&= ~HFSPLUS_XATTR_EXISTS
;
822 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
823 offsetof(struct hfsplus_cat_file
, flags
),
825 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
827 pr_err("invalid catalog entry type\n");
829 goto end_removexattr
;
833 hfs_find_exit(&cat_fd
);
837 static int hfsplus_osx_getxattr(const struct xattr_handler
*handler
,
838 struct dentry
*unused
, struct inode
*inode
,
839 const char *name
, void *buffer
, size_t size
)
842 * Don't allow retrieving properly prefixed attributes
843 * by prepending them with "osx."
845 if (is_known_namespace(name
))
849 * osx is the namespace we use to indicate an unprefixed
850 * attribute on the filesystem (like the ones that OS X
851 * creates), so we pass the name through unmodified (after
852 * ensuring it doesn't conflict with another namespace).
854 return __hfsplus_getxattr(inode
, name
, buffer
, size
);
857 static int hfsplus_osx_setxattr(const struct xattr_handler
*handler
,
858 struct mnt_idmap
*idmap
,
859 struct dentry
*unused
, struct inode
*inode
,
860 const char *name
, const void *buffer
,
861 size_t size
, int flags
)
864 * Don't allow setting properly prefixed attributes
865 * by prepending them with "osx."
867 if (is_known_namespace(name
))
871 * osx is the namespace we use to indicate an unprefixed
872 * attribute on the filesystem (like the ones that OS X
873 * creates), so we pass the name through unmodified (after
874 * ensuring it doesn't conflict with another namespace).
876 return __hfsplus_setxattr(inode
, name
, buffer
, size
, flags
);
879 const struct xattr_handler hfsplus_xattr_osx_handler
= {
880 .prefix
= XATTR_MAC_OSX_PREFIX
,
881 .get
= hfsplus_osx_getxattr
,
882 .set
= hfsplus_osx_setxattr
,