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