Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * net/9p/protocol.c |
| 3 | * |
| 4 | * 9P Protocol Support Code |
| 5 | * |
| 6 | * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com> |
| 7 | * |
| 8 | * Base on code from Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * Copyright (C) 2008 by IBM, Corp. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 |
| 13 | * as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to: |
| 22 | * Free Software Foundation |
| 23 | * 51 Franklin Street, Fifth Floor |
| 24 | * Boston, MA 02111-1301 USA |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/errno.h> |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 30 | #include <linux/kernel.h> |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 33 | #include <linux/sched.h> |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 34 | #include <linux/stddef.h> |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 35 | #include <linux/types.h> |
Al Viro | 4f3b35c | 2015-04-01 19:57:53 -0400 | [diff] [blame] | 36 | #include <linux/uio.h> |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 37 | #include <net/9p/9p.h> |
| 38 | #include <net/9p/client.h> |
| 39 | #include "protocol.h" |
| 40 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 41 | #include <trace/events/9p.h> |
| 42 | |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 43 | static int |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 44 | p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 45 | |
| 46 | void p9stat_free(struct p9_wstat *stbuf) |
| 47 | { |
| 48 | kfree(stbuf->name); |
Dominique Martinet | 62e3941 | 2018-08-28 07:32:35 +0900 | [diff] [blame] | 49 | stbuf->name = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 50 | kfree(stbuf->uid); |
Dominique Martinet | 62e3941 | 2018-08-28 07:32:35 +0900 | [diff] [blame] | 51 | stbuf->uid = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 52 | kfree(stbuf->gid); |
Dominique Martinet | 62e3941 | 2018-08-28 07:32:35 +0900 | [diff] [blame] | 53 | stbuf->gid = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 54 | kfree(stbuf->muid); |
Dominique Martinet | 62e3941 | 2018-08-28 07:32:35 +0900 | [diff] [blame] | 55 | stbuf->muid = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 56 | kfree(stbuf->extension); |
Dominique Martinet | 62e3941 | 2018-08-28 07:32:35 +0900 | [diff] [blame] | 57 | stbuf->extension = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 58 | } |
| 59 | EXPORT_SYMBOL(p9stat_free); |
| 60 | |
Aneesh Kumar K.V | abfa034 | 2011-08-16 10:50:10 +0530 | [diff] [blame] | 61 | size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 62 | { |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 63 | size_t len = min(pdu->size - pdu->offset, size); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 64 | memcpy(data, &pdu->sdata[pdu->offset], len); |
| 65 | pdu->offset += len; |
| 66 | return size - len; |
| 67 | } |
| 68 | |
| 69 | static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size) |
| 70 | { |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 71 | size_t len = min(pdu->capacity - pdu->size, size); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 72 | memcpy(&pdu->sdata[pdu->size], data, len); |
| 73 | pdu->size += len; |
| 74 | return size - len; |
| 75 | } |
| 76 | |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 77 | static size_t |
Al Viro | 4f3b35c | 2015-04-01 19:57:53 -0400 | [diff] [blame] | 78 | pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size) |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 79 | { |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 80 | size_t len = min(pdu->capacity - pdu->size, size); |
Al Viro | 4f3b35c | 2015-04-01 19:57:53 -0400 | [diff] [blame] | 81 | struct iov_iter i = *from; |
Al Viro | 1c512a7 | 2017-02-17 23:16:09 -0500 | [diff] [blame] | 82 | if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i)) |
Aneesh Kumar K.V | 7b3bb3f | 2010-10-19 09:17:02 +0530 | [diff] [blame] | 83 | len = 0; |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 84 | |
| 85 | pdu->size += len; |
| 86 | return size - len; |
| 87 | } |
| 88 | |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 89 | /* |
| 90 | b - int8_t |
| 91 | w - int16_t |
| 92 | d - int32_t |
| 93 | q - int64_t |
| 94 | s - string |
Eric W. Biederman | 97fc8b1 | 2013-01-29 17:07:42 -0800 | [diff] [blame] | 95 | u - numeric uid |
| 96 | g - numeric gid |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 97 | S - stat |
| 98 | Q - qid |
| 99 | D - data blob (int32_t size followed by void *, results are not freed) |
| 100 | T - array of strings (int16_t count, followed by strings) |
| 101 | R - array of qids (int16_t count, followed by qids) |
Sripathi Kodi | f085312 | 2010-07-12 20:07:23 +0530 | [diff] [blame] | 102 | A - stat for 9p2000.L (p9_stat_dotl) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 103 | ? - if optional = 1, continue parsing |
| 104 | */ |
| 105 | |
| 106 | static int |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 107 | p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, |
| 108 | va_list ap) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 109 | { |
| 110 | const char *ptr; |
| 111 | int errcode = 0; |
| 112 | |
| 113 | for (ptr = fmt; *ptr; ptr++) { |
| 114 | switch (*ptr) { |
| 115 | case 'b':{ |
| 116 | int8_t *val = va_arg(ap, int8_t *); |
| 117 | if (pdu_read(pdu, val, sizeof(*val))) { |
| 118 | errcode = -EFAULT; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | break; |
| 123 | case 'w':{ |
| 124 | int16_t *val = va_arg(ap, int16_t *); |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 125 | __le16 le_val; |
| 126 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 127 | errcode = -EFAULT; |
| 128 | break; |
| 129 | } |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 130 | *val = le16_to_cpu(le_val); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | case 'd':{ |
| 134 | int32_t *val = va_arg(ap, int32_t *); |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 135 | __le32 le_val; |
| 136 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 137 | errcode = -EFAULT; |
| 138 | break; |
| 139 | } |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 140 | *val = le32_to_cpu(le_val); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 141 | } |
| 142 | break; |
| 143 | case 'q':{ |
| 144 | int64_t *val = va_arg(ap, int64_t *); |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 145 | __le64 le_val; |
| 146 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 147 | errcode = -EFAULT; |
| 148 | break; |
| 149 | } |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 150 | *val = le64_to_cpu(le_val); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 151 | } |
| 152 | break; |
| 153 | case 's':{ |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 154 | char **sptr = va_arg(ap, char **); |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 155 | uint16_t len; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 156 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 157 | errcode = p9pdu_readf(pdu, proto_version, |
| 158 | "w", &len); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 159 | if (errcode) |
| 160 | break; |
| 161 | |
Aneesh Kumar K.V | eeff66e | 2011-03-08 16:39:47 +0530 | [diff] [blame] | 162 | *sptr = kmalloc(len + 1, GFP_NOFS); |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 163 | if (*sptr == NULL) { |
piaojun | b87d1d2 | 2018-07-11 08:43:49 +0800 | [diff] [blame] | 164 | errcode = -ENOMEM; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 165 | break; |
| 166 | } |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 167 | if (pdu_read(pdu, *sptr, len)) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 168 | errcode = -EFAULT; |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 169 | kfree(*sptr); |
| 170 | *sptr = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 171 | } else |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 172 | (*sptr)[len] = 0; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 173 | } |
| 174 | break; |
Eric W. Biederman | 97fc8b1 | 2013-01-29 17:07:42 -0800 | [diff] [blame] | 175 | case 'u': { |
| 176 | kuid_t *uid = va_arg(ap, kuid_t *); |
| 177 | __le32 le_val; |
| 178 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
| 179 | errcode = -EFAULT; |
| 180 | break; |
| 181 | } |
| 182 | *uid = make_kuid(&init_user_ns, |
| 183 | le32_to_cpu(le_val)); |
| 184 | } break; |
| 185 | case 'g': { |
| 186 | kgid_t *gid = va_arg(ap, kgid_t *); |
| 187 | __le32 le_val; |
| 188 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
| 189 | errcode = -EFAULT; |
| 190 | break; |
| 191 | } |
| 192 | *gid = make_kgid(&init_user_ns, |
| 193 | le32_to_cpu(le_val)); |
| 194 | } break; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 195 | case 'Q':{ |
| 196 | struct p9_qid *qid = |
| 197 | va_arg(ap, struct p9_qid *); |
| 198 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 199 | errcode = p9pdu_readf(pdu, proto_version, "bdq", |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 200 | &qid->type, &qid->version, |
| 201 | &qid->path); |
| 202 | } |
| 203 | break; |
| 204 | case 'S':{ |
| 205 | struct p9_wstat *stbuf = |
| 206 | va_arg(ap, struct p9_wstat *); |
| 207 | |
Eric Van Hensbergen | f0a0ac2 | 2008-10-17 12:45:23 -0500 | [diff] [blame] | 208 | memset(stbuf, 0, sizeof(struct p9_wstat)); |
Eric W. Biederman | 447c509 | 2013-01-29 16:18:50 -0800 | [diff] [blame] | 209 | stbuf->n_uid = stbuf->n_muid = INVALID_UID; |
| 210 | stbuf->n_gid = INVALID_GID; |
| 211 | |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 212 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 213 | p9pdu_readf(pdu, proto_version, |
Eric W. Biederman | 447c509 | 2013-01-29 16:18:50 -0800 | [diff] [blame] | 214 | "wwdQdddqssss?sugu", |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 215 | &stbuf->size, &stbuf->type, |
| 216 | &stbuf->dev, &stbuf->qid, |
| 217 | &stbuf->mode, &stbuf->atime, |
| 218 | &stbuf->mtime, &stbuf->length, |
| 219 | &stbuf->name, &stbuf->uid, |
| 220 | &stbuf->gid, &stbuf->muid, |
| 221 | &stbuf->extension, |
| 222 | &stbuf->n_uid, &stbuf->n_gid, |
| 223 | &stbuf->n_muid); |
| 224 | if (errcode) |
| 225 | p9stat_free(stbuf); |
| 226 | } |
| 227 | break; |
| 228 | case 'D':{ |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 229 | uint32_t *count = va_arg(ap, uint32_t *); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 230 | void **data = va_arg(ap, void **); |
| 231 | |
| 232 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 233 | p9pdu_readf(pdu, proto_version, "d", count); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 234 | if (!errcode) { |
| 235 | *count = |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 236 | min_t(uint32_t, *count, |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 237 | pdu->size - pdu->offset); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 238 | *data = &pdu->sdata[pdu->offset]; |
| 239 | } |
| 240 | } |
| 241 | break; |
| 242 | case 'T':{ |
Harsh Prateek Bora | b76225e | 2011-03-31 15:49:39 +0530 | [diff] [blame] | 243 | uint16_t *nwname = va_arg(ap, uint16_t *); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 244 | char ***wnames = va_arg(ap, char ***); |
| 245 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 246 | errcode = p9pdu_readf(pdu, proto_version, |
| 247 | "w", nwname); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 248 | if (!errcode) { |
| 249 | *wnames = |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 250 | kmalloc_array(*nwname, |
| 251 | sizeof(char *), |
| 252 | GFP_NOFS); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 253 | if (!*wnames) |
| 254 | errcode = -ENOMEM; |
| 255 | } |
| 256 | |
| 257 | if (!errcode) { |
| 258 | int i; |
| 259 | |
| 260 | for (i = 0; i < *nwname; i++) { |
| 261 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 262 | p9pdu_readf(pdu, |
| 263 | proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 264 | "s", |
| 265 | &(*wnames)[i]); |
| 266 | if (errcode) |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (errcode) { |
| 272 | if (*wnames) { |
| 273 | int i; |
| 274 | |
| 275 | for (i = 0; i < *nwname; i++) |
| 276 | kfree((*wnames)[i]); |
| 277 | } |
| 278 | kfree(*wnames); |
| 279 | *wnames = NULL; |
| 280 | } |
| 281 | } |
| 282 | break; |
| 283 | case 'R':{ |
Kirill A. Shutemov | 6250a8b | 2014-12-30 02:48:09 +0200 | [diff] [blame] | 284 | uint16_t *nwqid = va_arg(ap, uint16_t *); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 285 | struct p9_qid **wqids = |
| 286 | va_arg(ap, struct p9_qid **); |
| 287 | |
| 288 | *wqids = NULL; |
| 289 | |
| 290 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 291 | p9pdu_readf(pdu, proto_version, "w", nwqid); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 292 | if (!errcode) { |
| 293 | *wqids = |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 294 | kmalloc_array(*nwqid, |
| 295 | sizeof(struct p9_qid), |
| 296 | GFP_NOFS); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 297 | if (*wqids == NULL) |
| 298 | errcode = -ENOMEM; |
| 299 | } |
| 300 | |
| 301 | if (!errcode) { |
| 302 | int i; |
| 303 | |
| 304 | for (i = 0; i < *nwqid; i++) { |
| 305 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 306 | p9pdu_readf(pdu, |
| 307 | proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 308 | "Q", |
| 309 | &(*wqids)[i]); |
| 310 | if (errcode) |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (errcode) { |
| 316 | kfree(*wqids); |
| 317 | *wqids = NULL; |
| 318 | } |
| 319 | } |
| 320 | break; |
Sripathi Kodi | f085312 | 2010-07-12 20:07:23 +0530 | [diff] [blame] | 321 | case 'A': { |
| 322 | struct p9_stat_dotl *stbuf = |
| 323 | va_arg(ap, struct p9_stat_dotl *); |
| 324 | |
| 325 | memset(stbuf, 0, sizeof(struct p9_stat_dotl)); |
| 326 | errcode = |
| 327 | p9pdu_readf(pdu, proto_version, |
Eric W. Biederman | 447c509 | 2013-01-29 16:18:50 -0800 | [diff] [blame] | 328 | "qQdugqqqqqqqqqqqqqqq", |
Sripathi Kodi | f085312 | 2010-07-12 20:07:23 +0530 | [diff] [blame] | 329 | &stbuf->st_result_mask, |
| 330 | &stbuf->qid, |
| 331 | &stbuf->st_mode, |
| 332 | &stbuf->st_uid, &stbuf->st_gid, |
| 333 | &stbuf->st_nlink, |
| 334 | &stbuf->st_rdev, &stbuf->st_size, |
| 335 | &stbuf->st_blksize, &stbuf->st_blocks, |
| 336 | &stbuf->st_atime_sec, |
| 337 | &stbuf->st_atime_nsec, |
| 338 | &stbuf->st_mtime_sec, |
| 339 | &stbuf->st_mtime_nsec, |
| 340 | &stbuf->st_ctime_sec, |
| 341 | &stbuf->st_ctime_nsec, |
| 342 | &stbuf->st_btime_sec, |
| 343 | &stbuf->st_btime_nsec, |
| 344 | &stbuf->st_gen, |
| 345 | &stbuf->st_data_version); |
| 346 | } |
| 347 | break; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 348 | case '?': |
Sripathi Kodi | c56e4ac | 2010-03-25 12:40:35 +0000 | [diff] [blame] | 349 | if ((proto_version != p9_proto_2000u) && |
| 350 | (proto_version != p9_proto_2000L)) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 351 | return 0; |
| 352 | break; |
| 353 | default: |
| 354 | BUG(); |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (errcode) |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return errcode; |
| 363 | } |
| 364 | |
| 365 | int |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 366 | p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt, |
| 367 | va_list ap) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 368 | { |
| 369 | const char *ptr; |
| 370 | int errcode = 0; |
| 371 | |
| 372 | for (ptr = fmt; *ptr; ptr++) { |
| 373 | switch (*ptr) { |
| 374 | case 'b':{ |
| 375 | int8_t val = va_arg(ap, int); |
| 376 | if (pdu_write(pdu, &val, sizeof(val))) |
| 377 | errcode = -EFAULT; |
| 378 | } |
| 379 | break; |
| 380 | case 'w':{ |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 381 | __le16 val = cpu_to_le16(va_arg(ap, int)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 382 | if (pdu_write(pdu, &val, sizeof(val))) |
| 383 | errcode = -EFAULT; |
| 384 | } |
| 385 | break; |
| 386 | case 'd':{ |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 387 | __le32 val = cpu_to_le32(va_arg(ap, int32_t)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 388 | if (pdu_write(pdu, &val, sizeof(val))) |
| 389 | errcode = -EFAULT; |
| 390 | } |
| 391 | break; |
| 392 | case 'q':{ |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 393 | __le64 val = cpu_to_le64(va_arg(ap, int64_t)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 394 | if (pdu_write(pdu, &val, sizeof(val))) |
| 395 | errcode = -EFAULT; |
| 396 | } |
| 397 | break; |
| 398 | case 's':{ |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 399 | const char *sptr = va_arg(ap, const char *); |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 400 | uint16_t len = 0; |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 401 | if (sptr) |
Dan Carpenter | d31bb4f | 2012-06-26 23:01:41 +0000 | [diff] [blame] | 402 | len = min_t(size_t, strlen(sptr), |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 403 | USHRT_MAX); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 404 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 405 | errcode = p9pdu_writef(pdu, proto_version, |
| 406 | "w", len); |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 407 | if (!errcode && pdu_write(pdu, sptr, len)) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 408 | errcode = -EFAULT; |
| 409 | } |
| 410 | break; |
Eric W. Biederman | 97fc8b1 | 2013-01-29 17:07:42 -0800 | [diff] [blame] | 411 | case 'u': { |
| 412 | kuid_t uid = va_arg(ap, kuid_t); |
| 413 | __le32 val = cpu_to_le32( |
| 414 | from_kuid(&init_user_ns, uid)); |
| 415 | if (pdu_write(pdu, &val, sizeof(val))) |
| 416 | errcode = -EFAULT; |
| 417 | } break; |
| 418 | case 'g': { |
| 419 | kgid_t gid = va_arg(ap, kgid_t); |
| 420 | __le32 val = cpu_to_le32( |
| 421 | from_kgid(&init_user_ns, gid)); |
| 422 | if (pdu_write(pdu, &val, sizeof(val))) |
| 423 | errcode = -EFAULT; |
| 424 | } break; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 425 | case 'Q':{ |
| 426 | const struct p9_qid *qid = |
| 427 | va_arg(ap, const struct p9_qid *); |
| 428 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 429 | p9pdu_writef(pdu, proto_version, "bdq", |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 430 | qid->type, qid->version, |
| 431 | qid->path); |
| 432 | } break; |
| 433 | case 'S':{ |
| 434 | const struct p9_wstat *stbuf = |
| 435 | va_arg(ap, const struct p9_wstat *); |
| 436 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 437 | p9pdu_writef(pdu, proto_version, |
Eric W. Biederman | 447c509 | 2013-01-29 16:18:50 -0800 | [diff] [blame] | 438 | "wwdQdddqssss?sugu", |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 439 | stbuf->size, stbuf->type, |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 440 | stbuf->dev, &stbuf->qid, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 441 | stbuf->mode, stbuf->atime, |
| 442 | stbuf->mtime, stbuf->length, |
| 443 | stbuf->name, stbuf->uid, |
| 444 | stbuf->gid, stbuf->muid, |
| 445 | stbuf->extension, stbuf->n_uid, |
| 446 | stbuf->n_gid, stbuf->n_muid); |
| 447 | } break; |
Al Viro | 4f3b35c | 2015-04-01 19:57:53 -0400 | [diff] [blame] | 448 | case 'V':{ |
M. Mohan Kumar | 219fd58 | 2011-01-10 14:23:53 -0600 | [diff] [blame] | 449 | uint32_t count = va_arg(ap, uint32_t); |
Al Viro | 4f3b35c | 2015-04-01 19:57:53 -0400 | [diff] [blame] | 450 | struct iov_iter *from = |
| 451 | va_arg(ap, struct iov_iter *); |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 452 | errcode = p9pdu_writef(pdu, proto_version, "d", |
| 453 | count); |
Al Viro | 4f3b35c | 2015-04-01 19:57:53 -0400 | [diff] [blame] | 454 | if (!errcode && pdu_write_u(pdu, from, count)) |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 455 | errcode = -EFAULT; |
| 456 | } |
| 457 | break; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 458 | case 'T':{ |
Harsh Prateek Bora | b76225e | 2011-03-31 15:49:39 +0530 | [diff] [blame] | 459 | uint16_t nwname = va_arg(ap, int); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 460 | const char **wnames = va_arg(ap, const char **); |
| 461 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 462 | errcode = p9pdu_writef(pdu, proto_version, "w", |
| 463 | nwname); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 464 | if (!errcode) { |
| 465 | int i; |
| 466 | |
| 467 | for (i = 0; i < nwname; i++) { |
| 468 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 469 | p9pdu_writef(pdu, |
| 470 | proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 471 | "s", |
| 472 | wnames[i]); |
| 473 | if (errcode) |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | break; |
| 479 | case 'R':{ |
Kirill A. Shutemov | 6250a8b | 2014-12-30 02:48:09 +0200 | [diff] [blame] | 480 | uint16_t nwqid = va_arg(ap, int); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 481 | struct p9_qid *wqids = |
| 482 | va_arg(ap, struct p9_qid *); |
| 483 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 484 | errcode = p9pdu_writef(pdu, proto_version, "w", |
| 485 | nwqid); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 486 | if (!errcode) { |
| 487 | int i; |
| 488 | |
| 489 | for (i = 0; i < nwqid; i++) { |
| 490 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 491 | p9pdu_writef(pdu, |
| 492 | proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 493 | "Q", |
| 494 | &wqids[i]); |
| 495 | if (errcode) |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | break; |
Sripathi Kodi | 87d7845 | 2010-06-18 11:50:10 +0530 | [diff] [blame] | 501 | case 'I':{ |
| 502 | struct p9_iattr_dotl *p9attr = va_arg(ap, |
| 503 | struct p9_iattr_dotl *); |
| 504 | |
| 505 | errcode = p9pdu_writef(pdu, proto_version, |
Eric W. Biederman | 447c509 | 2013-01-29 16:18:50 -0800 | [diff] [blame] | 506 | "ddugqqqqq", |
Sripathi Kodi | 87d7845 | 2010-06-18 11:50:10 +0530 | [diff] [blame] | 507 | p9attr->valid, |
| 508 | p9attr->mode, |
| 509 | p9attr->uid, |
| 510 | p9attr->gid, |
| 511 | p9attr->size, |
| 512 | p9attr->atime_sec, |
| 513 | p9attr->atime_nsec, |
| 514 | p9attr->mtime_sec, |
| 515 | p9attr->mtime_nsec); |
| 516 | } |
| 517 | break; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 518 | case '?': |
Sripathi Kodi | c56e4ac | 2010-03-25 12:40:35 +0000 | [diff] [blame] | 519 | if ((proto_version != p9_proto_2000u) && |
| 520 | (proto_version != p9_proto_2000L)) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 521 | return 0; |
| 522 | break; |
| 523 | default: |
| 524 | BUG(); |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | if (errcode) |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | return errcode; |
| 533 | } |
| 534 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 535 | int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 536 | { |
| 537 | va_list ap; |
| 538 | int ret; |
| 539 | |
| 540 | va_start(ap, fmt); |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 541 | ret = p9pdu_vreadf(pdu, proto_version, fmt, ap); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 542 | va_end(ap); |
| 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static int |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 548 | p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 549 | { |
| 550 | va_list ap; |
| 551 | int ret; |
| 552 | |
| 553 | va_start(ap, fmt); |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 554 | ret = p9pdu_vwritef(pdu, proto_version, fmt, ap); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 555 | va_end(ap); |
| 556 | |
| 557 | return ret; |
| 558 | } |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 559 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 560 | int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st) |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 561 | { |
| 562 | struct p9_fcall fake_pdu; |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 563 | int ret; |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 564 | |
| 565 | fake_pdu.size = len; |
| 566 | fake_pdu.capacity = len; |
| 567 | fake_pdu.sdata = buf; |
| 568 | fake_pdu.offset = 0; |
| 569 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 570 | ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st); |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 571 | if (ret) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 572 | p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret); |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 573 | trace_9p_protocol_dump(clnt, &fake_pdu); |
Gertjan Halkes | 2803cf4 | 2018-09-05 15:41:29 +0900 | [diff] [blame] | 574 | return ret; |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 575 | } |
| 576 | |
Gertjan Halkes | 2803cf4 | 2018-09-05 15:41:29 +0900 | [diff] [blame] | 577 | return fake_pdu.offset; |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 578 | } |
| 579 | EXPORT_SYMBOL(p9stat_read); |
| 580 | |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 581 | int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type) |
| 582 | { |
Venkateswararao Jujjuri (JV) | 9bb6c10 | 2011-02-02 17:52:46 -0800 | [diff] [blame] | 583 | pdu->id = type; |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 584 | return p9pdu_writef(pdu, 0, "dbw", 0, type, tag); |
| 585 | } |
| 586 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 587 | int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu) |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 588 | { |
| 589 | int size = pdu->size; |
| 590 | int err; |
| 591 | |
| 592 | pdu->size = 0; |
| 593 | err = p9pdu_writef(pdu, 0, "d", size); |
| 594 | pdu->size = size; |
| 595 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 596 | trace_9p_protocol_dump(clnt, pdu); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 597 | p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", |
| 598 | pdu->size, pdu->id, pdu->tag); |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 599 | |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 600 | return err; |
| 601 | } |
| 602 | |
| 603 | void p9pdu_reset(struct p9_fcall *pdu) |
| 604 | { |
| 605 | pdu->offset = 0; |
| 606 | pdu->size = 0; |
| 607 | } |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 608 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 609 | int p9dirent_read(struct p9_client *clnt, char *buf, int len, |
| 610 | struct p9_dirent *dirent) |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 611 | { |
| 612 | struct p9_fcall fake_pdu; |
| 613 | int ret; |
| 614 | char *nameptr; |
| 615 | |
| 616 | fake_pdu.size = len; |
| 617 | fake_pdu.capacity = len; |
| 618 | fake_pdu.sdata = buf; |
| 619 | fake_pdu.offset = 0; |
| 620 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 621 | ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid, |
| 622 | &dirent->d_off, &dirent->d_type, &nameptr); |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 623 | if (ret) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 624 | p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret); |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 625 | trace_9p_protocol_dump(clnt, &fake_pdu); |
Dominique Martinet | ef5305f | 2018-09-08 00:36:08 +0900 | [diff] [blame^] | 626 | return ret; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Dominique Martinet | ef5305f | 2018-09-08 00:36:08 +0900 | [diff] [blame^] | 629 | ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name)); |
| 630 | if (ret < 0) { |
| 631 | p9_debug(P9_DEBUG_ERROR, |
| 632 | "On the wire dirent name too long: %s\n", |
| 633 | nameptr); |
| 634 | kfree(nameptr); |
| 635 | return ret; |
| 636 | } |
Pedro Scarapicchia Junior | 1b0bcbcf6 | 2011-05-09 14:10:49 +0000 | [diff] [blame] | 637 | kfree(nameptr); |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 638 | |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 639 | return fake_pdu.offset; |
| 640 | } |
| 641 | EXPORT_SYMBOL(p9dirent_read); |