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