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