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