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, NFS_FILEID(d_inode(dentry))); 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, NFS_FILEID(d_inode(dentry->d_parent))); 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 if (err) 5308 alias = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir), 5309 err, 5310 &exception)); 5311 } while (exception.retry); 5312 nfs4_label_release_security(label); 5313 5314 return alias; 5315 } 5316 5317 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg, 5318 struct nfs_readdir_res *nr_res) 5319 { 5320 struct inode *dir = d_inode(nr_arg->dentry); 5321 struct nfs_server *server = NFS_SERVER(dir); 5322 struct nfs4_readdir_arg args = { 5323 .fh = NFS_FH(dir), 5324 .pages = nr_arg->pages, 5325 .pgbase = 0, 5326 .count = nr_arg->page_len, 5327 .plus = nr_arg->plus, 5328 }; 5329 struct nfs4_readdir_res res; 5330 struct rpc_message msg = { 5331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], 5332 .rpc_argp = &args, 5333 .rpc_resp = &res, 5334 .rpc_cred = nr_arg->cred, 5335 }; 5336 int status; 5337 5338 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__, 5339 nr_arg->dentry, (unsigned long long)nr_arg->cookie); 5340 if (!(server->caps & NFS_CAP_SECURITY_LABEL)) 5341 args.bitmask = server->attr_bitmask_nl; 5342 else 5343 args.bitmask = server->attr_bitmask; 5344 5345 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args); 5346 res.pgbase = args.pgbase; 5347 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, 5348 &res.seq_res, 0); 5349 if (status >= 0) { 5350 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE); 5351 status += args.pgbase; 5352 } 5353 5354 nfs_invalidate_atime(dir); 5355 5356 dprintk("%s: returns %d\n", __func__, status); 5357 return status; 5358 } 5359 5360 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg, 5361 struct nfs_readdir_res *res) 5362 { 5363 struct nfs4_exception exception = { 5364 .interruptible = true, 5365 }; 5366 int err; 5367 do { 5368 err = _nfs4_proc_readdir(arg, res); 5369 trace_nfs4_readdir(d_inode(arg->dentry), err); 5370 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)), 5371 err, &exception); 5372 } while (exception.retry); 5373 return err; 5374 } 5375 5376 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 5377 struct iattr *sattr, struct nfs4_label *label, dev_t rdev) 5378 { 5379 struct nfs4_createdata *data; 5380 int mode = sattr->ia_mode; 5381 int status = -ENOMEM; 5382 5383 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); 5384 if (data == NULL) 5385 goto out; 5386 5387 if (S_ISFIFO(mode)) 5388 data->arg.ftype = NF4FIFO; 5389 else if (S_ISBLK(mode)) { 5390 data->arg.ftype = NF4BLK; 5391 data->arg.u.device.specdata1 = MAJOR(rdev); 5392 data->arg.u.device.specdata2 = MINOR(rdev); 5393 } 5394 else if (S_ISCHR(mode)) { 5395 data->arg.ftype = NF4CHR; 5396 data->arg.u.device.specdata1 = MAJOR(rdev); 5397 data->arg.u.device.specdata2 = MINOR(rdev); 5398 } else if (!S_ISSOCK(mode)) { 5399 status = -EINVAL; 5400 goto out_free; 5401 } 5402 5403 data->arg.label = label; 5404 status = nfs4_do_create(dir, dentry, data); 5405 out_free: 5406 nfs4_free_createdata(data); 5407 out: 5408 return status; 5409 } 5410 5411 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 5412 struct iattr *sattr, dev_t rdev) 5413 { 5414 struct nfs_server *server = NFS_SERVER(dir); 5415 struct nfs4_exception exception = { 5416 .interruptible = true, 5417 }; 5418 struct nfs4_label l, *label; 5419 int err; 5420 5421 label = nfs4_label_init_security(dir, dentry, sattr, &l); 5422 5423 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 5424 sattr->ia_mode &= ~current_umask(); 5425 do { 5426 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); 5427 trace_nfs4_mknod(dir, &dentry->d_name, err); 5428 err = nfs4_handle_exception(NFS_SERVER(dir), err, 5429 &exception); 5430 } while (exception.retry); 5431 5432 nfs4_label_release_security(label); 5433 5434 return err; 5435 } 5436 5437 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, 5438 struct nfs_fsstat *fsstat) 5439 { 5440 struct nfs4_statfs_arg args = { 5441 .fh = fhandle, 5442 .bitmask = server->attr_bitmask, 5443 }; 5444 struct nfs4_statfs_res res = { 5445 .fsstat = fsstat, 5446 }; 5447 struct rpc_message msg = { 5448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], 5449 .rpc_argp = &args, 5450 .rpc_resp = &res, 5451 }; 5452 5453 nfs_fattr_init(fsstat->fattr); 5454 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 5455 } 5456 5457 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) 5458 { 5459 struct nfs4_exception exception = { 5460 .interruptible = true, 5461 }; 5462 int err; 5463 do { 5464 err = nfs4_handle_exception(server, 5465 _nfs4_proc_statfs(server, fhandle, fsstat), 5466 &exception); 5467 } while (exception.retry); 5468 return err; 5469 } 5470 5471 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, 5472 struct nfs_fsinfo *fsinfo) 5473 { 5474 struct nfs4_fsinfo_arg args = { 5475 .fh = fhandle, 5476 .bitmask = server->attr_bitmask, 5477 }; 5478 struct nfs4_fsinfo_res res = { 5479 .fsinfo = fsinfo, 5480 }; 5481 struct rpc_message msg = { 5482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], 5483 .rpc_argp = &args, 5484 .rpc_resp = &res, 5485 }; 5486 5487 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 5488 } 5489 5490 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 5491 { 5492 struct nfs4_exception exception = { 5493 .interruptible = true, 5494 }; 5495 int err; 5496 5497 do { 5498 err = _nfs4_do_fsinfo(server, fhandle, fsinfo); 5499 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); 5500 if (err == 0) { 5501 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time); 5502 break; 5503 } 5504 err = nfs4_handle_exception(server, err, &exception); 5505 } while (exception.retry); 5506 return err; 5507 } 5508 5509 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 5510 { 5511 int error; 5512 5513 nfs_fattr_init(fsinfo->fattr); 5514 error = nfs4_do_fsinfo(server, fhandle, fsinfo); 5515 if (error == 0) { 5516 /* block layout checks this! */ 5517 server->pnfs_blksize = fsinfo->blksize; 5518 set_pnfs_layoutdriver(server, fhandle, fsinfo); 5519 } 5520 5521 return error; 5522 } 5523 5524 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 5525 struct nfs_pathconf *pathconf) 5526 { 5527 struct nfs4_pathconf_arg args = { 5528 .fh = fhandle, 5529 .bitmask = server->attr_bitmask, 5530 }; 5531 struct nfs4_pathconf_res res = { 5532 .pathconf = pathconf, 5533 }; 5534 struct rpc_message msg = { 5535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], 5536 .rpc_argp = &args, 5537 .rpc_resp = &res, 5538 }; 5539 5540 /* None of the pathconf attributes are mandatory to implement */ 5541 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { 5542 memset(pathconf, 0, sizeof(*pathconf)); 5543 return 0; 5544 } 5545 5546 nfs_fattr_init(pathconf->fattr); 5547 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 5548 } 5549 5550 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 5551 struct nfs_pathconf *pathconf) 5552 { 5553 struct nfs4_exception exception = { 5554 .interruptible = true, 5555 }; 5556 int err; 5557 5558 do { 5559 err = nfs4_handle_exception(server, 5560 _nfs4_proc_pathconf(server, fhandle, pathconf), 5561 &exception); 5562 } while (exception.retry); 5563 return err; 5564 } 5565 5566 int nfs4_set_rw_stateid(nfs4_stateid *stateid, 5567 const struct nfs_open_context *ctx, 5568 const struct nfs_lock_context *l_ctx, 5569 fmode_t fmode) 5570 { 5571 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL); 5572 } 5573 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); 5574 5575 static bool nfs4_stateid_is_current(nfs4_stateid *stateid, 5576 const struct nfs_open_context *ctx, 5577 const struct nfs_lock_context *l_ctx, 5578 fmode_t fmode) 5579 { 5580 nfs4_stateid _current_stateid; 5581 5582 /* If the current stateid represents a lost lock, then exit */ 5583 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO) 5584 return true; 5585 return nfs4_stateid_match(stateid, &_current_stateid); 5586 } 5587 5588 static bool nfs4_error_stateid_expired(int err) 5589 { 5590 switch (err) { 5591 case -NFS4ERR_DELEG_REVOKED: 5592 case -NFS4ERR_ADMIN_REVOKED: 5593 case -NFS4ERR_BAD_STATEID: 5594 case -NFS4ERR_STALE_STATEID: 5595 case -NFS4ERR_OLD_STATEID: 5596 case -NFS4ERR_OPENMODE: 5597 case -NFS4ERR_EXPIRED: 5598 return true; 5599 } 5600 return false; 5601 } 5602 5603 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) 5604 { 5605 struct nfs_server *server = NFS_SERVER(hdr->inode); 5606 5607 trace_nfs4_read(hdr, task->tk_status); 5608 if (task->tk_status < 0) { 5609 struct nfs4_exception exception = { 5610 .inode = hdr->inode, 5611 .state = hdr->args.context->state, 5612 .stateid = &hdr->args.stateid, 5613 .retrans = hdr->retrans, 5614 }; 5615 task->tk_status = nfs4_async_handle_exception(task, 5616 server, task->tk_status, &exception); 5617 hdr->retrans = exception.retrans; 5618 if (exception.retry) { 5619 rpc_restart_call_prepare(task); 5620 return -EAGAIN; 5621 } 5622 } 5623 5624 if (task->tk_status > 0) 5625 renew_lease(server, hdr->timestamp); 5626 return 0; 5627 } 5628 5629 static bool nfs4_read_stateid_changed(struct rpc_task *task, 5630 struct nfs_pgio_args *args) 5631 { 5632 5633 if (!nfs4_error_stateid_expired(task->tk_status) || 5634 nfs4_stateid_is_current(&args->stateid, 5635 args->context, 5636 args->lock_context, 5637 FMODE_READ)) 5638 return false; 5639 rpc_restart_call_prepare(task); 5640 return true; 5641 } 5642 5643 static bool nfs4_read_plus_not_supported(struct rpc_task *task, 5644 struct nfs_pgio_header *hdr) 5645 { 5646 struct nfs_server *server = NFS_SERVER(hdr->inode); 5647 struct rpc_message *msg = &task->tk_msg; 5648 5649 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] && 5650 task->tk_status == -ENOTSUPP) { 5651 server->caps &= ~NFS_CAP_READ_PLUS; 5652 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; 5653 rpc_restart_call_prepare(task); 5654 return true; 5655 } 5656 return false; 5657 } 5658 5659 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 5660 { 5661 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 5662 return -EAGAIN; 5663 if (nfs4_read_stateid_changed(task, &hdr->args)) 5664 return -EAGAIN; 5665 if (nfs4_read_plus_not_supported(task, hdr)) 5666 return -EAGAIN; 5667 if (task->tk_status > 0) 5668 nfs_invalidate_atime(hdr->inode); 5669 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 5670 nfs4_read_done_cb(task, hdr); 5671 } 5672 5673 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS 5674 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr, 5675 struct rpc_message *msg) 5676 { 5677 /* Note: We don't use READ_PLUS with pNFS yet */ 5678 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) { 5679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS]; 5680 return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE); 5681 } 5682 return false; 5683 } 5684 #else 5685 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr, 5686 struct rpc_message *msg) 5687 { 5688 return false; 5689 } 5690 #endif /* CONFIG_NFS_V4_2 */ 5691 5692 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, 5693 struct rpc_message *msg) 5694 { 5695 hdr->timestamp = jiffies; 5696 if (!hdr->pgio_done_cb) 5697 hdr->pgio_done_cb = nfs4_read_done_cb; 5698 if (!nfs42_read_plus_support(hdr, msg)) 5699 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; 5700 nfs4_init_sequence(NFS_SERVER(hdr->inode)->nfs_client, 5701 &hdr->args.seq_args, &hdr->res.seq_res, 0, 0); 5702 } 5703 5704 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, 5705 struct nfs_pgio_header *hdr) 5706 { 5707 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client, 5708 &hdr->args.seq_args, 5709 &hdr->res.seq_res, 5710 task)) 5711 return 0; 5712 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, 5713 hdr->args.lock_context, 5714 hdr->rw_mode) == -EIO) 5715 return -EIO; 5716 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) 5717 return -EIO; 5718 return 0; 5719 } 5720 5721 static int nfs4_write_done_cb(struct rpc_task *task, 5722 struct nfs_pgio_header *hdr) 5723 { 5724 struct inode *inode = hdr->inode; 5725 5726 trace_nfs4_write(hdr, task->tk_status); 5727 if (task->tk_status < 0) { 5728 struct nfs4_exception exception = { 5729 .inode = hdr->inode, 5730 .state = hdr->args.context->state, 5731 .stateid = &hdr->args.stateid, 5732 .retrans = hdr->retrans, 5733 }; 5734 task->tk_status = nfs4_async_handle_exception(task, 5735 NFS_SERVER(inode), task->tk_status, 5736 &exception); 5737 hdr->retrans = exception.retrans; 5738 if (exception.retry) { 5739 rpc_restart_call_prepare(task); 5740 return -EAGAIN; 5741 } 5742 } 5743 if (task->tk_status >= 0) { 5744 renew_lease(NFS_SERVER(inode), hdr->timestamp); 5745 nfs_writeback_update_inode(hdr); 5746 } 5747 return 0; 5748 } 5749 5750 static bool nfs4_write_stateid_changed(struct rpc_task *task, 5751 struct nfs_pgio_args *args) 5752 { 5753 5754 if (!nfs4_error_stateid_expired(task->tk_status) || 5755 nfs4_stateid_is_current(&args->stateid, 5756 args->context, 5757 args->lock_context, 5758 FMODE_WRITE)) 5759 return false; 5760 rpc_restart_call_prepare(task); 5761 return true; 5762 } 5763 5764 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 5765 { 5766 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 5767 return -EAGAIN; 5768 if (nfs4_write_stateid_changed(task, &hdr->args)) 5769 return -EAGAIN; 5770 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 5771 nfs4_write_done_cb(task, hdr); 5772 } 5773 5774 static 5775 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) 5776 { 5777 /* Don't request attributes for pNFS or O_DIRECT writes */ 5778 if (hdr->ds_clp != NULL || hdr->dreq != NULL) 5779 return false; 5780 /* Otherwise, request attributes if and only if we don't hold 5781 * a delegation 5782 */ 5783 return nfs4_have_delegation(hdr->inode, FMODE_READ, 0) == 0; 5784 } 5785 5786 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[], 5787 struct inode *inode, unsigned long cache_validity) 5788 { 5789 struct nfs_server *server = NFS_SERVER(inode); 5790 unsigned int i; 5791 5792 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ); 5793 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity); 5794 5795 if (cache_validity & NFS_INO_INVALID_CHANGE) 5796 bitmask[0] |= FATTR4_WORD0_CHANGE; 5797 if (cache_validity & NFS_INO_INVALID_ATIME) 5798 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS; 5799 if (cache_validity & NFS_INO_INVALID_MODE) 5800 bitmask[1] |= FATTR4_WORD1_MODE; 5801 if (cache_validity & NFS_INO_INVALID_OTHER) 5802 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP; 5803 if (cache_validity & NFS_INO_INVALID_NLINK) 5804 bitmask[1] |= FATTR4_WORD1_NUMLINKS; 5805 if (cache_validity & NFS_INO_INVALID_CTIME) 5806 bitmask[1] |= FATTR4_WORD1_TIME_METADATA; 5807 if (cache_validity & NFS_INO_INVALID_MTIME) 5808 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY; 5809 if (cache_validity & NFS_INO_INVALID_BLOCKS) 5810 bitmask[1] |= FATTR4_WORD1_SPACE_USED; 5811 if (cache_validity & NFS_INO_INVALID_BTIME) 5812 bitmask[1] |= FATTR4_WORD1_TIME_CREATE; 5813 5814 if (cache_validity & NFS_INO_INVALID_SIZE) 5815 bitmask[0] |= FATTR4_WORD0_SIZE; 5816 5817 for (i = 0; i < NFS4_BITMASK_SZ; i++) 5818 bitmask[i] &= server->attr_bitmask[i]; 5819 } 5820 5821 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, 5822 struct rpc_message *msg, 5823 struct rpc_clnt **clnt) 5824 { 5825 struct nfs_server *server = NFS_SERVER(hdr->inode); 5826 5827 if (!nfs4_write_need_cache_consistency_data(hdr)) { 5828 hdr->args.bitmask = NULL; 5829 hdr->res.fattr = NULL; 5830 } else { 5831 nfs4_bitmask_set(hdr->args.bitmask_store, 5832 server->cache_consistency_bitmask, 5833 hdr->inode, NFS_INO_INVALID_BLOCKS); 5834 hdr->args.bitmask = hdr->args.bitmask_store; 5835 } 5836 5837 if (!hdr->pgio_done_cb) 5838 hdr->pgio_done_cb = nfs4_write_done_cb; 5839 hdr->res.server = server; 5840 hdr->timestamp = jiffies; 5841 5842 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; 5843 nfs4_init_sequence(server->nfs_client, &hdr->args.seq_args, 5844 &hdr->res.seq_res, 0, 0); 5845 nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr); 5846 } 5847 5848 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) 5849 { 5850 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client, 5851 &data->args.seq_args, 5852 &data->res.seq_res, 5853 task); 5854 } 5855 5856 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) 5857 { 5858 struct inode *inode = data->inode; 5859 5860 trace_nfs4_commit(data, task->tk_status); 5861 if (nfs4_async_handle_error(task, NFS_SERVER(inode), 5862 NULL, NULL) == -EAGAIN) { 5863 rpc_restart_call_prepare(task); 5864 return -EAGAIN; 5865 } 5866 return 0; 5867 } 5868 5869 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) 5870 { 5871 if (!nfs4_sequence_done(task, &data->res.seq_res)) 5872 return -EAGAIN; 5873 return data->commit_done_cb(task, data); 5874 } 5875 5876 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg, 5877 struct rpc_clnt **clnt) 5878 { 5879 struct nfs_server *server = NFS_SERVER(data->inode); 5880 5881 if (data->commit_done_cb == NULL) 5882 data->commit_done_cb = nfs4_commit_done_cb; 5883 data->res.server = server; 5884 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; 5885 nfs4_init_sequence(server->nfs_client, &data->args.seq_args, 5886 &data->res.seq_res, 1, 0); 5887 nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client, 5888 NFS_SP4_MACH_CRED_COMMIT, clnt, msg); 5889 } 5890 5891 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args, 5892 struct nfs_commitres *res) 5893 { 5894 struct inode *dst_inode = file_inode(dst); 5895 struct nfs_server *server = NFS_SERVER(dst_inode); 5896 struct rpc_message msg = { 5897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], 5898 .rpc_argp = args, 5899 .rpc_resp = res, 5900 }; 5901 5902 args->fh = NFS_FH(dst_inode); 5903 return nfs4_call_sync(server->client, server, &msg, 5904 &args->seq_args, &res->seq_res, 1); 5905 } 5906 5907 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res) 5908 { 5909 struct nfs_commitargs args = { 5910 .offset = offset, 5911 .count = count, 5912 }; 5913 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst)); 5914 struct nfs4_exception exception = { }; 5915 int status; 5916 5917 do { 5918 status = _nfs4_proc_commit(dst, &args, res); 5919 status = nfs4_handle_exception(dst_server, status, &exception); 5920 } while (exception.retry); 5921 5922 return status; 5923 } 5924 5925 static bool nfs4_server_supports_acls(const struct nfs_server *server, 5926 enum nfs4_acl_type type) 5927 { 5928 switch (type) { 5929 default: 5930 return server->attr_bitmask[0] & FATTR4_WORD0_ACL; 5931 case NFS4ACL_DACL: 5932 return server->attr_bitmask[1] & FATTR4_WORD1_DACL; 5933 case NFS4ACL_SACL: 5934 return server->attr_bitmask[1] & FATTR4_WORD1_SACL; 5935 } 5936 } 5937 5938 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that 5939 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on 5940 * the stack. 5941 */ 5942 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) 5943 5944 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen, 5945 struct page **pages) 5946 { 5947 struct page *newpage, **spages; 5948 int rc = 0; 5949 size_t len; 5950 spages = pages; 5951 5952 do { 5953 len = min_t(size_t, PAGE_SIZE, buflen); 5954 newpage = alloc_page(GFP_KERNEL); 5955 5956 if (newpage == NULL) 5957 goto unwind; 5958 memcpy(page_address(newpage), buf, len); 5959 buf += len; 5960 buflen -= len; 5961 *pages++ = newpage; 5962 rc++; 5963 } while (buflen != 0); 5964 5965 return rc; 5966 5967 unwind: 5968 for(; rc > 0; rc--) 5969 __free_page(spages[rc-1]); 5970 return -ENOMEM; 5971 } 5972 5973 struct nfs4_cached_acl { 5974 enum nfs4_acl_type type; 5975 int cached; 5976 size_t len; 5977 char data[]; 5978 }; 5979 5980 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) 5981 { 5982 struct nfs_inode *nfsi = NFS_I(inode); 5983 5984 spin_lock(&inode->i_lock); 5985 kfree(nfsi->nfs4_acl); 5986 nfsi->nfs4_acl = acl; 5987 spin_unlock(&inode->i_lock); 5988 } 5989 5990 static void nfs4_zap_acl_attr(struct inode *inode) 5991 { 5992 nfs4_set_cached_acl(inode, NULL); 5993 } 5994 5995 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, 5996 size_t buflen, enum nfs4_acl_type type) 5997 { 5998 struct nfs_inode *nfsi = NFS_I(inode); 5999 struct nfs4_cached_acl *acl; 6000 int ret = -ENOENT; 6001 6002 spin_lock(&inode->i_lock); 6003 acl = nfsi->nfs4_acl; 6004 if (acl == NULL) 6005 goto out; 6006 if (acl->type != type) 6007 goto out; 6008 if (buf == NULL) /* user is just asking for length */ 6009 goto out_len; 6010 if (acl->cached == 0) 6011 goto out; 6012 ret = -ERANGE; /* see getxattr(2) man page */ 6013 if (acl->len > buflen) 6014 goto out; 6015 memcpy(buf, acl->data, acl->len); 6016 out_len: 6017 ret = acl->len; 6018 out: 6019 spin_unlock(&inode->i_lock); 6020 return ret; 6021 } 6022 6023 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, 6024 size_t pgbase, size_t acl_len, 6025 enum nfs4_acl_type type) 6026 { 6027 struct nfs4_cached_acl *acl; 6028 size_t buflen = sizeof(*acl) + acl_len; 6029 6030 if (buflen <= PAGE_SIZE) { 6031 acl = kmalloc(buflen, GFP_KERNEL); 6032 if (acl == NULL) 6033 goto out; 6034 acl->cached = 1; 6035 _copy_from_pages(acl->data, pages, pgbase, acl_len); 6036 } else { 6037 acl = kmalloc_obj(*acl); 6038 if (acl == NULL) 6039 goto out; 6040 acl->cached = 0; 6041 } 6042 acl->type = type; 6043 acl->len = acl_len; 6044 out: 6045 nfs4_set_cached_acl(inode, acl); 6046 } 6047 6048 /* 6049 * The getxattr API returns the required buffer length when called with a 6050 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating 6051 * the required buf. On a NULL buf, we send a page of data to the server 6052 * guessing that the ACL request can be serviced by a page. If so, we cache 6053 * up to the page of ACL data, and the 2nd call to getxattr is serviced by 6054 * the cache. If not so, we throw away the page, and cache the required 6055 * length. The next getxattr call will then produce another round trip to 6056 * the server, this time with the input buf of the required size. 6057 */ 6058 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, 6059 size_t buflen, enum nfs4_acl_type type) 6060 { 6061 struct page **pages; 6062 struct nfs_getaclargs args = { 6063 .fh = NFS_FH(inode), 6064 .acl_type = type, 6065 .acl_len = buflen, 6066 }; 6067 struct nfs_getaclres res = { 6068 .acl_type = type, 6069 .acl_len = buflen, 6070 }; 6071 struct rpc_message msg = { 6072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], 6073 .rpc_argp = &args, 6074 .rpc_resp = &res, 6075 }; 6076 unsigned int npages; 6077 int ret = -ENOMEM, i; 6078 struct nfs_server *server = NFS_SERVER(inode); 6079 6080 if (buflen == 0) 6081 buflen = server->rsize; 6082 6083 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1; 6084 pages = kmalloc_objs(struct page *, npages); 6085 if (!pages) 6086 return -ENOMEM; 6087 6088 args.acl_pages = pages; 6089 6090 for (i = 0; i < npages; i++) { 6091 pages[i] = alloc_page(GFP_KERNEL); 6092 if (!pages[i]) 6093 goto out_free; 6094 } 6095 6096 /* for decoding across pages */ 6097 res.acl_scratch = folio_alloc(GFP_KERNEL, 0); 6098 if (!res.acl_scratch) 6099 goto out_free; 6100 6101 args.acl_len = npages * PAGE_SIZE; 6102 6103 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", 6104 __func__, buf, buflen, npages, args.acl_len); 6105 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), 6106 &msg, &args.seq_args, &res.seq_res, 0); 6107 if (ret) 6108 goto out_free; 6109 6110 /* Handle the case where the passed-in buffer is too short */ 6111 if (res.acl_flags & NFS4_ACL_TRUNC) { 6112 /* Did the user only issue a request for the acl length? */ 6113 if (buf == NULL) 6114 goto out_ok; 6115 ret = -ERANGE; 6116 goto out_free; 6117 } 6118 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len, 6119 type); 6120 if (buf) { 6121 if (res.acl_len > buflen) { 6122 ret = -ERANGE; 6123 goto out_free; 6124 } 6125 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); 6126 } 6127 out_ok: 6128 ret = res.acl_len; 6129 out_free: 6130 while (--i >= 0) 6131 __free_page(pages[i]); 6132 if (res.acl_scratch) 6133 folio_put(res.acl_scratch); 6134 kfree(pages); 6135 return ret; 6136 } 6137 6138 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, 6139 size_t buflen, enum nfs4_acl_type type) 6140 { 6141 struct nfs4_exception exception = { 6142 .interruptible = true, 6143 }; 6144 ssize_t ret; 6145 do { 6146 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type); 6147 trace_nfs4_get_acl(inode, ret); 6148 if (ret >= 0) 6149 break; 6150 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); 6151 } while (exception.retry); 6152 return ret; 6153 } 6154 6155 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen, 6156 enum nfs4_acl_type type) 6157 { 6158 struct nfs_server *server = NFS_SERVER(inode); 6159 int ret; 6160 6161 if (unlikely(NFS_FH(inode)->size == 0)) 6162 return -ENODATA; 6163 if (!nfs4_server_supports_acls(server, type)) 6164 return -EOPNOTSUPP; 6165 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE); 6166 if (ret < 0) 6167 return ret; 6168 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) 6169 nfs_zap_acl_cache(inode); 6170 ret = nfs4_read_cached_acl(inode, buf, buflen, type); 6171 if (ret != -ENOENT) 6172 /* -ENOENT is returned if there is no ACL or if there is an ACL 6173 * but no cached acl data, just the acl length */ 6174 return ret; 6175 return nfs4_get_acl_uncached(inode, buf, buflen, type); 6176 } 6177 6178 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, 6179 size_t buflen, enum nfs4_acl_type type) 6180 { 6181 struct nfs_server *server = NFS_SERVER(inode); 6182 struct page *pages[NFS4ACL_MAXPAGES]; 6183 struct nfs_setaclargs arg = { 6184 .fh = NFS_FH(inode), 6185 .acl_type = type, 6186 .acl_len = buflen, 6187 .acl_pages = pages, 6188 }; 6189 struct nfs_setaclres res; 6190 struct rpc_message msg = { 6191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], 6192 .rpc_argp = &arg, 6193 .rpc_resp = &res, 6194 }; 6195 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); 6196 int ret, i; 6197 6198 /* You can't remove system.nfs4_acl: */ 6199 if (buflen == 0) 6200 return -EINVAL; 6201 if (!nfs4_server_supports_acls(server, type)) 6202 return -EOPNOTSUPP; 6203 if (npages > ARRAY_SIZE(pages)) 6204 return -ERANGE; 6205 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages); 6206 if (i < 0) 6207 return i; 6208 nfs4_inode_make_writeable(inode); 6209 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 6210 6211 /* 6212 * Free each page after tx, so the only ref left is 6213 * held by the network stack 6214 */ 6215 for (; i > 0; i--) 6216 put_page(pages[i-1]); 6217 6218 /* 6219 * Acl update can result in inode attribute update. 6220 * so mark the attribute cache invalid. 6221 */ 6222 spin_lock(&inode->i_lock); 6223 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | 6224 NFS_INO_INVALID_CTIME | 6225 NFS_INO_REVAL_FORCED); 6226 spin_unlock(&inode->i_lock); 6227 nfs_access_zap_cache(inode); 6228 nfs_zap_acl_cache(inode); 6229 return ret; 6230 } 6231 6232 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, 6233 size_t buflen, enum nfs4_acl_type type) 6234 { 6235 struct nfs4_exception exception = { }; 6236 int err; 6237 6238 if (unlikely(NFS_FH(inode)->size == 0)) 6239 return -ENODATA; 6240 do { 6241 err = __nfs4_proc_set_acl(inode, buf, buflen, type); 6242 trace_nfs4_set_acl(inode, err); 6243 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) { 6244 /* 6245 * no need to retry since the kernel 6246 * isn't involved in encoding the ACEs. 6247 */ 6248 err = -EINVAL; 6249 break; 6250 } 6251 err = nfs4_handle_exception(NFS_SERVER(inode), err, 6252 &exception); 6253 } while (exception.retry); 6254 return err; 6255 } 6256 6257 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 6258 static int _nfs4_get_security_label(struct inode *inode, void *buf, 6259 size_t buflen) 6260 { 6261 struct nfs_server *server = NFS_SERVER(inode); 6262 struct nfs4_label label = {0, 0, 0, buflen, buf}; 6263 6264 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 6265 struct nfs_fattr fattr = { 6266 .label = &label, 6267 }; 6268 struct nfs4_getattr_arg arg = { 6269 .fh = NFS_FH(inode), 6270 .bitmask = bitmask, 6271 }; 6272 struct nfs4_getattr_res res = { 6273 .fattr = &fattr, 6274 .server = server, 6275 }; 6276 struct rpc_message msg = { 6277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 6278 .rpc_argp = &arg, 6279 .rpc_resp = &res, 6280 }; 6281 int ret; 6282 6283 nfs_fattr_init(&fattr); 6284 6285 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); 6286 if (ret) 6287 return ret; 6288 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) 6289 return -ENOENT; 6290 return label.len; 6291 } 6292 6293 static int nfs4_get_security_label(struct inode *inode, void *buf, 6294 size_t buflen) 6295 { 6296 struct nfs4_exception exception = { 6297 .interruptible = true, 6298 }; 6299 int err; 6300 6301 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 6302 return -EOPNOTSUPP; 6303 6304 do { 6305 err = _nfs4_get_security_label(inode, buf, buflen); 6306 trace_nfs4_get_security_label(inode, err); 6307 err = nfs4_handle_exception(NFS_SERVER(inode), err, 6308 &exception); 6309 } while (exception.retry); 6310 return err; 6311 } 6312 6313 static int _nfs4_do_set_security_label(struct inode *inode, 6314 struct nfs4_label *ilabel, 6315 struct nfs_fattr *fattr) 6316 { 6317 6318 struct iattr sattr = {0}; 6319 struct nfs_server *server = NFS_SERVER(inode); 6320 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 6321 struct nfs_setattrargs arg = { 6322 .fh = NFS_FH(inode), 6323 .iap = &sattr, 6324 .server = server, 6325 .bitmask = bitmask, 6326 .label = ilabel, 6327 }; 6328 struct nfs_setattrres res = { 6329 .fattr = fattr, 6330 .server = server, 6331 }; 6332 struct rpc_message msg = { 6333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 6334 .rpc_argp = &arg, 6335 .rpc_resp = &res, 6336 }; 6337 int status; 6338 6339 nfs4_stateid_copy(&arg.stateid, &zero_stateid); 6340 6341 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 6342 if (status) 6343 dprintk("%s failed: %d\n", __func__, status); 6344 6345 return status; 6346 } 6347 6348 static int nfs4_do_set_security_label(struct inode *inode, 6349 struct nfs4_label *ilabel, 6350 struct nfs_fattr *fattr) 6351 { 6352 struct nfs4_exception exception = { }; 6353 int err; 6354 6355 do { 6356 err = _nfs4_do_set_security_label(inode, ilabel, fattr); 6357 trace_nfs4_set_security_label(inode, err); 6358 err = nfs4_handle_exception(NFS_SERVER(inode), err, 6359 &exception); 6360 } while (exception.retry); 6361 return err; 6362 } 6363 6364 static int 6365 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen) 6366 { 6367 struct nfs4_label ilabel = {0, 0, 0, buflen, (char *)buf }; 6368 struct nfs_fattr *fattr; 6369 int status; 6370 6371 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 6372 return -EOPNOTSUPP; 6373 6374 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 6375 if (fattr == NULL) 6376 return -ENOMEM; 6377 6378 status = nfs4_do_set_security_label(inode, &ilabel, fattr); 6379 if (status == 0) 6380 nfs_setsecurity(inode, fattr); 6381 6382 nfs_free_fattr(fattr); 6383 return status; 6384 } 6385 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ 6386 6387 6388 static void nfs4_init_boot_verifier(const struct nfs_client *clp, 6389 nfs4_verifier *bootverf) 6390 { 6391 __be32 verf[2]; 6392 6393 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { 6394 /* An impossible timestamp guarantees this value 6395 * will never match a generated boot time. */ 6396 verf[0] = cpu_to_be32(U32_MAX); 6397 verf[1] = cpu_to_be32(U32_MAX); 6398 } else { 6399 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 6400 u64 ns = ktime_to_ns(nn->boot_time); 6401 6402 verf[0] = cpu_to_be32(ns >> 32); 6403 verf[1] = cpu_to_be32(ns); 6404 } 6405 memcpy(bootverf->data, verf, sizeof(bootverf->data)); 6406 } 6407 6408 static size_t 6409 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen) 6410 { 6411 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 6412 struct nfs_netns_client *nn_clp = nn->nfs_client; 6413 const char *id; 6414 6415 buf[0] = '\0'; 6416 6417 if (nn_clp) { 6418 rcu_read_lock(); 6419 id = rcu_dereference(nn_clp->identifier); 6420 if (id) 6421 strscpy(buf, id, buflen); 6422 rcu_read_unlock(); 6423 } 6424 6425 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0') 6426 strscpy(buf, nfs4_client_id_uniquifier, buflen); 6427 6428 return strlen(buf); 6429 } 6430 6431 static int 6432 nfs4_init_nonuniform_client_string(struct nfs_client *clp) 6433 { 6434 char buf[NFS4_CLIENT_ID_UNIQ_LEN]; 6435 size_t buflen; 6436 size_t len; 6437 char *str; 6438 6439 if (clp->cl_owner_id != NULL) 6440 return 0; 6441 6442 rcu_read_lock(); 6443 len = 14 + 6444 strlen(clp->cl_rpcclient->cl_nodename) + 6445 1 + 6446 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) + 6447 1; 6448 rcu_read_unlock(); 6449 6450 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf)); 6451 if (buflen) 6452 len += buflen + 1; 6453 6454 if (len > NFS4_OPAQUE_LIMIT + 1) 6455 return -EINVAL; 6456 6457 /* 6458 * Since this string is allocated at mount time, and held until the 6459 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 6460 * about a memory-reclaim deadlock. 6461 */ 6462 str = kmalloc(len, GFP_KERNEL); 6463 if (!str) 6464 return -ENOMEM; 6465 6466 rcu_read_lock(); 6467 if (buflen) 6468 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s", 6469 clp->cl_rpcclient->cl_nodename, buf, 6470 rpc_peeraddr2str(clp->cl_rpcclient, 6471 RPC_DISPLAY_ADDR)); 6472 else 6473 scnprintf(str, len, "Linux NFSv4.0 %s/%s", 6474 clp->cl_rpcclient->cl_nodename, 6475 rpc_peeraddr2str(clp->cl_rpcclient, 6476 RPC_DISPLAY_ADDR)); 6477 rcu_read_unlock(); 6478 6479 clp->cl_owner_id = str; 6480 return 0; 6481 } 6482 6483 static int 6484 nfs4_init_uniform_client_string(struct nfs_client *clp) 6485 { 6486 char buf[NFS4_CLIENT_ID_UNIQ_LEN]; 6487 size_t buflen; 6488 size_t len; 6489 char *str; 6490 6491 if (clp->cl_owner_id != NULL) 6492 return 0; 6493 6494 len = 10 + 10 + 1 + 10 + 1 + 6495 strlen(clp->cl_rpcclient->cl_nodename) + 1; 6496 6497 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf)); 6498 if (buflen) 6499 len += buflen + 1; 6500 6501 if (len > NFS4_OPAQUE_LIMIT + 1) 6502 return -EINVAL; 6503 6504 /* 6505 * Since this string is allocated at mount time, and held until the 6506 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 6507 * about a memory-reclaim deadlock. 6508 */ 6509 str = kmalloc(len, GFP_KERNEL); 6510 if (!str) 6511 return -ENOMEM; 6512 6513 if (buflen) 6514 scnprintf(str, len, "Linux NFSv%u.%u %s/%s", 6515 clp->rpc_ops->version, clp->cl_minorversion, 6516 buf, clp->cl_rpcclient->cl_nodename); 6517 else 6518 scnprintf(str, len, "Linux NFSv%u.%u %s", 6519 clp->rpc_ops->version, clp->cl_minorversion, 6520 clp->cl_rpcclient->cl_nodename); 6521 clp->cl_owner_id = str; 6522 return 0; 6523 } 6524 6525 /* 6526 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback 6527 * services. Advertise one based on the address family of the 6528 * clientaddr. 6529 */ 6530 static unsigned int 6531 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) 6532 { 6533 if (strchr(clp->cl_ipaddr, ':') != NULL) 6534 return scnprintf(buf, len, "tcp6"); 6535 else 6536 return scnprintf(buf, len, "tcp"); 6537 } 6538 6539 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) 6540 { 6541 struct nfs4_setclientid *sc = calldata; 6542 6543 if (task->tk_status == 0) 6544 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); 6545 } 6546 6547 static const struct rpc_call_ops nfs4_setclientid_ops = { 6548 .rpc_call_done = nfs4_setclientid_done, 6549 }; 6550 6551 /** 6552 * nfs4_proc_setclientid - Negotiate client ID 6553 * @clp: state data structure 6554 * @program: RPC program for NFSv4 callback service 6555 * @port: IP port number for NFS4 callback service 6556 * @cred: credential to use for this call 6557 * @res: where to place the result 6558 * 6559 * Returns zero, a negative errno, or a negative NFS4ERR status code. 6560 */ 6561 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, 6562 unsigned short port, const struct cred *cred, 6563 struct nfs4_setclientid_res *res) 6564 { 6565 nfs4_verifier sc_verifier; 6566 struct nfs4_setclientid setclientid = { 6567 .sc_verifier = &sc_verifier, 6568 .sc_prog = program, 6569 .sc_clnt = clp, 6570 }; 6571 struct rpc_message msg = { 6572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], 6573 .rpc_argp = &setclientid, 6574 .rpc_resp = res, 6575 .rpc_cred = cred, 6576 }; 6577 struct rpc_task_setup task_setup_data = { 6578 .rpc_client = clp->cl_rpcclient, 6579 .rpc_message = &msg, 6580 .callback_ops = &nfs4_setclientid_ops, 6581 .callback_data = &setclientid, 6582 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, 6583 }; 6584 unsigned long now = jiffies; 6585 int status; 6586 6587 /* nfs_client_id4 */ 6588 nfs4_init_boot_verifier(clp, &sc_verifier); 6589 6590 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) 6591 status = nfs4_init_uniform_client_string(clp); 6592 else 6593 status = nfs4_init_nonuniform_client_string(clp); 6594 6595 if (status) 6596 goto out; 6597 6598 /* cb_client4 */ 6599 setclientid.sc_netid_len = 6600 nfs4_init_callback_netid(clp, 6601 setclientid.sc_netid, 6602 sizeof(setclientid.sc_netid)); 6603 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, 6604 sizeof(setclientid.sc_uaddr), "%s.%u.%u", 6605 clp->cl_ipaddr, port >> 8, port & 255); 6606 6607 dprintk("NFS call setclientid auth=%s, '%s'\n", 6608 clp->cl_rpcclient->cl_auth->au_ops->au_name, 6609 clp->cl_owner_id); 6610 6611 status = nfs4_call_sync_custom(&task_setup_data); 6612 if (setclientid.sc_cred) { 6613 kfree(clp->cl_acceptor); 6614 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); 6615 put_rpccred(setclientid.sc_cred); 6616 } 6617 6618 if (status == 0) 6619 do_renew_lease(clp, now); 6620 out: 6621 trace_nfs4_setclientid(clp, status); 6622 dprintk("NFS reply setclientid: %d\n", status); 6623 return status; 6624 } 6625 6626 /** 6627 * nfs4_proc_setclientid_confirm - Confirm client ID 6628 * @clp: state data structure 6629 * @arg: result of a previous SETCLIENTID 6630 * @cred: credential to use for this call 6631 * 6632 * Returns zero, a negative errno, or a negative NFS4ERR status code. 6633 */ 6634 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, 6635 struct nfs4_setclientid_res *arg, 6636 const struct cred *cred) 6637 { 6638 struct rpc_message msg = { 6639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], 6640 .rpc_argp = arg, 6641 .rpc_cred = cred, 6642 }; 6643 int status; 6644 6645 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", 6646 clp->cl_rpcclient->cl_auth->au_ops->au_name, 6647 clp->cl_clientid); 6648 status = rpc_call_sync(clp->cl_rpcclient, &msg, 6649 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 6650 trace_nfs4_setclientid_confirm(clp, status); 6651 dprintk("NFS reply setclientid_confirm: %d\n", status); 6652 return status; 6653 } 6654 6655 struct nfs4_delegreturndata { 6656 struct nfs4_delegreturnargs args; 6657 struct nfs4_delegreturnres res; 6658 struct nfs_fh fh; 6659 nfs4_stateid stateid; 6660 unsigned long timestamp; 6661 unsigned short retrans; 6662 struct { 6663 struct nfs4_layoutreturn_args arg; 6664 struct nfs4_layoutreturn_res res; 6665 struct nfs4_xdr_opaque_data ld_private; 6666 u32 roc_barrier; 6667 bool roc; 6668 } lr; 6669 struct nfs4_delegattr sattr; 6670 struct nfs_fattr fattr; 6671 int rpc_status; 6672 struct inode *inode; 6673 }; 6674 6675 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) 6676 { 6677 struct nfs4_delegreturndata *data = calldata; 6678 struct nfs4_exception exception = { 6679 .inode = data->inode, 6680 .stateid = &data->stateid, 6681 .task_is_privileged = data->args.seq_args.sa_privileged, 6682 .retrans = data->retrans, 6683 }; 6684 6685 if (!nfs4_sequence_done(task, &data->res.seq_res)) 6686 return; 6687 6688 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); 6689 6690 /* Handle Layoutreturn errors */ 6691 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res, 6692 &data->res.lr_ret) == -EAGAIN) 6693 goto out_restart; 6694 6695 if (data->args.sattr_args && task->tk_status != 0) { 6696 switch(data->res.sattr_ret) { 6697 case 0: 6698 data->args.sattr_args = NULL; 6699 data->res.sattr_res = false; 6700 break; 6701 case -NFS4ERR_ADMIN_REVOKED: 6702 case -NFS4ERR_DELEG_REVOKED: 6703 case -NFS4ERR_EXPIRED: 6704 case -NFS4ERR_BAD_STATEID: 6705 /* Let the main handler below do stateid recovery */ 6706 break; 6707 case -NFS4ERR_OLD_STATEID: 6708 if (nfs4_refresh_delegation_stateid(&data->stateid, 6709 data->inode)) 6710 goto out_restart; 6711 fallthrough; 6712 default: 6713 data->args.sattr_args = NULL; 6714 data->res.sattr_res = false; 6715 goto out_restart; 6716 } 6717 } 6718 6719 switch (task->tk_status) { 6720 case 0: 6721 renew_lease(data->res.server, data->timestamp); 6722 break; 6723 case -NFS4ERR_ADMIN_REVOKED: 6724 case -NFS4ERR_DELEG_REVOKED: 6725 case -NFS4ERR_EXPIRED: 6726 nfs4_free_revoked_stateid(data->res.server, 6727 data->args.stateid, 6728 task->tk_msg.rpc_cred); 6729 fallthrough; 6730 case -NFS4ERR_BAD_STATEID: 6731 case -NFS4ERR_STALE_STATEID: 6732 case -ETIMEDOUT: 6733 task->tk_status = 0; 6734 break; 6735 case -NFS4ERR_OLD_STATEID: 6736 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode)) 6737 nfs4_stateid_seqid_inc(&data->stateid); 6738 if (data->args.bitmask) { 6739 data->args.bitmask = NULL; 6740 data->res.fattr = NULL; 6741 } 6742 goto out_restart; 6743 case -NFS4ERR_ACCESS: 6744 if (data->args.bitmask) { 6745 data->args.bitmask = NULL; 6746 data->res.fattr = NULL; 6747 goto out_restart; 6748 } 6749 fallthrough; 6750 default: 6751 task->tk_status = nfs4_async_handle_exception(task, 6752 data->res.server, task->tk_status, 6753 &exception); 6754 data->retrans = exception.retrans; 6755 if (exception.retry) 6756 goto out_restart; 6757 } 6758 nfs_delegation_mark_returned(data->inode, data->args.stateid); 6759 data->rpc_status = task->tk_status; 6760 return; 6761 out_restart: 6762 task->tk_status = 0; 6763 rpc_restart_call_prepare(task); 6764 } 6765 6766 static void nfs4_delegreturn_release(void *calldata) 6767 { 6768 struct nfs4_delegreturndata *data = calldata; 6769 struct inode *inode = data->inode; 6770 6771 if (data->lr.roc) 6772 pnfs_roc_release(&data->lr.arg, &data->lr.res, 6773 data->res.lr_ret); 6774 if (inode) { 6775 nfs4_fattr_set_prechange(&data->fattr, 6776 inode_peek_iversion_raw(inode)); 6777 nfs_refresh_inode(inode, &data->fattr); 6778 nfs_iput_and_deactive(inode); 6779 } 6780 kfree(calldata); 6781 } 6782 6783 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) 6784 { 6785 struct nfs4_delegreturndata *d_data; 6786 struct pnfs_layout_hdr *lo; 6787 6788 d_data = data; 6789 6790 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) { 6791 nfs4_sequence_done(task, &d_data->res.seq_res); 6792 return; 6793 } 6794 6795 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL; 6796 if (lo && !pnfs_layout_is_valid(lo)) { 6797 d_data->args.lr_args = NULL; 6798 d_data->res.lr_res = NULL; 6799 } 6800 6801 nfs4_setup_sequence(d_data->res.server->nfs_client, 6802 &d_data->args.seq_args, 6803 &d_data->res.seq_res, 6804 task); 6805 } 6806 6807 static const struct rpc_call_ops nfs4_delegreturn_ops = { 6808 .rpc_call_prepare = nfs4_delegreturn_prepare, 6809 .rpc_call_done = nfs4_delegreturn_done, 6810 .rpc_release = nfs4_delegreturn_release, 6811 }; 6812 6813 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, 6814 const nfs4_stateid *stateid, 6815 struct nfs_delegation *delegation, 6816 int issync) 6817 { 6818 struct nfs4_delegreturndata *data; 6819 struct nfs_server *server = NFS_SERVER(inode); 6820 struct rpc_task *task; 6821 struct rpc_message msg = { 6822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], 6823 .rpc_cred = cred, 6824 }; 6825 struct rpc_task_setup task_setup_data = { 6826 .rpc_client = server->client, 6827 .rpc_message = &msg, 6828 .callback_ops = &nfs4_delegreturn_ops, 6829 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, 6830 }; 6831 int status = 0; 6832 6833 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE)) 6834 task_setup_data.flags |= RPC_TASK_MOVEABLE; 6835 6836 data = kzalloc_obj(*data); 6837 if (data == NULL) 6838 return -ENOMEM; 6839 6840 nfs4_state_protect(server->nfs_client, 6841 NFS_SP4_MACH_CRED_CLEANUP, 6842 &task_setup_data.rpc_client, &msg); 6843 6844 data->args.fhandle = &data->fh; 6845 data->args.stateid = &data->stateid; 6846 nfs4_bitmask_set(data->args.bitmask_store, 6847 server->cache_consistency_bitmask, inode, 0); 6848 data->args.bitmask = data->args.bitmask_store; 6849 nfs_copy_fh(&data->fh, NFS_FH(inode)); 6850 nfs4_stateid_copy(&data->stateid, stateid); 6851 data->res.fattr = &data->fattr; 6852 data->res.server = server; 6853 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; 6854 data->lr.arg.ld_private = &data->lr.ld_private; 6855 nfs_fattr_init(data->res.fattr); 6856 data->timestamp = jiffies; 6857 data->rpc_status = 0; 6858 data->inode = nfs_igrab_and_active(inode); 6859 if (data->inode || issync) { 6860 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, 6861 cred, issync); 6862 if (data->lr.roc) { 6863 data->args.lr_args = &data->lr.arg; 6864 data->res.lr_res = &data->lr.res; 6865 } 6866 } 6867 6868 if (delegation && 6869 test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags)) { 6870 if (delegation->type & FMODE_READ) { 6871 data->sattr.atime = inode_get_atime(inode); 6872 data->sattr.atime_set = true; 6873 } 6874 if (delegation->type & FMODE_WRITE) { 6875 data->sattr.mtime = inode_get_mtime(inode); 6876 data->sattr.mtime_set = true; 6877 } 6878 data->args.sattr_args = &data->sattr; 6879 data->res.sattr_res = true; 6880 } 6881 6882 nfs4_init_sequence(server->nfs_client, &data->args.seq_args, 6883 &data->res.seq_res, 1, !data->inode ? 1 : 0); 6884 6885 task_setup_data.callback_data = data; 6886 msg.rpc_argp = &data->args; 6887 msg.rpc_resp = &data->res; 6888 task = rpc_run_task(&task_setup_data); 6889 if (IS_ERR(task)) 6890 return PTR_ERR(task); 6891 if (!issync) 6892 goto out; 6893 status = rpc_wait_for_completion_task(task); 6894 if (status != 0) 6895 goto out; 6896 status = data->rpc_status; 6897 out: 6898 rpc_put_task(task); 6899 return status; 6900 } 6901 6902 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, 6903 const nfs4_stateid *stateid, 6904 struct nfs_delegation *delegation, int issync) 6905 { 6906 struct nfs_server *server = NFS_SERVER(inode); 6907 struct nfs4_exception exception = { }; 6908 int err; 6909 do { 6910 err = _nfs4_proc_delegreturn(inode, cred, stateid, 6911 delegation, issync); 6912 trace_nfs4_delegreturn(inode, stateid, err); 6913 switch (err) { 6914 case -NFS4ERR_STALE_STATEID: 6915 case -NFS4ERR_EXPIRED: 6916 case 0: 6917 return 0; 6918 } 6919 err = nfs4_handle_exception(server, err, &exception); 6920 } while (exception.retry); 6921 return err; 6922 } 6923 6924 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6925 { 6926 struct inode *inode = state->inode; 6927 struct nfs_server *server = NFS_SERVER(inode); 6928 struct nfs_client *clp = server->nfs_client; 6929 struct nfs_lockt_args arg = { 6930 .fh = NFS_FH(inode), 6931 .fl = request, 6932 }; 6933 struct nfs_lockt_res res = { 6934 .denied = request, 6935 }; 6936 struct rpc_message msg = { 6937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], 6938 .rpc_argp = &arg, 6939 .rpc_resp = &res, 6940 .rpc_cred = state->owner->so_cred, 6941 }; 6942 struct nfs4_lock_state *lsp; 6943 int status; 6944 6945 arg.lock_owner.clientid = clp->cl_clientid; 6946 status = nfs4_set_lock_state(state, request); 6947 if (status != 0) 6948 goto out; 6949 lsp = request->fl_u.nfs4_fl.owner; 6950 arg.lock_owner.id = lsp->ls_seqid.owner_id; 6951 arg.lock_owner.s_dev = server->s_dev; 6952 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 6953 switch (status) { 6954 case 0: 6955 request->c.flc_type = F_UNLCK; 6956 break; 6957 case -NFS4ERR_DENIED: 6958 status = 0; 6959 } 6960 request->fl_ops->fl_release_private(request); 6961 request->fl_ops = NULL; 6962 out: 6963 return status; 6964 } 6965 6966 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6967 { 6968 struct nfs4_exception exception = { 6969 .interruptible = true, 6970 }; 6971 int err; 6972 6973 do { 6974 err = _nfs4_proc_getlk(state, cmd, request); 6975 trace_nfs4_get_lock(request, state, cmd, err); 6976 err = nfs4_handle_exception(NFS_SERVER(state->inode), err, 6977 &exception); 6978 } while (exception.retry); 6979 return err; 6980 } 6981 6982 /* 6983 * Update the seqid of a lock stateid after receiving 6984 * NFS4ERR_OLD_STATEID 6985 */ 6986 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst, 6987 struct nfs4_lock_state *lsp) 6988 { 6989 struct nfs4_state *state = lsp->ls_state; 6990 bool ret = false; 6991 6992 spin_lock(&state->state_lock); 6993 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid)) 6994 goto out; 6995 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst)) 6996 nfs4_stateid_seqid_inc(dst); 6997 else 6998 dst->seqid = lsp->ls_stateid.seqid; 6999 ret = true; 7000 out: 7001 spin_unlock(&state->state_lock); 7002 return ret; 7003 } 7004 7005 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst, 7006 struct nfs4_lock_state *lsp) 7007 { 7008 struct nfs4_state *state = lsp->ls_state; 7009 bool ret; 7010 7011 spin_lock(&state->state_lock); 7012 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid); 7013 nfs4_stateid_copy(dst, &lsp->ls_stateid); 7014 spin_unlock(&state->state_lock); 7015 return ret; 7016 } 7017 7018 struct nfs4_unlockdata { 7019 struct nfs_locku_args arg; 7020 struct nfs_locku_res res; 7021 struct nfs4_lock_state *lsp; 7022 struct nfs_open_context *ctx; 7023 struct nfs_lock_context *l_ctx; 7024 struct file_lock fl; 7025 struct nfs_server *server; 7026 unsigned long timestamp; 7027 unsigned short retrans; 7028 }; 7029 7030 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, 7031 struct nfs_open_context *ctx, 7032 struct nfs4_lock_state *lsp, 7033 struct nfs_seqid *seqid) 7034 { 7035 struct nfs4_unlockdata *p; 7036 struct nfs4_state *state = lsp->ls_state; 7037 struct inode *inode = state->inode; 7038 struct nfs_lock_context *l_ctx; 7039 7040 p = kzalloc_obj(*p); 7041 if (p == NULL) 7042 return NULL; 7043 l_ctx = nfs_get_lock_context(ctx); 7044 if (!IS_ERR(l_ctx)) { 7045 p->l_ctx = l_ctx; 7046 } else { 7047 kfree(p); 7048 return NULL; 7049 } 7050 p->arg.fh = NFS_FH(inode); 7051 p->arg.fl = &p->fl; 7052 p->arg.seqid = seqid; 7053 p->res.seqid = seqid; 7054 p->lsp = lsp; 7055 /* Ensure we don't close file until we're done freeing locks! */ 7056 p->ctx = get_nfs_open_context(ctx); 7057 locks_init_lock(&p->fl); 7058 locks_copy_lock(&p->fl, fl); 7059 p->server = NFS_SERVER(inode); 7060 spin_lock(&state->state_lock); 7061 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid); 7062 spin_unlock(&state->state_lock); 7063 return p; 7064 } 7065 7066 static void nfs4_locku_release_calldata(void *data) 7067 { 7068 struct nfs4_unlockdata *calldata = data; 7069 nfs_free_seqid(calldata->arg.seqid); 7070 nfs4_put_lock_state(calldata->lsp); 7071 nfs_put_lock_context(calldata->l_ctx); 7072 put_nfs_open_context(calldata->ctx); 7073 kfree(calldata); 7074 } 7075 7076 static void nfs4_locku_done(struct rpc_task *task, void *data) 7077 { 7078 struct nfs4_unlockdata *calldata = data; 7079 struct nfs4_exception exception = { 7080 .inode = calldata->lsp->ls_state->inode, 7081 .stateid = &calldata->arg.stateid, 7082 .retrans = calldata->retrans, 7083 }; 7084 7085 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 7086 return; 7087 switch (task->tk_status) { 7088 case 0: 7089 renew_lease(calldata->server, calldata->timestamp); 7090 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl); 7091 if (nfs4_update_lock_stateid(calldata->lsp, 7092 &calldata->res.stateid)) 7093 break; 7094 fallthrough; 7095 case -NFS4ERR_ADMIN_REVOKED: 7096 case -NFS4ERR_EXPIRED: 7097 nfs4_free_revoked_stateid(calldata->server, 7098 &calldata->arg.stateid, 7099 task->tk_msg.rpc_cred); 7100 fallthrough; 7101 case -NFS4ERR_BAD_STATEID: 7102 case -NFS4ERR_STALE_STATEID: 7103 if (nfs4_sync_lock_stateid(&calldata->arg.stateid, 7104 calldata->lsp)) 7105 rpc_restart_call_prepare(task); 7106 break; 7107 case -NFS4ERR_OLD_STATEID: 7108 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid, 7109 calldata->lsp)) 7110 rpc_restart_call_prepare(task); 7111 break; 7112 default: 7113 task->tk_status = nfs4_async_handle_exception(task, 7114 calldata->server, task->tk_status, 7115 &exception); 7116 calldata->retrans = exception.retrans; 7117 if (exception.retry) 7118 rpc_restart_call_prepare(task); 7119 } 7120 nfs_release_seqid(calldata->arg.seqid); 7121 } 7122 7123 static void nfs4_locku_prepare(struct rpc_task *task, void *data) 7124 { 7125 struct nfs4_unlockdata *calldata = data; 7126 7127 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) && 7128 nfs_async_iocounter_wait(task, calldata->l_ctx)) 7129 return; 7130 7131 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 7132 goto out_wait; 7133 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { 7134 /* Note: exit _without_ running nfs4_locku_done */ 7135 goto out_no_action; 7136 } 7137 calldata->timestamp = jiffies; 7138 if (nfs4_setup_sequence(calldata->server->nfs_client, 7139 &calldata->arg.seq_args, 7140 &calldata->res.seq_res, 7141 task) != 0) 7142 nfs_release_seqid(calldata->arg.seqid); 7143 return; 7144 out_no_action: 7145 task->tk_action = NULL; 7146 out_wait: 7147 nfs4_sequence_done(task, &calldata->res.seq_res); 7148 } 7149 7150 static const struct rpc_call_ops nfs4_locku_ops = { 7151 .rpc_call_prepare = nfs4_locku_prepare, 7152 .rpc_call_done = nfs4_locku_done, 7153 .rpc_release = nfs4_locku_release_calldata, 7154 }; 7155 7156 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, 7157 struct nfs_open_context *ctx, 7158 struct nfs4_lock_state *lsp, 7159 struct nfs_seqid *seqid) 7160 { 7161 struct nfs4_unlockdata *data; 7162 struct nfs_client *clp = NFS_SERVER(lsp->ls_state->inode)->nfs_client; 7163 struct rpc_message msg = { 7164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], 7165 .rpc_cred = ctx->cred, 7166 }; 7167 struct rpc_task_setup task_setup_data = { 7168 .rpc_client = NFS_CLIENT(lsp->ls_state->inode), 7169 .rpc_message = &msg, 7170 .callback_ops = &nfs4_locku_ops, 7171 .workqueue = nfsiod_workqueue, 7172 .flags = RPC_TASK_ASYNC, 7173 }; 7174 7175 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE)) 7176 task_setup_data.flags |= RPC_TASK_MOVEABLE; 7177 7178 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_CLEANUP, 7179 &task_setup_data.rpc_client, &msg); 7180 7181 /* Ensure this is an unlock - when canceling a lock, the 7182 * canceled lock is passed in, and it won't be an unlock. 7183 */ 7184 fl->c.flc_type = F_UNLCK; 7185 if (fl->c.flc_flags & FL_CLOSE) 7186 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags); 7187 7188 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); 7189 if (data == NULL) { 7190 nfs_free_seqid(seqid); 7191 return ERR_PTR(-ENOMEM); 7192 } 7193 7194 nfs4_init_sequence(clp, &data->arg.seq_args, &data->res.seq_res, 1, 0); 7195 msg.rpc_argp = &data->arg; 7196 msg.rpc_resp = &data->res; 7197 task_setup_data.callback_data = data; 7198 return rpc_run_task(&task_setup_data); 7199 } 7200 7201 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) 7202 { 7203 struct inode *inode = state->inode; 7204 struct nfs4_state_owner *sp = state->owner; 7205 struct nfs_inode *nfsi = NFS_I(inode); 7206 struct nfs_seqid *seqid; 7207 struct nfs4_lock_state *lsp; 7208 struct rpc_task *task; 7209 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 7210 int status = 0; 7211 unsigned char saved_flags = request->c.flc_flags; 7212 7213 status = nfs4_set_lock_state(state, request); 7214 /* Unlock _before_ we do the RPC call */ 7215 request->c.flc_flags |= FL_EXISTS; 7216 /* Exclude nfs_delegation_claim_locks() */ 7217 mutex_lock(&sp->so_delegreturn_mutex); 7218 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ 7219 down_read(&nfsi->rwsem); 7220 if (locks_lock_inode_wait(inode, request) == -ENOENT) { 7221 up_read(&nfsi->rwsem); 7222 mutex_unlock(&sp->so_delegreturn_mutex); 7223 goto out; 7224 } 7225 lsp = request->fl_u.nfs4_fl.owner; 7226 set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags); 7227 up_read(&nfsi->rwsem); 7228 mutex_unlock(&sp->so_delegreturn_mutex); 7229 if (status != 0) 7230 goto out; 7231 /* Is this a delegated lock? */ 7232 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) 7233 goto out; 7234 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid; 7235 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); 7236 status = -ENOMEM; 7237 if (IS_ERR(seqid)) 7238 goto out; 7239 task = nfs4_do_unlck(request, 7240 nfs_file_open_context(request->c.flc_file), 7241 lsp, seqid); 7242 status = PTR_ERR(task); 7243 if (IS_ERR(task)) 7244 goto out; 7245 status = rpc_wait_for_completion_task(task); 7246 rpc_put_task(task); 7247 out: 7248 request->c.flc_flags = saved_flags; 7249 trace_nfs4_unlock(request, state, F_SETLK, status); 7250 return status; 7251 } 7252 7253 struct nfs4_lockdata { 7254 struct nfs_lock_args arg; 7255 struct nfs_lock_res res; 7256 struct nfs4_lock_state *lsp; 7257 struct nfs_open_context *ctx; 7258 struct file_lock fl; 7259 unsigned long timestamp; 7260 int rpc_status; 7261 int cancelled; 7262 struct nfs_server *server; 7263 }; 7264 7265 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, 7266 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, 7267 gfp_t gfp_mask) 7268 { 7269 struct nfs4_lockdata *p; 7270 struct inode *inode = lsp->ls_state->inode; 7271 struct nfs_server *server = NFS_SERVER(inode); 7272 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 7273 7274 p = kzalloc_obj(*p, gfp_mask); 7275 if (p == NULL) 7276 return NULL; 7277 7278 p->arg.fh = NFS_FH(inode); 7279 p->arg.fl = &p->fl; 7280 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); 7281 if (IS_ERR(p->arg.open_seqid)) 7282 goto out_free; 7283 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 7284 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask); 7285 if (IS_ERR(p->arg.lock_seqid)) 7286 goto out_free_seqid; 7287 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; 7288 p->arg.lock_owner.id = lsp->ls_seqid.owner_id; 7289 p->arg.lock_owner.s_dev = server->s_dev; 7290 p->res.lock_seqid = p->arg.lock_seqid; 7291 p->lsp = lsp; 7292 p->server = server; 7293 p->ctx = get_nfs_open_context(ctx); 7294 locks_init_lock(&p->fl); 7295 locks_copy_lock(&p->fl, fl); 7296 return p; 7297 out_free_seqid: 7298 nfs_free_seqid(p->arg.open_seqid); 7299 out_free: 7300 kfree(p); 7301 return NULL; 7302 } 7303 7304 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) 7305 { 7306 struct nfs4_lockdata *data = calldata; 7307 struct nfs4_state *state = data->lsp->ls_state; 7308 7309 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) 7310 goto out_wait; 7311 /* Do we need to do an open_to_lock_owner? */ 7312 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) { 7313 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { 7314 goto out_release_lock_seqid; 7315 } 7316 nfs4_stateid_copy(&data->arg.open_stateid, 7317 &state->open_stateid); 7318 data->arg.new_lock_owner = 1; 7319 data->res.open_seqid = data->arg.open_seqid; 7320 } else { 7321 data->arg.new_lock_owner = 0; 7322 nfs4_stateid_copy(&data->arg.lock_stateid, 7323 &data->lsp->ls_stateid); 7324 } 7325 if (!nfs4_valid_open_stateid(state)) { 7326 data->rpc_status = -EBADF; 7327 task->tk_action = NULL; 7328 goto out_release_open_seqid; 7329 } 7330 data->timestamp = jiffies; 7331 if (nfs4_setup_sequence(data->server->nfs_client, 7332 &data->arg.seq_args, 7333 &data->res.seq_res, 7334 task) == 0) 7335 return; 7336 out_release_open_seqid: 7337 nfs_release_seqid(data->arg.open_seqid); 7338 out_release_lock_seqid: 7339 nfs_release_seqid(data->arg.lock_seqid); 7340 out_wait: 7341 nfs4_sequence_done(task, &data->res.seq_res); 7342 dprintk("%s: ret = %d\n", __func__, data->rpc_status); 7343 } 7344 7345 static void nfs4_lock_done(struct rpc_task *task, void *calldata) 7346 { 7347 struct nfs4_lockdata *data = calldata; 7348 struct nfs4_lock_state *lsp = data->lsp; 7349 7350 if (!nfs4_sequence_done(task, &data->res.seq_res)) 7351 return; 7352 7353 data->rpc_status = task->tk_status; 7354 switch (task->tk_status) { 7355 case 0: 7356 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)), 7357 data->timestamp); 7358 if (data->arg.new_lock && !data->cancelled) { 7359 data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS); 7360 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) 7361 goto out_restart; 7362 } 7363 if (data->arg.new_lock_owner != 0) { 7364 nfs_confirm_seqid(&lsp->ls_seqid, 0); 7365 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid); 7366 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 7367 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid)) 7368 goto out_restart; 7369 break; 7370 case -NFS4ERR_OLD_STATEID: 7371 if (data->arg.new_lock_owner != 0 && 7372 nfs4_refresh_open_old_stateid(&data->arg.open_stateid, 7373 lsp->ls_state)) 7374 goto out_restart; 7375 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp)) 7376 goto out_restart; 7377 fallthrough; 7378 case -NFS4ERR_BAD_STATEID: 7379 case -NFS4ERR_STALE_STATEID: 7380 case -NFS4ERR_EXPIRED: 7381 if (data->arg.new_lock_owner != 0) { 7382 if (!nfs4_stateid_match(&data->arg.open_stateid, 7383 &lsp->ls_state->open_stateid)) 7384 goto out_restart; 7385 } else if (!nfs4_stateid_match(&data->arg.lock_stateid, 7386 &lsp->ls_stateid)) 7387 goto out_restart; 7388 } 7389 out_done: 7390 dprintk("%s: ret = %d!\n", __func__, data->rpc_status); 7391 return; 7392 out_restart: 7393 if (!data->cancelled) 7394 rpc_restart_call_prepare(task); 7395 goto out_done; 7396 } 7397 7398 static void nfs4_lock_release(void *calldata) 7399 { 7400 struct nfs4_lockdata *data = calldata; 7401 7402 nfs_free_seqid(data->arg.open_seqid); 7403 if (data->cancelled && data->rpc_status == 0) { 7404 struct rpc_task *task; 7405 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, 7406 data->arg.lock_seqid); 7407 if (!IS_ERR(task)) 7408 rpc_put_task_async(task); 7409 dprintk("%s: cancelling lock!\n", __func__); 7410 } else 7411 nfs_free_seqid(data->arg.lock_seqid); 7412 nfs4_put_lock_state(data->lsp); 7413 put_nfs_open_context(data->ctx); 7414 kfree(data); 7415 } 7416 7417 static const struct rpc_call_ops nfs4_lock_ops = { 7418 .rpc_call_prepare = nfs4_lock_prepare, 7419 .rpc_call_done = nfs4_lock_done, 7420 .rpc_release = nfs4_lock_release, 7421 }; 7422 7423 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) 7424 { 7425 switch (error) { 7426 case -NFS4ERR_ADMIN_REVOKED: 7427 case -NFS4ERR_EXPIRED: 7428 case -NFS4ERR_BAD_STATEID: 7429 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 7430 if (new_lock_owner != 0 || 7431 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) 7432 nfs4_schedule_stateid_recovery(server, lsp->ls_state); 7433 break; 7434 case -NFS4ERR_STALE_STATEID: 7435 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 7436 nfs4_schedule_lease_recovery(server->nfs_client); 7437 } 7438 } 7439 7440 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) 7441 { 7442 struct nfs4_lockdata *data; 7443 struct rpc_task *task; 7444 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; 7445 struct rpc_message msg = { 7446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], 7447 .rpc_cred = state->owner->so_cred, 7448 }; 7449 struct rpc_task_setup task_setup_data = { 7450 .rpc_client = NFS_CLIENT(state->inode), 7451 .rpc_message = &msg, 7452 .callback_ops = &nfs4_lock_ops, 7453 .workqueue = nfsiod_workqueue, 7454 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, 7455 }; 7456 int ret; 7457 7458 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE)) 7459 task_setup_data.flags |= RPC_TASK_MOVEABLE; 7460 7461 data = nfs4_alloc_lockdata(fl, 7462 nfs_file_open_context(fl->c.flc_file), 7463 fl->fl_u.nfs4_fl.owner, GFP_KERNEL); 7464 if (data == NULL) 7465 return -ENOMEM; 7466 if (IS_SETLKW(cmd)) 7467 data->arg.block = 1; 7468 nfs4_init_sequence(clp, &data->arg.seq_args, &data->res.seq_res, 1, 7469 recovery_type > NFS_LOCK_NEW); 7470 msg.rpc_argp = &data->arg; 7471 msg.rpc_resp = &data->res; 7472 task_setup_data.callback_data = data; 7473 if (recovery_type > NFS_LOCK_NEW) { 7474 if (recovery_type == NFS_LOCK_RECLAIM) 7475 data->arg.reclaim = NFS_LOCK_RECLAIM; 7476 } else 7477 data->arg.new_lock = 1; 7478 task = rpc_run_task(&task_setup_data); 7479 if (IS_ERR(task)) 7480 return PTR_ERR(task); 7481 ret = rpc_wait_for_completion_task(task); 7482 if (ret == 0) { 7483 ret = data->rpc_status; 7484 if (ret) 7485 nfs4_handle_setlk_error(data->server, data->lsp, 7486 data->arg.new_lock_owner, ret); 7487 } else 7488 data->cancelled = true; 7489 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret); 7490 rpc_put_task(task); 7491 dprintk("%s: ret = %d\n", __func__, ret); 7492 return ret; 7493 } 7494 7495 int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) 7496 { 7497 struct nfs_server *server = NFS_SERVER(state->inode); 7498 struct nfs4_exception exception = { 7499 .inode = state->inode, 7500 }; 7501 int err; 7502 7503 do { 7504 /* Cache the lock if possible... */ 7505 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 7506 return 0; 7507 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); 7508 if (err != -NFS4ERR_DELAY) 7509 break; 7510 nfs4_handle_exception(server, err, &exception); 7511 } while (exception.retry); 7512 return err; 7513 } 7514 7515 int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) 7516 { 7517 struct nfs_server *server = NFS_SERVER(state->inode); 7518 struct nfs4_exception exception = { 7519 .inode = state->inode, 7520 }; 7521 int err; 7522 7523 err = nfs4_set_lock_state(state, request); 7524 if (err != 0) 7525 return err; 7526 if (!recover_lost_locks) { 7527 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); 7528 return 0; 7529 } 7530 do { 7531 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 7532 return 0; 7533 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); 7534 switch (err) { 7535 default: 7536 goto out; 7537 case -NFS4ERR_GRACE: 7538 case -NFS4ERR_DELAY: 7539 nfs4_handle_exception(server, err, &exception); 7540 err = 0; 7541 } 7542 } while (exception.retry); 7543 out: 7544 return err; 7545 } 7546 7547 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) 7548 { 7549 struct nfs4_lock_state *lsp; 7550 int status; 7551 7552 status = nfs4_set_lock_state(state, request); 7553 if (status != 0) 7554 return status; 7555 lsp = request->fl_u.nfs4_fl.owner; 7556 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) || 7557 test_bit(NFS_LOCK_LOST, &lsp->ls_flags)) 7558 return 0; 7559 return nfs4_lock_expired(state, request); 7560 } 7561 7562 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7563 { 7564 struct nfs_inode *nfsi = NFS_I(state->inode); 7565 struct nfs4_state_owner *sp = state->owner; 7566 unsigned char flags = request->c.flc_flags; 7567 int status; 7568 7569 request->c.flc_flags |= FL_ACCESS; 7570 status = locks_lock_inode_wait(state->inode, request); 7571 if (status < 0) 7572 goto out; 7573 mutex_lock(&sp->so_delegreturn_mutex); 7574 down_read(&nfsi->rwsem); 7575 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { 7576 /* Yes: cache locks! */ 7577 /* ...but avoid races with delegation recall... */ 7578 request->c.flc_flags = flags & ~FL_SLEEP; 7579 status = locks_lock_inode_wait(state->inode, request); 7580 up_read(&nfsi->rwsem); 7581 mutex_unlock(&sp->so_delegreturn_mutex); 7582 goto out; 7583 } 7584 up_read(&nfsi->rwsem); 7585 mutex_unlock(&sp->so_delegreturn_mutex); 7586 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); 7587 out: 7588 request->c.flc_flags = flags; 7589 return status; 7590 } 7591 7592 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7593 { 7594 struct nfs4_exception exception = { 7595 .state = state, 7596 .inode = state->inode, 7597 .interruptible = true, 7598 }; 7599 int err; 7600 7601 do { 7602 err = _nfs4_proc_setlk(state, cmd, request); 7603 if (err == -NFS4ERR_DENIED) 7604 err = -EAGAIN; 7605 err = nfs4_handle_exception(NFS_SERVER(state->inode), 7606 err, &exception); 7607 } while (exception.retry); 7608 return err; 7609 } 7610 7611 #define NFS4_LOCK_MINTIMEOUT (1 * HZ) 7612 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) 7613 7614 static int 7615 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd, 7616 struct file_lock *request) 7617 { 7618 int status = -ERESTARTSYS; 7619 unsigned long timeout = NFS4_LOCK_MINTIMEOUT; 7620 7621 while(!signalled()) { 7622 status = nfs4_proc_setlk(state, cmd, request); 7623 if ((status != -EAGAIN) || IS_SETLK(cmd)) 7624 break; 7625 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE); 7626 schedule_timeout(timeout); 7627 timeout *= 2; 7628 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout); 7629 status = -ERESTARTSYS; 7630 } 7631 return status; 7632 } 7633 7634 struct nfs4_lock_waiter { 7635 struct inode *inode; 7636 struct nfs_lowner owner; 7637 wait_queue_entry_t wait; 7638 }; 7639 7640 static int 7641 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key) 7642 { 7643 struct nfs4_lock_waiter *waiter = 7644 container_of(wait, struct nfs4_lock_waiter, wait); 7645 7646 /* NULL key means to wake up everyone */ 7647 if (key) { 7648 struct cb_notify_lock_args *cbnl = key; 7649 struct nfs_lowner *lowner = &cbnl->cbnl_owner, 7650 *wowner = &waiter->owner; 7651 7652 /* Only wake if the callback was for the same owner. */ 7653 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev) 7654 return 0; 7655 7656 /* Make sure it's for the right inode */ 7657 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh)) 7658 return 0; 7659 } 7660 7661 return woken_wake_function(wait, mode, flags, key); 7662 } 7663 7664 static int 7665 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 7666 { 7667 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; 7668 struct nfs_server *server = NFS_SERVER(state->inode); 7669 struct nfs_client *clp = server->nfs_client; 7670 wait_queue_head_t *q = &clp->cl_lock_waitq; 7671 struct nfs4_lock_waiter waiter = { 7672 .inode = state->inode, 7673 .owner = { .clientid = clp->cl_clientid, 7674 .id = lsp->ls_seqid.owner_id, 7675 .s_dev = server->s_dev }, 7676 }; 7677 int status; 7678 7679 /* Don't bother with waitqueue if we don't expect a callback */ 7680 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags)) 7681 return nfs4_retry_setlk_simple(state, cmd, request); 7682 7683 init_wait(&waiter.wait); 7684 waiter.wait.func = nfs4_wake_lock_waiter; 7685 add_wait_queue(q, &waiter.wait); 7686 7687 do { 7688 status = nfs4_proc_setlk(state, cmd, request); 7689 if (status != -EAGAIN || IS_SETLK(cmd)) 7690 break; 7691 7692 status = -ERESTARTSYS; 7693 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE, 7694 NFS4_LOCK_MAXTIMEOUT); 7695 } while (!signalled()); 7696 7697 remove_wait_queue(q, &waiter.wait); 7698 7699 return status; 7700 } 7701 7702 static int 7703 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) 7704 { 7705 struct nfs_open_context *ctx; 7706 struct nfs4_state *state; 7707 int status; 7708 7709 /* verify open state */ 7710 ctx = nfs_file_open_context(filp); 7711 state = ctx->state; 7712 7713 if (IS_GETLK(cmd)) { 7714 if (state != NULL) 7715 return nfs4_proc_getlk(state, F_GETLK, request); 7716 return 0; 7717 } 7718 7719 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) 7720 return -EINVAL; 7721 7722 if (lock_is_unlock(request)) { 7723 if (state != NULL) 7724 return nfs4_proc_unlck(state, cmd, request); 7725 return 0; 7726 } 7727 7728 if (state == NULL) 7729 return -ENOLCK; 7730 7731 if ((request->c.flc_flags & FL_POSIX) && 7732 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) 7733 return -ENOLCK; 7734 7735 /* 7736 * Don't rely on the VFS having checked the file open mode, 7737 * since it won't do this for flock() locks. 7738 */ 7739 switch (request->c.flc_type) { 7740 case F_RDLCK: 7741 if (!(filp->f_mode & FMODE_READ)) 7742 return -EBADF; 7743 break; 7744 case F_WRLCK: 7745 if (!(filp->f_mode & FMODE_WRITE)) 7746 return -EBADF; 7747 } 7748 7749 status = nfs4_set_lock_state(state, request); 7750 if (status != 0) 7751 return status; 7752 7753 return nfs4_retry_setlk(state, cmd, request); 7754 } 7755 7756 static int nfs4_delete_lease(struct file *file, void **priv) 7757 { 7758 return generic_setlease(file, F_UNLCK, NULL, priv); 7759 } 7760 7761 static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease, 7762 void **priv) 7763 { 7764 struct inode *inode = file_inode(file); 7765 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE; 7766 int ret; 7767 7768 /* No delegation, no lease */ 7769 if (!nfs4_have_delegation(inode, type, 0)) 7770 return -EAGAIN; 7771 ret = generic_setlease(file, arg, lease, priv); 7772 if (ret || nfs4_have_delegation(inode, type, 0)) 7773 return ret; 7774 /* We raced with a delegation return */ 7775 nfs4_delete_lease(file, priv); 7776 return -EAGAIN; 7777 } 7778 7779 int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease, 7780 void **priv) 7781 { 7782 switch (arg) { 7783 case F_RDLCK: 7784 case F_WRLCK: 7785 return nfs4_add_lease(file, arg, lease, priv); 7786 case F_UNLCK: 7787 return nfs4_delete_lease(file, priv); 7788 default: 7789 return -EINVAL; 7790 } 7791 } 7792 7793 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) 7794 { 7795 struct nfs_server *server = NFS_SERVER(state->inode); 7796 int err; 7797 7798 err = nfs4_set_lock_state(state, fl); 7799 if (err != 0) 7800 return err; 7801 do { 7802 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); 7803 if (err != -NFS4ERR_DELAY && err != -NFS4ERR_GRACE) 7804 break; 7805 ssleep(1); 7806 } while (err == -NFS4ERR_DELAY || err == -NFSERR_GRACE); 7807 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err); 7808 } 7809 7810 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" 7811 7812 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler, 7813 struct mnt_idmap *idmap, 7814 struct dentry *unused, struct inode *inode, 7815 const char *key, const void *buf, 7816 size_t buflen, int flags) 7817 { 7818 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL); 7819 } 7820 7821 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler, 7822 struct dentry *unused, struct inode *inode, 7823 const char *key, void *buf, size_t buflen) 7824 { 7825 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL); 7826 } 7827 7828 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry) 7829 { 7830 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL); 7831 } 7832 7833 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl" 7834 7835 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler, 7836 struct mnt_idmap *idmap, 7837 struct dentry *unused, struct inode *inode, 7838 const char *key, const void *buf, 7839 size_t buflen, int flags) 7840 { 7841 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL); 7842 } 7843 7844 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler, 7845 struct dentry *unused, struct inode *inode, 7846 const char *key, void *buf, size_t buflen) 7847 { 7848 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL); 7849 } 7850 7851 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry) 7852 { 7853 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL); 7854 } 7855 7856 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl" 7857 7858 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler, 7859 struct mnt_idmap *idmap, 7860 struct dentry *unused, struct inode *inode, 7861 const char *key, const void *buf, 7862 size_t buflen, int flags) 7863 { 7864 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL); 7865 } 7866 7867 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler, 7868 struct dentry *unused, struct inode *inode, 7869 const char *key, void *buf, size_t buflen) 7870 { 7871 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL); 7872 } 7873 7874 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry) 7875 { 7876 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL); 7877 } 7878 7879 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 7880 7881 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler, 7882 struct mnt_idmap *idmap, 7883 struct dentry *unused, struct inode *inode, 7884 const char *key, const void *buf, 7885 size_t buflen, int flags) 7886 { 7887 if (security_ismaclabel(key)) 7888 return nfs4_set_security_label(inode, buf, buflen); 7889 7890 return -EOPNOTSUPP; 7891 } 7892 7893 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler, 7894 struct dentry *unused, struct inode *inode, 7895 const char *key, void *buf, size_t buflen) 7896 { 7897 if (security_ismaclabel(key)) 7898 return nfs4_get_security_label(inode, buf, buflen); 7899 return -EOPNOTSUPP; 7900 } 7901 7902 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = { 7903 .prefix = XATTR_SECURITY_PREFIX, 7904 .get = nfs4_xattr_get_nfs4_label, 7905 .set = nfs4_xattr_set_nfs4_label, 7906 }; 7907 7908 #endif 7909 7910 #ifdef CONFIG_NFS_V4_2 7911 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler, 7912 struct mnt_idmap *idmap, 7913 struct dentry *unused, struct inode *inode, 7914 const char *key, const void *buf, 7915 size_t buflen, int flags) 7916 { 7917 u32 mask; 7918 int ret; 7919 7920 if (!nfs_server_capable(inode, NFS_CAP_XATTR)) 7921 return -EOPNOTSUPP; 7922 7923 /* 7924 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA* 7925 * flags right now. Handling of xattr operations use the normal 7926 * file read/write permissions. 7927 * 7928 * Just in case the server has other ideas (which RFC 8276 allows), 7929 * do a cached access check for the XA* flags to possibly avoid 7930 * doing an RPC and getting EACCES back. 7931 */ 7932 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) { 7933 if (!(mask & NFS_ACCESS_XAWRITE)) 7934 return -EACCES; 7935 } 7936 7937 if (buf == NULL) { 7938 ret = nfs42_proc_removexattr(inode, key); 7939 if (!ret) 7940 nfs4_xattr_cache_remove(inode, key); 7941 } else { 7942 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags); 7943 if (!ret) 7944 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen); 7945 } 7946 7947 return ret; 7948 } 7949 7950 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler, 7951 struct dentry *unused, struct inode *inode, 7952 const char *key, void *buf, size_t buflen) 7953 { 7954 u32 mask; 7955 ssize_t ret; 7956 7957 if (!nfs_server_capable(inode, NFS_CAP_XATTR)) 7958 return -EOPNOTSUPP; 7959 7960 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) { 7961 if (!(mask & NFS_ACCESS_XAREAD)) 7962 return -EACCES; 7963 } 7964 7965 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE); 7966 if (ret) 7967 return ret; 7968 7969 ret = nfs4_xattr_cache_get(inode, key, buf, buflen); 7970 if (ret >= 0 || (ret < 0 && ret != -ENOENT)) 7971 return ret; 7972 7973 ret = nfs42_proc_getxattr(inode, key, buf, buflen); 7974 7975 return ret; 7976 } 7977 7978 static ssize_t 7979 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len) 7980 { 7981 u64 cookie; 7982 bool eof; 7983 ssize_t ret, size; 7984 char *buf; 7985 size_t buflen; 7986 u32 mask; 7987 7988 if (!nfs_server_capable(inode, NFS_CAP_XATTR)) 7989 return 0; 7990 7991 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) { 7992 if (!(mask & NFS_ACCESS_XALIST)) 7993 return 0; 7994 } 7995 7996 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE); 7997 if (ret) 7998 return ret; 7999 8000 ret = nfs4_xattr_cache_list(inode, list, list_len); 8001 if (ret >= 0 || (ret < 0 && ret != -ENOENT)) 8002 return ret; 8003 8004 cookie = 0; 8005 eof = false; 8006 buflen = list_len ? list_len : XATTR_LIST_MAX; 8007 buf = list_len ? list : NULL; 8008 size = 0; 8009 8010 while (!eof) { 8011 ret = nfs42_proc_listxattrs(inode, buf, buflen, 8012 &cookie, &eof); 8013 if (ret < 0) 8014 return ret; 8015 8016 if (list_len) { 8017 buf += ret; 8018 buflen -= ret; 8019 } 8020 size += ret; 8021 } 8022 8023 if (list_len) 8024 nfs4_xattr_cache_set_list(inode, list, size); 8025 8026 return size; 8027 } 8028 8029 #else 8030 8031 static ssize_t 8032 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len) 8033 { 8034 return 0; 8035 } 8036 #endif /* CONFIG_NFS_V4_2 */ 8037 8038 /* 8039 * nfs_fhget will use either the mounted_on_fileid or the fileid 8040 */ 8041 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) 8042 { 8043 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || 8044 (fattr->valid & NFS_ATTR_FATTR_FILEID)) && 8045 (fattr->valid & NFS_ATTR_FATTR_FSID) && 8046 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) 8047 return; 8048 8049 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | 8050 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; 8051 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; 8052 fattr->nlink = 2; 8053 } 8054 8055 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, 8056 const struct qstr *name, 8057 struct nfs4_fs_locations *fs_locations, 8058 struct page *page) 8059 { 8060 struct nfs_server *server = NFS_SERVER(dir); 8061 u32 bitmask[3]; 8062 struct nfs4_fs_locations_arg args = { 8063 .dir_fh = NFS_FH(dir), 8064 .name = name, 8065 .page = page, 8066 .bitmask = bitmask, 8067 }; 8068 struct nfs4_fs_locations_res res = { 8069 .fs_locations = fs_locations, 8070 }; 8071 struct rpc_message msg = { 8072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 8073 .rpc_argp = &args, 8074 .rpc_resp = &res, 8075 }; 8076 int status; 8077 8078 dprintk("%s: start\n", __func__); 8079 8080 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS; 8081 bitmask[1] = nfs4_fattr_bitmap[1]; 8082 8083 /* Ask for the fileid of the absent filesystem if mounted_on_fileid 8084 * is not supported */ 8085 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) 8086 bitmask[0] &= ~FATTR4_WORD0_FILEID; 8087 else 8088 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID; 8089 8090 nfs_fattr_init(fs_locations->fattr); 8091 fs_locations->server = server; 8092 fs_locations->nlocations = 0; 8093 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); 8094 dprintk("%s: returned status = %d\n", __func__, status); 8095 return status; 8096 } 8097 8098 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, 8099 const struct qstr *name, 8100 struct nfs4_fs_locations *fs_locations, 8101 struct page *page) 8102 { 8103 struct nfs4_exception exception = { 8104 .interruptible = true, 8105 }; 8106 int err; 8107 do { 8108 err = _nfs4_proc_fs_locations(client, dir, name, 8109 fs_locations, page); 8110 trace_nfs4_get_fs_locations(dir, name, err); 8111 err = nfs4_handle_exception(NFS_SERVER(dir), err, 8112 &exception); 8113 } while (exception.retry); 8114 return err; 8115 } 8116 8117 /* 8118 * This operation also signals the server that this client is 8119 * performing migration recovery. The server can stop asserting 8120 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID 8121 * performing this operation is identified in the SEQUENCE 8122 * operation in this compound. 8123 * 8124 * When the client supports GETATTR(fs_locations_info), it can 8125 * be plumbed in here. 8126 */ 8127 static int _nfs41_proc_get_locations(struct nfs_server *server, 8128 struct nfs_fh *fhandle, 8129 struct nfs4_fs_locations *locations, 8130 struct page *page, const struct cred *cred) 8131 { 8132 struct rpc_clnt *clnt = server->client; 8133 struct nfs_client *clp = server->nfs_client; 8134 u32 bitmask[2] = { 8135 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, 8136 }; 8137 struct nfs4_fs_locations_arg args = { 8138 .fh = fhandle, 8139 .page = page, 8140 .bitmask = bitmask, 8141 .migration = 1, /* skip LOOKUP */ 8142 }; 8143 struct nfs4_fs_locations_res res = { 8144 .fs_locations = locations, 8145 .migration = 1, 8146 }; 8147 struct rpc_message msg = { 8148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 8149 .rpc_argp = &args, 8150 .rpc_resp = &res, 8151 .rpc_cred = cred, 8152 }; 8153 struct nfs4_call_sync_data data = { 8154 .seq_server = server, 8155 .seq_args = &args.seq_args, 8156 .seq_res = &res.seq_res, 8157 }; 8158 struct rpc_task_setup task_setup_data = { 8159 .rpc_client = clnt, 8160 .rpc_message = &msg, 8161 .callback_ops = clp->cl_mvops->call_sync_ops, 8162 .callback_data = &data, 8163 .flags = RPC_TASK_NO_ROUND_ROBIN, 8164 }; 8165 int status; 8166 8167 nfs_fattr_init(locations->fattr); 8168 locations->server = server; 8169 locations->nlocations = 0; 8170 8171 nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 1); 8172 status = nfs4_call_sync_custom(&task_setup_data); 8173 if (status == NFS4_OK && 8174 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) 8175 status = -NFS4ERR_LEASE_MOVED; 8176 return status; 8177 } 8178 8179 /** 8180 * nfs4_proc_get_locations - discover locations for a migrated FSID 8181 * @server: pointer to nfs_server to process 8182 * @fhandle: pointer to the kernel NFS client file handle 8183 * @locations: result of query 8184 * @page: buffer 8185 * @cred: credential to use for this operation 8186 * 8187 * Returns NFS4_OK on success, a negative NFS4ERR status code if the 8188 * operation failed, or a negative errno if a local error occurred. 8189 * 8190 * On success, "locations" is filled in, but if the server has 8191 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not 8192 * asserted. 8193 * 8194 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases 8195 * from this client that require migration recovery. 8196 */ 8197 int nfs4_proc_get_locations(struct nfs_server *server, 8198 struct nfs_fh *fhandle, 8199 struct nfs4_fs_locations *locations, 8200 struct page *page, const struct cred *cred) 8201 { 8202 struct nfs_client *clp = server->nfs_client; 8203 const struct nfs4_mig_recovery_ops *ops = 8204 clp->cl_mvops->mig_recovery_ops; 8205 struct nfs4_exception exception = { 8206 .interruptible = true, 8207 }; 8208 int status; 8209 8210 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, 8211 (unsigned long long)server->fsid.major, 8212 (unsigned long long)server->fsid.minor, 8213 clp->cl_hostname); 8214 nfs_display_fhandle(fhandle, __func__); 8215 8216 do { 8217 status = ops->get_locations(server, fhandle, locations, page, 8218 cred); 8219 if (status != -NFS4ERR_DELAY) 8220 break; 8221 nfs4_handle_exception(server, status, &exception); 8222 } while (exception.retry); 8223 return status; 8224 } 8225 8226 /* 8227 * This operation also signals the server that this client is 8228 * performing "lease moved" recovery. The server can stop asserting 8229 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing 8230 * this operation is identified in the SEQUENCE operation in this 8231 * compound. 8232 */ 8233 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred) 8234 { 8235 struct nfs_server *server = NFS_SERVER(inode); 8236 struct rpc_clnt *clnt = server->client; 8237 struct nfs4_fsid_present_arg args = { 8238 .fh = NFS_FH(inode), 8239 }; 8240 struct nfs4_fsid_present_res res = { 8241 }; 8242 struct rpc_message msg = { 8243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], 8244 .rpc_argp = &args, 8245 .rpc_resp = &res, 8246 .rpc_cred = cred, 8247 }; 8248 int status; 8249 8250 res.fh = nfs_alloc_fhandle(); 8251 if (res.fh == NULL) 8252 return -ENOMEM; 8253 8254 nfs4_init_sequence(server->nfs_client, &args.seq_args, &res.seq_res, 0, 1); 8255 status = nfs4_call_sync_sequence(clnt, server, &msg, 8256 &args.seq_args, &res.seq_res); 8257 nfs_free_fhandle(res.fh); 8258 if (status == NFS4_OK && 8259 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) 8260 status = -NFS4ERR_LEASE_MOVED; 8261 return status; 8262 } 8263 8264 /** 8265 * nfs4_proc_fsid_present - Is this FSID present or absent on server? 8266 * @inode: inode on FSID to check 8267 * @cred: credential to use for this operation 8268 * 8269 * Server indicates whether the FSID is present, moved, or not 8270 * recognized. This operation is necessary to clear a LEASE_MOVED 8271 * condition for this client ID. 8272 * 8273 * Returns NFS4_OK if the FSID is present on this server, 8274 * -NFS4ERR_MOVED if the FSID is no longer present, a negative 8275 * NFS4ERR code if some error occurred on the server, or a 8276 * negative errno if a local failure occurred. 8277 */ 8278 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred) 8279 { 8280 struct nfs_server *server = NFS_SERVER(inode); 8281 struct nfs_client *clp = server->nfs_client; 8282 const struct nfs4_mig_recovery_ops *ops = 8283 clp->cl_mvops->mig_recovery_ops; 8284 struct nfs4_exception exception = { 8285 .interruptible = true, 8286 }; 8287 int status; 8288 8289 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, 8290 (unsigned long long)server->fsid.major, 8291 (unsigned long long)server->fsid.minor, 8292 clp->cl_hostname); 8293 nfs_display_fhandle(NFS_FH(inode), __func__); 8294 8295 do { 8296 status = ops->fsid_present(inode, cred); 8297 if (status != -NFS4ERR_DELAY) 8298 break; 8299 nfs4_handle_exception(server, status, &exception); 8300 } while (exception.retry); 8301 return status; 8302 } 8303 8304 /* 8305 * If 'use_integrity' is true and the state managment nfs_client 8306 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient 8307 * and the machine credential as per RFC3530bis and RFC5661 Security 8308 * Considerations sections. Otherwise, just use the user cred with the 8309 * filesystem's rpc_client. 8310 */ 8311 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity) 8312 { 8313 int status; 8314 struct rpc_clnt *clnt = NFS_SERVER(dir)->client; 8315 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client; 8316 struct nfs4_secinfo_arg args = { 8317 .dir_fh = NFS_FH(dir), 8318 .name = name, 8319 }; 8320 struct nfs4_secinfo_res res = { 8321 .flavors = flavors, 8322 }; 8323 struct rpc_message msg = { 8324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], 8325 .rpc_argp = &args, 8326 .rpc_resp = &res, 8327 }; 8328 struct nfs4_call_sync_data data = { 8329 .seq_server = NFS_SERVER(dir), 8330 .seq_args = &args.seq_args, 8331 .seq_res = &res.seq_res, 8332 }; 8333 struct rpc_task_setup task_setup = { 8334 .rpc_client = clnt, 8335 .rpc_message = &msg, 8336 .callback_ops = clp->cl_mvops->call_sync_ops, 8337 .callback_data = &data, 8338 .flags = RPC_TASK_NO_ROUND_ROBIN, 8339 }; 8340 const struct cred *cred = NULL; 8341 8342 if (use_integrity) { 8343 clnt = clp->cl_rpcclient; 8344 task_setup.rpc_client = clnt; 8345 8346 cred = nfs4_get_clid_cred(clp); 8347 msg.rpc_cred = cred; 8348 } 8349 8350 dprintk("NFS call secinfo %s\n", name->name); 8351 8352 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg); 8353 nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 0); 8354 status = nfs4_call_sync_custom(&task_setup); 8355 8356 dprintk("NFS reply secinfo: %d\n", status); 8357 8358 put_cred(cred); 8359 return status; 8360 } 8361 8362 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, 8363 struct nfs4_secinfo_flavors *flavors) 8364 { 8365 struct nfs4_exception exception = { 8366 .interruptible = true, 8367 }; 8368 int err; 8369 do { 8370 err = -NFS4ERR_WRONGSEC; 8371 8372 /* try to use integrity protection with machine cred */ 8373 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client)) 8374 err = _nfs4_proc_secinfo(dir, name, flavors, true); 8375 8376 /* 8377 * if unable to use integrity protection, or SECINFO with 8378 * integrity protection returns NFS4ERR_WRONGSEC (which is 8379 * disallowed by spec, but exists in deployed servers) use 8380 * the current filesystem's rpc_client and the user cred. 8381 */ 8382 if (err == -NFS4ERR_WRONGSEC) 8383 err = _nfs4_proc_secinfo(dir, name, flavors, false); 8384 8385 trace_nfs4_secinfo(dir, name, err); 8386 err = nfs4_handle_exception(NFS_SERVER(dir), err, 8387 &exception); 8388 } while (exception.retry); 8389 return err; 8390 } 8391 8392 /* 8393 * Check the exchange flags returned by the server for invalid flags, having 8394 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or 8395 * DS flags set. 8396 */ 8397 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version) 8398 { 8399 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R)) 8400 goto out_inval; 8401 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R)) 8402 goto out_inval; 8403 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && 8404 (flags & EXCHGID4_FLAG_USE_NON_PNFS)) 8405 goto out_inval; 8406 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) 8407 goto out_inval; 8408 return NFS_OK; 8409 out_inval: 8410 return -NFS4ERR_INVAL; 8411 } 8412 8413 static bool 8414 nfs41_same_server_scope(struct nfs41_server_scope *a, 8415 struct nfs41_server_scope *b) 8416 { 8417 if (a->server_scope_sz != b->server_scope_sz) 8418 return false; 8419 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0; 8420 } 8421 8422 static void 8423 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata) 8424 { 8425 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp; 8426 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp; 8427 struct nfs_client *clp = args->client; 8428 8429 switch (task->tk_status) { 8430 case -NFS4ERR_BADSESSION: 8431 case -NFS4ERR_DEADSESSION: 8432 nfs4_schedule_session_recovery(clp->cl_session, 8433 task->tk_status); 8434 return; 8435 } 8436 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH && 8437 res->dir != NFS4_CDFS4_BOTH) { 8438 rpc_task_close_connection(task); 8439 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES) 8440 rpc_restart_call(task); 8441 } 8442 } 8443 8444 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = { 8445 .rpc_call_done = nfs4_bind_one_conn_to_session_done, 8446 }; 8447 8448 /* 8449 * nfs4_proc_bind_one_conn_to_session() 8450 * 8451 * The 4.1 client currently uses the same TCP connection for the 8452 * fore and backchannel. 8453 */ 8454 static 8455 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt, 8456 struct rpc_xprt *xprt, 8457 struct nfs_client *clp, 8458 const struct cred *cred) 8459 { 8460 int status; 8461 struct nfs41_bind_conn_to_session_args args = { 8462 .client = clp, 8463 .dir = NFS4_CDFC4_FORE_OR_BOTH, 8464 .retries = 0, 8465 }; 8466 struct nfs41_bind_conn_to_session_res res; 8467 struct rpc_message msg = { 8468 .rpc_proc = 8469 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], 8470 .rpc_argp = &args, 8471 .rpc_resp = &res, 8472 .rpc_cred = cred, 8473 }; 8474 struct rpc_task_setup task_setup_data = { 8475 .rpc_client = clnt, 8476 .rpc_xprt = xprt, 8477 .callback_ops = &nfs4_bind_one_conn_to_session_ops, 8478 .rpc_message = &msg, 8479 .flags = RPC_TASK_TIMEOUT, 8480 }; 8481 struct rpc_task *task; 8482 8483 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id); 8484 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN)) 8485 args.dir = NFS4_CDFC4_FORE; 8486 8487 /* Do not set the backchannel flag unless this is clnt->cl_xprt */ 8488 if (xprt != rcu_access_pointer(clnt->cl_xprt)) 8489 args.dir = NFS4_CDFC4_FORE; 8490 8491 task = rpc_run_task(&task_setup_data); 8492 if (!IS_ERR(task)) { 8493 status = task->tk_status; 8494 rpc_put_task(task); 8495 } else 8496 status = PTR_ERR(task); 8497 trace_nfs4_bind_conn_to_session(clp, status); 8498 if (status == 0) { 8499 if (memcmp(res.sessionid.data, 8500 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { 8501 dprintk("NFS: %s: Session ID mismatch\n", __func__); 8502 return -EIO; 8503 } 8504 if ((res.dir & args.dir) != res.dir || res.dir == 0) { 8505 dprintk("NFS: %s: Unexpected direction from server\n", 8506 __func__); 8507 return -EIO; 8508 } 8509 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) { 8510 dprintk("NFS: %s: Server returned RDMA mode = true\n", 8511 __func__); 8512 return -EIO; 8513 } 8514 } 8515 8516 return status; 8517 } 8518 8519 struct rpc_bind_conn_calldata { 8520 struct nfs_client *clp; 8521 const struct cred *cred; 8522 }; 8523 8524 static int 8525 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt, 8526 struct rpc_xprt *xprt, 8527 void *calldata) 8528 { 8529 struct rpc_bind_conn_calldata *p = calldata; 8530 8531 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred); 8532 } 8533 8534 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred) 8535 { 8536 struct rpc_bind_conn_calldata data = { 8537 .clp = clp, 8538 .cred = cred, 8539 }; 8540 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient, 8541 nfs4_proc_bind_conn_to_session_callback, &data); 8542 } 8543 8544 /* 8545 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map 8546 * and operations we'd like to see to enable certain features in the allow map 8547 */ 8548 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = { 8549 .how = SP4_MACH_CRED, 8550 .enforce.u.words = { 8551 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | 8552 1 << (OP_EXCHANGE_ID - 32) | 8553 1 << (OP_CREATE_SESSION - 32) | 8554 1 << (OP_DESTROY_SESSION - 32) | 8555 1 << (OP_DESTROY_CLIENTID - 32) 8556 }, 8557 .allow.u.words = { 8558 [0] = 1 << (OP_CLOSE) | 8559 1 << (OP_OPEN_DOWNGRADE) | 8560 1 << (OP_LOCKU) | 8561 1 << (OP_DELEGRETURN) | 8562 1 << (OP_COMMIT), 8563 [1] = 1 << (OP_SECINFO - 32) | 8564 1 << (OP_SECINFO_NO_NAME - 32) | 8565 1 << (OP_LAYOUTRETURN - 32) | 8566 1 << (OP_TEST_STATEID - 32) | 8567 1 << (OP_FREE_STATEID - 32) | 8568 1 << (OP_WRITE - 32) 8569 } 8570 }; 8571 8572 /* 8573 * Select the state protection mode for client `clp' given the server results 8574 * from exchange_id in `sp'. 8575 * 8576 * Returns 0 on success, negative errno otherwise. 8577 */ 8578 static int nfs4_sp4_select_mode(struct nfs_client *clp, 8579 struct nfs41_state_protection *sp) 8580 { 8581 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = { 8582 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | 8583 1 << (OP_EXCHANGE_ID - 32) | 8584 1 << (OP_CREATE_SESSION - 32) | 8585 1 << (OP_DESTROY_SESSION - 32) | 8586 1 << (OP_DESTROY_CLIENTID - 32) 8587 }; 8588 unsigned long flags = 0; 8589 unsigned int i; 8590 int ret = 0; 8591 8592 if (sp->how == SP4_MACH_CRED) { 8593 /* Print state protect result */ 8594 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n"); 8595 for (i = 0; i <= LAST_NFS4_OP; i++) { 8596 if (test_bit(i, sp->enforce.u.longs)) 8597 dfprintk(MOUNT, " enforce op %d\n", i); 8598 if (test_bit(i, sp->allow.u.longs)) 8599 dfprintk(MOUNT, " allow op %d\n", i); 8600 } 8601 8602 /* make sure nothing is on enforce list that isn't supported */ 8603 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) { 8604 if (sp->enforce.u.words[i] & ~supported_enforce[i]) { 8605 dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); 8606 ret = -EINVAL; 8607 goto out; 8608 } 8609 } 8610 8611 /* 8612 * Minimal mode - state operations are allowed to use machine 8613 * credential. Note this already happens by default, so the 8614 * client doesn't have to do anything more than the negotiation. 8615 * 8616 * NOTE: we don't care if EXCHANGE_ID is in the list - 8617 * we're already using the machine cred for exchange_id 8618 * and will never use a different cred. 8619 */ 8620 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) && 8621 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) && 8622 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) && 8623 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) { 8624 dfprintk(MOUNT, "sp4_mach_cred:\n"); 8625 dfprintk(MOUNT, " minimal mode enabled\n"); 8626 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags); 8627 } else { 8628 dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); 8629 ret = -EINVAL; 8630 goto out; 8631 } 8632 8633 if (test_bit(OP_CLOSE, sp->allow.u.longs) && 8634 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) && 8635 test_bit(OP_DELEGRETURN, sp->allow.u.longs) && 8636 test_bit(OP_LOCKU, sp->allow.u.longs)) { 8637 dfprintk(MOUNT, " cleanup mode enabled\n"); 8638 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags); 8639 } 8640 8641 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) { 8642 dfprintk(MOUNT, " pnfs cleanup mode enabled\n"); 8643 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags); 8644 } 8645 8646 if (test_bit(OP_SECINFO, sp->allow.u.longs) && 8647 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) { 8648 dfprintk(MOUNT, " secinfo mode enabled\n"); 8649 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags); 8650 } 8651 8652 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) && 8653 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) { 8654 dfprintk(MOUNT, " stateid mode enabled\n"); 8655 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags); 8656 } 8657 8658 if (test_bit(OP_WRITE, sp->allow.u.longs)) { 8659 dfprintk(MOUNT, " write mode enabled\n"); 8660 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags); 8661 } 8662 8663 if (test_bit(OP_COMMIT, sp->allow.u.longs)) { 8664 dfprintk(MOUNT, " commit mode enabled\n"); 8665 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags); 8666 } 8667 } 8668 out: 8669 clp->cl_sp4_flags = flags; 8670 return ret; 8671 } 8672 8673 struct nfs41_exchange_id_data { 8674 struct nfs41_exchange_id_res res; 8675 struct nfs41_exchange_id_args args; 8676 }; 8677 8678 static void nfs4_exchange_id_release(void *data) 8679 { 8680 struct nfs41_exchange_id_data *cdata = 8681 (struct nfs41_exchange_id_data *)data; 8682 8683 nfs_put_client(cdata->args.client); 8684 kfree(cdata->res.impl_id); 8685 kfree(cdata->res.server_scope); 8686 kfree(cdata->res.server_owner); 8687 kfree(cdata); 8688 } 8689 8690 static const struct rpc_call_ops nfs4_exchange_id_call_ops = { 8691 .rpc_release = nfs4_exchange_id_release, 8692 }; 8693 8694 /* 8695 * _nfs4_proc_exchange_id() 8696 * 8697 * Wrapper for EXCHANGE_ID operation. 8698 */ 8699 static struct rpc_task * 8700 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred, 8701 u32 sp4_how, struct rpc_xprt *xprt) 8702 { 8703 struct rpc_message msg = { 8704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], 8705 .rpc_cred = cred, 8706 }; 8707 struct rpc_task_setup task_setup_data = { 8708 .rpc_client = clp->cl_rpcclient, 8709 .callback_ops = &nfs4_exchange_id_call_ops, 8710 .rpc_message = &msg, 8711 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, 8712 }; 8713 struct nfs41_exchange_id_data *calldata; 8714 int status; 8715 8716 if (!refcount_inc_not_zero(&clp->cl_count)) 8717 return ERR_PTR(-EIO); 8718 8719 status = -ENOMEM; 8720 calldata = kzalloc_obj(*calldata, GFP_NOFS); 8721 if (!calldata) 8722 goto out; 8723 8724 nfs4_init_boot_verifier(clp, &calldata->args.verifier); 8725 8726 status = nfs4_init_uniform_client_string(clp); 8727 if (status) 8728 goto out_calldata; 8729 8730 calldata->res.server_owner = kzalloc_obj(struct nfs41_server_owner, 8731 GFP_NOFS); 8732 status = -ENOMEM; 8733 if (unlikely(calldata->res.server_owner == NULL)) 8734 goto out_calldata; 8735 8736 calldata->res.server_scope = kzalloc_obj(struct nfs41_server_scope, 8737 GFP_NOFS); 8738 if (unlikely(calldata->res.server_scope == NULL)) 8739 goto out_server_owner; 8740 8741 calldata->res.impl_id = kzalloc_obj(struct nfs41_impl_id, GFP_NOFS); 8742 if (unlikely(calldata->res.impl_id == NULL)) 8743 goto out_server_scope; 8744 8745 switch (sp4_how) { 8746 case SP4_NONE: 8747 calldata->args.state_protect.how = SP4_NONE; 8748 break; 8749 8750 case SP4_MACH_CRED: 8751 calldata->args.state_protect = nfs4_sp4_mach_cred_request; 8752 break; 8753 8754 default: 8755 /* unsupported! */ 8756 WARN_ON_ONCE(1); 8757 status = -EINVAL; 8758 goto out_impl_id; 8759 } 8760 if (xprt) { 8761 task_setup_data.rpc_xprt = xprt; 8762 task_setup_data.flags |= RPC_TASK_SOFTCONN; 8763 memcpy(calldata->args.verifier.data, clp->cl_confirm.data, 8764 sizeof(calldata->args.verifier.data)); 8765 } 8766 calldata->args.client = clp; 8767 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | 8768 EXCHGID4_FLAG_BIND_PRINC_STATEID; 8769 #ifdef CONFIG_NFS_V4_1_MIGRATION 8770 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR; 8771 #endif 8772 if (test_bit(NFS_CS_PNFS, &clp->cl_flags)) 8773 calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS; 8774 msg.rpc_argp = &calldata->args; 8775 msg.rpc_resp = &calldata->res; 8776 task_setup_data.callback_data = calldata; 8777 8778 return rpc_run_task(&task_setup_data); 8779 8780 out_impl_id: 8781 kfree(calldata->res.impl_id); 8782 out_server_scope: 8783 kfree(calldata->res.server_scope); 8784 out_server_owner: 8785 kfree(calldata->res.server_owner); 8786 out_calldata: 8787 kfree(calldata); 8788 out: 8789 nfs_put_client(clp); 8790 return ERR_PTR(status); 8791 } 8792 8793 /* 8794 * _nfs4_proc_exchange_id() 8795 * 8796 * Wrapper for EXCHANGE_ID operation. 8797 */ 8798 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred, 8799 u32 sp4_how) 8800 { 8801 struct rpc_task *task; 8802 struct nfs41_exchange_id_args *argp; 8803 struct nfs41_exchange_id_res *resp; 8804 unsigned long now = jiffies; 8805 int status; 8806 8807 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL); 8808 if (IS_ERR(task)) 8809 return PTR_ERR(task); 8810 8811 argp = task->tk_msg.rpc_argp; 8812 resp = task->tk_msg.rpc_resp; 8813 status = task->tk_status; 8814 if (status != 0) 8815 goto out; 8816 8817 status = nfs4_check_cl_exchange_flags(resp->flags, 8818 clp->cl_mvops->minor_version); 8819 if (status != 0) 8820 goto out; 8821 8822 status = nfs4_sp4_select_mode(clp, &resp->state_protect); 8823 if (status != 0) 8824 goto out; 8825 8826 do_renew_lease(clp, now); 8827 8828 clp->cl_clientid = resp->clientid; 8829 clp->cl_exchange_flags = resp->flags; 8830 clp->cl_seqid = resp->seqid; 8831 /* Client ID is not confirmed */ 8832 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R)) 8833 clear_bit(NFS4_SESSION_ESTABLISHED, 8834 &clp->cl_session->session_state); 8835 8836 if (clp->cl_serverscope != NULL && 8837 !nfs41_same_server_scope(clp->cl_serverscope, 8838 resp->server_scope)) { 8839 dprintk("%s: server_scope mismatch detected\n", 8840 __func__); 8841 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); 8842 } 8843 8844 swap(clp->cl_serverowner, resp->server_owner); 8845 swap(clp->cl_serverscope, resp->server_scope); 8846 swap(clp->cl_implid, resp->impl_id); 8847 8848 /* Save the EXCHANGE_ID verifier session trunk tests */ 8849 memcpy(clp->cl_confirm.data, argp->verifier.data, 8850 sizeof(clp->cl_confirm.data)); 8851 out: 8852 trace_nfs4_exchange_id(clp, status); 8853 rpc_put_task(task); 8854 return status; 8855 } 8856 8857 /* 8858 * nfs4_proc_exchange_id() 8859 * 8860 * Returns zero, a negative errno, or a negative NFS4ERR status code. 8861 * 8862 * Since the clientid has expired, all compounds using sessions 8863 * associated with the stale clientid will be returning 8864 * NFS4ERR_BADSESSION in the sequence operation, and will therefore 8865 * be in some phase of session reset. 8866 * 8867 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used. 8868 */ 8869 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred) 8870 { 8871 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor; 8872 int status; 8873 8874 /* try SP4_MACH_CRED if krb5i/p */ 8875 if (authflavor == RPC_AUTH_GSS_KRB5I || 8876 authflavor == RPC_AUTH_GSS_KRB5P) { 8877 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED); 8878 if (!status) 8879 return 0; 8880 } 8881 8882 /* try SP4_NONE */ 8883 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE); 8884 } 8885 8886 /** 8887 * nfs4_test_session_trunk 8888 * 8889 * This is an add_xprt_test() test function called from 8890 * rpc_clnt_setup_test_and_add_xprt. 8891 * 8892 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt 8893 * and is dereferrenced in nfs4_exchange_id_release 8894 * 8895 * Upon success, add the new transport to the rpc_clnt 8896 * 8897 * @clnt: struct rpc_clnt to get new transport 8898 * @xprt: the rpc_xprt to test 8899 * @data: call data for _nfs4_proc_exchange_id. 8900 */ 8901 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt, 8902 void *data) 8903 { 8904 struct nfs4_add_xprt_data *adata = data; 8905 struct rpc_task *task; 8906 int status; 8907 8908 u32 sp4_how; 8909 8910 dprintk("--> %s try %s\n", __func__, 8911 xprt->address_strings[RPC_DISPLAY_ADDR]); 8912 8913 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED); 8914 8915 try_again: 8916 /* Test connection for session trunking. Async exchange_id call */ 8917 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt); 8918 if (IS_ERR(task)) 8919 return; 8920 8921 status = task->tk_status; 8922 if (status == 0) { 8923 status = nfs4_detect_session_trunking(adata->clp, 8924 task->tk_msg.rpc_resp, xprt); 8925 trace_nfs4_trunked_exchange_id(adata->clp, 8926 xprt->address_strings[RPC_DISPLAY_ADDR], status); 8927 } 8928 if (status == 0) 8929 rpc_clnt_xprt_switch_add_xprt(clnt, xprt); 8930 else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt, 8931 (struct sockaddr *)&xprt->addr)) 8932 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt); 8933 8934 rpc_put_task(task); 8935 if (status == -NFS4ERR_DELAY) { 8936 ssleep(1); 8937 goto try_again; 8938 } 8939 } 8940 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk); 8941 8942 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, 8943 const struct cred *cred) 8944 { 8945 struct rpc_message msg = { 8946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], 8947 .rpc_argp = clp, 8948 .rpc_cred = cred, 8949 }; 8950 int status; 8951 8952 status = rpc_call_sync(clp->cl_rpcclient, &msg, 8953 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 8954 trace_nfs4_destroy_clientid(clp, status); 8955 if (status) 8956 dprintk("NFS: Got error %d from the server %s on " 8957 "DESTROY_CLIENTID.", status, clp->cl_hostname); 8958 return status; 8959 } 8960 8961 static int nfs4_proc_destroy_clientid(struct nfs_client *clp, 8962 const struct cred *cred) 8963 { 8964 unsigned int loop; 8965 int ret; 8966 8967 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { 8968 ret = _nfs4_proc_destroy_clientid(clp, cred); 8969 switch (ret) { 8970 case -NFS4ERR_DELAY: 8971 case -NFS4ERR_CLIENTID_BUSY: 8972 ssleep(1); 8973 break; 8974 default: 8975 return ret; 8976 } 8977 } 8978 return 0; 8979 } 8980 8981 int nfs4_destroy_clientid(struct nfs_client *clp) 8982 { 8983 const struct cred *cred; 8984 int ret = 0; 8985 8986 if (clp->cl_mvops->minor_version < 1) 8987 goto out; 8988 if (clp->cl_exchange_flags == 0) 8989 goto out; 8990 if (clp->cl_preserve_clid) 8991 goto out; 8992 cred = nfs4_get_clid_cred(clp); 8993 ret = nfs4_proc_destroy_clientid(clp, cred); 8994 put_cred(cred); 8995 switch (ret) { 8996 case 0: 8997 case -NFS4ERR_STALE_CLIENTID: 8998 clp->cl_exchange_flags = 0; 8999 } 9000 out: 9001 return ret; 9002 } 9003 9004 struct nfs4_get_lease_time_data { 9005 struct nfs4_get_lease_time_args *args; 9006 struct nfs4_get_lease_time_res *res; 9007 struct nfs_client *clp; 9008 }; 9009 9010 static void nfs4_get_lease_time_prepare(struct rpc_task *task, 9011 void *calldata) 9012 { 9013 struct nfs4_get_lease_time_data *data = 9014 (struct nfs4_get_lease_time_data *)calldata; 9015 9016 /* just setup sequence, do not trigger session recovery 9017 since we're invoked within one */ 9018 nfs4_setup_sequence(data->clp, 9019 &data->args->la_seq_args, 9020 &data->res->lr_seq_res, 9021 task); 9022 } 9023 9024 /* 9025 * Called from nfs4_state_manager thread for session setup, so don't recover 9026 * from sequence operation or clientid errors. 9027 */ 9028 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) 9029 { 9030 struct nfs4_get_lease_time_data *data = 9031 (struct nfs4_get_lease_time_data *)calldata; 9032 9033 if (!nfs4_sequence_done(task, &data->res->lr_seq_res)) 9034 return; 9035 switch (task->tk_status) { 9036 case -NFS4ERR_DELAY: 9037 case -NFS4ERR_GRACE: 9038 rpc_delay(task, NFS4_POLL_RETRY_MIN); 9039 task->tk_status = 0; 9040 fallthrough; 9041 case -NFS4ERR_RETRY_UNCACHED_REP: 9042 rpc_restart_call_prepare(task); 9043 return; 9044 } 9045 } 9046 9047 static const struct rpc_call_ops nfs4_get_lease_time_ops = { 9048 .rpc_call_prepare = nfs4_get_lease_time_prepare, 9049 .rpc_call_done = nfs4_get_lease_time_done, 9050 }; 9051 9052 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) 9053 { 9054 struct nfs4_get_lease_time_args args; 9055 struct nfs4_get_lease_time_res res = { 9056 .lr_fsinfo = fsinfo, 9057 }; 9058 struct nfs4_get_lease_time_data data = { 9059 .args = &args, 9060 .res = &res, 9061 .clp = clp, 9062 }; 9063 struct rpc_message msg = { 9064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], 9065 .rpc_argp = &args, 9066 .rpc_resp = &res, 9067 }; 9068 struct rpc_task_setup task_setup = { 9069 .rpc_client = clp->cl_rpcclient, 9070 .rpc_message = &msg, 9071 .callback_ops = &nfs4_get_lease_time_ops, 9072 .callback_data = &data, 9073 .flags = RPC_TASK_TIMEOUT, 9074 }; 9075 9076 nfs4_init_sequence(clp, &args.la_seq_args, &res.lr_seq_res, 0, 1); 9077 return nfs4_call_sync_custom(&task_setup); 9078 } 9079 9080 /* 9081 * Initialize the values to be used by the client in CREATE_SESSION 9082 * If nfs4_init_session set the fore channel request and response sizes, 9083 * use them. 9084 * 9085 * Set the back channel max_resp_sz_cached to zero to force the client to 9086 * always set csa_cachethis to FALSE because the current implementation 9087 * of the back channel DRC only supports caching the CB_SEQUENCE operation. 9088 */ 9089 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args, 9090 struct rpc_clnt *clnt) 9091 { 9092 unsigned int max_rqst_sz, max_resp_sz; 9093 unsigned int max_bc_payload = rpc_max_bc_payload(clnt); 9094 unsigned int max_bc_slots = rpc_num_bc_slots(clnt); 9095 9096 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead; 9097 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead; 9098 9099 /* Fore channel attributes */ 9100 args->fc_attrs.max_rqst_sz = max_rqst_sz; 9101 args->fc_attrs.max_resp_sz = max_resp_sz; 9102 args->fc_attrs.max_ops = NFS4_MAX_OPS; 9103 args->fc_attrs.max_reqs = max_session_slots; 9104 9105 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " 9106 "max_ops=%u max_reqs=%u\n", 9107 __func__, 9108 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, 9109 args->fc_attrs.max_ops, args->fc_attrs.max_reqs); 9110 9111 /* Back channel attributes */ 9112 args->bc_attrs.max_rqst_sz = max_bc_payload; 9113 args->bc_attrs.max_resp_sz = max_bc_payload; 9114 args->bc_attrs.max_resp_sz_cached = 0; 9115 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; 9116 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1); 9117 if (args->bc_attrs.max_reqs > max_bc_slots) 9118 args->bc_attrs.max_reqs = max_bc_slots; 9119 9120 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " 9121 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", 9122 __func__, 9123 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, 9124 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, 9125 args->bc_attrs.max_reqs); 9126 } 9127 9128 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, 9129 struct nfs41_create_session_res *res) 9130 { 9131 struct nfs4_channel_attrs *sent = &args->fc_attrs; 9132 struct nfs4_channel_attrs *rcvd = &res->fc_attrs; 9133 9134 if (rcvd->max_resp_sz > sent->max_resp_sz) 9135 return -EINVAL; 9136 /* 9137 * Our requested max_ops is the minimum we need; we're not 9138 * prepared to break up compounds into smaller pieces than that. 9139 * So, no point even trying to continue if the server won't 9140 * cooperate: 9141 */ 9142 if (rcvd->max_ops < sent->max_ops) 9143 return -EINVAL; 9144 if (rcvd->max_reqs == 0) 9145 return -EINVAL; 9146 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) 9147 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; 9148 return 0; 9149 } 9150 9151 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, 9152 struct nfs41_create_session_res *res) 9153 { 9154 struct nfs4_channel_attrs *sent = &args->bc_attrs; 9155 struct nfs4_channel_attrs *rcvd = &res->bc_attrs; 9156 9157 if (!(res->flags & SESSION4_BACK_CHAN)) 9158 goto out; 9159 if (rcvd->max_rqst_sz > sent->max_rqst_sz) 9160 return -EINVAL; 9161 if (rcvd->max_resp_sz > sent->max_resp_sz) 9162 return -EINVAL; 9163 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) 9164 return -EINVAL; 9165 if (rcvd->max_ops > sent->max_ops) 9166 return -EINVAL; 9167 if (rcvd->max_reqs > sent->max_reqs) 9168 return -EINVAL; 9169 out: 9170 return 0; 9171 } 9172 9173 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, 9174 struct nfs41_create_session_res *res) 9175 { 9176 int ret; 9177 9178 ret = nfs4_verify_fore_channel_attrs(args, res); 9179 if (ret) 9180 return ret; 9181 return nfs4_verify_back_channel_attrs(args, res); 9182 } 9183 9184 static void nfs4_update_session(struct nfs4_session *session, 9185 struct nfs41_create_session_res *res) 9186 { 9187 nfs4_copy_sessionid(&session->sess_id, &res->sessionid); 9188 /* Mark client id and session as being confirmed */ 9189 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 9190 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state); 9191 session->flags = res->flags; 9192 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs)); 9193 if (res->flags & SESSION4_BACK_CHAN) 9194 memcpy(&session->bc_attrs, &res->bc_attrs, 9195 sizeof(session->bc_attrs)); 9196 } 9197 9198 static int _nfs4_proc_create_session(struct nfs_client *clp, 9199 const struct cred *cred) 9200 { 9201 struct nfs4_session *session = clp->cl_session; 9202 struct nfs41_create_session_args args = { 9203 .client = clp, 9204 .clientid = clp->cl_clientid, 9205 .seqid = clp->cl_seqid, 9206 .cb_program = NFS4_CALLBACK, 9207 }; 9208 struct nfs41_create_session_res res; 9209 9210 struct rpc_message msg = { 9211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], 9212 .rpc_argp = &args, 9213 .rpc_resp = &res, 9214 .rpc_cred = cred, 9215 }; 9216 int status; 9217 9218 nfs4_init_channel_attrs(&args, clp->cl_rpcclient); 9219 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); 9220 9221 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 9222 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 9223 trace_nfs4_create_session(clp, status); 9224 9225 switch (status) { 9226 case -NFS4ERR_STALE_CLIENTID: 9227 case -NFS4ERR_DELAY: 9228 case -ETIMEDOUT: 9229 case -EACCES: 9230 case -EAGAIN: 9231 goto out; 9232 } 9233 9234 clp->cl_seqid++; 9235 if (!status) { 9236 /* Verify the session's negotiated channel_attrs values */ 9237 status = nfs4_verify_channel_attrs(&args, &res); 9238 /* Increment the clientid slot sequence id */ 9239 if (status) 9240 goto out; 9241 nfs4_update_session(session, &res); 9242 } 9243 out: 9244 return status; 9245 } 9246 9247 /* 9248 * Issues a CREATE_SESSION operation to the server. 9249 * It is the responsibility of the caller to verify the session is 9250 * expired before calling this routine. 9251 */ 9252 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred) 9253 { 9254 int status; 9255 unsigned *ptr; 9256 struct nfs4_session *session = clp->cl_session; 9257 struct nfs4_add_xprt_data xprtdata = { 9258 .clp = clp, 9259 }; 9260 struct rpc_add_xprt_test rpcdata = { 9261 .add_xprt_test = clp->cl_mvops->session_trunk, 9262 .data = &xprtdata, 9263 }; 9264 9265 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); 9266 9267 status = _nfs4_proc_create_session(clp, cred); 9268 if (status) 9269 goto out; 9270 9271 /* Init or reset the session slot tables */ 9272 status = nfs4_setup_session_slot_tables(session); 9273 dprintk("slot table setup returned %d\n", status); 9274 if (status) 9275 goto out; 9276 9277 ptr = (unsigned *)&session->sess_id.data[0]; 9278 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, 9279 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); 9280 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata); 9281 out: 9282 return status; 9283 } 9284 9285 /* 9286 * Issue the over-the-wire RPC DESTROY_SESSION. 9287 * The caller must serialize access to this routine. 9288 */ 9289 int nfs4_proc_destroy_session(struct nfs4_session *session, 9290 const struct cred *cred) 9291 { 9292 struct rpc_message msg = { 9293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], 9294 .rpc_argp = session, 9295 .rpc_cred = cred, 9296 }; 9297 int status = 0; 9298 9299 /* session is still being setup */ 9300 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state)) 9301 return 0; 9302 9303 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 9304 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); 9305 trace_nfs4_destroy_session(session->clp, status); 9306 9307 if (status) 9308 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " 9309 "Session has been destroyed regardless...\n", status); 9310 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient); 9311 return status; 9312 } 9313 9314 /* 9315 * Renew the cl_session lease. 9316 */ 9317 struct nfs4_sequence_data { 9318 struct nfs_client *clp; 9319 struct nfs4_sequence_args args; 9320 struct nfs4_sequence_res res; 9321 }; 9322 9323 static void nfs41_sequence_release(void *data) 9324 { 9325 struct nfs4_sequence_data *calldata = data; 9326 struct nfs_client *clp = calldata->clp; 9327 9328 if (refcount_read(&clp->cl_count) > 1) 9329 nfs4_schedule_state_renewal(clp); 9330 nfs_put_client(clp); 9331 kfree(calldata); 9332 } 9333 9334 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) 9335 { 9336 switch(task->tk_status) { 9337 case -NFS4ERR_DELAY: 9338 rpc_delay(task, NFS4_POLL_RETRY_MAX); 9339 return -EAGAIN; 9340 default: 9341 nfs4_schedule_lease_recovery(clp); 9342 } 9343 return 0; 9344 } 9345 9346 static void nfs41_sequence_call_done(struct rpc_task *task, void *data) 9347 { 9348 struct nfs4_sequence_data *calldata = data; 9349 struct nfs_client *clp = calldata->clp; 9350 9351 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) 9352 return; 9353 9354 trace_nfs4_sequence(clp, task->tk_status); 9355 if (task->tk_status < 0 && clp->cl_cons_state >= 0) { 9356 dprintk("%s ERROR %d\n", __func__, task->tk_status); 9357 if (refcount_read(&clp->cl_count) == 1) 9358 return; 9359 9360 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { 9361 rpc_restart_call_prepare(task); 9362 return; 9363 } 9364 } 9365 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); 9366 } 9367 9368 static void nfs41_sequence_prepare(struct rpc_task *task, void *data) 9369 { 9370 struct nfs4_sequence_data *calldata = data; 9371 struct nfs_client *clp = calldata->clp; 9372 struct nfs4_sequence_args *args; 9373 struct nfs4_sequence_res *res; 9374 9375 args = task->tk_msg.rpc_argp; 9376 res = task->tk_msg.rpc_resp; 9377 9378 nfs4_setup_sequence(clp, args, res, task); 9379 } 9380 9381 static const struct rpc_call_ops nfs41_sequence_ops = { 9382 .rpc_call_done = nfs41_sequence_call_done, 9383 .rpc_call_prepare = nfs41_sequence_prepare, 9384 .rpc_release = nfs41_sequence_release, 9385 }; 9386 9387 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, 9388 const struct cred *cred, 9389 struct nfs4_slot *slot, 9390 bool is_privileged) 9391 { 9392 struct nfs4_sequence_data *calldata; 9393 struct rpc_message msg = { 9394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], 9395 .rpc_cred = cred, 9396 }; 9397 struct rpc_task_setup task_setup_data = { 9398 .rpc_client = clp->cl_rpcclient, 9399 .rpc_message = &msg, 9400 .callback_ops = &nfs41_sequence_ops, 9401 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE, 9402 }; 9403 struct rpc_task *ret; 9404 9405 ret = ERR_PTR(-EIO); 9406 if (!refcount_inc_not_zero(&clp->cl_count)) 9407 goto out_err; 9408 9409 ret = ERR_PTR(-ENOMEM); 9410 calldata = kzalloc_obj(*calldata); 9411 if (calldata == NULL) 9412 goto out_put_clp; 9413 nfs4_init_sequence(clp, &calldata->args, &calldata->res, 0, is_privileged); 9414 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot); 9415 msg.rpc_argp = &calldata->args; 9416 msg.rpc_resp = &calldata->res; 9417 calldata->clp = clp; 9418 task_setup_data.callback_data = calldata; 9419 9420 ret = rpc_run_task(&task_setup_data); 9421 if (IS_ERR(ret)) 9422 goto out_err; 9423 return ret; 9424 out_put_clp: 9425 nfs_put_client(clp); 9426 out_err: 9427 nfs41_release_slot(slot); 9428 return ret; 9429 } 9430 9431 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) 9432 { 9433 struct rpc_task *task; 9434 int ret = 0; 9435 9436 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) 9437 return -EAGAIN; 9438 task = _nfs41_proc_sequence(clp, cred, NULL, false); 9439 if (IS_ERR(task)) 9440 ret = PTR_ERR(task); 9441 else 9442 rpc_put_task_async(task); 9443 dprintk("<-- %s status=%d\n", __func__, ret); 9444 return ret; 9445 } 9446 9447 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred) 9448 { 9449 struct rpc_task *task; 9450 int ret; 9451 9452 task = _nfs41_proc_sequence(clp, cred, NULL, true); 9453 if (IS_ERR(task)) { 9454 ret = PTR_ERR(task); 9455 goto out; 9456 } 9457 ret = rpc_wait_for_completion_task(task); 9458 if (!ret) 9459 ret = task->tk_status; 9460 rpc_put_task(task); 9461 out: 9462 dprintk("<-- %s status=%d\n", __func__, ret); 9463 return ret; 9464 } 9465 9466 struct nfs4_reclaim_complete_data { 9467 struct nfs_client *clp; 9468 struct nfs41_reclaim_complete_args arg; 9469 struct nfs41_reclaim_complete_res res; 9470 }; 9471 9472 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) 9473 { 9474 struct nfs4_reclaim_complete_data *calldata = data; 9475 9476 nfs4_setup_sequence(calldata->clp, 9477 &calldata->arg.seq_args, 9478 &calldata->res.seq_res, 9479 task); 9480 } 9481 9482 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) 9483 { 9484 switch(task->tk_status) { 9485 case 0: 9486 wake_up_all(&clp->cl_lock_waitq); 9487 fallthrough; 9488 case -NFS4ERR_COMPLETE_ALREADY: 9489 case -NFS4ERR_WRONG_CRED: /* What to do here? */ 9490 break; 9491 case -NFS4ERR_DELAY: 9492 rpc_delay(task, NFS4_POLL_RETRY_MAX); 9493 fallthrough; 9494 case -NFS4ERR_RETRY_UNCACHED_REP: 9495 case -EACCES: 9496 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n", 9497 __func__, task->tk_status, clp->cl_hostname); 9498 return -EAGAIN; 9499 case -NFS4ERR_BADSESSION: 9500 case -NFS4ERR_DEADSESSION: 9501 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 9502 break; 9503 default: 9504 nfs4_schedule_lease_recovery(clp); 9505 } 9506 return 0; 9507 } 9508 9509 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) 9510 { 9511 struct nfs4_reclaim_complete_data *calldata = data; 9512 struct nfs_client *clp = calldata->clp; 9513 struct nfs4_sequence_res *res = &calldata->res.seq_res; 9514 9515 if (!nfs41_sequence_done(task, res)) 9516 return; 9517 9518 trace_nfs4_reclaim_complete(clp, task->tk_status); 9519 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { 9520 rpc_restart_call_prepare(task); 9521 return; 9522 } 9523 } 9524 9525 static void nfs4_free_reclaim_complete_data(void *data) 9526 { 9527 struct nfs4_reclaim_complete_data *calldata = data; 9528 9529 kfree(calldata); 9530 } 9531 9532 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { 9533 .rpc_call_prepare = nfs4_reclaim_complete_prepare, 9534 .rpc_call_done = nfs4_reclaim_complete_done, 9535 .rpc_release = nfs4_free_reclaim_complete_data, 9536 }; 9537 9538 /* 9539 * Issue a global reclaim complete. 9540 */ 9541 static int nfs41_proc_reclaim_complete(struct nfs_client *clp, 9542 const struct cred *cred) 9543 { 9544 struct nfs4_reclaim_complete_data *calldata; 9545 struct rpc_message msg = { 9546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], 9547 .rpc_cred = cred, 9548 }; 9549 struct rpc_task_setup task_setup_data = { 9550 .rpc_client = clp->cl_rpcclient, 9551 .rpc_message = &msg, 9552 .callback_ops = &nfs4_reclaim_complete_call_ops, 9553 .flags = RPC_TASK_NO_ROUND_ROBIN, 9554 }; 9555 int status = -ENOMEM; 9556 9557 calldata = kzalloc_obj(*calldata, GFP_NOFS); 9558 if (calldata == NULL) 9559 goto out; 9560 calldata->clp = clp; 9561 calldata->arg.one_fs = 0; 9562 9563 nfs4_init_sequence(clp, &calldata->arg.seq_args, &calldata->res.seq_res, 0, 1); 9564 msg.rpc_argp = &calldata->arg; 9565 msg.rpc_resp = &calldata->res; 9566 task_setup_data.callback_data = calldata; 9567 status = nfs4_call_sync_custom(&task_setup_data); 9568 out: 9569 dprintk("<-- %s status=%d\n", __func__, status); 9570 return status; 9571 } 9572 9573 static void 9574 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) 9575 { 9576 struct nfs4_layoutget *lgp = calldata; 9577 struct nfs_server *server = NFS_SERVER(lgp->args.inode); 9578 9579 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args, 9580 &lgp->res.seq_res, task); 9581 } 9582 9583 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) 9584 { 9585 struct nfs4_layoutget *lgp = calldata; 9586 9587 nfs41_sequence_process(task, &lgp->res.seq_res); 9588 } 9589 9590 static int 9591 nfs4_layoutget_handle_exception(struct rpc_task *task, 9592 struct nfs4_layoutget *lgp, struct nfs4_exception *exception) 9593 { 9594 struct inode *inode = lgp->args.inode; 9595 struct nfs_server *server = NFS_SERVER(inode); 9596 struct pnfs_layout_hdr *lo = lgp->lo; 9597 int nfs4err = task->tk_status; 9598 int err, status = 0; 9599 LIST_HEAD(head); 9600 9601 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status); 9602 9603 nfs4_sequence_free_slot(&lgp->res.seq_res); 9604 9605 exception->state = NULL; 9606 exception->stateid = NULL; 9607 9608 switch (nfs4err) { 9609 case 0: 9610 goto out; 9611 9612 /* 9613 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs 9614 * on the file. set tk_status to -ENODATA to tell upper layer to 9615 * retry go inband. 9616 */ 9617 case -NFS4ERR_LAYOUTUNAVAILABLE: 9618 status = -ENODATA; 9619 goto out; 9620 /* 9621 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of 9622 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3). 9623 */ 9624 case -NFS4ERR_BADLAYOUT: 9625 status = -EOVERFLOW; 9626 goto out; 9627 /* 9628 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client 9629 * (or clients) writing to the same RAID stripe except when 9630 * the minlength argument is 0 (see RFC5661 section 18.43.3). 9631 * 9632 * Treat it like we would RECALLCONFLICT -- we retry for a little 9633 * while, and then eventually give up. 9634 */ 9635 case -NFS4ERR_LAYOUTTRYLATER: 9636 if (lgp->args.minlength == 0) { 9637 status = -EOVERFLOW; 9638 goto out; 9639 } 9640 status = -EBUSY; 9641 break; 9642 case -NFS4ERR_RECALLCONFLICT: 9643 case -NFS4ERR_RETURNCONFLICT: 9644 status = -ERECALLCONFLICT; 9645 break; 9646 case -NFS4ERR_DELEG_REVOKED: 9647 case -NFS4ERR_ADMIN_REVOKED: 9648 case -NFS4ERR_EXPIRED: 9649 case -NFS4ERR_BAD_STATEID: 9650 exception->timeout = 0; 9651 spin_lock(&inode->i_lock); 9652 /* If the open stateid was bad, then recover it. */ 9653 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || 9654 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) { 9655 spin_unlock(&inode->i_lock); 9656 exception->state = lgp->args.ctx->state; 9657 exception->stateid = &lgp->args.stateid; 9658 break; 9659 } 9660 9661 /* 9662 * Mark the bad layout state as invalid, then retry 9663 */ 9664 pnfs_mark_layout_stateid_invalid(lo, &head); 9665 spin_unlock(&inode->i_lock); 9666 nfs_commit_inode(inode, 0); 9667 pnfs_free_lseg_list(&head); 9668 status = -EAGAIN; 9669 goto out; 9670 } 9671 9672 err = nfs4_handle_exception(server, nfs4err, exception); 9673 if (!status) { 9674 if (exception->retry) 9675 status = -EAGAIN; 9676 else 9677 status = err; 9678 } 9679 out: 9680 return status; 9681 } 9682 9683 size_t max_response_pages(struct nfs_server *server) 9684 { 9685 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; 9686 return nfs_page_array_len(0, max_resp_sz); 9687 } 9688 9689 static void nfs4_layoutget_release(void *calldata) 9690 { 9691 struct nfs4_layoutget *lgp = calldata; 9692 9693 nfs4_sequence_free_slot(&lgp->res.seq_res); 9694 pnfs_layoutget_free(lgp); 9695 } 9696 9697 static const struct rpc_call_ops nfs4_layoutget_call_ops = { 9698 .rpc_call_prepare = nfs4_layoutget_prepare, 9699 .rpc_call_done = nfs4_layoutget_done, 9700 .rpc_release = nfs4_layoutget_release, 9701 }; 9702 9703 struct pnfs_layout_segment * 9704 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, 9705 struct nfs4_exception *exception) 9706 { 9707 struct inode *inode = lgp->args.inode; 9708 struct nfs_server *server = NFS_SERVER(inode); 9709 struct rpc_task *task; 9710 struct rpc_message msg = { 9711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], 9712 .rpc_argp = &lgp->args, 9713 .rpc_resp = &lgp->res, 9714 .rpc_cred = lgp->cred, 9715 }; 9716 struct rpc_task_setup task_setup_data = { 9717 .rpc_client = server->client, 9718 .rpc_message = &msg, 9719 .callback_ops = &nfs4_layoutget_call_ops, 9720 .callback_data = lgp, 9721 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF | 9722 RPC_TASK_MOVEABLE, 9723 }; 9724 struct pnfs_layout_segment *lseg = NULL; 9725 int status = 0; 9726 9727 nfs4_init_sequence(server->nfs_client, &lgp->args.seq_args, 9728 &lgp->res.seq_res, 0, 0); 9729 exception->retry = 0; 9730 9731 task = rpc_run_task(&task_setup_data); 9732 if (IS_ERR(task)) 9733 return ERR_CAST(task); 9734 9735 status = rpc_wait_for_completion_task(task); 9736 if (status != 0) 9737 goto out; 9738 9739 if (task->tk_status < 0) { 9740 exception->retry = 1; 9741 status = nfs4_layoutget_handle_exception(task, lgp, exception); 9742 } else if (lgp->res.layoutp->len == 0) { 9743 exception->retry = 1; 9744 status = -EAGAIN; 9745 nfs4_update_delay(&exception->timeout); 9746 } else 9747 lseg = pnfs_layout_process(lgp); 9748 out: 9749 trace_nfs4_layoutget(lgp->args.ctx, 9750 &lgp->args.range, 9751 &lgp->res.range, 9752 &lgp->res.stateid, 9753 status); 9754 9755 rpc_put_task(task); 9756 dprintk("<-- %s status=%d\n", __func__, status); 9757 if (status) 9758 return ERR_PTR(status); 9759 return lseg; 9760 } 9761 9762 static void 9763 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) 9764 { 9765 struct nfs4_layoutreturn *lrp = calldata; 9766 9767 nfs4_setup_sequence(lrp->clp, 9768 &lrp->args.seq_args, 9769 &lrp->res.seq_res, 9770 task); 9771 if (!pnfs_layout_is_valid(lrp->args.layout)) 9772 rpc_exit(task, 0); 9773 } 9774 9775 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) 9776 { 9777 struct nfs4_layoutreturn *lrp = calldata; 9778 struct nfs_server *server; 9779 9780 if (!nfs41_sequence_process(task, &lrp->res.seq_res)) 9781 return; 9782 9783 if (task->tk_rpc_status < 0) { 9784 switch (task->tk_rpc_status) { 9785 case -EACCES: 9786 case -EIO: 9787 case -EKEYEXPIRED: 9788 case -ERESTARTSYS: 9789 case -EINTR: 9790 lrp->rpc_status = 0; 9791 break; 9792 case -ENETDOWN: 9793 case -ENETUNREACH: 9794 if (task->tk_flags & RPC_TASK_NETUNREACH_FATAL) 9795 lrp->rpc_status = 0; 9796 else 9797 lrp->rpc_status = -EAGAIN; 9798 break; 9799 default: 9800 lrp->rpc_status = -EAGAIN; 9801 break; 9802 } 9803 lrp->res.lrs_present = 0; 9804 return; 9805 } 9806 9807 server = NFS_SERVER(lrp->args.inode); 9808 switch (task->tk_status) { 9809 case -NFS4ERR_OLD_STATEID: 9810 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid, 9811 &lrp->args.range, 9812 lrp->args.inode)) 9813 goto out_restart; 9814 fallthrough; 9815 default: 9816 task->tk_status = 0; 9817 lrp->res.lrs_present = 0; 9818 fallthrough; 9819 case 0: 9820 break; 9821 case -NFS4ERR_BADSESSION: 9822 case -NFS4ERR_DEADSESSION: 9823 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 9824 nfs4_schedule_session_recovery(server->nfs_client->cl_session, 9825 task->tk_status); 9826 lrp->res.lrs_present = 0; 9827 lrp->rpc_status = -EAGAIN; 9828 task->tk_status = 0; 9829 break; 9830 case -NFS4ERR_DELAY: 9831 if (nfs4_async_handle_error(task, server, NULL, NULL) == 9832 -EAGAIN) 9833 goto out_restart; 9834 lrp->res.lrs_present = 0; 9835 break; 9836 } 9837 return; 9838 out_restart: 9839 task->tk_status = 0; 9840 nfs4_sequence_free_slot(&lrp->res.seq_res); 9841 rpc_restart_call_prepare(task); 9842 } 9843 9844 static void nfs4_layoutreturn_release(void *calldata) 9845 { 9846 struct nfs4_layoutreturn *lrp = calldata; 9847 struct pnfs_layout_hdr *lo = lrp->args.layout; 9848 9849 if (lrp->rpc_status == 0 || !lrp->inode) 9850 pnfs_layoutreturn_free_lsegs( 9851 lo, &lrp->args.stateid, &lrp->args.range, 9852 lrp->res.lrs_present ? &lrp->res.stateid : NULL); 9853 else 9854 pnfs_layoutreturn_retry_later(lo, &lrp->args.stateid, 9855 &lrp->args.range); 9856 nfs4_sequence_free_slot(&lrp->res.seq_res); 9857 if (lrp->ld_private.ops && lrp->ld_private.ops->free) 9858 lrp->ld_private.ops->free(&lrp->ld_private); 9859 pnfs_put_layout_hdr(lrp->args.layout); 9860 nfs_iput_and_deactive(lrp->inode); 9861 put_cred(lrp->cred); 9862 kfree(calldata); 9863 } 9864 9865 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { 9866 .rpc_call_prepare = nfs4_layoutreturn_prepare, 9867 .rpc_call_done = nfs4_layoutreturn_done, 9868 .rpc_release = nfs4_layoutreturn_release, 9869 }; 9870 9871 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags) 9872 { 9873 struct nfs_client *clp = NFS_SERVER(lrp->args.inode)->nfs_client; 9874 struct rpc_task *task; 9875 struct rpc_message msg = { 9876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], 9877 .rpc_argp = &lrp->args, 9878 .rpc_resp = &lrp->res, 9879 .rpc_cred = lrp->cred, 9880 }; 9881 struct rpc_task_setup task_setup_data = { 9882 .rpc_client = NFS_SERVER(lrp->args.inode)->client, 9883 .rpc_message = &msg, 9884 .callback_ops = &nfs4_layoutreturn_call_ops, 9885 .callback_data = lrp, 9886 .flags = RPC_TASK_MOVEABLE, 9887 }; 9888 int status = 0; 9889 9890 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_PNFS_CLEANUP, 9891 &task_setup_data.rpc_client, &msg); 9892 9893 lrp->inode = nfs_igrab_and_active(lrp->args.inode); 9894 if (flags & PNFS_FL_LAYOUTRETURN_ASYNC) { 9895 if (!lrp->inode) { 9896 nfs4_layoutreturn_release(lrp); 9897 return -EAGAIN; 9898 } 9899 task_setup_data.flags |= RPC_TASK_ASYNC; 9900 } 9901 if (!lrp->inode) 9902 flags |= PNFS_FL_LAYOUTRETURN_PRIVILEGED; 9903 9904 nfs4_init_sequence(clp, &lrp->args.seq_args, &lrp->res.seq_res, 1, 9905 flags & PNFS_FL_LAYOUTRETURN_PRIVILEGED ? 1 : 0); 9906 task = rpc_run_task(&task_setup_data); 9907 if (IS_ERR(task)) 9908 return PTR_ERR(task); 9909 if (!(flags & PNFS_FL_LAYOUTRETURN_ASYNC)) 9910 status = task->tk_status; 9911 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status); 9912 dprintk("<-- %s status=%d\n", __func__, status); 9913 rpc_put_task(task); 9914 return status; 9915 } 9916 9917 static int 9918 _nfs4_proc_getdeviceinfo(struct nfs_server *server, 9919 struct pnfs_device *pdev, 9920 const struct cred *cred) 9921 { 9922 struct nfs4_getdeviceinfo_args args = { 9923 .pdev = pdev, 9924 .notify_types = NOTIFY_DEVICEID4_CHANGE | 9925 NOTIFY_DEVICEID4_DELETE, 9926 }; 9927 struct nfs4_getdeviceinfo_res res = { 9928 .pdev = pdev, 9929 }; 9930 struct rpc_message msg = { 9931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], 9932 .rpc_argp = &args, 9933 .rpc_resp = &res, 9934 .rpc_cred = cred, 9935 }; 9936 int status; 9937 9938 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 9939 if (res.notification & ~args.notify_types) 9940 dprintk("%s: unsupported notification\n", __func__); 9941 if (res.notification != args.notify_types) 9942 pdev->nocache = 1; 9943 9944 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status); 9945 9946 dprintk("<-- %s status=%d\n", __func__, status); 9947 9948 return status; 9949 } 9950 9951 int nfs4_proc_getdeviceinfo(struct nfs_server *server, 9952 struct pnfs_device *pdev, 9953 const struct cred *cred) 9954 { 9955 struct nfs4_exception exception = { }; 9956 int err; 9957 9958 do { 9959 err = nfs4_handle_exception(server, 9960 _nfs4_proc_getdeviceinfo(server, pdev, cred), 9961 &exception); 9962 } while (exception.retry); 9963 return err; 9964 } 9965 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); 9966 9967 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) 9968 { 9969 struct nfs4_layoutcommit_data *data = calldata; 9970 struct nfs_server *server = NFS_SERVER(data->args.inode); 9971 9972 nfs4_setup_sequence(server->nfs_client, 9973 &data->args.seq_args, 9974 &data->res.seq_res, 9975 task); 9976 } 9977 9978 static void 9979 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) 9980 { 9981 struct nfs4_layoutcommit_data *data = calldata; 9982 struct nfs_server *server = NFS_SERVER(data->args.inode); 9983 9984 if (!nfs41_sequence_done(task, &data->res.seq_res)) 9985 return; 9986 9987 switch (task->tk_status) { /* Just ignore these failures */ 9988 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ 9989 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ 9990 case -NFS4ERR_BADLAYOUT: /* no layout */ 9991 case -NFS4ERR_GRACE: /* loca_recalim always false */ 9992 task->tk_status = 0; 9993 break; 9994 case 0: 9995 break; 9996 default: 9997 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) { 9998 rpc_restart_call_prepare(task); 9999 return; 10000 } 10001 } 10002 } 10003 10004 static void nfs4_layoutcommit_release(void *calldata) 10005 { 10006 struct nfs4_layoutcommit_data *data = calldata; 10007 10008 pnfs_cleanup_layoutcommit(data); 10009 nfs_post_op_update_inode_force_wcc(data->args.inode, 10010 data->res.fattr); 10011 put_cred(data->cred); 10012 nfs_iput_and_deactive(data->inode); 10013 kfree(data); 10014 } 10015 10016 static const struct rpc_call_ops nfs4_layoutcommit_ops = { 10017 .rpc_call_prepare = nfs4_layoutcommit_prepare, 10018 .rpc_call_done = nfs4_layoutcommit_done, 10019 .rpc_release = nfs4_layoutcommit_release, 10020 }; 10021 10022 int 10023 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) 10024 { 10025 struct rpc_message msg = { 10026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], 10027 .rpc_argp = &data->args, 10028 .rpc_resp = &data->res, 10029 .rpc_cred = data->cred, 10030 }; 10031 struct rpc_task_setup task_setup_data = { 10032 .task = &data->task, 10033 .rpc_client = NFS_CLIENT(data->args.inode), 10034 .rpc_message = &msg, 10035 .callback_ops = &nfs4_layoutcommit_ops, 10036 .callback_data = data, 10037 .flags = RPC_TASK_MOVEABLE, 10038 }; 10039 struct rpc_task *task; 10040 int status = 0; 10041 10042 dprintk("NFS: initiating layoutcommit call. sync %d " 10043 "lbw: %llu inode %llu\n", sync, 10044 data->args.lastbytewritten, 10045 data->args.inode->i_ino); 10046 10047 if (!sync) { 10048 data->inode = nfs_igrab_and_active(data->args.inode); 10049 if (data->inode == NULL) { 10050 nfs4_layoutcommit_release(data); 10051 return -EAGAIN; 10052 } 10053 task_setup_data.flags = RPC_TASK_ASYNC; 10054 } 10055 nfs4_init_sequence(NFS_SERVER(data->args.inode)->nfs_client, 10056 &data->args.seq_args, &data->res.seq_res, 1, 0); 10057 task = rpc_run_task(&task_setup_data); 10058 if (IS_ERR(task)) 10059 return PTR_ERR(task); 10060 if (sync) 10061 status = task->tk_status; 10062 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status); 10063 dprintk("%s: status %d\n", __func__, status); 10064 rpc_put_task(task); 10065 return status; 10066 } 10067 10068 /* 10069 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if 10070 * possible) as per RFC3530bis and RFC5661 Security Considerations sections 10071 */ 10072 static int _nfs41_proc_secinfo_no_name(struct nfs_server *server, 10073 struct nfs_fh *fhandle, 10074 struct nfs4_secinfo_flavors *flavors, 10075 bool use_integrity) 10076 { 10077 struct nfs_client *clp = server->nfs_client; 10078 struct nfs41_secinfo_no_name_args args = { 10079 .style = SECINFO_STYLE_CURRENT_FH, 10080 }; 10081 struct nfs4_secinfo_res res = { 10082 .flavors = flavors, 10083 }; 10084 struct rpc_message msg = { 10085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], 10086 .rpc_argp = &args, 10087 .rpc_resp = &res, 10088 }; 10089 struct nfs4_call_sync_data data = { 10090 .seq_server = server, 10091 .seq_args = &args.seq_args, 10092 .seq_res = &res.seq_res, 10093 }; 10094 struct rpc_task_setup task_setup = { 10095 .rpc_client = server->client, 10096 .rpc_message = &msg, 10097 .callback_ops = clp->cl_mvops->call_sync_ops, 10098 .callback_data = &data, 10099 .flags = RPC_TASK_NO_ROUND_ROBIN, 10100 }; 10101 const struct cred *cred = NULL; 10102 int status; 10103 10104 if (use_integrity) { 10105 task_setup.rpc_client = clp->cl_rpcclient; 10106 10107 cred = nfs4_get_clid_cred(clp); 10108 msg.rpc_cred = cred; 10109 } 10110 10111 nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 0); 10112 status = nfs4_call_sync_custom(&task_setup); 10113 dprintk("<-- %s status=%d\n", __func__, status); 10114 10115 put_cred(cred); 10116 10117 return status; 10118 } 10119 10120 static int nfs41_proc_secinfo_no_name(struct nfs_server *server, 10121 struct nfs_fh *fhandle, 10122 struct nfs4_secinfo_flavors *flavors) 10123 { 10124 struct nfs4_exception exception = { 10125 .interruptible = true, 10126 }; 10127 int err; 10128 do { 10129 /* first try using integrity protection */ 10130 err = -NFS4ERR_WRONGSEC; 10131 10132 /* try to use integrity protection with machine cred */ 10133 if (_nfs4_is_integrity_protected(server->nfs_client)) 10134 err = _nfs41_proc_secinfo_no_name(server, fhandle, 10135 flavors, true); 10136 10137 /* 10138 * if unable to use integrity protection, or SECINFO with 10139 * integrity protection returns NFS4ERR_WRONGSEC (which is 10140 * disallowed by spec, but exists in deployed servers) use 10141 * the current filesystem's rpc_client and the user cred. 10142 */ 10143 if (err == -NFS4ERR_WRONGSEC) 10144 err = _nfs41_proc_secinfo_no_name(server, fhandle, 10145 flavors, false); 10146 10147 switch (err) { 10148 case 0: 10149 case -NFS4ERR_WRONGSEC: 10150 case -ENOTSUPP: 10151 goto out; 10152 default: 10153 err = nfs4_handle_exception(server, err, &exception); 10154 } 10155 } while (exception.retry); 10156 out: 10157 return err; 10158 } 10159 10160 static int nfs41_find_root_sec(struct nfs_server *server, 10161 struct nfs_fh *fhandle, struct nfs_fattr *fattr) 10162 { 10163 int err; 10164 struct page *page; 10165 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR; 10166 struct nfs4_secinfo_flavors *flavors; 10167 struct nfs4_secinfo4 *secinfo; 10168 int i; 10169 10170 page = alloc_page(GFP_KERNEL); 10171 if (!page) { 10172 err = -ENOMEM; 10173 goto out; 10174 } 10175 10176 flavors = page_address(page); 10177 err = nfs41_proc_secinfo_no_name(server, fhandle, flavors); 10178 10179 /* 10180 * Fall back on "guess and check" method if 10181 * the server doesn't support SECINFO_NO_NAME 10182 */ 10183 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) { 10184 err = nfs4_find_root_sec(server, fhandle, fattr); 10185 goto out_freepage; 10186 } 10187 if (err) 10188 goto out_freepage; 10189 10190 for (i = 0; i < flavors->num_flavors; i++) { 10191 secinfo = &flavors->flavors[i]; 10192 10193 switch (secinfo->flavor) { 10194 case RPC_AUTH_NULL: 10195 case RPC_AUTH_UNIX: 10196 case RPC_AUTH_GSS: 10197 flavor = rpcauth_get_pseudoflavor(secinfo->flavor, 10198 &secinfo->flavor_info); 10199 break; 10200 default: 10201 flavor = RPC_AUTH_MAXFLAVOR; 10202 break; 10203 } 10204 10205 if (!nfs_auth_info_match(&server->auth_info, flavor)) 10206 flavor = RPC_AUTH_MAXFLAVOR; 10207 10208 if (flavor != RPC_AUTH_MAXFLAVOR) { 10209 err = nfs4_lookup_root_sec(server, fhandle, fattr, 10210 flavor); 10211 if (!err) 10212 break; 10213 } 10214 } 10215 10216 if (flavor == RPC_AUTH_MAXFLAVOR) 10217 err = -EPERM; 10218 10219 out_freepage: 10220 put_page(page); 10221 if (err == -EACCES) 10222 return -EPERM; 10223 out: 10224 return err; 10225 } 10226 10227 static int _nfs41_test_stateid(struct nfs_server *server, 10228 const nfs4_stateid *stateid, 10229 const struct cred *cred) 10230 { 10231 int status; 10232 struct nfs41_test_stateid_args args = { 10233 .stateid = *stateid, 10234 }; 10235 struct nfs41_test_stateid_res res; 10236 struct rpc_message msg = { 10237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], 10238 .rpc_argp = &args, 10239 .rpc_resp = &res, 10240 .rpc_cred = cred, 10241 }; 10242 struct rpc_clnt *rpc_client = server->client; 10243 struct nfs_client *clp = server->nfs_client; 10244 10245 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_STATEID, &rpc_client, &msg); 10246 10247 dprintk("NFS call test_stateid %p\n", stateid); 10248 nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 1); 10249 status = nfs4_call_sync_sequence(rpc_client, server, &msg, 10250 &args.seq_args, &res.seq_res); 10251 if (status != NFS_OK) { 10252 dprintk("NFS reply test_stateid: failed, %d\n", status); 10253 return status; 10254 } 10255 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); 10256 return -res.status; 10257 } 10258 10259 static void nfs4_handle_delay_or_session_error(struct nfs_server *server, 10260 int err, struct nfs4_exception *exception) 10261 { 10262 exception->retry = 0; 10263 switch(err) { 10264 case -NFS4ERR_DELAY: 10265 case -NFS4ERR_RETRY_UNCACHED_REP: 10266 nfs4_handle_exception(server, err, exception); 10267 break; 10268 case -NFS4ERR_BADSESSION: 10269 case -NFS4ERR_BADSLOT: 10270 case -NFS4ERR_BAD_HIGH_SLOT: 10271 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 10272 case -NFS4ERR_DEADSESSION: 10273 nfs4_do_handle_exception(server, err, exception); 10274 } 10275 } 10276 10277 /** 10278 * nfs41_test_stateid - perform a TEST_STATEID operation 10279 * 10280 * @server: server / transport on which to perform the operation 10281 * @stateid: state ID to test 10282 * @cred: credential 10283 * 10284 * Returns NFS_OK if the server recognizes that "stateid" is valid. 10285 * Otherwise a negative NFS4ERR value is returned if the operation 10286 * failed or the state ID is not currently valid. 10287 */ 10288 static int nfs41_test_stateid(struct nfs_server *server, 10289 const nfs4_stateid *stateid, 10290 const struct cred *cred) 10291 { 10292 struct nfs4_exception exception = { 10293 .interruptible = true, 10294 }; 10295 int err; 10296 do { 10297 err = _nfs41_test_stateid(server, stateid, cred); 10298 nfs4_handle_delay_or_session_error(server, err, &exception); 10299 } while (exception.retry); 10300 return err; 10301 } 10302 10303 struct nfs_free_stateid_data { 10304 struct nfs_server *server; 10305 struct nfs41_free_stateid_args args; 10306 struct nfs41_free_stateid_res res; 10307 }; 10308 10309 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata) 10310 { 10311 struct nfs_free_stateid_data *data = calldata; 10312 nfs4_setup_sequence(data->server->nfs_client, 10313 &data->args.seq_args, 10314 &data->res.seq_res, 10315 task); 10316 } 10317 10318 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata) 10319 { 10320 struct nfs_free_stateid_data *data = calldata; 10321 10322 nfs41_sequence_done(task, &data->res.seq_res); 10323 10324 switch (task->tk_status) { 10325 case -NFS4ERR_DELAY: 10326 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN) 10327 rpc_restart_call_prepare(task); 10328 } 10329 } 10330 10331 static void nfs41_free_stateid_release(void *calldata) 10332 { 10333 struct nfs_free_stateid_data *data = calldata; 10334 struct nfs_client *clp = data->server->nfs_client; 10335 10336 nfs_put_client(clp); 10337 kfree(calldata); 10338 } 10339 10340 static const struct rpc_call_ops nfs41_free_stateid_ops = { 10341 .rpc_call_prepare = nfs41_free_stateid_prepare, 10342 .rpc_call_done = nfs41_free_stateid_done, 10343 .rpc_release = nfs41_free_stateid_release, 10344 }; 10345 10346 /** 10347 * nfs41_free_stateid - perform a FREE_STATEID operation 10348 * 10349 * @server: server / transport on which to perform the operation 10350 * @stateid: state ID to release 10351 * @cred: credential 10352 * @privileged: set to true if this call needs to be privileged 10353 * 10354 * Note: this function is always asynchronous. 10355 */ 10356 static int nfs41_free_stateid(struct nfs_server *server, 10357 nfs4_stateid *stateid, 10358 const struct cred *cred, 10359 bool privileged) 10360 { 10361 struct rpc_message msg = { 10362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], 10363 .rpc_cred = cred, 10364 }; 10365 struct rpc_task_setup task_setup = { 10366 .rpc_client = server->client, 10367 .rpc_message = &msg, 10368 .callback_ops = &nfs41_free_stateid_ops, 10369 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE, 10370 }; 10371 struct nfs_free_stateid_data *data; 10372 struct rpc_task *task; 10373 struct nfs_client *clp = server->nfs_client; 10374 10375 if (!refcount_inc_not_zero(&clp->cl_count)) 10376 return -EIO; 10377 10378 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_STATEID, 10379 &task_setup.rpc_client, &msg); 10380 10381 dprintk("NFS call free_stateid %p\n", stateid); 10382 data = kmalloc_obj(*data); 10383 if (!data) 10384 return -ENOMEM; 10385 data->server = server; 10386 nfs4_stateid_copy(&data->args.stateid, stateid); 10387 10388 task_setup.callback_data = data; 10389 10390 msg.rpc_argp = &data->args; 10391 msg.rpc_resp = &data->res; 10392 nfs4_init_sequence(clp, &data->args.seq_args, &data->res.seq_res, 1, 10393 privileged); 10394 task = rpc_run_task(&task_setup); 10395 if (IS_ERR(task)) 10396 return PTR_ERR(task); 10397 rpc_put_task(task); 10398 stateid->type = NFS4_FREED_STATEID_TYPE; 10399 return 0; 10400 } 10401 10402 static void 10403 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) 10404 { 10405 const struct cred *cred = lsp->ls_state->owner->so_cred; 10406 10407 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false); 10408 nfs4_free_lock_state(server, lsp); 10409 } 10410 10411 static bool nfs41_match_stateid(const nfs4_stateid *s1, 10412 const nfs4_stateid *s2) 10413 { 10414 trace_nfs41_match_stateid(s1, s2); 10415 10416 if (s1->type != s2->type) 10417 return false; 10418 10419 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) 10420 return false; 10421 10422 if (s1->seqid == s2->seqid) 10423 return true; 10424 10425 return s1->seqid == 0 || s2->seqid == 0; 10426 } 10427 10428 bool nfs4_match_stateid(const nfs4_stateid *s1, 10429 const nfs4_stateid *s2) 10430 { 10431 trace_nfs4_match_stateid(s1, s2); 10432 10433 return nfs4_stateid_match(s1, s2); 10434 } 10435 10436 10437 static const struct nfs4_sequence_slot_ops nfs41_sequence_slot_ops = { 10438 .process = nfs41_sequence_process, 10439 .done = nfs41_sequence_done, 10440 .free_slot = nfs41_sequence_free_slot, 10441 }; 10442 10443 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { 10444 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, 10445 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, 10446 .recover_open = nfs4_open_reclaim, 10447 .recover_lock = nfs4_lock_reclaim, 10448 .establish_clid = nfs41_init_clientid, 10449 .reclaim_complete = nfs41_proc_reclaim_complete, 10450 .detect_trunking = nfs41_discover_server_trunking, 10451 }; 10452 10453 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { 10454 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, 10455 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, 10456 .recover_open = nfs41_open_expired, 10457 .recover_lock = nfs41_lock_expired, 10458 .establish_clid = nfs41_init_clientid, 10459 }; 10460 10461 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { 10462 .sched_state_renewal = nfs41_proc_async_sequence, 10463 .get_state_renewal_cred = nfs4_get_machine_cred, 10464 .renew_lease = nfs4_proc_sequence, 10465 }; 10466 10467 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = { 10468 .get_locations = _nfs41_proc_get_locations, 10469 .fsid_present = _nfs41_proc_fsid_present, 10470 }; 10471 10472 static struct nfs_seqid * 10473 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2) 10474 { 10475 return NULL; 10476 } 10477 10478 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { 10479 .minor_version = 1, 10480 .init_caps = NFS_CAP_READDIRPLUS 10481 | NFS_CAP_ATOMIC_OPEN 10482 | NFS_CAP_DIR_DELEG 10483 | NFS_CAP_POSIX_LOCK 10484 | NFS_CAP_STATEID_NFSV41 10485 | NFS_CAP_ATOMIC_OPEN_V1 10486 | NFS_CAP_LGOPEN 10487 | NFS_CAP_MOVEABLE, 10488 .init_client = nfs41_init_client, 10489 .shutdown_client = nfs41_shutdown_client, 10490 .match_stateid = nfs41_match_stateid, 10491 .find_root_sec = nfs41_find_root_sec, 10492 .free_lock_state = nfs41_free_lock_state, 10493 .test_and_free_expired = nfs41_test_and_free_expired_stateid, 10494 .alloc_seqid = nfs_alloc_no_seqid, 10495 .session_trunk = nfs4_test_session_trunk, 10496 .call_sync_ops = &nfs41_call_sync_ops, 10497 .sequence_slot_ops = &nfs41_sequence_slot_ops, 10498 .reboot_recovery_ops = &nfs41_reboot_recovery_ops, 10499 .nograce_recovery_ops = &nfs41_nograce_recovery_ops, 10500 .state_renewal_ops = &nfs41_state_renewal_ops, 10501 .mig_recovery_ops = &nfs41_mig_recovery_ops, 10502 }; 10503 10504 #if defined(CONFIG_NFS_V4_2) 10505 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = { 10506 .minor_version = 2, 10507 .init_caps = NFS_CAP_READDIRPLUS 10508 | NFS_CAP_ATOMIC_OPEN 10509 | NFS_CAP_DIR_DELEG 10510 | NFS_CAP_POSIX_LOCK 10511 | NFS_CAP_STATEID_NFSV41 10512 | NFS_CAP_ATOMIC_OPEN_V1 10513 | NFS_CAP_LGOPEN 10514 | NFS_CAP_ALLOCATE 10515 | NFS_CAP_COPY 10516 | NFS_CAP_OFFLOAD_CANCEL 10517 | NFS_CAP_COPY_NOTIFY 10518 | NFS_CAP_DEALLOCATE 10519 | NFS_CAP_ZERO_RANGE 10520 | NFS_CAP_SEEK 10521 | NFS_CAP_LAYOUTSTATS 10522 | NFS_CAP_CLONE 10523 | NFS_CAP_LAYOUTERROR 10524 | NFS_CAP_READ_PLUS 10525 | NFS_CAP_MOVEABLE 10526 | NFS_CAP_OFFLOAD_STATUS, 10527 .init_client = nfs41_init_client, 10528 .shutdown_client = nfs41_shutdown_client, 10529 .match_stateid = nfs41_match_stateid, 10530 .find_root_sec = nfs41_find_root_sec, 10531 .free_lock_state = nfs41_free_lock_state, 10532 .call_sync_ops = &nfs41_call_sync_ops, 10533 .sequence_slot_ops = &nfs41_sequence_slot_ops, 10534 .test_and_free_expired = nfs41_test_and_free_expired_stateid, 10535 .alloc_seqid = nfs_alloc_no_seqid, 10536 .session_trunk = nfs4_test_session_trunk, 10537 .reboot_recovery_ops = &nfs41_reboot_recovery_ops, 10538 .nograce_recovery_ops = &nfs41_nograce_recovery_ops, 10539 .state_renewal_ops = &nfs41_state_renewal_ops, 10540 .mig_recovery_ops = &nfs41_mig_recovery_ops, 10541 }; 10542 #endif 10543 10544 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { 10545 #if defined(CONFIG_NFS_V4_0) 10546 [0] = &nfs_v4_0_minor_ops, 10547 #endif /* CONFIG_NFS_V4_0 */ 10548 [1] = &nfs_v4_1_minor_ops, 10549 #if defined(CONFIG_NFS_V4_2) 10550 [2] = &nfs_v4_2_minor_ops, 10551 #endif 10552 }; 10553 10554 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size) 10555 { 10556 ssize_t error, error2, error3; 10557 size_t left = size; 10558 10559 error = generic_listxattr(dentry, list, left); 10560 if (error < 0) 10561 return error; 10562 if (list) { 10563 list += error; 10564 left -= error; 10565 } 10566 10567 error2 = security_inode_listsecurity(d_inode(dentry), list, left); 10568 if (error2 < 0) 10569 return error2; 10570 if (list) { 10571 list += error2; 10572 left -= error2; 10573 } 10574 10575 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left); 10576 if (error3 < 0) 10577 return error3; 10578 10579 error += error2 + error3; 10580 if (size && error > size) 10581 return -ERANGE; 10582 return error; 10583 } 10584 10585 static void nfs4_enable_swap(struct inode *inode) 10586 { 10587 /* The state manager thread must always be running. 10588 * It will notice the client is a swapper, and stay put. 10589 */ 10590 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 10591 10592 nfs4_schedule_state_manager(clp); 10593 } 10594 10595 static void nfs4_disable_swap(struct inode *inode) 10596 { 10597 /* The state manager thread will now exit once it is 10598 * woken. 10599 */ 10600 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 10601 10602 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state); 10603 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state); 10604 wake_up_var(&clp->cl_state); 10605 } 10606 10607 static const struct inode_operations nfs4_dir_inode_operations = { 10608 .create = nfs_create, 10609 .lookup = nfs_lookup, 10610 .atomic_open = nfs_atomic_open, 10611 .link = nfs_link, 10612 .unlink = nfs_unlink, 10613 .symlink = nfs_symlink, 10614 .mkdir = nfs_mkdir, 10615 .rmdir = nfs_rmdir, 10616 .mknod = nfs_mknod, 10617 .rename = nfs_rename, 10618 .permission = nfs_permission, 10619 .getattr = nfs_getattr, 10620 .setattr = nfs_setattr, 10621 .listxattr = nfs4_listxattr, 10622 .fileattr_get = nfs_fileattr_get, 10623 }; 10624 10625 static const struct inode_operations nfs4_file_inode_operations = { 10626 .permission = nfs_permission, 10627 .getattr = nfs_getattr, 10628 .setattr = nfs_setattr, 10629 .listxattr = nfs4_listxattr, 10630 .fileattr_get = nfs_fileattr_get, 10631 }; 10632 10633 static struct nfs_server *nfs4_clone_server(struct nfs_server *source, 10634 struct nfs_fh *fh, struct nfs_fattr *fattr, 10635 rpc_authflavor_t flavor) 10636 { 10637 struct nfs_server *server; 10638 int error; 10639 10640 server = nfs_clone_server(source, fh, fattr, flavor); 10641 if (IS_ERR(server)) 10642 return server; 10643 10644 nfs4_session_limit_rwsize(server); 10645 nfs4_session_limit_xasize(server); 10646 10647 error = nfs4_delegation_hash_alloc(server); 10648 if (error) { 10649 nfs_free_server(server); 10650 return ERR_PTR(error); 10651 } 10652 10653 return server; 10654 } 10655 10656 const struct nfs_rpc_ops nfs_v4_clientops = { 10657 .version = 4, /* protocol version */ 10658 .dentry_ops = &nfs4_dentry_operations, 10659 .dir_inode_ops = &nfs4_dir_inode_operations, 10660 .file_inode_ops = &nfs4_file_inode_operations, 10661 .file_ops = &nfs4_file_operations, 10662 .getroot = nfs4_proc_get_root, 10663 .submount = nfs4_submount, 10664 .try_get_tree = nfs4_try_get_tree, 10665 .getattr = nfs4_proc_getattr, 10666 .setattr = nfs4_proc_setattr, 10667 .lookup = nfs4_proc_lookup, 10668 .lookupp = nfs4_proc_lookupp, 10669 .access = nfs4_proc_access, 10670 .readlink = nfs4_proc_readlink, 10671 .create = nfs4_proc_create, 10672 .remove = nfs4_proc_remove, 10673 .unlink_setup = nfs4_proc_unlink_setup, 10674 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, 10675 .unlink_done = nfs4_proc_unlink_done, 10676 .rename_setup = nfs4_proc_rename_setup, 10677 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, 10678 .rename_done = nfs4_proc_rename_done, 10679 .link = nfs4_proc_link, 10680 .symlink = nfs4_proc_symlink, 10681 .mkdir = nfs4_proc_mkdir, 10682 .rmdir = nfs4_proc_rmdir, 10683 .readdir = nfs4_proc_readdir, 10684 .mknod = nfs4_proc_mknod, 10685 .statfs = nfs4_proc_statfs, 10686 .fsinfo = nfs4_proc_fsinfo, 10687 .pathconf = nfs4_proc_pathconf, 10688 .set_capabilities = nfs4_server_capabilities, 10689 .decode_dirent = nfs4_decode_dirent, 10690 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare, 10691 .read_setup = nfs4_proc_read_setup, 10692 .read_done = nfs4_read_done, 10693 .write_setup = nfs4_proc_write_setup, 10694 .write_done = nfs4_write_done, 10695 .commit_setup = nfs4_proc_commit_setup, 10696 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, 10697 .commit_done = nfs4_commit_done, 10698 .lock = nfs4_proc_lock, 10699 .clear_acl_cache = nfs4_zap_acl_attr, 10700 .close_context = nfs4_close_context, 10701 .open_context = nfs4_atomic_open, 10702 .have_delegation = nfs4_have_delegation, 10703 .return_delegation = nfs4_inode_return_delegation, 10704 .alloc_client = nfs4_alloc_client, 10705 .init_client = nfs4_init_client, 10706 .free_client = nfs4_free_client, 10707 .create_server = nfs4_create_server, 10708 .clone_server = nfs4_clone_server, 10709 .discover_trunking = nfs4_discover_trunking, 10710 .enable_swap = nfs4_enable_swap, 10711 .disable_swap = nfs4_disable_swap, 10712 }; 10713 10714 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { 10715 .name = XATTR_NAME_NFSV4_ACL, 10716 .list = nfs4_xattr_list_nfs4_acl, 10717 .get = nfs4_xattr_get_nfs4_acl, 10718 .set = nfs4_xattr_set_nfs4_acl, 10719 }; 10720 10721 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = { 10722 .name = XATTR_NAME_NFSV4_DACL, 10723 .list = nfs4_xattr_list_nfs4_dacl, 10724 .get = nfs4_xattr_get_nfs4_dacl, 10725 .set = nfs4_xattr_set_nfs4_dacl, 10726 }; 10727 10728 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = { 10729 .name = XATTR_NAME_NFSV4_SACL, 10730 .list = nfs4_xattr_list_nfs4_sacl, 10731 .get = nfs4_xattr_get_nfs4_sacl, 10732 .set = nfs4_xattr_set_nfs4_sacl, 10733 }; 10734 10735 #ifdef CONFIG_NFS_V4_2 10736 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = { 10737 .prefix = XATTR_USER_PREFIX, 10738 .get = nfs4_xattr_get_nfs4_user, 10739 .set = nfs4_xattr_set_nfs4_user, 10740 }; 10741 #endif 10742 10743 const struct xattr_handler * const nfs4_xattr_handlers[] = { 10744 &nfs4_xattr_nfs4_acl_handler, 10745 &nfs4_xattr_nfs4_dacl_handler, 10746 &nfs4_xattr_nfs4_sacl_handler, 10747 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 10748 &nfs4_xattr_nfs4_label_handler, 10749 #endif 10750 #ifdef CONFIG_NFS_V4_2 10751 &nfs4_xattr_nfs4_user_handler, 10752 #endif 10753 NULL 10754 }; 10755