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