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