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