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 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);
58 if (len > HFSPLUS_ATTR_MAX_STRLEN) {
59 printk(KERN_ERR "hfs: invalid xattr name's length\n");
63 (struct hfsplus_unistr *)&key->attr.key_name,
64 HFSPLUS_ATTR_MAX_STRLEN, name, len);
65 len = be16_to_cpu(key->attr.key_name.length);
67 key->attr.key_name.length = 0;
70 /* The key_length (be16) doesn't summed in the lenght of whole key.
71 But the length of the string (be16) should be included in sum.
72 So, offsetof(hfsplus_attr_key, key_name) is a trick that give
75 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
81 void hfsplus_attr_build_key_uni(hfsplus_btree_key *key,
83 struct hfsplus_attr_unistr *name)
87 memset(key, 0, sizeof(struct hfsplus_attr_key));
88 ustrlen = be16_to_cpu(name->length);
89 key->attr.cnid = cpu_to_be32(cnid);
90 key->attr.key_name.length = cpu_to_be16(ustrlen);
92 memcpy(key->attr.key_name.unicode, name->unicode, ustrlen);
93 /* The key_length (be16) doesn't summed in the lenght of whole key.
94 But the length of the string (be16) should be included in sum.
95 So, offsetof(hfsplus_attr_key, key_name) is a trick that give
98 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
102 hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
104 hfsplus_attr_entry *entry;
105 entry = kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
107 return (entry) ? entry : NULL;
110 void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
113 kmem_cache_free(hfsplus_attr_tree_cachep, entry);
116 #define HFSPLUS_INVALID_ATTR_RECORD -1
118 static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
119 u32 cnid, const void *value, size_t size)
121 if (record_type == HFSPLUS_ATTR_FORK_DATA) {
122 /* Mac OS X supports only inline data attributes.
124 memset(entry, 0, sizeof(*entry));
125 return sizeof(struct hfsplus_attr_fork_data);
126 } else if (record_type == HFSPLUS_ATTR_EXTENTS) {
127 /* Mac OS X supports only inline data attributes.
129 memset(entry, 0, sizeof(*entry));
130 return sizeof(struct hfsplus_attr_extents);
131 } else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
134 memset(entry, 0, sizeof(struct hfsplus_attr_inline_data));
135 entry->inline_data.record_type = cpu_to_be32(record_type);
136 if (size <= HFSPLUS_MAX_INLINE_DATA_SIZE)
139 return HFSPLUS_INVALID_ATTR_RECORD;
140 entry->inline_data.length = cpu_to_be16(len);
141 memcpy(entry->inline_data.raw_bytes, value, len);
142 /* Align len on two-byte boundary.
143 It needs to add pad byte if we have odd len. */
144 len = round_up(len, 2);
145 return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
147 } else /* invalid input */
148 memset(entry, 0, sizeof(*entry));
150 return HFSPLUS_INVALID_ATTR_RECORD;
153 int hfsplus_find_attr(struct super_block *sb, u32 cnid,
154 const char *name, struct hfs_find_data *fd)
158 dprint(DBG_ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
160 if (!HFSPLUS_SB(sb)->attr_tree) {
161 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
166 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
168 goto failed_find_attr;
169 err = hfs_brec_find(fd, hfs_find_rec_by_key);
171 goto failed_find_attr;
173 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
175 goto failed_find_attr;
176 err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
178 goto failed_find_attr;
185 int hfsplus_attr_exists(struct inode *inode, const char *name)
188 struct super_block *sb = inode->i_sb;
189 struct hfs_find_data fd;
191 if (!HFSPLUS_SB(sb)->attr_tree)
194 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
198 err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
210 int hfsplus_create_attr(struct inode *inode,
212 const void *value, size_t size)
214 struct super_block *sb = inode->i_sb;
215 struct hfs_find_data fd;
216 hfsplus_attr_entry *entry_ptr;
220 dprint(DBG_ATTR_MOD, "create_attr: %s,%ld\n",
221 name ? name : NULL, inode->i_ino);
223 if (!HFSPLUS_SB(sb)->attr_tree) {
224 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
228 entry_ptr = hfsplus_alloc_attr_entry();
232 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
234 goto failed_init_create_attr;
237 err = hfsplus_attr_build_key(sb, fd.search_key,
240 goto failed_create_attr;
243 goto failed_create_attr;
246 /* Mac OS X supports only inline data attributes. */
247 entry_size = hfsplus_attr_build_record(entry_ptr,
248 HFSPLUS_ATTR_INLINE_DATA,
251 if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
253 goto failed_create_attr;
256 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
257 if (err != -ENOENT) {
260 goto failed_create_attr;
263 err = hfs_brec_insert(&fd, entry_ptr, entry_size);
265 goto failed_create_attr;
267 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
272 failed_init_create_attr:
273 hfsplus_destroy_attr_entry(entry_ptr);
277 static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
278 struct hfs_find_data *fd)
281 __be32 found_cnid, record_type;
283 hfs_bnode_read(fd->bnode, &found_cnid,
285 offsetof(struct hfsplus_attr_key, cnid),
287 if (cnid != be32_to_cpu(found_cnid))
290 hfs_bnode_read(fd->bnode, &record_type,
291 fd->entryoffset, sizeof(record_type));
293 switch (be32_to_cpu(record_type)) {
294 case HFSPLUS_ATTR_INLINE_DATA:
295 /* All is OK. Do nothing. */
297 case HFSPLUS_ATTR_FORK_DATA:
298 case HFSPLUS_ATTR_EXTENTS:
299 printk(KERN_ERR "hfs: only inline data xattr are supported\n");
302 printk(KERN_ERR "hfs: invalid extended attribute record\n");
306 err = hfs_brec_remove(fd);
310 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
314 int hfsplus_delete_attr(struct inode *inode, const char *name)
317 struct super_block *sb = inode->i_sb;
318 struct hfs_find_data fd;
320 dprint(DBG_ATTR_MOD, "delete_attr: %s,%ld\n",
321 name ? name : NULL, inode->i_ino);
323 if (!HFSPLUS_SB(sb)->attr_tree) {
324 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
328 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
333 err = hfsplus_attr_build_key(sb, fd.search_key,
338 printk(KERN_ERR "hfs: invalid extended attribute name\n");
343 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
347 err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
356 int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
359 struct hfs_find_data fd;
361 dprint(DBG_ATTR_MOD, "delete_all_attrs: %d\n", cnid);
363 if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
364 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
368 err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
373 err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
376 printk(KERN_ERR "hfs: xattr search failed.\n");
380 err = __hfsplus_delete_attr(dir, cnid, &fd);