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