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