]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/hfsplus/attributes.c
hfsplus: add functionality of manipulating by records in attributes tree
[karo-tx-linux.git] / fs / hfsplus / attributes.c
1 /*
2  * linux/fs/hfsplus/attributes.c
3  *
4  * Vyacheslav Dubeyko <slava@dubeyko.com>
5  *
6  * Handling of records in attributes tree
7  */
8
9 #include "hfsplus_fs.h"
10 #include "hfsplus_raw.h"
11
12 static struct kmem_cache *hfsplus_attr_tree_cachep;
13
14 int hfsplus_create_attr_tree_cache(void)
15 {
16         if (hfsplus_attr_tree_cachep)
17                 return -EEXIST;
18
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)
24                 return -ENOMEM;
25
26         return 0;
27 }
28
29 void hfsplus_destroy_attr_tree_cache(void)
30 {
31         kmem_cache_destroy(hfsplus_attr_tree_cachep);
32 }
33
34 int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1,
35                                 const hfsplus_btree_key *k2)
36 {
37         __be32 k1_cnid, k2_cnid;
38
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;
43
44         return hfsplus_strcmp(
45                         (const struct hfsplus_unistr *)&k1->attr.key_name,
46                         (const struct hfsplus_unistr *)&k2->attr.key_name);
47 }
48
49 int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
50                         u32 cnid, const char *name)
51 {
52         int len;
53
54         memset(key, 0, sizeof(struct hfsplus_attr_key));
55         key->attr.cnid = cpu_to_be32(cnid);
56         if (name) {
57                 len = strlen(name);
58                 if (len > HFSPLUS_ATTR_MAX_STRLEN) {
59                         printk(KERN_ERR "hfs: invalid xattr name's length\n");
60                         return -EINVAL;
61                 }
62                 hfsplus_asc2uni(sb,
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);
66         } else {
67                 key->attr.key_name.length = 0;
68                 len = 0;
69         }
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
73            right value. */
74         key->key_len =
75                 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
76                                 2 * len);
77
78         return 0;
79 }
80
81 void hfsplus_attr_build_key_uni(hfsplus_btree_key *key,
82                                         u32 cnid,
83                                         struct hfsplus_attr_unistr *name)
84 {
85         int ustrlen;
86
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);
91         ustrlen *= 2;
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
96            right value. */
97         key->key_len =
98                 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
99                                 ustrlen);
100 }
101
102 hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
103 {
104         hfsplus_attr_entry *entry;
105         entry = kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
106
107         return (entry) ? entry : NULL;
108 }
109
110 void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
111 {
112         if (entry)
113                 kmem_cache_free(hfsplus_attr_tree_cachep, entry);
114 }
115
116 #define HFSPLUS_INVALID_ATTR_RECORD -1
117
118 static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
119                                 u32 cnid, const void *value, size_t size)
120 {
121         if (record_type == HFSPLUS_ATTR_FORK_DATA) {
122                 /* Mac OS X supports only inline data attributes.
123                    Do nothing. */
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.
128                    Do nothing. */
129                 memset(entry, 0, sizeof(*entry));
130                 return sizeof(struct hfsplus_attr_extents);
131         } else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
132                 u16 len;
133
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)
137                         len = size;
138                 else
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) +
146                                         len;
147         } else /* invalid input */
148                 memset(entry, 0, sizeof(*entry));
149
150         return HFSPLUS_INVALID_ATTR_RECORD;
151 }
152
153 int hfsplus_find_attr(struct super_block *sb, u32 cnid,
154                         const char *name, struct hfs_find_data *fd)
155 {
156         int err = 0;
157
158         dprint(DBG_ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
159
160         if (!HFSPLUS_SB(sb)->attr_tree) {
161                 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
162                 return -EINVAL;
163         }
164
165         if (name) {
166                 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
167                 if (err)
168                         goto failed_find_attr;
169                 err = hfs_brec_find(fd, hfs_find_rec_by_key);
170                 if (err)
171                         goto failed_find_attr;
172         } else {
173                 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
174                 if (err)
175                         goto failed_find_attr;
176                 err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
177                 if (err)
178                         goto failed_find_attr;
179         }
180
181 failed_find_attr:
182         return err;
183 }
184
185 int hfsplus_attr_exists(struct inode *inode, const char *name)
186 {
187         int err = 0;
188         struct super_block *sb = inode->i_sb;
189         struct hfs_find_data fd;
190
191         if (!HFSPLUS_SB(sb)->attr_tree)
192                 return 0;
193
194         err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
195         if (err)
196                 return 0;
197
198         err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
199         if (err)
200                 goto attr_not_found;
201
202         hfs_find_exit(&fd);
203         return 1;
204
205 attr_not_found:
206         hfs_find_exit(&fd);
207         return 0;
208 }
209
210 int hfsplus_create_attr(struct inode *inode,
211                                 const char *name,
212                                 const void *value, size_t size)
213 {
214         struct super_block *sb = inode->i_sb;
215         struct hfs_find_data fd;
216         hfsplus_attr_entry *entry_ptr;
217         int entry_size;
218         int err;
219
220         dprint(DBG_ATTR_MOD, "create_attr: %s,%ld\n",
221                 name ? name : NULL, inode->i_ino);
222
223         if (!HFSPLUS_SB(sb)->attr_tree) {
224                 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
225                 return -EINVAL;
226         }
227
228         entry_ptr = hfsplus_alloc_attr_entry();
229         if (!entry_ptr)
230                 return -ENOMEM;
231
232         err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
233         if (err)
234                 goto failed_init_create_attr;
235
236         if (name) {
237                 err = hfsplus_attr_build_key(sb, fd.search_key,
238                                                 inode->i_ino, name);
239                 if (err)
240                         goto failed_create_attr;
241         } else {
242                 err = -EINVAL;
243                 goto failed_create_attr;
244         }
245
246         /* Mac OS X supports only inline data attributes. */
247         entry_size = hfsplus_attr_build_record(entry_ptr,
248                                         HFSPLUS_ATTR_INLINE_DATA,
249                                         inode->i_ino,
250                                         value, size);
251         if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
252                 err = -EINVAL;
253                 goto failed_create_attr;
254         }
255
256         err = hfs_brec_find(&fd, hfs_find_rec_by_key);
257         if (err != -ENOENT) {
258                 if (!err)
259                         err = -EEXIST;
260                 goto failed_create_attr;
261         }
262
263         err = hfs_brec_insert(&fd, entry_ptr, entry_size);
264         if (err)
265                 goto failed_create_attr;
266
267         hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
268
269 failed_create_attr:
270         hfs_find_exit(&fd);
271
272 failed_init_create_attr:
273         hfsplus_destroy_attr_entry(entry_ptr);
274         return err;
275 }
276
277 static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
278                                         struct hfs_find_data *fd)
279 {
280         int err = 0;
281         __be32 found_cnid, record_type;
282
283         hfs_bnode_read(fd->bnode, &found_cnid,
284                         fd->keyoffset +
285                         offsetof(struct hfsplus_attr_key, cnid),
286                         sizeof(__be32));
287         if (cnid != be32_to_cpu(found_cnid))
288                 return -ENOENT;
289
290         hfs_bnode_read(fd->bnode, &record_type,
291                         fd->entryoffset, sizeof(record_type));
292
293         switch (be32_to_cpu(record_type)) {
294         case HFSPLUS_ATTR_INLINE_DATA:
295                 /* All is OK. Do nothing. */
296                 break;
297         case HFSPLUS_ATTR_FORK_DATA:
298         case HFSPLUS_ATTR_EXTENTS:
299                 printk(KERN_ERR "hfs: only inline data xattr are supported\n");
300                 return -EOPNOTSUPP;
301         default:
302                 printk(KERN_ERR "hfs: invalid extended attribute record\n");
303                 return -ENOENT;
304         }
305
306         err = hfs_brec_remove(fd);
307         if (err)
308                 return err;
309
310         hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
311         return err;
312 }
313
314 int hfsplus_delete_attr(struct inode *inode, const char *name)
315 {
316         int err = 0;
317         struct super_block *sb = inode->i_sb;
318         struct hfs_find_data fd;
319
320         dprint(DBG_ATTR_MOD, "delete_attr: %s,%ld\n",
321                 name ? name : NULL, inode->i_ino);
322
323         if (!HFSPLUS_SB(sb)->attr_tree) {
324                 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
325                 return -EINVAL;
326         }
327
328         err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
329         if (err)
330                 return err;
331
332         if (name) {
333                 err = hfsplus_attr_build_key(sb, fd.search_key,
334                                                 inode->i_ino, name);
335                 if (err)
336                         goto out;
337         } else {
338                 printk(KERN_ERR "hfs: invalid extended attribute name\n");
339                 err = -EINVAL;
340                 goto out;
341         }
342
343         err = hfs_brec_find(&fd, hfs_find_rec_by_key);
344         if (err)
345                 goto out;
346
347         err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
348         if (err)
349                 goto out;
350
351 out:
352         hfs_find_exit(&fd);
353         return err;
354 }
355
356 int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
357 {
358         int err = 0;
359         struct hfs_find_data fd;
360
361         dprint(DBG_ATTR_MOD, "delete_all_attrs: %d\n", cnid);
362
363         if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
364                 printk(KERN_ERR "hfs: attributes file doesn't exist\n");
365                 return -EINVAL;
366         }
367
368         err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
369         if (err)
370                 return err;
371
372         for (;;) {
373                 err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
374                 if (err) {
375                         if (err != -ENOENT)
376                                 printk(KERN_ERR "hfs: xattr search failed.\n");
377                         goto end_delete_all;
378                 }
379
380                 err = __hfsplus_delete_attr(dir, cnid, &fd);
381                 if (err)
382                         goto end_delete_all;
383         }
384
385 end_delete_all:
386         hfs_find_exit(&fd);
387         return err;
388 }