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
*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
);
207 pr_err("failed to allocate memory for header node\n");
209 goto end_attr_file_creation
;
212 hfsplus_init_header_node(attr_file
, clump_size
, buf
, node_size
);
214 mapping
= attr_file
->i_mapping
;
218 for (; written
< node_size
; index
++, written
+= PAGE_SIZE
) {
221 page
= read_mapping_page(mapping
, index
, NULL
);
224 goto failed_header_node_init
;
227 kaddr
= kmap_atomic(page
);
228 memcpy(kaddr
, buf
+ written
,
229 min_t(size_t, PAGE_SIZE
, node_size
- written
));
230 kunmap_atomic(kaddr
);
232 set_page_dirty(page
);
236 hfsplus_mark_inode_dirty(attr_file
, HFSPLUS_I_ATTR_DIRTY
);
238 sbi
->attr_tree
= hfs_btree_open(sb
, HFSPLUS_ATTR_CNID
);
240 pr_err("failed to load attributes file\n");
242 failed_header_node_init
:
245 end_attr_file_creation
:
249 atomic_set(&sbi
->attr_tree_state
, HFSPLUS_VALID_ATTR_TREE
);
250 else if (err
== -ENOSPC
)
251 atomic_set(&sbi
->attr_tree_state
, HFSPLUS_EMPTY_ATTR_TREE
);
253 atomic_set(&sbi
->attr_tree_state
, HFSPLUS_FAILED_ATTR_TREE
);
258 int __hfsplus_setxattr(struct inode
*inode
, const char *name
,
259 const void *value
, size_t size
, int flags
)
262 struct hfs_find_data cat_fd
;
263 hfsplus_cat_entry entry
;
264 u16 cat_entry_flags
, cat_entry_type
;
265 u16 folder_finderinfo_len
= sizeof(struct DInfo
) +
266 sizeof(struct DXInfo
);
267 u16 file_finderinfo_len
= sizeof(struct FInfo
) +
268 sizeof(struct FXInfo
);
270 if ((!S_ISREG(inode
->i_mode
) &&
271 !S_ISDIR(inode
->i_mode
)) ||
272 HFSPLUS_IS_RSRC(inode
))
276 return hfsplus_removexattr(inode
, name
);
278 err
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &cat_fd
);
280 pr_err("can't init xattr find struct\n");
284 err
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &cat_fd
);
286 pr_err("catalog searching failed\n");
290 if (!strcmp_xattr_finder_info(name
)) {
291 if (flags
& XATTR_CREATE
) {
292 pr_err("xattr exists yet\n");
296 hfs_bnode_read(cat_fd
.bnode
, &entry
, cat_fd
.entryoffset
,
297 sizeof(hfsplus_cat_entry
));
298 if (be16_to_cpu(entry
.type
) == HFSPLUS_FOLDER
) {
299 if (size
== folder_finderinfo_len
) {
300 memcpy(&entry
.folder
.user_info
, value
,
301 folder_finderinfo_len
);
302 hfs_bnode_write(cat_fd
.bnode
, &entry
,
304 sizeof(struct hfsplus_cat_folder
));
305 hfsplus_mark_inode_dirty(inode
,
306 HFSPLUS_I_CAT_DIRTY
);
311 } else if (be16_to_cpu(entry
.type
) == HFSPLUS_FILE
) {
312 if (size
== file_finderinfo_len
) {
313 memcpy(&entry
.file
.user_info
, value
,
314 file_finderinfo_len
);
315 hfs_bnode_write(cat_fd
.bnode
, &entry
,
317 sizeof(struct hfsplus_cat_file
));
318 hfsplus_mark_inode_dirty(inode
,
319 HFSPLUS_I_CAT_DIRTY
);
331 if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
) {
332 err
= hfsplus_create_attributes_file(inode
->i_sb
);
337 if (hfsplus_attr_exists(inode
, name
)) {
338 if (flags
& XATTR_CREATE
) {
339 pr_err("xattr exists yet\n");
343 err
= hfsplus_delete_attr(inode
, name
);
346 err
= hfsplus_create_attr(inode
, name
, value
, size
);
350 if (flags
& XATTR_REPLACE
) {
351 pr_err("cannot replace xattr\n");
355 err
= hfsplus_create_attr(inode
, name
, value
, size
);
360 cat_entry_type
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
);
361 if (cat_entry_type
== HFSPLUS_FOLDER
) {
362 cat_entry_flags
= hfs_bnode_read_u16(cat_fd
.bnode
,
364 offsetof(struct hfsplus_cat_folder
, flags
));
365 cat_entry_flags
|= HFSPLUS_XATTR_EXISTS
;
366 if (!strcmp_xattr_acl(name
))
367 cat_entry_flags
|= HFSPLUS_ACL_EXISTS
;
368 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
369 offsetof(struct hfsplus_cat_folder
, flags
),
371 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
372 } else if (cat_entry_type
== HFSPLUS_FILE
) {
373 cat_entry_flags
= hfs_bnode_read_u16(cat_fd
.bnode
,
375 offsetof(struct hfsplus_cat_file
, flags
));
376 cat_entry_flags
|= HFSPLUS_XATTR_EXISTS
;
377 if (!strcmp_xattr_acl(name
))
378 cat_entry_flags
|= HFSPLUS_ACL_EXISTS
;
379 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
380 offsetof(struct hfsplus_cat_file
, flags
),
382 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
384 pr_err("invalid catalog entry type\n");
390 hfs_find_exit(&cat_fd
);
394 static int name_len(const char *xattr_name
, int xattr_name_len
)
396 int len
= xattr_name_len
+ 1;
398 if (!is_known_namespace(xattr_name
))
399 len
+= XATTR_MAC_OSX_PREFIX_LEN
;
404 static int copy_name(char *buffer
, const char *xattr_name
, int name_len
)
409 if (!is_known_namespace(xattr_name
)) {
410 memcpy(buffer
, XATTR_MAC_OSX_PREFIX
, XATTR_MAC_OSX_PREFIX_LEN
);
411 offset
+= XATTR_MAC_OSX_PREFIX_LEN
;
412 len
+= XATTR_MAC_OSX_PREFIX_LEN
;
415 strncpy(buffer
+ offset
, xattr_name
, name_len
);
416 memset(buffer
+ offset
+ name_len
, 0, 1);
422 int hfsplus_setxattr(struct inode
*inode
, const char *name
,
423 const void *value
, size_t size
, int flags
,
424 const char *prefix
, size_t prefixlen
)
429 xattr_name
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
+ 1,
433 strcpy(xattr_name
, prefix
);
434 strcpy(xattr_name
+ prefixlen
, name
);
435 res
= __hfsplus_setxattr(inode
, xattr_name
, value
, size
, flags
);
440 static ssize_t
hfsplus_getxattr_finder_info(struct inode
*inode
,
441 void *value
, size_t size
)
444 struct hfs_find_data fd
;
446 u16 folder_rec_len
= sizeof(struct DInfo
) + sizeof(struct DXInfo
);
447 u16 file_rec_len
= sizeof(struct FInfo
) + sizeof(struct FXInfo
);
448 u16 record_len
= max(folder_rec_len
, file_rec_len
);
449 u8 folder_finder_info
[sizeof(struct DInfo
) + sizeof(struct DXInfo
)];
450 u8 file_finder_info
[sizeof(struct FInfo
) + sizeof(struct FXInfo
)];
452 if (size
>= record_len
) {
453 res
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &fd
);
455 pr_err("can't init xattr find struct\n");
458 res
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &fd
);
460 goto end_getxattr_finder_info
;
461 entry_type
= hfs_bnode_read_u16(fd
.bnode
, fd
.entryoffset
);
463 if (entry_type
== HFSPLUS_FOLDER
) {
464 hfs_bnode_read(fd
.bnode
, folder_finder_info
,
466 offsetof(struct hfsplus_cat_folder
, user_info
),
468 memcpy(value
, folder_finder_info
, folder_rec_len
);
469 res
= folder_rec_len
;
470 } else if (entry_type
== HFSPLUS_FILE
) {
471 hfs_bnode_read(fd
.bnode
, file_finder_info
,
473 offsetof(struct hfsplus_cat_file
, user_info
),
475 memcpy(value
, file_finder_info
, file_rec_len
);
479 goto end_getxattr_finder_info
;
482 res
= size
? -ERANGE
: record_len
;
484 end_getxattr_finder_info
:
485 if (size
>= record_len
)
490 ssize_t
__hfsplus_getxattr(struct inode
*inode
, const char *name
,
491 void *value
, size_t size
)
493 struct hfs_find_data fd
;
494 hfsplus_attr_entry
*entry
;
495 __be32 xattr_record_type
;
497 u16 record_length
= 0;
500 if ((!S_ISREG(inode
->i_mode
) &&
501 !S_ISDIR(inode
->i_mode
)) ||
502 HFSPLUS_IS_RSRC(inode
))
505 if (!strcmp_xattr_finder_info(name
))
506 return hfsplus_getxattr_finder_info(inode
, value
, size
);
508 if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
)
511 entry
= hfsplus_alloc_attr_entry();
513 pr_err("can't allocate xattr entry\n");
517 res
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->attr_tree
, &fd
);
519 pr_err("can't init xattr find struct\n");
520 goto failed_getxattr_init
;
523 res
= hfsplus_find_attr(inode
->i_sb
, inode
->i_ino
, name
, &fd
);
528 pr_err("xattr searching failed\n");
532 hfs_bnode_read(fd
.bnode
, &xattr_record_type
,
533 fd
.entryoffset
, sizeof(xattr_record_type
));
534 record_type
= be32_to_cpu(xattr_record_type
);
535 if (record_type
== HFSPLUS_ATTR_INLINE_DATA
) {
536 record_length
= hfs_bnode_read_u16(fd
.bnode
,
538 offsetof(struct hfsplus_attr_inline_data
,
540 if (record_length
> HFSPLUS_MAX_INLINE_DATA_SIZE
) {
541 pr_err("invalid xattr record size\n");
545 } else if (record_type
== HFSPLUS_ATTR_FORK_DATA
||
546 record_type
== HFSPLUS_ATTR_EXTENTS
) {
547 pr_err("only inline data xattr are supported\n");
551 pr_err("invalid xattr record\n");
557 hfs_bnode_read(fd
.bnode
, entry
, fd
.entryoffset
,
558 offsetof(struct hfsplus_attr_inline_data
,
559 raw_bytes
) + record_length
);
562 if (size
>= record_length
) {
563 memcpy(value
, entry
->inline_data
.raw_bytes
, record_length
);
566 res
= size
? -ERANGE
: record_length
;
571 failed_getxattr_init
:
572 hfsplus_destroy_attr_entry(entry
);
576 ssize_t
hfsplus_getxattr(struct inode
*inode
, const char *name
,
577 void *value
, size_t size
,
578 const char *prefix
, size_t prefixlen
)
583 xattr_name
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
+ 1,
588 strcpy(xattr_name
, prefix
);
589 strcpy(xattr_name
+ prefixlen
, name
);
591 res
= __hfsplus_getxattr(inode
, xattr_name
, value
, size
);
597 static inline int can_list(const char *xattr_name
)
602 return strncmp(xattr_name
, XATTR_TRUSTED_PREFIX
,
603 XATTR_TRUSTED_PREFIX_LEN
) ||
604 capable(CAP_SYS_ADMIN
);
607 static ssize_t
hfsplus_listxattr_finder_info(struct dentry
*dentry
,
608 char *buffer
, size_t size
)
611 struct inode
*inode
= d_inode(dentry
);
612 struct hfs_find_data fd
;
614 u8 folder_finder_info
[sizeof(struct DInfo
) + sizeof(struct DXInfo
)];
615 u8 file_finder_info
[sizeof(struct FInfo
) + sizeof(struct FXInfo
)];
616 unsigned long len
, found_bit
;
617 int xattr_name_len
, symbols_count
;
619 res
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &fd
);
621 pr_err("can't init xattr find struct\n");
625 res
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &fd
);
627 goto end_listxattr_finder_info
;
629 entry_type
= hfs_bnode_read_u16(fd
.bnode
, fd
.entryoffset
);
630 if (entry_type
== HFSPLUS_FOLDER
) {
631 len
= sizeof(struct DInfo
) + sizeof(struct DXInfo
);
632 hfs_bnode_read(fd
.bnode
, folder_finder_info
,
634 offsetof(struct hfsplus_cat_folder
, user_info
),
636 found_bit
= find_first_bit((void *)folder_finder_info
, len
*8);
637 } else if (entry_type
== HFSPLUS_FILE
) {
638 len
= sizeof(struct FInfo
) + sizeof(struct FXInfo
);
639 hfs_bnode_read(fd
.bnode
, file_finder_info
,
641 offsetof(struct hfsplus_cat_file
, user_info
),
643 found_bit
= find_first_bit((void *)file_finder_info
, len
*8);
646 goto end_listxattr_finder_info
;
649 if (found_bit
>= (len
*8))
652 symbols_count
= sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME
) - 1;
654 name_len(HFSPLUS_XATTR_FINDER_INFO_NAME
, symbols_count
);
655 if (!buffer
|| !size
) {
656 if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME
))
657 res
= xattr_name_len
;
658 } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME
)) {
659 if (size
< xattr_name_len
)
662 res
= copy_name(buffer
,
663 HFSPLUS_XATTR_FINDER_INFO_NAME
,
669 end_listxattr_finder_info
:
675 ssize_t
hfsplus_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
679 struct inode
*inode
= d_inode(dentry
);
680 struct hfs_find_data fd
;
682 struct hfsplus_attr_key attr_key
;
686 if ((!S_ISREG(inode
->i_mode
) &&
687 !S_ISDIR(inode
->i_mode
)) ||
688 HFSPLUS_IS_RSRC(inode
))
691 res
= hfsplus_listxattr_finder_info(dentry
, buffer
, size
);
694 else if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
)
695 return (res
== 0) ? -EOPNOTSUPP
: res
;
697 err
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->attr_tree
, &fd
);
699 pr_err("can't init xattr find struct\n");
703 strbuf
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
+
704 XATTR_MAC_OSX_PREFIX_LEN
+ 1, GFP_KERNEL
);
710 err
= hfsplus_find_attr(inode
->i_sb
, inode
->i_ino
, NULL
, &fd
);
712 if (err
== -ENOENT
) {
723 key_len
= hfs_bnode_read_u16(fd
.bnode
, fd
.keyoffset
);
724 if (key_len
== 0 || key_len
> fd
.tree
->max_key_len
) {
725 pr_err("invalid xattr key length: %d\n", key_len
);
730 hfs_bnode_read(fd
.bnode
, &attr_key
,
731 fd
.keyoffset
, key_len
+ sizeof(key_len
));
733 if (be32_to_cpu(attr_key
.cnid
) != inode
->i_ino
)
736 xattr_name_len
= NLS_MAX_CHARSET_SIZE
* HFSPLUS_ATTR_MAX_STRLEN
;
737 if (hfsplus_uni2asc(inode
->i_sb
,
738 (const struct hfsplus_unistr
*)&fd
.key
->attr
.key_name
,
739 strbuf
, &xattr_name_len
)) {
740 pr_err("unicode conversion failed\n");
745 if (!buffer
|| !size
) {
746 if (can_list(strbuf
))
747 res
+= name_len(strbuf
, xattr_name_len
);
748 } else if (can_list(strbuf
)) {
749 if (size
< (res
+ name_len(strbuf
, xattr_name_len
))) {
753 res
+= copy_name(buffer
+ res
,
754 strbuf
, xattr_name_len
);
757 if (hfs_brec_goto(&fd
, 1))
768 static int hfsplus_removexattr(struct inode
*inode
, const char *name
)
771 struct hfs_find_data cat_fd
;
774 int is_xattr_acl_deleted
= 0;
775 int is_all_xattrs_deleted
= 0;
777 if (!HFSPLUS_SB(inode
->i_sb
)->attr_tree
)
780 if (!strcmp_xattr_finder_info(name
))
783 err
= hfs_find_init(HFSPLUS_SB(inode
->i_sb
)->cat_tree
, &cat_fd
);
785 pr_err("can't init xattr find struct\n");
789 err
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &cat_fd
);
791 pr_err("catalog searching failed\n");
792 goto end_removexattr
;
795 err
= hfsplus_delete_attr(inode
, name
);
797 goto end_removexattr
;
799 is_xattr_acl_deleted
= !strcmp_xattr_acl(name
);
800 is_all_xattrs_deleted
= !hfsplus_attr_exists(inode
, NULL
);
802 if (!is_xattr_acl_deleted
&& !is_all_xattrs_deleted
)
803 goto end_removexattr
;
805 cat_entry_type
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
);
807 if (cat_entry_type
== HFSPLUS_FOLDER
) {
808 flags
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
809 offsetof(struct hfsplus_cat_folder
, flags
));
810 if (is_xattr_acl_deleted
)
811 flags
&= ~HFSPLUS_ACL_EXISTS
;
812 if (is_all_xattrs_deleted
)
813 flags
&= ~HFSPLUS_XATTR_EXISTS
;
814 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
815 offsetof(struct hfsplus_cat_folder
, flags
),
817 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
818 } else if (cat_entry_type
== HFSPLUS_FILE
) {
819 flags
= hfs_bnode_read_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
820 offsetof(struct hfsplus_cat_file
, flags
));
821 if (is_xattr_acl_deleted
)
822 flags
&= ~HFSPLUS_ACL_EXISTS
;
823 if (is_all_xattrs_deleted
)
824 flags
&= ~HFSPLUS_XATTR_EXISTS
;
825 hfs_bnode_write_u16(cat_fd
.bnode
, cat_fd
.entryoffset
+
826 offsetof(struct hfsplus_cat_file
, flags
),
828 hfsplus_mark_inode_dirty(inode
, HFSPLUS_I_CAT_DIRTY
);
830 pr_err("invalid catalog entry type\n");
832 goto end_removexattr
;
836 hfs_find_exit(&cat_fd
);
840 static int hfsplus_osx_getxattr(const struct xattr_handler
*handler
,
841 struct dentry
*unused
, struct inode
*inode
,
842 const char *name
, void *buffer
, size_t size
)
845 * Don't allow retrieving properly prefixed attributes
846 * by prepending them with "osx."
848 if (is_known_namespace(name
))
852 * osx is the namespace we use to indicate an unprefixed
853 * attribute on the filesystem (like the ones that OS X
854 * creates), so we pass the name through unmodified (after
855 * ensuring it doesn't conflict with another namespace).
857 return __hfsplus_getxattr(inode
, name
, buffer
, size
);
860 static int hfsplus_osx_setxattr(const struct xattr_handler
*handler
,
861 struct dentry
*unused
, struct inode
*inode
,
862 const char *name
, const void *buffer
,
863 size_t size
, int flags
)
866 * Don't allow setting properly prefixed attributes
867 * by prepending them with "osx."
869 if (is_known_namespace(name
))
873 * osx is the namespace we use to indicate an unprefixed
874 * attribute on the filesystem (like the ones that OS X
875 * creates), so we pass the name through unmodified (after
876 * ensuring it doesn't conflict with another namespace).
878 return __hfsplus_setxattr(inode
, name
, buffer
, size
, flags
);
881 const struct xattr_handler hfsplus_xattr_osx_handler
= {
882 .prefix
= XATTR_MAC_OSX_PREFIX
,
883 .get
= hfsplus_osx_getxattr
,
884 .set
= hfsplus_osx_setxattr
,