Loading...
1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
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.
8 *
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.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/fs.h>
17#include <linux/sched.h>
18#include <net/9p/9p.h>
19#include <net/9p/client.h>
20
21#include "fid.h"
22#include "xattr.h"
23
24ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
25 void *buffer, size_t buffer_size)
26{
27 ssize_t retval;
28 int msize, read_count;
29 u64 offset = 0, attr_size;
30 struct p9_fid *attr_fid;
31
32 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
33 if (IS_ERR(attr_fid)) {
34 retval = PTR_ERR(attr_fid);
35 P9_DPRINTK(P9_DEBUG_VFS,
36 "p9_client_attrwalk failed %zd\n", retval);
37 attr_fid = NULL;
38 goto error;
39 }
40 if (!buffer_size) {
41 /* request to get the attr_size */
42 retval = attr_size;
43 goto error;
44 }
45 if (attr_size > buffer_size) {
46 retval = -ERANGE;
47 goto error;
48 }
49 msize = attr_fid->clnt->msize;
50 while (attr_size) {
51 if (attr_size > (msize - P9_IOHDRSZ))
52 read_count = msize - P9_IOHDRSZ;
53 else
54 read_count = attr_size;
55 read_count = p9_client_read(attr_fid, ((char *)buffer)+offset,
56 NULL, offset, read_count);
57 if (read_count < 0) {
58 /* error in xattr read */
59 retval = read_count;
60 goto error;
61 }
62 offset += read_count;
63 attr_size -= read_count;
64 }
65 /* Total read xattr bytes */
66 retval = offset;
67error:
68 if (attr_fid)
69 p9_client_clunk(attr_fid);
70 return retval;
71
72}
73
74
75/*
76 * v9fs_xattr_get()
77 *
78 * Copy an extended attribute into the buffer
79 * provided, or compute the buffer size required.
80 * Buffer is NULL to compute the size of the buffer required.
81 *
82 * Returns a negative error number on failure, or the number of bytes
83 * used / required on success.
84 */
85ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
86 void *buffer, size_t buffer_size)
87{
88 struct p9_fid *fid;
89
90 P9_DPRINTK(P9_DEBUG_VFS, "%s: name = %s value_len = %zu\n",
91 __func__, name, buffer_size);
92 fid = v9fs_fid_lookup(dentry);
93 if (IS_ERR(fid))
94 return PTR_ERR(fid);
95
96 return v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
97}
98
99/*
100 * v9fs_xattr_set()
101 *
102 * Create, replace or remove an extended attribute for this inode. Buffer
103 * is NULL to remove an existing extended attribute, and non-NULL to
104 * either replace an existing extended attribute, or create a new extended
105 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
106 * specify that an extended attribute must exist and must not exist
107 * previous to the call, respectively.
108 *
109 * Returns 0, or a negative error number on failure.
110 */
111int v9fs_xattr_set(struct dentry *dentry, const char *name,
112 const void *value, size_t value_len, int flags)
113{
114 u64 offset = 0;
115 int retval, msize, write_count;
116 struct p9_fid *fid = NULL;
117
118 P9_DPRINTK(P9_DEBUG_VFS, "%s: name = %s value_len = %zu flags = %d\n",
119 __func__, name, value_len, flags);
120
121 fid = v9fs_fid_clone(dentry);
122 if (IS_ERR(fid)) {
123 retval = PTR_ERR(fid);
124 fid = NULL;
125 goto error;
126 }
127 /*
128 * On success fid points to xattr
129 */
130 retval = p9_client_xattrcreate(fid, name, value_len, flags);
131 if (retval < 0) {
132 P9_DPRINTK(P9_DEBUG_VFS,
133 "p9_client_xattrcreate failed %d\n", retval);
134 goto error;
135 }
136 msize = fid->clnt->msize;
137 while (value_len) {
138 if (value_len > (msize - P9_IOHDRSZ))
139 write_count = msize - P9_IOHDRSZ;
140 else
141 write_count = value_len;
142 write_count = p9_client_write(fid, ((char *)value)+offset,
143 NULL, offset, write_count);
144 if (write_count < 0) {
145 /* error in xattr write */
146 retval = write_count;
147 goto error;
148 }
149 offset += write_count;
150 value_len -= write_count;
151 }
152 /* Total read xattr bytes */
153 retval = offset;
154error:
155 if (fid)
156 retval = p9_client_clunk(fid);
157 return retval;
158}
159
160ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
161{
162 return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
163}
164
165const struct xattr_handler *v9fs_xattr_handlers[] = {
166 &v9fs_xattr_user_handler,
167#ifdef CONFIG_9P_FS_POSIX_ACL
168 &v9fs_xattr_acl_access_handler,
169 &v9fs_xattr_acl_default_handler,
170#endif
171 NULL
172};
1// SPDX-License-Identifier: LGPL-2.1
2/*
3 * Copyright IBM Corporation, 2010
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 */
6
7#include <linux/module.h>
8#include <linux/fs.h>
9#include <linux/sched.h>
10#include <linux/uio.h>
11#include <linux/posix_acl_xattr.h>
12#include <net/9p/9p.h>
13#include <net/9p/client.h>
14
15#include "fid.h"
16#include "xattr.h"
17
18ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
19 void *buffer, size_t buffer_size)
20{
21 ssize_t retval;
22 u64 attr_size;
23 struct p9_fid *attr_fid;
24 struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
25 struct iov_iter to;
26 int err;
27
28 iov_iter_kvec(&to, ITER_DEST, &kvec, 1, buffer_size);
29
30 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
31 if (IS_ERR(attr_fid)) {
32 retval = PTR_ERR(attr_fid);
33 p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
34 retval);
35 return retval;
36 }
37 if (attr_size > buffer_size) {
38 if (!buffer_size) /* request to get the attr_size */
39 retval = attr_size;
40 else
41 retval = -ERANGE;
42 } else {
43 iov_iter_truncate(&to, attr_size);
44 retval = p9_client_read(attr_fid, 0, &to, &err);
45 if (err)
46 retval = err;
47 }
48 p9_fid_put(attr_fid);
49 return retval;
50}
51
52
53/*
54 * v9fs_xattr_get()
55 *
56 * Copy an extended attribute into the buffer
57 * provided, or compute the buffer size required.
58 * Buffer is NULL to compute the size of the buffer required.
59 *
60 * Returns a negative error number on failure, or the number of bytes
61 * used / required on success.
62 */
63ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
64 void *buffer, size_t buffer_size)
65{
66 struct p9_fid *fid;
67 int ret;
68
69 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
70 name, buffer_size);
71 fid = v9fs_fid_lookup(dentry);
72 if (IS_ERR(fid))
73 return PTR_ERR(fid);
74 ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
75 p9_fid_put(fid);
76
77 return ret;
78}
79
80/*
81 * v9fs_xattr_set()
82 *
83 * Create, replace or remove an extended attribute for this inode. Buffer
84 * is NULL to remove an existing extended attribute, and non-NULL to
85 * either replace an existing extended attribute, or create a new extended
86 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
87 * specify that an extended attribute must exist and must not exist
88 * previous to the call, respectively.
89 *
90 * Returns 0, or a negative error number on failure.
91 */
92int v9fs_xattr_set(struct dentry *dentry, const char *name,
93 const void *value, size_t value_len, int flags)
94{
95 int ret;
96 struct p9_fid *fid;
97
98 fid = v9fs_fid_lookup(dentry);
99 if (IS_ERR(fid))
100 return PTR_ERR(fid);
101 ret = v9fs_fid_xattr_set(fid, name, value, value_len, flags);
102 p9_fid_put(fid);
103 return ret;
104}
105
106int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
107 const void *value, size_t value_len, int flags)
108{
109 struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
110 struct iov_iter from;
111 int retval, err;
112
113 iov_iter_kvec(&from, ITER_SOURCE, &kvec, 1, value_len);
114
115 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
116 name, value_len, flags);
117
118 /* Clone it */
119 fid = clone_fid(fid);
120 if (IS_ERR(fid))
121 return PTR_ERR(fid);
122
123 /*
124 * On success fid points to xattr
125 */
126 retval = p9_client_xattrcreate(fid, name, value_len, flags);
127 if (retval < 0)
128 p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
129 retval);
130 else
131 p9_client_write(fid, 0, &from, &retval);
132 err = p9_fid_put(fid);
133 if (!retval && err)
134 retval = err;
135 return retval;
136}
137
138ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
139{
140 return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
141}
142
143static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
144 struct dentry *dentry, struct inode *inode,
145 const char *name, void *buffer, size_t size)
146{
147 const char *full_name = xattr_full_name(handler, name);
148
149 return v9fs_xattr_get(dentry, full_name, buffer, size);
150}
151
152static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
153 struct user_namespace *mnt_userns,
154 struct dentry *dentry, struct inode *inode,
155 const char *name, const void *value,
156 size_t size, int flags)
157{
158 const char *full_name = xattr_full_name(handler, name);
159
160 return v9fs_xattr_set(dentry, full_name, value, size, flags);
161}
162
163static struct xattr_handler v9fs_xattr_user_handler = {
164 .prefix = XATTR_USER_PREFIX,
165 .get = v9fs_xattr_handler_get,
166 .set = v9fs_xattr_handler_set,
167};
168
169static struct xattr_handler v9fs_xattr_trusted_handler = {
170 .prefix = XATTR_TRUSTED_PREFIX,
171 .get = v9fs_xattr_handler_get,
172 .set = v9fs_xattr_handler_set,
173};
174
175#ifdef CONFIG_9P_FS_SECURITY
176static struct xattr_handler v9fs_xattr_security_handler = {
177 .prefix = XATTR_SECURITY_PREFIX,
178 .get = v9fs_xattr_handler_get,
179 .set = v9fs_xattr_handler_set,
180};
181#endif
182
183const struct xattr_handler *v9fs_xattr_handlers[] = {
184 &v9fs_xattr_user_handler,
185 &v9fs_xattr_trusted_handler,
186#ifdef CONFIG_FS_POSIX_ACL
187 &posix_acl_access_xattr_handler,
188 &posix_acl_default_xattr_handler,
189#endif
190#ifdef CONFIG_9P_FS_SECURITY
191 &v9fs_xattr_security_handler,
192#endif
193 NULL
194};