blob: a6bd349bab23ef02b7b46ccbba320c7191305879 [file] [log] [blame]
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +05301/*
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>
Al Viro070b3652015-04-01 20:17:51 -040018#include <linux/uio.h>
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053019#include <net/9p/9p.h>
20#include <net/9p/client.h>
21
22#include "fid.h"
23#include "xattr.h"
24
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053025ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
26 void *buffer, size_t buffer_size)
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053027{
28 ssize_t retval;
Al Viroe1200fe62015-04-01 23:42:28 -040029 u64 attr_size;
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053030 struct p9_fid *attr_fid;
Al Viroe1200fe62015-04-01 23:42:28 -040031 struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
32 struct iov_iter to;
33 int err;
34
35 iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buffer_size);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053036
37 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
38 if (IS_ERR(attr_fid)) {
39 retval = PTR_ERR(attr_fid);
Joe Perches5d385152011-11-28 10:40:46 -080040 p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
41 retval);
Al Viroe1200fe62015-04-01 23:42:28 -040042 return retval;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053043 }
44 if (attr_size > buffer_size) {
Al Viroe1200fe62015-04-01 23:42:28 -040045 if (!buffer_size) /* request to get the attr_size */
46 retval = attr_size;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053047 else
Al Viroe1200fe62015-04-01 23:42:28 -040048 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;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053054 }
Al Viroe1200fe62015-04-01 23:42:28 -040055 p9_client_clunk(attr_fid);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053056 return retval;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053057}
58
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053059
60/*
61 * v9fs_xattr_get()
62 *
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.
66 *
67 * Returns a negative error number on failure, or the number of bytes
68 * used / required on success.
69 */
70ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
71 void *buffer, size_t buffer_size)
72{
73 struct p9_fid *fid;
74
Joe Perches5d385152011-11-28 10:40:46 -080075 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
76 name, buffer_size);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053077 fid = v9fs_fid_lookup(dentry);
78 if (IS_ERR(fid))
79 return PTR_ERR(fid);
80
81 return v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
82}
83
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053084/*
85 * v9fs_xattr_set()
86 *
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.
93 *
94 * Returns 0, or a negative error number on failure.
95 */
96int v9fs_xattr_set(struct dentry *dentry, const char *name,
97 const void *value, size_t value_len, int flags)
98{
Al Viro38baba92013-01-31 12:34:58 -050099 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);
103}
104
105int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
106 const void *value, size_t value_len, int flags)
107{
Al Viro070b3652015-04-01 20:17:51 -0400108 struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
109 struct iov_iter from;
110 int retval;
111
112 iov_iter_kvec(&from, WRITE | ITER_KVEC, &kvec, 1, value_len);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530113
Joe Perches5d385152011-11-28 10:40:46 -0800114 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
115 name, value_len, flags);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530116
Al Viro38baba92013-01-31 12:34:58 -0500117 /* Clone it */
118 fid = p9_client_walk(fid, 0, NULL, 1);
119 if (IS_ERR(fid))
120 return PTR_ERR(fid);
121
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530122 /*
123 * On success fid points to xattr
124 */
125 retval = p9_client_xattrcreate(fid, name, value_len, flags);
Al Viro070b3652015-04-01 20:17:51 -0400126 if (retval < 0)
Joe Perches5d385152011-11-28 10:40:46 -0800127 p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
128 retval);
Al Viro070b3652015-04-01 20:17:51 -0400129 else
130 p9_client_write(fid, 0, &from, &retval);
Geyslan G. Bembdd5c282013-10-21 16:47:58 -0300131 p9_client_clunk(fid);
132 return retval;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530133}
134
135ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
136{
137 return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
138}
139
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200140static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -0400141 struct dentry *dentry, struct inode *inode,
142 const char *name, void *buffer, size_t size)
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200143{
144 const char *full_name = xattr_full_name(handler, name);
145
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200146 return v9fs_xattr_get(dentry, full_name, buffer, size);
147}
148
149static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -0400150 struct dentry *dentry, struct inode *inode,
151 const char *name, const void *value,
152 size_t size, int flags)
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200153{
154 const char *full_name = xattr_full_name(handler, name);
155
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200156 return v9fs_xattr_set(dentry, full_name, value, size, flags);
157}
158
159static struct xattr_handler v9fs_xattr_user_handler = {
160 .prefix = XATTR_USER_PREFIX,
161 .get = v9fs_xattr_handler_get,
162 .set = v9fs_xattr_handler_set,
163};
164
165static struct xattr_handler v9fs_xattr_trusted_handler = {
166 .prefix = XATTR_TRUSTED_PREFIX,
167 .get = v9fs_xattr_handler_get,
168 .set = v9fs_xattr_handler_set,
169};
170
171#ifdef CONFIG_9P_FS_SECURITY
172static struct xattr_handler v9fs_xattr_security_handler = {
173 .prefix = XATTR_SECURITY_PREFIX,
174 .get = v9fs_xattr_handler_get,
175 .set = v9fs_xattr_handler_set,
176};
177#endif
178
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530179const struct xattr_handler *v9fs_xattr_handlers[] = {
180 &v9fs_xattr_user_handler,
Jim Garlickd9a73852013-05-29 12:09:39 -0700181 &v9fs_xattr_trusted_handler,
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530182#ifdef CONFIG_9P_FS_POSIX_ACL
183 &v9fs_xattr_acl_access_handler,
184 &v9fs_xattr_acl_default_handler,
185#endif
Jim Garlickd9a73852013-05-29 12:09:39 -0700186#ifdef CONFIG_9P_FS_SECURITY
187 &v9fs_xattr_security_handler,
188#endif
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530189 NULL
190};