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