Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 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 | |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 24 | ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name, |
| 25 | void *buffer, size_t buffer_size) |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 26 | { |
| 27 | ssize_t retval; |
| 28 | int msize, read_count; |
| 29 | u64 offset = 0, attr_size; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 30 | struct p9_fid *attr_fid; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 31 | |
| 32 | attr_fid = p9_client_xattrwalk(fid, name, &attr_size); |
| 33 | if (IS_ERR(attr_fid)) { |
| 34 | retval = PTR_ERR(attr_fid); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 35 | p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n", |
| 36 | retval); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 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, |
Eric Van Hensbergen | 327aec0 | 2010-08-02 11:36:18 -0500 | [diff] [blame] | 56 | NULL, offset, read_count); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 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; |
| 67 | error: |
| 68 | if (attr_fid) |
| 69 | p9_client_clunk(attr_fid); |
| 70 | return retval; |
| 71 | |
| 72 | } |
| 73 | |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 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 | */ |
| 85 | ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name, |
| 86 | void *buffer, size_t buffer_size) |
| 87 | { |
| 88 | struct p9_fid *fid; |
| 89 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 90 | p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n", |
| 91 | name, buffer_size); |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 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 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 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 | */ |
| 111 | int v9fs_xattr_set(struct dentry *dentry, const char *name, |
| 112 | const void *value, size_t value_len, int flags) |
| 113 | { |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 114 | struct p9_fid *fid = v9fs_fid_lookup(dentry); |
| 115 | if (IS_ERR(fid)) |
| 116 | return PTR_ERR(fid); |
| 117 | return v9fs_fid_xattr_set(fid, name, value, value_len, flags); |
| 118 | } |
| 119 | |
| 120 | int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name, |
| 121 | const void *value, size_t value_len, int flags) |
| 122 | { |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 123 | u64 offset = 0; |
| 124 | int retval, msize, write_count; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 125 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 126 | p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n", |
| 127 | name, value_len, flags); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 128 | |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 129 | /* Clone it */ |
| 130 | fid = p9_client_walk(fid, 0, NULL, 1); |
| 131 | if (IS_ERR(fid)) |
| 132 | return PTR_ERR(fid); |
| 133 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 134 | /* |
| 135 | * On success fid points to xattr |
| 136 | */ |
| 137 | retval = p9_client_xattrcreate(fid, name, value_len, flags); |
| 138 | if (retval < 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 139 | p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n", |
| 140 | retval); |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 141 | p9_client_clunk(fid); |
| 142 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 143 | } |
Joe Perches | 009ca38 | 2010-11-15 03:04:51 +0000 | [diff] [blame] | 144 | msize = fid->clnt->msize; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 145 | while (value_len) { |
| 146 | if (value_len > (msize - P9_IOHDRSZ)) |
| 147 | write_count = msize - P9_IOHDRSZ; |
| 148 | else |
| 149 | write_count = value_len; |
| 150 | write_count = p9_client_write(fid, ((char *)value)+offset, |
Eric Van Hensbergen | 327aec0 | 2010-08-02 11:36:18 -0500 | [diff] [blame] | 151 | NULL, offset, write_count); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 152 | if (write_count < 0) { |
| 153 | /* error in xattr write */ |
| 154 | retval = write_count; |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 155 | break; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 156 | } |
| 157 | offset += write_count; |
| 158 | value_len -= write_count; |
| 159 | } |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 160 | return p9_client_clunk(fid); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 164 | { |
| 165 | return v9fs_xattr_get(dentry, NULL, buffer, buffer_size); |
| 166 | } |
| 167 | |
| 168 | const struct xattr_handler *v9fs_xattr_handlers[] = { |
| 169 | &v9fs_xattr_user_handler, |
Jim Garlick | d9a7385 | 2013-05-29 12:09:39 -0700 | [diff] [blame] | 170 | &v9fs_xattr_trusted_handler, |
Aneesh Kumar K.V | 7a4566b | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 171 | #ifdef CONFIG_9P_FS_POSIX_ACL |
| 172 | &v9fs_xattr_acl_access_handler, |
| 173 | &v9fs_xattr_acl_default_handler, |
| 174 | #endif |
Jim Garlick | d9a7385 | 2013-05-29 12:09:39 -0700 | [diff] [blame] | 175 | #ifdef CONFIG_9P_FS_SECURITY |
| 176 | &v9fs_xattr_security_handler, |
| 177 | #endif |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 178 | NULL |
| 179 | }; |