40bb850711bebf194f48b609d9f3a0d08e34f903
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / fs / sdfat / xattr.c
1 /*
2 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /************************************************************************/
19 /* */
20 /* PROJECT : exFAT & FAT12/16/32 File System */
21 /* FILE : xattr.c */
22 /* PURPOSE : sdFAT code for supporting xattr(Extended File Attributes) */
23 /* */
24 /*----------------------------------------------------------------------*/
25 /* NOTES */
26 /* */
27 /* */
28 /************************************************************************/
29
30 #include <linux/file.h>
31 #include <linux/fs.h>
32 #include <linux/xattr.h>
33 #include <linux/dcache.h>
34 #include "sdfat.h"
35
36 #ifndef CONFIG_SDFAT_VIRTUAL_XATTR_SELINUX_LABEL
37 #define CONFIG_SDFAT_VIRTUAL_XATTR_SELINUX_LABEL ("undefined")
38 #endif
39
40 static const char default_xattr[] = CONFIG_SDFAT_VIRTUAL_XATTR_SELINUX_LABEL;
41
42 static int can_support(const char *name)
43 {
44 if (!name || strcmp(name, "security.selinux"))
45 return -1;
46 return 0;
47 }
48
49 ssize_t sdfat_listxattr(struct dentry *dentry, char *list, size_t size)
50 {
51 return 0;
52 }
53
54
55 /*************************************************************************
56 * INNER FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
57 *************************************************************************/
58 static int __sdfat_xattr_check_support(const char *name)
59 {
60 if (can_support(name))
61 return -EOPNOTSUPP;
62
63 return 0;
64 }
65
66 ssize_t __sdfat_getxattr(const char *name, void *value, size_t size)
67 {
68 if (can_support(name))
69 return -EOPNOTSUPP;
70
71 if ((size > strlen(default_xattr)+1) && value)
72 strcpy(value, default_xattr);
73
74 return strlen(default_xattr);
75 }
76
77
78 /*************************************************************************
79 * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
80 *************************************************************************/
81 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
82 static int sdfat_xattr_get(const struct xattr_handler *handler,
83 struct dentry *dentry, struct inode *inode,
84 const char *name, void *buffer, size_t size)
85 {
86 return __sdfat_getxattr(name, buffer, size);
87 }
88
89 static int sdfat_xattr_set(const struct xattr_handler *handler,
90 struct dentry *dentry, struct inode *inode,
91 const char *name, const void *value, size_t size,
92 int flags)
93 {
94 return __sdfat_xattr_check_support(name);
95 }
96
97 const struct xattr_handler sdfat_xattr_handler = {
98 .prefix = "", /* match anything */
99 .get = sdfat_xattr_get,
100 .set = sdfat_xattr_set,
101 };
102
103 const struct xattr_handler *sdfat_xattr_handlers[] = {
104 &sdfat_xattr_handler,
105 NULL
106 };
107
108 void setup_sdfat_xattr_handler(struct super_block *sb)
109 {
110 sb->s_xattr = sdfat_xattr_handlers;
111 }
112 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) */
113 int sdfat_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
114 {
115 return __sdfat_xattr_check_support(name);
116 }
117
118 ssize_t sdfat_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
119 {
120 return __sdfat_getxattr(name, value, size);
121 }
122
123 int sdfat_removexattr(struct dentry *dentry, const char *name)
124 {
125 return __sdfat_xattr_check_support(name);
126 }
127
128 void setup_sdfat_xattr_handler(struct super_block *sb)
129 {
130 /* DO NOTHING */
131 }
132 #endif