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