Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
v4.6
 
  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 <linux/uio.h>
 
 19#include <net/9p/9p.h>
 20#include <net/9p/client.h>
 21
 22#include "fid.h"
 23#include "xattr.h"
 24
 25ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
 26			   void *buffer, size_t buffer_size)
 27{
 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;
 34
 35	iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buffer_size);
 36
 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;
 43	}
 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;
 54	}
 55	p9_client_clunk(attr_fid);
 56	return retval;
 57}
 58
 59
 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
 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);
 
 
 80
 81	return v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
 82}
 83
 84/*
 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{
 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);
 
 
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{
108	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);
113
114	p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
115		 name, value_len, flags);
116
117	/* Clone it */
118	fid = p9_client_walk(fid, 0, NULL, 1);
119	if (IS_ERR(fid))
120		return PTR_ERR(fid);
121
122	/*
123	 * On success fid points to xattr
124	 */
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	p9_client_clunk(fid);
 
 
132	return retval;
133}
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
140static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
141				  struct dentry *dentry, const char *name,
142				  void *buffer, size_t size)
143{
144	const char *full_name = xattr_full_name(handler, name);
145
146	return v9fs_xattr_get(dentry, full_name, buffer, size);
147}
148
149static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
150				  struct dentry *dentry, const char *name,
151				  const void *value, size_t size, int flags)
 
 
152{
153	const char *full_name = xattr_full_name(handler, name);
154
155	return v9fs_xattr_set(dentry, full_name, value, size, flags);
156}
157
158static struct xattr_handler v9fs_xattr_user_handler = {
159	.prefix	= XATTR_USER_PREFIX,
160	.get	= v9fs_xattr_handler_get,
161	.set	= v9fs_xattr_handler_set,
162};
163
164static struct xattr_handler v9fs_xattr_trusted_handler = {
165	.prefix	= XATTR_TRUSTED_PREFIX,
166	.get	= v9fs_xattr_handler_get,
167	.set	= v9fs_xattr_handler_set,
168};
169
170#ifdef CONFIG_9P_FS_SECURITY
171static struct xattr_handler v9fs_xattr_security_handler = {
172	.prefix	= XATTR_SECURITY_PREFIX,
173	.get	= v9fs_xattr_handler_get,
174	.set	= v9fs_xattr_handler_set,
175};
176#endif
177
178const struct xattr_handler *v9fs_xattr_handlers[] = {
179	&v9fs_xattr_user_handler,
180	&v9fs_xattr_trusted_handler,
181#ifdef CONFIG_9P_FS_POSIX_ACL
182	&v9fs_xattr_acl_access_handler,
183	&v9fs_xattr_acl_default_handler,
184#endif
185#ifdef CONFIG_9P_FS_SECURITY
186	&v9fs_xattr_security_handler,
187#endif
188	NULL
189};
v6.9.4
  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)
 
 
 39			retval = -ERANGE;
 40		else if (attr_size > SSIZE_MAX)
 41			retval = -EOVERFLOW;
 42		else /* request to get the attr_size */
 43			retval = attr_size;
 44	} else {
 45		iov_iter_truncate(&to, attr_size);
 46		retval = p9_client_read(attr_fid, 0, &to, &err);
 47		if (err)
 48			retval = err;
 49	}
 50	p9_fid_put(attr_fid);
 51	return retval;
 52}
 53
 54
 55/*
 56 * v9fs_xattr_get()
 57 *
 58 * Copy an extended attribute into the buffer
 59 * provided, or compute the buffer size required.
 60 * Buffer is NULL to compute the size of the buffer required.
 61 *
 62 * Returns a negative error number on failure, or the number of bytes
 63 * used / required on success.
 64 */
 65ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
 66		       void *buffer, size_t buffer_size)
 67{
 68	struct p9_fid *fid;
 69	int ret;
 70
 71	p9_debug(P9_DEBUG_VFS, "name = '%s' value_len = %zu\n",
 72		 name, buffer_size);
 73	fid = v9fs_fid_lookup(dentry);
 74	if (IS_ERR(fid))
 75		return PTR_ERR(fid);
 76	ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
 77	p9_fid_put(fid);
 78
 79	return ret;
 80}
 81
 82/*
 83 * v9fs_xattr_set()
 84 *
 85 * Create, replace or remove an extended attribute for this inode. Buffer
 86 * is NULL to remove an existing extended attribute, and non-NULL to
 87 * either replace an existing extended attribute, or create a new extended
 88 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
 89 * specify that an extended attribute must exist and must not exist
 90 * previous to the call, respectively.
 91 *
 92 * Returns 0, or a negative error number on failure.
 93 */
 94int v9fs_xattr_set(struct dentry *dentry, const char *name,
 95		   const void *value, size_t value_len, int flags)
 96{
 97	int ret;
 98	struct p9_fid *fid;
 99
100	fid  = v9fs_fid_lookup(dentry);
101	if (IS_ERR(fid))
102		return PTR_ERR(fid);
103	ret = v9fs_fid_xattr_set(fid, name, value, value_len, flags);
104	p9_fid_put(fid);
105	return ret;
106}
107
108int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
109		   const void *value, size_t value_len, int flags)
110{
111	struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
112	struct iov_iter from;
113	int retval, err;
114
115	iov_iter_kvec(&from, ITER_SOURCE, &kvec, 1, value_len);
116
117	p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
118		 name, value_len, flags);
119
120	/* Clone it */
121	fid = clone_fid(fid);
122	if (IS_ERR(fid))
123		return PTR_ERR(fid);
124
125	/*
126	 * On success fid points to xattr
127	 */
128	retval = p9_client_xattrcreate(fid, name, value_len, flags);
129	if (retval < 0)
130		p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
131			 retval);
132	else
133		p9_client_write(fid, 0, &from, &retval);
134	err = p9_fid_put(fid);
135	if (!retval && err)
136		retval = err;
137	return retval;
138}
139
140ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
141{
142	/* Txattrwalk with an empty string lists xattrs instead */
143	return v9fs_xattr_get(dentry, "", buffer, buffer_size);
144}
145
146static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
147				  struct dentry *dentry, struct inode *inode,
148				  const char *name, void *buffer, size_t size)
149{
150	const char *full_name = xattr_full_name(handler, name);
151
152	return v9fs_xattr_get(dentry, full_name, buffer, size);
153}
154
155static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
156				  struct mnt_idmap *idmap,
157				  struct dentry *dentry, struct inode *inode,
158				  const char *name, const void *value,
159				  size_t size, int flags)
160{
161	const char *full_name = xattr_full_name(handler, name);
162
163	return v9fs_xattr_set(dentry, full_name, value, size, flags);
164}
165
166static const struct xattr_handler v9fs_xattr_user_handler = {
167	.prefix	= XATTR_USER_PREFIX,
168	.get	= v9fs_xattr_handler_get,
169	.set	= v9fs_xattr_handler_set,
170};
171
172static const struct xattr_handler v9fs_xattr_trusted_handler = {
173	.prefix	= XATTR_TRUSTED_PREFIX,
174	.get	= v9fs_xattr_handler_get,
175	.set	= v9fs_xattr_handler_set,
176};
177
178#ifdef CONFIG_9P_FS_SECURITY
179static const struct xattr_handler v9fs_xattr_security_handler = {
180	.prefix	= XATTR_SECURITY_PREFIX,
181	.get	= v9fs_xattr_handler_get,
182	.set	= v9fs_xattr_handler_set,
183};
184#endif
185
186const struct xattr_handler * const v9fs_xattr_handlers[] = {
187	&v9fs_xattr_user_handler,
188	&v9fs_xattr_trusted_handler,
 
 
 
 
189#ifdef CONFIG_9P_FS_SECURITY
190	&v9fs_xattr_security_handler,
191#endif
192	NULL
193};