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