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 | |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 117 | /* |
| 118 | b - int8_t |
| 119 | w - int16_t |
| 120 | d - int32_t |
| 121 | q - int64_t |
| 122 | s - string |
| 123 | S - stat |
| 124 | Q - qid |
| 125 | D - data blob (int32_t size followed by void *, results are not freed) |
| 126 | T - array of strings (int16_t count, followed by strings) |
| 127 | R - array of qids (int16_t count, followed by qids) |
Sripathi Kodi | f085312 | 2010-07-12 20:07:23 +0530 | [diff] [blame] | 128 | A - stat for 9p2000.L (p9_stat_dotl) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 129 | ? - if optional = 1, continue parsing |
| 130 | */ |
| 131 | |
| 132 | static int |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 133 | p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, |
| 134 | va_list ap) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 135 | { |
| 136 | const char *ptr; |
| 137 | int errcode = 0; |
| 138 | |
| 139 | for (ptr = fmt; *ptr; ptr++) { |
| 140 | switch (*ptr) { |
| 141 | case 'b':{ |
| 142 | int8_t *val = va_arg(ap, int8_t *); |
| 143 | if (pdu_read(pdu, val, sizeof(*val))) { |
| 144 | errcode = -EFAULT; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | break; |
| 149 | case 'w':{ |
| 150 | int16_t *val = va_arg(ap, int16_t *); |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 151 | __le16 le_val; |
| 152 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 153 | errcode = -EFAULT; |
| 154 | break; |
| 155 | } |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 156 | *val = le16_to_cpu(le_val); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 157 | } |
| 158 | break; |
| 159 | case 'd':{ |
| 160 | int32_t *val = va_arg(ap, int32_t *); |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 161 | __le32 le_val; |
| 162 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 163 | errcode = -EFAULT; |
| 164 | break; |
| 165 | } |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 166 | *val = le32_to_cpu(le_val); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 167 | } |
| 168 | break; |
| 169 | case 'q':{ |
| 170 | int64_t *val = va_arg(ap, int64_t *); |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 171 | __le64 le_val; |
| 172 | if (pdu_read(pdu, &le_val, sizeof(le_val))) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 173 | errcode = -EFAULT; |
| 174 | break; |
| 175 | } |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 176 | *val = le64_to_cpu(le_val); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 177 | } |
| 178 | break; |
| 179 | case 's':{ |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 180 | char **sptr = va_arg(ap, char **); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 181 | int16_t len; |
| 182 | int size; |
| 183 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 184 | errcode = p9pdu_readf(pdu, proto_version, |
| 185 | "w", &len); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 186 | if (errcode) |
| 187 | break; |
| 188 | |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 189 | size = max_t(int16_t, len, 0); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 190 | |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 191 | *sptr = kmalloc(size + 1, GFP_KERNEL); |
| 192 | if (*sptr == NULL) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 193 | errcode = -EFAULT; |
| 194 | break; |
| 195 | } |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 196 | if (pdu_read(pdu, *sptr, size)) { |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 197 | errcode = -EFAULT; |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 198 | kfree(*sptr); |
| 199 | *sptr = NULL; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 200 | } else |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 201 | (*sptr)[size] = 0; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 202 | } |
| 203 | break; |
| 204 | case 'Q':{ |
| 205 | struct p9_qid *qid = |
| 206 | va_arg(ap, struct p9_qid *); |
| 207 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 208 | errcode = p9pdu_readf(pdu, proto_version, "bdq", |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 209 | &qid->type, &qid->version, |
| 210 | &qid->path); |
| 211 | } |
| 212 | break; |
| 213 | case 'S':{ |
| 214 | struct p9_wstat *stbuf = |
| 215 | va_arg(ap, struct p9_wstat *); |
| 216 | |
Eric Van Hensbergen | f0a0ac2 | 2008-10-17 12:45:23 -0500 | [diff] [blame] | 217 | memset(stbuf, 0, sizeof(struct p9_wstat)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 218 | stbuf->n_uid = stbuf->n_gid = stbuf->n_muid = |
Eric Van Hensbergen | f0a0ac2 | 2008-10-17 12:45:23 -0500 | [diff] [blame] | 219 | -1; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 220 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 221 | p9pdu_readf(pdu, proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 222 | "wwdQdddqssss?sddd", |
| 223 | &stbuf->size, &stbuf->type, |
| 224 | &stbuf->dev, &stbuf->qid, |
| 225 | &stbuf->mode, &stbuf->atime, |
| 226 | &stbuf->mtime, &stbuf->length, |
| 227 | &stbuf->name, &stbuf->uid, |
| 228 | &stbuf->gid, &stbuf->muid, |
| 229 | &stbuf->extension, |
| 230 | &stbuf->n_uid, &stbuf->n_gid, |
| 231 | &stbuf->n_muid); |
| 232 | if (errcode) |
| 233 | p9stat_free(stbuf); |
| 234 | } |
| 235 | break; |
| 236 | case 'D':{ |
| 237 | int32_t *count = va_arg(ap, int32_t *); |
| 238 | void **data = va_arg(ap, void **); |
| 239 | |
| 240 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 241 | p9pdu_readf(pdu, proto_version, "d", count); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 242 | if (!errcode) { |
| 243 | *count = |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 244 | min_t(int32_t, *count, |
| 245 | pdu->size - pdu->offset); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 246 | *data = &pdu->sdata[pdu->offset]; |
| 247 | } |
| 248 | } |
| 249 | break; |
| 250 | case 'T':{ |
| 251 | int16_t *nwname = va_arg(ap, int16_t *); |
| 252 | char ***wnames = va_arg(ap, char ***); |
| 253 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 254 | errcode = p9pdu_readf(pdu, proto_version, |
| 255 | "w", nwname); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 256 | if (!errcode) { |
| 257 | *wnames = |
| 258 | kmalloc(sizeof(char *) * *nwname, |
| 259 | GFP_KERNEL); |
| 260 | if (!*wnames) |
| 261 | errcode = -ENOMEM; |
| 262 | } |
| 263 | |
| 264 | if (!errcode) { |
| 265 | int i; |
| 266 | |
| 267 | for (i = 0; i < *nwname; i++) { |
| 268 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 269 | p9pdu_readf(pdu, |
| 270 | proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 271 | "s", |
| 272 | &(*wnames)[i]); |
| 273 | if (errcode) |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (errcode) { |
| 279 | if (*wnames) { |
| 280 | int i; |
| 281 | |
| 282 | for (i = 0; i < *nwname; i++) |
| 283 | kfree((*wnames)[i]); |
| 284 | } |
| 285 | kfree(*wnames); |
| 286 | *wnames = NULL; |
| 287 | } |
| 288 | } |
| 289 | break; |
| 290 | case 'R':{ |
| 291 | int16_t *nwqid = va_arg(ap, int16_t *); |
| 292 | struct p9_qid **wqids = |
| 293 | va_arg(ap, struct p9_qid **); |
| 294 | |
| 295 | *wqids = NULL; |
| 296 | |
| 297 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 298 | p9pdu_readf(pdu, proto_version, "w", nwqid); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 299 | if (!errcode) { |
| 300 | *wqids = |
| 301 | kmalloc(*nwqid * |
| 302 | sizeof(struct p9_qid), |
| 303 | GFP_KERNEL); |
| 304 | if (*wqids == NULL) |
| 305 | errcode = -ENOMEM; |
| 306 | } |
| 307 | |
| 308 | if (!errcode) { |
| 309 | int i; |
| 310 | |
| 311 | for (i = 0; i < *nwqid; i++) { |
| 312 | errcode = |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 313 | p9pdu_readf(pdu, |
| 314 | proto_version, |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 315 | "Q", |
| 316 | &(*wqids)[i]); |
| 317 | if (errcode) |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (errcode) { |
| 323 | kfree(*wqids); |
| 324 | *wqids = NULL; |
| 325 | } |
| 326 | } |
| 327 | break; |
Sripathi Kodi | f085312 | 2010-07-12 20:07:23 +0530 | [diff] [blame] | 328 | case 'A': { |
| 329 | struct p9_stat_dotl *stbuf = |
| 330 | va_arg(ap, struct p9_stat_dotl *); |
| 331 | |
| 332 | memset(stbuf, 0, sizeof(struct p9_stat_dotl)); |
| 333 | errcode = |
| 334 | p9pdu_readf(pdu, proto_version, |
| 335 | "qQdddqqqqqqqqqqqqqqq", |
| 336 | &stbuf->st_result_mask, |
| 337 | &stbuf->qid, |
| 338 | &stbuf->st_mode, |
| 339 | &stbuf->st_uid, &stbuf->st_gid, |
| 340 | &stbuf->st_nlink, |
| 341 | &stbuf->st_rdev, &stbuf->st_size, |
| 342 | &stbuf->st_blksize, &stbuf->st_blocks, |
| 343 | &stbuf->st_atime_sec, |
| 344 | &stbuf->st_atime_nsec, |
| 345 | &stbuf->st_mtime_sec, |
| 346 | &stbuf->st_mtime_nsec, |
| 347 | &stbuf->st_ctime_sec, |
| 348 | &stbuf->st_ctime_nsec, |
| 349 | &stbuf->st_btime_sec, |
| 350 | &stbuf->st_btime_nsec, |
| 351 | &stbuf->st_gen, |
| 352 | &stbuf->st_data_version); |
| 353 | } |
| 354 | break; |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 355 | case '?': |
Sripathi Kodi | c56e4ac | 2010-03-25 12:40:35 +0000 | [diff] [blame] | 356 | if ((proto_version != p9_proto_2000u) && |
| 357 | (proto_version != p9_proto_2000L)) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 358 | return 0; |
| 359 | break; |
| 360 | default: |
| 361 | BUG(); |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | if (errcode) |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | return errcode; |
| 370 | } |
| 371 | |
| 372 | int |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 373 | p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt, |
| 374 | va_list ap) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 375 | { |
| 376 | const char *ptr; |
| 377 | int errcode = 0; |
| 378 | |
| 379 | for (ptr = fmt; *ptr; ptr++) { |
| 380 | switch (*ptr) { |
| 381 | case 'b':{ |
| 382 | int8_t val = va_arg(ap, int); |
| 383 | if (pdu_write(pdu, &val, sizeof(val))) |
| 384 | errcode = -EFAULT; |
| 385 | } |
| 386 | break; |
| 387 | case 'w':{ |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 388 | __le16 val = cpu_to_le16(va_arg(ap, int)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 389 | if (pdu_write(pdu, &val, sizeof(val))) |
| 390 | errcode = -EFAULT; |
| 391 | } |
| 392 | break; |
| 393 | case 'd':{ |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 394 | __le32 val = cpu_to_le32(va_arg(ap, int32_t)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 395 | if (pdu_write(pdu, &val, sizeof(val))) |
| 396 | errcode = -EFAULT; |
| 397 | } |
| 398 | break; |
| 399 | case 'q':{ |
Eric Van Hensbergen | beeebc9 | 2009-02-06 22:07:41 -0800 | [diff] [blame] | 400 | __le64 val = cpu_to_le64(va_arg(ap, int64_t)); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 401 | if (pdu_write(pdu, &val, sizeof(val))) |
| 402 | errcode = -EFAULT; |
| 403 | } |
| 404 | break; |
| 405 | case 's':{ |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 406 | const char *sptr = va_arg(ap, const char *); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 407 | int16_t len = 0; |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 408 | if (sptr) |
Thiago Farina | 01b0c5c | 2010-12-04 15:22:46 +0000 | [diff] [blame] | 409 | len = min_t(int16_t, strlen(sptr), USHRT_MAX); |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 410 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 411 | errcode = p9pdu_writef(pdu, proto_version, |
| 412 | "w", len); |
Eric Van Hensbergen | e45c540 | 2008-10-22 18:54:47 -0500 | [diff] [blame] | 413 | if (!errcode && pdu_write(pdu, sptr, len)) |
Eric Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 414 | errcode = -EFAULT; |
| 415 | } |
| 416 | break; |
| 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 Van Hensbergen | ace51c4 | 2008-10-13 20:40:27 -0500 | [diff] [blame] | 430 | "wwdQdddqssss?sddd", |
| 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':{ |
| 441 | int32_t count = va_arg(ap, int32_t); |
| 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':{ |
| 461 | int16_t nwname = va_arg(ap, int); |
| 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, |
| 508 | "ddddqqqqq", |
| 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 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 562 | 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] | 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 | |
Sripathi Kodi | 342fee1 | 2010-03-05 18:50:14 +0000 | [diff] [blame] | 572 | ret = p9pdu_readf(&fake_pdu, proto_version, "S", st); |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 573 | if (ret) { |
| 574 | P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret); |
| 575 | p9pdu_dump(1, &fake_pdu); |
| 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 | { |
| 584 | return p9pdu_writef(pdu, 0, "dbw", 0, type, tag); |
| 585 | } |
| 586 | |
| 587 | int p9pdu_finalize(struct p9_fcall *pdu) |
| 588 | { |
| 589 | int size = pdu->size; |
| 590 | int err; |
| 591 | |
| 592 | pdu->size = 0; |
| 593 | err = p9pdu_writef(pdu, 0, "d", size); |
| 594 | pdu->size = size; |
| 595 | |
Eric Van Hensbergen | 0b15a3a | 2008-10-22 18:47:40 -0500 | [diff] [blame] | 596 | #ifdef CONFIG_NET_9P_DEBUG |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 597 | if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT) |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 598 | p9pdu_dump(0, pdu); |
Eric Van Hensbergen | 0b15a3a | 2008-10-22 18:47:40 -0500 | [diff] [blame] | 599 | #endif |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 600 | |
Eric Van Hensbergen | e7f4b8f | 2008-10-17 16:20:07 -0500 | [diff] [blame] | 601 | P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size, |
| 602 | pdu->id, pdu->tag); |
| 603 | |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 604 | return err; |
| 605 | } |
| 606 | |
| 607 | void p9pdu_reset(struct p9_fcall *pdu) |
| 608 | { |
| 609 | pdu->offset = 0; |
| 610 | pdu->size = 0; |
| 611 | } |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 612 | |
| 613 | int p9dirent_read(char *buf, int len, struct p9_dirent *dirent, |
| 614 | int proto_version) |
| 615 | { |
| 616 | struct p9_fcall fake_pdu; |
| 617 | int ret; |
| 618 | char *nameptr; |
| 619 | |
| 620 | fake_pdu.size = len; |
| 621 | fake_pdu.capacity = len; |
| 622 | fake_pdu.sdata = buf; |
| 623 | fake_pdu.offset = 0; |
| 624 | |
| 625 | ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid, |
| 626 | &dirent->d_off, &dirent->d_type, &nameptr); |
| 627 | if (ret) { |
| 628 | P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret); |
| 629 | p9pdu_dump(1, &fake_pdu); |
| 630 | goto out; |
| 631 | } |
| 632 | |
| 633 | strcpy(dirent->d_name, nameptr); |
| 634 | |
| 635 | out: |
| 636 | return fake_pdu.offset; |
| 637 | } |
| 638 | EXPORT_SYMBOL(p9dirent_read); |