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