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