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