xref: /linux/fs/hfsplus/xattr.c (revision c411ed854584a71b0e86ac3019b60e4789d88086)
1 /*
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"
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/nls.h>
12 #include "xattr.h"
13 #include "acl.h"
14 
15 static int hfsplus_removexattr(struct inode *inode, const char *name);
16 
17 const struct xattr_handler *hfsplus_xattr_handlers[] = {
18 	&hfsplus_xattr_osx_handler,
19 	&hfsplus_xattr_user_handler,
20 	&hfsplus_xattr_trusted_handler,
21 #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
22 	&posix_acl_access_xattr_handler,
23 	&posix_acl_default_xattr_handler,
24 #endif
25 	&hfsplus_xattr_security_handler,
26 	NULL
27 };
28 
29 static 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 
38 static 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 
47 static bool 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 
58 static void hfsplus_init_header_node(struct inode *attr_file,
59 					u32 clump_size,
60 					char *buf, u16 node_size)
61 {
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;
70 	u64 tmp;
71 
72 	hfs_dbg(ATTR_MOD, "init_hdr_attr_file: clump %u, node_size %u\n",
73 		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);
86 	tmp = i_size_read(attr_file);
87 	do_div(tmp, node_size);
88 	head->node_count = cpu_to_be32(tmp);
89 	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 
126 static 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 
142 check_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) {
199 		err = hfsplus_file_extend(attr_file, false);
200 		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_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_SIZE, node_size - written));
235 		kunmap_atomic(kaddr);
236 
237 		set_page_dirty(page);
238 		put_page(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 
247 failed_header_node_init:
248 	kfree(buf);
249 
250 end_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 
263 int __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 
280 	if (value == NULL)
281 		return hfsplus_removexattr(inode, name);
282 
283 	err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
284 	if (err) {
285 		pr_err("can't init xattr find struct\n");
286 		return err;
287 	}
288 
289 	err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
290 	if (err) {
291 		pr_err("catalog searching failed\n");
292 		goto end_setxattr;
293 	}
294 
295 	if (!strcmp_xattr_finder_info(name)) {
296 		if (flags & XATTR_CREATE) {
297 			pr_err("xattr exists yet\n");
298 			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) {
337 		err = hfsplus_create_attributes_file(inode->i_sb);
338 		if (unlikely(err))
339 			goto end_setxattr;
340 	}
341 
342 	if (hfsplus_attr_exists(inode, name)) {
343 		if (flags & XATTR_CREATE) {
344 			pr_err("xattr exists yet\n");
345 			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) {
356 			pr_err("cannot replace xattr\n");
357 			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 {
389 		pr_err("invalid catalog entry type\n");
390 		err = -EIO;
391 		goto end_setxattr;
392 	}
393 
394 end_setxattr:
395 	hfs_find_exit(&cat_fd);
396 	return err;
397 }
398 
399 static int name_len(const char *xattr_name, int xattr_name_len)
400 {
401 	int len = xattr_name_len + 1;
402 
403 	if (!is_known_namespace(xattr_name))
404 		len += XATTR_MAC_OSX_PREFIX_LEN;
405 
406 	return len;
407 }
408 
409 static int copy_name(char *buffer, const char *xattr_name, int name_len)
410 {
411 	int len = name_len;
412 	int offset = 0;
413 
414 	if (!is_known_namespace(xattr_name)) {
415 		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 
427 int hfsplus_setxattr(struct inode *inode, const char *name,
428 		     const void *value, size_t size, int flags,
429 		     const char *prefix, size_t prefixlen)
430 {
431 	char *xattr_name;
432 	int res;
433 
434 	xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
435 		GFP_KERNEL);
436 	if (!xattr_name)
437 		return -ENOMEM;
438 	strcpy(xattr_name, prefix);
439 	strcpy(xattr_name + prefixlen, name);
440 	res = __hfsplus_setxattr(inode, xattr_name, value, size, flags);
441 	kfree(xattr_name);
442 	return res;
443 }
444 
445 static ssize_t hfsplus_getxattr_finder_info(struct inode *inode,
446 						void *value, size_t size)
447 {
448 	ssize_t res = 0;
449 	struct hfs_find_data fd;
450 	u16 entry_type;
451 	u16 folder_rec_len = sizeof(struct DInfo) + sizeof(struct DXInfo);
452 	u16 file_rec_len = sizeof(struct FInfo) + sizeof(struct FXInfo);
453 	u16 record_len = max(folder_rec_len, file_rec_len);
454 	u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
455 	u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
456 
457 	if (size >= record_len) {
458 		res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
459 		if (res) {
460 			pr_err("can't init xattr find struct\n");
461 			return res;
462 		}
463 		res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
464 		if (res)
465 			goto end_getxattr_finder_info;
466 		entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
467 
468 		if (entry_type == HFSPLUS_FOLDER) {
469 			hfs_bnode_read(fd.bnode, folder_finder_info,
470 				fd.entryoffset +
471 				offsetof(struct hfsplus_cat_folder, user_info),
472 				folder_rec_len);
473 			memcpy(value, folder_finder_info, folder_rec_len);
474 			res = folder_rec_len;
475 		} else if (entry_type == HFSPLUS_FILE) {
476 			hfs_bnode_read(fd.bnode, file_finder_info,
477 				fd.entryoffset +
478 				offsetof(struct hfsplus_cat_file, user_info),
479 				file_rec_len);
480 			memcpy(value, file_finder_info, file_rec_len);
481 			res = file_rec_len;
482 		} else {
483 			res = -EOPNOTSUPP;
484 			goto end_getxattr_finder_info;
485 		}
486 	} else
487 		res = size ? -ERANGE : record_len;
488 
489 end_getxattr_finder_info:
490 	if (size >= record_len)
491 		hfs_find_exit(&fd);
492 	return res;
493 }
494 
495 ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
496 			 void *value, size_t size)
497 {
498 	struct hfs_find_data fd;
499 	hfsplus_attr_entry *entry;
500 	__be32 xattr_record_type;
501 	u32 record_type;
502 	u16 record_length = 0;
503 	ssize_t res = 0;
504 
505 	if ((!S_ISREG(inode->i_mode) &&
506 			!S_ISDIR(inode->i_mode)) ||
507 				HFSPLUS_IS_RSRC(inode))
508 		return -EOPNOTSUPP;
509 
510 	if (!strcmp_xattr_finder_info(name))
511 		return hfsplus_getxattr_finder_info(inode, value, size);
512 
513 	if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
514 		return -EOPNOTSUPP;
515 
516 	entry = hfsplus_alloc_attr_entry();
517 	if (!entry) {
518 		pr_err("can't allocate xattr entry\n");
519 		return -ENOMEM;
520 	}
521 
522 	res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
523 	if (res) {
524 		pr_err("can't init xattr find struct\n");
525 		goto failed_getxattr_init;
526 	}
527 
528 	res = hfsplus_find_attr(inode->i_sb, inode->i_ino, name, &fd);
529 	if (res) {
530 		if (res == -ENOENT)
531 			res = -ENODATA;
532 		else
533 			pr_err("xattr searching failed\n");
534 		goto out;
535 	}
536 
537 	hfs_bnode_read(fd.bnode, &xattr_record_type,
538 			fd.entryoffset, sizeof(xattr_record_type));
539 	record_type = be32_to_cpu(xattr_record_type);
540 	if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
541 		record_length = hfs_bnode_read_u16(fd.bnode,
542 				fd.entryoffset +
543 				offsetof(struct hfsplus_attr_inline_data,
544 				length));
545 		if (record_length > HFSPLUS_MAX_INLINE_DATA_SIZE) {
546 			pr_err("invalid xattr record size\n");
547 			res = -EIO;
548 			goto out;
549 		}
550 	} else if (record_type == HFSPLUS_ATTR_FORK_DATA ||
551 			record_type == HFSPLUS_ATTR_EXTENTS) {
552 		pr_err("only inline data xattr are supported\n");
553 		res = -EOPNOTSUPP;
554 		goto out;
555 	} else {
556 		pr_err("invalid xattr record\n");
557 		res = -EIO;
558 		goto out;
559 	}
560 
561 	if (size) {
562 		hfs_bnode_read(fd.bnode, entry, fd.entryoffset,
563 				offsetof(struct hfsplus_attr_inline_data,
564 					raw_bytes) + record_length);
565 	}
566 
567 	if (size >= record_length) {
568 		memcpy(value, entry->inline_data.raw_bytes, record_length);
569 		res = record_length;
570 	} else
571 		res = size ? -ERANGE : record_length;
572 
573 out:
574 	hfs_find_exit(&fd);
575 
576 failed_getxattr_init:
577 	hfsplus_destroy_attr_entry(entry);
578 	return res;
579 }
580 
581 ssize_t hfsplus_getxattr(struct inode *inode, const char *name,
582 			 void *value, size_t size,
583 			 const char *prefix, size_t prefixlen)
584 {
585 	int res;
586 	char *xattr_name;
587 
588 	xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
589 			     GFP_KERNEL);
590 	if (!xattr_name)
591 		return -ENOMEM;
592 
593 	strcpy(xattr_name, prefix);
594 	strcpy(xattr_name + prefixlen, name);
595 
596 	res = __hfsplus_getxattr(inode, xattr_name, value, size);
597 	kfree(xattr_name);
598 	return res;
599 
600 }
601 
602 static inline int can_list(const char *xattr_name)
603 {
604 	if (!xattr_name)
605 		return 0;
606 
607 	return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
608 			XATTR_TRUSTED_PREFIX_LEN) ||
609 				capable(CAP_SYS_ADMIN);
610 }
611 
612 static ssize_t hfsplus_listxattr_finder_info(struct dentry *dentry,
613 						char *buffer, size_t size)
614 {
615 	ssize_t res = 0;
616 	struct inode *inode = d_inode(dentry);
617 	struct hfs_find_data fd;
618 	u16 entry_type;
619 	u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
620 	u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
621 	unsigned long len, found_bit;
622 	int xattr_name_len, symbols_count;
623 
624 	res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
625 	if (res) {
626 		pr_err("can't init xattr find struct\n");
627 		return res;
628 	}
629 
630 	res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
631 	if (res)
632 		goto end_listxattr_finder_info;
633 
634 	entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
635 	if (entry_type == HFSPLUS_FOLDER) {
636 		len = sizeof(struct DInfo) + sizeof(struct DXInfo);
637 		hfs_bnode_read(fd.bnode, folder_finder_info,
638 				fd.entryoffset +
639 				offsetof(struct hfsplus_cat_folder, user_info),
640 				len);
641 		found_bit = find_first_bit((void *)folder_finder_info, len*8);
642 	} else if (entry_type == HFSPLUS_FILE) {
643 		len = sizeof(struct FInfo) + sizeof(struct FXInfo);
644 		hfs_bnode_read(fd.bnode, file_finder_info,
645 				fd.entryoffset +
646 				offsetof(struct hfsplus_cat_file, user_info),
647 				len);
648 		found_bit = find_first_bit((void *)file_finder_info, len*8);
649 	} else {
650 		res = -EOPNOTSUPP;
651 		goto end_listxattr_finder_info;
652 	}
653 
654 	if (found_bit >= (len*8))
655 		res = 0;
656 	else {
657 		symbols_count = sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME) - 1;
658 		xattr_name_len =
659 			name_len(HFSPLUS_XATTR_FINDER_INFO_NAME, symbols_count);
660 		if (!buffer || !size) {
661 			if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME))
662 				res = xattr_name_len;
663 		} else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME)) {
664 			if (size < xattr_name_len)
665 				res = -ERANGE;
666 			else {
667 				res = copy_name(buffer,
668 						HFSPLUS_XATTR_FINDER_INFO_NAME,
669 						symbols_count);
670 			}
671 		}
672 	}
673 
674 end_listxattr_finder_info:
675 	hfs_find_exit(&fd);
676 
677 	return res;
678 }
679 
680 ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
681 {
682 	ssize_t err;
683 	ssize_t res = 0;
684 	struct inode *inode = d_inode(dentry);
685 	struct hfs_find_data fd;
686 	u16 key_len = 0;
687 	struct hfsplus_attr_key attr_key;
688 	char *strbuf;
689 	int xattr_name_len;
690 
691 	if ((!S_ISREG(inode->i_mode) &&
692 			!S_ISDIR(inode->i_mode)) ||
693 				HFSPLUS_IS_RSRC(inode))
694 		return -EOPNOTSUPP;
695 
696 	res = hfsplus_listxattr_finder_info(dentry, buffer, size);
697 	if (res < 0)
698 		return res;
699 	else if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
700 		return (res == 0) ? -EOPNOTSUPP : res;
701 
702 	err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
703 	if (err) {
704 		pr_err("can't init xattr find struct\n");
705 		return err;
706 	}
707 
708 	strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN +
709 			XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
710 	if (!strbuf) {
711 		res = -ENOMEM;
712 		goto out;
713 	}
714 
715 	err = hfsplus_find_attr(inode->i_sb, inode->i_ino, NULL, &fd);
716 	if (err) {
717 		if (err == -ENOENT) {
718 			if (res == 0)
719 				res = -ENODATA;
720 			goto end_listxattr;
721 		} else {
722 			res = err;
723 			goto end_listxattr;
724 		}
725 	}
726 
727 	for (;;) {
728 		key_len = hfs_bnode_read_u16(fd.bnode, fd.keyoffset);
729 		if (key_len == 0 || key_len > fd.tree->max_key_len) {
730 			pr_err("invalid xattr key length: %d\n", key_len);
731 			res = -EIO;
732 			goto end_listxattr;
733 		}
734 
735 		hfs_bnode_read(fd.bnode, &attr_key,
736 				fd.keyoffset, key_len + sizeof(key_len));
737 
738 		if (be32_to_cpu(attr_key.cnid) != inode->i_ino)
739 			goto end_listxattr;
740 
741 		xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN;
742 		if (hfsplus_uni2asc(inode->i_sb,
743 			(const struct hfsplus_unistr *)&fd.key->attr.key_name,
744 					strbuf, &xattr_name_len)) {
745 			pr_err("unicode conversion failed\n");
746 			res = -EIO;
747 			goto end_listxattr;
748 		}
749 
750 		if (!buffer || !size) {
751 			if (can_list(strbuf))
752 				res += name_len(strbuf, xattr_name_len);
753 		} else if (can_list(strbuf)) {
754 			if (size < (res + name_len(strbuf, xattr_name_len))) {
755 				res = -ERANGE;
756 				goto end_listxattr;
757 			} else
758 				res += copy_name(buffer + res,
759 						strbuf, xattr_name_len);
760 		}
761 
762 		if (hfs_brec_goto(&fd, 1))
763 			goto end_listxattr;
764 	}
765 
766 end_listxattr:
767 	kfree(strbuf);
768 out:
769 	hfs_find_exit(&fd);
770 	return res;
771 }
772 
773 static int hfsplus_removexattr(struct inode *inode, const char *name)
774 {
775 	int err = 0;
776 	struct hfs_find_data cat_fd;
777 	u16 flags;
778 	u16 cat_entry_type;
779 	int is_xattr_acl_deleted = 0;
780 	int is_all_xattrs_deleted = 0;
781 
782 	if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
783 		return -EOPNOTSUPP;
784 
785 	if (!strcmp_xattr_finder_info(name))
786 		return -EOPNOTSUPP;
787 
788 	err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
789 	if (err) {
790 		pr_err("can't init xattr find struct\n");
791 		return err;
792 	}
793 
794 	err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
795 	if (err) {
796 		pr_err("catalog searching failed\n");
797 		goto end_removexattr;
798 	}
799 
800 	err = hfsplus_delete_attr(inode, name);
801 	if (err)
802 		goto end_removexattr;
803 
804 	is_xattr_acl_deleted = !strcmp_xattr_acl(name);
805 	is_all_xattrs_deleted = !hfsplus_attr_exists(inode, NULL);
806 
807 	if (!is_xattr_acl_deleted && !is_all_xattrs_deleted)
808 		goto end_removexattr;
809 
810 	cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
811 
812 	if (cat_entry_type == HFSPLUS_FOLDER) {
813 		flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
814 				offsetof(struct hfsplus_cat_folder, flags));
815 		if (is_xattr_acl_deleted)
816 			flags &= ~HFSPLUS_ACL_EXISTS;
817 		if (is_all_xattrs_deleted)
818 			flags &= ~HFSPLUS_XATTR_EXISTS;
819 		hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
820 				offsetof(struct hfsplus_cat_folder, flags),
821 				flags);
822 		hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
823 	} else if (cat_entry_type == HFSPLUS_FILE) {
824 		flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
825 				offsetof(struct hfsplus_cat_file, flags));
826 		if (is_xattr_acl_deleted)
827 			flags &= ~HFSPLUS_ACL_EXISTS;
828 		if (is_all_xattrs_deleted)
829 			flags &= ~HFSPLUS_XATTR_EXISTS;
830 		hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
831 				offsetof(struct hfsplus_cat_file, flags),
832 				flags);
833 		hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
834 	} else {
835 		pr_err("invalid catalog entry type\n");
836 		err = -EIO;
837 		goto end_removexattr;
838 	}
839 
840 end_removexattr:
841 	hfs_find_exit(&cat_fd);
842 	return err;
843 }
844 
845 static int hfsplus_osx_getxattr(const struct xattr_handler *handler,
846 				struct dentry *unused, struct inode *inode,
847 				const char *name, void *buffer, size_t size)
848 {
849 	/*
850 	 * Don't allow retrieving properly prefixed attributes
851 	 * by prepending them with "osx."
852 	 */
853 	if (is_known_namespace(name))
854 		return -EOPNOTSUPP;
855 
856 	/*
857 	 * osx is the namespace we use to indicate an unprefixed
858 	 * attribute on the filesystem (like the ones that OS X
859 	 * creates), so we pass the name through unmodified (after
860 	 * ensuring it doesn't conflict with another namespace).
861 	 */
862 	return __hfsplus_getxattr(inode, name, buffer, size);
863 }
864 
865 static int hfsplus_osx_setxattr(const struct xattr_handler *handler,
866 				struct dentry *unused, struct inode *inode,
867 				const char *name, const void *buffer,
868 				size_t size, int flags)
869 {
870 	/*
871 	 * Don't allow setting properly prefixed attributes
872 	 * by prepending them with "osx."
873 	 */
874 	if (is_known_namespace(name))
875 		return -EOPNOTSUPP;
876 
877 	/*
878 	 * osx is the namespace we use to indicate an unprefixed
879 	 * attribute on the filesystem (like the ones that OS X
880 	 * creates), so we pass the name through unmodified (after
881 	 * ensuring it doesn't conflict with another namespace).
882 	 */
883 	return __hfsplus_setxattr(inode, name, buffer, size, flags);
884 }
885 
886 const struct xattr_handler hfsplus_xattr_osx_handler = {
887 	.prefix	= XATTR_MAC_OSX_PREFIX,
888 	.get	= hfsplus_osx_getxattr,
889 	.set	= hfsplus_osx_setxattr,
890 };
891