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