xref: /linux/fs/hfsplus/attributes.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23e05ca20SVyacheslav Dubeyko /*
33e05ca20SVyacheslav Dubeyko  * linux/fs/hfsplus/attributes.c
43e05ca20SVyacheslav Dubeyko  *
53e05ca20SVyacheslav Dubeyko  * Vyacheslav Dubeyko <slava@dubeyko.com>
63e05ca20SVyacheslav Dubeyko  *
73e05ca20SVyacheslav Dubeyko  * Handling of records in attributes tree
83e05ca20SVyacheslav Dubeyko  */
93e05ca20SVyacheslav Dubeyko 
103e05ca20SVyacheslav Dubeyko #include "hfsplus_fs.h"
113e05ca20SVyacheslav Dubeyko #include "hfsplus_raw.h"
123e05ca20SVyacheslav Dubeyko 
133e05ca20SVyacheslav Dubeyko static struct kmem_cache *hfsplus_attr_tree_cachep;
143e05ca20SVyacheslav Dubeyko 
15c11e614dSFabian Frederick int __init hfsplus_create_attr_tree_cache(void)
163e05ca20SVyacheslav Dubeyko {
173e05ca20SVyacheslav Dubeyko 	if (hfsplus_attr_tree_cachep)
183e05ca20SVyacheslav Dubeyko 		return -EEXIST;
193e05ca20SVyacheslav Dubeyko 
203e05ca20SVyacheslav Dubeyko 	hfsplus_attr_tree_cachep =
213e05ca20SVyacheslav Dubeyko 		kmem_cache_create("hfsplus_attr_cache",
223e05ca20SVyacheslav Dubeyko 			sizeof(hfsplus_attr_entry), 0,
233e05ca20SVyacheslav Dubeyko 			SLAB_HWCACHE_ALIGN, NULL);
243e05ca20SVyacheslav Dubeyko 	if (!hfsplus_attr_tree_cachep)
253e05ca20SVyacheslav Dubeyko 		return -ENOMEM;
263e05ca20SVyacheslav Dubeyko 
273e05ca20SVyacheslav Dubeyko 	return 0;
283e05ca20SVyacheslav Dubeyko }
293e05ca20SVyacheslav Dubeyko 
303e05ca20SVyacheslav Dubeyko void hfsplus_destroy_attr_tree_cache(void)
313e05ca20SVyacheslav Dubeyko {
323e05ca20SVyacheslav Dubeyko 	kmem_cache_destroy(hfsplus_attr_tree_cachep);
333e05ca20SVyacheslav Dubeyko }
343e05ca20SVyacheslav Dubeyko 
353e05ca20SVyacheslav Dubeyko int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1,
363e05ca20SVyacheslav Dubeyko 				const hfsplus_btree_key *k2)
373e05ca20SVyacheslav Dubeyko {
383e05ca20SVyacheslav Dubeyko 	__be32 k1_cnid, k2_cnid;
393e05ca20SVyacheslav Dubeyko 
403e05ca20SVyacheslav Dubeyko 	k1_cnid = k1->attr.cnid;
413e05ca20SVyacheslav Dubeyko 	k2_cnid = k2->attr.cnid;
423e05ca20SVyacheslav Dubeyko 	if (k1_cnid != k2_cnid)
433e05ca20SVyacheslav Dubeyko 		return be32_to_cpu(k1_cnid) < be32_to_cpu(k2_cnid) ? -1 : 1;
443e05ca20SVyacheslav Dubeyko 
453e05ca20SVyacheslav Dubeyko 	return hfsplus_strcmp(
463e05ca20SVyacheslav Dubeyko 			(const struct hfsplus_unistr *)&k1->attr.key_name,
473e05ca20SVyacheslav Dubeyko 			(const struct hfsplus_unistr *)&k2->attr.key_name);
483e05ca20SVyacheslav Dubeyko }
493e05ca20SVyacheslav Dubeyko 
503e05ca20SVyacheslav Dubeyko int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
513e05ca20SVyacheslav Dubeyko 			u32 cnid, const char *name)
523e05ca20SVyacheslav Dubeyko {
533e05ca20SVyacheslav Dubeyko 	int len;
543e05ca20SVyacheslav Dubeyko 
553e05ca20SVyacheslav Dubeyko 	memset(key, 0, sizeof(struct hfsplus_attr_key));
563e05ca20SVyacheslav Dubeyko 	key->attr.cnid = cpu_to_be32(cnid);
573e05ca20SVyacheslav Dubeyko 	if (name) {
58bf29e886SHin-Tak Leung 		int res = hfsplus_asc2uni(sb,
593e05ca20SVyacheslav Dubeyko 				(struct hfsplus_unistr *)&key->attr.key_name,
60bf29e886SHin-Tak Leung 				HFSPLUS_ATTR_MAX_STRLEN, name, strlen(name));
61bf29e886SHin-Tak Leung 		if (res)
62bf29e886SHin-Tak Leung 			return res;
633e05ca20SVyacheslav Dubeyko 		len = be16_to_cpu(key->attr.key_name.length);
643e05ca20SVyacheslav Dubeyko 	} else {
653e05ca20SVyacheslav Dubeyko 		key->attr.key_name.length = 0;
663e05ca20SVyacheslav Dubeyko 		len = 0;
673e05ca20SVyacheslav Dubeyko 	}
683e05ca20SVyacheslav Dubeyko 
693e05ca20SVyacheslav Dubeyko 	/* The length of the key, as stored in key_len field, does not include
703e05ca20SVyacheslav Dubeyko 	 * the size of the key_len field itself.
713e05ca20SVyacheslav Dubeyko 	 * So, offsetof(hfsplus_attr_key, key_name) is a trick because
723e05ca20SVyacheslav Dubeyko 	 * it takes into consideration key_len field (__be16) of
733e05ca20SVyacheslav Dubeyko 	 * hfsplus_attr_key structure instead of length field (__be16) of
743e05ca20SVyacheslav Dubeyko 	 * hfsplus_attr_unistr structure.
753e05ca20SVyacheslav Dubeyko 	 */
763e05ca20SVyacheslav Dubeyko 	key->key_len =
773e05ca20SVyacheslav Dubeyko 		cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
783e05ca20SVyacheslav Dubeyko 				2 * len);
793e05ca20SVyacheslav Dubeyko 
803e05ca20SVyacheslav Dubeyko 	return 0;
813e05ca20SVyacheslav Dubeyko }
823e05ca20SVyacheslav Dubeyko 
833e05ca20SVyacheslav Dubeyko hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
843e05ca20SVyacheslav Dubeyko {
853e05ca20SVyacheslav Dubeyko 	return kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
863e05ca20SVyacheslav Dubeyko }
873e05ca20SVyacheslav Dubeyko 
883e05ca20SVyacheslav Dubeyko void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
893e05ca20SVyacheslav Dubeyko {
903e05ca20SVyacheslav Dubeyko 	if (entry)
913e05ca20SVyacheslav Dubeyko 		kmem_cache_free(hfsplus_attr_tree_cachep, entry);
923e05ca20SVyacheslav Dubeyko }
933e05ca20SVyacheslav Dubeyko 
943e05ca20SVyacheslav Dubeyko #define HFSPLUS_INVALID_ATTR_RECORD -1
953e05ca20SVyacheslav Dubeyko 
963e05ca20SVyacheslav Dubeyko static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
973e05ca20SVyacheslav Dubeyko 				u32 cnid, const void *value, size_t size)
983e05ca20SVyacheslav Dubeyko {
993e05ca20SVyacheslav Dubeyko 	if (record_type == HFSPLUS_ATTR_FORK_DATA) {
1003e05ca20SVyacheslav Dubeyko 		/*
1013e05ca20SVyacheslav Dubeyko 		 * Mac OS X supports only inline data attributes.
1023e05ca20SVyacheslav Dubeyko 		 * Do nothing
1033e05ca20SVyacheslav Dubeyko 		 */
1043e05ca20SVyacheslav Dubeyko 		memset(entry, 0, sizeof(*entry));
1053e05ca20SVyacheslav Dubeyko 		return sizeof(struct hfsplus_attr_fork_data);
1063e05ca20SVyacheslav Dubeyko 	} else if (record_type == HFSPLUS_ATTR_EXTENTS) {
1073e05ca20SVyacheslav Dubeyko 		/*
1083e05ca20SVyacheslav Dubeyko 		 * Mac OS X supports only inline data attributes.
1093e05ca20SVyacheslav Dubeyko 		 * Do nothing.
1103e05ca20SVyacheslav Dubeyko 		 */
1113e05ca20SVyacheslav Dubeyko 		memset(entry, 0, sizeof(*entry));
1123e05ca20SVyacheslav Dubeyko 		return sizeof(struct hfsplus_attr_extents);
1133e05ca20SVyacheslav Dubeyko 	} else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
1143e05ca20SVyacheslav Dubeyko 		u16 len;
1153e05ca20SVyacheslav Dubeyko 
1163e05ca20SVyacheslav Dubeyko 		memset(entry, 0, sizeof(struct hfsplus_attr_inline_data));
1173e05ca20SVyacheslav Dubeyko 		entry->inline_data.record_type = cpu_to_be32(record_type);
1183e05ca20SVyacheslav Dubeyko 		if (size <= HFSPLUS_MAX_INLINE_DATA_SIZE)
1193e05ca20SVyacheslav Dubeyko 			len = size;
1203e05ca20SVyacheslav Dubeyko 		else
1213e05ca20SVyacheslav Dubeyko 			return HFSPLUS_INVALID_ATTR_RECORD;
1223e05ca20SVyacheslav Dubeyko 		entry->inline_data.length = cpu_to_be16(len);
1233e05ca20SVyacheslav Dubeyko 		memcpy(entry->inline_data.raw_bytes, value, len);
1243e05ca20SVyacheslav Dubeyko 		/*
1253e05ca20SVyacheslav Dubeyko 		 * Align len on two-byte boundary.
1263e05ca20SVyacheslav Dubeyko 		 * It needs to add pad byte if we have odd len.
1273e05ca20SVyacheslav Dubeyko 		 */
1283e05ca20SVyacheslav Dubeyko 		len = round_up(len, 2);
1293e05ca20SVyacheslav Dubeyko 		return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
1303e05ca20SVyacheslav Dubeyko 					len;
1313e05ca20SVyacheslav Dubeyko 	} else /* invalid input */
1323e05ca20SVyacheslav Dubeyko 		memset(entry, 0, sizeof(*entry));
1333e05ca20SVyacheslav Dubeyko 
1343e05ca20SVyacheslav Dubeyko 	return HFSPLUS_INVALID_ATTR_RECORD;
1353e05ca20SVyacheslav Dubeyko }
1363e05ca20SVyacheslav Dubeyko 
1373e05ca20SVyacheslav Dubeyko int hfsplus_find_attr(struct super_block *sb, u32 cnid,
1383e05ca20SVyacheslav Dubeyko 			const char *name, struct hfs_find_data *fd)
1393e05ca20SVyacheslav Dubeyko {
1403e05ca20SVyacheslav Dubeyko 	int err = 0;
1413e05ca20SVyacheslav Dubeyko 
142c2b3e1f7SJoe Perches 	hfs_dbg(ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
1433e05ca20SVyacheslav Dubeyko 
1443e05ca20SVyacheslav Dubeyko 	if (!HFSPLUS_SB(sb)->attr_tree) {
145d6142673SJoe Perches 		pr_err("attributes file doesn't exist\n");
1463e05ca20SVyacheslav Dubeyko 		return -EINVAL;
1473e05ca20SVyacheslav Dubeyko 	}
1483e05ca20SVyacheslav Dubeyko 
1493e05ca20SVyacheslav Dubeyko 	if (name) {
1503e05ca20SVyacheslav Dubeyko 		err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
1513e05ca20SVyacheslav Dubeyko 		if (err)
1523e05ca20SVyacheslav Dubeyko 			goto failed_find_attr;
1533e05ca20SVyacheslav Dubeyko 		err = hfs_brec_find(fd, hfs_find_rec_by_key);
1543e05ca20SVyacheslav Dubeyko 		if (err)
1553e05ca20SVyacheslav Dubeyko 			goto failed_find_attr;
1563e05ca20SVyacheslav Dubeyko 	} else {
1573e05ca20SVyacheslav Dubeyko 		err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
1583e05ca20SVyacheslav Dubeyko 		if (err)
1593e05ca20SVyacheslav Dubeyko 			goto failed_find_attr;
1603e05ca20SVyacheslav Dubeyko 		err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
1613e05ca20SVyacheslav Dubeyko 		if (err)
1623e05ca20SVyacheslav Dubeyko 			goto failed_find_attr;
1633e05ca20SVyacheslav Dubeyko 	}
1643e05ca20SVyacheslav Dubeyko 
1653e05ca20SVyacheslav Dubeyko failed_find_attr:
1663e05ca20SVyacheslav Dubeyko 	return err;
1673e05ca20SVyacheslav Dubeyko }
1683e05ca20SVyacheslav Dubeyko 
1693e05ca20SVyacheslav Dubeyko int hfsplus_attr_exists(struct inode *inode, const char *name)
1703e05ca20SVyacheslav Dubeyko {
1713e05ca20SVyacheslav Dubeyko 	int err = 0;
1723e05ca20SVyacheslav Dubeyko 	struct super_block *sb = inode->i_sb;
1733e05ca20SVyacheslav Dubeyko 	struct hfs_find_data fd;
1743e05ca20SVyacheslav Dubeyko 
1753e05ca20SVyacheslav Dubeyko 	if (!HFSPLUS_SB(sb)->attr_tree)
1763e05ca20SVyacheslav Dubeyko 		return 0;
1773e05ca20SVyacheslav Dubeyko 
1783e05ca20SVyacheslav Dubeyko 	err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
1793e05ca20SVyacheslav Dubeyko 	if (err)
1803e05ca20SVyacheslav Dubeyko 		return 0;
1813e05ca20SVyacheslav Dubeyko 
1823e05ca20SVyacheslav Dubeyko 	err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
1833e05ca20SVyacheslav Dubeyko 	if (err)
1843e05ca20SVyacheslav Dubeyko 		goto attr_not_found;
1853e05ca20SVyacheslav Dubeyko 
1863e05ca20SVyacheslav Dubeyko 	hfs_find_exit(&fd);
1873e05ca20SVyacheslav Dubeyko 	return 1;
1883e05ca20SVyacheslav Dubeyko 
1893e05ca20SVyacheslav Dubeyko attr_not_found:
1903e05ca20SVyacheslav Dubeyko 	hfs_find_exit(&fd);
1913e05ca20SVyacheslav Dubeyko 	return 0;
1923e05ca20SVyacheslav Dubeyko }
1933e05ca20SVyacheslav Dubeyko 
1943e05ca20SVyacheslav Dubeyko int hfsplus_create_attr(struct inode *inode,
1953e05ca20SVyacheslav Dubeyko 				const char *name,
1963e05ca20SVyacheslav Dubeyko 				const void *value, size_t size)
1973e05ca20SVyacheslav Dubeyko {
1983e05ca20SVyacheslav Dubeyko 	struct super_block *sb = inode->i_sb;
1993e05ca20SVyacheslav Dubeyko 	struct hfs_find_data fd;
2003e05ca20SVyacheslav Dubeyko 	hfsplus_attr_entry *entry_ptr;
2013e05ca20SVyacheslav Dubeyko 	int entry_size;
2023e05ca20SVyacheslav Dubeyko 	int err;
2033e05ca20SVyacheslav Dubeyko 
204c2b3e1f7SJoe Perches 	hfs_dbg(ATTR_MOD, "create_attr: %s,%ld\n",
2053e05ca20SVyacheslav Dubeyko 		name ? name : NULL, inode->i_ino);
2063e05ca20SVyacheslav Dubeyko 
2073e05ca20SVyacheslav Dubeyko 	if (!HFSPLUS_SB(sb)->attr_tree) {
208d6142673SJoe Perches 		pr_err("attributes file doesn't exist\n");
2093e05ca20SVyacheslav Dubeyko 		return -EINVAL;
2103e05ca20SVyacheslav Dubeyko 	}
2113e05ca20SVyacheslav Dubeyko 
2123e05ca20SVyacheslav Dubeyko 	entry_ptr = hfsplus_alloc_attr_entry();
2133e05ca20SVyacheslav Dubeyko 	if (!entry_ptr)
2143e05ca20SVyacheslav Dubeyko 		return -ENOMEM;
2153e05ca20SVyacheslav Dubeyko 
2163e05ca20SVyacheslav Dubeyko 	err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
2173e05ca20SVyacheslav Dubeyko 	if (err)
2183e05ca20SVyacheslav Dubeyko 		goto failed_init_create_attr;
2193e05ca20SVyacheslav Dubeyko 
2203e05ca20SVyacheslav Dubeyko 	if (name) {
2213e05ca20SVyacheslav Dubeyko 		err = hfsplus_attr_build_key(sb, fd.search_key,
2223e05ca20SVyacheslav Dubeyko 						inode->i_ino, name);
2233e05ca20SVyacheslav Dubeyko 		if (err)
2243e05ca20SVyacheslav Dubeyko 			goto failed_create_attr;
2253e05ca20SVyacheslav Dubeyko 	} else {
2263e05ca20SVyacheslav Dubeyko 		err = -EINVAL;
2273e05ca20SVyacheslav Dubeyko 		goto failed_create_attr;
2283e05ca20SVyacheslav Dubeyko 	}
2293e05ca20SVyacheslav Dubeyko 
2303e05ca20SVyacheslav Dubeyko 	/* Mac OS X supports only inline data attributes. */
2313e05ca20SVyacheslav Dubeyko 	entry_size = hfsplus_attr_build_record(entry_ptr,
2323e05ca20SVyacheslav Dubeyko 					HFSPLUS_ATTR_INLINE_DATA,
2333e05ca20SVyacheslav Dubeyko 					inode->i_ino,
2343e05ca20SVyacheslav Dubeyko 					value, size);
2353e05ca20SVyacheslav Dubeyko 	if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
2363e05ca20SVyacheslav Dubeyko 		err = -EINVAL;
2373e05ca20SVyacheslav Dubeyko 		goto failed_create_attr;
2383e05ca20SVyacheslav Dubeyko 	}
2393e05ca20SVyacheslav Dubeyko 
2403e05ca20SVyacheslav Dubeyko 	err = hfs_brec_find(&fd, hfs_find_rec_by_key);
2413e05ca20SVyacheslav Dubeyko 	if (err != -ENOENT) {
2423e05ca20SVyacheslav Dubeyko 		if (!err)
2433e05ca20SVyacheslav Dubeyko 			err = -EEXIST;
2443e05ca20SVyacheslav Dubeyko 		goto failed_create_attr;
2453e05ca20SVyacheslav Dubeyko 	}
2463e05ca20SVyacheslav Dubeyko 
2473e05ca20SVyacheslav Dubeyko 	err = hfs_brec_insert(&fd, entry_ptr, entry_size);
2483e05ca20SVyacheslav Dubeyko 	if (err)
2493e05ca20SVyacheslav Dubeyko 		goto failed_create_attr;
2503e05ca20SVyacheslav Dubeyko 
2513e05ca20SVyacheslav Dubeyko 	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
2523e05ca20SVyacheslav Dubeyko 
2533e05ca20SVyacheslav Dubeyko failed_create_attr:
2543e05ca20SVyacheslav Dubeyko 	hfs_find_exit(&fd);
2553e05ca20SVyacheslav Dubeyko 
2563e05ca20SVyacheslav Dubeyko failed_init_create_attr:
2573e05ca20SVyacheslav Dubeyko 	hfsplus_destroy_attr_entry(entry_ptr);
2583e05ca20SVyacheslav Dubeyko 	return err;
2593e05ca20SVyacheslav Dubeyko }
2603e05ca20SVyacheslav Dubeyko 
2613e05ca20SVyacheslav Dubeyko static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
2623e05ca20SVyacheslav Dubeyko 					struct hfs_find_data *fd)
2633e05ca20SVyacheslav Dubeyko {
2643e05ca20SVyacheslav Dubeyko 	int err = 0;
2653e05ca20SVyacheslav Dubeyko 	__be32 found_cnid, record_type;
2663e05ca20SVyacheslav Dubeyko 
2673e05ca20SVyacheslav Dubeyko 	hfs_bnode_read(fd->bnode, &found_cnid,
2683e05ca20SVyacheslav Dubeyko 			fd->keyoffset +
2693e05ca20SVyacheslav Dubeyko 			offsetof(struct hfsplus_attr_key, cnid),
2703e05ca20SVyacheslav Dubeyko 			sizeof(__be32));
2713e05ca20SVyacheslav Dubeyko 	if (cnid != be32_to_cpu(found_cnid))
2723e05ca20SVyacheslav Dubeyko 		return -ENOENT;
2733e05ca20SVyacheslav Dubeyko 
2743e05ca20SVyacheslav Dubeyko 	hfs_bnode_read(fd->bnode, &record_type,
2753e05ca20SVyacheslav Dubeyko 			fd->entryoffset, sizeof(record_type));
2763e05ca20SVyacheslav Dubeyko 
2773e05ca20SVyacheslav Dubeyko 	switch (be32_to_cpu(record_type)) {
2783e05ca20SVyacheslav Dubeyko 	case HFSPLUS_ATTR_INLINE_DATA:
2793e05ca20SVyacheslav Dubeyko 		/* All is OK. Do nothing. */
2803e05ca20SVyacheslav Dubeyko 		break;
2813e05ca20SVyacheslav Dubeyko 	case HFSPLUS_ATTR_FORK_DATA:
2823e05ca20SVyacheslav Dubeyko 	case HFSPLUS_ATTR_EXTENTS:
283d6142673SJoe Perches 		pr_err("only inline data xattr are supported\n");
2843e05ca20SVyacheslav Dubeyko 		return -EOPNOTSUPP;
2853e05ca20SVyacheslav Dubeyko 	default:
286d6142673SJoe Perches 		pr_err("invalid extended attribute record\n");
2873e05ca20SVyacheslav Dubeyko 		return -ENOENT;
2883e05ca20SVyacheslav Dubeyko 	}
2893e05ca20SVyacheslav Dubeyko 
2903e05ca20SVyacheslav Dubeyko 	err = hfs_brec_remove(fd);
2913e05ca20SVyacheslav Dubeyko 	if (err)
2923e05ca20SVyacheslav Dubeyko 		return err;
2933e05ca20SVyacheslav Dubeyko 
2943e05ca20SVyacheslav Dubeyko 	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
2953e05ca20SVyacheslav Dubeyko 	return err;
2963e05ca20SVyacheslav Dubeyko }
2973e05ca20SVyacheslav Dubeyko 
2983e05ca20SVyacheslav Dubeyko int hfsplus_delete_attr(struct inode *inode, const char *name)
2993e05ca20SVyacheslav Dubeyko {
3003e05ca20SVyacheslav Dubeyko 	int err = 0;
3013e05ca20SVyacheslav Dubeyko 	struct super_block *sb = inode->i_sb;
3023e05ca20SVyacheslav Dubeyko 	struct hfs_find_data fd;
3033e05ca20SVyacheslav Dubeyko 
304c2b3e1f7SJoe Perches 	hfs_dbg(ATTR_MOD, "delete_attr: %s,%ld\n",
3053e05ca20SVyacheslav Dubeyko 		name ? name : NULL, inode->i_ino);
3063e05ca20SVyacheslav Dubeyko 
3073e05ca20SVyacheslav Dubeyko 	if (!HFSPLUS_SB(sb)->attr_tree) {
308d6142673SJoe Perches 		pr_err("attributes file doesn't exist\n");
3093e05ca20SVyacheslav Dubeyko 		return -EINVAL;
3103e05ca20SVyacheslav Dubeyko 	}
3113e05ca20SVyacheslav Dubeyko 
3123e05ca20SVyacheslav Dubeyko 	err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
3133e05ca20SVyacheslav Dubeyko 	if (err)
3143e05ca20SVyacheslav Dubeyko 		return err;
3153e05ca20SVyacheslav Dubeyko 
3163e05ca20SVyacheslav Dubeyko 	if (name) {
3173e05ca20SVyacheslav Dubeyko 		err = hfsplus_attr_build_key(sb, fd.search_key,
3183e05ca20SVyacheslav Dubeyko 						inode->i_ino, name);
3193e05ca20SVyacheslav Dubeyko 		if (err)
3203e05ca20SVyacheslav Dubeyko 			goto out;
3213e05ca20SVyacheslav Dubeyko 	} else {
322d6142673SJoe Perches 		pr_err("invalid extended attribute name\n");
3233e05ca20SVyacheslav Dubeyko 		err = -EINVAL;
3243e05ca20SVyacheslav Dubeyko 		goto out;
3253e05ca20SVyacheslav Dubeyko 	}
3263e05ca20SVyacheslav Dubeyko 
3273e05ca20SVyacheslav Dubeyko 	err = hfs_brec_find(&fd, hfs_find_rec_by_key);
3283e05ca20SVyacheslav Dubeyko 	if (err)
3293e05ca20SVyacheslav Dubeyko 		goto out;
3303e05ca20SVyacheslav Dubeyko 
3313e05ca20SVyacheslav Dubeyko 	err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
3323e05ca20SVyacheslav Dubeyko 	if (err)
3333e05ca20SVyacheslav Dubeyko 		goto out;
3343e05ca20SVyacheslav Dubeyko 
3353e05ca20SVyacheslav Dubeyko out:
3363e05ca20SVyacheslav Dubeyko 	hfs_find_exit(&fd);
3373e05ca20SVyacheslav Dubeyko 	return err;
3383e05ca20SVyacheslav Dubeyko }
3393e05ca20SVyacheslav Dubeyko 
3403e05ca20SVyacheslav Dubeyko int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
3413e05ca20SVyacheslav Dubeyko {
3423e05ca20SVyacheslav Dubeyko 	int err = 0;
3433e05ca20SVyacheslav Dubeyko 	struct hfs_find_data fd;
3443e05ca20SVyacheslav Dubeyko 
345c2b3e1f7SJoe Perches 	hfs_dbg(ATTR_MOD, "delete_all_attrs: %d\n", cnid);
3463e05ca20SVyacheslav Dubeyko 
3473e05ca20SVyacheslav Dubeyko 	if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
348d6142673SJoe Perches 		pr_err("attributes file doesn't exist\n");
3493e05ca20SVyacheslav Dubeyko 		return -EINVAL;
3503e05ca20SVyacheslav Dubeyko 	}
3513e05ca20SVyacheslav Dubeyko 
3523e05ca20SVyacheslav Dubeyko 	err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
3533e05ca20SVyacheslav Dubeyko 	if (err)
3543e05ca20SVyacheslav Dubeyko 		return err;
3553e05ca20SVyacheslav Dubeyko 
3563e05ca20SVyacheslav Dubeyko 	for (;;) {
3573e05ca20SVyacheslav Dubeyko 		err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
3583e05ca20SVyacheslav Dubeyko 		if (err) {
3593e05ca20SVyacheslav Dubeyko 			if (err != -ENOENT)
360d6142673SJoe Perches 				pr_err("xattr search failed\n");
3613e05ca20SVyacheslav Dubeyko 			goto end_delete_all;
3623e05ca20SVyacheslav Dubeyko 		}
3633e05ca20SVyacheslav Dubeyko 
3643e05ca20SVyacheslav Dubeyko 		err = __hfsplus_delete_attr(dir, cnid, &fd);
3653e05ca20SVyacheslav Dubeyko 		if (err)
3663e05ca20SVyacheslav Dubeyko 			goto end_delete_all;
3673e05ca20SVyacheslav Dubeyko 	}
3683e05ca20SVyacheslav Dubeyko 
3693e05ca20SVyacheslav Dubeyko end_delete_all:
3703e05ca20SVyacheslav Dubeyko 	hfs_find_exit(&fd);
3713e05ca20SVyacheslav Dubeyko 	return err;
3723e05ca20SVyacheslav Dubeyko }
373