• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FS_KEY_DESCRIPTOR_SIZE
39 #define FS_KEY_DESCRIPTOR_SIZE 8
40 #endif
41 #define FS_KEY_DERIVATION_NONCE_SIZE 16
42 
43 struct fscrypt_context {
44 	u8 format;
45 	u8 contents_encryption_mode;
46 	u8 filenames_encryption_mode;
47 	u8 flags;
48 	u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
49 	u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
50 };
51 
52 static_assert(sizeof(struct fscrypt_context) == 28, "");
53 
54 #define F2FS_ACL_VERSION	0x0001
55 
56 struct f2fs_acl_entry {
57 	__le16 e_tag;
58 	__le16 e_perm;
59 	__le32 e_id;
60 };
61 
62 struct f2fs_acl_entry_short {
63 	__le16 e_tag;
64 	__le16 e_perm;
65 };
66 
67 struct f2fs_acl_header {
68 	__le32 a_version;
69 };
70 
f2fs_acl_count(int size)71 static inline int f2fs_acl_count(int size)
72 {
73 	ssize_t s;
74 	size -= sizeof(struct f2fs_acl_header);
75 	s = size - 4 * sizeof(struct f2fs_acl_entry_short);
76 	if (s < 0) {
77 		if (size % sizeof(struct f2fs_acl_entry_short))
78 			return -1;
79 		return size / sizeof(struct f2fs_acl_entry_short);
80 	} else {
81 		if (s % sizeof(struct f2fs_acl_entry))
82 			return -1;
83 		return s / sizeof(struct f2fs_acl_entry) + 4;
84 	}
85 }
86 
87 #ifndef XATTR_USER_PREFIX
88 #define XATTR_USER_PREFIX	"user."
89 #endif
90 #ifndef XATTR_SECURITY_PREFIX
91 #define XATTR_SECURITY_PREFIX	"security."
92 #endif
93 #ifndef XATTR_TRUSTED_PREFIX
94 #define XATTR_TRUSTED_PREFIX	"trusted."
95 #endif
96 
97 #ifndef XATTR_CREATE
98 #define XATTR_CREATE 0x1
99 #endif
100 #ifndef XATTR_REPLACE
101 #define XATTR_REPLACE 0x2
102 #endif
103 
104 #define XATTR_ROUND	(3)
105 
106 #define XATTR_SELINUX_SUFFIX "selinux"
107 #define F2FS_XATTR_INDEX_USER			1
108 #define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS	2
109 #define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT	3
110 #define F2FS_XATTR_INDEX_TRUSTED		4
111 #define F2FS_XATTR_INDEX_LUSTRE			5
112 #define F2FS_XATTR_INDEX_SECURITY		6
113 #define F2FS_XATTR_INDEX_ENCRYPTION		9
114 
115 #define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
116 
117 #define XATTR_HDR(ptr)		((struct f2fs_xattr_header *)(ptr))
118 #define XATTR_ENTRY(ptr) 	((struct f2fs_xattr_entry *)(ptr))
119 #define F2FS_XATTR_MAGIC	0xF2F52011
120 
121 #define XATTR_NEXT_ENTRY(entry) ((struct f2fs_xattr_entry *) ((char *)(entry) +\
122 					ENTRY_SIZE(entry)))
123 #define XATTR_FIRST_ENTRY(ptr)	(XATTR_ENTRY(XATTR_HDR(ptr) + 1))
124 
125 #define XATTR_ALIGN(size)	((size + XATTR_ROUND) & ~XATTR_ROUND)
126 
127 #define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
128 			entry->e_name_len + le16_to_cpu(entry->e_value_size)))
129 
130 #define list_for_each_xattr(entry, addr) \
131 	for (entry = XATTR_FIRST_ENTRY(addr); \
132 			!IS_XATTR_LAST_ENTRY(entry); \
133 			entry = XATTR_NEXT_ENTRY(entry))
134 
135 #define MIN_OFFSET	XATTR_ALIGN(F2FS_BLKSIZE -		\
136 		sizeof(struct node_footer) - sizeof(__u32))
137 
138 #define MAX_VALUE_LEN	(MIN_OFFSET -				\
139 		sizeof(struct f2fs_xattr_header) -		\
140 		sizeof(struct f2fs_xattr_entry))
141 
142 #define MAX_INLINE_XATTR_SIZE						\
143 			(DEF_ADDRS_PER_INODE -				\
144 			F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -	\
145 			DEF_INLINE_RESERVED_SIZE -			\
146 			MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
147 #endif
148