blob: aab093c27c593af02aa389166e52ca48e72e89db [file] [log] [blame]
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -08001/*
2 * linux/fs/hfsplus/xattr.c
3 *
4 * Vyacheslav Dubeyko <slava@dubeyko.com>
5 *
6 * Logic of processing extended attributes
7 */
8
9#include "hfsplus_fs.h"
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -080010#include <linux/posix_acl_xattr.h>
Hin-Tak Leung017f8da2014-06-06 14:36:21 -070011#include <linux/nls.h>
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -080012#include "xattr.h"
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -070013#include "acl.h"
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -080014
Christoph Hellwigb168fff2014-01-29 23:59:19 -080015static int hfsplus_removexattr(struct inode *inode, const char *name);
16
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -080017const struct xattr_handler *hfsplus_xattr_handlers[] = {
18 &hfsplus_xattr_osx_handler,
19 &hfsplus_xattr_user_handler,
20 &hfsplus_xattr_trusted_handler,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -070021#ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -080022 &posix_acl_access_xattr_handler,
23 &posix_acl_default_xattr_handler,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -070024#endif
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -080025 &hfsplus_xattr_security_handler,
26 NULL
27};
28
29static int strcmp_xattr_finder_info(const char *name)
30{
31 if (name) {
32 return strncmp(name, HFSPLUS_XATTR_FINDER_INFO_NAME,
33 sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME));
34 }
35 return -1;
36}
37
38static int strcmp_xattr_acl(const char *name)
39{
40 if (name) {
41 return strncmp(name, HFSPLUS_XATTR_ACL_NAME,
42 sizeof(HFSPLUS_XATTR_ACL_NAME));
43 }
44 return -1;
45}
46
47static inline int is_known_namespace(const char *name)
48{
49 if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
50 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
51 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
52 strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
53 return false;
54
55 return true;
56}
57
Vyacheslav Dubeyko099e9242013-11-12 15:11:08 -080058static void hfsplus_init_header_node(struct inode *attr_file,
59 u32 clump_size,
Geert Uytterhoevena99b7062013-11-14 14:32:18 -080060 char *buf, u16 node_size)
Vyacheslav Dubeyko099e9242013-11-12 15:11:08 -080061{
62 struct hfs_bnode_desc *desc;
63 struct hfs_btree_header_rec *head;
64 u16 offset;
65 __be16 *rec_offsets;
66 u32 hdr_node_map_rec_bits;
67 char *bmp;
68 u32 used_nodes;
69 u32 used_bmp_bytes;
Geert Uytterhoevena99b7062013-11-14 14:32:18 -080070 loff_t tmp;
Vyacheslav Dubeyko099e9242013-11-12 15:11:08 -080071
Geert Uytterhoevena99b7062013-11-14 14:32:18 -080072 hfs_dbg(ATTR_MOD, "init_hdr_attr_file: clump %u, node_size %u\n",
Vyacheslav Dubeyko099e9242013-11-12 15:11:08 -080073 clump_size, node_size);
74
75 /* The end of the node contains list of record offsets */
76 rec_offsets = (__be16 *)(buf + node_size);
77
78 desc = (struct hfs_bnode_desc *)buf;
79 desc->type = HFS_NODE_HEADER;
80 desc->num_recs = cpu_to_be16(HFSPLUS_BTREE_HDR_NODE_RECS_COUNT);
81 offset = sizeof(struct hfs_bnode_desc);
82 *--rec_offsets = cpu_to_be16(offset);
83
84 head = (struct hfs_btree_header_rec *)(buf + offset);
85 head->node_size = cpu_to_be16(node_size);
Geert Uytterhoevena99b7062013-11-14 14:32:18 -080086 tmp = i_size_read(attr_file);
87 do_div(tmp, node_size);
88 head->node_count = cpu_to_be32(tmp);
Vyacheslav Dubeyko099e9242013-11-12 15:11:08 -080089 head->free_nodes = cpu_to_be32(be32_to_cpu(head->node_count) - 1);
90 head->clump_size = cpu_to_be32(clump_size);
91 head->attributes |= cpu_to_be32(HFS_TREE_BIGKEYS | HFS_TREE_VARIDXKEYS);
92 head->max_key_len = cpu_to_be16(HFSPLUS_ATTR_KEYLEN - sizeof(u16));
93 offset += sizeof(struct hfs_btree_header_rec);
94 *--rec_offsets = cpu_to_be16(offset);
95 offset += HFSPLUS_BTREE_HDR_USER_BYTES;
96 *--rec_offsets = cpu_to_be16(offset);
97
98 hdr_node_map_rec_bits = 8 * (node_size - offset - (4 * sizeof(u16)));
99 if (be32_to_cpu(head->node_count) > hdr_node_map_rec_bits) {
100 u32 map_node_bits;
101 u32 map_nodes;
102
103 desc->next = cpu_to_be32(be32_to_cpu(head->leaf_tail) + 1);
104 map_node_bits = 8 * (node_size - sizeof(struct hfs_bnode_desc) -
105 (2 * sizeof(u16)) - 2);
106 map_nodes = (be32_to_cpu(head->node_count) -
107 hdr_node_map_rec_bits +
108 (map_node_bits - 1)) / map_node_bits;
109 be32_add_cpu(&head->free_nodes, 0 - map_nodes);
110 }
111
112 bmp = buf + offset;
113 used_nodes =
114 be32_to_cpu(head->node_count) - be32_to_cpu(head->free_nodes);
115 used_bmp_bytes = used_nodes / 8;
116 if (used_bmp_bytes) {
117 memset(bmp, 0xFF, used_bmp_bytes);
118 bmp += used_bmp_bytes;
119 used_nodes %= 8;
120 }
121 *bmp = ~(0xFF >> used_nodes);
122 offset += hdr_node_map_rec_bits / 8;
123 *--rec_offsets = cpu_to_be16(offset);
124}
125
Vyacheslav Dubeyko95e0d7d2013-11-12 15:11:09 -0800126static int hfsplus_create_attributes_file(struct super_block *sb)
127{
128 int err = 0;
129 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
130 struct inode *attr_file;
131 struct hfsplus_inode_info *hip;
132 u32 clump_size;
133 u16 node_size = HFSPLUS_ATTR_TREE_NODE_SIZE;
134 char *buf;
135 int index, written;
136 struct address_space *mapping;
137 struct page *page;
138 int old_state = HFSPLUS_EMPTY_ATTR_TREE;
139
140 hfs_dbg(ATTR_MOD, "create_attr_file: ino %d\n", HFSPLUS_ATTR_CNID);
141
142check_attr_tree_state_again:
143 switch (atomic_read(&sbi->attr_tree_state)) {
144 case HFSPLUS_EMPTY_ATTR_TREE:
145 if (old_state != atomic_cmpxchg(&sbi->attr_tree_state,
146 old_state,
147 HFSPLUS_CREATING_ATTR_TREE))
148 goto check_attr_tree_state_again;
149 break;
150 case HFSPLUS_CREATING_ATTR_TREE:
151 /*
152 * This state means that another thread is in process
153 * of AttributesFile creation. Theoretically, it is
154 * possible to be here. But really __setxattr() method
155 * first of all calls hfs_find_init() for lookup in
156 * B-tree of CatalogFile. This method locks mutex of
157 * CatalogFile's B-tree. As a result, if some thread
158 * is inside AttributedFile creation operation then
159 * another threads will be waiting unlocking of
160 * CatalogFile's B-tree's mutex. However, if code will
161 * change then we will return error code (-EAGAIN) from
162 * here. Really, it means that first try to set of xattr
163 * fails with error but second attempt will have success.
164 */
165 return -EAGAIN;
166 case HFSPLUS_VALID_ATTR_TREE:
167 return 0;
168 case HFSPLUS_FAILED_ATTR_TREE:
169 return -EOPNOTSUPP;
170 default:
171 BUG();
172 }
173
174 attr_file = hfsplus_iget(sb, HFSPLUS_ATTR_CNID);
175 if (IS_ERR(attr_file)) {
176 pr_err("failed to load attributes file\n");
177 return PTR_ERR(attr_file);
178 }
179
180 BUG_ON(i_size_read(attr_file) != 0);
181
182 hip = HFSPLUS_I(attr_file);
183
184 clump_size = hfsplus_calc_btree_clump_size(sb->s_blocksize,
185 node_size,
186 sbi->sect_count,
187 HFSPLUS_ATTR_CNID);
188
189 mutex_lock(&hip->extents_lock);
190 hip->clump_blocks = clump_size >> sbi->alloc_blksz_shift;
191 mutex_unlock(&hip->extents_lock);
192
193 if (sbi->free_blocks <= (hip->clump_blocks << 1)) {
194 err = -ENOSPC;
195 goto end_attr_file_creation;
196 }
197
198 while (hip->alloc_blocks < hip->clump_blocks) {
Sergei Antonov2cd282a2014-06-06 14:36:28 -0700199 err = hfsplus_file_extend(attr_file, false);
Vyacheslav Dubeyko95e0d7d2013-11-12 15:11:09 -0800200 if (unlikely(err)) {
201 pr_err("failed to extend attributes file\n");
202 goto end_attr_file_creation;
203 }
204 hip->phys_size = attr_file->i_size =
205 (loff_t)hip->alloc_blocks << sbi->alloc_blksz_shift;
206 hip->fs_blocks = hip->alloc_blocks << sbi->fs_shift;
207 inode_set_bytes(attr_file, attr_file->i_size);
208 }
209
210 buf = kzalloc(node_size, GFP_NOFS);
211 if (!buf) {
212 pr_err("failed to allocate memory for header node\n");
213 err = -ENOMEM;
214 goto end_attr_file_creation;
215 }
216
217 hfsplus_init_header_node(attr_file, clump_size, buf, node_size);
218
219 mapping = attr_file->i_mapping;
220
221 index = 0;
222 written = 0;
223 for (; written < node_size; index++, written += PAGE_CACHE_SIZE) {
224 void *kaddr;
225
226 page = read_mapping_page(mapping, index, NULL);
227 if (IS_ERR(page)) {
228 err = PTR_ERR(page);
229 goto failed_header_node_init;
230 }
231
232 kaddr = kmap_atomic(page);
233 memcpy(kaddr, buf + written,
234 min_t(size_t, PAGE_CACHE_SIZE, node_size - written));
235 kunmap_atomic(kaddr);
236
237 set_page_dirty(page);
238 page_cache_release(page);
239 }
240
241 hfsplus_mark_inode_dirty(attr_file, HFSPLUS_I_ATTR_DIRTY);
242
243 sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
244 if (!sbi->attr_tree)
245 pr_err("failed to load attributes file\n");
246
247failed_header_node_init:
248 kfree(buf);
249
250end_attr_file_creation:
251 iput(attr_file);
252
253 if (!err)
254 atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
255 else if (err == -ENOSPC)
256 atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
257 else
258 atomic_set(&sbi->attr_tree_state, HFSPLUS_FAILED_ATTR_TREE);
259
260 return err;
261}
262
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800263int __hfsplus_setxattr(struct inode *inode, const char *name,
264 const void *value, size_t size, int flags)
265{
266 int err = 0;
267 struct hfs_find_data cat_fd;
268 hfsplus_cat_entry entry;
269 u16 cat_entry_flags, cat_entry_type;
270 u16 folder_finderinfo_len = sizeof(struct DInfo) +
271 sizeof(struct DXInfo);
272 u16 file_finderinfo_len = sizeof(struct FInfo) +
273 sizeof(struct FXInfo);
274
275 if ((!S_ISREG(inode->i_mode) &&
276 !S_ISDIR(inode->i_mode)) ||
277 HFSPLUS_IS_RSRC(inode))
278 return -EOPNOTSUPP;
279
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800280 if (value == NULL)
281 return hfsplus_removexattr(inode, name);
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800282
283 err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
284 if (err) {
Joe Perchesd6142672013-04-30 15:27:55 -0700285 pr_err("can't init xattr find struct\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800286 return err;
287 }
288
289 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
290 if (err) {
Joe Perchesd6142672013-04-30 15:27:55 -0700291 pr_err("catalog searching failed\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800292 goto end_setxattr;
293 }
294
295 if (!strcmp_xattr_finder_info(name)) {
296 if (flags & XATTR_CREATE) {
Joe Perchesd6142672013-04-30 15:27:55 -0700297 pr_err("xattr exists yet\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800298 err = -EOPNOTSUPP;
299 goto end_setxattr;
300 }
301 hfs_bnode_read(cat_fd.bnode, &entry, cat_fd.entryoffset,
302 sizeof(hfsplus_cat_entry));
303 if (be16_to_cpu(entry.type) == HFSPLUS_FOLDER) {
304 if (size == folder_finderinfo_len) {
305 memcpy(&entry.folder.user_info, value,
306 folder_finderinfo_len);
307 hfs_bnode_write(cat_fd.bnode, &entry,
308 cat_fd.entryoffset,
309 sizeof(struct hfsplus_cat_folder));
310 hfsplus_mark_inode_dirty(inode,
311 HFSPLUS_I_CAT_DIRTY);
312 } else {
313 err = -ERANGE;
314 goto end_setxattr;
315 }
316 } else if (be16_to_cpu(entry.type) == HFSPLUS_FILE) {
317 if (size == file_finderinfo_len) {
318 memcpy(&entry.file.user_info, value,
319 file_finderinfo_len);
320 hfs_bnode_write(cat_fd.bnode, &entry,
321 cat_fd.entryoffset,
322 sizeof(struct hfsplus_cat_file));
323 hfsplus_mark_inode_dirty(inode,
324 HFSPLUS_I_CAT_DIRTY);
325 } else {
326 err = -ERANGE;
327 goto end_setxattr;
328 }
329 } else {
330 err = -EOPNOTSUPP;
331 goto end_setxattr;
332 }
333 goto end_setxattr;
334 }
335
336 if (!HFSPLUS_SB(inode->i_sb)->attr_tree) {
Vyacheslav Dubeyko95e0d7d2013-11-12 15:11:09 -0800337 err = hfsplus_create_attributes_file(inode->i_sb);
338 if (unlikely(err))
339 goto end_setxattr;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800340 }
341
342 if (hfsplus_attr_exists(inode, name)) {
343 if (flags & XATTR_CREATE) {
Joe Perchesd6142672013-04-30 15:27:55 -0700344 pr_err("xattr exists yet\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800345 err = -EOPNOTSUPP;
346 goto end_setxattr;
347 }
348 err = hfsplus_delete_attr(inode, name);
349 if (err)
350 goto end_setxattr;
351 err = hfsplus_create_attr(inode, name, value, size);
352 if (err)
353 goto end_setxattr;
354 } else {
355 if (flags & XATTR_REPLACE) {
Joe Perchesd6142672013-04-30 15:27:55 -0700356 pr_err("cannot replace xattr\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800357 err = -EOPNOTSUPP;
358 goto end_setxattr;
359 }
360 err = hfsplus_create_attr(inode, name, value, size);
361 if (err)
362 goto end_setxattr;
363 }
364
365 cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
366 if (cat_entry_type == HFSPLUS_FOLDER) {
367 cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
368 cat_fd.entryoffset +
369 offsetof(struct hfsplus_cat_folder, flags));
370 cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
371 if (!strcmp_xattr_acl(name))
372 cat_entry_flags |= HFSPLUS_ACL_EXISTS;
373 hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
374 offsetof(struct hfsplus_cat_folder, flags),
375 cat_entry_flags);
376 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
377 } else if (cat_entry_type == HFSPLUS_FILE) {
378 cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
379 cat_fd.entryoffset +
380 offsetof(struct hfsplus_cat_file, flags));
381 cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
382 if (!strcmp_xattr_acl(name))
383 cat_entry_flags |= HFSPLUS_ACL_EXISTS;
384 hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
385 offsetof(struct hfsplus_cat_file, flags),
386 cat_entry_flags);
387 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
388 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700389 pr_err("invalid catalog entry type\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800390 err = -EIO;
391 goto end_setxattr;
392 }
393
394end_setxattr:
395 hfs_find_exit(&cat_fd);
396 return err;
397}
398
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800399static int name_len(const char *xattr_name, int xattr_name_len)
400{
401 int len = xattr_name_len + 1;
402
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800403 if (!is_known_namespace(xattr_name))
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800404 len += XATTR_MAC_OSX_PREFIX_LEN;
405
406 return len;
407}
408
409static int copy_name(char *buffer, const char *xattr_name, int name_len)
410{
411 int len = name_len;
412 int offset = 0;
413
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800414 if (!is_known_namespace(xattr_name)) {
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800415 strncpy(buffer, XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN);
416 offset += XATTR_MAC_OSX_PREFIX_LEN;
417 len += XATTR_MAC_OSX_PREFIX_LEN;
418 }
419
420 strncpy(buffer + offset, xattr_name, name_len);
421 memset(buffer + offset + name_len, 0, 1);
422 len += 1;
423
424 return len;
425}
426
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700427static ssize_t hfsplus_getxattr_finder_info(struct inode *inode,
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800428 void *value, size_t size)
429{
430 ssize_t res = 0;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800431 struct hfs_find_data fd;
432 u16 entry_type;
433 u16 folder_rec_len = sizeof(struct DInfo) + sizeof(struct DXInfo);
434 u16 file_rec_len = sizeof(struct FInfo) + sizeof(struct FXInfo);
435 u16 record_len = max(folder_rec_len, file_rec_len);
436 u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
437 u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
438
439 if (size >= record_len) {
440 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
441 if (res) {
Joe Perchesd6142672013-04-30 15:27:55 -0700442 pr_err("can't init xattr find struct\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800443 return res;
444 }
445 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
446 if (res)
447 goto end_getxattr_finder_info;
448 entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
449
450 if (entry_type == HFSPLUS_FOLDER) {
451 hfs_bnode_read(fd.bnode, folder_finder_info,
452 fd.entryoffset +
453 offsetof(struct hfsplus_cat_folder, user_info),
454 folder_rec_len);
455 memcpy(value, folder_finder_info, folder_rec_len);
456 res = folder_rec_len;
457 } else if (entry_type == HFSPLUS_FILE) {
458 hfs_bnode_read(fd.bnode, file_finder_info,
459 fd.entryoffset +
460 offsetof(struct hfsplus_cat_file, user_info),
461 file_rec_len);
462 memcpy(value, file_finder_info, file_rec_len);
463 res = file_rec_len;
464 } else {
465 res = -EOPNOTSUPP;
466 goto end_getxattr_finder_info;
467 }
468 } else
469 res = size ? -ERANGE : record_len;
470
471end_getxattr_finder_info:
472 if (size >= record_len)
473 hfs_find_exit(&fd);
474 return res;
475}
476
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700477ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800478 void *value, size_t size)
479{
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800480 struct hfs_find_data fd;
481 hfsplus_attr_entry *entry;
482 __be32 xattr_record_type;
483 u32 record_type;
484 u16 record_length = 0;
485 ssize_t res = 0;
486
487 if ((!S_ISREG(inode->i_mode) &&
488 !S_ISDIR(inode->i_mode)) ||
489 HFSPLUS_IS_RSRC(inode))
490 return -EOPNOTSUPP;
491
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800492 if (!strcmp_xattr_finder_info(name))
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700493 return hfsplus_getxattr_finder_info(inode, value, size);
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800494
495 if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
496 return -EOPNOTSUPP;
497
498 entry = hfsplus_alloc_attr_entry();
499 if (!entry) {
Joe Perchesd6142672013-04-30 15:27:55 -0700500 pr_err("can't allocate xattr entry\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800501 return -ENOMEM;
502 }
503
504 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
505 if (res) {
Joe Perchesd6142672013-04-30 15:27:55 -0700506 pr_err("can't init xattr find struct\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800507 goto failed_getxattr_init;
508 }
509
510 res = hfsplus_find_attr(inode->i_sb, inode->i_ino, name, &fd);
511 if (res) {
512 if (res == -ENOENT)
513 res = -ENODATA;
514 else
Joe Perchesd6142672013-04-30 15:27:55 -0700515 pr_err("xattr searching failed\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800516 goto out;
517 }
518
519 hfs_bnode_read(fd.bnode, &xattr_record_type,
520 fd.entryoffset, sizeof(xattr_record_type));
521 record_type = be32_to_cpu(xattr_record_type);
522 if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
523 record_length = hfs_bnode_read_u16(fd.bnode,
524 fd.entryoffset +
525 offsetof(struct hfsplus_attr_inline_data,
526 length));
527 if (record_length > HFSPLUS_MAX_INLINE_DATA_SIZE) {
Joe Perchesd6142672013-04-30 15:27:55 -0700528 pr_err("invalid xattr record size\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800529 res = -EIO;
530 goto out;
531 }
532 } else if (record_type == HFSPLUS_ATTR_FORK_DATA ||
533 record_type == HFSPLUS_ATTR_EXTENTS) {
Joe Perchesd6142672013-04-30 15:27:55 -0700534 pr_err("only inline data xattr are supported\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800535 res = -EOPNOTSUPP;
536 goto out;
537 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700538 pr_err("invalid xattr record\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800539 res = -EIO;
540 goto out;
541 }
542
543 if (size) {
544 hfs_bnode_read(fd.bnode, entry, fd.entryoffset,
545 offsetof(struct hfsplus_attr_inline_data,
546 raw_bytes) + record_length);
547 }
548
549 if (size >= record_length) {
550 memcpy(value, entry->inline_data.raw_bytes, record_length);
551 res = record_length;
552 } else
553 res = size ? -ERANGE : record_length;
554
555out:
556 hfs_find_exit(&fd);
557
558failed_getxattr_init:
559 hfsplus_destroy_attr_entry(entry);
560 return res;
561}
562
563static inline int can_list(const char *xattr_name)
564{
565 if (!xattr_name)
566 return 0;
567
568 return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
569 XATTR_TRUSTED_PREFIX_LEN) ||
570 capable(CAP_SYS_ADMIN);
571}
572
573static ssize_t hfsplus_listxattr_finder_info(struct dentry *dentry,
574 char *buffer, size_t size)
575{
576 ssize_t res = 0;
577 struct inode *inode = dentry->d_inode;
578 struct hfs_find_data fd;
579 u16 entry_type;
580 u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
581 u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
582 unsigned long len, found_bit;
583 int xattr_name_len, symbols_count;
584
585 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
586 if (res) {
Joe Perchesd6142672013-04-30 15:27:55 -0700587 pr_err("can't init xattr find struct\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800588 return res;
589 }
590
591 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
592 if (res)
593 goto end_listxattr_finder_info;
594
595 entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
596 if (entry_type == HFSPLUS_FOLDER) {
597 len = sizeof(struct DInfo) + sizeof(struct DXInfo);
598 hfs_bnode_read(fd.bnode, folder_finder_info,
599 fd.entryoffset +
600 offsetof(struct hfsplus_cat_folder, user_info),
601 len);
602 found_bit = find_first_bit((void *)folder_finder_info, len*8);
603 } else if (entry_type == HFSPLUS_FILE) {
604 len = sizeof(struct FInfo) + sizeof(struct FXInfo);
605 hfs_bnode_read(fd.bnode, file_finder_info,
606 fd.entryoffset +
607 offsetof(struct hfsplus_cat_file, user_info),
608 len);
609 found_bit = find_first_bit((void *)file_finder_info, len*8);
610 } else {
611 res = -EOPNOTSUPP;
612 goto end_listxattr_finder_info;
613 }
614
615 if (found_bit >= (len*8))
616 res = 0;
617 else {
618 symbols_count = sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME) - 1;
619 xattr_name_len =
620 name_len(HFSPLUS_XATTR_FINDER_INFO_NAME, symbols_count);
621 if (!buffer || !size) {
622 if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME))
623 res = xattr_name_len;
624 } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME)) {
625 if (size < xattr_name_len)
626 res = -ERANGE;
627 else {
628 res = copy_name(buffer,
629 HFSPLUS_XATTR_FINDER_INFO_NAME,
630 symbols_count);
631 }
632 }
633 }
634
635end_listxattr_finder_info:
636 hfs_find_exit(&fd);
637
638 return res;
639}
640
641ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
642{
643 ssize_t err;
644 ssize_t res = 0;
645 struct inode *inode = dentry->d_inode;
646 struct hfs_find_data fd;
647 u16 key_len = 0;
648 struct hfsplus_attr_key attr_key;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700649 char *strbuf;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800650 int xattr_name_len;
651
652 if ((!S_ISREG(inode->i_mode) &&
653 !S_ISDIR(inode->i_mode)) ||
654 HFSPLUS_IS_RSRC(inode))
655 return -EOPNOTSUPP;
656
657 res = hfsplus_listxattr_finder_info(dentry, buffer, size);
658 if (res < 0)
659 return res;
660 else if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
661 return (res == 0) ? -EOPNOTSUPP : res;
662
663 err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
664 if (err) {
Joe Perchesd6142672013-04-30 15:27:55 -0700665 pr_err("can't init xattr find struct\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800666 return err;
667 }
668
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700669 strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN +
670 XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
671 if (!strbuf) {
672 res = -ENOMEM;
673 goto out;
674 }
675
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800676 err = hfsplus_find_attr(inode->i_sb, inode->i_ino, NULL, &fd);
677 if (err) {
678 if (err == -ENOENT) {
679 if (res == 0)
680 res = -ENODATA;
681 goto end_listxattr;
682 } else {
683 res = err;
684 goto end_listxattr;
685 }
686 }
687
688 for (;;) {
689 key_len = hfs_bnode_read_u16(fd.bnode, fd.keyoffset);
690 if (key_len == 0 || key_len > fd.tree->max_key_len) {
Joe Perchesd6142672013-04-30 15:27:55 -0700691 pr_err("invalid xattr key length: %d\n", key_len);
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800692 res = -EIO;
693 goto end_listxattr;
694 }
695
696 hfs_bnode_read(fd.bnode, &attr_key,
697 fd.keyoffset, key_len + sizeof(key_len));
698
699 if (be32_to_cpu(attr_key.cnid) != inode->i_ino)
700 goto end_listxattr;
701
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700702 xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800703 if (hfsplus_uni2asc(inode->i_sb,
704 (const struct hfsplus_unistr *)&fd.key->attr.key_name,
705 strbuf, &xattr_name_len)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700706 pr_err("unicode conversion failed\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800707 res = -EIO;
708 goto end_listxattr;
709 }
710
711 if (!buffer || !size) {
712 if (can_list(strbuf))
713 res += name_len(strbuf, xattr_name_len);
714 } else if (can_list(strbuf)) {
715 if (size < (res + name_len(strbuf, xattr_name_len))) {
716 res = -ERANGE;
717 goto end_listxattr;
718 } else
719 res += copy_name(buffer + res,
720 strbuf, xattr_name_len);
721 }
722
723 if (hfs_brec_goto(&fd, 1))
724 goto end_listxattr;
725 }
726
727end_listxattr:
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700728 kfree(strbuf);
729out:
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800730 hfs_find_exit(&fd);
731 return res;
732}
733
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800734static int hfsplus_removexattr(struct inode *inode, const char *name)
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800735{
736 int err = 0;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800737 struct hfs_find_data cat_fd;
738 u16 flags;
739 u16 cat_entry_type;
740 int is_xattr_acl_deleted = 0;
741 int is_all_xattrs_deleted = 0;
742
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800743 if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
744 return -EOPNOTSUPP;
745
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800746 if (!strcmp_xattr_finder_info(name))
747 return -EOPNOTSUPP;
748
749 err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
750 if (err) {
Joe Perchesd6142672013-04-30 15:27:55 -0700751 pr_err("can't init xattr find struct\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800752 return err;
753 }
754
755 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
756 if (err) {
Joe Perchesd6142672013-04-30 15:27:55 -0700757 pr_err("catalog searching failed\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800758 goto end_removexattr;
759 }
760
761 err = hfsplus_delete_attr(inode, name);
762 if (err)
763 goto end_removexattr;
764
765 is_xattr_acl_deleted = !strcmp_xattr_acl(name);
766 is_all_xattrs_deleted = !hfsplus_attr_exists(inode, NULL);
767
768 if (!is_xattr_acl_deleted && !is_all_xattrs_deleted)
769 goto end_removexattr;
770
771 cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
772
773 if (cat_entry_type == HFSPLUS_FOLDER) {
774 flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
775 offsetof(struct hfsplus_cat_folder, flags));
776 if (is_xattr_acl_deleted)
777 flags &= ~HFSPLUS_ACL_EXISTS;
778 if (is_all_xattrs_deleted)
779 flags &= ~HFSPLUS_XATTR_EXISTS;
780 hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
781 offsetof(struct hfsplus_cat_folder, flags),
782 flags);
783 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
784 } else if (cat_entry_type == HFSPLUS_FILE) {
785 flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
786 offsetof(struct hfsplus_cat_file, flags));
787 if (is_xattr_acl_deleted)
788 flags &= ~HFSPLUS_ACL_EXISTS;
789 if (is_all_xattrs_deleted)
790 flags &= ~HFSPLUS_XATTR_EXISTS;
791 hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
792 offsetof(struct hfsplus_cat_file, flags),
793 flags);
794 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
795 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700796 pr_err("invalid catalog entry type\n");
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800797 err = -EIO;
798 goto end_removexattr;
799 }
800
801end_removexattr:
802 hfs_find_exit(&cat_fd);
803 return err;
804}
805
806static int hfsplus_osx_getxattr(struct dentry *dentry, const char *name,
807 void *buffer, size_t size, int type)
808{
Hin-Tak Leungbf29e882014-06-06 14:36:22 -0700809 char *xattr_name;
810 int res;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800811
812 if (!strcmp(name, ""))
813 return -EINVAL;
814
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800815 /*
816 * Don't allow retrieving properly prefixed attributes
817 * by prepending them with "osx."
818 */
819 if (is_known_namespace(name))
820 return -EOPNOTSUPP;
Hin-Tak Leungbf29e882014-06-06 14:36:22 -0700821 xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN
822 + XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
823 if (!xattr_name)
824 return -ENOMEM;
825 strcpy(xattr_name, XATTR_MAC_OSX_PREFIX);
826 strcpy(xattr_name + XATTR_MAC_OSX_PREFIX_LEN, name);
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800827
Hin-Tak Leungbf29e882014-06-06 14:36:22 -0700828 res = hfsplus_getxattr(dentry, xattr_name, buffer, size);
829 kfree(xattr_name);
830 return res;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800831}
832
833static int hfsplus_osx_setxattr(struct dentry *dentry, const char *name,
834 const void *buffer, size_t size, int flags, int type)
835{
Hin-Tak Leungbf29e882014-06-06 14:36:22 -0700836 char *xattr_name;
837 int res;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800838
839 if (!strcmp(name, ""))
840 return -EINVAL;
841
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800842 /*
843 * Don't allow setting properly prefixed attributes
844 * by prepending them with "osx."
845 */
Christoph Hellwig2796e4c2013-12-20 05:16:56 -0800846 if (is_known_namespace(name))
847 return -EOPNOTSUPP;
Hin-Tak Leungbf29e882014-06-06 14:36:22 -0700848 xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN
849 + XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
850 if (!xattr_name)
851 return -ENOMEM;
852 strcpy(xattr_name, XATTR_MAC_OSX_PREFIX);
853 strcpy(xattr_name + XATTR_MAC_OSX_PREFIX_LEN, name);
Christoph Hellwig2796e4c2013-12-20 05:16:56 -0800854
Hin-Tak Leungbf29e882014-06-06 14:36:22 -0700855 res = hfsplus_setxattr(dentry, xattr_name, buffer, size, flags);
856 kfree(xattr_name);
857 return res;
Vyacheslav Dubeyko127e5f52013-02-27 17:03:03 -0800858}
859
860static size_t hfsplus_osx_listxattr(struct dentry *dentry, char *list,
861 size_t list_size, const char *name, size_t name_len, int type)
862{
863 /*
864 * This method is not used.
865 * It is used hfsplus_listxattr() instead of generic_listxattr().
866 */
867 return -EOPNOTSUPP;
868}
869
870const struct xattr_handler hfsplus_xattr_osx_handler = {
871 .prefix = XATTR_MAC_OSX_PREFIX,
872 .list = hfsplus_osx_listxattr,
873 .get = hfsplus_osx_getxattr,
874 .set = hfsplus_osx_setxattr,
875};