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