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