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