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