2 * linux/fs/hfsplus/attributes.c
4 * Vyacheslav Dubeyko <slava@dubeyko.com>
6 * Handling of records in attributes tree
9 #include "hfsplus_fs.h"
10 #include "hfsplus_raw.h"
12 static struct kmem_cache *hfsplus_attr_tree_cachep;
14 int __init hfsplus_create_attr_tree_cache(void)
16 if (hfsplus_attr_tree_cachep)
19 hfsplus_attr_tree_cachep =
20 kmem_cache_create("hfsplus_attr_cache",
21 sizeof(hfsplus_attr_entry), 0,
22 SLAB_HWCACHE_ALIGN, NULL);
23 if (!hfsplus_attr_tree_cachep)
29 void hfsplus_destroy_attr_tree_cache(void)
31 kmem_cache_destroy(hfsplus_attr_tree_cachep);
34 int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1,
35 const hfsplus_btree_key *k2)
37 __be32 k1_cnid, k2_cnid;
39 k1_cnid = k1->attr.cnid;
40 k2_cnid = k2->attr.cnid;
41 if (k1_cnid != k2_cnid)
42 return be32_to_cpu(k1_cnid) < be32_to_cpu(k2_cnid) ? -1 : 1;
44 return hfsplus_strcmp(
45 (const struct hfsplus_unistr *)&k1->attr.key_name,
46 (const struct hfsplus_unistr *)&k2->attr.key_name);
49 int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
50 u32 cnid, const char *name)
54 memset(key, 0, sizeof(struct hfsplus_attr_key));
55 key->attr.cnid = cpu_to_be32(cnid);
57 int res = hfsplus_asc2uni(sb,
58 (struct hfsplus_unistr *)&key->attr.key_name,
59 HFSPLUS_ATTR_MAX_STRLEN, name, strlen(name));
62 len = be16_to_cpu(key->attr.key_name.length);
64 key->attr.key_name.length = 0;
68 /* The length of the key, as stored in key_len field, does not include
69 * the size of the key_len field itself.
70 * So, offsetof(hfsplus_attr_key, key_name) is a trick because
71 * it takes into consideration key_len field (__be16) of
72 * hfsplus_attr_key structure instead of length field (__be16) of
73 * hfsplus_attr_unistr structure.
76 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
82 void hfsplus_attr_build_key_uni(hfsplus_btree_key *key,
84 struct hfsplus_attr_unistr *name)
88 memset(key, 0, sizeof(struct hfsplus_attr_key));
89 ustrlen = be16_to_cpu(name->length);
90 key->attr.cnid = cpu_to_be32(cnid);
91 key->attr.key_name.length = cpu_to_be16(ustrlen);
93 memcpy(key->attr.key_name.unicode, name->unicode, ustrlen);
95 /* The length of the key, as stored in key_len field, does not include
96 * the size of the key_len field itself.
97 * So, offsetof(hfsplus_attr_key, key_name) is a trick because
98 * it takes into consideration key_len field (__be16) of
99 * hfsplus_attr_key structure instead of length field (__be16) of
100 * hfsplus_attr_unistr structure.
103 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
107 hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
109 return kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
112 void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
115 kmem_cache_free(hfsplus_attr_tree_cachep, entry);
118 #define HFSPLUS_INVALID_ATTR_RECORD -1
120 static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
121 u32 cnid, const void *value, size_t size)
123 if (record_type == HFSPLUS_ATTR_FORK_DATA) {
125 * Mac OS X supports only inline data attributes.
128 memset(entry, 0, sizeof(*entry));
129 return sizeof(struct hfsplus_attr_fork_data);
130 } else if (record_type == HFSPLUS_ATTR_EXTENTS) {
132 * Mac OS X supports only inline data attributes.
135 memset(entry, 0, sizeof(*entry));
136 return sizeof(struct hfsplus_attr_extents);
137 } else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
140 memset(entry, 0, sizeof(struct hfsplus_attr_inline_data));
141 entry->inline_data.record_type = cpu_to_be32(record_type);
142 if (size <= HFSPLUS_MAX_INLINE_DATA_SIZE)
145 return HFSPLUS_INVALID_ATTR_RECORD;
146 entry->inline_data.length = cpu_to_be16(len);
147 memcpy(entry->inline_data.raw_bytes, value, len);
149 * Align len on two-byte boundary.
150 * It needs to add pad byte if we have odd len.
152 len = round_up(len, 2);
153 return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
155 } else /* invalid input */
156 memset(entry, 0, sizeof(*entry));
158 return HFSPLUS_INVALID_ATTR_RECORD;
161 int hfsplus_find_attr(struct super_block *sb, u32 cnid,
162 const char *name, struct hfs_find_data *fd)
166 hfs_dbg(ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
168 if (!HFSPLUS_SB(sb)->attr_tree) {
169 pr_err("attributes file doesn't exist\n");
174 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
176 goto failed_find_attr;
177 err = hfs_brec_find(fd, hfs_find_rec_by_key);
179 goto failed_find_attr;
181 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
183 goto failed_find_attr;
184 err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
186 goto failed_find_attr;
193 int hfsplus_attr_exists(struct inode *inode, const char *name)
196 struct super_block *sb = inode->i_sb;
197 struct hfs_find_data fd;
199 if (!HFSPLUS_SB(sb)->attr_tree)
202 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
206 err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
218 int hfsplus_create_attr(struct inode *inode,
220 const void *value, size_t size)
222 struct super_block *sb = inode->i_sb;
223 struct hfs_find_data fd;
224 hfsplus_attr_entry *entry_ptr;
228 hfs_dbg(ATTR_MOD, "create_attr: %s,%ld\n",
229 name ? name : NULL, inode->i_ino);
231 if (!HFSPLUS_SB(sb)->attr_tree) {
232 pr_err("attributes file doesn't exist\n");
236 entry_ptr = hfsplus_alloc_attr_entry();
240 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
242 goto failed_init_create_attr;
245 err = hfsplus_attr_build_key(sb, fd.search_key,
248 goto failed_create_attr;
251 goto failed_create_attr;
254 /* Mac OS X supports only inline data attributes. */
255 entry_size = hfsplus_attr_build_record(entry_ptr,
256 HFSPLUS_ATTR_INLINE_DATA,
259 if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
261 goto failed_create_attr;
264 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
265 if (err != -ENOENT) {
268 goto failed_create_attr;
271 err = hfs_brec_insert(&fd, entry_ptr, entry_size);
273 goto failed_create_attr;
275 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
280 failed_init_create_attr:
281 hfsplus_destroy_attr_entry(entry_ptr);
285 static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
286 struct hfs_find_data *fd)
289 __be32 found_cnid, record_type;
291 hfs_bnode_read(fd->bnode, &found_cnid,
293 offsetof(struct hfsplus_attr_key, cnid),
295 if (cnid != be32_to_cpu(found_cnid))
298 hfs_bnode_read(fd->bnode, &record_type,
299 fd->entryoffset, sizeof(record_type));
301 switch (be32_to_cpu(record_type)) {
302 case HFSPLUS_ATTR_INLINE_DATA:
303 /* All is OK. Do nothing. */
305 case HFSPLUS_ATTR_FORK_DATA:
306 case HFSPLUS_ATTR_EXTENTS:
307 pr_err("only inline data xattr are supported\n");
310 pr_err("invalid extended attribute record\n");
314 err = hfs_brec_remove(fd);
318 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
322 int hfsplus_delete_attr(struct inode *inode, const char *name)
325 struct super_block *sb = inode->i_sb;
326 struct hfs_find_data fd;
328 hfs_dbg(ATTR_MOD, "delete_attr: %s,%ld\n",
329 name ? name : NULL, inode->i_ino);
331 if (!HFSPLUS_SB(sb)->attr_tree) {
332 pr_err("attributes file doesn't exist\n");
336 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
341 err = hfsplus_attr_build_key(sb, fd.search_key,
346 pr_err("invalid extended attribute name\n");
351 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
355 err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
364 int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
367 struct hfs_find_data fd;
369 hfs_dbg(ATTR_MOD, "delete_all_attrs: %d\n", cnid);
371 if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
372 pr_err("attributes file doesn't exist\n");
376 err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
381 err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
384 pr_err("xattr search failed\n");
388 err = __hfsplus_delete_attr(dir, cnid, &fd);