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