1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2127e5f5aSVyacheslav Dubeyko /*
3127e5f5aSVyacheslav Dubeyko * linux/fs/hfsplus/xattr.c
4127e5f5aSVyacheslav Dubeyko *
5127e5f5aSVyacheslav Dubeyko * Vyacheslav Dubeyko <slava@dubeyko.com>
6127e5f5aSVyacheslav Dubeyko *
7127e5f5aSVyacheslav Dubeyko * Logic of processing extended attributes
8127e5f5aSVyacheslav Dubeyko */
9127e5f5aSVyacheslav Dubeyko
10127e5f5aSVyacheslav Dubeyko #include "hfsplus_fs.h"
11017f8da4SHin-Tak Leung #include <linux/nls.h>
12127e5f5aSVyacheslav Dubeyko #include "xattr.h"
13127e5f5aSVyacheslav Dubeyko
14b168fff7SChristoph Hellwig static int hfsplus_removexattr(struct inode *inode, const char *name);
15b168fff7SChristoph Hellwig
16127e5f5aSVyacheslav Dubeyko const struct xattr_handler *hfsplus_xattr_handlers[] = {
17127e5f5aSVyacheslav Dubeyko &hfsplus_xattr_osx_handler,
18127e5f5aSVyacheslav Dubeyko &hfsplus_xattr_user_handler,
19127e5f5aSVyacheslav Dubeyko &hfsplus_xattr_trusted_handler,
20127e5f5aSVyacheslav Dubeyko &hfsplus_xattr_security_handler,
21127e5f5aSVyacheslav Dubeyko NULL
22127e5f5aSVyacheslav Dubeyko };
23127e5f5aSVyacheslav Dubeyko
strcmp_xattr_finder_info(const char * name)24127e5f5aSVyacheslav Dubeyko static int strcmp_xattr_finder_info(const char *name)
25127e5f5aSVyacheslav Dubeyko {
26127e5f5aSVyacheslav Dubeyko if (name) {
27127e5f5aSVyacheslav Dubeyko return strncmp(name, HFSPLUS_XATTR_FINDER_INFO_NAME,
28127e5f5aSVyacheslav Dubeyko sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME));
29127e5f5aSVyacheslav Dubeyko }
30127e5f5aSVyacheslav Dubeyko return -1;
31127e5f5aSVyacheslav Dubeyko }
32127e5f5aSVyacheslav Dubeyko
strcmp_xattr_acl(const char * name)33127e5f5aSVyacheslav Dubeyko static int strcmp_xattr_acl(const char *name)
34127e5f5aSVyacheslav Dubeyko {
35127e5f5aSVyacheslav Dubeyko if (name) {
36127e5f5aSVyacheslav Dubeyko return strncmp(name, HFSPLUS_XATTR_ACL_NAME,
37127e5f5aSVyacheslav Dubeyko sizeof(HFSPLUS_XATTR_ACL_NAME));
38127e5f5aSVyacheslav Dubeyko }
39127e5f5aSVyacheslav Dubeyko return -1;
40127e5f5aSVyacheslav Dubeyko }
41127e5f5aSVyacheslav Dubeyko
is_known_namespace(const char * name)421ad8d63dSFabian Frederick static bool is_known_namespace(const char *name)
43127e5f5aSVyacheslav Dubeyko {
44127e5f5aSVyacheslav Dubeyko if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
45127e5f5aSVyacheslav Dubeyko strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
46127e5f5aSVyacheslav Dubeyko strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
47127e5f5aSVyacheslav Dubeyko strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
48127e5f5aSVyacheslav Dubeyko return false;
49127e5f5aSVyacheslav Dubeyko
50127e5f5aSVyacheslav Dubeyko return true;
51127e5f5aSVyacheslav Dubeyko }
52127e5f5aSVyacheslav Dubeyko
hfsplus_init_header_node(struct inode * attr_file,u32 clump_size,char * buf,u16 node_size)53099e9245SVyacheslav Dubeyko static void hfsplus_init_header_node(struct inode *attr_file,
54099e9245SVyacheslav Dubeyko u32 clump_size,
55a99b7069SGeert Uytterhoeven char *buf, u16 node_size)
56099e9245SVyacheslav Dubeyko {
57099e9245SVyacheslav Dubeyko struct hfs_bnode_desc *desc;
58099e9245SVyacheslav Dubeyko struct hfs_btree_header_rec *head;
59099e9245SVyacheslav Dubeyko u16 offset;
60099e9245SVyacheslav Dubeyko __be16 *rec_offsets;
61099e9245SVyacheslav Dubeyko u32 hdr_node_map_rec_bits;
62099e9245SVyacheslav Dubeyko char *bmp;
63099e9245SVyacheslav Dubeyko u32 used_nodes;
64099e9245SVyacheslav Dubeyko u32 used_bmp_bytes;
65df3d4e7aSChristian Kujau u64 tmp;
66099e9245SVyacheslav Dubeyko
67a99b7069SGeert Uytterhoeven hfs_dbg(ATTR_MOD, "init_hdr_attr_file: clump %u, node_size %u\n",
68099e9245SVyacheslav Dubeyko clump_size, node_size);
69099e9245SVyacheslav Dubeyko
70099e9245SVyacheslav Dubeyko /* The end of the node contains list of record offsets */
71099e9245SVyacheslav Dubeyko rec_offsets = (__be16 *)(buf + node_size);
72099e9245SVyacheslav Dubeyko
73099e9245SVyacheslav Dubeyko desc = (struct hfs_bnode_desc *)buf;
74099e9245SVyacheslav Dubeyko desc->type = HFS_NODE_HEADER;
75099e9245SVyacheslav Dubeyko desc->num_recs = cpu_to_be16(HFSPLUS_BTREE_HDR_NODE_RECS_COUNT);
76099e9245SVyacheslav Dubeyko offset = sizeof(struct hfs_bnode_desc);
77099e9245SVyacheslav Dubeyko *--rec_offsets = cpu_to_be16(offset);
78099e9245SVyacheslav Dubeyko
79099e9245SVyacheslav Dubeyko head = (struct hfs_btree_header_rec *)(buf + offset);
80099e9245SVyacheslav Dubeyko head->node_size = cpu_to_be16(node_size);
81a99b7069SGeert Uytterhoeven tmp = i_size_read(attr_file);
82a99b7069SGeert Uytterhoeven do_div(tmp, node_size);
83a99b7069SGeert Uytterhoeven head->node_count = cpu_to_be32(tmp);
84099e9245SVyacheslav Dubeyko head->free_nodes = cpu_to_be32(be32_to_cpu(head->node_count) - 1);
85099e9245SVyacheslav Dubeyko head->clump_size = cpu_to_be32(clump_size);
86099e9245SVyacheslav Dubeyko head->attributes |= cpu_to_be32(HFS_TREE_BIGKEYS | HFS_TREE_VARIDXKEYS);
87099e9245SVyacheslav Dubeyko head->max_key_len = cpu_to_be16(HFSPLUS_ATTR_KEYLEN - sizeof(u16));
88099e9245SVyacheslav Dubeyko offset += sizeof(struct hfs_btree_header_rec);
89099e9245SVyacheslav Dubeyko *--rec_offsets = cpu_to_be16(offset);
90099e9245SVyacheslav Dubeyko offset += HFSPLUS_BTREE_HDR_USER_BYTES;
91099e9245SVyacheslav Dubeyko *--rec_offsets = cpu_to_be16(offset);
92099e9245SVyacheslav Dubeyko
93099e9245SVyacheslav Dubeyko hdr_node_map_rec_bits = 8 * (node_size - offset - (4 * sizeof(u16)));
94099e9245SVyacheslav Dubeyko if (be32_to_cpu(head->node_count) > hdr_node_map_rec_bits) {
95099e9245SVyacheslav Dubeyko u32 map_node_bits;
96099e9245SVyacheslav Dubeyko u32 map_nodes;
97099e9245SVyacheslav Dubeyko
98099e9245SVyacheslav Dubeyko desc->next = cpu_to_be32(be32_to_cpu(head->leaf_tail) + 1);
99099e9245SVyacheslav Dubeyko map_node_bits = 8 * (node_size - sizeof(struct hfs_bnode_desc) -
100099e9245SVyacheslav Dubeyko (2 * sizeof(u16)) - 2);
101099e9245SVyacheslav Dubeyko map_nodes = (be32_to_cpu(head->node_count) -
102099e9245SVyacheslav Dubeyko hdr_node_map_rec_bits +
103099e9245SVyacheslav Dubeyko (map_node_bits - 1)) / map_node_bits;
104099e9245SVyacheslav Dubeyko be32_add_cpu(&head->free_nodes, 0 - map_nodes);
105099e9245SVyacheslav Dubeyko }
106099e9245SVyacheslav Dubeyko
107099e9245SVyacheslav Dubeyko bmp = buf + offset;
108099e9245SVyacheslav Dubeyko used_nodes =
109099e9245SVyacheslav Dubeyko be32_to_cpu(head->node_count) - be32_to_cpu(head->free_nodes);
110099e9245SVyacheslav Dubeyko used_bmp_bytes = used_nodes / 8;
111099e9245SVyacheslav Dubeyko if (used_bmp_bytes) {
112099e9245SVyacheslav Dubeyko memset(bmp, 0xFF, used_bmp_bytes);
113099e9245SVyacheslav Dubeyko bmp += used_bmp_bytes;
114099e9245SVyacheslav Dubeyko used_nodes %= 8;
115099e9245SVyacheslav Dubeyko }
116099e9245SVyacheslav Dubeyko *bmp = ~(0xFF >> used_nodes);
117099e9245SVyacheslav Dubeyko offset += hdr_node_map_rec_bits / 8;
118099e9245SVyacheslav Dubeyko *--rec_offsets = cpu_to_be16(offset);
119099e9245SVyacheslav Dubeyko }
120099e9245SVyacheslav Dubeyko
hfsplus_create_attributes_file(struct super_block * sb)12195e0d7dbSVyacheslav Dubeyko static int hfsplus_create_attributes_file(struct super_block *sb)
12295e0d7dbSVyacheslav Dubeyko {
12395e0d7dbSVyacheslav Dubeyko int err = 0;
12495e0d7dbSVyacheslav Dubeyko struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
12595e0d7dbSVyacheslav Dubeyko struct inode *attr_file;
12695e0d7dbSVyacheslav Dubeyko struct hfsplus_inode_info *hip;
12795e0d7dbSVyacheslav Dubeyko u32 clump_size;
12895e0d7dbSVyacheslav Dubeyko u16 node_size = HFSPLUS_ATTR_TREE_NODE_SIZE;
12995e0d7dbSVyacheslav Dubeyko char *buf;
13095e0d7dbSVyacheslav Dubeyko int index, written;
13195e0d7dbSVyacheslav Dubeyko struct address_space *mapping;
13295e0d7dbSVyacheslav Dubeyko struct page *page;
13395e0d7dbSVyacheslav Dubeyko int old_state = HFSPLUS_EMPTY_ATTR_TREE;
13495e0d7dbSVyacheslav Dubeyko
13595e0d7dbSVyacheslav Dubeyko hfs_dbg(ATTR_MOD, "create_attr_file: ino %d\n", HFSPLUS_ATTR_CNID);
13695e0d7dbSVyacheslav Dubeyko
13795e0d7dbSVyacheslav Dubeyko check_attr_tree_state_again:
13895e0d7dbSVyacheslav Dubeyko switch (atomic_read(&sbi->attr_tree_state)) {
13995e0d7dbSVyacheslav Dubeyko case HFSPLUS_EMPTY_ATTR_TREE:
14095e0d7dbSVyacheslav Dubeyko if (old_state != atomic_cmpxchg(&sbi->attr_tree_state,
14195e0d7dbSVyacheslav Dubeyko old_state,
14295e0d7dbSVyacheslav Dubeyko HFSPLUS_CREATING_ATTR_TREE))
14395e0d7dbSVyacheslav Dubeyko goto check_attr_tree_state_again;
14495e0d7dbSVyacheslav Dubeyko break;
14595e0d7dbSVyacheslav Dubeyko case HFSPLUS_CREATING_ATTR_TREE:
14695e0d7dbSVyacheslav Dubeyko /*
14795e0d7dbSVyacheslav Dubeyko * This state means that another thread is in process
14895e0d7dbSVyacheslav Dubeyko * of AttributesFile creation. Theoretically, it is
14995e0d7dbSVyacheslav Dubeyko * possible to be here. But really __setxattr() method
15095e0d7dbSVyacheslav Dubeyko * first of all calls hfs_find_init() for lookup in
15195e0d7dbSVyacheslav Dubeyko * B-tree of CatalogFile. This method locks mutex of
15295e0d7dbSVyacheslav Dubeyko * CatalogFile's B-tree. As a result, if some thread
15395e0d7dbSVyacheslav Dubeyko * is inside AttributedFile creation operation then
15495e0d7dbSVyacheslav Dubeyko * another threads will be waiting unlocking of
15595e0d7dbSVyacheslav Dubeyko * CatalogFile's B-tree's mutex. However, if code will
15695e0d7dbSVyacheslav Dubeyko * change then we will return error code (-EAGAIN) from
15795e0d7dbSVyacheslav Dubeyko * here. Really, it means that first try to set of xattr
15895e0d7dbSVyacheslav Dubeyko * fails with error but second attempt will have success.
15995e0d7dbSVyacheslav Dubeyko */
16095e0d7dbSVyacheslav Dubeyko return -EAGAIN;
16195e0d7dbSVyacheslav Dubeyko case HFSPLUS_VALID_ATTR_TREE:
16295e0d7dbSVyacheslav Dubeyko return 0;
16395e0d7dbSVyacheslav Dubeyko case HFSPLUS_FAILED_ATTR_TREE:
16495e0d7dbSVyacheslav Dubeyko return -EOPNOTSUPP;
16595e0d7dbSVyacheslav Dubeyko default:
16695e0d7dbSVyacheslav Dubeyko BUG();
16795e0d7dbSVyacheslav Dubeyko }
16895e0d7dbSVyacheslav Dubeyko
16995e0d7dbSVyacheslav Dubeyko attr_file = hfsplus_iget(sb, HFSPLUS_ATTR_CNID);
17095e0d7dbSVyacheslav Dubeyko if (IS_ERR(attr_file)) {
17195e0d7dbSVyacheslav Dubeyko pr_err("failed to load attributes file\n");
17295e0d7dbSVyacheslav Dubeyko return PTR_ERR(attr_file);
17395e0d7dbSVyacheslav Dubeyko }
17495e0d7dbSVyacheslav Dubeyko
17595e0d7dbSVyacheslav Dubeyko BUG_ON(i_size_read(attr_file) != 0);
17695e0d7dbSVyacheslav Dubeyko
17795e0d7dbSVyacheslav Dubeyko hip = HFSPLUS_I(attr_file);
17895e0d7dbSVyacheslav Dubeyko
17995e0d7dbSVyacheslav Dubeyko clump_size = hfsplus_calc_btree_clump_size(sb->s_blocksize,
18095e0d7dbSVyacheslav Dubeyko node_size,
18195e0d7dbSVyacheslav Dubeyko sbi->sect_count,
18295e0d7dbSVyacheslav Dubeyko HFSPLUS_ATTR_CNID);
18395e0d7dbSVyacheslav Dubeyko
18495e0d7dbSVyacheslav Dubeyko mutex_lock(&hip->extents_lock);
18595e0d7dbSVyacheslav Dubeyko hip->clump_blocks = clump_size >> sbi->alloc_blksz_shift;
18695e0d7dbSVyacheslav Dubeyko mutex_unlock(&hip->extents_lock);
18795e0d7dbSVyacheslav Dubeyko
18895e0d7dbSVyacheslav Dubeyko if (sbi->free_blocks <= (hip->clump_blocks << 1)) {
18995e0d7dbSVyacheslav Dubeyko err = -ENOSPC;
19095e0d7dbSVyacheslav Dubeyko goto end_attr_file_creation;
19195e0d7dbSVyacheslav Dubeyko }
19295e0d7dbSVyacheslav Dubeyko
19395e0d7dbSVyacheslav Dubeyko while (hip->alloc_blocks < hip->clump_blocks) {
1942cd282a1SSergei Antonov err = hfsplus_file_extend(attr_file, false);
19595e0d7dbSVyacheslav Dubeyko if (unlikely(err)) {
19695e0d7dbSVyacheslav Dubeyko pr_err("failed to extend attributes file\n");
19795e0d7dbSVyacheslav Dubeyko goto end_attr_file_creation;
19895e0d7dbSVyacheslav Dubeyko }
19995e0d7dbSVyacheslav Dubeyko hip->phys_size = attr_file->i_size =
20095e0d7dbSVyacheslav Dubeyko (loff_t)hip->alloc_blocks << sbi->alloc_blksz_shift;
20195e0d7dbSVyacheslav Dubeyko hip->fs_blocks = hip->alloc_blocks << sbi->fs_shift;
20295e0d7dbSVyacheslav Dubeyko inode_set_bytes(attr_file, attr_file->i_size);
20395e0d7dbSVyacheslav Dubeyko }
20495e0d7dbSVyacheslav Dubeyko
20595e0d7dbSVyacheslav Dubeyko buf = kzalloc(node_size, GFP_NOFS);
20695e0d7dbSVyacheslav Dubeyko if (!buf) {
20795e0d7dbSVyacheslav Dubeyko err = -ENOMEM;
20895e0d7dbSVyacheslav Dubeyko goto end_attr_file_creation;
20995e0d7dbSVyacheslav Dubeyko }
21095e0d7dbSVyacheslav Dubeyko
21195e0d7dbSVyacheslav Dubeyko hfsplus_init_header_node(attr_file, clump_size, buf, node_size);
21295e0d7dbSVyacheslav Dubeyko
21395e0d7dbSVyacheslav Dubeyko mapping = attr_file->i_mapping;
21495e0d7dbSVyacheslav Dubeyko
21595e0d7dbSVyacheslav Dubeyko index = 0;
21695e0d7dbSVyacheslav Dubeyko written = 0;
21709cbfeafSKirill A. Shutemov for (; written < node_size; index++, written += PAGE_SIZE) {
21895e0d7dbSVyacheslav Dubeyko void *kaddr;
21995e0d7dbSVyacheslav Dubeyko
22095e0d7dbSVyacheslav Dubeyko page = read_mapping_page(mapping, index, NULL);
22195e0d7dbSVyacheslav Dubeyko if (IS_ERR(page)) {
22295e0d7dbSVyacheslav Dubeyko err = PTR_ERR(page);
22395e0d7dbSVyacheslav Dubeyko goto failed_header_node_init;
22495e0d7dbSVyacheslav Dubeyko }
22595e0d7dbSVyacheslav Dubeyko
22695e0d7dbSVyacheslav Dubeyko kaddr = kmap_atomic(page);
22795e0d7dbSVyacheslav Dubeyko memcpy(kaddr, buf + written,
22809cbfeafSKirill A. Shutemov min_t(size_t, PAGE_SIZE, node_size - written));
22995e0d7dbSVyacheslav Dubeyko kunmap_atomic(kaddr);
23095e0d7dbSVyacheslav Dubeyko
23195e0d7dbSVyacheslav Dubeyko set_page_dirty(page);
23209cbfeafSKirill A. Shutemov put_page(page);
23395e0d7dbSVyacheslav Dubeyko }
23495e0d7dbSVyacheslav Dubeyko
23595e0d7dbSVyacheslav Dubeyko hfsplus_mark_inode_dirty(attr_file, HFSPLUS_I_ATTR_DIRTY);
23695e0d7dbSVyacheslav Dubeyko
23795e0d7dbSVyacheslav Dubeyko sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
23895e0d7dbSVyacheslav Dubeyko if (!sbi->attr_tree)
23995e0d7dbSVyacheslav Dubeyko pr_err("failed to load attributes file\n");
24095e0d7dbSVyacheslav Dubeyko
24195e0d7dbSVyacheslav Dubeyko failed_header_node_init:
24295e0d7dbSVyacheslav Dubeyko kfree(buf);
24395e0d7dbSVyacheslav Dubeyko
24495e0d7dbSVyacheslav Dubeyko end_attr_file_creation:
24595e0d7dbSVyacheslav Dubeyko iput(attr_file);
24695e0d7dbSVyacheslav Dubeyko
24795e0d7dbSVyacheslav Dubeyko if (!err)
24895e0d7dbSVyacheslav Dubeyko atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
24995e0d7dbSVyacheslav Dubeyko else if (err == -ENOSPC)
25095e0d7dbSVyacheslav Dubeyko atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
25195e0d7dbSVyacheslav Dubeyko else
25295e0d7dbSVyacheslav Dubeyko atomic_set(&sbi->attr_tree_state, HFSPLUS_FAILED_ATTR_TREE);
25395e0d7dbSVyacheslav Dubeyko
25495e0d7dbSVyacheslav Dubeyko return err;
25595e0d7dbSVyacheslav Dubeyko }
25695e0d7dbSVyacheslav Dubeyko
__hfsplus_setxattr(struct inode * inode,const char * name,const void * value,size_t size,int flags)257127e5f5aSVyacheslav Dubeyko int __hfsplus_setxattr(struct inode *inode, const char *name,
258127e5f5aSVyacheslav Dubeyko const void *value, size_t size, int flags)
259127e5f5aSVyacheslav Dubeyko {
2601b2fde2dSXU pengfei int err;
261127e5f5aSVyacheslav Dubeyko struct hfs_find_data cat_fd;
262127e5f5aSVyacheslav Dubeyko hfsplus_cat_entry entry;
263127e5f5aSVyacheslav Dubeyko u16 cat_entry_flags, cat_entry_type;
264127e5f5aSVyacheslav Dubeyko u16 folder_finderinfo_len = sizeof(struct DInfo) +
265127e5f5aSVyacheslav Dubeyko sizeof(struct DXInfo);
266127e5f5aSVyacheslav Dubeyko u16 file_finderinfo_len = sizeof(struct FInfo) +
267127e5f5aSVyacheslav Dubeyko sizeof(struct FXInfo);
268127e5f5aSVyacheslav Dubeyko
269127e5f5aSVyacheslav Dubeyko if ((!S_ISREG(inode->i_mode) &&
270127e5f5aSVyacheslav Dubeyko !S_ISDIR(inode->i_mode)) ||
271127e5f5aSVyacheslav Dubeyko HFSPLUS_IS_RSRC(inode))
272127e5f5aSVyacheslav Dubeyko return -EOPNOTSUPP;
273127e5f5aSVyacheslav Dubeyko
274b168fff7SChristoph Hellwig if (value == NULL)
275b168fff7SChristoph Hellwig return hfsplus_removexattr(inode, name);
276127e5f5aSVyacheslav Dubeyko
277127e5f5aSVyacheslav Dubeyko err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
278127e5f5aSVyacheslav Dubeyko if (err) {
279d6142673SJoe Perches pr_err("can't init xattr find struct\n");
280127e5f5aSVyacheslav Dubeyko return err;
281127e5f5aSVyacheslav Dubeyko }
282127e5f5aSVyacheslav Dubeyko
283127e5f5aSVyacheslav Dubeyko err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
284127e5f5aSVyacheslav Dubeyko if (err) {
285d6142673SJoe Perches pr_err("catalog searching failed\n");
286127e5f5aSVyacheslav Dubeyko goto end_setxattr;
287127e5f5aSVyacheslav Dubeyko }
288127e5f5aSVyacheslav Dubeyko
289127e5f5aSVyacheslav Dubeyko if (!strcmp_xattr_finder_info(name)) {
290127e5f5aSVyacheslav Dubeyko if (flags & XATTR_CREATE) {
291d6142673SJoe Perches pr_err("xattr exists yet\n");
292127e5f5aSVyacheslav Dubeyko err = -EOPNOTSUPP;
293127e5f5aSVyacheslav Dubeyko goto end_setxattr;
294127e5f5aSVyacheslav Dubeyko }
295127e5f5aSVyacheslav Dubeyko hfs_bnode_read(cat_fd.bnode, &entry, cat_fd.entryoffset,
296127e5f5aSVyacheslav Dubeyko sizeof(hfsplus_cat_entry));
297127e5f5aSVyacheslav Dubeyko if (be16_to_cpu(entry.type) == HFSPLUS_FOLDER) {
298127e5f5aSVyacheslav Dubeyko if (size == folder_finderinfo_len) {
299e35fa567SKees Cook memcpy(&entry.folder.info, value,
300127e5f5aSVyacheslav Dubeyko folder_finderinfo_len);
301127e5f5aSVyacheslav Dubeyko hfs_bnode_write(cat_fd.bnode, &entry,
302127e5f5aSVyacheslav Dubeyko cat_fd.entryoffset,
303127e5f5aSVyacheslav Dubeyko sizeof(struct hfsplus_cat_folder));
304127e5f5aSVyacheslav Dubeyko hfsplus_mark_inode_dirty(inode,
305127e5f5aSVyacheslav Dubeyko HFSPLUS_I_CAT_DIRTY);
306127e5f5aSVyacheslav Dubeyko } else {
307127e5f5aSVyacheslav Dubeyko err = -ERANGE;
308127e5f5aSVyacheslav Dubeyko goto end_setxattr;
309127e5f5aSVyacheslav Dubeyko }
310127e5f5aSVyacheslav Dubeyko } else if (be16_to_cpu(entry.type) == HFSPLUS_FILE) {
311127e5f5aSVyacheslav Dubeyko if (size == file_finderinfo_len) {
312e35fa567SKees Cook memcpy(&entry.file.info, value,
313127e5f5aSVyacheslav Dubeyko file_finderinfo_len);
314127e5f5aSVyacheslav Dubeyko hfs_bnode_write(cat_fd.bnode, &entry,
315127e5f5aSVyacheslav Dubeyko cat_fd.entryoffset,
316127e5f5aSVyacheslav Dubeyko sizeof(struct hfsplus_cat_file));
317127e5f5aSVyacheslav Dubeyko hfsplus_mark_inode_dirty(inode,
318127e5f5aSVyacheslav Dubeyko HFSPLUS_I_CAT_DIRTY);
319127e5f5aSVyacheslav Dubeyko } else {
320127e5f5aSVyacheslav Dubeyko err = -ERANGE;
321127e5f5aSVyacheslav Dubeyko goto end_setxattr;
322127e5f5aSVyacheslav Dubeyko }
323127e5f5aSVyacheslav Dubeyko } else {
324127e5f5aSVyacheslav Dubeyko err = -EOPNOTSUPP;
325127e5f5aSVyacheslav Dubeyko goto end_setxattr;
326127e5f5aSVyacheslav Dubeyko }
327127e5f5aSVyacheslav Dubeyko goto end_setxattr;
328127e5f5aSVyacheslav Dubeyko }
329127e5f5aSVyacheslav Dubeyko
330127e5f5aSVyacheslav Dubeyko if (!HFSPLUS_SB(inode->i_sb)->attr_tree) {
33195e0d7dbSVyacheslav Dubeyko err = hfsplus_create_attributes_file(inode->i_sb);
33295e0d7dbSVyacheslav Dubeyko if (unlikely(err))
333127e5f5aSVyacheslav Dubeyko goto end_setxattr;
334127e5f5aSVyacheslav Dubeyko }
335127e5f5aSVyacheslav Dubeyko
336127e5f5aSVyacheslav Dubeyko if (hfsplus_attr_exists(inode, name)) {
337127e5f5aSVyacheslav Dubeyko if (flags & XATTR_CREATE) {
338d6142673SJoe Perches pr_err("xattr exists yet\n");
339127e5f5aSVyacheslav Dubeyko err = -EOPNOTSUPP;
340127e5f5aSVyacheslav Dubeyko goto end_setxattr;
341127e5f5aSVyacheslav Dubeyko }
342127e5f5aSVyacheslav Dubeyko err = hfsplus_delete_attr(inode, name);
343127e5f5aSVyacheslav Dubeyko if (err)
344127e5f5aSVyacheslav Dubeyko goto end_setxattr;
345127e5f5aSVyacheslav Dubeyko err = hfsplus_create_attr(inode, name, value, size);
346127e5f5aSVyacheslav Dubeyko if (err)
347127e5f5aSVyacheslav Dubeyko goto end_setxattr;
348127e5f5aSVyacheslav Dubeyko } else {
349127e5f5aSVyacheslav Dubeyko if (flags & XATTR_REPLACE) {
350d6142673SJoe Perches pr_err("cannot replace xattr\n");
351127e5f5aSVyacheslav Dubeyko err = -EOPNOTSUPP;
352127e5f5aSVyacheslav Dubeyko goto end_setxattr;
353127e5f5aSVyacheslav Dubeyko }
354127e5f5aSVyacheslav Dubeyko err = hfsplus_create_attr(inode, name, value, size);
355127e5f5aSVyacheslav Dubeyko if (err)
356127e5f5aSVyacheslav Dubeyko goto end_setxattr;
357127e5f5aSVyacheslav Dubeyko }
358127e5f5aSVyacheslav Dubeyko
359127e5f5aSVyacheslav Dubeyko cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
360127e5f5aSVyacheslav Dubeyko if (cat_entry_type == HFSPLUS_FOLDER) {
361127e5f5aSVyacheslav Dubeyko cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
362127e5f5aSVyacheslav Dubeyko cat_fd.entryoffset +
363127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_folder, flags));
364127e5f5aSVyacheslav Dubeyko cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
365127e5f5aSVyacheslav Dubeyko if (!strcmp_xattr_acl(name))
366127e5f5aSVyacheslav Dubeyko cat_entry_flags |= HFSPLUS_ACL_EXISTS;
367127e5f5aSVyacheslav Dubeyko hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
368127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_folder, flags),
369127e5f5aSVyacheslav Dubeyko cat_entry_flags);
370127e5f5aSVyacheslav Dubeyko hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
371127e5f5aSVyacheslav Dubeyko } else if (cat_entry_type == HFSPLUS_FILE) {
372127e5f5aSVyacheslav Dubeyko cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
373127e5f5aSVyacheslav Dubeyko cat_fd.entryoffset +
374127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_file, flags));
375127e5f5aSVyacheslav Dubeyko cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
376127e5f5aSVyacheslav Dubeyko if (!strcmp_xattr_acl(name))
377127e5f5aSVyacheslav Dubeyko cat_entry_flags |= HFSPLUS_ACL_EXISTS;
378127e5f5aSVyacheslav Dubeyko hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
379127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_file, flags),
380127e5f5aSVyacheslav Dubeyko cat_entry_flags);
381127e5f5aSVyacheslav Dubeyko hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
382127e5f5aSVyacheslav Dubeyko } else {
383d6142673SJoe Perches pr_err("invalid catalog entry type\n");
384127e5f5aSVyacheslav Dubeyko err = -EIO;
385127e5f5aSVyacheslav Dubeyko goto end_setxattr;
386127e5f5aSVyacheslav Dubeyko }
387127e5f5aSVyacheslav Dubeyko
388127e5f5aSVyacheslav Dubeyko end_setxattr:
389127e5f5aSVyacheslav Dubeyko hfs_find_exit(&cat_fd);
390127e5f5aSVyacheslav Dubeyko return err;
391127e5f5aSVyacheslav Dubeyko }
392127e5f5aSVyacheslav Dubeyko
name_len(const char * xattr_name,int xattr_name_len)393127e5f5aSVyacheslav Dubeyko static int name_len(const char *xattr_name, int xattr_name_len)
394127e5f5aSVyacheslav Dubeyko {
395127e5f5aSVyacheslav Dubeyko int len = xattr_name_len + 1;
396127e5f5aSVyacheslav Dubeyko
397b168fff7SChristoph Hellwig if (!is_known_namespace(xattr_name))
398127e5f5aSVyacheslav Dubeyko len += XATTR_MAC_OSX_PREFIX_LEN;
399127e5f5aSVyacheslav Dubeyko
400127e5f5aSVyacheslav Dubeyko return len;
401127e5f5aSVyacheslav Dubeyko }
402127e5f5aSVyacheslav Dubeyko
copy_name(char * buffer,const char * xattr_name,int name_len)403127e5f5aSVyacheslav Dubeyko static int copy_name(char *buffer, const char *xattr_name, int name_len)
404127e5f5aSVyacheslav Dubeyko {
405127e5f5aSVyacheslav Dubeyko int len = name_len;
406127e5f5aSVyacheslav Dubeyko int offset = 0;
407127e5f5aSVyacheslav Dubeyko
408b168fff7SChristoph Hellwig if (!is_known_namespace(xattr_name)) {
40929774f3fSMathieu Malaterre memcpy(buffer, XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN);
410127e5f5aSVyacheslav Dubeyko offset += XATTR_MAC_OSX_PREFIX_LEN;
411127e5f5aSVyacheslav Dubeyko len += XATTR_MAC_OSX_PREFIX_LEN;
412127e5f5aSVyacheslav Dubeyko }
413127e5f5aSVyacheslav Dubeyko
414127e5f5aSVyacheslav Dubeyko strncpy(buffer + offset, xattr_name, name_len);
415127e5f5aSVyacheslav Dubeyko memset(buffer + offset + name_len, 0, 1);
416127e5f5aSVyacheslav Dubeyko len += 1;
417127e5f5aSVyacheslav Dubeyko
418127e5f5aSVyacheslav Dubeyko return len;
419127e5f5aSVyacheslav Dubeyko }
420127e5f5aSVyacheslav Dubeyko
hfsplus_setxattr(struct inode * inode,const char * name,const void * value,size_t size,int flags,const char * prefix,size_t prefixlen)42159301226SAl Viro int hfsplus_setxattr(struct inode *inode, const char *name,
4225e61473eSFabian Frederick const void *value, size_t size, int flags,
4235e61473eSFabian Frederick const char *prefix, size_t prefixlen)
4245e61473eSFabian Frederick {
4255e61473eSFabian Frederick char *xattr_name;
4265e61473eSFabian Frederick int res;
4275e61473eSFabian Frederick
4285e61473eSFabian Frederick xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
4295e61473eSFabian Frederick GFP_KERNEL);
4305e61473eSFabian Frederick if (!xattr_name)
4315e61473eSFabian Frederick return -ENOMEM;
4325e61473eSFabian Frederick strcpy(xattr_name, prefix);
4335e61473eSFabian Frederick strcpy(xattr_name + prefixlen, name);
43459301226SAl Viro res = __hfsplus_setxattr(inode, xattr_name, value, size, flags);
4355e61473eSFabian Frederick kfree(xattr_name);
4365e61473eSFabian Frederick return res;
4375e61473eSFabian Frederick }
4385e61473eSFabian Frederick
hfsplus_getxattr_finder_info(struct inode * inode,void * value,size_t size)439b4c1107cSVyacheslav Dubeyko static ssize_t hfsplus_getxattr_finder_info(struct inode *inode,
440127e5f5aSVyacheslav Dubeyko void *value, size_t size)
441127e5f5aSVyacheslav Dubeyko {
442127e5f5aSVyacheslav Dubeyko ssize_t res = 0;
443127e5f5aSVyacheslav Dubeyko struct hfs_find_data fd;
444127e5f5aSVyacheslav Dubeyko u16 entry_type;
445127e5f5aSVyacheslav Dubeyko u16 folder_rec_len = sizeof(struct DInfo) + sizeof(struct DXInfo);
446127e5f5aSVyacheslav Dubeyko u16 file_rec_len = sizeof(struct FInfo) + sizeof(struct FXInfo);
447127e5f5aSVyacheslav Dubeyko u16 record_len = max(folder_rec_len, file_rec_len);
448127e5f5aSVyacheslav Dubeyko u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
449127e5f5aSVyacheslav Dubeyko u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
450127e5f5aSVyacheslav Dubeyko
451127e5f5aSVyacheslav Dubeyko if (size >= record_len) {
452127e5f5aSVyacheslav Dubeyko res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
453127e5f5aSVyacheslav Dubeyko if (res) {
454d6142673SJoe Perches pr_err("can't init xattr find struct\n");
455127e5f5aSVyacheslav Dubeyko return res;
456127e5f5aSVyacheslav Dubeyko }
457127e5f5aSVyacheslav Dubeyko res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
458127e5f5aSVyacheslav Dubeyko if (res)
459127e5f5aSVyacheslav Dubeyko goto end_getxattr_finder_info;
460127e5f5aSVyacheslav Dubeyko entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
461127e5f5aSVyacheslav Dubeyko
462127e5f5aSVyacheslav Dubeyko if (entry_type == HFSPLUS_FOLDER) {
463127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, folder_finder_info,
464127e5f5aSVyacheslav Dubeyko fd.entryoffset +
465127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_folder, user_info),
466127e5f5aSVyacheslav Dubeyko folder_rec_len);
467127e5f5aSVyacheslav Dubeyko memcpy(value, folder_finder_info, folder_rec_len);
468127e5f5aSVyacheslav Dubeyko res = folder_rec_len;
469127e5f5aSVyacheslav Dubeyko } else if (entry_type == HFSPLUS_FILE) {
470127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, file_finder_info,
471127e5f5aSVyacheslav Dubeyko fd.entryoffset +
472127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_file, user_info),
473127e5f5aSVyacheslav Dubeyko file_rec_len);
474127e5f5aSVyacheslav Dubeyko memcpy(value, file_finder_info, file_rec_len);
475127e5f5aSVyacheslav Dubeyko res = file_rec_len;
476127e5f5aSVyacheslav Dubeyko } else {
477127e5f5aSVyacheslav Dubeyko res = -EOPNOTSUPP;
478127e5f5aSVyacheslav Dubeyko goto end_getxattr_finder_info;
479127e5f5aSVyacheslav Dubeyko }
480127e5f5aSVyacheslav Dubeyko } else
481127e5f5aSVyacheslav Dubeyko res = size ? -ERANGE : record_len;
482127e5f5aSVyacheslav Dubeyko
483127e5f5aSVyacheslav Dubeyko end_getxattr_finder_info:
484127e5f5aSVyacheslav Dubeyko if (size >= record_len)
485127e5f5aSVyacheslav Dubeyko hfs_find_exit(&fd);
486127e5f5aSVyacheslav Dubeyko return res;
487127e5f5aSVyacheslav Dubeyko }
488127e5f5aSVyacheslav Dubeyko
__hfsplus_getxattr(struct inode * inode,const char * name,void * value,size_t size)489b4c1107cSVyacheslav Dubeyko ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
490127e5f5aSVyacheslav Dubeyko void *value, size_t size)
491127e5f5aSVyacheslav Dubeyko {
492127e5f5aSVyacheslav Dubeyko struct hfs_find_data fd;
493127e5f5aSVyacheslav Dubeyko hfsplus_attr_entry *entry;
494127e5f5aSVyacheslav Dubeyko __be32 xattr_record_type;
495127e5f5aSVyacheslav Dubeyko u32 record_type;
496127e5f5aSVyacheslav Dubeyko u16 record_length = 0;
4971b2fde2dSXU pengfei ssize_t res;
498127e5f5aSVyacheslav Dubeyko
499127e5f5aSVyacheslav Dubeyko if ((!S_ISREG(inode->i_mode) &&
500127e5f5aSVyacheslav Dubeyko !S_ISDIR(inode->i_mode)) ||
501127e5f5aSVyacheslav Dubeyko HFSPLUS_IS_RSRC(inode))
502127e5f5aSVyacheslav Dubeyko return -EOPNOTSUPP;
503127e5f5aSVyacheslav Dubeyko
504127e5f5aSVyacheslav Dubeyko if (!strcmp_xattr_finder_info(name))
505b4c1107cSVyacheslav Dubeyko return hfsplus_getxattr_finder_info(inode, value, size);
506127e5f5aSVyacheslav Dubeyko
507127e5f5aSVyacheslav Dubeyko if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
508127e5f5aSVyacheslav Dubeyko return -EOPNOTSUPP;
509127e5f5aSVyacheslav Dubeyko
510127e5f5aSVyacheslav Dubeyko entry = hfsplus_alloc_attr_entry();
511127e5f5aSVyacheslav Dubeyko if (!entry) {
512d6142673SJoe Perches pr_err("can't allocate xattr entry\n");
513127e5f5aSVyacheslav Dubeyko return -ENOMEM;
514127e5f5aSVyacheslav Dubeyko }
515127e5f5aSVyacheslav Dubeyko
516127e5f5aSVyacheslav Dubeyko res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
517127e5f5aSVyacheslav Dubeyko if (res) {
518d6142673SJoe Perches pr_err("can't init xattr find struct\n");
519127e5f5aSVyacheslav Dubeyko goto failed_getxattr_init;
520127e5f5aSVyacheslav Dubeyko }
521127e5f5aSVyacheslav Dubeyko
522127e5f5aSVyacheslav Dubeyko res = hfsplus_find_attr(inode->i_sb, inode->i_ino, name, &fd);
523127e5f5aSVyacheslav Dubeyko if (res) {
524127e5f5aSVyacheslav Dubeyko if (res == -ENOENT)
525127e5f5aSVyacheslav Dubeyko res = -ENODATA;
526127e5f5aSVyacheslav Dubeyko else
527d6142673SJoe Perches pr_err("xattr searching failed\n");
528127e5f5aSVyacheslav Dubeyko goto out;
529127e5f5aSVyacheslav Dubeyko }
530127e5f5aSVyacheslav Dubeyko
531127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, &xattr_record_type,
532127e5f5aSVyacheslav Dubeyko fd.entryoffset, sizeof(xattr_record_type));
533127e5f5aSVyacheslav Dubeyko record_type = be32_to_cpu(xattr_record_type);
534127e5f5aSVyacheslav Dubeyko if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
535127e5f5aSVyacheslav Dubeyko record_length = hfs_bnode_read_u16(fd.bnode,
536127e5f5aSVyacheslav Dubeyko fd.entryoffset +
537127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_attr_inline_data,
538127e5f5aSVyacheslav Dubeyko length));
539127e5f5aSVyacheslav Dubeyko if (record_length > HFSPLUS_MAX_INLINE_DATA_SIZE) {
540d6142673SJoe Perches pr_err("invalid xattr record size\n");
541127e5f5aSVyacheslav Dubeyko res = -EIO;
542127e5f5aSVyacheslav Dubeyko goto out;
543127e5f5aSVyacheslav Dubeyko }
544127e5f5aSVyacheslav Dubeyko } else if (record_type == HFSPLUS_ATTR_FORK_DATA ||
545127e5f5aSVyacheslav Dubeyko record_type == HFSPLUS_ATTR_EXTENTS) {
546d6142673SJoe Perches pr_err("only inline data xattr are supported\n");
547127e5f5aSVyacheslav Dubeyko res = -EOPNOTSUPP;
548127e5f5aSVyacheslav Dubeyko goto out;
549127e5f5aSVyacheslav Dubeyko } else {
550d6142673SJoe Perches pr_err("invalid xattr record\n");
551127e5f5aSVyacheslav Dubeyko res = -EIO;
552127e5f5aSVyacheslav Dubeyko goto out;
553127e5f5aSVyacheslav Dubeyko }
554127e5f5aSVyacheslav Dubeyko
555127e5f5aSVyacheslav Dubeyko if (size) {
556127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, entry, fd.entryoffset,
557127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_attr_inline_data,
558127e5f5aSVyacheslav Dubeyko raw_bytes) + record_length);
559127e5f5aSVyacheslav Dubeyko }
560127e5f5aSVyacheslav Dubeyko
561127e5f5aSVyacheslav Dubeyko if (size >= record_length) {
562127e5f5aSVyacheslav Dubeyko memcpy(value, entry->inline_data.raw_bytes, record_length);
563127e5f5aSVyacheslav Dubeyko res = record_length;
564127e5f5aSVyacheslav Dubeyko } else
565127e5f5aSVyacheslav Dubeyko res = size ? -ERANGE : record_length;
566127e5f5aSVyacheslav Dubeyko
567127e5f5aSVyacheslav Dubeyko out:
568127e5f5aSVyacheslav Dubeyko hfs_find_exit(&fd);
569127e5f5aSVyacheslav Dubeyko
570127e5f5aSVyacheslav Dubeyko failed_getxattr_init:
571127e5f5aSVyacheslav Dubeyko hfsplus_destroy_attr_entry(entry);
572127e5f5aSVyacheslav Dubeyko return res;
573127e5f5aSVyacheslav Dubeyko }
574127e5f5aSVyacheslav Dubeyko
hfsplus_getxattr(struct inode * inode,const char * name,void * value,size_t size,const char * prefix,size_t prefixlen)575b296821aSAl Viro ssize_t hfsplus_getxattr(struct inode *inode, const char *name,
576a3cef4cdSFabian Frederick void *value, size_t size,
577a3cef4cdSFabian Frederick const char *prefix, size_t prefixlen)
578a3cef4cdSFabian Frederick {
579a3cef4cdSFabian Frederick int res;
580a3cef4cdSFabian Frederick char *xattr_name;
581a3cef4cdSFabian Frederick
582a3cef4cdSFabian Frederick xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
583a3cef4cdSFabian Frederick GFP_KERNEL);
584a3cef4cdSFabian Frederick if (!xattr_name)
585a3cef4cdSFabian Frederick return -ENOMEM;
586a3cef4cdSFabian Frederick
587a3cef4cdSFabian Frederick strcpy(xattr_name, prefix);
588a3cef4cdSFabian Frederick strcpy(xattr_name + prefixlen, name);
589a3cef4cdSFabian Frederick
590b296821aSAl Viro res = __hfsplus_getxattr(inode, xattr_name, value, size);
591a3cef4cdSFabian Frederick kfree(xattr_name);
592a3cef4cdSFabian Frederick return res;
593a3cef4cdSFabian Frederick
594a3cef4cdSFabian Frederick }
595a3cef4cdSFabian Frederick
can_list(const char * xattr_name)596127e5f5aSVyacheslav Dubeyko static inline int can_list(const char *xattr_name)
597127e5f5aSVyacheslav Dubeyko {
598127e5f5aSVyacheslav Dubeyko if (!xattr_name)
599127e5f5aSVyacheslav Dubeyko return 0;
600127e5f5aSVyacheslav Dubeyko
601127e5f5aSVyacheslav Dubeyko return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
602127e5f5aSVyacheslav Dubeyko XATTR_TRUSTED_PREFIX_LEN) ||
603127e5f5aSVyacheslav Dubeyko capable(CAP_SYS_ADMIN);
604127e5f5aSVyacheslav Dubeyko }
605127e5f5aSVyacheslav Dubeyko
hfsplus_listxattr_finder_info(struct dentry * dentry,char * buffer,size_t size)606127e5f5aSVyacheslav Dubeyko static ssize_t hfsplus_listxattr_finder_info(struct dentry *dentry,
607127e5f5aSVyacheslav Dubeyko char *buffer, size_t size)
608127e5f5aSVyacheslav Dubeyko {
6091b2fde2dSXU pengfei ssize_t res;
6102b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
611127e5f5aSVyacheslav Dubeyko struct hfs_find_data fd;
612127e5f5aSVyacheslav Dubeyko u16 entry_type;
613127e5f5aSVyacheslav Dubeyko u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
614127e5f5aSVyacheslav Dubeyko u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
615127e5f5aSVyacheslav Dubeyko unsigned long len, found_bit;
616127e5f5aSVyacheslav Dubeyko int xattr_name_len, symbols_count;
617127e5f5aSVyacheslav Dubeyko
618127e5f5aSVyacheslav Dubeyko res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
619127e5f5aSVyacheslav Dubeyko if (res) {
620d6142673SJoe Perches pr_err("can't init xattr find struct\n");
621127e5f5aSVyacheslav Dubeyko return res;
622127e5f5aSVyacheslav Dubeyko }
623127e5f5aSVyacheslav Dubeyko
624127e5f5aSVyacheslav Dubeyko res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
625127e5f5aSVyacheslav Dubeyko if (res)
626127e5f5aSVyacheslav Dubeyko goto end_listxattr_finder_info;
627127e5f5aSVyacheslav Dubeyko
628127e5f5aSVyacheslav Dubeyko entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
629127e5f5aSVyacheslav Dubeyko if (entry_type == HFSPLUS_FOLDER) {
630127e5f5aSVyacheslav Dubeyko len = sizeof(struct DInfo) + sizeof(struct DXInfo);
631127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, folder_finder_info,
632127e5f5aSVyacheslav Dubeyko fd.entryoffset +
633127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_folder, user_info),
634127e5f5aSVyacheslav Dubeyko len);
635127e5f5aSVyacheslav Dubeyko found_bit = find_first_bit((void *)folder_finder_info, len*8);
636127e5f5aSVyacheslav Dubeyko } else if (entry_type == HFSPLUS_FILE) {
637127e5f5aSVyacheslav Dubeyko len = sizeof(struct FInfo) + sizeof(struct FXInfo);
638127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, file_finder_info,
639127e5f5aSVyacheslav Dubeyko fd.entryoffset +
640127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_file, user_info),
641127e5f5aSVyacheslav Dubeyko len);
642127e5f5aSVyacheslav Dubeyko found_bit = find_first_bit((void *)file_finder_info, len*8);
643127e5f5aSVyacheslav Dubeyko } else {
644127e5f5aSVyacheslav Dubeyko res = -EOPNOTSUPP;
645127e5f5aSVyacheslav Dubeyko goto end_listxattr_finder_info;
646127e5f5aSVyacheslav Dubeyko }
647127e5f5aSVyacheslav Dubeyko
648127e5f5aSVyacheslav Dubeyko if (found_bit >= (len*8))
649127e5f5aSVyacheslav Dubeyko res = 0;
650127e5f5aSVyacheslav Dubeyko else {
651127e5f5aSVyacheslav Dubeyko symbols_count = sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME) - 1;
652127e5f5aSVyacheslav Dubeyko xattr_name_len =
653127e5f5aSVyacheslav Dubeyko name_len(HFSPLUS_XATTR_FINDER_INFO_NAME, symbols_count);
654127e5f5aSVyacheslav Dubeyko if (!buffer || !size) {
655127e5f5aSVyacheslav Dubeyko if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME))
656127e5f5aSVyacheslav Dubeyko res = xattr_name_len;
657127e5f5aSVyacheslav Dubeyko } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME)) {
658127e5f5aSVyacheslav Dubeyko if (size < xattr_name_len)
659127e5f5aSVyacheslav Dubeyko res = -ERANGE;
660127e5f5aSVyacheslav Dubeyko else {
661127e5f5aSVyacheslav Dubeyko res = copy_name(buffer,
662127e5f5aSVyacheslav Dubeyko HFSPLUS_XATTR_FINDER_INFO_NAME,
663127e5f5aSVyacheslav Dubeyko symbols_count);
664127e5f5aSVyacheslav Dubeyko }
665127e5f5aSVyacheslav Dubeyko }
666127e5f5aSVyacheslav Dubeyko }
667127e5f5aSVyacheslav Dubeyko
668127e5f5aSVyacheslav Dubeyko end_listxattr_finder_info:
669127e5f5aSVyacheslav Dubeyko hfs_find_exit(&fd);
670127e5f5aSVyacheslav Dubeyko
671127e5f5aSVyacheslav Dubeyko return res;
672127e5f5aSVyacheslav Dubeyko }
673127e5f5aSVyacheslav Dubeyko
hfsplus_listxattr(struct dentry * dentry,char * buffer,size_t size)674127e5f5aSVyacheslav Dubeyko ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
675127e5f5aSVyacheslav Dubeyko {
676127e5f5aSVyacheslav Dubeyko ssize_t err;
6771b2fde2dSXU pengfei ssize_t res;
6782b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
679127e5f5aSVyacheslav Dubeyko struct hfs_find_data fd;
680127e5f5aSVyacheslav Dubeyko struct hfsplus_attr_key attr_key;
681017f8da4SHin-Tak Leung char *strbuf;
682127e5f5aSVyacheslav Dubeyko int xattr_name_len;
683127e5f5aSVyacheslav Dubeyko
684127e5f5aSVyacheslav Dubeyko if ((!S_ISREG(inode->i_mode) &&
685127e5f5aSVyacheslav Dubeyko !S_ISDIR(inode->i_mode)) ||
686127e5f5aSVyacheslav Dubeyko HFSPLUS_IS_RSRC(inode))
687127e5f5aSVyacheslav Dubeyko return -EOPNOTSUPP;
688127e5f5aSVyacheslav Dubeyko
689127e5f5aSVyacheslav Dubeyko res = hfsplus_listxattr_finder_info(dentry, buffer, size);
690127e5f5aSVyacheslav Dubeyko if (res < 0)
691127e5f5aSVyacheslav Dubeyko return res;
692127e5f5aSVyacheslav Dubeyko else if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
693127e5f5aSVyacheslav Dubeyko return (res == 0) ? -EOPNOTSUPP : res;
694127e5f5aSVyacheslav Dubeyko
695127e5f5aSVyacheslav Dubeyko err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
696127e5f5aSVyacheslav Dubeyko if (err) {
697d6142673SJoe Perches pr_err("can't init xattr find struct\n");
698127e5f5aSVyacheslav Dubeyko return err;
699127e5f5aSVyacheslav Dubeyko }
700127e5f5aSVyacheslav Dubeyko
701*f08956d8SEdward Adam Davis strbuf = kzalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN +
702017f8da4SHin-Tak Leung XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
703017f8da4SHin-Tak Leung if (!strbuf) {
704017f8da4SHin-Tak Leung res = -ENOMEM;
705017f8da4SHin-Tak Leung goto out;
706017f8da4SHin-Tak Leung }
707017f8da4SHin-Tak Leung
708127e5f5aSVyacheslav Dubeyko err = hfsplus_find_attr(inode->i_sb, inode->i_ino, NULL, &fd);
709127e5f5aSVyacheslav Dubeyko if (err) {
710127e5f5aSVyacheslav Dubeyko if (err == -ENOENT) {
711127e5f5aSVyacheslav Dubeyko if (res == 0)
712127e5f5aSVyacheslav Dubeyko res = -ENODATA;
713127e5f5aSVyacheslav Dubeyko goto end_listxattr;
714127e5f5aSVyacheslav Dubeyko } else {
715127e5f5aSVyacheslav Dubeyko res = err;
716127e5f5aSVyacheslav Dubeyko goto end_listxattr;
717127e5f5aSVyacheslav Dubeyko }
718127e5f5aSVyacheslav Dubeyko }
719127e5f5aSVyacheslav Dubeyko
720127e5f5aSVyacheslav Dubeyko for (;;) {
7211b2fde2dSXU pengfei u16 key_len = hfs_bnode_read_u16(fd.bnode, fd.keyoffset);
7221b2fde2dSXU pengfei
723127e5f5aSVyacheslav Dubeyko if (key_len == 0 || key_len > fd.tree->max_key_len) {
724d6142673SJoe Perches pr_err("invalid xattr key length: %d\n", key_len);
725127e5f5aSVyacheslav Dubeyko res = -EIO;
726127e5f5aSVyacheslav Dubeyko goto end_listxattr;
727127e5f5aSVyacheslav Dubeyko }
728127e5f5aSVyacheslav Dubeyko
729127e5f5aSVyacheslav Dubeyko hfs_bnode_read(fd.bnode, &attr_key,
730127e5f5aSVyacheslav Dubeyko fd.keyoffset, key_len + sizeof(key_len));
731127e5f5aSVyacheslav Dubeyko
732127e5f5aSVyacheslav Dubeyko if (be32_to_cpu(attr_key.cnid) != inode->i_ino)
733127e5f5aSVyacheslav Dubeyko goto end_listxattr;
734127e5f5aSVyacheslav Dubeyko
735017f8da4SHin-Tak Leung xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN;
736127e5f5aSVyacheslav Dubeyko if (hfsplus_uni2asc(inode->i_sb,
737127e5f5aSVyacheslav Dubeyko (const struct hfsplus_unistr *)&fd.key->attr.key_name,
738127e5f5aSVyacheslav Dubeyko strbuf, &xattr_name_len)) {
739d6142673SJoe Perches pr_err("unicode conversion failed\n");
740127e5f5aSVyacheslav Dubeyko res = -EIO;
741127e5f5aSVyacheslav Dubeyko goto end_listxattr;
742127e5f5aSVyacheslav Dubeyko }
743127e5f5aSVyacheslav Dubeyko
744127e5f5aSVyacheslav Dubeyko if (!buffer || !size) {
745127e5f5aSVyacheslav Dubeyko if (can_list(strbuf))
746127e5f5aSVyacheslav Dubeyko res += name_len(strbuf, xattr_name_len);
747127e5f5aSVyacheslav Dubeyko } else if (can_list(strbuf)) {
748127e5f5aSVyacheslav Dubeyko if (size < (res + name_len(strbuf, xattr_name_len))) {
749127e5f5aSVyacheslav Dubeyko res = -ERANGE;
750127e5f5aSVyacheslav Dubeyko goto end_listxattr;
751127e5f5aSVyacheslav Dubeyko } else
752127e5f5aSVyacheslav Dubeyko res += copy_name(buffer + res,
753127e5f5aSVyacheslav Dubeyko strbuf, xattr_name_len);
754127e5f5aSVyacheslav Dubeyko }
755127e5f5aSVyacheslav Dubeyko
756127e5f5aSVyacheslav Dubeyko if (hfs_brec_goto(&fd, 1))
757127e5f5aSVyacheslav Dubeyko goto end_listxattr;
758127e5f5aSVyacheslav Dubeyko }
759127e5f5aSVyacheslav Dubeyko
760127e5f5aSVyacheslav Dubeyko end_listxattr:
761017f8da4SHin-Tak Leung kfree(strbuf);
762017f8da4SHin-Tak Leung out:
763127e5f5aSVyacheslav Dubeyko hfs_find_exit(&fd);
764127e5f5aSVyacheslav Dubeyko return res;
765127e5f5aSVyacheslav Dubeyko }
766127e5f5aSVyacheslav Dubeyko
hfsplus_removexattr(struct inode * inode,const char * name)767b168fff7SChristoph Hellwig static int hfsplus_removexattr(struct inode *inode, const char *name)
768127e5f5aSVyacheslav Dubeyko {
7691b2fde2dSXU pengfei int err;
770127e5f5aSVyacheslav Dubeyko struct hfs_find_data cat_fd;
771127e5f5aSVyacheslav Dubeyko u16 flags;
772127e5f5aSVyacheslav Dubeyko u16 cat_entry_type;
7731b2fde2dSXU pengfei int is_xattr_acl_deleted;
7741b2fde2dSXU pengfei int is_all_xattrs_deleted;
775127e5f5aSVyacheslav Dubeyko
776127e5f5aSVyacheslav Dubeyko if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
777127e5f5aSVyacheslav Dubeyko return -EOPNOTSUPP;
778127e5f5aSVyacheslav Dubeyko
779127e5f5aSVyacheslav Dubeyko if (!strcmp_xattr_finder_info(name))
780127e5f5aSVyacheslav Dubeyko return -EOPNOTSUPP;
781127e5f5aSVyacheslav Dubeyko
782127e5f5aSVyacheslav Dubeyko err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
783127e5f5aSVyacheslav Dubeyko if (err) {
784d6142673SJoe Perches pr_err("can't init xattr find struct\n");
785127e5f5aSVyacheslav Dubeyko return err;
786127e5f5aSVyacheslav Dubeyko }
787127e5f5aSVyacheslav Dubeyko
788127e5f5aSVyacheslav Dubeyko err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
789127e5f5aSVyacheslav Dubeyko if (err) {
790d6142673SJoe Perches pr_err("catalog searching failed\n");
791127e5f5aSVyacheslav Dubeyko goto end_removexattr;
792127e5f5aSVyacheslav Dubeyko }
793127e5f5aSVyacheslav Dubeyko
794127e5f5aSVyacheslav Dubeyko err = hfsplus_delete_attr(inode, name);
795127e5f5aSVyacheslav Dubeyko if (err)
796127e5f5aSVyacheslav Dubeyko goto end_removexattr;
797127e5f5aSVyacheslav Dubeyko
798127e5f5aSVyacheslav Dubeyko is_xattr_acl_deleted = !strcmp_xattr_acl(name);
799127e5f5aSVyacheslav Dubeyko is_all_xattrs_deleted = !hfsplus_attr_exists(inode, NULL);
800127e5f5aSVyacheslav Dubeyko
801127e5f5aSVyacheslav Dubeyko if (!is_xattr_acl_deleted && !is_all_xattrs_deleted)
802127e5f5aSVyacheslav Dubeyko goto end_removexattr;
803127e5f5aSVyacheslav Dubeyko
804127e5f5aSVyacheslav Dubeyko cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
805127e5f5aSVyacheslav Dubeyko
806127e5f5aSVyacheslav Dubeyko if (cat_entry_type == HFSPLUS_FOLDER) {
807127e5f5aSVyacheslav Dubeyko flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
808127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_folder, flags));
809127e5f5aSVyacheslav Dubeyko if (is_xattr_acl_deleted)
810127e5f5aSVyacheslav Dubeyko flags &= ~HFSPLUS_ACL_EXISTS;
811127e5f5aSVyacheslav Dubeyko if (is_all_xattrs_deleted)
812127e5f5aSVyacheslav Dubeyko flags &= ~HFSPLUS_XATTR_EXISTS;
813127e5f5aSVyacheslav Dubeyko hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
814127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_folder, flags),
815127e5f5aSVyacheslav Dubeyko flags);
816127e5f5aSVyacheslav Dubeyko hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
817127e5f5aSVyacheslav Dubeyko } else if (cat_entry_type == HFSPLUS_FILE) {
818127e5f5aSVyacheslav Dubeyko flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
819127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_file, flags));
820127e5f5aSVyacheslav Dubeyko if (is_xattr_acl_deleted)
821127e5f5aSVyacheslav Dubeyko flags &= ~HFSPLUS_ACL_EXISTS;
822127e5f5aSVyacheslav Dubeyko if (is_all_xattrs_deleted)
823127e5f5aSVyacheslav Dubeyko flags &= ~HFSPLUS_XATTR_EXISTS;
824127e5f5aSVyacheslav Dubeyko hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
825127e5f5aSVyacheslav Dubeyko offsetof(struct hfsplus_cat_file, flags),
826127e5f5aSVyacheslav Dubeyko flags);
827127e5f5aSVyacheslav Dubeyko hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
828127e5f5aSVyacheslav Dubeyko } else {
829d6142673SJoe Perches pr_err("invalid catalog entry type\n");
830127e5f5aSVyacheslav Dubeyko err = -EIO;
831127e5f5aSVyacheslav Dubeyko goto end_removexattr;
832127e5f5aSVyacheslav Dubeyko }
833127e5f5aSVyacheslav Dubeyko
834127e5f5aSVyacheslav Dubeyko end_removexattr:
835127e5f5aSVyacheslav Dubeyko hfs_find_exit(&cat_fd);
836127e5f5aSVyacheslav Dubeyko return err;
837127e5f5aSVyacheslav Dubeyko }
838127e5f5aSVyacheslav Dubeyko
hfsplus_osx_getxattr(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)839d9a82a04SAndreas Gruenbacher static int hfsplus_osx_getxattr(const struct xattr_handler *handler,
840b296821aSAl Viro struct dentry *unused, struct inode *inode,
841b296821aSAl Viro const char *name, void *buffer, size_t size)
842127e5f5aSVyacheslav Dubeyko {
843b168fff7SChristoph Hellwig /*
844b168fff7SChristoph Hellwig * Don't allow retrieving properly prefixed attributes
845b168fff7SChristoph Hellwig * by prepending them with "osx."
846b168fff7SChristoph Hellwig */
847b168fff7SChristoph Hellwig if (is_known_namespace(name))
848b168fff7SChristoph Hellwig return -EOPNOTSUPP;
849127e5f5aSVyacheslav Dubeyko
850db579e76SThomas Hebb /*
851db579e76SThomas Hebb * osx is the namespace we use to indicate an unprefixed
852db579e76SThomas Hebb * attribute on the filesystem (like the ones that OS X
853db579e76SThomas Hebb * creates), so we pass the name through unmodified (after
854db579e76SThomas Hebb * ensuring it doesn't conflict with another namespace).
855db579e76SThomas Hebb */
856b296821aSAl Viro return __hfsplus_getxattr(inode, name, buffer, size);
857127e5f5aSVyacheslav Dubeyko }
858127e5f5aSVyacheslav Dubeyko
hfsplus_osx_setxattr(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * buffer,size_t size,int flags)859d9a82a04SAndreas Gruenbacher static int hfsplus_osx_setxattr(const struct xattr_handler *handler,
86039f60c1cSChristian Brauner struct mnt_idmap *idmap,
86159301226SAl Viro struct dentry *unused, struct inode *inode,
86259301226SAl Viro const char *name, const void *buffer,
86359301226SAl Viro size_t size, int flags)
864127e5f5aSVyacheslav Dubeyko {
865b168fff7SChristoph Hellwig /*
866b168fff7SChristoph Hellwig * Don't allow setting properly prefixed attributes
867b168fff7SChristoph Hellwig * by prepending them with "osx."
868b168fff7SChristoph Hellwig */
8692796e4ceSChristoph Hellwig if (is_known_namespace(name))
8702796e4ceSChristoph Hellwig return -EOPNOTSUPP;
8712796e4ceSChristoph Hellwig
872db579e76SThomas Hebb /*
873db579e76SThomas Hebb * osx is the namespace we use to indicate an unprefixed
874db579e76SThomas Hebb * attribute on the filesystem (like the ones that OS X
875db579e76SThomas Hebb * creates), so we pass the name through unmodified (after
876db579e76SThomas Hebb * ensuring it doesn't conflict with another namespace).
877db579e76SThomas Hebb */
87859301226SAl Viro return __hfsplus_setxattr(inode, name, buffer, size, flags);
879127e5f5aSVyacheslav Dubeyko }
880127e5f5aSVyacheslav Dubeyko
881127e5f5aSVyacheslav Dubeyko const struct xattr_handler hfsplus_xattr_osx_handler = {
882127e5f5aSVyacheslav Dubeyko .prefix = XATTR_MAC_OSX_PREFIX,
883127e5f5aSVyacheslav Dubeyko .get = hfsplus_osx_getxattr,
884127e5f5aSVyacheslav Dubeyko .set = hfsplus_osx_setxattr,
885127e5f5aSVyacheslav Dubeyko };
886