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