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