| 1 | /* |
| 2 | Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifdef WIN32 |
| 19 | #include "win32_compat.h" |
| 20 | #else |
| 21 | #include <sys/stat.h> |
| 22 | #endif/*WIN32*/ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <errno.h> |
| 26 | #include <string.h> |
| 27 | #include <rpc/rpc.h> |
| 28 | #include <rpc/xdr.h> |
| 29 | #include "libnfs.h" |
| 30 | #include "libnfs-raw.h" |
| 31 | #include "libnfs-private.h" |
| 32 | #include "libnfs-raw-nfs.h" |
| 33 | |
| 34 | char *nfsstat3_to_str(int error) |
| 35 | { |
| 36 | switch (error) { |
| 37 | case NFS3_OK: return "NFS3_OK"; break; |
| 38 | case NFS3ERR_PERM: return "NFS3ERR_PERM"; break; |
| 39 | case NFS3ERR_NOENT: return "NFS3ERR_NOENT"; break; |
| 40 | case NFS3ERR_IO: return "NFS3ERR_IO"; break; |
| 41 | case NFS3ERR_NXIO: return "NFS3ERR_NXIO"; break; |
| 42 | case NFS3ERR_ACCES: return "NFS3ERR_ACCES"; break; |
| 43 | case NFS3ERR_EXIST: return "NFS3ERR_EXIST"; break; |
| 44 | case NFS3ERR_XDEV: return "NFS3ERR_XDEV"; break; |
| 45 | case NFS3ERR_NODEV: return "NFS3ERR_NODEV"; break; |
| 46 | case NFS3ERR_NOTDIR: return "NFS3ERR_NOTDIR"; break; |
| 47 | case NFS3ERR_ISDIR: return "NFS3ERR_ISDIR"; break; |
| 48 | case NFS3ERR_INVAL: return "NFS3ERR_INVAL"; break; |
| 49 | case NFS3ERR_FBIG: return "NFS3ERR_FBIG"; break; |
| 50 | case NFS3ERR_NOSPC: return "NFS3ERR_NOSPC"; break; |
| 51 | case NFS3ERR_ROFS: return "NFS3ERR_ROFS"; break; |
| 52 | case NFS3ERR_MLINK: return "NFS3ERR_MLINK"; break; |
| 53 | case NFS3ERR_NAMETOOLONG: return "NFS3ERR_NAMETOOLONG"; break; |
| 54 | case NFS3ERR_NOTEMPTY: return "NFS3ERR_NOTEMPTY"; break; |
| 55 | case NFS3ERR_DQUOT: return "NFS3ERR_DQUOT"; break; |
| 56 | case NFS3ERR_STALE: return "NFS3ERR_STALE"; break; |
| 57 | case NFS3ERR_REMOTE: return "NFS3ERR_REMOTE"; break; |
| 58 | case NFS3ERR_BADHANDLE: return "NFS3ERR_BADHANDLE"; break; |
| 59 | case NFS3ERR_NOT_SYNC: return "NFS3ERR_NOT_SYNC"; break; |
| 60 | case NFS3ERR_BAD_COOKIE: return "NFS3ERR_BAD_COOKIE"; break; |
| 61 | case NFS3ERR_NOTSUPP: return "NFS3ERR_NOTSUPP"; break; |
| 62 | case NFS3ERR_TOOSMALL: return "NFS3ERR_TOOSMALL"; break; |
| 63 | case NFS3ERR_SERVERFAULT: return "NFS3ERR_SERVERFAULT"; break; |
| 64 | case NFS3ERR_BADTYPE: return "NFS3ERR_BADTYPE"; break; |
| 65 | case NFS3ERR_JUKEBOX: return "NFS3ERR_JUKEBOX"; break; |
| 66 | }; |
| 67 | return "unknown nfs error"; |
| 68 | } |
| 69 | |
| 70 | int nfsstat3_to_errno(int error) |
| 71 | { |
| 72 | switch (error) { |
| 73 | case NFS3_OK: return 0; break; |
| 74 | case NFS3ERR_PERM: return -EPERM; break; |
| 75 | case NFS3ERR_NOENT: return -ENOENT; break; |
| 76 | case NFS3ERR_IO: return -EIO; break; |
| 77 | case NFS3ERR_NXIO: return -ENXIO; break; |
| 78 | case NFS3ERR_ACCES: return -EACCES; break; |
| 79 | case NFS3ERR_EXIST: return -EEXIST; break; |
| 80 | case NFS3ERR_XDEV: return -EXDEV; break; |
| 81 | case NFS3ERR_NODEV: return -ENODEV; break; |
| 82 | case NFS3ERR_NOTDIR: return -ENOTDIR; break; |
| 83 | case NFS3ERR_ISDIR: return -EISDIR; break; |
| 84 | case NFS3ERR_INVAL: return -EINVAL; break; |
| 85 | case NFS3ERR_FBIG: return -EFBIG; break; |
| 86 | case NFS3ERR_NOSPC: return -ENOSPC; break; |
| 87 | case NFS3ERR_ROFS: return -EROFS; break; |
| 88 | case NFS3ERR_MLINK: return -EMLINK; break; |
| 89 | case NFS3ERR_NAMETOOLONG: return -ENAMETOOLONG; break; |
| 90 | case NFS3ERR_NOTEMPTY: return -EEXIST; break; |
| 91 | case NFS3ERR_DQUOT: return -ERANGE; break; |
| 92 | case NFS3ERR_STALE: return -EIO; break; |
| 93 | case NFS3ERR_REMOTE: return -EIO; break; |
| 94 | case NFS3ERR_BADHANDLE: return -EIO; break; |
| 95 | case NFS3ERR_NOT_SYNC: return -EIO; break; |
| 96 | case NFS3ERR_BAD_COOKIE: return -EIO; break; |
| 97 | case NFS3ERR_NOTSUPP: return -EINVAL; break; |
| 98 | case NFS3ERR_TOOSMALL: return -EIO; break; |
| 99 | case NFS3ERR_SERVERFAULT: return -EIO; break; |
| 100 | case NFS3ERR_BADTYPE: return -EINVAL; break; |
| 101 | case NFS3ERR_JUKEBOX: return -EAGAIN; break; |
| 102 | }; |
| 103 | return -ERANGE; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | int rpc_nfs_null_async(struct rpc_context *rpc, rpc_cb cb, void *private_data) |
| 108 | { |
| 109 | struct rpc_pdu *pdu; |
| 110 | |
| 111 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_NULL, cb, private_data, (xdrproc_t)xdr_void, 0); |
| 112 | if (pdu == NULL) { |
| 113 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/null call"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 118 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/null call"); |
| 119 | rpc_free_pdu(rpc, pdu); |
| 120 | return -2; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int rpc_nfs_getattr_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, void *private_data) |
| 127 | { |
| 128 | struct rpc_pdu *pdu; |
| 129 | GETATTR3args args; |
| 130 | |
| 131 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_GETATTR, cb, private_data, (xdrproc_t)xdr_GETATTR3res, sizeof(GETATTR3res)); |
| 132 | if (pdu == NULL) { |
| 133 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/null call"); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | args.object.data.data_len = fh->data.data_len; |
| 138 | args.object.data.data_val = fh->data.data_val; |
| 139 | |
| 140 | if (xdr_GETATTR3args(&pdu->xdr, &args) == 0) { |
| 141 | rpc_set_error(rpc, "XDR error: Failed to encode GETATTR3args"); |
| 142 | rpc_free_pdu(rpc, pdu); |
| 143 | return -2; |
| 144 | } |
| 145 | |
| 146 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 147 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/null call"); |
| 148 | rpc_free_pdu(rpc, pdu); |
| 149 | return -3; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int rpc_nfs_lookup_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *name, void *private_data) |
| 156 | { |
| 157 | struct rpc_pdu *pdu; |
| 158 | LOOKUP3args args; |
| 159 | |
| 160 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_LOOKUP, cb, private_data, (xdrproc_t)xdr_LOOKUP3res, sizeof(LOOKUP3res)); |
| 161 | if (pdu == NULL) { |
| 162 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/lookup call"); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | args.what.dir.data.data_len = fh->data.data_len; |
| 167 | args.what.dir.data.data_val = fh->data.data_val; |
| 168 | args.what.name = name; |
| 169 | |
| 170 | if (xdr_LOOKUP3args(&pdu->xdr, &args) == 0) { |
| 171 | rpc_set_error(rpc, "XDR error: Failed to encode LOOKUP3args"); |
| 172 | rpc_free_pdu(rpc, pdu); |
| 173 | return -2; |
| 174 | } |
| 175 | |
| 176 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 177 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/lookup call"); |
| 178 | rpc_free_pdu(rpc, pdu); |
| 179 | return -3; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | int rpc_nfs_access_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, int access, void *private_data) |
| 187 | { |
| 188 | struct rpc_pdu *pdu; |
| 189 | ACCESS3args args; |
| 190 | |
| 191 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_ACCESS, cb, private_data, (xdrproc_t)xdr_ACCESS3res, sizeof(ACCESS3res)); |
| 192 | if (pdu == NULL) { |
| 193 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/access call"); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | args.object.data.data_len = fh->data.data_len; |
| 198 | args.object.data.data_val = fh->data.data_val; |
| 199 | args.access = access; |
| 200 | |
| 201 | if (xdr_ACCESS3args(&pdu->xdr, &args) == 0) { |
| 202 | rpc_set_error(rpc, "XDR error: Failed to encode ACCESS3args"); |
| 203 | rpc_free_pdu(rpc, pdu); |
| 204 | return -2; |
| 205 | } |
| 206 | |
| 207 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 208 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/access call"); |
| 209 | rpc_free_pdu(rpc, pdu); |
| 210 | return -3; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | int rpc_nfs_read_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, uint64_t offset, uint64_t count, void *private_data) |
| 219 | { |
| 220 | struct rpc_pdu *pdu; |
| 221 | READ3args args; |
| 222 | |
| 223 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_READ, cb, private_data, (xdrproc_t)xdr_READ3res, sizeof(READ3res)); |
| 224 | if (pdu == NULL) { |
| 225 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/read call"); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | args.file.data.data_len = fh->data.data_len; |
| 230 | args.file.data.data_val = fh->data.data_val; |
| 231 | args.offset = offset; |
| 232 | args.count = count; |
| 233 | |
| 234 | if (xdr_READ3args(&pdu->xdr, &args) == 0) { |
| 235 | rpc_set_error(rpc, "XDR error: Failed to encode READ3args"); |
| 236 | rpc_free_pdu(rpc, pdu); |
| 237 | return -2; |
| 238 | } |
| 239 | |
| 240 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 241 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/read call"); |
| 242 | rpc_free_pdu(rpc, pdu); |
| 243 | return -3; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | int rpc_nfs_write_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *buf, uint64_t offset, uint64_t count, int stable_how, void *private_data) |
| 251 | { |
| 252 | struct rpc_pdu *pdu; |
| 253 | WRITE3args args; |
| 254 | |
| 255 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_WRITE, cb, private_data, (xdrproc_t)xdr_WRITE3res, sizeof(WRITE3res)); |
| 256 | if (pdu == NULL) { |
| 257 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/write call"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | args.file.data.data_len = fh->data.data_len; |
| 262 | args.file.data.data_val = fh->data.data_val; |
| 263 | args.offset = offset; |
| 264 | args.count = count; |
| 265 | args.stable = stable_how;; |
| 266 | args.data.data_len = count; |
| 267 | args.data.data_val = buf; |
| 268 | |
| 269 | if (xdr_WRITE3args(&pdu->xdr, &args) == 0) { |
| 270 | rpc_set_error(rpc, "XDR error: Failed to encode WRITE3args"); |
| 271 | rpc_free_pdu(rpc, pdu); |
| 272 | return -2; |
| 273 | } |
| 274 | |
| 275 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 276 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/write call"); |
| 277 | rpc_free_pdu(rpc, pdu); |
| 278 | return -3; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | int rpc_nfs_commit_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, void *private_data) |
| 287 | { |
| 288 | struct rpc_pdu *pdu; |
| 289 | COMMIT3args args; |
| 290 | |
| 291 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_COMMIT, cb, private_data, (xdrproc_t)xdr_COMMIT3res, sizeof(COMMIT3res)); |
| 292 | if (pdu == NULL) { |
| 293 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/commit call"); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | args.file.data.data_len = fh->data.data_len; |
| 298 | args.file.data.data_val = fh->data.data_val; |
| 299 | args.offset = 0; |
| 300 | args.count = 0; |
| 301 | |
| 302 | if (xdr_COMMIT3args(&pdu->xdr, &args) == 0) { |
| 303 | rpc_set_error(rpc, "XDR error: Failed to encode WRITE3args"); |
| 304 | rpc_free_pdu(rpc, pdu); |
| 305 | return -2; |
| 306 | } |
| 307 | |
| 308 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 309 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/commit call"); |
| 310 | rpc_free_pdu(rpc, pdu); |
| 311 | return -3; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | int rpc_nfs_setattr_async(struct rpc_context *rpc, rpc_cb cb, SETATTR3args *args, void *private_data) |
| 319 | { |
| 320 | struct rpc_pdu *pdu; |
| 321 | |
| 322 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_SETATTR, cb, private_data, (xdrproc_t)xdr_SETATTR3res, sizeof(SETATTR3res)); |
| 323 | if (pdu == NULL) { |
| 324 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/setattr call"); |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | if (xdr_SETATTR3args(&pdu->xdr, args) == 0) { |
| 329 | rpc_set_error(rpc, "XDR error: Failed to encode SETATTR3args"); |
| 330 | rpc_free_pdu(rpc, pdu); |
| 331 | return -2; |
| 332 | } |
| 333 | |
| 334 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 335 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/setattr call"); |
| 336 | rpc_free_pdu(rpc, pdu); |
| 337 | return -3; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | int rpc_nfs_mkdir_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *dir, void *private_data) |
| 346 | { |
| 347 | struct rpc_pdu *pdu; |
| 348 | MKDIR3args args; |
| 349 | |
| 350 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_MKDIR, cb, private_data, (xdrproc_t)xdr_MKDIR3res, sizeof(MKDIR3res)); |
| 351 | if (pdu == NULL) { |
| 352 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/setattr call"); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | memset(&args, 0, sizeof(MKDIR3args)); |
| 357 | args.where.dir.data.data_len = fh->data.data_len; |
| 358 | args.where.dir.data.data_val = fh->data.data_val; |
| 359 | args.where.name = dir; |
| 360 | args.attributes.mode.set_it = 1; |
| 361 | args.attributes.mode.set_mode3_u.mode = 0755; |
| 362 | |
| 363 | if (xdr_MKDIR3args(&pdu->xdr, &args) == 0) { |
| 364 | rpc_set_error(rpc, "XDR error: Failed to encode MKDIR3args"); |
| 365 | rpc_free_pdu(rpc, pdu); |
| 366 | return -2; |
| 367 | } |
| 368 | |
| 369 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 370 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/mkdir call"); |
| 371 | rpc_free_pdu(rpc, pdu); |
| 372 | return -3; |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | int rpc_nfs_rmdir_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *dir, void *private_data) |
| 382 | { |
| 383 | struct rpc_pdu *pdu; |
| 384 | RMDIR3args args; |
| 385 | |
| 386 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_RMDIR, cb, private_data, (xdrproc_t)xdr_RMDIR3res, sizeof(RMDIR3res)); |
| 387 | if (pdu == NULL) { |
| 388 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/rmdir call"); |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | memset(&args, 0, sizeof(RMDIR3args)); |
| 393 | args.object.dir.data.data_len = fh->data.data_len; |
| 394 | args.object.dir.data.data_val = fh->data.data_val; |
| 395 | args.object.name = dir; |
| 396 | |
| 397 | if (xdr_RMDIR3args(&pdu->xdr, &args) == 0) { |
| 398 | rpc_set_error(rpc, "XDR error: Failed to encode RMDIR3args"); |
| 399 | rpc_free_pdu(rpc, pdu); |
| 400 | return -2; |
| 401 | } |
| 402 | |
| 403 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 404 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/rmdir call"); |
| 405 | rpc_free_pdu(rpc, pdu); |
| 406 | return -3; |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | int rpc_nfs_create_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *file, int mode, void *private_data) |
| 415 | { |
| 416 | struct rpc_pdu *pdu; |
| 417 | CREATE3args args; |
| 418 | |
| 419 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_CREATE, cb, private_data, (xdrproc_t)xdr_CREATE3res, sizeof(CREATE3res)); |
| 420 | if (pdu == NULL) { |
| 421 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/create call"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | memset(&args, 0, sizeof(CREATE3args)); |
| 426 | args.where.dir.data.data_len = fh->data.data_len; |
| 427 | args.where.dir.data.data_val = fh->data.data_val; |
| 428 | args.where.name = file; |
| 429 | args.how.mode = UNCHECKED; |
| 430 | args.how.createhow3_u.obj_attributes.mode.set_it = 1; |
| 431 | args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = mode; |
| 432 | |
| 433 | if (xdr_CREATE3args(&pdu->xdr, &args) == 0) { |
| 434 | rpc_set_error(rpc, "XDR error: Failed to encode CREATE3args"); |
| 435 | rpc_free_pdu(rpc, pdu); |
| 436 | return -2; |
| 437 | } |
| 438 | |
| 439 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 440 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/create call"); |
| 441 | rpc_free_pdu(rpc, pdu); |
| 442 | return -3; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | int rpc_nfs_mknod_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *file, int mode, int major, int minor, void *private_data) |
| 451 | { |
| 452 | struct rpc_pdu *pdu; |
| 453 | MKNOD3args args; |
| 454 | |
| 455 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_MKNOD, cb, private_data, (xdrproc_t)xdr_MKNOD3res, sizeof(MKNOD3res)); |
| 456 | if (pdu == NULL) { |
| 457 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/mknod call"); |
| 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | memset(&args, 0, sizeof(MKNOD3args)); |
| 462 | args.where.dir.data.data_len = fh->data.data_len; |
| 463 | args.where.dir.data.data_val = fh->data.data_val; |
| 464 | args.where.name = file; |
| 465 | switch (mode & S_IFMT) { |
| 466 | case S_IFCHR: |
| 467 | args.what.type = NF3CHR; |
| 468 | args.what.mknoddata3_u.chr_device.dev_attributes.mode.set_it = 1; |
| 469 | args.what.mknoddata3_u.chr_device.dev_attributes.mode.set_mode3_u.mode = mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 470 | args.what.mknoddata3_u.chr_device.spec.specdata1 = major; |
| 471 | args.what.mknoddata3_u.chr_device.spec.specdata2 = minor; |
| 472 | break; |
| 473 | case S_IFBLK: |
| 474 | args.what.type = NF3BLK; |
| 475 | args.what.mknoddata3_u.blk_device.dev_attributes.mode.set_it = 1; |
| 476 | args.what.mknoddata3_u.blk_device.dev_attributes.mode.set_mode3_u.mode = mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 477 | args.what.mknoddata3_u.blk_device.spec.specdata1 = major; |
| 478 | args.what.mknoddata3_u.blk_device.spec.specdata2 = minor; |
| 479 | case S_IFSOCK: |
| 480 | args.what.type = NF3SOCK; |
| 481 | args.what.mknoddata3_u.sock_attributes.mode.set_it = 1; |
| 482 | args.what.mknoddata3_u.sock_attributes.mode.set_mode3_u.mode = mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 483 | break; |
| 484 | case S_IFIFO: |
| 485 | args.what.type = NF3FIFO; |
| 486 | args.what.mknoddata3_u.pipe_attributes.mode.set_it = 1; |
| 487 | args.what.mknoddata3_u.pipe_attributes.mode.set_mode3_u.mode = mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 488 | break; |
| 489 | default: |
| 490 | rpc_set_error(rpc, "Invalid file type for nfs/mknod call"); |
| 491 | rpc_free_pdu(rpc, pdu); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | if (xdr_MKNOD3args(&pdu->xdr, &args) == 0) { |
| 496 | rpc_set_error(rpc, "XDR error: Failed to encode MKNOD3args"); |
| 497 | rpc_free_pdu(rpc, pdu); |
| 498 | return -2; |
| 499 | } |
| 500 | |
| 501 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 502 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/mknod call"); |
| 503 | rpc_free_pdu(rpc, pdu); |
| 504 | return -3; |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | int rpc_nfs_remove_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *file, void *private_data) |
| 512 | { |
| 513 | struct rpc_pdu *pdu; |
| 514 | REMOVE3args args; |
| 515 | |
| 516 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_REMOVE, cb, private_data, (xdrproc_t)xdr_REMOVE3res, sizeof(REMOVE3res)); |
| 517 | if (pdu == NULL) { |
| 518 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/remove call"); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | memset(&args, 0, sizeof(REMOVE3args)); |
| 523 | args.object.dir.data.data_len = fh->data.data_len; |
| 524 | args.object.dir.data.data_val = fh->data.data_val; |
| 525 | args.object.name = file; |
| 526 | |
| 527 | if (xdr_REMOVE3args(&pdu->xdr, &args) == 0) { |
| 528 | rpc_set_error(rpc, "XDR error: Failed to encode REMOVE3args"); |
| 529 | rpc_free_pdu(rpc, pdu); |
| 530 | return -2; |
| 531 | } |
| 532 | |
| 533 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 534 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/remove call"); |
| 535 | rpc_free_pdu(rpc, pdu); |
| 536 | return -3; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | int rpc_nfs_readdir_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, uint64_t cookie, char *cookieverf, int count, void *private_data) |
| 543 | { |
| 544 | struct rpc_pdu *pdu; |
| 545 | READDIR3args args; |
| 546 | |
| 547 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_READDIR, cb, private_data, (xdrproc_t)xdr_READDIR3res, sizeof(READDIR3res)); |
| 548 | if (pdu == NULL) { |
| 549 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/readdir call"); |
| 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | memset(&args, 0, sizeof(READDIR3args)); |
| 554 | args.dir.data.data_len = fh->data.data_len; |
| 555 | args.dir.data.data_val = fh->data.data_val; |
| 556 | args.cookie = cookie; |
| 557 | memcpy(&args.cookieverf, cookieverf, sizeof(cookieverf3)); |
| 558 | args.count = count; |
| 559 | |
| 560 | if (xdr_READDIR3args(&pdu->xdr, &args) == 0) { |
| 561 | rpc_set_error(rpc, "XDR error: Failed to encode READDIR3args"); |
| 562 | rpc_free_pdu(rpc, pdu); |
| 563 | return -2; |
| 564 | } |
| 565 | |
| 566 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 567 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/readdir call"); |
| 568 | rpc_free_pdu(rpc, pdu); |
| 569 | return -3; |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | int rpc_nfs_readdirplus_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, uint64_t cookie, char *cookieverf, int count, void *private_data) |
| 576 | { |
| 577 | struct rpc_pdu *pdu; |
| 578 | READDIRPLUS3args args; |
| 579 | |
| 580 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_READDIRPLUS, cb, private_data, (xdrproc_t)xdr_READDIRPLUS3res, sizeof(READDIRPLUS3res)); |
| 581 | if (pdu == NULL) { |
| 582 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/readdirplus call"); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | memset(&args, 0, sizeof(READDIRPLUS3args)); |
| 587 | args.dir.data.data_len = fh->data.data_len; |
| 588 | args.dir.data.data_val = fh->data.data_val; |
| 589 | args.cookie = cookie; |
| 590 | memcpy(&args.cookieverf, cookieverf, sizeof(cookieverf3)); |
| 591 | args.dircount = count; |
| 592 | args.maxcount = count; |
| 593 | |
| 594 | if (xdr_READDIRPLUS3args(&pdu->xdr, &args) == 0) { |
| 595 | rpc_set_error(rpc, "XDR error: Failed to encode READDIRPLUS3args"); |
| 596 | rpc_free_pdu(rpc, pdu); |
| 597 | return -2; |
| 598 | } |
| 599 | |
| 600 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 601 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/readdirplus call"); |
| 602 | rpc_free_pdu(rpc, pdu); |
| 603 | return -3; |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | int rpc_nfs_fsstat_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, void *private_data) |
| 610 | { |
| 611 | struct rpc_pdu *pdu; |
| 612 | FSSTAT3args args; |
| 613 | |
| 614 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_FSSTAT, cb, private_data, (xdrproc_t)xdr_FSSTAT3res, sizeof(FSSTAT3res)); |
| 615 | if (pdu == NULL) { |
| 616 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/fsstat call"); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | args.fsroot.data.data_len = fh->data.data_len; |
| 621 | args.fsroot.data.data_val = fh->data.data_val; |
| 622 | |
| 623 | if (xdr_FSSTAT3args(&pdu->xdr, &args) == 0) { |
| 624 | rpc_set_error(rpc, "XDR error: Failed to encode FSSTAT3args"); |
| 625 | rpc_free_pdu(rpc, pdu); |
| 626 | return -2; |
| 627 | } |
| 628 | |
| 629 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 630 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/fsstat call"); |
| 631 | rpc_free_pdu(rpc, pdu); |
| 632 | return -3; |
| 633 | } |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | int rpc_nfs_fsinfo_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, void *private_data) |
| 639 | { |
| 640 | struct rpc_pdu *pdu; |
| 641 | FSINFO3args args; |
| 642 | |
| 643 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_FSINFO, cb, private_data, (xdrproc_t)xdr_FSINFO3res, sizeof(FSINFO3res)); |
| 644 | if (pdu == NULL) { |
| 645 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/fsinfo call"); |
| 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | args.fsroot.data.data_len = fh->data.data_len; |
| 650 | args.fsroot.data.data_val = fh->data.data_val; |
| 651 | |
| 652 | if (xdr_FSINFO3args(&pdu->xdr, &args) == 0) { |
| 653 | rpc_set_error(rpc, "XDR error: Failed to encode FSINFO3args"); |
| 654 | rpc_free_pdu(rpc, pdu); |
| 655 | return -2; |
| 656 | } |
| 657 | |
| 658 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 659 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/fsinfo call"); |
| 660 | rpc_free_pdu(rpc, pdu); |
| 661 | return -3; |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | int rpc_nfs_readlink_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, void *private_data) |
| 668 | { |
| 669 | struct rpc_pdu *pdu; |
| 670 | READLINK3args args; |
| 671 | |
| 672 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_READLINK, cb, private_data, (xdrproc_t)xdr_READLINK3res, sizeof(READLINK3res)); |
| 673 | if (pdu == NULL) { |
| 674 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/readlink call"); |
| 675 | return -1; |
| 676 | } |
| 677 | |
| 678 | args.symlink.data.data_len = fh->data.data_len; |
| 679 | args.symlink.data.data_val = fh->data.data_val; |
| 680 | |
| 681 | if (xdr_READLINK3args(&pdu->xdr, &args) == 0) { |
| 682 | rpc_set_error(rpc, "XDR error: Failed to encode READLINK3args"); |
| 683 | rpc_free_pdu(rpc, pdu); |
| 684 | return -2; |
| 685 | } |
| 686 | |
| 687 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 688 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/readlink call"); |
| 689 | rpc_free_pdu(rpc, pdu); |
| 690 | return -3; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | int rpc_nfs_symlink_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *fh, char *newname, char *oldpath, void *private_data) |
| 698 | { |
| 699 | struct rpc_pdu *pdu; |
| 700 | SYMLINK3args args; |
| 701 | |
| 702 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_SYMLINK, cb, private_data, (xdrproc_t)xdr_SYMLINK3res, sizeof(SYMLINK3res)); |
| 703 | if (pdu == NULL) { |
| 704 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/symlink call"); |
| 705 | return -1; |
| 706 | } |
| 707 | |
| 708 | memset(&args, 0, sizeof(SYMLINK3args)); |
| 709 | args.where.dir.data.data_len = fh->data.data_len; |
| 710 | args.where.dir.data.data_val = fh->data.data_val; |
| 711 | args.where.name = newname; |
| 712 | args.symlink.symlink_attributes.mode.set_it = 1; |
| 713 | args.symlink.symlink_attributes.mode.set_mode3_u.mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH; |
| 714 | args.symlink.symlink_data = oldpath; |
| 715 | |
| 716 | if (xdr_SYMLINK3args(&pdu->xdr, &args) == 0) { |
| 717 | rpc_set_error(rpc, "XDR error: Failed to encode SYMLINK3args"); |
| 718 | rpc_free_pdu(rpc, pdu); |
| 719 | return -2; |
| 720 | } |
| 721 | |
| 722 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 723 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/symlink call"); |
| 724 | rpc_free_pdu(rpc, pdu); |
| 725 | return -3; |
| 726 | } |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | int rpc_nfs_rename_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *olddir, char *oldname, struct nfs_fh3 *newdir, char *newname, void *private_data) |
| 735 | { |
| 736 | struct rpc_pdu *pdu; |
| 737 | RENAME3args args; |
| 738 | |
| 739 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_RENAME, cb, private_data, (xdrproc_t)xdr_RENAME3res, sizeof(RENAME3res)); |
| 740 | if (pdu == NULL) { |
| 741 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/rename call"); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | memset(&args, 0, sizeof(RENAME3args)); |
| 746 | args.from.dir.data.data_len = olddir->data.data_len; |
| 747 | args.from.dir.data.data_val = olddir->data.data_val; |
| 748 | args.from.name = oldname; |
| 749 | args.to.dir.data.data_len = newdir->data.data_len; |
| 750 | args.to.dir.data.data_val = newdir->data.data_val; |
| 751 | args.to.name = newname; |
| 752 | |
| 753 | if (xdr_RENAME3args(&pdu->xdr, &args) == 0) { |
| 754 | rpc_set_error(rpc, "XDR error: Failed to encode RENAME3args"); |
| 755 | rpc_free_pdu(rpc, pdu); |
| 756 | return -2; |
| 757 | } |
| 758 | |
| 759 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 760 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/rename call"); |
| 761 | rpc_free_pdu(rpc, pdu); |
| 762 | return -3; |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | int rpc_nfs_link_async(struct rpc_context *rpc, rpc_cb cb, struct nfs_fh3 *file, struct nfs_fh3 *newdir, char *newname, void *private_data) |
| 772 | { |
| 773 | struct rpc_pdu *pdu; |
| 774 | LINK3args args; |
| 775 | |
| 776 | pdu = rpc_allocate_pdu(rpc, NFS_PROGRAM, NFS_V3, NFS3_LINK, cb, private_data, (xdrproc_t)xdr_LINK3res, sizeof(LINK3res)); |
| 777 | if (pdu == NULL) { |
| 778 | rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for nfs/link call"); |
| 779 | return -1; |
| 780 | } |
| 781 | |
| 782 | memset(&args, 0, sizeof(LINK3args)); |
| 783 | args.file.data.data_len = file->data.data_len; |
| 784 | args.file.data.data_val = file->data.data_val; |
| 785 | args.link.dir.data.data_len = newdir->data.data_len; |
| 786 | args.link.dir.data.data_val = newdir->data.data_val; |
| 787 | args.link.name = newname; |
| 788 | |
| 789 | if (xdr_LINK3args(&pdu->xdr, &args) == 0) { |
| 790 | rpc_set_error(rpc, "XDR error: Failed to encode LINK3args"); |
| 791 | rpc_free_pdu(rpc, pdu); |
| 792 | return -2; |
| 793 | } |
| 794 | |
| 795 | if (rpc_queue_pdu(rpc, pdu) != 0) { |
| 796 | rpc_set_error(rpc, "Out of memory. Failed to queue pdu for nfs/link call"); |
| 797 | rpc_free_pdu(rpc, pdu); |
| 798 | return -3; |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | |
| 806 | |