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