1 /* 2 * fs/nfs/nfs4proc.c 3 * 4 * Client-side procedure declarations for NFSv4. 5 * 6 * Copyright (c) 2002 The Regents of the University of Michigan. 7 * All rights reserved. 8 * 9 * Kendrick Smith <kmsmith@umich.edu> 10 * Andy Adamson <andros@umich.edu> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <linux/mm.h> 39 #include <linux/delay.h> 40 #include <linux/errno.h> 41 #include <linux/file.h> 42 #include <linux/string.h> 43 #include <linux/ratelimit.h> 44 #include <linux/printk.h> 45 #include <linux/slab.h> 46 #include <linux/sunrpc/clnt.h> 47 #include <linux/nfs.h> 48 #include <linux/nfs4.h> 49 #include <linux/nfs_fs.h> 50 #include <linux/nfs_page.h> 51 #include <linux/nfs_mount.h> 52 #include <linux/namei.h> 53 #include <linux/mount.h> 54 #include <linux/module.h> 55 #include <linux/xattr.h> 56 #include <linux/utsname.h> 57 #include <linux/freezer.h> 58 59 #include "nfs4_fs.h" 60 #include "delegation.h" 61 #include "internal.h" 62 #include "iostat.h" 63 #include "callback.h" 64 #include "pnfs.h" 65 #include "netns.h" 66 #include "nfs4idmap.h" 67 #include "nfs4session.h" 68 #include "fscache.h" 69 70 #include "nfs4trace.h" 71 72 #define NFSDBG_FACILITY NFSDBG_PROC 73 74 #define NFS4_POLL_RETRY_MIN (HZ/10) 75 #define NFS4_POLL_RETRY_MAX (15*HZ) 76 77 struct nfs4_opendata; 78 static int _nfs4_proc_open(struct nfs4_opendata *data); 79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data); 80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); 81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *); 82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); 83 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label); 84 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label); 85 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, 86 struct nfs_fattr *fattr, struct iattr *sattr, 87 struct nfs4_state *state, struct nfs4_label *ilabel, 88 struct nfs4_label *olabel); 89 #ifdef CONFIG_NFS_V4_1 90 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *, 91 struct rpc_cred *); 92 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *, 93 struct rpc_cred *); 94 #endif 95 96 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 97 static inline struct nfs4_label * 98 nfs4_label_init_security(struct inode *dir, struct dentry *dentry, 99 struct iattr *sattr, struct nfs4_label *label) 100 { 101 int err; 102 103 if (label == NULL) 104 return NULL; 105 106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0) 107 return NULL; 108 109 err = security_dentry_init_security(dentry, sattr->ia_mode, 110 &dentry->d_name, (void **)&label->label, &label->len); 111 if (err == 0) 112 return label; 113 114 return NULL; 115 } 116 static inline void 117 nfs4_label_release_security(struct nfs4_label *label) 118 { 119 if (label) 120 security_release_secctx(label->label, label->len); 121 } 122 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) 123 { 124 if (label) 125 return server->attr_bitmask; 126 127 return server->attr_bitmask_nl; 128 } 129 #else 130 static inline struct nfs4_label * 131 nfs4_label_init_security(struct inode *dir, struct dentry *dentry, 132 struct iattr *sattr, struct nfs4_label *l) 133 { return NULL; } 134 static inline void 135 nfs4_label_release_security(struct nfs4_label *label) 136 { return; } 137 static inline u32 * 138 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) 139 { return server->attr_bitmask; } 140 #endif 141 142 /* Prevent leaks of NFSv4 errors into userland */ 143 static int nfs4_map_errors(int err) 144 { 145 if (err >= -1000) 146 return err; 147 switch (err) { 148 case -NFS4ERR_RESOURCE: 149 case -NFS4ERR_LAYOUTTRYLATER: 150 case -NFS4ERR_RECALLCONFLICT: 151 return -EREMOTEIO; 152 case -NFS4ERR_WRONGSEC: 153 case -NFS4ERR_WRONG_CRED: 154 return -EPERM; 155 case -NFS4ERR_BADOWNER: 156 case -NFS4ERR_BADNAME: 157 return -EINVAL; 158 case -NFS4ERR_SHARE_DENIED: 159 return -EACCES; 160 case -NFS4ERR_MINOR_VERS_MISMATCH: 161 return -EPROTONOSUPPORT; 162 case -NFS4ERR_FILE_OPEN: 163 return -EBUSY; 164 default: 165 dprintk("%s could not handle NFSv4 error %d\n", 166 __func__, -err); 167 break; 168 } 169 return -EIO; 170 } 171 172 /* 173 * This is our standard bitmap for GETATTR requests. 174 */ 175 const u32 nfs4_fattr_bitmap[3] = { 176 FATTR4_WORD0_TYPE 177 | FATTR4_WORD0_CHANGE 178 | FATTR4_WORD0_SIZE 179 | FATTR4_WORD0_FSID 180 | FATTR4_WORD0_FILEID, 181 FATTR4_WORD1_MODE 182 | FATTR4_WORD1_NUMLINKS 183 | FATTR4_WORD1_OWNER 184 | FATTR4_WORD1_OWNER_GROUP 185 | FATTR4_WORD1_RAWDEV 186 | FATTR4_WORD1_SPACE_USED 187 | FATTR4_WORD1_TIME_ACCESS 188 | FATTR4_WORD1_TIME_METADATA 189 | FATTR4_WORD1_TIME_MODIFY 190 | FATTR4_WORD1_MOUNTED_ON_FILEID, 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(d_inode(dentry))); 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(d_inode(dentry->d_parent))); 311 312 readdir->pgbase = (char *)p - (char *)start; 313 readdir->count -= readdir->pgbase; 314 kunmap_atomic(start); 315 } 316 317 static long nfs4_update_delay(long *timeout) 318 { 319 long ret; 320 if (!timeout) 321 return NFS4_POLL_RETRY_MAX; 322 if (*timeout <= 0) 323 *timeout = NFS4_POLL_RETRY_MIN; 324 if (*timeout > NFS4_POLL_RETRY_MAX) 325 *timeout = NFS4_POLL_RETRY_MAX; 326 ret = *timeout; 327 *timeout <<= 1; 328 return ret; 329 } 330 331 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) 332 { 333 int res = 0; 334 335 might_sleep(); 336 337 freezable_schedule_timeout_killable_unsafe( 338 nfs4_update_delay(timeout)); 339 if (fatal_signal_pending(current)) 340 res = -ERESTARTSYS; 341 return res; 342 } 343 344 /* This is the error handling routine for processes that are allowed 345 * to sleep. 346 */ 347 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) 348 { 349 struct nfs_client *clp = server->nfs_client; 350 struct nfs4_state *state = exception->state; 351 struct inode *inode = exception->inode; 352 int ret = errorcode; 353 354 exception->retry = 0; 355 switch(errorcode) { 356 case 0: 357 return 0; 358 case -NFS4ERR_OPENMODE: 359 case -NFS4ERR_DELEG_REVOKED: 360 case -NFS4ERR_ADMIN_REVOKED: 361 case -NFS4ERR_BAD_STATEID: 362 if (inode && nfs4_have_delegation(inode, FMODE_READ)) { 363 nfs4_inode_return_delegation(inode); 364 exception->retry = 1; 365 return 0; 366 } 367 if (state == NULL) 368 break; 369 ret = nfs4_schedule_stateid_recovery(server, state); 370 if (ret < 0) 371 break; 372 goto wait_on_recovery; 373 case -NFS4ERR_EXPIRED: 374 if (state != NULL) { 375 ret = nfs4_schedule_stateid_recovery(server, state); 376 if (ret < 0) 377 break; 378 } 379 case -NFS4ERR_STALE_STATEID: 380 case -NFS4ERR_STALE_CLIENTID: 381 nfs4_schedule_lease_recovery(clp); 382 goto wait_on_recovery; 383 case -NFS4ERR_MOVED: 384 ret = nfs4_schedule_migration_recovery(server); 385 if (ret < 0) 386 break; 387 goto wait_on_recovery; 388 case -NFS4ERR_LEASE_MOVED: 389 nfs4_schedule_lease_moved_recovery(clp); 390 goto wait_on_recovery; 391 #if defined(CONFIG_NFS_V4_1) 392 case -NFS4ERR_BADSESSION: 393 case -NFS4ERR_BADSLOT: 394 case -NFS4ERR_BAD_HIGH_SLOT: 395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 396 case -NFS4ERR_DEADSESSION: 397 case -NFS4ERR_SEQ_FALSE_RETRY: 398 case -NFS4ERR_SEQ_MISORDERED: 399 dprintk("%s ERROR: %d Reset session\n", __func__, 400 errorcode); 401 nfs4_schedule_session_recovery(clp->cl_session, errorcode); 402 goto wait_on_recovery; 403 #endif /* defined(CONFIG_NFS_V4_1) */ 404 case -NFS4ERR_FILE_OPEN: 405 if (exception->timeout > HZ) { 406 /* We have retried a decent amount, time to 407 * fail 408 */ 409 ret = -EBUSY; 410 break; 411 } 412 case -NFS4ERR_GRACE: 413 case -NFS4ERR_DELAY: 414 ret = nfs4_delay(server->client, &exception->timeout); 415 if (ret != 0) 416 break; 417 case -NFS4ERR_RETRY_UNCACHED_REP: 418 case -NFS4ERR_OLD_STATEID: 419 exception->retry = 1; 420 break; 421 case -NFS4ERR_BADOWNER: 422 /* The following works around a Linux server bug! */ 423 case -NFS4ERR_BADNAME: 424 if (server->caps & NFS_CAP_UIDGID_NOMAP) { 425 server->caps &= ~NFS_CAP_UIDGID_NOMAP; 426 exception->retry = 1; 427 printk(KERN_WARNING "NFS: v4 server %s " 428 "does not accept raw " 429 "uid/gids. " 430 "Reenabling the idmapper.\n", 431 server->nfs_client->cl_hostname); 432 } 433 } 434 /* We failed to handle the error */ 435 return nfs4_map_errors(ret); 436 wait_on_recovery: 437 ret = nfs4_wait_clnt_recover(clp); 438 if (test_bit(NFS_MIG_FAILED, &server->mig_status)) 439 return -EIO; 440 if (ret == 0) 441 exception->retry = 1; 442 return ret; 443 } 444 445 /* 446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected 447 * or 'false' otherwise. 448 */ 449 static bool _nfs4_is_integrity_protected(struct nfs_client *clp) 450 { 451 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor; 452 453 if (flavor == RPC_AUTH_GSS_KRB5I || 454 flavor == RPC_AUTH_GSS_KRB5P) 455 return true; 456 457 return false; 458 } 459 460 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) 461 { 462 spin_lock(&clp->cl_lock); 463 if (time_before(clp->cl_last_renewal,timestamp)) 464 clp->cl_last_renewal = timestamp; 465 spin_unlock(&clp->cl_lock); 466 } 467 468 static void renew_lease(const struct nfs_server *server, unsigned long timestamp) 469 { 470 struct nfs_client *clp = server->nfs_client; 471 472 if (!nfs4_has_session(clp)) 473 do_renew_lease(clp, timestamp); 474 } 475 476 struct nfs4_call_sync_data { 477 const struct nfs_server *seq_server; 478 struct nfs4_sequence_args *seq_args; 479 struct nfs4_sequence_res *seq_res; 480 }; 481 482 void nfs4_init_sequence(struct nfs4_sequence_args *args, 483 struct nfs4_sequence_res *res, int cache_reply) 484 { 485 args->sa_slot = NULL; 486 args->sa_cache_this = cache_reply; 487 args->sa_privileged = 0; 488 489 res->sr_slot = NULL; 490 } 491 492 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args) 493 { 494 args->sa_privileged = 1; 495 } 496 497 int nfs40_setup_sequence(struct nfs4_slot_table *tbl, 498 struct nfs4_sequence_args *args, 499 struct nfs4_sequence_res *res, 500 struct rpc_task *task) 501 { 502 struct nfs4_slot *slot; 503 504 /* slot already allocated? */ 505 if (res->sr_slot != NULL) 506 goto out_start; 507 508 spin_lock(&tbl->slot_tbl_lock); 509 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) 510 goto out_sleep; 511 512 slot = nfs4_alloc_slot(tbl); 513 if (IS_ERR(slot)) { 514 if (slot == ERR_PTR(-ENOMEM)) 515 task->tk_timeout = HZ >> 2; 516 goto out_sleep; 517 } 518 spin_unlock(&tbl->slot_tbl_lock); 519 520 args->sa_slot = slot; 521 res->sr_slot = slot; 522 523 out_start: 524 rpc_call_start(task); 525 return 0; 526 527 out_sleep: 528 if (args->sa_privileged) 529 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, 530 NULL, RPC_PRIORITY_PRIVILEGED); 531 else 532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); 533 spin_unlock(&tbl->slot_tbl_lock); 534 return -EAGAIN; 535 } 536 EXPORT_SYMBOL_GPL(nfs40_setup_sequence); 537 538 static int nfs40_sequence_done(struct rpc_task *task, 539 struct nfs4_sequence_res *res) 540 { 541 struct nfs4_slot *slot = res->sr_slot; 542 struct nfs4_slot_table *tbl; 543 544 if (slot == NULL) 545 goto out; 546 547 tbl = slot->table; 548 spin_lock(&tbl->slot_tbl_lock); 549 if (!nfs41_wake_and_assign_slot(tbl, slot)) 550 nfs4_free_slot(tbl, slot); 551 spin_unlock(&tbl->slot_tbl_lock); 552 553 res->sr_slot = NULL; 554 out: 555 return 1; 556 } 557 558 #if defined(CONFIG_NFS_V4_1) 559 560 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) 561 { 562 struct nfs4_session *session; 563 struct nfs4_slot_table *tbl; 564 struct nfs4_slot *slot = res->sr_slot; 565 bool send_new_highest_used_slotid = false; 566 567 tbl = slot->table; 568 session = tbl->session; 569 570 spin_lock(&tbl->slot_tbl_lock); 571 /* Be nice to the server: try to ensure that the last transmitted 572 * value for highest_user_slotid <= target_highest_slotid 573 */ 574 if (tbl->highest_used_slotid > tbl->target_highest_slotid) 575 send_new_highest_used_slotid = true; 576 577 if (nfs41_wake_and_assign_slot(tbl, slot)) { 578 send_new_highest_used_slotid = false; 579 goto out_unlock; 580 } 581 nfs4_free_slot(tbl, slot); 582 583 if (tbl->highest_used_slotid != NFS4_NO_SLOT) 584 send_new_highest_used_slotid = false; 585 out_unlock: 586 spin_unlock(&tbl->slot_tbl_lock); 587 res->sr_slot = NULL; 588 if (send_new_highest_used_slotid) 589 nfs41_server_notify_highest_slotid_update(session->clp); 590 } 591 592 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) 593 { 594 struct nfs4_session *session; 595 struct nfs4_slot *slot = res->sr_slot; 596 struct nfs_client *clp; 597 bool interrupted = false; 598 int ret = 1; 599 600 if (slot == NULL) 601 goto out_noaction; 602 /* don't increment the sequence number if the task wasn't sent */ 603 if (!RPC_WAS_SENT(task)) 604 goto out; 605 606 session = slot->table->session; 607 608 if (slot->interrupted) { 609 slot->interrupted = 0; 610 interrupted = true; 611 } 612 613 trace_nfs4_sequence_done(session, res); 614 /* Check the SEQUENCE operation status */ 615 switch (res->sr_status) { 616 case 0: 617 /* Update the slot's sequence and clientid lease timer */ 618 ++slot->seq_nr; 619 clp = session->clp; 620 do_renew_lease(clp, res->sr_timestamp); 621 /* Check sequence flags */ 622 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags); 623 nfs41_update_target_slotid(slot->table, slot, res); 624 break; 625 case 1: 626 /* 627 * sr_status remains 1 if an RPC level error occurred. 628 * The server may or may not have processed the sequence 629 * operation.. 630 * Mark the slot as having hosted an interrupted RPC call. 631 */ 632 slot->interrupted = 1; 633 goto out; 634 case -NFS4ERR_DELAY: 635 /* The server detected a resend of the RPC call and 636 * returned NFS4ERR_DELAY as per Section 2.10.6.2 637 * of RFC5661. 638 */ 639 dprintk("%s: slot=%u seq=%u: Operation in progress\n", 640 __func__, 641 slot->slot_nr, 642 slot->seq_nr); 643 goto out_retry; 644 case -NFS4ERR_BADSLOT: 645 /* 646 * The slot id we used was probably retired. Try again 647 * using a different slot id. 648 */ 649 goto retry_nowait; 650 case -NFS4ERR_SEQ_MISORDERED: 651 /* 652 * Was the last operation on this sequence interrupted? 653 * If so, retry after bumping the sequence number. 654 */ 655 if (interrupted) { 656 ++slot->seq_nr; 657 goto retry_nowait; 658 } 659 /* 660 * Could this slot have been previously retired? 661 * If so, then the server may be expecting seq_nr = 1! 662 */ 663 if (slot->seq_nr != 1) { 664 slot->seq_nr = 1; 665 goto retry_nowait; 666 } 667 break; 668 case -NFS4ERR_SEQ_FALSE_RETRY: 669 ++slot->seq_nr; 670 goto retry_nowait; 671 default: 672 /* Just update the slot sequence no. */ 673 ++slot->seq_nr; 674 } 675 out: 676 /* The session may be reset by one of the error handlers. */ 677 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); 678 nfs41_sequence_free_slot(res); 679 out_noaction: 680 return ret; 681 retry_nowait: 682 if (rpc_restart_call_prepare(task)) { 683 task->tk_status = 0; 684 ret = 0; 685 } 686 goto out; 687 out_retry: 688 if (!rpc_restart_call(task)) 689 goto out; 690 rpc_delay(task, NFS4_POLL_RETRY_MAX); 691 return 0; 692 } 693 EXPORT_SYMBOL_GPL(nfs41_sequence_done); 694 695 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) 696 { 697 if (res->sr_slot == NULL) 698 return 1; 699 if (!res->sr_slot->table->session) 700 return nfs40_sequence_done(task, res); 701 return nfs41_sequence_done(task, res); 702 } 703 EXPORT_SYMBOL_GPL(nfs4_sequence_done); 704 705 int nfs41_setup_sequence(struct nfs4_session *session, 706 struct nfs4_sequence_args *args, 707 struct nfs4_sequence_res *res, 708 struct rpc_task *task) 709 { 710 struct nfs4_slot *slot; 711 struct nfs4_slot_table *tbl; 712 713 dprintk("--> %s\n", __func__); 714 /* slot already allocated? */ 715 if (res->sr_slot != NULL) 716 goto out_success; 717 718 tbl = &session->fc_slot_table; 719 720 task->tk_timeout = 0; 721 722 spin_lock(&tbl->slot_tbl_lock); 723 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) && 724 !args->sa_privileged) { 725 /* The state manager will wait until the slot table is empty */ 726 dprintk("%s session is draining\n", __func__); 727 goto out_sleep; 728 } 729 730 slot = nfs4_alloc_slot(tbl); 731 if (IS_ERR(slot)) { 732 /* If out of memory, try again in 1/4 second */ 733 if (slot == ERR_PTR(-ENOMEM)) 734 task->tk_timeout = HZ >> 2; 735 dprintk("<-- %s: no free slots\n", __func__); 736 goto out_sleep; 737 } 738 spin_unlock(&tbl->slot_tbl_lock); 739 740 args->sa_slot = slot; 741 742 dprintk("<-- %s slotid=%u seqid=%u\n", __func__, 743 slot->slot_nr, slot->seq_nr); 744 745 res->sr_slot = slot; 746 res->sr_timestamp = jiffies; 747 res->sr_status_flags = 0; 748 /* 749 * sr_status is only set in decode_sequence, and so will remain 750 * set to 1 if an rpc level failure occurs. 751 */ 752 res->sr_status = 1; 753 trace_nfs4_setup_sequence(session, args); 754 out_success: 755 rpc_call_start(task); 756 return 0; 757 out_sleep: 758 /* Privileged tasks are queued with top priority */ 759 if (args->sa_privileged) 760 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, 761 NULL, RPC_PRIORITY_PRIVILEGED); 762 else 763 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); 764 spin_unlock(&tbl->slot_tbl_lock); 765 return -EAGAIN; 766 } 767 EXPORT_SYMBOL_GPL(nfs41_setup_sequence); 768 769 static int nfs4_setup_sequence(const struct nfs_server *server, 770 struct nfs4_sequence_args *args, 771 struct nfs4_sequence_res *res, 772 struct rpc_task *task) 773 { 774 struct nfs4_session *session = nfs4_get_session(server); 775 int ret = 0; 776 777 if (!session) 778 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, 779 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->nfs_client->cl_slot_tbl, 821 args, res, task); 822 } 823 824 int nfs4_sequence_done(struct rpc_task *task, 825 struct nfs4_sequence_res *res) 826 { 827 return nfs40_sequence_done(task, res); 828 } 829 EXPORT_SYMBOL_GPL(nfs4_sequence_done); 830 831 #endif /* !CONFIG_NFS_V4_1 */ 832 833 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata) 834 { 835 struct nfs4_call_sync_data *data = calldata; 836 nfs4_setup_sequence(data->seq_server, 837 data->seq_args, data->seq_res, task); 838 } 839 840 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata) 841 { 842 struct nfs4_call_sync_data *data = calldata; 843 nfs4_sequence_done(task, data->seq_res); 844 } 845 846 static const struct rpc_call_ops nfs40_call_sync_ops = { 847 .rpc_call_prepare = nfs40_call_sync_prepare, 848 .rpc_call_done = nfs40_call_sync_done, 849 }; 850 851 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, 852 struct nfs_server *server, 853 struct rpc_message *msg, 854 struct nfs4_sequence_args *args, 855 struct nfs4_sequence_res *res) 856 { 857 int ret; 858 struct rpc_task *task; 859 struct nfs_client *clp = server->nfs_client; 860 struct nfs4_call_sync_data data = { 861 .seq_server = server, 862 .seq_args = args, 863 .seq_res = res, 864 }; 865 struct rpc_task_setup task_setup = { 866 .rpc_client = clnt, 867 .rpc_message = msg, 868 .callback_ops = clp->cl_mvops->call_sync_ops, 869 .callback_data = &data 870 }; 871 872 task = rpc_run_task(&task_setup); 873 if (IS_ERR(task)) 874 ret = PTR_ERR(task); 875 else { 876 ret = task->tk_status; 877 rpc_put_task(task); 878 } 879 return ret; 880 } 881 882 int nfs4_call_sync(struct rpc_clnt *clnt, 883 struct nfs_server *server, 884 struct rpc_message *msg, 885 struct nfs4_sequence_args *args, 886 struct nfs4_sequence_res *res, 887 int cache_reply) 888 { 889 nfs4_init_sequence(args, res, cache_reply); 890 return nfs4_call_sync_sequence(clnt, server, msg, args, res); 891 } 892 893 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo) 894 { 895 struct nfs_inode *nfsi = NFS_I(dir); 896 897 spin_lock(&dir->i_lock); 898 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA; 899 if (!cinfo->atomic || cinfo->before != dir->i_version) 900 nfs_force_lookup_revalidate(dir); 901 dir->i_version = cinfo->after; 902 nfsi->attr_gencount = nfs_inc_attr_generation_counter(); 903 nfs_fscache_invalidate(dir); 904 spin_unlock(&dir->i_lock); 905 } 906 907 struct nfs4_opendata { 908 struct kref kref; 909 struct nfs_openargs o_arg; 910 struct nfs_openres o_res; 911 struct nfs_open_confirmargs c_arg; 912 struct nfs_open_confirmres c_res; 913 struct nfs4_string owner_name; 914 struct nfs4_string group_name; 915 struct nfs4_label *a_label; 916 struct nfs_fattr f_attr; 917 struct nfs4_label *f_label; 918 struct dentry *dir; 919 struct dentry *dentry; 920 struct nfs4_state_owner *owner; 921 struct nfs4_state *state; 922 struct iattr attrs; 923 unsigned long timestamp; 924 unsigned int rpc_done : 1; 925 unsigned int file_created : 1; 926 unsigned int is_recover : 1; 927 int rpc_status; 928 int cancelled; 929 }; 930 931 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, 932 int err, struct nfs4_exception *exception) 933 { 934 if (err != -EINVAL) 935 return false; 936 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) 937 return false; 938 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; 939 exception->retry = 1; 940 return true; 941 } 942 943 static u32 944 nfs4_map_atomic_open_share(struct nfs_server *server, 945 fmode_t fmode, int openflags) 946 { 947 u32 res = 0; 948 949 switch (fmode & (FMODE_READ | FMODE_WRITE)) { 950 case FMODE_READ: 951 res = NFS4_SHARE_ACCESS_READ; 952 break; 953 case FMODE_WRITE: 954 res = NFS4_SHARE_ACCESS_WRITE; 955 break; 956 case FMODE_READ|FMODE_WRITE: 957 res = NFS4_SHARE_ACCESS_BOTH; 958 } 959 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) 960 goto out; 961 /* Want no delegation if we're using O_DIRECT */ 962 if (openflags & O_DIRECT) 963 res |= NFS4_SHARE_WANT_NO_DELEG; 964 out: 965 return res; 966 } 967 968 static enum open_claim_type4 969 nfs4_map_atomic_open_claim(struct nfs_server *server, 970 enum open_claim_type4 claim) 971 { 972 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) 973 return claim; 974 switch (claim) { 975 default: 976 return claim; 977 case NFS4_OPEN_CLAIM_FH: 978 return NFS4_OPEN_CLAIM_NULL; 979 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 980 return NFS4_OPEN_CLAIM_DELEGATE_CUR; 981 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 982 return NFS4_OPEN_CLAIM_DELEGATE_PREV; 983 } 984 } 985 986 static void nfs4_init_opendata_res(struct nfs4_opendata *p) 987 { 988 p->o_res.f_attr = &p->f_attr; 989 p->o_res.f_label = p->f_label; 990 p->o_res.seqid = p->o_arg.seqid; 991 p->c_res.seqid = p->c_arg.seqid; 992 p->o_res.server = p->o_arg.server; 993 p->o_res.access_request = p->o_arg.access; 994 nfs_fattr_init(&p->f_attr); 995 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); 996 } 997 998 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, 999 struct nfs4_state_owner *sp, fmode_t fmode, int flags, 1000 const struct iattr *attrs, 1001 struct nfs4_label *label, 1002 enum open_claim_type4 claim, 1003 gfp_t gfp_mask) 1004 { 1005 struct dentry *parent = dget_parent(dentry); 1006 struct inode *dir = d_inode(parent); 1007 struct nfs_server *server = NFS_SERVER(dir); 1008 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 1009 struct nfs4_opendata *p; 1010 1011 p = kzalloc(sizeof(*p), gfp_mask); 1012 if (p == NULL) 1013 goto err; 1014 1015 p->f_label = nfs4_label_alloc(server, gfp_mask); 1016 if (IS_ERR(p->f_label)) 1017 goto err_free_p; 1018 1019 p->a_label = nfs4_label_alloc(server, gfp_mask); 1020 if (IS_ERR(p->a_label)) 1021 goto err_free_f; 1022 1023 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 1024 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask); 1025 if (IS_ERR(p->o_arg.seqid)) 1026 goto err_free_label; 1027 nfs_sb_active(dentry->d_sb); 1028 p->dentry = dget(dentry); 1029 p->dir = parent; 1030 p->owner = sp; 1031 atomic_inc(&sp->so_count); 1032 p->o_arg.open_flags = flags; 1033 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); 1034 p->o_arg.share_access = nfs4_map_atomic_open_share(server, 1035 fmode, flags); 1036 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS 1037 * will return permission denied for all bits until close */ 1038 if (!(flags & O_EXCL)) { 1039 /* ask server to check for all possible rights as results 1040 * are cached */ 1041 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY | 1042 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE; 1043 } 1044 p->o_arg.clientid = server->nfs_client->cl_clientid; 1045 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); 1046 p->o_arg.id.uniquifier = sp->so_seqid.owner_id; 1047 p->o_arg.name = &dentry->d_name; 1048 p->o_arg.server = server; 1049 p->o_arg.bitmask = nfs4_bitmask(server, label); 1050 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; 1051 p->o_arg.label = nfs4_label_copy(p->a_label, label); 1052 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); 1053 switch (p->o_arg.claim) { 1054 case NFS4_OPEN_CLAIM_NULL: 1055 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 1056 case NFS4_OPEN_CLAIM_DELEGATE_PREV: 1057 p->o_arg.fh = NFS_FH(dir); 1058 break; 1059 case NFS4_OPEN_CLAIM_PREVIOUS: 1060 case NFS4_OPEN_CLAIM_FH: 1061 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1062 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 1063 p->o_arg.fh = NFS_FH(d_inode(dentry)); 1064 } 1065 if (attrs != NULL && attrs->ia_valid != 0) { 1066 __u32 verf[2]; 1067 1068 p->o_arg.u.attrs = &p->attrs; 1069 memcpy(&p->attrs, attrs, sizeof(p->attrs)); 1070 1071 verf[0] = jiffies; 1072 verf[1] = current->pid; 1073 memcpy(p->o_arg.u.verifier.data, verf, 1074 sizeof(p->o_arg.u.verifier.data)); 1075 } 1076 p->c_arg.fh = &p->o_res.fh; 1077 p->c_arg.stateid = &p->o_res.stateid; 1078 p->c_arg.seqid = p->o_arg.seqid; 1079 nfs4_init_opendata_res(p); 1080 kref_init(&p->kref); 1081 return p; 1082 1083 err_free_label: 1084 nfs4_label_free(p->a_label); 1085 err_free_f: 1086 nfs4_label_free(p->f_label); 1087 err_free_p: 1088 kfree(p); 1089 err: 1090 dput(parent); 1091 return NULL; 1092 } 1093 1094 static void nfs4_opendata_free(struct kref *kref) 1095 { 1096 struct nfs4_opendata *p = container_of(kref, 1097 struct nfs4_opendata, kref); 1098 struct super_block *sb = p->dentry->d_sb; 1099 1100 nfs_free_seqid(p->o_arg.seqid); 1101 if (p->state != NULL) 1102 nfs4_put_open_state(p->state); 1103 nfs4_put_state_owner(p->owner); 1104 1105 nfs4_label_free(p->a_label); 1106 nfs4_label_free(p->f_label); 1107 1108 dput(p->dir); 1109 dput(p->dentry); 1110 nfs_sb_deactive(sb); 1111 nfs_fattr_free_names(&p->f_attr); 1112 kfree(p->f_attr.mdsthreshold); 1113 kfree(p); 1114 } 1115 1116 static void nfs4_opendata_put(struct nfs4_opendata *p) 1117 { 1118 if (p != NULL) 1119 kref_put(&p->kref, nfs4_opendata_free); 1120 } 1121 1122 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) 1123 { 1124 int ret; 1125 1126 ret = rpc_wait_for_completion_task(task); 1127 return ret; 1128 } 1129 1130 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state, 1131 fmode_t fmode) 1132 { 1133 switch(fmode & (FMODE_READ|FMODE_WRITE)) { 1134 case FMODE_READ|FMODE_WRITE: 1135 return state->n_rdwr != 0; 1136 case FMODE_WRITE: 1137 return state->n_wronly != 0; 1138 case FMODE_READ: 1139 return state->n_rdonly != 0; 1140 } 1141 WARN_ON_ONCE(1); 1142 return false; 1143 } 1144 1145 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode) 1146 { 1147 int ret = 0; 1148 1149 if (open_mode & (O_EXCL|O_TRUNC)) 1150 goto out; 1151 switch (mode & (FMODE_READ|FMODE_WRITE)) { 1152 case FMODE_READ: 1153 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 1154 && state->n_rdonly != 0; 1155 break; 1156 case FMODE_WRITE: 1157 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 1158 && state->n_wronly != 0; 1159 break; 1160 case FMODE_READ|FMODE_WRITE: 1161 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 1162 && state->n_rdwr != 0; 1163 } 1164 out: 1165 return ret; 1166 } 1167 1168 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode) 1169 { 1170 if (delegation == NULL) 1171 return 0; 1172 if ((delegation->type & fmode) != fmode) 1173 return 0; 1174 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) 1175 return 0; 1176 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 1177 return 0; 1178 nfs_mark_delegation_referenced(delegation); 1179 return 1; 1180 } 1181 1182 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) 1183 { 1184 switch (fmode) { 1185 case FMODE_WRITE: 1186 state->n_wronly++; 1187 break; 1188 case FMODE_READ: 1189 state->n_rdonly++; 1190 break; 1191 case FMODE_READ|FMODE_WRITE: 1192 state->n_rdwr++; 1193 } 1194 nfs4_state_set_mode_locked(state, state->state | fmode); 1195 } 1196 1197 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state) 1198 { 1199 struct nfs_client *clp = state->owner->so_server->nfs_client; 1200 bool need_recover = false; 1201 1202 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly) 1203 need_recover = true; 1204 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly) 1205 need_recover = true; 1206 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr) 1207 need_recover = true; 1208 if (need_recover) 1209 nfs4_state_mark_reclaim_nograce(clp, state); 1210 } 1211 1212 static bool nfs_need_update_open_stateid(struct nfs4_state *state, 1213 nfs4_stateid *stateid) 1214 { 1215 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0) 1216 return true; 1217 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) { 1218 nfs_test_and_clear_all_open_stateid(state); 1219 return true; 1220 } 1221 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) 1222 return true; 1223 return false; 1224 } 1225 1226 static void nfs_resync_open_stateid_locked(struct nfs4_state *state) 1227 { 1228 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr)) 1229 return; 1230 if (state->n_wronly) 1231 set_bit(NFS_O_WRONLY_STATE, &state->flags); 1232 if (state->n_rdonly) 1233 set_bit(NFS_O_RDONLY_STATE, &state->flags); 1234 if (state->n_rdwr) 1235 set_bit(NFS_O_RDWR_STATE, &state->flags); 1236 set_bit(NFS_OPEN_STATE, &state->flags); 1237 } 1238 1239 static void nfs_clear_open_stateid_locked(struct nfs4_state *state, 1240 nfs4_stateid *arg_stateid, 1241 nfs4_stateid *stateid, fmode_t fmode) 1242 { 1243 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1244 switch (fmode & (FMODE_READ|FMODE_WRITE)) { 1245 case FMODE_WRITE: 1246 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1247 break; 1248 case FMODE_READ: 1249 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1250 break; 1251 case 0: 1252 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1253 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1254 clear_bit(NFS_OPEN_STATE, &state->flags); 1255 } 1256 if (stateid == NULL) 1257 return; 1258 /* Handle races with OPEN */ 1259 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) || 1260 (nfs4_stateid_match_other(stateid, &state->open_stateid) && 1261 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) { 1262 nfs_resync_open_stateid_locked(state); 1263 return; 1264 } 1265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1266 nfs4_stateid_copy(&state->stateid, stateid); 1267 nfs4_stateid_copy(&state->open_stateid, stateid); 1268 } 1269 1270 static void nfs_clear_open_stateid(struct nfs4_state *state, 1271 nfs4_stateid *arg_stateid, 1272 nfs4_stateid *stateid, fmode_t fmode) 1273 { 1274 write_seqlock(&state->seqlock); 1275 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode); 1276 write_sequnlock(&state->seqlock); 1277 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) 1278 nfs4_schedule_state_manager(state->owner->so_server->nfs_client); 1279 } 1280 1281 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) 1282 { 1283 switch (fmode) { 1284 case FMODE_READ: 1285 set_bit(NFS_O_RDONLY_STATE, &state->flags); 1286 break; 1287 case FMODE_WRITE: 1288 set_bit(NFS_O_WRONLY_STATE, &state->flags); 1289 break; 1290 case FMODE_READ|FMODE_WRITE: 1291 set_bit(NFS_O_RDWR_STATE, &state->flags); 1292 } 1293 if (!nfs_need_update_open_stateid(state, stateid)) 1294 return; 1295 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1296 nfs4_stateid_copy(&state->stateid, stateid); 1297 nfs4_stateid_copy(&state->open_stateid, stateid); 1298 } 1299 1300 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode) 1301 { 1302 /* 1303 * Protect the call to nfs4_state_set_mode_locked and 1304 * serialise the stateid update 1305 */ 1306 write_seqlock(&state->seqlock); 1307 if (deleg_stateid != NULL) { 1308 nfs4_stateid_copy(&state->stateid, deleg_stateid); 1309 set_bit(NFS_DELEGATED_STATE, &state->flags); 1310 } 1311 if (open_stateid != NULL) 1312 nfs_set_open_stateid_locked(state, open_stateid, fmode); 1313 write_sequnlock(&state->seqlock); 1314 spin_lock(&state->owner->so_lock); 1315 update_open_stateflags(state, fmode); 1316 spin_unlock(&state->owner->so_lock); 1317 } 1318 1319 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode) 1320 { 1321 struct nfs_inode *nfsi = NFS_I(state->inode); 1322 struct nfs_delegation *deleg_cur; 1323 int ret = 0; 1324 1325 fmode &= (FMODE_READ|FMODE_WRITE); 1326 1327 rcu_read_lock(); 1328 deleg_cur = rcu_dereference(nfsi->delegation); 1329 if (deleg_cur == NULL) 1330 goto no_delegation; 1331 1332 spin_lock(&deleg_cur->lock); 1333 if (rcu_dereference(nfsi->delegation) != deleg_cur || 1334 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || 1335 (deleg_cur->type & fmode) != fmode) 1336 goto no_delegation_unlock; 1337 1338 if (delegation == NULL) 1339 delegation = &deleg_cur->stateid; 1340 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation)) 1341 goto no_delegation_unlock; 1342 1343 nfs_mark_delegation_referenced(deleg_cur); 1344 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode); 1345 ret = 1; 1346 no_delegation_unlock: 1347 spin_unlock(&deleg_cur->lock); 1348 no_delegation: 1349 rcu_read_unlock(); 1350 1351 if (!ret && open_stateid != NULL) { 1352 __update_open_stateid(state, open_stateid, NULL, fmode); 1353 ret = 1; 1354 } 1355 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) 1356 nfs4_schedule_state_manager(state->owner->so_server->nfs_client); 1357 1358 return ret; 1359 } 1360 1361 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp, 1362 const nfs4_stateid *stateid) 1363 { 1364 struct nfs4_state *state = lsp->ls_state; 1365 bool ret = false; 1366 1367 spin_lock(&state->state_lock); 1368 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid)) 1369 goto out_noupdate; 1370 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid)) 1371 goto out_noupdate; 1372 nfs4_stateid_copy(&lsp->ls_stateid, stateid); 1373 ret = true; 1374 out_noupdate: 1375 spin_unlock(&state->state_lock); 1376 return ret; 1377 } 1378 1379 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) 1380 { 1381 struct nfs_delegation *delegation; 1382 1383 rcu_read_lock(); 1384 delegation = rcu_dereference(NFS_I(inode)->delegation); 1385 if (delegation == NULL || (delegation->type & fmode) == fmode) { 1386 rcu_read_unlock(); 1387 return; 1388 } 1389 rcu_read_unlock(); 1390 nfs4_inode_return_delegation(inode); 1391 } 1392 1393 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) 1394 { 1395 struct nfs4_state *state = opendata->state; 1396 struct nfs_inode *nfsi = NFS_I(state->inode); 1397 struct nfs_delegation *delegation; 1398 int open_mode = opendata->o_arg.open_flags; 1399 fmode_t fmode = opendata->o_arg.fmode; 1400 nfs4_stateid stateid; 1401 int ret = -EAGAIN; 1402 1403 for (;;) { 1404 spin_lock(&state->owner->so_lock); 1405 if (can_open_cached(state, fmode, open_mode)) { 1406 update_open_stateflags(state, fmode); 1407 spin_unlock(&state->owner->so_lock); 1408 goto out_return_state; 1409 } 1410 spin_unlock(&state->owner->so_lock); 1411 rcu_read_lock(); 1412 delegation = rcu_dereference(nfsi->delegation); 1413 if (!can_open_delegated(delegation, fmode)) { 1414 rcu_read_unlock(); 1415 break; 1416 } 1417 /* Save the delegation */ 1418 nfs4_stateid_copy(&stateid, &delegation->stateid); 1419 rcu_read_unlock(); 1420 nfs_release_seqid(opendata->o_arg.seqid); 1421 if (!opendata->is_recover) { 1422 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); 1423 if (ret != 0) 1424 goto out; 1425 } 1426 ret = -EAGAIN; 1427 1428 /* Try to update the stateid using the delegation */ 1429 if (update_open_stateid(state, NULL, &stateid, fmode)) 1430 goto out_return_state; 1431 } 1432 out: 1433 return ERR_PTR(ret); 1434 out_return_state: 1435 atomic_inc(&state->count); 1436 return state; 1437 } 1438 1439 static void 1440 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) 1441 { 1442 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; 1443 struct nfs_delegation *delegation; 1444 int delegation_flags = 0; 1445 1446 rcu_read_lock(); 1447 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 1448 if (delegation) 1449 delegation_flags = delegation->flags; 1450 rcu_read_unlock(); 1451 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) { 1452 pr_err_ratelimited("NFS: Broken NFSv4 server %s is " 1453 "returning a delegation for " 1454 "OPEN(CLAIM_DELEGATE_CUR)\n", 1455 clp->cl_hostname); 1456 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) 1457 nfs_inode_set_delegation(state->inode, 1458 data->owner->so_cred, 1459 &data->o_res); 1460 else 1461 nfs_inode_reclaim_delegation(state->inode, 1462 data->owner->so_cred, 1463 &data->o_res); 1464 } 1465 1466 /* 1467 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes 1468 * and update the nfs4_state. 1469 */ 1470 static struct nfs4_state * 1471 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) 1472 { 1473 struct inode *inode = data->state->inode; 1474 struct nfs4_state *state = data->state; 1475 int ret; 1476 1477 if (!data->rpc_done) { 1478 if (data->rpc_status) { 1479 ret = data->rpc_status; 1480 goto err; 1481 } 1482 /* cached opens have already been processed */ 1483 goto update; 1484 } 1485 1486 ret = nfs_refresh_inode(inode, &data->f_attr); 1487 if (ret) 1488 goto err; 1489 1490 if (data->o_res.delegation_type != 0) 1491 nfs4_opendata_check_deleg(data, state); 1492 update: 1493 update_open_stateid(state, &data->o_res.stateid, NULL, 1494 data->o_arg.fmode); 1495 atomic_inc(&state->count); 1496 1497 return state; 1498 err: 1499 return ERR_PTR(ret); 1500 1501 } 1502 1503 static struct nfs4_state * 1504 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) 1505 { 1506 struct inode *inode; 1507 struct nfs4_state *state = NULL; 1508 int ret; 1509 1510 if (!data->rpc_done) { 1511 state = nfs4_try_open_cached(data); 1512 goto out; 1513 } 1514 1515 ret = -EAGAIN; 1516 if (!(data->f_attr.valid & NFS_ATTR_FATTR)) 1517 goto err; 1518 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label); 1519 ret = PTR_ERR(inode); 1520 if (IS_ERR(inode)) 1521 goto err; 1522 ret = -ENOMEM; 1523 state = nfs4_get_open_state(inode, data->owner); 1524 if (state == NULL) 1525 goto err_put_inode; 1526 if (data->o_res.delegation_type != 0) 1527 nfs4_opendata_check_deleg(data, state); 1528 update_open_stateid(state, &data->o_res.stateid, NULL, 1529 data->o_arg.fmode); 1530 iput(inode); 1531 out: 1532 nfs_release_seqid(data->o_arg.seqid); 1533 return state; 1534 err_put_inode: 1535 iput(inode); 1536 err: 1537 return ERR_PTR(ret); 1538 } 1539 1540 static struct nfs4_state * 1541 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) 1542 { 1543 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) 1544 return _nfs4_opendata_reclaim_to_nfs4_state(data); 1545 return _nfs4_opendata_to_nfs4_state(data); 1546 } 1547 1548 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state) 1549 { 1550 struct nfs_inode *nfsi = NFS_I(state->inode); 1551 struct nfs_open_context *ctx; 1552 1553 spin_lock(&state->inode->i_lock); 1554 list_for_each_entry(ctx, &nfsi->open_files, list) { 1555 if (ctx->state != state) 1556 continue; 1557 get_nfs_open_context(ctx); 1558 spin_unlock(&state->inode->i_lock); 1559 return ctx; 1560 } 1561 spin_unlock(&state->inode->i_lock); 1562 return ERR_PTR(-ENOENT); 1563 } 1564 1565 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, 1566 struct nfs4_state *state, enum open_claim_type4 claim) 1567 { 1568 struct nfs4_opendata *opendata; 1569 1570 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, 1571 NULL, NULL, claim, GFP_NOFS); 1572 if (opendata == NULL) 1573 return ERR_PTR(-ENOMEM); 1574 opendata->state = state; 1575 atomic_inc(&state->count); 1576 return opendata; 1577 } 1578 1579 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, 1580 fmode_t fmode) 1581 { 1582 struct nfs4_state *newstate; 1583 int ret; 1584 1585 if (!nfs4_mode_match_open_stateid(opendata->state, fmode)) 1586 return 0; 1587 opendata->o_arg.open_flags = 0; 1588 opendata->o_arg.fmode = fmode; 1589 opendata->o_arg.share_access = nfs4_map_atomic_open_share( 1590 NFS_SB(opendata->dentry->d_sb), 1591 fmode, 0); 1592 memset(&opendata->o_res, 0, sizeof(opendata->o_res)); 1593 memset(&opendata->c_res, 0, sizeof(opendata->c_res)); 1594 nfs4_init_opendata_res(opendata); 1595 ret = _nfs4_recover_proc_open(opendata); 1596 if (ret != 0) 1597 return ret; 1598 newstate = nfs4_opendata_to_nfs4_state(opendata); 1599 if (IS_ERR(newstate)) 1600 return PTR_ERR(newstate); 1601 if (newstate != opendata->state) 1602 ret = -ESTALE; 1603 nfs4_close_state(newstate, fmode); 1604 return ret; 1605 } 1606 1607 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) 1608 { 1609 int ret; 1610 1611 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */ 1612 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1613 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1614 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1615 /* memory barrier prior to reading state->n_* */ 1616 clear_bit(NFS_DELEGATED_STATE, &state->flags); 1617 clear_bit(NFS_OPEN_STATE, &state->flags); 1618 smp_rmb(); 1619 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); 1620 if (ret != 0) 1621 return ret; 1622 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE); 1623 if (ret != 0) 1624 return ret; 1625 ret = nfs4_open_recover_helper(opendata, FMODE_READ); 1626 if (ret != 0) 1627 return ret; 1628 /* 1629 * We may have performed cached opens for all three recoveries. 1630 * Check if we need to update the current stateid. 1631 */ 1632 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && 1633 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { 1634 write_seqlock(&state->seqlock); 1635 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1636 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 1637 write_sequnlock(&state->seqlock); 1638 } 1639 return 0; 1640 } 1641 1642 /* 1643 * OPEN_RECLAIM: 1644 * reclaim state on the server after a reboot. 1645 */ 1646 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) 1647 { 1648 struct nfs_delegation *delegation; 1649 struct nfs4_opendata *opendata; 1650 fmode_t delegation_type = 0; 1651 int status; 1652 1653 opendata = nfs4_open_recoverdata_alloc(ctx, state, 1654 NFS4_OPEN_CLAIM_PREVIOUS); 1655 if (IS_ERR(opendata)) 1656 return PTR_ERR(opendata); 1657 rcu_read_lock(); 1658 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 1659 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) 1660 delegation_type = delegation->type; 1661 rcu_read_unlock(); 1662 opendata->o_arg.u.delegation_type = delegation_type; 1663 status = nfs4_open_recover(opendata, state); 1664 nfs4_opendata_put(opendata); 1665 return status; 1666 } 1667 1668 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) 1669 { 1670 struct nfs_server *server = NFS_SERVER(state->inode); 1671 struct nfs4_exception exception = { }; 1672 int err; 1673 do { 1674 err = _nfs4_do_open_reclaim(ctx, state); 1675 trace_nfs4_open_reclaim(ctx, 0, err); 1676 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) 1677 continue; 1678 if (err != -NFS4ERR_DELAY) 1679 break; 1680 nfs4_handle_exception(server, err, &exception); 1681 } while (exception.retry); 1682 return err; 1683 } 1684 1685 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) 1686 { 1687 struct nfs_open_context *ctx; 1688 int ret; 1689 1690 ctx = nfs4_state_find_open_context(state); 1691 if (IS_ERR(ctx)) 1692 return -EAGAIN; 1693 ret = nfs4_do_open_reclaim(ctx, state); 1694 put_nfs_open_context(ctx); 1695 return ret; 1696 } 1697 1698 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err) 1699 { 1700 switch (err) { 1701 default: 1702 printk(KERN_ERR "NFS: %s: unhandled error " 1703 "%d.\n", __func__, err); 1704 case 0: 1705 case -ENOENT: 1706 case -EAGAIN: 1707 case -ESTALE: 1708 break; 1709 case -NFS4ERR_BADSESSION: 1710 case -NFS4ERR_BADSLOT: 1711 case -NFS4ERR_BAD_HIGH_SLOT: 1712 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1713 case -NFS4ERR_DEADSESSION: 1714 set_bit(NFS_DELEGATED_STATE, &state->flags); 1715 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err); 1716 return -EAGAIN; 1717 case -NFS4ERR_STALE_CLIENTID: 1718 case -NFS4ERR_STALE_STATEID: 1719 set_bit(NFS_DELEGATED_STATE, &state->flags); 1720 case -NFS4ERR_EXPIRED: 1721 /* Don't recall a delegation if it was lost */ 1722 nfs4_schedule_lease_recovery(server->nfs_client); 1723 return -EAGAIN; 1724 case -NFS4ERR_MOVED: 1725 nfs4_schedule_migration_recovery(server); 1726 return -EAGAIN; 1727 case -NFS4ERR_LEASE_MOVED: 1728 nfs4_schedule_lease_moved_recovery(server->nfs_client); 1729 return -EAGAIN; 1730 case -NFS4ERR_DELEG_REVOKED: 1731 case -NFS4ERR_ADMIN_REVOKED: 1732 case -NFS4ERR_BAD_STATEID: 1733 case -NFS4ERR_OPENMODE: 1734 nfs_inode_find_state_and_recover(state->inode, 1735 stateid); 1736 nfs4_schedule_stateid_recovery(server, state); 1737 return -EAGAIN; 1738 case -NFS4ERR_DELAY: 1739 case -NFS4ERR_GRACE: 1740 set_bit(NFS_DELEGATED_STATE, &state->flags); 1741 ssleep(1); 1742 return -EAGAIN; 1743 case -ENOMEM: 1744 case -NFS4ERR_DENIED: 1745 /* kill_proc(fl->fl_pid, SIGLOST, 1); */ 1746 return 0; 1747 } 1748 return err; 1749 } 1750 1751 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, 1752 struct nfs4_state *state, const nfs4_stateid *stateid, 1753 fmode_t type) 1754 { 1755 struct nfs_server *server = NFS_SERVER(state->inode); 1756 struct nfs4_opendata *opendata; 1757 int err = 0; 1758 1759 opendata = nfs4_open_recoverdata_alloc(ctx, state, 1760 NFS4_OPEN_CLAIM_DELEG_CUR_FH); 1761 if (IS_ERR(opendata)) 1762 return PTR_ERR(opendata); 1763 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); 1764 clear_bit(NFS_DELEGATED_STATE, &state->flags); 1765 switch (type & (FMODE_READ|FMODE_WRITE)) { 1766 case FMODE_READ|FMODE_WRITE: 1767 case FMODE_WRITE: 1768 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); 1769 if (err) 1770 break; 1771 err = nfs4_open_recover_helper(opendata, FMODE_WRITE); 1772 if (err) 1773 break; 1774 case FMODE_READ: 1775 err = nfs4_open_recover_helper(opendata, FMODE_READ); 1776 } 1777 nfs4_opendata_put(opendata); 1778 return nfs4_handle_delegation_recall_error(server, state, stateid, err); 1779 } 1780 1781 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) 1782 { 1783 struct nfs4_opendata *data = calldata; 1784 1785 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl, 1786 &data->c_arg.seq_args, &data->c_res.seq_res, task); 1787 } 1788 1789 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) 1790 { 1791 struct nfs4_opendata *data = calldata; 1792 1793 nfs40_sequence_done(task, &data->c_res.seq_res); 1794 1795 data->rpc_status = task->tk_status; 1796 if (data->rpc_status == 0) { 1797 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); 1798 nfs_confirm_seqid(&data->owner->so_seqid, 0); 1799 renew_lease(data->o_res.server, data->timestamp); 1800 data->rpc_done = 1; 1801 } 1802 } 1803 1804 static void nfs4_open_confirm_release(void *calldata) 1805 { 1806 struct nfs4_opendata *data = calldata; 1807 struct nfs4_state *state = NULL; 1808 1809 /* If this request hasn't been cancelled, do nothing */ 1810 if (data->cancelled == 0) 1811 goto out_free; 1812 /* In case of error, no cleanup! */ 1813 if (!data->rpc_done) 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_confirm_ops = { 1823 .rpc_call_prepare = nfs4_open_confirm_prepare, 1824 .rpc_call_done = nfs4_open_confirm_done, 1825 .rpc_release = nfs4_open_confirm_release, 1826 }; 1827 1828 /* 1829 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata 1830 */ 1831 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) 1832 { 1833 struct nfs_server *server = NFS_SERVER(d_inode(data->dir)); 1834 struct rpc_task *task; 1835 struct rpc_message msg = { 1836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], 1837 .rpc_argp = &data->c_arg, 1838 .rpc_resp = &data->c_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_confirm_ops, 1845 .callback_data = data, 1846 .workqueue = nfsiod_workqueue, 1847 .flags = RPC_TASK_ASYNC, 1848 }; 1849 int status; 1850 1851 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1); 1852 kref_get(&data->kref); 1853 data->rpc_done = 0; 1854 data->rpc_status = 0; 1855 data->timestamp = jiffies; 1856 task = rpc_run_task(&task_setup_data); 1857 if (IS_ERR(task)) 1858 return PTR_ERR(task); 1859 status = nfs4_wait_for_completion_rpc_task(task); 1860 if (status != 0) { 1861 data->cancelled = 1; 1862 smp_wmb(); 1863 } else 1864 status = data->rpc_status; 1865 rpc_put_task(task); 1866 return status; 1867 } 1868 1869 static void nfs4_open_prepare(struct rpc_task *task, void *calldata) 1870 { 1871 struct nfs4_opendata *data = calldata; 1872 struct nfs4_state_owner *sp = data->owner; 1873 struct nfs_client *clp = sp->so_server->nfs_client; 1874 1875 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) 1876 goto out_wait; 1877 /* 1878 * Check if we still need to send an OPEN call, or if we can use 1879 * a delegation instead. 1880 */ 1881 if (data->state != NULL) { 1882 struct nfs_delegation *delegation; 1883 1884 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags)) 1885 goto out_no_action; 1886 rcu_read_lock(); 1887 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation); 1888 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR && 1889 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH && 1890 can_open_delegated(delegation, data->o_arg.fmode)) 1891 goto unlock_no_action; 1892 rcu_read_unlock(); 1893 } 1894 /* Update client id. */ 1895 data->o_arg.clientid = clp->cl_clientid; 1896 switch (data->o_arg.claim) { 1897 case NFS4_OPEN_CLAIM_PREVIOUS: 1898 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1899 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 1900 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; 1901 case NFS4_OPEN_CLAIM_FH: 1902 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; 1903 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh); 1904 } 1905 data->timestamp = jiffies; 1906 if (nfs4_setup_sequence(data->o_arg.server, 1907 &data->o_arg.seq_args, 1908 &data->o_res.seq_res, 1909 task) != 0) 1910 nfs_release_seqid(data->o_arg.seqid); 1911 1912 /* Set the create mode (note dependency on the session type) */ 1913 data->o_arg.createmode = NFS4_CREATE_UNCHECKED; 1914 if (data->o_arg.open_flags & O_EXCL) { 1915 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE; 1916 if (nfs4_has_persistent_session(clp)) 1917 data->o_arg.createmode = NFS4_CREATE_GUARDED; 1918 else if (clp->cl_mvops->minor_version > 0) 1919 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1; 1920 } 1921 return; 1922 unlock_no_action: 1923 rcu_read_unlock(); 1924 out_no_action: 1925 task->tk_action = NULL; 1926 out_wait: 1927 nfs4_sequence_done(task, &data->o_res.seq_res); 1928 } 1929 1930 static void nfs4_open_done(struct rpc_task *task, void *calldata) 1931 { 1932 struct nfs4_opendata *data = calldata; 1933 1934 data->rpc_status = task->tk_status; 1935 1936 if (!nfs4_sequence_done(task, &data->o_res.seq_res)) 1937 return; 1938 1939 if (task->tk_status == 0) { 1940 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { 1941 switch (data->o_res.f_attr->mode & S_IFMT) { 1942 case S_IFREG: 1943 break; 1944 case S_IFLNK: 1945 data->rpc_status = -ELOOP; 1946 break; 1947 case S_IFDIR: 1948 data->rpc_status = -EISDIR; 1949 break; 1950 default: 1951 data->rpc_status = -ENOTDIR; 1952 } 1953 } 1954 renew_lease(data->o_res.server, data->timestamp); 1955 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) 1956 nfs_confirm_seqid(&data->owner->so_seqid, 0); 1957 } 1958 data->rpc_done = 1; 1959 } 1960 1961 static void nfs4_open_release(void *calldata) 1962 { 1963 struct nfs4_opendata *data = calldata; 1964 struct nfs4_state *state = NULL; 1965 1966 /* If this request hasn't been cancelled, do nothing */ 1967 if (data->cancelled == 0) 1968 goto out_free; 1969 /* In case of error, no cleanup! */ 1970 if (data->rpc_status != 0 || !data->rpc_done) 1971 goto out_free; 1972 /* In case we need an open_confirm, no cleanup! */ 1973 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) 1974 goto out_free; 1975 state = nfs4_opendata_to_nfs4_state(data); 1976 if (!IS_ERR(state)) 1977 nfs4_close_state(state, data->o_arg.fmode); 1978 out_free: 1979 nfs4_opendata_put(data); 1980 } 1981 1982 static const struct rpc_call_ops nfs4_open_ops = { 1983 .rpc_call_prepare = nfs4_open_prepare, 1984 .rpc_call_done = nfs4_open_done, 1985 .rpc_release = nfs4_open_release, 1986 }; 1987 1988 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover) 1989 { 1990 struct inode *dir = d_inode(data->dir); 1991 struct nfs_server *server = NFS_SERVER(dir); 1992 struct nfs_openargs *o_arg = &data->o_arg; 1993 struct nfs_openres *o_res = &data->o_res; 1994 struct rpc_task *task; 1995 struct rpc_message msg = { 1996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], 1997 .rpc_argp = o_arg, 1998 .rpc_resp = o_res, 1999 .rpc_cred = data->owner->so_cred, 2000 }; 2001 struct rpc_task_setup task_setup_data = { 2002 .rpc_client = server->client, 2003 .rpc_message = &msg, 2004 .callback_ops = &nfs4_open_ops, 2005 .callback_data = data, 2006 .workqueue = nfsiod_workqueue, 2007 .flags = RPC_TASK_ASYNC, 2008 }; 2009 int status; 2010 2011 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1); 2012 kref_get(&data->kref); 2013 data->rpc_done = 0; 2014 data->rpc_status = 0; 2015 data->cancelled = 0; 2016 data->is_recover = 0; 2017 if (isrecover) { 2018 nfs4_set_sequence_privileged(&o_arg->seq_args); 2019 data->is_recover = 1; 2020 } 2021 task = rpc_run_task(&task_setup_data); 2022 if (IS_ERR(task)) 2023 return PTR_ERR(task); 2024 status = nfs4_wait_for_completion_rpc_task(task); 2025 if (status != 0) { 2026 data->cancelled = 1; 2027 smp_wmb(); 2028 } else 2029 status = data->rpc_status; 2030 rpc_put_task(task); 2031 2032 return status; 2033 } 2034 2035 static int _nfs4_recover_proc_open(struct nfs4_opendata *data) 2036 { 2037 struct inode *dir = d_inode(data->dir); 2038 struct nfs_openres *o_res = &data->o_res; 2039 int status; 2040 2041 status = nfs4_run_open_task(data, 1); 2042 if (status != 0 || !data->rpc_done) 2043 return status; 2044 2045 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); 2046 2047 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { 2048 status = _nfs4_proc_open_confirm(data); 2049 if (status != 0) 2050 return status; 2051 } 2052 2053 return status; 2054 } 2055 2056 /* 2057 * Additional permission checks in order to distinguish between an 2058 * open for read, and an open for execute. This works around the 2059 * fact that NFSv4 OPEN treats read and execute permissions as being 2060 * the same. 2061 * Note that in the non-execute case, we want to turn off permission 2062 * checking if we just created a new file (POSIX open() semantics). 2063 */ 2064 static int nfs4_opendata_access(struct rpc_cred *cred, 2065 struct nfs4_opendata *opendata, 2066 struct nfs4_state *state, fmode_t fmode, 2067 int openflags) 2068 { 2069 struct nfs_access_entry cache; 2070 u32 mask; 2071 2072 /* access call failed or for some reason the server doesn't 2073 * support any access modes -- defer access call until later */ 2074 if (opendata->o_res.access_supported == 0) 2075 return 0; 2076 2077 mask = 0; 2078 /* 2079 * Use openflags to check for exec, because fmode won't 2080 * always have FMODE_EXEC set when file open for exec. 2081 */ 2082 if (openflags & __FMODE_EXEC) { 2083 /* ONLY check for exec rights */ 2084 mask = MAY_EXEC; 2085 } else if ((fmode & FMODE_READ) && !opendata->file_created) 2086 mask = MAY_READ; 2087 2088 cache.cred = cred; 2089 cache.jiffies = jiffies; 2090 nfs_access_set_mask(&cache, opendata->o_res.access_result); 2091 nfs_access_add_cache(state->inode, &cache); 2092 2093 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0) 2094 return 0; 2095 2096 /* even though OPEN succeeded, access is denied. Close the file */ 2097 nfs4_close_state(state, fmode); 2098 return -EACCES; 2099 } 2100 2101 /* 2102 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata 2103 */ 2104 static int _nfs4_proc_open(struct nfs4_opendata *data) 2105 { 2106 struct inode *dir = d_inode(data->dir); 2107 struct nfs_server *server = NFS_SERVER(dir); 2108 struct nfs_openargs *o_arg = &data->o_arg; 2109 struct nfs_openres *o_res = &data->o_res; 2110 int status; 2111 2112 status = nfs4_run_open_task(data, 0); 2113 if (!data->rpc_done) 2114 return status; 2115 if (status != 0) { 2116 if (status == -NFS4ERR_BADNAME && 2117 !(o_arg->open_flags & O_CREAT)) 2118 return -ENOENT; 2119 return status; 2120 } 2121 2122 nfs_fattr_map_and_free_names(server, &data->f_attr); 2123 2124 if (o_arg->open_flags & O_CREAT) { 2125 update_changeattr(dir, &o_res->cinfo); 2126 if (o_arg->open_flags & O_EXCL) 2127 data->file_created = 1; 2128 else if (o_res->cinfo.before != o_res->cinfo.after) 2129 data->file_created = 1; 2130 } 2131 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) 2132 server->caps &= ~NFS_CAP_POSIX_LOCK; 2133 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { 2134 status = _nfs4_proc_open_confirm(data); 2135 if (status != 0) 2136 return status; 2137 } 2138 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) 2139 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label); 2140 return 0; 2141 } 2142 2143 static int nfs4_recover_expired_lease(struct nfs_server *server) 2144 { 2145 return nfs4_client_recover_expired_lease(server->nfs_client); 2146 } 2147 2148 /* 2149 * OPEN_EXPIRED: 2150 * reclaim state on the server after a network partition. 2151 * Assumes caller holds the appropriate lock 2152 */ 2153 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) 2154 { 2155 struct nfs4_opendata *opendata; 2156 int ret; 2157 2158 opendata = nfs4_open_recoverdata_alloc(ctx, state, 2159 NFS4_OPEN_CLAIM_FH); 2160 if (IS_ERR(opendata)) 2161 return PTR_ERR(opendata); 2162 ret = nfs4_open_recover(opendata, state); 2163 if (ret == -ESTALE) 2164 d_drop(ctx->dentry); 2165 nfs4_opendata_put(opendata); 2166 return ret; 2167 } 2168 2169 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) 2170 { 2171 struct nfs_server *server = NFS_SERVER(state->inode); 2172 struct nfs4_exception exception = { }; 2173 int err; 2174 2175 do { 2176 err = _nfs4_open_expired(ctx, state); 2177 trace_nfs4_open_expired(ctx, 0, err); 2178 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) 2179 continue; 2180 switch (err) { 2181 default: 2182 goto out; 2183 case -NFS4ERR_GRACE: 2184 case -NFS4ERR_DELAY: 2185 nfs4_handle_exception(server, err, &exception); 2186 err = 0; 2187 } 2188 } while (exception.retry); 2189 out: 2190 return err; 2191 } 2192 2193 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2194 { 2195 struct nfs_open_context *ctx; 2196 int ret; 2197 2198 ctx = nfs4_state_find_open_context(state); 2199 if (IS_ERR(ctx)) 2200 return -EAGAIN; 2201 ret = nfs4_do_open_expired(ctx, state); 2202 put_nfs_open_context(ctx); 2203 return ret; 2204 } 2205 2206 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state) 2207 { 2208 nfs_remove_bad_delegation(state->inode); 2209 write_seqlock(&state->seqlock); 2210 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 2211 write_sequnlock(&state->seqlock); 2212 clear_bit(NFS_DELEGATED_STATE, &state->flags); 2213 } 2214 2215 static void nfs40_clear_delegation_stateid(struct nfs4_state *state) 2216 { 2217 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL) 2218 nfs_finish_clear_delegation_stateid(state); 2219 } 2220 2221 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2222 { 2223 /* NFSv4.0 doesn't allow for delegation recovery on open expire */ 2224 nfs40_clear_delegation_stateid(state); 2225 return nfs4_open_expired(sp, state); 2226 } 2227 2228 #if defined(CONFIG_NFS_V4_1) 2229 static void nfs41_check_delegation_stateid(struct nfs4_state *state) 2230 { 2231 struct nfs_server *server = NFS_SERVER(state->inode); 2232 nfs4_stateid stateid; 2233 struct nfs_delegation *delegation; 2234 struct rpc_cred *cred; 2235 int status; 2236 2237 /* Get the delegation credential for use by test/free_stateid */ 2238 rcu_read_lock(); 2239 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 2240 if (delegation == NULL) { 2241 rcu_read_unlock(); 2242 return; 2243 } 2244 2245 nfs4_stateid_copy(&stateid, &delegation->stateid); 2246 cred = get_rpccred(delegation->cred); 2247 rcu_read_unlock(); 2248 status = nfs41_test_stateid(server, &stateid, cred); 2249 trace_nfs4_test_delegation_stateid(state, NULL, status); 2250 2251 if (status != NFS_OK) { 2252 /* Free the stateid unless the server explicitly 2253 * informs us the stateid is unrecognized. */ 2254 if (status != -NFS4ERR_BAD_STATEID) 2255 nfs41_free_stateid(server, &stateid, cred); 2256 nfs_finish_clear_delegation_stateid(state); 2257 } 2258 2259 put_rpccred(cred); 2260 } 2261 2262 /** 2263 * nfs41_check_open_stateid - possibly free an open stateid 2264 * 2265 * @state: NFSv4 state for an inode 2266 * 2267 * Returns NFS_OK if recovery for this stateid is now finished. 2268 * Otherwise a negative NFS4ERR value is returned. 2269 */ 2270 static int nfs41_check_open_stateid(struct nfs4_state *state) 2271 { 2272 struct nfs_server *server = NFS_SERVER(state->inode); 2273 nfs4_stateid *stateid = &state->open_stateid; 2274 struct rpc_cred *cred = state->owner->so_cred; 2275 int status; 2276 2277 /* If a state reset has been done, test_stateid is unneeded */ 2278 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) && 2279 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) && 2280 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0)) 2281 return -NFS4ERR_BAD_STATEID; 2282 2283 status = nfs41_test_stateid(server, stateid, cred); 2284 trace_nfs4_test_open_stateid(state, NULL, status); 2285 if (status != NFS_OK) { 2286 /* Free the stateid unless the server explicitly 2287 * informs us the stateid is unrecognized. */ 2288 if (status != -NFS4ERR_BAD_STATEID) 2289 nfs41_free_stateid(server, stateid, cred); 2290 2291 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 2292 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 2293 clear_bit(NFS_O_RDWR_STATE, &state->flags); 2294 clear_bit(NFS_OPEN_STATE, &state->flags); 2295 } 2296 return status; 2297 } 2298 2299 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2300 { 2301 int status; 2302 2303 nfs41_check_delegation_stateid(state); 2304 status = nfs41_check_open_stateid(state); 2305 if (status != NFS_OK) 2306 status = nfs4_open_expired(sp, state); 2307 return status; 2308 } 2309 #endif 2310 2311 /* 2312 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* 2313 * fields corresponding to attributes that were used to store the verifier. 2314 * Make sure we clobber those fields in the later setattr call 2315 */ 2316 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr) 2317 { 2318 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) && 2319 !(sattr->ia_valid & ATTR_ATIME_SET)) 2320 sattr->ia_valid |= ATTR_ATIME; 2321 2322 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) && 2323 !(sattr->ia_valid & ATTR_MTIME_SET)) 2324 sattr->ia_valid |= ATTR_MTIME; 2325 } 2326 2327 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, 2328 fmode_t fmode, 2329 int flags, 2330 struct nfs_open_context *ctx) 2331 { 2332 struct nfs4_state_owner *sp = opendata->owner; 2333 struct nfs_server *server = sp->so_server; 2334 struct dentry *dentry; 2335 struct nfs4_state *state; 2336 unsigned int seq; 2337 int ret; 2338 2339 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); 2340 2341 ret = _nfs4_proc_open(opendata); 2342 if (ret != 0) 2343 goto out; 2344 2345 state = nfs4_opendata_to_nfs4_state(opendata); 2346 ret = PTR_ERR(state); 2347 if (IS_ERR(state)) 2348 goto out; 2349 if (server->caps & NFS_CAP_POSIX_LOCK) 2350 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); 2351 2352 dentry = opendata->dentry; 2353 if (d_really_is_negative(dentry)) { 2354 /* FIXME: Is this d_drop() ever needed? */ 2355 d_drop(dentry); 2356 dentry = d_add_unique(dentry, igrab(state->inode)); 2357 if (dentry == NULL) { 2358 dentry = opendata->dentry; 2359 } else if (dentry != ctx->dentry) { 2360 dput(ctx->dentry); 2361 ctx->dentry = dget(dentry); 2362 } 2363 nfs_set_verifier(dentry, 2364 nfs_save_change_attribute(d_inode(opendata->dir))); 2365 } 2366 2367 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags); 2368 if (ret != 0) 2369 goto out; 2370 2371 ctx->state = state; 2372 if (d_inode(dentry) == state->inode) { 2373 nfs_inode_attach_open_context(ctx); 2374 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) 2375 nfs4_schedule_stateid_recovery(server, state); 2376 } 2377 out: 2378 return ret; 2379 } 2380 2381 /* 2382 * Returns a referenced nfs4_state 2383 */ 2384 static int _nfs4_do_open(struct inode *dir, 2385 struct nfs_open_context *ctx, 2386 int flags, 2387 struct iattr *sattr, 2388 struct nfs4_label *label, 2389 int *opened) 2390 { 2391 struct nfs4_state_owner *sp; 2392 struct nfs4_state *state = NULL; 2393 struct nfs_server *server = NFS_SERVER(dir); 2394 struct nfs4_opendata *opendata; 2395 struct dentry *dentry = ctx->dentry; 2396 struct rpc_cred *cred = ctx->cred; 2397 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold; 2398 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC); 2399 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; 2400 struct nfs4_label *olabel = NULL; 2401 int status; 2402 2403 /* Protect against reboot recovery conflicts */ 2404 status = -ENOMEM; 2405 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); 2406 if (sp == NULL) { 2407 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); 2408 goto out_err; 2409 } 2410 status = nfs4_recover_expired_lease(server); 2411 if (status != 0) 2412 goto err_put_state_owner; 2413 if (d_really_is_positive(dentry)) 2414 nfs4_return_incompatible_delegation(d_inode(dentry), fmode); 2415 status = -ENOMEM; 2416 if (d_really_is_positive(dentry)) 2417 claim = NFS4_OPEN_CLAIM_FH; 2418 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, 2419 label, claim, GFP_KERNEL); 2420 if (opendata == NULL) 2421 goto err_put_state_owner; 2422 2423 if (label) { 2424 olabel = nfs4_label_alloc(server, GFP_KERNEL); 2425 if (IS_ERR(olabel)) { 2426 status = PTR_ERR(olabel); 2427 goto err_opendata_put; 2428 } 2429 } 2430 2431 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { 2432 if (!opendata->f_attr.mdsthreshold) { 2433 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); 2434 if (!opendata->f_attr.mdsthreshold) 2435 goto err_free_label; 2436 } 2437 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; 2438 } 2439 if (d_really_is_positive(dentry)) 2440 opendata->state = nfs4_get_open_state(d_inode(dentry), sp); 2441 2442 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx); 2443 if (status != 0) 2444 goto err_free_label; 2445 state = ctx->state; 2446 2447 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) && 2448 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) { 2449 nfs4_exclusive_attrset(opendata, sattr); 2450 2451 nfs_fattr_init(opendata->o_res.f_attr); 2452 status = nfs4_do_setattr(state->inode, cred, 2453 opendata->o_res.f_attr, sattr, 2454 state, label, olabel); 2455 if (status == 0) { 2456 nfs_setattr_update_inode(state->inode, sattr, 2457 opendata->o_res.f_attr); 2458 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel); 2459 } 2460 } 2461 if (opendata->file_created) 2462 *opened |= FILE_CREATED; 2463 2464 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) { 2465 *ctx_th = opendata->f_attr.mdsthreshold; 2466 opendata->f_attr.mdsthreshold = NULL; 2467 } 2468 2469 nfs4_label_free(olabel); 2470 2471 nfs4_opendata_put(opendata); 2472 nfs4_put_state_owner(sp); 2473 return 0; 2474 err_free_label: 2475 nfs4_label_free(olabel); 2476 err_opendata_put: 2477 nfs4_opendata_put(opendata); 2478 err_put_state_owner: 2479 nfs4_put_state_owner(sp); 2480 out_err: 2481 return status; 2482 } 2483 2484 2485 static struct nfs4_state *nfs4_do_open(struct inode *dir, 2486 struct nfs_open_context *ctx, 2487 int flags, 2488 struct iattr *sattr, 2489 struct nfs4_label *label, 2490 int *opened) 2491 { 2492 struct nfs_server *server = NFS_SERVER(dir); 2493 struct nfs4_exception exception = { }; 2494 struct nfs4_state *res; 2495 int status; 2496 2497 do { 2498 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened); 2499 res = ctx->state; 2500 trace_nfs4_open_file(ctx, flags, status); 2501 if (status == 0) 2502 break; 2503 /* NOTE: BAD_SEQID means the server and client disagree about the 2504 * book-keeping w.r.t. state-changing operations 2505 * (OPEN/CLOSE/LOCK/LOCKU...) 2506 * It is actually a sign of a bug on the client or on the server. 2507 * 2508 * If we receive a BAD_SEQID error in the particular case of 2509 * doing an OPEN, we assume that nfs_increment_open_seqid() will 2510 * have unhashed the old state_owner for us, and that we can 2511 * therefore safely retry using a new one. We should still warn 2512 * the user though... 2513 */ 2514 if (status == -NFS4ERR_BAD_SEQID) { 2515 pr_warn_ratelimited("NFS: v4 server %s " 2516 " returned a bad sequence-id error!\n", 2517 NFS_SERVER(dir)->nfs_client->cl_hostname); 2518 exception.retry = 1; 2519 continue; 2520 } 2521 /* 2522 * BAD_STATEID on OPEN means that the server cancelled our 2523 * state before it received the OPEN_CONFIRM. 2524 * Recover by retrying the request as per the discussion 2525 * on Page 181 of RFC3530. 2526 */ 2527 if (status == -NFS4ERR_BAD_STATEID) { 2528 exception.retry = 1; 2529 continue; 2530 } 2531 if (status == -EAGAIN) { 2532 /* We must have found a delegation */ 2533 exception.retry = 1; 2534 continue; 2535 } 2536 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) 2537 continue; 2538 res = ERR_PTR(nfs4_handle_exception(server, 2539 status, &exception)); 2540 } while (exception.retry); 2541 return res; 2542 } 2543 2544 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, 2545 struct nfs_fattr *fattr, struct iattr *sattr, 2546 struct nfs4_state *state, struct nfs4_label *ilabel, 2547 struct nfs4_label *olabel) 2548 { 2549 struct nfs_server *server = NFS_SERVER(inode); 2550 struct nfs_setattrargs arg = { 2551 .fh = NFS_FH(inode), 2552 .iap = sattr, 2553 .server = server, 2554 .bitmask = server->attr_bitmask, 2555 .label = ilabel, 2556 }; 2557 struct nfs_setattrres res = { 2558 .fattr = fattr, 2559 .label = olabel, 2560 .server = server, 2561 }; 2562 struct rpc_message msg = { 2563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 2564 .rpc_argp = &arg, 2565 .rpc_resp = &res, 2566 .rpc_cred = cred, 2567 }; 2568 unsigned long timestamp = jiffies; 2569 fmode_t fmode; 2570 bool truncate; 2571 int status; 2572 2573 arg.bitmask = nfs4_bitmask(server, ilabel); 2574 if (ilabel) 2575 arg.bitmask = nfs4_bitmask(server, olabel); 2576 2577 nfs_fattr_init(fattr); 2578 2579 /* Servers should only apply open mode checks for file size changes */ 2580 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false; 2581 fmode = truncate ? FMODE_WRITE : FMODE_READ; 2582 2583 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) { 2584 /* Use that stateid */ 2585 } else if (truncate && state != NULL) { 2586 struct nfs_lockowner lockowner = { 2587 .l_owner = current->files, 2588 .l_pid = current->tgid, 2589 }; 2590 if (!nfs4_valid_open_stateid(state)) 2591 return -EBADF; 2592 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE, 2593 &lockowner) == -EIO) 2594 return -EBADF; 2595 } else 2596 nfs4_stateid_copy(&arg.stateid, &zero_stateid); 2597 2598 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 2599 if (status == 0 && state != NULL) 2600 renew_lease(server, timestamp); 2601 return status; 2602 } 2603 2604 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, 2605 struct nfs_fattr *fattr, struct iattr *sattr, 2606 struct nfs4_state *state, struct nfs4_label *ilabel, 2607 struct nfs4_label *olabel) 2608 { 2609 struct nfs_server *server = NFS_SERVER(inode); 2610 struct nfs4_exception exception = { 2611 .state = state, 2612 .inode = inode, 2613 }; 2614 int err; 2615 do { 2616 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel); 2617 trace_nfs4_setattr(inode, err); 2618 switch (err) { 2619 case -NFS4ERR_OPENMODE: 2620 if (!(sattr->ia_valid & ATTR_SIZE)) { 2621 pr_warn_once("NFSv4: server %s is incorrectly " 2622 "applying open mode checks to " 2623 "a SETATTR that is not " 2624 "changing file size.\n", 2625 server->nfs_client->cl_hostname); 2626 } 2627 if (state && !(state->state & FMODE_WRITE)) { 2628 err = -EBADF; 2629 if (sattr->ia_valid & ATTR_OPEN) 2630 err = -EACCES; 2631 goto out; 2632 } 2633 } 2634 err = nfs4_handle_exception(server, err, &exception); 2635 } while (exception.retry); 2636 out: 2637 return err; 2638 } 2639 2640 struct nfs4_closedata { 2641 struct inode *inode; 2642 struct nfs4_state *state; 2643 struct nfs_closeargs arg; 2644 struct nfs_closeres res; 2645 struct nfs_fattr fattr; 2646 unsigned long timestamp; 2647 bool roc; 2648 u32 roc_barrier; 2649 }; 2650 2651 static void nfs4_free_closedata(void *data) 2652 { 2653 struct nfs4_closedata *calldata = data; 2654 struct nfs4_state_owner *sp = calldata->state->owner; 2655 struct super_block *sb = calldata->state->inode->i_sb; 2656 2657 if (calldata->roc) 2658 pnfs_roc_release(calldata->state->inode); 2659 nfs4_put_open_state(calldata->state); 2660 nfs_free_seqid(calldata->arg.seqid); 2661 nfs4_put_state_owner(sp); 2662 nfs_sb_deactive(sb); 2663 kfree(calldata); 2664 } 2665 2666 static void nfs4_close_done(struct rpc_task *task, void *data) 2667 { 2668 struct nfs4_closedata *calldata = data; 2669 struct nfs4_state *state = calldata->state; 2670 struct nfs_server *server = NFS_SERVER(calldata->inode); 2671 nfs4_stateid *res_stateid = NULL; 2672 2673 dprintk("%s: begin!\n", __func__); 2674 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 2675 return; 2676 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status); 2677 /* hmm. we are done with the inode, and in the process of freeing 2678 * the state_owner. we keep this around to process errors 2679 */ 2680 switch (task->tk_status) { 2681 case 0: 2682 res_stateid = &calldata->res.stateid; 2683 if (calldata->arg.fmode == 0 && calldata->roc) 2684 pnfs_roc_set_barrier(state->inode, 2685 calldata->roc_barrier); 2686 renew_lease(server, calldata->timestamp); 2687 break; 2688 case -NFS4ERR_ADMIN_REVOKED: 2689 case -NFS4ERR_STALE_STATEID: 2690 case -NFS4ERR_OLD_STATEID: 2691 case -NFS4ERR_BAD_STATEID: 2692 case -NFS4ERR_EXPIRED: 2693 if (!nfs4_stateid_match(&calldata->arg.stateid, 2694 &state->open_stateid)) { 2695 rpc_restart_call_prepare(task); 2696 goto out_release; 2697 } 2698 if (calldata->arg.fmode == 0) 2699 break; 2700 default: 2701 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) { 2702 rpc_restart_call_prepare(task); 2703 goto out_release; 2704 } 2705 } 2706 nfs_clear_open_stateid(state, &calldata->arg.stateid, 2707 res_stateid, calldata->arg.fmode); 2708 out_release: 2709 nfs_release_seqid(calldata->arg.seqid); 2710 nfs_refresh_inode(calldata->inode, calldata->res.fattr); 2711 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status); 2712 } 2713 2714 static void nfs4_close_prepare(struct rpc_task *task, void *data) 2715 { 2716 struct nfs4_closedata *calldata = data; 2717 struct nfs4_state *state = calldata->state; 2718 struct inode *inode = calldata->inode; 2719 bool is_rdonly, is_wronly, is_rdwr; 2720 int call_close = 0; 2721 2722 dprintk("%s: begin!\n", __func__); 2723 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 2724 goto out_wait; 2725 2726 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; 2727 spin_lock(&state->owner->so_lock); 2728 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags); 2729 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags); 2730 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags); 2731 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid); 2732 /* Calculate the change in open mode */ 2733 calldata->arg.fmode = 0; 2734 if (state->n_rdwr == 0) { 2735 if (state->n_rdonly == 0) 2736 call_close |= is_rdonly; 2737 else if (is_rdonly) 2738 calldata->arg.fmode |= FMODE_READ; 2739 if (state->n_wronly == 0) 2740 call_close |= is_wronly; 2741 else if (is_wronly) 2742 calldata->arg.fmode |= FMODE_WRITE; 2743 } else if (is_rdwr) 2744 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE; 2745 2746 if (calldata->arg.fmode == 0) 2747 call_close |= is_rdwr; 2748 2749 if (!nfs4_valid_open_stateid(state)) 2750 call_close = 0; 2751 spin_unlock(&state->owner->so_lock); 2752 2753 if (!call_close) { 2754 /* Note: exit _without_ calling nfs4_close_done */ 2755 goto out_no_action; 2756 } 2757 2758 if (calldata->arg.fmode == 0) { 2759 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; 2760 if (calldata->roc && 2761 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) { 2762 nfs_release_seqid(calldata->arg.seqid); 2763 goto out_wait; 2764 } 2765 } 2766 calldata->arg.share_access = 2767 nfs4_map_atomic_open_share(NFS_SERVER(inode), 2768 calldata->arg.fmode, 0); 2769 2770 nfs_fattr_init(calldata->res.fattr); 2771 calldata->timestamp = jiffies; 2772 if (nfs4_setup_sequence(NFS_SERVER(inode), 2773 &calldata->arg.seq_args, 2774 &calldata->res.seq_res, 2775 task) != 0) 2776 nfs_release_seqid(calldata->arg.seqid); 2777 dprintk("%s: done!\n", __func__); 2778 return; 2779 out_no_action: 2780 task->tk_action = NULL; 2781 out_wait: 2782 nfs4_sequence_done(task, &calldata->res.seq_res); 2783 } 2784 2785 static const struct rpc_call_ops nfs4_close_ops = { 2786 .rpc_call_prepare = nfs4_close_prepare, 2787 .rpc_call_done = nfs4_close_done, 2788 .rpc_release = nfs4_free_closedata, 2789 }; 2790 2791 static bool nfs4_roc(struct inode *inode) 2792 { 2793 if (!nfs_have_layout(inode)) 2794 return false; 2795 return pnfs_roc(inode); 2796 } 2797 2798 /* 2799 * It is possible for data to be read/written from a mem-mapped file 2800 * after the sys_close call (which hits the vfs layer as a flush). 2801 * This means that we can't safely call nfsv4 close on a file until 2802 * the inode is cleared. This in turn means that we are not good 2803 * NFSv4 citizens - we do not indicate to the server to update the file's 2804 * share state even when we are done with one of the three share 2805 * stateid's in the inode. 2806 * 2807 * NOTE: Caller must be holding the sp->so_owner semaphore! 2808 */ 2809 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) 2810 { 2811 struct nfs_server *server = NFS_SERVER(state->inode); 2812 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 2813 struct nfs4_closedata *calldata; 2814 struct nfs4_state_owner *sp = state->owner; 2815 struct rpc_task *task; 2816 struct rpc_message msg = { 2817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], 2818 .rpc_cred = state->owner->so_cred, 2819 }; 2820 struct rpc_task_setup task_setup_data = { 2821 .rpc_client = server->client, 2822 .rpc_message = &msg, 2823 .callback_ops = &nfs4_close_ops, 2824 .workqueue = nfsiod_workqueue, 2825 .flags = RPC_TASK_ASYNC, 2826 }; 2827 int status = -ENOMEM; 2828 2829 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP, 2830 &task_setup_data.rpc_client, &msg); 2831 2832 calldata = kzalloc(sizeof(*calldata), gfp_mask); 2833 if (calldata == NULL) 2834 goto out; 2835 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1); 2836 calldata->inode = state->inode; 2837 calldata->state = state; 2838 calldata->arg.fh = NFS_FH(state->inode); 2839 /* Serialization for the sequence id */ 2840 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 2841 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask); 2842 if (IS_ERR(calldata->arg.seqid)) 2843 goto out_free_calldata; 2844 calldata->arg.fmode = 0; 2845 calldata->arg.bitmask = server->cache_consistency_bitmask; 2846 calldata->res.fattr = &calldata->fattr; 2847 calldata->res.seqid = calldata->arg.seqid; 2848 calldata->res.server = server; 2849 calldata->roc = nfs4_roc(state->inode); 2850 nfs_sb_active(calldata->inode->i_sb); 2851 2852 msg.rpc_argp = &calldata->arg; 2853 msg.rpc_resp = &calldata->res; 2854 task_setup_data.callback_data = calldata; 2855 task = rpc_run_task(&task_setup_data); 2856 if (IS_ERR(task)) 2857 return PTR_ERR(task); 2858 status = 0; 2859 if (wait) 2860 status = rpc_wait_for_completion_task(task); 2861 rpc_put_task(task); 2862 return status; 2863 out_free_calldata: 2864 kfree(calldata); 2865 out: 2866 nfs4_put_open_state(state); 2867 nfs4_put_state_owner(sp); 2868 return status; 2869 } 2870 2871 static struct inode * 2872 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, 2873 int open_flags, struct iattr *attr, int *opened) 2874 { 2875 struct nfs4_state *state; 2876 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL; 2877 2878 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l); 2879 2880 /* Protect against concurrent sillydeletes */ 2881 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened); 2882 2883 nfs4_label_release_security(label); 2884 2885 if (IS_ERR(state)) 2886 return ERR_CAST(state); 2887 return state->inode; 2888 } 2889 2890 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) 2891 { 2892 if (ctx->state == NULL) 2893 return; 2894 if (is_sync) 2895 nfs4_close_sync(ctx->state, ctx->mode); 2896 else 2897 nfs4_close_state(ctx->state, ctx->mode); 2898 } 2899 2900 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL) 2901 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL) 2902 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL) 2903 2904 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) 2905 { 2906 struct nfs4_server_caps_arg args = { 2907 .fhandle = fhandle, 2908 }; 2909 struct nfs4_server_caps_res res = {}; 2910 struct rpc_message msg = { 2911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], 2912 .rpc_argp = &args, 2913 .rpc_resp = &res, 2914 }; 2915 int status; 2916 2917 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 2918 if (status == 0) { 2919 /* Sanity check the server answers */ 2920 switch (server->nfs_client->cl_minorversion) { 2921 case 0: 2922 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK; 2923 res.attr_bitmask[2] = 0; 2924 break; 2925 case 1: 2926 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK; 2927 break; 2928 case 2: 2929 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK; 2930 } 2931 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); 2932 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| 2933 NFS_CAP_SYMLINKS|NFS_CAP_FILEID| 2934 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| 2935 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| 2936 NFS_CAP_CTIME|NFS_CAP_MTIME| 2937 NFS_CAP_SECURITY_LABEL); 2938 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL && 2939 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) 2940 server->caps |= NFS_CAP_ACLS; 2941 if (res.has_links != 0) 2942 server->caps |= NFS_CAP_HARDLINKS; 2943 if (res.has_symlinks != 0) 2944 server->caps |= NFS_CAP_SYMLINKS; 2945 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) 2946 server->caps |= NFS_CAP_FILEID; 2947 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) 2948 server->caps |= NFS_CAP_MODE; 2949 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) 2950 server->caps |= NFS_CAP_NLINK; 2951 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) 2952 server->caps |= NFS_CAP_OWNER; 2953 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) 2954 server->caps |= NFS_CAP_OWNER_GROUP; 2955 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) 2956 server->caps |= NFS_CAP_ATIME; 2957 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) 2958 server->caps |= NFS_CAP_CTIME; 2959 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) 2960 server->caps |= NFS_CAP_MTIME; 2961 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 2962 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL) 2963 server->caps |= NFS_CAP_SECURITY_LABEL; 2964 #endif 2965 memcpy(server->attr_bitmask_nl, res.attr_bitmask, 2966 sizeof(server->attr_bitmask)); 2967 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL; 2968 2969 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); 2970 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; 2971 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; 2972 server->cache_consistency_bitmask[2] = 0; 2973 server->acl_bitmask = res.acl_bitmask; 2974 server->fh_expire_type = res.fh_expire_type; 2975 } 2976 2977 return status; 2978 } 2979 2980 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) 2981 { 2982 struct nfs4_exception exception = { }; 2983 int err; 2984 do { 2985 err = nfs4_handle_exception(server, 2986 _nfs4_server_capabilities(server, fhandle), 2987 &exception); 2988 } while (exception.retry); 2989 return err; 2990 } 2991 2992 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, 2993 struct nfs_fsinfo *info) 2994 { 2995 u32 bitmask[3]; 2996 struct nfs4_lookup_root_arg args = { 2997 .bitmask = bitmask, 2998 }; 2999 struct nfs4_lookup_res res = { 3000 .server = server, 3001 .fattr = info->fattr, 3002 .fh = fhandle, 3003 }; 3004 struct rpc_message msg = { 3005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], 3006 .rpc_argp = &args, 3007 .rpc_resp = &res, 3008 }; 3009 3010 bitmask[0] = nfs4_fattr_bitmap[0]; 3011 bitmask[1] = nfs4_fattr_bitmap[1]; 3012 /* 3013 * Process the label in the upcoming getfattr 3014 */ 3015 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL; 3016 3017 nfs_fattr_init(info->fattr); 3018 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3019 } 3020 3021 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, 3022 struct nfs_fsinfo *info) 3023 { 3024 struct nfs4_exception exception = { }; 3025 int err; 3026 do { 3027 err = _nfs4_lookup_root(server, fhandle, info); 3028 trace_nfs4_lookup_root(server, fhandle, info->fattr, err); 3029 switch (err) { 3030 case 0: 3031 case -NFS4ERR_WRONGSEC: 3032 goto out; 3033 default: 3034 err = nfs4_handle_exception(server, err, &exception); 3035 } 3036 } while (exception.retry); 3037 out: 3038 return err; 3039 } 3040 3041 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 3042 struct nfs_fsinfo *info, rpc_authflavor_t flavor) 3043 { 3044 struct rpc_auth_create_args auth_args = { 3045 .pseudoflavor = flavor, 3046 }; 3047 struct rpc_auth *auth; 3048 int ret; 3049 3050 auth = rpcauth_create(&auth_args, server->client); 3051 if (IS_ERR(auth)) { 3052 ret = -EACCES; 3053 goto out; 3054 } 3055 ret = nfs4_lookup_root(server, fhandle, info); 3056 out: 3057 return ret; 3058 } 3059 3060 /* 3061 * Retry pseudoroot lookup with various security flavors. We do this when: 3062 * 3063 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC 3064 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation 3065 * 3066 * Returns zero on success, or a negative NFS4ERR value, or a 3067 * negative errno value. 3068 */ 3069 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 3070 struct nfs_fsinfo *info) 3071 { 3072 /* Per 3530bis 15.33.5 */ 3073 static const rpc_authflavor_t flav_array[] = { 3074 RPC_AUTH_GSS_KRB5P, 3075 RPC_AUTH_GSS_KRB5I, 3076 RPC_AUTH_GSS_KRB5, 3077 RPC_AUTH_UNIX, /* courtesy */ 3078 RPC_AUTH_NULL, 3079 }; 3080 int status = -EPERM; 3081 size_t i; 3082 3083 if (server->auth_info.flavor_len > 0) { 3084 /* try each flavor specified by user */ 3085 for (i = 0; i < server->auth_info.flavor_len; i++) { 3086 status = nfs4_lookup_root_sec(server, fhandle, info, 3087 server->auth_info.flavors[i]); 3088 if (status == -NFS4ERR_WRONGSEC || status == -EACCES) 3089 continue; 3090 break; 3091 } 3092 } else { 3093 /* no flavors specified by user, try default list */ 3094 for (i = 0; i < ARRAY_SIZE(flav_array); i++) { 3095 status = nfs4_lookup_root_sec(server, fhandle, info, 3096 flav_array[i]); 3097 if (status == -NFS4ERR_WRONGSEC || status == -EACCES) 3098 continue; 3099 break; 3100 } 3101 } 3102 3103 /* 3104 * -EACCESS could mean that the user doesn't have correct permissions 3105 * to access the mount. It could also mean that we tried to mount 3106 * with a gss auth flavor, but rpc.gssd isn't running. Either way, 3107 * existing mount programs don't handle -EACCES very well so it should 3108 * be mapped to -EPERM instead. 3109 */ 3110 if (status == -EACCES) 3111 status = -EPERM; 3112 return status; 3113 } 3114 3115 static int nfs4_do_find_root_sec(struct nfs_server *server, 3116 struct nfs_fh *fhandle, struct nfs_fsinfo *info) 3117 { 3118 int mv = server->nfs_client->cl_minorversion; 3119 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info); 3120 } 3121 3122 /** 3123 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot 3124 * @server: initialized nfs_server handle 3125 * @fhandle: we fill in the pseudo-fs root file handle 3126 * @info: we fill in an FSINFO struct 3127 * @auth_probe: probe the auth flavours 3128 * 3129 * Returns zero on success, or a negative errno. 3130 */ 3131 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, 3132 struct nfs_fsinfo *info, 3133 bool auth_probe) 3134 { 3135 int status = 0; 3136 3137 if (!auth_probe) 3138 status = nfs4_lookup_root(server, fhandle, info); 3139 3140 if (auth_probe || status == NFS4ERR_WRONGSEC) 3141 status = nfs4_do_find_root_sec(server, fhandle, info); 3142 3143 if (status == 0) 3144 status = nfs4_server_capabilities(server, fhandle); 3145 if (status == 0) 3146 status = nfs4_do_fsinfo(server, fhandle, info); 3147 3148 return nfs4_map_errors(status); 3149 } 3150 3151 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, 3152 struct nfs_fsinfo *info) 3153 { 3154 int error; 3155 struct nfs_fattr *fattr = info->fattr; 3156 struct nfs4_label *label = NULL; 3157 3158 error = nfs4_server_capabilities(server, mntfh); 3159 if (error < 0) { 3160 dprintk("nfs4_get_root: getcaps error = %d\n", -error); 3161 return error; 3162 } 3163 3164 label = nfs4_label_alloc(server, GFP_KERNEL); 3165 if (IS_ERR(label)) 3166 return PTR_ERR(label); 3167 3168 error = nfs4_proc_getattr(server, mntfh, fattr, label); 3169 if (error < 0) { 3170 dprintk("nfs4_get_root: getattr error = %d\n", -error); 3171 goto err_free_label; 3172 } 3173 3174 if (fattr->valid & NFS_ATTR_FATTR_FSID && 3175 !nfs_fsid_equal(&server->fsid, &fattr->fsid)) 3176 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 3177 3178 err_free_label: 3179 nfs4_label_free(label); 3180 3181 return error; 3182 } 3183 3184 /* 3185 * Get locations and (maybe) other attributes of a referral. 3186 * Note that we'll actually follow the referral later when 3187 * we detect fsid mismatch in inode revalidation 3188 */ 3189 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, 3190 const struct qstr *name, struct nfs_fattr *fattr, 3191 struct nfs_fh *fhandle) 3192 { 3193 int status = -ENOMEM; 3194 struct page *page = NULL; 3195 struct nfs4_fs_locations *locations = NULL; 3196 3197 page = alloc_page(GFP_KERNEL); 3198 if (page == NULL) 3199 goto out; 3200 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); 3201 if (locations == NULL) 3202 goto out; 3203 3204 status = nfs4_proc_fs_locations(client, dir, name, locations, page); 3205 if (status != 0) 3206 goto out; 3207 3208 /* 3209 * If the fsid didn't change, this is a migration event, not a 3210 * referral. Cause us to drop into the exception handler, which 3211 * will kick off migration recovery. 3212 */ 3213 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { 3214 dprintk("%s: server did not return a different fsid for" 3215 " a referral at %s\n", __func__, name->name); 3216 status = -NFS4ERR_MOVED; 3217 goto out; 3218 } 3219 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ 3220 nfs_fixup_referral_attributes(&locations->fattr); 3221 3222 /* replace the lookup nfs_fattr with the locations nfs_fattr */ 3223 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); 3224 memset(fhandle, 0, sizeof(struct nfs_fh)); 3225 out: 3226 if (page) 3227 __free_page(page); 3228 kfree(locations); 3229 return status; 3230 } 3231 3232 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 3233 struct nfs_fattr *fattr, struct nfs4_label *label) 3234 { 3235 struct nfs4_getattr_arg args = { 3236 .fh = fhandle, 3237 .bitmask = server->attr_bitmask, 3238 }; 3239 struct nfs4_getattr_res res = { 3240 .fattr = fattr, 3241 .label = label, 3242 .server = server, 3243 }; 3244 struct rpc_message msg = { 3245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 3246 .rpc_argp = &args, 3247 .rpc_resp = &res, 3248 }; 3249 3250 args.bitmask = nfs4_bitmask(server, label); 3251 3252 nfs_fattr_init(fattr); 3253 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3254 } 3255 3256 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 3257 struct nfs_fattr *fattr, struct nfs4_label *label) 3258 { 3259 struct nfs4_exception exception = { }; 3260 int err; 3261 do { 3262 err = _nfs4_proc_getattr(server, fhandle, fattr, label); 3263 trace_nfs4_getattr(server, fhandle, fattr, err); 3264 err = nfs4_handle_exception(server, err, 3265 &exception); 3266 } while (exception.retry); 3267 return err; 3268 } 3269 3270 /* 3271 * The file is not closed if it is opened due to the a request to change 3272 * the size of the file. The open call will not be needed once the 3273 * VFS layer lookup-intents are implemented. 3274 * 3275 * Close is called when the inode is destroyed. 3276 * If we haven't opened the file for O_WRONLY, we 3277 * need to in the size_change case to obtain a stateid. 3278 * 3279 * Got race? 3280 * Because OPEN is always done by name in nfsv4, it is 3281 * possible that we opened a different file by the same 3282 * name. We can recognize this race condition, but we 3283 * can't do anything about it besides returning an error. 3284 * 3285 * This will be fixed with VFS changes (lookup-intent). 3286 */ 3287 static int 3288 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, 3289 struct iattr *sattr) 3290 { 3291 struct inode *inode = d_inode(dentry); 3292 struct rpc_cred *cred = NULL; 3293 struct nfs4_state *state = NULL; 3294 struct nfs4_label *label = NULL; 3295 int status; 3296 3297 if (pnfs_ld_layoutret_on_setattr(inode) && 3298 sattr->ia_valid & ATTR_SIZE && 3299 sattr->ia_size < i_size_read(inode)) 3300 pnfs_commit_and_return_layout(inode); 3301 3302 nfs_fattr_init(fattr); 3303 3304 /* Deal with open(O_TRUNC) */ 3305 if (sattr->ia_valid & ATTR_OPEN) 3306 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME); 3307 3308 /* Optimization: if the end result is no change, don't RPC */ 3309 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) 3310 return 0; 3311 3312 /* Search for an existing open(O_WRITE) file */ 3313 if (sattr->ia_valid & ATTR_FILE) { 3314 struct nfs_open_context *ctx; 3315 3316 ctx = nfs_file_open_context(sattr->ia_file); 3317 if (ctx) { 3318 cred = ctx->cred; 3319 state = ctx->state; 3320 } 3321 } 3322 3323 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 3324 if (IS_ERR(label)) 3325 return PTR_ERR(label); 3326 3327 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label); 3328 if (status == 0) { 3329 nfs_setattr_update_inode(inode, sattr, fattr); 3330 nfs_setsecurity(inode, fattr, label); 3331 } 3332 nfs4_label_free(label); 3333 return status; 3334 } 3335 3336 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, 3337 const struct qstr *name, struct nfs_fh *fhandle, 3338 struct nfs_fattr *fattr, struct nfs4_label *label) 3339 { 3340 struct nfs_server *server = NFS_SERVER(dir); 3341 int status; 3342 struct nfs4_lookup_arg args = { 3343 .bitmask = server->attr_bitmask, 3344 .dir_fh = NFS_FH(dir), 3345 .name = name, 3346 }; 3347 struct nfs4_lookup_res res = { 3348 .server = server, 3349 .fattr = fattr, 3350 .label = label, 3351 .fh = fhandle, 3352 }; 3353 struct rpc_message msg = { 3354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], 3355 .rpc_argp = &args, 3356 .rpc_resp = &res, 3357 }; 3358 3359 args.bitmask = nfs4_bitmask(server, label); 3360 3361 nfs_fattr_init(fattr); 3362 3363 dprintk("NFS call lookup %s\n", name->name); 3364 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0); 3365 dprintk("NFS reply lookup: %d\n", status); 3366 return status; 3367 } 3368 3369 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) 3370 { 3371 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | 3372 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; 3373 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; 3374 fattr->nlink = 2; 3375 } 3376 3377 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, 3378 struct qstr *name, struct nfs_fh *fhandle, 3379 struct nfs_fattr *fattr, struct nfs4_label *label) 3380 { 3381 struct nfs4_exception exception = { }; 3382 struct rpc_clnt *client = *clnt; 3383 int err; 3384 do { 3385 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label); 3386 trace_nfs4_lookup(dir, name, err); 3387 switch (err) { 3388 case -NFS4ERR_BADNAME: 3389 err = -ENOENT; 3390 goto out; 3391 case -NFS4ERR_MOVED: 3392 err = nfs4_get_referral(client, dir, name, fattr, fhandle); 3393 if (err == -NFS4ERR_MOVED) 3394 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); 3395 goto out; 3396 case -NFS4ERR_WRONGSEC: 3397 err = -EPERM; 3398 if (client != *clnt) 3399 goto out; 3400 client = nfs4_negotiate_security(client, dir, name); 3401 if (IS_ERR(client)) 3402 return PTR_ERR(client); 3403 3404 exception.retry = 1; 3405 break; 3406 default: 3407 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); 3408 } 3409 } while (exception.retry); 3410 3411 out: 3412 if (err == 0) 3413 *clnt = client; 3414 else if (client != *clnt) 3415 rpc_shutdown_client(client); 3416 3417 return err; 3418 } 3419 3420 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, 3421 struct nfs_fh *fhandle, struct nfs_fattr *fattr, 3422 struct nfs4_label *label) 3423 { 3424 int status; 3425 struct rpc_clnt *client = NFS_CLIENT(dir); 3426 3427 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label); 3428 if (client != NFS_CLIENT(dir)) { 3429 rpc_shutdown_client(client); 3430 nfs_fixup_secinfo_attributes(fattr); 3431 } 3432 return status; 3433 } 3434 3435 struct rpc_clnt * 3436 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name, 3437 struct nfs_fh *fhandle, struct nfs_fattr *fattr) 3438 { 3439 struct rpc_clnt *client = NFS_CLIENT(dir); 3440 int status; 3441 3442 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL); 3443 if (status < 0) 3444 return ERR_PTR(status); 3445 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client; 3446 } 3447 3448 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) 3449 { 3450 struct nfs_server *server = NFS_SERVER(inode); 3451 struct nfs4_accessargs args = { 3452 .fh = NFS_FH(inode), 3453 .bitmask = server->cache_consistency_bitmask, 3454 }; 3455 struct nfs4_accessres res = { 3456 .server = server, 3457 }; 3458 struct rpc_message msg = { 3459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], 3460 .rpc_argp = &args, 3461 .rpc_resp = &res, 3462 .rpc_cred = entry->cred, 3463 }; 3464 int mode = entry->mask; 3465 int status = 0; 3466 3467 /* 3468 * Determine which access bits we want to ask for... 3469 */ 3470 if (mode & MAY_READ) 3471 args.access |= NFS4_ACCESS_READ; 3472 if (S_ISDIR(inode->i_mode)) { 3473 if (mode & MAY_WRITE) 3474 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; 3475 if (mode & MAY_EXEC) 3476 args.access |= NFS4_ACCESS_LOOKUP; 3477 } else { 3478 if (mode & MAY_WRITE) 3479 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; 3480 if (mode & MAY_EXEC) 3481 args.access |= NFS4_ACCESS_EXECUTE; 3482 } 3483 3484 res.fattr = nfs_alloc_fattr(); 3485 if (res.fattr == NULL) 3486 return -ENOMEM; 3487 3488 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3489 if (!status) { 3490 nfs_access_set_mask(entry, res.access); 3491 nfs_refresh_inode(inode, res.fattr); 3492 } 3493 nfs_free_fattr(res.fattr); 3494 return status; 3495 } 3496 3497 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) 3498 { 3499 struct nfs4_exception exception = { }; 3500 int err; 3501 do { 3502 err = _nfs4_proc_access(inode, entry); 3503 trace_nfs4_access(inode, err); 3504 err = nfs4_handle_exception(NFS_SERVER(inode), err, 3505 &exception); 3506 } while (exception.retry); 3507 return err; 3508 } 3509 3510 /* 3511 * TODO: For the time being, we don't try to get any attributes 3512 * along with any of the zero-copy operations READ, READDIR, 3513 * READLINK, WRITE. 3514 * 3515 * In the case of the first three, we want to put the GETATTR 3516 * after the read-type operation -- this is because it is hard 3517 * to predict the length of a GETATTR response in v4, and thus 3518 * align the READ data correctly. This means that the GETATTR 3519 * may end up partially falling into the page cache, and we should 3520 * shift it into the 'tail' of the xdr_buf before processing. 3521 * To do this efficiently, we need to know the total length 3522 * of data received, which doesn't seem to be available outside 3523 * of the RPC layer. 3524 * 3525 * In the case of WRITE, we also want to put the GETATTR after 3526 * the operation -- in this case because we want to make sure 3527 * we get the post-operation mtime and size. 3528 * 3529 * Both of these changes to the XDR layer would in fact be quite 3530 * minor, but I decided to leave them for a subsequent patch. 3531 */ 3532 static int _nfs4_proc_readlink(struct inode *inode, struct page *page, 3533 unsigned int pgbase, unsigned int pglen) 3534 { 3535 struct nfs4_readlink args = { 3536 .fh = NFS_FH(inode), 3537 .pgbase = pgbase, 3538 .pglen = pglen, 3539 .pages = &page, 3540 }; 3541 struct nfs4_readlink_res res; 3542 struct rpc_message msg = { 3543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], 3544 .rpc_argp = &args, 3545 .rpc_resp = &res, 3546 }; 3547 3548 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); 3549 } 3550 3551 static int nfs4_proc_readlink(struct inode *inode, struct page *page, 3552 unsigned int pgbase, unsigned int pglen) 3553 { 3554 struct nfs4_exception exception = { }; 3555 int err; 3556 do { 3557 err = _nfs4_proc_readlink(inode, page, pgbase, pglen); 3558 trace_nfs4_readlink(inode, err); 3559 err = nfs4_handle_exception(NFS_SERVER(inode), err, 3560 &exception); 3561 } while (exception.retry); 3562 return err; 3563 } 3564 3565 /* 3566 * This is just for mknod. open(O_CREAT) will always do ->open_context(). 3567 */ 3568 static int 3569 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 3570 int flags) 3571 { 3572 struct nfs4_label l, *ilabel = NULL; 3573 struct nfs_open_context *ctx; 3574 struct nfs4_state *state; 3575 int opened = 0; 3576 int status = 0; 3577 3578 ctx = alloc_nfs_open_context(dentry, FMODE_READ); 3579 if (IS_ERR(ctx)) 3580 return PTR_ERR(ctx); 3581 3582 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l); 3583 3584 sattr->ia_mode &= ~current_umask(); 3585 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened); 3586 if (IS_ERR(state)) { 3587 status = PTR_ERR(state); 3588 goto out; 3589 } 3590 out: 3591 nfs4_label_release_security(ilabel); 3592 put_nfs_open_context(ctx); 3593 return status; 3594 } 3595 3596 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name) 3597 { 3598 struct nfs_server *server = NFS_SERVER(dir); 3599 struct nfs_removeargs args = { 3600 .fh = NFS_FH(dir), 3601 .name = *name, 3602 }; 3603 struct nfs_removeres res = { 3604 .server = server, 3605 }; 3606 struct rpc_message msg = { 3607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], 3608 .rpc_argp = &args, 3609 .rpc_resp = &res, 3610 }; 3611 int status; 3612 3613 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); 3614 if (status == 0) 3615 update_changeattr(dir, &res.cinfo); 3616 return status; 3617 } 3618 3619 static int nfs4_proc_remove(struct inode *dir, struct qstr *name) 3620 { 3621 struct nfs4_exception exception = { }; 3622 int err; 3623 do { 3624 err = _nfs4_proc_remove(dir, name); 3625 trace_nfs4_remove(dir, name, err); 3626 err = nfs4_handle_exception(NFS_SERVER(dir), err, 3627 &exception); 3628 } while (exception.retry); 3629 return err; 3630 } 3631 3632 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir) 3633 { 3634 struct nfs_server *server = NFS_SERVER(dir); 3635 struct nfs_removeargs *args = msg->rpc_argp; 3636 struct nfs_removeres *res = msg->rpc_resp; 3637 3638 res->server = server; 3639 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; 3640 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1); 3641 3642 nfs_fattr_init(res->dir_attr); 3643 } 3644 3645 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) 3646 { 3647 nfs4_setup_sequence(NFS_SERVER(data->dir), 3648 &data->args.seq_args, 3649 &data->res.seq_res, 3650 task); 3651 } 3652 3653 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) 3654 { 3655 struct nfs_unlinkdata *data = task->tk_calldata; 3656 struct nfs_removeres *res = &data->res; 3657 3658 if (!nfs4_sequence_done(task, &res->seq_res)) 3659 return 0; 3660 if (nfs4_async_handle_error(task, res->server, NULL, 3661 &data->timeout) == -EAGAIN) 3662 return 0; 3663 update_changeattr(dir, &res->cinfo); 3664 return 1; 3665 } 3666 3667 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir) 3668 { 3669 struct nfs_server *server = NFS_SERVER(dir); 3670 struct nfs_renameargs *arg = msg->rpc_argp; 3671 struct nfs_renameres *res = msg->rpc_resp; 3672 3673 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; 3674 res->server = server; 3675 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1); 3676 } 3677 3678 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) 3679 { 3680 nfs4_setup_sequence(NFS_SERVER(data->old_dir), 3681 &data->args.seq_args, 3682 &data->res.seq_res, 3683 task); 3684 } 3685 3686 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, 3687 struct inode *new_dir) 3688 { 3689 struct nfs_renamedata *data = task->tk_calldata; 3690 struct nfs_renameres *res = &data->res; 3691 3692 if (!nfs4_sequence_done(task, &res->seq_res)) 3693 return 0; 3694 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN) 3695 return 0; 3696 3697 update_changeattr(old_dir, &res->old_cinfo); 3698 update_changeattr(new_dir, &res->new_cinfo); 3699 return 1; 3700 } 3701 3702 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) 3703 { 3704 struct nfs_server *server = NFS_SERVER(inode); 3705 struct nfs4_link_arg arg = { 3706 .fh = NFS_FH(inode), 3707 .dir_fh = NFS_FH(dir), 3708 .name = name, 3709 .bitmask = server->attr_bitmask, 3710 }; 3711 struct nfs4_link_res res = { 3712 .server = server, 3713 .label = NULL, 3714 }; 3715 struct rpc_message msg = { 3716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], 3717 .rpc_argp = &arg, 3718 .rpc_resp = &res, 3719 }; 3720 int status = -ENOMEM; 3721 3722 res.fattr = nfs_alloc_fattr(); 3723 if (res.fattr == NULL) 3724 goto out; 3725 3726 res.label = nfs4_label_alloc(server, GFP_KERNEL); 3727 if (IS_ERR(res.label)) { 3728 status = PTR_ERR(res.label); 3729 goto out; 3730 } 3731 arg.bitmask = nfs4_bitmask(server, res.label); 3732 3733 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 3734 if (!status) { 3735 update_changeattr(dir, &res.cinfo); 3736 status = nfs_post_op_update_inode(inode, res.fattr); 3737 if (!status) 3738 nfs_setsecurity(inode, res.fattr, res.label); 3739 } 3740 3741 3742 nfs4_label_free(res.label); 3743 3744 out: 3745 nfs_free_fattr(res.fattr); 3746 return status; 3747 } 3748 3749 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) 3750 { 3751 struct nfs4_exception exception = { }; 3752 int err; 3753 do { 3754 err = nfs4_handle_exception(NFS_SERVER(inode), 3755 _nfs4_proc_link(inode, dir, name), 3756 &exception); 3757 } while (exception.retry); 3758 return err; 3759 } 3760 3761 struct nfs4_createdata { 3762 struct rpc_message msg; 3763 struct nfs4_create_arg arg; 3764 struct nfs4_create_res res; 3765 struct nfs_fh fh; 3766 struct nfs_fattr fattr; 3767 struct nfs4_label *label; 3768 }; 3769 3770 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, 3771 struct qstr *name, struct iattr *sattr, u32 ftype) 3772 { 3773 struct nfs4_createdata *data; 3774 3775 data = kzalloc(sizeof(*data), GFP_KERNEL); 3776 if (data != NULL) { 3777 struct nfs_server *server = NFS_SERVER(dir); 3778 3779 data->label = nfs4_label_alloc(server, GFP_KERNEL); 3780 if (IS_ERR(data->label)) 3781 goto out_free; 3782 3783 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; 3784 data->msg.rpc_argp = &data->arg; 3785 data->msg.rpc_resp = &data->res; 3786 data->arg.dir_fh = NFS_FH(dir); 3787 data->arg.server = server; 3788 data->arg.name = name; 3789 data->arg.attrs = sattr; 3790 data->arg.ftype = ftype; 3791 data->arg.bitmask = nfs4_bitmask(server, data->label); 3792 data->res.server = server; 3793 data->res.fh = &data->fh; 3794 data->res.fattr = &data->fattr; 3795 data->res.label = data->label; 3796 nfs_fattr_init(data->res.fattr); 3797 } 3798 return data; 3799 out_free: 3800 kfree(data); 3801 return NULL; 3802 } 3803 3804 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) 3805 { 3806 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, 3807 &data->arg.seq_args, &data->res.seq_res, 1); 3808 if (status == 0) { 3809 update_changeattr(dir, &data->res.dir_cinfo); 3810 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label); 3811 } 3812 return status; 3813 } 3814 3815 static void nfs4_free_createdata(struct nfs4_createdata *data) 3816 { 3817 nfs4_label_free(data->label); 3818 kfree(data); 3819 } 3820 3821 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 3822 struct page *page, unsigned int len, struct iattr *sattr, 3823 struct nfs4_label *label) 3824 { 3825 struct nfs4_createdata *data; 3826 int status = -ENAMETOOLONG; 3827 3828 if (len > NFS4_MAXPATHLEN) 3829 goto out; 3830 3831 status = -ENOMEM; 3832 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); 3833 if (data == NULL) 3834 goto out; 3835 3836 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; 3837 data->arg.u.symlink.pages = &page; 3838 data->arg.u.symlink.len = len; 3839 data->arg.label = label; 3840 3841 status = nfs4_do_create(dir, dentry, data); 3842 3843 nfs4_free_createdata(data); 3844 out: 3845 return status; 3846 } 3847 3848 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 3849 struct page *page, unsigned int len, struct iattr *sattr) 3850 { 3851 struct nfs4_exception exception = { }; 3852 struct nfs4_label l, *label = NULL; 3853 int err; 3854 3855 label = nfs4_label_init_security(dir, dentry, sattr, &l); 3856 3857 do { 3858 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label); 3859 trace_nfs4_symlink(dir, &dentry->d_name, err); 3860 err = nfs4_handle_exception(NFS_SERVER(dir), err, 3861 &exception); 3862 } while (exception.retry); 3863 3864 nfs4_label_release_security(label); 3865 return err; 3866 } 3867 3868 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 3869 struct iattr *sattr, struct nfs4_label *label) 3870 { 3871 struct nfs4_createdata *data; 3872 int status = -ENOMEM; 3873 3874 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); 3875 if (data == NULL) 3876 goto out; 3877 3878 data->arg.label = label; 3879 status = nfs4_do_create(dir, dentry, data); 3880 3881 nfs4_free_createdata(data); 3882 out: 3883 return status; 3884 } 3885 3886 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 3887 struct iattr *sattr) 3888 { 3889 struct nfs4_exception exception = { }; 3890 struct nfs4_label l, *label = NULL; 3891 int err; 3892 3893 label = nfs4_label_init_security(dir, dentry, sattr, &l); 3894 3895 sattr->ia_mode &= ~current_umask(); 3896 do { 3897 err = _nfs4_proc_mkdir(dir, dentry, sattr, label); 3898 trace_nfs4_mkdir(dir, &dentry->d_name, err); 3899 err = nfs4_handle_exception(NFS_SERVER(dir), err, 3900 &exception); 3901 } while (exception.retry); 3902 nfs4_label_release_security(label); 3903 3904 return err; 3905 } 3906 3907 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, 3908 u64 cookie, struct page **pages, unsigned int count, int plus) 3909 { 3910 struct inode *dir = d_inode(dentry); 3911 struct nfs4_readdir_arg args = { 3912 .fh = NFS_FH(dir), 3913 .pages = pages, 3914 .pgbase = 0, 3915 .count = count, 3916 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask, 3917 .plus = plus, 3918 }; 3919 struct nfs4_readdir_res res; 3920 struct rpc_message msg = { 3921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], 3922 .rpc_argp = &args, 3923 .rpc_resp = &res, 3924 .rpc_cred = cred, 3925 }; 3926 int status; 3927 3928 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__, 3929 dentry, 3930 (unsigned long long)cookie); 3931 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args); 3932 res.pgbase = args.pgbase; 3933 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); 3934 if (status >= 0) { 3935 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE); 3936 status += args.pgbase; 3937 } 3938 3939 nfs_invalidate_atime(dir); 3940 3941 dprintk("%s: returns %d\n", __func__, status); 3942 return status; 3943 } 3944 3945 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, 3946 u64 cookie, struct page **pages, unsigned int count, int plus) 3947 { 3948 struct nfs4_exception exception = { }; 3949 int err; 3950 do { 3951 err = _nfs4_proc_readdir(dentry, cred, cookie, 3952 pages, count, plus); 3953 trace_nfs4_readdir(d_inode(dentry), err); 3954 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err, 3955 &exception); 3956 } while (exception.retry); 3957 return err; 3958 } 3959 3960 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 3961 struct iattr *sattr, struct nfs4_label *label, dev_t rdev) 3962 { 3963 struct nfs4_createdata *data; 3964 int mode = sattr->ia_mode; 3965 int status = -ENOMEM; 3966 3967 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); 3968 if (data == NULL) 3969 goto out; 3970 3971 if (S_ISFIFO(mode)) 3972 data->arg.ftype = NF4FIFO; 3973 else if (S_ISBLK(mode)) { 3974 data->arg.ftype = NF4BLK; 3975 data->arg.u.device.specdata1 = MAJOR(rdev); 3976 data->arg.u.device.specdata2 = MINOR(rdev); 3977 } 3978 else if (S_ISCHR(mode)) { 3979 data->arg.ftype = NF4CHR; 3980 data->arg.u.device.specdata1 = MAJOR(rdev); 3981 data->arg.u.device.specdata2 = MINOR(rdev); 3982 } else if (!S_ISSOCK(mode)) { 3983 status = -EINVAL; 3984 goto out_free; 3985 } 3986 3987 data->arg.label = label; 3988 status = nfs4_do_create(dir, dentry, data); 3989 out_free: 3990 nfs4_free_createdata(data); 3991 out: 3992 return status; 3993 } 3994 3995 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 3996 struct iattr *sattr, dev_t rdev) 3997 { 3998 struct nfs4_exception exception = { }; 3999 struct nfs4_label l, *label = NULL; 4000 int err; 4001 4002 label = nfs4_label_init_security(dir, dentry, sattr, &l); 4003 4004 sattr->ia_mode &= ~current_umask(); 4005 do { 4006 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); 4007 trace_nfs4_mknod(dir, &dentry->d_name, err); 4008 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4009 &exception); 4010 } while (exception.retry); 4011 4012 nfs4_label_release_security(label); 4013 4014 return err; 4015 } 4016 4017 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, 4018 struct nfs_fsstat *fsstat) 4019 { 4020 struct nfs4_statfs_arg args = { 4021 .fh = fhandle, 4022 .bitmask = server->attr_bitmask, 4023 }; 4024 struct nfs4_statfs_res res = { 4025 .fsstat = fsstat, 4026 }; 4027 struct rpc_message msg = { 4028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], 4029 .rpc_argp = &args, 4030 .rpc_resp = &res, 4031 }; 4032 4033 nfs_fattr_init(fsstat->fattr); 4034 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4035 } 4036 4037 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) 4038 { 4039 struct nfs4_exception exception = { }; 4040 int err; 4041 do { 4042 err = nfs4_handle_exception(server, 4043 _nfs4_proc_statfs(server, fhandle, fsstat), 4044 &exception); 4045 } while (exception.retry); 4046 return err; 4047 } 4048 4049 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, 4050 struct nfs_fsinfo *fsinfo) 4051 { 4052 struct nfs4_fsinfo_arg args = { 4053 .fh = fhandle, 4054 .bitmask = server->attr_bitmask, 4055 }; 4056 struct nfs4_fsinfo_res res = { 4057 .fsinfo = fsinfo, 4058 }; 4059 struct rpc_message msg = { 4060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], 4061 .rpc_argp = &args, 4062 .rpc_resp = &res, 4063 }; 4064 4065 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4066 } 4067 4068 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 4069 { 4070 struct nfs4_exception exception = { }; 4071 unsigned long now = jiffies; 4072 int err; 4073 4074 do { 4075 err = _nfs4_do_fsinfo(server, fhandle, fsinfo); 4076 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); 4077 if (err == 0) { 4078 struct nfs_client *clp = server->nfs_client; 4079 4080 spin_lock(&clp->cl_lock); 4081 clp->cl_lease_time = fsinfo->lease_time * HZ; 4082 clp->cl_last_renewal = now; 4083 spin_unlock(&clp->cl_lock); 4084 break; 4085 } 4086 err = nfs4_handle_exception(server, err, &exception); 4087 } while (exception.retry); 4088 return err; 4089 } 4090 4091 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 4092 { 4093 int error; 4094 4095 nfs_fattr_init(fsinfo->fattr); 4096 error = nfs4_do_fsinfo(server, fhandle, fsinfo); 4097 if (error == 0) { 4098 /* block layout checks this! */ 4099 server->pnfs_blksize = fsinfo->blksize; 4100 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype); 4101 } 4102 4103 return error; 4104 } 4105 4106 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 4107 struct nfs_pathconf *pathconf) 4108 { 4109 struct nfs4_pathconf_arg args = { 4110 .fh = fhandle, 4111 .bitmask = server->attr_bitmask, 4112 }; 4113 struct nfs4_pathconf_res res = { 4114 .pathconf = pathconf, 4115 }; 4116 struct rpc_message msg = { 4117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], 4118 .rpc_argp = &args, 4119 .rpc_resp = &res, 4120 }; 4121 4122 /* None of the pathconf attributes are mandatory to implement */ 4123 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { 4124 memset(pathconf, 0, sizeof(*pathconf)); 4125 return 0; 4126 } 4127 4128 nfs_fattr_init(pathconf->fattr); 4129 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4130 } 4131 4132 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 4133 struct nfs_pathconf *pathconf) 4134 { 4135 struct nfs4_exception exception = { }; 4136 int err; 4137 4138 do { 4139 err = nfs4_handle_exception(server, 4140 _nfs4_proc_pathconf(server, fhandle, pathconf), 4141 &exception); 4142 } while (exception.retry); 4143 return err; 4144 } 4145 4146 int nfs4_set_rw_stateid(nfs4_stateid *stateid, 4147 const struct nfs_open_context *ctx, 4148 const struct nfs_lock_context *l_ctx, 4149 fmode_t fmode) 4150 { 4151 const struct nfs_lockowner *lockowner = NULL; 4152 4153 if (l_ctx != NULL) 4154 lockowner = &l_ctx->lockowner; 4155 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner); 4156 } 4157 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); 4158 4159 static bool nfs4_stateid_is_current(nfs4_stateid *stateid, 4160 const struct nfs_open_context *ctx, 4161 const struct nfs_lock_context *l_ctx, 4162 fmode_t fmode) 4163 { 4164 nfs4_stateid current_stateid; 4165 4166 /* If the current stateid represents a lost lock, then exit */ 4167 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO) 4168 return true; 4169 return nfs4_stateid_match(stateid, ¤t_stateid); 4170 } 4171 4172 static bool nfs4_error_stateid_expired(int err) 4173 { 4174 switch (err) { 4175 case -NFS4ERR_DELEG_REVOKED: 4176 case -NFS4ERR_ADMIN_REVOKED: 4177 case -NFS4ERR_BAD_STATEID: 4178 case -NFS4ERR_STALE_STATEID: 4179 case -NFS4ERR_OLD_STATEID: 4180 case -NFS4ERR_OPENMODE: 4181 case -NFS4ERR_EXPIRED: 4182 return true; 4183 } 4184 return false; 4185 } 4186 4187 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr) 4188 { 4189 nfs_invalidate_atime(hdr->inode); 4190 } 4191 4192 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) 4193 { 4194 struct nfs_server *server = NFS_SERVER(hdr->inode); 4195 4196 trace_nfs4_read(hdr, task->tk_status); 4197 if (nfs4_async_handle_error(task, server, 4198 hdr->args.context->state, 4199 NULL) == -EAGAIN) { 4200 rpc_restart_call_prepare(task); 4201 return -EAGAIN; 4202 } 4203 4204 __nfs4_read_done_cb(hdr); 4205 if (task->tk_status > 0) 4206 renew_lease(server, hdr->timestamp); 4207 return 0; 4208 } 4209 4210 static bool nfs4_read_stateid_changed(struct rpc_task *task, 4211 struct nfs_pgio_args *args) 4212 { 4213 4214 if (!nfs4_error_stateid_expired(task->tk_status) || 4215 nfs4_stateid_is_current(&args->stateid, 4216 args->context, 4217 args->lock_context, 4218 FMODE_READ)) 4219 return false; 4220 rpc_restart_call_prepare(task); 4221 return true; 4222 } 4223 4224 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 4225 { 4226 4227 dprintk("--> %s\n", __func__); 4228 4229 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 4230 return -EAGAIN; 4231 if (nfs4_read_stateid_changed(task, &hdr->args)) 4232 return -EAGAIN; 4233 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 4234 nfs4_read_done_cb(task, hdr); 4235 } 4236 4237 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, 4238 struct rpc_message *msg) 4239 { 4240 hdr->timestamp = jiffies; 4241 hdr->pgio_done_cb = nfs4_read_done_cb; 4242 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; 4243 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0); 4244 } 4245 4246 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, 4247 struct nfs_pgio_header *hdr) 4248 { 4249 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode), 4250 &hdr->args.seq_args, 4251 &hdr->res.seq_res, 4252 task)) 4253 return 0; 4254 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, 4255 hdr->args.lock_context, 4256 hdr->rw_ops->rw_mode) == -EIO) 4257 return -EIO; 4258 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) 4259 return -EIO; 4260 return 0; 4261 } 4262 4263 static int nfs4_write_done_cb(struct rpc_task *task, 4264 struct nfs_pgio_header *hdr) 4265 { 4266 struct inode *inode = hdr->inode; 4267 4268 trace_nfs4_write(hdr, task->tk_status); 4269 if (nfs4_async_handle_error(task, NFS_SERVER(inode), 4270 hdr->args.context->state, 4271 NULL) == -EAGAIN) { 4272 rpc_restart_call_prepare(task); 4273 return -EAGAIN; 4274 } 4275 if (task->tk_status >= 0) { 4276 renew_lease(NFS_SERVER(inode), hdr->timestamp); 4277 nfs_writeback_update_inode(hdr); 4278 } 4279 return 0; 4280 } 4281 4282 static bool nfs4_write_stateid_changed(struct rpc_task *task, 4283 struct nfs_pgio_args *args) 4284 { 4285 4286 if (!nfs4_error_stateid_expired(task->tk_status) || 4287 nfs4_stateid_is_current(&args->stateid, 4288 args->context, 4289 args->lock_context, 4290 FMODE_WRITE)) 4291 return false; 4292 rpc_restart_call_prepare(task); 4293 return true; 4294 } 4295 4296 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 4297 { 4298 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 4299 return -EAGAIN; 4300 if (nfs4_write_stateid_changed(task, &hdr->args)) 4301 return -EAGAIN; 4302 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 4303 nfs4_write_done_cb(task, hdr); 4304 } 4305 4306 static 4307 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) 4308 { 4309 /* Don't request attributes for pNFS or O_DIRECT writes */ 4310 if (hdr->ds_clp != NULL || hdr->dreq != NULL) 4311 return false; 4312 /* Otherwise, request attributes if and only if we don't hold 4313 * a delegation 4314 */ 4315 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; 4316 } 4317 4318 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, 4319 struct rpc_message *msg) 4320 { 4321 struct nfs_server *server = NFS_SERVER(hdr->inode); 4322 4323 if (!nfs4_write_need_cache_consistency_data(hdr)) { 4324 hdr->args.bitmask = NULL; 4325 hdr->res.fattr = NULL; 4326 } else 4327 hdr->args.bitmask = server->cache_consistency_bitmask; 4328 4329 if (!hdr->pgio_done_cb) 4330 hdr->pgio_done_cb = nfs4_write_done_cb; 4331 hdr->res.server = server; 4332 hdr->timestamp = jiffies; 4333 4334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; 4335 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1); 4336 } 4337 4338 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) 4339 { 4340 nfs4_setup_sequence(NFS_SERVER(data->inode), 4341 &data->args.seq_args, 4342 &data->res.seq_res, 4343 task); 4344 } 4345 4346 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) 4347 { 4348 struct inode *inode = data->inode; 4349 4350 trace_nfs4_commit(data, task->tk_status); 4351 if (nfs4_async_handle_error(task, NFS_SERVER(inode), 4352 NULL, NULL) == -EAGAIN) { 4353 rpc_restart_call_prepare(task); 4354 return -EAGAIN; 4355 } 4356 return 0; 4357 } 4358 4359 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) 4360 { 4361 if (!nfs4_sequence_done(task, &data->res.seq_res)) 4362 return -EAGAIN; 4363 return data->commit_done_cb(task, data); 4364 } 4365 4366 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg) 4367 { 4368 struct nfs_server *server = NFS_SERVER(data->inode); 4369 4370 if (data->commit_done_cb == NULL) 4371 data->commit_done_cb = nfs4_commit_done_cb; 4372 data->res.server = server; 4373 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; 4374 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); 4375 } 4376 4377 struct nfs4_renewdata { 4378 struct nfs_client *client; 4379 unsigned long timestamp; 4380 }; 4381 4382 /* 4383 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special 4384 * standalone procedure for queueing an asynchronous RENEW. 4385 */ 4386 static void nfs4_renew_release(void *calldata) 4387 { 4388 struct nfs4_renewdata *data = calldata; 4389 struct nfs_client *clp = data->client; 4390 4391 if (atomic_read(&clp->cl_count) > 1) 4392 nfs4_schedule_state_renewal(clp); 4393 nfs_put_client(clp); 4394 kfree(data); 4395 } 4396 4397 static void nfs4_renew_done(struct rpc_task *task, void *calldata) 4398 { 4399 struct nfs4_renewdata *data = calldata; 4400 struct nfs_client *clp = data->client; 4401 unsigned long timestamp = data->timestamp; 4402 4403 trace_nfs4_renew_async(clp, task->tk_status); 4404 switch (task->tk_status) { 4405 case 0: 4406 break; 4407 case -NFS4ERR_LEASE_MOVED: 4408 nfs4_schedule_lease_moved_recovery(clp); 4409 break; 4410 default: 4411 /* Unless we're shutting down, schedule state recovery! */ 4412 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) 4413 return; 4414 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { 4415 nfs4_schedule_lease_recovery(clp); 4416 return; 4417 } 4418 nfs4_schedule_path_down_recovery(clp); 4419 } 4420 do_renew_lease(clp, timestamp); 4421 } 4422 4423 static const struct rpc_call_ops nfs4_renew_ops = { 4424 .rpc_call_done = nfs4_renew_done, 4425 .rpc_release = nfs4_renew_release, 4426 }; 4427 4428 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) 4429 { 4430 struct rpc_message msg = { 4431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], 4432 .rpc_argp = clp, 4433 .rpc_cred = cred, 4434 }; 4435 struct nfs4_renewdata *data; 4436 4437 if (renew_flags == 0) 4438 return 0; 4439 if (!atomic_inc_not_zero(&clp->cl_count)) 4440 return -EIO; 4441 data = kmalloc(sizeof(*data), GFP_NOFS); 4442 if (data == NULL) 4443 return -ENOMEM; 4444 data->client = clp; 4445 data->timestamp = jiffies; 4446 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT, 4447 &nfs4_renew_ops, data); 4448 } 4449 4450 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred) 4451 { 4452 struct rpc_message msg = { 4453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], 4454 .rpc_argp = clp, 4455 .rpc_cred = cred, 4456 }; 4457 unsigned long now = jiffies; 4458 int status; 4459 4460 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 4461 if (status < 0) 4462 return status; 4463 do_renew_lease(clp, now); 4464 return 0; 4465 } 4466 4467 static inline int nfs4_server_supports_acls(struct nfs_server *server) 4468 { 4469 return server->caps & NFS_CAP_ACLS; 4470 } 4471 4472 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that 4473 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on 4474 * the stack. 4475 */ 4476 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) 4477 4478 static int buf_to_pages_noslab(const void *buf, size_t buflen, 4479 struct page **pages, unsigned int *pgbase) 4480 { 4481 struct page *newpage, **spages; 4482 int rc = 0; 4483 size_t len; 4484 spages = pages; 4485 4486 do { 4487 len = min_t(size_t, PAGE_SIZE, buflen); 4488 newpage = alloc_page(GFP_KERNEL); 4489 4490 if (newpage == NULL) 4491 goto unwind; 4492 memcpy(page_address(newpage), buf, len); 4493 buf += len; 4494 buflen -= len; 4495 *pages++ = newpage; 4496 rc++; 4497 } while (buflen != 0); 4498 4499 return rc; 4500 4501 unwind: 4502 for(; rc > 0; rc--) 4503 __free_page(spages[rc-1]); 4504 return -ENOMEM; 4505 } 4506 4507 struct nfs4_cached_acl { 4508 int cached; 4509 size_t len; 4510 char data[0]; 4511 }; 4512 4513 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) 4514 { 4515 struct nfs_inode *nfsi = NFS_I(inode); 4516 4517 spin_lock(&inode->i_lock); 4518 kfree(nfsi->nfs4_acl); 4519 nfsi->nfs4_acl = acl; 4520 spin_unlock(&inode->i_lock); 4521 } 4522 4523 static void nfs4_zap_acl_attr(struct inode *inode) 4524 { 4525 nfs4_set_cached_acl(inode, NULL); 4526 } 4527 4528 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) 4529 { 4530 struct nfs_inode *nfsi = NFS_I(inode); 4531 struct nfs4_cached_acl *acl; 4532 int ret = -ENOENT; 4533 4534 spin_lock(&inode->i_lock); 4535 acl = nfsi->nfs4_acl; 4536 if (acl == NULL) 4537 goto out; 4538 if (buf == NULL) /* user is just asking for length */ 4539 goto out_len; 4540 if (acl->cached == 0) 4541 goto out; 4542 ret = -ERANGE; /* see getxattr(2) man page */ 4543 if (acl->len > buflen) 4544 goto out; 4545 memcpy(buf, acl->data, acl->len); 4546 out_len: 4547 ret = acl->len; 4548 out: 4549 spin_unlock(&inode->i_lock); 4550 return ret; 4551 } 4552 4553 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len) 4554 { 4555 struct nfs4_cached_acl *acl; 4556 size_t buflen = sizeof(*acl) + acl_len; 4557 4558 if (buflen <= PAGE_SIZE) { 4559 acl = kmalloc(buflen, GFP_KERNEL); 4560 if (acl == NULL) 4561 goto out; 4562 acl->cached = 1; 4563 _copy_from_pages(acl->data, pages, pgbase, acl_len); 4564 } else { 4565 acl = kmalloc(sizeof(*acl), GFP_KERNEL); 4566 if (acl == NULL) 4567 goto out; 4568 acl->cached = 0; 4569 } 4570 acl->len = acl_len; 4571 out: 4572 nfs4_set_cached_acl(inode, acl); 4573 } 4574 4575 /* 4576 * The getxattr API returns the required buffer length when called with a 4577 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating 4578 * the required buf. On a NULL buf, we send a page of data to the server 4579 * guessing that the ACL request can be serviced by a page. If so, we cache 4580 * up to the page of ACL data, and the 2nd call to getxattr is serviced by 4581 * the cache. If not so, we throw away the page, and cache the required 4582 * length. The next getxattr call will then produce another round trip to 4583 * the server, this time with the input buf of the required size. 4584 */ 4585 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) 4586 { 4587 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, }; 4588 struct nfs_getaclargs args = { 4589 .fh = NFS_FH(inode), 4590 .acl_pages = pages, 4591 .acl_len = buflen, 4592 }; 4593 struct nfs_getaclres res = { 4594 .acl_len = buflen, 4595 }; 4596 struct rpc_message msg = { 4597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], 4598 .rpc_argp = &args, 4599 .rpc_resp = &res, 4600 }; 4601 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); 4602 int ret = -ENOMEM, i; 4603 4604 /* As long as we're doing a round trip to the server anyway, 4605 * let's be prepared for a page of acl data. */ 4606 if (npages == 0) 4607 npages = 1; 4608 if (npages > ARRAY_SIZE(pages)) 4609 return -ERANGE; 4610 4611 for (i = 0; i < npages; i++) { 4612 pages[i] = alloc_page(GFP_KERNEL); 4613 if (!pages[i]) 4614 goto out_free; 4615 } 4616 4617 /* for decoding across pages */ 4618 res.acl_scratch = alloc_page(GFP_KERNEL); 4619 if (!res.acl_scratch) 4620 goto out_free; 4621 4622 args.acl_len = npages * PAGE_SIZE; 4623 args.acl_pgbase = 0; 4624 4625 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", 4626 __func__, buf, buflen, npages, args.acl_len); 4627 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), 4628 &msg, &args.seq_args, &res.seq_res, 0); 4629 if (ret) 4630 goto out_free; 4631 4632 /* Handle the case where the passed-in buffer is too short */ 4633 if (res.acl_flags & NFS4_ACL_TRUNC) { 4634 /* Did the user only issue a request for the acl length? */ 4635 if (buf == NULL) 4636 goto out_ok; 4637 ret = -ERANGE; 4638 goto out_free; 4639 } 4640 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len); 4641 if (buf) { 4642 if (res.acl_len > buflen) { 4643 ret = -ERANGE; 4644 goto out_free; 4645 } 4646 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); 4647 } 4648 out_ok: 4649 ret = res.acl_len; 4650 out_free: 4651 for (i = 0; i < npages; i++) 4652 if (pages[i]) 4653 __free_page(pages[i]); 4654 if (res.acl_scratch) 4655 __free_page(res.acl_scratch); 4656 return ret; 4657 } 4658 4659 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) 4660 { 4661 struct nfs4_exception exception = { }; 4662 ssize_t ret; 4663 do { 4664 ret = __nfs4_get_acl_uncached(inode, buf, buflen); 4665 trace_nfs4_get_acl(inode, ret); 4666 if (ret >= 0) 4667 break; 4668 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); 4669 } while (exception.retry); 4670 return ret; 4671 } 4672 4673 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) 4674 { 4675 struct nfs_server *server = NFS_SERVER(inode); 4676 int ret; 4677 4678 if (!nfs4_server_supports_acls(server)) 4679 return -EOPNOTSUPP; 4680 ret = nfs_revalidate_inode(server, inode); 4681 if (ret < 0) 4682 return ret; 4683 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) 4684 nfs_zap_acl_cache(inode); 4685 ret = nfs4_read_cached_acl(inode, buf, buflen); 4686 if (ret != -ENOENT) 4687 /* -ENOENT is returned if there is no ACL or if there is an ACL 4688 * but no cached acl data, just the acl length */ 4689 return ret; 4690 return nfs4_get_acl_uncached(inode, buf, buflen); 4691 } 4692 4693 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) 4694 { 4695 struct nfs_server *server = NFS_SERVER(inode); 4696 struct page *pages[NFS4ACL_MAXPAGES]; 4697 struct nfs_setaclargs arg = { 4698 .fh = NFS_FH(inode), 4699 .acl_pages = pages, 4700 .acl_len = buflen, 4701 }; 4702 struct nfs_setaclres res; 4703 struct rpc_message msg = { 4704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], 4705 .rpc_argp = &arg, 4706 .rpc_resp = &res, 4707 }; 4708 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); 4709 int ret, i; 4710 4711 if (!nfs4_server_supports_acls(server)) 4712 return -EOPNOTSUPP; 4713 if (npages > ARRAY_SIZE(pages)) 4714 return -ERANGE; 4715 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase); 4716 if (i < 0) 4717 return i; 4718 nfs4_inode_return_delegation(inode); 4719 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 4720 4721 /* 4722 * Free each page after tx, so the only ref left is 4723 * held by the network stack 4724 */ 4725 for (; i > 0; i--) 4726 put_page(pages[i-1]); 4727 4728 /* 4729 * Acl update can result in inode attribute update. 4730 * so mark the attribute cache invalid. 4731 */ 4732 spin_lock(&inode->i_lock); 4733 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; 4734 spin_unlock(&inode->i_lock); 4735 nfs_access_zap_cache(inode); 4736 nfs_zap_acl_cache(inode); 4737 return ret; 4738 } 4739 4740 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) 4741 { 4742 struct nfs4_exception exception = { }; 4743 int err; 4744 do { 4745 err = __nfs4_proc_set_acl(inode, buf, buflen); 4746 trace_nfs4_set_acl(inode, err); 4747 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4748 &exception); 4749 } while (exception.retry); 4750 return err; 4751 } 4752 4753 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 4754 static int _nfs4_get_security_label(struct inode *inode, void *buf, 4755 size_t buflen) 4756 { 4757 struct nfs_server *server = NFS_SERVER(inode); 4758 struct nfs_fattr fattr; 4759 struct nfs4_label label = {0, 0, buflen, buf}; 4760 4761 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 4762 struct nfs4_getattr_arg arg = { 4763 .fh = NFS_FH(inode), 4764 .bitmask = bitmask, 4765 }; 4766 struct nfs4_getattr_res res = { 4767 .fattr = &fattr, 4768 .label = &label, 4769 .server = server, 4770 }; 4771 struct rpc_message msg = { 4772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 4773 .rpc_argp = &arg, 4774 .rpc_resp = &res, 4775 }; 4776 int ret; 4777 4778 nfs_fattr_init(&fattr); 4779 4780 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); 4781 if (ret) 4782 return ret; 4783 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) 4784 return -ENOENT; 4785 if (buflen < label.len) 4786 return -ERANGE; 4787 return 0; 4788 } 4789 4790 static int nfs4_get_security_label(struct inode *inode, void *buf, 4791 size_t buflen) 4792 { 4793 struct nfs4_exception exception = { }; 4794 int err; 4795 4796 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 4797 return -EOPNOTSUPP; 4798 4799 do { 4800 err = _nfs4_get_security_label(inode, buf, buflen); 4801 trace_nfs4_get_security_label(inode, err); 4802 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4803 &exception); 4804 } while (exception.retry); 4805 return err; 4806 } 4807 4808 static int _nfs4_do_set_security_label(struct inode *inode, 4809 struct nfs4_label *ilabel, 4810 struct nfs_fattr *fattr, 4811 struct nfs4_label *olabel) 4812 { 4813 4814 struct iattr sattr = {0}; 4815 struct nfs_server *server = NFS_SERVER(inode); 4816 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 4817 struct nfs_setattrargs arg = { 4818 .fh = NFS_FH(inode), 4819 .iap = &sattr, 4820 .server = server, 4821 .bitmask = bitmask, 4822 .label = ilabel, 4823 }; 4824 struct nfs_setattrres res = { 4825 .fattr = fattr, 4826 .label = olabel, 4827 .server = server, 4828 }; 4829 struct rpc_message msg = { 4830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 4831 .rpc_argp = &arg, 4832 .rpc_resp = &res, 4833 }; 4834 int status; 4835 4836 nfs4_stateid_copy(&arg.stateid, &zero_stateid); 4837 4838 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 4839 if (status) 4840 dprintk("%s failed: %d\n", __func__, status); 4841 4842 return status; 4843 } 4844 4845 static int nfs4_do_set_security_label(struct inode *inode, 4846 struct nfs4_label *ilabel, 4847 struct nfs_fattr *fattr, 4848 struct nfs4_label *olabel) 4849 { 4850 struct nfs4_exception exception = { }; 4851 int err; 4852 4853 do { 4854 err = _nfs4_do_set_security_label(inode, ilabel, 4855 fattr, olabel); 4856 trace_nfs4_set_security_label(inode, err); 4857 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4858 &exception); 4859 } while (exception.retry); 4860 return err; 4861 } 4862 4863 static int 4864 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen) 4865 { 4866 struct nfs4_label ilabel, *olabel = NULL; 4867 struct nfs_fattr fattr; 4868 struct rpc_cred *cred; 4869 struct inode *inode = d_inode(dentry); 4870 int status; 4871 4872 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 4873 return -EOPNOTSUPP; 4874 4875 nfs_fattr_init(&fattr); 4876 4877 ilabel.pi = 0; 4878 ilabel.lfs = 0; 4879 ilabel.label = (char *)buf; 4880 ilabel.len = buflen; 4881 4882 cred = rpc_lookup_cred(); 4883 if (IS_ERR(cred)) 4884 return PTR_ERR(cred); 4885 4886 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 4887 if (IS_ERR(olabel)) { 4888 status = -PTR_ERR(olabel); 4889 goto out; 4890 } 4891 4892 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel); 4893 if (status == 0) 4894 nfs_setsecurity(inode, &fattr, olabel); 4895 4896 nfs4_label_free(olabel); 4897 out: 4898 put_rpccred(cred); 4899 return status; 4900 } 4901 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ 4902 4903 4904 static int 4905 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, 4906 struct nfs4_state *state, long *timeout) 4907 { 4908 struct nfs_client *clp = server->nfs_client; 4909 4910 if (task->tk_status >= 0) 4911 return 0; 4912 switch(task->tk_status) { 4913 case -NFS4ERR_DELEG_REVOKED: 4914 case -NFS4ERR_ADMIN_REVOKED: 4915 case -NFS4ERR_BAD_STATEID: 4916 case -NFS4ERR_OPENMODE: 4917 if (state == NULL) 4918 break; 4919 if (nfs4_schedule_stateid_recovery(server, state) < 0) 4920 goto recovery_failed; 4921 goto wait_on_recovery; 4922 case -NFS4ERR_EXPIRED: 4923 if (state != NULL) { 4924 if (nfs4_schedule_stateid_recovery(server, state) < 0) 4925 goto recovery_failed; 4926 } 4927 case -NFS4ERR_STALE_STATEID: 4928 case -NFS4ERR_STALE_CLIENTID: 4929 nfs4_schedule_lease_recovery(clp); 4930 goto wait_on_recovery; 4931 case -NFS4ERR_MOVED: 4932 if (nfs4_schedule_migration_recovery(server) < 0) 4933 goto recovery_failed; 4934 goto wait_on_recovery; 4935 case -NFS4ERR_LEASE_MOVED: 4936 nfs4_schedule_lease_moved_recovery(clp); 4937 goto wait_on_recovery; 4938 #if defined(CONFIG_NFS_V4_1) 4939 case -NFS4ERR_BADSESSION: 4940 case -NFS4ERR_BADSLOT: 4941 case -NFS4ERR_BAD_HIGH_SLOT: 4942 case -NFS4ERR_DEADSESSION: 4943 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 4944 case -NFS4ERR_SEQ_FALSE_RETRY: 4945 case -NFS4ERR_SEQ_MISORDERED: 4946 dprintk("%s ERROR %d, Reset session\n", __func__, 4947 task->tk_status); 4948 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); 4949 goto wait_on_recovery; 4950 #endif /* CONFIG_NFS_V4_1 */ 4951 case -NFS4ERR_DELAY: 4952 nfs_inc_server_stats(server, NFSIOS_DELAY); 4953 rpc_delay(task, nfs4_update_delay(timeout)); 4954 goto restart_call; 4955 case -NFS4ERR_GRACE: 4956 rpc_delay(task, NFS4_POLL_RETRY_MAX); 4957 case -NFS4ERR_RETRY_UNCACHED_REP: 4958 case -NFS4ERR_OLD_STATEID: 4959 goto restart_call; 4960 } 4961 task->tk_status = nfs4_map_errors(task->tk_status); 4962 return 0; 4963 recovery_failed: 4964 task->tk_status = -EIO; 4965 return 0; 4966 wait_on_recovery: 4967 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); 4968 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) 4969 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); 4970 if (test_bit(NFS_MIG_FAILED, &server->mig_status)) 4971 goto recovery_failed; 4972 restart_call: 4973 task->tk_status = 0; 4974 return -EAGAIN; 4975 } 4976 4977 static void nfs4_init_boot_verifier(const struct nfs_client *clp, 4978 nfs4_verifier *bootverf) 4979 { 4980 __be32 verf[2]; 4981 4982 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { 4983 /* An impossible timestamp guarantees this value 4984 * will never match a generated boot time. */ 4985 verf[0] = 0; 4986 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1); 4987 } else { 4988 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 4989 verf[0] = cpu_to_be32(nn->boot_time.tv_sec); 4990 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec); 4991 } 4992 memcpy(bootverf->data, verf, sizeof(bootverf->data)); 4993 } 4994 4995 static int 4996 nfs4_init_nonuniform_client_string(struct nfs_client *clp) 4997 { 4998 int result; 4999 size_t len; 5000 char *str; 5001 bool retried = false; 5002 5003 if (clp->cl_owner_id != NULL) 5004 return 0; 5005 retry: 5006 rcu_read_lock(); 5007 len = 14 + strlen(clp->cl_ipaddr) + 1 + 5008 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) + 5009 1 + 5010 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) + 5011 1; 5012 rcu_read_unlock(); 5013 5014 if (len > NFS4_OPAQUE_LIMIT + 1) 5015 return -EINVAL; 5016 5017 /* 5018 * Since this string is allocated at mount time, and held until the 5019 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 5020 * about a memory-reclaim deadlock. 5021 */ 5022 str = kmalloc(len, GFP_KERNEL); 5023 if (!str) 5024 return -ENOMEM; 5025 5026 rcu_read_lock(); 5027 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s", 5028 clp->cl_ipaddr, 5029 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR), 5030 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)); 5031 rcu_read_unlock(); 5032 5033 /* Did something change? */ 5034 if (result >= len) { 5035 kfree(str); 5036 if (retried) 5037 return -EINVAL; 5038 retried = true; 5039 goto retry; 5040 } 5041 clp->cl_owner_id = str; 5042 return 0; 5043 } 5044 5045 static int 5046 nfs4_init_uniquifier_client_string(struct nfs_client *clp) 5047 { 5048 int result; 5049 size_t len; 5050 char *str; 5051 5052 len = 10 + 10 + 1 + 10 + 1 + 5053 strlen(nfs4_client_id_uniquifier) + 1 + 5054 strlen(clp->cl_rpcclient->cl_nodename) + 1; 5055 5056 if (len > NFS4_OPAQUE_LIMIT + 1) 5057 return -EINVAL; 5058 5059 /* 5060 * Since this string is allocated at mount time, and held until the 5061 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 5062 * about a memory-reclaim deadlock. 5063 */ 5064 str = kmalloc(len, GFP_KERNEL); 5065 if (!str) 5066 return -ENOMEM; 5067 5068 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s", 5069 clp->rpc_ops->version, clp->cl_minorversion, 5070 nfs4_client_id_uniquifier, 5071 clp->cl_rpcclient->cl_nodename); 5072 if (result >= len) { 5073 kfree(str); 5074 return -EINVAL; 5075 } 5076 clp->cl_owner_id = str; 5077 return 0; 5078 } 5079 5080 static int 5081 nfs4_init_uniform_client_string(struct nfs_client *clp) 5082 { 5083 int result; 5084 size_t len; 5085 char *str; 5086 5087 if (clp->cl_owner_id != NULL) 5088 return 0; 5089 5090 if (nfs4_client_id_uniquifier[0] != '\0') 5091 return nfs4_init_uniquifier_client_string(clp); 5092 5093 len = 10 + 10 + 1 + 10 + 1 + 5094 strlen(clp->cl_rpcclient->cl_nodename) + 1; 5095 5096 if (len > NFS4_OPAQUE_LIMIT + 1) 5097 return -EINVAL; 5098 5099 /* 5100 * Since this string is allocated at mount time, and held until the 5101 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 5102 * about a memory-reclaim deadlock. 5103 */ 5104 str = kmalloc(len, GFP_KERNEL); 5105 if (!str) 5106 return -ENOMEM; 5107 5108 result = scnprintf(str, len, "Linux NFSv%u.%u %s", 5109 clp->rpc_ops->version, clp->cl_minorversion, 5110 clp->cl_rpcclient->cl_nodename); 5111 if (result >= len) { 5112 kfree(str); 5113 return -EINVAL; 5114 } 5115 clp->cl_owner_id = str; 5116 return 0; 5117 } 5118 5119 /* 5120 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback 5121 * services. Advertise one based on the address family of the 5122 * clientaddr. 5123 */ 5124 static unsigned int 5125 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) 5126 { 5127 if (strchr(clp->cl_ipaddr, ':') != NULL) 5128 return scnprintf(buf, len, "tcp6"); 5129 else 5130 return scnprintf(buf, len, "tcp"); 5131 } 5132 5133 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) 5134 { 5135 struct nfs4_setclientid *sc = calldata; 5136 5137 if (task->tk_status == 0) 5138 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); 5139 } 5140 5141 static const struct rpc_call_ops nfs4_setclientid_ops = { 5142 .rpc_call_done = nfs4_setclientid_done, 5143 }; 5144 5145 /** 5146 * nfs4_proc_setclientid - Negotiate client ID 5147 * @clp: state data structure 5148 * @program: RPC program for NFSv4 callback service 5149 * @port: IP port number for NFS4 callback service 5150 * @cred: RPC credential to use for this call 5151 * @res: where to place the result 5152 * 5153 * Returns zero, a negative errno, or a negative NFS4ERR status code. 5154 */ 5155 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, 5156 unsigned short port, struct rpc_cred *cred, 5157 struct nfs4_setclientid_res *res) 5158 { 5159 nfs4_verifier sc_verifier; 5160 struct nfs4_setclientid setclientid = { 5161 .sc_verifier = &sc_verifier, 5162 .sc_prog = program, 5163 .sc_clnt = clp, 5164 }; 5165 struct rpc_message msg = { 5166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], 5167 .rpc_argp = &setclientid, 5168 .rpc_resp = res, 5169 .rpc_cred = cred, 5170 }; 5171 struct rpc_task *task; 5172 struct rpc_task_setup task_setup_data = { 5173 .rpc_client = clp->cl_rpcclient, 5174 .rpc_message = &msg, 5175 .callback_ops = &nfs4_setclientid_ops, 5176 .callback_data = &setclientid, 5177 .flags = RPC_TASK_TIMEOUT, 5178 }; 5179 int status; 5180 5181 /* nfs_client_id4 */ 5182 nfs4_init_boot_verifier(clp, &sc_verifier); 5183 5184 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) 5185 status = nfs4_init_uniform_client_string(clp); 5186 else 5187 status = nfs4_init_nonuniform_client_string(clp); 5188 5189 if (status) 5190 goto out; 5191 5192 /* cb_client4 */ 5193 setclientid.sc_netid_len = 5194 nfs4_init_callback_netid(clp, 5195 setclientid.sc_netid, 5196 sizeof(setclientid.sc_netid)); 5197 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, 5198 sizeof(setclientid.sc_uaddr), "%s.%u.%u", 5199 clp->cl_ipaddr, port >> 8, port & 255); 5200 5201 dprintk("NFS call setclientid auth=%s, '%s'\n", 5202 clp->cl_rpcclient->cl_auth->au_ops->au_name, 5203 clp->cl_owner_id); 5204 task = rpc_run_task(&task_setup_data); 5205 if (IS_ERR(task)) { 5206 status = PTR_ERR(task); 5207 goto out; 5208 } 5209 status = task->tk_status; 5210 if (setclientid.sc_cred) { 5211 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); 5212 put_rpccred(setclientid.sc_cred); 5213 } 5214 rpc_put_task(task); 5215 out: 5216 trace_nfs4_setclientid(clp, status); 5217 dprintk("NFS reply setclientid: %d\n", status); 5218 return status; 5219 } 5220 5221 /** 5222 * nfs4_proc_setclientid_confirm - Confirm client ID 5223 * @clp: state data structure 5224 * @res: result of a previous SETCLIENTID 5225 * @cred: RPC credential to use for this call 5226 * 5227 * Returns zero, a negative errno, or a negative NFS4ERR status code. 5228 */ 5229 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, 5230 struct nfs4_setclientid_res *arg, 5231 struct rpc_cred *cred) 5232 { 5233 struct rpc_message msg = { 5234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], 5235 .rpc_argp = arg, 5236 .rpc_cred = cred, 5237 }; 5238 int status; 5239 5240 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", 5241 clp->cl_rpcclient->cl_auth->au_ops->au_name, 5242 clp->cl_clientid); 5243 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 5244 trace_nfs4_setclientid_confirm(clp, status); 5245 dprintk("NFS reply setclientid_confirm: %d\n", status); 5246 return status; 5247 } 5248 5249 struct nfs4_delegreturndata { 5250 struct nfs4_delegreturnargs args; 5251 struct nfs4_delegreturnres res; 5252 struct nfs_fh fh; 5253 nfs4_stateid stateid; 5254 unsigned long timestamp; 5255 struct nfs_fattr fattr; 5256 int rpc_status; 5257 struct inode *inode; 5258 bool roc; 5259 u32 roc_barrier; 5260 }; 5261 5262 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) 5263 { 5264 struct nfs4_delegreturndata *data = calldata; 5265 5266 if (!nfs4_sequence_done(task, &data->res.seq_res)) 5267 return; 5268 5269 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); 5270 switch (task->tk_status) { 5271 case 0: 5272 renew_lease(data->res.server, data->timestamp); 5273 case -NFS4ERR_ADMIN_REVOKED: 5274 case -NFS4ERR_DELEG_REVOKED: 5275 case -NFS4ERR_BAD_STATEID: 5276 case -NFS4ERR_OLD_STATEID: 5277 case -NFS4ERR_STALE_STATEID: 5278 case -NFS4ERR_EXPIRED: 5279 task->tk_status = 0; 5280 if (data->roc) 5281 pnfs_roc_set_barrier(data->inode, data->roc_barrier); 5282 break; 5283 default: 5284 if (nfs4_async_handle_error(task, data->res.server, 5285 NULL, NULL) == -EAGAIN) { 5286 rpc_restart_call_prepare(task); 5287 return; 5288 } 5289 } 5290 data->rpc_status = task->tk_status; 5291 } 5292 5293 static void nfs4_delegreturn_release(void *calldata) 5294 { 5295 struct nfs4_delegreturndata *data = calldata; 5296 struct inode *inode = data->inode; 5297 5298 if (inode) { 5299 if (data->roc) 5300 pnfs_roc_release(inode); 5301 nfs_iput_and_deactive(inode); 5302 } 5303 kfree(calldata); 5304 } 5305 5306 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) 5307 { 5308 struct nfs4_delegreturndata *d_data; 5309 5310 d_data = (struct nfs4_delegreturndata *)data; 5311 5312 if (d_data->roc && 5313 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task)) 5314 return; 5315 5316 nfs4_setup_sequence(d_data->res.server, 5317 &d_data->args.seq_args, 5318 &d_data->res.seq_res, 5319 task); 5320 } 5321 5322 static const struct rpc_call_ops nfs4_delegreturn_ops = { 5323 .rpc_call_prepare = nfs4_delegreturn_prepare, 5324 .rpc_call_done = nfs4_delegreturn_done, 5325 .rpc_release = nfs4_delegreturn_release, 5326 }; 5327 5328 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) 5329 { 5330 struct nfs4_delegreturndata *data; 5331 struct nfs_server *server = NFS_SERVER(inode); 5332 struct rpc_task *task; 5333 struct rpc_message msg = { 5334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], 5335 .rpc_cred = cred, 5336 }; 5337 struct rpc_task_setup task_setup_data = { 5338 .rpc_client = server->client, 5339 .rpc_message = &msg, 5340 .callback_ops = &nfs4_delegreturn_ops, 5341 .flags = RPC_TASK_ASYNC, 5342 }; 5343 int status = 0; 5344 5345 data = kzalloc(sizeof(*data), GFP_NOFS); 5346 if (data == NULL) 5347 return -ENOMEM; 5348 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); 5349 data->args.fhandle = &data->fh; 5350 data->args.stateid = &data->stateid; 5351 data->args.bitmask = server->cache_consistency_bitmask; 5352 nfs_copy_fh(&data->fh, NFS_FH(inode)); 5353 nfs4_stateid_copy(&data->stateid, stateid); 5354 data->res.fattr = &data->fattr; 5355 data->res.server = server; 5356 nfs_fattr_init(data->res.fattr); 5357 data->timestamp = jiffies; 5358 data->rpc_status = 0; 5359 data->inode = nfs_igrab_and_active(inode); 5360 if (data->inode) 5361 data->roc = nfs4_roc(inode); 5362 5363 task_setup_data.callback_data = data; 5364 msg.rpc_argp = &data->args; 5365 msg.rpc_resp = &data->res; 5366 task = rpc_run_task(&task_setup_data); 5367 if (IS_ERR(task)) 5368 return PTR_ERR(task); 5369 if (!issync) 5370 goto out; 5371 status = nfs4_wait_for_completion_rpc_task(task); 5372 if (status != 0) 5373 goto out; 5374 status = data->rpc_status; 5375 if (status == 0) 5376 nfs_post_op_update_inode_force_wcc(inode, &data->fattr); 5377 else 5378 nfs_refresh_inode(inode, &data->fattr); 5379 out: 5380 rpc_put_task(task); 5381 return status; 5382 } 5383 5384 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) 5385 { 5386 struct nfs_server *server = NFS_SERVER(inode); 5387 struct nfs4_exception exception = { }; 5388 int err; 5389 do { 5390 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); 5391 trace_nfs4_delegreturn(inode, err); 5392 switch (err) { 5393 case -NFS4ERR_STALE_STATEID: 5394 case -NFS4ERR_EXPIRED: 5395 case 0: 5396 return 0; 5397 } 5398 err = nfs4_handle_exception(server, err, &exception); 5399 } while (exception.retry); 5400 return err; 5401 } 5402 5403 #define NFS4_LOCK_MINTIMEOUT (1 * HZ) 5404 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) 5405 5406 /* 5407 * sleep, with exponential backoff, and retry the LOCK operation. 5408 */ 5409 static unsigned long 5410 nfs4_set_lock_task_retry(unsigned long timeout) 5411 { 5412 freezable_schedule_timeout_killable_unsafe(timeout); 5413 timeout <<= 1; 5414 if (timeout > NFS4_LOCK_MAXTIMEOUT) 5415 return NFS4_LOCK_MAXTIMEOUT; 5416 return timeout; 5417 } 5418 5419 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 5420 { 5421 struct inode *inode = state->inode; 5422 struct nfs_server *server = NFS_SERVER(inode); 5423 struct nfs_client *clp = server->nfs_client; 5424 struct nfs_lockt_args arg = { 5425 .fh = NFS_FH(inode), 5426 .fl = request, 5427 }; 5428 struct nfs_lockt_res res = { 5429 .denied = request, 5430 }; 5431 struct rpc_message msg = { 5432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], 5433 .rpc_argp = &arg, 5434 .rpc_resp = &res, 5435 .rpc_cred = state->owner->so_cred, 5436 }; 5437 struct nfs4_lock_state *lsp; 5438 int status; 5439 5440 arg.lock_owner.clientid = clp->cl_clientid; 5441 status = nfs4_set_lock_state(state, request); 5442 if (status != 0) 5443 goto out; 5444 lsp = request->fl_u.nfs4_fl.owner; 5445 arg.lock_owner.id = lsp->ls_seqid.owner_id; 5446 arg.lock_owner.s_dev = server->s_dev; 5447 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 5448 switch (status) { 5449 case 0: 5450 request->fl_type = F_UNLCK; 5451 break; 5452 case -NFS4ERR_DENIED: 5453 status = 0; 5454 } 5455 request->fl_ops->fl_release_private(request); 5456 request->fl_ops = NULL; 5457 out: 5458 return status; 5459 } 5460 5461 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 5462 { 5463 struct nfs4_exception exception = { }; 5464 int err; 5465 5466 do { 5467 err = _nfs4_proc_getlk(state, cmd, request); 5468 trace_nfs4_get_lock(request, state, cmd, err); 5469 err = nfs4_handle_exception(NFS_SERVER(state->inode), err, 5470 &exception); 5471 } while (exception.retry); 5472 return err; 5473 } 5474 5475 static int do_vfs_lock(struct inode *inode, struct file_lock *fl) 5476 { 5477 int res = 0; 5478 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 5479 case FL_POSIX: 5480 res = posix_lock_inode_wait(inode, fl); 5481 break; 5482 case FL_FLOCK: 5483 res = flock_lock_inode_wait(inode, fl); 5484 break; 5485 default: 5486 BUG(); 5487 } 5488 return res; 5489 } 5490 5491 struct nfs4_unlockdata { 5492 struct nfs_locku_args arg; 5493 struct nfs_locku_res res; 5494 struct nfs4_lock_state *lsp; 5495 struct nfs_open_context *ctx; 5496 struct file_lock fl; 5497 const struct nfs_server *server; 5498 unsigned long timestamp; 5499 }; 5500 5501 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, 5502 struct nfs_open_context *ctx, 5503 struct nfs4_lock_state *lsp, 5504 struct nfs_seqid *seqid) 5505 { 5506 struct nfs4_unlockdata *p; 5507 struct inode *inode = lsp->ls_state->inode; 5508 5509 p = kzalloc(sizeof(*p), GFP_NOFS); 5510 if (p == NULL) 5511 return NULL; 5512 p->arg.fh = NFS_FH(inode); 5513 p->arg.fl = &p->fl; 5514 p->arg.seqid = seqid; 5515 p->res.seqid = seqid; 5516 p->lsp = lsp; 5517 atomic_inc(&lsp->ls_count); 5518 /* Ensure we don't close file until we're done freeing locks! */ 5519 p->ctx = get_nfs_open_context(ctx); 5520 memcpy(&p->fl, fl, sizeof(p->fl)); 5521 p->server = NFS_SERVER(inode); 5522 return p; 5523 } 5524 5525 static void nfs4_locku_release_calldata(void *data) 5526 { 5527 struct nfs4_unlockdata *calldata = data; 5528 nfs_free_seqid(calldata->arg.seqid); 5529 nfs4_put_lock_state(calldata->lsp); 5530 put_nfs_open_context(calldata->ctx); 5531 kfree(calldata); 5532 } 5533 5534 static void nfs4_locku_done(struct rpc_task *task, void *data) 5535 { 5536 struct nfs4_unlockdata *calldata = data; 5537 5538 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 5539 return; 5540 switch (task->tk_status) { 5541 case 0: 5542 renew_lease(calldata->server, calldata->timestamp); 5543 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl); 5544 if (nfs4_update_lock_stateid(calldata->lsp, 5545 &calldata->res.stateid)) 5546 break; 5547 case -NFS4ERR_BAD_STATEID: 5548 case -NFS4ERR_OLD_STATEID: 5549 case -NFS4ERR_STALE_STATEID: 5550 case -NFS4ERR_EXPIRED: 5551 if (!nfs4_stateid_match(&calldata->arg.stateid, 5552 &calldata->lsp->ls_stateid)) 5553 rpc_restart_call_prepare(task); 5554 break; 5555 default: 5556 if (nfs4_async_handle_error(task, calldata->server, 5557 NULL, NULL) == -EAGAIN) 5558 rpc_restart_call_prepare(task); 5559 } 5560 nfs_release_seqid(calldata->arg.seqid); 5561 } 5562 5563 static void nfs4_locku_prepare(struct rpc_task *task, void *data) 5564 { 5565 struct nfs4_unlockdata *calldata = data; 5566 5567 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 5568 goto out_wait; 5569 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid); 5570 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { 5571 /* Note: exit _without_ running nfs4_locku_done */ 5572 goto out_no_action; 5573 } 5574 calldata->timestamp = jiffies; 5575 if (nfs4_setup_sequence(calldata->server, 5576 &calldata->arg.seq_args, 5577 &calldata->res.seq_res, 5578 task) != 0) 5579 nfs_release_seqid(calldata->arg.seqid); 5580 return; 5581 out_no_action: 5582 task->tk_action = NULL; 5583 out_wait: 5584 nfs4_sequence_done(task, &calldata->res.seq_res); 5585 } 5586 5587 static const struct rpc_call_ops nfs4_locku_ops = { 5588 .rpc_call_prepare = nfs4_locku_prepare, 5589 .rpc_call_done = nfs4_locku_done, 5590 .rpc_release = nfs4_locku_release_calldata, 5591 }; 5592 5593 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, 5594 struct nfs_open_context *ctx, 5595 struct nfs4_lock_state *lsp, 5596 struct nfs_seqid *seqid) 5597 { 5598 struct nfs4_unlockdata *data; 5599 struct rpc_message msg = { 5600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], 5601 .rpc_cred = ctx->cred, 5602 }; 5603 struct rpc_task_setup task_setup_data = { 5604 .rpc_client = NFS_CLIENT(lsp->ls_state->inode), 5605 .rpc_message = &msg, 5606 .callback_ops = &nfs4_locku_ops, 5607 .workqueue = nfsiod_workqueue, 5608 .flags = RPC_TASK_ASYNC, 5609 }; 5610 5611 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client, 5612 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg); 5613 5614 /* Ensure this is an unlock - when canceling a lock, the 5615 * canceled lock is passed in, and it won't be an unlock. 5616 */ 5617 fl->fl_type = F_UNLCK; 5618 5619 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); 5620 if (data == NULL) { 5621 nfs_free_seqid(seqid); 5622 return ERR_PTR(-ENOMEM); 5623 } 5624 5625 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); 5626 msg.rpc_argp = &data->arg; 5627 msg.rpc_resp = &data->res; 5628 task_setup_data.callback_data = data; 5629 return rpc_run_task(&task_setup_data); 5630 } 5631 5632 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) 5633 { 5634 struct inode *inode = state->inode; 5635 struct nfs4_state_owner *sp = state->owner; 5636 struct nfs_inode *nfsi = NFS_I(inode); 5637 struct nfs_seqid *seqid; 5638 struct nfs4_lock_state *lsp; 5639 struct rpc_task *task; 5640 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 5641 int status = 0; 5642 unsigned char fl_flags = request->fl_flags; 5643 5644 status = nfs4_set_lock_state(state, request); 5645 /* Unlock _before_ we do the RPC call */ 5646 request->fl_flags |= FL_EXISTS; 5647 /* Exclude nfs_delegation_claim_locks() */ 5648 mutex_lock(&sp->so_delegreturn_mutex); 5649 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ 5650 down_read(&nfsi->rwsem); 5651 if (do_vfs_lock(inode, request) == -ENOENT) { 5652 up_read(&nfsi->rwsem); 5653 mutex_unlock(&sp->so_delegreturn_mutex); 5654 goto out; 5655 } 5656 up_read(&nfsi->rwsem); 5657 mutex_unlock(&sp->so_delegreturn_mutex); 5658 if (status != 0) 5659 goto out; 5660 /* Is this a delegated lock? */ 5661 lsp = request->fl_u.nfs4_fl.owner; 5662 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) 5663 goto out; 5664 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid; 5665 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); 5666 status = -ENOMEM; 5667 if (IS_ERR(seqid)) 5668 goto out; 5669 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); 5670 status = PTR_ERR(task); 5671 if (IS_ERR(task)) 5672 goto out; 5673 status = nfs4_wait_for_completion_rpc_task(task); 5674 rpc_put_task(task); 5675 out: 5676 request->fl_flags = fl_flags; 5677 trace_nfs4_unlock(request, state, F_SETLK, status); 5678 return status; 5679 } 5680 5681 struct nfs4_lockdata { 5682 struct nfs_lock_args arg; 5683 struct nfs_lock_res res; 5684 struct nfs4_lock_state *lsp; 5685 struct nfs_open_context *ctx; 5686 struct file_lock fl; 5687 unsigned long timestamp; 5688 int rpc_status; 5689 int cancelled; 5690 struct nfs_server *server; 5691 }; 5692 5693 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, 5694 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, 5695 gfp_t gfp_mask) 5696 { 5697 struct nfs4_lockdata *p; 5698 struct inode *inode = lsp->ls_state->inode; 5699 struct nfs_server *server = NFS_SERVER(inode); 5700 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 5701 5702 p = kzalloc(sizeof(*p), gfp_mask); 5703 if (p == NULL) 5704 return NULL; 5705 5706 p->arg.fh = NFS_FH(inode); 5707 p->arg.fl = &p->fl; 5708 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); 5709 if (IS_ERR(p->arg.open_seqid)) 5710 goto out_free; 5711 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 5712 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask); 5713 if (IS_ERR(p->arg.lock_seqid)) 5714 goto out_free_seqid; 5715 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; 5716 p->arg.lock_owner.id = lsp->ls_seqid.owner_id; 5717 p->arg.lock_owner.s_dev = server->s_dev; 5718 p->res.lock_seqid = p->arg.lock_seqid; 5719 p->lsp = lsp; 5720 p->server = server; 5721 atomic_inc(&lsp->ls_count); 5722 p->ctx = get_nfs_open_context(ctx); 5723 get_file(fl->fl_file); 5724 memcpy(&p->fl, fl, sizeof(p->fl)); 5725 return p; 5726 out_free_seqid: 5727 nfs_free_seqid(p->arg.open_seqid); 5728 out_free: 5729 kfree(p); 5730 return NULL; 5731 } 5732 5733 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) 5734 { 5735 struct nfs4_lockdata *data = calldata; 5736 struct nfs4_state *state = data->lsp->ls_state; 5737 5738 dprintk("%s: begin!\n", __func__); 5739 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) 5740 goto out_wait; 5741 /* Do we need to do an open_to_lock_owner? */ 5742 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) { 5743 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { 5744 goto out_release_lock_seqid; 5745 } 5746 nfs4_stateid_copy(&data->arg.open_stateid, 5747 &state->open_stateid); 5748 data->arg.new_lock_owner = 1; 5749 data->res.open_seqid = data->arg.open_seqid; 5750 } else { 5751 data->arg.new_lock_owner = 0; 5752 nfs4_stateid_copy(&data->arg.lock_stateid, 5753 &data->lsp->ls_stateid); 5754 } 5755 if (!nfs4_valid_open_stateid(state)) { 5756 data->rpc_status = -EBADF; 5757 task->tk_action = NULL; 5758 goto out_release_open_seqid; 5759 } 5760 data->timestamp = jiffies; 5761 if (nfs4_setup_sequence(data->server, 5762 &data->arg.seq_args, 5763 &data->res.seq_res, 5764 task) == 0) 5765 return; 5766 out_release_open_seqid: 5767 nfs_release_seqid(data->arg.open_seqid); 5768 out_release_lock_seqid: 5769 nfs_release_seqid(data->arg.lock_seqid); 5770 out_wait: 5771 nfs4_sequence_done(task, &data->res.seq_res); 5772 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); 5773 } 5774 5775 static void nfs4_lock_done(struct rpc_task *task, void *calldata) 5776 { 5777 struct nfs4_lockdata *data = calldata; 5778 struct nfs4_lock_state *lsp = data->lsp; 5779 5780 dprintk("%s: begin!\n", __func__); 5781 5782 if (!nfs4_sequence_done(task, &data->res.seq_res)) 5783 return; 5784 5785 data->rpc_status = task->tk_status; 5786 switch (task->tk_status) { 5787 case 0: 5788 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)), 5789 data->timestamp); 5790 if (data->arg.new_lock) { 5791 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS); 5792 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) { 5793 rpc_restart_call_prepare(task); 5794 break; 5795 } 5796 } 5797 if (data->arg.new_lock_owner != 0) { 5798 nfs_confirm_seqid(&lsp->ls_seqid, 0); 5799 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid); 5800 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 5801 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid)) 5802 rpc_restart_call_prepare(task); 5803 break; 5804 case -NFS4ERR_BAD_STATEID: 5805 case -NFS4ERR_OLD_STATEID: 5806 case -NFS4ERR_STALE_STATEID: 5807 case -NFS4ERR_EXPIRED: 5808 if (data->arg.new_lock_owner != 0) { 5809 if (!nfs4_stateid_match(&data->arg.open_stateid, 5810 &lsp->ls_state->open_stateid)) 5811 rpc_restart_call_prepare(task); 5812 } else if (!nfs4_stateid_match(&data->arg.lock_stateid, 5813 &lsp->ls_stateid)) 5814 rpc_restart_call_prepare(task); 5815 } 5816 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); 5817 } 5818 5819 static void nfs4_lock_release(void *calldata) 5820 { 5821 struct nfs4_lockdata *data = calldata; 5822 5823 dprintk("%s: begin!\n", __func__); 5824 nfs_free_seqid(data->arg.open_seqid); 5825 if (data->cancelled != 0) { 5826 struct rpc_task *task; 5827 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, 5828 data->arg.lock_seqid); 5829 if (!IS_ERR(task)) 5830 rpc_put_task_async(task); 5831 dprintk("%s: cancelling lock!\n", __func__); 5832 } else 5833 nfs_free_seqid(data->arg.lock_seqid); 5834 nfs4_put_lock_state(data->lsp); 5835 put_nfs_open_context(data->ctx); 5836 fput(data->fl.fl_file); 5837 kfree(data); 5838 dprintk("%s: done!\n", __func__); 5839 } 5840 5841 static const struct rpc_call_ops nfs4_lock_ops = { 5842 .rpc_call_prepare = nfs4_lock_prepare, 5843 .rpc_call_done = nfs4_lock_done, 5844 .rpc_release = nfs4_lock_release, 5845 }; 5846 5847 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) 5848 { 5849 switch (error) { 5850 case -NFS4ERR_ADMIN_REVOKED: 5851 case -NFS4ERR_BAD_STATEID: 5852 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 5853 if (new_lock_owner != 0 || 5854 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) 5855 nfs4_schedule_stateid_recovery(server, lsp->ls_state); 5856 break; 5857 case -NFS4ERR_STALE_STATEID: 5858 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 5859 case -NFS4ERR_EXPIRED: 5860 nfs4_schedule_lease_recovery(server->nfs_client); 5861 }; 5862 } 5863 5864 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) 5865 { 5866 struct nfs4_lockdata *data; 5867 struct rpc_task *task; 5868 struct rpc_message msg = { 5869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], 5870 .rpc_cred = state->owner->so_cred, 5871 }; 5872 struct rpc_task_setup task_setup_data = { 5873 .rpc_client = NFS_CLIENT(state->inode), 5874 .rpc_message = &msg, 5875 .callback_ops = &nfs4_lock_ops, 5876 .workqueue = nfsiod_workqueue, 5877 .flags = RPC_TASK_ASYNC, 5878 }; 5879 int ret; 5880 5881 dprintk("%s: begin!\n", __func__); 5882 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), 5883 fl->fl_u.nfs4_fl.owner, 5884 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS); 5885 if (data == NULL) 5886 return -ENOMEM; 5887 if (IS_SETLKW(cmd)) 5888 data->arg.block = 1; 5889 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); 5890 msg.rpc_argp = &data->arg; 5891 msg.rpc_resp = &data->res; 5892 task_setup_data.callback_data = data; 5893 if (recovery_type > NFS_LOCK_NEW) { 5894 if (recovery_type == NFS_LOCK_RECLAIM) 5895 data->arg.reclaim = NFS_LOCK_RECLAIM; 5896 nfs4_set_sequence_privileged(&data->arg.seq_args); 5897 } else 5898 data->arg.new_lock = 1; 5899 task = rpc_run_task(&task_setup_data); 5900 if (IS_ERR(task)) 5901 return PTR_ERR(task); 5902 ret = nfs4_wait_for_completion_rpc_task(task); 5903 if (ret == 0) { 5904 ret = data->rpc_status; 5905 if (ret) 5906 nfs4_handle_setlk_error(data->server, data->lsp, 5907 data->arg.new_lock_owner, ret); 5908 } else 5909 data->cancelled = 1; 5910 rpc_put_task(task); 5911 dprintk("%s: done, ret = %d!\n", __func__, ret); 5912 return ret; 5913 } 5914 5915 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) 5916 { 5917 struct nfs_server *server = NFS_SERVER(state->inode); 5918 struct nfs4_exception exception = { 5919 .inode = state->inode, 5920 }; 5921 int err; 5922 5923 do { 5924 /* Cache the lock if possible... */ 5925 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 5926 return 0; 5927 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); 5928 trace_nfs4_lock_reclaim(request, state, F_SETLK, err); 5929 if (err != -NFS4ERR_DELAY) 5930 break; 5931 nfs4_handle_exception(server, err, &exception); 5932 } while (exception.retry); 5933 return err; 5934 } 5935 5936 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) 5937 { 5938 struct nfs_server *server = NFS_SERVER(state->inode); 5939 struct nfs4_exception exception = { 5940 .inode = state->inode, 5941 }; 5942 int err; 5943 5944 err = nfs4_set_lock_state(state, request); 5945 if (err != 0) 5946 return err; 5947 if (!recover_lost_locks) { 5948 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); 5949 return 0; 5950 } 5951 do { 5952 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 5953 return 0; 5954 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); 5955 trace_nfs4_lock_expired(request, state, F_SETLK, err); 5956 switch (err) { 5957 default: 5958 goto out; 5959 case -NFS4ERR_GRACE: 5960 case -NFS4ERR_DELAY: 5961 nfs4_handle_exception(server, err, &exception); 5962 err = 0; 5963 } 5964 } while (exception.retry); 5965 out: 5966 return err; 5967 } 5968 5969 #if defined(CONFIG_NFS_V4_1) 5970 /** 5971 * nfs41_check_expired_locks - possibly free a lock stateid 5972 * 5973 * @state: NFSv4 state for an inode 5974 * 5975 * Returns NFS_OK if recovery for this stateid is now finished. 5976 * Otherwise a negative NFS4ERR value is returned. 5977 */ 5978 static int nfs41_check_expired_locks(struct nfs4_state *state) 5979 { 5980 int status, ret = -NFS4ERR_BAD_STATEID; 5981 struct nfs4_lock_state *lsp; 5982 struct nfs_server *server = NFS_SERVER(state->inode); 5983 5984 list_for_each_entry(lsp, &state->lock_states, ls_locks) { 5985 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { 5986 struct rpc_cred *cred = lsp->ls_state->owner->so_cred; 5987 5988 status = nfs41_test_stateid(server, 5989 &lsp->ls_stateid, 5990 cred); 5991 trace_nfs4_test_lock_stateid(state, lsp, status); 5992 if (status != NFS_OK) { 5993 /* Free the stateid unless the server 5994 * informs us the stateid is unrecognized. */ 5995 if (status != -NFS4ERR_BAD_STATEID) 5996 nfs41_free_stateid(server, 5997 &lsp->ls_stateid, 5998 cred); 5999 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 6000 ret = status; 6001 } 6002 } 6003 }; 6004 6005 return ret; 6006 } 6007 6008 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) 6009 { 6010 int status = NFS_OK; 6011 6012 if (test_bit(LK_STATE_IN_USE, &state->flags)) 6013 status = nfs41_check_expired_locks(state); 6014 if (status != NFS_OK) 6015 status = nfs4_lock_expired(state, request); 6016 return status; 6017 } 6018 #endif 6019 6020 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6021 { 6022 struct nfs_inode *nfsi = NFS_I(state->inode); 6023 unsigned char fl_flags = request->fl_flags; 6024 int status = -ENOLCK; 6025 6026 if ((fl_flags & FL_POSIX) && 6027 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) 6028 goto out; 6029 /* Is this a delegated open? */ 6030 status = nfs4_set_lock_state(state, request); 6031 if (status != 0) 6032 goto out; 6033 request->fl_flags |= FL_ACCESS; 6034 status = do_vfs_lock(state->inode, request); 6035 if (status < 0) 6036 goto out; 6037 down_read(&nfsi->rwsem); 6038 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { 6039 /* Yes: cache locks! */ 6040 /* ...but avoid races with delegation recall... */ 6041 request->fl_flags = fl_flags & ~FL_SLEEP; 6042 status = do_vfs_lock(state->inode, request); 6043 up_read(&nfsi->rwsem); 6044 goto out; 6045 } 6046 up_read(&nfsi->rwsem); 6047 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); 6048 out: 6049 request->fl_flags = fl_flags; 6050 return status; 6051 } 6052 6053 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6054 { 6055 struct nfs4_exception exception = { 6056 .state = state, 6057 .inode = state->inode, 6058 }; 6059 int err; 6060 6061 do { 6062 err = _nfs4_proc_setlk(state, cmd, request); 6063 trace_nfs4_set_lock(request, state, cmd, err); 6064 if (err == -NFS4ERR_DENIED) 6065 err = -EAGAIN; 6066 err = nfs4_handle_exception(NFS_SERVER(state->inode), 6067 err, &exception); 6068 } while (exception.retry); 6069 return err; 6070 } 6071 6072 static int 6073 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) 6074 { 6075 struct nfs_open_context *ctx; 6076 struct nfs4_state *state; 6077 unsigned long timeout = NFS4_LOCK_MINTIMEOUT; 6078 int status; 6079 6080 /* verify open state */ 6081 ctx = nfs_file_open_context(filp); 6082 state = ctx->state; 6083 6084 if (request->fl_start < 0 || request->fl_end < 0) 6085 return -EINVAL; 6086 6087 if (IS_GETLK(cmd)) { 6088 if (state != NULL) 6089 return nfs4_proc_getlk(state, F_GETLK, request); 6090 return 0; 6091 } 6092 6093 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) 6094 return -EINVAL; 6095 6096 if (request->fl_type == F_UNLCK) { 6097 if (state != NULL) 6098 return nfs4_proc_unlck(state, cmd, request); 6099 return 0; 6100 } 6101 6102 if (state == NULL) 6103 return -ENOLCK; 6104 /* 6105 * Don't rely on the VFS having checked the file open mode, 6106 * since it won't do this for flock() locks. 6107 */ 6108 switch (request->fl_type) { 6109 case F_RDLCK: 6110 if (!(filp->f_mode & FMODE_READ)) 6111 return -EBADF; 6112 break; 6113 case F_WRLCK: 6114 if (!(filp->f_mode & FMODE_WRITE)) 6115 return -EBADF; 6116 } 6117 6118 do { 6119 status = nfs4_proc_setlk(state, cmd, request); 6120 if ((status != -EAGAIN) || IS_SETLK(cmd)) 6121 break; 6122 timeout = nfs4_set_lock_task_retry(timeout); 6123 status = -ERESTARTSYS; 6124 if (signalled()) 6125 break; 6126 } while(status < 0); 6127 return status; 6128 } 6129 6130 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) 6131 { 6132 struct nfs_server *server = NFS_SERVER(state->inode); 6133 int err; 6134 6135 err = nfs4_set_lock_state(state, fl); 6136 if (err != 0) 6137 return err; 6138 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); 6139 return nfs4_handle_delegation_recall_error(server, state, stateid, err); 6140 } 6141 6142 struct nfs_release_lockowner_data { 6143 struct nfs4_lock_state *lsp; 6144 struct nfs_server *server; 6145 struct nfs_release_lockowner_args args; 6146 struct nfs_release_lockowner_res res; 6147 unsigned long timestamp; 6148 }; 6149 6150 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata) 6151 { 6152 struct nfs_release_lockowner_data *data = calldata; 6153 struct nfs_server *server = data->server; 6154 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, 6155 &data->args.seq_args, &data->res.seq_res, task); 6156 data->args.lock_owner.clientid = server->nfs_client->cl_clientid; 6157 data->timestamp = jiffies; 6158 } 6159 6160 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata) 6161 { 6162 struct nfs_release_lockowner_data *data = calldata; 6163 struct nfs_server *server = data->server; 6164 6165 nfs40_sequence_done(task, &data->res.seq_res); 6166 6167 switch (task->tk_status) { 6168 case 0: 6169 renew_lease(server, data->timestamp); 6170 break; 6171 case -NFS4ERR_STALE_CLIENTID: 6172 case -NFS4ERR_EXPIRED: 6173 nfs4_schedule_lease_recovery(server->nfs_client); 6174 break; 6175 case -NFS4ERR_LEASE_MOVED: 6176 case -NFS4ERR_DELAY: 6177 if (nfs4_async_handle_error(task, server, 6178 NULL, NULL) == -EAGAIN) 6179 rpc_restart_call_prepare(task); 6180 } 6181 } 6182 6183 static void nfs4_release_lockowner_release(void *calldata) 6184 { 6185 struct nfs_release_lockowner_data *data = calldata; 6186 nfs4_free_lock_state(data->server, data->lsp); 6187 kfree(calldata); 6188 } 6189 6190 static const struct rpc_call_ops nfs4_release_lockowner_ops = { 6191 .rpc_call_prepare = nfs4_release_lockowner_prepare, 6192 .rpc_call_done = nfs4_release_lockowner_done, 6193 .rpc_release = nfs4_release_lockowner_release, 6194 }; 6195 6196 static void 6197 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp) 6198 { 6199 struct nfs_release_lockowner_data *data; 6200 struct rpc_message msg = { 6201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], 6202 }; 6203 6204 if (server->nfs_client->cl_mvops->minor_version != 0) 6205 return; 6206 6207 data = kmalloc(sizeof(*data), GFP_NOFS); 6208 if (!data) 6209 return; 6210 data->lsp = lsp; 6211 data->server = server; 6212 data->args.lock_owner.clientid = server->nfs_client->cl_clientid; 6213 data->args.lock_owner.id = lsp->ls_seqid.owner_id; 6214 data->args.lock_owner.s_dev = server->s_dev; 6215 6216 msg.rpc_argp = &data->args; 6217 msg.rpc_resp = &data->res; 6218 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); 6219 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); 6220 } 6221 6222 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" 6223 6224 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key, 6225 const void *buf, size_t buflen, 6226 int flags, int type) 6227 { 6228 if (strcmp(key, "") != 0) 6229 return -EINVAL; 6230 6231 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen); 6232 } 6233 6234 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key, 6235 void *buf, size_t buflen, int type) 6236 { 6237 if (strcmp(key, "") != 0) 6238 return -EINVAL; 6239 6240 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen); 6241 } 6242 6243 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list, 6244 size_t list_len, const char *name, 6245 size_t name_len, int type) 6246 { 6247 size_t len = sizeof(XATTR_NAME_NFSV4_ACL); 6248 6249 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)))) 6250 return 0; 6251 6252 if (list && len <= list_len) 6253 memcpy(list, XATTR_NAME_NFSV4_ACL, len); 6254 return len; 6255 } 6256 6257 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 6258 static inline int nfs4_server_supports_labels(struct nfs_server *server) 6259 { 6260 return server->caps & NFS_CAP_SECURITY_LABEL; 6261 } 6262 6263 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key, 6264 const void *buf, size_t buflen, 6265