1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 File: linux/xattr.h
4
5 Extended attributes handling.
6
7 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8 Copyright (c) 2001-2002 Silicon Graphics, Inc. All Rights Reserved.
9 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
10 */
11 #ifndef _LINUX_XATTR_H
12 #define _LINUX_XATTR_H
13
14
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/mm.h>
19 #include <linux/rhashtable-types.h>
20 #include <linux/user_namespace.h>
21 #include <uapi/linux/xattr.h>
22
23 /* List of all open_how "versions". */
24 #define XATTR_ARGS_SIZE_VER0 16 /* sizeof first published struct */
25 #define XATTR_ARGS_SIZE_LATEST XATTR_ARGS_SIZE_VER0
26
27 struct inode;
28 struct dentry;
29
is_posix_acl_xattr(const char * name)30 static inline bool is_posix_acl_xattr(const char *name)
31 {
32 return (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
33 (strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0);
34 }
35
36 /*
37 * struct xattr_handler: When @name is set, match attributes with exactly that
38 * name. When @prefix is set instead, match attributes with that prefix and
39 * with a non-empty suffix.
40 */
41 struct xattr_handler {
42 const char *name;
43 const char *prefix;
44 int flags; /* fs private flags */
45 bool (*list)(struct dentry *dentry);
46 int (*get)(const struct xattr_handler *, struct dentry *dentry,
47 struct inode *inode, const char *name, void *buffer,
48 size_t size);
49 int (*set)(const struct xattr_handler *,
50 struct mnt_idmap *idmap, struct dentry *dentry,
51 struct inode *inode, const char *name, const void *buffer,
52 size_t size, int flags);
53 };
54
55 /**
56 * xattr_handler_can_list - check whether xattr can be listed
57 * @handler: handler for this type of xattr
58 * @dentry: dentry whose inode xattr to list
59 *
60 * Determine whether the xattr associated with @dentry can be listed given
61 * @handler.
62 *
63 * Return: true if xattr can be listed, false if not.
64 */
xattr_handler_can_list(const struct xattr_handler * handler,struct dentry * dentry)65 static inline bool xattr_handler_can_list(const struct xattr_handler *handler,
66 struct dentry *dentry)
67 {
68 return handler && (!handler->list || handler->list(dentry));
69 }
70
71 const char *xattr_full_name(const struct xattr_handler *, const char *);
72
73 struct xattr {
74 const char *name;
75 void *value;
76 size_t value_len;
77 };
78
79 ssize_t __vfs_getxattr(struct dentry *, struct inode *, const char *, void *, size_t);
80 ssize_t vfs_getxattr(struct mnt_idmap *, struct dentry *, const char *,
81 void *, size_t);
82 ssize_t vfs_listxattr(struct dentry *d, char *list, size_t size);
83 int __vfs_setxattr(struct mnt_idmap *, struct dentry *, struct inode *,
84 const char *, const void *, size_t, int);
85 int __vfs_setxattr_noperm(struct mnt_idmap *, struct dentry *,
86 const char *, const void *, size_t, int);
87 int __vfs_setxattr_locked(struct mnt_idmap *, struct dentry *,
88 const char *, const void *, size_t, int,
89 struct delegated_inode *);
90 int vfs_setxattr(struct mnt_idmap *, struct dentry *, const char *,
91 const void *, size_t, int);
92 int __vfs_removexattr(struct mnt_idmap *, struct dentry *, const char *);
93 int __vfs_removexattr_locked(struct mnt_idmap *, struct dentry *,
94 const char *, struct delegated_inode *);
95 int vfs_removexattr(struct mnt_idmap *, struct dentry *, const char *);
96
97 ssize_t generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size);
98 int vfs_getxattr_alloc(struct mnt_idmap *idmap,
99 struct dentry *dentry, const char *name,
100 char **xattr_value, size_t size, gfp_t flags);
101
102 int xattr_supports_user_prefix(struct inode *inode);
103
xattr_prefix(const struct xattr_handler * handler)104 static inline const char *xattr_prefix(const struct xattr_handler *handler)
105 {
106 return handler->prefix ?: handler->name;
107 }
108
109 struct simple_xattrs {
110 struct rhashtable ht;
111 };
112
113 struct simple_xattr {
114 struct rhash_head hash_node;
115 struct rcu_head rcu;
116 char *name;
117 size_t size;
118 char value[] __counted_by(size);
119 };
120
121 #define SIMPLE_XATTR_MAX_NR 128
122 #define SIMPLE_XATTR_MAX_SIZE (128 << 10)
123
124 struct simple_xattr_limits {
125 atomic_t nr_xattrs; /* current user.* xattr count */
126 atomic_t xattr_size; /* current total user.* value bytes */
127 };
128
simple_xattr_limits_init(struct simple_xattr_limits * limits)129 static inline void simple_xattr_limits_init(struct simple_xattr_limits *limits)
130 {
131 atomic_set(&limits->nr_xattrs, 0);
132 atomic_set(&limits->xattr_size, 0);
133 }
134
135 int simple_xattrs_init(struct simple_xattrs *xattrs);
136 struct simple_xattrs *simple_xattrs_alloc(void);
137 struct simple_xattrs *simple_xattrs_lazy_alloc(struct simple_xattrs **xattrsp,
138 const void *value, int flags);
139 void simple_xattrs_free(struct simple_xattrs *xattrs, size_t *freed_space);
140 size_t simple_xattr_space(const char *name, size_t size);
141 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size);
142 void simple_xattr_free(struct simple_xattr *xattr);
143 void simple_xattr_free_rcu(struct simple_xattr *xattr);
144 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
145 void *buffer, size_t size);
146 struct simple_xattr *simple_xattr_set(struct simple_xattrs *xattrs,
147 const char *name, const void *value,
148 size_t size, int flags);
149 int simple_xattr_set_limited(struct simple_xattrs *xattrs,
150 struct simple_xattr_limits *limits,
151 const char *name, const void *value,
152 size_t size, int flags);
153 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
154 char *buffer, size_t size);
155 int simple_xattr_add(struct simple_xattrs *xattrs,
156 struct simple_xattr *new_xattr);
157 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name);
158
159 DEFINE_CLASS(simple_xattr,
160 struct simple_xattr *,
161 if (!IS_ERR_OR_NULL(_T)) simple_xattr_free(_T),
162 simple_xattr_alloc(value, size),
163 const void *value, size_t size)
164
165 DEFINE_CLASS(simple_xattrs,
166 struct simple_xattrs *,
167 if (!IS_ERR_OR_NULL(_T)) { simple_xattrs_free(_T, NULL); kfree(_T); },
168 simple_xattrs_alloc(),
169 void)
170
171 #endif /* _LINUX_XATTR_H */
172