ARM: 8051/1: put_user: fix possible data corruption in put_user
[linux/fpc-iii.git] / fs / 9p / xattr.c
blobf35168ce426b8cba2ba108c8b8247dd924d0ba24
1 /*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/sched.h>
18 #include <linux/uio.h>
19 #include <net/9p/9p.h>
20 #include <net/9p/client.h>
22 #include "fid.h"
23 #include "xattr.h"
25 ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
26 void *buffer, size_t buffer_size)
28 ssize_t retval;
29 u64 attr_size;
30 struct p9_fid *attr_fid;
31 struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
32 struct iov_iter to;
33 int err;
35 iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buffer_size);
37 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
38 if (IS_ERR(attr_fid)) {
39 retval = PTR_ERR(attr_fid);
40 p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
41 retval);
42 return retval;
44 if (attr_size > buffer_size) {
45 if (!buffer_size) /* request to get the attr_size */
46 retval = attr_size;
47 else
48 retval = -ERANGE;
49 } else {
50 iov_iter_truncate(&to, attr_size);
51 retval = p9_client_read(attr_fid, 0, &to, &err);
52 if (err)
53 retval = err;
55 p9_client_clunk(attr_fid);
56 return retval;
61 * v9fs_xattr_get()
63 * Copy an extended attribute into the buffer
64 * provided, or compute the buffer size required.
65 * Buffer is NULL to compute the size of the buffer required.
67 * Returns a negative error number on failure, or the number of bytes
68 * used / required on success.
70 ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
71 void *buffer, size_t buffer_size)
73 struct p9_fid *fid;
75 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
76 name, buffer_size);
77 fid = v9fs_fid_lookup(dentry);
78 if (IS_ERR(fid))
79 return PTR_ERR(fid);
81 return v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
85 * v9fs_xattr_set()
87 * Create, replace or remove an extended attribute for this inode. Buffer
88 * is NULL to remove an existing extended attribute, and non-NULL to
89 * either replace an existing extended attribute, or create a new extended
90 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
91 * specify that an extended attribute must exist and must not exist
92 * previous to the call, respectively.
94 * Returns 0, or a negative error number on failure.
96 int v9fs_xattr_set(struct dentry *dentry, const char *name,
97 const void *value, size_t value_len, int flags)
99 struct p9_fid *fid = v9fs_fid_lookup(dentry);
100 if (IS_ERR(fid))
101 return PTR_ERR(fid);
102 return v9fs_fid_xattr_set(fid, name, value, value_len, flags);
105 int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
106 const void *value, size_t value_len, int flags)
108 struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
109 struct iov_iter from;
110 int retval, err;
112 iov_iter_kvec(&from, WRITE | ITER_KVEC, &kvec, 1, value_len);
114 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
115 name, value_len, flags);
117 /* Clone it */
118 fid = p9_client_walk(fid, 0, NULL, 1);
119 if (IS_ERR(fid))
120 return PTR_ERR(fid);
123 * On success fid points to xattr
125 retval = p9_client_xattrcreate(fid, name, value_len, flags);
126 if (retval < 0)
127 p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
128 retval);
129 else
130 p9_client_write(fid, 0, &from, &retval);
131 err = p9_client_clunk(fid);
132 if (!retval && err)
133 retval = err;
134 return retval;
137 ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
139 return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
142 static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
143 struct dentry *dentry, const char *name,
144 void *buffer, size_t size)
146 const char *full_name = xattr_full_name(handler, name);
148 if (strcmp(name, "") == 0)
149 return -EINVAL;
150 return v9fs_xattr_get(dentry, full_name, buffer, size);
153 static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
154 struct dentry *dentry, const char *name,
155 const void *value, size_t size, int flags)
157 const char *full_name = xattr_full_name(handler, name);
159 if (strcmp(name, "") == 0)
160 return -EINVAL;
161 return v9fs_xattr_set(dentry, full_name, value, size, flags);
164 static struct xattr_handler v9fs_xattr_user_handler = {
165 .prefix = XATTR_USER_PREFIX,
166 .get = v9fs_xattr_handler_get,
167 .set = v9fs_xattr_handler_set,
170 static struct xattr_handler v9fs_xattr_trusted_handler = {
171 .prefix = XATTR_TRUSTED_PREFIX,
172 .get = v9fs_xattr_handler_get,
173 .set = v9fs_xattr_handler_set,
176 #ifdef CONFIG_9P_FS_SECURITY
177 static struct xattr_handler v9fs_xattr_security_handler = {
178 .prefix = XATTR_SECURITY_PREFIX,
179 .get = v9fs_xattr_handler_get,
180 .set = v9fs_xattr_handler_set,
182 #endif
184 const struct xattr_handler *v9fs_xattr_handlers[] = {
185 &v9fs_xattr_user_handler,
186 &v9fs_xattr_trusted_handler,
187 #ifdef CONFIG_9P_FS_POSIX_ACL
188 &v9fs_xattr_acl_access_handler,
189 &v9fs_xattr_acl_default_handler,
190 #endif
191 #ifdef CONFIG_9P_FS_SECURITY
192 &v9fs_xattr_security_handler,
193 #endif
194 NULL