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