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