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