blob: f3345c0b1c62ec5fa388dabcf8dee2291cf384ee [file] [log] [blame]
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -08001/*
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
12static struct kmem_cache *hfsplus_attr_tree_cachep;
13
Fabian Frederickc11e6142014-04-03 14:50:35 -070014int __init hfsplus_create_attr_tree_cache(void)
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -080015{
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
29void hfsplus_destroy_attr_tree_cache(void)
30{
31 kmem_cache_destroy(hfsplus_attr_tree_cachep);
32}
33
34int 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
49int 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) {
Hin-Tak Leungbf29e882014-06-06 14:36:22 -070057 int res = hfsplus_asc2uni(sb,
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -080058 (struct hfsplus_unistr *)&key->attr.key_name,
Hin-Tak Leungbf29e882014-06-06 14:36:22 -070059 HFSPLUS_ATTR_MAX_STRLEN, name, strlen(name));
60 if (res)
61 return res;
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -080062 len = be16_to_cpu(key->attr.key_name.length);
63 } else {
64 key->attr.key_name.length = 0;
65 len = 0;
66 }
67
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.
74 */
75 key->key_len =
76 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
77 2 * len);
78
79 return 0;
80}
81
82void hfsplus_attr_build_key_uni(hfsplus_btree_key *key,
83 u32 cnid,
84 struct hfsplus_attr_unistr *name)
85{
86 int ustrlen;
87
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);
92 ustrlen *= 2;
93 memcpy(key->attr.key_name.unicode, name->unicode, ustrlen);
94
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.
101 */
102 key->key_len =
103 cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
104 ustrlen);
105}
106
107hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
108{
109 return kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
110}
111
112void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
113{
114 if (entry)
115 kmem_cache_free(hfsplus_attr_tree_cachep, entry);
116}
117
118#define HFSPLUS_INVALID_ATTR_RECORD -1
119
120static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
121 u32 cnid, const void *value, size_t size)
122{
123 if (record_type == HFSPLUS_ATTR_FORK_DATA) {
124 /*
125 * Mac OS X supports only inline data attributes.
126 * Do nothing
127 */
128 memset(entry, 0, sizeof(*entry));
129 return sizeof(struct hfsplus_attr_fork_data);
130 } else if (record_type == HFSPLUS_ATTR_EXTENTS) {
131 /*
132 * Mac OS X supports only inline data attributes.
133 * Do nothing.
134 */
135 memset(entry, 0, sizeof(*entry));
136 return sizeof(struct hfsplus_attr_extents);
137 } else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
138 u16 len;
139
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)
143 len = size;
144 else
145 return HFSPLUS_INVALID_ATTR_RECORD;
146 entry->inline_data.length = cpu_to_be16(len);
147 memcpy(entry->inline_data.raw_bytes, value, len);
148 /*
149 * Align len on two-byte boundary.
150 * It needs to add pad byte if we have odd len.
151 */
152 len = round_up(len, 2);
153 return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
154 len;
155 } else /* invalid input */
156 memset(entry, 0, sizeof(*entry));
157
158 return HFSPLUS_INVALID_ATTR_RECORD;
159}
160
161int hfsplus_find_attr(struct super_block *sb, u32 cnid,
162 const char *name, struct hfs_find_data *fd)
163{
164 int err = 0;
165
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700166 hfs_dbg(ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800167
168 if (!HFSPLUS_SB(sb)->attr_tree) {
Joe Perchesd6142672013-04-30 15:27:55 -0700169 pr_err("attributes file doesn't exist\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800170 return -EINVAL;
171 }
172
173 if (name) {
174 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
175 if (err)
176 goto failed_find_attr;
177 err = hfs_brec_find(fd, hfs_find_rec_by_key);
178 if (err)
179 goto failed_find_attr;
180 } else {
181 err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
182 if (err)
183 goto failed_find_attr;
184 err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
185 if (err)
186 goto failed_find_attr;
187 }
188
189failed_find_attr:
190 return err;
191}
192
193int hfsplus_attr_exists(struct inode *inode, const char *name)
194{
195 int err = 0;
196 struct super_block *sb = inode->i_sb;
197 struct hfs_find_data fd;
198
199 if (!HFSPLUS_SB(sb)->attr_tree)
200 return 0;
201
202 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
203 if (err)
204 return 0;
205
206 err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
207 if (err)
208 goto attr_not_found;
209
210 hfs_find_exit(&fd);
211 return 1;
212
213attr_not_found:
214 hfs_find_exit(&fd);
215 return 0;
216}
217
218int hfsplus_create_attr(struct inode *inode,
219 const char *name,
220 const void *value, size_t size)
221{
222 struct super_block *sb = inode->i_sb;
223 struct hfs_find_data fd;
224 hfsplus_attr_entry *entry_ptr;
225 int entry_size;
226 int err;
227
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700228 hfs_dbg(ATTR_MOD, "create_attr: %s,%ld\n",
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800229 name ? name : NULL, inode->i_ino);
230
231 if (!HFSPLUS_SB(sb)->attr_tree) {
Joe Perchesd6142672013-04-30 15:27:55 -0700232 pr_err("attributes file doesn't exist\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800233 return -EINVAL;
234 }
235
236 entry_ptr = hfsplus_alloc_attr_entry();
237 if (!entry_ptr)
238 return -ENOMEM;
239
240 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
241 if (err)
242 goto failed_init_create_attr;
243
244 if (name) {
245 err = hfsplus_attr_build_key(sb, fd.search_key,
246 inode->i_ino, name);
247 if (err)
248 goto failed_create_attr;
249 } else {
250 err = -EINVAL;
251 goto failed_create_attr;
252 }
253
254 /* Mac OS X supports only inline data attributes. */
255 entry_size = hfsplus_attr_build_record(entry_ptr,
256 HFSPLUS_ATTR_INLINE_DATA,
257 inode->i_ino,
258 value, size);
259 if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
260 err = -EINVAL;
261 goto failed_create_attr;
262 }
263
264 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
265 if (err != -ENOENT) {
266 if (!err)
267 err = -EEXIST;
268 goto failed_create_attr;
269 }
270
271 err = hfs_brec_insert(&fd, entry_ptr, entry_size);
272 if (err)
273 goto failed_create_attr;
274
275 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
276
277failed_create_attr:
278 hfs_find_exit(&fd);
279
280failed_init_create_attr:
281 hfsplus_destroy_attr_entry(entry_ptr);
282 return err;
283}
284
285static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
286 struct hfs_find_data *fd)
287{
288 int err = 0;
289 __be32 found_cnid, record_type;
290
291 hfs_bnode_read(fd->bnode, &found_cnid,
292 fd->keyoffset +
293 offsetof(struct hfsplus_attr_key, cnid),
294 sizeof(__be32));
295 if (cnid != be32_to_cpu(found_cnid))
296 return -ENOENT;
297
298 hfs_bnode_read(fd->bnode, &record_type,
299 fd->entryoffset, sizeof(record_type));
300
301 switch (be32_to_cpu(record_type)) {
302 case HFSPLUS_ATTR_INLINE_DATA:
303 /* All is OK. Do nothing. */
304 break;
305 case HFSPLUS_ATTR_FORK_DATA:
306 case HFSPLUS_ATTR_EXTENTS:
Joe Perchesd6142672013-04-30 15:27:55 -0700307 pr_err("only inline data xattr are supported\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800308 return -EOPNOTSUPP;
309 default:
Joe Perchesd6142672013-04-30 15:27:55 -0700310 pr_err("invalid extended attribute record\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800311 return -ENOENT;
312 }
313
314 err = hfs_brec_remove(fd);
315 if (err)
316 return err;
317
318 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
319 return err;
320}
321
322int hfsplus_delete_attr(struct inode *inode, const char *name)
323{
324 int err = 0;
325 struct super_block *sb = inode->i_sb;
326 struct hfs_find_data fd;
327
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700328 hfs_dbg(ATTR_MOD, "delete_attr: %s,%ld\n",
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800329 name ? name : NULL, inode->i_ino);
330
331 if (!HFSPLUS_SB(sb)->attr_tree) {
Joe Perchesd6142672013-04-30 15:27:55 -0700332 pr_err("attributes file doesn't exist\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800333 return -EINVAL;
334 }
335
336 err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
337 if (err)
338 return err;
339
340 if (name) {
341 err = hfsplus_attr_build_key(sb, fd.search_key,
342 inode->i_ino, name);
343 if (err)
344 goto out;
345 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700346 pr_err("invalid extended attribute name\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800347 err = -EINVAL;
348 goto out;
349 }
350
351 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
352 if (err)
353 goto out;
354
355 err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
356 if (err)
357 goto out;
358
359out:
360 hfs_find_exit(&fd);
361 return err;
362}
363
364int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
365{
366 int err = 0;
367 struct hfs_find_data fd;
368
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700369 hfs_dbg(ATTR_MOD, "delete_all_attrs: %d\n", cnid);
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800370
371 if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
Joe Perchesd6142672013-04-30 15:27:55 -0700372 pr_err("attributes file doesn't exist\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800373 return -EINVAL;
374 }
375
376 err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
377 if (err)
378 return err;
379
380 for (;;) {
381 err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
382 if (err) {
383 if (err != -ENOENT)
Joe Perchesd6142672013-04-30 15:27:55 -0700384 pr_err("xattr search failed\n");
Vyacheslav Dubeyko3e05ca22013-02-27 17:03:01 -0800385 goto end_delete_all;
386 }
387
388 err = __hfsplus_delete_attr(dir, cnid, &fd);
389 if (err)
390 goto end_delete_all;
391 }
392
393end_delete_all:
394 hfs_find_exit(&fd);
395 return err;
396}