1 /* 2 * fs/nfs/nfs4proc.c 3 * 4 * Client-side procedure declarations for NFSv4. 5 * 6 * Copyright (c) 2002 The Regents of the University of Michigan. 7 * All rights reserved. 8 * 9 * Kendrick Smith <kmsmith@umich.edu> 10 * Andy Adamson <andros@umich.edu> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <linux/mm.h> 39 #include <linux/delay.h> 40 #include <linux/errno.h> 41 #include <linux/string.h> 42 #include <linux/ratelimit.h> 43 #include <linux/printk.h> 44 #include <linux/slab.h> 45 #include <linux/sunrpc/clnt.h> 46 #include <linux/nfs.h> 47 #include <linux/nfs4.h> 48 #include <linux/nfs_fs.h> 49 #include <linux/nfs_page.h> 50 #include <linux/nfs_mount.h> 51 #include <linux/namei.h> 52 #include <linux/mount.h> 53 #include <linux/module.h> 54 #include <linux/xattr.h> 55 #include <linux/utsname.h> 56 #include <linux/freezer.h> 57 #include <linux/iversion.h> 58 59 #include "nfs4_fs.h" 60 #include "delegation.h" 61 #include "internal.h" 62 #include "iostat.h" 63 #include "callback.h" 64 #include "pnfs.h" 65 #include "netns.h" 66 #include "nfs4idmap.h" 67 #include "nfs4session.h" 68 #include "fscache.h" 69 70 #include "nfs4trace.h" 71 72 #define NFSDBG_FACILITY NFSDBG_PROC 73 74 #define NFS4_BITMASK_SZ 3 75 76 #define NFS4_POLL_RETRY_MIN (HZ/10) 77 #define NFS4_POLL_RETRY_MAX (15*HZ) 78 79 /* file attributes which can be mapped to nfs attributes */ 80 #define NFS4_VALID_ATTRS (ATTR_MODE \ 81 | ATTR_UID \ 82 | ATTR_GID \ 83 | ATTR_SIZE \ 84 | ATTR_ATIME \ 85 | ATTR_MTIME \ 86 | ATTR_CTIME \ 87 | ATTR_ATIME_SET \ 88 | ATTR_MTIME_SET) 89 90 struct nfs4_opendata; 91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data); 92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); 93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); 94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode); 95 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred, 96 struct nfs_fattr *fattr, struct iattr *sattr, 97 struct nfs_open_context *ctx, struct nfs4_label *ilabel, 98 struct nfs4_label *olabel); 99 #ifdef CONFIG_NFS_V4_1 100 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, 101 const struct cred *cred, 102 struct nfs4_slot *slot, 103 bool is_privileged); 104 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *, 105 const struct cred *); 106 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *, 107 const struct cred *, bool); 108 #endif 109 110 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 111 static inline struct nfs4_label * 112 nfs4_label_init_security(struct inode *dir, struct dentry *dentry, 113 struct iattr *sattr, struct nfs4_label *label) 114 { 115 int err; 116 117 if (label == NULL) 118 return NULL; 119 120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0) 121 return NULL; 122 123 err = security_dentry_init_security(dentry, sattr->ia_mode, 124 &dentry->d_name, (void **)&label->label, &label->len); 125 if (err == 0) 126 return label; 127 128 return NULL; 129 } 130 static inline void 131 nfs4_label_release_security(struct nfs4_label *label) 132 { 133 if (label) 134 security_release_secctx(label->label, label->len); 135 } 136 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) 137 { 138 if (label) 139 return server->attr_bitmask; 140 141 return server->attr_bitmask_nl; 142 } 143 #else 144 static inline struct nfs4_label * 145 nfs4_label_init_security(struct inode *dir, struct dentry *dentry, 146 struct iattr *sattr, struct nfs4_label *l) 147 { return NULL; } 148 static inline void 149 nfs4_label_release_security(struct nfs4_label *label) 150 { return; } 151 static inline u32 * 152 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) 153 { return server->attr_bitmask; } 154 #endif 155 156 /* Prevent leaks of NFSv4 errors into userland */ 157 static int nfs4_map_errors(int err) 158 { 159 if (err >= -1000) 160 return err; 161 switch (err) { 162 case -NFS4ERR_RESOURCE: 163 case -NFS4ERR_LAYOUTTRYLATER: 164 case -NFS4ERR_RECALLCONFLICT: 165 return -EREMOTEIO; 166 case -NFS4ERR_WRONGSEC: 167 case -NFS4ERR_WRONG_CRED: 168 return -EPERM; 169 case -NFS4ERR_BADOWNER: 170 case -NFS4ERR_BADNAME: 171 return -EINVAL; 172 case -NFS4ERR_SHARE_DENIED: 173 return -EACCES; 174 case -NFS4ERR_MINOR_VERS_MISMATCH: 175 return -EPROTONOSUPPORT; 176 case -NFS4ERR_FILE_OPEN: 177 return -EBUSY; 178 default: 179 dprintk("%s could not handle NFSv4 error %d\n", 180 __func__, -err); 181 break; 182 } 183 return -EIO; 184 } 185 186 /* 187 * This is our standard bitmap for GETATTR requests. 188 */ 189 const u32 nfs4_fattr_bitmap[3] = { 190 FATTR4_WORD0_TYPE 191 | FATTR4_WORD0_CHANGE 192 | FATTR4_WORD0_SIZE 193 | FATTR4_WORD0_FSID 194 | FATTR4_WORD0_FILEID, 195 FATTR4_WORD1_MODE 196 | FATTR4_WORD1_NUMLINKS 197 | FATTR4_WORD1_OWNER 198 | FATTR4_WORD1_OWNER_GROUP 199 | FATTR4_WORD1_RAWDEV 200 | FATTR4_WORD1_SPACE_USED 201 | FATTR4_WORD1_TIME_ACCESS 202 | FATTR4_WORD1_TIME_METADATA 203 | FATTR4_WORD1_TIME_MODIFY 204 | FATTR4_WORD1_MOUNTED_ON_FILEID, 205 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 206 FATTR4_WORD2_SECURITY_LABEL 207 #endif 208 }; 209 210 static const u32 nfs4_pnfs_open_bitmap[3] = { 211 FATTR4_WORD0_TYPE 212 | FATTR4_WORD0_CHANGE 213 | FATTR4_WORD0_SIZE 214 | FATTR4_WORD0_FSID 215 | FATTR4_WORD0_FILEID, 216 FATTR4_WORD1_MODE 217 | FATTR4_WORD1_NUMLINKS 218 | FATTR4_WORD1_OWNER 219 | FATTR4_WORD1_OWNER_GROUP 220 | FATTR4_WORD1_RAWDEV 221 | FATTR4_WORD1_SPACE_USED 222 | FATTR4_WORD1_TIME_ACCESS 223 | FATTR4_WORD1_TIME_METADATA 224 | FATTR4_WORD1_TIME_MODIFY, 225 FATTR4_WORD2_MDSTHRESHOLD 226 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 227 | FATTR4_WORD2_SECURITY_LABEL 228 #endif 229 }; 230 231 static const u32 nfs4_open_noattr_bitmap[3] = { 232 FATTR4_WORD0_TYPE 233 | FATTR4_WORD0_FILEID, 234 }; 235 236 const u32 nfs4_statfs_bitmap[3] = { 237 FATTR4_WORD0_FILES_AVAIL 238 | FATTR4_WORD0_FILES_FREE 239 | FATTR4_WORD0_FILES_TOTAL, 240 FATTR4_WORD1_SPACE_AVAIL 241 | FATTR4_WORD1_SPACE_FREE 242 | FATTR4_WORD1_SPACE_TOTAL 243 }; 244 245 const u32 nfs4_pathconf_bitmap[3] = { 246 FATTR4_WORD0_MAXLINK 247 | FATTR4_WORD0_MAXNAME, 248 0 249 }; 250 251 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE 252 | FATTR4_WORD0_MAXREAD 253 | FATTR4_WORD0_MAXWRITE 254 | FATTR4_WORD0_LEASE_TIME, 255 FATTR4_WORD1_TIME_DELTA 256 | FATTR4_WORD1_FS_LAYOUT_TYPES, 257 FATTR4_WORD2_LAYOUT_BLKSIZE 258 | FATTR4_WORD2_CLONE_BLKSIZE 259 }; 260 261 const u32 nfs4_fs_locations_bitmap[3] = { 262 FATTR4_WORD0_CHANGE 263 | FATTR4_WORD0_SIZE 264 | FATTR4_WORD0_FSID 265 | FATTR4_WORD0_FILEID 266 | FATTR4_WORD0_FS_LOCATIONS, 267 FATTR4_WORD1_OWNER 268 | FATTR4_WORD1_OWNER_GROUP 269 | FATTR4_WORD1_RAWDEV 270 | FATTR4_WORD1_SPACE_USED 271 | FATTR4_WORD1_TIME_ACCESS 272 | FATTR4_WORD1_TIME_METADATA 273 | FATTR4_WORD1_TIME_MODIFY 274 | FATTR4_WORD1_MOUNTED_ON_FILEID, 275 }; 276 277 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src, 278 struct inode *inode) 279 { 280 unsigned long cache_validity; 281 282 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst)); 283 if (!inode || !nfs4_have_delegation(inode, FMODE_READ)) 284 return; 285 286 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); 287 if (!(cache_validity & NFS_INO_REVAL_FORCED)) 288 cache_validity &= ~(NFS_INO_INVALID_CHANGE 289 | NFS_INO_INVALID_SIZE); 290 291 if (!(cache_validity & NFS_INO_INVALID_SIZE)) 292 dst[0] &= ~FATTR4_WORD0_SIZE; 293 294 if (!(cache_validity & NFS_INO_INVALID_CHANGE)) 295 dst[0] &= ~FATTR4_WORD0_CHANGE; 296 } 297 298 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst, 299 const __u32 *src, struct inode *inode) 300 { 301 nfs4_bitmap_copy_adjust(dst, src, inode); 302 } 303 304 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, 305 struct nfs4_readdir_arg *readdir) 306 { 307 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE; 308 __be32 *start, *p; 309 310 if (cookie > 2) { 311 readdir->cookie = cookie; 312 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); 313 return; 314 } 315 316 readdir->cookie = 0; 317 memset(&readdir->verifier, 0, sizeof(readdir->verifier)); 318 if (cookie == 2) 319 return; 320 321 /* 322 * NFSv4 servers do not return entries for '.' and '..' 323 * Therefore, we fake these entries here. We let '.' 324 * have cookie 0 and '..' have cookie 1. Note that 325 * when talking to the server, we always send cookie 0 326 * instead of 1 or 2. 327 */ 328 start = p = kmap_atomic(*readdir->pages); 329 330 if (cookie == 0) { 331 *p++ = xdr_one; /* next */ 332 *p++ = xdr_zero; /* cookie, first word */ 333 *p++ = xdr_one; /* cookie, second word */ 334 *p++ = xdr_one; /* entry len */ 335 memcpy(p, ".\0\0\0", 4); /* entry */ 336 p++; 337 *p++ = xdr_one; /* bitmap length */ 338 *p++ = htonl(attrs); /* bitmap */ 339 *p++ = htonl(12); /* attribute buffer length */ 340 *p++ = htonl(NF4DIR); 341 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry))); 342 } 343 344 *p++ = xdr_one; /* next */ 345 *p++ = xdr_zero; /* cookie, first word */ 346 *p++ = xdr_two; /* cookie, second word */ 347 *p++ = xdr_two; /* entry len */ 348 memcpy(p, "..\0\0", 4); /* entry */ 349 p++; 350 *p++ = xdr_one; /* bitmap length */ 351 *p++ = htonl(attrs); /* bitmap */ 352 *p++ = htonl(12); /* attribute buffer length */ 353 *p++ = htonl(NF4DIR); 354 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent))); 355 356 readdir->pgbase = (char *)p - (char *)start; 357 readdir->count -= readdir->pgbase; 358 kunmap_atomic(start); 359 } 360 361 static void nfs4_test_and_free_stateid(struct nfs_server *server, 362 nfs4_stateid *stateid, 363 const struct cred *cred) 364 { 365 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; 366 367 ops->test_and_free_expired(server, stateid, cred); 368 } 369 370 static void __nfs4_free_revoked_stateid(struct nfs_server *server, 371 nfs4_stateid *stateid, 372 const struct cred *cred) 373 { 374 stateid->type = NFS4_REVOKED_STATEID_TYPE; 375 nfs4_test_and_free_stateid(server, stateid, cred); 376 } 377 378 static void nfs4_free_revoked_stateid(struct nfs_server *server, 379 const nfs4_stateid *stateid, 380 const struct cred *cred) 381 { 382 nfs4_stateid tmp; 383 384 nfs4_stateid_copy(&tmp, stateid); 385 __nfs4_free_revoked_stateid(server, &tmp, cred); 386 } 387 388 static long nfs4_update_delay(long *timeout) 389 { 390 long ret; 391 if (!timeout) 392 return NFS4_POLL_RETRY_MAX; 393 if (*timeout <= 0) 394 *timeout = NFS4_POLL_RETRY_MIN; 395 if (*timeout > NFS4_POLL_RETRY_MAX) 396 *timeout = NFS4_POLL_RETRY_MAX; 397 ret = *timeout; 398 *timeout <<= 1; 399 return ret; 400 } 401 402 static int nfs4_delay_killable(long *timeout) 403 { 404 might_sleep(); 405 406 freezable_schedule_timeout_killable_unsafe( 407 nfs4_update_delay(timeout)); 408 if (!__fatal_signal_pending(current)) 409 return 0; 410 return -EINTR; 411 } 412 413 static int nfs4_delay_interruptible(long *timeout) 414 { 415 might_sleep(); 416 417 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout)); 418 if (!signal_pending(current)) 419 return 0; 420 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS; 421 } 422 423 static int nfs4_delay(long *timeout, bool interruptible) 424 { 425 if (interruptible) 426 return nfs4_delay_interruptible(timeout); 427 return nfs4_delay_killable(timeout); 428 } 429 430 static const nfs4_stateid * 431 nfs4_recoverable_stateid(const nfs4_stateid *stateid) 432 { 433 if (!stateid) 434 return NULL; 435 switch (stateid->type) { 436 case NFS4_OPEN_STATEID_TYPE: 437 case NFS4_LOCK_STATEID_TYPE: 438 case NFS4_DELEGATION_STATEID_TYPE: 439 return stateid; 440 default: 441 break; 442 } 443 return NULL; 444 } 445 446 /* This is the error handling routine for processes that are allowed 447 * to sleep. 448 */ 449 static int nfs4_do_handle_exception(struct nfs_server *server, 450 int errorcode, struct nfs4_exception *exception) 451 { 452 struct nfs_client *clp = server->nfs_client; 453 struct nfs4_state *state = exception->state; 454 const nfs4_stateid *stateid; 455 struct inode *inode = exception->inode; 456 int ret = errorcode; 457 458 exception->delay = 0; 459 exception->recovering = 0; 460 exception->retry = 0; 461 462 stateid = nfs4_recoverable_stateid(exception->stateid); 463 if (stateid == NULL && state != NULL) 464 stateid = nfs4_recoverable_stateid(&state->stateid); 465 466 switch(errorcode) { 467 case 0: 468 return 0; 469 case -NFS4ERR_BADHANDLE: 470 case -ESTALE: 471 if (inode != NULL && S_ISREG(inode->i_mode)) 472 pnfs_destroy_layout(NFS_I(inode)); 473 break; 474 case -NFS4ERR_DELEG_REVOKED: 475 case -NFS4ERR_ADMIN_REVOKED: 476 case -NFS4ERR_EXPIRED: 477 case -NFS4ERR_BAD_STATEID: 478 case -NFS4ERR_PARTNER_NO_AUTH: 479 if (inode != NULL && stateid != NULL) { 480 nfs_inode_find_state_and_recover(inode, 481 stateid); 482 goto wait_on_recovery; 483 } 484 /* Fall through */ 485 case -NFS4ERR_OPENMODE: 486 if (inode) { 487 int err; 488 489 err = nfs_async_inode_return_delegation(inode, 490 stateid); 491 if (err == 0) 492 goto wait_on_recovery; 493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) { 494 exception->retry = 1; 495 break; 496 } 497 } 498 if (state == NULL) 499 break; 500 ret = nfs4_schedule_stateid_recovery(server, state); 501 if (ret < 0) 502 break; 503 goto wait_on_recovery; 504 case -NFS4ERR_STALE_STATEID: 505 case -NFS4ERR_STALE_CLIENTID: 506 nfs4_schedule_lease_recovery(clp); 507 goto wait_on_recovery; 508 case -NFS4ERR_MOVED: 509 ret = nfs4_schedule_migration_recovery(server); 510 if (ret < 0) 511 break; 512 goto wait_on_recovery; 513 case -NFS4ERR_LEASE_MOVED: 514 nfs4_schedule_lease_moved_recovery(clp); 515 goto wait_on_recovery; 516 #if defined(CONFIG_NFS_V4_1) 517 case -NFS4ERR_BADSESSION: 518 case -NFS4ERR_BADSLOT: 519 case -NFS4ERR_BAD_HIGH_SLOT: 520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 521 case -NFS4ERR_DEADSESSION: 522 case -NFS4ERR_SEQ_FALSE_RETRY: 523 case -NFS4ERR_SEQ_MISORDERED: 524 /* Handled in nfs41_sequence_process() */ 525 goto wait_on_recovery; 526 #endif /* defined(CONFIG_NFS_V4_1) */ 527 case -NFS4ERR_FILE_OPEN: 528 if (exception->timeout > HZ) { 529 /* We have retried a decent amount, time to 530 * fail 531 */ 532 ret = -EBUSY; 533 break; 534 } 535 /* Fall through */ 536 case -NFS4ERR_DELAY: 537 nfs_inc_server_stats(server, NFSIOS_DELAY); 538 /* Fall through */ 539 case -NFS4ERR_GRACE: 540 case -NFS4ERR_LAYOUTTRYLATER: 541 case -NFS4ERR_RECALLCONFLICT: 542 exception->delay = 1; 543 return 0; 544 545 case -NFS4ERR_RETRY_UNCACHED_REP: 546 case -NFS4ERR_OLD_STATEID: 547 exception->retry = 1; 548 break; 549 case -NFS4ERR_BADOWNER: 550 /* The following works around a Linux server bug! */ 551 case -NFS4ERR_BADNAME: 552 if (server->caps & NFS_CAP_UIDGID_NOMAP) { 553 server->caps &= ~NFS_CAP_UIDGID_NOMAP; 554 exception->retry = 1; 555 printk(KERN_WARNING "NFS: v4 server %s " 556 "does not accept raw " 557 "uid/gids. " 558 "Reenabling the idmapper.\n", 559 server->nfs_client->cl_hostname); 560 } 561 } 562 /* We failed to handle the error */ 563 return nfs4_map_errors(ret); 564 wait_on_recovery: 565 exception->recovering = 1; 566 return 0; 567 } 568 569 /* This is the error handling routine for processes that are allowed 570 * to sleep. 571 */ 572 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) 573 { 574 struct nfs_client *clp = server->nfs_client; 575 int ret; 576 577 ret = nfs4_do_handle_exception(server, errorcode, exception); 578 if (exception->delay) { 579 ret = nfs4_delay(&exception->timeout, 580 exception->interruptible); 581 goto out_retry; 582 } 583 if (exception->recovering) { 584 ret = nfs4_wait_clnt_recover(clp); 585 if (test_bit(NFS_MIG_FAILED, &server->mig_status)) 586 return -EIO; 587 goto out_retry; 588 } 589 return ret; 590 out_retry: 591 if (ret == 0) 592 exception->retry = 1; 593 return ret; 594 } 595 596 static int 597 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server, 598 int errorcode, struct nfs4_exception *exception) 599 { 600 struct nfs_client *clp = server->nfs_client; 601 int ret; 602 603 ret = nfs4_do_handle_exception(server, errorcode, exception); 604 if (exception->delay) { 605 rpc_delay(task, nfs4_update_delay(&exception->timeout)); 606 goto out_retry; 607 } 608 if (exception->recovering) { 609 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); 610 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) 611 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); 612 goto out_retry; 613 } 614 if (test_bit(NFS_MIG_FAILED, &server->mig_status)) 615 ret = -EIO; 616 return ret; 617 out_retry: 618 if (ret == 0) { 619 exception->retry = 1; 620 /* 621 * For NFS4ERR_MOVED, the client transport will need to 622 * be recomputed after migration recovery has completed. 623 */ 624 if (errorcode == -NFS4ERR_MOVED) 625 rpc_task_release_transport(task); 626 } 627 return ret; 628 } 629 630 int 631 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server, 632 struct nfs4_state *state, long *timeout) 633 { 634 struct nfs4_exception exception = { 635 .state = state, 636 }; 637 638 if (task->tk_status >= 0) 639 return 0; 640 if (timeout) 641 exception.timeout = *timeout; 642 task->tk_status = nfs4_async_handle_exception(task, server, 643 task->tk_status, 644 &exception); 645 if (exception.delay && timeout) 646 *timeout = exception.timeout; 647 if (exception.retry) 648 return -EAGAIN; 649 return 0; 650 } 651 652 /* 653 * Return 'true' if 'clp' is using an rpc_client that is integrity protected 654 * or 'false' otherwise. 655 */ 656 static bool _nfs4_is_integrity_protected(struct nfs_client *clp) 657 { 658 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor; 659 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P); 660 } 661 662 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) 663 { 664 spin_lock(&clp->cl_lock); 665 if (time_before(clp->cl_last_renewal,timestamp)) 666 clp->cl_last_renewal = timestamp; 667 spin_unlock(&clp->cl_lock); 668 } 669 670 static void renew_lease(const struct nfs_server *server, unsigned long timestamp) 671 { 672 struct nfs_client *clp = server->nfs_client; 673 674 if (!nfs4_has_session(clp)) 675 do_renew_lease(clp, timestamp); 676 } 677 678 struct nfs4_call_sync_data { 679 const struct nfs_server *seq_server; 680 struct nfs4_sequence_args *seq_args; 681 struct nfs4_sequence_res *seq_res; 682 }; 683 684 void nfs4_init_sequence(struct nfs4_sequence_args *args, 685 struct nfs4_sequence_res *res, int cache_reply, 686 int privileged) 687 { 688 args->sa_slot = NULL; 689 args->sa_cache_this = cache_reply; 690 args->sa_privileged = privileged; 691 692 res->sr_slot = NULL; 693 } 694 695 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res) 696 { 697 struct nfs4_slot *slot = res->sr_slot; 698 struct nfs4_slot_table *tbl; 699 700 tbl = slot->table; 701 spin_lock(&tbl->slot_tbl_lock); 702 if (!nfs41_wake_and_assign_slot(tbl, slot)) 703 nfs4_free_slot(tbl, slot); 704 spin_unlock(&tbl->slot_tbl_lock); 705 706 res->sr_slot = NULL; 707 } 708 709 static int nfs40_sequence_done(struct rpc_task *task, 710 struct nfs4_sequence_res *res) 711 { 712 if (res->sr_slot != NULL) 713 nfs40_sequence_free_slot(res); 714 return 1; 715 } 716 717 #if defined(CONFIG_NFS_V4_1) 718 719 static void nfs41_release_slot(struct nfs4_slot *slot) 720 { 721 struct nfs4_session *session; 722 struct nfs4_slot_table *tbl; 723 bool send_new_highest_used_slotid = false; 724 725 if (!slot) 726 return; 727 tbl = slot->table; 728 session = tbl->session; 729 730 /* Bump the slot sequence number */ 731 if (slot->seq_done) 732 slot->seq_nr++; 733 slot->seq_done = 0; 734 735 spin_lock(&tbl->slot_tbl_lock); 736 /* Be nice to the server: try to ensure that the last transmitted 737 * value for highest_user_slotid <= target_highest_slotid 738 */ 739 if (tbl->highest_used_slotid > tbl->target_highest_slotid) 740 send_new_highest_used_slotid = true; 741 742 if (nfs41_wake_and_assign_slot(tbl, slot)) { 743 send_new_highest_used_slotid = false; 744 goto out_unlock; 745 } 746 nfs4_free_slot(tbl, slot); 747 748 if (tbl->highest_used_slotid != NFS4_NO_SLOT) 749 send_new_highest_used_slotid = false; 750 out_unlock: 751 spin_unlock(&tbl->slot_tbl_lock); 752 if (send_new_highest_used_slotid) 753 nfs41_notify_server(session->clp); 754 if (waitqueue_active(&tbl->slot_waitq)) 755 wake_up_all(&tbl->slot_waitq); 756 } 757 758 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) 759 { 760 nfs41_release_slot(res->sr_slot); 761 res->sr_slot = NULL; 762 } 763 764 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot, 765 u32 seqnr) 766 { 767 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0) 768 slot->seq_nr_highest_sent = seqnr; 769 } 770 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, 771 u32 seqnr) 772 { 773 slot->seq_nr_highest_sent = seqnr; 774 slot->seq_nr_last_acked = seqnr; 775 } 776 777 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred, 778 struct nfs4_slot *slot) 779 { 780 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true); 781 if (!IS_ERR(task)) 782 rpc_put_task_async(task); 783 } 784 785 static int nfs41_sequence_process(struct rpc_task *task, 786 struct nfs4_sequence_res *res) 787 { 788 struct nfs4_session *session; 789 struct nfs4_slot *slot = res->sr_slot; 790 struct nfs_client *clp; 791 int status; 792 int ret = 1; 793 794 if (slot == NULL) 795 goto out_noaction; 796 /* don't increment the sequence number if the task wasn't sent */ 797 if (!RPC_WAS_SENT(task) || slot->seq_done) 798 goto out; 799 800 session = slot->table->session; 801 clp = session->clp; 802 803 trace_nfs4_sequence_done(session, res); 804 805 status = res->sr_status; 806 if (task->tk_status == -NFS4ERR_DEADSESSION) 807 status = -NFS4ERR_DEADSESSION; 808 809 /* Check the SEQUENCE operation status */ 810 switch (status) { 811 case 0: 812 /* Mark this sequence number as having been acked */ 813 nfs4_slot_sequence_acked(slot, slot->seq_nr); 814 /* Update the slot's sequence and clientid lease timer */ 815 slot->seq_done = 1; 816 do_renew_lease(clp, res->sr_timestamp); 817 /* Check sequence flags */ 818 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags, 819 !!slot->privileged); 820 nfs41_update_target_slotid(slot->table, slot, res); 821 break; 822 case 1: 823 /* 824 * sr_status remains 1 if an RPC level error occurred. 825 * The server may or may not have processed the sequence 826 * operation.. 827 */ 828 nfs4_slot_sequence_record_sent(slot, slot->seq_nr); 829 slot->seq_done = 1; 830 goto out; 831 case -NFS4ERR_DELAY: 832 /* The server detected a resend of the RPC call and 833 * returned NFS4ERR_DELAY as per Section 2.10.6.2 834 * of RFC5661. 835 */ 836 dprintk("%s: slot=%u seq=%u: Operation in progress\n", 837 __func__, 838 slot->slot_nr, 839 slot->seq_nr); 840 nfs4_slot_sequence_acked(slot, slot->seq_nr); 841 goto out_retry; 842 case -NFS4ERR_RETRY_UNCACHED_REP: 843 case -NFS4ERR_SEQ_FALSE_RETRY: 844 /* 845 * The server thinks we tried to replay a request. 846 * Retry the call after bumping the sequence ID. 847 */ 848 nfs4_slot_sequence_acked(slot, slot->seq_nr); 849 goto retry_new_seq; 850 case -NFS4ERR_BADSLOT: 851 /* 852 * The slot id we used was probably retired. Try again 853 * using a different slot id. 854 */ 855 if (slot->slot_nr < slot->table->target_highest_slotid) 856 goto session_recover; 857 goto retry_nowait; 858 case -NFS4ERR_SEQ_MISORDERED: 859 nfs4_slot_sequence_record_sent(slot, slot->seq_nr); 860 /* 861 * Were one or more calls using this slot interrupted? 862 * If the server never received the request, then our 863 * transmitted slot sequence number may be too high. However, 864 * if the server did receive the request then it might 865 * accidentally give us a reply with a mismatched operation. 866 * We can sort this out by sending a lone sequence operation 867 * to the server on the same slot. 868 */ 869 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) { 870 slot->seq_nr--; 871 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) { 872 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot); 873 res->sr_slot = NULL; 874 } 875 goto retry_nowait; 876 } 877 /* 878 * RFC5661: 879 * A retry might be sent while the original request is 880 * still in progress on the replier. The replier SHOULD 881 * deal with the issue by returning NFS4ERR_DELAY as the 882 * reply to SEQUENCE or CB_SEQUENCE operation, but 883 * implementations MAY return NFS4ERR_SEQ_MISORDERED. 884 * 885 * Restart the search after a delay. 886 */ 887 slot->seq_nr = slot->seq_nr_highest_sent; 888 goto out_retry; 889 case -NFS4ERR_BADSESSION: 890 case -NFS4ERR_DEADSESSION: 891 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 892 goto session_recover; 893 default: 894 /* Just update the slot sequence no. */ 895 slot->seq_done = 1; 896 } 897 out: 898 /* The session may be reset by one of the error handlers. */ 899 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); 900 out_noaction: 901 return ret; 902 session_recover: 903 nfs4_schedule_session_recovery(session, status); 904 dprintk("%s ERROR: %d Reset session\n", __func__, status); 905 nfs41_sequence_free_slot(res); 906 goto out; 907 retry_new_seq: 908 ++slot->seq_nr; 909 retry_nowait: 910 if (rpc_restart_call_prepare(task)) { 911 nfs41_sequence_free_slot(res); 912 task->tk_status = 0; 913 ret = 0; 914 } 915 goto out; 916 out_retry: 917 if (!rpc_restart_call(task)) 918 goto out; 919 rpc_delay(task, NFS4_POLL_RETRY_MAX); 920 return 0; 921 } 922 923 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) 924 { 925 if (!nfs41_sequence_process(task, res)) 926 return 0; 927 if (res->sr_slot != NULL) 928 nfs41_sequence_free_slot(res); 929 return 1; 930 931 } 932 EXPORT_SYMBOL_GPL(nfs41_sequence_done); 933 934 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) 935 { 936 if (res->sr_slot == NULL) 937 return 1; 938 if (res->sr_slot->table->session != NULL) 939 return nfs41_sequence_process(task, res); 940 return nfs40_sequence_done(task, res); 941 } 942 943 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) 944 { 945 if (res->sr_slot != NULL) { 946 if (res->sr_slot->table->session != NULL) 947 nfs41_sequence_free_slot(res); 948 else 949 nfs40_sequence_free_slot(res); 950 } 951 } 952 953 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) 954 { 955 if (res->sr_slot == NULL) 956 return 1; 957 if (!res->sr_slot->table->session) 958 return nfs40_sequence_done(task, res); 959 return nfs41_sequence_done(task, res); 960 } 961 EXPORT_SYMBOL_GPL(nfs4_sequence_done); 962 963 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) 964 { 965 struct nfs4_call_sync_data *data = calldata; 966 967 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server); 968 969 nfs4_setup_sequence(data->seq_server->nfs_client, 970 data->seq_args, data->seq_res, task); 971 } 972 973 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) 974 { 975 struct nfs4_call_sync_data *data = calldata; 976 977 nfs41_sequence_done(task, data->seq_res); 978 } 979 980 static const struct rpc_call_ops nfs41_call_sync_ops = { 981 .rpc_call_prepare = nfs41_call_sync_prepare, 982 .rpc_call_done = nfs41_call_sync_done, 983 }; 984 985 #else /* !CONFIG_NFS_V4_1 */ 986 987 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) 988 { 989 return nfs40_sequence_done(task, res); 990 } 991 992 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) 993 { 994 if (res->sr_slot != NULL) 995 nfs40_sequence_free_slot(res); 996 } 997 998 int nfs4_sequence_done(struct rpc_task *task, 999 struct nfs4_sequence_res *res) 1000 { 1001 return nfs40_sequence_done(task, res); 1002 } 1003 EXPORT_SYMBOL_GPL(nfs4_sequence_done); 1004 1005 #endif /* !CONFIG_NFS_V4_1 */ 1006 1007 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res) 1008 { 1009 res->sr_timestamp = jiffies; 1010 res->sr_status_flags = 0; 1011 res->sr_status = 1; 1012 } 1013 1014 static 1015 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args, 1016 struct nfs4_sequence_res *res, 1017 struct nfs4_slot *slot) 1018 { 1019 if (!slot) 1020 return; 1021 slot->privileged = args->sa_privileged ? 1 : 0; 1022 args->sa_slot = slot; 1023 1024 res->sr_slot = slot; 1025 } 1026 1027 int nfs4_setup_sequence(struct nfs_client *client, 1028 struct nfs4_sequence_args *args, 1029 struct nfs4_sequence_res *res, 1030 struct rpc_task *task) 1031 { 1032 struct nfs4_session *session = nfs4_get_session(client); 1033 struct nfs4_slot_table *tbl = client->cl_slot_tbl; 1034 struct nfs4_slot *slot; 1035 1036 /* slot already allocated? */ 1037 if (res->sr_slot != NULL) 1038 goto out_start; 1039 1040 if (session) 1041 tbl = &session->fc_slot_table; 1042 1043 spin_lock(&tbl->slot_tbl_lock); 1044 /* The state manager will wait until the slot table is empty */ 1045 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) 1046 goto out_sleep; 1047 1048 slot = nfs4_alloc_slot(tbl); 1049 if (IS_ERR(slot)) { 1050 if (slot == ERR_PTR(-ENOMEM)) 1051 goto out_sleep_timeout; 1052 goto out_sleep; 1053 } 1054 spin_unlock(&tbl->slot_tbl_lock); 1055 1056 nfs4_sequence_attach_slot(args, res, slot); 1057 1058 trace_nfs4_setup_sequence(session, args); 1059 out_start: 1060 nfs41_sequence_res_init(res); 1061 rpc_call_start(task); 1062 return 0; 1063 out_sleep_timeout: 1064 /* Try again in 1/4 second */ 1065 if (args->sa_privileged) 1066 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task, 1067 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED); 1068 else 1069 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task, 1070 NULL, jiffies + (HZ >> 2)); 1071 spin_unlock(&tbl->slot_tbl_lock); 1072 return -EAGAIN; 1073 out_sleep: 1074 if (args->sa_privileged) 1075 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, 1076 RPC_PRIORITY_PRIVILEGED); 1077 else 1078 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); 1079 spin_unlock(&tbl->slot_tbl_lock); 1080 return -EAGAIN; 1081 } 1082 EXPORT_SYMBOL_GPL(nfs4_setup_sequence); 1083 1084 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata) 1085 { 1086 struct nfs4_call_sync_data *data = calldata; 1087 nfs4_setup_sequence(data->seq_server->nfs_client, 1088 data->seq_args, data->seq_res, task); 1089 } 1090 1091 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata) 1092 { 1093 struct nfs4_call_sync_data *data = calldata; 1094 nfs4_sequence_done(task, data->seq_res); 1095 } 1096 1097 static const struct rpc_call_ops nfs40_call_sync_ops = { 1098 .rpc_call_prepare = nfs40_call_sync_prepare, 1099 .rpc_call_done = nfs40_call_sync_done, 1100 }; 1101 1102 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup) 1103 { 1104 int ret; 1105 struct rpc_task *task; 1106 1107 task = rpc_run_task(task_setup); 1108 if (IS_ERR(task)) 1109 return PTR_ERR(task); 1110 1111 ret = task->tk_status; 1112 rpc_put_task(task); 1113 return ret; 1114 } 1115 1116 static int nfs4_do_call_sync(struct rpc_clnt *clnt, 1117 struct nfs_server *server, 1118 struct rpc_message *msg, 1119 struct nfs4_sequence_args *args, 1120 struct nfs4_sequence_res *res, 1121 unsigned short task_flags) 1122 { 1123 struct nfs_client *clp = server->nfs_client; 1124 struct nfs4_call_sync_data data = { 1125 .seq_server = server, 1126 .seq_args = args, 1127 .seq_res = res, 1128 }; 1129 struct rpc_task_setup task_setup = { 1130 .rpc_client = clnt, 1131 .rpc_message = msg, 1132 .callback_ops = clp->cl_mvops->call_sync_ops, 1133 .callback_data = &data, 1134 .flags = task_flags, 1135 }; 1136 1137 return nfs4_call_sync_custom(&task_setup); 1138 } 1139 1140 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, 1141 struct nfs_server *server, 1142 struct rpc_message *msg, 1143 struct nfs4_sequence_args *args, 1144 struct nfs4_sequence_res *res) 1145 { 1146 return nfs4_do_call_sync(clnt, server, msg, args, res, 0); 1147 } 1148 1149 1150 int nfs4_call_sync(struct rpc_clnt *clnt, 1151 struct nfs_server *server, 1152 struct rpc_message *msg, 1153 struct nfs4_sequence_args *args, 1154 struct nfs4_sequence_res *res, 1155 int cache_reply) 1156 { 1157 nfs4_init_sequence(args, res, cache_reply, 0); 1158 return nfs4_call_sync_sequence(clnt, server, msg, args, res); 1159 } 1160 1161 static void 1162 nfs4_inc_nlink_locked(struct inode *inode) 1163 { 1164 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER; 1165 inc_nlink(inode); 1166 } 1167 1168 static void 1169 nfs4_dec_nlink_locked(struct inode *inode) 1170 { 1171 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER; 1172 drop_nlink(inode); 1173 } 1174 1175 static void 1176 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo, 1177 unsigned long timestamp, unsigned long cache_validity) 1178 { 1179 struct nfs_inode *nfsi = NFS_I(dir); 1180 1181 nfsi->cache_validity |= NFS_INO_INVALID_CTIME 1182 | NFS_INO_INVALID_MTIME 1183 | NFS_INO_INVALID_DATA 1184 | cache_validity; 1185 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) { 1186 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE; 1187 nfsi->attrtimeo_timestamp = jiffies; 1188 } else { 1189 nfs_force_lookup_revalidate(dir); 1190 if (cinfo->before != inode_peek_iversion_raw(dir)) 1191 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS | 1192 NFS_INO_INVALID_ACL; 1193 } 1194 inode_set_iversion_raw(dir, cinfo->after); 1195 nfsi->read_cache_jiffies = timestamp; 1196 nfsi->attr_gencount = nfs_inc_attr_generation_counter(); 1197 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE; 1198 nfs_fscache_invalidate(dir); 1199 } 1200 1201 static void 1202 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo, 1203 unsigned long timestamp, unsigned long cache_validity) 1204 { 1205 spin_lock(&dir->i_lock); 1206 update_changeattr_locked(dir, cinfo, timestamp, cache_validity); 1207 spin_unlock(&dir->i_lock); 1208 } 1209 1210 struct nfs4_open_createattrs { 1211 struct nfs4_label *label; 1212 struct iattr *sattr; 1213 const __u32 verf[2]; 1214 }; 1215 1216 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, 1217 int err, struct nfs4_exception *exception) 1218 { 1219 if (err != -EINVAL) 1220 return false; 1221 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) 1222 return false; 1223 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; 1224 exception->retry = 1; 1225 return true; 1226 } 1227 1228 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx) 1229 { 1230 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC); 1231 } 1232 1233 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx) 1234 { 1235 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE); 1236 1237 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret; 1238 } 1239 1240 static u32 1241 nfs4_map_atomic_open_share(struct nfs_server *server, 1242 fmode_t fmode, int openflags) 1243 { 1244 u32 res = 0; 1245 1246 switch (fmode & (FMODE_READ | FMODE_WRITE)) { 1247 case FMODE_READ: 1248 res = NFS4_SHARE_ACCESS_READ; 1249 break; 1250 case FMODE_WRITE: 1251 res = NFS4_SHARE_ACCESS_WRITE; 1252 break; 1253 case FMODE_READ|FMODE_WRITE: 1254 res = NFS4_SHARE_ACCESS_BOTH; 1255 } 1256 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) 1257 goto out; 1258 /* Want no delegation if we're using O_DIRECT */ 1259 if (openflags & O_DIRECT) 1260 res |= NFS4_SHARE_WANT_NO_DELEG; 1261 out: 1262 return res; 1263 } 1264 1265 static enum open_claim_type4 1266 nfs4_map_atomic_open_claim(struct nfs_server *server, 1267 enum open_claim_type4 claim) 1268 { 1269 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) 1270 return claim; 1271 switch (claim) { 1272 default: 1273 return claim; 1274 case NFS4_OPEN_CLAIM_FH: 1275 return NFS4_OPEN_CLAIM_NULL; 1276 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1277 return NFS4_OPEN_CLAIM_DELEGATE_CUR; 1278 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 1279 return NFS4_OPEN_CLAIM_DELEGATE_PREV; 1280 } 1281 } 1282 1283 static void nfs4_init_opendata_res(struct nfs4_opendata *p) 1284 { 1285 p->o_res.f_attr = &p->f_attr; 1286 p->o_res.f_label = p->f_label; 1287 p->o_res.seqid = p->o_arg.seqid; 1288 p->c_res.seqid = p->c_arg.seqid; 1289 p->o_res.server = p->o_arg.server; 1290 p->o_res.access_request = p->o_arg.access; 1291 nfs_fattr_init(&p->f_attr); 1292 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); 1293 } 1294 1295 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, 1296 struct nfs4_state_owner *sp, fmode_t fmode, int flags, 1297 const struct nfs4_open_createattrs *c, 1298 enum open_claim_type4 claim, 1299 gfp_t gfp_mask) 1300 { 1301 struct dentry *parent = dget_parent(dentry); 1302 struct inode *dir = d_inode(parent); 1303 struct nfs_server *server = NFS_SERVER(dir); 1304 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 1305 struct nfs4_label *label = (c != NULL) ? c->label : NULL; 1306 struct nfs4_opendata *p; 1307 1308 p = kzalloc(sizeof(*p), gfp_mask); 1309 if (p == NULL) 1310 goto err; 1311 1312 p->f_label = nfs4_label_alloc(server, gfp_mask); 1313 if (IS_ERR(p->f_label)) 1314 goto err_free_p; 1315 1316 p->a_label = nfs4_label_alloc(server, gfp_mask); 1317 if (IS_ERR(p->a_label)) 1318 goto err_free_f; 1319 1320 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 1321 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask); 1322 if (IS_ERR(p->o_arg.seqid)) 1323 goto err_free_label; 1324 nfs_sb_active(dentry->d_sb); 1325 p->dentry = dget(dentry); 1326 p->dir = parent; 1327 p->owner = sp; 1328 atomic_inc(&sp->so_count); 1329 p->o_arg.open_flags = flags; 1330 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); 1331 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); 1332 p->o_arg.share_access = nfs4_map_atomic_open_share(server, 1333 fmode, flags); 1334 if (flags & O_CREAT) { 1335 p->o_arg.umask = current_umask(); 1336 p->o_arg.label = nfs4_label_copy(p->a_label, label); 1337 if (c->sattr != NULL && c->sattr->ia_valid != 0) { 1338 p->o_arg.u.attrs = &p->attrs; 1339 memcpy(&p->attrs, c->sattr, sizeof(p->attrs)); 1340 1341 memcpy(p->o_arg.u.verifier.data, c->verf, 1342 sizeof(p->o_arg.u.verifier.data)); 1343 } 1344 } 1345 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS 1346 * will return permission denied for all bits until close */ 1347 if (!(flags & O_EXCL)) { 1348 /* ask server to check for all possible rights as results 1349 * are cached */ 1350 switch (p->o_arg.claim) { 1351 default: 1352 break; 1353 case NFS4_OPEN_CLAIM_NULL: 1354 case NFS4_OPEN_CLAIM_FH: 1355 p->o_arg.access = NFS4_ACCESS_READ | 1356 NFS4_ACCESS_MODIFY | 1357 NFS4_ACCESS_EXTEND | 1358 NFS4_ACCESS_EXECUTE; 1359 } 1360 } 1361 p->o_arg.clientid = server->nfs_client->cl_clientid; 1362 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); 1363 p->o_arg.id.uniquifier = sp->so_seqid.owner_id; 1364 p->o_arg.name = &dentry->d_name; 1365 p->o_arg.server = server; 1366 p->o_arg.bitmask = nfs4_bitmask(server, label); 1367 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; 1368 switch (p->o_arg.claim) { 1369 case NFS4_OPEN_CLAIM_NULL: 1370 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 1371 case NFS4_OPEN_CLAIM_DELEGATE_PREV: 1372 p->o_arg.fh = NFS_FH(dir); 1373 break; 1374 case NFS4_OPEN_CLAIM_PREVIOUS: 1375 case NFS4_OPEN_CLAIM_FH: 1376 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1377 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 1378 p->o_arg.fh = NFS_FH(d_inode(dentry)); 1379 } 1380 p->c_arg.fh = &p->o_res.fh; 1381 p->c_arg.stateid = &p->o_res.stateid; 1382 p->c_arg.seqid = p->o_arg.seqid; 1383 nfs4_init_opendata_res(p); 1384 kref_init(&p->kref); 1385 return p; 1386 1387 err_free_label: 1388 nfs4_label_free(p->a_label); 1389 err_free_f: 1390 nfs4_label_free(p->f_label); 1391 err_free_p: 1392 kfree(p); 1393 err: 1394 dput(parent); 1395 return NULL; 1396 } 1397 1398 static void nfs4_opendata_free(struct kref *kref) 1399 { 1400 struct nfs4_opendata *p = container_of(kref, 1401 struct nfs4_opendata, kref); 1402 struct super_block *sb = p->dentry->d_sb; 1403 1404 nfs4_lgopen_release(p->lgp); 1405 nfs_free_seqid(p->o_arg.seqid); 1406 nfs4_sequence_free_slot(&p->o_res.seq_res); 1407 if (p->state != NULL) 1408 nfs4_put_open_state(p->state); 1409 nfs4_put_state_owner(p->owner); 1410 1411 nfs4_label_free(p->a_label); 1412 nfs4_label_free(p->f_label); 1413 1414 dput(p->dir); 1415 dput(p->dentry); 1416 nfs_sb_deactive(sb); 1417 nfs_fattr_free_names(&p->f_attr); 1418 kfree(p->f_attr.mdsthreshold); 1419 kfree(p); 1420 } 1421 1422 static void nfs4_opendata_put(struct nfs4_opendata *p) 1423 { 1424 if (p != NULL) 1425 kref_put(&p->kref, nfs4_opendata_free); 1426 } 1427 1428 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state, 1429 fmode_t fmode) 1430 { 1431 switch(fmode & (FMODE_READ|FMODE_WRITE)) { 1432 case FMODE_READ|FMODE_WRITE: 1433 return state->n_rdwr != 0; 1434 case FMODE_WRITE: 1435 return state->n_wronly != 0; 1436 case FMODE_READ: 1437 return state->n_rdonly != 0; 1438 } 1439 WARN_ON_ONCE(1); 1440 return false; 1441 } 1442 1443 static int can_open_cached(struct nfs4_state *state, fmode_t mode, 1444 int open_mode, enum open_claim_type4 claim) 1445 { 1446 int ret = 0; 1447 1448 if (open_mode & (O_EXCL|O_TRUNC)) 1449 goto out; 1450 switch (claim) { 1451 case NFS4_OPEN_CLAIM_NULL: 1452 case NFS4_OPEN_CLAIM_FH: 1453 goto out; 1454 default: 1455 break; 1456 } 1457 switch (mode & (FMODE_READ|FMODE_WRITE)) { 1458 case FMODE_READ: 1459 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 1460 && state->n_rdonly != 0; 1461 break; 1462 case FMODE_WRITE: 1463 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 1464 && state->n_wronly != 0; 1465 break; 1466 case FMODE_READ|FMODE_WRITE: 1467 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 1468 && state->n_rdwr != 0; 1469 } 1470 out: 1471 return ret; 1472 } 1473 1474 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode, 1475 enum open_claim_type4 claim) 1476 { 1477 if (delegation == NULL) 1478 return 0; 1479 if ((delegation->type & fmode) != fmode) 1480 return 0; 1481 switch (claim) { 1482 case NFS4_OPEN_CLAIM_NULL: 1483 case NFS4_OPEN_CLAIM_FH: 1484 break; 1485 case NFS4_OPEN_CLAIM_PREVIOUS: 1486 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) 1487 break; 1488 /* Fall through */ 1489 default: 1490 return 0; 1491 } 1492 nfs_mark_delegation_referenced(delegation); 1493 return 1; 1494 } 1495 1496 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) 1497 { 1498 switch (fmode) { 1499 case FMODE_WRITE: 1500 state->n_wronly++; 1501 break; 1502 case FMODE_READ: 1503 state->n_rdonly++; 1504 break; 1505 case FMODE_READ|FMODE_WRITE: 1506 state->n_rdwr++; 1507 } 1508 nfs4_state_set_mode_locked(state, state->state | fmode); 1509 } 1510 1511 #ifdef CONFIG_NFS_V4_1 1512 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state) 1513 { 1514 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags)) 1515 return true; 1516 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags)) 1517 return true; 1518 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags)) 1519 return true; 1520 return false; 1521 } 1522 #endif /* CONFIG_NFS_V4_1 */ 1523 1524 static void nfs_state_log_update_open_stateid(struct nfs4_state *state) 1525 { 1526 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags)) 1527 wake_up_all(&state->waitq); 1528 } 1529 1530 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state, 1531 const nfs4_stateid *stateid) 1532 { 1533 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid); 1534 u32 stateid_seqid = be32_to_cpu(stateid->seqid); 1535 1536 if (stateid_seqid == state_seqid + 1U || 1537 (stateid_seqid == 1U && state_seqid == 0xffffffffU)) 1538 nfs_state_log_update_open_stateid(state); 1539 else 1540 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags); 1541 } 1542 1543 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state) 1544 { 1545 struct nfs_client *clp = state->owner->so_server->nfs_client; 1546 bool need_recover = false; 1547 1548 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly) 1549 need_recover = true; 1550 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly) 1551 need_recover = true; 1552 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr) 1553 need_recover = true; 1554 if (need_recover) 1555 nfs4_state_mark_reclaim_nograce(clp, state); 1556 } 1557 1558 /* 1559 * Check for whether or not the caller may update the open stateid 1560 * to the value passed in by stateid. 1561 * 1562 * Note: This function relies heavily on the server implementing 1563 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2 1564 * correctly. 1565 * i.e. The stateid seqids have to be initialised to 1, and 1566 * are then incremented on every state transition. 1567 */ 1568 static bool nfs_need_update_open_stateid(struct nfs4_state *state, 1569 const nfs4_stateid *stateid) 1570 { 1571 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 || 1572 !nfs4_stateid_match_other(stateid, &state->open_stateid)) { 1573 if (stateid->seqid == cpu_to_be32(1)) 1574 nfs_state_log_update_open_stateid(state); 1575 else 1576 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags); 1577 return true; 1578 } 1579 1580 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) { 1581 nfs_state_log_out_of_order_open_stateid(state, stateid); 1582 return true; 1583 } 1584 return false; 1585 } 1586 1587 static void nfs_resync_open_stateid_locked(struct nfs4_state *state) 1588 { 1589 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr)) 1590 return; 1591 if (state->n_wronly) 1592 set_bit(NFS_O_WRONLY_STATE, &state->flags); 1593 if (state->n_rdonly) 1594 set_bit(NFS_O_RDONLY_STATE, &state->flags); 1595 if (state->n_rdwr) 1596 set_bit(NFS_O_RDWR_STATE, &state->flags); 1597 set_bit(NFS_OPEN_STATE, &state->flags); 1598 } 1599 1600 static void nfs_clear_open_stateid_locked(struct nfs4_state *state, 1601 nfs4_stateid *stateid, fmode_t fmode) 1602 { 1603 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1604 switch (fmode & (FMODE_READ|FMODE_WRITE)) { 1605 case FMODE_WRITE: 1606 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1607 break; 1608 case FMODE_READ: 1609 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1610 break; 1611 case 0: 1612 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1613 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1614 clear_bit(NFS_OPEN_STATE, &state->flags); 1615 } 1616 if (stateid == NULL) 1617 return; 1618 /* Handle OPEN+OPEN_DOWNGRADE races */ 1619 if (nfs4_stateid_match_other(stateid, &state->open_stateid) && 1620 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) { 1621 nfs_resync_open_stateid_locked(state); 1622 goto out; 1623 } 1624 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1625 nfs4_stateid_copy(&state->stateid, stateid); 1626 nfs4_stateid_copy(&state->open_stateid, stateid); 1627 trace_nfs4_open_stateid_update(state->inode, stateid, 0); 1628 out: 1629 nfs_state_log_update_open_stateid(state); 1630 } 1631 1632 static void nfs_clear_open_stateid(struct nfs4_state *state, 1633 nfs4_stateid *arg_stateid, 1634 nfs4_stateid *stateid, fmode_t fmode) 1635 { 1636 write_seqlock(&state->seqlock); 1637 /* Ignore, if the CLOSE argment doesn't match the current stateid */ 1638 if (nfs4_state_match_open_stateid_other(state, arg_stateid)) 1639 nfs_clear_open_stateid_locked(state, stateid, fmode); 1640 write_sequnlock(&state->seqlock); 1641 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) 1642 nfs4_schedule_state_manager(state->owner->so_server->nfs_client); 1643 } 1644 1645 static void nfs_set_open_stateid_locked(struct nfs4_state *state, 1646 const nfs4_stateid *stateid, nfs4_stateid *freeme) 1647 __must_hold(&state->owner->so_lock) 1648 __must_hold(&state->seqlock) 1649 __must_hold(RCU) 1650 1651 { 1652 DEFINE_WAIT(wait); 1653 int status = 0; 1654 for (;;) { 1655 1656 if (!nfs_need_update_open_stateid(state, stateid)) 1657 return; 1658 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags)) 1659 break; 1660 if (status) 1661 break; 1662 /* Rely on seqids for serialisation with NFSv4.0 */ 1663 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client)) 1664 break; 1665 1666 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE); 1667 /* 1668 * Ensure we process the state changes in the same order 1669 * in which the server processed them by delaying the 1670 * update of the stateid until we are in sequence. 1671 */ 1672 write_sequnlock(&state->seqlock); 1673 spin_unlock(&state->owner->so_lock); 1674 rcu_read_unlock(); 1675 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0); 1676 if (!signal_pending(current)) { 1677 if (schedule_timeout(5*HZ) == 0) 1678 status = -EAGAIN; 1679 else 1680 status = 0; 1681 } else 1682 status = -EINTR; 1683 finish_wait(&state->waitq, &wait); 1684 rcu_read_lock(); 1685 spin_lock(&state->owner->so_lock); 1686 write_seqlock(&state->seqlock); 1687 } 1688 1689 if (test_bit(NFS_OPEN_STATE, &state->flags) && 1690 !nfs4_stateid_match_other(stateid, &state->open_stateid)) { 1691 nfs4_stateid_copy(freeme, &state->open_stateid); 1692 nfs_test_and_clear_all_open_stateid(state); 1693 } 1694 1695 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1696 nfs4_stateid_copy(&state->stateid, stateid); 1697 nfs4_stateid_copy(&state->open_stateid, stateid); 1698 trace_nfs4_open_stateid_update(state->inode, stateid, status); 1699 nfs_state_log_update_open_stateid(state); 1700 } 1701 1702 static void nfs_state_set_open_stateid(struct nfs4_state *state, 1703 const nfs4_stateid *open_stateid, 1704 fmode_t fmode, 1705 nfs4_stateid *freeme) 1706 { 1707 /* 1708 * Protect the call to nfs4_state_set_mode_locked and 1709 * serialise the stateid update 1710 */ 1711 write_seqlock(&state->seqlock); 1712 nfs_set_open_stateid_locked(state, open_stateid, freeme); 1713 switch (fmode) { 1714 case FMODE_READ: 1715 set_bit(NFS_O_RDONLY_STATE, &state->flags); 1716 break; 1717 case FMODE_WRITE: 1718 set_bit(NFS_O_WRONLY_STATE, &state->flags); 1719 break; 1720 case FMODE_READ|FMODE_WRITE: 1721 set_bit(NFS_O_RDWR_STATE, &state->flags); 1722 } 1723 set_bit(NFS_OPEN_STATE, &state->flags); 1724 write_sequnlock(&state->seqlock); 1725 } 1726 1727 static void nfs_state_clear_open_state_flags(struct nfs4_state *state) 1728 { 1729 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1730 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1731 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1732 clear_bit(NFS_OPEN_STATE, &state->flags); 1733 } 1734 1735 static void nfs_state_set_delegation(struct nfs4_state *state, 1736 const nfs4_stateid *deleg_stateid, 1737 fmode_t fmode) 1738 { 1739 /* 1740 * Protect the call to nfs4_state_set_mode_locked and 1741 * serialise the stateid update 1742 */ 1743 write_seqlock(&state->seqlock); 1744 nfs4_stateid_copy(&state->stateid, deleg_stateid); 1745 set_bit(NFS_DELEGATED_STATE, &state->flags); 1746 write_sequnlock(&state->seqlock); 1747 } 1748 1749 static void nfs_state_clear_delegation(struct nfs4_state *state) 1750 { 1751 write_seqlock(&state->seqlock); 1752 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 1753 clear_bit(NFS_DELEGATED_STATE, &state->flags); 1754 write_sequnlock(&state->seqlock); 1755 } 1756 1757 int update_open_stateid(struct nfs4_state *state, 1758 const nfs4_stateid *open_stateid, 1759 const nfs4_stateid *delegation, 1760 fmode_t fmode) 1761 { 1762 struct nfs_server *server = NFS_SERVER(state->inode); 1763 struct nfs_client *clp = server->nfs_client; 1764 struct nfs_inode *nfsi = NFS_I(state->inode); 1765 struct nfs_delegation *deleg_cur; 1766 nfs4_stateid freeme = { }; 1767 int ret = 0; 1768 1769 fmode &= (FMODE_READ|FMODE_WRITE); 1770 1771 rcu_read_lock(); 1772 spin_lock(&state->owner->so_lock); 1773 if (open_stateid != NULL) { 1774 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme); 1775 ret = 1; 1776 } 1777 1778 deleg_cur = nfs4_get_valid_delegation(state->inode); 1779 if (deleg_cur == NULL) 1780 goto no_delegation; 1781 1782 spin_lock(&deleg_cur->lock); 1783 if (rcu_dereference(nfsi->delegation) != deleg_cur || 1784 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || 1785 (deleg_cur->type & fmode) != fmode) 1786 goto no_delegation_unlock; 1787 1788 if (delegation == NULL) 1789 delegation = &deleg_cur->stateid; 1790 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation)) 1791 goto no_delegation_unlock; 1792 1793 nfs_mark_delegation_referenced(deleg_cur); 1794 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode); 1795 ret = 1; 1796 no_delegation_unlock: 1797 spin_unlock(&deleg_cur->lock); 1798 no_delegation: 1799 if (ret) 1800 update_open_stateflags(state, fmode); 1801 spin_unlock(&state->owner->so_lock); 1802 rcu_read_unlock(); 1803 1804 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) 1805 nfs4_schedule_state_manager(clp); 1806 if (freeme.type != 0) 1807 nfs4_test_and_free_stateid(server, &freeme, 1808 state->owner->so_cred); 1809 1810 return ret; 1811 } 1812 1813 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp, 1814 const nfs4_stateid *stateid) 1815 { 1816 struct nfs4_state *state = lsp->ls_state; 1817 bool ret = false; 1818 1819 spin_lock(&state->state_lock); 1820 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid)) 1821 goto out_noupdate; 1822 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid)) 1823 goto out_noupdate; 1824 nfs4_stateid_copy(&lsp->ls_stateid, stateid); 1825 ret = true; 1826 out_noupdate: 1827 spin_unlock(&state->state_lock); 1828 return ret; 1829 } 1830 1831 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) 1832 { 1833 struct nfs_delegation *delegation; 1834 1835 fmode &= FMODE_READ|FMODE_WRITE; 1836 rcu_read_lock(); 1837 delegation = nfs4_get_valid_delegation(inode); 1838 if (delegation == NULL || (delegation->type & fmode) == fmode) { 1839 rcu_read_unlock(); 1840 return; 1841 } 1842 rcu_read_unlock(); 1843 nfs4_inode_return_delegation(inode); 1844 } 1845 1846 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) 1847 { 1848 struct nfs4_state *state = opendata->state; 1849 struct nfs_delegation *delegation; 1850 int open_mode = opendata->o_arg.open_flags; 1851 fmode_t fmode = opendata->o_arg.fmode; 1852 enum open_claim_type4 claim = opendata->o_arg.claim; 1853 nfs4_stateid stateid; 1854 int ret = -EAGAIN; 1855 1856 for (;;) { 1857 spin_lock(&state->owner->so_lock); 1858 if (can_open_cached(state, fmode, open_mode, claim)) { 1859 update_open_stateflags(state, fmode); 1860 spin_unlock(&state->owner->so_lock); 1861 goto out_return_state; 1862 } 1863 spin_unlock(&state->owner->so_lock); 1864 rcu_read_lock(); 1865 delegation = nfs4_get_valid_delegation(state->inode); 1866 if (!can_open_delegated(delegation, fmode, claim)) { 1867 rcu_read_unlock(); 1868 break; 1869 } 1870 /* Save the delegation */ 1871 nfs4_stateid_copy(&stateid, &delegation->stateid); 1872 rcu_read_unlock(); 1873 nfs_release_seqid(opendata->o_arg.seqid); 1874 if (!opendata->is_recover) { 1875 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); 1876 if (ret != 0) 1877 goto out; 1878 } 1879 ret = -EAGAIN; 1880 1881 /* Try to update the stateid using the delegation */ 1882 if (update_open_stateid(state, NULL, &stateid, fmode)) 1883 goto out_return_state; 1884 } 1885 out: 1886 return ERR_PTR(ret); 1887 out_return_state: 1888 refcount_inc(&state->count); 1889 return state; 1890 } 1891 1892 static void 1893 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) 1894 { 1895 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; 1896 struct nfs_delegation *delegation; 1897 int delegation_flags = 0; 1898 1899 rcu_read_lock(); 1900 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 1901 if (delegation) 1902 delegation_flags = delegation->flags; 1903 rcu_read_unlock(); 1904 switch (data->o_arg.claim) { 1905 default: 1906 break; 1907 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 1908 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1909 pr_err_ratelimited("NFS: Broken NFSv4 server %s is " 1910 "returning a delegation for " 1911 "OPEN(CLAIM_DELEGATE_CUR)\n", 1912 clp->cl_hostname); 1913 return; 1914 } 1915 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) 1916 nfs_inode_set_delegation(state->inode, 1917 data->owner->so_cred, 1918 data->o_res.delegation_type, 1919 &data->o_res.delegation, 1920 data->o_res.pagemod_limit); 1921 else 1922 nfs_inode_reclaim_delegation(state->inode, 1923 data->owner->so_cred, 1924 data->o_res.delegation_type, 1925 &data->o_res.delegation, 1926 data->o_res.pagemod_limit); 1927 1928 if (data->o_res.do_recall) 1929 nfs_async_inode_return_delegation(state->inode, 1930 &data->o_res.delegation); 1931 } 1932 1933 /* 1934 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes 1935 * and update the nfs4_state. 1936 */ 1937 static struct nfs4_state * 1938 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) 1939 { 1940 struct inode *inode = data->state->inode; 1941 struct nfs4_state *state = data->state; 1942 int ret; 1943 1944 if (!data->rpc_done) { 1945 if (data->rpc_status) 1946 return ERR_PTR(data->rpc_status); 1947 /* cached opens have already been processed */ 1948 goto update; 1949 } 1950 1951 ret = nfs_refresh_inode(inode, &data->f_attr); 1952 if (ret) 1953 return ERR_PTR(ret); 1954 1955 if (data->o_res.delegation_type != 0) 1956 nfs4_opendata_check_deleg(data, state); 1957 update: 1958 if (!update_open_stateid(state, &data->o_res.stateid, 1959 NULL, data->o_arg.fmode)) 1960 return ERR_PTR(-EAGAIN); 1961 refcount_inc(&state->count); 1962 1963 return state; 1964 } 1965 1966 static struct inode * 1967 nfs4_opendata_get_inode(struct nfs4_opendata *data) 1968 { 1969 struct inode *inode; 1970 1971 switch (data->o_arg.claim) { 1972 case NFS4_OPEN_CLAIM_NULL: 1973 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 1974 case NFS4_OPEN_CLAIM_DELEGATE_PREV: 1975 if (!(data->f_attr.valid & NFS_ATTR_FATTR)) 1976 return ERR_PTR(-EAGAIN); 1977 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, 1978 &data->f_attr, data->f_label); 1979 break; 1980 default: 1981 inode = d_inode(data->dentry); 1982 ihold(inode); 1983 nfs_refresh_inode(inode, &data->f_attr); 1984 } 1985 return inode; 1986 } 1987 1988 static struct nfs4_state * 1989 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data) 1990 { 1991 struct nfs4_state *state; 1992 struct inode *inode; 1993 1994 inode = nfs4_opendata_get_inode(data); 1995 if (IS_ERR(inode)) 1996 return ERR_CAST(inode); 1997 if (data->state != NULL && data->state->inode == inode) { 1998 state = data->state; 1999 refcount_inc(&state->count); 2000 } else 2001 state = nfs4_get_open_state(inode, data->owner); 2002 iput(inode); 2003 if (state == NULL) 2004 state = ERR_PTR(-ENOMEM); 2005 return state; 2006 } 2007 2008 static struct nfs4_state * 2009 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) 2010 { 2011 struct nfs4_state *state; 2012 2013 if (!data->rpc_done) { 2014 state = nfs4_try_open_cached(data); 2015 trace_nfs4_cached_open(data->state); 2016 goto out; 2017 } 2018 2019 state = nfs4_opendata_find_nfs4_state(data); 2020 if (IS_ERR(state)) 2021 goto out; 2022 2023 if (data->o_res.delegation_type != 0) 2024 nfs4_opendata_check_deleg(data, state); 2025 if (!update_open_stateid(state, &data->o_res.stateid, 2026 NULL, data->o_arg.fmode)) { 2027 nfs4_put_open_state(state); 2028 state = ERR_PTR(-EAGAIN); 2029 } 2030 out: 2031 nfs_release_seqid(data->o_arg.seqid); 2032 return state; 2033 } 2034 2035 static struct nfs4_state * 2036 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) 2037 { 2038 struct nfs4_state *ret; 2039 2040 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) 2041 ret =_nfs4_opendata_reclaim_to_nfs4_state(data); 2042 else 2043 ret = _nfs4_opendata_to_nfs4_state(data); 2044 nfs4_sequence_free_slot(&data->o_res.seq_res); 2045 return ret; 2046 } 2047 2048 static struct nfs_open_context * 2049 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode) 2050 { 2051 struct nfs_inode *nfsi = NFS_I(state->inode); 2052 struct nfs_open_context *ctx; 2053 2054 rcu_read_lock(); 2055 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { 2056 if (ctx->state != state) 2057 continue; 2058 if ((ctx->mode & mode) != mode) 2059 continue; 2060 if (!get_nfs_open_context(ctx)) 2061 continue; 2062 rcu_read_unlock(); 2063 return ctx; 2064 } 2065 rcu_read_unlock(); 2066 return ERR_PTR(-ENOENT); 2067 } 2068 2069 static struct nfs_open_context * 2070 nfs4_state_find_open_context(struct nfs4_state *state) 2071 { 2072 struct nfs_open_context *ctx; 2073 2074 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE); 2075 if (!IS_ERR(ctx)) 2076 return ctx; 2077 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE); 2078 if (!IS_ERR(ctx)) 2079 return ctx; 2080 return nfs4_state_find_open_context_mode(state, FMODE_READ); 2081 } 2082 2083 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, 2084 struct nfs4_state *state, enum open_claim_type4 claim) 2085 { 2086 struct nfs4_opendata *opendata; 2087 2088 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, 2089 NULL, claim, GFP_NOFS); 2090 if (opendata == NULL) 2091 return ERR_PTR(-ENOMEM); 2092 opendata->state = state; 2093 refcount_inc(&state->count); 2094 return opendata; 2095 } 2096 2097 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, 2098 fmode_t fmode) 2099 { 2100 struct nfs4_state *newstate; 2101 int ret; 2102 2103 if (!nfs4_mode_match_open_stateid(opendata->state, fmode)) 2104 return 0; 2105 opendata->o_arg.open_flags = 0; 2106 opendata->o_arg.fmode = fmode; 2107 opendata->o_arg.share_access = nfs4_map_atomic_open_share( 2108 NFS_SB(opendata->dentry->d_sb), 2109 fmode, 0); 2110 memset(&opendata->o_res, 0, sizeof(opendata->o_res)); 2111 memset(&opendata->c_res, 0, sizeof(opendata->c_res)); 2112 nfs4_init_opendata_res(opendata); 2113 ret = _nfs4_recover_proc_open(opendata); 2114 if (ret != 0) 2115 return ret; 2116 newstate = nfs4_opendata_to_nfs4_state(opendata); 2117 if (IS_ERR(newstate)) 2118 return PTR_ERR(newstate); 2119 if (newstate != opendata->state) 2120 ret = -ESTALE; 2121 nfs4_close_state(newstate, fmode); 2122 return ret; 2123 } 2124 2125 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) 2126 { 2127 int ret; 2128 2129 /* memory barrier prior to reading state->n_* */ 2130 smp_rmb(); 2131 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); 2132 if (ret != 0) 2133 return ret; 2134 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE); 2135 if (ret != 0) 2136 return ret; 2137 ret = nfs4_open_recover_helper(opendata, FMODE_READ); 2138 if (ret != 0) 2139 return ret; 2140 /* 2141 * We may have performed cached opens for all three recoveries. 2142 * Check if we need to update the current stateid. 2143 */ 2144 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && 2145 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { 2146 write_seqlock(&state->seqlock); 2147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 2148 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 2149 write_sequnlock(&state->seqlock); 2150 } 2151 return 0; 2152 } 2153 2154 /* 2155 * OPEN_RECLAIM: 2156 * reclaim state on the server after a reboot. 2157 */ 2158 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) 2159 { 2160 struct nfs_delegation *delegation; 2161 struct nfs4_opendata *opendata; 2162 fmode_t delegation_type = 0; 2163 int status; 2164 2165 opendata = nfs4_open_recoverdata_alloc(ctx, state, 2166 NFS4_OPEN_CLAIM_PREVIOUS); 2167 if (IS_ERR(opendata)) 2168 return PTR_ERR(opendata); 2169 rcu_read_lock(); 2170 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 2171 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) 2172 delegation_type = delegation->type; 2173 rcu_read_unlock(); 2174 opendata->o_arg.u.delegation_type = delegation_type; 2175 status = nfs4_open_recover(opendata, state); 2176 nfs4_opendata_put(opendata); 2177 return status; 2178 } 2179 2180 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) 2181 { 2182 struct nfs_server *server = NFS_SERVER(state->inode); 2183 struct nfs4_exception exception = { }; 2184 int err; 2185 do { 2186 err = _nfs4_do_open_reclaim(ctx, state); 2187 trace_nfs4_open_reclaim(ctx, 0, err); 2188 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) 2189 continue; 2190 if (err != -NFS4ERR_DELAY) 2191 break; 2192 nfs4_handle_exception(server, err, &exception); 2193 } while (exception.retry); 2194 return err; 2195 } 2196 2197 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) 2198 { 2199 struct nfs_open_context *ctx; 2200 int ret; 2201 2202 ctx = nfs4_state_find_open_context(state); 2203 if (IS_ERR(ctx)) 2204 return -EAGAIN; 2205 clear_bit(NFS_DELEGATED_STATE, &state->flags); 2206 nfs_state_clear_open_state_flags(state); 2207 ret = nfs4_do_open_reclaim(ctx, state); 2208 put_nfs_open_context(ctx); 2209 return ret; 2210 } 2211 2212 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err) 2213 { 2214 switch (err) { 2215 default: 2216 printk(KERN_ERR "NFS: %s: unhandled error " 2217 "%d.\n", __func__, err); 2218 case 0: 2219 case -ENOENT: 2220 case -EAGAIN: 2221 case -ESTALE: 2222 case -ETIMEDOUT: 2223 break; 2224 case -NFS4ERR_BADSESSION: 2225 case -NFS4ERR_BADSLOT: 2226 case -NFS4ERR_BAD_HIGH_SLOT: 2227 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 2228 case -NFS4ERR_DEADSESSION: 2229 return -EAGAIN; 2230 case -NFS4ERR_STALE_CLIENTID: 2231 case -NFS4ERR_STALE_STATEID: 2232 /* Don't recall a delegation if it was lost */ 2233 nfs4_schedule_lease_recovery(server->nfs_client); 2234 return -EAGAIN; 2235 case -NFS4ERR_MOVED: 2236 nfs4_schedule_migration_recovery(server); 2237 return -EAGAIN; 2238 case -NFS4ERR_LEASE_MOVED: 2239 nfs4_schedule_lease_moved_recovery(server->nfs_client); 2240 return -EAGAIN; 2241 case -NFS4ERR_DELEG_REVOKED: 2242 case -NFS4ERR_ADMIN_REVOKED: 2243 case -NFS4ERR_EXPIRED: 2244 case -NFS4ERR_BAD_STATEID: 2245 case -NFS4ERR_OPENMODE: 2246 nfs_inode_find_state_and_recover(state->inode, 2247 stateid); 2248 nfs4_schedule_stateid_recovery(server, state); 2249 return -EAGAIN; 2250 case -NFS4ERR_DELAY: 2251 case -NFS4ERR_GRACE: 2252 ssleep(1); 2253 return -EAGAIN; 2254 case -ENOMEM: 2255 case -NFS4ERR_DENIED: 2256 if (fl) { 2257 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner; 2258 if (lsp) 2259 set_bit(NFS_LOCK_LOST, &lsp->ls_flags); 2260 } 2261 return 0; 2262 } 2263 return err; 2264 } 2265 2266 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, 2267 struct nfs4_state *state, const nfs4_stateid *stateid) 2268 { 2269 struct nfs_server *server = NFS_SERVER(state->inode); 2270 struct nfs4_opendata *opendata; 2271 int err = 0; 2272 2273 opendata = nfs4_open_recoverdata_alloc(ctx, state, 2274 NFS4_OPEN_CLAIM_DELEG_CUR_FH); 2275 if (IS_ERR(opendata)) 2276 return PTR_ERR(opendata); 2277 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); 2278 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) { 2279 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); 2280 if (err) 2281 goto out; 2282 } 2283 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) { 2284 err = nfs4_open_recover_helper(opendata, FMODE_WRITE); 2285 if (err) 2286 goto out; 2287 } 2288 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) { 2289 err = nfs4_open_recover_helper(opendata, FMODE_READ); 2290 if (err) 2291 goto out; 2292 } 2293 nfs_state_clear_delegation(state); 2294 out: 2295 nfs4_opendata_put(opendata); 2296 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err); 2297 } 2298 2299 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) 2300 { 2301 struct nfs4_opendata *data = calldata; 2302 2303 nfs4_setup_sequence(data->o_arg.server->nfs_client, 2304 &data->c_arg.seq_args, &data->c_res.seq_res, task); 2305 } 2306 2307 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) 2308 { 2309 struct nfs4_opendata *data = calldata; 2310 2311 nfs40_sequence_done(task, &data->c_res.seq_res); 2312 2313 data->rpc_status = task->tk_status; 2314 if (data->rpc_status == 0) { 2315 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); 2316 nfs_confirm_seqid(&data->owner->so_seqid, 0); 2317 renew_lease(data->o_res.server, data->timestamp); 2318 data->rpc_done = true; 2319 } 2320 } 2321 2322 static void nfs4_open_confirm_release(void *calldata) 2323 { 2324 struct nfs4_opendata *data = calldata; 2325 struct nfs4_state *state = NULL; 2326 2327 /* If this request hasn't been cancelled, do nothing */ 2328 if (!data->cancelled) 2329 goto out_free; 2330 /* In case of error, no cleanup! */ 2331 if (!data->rpc_done) 2332 goto out_free; 2333 state = nfs4_opendata_to_nfs4_state(data); 2334 if (!IS_ERR(state)) 2335 nfs4_close_state(state, data->o_arg.fmode); 2336 out_free: 2337 nfs4_opendata_put(data); 2338 } 2339 2340 static const struct rpc_call_ops nfs4_open_confirm_ops = { 2341 .rpc_call_prepare = nfs4_open_confirm_prepare, 2342 .rpc_call_done = nfs4_open_confirm_done, 2343 .rpc_release = nfs4_open_confirm_release, 2344 }; 2345 2346 /* 2347 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata 2348 */ 2349 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) 2350 { 2351 struct nfs_server *server = NFS_SERVER(d_inode(data->dir)); 2352 struct rpc_task *task; 2353 struct rpc_message msg = { 2354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], 2355 .rpc_argp = &data->c_arg, 2356 .rpc_resp = &data->c_res, 2357 .rpc_cred = data->owner->so_cred, 2358 }; 2359 struct rpc_task_setup task_setup_data = { 2360 .rpc_client = server->client, 2361 .rpc_message = &msg, 2362 .callback_ops = &nfs4_open_confirm_ops, 2363 .callback_data = data, 2364 .workqueue = nfsiod_workqueue, 2365 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, 2366 }; 2367 int status; 2368 2369 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1, 2370 data->is_recover); 2371 kref_get(&data->kref); 2372 data->rpc_done = false; 2373 data->rpc_status = 0; 2374 data->timestamp = jiffies; 2375 task = rpc_run_task(&task_setup_data); 2376 if (IS_ERR(task)) 2377 return PTR_ERR(task); 2378 status = rpc_wait_for_completion_task(task); 2379 if (status != 0) { 2380 data->cancelled = true; 2381 smp_wmb(); 2382 } else 2383 status = data->rpc_status; 2384 rpc_put_task(task); 2385 return status; 2386 } 2387 2388 static void nfs4_open_prepare(struct rpc_task *task, void *calldata) 2389 { 2390 struct nfs4_opendata *data = calldata; 2391 struct nfs4_state_owner *sp = data->owner; 2392 struct nfs_client *clp = sp->so_server->nfs_client; 2393 enum open_claim_type4 claim = data->o_arg.claim; 2394 2395 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) 2396 goto out_wait; 2397 /* 2398 * Check if we still need to send an OPEN call, or if we can use 2399 * a delegation instead. 2400 */ 2401 if (data->state != NULL) { 2402 struct nfs_delegation *delegation; 2403 2404 if (can_open_cached(data->state, data->o_arg.fmode, 2405 data->o_arg.open_flags, claim)) 2406 goto out_no_action; 2407 rcu_read_lock(); 2408 delegation = nfs4_get_valid_delegation(data->state->inode); 2409 if (can_open_delegated(delegation, data->o_arg.fmode, claim)) 2410 goto unlock_no_action; 2411 rcu_read_unlock(); 2412 } 2413 /* Update client id. */ 2414 data->o_arg.clientid = clp->cl_clientid; 2415 switch (claim) { 2416 default: 2417 break; 2418 case NFS4_OPEN_CLAIM_PREVIOUS: 2419 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 2420 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 2421 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; 2422 /* Fall through */ 2423 case NFS4_OPEN_CLAIM_FH: 2424 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; 2425 } 2426 data->timestamp = jiffies; 2427 if (nfs4_setup_sequence(data->o_arg.server->nfs_client, 2428 &data->o_arg.seq_args, 2429 &data->o_res.seq_res, 2430 task) != 0) 2431 nfs_release_seqid(data->o_arg.seqid); 2432 2433 /* Set the create mode (note dependency on the session type) */ 2434 data->o_arg.createmode = NFS4_CREATE_UNCHECKED; 2435 if (data->o_arg.open_flags & O_EXCL) { 2436 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE; 2437 if (nfs4_has_persistent_session(clp)) 2438 data->o_arg.createmode = NFS4_CREATE_GUARDED; 2439 else if (clp->cl_mvops->minor_version > 0) 2440 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1; 2441 } 2442 return; 2443 unlock_no_action: 2444 trace_nfs4_cached_open(data->state); 2445 rcu_read_unlock(); 2446 out_no_action: 2447 task->tk_action = NULL; 2448 out_wait: 2449 nfs4_sequence_done(task, &data->o_res.seq_res); 2450 } 2451 2452 static void nfs4_open_done(struct rpc_task *task, void *calldata) 2453 { 2454 struct nfs4_opendata *data = calldata; 2455 2456 data->rpc_status = task->tk_status; 2457 2458 if (!nfs4_sequence_process(task, &data->o_res.seq_res)) 2459 return; 2460 2461 if (task->tk_status == 0) { 2462 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { 2463 switch (data->o_res.f_attr->mode & S_IFMT) { 2464 case S_IFREG: 2465 break; 2466 case S_IFLNK: 2467 data->rpc_status = -ELOOP; 2468 break; 2469 case S_IFDIR: 2470 data->rpc_status = -EISDIR; 2471 break; 2472 default: 2473 data->rpc_status = -ENOTDIR; 2474 } 2475 } 2476 renew_lease(data->o_res.server, data->timestamp); 2477 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) 2478 nfs_confirm_seqid(&data->owner->so_seqid, 0); 2479 } 2480 data->rpc_done = true; 2481 } 2482 2483 static void nfs4_open_release(void *calldata) 2484 { 2485 struct nfs4_opendata *data = calldata; 2486 struct nfs4_state *state = NULL; 2487 2488 /* If this request hasn't been cancelled, do nothing */ 2489 if (!data->cancelled) 2490 goto out_free; 2491 /* In case of error, no cleanup! */ 2492 if (data->rpc_status != 0 || !data->rpc_done) 2493 goto out_free; 2494 /* In case we need an open_confirm, no cleanup! */ 2495 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) 2496 goto out_free; 2497 state = nfs4_opendata_to_nfs4_state(data); 2498 if (!IS_ERR(state)) 2499 nfs4_close_state(state, data->o_arg.fmode); 2500 out_free: 2501 nfs4_opendata_put(data); 2502 } 2503 2504 static const struct rpc_call_ops nfs4_open_ops = { 2505 .rpc_call_prepare = nfs4_open_prepare, 2506 .rpc_call_done = nfs4_open_done, 2507 .rpc_release = nfs4_open_release, 2508 }; 2509 2510 static int nfs4_run_open_task(struct nfs4_opendata *data, 2511 struct nfs_open_context *ctx) 2512 { 2513 struct inode *dir = d_inode(data->dir); 2514 struct nfs_server *server = NFS_SERVER(dir); 2515 struct nfs_openargs *o_arg = &data->o_arg; 2516 struct nfs_openres *o_res = &data->o_res; 2517 struct rpc_task *task; 2518 struct rpc_message msg = { 2519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], 2520 .rpc_argp = o_arg, 2521 .rpc_resp = o_res, 2522 .rpc_cred = data->owner->so_cred, 2523 }; 2524 struct rpc_task_setup task_setup_data = { 2525 .rpc_client = server->client, 2526 .rpc_message = &msg, 2527 .callback_ops = &nfs4_open_ops, 2528 .callback_data = data, 2529 .workqueue = nfsiod_workqueue, 2530 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, 2531 }; 2532 int status; 2533 2534 kref_get(&data->kref); 2535 data->rpc_done = false; 2536 data->rpc_status = 0; 2537 data->cancelled = false; 2538 data->is_recover = false; 2539 if (!ctx) { 2540 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1); 2541 data->is_recover = true; 2542 task_setup_data.flags |= RPC_TASK_TIMEOUT; 2543 } else { 2544 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0); 2545 pnfs_lgopen_prepare(data, ctx); 2546 } 2547 task = rpc_run_task(&task_setup_data); 2548 if (IS_ERR(task)) 2549 return PTR_ERR(task); 2550 status = rpc_wait_for_completion_task(task); 2551 if (status != 0) { 2552 data->cancelled = true; 2553 smp_wmb(); 2554 } else 2555 status = data->rpc_status; 2556 rpc_put_task(task); 2557 2558 return status; 2559 } 2560 2561 static int _nfs4_recover_proc_open(struct nfs4_opendata *data) 2562 { 2563 struct inode *dir = d_inode(data->dir); 2564 struct nfs_openres *o_res = &data->o_res; 2565 int status; 2566 2567 status = nfs4_run_open_task(data, NULL); 2568 if (status != 0 || !data->rpc_done) 2569 return status; 2570 2571 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); 2572 2573 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) 2574 status = _nfs4_proc_open_confirm(data); 2575 2576 return status; 2577 } 2578 2579 /* 2580 * Additional permission checks in order to distinguish between an 2581 * open for read, and an open for execute. This works around the 2582 * fact that NFSv4 OPEN treats read and execute permissions as being 2583 * the same. 2584 * Note that in the non-execute case, we want to turn off permission 2585 * checking if we just created a new file (POSIX open() semantics). 2586 */ 2587 static int nfs4_opendata_access(const struct cred *cred, 2588 struct nfs4_opendata *opendata, 2589 struct nfs4_state *state, fmode_t fmode, 2590 int openflags) 2591 { 2592 struct nfs_access_entry cache; 2593 u32 mask, flags; 2594 2595 /* access call failed or for some reason the server doesn't 2596 * support any access modes -- defer access call until later */ 2597 if (opendata->o_res.access_supported == 0) 2598 return 0; 2599 2600 mask = 0; 2601 /* 2602 * Use openflags to check for exec, because fmode won't 2603 * always have FMODE_EXEC set when file open for exec. 2604 */ 2605 if (openflags & __FMODE_EXEC) { 2606 /* ONLY check for exec rights */ 2607 if (S_ISDIR(state->inode->i_mode)) 2608 mask = NFS4_ACCESS_LOOKUP; 2609 else 2610 mask = NFS4_ACCESS_EXECUTE; 2611 } else if ((fmode & FMODE_READ) && !opendata->file_created) 2612 mask = NFS4_ACCESS_READ; 2613 2614 cache.cred = cred; 2615 nfs_access_set_mask(&cache, opendata->o_res.access_result); 2616 nfs_access_add_cache(state->inode, &cache); 2617 2618 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP; 2619 if ((mask & ~cache.mask & flags) == 0) 2620 return 0; 2621 2622 return -EACCES; 2623 } 2624 2625 /* 2626 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata 2627 */ 2628 static int _nfs4_proc_open(struct nfs4_opendata *data, 2629 struct nfs_open_context *ctx) 2630 { 2631 struct inode *dir = d_inode(data->dir); 2632 struct nfs_server *server = NFS_SERVER(dir); 2633 struct nfs_openargs *o_arg = &data->o_arg; 2634 struct nfs_openres *o_res = &data->o_res; 2635 int status; 2636 2637 status = nfs4_run_open_task(data, ctx); 2638 if (!data->rpc_done) 2639 return status; 2640 if (status != 0) { 2641 if (status == -NFS4ERR_BADNAME && 2642 !(o_arg->open_flags & O_CREAT)) 2643 return -ENOENT; 2644 return status; 2645 } 2646 2647 nfs_fattr_map_and_free_names(server, &data->f_attr); 2648 2649 if (o_arg->open_flags & O_CREAT) { 2650 if (o_arg->open_flags & O_EXCL) 2651 data->file_created = true; 2652 else if (o_res->cinfo.before != o_res->cinfo.after) 2653 data->file_created = true; 2654 if (data->file_created || 2655 inode_peek_iversion_raw(dir) != o_res->cinfo.after) 2656 update_changeattr(dir, &o_res->cinfo, 2657 o_res->f_attr->time_start, 0); 2658 } 2659 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) 2660 server->caps &= ~NFS_CAP_POSIX_LOCK; 2661 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { 2662 status = _nfs4_proc_open_confirm(data); 2663 if (status != 0) 2664 return status; 2665 } 2666 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) { 2667 nfs4_sequence_free_slot(&o_res->seq_res); 2668 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, 2669 o_res->f_label, NULL); 2670 } 2671 return 0; 2672 } 2673 2674 /* 2675 * OPEN_EXPIRED: 2676 * reclaim state on the server after a network partition. 2677 * Assumes caller holds the appropriate lock 2678 */ 2679 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) 2680 { 2681 struct nfs4_opendata *opendata; 2682 int ret; 2683 2684 opendata = nfs4_open_recoverdata_alloc(ctx, state, 2685 NFS4_OPEN_CLAIM_FH); 2686 if (IS_ERR(opendata)) 2687 return PTR_ERR(opendata); 2688 ret = nfs4_open_recover(opendata, state); 2689 if (ret == -ESTALE) 2690 d_drop(ctx->dentry); 2691 nfs4_opendata_put(opendata); 2692 return ret; 2693 } 2694 2695 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) 2696 { 2697 struct nfs_server *server = NFS_SERVER(state->inode); 2698 struct nfs4_exception exception = { }; 2699 int err; 2700 2701 do { 2702 err = _nfs4_open_expired(ctx, state); 2703 trace_nfs4_open_expired(ctx, 0, err); 2704 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) 2705 continue; 2706 switch (err) { 2707 default: 2708 goto out; 2709 case -NFS4ERR_GRACE: 2710 case -NFS4ERR_DELAY: 2711 nfs4_handle_exception(server, err, &exception); 2712 err = 0; 2713 } 2714 } while (exception.retry); 2715 out: 2716 return err; 2717 } 2718 2719 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2720 { 2721 struct nfs_open_context *ctx; 2722 int ret; 2723 2724 ctx = nfs4_state_find_open_context(state); 2725 if (IS_ERR(ctx)) 2726 return -EAGAIN; 2727 ret = nfs4_do_open_expired(ctx, state); 2728 put_nfs_open_context(ctx); 2729 return ret; 2730 } 2731 2732 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state, 2733 const nfs4_stateid *stateid) 2734 { 2735 nfs_remove_bad_delegation(state->inode, stateid); 2736 nfs_state_clear_delegation(state); 2737 } 2738 2739 static void nfs40_clear_delegation_stateid(struct nfs4_state *state) 2740 { 2741 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL) 2742 nfs_finish_clear_delegation_stateid(state, NULL); 2743 } 2744 2745 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2746 { 2747 /* NFSv4.0 doesn't allow for delegation recovery on open expire */ 2748 nfs40_clear_delegation_stateid(state); 2749 nfs_state_clear_open_state_flags(state); 2750 return nfs4_open_expired(sp, state); 2751 } 2752 2753 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server, 2754 nfs4_stateid *stateid, 2755 const struct cred *cred) 2756 { 2757 return -NFS4ERR_BAD_STATEID; 2758 } 2759 2760 #if defined(CONFIG_NFS_V4_1) 2761 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server, 2762 nfs4_stateid *stateid, 2763 const struct cred *cred) 2764 { 2765 int status; 2766 2767 switch (stateid->type) { 2768 default: 2769 break; 2770 case NFS4_INVALID_STATEID_TYPE: 2771 case NFS4_SPECIAL_STATEID_TYPE: 2772 return -NFS4ERR_BAD_STATEID; 2773 case NFS4_REVOKED_STATEID_TYPE: 2774 goto out_free; 2775 } 2776 2777 status = nfs41_test_stateid(server, stateid, cred); 2778 switch (status) { 2779 case -NFS4ERR_EXPIRED: 2780 case -NFS4ERR_ADMIN_REVOKED: 2781 case -NFS4ERR_DELEG_REVOKED: 2782 break; 2783 default: 2784 return status; 2785 } 2786 out_free: 2787 /* Ack the revoked state to the server */ 2788 nfs41_free_stateid(server, stateid, cred, true); 2789 return -NFS4ERR_EXPIRED; 2790 } 2791 2792 static int nfs41_check_delegation_stateid(struct nfs4_state *state) 2793 { 2794 struct nfs_server *server = NFS_SERVER(state->inode); 2795 nfs4_stateid stateid; 2796 struct nfs_delegation *delegation; 2797 const struct cred *cred = NULL; 2798 int status, ret = NFS_OK; 2799 2800 /* Get the delegation credential for use by test/free_stateid */ 2801 rcu_read_lock(); 2802 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 2803 if (delegation == NULL) { 2804 rcu_read_unlock(); 2805 nfs_state_clear_delegation(state); 2806 return NFS_OK; 2807 } 2808 2809 spin_lock(&delegation->lock); 2810 nfs4_stateid_copy(&stateid, &delegation->stateid); 2811 2812 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, 2813 &delegation->flags)) { 2814 spin_unlock(&delegation->lock); 2815 rcu_read_unlock(); 2816 return NFS_OK; 2817 } 2818 2819 if (delegation->cred) 2820 cred = get_cred(delegation->cred); 2821 spin_unlock(&delegation->lock); 2822 rcu_read_unlock(); 2823 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred); 2824 trace_nfs4_test_delegation_stateid(state, NULL, status); 2825 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) 2826 nfs_finish_clear_delegation_stateid(state, &stateid); 2827 else 2828 ret = status; 2829 2830 put_cred(cred); 2831 return ret; 2832 } 2833 2834 static void nfs41_delegation_recover_stateid(struct nfs4_state *state) 2835 { 2836 nfs4_stateid tmp; 2837 2838 if (test_bit(NFS_DELEGATED_STATE, &state->flags) && 2839 nfs4_copy_delegation_stateid(state->inode, state->state, 2840 &tmp, NULL) && 2841 nfs4_stateid_match_other(&state->stateid, &tmp)) 2842 nfs_state_set_delegation(state, &tmp, state->state); 2843 else 2844 nfs_state_clear_delegation(state); 2845 } 2846 2847 /** 2848 * nfs41_check_expired_locks - possibly free a lock stateid 2849 * 2850 * @state: NFSv4 state for an inode 2851 * 2852 * Returns NFS_OK if recovery for this stateid is now finished. 2853 * Otherwise a negative NFS4ERR value is returned. 2854 */ 2855 static int nfs41_check_expired_locks(struct nfs4_state *state) 2856 { 2857 int status, ret = NFS_OK; 2858 struct nfs4_lock_state *lsp, *prev = NULL; 2859 struct nfs_server *server = NFS_SERVER(state->inode); 2860 2861 if (!test_bit(LK_STATE_IN_USE, &state->flags)) 2862 goto out; 2863 2864 spin_lock(&state->state_lock); 2865 list_for_each_entry(lsp, &state->lock_states, ls_locks) { 2866 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { 2867 const struct cred *cred = lsp->ls_state->owner->so_cred; 2868 2869 refcount_inc(&lsp->ls_count); 2870 spin_unlock(&state->state_lock); 2871 2872 nfs4_put_lock_state(prev); 2873 prev = lsp; 2874 2875 status = nfs41_test_and_free_expired_stateid(server, 2876 &lsp->ls_stateid, 2877 cred); 2878 trace_nfs4_test_lock_stateid(state, lsp, status); 2879 if (status == -NFS4ERR_EXPIRED || 2880 status == -NFS4ERR_BAD_STATEID) { 2881 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 2882 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE; 2883 if (!recover_lost_locks) 2884 set_bit(NFS_LOCK_LOST, &lsp->ls_flags); 2885 } else if (status != NFS_OK) { 2886 ret = status; 2887 nfs4_put_lock_state(prev); 2888 goto out; 2889 } 2890 spin_lock(&state->state_lock); 2891 } 2892 } 2893 spin_unlock(&state->state_lock); 2894 nfs4_put_lock_state(prev); 2895 out: 2896 return ret; 2897 } 2898 2899 /** 2900 * nfs41_check_open_stateid - possibly free an open stateid 2901 * 2902 * @state: NFSv4 state for an inode 2903 * 2904 * Returns NFS_OK if recovery for this stateid is now finished. 2905 * Otherwise a negative NFS4ERR value is returned. 2906 */ 2907 static int nfs41_check_open_stateid(struct nfs4_state *state) 2908 { 2909 struct nfs_server *server = NFS_SERVER(state->inode); 2910 nfs4_stateid *stateid = &state->open_stateid; 2911 const struct cred *cred = state->owner->so_cred; 2912 int status; 2913 2914 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) 2915 return -NFS4ERR_BAD_STATEID; 2916 status = nfs41_test_and_free_expired_stateid(server, stateid, cred); 2917 trace_nfs4_test_open_stateid(state, NULL, status); 2918 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) { 2919 nfs_state_clear_open_state_flags(state); 2920 stateid->type = NFS4_INVALID_STATEID_TYPE; 2921 return status; 2922 } 2923 if (nfs_open_stateid_recover_openmode(state)) 2924 return -NFS4ERR_OPENMODE; 2925 return NFS_OK; 2926 } 2927 2928 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2929 { 2930 int status; 2931 2932 status = nfs41_check_delegation_stateid(state); 2933 if (status != NFS_OK) 2934 return status; 2935 nfs41_delegation_recover_stateid(state); 2936 2937 status = nfs41_check_expired_locks(state); 2938 if (status != NFS_OK) 2939 return status; 2940 status = nfs41_check_open_stateid(state); 2941 if (status != NFS_OK) 2942 status = nfs4_open_expired(sp, state); 2943 return status; 2944 } 2945 #endif 2946 2947 /* 2948 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* 2949 * fields corresponding to attributes that were used to store the verifier. 2950 * Make sure we clobber those fields in the later setattr call 2951 */ 2952 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata, 2953 struct iattr *sattr, struct nfs4_label **label) 2954 { 2955 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask; 2956 __u32 attrset[3]; 2957 unsigned ret; 2958 unsigned i; 2959 2960 for (i = 0; i < ARRAY_SIZE(attrset); i++) { 2961 attrset[i] = opendata->o_res.attrset[i]; 2962 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1) 2963 attrset[i] &= ~bitmask[i]; 2964 } 2965 2966 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ? 2967 sattr->ia_valid : 0; 2968 2969 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) { 2970 if (sattr->ia_valid & ATTR_ATIME_SET) 2971 ret |= ATTR_ATIME_SET; 2972 else 2973 ret |= ATTR_ATIME; 2974 } 2975 2976 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) { 2977 if (sattr->ia_valid & ATTR_MTIME_SET) 2978 ret |= ATTR_MTIME_SET; 2979 else 2980 ret |= ATTR_MTIME; 2981 } 2982 2983 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL)) 2984 *label = NULL; 2985 return ret; 2986 } 2987 2988 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, 2989 int flags, struct nfs_open_context *ctx) 2990 { 2991 struct nfs4_state_owner *sp = opendata->owner; 2992 struct nfs_server *server = sp->so_server; 2993 struct dentry *dentry; 2994 struct nfs4_state *state; 2995 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx); 2996 struct inode *dir = d_inode(opendata->dir); 2997 unsigned long dir_verifier; 2998 unsigned int seq; 2999 int ret; 3000 3001 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); 3002 dir_verifier = nfs_save_change_attribute(dir); 3003 3004 ret = _nfs4_proc_open(opendata, ctx); 3005 if (ret != 0) 3006 goto out; 3007 3008 state = _nfs4_opendata_to_nfs4_state(opendata); 3009 ret = PTR_ERR(state); 3010 if (IS_ERR(state)) 3011 goto out; 3012 ctx->state = state; 3013 if (server->caps & NFS_CAP_POSIX_LOCK) 3014 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); 3015 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK) 3016 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags); 3017 3018 dentry = opendata->dentry; 3019 if (d_really_is_negative(dentry)) { 3020 struct dentry *alias; 3021 d_drop(dentry); 3022 alias = d_exact_alias(dentry, state->inode); 3023 if (!alias) 3024 alias = d_splice_alias(igrab(state->inode), dentry); 3025 /* d_splice_alias() can't fail here - it's a non-directory */ 3026 if (alias) { 3027 dput(ctx->dentry); 3028 ctx->dentry = dentry = alias; 3029 } 3030 } 3031 3032 switch(opendata->o_arg.claim) { 3033 default: 3034 break; 3035 case NFS4_OPEN_CLAIM_NULL: 3036 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 3037 case NFS4_OPEN_CLAIM_DELEGATE_PREV: 3038 if (!opendata->rpc_done) 3039 break; 3040 if (opendata->o_res.delegation_type != 0) 3041 dir_verifier = nfs_save_change_attribute(dir); 3042 nfs_set_verifier(dentry, dir_verifier); 3043 } 3044 3045 /* Parse layoutget results before we check for access */ 3046 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx); 3047 3048 ret = nfs4_opendata_access(sp->so_cred, opendata, state, 3049 acc_mode, flags); 3050 if (ret != 0) 3051 goto out; 3052 3053 if (d_inode(dentry) == state->inode) { 3054 nfs_inode_attach_open_context(ctx); 3055 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) 3056 nfs4_schedule_stateid_recovery(server, state); 3057 } 3058 3059 out: 3060 if (!opendata->cancelled) 3061 nfs4_sequence_free_slot(&opendata->o_res.seq_res); 3062 return ret; 3063 } 3064 3065 /* 3066 * Returns a referenced nfs4_state 3067 */ 3068 static int _nfs4_do_open(struct inode *dir, 3069 struct nfs_open_context *ctx, 3070 int flags, 3071 const struct nfs4_open_createattrs *c, 3072 int *opened) 3073 { 3074 struct nfs4_state_owner *sp; 3075 struct nfs4_state *state = NULL; 3076 struct nfs_server *server = NFS_SERVER(dir); 3077 struct nfs4_opendata *opendata; 3078 struct dentry *dentry = ctx->dentry; 3079 const struct cred *cred = ctx->cred; 3080 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold; 3081 fmode_t fmode = _nfs4_ctx_to_openmode(ctx); 3082 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; 3083 struct iattr *sattr = c->sattr; 3084 struct nfs4_label *label = c->label; 3085 struct nfs4_label *olabel = NULL; 3086 int status; 3087 3088 /* Protect against reboot recovery conflicts */ 3089 status = -ENOMEM; 3090 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); 3091 if (sp == NULL) { 3092 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); 3093 goto out_err; 3094 } 3095 status = nfs4_client_recover_expired_lease(server->nfs_client); 3096 if (status != 0) 3097 goto err_put_state_owner; 3098 if (d_really_is_positive(dentry)) 3099 nfs4_return_incompatible_delegation(d_inode(dentry), fmode); 3100 status = -ENOMEM; 3101 if (d_really_is_positive(dentry)) 3102 claim = NFS4_OPEN_CLAIM_FH; 3103 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, 3104 c, claim, GFP_KERNEL); 3105 if (opendata == NULL) 3106 goto err_put_state_owner; 3107 3108 if (label) { 3109 olabel = nfs4_label_alloc(server, GFP_KERNEL); 3110 if (IS_ERR(olabel)) { 3111 status = PTR_ERR(olabel); 3112 goto err_opendata_put; 3113 } 3114 } 3115 3116 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { 3117 if (!opendata->f_attr.mdsthreshold) { 3118 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); 3119 if (!opendata->f_attr.mdsthreshold) 3120 goto err_free_label; 3121 } 3122 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; 3123 } 3124 if (d_really_is_positive(dentry)) 3125 opendata->state = nfs4_get_open_state(d_inode(dentry), sp); 3126 3127 status = _nfs4_open_and_get_state(opendata, flags, ctx); 3128 if (status != 0) 3129 goto err_free_label; 3130 state = ctx->state; 3131 3132 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) && 3133 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) { 3134 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label); 3135 /* 3136 * send create attributes which was not set by open 3137 * with an extra setattr. 3138 */ 3139 if (attrs || label) { 3140 unsigned ia_old = sattr->ia_valid; 3141 3142 sattr->ia_valid = attrs; 3143 nfs_fattr_init(opendata->o_res.f_attr); 3144 status = nfs4_do_setattr(state->inode, cred, 3145 opendata->o_res.f_attr, sattr, 3146 ctx, label, olabel); 3147 if (status == 0) { 3148 nfs_setattr_update_inode(state->inode, sattr, 3149 opendata->o_res.f_attr); 3150 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel); 3151 } 3152 sattr->ia_valid = ia_old; 3153 } 3154 } 3155 if (opened && opendata->file_created) 3156 *opened = 1; 3157 3158 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) { 3159 *ctx_th = opendata->f_attr.mdsthreshold; 3160 opendata->f_attr.mdsthreshold = NULL; 3161 } 3162 3163 nfs4_label_free(olabel); 3164 3165 nfs4_opendata_put(opendata); 3166 nfs4_put_state_owner(sp); 3167 return 0; 3168 err_free_label: 3169 nfs4_label_free(olabel); 3170 err_opendata_put: 3171 nfs4_opendata_put(opendata); 3172 err_put_state_owner: 3173 nfs4_put_state_owner(sp); 3174 out_err: 3175 return status; 3176 } 3177 3178 3179 static struct nfs4_state *nfs4_do_open(struct inode *dir, 3180 struct nfs_open_context *ctx, 3181 int flags, 3182 struct iattr *sattr, 3183 struct nfs4_label *label, 3184 int *opened) 3185 { 3186 struct nfs_server *server = NFS_SERVER(dir); 3187 struct nfs4_exception exception = { 3188 .interruptible = true, 3189 }; 3190 struct nfs4_state *res; 3191 struct nfs4_open_createattrs c = { 3192 .label = label, 3193 .sattr = sattr, 3194 .verf = { 3195 [0] = (__u32)jiffies, 3196 [1] = (__u32)current->pid, 3197 }, 3198 }; 3199 int status; 3200 3201 do { 3202 status = _nfs4_do_open(dir, ctx, flags, &c, opened); 3203 res = ctx->state; 3204 trace_nfs4_open_file(ctx, flags, status); 3205 if (status == 0) 3206 break; 3207 /* NOTE: BAD_SEQID means the server and client disagree about the 3208 * book-keeping w.r.t. state-changing operations 3209 * (OPEN/CLOSE/LOCK/LOCKU...) 3210 * It is actually a sign of a bug on the client or on the server. 3211 * 3212 * If we receive a BAD_SEQID error in the particular case of 3213 * doing an OPEN, we assume that nfs_increment_open_seqid() will 3214 * have unhashed the old state_owner for us, and that we can 3215 * therefore safely retry using a new one. We should still warn 3216 * the user though... 3217 */ 3218 if (status == -NFS4ERR_BAD_SEQID) { 3219 pr_warn_ratelimited("NFS: v4 server %s " 3220 " returned a bad sequence-id error!\n", 3221 NFS_SERVER(dir)->nfs_client->cl_hostname); 3222 exception.retry = 1; 3223 continue; 3224 } 3225 /* 3226 * BAD_STATEID on OPEN means that the server cancelled our 3227 * state before it received the OPEN_CONFIRM. 3228 * Recover by retrying the request as per the discussion 3229 * on Page 181 of RFC3530. 3230 */ 3231 if (status == -NFS4ERR_BAD_STATEID) { 3232 exception.retry = 1; 3233 continue; 3234 } 3235 if (status == -NFS4ERR_EXPIRED) { 3236 nfs4_schedule_lease_recovery(server->nfs_client); 3237 exception.retry = 1; 3238 continue; 3239 } 3240 if (status == -EAGAIN) { 3241 /* We must have found a delegation */ 3242 exception.retry = 1; 3243 continue; 3244 } 3245 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) 3246 continue; 3247 res = ERR_PTR(nfs4_handle_exception(server, 3248 status, &exception)); 3249 } while (exception.retry); 3250 return res; 3251 } 3252 3253 static int _nfs4_do_setattr(struct inode *inode, 3254 struct nfs_setattrargs *arg, 3255 struct nfs_setattrres *res, 3256 const struct cred *cred, 3257 struct nfs_open_context *ctx) 3258 { 3259 struct nfs_server *server = NFS_SERVER(inode); 3260 struct rpc_message msg = { 3261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 3262 .rpc_argp = arg, 3263 .rpc_resp = res, 3264 .rpc_cred = cred, 3265 }; 3266 const struct cred *delegation_cred = NULL; 3267 unsigned long timestamp = jiffies; 3268 bool truncate; 3269 int status; 3270 3271 nfs_fattr_init(res->fattr); 3272 3273 /* Servers should only apply open mode checks for file size changes */ 3274 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false; 3275 if (!truncate) 3276 goto zero_stateid; 3277 3278 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) { 3279 /* Use that stateid */ 3280 } else if (ctx != NULL && ctx->state) { 3281 struct nfs_lock_context *l_ctx; 3282 if (!nfs4_valid_open_stateid(ctx->state)) 3283 return -EBADF; 3284 l_ctx = nfs_get_lock_context(ctx); 3285 if (IS_ERR(l_ctx)) 3286 return PTR_ERR(l_ctx); 3287 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx, 3288 &arg->stateid, &delegation_cred); 3289 nfs_put_lock_context(l_ctx); 3290 if (status == -EIO) 3291 return -EBADF; 3292 else if (status == -EAGAIN) 3293 goto zero_stateid; 3294 } else { 3295 zero_stateid: 3296 nfs4_stateid_copy(&arg->stateid, &zero_stateid); 3297 } 3298 if (delegation_cred) 3299 msg.rpc_cred = delegation_cred; 3300 3301 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1); 3302 3303 put_cred(delegation_cred); 3304 if (status == 0 && ctx != NULL) 3305 renew_lease(server, timestamp); 3306 trace_nfs4_setattr(inode, &arg->stateid, status); 3307 return status; 3308 } 3309 3310 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred, 3311 struct nfs_fattr *fattr, struct iattr *sattr, 3312 struct nfs_open_context *ctx, struct nfs4_label *ilabel, 3313 struct nfs4_label *olabel) 3314 { 3315 struct nfs_server *server = NFS_SERVER(inode); 3316 __u32 bitmask[NFS4_BITMASK_SZ]; 3317 struct nfs4_state *state = ctx ? ctx->state : NULL; 3318 struct nfs_setattrargs arg = { 3319 .fh = NFS_FH(inode), 3320 .iap = sattr, 3321 .server = server, 3322 .bitmask = bitmask, 3323 .label = ilabel, 3324 }; 3325 struct nfs_setattrres res = { 3326 .fattr = fattr, 3327 .label = olabel, 3328 .server = server, 3329 }; 3330 struct nfs4_exception exception = { 3331 .state = state, 3332 .inode = inode, 3333 .stateid = &arg.stateid, 3334 }; 3335 int err; 3336 3337 do { 3338 nfs4_bitmap_copy_adjust_setattr(bitmask, 3339 nfs4_bitmask(server, olabel), 3340 inode); 3341 3342 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx); 3343 switch (err) { 3344 case -NFS4ERR_OPENMODE: 3345 if (!(sattr->ia_valid & ATTR_SIZE)) { 3346 pr_warn_once("NFSv4: server %s is incorrectly " 3347 "applying open mode checks to " 3348 "a SETATTR that is not " 3349 "changing file size.\n", 3350 server->nfs_client->cl_hostname); 3351 } 3352 if (state && !(state->state & FMODE_WRITE)) { 3353 err = -EBADF; 3354 if (sattr->ia_valid & ATTR_OPEN) 3355 err = -EACCES; 3356 goto out; 3357 } 3358 } 3359 err = nfs4_handle_exception(server, err, &exception); 3360 } while (exception.retry); 3361 out: 3362 return err; 3363 } 3364 3365 static bool 3366 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task) 3367 { 3368 if (inode == NULL || !nfs_have_layout(inode)) 3369 return false; 3370 3371 return pnfs_wait_on_layoutreturn(inode, task); 3372 } 3373 3374 /* 3375 * Update the seqid of an open stateid 3376 */ 3377 static void nfs4_sync_open_stateid(nfs4_stateid *dst, 3378 struct nfs4_state *state) 3379 { 3380 __be32 seqid_open; 3381 u32 dst_seqid; 3382 int seq; 3383 3384 for (;;) { 3385 if (!nfs4_valid_open_stateid(state)) 3386 break; 3387 seq = read_seqbegin(&state->seqlock); 3388 if (!nfs4_state_match_open_stateid_other(state, dst)) { 3389 nfs4_stateid_copy(dst, &state->open_stateid); 3390 if (read_seqretry(&state->seqlock, seq)) 3391 continue; 3392 break; 3393 } 3394 seqid_open = state->open_stateid.seqid; 3395 if (read_seqretry(&state->seqlock, seq)) 3396 continue; 3397 3398 dst_seqid = be32_to_cpu(dst->seqid); 3399 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0) 3400 dst->seqid = seqid_open; 3401 break; 3402 } 3403 } 3404 3405 /* 3406 * Update the seqid of an open stateid after receiving 3407 * NFS4ERR_OLD_STATEID 3408 */ 3409 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst, 3410 struct nfs4_state *state) 3411 { 3412 __be32 seqid_open; 3413 u32 dst_seqid; 3414 bool ret; 3415 int seq; 3416 3417 for (;;) { 3418 ret = false; 3419 if (!nfs4_valid_open_stateid(state)) 3420 break; 3421 seq = read_seqbegin(&state->seqlock); 3422 if (!nfs4_state_match_open_stateid_other(state, dst)) { 3423 if (read_seqretry(&state->seqlock, seq)) 3424 continue; 3425 break; 3426 } 3427 seqid_open = state->open_stateid.seqid; 3428 if (read_seqretry(&state->seqlock, seq)) 3429 continue; 3430 3431 dst_seqid = be32_to_cpu(dst->seqid); 3432 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0) 3433 dst->seqid = cpu_to_be32(dst_seqid + 1); 3434 else 3435 dst->seqid = seqid_open; 3436 ret = true; 3437 break; 3438 } 3439 3440 return ret; 3441 } 3442 3443 struct nfs4_closedata { 3444 struct inode *inode; 3445 struct nfs4_state *state; 3446 struct nfs_closeargs arg; 3447 struct nfs_closeres res; 3448 struct { 3449 struct nfs4_layoutreturn_args arg; 3450 struct nfs4_layoutreturn_res res; 3451 struct nfs4_xdr_opaque_data ld_private; 3452 u32 roc_barrier; 3453 bool roc; 3454 } lr; 3455 struct nfs_fattr fattr; 3456 unsigned long timestamp; 3457 }; 3458 3459 static void nfs4_free_closedata(void *data) 3460 { 3461 struct nfs4_closedata *calldata = data; 3462 struct nfs4_state_owner *sp = calldata->state->owner; 3463 struct super_block *sb = calldata->state->inode->i_sb; 3464 3465 if (calldata->lr.roc) 3466 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res, 3467 calldata->res.lr_ret); 3468 nfs4_put_open_state(calldata->state); 3469 nfs_free_seqid(calldata->arg.seqid); 3470 nfs4_put_state_owner(sp); 3471 nfs_sb_deactive(sb); 3472 kfree(calldata); 3473 } 3474 3475 static void nfs4_close_done(struct rpc_task *task, void *data) 3476 { 3477 struct nfs4_closedata *calldata = data; 3478 struct nfs4_state *state = calldata->state; 3479 struct nfs_server *server = NFS_SERVER(calldata->inode); 3480 nfs4_stateid *res_stateid = NULL; 3481 struct nfs4_exception exception = { 3482 .state = state, 3483 .inode = calldata->inode, 3484 .stateid = &calldata->arg.stateid, 3485 }; 3486 3487 dprintk("%s: begin!\n", __func__); 3488 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 3489 return; 3490 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status); 3491 3492 /* Handle Layoutreturn errors */ 3493 if (pnfs_roc_done(task, calldata->inode, 3494 &calldata->arg.lr_args, 3495 &calldata->res.lr_res, 3496 &calldata->res.lr_ret) == -EAGAIN) 3497 goto out_restart; 3498 3499 /* hmm. we are done with the inode, and in the process of freeing 3500 * the state_owner. we keep this around to process errors 3501 */ 3502 switch (task->tk_status) { 3503 case 0: 3504 res_stateid = &calldata->res.stateid; 3505 renew_lease(server, calldata->timestamp); 3506 break; 3507 case -NFS4ERR_ACCESS: 3508 if (calldata->arg.bitmask != NULL) { 3509 calldata->arg.bitmask = NULL; 3510 calldata->res.fattr = NULL; 3511 goto out_restart; 3512 3513 } 3514 break; 3515 case -NFS4ERR_OLD_STATEID: 3516 /* Did we race with OPEN? */ 3517 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid, 3518 state)) 3519 goto out_restart; 3520 goto out_release; 3521 case -NFS4ERR_ADMIN_REVOKED: 3522 case -NFS4ERR_STALE_STATEID: 3523 case -NFS4ERR_EXPIRED: 3524 nfs4_free_revoked_stateid(server, 3525 &calldata->arg.stateid, 3526 task->tk_msg.rpc_cred); 3527 /* Fallthrough */ 3528 case -NFS4ERR_BAD_STATEID: 3529 if (calldata->arg.fmode == 0) 3530 break; 3531 /* Fallthrough */ 3532 default: 3533 task->tk_status = nfs4_async_handle_exception(task, 3534 server, task->tk_status, &exception); 3535 if (exception.retry) 3536 goto out_restart; 3537 } 3538 nfs_clear_open_stateid(state, &calldata->arg.stateid, 3539 res_stateid, calldata->arg.fmode); 3540 out_release: 3541 task->tk_status = 0; 3542 nfs_release_seqid(calldata->arg.seqid); 3543 nfs_refresh_inode(calldata->inode, &calldata->fattr); 3544 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status); 3545 return; 3546 out_restart: 3547 task->tk_status = 0; 3548 rpc_restart_call_prepare(task); 3549 goto out_release; 3550 } 3551 3552 static void nfs4_close_prepare(struct rpc_task *task, void *data) 3553 { 3554 struct nfs4_closedata *calldata = data; 3555 struct nfs4_state *state = calldata->state; 3556 struct inode *inode = calldata->inode; 3557 struct pnfs_layout_hdr *lo; 3558 bool is_rdonly, is_wronly, is_rdwr; 3559 int call_close = 0; 3560 3561 dprintk("%s: begin!\n", __func__); 3562 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 3563 goto out_wait; 3564 3565 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; 3566 spin_lock(&state->owner->so_lock); 3567 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags); 3568 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags); 3569 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags); 3570 /* Calculate the change in open mode */ 3571 calldata->arg.fmode = 0; 3572 if (state->n_rdwr == 0) { 3573 if (state->n_rdonly == 0) 3574 call_close |= is_rdonly; 3575 else if (is_rdonly) 3576 calldata->arg.fmode |= FMODE_READ; 3577 if (state->n_wronly == 0) 3578 call_close |= is_wronly; 3579 else if (is_wronly) 3580 calldata->arg.fmode |= FMODE_WRITE; 3581 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE)) 3582 call_close |= is_rdwr; 3583 } else if (is_rdwr) 3584 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE; 3585 3586 nfs4_sync_open_stateid(&calldata->arg.stateid, state); 3587 if (!nfs4_valid_open_stateid(state)) 3588 call_close = 0; 3589 spin_unlock(&state->owner->so_lock); 3590 3591 if (!call_close) { 3592 /* Note: exit _without_ calling nfs4_close_done */ 3593 goto out_no_action; 3594 } 3595 3596 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) { 3597 nfs_release_seqid(calldata->arg.seqid); 3598 goto out_wait; 3599 } 3600 3601 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL; 3602 if (lo && !pnfs_layout_is_valid(lo)) { 3603 calldata->arg.lr_args = NULL; 3604 calldata->res.lr_res = NULL; 3605 } 3606 3607 if (calldata->arg.fmode == 0) 3608 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; 3609 3610 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) { 3611 /* Close-to-open cache consistency revalidation */ 3612 if (!nfs4_have_delegation(inode, FMODE_READ)) 3613 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; 3614 else 3615 calldata->arg.bitmask = NULL; 3616 } 3617 3618 calldata->arg.share_access = 3619 nfs4_map_atomic_open_share(NFS_SERVER(inode), 3620 calldata->arg.fmode, 0); 3621 3622 if (calldata->res.fattr == NULL) 3623 calldata->arg.bitmask = NULL; 3624 else if (calldata->arg.bitmask == NULL) 3625 calldata->res.fattr = NULL; 3626 calldata->timestamp = jiffies; 3627 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client, 3628 &calldata->arg.seq_args, 3629 &calldata->res.seq_res, 3630 task) != 0) 3631 nfs_release_seqid(calldata->arg.seqid); 3632 dprintk("%s: done!\n", __func__); 3633 return; 3634 out_no_action: 3635 task->tk_action = NULL; 3636 out_wait: 3637 nfs4_sequence_done(task, &calldata->res.seq_res); 3638 } 3639 3640 static const struct rpc_call_ops nfs4_close_ops = { 3641 .rpc_call_prepare = nfs4_close_prepare, 3642 .rpc_call_done = nfs4_close_done, 3643 .rpc_release = nfs4_free_closedata, 3644 }; 3645 3646 /* 3647 * It is possible for data to be read/written from a mem-mapped file 3648 * after the sys_close call (which hits the vfs layer as a flush). 3649 * This means that we can't safely call nfsv4 close on a file until 3650 * the inode is cleared. This in turn means that we are not good 3651 * NFSv4 citizens - we do not indicate to the server to update the file's 3652 * share state even when we are done with one of the three share 3653 * stateid's in the inode. 3654 * 3655 * NOTE: Caller must be holding the sp->so_owner semaphore! 3656 */ 3657 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) 3658 { 3659 struct nfs_server *server = NFS_SERVER(state->inode); 3660 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 3661 struct nfs4_closedata *calldata; 3662 struct nfs4_state_owner *sp = state->owner; 3663 struct rpc_task *task; 3664 struct rpc_message msg = { 3665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], 3666 .rpc_cred = state->owner->so_cred, 3667 }; 3668 struct rpc_task_setup task_setup_data = { 3669 .rpc_client = server->client, 3670 .rpc_message = &msg, 3671 .callback_ops = &nfs4_close_ops, 3672 .workqueue = nfsiod_workqueue, 3673 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, 3674 }; 3675 int status = -ENOMEM; 3676 3677 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP, 3678 &task_setup_data.rpc_client, &msg); 3679 3680 calldata = kzalloc(sizeof(*calldata), gfp_mask); 3681 if (calldata == NULL) 3682 goto out; 3683 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0); 3684 calldata->inode = state->inode; 3685 calldata->state = state; 3686 calldata->arg.fh = NFS_FH(state->inode); 3687 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state)) 3688 goto out_free_calldata; 3689 /* Serialization for the sequence id */ 3690 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 3691 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask); 3692 if (IS_ERR(calldata->arg.seqid)) 3693 goto out_free_calldata; 3694 nfs_fattr_init(&calldata->fattr); 3695 calldata->arg.fmode = 0; 3696 calldata->lr.arg.ld_private = &calldata->lr.ld_private; 3697 calldata->res.fattr = &calldata->fattr; 3698 calldata->res.seqid = calldata->arg.seqid; 3699 calldata->res.server = server; 3700 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; 3701 calldata->lr.roc = pnfs_roc(state->inode, 3702 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred); 3703 if (calldata->lr.roc) { 3704 calldata->arg.lr_args = &calldata->lr.arg; 3705 calldata->res.lr_res = &calldata->lr.res; 3706 } 3707 nfs_sb_active(calldata->inode->i_sb); 3708 3709 msg.rpc_argp = &calldata->arg; 3710 msg.rpc_resp = &calldata->res; 3711 task_setup_data.callback_data = calldata; 3712 task = rpc_run_task(&task_setup_data); 3713 if (IS_ERR(task)) 3714 return PTR_ERR(task); 3715 status = 0; 3716 if (wait) 3717 status = rpc_wait_for_completion_task(task); 3718 rpc_put_task(task); 3719 return status; 3720 out_free_calldata: 3721 kfree(calldata); 3722 out: 3723 nfs4_put_open_state(state); 3724 nfs4_put_state_owner(sp); 3725 return status; 3726 } 3727 3728 static struct inode * 3729 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, 3730 int open_flags, struct iattr *attr, int *opened) 3731 { 3732 struct nfs4_state *state; 3733 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL; 3734 3735 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l); 3736 3737 /* Protect against concurrent sillydeletes */ 3738 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened); 3739 3740 nfs4_label_release_security(label); 3741 3742 if (IS_ERR(state)) 3743 return ERR_CAST(state); 3744 return state->inode; 3745 } 3746 3747 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) 3748 { 3749 if (ctx->state == NULL) 3750 return; 3751 if (is_sync) 3752 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx)); 3753 else 3754 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx)); 3755 } 3756 3757 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL) 3758 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL) 3759 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL) 3760 3761 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) 3762 { 3763 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion; 3764 struct nfs4_server_caps_arg args = { 3765 .fhandle = fhandle, 3766 .bitmask = bitmask, 3767 }; 3768 struct nfs4_server_caps_res res = {}; 3769 struct rpc_message msg = { 3770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], 3771 .rpc_argp = &args, 3772 .rpc_resp = &res, 3773 }; 3774 int status; 3775 int i; 3776 3777 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS | 3778 FATTR4_WORD0_FH_EXPIRE_TYPE | 3779 FATTR4_WORD0_LINK_SUPPORT | 3780 FATTR4_WORD0_SYMLINK_SUPPORT | 3781 FATTR4_WORD0_ACLSUPPORT; 3782 if (minorversion) 3783 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT; 3784 3785 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3786 if (status == 0) { 3787 /* Sanity check the server answers */ 3788 switch (minorversion) { 3789 case 0: 3790 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK; 3791 res.attr_bitmask[2] = 0; 3792 break; 3793 case 1: 3794 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK; 3795 break; 3796 case 2: 3797 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK; 3798 } 3799 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); 3800 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| 3801 NFS_CAP_SYMLINKS|NFS_CAP_FILEID| 3802 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| 3803 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| 3804 NFS_CAP_CTIME|NFS_CAP_MTIME| 3805 NFS_CAP_SECURITY_LABEL); 3806 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL && 3807 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) 3808 server->caps |= NFS_CAP_ACLS; 3809 if (res.has_links != 0) 3810 server->caps |= NFS_CAP_HARDLINKS; 3811 if (res.has_symlinks != 0) 3812 server->caps |= NFS_CAP_SYMLINKS; 3813 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) 3814 server->caps |= NFS_CAP_FILEID; 3815 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) 3816 server->caps |= NFS_CAP_MODE; 3817 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) 3818 server->caps |= NFS_CAP_NLINK; 3819 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) 3820 server->caps |= NFS_CAP_OWNER; 3821 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) 3822 server->caps |= NFS_CAP_OWNER_GROUP; 3823 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) 3824 server->caps |= NFS_CAP_ATIME; 3825 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) 3826 server->caps |= NFS_CAP_CTIME; 3827 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) 3828 server->caps |= NFS_CAP_MTIME; 3829 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 3830 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL) 3831 server->caps |= NFS_CAP_SECURITY_LABEL; 3832 #endif 3833 memcpy(server->attr_bitmask_nl, res.attr_bitmask, 3834 sizeof(server->attr_bitmask)); 3835 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL; 3836 3837 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); 3838 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; 3839 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; 3840 server->cache_consistency_bitmask[2] = 0; 3841 3842 /* Avoid a regression due to buggy server */ 3843 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++) 3844 res.exclcreat_bitmask[i] &= res.attr_bitmask[i]; 3845 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask, 3846 sizeof(server->exclcreat_bitmask)); 3847 3848 server->acl_bitmask = res.acl_bitmask; 3849 server->fh_expire_type = res.fh_expire_type; 3850 } 3851 3852 return status; 3853 } 3854 3855 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) 3856 { 3857 struct nfs4_exception exception = { 3858 .interruptible = true, 3859 }; 3860 int err; 3861 do { 3862 err = nfs4_handle_exception(server, 3863 _nfs4_server_capabilities(server, fhandle), 3864 &exception); 3865 } while (exception.retry); 3866 return err; 3867 } 3868 3869 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, 3870 struct nfs_fsinfo *info) 3871 { 3872 u32 bitmask[3]; 3873 struct nfs4_lookup_root_arg args = { 3874 .bitmask = bitmask, 3875 }; 3876 struct nfs4_lookup_res res = { 3877 .server = server, 3878 .fattr = info->fattr, 3879 .fh = fhandle, 3880 }; 3881 struct rpc_message msg = { 3882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], 3883 .rpc_argp = &args, 3884 .rpc_resp = &res, 3885 }; 3886 3887 bitmask[0] = nfs4_fattr_bitmap[0]; 3888 bitmask[1] = nfs4_fattr_bitmap[1]; 3889 /* 3890 * Process the label in the upcoming getfattr 3891 */ 3892 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL; 3893 3894 nfs_fattr_init(info->fattr); 3895 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3896 } 3897 3898 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, 3899 struct nfs_fsinfo *info) 3900 { 3901 struct nfs4_exception exception = { 3902 .interruptible = true, 3903 }; 3904 int err; 3905 do { 3906 err = _nfs4_lookup_root(server, fhandle, info); 3907 trace_nfs4_lookup_root(server, fhandle, info->fattr, err); 3908 switch (err) { 3909 case 0: 3910 case -NFS4ERR_WRONGSEC: 3911 goto out; 3912 default: 3913 err = nfs4_handle_exception(server, err, &exception); 3914 } 3915 } while (exception.retry); 3916 out: 3917 return err; 3918 } 3919 3920 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 3921 struct nfs_fsinfo *info, rpc_authflavor_t flavor) 3922 { 3923 struct rpc_auth_create_args auth_args = { 3924 .pseudoflavor = flavor, 3925 }; 3926 struct rpc_auth *auth; 3927 3928 auth = rpcauth_create(&auth_args, server->client); 3929 if (IS_ERR(auth)) 3930 return -EACCES; 3931 return nfs4_lookup_root(server, fhandle, info); 3932 } 3933 3934 /* 3935 * Retry pseudoroot lookup with various security flavors. We do this when: 3936 * 3937 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC 3938 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation 3939 * 3940 * Returns zero on success, or a negative NFS4ERR value, or a 3941 * negative errno value. 3942 */ 3943 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 3944 struct nfs_fsinfo *info) 3945 { 3946 /* Per 3530bis 15.33.5 */ 3947 static const rpc_authflavor_t flav_array[] = { 3948 RPC_AUTH_GSS_KRB5P, 3949 RPC_AUTH_GSS_KRB5I, 3950 RPC_AUTH_GSS_KRB5, 3951 RPC_AUTH_UNIX, /* courtesy */ 3952 RPC_AUTH_NULL, 3953 }; 3954 int status = -EPERM; 3955 size_t i; 3956 3957 if (server->auth_info.flavor_len > 0) { 3958 /* try each flavor specified by user */ 3959 for (i = 0; i < server->auth_info.flavor_len; i++) { 3960 status = nfs4_lookup_root_sec(server, fhandle, info, 3961 server->auth_info.flavors[i]); 3962 if (status == -NFS4ERR_WRONGSEC || status == -EACCES) 3963 continue; 3964 break; 3965 } 3966 } else { 3967 /* no flavors specified by user, try default list */ 3968 for (i = 0; i < ARRAY_SIZE(flav_array); i++) { 3969 status = nfs4_lookup_root_sec(server, fhandle, info, 3970 flav_array[i]); 3971 if (status == -NFS4ERR_WRONGSEC || status == -EACCES) 3972 continue; 3973 break; 3974 } 3975 } 3976 3977 /* 3978 * -EACCES could mean that the user doesn't have correct permissions 3979 * to access the mount. It could also mean that we tried to mount 3980 * with a gss auth flavor, but rpc.gssd isn't running. Either way, 3981 * existing mount programs don't handle -EACCES very well so it should 3982 * be mapped to -EPERM instead. 3983 */ 3984 if (status == -EACCES) 3985 status = -EPERM; 3986 return status; 3987 } 3988 3989 /** 3990 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot 3991 * @server: initialized nfs_server handle 3992 * @fhandle: we fill in the pseudo-fs root file handle 3993 * @info: we fill in an FSINFO struct 3994 * @auth_probe: probe the auth flavours 3995 * 3996 * Returns zero on success, or a negative errno. 3997 */ 3998 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, 3999 struct nfs_fsinfo *info, 4000 bool auth_probe) 4001 { 4002 int status = 0; 4003 4004 if (!auth_probe) 4005 status = nfs4_lookup_root(server, fhandle, info); 4006 4007 if (auth_probe || status == NFS4ERR_WRONGSEC) 4008 status = server->nfs_client->cl_mvops->find_root_sec(server, 4009 fhandle, info); 4010 4011 if (status == 0) 4012 status = nfs4_server_capabilities(server, fhandle); 4013 if (status == 0) 4014 status = nfs4_do_fsinfo(server, fhandle, info); 4015 4016 return nfs4_map_errors(status); 4017 } 4018 4019 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, 4020 struct nfs_fsinfo *info) 4021 { 4022 int error; 4023 struct nfs_fattr *fattr = info->fattr; 4024 struct nfs4_label *label = fattr->label; 4025 4026 error = nfs4_server_capabilities(server, mntfh); 4027 if (error < 0) { 4028 dprintk("nfs4_get_root: getcaps error = %d\n", -error); 4029 return error; 4030 } 4031 4032 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL); 4033 if (error < 0) { 4034 dprintk("nfs4_get_root: getattr error = %d\n", -error); 4035 goto out; 4036 } 4037 4038 if (fattr->valid & NFS_ATTR_FATTR_FSID && 4039 !nfs_fsid_equal(&server->fsid, &fattr->fsid)) 4040 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 4041 4042 out: 4043 return error; 4044 } 4045 4046 /* 4047 * Get locations and (maybe) other attributes of a referral. 4048 * Note that we'll actually follow the referral later when 4049 * we detect fsid mismatch in inode revalidation 4050 */ 4051 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, 4052 const struct qstr *name, struct nfs_fattr *fattr, 4053 struct nfs_fh *fhandle) 4054 { 4055 int status = -ENOMEM; 4056 struct page *page = NULL; 4057 struct nfs4_fs_locations *locations = NULL; 4058 4059 page = alloc_page(GFP_KERNEL); 4060 if (page == NULL) 4061 goto out; 4062 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); 4063 if (locations == NULL) 4064 goto out; 4065 4066 status = nfs4_proc_fs_locations(client, dir, name, locations, page); 4067 if (status != 0) 4068 goto out; 4069 4070 /* 4071 * If the fsid didn't change, this is a migration event, not a 4072 * referral. Cause us to drop into the exception handler, which 4073 * will kick off migration recovery. 4074 */ 4075 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { 4076 dprintk("%s: server did not return a different fsid for" 4077 " a referral at %s\n", __func__, name->name); 4078 status = -NFS4ERR_MOVED; 4079 goto out; 4080 } 4081 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ 4082 nfs_fixup_referral_attributes(&locations->fattr); 4083 4084 /* replace the lookup nfs_fattr with the locations nfs_fattr */ 4085 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); 4086 memset(fhandle, 0, sizeof(struct nfs_fh)); 4087 out: 4088 if (page) 4089 __free_page(page); 4090 kfree(locations); 4091 return status; 4092 } 4093 4094 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 4095 struct nfs_fattr *fattr, struct nfs4_label *label, 4096 struct inode *inode) 4097 { 4098 __u32 bitmask[NFS4_BITMASK_SZ]; 4099 struct nfs4_getattr_arg args = { 4100 .fh = fhandle, 4101 .bitmask = bitmask, 4102 }; 4103 struct nfs4_getattr_res res = { 4104 .fattr = fattr, 4105 .label = label, 4106 .server = server, 4107 }; 4108 struct rpc_message msg = { 4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 4110 .rpc_argp = &args, 4111 .rpc_resp = &res, 4112 }; 4113 unsigned short task_flags = 0; 4114 4115 /* Is this is an attribute revalidation, subject to softreval? */ 4116 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL)) 4117 task_flags |= RPC_TASK_TIMEOUT; 4118 4119 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode); 4120 4121 nfs_fattr_init(fattr); 4122 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); 4123 return nfs4_do_call_sync(server->client, server, &msg, 4124 &args.seq_args, &res.seq_res, task_flags); 4125 } 4126 4127 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 4128 struct nfs_fattr *fattr, struct nfs4_label *label, 4129 struct inode *inode) 4130 { 4131 struct nfs4_exception exception = { 4132 .interruptible = true, 4133 }; 4134 int err; 4135 do { 4136 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode); 4137 trace_nfs4_getattr(server, fhandle, fattr, err); 4138 err = nfs4_handle_exception(server, err, 4139 &exception); 4140 } while (exception.retry); 4141 return err; 4142 } 4143 4144 /* 4145 * The file is not closed if it is opened due to the a request to change 4146 * the size of the file. The open call will not be needed once the 4147 * VFS layer lookup-intents are implemented. 4148 * 4149 * Close is called when the inode is destroyed. 4150 * If we haven't opened the file for O_WRONLY, we 4151 * need to in the size_change case to obtain a stateid. 4152 * 4153 * Got race? 4154 * Because OPEN is always done by name in nfsv4, it is 4155 * possible that we opened a different file by the same 4156 * name. We can recognize this race condition, but we 4157 * can't do anything about it besides returning an error. 4158 * 4159 * This will be fixed with VFS changes (lookup-intent). 4160 */ 4161 static int 4162 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, 4163 struct iattr *sattr) 4164 { 4165 struct inode *inode = d_inode(dentry); 4166 const struct cred *cred = NULL; 4167 struct nfs_open_context *ctx = NULL; 4168 struct nfs4_label *label = NULL; 4169 int status; 4170 4171 if (pnfs_ld_layoutret_on_setattr(inode) && 4172 sattr->ia_valid & ATTR_SIZE && 4173 sattr->ia_size < i_size_read(inode)) 4174 pnfs_commit_and_return_layout(inode); 4175 4176 nfs_fattr_init(fattr); 4177 4178 /* Deal with open(O_TRUNC) */ 4179 if (sattr->ia_valid & ATTR_OPEN) 4180 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME); 4181 4182 /* Optimization: if the end result is no change, don't RPC */ 4183 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) 4184 return 0; 4185 4186 /* Search for an existing open(O_WRITE) file */ 4187 if (sattr->ia_valid & ATTR_FILE) { 4188 4189 ctx = nfs_file_open_context(sattr->ia_file); 4190 if (ctx) 4191 cred = ctx->cred; 4192 } 4193 4194 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 4195 if (IS_ERR(label)) 4196 return PTR_ERR(label); 4197 4198 /* Return any delegations if we're going to change ACLs */ 4199 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) 4200 nfs4_inode_make_writeable(inode); 4201 4202 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label); 4203 if (status == 0) { 4204 nfs_setattr_update_inode(inode, sattr, fattr); 4205 nfs_setsecurity(inode, fattr, label); 4206 } 4207 nfs4_label_free(label); 4208 return status; 4209 } 4210 4211 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, 4212 struct dentry *dentry, struct nfs_fh *fhandle, 4213 struct nfs_fattr *fattr, struct nfs4_label *label) 4214 { 4215 struct nfs_server *server = NFS_SERVER(dir); 4216 int status; 4217 struct nfs4_lookup_arg args = { 4218 .bitmask = server->attr_bitmask, 4219 .dir_fh = NFS_FH(dir), 4220 .name = &dentry->d_name, 4221 }; 4222 struct nfs4_lookup_res res = { 4223 .server = server, 4224 .fattr = fattr, 4225 .label = label, 4226 .fh = fhandle, 4227 }; 4228 struct rpc_message msg = { 4229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], 4230 .rpc_argp = &args, 4231 .rpc_resp = &res, 4232 }; 4233 unsigned short task_flags = 0; 4234 4235 /* Is this is an attribute revalidation, subject to softreval? */ 4236 if (nfs_lookup_is_soft_revalidate(dentry)) 4237 task_flags |= RPC_TASK_TIMEOUT; 4238 4239 args.bitmask = nfs4_bitmask(server, label); 4240 4241 nfs_fattr_init(fattr); 4242 4243 dprintk("NFS call lookup %pd2\n", dentry); 4244 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); 4245 status = nfs4_do_call_sync(clnt, server, &msg, 4246 &args.seq_args, &res.seq_res, task_flags); 4247 dprintk("NFS reply lookup: %d\n", status); 4248 return status; 4249 } 4250 4251 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) 4252 { 4253 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | 4254 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; 4255 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; 4256 fattr->nlink = 2; 4257 } 4258 4259 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, 4260 struct dentry *dentry, struct nfs_fh *fhandle, 4261 struct nfs_fattr *fattr, struct nfs4_label *label) 4262 { 4263 struct nfs4_exception exception = { 4264 .interruptible = true, 4265 }; 4266 struct rpc_clnt *client = *clnt; 4267 const struct qstr *name = &dentry->d_name; 4268 int err; 4269 do { 4270 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label); 4271 trace_nfs4_lookup(dir, name, err); 4272 switch (err) { 4273 case -NFS4ERR_BADNAME: 4274 err = -ENOENT; 4275 goto out; 4276 case -NFS4ERR_MOVED: 4277 err = nfs4_get_referral(client, dir, name, fattr, fhandle); 4278 if (err == -NFS4ERR_MOVED) 4279 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); 4280 goto out; 4281 case -NFS4ERR_WRONGSEC: 4282 err = -EPERM; 4283 if (client != *clnt) 4284 goto out; 4285 client = nfs4_negotiate_security(client, dir, name); 4286 if (IS_ERR(client)) 4287 return PTR_ERR(client); 4288 4289 exception.retry = 1; 4290 break; 4291 default: 4292 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); 4293 } 4294 } while (exception.retry); 4295 4296 out: 4297 if (err == 0) 4298 *clnt = client; 4299 else if (client != *clnt) 4300 rpc_shutdown_client(client); 4301 4302 return err; 4303 } 4304 4305 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry, 4306 struct nfs_fh *fhandle, struct nfs_fattr *fattr, 4307 struct nfs4_label *label) 4308 { 4309 int status; 4310 struct rpc_clnt *client = NFS_CLIENT(dir); 4311 4312 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label); 4313 if (client != NFS_CLIENT(dir)) { 4314 rpc_shutdown_client(client); 4315 nfs_fixup_secinfo_attributes(fattr); 4316 } 4317 return status; 4318 } 4319 4320 struct rpc_clnt * 4321 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry, 4322 struct nfs_fh *fhandle, struct nfs_fattr *fattr) 4323 { 4324 struct rpc_clnt *client = NFS_CLIENT(dir); 4325 int status; 4326 4327 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL); 4328 if (status < 0) 4329 return ERR_PTR(status); 4330 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client; 4331 } 4332 4333 static int _nfs4_proc_lookupp(struct inode *inode, 4334 struct nfs_fh *fhandle, struct nfs_fattr *fattr, 4335 struct nfs4_label *label) 4336 { 4337 struct rpc_clnt *clnt = NFS_CLIENT(inode); 4338 struct nfs_server *server = NFS_SERVER(inode); 4339 int status; 4340 struct nfs4_lookupp_arg args = { 4341 .bitmask = server->attr_bitmask, 4342 .fh = NFS_FH(inode), 4343 }; 4344 struct nfs4_lookupp_res res = { 4345 .server = server, 4346 .fattr = fattr, 4347 .label = label, 4348 .fh = fhandle, 4349 }; 4350 struct rpc_message msg = { 4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP], 4352 .rpc_argp = &args, 4353 .rpc_resp = &res, 4354 }; 4355 4356 args.bitmask = nfs4_bitmask(server, label); 4357 4358 nfs_fattr_init(fattr); 4359 4360 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino); 4361 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, 4362 &res.seq_res, 0); 4363 dprintk("NFS reply lookupp: %d\n", status); 4364 return status; 4365 } 4366 4367 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle, 4368 struct nfs_fattr *fattr, struct nfs4_label *label) 4369 { 4370 struct nfs4_exception exception = { 4371 .interruptible = true, 4372 }; 4373 int err; 4374 do { 4375 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label); 4376 trace_nfs4_lookupp(inode, err); 4377 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4378 &exception); 4379 } while (exception.retry); 4380 return err; 4381 } 4382 4383 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) 4384 { 4385 struct nfs_server *server = NFS_SERVER(inode); 4386 struct nfs4_accessargs args = { 4387 .fh = NFS_FH(inode), 4388 .access = entry->mask, 4389 }; 4390 struct nfs4_accessres res = { 4391 .server = server, 4392 }; 4393 struct rpc_message msg = { 4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], 4395 .rpc_argp = &args, 4396 .rpc_resp = &res, 4397 .rpc_cred = entry->cred, 4398 }; 4399 int status = 0; 4400 4401 if (!nfs4_have_delegation(inode, FMODE_READ)) { 4402 res.fattr = nfs_alloc_fattr(); 4403 if (res.fattr == NULL) 4404 return -ENOMEM; 4405 args.bitmask = server->cache_consistency_bitmask; 4406 } 4407 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4408 if (!status) { 4409 nfs_access_set_mask(entry, res.access); 4410 if (res.fattr) 4411 nfs_refresh_inode(inode, res.fattr); 4412 } 4413 nfs_free_fattr(res.fattr); 4414 return status; 4415 } 4416 4417 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) 4418 { 4419 struct nfs4_exception exception = { 4420 .interruptible = true, 4421 }; 4422 int err; 4423 do { 4424 err = _nfs4_proc_access(inode, entry); 4425 trace_nfs4_access(inode, err); 4426 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4427 &exception); 4428 } while (exception.retry); 4429 return err; 4430 } 4431 4432 /* 4433 * TODO: For the time being, we don't try to get any attributes 4434 * along with any of the zero-copy operations READ, READDIR, 4435 * READLINK, WRITE. 4436 * 4437 * In the case of the first three, we want to put the GETATTR 4438 * after the read-type operation -- this is because it is hard 4439 * to predict the length of a GETATTR response in v4, and thus 4440 * align the READ data correctly. This means that the GETATTR 4441 * may end up partially falling into the page cache, and we should 4442 * shift it into the 'tail' of the xdr_buf before processing. 4443 * To do this efficiently, we need to know the total length 4444 * of data received, which doesn't seem to be available outside 4445 * of the RPC layer. 4446 * 4447 * In the case of WRITE, we also want to put the GETATTR after 4448 * the operation -- in this case because we want to make sure 4449 * we get the post-operation mtime and size. 4450 * 4451 * Both of these changes to the XDR layer would in fact be quite 4452 * minor, but I decided to leave them for a subsequent patch. 4453 */ 4454 static int _nfs4_proc_readlink(struct inode *inode, struct page *page, 4455 unsigned int pgbase, unsigned int pglen) 4456 { 4457 struct nfs4_readlink args = { 4458 .fh = NFS_FH(inode), 4459 .pgbase = pgbase, 4460 .pglen = pglen, 4461 .pages = &page, 4462 }; 4463 struct nfs4_readlink_res res; 4464 struct rpc_message msg = { 4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], 4466 .rpc_argp = &args, 4467 .rpc_resp = &res, 4468 }; 4469 4470 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); 4471 } 4472 4473 static int nfs4_proc_readlink(struct inode *inode, struct page *page, 4474 unsigned int pgbase, unsigned int pglen) 4475 { 4476 struct nfs4_exception exception = { 4477 .interruptible = true, 4478 }; 4479 int err; 4480 do { 4481 err = _nfs4_proc_readlink(inode, page, pgbase, pglen); 4482 trace_nfs4_readlink(inode, err); 4483 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4484 &exception); 4485 } while (exception.retry); 4486 return err; 4487 } 4488 4489 /* 4490 * This is just for mknod. open(O_CREAT) will always do ->open_context(). 4491 */ 4492 static int 4493 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 4494 int flags) 4495 { 4496 struct nfs_server *server = NFS_SERVER(dir); 4497 struct nfs4_label l, *ilabel = NULL; 4498 struct nfs_open_context *ctx; 4499 struct nfs4_state *state; 4500 int status = 0; 4501 4502 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL); 4503 if (IS_ERR(ctx)) 4504 return PTR_ERR(ctx); 4505 4506 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l); 4507 4508 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 4509 sattr->ia_mode &= ~current_umask(); 4510 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL); 4511 if (IS_ERR(state)) { 4512 status = PTR_ERR(state); 4513 goto out; 4514 } 4515 out: 4516 nfs4_label_release_security(ilabel); 4517 put_nfs_open_context(ctx); 4518 return status; 4519 } 4520 4521 static int 4522 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype) 4523 { 4524 struct nfs_server *server = NFS_SERVER(dir); 4525 struct nfs_removeargs args = { 4526 .fh = NFS_FH(dir), 4527 .name = *name, 4528 }; 4529 struct nfs_removeres res = { 4530 .server = server, 4531 }; 4532 struct rpc_message msg = { 4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], 4534 .rpc_argp = &args, 4535 .rpc_resp = &res, 4536 }; 4537 unsigned long timestamp = jiffies; 4538 int status; 4539 4540 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); 4541 if (status == 0) { 4542 spin_lock(&dir->i_lock); 4543 update_changeattr_locked(dir, &res.cinfo, timestamp, 0); 4544 /* Removing a directory decrements nlink in the parent */ 4545 if (ftype == NF4DIR && dir->i_nlink > 2) 4546 nfs4_dec_nlink_locked(dir); 4547 spin_unlock(&dir->i_lock); 4548 } 4549 return status; 4550 } 4551 4552 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry) 4553 { 4554 struct nfs4_exception exception = { 4555 .interruptible = true, 4556 }; 4557 struct inode *inode = d_inode(dentry); 4558 int err; 4559 4560 if (inode) { 4561 if (inode->i_nlink == 1) 4562 nfs4_inode_return_delegation(inode); 4563 else 4564 nfs4_inode_make_writeable(inode); 4565 } 4566 do { 4567 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG); 4568 trace_nfs4_remove(dir, &dentry->d_name, err); 4569 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4570 &exception); 4571 } while (exception.retry); 4572 return err; 4573 } 4574 4575 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name) 4576 { 4577 struct nfs4_exception exception = { 4578 .interruptible = true, 4579 }; 4580 int err; 4581 4582 do { 4583 err = _nfs4_proc_remove(dir, name, NF4DIR); 4584 trace_nfs4_remove(dir, name, err); 4585 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4586 &exception); 4587 } while (exception.retry); 4588 return err; 4589 } 4590 4591 static void nfs4_proc_unlink_setup(struct rpc_message *msg, 4592 struct dentry *dentry, 4593 struct inode *inode) 4594 { 4595 struct nfs_removeargs *args = msg->rpc_argp; 4596 struct nfs_removeres *res = msg->rpc_resp; 4597 4598 res->server = NFS_SB(dentry->d_sb); 4599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; 4600 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0); 4601 4602 nfs_fattr_init(res->dir_attr); 4603 4604 if (inode) 4605 nfs4_inode_return_delegation(inode); 4606 } 4607 4608 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) 4609 { 4610 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client, 4611 &data->args.seq_args, 4612 &data->res.seq_res, 4613 task); 4614 } 4615 4616 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) 4617 { 4618 struct nfs_unlinkdata *data = task->tk_calldata; 4619 struct nfs_removeres *res = &data->res; 4620 4621 if (!nfs4_sequence_done(task, &res->seq_res)) 4622 return 0; 4623 if (nfs4_async_handle_error(task, res->server, NULL, 4624 &data->timeout) == -EAGAIN) 4625 return 0; 4626 if (task->tk_status == 0) 4627 update_changeattr(dir, &res->cinfo, 4628 res->dir_attr->time_start, 0); 4629 return 1; 4630 } 4631 4632 static void nfs4_proc_rename_setup(struct rpc_message *msg, 4633 struct dentry *old_dentry, 4634 struct dentry *new_dentry) 4635 { 4636 struct nfs_renameargs *arg = msg->rpc_argp; 4637 struct nfs_renameres *res = msg->rpc_resp; 4638 struct inode *old_inode = d_inode(old_dentry); 4639 struct inode *new_inode = d_inode(new_dentry); 4640 4641 if (old_inode) 4642 nfs4_inode_make_writeable(old_inode); 4643 if (new_inode) 4644 nfs4_inode_return_delegation(new_inode); 4645 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; 4646 res->server = NFS_SB(old_dentry->d_sb); 4647 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0); 4648 } 4649 4650 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) 4651 { 4652 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client, 4653 &data->args.seq_args, 4654 &data->res.seq_res, 4655 task); 4656 } 4657 4658 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, 4659 struct inode *new_dir) 4660 { 4661 struct nfs_renamedata *data = task->tk_calldata; 4662 struct nfs_renameres *res = &data->res; 4663 4664 if (!nfs4_sequence_done(task, &res->seq_res)) 4665 return 0; 4666 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN) 4667 return 0; 4668 4669 if (task->tk_status == 0) { 4670 if (new_dir != old_dir) { 4671 /* Note: If we moved a directory, nlink will change */ 4672 update_changeattr(old_dir, &res->old_cinfo, 4673 res->old_fattr->time_start, 4674 NFS_INO_INVALID_OTHER); 4675 update_changeattr(new_dir, &res->new_cinfo, 4676 res->new_fattr->time_start, 4677 NFS_INO_INVALID_OTHER); 4678 } else 4679 update_changeattr(old_dir, &res->old_cinfo, 4680 res->old_fattr->time_start, 4681 0); 4682 } 4683 return 1; 4684 } 4685 4686 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) 4687 { 4688 struct nfs_server *server = NFS_SERVER(inode); 4689 __u32 bitmask[NFS4_BITMASK_SZ]; 4690 struct nfs4_link_arg arg = { 4691 .fh = NFS_FH(inode), 4692 .dir_fh = NFS_FH(dir), 4693 .name = name, 4694 .bitmask = bitmask, 4695 }; 4696 struct nfs4_link_res res = { 4697 .server = server, 4698 .label = NULL, 4699 }; 4700 struct rpc_message msg = { 4701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], 4702 .rpc_argp = &arg, 4703 .rpc_resp = &res, 4704 }; 4705 int status = -ENOMEM; 4706 4707 res.fattr = nfs_alloc_fattr(); 4708 if (res.fattr == NULL) 4709 goto out; 4710 4711 res.label = nfs4_label_alloc(server, GFP_KERNEL); 4712 if (IS_ERR(res.label)) { 4713 status = PTR_ERR(res.label); 4714 goto out; 4715 } 4716 4717 nfs4_inode_make_writeable(inode); 4718 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode); 4719 4720 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 4721 if (!status) { 4722 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0); 4723 status = nfs_post_op_update_inode(inode, res.fattr); 4724 if (!status) 4725 nfs_setsecurity(inode, res.fattr, res.label); 4726 } 4727 4728 4729 nfs4_label_free(res.label); 4730 4731 out: 4732 nfs_free_fattr(res.fattr); 4733 return status; 4734 } 4735 4736 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) 4737 { 4738 struct nfs4_exception exception = { 4739 .interruptible = true, 4740 }; 4741 int err; 4742 do { 4743 err = nfs4_handle_exception(NFS_SERVER(inode), 4744 _nfs4_proc_link(inode, dir, name), 4745 &exception); 4746 } while (exception.retry); 4747 return err; 4748 } 4749 4750 struct nfs4_createdata { 4751 struct rpc_message msg; 4752 struct nfs4_create_arg arg; 4753 struct nfs4_create_res res; 4754 struct nfs_fh fh; 4755 struct nfs_fattr fattr; 4756 struct nfs4_label *label; 4757 }; 4758 4759 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, 4760 const struct qstr *name, struct iattr *sattr, u32 ftype) 4761 { 4762 struct nfs4_createdata *data; 4763 4764 data = kzalloc(sizeof(*data), GFP_KERNEL); 4765 if (data != NULL) { 4766 struct nfs_server *server = NFS_SERVER(dir); 4767 4768 data->label = nfs4_label_alloc(server, GFP_KERNEL); 4769 if (IS_ERR(data->label)) 4770 goto out_free; 4771 4772 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; 4773 data->msg.rpc_argp = &data->arg; 4774 data->msg.rpc_resp = &data->res; 4775 data->arg.dir_fh = NFS_FH(dir); 4776 data->arg.server = server; 4777 data->arg.name = name; 4778 data->arg.attrs = sattr; 4779 data->arg.ftype = ftype; 4780 data->arg.bitmask = nfs4_bitmask(server, data->label); 4781 data->arg.umask = current_umask(); 4782 data->res.server = server; 4783 data->res.fh = &data->fh; 4784 data->res.fattr = &data->fattr; 4785 data->res.label = data->label; 4786 nfs_fattr_init(data->res.fattr); 4787 } 4788 return data; 4789 out_free: 4790 kfree(data); 4791 return NULL; 4792 } 4793 4794 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) 4795 { 4796 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, 4797 &data->arg.seq_args, &data->res.seq_res, 1); 4798 if (status == 0) { 4799 spin_lock(&dir->i_lock); 4800 update_changeattr_locked(dir, &data->res.dir_cinfo, 4801 data->res.fattr->time_start, 0); 4802 /* Creating a directory bumps nlink in the parent */ 4803 if (data->arg.ftype == NF4DIR) 4804 nfs4_inc_nlink_locked(dir); 4805 spin_unlock(&dir->i_lock); 4806 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label); 4807 } 4808 return status; 4809 } 4810 4811 static void nfs4_free_createdata(struct nfs4_createdata *data) 4812 { 4813 nfs4_label_free(data->label); 4814 kfree(data); 4815 } 4816 4817 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 4818 struct page *page, unsigned int len, struct iattr *sattr, 4819 struct nfs4_label *label) 4820 { 4821 struct nfs4_createdata *data; 4822 int status = -ENAMETOOLONG; 4823 4824 if (len > NFS4_MAXPATHLEN) 4825 goto out; 4826 4827 status = -ENOMEM; 4828 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); 4829 if (data == NULL) 4830 goto out; 4831 4832 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; 4833 data->arg.u.symlink.pages = &page; 4834 data->arg.u.symlink.len = len; 4835 data->arg.label = label; 4836 4837 status = nfs4_do_create(dir, dentry, data); 4838 4839 nfs4_free_createdata(data); 4840 out: 4841 return status; 4842 } 4843 4844 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 4845 struct page *page, unsigned int len, struct iattr *sattr) 4846 { 4847 struct nfs4_exception exception = { 4848 .interruptible = true, 4849 }; 4850 struct nfs4_label l, *label = NULL; 4851 int err; 4852 4853 label = nfs4_label_init_security(dir, dentry, sattr, &l); 4854 4855 do { 4856 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label); 4857 trace_nfs4_symlink(dir, &dentry->d_name, err); 4858 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4859 &exception); 4860 } while (exception.retry); 4861 4862 nfs4_label_release_security(label); 4863 return err; 4864 } 4865 4866 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 4867 struct iattr *sattr, struct nfs4_label *label) 4868 { 4869 struct nfs4_createdata *data; 4870 int status = -ENOMEM; 4871 4872 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); 4873 if (data == NULL) 4874 goto out; 4875 4876 data->arg.label = label; 4877 status = nfs4_do_create(dir, dentry, data); 4878 4879 nfs4_free_createdata(data); 4880 out: 4881 return status; 4882 } 4883 4884 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 4885 struct iattr *sattr) 4886 { 4887 struct nfs_server *server = NFS_SERVER(dir); 4888 struct nfs4_exception exception = { 4889 .interruptible = true, 4890 }; 4891 struct nfs4_label l, *label = NULL; 4892 int err; 4893 4894 label = nfs4_label_init_security(dir, dentry, sattr, &l); 4895 4896 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 4897 sattr->ia_mode &= ~current_umask(); 4898 do { 4899 err = _nfs4_proc_mkdir(dir, dentry, sattr, label); 4900 trace_nfs4_mkdir(dir, &dentry->d_name, err); 4901 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4902 &exception); 4903 } while (exception.retry); 4904 nfs4_label_release_security(label); 4905 4906 return err; 4907 } 4908 4909 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred, 4910 u64 cookie, struct page **pages, unsigned int count, bool plus) 4911 { 4912 struct inode *dir = d_inode(dentry); 4913 struct nfs4_readdir_arg args = { 4914 .fh = NFS_FH(dir), 4915 .pages = pages, 4916 .pgbase = 0, 4917 .count = count, 4918 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask, 4919 .plus = plus, 4920 }; 4921 struct nfs4_readdir_res res; 4922 struct rpc_message msg = { 4923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], 4924 .rpc_argp = &args, 4925 .rpc_resp = &res, 4926 .rpc_cred = cred, 4927 }; 4928 int status; 4929 4930 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__, 4931 dentry, 4932 (unsigned long long)cookie); 4933 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args); 4934 res.pgbase = args.pgbase; 4935 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); 4936 if (status >= 0) { 4937 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE); 4938 status += args.pgbase; 4939 } 4940 4941 nfs_invalidate_atime(dir); 4942 4943 dprintk("%s: returns %d\n", __func__, status); 4944 return status; 4945 } 4946 4947 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred, 4948 u64 cookie, struct page **pages, unsigned int count, bool plus) 4949 { 4950 struct nfs4_exception exception = { 4951 .interruptible = true, 4952 }; 4953 int err; 4954 do { 4955 err = _nfs4_proc_readdir(dentry, cred, cookie, 4956 pages, count, plus); 4957 trace_nfs4_readdir(d_inode(dentry), err); 4958 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err, 4959 &exception); 4960 } while (exception.retry); 4961 return err; 4962 } 4963 4964 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 4965 struct iattr *sattr, struct nfs4_label *label, dev_t rdev) 4966 { 4967 struct nfs4_createdata *data; 4968 int mode = sattr->ia_mode; 4969 int status = -ENOMEM; 4970 4971 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); 4972 if (data == NULL) 4973 goto out; 4974 4975 if (S_ISFIFO(mode)) 4976 data->arg.ftype = NF4FIFO; 4977 else if (S_ISBLK(mode)) { 4978 data->arg.ftype = NF4BLK; 4979 data->arg.u.device.specdata1 = MAJOR(rdev); 4980 data->arg.u.device.specdata2 = MINOR(rdev); 4981 } 4982 else if (S_ISCHR(mode)) { 4983 data->arg.ftype = NF4CHR; 4984 data->arg.u.device.specdata1 = MAJOR(rdev); 4985 data->arg.u.device.specdata2 = MINOR(rdev); 4986 } else if (!S_ISSOCK(mode)) { 4987 status = -EINVAL; 4988 goto out_free; 4989 } 4990 4991 data->arg.label = label; 4992 status = nfs4_do_create(dir, dentry, data); 4993 out_free: 4994 nfs4_free_createdata(data); 4995 out: 4996 return status; 4997 } 4998 4999 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 5000 struct iattr *sattr, dev_t rdev) 5001 { 5002 struct nfs_server *server = NFS_SERVER(dir); 5003 struct nfs4_exception exception = { 5004 .interruptible = true, 5005 }; 5006 struct nfs4_label l, *label = NULL; 5007 int err; 5008 5009 label = nfs4_label_init_security(dir, dentry, sattr, &l); 5010 5011 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 5012 sattr->ia_mode &= ~current_umask(); 5013 do { 5014 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); 5015 trace_nfs4_mknod(dir, &dentry->d_name, err); 5016 err = nfs4_handle_exception(NFS_SERVER(dir), err, 5017 &exception); 5018 } while (exception.retry); 5019 5020 nfs4_label_release_security(label); 5021 5022 return err; 5023 } 5024 5025 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, 5026 struct nfs_fsstat *fsstat) 5027 { 5028 struct nfs4_statfs_arg args = { 5029 .fh = fhandle, 5030 .bitmask = server->attr_bitmask, 5031 }; 5032 struct nfs4_statfs_res res = { 5033 .fsstat = fsstat, 5034 }; 5035 struct rpc_message msg = { 5036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], 5037 .rpc_argp = &args, 5038 .rpc_resp = &res, 5039 }; 5040 5041 nfs_fattr_init(fsstat->fattr); 5042 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 5043 } 5044 5045 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) 5046 { 5047 struct nfs4_exception exception = { 5048 .interruptible = true, 5049 }; 5050 int err; 5051 do { 5052 err = nfs4_handle_exception(server, 5053 _nfs4_proc_statfs(server, fhandle, fsstat), 5054 &exception); 5055 } while (exception.retry); 5056 return err; 5057 } 5058 5059 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, 5060 struct nfs_fsinfo *fsinfo) 5061 { 5062 struct nfs4_fsinfo_arg args = { 5063 .fh = fhandle, 5064 .bitmask = server->attr_bitmask, 5065 }; 5066 struct nfs4_fsinfo_res res = { 5067 .fsinfo = fsinfo, 5068 }; 5069 struct rpc_message msg = { 5070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], 5071 .rpc_argp = &args, 5072 .rpc_resp = &res, 5073 }; 5074 5075 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 5076 } 5077 5078 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 5079 { 5080 struct nfs4_exception exception = { 5081 .interruptible = true, 5082 }; 5083 int err; 5084 5085 do { 5086 err = _nfs4_do_fsinfo(server, fhandle, fsinfo); 5087 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); 5088 if (err == 0) { 5089 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ); 5090 break; 5091 } 5092 err = nfs4_handle_exception(server, err, &exception); 5093 } while (exception.retry); 5094 return err; 5095 } 5096 5097 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 5098 { 5099 int error; 5100 5101 nfs_fattr_init(fsinfo->fattr); 5102 error = nfs4_do_fsinfo(server, fhandle, fsinfo); 5103 if (error == 0) { 5104 /* block layout checks this! */ 5105 server->pnfs_blksize = fsinfo->blksize; 5106 set_pnfs_layoutdriver(server, fhandle, fsinfo); 5107 } 5108 5109 return error; 5110 } 5111 5112 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 5113 struct nfs_pathconf *pathconf) 5114 { 5115 struct nfs4_pathconf_arg args = { 5116 .fh = fhandle, 5117 .bitmask = server->attr_bitmask, 5118 }; 5119 struct nfs4_pathconf_res res = { 5120 .pathconf = pathconf, 5121 }; 5122 struct rpc_message msg = { 5123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], 5124 .rpc_argp = &args, 5125 .rpc_resp = &res, 5126 }; 5127 5128 /* None of the pathconf attributes are mandatory to implement */ 5129 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { 5130 memset(pathconf, 0, sizeof(*pathconf)); 5131 return 0; 5132 } 5133 5134 nfs_fattr_init(pathconf->fattr); 5135 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 5136 } 5137 5138 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 5139 struct nfs_pathconf *pathconf) 5140 { 5141 struct nfs4_exception exception = { 5142 .interruptible = true, 5143 }; 5144 int err; 5145 5146 do { 5147 err = nfs4_handle_exception(server, 5148 _nfs4_proc_pathconf(server, fhandle, pathconf), 5149 &exception); 5150 } while (exception.retry); 5151 return err; 5152 } 5153 5154 int nfs4_set_rw_stateid(nfs4_stateid *stateid, 5155 const struct nfs_open_context *ctx, 5156 const struct nfs_lock_context *l_ctx, 5157 fmode_t fmode) 5158 { 5159 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL); 5160 } 5161 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); 5162 5163 static bool nfs4_stateid_is_current(nfs4_stateid *stateid, 5164 const struct nfs_open_context *ctx, 5165 const struct nfs_lock_context *l_ctx, 5166 fmode_t fmode) 5167 { 5168 nfs4_stateid _current_stateid; 5169 5170 /* If the current stateid represents a lost lock, then exit */ 5171 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO) 5172 return true; 5173 return nfs4_stateid_match(stateid, &_current_stateid); 5174 } 5175 5176 static bool nfs4_error_stateid_expired(int err) 5177 { 5178 switch (err) { 5179 case -NFS4ERR_DELEG_REVOKED: 5180 case -NFS4ERR_ADMIN_REVOKED: 5181 case -NFS4ERR_BAD_STATEID: 5182 case -NFS4ERR_STALE_STATEID: 5183 case -NFS4ERR_OLD_STATEID: 5184 case -NFS4ERR_OPENMODE: 5185 case -NFS4ERR_EXPIRED: 5186 return true; 5187 } 5188 return false; 5189 } 5190 5191 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) 5192 { 5193 struct nfs_server *server = NFS_SERVER(hdr->inode); 5194 5195 trace_nfs4_read(hdr, task->tk_status); 5196 if (task->tk_status < 0) { 5197 struct nfs4_exception exception = { 5198 .inode = hdr->inode, 5199 .state = hdr->args.context->state, 5200 .stateid = &hdr->args.stateid, 5201 }; 5202 task->tk_status = nfs4_async_handle_exception(task, 5203 server, task->tk_status, &exception); 5204 if (exception.retry) { 5205 rpc_restart_call_prepare(task); 5206 return -EAGAIN; 5207 } 5208 } 5209 5210 if (task->tk_status > 0) 5211 renew_lease(server, hdr->timestamp); 5212 return 0; 5213 } 5214 5215 static bool nfs4_read_stateid_changed(struct rpc_task *task, 5216 struct nfs_pgio_args *args) 5217 { 5218 5219 if (!nfs4_error_stateid_expired(task->tk_status) || 5220 nfs4_stateid_is_current(&args->stateid, 5221 args->context, 5222 args->lock_context, 5223 FMODE_READ)) 5224 return false; 5225 rpc_restart_call_prepare(task); 5226 return true; 5227 } 5228 5229 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 5230 { 5231 5232 dprintk("--> %s\n", __func__); 5233 5234 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 5235 return -EAGAIN; 5236 if (nfs4_read_stateid_changed(task, &hdr->args)) 5237 return -EAGAIN; 5238 if (task->tk_status > 0) 5239 nfs_invalidate_atime(hdr->inode); 5240 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 5241 nfs4_read_done_cb(task, hdr); 5242 } 5243 5244 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, 5245 struct rpc_message *msg) 5246 { 5247 hdr->timestamp = jiffies; 5248 if (!hdr->pgio_done_cb) 5249 hdr->pgio_done_cb = nfs4_read_done_cb; 5250 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; 5251 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0); 5252 } 5253 5254 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, 5255 struct nfs_pgio_header *hdr) 5256 { 5257 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client, 5258 &hdr->args.seq_args, 5259 &hdr->res.seq_res, 5260 task)) 5261 return 0; 5262 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, 5263 hdr->args.lock_context, 5264 hdr->rw_mode) == -EIO) 5265 return -EIO; 5266 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) 5267 return -EIO; 5268 return 0; 5269 } 5270 5271 static int nfs4_write_done_cb(struct rpc_task *task, 5272 struct nfs_pgio_header *hdr) 5273 { 5274 struct inode *inode = hdr->inode; 5275 5276 trace_nfs4_write(hdr, task->tk_status); 5277 if (task->tk_status < 0) { 5278 struct nfs4_exception exception = { 5279 .inode = hdr->inode, 5280 .state = hdr->args.context->state, 5281 .stateid = &hdr->args.stateid, 5282 }; 5283 task->tk_status = nfs4_async_handle_exception(task, 5284 NFS_SERVER(inode), task->tk_status, 5285 &exception); 5286 if (exception.retry) { 5287 rpc_restart_call_prepare(task); 5288 return -EAGAIN; 5289 } 5290 } 5291 if (task->tk_status >= 0) { 5292 renew_lease(NFS_SERVER(inode), hdr->timestamp); 5293 nfs_writeback_update_inode(hdr); 5294 } 5295 return 0; 5296 } 5297 5298 static bool nfs4_write_stateid_changed(struct rpc_task *task, 5299 struct nfs_pgio_args *args) 5300 { 5301 5302 if (!nfs4_error_stateid_expired(task->tk_status) || 5303 nfs4_stateid_is_current(&args->stateid, 5304 args->context, 5305 args->lock_context, 5306 FMODE_WRITE)) 5307 return false; 5308 rpc_restart_call_prepare(task); 5309 return true; 5310 } 5311 5312 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 5313 { 5314 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 5315 return -EAGAIN; 5316 if (nfs4_write_stateid_changed(task, &hdr->args)) 5317 return -EAGAIN; 5318 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 5319 nfs4_write_done_cb(task, hdr); 5320 } 5321 5322 static 5323 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) 5324 { 5325 /* Don't request attributes for pNFS or O_DIRECT writes */ 5326 if (hdr->ds_clp != NULL || hdr->dreq != NULL) 5327 return false; 5328 /* Otherwise, request attributes if and only if we don't hold 5329 * a delegation 5330 */ 5331 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; 5332 } 5333 5334 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, 5335 struct rpc_message *msg, 5336 struct rpc_clnt **clnt) 5337 { 5338 struct nfs_server *server = NFS_SERVER(hdr->inode); 5339 5340 if (!nfs4_write_need_cache_consistency_data(hdr)) { 5341 hdr->args.bitmask = NULL; 5342 hdr->res.fattr = NULL; 5343 } else 5344 hdr->args.bitmask = server->cache_consistency_bitmask; 5345 5346 if (!hdr->pgio_done_cb) 5347 hdr->pgio_done_cb = nfs4_write_done_cb; 5348 hdr->res.server = server; 5349 hdr->timestamp = jiffies; 5350 5351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; 5352 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0); 5353 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr); 5354 } 5355 5356 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) 5357 { 5358 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client, 5359 &data->args.seq_args, 5360 &data->res.seq_res, 5361 task); 5362 } 5363 5364 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) 5365 { 5366 struct inode *inode = data->inode; 5367 5368 trace_nfs4_commit(data, task->tk_status); 5369 if (nfs4_async_handle_error(task, NFS_SERVER(inode), 5370 NULL, NULL) == -EAGAIN) { 5371 rpc_restart_call_prepare(task); 5372 return -EAGAIN; 5373 } 5374 return 0; 5375 } 5376 5377 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) 5378 { 5379 if (!nfs4_sequence_done(task, &data->res.seq_res)) 5380 return -EAGAIN; 5381 return data->commit_done_cb(task, data); 5382 } 5383 5384 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg, 5385 struct rpc_clnt **clnt) 5386 { 5387 struct nfs_server *server = NFS_SERVER(data->inode); 5388 5389 if (data->commit_done_cb == NULL) 5390 data->commit_done_cb = nfs4_commit_done_cb; 5391 data->res.server = server; 5392 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; 5393 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); 5394 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg); 5395 } 5396 5397 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args, 5398 struct nfs_commitres *res) 5399 { 5400 struct inode *dst_inode = file_inode(dst); 5401 struct nfs_server *server = NFS_SERVER(dst_inode); 5402 struct rpc_message msg = { 5403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], 5404 .rpc_argp = args, 5405 .rpc_resp = res, 5406 }; 5407 5408 args->fh = NFS_FH(dst_inode); 5409 return nfs4_call_sync(server->client, server, &msg, 5410 &args->seq_args, &res->seq_res, 1); 5411 } 5412 5413 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res) 5414 { 5415 struct nfs_commitargs args = { 5416 .offset = offset, 5417 .count = count, 5418 }; 5419 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst)); 5420 struct nfs4_exception exception = { }; 5421 int status; 5422 5423 do { 5424 status = _nfs4_proc_commit(dst, &args, res); 5425 status = nfs4_handle_exception(dst_server, status, &exception); 5426 } while (exception.retry); 5427 5428 return status; 5429 } 5430 5431 struct nfs4_renewdata { 5432 struct nfs_client *client; 5433 unsigned long timestamp; 5434 }; 5435 5436 /* 5437 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special 5438 * standalone procedure for queueing an asynchronous RENEW. 5439 */ 5440 static void nfs4_renew_release(void *calldata) 5441 { 5442 struct nfs4_renewdata *data = calldata; 5443 struct nfs_client *clp = data->client; 5444 5445 if (refcount_read(&clp->cl_count) > 1) 5446 nfs4_schedule_state_renewal(clp); 5447 nfs_put_client(clp); 5448 kfree(data); 5449 } 5450 5451 static void nfs4_renew_done(struct rpc_task *task, void *calldata) 5452 { 5453 struct nfs4_renewdata *data = calldata; 5454 struct nfs_client *clp = data->client; 5455 unsigned long timestamp = data->timestamp; 5456 5457 trace_nfs4_renew_async(clp, task->tk_status); 5458 switch (task->tk_status) { 5459 case 0: 5460 break; 5461 case -NFS4ERR_LEASE_MOVED: 5462 nfs4_schedule_lease_moved_recovery(clp); 5463 break; 5464 default: 5465 /* Unless we're shutting down, schedule state recovery! */ 5466 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) 5467 return; 5468 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { 5469 nfs4_schedule_lease_recovery(clp); 5470 return; 5471 } 5472 nfs4_schedule_path_down_recovery(clp); 5473 } 5474 do_renew_lease(clp, timestamp); 5475 } 5476 5477 static const struct rpc_call_ops nfs4_renew_ops = { 5478 .rpc_call_done = nfs4_renew_done, 5479 .rpc_release = nfs4_renew_release, 5480 }; 5481 5482 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) 5483 { 5484 struct rpc_message msg = { 5485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], 5486 .rpc_argp = clp, 5487 .rpc_cred = cred, 5488 }; 5489 struct nfs4_renewdata *data; 5490 5491 if (renew_flags == 0) 5492 return 0; 5493 if (!refcount_inc_not_zero(&clp->cl_count)) 5494 return -EIO; 5495 data = kmalloc(sizeof(*data), GFP_NOFS); 5496 if (data == NULL) { 5497 nfs_put_client(clp); 5498 return -ENOMEM; 5499 } 5500 data->client = clp; 5501 data->timestamp = jiffies; 5502 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT, 5503 &nfs4_renew_ops, data); 5504 } 5505 5506 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred) 5507 { 5508 struct rpc_message msg = { 5509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], 5510 .rpc_argp = clp, 5511 .rpc_cred = cred, 5512 }; 5513 unsigned long now = jiffies; 5514 int status; 5515 5516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 5517 if (status < 0) 5518 return status; 5519 do_renew_lease(clp, now); 5520 return 0; 5521 } 5522 5523 static inline int nfs4_server_supports_acls(struct nfs_server *server) 5524 { 5525 return server->caps & NFS_CAP_ACLS; 5526 } 5527 5528 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that 5529 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on 5530 * the stack. 5531 */ 5532 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) 5533 5534 static int buf_to_pages_noslab(const void *buf, size_t buflen, 5535 struct page **pages) 5536 { 5537 struct page *newpage, **spages; 5538 int rc = 0; 5539 size_t len; 5540 spages = pages; 5541 5542 do { 5543 len = min_t(size_t, PAGE_SIZE, buflen); 5544 newpage = alloc_page(GFP_KERNEL); 5545 5546 if (newpage == NULL) 5547 goto unwind; 5548 memcpy(page_address(newpage), buf, len); 5549 buf += len; 5550 buflen -= len; 5551 *pages++ = newpage; 5552 rc++; 5553 } while (buflen != 0); 5554 5555 return rc; 5556 5557 unwind: 5558 for(; rc > 0; rc--) 5559 __free_page(spages[rc-1]); 5560 return -ENOMEM; 5561 } 5562 5563 struct nfs4_cached_acl { 5564 int cached; 5565 size_t len; 5566 char data[]; 5567 }; 5568 5569 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) 5570 { 5571 struct nfs_inode *nfsi = NFS_I(inode); 5572 5573 spin_lock(&inode->i_lock); 5574 kfree(nfsi->nfs4_acl); 5575 nfsi->nfs4_acl = acl; 5576 spin_unlock(&inode->i_lock); 5577 } 5578 5579 static void nfs4_zap_acl_attr(struct inode *inode) 5580 { 5581 nfs4_set_cached_acl(inode, NULL); 5582 } 5583 5584 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) 5585 { 5586 struct nfs_inode *nfsi = NFS_I(inode); 5587 struct nfs4_cached_acl *acl; 5588 int ret = -ENOENT; 5589 5590 spin_lock(&inode->i_lock); 5591 acl = nfsi->nfs4_acl; 5592 if (acl == NULL) 5593 goto out; 5594 if (buf == NULL) /* user is just asking for length */ 5595 goto out_len; 5596 if (acl->cached == 0) 5597 goto out; 5598 ret = -ERANGE; /* see getxattr(2) man page */ 5599 if (acl->len > buflen) 5600 goto out; 5601 memcpy(buf, acl->data, acl->len); 5602 out_len: 5603 ret = acl->len; 5604 out: 5605 spin_unlock(&inode->i_lock); 5606 return ret; 5607 } 5608 5609 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len) 5610 { 5611 struct nfs4_cached_acl *acl; 5612 size_t buflen = sizeof(*acl) + acl_len; 5613 5614 if (buflen <= PAGE_SIZE) { 5615 acl = kmalloc(buflen, GFP_KERNEL); 5616 if (acl == NULL) 5617 goto out; 5618 acl->cached = 1; 5619 _copy_from_pages(acl->data, pages, pgbase, acl_len); 5620 } else { 5621 acl = kmalloc(sizeof(*acl), GFP_KERNEL); 5622 if (acl == NULL) 5623 goto out; 5624 acl->cached = 0; 5625 } 5626 acl->len = acl_len; 5627 out: 5628 nfs4_set_cached_acl(inode, acl); 5629 } 5630 5631 /* 5632 * The getxattr API returns the required buffer length when called with a 5633 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating 5634 * the required buf. On a NULL buf, we send a page of data to the server 5635 * guessing that the ACL request can be serviced by a page. If so, we cache 5636 * up to the page of ACL data, and the 2nd call to getxattr is serviced by 5637 * the cache. If not so, we throw away the page, and cache the required 5638 * length. The next getxattr call will then produce another round trip to 5639 * the server, this time with the input buf of the required size. 5640 */ 5641 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) 5642 { 5643 struct page **pages; 5644 struct nfs_getaclargs args = { 5645 .fh = NFS_FH(inode), 5646 .acl_len = buflen, 5647 }; 5648 struct nfs_getaclres res = { 5649 .acl_len = buflen, 5650 }; 5651 struct rpc_message msg = { 5652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], 5653 .rpc_argp = &args, 5654 .rpc_resp = &res, 5655 }; 5656 unsigned int npages; 5657 int ret = -ENOMEM, i; 5658 struct nfs_server *server = NFS_SERVER(inode); 5659 5660 if (buflen == 0) 5661 buflen = server->rsize; 5662 5663 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1; 5664 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS); 5665 if (!pages) 5666 return -ENOMEM; 5667 5668 args.acl_pages = pages; 5669 5670 for (i = 0; i < npages; i++) { 5671 pages[i] = alloc_page(GFP_KERNEL); 5672 if (!pages[i]) 5673 goto out_free; 5674 } 5675 5676 /* for decoding across pages */ 5677 res.acl_scratch = alloc_page(GFP_KERNEL); 5678 if (!res.acl_scratch) 5679 goto out_free; 5680 5681 args.acl_len = npages * PAGE_SIZE; 5682 5683 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", 5684 __func__, buf, buflen, npages, args.acl_len); 5685 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), 5686 &msg, &args.seq_args, &res.seq_res, 0); 5687 if (ret) 5688 goto out_free; 5689 5690 /* Handle the case where the passed-in buffer is too short */ 5691 if (res.acl_flags & NFS4_ACL_TRUNC) { 5692 /* Did the user only issue a request for the acl length? */ 5693 if (buf == NULL) 5694 goto out_ok; 5695 ret = -ERANGE; 5696 goto out_free; 5697 } 5698 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len); 5699 if (buf) { 5700 if (res.acl_len > buflen) { 5701 ret = -ERANGE; 5702 goto out_free; 5703 } 5704 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); 5705 } 5706 out_ok: 5707 ret = res.acl_len; 5708 out_free: 5709 for (i = 0; i < npages; i++) 5710 if (pages[i]) 5711 __free_page(pages[i]); 5712 if (res.acl_scratch) 5713 __free_page(res.acl_scratch); 5714 kfree(pages); 5715 return ret; 5716 } 5717 5718 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) 5719 { 5720 struct nfs4_exception exception = { 5721 .interruptible = true, 5722 }; 5723 ssize_t ret; 5724 do { 5725 ret = __nfs4_get_acl_uncached(inode, buf, buflen); 5726 trace_nfs4_get_acl(inode, ret); 5727 if (ret >= 0) 5728 break; 5729 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); 5730 } while (exception.retry); 5731 return ret; 5732 } 5733 5734 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) 5735 { 5736 struct nfs_server *server = NFS_SERVER(inode); 5737 int ret; 5738 5739 if (!nfs4_server_supports_acls(server)) 5740 return -EOPNOTSUPP; 5741 ret = nfs_revalidate_inode(server, inode); 5742 if (ret < 0) 5743 return ret; 5744 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) 5745 nfs_zap_acl_cache(inode); 5746 ret = nfs4_read_cached_acl(inode, buf, buflen); 5747 if (ret != -ENOENT) 5748 /* -ENOENT is returned if there is no ACL or if there is an ACL 5749 * but no cached acl data, just the acl length */ 5750 return ret; 5751 return nfs4_get_acl_uncached(inode, buf, buflen); 5752 } 5753 5754 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) 5755 { 5756 struct nfs_server *server = NFS_SERVER(inode); 5757 struct page *pages[NFS4ACL_MAXPAGES]; 5758 struct nfs_setaclargs arg = { 5759 .fh = NFS_FH(inode), 5760 .acl_pages = pages, 5761 .acl_len = buflen, 5762 }; 5763 struct nfs_setaclres res; 5764 struct rpc_message msg = { 5765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], 5766 .rpc_argp = &arg, 5767 .rpc_resp = &res, 5768 }; 5769 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); 5770 int ret, i; 5771 5772 if (!nfs4_server_supports_acls(server)) 5773 return -EOPNOTSUPP; 5774 if (npages > ARRAY_SIZE(pages)) 5775 return -ERANGE; 5776 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages); 5777 if (i < 0) 5778 return i; 5779 nfs4_inode_make_writeable(inode); 5780 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 5781 5782 /* 5783 * Free each page after tx, so the only ref left is 5784 * held by the network stack 5785 */ 5786 for (; i > 0; i--) 5787 put_page(pages[i-1]); 5788 5789 /* 5790 * Acl update can result in inode attribute update. 5791 * so mark the attribute cache invalid. 5792 */ 5793 spin_lock(&inode->i_lock); 5794 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 5795 | NFS_INO_INVALID_CTIME 5796 | NFS_INO_REVAL_FORCED; 5797 spin_unlock(&inode->i_lock); 5798 nfs_access_zap_cache(inode); 5799 nfs_zap_acl_cache(inode); 5800 return ret; 5801 } 5802 5803 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) 5804 { 5805 struct nfs4_exception exception = { }; 5806 int err; 5807 do { 5808 err = __nfs4_proc_set_acl(inode, buf, buflen); 5809 trace_nfs4_set_acl(inode, err); 5810 err = nfs4_handle_exception(NFS_SERVER(inode), err, 5811 &exception); 5812 } while (exception.retry); 5813 return err; 5814 } 5815 5816 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 5817 static int _nfs4_get_security_label(struct inode *inode, void *buf, 5818 size_t buflen) 5819 { 5820 struct nfs_server *server = NFS_SERVER(inode); 5821 struct nfs_fattr fattr; 5822 struct nfs4_label label = {0, 0, buflen, buf}; 5823 5824 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 5825 struct nfs4_getattr_arg arg = { 5826 .fh = NFS_FH(inode), 5827 .bitmask = bitmask, 5828 }; 5829 struct nfs4_getattr_res res = { 5830 .fattr = &fattr, 5831 .label = &label, 5832 .server = server, 5833 }; 5834 struct rpc_message msg = { 5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 5836 .rpc_argp = &arg, 5837 .rpc_resp = &res, 5838 }; 5839 int ret; 5840 5841 nfs_fattr_init(&fattr); 5842 5843 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); 5844 if (ret) 5845 return ret; 5846 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) 5847 return -ENOENT; 5848 if (buflen < label.len) 5849 return -ERANGE; 5850 return 0; 5851 } 5852 5853 static int nfs4_get_security_label(struct inode *inode, void *buf, 5854 size_t buflen) 5855 { 5856 struct nfs4_exception exception = { 5857 .interruptible = true, 5858 }; 5859 int err; 5860 5861 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 5862 return -EOPNOTSUPP; 5863 5864 do { 5865 err = _nfs4_get_security_label(inode, buf, buflen); 5866 trace_nfs4_get_security_label(inode, err); 5867 err = nfs4_handle_exception(NFS_SERVER(inode), err, 5868 &exception); 5869 } while (exception.retry); 5870 return err; 5871 } 5872 5873 static int _nfs4_do_set_security_label(struct inode *inode, 5874 struct nfs4_label *ilabel, 5875 struct nfs_fattr *fattr, 5876 struct nfs4_label *olabel) 5877 { 5878 5879 struct iattr sattr = {0}; 5880 struct nfs_server *server = NFS_SERVER(inode); 5881 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 5882 struct nfs_setattrargs arg = { 5883 .fh = NFS_FH(inode), 5884 .iap = &sattr, 5885 .server = server, 5886 .bitmask = bitmask, 5887 .label = ilabel, 5888 }; 5889 struct nfs_setattrres res = { 5890 .fattr = fattr, 5891 .label = olabel, 5892 .server = server, 5893 }; 5894 struct rpc_message msg = { 5895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 5896 .rpc_argp = &arg, 5897 .rpc_resp = &res, 5898 }; 5899 int status; 5900 5901 nfs4_stateid_copy(&arg.stateid, &zero_stateid); 5902 5903 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 5904 if (status) 5905 dprintk("%s failed: %d\n", __func__, status); 5906 5907 return status; 5908 } 5909 5910 static int nfs4_do_set_security_label(struct inode *inode, 5911 struct nfs4_label *ilabel, 5912 struct nfs_fattr *fattr, 5913 struct nfs4_label *olabel) 5914 { 5915 struct nfs4_exception exception = { }; 5916 int err; 5917 5918 do { 5919 err = _nfs4_do_set_security_label(inode, ilabel, 5920 fattr, olabel); 5921 trace_nfs4_set_security_label(inode, err); 5922 err = nfs4_handle_exception(NFS_SERVER(inode), err, 5923 &exception); 5924 } while (exception.retry); 5925 return err; 5926 } 5927 5928 static int 5929 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen) 5930 { 5931 struct nfs4_label ilabel, *olabel = NULL; 5932 struct nfs_fattr fattr; 5933 int status; 5934 5935 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 5936 return -EOPNOTSUPP; 5937 5938 nfs_fattr_init(&fattr); 5939 5940 ilabel.pi = 0; 5941 ilabel.lfs = 0; 5942 ilabel.label = (char *)buf; 5943 ilabel.len = buflen; 5944 5945 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 5946 if (IS_ERR(olabel)) { 5947 status = -PTR_ERR(olabel); 5948 goto out; 5949 } 5950 5951 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel); 5952 if (status == 0) 5953 nfs_setsecurity(inode, &fattr, olabel); 5954 5955 nfs4_label_free(olabel); 5956 out: 5957 return status; 5958 } 5959 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ 5960 5961 5962 static void nfs4_init_boot_verifier(const struct nfs_client *clp, 5963 nfs4_verifier *bootverf) 5964 { 5965 __be32 verf[2]; 5966 5967 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { 5968 /* An impossible timestamp guarantees this value 5969 * will never match a generated boot time. */ 5970 verf[0] = cpu_to_be32(U32_MAX); 5971 verf[1] = cpu_to_be32(U32_MAX); 5972 } else { 5973 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 5974 u64 ns = ktime_to_ns(nn->boot_time); 5975 5976 verf[0] = cpu_to_be32(ns >> 32); 5977 verf[1] = cpu_to_be32(ns); 5978 } 5979 memcpy(bootverf->data, verf, sizeof(bootverf->data)); 5980 } 5981 5982 static int 5983 nfs4_init_nonuniform_client_string(struct nfs_client *clp) 5984 { 5985 size_t len; 5986 char *str; 5987 5988 if (clp->cl_owner_id != NULL) 5989 return 0; 5990 5991 rcu_read_lock(); 5992 len = 14 + 5993 strlen(clp->cl_rpcclient->cl_nodename) + 5994 1 + 5995 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) + 5996 1; 5997 rcu_read_unlock(); 5998 if (nfs4_client_id_uniquifier[0] != '\0') 5999 len += strlen(nfs4_client_id_uniquifier) + 1; 6000 if (len > NFS4_OPAQUE_LIMIT + 1) 6001 return -EINVAL; 6002 6003 /* 6004 * Since this string is allocated at mount time, and held until the 6005 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 6006 * about a memory-reclaim deadlock. 6007 */ 6008 str = kmalloc(len, GFP_KERNEL); 6009 if (!str) 6010 return -ENOMEM; 6011 6012 rcu_read_lock(); 6013 if (nfs4_client_id_uniquifier[0] != '\0') 6014 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s", 6015 clp->cl_rpcclient->cl_nodename, 6016 nfs4_client_id_uniquifier, 6017 rpc_peeraddr2str(clp->cl_rpcclient, 6018 RPC_DISPLAY_ADDR)); 6019 else 6020 scnprintf(str, len, "Linux NFSv4.0 %s/%s", 6021 clp->cl_rpcclient->cl_nodename, 6022 rpc_peeraddr2str(clp->cl_rpcclient, 6023 RPC_DISPLAY_ADDR)); 6024 rcu_read_unlock(); 6025 6026 clp->cl_owner_id = str; 6027 return 0; 6028 } 6029 6030 static int 6031 nfs4_init_uniquifier_client_string(struct nfs_client *clp) 6032 { 6033 size_t len; 6034 char *str; 6035 6036 len = 10 + 10 + 1 + 10 + 1 + 6037 strlen(nfs4_client_id_uniquifier) + 1 + 6038 strlen(clp->cl_rpcclient->cl_nodename) + 1; 6039 6040 if (len > NFS4_OPAQUE_LIMIT + 1) 6041 return -EINVAL; 6042 6043 /* 6044 * Since this string is allocated at mount time, and held until the 6045 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 6046 * about a memory-reclaim deadlock. 6047 */ 6048 str = kmalloc(len, GFP_KERNEL); 6049 if (!str) 6050 return -ENOMEM; 6051 6052 scnprintf(str, len, "Linux NFSv%u.%u %s/%s", 6053 clp->rpc_ops->version, clp->cl_minorversion, 6054 nfs4_client_id_uniquifier, 6055 clp->cl_rpcclient->cl_nodename); 6056 clp->cl_owner_id = str; 6057 return 0; 6058 } 6059 6060 static int 6061 nfs4_init_uniform_client_string(struct nfs_client *clp) 6062 { 6063 size_t len; 6064 char *str; 6065 6066 if (clp->cl_owner_id != NULL) 6067 return 0; 6068 6069 if (nfs4_client_id_uniquifier[0] != '\0') 6070 return nfs4_init_uniquifier_client_string(clp); 6071 6072 len = 10 + 10 + 1 + 10 + 1 + 6073 strlen(clp->cl_rpcclient->cl_nodename) + 1; 6074 6075 if (len > NFS4_OPAQUE_LIMIT + 1) 6076 return -EINVAL; 6077 6078 /* 6079 * Since this string is allocated at mount time, and held until the 6080 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 6081 * about a memory-reclaim deadlock. 6082 */ 6083 str = kmalloc(len, GFP_KERNEL); 6084 if (!str) 6085 return -ENOMEM; 6086 6087 scnprintf(str, len, "Linux NFSv%u.%u %s", 6088 clp->rpc_ops->version, clp->cl_minorversion, 6089 clp->cl_rpcclient->cl_nodename); 6090 clp->cl_owner_id = str; 6091 return 0; 6092 } 6093 6094 /* 6095 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback 6096 * services. Advertise one based on the address family of the 6097 * clientaddr. 6098 */ 6099 static unsigned int 6100 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) 6101 { 6102 if (strchr(clp->cl_ipaddr, ':') != NULL) 6103 return scnprintf(buf, len, "tcp6"); 6104 else 6105 return scnprintf(buf, len, "tcp"); 6106 } 6107 6108 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) 6109 { 6110 struct nfs4_setclientid *sc = calldata; 6111 6112 if (task->tk_status == 0) 6113 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); 6114 } 6115 6116 static const struct rpc_call_ops nfs4_setclientid_ops = { 6117 .rpc_call_done = nfs4_setclientid_done, 6118 }; 6119 6120 /** 6121 * nfs4_proc_setclientid - Negotiate client ID 6122 * @clp: state data structure 6123 * @program: RPC program for NFSv4 callback service 6124 * @port: IP port number for NFS4 callback service 6125 * @cred: credential to use for this call 6126 * @res: where to place the result 6127 * 6128 * Returns zero, a negative errno, or a negative NFS4ERR status code. 6129 */ 6130 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, 6131 unsigned short port, const struct cred *cred, 6132 struct nfs4_setclientid_res *res) 6133 { 6134 nfs4_verifier sc_verifier; 6135 struct nfs4_setclientid setclientid = { 6136 .sc_verifier = &sc_verifier, 6137 .sc_prog = program, 6138 .sc_clnt = clp, 6139 }; 6140 struct rpc_message msg = { 6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], 6142 .rpc_argp = &setclientid, 6143 .rpc_resp = res, 6144 .rpc_cred = cred, 6145 }; 6146 struct rpc_task_setup task_setup_data = { 6147 .rpc_client = clp->cl_rpcclient, 6148 .rpc_message = &msg, 6149 .callback_ops = &nfs4_setclientid_ops, 6150 .callback_data = &setclientid, 6151 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, 6152 }; 6153 unsigned long now = jiffies; 6154 int status; 6155 6156 /* nfs_client_id4 */ 6157 nfs4_init_boot_verifier(clp, &sc_verifier); 6158 6159 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) 6160 status = nfs4_init_uniform_client_string(clp); 6161 else 6162 status = nfs4_init_nonuniform_client_string(clp); 6163 6164 if (status) 6165 goto out; 6166 6167 /* cb_client4 */ 6168 setclientid.sc_netid_len = 6169 nfs4_init_callback_netid(clp, 6170 setclientid.sc_netid, 6171 sizeof(setclientid.sc_netid)); 6172 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, 6173 sizeof(setclientid.sc_uaddr), "%s.%u.%u", 6174 clp->cl_ipaddr, port >> 8, port & 255); 6175 6176 dprintk("NFS call setclientid auth=%s, '%s'\n", 6177 clp->cl_rpcclient->cl_auth->au_ops->au_name, 6178 clp->cl_owner_id); 6179 6180 status = nfs4_call_sync_custom(&task_setup_data); 6181 if (setclientid.sc_cred) { 6182 kfree(clp->cl_acceptor); 6183 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); 6184 put_rpccred(setclientid.sc_cred); 6185 } 6186 6187 if (status == 0) 6188 do_renew_lease(clp, now); 6189 out: 6190 trace_nfs4_setclientid(clp, status); 6191 dprintk("NFS reply setclientid: %d\n", status); 6192 return status; 6193 } 6194 6195 /** 6196 * nfs4_proc_setclientid_confirm - Confirm client ID 6197 * @clp: state data structure 6198 * @arg: result of a previous SETCLIENTID 6199 * @cred: credential to use for this call 6200 * 6201 * Returns zero, a negative errno, or a negative NFS4ERR status code. 6202 */ 6203 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, 6204 struct nfs4_setclientid_res *arg, 6205 const struct cred *cred) 6206 { 6207 struct rpc_message msg = { 6208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], 6209 .rpc_argp = arg, 6210 .rpc_cred = cred, 6211 }; 6212 int status; 6213 6214 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", 6215 clp->cl_rpcclient->cl_auth->au_ops->au_name, 6216 clp->cl_clientid); 6217 status = rpc_call_sync(clp->cl_rpcclient, &msg, 6218 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 6219 trace_nfs4_setclientid_confirm(clp, status); 6220 dprintk("NFS reply setclientid_confirm: %d\n", status); 6221 return status; 6222 } 6223 6224 struct nfs4_delegreturndata { 6225 struct nfs4_delegreturnargs args; 6226 struct nfs4_delegreturnres res; 6227 struct nfs_fh fh; 6228 nfs4_stateid stateid; 6229 unsigned long timestamp; 6230 struct { 6231 struct nfs4_layoutreturn_args arg; 6232 struct nfs4_layoutreturn_res res; 6233 struct nfs4_xdr_opaque_data ld_private; 6234 u32 roc_barrier; 6235 bool roc; 6236 } lr; 6237 struct nfs_fattr fattr; 6238 int rpc_status; 6239 struct inode *inode; 6240 }; 6241 6242 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) 6243 { 6244 struct nfs4_delegreturndata *data = calldata; 6245 struct nfs4_exception exception = { 6246 .inode = data->inode, 6247 .stateid = &data->stateid, 6248 }; 6249 6250 if (!nfs4_sequence_done(task, &data->res.seq_res)) 6251 return; 6252 6253 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); 6254 6255 /* Handle Layoutreturn errors */ 6256 if (pnfs_roc_done(task, data->inode, 6257 &data->args.lr_args, 6258 &data->res.lr_res, 6259 &data->res.lr_ret) == -EAGAIN) 6260 goto out_restart; 6261 6262 switch (task->tk_status) { 6263 case 0: 6264 renew_lease(data->res.server, data->timestamp); 6265 break; 6266 case -NFS4ERR_ADMIN_REVOKED: 6267 case -NFS4ERR_DELEG_REVOKED: 6268 case -NFS4ERR_EXPIRED: 6269 nfs4_free_revoked_stateid(data->res.server, 6270 data->args.stateid, 6271 task->tk_msg.rpc_cred); 6272 /* Fallthrough */ 6273 case -NFS4ERR_BAD_STATEID: 6274 case -NFS4ERR_STALE_STATEID: 6275 case -ETIMEDOUT: 6276 task->tk_status = 0; 6277 break; 6278 case -NFS4ERR_OLD_STATEID: 6279 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode)) 6280 nfs4_stateid_seqid_inc(&data->stateid); 6281 if (data->args.bitmask) { 6282 data->args.bitmask = NULL; 6283 data->res.fattr = NULL; 6284 } 6285 goto out_restart; 6286 case -NFS4ERR_ACCESS: 6287 if (data->args.bitmask) { 6288 data->args.bitmask = NULL; 6289 data->res.fattr = NULL; 6290 goto out_restart; 6291 } 6292 /* Fallthrough */ 6293 default: 6294 task->tk_status = nfs4_async_handle_exception(task, 6295 data->res.server, task->tk_status, 6296 &exception); 6297 if (exception.retry) 6298 goto out_restart; 6299 } 6300 nfs_delegation_mark_returned(data->inode, data->args.stateid); 6301 data->rpc_status = task->tk_status; 6302 return; 6303 out_restart: 6304 task->tk_status = 0; 6305 rpc_restart_call_prepare(task); 6306 } 6307 6308 static void nfs4_delegreturn_release(void *calldata) 6309 { 6310 struct nfs4_delegreturndata *data = calldata; 6311 struct inode *inode = data->inode; 6312 6313 if (inode) { 6314 if (data->lr.roc) 6315 pnfs_roc_release(&data->lr.arg, &data->lr.res, 6316 data->res.lr_ret); 6317 nfs_post_op_update_inode_force_wcc(inode, &data->fattr); 6318 nfs_iput_and_deactive(inode); 6319 } 6320 kfree(calldata); 6321 } 6322 6323 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) 6324 { 6325 struct nfs4_delegreturndata *d_data; 6326 struct pnfs_layout_hdr *lo; 6327 6328 d_data = (struct nfs4_delegreturndata *)data; 6329 6330 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) { 6331 nfs4_sequence_done(task, &d_data->res.seq_res); 6332 return; 6333 } 6334 6335 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL; 6336 if (lo && !pnfs_layout_is_valid(lo)) { 6337 d_data->args.lr_args = NULL; 6338 d_data->res.lr_res = NULL; 6339 } 6340 6341 nfs4_setup_sequence(d_data->res.server->nfs_client, 6342 &d_data->args.seq_args, 6343 &d_data->res.seq_res, 6344 task); 6345 } 6346 6347 static const struct rpc_call_ops nfs4_delegreturn_ops = { 6348 .rpc_call_prepare = nfs4_delegreturn_prepare, 6349 .rpc_call_done = nfs4_delegreturn_done, 6350 .rpc_release = nfs4_delegreturn_release, 6351 }; 6352 6353 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync) 6354 { 6355 struct nfs4_delegreturndata *data; 6356 struct nfs_server *server = NFS_SERVER(inode); 6357 struct rpc_task *task; 6358 struct rpc_message msg = { 6359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], 6360 .rpc_cred = cred, 6361 }; 6362 struct rpc_task_setup task_setup_data = { 6363 .rpc_client = server->client, 6364 .rpc_message = &msg, 6365 .callback_ops = &nfs4_delegreturn_ops, 6366 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, 6367 }; 6368 int status = 0; 6369 6370 data = kzalloc(sizeof(*data), GFP_NOFS); 6371 if (data == NULL) 6372 return -ENOMEM; 6373 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); 6374 6375 nfs4_state_protect(server->nfs_client, 6376 NFS_SP4_MACH_CRED_CLEANUP, 6377 &task_setup_data.rpc_client, &msg); 6378 6379 data->args.fhandle = &data->fh; 6380 data->args.stateid = &data->stateid; 6381 data->args.bitmask = server->cache_consistency_bitmask; 6382 nfs_copy_fh(&data->fh, NFS_FH(inode)); 6383 nfs4_stateid_copy(&data->stateid, stateid); 6384 data->res.fattr = &data->fattr; 6385 data->res.server = server; 6386 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; 6387 data->lr.arg.ld_private = &data->lr.ld_private; 6388 nfs_fattr_init(data->res.fattr); 6389 data->timestamp = jiffies; 6390 data->rpc_status = 0; 6391 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred); 6392 data->inode = nfs_igrab_and_active(inode); 6393 if (data->inode) { 6394 if (data->lr.roc) { 6395 data->args.lr_args = &data->lr.arg; 6396 data->res.lr_res = &data->lr.res; 6397 } 6398 } else if (data->lr.roc) { 6399 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0); 6400 data->lr.roc = false; 6401 } 6402 6403 task_setup_data.callback_data = data; 6404 msg.rpc_argp = &data->args; 6405 msg.rpc_resp = &data->res; 6406 task = rpc_run_task(&task_setup_data); 6407 if (IS_ERR(task)) 6408 return PTR_ERR(task); 6409 if (!issync) 6410 goto out; 6411 status = rpc_wait_for_completion_task(task); 6412 if (status != 0) 6413 goto out; 6414 status = data->rpc_status; 6415 out: 6416 rpc_put_task(task); 6417 return status; 6418 } 6419 6420 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync) 6421 { 6422 struct nfs_server *server = NFS_SERVER(inode); 6423 struct nfs4_exception exception = { }; 6424 int err; 6425 do { 6426 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); 6427 trace_nfs4_delegreturn(inode, stateid, err); 6428 switch (err) { 6429 case -NFS4ERR_STALE_STATEID: 6430 case -NFS4ERR_EXPIRED: 6431 case 0: 6432 return 0; 6433 } 6434 err = nfs4_handle_exception(server, err, &exception); 6435 } while (exception.retry); 6436 return err; 6437 } 6438 6439 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6440 { 6441 struct inode *inode = state->inode; 6442 struct nfs_server *server = NFS_SERVER(inode); 6443 struct nfs_client *clp = server->nfs_client; 6444 struct nfs_lockt_args arg = { 6445 .fh = NFS_FH(inode), 6446 .fl = request, 6447 }; 6448 struct nfs_lockt_res res = { 6449 .denied = request, 6450 }; 6451 struct rpc_message msg = { 6452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], 6453 .rpc_argp = &arg, 6454 .rpc_resp = &res, 6455 .rpc_cred = state->owner->so_cred, 6456 }; 6457 struct nfs4_lock_state *lsp; 6458 int status; 6459 6460 arg.lock_owner.clientid = clp->cl_clientid; 6461 status = nfs4_set_lock_state(state, request); 6462 if (status != 0) 6463 goto out; 6464 lsp = request->fl_u.nfs4_fl.owner; 6465 arg.lock_owner.id = lsp->ls_seqid.owner_id; 6466 arg.lock_owner.s_dev = server->s_dev; 6467 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 6468 switch (status) { 6469 case 0: 6470 request->fl_type = F_UNLCK; 6471 break; 6472 case -NFS4ERR_DENIED: 6473 status = 0; 6474 } 6475 request->fl_ops->fl_release_private(request); 6476 request->fl_ops = NULL; 6477 out: 6478 return status; 6479 } 6480 6481 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6482 { 6483 struct nfs4_exception exception = { 6484 .interruptible = true, 6485 }; 6486 int err; 6487 6488 do { 6489 err = _nfs4_proc_getlk(state, cmd, request); 6490 trace_nfs4_get_lock(request, state, cmd, err); 6491 err = nfs4_handle_exception(NFS_SERVER(state->inode), err, 6492 &exception); 6493 } while (exception.retry); 6494 return err; 6495 } 6496 6497 /* 6498 * Update the seqid of a lock stateid after receiving 6499 * NFS4ERR_OLD_STATEID 6500 */ 6501 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst, 6502 struct nfs4_lock_state *lsp) 6503 { 6504 struct nfs4_state *state = lsp->ls_state; 6505 bool ret = false; 6506 6507 spin_lock(&state->state_lock); 6508 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid)) 6509 goto out; 6510 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst)) 6511 nfs4_stateid_seqid_inc(dst); 6512 else 6513 dst->seqid = lsp->ls_stateid.seqid; 6514 ret = true; 6515 out: 6516 spin_unlock(&state->state_lock); 6517 return ret; 6518 } 6519 6520 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst, 6521 struct nfs4_lock_state *lsp) 6522 { 6523 struct nfs4_state *state = lsp->ls_state; 6524 bool ret; 6525 6526 spin_lock(&state->state_lock); 6527 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid); 6528 nfs4_stateid_copy(dst, &lsp->ls_stateid); 6529 spin_unlock(&state->state_lock); 6530 return ret; 6531 } 6532 6533 struct nfs4_unlockdata { 6534 struct nfs_locku_args arg; 6535 struct nfs_locku_res res; 6536 struct nfs4_lock_state *lsp; 6537 struct nfs_open_context *ctx; 6538 struct nfs_lock_context *l_ctx; 6539 struct file_lock fl; 6540 struct nfs_server *server; 6541 unsigned long timestamp; 6542 }; 6543 6544 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, 6545 struct nfs_open_context *ctx, 6546 struct nfs4_lock_state *lsp, 6547 struct nfs_seqid *seqid) 6548 { 6549 struct nfs4_unlockdata *p; 6550 struct nfs4_state *state = lsp->ls_state; 6551 struct inode *inode = state->inode; 6552 6553 p = kzalloc(sizeof(*p), GFP_NOFS); 6554 if (p == NULL) 6555 return NULL; 6556 p->arg.fh = NFS_FH(inode); 6557 p->arg.fl = &p->fl; 6558 p->arg.seqid = seqid; 6559 p->res.seqid = seqid; 6560 p->lsp = lsp; 6561 /* Ensure we don't close file until we're done freeing locks! */ 6562 p->ctx = get_nfs_open_context(ctx); 6563 p->l_ctx = nfs_get_lock_context(ctx); 6564 locks_init_lock(&p->fl); 6565 locks_copy_lock(&p->fl, fl); 6566 p->server = NFS_SERVER(inode); 6567 spin_lock(&state->state_lock); 6568 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid); 6569 spin_unlock(&state->state_lock); 6570 return p; 6571 } 6572 6573 static void nfs4_locku_release_calldata(void *data) 6574 { 6575 struct nfs4_unlockdata *calldata = data; 6576 nfs_free_seqid(calldata->arg.seqid); 6577 nfs4_put_lock_state(calldata->lsp); 6578 nfs_put_lock_context(calldata->l_ctx); 6579 put_nfs_open_context(calldata->ctx); 6580 kfree(calldata); 6581 } 6582 6583 static void nfs4_locku_done(struct rpc_task *task, void *data) 6584 { 6585 struct nfs4_unlockdata *calldata = data; 6586 struct nfs4_exception exception = { 6587 .inode = calldata->lsp->ls_state->inode, 6588 .stateid = &calldata->arg.stateid, 6589 }; 6590 6591 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 6592 return; 6593 switch (task->tk_status) { 6594 case 0: 6595 renew_lease(calldata->server, calldata->timestamp); 6596 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl); 6597 if (nfs4_update_lock_stateid(calldata->lsp, 6598 &calldata->res.stateid)) 6599 break; 6600 /* Fall through */ 6601 case -NFS4ERR_ADMIN_REVOKED: 6602 case -NFS4ERR_EXPIRED: 6603 nfs4_free_revoked_stateid(calldata->server, 6604 &calldata->arg.stateid, 6605 task->tk_msg.rpc_cred); 6606 /* Fall through */ 6607 case -NFS4ERR_BAD_STATEID: 6608 case -NFS4ERR_STALE_STATEID: 6609 if (nfs4_sync_lock_stateid(&calldata->arg.stateid, 6610 calldata->lsp)) 6611 rpc_restart_call_prepare(task); 6612 break; 6613 case -NFS4ERR_OLD_STATEID: 6614 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid, 6615 calldata->lsp)) 6616 rpc_restart_call_prepare(task); 6617 break; 6618 default: 6619 task->tk_status = nfs4_async_handle_exception(task, 6620 calldata->server, task->tk_status, 6621 &exception); 6622 if (exception.retry) 6623 rpc_restart_call_prepare(task); 6624 } 6625 nfs_release_seqid(calldata->arg.seqid); 6626 } 6627 6628 static void nfs4_locku_prepare(struct rpc_task *task, void *data) 6629 { 6630 struct nfs4_unlockdata *calldata = data; 6631 6632 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) && 6633 nfs_async_iocounter_wait(task, calldata->l_ctx)) 6634 return; 6635 6636 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 6637 goto out_wait; 6638 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { 6639 /* Note: exit _without_ running nfs4_locku_done */ 6640 goto out_no_action; 6641 } 6642 calldata->timestamp = jiffies; 6643 if (nfs4_setup_sequence(calldata->server->nfs_client, 6644 &calldata->arg.seq_args, 6645 &calldata->res.seq_res, 6646 task) != 0) 6647 nfs_release_seqid(calldata->arg.seqid); 6648 return; 6649 out_no_action: 6650 task->tk_action = NULL; 6651 out_wait: 6652 nfs4_sequence_done(task, &calldata->res.seq_res); 6653 } 6654 6655 static const struct rpc_call_ops nfs4_locku_ops = { 6656 .rpc_call_prepare = nfs4_locku_prepare, 6657 .rpc_call_done = nfs4_locku_done, 6658 .rpc_release = nfs4_locku_release_calldata, 6659 }; 6660 6661 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, 6662 struct nfs_open_context *ctx, 6663 struct nfs4_lock_state *lsp, 6664 struct nfs_seqid *seqid) 6665 { 6666 struct nfs4_unlockdata *data; 6667 struct rpc_message msg = { 6668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], 6669 .rpc_cred = ctx->cred, 6670 }; 6671 struct rpc_task_setup task_setup_data = { 6672 .rpc_client = NFS_CLIENT(lsp->ls_state->inode), 6673 .rpc_message = &msg, 6674 .callback_ops = &nfs4_locku_ops, 6675 .workqueue = nfsiod_workqueue, 6676 .flags = RPC_TASK_ASYNC, 6677 }; 6678 6679 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client, 6680 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg); 6681 6682 /* Ensure this is an unlock - when canceling a lock, the 6683 * canceled lock is passed in, and it won't be an unlock. 6684 */ 6685 fl->fl_type = F_UNLCK; 6686 if (fl->fl_flags & FL_CLOSE) 6687 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags); 6688 6689 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); 6690 if (data == NULL) { 6691 nfs_free_seqid(seqid); 6692 return ERR_PTR(-ENOMEM); 6693 } 6694 6695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0); 6696 msg.rpc_argp = &data->arg; 6697 msg.rpc_resp = &data->res; 6698 task_setup_data.callback_data = data; 6699 return rpc_run_task(&task_setup_data); 6700 } 6701 6702 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) 6703 { 6704 struct inode *inode = state->inode; 6705 struct nfs4_state_owner *sp = state->owner; 6706 struct nfs_inode *nfsi = NFS_I(inode); 6707 struct nfs_seqid *seqid; 6708 struct nfs4_lock_state *lsp; 6709 struct rpc_task *task; 6710 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 6711 int status = 0; 6712 unsigned char fl_flags = request->fl_flags; 6713 6714 status = nfs4_set_lock_state(state, request); 6715 /* Unlock _before_ we do the RPC call */ 6716 request->fl_flags |= FL_EXISTS; 6717 /* Exclude nfs_delegation_claim_locks() */ 6718 mutex_lock(&sp->so_delegreturn_mutex); 6719 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ 6720 down_read(&nfsi->rwsem); 6721 if (locks_lock_inode_wait(inode, request) == -ENOENT) { 6722 up_read(&nfsi->rwsem); 6723 mutex_unlock(&sp->so_delegreturn_mutex); 6724 goto out; 6725 } 6726 up_read(&nfsi->rwsem); 6727 mutex_unlock(&sp->so_delegreturn_mutex); 6728 if (status != 0) 6729 goto out; 6730 /* Is this a delegated lock? */ 6731 lsp = request->fl_u.nfs4_fl.owner; 6732 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) 6733 goto out; 6734 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid; 6735 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); 6736 status = -ENOMEM; 6737 if (IS_ERR(seqid)) 6738 goto out; 6739 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); 6740 status = PTR_ERR(task); 6741 if (IS_ERR(task)) 6742 goto out; 6743 status = rpc_wait_for_completion_task(task); 6744 rpc_put_task(task); 6745 out: 6746 request->fl_flags = fl_flags; 6747 trace_nfs4_unlock(request, state, F_SETLK, status); 6748 return status; 6749 } 6750 6751 struct nfs4_lockdata { 6752 struct nfs_lock_args arg; 6753 struct nfs_lock_res res; 6754 struct nfs4_lock_state *lsp; 6755 struct nfs_open_context *ctx; 6756 struct file_lock fl; 6757 unsigned long timestamp; 6758 int rpc_status; 6759 int cancelled; 6760 struct nfs_server *server; 6761 }; 6762 6763 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, 6764 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, 6765 gfp_t gfp_mask) 6766 { 6767 struct nfs4_lockdata *p; 6768 struct inode *inode = lsp->ls_state->inode; 6769 struct nfs_server *server = NFS_SERVER(inode); 6770 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 6771 6772 p = kzalloc(sizeof(*p), gfp_mask); 6773 if (p == NULL) 6774 return NULL; 6775 6776 p->arg.fh = NFS_FH(inode); 6777 p->arg.fl = &p->fl; 6778 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); 6779 if (IS_ERR(p->arg.open_seqid)) 6780 goto out_free; 6781 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 6782 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask); 6783 if (IS_ERR(p->arg.lock_seqid)) 6784 goto out_free_seqid; 6785 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; 6786 p->arg.lock_owner.id = lsp->ls_seqid.owner_id; 6787 p->arg.lock_owner.s_dev = server->s_dev; 6788 p->res.lock_seqid = p->arg.lock_seqid; 6789 p->lsp = lsp; 6790 p->server = server; 6791 p->ctx = get_nfs_open_context(ctx); 6792 locks_init_lock(&p->fl); 6793 locks_copy_lock(&p->fl, fl); 6794 return p; 6795 out_free_seqid: 6796 nfs_free_seqid(p->arg.open_seqid); 6797 out_free: 6798 kfree(p); 6799 return NULL; 6800 } 6801 6802 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) 6803 { 6804 struct nfs4_lockdata *data = calldata; 6805 struct nfs4_state *state = data->lsp->ls_state; 6806 6807 dprintk("%s: begin!\n", __func__); 6808 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) 6809 goto out_wait; 6810 /* Do we need to do an open_to_lock_owner? */ 6811 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) { 6812 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { 6813 goto out_release_lock_seqid; 6814 } 6815 nfs4_stateid_copy(&data->arg.open_stateid, 6816 &state->open_stateid); 6817 data->arg.new_lock_owner = 1; 6818 data->res.open_seqid = data->arg.open_seqid; 6819 } else { 6820 data->arg.new_lock_owner = 0; 6821 nfs4_stateid_copy(&data->arg.lock_stateid, 6822 &data->lsp->ls_stateid); 6823 } 6824 if (!nfs4_valid_open_stateid(state)) { 6825 data->rpc_status = -EBADF; 6826 task->tk_action = NULL; 6827 goto out_release_open_seqid; 6828 } 6829 data->timestamp = jiffies; 6830 if (nfs4_setup_sequence(data->server->nfs_client, 6831 &data->arg.seq_args, 6832 &data->res.seq_res, 6833 task) == 0) 6834 return; 6835 out_release_open_seqid: 6836 nfs_release_seqid(data->arg.open_seqid); 6837 out_release_lock_seqid: 6838 nfs_release_seqid(data->arg.lock_seqid); 6839 out_wait: 6840 nfs4_sequence_done(task, &data->res.seq_res); 6841 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); 6842 } 6843 6844 static void nfs4_lock_done(struct rpc_task *task, void *calldata) 6845 { 6846 struct nfs4_lockdata *data = calldata; 6847 struct nfs4_lock_state *lsp = data->lsp; 6848 6849 dprintk("%s: begin!\n", __func__); 6850 6851 if (!nfs4_sequence_done(task, &data->res.seq_res)) 6852 return; 6853 6854 data->rpc_status = task->tk_status; 6855 switch (task->tk_status) { 6856 case 0: 6857 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)), 6858 data->timestamp); 6859 if (data->arg.new_lock && !data->cancelled) { 6860 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS); 6861 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) 6862 goto out_restart; 6863 } 6864 if (data->arg.new_lock_owner != 0) { 6865 nfs_confirm_seqid(&lsp->ls_seqid, 0); 6866 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid); 6867 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 6868 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid)) 6869 goto out_restart; 6870 break; 6871 case -NFS4ERR_BAD_STATEID: 6872 case -NFS4ERR_OLD_STATEID: 6873 case -NFS4ERR_STALE_STATEID: 6874 case -NFS4ERR_EXPIRED: 6875 if (data->arg.new_lock_owner != 0) { 6876 if (!nfs4_stateid_match(&data->arg.open_stateid, 6877 &lsp->ls_state->open_stateid)) 6878 goto out_restart; 6879 } else if (!nfs4_stateid_match(&data->arg.lock_stateid, 6880 &lsp->ls_stateid)) 6881 goto out_restart; 6882 } 6883 out_done: 6884 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); 6885 return; 6886 out_restart: 6887 if (!data->cancelled) 6888 rpc_restart_call_prepare(task); 6889 goto out_done; 6890 } 6891 6892 static void nfs4_lock_release(void *calldata) 6893 { 6894 struct nfs4_lockdata *data = calldata; 6895 6896 dprintk("%s: begin!\n", __func__); 6897 nfs_free_seqid(data->arg.open_seqid); 6898 if (data->cancelled && data->rpc_status == 0) { 6899 struct rpc_task *task; 6900 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, 6901 data->arg.lock_seqid); 6902 if (!IS_ERR(task)) 6903 rpc_put_task_async(task); 6904 dprintk("%s: cancelling lock!\n", __func__); 6905 } else 6906 nfs_free_seqid(data->arg.lock_seqid); 6907 nfs4_put_lock_state(data->lsp); 6908 put_nfs_open_context(data->ctx); 6909 kfree(data); 6910 dprintk("%s: done!\n", __func__); 6911 } 6912 6913 static const struct rpc_call_ops nfs4_lock_ops = { 6914 .rpc_call_prepare = nfs4_lock_prepare, 6915 .rpc_call_done = nfs4_lock_done, 6916 .rpc_release = nfs4_lock_release, 6917 }; 6918 6919 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) 6920 { 6921 switch (error) { 6922 case -NFS4ERR_ADMIN_REVOKED: 6923 case -NFS4ERR_EXPIRED: 6924 case -NFS4ERR_BAD_STATEID: 6925 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 6926 if (new_lock_owner != 0 || 6927 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) 6928 nfs4_schedule_stateid_recovery(server, lsp->ls_state); 6929 break; 6930 case -NFS4ERR_STALE_STATEID: 6931 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 6932 nfs4_schedule_lease_recovery(server->nfs_client); 6933 } 6934 } 6935 6936 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) 6937 { 6938 struct nfs4_lockdata *data; 6939 struct rpc_task *task; 6940 struct rpc_message msg = { 6941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], 6942 .rpc_cred = state->owner->so_cred, 6943 }; 6944 struct rpc_task_setup task_setup_data = { 6945 .rpc_client = NFS_CLIENT(state->inode), 6946 .rpc_message = &msg, 6947 .callback_ops = &nfs4_lock_ops, 6948 .workqueue = nfsiod_workqueue, 6949 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, 6950 }; 6951 int ret; 6952 6953 dprintk("%s: begin!\n", __func__); 6954 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), 6955 fl->fl_u.nfs4_fl.owner, 6956 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS); 6957 if (data == NULL) 6958 return -ENOMEM; 6959 if (IS_SETLKW(cmd)) 6960 data->arg.block = 1; 6961 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 6962 recovery_type > NFS_LOCK_NEW); 6963 msg.rpc_argp = &data->arg; 6964 msg.rpc_resp = &data->res; 6965 task_setup_data.callback_data = data; 6966 if (recovery_type > NFS_LOCK_NEW) { 6967 if (recovery_type == NFS_LOCK_RECLAIM) 6968 data->arg.reclaim = NFS_LOCK_RECLAIM; 6969 } else 6970 data->arg.new_lock = 1; 6971 task = rpc_run_task(&task_setup_data); 6972 if (IS_ERR(task)) 6973 return PTR_ERR(task); 6974 ret = rpc_wait_for_completion_task(task); 6975 if (ret == 0) { 6976 ret = data->rpc_status; 6977 if (ret) 6978 nfs4_handle_setlk_error(data->server, data->lsp, 6979 data->arg.new_lock_owner, ret); 6980 } else 6981 data->cancelled = true; 6982 rpc_put_task(task); 6983 dprintk("%s: done, ret = %d!\n", __func__, ret); 6984 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret); 6985 return ret; 6986 } 6987 6988 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) 6989 { 6990 struct nfs_server *server = NFS_SERVER(state->inode); 6991 struct nfs4_exception exception = { 6992 .inode = state->inode, 6993 }; 6994 int err; 6995 6996 do { 6997 /* Cache the lock if possible... */ 6998 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 6999 return 0; 7000 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); 7001 if (err != -NFS4ERR_DELAY) 7002 break; 7003 nfs4_handle_exception(server, err, &exception); 7004 } while (exception.retry); 7005 return err; 7006 } 7007 7008 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) 7009 { 7010 struct nfs_server *server = NFS_SERVER(state->inode); 7011 struct nfs4_exception exception = { 7012 .inode = state->inode, 7013 }; 7014 int err; 7015 7016 err = nfs4_set_lock_state(state, request); 7017 if (err != 0) 7018 return err; 7019 if (!recover_lost_locks) { 7020 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); 7021 return 0; 7022 } 7023 do { 7024 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 7025 return 0; 7026 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); 7027 switch (err) { 7028 default: 7029 goto out; 7030 case -NFS4ERR_GRACE: 7031 case -NFS4ERR_DELAY: 7032 nfs4_handle_exception(server, err, &exception); 7033 err = 0; 7034 } 7035 } while (exception.retry); 7036 out: 7037 return err; 7038 } 7039 7040 #if defined(CONFIG_NFS_V4_1) 7041 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) 7042 { 7043 struct nfs4_lock_state *lsp; 7044 int status; 7045 7046 status = nfs4_set_lock_state(state, request); 7047 if (status != 0) 7048 return status; 7049 lsp = request->fl_u.nfs4_fl.owner; 7050 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) || 7051 test_bit(NFS_LOCK_LOST, &lsp->ls_flags)) 7052 return 0; 7053 return nfs4_lock_expired(state, request); 7054 } 7055 #endif 7056 7057 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7058 { 7059 struct nfs_inode *nfsi = NFS_I(state->inode); 7060 struct nfs4_state_owner *sp = state->owner; 7061 unsigned char fl_flags = request->fl_flags; 7062 int status; 7063 7064 request->fl_flags |= FL_ACCESS; 7065 status = locks_lock_inode_wait(state->inode, request); 7066 if (status < 0) 7067 goto out; 7068 mutex_lock(&sp->so_delegreturn_mutex); 7069 down_read(&nfsi->rwsem); 7070 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { 7071 /* Yes: cache locks! */ 7072 /* ...but avoid races with delegation recall... */ 7073 request->fl_flags = fl_flags & ~FL_SLEEP; 7074 status = locks_lock_inode_wait(state->inode, request); 7075 up_read(&nfsi->rwsem); 7076 mutex_unlock(&sp->so_delegreturn_mutex); 7077 goto out; 7078 } 7079 up_read(&nfsi->rwsem); 7080 mutex_unlock(&sp->so_delegreturn_mutex); 7081 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); 7082 out: 7083 request->fl_flags = fl_flags; 7084 return status; 7085 } 7086 7087 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7088 { 7089 struct nfs4_exception exception = { 7090 .state = state, 7091 .inode = state->inode, 7092 .interruptible = true, 7093 }; 7094 int err; 7095 7096 do { 7097 err = _nfs4_proc_setlk(state, cmd, request); 7098 if (err == -NFS4ERR_DENIED) 7099 err = -EAGAIN; 7100 err = nfs4_handle_exception(NFS_SERVER(state->inode), 7101 err, &exception); 7102 } while (exception.retry); 7103 return err; 7104 } 7105 7106 #define NFS4_LOCK_MINTIMEOUT (1 * HZ) 7107 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) 7108 7109 static int 7110 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd, 7111 struct file_lock *request) 7112 { 7113 int status = -ERESTARTSYS; 7114 unsigned long timeout = NFS4_LOCK_MINTIMEOUT; 7115 7116 while(!signalled()) { 7117 status = nfs4_proc_setlk(state, cmd, request); 7118 if ((status != -EAGAIN) || IS_SETLK(cmd)) 7119 break; 7120 freezable_schedule_timeout_interruptible(timeout); 7121 timeout *= 2; 7122 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout); 7123 status = -ERESTARTSYS; 7124 } 7125 return status; 7126 } 7127 7128 #ifdef CONFIG_NFS_V4_1 7129 struct nfs4_lock_waiter { 7130 struct task_struct *task; 7131 struct inode *inode; 7132 struct nfs_lowner *owner; 7133 }; 7134 7135 static int 7136 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key) 7137 { 7138 int ret; 7139 struct nfs4_lock_waiter *waiter = wait->private; 7140 7141 /* NULL key means to wake up everyone */ 7142 if (key) { 7143 struct cb_notify_lock_args *cbnl = key; 7144 struct nfs_lowner *lowner = &cbnl->cbnl_owner, 7145 *wowner = waiter->owner; 7146 7147 /* Only wake if the callback was for the same owner. */ 7148 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev) 7149 return 0; 7150 7151 /* Make sure it's for the right inode */ 7152 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh)) 7153 return 0; 7154 } 7155 7156 /* override "private" so we can use default_wake_function */ 7157 wait->private = waiter->task; 7158 ret = woken_wake_function(wait, mode, flags, key); 7159 if (ret) 7160 list_del_init(&wait->entry); 7161 wait->private = waiter; 7162 return ret; 7163 } 7164 7165 static int 7166 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7167 { 7168 int status = -ERESTARTSYS; 7169 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; 7170 struct nfs_server *server = NFS_SERVER(state->inode); 7171 struct nfs_client *clp = server->nfs_client; 7172 wait_queue_head_t *q = &clp->cl_lock_waitq; 7173 struct nfs_lowner owner = { .clientid = clp->cl_clientid, 7174 .id = lsp->ls_seqid.owner_id, 7175 .s_dev = server->s_dev }; 7176 struct nfs4_lock_waiter waiter = { .task = current, 7177 .inode = state->inode, 7178 .owner = &owner}; 7179 wait_queue_entry_t wait; 7180 7181 /* Don't bother with waitqueue if we don't expect a callback */ 7182 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags)) 7183 return nfs4_retry_setlk_simple(state, cmd, request); 7184 7185 init_wait(&wait); 7186 wait.private = &waiter; 7187 wait.func = nfs4_wake_lock_waiter; 7188 7189 while(!signalled()) { 7190 add_wait_queue(q, &wait); 7191 status = nfs4_proc_setlk(state, cmd, request); 7192 if ((status != -EAGAIN) || IS_SETLK(cmd)) { 7193 finish_wait(q, &wait); 7194 break; 7195 } 7196 7197 status = -ERESTARTSYS; 7198 freezer_do_not_count(); 7199 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT); 7200 freezer_count(); 7201 finish_wait(q, &wait); 7202 } 7203 7204 return status; 7205 } 7206 #else /* !CONFIG_NFS_V4_1 */ 7207 static inline int 7208 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7209 { 7210 return nfs4_retry_setlk_simple(state, cmd, request); 7211 } 7212 #endif 7213 7214 static int 7215 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) 7216 { 7217 struct nfs_open_context *ctx; 7218 struct nfs4_state *state; 7219 int status; 7220 7221 /* verify open state */ 7222 ctx = nfs_file_open_context(filp); 7223 state = ctx->state; 7224 7225 if (IS_GETLK(cmd)) { 7226 if (state != NULL) 7227 return nfs4_proc_getlk(state, F_GETLK, request); 7228 return 0; 7229 } 7230 7231 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) 7232 return -EINVAL; 7233 7234 if (request->fl_type == F_UNLCK) { 7235 if (state != NULL) 7236 return nfs4_proc_unlck(state, cmd, request); 7237 return 0; 7238 } 7239 7240 if (state == NULL) 7241 return -ENOLCK; 7242 7243 if ((request->fl_flags & FL_POSIX) && 7244 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) 7245 return -ENOLCK; 7246 7247 /* 7248 * Don't rely on the VFS having checked the file open mode, 7249 * since it won't do this for flock() locks. 7250 */ 7251 switch (request->fl_type) { 7252 case F_RDLCK: 7253 if (!(filp->f_mode & FMODE_READ)) 7254 return -EBADF; 7255 break; 7256 case F_WRLCK: 7257 if (!(filp->f_mode & FMODE_WRITE)) 7258 return -EBADF; 7259 } 7260 7261 status = nfs4_set_lock_state(state, request); 7262 if (status != 0) 7263 return status; 7264 7265 return nfs4_retry_setlk(state, cmd, request); 7266 } 7267 7268 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) 7269 { 7270 struct nfs_server *server = NFS_SERVER(state->inode); 7271 int err; 7272 7273 err = nfs4_set_lock_state(state, fl); 7274 if (err != 0) 7275 return err; 7276 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); 7277 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err); 7278 } 7279 7280 struct nfs_release_lockowner_data { 7281 struct nfs4_lock_state *lsp; 7282 struct nfs_server *server; 7283 struct nfs_release_lockowner_args args; 7284 struct nfs_release_lockowner_res res; 7285 unsigned long timestamp; 7286 }; 7287 7288 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata) 7289 { 7290 struct nfs_release_lockowner_data *data = calldata; 7291 struct nfs_server *server = data->server; 7292 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, 7293 &data->res.seq_res, task); 7294 data->args.lock_owner.clientid = server->nfs_client->cl_clientid; 7295 data->timestamp = jiffies; 7296 } 7297 7298 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata) 7299 { 7300 struct nfs_release_lockowner_data *data = calldata; 7301 struct nfs_server *server = data->server; 7302 7303 nfs40_sequence_done(task, &data->res.seq_res); 7304 7305 switch (task->tk_status) { 7306 case 0: 7307 renew_lease(server, data->timestamp); 7308 break; 7309 case -NFS4ERR_STALE_CLIENTID: 7310 case -NFS4ERR_EXPIRED: 7311 nfs4_schedule_lease_recovery(server->nfs_client); 7312 break; 7313 case -NFS4ERR_LEASE_MOVED: 7314 case -NFS4ERR_DELAY: 7315 if (nfs4_async_handle_error(task, server, 7316 NULL, NULL) == -EAGAIN) 7317 rpc_restart_call_prepare(task); 7318 } 7319 } 7320 7321 static void nfs4_release_lockowner_release(void *calldata) 7322 { 7323 struct nfs_release_lockowner_data *data = calldata; 7324 nfs4_free_lock_state(data->server, data->lsp); 7325 kfree(calldata); 7326 } 7327 7328 static const struct rpc_call_ops nfs4_release_lockowner_ops = { 7329 .rpc_call_prepare = nfs4_release_lockowner_prepare, 7330 .rpc_call_done = nfs4_release_lockowner_done, 7331 .rpc_release = nfs4_release_lockowner_release, 7332 }; 7333 7334 static void 7335 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp) 7336 { 7337 struct nfs_release_lockowner_data *data; 7338 struct rpc_message msg = { 7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], 7340 }; 7341 7342 if (server->nfs_client->cl_mvops->minor_version != 0) 7343 return; 7344 7345 data = kmalloc(sizeof(*data), GFP_NOFS); 7346 if (!data) 7347 return; 7348 data->lsp = lsp; 7349 data->server = server; 7350 data->args.lock_owner.clientid = server->nfs_client->cl_clientid; 7351 data->args.lock_owner.id = lsp->ls_seqid.owner_id; 7352 data->args.lock_owner.s_dev = server->s_dev; 7353 7354 msg.rpc_argp = &data->args; 7355 msg.rpc_resp = &data->res; 7356 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0); 7357 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); 7358 } 7359 7360 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" 7361 7362 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler, 7363 struct dentry *unused, struct inode *inode, 7364 const char *key, const void *buf, 7365 size_t buflen, int flags) 7366 { 7367 return nfs4_proc_set_acl(inode, buf, buflen); 7368 } 7369 7370 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler, 7371 struct dentry *unused, struct inode *inode, 7372 const char *key, void *buf, size_t buflen) 7373 { 7374 return nfs4_proc_get_acl(inode, buf, buflen); 7375 } 7376 7377 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry) 7378 { 7379 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))); 7380 } 7381 7382 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 7383 7384 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler, 7385 struct dentry *unused, struct inode *inode, 7386 const char *key, const void *buf, 7387 size_t buflen, int flags) 7388 { 7389 if (security_ismaclabel(key)) 7390 return nfs4_set_security_label(inode, buf, buflen); 7391 7392 return -EOPNOTSUPP; 7393 } 7394 7395 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler, 7396 struct dentry *unused, struct inode *inode, 7397 const char *key, void *buf, size_t buflen) 7398 { 7399 if (security_ismaclabel(key)) 7400 return nfs4_get_security_label(inode, buf, buflen); 7401 return -EOPNOTSUPP; 7402 } 7403 7404 static ssize_t 7405 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) 7406 { 7407 int len = 0; 7408 7409 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) { 7410 len = security_inode_listsecurity(inode, list, list_len); 7411 if (list_len && len > list_len) 7412 return -ERANGE; 7413 } 7414 return len; 7415 } 7416 7417 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = { 7418 .prefix = XATTR_SECURITY_PREFIX, 7419 .get = nfs4_xattr_get_nfs4_label, 7420 .set = nfs4_xattr_set_nfs4_label, 7421 }; 7422 7423 #else 7424 7425 static ssize_t 7426 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) 7427 { 7428 return 0; 7429 } 7430 7431 #endif 7432 7433 /* 7434 * nfs_fhget will use either the mounted_on_fileid or the fileid 7435 */ 7436 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) 7437 { 7438 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || 7439 (fattr->valid & NFS_ATTR_FATTR_FILEID)) && 7440 (fattr->valid & NFS_ATTR_FATTR_FSID) && 7441 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) 7442 return; 7443 7444 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | 7445 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; 7446 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; 7447 fattr->nlink = 2; 7448 } 7449 7450 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, 7451 const struct qstr *name, 7452 struct nfs4_fs_locations *fs_locations, 7453 struct page *page) 7454 { 7455 struct nfs_server *server = NFS_SERVER(dir); 7456 u32 bitmask[3]; 7457 struct nfs4_fs_locations_arg args = { 7458 .dir_fh = NFS_FH(dir), 7459 .name = name, 7460 .page = page, 7461 .bitmask = bitmask, 7462 }; 7463 struct nfs4_fs_locations_res res = { 7464 .fs_locations = fs_locations, 7465 }; 7466 struct rpc_message msg = { 7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 7468 .rpc_argp = &args, 7469 .rpc_resp = &res, 7470 }; 7471 int status; 7472 7473 dprintk("%s: start\n", __func__); 7474 7475 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS; 7476 bitmask[1] = nfs4_fattr_bitmap[1]; 7477 7478 /* Ask for the fileid of the absent filesystem if mounted_on_fileid 7479 * is not supported */ 7480 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) 7481 bitmask[0] &= ~FATTR4_WORD0_FILEID; 7482 else 7483 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID; 7484 7485 nfs_fattr_init(&fs_locations->fattr); 7486 fs_locations->server = server; 7487 fs_locations->nlocations = 0; 7488 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); 7489 dprintk("%s: returned status = %d\n", __func__, status); 7490 return status; 7491 } 7492 7493 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, 7494 const struct qstr *name, 7495 struct nfs4_fs_locations *fs_locations, 7496 struct page *page) 7497 { 7498 struct nfs4_exception exception = { 7499 .interruptible = true, 7500 }; 7501 int err; 7502 do { 7503 err = _nfs4_proc_fs_locations(client, dir, name, 7504 fs_locations, page); 7505 trace_nfs4_get_fs_locations(dir, name, err); 7506 err = nfs4_handle_exception(NFS_SERVER(dir), err, 7507 &exception); 7508 } while (exception.retry); 7509 return err; 7510 } 7511 7512 /* 7513 * This operation also signals the server that this client is 7514 * performing migration recovery. The server can stop returning 7515 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is 7516 * appended to this compound to identify the client ID which is 7517 * performing recovery. 7518 */ 7519 static int _nfs40_proc_get_locations(struct inode *inode, 7520 struct nfs4_fs_locations *locations, 7521 struct page *page, const struct cred *cred) 7522 { 7523 struct nfs_server *server = NFS_SERVER(inode); 7524 struct rpc_clnt *clnt = server->client; 7525 u32 bitmask[2] = { 7526 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, 7527 }; 7528 struct nfs4_fs_locations_arg args = { 7529 .clientid = server->nfs_client->cl_clientid, 7530 .fh = NFS_FH(inode), 7531 .page = page, 7532 .bitmask = bitmask, 7533 .migration = 1, /* skip LOOKUP */ 7534 .renew = 1, /* append RENEW */ 7535 }; 7536 struct nfs4_fs_locations_res res = { 7537 .fs_locations = locations, 7538 .migration = 1, 7539 .renew = 1, 7540 }; 7541 struct rpc_message msg = { 7542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 7543 .rpc_argp = &args, 7544 .rpc_resp = &res, 7545 .rpc_cred = cred, 7546 }; 7547 unsigned long now = jiffies; 7548 int status; 7549 7550 nfs_fattr_init(&locations->fattr); 7551 locations->server = server; 7552 locations->nlocations = 0; 7553 7554 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); 7555 status = nfs4_call_sync_sequence(clnt, server, &msg, 7556 &args.seq_args, &res.seq_res); 7557 if (status) 7558 return status; 7559 7560 renew_lease(server, now); 7561 return 0; 7562 } 7563 7564 #ifdef CONFIG_NFS_V4_1 7565 7566 /* 7567 * This operation also signals the server that this client is 7568 * performing migration recovery. The server can stop asserting 7569 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID 7570 * performing this operation is identified in the SEQUENCE 7571 * operation in this compound. 7572 * 7573 * When the client supports GETATTR(fs_locations_info), it can 7574 * be plumbed in here. 7575 */ 7576 static int _nfs41_proc_get_locations(struct inode *inode, 7577 struct nfs4_fs_locations *locations, 7578 struct page *page, const struct cred *cred) 7579 { 7580 struct nfs_server *server = NFS_SERVER(inode); 7581 struct rpc_clnt *clnt = server->client; 7582 u32 bitmask[2] = { 7583 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, 7584 }; 7585 struct nfs4_fs_locations_arg args = { 7586 .fh = NFS_FH(inode), 7587 .page = page, 7588 .bitmask = bitmask, 7589 .migration = 1, /* skip LOOKUP */ 7590 }; 7591 struct nfs4_fs_locations_res res = { 7592 .fs_locations = locations, 7593 .migration = 1, 7594 }; 7595 struct rpc_message msg = { 7596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 7597 .rpc_argp = &args, 7598 .rpc_resp = &res, 7599 .rpc_cred = cred, 7600 }; 7601 int status; 7602 7603 nfs_fattr_init(&locations->fattr); 7604 locations->server = server; 7605 locations->nlocations = 0; 7606 7607 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); 7608 status = nfs4_call_sync_sequence(clnt, server, &msg, 7609 &args.seq_args, &res.seq_res); 7610 if (status == NFS4_OK && 7611 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) 7612 status = -NFS4ERR_LEASE_MOVED; 7613 return status; 7614 } 7615 7616 #endif /* CONFIG_NFS_V4_1 */ 7617 7618 /** 7619 * nfs4_proc_get_locations - discover locations for a migrated FSID 7620 * @inode: inode on FSID that is migrating 7621 * @locations: result of query 7622 * @page: buffer 7623 * @cred: credential to use for this operation 7624 * 7625 * Returns NFS4_OK on success, a negative NFS4ERR status code if the 7626 * operation failed, or a negative errno if a local error occurred. 7627 * 7628 * On success, "locations" is filled in, but if the server has 7629 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not 7630 * asserted. 7631 * 7632 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases 7633 * from this client that require migration recovery. 7634 */ 7635 int nfs4_proc_get_locations(struct inode *inode, 7636 struct nfs4_fs_locations *locations, 7637 struct page *page, const struct cred *cred) 7638 { 7639 struct nfs_server *server = NFS_SERVER(inode); 7640 struct nfs_client *clp = server->nfs_client; 7641 const struct nfs4_mig_recovery_ops *ops = 7642 clp->cl_mvops->mig_recovery_ops; 7643 struct nfs4_exception exception = { 7644 .interruptible = true, 7645 }; 7646 int status; 7647 7648 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, 7649 (unsigned long long)server->fsid.major, 7650 (unsigned long long)server->fsid.minor, 7651 clp->cl_hostname); 7652 nfs_display_fhandle(NFS_FH(inode), __func__); 7653 7654 do { 7655 status = ops->get_locations(inode, locations, page, cred); 7656 if (status != -NFS4ERR_DELAY) 7657 break; 7658 nfs4_handle_exception(server, status, &exception); 7659 } while (exception.retry); 7660 return status; 7661 } 7662 7663 /* 7664 * This operation also signals the server that this client is 7665 * performing "lease moved" recovery. The server can stop 7666 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation 7667 * is appended to this compound to identify the client ID which is 7668 * performing recovery. 7669 */ 7670 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred) 7671 { 7672 struct nfs_server *server = NFS_SERVER(inode); 7673 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 7674 struct rpc_clnt *clnt = server->client; 7675 struct nfs4_fsid_present_arg args = { 7676 .fh = NFS_FH(inode), 7677 .clientid = clp->cl_clientid, 7678 .renew = 1, /* append RENEW */ 7679 }; 7680 struct nfs4_fsid_present_res res = { 7681 .renew = 1, 7682 }; 7683 struct rpc_message msg = { 7684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], 7685 .rpc_argp = &args, 7686 .rpc_resp = &res, 7687 .rpc_cred = cred, 7688 }; 7689 unsigned long now = jiffies; 7690 int status; 7691 7692 res.fh = nfs_alloc_fhandle(); 7693 if (res.fh == NULL) 7694 return -ENOMEM; 7695 7696 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); 7697 status = nfs4_call_sync_sequence(clnt, server, &msg, 7698 &args.seq_args, &res.seq_res); 7699 nfs_free_fhandle(res.fh); 7700 if (status) 7701 return status; 7702 7703 do_renew_lease(clp, now); 7704 return 0; 7705 } 7706 7707 #ifdef CONFIG_NFS_V4_1 7708 7709 /* 7710 * This operation also signals the server that this client is 7711 * performing "lease moved" recovery. The server can stop asserting 7712 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing 7713 * this operation is identified in the SEQUENCE operation in this 7714 * compound. 7715 */ 7716 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred) 7717 { 7718 struct nfs_server *server = NFS_SERVER(inode); 7719 struct rpc_clnt *clnt = server->client; 7720 struct nfs4_fsid_present_arg args = { 7721 .fh = NFS_FH(inode), 7722 }; 7723 struct nfs4_fsid_present_res res = { 7724 }; 7725 struct rpc_message msg = { 7726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], 7727 .rpc_argp = &args, 7728 .rpc_resp = &res, 7729 .rpc_cred = cred, 7730 }; 7731 int status; 7732 7733 res.fh = nfs_alloc_fhandle(); 7734 if (res.fh == NULL) 7735 return -ENOMEM; 7736 7737 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); 7738 status = nfs4_call_sync_sequence(clnt, server, &msg, 7739 &args.seq_args, &res.seq_res); 7740 nfs_free_fhandle(res.fh); 7741 if (status == NFS4_OK && 7742 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) 7743 status = -NFS4ERR_LEASE_MOVED; 7744 return status; 7745 } 7746 7747 #endif /* CONFIG_NFS_V4_1 */ 7748 7749 /** 7750 * nfs4_proc_fsid_present - Is this FSID present or absent on server? 7751 * @inode: inode on FSID to check 7752 * @cred: credential to use for this operation 7753 * 7754 * Server indicates whether the FSID is present, moved, or not 7755 * recognized. This operation is necessary to clear a LEASE_MOVED 7756 * condition for this client ID. 7757 * 7758 * Returns NFS4_OK if the FSID is present on this server, 7759 * -NFS4ERR_MOVED if the FSID is no longer present, a negative 7760 * NFS4ERR code if some error occurred on the server, or a 7761 * negative errno if a local failure occurred. 7762 */ 7763 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred) 7764 { 7765 struct nfs_server *server = NFS_SERVER(inode); 7766 struct nfs_client *clp = server->nfs_client; 7767 const struct nfs4_mig_recovery_ops *ops = 7768 clp->cl_mvops->mig_recovery_ops; 7769 struct nfs4_exception exception = { 7770 .interruptible = true, 7771 }; 7772 int status; 7773 7774 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, 7775 (unsigned long long)server->fsid.major, 7776 (unsigned long long)server->fsid.minor, 7777 clp->cl_hostname); 7778 nfs_display_fhandle(NFS_FH(inode), __func__); 7779 7780 do { 7781 status = ops->fsid_present(inode, cred); 7782 if (status != -NFS4ERR_DELAY) 7783 break; 7784 nfs4_handle_exception(server, status, &exception); 7785 } while (exception.retry); 7786 return status; 7787 } 7788 7789 /* 7790 * If 'use_integrity' is true and the state managment nfs_client 7791 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient 7792 * and the machine credential as per RFC3530bis and RFC5661 Security 7793 * Considerations sections. Otherwise, just use the user cred with the 7794 * filesystem's rpc_client. 7795 */ 7796 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity) 7797 { 7798 int status; 7799 struct rpc_clnt *clnt = NFS_SERVER(dir)->client; 7800 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client; 7801 struct nfs4_secinfo_arg args = { 7802 .dir_fh = NFS_FH(dir), 7803 .name = name, 7804 }; 7805 struct nfs4_secinfo_res res = { 7806 .flavors = flavors, 7807 }; 7808 struct rpc_message msg = { 7809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], 7810 .rpc_argp = &args, 7811 .rpc_resp = &res, 7812 }; 7813 struct nfs4_call_sync_data data = { 7814 .seq_server = NFS_SERVER(dir), 7815 .seq_args = &args.seq_args, 7816 .seq_res = &res.seq_res, 7817 }; 7818 struct rpc_task_setup task_setup = { 7819 .rpc_client = clnt, 7820 .rpc_message = &msg, 7821 .callback_ops = clp->cl_mvops->call_sync_ops, 7822 .callback_data = &data, 7823 .flags = RPC_TASK_NO_ROUND_ROBIN, 7824 }; 7825 const struct cred *cred = NULL; 7826 7827 if (use_integrity) { 7828 clnt = clp->cl_rpcclient; 7829 task_setup.rpc_client = clnt; 7830 7831 cred = nfs4_get_clid_cred(clp); 7832 msg.rpc_cred = cred; 7833 } 7834 7835 dprintk("NFS call secinfo %s\n", name->name); 7836 7837 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg); 7838 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); 7839 status = nfs4_call_sync_custom(&task_setup); 7840 7841 dprintk("NFS reply secinfo: %d\n", status); 7842 7843 put_cred(cred); 7844 return status; 7845 } 7846 7847 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, 7848 struct nfs4_secinfo_flavors *flavors) 7849 { 7850 struct nfs4_exception exception = { 7851 .interruptible = true, 7852 }; 7853 int err; 7854 do { 7855 err = -NFS4ERR_WRONGSEC; 7856 7857 /* try to use integrity protection with machine cred */ 7858 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client)) 7859 err = _nfs4_proc_secinfo(dir, name, flavors, true); 7860 7861 /* 7862 * if unable to use integrity protection, or SECINFO with 7863 * integrity protection returns NFS4ERR_WRONGSEC (which is 7864 * disallowed by spec, but exists in deployed servers) use 7865 * the current filesystem's rpc_client and the user cred. 7866 */ 7867 if (err == -NFS4ERR_WRONGSEC) 7868 err = _nfs4_proc_secinfo(dir, name, flavors, false); 7869 7870 trace_nfs4_secinfo(dir, name, err); 7871 err = nfs4_handle_exception(NFS_SERVER(dir), err, 7872 &exception); 7873 } while (exception.retry); 7874 return err; 7875 } 7876 7877 #ifdef CONFIG_NFS_V4_1 7878 /* 7879 * Check the exchange flags returned by the server for invalid flags, having 7880 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or 7881 * DS flags set. 7882 */ 7883 static int nfs4_check_cl_exchange_flags(u32 flags) 7884 { 7885 if (flags & ~EXCHGID4_FLAG_MASK_R) 7886 goto out_inval; 7887 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && 7888 (flags & EXCHGID4_FLAG_USE_NON_PNFS)) 7889 goto out_inval; 7890 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) 7891 goto out_inval; 7892 return NFS_OK; 7893 out_inval: 7894 return -NFS4ERR_INVAL; 7895 } 7896 7897 static bool 7898 nfs41_same_server_scope(struct nfs41_server_scope *a, 7899 struct nfs41_server_scope *b) 7900 { 7901 if (a->server_scope_sz != b->server_scope_sz) 7902 return false; 7903 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0; 7904 } 7905 7906 static void 7907 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata) 7908 { 7909 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp; 7910 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp; 7911 struct nfs_client *clp = args->client; 7912 7913 switch (task->tk_status) { 7914 case -NFS4ERR_BADSESSION: 7915 case -NFS4ERR_DEADSESSION: 7916 nfs4_schedule_session_recovery(clp->cl_session, 7917 task->tk_status); 7918 } 7919 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH && 7920 res->dir != NFS4_CDFS4_BOTH) { 7921 rpc_task_close_connection(task); 7922 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES) 7923 rpc_restart_call(task); 7924 } 7925 } 7926 7927 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = { 7928 .rpc_call_done = nfs4_bind_one_conn_to_session_done, 7929 }; 7930 7931 /* 7932 * nfs4_proc_bind_one_conn_to_session() 7933 * 7934 * The 4.1 client currently uses the same TCP connection for the 7935 * fore and backchannel. 7936 */ 7937 static 7938 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt, 7939 struct rpc_xprt *xprt, 7940 struct nfs_client *clp, 7941 const struct cred *cred) 7942 { 7943 int status; 7944 struct nfs41_bind_conn_to_session_args args = { 7945 .client = clp, 7946 .dir = NFS4_CDFC4_FORE_OR_BOTH, 7947 .retries = 0, 7948 }; 7949 struct nfs41_bind_conn_to_session_res res; 7950 struct rpc_message msg = { 7951 .rpc_proc = 7952 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], 7953 .rpc_argp = &args, 7954 .rpc_resp = &res, 7955 .rpc_cred = cred, 7956 }; 7957 struct rpc_task_setup task_setup_data = { 7958 .rpc_client = clnt, 7959 .rpc_xprt = xprt, 7960 .callback_ops = &nfs4_bind_one_conn_to_session_ops, 7961 .rpc_message = &msg, 7962 .flags = RPC_TASK_TIMEOUT, 7963 }; 7964 struct rpc_task *task; 7965 7966 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id); 7967 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN)) 7968 args.dir = NFS4_CDFC4_FORE; 7969 7970 /* Do not set the backchannel flag unless this is clnt->cl_xprt */ 7971 if (xprt != rcu_access_pointer(clnt->cl_xprt)) 7972 args.dir = NFS4_CDFC4_FORE; 7973 7974 task = rpc_run_task(&task_setup_data); 7975 if (!IS_ERR(task)) { 7976 status = task->tk_status; 7977 rpc_put_task(task); 7978 } else 7979 status = PTR_ERR(task); 7980 trace_nfs4_bind_conn_to_session(clp, status); 7981 if (status == 0) { 7982 if (memcmp(res.sessionid.data, 7983 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { 7984 dprintk("NFS: %s: Session ID mismatch\n", __func__); 7985 return -EIO; 7986 } 7987 if ((res.dir & args.dir) != res.dir || res.dir == 0) { 7988 dprintk("NFS: %s: Unexpected direction from server\n", 7989 __func__); 7990 return -EIO; 7991 } 7992 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) { 7993 dprintk("NFS: %s: Server returned RDMA mode = true\n", 7994 __func__); 7995 return -EIO; 7996 } 7997 } 7998 7999 return status; 8000 } 8001 8002 struct rpc_bind_conn_calldata { 8003 struct nfs_client *clp; 8004 const struct cred *cred; 8005 }; 8006 8007 static int 8008 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt, 8009 struct rpc_xprt *xprt, 8010 void *calldata) 8011 { 8012 struct rpc_bind_conn_calldata *p = calldata; 8013 8014 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred); 8015 } 8016 8017 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred) 8018 { 8019 struct rpc_bind_conn_calldata data = { 8020 .clp = clp, 8021 .cred = cred, 8022 }; 8023 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient, 8024 nfs4_proc_bind_conn_to_session_callback, &data); 8025 } 8026 8027 /* 8028 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map 8029 * and operations we'd like to see to enable certain features in the allow map 8030 */ 8031 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = { 8032 .how = SP4_MACH_CRED, 8033 .enforce.u.words = { 8034 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | 8035 1 << (OP_EXCHANGE_ID - 32) | 8036 1 << (OP_CREATE_SESSION - 32) | 8037 1 << (OP_DESTROY_SESSION - 32) | 8038 1 << (OP_DESTROY_CLIENTID - 32) 8039 }, 8040 .allow.u.words = { 8041 [0] = 1 << (OP_CLOSE) | 8042 1 << (OP_OPEN_DOWNGRADE) | 8043 1 << (OP_LOCKU) | 8044 1 << (OP_DELEGRETURN) | 8045 1 << (OP_COMMIT), 8046 [1] = 1 << (OP_SECINFO - 32) | 8047 1 << (OP_SECINFO_NO_NAME - 32) | 8048 1 << (OP_LAYOUTRETURN - 32) | 8049 1 << (OP_TEST_STATEID - 32) | 8050 1 << (OP_FREE_STATEID - 32) | 8051 1 << (OP_WRITE - 32) 8052 } 8053 }; 8054 8055 /* 8056 * Select the state protection mode for client `clp' given the server results 8057 * from exchange_id in `sp'. 8058 * 8059 * Returns 0 on success, negative errno otherwise. 8060 */ 8061 static int nfs4_sp4_select_mode(struct nfs_client *clp, 8062 struct nfs41_state_protection *sp) 8063 { 8064 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = { 8065 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | 8066 1 << (OP_EXCHANGE_ID - 32) | 8067 1 << (OP_CREATE_SESSION - 32) | 8068 1 << (OP_DESTROY_SESSION - 32) | 8069 1 << (OP_DESTROY_CLIENTID - 32) 8070 }; 8071 unsigned long flags = 0; 8072 unsigned int i; 8073 int ret = 0; 8074 8075 if (sp->how == SP4_MACH_CRED) { 8076 /* Print state protect result */ 8077 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n"); 8078 for (i = 0; i <= LAST_NFS4_OP; i++) { 8079 if (test_bit(i, sp->enforce.u.longs)) 8080 dfprintk(MOUNT, " enforce op %d\n", i); 8081 if (test_bit(i, sp->allow.u.longs)) 8082 dfprintk(MOUNT, " allow op %d\n", i); 8083 } 8084 8085 /* make sure nothing is on enforce list that isn't supported */ 8086 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) { 8087 if (sp->enforce.u.words[i] & ~supported_enforce[i]) { 8088 dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); 8089 ret = -EINVAL; 8090 goto out; 8091 } 8092 } 8093 8094 /* 8095 * Minimal mode - state operations are allowed to use machine 8096 * credential. Note this already happens by default, so the 8097 * client doesn't have to do anything more than the negotiation. 8098 * 8099 * NOTE: we don't care if EXCHANGE_ID is in the list - 8100 * we're already using the machine cred for exchange_id 8101 * and will never use a different cred. 8102 */ 8103 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) && 8104 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) && 8105 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) && 8106 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) { 8107 dfprintk(MOUNT, "sp4_mach_cred:\n"); 8108 dfprintk(MOUNT, " minimal mode enabled\n"); 8109 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags); 8110 } else { 8111 dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); 8112 ret = -EINVAL; 8113 goto out; 8114 } 8115 8116 if (test_bit(OP_CLOSE, sp->allow.u.longs) && 8117 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) && 8118 test_bit(OP_DELEGRETURN, sp->allow.u.longs) && 8119 test_bit(OP_LOCKU, sp->allow.u.longs)) { 8120 dfprintk(MOUNT, " cleanup mode enabled\n"); 8121 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags); 8122 } 8123 8124 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) { 8125 dfprintk(MOUNT, " pnfs cleanup mode enabled\n"); 8126 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags); 8127 } 8128 8129 if (test_bit(OP_SECINFO, sp->allow.u.longs) && 8130 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) { 8131 dfprintk(MOUNT, " secinfo mode enabled\n"); 8132 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags); 8133 } 8134 8135 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) && 8136 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) { 8137 dfprintk(MOUNT, " stateid mode enabled\n"); 8138 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags); 8139 } 8140 8141 if (test_bit(OP_WRITE, sp->allow.u.longs)) { 8142 dfprintk(MOUNT, " write mode enabled\n"); 8143 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags); 8144 } 8145 8146 if (test_bit(OP_COMMIT, sp->allow.u.longs)) { 8147 dfprintk(MOUNT, " commit mode enabled\n"); 8148 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags); 8149 } 8150 } 8151 out: 8152 clp->cl_sp4_flags = flags; 8153 return ret; 8154 } 8155 8156 struct nfs41_exchange_id_data { 8157 struct nfs41_exchange_id_res res; 8158 struct nfs41_exchange_id_args args; 8159 }; 8160 8161 static void nfs4_exchange_id_release(void *data) 8162 { 8163 struct nfs41_exchange_id_data *cdata = 8164 (struct nfs41_exchange_id_data *)data; 8165 8166 nfs_put_client(cdata->args.client); 8167 kfree(cdata->res.impl_id); 8168 kfree(cdata->res.server_scope); 8169 kfree(cdata->res.server_owner); 8170 kfree(cdata); 8171 } 8172 8173 static const struct rpc_call_ops nfs4_exchange_id_call_ops = { 8174 .rpc_release = nfs4_exchange_id_release, 8175 }; 8176 8177 /* 8178 * _nfs4_proc_exchange_id() 8179 * 8180 * Wrapper for EXCHANGE_ID operation. 8181 */ 8182 static struct rpc_task * 8183 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred, 8184 u32 sp4_how, struct rpc_xprt *xprt) 8185 { 8186 struct rpc_message msg = { 8187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], 8188 .rpc_cred = cred, 8189 }; 8190 struct rpc_task_setup task_setup_data = { 8191 .rpc_client = clp->cl_rpcclient, 8192 .callback_ops = &nfs4_exchange_id_call_ops, 8193 .rpc_message = &msg, 8194 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, 8195 }; 8196 struct nfs41_exchange_id_data *calldata; 8197 int status; 8198 8199 if (!refcount_inc_not_zero(&clp->cl_count)) 8200 return ERR_PTR(-EIO); 8201 8202 status = -ENOMEM; 8203 calldata = kzalloc(sizeof(*calldata), GFP_NOFS); 8204 if (!calldata) 8205 goto out; 8206 8207 nfs4_init_boot_verifier(clp, &calldata->args.verifier); 8208 8209 status = nfs4_init_uniform_client_string(clp); 8210 if (status) 8211 goto out_calldata; 8212 8213 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner), 8214 GFP_NOFS); 8215 status = -ENOMEM; 8216 if (unlikely(calldata->res.server_owner == NULL)) 8217 goto out_calldata; 8218 8219 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope), 8220 GFP_NOFS); 8221 if (unlikely(calldata->res.server_scope == NULL)) 8222 goto out_server_owner; 8223 8224 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS); 8225 if (unlikely(calldata->res.impl_id == NULL)) 8226 goto out_server_scope; 8227 8228 switch (sp4_how) { 8229 case SP4_NONE: 8230 calldata->args.state_protect.how = SP4_NONE; 8231 break; 8232 8233 case SP4_MACH_CRED: 8234 calldata->args.state_protect = nfs4_sp4_mach_cred_request; 8235 break; 8236 8237 default: 8238 /* unsupported! */ 8239 WARN_ON_ONCE(1); 8240 status = -EINVAL; 8241 goto out_impl_id; 8242 } 8243 if (xprt) { 8244 task_setup_data.rpc_xprt = xprt; 8245 task_setup_data.flags |= RPC_TASK_SOFTCONN; 8246 memcpy(calldata->args.verifier.data, clp->cl_confirm.data, 8247 sizeof(calldata->args.verifier.data)); 8248 } 8249 calldata->args.client = clp; 8250 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | 8251 EXCHGID4_FLAG_BIND_PRINC_STATEID; 8252 #ifdef CONFIG_NFS_V4_1_MIGRATION 8253 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR; 8254 #endif 8255 msg.rpc_argp = &calldata->args; 8256 msg.rpc_resp = &calldata->res; 8257 task_setup_data.callback_data = calldata; 8258 8259 return rpc_run_task(&task_setup_data); 8260 8261 out_impl_id: 8262 kfree(calldata->res.impl_id); 8263 out_server_scope: 8264 kfree(calldata->res.server_scope); 8265 out_server_owner: 8266 kfree(calldata->res.server_owner); 8267 out_calldata: 8268 kfree(calldata); 8269 out: 8270 nfs_put_client(clp); 8271 return ERR_PTR(status); 8272 } 8273 8274 /* 8275 * _nfs4_proc_exchange_id() 8276 * 8277 * Wrapper for EXCHANGE_ID operation. 8278 */ 8279 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred, 8280 u32 sp4_how) 8281 { 8282 struct rpc_task *task; 8283 struct nfs41_exchange_id_args *argp; 8284 struct nfs41_exchange_id_res *resp; 8285 unsigned long now = jiffies; 8286 int status; 8287 8288 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL); 8289 if (IS_ERR(task)) 8290 return PTR_ERR(task); 8291 8292 argp = task->tk_msg.rpc_argp; 8293 resp = task->tk_msg.rpc_resp; 8294 status = task->tk_status; 8295 if (status != 0) 8296 goto out; 8297 8298 status = nfs4_check_cl_exchange_flags(resp->flags); 8299 if (status != 0) 8300 goto out; 8301 8302 status = nfs4_sp4_select_mode(clp, &resp->state_protect); 8303 if (status != 0) 8304 goto out; 8305 8306 do_renew_lease(clp, now); 8307 8308 clp->cl_clientid = resp->clientid; 8309 clp->cl_exchange_flags = resp->flags; 8310 clp->cl_seqid = resp->seqid; 8311 /* Client ID is not confirmed */ 8312 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R)) 8313 clear_bit(NFS4_SESSION_ESTABLISHED, 8314 &clp->cl_session->session_state); 8315 8316 if (clp->cl_serverscope != NULL && 8317 !nfs41_same_server_scope(clp->cl_serverscope, 8318 resp->server_scope)) { 8319 dprintk("%s: server_scope mismatch detected\n", 8320 __func__); 8321 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); 8322 } 8323 8324 swap(clp->cl_serverowner, resp->server_owner); 8325 swap(clp->cl_serverscope, resp->server_scope); 8326 swap(clp->cl_implid, resp->impl_id); 8327 8328 /* Save the EXCHANGE_ID verifier session trunk tests */ 8329 memcpy(clp->cl_confirm.data, argp->verifier.data, 8330 sizeof(clp->cl_confirm.data)); 8331 out: 8332 trace_nfs4_exchange_id(clp, status); 8333 rpc_put_task(task); 8334 return status; 8335 } 8336 8337 /* 8338 * nfs4_proc_exchange_id() 8339 * 8340 * Returns zero, a negative errno, or a negative NFS4ERR status code. 8341 * 8342 * Since the clientid has expired, all compounds using sessions 8343 * associated with the stale clientid will be returning 8344 * NFS4ERR_BADSESSION in the sequence operation, and will therefore 8345 * be in some phase of session reset. 8346 * 8347 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used. 8348 */ 8349 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred) 8350 { 8351 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor; 8352 int status; 8353 8354 /* try SP4_MACH_CRED if krb5i/p */ 8355 if (authflavor == RPC_AUTH_GSS_KRB5I || 8356 authflavor == RPC_AUTH_GSS_KRB5P) { 8357 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED); 8358 if (!status) 8359 return 0; 8360 } 8361 8362 /* try SP4_NONE */ 8363 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE); 8364 } 8365 8366 /** 8367 * nfs4_test_session_trunk 8368 * 8369 * This is an add_xprt_test() test function called from 8370 * rpc_clnt_setup_test_and_add_xprt. 8371 * 8372 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt 8373 * and is dereferrenced in nfs4_exchange_id_release 8374 * 8375 * Upon success, add the new transport to the rpc_clnt 8376 * 8377 * @clnt: struct rpc_clnt to get new transport 8378 * @xprt: the rpc_xprt to test 8379 * @data: call data for _nfs4_proc_exchange_id. 8380 */ 8381 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt, 8382 void *data) 8383 { 8384 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data; 8385 struct rpc_task *task; 8386 int status; 8387 8388 u32 sp4_how; 8389 8390 dprintk("--> %s try %s\n", __func__, 8391 xprt->address_strings[RPC_DISPLAY_ADDR]); 8392 8393 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED); 8394 8395 /* Test connection for session trunking. Async exchange_id call */ 8396 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt); 8397 if (IS_ERR(task)) 8398 return; 8399 8400 status = task->tk_status; 8401 if (status == 0) 8402 status = nfs4_detect_session_trunking(adata->clp, 8403 task->tk_msg.rpc_resp, xprt); 8404 8405 if (status == 0) 8406 rpc_clnt_xprt_switch_add_xprt(clnt, xprt); 8407 8408 rpc_put_task(task); 8409 } 8410 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk); 8411 8412 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, 8413 const struct cred *cred) 8414 { 8415 struct rpc_message msg = { 8416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], 8417 .rpc_argp = clp, 8418 .rpc_cred = cred, 8419 }; 8420 int status; 8421 8422 status = rpc_call_sync(clp->cl_rpcclient, &msg, 8423 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 8424 trace_nfs4_destroy_clientid(clp, status); 8425 if (status) 8426 dprintk("NFS: Got error %d from the server %s on " 8427 "DESTROY_CLIENTID.", status, clp->cl_hostname); 8428 return status; 8429 } 8430 8431 static int nfs4_proc_destroy_clientid(struct nfs_client *clp, 8432 const struct cred *cred) 8433 { 8434 unsigned int loop; 8435 int ret; 8436 8437 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { 8438 ret = _nfs4_proc_destroy_clientid(clp, cred); 8439 switch (ret) { 8440 case -NFS4ERR_DELAY: 8441 case -NFS4ERR_CLIENTID_BUSY: 8442 ssleep(1); 8443 break; 8444 default: 8445 return ret; 8446 } 8447 } 8448 return 0; 8449 } 8450 8451 int nfs4_destroy_clientid(struct nfs_client *clp) 8452 { 8453 const struct cred *cred; 8454 int ret = 0; 8455 8456 if (clp->cl_mvops->minor_version < 1) 8457 goto out; 8458 if (clp->cl_exchange_flags == 0) 8459 goto out; 8460 if (clp->cl_preserve_clid) 8461 goto out; 8462 cred = nfs4_get_clid_cred(clp); 8463 ret = nfs4_proc_destroy_clientid(clp, cred); 8464 put_cred(cred); 8465 switch (ret) { 8466 case 0: 8467 case -NFS4ERR_STALE_CLIENTID: 8468 clp->cl_exchange_flags = 0; 8469 } 8470 out: 8471 return ret; 8472 } 8473 8474 #endif /* CONFIG_NFS_V4_1 */ 8475 8476 struct nfs4_get_lease_time_data { 8477 struct nfs4_get_lease_time_args *args; 8478 struct nfs4_get_lease_time_res *res; 8479 struct nfs_client *clp; 8480 }; 8481 8482 static void nfs4_get_lease_time_prepare(struct rpc_task *task, 8483 void *calldata) 8484 { 8485 struct nfs4_get_lease_time_data *data = 8486 (struct nfs4_get_lease_time_data *)calldata; 8487 8488 dprintk("--> %s\n", __func__); 8489 /* just setup sequence, do not trigger session recovery 8490 since we're invoked within one */ 8491 nfs4_setup_sequence(data->clp, 8492 &data->args->la_seq_args, 8493 &data->res->lr_seq_res, 8494 task); 8495 dprintk("<-- %s\n", __func__); 8496 } 8497 8498 /* 8499 * Called from nfs4_state_manager thread for session setup, so don't recover 8500 * from sequence operation or clientid errors. 8501 */ 8502 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) 8503 { 8504 struct nfs4_get_lease_time_data *data = 8505 (struct nfs4_get_lease_time_data *)calldata; 8506 8507 dprintk("--> %s\n", __func__); 8508 if (!nfs4_sequence_done(task, &data->res->lr_seq_res)) 8509 return; 8510 switch (task->tk_status) { 8511 case -NFS4ERR_DELAY: 8512 case -NFS4ERR_GRACE: 8513 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status); 8514 rpc_delay(task, NFS4_POLL_RETRY_MIN); 8515 task->tk_status = 0; 8516 /* fall through */ 8517 case -NFS4ERR_RETRY_UNCACHED_REP: 8518 rpc_restart_call_prepare(task); 8519 return; 8520 } 8521 dprintk("<-- %s\n", __func__); 8522 } 8523 8524 static const struct rpc_call_ops nfs4_get_lease_time_ops = { 8525 .rpc_call_prepare = nfs4_get_lease_time_prepare, 8526 .rpc_call_done = nfs4_get_lease_time_done, 8527 }; 8528 8529 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) 8530 { 8531 struct nfs4_get_lease_time_args args; 8532 struct nfs4_get_lease_time_res res = { 8533 .lr_fsinfo = fsinfo, 8534 }; 8535 struct nfs4_get_lease_time_data data = { 8536 .args = &args, 8537 .res = &res, 8538 .clp = clp, 8539 }; 8540 struct rpc_message msg = { 8541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], 8542 .rpc_argp = &args, 8543 .rpc_resp = &res, 8544 }; 8545 struct rpc_task_setup task_setup = { 8546 .rpc_client = clp->cl_rpcclient, 8547 .rpc_message = &msg, 8548 .callback_ops = &nfs4_get_lease_time_ops, 8549 .callback_data = &data, 8550 .flags = RPC_TASK_TIMEOUT, 8551 }; 8552 8553 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1); 8554 return nfs4_call_sync_custom(&task_setup); 8555 } 8556 8557 #ifdef CONFIG_NFS_V4_1 8558 8559 /* 8560 * Initialize the values to be used by the client in CREATE_SESSION 8561 * If nfs4_init_session set the fore channel request and response sizes, 8562 * use them. 8563 * 8564 * Set the back channel max_resp_sz_cached to zero to force the client to 8565 * always set csa_cachethis to FALSE because the current implementation 8566 * of the back channel DRC only supports caching the CB_SEQUENCE operation. 8567 */ 8568 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args, 8569 struct rpc_clnt *clnt) 8570 { 8571 unsigned int max_rqst_sz, max_resp_sz; 8572 unsigned int max_bc_payload = rpc_max_bc_payload(clnt); 8573 unsigned int max_bc_slots = rpc_num_bc_slots(clnt); 8574 8575 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead; 8576 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead; 8577 8578 /* Fore channel attributes */ 8579 args->fc_attrs.max_rqst_sz = max_rqst_sz; 8580 args->fc_attrs.max_resp_sz = max_resp_sz; 8581 args->fc_attrs.max_ops = NFS4_MAX_OPS; 8582 args->fc_attrs.max_reqs = max_session_slots; 8583 8584 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " 8585 "max_ops=%u max_reqs=%u\n", 8586 __func__, 8587 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, 8588 args->fc_attrs.max_ops, args->fc_attrs.max_reqs); 8589 8590 /* Back channel attributes */ 8591 args->bc_attrs.max_rqst_sz = max_bc_payload; 8592 args->bc_attrs.max_resp_sz = max_bc_payload; 8593 args->bc_attrs.max_resp_sz_cached = 0; 8594 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; 8595 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1); 8596 if (args->bc_attrs.max_reqs > max_bc_slots) 8597 args->bc_attrs.max_reqs = max_bc_slots; 8598 8599 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " 8600 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", 8601 __func__, 8602 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, 8603 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, 8604 args->bc_attrs.max_reqs); 8605 } 8606 8607 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, 8608 struct nfs41_create_session_res *res) 8609 { 8610 struct nfs4_channel_attrs *sent = &args->fc_attrs; 8611 struct nfs4_channel_attrs *rcvd = &res->fc_attrs; 8612 8613 if (rcvd->max_resp_sz > sent->max_resp_sz) 8614 return -EINVAL; 8615 /* 8616 * Our requested max_ops is the minimum we need; we're not 8617 * prepared to break up compounds into smaller pieces than that. 8618 * So, no point even trying to continue if the server won't 8619 * cooperate: 8620 */ 8621 if (rcvd->max_ops < sent->max_ops) 8622 return -EINVAL; 8623 if (rcvd->max_reqs == 0) 8624 return -EINVAL; 8625 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) 8626 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; 8627 return 0; 8628 } 8629 8630 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, 8631 struct nfs41_create_session_res *res) 8632 { 8633 struct nfs4_channel_attrs *sent = &args->bc_attrs; 8634 struct nfs4_channel_attrs *rcvd = &res->bc_attrs; 8635 8636 if (!(res->flags & SESSION4_BACK_CHAN)) 8637 goto out; 8638 if (rcvd->max_rqst_sz > sent->max_rqst_sz) 8639 return -EINVAL; 8640 if (rcvd->max_resp_sz < sent->max_resp_sz) 8641 return -EINVAL; 8642 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) 8643 return -EINVAL; 8644 if (rcvd->max_ops > sent->max_ops) 8645 return -EINVAL; 8646 if (rcvd->max_reqs > sent->max_reqs) 8647 return -EINVAL; 8648 out: 8649 return 0; 8650 } 8651 8652 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, 8653 struct nfs41_create_session_res *res) 8654 { 8655 int ret; 8656 8657 ret = nfs4_verify_fore_channel_attrs(args, res); 8658 if (ret) 8659 return ret; 8660 return nfs4_verify_back_channel_attrs(args, res); 8661 } 8662 8663 static void nfs4_update_session(struct nfs4_session *session, 8664 struct nfs41_create_session_res *res) 8665 { 8666 nfs4_copy_sessionid(&session->sess_id, &res->sessionid); 8667 /* Mark client id and session as being confirmed */ 8668 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 8669 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state); 8670 session->flags = res->flags; 8671 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs)); 8672 if (res->flags & SESSION4_BACK_CHAN) 8673 memcpy(&session->bc_attrs, &res->bc_attrs, 8674 sizeof(session->bc_attrs)); 8675 } 8676 8677 static int _nfs4_proc_create_session(struct nfs_client *clp, 8678 const struct cred *cred) 8679 { 8680 struct nfs4_session *session = clp->cl_session; 8681 struct nfs41_create_session_args args = { 8682 .client = clp, 8683 .clientid = clp->cl_clientid, 8684 .seqid = clp->cl_seqid, 8685 .cb_program = NFS4_CALLBACK, 8686 }; 8687 struct nfs41_create_session_res res; 8688 8689 struct rpc_message msg = { 8690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], 8691 .rpc_argp = &args, 8692 .rpc_resp = &res, 8693 .rpc_cred = cred, 8694 }; 8695 int status; 8696 8697 nfs4_init_channel_attrs(&args, clp->cl_rpcclient); 8698 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); 8699 8700 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 8701 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 8702 trace_nfs4_create_session(clp, status); 8703 8704 switch (status) { 8705 case -NFS4ERR_STALE_CLIENTID: 8706 case -NFS4ERR_DELAY: 8707 case -ETIMEDOUT: 8708 case -EACCES: 8709 case -EAGAIN: 8710 goto out; 8711 } 8712 8713 clp->cl_seqid++; 8714 if (!status) { 8715 /* Verify the session's negotiated channel_attrs values */ 8716 status = nfs4_verify_channel_attrs(&args, &res); 8717 /* Increment the clientid slot sequence id */ 8718 if (status) 8719 goto out; 8720 nfs4_update_session(session, &res); 8721 } 8722 out: 8723 return status; 8724 } 8725 8726 /* 8727 * Issues a CREATE_SESSION operation to the server. 8728 * It is the responsibility of the caller to verify the session is 8729 * expired before calling this routine. 8730 */ 8731 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred) 8732 { 8733 int status; 8734 unsigned *ptr; 8735 struct nfs4_session *session = clp->cl_session; 8736 8737 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); 8738 8739 status = _nfs4_proc_create_session(clp, cred); 8740 if (status) 8741 goto out; 8742 8743 /* Init or reset the session slot tables */ 8744 status = nfs4_setup_session_slot_tables(session); 8745 dprintk("slot table setup returned %d\n", status); 8746 if (status) 8747 goto out; 8748 8749 ptr = (unsigned *)&session->sess_id.data[0]; 8750 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, 8751 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); 8752 out: 8753 dprintk("<-- %s\n", __func__); 8754 return status; 8755 } 8756 8757 /* 8758 * Issue the over-the-wire RPC DESTROY_SESSION. 8759 * The caller must serialize access to this routine. 8760 */ 8761 int nfs4_proc_destroy_session(struct nfs4_session *session, 8762 const struct cred *cred) 8763 { 8764 struct rpc_message msg = { 8765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], 8766 .rpc_argp = session, 8767 .rpc_cred = cred, 8768 }; 8769 int status = 0; 8770 8771 dprintk("--> nfs4_proc_destroy_session\n"); 8772 8773 /* session is still being setup */ 8774 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state)) 8775 return 0; 8776 8777 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 8778 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 8779 trace_nfs4_destroy_session(session->clp, status); 8780 8781 if (status) 8782 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " 8783 "Session has been destroyed regardless...\n", status); 8784 8785 dprintk("<-- nfs4_proc_destroy_session\n"); 8786 return status; 8787 } 8788 8789 /* 8790 * Renew the cl_session lease. 8791 */ 8792 struct nfs4_sequence_data { 8793 struct nfs_client *clp; 8794 struct nfs4_sequence_args args; 8795 struct nfs4_sequence_res res; 8796 }; 8797 8798 static void nfs41_sequence_release(void *data) 8799 { 8800 struct nfs4_sequence_data *calldata = data; 8801 struct nfs_client *clp = calldata->clp; 8802 8803 if (refcount_read(&clp->cl_count) > 1) 8804 nfs4_schedule_state_renewal(clp); 8805 nfs_put_client(clp); 8806 kfree(calldata); 8807 } 8808 8809 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) 8810 { 8811 switch(task->tk_status) { 8812 case -NFS4ERR_DELAY: 8813 rpc_delay(task, NFS4_POLL_RETRY_MAX); 8814 return -EAGAIN; 8815 default: 8816 nfs4_schedule_lease_recovery(clp); 8817 } 8818 return 0; 8819 } 8820 8821 static void nfs41_sequence_call_done(struct rpc_task *task, void *data) 8822 { 8823 struct nfs4_sequence_data *calldata = data; 8824 struct nfs_client *clp = calldata->clp; 8825 8826 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) 8827 return; 8828 8829 trace_nfs4_sequence(clp, task->tk_status); 8830 if (task->tk_status < 0) { 8831 dprintk("%s ERROR %d\n", __func__, task->tk_status); 8832 if (refcount_read(&clp->cl_count) == 1) 8833 goto out; 8834 8835 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { 8836 rpc_restart_call_prepare(task); 8837 return; 8838 } 8839 } 8840 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); 8841 out: 8842 dprintk("<-- %s\n", __func__); 8843 } 8844 8845 static void nfs41_sequence_prepare(struct rpc_task *task, void *data) 8846 { 8847 struct nfs4_sequence_data *calldata = data; 8848 struct nfs_client *clp = calldata->clp; 8849 struct nfs4_sequence_args *args; 8850 struct nfs4_sequence_res *res; 8851 8852 args = task->tk_msg.rpc_argp; 8853 res = task->tk_msg.rpc_resp; 8854 8855 nfs4_setup_sequence(clp, args, res, task); 8856 } 8857 8858 static const struct rpc_call_ops nfs41_sequence_ops = { 8859 .rpc_call_done = nfs41_sequence_call_done, 8860 .rpc_call_prepare = nfs41_sequence_prepare, 8861 .rpc_release = nfs41_sequence_release, 8862 }; 8863 8864 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, 8865 const struct cred *cred, 8866 struct nfs4_slot *slot, 8867 bool is_privileged) 8868 { 8869 struct nfs4_sequence_data *calldata; 8870 struct rpc_message msg = { 8871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], 8872 .rpc_cred = cred, 8873 }; 8874 struct rpc_task_setup task_setup_data = { 8875 .rpc_client = clp->cl_rpcclient, 8876 .rpc_message = &msg, 8877 .callback_ops = &nfs41_sequence_ops, 8878 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, 8879 }; 8880 struct rpc_task *ret; 8881 8882 ret = ERR_PTR(-EIO); 8883 if (!refcount_inc_not_zero(&clp->cl_count)) 8884 goto out_err; 8885 8886 ret = ERR_PTR(-ENOMEM); 8887 calldata = kzalloc(sizeof(*calldata), GFP_NOFS); 8888 if (calldata == NULL) 8889 goto out_put_clp; 8890 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged); 8891 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot); 8892 msg.rpc_argp = &calldata->args; 8893 msg.rpc_resp = &calldata->res; 8894 calldata->clp = clp; 8895 task_setup_data.callback_data = calldata; 8896 8897 ret = rpc_run_task(&task_setup_data); 8898 if (IS_ERR(ret)) 8899 goto out_err; 8900 return ret; 8901 out_put_clp: 8902 nfs_put_client(clp); 8903 out_err: 8904 nfs41_release_slot(slot); 8905 return ret; 8906 } 8907 8908 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) 8909 { 8910 struct rpc_task *task; 8911 int ret = 0; 8912 8913 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) 8914 return -EAGAIN; 8915 task = _nfs41_proc_sequence(clp, cred, NULL, false); 8916 if (IS_ERR(task)) 8917 ret = PTR_ERR(task); 8918 else 8919 rpc_put_task_async(task); 8920 dprintk("<-- %s status=%d\n", __func__, ret); 8921 return ret; 8922 } 8923 8924 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred) 8925 { 8926 struct rpc_task *task; 8927 int ret; 8928 8929 task = _nfs41_proc_sequence(clp, cred, NULL, true); 8930 if (IS_ERR(task)) { 8931 ret = PTR_ERR(task); 8932 goto out; 8933 } 8934 ret = rpc_wait_for_completion_task(task); 8935 if (!ret) 8936 ret = task->tk_status; 8937 rpc_put_task(task); 8938 out: 8939 dprintk("<-- %s status=%d\n", __func__, ret); 8940 return ret; 8941 } 8942 8943 struct nfs4_reclaim_complete_data { 8944 struct nfs_client *clp; 8945 struct nfs41_reclaim_complete_args arg; 8946 struct nfs41_reclaim_complete_res res; 8947 }; 8948 8949 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) 8950 { 8951 struct nfs4_reclaim_complete_data *calldata = data; 8952 8953 nfs4_setup_sequence(calldata->clp, 8954 &calldata->arg.seq_args, 8955 &calldata->res.seq_res, 8956 task); 8957 } 8958 8959 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) 8960 { 8961 switch(task->tk_status) { 8962 case 0: 8963 wake_up_all(&clp->cl_lock_waitq); 8964 /* Fallthrough */ 8965 case -NFS4ERR_COMPLETE_ALREADY: 8966 case -NFS4ERR_WRONG_CRED: /* What to do here? */ 8967 break; 8968 case -NFS4ERR_DELAY: 8969 rpc_delay(task, NFS4_POLL_RETRY_MAX); 8970 /* fall through */ 8971 case -NFS4ERR_RETRY_UNCACHED_REP: 8972 return -EAGAIN; 8973 case -NFS4ERR_BADSESSION: 8974 case -NFS4ERR_DEADSESSION: 8975 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 8976 break; 8977 default: 8978 nfs4_schedule_lease_recovery(clp); 8979 } 8980 return 0; 8981 } 8982 8983 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) 8984 { 8985 struct nfs4_reclaim_complete_data *calldata = data; 8986 struct nfs_client *clp = calldata->clp; 8987 struct nfs4_sequence_res *res = &calldata->res.seq_res; 8988 8989 dprintk("--> %s\n", __func__); 8990 if (!nfs41_sequence_done(task, res)) 8991 return; 8992 8993 trace_nfs4_reclaim_complete(clp, task->tk_status); 8994 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { 8995 rpc_restart_call_prepare(task); 8996 return; 8997 } 8998 dprintk("<-- %s\n", __func__); 8999 } 9000 9001 static void nfs4_free_reclaim_complete_data(void *data) 9002 { 9003 struct nfs4_reclaim_complete_data *calldata = data; 9004 9005 kfree(calldata); 9006 } 9007 9008 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { 9009 .rpc_call_prepare = nfs4_reclaim_complete_prepare, 9010 .rpc_call_done = nfs4_reclaim_complete_done, 9011 .rpc_release = nfs4_free_reclaim_complete_data, 9012 }; 9013 9014 /* 9015 * Issue a global reclaim complete. 9016 */ 9017 static int nfs41_proc_reclaim_complete(struct nfs_client *clp, 9018 const struct cred *cred) 9019 { 9020 struct nfs4_reclaim_complete_data *calldata; 9021 struct rpc_message msg = { 9022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], 9023 .rpc_cred = cred, 9024 }; 9025 struct rpc_task_setup task_setup_data = { 9026 .rpc_client = clp->cl_rpcclient, 9027 .rpc_message = &msg, 9028 .callback_ops = &nfs4_reclaim_complete_call_ops, 9029 .flags = RPC_TASK_NO_ROUND_ROBIN, 9030 }; 9031 int status = -ENOMEM; 9032 9033 dprintk("--> %s\n", __func__); 9034 calldata = kzalloc(sizeof(*calldata), GFP_NOFS); 9035 if (calldata == NULL) 9036 goto out; 9037 calldata->clp = clp; 9038 calldata->arg.one_fs = 0; 9039 9040 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1); 9041 msg.rpc_argp = &calldata->arg; 9042 msg.rpc_resp = &calldata->res; 9043 task_setup_data.callback_data = calldata; 9044 status = nfs4_call_sync_custom(&task_setup_data); 9045 out: 9046 dprintk("<-- %s status=%d\n", __func__, status); 9047 return status; 9048 } 9049 9050 static void 9051 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) 9052 { 9053 struct nfs4_layoutget *lgp = calldata; 9054 struct nfs_server *server = NFS_SERVER(lgp->args.inode); 9055 9056 dprintk("--> %s\n", __func__); 9057 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args, 9058 &lgp->res.seq_res, task); 9059 dprintk("<-- %s\n", __func__); 9060 } 9061 9062 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) 9063 { 9064 struct nfs4_layoutget *lgp = calldata; 9065 9066 dprintk("--> %s\n", __func__); 9067 nfs41_sequence_process(task, &lgp->res.seq_res); 9068 dprintk("<-- %s\n", __func__); 9069 } 9070 9071 static int 9072 nfs4_layoutget_handle_exception(struct rpc_task *task, 9073 struct nfs4_layoutget *lgp, struct nfs4_exception *exception) 9074 { 9075 struct inode *inode = lgp->args.inode; 9076 struct nfs_server *server = NFS_SERVER(inode); 9077 struct pnfs_layout_hdr *lo; 9078 int nfs4err = task->tk_status; 9079 int err, status = 0; 9080 LIST_HEAD(head); 9081 9082 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status); 9083 9084 nfs4_sequence_free_slot(&lgp->res.seq_res); 9085 9086 switch (nfs4err) { 9087 case 0: 9088 goto out; 9089 9090 /* 9091 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs 9092 * on the file. set tk_status to -ENODATA to tell upper layer to 9093 * retry go inband. 9094 */ 9095 case -NFS4ERR_LAYOUTUNAVAILABLE: 9096 status = -ENODATA; 9097 goto out; 9098 /* 9099 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of 9100 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3). 9101 */ 9102 case -NFS4ERR_BADLAYOUT: 9103 status = -EOVERFLOW; 9104 goto out; 9105 /* 9106 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client 9107 * (or clients) writing to the same RAID stripe except when 9108 * the minlength argument is 0 (see RFC5661 section 18.43.3). 9109 * 9110 * Treat it like we would RECALLCONFLICT -- we retry for a little 9111 * while, and then eventually give up. 9112 */ 9113 case -NFS4ERR_LAYOUTTRYLATER: 9114 if (lgp->args.minlength == 0) { 9115 status = -EOVERFLOW; 9116 goto out; 9117 } 9118 status = -EBUSY; 9119 break; 9120 case -NFS4ERR_RECALLCONFLICT: 9121 status = -ERECALLCONFLICT; 9122 break; 9123 case -NFS4ERR_DELEG_REVOKED: 9124 case -NFS4ERR_ADMIN_REVOKED: 9125 case -NFS4ERR_EXPIRED: 9126 case -NFS4ERR_BAD_STATEID: 9127 exception->timeout = 0; 9128 spin_lock(&inode->i_lock); 9129 lo = NFS_I(inode)->layout; 9130 /* If the open stateid was bad, then recover it. */ 9131 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || 9132 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) { 9133 spin_unlock(&inode->i_lock); 9134 exception->state = lgp->args.ctx->state; 9135 exception->stateid = &lgp->args.stateid; 9136 break; 9137 } 9138 9139 /* 9140 * Mark the bad layout state as invalid, then retry 9141 */ 9142 pnfs_mark_layout_stateid_invalid(lo, &head); 9143 spin_unlock(&inode->i_lock); 9144 nfs_commit_inode(inode, 0); 9145 pnfs_free_lseg_list(&head); 9146 status = -EAGAIN; 9147 goto out; 9148 } 9149 9150 err = nfs4_handle_exception(server, nfs4err, exception); 9151 if (!status) { 9152 if (exception->retry) 9153 status = -EAGAIN; 9154 else 9155 status = err; 9156 } 9157 out: 9158 dprintk("<-- %s\n", __func__); 9159 return status; 9160 } 9161 9162 size_t max_response_pages(struct nfs_server *server) 9163 { 9164 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; 9165 return nfs_page_array_len(0, max_resp_sz); 9166 } 9167 9168 static void nfs4_layoutget_release(void *calldata) 9169 { 9170 struct nfs4_layoutget *lgp = calldata; 9171 9172 dprintk("--> %s\n", __func__); 9173 nfs4_sequence_free_slot(&lgp->res.seq_res); 9174 pnfs_layoutget_free(lgp); 9175 dprintk("<-- %s\n", __func__); 9176 } 9177 9178 static const struct rpc_call_ops nfs4_layoutget_call_ops = { 9179 .rpc_call_prepare = nfs4_layoutget_prepare, 9180 .rpc_call_done = nfs4_layoutget_done, 9181 .rpc_release = nfs4_layoutget_release, 9182 }; 9183 9184 struct pnfs_layout_segment * 9185 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout) 9186 { 9187 struct inode *inode = lgp->args.inode; 9188 struct nfs_server *server = NFS_SERVER(inode); 9189 struct rpc_task *task; 9190 struct rpc_message msg = { 9191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], 9192 .rpc_argp = &lgp->args, 9193 .rpc_resp = &lgp->res, 9194 .rpc_cred = lgp->cred, 9195 }; 9196 struct rpc_task_setup task_setup_data = { 9197 .rpc_client = server->client, 9198 .rpc_message = &msg, 9199 .callback_ops = &nfs4_layoutget_call_ops, 9200 .callback_data = lgp, 9201 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, 9202 }; 9203 struct pnfs_layout_segment *lseg = NULL; 9204 struct nfs4_exception exception = { 9205 .inode = inode, 9206 .timeout = *timeout, 9207 }; 9208 int status = 0; 9209 9210 dprintk("--> %s\n", __func__); 9211 9212 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */ 9213 pnfs_get_layout_hdr(NFS_I(inode)->layout); 9214 9215 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0); 9216 9217 task = rpc_run_task(&task_setup_data); 9218 9219 status = rpc_wait_for_completion_task(task); 9220 if (status != 0) 9221 goto out; 9222 9223 if (task->tk_status < 0) { 9224 status = nfs4_layoutget_handle_exception(task, lgp, &exception); 9225 *timeout = exception.timeout; 9226 } else if (lgp->res.layoutp->len == 0) { 9227 status = -EAGAIN; 9228 *timeout = nfs4_update_delay(&exception.timeout); 9229 } else 9230 lseg = pnfs_layout_process(lgp); 9231 out: 9232 trace_nfs4_layoutget(lgp->args.ctx, 9233 &lgp->args.range, 9234 &lgp->res.range, 9235 &lgp->res.stateid, 9236 status); 9237 9238 rpc_put_task(task); 9239 dprintk("<-- %s status=%d\n", __func__, status); 9240 if (status) 9241 return ERR_PTR(status); 9242 return lseg; 9243 } 9244 9245 static void 9246 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) 9247 { 9248 struct nfs4_layoutreturn *lrp = calldata; 9249 9250 dprintk("--> %s\n", __func__); 9251 nfs4_setup_sequence(lrp->clp, 9252 &lrp->args.seq_args, 9253 &lrp->res.seq_res, 9254 task); 9255 if (!pnfs_layout_is_valid(lrp->args.layout)) 9256 rpc_exit(task, 0); 9257 } 9258 9259 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) 9260 { 9261 struct nfs4_layoutreturn *lrp = calldata; 9262 struct nfs_server *server; 9263 9264 dprintk("--> %s\n", __func__); 9265 9266 if (!nfs41_sequence_process(task, &lrp->res.seq_res)) 9267 return; 9268 9269 /* 9270 * Was there an RPC level error? Assume the call succeeded, 9271 * and that we need to release the layout 9272 */ 9273 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) { 9274 lrp->res.lrs_present = 0; 9275 return; 9276 } 9277 9278 server = NFS_SERVER(lrp->args.inode); 9279 switch (task->tk_status) { 9280 case -NFS4ERR_OLD_STATEID: 9281 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid, 9282 &lrp->args.range, 9283 lrp->args.inode)) 9284 goto out_restart; 9285 /* Fallthrough */ 9286 default: 9287 task->tk_status = 0; 9288 /* Fallthrough */ 9289 case 0: 9290 break; 9291 case -NFS4ERR_DELAY: 9292 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN) 9293 break; 9294 goto out_restart; 9295 } 9296 dprintk("<-- %s\n", __func__); 9297 return; 9298 out_restart: 9299 task->tk_status = 0; 9300 nfs4_sequence_free_slot(&lrp->res.seq_res); 9301 rpc_restart_call_prepare(task); 9302 } 9303 9304 static void nfs4_layoutreturn_release(void *calldata) 9305 { 9306 struct nfs4_layoutreturn *lrp = calldata; 9307 struct pnfs_layout_hdr *lo = lrp->args.layout; 9308 9309 dprintk("--> %s\n", __func__); 9310 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range, 9311 lrp->res.lrs_present ? &lrp->res.stateid : NULL); 9312 nfs4_sequence_free_slot(&lrp->res.seq_res); 9313 if (lrp->ld_private.ops && lrp->ld_private.ops->free) 9314 lrp->ld_private.ops->free(&lrp->ld_private); 9315 pnfs_put_layout_hdr(lrp->args.layout); 9316 nfs_iput_and_deactive(lrp->inode); 9317 put_cred(lrp->cred); 9318 kfree(calldata); 9319 dprintk("<-- %s\n", __func__); 9320 } 9321 9322 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { 9323 .rpc_call_prepare = nfs4_layoutreturn_prepare, 9324 .rpc_call_done = nfs4_layoutreturn_done, 9325 .rpc_release = nfs4_layoutreturn_release, 9326 }; 9327 9328 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync) 9329 { 9330 struct rpc_task *task; 9331 struct rpc_message msg = { 9332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], 9333 .rpc_argp = &lrp->args, 9334 .rpc_resp = &lrp->res, 9335 .rpc_cred = lrp->cred, 9336 }; 9337 struct rpc_task_setup task_setup_data = { 9338 .rpc_client = NFS_SERVER(lrp->args.inode)->client, 9339 .rpc_message = &msg, 9340 .callback_ops = &nfs4_layoutreturn_call_ops, 9341 .callback_data = lrp, 9342 }; 9343 int status = 0; 9344 9345 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client, 9346 NFS_SP4_MACH_CRED_PNFS_CLEANUP, 9347 &task_setup_data.rpc_client, &msg); 9348 9349 dprintk("--> %s\n", __func__); 9350 if (!sync) { 9351 lrp->inode = nfs_igrab_and_active(lrp->args.inode); 9352 if (!lrp->inode) { 9353 nfs4_layoutreturn_release(lrp); 9354 return -EAGAIN; 9355 } 9356 task_setup_data.flags |= RPC_TASK_ASYNC; 9357 } 9358 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0); 9359 task = rpc_run_task(&task_setup_data); 9360 if (IS_ERR(task)) 9361 return PTR_ERR(task); 9362 if (sync) 9363 status = task->tk_status; 9364 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status); 9365 dprintk("<-- %s status=%d\n", __func__, status); 9366 rpc_put_task(task); 9367 return status; 9368 } 9369 9370 static int 9371 _nfs4_proc_getdeviceinfo(struct nfs_server *server, 9372 struct pnfs_device *pdev, 9373 const struct cred *cred) 9374 { 9375 struct nfs4_getdeviceinfo_args args = { 9376 .pdev = pdev, 9377 .notify_types = NOTIFY_DEVICEID4_CHANGE | 9378 NOTIFY_DEVICEID4_DELETE, 9379 }; 9380 struct nfs4_getdeviceinfo_res res = { 9381 .pdev = pdev, 9382 }; 9383 struct rpc_message msg = { 9384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], 9385 .rpc_argp = &args, 9386 .rpc_resp = &res, 9387 .rpc_cred = cred, 9388 }; 9389 int status; 9390 9391 dprintk("--> %s\n", __func__); 9392 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 9393 if (res.notification & ~args.notify_types) 9394 dprintk("%s: unsupported notification\n", __func__); 9395 if (res.notification != args.notify_types) 9396 pdev->nocache = 1; 9397 9398 dprintk("<-- %s status=%d\n", __func__, status); 9399 9400 return status; 9401 } 9402 9403 int nfs4_proc_getdeviceinfo(struct nfs_server *server, 9404 struct pnfs_device *pdev, 9405 const struct cred *cred) 9406 { 9407 struct nfs4_exception exception = { }; 9408 int err; 9409 9410 do { 9411 err = nfs4_handle_exception(server, 9412 _nfs4_proc_getdeviceinfo(server, pdev, cred), 9413 &exception); 9414 } while (exception.retry); 9415 return err; 9416 } 9417 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); 9418 9419 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) 9420 { 9421 struct nfs4_layoutcommit_data *data = calldata; 9422 struct nfs_server *server = NFS_SERVER(data->args.inode); 9423 9424 nfs4_setup_sequence(server->nfs_client, 9425 &data->args.seq_args, 9426 &data->res.seq_res, 9427 task); 9428 } 9429 9430 static void 9431 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) 9432 { 9433 struct nfs4_layoutcommit_data *data = calldata; 9434 struct nfs_server *server = NFS_SERVER(data->args.inode); 9435 9436 if (!nfs41_sequence_done(task, &data->res.seq_res)) 9437 return; 9438 9439 switch (task->tk_status) { /* Just ignore these failures */ 9440 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ 9441 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ 9442 case -NFS4ERR_BADLAYOUT: /* no layout */ 9443 case -NFS4ERR_GRACE: /* loca_recalim always false */ 9444 task->tk_status = 0; 9445 case 0: 9446 break; 9447 default: 9448 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) { 9449 rpc_restart_call_prepare(task); 9450 return; 9451 } 9452 } 9453 } 9454 9455 static void nfs4_layoutcommit_release(void *calldata) 9456 { 9457 struct nfs4_layoutcommit_data *data = calldata; 9458 9459 pnfs_cleanup_layoutcommit(data); 9460 nfs_post_op_update_inode_force_wcc(data->args.inode, 9461 data->res.fattr); 9462 put_cred(data->cred); 9463 nfs_iput_and_deactive(data->inode); 9464 kfree(data); 9465 } 9466 9467 static const struct rpc_call_ops nfs4_layoutcommit_ops = { 9468 .rpc_call_prepare = nfs4_layoutcommit_prepare, 9469 .rpc_call_done = nfs4_layoutcommit_done, 9470 .rpc_release = nfs4_layoutcommit_release, 9471 }; 9472 9473 int 9474 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) 9475 { 9476 struct rpc_message msg = { 9477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], 9478 .rpc_argp = &data->args, 9479 .rpc_resp = &data->res, 9480 .rpc_cred = data->cred, 9481 }; 9482 struct rpc_task_setup task_setup_data = { 9483 .task = &data->task, 9484 .rpc_client = NFS_CLIENT(data->args.inode), 9485 .rpc_message = &msg, 9486 .callback_ops = &nfs4_layoutcommit_ops, 9487 .callback_data = data, 9488 }; 9489 struct rpc_task *task; 9490 int status = 0; 9491 9492 dprintk("NFS: initiating layoutcommit call. sync %d " 9493 "lbw: %llu inode %lu\n", sync, 9494 data->args.lastbytewritten, 9495 data->args.inode->i_ino); 9496 9497 if (!sync) { 9498 data->inode = nfs_igrab_and_active(data->args.inode); 9499 if (data->inode == NULL) { 9500 nfs4_layoutcommit_release(data); 9501 return -EAGAIN; 9502 } 9503 task_setup_data.flags = RPC_TASK_ASYNC; 9504 } 9505 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); 9506 task = rpc_run_task(&task_setup_data); 9507 if (IS_ERR(task)) 9508 return PTR_ERR(task); 9509 if (sync) 9510 status = task->tk_status; 9511 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status); 9512 dprintk("%s: status %d\n", __func__, status); 9513 rpc_put_task(task); 9514 return status; 9515 } 9516 9517 /* 9518 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if 9519 * possible) as per RFC3530bis and RFC5661 Security Considerations sections 9520 */ 9521 static int 9522 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, 9523 struct nfs_fsinfo *info, 9524 struct nfs4_secinfo_flavors *flavors, bool use_integrity) 9525 { 9526 struct nfs41_secinfo_no_name_args args = { 9527 .style = SECINFO_STYLE_CURRENT_FH, 9528 }; 9529 struct nfs4_secinfo_res res = { 9530 .flavors = flavors, 9531 }; 9532 struct rpc_message msg = { 9533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], 9534 .rpc_argp = &args, 9535 .rpc_resp = &res, 9536 }; 9537 struct rpc_clnt *clnt = server->client; 9538 struct nfs4_call_sync_data data = { 9539 .seq_server = server, 9540 .seq_args = &args.seq_args, 9541 .seq_res = &res.seq_res, 9542 }; 9543 struct rpc_task_setup task_setup = { 9544 .rpc_client = server->client, 9545 .rpc_message = &msg, 9546 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops, 9547 .callback_data = &data, 9548 .flags = RPC_TASK_NO_ROUND_ROBIN, 9549 }; 9550 const struct cred *cred = NULL; 9551 int status; 9552 9553 if (use_integrity) { 9554 clnt = server->nfs_client->cl_rpcclient; 9555 task_setup.rpc_client = clnt; 9556 9557 cred = nfs4_get_clid_cred(server->nfs_client); 9558 msg.rpc_cred = cred; 9559 } 9560 9561 dprintk("--> %s\n", __func__); 9562 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); 9563 status = nfs4_call_sync_custom(&task_setup); 9564 dprintk("<-- %s status=%d\n", __func__, status); 9565 9566 put_cred(cred); 9567 9568 return status; 9569 } 9570 9571 static int 9572 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, 9573 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) 9574 { 9575 struct nfs4_exception exception = { 9576 .interruptible = true, 9577 }; 9578 int err; 9579 do { 9580 /* first try using integrity protection */ 9581 err = -NFS4ERR_WRONGSEC; 9582 9583 /* try to use integrity protection with machine cred */ 9584 if (_nfs4_is_integrity_protected(server->nfs_client)) 9585 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, 9586 flavors, true); 9587 9588 /* 9589 * if unable to use integrity protection, or SECINFO with 9590 * integrity protection returns NFS4ERR_WRONGSEC (which is 9591 * disallowed by spec, but exists in deployed servers) use 9592 * the current filesystem's rpc_client and the user cred. 9593 */ 9594 if (err == -NFS4ERR_WRONGSEC) 9595 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, 9596 flavors, false); 9597 9598 switch (err) { 9599 case 0: 9600 case -NFS4ERR_WRONGSEC: 9601 case -ENOTSUPP: 9602 goto out; 9603 default: 9604 err = nfs4_handle_exception(server, err, &exception); 9605 } 9606 } while (exception.retry); 9607 out: 9608 return err; 9609 } 9610 9611 static int 9612 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 9613 struct nfs_fsinfo *info) 9614 { 9615 int err; 9616 struct page *page; 9617 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR; 9618 struct nfs4_secinfo_flavors *flavors; 9619 struct nfs4_secinfo4 *secinfo; 9620 int i; 9621 9622 page = alloc_page(GFP_KERNEL); 9623 if (!page) { 9624 err = -ENOMEM; 9625 goto out; 9626 } 9627 9628 flavors = page_address(page); 9629 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); 9630 9631 /* 9632 * Fall back on "guess and check" method if 9633 * the server doesn't support SECINFO_NO_NAME 9634 */ 9635 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) { 9636 err = nfs4_find_root_sec(server, fhandle, info); 9637 goto out_freepage; 9638 } 9639 if (err) 9640 goto out_freepage; 9641 9642 for (i = 0; i < flavors->num_flavors; i++) { 9643 secinfo = &flavors->flavors[i]; 9644 9645 switch (secinfo->flavor) { 9646 case RPC_AUTH_NULL: 9647 case RPC_AUTH_UNIX: 9648 case RPC_AUTH_GSS: 9649 flavor = rpcauth_get_pseudoflavor(secinfo->flavor, 9650 &secinfo->flavor_info); 9651 break; 9652 default: 9653 flavor = RPC_AUTH_MAXFLAVOR; 9654 break; 9655 } 9656 9657 if (!nfs_auth_info_match(&server->auth_info, flavor)) 9658 flavor = RPC_AUTH_MAXFLAVOR; 9659 9660 if (flavor != RPC_AUTH_MAXFLAVOR) { 9661 err = nfs4_lookup_root_sec(server, fhandle, 9662 info, flavor); 9663 if (!err) 9664 break; 9665 } 9666 } 9667 9668 if (flavor == RPC_AUTH_MAXFLAVOR) 9669 err = -EPERM; 9670 9671 out_freepage: 9672 put_page(page); 9673 if (err == -EACCES) 9674 return -EPERM; 9675 out: 9676 return err; 9677 } 9678 9679 static int _nfs41_test_stateid(struct nfs_server *server, 9680 nfs4_stateid *stateid, 9681 const struct cred *cred) 9682 { 9683 int status; 9684 struct nfs41_test_stateid_args args = { 9685 .stateid = stateid, 9686 }; 9687 struct nfs41_test_stateid_res res; 9688 struct rpc_message msg = { 9689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], 9690 .rpc_argp = &args, 9691 .rpc_resp = &res, 9692 .rpc_cred = cred, 9693 }; 9694 struct rpc_clnt *rpc_client = server->client; 9695 9696 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, 9697 &rpc_client, &msg); 9698 9699 dprintk("NFS call test_stateid %p\n", stateid); 9700 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); 9701 status = nfs4_call_sync_sequence(rpc_client, server, &msg, 9702 &args.seq_args, &res.seq_res); 9703 if (status != NFS_OK) { 9704 dprintk("NFS reply test_stateid: failed, %d\n", status); 9705 return status; 9706 } 9707 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); 9708 return -res.status; 9709 } 9710 9711 static void nfs4_handle_delay_or_session_error(struct nfs_server *server, 9712 int err, struct nfs4_exception *exception) 9713 { 9714 exception->retry = 0; 9715 switch(err) { 9716 case -NFS4ERR_DELAY: 9717 case -NFS4ERR_RETRY_UNCACHED_REP: 9718 nfs4_handle_exception(server, err, exception); 9719 break; 9720 case -NFS4ERR_BADSESSION: 9721 case -NFS4ERR_BADSLOT: 9722 case -NFS4ERR_BAD_HIGH_SLOT: 9723 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 9724 case -NFS4ERR_DEADSESSION: 9725 nfs4_do_handle_exception(server, err, exception); 9726 } 9727 } 9728 9729 /** 9730 * nfs41_test_stateid - perform a TEST_STATEID operation 9731 * 9732 * @server: server / transport on which to perform the operation 9733 * @stateid: state ID to test 9734 * @cred: credential 9735 * 9736 * Returns NFS_OK if the server recognizes that "stateid" is valid. 9737 * Otherwise a negative NFS4ERR value is returned if the operation 9738 * failed or the state ID is not currently valid. 9739 */ 9740 static int nfs41_test_stateid(struct nfs_server *server, 9741 nfs4_stateid *stateid, 9742 const struct cred *cred) 9743 { 9744 struct nfs4_exception exception = { 9745 .interruptible = true, 9746 }; 9747 int err; 9748 do { 9749 err = _nfs41_test_stateid(server, stateid, cred); 9750 nfs4_handle_delay_or_session_error(server, err, &exception); 9751 } while (exception.retry); 9752 return err; 9753 } 9754 9755 struct nfs_free_stateid_data { 9756 struct nfs_server *server; 9757 struct nfs41_free_stateid_args args; 9758 struct nfs41_free_stateid_res res; 9759 }; 9760 9761 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata) 9762 { 9763 struct nfs_free_stateid_data *data = calldata; 9764 nfs4_setup_sequence(data->server->nfs_client, 9765 &data->args.seq_args, 9766 &data->res.seq_res, 9767 task); 9768 } 9769 9770 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata) 9771 { 9772 struct nfs_free_stateid_data *data = calldata; 9773 9774 nfs41_sequence_done(task, &data->res.seq_res); 9775 9776 switch (task->tk_status) { 9777 case -NFS4ERR_DELAY: 9778 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN) 9779 rpc_restart_call_prepare(task); 9780 } 9781 } 9782 9783 static void nfs41_free_stateid_release(void *calldata) 9784 { 9785 kfree(calldata); 9786 } 9787 9788 static const struct rpc_call_ops nfs41_free_stateid_ops = { 9789 .rpc_call_prepare = nfs41_free_stateid_prepare, 9790 .rpc_call_done = nfs41_free_stateid_done, 9791 .rpc_release = nfs41_free_stateid_release, 9792 }; 9793 9794 /** 9795 * nfs41_free_stateid - perform a FREE_STATEID operation 9796 * 9797 * @server: server / transport on which to perform the operation 9798 * @stateid: state ID to release 9799 * @cred: credential 9800 * @privileged: set to true if this call needs to be privileged 9801 * 9802 * Note: this function is always asynchronous. 9803 */ 9804 static int nfs41_free_stateid(struct nfs_server *server, 9805 const nfs4_stateid *stateid, 9806 const struct cred *cred, 9807 bool privileged) 9808 { 9809 struct rpc_message msg = { 9810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], 9811 .rpc_cred = cred, 9812 }; 9813 struct rpc_task_setup task_setup = { 9814 .rpc_client = server->client, 9815 .rpc_message = &msg, 9816 .callback_ops = &nfs41_free_stateid_ops, 9817 .flags = RPC_TASK_ASYNC, 9818 }; 9819 struct nfs_free_stateid_data *data; 9820 struct rpc_task *task; 9821 9822 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, 9823 &task_setup.rpc_client, &msg); 9824 9825 dprintk("NFS call free_stateid %p\n", stateid); 9826 data = kmalloc(sizeof(*data), GFP_NOFS); 9827 if (!data) 9828 return -ENOMEM; 9829 data->server = server; 9830 nfs4_stateid_copy(&data->args.stateid, stateid); 9831 9832 task_setup.callback_data = data; 9833 9834 msg.rpc_argp = &data->args; 9835 msg.rpc_resp = &data->res; 9836 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged); 9837 task = rpc_run_task(&task_setup); 9838 if (IS_ERR(task)) 9839 return PTR_ERR(task); 9840 rpc_put_task(task); 9841 return 0; 9842 } 9843 9844 static void 9845 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) 9846 { 9847 const struct cred *cred = lsp->ls_state->owner->so_cred; 9848 9849 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false); 9850 nfs4_free_lock_state(server, lsp); 9851 } 9852 9853 static bool nfs41_match_stateid(const nfs4_stateid *s1, 9854 const nfs4_stateid *s2) 9855 { 9856 if (s1->type != s2->type) 9857 return false; 9858 9859 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) 9860 return false; 9861 9862 if (s1->seqid == s2->seqid) 9863 return true; 9864 9865 return s1->seqid == 0 || s2->seqid == 0; 9866 } 9867 9868 #endif /* CONFIG_NFS_V4_1 */ 9869 9870 static bool nfs4_match_stateid(const nfs4_stateid *s1, 9871 const nfs4_stateid *s2) 9872 { 9873 return nfs4_stateid_match(s1, s2); 9874 } 9875 9876 9877 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { 9878 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, 9879 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, 9880 .recover_open = nfs4_open_reclaim, 9881 .recover_lock = nfs4_lock_reclaim, 9882 .establish_clid = nfs4_init_clientid, 9883 .detect_trunking = nfs40_discover_server_trunking, 9884 }; 9885 9886 #if defined(CONFIG_NFS_V4_1) 9887 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { 9888 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, 9889 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, 9890 .recover_open = nfs4_open_reclaim, 9891 .recover_lock = nfs4_lock_reclaim, 9892 .establish_clid = nfs41_init_clientid, 9893 .reclaim_complete = nfs41_proc_reclaim_complete, 9894 .detect_trunking = nfs41_discover_server_trunking, 9895 }; 9896 #endif /* CONFIG_NFS_V4_1 */ 9897 9898 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { 9899 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, 9900 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, 9901 .recover_open = nfs40_open_expired, 9902 .recover_lock = nfs4_lock_expired, 9903 .establish_clid = nfs4_init_clientid, 9904 }; 9905 9906 #if defined(CONFIG_NFS_V4_1) 9907 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { 9908 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, 9909 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, 9910 .recover_open = nfs41_open_expired, 9911 .recover_lock = nfs41_lock_expired, 9912 .establish_clid = nfs41_init_clientid, 9913 }; 9914 #endif /* CONFIG_NFS_V4_1 */ 9915 9916 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { 9917 .sched_state_renewal = nfs4_proc_async_renew, 9918 .get_state_renewal_cred = nfs4_get_renew_cred, 9919 .renew_lease = nfs4_proc_renew, 9920 }; 9921 9922 #if defined(CONFIG_NFS_V4_1) 9923 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { 9924 .sched_state_renewal = nfs41_proc_async_sequence, 9925 .get_state_renewal_cred = nfs4_get_machine_cred, 9926 .renew_lease = nfs4_proc_sequence, 9927 }; 9928 #endif 9929 9930 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = { 9931 .get_locations = _nfs40_proc_get_locations, 9932 .fsid_present = _nfs40_proc_fsid_present, 9933 }; 9934 9935 #if defined(CONFIG_NFS_V4_1) 9936 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = { 9937 .get_locations = _nfs41_proc_get_locations, 9938 .fsid_present = _nfs41_proc_fsid_present, 9939 }; 9940 #endif /* CONFIG_NFS_V4_1 */ 9941 9942 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = { 9943 .minor_version = 0, 9944 .init_caps = NFS_CAP_READDIRPLUS 9945 | NFS_CAP_ATOMIC_OPEN 9946 | NFS_CAP_POSIX_LOCK, 9947 .init_client = nfs40_init_client, 9948 .shutdown_client = nfs40_shutdown_client, 9949 .match_stateid = nfs4_match_stateid, 9950 .find_root_sec = nfs4_find_root_sec, 9951 .free_lock_state = nfs4_release_lockowner, 9952 .test_and_free_expired = nfs40_test_and_free_expired_stateid, 9953 .alloc_seqid = nfs_alloc_seqid, 9954 .call_sync_ops = &nfs40_call_sync_ops, 9955 .reboot_recovery_ops = &nfs40_reboot_recovery_ops, 9956 .nograce_recovery_ops = &nfs40_nograce_recovery_ops, 9957 .state_renewal_ops = &nfs40_state_renewal_ops, 9958 .mig_recovery_ops = &nfs40_mig_recovery_ops, 9959 }; 9960 9961 #if defined(CONFIG_NFS_V4_1) 9962 static struct nfs_seqid * 9963 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2) 9964 { 9965 return NULL; 9966 } 9967 9968 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { 9969 .minor_version = 1, 9970 .init_caps = NFS_CAP_READDIRPLUS 9971 | NFS_CAP_ATOMIC_OPEN 9972 | NFS_CAP_POSIX_LOCK 9973 | NFS_CAP_STATEID_NFSV41 9974 | NFS_CAP_ATOMIC_OPEN_V1 9975 | NFS_CAP_LGOPEN, 9976 .init_client = nfs41_init_client, 9977 .shutdown_client = nfs41_shutdown_client, 9978 .match_stateid = nfs41_match_stateid, 9979 .find_root_sec = nfs41_find_root_sec, 9980 .free_lock_state = nfs41_free_lock_state, 9981 .test_and_free_expired = nfs41_test_and_free_expired_stateid, 9982 .alloc_seqid = nfs_alloc_no_seqid, 9983 .session_trunk = nfs4_test_session_trunk, 9984 .call_sync_ops = &nfs41_call_sync_ops, 9985 .reboot_recovery_ops = &nfs41_reboot_recovery_ops, 9986 .nograce_recovery_ops = &nfs41_nograce_recovery_ops, 9987 .state_renewal_ops = &nfs41_state_renewal_ops, 9988 .mig_recovery_ops = &nfs41_mig_recovery_ops, 9989 }; 9990 #endif 9991 9992 #if defined(CONFIG_NFS_V4_2) 9993 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = { 9994 .minor_version = 2, 9995 .init_caps = NFS_CAP_READDIRPLUS 9996 | NFS_CAP_ATOMIC_OPEN 9997 | NFS_CAP_POSIX_LOCK 9998 | NFS_CAP_STATEID_NFSV41 9999 | NFS_CAP_ATOMIC_OPEN_V1 10000 | NFS_CAP_LGOPEN 10001 | NFS_CAP_ALLOCATE 10002 | NFS_CAP_COPY 10003 | NFS_CAP_OFFLOAD_CANCEL 10004 | NFS_CAP_COPY_NOTIFY 10005 | NFS_CAP_DEALLOCATE 10006 | NFS_CAP_SEEK 10007 | NFS_CAP_LAYOUTSTATS 10008 | NFS_CAP_CLONE 10009 | NFS_CAP_LAYOUTERROR, 10010 .init_client = nfs41_init_client, 10011 .shutdown_client = nfs41_shutdown_client, 10012 .match_stateid = nfs41_match_stateid, 10013 .find_root_sec = nfs41_find_root_sec, 10014 .free_lock_state = nfs41_free_lock_state, 10015 .call_sync_ops = &nfs41_call_sync_ops, 10016 .test_and_free_expired = nfs41_test_and_free_expired_stateid, 10017 .alloc_seqid = nfs_alloc_no_seqid, 10018 .session_trunk = nfs4_test_session_trunk, 10019 .reboot_recovery_ops = &nfs41_reboot_recovery_ops, 10020 .nograce_recovery_ops = &nfs41_nograce_recovery_ops, 10021 .state_renewal_ops = &nfs41_state_renewal_ops, 10022 .mig_recovery_ops = &nfs41_mig_recovery_ops, 10023 }; 10024 #endif 10025 10026 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { 10027 [0] = &nfs_v4_0_minor_ops, 10028 #if defined(CONFIG_NFS_V4_1) 10029 [1] = &nfs_v4_1_minor_ops, 10030 #endif 10031 #if defined(CONFIG_NFS_V4_2) 10032 [2] = &nfs_v4_2_minor_ops, 10033 #endif 10034 }; 10035 10036 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size) 10037 { 10038 ssize_t error, error2; 10039 10040 error = generic_listxattr(dentry, list, size); 10041 if (error < 0) 10042 return error; 10043 if (list) { 10044 list += error; 10045 size -= error; 10046 } 10047 10048 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size); 10049 if (error2 < 0) 10050 return error2; 10051 return error + error2; 10052 } 10053 10054 static const struct inode_operations nfs4_dir_inode_operations = { 10055 .create = nfs_create, 10056 .lookup = nfs_lookup, 10057 .atomic_open = nfs_atomic_open, 10058 .link = nfs_link, 10059 .unlink = nfs_unlink, 10060 .symlink = nfs_symlink, 10061 .mkdir = nfs_mkdir, 10062 .rmdir = nfs_rmdir, 10063 .mknod = nfs_mknod, 10064 .rename = nfs_rename, 10065 .permission = nfs_permission, 10066 .getattr = nfs_getattr, 10067 .setattr = nfs_setattr, 10068 .listxattr = nfs4_listxattr, 10069 }; 10070 10071 static const struct inode_operations nfs4_file_inode_operations = { 10072 .permission = nfs_permission, 10073 .getattr = nfs_getattr, 10074 .setattr = nfs_setattr, 10075 .listxattr = nfs4_listxattr, 10076 }; 10077 10078 const struct nfs_rpc_ops nfs_v4_clientops = { 10079 .version = 4, /* protocol version */ 10080 .dentry_ops = &nfs4_dentry_operations, 10081 .dir_inode_ops = &nfs4_dir_inode_operations, 10082 .file_inode_ops = &nfs4_file_inode_operations, 10083 .file_ops = &nfs4_file_operations, 10084 .getroot = nfs4_proc_get_root, 10085 .submount = nfs4_submount, 10086 .try_get_tree = nfs4_try_get_tree, 10087 .getattr = nfs4_proc_getattr, 10088 .setattr = nfs4_proc_setattr, 10089 .lookup = nfs4_proc_lookup, 10090 .lookupp = nfs4_proc_lookupp, 10091 .access = nfs4_proc_access, 10092 .readlink = nfs4_proc_readlink, 10093 .create = nfs4_proc_create, 10094 .remove = nfs4_proc_remove, 10095 .unlink_setup = nfs4_proc_unlink_setup, 10096 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, 10097 .unlink_done = nfs4_proc_unlink_done, 10098 .rename_setup = nfs4_proc_rename_setup, 10099 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, 10100 .rename_done = nfs4_proc_rename_done, 10101 .link = nfs4_proc_link, 10102 .symlink = nfs4_proc_symlink, 10103 .mkdir = nfs4_proc_mkdir, 10104 .rmdir = nfs4_proc_rmdir, 10105 .readdir = nfs4_proc_readdir, 10106 .mknod = nfs4_proc_mknod, 10107 .statfs = nfs4_proc_statfs, 10108 .fsinfo = nfs4_proc_fsinfo, 10109 .pathconf = nfs4_proc_pathconf, 10110 .set_capabilities = nfs4_server_capabilities, 10111 .decode_dirent = nfs4_decode_dirent, 10112 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare, 10113 .read_setup = nfs4_proc_read_setup, 10114 .read_done = nfs4_read_done, 10115 .write_setup = nfs4_proc_write_setup, 10116 .write_done = nfs4_write_done, 10117 .commit_setup = nfs4_proc_commit_setup, 10118 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, 10119 .commit_done = nfs4_commit_done, 10120 .lock = nfs4_proc_lock, 10121 .clear_acl_cache = nfs4_zap_acl_attr, 10122 .close_context = nfs4_close_context, 10123 .open_context = nfs4_atomic_open, 10124 .have_delegation = nfs4_have_delegation, 10125 .alloc_client = nfs4_alloc_client, 10126 .init_client = nfs4_init_client, 10127 .free_client = nfs4_free_client, 10128 .create_server = nfs4_create_server, 10129 .clone_server = nfs_clone_server, 10130 }; 10131 10132 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { 10133 .name = XATTR_NAME_NFSV4_ACL, 10134 .list = nfs4_xattr_list_nfs4_acl, 10135 .get = nfs4_xattr_get_nfs4_acl, 10136 .set = nfs4_xattr_set_nfs4_acl, 10137 }; 10138 10139 const struct xattr_handler *nfs4_xattr_handlers[] = { 10140 &nfs4_xattr_nfs4_acl_handler, 10141 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 10142 &nfs4_xattr_nfs4_label_handler, 10143 #endif 10144 NULL 10145 }; 10146 10147 /* 10148 * Local variables: 10149 * c-basic-offset: 8 10150 * End: 10151 */ 10152