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