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