1 /**
2  * xattr.h
3  *
4  * Many parts of codes are copied from Linux kernel/fs/f2fs.
5  *
6  * Copyright (C) 2015 Huawei Ltd.
7  * Witten by:
8  *   Hou Pengyang <houpengyang@huawei.com>
9  *   Liu Shuoran <liushuoran@huawei.com>
10  *   Jaegeuk Kim <jaegeuk@kernel.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16 #ifndef _XATTR_H_
17 #define _XATTR_H_
18 
19 #include "f2fs.h"
20 #ifdef HAVE_SYS_XATTR_H
21 #include <sys/xattr.h>
22 #endif
23 
24 struct f2fs_xattr_header {
25 	__le32 h_magic;		/* magic number for identification */
26 	__le32 h_refcount;	/* reference count */
27 	__u32 h_sloadd[4];	/* zero right now */
28 };
29 
30 struct f2fs_xattr_entry {
31 	__u8 e_name_index;
32 	__u8 e_name_len;
33 	__le16 e_value_size;	/* size of attribute value */
34 	char e_name[0];		/* attribute name */
35 };
36 
37 #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
38 #define FS_KEY_DESCRIPTOR_SIZE 8
39 #define FS_KEY_DERIVATION_NONCE_SIZE 16
40 
41 struct fscrypt_context {
42 	u8 format;
43 	u8 contents_encryption_mode;
44 	u8 filenames_encryption_mode;
45 	u8 flags;
46 	u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
47 	u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
48 } __attribute__((packed));
49 
50 #define F2FS_ACL_VERSION	0x0001
51 
52 struct f2fs_acl_entry {
53 	__le16 e_tag;
54 	__le16 e_perm;
55 	__le32 e_id;
56 };
57 
58 struct f2fs_acl_entry_short {
59 	__le16 e_tag;
60 	__le16 e_perm;
61 };
62 
63 struct f2fs_acl_header {
64 	__le32 a_version;
65 };
66 
f2fs_acl_count(int size)67 static inline int f2fs_acl_count(int size)
68 {
69 	ssize_t s;
70 	size -= sizeof(struct f2fs_acl_header);
71 	s = size - 4 * sizeof(struct f2fs_acl_entry_short);
72 	if (s < 0) {
73 		if (size % sizeof(struct f2fs_acl_entry_short))
74 			return -1;
75 		return size / sizeof(struct f2fs_acl_entry_short);
76 	} else {
77 		if (s % sizeof(struct f2fs_acl_entry))
78 			return -1;
79 		return s / sizeof(struct f2fs_acl_entry) + 4;
80 	}
81 }
82 
83 #ifndef XATTR_USER_PREFIX
84 #define XATTR_USER_PREFIX	"user."
85 #endif
86 #ifndef XATTR_SECURITY_PREFIX
87 #define XATTR_SECURITY_PREFIX	"security."
88 #endif
89 #ifndef XATTR_TRUSTED_PREFIX
90 #define XATTR_TRUSTED_PREFIX	"trusted."
91 #endif
92 
93 #ifndef XATTR_CREATE
94 #define XATTR_CREATE 0x1
95 #endif
96 #ifndef XATTR_REPLACE
97 #define XATTR_REPLACE 0x2
98 #endif
99 
100 #define XATTR_ROUND	(3)
101 
102 #define XATTR_SELINUX_SUFFIX "selinux"
103 #define F2FS_XATTR_INDEX_USER			1
104 #define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS	2
105 #define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT	3
106 #define F2FS_XATTR_INDEX_TRUSTED		4
107 #define F2FS_XATTR_INDEX_LUSTRE			5
108 #define F2FS_XATTR_INDEX_SECURITY		6
109 #define F2FS_XATTR_INDEX_ENCRYPTION		9
110 
111 #define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
112 
113 #define XATTR_HDR(ptr)		((struct f2fs_xattr_header *)(ptr))
114 #define XATTR_ENTRY(ptr) 	((struct f2fs_xattr_entry *)(ptr))
115 #define F2FS_XATTR_MAGIC	0xF2F52011
116 
117 #define XATTR_NEXT_ENTRY(entry) ((struct f2fs_xattr_entry *) ((char *)(entry) +\
118 					ENTRY_SIZE(entry)))
119 #define XATTR_FIRST_ENTRY(ptr)	(XATTR_ENTRY(XATTR_HDR(ptr) + 1))
120 
121 #define XATTR_ALIGN(size)	((size + XATTR_ROUND) & ~XATTR_ROUND)
122 
123 #define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
124 			entry->e_name_len + le16_to_cpu(entry->e_value_size)))
125 
126 #define list_for_each_xattr(entry, addr) \
127 	for (entry = XATTR_FIRST_ENTRY(addr); \
128 			!IS_XATTR_LAST_ENTRY(entry); \
129 			entry = XATTR_NEXT_ENTRY(entry))
130 
131 #define MIN_OFFSET	XATTR_ALIGN(PAGE_SIZE -			\
132 		sizeof(struct node_footer) - sizeof(__u32))
133 
134 #define MAX_VALUE_LEN	(MIN_OFFSET -				\
135 		sizeof(struct f2fs_xattr_header) -		\
136 		sizeof(struct f2fs_xattr_entry))
137 
138 #define MAX_INLINE_XATTR_SIZE						\
139 			(DEF_ADDRS_PER_INODE -				\
140 			F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -	\
141 			DEF_INLINE_RESERVED_SIZE -			\
142 			MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
143 #endif
144